78 General Reciprocity

This section carries reciprocity (Section 77) from \(\mathrm {GL}_2\) over \(\mathbb Q\) to \(\mathrm {GL}_n\) over a number field.

78.1 Link 65: the Satake transform

The Hecke algebra of \(\mathrm {GL}_n\) at \(p\), with convolution, is a ring of symmetric polynomials [981]:

\begin{align*} \mathcal S(f)(t) &= \delta ^{1/2}(t)\int _N f(tn)\,dn, \\ \mathcal H\bigl (\mathrm {GL}_n(\mathbb Q_p), \mathrm {GL}_n(\mathbb Z_p)\bigr ) &\cong \mathbb Z[X_1^{\pm }, \dots , X_n^{\pm }]^{S_n} . \end{align*}

With \(T_p^{(k)}\) the double coset of \(\mathrm {diag}(p, \dots , p, 1, \dots , 1)\) (\(k\) entries \(p\)),

\begin{align*} \mathcal S\bigl (T_p^{(k)}\bigr ) &= p^{k(n-k)/2}\,e_k(X_1, \dots , X_n), \\ H_p(X) &= \sum _{k=0}^{n} (-1)^k\,p^{k(k-1)/2}\,T_p^{(k)}\,X^{n-k} . \end{align*}

For \(n = 2\): \(H_p(X) = X^2 - T_p X + p\,S_p\).

78.2 Link 66: the congruence relation

Link 49 is the case \(n = 2\) of

\begin{align*} H_p(\mathrm {Frob}_p) &= 0 \end{align*}

on the cohomology of a Shimura variety. If \(\pi _p\) has Satake parameters \(\alpha _1, \dots , \alpha _n\), the characteristic polynomial of \(\mathrm {Frob}_p\) is \(\prod _i (X - \alpha _i)\), and

\begin{align*} \mathrm {tr}\,\rho (\mathrm {Frob}_p) &= \alpha _1 + \cdots + \alpha _n = a_p(\pi ) . \end{align*}

78.3 Link 67: weight \(k\), the Hecke relations and the bound

For a weight-\(k\) eigenform on \(\mathrm {GL}_2/\mathbb Q\),

\begin{align*} H_p(X) &= X^2 - a_p X + p^{k-1}, & \alpha _p + \beta _p &= a_p, & \alpha _p\beta _p &= p^{k-1} . \end{align*}

Put \(s_r := \sum _{i=0}^{r} \alpha ^i \beta ^{r-i}\). Then

\begin{align*} s_{r+1} &= (\alpha + \beta )\,s_r - \alpha \beta \,s_{r-1}, \\ a_{p^{r+1}} &= a_p\,a_{p^r} - p^{k-1}\,a_{p^{r-1}}, & a_{mn} &= a_m\,a_n \quad (\gcd (m,n) = 1) . \end{align*}

The parameters lie on the circle \(|\alpha _p| = p^{(k-1)/2}\) exactly when the discriminant of \(H_p\) is not positive:

\begin{align*} a_p^2 &\leq 4\,p^{k-1} . \end{align*}

For \(\Delta = q\prod _{n\geq 1}(1 - q^n)^{24}\), \(k = 12\) [982]:

\(n\) \(\tau (n)\) relation
2 \(-24\)
3 \(252\)
4 \(-1472\) \(\tau (2)^2 - 2^{11} = 576 - 2048\)
6 \(-6048\) \(\tau (2)\,\tau (3) = -24 \cdot 252\)

For the form of Section 77, \(k = 2\): \(a_4 = a_2^2 - 2 = 2\) and \(a_6 = a_2 a_3 = 2\).

78.4 Link 68: the descent

Let \(F\) be a CM field with totally real subfield \(F^+\) and complex conjugation \(c\). If \(\pi \) on \(\mathrm {GL}_n(\mathbb A_F)\) is regular algebraic and

\begin{align*} \pi ^\vee &\cong \pi ^c , \end{align*}

then \(\pi \) comes from a unitary group \(U(n)\) for \(F/F^+\), whose Shimura variety carries link 66 [983].

78.5 Link 69: the defect

With \(l_0\) as in link 52,

\begin{align*} l_0(\mathrm {GL}_n/F) &= r_1\Bigl (\Bigl \lceil \frac n2\Bigr \rceil - 1\Bigr ) + r_2\,(n-1), \end{align*}

the locally symmetric space is algebraic exactly when \(l_0 = 0\). For \(\mathrm {GL}_n\) itself this is \(n = 1\), or \(n = 2\) over a totally real field. Link 68 trades \(l_0(\mathrm {GL}_n/F) = d(n-1)\) for \(l_0(U(n)/F^+) = 0\), at the price \(\pi ^\vee \cong \pi ^c\).

78.6 Link 70: \(l_0 = 1\)

Cuspidal cohomology spreads over degrees \(q_0, \dots , q_0 + l_0\) with multiplicities \(\binom {l_0}{i}\) [984]. Patching gives a complex of finite free modules over \(S_\infty = \mathcal O[[x_1, \dots , x_q]]\) [985]. For \(l_0 = 1\) its two ranks are equal, so the differential is a square matrix:

\begin{align*} M_\infty &= \bigl [\,M_1 \xrightarrow {\ d\ } M_0\,\bigr ], \qquad \mathrm {rk}\,M_1 = \mathrm {rk}\,M_0, \\ H_1(M_\infty ) = 0 &\iff \det d \neq 0 . \end{align*}
\begin{align*} \det d \neq 0 &\Rightarrow \mathrm {pd}_{S_\infty } H_0 \leq 1 \\ &\Rightarrow \mathrm {depth}\,H_0 \geq q = \dim R_\infty && \text {(Auslander--Buchsbaum)} \\ &\Rightarrow H_0 \text { is maximal Cohen--Macaulay over } R_\infty \\ &\Rightarrow R = \mathbb T . \end{align*}

The cases with \(l_0 = 1\) include \(\mathrm {GL}_2\) over an imaginary quadratic field and \(\mathrm {GL}_3\), \(\mathrm {GL}_4\) over \(\mathbb Q\).

78.7 Link 71: \(l_0 \geq 2\)

Ranks \(\binom {l_0}{i}\) are those of a Koszul complex on \(l_0\) elements:

\begin{align*} M_\infty &\cong K(f_1, \dots , f_{l_0};\,N), \\ H_i(M_\infty ) = 0\ (i > 0) &\iff f_1, \dots , f_{l_0} \text { is a regular sequence on } N, \end{align*}

that is, \(l_0\) steps of link 70, one element at a time. The number of elements is the dimension of a motivic cohomology group [986]:

\begin{align*} \dim H^1_{\mathrm {mot}}\bigl (\mathbb Q, \mathrm {ad}^0\rho (1)\bigr ) &= l_0, & H^*_{\mathrm {cusp}}[\pi ] &\cong \Lambda ^*\bigl (H^1_{\mathrm {mot}}(\mathbb Q,\mathrm {ad}^0\rho (1))^\vee \bigr ) \otimes H^{q_0}_{\mathrm {cusp}}[\pi ] . \end{align*}

Galois representations attached to the torsion classes, without \(\pi ^\vee \cong \pi ^c\), are constructed in [992].

78.8 Link 72: the general statement

\begin{align*} \mathrm {tr}\,\rho _\pi (\mathrm {Frob}_p) &= a_p(\pi ) \quad \text {for almost all } p \\ &\Longleftarrow \begin {cases} l_0 = 0, \ \text {or } \pi ^\vee \cong \pi ^c & \text {(links 66, 68)} \\ l_0 \geq 1: \ f_1, \dots , f_{l_0} \text { regular} & \text {(links 70, 71)} \end {cases} \end{align*}

78.9 Links 73–75: non-abelian reciprocity

Link 73: the smallest non-abelian case. Let \(K\) be the splitting field of \(f(x) = x^3 - x - 1\), with \(\mathrm {Gal}(K/\mathbb Q) = S_3\) and discriminant \(-23\). The two-dimensional representation \(\rho \) of \(S_3\) has character “fixed points minus one”:

\(f \bmod p\) \(\mathrm {Frob}_p\) roots \(r_p\) \(\mathrm {tr}\,\rho \) quadratic form
three linear factors identity 3 \(2\) \(p = x^2 + xy + 6y^2\)
linear \(\times \) quadratic transposition 1 \(0\) \(\bigl (\frac {-23}{p}\bigr ) = -1\)
irreducible 3-cycle 0 \(-1\) \(p = 2x^2 \pm xy + 3y^2\)
\begin{align*} \langle \chi _\rho , \chi _\rho \rangle &= \frac {1}{6}\bigl (2^2 + 3\cdot 0^2 + 2\cdot (-1)^2\bigr ) = 1 , \end{align*}

so \(\rho \) is irreducible, and it is not abelian. On the automorphic side, the weight-one form

\begin{align*} \eta (z)\,\eta (23z) &= q\prod _{n \geq 1}(1 - q^n)(1 - q^{23n}) = \sum _{n \geq 1} a_n\,q^n \end{align*}

has, for every prime \(p \neq 23\),

\begin{align*} a_p &= r_p - 1 = \mathrm {tr}\,\rho (\mathrm {Frob}_p) . \end{align*}

\(p\) \(r_p\) \(a_p\)
2 0 \(-1\)
3 0 \(-1\)
5 1 \(0\)

Three readings of \(p\) agree: the roots of a cubic, a quadratic form, and the coefficients of an eta product.

Link 74: the local case. For \(\mathrm {GL}_n\) over a \(p\)-adic field the correspondence between \(n\)-dimensional Weil–Deligne representations and irreducible representations of \(\mathrm {GL}_n\) is known for every \(n\) [983, 993]: non-abelian local class field theory, with \(n = 1\) the local reciprocity map.

Link 75: two-dimensional Artin representations over \(\mathbb Q\). The projective image of a two-dimensional \(\rho \) is dihedral, \(A_4\), \(S_4\) or \(A_5\). The dihedral case is class field theory of a quadratic field (link 73 is one); \(A_4\) and \(S_4\) follow by base change [994, 995]; and every odd two-dimensional Artin representation of \(\mathbb Q\) is attached to a weight-one form through Serre’s conjecture [996].

78.10 The chain

Link From What appears
65 49, 50 Satake; the Hecke polynomial \(H_p\)
66 49, 65 \(H_p(\mathrm {Frob}_p) = 0\); \(\mathrm {tr} = a_p\)
67 65, 66 Hecke relations; \(a_p^2 \leq 4p^{k-1}\)
68 66 descent for \(\pi ^\vee \cong \pi ^c\)
69 52, 68 the defect \(l_0\); \(l_0 = 0\)
70 53, 69 \(l_0 = 1\): \(\det d \neq 0\)
71 53, 70 \(l_0 \geq 2\): Koszul, a regular sequence
72 66–71 the general statement
73 45, 66 \(S_3\): \(a_p = r_p - 1\) for \(\eta (z)\eta (23z)\)
74 66 local Langlands for \(\mathrm {GL}_n\)
75 68, 73 two-dimensional Artin representations over \(\mathbb Q\)

78.11 Machine check

The integer statements of this section are checked in two more files of the library Work, HeckeChain.lean and NonAbChain.lean, under the same rules as before. All \(28 + 15\) theorems report “does not depend on any axioms” (Listing 16). The Satake parameters are checked numerically by numeric_hecke.py, Python standard library only; every check passes (Listing 20).

Link Check Scope
67 s_succ, hecke_rec every \(r\), any commutative ring
67 hecke_rec_nat, s_nat_values natural numbers
67 deltaList_eq, tau_values \(\tau (n)\), \(n \leq 200\)
67 tau_mult, tau_hecke, tau_ramanujan \(n \leq 200\), primes \(p < 200\)
67 a11_mult, a11_hecke, a11_ramanujan the form of link 51
67 \(\tau (p)^2 \leq 4p^{11}\), \(|\alpha _p| = p^{11/2}\) numerical, 168 primes \(p < 1000\)
67 \(a_p^2 \leq 4p\), \(|\alpha _p| = \sqrt p\) for \(y^2 + y = x^3 - x^2\) numerical, 167 primes \(p < 1000\)
73 fix_values, chi_values, chi_class, chi_norm all of \(S_3\)
73 noncomm \(S_3\) is not commutative
73 a23List_eq, r_values, ap_eq_rp, rp_013 45 primes \(p < 200\), \(p \neq 23\)

What the check rests on. The recursion \(s_{r+2} + \alpha \beta \,s_r = (\alpha +\beta )\,s_{r+1}\) is proved for every \(r\) over any ring given by its operations and laws, written without subtraction. The coefficients of \(\Delta \), of the form of link 51 and of \(\eta (z)\eta (23z)\) are computed as integer lists and compared by rfl up to \(200\). In the numerical check the largest ratio \(|\tau (p)|/(2p^{11/2})\) below \(1000\) is \(0.9594\), at \(p = 103\). Links 65, 66, 68, 70, 71, 74 and 75 are not in these files; the defect of link 69 is the one checked in DefectChain.lean.

Listing 17: HeckeChain.lean: the Hecke recursion, \(\tau (n)\) and the bound.

HeckeChain.lean

/-
  HeckeChain.lean
  Hecke relations (link 67).

  Lean 4 core plus the earlier work/ files (ESChain for the 11a series and
  `primes200`). Proofs are hand-written terms and plain rewriting; no omega /
  simp / ring / decide / norm_num / linarith. Concrete numbers use `rfl`.

  General part: in any commutative semiring given by its operations and laws
  (`CSR`), `s r = Σ_{i=0}^{r} α^i β^(r-i)` satisfies, for every r,
      s (r+2) + αβ · s r = (α+β) · s (r+1).
  This is the Hecke recursion for T(p^r) with α+β = τ(p), αβ = p^11
  (weight 12) or α+β = a_p, αβ = p (weight 2).

  Numeric part: τ(n) for n ≤ 200 from Δ = q Π (1-q^n)^24, computed by Int list
  products as (Π (1-q^n)^3)^8, with each partial product checked against a
  literal checkpoint by `rfl` (one big `rfl` is too slow); multiplicativity, the prime-power recursion and the bound
  τ(p)^2 ≤ 4p^11 are checked with `rfl`; the same three for the 11a form.

  Proved here:
    add_zero', one_mul', right_distrib'          derived laws
    sumR_ext, sumR_shift, mul_sumR               finite sums in a CSR
    s_succ                                       s (r+1) = β^(r+1) + α · s r
    hecke_rec                                    s (r+2) + αβ s r = (α+β) s (r+1)
    natCSR, hecke_rec_nat                        the Nat instance
    e3_eq, step6 … step24                        checkpoints Π (1-q^n)^(3k)
    deltaList_eq                                 (Π (1-q^n)^3)^8 = tauTable
    tau_values                                   τ(2), τ(3), τ(4), τ(6)
    tau_mult, tau_hecke, tau_ramanujan           the three checks for Δ
    a11List_eq, a11_values, a11_mult, a11_hecke, a11_ramanujan   the three checks for 11a
-/
import ESChain

namespace HeckeChain
open FTAChain

/-! ## A commutative semiring, given by its operations and laws -/

structure CSR (R : Type) where
  add : R → R → R
  mul : R → R → R
  zero : R
  one : R
  add_assoc : ∀ a b c, add (add a b) c = add a (add b c)
  add_comm : ∀ a b, add a b = add b a
  zero_add : ∀ a, add zero a = a
  mul_assoc : ∀ a b c, mul (mul a b) c = mul a (mul b c)
  mul_comm : ∀ a b, mul a b = mul b a
  mul_one : ∀ a, mul a one = a
  mul_zero : ∀ a, mul a zero = zero
  left_distrib : ∀ a b c, mul a (add b c) = add (mul a b) (mul a c)

section General
variable {R : Type} (K : CSR R)

theorem add_zero' (a : R) : K.add a K.zero = a := (K.add_comm a K.zero).trans (K.zero_add a)

theorem one_mul' (a : R) : K.mul K.one a = a := (K.mul_comm K.one a).trans (K.mul_one a)

theorem right_distrib' (a b c : R) : K.mul (K.add a b) c = K.add (K.mul a c) (K.mul b c) := by
  rw [K.mul_comm (K.add a b) c, K.left_distrib, K.mul_comm c a, K.mul_comm c b]

/-- `a^n`. -/
def pw (a : R) : Nat → R
  | 0 => K.one
  | n + 1 => K.mul (pw a n) a

/-- `Σ_{k<n} f k`. -/
def sumR : Nat → (Nat → R) → R
  | 0, _ => K.zero
  | n + 1, f => K.add (sumR n f) (f n)

theorem sumR_ext : ∀ (n : Nat) (f g : Nat → R), (∀ k, f k = g k) → sumR K n f = sumR K n g
  | 0, _, _, _ => rfl
  | n + 1, f, g, h => by
    show K.add (sumR K n f) (f n) = K.add (sumR K n g) (g n)
    rw [sumR_ext n f g h, h n]

theorem sumR_shift : ∀ (n : Nat) (f : Nat → R),
    sumR K (n + 1) f = K.add (f 0) (sumR K n (fun k => f (k + 1)))
  | 0, f => by
    show K.add K.zero (f 0) = K.add (f 0) K.zero
    rw [K.zero_add, add_zero']
  | n + 1, f => by
    show K.add (sumR K (n + 1) f) (f (n + 1))
      = K.add (f 0) (K.add (sumR K n (fun k => f (k + 1))) (f (n + 1)))
    rw [sumR_shift n f, K.add_assoc]

theorem mul_sumR (c : R) : ∀ (n : Nat) (f : Nat → R),
    K.mul c (sumR K n f) = sumR K n (fun k => K.mul c (f k))
  | 0, _ => K.mul_zero c
  | n + 1, f => by
    show K.mul c (K.add (sumR K n f) (f n)) = K.add (sumR K n (fun k => K.mul c (f k))) (K.mul c (f n))
    rw [K.left_distrib, mul_sumR c n f]

/-- `s r = Σ_{i=0}^{r} α^i β^(r-i)`. -/
def s (a b : R) (r : Nat) : R := sumR K (r + 1) (fun i => K.mul (pw K a i) (pw K b (r - i)))

theorem s_succ (a b : R) (r : Nat) : s K a b (r + 1) = K.add (pw K b (r + 1)) (K.mul a (s K a b r)) := by
  show sumR K (r + 1 + 1) (fun i => K.mul (pw K a i) (pw K b (r + 1 - i))) = _
  rw [sumR_shift]
  show K.add (K.mul K.one (pw K b (r + 1)))
    (sumR K (r + 1) (fun k => K.mul (pw K a (k + 1)) (pw K b (r + 1 - (k + 1))))) = _
  rw [one_mul']
  have e : sumR K (r + 1) (fun k => K.mul (pw K a (k + 1)) (pw K b (r + 1 - (k + 1))))
      = sumR K (r + 1) (fun k => K.mul a (K.mul (pw K a k) (pw K b (r - k)))) :=
    sumR_ext K (r + 1) _ _ (fun k => by
      rw [Nat.succ_sub_succ]
      show K.mul (K.mul (pw K a k) a) (pw K b (r - k)) = _
      rw [K.mul_comm (pw K a k) a, K.mul_assoc])
  rw [e, ← mul_sumR]
  rfl

/-- **Hecke recursion.** For every r:
    `s (r+2) + αβ · s r = (α+β) · s (r+1)`. -/
theorem hecke_rec (a b : R) (r : Nat) :
    K.add (s K a b (r + 2)) (K.mul (K.mul a b) (s K a b r)) = K.mul (K.add a b) (s K a b (r + 1)) := by
  have h1 : s K a b (r + 2) = K.add (K.mul (pw K b (r + 1)) b) (K.mul a (s K a b (r + 1))) :=
    s_succ K a b (r + 1)
  have h3 : K.mul b (s K a b (r + 1))
      = K.add (K.mul b (pw K b (r + 1))) (K.mul b (K.mul a (s K a b r))) := by
    rw [s_succ K a b r, K.left_distrib]
  rw [h1, right_distrib' K a b, h3, K.mul_comm (pw K b (r + 1)) b, K.mul_comm a b,
    K.mul_assoc b a, K.add_comm (K.mul b (pw K b (r + 1))) (K.mul a (s K a b (r + 1))),
    K.add_assoc]

end General

/-! ## The Nat instance -/

def natCSR : CSR Nat where
  add := Nat.add
  mul := Nat.mul
  zero := 0
  one := 1
  add_assoc := nadd_assoc
  add_comm := nadd_comm
  zero_add := nzero_add
  mul_assoc := fun a b c => nmul_assoc a b c
  mul_comm := nmul_comm
  mul_one := nmul_one
  mul_zero := fun _ => rfl
  left_distrib := fun a b c => nleft_distrib a b c

theorem hecke_rec_nat (a b r : Nat) :
    s natCSR a b (r + 2) + a * b * s natCSR a b r = (a + b) * s natCSR a b (r + 1) :=
  hecke_rec natCSR a b r

theorem s_nat_values : s natCSR 2 3 0 = 1 ∧ s natCSR 2 3 1 = 5 ∧ s natCSR 2 3 2 = 19 :=
  ⟨rfl, rfl, rfl⟩

/-! ## Δ = q Π (1-q^n)^24 -/

/-- Pointwise sum of coefficient lists. -/
def zadd : List Int → List Int → List Int
  | [], bs => bs
  | a :: as, [] => a :: as
  | a :: as, b :: bs => (a + b) :: zadd as bs

def scale (c : Int) : List Int → List Int
  | [] => []
  | b :: bs => c * b :: scale c bs

/-- Add `x · b` to `rest`; nothing is computed when `x = 0`. -/
def rowAdd : Int → List Int → List Int → List Int
  | Int.ofNat 0, _, rest => rest
  | Int.ofNat (k + 1), b, rest => zadd (scale (Int.ofNat (k + 1)) b) rest
  | Int.negSucc k, b, rest => zadd (scale (Int.negSucc k) b) rest

/-- Product of power series, keeping the first `N` coefficients. -/
def pmulN : Nat → List Int → List Int → List Int
  | 0, _, _ => []
  | _ + 1, [], _ => []
  | N + 1, x :: xs, b => rowAdd x (b.take (N + 1)) (0 :: pmulN N xs b)

/-- Multiply by `Π_{n=1}^{N} (1 - q^n)^3`. -/
def prodE3 : Nat → List Int → List Int
  | 0, l => l
  | n + 1, l => prodE3 n (ESChain.mul1m (n + 1) (ESChain.mul1m (n + 1) (ESChain.mul1m (n + 1) l)))

/-- `Π_{n≥1} (1-q^n)^3` up to `q^(200-1)`. -/
def e3 : List Int := prodE3 200 (ESChain.oneS 200)

/-- Multiply by `Π (1-q^n)^3`. -/
def mulE3 (l : List Int) : List Int := pmulN 200 e3 l

/-- `Π_{n≥1} (1-q^n)^24 = (Π (1-q^n)^3)^8` up to `q^(200-1)`. -/
def delta24 : List Int := mulE3 (mulE3 (mulE3 (mulE3 (mulE3 (mulE3 (mulE3 e3))))))

/-! Checkpoints `Π (1-q^n)^(3k)`, k = 1..8. -/

def L3 : List Int :=
  [1, -3, 0, 5, 0, 0, -7, 0, 0, 0, 9, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, -15,
   0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, -19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, -23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, -27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   -31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, -35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, -39, 0, 0, 0, 0, 0, 0, 0, 0, 0]
def L6 : List Int :=
  [1, -6, 9, 10, -30, 0, 11, 42, 0, -70, 18, -54, 49, 90, 0, -22, -60, 0, -110, 0, 81, 180, -78,
   0, 130, -198, 0, -182, -30, 90, 121, 84, 0, 0, 210, 0, -252, -102, -270, 170, 0, 0, -69, 330,
   0, -38, 420, 0, -190, -390, 0, -108, 0, 0, 0, -300, 99, 442, 210, 0, 418, -294, 0, 0, -510,
   378, -540, 138, 0, -230, -462, 0, 611, 570, 0, 0, 132, 0, 50, -150, 0, 110, 0, -630, -350, 0,
   0, -598, 450, 0, 361, 660, 162, -550, 420, 0, 0, 378, 0, 650, -798, -486, -782, 0, 0, 58, -330,
   0, 290, 0, 441, 468, -702, 0, 850, 522, 0, 0, 0, 810, -700, -780, 0, -1260, 0, 0, 1188, -918,
   0, 0, -558, 0, 529, 180, 0, 682, 1092, -198, 0, 330, 0, 180, -462, 0, -1150, 0, -540, 0, 930,
   0, -1102, -726, 0, -70, 210, 0, -779, 2100, 0, 490, 1218, 0, 0, -630, -990, 1178, 0, 0, 770,
   -1350, 0, -1260, 900, 0, -540, -1302, 0, -518, 0, 0, 0, 462, 729, 1450, 0, 0, 612, 0, 0, -1190,
   -78, 1620, 962, -390, 0, 0, -1020, 0, -220, -1110]
def L9 : List Int :=
  [1, -9, 27, -12, -90, 135, 54, -99, -189, -85, 657, -162, -135, -171, -810, 702, 495, 837, -673,
   -900, 243, -1053, -297, 1566, 2700, -1764, 81, -1188, -1377, 270, -2043, 3321, -756, 3726,
   3015, -4563, -3348, 504, -351, -1350, -468, -891, 7074, 1611, 2700, -2423, -1512, -3267, -5265,
   -1800, 3510, 2970, -6741, 8910, -1620, 7227, 7506, -648, -13923, -9045, 7884, -5985, -2079,
   -815, 6930, 1107, -1485, 12231, 5049, -8100, 15075, -11205, -4104, -10719, -18900, 7371, 5310,
   7587, -756, 6390, -11799, 16632, 4041, 13500, 1020, -9117, -297, -23274, -12078, 9180, -4212,
   -16236, 5535, 9855, 29700, -3780, -2862, -11034, 23463, 13500, -20322, 12177, 6318, -23751,
   -13230, -37206, -7794, 12393, 19619, 18900, -20223, 21303, 3690, 12933, -11880, 36630, -18171,
   7992, 6327, -29700, -2565, -135, -14472, -43848, 19800, -24516, -10044, 18963, 44172, 31590,
   2520, -3861, 15282, -32652, -11475, 46799, 22995, -34614, -8424, -26730, 21087, -38664, -53424,
   9018, -37800, 44577, 48573, 7425, -34101, 27000, 31266, 35442, 17874, 3996, -53820, 15228,
   -32643, 19305, 6210, -18090, -22815, -20817, 39096, -81819, -43470, 8076, 9828, -2376, 74034,
   47700, 65826, -45900, 29232, -17604, -13500, 37746, -39636, -6156, 37269, -3780, -86184, 10971,
   -29511, -9477, 33165, -118719, 57348, -18909, -37044, 70740, 909, 72981, -46413, 83412, 24300,
   53082, 28611, 16767, -80879, -61200]
def L12 : List Int :=
  [1, -12, 54, -88, -99, 540, -418, -648, 594, 836, 1056, -4104, -209, 4104, -594, 4256, -6480,
   -4752, -298, 5016, 17226, -12100, -5346, -1296, -9063, -7128, 19494, 29160, -10032, -7668,
   -34738, 8712, -22572, 21812, 49248, -46872, 67562, 2508, -47520, -76912, -25191, 67716, 32076,
   7128, 29754, 36784, -51072, 45144, -122398, -53460, 11286, -27256, 57024, 122364, 99902, 3576,
   -29646, -221616, 41382, -52272, 130549, -206712, -180036, 336512, 145200, 100980, -73568,
   221616, -317142, -148324, 15552, -225720, -32076, 108756, 196614, 74360, -58806, 229824,
   120878, -233928, 361152, -111340, -349920, -491832, -196569, -82764, 707454, 18392, 92016,
   493668, -559450, 416856, -16092, 320760, -361152, -724032, 7106, 270864, -530442, 56168,
   -261744, 52272, 930204, 406296, 451440, -339196, 562464, -653400, -374528, -810744, -248292,
   779360, 20691, -744876, -272746, 570240, -153846, -69984, 922944, 1154736, 657074, -694980,
   -489402, -349448, -812592, 1341900, -2216160, -384912, 132354, 26224, 58806, 943272, 1052676,
   -357048, 967518, -518320, -441408, -112860, 2222726, -421344, -196614, -1552276, -541728,
   -1515888, -1067021, 1468776, -1072170, -414072, 2216160, 1715472, 1064800, -135432, -1875852,
   1585892, 327072, -730728, 584858, 470448, -2482866, -320760, -1468368, 496584, 87362, -1198824,
   114048, 377948, 29502, 1177848, 639122, 355752, 2298240, 2276560, 2659392, -2904660, -3991570,
   -1715472, 1429218, -2531088, 627264, 1161864, -1777203, -1566588, 3648348, -1089232, -1705374,
   -1715472, 3505766, 2160432, 248292, 4043852, -4038144, 5187456, -2566080, 1197900, -950346,
   -2437776, -1211760, -4153248, -520738, 882816]
def L15 : List Int :=
  [1, -15, 90, -245, 105, 1107, -2485, 195, 4860, -2420, -3990, -8190, 19695, 13755, -38475, 3990,
   -9750, 34020, 43015, -46605, -13860, -127385, 106485, 165240, -79275, -16380, -92340, -35840,
   -151995, 188550, 315783, 90090, -271215, -307485, 20475, -505440, 915385, 209340, -284130,
   337645, -294225, 269325, -1707970, -70305, 1297620, 574210, 492765, 251370, -847245, -1102725,
   438129, -1416190, 641445, 0, 1537330, 1239420, -800370, 1403815, -472080, -2750895, -2707950,
   -761490, 952560, 7162255, -1869450, -1047150, 1169350, 728805, 500175, -5564055, 1575990,
   -5375160, 939590, 1736280, 4629870, 151789, 2775045, 8922960, -9469990, -4736745, -408240,
   -2820545, -7843095, 4410630, 7312455, 4813590, -2678940, -1306095, 4328205, 4383720, 2850575,
   650415, -16312140, -13250360, 3613365, -10234350, 25950965, -2986830, -126360, 14627375,
   -1348440, 4511430, -1500905, -9198165, -15036840, -6051657, 1866045, 14079420, -25952080,
   8872605, 13162500, 3784270, -3698940, 21802365, 13822370, 2059575, -5443200, -36745585,
   4354350, -32495760, -2579090, -10418370, 35281575, 23239370, 2164095, 6259680, -36269395,
   27197715, 39008880, 4624130, -24190320, -46725255, 21403445, -9364305, -49460220, 27860480,
   -14923755, 10128510, 3267385, 9231495, 4416930, 84066950, 24607620, -24426360, -20576010,
   -22731630, -33827220, -55611745, 37965930, -25008750, 6789020, -6995835, 46937880, -32399325,
   19526415, 21899880, 10264600, -7542990, 62460720, 53250665, -55884570, 49246380, -109977255,
   -58941300, -17829630, -48743275, 26869050, 47617605, -63874780, 60856560, 34399080, 63857745,
   -6648915, -29717010, 127947820, 85312500, -119541825, -135014845, -24014445, -39905460,
   84706867, 1467375, 6761430, -81434260, -59929440, -25446960, 106918315, -47015700, 20650140,
   193630840, -2268825, 34783740, 127085210, -10985415, -141015195, -172260130, 50869995,
   -39040380, -86690270, -21624225]
def L18 : List Int :=
  [1, -18, 135, -510, 765, 1242, -7038, 8280, 9180, -27710, 3519, 20196, 50370, -68850, -153765,
   244782, 52785, -71010, -130525, -343620, 517293, 54978, 498780, -390150, -1835865, 1161270,
   896751, 793730, -633420, -906660, -75582, -2589984, 1523745, 3589380, 2472615, -3740850,
   -767039, -4649670, -4222800, 11166210, 1718937, 4728294, -10403235, 2349450, 5331285,
   -23826622, 6799950, 7601040, 14132100, 7375230, -989604, -16889958, -19556595, -1794690,
   6516610, 16045416, -10484343, 48988800, -10299960, -31037580, 6486723, -21198150, -924885,
   -25688700, -14287140, 52029486, 86795778, 45900, -17620875, -112599720, 9017955, 1360476,
   -46670270, 78420150, -34415820, 65991042, 38393973, 75746610, -124838820, 7422030, 38533050,
   -156118718, -71136585, -26920350, 236345730, -89959716, 90024723, 195022980, 36215100,
   -156734730, -153057119, -131160492, -200991780, 265786500, 162542475, -101009376, -47854881,
   62559540, 175246200, 277603200, -229438800, 107153550, -386441790, -263819430, 77479065,
   -274645404, 428879196, -6726780, -28038780, 450951570, 526572603, -454098558, -10203570,
   -254377800, -354504060, -227791908, -163327347, 435317570, -99851625, -34055640, 450696960,
   214020054, -391928625, 936291150, -555307515, 310659138, -47886621, -117298980, -592247700,
   -1767636030, 197489799, 417063600, 2091050235, -457343010, -103550265, 627094402, 73087353,
   42058170, -404802300, -97511490, -307155915, -947934738, -534880485, -162112050, -29675710,
   1507438350, 427716855, -1124840190, 462396600, 1694862090, -717823215, 1141253640, -559631160,
   38546820, -90569745, -1562324004, -2082024000, -1221691230, 1320891840, 965664990, -495870687,
   1402782030, 3004646400, -777109950, 1556365725, -1395752700, 68282676, -3216593970, 918634950,
   840064860, -2118398049, 1694468098, -2156609835, -1261033650, 521423490, 607203450,
   -1187838756, 4672888380, 1907833500, 1588257180, -1621478462, -2021892246, 177252030,
   391189890, 683087625, 715330710, -7034667507, -2943241740, 2371331700, -3380227200, 2810136924,
   4260828150, 5277887490, -1080747000, -57820230, -386935164, 5164257789, -4254223140,
   -5694767100, 1017272520]
def L21 : List Int :=
  [1, -21, 189, -910, 2205, -378, -13321, 33345, -10395, -86870, 122703, 46683, -98287, -264915,
   96390, 1163064, -1113588, -1066527, 1042055, 536025, 2287467, -3603805, -1391733, 478170,
   -562555, 13742379, -7889805, -12745348, -1009470, 6926850, 21064883, -13691664, 4004343,
   -8355270, -30343950, 5444712, 39441969, 30902949, -22027005, 4899895, -91000161, -20752011,
   44496424, 116926740, 78003135, -226108554, 116695530, -60695838, -129044790, 45585540,
   12284811, 283682483, -27484758, 67323690, -214802315, -371932239, -82198368, 417708354,
   111666555, -52005240, 638583673, -444188745, -237602295, -567970480, 43637055, 576707796,
   -400022259, 341089497, 280802025, 408518215, 605957193, -681119145, -2097510107, 178523415,
   -114017085, 1294459054, 1259851698, -395936100, 825849885, -755830845, 224243019, -2566400585,
   -408874242, 693647955, 1174425490, -1533234843, 944315253, 4929083341, -2896680150, 837058320,
   -1662770753, -86999157, -2907571275, -1054415530, 154033110, -2631729177, 6834150050,
   6506694684, 2406336660, -8531638360, -2570420601, 835371117, -3776824261, 17334135, 3005512965,
   -3962727241, 3394367613, 1674066366, 1831163915, 9675444975, -4638505851, -470511573,
   -7226945334, -1984753260, -2886200205, -3277380015, -3542499576, 7108862460, 516036885,
   8749762560, 23073233701, -16559671275, -589467375, -621266030, -14047912620, -9873411597,
   -1427454266, 41809152, 8161994295, -5627832490, 32452074459, 5330325609, -27912318161,
   7008613290, -692506395, 29194385640, -15388495122, -6044259312, -40948031355, -4846020795,
   782992413, 3488503837, 38782369080, -2374229340, 28112471345, -2193913827, 1362238290,
   -22520473725, 19269433575, -8338442805, -47369561498, -34643853657, 6453091827, 40618530260,
   -5640802440, -8035613397, 56220671689, 15962029461, 2931333300, 4954343240, -77548244697,
   47249624646, 14285399303, -28276810590, -25942543515, -32222834784, -8452228806, 3284996904,
   -1702831760, 34747323975, -6667081785, 74023864530, 63865865175, -25483545675, 26371696855,
   -20051999730, -50065696107, -167281182950, 28424333700, -15641447220, 29252762917, 42620901498,
   73792687122, -37805736290, -27726712020, 90471133872, -64303207234, 166324013037, 13901410215,
   -77213528210, -76482050925, -55529640369, -43712869767, -37250101350, -19983071115,
   194978031542, -120168877725, -114149698740, 100457800555, 20825668500]

/-- τ(1), ..., τ(200) (index i holds τ(i+1)), i.e. `Π (1-q^n)^24`. -/
def tauTable : List Int :=
  [1, -24, 252, -1472, 4830, -6048, -16744, 84480, -113643, -115920, 534612, -370944, -577738,
   401856, 1217160, 987136, -6905934, 2727432, 10661420, -7109760, -4219488, -12830688, 18643272,
   21288960, -25499225, 13865712, -73279080, 24647168, 128406630, -29211840, -52843168,
   -196706304, 134722224, 165742416, -80873520, 167282496, -182213314, -255874080, -145589976,
   408038400, 308120442, 101267712, -17125708, -786948864, -548895690, -447438528, 2687348496,
   248758272, -1696965207, 611981400, -1740295368, 850430336, -1596055698, 1758697920, 2582175960,
   -1414533120, 2686677840, -3081759120, -5189203740, -1791659520, 6956478662, 1268236032,
   1902838392, 2699296768, -2790474540, -3233333376, -15481826884, 10165534848, 4698104544,
   1940964480, 9791485272, -9600560640, 1463791322, 4373119536, -6425804700, -15693610240,
   -8951543328, 3494159424, 38116845680, 4767866880, 1665188361, -7394890608, -29335099668,
   6211086336, -33355661220, 411016992, 32358470760, 45164021760, -24992917110, 13173496560,
   9673645072, -27442896384, -13316478336, -64496363904, 51494658600, -49569988608, 75013568546,
   40727164968, -60754911516, 37534859200, 81742959102, 41767088832, -225755128648, -48807306240,
   -20380127040, 38305336752, 90241258356, 107866805760, 73482676310, -61972223040, -45917755128,
   -16528605184, -85146862638, -64480268160, 90047003760, -189014559360, 65655879534,
   124540889760, 115632958896, 102825676800, 498319933, -166955487888, 77646351384, 77785143296,
   -359001100500, -45668121408, -262717201024, 338071388160, -4315678416, 66971388960,
   631528759932, -198311113728, -178514816480, 371563845216, -353937956400, -583413304320,
   -297198746214, -112754509056, 596793577940, 119045821440, 677211820992, -234995646528,
   -308865667656, -112181096448, 620204022900, -35130991728, -427635232164, 268217998208,
   -1115433620850, 154219312800, -824447297848, 900676761600, 784811057562, 214837039872,
   -255232501440, 214308444672, 1315116754406, -914804296320, -402206035896, -950091448320,
   -312162946368, -39964520664, -357832759588, -453553290624, 650708341920, 704042392032,
   2754833892216, -356462346240, -1458379197393, 800535869280, -1211595753060, 25209042176,
   -950387449578, -776603298240, 426959023400, 527734751232, -1307679342480, 599830010640,
   1681384224780, 807974455680, -996774496018, -232167481728, 1753032622824, 1574983618560,
   -880090306620, 319595480064, -3691995187608, -3955776986112, 1226984915520, -1235871806400,
   2762403350592, 680222785536, 5442387685442, -1800325645104, -703199584080, 2497932784704,
   -2876091504354, 1458117876384, 728391402200, -2154174528000]

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem e3_eq : e3 = L3 := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem step6 : pmulN 200 L3 L3 = L6 := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem step9 : pmulN 200 L3 L6 = L9 := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem step12 : pmulN 200 L3 L9 = L12 := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem step15 : pmulN 200 L3 L12 = L15 := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem step18 : pmulN 200 L3 L15 = L18 := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem step21 : pmulN 200 L3 L18 = L21 := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem step24 : pmulN 200 L3 L21 = tauTable := by rfl

/-- **The table is the Int list product `(Π (1-q^n)^3)^8`.** -/
theorem deltaList_eq : delta24 = tauTable := by
  show pmulN 200 e3 (pmulN 200 e3 (pmulN 200 e3 (pmulN 200 e3 (pmulN 200 e3
    (pmulN 200 e3 (pmulN 200 e3 e3)))))) = tauTable
  rw [e3_eq, step6, step9, step12, step15, step18, step21, step24]

/-- `τ(n)` for `1 ≤ n ≤ 200`. -/
def tau (n : Nat) : Int := ESChain.nth tauTable (n - 1)

theorem tau_values : tau 1 = 1 ∧ tau 2 = -24 ∧ tau 3 = 252 ∧ tau 4 = -1472 ∧ tau 6 = -6048 :=
  ⟨rfl, rfl, rfl, rfl, rfl⟩

/-! ## Boolean checks (comparisons written by hand) -/

def intEq : Int → Int → Bool
  | Int.ofNat a, Int.ofNat b => Nat.beq a b
  | Int.ofNat _, Int.negSucc _ => false
  | Int.negSucc _, Int.ofNat _ => false
  | Int.negSucc a, Int.negSucc b => Nat.beq a b

def intLe (a b : Int) : Bool :=
  match b - a with
  | Int.ofNat _ => true
  | Int.negSucc _ => false

/-- gcd with fuel. -/
def gcdF : Nat → Nat → Nat → Nat
  | 0, a, _ => a
  | f + 1, a, b => match Nat.beq b 0 with
    | true => a
    | false => gcdF f b (a % b)

/-- `P 1 && ... && P k`. -/
def allTo (P : Nat → Bool) : Nat → Bool
  | 0 => true
  | k + 1 => allTo P k && P (k + 1)

/-- For all `m, n ≥ 1` with `mn ≤ 200` and `gcd(m,n) = 1`: `f(mn) = f(m) f(n)`.
    (For each m, n runs over `1, ..., 200 / m`, which are exactly the n with mn ≤ 200.) -/
def multCheck (f : Nat → Int) : Bool :=
  allTo (fun m => allTo (fun n =>
    !(Nat.beq (gcdF 200 m n) 1) || intEq (f (m * n)) (f m * f n)) (200 / m)) 200

/-- For all `r ≥ 1` with `p^(r+1) ≤ 200`:
    `f(p^(r+1)) = f(p) f(p^r) - c(p) f(p^(r-1))`. -/
def heckeCheckP (f : Nat → Int) (c : Nat → Int) (p : Nat) : Bool :=
  allTo (fun r => !(Nat.ble (p ^ (r + 1)) 200) ||
    intEq (f (p ^ (r + 1))) (f p * f (p ^ r) - c p * f (p ^ (r - 1)))) 7

def heckeCheck (f : Nat → Int) (c : Nat → Int) : Bool :=
  ESChain.primes200.all (heckeCheckP f c)

/-- For every prime `p < 200`: `f(p)^2 ≤ 4 c(p)`. -/
def ramCheck (f : Nat → Int) (c : Nat → Int) : Bool :=
  ESChain.primes200.all (fun p => intLe (f p * f p) (4 * c p))

/-- Weight 12: `p^11`. -/
def c12 (p : Nat) : Int := Int.ofNat (p ^ 11)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **τ(mn) = τ(m) τ(n)** for all coprime m, n with mn ≤ 200. -/
theorem tau_mult : multCheck tau = true := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **τ(p^(r+1)) = τ(p) τ(p^r) - p^11 τ(p^(r-1))** for all p^(r+1) ≤ 200, r ≥ 1. -/
theorem tau_hecke : heckeCheck tau c12 = true := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **τ(p)^2 ≤ 4 p^11** for every prime p < 200. -/
theorem tau_ramanujan : ramCheck tau c12 = true := by rfl

/-! ## 11a, weight 2 -/

/-- Coefficients of q^1, ..., q^200 of `q Π (1-q^n)^2 (1-q^(11n))^2`. -/
def a11Table : List Int :=
  [1, -2, -1, 2, 1, 2, -2, 0, -2, -2, 1, -2, 4, 4, -1, -4, -2, 4, 0, 2, 2, -2, -1, 0, -4, -8, 5,
   -4, 0, 2, 7, 8, -1, 4, -2, -4, 3, 0, -4, 0, -8, -4, -6, 2, -2, 2, 8, 4, -3, 8, 2, 8, -6, -10,
   1, 0, 0, 0, 5, -2, 12, -14, 4, -8, 4, 2, -7, -4, 1, 4, -3, 0, 4, -6, 4, 0, -2, 8, -10, -4, 1,
   16, -6, 4, -2, 12, 0, 0, 15, 4, -8, -2, -7, -16, 0, -8, -7, 6, -2, -8, 2, -4, -16, 0, 2, 12,
   18, 10, 10, -2, -3, 8, 9, 0, -1, 0, -8, -10, 4, 0, 1, -24, 8, 14, -9, -8, 8, 0, 6, -8, -18, -2,
   0, 14, 5, 0, -7, -2, 10, -4, -8, 6, 4, 8, 0, -8, 3, 6, -10, -8, 2, 0, 4, 4, 7, -8, -7, 20, 6,
   8, 2, -2, 4, -16, -1, 12, -12, 0, 3, 4, 0, -12, -6, 0, 8, -4, -5, -30, -15, -4, 7, 16, -12, 0,
   3, 14, -2, 16, -10, 0, 17, 8, 4, 14, -4, -6, -2, 4, 0, 0]

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem a11List_eq : ESChain.etaProd 200 = a11Table := by rfl

def a11 (n : Nat) : Int := ESChain.nth a11Table (n - 1)

theorem a11_values : a11 2 = -2 ∧ a11 3 = -1 ∧ a11 5 = 1 ∧ a11 11 = 1 := ⟨rfl, rfl, rfl, rfl⟩

/-- Weight 2, level 11: `χ(p) p` with the trivial character mod 11, so `χ(11) = 0`. -/
def c2 (p : Nat) : Int := match Nat.beq p 11 with
  | true => 0
  | false => Int.ofNat p

/-- The bound `4p`, for every p. -/
def c2b (p : Nat) : Int := Int.ofNat p

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- `a(mn) = a(m) a(n)` for all coprime m, n with mn ≤ 200. -/
theorem a11_mult : multCheck a11 = true := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- `a(p^(r+1)) = a_p a(p^r) - p a(p^(r-1))` for p ≠ 11, and `a(11^(r+1)) = a_11 a(11^r)`. -/
theorem a11_hecke : heckeCheck a11 c2 = true := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- `a_p^2 ≤ 4p` for every prime p < 200. -/
theorem a11_ramanujan : ramCheck a11 c2b = true := by rfl

end HeckeChain

#print axioms HeckeChain.add_zero'
#print axioms HeckeChain.one_mul'
#print axioms HeckeChain.right_distrib'
#print axioms HeckeChain.sumR_ext
#print axioms HeckeChain.sumR_shift
#print axioms HeckeChain.mul_sumR
#print axioms HeckeChain.s_succ
#print axioms HeckeChain.hecke_rec
#print axioms HeckeChain.hecke_rec_nat
#print axioms HeckeChain.s_nat_values
#print axioms HeckeChain.e3_eq
#print axioms HeckeChain.step6
#print axioms HeckeChain.step9
#print axioms HeckeChain.step12
#print axioms HeckeChain.step15
#print axioms HeckeChain.step18
#print axioms HeckeChain.step21
#print axioms HeckeChain.step24
#print axioms HeckeChain.deltaList_eq
#print axioms HeckeChain.tau_values
#print axioms HeckeChain.tau_mult
#print axioms HeckeChain.tau_hecke
#print axioms HeckeChain.tau_ramanujan
#print axioms HeckeChain.a11List_eq
#print axioms HeckeChain.a11_values
#print axioms HeckeChain.a11_mult
#print axioms HeckeChain.a11_hecke
#print axioms HeckeChain.a11_ramanujan

Listing 18: NonAbChain.lean: \(S_3\), its character, and \(a_p = r_p - 1\) for \(\eta (z)\eta (23z)\).

NonAbChain.lean

/-
  NonAbChain.lean
  Non-abelian reciprocity for S3 (link 73).

  Lean 4 core plus the earlier work/ files (ESChain for `mul1m`, `oneS`, `nth`,
  `primes200`; HeckeChain for `intEq`). Proofs are hand-written terms;
  no omega / simp / ring / decide / norm_num / linarith. Concrete checks use `rfl`.

  S3 is listed as the six permutations of Fin 3. The 2-dimensional
  irreducible character is χ = (fixed points) - 1. The weight-1 form
  η(z)η(23z) = q Π (1-q^n)(1-q^(23n)) has a_p = r_p - 1, where r_p is the
  number of roots of x^3 - x - 1 mod p (the splitting field of that cubic has
  Galois group S3 and discriminant -23).

  Proved here:
    perms_list, fix_values, chi_values      the six permutations, fixed points, χ
    comp_check, inv_check                   composition and inverse are right
    chi_class                               χ(h g h⁻¹) = χ(g) for all 36 pairs
    chi_norm                                Σ χ(g)^2 = 6, i.e. ⟨χ, χ⟩ = 1
    noncomm_bool, noncomm                   στ ≠ τσ
    a23List_eq, a23_values                  coefficients of η(z)η(23z), n ≤ 200
    r_values                                r_p for small p
    ap_eq_rp                                a_p = r_p - 1, primes p < 200, p ≠ 23
    rp_013                                  r_p ∈ {0, 1, 3} for the same p
-/
import HeckeChain

namespace NonAbChain
open FTAChain HeckeChain

/-! ## S3 as the permutations of Fin 3 -/

/-- A map `Fin 3 → Fin 3` given by its values at 0, 1, 2. -/
structure P3 where
  a : Fin 3
  b : Fin 3
  c : Fin 3

def f0 : Fin 3 := ⟨0, Nat.zero_lt_succ 2⟩
def f1 : Fin 3 := ⟨1, Nat.succ_lt_succ (Nat.zero_lt_succ 1)⟩
def f2 : Fin 3 := ⟨2, Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ 0))⟩

def app (s : P3) (i : Fin 3) : Fin 3 :=
  match i.val with
  | 0 => s.a
  | 1 => s.b
  | _ + 2 => s.c

/-- `(s ∘ t)(i) = s (t i)`. -/
def comp (s t : P3) : P3 := ⟨app s (app t f0), app s (app t f1), app s (app t f2)⟩

def finEq (i j : Fin 3) : Bool := Nat.beq i.val j.val

def p3Eq (s t : P3) : Bool := finEq s.a t.a && finEq s.b t.b && finEq s.c t.c

/-- The preimage of `j` under a permutation. -/
def pre (s : P3) (j : Fin 3) : Fin 3 :=
  match finEq s.a j with
  | true => f0
  | false => match finEq s.b j with
    | true => f1
    | false => f2

def inv (s : P3) : P3 := ⟨pre s f0, pre s f1, pre s f2⟩

def e   : P3 := ⟨f0, f1, f2⟩
def t01 : P3 := ⟨f1, f0, f2⟩
def t02 : P3 := ⟨f2, f1, f0⟩
def t12 : P3 := ⟨f0, f2, f1⟩
def c1  : P3 := ⟨f1, f2, f0⟩
def c2  : P3 := ⟨f2, f0, f1⟩

/-- **The six elements of S3.** -/
def S3 : List P3 := [e, t01, t02, t12, c1, c2]

def vals (s : P3) : List Nat := [s.a.val, s.b.val, s.c.val]

theorem perms_list : S3.map vals =
    [[0, 1, 2], [1, 0, 2], [2, 1, 0], [0, 2, 1], [1, 2, 0], [2, 0, 1]] := rfl

/-- Number of fixed points. -/
def fix (s : P3) : Nat :=
  (match finEq s.a f0 with | true => 1 | false => 0) +
  (match finEq s.b f1 with | true => 1 | false => 0) +
  (match finEq s.c f2 with | true => 1 | false => 0)

/-- The character χ = fixed points - 1. -/
def chi (s : P3) : Int := Int.ofNat (fix s) - 1

theorem fix_values : S3.map fix = [3, 1, 1, 1, 0, 0] := rfl

theorem chi_values : S3.map chi = [2, 0, 0, 0, -1, -1] := rfl

/-- `allL L P` : `P` holds on every element of `L`. -/
def allL {A : Type} (P : A → Bool) : List A → Bool
  | [] => true
  | x :: xs => P x && allL P xs

/-- Every element of S3 is a permutation: `s ∘ s⁻¹ = s⁻¹ ∘ s = e`. -/
theorem inv_check : allL (fun s => p3Eq (comp s (inv s)) e && p3Eq (comp (inv s) s) e) S3 = true :=
  rfl

/-- `anyL L P` : `P` holds on some element of `L`. -/
def anyL {A : Type} (P : A → Bool) : List A → Bool
  | [] => false
  | x :: xs => P x || anyL P xs

/-- S3 is closed under composition: every `s ∘ t` is in the list. -/
theorem comp_check : allL (fun s => allL (fun t =>
    anyL (fun u => p3Eq (comp s t) u) S3) S3) S3 = true := rfl

/-- **χ is a class function**: `χ(h g h⁻¹) = χ(g)` for all 6 × 6 pairs. -/
theorem chi_class : allL (fun g => allL (fun h =>
    intEq (chi (comp (comp h g) (inv h))) (chi g)) S3) S3 = true := rfl

def sumI : List Int → Int
  | [] => 0
  | x :: xs => x + sumI xs

/-- **Σ_g χ(g)^2 = 6 = |S3|**, i.e. ⟨χ, χ⟩ = 1: χ is irreducible. -/
theorem chi_norm : sumI (S3.map (fun g => chi g * chi g)) = 6 := rfl

/-- **S3 is non-abelian** (Bool form). -/
theorem noncomm_bool : p3Eq (comp t01 t12) (comp t12 t01) = false := rfl

/-- **S3 is non-abelian**: `t01 ∘ t12 ≠ t12 ∘ t01` (they send 0 to 1 and 2). -/
theorem noncomm : comp t01 t12 ≠ comp t12 t01 := fun h =>
  have h12 : (1 : Nat) = 2 := congrArg (fun s => s.a.val) h
  succ_ne_zero' 0 (succ_inj' h12).symm

/-! ## η(z) η(23z) -/

/-- Multiply by `Π_{n=1}^{N} (1 - q^n)`. -/
def prodA : Nat → List Int → List Int
  | 0, l => l
  | n + 1, l => prodA n (ESChain.mul1m (n + 1) l)

/-- Multiply by `Π_{n=1}^{N} (1 - q^(23n))`. -/
def prodB : Nat → List Int → List Int
  | 0, l => l
  | n + 1, l => prodB n (ESChain.mul1m (23 * (n + 1)) l)

/-- Coefficients of `q^0, ..., q^(N-1)` of `Π (1-q^n)(1-q^(23n))`. -/
def eta23 (N : Nat) : List Int := prodB N (prodA N (ESChain.oneS N))

/-- Coefficients of q^1, ..., q^200 of `η(z)η(23z) = q Π (1-q^n)(1-q^(23n))`. -/
def a23Table : List Int :=
  [1, -1, -1, 0, 0, 1, 0, 1, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1, -1, 1, 1, 1, 0, -1, 0,
   -1, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, -1, -1, 1, 1, -1, 0, 0, 0, -1, 0, 0, 0, 1, 2, 0,
   0, 1, 0, 1, 0, 0, 0, 0, -1, 0, -1, 0, -1, 0, -1, 0, 0, -1, 0, 0, -1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
   0, 0, 1, 1, 0, 0, 0, -1, 0, 0, 2, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 1,
   0, 1, 0, 0, 0, -1, -1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1, -1, 0, 1, 1, 0, 0, 0, 1, -1, 0, 0, 1, -1,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, -1, 0, 0, -2, 0, -1, 0, 0,
   0, 0, 1, 0, -1, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, -1, 0, 0, 1]

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem a23List_eq : eta23 200 = a23Table := by rfl

/-- `a_n` for `1 ≤ n ≤ 200`. -/
def a23 (n : Nat) : Int := ESChain.nth a23Table (n - 1)

theorem a23_values : a23 1 = 1 ∧ a23 2 = -1 ∧ a23 3 = -1 ∧ a23 6 = 1 ∧ a23 8 = 1 :=
  ⟨rfl, rfl, rfl, rfl, rfl⟩

/-! ## Roots of x^3 - x - 1 mod p -/

/-- `x^3 - x - 1 ≡ 0 (mod p)`, i.e. `x^3 ≡ x + 1`. -/
def isRoot (p x : Nat) : Nat :=
  match Nat.beq ((x * x * x) % p) ((x + 1) % p) with
  | true => 1
  | false => 0

/-- `Σ_{x < k} isRoot p x`. -/
def cntTo (p : Nat) : Nat → Nat
  | 0 => 0
  | k + 1 => cntTo p k + isRoot p k

/-- `r_p = #{x mod p : x^3 - x - 1 ≡ 0}`. -/
def r (p : Nat) : Nat := cntTo p p

theorem r_values : r 2 = 0 ∧ r 3 = 0 ∧ r 5 = 1 ∧ r 7 = 1 ∧ r 59 = 3 ∧ r 101 = 3 :=
  ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩

/-- The primes below 200 other than 23. -/
def primesNo23 : List Nat := ESChain.primes200.filter (fun p => !(Nat.beq p 23))

theorem primesNo23_length : primesNo23.length = 45 := rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Non-abelian reciprocity for S3**: for every prime p < 200, p ≠ 23,
    the q^p coefficient of η(z)η(23z) is `r_p - 1`. -/
theorem ap_eq_rp : allL (fun p => intEq (a23 p) (Int.ofNat (r p) - 1)) primesNo23 = true := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- For the same p, `r_p ∈ {0, 1, 3}`: there is no p with exactly two roots. -/
theorem rp_013 : allL (fun p => Nat.ble (r p) 3 && !(Nat.beq (r p) 2)) primesNo23 = true := by rfl

end NonAbChain

#print axioms NonAbChain.perms_list
#print axioms NonAbChain.fix_values
#print axioms NonAbChain.chi_values
#print axioms NonAbChain.inv_check
#print axioms NonAbChain.comp_check
#print axioms NonAbChain.chi_class
#print axioms NonAbChain.chi_norm
#print axioms NonAbChain.noncomm_bool
#print axioms NonAbChain.noncomm
#print axioms NonAbChain.a23List_eq
#print axioms NonAbChain.a23_values
#print axioms NonAbChain.r_values
#print axioms NonAbChain.primesNo23_length
#print axioms NonAbChain.ap_eq_rp
#print axioms NonAbChain.rp_013

Listing 19: numeric_hecke.py: Satake parameters.

numeric_hecke.py

#!/usr/bin/env python3
"""T11 Satake parameters (Python standard library only).

Writes the results to work/NUMERIC_HECKE.txt next to this script.
"""
import math
import os

N = 1000


def series_mul_1m(l, k):
    """Multiply a truncated series by (1 - q^k) in place."""
    for i in range(len(l) - 1, k - 1, -1):
        l[i] -= l[i - k]


def tau_list(n_max):
    """τ(1..n_max) from Δ = q Π (1-q^n)^24, exact integers."""
    L = n_max  # coefficients of q^0 .. q^(n_max-1) of Π(1-q^n)^24
    e = [1] + [0] * (L - 1)
    for n in range(1, L + 1):
        for _ in range(24):
            series_mul_1m(e, n)
    return [None] + e  # tau[n] = e[n-1]


def a11_list(n_max):
    """a(1..n_max) from q Π (1-q^n)^2 (1-q^(11n))^2."""
    L = n_max
    e = [1] + [0] * (L - 1)
    for n in range(1, L + 1):
        for _ in range(2):
            series_mul_1m(e, n)
            if 11 * n < L:
                series_mul_1m(e, 11 * n)
    return [None] + e


def primes_below(n):
    sieve = bytearray([1]) * n
    sieve[0:2] = b"\x00\x00"
    for i in range(2, int(n ** 0.5) + 1):
        if sieve[i]:
            sieve[i * i::i] = bytearray(len(sieve[i * i::i]))
    return [i for i in range(n) if sieve[i]]


def satake_abs(a, c):
    """|α| for the roots of x^2 - a x + c with a^2 ≤ 4c: α = (a ± i√(4c - a^2)) / 2."""
    re = a / 2
    im = math.sqrt(4 * c - a * a) / 2
    return math.hypot(re, im)


def main():
    out = []
    ok = True

    def check(name, cond):
        nonlocal ok
        ok = ok and cond
        out.append(f"  [{'PASS' if cond else 'FAIL'}] {name}")

    tau = tau_list(N)
    a = a11_list(N)
    ps = primes_below(N)

    out.append(f"τ(n) for n ≤ {N} (exact integers)")
    out.append("  τ(1..10) = " + ", ".join(str(tau[n]) for n in range(1, 11)))
    out.append(f"  τ(997) = {tau[997]}")
    out.append(f"  τ(1000) = {tau[1000]}")
    check("τ(2) = -24, τ(3) = 252, τ(4) = -1472, τ(6) = -6048",
          (tau[2], tau[3], tau[4], tau[6]) == (-24, 252, -1472, -6048))

    # Weight 12
    out.append(f"Δ, weight 12: {len(ps)} primes p < {N}")
    disc_ok = all(tau[p] ** 2 - 4 * p ** 11 <= 0 for p in ps)
    worst = 0.0
    max_ratio = (0, 0.0)
    for p in ps:
        r = satake_abs(tau[p], p ** 11) / p ** 5.5
        worst = max(worst, abs(r - 1))
        x = abs(tau[p]) / (2 * p ** 5.5)
        if x > max_ratio[1]:
            max_ratio = (p, x)
    out.append(f"  max_p |τ(p)| / (2 p^(11/2)) = {max_ratio[1]:.12f} at p = {max_ratio[0]}")
    out.append(f"  max_p | |α_p| / p^(11/2) - 1 | = {worst:.3e}")
    check("τ(p)^2 - 4 p^11 ≤ 0 for all p < 1000 (exact)", disc_ok)
    check("|α_p| / p^(11/2) = 1 within 1e-12", worst < 1e-12)

    # Weight 2, 11a
    ps11 = [p for p in ps if p != 11]
    out.append(f"11a, weight 2: {len(ps11)} primes p < {N}, p ≠ 11")
    out.append("  a_p (p ≤ 29) = " + ", ".join(f"{p}:{a[p]}" for p in ps if p <= 29))
    bound_ok = all(a[p] ** 2 <= 4 * p for p in ps11)
    worst2 = 0.0
    for p in ps11:
        r = satake_abs(a[p], p) / math.sqrt(p)
        worst2 = max(worst2, abs(r - 1))
    out.append(f"  max_p | |α_p| / √p - 1 | = {worst2:.3e}")
    check("a_p^2 ≤ 4p for all p < 1000, p ≠ 11 (exact)", bound_ok)
    check("|α_p| = √p within 1e-12", worst2 < 1e-12)

    out.append(f"ALL {'PASS' if ok else 'FAIL'}")
    text = "\n".join(out) + "\n"
    path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "NUMERIC_HECKE.txt")
    with open(path, "w", encoding="utf-8") as fh:
        fh.write(text)
    print(text, end="")
    return 0 if ok else 1


if __name__ == "__main__":
    raise SystemExit(main())

Listing 20: NUMERIC_HECKE.txt: output of numeric_hecke.py.

NUMERIC_HECKE.txt

τ(n) for n ≤ 1000 (exact integers)
  τ(1..10) = 1, -24, 252, -1472, 4830, -6048, -16744, 84480, -113643, -115920
  τ(997) = -21400415987399554
  τ(1000) = -30328412970240000
  [PASS] τ(2) = -24, τ(3) = 252, τ(4) = -1472, τ(6) = -6048
Δ, weight 12: 168 primes p < 1000
  max_p |τ(p)| / (2 p^(11/2)) = 0.959407024120 at p = 103
  max_p | |α_p| / p^(11/2) - 1 | = 2.220e-16
  [PASS] τ(p)^2 - 4 p^11 ≤ 0 for all p < 1000 (exact)
  [PASS] |α_p| / p^(11/2) = 1 within 1e-12
11a, weight 2: 167 primes p < 1000, p ≠ 11
  a_p (p ≤ 29) = 2:-2, 3:-1, 5:1, 7:-2, 11:1, 13:4, 17:-2, 19:0, 23:-1, 29:0
  max_p | |α_p| / √p - 1 | = 2.220e-16
  [PASS] a_p^2 ≤ 4p for all p < 1000, p ≠ 11 (exact)
  [PASS] |α_p| = √p within 1e-12
ALL PASS

78.12 Paths

Two statements are the targets. Reciprocity for \(\mathrm {GL}_n\) over a number field \(F\) matches cuspidal algebraic automorphic representations \(\pi \) of \(\mathrm {GL}_n(\mathbb A_F)\) with irreducible geometric \(\ell \)-adic representations \(\rho \) of \(G_F\), with equal Frobenius and Satake parameters at almost all places. Functoriality transfers automorphic representations along every \(L\)-homomorphism \({}^LH\to {}^LG\). The dependency graph, with every node marked proved (Lean), cited (paper and theorem) or remaining (exact statement), is LANGLANDS_MAP.md. This subsection follows the seven branches.

R6: a model with two finite sides (Lean). Let \(G\) be a finite group listed once each, \(H\subseteq G\) a subgroup, and let functions take values in any commutative ring. A bijection of \(G\) leaves sums over \(G\) unchanged (reindex); the proof writes \(F(\sigma x)=\sum _y[y=\sigma x]F(y)\) and exchanges the sums. Induction \((\operatorname {Ind}f)(x)=\sum _{g\in G}f^\circ (gxg^{-1})\), with \(f^\circ \) the extension of \(f\) by zero, gives a class function on \(G\) (ind_class). For every class function \(\chi \) on \(G\),

\[ \langle \operatorname {Ind}f,\chi \rangle _G=|G|\sum _{y\in H}f(y)\,\chi (y^{-1}) \]
(frobenius). So the transfer from \(H\) to \(G\) is adjoint to restriction. This is functoriality in the model where both sides are finite: class functions play the automorphic side and characters the Galois side. Together with the abelian and \(S_3\) reciprocity laws proved earlier (QRChain, NonAbChain), it is the finite-image layer of the correspondence.

R1: patching and torsion. Scholze attached Galois representations to torsion classes in the cohomology of locally symmetric spaces for \(\mathrm {GL}_n\) over totally real and CM fields [992]. Calegari and Geraghty extended patching to \(\ell _0>0\) given such representations [985]. The ten-author paper proves modularity lifting over CM fields without self-duality and potential automorphy [997]; local–global compatibility at \(\ell =p\) is known in the Fontaine–Laffaille and ordinary cases, the latter up to a nilpotent ideal [1010].

The vanishing outside \([q_0,q_0+\ell _0]\) is the Koszul condition, proved here for \(\ell _0\le 2\) (KoszulRangeChain). For \(\ell _0=1\), \(H_1\) of \(0\to R\xrightarrow {x}R\to 0\) vanishes exactly when \(x\) is regular (range1_iff). For \(\ell _0=2\), a regular sequence \((x,y)\) gives \(H_2=H_1=0\) (range2_of_regular); conversely, if \(x\) is regular and \(H_1=0\), then \((x,y)\) is regular (regular_of_range2, range2_iff). The hypothesis on \(x\) is needed: over \(\mathbb Z/6\) the pair \((2,3)\) has exact Koszul complex and \(2\cdot 3=0\). In a Noetherian local ring it can be dropped [1004].

The vanishing was computed in two families.

The remaining lemma of this branch is compatibility at \(\ell =p\) outside the ordinary and Fontaine–Laffaille cases.

R2: descent to CM fields. Solvable base change and descent hold for \(\mathrm {GL}_n\) [999]. A monomial \(\rho =\operatorname {Ind}_H\psi \) with \(H\) solvable is therefore automorphic by induction. If an irreducible \(\chi \) is a nonnegative combination of induced characters, then \(\chi =\operatorname {Ind}\psi \) for a single \(\psi \). So the cases left by Brauer induction are exactly the non-monomial irreducibles. For \(2I=\mathrm {SL}_2(\mathbb F_5)\), the image of an icosahedral Galois representation, the irreducible degrees are \(1,2,2,3,3,4,4,5,6\). There is no subgroup of index \(2\), \(3\) or \(4\), so the irreducibles of degrees \(2,2,3,3,4,4\) are not monomial. The same holds for \(A_5\) in degrees \(3,3,4\) (NUMERIC_R2.txt). For these, every Brauer expression has negative coefficients. The missing lemma is the descent of such a virtual automorphic sum.

R3: beyond endoscopy. For \(\mathrm {GL}_2\) over \(\mathbb Q\), Poisson summation on the elliptic part of the trace formula removes the trivial and special contributions [998]. This extends to a class of totally real fields [1008] and has an adelic form [1013]; the smoothing of real elliptic orbital integrals holds for \(\mathrm {GL}_n\) [1009]. In exact arithmetic (NUMERIC_R3.txt) the elliptic sum satisfies \(\sum _{t^2\le 4n}H(4n-t^2)=2\sigma _1(n)-\sum _{dd'=n}\min (d,d')\) for \(n\le 200\). This identity is the cancellation of the trivial representation at \(k=2\). Eichler–Selberg returns \(\tau (n)\) at \(k=12\) for \(n\le 60\). For \(\mathrm {GL}_3\) the elliptic terms are the cubics \(x^3-ax^2+bx-n\) with complex roots, and their number in the tempered box is \(\approx 147\,n\), against \(\approx n^{1/2}\) for \(\mathrm {GL}_2\). The cancellation of the trivial term among these is the missing lemma.

R4: converse theorems. The converse theorem for \(\mathrm {GL}_N\) needs \(L(s,\Pi \times \tau )\) entire, bounded in vertical strips and with functional equation for \(\tau \) on \(\mathrm {GL}_m\), \(m\le N-2\) [1000]. For \(\mathrm {Sym}^5\) these are \(L(s,\mathrm {Sym}^5\pi \times \tau )\) with \(\tau \) on \(\mathrm {GL}_1\) to \(\mathrm {GL}_4\). Langlands–Shahidi gave \(\mathrm {Sym}^3\), \(\wedge ^2\) on \(\mathrm {GL}_4\) and \(\mathrm {Sym}^4\); \(\mathrm {Sym}^n\) for holomorphic non-CM forms was reached by automorphy lifting [1006]. The functional equations of \(L(s,\mathrm {Sym}^n\Delta )\), \(n\le 5\), were measured with Dokchitser’s theta test (NUMERIC_R4.txt). \(\theta (1/t)/(t\,\theta (t))\) is constant to \(10^{-13}\), with root numbers \(+1,+1,-1,+1,-1\), and a \(1\%\) change in one coefficient moves it by \(3\%\). For \(\mathrm {Sym}^n\) of the curve \(11a\), \(n\le 3\), with conductor \(11^n\) and the Steinberg factor at \(11\), the same ratio is \(+1\) to \(10^{-14}\). The missing lemma is the analytic package for Maass forms and general number fields.

R5: geometrisation. Fargues and Scholze geometrise the local correspondence on the Fargues–Fontaine curve [1001]; over function fields V. Lafforgue attaches Langlands parameters by excursion operators [1007]; the categorical geometric conjecture is proved in characteristic zero [1002]. Six properties of \(\mathrm {Bun}_G\) of a curve over \(\mathbb F_q\) are used (LANGLANDS_MAP.md, R5):

The adelic quotient \(G(F)\backslash G(\mathbb A_F)/K\) has the Hecke correspondences and the finiteness. The Fargues–Fontaine curve has all six, but at one place at a time. The missing object glues the local \(\mathrm {Bun}_G\) over all places.

R7: the function-field path. Over \(\mathbb F_q(C)\), L. Lafforgue proves reciprocity for \(\mathrm {GL}_n\) in six steps (LANGLANDS_MAP.md, §R7) [1003]. Step 4 uses the rationality and functional equation of \(L(s,\rho \otimes \tau )\) [1012]; over a number field this is Artin’s holomorphy. For two-dimensional \(\rho \) it holds by image type:

For \(x^5+20x+16\), which has Galois group \(A_5\) and one real root, the Frobenius frequencies up to \(3\cdot 10^5\) agree with Chebotarev to \(0.001\) (NUMERIC_R7.txt). The sums \(\sum _{p\le X}\chi (\mathrm {Frob}_p)/p\) for \(\chi _4\), \(\chi _5\) and \(\chi _3+\chi _3'\) change by at most \(0.006\) from \(X=10^4\) to \(3\cdot 10^5\), against \(0.31\) for a simple pole. The missing case is the even icosahedral one.

78.13 Status

statement

status where

reindexing; induced functions are class functions; Frobenius reciprocity

proved FiniteRecChain

abelian and \(S_3\) reciprocity in finite models

proved QRChain, NonAbChain

range \(\Leftrightarrow \) Koszul with regular sequence, \(\ell _0\le 2\)

proved KoszulRangeChain

reciprocity as a split idempotent; shell for any group with a Chebotarev section

proved lang/Recip1, lang/Frobenius

Frobenius additive in characteristic \(p\); \(x^q=x\) in finite fields

proved lang/Frobenius, lang/FiniteField

GL\(_2\) at unramified \(p\): \(a(p^n)=\operatorname {tr}\mathrm {Sym}^n\rho (\mathrm {Frob}_p)\); GL\(_n\): \(\sum h_kX^k=1/\prod (1-\alpha _iX)\)

proved lang/GL2Local, lang/GLnLocal

CM case: trace of \(\operatorname {Ind}\) from index 2

proved lang/Dihedral

local unramified functoriality: \(\mathrm {Sym}^n\) (\(n\ge 1\)), twists, \(\mathrm {GL}_2\times \mathrm {GL}_2\to \mathrm {GL}_4\), \(\wedge ^2:\mathrm {GL}_4\to \mathrm {GL}_6\), unramified quadratic base change for every \(\mathrm {GL}_n\), commuting with \(\mathrm {Sym}^n\); adjoint \(\mathrm {Ad}:\mathrm {GL}_2\to \mathrm {GL}_3\)

proved lang/Sym2Local, SymNLocal, TwistLocal, TensorLocal, ExtSquareLocal, BaseChangeLocal, BaseChangeGLn, SymBC, AdjointLocal

vanishing outside the range: Bianchi \(N\le 103\), \(\mathrm {SL}_3(\mathbb Z)\) \(p\le 17\), \(\ell \le 13\)

measured NUMERIC_R1, _R1_GL3

non-monomial irreducibles of \(2I\) and \(A_5\)

measured NUMERIC_R2

GL2 trivial-term cancellation, Eichler–Selberg

measured NUMERIC_R3

functional equations of \(L(s,\mathrm {Sym}^n\Delta )\), \(n\le 5\), and \(\mathrm {Sym}^n 11a\), \(n\le 3\)

measured NUMERIC_R4

\(A_5\) Artin sums without a pole, Chebotarev

measured NUMERIC_R7

GL\(_1\) reciprocity for \(\mathbb Q(i)\), \(\mathbb Q(\sqrt {-3})\), \(\mathbb Q(\sqrt 2)\) and every \(\mathbb Q(\sqrt a)\): \((a/p)\) depends only on \(p \bmod 4a\)

proved lang/GaussSplit, Eisen, TwoSupp, QuadAll

GL\(_1\) reciprocity for every \(\mathbb Q(\zeta _N)\): an element of order \(N\) in \(\mathbb F_p^\times \) iff \(N\mid p-1\), in \(\mathbb F_{p^2}^\times \) iff \(N\mid p^2-1\) (residue degree \(\le 2\)); \(\mathbb F_q^\times \) cyclic

proved lang/CycloSplit, CycloPow, OrdMul, PrimRoot, FinCyclo

split primes are norms: \(p\equiv 1\ (4)\Rightarrow p=u^2+v^2\); \(p\equiv 1\ (3)\Rightarrow p=u^2+3v^2\); \(p\equiv 1,3\ (8)\Rightarrow p=u^2+2v^2\)

proved lang/TwoSquares, EisenNorm, TwoNorm2, TwoNorm2Split

GL\(_2/\mathbb Q\) CM, \(y^2=x^3-x\): \(a_p=0\) at \(p\equiv 3\ (4)\); \(a_p=2\,\mathrm {Re}\,\pi \) for the primary \(\pi \) above \(p\equiv 1\ (4)\) (Hecke character of \(\mathbb Q(i)\)); Hasse invariant; Gauss’s congruence

proved lang/CMInert, CMModular, SignCM3, HasseCM, GaussCong

GL\(_2/\mathbb Q\) CM, \(y^2=x^3+1\): \(a_p=0\) at \(p\equiv 2\ (3)\); \(a_p=2a\) with \(p=a^2+3b^2\), \(a\equiv 1\ (3)\) at \(p\equiv 1\ (3)\) (Hecke character of \(\mathbb Q(\sqrt {-3})\)); Hasse invariant

proved lang/CMEisen, CubicTrace, CubicSign, HasseEisen

GL\(_2/\mathbb Q\) CM families \(y^2=x^3+Dx\), \(y^2=x^3+D\) (all \(D\)): \(a_p=0\) at inert \(p\); \(a_p\) from the norm forms at split \(p\)

proved lang/QuarticFamily, SexticFamily

GL\(_2\) over the CM field \(\mathbb Q(i)\): \(y^2=x^3-x\) over \(\mathbb F_{p^2}\), \(p\equiv 3\ (4)\): \(\#E=(p+1)^2\), \(a_{(p)}=-2p\) (base change); over every \(\mathbb F_q\ni i\), in particular every \(\mathbb F_{p^2}\): \(a=2\,\mathrm {Re}\,\pi \), \(\pi \) primary of norm \(q\), \(a\equiv q+1\ (8)\), \(a^2\le 4q\)

proved lang/SuperSing, FinTrace, FinPrimary, FinField–FinSign, Fp2Inst, Fq2–Fq2Trace

GL\(_2\) over the CM field \(\mathbb Q(\sqrt {-3})\): \(y^2=x^3+1\) over every \(\mathbb F_q\), \(3\mid q-1\): \(a=2a'\), \(q=a'^2+3b^2\), \(a'\equiv 1\ (3)\), \(a^2\le 4q\); over \(\mathbb F_{p^2}\), every \(p\equiv 2\ (3)\), \(p>3\): \(\#E=(p+1)^2\), \(a_{(p)}=-2p\) (base change); \(\mathbb F_q^\times \) cyclic

proved lang/FinCyclic–FinCubicSign, CubicSuper, CubicSuper2

base change at inert primes: \(a_{(p)}=a_p^2-2p\) for \(y^2=x^3-x\) (\(p\equiv 3\ (4)\)) and \(y^2=x^3+1\) (\(p\equiv 2\ (3)\), \(p>3\)), as integer identities between counts over \(\mathbb F_p\) and \(\mathbb F_{p^2}\)

proved lang/BaseChange, CubicSuper2

\(y^2=x^3-x\) at every odd \(p\) (one model \(\mathbb F_p[\sqrt n]\)) and \(y^2=x^3+1\) at every \(p>3\): \(a(\mathbb F_{p^2})=a_p^2-2p\) (Hasse–Davenport), via \(S(\mathbb F_{p^2})\equiv -S(\mathbb F_p)^2\ (p)\) (Hasse invariant, Lucas)

proved lang/FinPowerSum, FinHasse, FinHasse3, Lucas, FinCast, SplitHasse, SplitHasse3, HasseDavenport, HasseDavenport3, InertFq, EveryPrime, EveryPrime3

Deuring’s criterion: \(p\mid a_p\iff p\) inert in the CM field (also for every twist \(y^2=x^3+Dx\), \(y^2=x^3+D\)); Hasse’s bound \(a_p^2\le 4p\) (Ramanujan for the CM forms), for \(y^2=x^3-x\), \(y^2=x^3+1\) and all their twists \(y^2=x^3+Dx\), \(y^2=x^3+D\)

proved lang/Deuring, DeuringTwist, HasseBoundP, FamilyBound

all quartic twists \(y^2=x^3+Dx\) at every odd \(p\), all sextic twists \(y^2=x^3+D\) at every \(p>3\) (\(D\in \mathbb F_p^\times \)): \(a(\mathbb F_{p^2})=a_p^2-2p\)

proved lang/FamilyFq, FamilyHD, SexticInert, FinCubicNorm, FinHasseD, SplitHasseD, CubeCount, CubeLift, SexticHD

Dirichlet: infinitely many primes \(p\equiv 1,3\ (4)\), \(p\equiv 1,2\ (3)\), every class mod \(8\) (both classes of \(\mathbb Q(i)\), \(\mathbb Q(\sqrt {-3})\)), \(p\equiv 4\ (5)\) (Gauss sum in \(\mathbb F_q[\zeta _5]\)), every class mod \(12\), every class mod \(24\), and \(p\equiv 1\ (\ell ^e)\) for every prime power \(\ell ^e\) (Chebotarev sections exist for these classes)

proved lang/InfSplit, InfInert, InfInert3, InfCyclo, InfCycloPow, Inf7mod8, Inf5mod8, Inf3mod8, Cyclo5, Gauss5, Inf4mod5, Sq3, Inf11mod12, Inf7mod12, Inf1mod12, Inf5mod12, Inf13mod24, Inf1mod24, Inf7mod24, Inf17mod24, Inf19mod24, Inf23mod24, Inf11mod24, Inf5mod24

strong multiplicity one for GL\(_1\) mod \(3,4,8\): characters agreeing at all primes \(>B\) agree on every unit class

proved lang/StrongMult1

\(a(\mathbb F_{p^3})=a_p^3-3pa_p\) for \(y^2=x^3-x\) and \(y^2=x^3+1\) at every \(p\equiv 1\ (3)\)

proved lang/CubeAp, CubeInert, CubeAp3

reciprocity for \(y^2=x^3-x\) and \(y^2=x^3+1\): over every finite field of characteristic \(p>3\) the trace is \(s_N(\psi (\pi )+\psi (\bar \pi ),p)\) (split) or \(s_N(0,p)\) (inert)

proved lang/CMReciprocity

theta series \(\theta _\psi \) of \(\mathbb Q(i)\): the primary elements of norm \(p^k\) are \(\pi ^j\bar \pi ^{k-j}\) and \(c_{k+2}=a_pc_{k+1}-pc_k\) (the \(L\)-factor of \(y^2=x^3-x\))

proved lang/ThetaCoef

local zeta function of \(y^2=x^3+1\) at every \(p>3\), over every field of order \(p^N\) of characteristic \(p\); such fields of every order \(p^{3^b2^{L+1}}\)

proved lang/ZetaAll3, FinPow3, InertPow3, InertOdd3, TowerZeta, MixedTower, MixedZeta

local zeta function of \(y^2=x^3-x\) at every odd \(p\): \(a(F)=\alpha ^N+\beta ^N\) (\(\alpha +\beta =a_p\), \(\alpha \beta =p\)) over every field of order \(p^N\) of characteristic \(p\)

proved lang/ZetaAll, ZetaRec, SplitAll, InertAll

\(a(\mathbb F_{p^N})=2\,\mathrm {Re}\,\pi ^N=\alpha ^N+\beta ^N\)for \(y^2=x^3-x\) over every field of order \(p^N\) of characteristic \(p\equiv 1\ (4)\) (every level \(\mathbb F_{p^{2^L}}\) of the tower)

proved lang/FinPow, FinPow2, SplitAll, TowerPow, GaussPow, LucasIter

\(a(\mathbb F_{p^N})=2\,\mathrm {Re}\,\pi ^N\) for \(y^2=x^3+1\) over every field of order \(p^N\), \(p\equiv 1\ (6)\) (every level of the tower)

proved lang/FinPow3, TowerPow3, EisenPow, EisenFactor

\(a(\mathbb F_{p^{2M}})=2(-p)^M\) for \(y^2=x^3-x\) at inert \(p\equiv 3\ (4)\), every field of order \(p^{2M}\); \(a=0\) over every field of order \(p^{2M+1}\)

proved lang/InertPow, InertTower, InertAll

\(a(\mathbb F_{p^{2M}})=2(-p)^M\) for \(y^2=x^3+1\) at inert \(p\equiv 2\ (3)\), every field of order \(p^{2M}\); \(a=0\) over every field with \(q\equiv 2\ (3)\)

proved lang/InertPow3, InertOdd3

finite fields of the interface from a list and Fermat (Euler’s criterion by counting); \(\mathbb F_q[\sqrt n]\) for every \(\mathbb F_q\); the tower \(\mathbb F_{p^{2^k}}\); \(\mathbb F_{p^3}=\mathbb F_p[\sqrt [3]{c}]\) for \(p\equiv 1\ (3)\); a cubic extension of order \(q^3\) of every field of order \(q=p^N\equiv 1\ (3)\)

proved lang/GenericEuler, MkFF, QuadExt, FieldTower, Cubic, CubicFrob, CubicField, CubicInst, GenCubic, GenCubicField, GenCubicInst, GenCubicExt

quadratic Gauss sum: \(g=\sum _a(a/p)\zeta ^a\) has \(g^2=p^*=(-1/p)\,p\) in \(\mathbb Z[\zeta _p]=\mathbb Z[C_p]/(N)\), so \(\mathbb Q(\sqrt {p^*})\subset \mathbb Q(\zeta _p)\)

proved lang/GaussSum

quadratic reciprocity through the Gauss sum: in \(\mathbb F_q[C_p]\), \(g^q=(q/p)\,g\) and \(g^q=(p^*)^{(q-1)/2}g\), so \((q/p)=(p^*/q)\)

proved lang/GroupRing, GroupRingFrob, GaussFrob, GaussQR

cubic Jacobi sum: for \(p\equiv 1\ (3)\) and the cubic character \(\chi \) of \(\mathbb F_p\), \(J=\sum _t\chi (t)\chi (1-t)=a+b\omega \) has \(a^2-ab+b^2=p\) (\(g\bar g=p\zeta ^0-N\), \(g^2=J\bar g\) in \(\mathbb Z[\omega ][C_p]\)) and is primary, \(J\equiv -1\ (3)\) (\(g^3\) in \(\mathbb F_3[\omega ][C_p]\))

proved lang/Cyclo3, CubicChar, JacobiSum, JacobiNorm, JacobiPrimary

cubic reciprocity at inert primes: for \(p\equiv 1\ (3)\) with primary \(J=J(\chi ,\chi )\) (\(N(J)=p\)) and a prime \(q=3k+2\ne p\), \(\chi _p(q)=J^{k+1}p^k\) in \(\mathbb F_q[\omega ]\) (\(g^{q+1}\) two ways in \(\mathbb F_q[\omega ][C_p]\)); at split \(q=3k+1\), \(\bar \chi _p(q)=(Jp)^k\)

proved lang/CubicRecip, CubicRecipSplit

quartic Jacobi sum: for \(p\equiv 1\ (4)\) and the quartic character \(\chi \) of \(\mathbb F_p\), \(J=\sum _t\chi (t)\chi (1-t)=a+bi\) has \(a^2+b^2=p\) (\(g\bar g=\chi (-1)X\), \(g^2=Jg_\rho \), \(g_\rho ^2=X\) in \(\mathbb Z[i][C_p]\))

proved lang/Cyclo4, QuartChar, CharSumGen, QuartJacobi

quartic reciprocity at every rational prime \(q\ne p\): for \(p\equiv 1\ (4)\) and \(J=J(\chi ,\chi )\), \(\chi (q)\bar \chi (-1)=J^{2(k+1)}p^k\) in \(\mathbb F_q[i]\) for \(q=4k+3\), \(\bar \chi (q)=(J^2p)^{k+1}\) for \(q=4k+5\)

proved lang/QuartRecip

Hecke character of \(\mathbb Q(i)\): \(c_n=\sum _{\alpha \ \mathrm {primary},\,N\alpha =n}\alpha \) is multiplicative, \(c_{mn}=c_mc_n\) for coprime odd \(m,n\) (unique primary factorisation in \(\mathbb Z[i]\) from Euclid), and at split \(p=\pi \bar \pi \): \(c_{p^k}=\sum _{j+l=k}\pi ^j\bar \pi ^l\), \(c_{p^{k+2}}=2a\,c_{p^{k+1}}-p\,c_{p^k}\); at inert \(q\equiv 3\ (4)\): \(c_{q^{2j}}=(-q)^j\), \(c_{q^{2j+1}}=0\)

proved lang/GaussEuclid, GaussCoprime, GaussUnique, HeckeMult, HeckePrime, HeckeInert

Euler product of \(\theta _\psi \) (\(\psi \) the Hecke character of \(\mathbb Q(i)\)), coefficientwise for all odd \(n\): \(c_{n_1\cdots n_r}=c_{n_1}\cdots c_{n_r}\) for pairwise coprime odd \(n_i\), with the local factors of HeckePrime/HeckeInert

proved lang/HeckeEuler

Gauss’s cubic character of \(2\): \(2\) is a cube modulo \(p\equiv 1\ (3)\) if and only if \(p=x^2+27y^2\) (cubic reciprocity at \(q=2\): \(\chi (2)=J\) in \(\mathbb F_2[\omega ]\))

proved lang/CubicTwo

points on the Fermat cubic: for \(p\equiv 1\ (3)\), \(\#\{x^3+y^3=1\}=p-2+J+\bar J=p-2+(2a-b)\) with \((2a-b)^2+3b^2=4p\)

proved lang/FermatCubic

Jacobi’s cubic characters of \(5,7,11,13\): with \(4p=L^2+27M^2\), \(q\) is a cube mod \(p\) iff \(5\mid LM\), \(7\mid LM\), \(11\mid LM(L^2-16M^2)\), \(13\mid LM(L^2-M^2)\)

proved lang/CubicFive, CubicSeven, CubicEleven, CubicThirteen

quartic characters of \(5,13\): with \(J=a+bi\), \(N(J)=p\equiv 1\ (4)\), \(5\) is a quartic residue iff \(5\mid b\), \(13\) iff \(13\mid b(b^2-3a^2)\)

proved lang/QuartFive, QuartThirteen

quartic characters of \(3,7\): \(3\) iff \(3\mid b\) (\(p\equiv 1\ (8)\)), \(3\mid a\) (\(p\equiv 5\ (8)\)); \(7\) iff \(7\mid ab\) (\(p\equiv 1\ (8)\)), \(7\mid a^2-b^2\) (\(p\equiv 5\ (8)\))

proved lang/QuartThree1, QuartThree5, QuartSeven1, QuartSeven5

cubic and quartic residue criteria for every prime \(q\ne p\): \(q\) is a cube mod \(p\) iff \(J^{k+1}p^k=1\) (\(q=3k+2\)) resp. \((Jp)^{k+1}=1\) (\(q=3k+4\)) in \(\mathbb F_q[\omega ]\); \(q\) is a quartic residue iff \(J^{2k+2}p^{k+1}=1\) (\(q=4k+5\)) resp. \(J^{2k+2}p^k=\chi (-1)\) (\(q=4k+3\)) in \(\mathbb F_q[i]\)

proved lang/ResidueCriterion

quartic character of \(17\): \(17\) is a quartic residue mod \(p=a^2+b^2\) iff \(17\mid ab(a^2-b^2)\)

proved lang/QuartSeventeen

the quartic Jacobi sum is primary up to \(-\chi (-1)\): \(J=a+bi\) with \(a\equiv \chi (-1)+2n\), \(b\equiv 2n+2\pmod 4\) for \(p=4n+5\)

proved lang/QuartPrimary

duplication for the quartic character: \(\#\{x^2=a\}=1+\rho (a)\) and \(J(\chi ,\chi )=\chi (-1)\,J(\rho ,\chi )\)

proved lang/QuartDup

points on the Fermat quartic: \(\#\{x^4+y^4=1\}=p-1-2\chi (-1)+(2+4\chi (-1))\,a\) for \(J=a+bi\), \(p\equiv 1\ (4)\)

proved lang/FermatQuartic

Gauss’s quartic character of \(2\): \(2\) is a quartic residue mod \(p\equiv 1\ (4)\) iff \(8\mid b\) (\(J=a+bi\)) iff \(p=x^2+64y^2\)

proved lang/QuartTwo

Gauss’s cyclotomic number \((0,0)\) of order \(4\): \(16\,\#\{a: a,1-a \text { fourth powers}\}=p-9-2\chi (-1)+(2+4\chi (-1))a\)

proved lang/QuartTwo

exact trace of every twist \(y^2=x^3+D\) at \(p\equiv 1\ (3)\): \(\sum _x\rho (x^3+D)=\rho (D)\,\mathrm {Tr}\,\chi (-D)J(\chi ,\rho )\)

proved lang/CubicSextic

cubic duplication \(J(\chi ,\rho )=\chi (4)J(\chi ,\chi )\), so \(\sum _x\rho (x^3+D)=\rho (D)\,\mathrm {Tr}\,\chi (-4D)J\) for every \(D\ne 0\)

proved lang/CubicDup

exact trace of every quartic twist \(y^2=x^3-Dx\) at \(p\equiv 1\ (4)\): \(\sum _x\rho (x^3-Dx)=\mathrm {Tr}\,\bar \chi (D)\chi (-1)J\)

proved lang/QuartTwist

the quartic \(y^2=x^4+D\) at \(p\equiv 1\ (4)\): \(\sum _x\rho (x^4+D)=-1+\rho (D)\,\mathrm {Tr}\,\chi (-D)J(\chi ,\rho )\)

proved lang/QuartQuartic

\(y^2=x^4+D\) and \(y^2=x^3-4Dx\) have the same trace: \(\sum _x\rho (x^4+D)+1=\sum _x\rho (x^3-4Dx)\)

proved lang/QuartQuartic

2-isogenous twists: \(\sum _x\rho (x^3-Dx)=\sum _x\rho (x^3+4Dx)\)

proved lang/QuartQuartic

quadratic twists of every Weierstrass curve: \(\sum _x\chi (x^3+t^2ax+t^3b)=\chi (t)\sum _x\chi (x^3+ax+b)\)

proved lang/QuadTwist

the traces of the pencil \(b\mapsto y^2=x^3+ax+b\) sum to zero: \(\sum _b\sum _x\chi (x^3+ax+b)=0\)

proved lang/QuadTwist

summing over \(a\): \(\sum _a\sum _x\chi (x^3+ax+b)=p\,\chi (b)\)

proved lang/PencilMoment

\(\sum _b\big (\sum _a\sum _x\chi (x^3+ax+b)\big )^2=p^2(p-1)\)

proved lang/PencilMoment

second moment over the pencil: \(\sum _b\big (\sum _x\chi (x^3+ax+b)\big )^2=-p^2+p\,\#\{x^3+ax=y^3+ay\}\)

proved lang/PencilMoment

covariance over the pencil: \(\sum _bW(a,b)W(a',b)=-p^2+p\,\#\{x^3+ax=y^3+a'y\}\)

proved lang/PencilMoment

the pencil second moment in closed form: \(\sum _bW(a,b)^2=p^2-p-p\chi (-3)-p\chi (-3a)\) (\(a\ne 0\), \(p>3\))

proved lang/PencilClosed

the pencil \(y^2=x^3+b\) at \(p\equiv 2\pmod 3\): \(\sum _b\big (\sum _x\chi (x^3+b)\big )^2=0\)

proved lang/PencilClosed

the pencil \(y^2=x^3+b\), every \(p>3\): \(\sum _b\big (\sum _x\chi (x^3+b)\big )^2=p(p-1)(1+\chi (-3))\)

proved lang/PencilClosed

every quadratic character sum: \(\sum _x\chi (x^2+bx+c)=-1\) (\(b^2\ne 4c\)), \(=p-1\) (\(b^2=4c\))

proved lang/PencilClosed

points on \(y^2=x^2+bx+c\): \(p-1\) (\(b^2\ne 4c\)), \(2p-1\) (\(b^2=4c\))

proved lang/PencilClosed

\(a\ne 0\): \(\sum _x\chi (ax^2+bx+c)=-\chi (a)\) (\(b^2\ne 4ac\)), \((p-1)\chi (a)\) (\(b^2=4ac\)); \(\#\{y^2=ax^2+bx+c\}=p-\chi (a)\)

proved lang/PencilClosed

\(a,b,c\ne 0\): \(\#\{ax^2+by^2=c\}=p-\chi (-ab)\)

proved lang/PencilClosed

\(b\ne 0\): \(\#\{ax^2+by^2=0\}=p+(p-1)\chi (-ab)\)

proved lang/PencilClosed

\(a,b,c\ne 0\): \(\#\{ax^2+by^2+cz^2=d\}=p^2+p\,\chi (-abcd)\)

proved lang/PencilClosed

\(a,b,c,d,e\ne 0\): \(\#\{ax^2+by^2+cz^2+dw^2=e\}=p^3-p\,\chi (abcd)\)

proved lang/PencilClosed

\(a,b,c,d\ne 0\): \(\#\{ax^2+by^2+cz^2+dw^2=0\}=p^3+(p^2-p)\chi (abcd)\)

proved lang/PencilClosed

Legendre family: \(\sum _\lambda \big (\sum _x\chi (x(x-1)(x-\lambda ))\big )^2=p^2-2p-1\)

proved lang/LegendreMoment

Legendre family symmetries: \(a(1-\lambda )=\chi (-1)a(\lambda )\), \(a(1/\lambda )=\chi (\lambda )a(\lambda )\), \(\sum _\lambda a(\lambda )=0\)

proved lang/LegendreMoment

Legendre family, twisted: \(\sum _\lambda \chi (\lambda )a(\lambda )=\chi (-1)\)

proved lang/LegendreMoment

Legendre family, adjacent: \(\sum _\lambda a(\lambda )a(\lambda +1)=-1-p(1+\chi (-1))\)

proved lang/LegendreMoment

Legendre family, \(p\equiv 3\pmod 4\): \(\sum _\lambda a(\lambda )^{2k+1}=0\)

proved lang/LegendreMoment

every family \(y^2=(ax^2+bx+c)(x-\lambda )\): \(\sum _\lambda a_\lambda ^2=-1+p(p-1-\chi (b^2-4ac))\)

proved lang/LegendreMoment

every translation family \(y^2=h(x)+\lambda \): \(\sum _\lambda \big (\sum _x\chi (h(x)+\lambda )\big )^2=-p^2+p\,\#\{h(x)=h(y)\}\)

proved lang/LegendreMoment

two translation families: \(\sum _\lambda S_h(\lambda )S_k(\lambda )=-p^2+p\,\#\{h(x)=k(y)\}\)

proved lang/LegendreMoment

\(\#\{x^2=y^2\}=2p-1\), \(\sum _\lambda \big (\sum _x\chi (x^2+\lambda )\big )^2=p^2-p\)

proved lang/LegendreMoment

\(p>3\): \(\#\{x^3=y^3\}=p+(p-1)(1+\chi (-3))\) (from two second moments)

proved lang/LegendreMoment

\(p>3\): \(\#\{z:z^3=1\}=2+\chi (-3)\)

proved lang/LegendreMoment

\(\#\{z:z^4=1\}=3+\chi (-1)\)

proved lang/LegendreMoment

\(p>3\): \(\#\{z:z^6=1\}=4+2\chi (-3)\)

proved lang/LegendreMoment

\(\#\{z:z^4=-1\}=(1+\chi (-1))(1+\chi (2))\)

proved lang/LegendreMoment

\(\#\{z:z^8=1\}=3+\chi (-1)+(1+\chi (-1))(1+\chi (2))\)

proved lang/LegendreMoment

\(p>3\): \(\#\{z^6=-1\}=(1+\chi (-1))(2+\chi (-3))\), \(\#\{z^{12}=1\}=(2+\chi (-3))(3+\chi (-1))\)

proved lang/LegendreMoment

residue pairs: \(\sum _x(1+\chi (x))(1+\chi (x+d))=p-1\) (\(d\ne 0\))

proved lang/LegendreMoment

quadratic Jacobi sum: \(J(\chi ,\chi )=\sum _x\chi (x)\chi (1-x)=-\chi (-1)\)

proved lang/LegendreMoment

\(\lambda \ne 0,1\): \(\sum _x\chi (x(x-1)(x-\lambda ))\) is even

proved lang/LegendreMoment

\(\lambda \ne 0,1\): \(\#E_\lambda (\mathbb F_p)=p+1+a(\lambda )\equiv 0\pmod 4\)

proved lang/LegendreFour

orbits of an order-3 map counted by sums; \(3\mid p-3-\chi (-3)\) from \(x\mapsto 1/(1-x)\)

proved lang/Orbit3

orbits of an order-5 map counted by sums (any finite type)

proved lang/Orbit5

orbits of an order-7 map counted by sums (any finite type)

proved lang/Orbit7

\(\{O\}\cup \mathbb F_p^2\) completely listed with an injective code

proved lang/Pairs

11a: \(P+T=(-y/x^2,\,y^2/x^3-1)\) stays on \(y^2+y=x^3-x^2\)

proved lang/G4Trans

11a: \(\tau ^2P=P+2T=\big (x(x^2+y)/y^2,\,-(x^2+y)^2/y^3-1\big )\)

proved lang/G4Step

11a: \(\tau (P+2T)=P-2T=\big (x(x^2-y-1)/(y+1)^2,\,-(x^2-y-1)^2/(y+1)^3\big )\)

proved lang/G4Step3

11a: \(\tau (P-2T)=P-T=\big ((y+1)/x^2,\,-(y+1)^2/x^3\big )\), \(\tau (P-T)=P\)

proved lang/G4Step4

11a: \(x\notin \{0,1\}\Rightarrow y,\,y+1,\,x^2+y,\,x^2-y-1\ne 0\)

proved lang/G4Gen

11a: \(\tau =(\cdot +T)\) on \(\{O\}\cup \mathbb F_p^2\) has \(\tau ^5=\mathrm {id}\)

proved lang/G4Map

11a, \(p\ne 11\): \(\tau \) has no fixed point on \(E\cup \{O\}\) and preserves it

proved lang/G4Fix

11a: \(5\mid \#E(\mathbb F_p)\) for odd \(p\ne 11\), i.e. \(a_p\equiv p+1\pmod 5\)

proved lang/G4Count

\(y^2+axy+by=x^3\) (\(b\ne 0\), \(a^3\ne 27b\)): \(3\mid \#E(\mathbb F_p)\)

proved lang/G5Tate3

\(y^2=x^3+ax^2+bx\) (\(b\ne 0\), \(a^2\ne 4b\)): \(2\mid \#E(\mathbb F_p)\)

proved lang/G5Two

\(y^2=x(x-a)(x-b)\) (\(ab\ne 0\), \(a\ne b\)): \(4\mid \#E(\mathbb F_p)\)

proved lang/FullTwo

\(y^2=x(x+a^2)(x+b^2)\) (\(ab(a^2-b^2)\ne 0\)): \(8\mid \#E(\mathbb F_p)\)

proved lang/Z4Z2g (with Z4Z2, Z4Z2b–f, Orbit42)

\(y^2+2(w^2+w+1)xy+2w^2(w+1)^2y=x^3\): \(12\mid \#E(\mathbb F_p)\)

proved lang/Z6Z2

\(y^2+(1-t)xy-ty=x^3-tx^2\), \(t(u-1)^2=u^2(2u-1)\): \(10\mid \#E(\mathbb F_p)\)

proved lang/Z10

\(y^2+xy-ty=x^3-tx^2\) with a rational flex: \(12\mid \#E(\mathbb F_p)\)

proved lang/Z12

\(y^2+xy-ty=x^3-tx^2\), \(t=-w^2(w-1)^2\): \(8\mid \#E(\mathbb F_p)\)

proved lang/Z8Count (with Z8Chord–Z8All, Orbit8)

\(y^2+axy+by=x^3\), \(a=f^3-3fg^2-g^3\), \(b=f^3g^3\), \(f+g=1\): \(9\mid \#E(\mathbb F_p)\)

proved lang/Z9Count (with Z9Chord–Z9All, Orbit9)

\(y^2+xy-ty=x^3-tx^2\), \(t=-w^2(w-1)^2\), \(r^2=1+4w-4w^2\): \(16\mid \#E(\mathbb F_p)\)

proved lang/Z82Count (with Z82Fam–Z82Fix, Orbit82)

Dirichlet: infinitely many primes \(p\equiv 6\ (7)\)

proved lang/Seven7, Seven7b, Seven7c

Dirichlet: infinitely many primes \(p\equiv 8\ (9)\)

proved lang/Nine9, Nine9b, Nine9c

Dirichlet: infinitely many primes \(p\equiv 10\ (11)\)

proved lang/Ell11, Ell11b, Ell11c

Dirichlet: infinitely many primes \(p\equiv 12\ (13)\)

proved lang/Ell13, Ell13b, Ell13c

Dirichlet: infinitely many primes \(p\equiv -1\ (\ell )\) for every prime \(\ell \not \equiv 1\ (8)\)

proved lang/Cheb, ChebKey, ChebRoot, ChebNat, ChebCong, ChebInf

Dirichlet: infinitely many primes \(p\equiv -1\ (\ell )\) for every odd prime \(\ell \)

proved lang/ChebM1, ChebAll

Dirichlet: infinitely many primes \(p\equiv -1\ (\ell ^{e})\) for every prime \(\ell \not \equiv 1\ (8)\), every \(e\)

proved lang/ChebPow, ChebRootPow, ChebPowInf

Dirichlet: infinitely many primes \(p\equiv -1\ (\ell ^{e})\) for every odd prime \(\ell \), every \(e\)

proved lang/ChebPowAll

Dirichlet: infinitely many primes \(p\equiv -1\ (2^{e})\), every \(e\)

proved lang/Two2, Two2Root, Two2Inf

Dirichlet: infinitely many primes \(p\equiv -1\ (\ell ^{e})\) for every prime power \(\ell ^{e}\) (one theorem)

proved lang/MinusOnePP

Dirichlet: infinitely many primes \(p\equiv 1\ (2^{a}\ell ^{b})\), \(\ell \) odd prime

proved lang/Cyclo2l

Dirichlet: infinitely many primes \(p\equiv 1\ (\ell _1^{a}\ell _2^{b})\), any primes \(\ell _1,\ell _2\)

proved lang/TwoPrime, TwoPrimeInf

Dirichlet: infinitely many primes \(p\equiv 1\ (n)\) for every squarefree \(n\ge 2\)

proved lang/PhiSqInf (with PhiS, Val, ValGeo, OrdQ, PhiInv, PhiSize, PhiSq)

Dirichlet: infinitely many primes \(p\equiv 1\ (n^{a+1})\) for every squarefree \(n\ge 2\), every \(a\)

proved lang/PhiPow

Dirichlet: infinitely many primes \(p\equiv 1\ (N)\) for every \(N\ge 1\)

proved lang/PhiAll

Dirichlet: infinitely many primes \(p\equiv 2^{j+2}-1\ (2^{j+3})\), every \(j\)

proved lang/Inf2em1 (with Two2m, Inf2em1a)

Dirichlet: infinitely many primes \(p\equiv 1\ (4)\), \(p\equiv -1\ (\ell )\), every prime \(\ell \equiv 3\ (4)\)

proved lang/InfMix (with LucChar–LucModQ)

Dirichlet: infinitely many primes \(p\equiv 3\ (4)\), \(p\equiv 1\ (\ell )\), every odd prime \(\ell \)

proved lang/Inf3m4

Dirichlet: infinitely many primes \(p\equiv 3\ (4)\), \(p\equiv 1\ (\ell ^{e+1})\), every prime \(\ell \equiv 3\ (4)\), every \(e\)

proved lang/Inf3m4p

Dirichlet: infinitely many primes \(p\equiv 3\ (4)\), \(p\equiv 1\ (n)\), every odd squarefree \(n\ge 3\)

proved lang/InfSq3 (with PhiSq4)

Dirichlet: infinitely many primes \(p\equiv 5\ (8)\), \(p\equiv 1\ (4n)\), every odd squarefree \(n\ge 3\)

proved lang/Inf5m8 (with PhiSq8)

Dirichlet: infinitely many primes \(p\equiv 7\ (8)\), \(p\equiv 1\ (n)\), every odd squarefree \(n\ge 3\)

proved lang/Inf7m8

Dirichlet: infinitely many primes \(p\equiv 3\ (8)\), \(p\equiv 1\ (2n)\), every odd squarefree \(n\ge 3\)

proved lang/Inf3m8

Dirichlet: infinitely many primes \(p\equiv 2\ (3)\), \(p\equiv 1\ (\ell ^{e+1})\), every prime \(\ell \equiv 5\ (6)\), every \(e\)

proved lang/Inf2m3

Gauss: \(\sum _j(-1)^jq^{j^2}=\prod _n(1-q^n)^2/(1-q^{2n})\) at every coefficient

proved lang/GaussMod (with FinJTP2, QBinom3, FinJTP3)

Jacobi: \(\prod _n(1-Q^n)^3=\sum _{j\ge 0}(-1)^j(2j+1)Q^{j(j+1)/2}\) at every coefficient

proved lang/JacobiP (with JacobiS, JacobiE, JacobiL)

32a: \(\eta (4z)^2\eta (8z)^2=\sum _{n\in \mathbb Z,\,j\ge 0}(-1)^{n+j}(2j+1)q^{4n^2+(2j+1)^2}\) at every coefficient

proved lang/Eta32

32a: the coefficient of \(q^n\) in \(\eta (4z)^2\eta (8z)^2\) is \(\operatorname {Re}c_n\), \(c_n=\sum _{\alpha \ \text {primary},\,N\alpha =n}\alpha \), for every \(n\)

proved lang/Hecke32 (with Trunc, TruncQ, TruncC, Eta32C, Gz, Prim32, Re32)

32a: \(a_p(y^2=x^3-x)=[q^p]\,\eta (4z)^2\eta (8z)^2\) at every prime \(p\equiv 1\ (4)\); \([q^p]=0\) at every \(p\equiv 3\ (4)\)

proved lang/Ap32 (with Hecke32, HeckePrime, SignCM3, HeckeInert)

32a: \(a_p(y^2=x^3-x)=[q^p]\,\eta (4z)^2\eta (8z)^2\) at every prime \(p\equiv 3\ (4)\) (both \(0\), \(\sum \chi \) in \(\mathbb Z\))

proved lang/Ap32i

32a: \([q^{mn}]=[q^m][q^n]\) in \(\eta (4z)^2\eta (8z)^2\) for all coprime odd \(m,n\)

proved lang/Mult32

32a: \([q^{p^{k+2}}]=[q^p][q^{p^{k+1}}]-p\,[q^{p^k}]\) at every odd prime \(p\)

proved lang/Rec32

32a, 36a: \(|a_p|^2\le 4p\) at every good prime

proved lang/Raman

32a: \([q^{2n}]\,\eta (4z)^2\eta (8z)^2=0\) for every \(n\)

proved lang/Even32

36a: \(\eta (6z)^4=\sum _{x\equiv 1\,(3)}x\,q^{x^2+3y^2}\) at every odd coefficient

proved lang/All36 (with Theta36, Bij36)

36a: \(\eta (6z)^4=\sum _{x\equiv 1\,(3)}x\,q^{x^2+3y^2}\) at every coefficient

proved lang/Eta36All (with Even36, Rot36, Orb36)

Hecke characters \(\alpha \mapsto \alpha ^k\) of \(\mathbb Q(i)\): \(c_k(mn)=c_k(m)c_k(n)\) for coprime odd \(m,n\)

proved lang/HeckeMultW

Hecke characters of \(\mathbb Q(i)\): \(c_W(p^{k+2})=(W\pi +W\bar \pi )c_W(p^{k+1})-W\pi \,W\bar \pi \,c_W(p^k)\) at split \(p\)

proved lang/HeckePrimeW

Hecke characters of \(\mathbb Q(i)\) at inert \(q\): \(c_W(q^{2j})=W(-q)^j\), \(c_W(q^{2j+1})=0\)

proved lang/HeckeInertW

Hecke characters of \(\mathbb Q(i)\): Euler product \(c_W(n_1\cdots n_r)=\prod c_W(n_i)\)

proved lang/HeckeEulerW

2-isogeny: \(\sum \chi (x(x^2+ax+b))=\sum \chi (x(x^2-2ax+a^2-4b))\), \(b\ne 0\)

proved lang/Isog2 (with SqCount)

CM by \(\mathbb Z[\sqrt {-2}]\): \(a_p(y^2=x^3+4x^2+2x)=0\) when \(\chi (-2)=-1\)

proved lang/CM2

CM by \(\mathbb Z[\sqrt {-2}]\): \(a_p(y^2=x^3+4x^2+2x)=0\) at every \(p\equiv 5,7\ (8)\)

proved lang/Chi2

Euler: \(\prod _n(1-t^n)=\sum _j(-1)^jt^{j(3j+1)/2}\) at every coefficient

proved lang/Pent (with PentE, PentD, PentL)

36a: \(\eta (6z)^4=\sum _{j\in \mathbb Z,\,i\ge 0}(-1)^{j+i}(2i+1)q^{((6j+1)^2+3(2i+1)^2)/4}\) at every coefficient

proved lang/Eta36

36a: \(a_p(y^2=x^3+1)=[q^p]\,\eta (6z)^4\) at every prime \(p\equiv 1\ (3)\)

proved lang/Ap36 (with Eta36C, Coef36, GzGen, Rep36, W36, CubicSign)

36a: \(a_p(y^2=x^3+1)=[q^p]\,\eta (6z)^4=0\) at every prime \(p\equiv 2\ (3)\)

proved lang/Ap36i

\(n\)-th powers: \(a\ne 0\) is an \(n\)-th power mod \(p\) iff \(a^{(p-1)/n}=1\), every \(n\mid p-1\)

proved lang/NthPow

\(n\)-th powers: exactly \((p-1)/n\) nonzero \(n\)-th powers mod \(p\)

proved lang/NthCount

Gauss sums of any order: \(G_\psi ^q=\psi (q)^{-1}G_\psi \) in characteristic \(q\)

proved lang/GaussFrobN

Gauss sums of any order: \(G_\psi G_{\bar \psi }=\bar \psi (-1)(p\zeta ^0-N)\)

proved lang/GaussNormN

Gauss and Jacobi sums: \(G_\chi G_\lambda =J(\chi ,\lambda )G_{\chi \lambda }\), \(\chi \lambda \ne 1\)

proved lang/JacobiGauss

\(n\)-th power residue criterion: \(\psi (q)^{-1}=(Ap)^{(q-1)/n}\) for \(q\equiv 1\ (n)\), every \(n\)

proved lang/NthRecip (with GaussPowN, GaussCritN)

\(n\)-th power residue criterion at \(q\equiv -1\ (n)\): \(\psi (q)\bar \psi (-1)=A^mp^{m-1}\)

proved lang/NthRecipInert

The criterion at \(n=2\): \(\chi _p(q^{-1})=(\chi _p(-1)p)^{(q-1)/2}\) in \(\mathbb F_q\)

proved lang/NthRecip2

\(n\)-th power residue criterion at every prime \(q\nmid np\) (via \(q^e\equiv 1\) mod \(n\))

proved lang/NthRecipPow

Eisenstein reciprocity for \(\ell =3\), inert case

proved lang/Eis3

Eisenstein reciprocity for \(\ell =3\), split case

proved lang/Eis3s

Quartic Eisenstein reciprocity, inert case

proved lang/Eis4

Quartic Eisenstein reciprocity, split case

proved lang/Eis4s

Fifth roots of unity in \(\mathbb F_p\)

proved lang/Quint1

Frobenius on \(\mathbb F_q[\zeta _5]\)

proved lang/Quint2

Explicit Frobenius on \(\mathbb F_q[\zeta _5]\) by \(q\bmod 5\)

proved lang/Quint3

Quintic character of \(\mathbb F_p\) with values in \(\mathbb Z[\zeta _5]\)

proved lang/Quint4

Quintic Jacobi sum has norm \(p\)

proved lang/Quint5

Reduction \(\mathbb Z[\zeta _5]\to \mathbb F_q[\zeta _5]\) and quintic character powers

proved lang/Quint6

Quintic residue criterion at \(q\equiv 4\pmod 5\)

proved lang/Quint7

Quintic residue criterion at \(q\equiv 1\pmod 5\)

proved lang/Quint8

Quintic residue criterion at every prime \(q\neq 5,p\)

proved lang/Quint9

Reflection formula for Jacobi sums

proved lang/JacRefl

The quintic product \(A=J(\psi ,\psi )^2J(\psi ^2,\psi ^2)\)

proved lang/Quint10

\(A=J^2\,\sigma _2(J)\) for the quintic Jacobi sum

proved lang/Quint11

Quintic residue criteria through \(J(\chi ,\chi )\)

proved lang/Quint12

Quintic split case in symbol form

proved lang/Quint13

Quintic case \(q\equiv 4\pmod 5\) in symbol form

proved lang/Quint14

A root of \(\Phi _5\) in \(T[\sqrt D]\) from \(\tfrac 12\) and \(\sqrt 5\)

proved lang/Quint15

\(\sqrt 5\in \mathbb F_q\) for \(q\equiv 4\pmod 5\); unconditional symbol form

proved lang/Quint16

Quintic symbol form at every odd prime \(q\neq 5,p\)

proved lang/Quint17

\(J^2\sigma _2(J)\equiv -1\pmod 5\) for the quintic Jacobi sum

proved lang/Quint18

\(\chi _p(q)^{-4}\equiv A^{(q^4-1)/5}\pmod q\), \(A=J^2\sigma _2(J)\)

proved lang/Quint19

\(A\overline A=p^3\) for \(A=J^2\sigma _2(J)\)

proved lang/Quint20

The ring \(K[\zeta _7]\)

proved lang/Cyclo7

Seventh roots of unity in \(\mathbb F_p\)

proved lang/Sept1

The septic character of \(\mathbb F_p\)

proved lang/Sept2

Septic Jacobi sum has norm \(p\)

proved lang/Sept3

Reduction \(\mathbb Z[\zeta _7]\to T[\zeta _7]\) and septic character powers

proved lang/Sept4

Septic residue criterion at every prime \(q\neq 7,p\)

proved lang/Sept5

Primary normalisation of the septic product (\(A\equiv -1\bmod 7\))

proved lang/Sept6

The septic product through three Jacobi sums

proved lang/Sept7

\(A=J^2\,\sigma _2(J')^2\,\sigma _3(J)\) for the septic Jacobi sums

proved lang/Sept8

Septic residue criterion through \(J(\chi ,\chi )\) and \(J(\chi ,\chi ^2)\)

proved lang/Sept9

Septic split case in symbol form

proved lang/Sept10

Septic symbol form at every prime \(q\neq 7,p\)

proved lang/Sept11

\(\sigma _2J'=J'\) for \(J'=J(\chi ,\chi ^2)\)

proved lang/Sept12

\(J(\chi ,\chi ^2)\) has norm \(p\)

proved lang/Sept13

\(4p=(2a-b)^2+7b^2\) from \(J(\chi ,\chi ^2)\)

proved lang/Sept14

\(A\overline A=p^5\) for \(A=J^2J'^2\sigma _3(J)\)

proved lang/Sept15

\(J(\chi ,\chi ^2)\equiv -1\pmod {1-\zeta }\), \(a+3b\equiv -1\pmod 7\)

proved lang/Sept16

Septic symbol form with \(J'\) at \(u\)

proved lang/Sept17

\(J\equiv J'\equiv -1\pmod {(1-\zeta )^2}\) (septic)

proved lang/Sept18

Number of seventh roots through the septic character

proved lang/Sept19

Fermat septic \(x^7+y^7=1\) through septic Jacobi sums

proved lang/Sept20

Trivial-character septic Jacobi sums; \(p-6+\sum ' J\)

proved lang/Sept21

Fermat septic count by Jacobi-sum classes

proved lang/Sept22

Fermat septic count by Galois conjugates of \(J\)

proved lang/Sept23

Klein quartic substitution \(x=y^3/v\)

proved lang/Sept24

Affine Klein count as a superelliptic count

proved lang/Sept25

Superelliptic count \(y^7=-v^2(1+v)\) by Jacobi sums

proved lang/Sept26

Klein quartic point count

proved lang/Sept27

Hecke eigen-sequences are determined by prime coefficients

proved lang/HeckeUniq

\(T_p\)-eigen sequences are multiplicative

proved lang/HeckeEigSeq

11a: \(a(n)\equiv \sigma (n)\pmod 5\) for \(11\nmid n\) (curve side)

proved lang/G4b1, lang/G4b1c

\(\mathbb Z[\omega ]\) is Euclidean (division, gcd, Euclid’s lemma)

proved lang/EisEuclid

Coprime splitting in \(\mathbb Z[\omega ]\): \(N(\gamma )=mn\), \((m,n)=1\) \(\Rightarrow \) \(\gamma =\alpha \beta \)

proved lang/EisCoprime

Primary normalisation in \(\mathbb Z[\omega ]\): \(3\nmid N(\alpha )\Rightarrow u\alpha \equiv 1\ (3)\)

proved lang/EisPrim

Unique primary splitting in \(\mathbb Z[\omega ]\) by coprime norms

proved lang/EisUnique3

Hecke characters of \(\mathbb Q(\sqrt {-3})\) of every \(\infty \)-type: theta coefficients multiplicative

proved lang/HeckeMult3

Factorisation in \(\mathbb Z[\omega ]\) above a split prime

proved lang/EisPrime3

Hecke characters of \(\mathbb Q(\sqrt {-3})\): recursion at split prime powers

proved lang/HeckePrime3

27a at every \(p\equiv 1\ (3)\): \(\#E(\mathbb F_p)=p+1-c(p)\), \(c\) the Hecke theta series

proved lang/FermatHecke

27a at \(p\equiv 2\ (3)\): \(\#E(\mathbb F_p)=p+1=p+1-c(p)\)

proved lang/FermatInert3

Hecke characters of \(\mathbb Q(\sqrt {-3})\) at inert prime powers

proved lang/EisInert3

The Hecke character of 36a as a multiplicative weight on \(\mathbb Z[\omega ]\)

proved lang/W36Mul

The theta series of \(W_{36}\) is the sum over \(S\)

proved lang/Theta36W

36a: the coefficients of \(\eta (6z)^4\) are multiplicative

proved lang/Mult36

36a: Hecke recursion at every prime \(p\nmid 6\)

proved lang/Hecke36Rec

\(\mathbb Z[\sqrt {-2}]\) is Euclidean

proved lang/Sq2Euclid

Coprime splitting in \(\mathbb Z[\sqrt {-2}]\)

proved lang/Sq2Coprime

Unique primary splitting in \(\mathbb Z[\sqrt {-2}]\)

proved lang/Sq2Unique

Hecke characters of \(\mathbb Q(\sqrt {-2})\) of every \(\infty \)-type: theta coefficients multiplicative

proved lang/HeckeMult2

The primary class of the Hecke character of 256a in \(\mathbb Z[\sqrt {-2}]\)

proved lang/Sq2Prim8

Unique splitting into \(H_8\)-elements in \(\mathbb Z[\sqrt {-2}]\)

proved lang/Sq2Unique8

256a: Hecke theta coefficients multiplicative

proved lang/HeckeMult8

Factorisation in \(\mathbb Z[\sqrt {-2}]\) above a split prime

proved lang/Sq2Prime8

256a: Hecke recursion at split prime powers

proved lang/HeckePrime8

256a: Hecke character at inert prime powers

proved lang/Sq2Inert8

256a at \(p\equiv 5,7\ (8)\): \(a_p=c(p)=0\)

proved lang/Curve256Inert

256a: the elliptic quotient as a twist

proved lang/Q256

The fibre sum for \(x+c/x\)

proved lang/FibSum

\(y^2=x^5-x\) splits into two elliptic quotients

proved lang/Genus2

\(y^2=x^5-x\) is twice 256a

proved lang/Split256

The Jacobsthal family on fourth-power classes

proved lang/Jac1

The weighted translation moment

proved lang/Jac2

The second moment of the Jacobsthal family

proved lang/Jac3

Inversion in the Jacobsthal family

proved lang/Jac4

\(T^2\) on the classes

proved lang/Jac5

\(T(1)^2+2T(g)^2=16p\)

proved lang/Jac6

Parity and uniqueness for \(a^2+2b^2\)

proved lang/Jac7a

256a at \(p\equiv 1\pmod 8\) up to sign

proved lang/Curve256Split

The fourth-power transform \(R(t)\)

proved lang/Sign1

\(T(1)\equiv p-5\pmod {16}\)

proved lang/Sign2

256a at \(p\equiv 1\pmod 8\) with sign

proved lang/Sign3

256a at \(p\equiv 1\pmod 8\): the Hecke statement

proved lang/Curve256Full

256a at every \(p\equiv 1\pmod 8\)

proved lang/Curve256P1

The ring \(\mathbb Z[(1+\sqrt {-7})/2]\)

proved lang/Ring7

\(\mathbb Z[(1+\sqrt {-7})/2]\) is Euclidean

proved lang/Euc7

Coprime splitting in \(\mathbb Z[(1+\sqrt {-7})/2]\)

proved lang/Cop7

The primary class of 49a in \(\mathbb Z[(1+\sqrt {-7})/2]\)

proved lang/Prim7

Unique splitting into \(H_7\)-elements

proved lang/Uniq7

49a: Hecke theta coefficients multiplicative

proved lang/HeckeMult7

Factorisation in \(\mathbb Z[(1+\sqrt {-7})/2]\) above a split prime

proved lang/Prime7

49a: Hecke recursion at split prime powers

proved lang/HeckePrime7

49a: Hecke character at inert prime powers

proved lang/Inert7

The ring \(\mathbb Z[(1+\sqrt {-11})/2]\)

proved lang/Ring11

\(\mathbb Z[(1+\sqrt {-11})/2]\) is Euclidean

proved lang/Euc11

Coprime splitting in \(\mathbb Z[(1+\sqrt {-11})/2]\)

proved lang/Cop11

The primary class of 121b in \(\mathbb Z[(1+\sqrt {-11})/2]\)

proved lang/Prim11

Unique splitting into \(H_{11}\)-elements

proved lang/Uniq11

121b: Hecke theta coefficients multiplicative

proved lang/HeckeMult11

Factorisation in \(\mathbb Z[(1+\sqrt {-11})/2]\) above a split prime

proved lang/Prime11

121b: Hecke recursion at split prime powers

proved lang/HeckePrime11

121b: Hecke character at inert prime powers

proved lang/Inert11

The ring \(\mathbb Z[(1+\sqrt {-19})/2]\)

proved lang/Ring19

Dedekind–Hasse step in \(\mathbb Z[(1+\sqrt {-19})/2]\)

proved lang/Euc19

gcd as a combination in \(\mathbb Z[(1+\sqrt {-19})/2]\)

proved lang/Gcd19

Coprime splitting in \(\mathbb Z[(1+\sqrt {-19})/2]\)

proved lang/Cop19

The primary class of 361a

proved lang/Prim19

Unique splitting into \(H_{19}\)

proved lang/Uniq19

361a: Hecke theta coefficients multiplicative

proved lang/HeckeMult19

Factorisation above a split prime (\(d=19\))

proved lang/Prime19

361a: Hecke recursion at split prime powers

proved lang/HeckePrime19

361a: Hecke character at inert prime powers

proved lang/Inert19

The ring \(\mathbb Z[(1+\sqrt {-43})/2]\)

proved lang/Ring43

Norm facts in \(\mathbb Z[(1+\sqrt {-43})/2]\)

proved lang/Euc43

Dedekind–Hasse step for \(d=43\)

proved lang/DH43

gcd in \(\mathbb Z[(1+\sqrt {-43})/2]\)

proved lang/Gcd43

Coprime splitting (\(d=43\))

proved lang/Cop43

The primary class of 1849a

proved lang/Prim43

Unique splitting into \(H_{43}\)

proved lang/Uniq43

1849a: Hecke theta multiplicative

proved lang/HeckeMult43

Factorisation above a split prime (\(d=43\))

proved lang/Prime43

1849a: Hecke recursion

proved lang/HeckePrime43

1849a: Hecke character at inert primes

proved lang/Inert43

The ring \(\mathbb Z[(1+\sqrt {-67})/2]\)

proved lang/Ring67

Norm facts in \(\mathbb Z[(1+\sqrt {-67})/2]\)

proved lang/Euc67

Dedekind–Hasse step for \(d=67\)

proved lang/DH67

gcd in \(\mathbb Z[(1+\sqrt {-67})/2]\)

proved lang/Gcd67

Coprime splitting (\(d=67\))

proved lang/Cop67

The primary class of 4489a

proved lang/Prim67

Unique splitting into \(H_{67}\)

proved lang/Uniq67

4489a: Hecke theta multiplicative

proved lang/HeckeMult67

Factorisation above a split prime (\(d=67\))

proved lang/Prime67

4489a: Hecke recursion

proved lang/HeckePrime67

4489a: Hecke character at inert primes

proved lang/Inert67

The ring \(\mathbb Z[(1+\sqrt {-163})/2]\)

proved lang/Ring163

Norm facts in \(\mathbb Z[(1+\sqrt {-163})/2]\)

proved lang/Euc163

Dedekind–Hasse step for \(d=163\)

proved lang/DH163

gcd in \(\mathbb Z[(1+\sqrt {-163})/2]\)

proved lang/Gcd163

Coprime splitting (\(d=163\))

proved lang/Cop163

The primary class of 26569a

proved lang/Prim163

Unique splitting into \(H_{163}\)

proved lang/Uniq163

26569a: Hecke theta multiplicative

proved lang/HeckeMult163

Factorisation above a split prime (\(d=163\))

proved lang/Prime163

26569a: Hecke recursion

proved lang/HeckePrime163

26569a: Hecke character at inert primes

proved lang/Inert163

1849a: vanishing at the ramified prime

proved lang/Ram43

4489a: vanishing at the ramified prime

proved lang/Ram67

26569a: vanishing at the ramified prime

proved lang/Ram163

49a: vanishing at the ramified prime

proved lang/Ram7

121b: vanishing at the ramified prime

proved lang/Ram11

361a: vanishing at the ramified prime

proved lang/Ram19

Lifting prime-power recursions

proved lang/HeckeEig

49a: Hecke eigenform at every unramified prime

proved lang/Eig7

121b: Hecke eigenform at every unramified prime

proved lang/Eig11

361a: Hecke eigenform at every unramified prime

proved lang/Eig19

1849a: Hecke eigenform at every unramified prime

proved lang/Eig43

4489a: Hecke eigenform at every unramified prime

proved lang/Eig67

26569a: Hecke eigenform at every unramified prime

proved lang/Eig163

Hecke eigenform for d = 3

proved lang/Eig3

Hecke eigenform for d = 2

proved lang/Eig8

Hecke eigenform for d = 1

proved lang/EigG

Vanishing at 2 and 3

proved lang/RamS

\(y^2+xy-ty=x^3-tx^2\) (\(t\ne 0\), \(16t+1\ne 0\)): \(4\mid \#E(\mathbb F_p)\)

proved lang/T4Count

\(y^2+(1-t)xy-ty=x^3-tx^2\) (\(t\ne 0\), \(t^2-11t-1\ne 0\)): \(5\mid \#E(\mathbb F_p)\)

proved lang/T5Done

\(y^2+(1-t)xy-(t+t^2)y=x^3-(t+t^2)x^2\) (\(t\ne 0,-1\), \(9t+1\ne 0\)): \(6\mid \#E(\mathbb F_p)\)

proved lang/T6Final

\(E_t\), \(b=t^3-t^2\), \(c=t^2-t\) (\(t\ne 0,1\), \(t^3-8t^2+5t+1\ne 0\)): \(7\mid \#E(\mathbb F_p)\)

proved lang/G7Final

Tate form \(E(b,c)\): \(P+T\) on \(E\), \(-P\) on \(E\), \(\tau (-\tau (-Q))=Q\)

proved lang/G7Tate1

Tate form \(E(b,c)\): \(P+2T=\big (bx(x^2-y)/y^2,\,-b(x^3-y^2)(x^2-y)/y^3+b\big )\)

proved lang/G7Tate2

Tate form \(E(b,c)\): \(P+3T=\big (yM/(x^2-y)^2,\,b-KM/(x^2-y)^3\big )\)

proved lang/G7Tate3

every \(g\): \(\sum _x\chi (g(x))+\#\{g=0\}\equiv p\pmod 2\)

proved lang/LegendreMoment

\(p>3\): \(\chi (-3)=1\iff 3\mid p-1\)

proved lang/LegendreMoment

\(\chi (-1)=1\iff 4\mid p-1\)

proved lang/LegendreMoment

\(\chi (-1)=\chi (2)=1\Rightarrow 8\mid p-1\)

proved lang/LegendreMoment

\(8\mid p-1\Rightarrow \chi (2)=1\)

proved lang/LegendreMoment

\(p\equiv \pm 1\pmod {12}\Rightarrow \chi (3)=1\)

proved lang/LegendreMoment

\(p\equiv \pm 5\pmod {12}\Rightarrow \chi (3)=-1\)

proved lang/LegendreMoment

\(5\mid p-1\Rightarrow \chi (5)=1\) (order-5 Gauss sum in \(\mathbb F_p\))

proved lang/LegendreMoment

\(q\equiv 4\pmod 5\Rightarrow \chi (5)=1\) (so \(\chi (5)=1\iff q\equiv \pm 1\pmod 5\))

proved lang/Gauss5Conv

\(7\mid p-1\Rightarrow \chi (-7)=1\) (order-7 Gauss sum in \(\mathbb F_p\))

proved lang/LegendreMoment

\(11\mid p-1\Rightarrow \chi (-11)=1\), \(13\mid p-1\Rightarrow \chi (13)=1\)

proved lang/LegendreMoment

\(17\mid p-1\Rightarrow \chi (17)=1\), \(19\mid p-1\Rightarrow \chi (-19)=1\)

proved lang/LegendreMoment

Stickelberger for cubics with a root: \(\chi (-4a^3-27b^2)=\chi (r^2-4c)\)

proved lang/LegendreMoment

Birch’s second moment over all curves: \(\sum _{a,b}\big (\sum _x\chi (x^3+ax+b)\big )^2=p^3-p^2\)

proved lang/PencilMoment

Birch’s third moment: \(\sum _{a,b}\big (\sum _x\chi (x^3+ax+b)\big )^3=0\)

proved lang/PencilMoment

Dirichlet for \(p\equiv 9\pmod {16}\): infinitely many such primes

proved lang/Inf9mod16

Dirichlet for \(p\equiv 2^{s+2}+1\pmod {2^{s+3}}\), every \(s\)

proved lang/Inf2e1

every odd Birch moment: \(\sum _{a,b}\big (\sum _x\chi (x^3+ax+b)\big )^{2k+1}=0\)

proved lang/PencilMoment

the four quartic twists cancel: \(\bar \chi (v)=i\Rightarrow \sum _{k=0}^{3}\sum _x\rho (x^3-v^kDx)=0\)

proved lang/QuartQuartic

second moment over the quartic twists: \(\sum _{k=0}^{3}\big (\sum _x\rho (x^3-v^kDx)\big )^2=8p\)

proved lang/QuartQuartic

fourth moment over the quartic twists: \(\sum _{k=0}^{3}a_k^4=24p^2+4(J^4+\bar J^4)\)

proved lang/QuartQuartic

third moment over the quartic twists: \(\sum _{k=0}^{3}\big (\sum _x\rho (x^3-v^kDx)\big )^3=0\)

proved lang/QuartQuartic

quartic twists of \(y^2=x^4+D\): \(\chi (v)=i,\ \rho (v)=-1\Rightarrow \sum _{k=0}^{3}\sum _x\rho (x^4+v^kD)=-4\)

proved lang/QuartQuartic

second moment over the quartic twists of \(y^2=x^4+D\): \(\sum _{k=0}^{3}\big (1+\sum _x\rho (x^4+v^kD)\big )^2=8p\)

proved lang/QuartMoments

fourth moment over the quartic twists of \(y^2=x^4+D\): \(\sum _{k=0}^{3}\big (1+\sum _x\rho (x^4+v^kD)\big )^4=24p^2+4(J^4+\bar J^4)\)

proved lang/QuartMoments

quartic twists of the Fermat quartic: \(\chi (v)=i,\ \rho (v)=-1\Rightarrow \sum _{k=0}^{3}\#\{x^4+y^4=v^kc\}=4(p-1)-8\chi (-1)\)

proved lang/QuartTwistCount

second moment over the quartic twists of the Fermat quartic: \(\sum _{k=0}^{3}\big (\#\{x^4+y^4=v^kc\}-p+1+2\chi (-1)\big )^2=4(J+\bar J)^2+32p\)

proved lang/QuartMoments

genus \(2\): \(\sum _x\rho (x^6+D)=-1+\rho (D)\,\mathrm {Tr}\,\chi (-D)J(\chi ,\rho )+\rho (D)\,\mathrm {Tr}\,\psi (-D)J(\psi ,\rho )\), \(\psi =\chi \rho \), \(J(\psi ,\rho )=\rho (-1)\bar \chi (4)\bar J\), at \(p\equiv 1\ (3)\)

proved lang/CubicSix

the cubic twists \(x^3+y^3=c\): \(\#\{x^3+y^3=c\}=p-2+\mathrm {Tr}\,\bar \chi (c)J\) for every \(c\ne 0\), and \((\#-p+2)^2\le 4p\); also \(a_p^2\le 4p\) for every \(y^2=x^3+D\)

proved lang/CubicTwistCount

the Fermat cubic and \(y^2=x^3-432\) have the same number of points: \(\#\{x^3+y^3=1\}+3=p+1+\sum _x\rho (x^3-432)\)

proved lang/CubicTwistCount

3-isogenous twists: \(\sum _x\rho (x^3+D)=\sum _x\rho (x^3-27D)\)

proved lang/CubicTwistCount

the three cubic twists cancel: \(\chi (v)=\omega \Rightarrow \sum _{k=0}^{2}\sum _x\rho (x^3+v^{2k}D)=0\)

proved lang/CubicTwistCount

the six sextic twists cancel: \(\chi (v)=\omega ,\ \rho (v)=-1\Rightarrow \sum _{k=0}^{5}\sum _x\rho (x^3+v^kD)=0\)

proved lang/CubicTwistCount

second moment over the sextic twists: \(\sum _{k=0}^{5}\big (\sum _x\rho (x^3+v^kD)\big )^2=12p\)

proved lang/CubicTwistCount

fourth moment over the sextic twists: \(\sum _{k=0}^{5}\big (\sum _x\rho (x^3+v^kD)\big )^4=36p^2\)

proved lang/CubicTwistCount

third moment over the sextic twists: \(\sum _{k=0}^{5}\big (\sum _x\rho (x^3+v^kD)\big )^3=0\)

proved lang/CubicTwistCount

sixth moment over the sextic twists: \(\sum _{k=0}^{5}a_k^6=120p^3+6(J^6+\bar J^6)\)

proved lang/CubicTwistCount

second moment over the twists of the genus-2 curve: \(\sum _{k=0}^{5}\big (1+\sum _x\rho (x^6+v^kD)\big )^2=24p\)

proved lang/CubicTwistCount

cubic twists of the Fermat curve: \(\chi (v)=\omega \Rightarrow \sum _{k=0}^{2}\#\{x^3+y^3=v^kc\}=3(p-2)\)

proved lang/CubicTwistCount

second moment over the cubic twists of the Fermat curve: \(\sum _{k=0}^{2}\big (\#\{x^3+y^3=v^kc\}-p+2\big )^2=6p\)

proved lang/CubicMoments

third moment over the cubic twists of the Fermat curve: \(\sum _{k=0}^{2}\big (\#\{x^3+y^3=v^kc\}-p+2\big )^3=3(J^3+\bar J^3)\)

proved lang/CubicMoments

sextic twists of \(y^2=x^6+D\): \(\chi (v)=\omega ,\ \rho (v)=-1\Rightarrow \sum _{k=0}^{5}\sum _x\rho (x^6+v^kD)=-6\)

proved lang/CubicSix

modularity of the Fermat cubic (27a) at every prime \(p<200\), \(p\ne 3\): \(a_p(\eta (3z)^2\eta (9z)^2)=p+1-\#X(\mathbb F_p)\) (evaluation)

proved lang/Fermat27a

modularity of 32a (\(\eta (4z)^2\eta (8z)^2\)) and 36a (\(\eta (6z)^4\)) at every prime \(p<200\) of good reduction (evaluation)

proved lang/CMModularCheck

Hecke theta series \(=\) \(\eta \)-products for 32a, 27a, 36a, coefficients \(q^1,\dots ,q^{199}\) (evaluation)

proved lang/ThetaEta

the \(\eta \)-products of 32a, 27a, 36a are Hecke eigenforms for \(n<200\) (evaluation)

proved lang/HeckeCheck

the \(\eta \)-product of 11a is a Hecke eigenform for \(n<200\) (evaluation)

proved lang/HeckeCheck11

Ramanujan–Hasse bound for 11a: \(a(p)^2\le 4p\) for every prime \(p<200\)

proved lang/Ramanujan11

Hecke recursion \(a(p^{k+1})=a(p)a(p^k)-p\,a(p^{k-1})\) at all good prime powers \(<200\) (11a, 32a, 27a, 36a)

proved lang/Ramanujan11

11a: \(a(p)\equiv 1+p\pmod 5\) for every prime \(p\ne 11\) below 200

proved lang/Ramanujan11

the quartic twists \(x^4+y^4=c\): \(\#=p-1-2\chi (-1)+\rho (c)\,\mathrm {Tr}\,J+2\,\mathrm {Tr}\,\bar \chi (c)K\), \(K=\chi (-1)J\)

proved lang/QuartTwistCount

Weil’s diagonal cubic surface: \(\#\{x^3+y^3+z^3=1\}=p^2+6p-(J+\bar J)\) at \(p\equiv 1\ (3)\)

proved lang/CubicSurface

the diagonal quartic (K3) surface: \(\#\{x^4+y^4+z^4=1\}=p^2+17p+4a^2-6a\) (\(p\equiv 1\ (8)\)), \(p^2-3p-4a^2-6a\) (\(p\equiv 5\ (8)\))

proved lang/QuartSurface

the surfaces \(x^3+y^3+z^3=c\) and the cubic threefold: \(\#\{x^3+y^3+z^3+u^3=1\}=p^3+5p\,\mathrm {Tr}\,J-6p\)

proved lang/CubicThreefold

diagonal cubic hypersurfaces in every dimension: \(\#\{x_1^3+\dots +x_k^3=c\}=A_k+B_k\bar \chi (c)+B'_k\chi (c)\) (\(c\ne 0\)), \(C_k\) (\(c=0\)), by a recursion in \(J,\bar J\)

proved lang/CubicDiagonal

every diagonal cubic form \(\sum a_ix_i^3\) (\(a_i\ne 0\)) over \(\mathbb F_p\), \(p\equiv 1\ (3)\): \(\#\{=c\}=A+B\bar \chi (c)+B'\chi (c)\), recursion in \(\bar \chi (a_i)\), \(J\)

proved lang/CubicDiagonal

diagonal quartic hypersurfaces in every dimension: \(\#\{x_1^4+\dots +x_k^4=c\}=A_k+B_k\bar \chi (c)+B^{(2)}_k\rho (c)+B^{(3)}_k\chi (c)\), \(C_k\) at \(c=0\)

proved lang/QuartDiagonal

every diagonal quartic form \(\sum a_ix_i^4\) (\(a_i\ne 0\)) over \(\mathbb F_p\), \(p\equiv 1\ (4)\): the same shape with a recursion in \(\chi ^j(a_i)\)

proved lang/QuartDiagonal

sums of \(k\) squares over \(\mathbb F_p\) for every odd \(p\) and every \(k\): \(\#\{x_1^2+\dots +x_k^2=c\}=A_k+B_k\left (\frac cp\right )\) (\(c\ne 0\)), \(C_k\) (\(c=0\))

proved lang/QuadDiagonal

every nondegenerate diagonal quadratic form \(\sum a_ix_i^2\) over \(\mathbb F_p\): \(\#\{\sum a_ix_i^2=c\}=A+B\left (\frac cp\right )\), recursion with \(\left (\frac {a_i}p\right )\)

proved lang/QuadDiagonal

closed forms for sums of squares: \(p^{2j}+e^jp^j\left (\frac cp\right )\) (\(k=2j+1\)), \(p^{2j+1}-e^{j+1}p^j\) (\(k=2j+2\), \(c\ne 0\)), \(e=\left (\frac {-1}p\right )\)

proved lang/QuadDiagonal

closed forms for every diagonal form \(\sum a_ix_i^2\) in terms of \(d=\prod \left (\frac {a_i}p\right )\) (the discriminant character)

proved lang/QuadDiagonal

Gauss’s cyclotomic number \((0,0)\) of order \(3\): \(9\,\#\{a: a,1-a \text { nonzero cubes}\}=p-8+(2a-b)\)

proved lang/CubicCyclo

all nine cyclotomic numbers of order \(3\): \(9\,\#\{a:\chi (a)=v,\chi (1-a)=u\}=p+\bar v\bar uJ+vu\bar J-\bar vu-v\bar u-(1+\bar v+v)-(1+\bar u+u)\)

proved lang/CubicCyclo

all sixteen cyclotomic numbers of order \(4\): \(16\,\#\{a:\chi (a)=v,\chi (1-a)=u\}\) in terms of \(J\), \(J(\rho ,\chi )=\chi (-1)J\) and \(\chi (-1)\)

proved lang/QuartCyclo

torsion Galois representations; patching; CM potential automorphy

cited [985, 992, 997]

compatibility at \(\ell =p\) in general

remaining R1

\(\det d\ne 0\) of the patched map at each non-Eisenstein \(\mathfrak m\)

remaining R1 (T21)

descent of a virtual sum for non-monomial \(\rho \)

remaining R2

Poisson cancellation for \(\mathrm {GL}_3\)

remaining R3

analytic package for \(L(s,\mathrm {Sym}^5\pi \times \tau )\), Maass / general \(F\)

remaining R4

global \(\mathrm {Bun}_G\) for a number field

remaining R5

Artin holomorphy, even icosahedral

remaining R7

\(\mathrm {GL}_n\) reciprocity over number fields; general functoriality

remaining —
Table 12: Status of the paths.

78.14 GL\(_1\): reciprocity as a split idempotent, and the shell over a number field

Let \(\mathrm {place}:A\to B\) and \(\mathrm {read}:B\to A\) satisfy \(\mathrm {read}\circ \mathrm {place}=\mathrm {id}\), and put \(e=\mathrm {place}\circ \mathrm {read}\). For a group \(G\), primes \(P\), a Frobenius map \(\mathrm {frob}:P\to G\) and a Chebotarev section \(\mathrm {sec}\) with \(\mathrm {frob}\circ \mathrm {sec}=\mathrm {id}\),

\[ e\circ e=e,\qquad b\in \operatorname {im}\mathrm {place}\iff e\,b=b,\qquad \lambda \ \text {factors through}\ \mathrm {frob}\iff \lambda =\lambda \circ \mathrm {sec}\circ \mathrm {frob}. \]
The steps are
\[ \begin {aligned} e\,e\,b&=\mathrm {place}(\mathrm {read}\,\mathrm {place}(\mathrm {read}\,b))=\mathrm {place}(\mathrm {read}\,b)=e\,b,\\ \lambda =\mu \circ \mathrm {frob}&\Rightarrow \lambda \circ \mathrm {sec}\circ \mathrm {frob}=\mu \circ \mathrm {frob}\circ \mathrm {sec}\circ \mathrm {frob}=\mu \circ \mathrm {frob}=\lambda ,\\ (a+b)^p&=a^p+b^p\quad \text {in characteristic }p\ \text {(Pascal rows, }(k+1)\tbinom {n+1}{k+1}=(n+1)\tbinom nk). \end {aligned} \]

Listing 21: lang/Recip1.lean

lang/Recip1.lean

/-
  lang/Recip1 — T43, cycle 1 (branch R8): reciprocity as a split idempotent, and GL1 over ℚ.

  The pattern of the chain (FTAChain.split_idem, read_inj): placing then reading is the identity,
  reading then placing is idempotent. Read as an adjunction between a Galois side `A` and an
  automorphic side `B`:
    unit_iso      read ∘ place = id                         (the unit is an isomorphism)
    e_idem        e = place ∘ read is idempotent
    image_iff     b is in the image of place  ↔  e b = b     (the counit is an isomorphism on Fix e)
    place_inj     place is injective
  So A ≅ Fix(e): reciprocity is the statement that the automorphic objects are the fixed points.

  GL1 over ℚ, modulus N. Galois side: functions χ on residues (Dirichlet-type, periodic mod N).
  Automorphic side: eigen-systems λ : ℕ → R indexed by primes. place χ = (p ↦ χ (p % N)) is
  "Frobenius at p is p mod N". read λ = (a ↦ λ (w a)) with a prime witness w a ≡ a (mod N).
    gl1_unit      read (place χ) = χ on residues with a witness
    gl1_fix       an eigen-system is fixed by e iff it is p ↦ χ (p % N) for some χ (pointwise)
  Witnesses for N = 5 and N = 8 are checked by evaluation (`rfl` on concrete numbers).

  Lean 4 core only, proofs by hand, no axioms.
-/

namespace Recip1

universe u

section Split
variable {A B : Type u} (place : A → B) (read : B → A)

def e (b : B) : B := place (read b)

theorem unit_iso (h : ∀ a, read (place a) = a) (a : A) : read (place a) = a := h a

theorem e_idem (h : ∀ a, read (place a) = a) (b : B) : e place read (e place read b) = e place read b :=
  congrArg place (h (read b))

theorem image_iff (h : ∀ a, read (place a) = a) (b : B) : (∃ a, place a = b) ↔ e place read b = b :=
  ⟨fun ⟨a, ha⟩ => by
      show place (read b) = b
      rw [← ha, h a],
   fun hb => ⟨read b, hb⟩⟩

theorem place_inj (h : ∀ a, read (place a) = a) {a a' : A} (hp : place a = place a') : a = a' :=
  Eq.trans (h a).symm (Eq.trans (congrArg read hp) (h a'))

end Split

/-! ## GL1 over ℚ -/

section GL1
variable {R : Type u} (N : Nat) (w : Nat → Nat)

/-- Galois side: functions on ℕ that only see the residue mod N. -/
def Periodic (χ : Nat → R) : Prop := ∀ n, χ n = χ (n % N)

/-- Frobenius at p acts through p mod N. -/
def placeF (χ : Nat → R) : Nat → R := fun p => χ (p % N)

/-- Read an eigen-system at the prime witnesses. -/
def readF (lam : Nat → R) : Nat → R := fun a => lam (w (a % N))

theorem mod_mod' (a : Nat) (hw : ∀ b, b < N → w b % N = b) (hN : 0 < N) :
    w (a % N) % N = a % N := hw (a % N) (Nat.mod_lt a hN)

theorem gl1_unit (χ : Nat → R) (hχ : Periodic N χ) (hw : ∀ b, b < N → w b % N = b) (hN : 0 < N)
    (a : Nat) : readF N w (placeF N χ) a = χ a := by
  show χ (w (a % N) % N) = χ a
  rw [mod_mod' N w a hw hN, ← hχ a]

/-- **GL1 reciprocity (finite form).** An eigen-system `λ` is fixed by `e = place ∘ read` iff it is
    `p ↦ χ (p % N)` for a periodic `χ`. -/
theorem gl1_fix (lam : Nat → R) (hw : ∀ b, b < N → w b % N = b) (hN : 0 < N)
    (hmod : ∀ a, a < N → a % N = a) :
    (∃ χ : Nat → R, Periodic N χ ∧ ∀ p, placeF N χ p = lam p) ↔ ∀ p, placeF N (readF N w lam) p = lam p := by
  constructor
  · intro ⟨χ, hχ, hl⟩ p
    rw [← hl p]
    show lam (w ((p % N) % N)) = χ (p % N)
    rw [← hl (w ((p % N) % N))]
    show χ (w ((p % N) % N) % N) = χ (p % N)
    have hpp : (p % N) % N = p % N := hmod (p % N) (Nat.mod_lt p hN)
    rw [hpp, hw (p % N) (Nat.mod_lt p hN)]
  · intro hf
    refine ⟨readF N w lam, fun n => ?_, hf⟩
    show lam (w (n % N)) = lam (w ((n % N) % N))
    rw [hmod (n % N) (Nat.mod_lt n hN)]

end GL1

/-! ## Prime witnesses, checked by evaluation -/

/-- Trial-division primality as a Boolean. -/
def noDivBelow (n : Nat) : Nat → Bool
  | 0 => true
  | 1 => true
  | k + 2 => (n % (k + 2) != 0) && noDivBelow n (k + 1)

def isPrimeB (n : Nat) : Bool := (1 < n) && noDivBelow n (n - 1)

/-- Witnesses mod 5: residues 1, 2, 3, 4 -> primes 11, 2, 3, 19 (residue 0: 5). -/
def w5 : Nat → Nat
  | 0 => 5 | 1 => 11 | 2 => 2 | 3 => 3 | _ => 19

theorem w5_prime : (isPrimeB (w5 1), isPrimeB (w5 2), isPrimeB (w5 3), isPrimeB (w5 4))
    = (true, true, true, true) := rfl

theorem w5_res : (w5 0 % 5, w5 1 % 5, w5 2 % 5, w5 3 % 5, w5 4 % 5) = (0, 1, 2, 3, 4) := rfl

theorem w5_hw : ∀ b, b < 5 → w5 b % 5 = b
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, hk => absurd hk (Nat.not_lt_of_le (Nat.le_add_left 5 k))

/-- Witnesses mod 8: odd residues 1, 3, 5, 7 -> primes 17, 3, 5, 7; even residues by 2 and placeholders. -/
def w8 : Nat → Nat
  | 0 => 8 | 1 => 17 | 2 => 2 | 3 => 3 | 4 => 4 | 5 => 5 | 6 => 6 | _ => 7

theorem w8_prime : (isPrimeB (w8 1), isPrimeB (w8 3), isPrimeB (w8 5), isPrimeB (w8 7))
    = (true, true, true, true) := rfl

theorem w8_hw : ∀ b, b < 8 → w8 b % 8 = b
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | k + 8, hk => absurd hk (Nat.not_lt_of_le (Nat.le_add_left 8 k))

theorem mod5_small : ∀ a, a < 5 → a % 5 = a
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, hk => absurd hk (Nat.not_lt_of_le (Nat.le_add_left 5 k))

theorem mod8_small : ∀ a, a < 8 → a % 8 = a
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | k + 8, hk => absurd hk (Nat.not_lt_of_le (Nat.le_add_left 8 k))

/-- GL1 reciprocity mod 5 and mod 8, with the witnesses above. -/
theorem gl1_mod5 {R : Type u} (lam : Nat → R) :
    (∃ χ : Nat → R, Periodic 5 χ ∧ ∀ p, placeF 5 χ p = lam p) ↔ ∀ p, placeF 5 (readF 5 w5 lam) p = lam p :=
  gl1_fix 5 w5 lam w5_hw (Nat.zero_lt_succ 4) mod5_small

theorem gl1_mod8 {R : Type u} (lam : Nat → R) :
    (∃ χ : Nat → R, Periodic 8 χ ∧ ∀ p, placeF 8 χ p = lam p) ↔ ∀ p, placeF 8 (readF 8 w8 lam) p = lam p :=
  gl1_fix 8 w8 lam w8_hw (Nat.zero_lt_succ 7) mod8_small

end Recip1

#print axioms Recip1.unit_iso
#print axioms Recip1.e_idem
#print axioms Recip1.image_iff
#print axioms Recip1.place_inj
#print axioms Recip1.mod_mod'
#print axioms Recip1.gl1_unit
#print axioms Recip1.gl1_fix
#print axioms Recip1.w5_prime
#print axioms Recip1.w5_res
#print axioms Recip1.w5_hw
#print axioms Recip1.w8_prime
#print axioms Recip1.w8_hw
#print axioms Recip1.mod5_small
#print axioms Recip1.mod8_small
#print axioms Recip1.gl1_mod5
#print axioms Recip1.gl1_mod8

Listing 22: lang/Frobenius.lean

lang/Frobenius.lean

/-
  lang/Frobenius — T43, cycle 2: GL1 over a general number field, the general pieces.

  (a) The reciprocity shell, for any group G and any set of primes P with a Frobenius map
      frob : P → G and a Chebotarev section sec : G → P (frob (sec g) = g):
        shell          λ factors through frob  ↔  λ = λ ∘ sec ∘ frob
        shell_unique   the factor is λ ∘ sec
      Over a number field F with abelian L/F, G = Gal(L/F) and frob p = Artin symbol; the arithmetic
      inputs are the Frobenius map itself and the section (Chebotarev). For non-abelian G the same
      shell applies with frob landing in conjugacy classes.
  (b) Frobenius is additive in characteristic p, in every commutative ring (every residue field
      O_F/p of every number field):
        ev_nxt         ev (nxt p r) = (a + b) · ev r + p • b^|r|   (Pascal rows, no subtraction)
        ev_row         (a + b)^n = ev (row n)                       (binomial theorem)
        C_absorb       (k + 1) C(n+1, k+1) = (n + 1) C(n, k)
        dv_choose      p | C(p, k+1) for k + 1 < p, p prime          (Euclid: FTAChain.euclid_core)
        row_eq         row n = [C(n,0), …, C(n,n)]
        nsm_char       p • 1 = 0  ⇒  (q p) • y = 0
        frob_add       p prime, p • 1 = 0  ⇒  (a + b)^p = a^p + b^p
        pw_mul         (a b)^n = a^n b^n
      So x ↦ x^p is a ring endomorphism of every commutative ring of characteristic p.

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain
import FTAChain

namespace Frobenius

/-! ## (a) The shell -/

section Shell
variable {P G R : Type} (frob : P → G) (sec : G → P) (hs : ∀ g, frob (sec g) = g)

theorem shell (lam : P → R) (hs : ∀ g, frob (sec g) = g) :
    (∃ χ : G → R, ∀ p, lam p = χ (frob p)) ↔ ∀ p, lam p = lam (sec (frob p)) :=
  ⟨fun ⟨χ, hχ⟩ p => by rw [hχ p, hχ (sec (frob p)), hs (frob p)],
   fun h => ⟨fun g => lam (sec g), h⟩⟩

theorem shell_unique (lam : P → R) (χ : G → R) (hs : ∀ g, frob (sec g) = g)
    (hχ : ∀ p, lam p = χ (frob p)) (g : G) : χ g = lam (sec g) := by
  rw [hχ (sec g), hs g]

end Shell

/-! ## (b) Pascal rows and the binomial theorem in a commutative ring -/

section Binom
variable {R : Type} (K : DetChain.CRing R)

/-- `n • x`. -/
def nsm : Nat → R → R
  | 0, _ => K.zero
  | n + 1, x => K.add (nsm n x) x

def pw (x : R) : Nat → R
  | 0 => K.one
  | k + 1 => K.mul (pw x k) x

/-- Homogeneous evaluation of a coefficient list `c_0, …, c_d`: `Σ c_k a^k b^(d-k)`. -/
def ev (a b : R) : List Nat → R
  | [] => K.zero
  | c :: cs => K.add (nsm K c (pw K b cs.length)) (K.mul a (ev a b cs))

/-- Next Pascal row: `n_k = r_k + r_(k-1)`. -/
def nxt : Nat → List Nat → List Nat
  | p, [] => [p]
  | p, c :: cs => (c + p) :: nxt c cs

def row : Nat → List Nat
  | 0 => [1]
  | n + 1 => nxt 0 (row n)

theorem nsm_add (m n : Nat) (x : R) : nsm K (m + n) x = K.add (nsm K m x) (nsm K n x) := by
  induction n with
  | zero => exact (DetChain.add_zero' K _).symm
  | succ n ih =>
    show K.add (nsm K (m + n) x) x = K.add (nsm K m x) (K.add (nsm K n x) x)
    rw [ih, K.add_assoc]

theorem mul_nsm (a : R) (n : Nat) (x : R) : K.mul a (nsm K n x) = nsm K n (K.mul a x) := by
  induction n with
  | zero => exact DetChain.mul_zero K a
  | succ n ih =>
    show K.mul a (K.add (nsm K n x) x) = K.add (nsm K n (K.mul a x)) (K.mul a x)
    rw [K.distrib, ih]

theorem len_nxt : ∀ (p : Nat) (r : List Nat), (nxt p r).length = r.length + 1
  | _, [] => rfl
  | _, c :: cs => congrArg Nat.succ (len_nxt c cs)

theorem add4 (u v w z : R) : K.add (K.add u v) (K.add w z) = K.add (K.add u w) (K.add v z) :=
  DetChain.add_mid4 K u v w z

theorem nsm_eq_mul (n : Nat) (x : R) : nsm K n x = K.mul (nsm K n K.one) x := by
  induction n with
  | zero => exact (DetChain.zero_mul' K x).symm
  | succ n ih =>
    show K.add (nsm K n x) x = K.mul (K.add (nsm K n K.one) K.one) x
    rw [ih, DetChain.right_distrib, K.one_mul]

theorem perm4 (X Y Z U : R) : K.add (K.add X Y) (K.add Z U) = K.add (K.add (K.add U X) Z) Y := by
  rw [K.add_comm Z U, add4 K, K.add_comm X U, K.add_assoc (K.add U X) Z Y, K.add_comm Z Y,
    ← K.add_assoc (K.add U X) Y Z, K.add_assoc U X Y, K.add_comm (K.add U (K.add X Y)) Z,
    K.add_comm Z, K.add_assoc U (K.add X Y) Z, K.add_comm (K.add X Y) Z, ← K.add_assoc U Z]

theorem swap3' (a s E : R) : K.mul a (K.mul s E) = K.mul s (K.mul a E) := by
  rw [← K.mul_assoc, K.mul_comm a s, K.mul_assoc]

theorem ring_step (a b C P E B : R) :
    K.add (K.add (K.mul C (K.mul B b)) (K.mul P (K.mul B b))) (K.mul a (K.add (K.mul (K.add a b) E) (K.mul C B)))
      = K.add (K.mul (K.add a b) (K.add (K.mul C B) (K.mul a E))) (K.mul P (K.mul B b)) := by
  have l1 : K.mul a (K.add (K.mul (K.add a b) E) (K.mul C B))
      = K.add (K.mul (K.add a b) (K.mul a E)) (K.mul a (K.mul C B)) := by
    rw [K.distrib, swap3' K a (K.add a b) E]
  have r1 : K.mul (K.add a b) (K.add (K.mul C B) (K.mul a E))
      = K.add (K.add (K.mul a (K.mul C B)) (K.mul C (K.mul B b))) (K.mul (K.add a b) (K.mul a E)) := by
    rw [K.distrib, DetChain.right_distrib, K.mul_comm b (K.mul C B), K.mul_assoc C B b]
  rw [l1, r1]
  exact perm4 K _ _ _ _

/-- **Pascal step**: `ev (nxt p r) = (a + b) · ev r + p • b^|r|`. -/
theorem ev_nxt (a b : R) : ∀ (p : Nat) (r : List Nat),
    ev K a b (nxt p r) = K.add (K.mul (K.add a b) (ev K a b r)) (nsm K p (pw K b r.length))
  | p, [] => by
    show K.add (nsm K p (pw K b 0)) (K.mul a K.zero) = K.add (K.mul (K.add a b) K.zero) (nsm K p (pw K b 0))
    rw [DetChain.mul_zero, DetChain.mul_zero, DetChain.add_zero', K.zero_add]
  | p, c :: cs => by
    show K.add (nsm K (c + p) (pw K b (nxt c cs).length)) (K.mul a (ev K a b (nxt c cs)))
      = K.add (K.mul (K.add a b) (K.add (nsm K c (pw K b cs.length)) (K.mul a (ev K a b cs))))
          (nsm K p (pw K b (cs.length + 1)))
    rw [ev_nxt a b c cs, len_nxt, nsm_add]
    have hB' : pw K b (cs.length + 1) = K.mul (pw K b cs.length) b := rfl
    rw [hB', nsm_eq_mul K c (K.mul (pw K b cs.length) b), nsm_eq_mul K p (K.mul (pw K b cs.length) b),
      nsm_eq_mul K c (pw K b cs.length)]
    exact ring_step K a b _ _ _ _

theorem ev_row (a b : R) : ∀ n, ev K a b (row n) = pw K (K.add a b) n
  | 0 => by
    show K.add (nsm K 1 (pw K b 0)) (K.mul a K.zero) = K.one
    rw [DetChain.mul_zero, DetChain.add_zero']
    show K.add K.zero K.one = K.one
    exact K.zero_add _
  | n + 1 => by
    show ev K a b (nxt 0 (row n)) = K.mul (pw K (K.add a b) n) (K.add a b)
    rw [ev_nxt, ev_row a b n]
    show K.add (K.mul (K.add a b) (pw K (K.add a b) n)) K.zero = _
    rw [DetChain.add_zero', K.mul_comm]

end Binom

/-! ## Binomial coefficients, and p | C(p, k) -/

section Choose
open FTAChain

def C : Nat → Nat → Nat
  | 0, 0 => 1
  | 0, _ + 1 => 0
  | _ + 1, 0 => 1
  | n + 1, k + 1 => C n k + C n (k + 1)

theorem C_zero_right : ∀ n, C n 0 = 1
  | 0 => rfl
  | _ + 1 => rfl

theorem C_above : ∀ n k, C n (k + n + 1) = 0
  | 0, _ => rfl
  | n + 1, k => by
    show C n (k + n + 1) + C n (k + n + 1 + 1) = 0
    have e : k + n + 1 + 1 = (k + 1) + n + 1 := by rw [nadd_right_comm k n 1]
    rw [C_above n k, e, C_above n (k + 1)]

theorem C_above' (n : Nat) : C n (n + 1) = 0 := by
  have h := C_above n 0
  rw [nzero_add] at h
  exact h

theorem C_self : ∀ n, C n n = 1
  | 0 => rfl
  | n + 1 => by
    show C n n + C n (n + 1) = 1
    rw [C_self n, C_above' n]

theorem C_one : ∀ n, C (n + 1) 1 = n + 1
  | 0 => rfl
  | n + 1 => by
    show C (n + 1) 0 + C (n + 1) 1 = n + 1 + 1
    rw [C_zero_right, C_one n, nadd_comm]

/-- `(k + 1) · C(n+1, k+1) = (n + 1) · C(n, k)`. -/
theorem C_absorb : ∀ n k, (k + 1) * C (n + 1) (k + 1) = (n + 1) * C n k
  | 0, 0 => rfl
  | 0, k + 1 => by
    show (k + 1 + 1) * (C 0 (k + 1) + C 0 (k + 1 + 1)) = 1 * C 0 (k + 1)
    rfl
  | n + 1, 0 => by
    show 1 * (C (n + 1) 0 + C (n + 1) 1) = (n + 1 + 1) * C (n + 1) 0
    rw [C_zero_right, C_one, nmul_one (n + 1 + 1), nmul_comm 1, nmul_one, nadd_comm]
  | n + 1, j + 1 => by
    show (j + 1 + 1) * (C (n + 1) (j + 1) + C (n + 1) (j + 1 + 1)) = (n + 1 + 1) * C (n + 1) (j + 1)
    rw [nleft_distrib, C_absorb n (j + 1)]
    have e1 : (j + 1 + 1) * C (n + 1) (j + 1) = (n + 1) * C n j + C (n + 1) (j + 1) := by
      rw [nsucc_mul, C_absorb n j]
    rw [e1]
    show (n + 1) * C n j + C (n + 1) (j + 1) + (n + 1) * C n (j + 1) = (n + 1 + 1) * (C n j + C n (j + 1))
    rw [nsucc_mul (n + 1), nleft_distrib (n + 1), nadd_right_comm]
    rfl

theorem le_succ_self_absurd : ∀ n, Le (n + 1) n → False
  | 0, h => le_zero_absurd 0 h
  | n + 1, h => le_succ_self_absurd n (le_pred (n + 1) n h)

/-- **p divides C(p, k+1) for k + 1 < p** (p = j + 2 prime). -/
theorem dv_choose (j k : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hk : Le (k + 1 + 1) (j + 1 + 1)) : Dv (j + 1 + 1) (C (j + 1 + 1) (k + 1)) := by
  have h1 : Dv (j + 1 + 1) ((k + 1) * C (j + 1 + 1) (k + 1)) :=
    ⟨C (j + 1) k, by rw [C_absorb (j + 1) k, nmul_comm]⟩
  cases euclid_core j k (C (j + 1 + 1) (k + 1)) hpr h1 with
  | inl h => exact absurd (le_trans' _ _ _ hk (dv_le (j + 1 + 1) k h)) (le_succ_self_absurd (k + 1))
  | inr h => exact h

/-- `[C n k, C n (k+1), …]`, `m` entries. -/
def listC (n : Nat) : Nat → Nat → List Nat
  | _, 0 => []
  | k, m + 1 => C n k :: listC n (k + 1) m

theorem nxt_listC (n : Nat) : ∀ (m k : Nat), C n (k + 1 + m) = 0 →
    nxt (C n k) (listC n (k + 1) m) = listC (n + 1) (k + 1) (m + 1)
  | 0, k, h => by
    show [C n k] = [C n k + C n (k + 1)]
    rw [show C n (k + 1) = 0 from h]
    exact rfl
  | m + 1, k, h => by
    show (C n (k + 1) + C n k) :: nxt (C n (k + 1)) (listC n (k + 1 + 1) m)
      = (C n k + C n (k + 1)) :: listC (n + 1) (k + 1 + 1) (m + 1)
    have h' : C n (k + 1 + 1 + m) = 0 := by
      rw [nadd_assoc (k + 1) 1 m, nadd_comm 1 m]; exact h
    rw [nxt_listC n m (k + 1) h', nadd_comm (C n (k + 1))]

theorem row_eq : ∀ n, row n = listC n 0 (n + 1)
  | 0 => rfl
  | n + 1 => by
    show nxt 0 (row n) = C (n + 1) 0 :: listC (n + 1) 1 (n + 1)
    rw [row_eq n]
    show (C n 0 + 0) :: nxt (C n 0) (listC n 1 n) = C (n + 1) 0 :: listC (n + 1) 1 (n + 1)
    have h : C n (0 + 1 + n) = 0 := by rw [nadd_comm (0 + 1) n]; exact C_above' n
    rw [nxt_listC n n 0 h, C_zero_right n, C_zero_right (n + 1)]

end Choose

/-! ## Frobenius in characteristic p -/

section Frob
open FTAChain
variable {R : Type} (K : DetChain.CRing R)

theorem len_listC (n : Nat) : ∀ k m, (listC n k m).length = m
  | _, 0 => rfl
  | k, m + 1 => congrArg Nat.succ (len_listC n (k + 1) m)

theorem nsm_one (x : R) : nsm K 1 x = x := K.zero_add x

theorem nsm_mul_nat (q : Nat) (x : R) : ∀ n, nsm K (q * n) x = nsm K n (nsm K q x)
  | 0 => rfl
  | n + 1 => by
    show nsm K (q * n + q) x = K.add (nsm K n (nsm K q x)) (nsm K q x)
    rw [nsm_add, nsm_mul_nat q x n]

/-- In characteristic `p` (p • 1 = 0) every multiple of `p` kills every element. -/
theorem nsm_char (p q : Nat) (hp : nsm K p K.one = K.zero) (y : R) : nsm K (q * p) y = K.zero := by
  rw [nsm_mul_nat, nsm_eq_mul, hp, DetChain.zero_mul']

theorem ev_tail (a b : R) (n : Nat) : ∀ m k, (∀ i y, Le (i + 1) m → nsm K (C n (k + i)) y = K.zero) →
    ev K a b (listC n k (m + 1)) = K.mul (pw K a m) (nsm K (C n (k + m)) K.one)
  | 0, k, _ => by
    show K.add (nsm K (C n k) K.one) (K.mul a K.zero) = K.mul K.one (nsm K (C n k) K.one)
    rw [DetChain.mul_zero, DetChain.add_zero', K.one_mul]
  | m + 1, k, h => by
    show K.add (nsm K (C n k) (pw K b (listC n (k + 1) (m + 1)).length)) (K.mul a (ev K a b (listC n (k + 1) (m + 1))))
      = K.mul (K.mul (pw K a m) a) (nsm K (C n (k + (m + 1))) K.one)
    have h0 : nsm K (C n k) (pw K b (listC n (k + 1) (m + 1)).length) = K.zero :=
      h 0 _ ⟨m, nadd_comm 1 m⟩
    have h' : ∀ i y, Le (i + 1) m → nsm K (C n (k + 1 + i)) y = K.zero := fun i y hi => by
      rw [nadd_assoc k 1 i, nadd_comm 1 i]
      exact h (i + 1) y (le_succ (i + 1) m hi)
    rw [h0, K.zero_add, ev_tail a b n m (k + 1) h', ← K.mul_assoc, K.mul_comm a (pw K a m),
      nadd_assoc k 1 m, nadd_comm 1 m]

/-- **Frobenius is additive in characteristic p** (p = j + 2 prime, p • 1 = 0 in the ring):
    `(a + b)^p = a^p + b^p`. This holds in every residue ring `O_F/p` of every number field. -/
theorem frob_add (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (a b : R) :
    pw K (K.add a b) (j + 1 + 1) = K.add (pw K a (j + 1 + 1)) (pw K b (j + 1 + 1)) := by
  rw [← ev_row K a b, row_eq]
  show K.add (nsm K (C (j + 1 + 1) 0) (pw K b (listC (j + 1 + 1) 1 (j + 1 + 1)).length))
      (K.mul a (ev K a b (listC (j + 1 + 1) 1 (j + 1 + 1)))) = _
  have hv : ∀ i y, Le (i + 1) (j + 1) → nsm K (C (j + 1 + 1) (1 + i)) y = K.zero := fun i y hi => by
    rw [nadd_comm 1 i]
    obtain ⟨q, hq⟩ := dv_choose j i hpr (le_succ (i + 1) (j + 1) hi)
    rw [hq]
    exact nsm_char K (j + 1 + 1) q hp y
  rw [ev_tail K a b (j + 1 + 1) (j + 1) 1 hv, len_listC, C_zero_right, nsm_one]
  have hc : C (j + 1 + 1) (1 + (j + 1)) = 1 := by rw [nadd_comm 1 (j + 1)]; exact C_self (j + 1 + 1)
  rw [hc, nsm_one, ← K.mul_assoc, K.mul_comm a (pw K a (j + 1)), K.add_comm]
  show K.add (K.mul (K.mul (pw K a (j + 1)) a) K.one) (pw K b (j + 1 + 1)) = _
  rw [K.mul_comm _ K.one, K.one_mul]
  rfl

theorem pw_mul (a b : R) : ∀ n, pw K (K.mul a b) n = K.mul (pw K a n) (pw K b n)
  | 0 => (K.one_mul K.one).symm
  | n + 1 => by
    show K.mul (pw K (K.mul a b) n) (K.mul a b) = K.mul (K.mul (pw K a n) a) (K.mul (pw K b n) b)
    rw [pw_mul a b n, K.mul_assoc, ← K.mul_assoc (pw K b n) a b, K.mul_comm (pw K b n) a,
      K.mul_assoc a (pw K b n) b, ← K.mul_assoc (pw K a n) a]

end Frob

end Frobenius

#print axioms Frobenius.shell
#print axioms Frobenius.shell_unique
#print axioms Frobenius.nsm_add
#print axioms Frobenius.mul_nsm
#print axioms Frobenius.len_nxt
#print axioms Frobenius.add4
#print axioms Frobenius.nsm_eq_mul
#print axioms Frobenius.perm4
#print axioms Frobenius.swap3'
#print axioms Frobenius.ring_step
#print axioms Frobenius.ev_nxt
#print axioms Frobenius.ev_row
#print axioms Frobenius.C_zero_right
#print axioms Frobenius.C_above
#print axioms Frobenius.C_above'
#print axioms Frobenius.C_self
#print axioms Frobenius.C_one
#print axioms Frobenius.C_absorb
#print axioms Frobenius.le_succ_self_absurd
#print axioms Frobenius.dv_choose
#print axioms Frobenius.nxt_listC
#print axioms Frobenius.row_eq
#print axioms Frobenius.len_listC
#print axioms Frobenius.nsm_one
#print axioms Frobenius.nsm_mul_nat
#print axioms Frobenius.nsm_char
#print axioms Frobenius.ev_tail
#print axioms Frobenius.frob_add
#print axioms Frobenius.pw_mul

78.15 GL\(_1\): \(x^q=x\) in every finite field

In a field whose \(n\) nonzero elements are listed once each, every \(x\) satisfies \(x^{n+1}=x\), so \(\mathrm {Frob}_{\mathfrak p}=(x\mapsto x^q)\) fixes the residue field. The steps are

\[ \begin {aligned} \prod _{u\ne 0}(xu)&=\prod _{u\ne 0}u\qquad (u\mapsto xu\ \text {permutes the units}),\\ x^n\prod _{u\ne 0}u&=\prod _{u\ne 0}u\ \Rightarrow \ x^n=1\quad (x\ne 0),\\ x^{n+1}&=x\quad (\text {also for }x=0). \end {aligned} \]

Listing 23: lang/FiniteField.lean

lang/FiniteField.lean

/-
  lang/FiniteField — T43, cycle 3 (GL1 over a general number field): x^q = x in every finite field.

  For a residue field O_F/p with q = Np elements, Frob_p = (x ↦ x^q) fixes every element; with
  Frobenius.frob_add this makes Frob_p a field automorphism of O_L/P over O_F/p.

    prodL_perm      products over two lists with the same multiplicities are equal
    cnt_map         multiplicities of a list mapped by a bijection
    unit_pow        for a unit x of a finite field whose nonzero elements are listed once each
                    (n of them): x^n = 1  (Lagrange, by permuting the product of all units)
    fermat_field    x^(n+1) = x for every x, i.e. x^q = x with q = n + 1

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain
import lang.Frobenius

namespace FiniteField

open Frobenius

variable {R : Type} [DecidableEq R] (K : DetChain.CRing R)

def prodL : List R → R
  | [] => K.one
  | a :: l => K.mul a (prodL l)

def cnt (z : R) : List R → Nat
  | [] => 0
  | a :: l => (if a = z then 1 else 0) + cnt z l

/-- Remove the first occurrence. -/
def erase1 (z : R) : List R → List R
  | [] => []
  | a :: l => if a = z then l else a :: erase1 z l

theorem ite_t {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : c) : @ite α c d a b = a := by
  cases d with
  | isTrue _ => rfl
  | isFalse hn => exact absurd h hn

theorem ite_f {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : ¬ c) : @ite α c d a b = b := by
  cases d with
  | isTrue hc => exact absurd hc h
  | isFalse _ => rfl

/-- If `z` occurs in `l`, then `prod l = z · prod (erase1 z l)`. -/
theorem prod_erase (z : R) : ∀ l : List R, 0 < cnt z l → prodL K l = K.mul z (prodL K (erase1 z l))
  | [], h => absurd h (Nat.lt_irrefl 0)
  | a :: l, h => by
    by_cases ha : a = z
    · show K.mul a (prodL K l) = K.mul z (prodL K (erase1 z (a :: l)))
      show _ = K.mul z (prodL K (if a = z then l else a :: erase1 z l))
      rw [ite_t _ _ ha, ha]
    · have h' : 0 < cnt z l := by
        have e : cnt z (a :: l) = 0 + cnt z l := by
          show (if a = z then 1 else 0) + cnt z l = 0 + cnt z l
          rw [ite_f _ _ ha]
        rw [e, Nat.zero_add] at h
        exact h
      show K.mul a (prodL K l) = K.mul z (prodL K (if a = z then l else a :: erase1 z l))
      rw [ite_f _ _ ha]
      show K.mul a (prodL K l) = K.mul z (K.mul a (prodL K (erase1 z l)))
      rw [prod_erase z l h', ← K.mul_assoc, K.mul_comm a z, K.mul_assoc]

theorem cnt_erase_same (z : R) : ∀ l : List R, 0 < cnt z l → cnt z (erase1 z l) + 1 = cnt z l
  | [], h => absurd h (Nat.lt_irrefl 0)
  | a :: l, h => by
    by_cases ha : a = z
    · show cnt z (if a = z then l else a :: erase1 z l) + 1 = (if a = z then 1 else 0) + cnt z l
      rw [ite_t _ _ ha, ite_t (1 : Nat) 0 ha, Nat.add_comm]
    · have h' : 0 < cnt z l := by
        have e : cnt z (a :: l) = 0 + cnt z l := by
          show (if a = z then 1 else 0) + cnt z l = 0 + cnt z l
          rw [ite_f _ _ ha]
        rw [e, Nat.zero_add] at h
        exact h
      show cnt z (if a = z then l else a :: erase1 z l) + 1 = (if a = z then 1 else 0) + cnt z l
      rw [ite_f _ _ ha, ite_f (1 : Nat) 0 ha]
      show (if a = z then 1 else 0) + cnt z (erase1 z l) + 1 = 0 + cnt z l
      rw [ite_f (1 : Nat) 0 ha, Nat.zero_add, Nat.zero_add, cnt_erase_same z l h']

theorem cnt_erase_other (z w : R) (hzw : w ≠ z) : ∀ l : List R, cnt w (erase1 z l) = cnt w l
  | [] => rfl
  | a :: l => by
    by_cases ha : a = z
    · show cnt w (if a = z then l else a :: erase1 z l) = (if a = w then 1 else 0) + cnt w l
      rw [ite_t _ _ ha]
      have : ¬ a = w := fun h => hzw (h ▸ ha)
      rw [ite_f (1 : Nat) 0 this, Nat.zero_add]
    · show cnt w (if a = z then l else a :: erase1 z l) = (if a = w then 1 else 0) + cnt w l
      rw [ite_f _ _ ha]
      show (if a = w then 1 else 0) + cnt w (erase1 z l) = _
      rw [cnt_erase_other z w hzw l]

/-- **Products are invariant under permutation** (equal multiplicities). -/
theorem prodL_perm : ∀ (l l' : List R), (∀ z, cnt z l = cnt z l') → prodL K l = prodL K l'
  | [], l', h => by
    cases l' with
    | nil => rfl
    | cons b l' =>
      have := h b
      have e : cnt b (b :: l') = 1 + cnt b l' := by
        show (if b = b then 1 else 0) + cnt b l' = 1 + cnt b l'
        rw [ite_t (1 : Nat) 0 rfl]
      rw [e] at this
      exact absurd this (fun h0 => Nat.noConfusion (h0.trans (Nat.add_comm 1 _)))
  | a :: l, l', h => by
    have hpos : 0 < cnt a l' := by
      rw [← h a]
      show 0 < (if a = a then 1 else 0) + cnt a l
      rw [ite_t (1 : Nat) 0 rfl, Nat.add_comm]
      exact Nat.succ_pos _
    rw [prod_erase K a l' hpos]
    show K.mul a (prodL K l) = _
    have hrest : ∀ z, cnt z l = cnt z (erase1 a l') := fun z => by
      by_cases hz : z = a
      · rw [hz]
        have e1 := cnt_erase_same a l' hpos
        have e2 : cnt a (a :: l) = 1 + cnt a l := by
          show (if a = a then 1 else 0) + cnt a l = 1 + cnt a l
          rw [ite_t (1 : Nat) 0 rfl]
        rw [← h a, e2, Nat.add_comm] at e1
        have e3 : cnt a (erase1 a l') + 1 = cnt a l + 1 := by
          rw [Nat.add_comm (cnt a (erase1 a l')) 1, Nat.add_comm (cnt a l) 1]; exact e1
        exact (Nat.succ.inj e3).symm
      · rw [cnt_erase_other a z hz l']
        have e2 : cnt z (a :: l) = 0 + cnt z l := by
          show (if a = z then 1 else 0) + cnt z l = 0 + cnt z l
          rw [ite_f (1 : Nat) 0 (fun e => hz e.symm)]
        rw [← h z, e2, Nat.zero_add]
    rw [prodL_perm l (erase1 a l') hrest]

/-- Multiplicities of a mapped list, for a bijection `f` with inverse `g`. -/
theorem cnt_map (f g : R → R) (hgf : ∀ x, g (f x) = x) (hfg : ∀ y, f (g y) = y) (z : R) :
    ∀ l : List R, cnt z (l.map f) = cnt (g z) l
  | [] => rfl
  | a :: l => by
    show (if f a = z then 1 else 0) + cnt z (l.map f) = (if a = g z then 1 else 0) + cnt (g z) l
    rw [cnt_map f g hgf hfg z l]
    by_cases ha : a = g z
    · rw [ite_t (1 : Nat) 0 ha, ite_t (1 : Nat) 0 (by rw [ha, hfg])]
    · rw [ite_f (1 : Nat) 0 ha, ite_f (1 : Nat) 0 (fun e => ha (by rw [← e, hgf]))]

omit [DecidableEq R] in
theorem prod_map_mul (x : R) : ∀ l : List R, prodL K (l.map (K.mul x)) = K.mul (pw K x l.length) (prodL K l)
  | [] => (K.one_mul K.one).symm
  | a :: l => by
    show K.mul (K.mul x a) (prodL K (l.map (K.mul x))) = K.mul (K.mul (pw K x l.length) x) (K.mul a (prodL K l))
    rw [prod_map_mul x l, K.mul_assoc, ← K.mul_assoc a, K.mul_comm a (pw K x l.length), K.mul_assoc,
      ← K.mul_assoc x, K.mul_comm x (pw K x l.length), K.mul_assoc]

/-- **Lagrange for the units of a finite field.** `us` lists every nonzero element exactly once;
    `inv` is a two-sided inverse on nonzero elements; the ring has no zero divisors. -/
theorem unit_pow (hK : DetChain.NoZD K) (inv : R → R) (hinv : ∀ x, x ≠ K.zero → K.mul (inv x) x = K.one)
    (us : List R) (hus : ∀ z, cnt z us = if z = K.zero then 0 else 1)
    (hnz : ∀ z, 0 < cnt z us → z ≠ K.zero) (hone : K.one ≠ K.zero)
    (x : R) (hx : x ≠ K.zero) : pw K x us.length = K.one := by
  have hxinv : ∀ y, K.mul x (K.mul (inv x) y) = y := fun y => by
    rw [← K.mul_assoc, K.mul_comm x (inv x), hinv x hx, K.one_mul]
  have hinvx : ∀ y, K.mul (inv x) (K.mul x y) = y := fun y => by
    rw [← K.mul_assoc, hinv x hx, K.one_mul]
  -- x·u ranges over the units as u does
  have hperm : ∀ z, cnt z (us.map (K.mul x)) = cnt z us := fun z => by
    rw [cnt_map (K.mul x) (K.mul (inv x)) hinvx hxinv z us, hus, hus]
    by_cases hz : z = K.zero
    · rw [ite_t (0 : Nat) 1 hz]
      have : K.mul (inv x) z = K.zero := by rw [hz]; exact DetChain.mul_zero K _
      rw [ite_t (0 : Nat) 1 this]
    · rw [ite_f (0 : Nat) 1 hz]
      have : ¬ K.mul (inv x) z = K.zero := fun h0 => by
        apply hz
        have := congrArg (K.mul x) h0
        rw [hxinv, DetChain.mul_zero] at this
        exact this
      rw [ite_f (0 : Nat) 1 this]
  have e := prodL_perm K _ _ hperm
  rw [prod_map_mul] at e
  -- P = prod us ≠ 0
  have hP : ∀ l : List R, (∀ z, 0 < cnt z l → z ≠ K.zero) → prodL K l ≠ K.zero := by
    intro l
    induction l with
    | nil => intro _; exact hone
    | cons a l ih =>
      intro hl h0
      have ha : a ≠ K.zero := hl a (by
        show 0 < (if a = a then 1 else 0) + cnt a l
        rw [ite_t (1 : Nat) 0 rfl, Nat.add_comm]; exact Nat.succ_pos _)
      have hl' : ∀ z, 0 < cnt z l → z ≠ K.zero := fun z hz => hl z (by
        show 0 < (if a = z then 1 else 0) + cnt z l
        exact Nat.lt_of_lt_of_le hz (Nat.le_add_left _ _))
      cases hK a (prodL K l) h0 with
      | inl h => exact ha h
      | inr h => exact ih hl' h
  -- x^n P = P, so (x^n - 1) P = 0
  have e2 : K.mul (K.add (pw K x us.length) (K.neg K.one)) (prodL K us) = K.zero := by
    rw [DetChain.right_distrib, e, DetChain.neg_mul_left, K.one_mul, K.add_neg]
  cases hK _ _ e2 with
  | inl h =>
    have this : K.add (K.add (pw K x us.length) (K.neg K.one)) K.one = K.add K.zero K.one :=
      congrArg (fun t => K.add t K.one) h
    show pw K x us.length = K.one
    rw [K.add_assoc, DetChain.add_neg' K K.one, DetChain.add_zero', K.zero_add] at this
    exact this
  | inr h => exact absurd h (hP us hnz)

/-- **x^q = x in a finite field** (q = n + 1 elements, `us` the n nonzero ones): the Frobenius
    x ↦ x^q of the residue field O_F/p (q = Np) is the identity. -/
theorem fermat_field (hK : DetChain.NoZD K) (inv : R → R) (hinv : ∀ x, x ≠ K.zero → K.mul (inv x) x = K.one)
    (us : List R) (hus : ∀ z, cnt z us = if z = K.zero then 0 else 1)
    (hnz : ∀ z, 0 < cnt z us → z ≠ K.zero) (hone : K.one ≠ K.zero) (x : R) :
    pw K x (us.length + 1) = x := by
  by_cases hx : x = K.zero
  · rw [hx]; exact DetChain.mul_zero K _
  · show K.mul (pw K x us.length) x = x
    rw [unit_pow K hK inv hinv us hus hnz hone x hx, K.one_mul]

end FiniteField

#print axioms FiniteField.ite_t
#print axioms FiniteField.ite_f
#print axioms FiniteField.prod_erase
#print axioms FiniteField.cnt_erase_same
#print axioms FiniteField.cnt_erase_other
#print axioms FiniteField.prodL_perm
#print axioms FiniteField.cnt_map
#print axioms FiniteField.prod_map_mul
#print axioms FiniteField.unit_pow
#print axioms FiniteField.fermat_field

78.16 GL\(_1\) for every quadratic field

For odd primes \(p,p'\nmid a\),

\[ \Bigl (\frac {-1}{p}\Bigr )=1\iff p\equiv 1\ (4),\qquad \Bigl (\frac {-3}{p}\Bigr )=1\iff p\equiv 1\ (3),\qquad \Bigl (\frac {2}{p}\Bigr )=1\iff p\equiv \pm 1\ (8),\qquad p'\equiv p\ (4a)\Rightarrow \Bigl (\frac {a}{p'}\Bigr )=\Bigl (\frac {a}{p}\Bigr ). \]
So the Frobenius of \(p\) in \(\mathbb Q(\sqrt {\pm a})/\mathbb Q\) is a Dirichlet character. The steps are
\[ \begin {aligned} p=4k+3,\ y^2=-1&\Rightarrow y^{p-1}=(y^2)^{2k+1}=-1\ne 1,\\ p=4k+1&\Rightarrow (-1)^{(p-1)/2}=1\Rightarrow -1\ \text {is a square (Euler)},\\ z^2=-3&\Rightarrow y=\tfrac {z-1}{2},\ y^2+y+1=0,\ y^3=1,\ y\ne 1\Rightarrow 3\mid p-1,\\ \Bigl (\frac 2p\Bigr )&=(-1)^{\mu (h,2)},\quad \mu (h,2)=n+r\quad (h=2n+r,\ \text {Gauss's lemma}),\\ \Bigl (\frac {qa'}{p}\Bigr )&=\Bigl (\frac {q}{p}\Bigr )\Bigl (\frac {a'}{p}\Bigr ),\ \text {each factor periodic mod }4q,\ 4a'. \end {aligned} \]

Listing 24: lang/GaussSplit.lean

lang/GaussSplit.lean

/-
  lang/GaussSplit — T43, cycle 23 (GL1 for ℚ(i)): the splitting law of ℤ[i].

  For every odd prime p, −1 is a square in ℤ/p exactly when p ≡ 1 (mod 4):
    two_ne_zero          1 + 1 ≠ 0 in ℤ/p (p odd)
    one_ne_negone        1 ≠ −1 in ℤ/p
    split_one_mod_four   p = 4k + 5  ⇒  ∃ y, y² = −1          (Euler's criterion, EulerHard.euler_hard)
    inert_three_mod_four p = 4k + 3  ⇒  ∀ y, y² ≠ −1          (y^(p−1) = (y²)^(2k+1) = −1 ≠ 1, ZMod.unit_pow)
  So the Artin symbol of p in ℚ(i)/ℚ (split ⇔ x² + 1 has a root mod p) is the Dirichlet character χ₋₄(p) of
  conductor 4: GL1 reciprocity for ℚ(i), the Hecke-character input of the CM form of y² = x³ − x (lang/CMInert).
  The case p = 4k + 1 is written p = 4k + 5 together with p = 5 at k = 0.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EulerHard
import lang.TwoPower

namespace GaussSplit

open FTAChain ZMod

/-- `1 + 1 ≠ 0` in ℤ/(j + 2), j ≥ 1. -/
theorem two_ne_zero (j : Nat) :
    (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero := fun h => by
  have hv := congrArg Subtype.val h
  have h2 : ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one (ring (j + 1 + 1)).one).val = 2 := by
    show md (j + 1 + 1) (md (j + 1 + 1) 1 + md (j + 1 + 1) 1) = 2
    rw [md_small _ 1 (Nat.succ_lt_succ (Nat.zero_lt_succ _))]
    exact md_small _ 2 (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ _)))
  rw [h2] at hv
  exact Nat.noConfusion hv

theorem one_ne_negone (j : Nat) : (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one := fun h => by
  apply two_ne_zero j
  have := congrArg (fun z => (ring (j + 1 + 1)).add z (ring (j + 1 + 1)).one) h
  exact this.trans (DetChain.add_neg' (ring (j + 1 + 1)) _)

/-- **Split primes**: p = 4k + 5 ⇒ −1 is a square mod p. -/
theorem split_one_mod_four (k : Nat)
    (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1) :
    ∃ y : Zp (2 * (2 * k + 1) + 1 + 1),
      (ring (2 * (2 * k + 1) + 1 + 1)).mul y y = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one := by
  apply EulerHard.euler_hard (2 * k + 1) hpr
  show Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) _ (2 * (k + 1)) = _
  rw [← TwoPower.pwpow]
  have e2 : Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) 2
      = (ring (2 * (2 * k + 1) + 1 + 1)).one := by
    show (ring _).mul ((ring _).mul (ring _).one _) _ = _
    rw [(ring _).one_mul]
    exact TwoPower.negone_sq' (2 * (2 * k + 1) + 1)
  rw [e2, TwoPower.pwone]

/-- **Inert primes**: p = 4k + 3 ⇒ −1 is not a square mod p. -/
theorem inert_three_mod_four (k : Nat)
    (hpr : ∀ d, Dv d (2 * (2 * k) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k) + 1 + 1 + 1)
    (y : Zp (2 * (2 * k) + 1 + 1)) :
    (ring (2 * (2 * k) + 1 + 1)).mul y y ≠ (ring (2 * (2 * k) + 1 + 1)).neg (ring (2 * (2 * k) + 1 + 1)).one := fun h => by
  have hy : y ≠ (ring (2 * (2 * k) + 1 + 1)).zero := fun e => by
    rw [e, DetChain.mul_zero] at h
    have := congrArg (fun z => (ring (2 * (2 * k) + 1 + 1)).add z (ring (2 * (2 * k) + 1 + 1)).one) h
    have h1 : (ring (2 * (2 * k) + 1 + 1)).one = (ring (2 * (2 * k) + 1 + 1)).zero :=
      ((ring _).zero_add _).symm.trans (this.trans (DetChain.add_neg' (ring _) _))
    exact one_ne_negone (2 * (2 * k)) (h1.trans h)
  have hu := unit_pow (2 * (2 * k) + 1) hpr y hy
  have e : Frobenius.pw (ring (2 * (2 * k) + 1 + 1)) y (2 * (2 * k) + 1 + 1)
      = Frobenius.pw (ring (2 * (2 * k) + 1 + 1)) (Frobenius.pw (ring (2 * (2 * k) + 1 + 1)) y 2) (2 * k + 1) :=
    (TwoPower.pwpow _ y 2 (2 * k + 1)).symm
  have e2 : Frobenius.pw (ring (2 * (2 * k) + 1 + 1)) y 2 = (ring (2 * (2 * k) + 1 + 1)).mul y y := by
    show (ring _).mul ((ring _).mul (ring _).one y) y = _
    rw [(ring _).one_mul]
  rw [e, e2, h, TwoPower.negone_odd' (2 * (2 * k) + 1) k] at hu
  exact one_ne_negone (2 * (2 * k)) hu.symm

end GaussSplit

#print axioms GaussSplit.two_ne_zero
#print axioms GaussSplit.one_ne_negone
#print axioms GaussSplit.split_one_mod_four
#print axioms GaussSplit.inert_three_mod_four

Listing 25: lang/Eisen.lean

lang/Eisen.lean

/-
  lang/Eisen — T43, cycle 37 (GL1 for ℚ(√−3) = ℚ(ζ₃)): −3 is a square mod p ⇔ p ≡ 1 (mod 3).

  p = J + 2 prime, p > 3, ℤ/p = ring (J + 1), 3 = 1 + 1 + 1.
    cube_factor     y³ − 1 = (y − 1)(y² + y + 1)
    split_neg3      3 | p − 1 ⇒ ∃ z, z² = −3          (ζ₃ ∈ 𝔽_p by CycloSplit, z = 2ζ₃ + 1)
    inert_neg3      z² = −3 ⇒ 3 | p − 1               (y = (z − 1)/2 has y² + y + 1 = 0, so order 3)
    neg3_iff        the equivalence
  The splitting of p in ℤ[ω] (Eisenstein integers) is the Dirichlet character mod 3 — the Hecke-character input of
  the CM forms of discriminant −3 (y² = x³ + 1, y² = x³ − 432 …), as GaussSplit was for ℤ[i].

  Lean 4 core only, proofs by hand (lib_ring from lib_shared for ring identities), no axioms.
-/
import lang.CycloSplit
import lib.RingTac

namespace Eisen

open FTAChain ZMod

variable (J : Nat)

def three : Zp (J + 1) := (ring (J + 1)).add (ring (J + 1)).one ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one)

theorem cube_factor (y : Zp (J + 1)) :
    (ring (J + 1)).mul ((ring (J + 1)).add y ((ring (J + 1)).neg (ring (J + 1)).one)) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul y y) y) (ring (J + 1)).one)
      = (ring (J + 1)).add ((ring (J + 1)).mul ((ring (J + 1)).mul y y) y) ((ring (J + 1)).neg (ring (J + 1)).one) := by lib_ring

theorem pw3 (y : Zp (J + 1)) : Frobenius.pw (ring (J + 1)) y 3 = (ring (J + 1)).mul ((ring (J + 1)).mul y y) y := by
  show (ring (J + 1)).mul ((ring (J + 1)).mul ((ring (J + 1)).mul (ring (J + 1)).one y) y) y = _
  rw [(ring (J + 1)).one_mul]

theorem eq_of_sub_zero (a b : Zp (J + 1)) (h : (ring (J + 1)).add a ((ring (J + 1)).neg b) = (ring (J + 1)).zero) : a = b := by
  have := congrArg (fun z => (ring (J + 1)).add z b) h
  have e : (ring (J + 1)).add ((ring (J + 1)).add a ((ring (J + 1)).neg b)) b = a := by
    rw [(ring (J + 1)).add_assoc, DetChain.add_neg', DetChain.add_zero']
  rw [← e]
  exact this.trans ((ring (J + 1)).zero_add b)

theorem lt3 (h3 : 2 ≤ J) (a : Nat) (ha : a ≤ 3) : a < J + 1 + 1 :=
  Nat.lt_succ_of_le (Nat.le_trans ha (Nat.succ_le_succ h3))

theorem three_val (h3 : 2 ≤ J) : (three J).val = 3 := by
  show md (J + 1) (md (J + 1) 1 + md (J + 1) (md (J + 1) 1 + md (J + 1) 1)) = 3
  have h1 : md (J + 1) 1 = 1 := md_small _ 1 (lt3 J h3 1 (Nat.le_succ_of_le (Nat.le_succ 1)))
  have h2 : md (J + 1) 2 = 2 := md_small _ 2 (lt3 J h3 2 (Nat.le_succ 2))
  rw [h1, h2]
  exact md_small _ 3 (lt3 J h3 3 (Nat.le_refl 3))

theorem three_ne_zero (h3 : 2 ≤ J) : three J ≠ (ring (J + 1)).zero := fun e => by
  have := congrArg Subtype.val e
  rw [three_val J h3] at this
  exact Nat.noConfusion this

theorem two_ne_zero' (h3 : 2 ≤ J) : (ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one ≠ (ring (J + 1)).zero := fun e => by
  have := congrArg Subtype.val e
  have h1 : md (J + 1) 1 = 1 := md_small _ 1 (lt3 J h3 1 (Nat.le_succ_of_le (Nat.le_succ 1)))
  have v : ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one).val = 2 := by
    show md (J + 1) (md (J + 1) 1 + md (J + 1) 1) = 2
    rw [h1]
    exact md_small _ 2 (lt3 J h3 2 (Nat.le_succ 2))
  rw [v] at this
  exact Nat.noConfusion this

theorem prime3 : ∀ d, Dv d (1 + 2) → d = 1 ∨ d = 1 + 2
  | 0, ⟨q, e⟩ => absurd (e.trans (Nat.mul_zero q)) (fun e' => Nat.noConfusion e')
  | 1, _ => Or.inl rfl
  | 2, ⟨q, e⟩ => by
    exfalso
    cases q with
    | zero => exact Nat.noConfusion e
    | succ q =>
      cases q with
      | zero => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj e))
      | succ q =>
        have a : 2 * 2 ≤ (q + 1 + 1) * 2 := Nat.mul_le_mul_right 2 (Nat.le_add_left 2 q)
        exact Nat.not_succ_le_self 3 (Nat.le_trans a (Nat.le_of_eq e.symm))
  | 3, _ => Or.inr rfl
  | d + 4, ⟨q, e⟩ => by
    exfalso
    cases q with
    | zero => exact Nat.noConfusion (e.trans (Nat.zero_mul _))
    | succ q =>
      have a : d + 4 ≤ (q + 1) * (d + 4) := by rw [Nat.succ_mul]; exact Nat.le_add_left _ _
      have this : d + 4 ≤ 3 := Nat.le_trans a (Nat.le_of_eq e.symm)
      exact Nat.not_succ_le_self 3 (Nat.le_trans (Nat.succ_le_succ (Nat.succ_le_succ (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le d))))) this)

/-- **Split**: 3 | p − 1 ⇒ −3 is a square. -/
theorem split_neg3 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hd : Dv (1 + 2) (J + 1)) :
    ∃ z : Zp (J + 1), (ring (J + 1)).mul z z = (ring (J + 1)).neg (three J) := by
  match CycloSplit.split_of_dvd J hpr 1 hd with
  | ⟨y, hy1, hy3⟩ =>
    have hc := cube_factor J y
    rw [← pw3, hy3, (ring (J + 1)).add_neg] at hc
    cases nozd J hpr _ _ hc with
    | inl h => exact absurd (eq_of_sub_zero J y (ring (J + 1)).one h) hy1
    | inr h =>
      refine ⟨(ring (J + 1)).add ((ring (J + 1)).add y y) (ring (J + 1)).one, ?_⟩
      apply DetChain.neg_unique
      have e : (ring (J + 1)).add ((ring (J + 1)).mul ((ring (J + 1)).add ((ring (J + 1)).add y y) (ring (J + 1)).one) ((ring (J + 1)).add ((ring (J + 1)).add y y) (ring (J + 1)).one)) (three J)
          = (ring (J + 1)).mul ((ring (J + 1)).add ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one)) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul y y) y) (ring (J + 1)).one) := by
        unfold three; lib_ring
      rw [e, h, DetChain.mul_zero]

/-- **Inert direction**: −3 a square ⇒ 3 | p − 1. -/
theorem inert_neg3 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (h3 : 2 ≤ J) (z : Zp (J + 1))
    (hz : (ring (J + 1)).mul z z = (ring (J + 1)).neg (three J)) : Dv (1 + 2) (J + 1) := by
  have h2 := two_ne_zero' J h3
  let w := Frobenius.pw (ring (J + 1)) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) J
  have hw : (ring (J + 1)).mul w ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) = (ring (J + 1)).one := inv_exists J hpr _ h2
  let u := (ring (J + 1)).add z ((ring (J + 1)).neg (ring (J + 1)).one)
  let y := (ring (J + 1)).mul u w
  -- u² + 2u + 4 = z² + 3 = 0
  have hu : (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul u u) ((ring (J + 1)).mul ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) u)) ((ring (J + 1)).add ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one))
      = (ring (J + 1)).zero := by
    have e : (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul u u) ((ring (J + 1)).mul ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) u)) ((ring (J + 1)).add ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one))
        = (ring (J + 1)).add ((ring (J + 1)).mul z z) (three J) := by
      show (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul ((ring (J + 1)).add z ((ring (J + 1)).neg (ring (J + 1)).one)) ((ring (J + 1)).add z ((ring (J + 1)).neg (ring (J + 1)).one)))
        ((ring (J + 1)).mul ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) ((ring (J + 1)).add z ((ring (J + 1)).neg (ring (J + 1)).one)))) ((ring (J + 1)).add ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one)) = _
      unfold three; lib_ring
    rw [e, hz, DetChain.add_neg']
  -- y² + y + 1 = w² (u² + 2u + 4)
  have hy0 : (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul y y) y) (ring (J + 1)).one = (ring (J + 1)).zero := by
    have e : (ring (J + 1)).mul ((ring (J + 1)).mul w w) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul u u) ((ring (J + 1)).mul ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) u))
          ((ring (J + 1)).add ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one)))
        = (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul ((ring (J + 1)).mul u w) ((ring (J + 1)).mul u w)) ((ring (J + 1)).mul ((ring (J + 1)).mul u w) ((ring (J + 1)).mul w ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one))))
            ((ring (J + 1)).mul ((ring (J + 1)).mul w ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one)) ((ring (J + 1)).mul w ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one))) := by lib_ring
    rw [hu, DetChain.mul_zero, hw, (ring (J + 1)).mul_comm ((ring (J + 1)).mul u w) (ring (J + 1)).one, (ring (J + 1)).one_mul, (ring (J + 1)).one_mul] at e
    exact e.symm
  have hy1 : y ≠ (ring (J + 1)).one := fun e => by
    rw [e] at hy0
    apply three_ne_zero J h3
    have : (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).one (ring (J + 1)).one) (ring (J + 1)).one) (ring (J + 1)).one = three J := by unfold three; lib_ring
    rw [← this]; exact hy0
  have hy3 : Frobenius.pw (ring (J + 1)) y (1 + 2) = (ring (J + 1)).one := by
    rw [pw3]
    apply eq_of_sub_zero
    rw [← cube_factor, hy0, DetChain.mul_zero]
  exact CycloSplit.dvd_of_split J hpr 1 prime3 y hy1 hy3

/-- **−3 is a square mod p ⇔ p ≡ 1 (mod 3)** (p > 3). -/
theorem neg3_iff (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (h3 : 2 ≤ J) :
    (∃ z : Zp (J + 1), (ring (J + 1)).mul z z = (ring (J + 1)).neg (three J)) ↔ Dv (1 + 2) (J + 1) :=
  ⟨fun ⟨z, hz⟩ => inert_neg3 J hpr h3 z hz, split_neg3 J hpr⟩

end Eisen

#print axioms Eisen.cube_factor
#print axioms Eisen.pw3
#print axioms Eisen.eq_of_sub_zero
#print axioms Eisen.lt3
#print axioms Eisen.three_val
#print axioms Eisen.three_ne_zero
#print axioms Eisen.two_ne_zero'
#print axioms Eisen.prime3
#print axioms Eisen.split_neg3
#print axioms Eisen.inert_neg3
#print axioms Eisen.neg3_iff

Listing 26: lang/TwoSupp.lean

lang/TwoSupp.lean

/-
  lang/TwoSupp — T43, cycle 31 (GL1 for ℚ(√2)): 2 is a square mod p ⇔ p ≡ ±1 (mod 8).

  p = h + h + 1 prime. By Gauss's lemma (QRChain.gauss_lemma) 2 is a square iff μ(h, 2) is even, where μ counts
  k ∈ [1, h] with 2k > h. Writing h = n + (n + r), r ∈ {0, 1}:
    lk_two         for k ≤ h:  lk h 2 k = [2k > h]
    sum_low, sum_high, mu_two    μ(h, 2) = n + r
    two_sq_iff     2 is a square mod p ⇔ n + r is even
    two_8m1, two_8m3, two_8m5, two_8m7   p = 8m+1, 8m+7: square;  p = 8m+3, 8m+5: not a square
  So the Frobenius of p in ℚ(√2)/ℚ is the Dirichlet character χ₈ of conductor 8 — GL1 reciprocity for ℚ(√2),
  the real subfield of ℚ(ζ₈) (CycloPow at ℓ^e = 8).

  Lean 4 core only, proofs by hand, no axioms.
-/
import QRChain

namespace TwoSupp

open FTAChain QRChain

theorem lk_two (h k : Nat) (hk : k ≤ h) : lk h 2 k = (if 2 * k ≤ h then 0 else 1) := by
  have hr : rk h 2 k = 2 * k := by
    show rem (h + h + 1) (2 * k) = 2 * k
    apply rem_of_lt (h + h) (2 * k)
    rw [Nat.two_mul]
    exact Nat.lt_succ_of_le (Nat.add_le_add hk hk)
  show (if rk h 2 k ≤ h then 0 else 1) = _
  rw [hr]

theorem sum_low (h : Nat) : ∀ j, 2 * j ≤ h → sum1 j (lk h 2) = 0
  | 0, _ => rfl
  | j + 1, hj => by
    show sum1 j (lk h 2) + lk h 2 (j + 1) = 0
    have hj' : 2 * j ≤ h := Nat.le_trans (Nat.mul_le_mul_left 2 (Nat.le_succ j)) hj
    have hk : j + 1 ≤ h := Nat.le_trans (by rw [Nat.two_mul]; exact Nat.le_add_left (j + 1) (j + 1)) hj
    rw [sum_low h j hj', lk_two h (j + 1) hk, ite_pos' _ _ hj]
where
  ite_pos' {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : c) : @ite α c d a b = a := by
    cases d with
    | isTrue _ => rfl
    | isFalse hn => exact absurd h hn

theorem ite_neg'' {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : ¬ c) : @ite α c d a b = b := by
  cases d with
  | isTrue hc => exact absurd hc h
  | isFalse _ => rfl

theorem sum_high (h n : Nat) (h0 : sum1 n (lk h 2) = 0) (hn : h < 2 * (n + 1)) :
    ∀ i, n + i ≤ h → sum1 (n + i) (lk h 2) = i
  | 0, _ => h0
  | i + 1, hi => by
    show sum1 (n + i) (lk h 2) + lk h 2 (n + i + 1) = i + 1
    rw [sum_high h n h0 hn i (Nat.le_of_succ_le hi), lk_two h (n + i + 1) hi]
    have hlt : ¬ 2 * (n + i + 1) ≤ h := fun hle =>
      Nat.lt_irrefl h (Nat.lt_of_lt_of_le hn (Nat.le_trans (Nat.mul_le_mul_left 2
        (Nat.succ_le_succ (Nat.le_add_right n i))) hle))
    rw [ite_neg'' _ _ hlt]

/-- `μ(n + (n + r), 2) = n + r` for `r ≤ 1`. -/
theorem mu_two (n r : Nat) (hr : r ≤ 1) : mu (n + (n + r)) 2 = n + r := by
  have h2n : 2 * n ≤ n + (n + r) := by rw [Nat.two_mul]; exact Nat.add_le_add_left (Nat.le_add_right n r) n
  have h0 := sum_low (n + (n + r)) n h2n
  have hn : n + (n + r) < 2 * (n + 1) := by
    show n + (n + r) < 2 * n + 2
    rw [Nat.two_mul, Nat.add_assoc]
    exact Nat.add_lt_add_left (Nat.lt_succ_of_le (Nat.add_le_add_left hr n)) n
  exact sum_high (n + (n + r)) n h0 hn (n + r) (Nat.le_refl _)

theorem not_dv_two (h : Nat) (h1 : 1 ≤ h) : ¬ Dv (h + h + 1) 2 := fun ⟨q, e⟩ => by
  cases q with
  | zero => exact Nat.noConfusion (e.trans (Nat.zero_mul _))
  | succ q =>
    have a : h + h + 1 ≤ 2 := by rw [e, Nat.succ_mul]; exact Nat.le_add_left _ _
    have b : 1 + 1 + 1 ≤ h + h + 1 := Nat.succ_le_succ (Nat.add_le_add h1 h1)
    exact Nat.not_succ_le_self 2 (Nat.le_trans b a)

/-- **2 is a square mod p ⇔ n + r is even** (p = 2(n + (n + r)) + 1). -/
theorem two_sq_iff (n r : Nat) (hr : r ≤ 1) (h1 : 1 ≤ n + (n + r)) (hp : Pr (n + (n + r) + (n + (n + r)) + 1)) :
    (∃ x, rem (n + (n + r) + (n + (n + r)) + 1) (x * x) = rem (n + (n + r) + (n + (n + r)) + 1) 2)
      ↔ par (n + r) = false := by
  have g := gauss_lemma (n + (n + r)) 2 hp (not_dv_two _ h1)
  have e := mu_two n r hr
  exact ⟨fun hx => e ▸ g.mp hx, fun hpar => g.mpr (e.symm ▸ hpar)⟩

theorem par_mm (m : Nat) : par (m + m) = false := par_double m

theorem par_mm1 (m : Nat) : par (m + m + 1) = true := by
  show (!par (m + m)) = true
  rw [par_double]
  rfl

/-- p = 8m + 9 (≡ 1 mod 8): 2 is a square. -/
theorem two_8m1 (m : Nat) (hp : Pr ((m + 1 + (m + 1)) + ((m + 1 + (m + 1)) + 0) + ((m + 1 + (m + 1)) + ((m + 1 + (m + 1)) + 0)) + 1)) :
    ∃ x, rem ((m + 1 + (m + 1)) + ((m + 1 + (m + 1)) + 0) + ((m + 1 + (m + 1)) + ((m + 1 + (m + 1)) + 0)) + 1) (x * x)
      = rem ((m + 1 + (m + 1)) + ((m + 1 + (m + 1)) + 0) + ((m + 1 + (m + 1)) + ((m + 1 + (m + 1)) + 0)) + 1) 2 :=
  (two_sq_iff (m + 1 + (m + 1)) 0 (Nat.zero_le 1) (Nat.le_add_right_of_le (Nat.le_add_right_of_le (Nat.succ_le_succ (Nat.zero_le m)))) hp).mpr
    (par_mm (m + 1))

/-- p = 8m + 3: 2 is not a square. -/
theorem two_8m3 (m : Nat) (hp : Pr ((m + m) + ((m + m) + 1) + ((m + m) + ((m + m) + 1)) + 1)) :
    ¬ ∃ x, rem ((m + m) + ((m + m) + 1) + ((m + m) + ((m + m) + 1)) + 1) (x * x)
      = rem ((m + m) + ((m + m) + 1) + ((m + m) + ((m + m) + 1)) + 1) 2 := fun hx => by
  have := (two_sq_iff (m + m) 1 (Nat.le_refl 1) (Nat.le_add_left 1 _) hp).mp hx
  rw [par_mm1] at this
  exact Bool.noConfusion this

/-- p = 8m + 5: 2 is not a square. -/
theorem two_8m5 (m : Nat) (hp : Pr ((m + m + 1) + ((m + m + 1) + 0) + ((m + m + 1) + ((m + m + 1) + 0)) + 1)) :
    ¬ ∃ x, rem ((m + m + 1) + ((m + m + 1) + 0) + ((m + m + 1) + ((m + m + 1) + 0)) + 1) (x * x)
      = rem ((m + m + 1) + ((m + m + 1) + 0) + ((m + m + 1) + ((m + m + 1) + 0)) + 1) 2 := fun hx => by
  have := (two_sq_iff (m + m + 1) 0 (Nat.zero_le 1) (Nat.le_add_right_of_le (Nat.le_add_left 1 _)) hp).mp hx
  rw [show m + m + 1 + 0 = m + m + 1 from rfl, par_mm1] at this
  exact Bool.noConfusion this

/-- p = 8m + 7: 2 is a square. -/
theorem two_8m7 (m : Nat) (hp : Pr ((m + m + 1) + ((m + m + 1) + 1) + ((m + m + 1) + ((m + m + 1) + 1)) + 1)) :
    ∃ x, rem ((m + m + 1) + ((m + m + 1) + 1) + ((m + m + 1) + ((m + m + 1) + 1)) + 1) (x * x)
      = rem ((m + m + 1) + ((m + m + 1) + 1) + ((m + m + 1) + ((m + m + 1) + 1)) + 1) 2 :=
  (two_sq_iff (m + m + 1) 1 (Nat.le_refl 1) (Nat.le_add_right_of_le (Nat.le_add_left 1 _)) hp).mpr (by
    show (!(!par (m + m))) = false
    rw [par_mm]
    rfl)

end TwoSupp

#print axioms TwoSupp.lk_two
#print axioms TwoSupp.sum_low
#print axioms TwoSupp.ite_neg''
#print axioms TwoSupp.sum_high
#print axioms TwoSupp.mu_two
#print axioms TwoSupp.not_dv_two
#print axioms TwoSupp.two_sq_iff
#print axioms TwoSupp.par_mm
#print axioms TwoSupp.par_mm1
#print axioms TwoSupp.two_8m1
#print axioms TwoSupp.two_8m3
#print axioms TwoSupp.two_8m5
#print axioms TwoSupp.two_8m7

Listing 27: lang/QuadAll.lean

lang/QuadAll.lean

/-
  lang/QuadAll — T43, cycle 36: Artin reciprocity for every quadratic field ℚ(√a), a ≥ 1.

  For odd primes p, p' not dividing a with p' ≡ p (mod 4a):  (a/p') = (a/p).
    leg_one        (1/p) = 1
    two_period     (2/p) depends only on p mod 8           (TwoSupp.two_sq_iff)
    odd_prime_form a prime q ≠ 2 is g + g + 1
    leg_period     (a/p) depends only on p mod 4a          (strong induction on a: least prime factor q, a = q a',
                   LegMul.legB_mul, QuadPeriod.legB_mod_4q for odd q, two_period for q = 2)
  So p ↦ (a/p) is a Dirichlet character mod 4a: the Frobenius of p in ℚ(√a)/ℚ depends only on p mod 4a — GL1
  reciprocity for all real quadratic fields (with GaussSplit for −1, all quadratic fields ℚ(√±a)).

  Lean 4 core only, proofs by hand (lib/Semi's lib_nat for Nat identities), no axioms.
-/
import lang.LegMul
import lang.TwoSupp
import lib.Semi

namespace QuadAll

open FTAChain QRChain

theorem bool_eq {a b : Bool} (h1 : a = true → b = true) (h2 : b = true → a = true) : a = b := by
  cases a <;> cases b
  · rfl
  · exact absurd (h2 rfl) (fun e => Bool.noConfusion e)
  · exact absurd (h1 rfl) (fun e => Bool.noConfusion e)
  · rfl

theorem leg_one (h : Nat) : legB (h + h + 1) 1 = true :=
  (legB_iff (h + h) 1).mpr ⟨1, rfl⟩

/-- **(2/p) depends only on p mod 8.** -/
theorem two_period (h K : Nat) (h1 : 1 ≤ h) (hp : Pr (h + h + 1)) (hp' : Pr ((h + K * 4) + (h + K * 4) + 1)) :
    legB ((h + K * 4) + (h + K * 4) + 1) 2 = legB (h + h + 1) 2 := by
  match (par_split h).1, (par_split h).2 with
  | f0, f1 =>
    -- h = n + (n + r)
    have rep : ∃ n r, r ≤ 1 ∧ h = n + (n + r) := by
      cases hpar : par h with
      | false => match f0 hpar with | ⟨k, hk⟩ => exact ⟨k, 0, Nat.zero_le 1, hk⟩
      | true => match f1 hpar with | ⟨k, hk⟩ => exact ⟨k, 1, Nat.le_refl 1, by rw [hk]; lib_nat⟩
    match rep with
    | ⟨n, r, hr, hn⟩ =>
      have hn' : h + K * 4 = (n + K * 2) + ((n + K * 2) + r) := by rw [hn]; lib_nat
      have i1 := TwoSupp.two_sq_iff n r hr (hn ▸ h1) (hn ▸ hp)
      have i2 := TwoSupp.two_sq_iff (n + K * 2) r hr (hn' ▸ Nat.le_trans h1 (Nat.le_add_right h _)) (hn' ▸ hp')
      have hpar : par (n + K * 2 + r) = par (n + r) := by
        rw [show n + K * 2 + r = (n + r) + (K + K) by lib_nat, par_add, par_double]
        cases par (n + r) <;> rfl
      rw [hpar] at i2
      apply bool_eq
      · intro t
        apply (legB_iff (h + h) 2).mpr
        have a1 := (legB_iff ((h + K * 4) + (h + K * 4)) 2).mp t
        rw [hn'] at a1
        have := i1.mpr (i2.mp a1)
        rw [← hn] at this
        exact this
      · intro t
        apply (legB_iff ((h + K * 4) + (h + K * 4)) 2).mpr
        have a1 := (legB_iff (h + h) 2).mp t
        rw [hn] at a1
        have := i2.mpr (i1.mp a1)
        rw [← hn'] at this
        exact this

theorem odd_prime_form (l : Nat) (hq : ∀ d, Dv d (l + 1 + 1 + 1) → d = 1 ∨ d = l + 1 + 1 + 1) :
    ∃ g, l + 1 + 1 + 1 = g + g + 1 := by
  cases hpar : par (l + 1 + 1 + 1) with
  | true => match (par_split _).2 hpar with | ⟨g, hg⟩ => exact ⟨g, hg⟩
  | false =>
    exfalso
    match (par_split _).1 hpar with
    | ⟨k, hk⟩ =>
      cases hq 2 ⟨k, by rw [hk]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e =>
        have : l + 1 = 0 := Nat.succ.inj (Nat.succ.inj e.symm)
        exact Nat.noConfusion this

theorem not_dv_of_mul (p a b : Nat) (h : ¬ Dv p (a * b)) : ¬ Dv p a := fun ⟨c, e⟩ =>
  h ⟨c * b, by rw [e]; lib_nat⟩

/-- **(a/p) depends only on p mod 4a.** -/
theorem leg_period : ∀ F a, a ≤ F → 1 ≤ a → ∀ h h' k, Pr (h + h + 1) → Pr (h' + h' + 1) →
    ¬ Dv (h + h + 1) a → ¬ Dv (h' + h' + 1) a → h' + h' + 1 = h + h + 1 + 4 * a * k →
    legB (h' + h' + 1) a = legB (h + h + 1) a
  | _, 0, _, h1, _, _, _, _, _, _, _, _ => absurd h1 (Nat.not_succ_le_zero 0)
  | _, 1, _, _, h, h', _, _, _, _, _, _ => by rw [leg_one, leg_one]
  | 0, i + 2, hF, _, _, _, _, _, _, _, _, _ => absurd hF (Nat.not_succ_le_zero _)
  | F + 1, i + 2, hF, _, h, h', k, hp, hp', ha, ha', e => by
    match lp_exists i with
    | ⟨l, ⟨a', ea⟩, hmin⟩ =>
      have hq := lp_prime _ l ⟨a', ea⟩ hmin
      -- i + 2 = a' q
      have ha1 : 1 ≤ a' := by
        cases a' with
        | zero => rw [Nat.zero_mul] at ea; exact absurd ea (fun e => Nat.noConfusion e)
        | succ _ => exact Nat.succ_le_succ (Nat.zero_le _)
      have hlt : a' < i + 2 := by
        rw [ea]
        have b : a' < a' * 2 := by rw [Nat.mul_two]; exact Nat.lt_add_of_pos_right ha1
        exact Nat.lt_of_lt_of_le b (Nat.mul_le_mul_left a' (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le l))))
      have eq' : i + 2 = (l + 1 + 1) * a' := by rw [ea, nmul_comm]
      have hpq : ¬ Dv (h + h + 1) (l + 1 + 1) := not_dv_of_mul _ _ _ (eq' ▸ ha)
      have hpa : ¬ Dv (h + h + 1) a' := not_dv_of_mul _ _ _ ((show i + 2 = a' * (l + 1 + 1) from ea) ▸ ha)
      have hpq' : ¬ Dv (h' + h' + 1) (l + 1 + 1) := not_dv_of_mul _ _ _ (eq' ▸ ha')
      have hpa' : ¬ Dv (h' + h' + 1) a' := not_dv_of_mul _ _ _ ((show i + 2 = a' * (l + 1 + 1) from ea) ▸ ha')
      have ih := leg_period F a' (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hlt hF)) ha1 h h' ((l + 1 + 1) * k) hp hp' hpa hpa'
        (by rw [e, ea]; lib_nat)
      have hqq : legB (h' + h' + 1) (l + 1 + 1) = legB (h + h + 1) (l + 1 + 1) := by
        cases l with
        | zero =>
          -- q = 2: h' = h + 4 a' k
          have e2 : h' = h + (a' * k) * 4 := QuadPeriod.double_inj _ _ (by
            have e3 : h' + h' + 1 = (h + (a' * k) * 4) + (h + (a' * k) * 4) + 1 := by rw [e, ea]; lib_nat
            exact Nat.succ.inj e3)
          rw [e2] at hp' ⊢
          exact two_period h (a' * k) (h_pos h hp) hp hp'
        | succ l' =>
          match odd_prime_form l' hq with
          | ⟨g, hg⟩ =>
            rw [hg]
            have hne : h ≠ g := fun hh => hpq (by rw [hg, hh]; exact dv_refl _)
            have hne' : h' ≠ g := fun hh => hpq' (by rw [hg, hh]; exact dv_refl _)
            rw [hg] at hq
            exact QuadPeriod.legB_mod_4q h h' g (a' * k) hp hp' ⟨⟨l' + 1, hg.symm⟩, hq⟩ hne hne' (by rw [e, ea, hg]; lib_nat)
      rw [eq', LegMul.legB_mul h' _ _ hp' hpq' hpa', LegMul.legB_mul h _ _ hp hpq hpa, hqq, ih]

end QuadAll

#print axioms QuadAll.bool_eq
#print axioms QuadAll.leg_one
#print axioms QuadAll.two_period
#print axioms QuadAll.odd_prime_form
#print axioms QuadAll.not_dv_of_mul
#print axioms QuadAll.leg_period

78.17 GL\(_1\) for every cyclotomic field; \(\mathbb F_p^\times \) is cyclic

For a prime \(p\nmid N\),

\[ \exists \,y\in \mathbb F_p^\times \ \text {of order }N\iff N\mid p-1, \qquad \exists \,y\in \mathbb F_{p^2}^\times \ \text {of order }N\iff N\mid p^2-1, \qquad \exists \,g\ \text {of order }p-1 . \]
So \(p\) splits completely in \(\mathbb Q(\zeta _N)\) exactly when \(p\equiv 1\ (N)\). The steps are
\[ \begin {aligned} \ell ^{e}\mid p-1&\Rightarrow y=g_0^{(p-1)/\ell ^{e}}\ \text {has order }\ell ^{e}\quad (g_0\ \text {a non-root of }x^{(p-1)/\ell }-1),\\ \operatorname {ord}a=m,\ \operatorname {ord}b=n,\ \gcd (m,n)=1&\Rightarrow \operatorname {ord}(ab)=mn,\\ p-1=\textstyle \prod \ell ^{e_\ell }&\Rightarrow g=\textstyle \prod y_\ell \ \text {has order }p-1,\\ y^N=1,\ \operatorname {ord}y=N&\Rightarrow N\mid p-1\quad (\text {Fermat}),\\ \text {the same steps in any finite field}&\Rightarrow \ \text {the statement for}\ \mathbb F_{p^2}=\mathbb F_p[\sqrt n]. \end {aligned} \]

Listing 28: lang/CycloSplit.lean

lang/CycloSplit.lean

/-
  lang/CycloSplit — T43, cycle 29 (GL1 for the cyclotomic field ℚ(ζ_q)): p splits completely in ℚ(ζ_q) ⇔ p ≡ 1 (mod q).

  p = J + 2 and q = Q + 2 primes. "p splits completely in ℚ(ζ_q)" ⇔ 𝔽_p contains a primitive q-th root of unity
  (Φ_q has a root mod p) ⇔ ∃ y ≠ 1 with y^q = 1. Then
    split_of_dvd   q | p − 1  ⇒  ∃ y ≠ 1, y^q = 1          (y = g^((p−1)/q), g a non-root of x^((p−1)/q) − 1, PowerSum)
    dvd_of_split   y ≠ 1, y^q = 1  ⇒  q | p − 1            (the order of y is q, and divides p − 1: Order, Fermat)
    cyclo_split    the equivalence
  So the Frobenius of p in Gal(ℚ(ζ_q)/ℚ) = (ℤ/q)^× is trivial exactly when p ≡ 1 (mod q): the splitting of p depends
  only on p mod q — GL1 reciprocity (Artin) for the cyclotomic field, the abelian case that contains every quadratic
  one (QuadArtin, GaussSplit).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PowerSum
import lang.Order
import lang.HasseCM

namespace CycloSplit

open FTAChain ZMod

variable (J : Nat)

theorem pw_one_eq (x : Zp (J + 1)) : Frobenius.pw (ring (J + 1)) x 1 = x := (ring (J + 1)).one_mul x

theorem f_le (f Q : Nat) (h : J + 1 = f * (Q + 2)) (hf : 1 ≤ f) : f ≤ J := by
  have a : f * (Q + 2) = f * Q + f + f := rfl
  have b : f + 1 ≤ f * Q + f + f := Nat.add_le_add (Nat.le_add_left f (f * Q)) hf
  rw [← a, ← h] at b
  exact Nat.le_of_succ_le_succ b

/-- **q | p − 1 ⇒ a primitive q-th root of unity in 𝔽_p.** -/
theorem split_of_dvd (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (Q : Nat)
    (hd : Dv (Q + 2) (J + 1)) : ∃ y : Zp (J + 1), y ≠ (ring (J + 1)).one ∧ Frobenius.pw (ring (J + 1)) y (Q + 2) = (ring (J + 1)).one := by
  match hd with
  | ⟨0, h⟩ => exact absurd (h.trans (Nat.zero_mul _)) (fun e => Nat.noConfusion e)
  | ⟨f' + 1, h⟩ =>
    match PowerSum.exists_nonroot J hpr f' (f_le J (f' + 1) Q h (Nat.succ_le_succ (Nat.zero_le _))) with
    | ⟨g, hg0, hg⟩ =>
      refine ⟨Frobenius.pw (ring (J + 1)) g (f' + 1), hg, ?_⟩
      rw [TwoPower.pwpow, ← h]
      exact unit_pow J hpr g hg0

/-- **A primitive q-th root of unity in 𝔽_p ⇒ q | p − 1.** -/
theorem dvd_of_split (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (Q : Nat)
    (hq : ∀ d, Dv d (Q + 2) → d = 1 ∨ d = Q + 2) (y : Zp (J + 1)) (hy1 : y ≠ (ring (J + 1)).one)
    (hy : Frobenius.pw (ring (J + 1)) y (Q + 2) = (ring (J + 1)).one) : Dv (Q + 2) (J + 1) := by
  have hy0 : y ≠ (ring (J + 1)).zero := fun e => by
    rw [e] at hy
    exact PowerSum.one_ne_zero' J (hy.symm.trans (HasseCM.pw_zero_succ (ring (J + 1)) (Q + 1)))
  match Order.ord_exists (ring (J + 1)) y (Q + 1) hy with
  | ⟨d, hd1, hmin⟩ =>
    have hdq : Dv (d + 1) (Q + 2) := Order.ord_dvd (ring (J + 1)) y d hd1 hmin (Q + 2) hy
    cases hq (d + 1) hdq with
    | inl e =>
      exfalso
      apply hy1
      have : d = 0 := Nat.succ.inj e
      rw [this, pw_one_eq] at hd1
      exact hd1
    | inr e =>
      have := Order.ord_dvd (ring (J + 1)) y d hd1 hmin (J + 1) (unit_pow J hpr y hy0)
      rw [e] at this
      exact this

/-- **Splitting in ℚ(ζ_q) depends only on p mod q.** -/
theorem cyclo_split (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (Q : Nat)
    (hq : ∀ d, Dv d (Q + 2) → d = 1 ∨ d = Q + 2) :
    (∃ y : Zp (J + 1), y ≠ (ring (J + 1)).one ∧ Frobenius.pw (ring (J + 1)) y (Q + 2) = (ring (J + 1)).one) ↔ Dv (Q + 2) (J + 1) :=
  ⟨fun ⟨y, h1, h2⟩ => dvd_of_split J hpr Q hq y h1 h2, split_of_dvd J hpr Q⟩

end CycloSplit

#print axioms CycloSplit.pw_one_eq
#print axioms CycloSplit.f_le
#print axioms CycloSplit.split_of_dvd
#print axioms CycloSplit.dvd_of_split
#print axioms CycloSplit.cyclo_split

Listing 29: lang/CycloPow.lean

lang/CycloPow.lean

/-
  lang/CycloPow — T43, cycle 30 (GL1 for ℚ(ζ_{ℓ^e})): p splits completely in ℚ(ζ_{ℓ^e}) ⇔ p ≡ 1 (mod ℓ^e).

  p = J + 2, ℓ = t + 2 primes, e = s + 1. A primitive ℓ^e-th root of unity in 𝔽_p is y with y^(ℓ^(s+1)) = 1 and
  y^(ℓ^s) ≠ 1. Then
    split_pow     ℓ^(s+1) | p − 1 ⇒ such y exists   (y = g^f with p − 1 = f ℓ^(s+1), g a non-root of x^((p−1)/ℓ) − 1)
    dvd_of_pow    such y ⇒ ℓ^(s+1) | p − 1         (its order is ℓ^(s+1): PrimePowerOne.proper_div, Order, Fermat)
    cyclo_pow     the equivalence
  With CycloSplit (e = 1) this is Artin reciprocity for every prime-power cyclotomic field; the ℓ-parts multiply to
  ℚ(ζ_N) once orders of commuting elements with coprime orders multiply (next), which also gives 𝔽_p^× cyclic.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.CycloSplit
import lang.PrimePowerOne

namespace CycloPow

open FTAChain ZMod TwoPower Order PrimeOneModQ PrimePowerOne

variable (J : Nat)

theorem split_pow (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (t s : Nat)
    (hd : Dv ((t + 1 + 1) ^ (s + 1)) (J + 1)) :
    ∃ y : Zp (J + 1), Frobenius.pw (ring (J + 1)) y ((t + 1 + 1) ^ (s + 1)) = (ring (J + 1)).one ∧
      Frobenius.pw (ring (J + 1)) y ((t + 1 + 1) ^ s) ≠ (ring (J + 1)).one := by
  match hd, pow_succ_form (t + 1) s with
  | ⟨0, h⟩, _ => exact absurd (h.trans (Nat.zero_mul _)) (fun e => Nat.noConfusion e)
  | ⟨f' + 1, h⟩, ⟨n, hn⟩ =>
    -- F = (f'+1) ℓ^s, J + 1 = F ℓ
    have hF : J + 1 = ((f' + 1) * (t + 1 + 1) ^ s) * (t + 1 + 1) := by
      rw [h, pow_succ', nmul_assoc]
    have hF1 : (f' + 1) * (t + 1 + 1) ^ s = ((f' + 1) * n + f') + 1 := by
      rw [hn]; rfl
    have hFJ := CycloSplit.f_le J ((f' + 1) * (t + 1 + 1) ^ s) t hF (by rw [hF1]; exact Nat.succ_le_succ (Nat.zero_le _))
    rw [hF1] at hFJ
    match PowerSum.exists_nonroot J hpr ((f' + 1) * n + f') hFJ with
    | ⟨g, hg0, hg⟩ =>
      refine ⟨Frobenius.pw (ring (J + 1)) g (f' + 1), ?_, ?_⟩
      · rw [pwpow, ← h]
        exact unit_pow J hpr g hg0
      · rw [pwpow, hF1]
        exact hg

theorem dvd_of_pow (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (t s : Nat)
    (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) (x : Zp (J + 1))
    (hxfull : Frobenius.pw (ring (J + 1)) x ((t + 1 + 1) ^ (s + 1)) = (ring (J + 1)).one)
    (hX1 : Frobenius.pw (ring (J + 1)) x ((t + 1 + 1) ^ s) ≠ (ring (J + 1)).one) : Dv ((t + 1 + 1) ^ (s + 1)) (J + 1) := by
  obtain ⟨n, hn⟩ := pow_succ_form (t + 1) (s + 1)
  rw [hn] at hxfull
  obtain ⟨d, hd, hdmin⟩ := ord_exists (ring (J + 1)) x n hxfull
  obtain ⟨c, hc⟩ := ord_dvd (ring (J + 1)) x d hd hdmin (n + 1) hxfull
  have hdq : d + 1 = (t + 1 + 1) ^ (s + 1) := by
    cases c with
    | zero => rw [nzero_mul] at hc; exact absurd hc (Nat.noConfusion)
    | succ c =>
      cases c with
      | zero => rw [hn, hc, nmul_comm, nmul_one]
      | succ c =>
        exfalso
        apply hX1
        have hpd := proper_div t s (d + 1) c hq (by rw [hn, hc, nmul_comm])
        obtain ⟨m', hm'⟩ := hpd
        rw [hm']
        exact pw_qe (ring (J + 1)) x (d + 1) 0 hd m'
  have hx0 : x ≠ (ring (J + 1)).zero := fun e => by
    rw [e] at hd
    exact PowerSum.one_ne_zero' J (hd.symm.trans (HasseCM.pw_zero_succ (ring (J + 1)) d))
  have := ord_dvd (ring (J + 1)) x d hd hdmin (J + 1) (unit_pow J hpr x hx0)
  rw [hdq] at this
  exact this

/-- **Splitting in ℚ(ζ_{ℓ^e}) depends only on p mod ℓ^e.** -/
theorem cyclo_pow (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (t s : Nat)
    (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) :
    (∃ y : Zp (J + 1), Frobenius.pw (ring (J + 1)) y ((t + 1 + 1) ^ (s + 1)) = (ring (J + 1)).one ∧
      Frobenius.pw (ring (J + 1)) y ((t + 1 + 1) ^ s) ≠ (ring (J + 1)).one) ↔ Dv ((t + 1 + 1) ^ (s + 1)) (J + 1) :=
  ⟨fun ⟨y, h1, h2⟩ => dvd_of_pow J hpr t s hq y h1 h2, split_pow J hpr t s⟩

end CycloPow

#print axioms CycloPow.split_pow
#print axioms CycloPow.dvd_of_pow
#print axioms CycloPow.cyclo_pow

Listing 30: lang/OrdMul.lean

lang/OrdMul.lean

/-
  lang/OrdMul — T43, cycle 32: orders in a commutative ring; coprime orders multiply; exact order ℓ^e in 𝔽_p.

    Ord x m           x^m = 1 and every r with x^r = 1 is a multiple of m (core ∣)
    ord_of_min        the least positive exponent (Order.ord_exists) is the order
    ord_mul_coprime   Ord a m, Ord b n, m n coprime ⇒ Ord (a b) (m n)      (LibGcd.gauss, coprime_mul_dvd)
    ord_prime_pow     ℓ^e | p − 1 ⇒ some y ∈ 𝔽_p has Ord y ℓ^e            (CycloPow.split_pow + the order argument)
  Next: splitting p − 1 into prime powers gives an element of order p − 1 (𝔽_p^× cyclic) and Artin reciprocity for
  ℚ(ζ_N), N arbitrary.

  Lean 4 core only, proofs by hand (lib/Gcd from lib_shared), no axioms.
-/
import lang.CycloPow
import lib.Gcd

namespace OrdMul

open FTAChain ZMod TwoPower Order PrimeOneModQ PrimePowerOne

variable {R : Type} (K : DetChain.CRing R)

def Ord (x : R) (m : Nat) : Prop := Frobenius.pw K x m = K.one ∧ ∀ r, Frobenius.pw K x r = K.one → m ∣ r

theorem dv_to_dvd {d f : Nat} (h : Dv d f) : d ∣ f :=
  match h with
  | ⟨q, e⟩ => ⟨q, by rw [e, nmul_comm]⟩

theorem ord_of_min (x : R) (d : Nat) (hd : Frobenius.pw K x (d + 1) = K.one)
    (hmin : ∀ r, 0 < r → r < d + 1 → Frobenius.pw K x r ≠ K.one) : Ord K x (d + 1) :=
  ⟨hd, fun r hr => dv_to_dvd (ord_dvd K x d hd hmin r hr)⟩

theorem pw_one_of_dvd (x : R) (m r : Nat) (hm : Frobenius.pw K x m = K.one) (h : m ∣ r) :
    Frobenius.pw K x r = K.one := by
  match h with
  | ⟨c, e⟩ => rw [e, ← pwpow, hm, pwone]

/-- **Coprime orders multiply.** -/
theorem ord_mul_coprime (a b : R) (m n : Nat) (ha : Ord K a m) (hb : Ord K b n)
    (hc : ∀ d, d ∣ m → d ∣ n → d = 1) : Ord K (K.mul a b) (m * n) := by
  refine ⟨?_, fun r hr => ?_⟩
  · rw [Frobenius.pw_mul, pw_one_of_dvd K a m (m * n) ha.1 ⟨n, rfl⟩,
      pw_one_of_dvd K b n (m * n) hb.1 ⟨m, nmul_comm m n⟩, K.one_mul]
  · -- (ab)^(r n) = 1 ⇒ a^(r n) = 1 ⇒ m | r n ⇒ m | r
    have hrn : Frobenius.pw K (K.mul a b) (r * n) = K.one := by
      rw [← pwpow, hr, pwone]
    rw [Frobenius.pw_mul, pw_one_of_dvd K b n (r * n) hb.1 ⟨r, nmul_comm r n⟩, K.mul_comm, K.one_mul] at hrn
    have hm : m ∣ r := LibGcd.gauss hc (ha.2 _ hrn)
    have hrm : Frobenius.pw K (K.mul a b) (r * m) = K.one := by
      rw [← pwpow, hr, pwone]
    rw [Frobenius.pw_mul, pw_one_of_dvd K a m (r * m) ha.1 ⟨r, nmul_comm r m⟩, K.one_mul] at hrm
    have hn : n ∣ r := LibGcd.gauss (fun d d1 d2 => hc d d2 d1) (hb.2 _ hrm)
    exact LibGcd.coprime_mul_dvd hc hm hn

variable (J : Nat)

/-- **An element of exact order ℓ^(s+1) in 𝔽_p** when ℓ^(s+1) | p − 1. -/
theorem ord_prime_pow (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (t s : Nat)
    (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) (hd : Dv ((t + 1 + 1) ^ (s + 1)) (J + 1)) :
    ∃ y : Zp (J + 1), Ord (ring (J + 1)) y ((t + 1 + 1) ^ (s + 1)) := by
  match CycloPow.split_pow J hpr t s hd with
  | ⟨x, hxfull, hX1⟩ =>
    obtain ⟨n, hn⟩ := pow_succ_form (t + 1) (s + 1)
    rw [hn] at hxfull
    obtain ⟨d, hd1, hdmin⟩ := ord_exists (ring (J + 1)) x n hxfull
    obtain ⟨c, hc⟩ := ord_dvd (ring (J + 1)) x d hd1 hdmin (n + 1) hxfull
    have hdq : d + 1 = (t + 1 + 1) ^ (s + 1) := by
      cases c with
      | zero => rw [nzero_mul] at hc; exact absurd hc (Nat.noConfusion)
      | succ c =>
        cases c with
        | zero => rw [hn, hc, nmul_comm, nmul_one]
        | succ c =>
          exfalso
          apply hX1
          have hpd := proper_div t s (d + 1) c hq (by rw [hn, hc, nmul_comm])
          obtain ⟨m', hm'⟩ := hpd
          rw [hm']
          exact pw_qe (ring (J + 1)) x (d + 1) 0 hd1 m'
    exact ⟨x, hdq ▸ ord_of_min (ring (J + 1)) x d hd1 hdmin⟩

end OrdMul

#print axioms OrdMul.dv_to_dvd
#print axioms OrdMul.ord_of_min
#print axioms OrdMul.pw_one_of_dvd
#print axioms OrdMul.ord_mul_coprime
#print axioms OrdMul.ord_prime_pow

Listing 31: lang/PrimRoot.lean

lang/PrimRoot.lean

/-
  lang/PrimRoot — T43, cycle 33: 𝔽_p^× is cyclic, and Artin reciprocity for every cyclotomic field ℚ(ζ_N).

  p = J + 2 prime, ℤ/p = ring (J + 1), orders as in lang/OrdMul (Ord x m: x^m = 1 and m divides every r with x^r = 1).
    extract          n ≥ 1, ℓ prime ⇒ n = ℓ^e n' with ℓ ∤ n'
    coprime_pow      ℓ ∤ n' (n' ≥ 1) ⇒ ℓ^(s+1) and n' are coprime
    ord_of_dvd       every n | p − 1 is the order of some element  (prime powers by OrdMul.ord_prime_pow, glued by
                     OrdMul.ord_mul_coprime, strong induction on n)
    primitive_root   some g has order p − 1: 𝔽_p^× is cyclic
    cyclo_N          for N ≥ 1: 𝔽_p has an element of order N  ⇔  N | p − 1
  So p splits completely in ℚ(ζ_N) (p ∤ N) exactly when p ≡ 1 (mod N): the Frobenius of p in
  Gal(ℚ(ζ_N)/ℚ) = (ℤ/N)^× is trivial iff p ≡ 1 — GL1 reciprocity (Kronecker–Weber side) for all cyclotomic fields.

  Lean 4 core only, proofs by hand (lib/Gcd from lib_shared), no axioms.
-/
import lang.OrdMul

namespace PrimRoot

open FTAChain ZMod TwoPower Order PrimeOneModQ PrimePowerOne OrdMul

theorem dvd_of_dv {d f : Nat} (h : Dv d f) : d ∣ f := dv_to_dvd h

theorem dv_of_dvd {d f : Nat} (h : d ∣ f) : Dv d f :=
  match h with
  | ⟨c, e⟩ => ⟨c, by rw [e, nmul_comm]⟩

/-- **ℓ-part.** -/
theorem extract (l : Nat) : ∀ F n, n ≤ F → 1 ≤ n →
    ∃ e n', n = (l + 1 + 1) ^ e * n' ∧ ¬ ((l + 1 + 1) ∣ n')
  | 0, n, hF, h1 => absurd (Nat.le_trans h1 hF) (Nat.not_succ_le_zero 0)
  | F + 1, n, hF, h1 =>
    match dv_dec (l + 1) n with
    | Or.inr hnd => ⟨0, n, by rw [Nat.pow_zero, Nat.one_mul], fun h => hnd (dv_of_dvd h)⟩
    | Or.inl ⟨q, hq⟩ => by
      -- n = q ℓ, 1 ≤ q < n
      have hq1 : 1 ≤ q := by
        cases q with
        | zero => rw [hq, Nat.zero_mul] at h1; exact absurd h1 (Nat.not_succ_le_zero 0)
        | succ q' => exact Nat.succ_le_succ (Nat.zero_le _)
      have hlt : q < n := by
        rw [hq]
        have a : q * 2 ≤ q * (l + 1 + 1) := Nat.mul_le_mul_left q (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le l)))
        have b : q < q * 2 := by
          rw [Nat.mul_two]
          exact Nat.lt_add_of_pos_right hq1
        exact Nat.lt_of_lt_of_le b a
      match extract l F q (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hlt hF)) hq1 with
      | ⟨e, n', he, hn'⟩ =>
        refine ⟨e + 1, n', ?_, hn'⟩
        rw [hq, he, Nat.pow_succ, nmul_assoc, nmul_assoc, nmul_comm n' (l + 1 + 1)]

theorem coprime_pow (l s n' : Nat) (hl : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hn1 : 1 ≤ n') (hn : ¬ ((l + 1 + 1) ∣ n')) :
    ∀ d, d ∣ (l + 1 + 1) ^ (s + 1) → d ∣ n' → d = 1
  | 0, _, h2 => by
    match h2 with
    | ⟨c, e⟩ => rw [e, Nat.zero_mul] at hn1; exact absurd hn1 (Nat.not_succ_le_zero 0)
  | 1, _, _ => rfl
  | k + 2, h1, h2 => by
    exfalso
    match lp_exists k with
    | ⟨r, hr, hmin⟩ =>
      have hrp := lp_prime _ r hr hmin
      have hr1 : Dv (r + 1 + 1) ((l + 1 + 1) ^ (s + 1)) := dv_trans _ _ _ hr (dv_of_dvd h1)
      have := prime_dvd_pow r l hrp hl (s + 1) hr1
      apply hn
      rw [← this]
      exact LibGcd.dvd_trans' (dvd_of_dv hr) h2

variable (J : Nat)

theorem ord_one : Ord (ring (J + 1)) (ring (J + 1)).one 1 :=
  ⟨pwone (ring (J + 1)) 1, fun r _ => LibGcd.one_dvd' r⟩

/-- **Every divisor of p − 1 is an order.** -/
theorem ord_of_dvd (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) :
    ∀ F n, n ≤ F → n ∣ J + 1 → ∃ g : Zp (J + 1), Ord (ring (J + 1)) g n
  | _, 0, _, h => by
    match h with
    | ⟨c, e⟩ => exact absurd (e.trans (Nat.zero_mul c)) (fun e' => Nat.noConfusion e')
  | _, 1, _, _ => ⟨(ring (J + 1)).one, ord_one J⟩
  | 0, k + 2, hF, _ => absurd hF (Nat.not_succ_le_zero _)
  | F + 1, k + 2, hF, hd => by
    match lp_exists k with
    | ⟨l, hl, hmin⟩ =>
      have hlp := lp_prime _ l hl hmin
      match extract l (k + 2) (k + 2) (Nat.le_refl _) (Nat.succ_le_succ (Nat.zero_le _)) with
      | ⟨0, n', he, hn'⟩ =>
        exfalso; apply hn'
        rw [Nat.pow_zero, Nat.one_mul] at he
        rw [← he]; exact dvd_of_dv hl
      | ⟨s + 1, n', he, hn'⟩ =>
        have hn1 : 1 ≤ n' := by
          cases n' with
          | zero => rw [Nat.mul_zero] at he; exact absurd he (fun e => Nat.noConfusion e)
          | succ _ => exact Nat.succ_le_succ (Nat.zero_le _)
        have hP2 : 2 ≤ (l + 1 + 1) ^ (s + 1) := by
          rw [Nat.pow_succ]
          have hp1 : 1 ≤ (l + 1 + 1) ^ s := match pow_succ_form (l + 1) s with
            | ⟨r, hr⟩ => by rw [hr]; exact Nat.succ_le_succ (Nat.zero_le r)
          have := Nat.mul_le_mul hp1
            (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le l)))
          rw [Nat.one_mul] at this
          exact this
        have hlt : n' < k + 2 := by
          rw [he]
          have a : 2 * n' ≤ (l + 1 + 1) ^ (s + 1) * n' := Nat.mul_le_mul_right n' hP2
          have b : n' < 2 * n' := by
            rw [Nat.two_mul]; exact Nat.lt_add_of_pos_right hn1
          exact Nat.lt_of_lt_of_le b a
        have hPd : (l + 1 + 1) ^ (s + 1) ∣ J + 1 := LibGcd.dvd_trans' ⟨n', he⟩ hd
        have hnd : n' ∣ J + 1 :=
          LibGcd.dvd_trans' ⟨(l + 1 + 1) ^ (s + 1), by rw [he, Nat.mul_comm]⟩ hd
        match ord_prime_pow J hpr l s hlp (dv_of_dvd hPd),
          ord_of_dvd hpr F n' (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hlt hF)) hnd with
        | ⟨y, hy⟩, ⟨z, hz⟩ =>
          refine ⟨(ring (J + 1)).mul y z, ?_⟩
          rw [he]
          exact ord_mul_coprime (ring (J + 1)) y z _ _ hy hz (coprime_pow l s n' hlp hn1 hn')

/-- **𝔽_p^× is cyclic.** -/
theorem primitive_root (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) :
    ∃ g : Zp (J + 1), Ord (ring (J + 1)) g (J + 1) :=
  ord_of_dvd J hpr (J + 1) (J + 1) (Nat.le_refl _) (LibGcd.dvd_refl' _)

/-- **Reciprocity for ℚ(ζ_N)**: an element of order N exists iff N | p − 1. -/
theorem cyclo_N (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (N : Nat) :
    (∃ y : Zp (J + 1), Ord (ring (J + 1)) y (N + 1)) ↔ (N + 1) ∣ J + 1 :=
  ⟨fun ⟨y, hy⟩ => by
    have hy0 : y ≠ (ring (J + 1)).zero := fun e => by
      have h := hy.1
      rw [e] at h
      exact PowerSum.one_ne_zero' J (h.symm.trans (HasseCM.pw_zero_succ (ring (J + 1)) N))
    exact hy.2 _ (unit_pow J hpr y hy0),
   fun hd => ord_of_dvd J hpr (N + 1) (N + 1) (Nat.le_refl _) hd⟩

end PrimRoot

#print axioms PrimRoot.dvd_of_dv
#print axioms PrimRoot.dv_of_dvd
#print axioms PrimRoot.extract
#print axioms PrimRoot.coprime_pow
#print axioms PrimRoot.ord_one
#print axioms PrimRoot.ord_of_dvd
#print axioms PrimRoot.primitive_root
#print axioms PrimRoot.cyclo_N

Listing 32: lang/FinCyclo.lean

lang/FinCyclo.lean

/-
  lang/FinCyclo — T43, cycle 93: roots of unity in every finite field, and residue degree 2 in ℚ(ζ_N).

    cyclo_ff     for every FF of order q:  𝔽_q has an element of order N + 1  ⇔  N + 1 | q − 1
    cyclo_p2     for every odd prime p:    𝔽_{p²} has an element of order N + 1  ⇔  N + 1 | p² − 1
  So a prime p ∤ N has residue degree f ≤ 2 in ℚ(ζ_N) (𝔽_{p²} ⊇ ℤ[ζ_N]/𝔭) exactly when p² ≡ 1 (mod N): the Frobenius of p
  in (ℤ/N)^× has order dividing 2 — GL1 reciprocity for ℚ(ζ_N) at the level of residue degrees (PrimRoot.cyclo_N is f = 1).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.FinCyclic
import lang.Fq2Inst

namespace FinCyclo

open FTAChain ZMod YMChain FinField OrdMul

theorem cyclo_ff {R : Type} [DecidableEq R] (F : FF R) (N : Nat) :
    (∃ y, Ord F.K y (N + 1)) ↔ (N + 1) ∣ 2 * F.h :=
  ⟨fun ⟨y, hy⟩ => by
    have hy0 : y ≠ F.K.zero := fun e => by
      have h := hy.1
      rw [e] at h
      exact FinConic.one_ne_zero F (h.symm.trans (HasseCM.pw_zero_succ F.K N))
    exact hy.2 _ (FinCyclic.fermat_ff F y hy0),
   fun hd => FinCyclic.ord_of_dvd_ff F (N + 1) (N + 1) (Nat.le_refl _) hd⟩

theorem two_h (k : Nat) : 2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) = (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) - 1 := by
  rw [show (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) = 2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1 by lib_nat]
  rfl

/-- **Residue degree ≤ 2 in ℚ(ζ_{N+1})**: 𝔽_{p²} contains an element of order N + 1 iff N + 1 | p² − 1. -/
theorem cyclo_p2 (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (N : Nat) :
    (∃ y, Ord (Fq2.G2 (2 * k + 1) n) y (N + 1)) ↔ (N + 1) ∣ (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) - 1 := by
  rw [← two_h k]
  exact cyclo_ff (Fq2Inst.ffq k hpr n hn) N

end FinCyclo

#print axioms FinCyclo.cyclo_ff
#print axioms FinCyclo.two_h
#print axioms FinCyclo.cyclo_p2

78.18 GL\(_1\): split primes are norms

\[ p\equiv 1\ (4)\Rightarrow p=u^2+v^2,\qquad p\equiv 1\ (3)\Rightarrow p=u^2+3v^2 . \]
The steps are
\[ \begin {aligned} &k=\lfloor \sqrt p\rfloor ,\ (k+1)^2>p\ \text {points }(a,b),\ 0\le a,b\le k,\\ &a+zb\equiv a'+zb'\ (p)\quad (\text {pigeonhole}),\ u=|a-a'|,\ v=|b-b'|,\\ &z^2=-1:\ p\mid u^2+v^2,\ 0<u^2+v^2<2p\Rightarrow u^2+v^2=p,\\ &z^2=-3:\ p\mid u^2+3v^2<4p,\ u^2+3v^2\ne 2p,\ u^2+3v^2=3p\Rightarrow v^2+3(u/3)^2=p. \end {aligned} \]

Listing 33: lang/TwoSquares.lean

lang/TwoSquares.lean

/-
  lang/TwoSquares — T43, cycle 26: Fermat's two squares theorem: every prime p ≡ 1 (mod 4) is a² + b².

  The split primes of ℤ[i] (lang/GaussSplit) are norms: p = N(a + bi). Assembled from
    isqrt          ∀ n, ∃ k, k² ≤ n < (k+1)²
    not_square     a prime is not a square, so p < (k+1)² strictly
    grid, dist_grid, len_grid, mem_grid   the (k+1)² points (a, b) with a, b ≤ k, pairwise distinct
    Pigeon.collision                      two of them have equal (a + y b) mod p
    Thue.thue_core                        then p = u² + v²
    two_squares    p = 4j + 5 prime  ⇒  ∃ u v, u² + v² = p
  With GaussSplit: an odd prime is a sum of two squares iff p ≡ 1 (mod 4) (the converse: squares are 0, 1 mod 4).
  This is the norm form of the Hecke character of ℚ(i) at split primes, ψ(π) = ±(u + vi), behind a_p = ±2u for the
  CM form of y² = x³ − x.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Thue
import lang.Pigeon
import lang.GaussSplit

namespace TwoSquares

open FTAChain ZMod YMChain Pigeon

theorem isqrt : ∀ n, ∃ k, k * k ≤ n ∧ n < (k + 1) * (k + 1)
  | 0 => ⟨0, Nat.le_refl 0, Nat.zero_lt_one⟩
  | n + 1 =>
    match isqrt n with
    | ⟨k, h1, h2⟩ =>
      match Nat.lt_or_ge (n + 1) ((k + 1) * (k + 1)) with
      | Or.inl h => ⟨k, Nat.le_trans h1 (Nat.le_succ n), h⟩
      | Or.inr h => by
        refine ⟨k + 1, h, ?_⟩
        have a : n + 1 ≤ (k + 1) * (k + 1) := Nat.succ_le_of_lt h2
        have b : (k + 1) * (k + 1) ≤ (k + 1 + 1) * (k + 1) := Nat.mul_le_mul_right (k + 1) (Nat.le_succ (k + 1))
        have c : (k + 1 + 1) * (k + 1) < (k + 1 + 1) * (k + 1) + (k + 1 + 1) :=
          Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)
        show n + 1 < (k + 1 + 1) * (k + 1) + (k + 1 + 1)
        exact Nat.lt_of_le_of_lt (Nat.le_trans a b) c

theorem not_square (m k : Nat) (hm : 1 ≤ m) (hpr : ∀ d, Dv d (m + 1) → d = 1 ∨ d = m + 1)
    (e : (k + 1) * (k + 1) = m + 1) : False := by
  cases hpr (k + 1) ⟨k + 1, e.symm⟩ with
  | inl h =>
    rw [h] at e
    have : m = 0 := Nat.succ.inj e.symm
    rw [this] at hm
    exact Nat.not_succ_le_zero 0 hm
  | inr h =>
    rw [h] at e
    have e1 : (m + 1) * (m + 1) = (m + 1) * 1 := e.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ m) e1
    have : m = 0 := Nat.succ.inj this
    rw [this] at hm
    exact Nat.not_succ_le_zero 0 hm

def rngL : Nat → List Nat
  | 0 => []
  | n + 1 => rngL n ++ [n]

theorem len_app {A : Type} : ∀ l l' : List A, (l ++ l').length = l.length + l'.length
  | [], l' => (Nat.zero_add _).symm
  | _ :: l, l' => by
    show Nat.succ (l ++ l').length = Nat.succ l.length + l'.length
    rw [len_app l l', Nat.succ_add]

theorem mem_app {A : Type} (r : A) : ∀ l l' : List A, memL r (l ++ l') → memL r l ∨ memL r l'
  | [], _, h => Or.inr h
  | _ :: l, l', h => by
    cases h with
    | inl e => exact Or.inl (Or.inl e)
    | inr e =>
      cases mem_app r l l' e with
      | inl h' => exact Or.inl (Or.inr h')
      | inr h' => exact Or.inr h'

theorem dist_app {A : Type} : ∀ l l' : List A, Dist l → Dist l' → (∀ x y, memL x l → memL y l' → x ≠ y) →
    Dist (l ++ l')
  | [], _, _, h', _ => h'
  | a :: l, l', hd, hd', hx => ⟨fun s hs => by
      cases mem_app s l l' hs with
      | inl h => exact hd.1 s h
      | inr h => exact fun e => hx a s (Or.inl rfl) h e.symm,
    dist_app l l' hd.2 hd' (fun x y hx' hy => hx x y (Or.inr hx') hy)⟩

theorem mem_rngL (x : Nat) : ∀ n, memL x (rngL n) → x < n
  | 0, h => False.elim h
  | n + 1, h => by
    cases mem_app x (rngL n) [n] h with
    | inl h' => exact Nat.lt_succ_of_lt (mem_rngL x n h')
    | inr h' =>
      cases h' with
      | inl e => rw [e]; exact Nat.lt_succ_self n
      | inr e => exact False.elim e

theorem dist_rngL : ∀ n, Dist (rngL n)
  | 0 => trivial
  | n + 1 => dist_app (rngL n) [n] (dist_rngL n) ⟨fun _ h => False.elim h, trivial⟩
      (fun x y hx hy e => by
        cases hy with
        | inl ey => rw [e, ey] at hx; exact Nat.lt_irrefl n (mem_rngL n n hx)
        | inr ey => exact False.elim ey)

theorem len_rngL : ∀ n, (rngL n).length = n
  | 0 => rfl
  | n + 1 => by
    show (rngL n ++ [n]).length = n + 1
    rw [len_app, len_rngL n]
    rfl

def row (k1 b : Nat) : List (Nat × Nat) := (rngL k1).map (fun a => (a, b))

def grid (k1 : Nat) : Nat → List (Nat × Nat)
  | 0 => []
  | j + 1 => grid k1 j ++ row k1 j

theorem mem_map {A B : Type} (f : A → B) (x : B) : ∀ l : List A, memL x (l.map f) → ∃ y, memL y l ∧ f y = x
  | [], h => False.elim h
  | a :: l, h => by
    cases h with
    | inl e => exact ⟨a, Or.inl rfl, e.symm⟩
    | inr e =>
      match mem_map f x l e with
      | ⟨y, hy, ey⟩ => exact ⟨y, Or.inr hy, ey⟩

theorem dist_map {A B : Type} (f : A → B) (hf : ∀ x y, f x = f y → x = y) : ∀ l : List A, Dist l → Dist (l.map f)
  | [], _ => trivial
  | a :: l, hd => ⟨fun s hs e => match mem_map f s l hs with
      | ⟨y, hy, ey⟩ => hd.1 y hy (hf y a (ey.trans e)),
    dist_map f hf l hd.2⟩

theorem mem_row (k1 b : Nat) (x : Nat × Nat) (h : memL x (row k1 b)) : x.1 < k1 ∧ x.2 = b :=
  match mem_map (fun a => (a, b)) x (rngL k1) h with
  | ⟨a, ha, ea⟩ => by rw [← ea]; exact ⟨mem_rngL a k1 ha, rfl⟩

theorem mem_grid (k1 : Nat) (x : Nat × Nat) : ∀ j, memL x (grid k1 j) → x.1 < k1 ∧ x.2 < j
  | 0, h => False.elim h
  | j + 1, h => by
    cases mem_app x (grid k1 j) (row k1 j) h with
    | inl h' =>
      have := mem_grid k1 x j h'
      exact ⟨this.1, Nat.lt_succ_of_lt this.2⟩
    | inr h' =>
      have := mem_row k1 j x h'
      exact ⟨this.1, by rw [this.2]; exact Nat.lt_succ_self j⟩

theorem dist_grid (k1 : Nat) : ∀ j, Dist (grid k1 j)
  | 0 => trivial
  | j + 1 => dist_app (grid k1 j) (row k1 j) (dist_grid k1 j)
      (dist_map (fun a => (a, j)) (fun x y e => congrArg Prod.fst e) (rngL k1) (dist_rngL k1))
      (fun x y hx hy e => by
        have a := (mem_grid k1 x j hx).2
        have b := (mem_row k1 j y hy).2
        rw [e, b] at a
        exact Nat.lt_irrefl j a)

theorem len_map {A B : Type} (f : A → B) : ∀ l : List A, (l.map f).length = l.length
  | [] => rfl
  | _ :: l => congrArg Nat.succ (len_map f l)

theorem len_grid (k1 : Nat) : ∀ j, (grid k1 j).length = j * k1
  | 0 => (Nat.zero_mul k1).symm
  | j + 1 => by
    show (grid k1 j ++ row k1 j).length = (j + 1) * k1
    rw [len_app, len_grid k1 j, Nat.succ_mul]
    show j * k1 + ((rngL k1).map _).length = j * k1 + k1
    rw [len_map, len_rngL]

/-- **Fermat's two squares theorem** for p = 4j + 5. -/
theorem two_squares (j : Nat)
    (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * j + 1) + 1 + 1 + 1) :
    ∃ u v, u * u + v * v = 2 * (2 * j + 1) + 1 + 1 + 1 := by
  match GaussSplit.split_one_mod_four j hpr, isqrt (2 * (2 * j + 1) + 1 + 1) with
  | ⟨y, hy⟩, ⟨k, hk1, hk2⟩ =>
    have hk : k * k < 2 * (2 * j + 1) + 1 + 1 + 1 := Nat.lt_succ_of_le hk1
    have hlen : 2 * (2 * j + 1) + 1 + 1 + 1 < (grid (k + 1) (k + 1)).length := by
      rw [len_grid]
      exact Nat.lt_of_le_of_ne (Nat.succ_le_of_lt hk2) (fun e =>
        not_square (2 * (2 * j + 1) + 1 + 1) k (Nat.succ_le_succ (Nat.zero_le _)) hpr e.symm)
    match collision (fun x => md (2 * (2 * j + 1) + 1 + 1) (x.1 + y.val * x.2)) (2 * (2 * j + 1) + 1 + 1 + 1)
        (grid (k + 1) (k + 1)) (dist_grid (k + 1) (k + 1)) (fun x => md_lt _ _) hlen with
    | ⟨(a, b), (a', b'), hx, hx', hne, hf⟩ =>
      have ma := mem_grid (k + 1) (a, b) (k + 1) hx
      have ma' := mem_grid (k + 1) (a', b') (k + 1) hx'
      have hc : (ring (2 * (2 * j + 1) + 1 + 1)).add (mk _ a) ((ring _).mul y (mk _ b))
          = (ring (2 * (2 * j + 1) + 1 + 1)).add (mk _ a') ((ring _).mul y (mk _ b')) := by
        rw [← mk_val _ y, TwoPower.mk_mul, TwoPower.mk_mul, PrimeOneModQ.mk_add, PrimeOneModQ.mk_add]
        exact Subtype.ext hf
      exact Thue.thue_core (2 * (2 * j + 1) + 1 + 1) y hy k a b a' b' hk (Nat.le_of_lt_succ ma.1) (Nat.le_of_lt_succ ma.2)
          (Nat.le_of_lt_succ ma'.1) (Nat.le_of_lt_succ ma'.2) hne hc

end TwoSquares

#print axioms TwoSquares.isqrt
#print axioms TwoSquares.not_square
#print axioms TwoSquares.len_app
#print axioms TwoSquares.mem_app
#print axioms TwoSquares.dist_app
#print axioms TwoSquares.mem_rngL
#print axioms TwoSquares.dist_rngL
#print axioms TwoSquares.len_rngL
#print axioms TwoSquares.mem_map
#print axioms TwoSquares.dist_map
#print axioms TwoSquares.mem_row
#print axioms TwoSquares.mem_grid
#print axioms TwoSquares.dist_grid
#print axioms TwoSquares.len_map
#print axioms TwoSquares.len_grid
#print axioms TwoSquares.two_squares

Listing 34: lang/EisenNorm.lean

lang/EisenNorm.lean

/-
  lang/EisenNorm — T43, cycle 38: every prime p ≡ 1 (mod 3) is u² + 3v² (the norm form of ℤ[√−3] ⊂ ℤ[ω]).

  p = J + 2 prime, 3 | p − 1, z² = −3 in 𝔽_p (Eisen.split_neg3). Thue's grid (TwoSquares, Pigeon) gives two points with
  a + z b ≡ a' + z b'; with u = |a − a'|, v = |b − b'|:
    sq3_zero      X = zY, z² = −3 ⇒ X² + 3Y² = 0
    core3         p | u² + 3v², 0 < u² + 3v² < 4p
    mult_cases    p | n, 0 < n < 4p ⇒ n ∈ {p, 2p, 3p}
    case3         u² + 3v² = 3p ⇒ 3 | u and v² + 3(u/3)² = p
    case2_absurd  u² + 3v² = 2p is impossible for odd p (u ≡ v mod 2 and then 4 | u² + 3v²)
    eisen_norm    ∃ u v, u² + 3v² = p
  With Eisen: an odd prime p > 3 is u² + 3v² iff p ≡ 1 (mod 3) — the split primes of ℤ[ω] are norms, the input of
  the Hecke character of ℚ(√−3) at split primes.

  Lean 4 core only, proofs by hand (lib_nat, lib_ring, LibGcd from lib_shared), no axioms.
-/
import lang.Eisen
import lang.TwoSquares
import lib.Gcd
import lang.PrimRoot
import QRChain

namespace EisenNorm

open FTAChain ZMod YMChain Pigeon QRChain

variable (m : Nat)

theorem sq3_zero (X Y z : Zp m) (hz : (ring m).mul z z = (ring m).neg ((ring m).add (ring m).one ((ring m).add (ring m).one (ring m).one)))
    (h : X = (ring m).mul z Y) : (ring m).add ((ring m).mul X X) ((ring m).mul ((ring m).add (ring m).one ((ring m).add (ring m).one (ring m).one)) ((ring m).mul Y Y)) = (ring m).zero := by
  rw [h]
  have e : (ring m).add ((ring m).mul ((ring m).mul z Y) ((ring m).mul z Y)) ((ring m).mul ((ring m).add (ring m).one ((ring m).add (ring m).one (ring m).one)) ((ring m).mul Y Y))
      = (ring m).mul ((ring m).add ((ring m).mul z z) ((ring m).add (ring m).one ((ring m).add (ring m).one (ring m).one))) ((ring m).mul Y Y) := by lib_ring
  rw [e, hz, DetChain.add_neg', DetChain.zero_mul']

theorem three_mk : (ring m).add (ring m).one ((ring m).add (ring m).one (ring m).one) = mk m 3 := by
  show (ring m).add (mk m 1) ((ring m).add (mk m 1) (mk m 1)) = mk m 3
  rw [PrimeOneModQ.mk_add, PrimeOneModQ.mk_add]

theorem core3 (z : Zp m) (hz : (ring m).mul z z = (ring m).neg ((ring m).add (ring m).one ((ring m).add (ring m).one (ring m).one))) (k a b a' b' : Nat)
    (hk : k * k < m + 1) (ha : a ≤ k) (hb : b ≤ k) (ha' : a' ≤ k) (hb' : b' ≤ k) (hne : (a, b) ≠ (a', b'))
    (hc : (ring m).add (mk m a) ((ring m).mul z (mk m b)) = (ring m).add (mk m a') ((ring m).mul z (mk m b'))) :
    ∃ u v, Dv (m + 1) (u * u + 3 * (v * v)) ∧ 0 < u * u + 3 * (v * v) ∧ u * u + 3 * (v * v) < 4 * (m + 1) := by
  match Thue.dist_exists a a', Thue.dist_exists b' b with
  | ⟨u, hu⟩, ⟨v, hv⟩ =>
    refine ⟨u, v, ?_, ?_, ?_⟩
    · have hX := Thue.shift_eq (ring m) _ _ _ _ z hc
      have hs := sq3_zero m _ _ z hz hX
      rw [Thue.diff_sq m a a' u hu, Thue.diff_sq m b' b v hv, three_mk, TwoPower.mk_mul, TwoPower.mk_mul,
        TwoPower.mk_mul, PrimeOneModQ.mk_add] at hs
      exact md_zero_dv m _ (congrArg Subtype.val hs)
    · cases Nat.eq_zero_or_pos (u * u + 3 * (v * v)) with
      | inr h => exact h
      | inl h =>
        exfalso
        have hu0 : u * u = 0 := Nat.eq_zero_of_le_zero (h ▸ Nat.le_add_right (u * u) (3 * (v * v)))
        have hv3 : 3 * (v * v) = 0 := Nat.eq_zero_of_le_zero (h ▸ Nat.le_add_left (3 * (v * v)) (u * u))
        have hv0 : v * v = 0 := by
          cases hvv : v * v with
          | zero => rfl
          | succ w => rw [hvv] at hv3; exact Nat.noConfusion hv3
        have u0 := Thue.zero_of_sq u hu0
        have v0 := Thue.zero_of_sq v hv0
        apply hne
        have ea : a = a' := by
          cases hu with
          | inl e => rw [e, u0]; rfl
          | inr e => rw [e, u0]; rfl
        have eb : b = b' := by
          cases hv with
          | inl e => rw [e, v0]; rfl
          | inr e => rw [e, v0]; rfl
        rw [ea, eb]
    · have uk := Thue.dist_le a a' u k ha ha' hu
      have vk := Thue.dist_le b' b v k hb' hb hv
      have hu2 : u * u < m + 1 := Nat.lt_of_le_of_lt (Nat.mul_le_mul uk uk) hk
      have hv2 : v * v < m + 1 := Nat.lt_of_le_of_lt (Nat.mul_le_mul vk vk) hk
      have e1 : u * u + 3 * (v * v) = u * u + v * v + v * v + v * v := by lib_nat
      have e2 : 4 * (m + 1) = (m + 1) + (m + 1) + (m + 1) + (m + 1) := by lib_nat
      rw [e1, e2]
      exact Nat.add_lt_add (Nat.add_lt_add (Nat.add_lt_add hu2 hv2) hv2) hv2

theorem mult_cases (n : Nat) (h : Dv (m + 1) n) (h0 : 0 < n) (h4 : n < 4 * (m + 1)) :
    n = 1 * (m + 1) ∨ n = 2 * (m + 1) ∨ n = 3 * (m + 1) := by
  match h with
  | ⟨0, e⟩ => rw [e, Nat.zero_mul] at h0; exact absurd h0 (Nat.lt_irrefl 0)
  | ⟨1, e⟩ => exact Or.inl e
  | ⟨2, e⟩ => exact Or.inr (Or.inl e)
  | ⟨3, e⟩ => exact Or.inr (Or.inr e)
  | ⟨c + 4, e⟩ =>
    exfalso
    have : 4 * (m + 1) ≤ n := by
      rw [e, show (c + 4) * (m + 1) = c * (m + 1) + 4 * (m + 1) by lib_nat]
      exact Nat.le_add_left _ _
    exact Nat.lt_irrefl n (Nat.lt_of_lt_of_le h4 this)

theorem odd_of_prime (hpr : ∀ d, Dv d (m + 1) → d = 1 ∨ d = m + 1) (hm : 2 ≤ m) (x : Nat)
    (h : m + 1 = 2 * x) : False := by
  cases hpr 2 ⟨x, by rw [h]; lib_nat⟩ with
  | inl e => exact Nat.noConfusion (Nat.succ.inj e)
  | inr e =>
    have : m = 1 := Nat.succ.inj e.symm
    rw [this] at hm
    exact Nat.not_succ_le_self 1 hm

theorem par_sq (u : Nat) : par (u * u) = par u := by
  rw [par_mul]; cases par u <;> rfl

theorem case2_absurd (hpr : ∀ d, Dv d (m + 1) → d = 1 ∨ d = m + 1) (hm : 2 ≤ m) (u v : Nat)
    (h : u * u + 3 * (v * v) = 2 * (m + 1)) : False := by
  have hp : par (u * u + 3 * (v * v)) = false := by
    rw [h, par_mul]; rfl
  rw [par_add, par_sq, par_mul 3, par_sq] at hp
  have heq : par u = par v := by
    revert hp
    show bx (par u) (true && par v) = false → par u = par v
    cases par u <;> cases par v <;> intro hh <;> first | rfl | exact Bool.noConfusion hh
  cases hu : par u with
  | false =>
    have hv : par v = false := heq ▸ hu
    match (par_split u).1 hu, (par_split v).1 hv with
    | ⟨a, ea⟩, ⟨b, eb⟩ =>
      apply odd_of_prime m hpr hm (a * a + 3 * (b * b))
      apply Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1)
      rw [← h, ea, eb]; lib_nat
  | true =>
    have hv : par v = true := heq ▸ hu
    match (par_split u).2 hu, (par_split v).2 hv with
    | ⟨a, ea⟩, ⟨b, eb⟩ =>
      apply odd_of_prime m hpr hm (a * a + a + 3 * (b * b) + 3 * b + 1)
      apply Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1)
      rw [← h, ea, eb]; lib_nat

theorem prime3' : ∀ d, d ∣ 3 → d = 1 ∨ d = 3 := fun d hd => Eisen.prime3 d (PrimRoot.dv_of_dvd hd)

theorem case3 (u v : Nat) (h : u * u + 3 * (v * v) = 3 * (m + 1)) : ∃ x y, x * x + 3 * (y * y) = m + 1 := by
  have h3 : Dv (2 + 1) (3 * (v * v) + u * u) := ⟨m + 1, by rw [Nat.add_comm, h]; lib_nat⟩
  have h3v : Dv (2 + 1) (3 * (v * v)) := ⟨v * v, by lib_nat⟩
  have hu : 3 ∣ u * u := PrimRoot.dvd_of_dv (dv_sub 2 _ _ h3v h3)
  have hu' : 3 ∣ u := by
    cases LibGcd.euclid_prime prime3' hu with
    | inl x => exact x
    | inr x => exact x
  match hu' with
  | ⟨w, ew⟩ =>
    refine ⟨v, w, Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 2) ?_⟩
    rw [← h, ew]; lib_nat

/-- **p ≡ 1 (mod 3) ⇒ p = u² + 3v².** -/
theorem eisen_norm (J : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (h3 : 2 ≤ J)
    (hd : Dv (1 + 2) (J + 1)) : ∃ u v, u * u + 3 * (v * v) = J + 1 + 1 := by
  match Eisen.split_neg3 J hpr hd, TwoSquares.isqrt (J + 1) with
  | ⟨z, hz⟩, ⟨k, hk1, hk2⟩ =>
    have hk : k * k < J + 1 + 1 := Nat.lt_succ_of_le hk1
    have hlen : J + 1 + 1 < (TwoSquares.grid (k + 1) (k + 1)).length := by
      rw [TwoSquares.len_grid]
      exact Nat.lt_of_le_of_ne (Nat.succ_le_of_lt hk2) (fun e =>
        TwoSquares.not_square (J + 1) k (Nat.succ_le_succ (Nat.zero_le _)) hpr e.symm)
    match collision (fun x => md (J + 1) (x.1 + z.val * x.2)) (J + 1 + 1)
        (TwoSquares.grid (k + 1) (k + 1)) (TwoSquares.dist_grid (k + 1) (k + 1)) (fun x => md_lt _ _) hlen with
    | ⟨(a, b), (a', b'), hx, hx', hne, hf⟩ =>
      have ma := TwoSquares.mem_grid (k + 1) (a, b) (k + 1) hx
      have ma' := TwoSquares.mem_grid (k + 1) (a', b') (k + 1) hx'
      have hc : (ring (J + 1)).add (mk _ a) ((ring _).mul z (mk _ b))
          = (ring (J + 1)).add (mk _ a') ((ring _).mul z (mk _ b')) := by
        rw [← mk_val _ z, TwoPower.mk_mul, TwoPower.mk_mul, PrimeOneModQ.mk_add, PrimeOneModQ.mk_add]
        exact Subtype.ext hf
      match core3 (J + 1) z hz k a b a' b' hk (Nat.le_of_lt_succ ma.1) (Nat.le_of_lt_succ ma.2)
          (Nat.le_of_lt_succ ma'.1) (Nat.le_of_lt_succ ma'.2) hne hc with
      | ⟨u, v, hdv, h0, h4⟩ =>
        cases mult_cases (J + 1) _ hdv h0 h4 with
        | inl e => exact ⟨u, v, e.trans (Nat.one_mul _)⟩
        | inr e =>
          cases e with
          | inl e2 => exact absurd (case2_absurd (J + 1) hpr (Nat.le_succ_of_le h3) u v e2) (fun x => x)
          | inr e3 => exact case3 (J + 1) u v e3

end EisenNorm

#print axioms EisenNorm.sq3_zero
#print axioms EisenNorm.three_mk
#print axioms EisenNorm.core3
#print axioms EisenNorm.mult_cases
#print axioms EisenNorm.odd_of_prime
#print axioms EisenNorm.par_sq
#print axioms EisenNorm.case2_absurd
#print axioms EisenNorm.prime3'
#print axioms EisenNorm.case3
#print axioms EisenNorm.eisen_norm

The same grid gives \(p=u^2+2v^2\) whenever \(-2\) is a square mod \(p\): \(0<u^2+2v^2<3p\) and \(p\mid u^2+2v^2\) leave \(p\) or \(2p\), and \(u^2+2v^2=2p\) forces \(u=2u'\), \(v^2+2u'^2=p\).

Listing 35: lang/TwoNorm2.lean

lang/TwoNorm2.lean

/-
  lang/TwoNorm2 — T43, cycle 134: p = u² + 2v² when −2 is a square mod p (the norm form of ℤ[√−2]).

  z² = −2 in 𝔽_p; Thue's grid (TwoSquares, Pigeon) gives u, v with p | u² + 2v², 0 < u² + 2v² < 3p:
    sq2_zero, two_mk, core2       as EisenNorm with 3 ↦ 2
    mult2                         u² + 2v² ∈ {p, 2p}
    case2                         u² + 2v² = 2p ⇒ u = 2u' and v² + 2u'² = p
    norm2                         ∃ u v, u² + 2v² = p
  The split primes of ℚ(√−2) are norms — the GL1 input for the CM curves with CM by ℤ[√−2].

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.EisenNorm
import lang.EveryPrime
import lang.InfSplit

namespace TwoNorm2

open FTAChain ZMod YMChain Pigeon QRChain

variable (m : Nat)

theorem sq2_zero (X Y z : Zp m) (hz : (ring m).mul z z = (ring m).neg ((ring m).add (ring m).one (ring m).one))
    (h : X = (ring m).mul z Y) : (ring m).add ((ring m).mul X X) ((ring m).mul ((ring m).add (ring m).one (ring m).one) ((ring m).mul Y Y)) = (ring m).zero := by
  rw [h]
  have e : (ring m).add ((ring m).mul ((ring m).mul z Y) ((ring m).mul z Y)) ((ring m).mul ((ring m).add (ring m).one (ring m).one) ((ring m).mul Y Y))
      = (ring m).mul ((ring m).add ((ring m).mul z z) ((ring m).add (ring m).one (ring m).one)) ((ring m).mul Y Y) := by lib_ring
  rw [e, hz, DetChain.add_neg' (ring m), DetChain.zero_mul']

theorem two_mk : (ring m).add (ring m).one (ring m).one = mk m 2 := by
  show (ring m).add (mk m 1) (mk m 1) = mk m 2
  rw [PrimeOneModQ.mk_add]

theorem core2 (z : Zp m) (hz : (ring m).mul z z = (ring m).neg ((ring m).add (ring m).one (ring m).one)) (k a b a' b' : Nat)
    (hk : k * k < m + 1) (ha : a ≤ k) (hb : b ≤ k) (ha' : a' ≤ k) (hb' : b' ≤ k) (hne : (a, b) ≠ (a', b'))
    (hc : (ring m).add (mk m a) ((ring m).mul z (mk m b)) = (ring m).add (mk m a') ((ring m).mul z (mk m b'))) :
    ∃ u v, Dv (m + 1) (u * u + 2 * (v * v)) ∧ 0 < u * u + 2 * (v * v) ∧ u * u + 2 * (v * v) < 3 * (m + 1) := by
  match Thue.dist_exists a a', Thue.dist_exists b' b with
  | ⟨u, hu⟩, ⟨v, hv⟩ =>
    refine ⟨u, v, ?_, ?_, ?_⟩
    · have hX := Thue.shift_eq (ring m) _ _ _ _ z hc
      have hs := sq2_zero m _ _ z hz hX
      rw [Thue.diff_sq m a a' u hu, Thue.diff_sq m b' b v hv, two_mk, TwoPower.mk_mul, TwoPower.mk_mul,
        TwoPower.mk_mul, PrimeOneModQ.mk_add] at hs
      exact md_zero_dv m _ (congrArg Subtype.val hs)
    · cases Nat.eq_zero_or_pos (u * u + 2 * (v * v)) with
      | inr h => exact h
      | inl h =>
        exfalso
        have hu0 : u * u = 0 := Nat.eq_zero_of_le_zero (h ▸ Nat.le_add_right (u * u) (2 * (v * v)))
        have hv3 : 2 * (v * v) = 0 := Nat.eq_zero_of_le_zero (h ▸ Nat.le_add_left (2 * (v * v)) (u * u))
        have hv0 : v * v = 0 := by
          cases hvv : v * v with
          | zero => rfl
          | succ w => rw [hvv] at hv3; exact Nat.noConfusion hv3
        have u0 := Thue.zero_of_sq u hu0
        have v0 := Thue.zero_of_sq v hv0
        apply hne
        have ea : a = a' := by
          cases hu with
          | inl e => rw [e, u0]; rfl
          | inr e => rw [e, u0]; rfl
        have eb : b = b' := by
          cases hv with
          | inl e => rw [e, v0]; rfl
          | inr e => rw [e, v0]; rfl
        rw [ea, eb]
    · have uk := Thue.dist_le a a' u k ha ha' hu
      have vk := Thue.dist_le b' b v k hb' hb hv
      have hu2 : u * u < m + 1 := Nat.lt_of_le_of_lt (Nat.mul_le_mul uk uk) hk
      have hv2 : v * v < m + 1 := Nat.lt_of_le_of_lt (Nat.mul_le_mul vk vk) hk
      have e1 : u * u + 2 * (v * v) = u * u + v * v + v * v := by lib_nat
      have e2 : 3 * (m + 1) = (m + 1) + (m + 1) + (m + 1) := by lib_nat
      rw [e1, e2]
      exact Nat.add_lt_add (Nat.add_lt_add hu2 hv2) hv2

theorem mult2 (n : Nat) (h : Dv (m + 1) n) (h0 : 0 < n) (h3 : n < 3 * (m + 1)) :
    n = 1 * (m + 1) ∨ n = 2 * (m + 1) := by
  match h with
  | ⟨0, e⟩ => rw [e, Nat.zero_mul] at h0; exact absurd h0 (Nat.lt_irrefl 0)
  | ⟨1, e⟩ => exact Or.inl e
  | ⟨2, e⟩ => exact Or.inr e
  | ⟨c + 3, e⟩ =>
    exfalso
    have : 3 * (m + 1) ≤ n := by
      rw [e, show (c + 3) * (m + 1) = c * (m + 1) + 3 * (m + 1) by lib_nat]
      exact Nat.le_add_left _ _
    exact Nat.lt_irrefl n (Nat.lt_of_lt_of_le h3 this)

theorem case2 (u v : Nat) (h : u * u + 2 * (v * v) = 2 * (m + 1)) : ∃ x y, x * x + 2 * (y * y) = m + 1 := by
  cases EveryPrime.nat_even_odd u with
  | inl hu =>
    match hu with
    | ⟨u', hu'⟩ =>
      refine ⟨v, u', ?_⟩
      apply Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1)
      rw [← h, hu']; lib_nat
  | inr hu =>
    exfalso
    match hu with
    | ⟨u', hu'⟩ =>
      rw [hu'] at h
      exact InfSplit.odd_not_dv2 (2 * u' * u' + 2 * u' + v * v) (m + 1) (by rw [show (m + 1) * 2 = 2 * (m + 1) by lib_nat, ← h]; lib_nat)

/-- **p = u² + 2v² when −2 is a square mod p.** -/
theorem norm2 (J : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (z : Zp (J + 1))
    (hz : (ring (J + 1)).mul z z = (ring (J + 1)).neg ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one)) :
    ∃ u v, u * u + 2 * (v * v) = J + 1 + 1 := by
  match TwoSquares.isqrt (J + 1) with
  | ⟨k, hk1, hk2⟩ =>
    have hk : k * k < J + 1 + 1 := Nat.lt_succ_of_le hk1
    have hlen : J + 1 + 1 < (TwoSquares.grid (k + 1) (k + 1)).length := by
      rw [TwoSquares.len_grid]
      exact Nat.lt_of_le_of_ne (Nat.succ_le_of_lt hk2) (fun e =>
        TwoSquares.not_square (J + 1) k (Nat.succ_le_succ (Nat.zero_le _)) hpr e.symm)
    match collision (fun x => md (J + 1) (x.1 + z.val * x.2)) (J + 1 + 1)
        (TwoSquares.grid (k + 1) (k + 1)) (TwoSquares.dist_grid (k + 1) (k + 1)) (fun x => md_lt _ _) hlen with
    | ⟨(a, b), (a', b'), hx, hx', hne, hf⟩ =>
      have ma := TwoSquares.mem_grid (k + 1) (a, b) (k + 1) hx
      have ma' := TwoSquares.mem_grid (k + 1) (a', b') (k + 1) hx'
      have hc : (ring (J + 1)).add (mk _ a) ((ring _).mul z (mk _ b))
          = (ring (J + 1)).add (mk _ a') ((ring _).mul z (mk _ b')) := by
        rw [← mk_val _ z, TwoPower.mk_mul, TwoPower.mk_mul, PrimeOneModQ.mk_add, PrimeOneModQ.mk_add]
        exact Subtype.ext hf
      match core2 (J + 1) z hz k a b a' b' hk (Nat.le_of_lt_succ ma.1) (Nat.le_of_lt_succ ma.2)
          (Nat.le_of_lt_succ ma'.1) (Nat.le_of_lt_succ ma'.2) hne hc with
      | ⟨u, v, hdv, h0, h3⟩ =>
        cases mult2 (J + 1) _ hdv h0 h3 with
        | inl e => exact ⟨u, v, e.trans (Nat.one_mul _)⟩
        | inr e => exact case2 (J + 1) u v e

end TwoNorm2

#print axioms TwoNorm2.sq2_zero
#print axioms TwoNorm2.two_mk
#print axioms TwoNorm2.core2
#print axioms TwoNorm2.mult2
#print axioms TwoNorm2.case2
#print axioms TwoNorm2.norm2

And \(-2\) is a square for \(p\equiv 1\ (8)\) (\(-2=(iw)^2\), \(i^2=-1\), \(w^2=2\)) and \(p\equiv 3\ (8)\) (\((-2)^h=(-1)^h2^h=(-1)(-1)=1\)), so these primes are \(u^2+2v^2\).

Listing 36: lang/TwoNorm2Split.lean

lang/TwoNorm2Split.lean

/-
  lang/TwoNorm2Split — T43, cycle 135: −2 is a square mod p for p ≡ 1, 3 (mod 8), so p = u² + 2v² (Fermat).

    neg2_sq_1     p = 8b + 9: −2 = (i·w)², i² = −1 (GaussSplit), w² = 2 (TwoSupp)
    neg2_sq_3     p = 8b + 3: (−2)^((p−1)/2) = (−1)^h·2^h = (−1)(−1) = 1 (neither −1 nor 2 is a square), Euler
    fermat_1, fermat_3   p = u² + 2v²
  GL1 for ℚ(√−2): the primes p ≡ 1, 3 (mod 8) split and are norms from ℤ[√−2].

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.TwoNorm2
import lang.Inf3mod8

namespace TwoNorm2Split

open FTAChain ZMod YMChain TwoSupp

theorem neg2_sq_1 (b : Nat) (hpr : ∀ d, Dv d (2 * (2 * (2 * b + 1) + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * (2 * b + 1) + 1) + 1 + 1 + 1)) :
    ∃ z, (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul z z = (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).neg ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).one (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).one) := by
  match GaussSplit.split_one_mod_four (2 * b + 1) hpr with
  | ⟨i, hi⟩ =>
    have e : (b + 1 + (b + 1)) + ((b + 1 + (b + 1)) + 0) + ((b + 1 + (b + 1)) + ((b + 1 + (b + 1)) + 0)) + 1 = (2 * (2 * (2 * b + 1) + 1) + 1 + 1 + 1) := by lib_nat
    match two_8m1 b (by rw [e]; exact ⟨⟨_, rfl⟩, hpr⟩) with
    | ⟨w0, hw0⟩ =>
      rw [e] at hw0
      have hw := Inf3mod8.rem_sq (2 * (2 * (2 * b + 1) + 1) + 1) w0 hw0
      rw [Inf3mod8.two_eq] at hw
      refine ⟨(ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul i (mk _ w0), ?_⟩
      rw [show (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul i (mk _ w0)) ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul i (mk _ w0)) = (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul i i) ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul (mk _ w0) (mk _ w0)) by lib_ring,
        hi, hw]
      lib_ring

theorem neg2_sq_3 (b : Nat) (hpr : ∀ d, Dv d (2 * (4 * b) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (4 * b) + 1 + 1 + 1)) :
    ∃ z, (ring (2 * (4 * b) + 1 + 1)).mul z z = (ring (2 * (4 * b) + 1 + 1)).neg ((ring (2 * (4 * b) + 1 + 1)).add (ring (2 * (4 * b) + 1 + 1)).one (ring (2 * (4 * b) + 1 + 1)).one) := by
  have h2ne : (ring (2 * (4 * b) + 1 + 1)).add (ring (2 * (4 * b) + 1 + 1)).one (ring (2 * (4 * b) + 1 + 1)).one ≠ (ring (2 * (4 * b) + 1 + 1)).zero := Fp2Sqrt.two_ne (4 * b)
  have hm2 : (ring (2 * (4 * b) + 1 + 1)).neg ((ring (2 * (4 * b) + 1 + 1)).add (ring (2 * (4 * b) + 1 + 1)).one (ring (2 * (4 * b) + 1 + 1)).one) ≠ (ring (2 * (4 * b) + 1 + 1)).zero := fun e => h2ne (by
    rw [← DetChain.neg_neg' (ring (2 * (4 * b) + 1 + 1)) ((ring (2 * (4 * b) + 1 + 1)).add (ring (2 * (4 * b) + 1 + 1)).one (ring (2 * (4 * b) + 1 + 1)).one), e]; exact (DetChain.neg_unique (ring (2 * (4 * b) + 1 + 1)) _ _ ((ring (2 * (4 * b) + 1 + 1)).zero_add _)).symm)
  -- 2^h = −1
  have h2 : Frobenius.pw (ring (2 * (4 * b) + 1 + 1)) ((ring (2 * (4 * b) + 1 + 1)).add (ring (2 * (4 * b) + 1 + 1)).one (ring (2 * (4 * b) + 1 + 1)).one) (4 * b + 1) = (ring (2 * (4 * b) + 1 + 1)).neg (ring (2 * (4 * b) + 1 + 1)).one := by
    cases Euler.euler_pm (4 * b) hpr _ h2ne with
    | inr h => exact h
    | inl h =>
      exfalso
      match EulerHard.euler_hard (4 * b) hpr _ h with
      | ⟨y, hy⟩ =>
        have e : (b + b) + ((b + b) + 1) + ((b + b) + ((b + b) + 1)) + 1 = (2 * (4 * b) + 1 + 1 + 1) := by lib_nat
        apply two_8m3 b (by rw [e]; exact ⟨⟨_, rfl⟩, hpr⟩)
        refine ⟨y.val, ?_⟩
        rw [e]
        apply Inf3mod8.sq_rem (2 * (4 * b) + 1) y.val
        rw [mk_val, hy, Inf3mod8.two_eq]
  -- (−1)^h = −1
  have hm1 : Frobenius.pw (ring (2 * (4 * b) + 1 + 1)) ((ring (2 * (4 * b) + 1 + 1)).neg (ring (2 * (4 * b) + 1 + 1)).one) (4 * b + 1) = (ring (2 * (4 * b) + 1 + 1)).neg (ring (2 * (4 * b) + 1 + 1)).one := by
    rw [show 4 * b + 1 = 2 * (2 * b) + 1 by lib_nat]
    exact TwoPower.negone_odd' (2 * (4 * b) + 1) (2 * b)
  have h : Frobenius.pw (ring (2 * (4 * b) + 1 + 1)) ((ring (2 * (4 * b) + 1 + 1)).neg ((ring (2 * (4 * b) + 1 + 1)).add (ring (2 * (4 * b) + 1 + 1)).one (ring (2 * (4 * b) + 1 + 1)).one)) (4 * b + 1) = (ring (2 * (4 * b) + 1 + 1)).one := by
    rw [show (ring (2 * (4 * b) + 1 + 1)).neg ((ring (2 * (4 * b) + 1 + 1)).add (ring (2 * (4 * b) + 1 + 1)).one (ring (2 * (4 * b) + 1 + 1)).one) = (ring (2 * (4 * b) + 1 + 1)).mul ((ring (2 * (4 * b) + 1 + 1)).neg (ring (2 * (4 * b) + 1 + 1)).one) ((ring (2 * (4 * b) + 1 + 1)).add (ring (2 * (4 * b) + 1 + 1)).one (ring (2 * (4 * b) + 1 + 1)).one) by lib_ring,
      Frobenius.pw_mul, hm1, h2]
    lib_ring
  exact EulerHard.euler_hard (4 * b) hpr _ h

theorem fermat_1 (b : Nat) (hpr : ∀ d, Dv d (2 * (2 * (2 * b + 1) + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * (2 * b + 1) + 1) + 1 + 1 + 1)) : ∃ u v, u * u + 2 * (v * v) = (2 * (2 * (2 * b + 1) + 1) + 1 + 1 + 1) :=
  match neg2_sq_1 b hpr with
  | ⟨z, hz⟩ => TwoNorm2.norm2 _ hpr z hz

theorem fermat_3 (b : Nat) (hpr : ∀ d, Dv d (2 * (4 * b) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (4 * b) + 1 + 1 + 1)) : ∃ u v, u * u + 2 * (v * v) = (2 * (4 * b) + 1 + 1 + 1) :=
  match neg2_sq_3 b hpr with
  | ⟨z, hz⟩ => TwoNorm2.norm2 _ hpr z hz

end TwoNorm2Split

#print axioms TwoNorm2Split.neg2_sq_1
#print axioms TwoNorm2Split.neg2_sq_3
#print axioms TwoNorm2Split.fermat_1
#print axioms TwoNorm2Split.fermat_3

78.19 GL\(_1\): infinitely many split primes in \(\mathbb Q(i)\)

For every \(B\) there is a prime \(p\equiv 1\ (4)\) with \(p>B\). The steps are

\[ \begin {aligned} &q\ \text {a prime factor of}\ (2\cdot B!)^2+1:\ (2\cdot B!)^2\equiv -1\ (q),\\ &q\le B\Rightarrow q\mid 2\cdot B!\Rightarrow -1\equiv 0\ (q),\ \text {impossible};\qquad q=2\ \text {impossible (odd)},\\ &q\equiv 3\ (4)\Rightarrow y^2\ne -1\ \text {for all }y\ (\text {GaussSplit}),\ \text {so}\ q\equiv 1\ (4). \end {aligned} \]
So the Chebotarev section of the reciprocity shell exists for the split class of \(\mathbb Q(i)/\mathbb Q\).

Listing 37: lang/InfSplit.lean

lang/InfSplit.lean

/-
  lang/InfSplit — T43, cycle 123: infinitely many primes split in ℚ(i) (p ≡ 1 mod 4).

  For every bound B there is a prime p = 4k + 5 > B: a prime factor q of (2·B!)² + 1 has −1 = (2·B!)² a square mod q, so
  q ≡ 1 (mod 4) (GaussSplit.inert_three_mod_four), q is odd, and q > B (q ∤ B!).
    fac, dv_fac, fac_pos     B! and its divisors
    inf_split                ∀ B, ∃ k, 4k + 5 is prime and B < 4k + 5
  A special case of Dirichlet's theorem — the existence of the Chebotarev section (Recip1/Frobenius shell) for the split
  class of ℚ(i)/ℚ, so `read` is defined on it (the R8-gap of LANGLANDS_MAP for N = 4, split class).

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.EveryPrime

namespace InfSplit

open FTAChain ZMod YMChain LibInt

def fac : Nat → Nat
  | 0 => 1
  | n + 1 => (n + 1) * fac n

theorem fac_pos : ∀ B, ∃ f, fac B = f + 1
  | 0 => ⟨0, rfl⟩
  | B + 1 => match fac_pos B with
    | ⟨f, hf⟩ => ⟨B * f + B + f, by show (B + 1) * fac B = _; rw [hf]; lib_nat⟩

theorem dv_fac : ∀ B d, 1 ≤ d → d ≤ B → Dv d (fac B)
  | 0, _, h1, h2 => absurd (Nat.le_trans h1 h2) (Nat.not_succ_le_zero 0)
  | B + 1, d, h1, h2 => by
    cases Nat.lt_or_ge d (B + 1) with
    | inl h =>
      match dv_fac B d h1 (Nat.le_of_lt_succ h) with
      | ⟨q, hq⟩ => exact ⟨(B + 1) * q, by show (B + 1) * fac B = _; rw [hq]; lib_nat⟩
    | inr h =>
      have e : d = B + 1 := Nat.le_antisymm h2 h
      exact ⟨fac B, by rw [e]; show (B + 1) * fac B = fac B * (B + 1); lib_nat⟩

theorem odd_not_dv2 (x r : Nat) (h : 2 * x + 1 = r * 2) : False :=
  GaussInt.int_odd_ne_even (Int.ofNat x) (Int.ofNat r) (by
    have := congrArg Int.ofNat h
    show 2 * Int.ofNat x + 1 = 2 * Int.ofNat r
    rw [show (2 : Int) * Int.ofNat r = Int.ofNat r * 2 by lib_int]; exact this)

/-- **Infinitely many primes p ≡ 1 (mod 4).** -/
theorem inf_split (B : Nat) : ∃ k, (∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1)
    ∧ B < 2 * (2 * k + 1) + 1 + 1 + 1 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  have hn : (2 * fac B) * (2 * fac B) + 1 = (4 * f * f + 8 * f + 3) + 1 + 1 := by rw [hf]; lib_nat
  match lp_exists (4 * f * f + 8 * f + 3) with
  | ⟨l, hl, hmin⟩ =>
  have hq := lp_prime _ l hl hmin
  have hdiv : Dv (l + 1 + 1) ((2 * fac B) * (2 * fac B) + 1) := by rw [hn]; exact hl
  -- y² = −1 in ℤ/q
  have hy : (ring (l + 1)).mul (mk (l + 1) (2 * fac B)) (mk (l + 1) (2 * fac B)) = (ring (l + 1)).neg (ring (l + 1)).one := by
    apply DetChain.neg_unique
    rw [TwoPower.mk_mul, show (ring (l + 1)).one = mk (l + 1) 1 from rfl, PrimeOneModQ.mk_add]
    exact Subtype.ext (dv_md_zero (l + 1) _ hdiv)
  -- q > B
  have hbig : B < l + 1 + 1 := by
    cases Nat.lt_or_ge B (l + 1 + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      match dv_fac B (l + 1 + 1) (Nat.le_add_left 1 _) h with
      | ⟨r, hr⟩ =>
        have h0 : mk (l + 1) (2 * fac B) = (ring (l + 1)).zero :=
          Subtype.ext (dv_md_zero (l + 1) _ ⟨2 * r, by rw [hr]; lib_nat⟩)
        rw [h0, DetChain.mul_zero] at hy
        apply PowerSum.one_ne_zero' l
        rw [← DetChain.neg_neg' (ring (l + 1)) (ring (l + 1)).one, ← hy]
        exact (DetChain.neg_unique (ring (l + 1)) _ _ ((ring (l + 1)).zero_add _)).symm
  cases EveryPrime.nat_even_odd l with
  | inl h =>
    exfalso
    match h with
    | ⟨c, hc⟩ =>
      cases hq 2 ⟨c + 1, by rw [hc]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e =>
        have e' : (0 : Nat) + 1 + 1 = 2 * c + 1 + 1 := by rw [← hc]; exact e
        have e0 : 0 = 2 * c := Nat.succ.inj (Nat.succ.inj e')
        have hc0 : c = 0 := by
          cases c with
          | zero => rfl
          | succ c' => exact absurd e0 (fun h => Nat.noConfusion (show 0 = Nat.succ (2 * c' + 1) from h))
        rw [hc, hc0] at hdiv
        match hdiv with
        | ⟨r, hr⟩ => exact odd_not_dv2 (2 * fac B * fac B) r (by rw [← hr]; lib_nat)
  | inr h =>
    match h with
    | ⟨c, hc⟩ =>
      cases EveryPrime.nat_even_odd c with
      | inl h' =>
        exfalso
        match h' with
        | ⟨k, hk⟩ =>
          subst hc; subst hk
          exact GaussSplit.inert_three_mod_four k hq _ hy
      | inr h' =>
        match h' with
        | ⟨k, hk⟩ =>
          subst hc; subst hk
          exact ⟨k, hq, hbig⟩

end InfSplit

#print axioms InfSplit.fac_pos
#print axioms InfSplit.dv_fac
#print axioms InfSplit.odd_not_dv2
#print axioms InfSplit.inf_split

For every prime \(\ell \) and every \(B\) there is a prime \(p>B\) with \(\ell \mid p-1\): with \(M=\ell \cdot B!\), a prime factor \(p\) of \(1+M+\dots +M^{\ell -1}\) has \(M^\ell \equiv 1\), \(M\not \equiv 1\) (\(p\nmid \ell \)), so \(M\) has order \(\ell \) in \(\mathbb F_p^\times \), and \(p\nmid M\) gives \(p>B\).

Listing 38: lang/InfCyclo.lean

lang/InfCyclo.lean

/-
  lang/InfCyclo — T43, cycle 124: for every prime ℓ, infinitely many primes p ≡ 1 (mod ℓ).

  For a bound B, M = ℓ·B! and N = 1 + M + … + M^(ℓ−1): a prime factor p of N has M of order ℓ in 𝔽_p (M^ℓ ≡ 1, M ≢ 1 since
  p ∤ ℓ), so ℓ | p − 1 (as PrimeOneModQ, base M instead of ℓ), and p > B since p ∤ M.
    inf_one_mod_q    ℓ prime, B ⇒ ∃ prime p > B with ℓ | p − 1
  Dirichlet for the class 1 mod ℓ: infinitely many primes split completely in ℚ(ζ_ℓ), so the Chebotarev section of the
  reciprocity shell exists for the trivial class of every prime cyclotomic field.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.PrimeOneModQ
import lang.InfSplit

namespace InfCyclo

open FTAChain ZMod TwoPower Order PrimeOneModQ InfSplit

theorem inf_one_mod_q (t : Nat) (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) (B : Nat) :
    ∃ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) ∧ Dv (t + 1 + 1) (l + 1) ∧ B < l + 1 + 1 := by
  match InfSplit.fac_pos B with
  | ⟨f, hf⟩ =>
  have hMq : (t + 1 + 1) * fac B = (t * f + 2 * f + t + 1) + 1 := by rw [hf]; lib_nat
  generalize hMdef : (t + 1 + 1) * fac B = M at hMq
  -- N = gs q q = 1 + q · M, M = gs q (t+1) = M' + 1
  obtain ⟨r, hr⟩ := pow_succ_form (t * f + 2 * f + t + 1) t
  have hM : gs M (t + 1) = (gs M t + r) + 1 := by
    show gs M t + M ^ t = _
    rw [hMq, hr]
    rfl
  have hN : gs M (t + 1 + 1) = 1 + M * ((gs M t + r) + 1) := by
    rw [geomN, hM]
  -- N = i + 2 with i = q (M' ) + t + 1
  have hNi : gs M (t + 1 + 1) = (M * (gs M t + r) + (t * f + 2 * f + t + 1)) + 1 + 1 := by
    rw [hN]
    generalize gs M t + r = G
    rw [hMq]; lib_nat
  obtain ⟨l, hl, hmin⟩ := lp_exists (M * (gs M t + r) + (t * f + 2 * f + t + 1))
  rw [← hNi] at hl hmin
  have hpr := lp_prime _ l hl hmin
  have hbig : B < l + 1 + 1 := by
    cases Nat.lt_or_ge B (l + 1 + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      match InfSplit.dv_fac B (l + 1 + 1) (Nat.le_add_left 1 _) h with
      | ⟨r', hr'⟩ =>
        have hdN : Dv (l + 1 + 1) (1 + M * ((gs M t + r) + 1)) := by rw [← hN]; exact hl
        have hdqM : Dv (l + 1 + 1) (M * ((gs M t + r) + 1)) := by
          generalize (gs M t + r) + 1 = Y; exact ⟨(t + 1 + 1) * r' * Y, by rw [← hMdef, hr']; lib_nat⟩
        rw [Nat.add_comm 1 (M * ((gs M t + r) + 1))] at hdN
        exact dv_one_absurd l (dv_sub (l + 1) _ 1 hdqM hdN)
  refine ⟨l, hpr, ?_, hbig⟩
  -- in ℤ/p, x = residue of q
  let K := ZMod.ring (l + 1)
  let x := mk (l + 1) M
  have hS : YMChain.sumK K (Frobenius.pw K x) (LinkIntegralZN.rng (t + 1 + 1)) = K.zero := by
    rw [mk_geom]; exact Subtype.ext (dv_md_zero (l + 1) _ hl)
  have hxq : Frobenius.pw K x (t + 1 + 1) = K.one := by
    have g := geom_ring K x (t + 1 + 1)
    rw [hS, DetChain.mul_zero] at g
    have := congrArg (fun z => K.add z K.one) g.symm
    have h' : K.add (K.add (Frobenius.pw K x (t + 1 + 1)) (K.neg K.one)) K.one = K.add K.zero K.one := this
    rw [K.add_assoc, DetChain.add_neg' K K.one, DetChain.add_zero', K.zero_add] at h'
    exact h'
  -- p ≠ q: p | N = 1 + q M and p | q would give p | 1
  have hx1 : x ≠ K.one := fun e => by
    have hS1 : YMChain.sumK K (Frobenius.pw K K.one) (LinkIntegralZN.rng (t + 1 + 1)) = K.zero := by
      rw [← e]; exact hS
    -- Σ_{i<q} 1 = mk q
    have hq1 : YMChain.sumK K (Frobenius.pw K K.one) (LinkIntegralZN.rng (t + 1 + 1)) = mk (l + 1) (t + 1 + 1) := by
      have := mk_geom (l + 1) 1 (t + 1 + 1)
      have one_eq : mk (l + 1) 1 = K.one := rfl
      rw [one_eq] at this
      rw [this]
      exact congrArg (mk (l + 1)) (gs_one (t + 1 + 1))
    rw [hq1] at hS1
    have hdq : Dv (l + 1 + 1) (t + 1 + 1) := md_zero_dv (l + 1) _ (congrArg Subtype.val hS1)
    cases hq (l + 1 + 1) hdq with
    | inl h1 => exact Nat.noConfusion (Nat.succ.inj h1)
    | inr hpq =>
      -- p = q divides N − 1 = q M, and N
      have hdN : Dv (l + 1 + 1) (1 + M * ((gs M t + r) + 1)) := by rw [← hN]; exact hl
      have hdqM : Dv (l + 1 + 1) (M * ((gs M t + r) + 1)) := by
        rw [hpq]; generalize (gs M t + r) + 1 = Y; exact ⟨fac B * Y, by rw [← hMdef]; lib_nat⟩
      rw [Nat.add_comm 1 (M * ((gs M t + r) + 1))] at hdN
      exact dv_one_absurd l (dv_sub (l + 1) _ 1 hdqM hdN)
  -- order of x is q
  obtain ⟨d, hd, hdmin⟩ := ord_exists K x (t + 1) hxq
  have hdq : Dv (d + 1) (t + 1 + 1) := ord_dvd K x d hd hdmin (t + 1 + 1) hxq
  have hd' : d + 1 = t + 1 + 1 := by
    cases hq (d + 1) hdq with
    | inr h => exact h
    | inl h =>
      have h0 : d = 0 := Nat.succ.inj h
      rw [h0] at hd
      exact absurd (by have := hd; show x = K.one; rw [← K.one_mul x]; exact this) hx1
  -- x ≠ 0, so x^(p−1) = 1, and the order divides p − 1
  have hx0 : x ≠ K.zero := fun e => by
    rw [e] at hxq
    have h0 : Frobenius.pw K K.zero (t + 1 + 1) = K.zero := DetChain.mul_zero K _
    rw [h0] at hxq
    have := congrArg Subtype.val hxq
    rw [one_val l] at this
    exact Nat.noConfusion this
  have hp1 := ZMod.unit_pow l hpr x hx0
  have := ord_dvd K x d hd hdmin (l + 1) hp1
  rw [hd'] at this
  exact this

end InfCyclo

#print axioms InfCyclo.inf_one_mod_q

For a prime power, the base \(M=(\ell \cdot B!)^{\ell ^{e-1}}\) has order \(\ell \) modulo a prime factor \(p\) of \(1+M+\dots +M^{\ell -1}\), so \(y=\ell \cdot B!\) has \(y^{\ell ^e}\equiv 1\), \(y^{\ell ^{e-1}}\not \equiv 1\), i.e. order \(\ell ^e\), and \(\ell ^e\mid p-1\).

Listing 39: lang/InfCycloPow.lean

lang/InfCycloPow.lean

/-
  lang/InfCycloPow — T43, cycle 127: for every prime power ℓ^e, infinitely many primes p ≡ 1 (mod ℓ^e).

    base_order     for M ≥ 1 with ℓ | M and every d ≤ B dividing M: some prime p > B has M of order ℓ in 𝔽_p
                   (a prime factor of 1 + M + … + M^(ℓ−1), as InfCyclo)
    inf_pow        ℓ prime, s, B ⇒ ∃ prime p > B with ℓ^(s+1) | p − 1      (M = (ℓ·B!)^(ℓ^s), y = ℓ·B!: y^(ℓ^s) = M ≠ 1, y^(ℓ^(s+1)) = 1,
                                                                            CycloPow.dvd_of_pow)
  Dirichlet for the trivial class of every prime-power cyclotomic field ℚ(ζ_{ℓ^e}).

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.InfCyclo
import lang.CycloPow

namespace InfCycloPow

open FTAChain ZMod TwoPower Order PrimeOneModQ InfSplit

theorem base_order (t : Nat) (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) (B M m : Nat) (hMq : M = m + 1)
    (hdvl : Dv (t + 1 + 1) M) (hdvB : ∀ d, 1 ≤ d → d ≤ B → Dv d M) :
    ∃ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) ∧ B < l + 1 + 1 ∧
      Frobenius.pw (ZMod.ring (l + 1)) (mk (l + 1) M) (t + 1 + 1) = (ZMod.ring (l + 1)).one ∧
      mk (l + 1) M ≠ (ZMod.ring (l + 1)).one := by
  -- N = gs q q = 1 + q · M, M = gs q (t+1) = M' + 1
  obtain ⟨r, hr⟩ := pow_succ_form m t
  have hM : gs M (t + 1) = (gs M t + r) + 1 := by
    show gs M t + M ^ t = _
    rw [hMq, hr]
    rfl
  have hN : gs M (t + 1 + 1) = 1 + M * ((gs M t + r) + 1) := by
    rw [geomN, hM]
  -- N = i + 2 with i = q (M' ) + t + 1
  have hNi : gs M (t + 1 + 1) = (M * (gs M t + r) + m) + 1 + 1 := by
    rw [hN]
    generalize gs M t + r = G
    rw [hMq]; lib_nat
  obtain ⟨l, hl, hmin⟩ := lp_exists (M * (gs M t + r) + m)
  rw [← hNi] at hl hmin
  have hpr := lp_prime _ l hl hmin
  have hbig : B < l + 1 + 1 := by
    cases Nat.lt_or_ge B (l + 1 + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      match hdvB (l + 1 + 1) (Nat.le_add_left 1 _) h with
      | ⟨r', hr'⟩ =>
        have hdN : Dv (l + 1 + 1) (1 + M * ((gs M t + r) + 1)) := by rw [← hN]; exact hl
        have hdqM : Dv (l + 1 + 1) (M * ((gs M t + r) + 1)) := by
          generalize (gs M t + r) + 1 = Y; exact ⟨r' * Y, by rw [hr']; lib_nat⟩
        rw [Nat.add_comm 1 (M * ((gs M t + r) + 1))] at hdN
        exact dv_one_absurd l (dv_sub (l + 1) _ 1 hdqM hdN)
  refine ⟨l, hpr, hbig, ?_⟩
  -- in ℤ/p, x = residue of q
  let K := ZMod.ring (l + 1)
  let x := mk (l + 1) M
  have hS : YMChain.sumK K (Frobenius.pw K x) (LinkIntegralZN.rng (t + 1 + 1)) = K.zero := by
    rw [mk_geom]; exact Subtype.ext (dv_md_zero (l + 1) _ hl)
  have hxq : Frobenius.pw K x (t + 1 + 1) = K.one := by
    have g := geom_ring K x (t + 1 + 1)
    rw [hS, DetChain.mul_zero] at g
    have := congrArg (fun z => K.add z K.one) g.symm
    have h' : K.add (K.add (Frobenius.pw K x (t + 1 + 1)) (K.neg K.one)) K.one = K.add K.zero K.one := this
    rw [K.add_assoc, DetChain.add_neg' K K.one, DetChain.add_zero', K.zero_add] at h'
    exact h'
  -- p ≠ q: p | N = 1 + q M and p | q would give p | 1
  have hx1 : x ≠ K.one := fun e => by
    have hS1 : YMChain.sumK K (Frobenius.pw K K.one) (LinkIntegralZN.rng (t + 1 + 1)) = K.zero := by
      rw [← e]; exact hS
    -- Σ_{i<q} 1 = mk q
    have hq1 : YMChain.sumK K (Frobenius.pw K K.one) (LinkIntegralZN.rng (t + 1 + 1)) = mk (l + 1) (t + 1 + 1) := by
      have := mk_geom (l + 1) 1 (t + 1 + 1)
      have one_eq : mk (l + 1) 1 = K.one := rfl
      rw [one_eq] at this
      rw [this]
      exact congrArg (mk (l + 1)) (gs_one (t + 1 + 1))
    rw [hq1] at hS1
    have hdq : Dv (l + 1 + 1) (t + 1 + 1) := md_zero_dv (l + 1) _ (congrArg Subtype.val hS1)
    cases hq (l + 1 + 1) hdq with
    | inl h1 => exact Nat.noConfusion (Nat.succ.inj h1)
    | inr hpq =>
      -- p = q divides N − 1 = q M, and N
      have hdN : Dv (l + 1 + 1) (1 + M * ((gs M t + r) + 1)) := by rw [← hN]; exact hl
      have hdqM : Dv (l + 1 + 1) (M * ((gs M t + r) + 1)) := by
        rw [hpq]; match hdvl with
          | ⟨c, hc⟩ => generalize (gs M t + r) + 1 = Y; exact ⟨c * Y, by rw [hc]; lib_nat⟩
      rw [Nat.add_comm 1 (M * ((gs M t + r) + 1))] at hdN
      exact dv_one_absurd l (dv_sub (l + 1) _ 1 hdqM hdN)
  exact ⟨hxq, hx1⟩

/-- **Infinitely many primes p ≡ 1 (mod ℓ^(s+1)).** -/
theorem inf_pow (t : Nat) (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1) (s B : Nat) :
    ∃ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) ∧ Dv ((t + 1 + 1) ^ (s + 1)) (l + 1) ∧ B < l + 1 + 1 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  have hA : (t + 1 + 1) * fac B = (t * f + 2 * f + t + 1) + 1 := by rw [hf]; lib_nat
  generalize hAdef : (t + 1 + 1) * fac B = A at hA
  match pow_succ_form (t + 1) s with
  | ⟨e, he⟩ =>
  match pow_succ_form (t * f + 2 * f + t + 1) (e + 1) with
  | ⟨m, hm⟩ =>
  have hM : A ^ ((t + 1 + 1) ^ s) = m + 1 := by rw [he, hA]; exact hm
  have hdvA : ∀ d, Dv d A → Dv d (A ^ ((t + 1 + 1) ^ s)) := fun d ⟨c, hc⟩ => by
    rw [he]
    exact ⟨c * A ^ e, by show A ^ e * A = _; rw [hc]; lib_nat⟩
  match base_order t hq B (A ^ ((t + 1 + 1) ^ s)) m hM
      (hdvA _ ⟨fac B, by rw [← hAdef]; lib_nat⟩)
      (fun d h1 h2 => hdvA d (match dv_fac B d h1 h2 with | ⟨c, hc⟩ => ⟨(t + 1 + 1) * c, by rw [← hAdef, hc]; lib_nat⟩)) with
  | ⟨l, hpr, hbig, hxq, hx1⟩ =>
    refine ⟨l, hpr, ?_, hbig⟩
    have hy : Frobenius.pw (ZMod.ring (l + 1)) (mk (l + 1) A) ((t + 1 + 1) ^ s) = mk (l + 1) (A ^ ((t + 1 + 1) ^ s)) := mk_pow _ _ _
    apply CycloPow.dvd_of_pow l hpr t s hq (mk (l + 1) A)
    · show Frobenius.pw (ZMod.ring (l + 1)) (mk (l + 1) A) ((t + 1 + 1) ^ s * (t + 1 + 1)) = _
      rw [← TwoPower.pwpow, hy]; exact hxq
    · rw [hy]; exact hx1

end InfCycloPow

#print axioms InfCycloPow.base_order
#print axioms InfCycloPow.inf_pow

Also \(p\equiv 7\ (8)\): every prime factor \(q\) of \(8(B!)^2-1\) has \(2\equiv (4\cdot B!)^2\) a square, so \(q\equiv \pm 1\ (8)\) (the splitting law of \(\mathbb Q(\sqrt 2)\)), and the product is \(\equiv 7\), so some \(q\equiv 7\ (8)\). With \(p\equiv 1\ (8)\) both residues of the split primes of \(\mathbb Q(\sqrt 2)\) occur infinitely often.

Listing 40: lang/Inf7mod8.lean

lang/Inf7mod8.lean

/-
  lang/Inf7mod8 — T43, cycle 128: infinitely many primes p ≡ 7 (mod 8).

  N = 8·(B!)² − 1 ≡ 7 (mod 8). For every prime factor q of N, 2·(2·B!)² ≡ 1, so 2 ≡ (4·B!)² is a square mod q and
  q ≡ ±1 (mod 8) (TwoSupp: 2 is not a square at 8m + 3, 8m + 5). A product of primes ≡ 1 (mod 8) is ≡ 1, so some factor is
  ≡ 7; and q ∤ B! gives q > B.
    uniq_rem8, cofactor8     residues mod 8
    allowed8                 q | N ⇒ q ≡ 1 or 7 (mod 8)
    has7                     a factor ≡ 7 (mod 8) of every divisor N ≡ 7 of N₀
    inf_7mod8                ∀ B, ∃ prime p = 8b + 7 > B
  With InfCycloPow (ℓ^e = 8) both residues ±1 mod 8 of the split primes of ℚ(√2) occur infinitely often.

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.InfInert3
import lang.TwoSupp

namespace Inf7mod8

open FTAChain ZMod LibInt InfSplit TwoSupp

theorem uniq_rem8 (x y r c : Nat) (hr : r < 8) (hc : c < 8) (h : x * 8 + r = y * 8 + c) : r = c := by
  have big : ∀ x y r c : Nat, r < 8 → x < y → x * 8 + r = y * 8 + c → False := fun x y r c hr hxy h => by
    have h1 : x * 8 + r < (x + 1) * 8 := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * 8 ≤ y * 8 := Nat.mul_le_mul_right 8 hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * 8 + Int.ofNat r = Int.ofNat x * 8 + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * 8 + Int.ofNat r) - Int.ofNat x * 8 := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

/-- residue of n mod 8 as a case split. -/
theorem mod8 (n : Nat) : ∃ a r, n = a * 8 + r ∧ r < 8 :=
  match div_exists 7 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (8 : Nat) = 7 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor8 (b N' a : Nat) (h : (b * 8 + 1) * N' = a * 8 + 7) : ∃ c, N' = c * 8 + 7 :=
  match mod8 N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 8 + 1) * N' = (b * 8 * c + b * r + c) * 8 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_rem8 _ _ r 7 hr (Nat.lt_succ_self 7) h]⟩

/-- Every prime factor of N₀ = 8F² + 16F + 7 = 8(F + 1)² − 1 is ≡ 1 or 7 (mod 8). -/
theorem allowed8 (F l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (8 * F * F + 16 * F + 7)) : ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 7) := by
  have hPr : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, hq⟩
  -- 2 is a square mod q: (4(F+1))² = 2 + 2 N₀
  have hsq : rem (l + 1 + 1) ((4 * (F + 1)) * (4 * (F + 1))) = rem (l + 1 + 1) 2 := by
    match hdv with
    | ⟨r, hr⟩ =>
      rw [show (4 * (F + 1)) * (4 * (F + 1)) = (2 * r) * (l + 1 + 1) + 2 by rw [show (2 * r) * (l + 1 + 1) = 2 * (r * (l + 1 + 1)) by lib_nat, ← hr]; lib_nat]
      exact rem_add_mul (l + 1) 2 (2 * r)
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨c, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    match hdv with
    | ⟨r, hr⟩ => exact odd_not_dv2 (4 * F * F + 8 * F + 3) r (by rw [← hr]; lib_nat)
  match mod8 (l + 1 + 1) with
  | ⟨b, r, hb, hr⟩ =>
    match r, hr with
    | 0, _ => exact absurd hb (fun e => hodd (4 * b) (by rw [e]; lib_nat))
    | 1, _ => exact ⟨b, 1, hb, Or.inl rfl⟩
    | 2, _ => exact absurd hb (fun e => hodd (4 * b + 1) (by rw [e]; lib_nat))
    | 3, _ =>
      exfalso
      have e : (b + b) + ((b + b) + 1) + ((b + b) + ((b + b) + 1)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
      exact two_8m3 b (by rw [e]; exact hPr) ⟨4 * (F + 1), by rw [e]; exact hsq⟩
    | 4, _ => exact absurd hb (fun e => hodd (4 * b + 2) (by rw [e]; lib_nat))
    | 5, _ =>
      exfalso
      have e : (b + b + 1) + ((b + b + 1) + 0) + ((b + b + 1) + ((b + b + 1) + 0)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
      exact two_8m5 b (by rw [e]; exact hPr) ⟨4 * (F + 1), by rw [e]; exact hsq⟩
    | 6, _ => exact absurd hb (fun e => hodd (4 * b + 3) (by rw [e]; lib_nat))
    | 7, _ => exact ⟨b, 7, hb, Or.inr rfl⟩
    | r + 8, hr => exact absurd hr (fun h => Nat.lt_irrefl 8 (Nat.lt_of_le_of_lt (Nat.le_add_left 8 r) h))

/-- **A factor ≡ 7 (mod 8).** -/
theorem has7 (F : Nat) : ∀ G N, N ≤ G → Dv N (8 * F * F + 16 * F + 7) → (∃ a, N = a * 8 + 7) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 8 + 7) ∧ Dv q N
  | 0, N, hN, _, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 8 + 5) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    match allowed8 F l hq (dv_trans _ _ _ hlN hN0) with
    | ⟨b, r, hb, hr⟩ =>
      cases hr with
      | inr h7 => exact ⟨l + 1 + 1, hq, ⟨b, by rw [hb, h7]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor8 b N' a (by rw [← h1, ← hb, ← ha, hN']; lib_nat) with
        | ⟨c, hc⟩ =>
        have hlt : N' ≤ G := by
          have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hN))
        have hN'0 : Dv N' (8 * F * F + 16 * F + 7) := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has7 F G N' hlt hN'0 ⟨c, hc⟩ with
        | ⟨q, hq', hq7, hqN⟩ => exact ⟨q, hq', hq7, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- **Infinitely many primes p ≡ 7 (mod 8).** -/
theorem inf_7mod8 (B : Nat) : ∃ b, (∀ d, Dv d (b * 8 + 7) → d = 1 ∨ d = b * 8 + 7) ∧ B < b * 8 + 7 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  match has7 f (8 * f * f + 16 * f + 7) (8 * f * f + 16 * f + 7) (Nat.le_refl _) ⟨1, by lib_nat⟩ ⟨f * f + 2 * f, by lib_nat⟩ with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      match dv_fac B q (by rw [hb]; exact Nat.le_add_left 1 _) h with
      | ⟨r, hr⟩ =>
        have h8 : Dv q (8 * f * f + 16 * f + 7 + 1) := ⟨8 * r * (f + 1), by
          rw [show 8 * f * f + 16 * f + 7 + 1 = 8 * (f + 1) * (f + 1) by lib_nat, ← hf, hr]; lib_nat⟩
        have hq2 : q = (b * 8 + 6) + 1 := hb
        rw [hq2] at hqN h8
        exact dv_one_absurd (b * 8 + 5) (dv_sub (b * 8 + 6) _ 1 hqN h8)

end Inf7mod8

#print axioms Inf7mod8.uniq_rem8
#print axioms Inf7mod8.mod8
#print axioms Inf7mod8.cofactor8
#print axioms Inf7mod8.allowed8
#print axioms Inf7mod8.has7
#print axioms Inf7mod8.inf_7mod8

And \(p\equiv 5\ (8)\): with \(x\) the product of the odd numbers \(\le B\), every prime factor of \(4x^2+1\equiv 5\ (8)\) is \(\equiv 1\ (4)\), hence \(\equiv 1\) or \(5\ (8)\), and one of them is \(\equiv 5\).

Listing 41: lang/Inf5mod8.lean

lang/Inf5mod8.lean

/-
  lang/Inf5mod8 — T43, cycle 129: infinitely many primes p ≡ 5 (mod 8).

  x = product of the odd numbers ≤ B (odd, divisible by every odd d ≤ B), N = 4x² + 1 ≡ 5 (mod 8). Every prime factor q of
  N has −1 ≡ (2x)² a square, so q ≡ 1 (mod 4) (GaussSplit), i.e. q ≡ 1 or 5 (mod 8); the product is ≡ 5, so some factor is
  ≡ 5; odd q ≤ B divides x and cannot divide N.
    ofac, ofac_odd, dv_ofac     the odd factorial
    allowed5, has5, inf_5mod8
  The residue 5 mod 8 is the class of the inert primes of ℚ(√2) that split in ℚ(i).

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.Inf7mod8

namespace Inf5mod8

open FTAChain ZMod LibInt InfSplit Inf7mod8 QRChain

def ofac : Nat → Nat
  | 0 => 1
  | n + 1 => if par (n + 1) = true then (n + 1) * ofac n else ofac n

theorem par_odd (c : Nat) : par (2 * c + 1) = true := by
  show (!par (2 * c)) = true
  rw [show 2 * c = c + c by lib_nat, par_double]; rfl

theorem par_even (c : Nat) : par (2 * c) = false := by rw [show 2 * c = c + c by lib_nat, par_double]

theorem ofac_odd : ∀ n, ∃ y, ofac n = 2 * y + 1
  | 0 => ⟨0, rfl⟩
  | n + 1 => by
    match ofac_odd n with
    | ⟨y, hy⟩ =>
    cases EveryPrime.nat_even_odd (n + 1) with
    | inl h =>
      match h with
      | ⟨c, hc⟩ =>
        exact ⟨y, by show (if par (n + 1) = true then (n + 1) * ofac n else ofac n) = _; rw [hc, par_even]; exact hy⟩
    | inr h =>
      match h with
      | ⟨c, hc⟩ =>
        refine ⟨2 * c * y + c + y, ?_⟩
        show (if par (n + 1) = true then (n + 1) * ofac n else ofac n) = _
        rw [hc, par_odd, FiniteRecChain.ite_pos' _ _ rfl, hy]; lib_nat

theorem dv_ofac : ∀ n c, 2 * c + 1 ≤ n → Dv (2 * c + 1) (ofac n)
  | 0, _, h => absurd h (Nat.not_succ_le_zero _)
  | n + 1, c, h => by
    show Dv (2 * c + 1) (if par (n + 1) = true then (n + 1) * ofac n else ofac n)
    cases Nat.lt_or_ge (2 * c + 1) (n + 1) with
    | inl hlt =>
      have ih := dv_ofac n c (Nat.le_of_lt_succ hlt)
      cases EveryPrime.nat_even_odd (n + 1) with
      | inl he => match he with
        | ⟨e, he⟩ => rw [he, par_even, FiniteRecChain.ite_neg' _ _ (fun h => Bool.noConfusion h)]; exact ih
      | inr ho => match ho with
        | ⟨e, he⟩ =>
          rw [he, par_odd, FiniteRecChain.ite_pos' _ _ rfl]
          match ih with
          | ⟨q, hq⟩ => exact ⟨(2 * e + 1) * q, by rw [hq]; lib_nat⟩
    | inr hge =>
      have e : 2 * c + 1 = n + 1 := Nat.le_antisymm h hge
      rw [← e, par_odd, FiniteRecChain.ite_pos' _ _ rfl]
      exact ⟨ofac n, by lib_nat⟩

theorem cofactor5 (b N' a : Nat) (h : (b * 8 + 1) * N' = a * 8 + 5) : ∃ c, N' = c * 8 + 5 :=
  match mod8 N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 8 + 1) * N' = (b * 8 * c + b * r + c) * 8 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_rem8 _ _ r 5 hr (by exact Nat.lt_of_lt_of_le (Nat.lt_succ_self 5) (Nat.le_succ_of_le (Nat.le_succ 6))) h]⟩

/-- prime factors of 4x² + 1 are ≡ 1 or 5 (mod 8). -/
theorem allowed5 (x l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (4 * x * x + 1)) : ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 5) := by
  have hy : (ring (l + 1)).mul (mk (l + 1) (2 * x)) (mk (l + 1) (2 * x)) = (ring (l + 1)).neg (ring (l + 1)).one := by
    apply DetChain.neg_unique
    rw [TwoPower.mk_mul, show (ring (l + 1)).one = mk (l + 1) 1 from rfl, PrimeOneModQ.mk_add,
      show 2 * x * (2 * x) + 1 = 4 * x * x + 1 by lib_nat]
    exact Subtype.ext (dv_md_zero (l + 1) _ hdv)
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨c, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    match hdv with
    | ⟨r, hr⟩ => exact odd_not_dv2 (2 * x * x) r (by rw [← hr]; lib_nat)
  have h34 : ∀ k, l + 1 + 1 = 2 * (2 * k) + 1 + 1 + 1 → False := fun k hk => by
    have hl : l = 2 * (2 * k) + 1 := Nat.succ.inj (Nat.succ.inj (hk.trans (by lib_nat)))
    subst hl
    exact GaussSplit.inert_three_mod_four k hq _ hy
  match mod8 (l + 1 + 1) with
  | ⟨b, r, hb, hr⟩ =>
    match r, hr with
    | 0, _ => exact absurd hb (fun e => hodd (4 * b) (by rw [e]; lib_nat))
    | 1, _ => exact ⟨b, 1, hb, Or.inl rfl⟩
    | 2, _ => exact absurd hb (fun e => hodd (4 * b + 1) (by rw [e]; lib_nat))
    | 3, _ => exact absurd hb (fun e => h34 (2 * b) (by rw [e]; lib_nat))
    | 4, _ => exact absurd hb (fun e => hodd (4 * b + 2) (by rw [e]; lib_nat))
    | 5, _ => exact ⟨b, 5, hb, Or.inr rfl⟩
    | 6, _ => exact absurd hb (fun e => hodd (4 * b + 3) (by rw [e]; lib_nat))
    | 7, _ => exact absurd hb (fun e => h34 (2 * b + 1) (by rw [e]; lib_nat))
    | r + 8, hr => exact absurd hr (fun h => Nat.lt_irrefl 8 (Nat.lt_of_le_of_lt (Nat.le_add_left 8 r) h))

theorem has5 (x : Nat) : ∀ G N, N ≤ G → Dv N (4 * x * x + 1) → (∃ a, N = a * 8 + 5) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 8 + 5) ∧ Dv q N
  | 0, N, hN, _, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 8 + 3) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    match allowed5 x l hq (dv_trans _ _ _ hlN hN0) with
    | ⟨b, r, hb, hr⟩ =>
      cases hr with
      | inr h5 => exact ⟨l + 1 + 1, hq, ⟨b, by rw [hb, h5]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor5 b N' a (by rw [← h1, ← hb, ← ha, hN']; lib_nat) with
        | ⟨c, hc⟩ =>
        have hlt : N' ≤ G := by
          have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hN))
        have hN'0 : Dv N' (4 * x * x + 1) := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has5 x G N' hlt hN'0 ⟨c, hc⟩ with
        | ⟨q, hq', hq5, hqN⟩ => exact ⟨q, hq', hq5, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- **Infinitely many primes p ≡ 5 (mod 8).** -/
theorem inf_5mod8 (B : Nat) : ∃ b, (∀ d, Dv d (b * 8 + 5) → d = 1 ∨ d = b * 8 + 5) ∧ B < b * 8 + 5 := by
  match ofac_odd B with
  | ⟨y, hy⟩ =>
  have hN : 4 * ofac B * ofac B + 1 = (2 * y * y + 2 * y) * 8 + 5 := by rw [hy]; lib_nat
  match has5 (ofac B) _ _ (Nat.le_refl _) ⟨1, by lib_nat⟩ ⟨2 * y * y + 2 * y, hN⟩ with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      -- q is odd: q = 2(4b + 2) + 1
      match dv_ofac B (4 * b + 2) (by rw [show 2 * (4 * b + 2) + 1 = b * 8 + 5 by lib_nat, ← hb]; exact h) with
      | ⟨r, hr⟩ =>
        have hr' : ofac B = r * q := by rw [hr, hb]; lib_nat
        have h1 : Dv q (4 * ofac B * ofac B) := ⟨4 * r * ofac B, by rw [hr']; lib_nat⟩
        have hq2 : q = (b * 8 + 4) + 1 := hb
        rw [hq2] at hqN h1
        exact dv_one_absurd (b * 8 + 3) (dv_sub (b * 8 + 4) _ 1 h1 hqN)

end Inf5mod8

#print axioms Inf5mod8.par_odd
#print axioms Inf5mod8.par_even
#print axioms Inf5mod8.ofac_odd
#print axioms Inf5mod8.dv_ofac
#print axioms Inf5mod8.cofactor5
#print axioms Inf5mod8.allowed5
#print axioms Inf5mod8.has5
#print axioms Inf5mod8.inf_5mod8

And \(p\equiv 3\ (8)\): prime factors of \(x^2+2\) (\(x\) the odd factorial) have \(-2\) a square, which fails at \(q\equiv 7\) (\(-1=(x/w)^2\) with \(w^2=2\)) and at \(q\equiv 5\) (\(2=(zx)^2\) with \(z^2=-1\)); so they are \(\equiv 1,3\ (8)\) and one is \(\equiv 3\). Every class of \((\mathbb Z/8)^\times \) therefore contains infinitely many primes: the Chebotarev section exists for \(\mathbb Q(\zeta _8)/\mathbb Q\).

Listing 42: lang/Inf3mod8.lean

lang/Inf3mod8.lean

/-
  lang/Inf3mod8 — T43, cycle 130: infinitely many primes p ≡ 3 (mod 8); with cycles 125–129, Dirichlet for every class mod 8.

  x the odd factorial of B, N = x² + 2 ≡ 3 (mod 8). Every prime factor q of N has −2 ≡ x² a square, so q ≢ 5, 7 (mod 8):
  at q ≡ 7, 2 = w² (TwoSupp) gives −1 = (x/w)², impossible for q ≡ 3 (mod 4); at q ≡ 5, −1 = z² gives 2 = (zx)², impossible
  (TwoSupp). So q ≡ 1 or 3 (mod 8), and some factor is ≡ 3.
    uniq_rem, md_rem         the ZMod residue equals the FTAChain remainder
    allowed3, has3m8, inf_3mod8
  So every class a ∈ (ℤ/8)^× contains infinitely many primes: the Chebotarev section exists for ℚ(ζ₈)/ℚ.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat, lib_int), no axioms.
-/
import lang.Inf5mod8

namespace Inf3mod8

open FTAChain ZMod LibInt InfSplit Inf7mod8 Inf5mod8 TwoSupp

theorem uniq_rem (m' x y r c : Nat) (hr : r < m' + 1) (hc : c < m' + 1) (h : x * (m' + 1) + r = y * (m' + 1) + c) : r = c := by
  have big : ∀ x y r c : Nat, r < m' + 1 → x < y → x * (m' + 1) + r = y * (m' + 1) + c → False := fun x y r c hr hxy h => by
    have h1 : x * (m' + 1) + r < (x + 1) * (m' + 1) := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * (m' + 1) ≤ y * (m' + 1) := Nat.mul_le_mul_right (m' + 1) hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have e' : Int.ofNat x * Int.ofNat (m' + 1) + Int.ofNat r = Int.ofNat x * Int.ofNat (m' + 1) + Int.ofNat c := congrArg Int.ofNat h
      calc Int.ofNat r = (Int.ofNat x * Int.ofNat (m' + 1) + Int.ofNat r) - Int.ofNat x * Int.ofNat (m' + 1) := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

theorem md_rem (m' a : Nat) : md m' a = rem (m' + 1) a :=
  match md_spec m' a, rem_spec m' a with
  | ⟨q, hq⟩, ⟨q', r, k, ha, hr, hrem⟩ => by
    rw [hrem]
    apply uniq_rem m' q q' _ _ (md_lt m' a) (Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k)))
    exact (show q * (m' + 1) + md m' a = md m' a + q * (m' + 1) by lib_nat).trans (hq.symm.trans ha)

/-- squares: ZMod ↔ remainders. -/
theorem sq_rem (l y : Nat) (h : (ring (l + 1)).mul (mk (l + 1) y) (mk (l + 1) y) = mk (l + 1) 2) :
    rem (l + 1 + 1) (y * y) = rem (l + 1 + 1) 2 := by
  rw [TwoPower.mk_mul] at h
  rw [← md_rem, ← md_rem]; exact congrArg Subtype.val h

theorem rem_sq (l y : Nat) (h : rem (l + 1 + 1) (y * y) = rem (l + 1 + 1) 2) :
    (ring (l + 1)).mul (mk (l + 1) y) (mk (l + 1) y) = mk (l + 1) 2 := by
  rw [TwoPower.mk_mul]; apply Subtype.ext; show md (l + 1) (y * y) = md (l + 1) 2; rw [md_rem, md_rem]; exact h

theorem two_eq (l : Nat) : mk (l + 1) 2 = (ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one :=
  (PrimeOneModQ.mk_add (l + 1) 1 1).symm

theorem cofactor3 (b N' a : Nat) (h : (b * 8 + 1) * N' = a * 8 + 3) : ∃ c, N' = c * 8 + 3 :=
  match mod8 N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 8 + 1) * N' = (b * 8 * c + b * r + c) * 8 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_rem8 _ _ r 3 hr (Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right 4 4)) h]⟩

/-- prime factors of x² + 2 (x odd) are ≡ 1 or 3 (mod 8). -/
theorem allowed3 (x l : Nat) (hxo : ∃ y, x = 2 * y + 1) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) (x * x + 2)) : ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 3) := by
  have hPr : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, hq⟩
  have hx : (ring (l + 1)).mul (mk (l + 1) x) (mk (l + 1) x) = (ring (l + 1)).neg ((ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one) := by
    apply DetChain.neg_unique
    rw [TwoPower.mk_mul, ← two_eq, PrimeOneModQ.mk_add]
    exact Subtype.ext (dv_md_zero (l + 1) _ hdv)
  have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨c, by rw [hc]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    match hdv, hxo with
    | ⟨r, hr⟩, ⟨y, hy⟩ => exact odd_not_dv2 (2 * y * y + 2 * y + 1) r (by rw [← hr, hy]; lib_nat)
  match mod8 (l + 1 + 1) with
  | ⟨b, r, hb, hr⟩ =>
    match r, hr with
    | 0, _ => exact absurd hb (fun e => hodd (4 * b) (by rw [e]; lib_nat))
    | 1, _ => exact ⟨b, 1, hb, Or.inl rfl⟩
    | 2, _ => exact absurd hb (fun e => hodd (4 * b + 1) (by rw [e]; lib_nat))
    | 3, _ => exact ⟨b, 3, hb, Or.inr rfl⟩
    | 4, _ => exact absurd hb (fun e => hodd (4 * b + 2) (by rw [e]; lib_nat))
    | 5, _ =>
      exfalso
      have hl : l = 2 * (2 * (2 * b) + 1) + 1 := Nat.succ.inj (Nat.succ.inj (hb.trans (by lib_nat)))
      subst hl
      match GaussSplit.split_one_mod_four (2 * b) hq with
      | ⟨z, hz⟩ =>
        have h2 : (ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul (mk _ ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul z (mk _ x)).val)
            (mk _ ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul z (mk _ x)).val) = mk _ 2 := by
          rw [mk_val, two_eq]
          rw [show (ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul z (mk _ x)) ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul z (mk _ x))
              = (ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul z z) ((ring (2 * (2 * (2 * b) + 1) + 1 + 1)).mul (mk _ x) (mk _ x)) by lib_ring,
            hz, hx]
          lib_ring
        have e : (b + b + 1) + ((b + b + 1) + 0) + ((b + b + 1) + ((b + b + 1) + 0)) + 1 = 2 * (2 * (2 * b) + 1) + 1 + 1 + 1 := by lib_nat
        exact two_8m5 b (by rw [e]; exact hPr) ⟨_, by rw [e]; exact sq_rem _ _ h2⟩
    | 6, _ => exact absurd hb (fun e => hodd (4 * b + 3) (by rw [e]; lib_nat))
    | 7, _ =>
      exfalso
      have e : (b + b + 1) + ((b + b + 1) + 1) + ((b + b + 1) + ((b + b + 1) + 1)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
      match two_8m7 b (by rw [e]; exact hPr) with
      | ⟨w0, hw0⟩ =>
        rw [e] at hw0
        have hw := rem_sq l w0 hw0
        rw [two_eq] at hw
        have hwn : mk (l + 1) w0 ≠ (ring (l + 1)).zero := fun e0 => by
          rw [e0, DetChain.mul_zero] at hw
          have h2 : Dv (l + 1 + 1) (1 + 1) := md_zero_dv (l + 1) 2 (by
            have := congrArg Subtype.val hw.symm
            rw [← two_eq] at this; exact this)
          match dv_le (l + 1 + 1) 1 h2 with
          | ⟨k, hk⟩ =>
            have h0 : l + k = 0 := Nat.succ.inj (Nat.succ.inj ((show l + k + 1 + 1 = l + 1 + 1 + k by lib_nat).trans hk))
            exact hodd 1 (by rw [(HasseDavenport.nat_sum_zero _ _ h0).1])
        have hinv := ZMod.inv_exists l hq _ hwn
        have hl : l = 2 * (2 * (2 * b + 1)) + 1 := Nat.succ.inj (Nat.succ.inj (hb.trans (by lib_nat)))
        subst hl
        apply GaussSplit.inert_three_mod_four (2 * b + 1) hq
          ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul (mk _ x) (Frobenius.pw (ring (2 * (2 * (2 * b + 1)) + 1 + 1)) (mk _ w0) (2 * (2 * (2 * b + 1)) + 1)))
        generalize Frobenius.pw (ring (2 * (2 * (2 * b + 1)) + 1 + 1)) (mk _ w0) (2 * (2 * (2 * b + 1)) + 1) = v at hinv ⊢
        rw [show (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul (mk _ x) v) ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul (mk _ x) v)
            = (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul (mk _ x) (mk _ x)) ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul v v) by lib_ring,
          hx, ← hw]
        rw [show (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).neg ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul (mk _ w0) (mk _ w0))) ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul v v)
            = (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).neg ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul v (mk _ w0)) ((ring (2 * (2 * (2 * b + 1)) + 1 + 1)).mul v (mk _ w0))) by lib_ring,
          hinv, (ring (2 * (2 * (2 * b + 1)) + 1 + 1)).one_mul]
    | r + 8, hr => exact absurd hr (fun h => Nat.lt_irrefl 8 (Nat.lt_of_le_of_lt (Nat.le_add_left 8 r) h))

theorem has3m8 (x : Nat) (hxo : ∃ y, x = 2 * y + 1) : ∀ G N, N ≤ G → Dv N (x * x + 2) → (∃ a, N = a * 8 + 3) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 8 + 3) ∧ Dv q N
  | 0, N, hN, _, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 8 + 1) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    match allowed3 x l hxo hq (dv_trans _ _ _ hlN hN0) with
    | ⟨b, r, hb, hr⟩ =>
      cases hr with
      | inr h3 => exact ⟨l + 1 + 1, hq, ⟨b, by rw [hb, h3]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor3 b N' a (by rw [← h1, ← hb, ← ha, hN']; lib_nat) with
        | ⟨c, hc⟩ =>
        have hlt : N' ≤ G := by
          have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hN))
        have hN'0 : Dv N' (x * x + 2) := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has3m8 x hxo G N' hlt hN'0 ⟨c, hc⟩ with
        | ⟨q, hq', hq3, hqN⟩ => exact ⟨q, hq', hq3, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- **Infinitely many primes p ≡ 3 (mod 8).** -/
theorem inf_3mod8 (B : Nat) : ∃ b, (∀ d, Dv d (b * 8 + 3) → d = 1 ∨ d = b * 8 + 3) ∧ B < b * 8 + 3 := by
  match ofac_odd B with
  | ⟨y, hy⟩ =>
  have hN : ∃ a, ofac B * ofac B + 2 = a * 8 + 3 := by
    cases EveryPrime.nat_even_odd y with
    | inl h => match h with | ⟨c, hc⟩ => exact ⟨c * (2 * c + 1), by rw [hy, hc]; lib_nat⟩
    | inr h => match h with | ⟨c, hc⟩ => exact ⟨(2 * c + 1) * (c + 1), by rw [hy, hc]; lib_nat⟩
  match has3m8 (ofac B) ⟨y, hy⟩ _ _ (Nat.le_refl _) ⟨1, by lib_nat⟩ hN with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      match dv_ofac B (4 * b + 1) (by rw [show 2 * (4 * b + 1) + 1 = b * 8 + 3 by lib_nat, ← hb]; exact h) with
      | ⟨r, hr⟩ =>
        have hr' : ofac B = r * q := by rw [hr, hb]; lib_nat
        have h1 : Dv q (ofac B * ofac B) := ⟨r * ofac B, by rw [hr']; lib_nat⟩
        have hq2 : q = (b * 8 + 2) + 1 := hb
        rw [hq2] at hqN h1
        have h2 := dv_sub (b * 8 + 2) _ 2 h1 hqN
        match dv_le (b * 8 + 2 + 1) 1 h2 with
        | ⟨k, hk⟩ =>
          have e : b * 8 + 1 + k + 1 + 1 = 0 + 1 + 1 := (show b * 8 + 1 + k + 1 + 1 = b * 8 + 2 + 1 + k by lib_nat).trans hk
          exact Nat.noConfusion ((show b * 8 + k + 1 = b * 8 + 1 + k by lib_nat).trans (Nat.succ.inj (Nat.succ.inj e)))

end Inf3mod8

#print axioms Inf3mod8.uniq_rem
#print axioms Inf3mod8.md_rem
#print axioms Inf3mod8.sq_rem
#print axioms Inf3mod8.rem_sq
#print axioms Inf3mod8.two_eq
#print axioms Inf3mod8.cofactor3
#print axioms Inf3mod8.allowed3
#print axioms Inf3mod8.has3m8
#print axioms Inf3mod8.inf_3mod8

78.20 Strong multiplicity one for GL\(_1\) with moduli \(3\), \(4\), \(8\)

If two functions on residues mod \(N\in \{3,4,8\}\) agree at \(p\bmod N\) for every prime \(p>B\), they agree on every unit class. The steps are

\[ \begin {aligned} &\text {each unit class mod }N\text { contains a prime }p>B\quad (\text {the Dirichlet cases above}),\\ &p=bN+r\ \Rightarrow \ \chi (r)=\chi '(r). \end {aligned} \]

Listing 43: lang/StrongMult1.lean

lang/StrongMult1.lean

/-
  lang/StrongMult1 — T43, cycle 131: strong multiplicity one for GL1 over ℚ with moduli 3, 4, 8.

  A Dirichlet character is read off from its Frobenius values at primes beyond any bound: if two functions on residues agree
  at r whenever b·N + r is a prime > B, they agree on every unit class mod N.
    two_prime, three_prime
    sm1_mod4, sm1_mod3, sm1_mod8
  (InfSplit, InfInert, InfInert3, InfCyclo, InfCycloPow, Inf7mod8, Inf5mod8, Inf3mod8). On the automorphic side: two Hecke
  characters of ℚ(i), ℚ(√−3) or ℚ(ζ₈) with the same eigenvalues at almost all primes coincide.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Inf3mod8
import lang.InfCycloPow

namespace StrongMult1

open FTAChain

variable {R : Type}

theorem two_prime : ∀ d, Dv d (0 + 1 + 1) → d = 1 ∨ d = 0 + 1 + 1 := fun d hd =>
  match dv_le d 1 hd with
  | ⟨k, hk⟩ => by
    cases d with
    | zero => match hd with | ⟨q, hq⟩ => exact absurd hq (fun e => Nat.noConfusion e)
    | succ d =>
      cases d with
      | zero => exact Or.inl rfl
      | succ d =>
        cases d with
        | zero => exact Or.inr rfl
        | succ d => exact absurd hk (fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj ((show d + k + 1 + 1 + 1 = d + 1 + 1 + 1 + k by lib_nat).trans e))))

theorem three_prime : ∀ d, Dv d (1 + 1 + 1) → d = 1 ∨ d = 1 + 1 + 1 := fun d hd =>
  match dv_le d 2 hd with
  | ⟨k, hk⟩ => by
    cases d with
    | zero => match hd with | ⟨q, hq⟩ => exact absurd hq (fun e => Nat.noConfusion e)
    | succ d =>
      cases d with
      | zero => exact Or.inl rfl
      | succ d =>
        cases d with
        | zero => exfalso; match hd with | ⟨q, hq⟩ => exact InfSplit.odd_not_dv2 1 q hq
        | succ d =>
          cases d with
          | zero => exact Or.inr rfl
          | succ d => exact absurd hk (fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show d + k + 1 + 1 + 1 + 1 = d + 1 + 1 + 1 + 1 + k by lib_nat).trans e)))))

/-- **mod 4.** -/
theorem sm1_mod4 (χ χ' : Nat → R) (B : Nat)
    (h : ∀ b r, (∀ d, Dv d (b * 4 + r) → d = 1 ∨ d = b * 4 + r) → B < b * 4 + r → χ r = χ' r) :
    χ 1 = χ' 1 ∧ χ 3 = χ' 3 := by
  constructor
  · match InfSplit.inf_split B with
    | ⟨k, hp, hB⟩ =>
      have e : 2 * (2 * k + 1) + 1 + 1 + 1 = (k + 1) * 4 + 1 := by lib_nat
      exact h (k + 1) 1 (by rw [← e]; exact hp) (by rw [← e]; exact hB)
  · match InfInert.inf_inert B with
    | ⟨k, hp, hB⟩ =>
      have e : 2 * (2 * k) + 1 + 1 + 1 = k * 4 + 3 := by lib_nat
      exact h k 3 (by rw [← e]; exact hp) (by rw [← e]; exact hB)

/-- **mod 3.** -/
theorem sm1_mod3 (χ χ' : Nat → R) (B : Nat)
    (h : ∀ b r, (∀ d, Dv d (b * 3 + r) → d = 1 ∨ d = b * 3 + r) → B < b * 3 + r → χ r = χ' r) :
    χ 1 = χ' 1 ∧ χ 2 = χ' 2 := by
  constructor
  · match InfCyclo.inf_one_mod_q 1 three_prime B with
    | ⟨l, hp, ⟨c, hc⟩, hB⟩ =>
      have e : l + 1 + 1 = c * 3 + 1 := by rw [show l + 1 + 1 = (l + 1) + 1 from rfl, hc]
      exact h c 1 (by rw [← e]; exact hp) (by rw [← e]; exact hB)
  · match InfInert3.inf_inert3 B with
    | ⟨k, hp, hB⟩ =>
      have e : 3 * k + 2 = k * 3 + 2 := by lib_nat
      exact h k 2 (by rw [← e]; exact hp) (by rw [← e]; exact hB)

/-- **mod 8.** -/
theorem sm1_mod8 (χ χ' : Nat → R) (B : Nat)
    (h : ∀ b r, (∀ d, Dv d (b * 8 + r) → d = 1 ∨ d = b * 8 + r) → B < b * 8 + r → χ r = χ' r) :
    χ 1 = χ' 1 ∧ χ 3 = χ' 3 ∧ χ 5 = χ' 5 ∧ χ 7 = χ' 7 := by
  refine ⟨?_, ?_, ?_, ?_⟩
  · match InfCycloPow.inf_pow 0 two_prime 2 B with
    | ⟨l, hp, ⟨c, hc⟩, hB⟩ =>
      have hc' : l + 1 = c * 8 := hc
      have e : l + 1 + 1 = c * 8 + 1 := by rw [hc']
      exact h c 1 (by rw [← e]; exact hp) (by rw [← e]; exact hB)
  · match Inf3mod8.inf_3mod8 B with
    | ⟨b, hp, hB⟩ => exact h b 3 hp hB
  · match Inf5mod8.inf_5mod8 B with
    | ⟨b, hp, hB⟩ => exact h b 5 hp hB
  · match Inf7mod8.inf_7mod8 B with
    | ⟨b, hp, hB⟩ => exact h b 7 hp hB

end StrongMult1

#print axioms StrongMult1.two_prime
#print axioms StrongMult1.three_prime
#print axioms StrongMult1.sm1_mod4
#print axioms StrongMult1.sm1_mod3
#print axioms StrongMult1.sm1_mod8

Also infinitely many primes \(p\equiv 3\ (4)\): every \(N\equiv 3\ (4)\) has a prime factor \(\equiv 3\ (4)\) (a product of primes \(\equiv 1\ (4)\) is \(\equiv 1\)), and \(N=4\cdot B!-1\) has no prime factor \(\le B\). So both Frobenius classes of \(\mathrm {Gal}(\mathbb Q(i)/\mathbb Q)\) contain infinitely many primes.

Listing 44: lang/InfInert.lean

lang/InfInert.lean

/-
  lang/InfInert — T43, cycle 125: infinitely many primes inert in ℚ(i) (p ≡ 3 mod 4).

    has3        every N ≡ 3 (mod 4) has a prime factor ≡ 3 (mod 4)       (a product of primes ≡ 1 (mod 4) is ≡ 1)
    inf_inert   ∀ B, ∃ prime p = 4k + 3 > B                               (a factor of 4·B! − 1)
  With InfSplit both Frobenius classes of Gal(ℚ(i)/ℚ) contain infinitely many primes: the Chebotarev section of the
  reciprocity shell exists for ℚ(i)/ℚ, and the GL1 reciprocity of ℚ(i) is a bijection on characters mod 4.

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.InfCyclo

namespace InfInert

open FTAChain ZMod LibInt InfSplit

theorem mod4_cases (n : Nat) : (∃ a, n = 4 * a) ∨ (∃ a, n = 4 * a + 1) ∨ (∃ a, n = 4 * a + 2) ∨ (∃ a, n = 4 * a + 3) := by
  cases EveryPrime.nat_even_odd n with
  | inl h => match h with
    | ⟨c, hc⟩ => cases EveryPrime.nat_even_odd c with
      | inl h' => match h' with | ⟨a, ha⟩ => exact Or.inl ⟨a, by rw [hc, ha]; lib_nat⟩
      | inr h' => match h' with | ⟨a, ha⟩ => exact Or.inr (Or.inr (Or.inl ⟨a, by rw [hc, ha]; lib_nat⟩))
  | inr h => match h with
    | ⟨c, hc⟩ => cases EveryPrime.nat_even_odd c with
      | inl h' => match h' with | ⟨a, ha⟩ => exact Or.inr (Or.inl ⟨a, by rw [hc, ha]; lib_nat⟩)
      | inr h' => match h' with | ⟨a, ha⟩ => exact Or.inr (Or.inr (Or.inr ⟨a, by rw [hc, ha]; lib_nat⟩))

/-- 4x + r = 4y + s with r, s < 4 forces r = s; used through Int: 4X = s − r with |s − r| ≤ 3. -/
theorem four_ne (X : Int) (r : Int) (h : 4 * X = r) (h1 : r = 1 ∨ r = 2 ∨ r = 3 ∨ r = -1 ∨ r = -2 ∨ r = -3) : False := by
  have two : ∀ Y : Int, 2 * Y ≠ 1 := fun Y e => GaussInt.two_ne_one Y e
  rcases h1 with e | e | e | e | e | e <;> rw [e] at h
  · exact two (2 * X) (by rw [← h]; lib_int)
  · exact GaussInt.int_odd_ne_even 0 X (FinPrimary.two_cancel _ _ (by rw [show 2 * (2 * X) = 4 * X by lib_int, h]; lib_int)) |> fun x => x
  · exact GaussInt.int_odd_ne_even (2 * X - 2) 0 (by rw [show 2 * (2 * X - 2) + 1 = 4 * X - 3 by lib_int, h]; lib_int)
  · exact GaussInt.int_odd_ne_even (-2 * X - 1) 0 (by rw [show 2 * (-2 * X - 1) + 1 = -(4 * X) - 1 by lib_int, h]; lib_int)
  · exact GaussInt.int_odd_ne_even X 0 (FinPrimary.two_cancel _ _ (by rw [show 2 * (2 * X + 1) = 4 * X + 2 by lib_int, h]; lib_int))
  · exact two (2 * X + 2) (by rw [show 2 * (2 * X + 2) = 4 * X + 4 by lib_int, h]; lib_int)

theorem even_prime (q b : Nat) (hq : ∀ d, Dv d q → d = 1 ∨ d = q) (he : q = 2 * b) : q = 2 := by
  cases hq 2 ⟨b, by rw [he]; lib_nat⟩ with
  | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
  | inr e => exact e.symm

theorem odd_ne_2dv (a r : Nat) (h : 4 * a + 3 = r * 2) : False :=
  odd_not_dv2 (2 * a + 1) r (by rw [← h]; lib_nat)

/-- **Every N ≡ 3 (mod 4) has a prime factor ≡ 3 (mod 4).** -/
theorem has3 : ∀ F N, N ≤ F → (∃ a, N = 4 * a + 3) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ k, q = 4 * k + 3) ∧ Dv q N
  | 0, N, hN, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | F + 1, N, hN, ⟨a, ha⟩ => by
    match lp_exists (4 * a + 1) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    cases mod4_cases (l + 1 + 1) with
    | inl h =>
      exfalso
      match h with
      | ⟨b, hb⟩ =>
        have h2 := even_prime _ (2 * b) hq (by rw [hb]; lib_nat)
        rw [h2, ha] at hlN
        match hlN with | ⟨r, hr⟩ => exact odd_ne_2dv a r hr
    | inr h =>
      cases h with
      | inr h =>
        cases h with
        | inl h =>
          exfalso
          match h with
          | ⟨b, hb⟩ =>
            have h2 := even_prime _ (2 * b + 1) hq (by rw [hb]; lib_nat)
            rw [h2, ha] at hlN
            match hlN with | ⟨r, hr⟩ => exact odd_ne_2dv a r hr
        | inr h => exact ⟨l + 1 + 1, hq, h, hlN⟩
      | inl h =>
        match h, hlN with
        | ⟨b, hb⟩, ⟨N', hN'⟩ =>
          have hN'3 : ∃ c, N' = 4 * c + 3 := by
            have key : ∀ c r : Nat, N' = 4 * c + r → 4 * (Int.ofNat a - 4 * Int.ofNat b * Int.ofNat c - Int.ofNat c - Int.ofNat r * Int.ofNat b) = Int.ofNat r - 3 := by
              intro c r hc
              have e : Int.ofNat (4 * a + 3) = Int.ofNat ((4 * c + r) * (4 * b + 1)) := by
                rw [← ha, hN', hc, hb]
              have e' : 4 * Int.ofNat a + 3 = (4 * Int.ofNat c + Int.ofNat r) * (4 * Int.ofNat b + 1) := e
              calc 4 * (Int.ofNat a - 4 * Int.ofNat b * Int.ofNat c - Int.ofNat c - Int.ofNat r * Int.ofNat b)
                  = (4 * Int.ofNat a + 3) - 16 * Int.ofNat b * Int.ofNat c - 4 * Int.ofNat c - 4 * Int.ofNat r * Int.ofNat b - 3 := by lib_int
                _ = Int.ofNat r - 3 := by rw [e']; lib_int
            rcases mod4_cases N' with ⟨c, hc⟩ | ⟨c, hc⟩ | ⟨c, hc⟩ | ⟨c, hc⟩
            · exact absurd (key c 0 (by rw [hc]; rfl)) (fun h => four_ne _ _ h (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl))))))
            · exact absurd (key c 1 hc) (fun h => four_ne _ _ h (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))))
            · exact absurd (key c 2 hc) (fun h => four_ne _ _ h (Or.inr (Or.inr (Or.inr (Or.inl rfl)))))
            · exact ⟨c, hc⟩
          match hN'3 with
          | ⟨c, hc⟩ =>
          have hlt : N' ≤ F := by
            have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
            have h1 : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
            exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1 N') (by rw [← e]; exact hN))
          match has3 F N' hlt ⟨c, hc⟩ with
          | ⟨q, hq', hq3, hqN⟩ => exact ⟨q, hq', hq3, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- **Infinitely many primes p ≡ 3 (mod 4).** -/
theorem inf_inert (B : Nat) : ∃ k, (∀ d, Dv d (2 * (2 * k) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k) + 1 + 1 + 1)
    ∧ B < 2 * (2 * k) + 1 + 1 + 1 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  match has3 (4 * f + 3) (4 * f + 3) (Nat.le_refl _) ⟨f, rfl⟩ with
  | ⟨q, hq, ⟨k, hk⟩, hqN⟩ =>
    refine ⟨k, by rw [show 2 * (2 * k) + 1 + 1 + 1 = 4 * k + 3 by lib_nat, ← hk]; exact hq, ?_⟩
    rw [show 2 * (2 * k) + 1 + 1 + 1 = 4 * k + 3 by lib_nat, ← hk]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      have hq2 : q = (4 * k + 2) + 1 := hk
      match dv_fac B q (by rw [hk]; exact Nat.le_add_left 1 _) h with
      | ⟨r, hr⟩ =>
        have h4 : Dv q (4 * f + 3 + 1) := ⟨4 * r, by rw [show 4 * f + 3 + 1 = 4 * (f + 1) by lib_nat, ← hf, hr]; lib_nat⟩
        rw [hq2] at hqN h4
        have := dv_sub (4 * k + 2) _ 1 hqN h4
        exact dv_one_absurd (4 * k + 1) this

end InfInert

#print axioms InfInert.mod4_cases
#print axioms InfInert.four_ne
#print axioms InfInert.even_prime
#print axioms InfInert.odd_ne_2dv
#print axioms InfInert.has3
#print axioms InfInert.inf_inert

In the same way \(3\cdot B!-1\) has a prime factor \(\equiv 2\ (3)\) above \(B\), so both classes of \(\mathrm {Gal}(\mathbb Q(\sqrt {-3})/\mathbb Q)\) contain infinitely many primes.

Listing 45: lang/InfInert3.lean

lang/InfInert3.lean

/-
  lang/InfInert3 — T43, cycle 126: infinitely many primes inert in ℚ(√−3) (p ≡ 2 mod 3).

    has2        every N ≡ 2 (mod 3) has a prime factor ≡ 2 (mod 3)
    inf_inert3  ∀ B, ∃ prime p = 3k + 2 > B                                 (a factor of 3·B! − 1)
  With InfCyclo (ℓ = 3) both Frobenius classes of Gal(ℚ(√−3)/ℚ) contain infinitely many primes.

  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.InfInert
import lang.EveryPrime3

namespace InfInert3

open FTAChain ZMod LibInt InfSplit

theorem three_ne (X r : Int) (h : 3 * X = r) (h1 : r = 1 ∨ r = 2 ∨ r = -1 ∨ r = -2) : False := by
  rcases h1 with e | e | e | e <;> rw [e] at h
  · exact CubicSuper.three_ne_one X h
  · exact CubicSuper.three_ne_one (1 - X) (by rw [show 3 * (1 - X) = 3 - 3 * X by lib_int, h]; lib_int)
  · exact CubicSuper.three_ne_one (-X) (by rw [show 3 * -X = -(3 * X) by lib_int, h]; lib_int)
  · exact CubicSuper.three_ne_one (X + 1) (by rw [show 3 * (X + 1) = 3 * X + 3 by lib_int, h]; lib_int)

/-- **Every N ≡ 2 (mod 3) has a prime factor ≡ 2 (mod 3).** -/
theorem has2 : ∀ F N, N ≤ F → (∃ a, N = 3 * a + 2) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ k, q = 3 * k + 2) ∧ Dv q N
  | 0, N, hN, ⟨a, ha⟩ => absurd (ha ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | F + 1, N, hN, ⟨a, ha⟩ => by
    match lp_exists (3 * a) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    rcases EveryPrime3.nat_mod3 (l + 1 + 1) with ⟨b, hb⟩ | ⟨b, hb⟩ | ⟨b, hb⟩
    · -- q = 3b: q = 3, but 3 ∤ 3a + 2
      exfalso
      have h3 : l + 1 + 1 = 3 := by
        cases hq 3 ⟨b, by rw [hb]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h3, ha] at hlN
      match hlN with
      | ⟨r, hr⟩ =>
        exact CubicSuper.three_ne_one (Int.ofNat a + 1 - Int.ofNat r) (by
          have e : Int.ofNat (3 * a + 2) = Int.ofNat (r * 3) := congrArg Int.ofNat hr
          have e' : 3 * Int.ofNat a + 2 = Int.ofNat r * 3 := e
          calc 3 * (Int.ofNat a + 1 - Int.ofNat r) = (3 * Int.ofNat a + 2) - Int.ofNat r * 3 + 1 := by lib_int
            _ = 1 := by rw [e']; lib_int)
    · match hlN with
      | ⟨N', hN'⟩ =>
        have hN'2 : ∃ c, N' = 3 * c + 2 := by
          have key : ∀ c r : Nat, N' = 3 * c + r → 3 * (Int.ofNat a - 3 * Int.ofNat b * Int.ofNat c - Int.ofNat c - Int.ofNat r * Int.ofNat b) = Int.ofNat r - 2 := by
            intro c r hc
            have e : Int.ofNat (3 * a + 2) = Int.ofNat ((3 * c + r) * (3 * b + 1)) := by rw [← ha, hN', hc, hb]
            have e' : 3 * Int.ofNat a + 2 = (3 * Int.ofNat c + Int.ofNat r) * (3 * Int.ofNat b + 1) := e
            calc 3 * (Int.ofNat a - 3 * Int.ofNat b * Int.ofNat c - Int.ofNat c - Int.ofNat r * Int.ofNat b)
                = (3 * Int.ofNat a + 2) - 9 * Int.ofNat b * Int.ofNat c - 3 * Int.ofNat c - 3 * Int.ofNat r * Int.ofNat b - 2 := by lib_int
              _ = Int.ofNat r - 2 := by rw [e']; lib_int
          rcases EveryPrime3.nat_mod3 N' with ⟨c, hc⟩ | ⟨c, hc⟩ | ⟨c, hc⟩
          · exact absurd (key c 0 (by rw [hc]; rfl)) (fun h => three_ne _ _ h (Or.inr (Or.inr (Or.inr rfl))))
          · exact absurd (key c 1 hc) (fun h => three_ne _ _ h (Or.inr (Or.inr (Or.inl rfl))))
          · exact ⟨c, hc⟩
        match hN'2 with
        | ⟨c, hc⟩ =>
        have hlt : N' ≤ F := by
          have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1 : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1 N') (by rw [← e]; exact hN))
        match has2 F N' hlt ⟨c, hc⟩ with
        | ⟨q, hq', hq2, hqN⟩ => exact ⟨q, hq', hq2, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l + 1 + 1, hq, ⟨b, hb⟩, hlN⟩

/-- **Infinitely many primes p ≡ 2 (mod 3).** -/
theorem inf_inert3 (B : Nat) : ∃ k, (∀ d, Dv d (3 * k + 2) → d = 1 ∨ d = 3 * k + 2) ∧ B < 3 * k + 2 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  match has2 (3 * f + 2) (3 * f + 2) (Nat.le_refl _) ⟨f, rfl⟩ with
  | ⟨q, hq, ⟨k, hk⟩, hqN⟩ =>
    refine ⟨k, by rw [← hk]; exact hq, ?_⟩
    rw [← hk]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      cases k with
      | zero =>
        -- q = 2 ≤ B: 2 | B!, and 2 | 3·B! − 1 = 3f + 2 means 2 | 3f, fine; use q ∤ 1 directly
        match dv_fac B q (by rw [hk]; exact Nat.le_add_left 1 _) h with
        | ⟨r, hr⟩ =>
          have h3 : Dv q (3 * f + 2 + 1) := ⟨3 * r, by rw [show 3 * f + 2 + 1 = 3 * (f + 1) by lib_nat, ← hf, hr]; lib_nat⟩
          have hq2 : q = 1 + 1 := hk
          rw [hq2] at hqN h3
          exact dv_one_absurd 0 (dv_sub 1 _ 1 hqN h3)
      | succ k' =>
        match dv_fac B q (by rw [hk]; exact Nat.le_add_left 1 _) h with
        | ⟨r, hr⟩ =>
          have h3 : Dv q (3 * f + 2 + 1) := ⟨3 * r, by rw [show 3 * f + 2 + 1 = 3 * (f + 1) by lib_nat, ← hf, hr]; lib_nat⟩
          have hq2 : q = (3 * k' + 3 + 1) + 1 := by rw [hk]; lib_nat
          rw [hq2] at hqN h3
          exact dv_one_absurd (3 * k' + 3) (dv_sub (3 * k' + 3 + 1) _ 1 hqN h3)

end InfInert3

#print axioms InfInert3.three_ne
#print axioms InfInert3.has2
#print axioms InfInert3.inf_inert3

78.21 GL\(_2/\mathbb Q\) CM: \(y^2=x^3-x\)

With \(a_p=-\sum _x\chi (x^3-x)\), \(\chi \) the quadratic character of \(\mathbb F_p\),

\[ \begin {aligned} p\equiv 3\ (4)&:\ a_p=0,\\ p\equiv 1\ (4)&:\ a_p=2a,\ p=a^2+b^2,\ a+bi\ \text {primary},\\ p=4k+5&:\ a_p\equiv (-1)^{k+1}\tbinom {2k+2}{k+1}\ (p),\qquad \tbinom {2k+2}{k+1}\equiv \pm 2u\ (p). \end {aligned} \]
The steps are
\[ \begin {aligned} \textstyle \sum _x\chi \bigl ((-x)^3+x\bigr )&=-\textstyle \sum _x\chi (x^3-x)\quad (\chi (-1)=-1\ \text {at}\ p\equiv 3\ (4)),\\ \varphi (1)^2+\varphi (n)^2&=4p\quad (\text {Jacobsthal moments}),\ \text {so}\ \textstyle \sum _x\chi (x^3-x)=\pm 2u,\\ \textstyle \sum _x\chi (x^3-x)&\equiv 7-p\ (8)\quad (\text {pairing }x\mapsto -x,\ 1/x,\ i/x)\ \Rightarrow \ \text {the sign},\\ \textstyle \sum _x(x^3-x)^{(p-1)/2}&=-(-1)^{k+1}\tbinom {2k+2}{k+1}\quad (\text {power sums }\textstyle \sum _x x^e). \end {aligned} \]

Listing 46: lang/CMInert.lean

lang/CMInert.lean

/-
  lang/CMInert — T43, cycle 22 (GL2 over ℚ, CM form of y² = x³ − x): a_p = 0 at every inert prime p ≡ 3 (mod 4).

  p = 4k + 3, ℤ/p (lang/ZMod), χ(z) = z^((p−1)/2) = z^(2k+1).
    allZ, complete_allZ   the residues 0, …, p−1 listed once each (FiniteRecChain.Complete)
    sum_neg_fun, cub_neg  Σ(−F) = −ΣF; (−x)³ − (−x) = −(x³ − x)
    char_sum_cm           Σ_x χ(x³ − x) = 0
  Since #E(F_p) = p + 1 + Σ_x χ(x³ − x), this is a_p = 0 for every p ≡ 3 (mod 4) — the inert primes of ℚ(i), where the
  Galois side (Dihedral.ind_inert: the trace of Ind ψ vanishes off the index-2 subgroup) also gives 0.
  Checked earlier (cycle 5): a_p = 0 for all p ≡ 3 (mod 4) below 400.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.ZMod
import lang.TwoPower
import FiniteRecChain

namespace CMInert

open FTAChain ZMod YMChain FiniteRecChain

variable (m : Nat)

def allZ : Nat → List (Zp m)
  | 0 => []
  | k + 1 => allZ k ++ [mk m k]

theorem sumK_app' {R : Type} (Rg : DetChain.CRing R) {A : Type} (f : A → R) :
    ∀ l l' : List A, sumK Rg f (l ++ l') = Rg.add (sumK Rg f l) (sumK Rg f l')
  | [], l' => (Rg.zero_add _).symm
  | a :: l, l' => by
    show Rg.add (f a) (sumK Rg f (l ++ l')) = Rg.add (Rg.add (f a) (sumK Rg f l)) (sumK Rg f l')
    rw [sumK_app' Rg f l l', Rg.add_assoc]

/-- Each residue `z` occurs among `mk 0, …, mk (k−1)` exactly once if `z.val < k`, else not at all. -/
theorem count_allZ {R : Type} (Rg : DetChain.CRing R) (z : Zp m) : ∀ k, k ≤ m + 1 →
    sumK Rg (fun y => delta Rg y z) (allZ m k) = (if z.val < k then Rg.one else Rg.zero)
  | 0, _ => by
    show Rg.zero = (if z.val < 0 then Rg.one else Rg.zero)
    rw [ite_neg' _ _ (Nat.not_lt_zero _)]
  | k + 1, hk => by
    show sumK Rg (fun y => delta Rg y z) (allZ m k ++ [mk m k]) = _
    rw [sumK_app', count_allZ Rg z k (Nat.le_of_succ_le hk)]
    show Rg.add (if z.val < k then Rg.one else Rg.zero) (Rg.add (delta Rg (mk m k) z) Rg.zero) = _
    rw [DetChain.add_zero']
    have hmk : (mk m k).val = k := md_small m k (Nat.lt_of_succ_le hk)
    by_cases hz : z.val < k
    · have hne : ¬ mk m k = z := fun e => by rw [← e, hmk] at hz; exact Nat.lt_irrefl k hz
      rw [ite_pos' _ _ hz, ite_pos' _ _ (Nat.lt_succ_of_lt hz)]
      show Rg.add Rg.one (if mk m k = z then Rg.one else Rg.zero) = Rg.one
      rw [ite_neg' _ _ hne, DetChain.add_zero']
    · rw [ite_neg' _ _ hz]
      by_cases hzk : z.val = k
      · have he : mk m k = z := Subtype.ext (hmk.trans hzk.symm)
        rw [ite_pos' _ _ (by rw [hzk]; exact Nat.lt_succ_self k)]
        show Rg.add Rg.zero (if mk m k = z then Rg.one else Rg.zero) = Rg.one
        rw [ite_pos' _ _ he, Rg.zero_add]
      · have hne : ¬ mk m k = z := fun e => hzk (by rw [← e]; exact hmk)
        have hnl : ¬ z.val < k + 1 := fun h => by
          cases Nat.lt_or_eq_of_le (Nat.le_of_lt_succ h) with
          | inl h' => exact hz h'
          | inr h' => exact hzk h'
        rw [ite_neg' _ _ hnl]
        show Rg.add Rg.zero (if mk m k = z then Rg.one else Rg.zero) = Rg.zero
        rw [ite_neg' _ _ hne, Rg.zero_add]

theorem complete_allZ {R : Type} (Rg : DetChain.CRing R) : Complete Rg (allZ m (m + 1)) := fun z => by
  rw [count_allZ m Rg z (m + 1) (Nat.le_refl _), ite_pos' _ _ z.property]

theorem sum_neg_fun {R : Type} (Rg : DetChain.CRing R) {A : Type} (f : A → R) :
    ∀ l : List A, sumK Rg (fun x => Rg.neg (f x)) l = Rg.neg (sumK Rg f l)
  | [] => (DetChain.neg_unique Rg _ _ (Rg.zero_add Rg.zero))
  | a :: l => by
    show Rg.add (Rg.neg (f a)) (sumK Rg (fun x => Rg.neg (f x)) l) = Rg.neg (Rg.add (f a) (sumK Rg f l))
    rw [sum_neg_fun Rg f l]
    apply DetChain.neg_unique
    rw [DetChain.add_mid4, DetChain.add_neg', DetChain.add_neg', Rg.zero_add]

/-- `x³ − x`. -/
def cub {R : Type} (Rg : DetChain.CRing R) (x : R) : R := Rg.add (Rg.mul (Rg.mul x x) x) (Rg.neg x)

theorem cub_neg {R : Type} (Rg : DetChain.CRing R) (x : R) : cub Rg (Rg.neg x) = Rg.neg (cub Rg x) := by
  show Rg.add (Rg.mul (Rg.mul (Rg.neg x) (Rg.neg x)) (Rg.neg x)) (Rg.neg (Rg.neg x)) = Rg.neg (Rg.add (Rg.mul (Rg.mul x x) x) (Rg.neg x))
  have nn : Rg.mul (Rg.neg x) (Rg.neg x) = Rg.mul x x := by
    rw [DetChain.neg_mul_left, Rg.mul_comm x (Rg.neg x), DetChain.neg_mul_left, DetChain.neg_neg']
  rw [nn, Rg.mul_comm (Rg.mul x x) (Rg.neg x), DetChain.neg_mul_left, Rg.mul_comm x (Rg.mul x x)]
  apply DetChain.neg_unique
  rw [DetChain.add_mid4, DetChain.add_neg', DetChain.neg_neg', Rg.add_neg, Rg.zero_add]

/-- **The CM character sum vanishes at inert primes** (p = 4k + 3, written 2m + 3 with m = 2k). -/
theorem char_sum_cm (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k) + 1 + 1 + 1) :
    sumK (ZMod.ring (2 * (2 * k) + 1 + 1))
      (fun x => Frobenius.pw (ZMod.ring (2 * (2 * k) + 1 + 1)) (cub (ZMod.ring (2 * (2 * k) + 1 + 1)) x) (2 * k + 1))
      (allZ (2 * (2 * k) + 1 + 1) (2 * (2 * k) + 1 + 1 + 1)) = (ZMod.ring (2 * (2 * k) + 1 + 1)).zero := by
  let K := ZMod.ring (2 * (2 * k) + 1 + 1)
  let F := fun x => Frobenius.pw K (cub K x) (2 * k + 1)
  have hc := complete_allZ (2 * (2 * k) + 1 + 1) K
  -- Σ F(−x) = Σ F(x)
  have hre := reindex K (allZ _ _) hc K.neg K.neg (fun x => DetChain.neg_neg' K x) (fun x => DetChain.neg_neg' K x) F
  -- F(−x) = −F(x)
  have hodd : ∀ x, F (K.neg x) = K.neg (F x) := fun x => by
    show Frobenius.pw K (cub K (K.neg x)) (2 * k + 1) = K.neg (Frobenius.pw K (cub K x) (2 * k + 1))
    rw [cub_neg]
    have e : K.neg (cub K x) = K.mul (K.neg K.one) (cub K x) := by rw [DetChain.neg_mul_left, K.one_mul]
    rw [e, Frobenius.pw_mul, TwoPower.negone_odd' (2 * (2 * k) + 1) k, DetChain.neg_mul_left, K.one_mul]
  rw [sumK_ext K _ _ hodd, sum_neg_fun] at hre
  -- S = −S ⇒ (1 + 1) S = 0
  have e2 : K.mul (K.add K.one K.one) (sumK K F (allZ (2 * (2 * k) + 1 + 1) (2 * (2 * k) + 1 + 1 + 1))) = K.zero := by
    rw [DetChain.right_distrib, K.one_mul]
    have hs : K.add (sumK K F (allZ (2 * (2 * k) + 1 + 1) (2 * (2 * k) + 1 + 1 + 1))) (sumK K F (allZ (2 * (2 * k) + 1 + 1) (2 * (2 * k) + 1 + 1 + 1)))
        = K.add (K.neg (sumK K F (allZ (2 * (2 * k) + 1 + 1) (2 * (2 * k) + 1 + 1 + 1)))) (sumK K F (allZ (2 * (2 * k) + 1 + 1) (2 * (2 * k) + 1 + 1 + 1))) :=
      congrArg (fun z => K.add z (sumK K F (allZ (2 * (2 * k) + 1 + 1) (2 * (2 * k) + 1 + 1 + 1)))) hre.symm
    rw [hs]
    exact DetChain.add_neg' K _
  cases ZMod.nozd (2 * (2 * k) + 1) hpr _ _ e2 with
  | inl h =>
    exfalso
    have := congrArg Subtype.val h
    -- (1 + 1).val = 2 ≠ 0
    have h2 : (K.add K.one K.one).val = 2 := by
      show md (2 * (2 * k) + 1 + 1) (md (2 * (2 * k) + 1 + 1) 1 + md (2 * (2 * k) + 1 + 1) 1) = 2
      rw [md_small _ 1 (Nat.succ_lt_succ (Nat.zero_lt_succ _))]
      exact md_small _ 2 (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ _)))
    rw [h2] at this
    exact Nat.noConfusion this
  | inr h => exact h

end CMInert

#print axioms CMInert.sumK_app'
#print axioms CMInert.count_allZ
#print axioms CMInert.complete_allZ
#print axioms CMInert.sum_neg_fun
#print axioms CMInert.cub_neg
#print axioms CMInert.char_sum_cm

Listing 47: lang/CMModular.lean

lang/CMModular.lean

/-
  lang/CMModular — T43, cycle 46: the Frobenius trace of y² = x³ − x at split primes is ±2u, p = u² + v² — in integers.

  p = 4k + 5 (m = 2k + 1, p = 2m + 3), χ, φ as before. Then
    chi_neg_one        χ(−1) = 1                                       (GaussSplit)
    sum4p              φ(1)² + φ(n)² = 4p                               (JacobMoment.moment, JacobClass.grouped, p − 1 ≠ 0)
    nat_sq             every integer s has s² = |s|² with |s| ∈ ℕ
    halves             a² + b² = 4p in ℕ ⇒ a = 2u, b = 2v, u² + v² = p
    phi_neg_one        φ(−1) = ±φ(1)                                    (φ(i² · 1) = χ(i) φ(1))
    cm_trace           ∃ u v, u² + v² = p ∧ (Σ_x χ(x³ − x) = 2u ∨ Σ_x χ(x³ − x) = −2u)
  Since #E(𝔽_p) = p + 1 + Σ_x χ(x³ − x), the trace a_p = −Σ_x χ(x³ − x) is ±2u: on the automorphic side the Hecke
  character of ℚ(i) gives ψ(π) + ψ(π̄) = ±2u for π = u + vi — the trace formula of the CM newform of y² = x³ − x, now as an
  integer identity at every split prime (CMInert: a_p = 0 at every inert prime).

  Lean 4 core only, proofs by hand (lib_int, lib_nat, lib_ring), no axioms.
-/
import lang.JacobClass
import lang.GaussSplit
import QRChain

namespace CMModular

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal JacobMoment JacobClass QRChain

theorem nat_sq (s : Int) : ∃ a : Nat, s * s = Int.ofNat (a * a) ∧ (s = Int.ofNat a ∨ s = -Int.ofNat a) := by
  cases s with
  | ofNat a => exact ⟨a, rfl, Or.inl rfl⟩
  | negSucc a => exact ⟨a + 1, rfl, Or.inr rfl⟩

theorem par_sq' (u : Nat) : par (u * u) = par u := by rw [par_mul]; cases par u <;> rfl

theorem halves (a b p : Nat) (h : a * a + b * b = 4 * p) : ∃ u v, a = 2 * u ∧ b = 2 * v ∧ u * u + v * v = p := by
  have hp : par (a * a + b * b) = false := by rw [h, par_mul]; rfl
  rw [par_add, par_sq', par_sq'] at hp
  cases ha : par a with
  | true =>
    have hb : par b = true := by rw [ha] at hp; revert hp; cases par b <;> intro e <;> first | rfl | exact Bool.noConfusion e
    match (par_split a).2 ha, (par_split b).2 hb with
    | ⟨x, ex⟩, ⟨y, ey⟩ =>
      exfalso
      have e : 2 * (2 * (x * x + x + y * y + y) + 1) = 2 * (2 * p) := by
        have h' := h
        rw [ex, ey] at h'
        rw [show 2 * (2 * p) = 4 * p by lib_nat, ← h']
        lib_nat
      have e2 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1) e
      have := congrArg par e2
      rw [par_add, par_mul 2, par_mul 2] at this
      exact Bool.noConfusion this
  | false =>
    have hb : par b = false := by rw [ha] at hp; revert hp; cases par b <;> intro e <;> first | rfl | exact Bool.noConfusion e
    match (par_split a).1 ha, (par_split b).1 hb with
    | ⟨x, ex⟩, ⟨y, ey⟩ =>
      refine ⟨x, y, by rw [ex]; lib_nat, by rw [ey]; lib_nat, ?_⟩
      apply Nat.eq_of_mul_eq_mul_left (show 0 < 4 from Nat.zero_lt_succ 3)
      rw [← h, ex, ey]; lib_nat

variable (k : Nat)

/-- **The Frobenius trace of y² = x³ − x at p = 4k + 5 is ±2u with p = u² + v².** -/
theorem cm_trace (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1) :
    ∃ u v : Nat, u * u + v * v = 2 * (2 * k + 1) + 1 + 1 + 1 ∧
      (sumK Z (fun x => chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (L (2 * k + 1)) = Int.ofNat (2 * u) ∨
       sumK Z (fun x => chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (L (2 * k + 1)) = -Int.ofNat (2 * u)) := by
  match GaussSplit.split_one_mod_four k hpr, exists_nonres (2 * k + 1) hpr with
  | ⟨i, hi⟩, ⟨n, hn0, hn⟩ =>
    have hi0 : i ≠ (ring (2 * (2 * k + 1) + 1 + 1)).zero := fun e => by
      rw [e, DetChain.zero_mul'] at hi
      apply PowerSum.one_ne_zero' (2 * (2 * k + 1) + 1)
      have := congrArg (fun z => (ring (2 * (2 * k + 1) + 1 + 1)).add z (ring (2 * (2 * k + 1) + 1 + 1)).one) hi
      exact (((ring (2 * (2 * k + 1) + 1 + 1)).zero_add (ring (2 * (2 * k + 1) + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * (2 * k + 1) + 1 + 1)) (ring (2 * (2 * k + 1) + 1 + 1)).one)))
    have hchi : chi (2 * k + 1) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) = 1 := by rw [← hi]; exact chi_sq (2 * k + 1) hpr i hi0
    have hM := moment (2 * k + 1) hpr
    have hG := grouped (2 * k + 1) hpr n hn0 hn
    rw [hchi] at hM
    rw [hM] at hG
    -- (P − 1)(A + B − 4P) = 0
    have hz : (P (2 * k + 1) + -1) * (phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n + -1 * (4 * P (2 * k + 1))) = 0 := by
      have : (P (2 * k + 1) + -1) * (phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n + -1 * (4 * P (2 * k + 1)))
          = (P (2 * k + 1) + -1) * (phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n) + -1 * (2 * (P (2 * k + 1) * ((1 + 1) * (P (2 * k + 1) + -1)))) := by
        lib_int
      rw [this, ← hG]; lib_int
    have hP1 : P (2 * k + 1) + -1 ≠ 0 := fun e => by
      have e1 : P (2 * k + 1) = 1 := by
        have : P (2 * k + 1) = (P (2 * k + 1) + -1) + 1 := by lib_int
        rw [this, e]; rfl
      have e2 : Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) = Int.ofNat 1 := e1
      have e3 := Int.ofNat.inj e2
      exact Nat.noConfusion (Nat.succ.inj e3)
    have hAB : phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n = 4 * P (2 * k + 1) := by
      cases LibInt.int_noZD _ _ hz with
      | inl h => exact absurd h hP1
      | inr h =>
        have : phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n
            = (phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n + -1 * (4 * P (2 * k + 1))) + 4 * P (2 * k + 1) := by lib_int
        rw [this, show (phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n + -1 * (4 * P (2 * k + 1))) = 0 from h]
        lib_int
    match nat_sq (phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one), nat_sq (phi (2 * k + 1) n) with
    | ⟨a, ha, hsa⟩, ⟨b, hb, _⟩ =>
      rw [ha, hb] at hAB
      have hnat : a * a + b * b = 4 * (2 * (2 * k + 1) + 1 + 1 + 1) := Int.ofNat.inj hAB
      match halves a b _ hnat with
      | ⟨u, v, hau, _, huv⟩ =>
        refine ⟨u, v, huv, ?_⟩
        -- Σ χ(x³ − x) = φ(−1) = χ(i) φ(1)
        have e1 : sumK Z (fun x => chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (L (2 * k + 1)) = phi (2 * k + 1) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) :=
          sumK_ext Z _ _ (fun x => by
            show chi (2 * k + 1) ((ring (2 * (2 * k + 1) + 1 + 1)).add ((ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).mul x x) x) ((ring (2 * (2 * k + 1) + 1 + 1)).neg x)) = chi (2 * k + 1) ((ring (2 * (2 * k + 1) + 1 + 1)).mul x ((ring (2 * (2 * k + 1) + 1 + 1)).add ((ring (2 * (2 * k + 1) + 1 + 1)).mul x x) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1  -- (+13 chars; full line in the Zenodo source)
            have : (ring (2 * (2 * k + 1) + 1 + 1)).add ((ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).mul x x) x) ((ring (2 * (2 * k + 1) + 1 + 1)).neg x) = (ring (2 * (2 * k + 1) + 1 + 1)).mul x ((ring (2 * (2 * k + 1) + 1 + 1)).add ((ring (2 * (2 * k + 1) + 1 + 1)).mul x x) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one)) := by lib_ring
            rw [this]) _
        have e2 : phi (2 * k + 1) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) = chi (2 * k + 1) i * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one := by
          rw [← hi, ← phi_scale (2 * k + 1) hpr (ring (2 * (2 * k + 1) + 1 + 1)).one i hi0, (ring (2 * (2 * k + 1) + 1 + 1)).one_mul]
        rw [e1, e2]
        cases chi_cases (2 * k + 1) i with
        | inl h0 => exact absurd h0 (chi_ne_zero (2 * k + 1) i hi0)
        | inr h =>
          cases hsa with
          | inl sa =>
            cases h with
            | inl h1 => left; rw [h1, sa, hau]; lib_int
            | inr h1 => right; rw [h1, sa, hau]; lib_int
          | inr sa =>
            cases h with
            | inl h1 => right; rw [h1, sa, hau]; lib_int
            | inr h1 => left; rw [h1, sa, hau]; lib_int

end CMModular

#print axioms CMModular.nat_sq
#print axioms CMModular.par_sq'
#print axioms CMModular.halves
#print axioms CMModular.cm_trace

Listing 48: lang/SignCM3.lean

lang/SignCM3.lean

/-
  lang/SignCM3 — T43, cycle 58: a_p(y² = x³ − x) = 2 Re π for the primary π above p (the exact sign).

  p = 4k + 5. Since i³ − i = −2i = (1 − i)², I(i) = 1, so (SignCM2.sign_mod8)
    S_mod8        Σ_x χ(x³ − x) = 2 − 4k + 8 H₂      (≡ 7 − p mod 8; equivalently 8 | #E(𝔽_p))
  With CMModular (Σ = ±2u, u² + v² = p):
    ap_primary    ∃ a b ∈ ℤ, a² + b² = p, a + b i primary (b even, a + b ≡ 1 mod 4), and −Σ_x χ(x³ − x) = 2a,
  i.e. a_p = ψ(π) + ψ(π̄) for the Hecke character ψ(π) = π on primary π (GaussInt) — the CM newform of y² = x³ − x
  and the Hecke character of ℚ(i) have the same Frobenius traces at every split prime, sign included.

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.SignCM2
import lang.GaussInt
import lang.CubicTrace

namespace SignCM3

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum JacobMoment SignCM SignCM2
open Pairing (ltI eqI)

variable (m : Nat)

theorem I_i_one (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (i : Zp (2 * m + 1 + 1))
    (hi : (ring (2 * m + 1 + 1)).mul i i = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) : I m i = 1 := by
  have e : f m i = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i)) := by
    have id : f m i = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i)))
        ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add i ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one)) := by
      unfold f; show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i i) i) ((ring (2 * m + 1 + 1)).neg i) = _; lib_ring
    rw [id, hi, DetChain.add_neg' (ring (2 * m + 1 + 1)) (ring (2 * m + 1 + 1)).one, DetChain.mul_zero, DetChain.add_zero']
  have h1 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i) ≠ (ring (2 * m + 1 + 1)).zero := fun h => by
    have := Eisen.eq_of_sub_zero (2 * m + 1) _ _ h
    rw [← this, (ring (2 * m + 1 + 1)).one_mul] at hi
    exact ne_pm m hi.symm
  unfold I
  rw [e, chi_sq m hpr _ h1, ite_pos' _ _ rfl]

theorem odd_sq (e : Int) : (2 * e + 1) * (2 * e + 1) = 4 * (e * e + e) + 1 := by lib_int

theorem not_odd_mult4 (x y : Int) (h : 4 * x + 1 = 4 * y) : False :=
  GaussInt.int_odd_ne_even (2 * x) (2 * y) (by rw [show 2 * (2 * x) + 1 = 4 * x + 1 by lib_int, h]; lib_int)

theorem cancel4 (x y : Int) (h : 4 * x = 4 * y) : x = y :=
  CubicTrace.cancel_N 4 (fun e => Nat.noConfusion e) x y h

/-- **a_p = 2 Re π for the primary π** (p = 4k + 5). -/
theorem ap_primary (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1) :
    ∃ a b : Int, a * a + b * b = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) ∧ GaussInt.Primary ⟨a, b⟩ ∧
      -sumK Z (fun x => chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (L (2 * k + 1)) = 2 * a := by
  match GaussSplit.split_one_mod_four k hpr, CMModular.cm_trace k hpr with
  | ⟨i, hi⟩, ⟨u, v, huv, hS⟩ =>
    have hM := SignCM2.sign_mod8 (2 * k + 1) hpr i hi
    rw [I_i_one (2 * k + 1) hpr i hi] at hM
    have hP : P (2 * k + 1) = 4 * Int.ofNat k + 5 := by
      show Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) = _
      rw [show 2 * (2 * k + 1) + 1 + 1 + 1 = k + k + k + k + 5 by lib_nat]
      show Int.ofNat k + Int.ofNat k + Int.ofNat k + Int.ofNat k + 5 = _
      lib_int
    rw [hP] at hM
    have hS' : sumK Z (fun x => chi (2 * k + 1) (f (2 * k + 1) x)) (L (2 * k + 1)) = sumK Z (fun x => chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (L (2 * k + 1)) := rfl
    rw [hS'] at hM
    generalize hH : sumK Z (fun x => ltI (2 * (2 * k + 1) + 1) x (rho (2 * k + 1) x) * J (2 * k + 1) x) (L (2 * k + 1)) = H2 at hM
    generalize hSS : sumK Z (fun x => chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (L (2 * k + 1)) = S at hM hS ⊢
    -- s = S/2 = 1 − 2k + 4 H₂, and s = ±u
    have hs : S = 2 * (1 + -2 * Int.ofNat k + 4 * H2) := by rw [hM]; lib_int
    have hu : Int.ofNat u = 1 + -2 * Int.ofNat k + 4 * H2 ∨ Int.ofNat u = -(1 + -2 * Int.ofNat k + 4 * H2) := by
      cases hS with
      | inl e =>
        left; apply CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e)
        show (2 : Int) * Int.ofNat u = 2 * _
        rw [← hs]; exact e.symm
      | inr e =>
        right; apply CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e)
        show (2 : Int) * Int.ofNat u = 2 * _
        rw [show (2 : Int) * -(1 + -2 * Int.ofNat k + 4 * H2) = -(2 * (1 + -2 * Int.ofNat k + 4 * H2)) by lib_int, ← hs, e]
        show (2 : Int) * Int.ofNat u = -(-Int.ofNat (2 * u))
        show (2 : Int) * Int.ofNat u = -(-(2 * Int.ofNat u))
        lib_int
    -- u² = 4(e² + e) + 1 with e = −k + 2H₂ (or its negative's partner)
    have hu2 : Int.ofNat u * Int.ofNat u = 4 * ((-Int.ofNat k + 2 * H2) * (-Int.ofNat k + 2 * H2) + (-Int.ofNat k + 2 * H2)) + 1 := by
      cases hu with
      | inl e => rw [e]; lib_int
      | inr e => rw [e]; lib_int
    have hp : Int.ofNat u * Int.ofNat u + Int.ofNat v * Int.ofNat v = 4 * Int.ofNat k + 5 := by
      show Int.ofNat (u * u + v * v) = _
      rw [huv, show 2 * (2 * k + 1) + 1 + 1 + 1 = k + k + k + k + 5 by lib_nat]
      show Int.ofNat k + Int.ofNat k + Int.ofNat k + Int.ofNat k + 5 = _
      lib_int
    generalize he : -Int.ofNat k + 2 * H2 = e at hu2
    -- v is even
    have hv2 : Int.ofNat v * Int.ofNat v = 4 * (Int.ofNat k + 1 + -1 * (e * e + e)) := by
      have : Int.ofNat v * Int.ofNat v = (Int.ofNat u * Int.ofNat u + Int.ofNat v * Int.ofNat v) + -1 * (Int.ofNat u * Int.ofNat u) := by lib_int
      rw [this, hp, hu2]; lib_int
    match GaussInt.int_even_odd (Int.ofNat v) with
    | Or.inr ⟨c, hc⟩ =>
      exfalso
      rw [hc, odd_sq] at hv2
      exact not_odd_mult4 _ _ hv2
    | Or.inl ⟨c, hc⟩ =>
      rw [hc] at hv2
      have hcc : c * c = Int.ofNat k + 1 + -1 * (e * e + e) := cancel4 _ _ (by rw [← hv2]; lib_int)
      refine ⟨-(1 + -2 * Int.ofNat k + 4 * H2), Int.ofNat v, ?_, ⟨⟨c, hc⟩, ?_⟩, ?_⟩
      · rw [show -(1 + -2 * Int.ofNat k + 4 * H2) * -(1 + -2 * Int.ofNat k + 4 * H2) = Int.ofNat u * Int.ofNat u by
          cases hu with
          | inl e' => rw [e']; lib_int
          | inr e' => rw [e']]
        rw [hp]
        show _ = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1)
        rw [show 2 * (2 * k + 1) + 1 + 1 + 1 = k + k + k + k + 5 by lib_nat]
        show _ = Int.ofNat k + Int.ofNat k + Int.ofNat k + Int.ofNat k + 5
        lib_int
      · -- a + b − 1 = 2(k − 1 + c − 2H₂); parity of c is that of k + 1
        show ∃ q, -(1 + -2 * Int.ofNat k + 4 * H2) + Int.ofNat v - 1 = 4 * q
        rw [hc]
        match GaussInt.int_even_odd e, GaussInt.int_even_odd c with
        | Or.inl ⟨f0, hf⟩, Or.inl ⟨d, hd⟩ =>
          refine ⟨2 * d * d + 2 * f0 * f0 + f0 + d + -1 + -1 * H2, ?_⟩
          have hk : Int.ofNat k = c * c + -1 + (e * e + e) := by rw [hcc]; lib_int
          rw [hk, hf, hd]; lib_int
        | Or.inl ⟨f0, hf⟩, Or.inr ⟨d, hd⟩ =>
          refine ⟨2 * d * d + 2 * d + 2 * f0 * f0 + f0 + d + -1 * H2, ?_⟩
          have hk : Int.ofNat k = c * c + -1 + (e * e + e) := by rw [hcc]; lib_int
          rw [hk, hf, hd]; lib_int
        | Or.inr ⟨f0, hf⟩, Or.inl ⟨d, hd⟩ =>
          refine ⟨2 * d * d + d + 2 * f0 * f0 + 3 * f0 + -1 * H2, ?_⟩
          have hk : Int.ofNat k = c * c + -1 + (e * e + e) := by rw [hcc]; lib_int
          rw [hk, hf, hd]; lib_int
        | Or.inr ⟨f0, hf⟩, Or.inr ⟨d, hd⟩ =>
          refine ⟨2 * d * d + 2 * d + 2 * f0 * f0 + 3 * f0 + 1 + d + -1 * H2, ?_⟩
          have hk : Int.ofNat k = c * c + -1 + (e * e + e) := by rw [hcc]; lib_int
          rw [hk, hf, hd]; lib_int
      · rw [hs]; lib_int

end SignCM3

#print axioms SignCM3.I_i_one
#print axioms SignCM3.odd_sq
#print axioms SignCM3.not_odd_mult4
#print axioms SignCM3.cancel4
#print axioms SignCM3.ap_primary

Listing 49: lang/HasseCM.lean

lang/HasseCM.lean

/-
  lang/HasseCM — T43, cycle 28: the Hasse invariant of y² = x³ − x at p = 4k + 5.

  p = J + 2 prime, h = (p − 1)/2. Writing (x³ − x)^h = x^h (x² − 1)^h = x^h Σ_i C(h, i) x^(2i) (−1)^(h−i)
  (Frobenius.ev_row) and summing over x with the power sums of lang/PowerSum:
    ps_low, ps_top, ps_high   Σ_x x^e = 0 (1 ≤ e ≤ p−2), = −1 (e = p−1), = 0 (p ≤ e ≤ 2p−3)
    T, T_cons, T_zero, T_skip, skip_many   Σ_x x^e · ev(x², −1, cs), one coefficient at a time
    cub_factor                x³ − x = x (x² − 1)
    hasse_cm                  for p = 4k + 5:  Σ_x (x³ − x)^((p−1)/2) = −(−1)^(k+1) · C(2k + 2, k + 1)
  Since #E(𝔽_p) = p + 1 + Σ_x χ(x³ − x), this gives a_p ≡ (−1)^(k+1) C((p−1)/2, (p−1)/4) (mod p) at every split
  prime — the Hasse invariant; with |a_p| < 2√p it fixes a_p (next: C((p−1)/2, (p−1)/4) ≡ ±2u (mod p),
  p = u² + v², Gauss).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PowerSum

namespace HasseCM

open FTAChain ZMod YMChain Frobenius

variable (J : Nat)

def ps (e : Nat) : Zp (J + 1) :=
  sumK (ring (J + 1)) (fun x => pw (ring (J + 1)) x e) (CMInert.allZ (J + 1) (J + 1 + 1))

theorem pw_add' {R : Type} (K : DetChain.CRing R) (x : R) (a : Nat) : ∀ b, pw K x (a + b) = K.mul (pw K x a) (pw K x b)
  | 0 => by show pw K x a = K.mul (pw K x a) K.one; rw [K.mul_comm, K.one_mul]
  | b + 1 => by
    show K.mul (pw K x (a + b)) x = K.mul (pw K x a) (K.mul (pw K x b) x)
    rw [pw_add' K x a b, K.mul_assoc]

theorem pw_zero_succ {R : Type} (K : DetChain.CRing R) (r : Nat) : pw K K.zero (r + 1) = K.zero :=
  DetChain.mul_zero K _

theorem ps_low (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (e : Nat) (h1 : 1 ≤ e) (h2 : e ≤ J) :
    ps J e = (ring (J + 1)).zero := by
  cases e with
  | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
  | succ e' => exact PowerSum.power_sum_zero J hpr e' h2

theorem ps_top (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) : ps J (J + 1) = (ring (J + 1)).neg (ring (J + 1)).one :=
  PowerSum.power_sum_top J hpr

theorem ps_high (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (r : Nat) (h1 : 1 ≤ r) (h2 : r ≤ J) :
    ps J (J + 1 + r) = (ring (J + 1)).zero := by
  have e : ps J (J + 1 + r) = ps J r := sumK_ext (ring (J + 1)) _ _ (fun x => by
    show pw (ring (J + 1)) x (J + 1 + r) = pw (ring (J + 1)) x r
    by_cases hx : x = (ring (J + 1)).zero
    · rw [hx]
      cases r with
      | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
      | succ r' => rw [pw_zero_succ, ← Nat.add_assoc, pw_zero_succ]
    · rw [pw_add' (ring (J + 1)) x (J + 1) r, unit_pow J hpr x hx, (ring (J + 1)).one_mul]) _
  rw [e]
  exact ps_low J hpr r h1 h2

def evs (x : Zp (J + 1)) (cs : List Nat) : Zp (J + 1) := ev (ring (J + 1)) ((ring (J + 1)).mul x x) ((ring (J + 1)).neg (ring (J + 1)).one) cs

def T (e : Nat) (cs : List Nat) : Zp (J + 1) :=
  sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x e) (evs J x cs)) (CMInert.allZ (J + 1) (J + 1 + 1))

theorem T_cons (e c : Nat) (cs : List Nat) :
    T J e (c :: cs) = (ring (J + 1)).add ((ring (J + 1)).mul (nsm (ring (J + 1)) c (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) cs.length)) (ps J e)) (T J (e + 2) cs) := by
  have pt : ∀ x : Zp (J + 1), (ring (J + 1)).mul (pw (ring (J + 1)) x e) (evs J x (c :: cs))
      = (ring (J + 1)).add ((ring (J + 1)).mul (nsm (ring (J + 1)) c (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) cs.length)) (pw (ring (J + 1)) x e)) ((ring (J + 1)).mul (pw (ring (J + 1)) x (e + 2)) (evs J x cs)) :=
    fun x => by
      show (ring (J + 1)).mul (pw (ring (J + 1)) x e) ((ring (J + 1)).add (nsm (ring (J + 1)) c (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) cs.length)) ((ring (J + 1)).mul ((ring (J + 1)).mul x x) (evs J x cs))) = _
      rw [(ring (J + 1)).distrib, (ring (J + 1)).mul_comm (pw (ring (J + 1)) x e) (nsm (ring (J + 1)) c _)]
      show _ = (ring (J + 1)).add _ ((ring (J + 1)).mul ((ring (J + 1)).mul ((ring (J + 1)).mul (pw (ring (J + 1)) x e) x) x) (evs J x cs))
      rw [(ring (J + 1)).mul_assoc ((ring (J + 1)).mul (pw (ring (J + 1)) x e) x) x, (ring (J + 1)).mul_assoc (pw (ring (J + 1)) x e) x, ← (ring (J + 1)).mul_assoc x x]
  show sumK (ring (J + 1)) _ _ = _
  rw [sumK_ext (ring (J + 1)) _ _ pt, sumK_add, ← mul_sumK]
  rfl

theorem idx2 (e i : Nat) : e + 2 + 2 * i = e + 2 * (i + 1) := by
  rw [Nat.add_assoc, Nat.add_comm 2 (2 * i)]
  rfl

theorem sumK_zero' {A : Type} : ∀ l : List A, sumK (ring (J + 1)) (fun _ => (ring (J + 1)).zero) l = (ring (J + 1)).zero
  | [] => rfl
  | _ :: l => by
    show (ring (J + 1)).add (ring (J + 1)).zero (sumK (ring (J + 1)) (fun _ => (ring (J + 1)).zero) l) = (ring (J + 1)).zero
    rw [sumK_zero' l, (ring (J + 1)).zero_add]

theorem T_nil (e : Nat) : T J e [] = (ring (J + 1)).zero := by
  show sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x e) (ring (J + 1)).zero) _ = _
  rw [sumK_ext (ring (J + 1)) _ (fun _ => (ring (J + 1)).zero) (fun x => DetChain.mul_zero (ring (J + 1)) _), sumK_zero']

theorem T_zero : ∀ (cs : List Nat) (e : Nat), (∀ i, i < cs.length → ps J (e + 2 * i) = (ring (J + 1)).zero) → T J e cs = (ring (J + 1)).zero
  | [], e, _ => T_nil J e
  | c :: cs, e, h => by
    have h0 : ps J e = (ring (J + 1)).zero := h 0 (Nat.zero_lt_succ _)
    rw [T_cons, h0, DetChain.mul_zero, (ring (J + 1)).zero_add]
    exact T_zero cs (e + 2) (fun i hi => by rw [idx2]; exact h (i + 1) (Nat.succ_lt_succ hi))

theorem T_skip (e c : Nat) (cs : List Nat) (h : ps J e = (ring (J + 1)).zero) : T J e (c :: cs) = T J (e + 2) cs := by
  rw [T_cons, h, DetChain.mul_zero, (ring (J + 1)).zero_add]

theorem skip_many (n : Nat) (m : Nat) : ∀ (i j e : Nat), (∀ i', i' < i → ps J (e + 2 * i') = (ring (J + 1)).zero) →
    T J e (Frobenius.listC n j (i + m)) = T J (e + 2 * i) (Frobenius.listC n (j + i) m)
  | 0, j, e, _ => by rw [Nat.zero_add]; rfl
  | i + 1, j, e, h => by
    rw [Nat.succ_add]
    show T J e (Frobenius.C n j :: Frobenius.listC n (j + 1) (i + m)) = _
    rw [T_skip J e _ _ (h 0 (Nat.zero_lt_succ _) : ps J e = _),
      skip_many n m i (j + 1) (e + 2) (fun i' hi' => by rw [idx2]; exact h (i' + 1) (Nat.succ_lt_succ hi')),
      idx2, Nat.add_assoc j 1 i, Nat.add_comm 1 i]

theorem cub_factor (x : Zp (J + 1)) :
    CMInert.cub (ring (J + 1)) x = (ring (J + 1)).mul x ((ring (J + 1)).add ((ring (J + 1)).mul x x) ((ring (J + 1)).neg (ring (J + 1)).one)) := by
  show (ring (J + 1)).add ((ring (J + 1)).mul ((ring (J + 1)).mul x x) x) ((ring (J + 1)).neg x) = _
  rw [(ring (J + 1)).distrib, mul_neg_r (ring (J + 1)), (ring (J + 1)).mul_comm x (ring (J + 1)).one, (ring (J + 1)).one_mul, (ring (J + 1)).mul_comm ((ring (J + 1)).mul x x) x]

theorem ar1 (k : Nat) : 2 * k + 2 + 1 = k + 1 + (k + 2) := by
  rw [Nat.two_mul, Nat.add_assoc k k 2, Nat.add_assoc k (k + 2) 1, Nat.add_assoc k 1 (k + 2), Nat.add_comm 1 (k + 2)]

theorem ar2 (k : Nat) : 2 * k + 2 + 2 * (k + 1) = 2 * (2 * k + 1) + 1 + 1 := by
  show 2 * k + 2 + (2 * k + 2) = 2 * (2 * k) + 2 + 1 + 1
  rw [Nat.two_mul (2 * k), ← Nat.add_assoc, Nat.add_right_comm (2 * k) 2 (2 * k)]

theorem ar3 (k i : Nat) (hi : i ≤ k) : 2 * k + 2 + 2 * i ≤ 2 * (2 * k + 1) + 1 := by
  have a : 2 * k + 2 + 2 * i ≤ 2 * k + 2 + 2 * k := Nat.add_le_add_left (Nat.mul_le_mul_left 2 hi) _
  have b : 2 * k + 2 + 2 * k = 2 * (2 * k + 1) := by
    show 2 * k + 2 + 2 * k = 2 * (2 * k) + 2
    rw [Nat.two_mul (2 * k), Nat.add_right_comm (2 * k) 2 (2 * k)]
  rw [b] at a
  exact Nat.le_succ_of_le a

theorem ar4 (k i : Nat) (hi : i ≤ k) : 2 + 2 * i ≤ 2 * (2 * k + 1) + 1 :=
  Nat.le_trans (Nat.add_le_add_right (Nat.le_add_left 2 (2 * k)) (2 * i)) (ar3 k i hi)

/-- **The Hasse invariant of y² = x³ − x** at p = 4k + 5:
    `Σ_x (x³ − x)^((p−1)/2) = −(−1)^(k+1) C(2k+2, k+1)`. -/
theorem hasse_cm (k : Nat) (hpr : ∀ d, Dv d ((2 * (2 * k + 1) + 1) + 1 + 1) → d = 1 ∨ d = (2 * (2 * k + 1) + 1) + 1 + 1) :
    sumK (ring (2 * (2 * k + 1) + 1 + 1)) (fun x => pw (ring (2 * (2 * k + 1) + 1 + 1)) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x) (2 * k + 2)) (CMInert.allZ ((2 * (2 * k + 1) + 1) + 1) ((2 * (2 * k + 1) + 1) + 1 + 1))
      = (ring (2 * (2 * k + 1) + 1 + 1)).neg (nsm (ring (2 * (2 * k + 1) + 1 + 1)) (Frobenius.C (2 * k + 2) (k + 1)) (pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1))) := by
  have eA : sumK (ring (2 * (2 * k + 1) + 1 + 1)) (fun x => pw (ring (2 * (2 * k + 1) + 1 + 1)) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x) (2 * k + 2)) (CMInert.allZ ((2 * (2 * k + 1) + 1) + 1) ((2 * (2 * k + 1) + 1) + 1 + 1))
      = T (2 * (2 * k + 1) + 1) (2 * k + 2) (Frobenius.row (2 * k + 2)) :=
    sumK_ext (ring (2 * (2 * k + 1) + 1 + 1)) _ _ (fun x => by
      rw [cub_factor (2 * (2 * k + 1) + 1) x, pw_mul, ← ev_row]
      rfl) _
  rw [eA, Frobenius.row_eq, ar1 k,
    skip_many (2 * (2 * k + 1) + 1) (2 * k + 2) (k + 2) (k + 1) 0 (2 * k + 2) (fun i' hi' =>
      ps_low (2 * (2 * k + 1) + 1) hpr _ (Nat.le_trans (Nat.succ_le_succ (Nat.zero_le _)) (Nat.le_add_right _ _))
        (ar3 k i' (Nat.le_of_lt_succ hi'))),
    Nat.zero_add]
  show T (2 * (2 * k + 1) + 1) (2 * k + 2 + 2 * (k + 1))
    (Frobenius.C (2 * k + 2) (k + 1) :: Frobenius.listC (2 * k + 2) (k + 1 + 1) (k + 1)) = _
  rw [T_cons, Frobenius.len_listC, ar2 k, ps_top (2 * (2 * k + 1) + 1) hpr,
    T_zero (2 * (2 * k + 1) + 1) _ _ (fun i hi => by
      rw [Frobenius.len_listC] at hi
      rw [Nat.add_assoc (2 * (2 * k + 1) + 1 + 1) 2 (2 * i)]
      exact ps_high (2 * (2 * k + 1) + 1) hpr _ (Nat.le_trans (Nat.succ_le_succ (Nat.zero_le 1)) (Nat.le_add_right 2 _))
        (ar4 k i (Nat.le_of_lt_succ hi))),
    DetChain.add_zero', mul_neg_r (ring (2 * (2 * k + 1) + 1 + 1)), (ring (2 * (2 * k + 1) + 1 + 1)).mul_comm _ (ring (2 * (2 * k + 1) + 1 + 1)).one, (ring (2 * (2 * k + 1) + 1 + 1)).one_mul]

end HasseCM

#print axioms HasseCM.pw_add'
#print axioms HasseCM.pw_zero_succ
#print axioms HasseCM.ps_low
#print axioms HasseCM.ps_top
#print axioms HasseCM.ps_high
#print axioms HasseCM.T_cons
#print axioms HasseCM.idx2
#print axioms HasseCM.sumK_zero'
#print axioms HasseCM.T_nil
#print axioms HasseCM.T_zero
#print axioms HasseCM.T_skip
#print axioms HasseCM.skip_many
#print axioms HasseCM.cub_factor
#print axioms HasseCM.ar1
#print axioms HasseCM.ar2
#print axioms HasseCM.ar3
#print axioms HasseCM.ar4
#print axioms HasseCM.hasse_cm

Listing 50: lang/GaussCong.lean

lang/GaussCong.lean

/-
  lang/GaussCong — T43, cycle 48: Gauss's congruence (1825). For p = 4k + 5 = u² + v² (u, v from CMModular):
      C(2k + 2, k + 1) ≡ ±2u  (mod p).

  The integer a_p-sum S = Σ_x χ(x³ − x) = ±2u (CMModular.cm_trace) is reduced mod p (IntCast): castZ(χ(z)) = z^((p−1)/2)
  (Euler), so castZ(S) = Σ_x (x³ − x)^((p−1)/2) = −(−1)^(k+1) C(2k + 2, k + 1) (HasseCM.hasse_cm). Hence
    cast_chi        castZ(χ(z)) = z^((p−1)/2)
    negone_pow      (−1)^n = ±1
    gauss_cong      mk C(2k+2, k+1) = mk (2u) or = −mk (2u), with u² + v² = p
  The Hasse invariant (a_p mod p) and the Hecke-character trace (a_p in ℤ) agree: the two halves of the CM
  correspondence for y² = x³ − x meet in Gauss's binomial congruence.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CMModular
import lang.IntCast
import lang.HasseCM

namespace GaussCong

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum JacobMoment LibRing

variable (k : Nat)

theorem cast_chi (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1) (z : Zp (2 * (2 * k + 1) + 1 + 1)) :
    castZ (ring (2 * (2 * k + 1) + 1 + 1)) (chi (2 * k + 1) z) = Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) z ((2 * k + 1) + 1) := by
  by_cases hz : z = (ring (2 * (2 * k + 1) + 1 + 1)).zero
  · rw [hz, chi_zero]
    exact (HasseCM.pw_zero_succ (ring (2 * (2 * k + 1) + 1 + 1)) (2 * k + 1)).symm
  · cases Euler.euler_pm (2 * k + 1) hpr z hz with
    | inl h =>
      rw [chi_one_of (2 * k + 1) z hz h, h]
      show (ring (2 * (2 * k + 1) + 1 + 1)).add (ring (2 * (2 * k + 1) + 1 + 1)).one (ring (2 * (2 * k + 1) + 1 + 1)).zero = (ring (2 * (2 * k + 1) + 1 + 1)).one
      exact DetChain.add_zero' (ring (2 * (2 * k + 1) + 1 + 1)) _
    | inr h =>
      have h' : Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) z ((2 * k + 1) + 1) ≠ (ring (2 * (2 * k + 1) + 1 + 1)).one := fun e => ne_pm (2 * k + 1) (h.symm.trans e)
      rw [chi_neg_of (2 * k + 1) z hz h', h]
      show (ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).add (ring (2 * (2 * k + 1) + 1 + 1)).one (ring (2 * (2 * k + 1) + 1 + 1)).zero) = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one
      rw [DetChain.add_zero']

theorem negone_pow : ∀ n, Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) n = (ring (2 * (2 * k + 1) + 1 + 1)).one ∨
    Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) n = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one
  | 0 => Or.inl rfl
  | n + 1 => by
    cases negone_pow n with
    | inl h =>
      right
      show (ring (2 * (2 * k + 1) + 1 + 1)).mul (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) n) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one
      rw [h, (ring (2 * (2 * k + 1) + 1 + 1)).one_mul]
    | inr h =>
      left
      show (ring (2 * (2 * k + 1) + 1 + 1)).mul (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) n) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) = (ring (2 * (2 * k + 1) + 1 + 1)).one
      rw [h]; exact TwoPower.negone_sq' _

theorem nsm_mk (n : Nat) : Frobenius.nsm (ring (2 * (2 * k + 1) + 1 + 1)) n (ring (2 * (2 * k + 1) + 1 + 1)).one = mk _ n :=
  Subtype.ext (nsm_one_val _ n)

/-- **Gauss's congruence.** -/
theorem gauss_cong (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1) :
    ∃ u v : Nat, u * u + v * v = 2 * (2 * k + 1) + 1 + 1 + 1 ∧
      (mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1)) = mk (2 * (2 * k + 1) + 1 + 1) (2 * u) ∨
       mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1)) = (ring (2 * (2 * k + 1) + 1 + 1)).neg (mk (2 * (2 * k + 1) + 1 + 1) (2 * u))) := by
  match CMModular.cm_trace k hpr with
  | ⟨u, v, huv, hS⟩ =>
    refine ⟨u, v, huv, ?_⟩
    have hc : castZ (ring (2 * (2 * k + 1) + 1 + 1)) (sumK LibInt.intR (fun x => chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (L (2 * k + 1)))
        = (ring (2 * (2 * k + 1) + 1 + 1)).neg (Frobenius.nsm (ring (2 * (2 * k + 1) + 1 + 1)) (Frobenius.C (2 * k + 2) (k + 1)) (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1))) := by
      rw [IntCast.castZ_sum]
      rw [sumK_ext (ring (2 * (2 * k + 1) + 1 + 1)) _ _ (fun x => cast_chi k hpr (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x))]
      exact HasseCM.hasse_cm k hpr
    rw [Frobenius.nsm_eq_mul, nsm_mk] at hc
    have key : ∀ X s : Zp (2 * (2 * k + 1) + 1 + 1), (s = (ring (2 * (2 * k + 1) + 1 + 1)).one ∨ s = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) →
        (X = mk (2 * (2 * k + 1) + 1 + 1) (2 * u) ∨ X = (ring (2 * (2 * k + 1) + 1 + 1)).neg (mk (2 * (2 * k + 1) + 1 + 1) (2 * u))) →
        X = (ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).mul (mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1))) s) →
        (mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1)) = mk (2 * (2 * k + 1) + 1 + 1) (2 * u) ∨
         mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1)) = (ring (2 * (2 * k + 1) + 1 + 1)).neg (mk (2 * (2 * k + 1) + 1 + 1) (2 * u))) := by
      intro X s hs hX e
      have nn : ∀ y : Zp (2 * (2 * k + 1) + 1 + 1), (ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).neg y) = y := DetChain.neg_neg' (ring (2 * (2 * k + 1) + 1 + 1))
      cases hs with
      | inl hs1 =>
        rw [hs1, (ring (2 * (2 * k + 1) + 1 + 1)).mul_comm, (ring (2 * (2 * k + 1) + 1 + 1)).one_mul] at e
        cases hX with
        | inl h => right; rw [← nn (mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1))), ← e, h]
        | inr h => left; rw [← nn (mk (2 * (2 * k + 1) + 1 + 1) (Frobenius.C (2 * k + 2) (k + 1))), ← e, h, nn]
      | inr hs1 =>
        rw [hs1, mul_neg_r (ring (2 * (2 * k + 1) + 1 + 1)), (ring (2 * (2 * k + 1) + 1 + 1)).mul_comm, (ring (2 * (2 * k + 1) + 1 + 1)).one_mul, nn] at e
        cases hX with
        | inl h => left; rw [← e, h]
        | inr h => right; rw [← e, h]
    apply key _ _ (negone_pow k (k + 1)) _ hc
    cases hS with
    | inl h => left; rw [h]; exact IntCast.castN_mk _ _
    | inr h => right; rw [h, IntCast.castZ_neg_nat, IntCast.castN_mk]

end GaussCong

#print axioms GaussCong.cast_chi
#print axioms GaussCong.negone_pow
#print axioms GaussCong.nsm_mk
#print axioms GaussCong.gauss_cong

78.22 GL\(_2/\mathbb Q\) CM: \(y^2=x^3+1\)

With \(a_p=-\psi (1)\), \(\psi (a)=\sum _x\chi (x^3+a)\),

\[ \begin {aligned} p\equiv 2\ (3)&:\ a_p=0,\\ p\equiv 1\ (3)&:\ a_p=2a,\ p=a^2+3b^2,\ a\equiv 1\ (3),\\ p=6k+7&:\ a_p\equiv \tbinom {3k+3}{2k+2}\ (p). \end {aligned} \]
The steps are
\[ \begin {aligned} 3e=2(p-1)+1&\Rightarrow x\mapsto x^3\ \text {is a bijection},\ \textstyle \sum _x\chi (x^3+1)=\sum _x\chi (x)=0,\\ A_i=\psi (g^i):\ A_0^2+A_1^2+A_2^2&=6p,\qquad A_0-A_1+A_2=0,\\ (A_2+c)^2+3c^2&=3p,\ \psi (1)=2c\ \Rightarrow \ \psi (1)=\pm 2u,\ p=u^2+3w^2,\\ \textstyle \sum _x G(x^3)&=G(0)+3\textstyle \sum _{k<N}G(g^{3k})\ \Rightarrow \ \psi (1)\equiv 1\ (3). \end {aligned} \]

Listing 51: lang/CMEisen.lean

lang/CMEisen.lean

/-
  lang/CMEisen — T43, cycle 39 (GL2/ℚ, CM by ℤ[ω]): a_p(y² = x³ + 1) = 0 at every prime p ≡ 2 (mod 3).

  p = J + 2 prime, ℤ/p = ring (J + 1). If 3e = 2(p − 1) + 1 (possible exactly when p ≡ 2 mod 3), x ↦ x³ is a bijection
  of 𝔽_p with inverse x ↦ x^e. Then
    cube_inv, inv_cube   (x³)^e = x = (x^e)³
    sum_cube             Σ_x F(x³) = Σ_x F(x)
    sum_shift            Σ_x F(x + 1) = Σ_x F(x)
    char_sum_cube        Σ_x (x³ + 1)^h = Σ_x x^h = 0 for 1 ≤ h ≤ p − 2      (PowerSum)
    cm_eisen_inert       p = 6j + 5:  Σ_x (x³ + 1)^((p−1)/2) = 0, i.e. a_p = 0
  The inert primes of ℤ[ω] (Eisen: −3 is not a square) have a_p = 0 — the Galois side of the induced representation
  Ind ψ (Dihedral.ind_inert) — matching the automorphic side, as CMInert did for ℤ[i].

  Lean 4 core only, proofs by hand (lib_nat from lib_shared), no axioms.
-/
import lang.HasseCM
import lib.Semi

namespace CMEisen

open FTAChain ZMod YMChain FiniteRecChain Frobenius

variable (J : Nat)

theorem pw_self (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (e : Nat) (he : 3 * e = 2 * (J + 1) + 1)
    (x : Zp (J + 1)) : pw (ring (J + 1)) x (3 * e) = x := by
  rw [he]
  by_cases hx : x = (ring (J + 1)).zero
  · rw [hx]; exact HasseCM.pw_zero_succ (ring (J + 1)) _
  · show (ring (J + 1)).mul (pw (ring (J + 1)) x (2 * (J + 1))) x = x
    rw [show 2 * (J + 1) = (J + 1) * 2 by lib_nat, ← TwoPower.pwpow, unit_pow J hpr x hx, TwoPower.pwone, (ring (J + 1)).one_mul]

theorem cube_inv (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (e : Nat) (he : 3 * e = 2 * (J + 1) + 1)
    (x : Zp (J + 1)) : pw (ring (J + 1)) (pw (ring (J + 1)) x 3) e = x := by
  rw [TwoPower.pwpow]; exact pw_self J hpr e he x

theorem inv_cube (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (e : Nat) (he : 3 * e = 2 * (J + 1) + 1)
    (x : Zp (J + 1)) : pw (ring (J + 1)) (pw (ring (J + 1)) x e) 3 = x := by
  rw [TwoPower.pwpow, show e * 3 = 3 * e by lib_nat]; exact pw_self J hpr e he x

theorem sum_cube (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (e : Nat) (he : 3 * e = 2 * (J + 1) + 1)
    (F : Zp (J + 1) → Zp (J + 1)) :
    sumK (ring (J + 1)) (fun x => F (pw (ring (J + 1)) x 3)) (CMInert.allZ (J + 1) (J + 1 + 1)) = sumK (ring (J + 1)) F (CMInert.allZ (J + 1) (J + 1 + 1)) :=
  reindex (ring (J + 1)) _ (CMInert.complete_allZ (J + 1) (ring (J + 1))) (fun x => pw (ring (J + 1)) x 3) (fun x => pw (ring (J + 1)) x e)
    (cube_inv J hpr e he) (inv_cube J hpr e he) F

theorem sum_shift (F : Zp (J + 1) → Zp (J + 1)) :
    sumK (ring (J + 1)) (fun x => F ((ring (J + 1)).add x (ring (J + 1)).one)) (CMInert.allZ (J + 1) (J + 1 + 1)) = sumK (ring (J + 1)) F (CMInert.allZ (J + 1) (J + 1 + 1)) :=
  reindex (ring (J + 1)) _ (CMInert.complete_allZ (J + 1) (ring (J + 1))) (fun x => (ring (J + 1)).add x (ring (J + 1)).one) (fun x => (ring (J + 1)).add x ((ring (J + 1)).neg (ring (J + 1)).one))
    (fun x => by rw [(ring (J + 1)).add_assoc, (ring (J + 1)).add_neg, DetChain.add_zero'])
    (fun y => by rw [(ring (J + 1)).add_assoc, DetChain.add_neg', DetChain.add_zero']) F

/-- **The character sum of y² = x³ + 1 vanishes when cubing is a bijection.** -/
theorem char_sum_cube (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (e : Nat) (he : 3 * e = 2 * (J + 1) + 1)
    (h : Nat) (h1 : 1 ≤ h) (h2 : h ≤ J) :
    sumK (ring (J + 1)) (fun x => pw (ring (J + 1)) ((ring (J + 1)).add (pw (ring (J + 1)) x 3) (ring (J + 1)).one) h) (CMInert.allZ (J + 1) (J + 1 + 1)) = (ring (J + 1)).zero := by
  rw [sum_cube J hpr e he (fun y => pw (ring (J + 1)) ((ring (J + 1)).add y (ring (J + 1)).one) h), sum_shift J (fun y => pw (ring (J + 1)) y h)]
  exact HasseCM.ps_low J hpr h h1 h2

/-- **a_p(y² = x³ + 1) = 0 at p = 6j + 5.** -/
theorem cm_eisen_inert (j : Nat) (hpr : ∀ d, Dv d (6 * j + 3 + 1 + 1) → d = 1 ∨ d = 6 * j + 3 + 1 + 1) :
    sumK (ring (6 * j + 3 + 1)) (fun x => pw (ring (6 * j + 3 + 1)) ((ring (6 * j + 3 + 1)).add (pw (ring (6 * j + 3 + 1)) x 3)
      (ring (6 * j + 3 + 1)).one) (3 * j + 2)) (CMInert.allZ (6 * j + 3 + 1) (6 * j + 3 + 1 + 1))
      = (ring (6 * j + 3 + 1)).zero :=
  char_sum_cube (6 * j + 3) hpr (4 * j + 3) (by lib_nat) (3 * j + 2) (Nat.succ_le_succ (Nat.zero_le _))
    (by rw [show 6 * j + 3 = (3 * j + 2) + (3 * j + 1) by lib_nat]; exact Nat.le_add_right _ _)

end CMEisen

#print axioms CMEisen.pw_self
#print axioms CMEisen.cube_inv
#print axioms CMEisen.inv_cube
#print axioms CMEisen.sum_cube
#print axioms CMEisen.sum_shift
#print axioms CMEisen.char_sum_cube
#print axioms CMEisen.cm_eisen_inert

Listing 52: lang/CubicTrace.lean

lang/CubicTrace.lean

/-
  lang/CubicTrace — T43, cycle 54: the Frobenius trace of y² = x³ + 1 at p ≡ 1 (mod 3) is ±2u with p = u² + 3w².

  p = 2m + 3, p − 1 = 3N, g a primitive root, A_i = ψ(g^i) with ψ(a) = Σ_x χ(x³ + a).
    psi_zero       ψ(0) = 0
    chi_g          χ(g) = −1, χ(g²) = 1
    sumA2          A₀² + A₁² + A₂² = 6p            (CubicMoment.moment_c, CubicClass.group3, DLog.sum_split)
    sumA1          A₀ − A₁ + A₂ = 0                 (CubicJacob.twisted)
    psi_one_even   ψ(1) is even                     (#{x : x³ = −1} = 3, p odd)
    cubic_trace    ∃ u w, u² + 3w² = p ∧ Σ_x χ(x³ + 1) = ±2u
  Since a_p(y² = x³ + 1) = −Σ_x χ(x³ + 1), the trace at every split prime of ℤ[ω] is ±2u — the Hecke-character trace of
  ℚ(√−3) — and with CMEisen (a_p = 0 at p ≡ 2 mod 3) the CM correspondence for y² = x³ + 1 holds at every prime p > 3
  up to sign, as for y² = x³ − x (CMModular).

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.CubicClass
import lang.CubicMoment
import lang.CMModular
import lang.EisenNorm

namespace CubicTrace

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal JacobMoment CubicJacob CubicMoment DLog CubicClass OrdMul

variable (m : Nat)

theorem psi_zero (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : psi m (ring (2 * m + 1 + 1)).zero = 0 := by
  show sumK Z (fun x => chi m ((ring (2 * m + 1 + 1)).add (cube m x) (ring (2 * m + 1 + 1)).zero)) (L m) = 0
  rw [sumK_ext Z _ (chi m) (fun x => by rw [DetChain.add_zero']; exact chi_cube m hpr x)]
  exact chi_sum m hpr

theorem chi_g (g : Zp (2 * m + 1 + 1))
    (hg : Ord (ring (2 * m + 1 + 1)) g (2 * m + 1 + 1)) : chi m g = -1 := by
  have hg0 : g ≠ (ring (2 * m + 1 + 1)).zero := fun h => by
    have := hg.1; rw [h, HasseCM.pw_zero_succ] at this; exact PowerSum.one_ne_zero' (2 * m + 1) this.symm
  cases JacobClass.chi_cases m g with
  | inl h0 => exact absurd h0 (JacobClass.chi_ne_zero m g hg0)
  | inr h =>
    cases h with
    | inr h1 => exact h1
    | inl h1 =>
      exfalso
      have hp := JacobClass.chi_pw_one m g h1
      match hg.2 (m + 1) hp with
      | ⟨c, hc⟩ =>
        cases c with
        | zero => rw [Nat.mul_zero] at hc; exact Nat.noConfusion hc
        | succ c =>
          have : 2 * m + 1 + 1 ≤ m + 1 := by rw [hc, Nat.mul_succ]; exact Nat.le_add_left _ _
          have h2 : m + 1 < 2 * m + 1 + 1 := Nat.succ_lt_succ (by rw [Nat.two_mul]; exact Nat.lt_succ_of_le (Nat.le_add_left m m))
          exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h2 this)

theorem chi_gg (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (g : Zp (2 * m + 1 + 1))
    (hg0 : g ≠ (ring (2 * m + 1 + 1)).zero) : chi m ((ring (2 * m + 1 + 1)).mul g g) = 1 := chi_sq m hpr g hg0

def nq (z : Zp (2 * m + 1 + 1)) : Int := if chi m z = -1 then 1 else 0

theorem chi_split (z : Zp (2 * m + 1 + 1)) : chi m z = 1 + -1 * delta Z z (ring (2 * m + 1 + 1)).zero + -2 * nq m z := by
  by_cases hz : z = (ring (2 * m + 1 + 1)).zero
  · rw [hz, chi_zero, delta_one m _ _ rfl]
    show (0 : Int) = 1 + -1 * 1 + -2 * (if (0 : Int) = -1 then 1 else 0)
    rw [ite_neg' _ _ (fun e => Int.noConfusion e)]; rfl
  · rw [delta_zero m _ _ hz]
    unfold nq
    cases JacobClass.chi_cases m z with
    | inl h0 => exact absurd h0 (JacobClass.chi_ne_zero m z hz)
    | inr h =>
      cases h with
      | inl h1 => rw [h1, ite_neg' _ _ (fun e => Int.noConfusion e)]; rfl
      | inr h1 => rw [h1, ite_pos' _ _ rfl]; rfl

theorem delta_cube_neg (z : Zp (2 * m + 1 + 1)) :
    delta Z ((ring (2 * m + 1 + 1)).add (cube m z) (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).zero = delta Z (cube m ((ring (2 * m + 1 + 1)).neg z)) (ring (2 * m + 1 + 1)).one := by
  have ec : cube m ((ring (2 * m + 1 + 1)).neg z) = (ring (2 * m + 1 + 1)).neg (cube m z) := by unfold cube; lib_ring
  rw [ec]
  by_cases h : (ring (2 * m + 1 + 1)).add (cube m z) (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).zero
  · have h' : (ring (2 * m + 1 + 1)).neg (cube m z) = (ring (2 * m + 1 + 1)).one := by
      have := DetChain.neg_unique (ring (2 * m + 1 + 1)) _ _ h
      rw [this, DetChain.neg_neg']
    rw [delta_one m _ _ h, delta_one m _ _ h']
  · have h' : (ring (2 * m + 1 + 1)).neg (cube m z) ≠ (ring (2 * m + 1 + 1)).one := fun e => h (by rw [← e]; exact (ring (2 * m + 1 + 1)).add_neg _)
    rw [delta_zero m _ _ h, delta_zero m _ _ h']

/-- **ψ(1) is even**: ψ(1) = 2 (m − Σ nq). -/
theorem psi_one_even (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (w : Zp (2 * m + 1 + 1))
    (hw1 : w ≠ (ring (2 * m + 1 + 1)).one) (hw3 : cube m w = (ring (2 * m + 1 + 1)).one) :
    psi m (ring (2 * m + 1 + 1)).one = 2 * (Int.ofNat m + -1 * sumK Z (fun x => nq m ((ring (2 * m + 1 + 1)).add (cube m x) (ring (2 * m + 1 + 1)).one)) (L m)) := by
  show sumK Z (fun x => chi m ((ring (2 * m + 1 + 1)).add (cube m x) (ring (2 * m + 1 + 1)).one)) (L m) = _
  have pt : ∀ x, chi m ((ring (2 * m + 1 + 1)).add (cube m x) (ring (2 * m + 1 + 1)).one) = Z.add (Z.add 1 (Z.mul (-1) (delta Z (cube m ((ring (2 * m + 1 + 1)).neg x)) (ring (2 * m + 1 + 1)).one)))
      (Z.mul (-2) (nq m ((ring (2 * m + 1 + 1)).add (cube m x) (ring (2 * m + 1 + 1)).one))) := fun x => by
    rw [chi_split, delta_cube_neg]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, sum_one]
  have hneg : sumK Z (fun x => delta Z (cube m ((ring (2 * m + 1 + 1)).neg x)) (ring (2 * m + 1 + 1)).one) (L m) = 3 := by
    have hr := reindex Z (L m) (compL m) (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).neg (fun x => DetChain.neg_neg' (ring (2 * m + 1 + 1)) x)
      (fun x => DetChain.neg_neg' (ring (2 * m + 1 + 1)) x) (fun y => delta Z (cube m y) (ring (2 * m + 1 + 1)).one)
    rw [hr]
    exact count_roots m hpr w hw1 hw3
  rw [hneg]
  have hP : Int.ofNat (2 * m + 1 + 1 + 1) = Int.ofNat m + Int.ofNat m + 3 := by
    rw [show 2 * m + 1 + 1 + 1 = m + m + 3 by lib_nat]; rfl
  show Int.ofNat (2 * m + 1 + 1 + 1) + -1 * 3 + -2 * _ = _
  rw [hP]
  lib_int

theorem cancel_N (N : Nat) (hN : N ≠ 0) (X Y : Int) (h : Int.ofNat N * X = Int.ofNat N * Y) : X = Y := by
  have h0 : Int.ofNat N * (X + -1 * Y) = 0 := by
    rw [show Int.ofNat N * (X + -1 * Y) = Int.ofNat N * X + -1 * (Int.ofNat N * Y) by lib_int, h]; lib_int
  cases LibInt.int_noZD _ _ h0 with
  | inl h1 => exact absurd (Int.ofNat.inj h1) hN
  | inr h1 =>
    have h2 : X + -1 * Y = 0 := h1
    have : X = (X + -1 * Y) + Y := by lib_int
    rw [this, h2]; lib_int

/-- **The Frobenius trace of y² = x³ + 1 at p ≡ 1 (mod 3) is ±2u with p = u² + 3w².** -/
theorem cubic_trace (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (N : Nat)
    (hN : 2 * m + 1 + 1 = 3 * N) :
    ∃ u w : Nat, u * u + 3 * (w * w) = 2 * m + 1 + 1 + 1 ∧
      (psi m (ring (2 * m + 1 + 1)).one = Int.ofNat (2 * u) ∨ psi m (ring (2 * m + 1 + 1)).one = -Int.ofNat (2 * u)) := by
  have hN0 : N ≠ 0 := fun e => by rw [e] at hN; exact Nat.noConfusion hN
  match PrimRoot.primitive_root (2 * m + 1) hpr,
      CycloSplit.split_of_dvd (2 * m + 1) hpr 1 ⟨N, by rw [hN]; lib_nat⟩ with
  | ⟨g, hg⟩, ⟨w, hw1, hw3'⟩ =>
    have hw3 : cube m w = (ring (2 * m + 1 + 1)).one := by
      rw [← hw3', show (1 + 2 : Nat) = 3 from rfl, Eisen.pw3]; unfold cube; lib_ring
    have hg0 : g ≠ (ring (2 * m + 1 + 1)).zero := fun h => by
      have := hg.1; rw [h, HasseCM.pw_zero_succ] at this; exact PowerSum.one_ne_zero' (2 * m + 1) this.symm
    have hP : P m = 3 * Int.ofNat N + 1 := by
      show Int.ofNat (2 * m + 1 + 1 + 1) = _
      rw [hN]; rfl
    -- second moment, grouped
    have hpw : pows (2 * m + 1) g (2 * m + 1 + 1) = pows (2 * m + 1) g (3 * N) := congrArg (pows (2 * m + 1) g) hN
    have M := moment_c m hpr w hw1 hw3
    rw [show L m = CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1) from rfl,
      sum_split (2 * m + 1) Z hpr g hg, hpw,
      group3 (2 * m + 1) hpr g hg0 (fun a => psi m a * psi m a) (fun a t ht => psi_sq_const m hpr a t ht) N,
      psi_zero m hpr] at M
    have S2 : psi m (ring (2 * m + 1 + 1)).one * psi m (ring (2 * m + 1 + 1)).one + psi m g * psi m g + psi m ((ring (2 * m + 1 + 1)).mul g g) * psi m ((ring (2 * m + 1 + 1)).mul g g)
        = 6 * P m := by
      apply cancel_N N hN0
      have : Int.ofNat N * (6 * P m) = 2 * P m * (P m + -1) := by rw [hP]; lib_int
      rw [this, ← M]
      show Int.ofNat N * _ = (0 * 0 : Int) + Int.ofNat N * _
      lib_int
    -- twisted sum, grouped
    have T := twisted m hpr
    rw [show L m = CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1) from rfl,
      sum_split (2 * m + 1) Z hpr g hg, hpw,
      group3 (2 * m + 1) hpr g hg0 (fun a => chi m a * psi m a) (fun a t ht => chi_psi_const m hpr a t ht) N,
      chi_zero, chi_one, chi_g m g hg, chi_gg m hpr g hg0] at T
    have S1 : psi m (ring (2 * m + 1 + 1)).one + -1 * psi m g + psi m ((ring (2 * m + 1 + 1)).mul g g) = 0 := by
      apply cancel_N N hN0
      rw [show Int.ofNat N * 0 = 0 by lib_int, ← T]
      show _ = 0 * psi m (ring (2 * m + 1 + 1)).zero + _
      lib_int
    -- ψ(1) = 2c
    have hE := psi_one_even m hpr w hw1 hw3
    generalize hc : Int.ofNat m + -1 * sumK Z (fun x => nq m ((ring (2 * m + 1 + 1)).add (cube m x) (ring (2 * m + 1 + 1)).one)) (L m) = c at hE
    -- (A₂ + c)² + 3c² = 3p
    have key : (psi m ((ring (2 * m + 1 + 1)).mul g g) + c) * (psi m ((ring (2 * m + 1 + 1)).mul g g) + c) + 3 * (c * c) = 3 * P m := by
      have h1 : psi m g = psi m (ring (2 * m + 1 + 1)).one + psi m ((ring (2 * m + 1 + 1)).mul g g) := by
        have e : psi m (ring (2 * m + 1 + 1)).one + psi m ((ring (2 * m + 1 + 1)).mul g g)
            = (psi m (ring (2 * m + 1 + 1)).one + -1 * psi m g + psi m ((ring (2 * m + 1 + 1)).mul g g)) + psi m g := by lib_int
        rw [S1] at e
        rw [e]; lib_int
      rw [h1, hE] at S2
      apply cancel_N 2 (fun e => Nat.noConfusion e)
      show (2 : Int) * _ = 2 * _
      rw [show (2 : Int) * (3 * P m) = 6 * P m by lib_int, ← S2]
      lib_int
    match CMModular.nat_sq (psi m ((ring (2 * m + 1 + 1)).mul g g) + c), CMModular.nat_sq c with
    | ⟨n, hn, _⟩, ⟨u, hu, hsu⟩ =>
      rw [hn, hu] at key
      have key' : Int.ofNat (n * n + 3 * (u * u)) = Int.ofNat (3 * (2 * m + 1 + 1 + 1)) := key
      have hnat := Int.ofNat.inj key'
      have h3 : 3 ∣ n * n := by
        have a : Dv (2 + 1) (3 * (u * u) + n * n) := ⟨2 * m + 1 + 1 + 1, by rw [Nat.add_comm, hnat]; lib_nat⟩
        have b : Dv (2 + 1) (3 * (u * u)) := ⟨u * u, by lib_nat⟩
        exact PrimRoot.dvd_of_dv (dv_sub 2 _ _ b a)
      have h3' : 3 ∣ n := by
        cases LibGcd.euclid_prime EisenNorm.prime3' h3 with
        | inl x => exact x
        | inr x => exact x
      match h3' with
      | ⟨wv, ewv⟩ =>
        refine ⟨u, wv, ?_, ?_⟩
        · apply Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 2)
          rw [← hnat, ewv]; lib_nat
        · rw [hE]
          cases hsu with
          | inl e => left; rw [e]; show 2 * Int.ofNat u = Int.ofNat (2 * u); rfl
          | inr e => right; rw [e]; show 2 * -Int.ofNat u = -Int.ofNat (2 * u)
                     show 2 * -Int.ofNat u = -(2 * Int.ofNat u); lib_int

end CubicTrace

#print axioms CubicTrace.psi_zero
#print axioms CubicTrace.chi_g
#print axioms CubicTrace.chi_gg
#print axioms CubicTrace.chi_split
#print axioms CubicTrace.delta_cube_neg
#print axioms CubicTrace.psi_one_even
#print axioms CubicTrace.cancel_N
#print axioms CubicTrace.cubic_trace

Listing 53: lang/CubicSign.lean

lang/CubicSign.lean

/-
  lang/CubicSign — T43, cycle 59: y² = x³ + 1 at p ≡ 1 (mod 3): Σ_x χ(x³ + 1) ≡ 1 (mod 3).

  p = J + 2, g of order p − 1 = 3N. For G : 𝔽_p → ℤ, x ↦ G(x³) takes each value three times on 𝔽_p^×:
    sumR, sumR_pows    Σ_{s ∈ pows n} F(s) = Σ_{k < n} F(g^k)
    sumR_add           Σ_{k < a + n} H(k) = Σ_{k < a} H(k) + Σ_{k < n} H(a + k)
    sumR_triple        H(k + N) = H(k) ⇒ Σ_{k < 3N} H(k) = 3 Σ_{k < N} H(k)
    cube_sum           Σ_x G(x³) = G(0) + 3 Σ_{k < N} G(g^(3k))
  For G(y) = χ(y + 1): Σ_x χ(x³ + 1) = 1 + 3T, so a_p(y² = x³ + 1) ≡ 2 (mod 3) — the 3-torsion point (0, 1) — which fixes
  the sign of a_p = ±2u (CubicTrace): a_p = 2a with a ≡ 1 (mod 3), p = a² + 3b² (next).

  Lean 4 core only, proofs by hand (lib_nat, lib_int, lib_ring), no axioms.
-/
import lang.CubicClass
import lang.CubicTrace
import lang.GaussInt

namespace CubicSign

open FTAChain ZMod YMChain FiniteRecChain DLog OrdMul

def sumR (H : Nat → Int) : Nat → Int
  | 0 => 0
  | n + 1 => H n + sumR H n

theorem sumR_add (H : Nat → Int) (a : Nat) : ∀ n, sumR H (a + n) = sumR H a + sumR (fun k => H (a + k)) n
  | 0 => by show sumR H a = sumR H a + 0; lib_int
  | n + 1 => by
    show H (a + n) + sumR H (a + n) = sumR H a + (H (a + n) + sumR (fun k => H (a + k)) n)
    rw [sumR_add H a n]; lib_int

theorem sumR_ext (H K : Nat → Int) (h : ∀ k, H k = K k) : ∀ n, sumR H n = sumR K n
  | 0 => rfl
  | n + 1 => by show H n + sumR H n = K n + sumR K n; rw [h n, sumR_ext H K h n]

theorem sumR_triple (H : Nat → Int) (N : Nat) (hp : ∀ k, H (k + N) = H k) : sumR H (3 * N) = 3 * sumR H N := by
  have h1 : ∀ k, H (N + k) = H k := fun k => by rw [Nat.add_comm]; exact hp k
  have h2 : ∀ k, H (N + N + k) = H k := fun k => by rw [show N + N + k = (k + N) + N by lib_nat, hp, hp]
  rw [show 3 * N = (N + N) + N by lib_nat, sumR_add, show N + N = N + N from rfl, sumR_add,
    sumR_ext _ _ h1, sumR_ext _ _ h2]
  lib_int

variable (J : Nat)

theorem sumR_pows (g : Zp (J + 1)) (F : Zp (J + 1) → Int) : ∀ n,
    sumK LibInt.intR F (pows J g n) = sumR (fun k => F (Frobenius.pw (ring (J + 1)) g k)) n
  | 0 => rfl
  | n + 1 => by
    show F (Frobenius.pw (ring (J + 1)) g n) + sumK LibInt.intR F (pows J g n) = _
    rw [sumR_pows g F n]; rfl

theorem cube_sum (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (g : Zp (J + 1)) (hg : Ord (ring (J + 1)) g (J + 1))
    (N : Nat) (hN : J + 1 = 3 * N) (G : Zp (J + 1) → Int) :
    sumK LibInt.intR (fun x => G (CubicClass.cb J x)) (CMInert.allZ (J + 1) (J + 1 + 1))
      = G (ring (J + 1)).zero + 3 * sumR (fun k => G (CubicClass.cb J (Frobenius.pw (ring (J + 1)) g k))) N := by
  rw [sum_split J LibInt.intR hpr g hg, sumR_pows, congrArg (sumR _) hN, sumR_triple]
  · have : CubicClass.cb J (ring (J + 1)).zero = (ring (J + 1)).zero := by unfold CubicClass.cb; rw [DetChain.zero_mul']
    rw [this]; rfl
  · intro k
    show G (CubicClass.cb J (Frobenius.pw (ring (J + 1)) g (k + N))) = G (CubicClass.cb J (Frobenius.pw (ring (J + 1)) g k))
    have : CubicClass.cb J (Frobenius.pw (ring (J + 1)) g (k + N)) = CubicClass.cb J (Frobenius.pw (ring (J + 1)) g k) := by
      unfold CubicClass.cb
      rw [HasseCM.pw_add']
      have h3 : (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) g N) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) g N) (Frobenius.pw (ring (J + 1)) g N)) = (ring (J + 1)).one := by
        have this : Frobenius.pw (ring (J + 1)) g (3 * N) = (ring (J + 1)).one := (congrArg (Frobenius.pw (ring (J + 1)) g) hN).symm.trans hg.1
        rw [show 3 * N = N * 3 by lib_nat, ← TwoPower.pwpow] at this
        rw [← this]
        show _ = (ring (J + 1)).mul ((ring (J + 1)).mul ((ring (J + 1)).mul (ring (J + 1)).one (Frobenius.pw (ring (J + 1)) g N)) (Frobenius.pw (ring (J + 1)) g N)) (Frobenius.pw (ring (J + 1)) g N)
        lib_ring
      have e : (ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) g k) (Frobenius.pw (ring (J + 1)) g N))
          ((ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) g k) (Frobenius.pw (ring (J + 1)) g N)) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) g k) (Frobenius.pw (ring (J + 1)) g N)))
          = (ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) g k) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) g k) (Frobenius.pw (ring (J + 1)) g k)))
            ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) g N) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) g N) (Frobenius.pw (ring (J + 1)) g N))) := by lib_ring
      rw [e, h3, (ring (J + 1)).mul_comm, (ring (J + 1)).one_mul]
    rw [this]

section Exact
open QChar ConicSum JacobMoment CubicJacob

variable (m : Nat)

theorem psi_one_mod3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (N : Nat)
    (hN : 2 * m + 1 + 1 = 3 * N) : ∃ T : Int, psi m (ring (2 * m + 1 + 1)).one = 1 + 3 * T := by
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    refine ⟨sumR (fun k => chi m ((ring (2 * m + 1 + 1)).add (CubicClass.cb (2 * m + 1) (Frobenius.pw (ring (2 * m + 1 + 1)) g k)) (ring (2 * m + 1 + 1)).one)) N, ?_⟩
    have h := cube_sum (2 * m + 1) hpr g hg N hN (fun y => chi m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one))
    show sumK Z (fun x => chi m ((ring (2 * m + 1 + 1)).add (cube m x) (ring (2 * m + 1 + 1)).one)) (L m) = _
    rw [show (fun x => chi m ((ring (2 * m + 1 + 1)).add (cube m x) (ring (2 * m + 1 + 1)).one)) = (fun x => chi m ((ring (2 * m + 1 + 1)).add (CubicClass.cb (2 * m + 1) x) (ring (2 * m + 1 + 1)).one)) from rfl]
    rw [show L m = CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1) from rfl, h, (ring (2 * m + 1 + 1)).zero_add, chi_one]

/-- **a_p(y² = x³ + 1) = 2a with p = a² + 3b², a ≡ 1 (mod 3)** (exact sign). -/
theorem cubic_exact (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (N : Nat)
    (hN : 2 * m + 1 + 1 = 3 * N) :
    ∃ a b : Int, a * a + 3 * (b * b) = Int.ofNat (2 * m + 1 + 1 + 1) ∧ LibGcd.IDv 3 (a - 1) ∧ -psi m (ring (2 * m + 1 + 1)).one = 2 * a := by
  match CubicTrace.cubic_trace m hpr N hN, psi_one_mod3 m hpr N hN with
  | ⟨u, w, huw, hs⟩, ⟨T, hT⟩ =>
    have hs' : psi m (ring (2 * m + 1 + 1)).one = 2 * Int.ofNat u ∨ psi m (ring (2 * m + 1 + 1)).one = 2 * -Int.ofNat u := by
      cases hs with
      | inl e => left; rw [e]; rfl
      | inr e => right; rw [e]; show -(2 * Int.ofNat u) = 2 * -Int.ofNat u; lib_int
    -- 1 + T is even
    have hev : ∃ s, 1 + T = 2 * s := by
      match GaussInt.int_even_odd (1 + T) with
      | Or.inl h => exact h
      | Or.inr ⟨s0, h0⟩ =>
        exfalso
        have hodd : psi m (ring (2 * m + 1 + 1)).one = 2 * (s0 + T) + 1 := by
          rw [hT]
          calc 1 + 3 * T = (1 + T) + 2 * T := by lib_int
            _ = 2 * (s0 + T) + 1 := by rw [h0]; lib_int
        cases hs' with
        | inl e => exact GaussInt.int_odd_ne_even (s0 + T) (Int.ofNat u) (hodd.symm.trans e)
        | inr e => exact GaussInt.int_odd_ne_even (s0 + T) (-Int.ofNat u) (hodd.symm.trans e)
    match hev with
    | ⟨s0, hs2⟩ =>
      have hTs : T = 2 * s0 + -1 := by
        calc T = (1 + T) + -1 := by lib_int
          _ = 2 * s0 + -1 := by rw [hs2]
      have hpsi : psi m (ring (2 * m + 1 + 1)).one = 2 * (3 * s0 + -1) := by rw [hT, hTs]; lib_int
      have hu : 3 * s0 + -1 = Int.ofNat u ∨ 3 * s0 + -1 = -Int.ofNat u := by
        cases hs' with
        | inl e => left; exact CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) _ _ (hpsi.symm.trans e)
        | inr e => right; exact CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) _ _ (hpsi.symm.trans e)
      refine ⟨1 + -3 * s0, Int.ofNat w, ?_, ⟨-s0, by lib_int⟩, ?_⟩
      · have hsq : (1 + -3 * s0) * (1 + -3 * s0) = Int.ofNat u * Int.ofNat u := by
          cases hu with
          | inl e => rw [← e]; lib_int
          | inr e =>
            have : Int.ofNat u = -(3 * s0 + -1) := by rw [e]; lib_int
            rw [this]; lib_int
        rw [hsq]
        show Int.ofNat (u * u + 3 * (w * w)) = _
        rw [huw]
      · rw [hpsi]; lib_int

end Exact

end CubicSign

#print axioms CubicSign.sumR_add
#print axioms CubicSign.sumR_ext
#print axioms CubicSign.sumR_triple
#print axioms CubicSign.sumR_pows
#print axioms CubicSign.cube_sum
#print axioms CubicSign.psi_one_mod3
#print axioms CubicSign.cubic_exact

Listing 54: lang/HasseEisen.lean

lang/HasseEisen.lean

/-
  lang/HasseEisen — T43, cycle 40: the Hasse invariant of y² = x³ + 1 at split primes p = 6k + 7.

  p = J + 2, h = (p − 1)/2 = 3k + 3. (x³ + 1)^h = Σ_i C(h, i) x^(3i) (Frobenius.ev_row with a = x³, b = 1); summing over
  x with the power sums (PowerSum, HasseCM.ps): the exponent 0 gives p · 1 = 0, the exponents 3i ∈ [3, p − 2] and
  [p, 2p − 3] give 0, and 3i = p − 1 (i = 2k + 2) gives −1:
    ps_zero          Σ_x 1 = 0
    T3, T3_cons, T3_zero, T3_skip, skip3_many    one coefficient at a time
    hasse_eisen      Σ_x (x³ + 1)^((p−1)/2) = −C(3k + 3, 2k + 2)
  So a_p(y² = x³ + 1) ≡ C((p−1)/2, (p−1)/3) (mod p) at every split prime of ℤ[ω] — the companion of CMEisen
  (inert primes) and of HasseCM for ℤ[i].

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.HasseCM
import lib.Semi

namespace HasseEisen

open FTAChain ZMod YMChain Frobenius HasseCM

variable (J : Nat)

theorem sum_one_partial : ∀ n, n ≤ J + 1 →
    sumK (ring (J + 1)) (fun x => pw (ring (J + 1)) x 0) (CMInert.allZ (J + 1) (n + 1)) = mk (J + 1) (n + 1)
  | 0, _ => by
    show (ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).zero = mk (J + 1) 1
    rw [DetChain.add_zero']; rfl
  | n + 1, hn => by
    show sumK (ring (J + 1)) _ (CMInert.allZ (J + 1) (n + 1) ++ [mk (J + 1) (n + 1)]) = _
    rw [CMInert.sumK_app', sum_one_partial n (Nat.le_of_succ_le hn)]
    show (ring (J + 1)).add (mk (J + 1) (n + 1)) ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).zero) = _
    rw [DetChain.add_zero', TwoPower.mk_add_one]

theorem ps_zero : ps J 0 = (ring (J + 1)).zero := by
  show sumK (ring (J + 1)) (fun x => pw (ring (J + 1)) x 0) (CMInert.allZ (J + 1) (J + 1 + 1)) = _
  rw [sum_one_partial J (J + 1) (Nat.le_refl _)]
  exact Subtype.ext (md_self (J + 1))

def ev3 (x : Zp (J + 1)) (cs : List Nat) : Zp (J + 1) := ev (ring (J + 1)) (pw (ring (J + 1)) x 3) (ring (J + 1)).one cs

def T3 (e : Nat) (cs : List Nat) : Zp (J + 1) :=
  sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x e) (ev3 J x cs)) (CMInert.allZ (J + 1) (J + 1 + 1))

theorem T3_cons (e c : Nat) (cs : List Nat) :
    T3 J e (c :: cs) = (ring (J + 1)).add ((ring (J + 1)).mul (nsm (ring (J + 1)) c (ring (J + 1)).one) (ps J e)) (T3 J (e + 3) cs) := by
  have pt : ∀ x : Zp (J + 1), (ring (J + 1)).mul (pw (ring (J + 1)) x e) (ev3 J x (c :: cs))
      = (ring (J + 1)).add ((ring (J + 1)).mul (nsm (ring (J + 1)) c (ring (J + 1)).one) (pw (ring (J + 1)) x e)) ((ring (J + 1)).mul (pw (ring (J + 1)) x (e + 3)) (ev3 J x cs)) := fun x => by
    show (ring (J + 1)).mul (pw (ring (J + 1)) x e) ((ring (J + 1)).add (nsm (ring (J + 1)) c (pw (ring (J + 1)) (ring (J + 1)).one cs.length)) ((ring (J + 1)).mul (pw (ring (J + 1)) x 3) (ev3 J x cs))) = _
    rw [TwoPower.pwone, (ring (J + 1)).distrib, (ring (J + 1)).mul_comm (pw (ring (J + 1)) x e) (nsm (ring (J + 1)) c _), ← (ring (J + 1)).mul_assoc, ← pw_add' (ring (J + 1)) x e 3]
  show sumK (ring (J + 1)) _ _ = _
  rw [sumK_ext (ring (J + 1)) _ _ pt, sumK_add, ← mul_sumK]
  rfl

theorem T3_nil (e : Nat) : T3 J e [] = (ring (J + 1)).zero := by
  show sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x e) (ring (J + 1)).zero) _ = _
  rw [sumK_ext (ring (J + 1)) _ (fun _ => (ring (J + 1)).zero) (fun x => DetChain.mul_zero (ring (J + 1)) _), sumK_zero']

theorem idx3 (e i : Nat) : e + 3 + 3 * i = e + 3 * (i + 1) := by lib_nat

theorem T3_zero : ∀ (cs : List Nat) (e : Nat), (∀ i, i < cs.length → ps J (e + 3 * i) = (ring (J + 1)).zero) → T3 J e cs = (ring (J + 1)).zero
  | [], e, _ => T3_nil J e
  | c :: cs, e, h => by
    have h0 : ps J e = (ring (J + 1)).zero := h 0 (Nat.zero_lt_succ _)
    rw [T3_cons, h0, DetChain.mul_zero, (ring (J + 1)).zero_add]
    exact T3_zero cs (e + 3) (fun i hi => by rw [idx3]; exact h (i + 1) (Nat.succ_lt_succ hi))

theorem skip3_many (n m : Nat) : ∀ (i j e : Nat), (∀ i', i' < i → ps J (e + 3 * i') = (ring (J + 1)).zero) →
    T3 J e (Frobenius.listC n j (i + m)) = T3 J (e + 3 * i) (Frobenius.listC n (j + i) m)
  | 0, j, e, _ => by rw [Nat.zero_add]; rfl
  | i + 1, j, e, h => by
    rw [Nat.succ_add]
    show T3 J e (Frobenius.C n j :: Frobenius.listC n (j + 1) (i + m)) = _
    have h0 : ps J e = (ring (J + 1)).zero := h 0 (Nat.zero_lt_succ _)
    rw [T3_cons, h0, DetChain.mul_zero, (ring (J + 1)).zero_add,
      skip3_many n m i (j + 1) (e + 3) (fun i' hi' => by rw [idx3]; exact h (i' + 1) (Nat.succ_lt_succ hi')),
      idx3, Nat.add_assoc j 1 i, Nat.add_comm 1 i]

/-- **The Hasse invariant of y² = x³ + 1** at p = 6k + 7:
    `Σ_x (x³ + 1)^((p−1)/2) = −C(3k + 3, 2k + 2)`. -/
theorem hasse_eisen (k : Nat) (hpr : ∀ d, Dv d ((6 * k + 5) + 1 + 1) → d = 1 ∨ d = (6 * k + 5) + 1 + 1) :
    sumK (ring ((6 * k + 5) + 1)) (fun x => pw (ring ((6 * k + 5) + 1)) ((ring ((6 * k + 5) + 1)).add (pw (ring ((6 * k + 5) + 1)) x 3) (ring ((6 * k + 5) + 1)).one) (3 * k + 3)) (CMInert.allZ ((6 * k + 5) + 1) ((6 * k + 5) + 1 + 1))
      = (ring ((6 * k + 5) + 1)).neg (nsm (ring ((6 * k + 5) + 1)) (Frobenius.C (3 * k + 3) (2 * k + 2)) (ring ((6 * k + 5) + 1)).one) := by
  have eA : sumK (ring ((6 * k + 5) + 1)) (fun x => pw (ring ((6 * k + 5) + 1)) ((ring ((6 * k + 5) + 1)).add (pw (ring ((6 * k + 5) + 1)) x 3) (ring ((6 * k + 5) + 1)).one) (3 * k + 3)) (CMInert.allZ ((6 * k + 5) + 1) ((6 * k + 5) + 1 + 1))
      = T3 (6 * k + 5) 0 (Frobenius.row (3 * k + 3)) :=
    sumK_ext (ring ((6 * k + 5) + 1)) _ _ (fun x => by
      show _ = (ring ((6 * k + 5) + 1)).mul (ring ((6 * k + 5) + 1)).one (ev3 (6 * k + 5) x (Frobenius.row (3 * k + 3)))
      rw [(ring ((6 * k + 5) + 1)).one_mul, ev3, ev_row]) _
  rw [eA, Frobenius.row_eq, show 3 * k + 3 + 1 = (2 * k + 2) + (k + 2) by lib_nat,
    skip3_many (6 * k + 5) (3 * k + 3) (k + 2) (2 * k + 2) 0 0 (fun i' hi' => by
      cases i' with
      | zero => exact ps_zero (6 * k + 5)
      | succ i'' =>
        apply ps_low (6 * k + 5) hpr
        · rw [show 0 + 3 * (i'' + 1) = 1 + (3 * i'' + 2) by lib_nat]; exact Nat.le_add_right 1 _
        · have hi : i'' ≤ 2 * k := Nat.le_of_succ_le_succ (Nat.le_of_lt_succ hi')
          have a : 0 + 3 * (i'' + 1) ≤ 0 + 3 * (2 * k + 1) := Nat.add_le_add_left (Nat.mul_le_mul_left 3 (Nat.succ_le_succ hi)) 0
          exact Nat.le_trans a (by rw [show 0 + 3 * (2 * k + 1) = 6 * k + 3 by lib_nat]; exact Nat.le_succ_of_le (Nat.le_succ _)))]
  rw [show 0 + (2 * k + 2) = 2 * k + 2 by lib_nat]
  show T3 (6 * k + 5) (0 + 3 * (2 * k + 2)) (Frobenius.C (3 * k + 3) (2 * k + 2) :: Frobenius.listC (3 * k + 3) (2 * k + 2 + 1) (k + 1)) = _
  rw [T3_cons, show 0 + 3 * (2 * k + 2) = (6 * k + 5) + 1 by lib_nat, ps_top (6 * k + 5) hpr,
    T3_zero (6 * k + 5) _ _ (fun i hi => by
      rw [Frobenius.len_listC] at hi
      rw [show (6 * k + 5) + 1 + 3 + 3 * i = (6 * k + 5) + 1 + (3 * i + 3) by lib_nat]
      apply ps_high (6 * k + 5) hpr
      · exact Nat.le_add_left 1 _
      · have a : 3 * i + 3 ≤ 3 * k + 3 := Nat.add_le_add_right (Nat.mul_le_mul_left 3 (Nat.le_of_lt_succ hi)) 3
        exact Nat.le_trans a (by rw [show 6 * k + 5 = (3 * k + 3) + (3 * k + 2) by lib_nat]; exact Nat.le_add_right _ _)),
    DetChain.add_zero', mul_neg_r (ring ((6 * k + 5) + 1)), (ring ((6 * k + 5) + 1)).mul_comm _ (ring ((6 * k + 5) + 1)).one, (ring ((6 * k + 5) + 1)).one_mul]

end HasseEisen

#print axioms HasseEisen.sum_one_partial
#print axioms HasseEisen.ps_zero
#print axioms HasseEisen.T3_cons
#print axioms HasseEisen.T3_nil
#print axioms HasseEisen.idx3
#print axioms HasseEisen.T3_zero
#print axioms HasseEisen.skip3_many
#print axioms HasseEisen.hasse_eisen

78.23 GL\(_2/\mathbb Q\) CM families \(y^2=x^3+Dx\) and \(y^2=x^3+D\)

For every \(D\ne 0\),

\[ \begin {aligned} y^2=x^3+Dx&:\ a_p=0\ (p\equiv 3\ (4)),\qquad a_p\in \{\pm 2u,\pm 2v\},\ p=u^2+v^2\ (p\equiv 1\ (4)),\\ y^2=x^3+D&:\ a_p=0\ (p\equiv 2\ (3)),\qquad a_p^2\in \{A_0^2,A_1^2,A_2^2\}\ (p\equiv 1\ (3)). \end {aligned} \]
The steps are
\[ \begin {aligned} \varphi (Dt^2)&=\chi (t)\,\varphi (D),\quad \varphi (D)^2\in \{\varphi (1)^2,\varphi (n)^2\},\\ \psi (Dt^3)&=\chi (t)\,\psi (D),\quad D=g^r(g^q)^3\Rightarrow \psi (D)^2=\psi (g^r)^2 . \end {aligned} \]

Listing 55: lang/QuarticFamily.lean

lang/QuarticFamily.lean

/-
  lang/QuarticFamily — T43, cycle 60: the CM family y² = x³ + D x (all quartic twists of y² = x³ − x).

  a_p(y² = x³ + Dx) = −φ(D), φ(D) = Σ_x χ(x(x² + D)) (Jacobsthal).
    phi_inert       p ≡ 3 (mod 4) (χ(−1) = −1): φ(D) = 0 for every D        (x ↦ −x)
    phi_values      p ≡ 1 (mod 4), D ≠ 0: φ(D)² = φ(1)² or φ(D)² = φ(n)²    (JacobClass.phi_class)
    family_trace    p = 4k + 5, D ≠ 0: ∃ u v, u² + v² = p ∧ φ(D) ∈ {±2u, ±2v}
  So every curve y² = x³ + Dx has a_p = 0 at the inert primes of ℤ[i] and a_p = ±2u or ±2v at the split primes, p = u² + v²
  — the Hecke characters ψ·χ_D (quartic twists) of ℚ(i) — uniformly in D.

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.CMModular
import lang.CubicTrace

namespace QuarticFamily

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal JacobMoment JacobClass

variable (m : Nat)

/-- **a_p = 0 at inert primes, for every D.** -/
theorem phi_inert (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (hm1 : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = -1) (D : Zp (2 * m + 1 + 1)) : phi m D = 0 := by
  have hr := reindex Z (L m) (compL m) (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).neg (fun x => DetChain.neg_neg' (ring (2 * m + 1 + 1)) x) (fun x => DetChain.neg_neg' (ring (2 * m + 1 + 1)) x)
    (fun x => chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) D)))
  have pt : ∀ x, chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg x) ((ring (2 * m + 1 + 1)).neg x)) D))
      = Z.mul (-1) (chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) D))) := fun x => by
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg x) ((ring (2 * m + 1 + 1)).neg x)) D)
        = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) D)) by lib_ring, chi_mul m hpr, hm1]
    rfl
  rw [sumK_ext Z _ _ pt, ← mul_sumK] at hr
  have h2 : phi m D + phi m D = 0 := by
    show sumK Z _ (L m) + sumK Z _ (L m) = 0
    calc sumK Z (fun x => chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) D))) (L m) + sumK Z (fun x => chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) D))) (L m)
        = Z.mul (-1) (sumK Z (fun x => chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) D))) (L m)) + sumK Z (fun x => chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) D))) (L m) := by rw [hr]
      _ = 0 := by show -1 * _ + _ = (0 : Int); lib_int
  exact int_two_zero _ h2

/-- **φ(D)² ∈ {φ(1)², φ(n)²}** for D ≠ 0. -/
theorem phi_values (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (n : Zp (2 * m + 1 + 1))
    (hn0 : n ≠ (ring (2 * m + 1 + 1)).zero) (hn : chi m n = -1) (D : Zp (2 * m + 1 + 1)) (hD : D ≠ (ring (2 * m + 1 + 1)).zero) :
    phi m D * phi m D = phi m (ring (2 * m + 1 + 1)).one * phi m (ring (2 * m + 1 + 1)).one ∨ phi m D * phi m D = phi m n * phi m n := by
  have hc := phi_class m hpr n hn0 hn D
  cases chi_cases m D with
  | inl h0 => exact absurd h0 (chi_ne_zero m D hD)
  | inr h =>
    cases h with
    | inl h1 =>
      left
      rw [h1] at hc
      apply CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e)
      show (2 : Int) * (phi m D * phi m D) = 2 * (phi m (ring (2 * m + 1 + 1)).one * phi m (ring (2 * m + 1 + 1)).one)
      rw [hc]; lib_int
    | inr h1 =>
      right
      rw [h1] at hc
      apply CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e)
      show (2 : Int) * (phi m D * phi m D) = 2 * (phi m n * phi m n)
      rw [hc]; lib_int

theorem sq_eq (X Y : Int) (h : X * X = Y * Y) : X = Y ∨ X = -Y := by
  have h0 : (X + -1 * Y) * (X + Y) = 0 := by
    rw [show (X + -1 * Y) * (X + Y) = X * X + -1 * (Y * Y) by lib_int, h]; lib_int
  cases LibInt.int_noZD _ _ h0 with
  | inl h1 =>
    left
    have h1' : X + -1 * Y = 0 := h1
    have : X = (X + -1 * Y) + Y := by lib_int
    rw [this, h1']; lib_int
  | inr h1 =>
    right
    have h1' : X + Y = 0 := h1
    have : X = (X + Y) + -1 * Y := by lib_int
    rw [this, h1']; lib_int

/-- **The family y² = x³ + Dx at split primes**: φ(D) ∈ {±2u, ±2v}, p = u² + v². -/
theorem family_trace (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1)
    (D : Zp (2 * (2 * k + 1) + 1 + 1)) (hD : D ≠ (ring (2 * (2 * k + 1) + 1 + 1)).zero) :
    ∃ u v : Nat, u * u + v * v = 2 * (2 * k + 1) + 1 + 1 + 1 ∧
      (phi (2 * k + 1) D = Int.ofNat (2 * u) ∨ phi (2 * k + 1) D = -Int.ofNat (2 * u) ∨
       phi (2 * k + 1) D = Int.ofNat (2 * v) ∨ phi (2 * k + 1) D = -Int.ofNat (2 * v)) := by
  match GaussSplit.split_one_mod_four k hpr, exists_nonres (2 * k + 1) hpr with
  | ⟨i, hi⟩, ⟨n, hn0, hn⟩ =>
    have hi0 : i ≠ (ring (2 * (2 * k + 1) + 1 + 1)).zero := fun e => by
      rw [e, DetChain.zero_mul'] at hi
      apply PowerSum.one_ne_zero' (2 * (2 * k + 1) + 1)
      have := congrArg (fun z => (ring (2 * (2 * k + 1) + 1 + 1)).add z (ring (2 * (2 * k + 1) + 1 + 1)).one) hi
      exact (((ring (2 * (2 * k + 1) + 1 + 1)).zero_add (ring (2 * (2 * k + 1) + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * (2 * k + 1) + 1 + 1)) (ring (2 * (2 * k + 1) + 1 + 1)).one)))
    have hchi : chi (2 * k + 1) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) = 1 := by rw [← hi]; exact chi_sq (2 * k + 1) hpr i hi0
    have hM := moment (2 * k + 1) hpr
    have hG := grouped (2 * k + 1) hpr n hn0 hn
    rw [hchi] at hM
    rw [hM] at hG
    -- (P − 1)(A + B − 4P) = 0
    have hz : (P (2 * k + 1) + -1) * (phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n + -1 * (4 * P (2 * k + 1))) = 0 := by
      have : (P (2 * k + 1) + -1) * (phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n + -1 * (4 * P (2 * k + 1)))
          = (P (2 * k + 1) + -1) * (phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n) + -1 * (2 * (P (2 * k + 1) * ((1 + 1) * (P (2 * k + 1) + -1)))) := by
        lib_int
      rw [this, ← hG]; lib_int
    have hP1 : P (2 * k + 1) + -1 ≠ 0 := fun e => by
      have e1 : P (2 * k + 1) = 1 := by
        have : P (2 * k + 1) = (P (2 * k + 1) + -1) + 1 := by lib_int
        rw [this, e]; rfl
      have e2 : Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) = Int.ofNat 1 := e1
      have e3 := Int.ofNat.inj e2
      exact Nat.noConfusion (Nat.succ.inj e3)
    have hAB : phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n = 4 * P (2 * k + 1) := by
      cases LibInt.int_noZD _ _ hz with
      | inl h => exact absurd h hP1
      | inr h =>
        have : phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n
            = (phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n + -1 * (4 * P (2 * k + 1))) + 4 * P (2 * k + 1) := by lib_int
        rw [this, show (phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one * phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one + phi (2 * k + 1) n * phi (2 * k + 1) n + -1 * (4 * P (2 * k + 1))) = 0 from h]
        lib_int
    match CMModular.nat_sq (phi (2 * k + 1) (ring (2 * (2 * k + 1) + 1 + 1)).one), CMModular.nat_sq (phi (2 * k + 1) n) with
    | ⟨a, ha, _⟩, ⟨b, hb, _⟩ =>
      rw [ha, hb] at hAB
      have hnat : a * a + b * b = 4 * (2 * (2 * k + 1) + 1 + 1 + 1) := Int.ofNat.inj hAB
      match CMModular.halves a b _ hnat with
      | ⟨u, v, hau, hbv, huv⟩ =>
        refine ⟨u, v, huv, ?_⟩
        cases phi_values (2 * k + 1) hpr n hn0 hn D hD with
        | inl h =>
          rw [ha, hau] at h
          cases sq_eq _ (Int.ofNat (2 * u)) h with
          | inl e => exact Or.inl e
          | inr e => exact Or.inr (Or.inl e)
        | inr h =>
          rw [hb, hbv] at h
          cases sq_eq _ (Int.ofNat (2 * v)) h with
          | inl e => exact Or.inr (Or.inr (Or.inl e))
          | inr e => exact Or.inr (Or.inr (Or.inr e))

end QuarticFamily

#print axioms QuarticFamily.phi_inert
#print axioms QuarticFamily.phi_values
#print axioms QuarticFamily.sq_eq
#print axioms QuarticFamily.family_trace

Listing 56: lang/SexticFamily.lean

lang/SexticFamily.lean

/-
  lang/SexticFamily — T43, cycle 61: the CM family y² = x³ + D (all sextic twists of y² = x³ + 1).

  a_p(y² = x³ + D) = −ψ(D), ψ(D) = Σ_x χ(x³ + D) (CubicJacob).
    psi_inert       p ≡ 2 (mod 3) (3e = 2(p − 1) + 1): ψ(D) = 0 for every D        (cubing is a bijection, then shift)
    psi_classes     p ≡ 1 (mod 3), D ≠ 0: ψ(D)² ∈ {ψ(1)², ψ(g)², ψ(g²)²}          (D = g^r (g^q)³, CubicJacob.psi_sq_const)
  With CubicTrace/CubicSign (ψ(1) = −2a, p = a² + 3b², and A₀ − A₁ + A₂ = 0): every curve y² = x³ + D has a_p = 0 at the
  inert primes of ℤ[ω] and a_p² ∈ {A₀², A₁², A₂²} at the split ones — the sextic twists of the Hecke character of ℚ(√−3).

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.CubicSign
import lang.CMEisen

namespace SexticFamily

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum JacobMoment CubicJacob DLog OrdMul

variable (m : Nat)

theorem cube_pw (x : Zp (2 * m + 1 + 1)) : cube m x = Frobenius.pw (ring (2 * m + 1 + 1)) x 3 := by
  unfold cube; show _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one x) x) x; lib_ring

/-- **a_p = 0 at the inert primes of ℤ[ω], for every D.** -/
theorem psi_inert (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (e : Nat)
    (he : 3 * e = 2 * (2 * m + 1 + 1) + 1) (D : Zp (2 * m + 1 + 1)) : psi m D = 0 := by
  have hr := reindex Z (L m) (compL m) (cube m) (fun x => Frobenius.pw (ring (2 * m + 1 + 1)) x e)
    (fun x => by rw [cube_pw]; exact CMEisen.cube_inv (2 * m + 1) hpr e he x)
    (fun y => by rw [cube_pw]; exact CMEisen.inv_cube (2 * m + 1) hpr e he y) (fun y => chi m ((ring (2 * m + 1 + 1)).add y D))
  show sumK Z (fun x => chi m ((ring (2 * m + 1 + 1)).add (cube m x) D)) (L m) = 0
  rw [hr, shift_sum m (chi m) D]
  exact chi_sum m hpr

/-- **ψ(D)² takes one of the three class values.** -/
theorem psi_classes (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (g : Zp (2 * m + 1 + 1))
    (hg : Ord (ring (2 * m + 1 + 1)) g (2 * m + 1 + 1)) (D : Zp (2 * m + 1 + 1)) (hD : D ≠ (ring (2 * m + 1 + 1)).zero) :
    psi m D * psi m D = psi m (ring (2 * m + 1 + 1)).one * psi m (ring (2 * m + 1 + 1)).one ∨ psi m D * psi m D = psi m g * psi m g ∨
      psi m D * psi m D = psi m ((ring (2 * m + 1 + 1)).mul g g) * psi m ((ring (2 * m + 1 + 1)).mul g g) := by
  have hg0 : g ≠ (ring (2 * m + 1 + 1)).zero := fun h => by
    have := hg.1; rw [h, HasseCM.pw_zero_succ] at this; exact PowerSum.one_ne_zero' (2 * m + 1) this.symm
  match mem_pows (2 * m + 1) g D _ (every_unit (2 * m + 1) hpr g hg D hD) with
  | ⟨k, _, hk⟩ =>
    match Order.divmod 2 k with
    | ⟨q, r, hqr, hr3⟩ =>
      have hD' : D = (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g r) (cube m (Frobenius.pw (ring (2 * m + 1 + 1)) g q)) := by
        rw [hk, hqr, show q * (2 + 1) + r = 3 * q + r by lib_nat, CubicClass.pw_split]; rfl
      have hgq := DLog.pw_ne_zero (2 * m + 1) hpr g hg0 q
      rw [hD', psi_sq_const m hpr _ _ hgq]
      cases r with
      | zero => left; rfl
      | succ r =>
        cases r with
        | zero => right; left; rw [show Frobenius.pw (ring (2 * m + 1 + 1)) g 1 = g from (ring (2 * m + 1 + 1)).one_mul g]
        | succ r =>
          cases r with
          | zero =>
            right; right
            rw [show Frobenius.pw (ring (2 * m + 1 + 1)) g 2 = (ring (2 * m + 1 + 1)).mul g g by show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one g) g = _; rw [(ring (2 * m + 1 + 1)).one_mul]]
          | succ r => exact absurd hr3 (Nat.not_lt_of_le (Nat.succ_le_succ (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le r)))))

end SexticFamily

#print axioms SexticFamily.cube_pw
#print axioms SexticFamily.psi_inert
#print axioms SexticFamily.psi_classes

78.24 GL\(_2\) over \(\mathbb Q(i)\): \(y^2=x^3-x\) over \(\mathbb F_{p^2}\) and over every \(\mathbb F_q\ni i\)

For \(p\equiv 3\ (4)\) and for every finite field \(\mathbb F_q\) of odd order with \(i^2=-1\),

\[ \begin {aligned} \textstyle \sum _{z\in \mathbb F_{p^2}}\chi (z^3-z)&=2p,\qquad \#E(\mathbb F_{p^2})=(p+1)^2,\qquad a_{(p)}=-2p,\\ -\textstyle \sum _{x\in \mathbb F_q}\chi (x^3-x)&=2a,\quad q=a^2+b^2,\quad a+bi\ \text {primary},\quad \textstyle \sum _{x}\chi (x^3-x)\equiv 7-q\ (8). \end {aligned} \]
The steps are
\[ \begin {aligned} &\mathbb F_{p^2}=\mathbb F_p[i]\ \text {satisfies the finite-field interface (Euler's criterion through the norm)},\\ &\mathbb F_{p^2}=\mathbb F_p[\sqrt n]\ \text {for every odd}\ p\ \text {and non-residue}\ n:\ x^{(p^2-1)/2}=N(x)^{(p-1)/2},\ \text {a non-residue from}\ \#\{x^h=1\}\le h<2h,\\ &\varphi (1)^2+\varphi (n)^2=4q\ \text {over every interface field},\\ &q=p^2=u^2+v^2,\ p\ \text {inert in}\ \mathbb Z[i]\Rightarrow \{u,v\}=\{p,0\},\\ &\textstyle \sum \equiv 7-p^2\equiv 6\ (8)\Rightarrow \textstyle \sum =2p,\\ &a^2+v^2=7+2a+8X\ \Rightarrow \ v\ \text {even},\ a+v\equiv 1\ (4)\quad (\text {parities of }a,v,a/2,v/2),\\ &i=(\sqrt n)^{(p-1)/2}\ \text {satisfies}\ i^2=n^{(p-1)/2}=-1\ \text {in every}\ \mathbb F_{p^2}. \end {aligned} \]

Listing 57: lang/FinField.lean

lang/FinField.lean

/-
  lang/FinField — T43, cycle 65: an interface for finite fields of odd order and their integer-valued quadratic character.

  FF R bundles a commutative ring K on R with a complete list of its q = 2h + 1 elements and the facts used by character
  sums: no zero divisors, inverses, Euler's criterion (x^h = ±1, squares give 1, x^h = 1 ⇒ square), a non-residue,
  −1 ≠ 1. 𝔽_p (lang/ZMod, Euler, EulerHard) and 𝔽_{p²} (lang/QuadFrob) are instances (next cycles).
    chi                 χ(x) ∈ {0, ±1}
    chi_mul, chi_sq     χ(xy) = χ(x)χ(y), χ(y²) = 1 (y ≠ 0)
    chi_sum             Σ_x χ(x) = 0
  The aim: the Jacobsthal chain over any finite field, then the base change of the CM form of y² = x³ − x to ℚ(i) at the
  inert primes (point counts over 𝔽_{p²}) — GL2 over the CM field ℚ(i).

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.QChar
import lang.JacobMoment
import lang.EulerHard
import lib.Semi

namespace FinField

open YMChain FiniteRecChain

structure FF (R : Type) [DecidableEq R] where
  K : DetChain.CRing R
  els : List R
  comp : Complete LibInt.intR els
  h : Nat
  card : sumK LibInt.intR (fun _ => (1 : Int)) els = Int.ofNat (2 * h + 1)
  nozd : DetChain.NoZD K
  inv : ∀ x, x ≠ K.zero → ∃ y, K.mul x y = K.one
  euler_pm : ∀ x, x ≠ K.zero → Frobenius.pw K x h = K.one ∨ Frobenius.pw K x h = K.neg K.one
  euler_sq : ∀ y, y ≠ K.zero → Frobenius.pw K (K.mul y y) h = K.one
  euler_hard : ∀ x, Frobenius.pw K x h = K.one → ∃ y, K.mul y y = x
  ne_pm : K.neg K.one ≠ K.one
  nonres : ∃ n, n ≠ K.zero ∧ Frobenius.pw K n h ≠ K.one
  iv : R → R
  iv_mul : ∀ x, x ≠ K.zero → K.mul (iv x) x = K.one
  iv_zero : iv K.zero = K.zero
  iv_hom : ∀ x y, iv (K.mul x y) = K.mul (iv x) (iv y)

variable {R : Type} [DecidableEq R] (F : FF R)

def chi (x : R) : Int := if x = F.K.zero then 0 else if Frobenius.pw F.K x F.h = F.K.one then 1 else -1

theorem chi_zero : chi F F.K.zero = 0 := ite_pos' _ _ rfl

theorem chi_one_of (x : R) (hx : x ≠ F.K.zero) (h : Frobenius.pw F.K x F.h = F.K.one) : chi F x = 1 := by
  unfold chi; rw [ite_neg' _ _ hx, ite_pos' _ _ h]

theorem chi_neg_of (x : R) (hx : x ≠ F.K.zero) (h : Frobenius.pw F.K x F.h ≠ F.K.one) : chi F x = -1 := by
  unfold chi; rw [ite_neg' _ _ hx, ite_neg' _ _ h]

theorem chi_mul (x y : R) : chi F (F.K.mul x y) = chi F x * chi F y := by
  by_cases hx : x = F.K.zero
  · rw [hx, DetChain.zero_mul', chi_zero]; lib_int
  by_cases hy : y = F.K.zero
  · rw [hy, DetChain.mul_zero, chi_zero]; lib_int
  have hxy : F.K.mul x y ≠ F.K.zero := fun e => by
    cases F.nozd _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have hp := Frobenius.pw_mul F.K x y F.h
  cases F.euler_pm x hx with
  | inl ex =>
    cases F.euler_pm y hy with
    | inl ey => rw [chi_one_of F x hx ex, chi_one_of F y hy ey, chi_one_of F _ hxy (by rw [hp, ex, ey, F.K.one_mul])]; rfl
    | inr ey =>
      have ey' : Frobenius.pw F.K y F.h ≠ F.K.one := fun e => F.ne_pm (ey.symm.trans e)
      rw [chi_one_of F x hx ex, chi_neg_of F y hy ey', chi_neg_of F _ hxy (by rw [hp, ex, ey, F.K.one_mul]; exact F.ne_pm)]; rfl
  | inr ex =>
    have ex' : Frobenius.pw F.K x F.h ≠ F.K.one := fun e => F.ne_pm (ex.symm.trans e)
    cases F.euler_pm y hy with
    | inl ey =>
      rw [chi_neg_of F x hx ex', chi_one_of F y hy ey,
        chi_neg_of F _ hxy (by rw [hp, ex, ey, F.K.mul_comm, F.K.one_mul]; exact F.ne_pm)]; rfl
    | inr ey =>
      have ey' : Frobenius.pw F.K y F.h ≠ F.K.one := fun e => F.ne_pm (ey.symm.trans e)
      have nn : F.K.mul (F.K.neg F.K.one) (F.K.neg F.K.one) = F.K.one := by
        rw [DetChain.neg_mul_left, F.K.one_mul, DetChain.neg_neg']
      rw [chi_neg_of F x hx ex', chi_neg_of F y hy ey', chi_one_of F _ hxy (by rw [hp, ex, ey]; exact nn)]; rfl

theorem chi_sq (y : R) (hy : y ≠ F.K.zero) : chi F (F.K.mul y y) = 1 := by
  have hyy : F.K.mul y y ≠ F.K.zero := fun e => by
    cases F.nozd _ _ e with
    | inl h => exact hy h
    | inr h => exact hy h
  exact chi_one_of F _ hyy (F.euler_sq y hy)

theorem chi_sum : sumK LibInt.intR (chi F) F.els = 0 := by
  match F.nonres with
  | ⟨n, hn0, hn⟩ =>
    match F.inv n hn0 with
    | ⟨ni, hni⟩ =>
      have hre := reindex LibInt.intR F.els F.comp (F.K.mul n) (F.K.mul ni)
        (fun x => by rw [← F.K.mul_assoc, F.K.mul_comm ni, hni, F.K.one_mul])
        (fun y => by rw [← F.K.mul_assoc, hni, F.K.one_mul]) (chi F)
      have hc : chi F n = -1 := chi_neg_of F n hn0 hn
      have e : sumK LibInt.intR (fun x => chi F (F.K.mul n x)) F.els
          = LibInt.intR.mul (-1) (sumK LibInt.intR (chi F) F.els) := by
        rw [mul_sumK]
        exact sumK_ext LibInt.intR _ _ (fun x => by rw [chi_mul F, hc]; rfl) _
      rw [e] at hre
      apply QChar.int_two_zero
      have : LibInt.intR.mul (-1) (sumK LibInt.intR (chi F) F.els) + sumK LibInt.intR (chi F) F.els = 0 := by
        show -1 * sumK LibInt.intR (chi F) F.els + sumK LibInt.intR (chi F) F.els = 0
        lib_int
      rw [hre] at this
      exact this

/-- **𝔽_p is an instance** (p = 2m + 3). -/
def ffp (m : Nat) (hpr : ∀ d, FTAChain.Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    FF (ZMod.Zp (2 * m + 1 + 1)) where
  K := (ZMod.ring (2 * m + 1 + 1))
  els := ConicSum.L m
  comp := JacobMoment.compL m
  h := m + 1
  card := by
    rw [show 2 * (m + 1) + 1 = 2 * m + 1 + 1 + 1 by lib_nat]
    exact JacobMoment.sum_one m
  nozd := ZMod.nozd (2 * m + 1) hpr
  inv := fun x hx => ⟨Frobenius.pw (ZMod.ring (2 * m + 1 + 1)) x (2 * m + 1), by rw [(ZMod.ring (2 * m + 1 + 1)).mul_comm]; exact ZMod.inv_exists (2 * m + 1) hpr x hx⟩
  euler_pm := fun x hx => Euler.euler_pm m hpr x hx
  euler_sq := fun y hy => Euler.euler_square m hpr y hy
  euler_hard := fun x hx => EulerHard.euler_hard m hpr x hx
  ne_pm := QChar.ne_pm m
  nonres := match PowerSum.exists_nonroot (2 * m + 1) hpr m (by rw [Nat.two_mul]; exact Nat.succ_le_succ (Nat.le_add_left m m)) with
    | ⟨n, hn0, hn⟩ => ⟨n, hn0, hn⟩
  iv := ConicSum.iv m
  iv_mul := fun x hx => ConicSum.iv_mul_self m hpr x hx
  iv_zero := ConicSum.iv_zero m
  iv_hom := fun x y => ConicSum.inv_mul m x y

end FinField

#print axioms FinField.ffp
#print axioms FinField.chi_zero
#print axioms FinField.chi_one_of
#print axioms FinField.chi_neg_of
#print axioms FinField.chi_mul
#print axioms FinField.chi_sq
#print axioms FinField.chi_sum

Listing 58: lang/FinSign.lean

lang/FinSign.lean

/-
  lang/FinSign — T43, cycle 73: the mod-8 count Σ_x χ(x³ − x) = 3 − q + 4 I(i) + 8 H₂ over any finite field containing
  i (i² = −1), ordered by an injective key — port of SignCM/SignCM2 to FinField.FF.

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FinPairing
import lang.Thue
import lang.CMInert

namespace FinSign

open YMChain FiniteRecChain FinField FinConic FinMoment

variable {R : Type} [DecidableEq R] (F : FF R) (key : R → Nat)

def f (x : R) : R := CMInert.cub F.K x

def I (x : R) : Int := if chi F (f F x) = 1 then 1 else 0

def H : Int := sumK Z (fun x => FinPairing.ltI key x (F.K.neg x) * I F x) F.els

theorem chi_f_split (x : R) :
    chi F (f F x) = 2 * I F x + -1 + delta Z (f F x) F.K.zero := by
  unfold I
  by_cases h0 : f F x = F.K.zero
  · rw [h0, chi_zero, FinMoment.delta_one _ _ rfl, ite_neg' _ _ (fun e => FinClass.z01 e)]; rfl
  · rw [FinMoment.delta_zero _ _ h0]
    cases FinClass.chi_cases F (f F x) with
    | inl h => exact absurd h (FinClass.chi_ne_zero F _ h0)
    | inr h =>
      cases h with
      | inl h1 => rw [h1, ite_pos' _ _ rfl]; rfl
      | inr h1 => rw [h1, ite_neg' _ _ (fun e => Int.noConfusion e)]; rfl

theorem f_neg (x : R) : f F (F.K.neg x) = F.K.neg (f F x) := by
  unfold f; show F.K.add (F.K.mul (F.K.mul (F.K.neg x) (F.K.neg x)) (F.K.neg x)) (F.K.neg (F.K.neg x))
    = F.K.neg (F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x)); lib_ring

theorem I_neg (hm1 : chi F (F.K.neg F.K.one) = 1) (x : R) : I F (F.K.neg x) = I F x := by
  unfold I
  have : chi F (f F (F.K.neg x)) = chi F (f F x) := by
    rw [f_neg, show F.K.neg (f F x) = F.K.mul (F.K.neg F.K.one) (f F x) by lib_ring, chi_mul F, hm1]; lib_int
  rw [this]

theorem f_zero_iff (x : R)
    (h : f F x = F.K.zero) : x = F.K.zero ∨ x = F.K.one ∨ x = F.K.neg F.K.one := by
  have e : f F x = F.K.mul x (F.K.mul (F.K.add x (F.K.neg F.K.one)) (F.K.add x F.K.one)) := by
    unfold f; show F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x) = _; lib_ring
  rw [e] at h
  cases F.nozd _ _ h with
  | inl h0 => exact Or.inl h0
  | inr h1 =>
    cases F.nozd _ _ h1 with
    | inl h2 => exact Or.inr (Or.inl (FinMoment.eq_of_sub_zero F _ _ h2))
    | inr h2 =>
      right; right
      exact DetChain.neg_unique F.K _ _ h2

theorem I_zero : I F F.K.zero = 0 := by
  unfold I
  have : f F F.K.zero = F.K.zero := by unfold f; show F.K.add (F.K.mul (F.K.mul F.K.zero F.K.zero) F.K.zero) (F.K.neg F.K.zero) = _; lib_ring
  rw [this, chi_zero, ite_neg' _ _ (fun e => FinClass.z01 e)]

theorem roots_f :
    sumK Z (fun x => delta Z (f F x) F.K.zero) F.els = 3 := by
  have n01 : F.K.zero ≠ F.K.one := fun e => FinConic.one_ne_zero F e.symm
  have n0m : F.K.zero ≠ F.K.neg F.K.one := fun e => n01 (by
    have := congrArg F.K.neg e; rw [DetChain.neg_neg'] at this
    rw [← this]; exact (DetChain.neg_unique F.K _ _ (F.K.zero_add F.K.zero)))
  have n1m : F.K.one ≠ F.K.neg F.K.one := fun e => F.ne_pm e.symm
  have pt : ∀ x, delta Z (f F x) F.K.zero = Z.add (delta Z x F.K.zero) (Z.add (delta Z x F.K.one) (delta Z x (F.K.neg F.K.one))) :=
    fun x => by
      by_cases h : f F x = F.K.zero
      · rw [FinMoment.delta_one _ _ h]
        cases f_zero_iff F x h with
        | inl h0 => rw [h0, FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ n01, FinMoment.delta_zero _ _ n0m]; rfl
        | inr h1 =>
          cases h1 with
          | inl h1 => rw [h1, FinMoment.delta_zero _ _ (fun e => n01 e.symm), FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ n1m]; rfl
          | inr h1 => rw [h1, FinMoment.delta_zero _ _ (fun e => n0m e.symm), FinMoment.delta_zero _ _ (fun e => n1m e.symm),
              FinMoment.delta_one _ _ rfl]; rfl
      · have hfx : ∀ c, x = c → f F c = F.K.zero → False := fun c e hc => h (e ▸ hc)
        have f0 : f F F.K.zero = F.K.zero := by unfold f; show F.K.add (F.K.mul (F.K.mul F.K.zero F.K.zero) F.K.zero) (F.K.neg F.K.zero) = _; lib_ring
        have f1 : f F F.K.one = F.K.zero := by unfold f; show F.K.add (F.K.mul (F.K.mul F.K.one F.K.one) F.K.one) (F.K.neg F.K.one) = _; lib_ring
        have fm : f F (F.K.neg F.K.one) = F.K.zero := by
          unfold f; show F.K.add (F.K.mul (F.K.mul (F.K.neg F.K.one) (F.K.neg F.K.one)) (F.K.neg F.K.one)) (F.K.neg (F.K.neg F.K.one)) = _; lib_ring
        rw [FinMoment.delta_zero _ _ h, FinMoment.delta_zero _ _ (fun e => hfx _ e f0), FinMoment.delta_zero _ _ (fun e => hfx _ e f1),
          FinMoment.delta_zero _ _ (fun e => hfx _ e fm)]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, sumK_add, show sumK Z (fun x => delta Z x F.K.zero) F.els = 1 from F.comp F.K.zero,
    show sumK Z (fun x => delta Z x F.K.one) F.els = 1 from F.comp F.K.one,
    show sumK Z (fun x => delta Z x (F.K.neg F.K.one)) F.els = 1 from F.comp _]
  rfl

theorem sum_I (hkey : ∀ x y, key x = key y → x = y) (hm1 : chi F (F.K.neg F.K.one) = 1) : sumK Z (I F) F.els = 2 * H F key := by
  rw [FinPairing.pairing key hkey F.K.neg (DetChain.neg_neg' F.K) (I F) (I_neg F hm1) F.els (F.comp)]
  have pt : ∀ x, FinPairing.eqI (F.K.neg x) x * I F x = 0 := fun x => by
    unfold FinPairing.eqI
    by_cases h : F.K.neg x = x
    · have hx : x = F.K.zero := by
        by_cases hx : x = F.K.zero
        · exact hx
        · exact absurd h.symm (FinMoment.two_ne F x hx)
      rw [hx, I_zero]; lib_int
    · rw [ite_neg' _ _ h]; lib_int
  rw [sumK_ext Z _ (fun _ => (0 : Int)) pt, zsum]
  show 0 + 2 * H F key = 2 * H F key
  lib_int

/-- **Σ_x χ(x³ − x) = 4H − p + 3.** -/
theorem S_eq (hkey : ∀ x y, key x = key y → x = y) (hm1 : chi F (F.K.neg F.K.one) = 1) : sumK Z (fun x => chi F (f F x)) F.els = 4 * H F key + -1 * PP F + 3 := by
  have pt : ∀ x, chi F (f F x) = Z.add (Z.add (Z.mul 2 (I F x)) (-1)) (delta Z (f F x) F.K.zero) := fun x => by
    rw [chi_f_split]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, sumK_add, ← mul_sumK, sum_I F key hkey hm1, roots_f F]
  have : sumK Z (fun _ => (-1 : Int)) F.els = -1 * PP F := by
    rw [← FinMoment.sum_one F]
    show _ = Z.mul (-1) (sumK Z (fun _ => (1 : Int)) F.els)
    rw [mul_sumK]; rfl
  rw [this]
  show 2 * (2 * H F key) + -1 * PP F + 3 = _
  lib_int



abbrev half (x : R) : Prop := key x < key (F.K.neg x)

theorem iv_neg_one : F.iv (F.K.neg F.K.one) = F.K.neg F.K.one := by
  have h0 : F.K.neg F.K.one ≠ F.K.zero := fun e => FinConic.one_ne_zero F (by
    rw [← DetChain.neg_neg' F.K F.K.one, e]; exact (DetChain.neg_unique F.K _ _ (F.K.zero_add F.K.zero)).symm)
  have h1 := F.iv_mul _ h0
  have h2 : F.K.mul (F.K.neg F.K.one) (F.K.neg F.K.one) = F.K.one := by lib_ring
  calc F.iv (F.K.neg F.K.one) = F.K.mul (F.iv (F.K.neg F.K.one)) (F.K.mul (F.K.neg F.K.one) (F.K.neg F.K.one)) := by rw [h2]; lib_ring
    _ = F.K.mul (F.K.mul (F.iv (F.K.neg F.K.one)) (F.K.neg F.K.one)) (F.K.neg F.K.one) := by lib_ring
    _ = F.K.neg F.K.one := by rw [h1]; lib_ring

theorem iv_neg (x : R) : F.iv (F.K.neg x) = F.K.neg (F.iv x) := by
  rw [show F.K.neg x = F.K.mul (F.K.neg F.K.one) x by lib_ring, F.iv_hom, iv_neg_one F]
  lib_ring

theorem I_inv (hm1 : chi F (F.K.neg F.K.one) = 1) (x : R) (hx : x ≠ F.K.zero) : I F (F.iv x) = I F x := by
  unfold I
  have hxy : F.K.mul x (F.iv x) = F.K.one := by rw [F.K.mul_comm]; exact F.iv_mul x hx
  have hy0 : F.iv x ≠ F.K.zero := fun e => by rw [e, DetChain.mul_zero] at hxy; exact FinConic.one_ne_zero F hxy.symm
  have e : f F (F.iv x) = F.K.mul (F.K.mul (F.K.mul (F.iv x) (F.iv x)) (F.K.mul (F.iv x) (F.iv x))) (F.K.mul (F.K.neg F.K.one) (f F x)) := by
    have id : f F (F.iv x) = F.K.add (F.K.mul (F.K.mul (F.K.mul (F.iv x) (F.iv x)) (F.K.mul (F.iv x) (F.iv x))) (F.K.mul (F.K.neg F.K.one) (f F x)))
        (F.K.add (F.K.mul (F.K.mul (F.K.mul (F.iv x) (F.iv x)) (F.iv x)) (F.K.add F.K.one (F.K.neg (F.K.mul x (F.iv x)))))
          (F.K.neg (F.K.mul (F.iv x) (F.K.add F.K.one (F.K.neg (F.K.mul (F.K.mul x (F.iv x)) (F.K.mul (F.K.mul x (F.iv x)) (F.K.mul x (F.iv x))))))))) := by
      unfold f; show F.K.add (F.K.mul (F.K.mul (F.iv x) (F.iv x)) (F.iv x)) (F.K.neg (F.iv x)) = _
      show _ = F.K.add _ (F.K.add _ (F.K.neg (F.K.mul (F.iv x) (F.K.add F.K.one (F.K.neg (F.K.mul (F.K.mul x (F.iv x))
        (F.K.mul (F.K.mul x (F.iv x)) (F.K.mul x (F.iv x))))))))) 
      unfold CMInert.cub
      lib_ring
    rw [id, hxy, F.K.add_neg, F.K.one_mul, F.K.one_mul, F.K.add_neg, DetChain.mul_zero, DetChain.mul_zero]
    have : F.K.neg F.K.zero = F.K.zero := (DetChain.neg_unique F.K _ _ (F.K.zero_add F.K.zero)).symm
    rw [this, DetChain.add_zero', DetChain.add_zero']
  have hc : chi F (f F (F.iv x)) = chi F (f F x) := by
    rw [e, chi_mul F, chi_mul F, chi_sq F _ hy0, chi_mul F, hm1]
    lib_int
  rw [hc]

theorem neg_neg (x : R) : F.K.neg (F.K.neg x) = x := DetChain.neg_neg' F.K x

theorem half_or (hkey : ∀ x y, key x = key y → x = y) (x : R)
    (hx : x ≠ F.K.zero) : half F key x ∨ half F key (F.K.neg x) := by
  unfold half
  rw [neg_neg]
  cases Nat.lt_or_ge (key x) (key (F.K.neg x)) with
  | inl h => exact Or.inl h
  | inr h => exact Or.inr (Nat.lt_of_le_of_ne h (fun e => FinMoment.two_ne F x hx (hkey _ _ e.symm)))

theorem half_not_both (x : R) (h1 : half F key x) (h2 : half F key (F.K.neg x)) : False := by
  unfold half at h1 h2
  rw [neg_neg] at h2
  exact Nat.lt_irrefl _ (Nat.lt_trans h1 h2)

def r (x : R) : R := if half F key (F.iv x) then F.iv x else F.K.neg (F.iv x)

theorem iv_ne_zero (x : R)
    (hx : x ≠ F.K.zero) : F.iv x ≠ F.K.zero := fun e => by
  have := F.iv_mul x hx
  rw [e, DetChain.zero_mul'] at this
  exact FinConic.one_ne_zero F this.symm

theorem r_half (hkey : ∀ x y, key x = key y → x = y) (x : R)
    (hx : x ≠ F.K.zero) : half F key (r F key x) := by
  unfold r
  by_cases h : half F key (F.iv x)
  · rw [ite_pos' _ _ h]; exact h
  · rw [ite_neg' _ _ h]
    cases half_or F key hkey (F.iv x) (FinConic.iv_ne_zero F x hx) with
    | inl h' => exact absurd h' h
    | inr h' => exact h'

theorem r_ne_zero (x : R)
    (hx : x ≠ F.K.zero) : r F key x ≠ F.K.zero := by
  unfold r
  by_cases h : half F key (F.iv x)
  · rw [ite_pos' _ _ h]; exact FinConic.iv_ne_zero F x hx
  · rw [ite_neg' _ _ h]
    intro e
    apply FinConic.iv_ne_zero F x hx
    rw [← neg_neg F (F.iv x), e]
    exact (DetChain.neg_unique F.K _ _ (F.K.zero_add F.K.zero)).symm

theorem iv_r (x : R) :
    F.iv (r F key x) = x ∨ F.iv (r F key x) = F.K.neg x := by
  unfold r
  by_cases h : half F key (F.iv x)
  · rw [ite_pos' _ _ h, FinConic.inv_inv F]; exact Or.inl rfl
  · rw [ite_neg' _ _ h, iv_neg, FinConic.inv_inv F]; exact Or.inr rfl

theorem r_r (x : R)
    (hhx : half F key x) : r F key (r F key x) = x := by
  show (if half F key (F.iv (r F key x)) then F.iv (r F key x) else F.K.neg (F.iv (r F key x))) = x
  cases iv_r F key x with
  | inl e => rw [e, ite_pos' _ _ hhx]
  | inr e =>
    rw [e, ite_neg' _ _ (fun h => half_not_both F key x hhx h), neg_neg]

def rho (x : R) : R := if (x ≠ F.K.zero ∧ half F key x) then r F key x else x

theorem rho_invol (hkey : ∀ x y, key x = key y → x = y) (x : R) :
    rho F key (rho F key x) = x := by
  by_cases h : x ≠ F.K.zero ∧ half F key x
  · have e1 : rho F key x = r F key x := ite_pos' _ _ h
    have h' : r F key x ≠ F.K.zero ∧ half F key (r F key x) := ⟨r_ne_zero F key x h.1, r_half F key hkey x h.1⟩
    rw [e1]
    show (if (r F key x ≠ F.K.zero ∧ half F key (r F key x)) then r F key (r F key x) else r F key x) = x
    rw [ite_pos' _ _ h', r_r F key x h.2]
  · have e1 : rho F key x = x := ite_neg' _ _ h
    rw [e1, e1]

def J (x : R) : Int := FinPairing.ltI key x (F.K.neg x) * I F x

theorem ltI_half (x : R) (h : half F key x) : FinPairing.ltI key x (F.K.neg x) = 1 := ite_pos' _ _ h
theorem ltI_nhalf (x : R) (h : ¬ half F key x) : FinPairing.ltI key x (F.K.neg x) = 0 := ite_neg' _ _ h

theorem I_r (hm1 : chi F (F.K.neg F.K.one) = 1)
    (x : R) (hx : x ≠ F.K.zero) : I F (r F key x) = I F x := by
  unfold r
  by_cases h : half F key (F.iv x)
  · rw [ite_pos' _ _ h]; exact I_inv F hm1 x hx
  · rw [ite_neg' _ _ h, I_neg F hm1]; exact I_inv F hm1 x hx

theorem J_rho (hkey : ∀ x y, key x = key y → x = y) (hm1 : chi F (F.K.neg F.K.one) = 1)
    (x : R) : J F key (rho F key x) = J F key x := by
  by_cases h : x ≠ F.K.zero ∧ half F key x
  · have e1 : rho F key x = r F key x := ite_pos' _ _ h
    rw [e1]
    unfold J
    rw [ltI_half F key _ (r_half F key hkey x h.1), ltI_half F key _ h.2, I_r F key hm1 x h.1]
  · have e1 : rho F key x = x := ite_neg' _ _ h
    rw [e1]

theorem iv_unique (y z : R)
    (hy : y ≠ F.K.zero) (h : F.K.mul y z = F.K.one) : F.iv y = z := by
  have := F.iv_mul y hy
  calc F.iv y = F.K.mul (F.iv y) (F.K.mul y z) := by rw [h, F.K.mul_comm, F.K.one_mul]
    _ = F.K.mul (F.K.mul (F.iv y) y) z := by lib_ring
    _ = z := by rw [this, F.K.one_mul]

theorem sq_roots (i x : R)
    (hi : F.K.mul i i = F.K.neg F.K.one) (hx : F.K.mul x x = F.K.neg F.K.one) : x = i ∨ x = F.K.neg i := by
  have e : F.K.mul (F.K.add x (F.K.neg i)) (F.K.add x i) = F.K.add (F.K.mul x x) (F.K.neg (F.K.mul i i)) := by lib_ring
  rw [hx, hi, F.K.add_neg] at e
  cases F.nozd _ _ e with
  | inl h => exact Or.inl (FinMoment.eq_of_sub_zero F _ _ h)
  | inr h => exact Or.inr (DetChain.neg_unique F.K _ _ h)

theorem I_pm_one (x : R)
    (hx : F.K.mul x x = F.K.one) : I F x = 0 := by
  have hf : f F x = F.K.zero := by
    have : f F x = F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)) := by
      unfold f; show F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x) = _; lib_ring
    rw [this, hx, F.K.add_neg, DetChain.mul_zero]
  unfold I
  rw [hf, chi_zero, ite_neg' _ _ (fun e => FinClass.z01 e)]

theorem i_ne_zero (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) : i ≠ F.K.zero := fun e => by
  rw [e, DetChain.zero_mul'] at hi
  apply FinConic.one_ne_zero F
  have := congrArg (fun z => F.K.add z F.K.one) hi
  exact (F.K.zero_add F.K.one).symm.trans (this.trans (DetChain.add_neg' F.K F.K.one))

theorem fixed_pt (i : R)
    (hi : F.K.mul i i = F.K.neg F.K.one) (x : R) :
    FinPairing.eqI (rho F key x) x * J F key x = delta Z x i * J F key x + delta Z x (F.K.neg i) * J F key x := by
  have hi0 := i_ne_zero F i hi
  by_cases h : x ≠ F.K.zero ∧ half F key x
  · have e1 : rho F key x = r F key x := ite_pos' _ _ h
    have hJ : J F key x = I F x := by unfold J; rw [ltI_half F key x h.2]; lib_int
    rw [e1, hJ]
    by_cases hx2 : F.K.mul x x = F.K.neg F.K.one
    · have hivx : F.iv x = F.K.neg x := iv_unique F x _ h.1 (by
        have : F.K.mul x (F.K.neg x) = F.K.neg (F.K.mul x x) := by lib_ring
        rw [this, hx2, neg_neg])
      have hrx : r F key x = x := by
        unfold r
        rw [hivx, ite_neg' _ _ (fun hh => half_not_both F key x h.2 hh), neg_neg]
      have hq : FinPairing.eqI (r F key x) x = 1 := by unfold FinPairing.eqI; rw [ite_pos' _ _ hrx]
      rw [hq]
      have hin : i ≠ F.K.neg i := FinMoment.two_ne F i hi0
      cases sq_roots F i x hi hx2 with
      | inl e => rw [e, FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ hin]; lib_int
      | inr e => rw [e, FinMoment.delta_zero _ _ (fun e' => hin e'.symm), FinMoment.delta_one _ _ rfl]; lib_int
    · have hxi : x ≠ i := fun e => hx2 (by rw [e]; exact hi)
      have hxi' : x ≠ F.K.neg i := fun e => hx2 (by rw [e, Thue.negsq F.K]; exact hi)
      rw [FinMoment.delta_zero _ _ hxi, FinMoment.delta_zero _ _ hxi']
      by_cases hr : r F key x = x
      · have hI : I F x = 0 := by
          unfold r at hr
          by_cases hh : half F key (F.iv x)
          · rw [ite_pos' _ _ hh] at hr
            apply I_pm_one F x
            have := F.iv_mul x h.1
            rw [hr] at this; exact this
          · rw [ite_neg' _ _ hh] at hr
            exfalso; apply hx2
            have h1 := F.iv_mul x h.1
            have h2 : F.iv x = F.K.neg x := by
              have := congrArg F.K.neg hr; rw [neg_neg] at this; exact this
            rw [h2] at h1
            have : F.K.mul x x = F.K.neg (F.K.mul (F.K.neg x) x) := by lib_ring
            rw [this, h1]
        rw [hI]; lib_int
      · have hq : FinPairing.eqI (r F key x) x = 0 := by unfold FinPairing.eqI; rw [ite_neg' _ _ hr]
        rw [hq]; lib_int
  · have hJ : J F key x = 0 := by
      unfold J
      by_cases hx0 : x = F.K.zero
      · rw [hx0, I_zero]; lib_int
      · have hnh : ¬ half F key x := fun hh => h ⟨hx0, hh⟩
        rw [ltI_nhalf F key x hnh]; lib_int
    rw [hJ]; lib_int

/-- **H ≡ I(i) (mod 2)**: H = I(i) + 2 H₂. -/
theorem H_mod (hkey : ∀ x y, key x = key y → x = y) (i : R)
    (hi : F.K.mul i i = F.K.neg F.K.one) :
    H F key = I F i + 2 * sumK Z (fun x => FinPairing.ltI key x (rho F key x) * J F key x) F.els := by
  have hm1 : chi F (F.K.neg F.K.one) = 1 := by
    have hi0 := i_ne_zero F i hi
    rw [← hi]; exact chi_sq F i hi0
  have hp := FinPairing.pairing key hkey (rho F key) (rho_invol F key hkey) (J F key) (J_rho F key hkey hm1) F.els (F.comp)
  show sumK Z (J F key) F.els = _
  rw [hp, sumK_ext Z _ (fun x => Z.add (Z.mul (delta Z x i) (J F key x)) (Z.mul (delta Z x (F.K.neg i)) (J F key x))) (fixed_pt F key i hi), sumK_add, pick Z F.els (F.comp) (J F key) i,
    pick Z F.els (F.comp) (J F key) (F.K.neg i)]
  have hJm : J F key (F.K.neg i) = FinPairing.ltI key (F.K.neg i) i * I F i := by
    unfold J; rw [neg_neg, I_neg F hm1]
  have hi0 := i_ne_zero F i hi
  have hs := FinPairing.lt_split key hkey i (F.K.neg i)
  have he : FinPairing.eqI (F.K.neg i) i = 0 := ite_neg' _ _ (fun e => FinMoment.two_ne F i hi0 e.symm)
  rw [he] at hs
  rw [hJm]
  show FinPairing.ltI key i (F.K.neg i) * I F i + FinPairing.ltI key (F.K.neg i) i * I F i + 2 * _ = _
  calc FinPairing.ltI key i (F.K.neg i) * I F i + FinPairing.ltI key (F.K.neg i) i * I F i + 2 * sumK Z (fun x => FinPairing.ltI key x (rho F key x) * J F key x) F.els
      = (0 + FinPairing.ltI key i (F.K.neg i) + FinPairing.ltI key (F.K.neg i) i) * I F i + 2 * sumK Z (fun x => FinPairing.ltI key x (rho F key x) * J F key x) F.els := by lib_int
    _ = _ := by rw [hs]; lib_int

/-- **The mod-8 congruence**: Σ_x χ(x³ − x) = 3 − p + 4 I(i) + 8 H₂. -/
theorem sign_mod8 (hkey : ∀ x y, key x = key y → x = y) (i : R)
    (hi : F.K.mul i i = F.K.neg F.K.one) :
    sumK Z (fun x => chi F (f F x)) F.els
      = 3 + -1 * PP F + 4 * I F i + 8 * sumK Z (fun x => FinPairing.ltI key x (rho F key x) * J F key x) F.els := by
  have hi0 := i_ne_zero F i hi
  have hm1 : chi F (F.K.neg F.K.one) = 1 := by rw [← hi]; exact chi_sq F i hi0
  rw [S_eq F key hkey hm1, H_mod F key hkey i hi]
  lib_int

end FinSign

#print axioms FinSign.chi_f_split
#print axioms FinSign.f_neg
#print axioms FinSign.I_neg
#print axioms FinSign.f_zero_iff
#print axioms FinSign.I_zero
#print axioms FinSign.roots_f
#print axioms FinSign.sum_I
#print axioms FinSign.S_eq
#print axioms FinSign.iv_neg_one
#print axioms FinSign.iv_neg
#print axioms FinSign.I_inv
#print axioms FinSign.neg_neg
#print axioms FinSign.half_or
#print axioms FinSign.half_not_both
#print axioms FinSign.iv_ne_zero
#print axioms FinSign.r_half
#print axioms FinSign.r_ne_zero
#print axioms FinSign.iv_r
#print axioms FinSign.r_r
#print axioms FinSign.rho_invol
#print axioms FinSign.ltI_half
#print axioms FinSign.ltI_nhalf
#print axioms FinSign.I_r
#print axioms FinSign.J_rho
#print axioms FinSign.iv_unique
#print axioms FinSign.sq_roots
#print axioms FinSign.I_pm_one
#print axioms FinSign.i_ne_zero
#print axioms FinSign.fixed_pt
#print axioms FinSign.H_mod
#print axioms FinSign.sign_mod8

Listing 59: lang/Fp2Inst.lean

lang/Fp2Inst.lean

/-
  lang/Fp2Inst — T43, cycle 72: 𝔽_{p²} = 𝔽_p[i] (p ≡ 3 mod 4) as an instance of the finite-field interface FinField.FF.

  p = 2k + 3 with (−1)^(k+1) = −1; q = p², h = (q − 1)/2 = (2k + 4)(k + 1).
    ff2     the FF structure: the p² pairs (Fp2), no zero divisors and inverses (Fp2), Euler's criterion through the
            norm (Fp2Euler), square roots (Fp2Sqrt), a non-residue 1 + b i (Fp2Roots)
  So every character-sum theorem proved for FF (FinConic, FinMoment, FinClass) holds over 𝔽_{p²}.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Fp2Sqrt
import lang.FinClass

namespace Fp2Inst

open FTAChain ZMod Quad Fp2 Fp2Euler

def ff2 (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    FinField.FF (Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1)) where
  K := F2 (2 * k + 1)
  els := pairs (2 * k + 1)
  comp := comp_pairs (2 * k + 1)
  h := (2 * k + 1 + 1 + 1 + 1) * (k + 1)
  card := by
    rw [card_pairs]
    show Int.ofNat ((2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1)) = Int.ofNat (2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1)
    rw [show (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) = 2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1 by lib_nat]
  nozd := nozd2 (2 * k + 1) (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn
  inv := fun x hx => ⟨iv2 (2 * k + 1) x, by
    rw [(F2 (2 * k + 1)).mul_comm]; exact iv2_mul (2 * k + 1) (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn x hx⟩
  euler_pm := fun x hx => euler_pm2 (2 * k + 1) (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn x hx
  euler_sq := fun y hy => euler_sq2 (2 * k + 1) (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn y hy
  euler_hard := fun x hx => Fp2Sqrt.euler_hard2 k hpr hn x hx
  ne_pm := fun e => QChar.ne_pm k (congrArg Prod.fst e)
  nonres := Fp2Roots.nonres2 k hpr hn
  iv := iv2 (2 * k + 1)
  iv_mul := fun x hx => iv2_mul (2 * k + 1) (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn x hx
  iv_zero := iv2_zero (2 * k + 1)
  iv_hom := iv2_hom (2 * k + 1)

end Fp2Inst

#print axioms Fp2Inst.ff2

Listing 60: lang/Fq2.lean

lang/Fq2.lean

/-
  lang/Fq2 — T43, cycle 88: 𝔽_{p²} = 𝔽_p[√n] for any non-residue n (generalising Fp2/Fp2Euler from n = −1).

  p = J + 2 = 2m + 1, n^m = −1, G2 = quad 𝔽_p n, N(a + b√n) = a² − n b², h₂ = (p + 1) m.
    iv, iv_zero, iv_mul, iv_hom, nozd     inverses x⁻¹ = x̄ · N(x)^(p−2), no zero divisors
    pw_p1, pw_h2                          x^(p+1) = N(x),  x^h₂ = N(x)^m
    euler_pm, euler_sq                    x^h₂ = ±1 (x ≠ 0),  (y²)^h₂ = 1
  The element list is Fp2.pairs (independent of n). This removes the restriction p ≡ 3 (mod 4): with n a non-residue
  (e.g. found by Euler's criterion) every 𝔽_{p²} is available, the input for point counts over 𝔽_{p²} at all p.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Fp2Euler

namespace Fq2

open FTAChain ZMod Quad QuadFrob Fp2

variable (J : Nat) (n : Zp (J + 1)) (m : Nat)

abbrev G2 : DetChain.CRing (Zp (J + 1) × Zp (J + 1)) := quad (ring (J + 1)) n

def iv (x : Zp (J + 1) × Zp (J + 1)) : Zp (J + 1) × Zp (J + 1) :=
  (G2 J n).mul (qconj (ring (J + 1)) x) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) J, (ring (J + 1)).zero)

theorem embed_mul (a b : Zp (J + 1)) : (G2 J n).mul (a, (ring (J + 1)).zero) (b, (ring (J + 1)).zero) = ((ring (J + 1)).mul a b, (ring (J + 1)).zero) := by
  apply Prod.ext
  · show (ring (J + 1)).add ((ring (J + 1)).mul a b) ((ring (J + 1)).mul n ((ring (J + 1)).mul (ring (J + 1)).zero (ring (J + 1)).zero)) = (ring (J + 1)).mul a b
    lib_ring
  · show (ring (J + 1)).add ((ring (J + 1)).mul a (ring (J + 1)).zero) ((ring (J + 1)).mul (ring (J + 1)).zero b) = (ring (J + 1)).zero
    lib_ring

theorem iv_zero : iv J n (G2 J n).zero = (G2 J n).zero := by
  apply Prod.ext
  · show (ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).zero (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n ((ring (J + 1)).zero, (ring (J + 1)).zero)) J))
      ((ring (J + 1)).mul n ((ring (J + 1)).mul ((ring (J + 1)).neg (ring (J + 1)).zero) (ring (J + 1)).zero)) = (ring (J + 1)).zero
    lib_ring
  · show (ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).zero (ring (J + 1)).zero) ((ring (J + 1)).mul ((ring (J + 1)).neg (ring (J + 1)).zero) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n ((ring (J + 1)).zero, (ring (J + 1)).zero)) J)) = (ring (J + 1)).zero
    lib_ring

theorem iv_mul (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 2 * m) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) (hx : x ≠ (G2 J n).zero) :
    (G2 J n).mul (iv J n x) x = (G2 J n).one := by
  rw [(G2 J n).mul_comm]
  exact quad_inv J n hpr m hJ h1 hn x hx

theorem iv_hom (x y : Zp (J + 1) × Zp (J + 1)) : iv J n ((G2 J n).mul x y) = (G2 J n).mul (iv J n x) (iv J n y) := by
  unfold iv
  rw [Quad.conj_mul, Quad.norm_mul, Frobenius.pw_mul, ← embed_mul]
  show qmul (ring (J + 1)) n (qmul (ring (J + 1)) n (qconj (ring (J + 1)) x) (qconj (ring (J + 1)) y))
      (qmul (ring (J + 1)) n (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) J, (ring (J + 1)).zero) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n y) J, (ring (J + 1)).zero))
    = qmul (ring (J + 1)) n (qmul (ring (J + 1)) n (qconj (ring (J + 1)) x) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) J, (ring (J + 1)).zero))
      (qmul (ring (J + 1)) n (qconj (ring (J + 1)) y) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n y) J, (ring (J + 1)).zero))
  rw [qmul_assoc, ← qmul_assoc (ring (J + 1)) n (qconj (ring (J + 1)) y), qmul_comm (ring (J + 1)) n (qconj (ring (J + 1)) y),
    qmul_assoc, ← qmul_assoc]

theorem nozd (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 2 * m) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) : DetChain.NoZD (G2 J n) := fun x y e => by
  by_cases hx : x = (G2 J n).zero
  · exact Or.inl hx
  · right
    have hi := iv_mul J n m hpr hJ h1 hn x hx
    calc y = (G2 J n).mul (G2 J n).one y := ((G2 J n).one_mul y).symm
      _ = (G2 J n).mul (G2 J n).one y := rfl
      _ = (G2 J n).mul ((G2 J n).mul (iv J n x) x) y := by rw [hi]
      _ = (G2 J n).mul (iv J n x) ((G2 J n).mul x y) := (G2 J n).mul_assoc _ _ _
      _ = (G2 J n).zero := by rw [e]; exact DetChain.mul_zero (G2 J n) _


theorem neg_embed (a : Zp (J + 1)) : (G2 J n).neg (a, (ring (J + 1)).zero) = ((ring (J + 1)).neg a, (ring (J + 1)).zero) := by
  apply Prod.ext
  · rfl
  · show (ring (J + 1)).neg (ring (J + 1)).zero = (ring (J + 1)).zero
    exact (DetChain.neg_unique (ring (J + 1)) _ _ ((ring (J + 1)).zero_add (ring (J + 1)).zero)).symm

theorem pw_p1 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 + 1 = 2 * m + 1)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) :
    Frobenius.pw (G2 J n) x (J + 1 + 1 + 1) = (qnorm (ring (J + 1)) n x, (ring (J + 1)).zero) := by
  show (G2 J n).mul (Frobenius.pw (G2 J n) x (J + 1 + 1)) x = _
  rw [show Frobenius.pw (G2 J n) x (J + 1 + 1) = Frobenius.pw (quad (ring (J + 1)) n) (x.1, x.2) (J + 1 + 1) from rfl,
    frob_conj J n hpr m hJ hn x.1 x.2, (G2 J n).mul_comm]
  exact mul_self_norm (ring (J + 1)) n x

theorem pw_h2 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 + 1 = 2 * m + 1)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) :
    Frobenius.pw (G2 J n) x ((J + 1 + 1 + 1) * m) = (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) m, (ring (J + 1)).zero) := by
  rw [← TwoPower.pwpow, pw_p1 J n m hpr hJ hn]
  exact pw_embed J n _ m

theorem euler_pm (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 + 1 = 2 * m + 1) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1)) (hx : x ≠ (G2 J n).zero) :
    Frobenius.pw (G2 J n) x ((J + 1 + 1 + 1) * m) = (G2 J n).one ∨
      Frobenius.pw (G2 J n) x ((J + 1 + 1 + 1) * m) = (G2 J n).neg (G2 J n).one := by
  have hJ' : J + 1 = 2 * m := Nat.succ.inj (hJ.trans rfl)
  have hN := norm_ne_zero J n hpr m hJ' h1 hn x.1 x.2 hx
  have hsq : (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) m) (Frobenius.pw (ring (J + 1)) (qnorm (ring (J + 1)) n x) m) = (ring (J + 1)).one := by
    rw [← HasseCM.pw_add', show m + m = J + 1 by rw [hJ']; lib_nat]
    exact unit_pow J hpr _ hN
  rw [pw_h2 J n m hpr hJ hn]
  cases Euler.sq_one (ring (J + 1)) (ZMod.nozd J hpr) _ hsq with
  | inl h => left; rw [h]; rfl
  | inr h => right; rw [h]; show ((ring (J + 1)).neg (ring (J + 1)).one, (ring (J + 1)).zero) = (G2 J n).neg ((ring (J + 1)).one, (ring (J + 1)).zero); rw [neg_embed]

theorem euler_sq (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 + 1 = 2 * m + 1) (h1 : 1 ≤ J)
    (hn : Frobenius.pw (ring (J + 1)) n m = (ring (J + 1)).neg (ring (J + 1)).one) (y : Zp (J + 1) × Zp (J + 1)) (hy : y ≠ (G2 J n).zero) :
    Frobenius.pw (G2 J n) ((G2 J n).mul y y) ((J + 1 + 1 + 1) * m) = (G2 J n).one := by
  rw [Frobenius.pw_mul]
  cases euler_pm J n m hpr hJ h1 hn y hy with
  | inl h => rw [h, (G2 J n).one_mul]
  | inr h => rw [h, DetChain.neg_mul_left, (G2 J n).one_mul, DetChain.neg_neg']

end Fq2

#print axioms Fq2.embed_mul
#print axioms Fq2.iv_zero
#print axioms Fq2.iv_mul
#print axioms Fq2.iv_hom
#print axioms Fq2.nozd
#print axioms Fq2.neg_embed
#print axioms Fq2.pw_p1
#print axioms Fq2.pw_h2
#print axioms Fq2.euler_pm
#print axioms Fq2.euler_sq

Listing 61: lang/Fq2Sqrt.lean

lang/Fq2Sqrt.lean

/-
  lang/Fq2Sqrt — T43, cycle 89: square roots in 𝔽_p[√n] for any non-residue n (Euler's criterion, hard half).

  p = 2k + 3, n^(k+1) = −1. If x = a + b√n has x^((p²−1)/2) = 1 then N(x) = a² − n b² = s² in 𝔽_p and
    b = 0:  a = c², or a n = d² and then a = n (d/n)² = (d/n · √n)²
    b ≠ 0:  u± = (a ± s)/2 have u₊u₋ = n b²/4, a non-residue, so one of them is u = c²; then y = c + (b/(2c)) √n
            has y² = (c² + n b²/(4u)) + b √n = a + b√n   (u² − a u + n b²/4 = 0)
    fq_sqrt   ∃ y, y² = x
  Generalises Fp2Sqrt (n = −1) to every non-residue.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Fq2
import lang.Fp2Sqrt

namespace Fq2Sqrt

open FTAChain ZMod YMChain Quad QuadFrob Fq2

variable (k : Nat) (n : Zp (2 * k + 1 + 1))

theorem sq_gen (c d : Zp (2 * k + 1 + 1)) :
    (G2 (2 * k + 1) n).mul (c, d) (c, d) = ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul d d)), (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c d)) := by
  apply Prod.ext
  · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul d d)) = _; rfl
  · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c d) ((ring (2 * k + 1 + 1)).mul d c) = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c d); lib_ring

/-- with 2t = 1 and s² = a² − n b², U = (a + s)t satisfies U² − a U + n b² t² = 0. -/
theorem quad_rel (a b s t : Zp (2 * k + 1 + 1)) (ht : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) t = (ring (2 * k + 1 + 1)).one)
    (hs : (ring (2 * k + 1 + 1)).mul s s = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul b b)))) :
    (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t)) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t))))
      ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t))) = (ring (2 * k + 1 + 1)).zero := by
  have e : (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t)) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t))))
      ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t)))
      = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul t t) ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul s s) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul b b)))))))
          ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t)) ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) t) ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one))) := by
    lib_ring
  rw [e, hs, (ring (2 * k + 1 + 1)).add_neg, ht, (ring (2 * k + 1 + 1)).add_neg, DetChain.mul_zero, DetChain.mul_zero, (ring (2 * k + 1 + 1)).zero_add]

/-- **Square roots in 𝔽_p[√n].** -/
theorem fq_sqrt (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (x : Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1))
    (hx : Frobenius.pw (G2 (2 * k + 1) n) x ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) = (G2 (2 * k + 1) n).one) :
    ∃ y, (G2 (2 * k + 1) n).mul y y = x := by
  match x with
  | (a, b) =>
  rw [pw_h2 (2 * k + 1) n (k + 1) hpr (by lib_nat) hn] at hx
  have hN : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul b b)))) (k + 1) = (ring (2 * k + 1 + 1)).one := by
    have := congrArg Prod.fst hx
    exact this
  have hn0 : n ≠ (ring (2 * k + 1 + 1)).zero := fun e => by
    rw [e, HasseCM.pw_zero_succ] at hn
    exact PowerSum.one_ne_zero' _ (by
      have := congrArg (fun z => (ring (2 * k + 1 + 1)).add z (ring (2 * k + 1 + 1)).one) hn
      exact (((ring (2 * k + 1 + 1)).zero_add (ring (2 * k + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * k + 1 + 1)) (ring (2 * k + 1 + 1)).one))))
  by_cases hb : b = (ring (2 * k + 1 + 1)).zero
  · by_cases ha : a = (ring (2 * k + 1 + 1)).zero
    · exact ⟨((ring (2 * k + 1 + 1)).zero, (ring (2 * k + 1 + 1)).zero), by rw [ha, hb]; exact DetChain.mul_zero (G2 (2 * k + 1) n) _⟩
    · cases Euler.euler_pm k hpr a ha with
      | inl h =>
        match EulerHard.euler_hard k hpr a h with
        | ⟨c, hc⟩ =>
          refine ⟨(c, (ring (2 * k + 1 + 1)).zero), ?_⟩
          rw [sq_gen, hc, hb]
          apply Prod.ext
          · show (ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero (ring (2 * k + 1 + 1)).zero)) = a; lib_ring
          · show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c (ring (2 * k + 1 + 1)).zero) = (ring (2 * k + 1 + 1)).zero; lib_ring
      | inr h =>
        have han : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul a n) (k + 1) = (ring (2 * k + 1 + 1)).one := by rw [Frobenius.pw_mul, h, hn]; lib_ring
        match EulerHard.euler_hard k hpr _ han with
        | ⟨d, hd⟩ =>
          have hni := ConicSum.iv_mul_self k hpr n hn0
          refine ⟨((ring (2 * k + 1 + 1)).zero, (ring (2 * k + 1 + 1)).mul d (ConicSum.iv k n)), ?_⟩
          rw [sq_gen, hb]
          apply Prod.ext
          · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero (ring (2 * k + 1 + 1)).zero) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul d (ConicSum.iv k n)) ((ring (2 * k + 1 + 1)).mul d (ConicSum.iv k n)))) = a
            rw [show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero (ring (2 * k + 1 + 1)).zero) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul d (ConicSum.iv k n)) ((ring (2 * k + 1 + 1)).mul d (ConicSum.iv k n))))
                = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k n) n) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul d d) (ConicSum.iv k n)) by lib_ring, hni, hd]
            rw [show (ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).one ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul a n) (ConicSum.iv k n)) = (ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k n) n) by lib_ring, hni]
            lib_ring
          · show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero ((ring (2 * k + 1 + 1)).mul d (ConicSum.iv k n))) = (ring (2 * k + 1 + 1)).zero
            lib_ring
  · match EulerHard.euler_hard k hpr _ hN with
    | ⟨s, hs⟩ =>
      have h2 := Fp2Sqrt.two_ne k
      let t := ConicSum.iv k ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one)
      have ht : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) t = (ring (2 * k + 1 + 1)).one := by
        rw [(ring (2 * k + 1 + 1)).mul_comm]; exact ConicSum.iv_mul_self k hpr _ h2
      have ht0 : t ≠ (ring (2 * k + 1 + 1)).zero := fun e => by rw [e, DetChain.mul_zero] at ht; exact PowerSum.one_ne_zero' _ ht.symm
      have choose : ∃ s', (ring (2 * k + 1 + 1)).mul s' s' = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul b b))) ∧
          Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s') t) (k + 1) = (ring (2 * k + 1 + 1)).one := by
        have hprod : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t) = (ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b t) ((ring (2 * k + 1 + 1)).mul b t)) := by
          have e : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t)
              = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b t) ((ring (2 * k + 1 + 1)).mul b t)))
                  ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul t t) ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul a a) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul b b)))) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul s s)))) := by lib_ring
          rw [e, hs, (ring (2 * k + 1 + 1)).add_neg, DetChain.mul_zero, DetChain.add_zero']
        have hbt : (ring (2 * k + 1 + 1)).mul b t ≠ (ring (2 * k + 1 + 1)).zero := fun e => by
          cases nozd (2 * k + 1) hpr _ _ e with
          | inl h => exact hb h
          | inr h => exact ht0 h
        have hp : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t)) (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one := by
          rw [hprod, Frobenius.pw_mul, hn, Euler.euler_square k hpr _ hbt]; lib_ring
        rw [Frobenius.pw_mul] at hp
        have hu1 : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s) t ≠ (ring (2 * k + 1 + 1)).zero := fun e => by
          rw [e, HasseCM.pw_zero_succ, DetChain.zero_mul'] at hp
          apply PowerSum.one_ne_zero' (2 * k + 1)
          have := congrArg (fun z => (ring (2 * k + 1 + 1)).add z (ring (2 * k + 1 + 1)).one) hp
          exact (((ring (2 * k + 1 + 1)).zero_add (ring (2 * k + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * k + 1 + 1)) (ring (2 * k + 1 + 1)).one)))
        cases Euler.euler_pm k hpr _ hu1 with
        | inl h1 => exact ⟨s, hs, h1⟩
        | inr h1 =>
          refine ⟨(ring (2 * k + 1 + 1)).neg s, by rw [Thue.negsq (ring (2 * k + 1 + 1))]; exact hs, ?_⟩
          rw [h1] at hp
          have : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t) (k + 1)
              = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a ((ring (2 * k + 1 + 1)).neg s)) t) (k + 1))) := by lib_ring
          rw [this, hp]; lib_ring
      match choose with
      | ⟨s', hs', hu⟩ =>
        match EulerHard.euler_hard k hpr _ hu with
        | ⟨c, hc⟩ =>
          have hu0 : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s') t ≠ (ring (2 * k + 1 + 1)).zero := fun e => by
            rw [e, HasseCM.pw_zero_succ] at hu; exact PowerSum.one_ne_zero' _ hu.symm
          have hc0 : c ≠ (ring (2 * k + 1 + 1)).zero := fun e => hu0 (by rw [← hc, e]; exact DetChain.mul_zero (ring (2 * k + 1 + 1)) _)
          have hci := ConicSum.iv_mul_self k hpr c hc0
          refine ⟨(c, (ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))), ?_⟩
          rw [sq_gen]
          apply Prod.ext
          · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))) ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))))) = a
            have hq := quad_rel k n a b s' t ht hs'
            generalize hU : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add a s') t = U at hq hc
            have hiu : (ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) (ConicSum.iv k c) = ConicSum.iv k U := by
              rw [← hc, ConicSum.inv_mul]
            have hUi : (ring (2 * k + 1 + 1)).mul U (ConicSum.iv k U) = (ring (2 * k + 1 + 1)).one := by
              rw [← hiu, ← hc]
              rw [show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul c c) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) (ConicSum.iv k c))
                  = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) c) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) c) by lib_ring, hci, (ring (2 * k + 1 + 1)).one_mul]
            rw [hc]
            have e : (ring (2 * k + 1 + 1)).add U ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))) ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c)))))
                = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul U U) ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul a U))) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t)))) (ConicSum.iv k U))
                    ((ring (2 * k + 1 + 1)).mul a ((ring (2 * k + 1 + 1)).mul U (ConicSum.iv k U))))
                    ((ring (2 * k + 1 + 1)).mul U ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul U (ConicSum.iv k U))))) := by
              rw [show (ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))) ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))))
                  = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b b) ((ring (2 * k + 1 + 1)).mul t t))) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) (ConicSum.iv k c)) by lib_ring, hiu]
              lib_ring
            rw [e, hq, hUi, (ring (2 * k + 1 + 1)).add_neg]
            lib_ring
          · show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c)))) = b
            have e : (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).mul c ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul t (ConicSum.iv k c))))
                = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul b ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).add (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one) t)) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k c) c) := by lib_ring
            rw [e, ht, hci]; lib_ring

end Fq2Sqrt

#print axioms Fq2Sqrt.sq_gen
#print axioms Fq2Sqrt.quad_rel
#print axioms Fq2Sqrt.fq_sqrt

Listing 62: lang/Fq2Inst.lean

lang/Fq2Inst.lean

/-
  lang/Fq2Inst — T43, cycle 90: 𝔽_{p²} = 𝔽_p[√n] as an instance of FinField.FF for every odd prime p.

  p = 2k + 3, n any non-residue (n^(k+1) = −1), q = p², h = (2k + 4)(k + 1).
    nonroot_gen   in a ring without zero divisors whose elements are listed once each (q = 2h + 1 of them),
                  some unit has x^(k+1) ≠ 1 whenever k + 2 ≤ 2h      (root bound; no FF structure needed)
    ffq           the FF structure: pairs (Fp2), inverses and Euler (Fq2), square roots (Fq2Sqrt), a non-residue
                  (nonroot_gen at k + 1 = h)
  Unlike Fp2Inst (p ≡ 3 mod 4 only) this covers every odd prime: every character-sum theorem for FF holds over every 𝔽_{p²}.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Fq2Sqrt
import lang.FinCyclic

namespace Fq2Inst

open FTAChain ZMod YMChain FiniteRecChain Quad Fp2 Fq2 FinList

theorem nonroot_gen {R : Type} [DecidableEq R] (K : DetChain.CRing R) (hK : DetChain.NoZD K) (els : List R)
    (comp : Complete LibInt.intR els) (h : Nat) (card : sumK LibInt.intR (fun _ => (1 : Int)) els = Int.ofNat (2 * h + 1))
    (k : Nat) (hk : k + 1 + 1 ≤ 2 * h) : ∃ g, g ≠ K.zero ∧ Frobenius.pw K g (k + 1) ≠ K.one := by
  have hlen : els.length = 2 * h + 1 := Int.ofNat.inj ((len_int els).symm.trans card)
  have hc1 : cnt K.zero els = 1 := Int.ofNat.inj ((cnt_int K.zero els).symm.trans (comp K.zero))
  have hl := len_nzL K.zero els
  rw [hc1, hlen] at hl
  have hlen' : (nzL K.zero els).length = 2 * h := Nat.succ.inj hl
  have hd := dist_nzL K.zero els (dist_of_complete els comp)
  have hfind : ∀ l : List R, (∃ g, memL g l ∧ Frobenius.pw K g (k + 1) ≠ K.one) ∨ (∀ g, memL g l → Frobenius.pw K g (k + 1) = K.one) := by
    intro l
    induction l with
    | nil => exact Or.inr (fun _ h => False.elim h)
    | cons a l ih =>
      by_cases ha : Frobenius.pw K a (k + 1) = K.one
      · cases ih with
        | inl h => match h with | ⟨g, hg, e⟩ => exact Or.inl ⟨g, Or.inr hg, e⟩
        | inr h => exact Or.inr (fun g hg => by cases hg with
            | inl e => rw [e]; exact ha
            | inr e => exact h g e)
      · exact Or.inl ⟨a, Or.inl rfl, ha⟩
  cases hfind (nzL K.zero els) with
  | inl hh => match hh with | ⟨g, hg, e⟩ => exact ⟨g, (mem_nzL K.zero g els hg).2, e⟩
  | inr hh =>
    exfalso
    have one_ne : K.one ≠ K.zero := fun e => by
      have : ∀ x, x = K.zero := fun x => by rw [← K.one_mul x, e, DetChain.zero_mul']
      -- then every element equals zero, so els has at most one distinct element
      have h2 : 2 ≤ 2 * h := Nat.le_trans (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le k))) hk
      match hn : nzL K.zero els with
      | [] => rw [hn] at hlen'; rw [← hlen'] at h2; exact Nat.not_succ_le_zero 1 h2
      | a :: _ => exact (mem_nzL K.zero a els (by rw [hn]; exact Or.inl rfl)).2 (this a)
    have := RootBound.root_bound K hK (k + 1) K.one (EulerHard.binomL.zerosThen K k)
      one_ne (EulerHard.len_zerosThen _ k) _ hd
      (fun r hr => by
        show RootBound.evH K r (EulerHard.binomL K k) = _
        rw [EulerHard.ev_binom, hh r hr, K.add_neg])
    rw [hlen'] at this
    exact Nat.not_succ_le_self _ (Nat.le_trans hk this)

theorem h_form (k : Nat) : (2 * k + 1 + 1 + 1 + 1) * (k + 1) = (2 * k * k + 6 * k + 3) + 1 := by lib_nat

/-- **𝔽_{p²} = 𝔽_p[√n] for every odd prime p.** -/
def ffq (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    FinField.FF (Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1)) where
  K := G2 (2 * k + 1) n
  els := pairs (2 * k + 1)
  comp := comp_pairs (2 * k + 1)
  h := (2 * k + 1 + 1 + 1 + 1) * (k + 1)
  card := by
    rw [card_pairs]
    show Int.ofNat ((2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1)) = Int.ofNat (2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1)
    rw [show (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) = 2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1 by lib_nat]
  nozd := Fq2.nozd (2 * k + 1) n (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn
  inv := fun x hx => ⟨Fq2.iv (2 * k + 1) n x, by
    rw [(G2 (2 * k + 1) n).mul_comm]; exact iv_mul (2 * k + 1) n (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn x hx⟩
  euler_pm := fun x hx => Fq2.euler_pm (2 * k + 1) n (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn x hx
  euler_sq := fun y hy => Fq2.euler_sq (2 * k + 1) n (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn y hy
  euler_hard := fun x hx => Fq2Sqrt.fq_sqrt k n hpr hn x hx
  ne_pm := fun e => QChar.ne_pm k (congrArg Prod.fst e)
  nonres := by
    have := nonroot_gen (G2 (2 * k + 1) n) (Fq2.nozd (2 * k + 1) n (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn)
      (pairs (2 * k + 1)) (comp_pairs (2 * k + 1)) ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) (by
        rw [card_pairs]
        show Int.ofNat ((2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1)) = Int.ofNat (2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1)
        rw [show (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) = 2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1 by lib_nat])
      (2 * k * k + 6 * k + 3) (Nat.le.intro (k := 2 * k * k + 6 * k + 3) (by lib_nat))
    rw [← h_form] at this
    exact this
  iv := Fq2.iv (2 * k + 1) n
  iv_mul := fun x hx => iv_mul (2 * k + 1) n (k + 1) hpr (by lib_nat) (Nat.le_add_left 1 _) hn x hx
  iv_zero := iv_zero (2 * k + 1) n
  iv_hom := iv_hom (2 * k + 1) n

end Fq2Inst

#print axioms Fq2Inst.nonroot_gen
#print axioms Fq2Inst.h_form
#print axioms Fq2Inst.ffq

Listing 63: lang/Fq2Trace.lean

lang/Fq2Trace.lean

/-
  lang/Fq2Trace — T43, cycle 92: y² = x³ − x over 𝔽_{p²} for every odd prime p.

  p = 2k + 3, 𝔽_{p²} = 𝔽_p[√n] (Fq2Inst.ffq).
    keyq, keyq_inj    an injective order key (a, b) ↦ a·p + b
    iq, iq_sq         i = (√n)^((p−1)/2) satisfies i² = n^((p−1)/2) = −1: every 𝔽_{p²} contains i
    fq_ap_primary     −Σ_{z ∈ 𝔽_{p²}} χ(z³ − z) = 2A with p² = A² + B², A + Bi primary      (FinPrimary)
    fq_hasse          (Σ_{z ∈ 𝔽_{p²}} χ(z³ − z))² ≤ 4p²                                     (Hasse's bound)
  So over 𝔽_{p²} the Frobenius trace of the CM curve is 2 Re of a primary Gaussian integer of norm p², at every p — split
  (p ≡ 1 mod 4) as well as inert.

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Fq2Inst
import lang.FinPrimary
import lang.Fp2Count
import lang.CubicSuper
import lang.FinCubicSign

namespace Fq2Trace

open FTAChain ZMod YMChain FiniteRecChain Quad Fq2

variable (k : Nat)

def keyq (x : Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1)) : Nat := x.1.val * (2 * k + 1 + 1 + 1) + x.2.val

theorem keyq_inj (x y : Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1)) (h : keyq k x = keyq k y) : x = y :=
  match Fp2Count.divmod_unique _ _ _ _ _ x.2.property y.2.property h with
  | ⟨h1, h2⟩ => Prod.ext (Subtype.ext h1) (Subtype.ext h2)

def iq (n : Zp (2 * k + 1 + 1)) : Zp (2 * k + 1 + 1) × Zp (2 * k + 1 + 1) :=
  Frobenius.pw (G2 (2 * k + 1) n) ((ring (2 * k + 1 + 1)).zero, (ring (2 * k + 1 + 1)).one) (k + 1)

theorem iq_sq (n : Zp (2 * k + 1 + 1)) (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    (G2 (2 * k + 1) n).mul (iq k n) (iq k n) = (G2 (2 * k + 1) n).neg (G2 (2 * k + 1) n).one := by
  unfold iq
  rw [← Frobenius.pw_mul]
  have e : (G2 (2 * k + 1) n).mul ((ring (2 * k + 1 + 1)).zero, (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).zero, (ring (2 * k + 1 + 1)).one)
      = (n, (ring (2 * k + 1 + 1)).zero) := by
    apply Prod.ext
    · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero (ring (2 * k + 1 + 1)).zero)
        ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).one)) = n
      lib_ring
    · show (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).zero (ring (2 * k + 1 + 1)).one)
        ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).one (ring (2 * k + 1 + 1)).zero) = (ring (2 * k + 1 + 1)).zero
      lib_ring
  rw [e, QuadFrob.pw_embed (2 * k + 1) n n (k + 1), hn, ← Fq2.neg_embed]
  rfl

variable (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) (n : Zp (2 * k + 1 + 1))
  (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one)

/-- **y² = x³ − x over every 𝔽_{p²}: a = 2 Re π, π primary of norm p².** -/
theorem fq_ap_primary :
    ∃ A B : Int, A * A + B * B = Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1) ∧ GaussInt.Primary ⟨A, B⟩ ∧
      -sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els = 2 * A := by
  match FinPrimary.fin_ap_primary (Fq2Inst.ffq k hpr n hn) (keyq k) (keyq_inj k) (iq k n) (iq_sq k n hn) with
  | ⟨A, B, hAB, hP, hS⟩ =>
    refine ⟨A, B, ?_, hP, hS⟩
    rw [hAB]
    show Int.ofNat (2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1) = Int.ofNat ((2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1))
    rw [CubicSuper.sq_id k]

/-- **Hasse's bound over every 𝔽_{p²}**: a² + 4B² = 4p². -/
theorem fq_hasse : ∃ s : Nat,
    sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els
      * sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els
      + Int.ofNat s = 4 * (Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1)) := by
  match fq_ap_primary k hpr n hn with
  | ⟨A, B, hAB, _, hS⟩ =>
    match FinCubicSign.sq_nn B with
    | ⟨m, hm⟩ =>
      refine ⟨4 * m, ?_⟩
      generalize sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els = S at hS ⊢
      have e : S = -(2 * A) := by rw [← hS]; lib_int
      rw [e, ← hAB, show Int.ofNat (4 * m) = 4 * Int.ofNat m from rfl, ← hm]
      lib_int

end Fq2Trace

#print axioms Fq2Trace.keyq
#print axioms Fq2Trace.keyq_inj
#print axioms Fq2Trace.iq
#print axioms Fq2Trace.iq_sq
#print axioms Fq2Trace.fq_ap_primary
#print axioms Fq2Trace.fq_hasse

78.25 Finite fields from a list: Euler’s criterion by counting, quadratic towers

In a ring without zero divisors whose \(q=2h+1\) elements are listed, with \(-1\ne 1\) and \(x^{q-1}=1\) for \(x\ne 0\), every \(x\) with \(x^h=1\) is a square; hence such a ring is a finite field of the interface, and so is \(\mathbb F_q[\sqrt n]\) for a non-residue \(n\). The steps are

\[ \begin {aligned} &S=\{y^2:y\ne 0\}\ \text {(distinct)},\qquad \textstyle \sum _{s\in S}\#\{y:y^2=s\}=2h,\qquad \#\{y:y^2=s\}\le 2\ (y^2=y_0^2\Rightarrow y=\pm y_0),\\ &|S|\ge h,\ s^h=1\ \text {on}\ S,\ x\notin S\Rightarrow h+1\ \text {roots of}\ X^h-1,\ \text {impossible},\\ &N(a+b\sqrt n)=a^2-nb^2\ne 0\ \text {for}\ (a,b)\ne 0,\qquad x^{-1}=\bar x\,N(x)^{-1},\qquad x^{q^2-1}=1\ \text {(units permute)},\\ &\mathbb F_p\subset \mathbb F_{p^2}\subset \mathbb F_{p^4}\subset \cdots ,\qquad |\mathbb F^{(k+1)}|=|\mathbb F^{(k)}|^2 . \end {aligned} \]

Listing 64: lang/GenericEuler.lean

lang/GenericEuler.lean

/-
  lang/GenericEuler — T43, cycle 137: Euler's criterion (hard half) in any finite field, from Fermat and counting alone.

  K a commutative ring without zero divisors whose q = 2h + 1 elements are listed once each, with −1 ≠ 1 and x^(2h) = 1 for
  x ≠ 0. Then x^h = 1 ⇒ x is a square:
    memD, sqs                the distinct squares of the units (built by insertion)
    sq_two                   a distinct list of square roots of one element has length ≤ 2 (y² = y₀² ⇒ y = ±y₀)
    sum_fiber                Σ_{s ∈ S} #{y ∈ U : y² = s} = |U|
    h_le_sqs                 h ≤ |S|
    generic_euler_hard       x^h = 1 ⇒ ∃ y, y² = x       (S ∪ {x} would be h + 1 roots of X^h − 1: RootBound)
  No explicit square roots are needed, so every field built from a list (towers of quadratic extensions, cubic
  extensions) gets Euler's criterion for free.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.FinDLog
import lang.FinList

namespace GenericEuler

open FTAChain YMChain FiniteRecChain FinList RootBound FinDLog

variable {R : Type} [DecidableEq R] (K : DetChain.CRing R)

def memD (a : R) : List R → Bool
  | [] => false
  | b :: l => if a = b then true else memD a l

theorem memD_true (a : R) : ∀ l : List R, memD a l = true → memL a l
  | [], h => Bool.noConfusion h
  | b :: l, h => by
    unfold memD at h
    by_cases e : a = b
    · exact Or.inl e
    · rw [ite_neg' _ _ e] at h; exact Or.inr (memD_true a l h)

theorem memD_false (a : R) : ∀ l : List R, memD a l = false → ¬ memL a l
  | [], _, h => h
  | b :: l, h, hm => by
    unfold memD at h
    by_cases e : a = b
    · rw [ite_pos' _ _ e] at h; exact Bool.noConfusion h
    · rw [ite_neg' _ _ e] at h
      cases hm with
      | inl e' => exact e e'
      | inr e' => exact memD_false a l h e'

def sqs : List R → List R
  | [] => []
  | y :: l => if memD (K.mul y y) (sqs l) = true then sqs l else K.mul y y :: sqs l

theorem sqs_dist : ∀ l : List R, Dist (sqs K l)
  | [] => trivial
  | y :: l => by
    unfold sqs
    cases hm : memD (K.mul y y) (sqs K l) with
    | true => rw [ite_pos' _ _ rfl]; exact sqs_dist l
    | false =>
      rw [ite_neg' _ _ (fun e => Bool.noConfusion e)]
      exact ⟨fun s hs e => memD_false _ _ hm (e ▸ hs), sqs_dist l⟩

theorem sqs_cover : ∀ (l : List R) (y : R), memL y l → memL (K.mul y y) (sqs K l)
  | [], _, h => False.elim h
  | b :: l, y, h => by
    unfold sqs
    cases hm : memD (K.mul b b) (sqs K l) with
    | true =>
      rw [ite_pos' _ _ rfl]
      cases h with
      | inl e => rw [e]; exact memD_true _ _ hm
      | inr e => exact sqs_cover l y e
    | false =>
      rw [ite_neg' _ _ (fun e => Bool.noConfusion e)]
      cases h with
      | inl e => rw [e]; exact Or.inl rfl
      | inr e => exact Or.inr (sqs_cover l y e)

theorem sqs_from : ∀ (l : List R) (s : R), memL s (sqs K l) → ∃ y, memL y l ∧ K.mul y y = s
  | [], _, h => False.elim h
  | b :: l, s, h => by
    unfold sqs at h
    cases hm : memD (K.mul b b) (sqs K l) with
    | true =>
      rw [hm, ite_pos' _ _ rfl] at h
      match sqs_from l s h with | ⟨y, hy, e⟩ => exact ⟨y, Or.inr hy, e⟩
    | false =>
      rw [hm, ite_neg' _ _ (fun e => Bool.noConfusion e)] at h
      cases h with
      | inl e => exact ⟨b, Or.inl rfl, e.symm⟩
      | inr e => match sqs_from l s e with | ⟨y, hy, e'⟩ => exact ⟨y, Or.inr hy, e'⟩

/-- number of y in l with y² = s. -/
def fib (s : R) : List R → Nat
  | [] => 0
  | y :: l => (if K.mul y y = s then 1 else 0) + fib s l

omit [DecidableEq R] in
theorem sq_eq (hK : DetChain.NoZD K) (a b : R) (h : K.mul a a = K.mul b b) : a = b ∨ a = K.neg b := by
  have e : K.mul (K.add a (K.neg b)) (K.add a b) = K.zero := by
    rw [show K.mul (K.add a (K.neg b)) (K.add a b) = K.add (K.mul a a) (K.neg (K.mul b b)) by lib_ring, h, K.add_neg]
  cases hK _ _ e with
  | inl h1 =>
    left
    have h2 : K.add (K.add a (K.neg b)) b = K.add K.zero b := congrArg (fun z => K.add z b) h1
    rw [show K.add (K.add a (K.neg b)) b = a by lib_ring, K.zero_add] at h2
    exact h2
  | inr h1 =>
    right
    exact DetChain.neg_unique K _ _ h1

def flt (s : R) : List R → List R
  | [] => []
  | y :: l => if K.mul y y = s then y :: flt s l else flt s l

theorem fib_len (s : R) : ∀ l : List R, fib K s l = (flt K s l).length
  | [] => rfl
  | y :: l => by
    unfold fib flt
    by_cases e : K.mul y y = s
    · rw [ite_pos' _ _ e, ite_pos' _ _ e, fib_len s l]; show 1 + _ = _ + 1; lib_nat
    · rw [ite_neg' _ _ e, ite_neg' _ _ e, fib_len s l, Nat.zero_add]

theorem flt_mem (s : R) : ∀ (l : List R) (y : R), memL y (flt K s l) → memL y l ∧ K.mul y y = s
  | [], _, h => False.elim h
  | b :: l, y, h => by
    unfold flt at h
    by_cases e : K.mul b b = s
    · rw [ite_pos' _ _ e] at h
      cases h with
      | inl e' => exact ⟨Or.inl e', by rw [e']; exact e⟩
      | inr e' => have := flt_mem s l y e'; exact ⟨Or.inr this.1, this.2⟩
    · rw [ite_neg' _ _ e] at h
      have := flt_mem s l y h; exact ⟨Or.inr this.1, this.2⟩

theorem flt_dist (s : R) : ∀ l : List R, Dist l → Dist (flt K s l)
  | [], _ => trivial
  | b :: l, hd => by
    unfold flt
    by_cases e : K.mul b b = s
    · rw [ite_pos' _ _ e]; exact ⟨fun t ht => hd.1 t (flt_mem K s l t ht).1, flt_dist s l hd.2⟩
    · rw [ite_neg' _ _ e]; exact flt_dist s l hd.2

omit [DecidableEq R] in
/-- **At most two square roots.** -/
theorem len_le2 (hK : DetChain.NoZD K) (s : R) :
    ∀ L : List R, Dist L → (∀ y, memL y L → K.mul y y = s) → L.length ≤ 2
  | [], _, _ => Nat.zero_le 2
  | [_], _, _ => Nat.le_succ 1
  | [_, _], _, _ => Nat.le_refl 2
  | a :: b :: c :: rest, hd, hs => by
    exfalso
    have hab := sq_eq K hK b a ((hs b (Or.inr (Or.inl rfl))).trans (hs a (Or.inl rfl)).symm)
    have hac := sq_eq K hK c a ((hs c (Or.inr (Or.inr (Or.inl rfl)))).trans (hs a (Or.inl rfl)).symm)
    have nba : b ≠ a := hd.1 b (Or.inl rfl)
    have nca : c ≠ a := hd.1 c (Or.inr (Or.inl rfl))
    have ncb : c ≠ b := hd.2.1 c (Or.inl rfl)
    cases hab with
    | inl e => exact nba e
    | inr eb =>
      cases hac with
      | inl e => exact nca e
      | inr ec => exact ncb (ec.trans eb.symm)

theorem fib_le2 (hK : DetChain.NoZD K) (s : R) (l : List R) (hd : Dist l) :
    fib K s l ≤ 2 := by
  rw [fib_len]
  exact len_le2 K hK s _ (flt_dist K s l hd) (fun y hy => (flt_mem K s l y hy).2)

theorem ind_cnt (y : R) : ∀ S : List R, sumN (fun s => if K.mul y y = s then 1 else 0) S = cnt (K.mul y y) S
  | [] => rfl
  | a :: S => by
    show (if K.mul y y = a then 1 else 0) + _ = (if a = K.mul y y then 1 else 0) + _
    rw [ind_cnt y S]
    by_cases e : K.mul y y = a
    · rw [ite_pos' _ _ e, ite_pos' _ _ e.symm]
    · rw [ite_neg' _ _ e, ite_neg' _ _ (fun e' => e e'.symm)]

/-- **Σ_{s ∈ S} #{y ∈ U : y² = s} = |U|.** -/
theorem sum_fiber (S : List R) (hS : Dist S) : ∀ U : List R, (∀ y, memL y U → memL (K.mul y y) S) →
    sumN (fun s => fib K s U) S = U.length
  | [], _ => by
    show sumN (fun _ => 0) S = 0
    have : ∀ l : List R, sumN (fun _ => (0 : Nat)) l = 0 := by
      intro l; induction l with
      | nil => rfl
      | cons _ l ih => show 0 + _ = 0; rw [ih]
    exact this S
  | y :: U, hU => by
    show sumN (fun s => (if K.mul y y = s then 1 else 0) + fib K s U) S = U.length + 1
    rw [sumN_add, ind_cnt, sum_fiber S hS U (fun z hz => hU z (Or.inr hz))]
    have h1 : cnt (K.mul y y) S = 1 :=
      Nat.le_antisymm (cnt_le_one_of_dist _ S hS) (cnt_pos _ S (hU y (Or.inl rfl)))
    rw [h1, Nat.add_comm]

omit [DecidableEq R] in
theorem sumN_const2 : ∀ S : List R, sumN (fun _ => (2 : Nat)) S = 2 * S.length
  | [] => rfl
  | _ :: S => by show 2 + sumN _ S = 2 * (S.length + 1); rw [sumN_const2 S]; lib_nat

/-- **Euler's criterion, hard half, in any finite field.** -/
theorem generic_euler_hard (hK : DetChain.NoZD K) (els : List R) (comp : Complete LibInt.intR els) (h : Nat) (hh : 1 ≤ h)
    (card : sumK LibInt.intR (fun _ => (1 : Int)) els = Int.ofNat (2 * h + 1))
    (hferm : ∀ x, x ≠ K.zero → Frobenius.pw K x (2 * h) = K.one) (hne1 : K.neg K.one ≠ K.one)
    (x : R) (hx : Frobenius.pw K x h = K.one) : ∃ y, K.mul y y = x := by
  have hlen : els.length = 2 * h + 1 := Int.ofNat.inj ((len_int els).symm.trans card)
  have hc1 : cnt K.zero els = 1 := Int.ofNat.inj ((cnt_int K.zero els).symm.trans (comp K.zero))
  have hl := len_nzL K.zero els
  rw [hc1, hlen] at hl
  have hU : (nzL K.zero els).length = 2 * h := Nat.succ.inj hl
  have hUd := dist_nzL K.zero els (dist_of_complete els comp)
  have hS := sqs_dist K (nzL K.zero els)
  have one0 : K.one ≠ K.zero := fun e => hne1 (by
    rw [e]; exact (DetChain.neg_unique K _ _ (K.zero_add K.zero)).symm)
  have hcount : 2 * h ≤ 2 * (sqs K (nzL K.zero els)).length := by
    rw [← hU, ← sum_fiber K _ hS _ (sqs_cover K _), ← sumN_const2]
    exact sumN_le _ _ (fun s => fib_le2 K hK s _ hUd) _
  have hle : h ≤ (sqs K (nzL K.zero els)).length := Nat.le_of_mul_le_mul_left hcount (Nat.zero_lt_succ 1)
  have hroots : ∀ s, memL s (sqs K (nzL K.zero els)) → Frobenius.pw K s h = K.one := fun s hs => by
    match sqs_from K _ s hs with
    | ⟨y, hy, e⟩ =>
      rw [← e, Frobenius.pw_mul, ← HasseCM.pw_add', show h + h = 2 * h by lib_nat]
      exact hferm y (mem_nzL K.zero y els hy).2
  cases hm : memD x (sqs K (nzL K.zero els)) with
  | true =>
    match sqs_from K _ x (memD_true _ _ hm) with
    | ⟨y, _, e⟩ => exact ⟨y, e⟩
  | false =>
    exfalso
    cases h with
    | zero => exact Nat.not_succ_le_zero 0 hh
    | succ k =>
      have hb := root_bound K hK (k + 1) K.one (EulerHard.binomL.zerosThen K k) one0 (EulerHard.len_zerosThen _ k)
        (x :: sqs K (nzL K.zero els)) ⟨fun s hs e => memD_false _ _ hm (e ▸ hs), hS⟩
        (fun r hr => by
          show evH K r (EulerHard.binomL K k) = _
          rw [EulerHard.ev_binom]
          cases hr with
          | inl e => rw [e, hx, K.add_neg]
          | inr e => rw [hroots r e, K.add_neg])
      exact Nat.not_succ_le_self _ (Nat.le_trans (Nat.succ_le_succ hle) hb)

end GenericEuler

#print axioms GenericEuler.memD_true
#print axioms GenericEuler.memD_false
#print axioms GenericEuler.sqs_dist
#print axioms GenericEuler.sqs_cover
#print axioms GenericEuler.sqs_from
#print axioms GenericEuler.sq_eq
#print axioms GenericEuler.fib_len
#print axioms GenericEuler.flt_mem
#print axioms GenericEuler.flt_dist
#print axioms GenericEuler.len_le2
#print axioms GenericEuler.fib_le2
#print axioms GenericEuler.ind_cnt
#print axioms GenericEuler.sum_fiber
#print axioms GenericEuler.sumN_const2
#print axioms GenericEuler.generic_euler_hard

Listing 65: lang/MkFF.lean

lang/MkFF.lean

/-
  lang/MkFF — T43, cycle 138: a finite field of the interface from a ring, its element list, Fermat and −1 ≠ 1.

  Given K without zero divisors, its q = 2(k + 1) + 1 elements listed once each, −1 ≠ 1 and x^(q−1) = 1 for x ≠ 0:
    mkFF     the FF structure: inverses x^(q−2), Euler's criterion ±1 (sq_one), squares, the hard half (GenericEuler),
             a non-residue (Fq2Inst.nonroot_gen), and the multiplicative inverse map
  Every further field (quadratic towers, cubic extensions) only has to supply its ring, its list and Fermat.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.GenericEuler
import lang.Fq2Inst
import lang.Euler

namespace MkFF

open FTAChain YMChain FiniteRecChain

def mkFF {R : Type} [DecidableEq R] (K : DetChain.CRing R) (hK : DetChain.NoZD K) (els : List R)
    (comp : Complete LibInt.intR els) (k : Nat)
    (card : sumK LibInt.intR (fun _ => (1 : Int)) els = Int.ofNat (2 * (k + 1) + 1))
    (hne1 : K.neg K.one ≠ K.one) (hferm : ∀ x, x ≠ K.zero → Frobenius.pw K x (2 * (k + 1)) = K.one) :
    FinField.FF R where
  K := K
  els := els
  comp := comp
  h := k + 1
  card := card
  nozd := hK
  inv := fun x hx => ⟨Frobenius.pw K x (2 * k + 1), by
    rw [K.mul_comm]; show K.mul (Frobenius.pw K x (2 * k + 1)) x = K.one
    rw [show K.mul (Frobenius.pw K x (2 * k + 1)) x = Frobenius.pw K x (2 * (k + 1)) from rfl]
    exact hferm x hx⟩
  euler_pm := fun x hx => Euler.sq_one K hK _ (by
    rw [← HasseCM.pw_add', show k + 1 + (k + 1) = 2 * (k + 1) by lib_nat]; exact hferm x hx)
  euler_sq := fun y hy => by
    rw [Frobenius.pw_mul, ← HasseCM.pw_add', show k + 1 + (k + 1) = 2 * (k + 1) by lib_nat]; exact hferm y hy
  euler_hard := fun x hx => GenericEuler.generic_euler_hard K hK els comp (k + 1) (Nat.le_add_left 1 k) card hferm hne1 x hx
  ne_pm := hne1
  nonres := Fq2Inst.nonroot_gen K hK els comp (k + 1) card k (Nat.le.intro (k := k) (by lib_nat))
  iv := fun x => Frobenius.pw K x (2 * k + 1)
  iv_mul := fun x hx => hferm x hx
  iv_zero := HasseCM.pw_zero_succ K (2 * k)
  iv_hom := fun x y => Frobenius.pw_mul K x y (2 * k + 1)

end MkFF

#print axioms MkFF.mkFF

Listing 66: lang/QuadExt.lean

lang/QuadExt.lean

/-
  lang/QuadExt — T43, cycle 139: the quadratic extension F[√n] of any finite field F of the interface.

  F of order q = 2h + 1 (h = j + 1), n a non-residue of F. E = quad F n on pairs:
    pairsL, comp_pairs, card_pairs     the q² pairs, listed once each
    cnt_nzL                            counts in the list of nonzero pairs
    norm_ne                            N(x) = a² − n b² ≠ 0 for x ≠ 0     (n is not a square)
    ivE, ivE_mul, nozdE                inverses x̄ · N(x)⁻¹, no zero divisors
    fermatE                            x^(q²−1) = 1                        (FiniteField.unit_pow)
    quadExt                            the FF structure on E               (MkFF)
  So 𝔽_{q²} is an instance whenever 𝔽_q is: the towers 𝔽_{p^(2^k)} and all FF theorems over them.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat, lib_int), no axioms.
-/
import lang.MkFF
import lang.FiniteField
import lang.Quad

namespace QuadExt

open FTAChain YMChain FiniteRecChain FinField Quad

variable {R : Type} [DecidableEq R]

abbrev Z := LibInt.intR

def pairsL (els : List R) : List R → List (R × R)
  | [] => []
  | a :: l => els.map (fun b => (a, b)) ++ pairsL els l

theorem sumK_map {A B : Type} (f : B → Int) (g : A → B) : ∀ l : List A, sumK Z f (l.map g) = sumK Z (fun a => f (g a)) l
  | [] => rfl
  | a :: l => by show f (g a) + sumK Z f (l.map g) = _; rw [sumK_map f g l]; rfl

omit [DecidableEq R] in
theorem sum_pairsL (els : List R) (G : R × R → Int) : ∀ l : List R,
    sumK Z G (pairsL els l) = sumK Z (fun a => sumK Z (fun b => G (a, b)) els) l
  | [] => rfl
  | a :: l => by
    show sumK Z G (els.map (fun b => (a, b)) ++ pairsL els l) = _
    rw [CMInert.sumK_app', sum_pairsL els G l, sumK_map]
    rfl

theorem delta_pair (y z : R × R) : delta Z y z = delta Z y.1 z.1 * delta Z y.2 z.2 := by
  unfold delta
  by_cases h1 : y.1 = z.1
  · by_cases h2 : y.2 = z.2
    · rw [ite_pos' _ _ (Prod.ext h1 h2), ite_pos' _ _ h1, ite_pos' _ _ h2]; rfl
    · rw [ite_neg' _ _ (fun e => h2 (congrArg Prod.snd e)), ite_pos' _ _ h1, ite_neg' _ _ h2]; rfl
  · rw [ite_neg' _ _ (fun e => h1 (congrArg Prod.fst e)), ite_neg' _ _ h1]; exact (DetChain.zero_mul' Z _).symm

theorem comp_pairs (els : List R) (hc : Complete Z els) : Complete Z (pairsL els els) := fun z => by
  rw [sum_pairsL]
  have pt : ∀ a, sumK Z (fun b => delta Z (a, b) z) els = delta Z a z.1 := fun a => by
    rw [sumK_ext Z _ (fun b => Z.mul (delta Z a z.1) (delta Z b z.2)) (fun b => delta_pair (a, b) z), ← mul_sumK, hc z.2]
    show delta Z a z.1 * 1 = _; lib_int
  rw [sumK_ext Z _ _ pt]
  exact hc z.1

omit [DecidableEq R] in
theorem card_pairs (els : List R) (q : Int) (hq : sumK Z (fun _ => (1 : Int)) els = q) :
    sumK Z (fun _ => (1 : Int)) (pairsL els els) = q * q := by
  rw [sum_pairsL, sumK_ext Z _ (fun _ => q) (fun _ => hq)]
  rw [show sumK Z (fun _ => q) els = Z.mul q (sumK Z (fun _ => (1 : Int)) els) by
    rw [mul_sumK]; exact sumK_ext Z _ _ (fun _ => by show _ = q * 1; lib_int) _, hq]
  rfl

theorem cnt_eq (z : R) : ∀ l : List R, FiniteField.cnt z l = FinList.cnt z l
  | [] => rfl
  | a :: l => by show (if a = z then 1 else 0) + _ = (if a = z then 1 else 0) + _; rw [cnt_eq z l]

theorem cnt_nzL (z w : R) : ∀ l : List R, FinList.cnt z (FinList.nzL w l) = if z = w then 0 else FinList.cnt z l
  | [] => by
    show 0 = if z = w then 0 else 0
    by_cases e : z = w
    · rw [ite_pos' _ _ e]
    · rw [ite_neg' _ _ e]
  | a :: l => by
    show FinList.cnt z (if a = w then FinList.nzL w l else a :: FinList.nzL w l) = _
    by_cases ea : a = w
    · rw [ite_pos' _ _ ea, cnt_nzL z w l]
      show _ = if z = w then 0 else (if a = z then 1 else 0) + FinList.cnt z l
      by_cases ez : z = w
      · rw [ite_pos' _ _ ez, ite_pos' _ _ ez]
      · rw [ite_neg' _ _ ez, ite_neg' _ _ ez, ite_neg' _ _ (fun (e : a = z) => ez (e ▸ ea)), Nat.zero_add]
    · rw [ite_neg' _ _ ea]
      show (if a = z then 1 else 0) + FinList.cnt z (FinList.nzL w l) = _
      rw [cnt_nzL z w l]
      by_cases ez : z = w
      · rw [ite_pos' _ _ ez, ite_pos' _ _ ez, ite_neg' _ _ (fun e => ea (e.trans ez))]
      · rw [ite_neg' _ _ ez, ite_neg' _ _ ez]; rfl

variable (F : FF R) (n : R)

theorem norm_ne (hn0 : n ≠ F.K.zero) (hnr : Frobenius.pw F.K n F.h ≠ F.K.one) (x : R × R) (hx : x ≠ (quad F.K n).zero) : qnorm F.K n x ≠ F.K.zero := fun hN => by
  by_cases hb : x.2 = F.K.zero
  · have ha : x.1 ≠ F.K.zero := fun ha => hx (Prod.ext ha hb)
    have : F.K.mul x.1 x.1 = F.K.zero := by
      rw [← hN]; show _ = F.K.add (F.K.mul x.1 x.1) (F.K.neg (F.K.mul n (F.K.mul x.2 x.2))); rw [hb]; lib_ring
    cases F.nozd _ _ this with
    | inl h => exact ha h
    | inr h => exact ha h
  · have hab : F.K.mul x.1 x.1 = F.K.mul n (F.K.mul x.2 x.2) := FinMoment.eq_of_sub_zero F _ _ hN
    have hbi := F.iv_mul x.2 hb
    have ht : F.K.mul (F.K.mul x.1 (F.iv x.2)) (F.K.mul x.1 (F.iv x.2)) = n := by
      rw [show F.K.mul (F.K.mul x.1 (F.iv x.2)) (F.K.mul x.1 (F.iv x.2)) = F.K.mul (F.K.mul x.1 x.1) (F.K.mul (F.iv x.2) (F.iv x.2)) by lib_ring,
        hab, show F.K.mul (F.K.mul n (F.K.mul x.2 x.2)) (F.K.mul (F.iv x.2) (F.iv x.2)) = F.K.mul n (F.K.mul (F.K.mul (F.iv x.2) x.2) (F.K.mul (F.iv x.2) x.2)) by lib_ring,
        hbi]
      lib_ring
    have ht0 : F.K.mul x.1 (F.iv x.2) ≠ F.K.zero := fun e => hn0 (by rw [← ht, e]; exact DetChain.mul_zero F.K _)
    exact hnr (by rw [← ht]; exact F.euler_sq _ ht0)

def ivE (x : R × R) : R × R := (quad F.K n).mul (qconj F.K x) (F.iv (qnorm F.K n x), F.K.zero)

theorem ivE_mul (hn0 : n ≠ F.K.zero) (hnr : Frobenius.pw F.K n F.h ≠ F.K.one) (x : R × R) (hx : x ≠ (quad F.K n).zero) : (quad F.K n).mul (ivE F n x) x = (quad F.K n).one := by
  have hN := F.iv_mul _ (norm_ne F n hn0 hnr x hx)
  rw [show (quad F.K n).mul (ivE F n x) x = (quad F.K n).mul ((quad F.K n).mul x (qconj F.K x)) (F.iv (qnorm F.K n x), F.K.zero) by
      unfold ivE; lib_ring, mul_self_norm]
  apply Prod.ext
  · show F.K.add (F.K.mul (qnorm F.K n x) (F.iv (qnorm F.K n x))) (F.K.mul n (F.K.mul F.K.zero F.K.zero)) = F.K.one
    rw [F.K.mul_comm (qnorm F.K n x), hN]; lib_ring
  · show F.K.add (F.K.mul (qnorm F.K n x) F.K.zero) (F.K.mul F.K.zero (F.iv (qnorm F.K n x))) = F.K.zero
    lib_ring

theorem nozdE (hn0 : n ≠ F.K.zero) (hnr : Frobenius.pw F.K n F.h ≠ F.K.one) : DetChain.NoZD (quad F.K n) := fun x y e => by
  by_cases hx : x = (quad F.K n).zero
  · exact Or.inl hx
  · right
    calc y = (quad F.K n).mul (quad F.K n).one y := ((quad F.K n).one_mul y).symm
      _ = (quad F.K n).mul ((quad F.K n).mul (ivE F n x) x) y := by rw [ivE_mul F n hn0 hnr x hx]
      _ = (quad F.K n).mul (ivE F n x) ((quad F.K n).mul x y) := (quad F.K n).mul_assoc _ _ _
      _ = (quad F.K n).zero := by rw [e]; exact DetChain.mul_zero (quad F.K n) _

theorem sq_form (j : Nat) : (2 * (j + 1) + 1) * (2 * (j + 1) + 1) = 2 * (2 * j * j + 6 * j + 3 + 1) + 1 := by lib_nat

theorem cardE (j : Nat) (hj : F.h = j + 1) :
    sumK Z (fun _ => (1 : Int)) (pairsL F.els F.els) = Int.ofNat (2 * (2 * j * j + 6 * j + 3 + 1) + 1) := by
  rw [card_pairs F.els _ F.card, hj, ← sq_form j]; rfl

theorem fermatE (j : Nat) (hj : F.h = j + 1) (hn0 : n ≠ F.K.zero) (hnr : Frobenius.pw F.K n F.h ≠ F.K.one)
    (x : R × R) (hx : x ≠ (quad F.K n).zero) :
    Frobenius.pw (quad F.K n) x (2 * (2 * j * j + 6 * j + 3 + 1)) = (quad F.K n).one := by
  have hc : ∀ z, FinList.cnt z (pairsL F.els F.els) = 1 := fun z =>
    Int.ofNat.inj ((FinList.cnt_int z _).symm.trans (comp_pairs F.els F.comp z))
  have hl := FinList.len_nzL (quad F.K n).zero (pairsL F.els F.els)
  have hlen : (pairsL F.els F.els).length = 2 * (2 * j * j + 6 * j + 3 + 1) + 1 :=
    Int.ofNat.inj ((FinList.len_int _).symm.trans (cardE F j hj))
  rw [hc, hlen] at hl
  have hus : (FinList.nzL (quad F.K n).zero (pairsL F.els F.els)).length = 2 * (2 * j * j + 6 * j + 3 + 1) := Nat.succ.inj hl
  rw [← hus]
  apply FiniteField.unit_pow (quad F.K n) (nozdE F n hn0 hnr) (ivE F n) (ivE_mul F n hn0 hnr)
  · intro z
    rw [cnt_eq, cnt_nzL]
    by_cases e : z = (quad F.K n).zero
    · rw [ite_pos' _ _ e, ite_pos' _ _ e]
    · rw [ite_neg' _ _ e, ite_neg' _ _ e, hc]
  · intro z hz e
    rw [cnt_eq, cnt_nzL, ite_pos' _ _ e] at hz
    exact Nat.lt_irrefl 0 hz
  · exact fun e => FinConic.one_ne_zero F (congrArg Prod.fst e)
  · exact hx

/-- **𝔽_{q²} = F[√n] as a finite field of the interface.** -/
def quadExt (j : Nat) (hj : F.h = j + 1) (hn0 : n ≠ F.K.zero) (hnr : Frobenius.pw F.K n F.h ≠ F.K.one) : FF (R × R) :=
  MkFF.mkFF (quad F.K n) (nozdE F n hn0 hnr) (pairsL F.els F.els) (comp_pairs F.els F.comp) (2 * j * j + 6 * j + 3)
    (cardE F j hj) (fun e => F.ne_pm (congrArg Prod.fst e)) (fermatE F n j hj hn0 hnr)

end QuadExt

#print axioms QuadExt.pairsL
#print axioms QuadExt.sumK_map
#print axioms QuadExt.sum_pairsL
#print axioms QuadExt.delta_pair
#print axioms QuadExt.comp_pairs
#print axioms QuadExt.card_pairs
#print axioms QuadExt.cnt_eq
#print axioms QuadExt.cnt_nzL
#print axioms QuadExt.norm_ne
#print axioms QuadExt.ivE
#print axioms QuadExt.ivE_mul
#print axioms QuadExt.nozdE
#print axioms QuadExt.sq_form
#print axioms QuadExt.cardE
#print axioms QuadExt.fermatE
#print axioms QuadExt.quadExt

Listing 67: lang/FieldTower.lean

lang/FieldTower.lean

/-
  lang/FieldTower — T43, cycle 140: the tower 𝔽_p ⊂ 𝔽_{p²} ⊂ 𝔽_{p⁴} ⊂ … ⊂ 𝔽_{p^(2^k)} as finite fields of the interface.

    T k            the carrier: Zp for k = 0, pairs of the previous level
    findNR         a non-residue found by search in the element list (no choice principle)
    tower k        an FF on T k with h = j + 1                     (QuadExt at each step)
    tower_card     the order of level k + 1 is the square of the order of level k
  So for every odd prime p and every k, a field of order p^(2^k) exists, carrying all FF theorems.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.QuadExt

namespace FieldTower

open FTAChain ZMod YMChain FinField

section Find
variable {R : Type} [DecidableEq R] (F : FF R)

def findNR : List R → Option R
  | [] => none
  | a :: l => if a ≠ F.K.zero ∧ Frobenius.pw F.K a F.h ≠ F.K.one then some a else findNR l

theorem findNR_spec : ∀ (l : List R) (y : R), findNR F l = some y → y ≠ F.K.zero ∧ Frobenius.pw F.K y F.h ≠ F.K.one
  | [], _, h => nomatch h
  | a :: l, y, h => by
    change (if a ≠ F.K.zero ∧ Frobenius.pw F.K a F.h ≠ F.K.one then some a else findNR F l) = some y at h
    by_cases e : a ≠ F.K.zero ∧ Frobenius.pw F.K a F.h ≠ F.K.one
    · rw [FiniteRecChain.ite_pos' _ _ e] at h
      have : a = y := Option.some.inj h
      rw [← this]; exact e
    · rw [FiniteRecChain.ite_neg' _ _ e] at h; exact findNR_spec l y h

theorem findNR_some : ∀ (l : List R) (x : R), memL x l → x ≠ F.K.zero → Frobenius.pw F.K x F.h ≠ F.K.one →
    ∃ y, findNR F l = some y
  | [], _, h, _, _ => False.elim h
  | a :: l, x, hm, h0, h1 => by
    show ∃ y, (if a ≠ F.K.zero ∧ Frobenius.pw F.K a F.h ≠ F.K.one then some a else findNR F l) = some y
    by_cases e : a ≠ F.K.zero ∧ Frobenius.pw F.K a F.h ≠ F.K.one
    · rw [FiniteRecChain.ite_pos' _ _ e]; exact ⟨a, rfl⟩
    · rw [FiniteRecChain.ite_neg' _ _ e]
      cases hm with
      | inl ex => exact absurd ⟨ex ▸ h0, ex ▸ h1⟩ e
      | inr hl => exact findNR_some l x hl h0 h1

end Find

def T (m : Nat) : Nat → Type
  | 0 => Zp (2 * m + 1 + 1)
  | k + 1 => T m k × T m k

instance instDec (m : Nat) : ∀ k, DecidableEq (T m k)
  | 0 => inferInstanceAs (DecidableEq (Zp (2 * m + 1 + 1)))
  | k + 1 => @instDecidableEqProd _ _ (instDec m k) (instDec m k)

structure Level (m k : Nat) where
  F : @FF (T m k) (instDec m k)
  j : Nat
  hj : F.h = j + 1

def step {R : Type} [DecidableEq R] (F : FF R) (j : Nat) (hj : F.h = j + 1) :
    (G : FF (R × R)) ×' (G.h = (2 * j * j + 6 * j + 3) + 1) :=
  match hf : findNR F F.els with
  | some y =>
    have hy := findNR_spec F F.els y hf
    ⟨QuadExt.quadExt F y j hj hy.1 hy.2, rfl⟩
  | none => False.elim (by
      match F.nonres with
      | ⟨x, hx0, hx1⟩ =>
        match findNR_some F F.els x (FinList.mem_els F x) hx0 hx1 with
        | ⟨y, hy⟩ => rw [hf] at hy; exact nomatch hy)

def tower (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∀ k, Level m k
  | 0 => ⟨ffp m hpr, m, rfl⟩
  | k + 1 =>
    match tower m hpr k with
    | ⟨F, j, hj⟩ =>
      match @step _ (instDec m k) F j hj with
      | ⟨G, hG⟩ => ⟨G, 2 * j * j + 6 * j + 3, hG⟩

/-- **The order squares at each level**: 2h' + 1 = (2h + 1)². -/
theorem order_sq (j : Nat) : 2 * ((2 * j * j + 6 * j + 3) + 1) + 1 = (2 * (j + 1) + 1) * (2 * (j + 1) + 1) := by lib_nat

end FieldTower

#print axioms FieldTower.findNR_spec
#print axioms FieldTower.findNR_some
#print axioms FieldTower.step
#print axioms FieldTower.tower
#print axioms FieldTower.order_sq

Over every quadratic extension \(F[\sqrt n]\) the element \(i=(\sqrt n)^{h}\) satisfies \(i^2=n^h=-1\), so the Frobenius trace of \(y^2=x^3-x\) over every level \(\mathbb F_{p^{2^k}}\), \(k\ge 1\), is \(2\,\mathrm {Re}\,\pi \) for a primary Gaussian integer \(\pi \) of norm \(p^{2^k}\); an injective key on each level is the base-\(M\) digit pair.

Listing 68: lang/TowerTrace.lean

lang/TowerTrace.lean

/-
  lang/TowerTrace — T43, cycle 141: y² = x³ − x over every field 𝔽_{p^(2^k)}, k ≥ 1, of the tower.

    M, keyT, keyT_lt, keyT_inj   an injective order key on every level (base-M digits)
    iE, iE_sq                    i = (√n)^h in F[√n]: i² = n^h = −1, for any F and non-residue n
    tower_trace                  over level k + 1: −Σ χ(z³ − z) = 2A with A + Bi primary, A² + B² = |𝔽|   (FinPrimary)
  So the Frobenius trace of the CM curve over 𝔽_{p^(2^k)} is 2 Re of a primary Gaussian integer of norm p^(2^k) — Hasse's
  bound and the CM shape at every level of the tower.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.FieldTower
import lang.FinPrimary
import lang.Fp2Count

namespace TowerTrace

open FTAChain ZMod YMChain FiniteRecChain FinField FieldTower Quad

def M (m : Nat) : Nat → Nat
  | 0 => 2 * m + 1 + 1 + 1
  | k + 1 => M m k * M m k

def keyT (m : Nat) : (k : Nat) → T m k → Nat
  | 0, x => x.val
  | k + 1, x => keyT m k x.1 * M m k + keyT m k x.2

theorem keyT_lt (m : Nat) : ∀ k (x : T m k), keyT m k x < M m k
  | 0, x => x.property
  | k + 1, x => by
    show keyT m k x.1 * M m k + keyT m k x.2 < M m k * M m k
    have h1 := keyT_lt m k x.1
    have h2 := keyT_lt m k x.2
    calc keyT m k x.1 * M m k + keyT m k x.2 < keyT m k x.1 * M m k + M m k := Nat.add_lt_add_left h2 _
      _ = (keyT m k x.1 + 1) * M m k := by rw [Nat.succ_mul]
      _ ≤ M m k * M m k := Nat.mul_le_mul_right _ h1

theorem keyT_inj (m : Nat) : ∀ k (x y : T m k), keyT m k x = keyT m k y → x = y
  | 0, _, _, h => Subtype.ext h
  | k + 1, x, y, h =>
    match Fp2Count.divmod_unique (M m k) _ _ _ _ (keyT_lt m k x.2) (keyT_lt m k y.2) h with
    | ⟨h1, h2⟩ => Prod.ext (keyT_inj m k x.1 y.1 h1) (keyT_inj m k x.2 y.2 h2)

section IE
variable {R : Type} [DecidableEq R] (F : FF R) (n : R)

def iE : R × R := Frobenius.pw (quad F.K n) (F.K.zero, F.K.one) F.h

theorem iE_sq (hn0 : n ≠ F.K.zero) (hnr : Frobenius.pw F.K n F.h ≠ F.K.one) :
    (quad F.K n).mul (iE F n) (iE F n) = (quad F.K n).neg (quad F.K n).one := by
  unfold iE
  rw [← Frobenius.pw_mul]
  have e : (quad F.K n).mul (F.K.zero, F.K.one) (F.K.zero, F.K.one) = (n, F.K.zero) := by
    apply Prod.ext
    · show F.K.add (F.K.mul F.K.zero F.K.zero) (F.K.mul n (F.K.mul F.K.one F.K.one)) = n; lib_ring
    · show F.K.add (F.K.mul F.K.zero F.K.one) (F.K.mul F.K.one F.K.zero) = F.K.zero; lib_ring
  have hpe : ∀ a : R, ∀ e, Frobenius.pw (quad F.K n) (a, F.K.zero) e = (Frobenius.pw F.K a e, F.K.zero) := by
    intro a e
    induction e with
    | zero => rfl
    | succ e ih =>
      show (quad F.K n).mul (Frobenius.pw (quad F.K n) (a, F.K.zero) e) (a, F.K.zero) = _
      rw [ih]
      apply Prod.ext
      · show F.K.add (F.K.mul (Frobenius.pw F.K a e) a) (F.K.mul n (F.K.mul F.K.zero F.K.zero)) = F.K.mul (Frobenius.pw F.K a e) a; lib_ring
      · show F.K.add (F.K.mul (Frobenius.pw F.K a e) F.K.zero) (F.K.mul F.K.zero a) = F.K.zero; lib_ring
  have hm1 : Frobenius.pw F.K n F.h = F.K.neg F.K.one := by
    cases F.euler_pm n hn0 with
    | inl h => exact absurd h hnr
    | inr h => exact h
  rw [e, hpe, hm1]
  apply Prod.ext
  · rfl
  · show F.K.zero = F.K.neg F.K.zero; exact (DetChain.neg_unique F.K _ _ (F.K.zero_add _))

end IE

/-- **y² = x³ − x over F[√n]** (any FF F with an injective key bounded by M, n a non-residue): a = 2 Re π, π primary of
    norm |F[√n]|. Applied level by level this covers the whole tower. -/
theorem quad_trace {R : Type} [DecidableEq R] (F : FF R) (n : R) (j : Nat) (hj : F.h = j + 1)
    (hn0 : n ≠ F.K.zero) (hnr : Frobenius.pw F.K n F.h ≠ F.K.one)
    (key : R → Nat) (Mb : Nat) (hlt : ∀ x, key x < Mb) (hkey : ∀ x y, key x = key y → x = y) :
    ∃ a b : Int, a * a + b * b = Int.ofNat (2 * (QuadExt.quadExt F n j hj hn0 hnr).h + 1) ∧ GaussInt.Primary ⟨a, b⟩ ∧
      -sumK LibInt.intR (fun x => FinField.chi (QuadExt.quadExt F n j hj hn0 hnr) (FinSign.f (QuadExt.quadExt F n j hj hn0 hnr) x))
        (QuadExt.quadExt F n j hj hn0 hnr).els = 2 * a :=
  FinPrimary.fin_ap_primary (QuadExt.quadExt F n j hj hn0 hnr)
    (fun x => key x.1 * Mb + key x.2)
    (fun x y h => match Fp2Count.divmod_unique Mb _ _ _ _ (hlt x.2) (hlt y.2) h with
      | ⟨h1, h2⟩ => Prod.ext (hkey _ _ h1) (hkey _ _ h2))
    (iE F n) (iE_sq F n hn0 hnr)

end TowerTrace

#print axioms TowerTrace.M
#print axioms TowerTrace.keyT
#print axioms TowerTrace.keyT_lt
#print axioms TowerTrace.keyT_inj
#print axioms TowerTrace.iE
#print axioms TowerTrace.iE_sq
#print axioms TowerTrace.quad_trace

For \(p\equiv 1\ (3)\) and \(c\) a non-cube, \(\mathbb F_p[t]/(t^3-c)\) is a field of order \(p^3\): with \(\sigma (x)=x^p=a+\omega bt+\omega ^2dt^2\) (\(\omega =c^{(p-1)/3}\ne 1\), \(\omega ^3=1\)), \(\sigma \)-fixed elements are scalars, \(N(x)=x\,\sigma (x)\,\sigma ^2(x)=x^{1+p+p^2}\) is \(\sigma \)-fixed, and \(N(x)=0\) forces \(x=x^{p^3}=N(x)\,x^{p^3-1-p-p^2}=0\); so \(x^{-1}=\sigma (x)\sigma ^2(x)N(x)^{-1}\).

Listing 69: lang/Cubic.lean

lang/Cubic.lean

/-
  lang/Cubic — T43, cycle 143: the ring K[t]/(t³ − c) over any commutative ring K.

  Elements a + b t + d t² as triples (a, (b, d)); t³ = c:
    cmul     (a + bt + dt²)(a' + b't + d't²) = (aa' + c(bd' + db')) + (ab' + ba' + c dd') t + (ad' + bb' + da') t²
    cubic    the commutative ring structure (every law by lib_ring, componentwise)
  For K = 𝔽_p, p ≡ 1 (mod 3), and c a non-cube this is 𝔽_{p³} (next cycles: Frobenius t^p = ωt, the norm, MkFF).

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lib.RingTac
import lang.Quad

namespace Cubic

variable {R : Type} (K : DetChain.CRing R) (c : R)

def cmul (x y : R × R × R) : R × R × R :=
  (K.add (K.mul (x.1) (y.1)) (K.mul c (K.add (K.mul (x.2.1) (y.2.2)) (K.mul (x.2.2) (y.2.1)))),
   K.add (K.add (K.mul (x.1) (y.2.1)) (K.mul (x.2.1) (y.1))) (K.mul c (K.mul (x.2.2) (y.2.2))),
   K.add (K.add (K.mul (x.1) (y.2.2)) (K.mul (x.2.1) (y.2.1))) (K.mul (x.2.2) (y.1)))

theorem cmul_assoc (a b e : R × R × R) : cmul K c (cmul K c a b) e = cmul K c a (cmul K c b e) := by
  apply Prod.ext
  · show K.add (K.mul (K.add (K.mul (a.1) (b.1)) (K.mul c (K.add (K.mul (a.2.1) (b.2.2)) (K.mul (a.2.2) (b.2.1))))) (e.1)) (K.mul c (K.add (K.mul (K.add (K.add (K.mul (a.1) (b.2.1)) (K.mul (a.2.1) (b.1))) (K.mul c (K.mul (a.2.2) (b.2.2)))) (e.2.2)) (K.mul (K.add (K.add (K.mul (a.1) (b.2.2)) (K.mul (a.2.1) (b.2.1))) (K.mul (a.2.2) (b.1))) (e.2.1)))) = K.add (K.mul (a.1) (K.add (K.mul (b.1) (e.1)) (  -- (+294 chars; full line in the Zenodo source)
    lib_ring
  · apply Prod.ext
    · show K.add (K.add (K.mul (K.add (K.mul (a.1) (b.1)) (K.mul c (K.add (K.mul (a.2.1) (b.2.2)) (K.mul (a.2.2) (b.2.1))))) (e.2.1)) (K.mul (K.add (K.add (K.mul (a.1) (b.2.1)) (K.mul (a.2.1) (b.1))) (K.mul c (K.mul (a.2.2) (b.2.2)))) (e.1))) (K.mul c (K.mul (K.add (K.add (K.mul (a.1) (b.2.2)) (K.mul (a.2.1) (b.2.1))) (K.mul (a.2.2) (b.1))) (e.2.2))) = K.add (K.add (K.mul (a.1) (K.add (K.add (K.mu  -- (+296 chars; full line in the Zenodo source)
      lib_ring
    · show K.add (K.add (K.mul (K.add (K.mul (a.1) (b.1)) (K.mul c (K.add (K.mul (a.2.1) (b.2.2)) (K.mul (a.2.2) (b.2.1))))) (e.2.2)) (K.mul (K.add (K.add (K.mul (a.1) (b.2.1)) (K.mul (a.2.1) (b.1))) (K.mul c (K.mul (a.2.2) (b.2.2)))) (e.2.1))) (K.mul (K.add (K.add (K.mul (a.1) (b.2.2)) (K.mul (a.2.1) (b.2.1))) (K.mul (a.2.2) (b.1))) (e.1)) = K.add (K.add (K.mul (a.1) (K.add (K.add (K.mul (b.1) (e  -- (+276 chars; full line in the Zenodo source)
      lib_ring

theorem cmul_comm (a b : R × R × R) : cmul K c a b = cmul K c b a := by
  apply Prod.ext
  · show K.add (K.mul (a.1) (b.1)) (K.mul c (K.add (K.mul (a.2.1) (b.2.2)) (K.mul (a.2.2) (b.2.1)))) = K.add (K.mul (b.1) (a.1)) (K.mul c (K.add (K.mul (b.2.1) (a.2.2)) (K.mul (b.2.2) (a.2.1))))
    lib_ring
  · apply Prod.ext
    · show K.add (K.add (K.mul (a.1) (b.2.1)) (K.mul (a.2.1) (b.1))) (K.mul c (K.mul (a.2.2) (b.2.2))) = K.add (K.add (K.mul (b.1) (a.2.1)) (K.mul (b.2.1) (a.1))) (K.mul c (K.mul (b.2.2) (a.2.2)))
      lib_ring
    · show K.add (K.add (K.mul (a.1) (b.2.2)) (K.mul (a.2.1) (b.2.1))) (K.mul (a.2.2) (b.1)) = K.add (K.add (K.mul (b.1) (a.2.2)) (K.mul (b.2.1) (a.2.1))) (K.mul (b.2.2) (a.1))
      lib_ring

theorem cone_mul (a : R × R × R) : cmul K c (K.one, K.zero, K.zero) a = a := by
  apply Prod.ext
  · show K.add (K.mul (K.one) (a.1)) (K.mul c (K.add (K.mul (K.zero) (a.2.2)) (K.mul (K.zero) (a.2.1)))) = a.1
    lib_ring
  · apply Prod.ext
    · show K.add (K.add (K.mul (K.one) (a.2.1)) (K.mul (K.zero) (a.1))) (K.mul c (K.mul (K.zero) (a.2.2))) = a.2.1
      lib_ring
    · show K.add (K.add (K.mul (K.one) (a.2.2)) (K.mul (K.zero) (a.2.1))) (K.mul (K.zero) (a.1)) = a.2.2
      lib_ring

theorem cdistrib (a b e : R × R × R) :
    cmul K c a (K.add b.1 e.1, K.add b.2.1 e.2.1, K.add b.2.2 e.2.2)
      = (K.add (cmul K c a b).1 (cmul K c a e).1, K.add (cmul K c a b).2.1 (cmul K c a e).2.1, K.add (cmul K c a b).2.2 (cmul K c a e).2.2) := by
  apply Prod.ext
  · show K.add (K.mul (a.1) (K.add b.1 e.1)) (K.mul c (K.add (K.mul (a.2.1) (K.add b.2.2 e.2.2)) (K.mul (a.2.2) (K.add b.2.1 e.2.1)))) = K.add (K.add (K.mul (a.1) (b.1)) (K.mul c (K.add (K.mul (a.2.1) (b.2.2)) (K.mul (a.2.2) (b.2.1))))) (K.add (K.mul (a.1) (e.1)) (K.mul c (K.add (K.mul (a.2.1) (e.2.2)) (K.mul (a.2.2) (e.2.1)))))
    lib_ring
  · apply Prod.ext
    · show K.add (K.add (K.mul (a.1) (K.add b.2.1 e.2.1)) (K.mul (a.2.1) (K.add b.1 e.1))) (K.mul c (K.mul (a.2.2) (K.add b.2.2 e.2.2))) = K.add (K.add (K.add (K.mul (a.1) (b.2.1)) (K.mul (a.2.1) (b.1))) (K.mul c (K.mul (a.2.2) (b.2.2)))) (K.add (K.add (K.mul (a.1) (e.2.1)) (K.mul (a.2.1) (e.1))) (K.mul c (K.mul (a.2.2) (e.2.2))))
      lib_ring
    · show K.add (K.add (K.mul (a.1) (K.add b.2.2 e.2.2)) (K.mul (a.2.1) (K.add b.2.1 e.2.1))) (K.mul (a.2.2) (K.add b.1 e.1)) = K.add (K.add (K.add (K.mul (a.1) (b.2.2)) (K.mul (a.2.1) (b.2.1))) (K.mul (a.2.2) (b.1))) (K.add (K.add (K.mul (a.1) (e.2.2)) (K.mul (a.2.1) (e.2.1))) (K.mul (a.2.2) (e.1)))
      lib_ring

def cubic : DetChain.CRing (R × R × R) where
  add := fun x y => (K.add x.1 y.1, K.add x.2.1 y.2.1, K.add x.2.2 y.2.2)
  mul := cmul K c
  neg := fun x => (K.neg x.1, K.neg x.2.1, K.neg x.2.2)
  zero := (K.zero, K.zero, K.zero)
  one := (K.one, K.zero, K.zero)
  add_assoc := fun _ _ _ => Prod.ext (K.add_assoc _ _ _) (Prod.ext (K.add_assoc _ _ _) (K.add_assoc _ _ _))
  add_comm := fun _ _ => Prod.ext (K.add_comm _ _) (Prod.ext (K.add_comm _ _) (K.add_comm _ _))
  zero_add := fun _ => Prod.ext (K.zero_add _) (Prod.ext (K.zero_add _) (K.zero_add _))
  add_neg := fun _ => Prod.ext (K.add_neg _) (Prod.ext (K.add_neg _) (K.add_neg _))
  mul_assoc := cmul_assoc K c
  mul_comm := cmul_comm K c
  one_mul := cone_mul K c
  distrib := cdistrib K c

end Cubic

#print axioms Cubic.cmul_assoc
#print axioms Cubic.cmul_comm
#print axioms Cubic.cone_mul
#print axioms Cubic.cdistrib
#print axioms Cubic.cubic

Listing 70: lang/CubicFrob.lean

lang/CubicFrob.lean

/-
  lang/CubicFrob — T43, cycle 144: Frobenius on 𝔽_p[t]/(t³ − c), p = J + 2 = 3m + 1: x^p = a + ω b t + ω² d t², ω = c^m.

    nsm_cubic, char_cubic     p · 1 = 0 in the cubic ring
    pw_embed3, scal_mul       scalars embedded as (a, 0, 0)
    t_pow                     t^(3m) = c^m, t^(3m+1) = c^m t
    frob3                     (a + bt + dt²)^p = a + ωbt + ω²dt²       (Frobenius.frob_add twice, Fermat on 𝔽_p)
  The automorphism σ that the norm and the inverse of the next cycle are built from.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Cubic
import lang.QuadFrob

namespace CubicFrob

open FTAChain ZMod Frobenius

variable (J : Nat) (c : Zp (J + 1))

theorem nsm_cubic (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1)) : ∀ k,
    nsm (Cubic.cubic (ring (J + 1)) c) k x = (nsm (ring (J + 1)) k x.1, nsm (ring (J + 1)) k x.2.1, nsm (ring (J + 1)) k x.2.2)
  | 0 => rfl
  | k + 1 => by
    show (Cubic.cubic (ring (J + 1)) c).add (nsm (Cubic.cubic (ring (J + 1)) c) k x) x = _
    rw [nsm_cubic x k]; rfl

theorem char_cubic : nsm (Cubic.cubic (ring (J + 1)) c) (J + 1 + 1) (Cubic.cubic (ring (J + 1)) c).one = (Cubic.cubic (ring (J + 1)) c).zero := by
  show nsm (Cubic.cubic (ring (J + 1)) c) (J + 1 + 1) ((ring (J + 1)).one, (ring (J + 1)).zero, (ring (J + 1)).zero) = ((ring (J + 1)).zero, (ring (J + 1)).zero, (ring (J + 1)).zero)
  rw [nsm_cubic]
  show (nsm (ring (J + 1)) (J + 1 + 1) (ring (J + 1)).one, nsm (ring (J + 1)) (J + 1 + 1) (ring (J + 1)).zero, nsm (ring (J + 1)) (J + 1 + 1) (ring (J + 1)).zero) = _
  rw [char_p, QuadFrob.nsm_zero']

theorem scal_mul (s : Zp (J + 1)) (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1)) :
    (Cubic.cubic (ring (J + 1)) c).mul (s, (ring (J + 1)).zero, (ring (J + 1)).zero) x = ((ring (J + 1)).mul s x.1, (ring (J + 1)).mul s x.2.1, (ring (J + 1)).mul s x.2.2) := by
  apply Prod.ext
  · show (ring (J + 1)).add ((ring (J + 1)).mul s x.1) ((ring (J + 1)).mul c ((ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).zero x.2.2) ((ring (J + 1)).mul (ring (J + 1)).zero x.2.1))) = (ring (J + 1)).mul s x.1; lib_ring
  · apply Prod.ext
    · show (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul s x.2.1) ((ring (J + 1)).mul (ring (J + 1)).zero x.1)) ((ring (J + 1)).mul c ((ring (J + 1)).mul (ring (J + 1)).zero x.2.2)) = (ring (J + 1)).mul s x.2.1; lib_ring
    · show (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul s x.2.2) ((ring (J + 1)).mul (ring (J + 1)).zero x.2.1)) ((ring (J + 1)).mul (ring (J + 1)).zero x.1) = (ring (J + 1)).mul s x.2.2; lib_ring

theorem pw_embed3 (a : Zp (J + 1)) : ∀ k, pw (Cubic.cubic (ring (J + 1)) c) (a, (ring (J + 1)).zero, (ring (J + 1)).zero) k = (pw (ring (J + 1)) a k, (ring (J + 1)).zero, (ring (J + 1)).zero)
  | 0 => rfl
  | k + 1 => by
    show (Cubic.cubic (ring (J + 1)) c).mul (pw (Cubic.cubic (ring (J + 1)) c) (a, (ring (J + 1)).zero, (ring (J + 1)).zero) k) (a, (ring (J + 1)).zero, (ring (J + 1)).zero) = _
    rw [pw_embed3 a k, scal_mul]
    show (((ring (J + 1)).mul (pw (ring (J + 1)) a k) a), (ring (J + 1)).mul (pw (ring (J + 1)) a k) (ring (J + 1)).zero, (ring (J + 1)).mul (pw (ring (J + 1)) a k) (ring (J + 1)).zero) = _
    rw [DetChain.mul_zero]; rfl

def tt : Zp (J + 1) × Zp (J + 1) × Zp (J + 1) := ((ring (J + 1)).zero, (ring (J + 1)).one, (ring (J + 1)).zero)

theorem tmul (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1)) :
    (Cubic.cubic (ring (J + 1)) c).mul (tt J) x = ((ring (J + 1)).mul c x.2.2, x.1, x.2.1) := by
  apply Prod.ext
  · show (ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).zero x.1) ((ring (J + 1)).mul c ((ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).one x.2.2) ((ring (J + 1)).mul (ring (J + 1)).zero x.2.1))) = (ring (J + 1)).mul c x.2.2; lib_ring
  · apply Prod.ext
    · show (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).zero x.2.1) ((ring (J + 1)).mul (ring (J + 1)).one x.1)) ((ring (J + 1)).mul c ((ring (J + 1)).mul (ring (J + 1)).zero x.2.2)) = x.1; lib_ring
    · show (ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (ring (J + 1)).zero x.2.2) ((ring (J + 1)).mul (ring (J + 1)).one x.2.1)) ((ring (J + 1)).mul (ring (J + 1)).zero x.1) = x.2.1; lib_ring

theorem pw_t_succ (k : Nat) : pw (Cubic.cubic (ring (J + 1)) c) (tt J) (k + 1) = (Cubic.cubic (ring (J + 1)) c).mul (tt J) (pw (Cubic.cubic (ring (J + 1)) c) (tt J) k) := (Cubic.cubic (ring (J + 1)) c).mul_comm _ _

theorem t_cube : pw (Cubic.cubic (ring (J + 1)) c) (tt J) 3 = (c, (ring (J + 1)).zero, (ring (J + 1)).zero) := by
  rw [pw_t_succ, pw_t_succ, pw_t_succ, show pw (Cubic.cubic (ring (J + 1)) c) (tt J) 0 = ((ring (J + 1)).one, (ring (J + 1)).zero, (ring (J + 1)).zero) from rfl, tmul, tmul, tmul]
  apply Prod.ext
  · show (ring (J + 1)).mul c (ring (J + 1)).one = c; rw [(ring (J + 1)).mul_comm, (ring (J + 1)).one_mul]
  · apply Prod.ext
    · show (ring (J + 1)).mul c (ring (J + 1)).zero = (ring (J + 1)).zero; exact DetChain.mul_zero (ring (J + 1)) c
    · show (ring (J + 1)).mul c (ring (J + 1)).zero = (ring (J + 1)).zero; exact DetChain.mul_zero (ring (J + 1)) c


theorem t_pow (m : Nat) : Frobenius.pw (Cubic.cubic (ring (J + 1)) c) (tt J) (3 * m) = (Frobenius.pw (ring (J + 1)) c m, (ring (J + 1)).zero, (ring (J + 1)).zero) := by
  rw [← TwoPower.pwpow, t_cube, pw_embed3]

theorem t_pow1 (m : Nat) : Frobenius.pw (Cubic.cubic (ring (J + 1)) c) (tt J) (3 * m + 1) = ((ring (J + 1)).zero, Frobenius.pw (ring (J + 1)) c m, (ring (J + 1)).zero) := by
  rw [pw_t_succ, t_pow, tmul]
  apply Prod.ext
  · show (ring (J + 1)).mul c (ring (J + 1)).zero = (ring (J + 1)).zero; exact DetChain.mul_zero (ring (J + 1)) c
  · rfl

/-- x = a + b t + d t². -/
theorem decomp (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1)) :
    x = (Cubic.cubic (ring (J + 1)) c).add (x.1, (ring (J + 1)).zero, (ring (J + 1)).zero) ((Cubic.cubic (ring (J + 1)) c).add ((Cubic.cubic (ring (J + 1)) c).mul (x.2.1, (ring (J + 1)).zero, (ring (J + 1)).zero) (tt J))
      ((Cubic.cubic (ring (J + 1)) c).mul (x.2.2, (ring (J + 1)).zero, (ring (J + 1)).zero) ((Cubic.cubic (ring (J + 1)) c).mul (tt J) (tt J)))) := by
  apply Prod.ext
  · show x.1 = (ring (J + 1)).add (x.1) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (x.2.1) ((ring (J + 1)).zero)) ((ring (J + 1)).mul c ((ring (J + 1)).add ((ring (J + 1)).mul ((ring (J + 1)).zero) ((ring (J + 1)).zero)) ((ring (J + 1)).mul ((ring (J + 1)).zero) ((ring (J + 1)).one))))) ((ring (J + 1)).add ((ring (J + 1)).mul (x.2.2) ((ring (J + 1)).add ((ring (J + 1)).mul ((ring   -- (+846 chars; full line in the Zenodo source)
  · apply Prod.ext
    · show x.2.1 = (ring (J + 1)).add ((ring (J + 1)).zero) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (x.2.1) ((ring (J + 1)).one)) ((ring (J + 1)).mul ((ring (J + 1)).zero) ((ring (J + 1)).zero))) ((ring (J + 1)).mul c ((ring (J + 1)).mul ((ring (J + 1)).zero) ((ring (J + 1)).zero)))) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (x.2.2) ((ring   -- (+866 chars; full line in the Zenodo source)
    · show x.2.2 = (ring (J + 1)).add ((ring (J + 1)).zero) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (x.2.1) ((ring (J + 1)).zero)) ((ring (J + 1)).mul ((ring (J + 1)).zero) ((ring (J + 1)).one))) ((ring (J + 1)).mul ((ring (J + 1)).zero) ((ring (J + 1)).zero))) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (x.2.2) ((ring (J + 1)).add ((ring (J   -- (+820 chars; full line in the Zenodo source)

/-- **Frobenius: (a + bt + dt²)^p = a + ωbt + ω²dt², ω = c^m, p = 3m + 1.** -/
theorem frob3 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (m : Nat) (hJ : J + 1 = 3 * m)
    (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1)) :
    Frobenius.pw (Cubic.cubic (ring (J + 1)) c) x (J + 1 + 1) = (x.1, (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) x.2.1, (ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m)) x.2.2) := by
  have hc := char_cubic J c
  conv => lhs; rw [decomp J c x]
  rw [Frobenius.frob_add (Cubic.cubic (ring (J + 1)) c) J hpr hc, Frobenius.frob_add (Cubic.cubic (ring (J + 1)) c) J hpr hc, Frobenius.pw_mul, Frobenius.pw_mul, Frobenius.pw_mul,
    pw_embed3, pw_embed3, pw_embed3, ZMod.fermat J hpr, ZMod.fermat J hpr, ZMod.fermat J hpr,
    show J + 1 + 1 = 3 * m + 1 by rw [hJ], t_pow1]
  apply Prod.ext
  · show (ring (J + 1)).add (x.1) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (x.2.1) ((ring (J + 1)).zero)) ((ring (J + 1)).mul c ((ring (J + 1)).add ((ring (J + 1)).mul ((ring (J + 1)).zero) ((ring (J + 1)).zero)) ((ring (J + 1)).mul ((ring (J + 1)).zero) ((Frobenius.pw (ring (J + 1)) c m)))))) ((ring (J + 1)).add ((ring (J + 1)).mul (x.2.2) ((ring (J + 1)).add ((ring (J + 1)).mu  -- (+951 chars; full line in the Zenodo source)
  · apply Prod.ext
    · show (ring (J + 1)).add ((ring (J + 1)).zero) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (x.2.1) ((Frobenius.pw (ring (J + 1)) c m))) ((ring (J + 1)).mul ((ring (J + 1)).zero) ((ring (J + 1)).zero))) ((ring (J + 1)).mul c ((ring (J + 1)).mul ((ring (J + 1)).zero) ((ring (J + 1)).zero)))) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (x.2.2)   -- (+1024 chars; full line in the Zenodo source)
    · show (ring (J + 1)).add ((ring (J + 1)).zero) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (x.2.1) ((ring (J + 1)).zero)) ((ring (J + 1)).mul ((ring (J + 1)).zero) ((Frobenius.pw (ring (J + 1)) c m)))) ((ring (J + 1)).mul ((ring (J + 1)).zero) ((ring (J + 1)).zero))) ((ring (J + 1)).add ((ring (J + 1)).add ((ring (J + 1)).mul (x.2.2) ((ring (J + 1)).add ((r  -- (+1033 chars; full line in the Zenodo source)

end CubicFrob

#print axioms CubicFrob.nsm_cubic
#print axioms CubicFrob.char_cubic
#print axioms CubicFrob.scal_mul
#print axioms CubicFrob.pw_embed3
#print axioms CubicFrob.tt
#print axioms CubicFrob.tmul
#print axioms CubicFrob.pw_t_succ
#print axioms CubicFrob.t_cube
#print axioms CubicFrob.t_pow
#print axioms CubicFrob.t_pow1
#print axioms CubicFrob.decomp
#print axioms CubicFrob.frob3

Listing 71: lang/CubicField.lean

lang/CubicField.lean

/-
  lang/CubicField — T43, cycle 145: 𝔽_p[t]/(t³ − c) has no zero divisors when c is a non-cube (p = 3m + 1).

  σ(x) = x^p = (a, ωb, ω²d), ω = c^m ≠ 1, ω³ = 1:
    omega3, omega_ne, omega2_ne      ω³ = 1, ω ≠ 1, ω² ≠ 1
    fixed_scalar                     σ(x) = x ⇒ x = (a, 0, 0)
    norm_fixed                       N(x) = x^(1+p+p²) satisfies σ(N) = N              (σ³ = id)
    norm_zero                        N(x) = 0 ⇒ x = 0                                   (x = x^(p³) = N(x)·x^(…))
    inverse3, nozd3                  x ≠ 0 ⇒ x · (x^(p+p²)·N⁻¹) = 1; no zero divisors

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CubicFrob
import lang.ConicSum

namespace CubicField

open FTAChain ZMod Frobenius CubicFrob

variable (J : Nat) (c : Zp (J + 1)) (m : Nat)

theorem omega3 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 3 * m) (hc0 : c ≠ (ring (J + 1)).zero) :
    (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m)) = (ring (J + 1)).one := by
  rw [show (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m)) = pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) c m) 3 by show _ = (ring (J + 1)).mul ((ring (J + 1)).mul ((ring (J + 1)).mul (ring (J + 1)).one _) _) _; lib_ring,
    TwoPower.pwpow, show m * 3 = J + 1 by rw [hJ]; lib_nat]
  exact unit_pow J hpr c hc0

theorem omega2_ne (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 3 * m) (hc0 : c ≠ (ring (J + 1)).zero)
    (hω : (Frobenius.pw (ring (J + 1)) c m) ≠ (ring (J + 1)).one) : (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m) ≠ (ring (J + 1)).one := fun e => hω (by
  have h3 := omega3 J c m hpr hJ hc0
  rw [e, (ring (J + 1)).mul_comm, (ring (J + 1)).one_mul] at h3; exact h3)

theorem cancel (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (u y : Zp (J + 1)) (hu : u ≠ (ring (J + 1)).one)
    (h : (ring (J + 1)).mul u y = y) : y = (ring (J + 1)).zero := by
  have e : (ring (J + 1)).mul ((ring (J + 1)).add u ((ring (J + 1)).neg (ring (J + 1)).one)) y = (ring (J + 1)).zero := by
    rw [DetChain.right_distrib, h, DetChain.neg_mul_left, (ring (J + 1)).one_mul, (ring (J + 1)).add_neg]
  cases nozd J hpr _ _ e with
  | inl h1 =>
    exfalso; apply hu
    have := congrArg (fun z => (ring (J + 1)).add z (ring (J + 1)).one) h1
    show u = (ring (J + 1)).one
    have h2 : (ring (J + 1)).add ((ring (J + 1)).add u ((ring (J + 1)).neg (ring (J + 1)).one)) (ring (J + 1)).one = (ring (J + 1)).add (ring (J + 1)).zero (ring (J + 1)).one := this
    rw [(ring (J + 1)).add_assoc, DetChain.add_neg' (ring (J + 1)) (ring (J + 1)).one, DetChain.add_zero', (ring (J + 1)).zero_add] at h2
    exact h2
  | inr h2 => exact h2

/-- **σ-fixed elements are scalars.** -/
theorem fixed_scalar (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 3 * m) (hc0 : c ≠ (ring (J + 1)).zero)
    (hω : (Frobenius.pw (ring (J + 1)) c m) ≠ (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1))
    (h : pw (Cubic.cubic (ring (J + 1)) c) x (J + 1 + 1) = x) : x = (x.1, (ring (J + 1)).zero, (ring (J + 1)).zero) := by
  rw [frob3 J c hpr m hJ] at h
  have h2 : (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) x.2.1 = x.2.1 := congrArg (fun z => z.2.1) h
  have h3 : (ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m)) x.2.2 = x.2.2 := congrArg (fun z => z.2.2) h
  apply Prod.ext
  · rfl
  · apply Prod.ext
    · exact cancel J hpr _ _ hω h2
    · exact cancel J hpr _ _ (omega2_ne J c m hpr hJ hc0 hω) h3

def Nm (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1)) : Zp (J + 1) × Zp (J + 1) × Zp (J + 1) :=
  (Cubic.cubic (ring (J + 1)) c).mul x ((Cubic.cubic (ring (J + 1)) c).mul (pw (Cubic.cubic (ring (J + 1)) c) x (J + 1 + 1)) (pw (Cubic.cubic (ring (J + 1)) c) x ((J + 1 + 1) * (J + 1 + 1))))

theorem sigma3 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 3 * m) (hc0 : c ≠ (ring (J + 1)).zero) (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1)) :
    pw (Cubic.cubic (ring (J + 1)) c) x ((J + 1 + 1) * (J + 1 + 1) * (J + 1 + 1)) = x := by
  rw [← TwoPower.pwpow, ← TwoPower.pwpow, frob3 J c hpr m hJ, frob3 J c hpr m hJ, frob3 J c hpr m hJ]
  have h3 := omega3 J c m hpr hJ hc0
  apply Prod.ext
  · rfl
  · apply Prod.ext
    · show (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) x.2.1)) = x.2.1
      rw [show (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) x.2.1)) = (ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m))) x.2.1 by lib_ring, h3, (ring (J + 1)).one_mul]
    · show (ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m)) ((ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m)) ((ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m)) x.2.2)) = x.2.2
      rw [show (ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m)) ((ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m)) ((ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m)) x.2.2))
          = (ring (J + 1)).mul ((ring (J + 1)).mul ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m))) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) ((ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) c m) (Frobenius.pw (ring (J + 1)) c m)))) x.2.2 by lib_ring, h3]
      rw [(ring (J + 1)).one_mul, (ring (J + 1)).one_mul]

theorem norm_fixed (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 3 * m) (hc0 : c ≠ (ring (J + 1)).zero) (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1)) : pw (Cubic.cubic (ring (J + 1)) c) (Nm J c x) (J + 1 + 1) = Nm J c x := by
  unfold Nm
  rw [pw_mul, pw_mul, TwoPower.pwpow, TwoPower.pwpow, show (J + 1 + 1) * (J + 1 + 1) * (J + 1 + 1) = (J + 1 + 1) * (J + 1 + 1) * (J + 1 + 1) from rfl, sigma3 J c m hpr hJ hc0]
  lib_ring

theorem norm_pw (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1)) : pw (Cubic.cubic (ring (J + 1)) c) x (1 + (J + 1 + 1) + (J + 1 + 1) * (J + 1 + 1)) = Nm J c x := by
  rw [HasseCM.pw_add', HasseCM.pw_add']
  unfold Nm
  rw [show pw (Cubic.cubic (ring (J + 1)) c) x 1 = x from (Cubic.cubic (ring (J + 1)) c).one_mul x]
  lib_ring

theorem norm_zero (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 3 * m) (hc0 : c ≠ (ring (J + 1)).zero) (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1)) (h : Nm J c x = (Cubic.cubic (ring (J + 1)) c).zero) : x = (Cubic.cubic (ring (J + 1)) c).zero := by
  rw [← sigma3 J c m hpr hJ hc0 x, show (J + 1 + 1) * (J + 1 + 1) * (J + 1 + 1) = (1 + (J + 1 + 1) + (J + 1 + 1) * (J + 1 + 1)) + (J * J * J + 5 * J * J + 7 * J + 1) by lib_nat,
    HasseCM.pw_add', norm_pw, h]
  exact DetChain.zero_mul' (Cubic.cubic (ring (J + 1)) c) _

theorem norm_scalar (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 3 * m) (hc0 : c ≠ (ring (J + 1)).zero) (hω : (Frobenius.pw (ring (J + 1)) c m) ≠ (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1)) :
    Nm J c x = ((Nm J c x).1, (ring (J + 1)).zero, (ring (J + 1)).zero) :=
  fixed_scalar J c m hpr hJ hc0 hω _ (norm_fixed J c m hpr hJ hc0 x)

/-- **Inverses in 𝔽_p[∛c].** -/
theorem inverse3 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 3 * m) (hc0 : c ≠ (ring (J + 1)).zero) (hω : (Frobenius.pw (ring (J + 1)) c m) ≠ (ring (J + 1)).one) (x : Zp (J + 1) × Zp (J + 1) × Zp (J + 1)) (hx : x ≠ (Cubic.cubic (ring (J + 1)) c).zero) :
    (Cubic.cubic (ring (J + 1)) c).mul x ((Cubic.cubic (ring (J + 1)) c).mul ((Cubic.cubic (ring (J + 1)) c).mul (pw (Cubic.cubic (ring (J + 1)) c) x (J + 1 + 1)) (pw (Cubic.cubic (ring (J + 1)) c) x ((J + 1 + 1) * (J + 1 + 1)))) (pw (ring (J + 1)) (Nm J c x).1 J, (ring (J + 1)).zero, (ring (J + 1)).zero)) = (Cubic.cubic (ring (J + 1)) c).one := by
  have hs := norm_scalar J c m hpr hJ hc0 hω x
  have hn : (Nm J c x).1 ≠ (ring (J + 1)).zero := fun e => hx (norm_zero J c m hpr hJ hc0 x (by rw [hs, e]; rfl))
  have hinv := inv_exists J hpr _ hn
  rw [← (Cubic.cubic (ring (J + 1)) c).mul_assoc, show (Cubic.cubic (ring (J + 1)) c).mul x ((Cubic.cubic (ring (J + 1)) c).mul (pw (Cubic.cubic (ring (J + 1)) c) x (J + 1 + 1)) (pw (Cubic.cubic (ring (J + 1)) c) x ((J + 1 + 1) * (J + 1 + 1)))) = Nm J c x from rfl, hs, scal_mul]
  apply Prod.ext
  · show (ring (J + 1)).mul (Nm J c x).1 (pw (ring (J + 1)) (Nm J c x).1 J) = (ring (J + 1)).one; rw [(ring (J + 1)).mul_comm]; exact hinv
  · apply Prod.ext
    · show (ring (J + 1)).mul (Nm J c x).1 (ring (J + 1)).zero = (ring (J + 1)).zero; exact DetChain.mul_zero (ring (J + 1)) _
    · show (ring (J + 1)).mul (Nm J c x).1 (ring (J + 1)).zero = (ring (J + 1)).zero; exact DetChain.mul_zero (ring (J + 1)) _

theorem nozd3 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hJ : J + 1 = 3 * m) (hc0 : c ≠ (ring (J + 1)).zero) (hω : (Frobenius.pw (ring (J + 1)) c m) ≠ (ring (J + 1)).one) : DetChain.NoZD (Cubic.cubic (ring (J + 1)) c) := fun x y e => by
  by_cases hx : x = (Cubic.cubic (ring (J + 1)) c).zero
  · exact Or.inl hx
  · right
    have hi := inverse3 J c m hpr hJ hc0 hω x hx
    generalize (Cubic.cubic (ring (J + 1)) c).mul ((Cubic.cubic (ring (J + 1)) c).mul (pw (Cubic.cubic (ring (J + 1)) c) x (J + 1 + 1)) (pw (Cubic.cubic (ring (J + 1)) c) x ((J + 1 + 1) * (J + 1 + 1)))) (pw (ring (J + 1)) (Nm J c x).1 J, (ring (J + 1)).zero, (ring (J + 1)).zero) = v at hi
    calc y = (Cubic.cubic (ring (J + 1)) c).mul (Cubic.cubic (ring (J + 1)) c).one y := ((Cubic.cubic (ring (J + 1)) c).one_mul y).symm
      _ = (Cubic.cubic (ring (J + 1)) c).mul ((Cubic.cubic (ring (J + 1)) c).mul x v) y := by rw [hi]
      _ = (Cubic.cubic (ring (J + 1)) c).mul v ((Cubic.cubic (ring (J + 1)) c).mul x y) := by lib_ring
      _ = (Cubic.cubic (ring (J + 1)) c).zero := by rw [e]; exact DetChain.mul_zero (Cubic.cubic (ring (J + 1)) c) _

end CubicField

#print axioms CubicField.omega3
#print axioms CubicField.omega2_ne
#print axioms CubicField.cancel
#print axioms CubicField.fixed_scalar
#print axioms CubicField.Nm
#print axioms CubicField.sigma3
#print axioms CubicField.norm_fixed
#print axioms CubicField.norm_pw
#print axioms CubicField.norm_zero
#print axioms CubicField.norm_scalar
#print axioms CubicField.inverse3
#print axioms CubicField.nozd3

Listing 72: lang/CubicInst.lean

lang/CubicInst.lean

/-
  lang/CubicInst — T43, cycle 146: 𝔽_{p³} = 𝔽_p[∛c] (p = 2r + 3 = 3m + 1, c a non-cube) as a finite field of the interface.

    prodL, comp_prodL, card_prodL      the p³ triples, listed once each
    ivC, ivC_mul                       inverses from CubicField.inverse3
    fermatC                            x^(p³−1) = 1                          (FiniteField.unit_pow)
    ffc                                the FF structure                       (MkFF)
  So every FF theorem (character sums, cubic Jacobsthal sums, primitive roots, Hasse invariants) holds over 𝔽_{p³}.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CubicField
import lang.QuadExt
import lang.CMInert
import lang.GaussSplit
import lang.Fp2

namespace CubicInst

open FTAChain ZMod YMChain FiniteRecChain Frobenius

abbrev Z := LibInt.intR

def prodL {A B : Type} (l2 : List B) : List A → List (A × B)
  | [] => []
  | a :: l => l2.map (fun b => (a, b)) ++ prodL l2 l

theorem sum_prodL {A B : Type} (l2 : List B) (G : A × B → Int) : ∀ l : List A,
    sumK Z G (prodL l2 l) = sumK Z (fun a => sumK Z (fun b => G (a, b)) l2) l
  | [] => rfl
  | a :: l => by
    show sumK Z G (l2.map (fun b => (a, b)) ++ prodL l2 l) = _
    rw [CMInert.sumK_app', sum_prodL l2 G l, QuadExt.sumK_map]
    rfl

theorem delta_prod {A B : Type} [DecidableEq A] [DecidableEq B] (y z : A × B) :
    delta Z y z = delta Z y.1 z.1 * delta Z y.2 z.2 := by
  unfold delta
  by_cases h1 : y.1 = z.1
  · by_cases h2 : y.2 = z.2
    · rw [ite_pos' _ _ (Prod.ext h1 h2), ite_pos' _ _ h1, ite_pos' _ _ h2]; rfl
    · rw [ite_neg' _ _ (fun e => h2 (congrArg Prod.snd e)), ite_pos' _ _ h1, ite_neg' _ _ h2]; rfl
  · rw [ite_neg' _ _ (fun e => h1 (congrArg Prod.fst e)), ite_neg' _ _ h1]; exact (DetChain.zero_mul' Z _).symm

theorem comp_prodL {A B : Type} [DecidableEq A] [DecidableEq B] (l1 : List A) (l2 : List B)
    (h1 : Complete Z l1) (h2 : Complete Z l2) : Complete Z (prodL l2 l1) := fun z => by
  rw [sum_prodL]
  have pt : ∀ a, sumK Z (fun b => delta Z (a, b) z) l2 = delta Z a z.1 := fun a => by
    rw [sumK_ext Z _ (fun b => Z.mul (delta Z a z.1) (delta Z b z.2)) (fun b => delta_prod (a, b) z), ← mul_sumK, h2 z.2]
    show delta Z a z.1 * 1 = _; lib_int
  rw [sumK_ext Z _ _ pt]
  exact h1 z.1

theorem card_prodL {A B : Type} (l1 : List A) (l2 : List B) (q1 q2 : Int)
    (h1 : sumK Z (fun _ => (1 : Int)) l1 = q1) (h2 : sumK Z (fun _ => (1 : Int)) l2 = q2) :
    sumK Z (fun _ => (1 : Int)) (prodL l2 l1) = q1 * q2 := by
  rw [sum_prodL, sumK_ext Z _ (fun _ => q2) (fun _ => h2)]
  rw [show sumK Z (fun _ => q2) l1 = Z.mul q2 (sumK Z (fun _ => (1 : Int)) l1) by
    rw [mul_sumK]; exact sumK_ext Z _ _ (fun _ => by show _ = q2 * 1; lib_int) _, h1]
  show q2 * q1 = q1 * q2; lib_int

section Inst
variable (r : Nat) (c : Zp (2 * r + 1 + 1)) (m : Nat)

theorem cube_form : (2 * r + 1 + 1 + 1) * ((2 * r + 1 + 1 + 1) * (2 * r + 1 + 1 + 1)) = 2 * ((4 * r * r * r + 18 * r * r + 27 * r + 12) + 1) + 1 := by lib_nat

theorem compT : Complete Z (prodL (prodL (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1)) (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1))) (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1))) :=
  comp_prodL _ _ (CMInert.complete_allZ (2 * r + 1 + 1) Z) (comp_prodL _ _ (CMInert.complete_allZ (2 * r + 1 + 1) Z) (CMInert.complete_allZ (2 * r + 1 + 1) Z))

theorem cardT : sumK Z (fun _ => (1 : Int)) (prodL (prodL (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1)) (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1))) (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1))) = Int.ofNat (2 * ((4 * r * r * r + 18 * r * r + 27 * r + 12) + 1) + 1) := by
  rw [card_prodL _ _ _ _ (Fp2.sum_one_n (2 * r + 1) (2 * r + 1 + 1 + 1)) (card_prodL _ _ _ _ (Fp2.sum_one_n (2 * r + 1) (2 * r + 1 + 1 + 1)) (Fp2.sum_one_n (2 * r + 1) (2 * r + 1 + 1 + 1))),
    ← cube_form]
  rfl

def ivC (x : Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1)) : Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) :=
  (Cubic.cubic (ring (2 * r + 1 + 1)) c).mul ((Cubic.cubic (ring (2 * r + 1 + 1)) c).mul (pw (Cubic.cubic (ring (2 * r + 1 + 1)) c) x (2 * r + 1 + 1 + 1)) (pw (Cubic.cubic (ring (2 * r + 1 + 1)) c) x ((2 * r + 1 + 1 + 1) * (2 * r + 1 + 1 + 1))))
    (pw (ring (2 * r + 1 + 1)) (CubicField.Nm (2 * r + 1) c x).1 (2 * r + 1), (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero)

theorem fermatC (hpr : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = (2 * r + 1 + 1 + 1)) (hJ : 2 * r + 1 + 1 = 3 * m) (hc0 : c ≠ (ring (2 * r + 1 + 1)).zero) (hω : (Frobenius.pw (ring (2 * r + 1 + 1)) c m) ≠ (ring (2 * r + 1 + 1)).one) (x : Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1)) (hx : x ≠ (Cubic.cubic (ring (2 * r + 1 + 1)) c).zero) :
    pw (Cubic.cubic (ring (2 * r + 1 + 1)) c) x (2 * ((4 * r * r * r + 18 * r * r + 27 * r + 12) + 1)) = (Cubic.cubic (ring (2 * r + 1 + 1)) c).one := by
  have hc : ∀ z, FinList.cnt z (prodL (prodL (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1)) (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1))) (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1))) = 1 := fun z => Int.ofNat.inj ((FinList.cnt_int z _).symm.trans (compT r z))
  have hl := FinList.len_nzL (Cubic.cubic (ring (2 * r + 1 + 1)) c).zero (prodL (prodL (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1)) (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1))) (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1)))
  have hlen : ((prodL (prodL (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1)) (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1))) (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1)))).length = 2 * ((4 * r * r * r + 18 * r * r + 27 * r + 12) + 1) + 1 :=
    Int.ofNat.inj ((FinList.len_int _).symm.trans (cardT r))
  rw [hc, hlen] at hl
  rw [← Nat.succ.inj hl]
  apply FiniteField.unit_pow (Cubic.cubic (ring (2 * r + 1 + 1)) c) (CubicField.nozd3 (2 * r + 1) c m hpr hJ hc0 hω) (ivC r c)
    (fun y hy => by unfold ivC; rw [(Cubic.cubic (ring (2 * r + 1 + 1)) c).mul_comm]; exact CubicField.inverse3 (2 * r + 1) c m hpr hJ hc0 hω y hy)
  · intro z
    rw [QuadExt.cnt_eq, QuadExt.cnt_nzL]
    by_cases e : z = (Cubic.cubic (ring (2 * r + 1 + 1)) c).zero
    · rw [ite_pos' _ _ e, ite_pos' _ _ e]
    · rw [ite_neg' _ _ e, ite_neg' _ _ e, hc]
  · intro z hz e
    rw [QuadExt.cnt_eq, QuadExt.cnt_nzL, ite_pos' _ _ e] at hz
    exact Nat.lt_irrefl 0 hz
  · exact fun e => PowerSum.one_ne_zero' (2 * r + 1) (congrArg Prod.fst e)
  · exact hx

/-- **𝔽_{p³} as a finite field of the interface.** -/
def ffc (hpr : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = (2 * r + 1 + 1 + 1)) (hJ : 2 * r + 1 + 1 = 3 * m) (hc0 : c ≠ (ring (2 * r + 1 + 1)).zero) (hω : (Frobenius.pw (ring (2 * r + 1 + 1)) c m) ≠ (ring (2 * r + 1 + 1)).one) : FinField.FF (Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1)) :=
  MkFF.mkFF (Cubic.cubic (ring (2 * r + 1 + 1)) c) (CubicField.nozd3 (2 * r + 1) c m hpr hJ hc0 hω) (prodL (prodL (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1)) (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1))) (CMInert.allZ (2 * r + 1 + 1) (2 * r + 1 + 1 + 1))) (compT r) (4 * r * r * r + 18 * r * r + 27 * r + 12)
    (cardT r) (fun e => GaussSplit.one_ne_negone (2 * r) (congrArg Prod.fst e).symm) (fermatC r c m hpr hJ hc0 hω)

end Inst

end CubicInst

#print axioms CubicInst.prodL
#print axioms CubicInst.sum_prodL
#print axioms CubicInst.delta_prod
#print axioms CubicInst.comp_prodL
#print axioms CubicInst.card_prodL
#print axioms CubicInst.cube_form
#print axioms CubicInst.compT
#print axioms CubicInst.cardT
#print axioms CubicInst.ivC
#print axioms CubicInst.fermatC
#print axioms CubicInst.ffc

Over \(\mathbb F_{p^3}\) (\(p\equiv 1\ (12)\), \(i\in \mathbb F_p\) embedded) the trace of \(y^2=x^3-x\) is again \(2\,\mathrm {Re}\,\pi \) with \(\pi \) primary of norm \(p^3\), so \(|a(\mathbb F_{p^3})|\le 2p^{3/2}\).

Listing 73: lang/CubeTrace.lean

lang/CubeTrace.lean

/-
  lang/CubeTrace — T43, cycle 147: y² = x³ − x over 𝔽_{p³} (p ≡ 1 mod 12): a = 2 Re π with π primary of norm p³.

  i ∈ 𝔽_p (p ≡ 1 mod 4) embeds as (i, 0, 0) with (i, 0, 0)² = −1 in 𝔽_{p³}; an injective key on triples; FinPrimary.
    key3, key3_inj, i3_sq, cube_trace
  Hasse's bound and the CM shape over the odd-degree field 𝔽_{p³}.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CubicInst
import lang.FinPrimary
import lang.Fp2Count

namespace CubeTrace

open FTAChain ZMod YMChain FiniteRecChain

def key3 (r : Nat) (x : Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1)) : Nat :=
  x.1.val * ((2 * r + 1 + 1 + 1) * (2 * r + 1 + 1 + 1)) + (x.2.1.val * (2 * r + 1 + 1 + 1) + x.2.2.val)

theorem key3_inj (r : Nat) (x y : Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1)) (h : key3 r x = key3 r y) : x = y := by
  have lt2 : ∀ z : Zp (2 * r + 1 + 1) × Zp (2 * r + 1 + 1), z.1.val * (2 * r + 1 + 1 + 1) + z.2.val < (2 * r + 1 + 1 + 1) * (2 * r + 1 + 1 + 1) := fun z =>
    calc z.1.val * (2 * r + 1 + 1 + 1) + z.2.val < z.1.val * (2 * r + 1 + 1 + 1) + (2 * r + 1 + 1 + 1) := Nat.add_lt_add_left z.2.property _
      _ = (z.1.val + 1) * (2 * r + 1 + 1 + 1) := (Nat.succ_mul z.1.val (2 * r + 1 + 1 + 1)).symm
      _ ≤ (2 * r + 1 + 1 + 1) * (2 * r + 1 + 1 + 1) := Nat.mul_le_mul_right _ z.1.property
  match Fp2Count.divmod_unique _ _ _ _ _ (lt2 x.2) (lt2 y.2) h with
  | ⟨h1, h2⟩ =>
    match Fp2Count.divmod_unique _ _ _ _ _ x.2.2.property y.2.2.property h2 with
    | ⟨h3, h4⟩ => exact Prod.ext (Subtype.ext h1) (Prod.ext (Subtype.ext h3) (Subtype.ext h4))

theorem i3_sq (r : Nat) (c : Zp (2 * r + 1 + 1)) (i : Zp (2 * r + 1 + 1)) (hi : (ring (2 * r + 1 + 1)).mul i i = (ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).one) :
    (Cubic.cubic (ring (2 * r + 1 + 1)) c).mul (i, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero) (i, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero) = (Cubic.cubic (ring (2 * r + 1 + 1)) c).neg (Cubic.cubic (ring (2 * r + 1 + 1)) c).one := by
  rw [CubicFrob.scal_mul, hi]
  apply Prod.ext
  · rfl
  · apply Prod.ext
    · show (ring (2 * r + 1 + 1)).mul i (ring (2 * r + 1 + 1)).zero = (ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).zero; rw [DetChain.mul_zero]; exact (DetChain.neg_unique (ring (2 * r + 1 + 1)) _ _ ((ring (2 * r + 1 + 1)).zero_add _))
    · show (ring (2 * r + 1 + 1)).mul i (ring (2 * r + 1 + 1)).zero = (ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).zero; rw [DetChain.mul_zero]; exact (DetChain.neg_unique (ring (2 * r + 1 + 1)) _ _ ((ring (2 * r + 1 + 1)).zero_add _))

/-- **y² = x³ − x over 𝔽_{p³}: a = 2 Re π, π primary of norm p³** (given i ∈ 𝔽_p). -/
theorem cube_trace (r : Nat) (c : Zp (2 * r + 1 + 1)) (m : Nat) (hpr : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = (2 * r + 1 + 1 + 1)) (hJ : 2 * r + 1 + 1 = 3 * m) (hc0 : c ≠ (ring (2 * r + 1 + 1)).zero) (hω : (Frobenius.pw (ring (2 * r + 1 + 1)) c m) ≠ (ring (2 * r + 1 + 1)).one) (i : Zp (2 * r + 1 + 1)) (hi : (ring (2 * r + 1 + 1)).mul i i = (ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).one  -- (+3 chars; full line in the Zenodo source)
    ∃ a b : Int, a * a + b * b = Int.ofNat ((2 * r + 1 + 1 + 1) * ((2 * r + 1 + 1 + 1) * (2 * r + 1 + 1 + 1))) ∧ GaussInt.Primary ⟨a, b⟩ ∧
      -sumK LibInt.intR (fun x => FinField.chi (CubicInst.ffc r c m hpr hJ hc0 hω) (FinSign.f (CubicInst.ffc r c m hpr hJ hc0 hω) x)) (CubicInst.ffc r c m hpr hJ hc0 hω).els = 2 * a := by
  match FinPrimary.fin_ap_primary (CubicInst.ffc r c m hpr hJ hc0 hω) (key3 r) (key3_inj r) (i, (ring (2 * r + 1 + 1)).zero, (ring (2 * r + 1 + 1)).zero) (i3_sq r c i hi) with
  | ⟨a, b, hab, hP, hS⟩ =>
    refine ⟨a, b, ?_, hP, hS⟩
    rw [hab, CubicInst.cube_form]; rfl

end CubeTrace

#print axioms CubeTrace.key3
#print axioms CubeTrace.key3_inj
#print axioms CubeTrace.i3_sq
#print axioms CubeTrace.cube_trace

The representation \(p=a^2+b^2\) is unique up to order and signs: from \(a^2+b^2=c^2+d^2=p\),

\[ (ac+bd)(ac-bd)=p\,(a^2+c^2-p),\qquad (ac+bd)^2+(ad-bc)^2=p^2, \]
so \(p\) divides \(ac+bd\) (or \(ac-bd\)) and the second identity leaves \((ac+bd,\,ad-bc)\in \{(\pm p,0),(0,\pm p)\}\). Hence the primary \(\pi \) above a split \(p\) is unique up to conjugation, and \(a_p=2\,\mathrm {Re}\,\pi \) is well defined from \(p\) alone.

Listing 74: lang/FermatUnique.lean

lang/FermatUnique.lean

/-
  lang/FermatUnique — T43, cycle 148: uniqueness in Fermat's two-squares theorem, and of the primary π above p.

  If a² + b² = p = c² + d² with p prime, then (c, d) = (±a, ±b) or (±b, ∓a) up to the sign of d:
    lagrange       (ac + bd)² + (ad − bc)² = (a² + b²)(c² + d²) = p²
    case_plus      p | ac + bd ⇒ (ac+bd)/p = ±1 and ad − bc = 0, or ac + bd = 0 and ad − bc = ±p
    fermat_unique  (ac + bd)(ac − bd) = p(a² + c² − p), so one factor is divisible by p: the eight possibilities
    squares_unique {c², d²} = {a², b²}
  So the Gaussian prime above a split p is unique up to units and conjugation, and the Hecke character value ψ(𝔭) = π
  (π primary) is determined up to conjugation: a_p = 2 Re π has a unique value.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.HasseDavenport
import lang.QuarticFamily
import lang.CubicTrace
import lang.FinCubicSign

namespace FermatUnique

open FTAChain LibInt LibRing

/-- p | xy with p prime (as Int, P = ofNat p) ⇒ p | x or p | y. -/
theorem int_euclid (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (x y q : Int)
    (h : x * y = Int.ofNat (p + 1 + 1) * q) :
    (∃ u, x = Int.ofNat (p + 1 + 1) * u) ∨ (∃ u, y = Int.ofNat (p + 1 + 1) * u) := by
  have h0 := HasseDavenport.castZ_mul (ZMod.ring (p + 1)) x y
  rw [h, HasseDavenport.castZ_mul] at h0
  have hP : castZ (ZMod.ring (p + 1)) (Int.ofNat (p + 1 + 1)) = (ZMod.ring (p + 1)).zero := by
    show castN (ZMod.ring (p + 1)) (p + 1 + 1) = _
    rw [IntCast.castN_mk]; exact Subtype.ext (ZMod.dv_md_zero (p + 1) _ ⟨1, by lib_nat⟩)
  rw [hP, DetChain.zero_mul'] at h0
  cases ZMod.nozd p hp _ _ h0.symm with
  | inl h1 => exact Or.inl (HasseDavenport.castZ_zero_dvd (p + 1) x h1)
  | inr h1 => exact Or.inr (HasseDavenport.castZ_zero_dvd (p + 1) y h1)

theorem cancelP (p : Nat) (x y : Int) (h : Int.ofNat (p + 1 + 1) * x = Int.ofNat (p + 1 + 1) * y) : x = y :=
  CubicTrace.cancel_N (p + 1 + 1) (fun e => Nat.noConfusion e) x y h

theorem add_cancel (a : Nat) : ∀ b c : Nat, a + b = a + c → b = c := by
  induction a with
  | zero => intro b c h; rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | succ k ih => intro b c h; apply ih; rw [Nat.succ_add, Nat.succ_add] at h; exact Nat.succ.inj h

theorem lagrange (a b c d : Int) :
    (a * c + b * d) * (a * c + b * d) + (a * d - b * c) * (a * d - b * c) = (a * a + b * b) * (c * c + d * d) := by lib_int

theorem cancelQ (p : Nat) (P : Int) (hP : Int.ofNat (p + 1 + 1) = P) (x y : Int) (h : P * x = P * y) : x = y := by
  rw [← hP] at h; exact cancelP p x y h

/-- the case p | ac + bd: (c, d) ∈ {(a, b), (−a, −b), (−b, a), (b, −a)}. -/
theorem case_plus (p : Nat) (a b c d u : Int) (h1 : a * a + b * b = Int.ofNat (p + 1 + 1))
    (h2 : c * c + d * d = Int.ofNat (p + 1 + 1)) (hu : a * c + b * d = Int.ofNat (p + 1 + 1) * u) :
    (c = a ∧ d = b) ∨ (c = -a ∧ d = -b) ∨ (c = -b ∧ d = a) ∨ (c = b ∧ d = -a) := by
  have hLag := lagrange a b c d
  have hc : (a * a + b * b) * c = a * (a * c + b * d) - b * (a * d - b * c) := by lib_int
  have hd : (a * a + b * b) * d = b * (a * c + b * d) + a * (a * d - b * c) := by lib_int
  rw [h1, h2, hu] at hLag
  rw [h1, hu] at hc hd
  generalize a * d - b * c = Zz at hLag hc hd
  generalize hP : Int.ofNat (p + 1 + 1) = P at hLag hc hd
  match FinCubicSign.sq_nn u, FinCubicSign.sq_nn Zz, FinCubicSign.sq_nn P with
  | ⟨nu, hnu⟩, ⟨nz, hnz⟩, ⟨pp, hpp⟩ =>
    have key : Int.ofNat (pp * nu + nz) = Int.ofNat (pp + 0) := by
      show Int.ofNat pp * Int.ofNat nu + Int.ofNat nz = Int.ofNat pp
      rw [← hpp, ← hnu, ← hnz, show P * P * (u * u) + Zz * Zz = P * u * (P * u) + Zz * Zz by lib_int]; exact hLag
    have hk := Int.ofNat.inj key
    have hpp0 : pp ≠ 0 := fun e => by
      have h0 : P * P = 0 := by rw [hpp, e]; rfl
      have hP0 := HasseDavenport.sq_zero P h0
      rw [← hP] at hP0; exact Nat.noConfusion (Int.ofNat.inj hP0)
    match nu with
    | 0 =>
      have u0 : u = 0 := HasseDavenport.sq_zero u hnu
      have hnzp : nz = pp := by
        have e : pp * 0 + nz = nz := by lib_nat
        rw [e] at hk; exact hk
      have hz : Zz * Zz = P * P := by rw [hnz, hpp, hnzp]
      rw [u0] at hc hd
      cases QuarticFamily.sq_eq Zz P hz with
      | inl e =>
        right; right; left
        rw [e] at hc hd
        exact ⟨cancelQ p P hP c (-b) (by rw [hc]; lib_int), cancelQ p P hP d a (by rw [hd]; lib_int)⟩
      | inr e =>
        right; right; right
        rw [e] at hc hd
        exact ⟨cancelQ p P hP c b (by rw [hc]; lib_int), cancelQ p P hP d (-a) (by rw [hd]; lib_int)⟩
    | 1 =>
      have hz0 : nz = 0 := add_cancel pp _ _ ((show pp + nz = pp * 1 + nz by lib_nat).trans hk)
      have z0 : Zz = 0 := HasseDavenport.sq_zero Zz (by rw [hnz, hz0]; rfl)
      rw [z0] at hc hd
      cases QuarticFamily.sq_eq u 1 (by rw [hnu]; rfl) with
      | inl e =>
        left
        rw [e] at hc hd
        exact ⟨cancelQ p P hP c a (by rw [hc]; lib_int), cancelQ p P hP d b (by rw [hd]; lib_int)⟩
      | inr e =>
        right; left
        rw [e] at hc hd
        exact ⟨cancelQ p P hP c (-a) (by rw [hc]; lib_int), cancelQ p P hP d (-b) (by rw [hd]; lib_int)⟩
    | n + 2 =>
      exfalso
      have h3 : pp * (n + 1) + nz = 0 := add_cancel pp _ _ ((show pp + (pp * (n + 1) + nz) = pp * (n + 2) + nz by lib_nat).trans hk)
      have h4 := (HasseDavenport.nat_sum_zero _ _ h3).1
      have h5 : pp + pp * n = 0 := (show pp + pp * n = pp * (n + 1) by lib_nat).trans h4
      exact hpp0 (HasseDavenport.nat_sum_zero _ _ h5).1

def Four (a b c d : Int) : Prop := (c = a ∧ d = b) ∨ (c = -a ∧ d = -b) ∨ (c = -b ∧ d = a) ∨ (c = b ∧ d = -a)

/-- **Uniqueness in Fermat's two-squares theorem**: a² + b² = p = c² + d² (p prime) ⇒ (c, ±d) ∈ {(a, b), (−a, −b), (−b, a), (b, −a)}. -/
theorem fermat_unique (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b c d : Int)
    (h1 : a * a + b * b = Int.ofNat (p + 1 + 1)) (h2 : c * c + d * d = Int.ofNat (p + 1 + 1)) :
    Four a b c d ∨ Four a b c (-d) := by
  have key : (a * c + b * d) * (a * c + -1 * (b * d)) = Int.ofNat (p + 1 + 1) * (a * a + c * c + -1 * Int.ofNat (p + 1 + 1)) := by
    have hb : b * b = Int.ofNat (p + 1 + 1) + -1 * (a * a) := by rw [← h1]; lib_int
    have hd : d * d = Int.ofNat (p + 1 + 1) + -1 * (c * c) := by rw [← h2]; lib_int
    rw [show (a * c + b * d) * (a * c + -1 * (b * d)) = a * a * (c * c) + -1 * ((b * b) * (d * d)) by lib_int, hb, hd]
    lib_int
  cases int_euclid p hp _ _ _ key with
  | inl h =>
    match h with
    | ⟨u, hu⟩ => exact Or.inl (case_plus p a b c d u h1 h2 hu)
  | inr h =>
    match h with
    | ⟨u, hu⟩ =>
      exact Or.inr (case_plus p a b c (-d) u h1 (by rw [← h2]; lib_int) (by rw [← hu]; lib_int))

/-- the squares {a², b²} are determined by p. -/
theorem squares_unique (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b c d : Int)
    (h1 : a * a + b * b = Int.ofNat (p + 1 + 1)) (h2 : c * c + d * d = Int.ofNat (p + 1 + 1)) :
    c * c = a * a ∨ c * c = b * b := by
  have aux : ∀ dd : Int, Four a b c dd → c * c = a * a ∨ c * c = b * b := fun dd h => by
    rcases h with ⟨e, _⟩ | ⟨e, _⟩ | ⟨e, _⟩ | ⟨e, _⟩
    · left; rw [e]
    · left; rw [e]; lib_int
    · right; rw [e]; lib_int
    · right; rw [e]
  cases fermat_unique p hp a b c d h1 h2 with
  | inl h => exact aux d h
  | inr h => exact aux (-d) h

end FermatUnique

#print axioms FermatUnique.int_euclid
#print axioms FermatUnique.cancelP
#print axioms FermatUnique.add_cancel
#print axioms FermatUnique.lagrange
#print axioms FermatUnique.cancelQ
#print axioms FermatUnique.case_plus
#print axioms FermatUnique.Four
#print axioms FermatUnique.fermat_unique
#print axioms FermatUnique.squares_unique

With \(b,d\) even and \(a+b\equiv c+d\equiv 1\pmod 4\), the eight sign and order variants reduce to \(c=a\), \(d=\pm b\): the variants \(c=\pm b\) make \(c+d\) even, and \(c=-a\) forces \(4\mid 2\). So the primary \(\pi \) above \(p\) is unique up to conjugation and \(\psi (\pi )+\psi (\bar \pi )=2a\) depends on \(p\) alone.

Listing 75: lang/PrimaryUnique.lean

lang/PrimaryUnique.lean

/-
  lang/PrimaryUnique — T43, cycle 149: the primary Gaussian prime above a split p is unique up to conjugation.

  π = a + bi and π' = c + di primary (b, d even; a + b ≡ c + d ≡ 1 mod 4) with N(π) = N(π') = p prime:
    four_bad          4k ≠ 2
    primary_unique    c = a and d = ±b                        (FermatUnique + the congruences mod 4)
    hecke_unique      ψ(π') + ψ(π̄') = ψ(π) + ψ(π̄) = 2a        (the Hecke value at p does not depend on the choice)
  So a_p = 2 Re π of the CM form of y² = x³ − x is a function of p alone.

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.FermatUnique
import lang.GaussInt

namespace PrimaryUnique

open FTAChain LibGcd GaussInt FermatUnique

theorem four_bad (k : Int) (h : 4 * k = 2) : False :=
  two_ne_one k (CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) (2 * k) 1
    (show (2 : Int) * (2 * k) = 2 * 1 by rw [show (2 : Int) * (2 * k) = 4 * k by lib_int, h]; rfl))

theorem settle (a b c d dd : Int) (hdd : dd = d ∨ dd = -d) (e f s t : Int)
    (hb : b = 2 * e) (hab : a + b - 1 = 4 * t) (hd : d = 2 * f) (hcd : c + d - 1 = 4 * s)
    (h : Four a b c dd) : c = a ∧ (d = b ∨ d = -b) := by
  rcases h with ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩
  · refine ⟨h1, ?_⟩
    cases hdd with
    | inl q => left; rw [← q, h2]
    | inr q => right; rw [← h2, q]; lib_int
  · exfalso
    have hd' : d = -b ∨ d = b := by
      cases hdd with
      | inl q => left; rw [← q, h2]
      | inr q => right; rw [show d = -(-d) by lib_int, ← q, h2]; lib_int
    cases hd' with
    | inl q =>
      apply four_bad (-(s + t))
      rw [show 4 * -(s + t) = -(4 * s) + -(4 * t) by lib_int, ← hcd, ← hab, h1, q]; lib_int
    | inr q =>
      apply four_bad (e - s - t)
      rw [show 4 * (e - s - t) = 2 * (2 * e) + -(4 * s) + -(4 * t) by lib_int, ← hb, ← hcd, ← hab, h1, q]; lib_int
  · exfalso
    apply two_ne_one (f - e - 2 * s)
    rw [show 2 * (f - e - 2 * s) = 2 * f + -(2 * e) + -(4 * s) by lib_int, ← hd, ← hb, ← hcd, h1]; lib_int
  · exfalso
    apply two_ne_one (e + f - 2 * s)
    rw [show 2 * (e + f - 2 * s) = 2 * e + 2 * f + -(4 * s) by lib_int, ← hd, ← hb, ← hcd, h1]; lib_int

/-- **The primary π above p is unique up to conjugation.** -/
theorem primary_unique (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b c d : Int)
    (hP1 : Primary ⟨a, b⟩) (hP2 : Primary ⟨c, d⟩)
    (h1 : a * a + b * b = Int.ofNat (p + 1 + 1)) (h2 : c * c + d * d = Int.ofNat (p + 1 + 1)) :
    c = a ∧ (d = b ∨ d = -b) := by
  match hP1, hP2 with
  | ⟨⟨e, hb⟩, ⟨t, hab⟩⟩, ⟨⟨f, hd⟩, ⟨s, hcd⟩⟩ =>
    cases fermat_unique p hp a b c d h1 h2 with
    | inl h => exact settle a b c d d (Or.inl rfl) e f s t hb hab hd hcd h
    | inr h => exact settle a b c d (-d) (Or.inr rfl) e f s t hb hab hd hcd h

/-- **The Hecke value at p is well defined.** -/
theorem hecke_unique (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (x y : GI)
    (hx : Primary x) (hy : Primary y) (h1 : gnorm x = Int.ofNat (p + 1 + 1)) (h2 : gnorm y = Int.ofNat (p + 1 + 1)) :
    y.re + (gconj y).re = x.re + (gconj x).re := by
  rw [hecke_trace, hecke_trace, (primary_unique p hp x.re x.im y.re y.im hx hy h1 h2).1]

end PrimaryUnique

#print axioms PrimaryUnique.four_bad
#print axioms PrimaryUnique.settle
#print axioms PrimaryUnique.primary_unique
#print axioms PrimaryUnique.hecke_unique

Gauss’s formula follows for every choice: for every prime \(p\equiv 1\pmod 4\) (\(\sqrt {-1}\in \mathbb F_p\) by Euler’s criterion) and any primary \(c+di\) of norm \(p\),

\[ -\sum _{x\in \mathbb F_p}\chi (x^3-x)=2c=\psi (\pi )+\psi (\bar \pi ). \]

Listing 76: lang/GaussAp.lean

lang/GaussAp.lean

/-
  lang/GaussAp — T43, cycle 150: Gauss's formula for y² = x³ − x over 𝔽_p, p ≡ 1 (mod 4), for every choice of π.

  For p = 2m + 3 prime with i² = −1 in 𝔽_p and ANY primary c + di with c² + d² = p:
    gauss_ap      −Σ_{x ∈ 𝔽_p} χ(x³ − x) = 2c
    exists_i      i exists for p = 4j + 5 (FF.euler_hard: (−1)^((p−1)/2) = 1)
    gauss_ap4     the formula at every p ≡ 1 (mod 4), no i assumed
  (FinPrimary gives one primary π with a_p = 2 Re π; PrimaryUnique says every primary of norm p has the same real part.)
  So a_p = ψ(π) + ψ(π̄) for the Hecke character ψ(α) = α on primary α, at every split prime, with no choice left —
  the GL1 → GL2 identity a_p(E) = a_p(θ_ψ) at split p as a statement about p alone.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PrimaryUnique
import lang.FinPrimary

namespace GaussAp

open FTAChain ZMod YMChain FiniteRecChain

theorem val_inj (m : Nat) (x y : Zp (2 * m + 1 + 1)) (h : x.val = y.val) : x = y := Subtype.ext h

/-- **Gauss: a_p = 2c for every primary c + di of norm p.** -/
theorem gauss_ap (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (i : Zp (2 * m + 1 + 1)) (hi : (ring (2 * m + 1 + 1)).mul i i = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)
    (c d : Int) (hP : GaussInt.Primary ⟨c, d⟩) (hcd : c * c + d * d = Int.ofNat (2 * m + 1 + 1 + 1)) :
    -sumK LibInt.intR (fun x => FinField.chi (FinField.ffp m hpr) (FinSign.f (FinField.ffp m hpr) x)) (FinField.ffp m hpr).els = 2 * c := by
  match FinPrimary.fin_ap_primary (FinField.ffp m hpr) (fun x => x.val) (val_inj m) i hi with
  | ⟨a, b, hab, hPa, hS⟩ =>
    have hab' : a * a + b * b = Int.ofNat (2 * m + 1 + 1 + 1) := by
      rw [hab]; exact congrArg Int.ofNat (show 2 * (m + 1) + 1 = 2 * m + 1 + 1 + 1 by lib_nat)
    rw [hS, (PrimaryUnique.primary_unique (2 * m + 1) hpr a b c d hPa hP hab' hcd).1]

/-- −1 is a square in 𝔽_p for p = 4j + 5 (Euler's criterion, the hard half). -/
theorem exists_i (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * j + 1) + 1 + 1 + 1) :
    ∃ i, (ring (2 * (2 * j + 1) + 1 + 1)).mul i i = (ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one := by
  apply (FinField.ffp (2 * j + 1) hpr).euler_hard
  show Frobenius.pw (ring (2 * (2 * j + 1) + 1 + 1)) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) (2 * j + 1 + 1) = (ring (2 * (2 * j + 1) + 1 + 1)).one
  rw [show 2 * j + 1 + 1 = 2 * (j + 1) by lib_nat, ← TwoPower.pwpow,
    show Frobenius.pw (ring (2 * (2 * j + 1) + 1 + 1)) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) 2 = (ring (2 * (2 * j + 1) + 1 + 1)).one from
      ((ring (2 * (2 * j + 1) + 1 + 1)).one_mul _ |> fun e => by show (ring (2 * (2 * j + 1) + 1 + 1)).mul ((ring (2 * (2 * j + 1) + 1 + 1)).mul (ring (2 * (2 * j + 1) + 1 + 1)).one _) _ = _; rw [e]; exact TwoPower.negone_sq' _),
    TwoPower.pwone]

/-- **Gauss's formula at every p ≡ 1 (mod 4)**, p = 4j + 5: a_p = 2c for every primary c + di of norm p. -/
theorem gauss_ap4 (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * j + 1) + 1 + 1 + 1)
    (c d : Int) (hP : GaussInt.Primary ⟨c, d⟩) (hcd : c * c + d * d = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1)) :
    -sumK LibInt.intR (fun x => FinField.chi (FinField.ffp (2 * j + 1) hpr) (FinSign.f (FinField.ffp (2 * j + 1) hpr) x))
      (FinField.ffp (2 * j + 1) hpr).els = 2 * c :=
  match exists_i j hpr with
  | ⟨i, hi⟩ => gauss_ap (2 * j + 1) hpr i hi c d hP hcd

end GaussAp

#print axioms GaussAp.val_inj
#print axioms GaussAp.gauss_ap
#print axioms GaussAp.exists_i
#print axioms GaussAp.gauss_ap4

Writing \(d=2e\) and \(c=1-d+4t\), the parity of \(e\) decides both \(p=c^2+d^2\bmod 8\) and \(2c\bmod 8\):

\[ p\equiv 1\ (8)\ \Rightarrow \ a_p\equiv 2\ (8),\qquad p\equiv 5\ (8)\ \Rightarrow \ a_p\equiv -2\ (8), \]
so \(8\mid \#E(\mathbb F_p)=p+1-a_p\) at every \(p\equiv 1\pmod 4\).

Listing 77: lang/ApMod8.lean

lang/ApMod8.lean

/-
  lang/ApMod8 — T43, cycle 152: a_p mod 8 for y² = x³ − x at p ≡ 1 (mod 4).

  π = c + di primary of norm p: d = 2e, c + d ≡ 1 (mod 4).
    prim_mod8     e even ⇒ 2c ≡ 2 (8) and p ≡ 1 (8);  e odd ⇒ 2c ≡ −2 (8) and p ≡ 5 (8)
    ap_mod8       p = 8l + 1 ⇒ a_p ≡ 2 (mod 8);  p = 8l + 5 ⇒ a_p ≡ −2 (mod 8)
    count_mod8    8 | #E(𝔽_p) = p + 1 − a_p at every p ≡ 1 (mod 4)
  The 2-adic shadow of the CM form: a_p ≡ 2 (mod 4) at every split p, and its class mod 8 is the class of p mod 8.

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.GaussAp
import lang.EveryPrime

namespace ApMod8

open FTAChain ZMod YMChain FiniteRecChain LibGcd GaussInt

theorem prim_mod8 (c d : Int) (hP : Primary ⟨c, d⟩) :
    ((∃ k, 2 * c - 2 = 8 * k) ∧ ∃ l, c * c + d * d = 8 * l + 1) ∨
    ((∃ k, 2 * c + 2 = 8 * k) ∧ ∃ l, c * c + d * d = 8 * l + 5) := by
  match hP with
  | ⟨⟨e, hd⟩, ⟨t, hcd⟩⟩ =>
    have hd' : d = 2 * e := hd
    have hcd' : c + d - 1 = 4 * t := hcd
    have hc : c = 1 - d + 4 * t := by rw [← hcd']; lib_int
    cases int_even_odd e with
    | inl h =>
      match h with
      | ⟨g, hg⟩ =>
        left
        rw [hc, hd', hg]
        exact ⟨⟨t - g, by lib_int⟩, ⟨(t - g) + 2 * (t - g) * (t - g) + 2 * g * g, by lib_int⟩⟩
    | inr h =>
      match h with
      | ⟨g, hg⟩ =>
        right
        rw [hc, hd', hg]
        exact ⟨⟨t - g, by lib_int⟩, ⟨-(t - g) + 2 * (t - g) * (t - g) + 2 * g * g + 2 * g, by lib_int⟩⟩

theorem one_ne_five (l l' : Int) (h : 8 * l + 1 = 8 * l' + 5) : False :=
  two_ne_one (l - l') (CubicTrace.cancel_N 4 (fun e => Nat.noConfusion e) (2 * (l - l')) 1
    (show (4 : Int) * (2 * (l - l')) = 4 * 1 by
      rw [show (4 : Int) * (2 * (l - l')) = 8 * l + 1 - (8 * l' + 5) + 4 by lib_int, h]; lib_int))

/-- **a_p mod 8.** -/
theorem ap_mod8 (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * j + 1) + 1 + 1 + 1) :
    ((∃ l, Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) = 8 * l + 1) → ∃ k,
      -sumK LibInt.intR (fun x => FinField.chi (FinField.ffp (2 * j + 1) hpr) (FinSign.f (FinField.ffp (2 * j + 1) hpr) x))
        (FinField.ffp (2 * j + 1) hpr).els - 2 = 8 * k) ∧
    ((∃ l, Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) = 8 * l + 5) → ∃ k,
      -sumK LibInt.intR (fun x => FinField.chi (FinField.ffp (2 * j + 1) hpr) (FinSign.f (FinField.ffp (2 * j + 1) hpr) x))
        (FinField.ffp (2 * j + 1) hpr).els + 2 = 8 * k) := by
  match GaussAp.exists_i j hpr with
  | ⟨i, hi⟩ =>
    match FinPrimary.fin_ap_primary (FinField.ffp (2 * j + 1) hpr) (fun x => x.val) (GaussAp.val_inj (2 * j + 1)) i hi with
    | ⟨a, b, hab, hPa, hS⟩ =>
      have hab' : a * a + b * b = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) := by
        rw [hab]; exact congrArg Int.ofNat (show 2 * (2 * j + 1 + 1) + 1 = 2 * (2 * j + 1) + 1 + 1 + 1 by lib_nat)
      rw [hS]
      cases prim_mod8 a b hPa with
      | inl h =>
        match h with
        | ⟨hk, ⟨l, hl⟩⟩ =>
          exact ⟨fun _ => hk, fun ⟨l', hl'⟩ => absurd (hl.symm.trans (hab'.trans hl')) (fun e => one_ne_five l l' e)⟩
      | inr h =>
        match h with
        | ⟨hk, ⟨l, hl⟩⟩ =>
          exact ⟨fun ⟨l', hl'⟩ => absurd (hl'.symm.trans (hab'.symm.trans hl)) (fun e => one_ne_five l' l e), fun _ => hk⟩

/-- **8 | #E(𝔽_p) = p + 1 − a_p** for y² = x³ − x at every p ≡ 1 (mod 4). -/
theorem count_mod8 (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * j + 1) + 1 + 1 + 1) :
    ∃ k, Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) + 1 - (-sumK LibInt.intR (fun x => FinField.chi (FinField.ffp (2 * j + 1) hpr) (FinSign.f (FinField.ffp (2 * j + 1) hpr) x)) (FinField.ffp (2 * j + 1) hpr).els) = 8 * k := by
  have h := ap_mod8 j hpr
  generalize -sumK LibInt.intR (fun x => FinField.chi (FinField.ffp (2 * j + 1) hpr) (FinSign.f (FinField.ffp (2 * j + 1) hpr) x)) (FinField.ffp (2 * j + 1) hpr).els = A at h ⊢
  cases EveryPrime.nat_even_odd j with
  | inl hj =>
    match hj with
    | ⟨g, hg⟩ =>
      have hp : Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) = 8 * Int.ofNat g + 5 := by
        rw [hg]; show Int.ofNat (2 * (2 * (2 * g) + 1) + 1 + 1 + 1) = Int.ofNat (8 * g + 5)
        exact congrArg Int.ofNat (by lib_nat)
      match h.2 ⟨Int.ofNat g, hp⟩ with
      | ⟨k, hk⟩ => exact ⟨Int.ofNat g + 1 - k, by rw [hp, show A = 8 * k - 2 by rw [← hk]; lib_int]; lib_int⟩
  | inr hj =>
    match hj with
    | ⟨g, hg⟩ =>
      have hp : Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) = 8 * Int.ofNat (g + 1) + 1 := by
        rw [hg]; show Int.ofNat (2 * (2 * (2 * g + 1) + 1) + 1 + 1 + 1) = Int.ofNat (8 * (g + 1) + 1)
        exact congrArg Int.ofNat (by lib_nat)
      match h.1 ⟨Int.ofNat (g + 1), hp⟩ with
      | ⟨k, hk⟩ => exact ⟨Int.ofNat (g + 1) - k, by rw [hp, show A = 8 * k + 2 by rw [← hk]; lib_int]; lib_int⟩

end ApMod8

#print axioms ApMod8.prim_mod8
#print axioms ApMod8.one_ne_five
#print axioms ApMod8.ap_mod8
#print axioms ApMod8.count_mod8

For \(\mathbb Q(\sqrt {-3})\) the same argument gives uniqueness of \(p=x^2+3y^2\) (\(p\ne 3\) prime):

\[ (xu+3yv)^2+3(xv-yu)^2=p^2,\qquad (xu+3yv)(xu-3yv)=p\,(x^2+u^2-p), \]
and \(3Z^2=p^2\) is impossible, so \(u=\pm x\).

Listing 78: lang/EisenUnique.lean

lang/EisenUnique.lean

/-
  lang/EisenUnique — T43, cycle 154: uniqueness of p = x² + 3y² (p prime, p ≠ 3).

    lagrange3      (xu + 3yv)² + 3(xv − yu)² = (x² + 3y²)(u² + 3v²)
    no_three_sq    3Z² ≠ P² for P prime ≠ 3                            (Euclid in ℤ/3, then 3 | P)
    case_plus3     P | xu + 3yv ⇒ u = ±x
    eisen_unique   x² + 3y² = p = u² + 3v² ⇒ u² = x², v² = y²          ((xu + 3yv)(xu − 3yv) = p(x² + u² − p))
  The Eisenstein analogue of FermatUnique: the prime of ℤ[ω] above a split p is unique up to units and conjugation,
  so the Hecke character of ℚ(√−3) has a well-defined value at p up to the sextic units.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.FermatUnique
import lang.Eisen

namespace EisenUnique

open FTAChain LibInt FermatUnique

theorem lagrange3 (x y u v : Int) :
    (x * u + 3 * (y * v)) * (x * u + 3 * (y * v)) + 3 * ((x * v - y * u) * (x * v - y * u))
      = (x * x + 3 * (y * y)) * (u * u + 3 * (v * v)) := by lib_int

theorem nat_of_three (n : Nat) (k : Int) (h : Int.ofNat n = 3 * k) : ∃ q : Nat, n = q * 3 := by
  cases k with
  | ofNat q => exact ⟨q, Int.ofNat.inj (h.trans (by show Int.ofNat (3 * q) = Int.ofNat (q * 3); rw [Nat.mul_comm]))⟩
  | negSucc q =>
    exfalso
    have e : (3 : Int) * Int.negSucc q = Int.negSucc (3 * q + 2) := by
      show -(3 * Int.ofNat (q + 1)) = Int.negSucc (3 * q + 2)
      rw [show (3 : Int) * Int.ofNat (q + 1) = Int.ofNat (3 * q + 2 + 1) by
        show Int.ofNat (3 * (q + 1)) = Int.ofNat (3 * q + 2 + 1); rw [show 3 * (q + 1) = 3 * q + 2 + 1 by lib_nat]]
      rfl
    rw [e] at h
    exact Int.noConfusion h

theorem no_three_sq (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (Z : Int)
    (h : 3 * (Z * Z) = Int.ofNat (p + 1 + 1) * Int.ofNat (p + 1 + 1)) : False := by
  have bad : ∀ k, Int.ofNat (p + 1 + 1) = 3 * k → False := fun k hk => by
    match nat_of_three _ k hk with
    | ⟨q, hq⟩ =>
      cases hp 3 ⟨q, hq⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e => exact h3 e.symm
  cases int_euclid 1 Eisen.prime3 _ _ (Z * Z) h.symm with
  | inl hu => match hu with | ⟨k, hk⟩ => exact bad k hk
  | inr hu => match hu with | ⟨k, hk⟩ => exact bad k hk

theorem case_plus3 (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3)
    (x y u v w : Int) (h1 : x * x + 3 * (y * y) = Int.ofNat (p + 1 + 1))
    (h2 : u * u + 3 * (v * v) = Int.ofNat (p + 1 + 1)) (hw : x * u + 3 * (y * v) = Int.ofNat (p + 1 + 1) * w) :
    u = x ∨ u = -x := by
  have hLag := lagrange3 x y u v
  have hu : (x * x + 3 * (y * y)) * u = x * (x * u + 3 * (y * v)) - 3 * (y * (x * v - y * u)) := by lib_int
  rw [h1, h2, hw] at hLag
  rw [h1, hw] at hu
  generalize x * v - y * u = Zz at hLag hu
  have hthree := no_three_sq p hp h3 Zz
  generalize hP : Int.ofNat (p + 1 + 1) = P at hLag hu hthree
  match FinCubicSign.sq_nn w, FinCubicSign.sq_nn Zz, FinCubicSign.sq_nn P with
  | ⟨nw, hnw⟩, ⟨nz, hnz⟩, ⟨pp, hpp⟩ =>
    have key : Int.ofNat (pp * nw + 3 * nz) = Int.ofNat (pp + 0) := by
      show Int.ofNat pp * Int.ofNat nw + 3 * Int.ofNat nz = Int.ofNat pp
      rw [← hpp, ← hnw, ← hnz, show P * P * (w * w) + 3 * (Zz * Zz) = P * w * (P * w) + 3 * (Zz * Zz) by lib_int]; exact hLag
    have hk := Int.ofNat.inj key
    have hpp0 : pp ≠ 0 := fun e => by
      have h0 : P * P = 0 := by rw [hpp, e]; rfl
      have hP0 := HasseDavenport.sq_zero P h0
      rw [← hP] at hP0; exact Nat.noConfusion (Int.ofNat.inj hP0)
    match nw with
    | 0 =>
      exfalso
      apply hthree
      have e : 3 * nz = pp := by rw [show pp * 0 + 3 * nz = 3 * nz by lib_nat] at hk; exact hk
      rw [hnz, hpp, ← e]; rfl
    | 1 =>
      have hz0 : 3 * nz = 0 := add_cancel pp _ _ ((show pp + 3 * nz = pp * 1 + 3 * nz by lib_nat).trans hk)
      have hz0' : nz = 0 := (HasseDavenport.nat_sum_zero nz (nz + nz) ((show nz + (nz + nz) = 3 * nz by lib_nat).trans hz0)).1
      have z0 : Zz = 0 := HasseDavenport.sq_zero Zz (by rw [hnz, hz0']; rfl)
      rw [z0] at hu
      cases QuarticFamily.sq_eq w 1 (by rw [hnw]; rfl) with
      | inl e =>
        left; rw [e] at hu
        exact cancelQ p P hP u x (by rw [hu]; lib_int)
      | inr e =>
        right; rw [e] at hu
        exact cancelQ p P hP u (-x) (by rw [hu]; lib_int)
    | n + 2 =>
      exfalso
      have h3' : pp * (n + 1) + 3 * nz = 0 := add_cancel pp _ _ ((show pp + (pp * (n + 1) + 3 * nz) = pp * (n + 2) + 3 * nz by lib_nat).trans hk)
      have h4 := (HasseDavenport.nat_sum_zero _ _ h3').1
      have h5 : pp + pp * n = 0 := (show pp + pp * n = pp * (n + 1) by lib_nat).trans h4
      exact hpp0 (HasseDavenport.nat_sum_zero _ _ h5).1

/-- **Uniqueness of p = x² + 3y².** -/
theorem eisen_unique (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (x y u v : Int)
    (h1 : x * x + 3 * (y * y) = Int.ofNat (p + 1 + 1)) (h2 : u * u + 3 * (v * v) = Int.ofNat (p + 1 + 1)) :
    u * u = x * x := by
  have key : (x * u + 3 * (y * v)) * (x * u + 3 * (y * (-v))) = Int.ofNat (p + 1 + 1) * (x * x + u * u + -1 * Int.ofNat (p + 1 + 1)) := by
    have hy : 3 * (y * y) = Int.ofNat (p + 1 + 1) + -1 * (x * x) := by rw [← h1]; lib_int
    have hv : 3 * (v * v) = Int.ofNat (p + 1 + 1) + -1 * (u * u) := by rw [← h2]; lib_int
    rw [show (x * u + 3 * (y * v)) * (x * u + 3 * (y * (-v))) = x * x * (u * u) + -1 * (3 * (y * y) * (3 * (v * v))) by lib_int, hy, hv]
    lib_int
  have fin : u = x ∨ u = -x → u * u = x * x := fun h => by
    cases h with
    | inl e => rw [e]
    | inr e => rw [e]; lib_int
  cases int_euclid p hp _ _ _ key with
  | inl h => match h with | ⟨w, hw⟩ => exact fin (case_plus3 p hp h3 x y u v w h1 h2 hw)
  | inr h => match h with | ⟨w, hw⟩ => exact fin (case_plus3 p hp h3 x y u (-v) w h1 (by rw [← h2]; lib_int) hw)

end EisenUnique

#print axioms EisenUnique.lagrange3
#print axioms EisenUnique.nat_of_three
#print axioms EisenUnique.no_three_sq
#print axioms EisenUnique.case_plus3
#print axioms EisenUnique.eisen_unique

With \(\psi (1)=\pm 2u\) (\(u^2+3w^2=p\)), the uniqueness and \(\psi (1)\equiv 1\pmod 3\) give the exact formula for \(y^2=x^3+1\) at every \(p\equiv 1\pmod 3\): such \(x\) exist, and for every \(x\equiv 1\pmod 3\) with \(x^2+3y^2=p\),

\[ a_p=-\sum _{t\in \mathbb F_p}\chi (t^3+1)=2x , \]
and \(p+1-a_p=9s^2+3y^2=2(m+2-x)\) (\(x=3s+1\), \(p=2m+3\)) gives \(6\mid \#E(\mathbb F_p)\).

Listing 79: lang/EisenAp.lean

lang/EisenAp.lean

/-
  lang/EisenAp — T43, cycle 155: the exact formula for y² = x³ + 1 at every split p (p ≡ 1 mod 3).

  p = 2m + 3 prime, 3 | p − 1. For ANY x, y with x² + 3y² = p and x ≡ 1 (mod 3):
    three_ne_r, p_ne_three        3k ≠ 1, 2;  3 | p − 1 ⇒ p ≠ 3
    eisen_ap                      Σ_{t ∈ 𝔽_p} χ(t³ + 1) = −2x,   i.e. a_p = 2x
    eisen_exists                  such an x exists
    count_mod6                    6 | #E(𝔽_p) = p + 1 − a_p
  (FinCubicExact: ψ(1) = ±2u with u² + 3w² = p; EisenUnique: x = ±u; FinCubicTrace: ψ(1) ≡ 1 mod 3 fixes the sign.)
  This is a_p = ψ(π) + ψ(π̄) for π = x + y√−3 ≡ 1 mod 3, the Hecke character of ℚ(√−3) attached to y² = x³ + 1,
  with no choice left — the Eisenstein counterpart of GaussAp.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.EisenUnique
import lang.FinCubicExact

namespace EisenAp

open FTAChain ZMod YMChain FiniteRecChain

theorem three_ne_r (k : Int) (r : Nat) (hr0 : r ≠ 0) (hr : r < 3) (h : 3 * k = Int.ofNat r) : False := by
  cases k with
  | ofNat q =>
    have e : 3 * q = r := Int.ofNat.inj h
    cases q with
    | zero => exact hr0 e.symm
    | succ q =>
      have : 3 * (q + 1) ≥ 3 := Nat.le_add_left 3 (3 * q)
      rw [e] at this; exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le hr this)
  | negSucc q =>
    have e : (3 : Int) * Int.negSucc q = Int.negSucc (3 * q + 2) := by
      show -(3 * Int.ofNat (q + 1)) = Int.negSucc (3 * q + 2)
      rw [show (3 : Int) * Int.ofNat (q + 1) = Int.ofNat (3 * q + 2 + 1) by
        show Int.ofNat (3 * (q + 1)) = Int.ofNat (3 * q + 2 + 1); rw [show 3 * (q + 1) = 3 * q + 2 + 1 by lib_nat]]
      rfl
    rw [e] at h
    exact Int.noConfusion h

theorem p_ne_three (m N : Nat) (hN : 2 * (m + 1) = 3 * N) : 2 * m + 1 + 1 + 1 ≠ 3 := fun e => by
  have hm : m = 0 := by
    cases m with
    | zero => rfl
    | succ k => exact absurd (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show 2 * (k + 1) + 1 + 1 + 1 = 2 * k + 2 + 1 + 1 + 1 by lib_nat).symm.trans e)))) (fun h => Nat.noConfusion h)
  rw [hm] at hN
  exact three_ne_r (Int.ofNat N) 2 (fun h => Nat.noConfusion h) (Nat.lt_succ_self 2)
    (congrArg Int.ofNat hN.symm)

/-- **y² = x³ + 1: Σ_t χ(t³ + 1) = −2x for every x ≡ 1 (mod 3) with x² + 3y² = p.** -/
theorem eisen_ap (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (N : Nat)
    (hN : 2 * (m + 1) = 3 * N) (x y : Int) (hxy : x * x + 3 * (y * y) = Int.ofNat (2 * m + 1 + 1 + 1))
    (hx : ∃ s, x = 3 * s + 1) :
    FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = -2 * x := by
  match FinCubicExact.fin_cubic_trace (FinField.ffp m hpr) N hN, FinCubicTrace.psi_one_mod3_ff (FinField.ffp m hpr) N hN, hx with
  | ⟨u, w, huw, hpsi⟩, ⟨T, hT⟩, ⟨s, hs⟩ =>
    have huw' : Int.ofNat u * Int.ofNat u + 3 * (Int.ofNat w * Int.ofNat w) = Int.ofNat (2 * m + 1 + 1 + 1) :=
      congrArg Int.ofNat (huw.trans (show 2 * (m + 1) + 1 = 2 * m + 1 + 1 + 1 by lib_nat))
    have hsq := EisenUnique.eisen_unique (2 * m + 1) hpr (p_ne_three m N hN) x y (Int.ofNat u) (Int.ofNat w) hxy huw'
    have hpm : FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = 2 * x ∨
        FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = -2 * x := by
      have h2u : Int.ofNat (2 * u) = 2 * Int.ofNat u := rfl
      cases QuarticFamily.sq_eq _ _ hsq with
      | inl e =>
        cases hpsi with
        | inl h => left; rw [h, h2u, e]
        | inr h => right; rw [h, h2u, e]; lib_int
      | inr e =>
        cases hpsi with
        | inl h => right; rw [h, h2u, e]; lib_int
        | inr h => left; rw [h, h2u, e]; lib_int
    cases hpm with
    | inl h =>
      exfalso
      apply three_ne_r (T - 2 * s) 1 (fun h => Nat.noConfusion h) (Nat.lt_succ_of_lt (Nat.lt_succ_self 1))
      have : 2 * x = 1 + 3 * T := h.symm.trans hT
      rw [hs] at this
      show _ = 1
      rw [show 3 * (T - 2 * s) = 1 + 3 * T + -(2 * (3 * s + 1)) + 1 by lib_int, ← this]; lib_int
    | inr h => exact h

/-- **Existence: there is an x ≡ 1 (mod 3) with x² + 3y² = p, and then a_p = 2x.** -/
theorem eisen_exists (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (N : Nat)
    (hN : 2 * (m + 1) = 3 * N) :
    ∃ x y : Int, x * x + 3 * (y * y) = Int.ofNat (2 * m + 1 + 1 + 1) ∧ (∃ s, x = 3 * s + 1) ∧
      FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = -2 * x := by
  match FinCubicExact.fin_cubic_trace (FinField.ffp m hpr) N hN, FinCubicTrace.psi_one_mod3_ff (FinField.ffp m hpr) N hN with
  | ⟨u, w, huw, hpsi⟩, ⟨T, hT⟩ =>
    have huw' : Int.ofNat u * Int.ofNat u + 3 * (Int.ofNat w * Int.ofNat w) = Int.ofNat (2 * m + 1 + 1 + 1) :=
      congrArg Int.ofNat (huw.trans (show 2 * (m + 1) + 1 = 2 * m + 1 + 1 + 1 by lib_nat))
    have h2u : Int.ofNat (2 * u) = 2 * Int.ofNat u := rfl
    generalize FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = S at hpsi hT ⊢
    have fin : ∀ x : Int, x * x = Int.ofNat u * Int.ofNat u → S = -2 * x →
        ∃ x y : Int, x * x + 3 * (y * y) = Int.ofNat (2 * m + 1 + 1 + 1) ∧ (∃ s, x = 3 * s + 1) ∧ S = -2 * x :=
      fun x hx hS => ⟨x, Int.ofNat w, by rw [hx]; exact huw', ⟨x + T, by
        have : -2 * x = 1 + 3 * T := hS.symm.trans hT
        rw [show 3 * (x + T) + 1 = 3 * x + (1 + 3 * T) by lib_int, ← this]; lib_int⟩, hS⟩
    cases hpsi with
    | inl h => exact fin (-Int.ofNat u) (by lib_int) (by rw [h, h2u]; lib_int)
    | inr h => exact fin (Int.ofNat u) rfl (by rw [h, h2u]; lib_int)

/-- **6 | #E(𝔽_p) = p + 1 + Σ_t χ(t³ + 1)** for y² = x³ + 1 at every p ≡ 1 (mod 3). -/
theorem count_mod6 (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (N : Nat)
    (hN : 2 * (m + 1) = 3 * N) :
    ∃ k, Int.ofNat (2 * m + 1 + 1 + 1) + 1 + FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = 6 * k := by
  match eisen_exists m hpr N hN with
  | ⟨x, y, hxy, ⟨s, hs⟩, hpsi⟩ =>
    rw [hpsi]
    have hm : Int.ofNat (2 * m + 1 + 1 + 1) = 2 * Int.ofNat m + 3 := rfl
    have e3 : 2 * (Int.ofNat m + 2 + -x) = 3 * (3 * (s * s) + y * y) := by
      rw [show 2 * (Int.ofNat m + 2 + -x) = (2 * Int.ofNat m + 3) + 1 + -2 * x by lib_int, ← hm, ← hxy, hs]; lib_int
    cases FermatUnique.int_euclid 1 Eisen.prime3 2 _ _ (e3.trans rfl) with
    | inl h =>
      match h with
      | ⟨c, hc⟩ => exact absurd (hc.symm.trans rfl) (fun e => three_ne_r c 2 (fun h => Nat.noConfusion h) (Nat.lt_succ_self 2) e)
    | inr h =>
      match h with
      | ⟨c, hc⟩ =>
        exact ⟨c, by
          have : Int.ofNat m + 2 + -x = 3 * c := hc
          rw [hm, show 2 * Int.ofNat m + 3 + 1 + -2 * x = 2 * (Int.ofNat m + 2 + -x) by lib_int, this]; lib_int⟩

end EisenAp

#print axioms EisenAp.three_ne_r
#print axioms EisenAp.p_ne_three
#print axioms EisenAp.eisen_ap
#print axioms EisenAp.eisen_exists
#print axioms EisenAp.count_mod6

Since a prime is not a square, the bound is strict at split primes:

\[ 4p=a_p^2+4d^2\ (y^2=x^3-x),\qquad 4p=a_p^2+12y^2\ (y^2=x^3+1),\qquad d,y\ne 0, \]
so \(a_p^2<4p\) and the Frobenius eigenvalues are non-real of absolute value \(\sqrt p\).

Listing 80: lang/StrictHasse.lean

lang/StrictHasse.lean

/-
  lang/StrictHasse — T43, cycle 158: the strict Hasse bound a_p² < 4p for the CM curves at split primes.

    sq_nat, not_square   a prime is not a square
    pos_sq               y ≠ 0 ⇒ y² = k + 1 for some k : ℕ
    strict_gauss         y² = x³ − x, p ≡ 1 (4):  4p = a_p² + 4d², d ≠ 0       ⇒ 4p = a_p² + (n + 1)
    strict_eisen         y² = x³ + 1, p ≡ 1 (3):  4p = a_p² + 12y², y ≠ 0      ⇒ 4p = a_p² + (n + 1)
  So the Frobenius eigenvalues π, π̄ are non-real complex conjugates of absolute value √p (never ±√p): the Ramanujan
  bound for these CM forms holds strictly.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.EisenAp
import lang.GaussAp

namespace StrictHasse

open FTAChain ZMod YMChain FiniteRecChain

theorem sq_nat (x : Int) : ∃ n : Nat, x * x = Int.ofNat (n * n) := by
  cases x with
  | ofNat n => exact ⟨n, rfl⟩
  | negSucc n => exact ⟨n + 1, rfl⟩

theorem not_square (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (x : Int)
    (h : x * x = Int.ofNat (p + 1 + 1)) : False := by
  match sq_nat x with
  | ⟨n, hn⟩ =>
    have e : p + 1 + 1 = n * n := (Int.ofNat.inj (hn.symm.trans h)).symm
    cases hp n ⟨n, e⟩ with
    | inl h1 => rw [h1] at e; exact Nat.noConfusion (Nat.succ.inj e)
    | inr h1 =>
      rw [h1] at e
      have h0 : 0 = p * (p + 1 + 1) + p + 1 + 1 :=
        FermatUnique.add_cancel (p + 1 + 1) _ _ (e.trans (by lib_nat))
      exact Nat.noConfusion h0

theorem pos_sq (y : Int) (hy : y * y ≠ 0) : ∃ k : Nat, y * y = Int.ofNat (k + 1) := by
  match sq_nat y with
  | ⟨n, hn⟩ =>
    cases hnn : n * n with
    | zero => exact absurd (hn.trans (by rw [hnn]; rfl)) hy
    | succ k => exact ⟨k, by rw [hn, hnn]⟩

/-- **Strict Hasse for y² = x³ − x at p = 4j + 5.** -/
theorem strict_gauss (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * j + 1) + 1 + 1 + 1) :
    ∃ n : Nat, 4 * Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) =
      (-sumK LibInt.intR (fun x => FinField.chi (FinField.ffp (2 * j + 1) hpr) (FinSign.f (FinField.ffp (2 * j + 1) hpr) x)) (FinField.ffp (2 * j + 1) hpr).els)
      * (-sumK LibInt.intR (fun x => FinField.chi (FinField.ffp (2 * j + 1) hpr) (FinSign.f (FinField.ffp (2 * j + 1) hpr) x)) (FinField.ffp (2 * j + 1) hpr).els)
      + Int.ofNat (n + 1) := by
  match GaussAp.exists_i j hpr with
  | ⟨i, hi⟩ =>
    match FinPrimary.fin_ap_primary (FinField.ffp (2 * j + 1) hpr) (fun x => x.val) (GaussAp.val_inj (2 * j + 1)) i hi with
    | ⟨a, b, hab, _, hS⟩ =>
      have hab' : a * a + b * b = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) := by
        rw [hab]; exact congrArg Int.ofNat (show 2 * (2 * j + 1 + 1) + 1 = 2 * (2 * j + 1) + 1 + 1 + 1 by lib_nat)
      have hb : b * b ≠ 0 := fun e => not_square (2 * (2 * j + 1) + 1) hpr a (by rw [← hab', e]; lib_int)
      match pos_sq b hb with
      | ⟨k, hk⟩ =>
        refine ⟨4 * k + 3, ?_⟩
        rw [hS, ← hab', show Int.ofNat (4 * k + 3 + 1) = 4 * Int.ofNat (k + 1) from rfl, ← hk]; lib_int

/-- **Strict Hasse for y² = x³ + 1 at p ≡ 1 (mod 3).** -/
theorem strict_eisen (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (N : Nat)
    (hN : 2 * (m + 1) = 3 * N) :
    ∃ n : Nat, 4 * Int.ofNat (2 * m + 1 + 1 + 1) =
      FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one * FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one
      + Int.ofNat (n + 1) := by
  match EisenAp.eisen_exists m hpr N hN with
  | ⟨x, y, hxy, _, hpsi⟩ =>
    have hy : y * y ≠ 0 := fun e => not_square (2 * m + 1) hpr x (by rw [← hxy, e]; lib_int)
    match pos_sq y hy with
    | ⟨k, hk⟩ =>
      refine ⟨12 * k + 11, ?_⟩
      rw [hpsi, ← hxy, show Int.ofNat (12 * k + 11 + 1) = 12 * Int.ofNat (k + 1) from rfl, ← hk]; lib_int

end StrictHasse

#print axioms StrictHasse.sq_nat
#print axioms StrictHasse.not_square
#print axioms StrictHasse.pos_sq
#print axioms StrictHasse.strict_gauss
#print axioms StrictHasse.strict_eisen

An element \(\pi =a+bi\) of prime norm \(p\) is a Gaussian prime: if \(p\mid c^2+d^2\) then

\[ (ca+db)(ca-db)=a^2(c^2+d^2)-d^2p,\qquad (ca+db)^2+(da-cb)^2=p\,(c^2+d^2), \]
so \(p\) divides \(ca+db\) and \(da-cb\) (or the conjugate pair), and then \(c+di=\pi \gamma \) (or \(\bar \pi \gamma \)) with \(\gamma =\bigl ((ca+db)+(da-cb)i\bigr )/p\).

Listing 81: lang/GaussDiv.lean

lang/GaussDiv.lean

/-
  lang/GaussDiv — T43, cycle 159: π = a + bi of prime norm p is a Gaussian prime: p | N(α) ⇒ π | α or π̄ | α.

    quot_exact     p | ca + db and p | da − cb ⇒ c + di = (a + bi)(u + vi)       (multiply out, cancel p)
    sq_div         p | z² ⇒ p | z                                                 (Euclid in ℤ/p)
    gauss_div      p | c² + d² ⇒ c + di = π γ or c + di = π̄ γ, and then N(c + di) = p · N(γ)
  Proof: (ca + db)(ca − db) = a²(c² + d²) − d² p, and (ca + db)² + (da − cb)² = p (c² + d²).
  The step of unique factorisation in ℤ[i] needed to list the primary elements of norm p^k (next: a(𝔽_{p³}) = a_p³ − 3p a_p).

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.FermatUnique
import lang.GaussInt

namespace GaussDiv

open FTAChain LibInt FermatUnique GaussInt

theorem quot_exact (p : Nat) (a b c d u v : Int) (hab : a * a + b * b = Int.ofNat (p + 1 + 1))
    (hu : c * a + d * b = Int.ofNat (p + 1 + 1) * u) (hv : d * a - c * b = Int.ofNat (p + 1 + 1) * v) :
    c = a * u - b * v ∧ d = a * v + b * u := by
  constructor
  · apply cancelP p
    rw [show Int.ofNat (p + 1 + 1) * (a * u - b * v) = a * (Int.ofNat (p + 1 + 1) * u) - b * (Int.ofNat (p + 1 + 1) * v) by lib_int,
      ← hu, ← hv, ← hab]; lib_int
  · apply cancelP p
    rw [show Int.ofNat (p + 1 + 1) * (a * v + b * u) = a * (Int.ofNat (p + 1 + 1) * v) + b * (Int.ofNat (p + 1 + 1) * u) by lib_int,
      ← hu, ← hv, ← hab]; lib_int

theorem sq_div (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (z q : Int)
    (h : z * z = Int.ofNat (p + 1 + 1) * q) : ∃ u, z = Int.ofNat (p + 1 + 1) * u := by
  cases int_euclid p hp z z q h with
  | inl h => exact h
  | inr h => exact h

/-- one direction: p | ca + db ⇒ π | c + di. -/
theorem div_plus (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b c d n u : Int)
    (hab : a * a + b * b = Int.ofNat (p + 1 + 1)) (hn : c * c + d * d = Int.ofNat (p + 1 + 1) * n)
    (hu : c * a + d * b = Int.ofNat (p + 1 + 1) * u) :
    ∃ x y : Int, c = a * x - b * y ∧ d = a * y + b * x := by
  have hsq : (d * a - c * b) * (d * a - c * b) = Int.ofNat (p + 1 + 1) * (Int.ofNat (p + 1 + 1) * n + -(Int.ofNat (p + 1 + 1) * u * u)) := by
    have e : (c * a + d * b) * (c * a + d * b) + (d * a - c * b) * (d * a - c * b) = (c * c + d * d) * (a * a + b * b) := by lib_int
    rw [hu, hn, hab] at e
    rw [show (d * a - c * b) * (d * a - c * b) = (Int.ofNat (p + 1 + 1) * u * (Int.ofNat (p + 1 + 1) * u)
      + (d * a - c * b) * (d * a - c * b)) + -(Int.ofNat (p + 1 + 1) * u * (Int.ofNat (p + 1 + 1) * u)) by lib_int, e]; lib_int
  match sq_div p hp _ _ hsq with
  | ⟨v, hv⟩ =>
    match quot_exact p a b c d u v hab hu hv with
    | ⟨h1, h2⟩ => exact ⟨u, v, h1, h2⟩

/-- **π or π̄ divides every α with p | N(α).** -/
theorem gauss_div (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b c d n : Int)
    (hab : a * a + b * b = Int.ofNat (p + 1 + 1)) (hn : c * c + d * d = Int.ofNat (p + 1 + 1) * n) :
    (∃ x y : Int, c = a * x - b * y ∧ d = a * y + b * x) ∨ (∃ x y : Int, c = a * x + b * y ∧ d = a * y - b * x) := by
  have key : (c * a + d * b) * (c * a + -1 * (d * b)) = Int.ofNat (p + 1 + 1) * (a * a * n + -1 * (d * d)) := by
    have hb : b * b = Int.ofNat (p + 1 + 1) + -1 * (a * a) := by rw [← hab]; lib_int
    rw [show (c * a + d * b) * (c * a + -1 * (d * b)) = a * a * (c * c + d * d) + -1 * (d * d * (a * a + b * b)) by lib_int, hn, hab]
    lib_int
  cases int_euclid p hp _ _ _ key with
  | inl h =>
    match h with
    | ⟨u, hu⟩ => exact Or.inl (div_plus p hp a b c d n u hab hn hu)
  | inr h =>
    match h with
    | ⟨u, hu⟩ =>
      match div_plus p hp a (-b) c d n u (by rw [← hab]; lib_int) hn (by rw [← hu]; lib_int) with
      | ⟨x, y, h1, h2⟩ => exact Or.inr ⟨x, y, by rw [h1]; lib_int, by rw [h2]; lib_int⟩

end GaussDiv

#print axioms GaussDiv.quot_exact
#print axioms GaussDiv.sq_div
#print axioms GaussDiv.div_plus
#print axioms GaussDiv.gauss_div

By induction on \(k\) (divide by \(\pi \) or \(\bar \pi \), cancel \(p\) in the norm, and \(N(\alpha )=1\) only for units),

\[ N(\alpha )=p^k\ \Longrightarrow \ \alpha =u\,\pi ^j\bar \pi ^{\,l},\qquad j+l=k,\quad u\in \{\pm 1,\pm i\}. \]

Listing 82: lang/GaussFactor.lean

lang/GaussFactor.lean

/-
  lang/GaussFactor — T43, cycle 160: unique factorisation in ℤ[i] above a split p.

  π = a + bi with a² + b² = p prime. Every α ∈ ℤ[i] with N(α) = p^k is u · π^j · π̄^l, j + l = k, u ∈ {±1, ±i}:
    gi_ext, gmul_comm, gmul_assoc, gpow     ℤ[i] arithmetic on GaussInt.GI
    norm_one                                N(α) = 1 ⇒ α a unit
    factor                                  the classification, by induction on k with GaussDiv.gauss_div
  The primary elements of norm p^k are therefore among the π^j π̄^(k−j) up to units — the input for a(𝔽_{p^k}).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.GaussDiv
import lang.StrictHasse

namespace GaussFactor

open FTAChain LibInt FermatUnique GaussInt

theorem gi_ext (x y : GI) (h1 : x.re = y.re) (h2 : x.im = y.im) : x = y := by
  cases x; cases y; cases h1; cases h2; rfl

theorem gmul_comm (x y : GI) : gmul x y = gmul y x :=
  gi_ext _ _ (by show x.re * y.re - x.im * y.im = y.re * x.re - y.im * x.im; lib_int)
    (by show x.re * y.im + x.im * y.re = y.re * x.im + y.im * x.re; lib_int)

theorem gmul_assoc (x y z : GI) : gmul (gmul x y) z = gmul x (gmul y z) :=
  gi_ext _ _ (by show (x.re * y.re - x.im * y.im) * z.re - (x.re * y.im + x.im * y.re) * z.im
      = x.re * (y.re * z.re - y.im * z.im) - x.im * (y.re * z.im + y.im * z.re); lib_int)
    (by show (x.re * y.re - x.im * y.im) * z.im + (x.re * y.im + x.im * y.re) * z.re
      = x.re * (y.re * z.im + y.im * z.re) + x.im * (y.re * z.re - y.im * z.im); lib_int)

def gone : GI := ⟨1, 0⟩

def gpow (x : GI) : Nat → GI
  | 0 => gone
  | n + 1 => gmul (gpow x n) x

def ipow (P : Int) : Nat → Int
  | 0 => 1
  | n + 1 => ipow P n * P

def IsUnit (u : GI) : Prop := u = ⟨1, 0⟩ ∨ u = ⟨-1, 0⟩ ∨ u = ⟨0, 1⟩ ∨ u = ⟨0, -1⟩

theorem nat_sq_one (n m : Nat) (h : n * n + m * m = 1) : (n = 1 ∧ m = 0) ∨ (n = 0 ∧ m = 1) := by
  match n, m with
  | 0, 0 => exact Nat.noConfusion h
  | 1, 0 => exact Or.inl ⟨rfl, rfl⟩
  | 0, 1 => exact Or.inr ⟨rfl, rfl⟩
  | 0, k + 2 => exact Nat.noConfusion (Nat.succ.inj ((show 0 * 0 + (k + 2) * (k + 2) = (k * k + 4 * k + 2) + 1 + 1 by lib_nat).symm.trans h))
  | k + 2, m => exact Nat.noConfusion (Nat.succ.inj ((show (k + 2) * (k + 2) + m * m = (k * k + 4 * k + 2 + m * m) + 1 + 1 by lib_nat).symm.trans h))
  | 1, k + 1 => exact Nat.noConfusion (Nat.succ.inj ((show 1 * 1 + (k + 1) * (k + 1) = (k * k + 2 * k) + 1 + 1 by lib_nat).symm.trans h))

theorem norm_one (x : GI) (h : gnorm x = 1) : IsUnit x := by
  match StrictHasse.sq_nat x.re, StrictHasse.sq_nat x.im with
  | ⟨n, hn⟩, ⟨m, hm⟩ =>
    have e : n * n + m * m = 1 := Int.ofNat.inj (show Int.ofNat (n * n) + Int.ofNat (m * m) = 1 by rw [← hn, ← hm]; exact h)
    cases nat_sq_one n m e with
    | inl h1 =>
      have hi : x.im = 0 := HasseDavenport.sq_zero _ (by rw [hm, h1.2]; rfl)
      cases QuarticFamily.sq_eq x.re 1 (by rw [hn, h1.1]; rfl) with
      | inl q => exact Or.inl (gi_ext _ _ q hi)
      | inr q => exact Or.inr (Or.inl (gi_ext _ _ q hi))
    | inr h1 =>
      have hr : x.re = 0 := HasseDavenport.sq_zero _ (by rw [hn, h1.1]; rfl)
      cases QuarticFamily.sq_eq x.im 1 (by rw [hm, h1.2]; rfl) with
      | inl q => exact Or.inr (Or.inr (Or.inl (gi_ext _ _ hr q)))
      | inr q => exact Or.inr (Or.inr (Or.inr (gi_ext _ _ hr q)))

theorem gone_mul (x : GI) : gmul gone x = x :=
  gi_ext _ _ (by show 1 * x.re - 0 * x.im = x.re; lib_int) (by show 1 * x.im + 0 * x.re = x.im; lib_int)

/-- **Factorisation above p.** -/
theorem factor (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b : Int)
    (hab : a * a + b * b = Int.ofNat (p + 1 + 1)) : ∀ (k : Nat) (x : GI), gnorm x = ipow (Int.ofNat (p + 1 + 1)) k →
    ∃ u j l, IsUnit u ∧ j + l = k ∧ x = gmul u (gmul (gpow ⟨a, b⟩ j) (gpow ⟨a, -b⟩ l))
  | 0, x, h => ⟨x, 0, 0, norm_one x h, rfl, by
      show x = gmul x (gmul gone gone)
      rw [gone_mul, gmul_comm, gone_mul]⟩
  | k + 1, x, h => by
    have hn : x.re * x.re + x.im * x.im = Int.ofNat (p + 1 + 1) * ipow (Int.ofNat (p + 1 + 1)) k := by
      rw [show Int.ofNat (p + 1 + 1) * ipow (Int.ofNat (p + 1 + 1)) k = ipow (Int.ofNat (p + 1 + 1)) (k + 1) by
        show _ = ipow _ k * _; lib_int]; exact h
    have normq : ∀ (q : GI) (x' y' : Int), gnorm q = Int.ofNat (p + 1 + 1) → x = gmul q ⟨x', y'⟩ →
        gnorm ⟨x', y'⟩ = ipow (Int.ofNat (p + 1 + 1)) k := fun q x' y' hq e => by
      apply cancelP p
      rw [← hn, ← hq, ← norm_mul, ← e]; rfl
    cases GaussDiv.gauss_div p hp a b x.re x.im _ hab hn with
    | inl h1 =>
      match h1 with
      | ⟨x', y', e1, e2⟩ =>
        have ex : x = gmul ⟨a, b⟩ ⟨x', y'⟩ := gi_ext _ _ e1 e2
        match factor p hp a b hab k ⟨x', y'⟩ (normq ⟨a, b⟩ x' y' hab ex) with
        | ⟨u, j, l, hu, hjl, hg⟩ =>
          refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
          rw [ex, hg]
          show gmul ⟨a, b⟩ (gmul u (gmul (gpow ⟨a, b⟩ j) (gpow ⟨a, -b⟩ l))) = gmul u (gmul (gmul (gpow ⟨a, b⟩ j) ⟨a, b⟩) (gpow ⟨a, -b⟩ l))
          rw [← gmul_assoc, gmul_comm ⟨a, b⟩ u, gmul_assoc, gmul_assoc, ← gmul_assoc ⟨a, b⟩, gmul_comm ⟨a, b⟩ (gpow ⟨a, b⟩ j), gmul_assoc]
    | inr h1 =>
      match h1 with
      | ⟨x', y', e1, e2⟩ =>
        have ex : x = gmul ⟨a, -b⟩ ⟨x', y'⟩ :=
          gi_ext _ _ (by rw [e1]; show _ = a * x' - -b * y'; lib_int) (by rw [e2]; show _ = a * y' + -b * x'; lib_int)
        match factor p hp a b hab k ⟨x', y'⟩ (normq _ x' y' (by rw [← hab]; show a * a + -b * -b = _; lib_int) ex) with
        | ⟨u, j, l, hu, hjl, hg⟩ =>
          refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
          rw [ex, hg]
          show gmul ⟨a, -b⟩ (gmul u (gmul (gpow ⟨a, b⟩ j) (gpow ⟨a, -b⟩ l))) = gmul u (gmul (gpow ⟨a, b⟩ j) (gmul (gpow ⟨a, -b⟩ l) ⟨a, -b⟩))
          rw [← gmul_assoc, gmul_comm ⟨a, -b⟩ u, gmul_assoc, ← gmul_assoc ⟨a, -b⟩, gmul_comm ⟨a, -b⟩ (gpow ⟨a, b⟩ j),
            gmul_assoc, gmul_comm ⟨a, -b⟩ (gpow ⟨a, -b⟩ l)]

end GaussFactor

#print axioms GaussFactor.gi_ext
#print axioms GaussFactor.gmul_comm
#print axioms GaussFactor.gmul_assoc
#print axioms GaussFactor.gone
#print axioms GaussFactor.gpow
#print axioms GaussFactor.ipow
#print axioms GaussFactor.nat_sq_one
#print axioms GaussFactor.norm_one
#print axioms GaussFactor.gone_mul
#print axioms GaussFactor.factor

For norm \(p^3\): \(\pi \bar \pi ^2=p\bar \pi \) and \(\pi ^2\bar \pi =p\pi \) have real part divisible by \(p\), and a unit times a primary element is primary only for the unit \(1\). So a primary \(\alpha \) with \(N(\alpha )=p^3\) and \(p\nmid \mathrm {Re}\,\alpha \) has

\[ \mathrm {Re}\,\alpha =\mathrm {Re}\,\pi ^3=a^3-3ab^2 . \]

Listing 83: lang/GaussCube.lean

lang/GaussCube.lean

/-
  lang/GaussCube — T43, cycle 161: the primary elements of norm p³ prime to p are π³ and π̄³.

  π = a + bi primary, a² + b² = p prime. If α = A + Bi is primary, N(α) = p³ and p ∤ A, then A = a³ − 3ab² = Re π³:
    unit_primary     u unit, z and u z primary ⇒ u = 1
    prim_pow         π^j primary
    cube_primary     the statement — GaussFactor gives α = u π^j π̄^(3−j); j = 1, 2 give p | A; j = 0, 3 give u = 1.
  With a(𝔽_{p³}) = 2A (CubeTrace) and p ∤ a(𝔽_{p³}) (ordinarity), this is a(𝔽_{p³}) = 2 Re π³ = a_p³ − 3p a_p.

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.GaussFactor
import lang.PrimaryUnique

namespace GaussCube

open FTAChain LibInt LibGcd FermatUnique GaussInt GaussFactor

theorem unit_primary (u z : GI) (hu : IsUnit u) (hz : Primary z) (huz : Primary (gmul u z)) : u = gone := by
  match hz, huz with
  | ⟨⟨e, he⟩, ⟨t, ht⟩⟩, ⟨⟨e', he'⟩, ⟨t', ht'⟩⟩ =>
    have hre : z.re = 2 * (2 * t - e) + 1 := by rw [show 2 * (2 * t - e) + 1 = 4 * t + 1 - 2 * e by lib_int, ← ht, he]; lib_int
    rcases hu with h | h | h | h
    · exact h
    · exfalso; subst h
      have h1 : -1 * z.re - 0 * z.im + (-1 * z.im + 0 * z.re) - 1 = 4 * t' := ht'
      apply PrimaryUnique.four_bad (-(t + t'))
      rw [show 4 * -(t + t') = -(4 * t) + -(4 * t') by lib_int, ← ht, ← h1]; lib_int
    · exfalso; subst h
      have h1 : 0 * z.im + 1 * z.re = 2 * e' := he'
      exact int_odd_ne_even _ e' (by rw [← hre, ← h1]; lib_int)
    · exfalso; subst h
      have h1 : 0 * z.im + -1 * z.re = 2 * e' := he'
      exact int_odd_ne_even _ (-e') (by rw [← hre, show 2 * -e' = -(2 * e') by lib_int, ← h1]; lib_int)

theorem prim_one : Primary gone := ⟨⟨0, rfl⟩, ⟨0, rfl⟩⟩

theorem prim_pow (x : GI) (hx : Primary x) : ∀ j, Primary (gpow x j)
  | 0 => prim_one
  | j + 1 => prim_mul _ _ (prim_pow x hx j) hx

theorem cube_re (x : GI) : (gpow x 3).re = x.re * x.re * x.re - 3 * (x.re * (x.im * x.im)) := by
  show ((1 * x.re - 0 * x.im) * x.re - (1 * x.im + 0 * x.re) * x.im) * x.re
    - ((1 * x.re - 0 * x.im) * x.im + (1 * x.im + 0 * x.re) * x.re) * x.im = _
  lib_int

theorem one_two (a b : Int) : gmul (gpow ⟨a, b⟩ 1) (gpow ⟨a, -b⟩ 2) = ⟨(a * a + b * b) * a, -((a * a + b * b) * b)⟩ := by
  apply gi_ext
  · show (1 * a - 0 * b) * ((1 * a - 0 * -b) * a - (1 * -b + 0 * a) * -b) - (1 * b + 0 * a) * ((1 * a - 0 * -b) * -b + (1 * -b + 0 * a) * a) = (a * a + b * b) * a
    lib_int
  · show (1 * a - 0 * b) * ((1 * a - 0 * -b) * -b + (1 * -b + 0 * a) * a) + (1 * b + 0 * a) * ((1 * a - 0 * -b) * a - (1 * -b + 0 * a) * -b) = -((a * a + b * b) * b)
    lib_int

theorem two_one (a b : Int) : gmul (gpow ⟨a, b⟩ 2) (gpow ⟨a, -b⟩ 1) = ⟨(a * a + b * b) * a, (a * a + b * b) * b⟩ := by
  apply gi_ext
  · show ((1 * a - 0 * b) * a - (1 * b + 0 * a) * b) * (1 * a - 0 * -b) - ((1 * a - 0 * b) * b + (1 * b + 0 * a) * a) * (1 * -b + 0 * a) = (a * a + b * b) * a
    lib_int
  · show ((1 * a - 0 * b) * a - (1 * b + 0 * a) * b) * (1 * -b + 0 * a) + ((1 * a - 0 * b) * b + (1 * b + 0 * a) * a) * (1 * a - 0 * -b) = (a * a + b * b) * b
    lib_int

/-- **Primary α of norm p³ with p ∤ Re α has Re α = Re π³.** -/
theorem cube_primary (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b : Int)
    (hab : a * a + b * b = Int.ofNat (p + 1 + 1)) (hπ : Primary ⟨a, b⟩) (x : GI) (hx : Primary x)
    (hN : gnorm x = ipow (Int.ofNat (p + 1 + 1)) 3) (hA : ¬ ∃ w, x.re = Int.ofNat (p + 1 + 1) * w) :
    x.re = a * a * a - 3 * (a * (b * b)) := by
  match factor p hp a b hab 3 x hN with
  | ⟨u, j, l, hu, hjl, hx'⟩ =>
    match j with
    | 0 =>
      have hl : l = 3 := (Nat.zero_add l).symm.trans hjl
      subst hl
      have hc : Primary (gmul (gpow ⟨a, b⟩ 0) (gpow ⟨a, -b⟩ 3)) :=
        prim_mul _ _ prim_one (prim_pow _ (prim_conj ⟨a, b⟩ hπ) 3)
      have h1 := unit_primary u _ hu hc (by rw [← hx']; exact hx)
      rw [hx', h1, gone_mul, show gpow ⟨a, b⟩ 0 = gone from rfl, gone_mul, cube_re]
      show a * a * a - 3 * (a * (-b * -b)) = _; lib_int
    | 3 =>
      have hl : l = 0 := add_cancel 3 l 0 (hjl.trans rfl)
      subst hl
      have hc : Primary (gmul (gpow ⟨a, b⟩ 3) (gpow ⟨a, -b⟩ 0)) := prim_mul _ _ (prim_pow _ hπ 3) prim_one
      have h1 := unit_primary u _ hu hc (by rw [← hx']; exact hx)
      rw [hx', h1, gone_mul, show gpow ⟨a, -b⟩ 0 = gone from rfl, gmul_comm, gone_mul, cube_re]
    | 1 =>
      have hl : l = 2 := Nat.succ.inj ((Nat.add_comm l 1).trans hjl)
      subst hl
      exfalso; apply hA
      refine ⟨u.re * a + u.im * b, ?_⟩
      rw [hx', one_two, ← hab]
      show u.re * ((a * a + b * b) * a) - u.im * -((a * a + b * b) * b) = _; lib_int
    | 2 =>
      have hl : l = 1 := Nat.succ.inj (Nat.succ.inj ((show l + 1 + 1 = 2 + l by lib_nat).trans hjl))
      subst hl
      exfalso; apply hA
      refine ⟨u.re * a - u.im * b, ?_⟩
      rw [hx', two_one, ← hab]
      show u.re * ((a * a + b * b) * a) - u.im * ((a * a + b * b) * b) = _; lib_int
    | j + 4 =>
      exfalso
      exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show j + l + 1 + 1 + 1 + 1 = j + 4 + l by lib_nat).trans hjl))))

end GaussCube

#print axioms GaussCube.unit_primary
#print axioms GaussCube.prim_one
#print axioms GaussCube.prim_pow
#print axioms GaussCube.cube_re
#print axioms GaussCube.one_two
#print axioms GaussCube.two_one
#print axioms GaussCube.cube_primary

Over \(\mathbb F_{p^3}\) (\(p=4k+5\equiv 1\pmod {12}\)) the Hasse invariant gives the congruence: with \(h=(p^3-1)/2=a+(a+ap)p\), \(h/2=c+(c+cp)p\) (\(a=2k+2\), \(c=k+1\)), Lucas’ theorem twice gives \(\binom {h}{h/2}\equiv \binom {a}{c}^3\), hence

\[ \sum _{z\in \mathbb F_{p^3}}\chi (z^3-z)\equiv \Bigl (\sum _{x\in \mathbb F_p}\chi (x^3-x)\Bigr )^3\pmod p,\qquad a(\mathbb F_{p^3})\equiv a_p^3\pmod p . \]

Listing 84: lang/CubeHasse.lean

lang/CubeHasse.lean

/-
  lang/CubeHasse — T43, cycle 162: a(𝔽_{p³}) ≡ a_p³ (mod p) for y² = x³ − x at p ≡ 1 (mod 12).

  S₁ = Σ_{𝔽_p} χ(x³ − x), S₃ = Σ_{𝔽_{p³}} χ(z³ − z), 𝔽_{p³} = 𝔽_p[∛c] (CubicInst.ffc), p = 4k + 5.
    castN_cubic, castZ_cubic   ℤ → 𝔽_{p³} lands in 𝔽_p
    hF3, idx…                  h = (p³ − 1)/2 = a + (a + a p) p,  h/2 = c + (c + c p) p,  a = 2k + 2, c = k + 1
    cast_S3                    S₃ ≡ −C(a, c)³ (−1)^(k+1)               (FinHasse over 𝔽_{p³}, Lucas twice)
    cube_hasse                 S₃ ≡ S₁³ (mod p)                          (HasseCM over 𝔽_p)
  With a = −S: a(𝔽_{p³}) ≡ a_p³ (mod p), the shadow of a(𝔽_{p³}) = a_p³ − 3p a_p; since p ∤ a_p, p ∤ a(𝔽_{p³}).

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.SplitHasse
import lang.CubeTrace

namespace CubeHasse

open FTAChain ZMod YMChain FiniteRecChain LibRing Frobenius

section Cub
variable {S : Type} (K : DetChain.CRing S) (c : S)

theorem castN_cubic : ∀ n, castN (Cubic.cubic K c) n = (castN K n, K.zero, K.zero)
  | 0 => rfl
  | n + 1 => by
    show (Cubic.cubic K c).add (Cubic.cubic K c).one (castN (Cubic.cubic K c) n) = (K.add K.one (castN K n), K.zero, K.zero)
    rw [castN_cubic n]
    apply Prod.ext
    · rfl
    · apply Prod.ext
      · show K.add K.zero K.zero = K.zero; exact K.zero_add _
      · show K.add K.zero K.zero = K.zero; exact K.zero_add _

theorem castZ_cubic (z : Int) : castZ (Cubic.cubic K c) z = (castZ K z, K.zero, K.zero) := by
  rw [IntCast.castZ_repr, IntCast.castZ_repr K z, castN_cubic, castN_cubic]
  apply Prod.ext
  · rfl
  · apply Prod.ext
    · show K.add K.zero (K.neg K.zero) = K.zero; lib_ring
    · show K.add K.zero (K.neg K.zero) = K.zero; lib_ring

end Cub

theorem negone_odd_mul {R : Type} (K : DetChain.CRing R) (s t : Nat) :
    Frobenius.pw K (K.neg K.one) (2 * s + t) = Frobenius.pw K (K.neg K.one) t := by
  rw [HasseCM.pw_add', SplitHasse.negone_even, K.one_mul]

theorem hF3 (k : Nat) : 4 * (2 * k + 1) * (2 * k + 1) * (2 * k + 1) + 18 * (2 * k + 1) * (2 * k + 1) + 27 * (2 * k + 1) + 12 + 1 = 2 * (16 * k * k * k + 60 * k * k + 75 * k + 30) + 2 := by lib_nat
theorem idx3a (k : Nat) : 2 * (16 * k * k * k + 60 * k * k + 75 * k + 30) + 2 = (2 * k + 2) + ((2 * k + 2) + (2 * k + 2) * (2 * (2 * k + 1) + 1 + 1 + 1)) * (2 * (2 * k + 1) + 1 + 1 + 1) := by lib_nat
theorem idx3c (k : Nat) : 16 * k * k * k + 60 * k * k + 75 * k + 30 + 1 = ((k + 1) * (2 * (2 * k + 1) + 1 + 1 + 1) + (k + 1)) * (2 * (2 * k + 1) + 1 + 1 + 1) + (k + 1) := by lib_nat
theorem idx3e (k : Nat) : 16 * k * k * k + 60 * k * k + 75 * k + 30 + 1 = 2 * (8 * k * k * k + 30 * k * k + 37 * k + 15) + (k + 1) := by lib_nat

variable (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * k + 1) + 1 + 1 + 1)) (c : Zp (2 * (2 * k + 1) + 1 + 1)) (m : Nat)
  (hJ : 2 * (2 * k + 1) + 1 + 1 = 3 * m) (hc0 : c ≠ (ring (2 * (2 * k + 1) + 1 + 1)).zero) (hω : Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) c m ≠ (ring (2 * (2 * k + 1) + 1 + 1)).one)

theorem nsm_prod {R : Type} (K : DetChain.CRing R) (x y : Nat) : nsm K (x * y) K.one = K.mul (nsm K x K.one) (nsm K y K.one) := by
  rw [Frobenius.nsm_mul_nat, Frobenius.nsm_eq_mul, K.mul_comm]

theorem neg_one_cubic {S : Type} (K : DetChain.CRing S) (c : S) :
    (Cubic.cubic K c).neg (Cubic.cubic K c).one = (K.neg K.one, K.zero, K.zero) :=
  Prod.ext rfl (Prod.ext (DetChain.neg_unique K _ _ (K.zero_add _)).symm (DetChain.neg_unique K _ _ (K.zero_add _)).symm)

/-- **S₃ ≡ −C(2k + 2, k + 1)³ (−1)^(k+1) in 𝔽_p.** -/
theorem cast_S3 :
    castZ (ring (2 * (2 * k + 1) + 1 + 1)) (sumK LibInt.intR (fun x => FinField.chi (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) (FinSign.f (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) x)) (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω).els) = (ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).mul (nsm   -- (+465 chars; full line in the Zenodo source)
  have hk : (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω).h = 2 * (16 * k * k * k + 60 * k * k + 75 * k + 30) + 2 := hF3 k
  have e1 := FinCast.cast_sum_ff (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) (FinSign.f (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω))
  rw [hk] at e1
  have e2 := e1.trans (FinHasse.fin_hasse (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) (16 * k * k * k + 60 * k * k + 75 * k + 30) hk)
  have proj : ∀ (Cn e : Nat), castZ (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω).K (sumK LibInt.intR (fun x => FinField.chi (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) (FinSign.f (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) x)) (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω).els) = (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω).K.neg (nsm (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω).K Cn (Froben  -- (+166 chars; full line in the Zenodo source)
      castZ (ring (2 * (2 * k + 1) + 1 + 1)) (sumK LibInt.intR (fun x => FinField.chi (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) (FinSign.f (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) x)) (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω).els) = (ring (2 * (2 * k + 1) + 1 + 1)).neg (nsm (ring (2 * (2 * k + 1) + 1 + 1)) Cn (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 +  -- (+70 chars; full line in the Zenodo source)
    have h3 := congrArg Prod.fst h
    rw [show castZ (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω).K (sumK LibInt.intR (fun x => FinField.chi (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) (FinSign.f (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) x)) (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω).els) = castZ (Cubic.cubic (ring (2 * (2 * k + 1) + 1 + 1)) c) (sumK LibInt.intR (fun x => FinField.chi (CubicInst.ffc (2 * k + 1) c m hpr h  -- (+150 chars; full line in the Zenodo source)
    refine h3.trans ?_
    show ((Cubic.cubic (ring (2 * (2 * k + 1) + 1 + 1)) c).neg (nsm (Cubic.cubic (ring (2 * (2 * k + 1) + 1 + 1)) c) Cn (Frobenius.pw (Cubic.cubic (ring (2 * (2 * k + 1) + 1 + 1)) c) ((Cubic.cubic (ring (2 * (2 * k + 1) + 1 + 1)) c).neg (Cubic.cubic (ring (2 * (2 * k + 1) + 1 + 1)) c).one) e))).1 = _
    rw [neg_one_cubic, CubicFrob.pw_embed3, CubicFrob.nsm_cubic]
    rfl
  have e4 := proj _ _ e2
  rw [e4, Frobenius.nsm_eq_mul, idx3e k, negone_odd_mul, idx3a k, ← idx3e k, idx3c k,
    Lucas.lucas (ring (2 * (2 * k + 1) + 1 + 1)) (2 * (2 * k + 1) + 1) hpr (ZMod.char_p _) (2 * k + 2) (k + 1) (SplitHasse.lt_p k) (SplitHasse.lt_p' k),
    nsm_prod,
    Lucas.lucas (ring (2 * (2 * k + 1) + 1 + 1)) (2 * (2 * k + 1) + 1) hpr (ZMod.char_p _) (2 * k + 2) (k + 1) (SplitHasse.lt_p k) (SplitHasse.lt_p' k),
    nsm_prod]

/-- **S₃ ≡ S₁³ (mod p)**, i.e. a(𝔽_{p³}) ≡ a_p³. -/
theorem cube_hasse :
    castZ (ring (2 * (2 * k + 1) + 1 + 1)) (sumK LibInt.intR (fun x => FinField.chi (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) (FinSign.f (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) x)) (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω).els) = (ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).mul (castZ (ring (2 * (2 * k + 1) + 1 + 1)) (sumK LibInt.intR (fun x => QChar.chi (2   -- (+426 chars; full line in the Zenodo source)
  rw [cast_S3, SplitHasse.cast_S1 k hpr, Frobenius.nsm_eq_mul (ring (2 * (2 * k + 1) + 1 + 1)) (Frobenius.C (2 * k + 2) (k + 1)) (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1))]
  have hs : (ring (2 * (2 * k + 1) + 1 + 1)).mul (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1)) (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1)) = (ring (2 * (2 * k + 1) + 1 + 1)).one := by
    rw [← Frobenius.pw_mul, show (ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) = (ring (2 * (2 * k + 1) + 1 + 1)).one by lib_ring, TwoPower.pwone]
  generalize (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1)) = Q at hs ⊢
  generalize (nsm (ring (2 * (2 * k + 1) + 1 + 1)) (Frobenius.C (2 * k + 2) (k + 1)) (ring (2 * (2 * k + 1) + 1 + 1)).one) = M
  rw [show (ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).mul M Q)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).mul M Q))) ((ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).mul M Q))
      = (ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).mul M M) M) Q) ((ring (2 * (2 * k + 1) + 1 + 1)).mul Q Q)) by lib_ring, hs]
  lib_ring

end CubeHasse

#print axioms CubeHasse.castN_cubic
#print axioms CubeHasse.castZ_cubic
#print axioms CubeHasse.negone_odd_mul
#print axioms CubeHasse.hF3
#print axioms CubeHasse.idx3a
#print axioms CubeHasse.idx3c
#print axioms CubeHasse.idx3e
#print axioms CubeHasse.nsm_prod
#print axioms CubeHasse.neg_one_cubic
#print axioms CubeHasse.cast_S3
#print axioms CubeHasse.cube_hasse

Putting the pieces together at \(p\equiv 1\pmod {12}\): \(a(\mathbb F_{p^3})=2A\) with \(A+Bi\) primary of norm \(p^3\); if \(p\mid A\) the congruence forces \(p\mid a_p=2a\), impossible for \(0<a^2<p\); so \(A=\mathrm {Re}\,\pi ^3\) and

\[ a(\mathbb F_{p^3})=a_p^3-3p\,a_p=\alpha ^3+\beta ^3,\qquad \alpha +\beta =a_p,\ \alpha \beta =p . \]

Listing 85: lang/CubeAp.lean

lang/CubeAp.lean

/-
  lang/CubeAp — T43, cycle 163: a(𝔽_{p³}) = a_p³ − 3p a_p for y² = x³ − x at every p ≡ 1 (mod 12).

  S₁ = Σ_{𝔽_p} χ(x³ − x) = −a_p = −2a (SignCM3, a + bi primary of norm p), S₃ = Σ_{𝔽_{p³}} χ(z³ − z) = −2A (CubeTrace,
  A + Bi primary of norm p³). CubeHasse: S₃ ≡ S₁³ (mod p). If p | A then p | S₁, so p | a, impossible for 0 < a² < p.
  GaussCube then gives A = a³ − 3ab² = Re π³, i.e.
    cube_ap      a(𝔽_{p³}) = a_p³ − 3p a_p
  the third coefficient of the local zeta function: with α + β = a_p, αβ = p, a(𝔽_{p³}) = α³ + β³ — the Hecke L-factor of
  ψ at p governs the point count over 𝔽_{p³} as well.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.CubeHasse
import lang.GaussCube
import lang.SignCM3
import lang.GaussAp
import lang.Eisen

namespace CubeAp

open FTAChain ZMod YMChain FiniteRecChain LibRing GaussInt GaussFactor

theorem castP (p : Nat) : castZ (ring (p + 1)) (Int.ofNat (p + 1 + 1)) = (ring (p + 1)).zero := by
  show castN (ring (p + 1)) (p + 1 + 1) = _
  rw [IntCast.castN_mk]; exact Subtype.ext (ZMod.dv_md_zero (p + 1) _ ⟨1, by lib_nat⟩)

theorem castTwo {R : Type} (K : DetChain.CRing R) : castZ K 2 = K.add K.one K.one := by
  show K.add K.one (K.add K.one K.zero) = _
  rw [DetChain.add_zero']

/-- a² + b² = p with a = p q forces q = 0. -/
theorem small (p : Nat) (a b q : Int) (hab : a * a + b * b = Int.ofNat (p + 1 + 1)) (hq : a = Int.ofNat (p + 1 + 1) * q) :
    q * q = 0 := by
  match StrictHasse.sq_nat q, StrictHasse.sq_nat b with
  | ⟨nq, hnq⟩, ⟨nb, hnb⟩ =>
    have e : (p + 1 + 1) * (p + 1 + 1) * (nq * nq) + nb * nb = p + 1 + 1 := Int.ofNat.inj (by
      show Int.ofNat (p + 1 + 1) * Int.ofNat (p + 1 + 1) * Int.ofNat (nq * nq) + Int.ofNat (nb * nb) = Int.ofNat (p + 1 + 1)
      have hab' := hab
      rw [hq] at hab'
      rw [← hnq, ← hnb]
      exact (show Int.ofNat (p + 1 + 1) * Int.ofNat (p + 1 + 1) * (q * q) + b * b = Int.ofNat (p + 1 + 1) * q * (Int.ofNat (p + 1 + 1) * q) + b * b by lib_int).trans hab')
    cases hn : nq * nq with
    | zero => rw [hnq, hn]; rfl
    | succ n =>
      rw [hn] at e
      exfalso
      have e2 := FermatUnique.add_cancel (p + 1 + 1) _ 0 ((show (p + 1 + 1) + ((p * p + 3 * p + 1) + 1 + (p + 1 + 1) * (p + 1 + 1) * n + nb * nb)
        = (p + 1 + 1) * (p + 1 + 1) * (n + 1) + nb * nb by lib_nat).trans e)
      exact Nat.noConfusion ((show (p * p + 3 * p + 1) + 1 + (p + 1 + 1) * (p + 1 + 1) * n + nb * nb
        = (p * p + 3 * p + 1 + (p + 1 + 1) * (p + 1 + 1) * n + nb * nb) + 1 by lib_nat).symm.trans e2)

/-- **a(𝔽_{p³}) = a_p³ − 3p a_p** (p = 4k + 5 ≡ 1 mod 12, 𝔽_{p³} = 𝔽_p[∛c]). -/
theorem cube_ap (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * k + 1) + 1 + 1 + 1)) (c : Zp (2 * (2 * k + 1) + 1 + 1)) (m : Nat)
    (hJ : 2 * (2 * k + 1) + 1 + 1 = 3 * m) (hc0 : c ≠ (ring (2 * (2 * k + 1) + 1 + 1)).zero) (hω : Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) c m ≠ (ring (2 * (2 * k + 1) + 1 + 1)).one) :
    -(sumK LibInt.intR (fun x => FinField.chi (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) (FinSign.f (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) x)) (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω).els) = (-(sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1)))) * (-(sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cu  -- (+379 chars; full line in the Zenodo source)
  match GaussAp.exists_i k hpr with
  | ⟨i, hi⟩ =>
  match CubeTrace.cube_trace (2 * k + 1) c m hpr hJ hc0 hω i hi, SignCM3.ap_primary k hpr with
  | ⟨A, B, hAB, hPA, hS3⟩, ⟨a, b, hab, hPa, hS1⟩ =>
    have hc := CubeHasse.cube_hasse k hpr c m hJ hc0 hω
    generalize (sumK LibInt.intR (fun x => FinField.chi (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) (FinSign.f (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω) x)) (CubicInst.ffc (2 * k + 1) c m hpr hJ hc0 hω).els) = s3 at hS3 hc ⊢
    generalize (sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1))) = s1 at hS1 hc ⊢
    have hA : ¬ ∃ w, A = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * w := fun hex => match hex with
      | ⟨w, hw⟩ => by
        have h3 : castZ (ring (2 * (2 * k + 1) + 1 + 1)) s3 = (ring (2 * (2 * k + 1) + 1 + 1)).zero := by
          rw [show s3 = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * (-2 * w) by rw [show s3 = -(-s3) by lib_int, hS3, hw]; lib_int,
            HasseDavenport.castZ_mul, castP, DetChain.zero_mul']
        rw [h3] at hc
        have h1 : castZ (ring (2 * (2 * k + 1) + 1 + 1)) s1 = (ring (2 * (2 * k + 1) + 1 + 1)).zero := by
          cases ZMod.nozd (2 * (2 * k + 1) + 1) hpr _ _ hc.symm with
          | inl h => cases ZMod.nozd (2 * (2 * k + 1) + 1) hpr _ _ h with
            | inl h' => exact h'
            | inr h' => exact h'
          | inr h => exact h
        have ha : castZ (ring (2 * (2 * k + 1) + 1 + 1)) a = (ring (2 * (2 * k + 1) + 1 + 1)).zero := by
          rw [show s1 = 2 * -a by rw [show s1 = -(-s1) by lib_int, hS1]; lib_int, HasseDavenport.castZ_mul, castTwo,
            HasseDavenport.castZ_neg] at h1
          cases ZMod.nozd (2 * (2 * k + 1) + 1) hpr _ _ h1 with
          | inl h => exact absurd h (Eisen.two_ne_zero' (2 * (2 * k + 1) + 1) (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le _))))
          | inr h =>
            rw [← DetChain.neg_neg' (ring (2 * (2 * k + 1) + 1 + 1)) (castZ (ring (2 * (2 * k + 1) + 1 + 1)) a), h]
            exact (DetChain.neg_unique (ring (2 * (2 * k + 1) + 1 + 1)) _ _ ((ring (2 * (2 * k + 1) + 1 + 1)).zero_add _)).symm
        match HasseDavenport.castZ_zero_dvd _ a ha with
        | ⟨q, hq⟩ =>
          have q0 : q = 0 := HasseDavenport.sq_zero q (small (2 * (2 * k + 1) + 1) a b q hab hq)
          rw [q0] at hq
          match hPa with
          | ⟨⟨e, he⟩, ⟨t, ht⟩⟩ =>
            have he' : b = 2 * e := he
            have ht' : a + b - 1 = 4 * t := ht
            exact GaussInt.two_ne_one (e - 2 * t) (by rw [show 2 * (e - 2 * t) = 2 * e - 4 * t by lib_int, ← he', ← ht', hq]; lib_int)
    have hN : gnorm ⟨A, B⟩ = ipow (Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1)) 3 := by
      show A * A + B * B = 1 * Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1)
      rw [hAB]; show Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * (Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1)) = _; lib_int
    have hre := GaussCube.cube_primary (2 * (2 * k + 1) + 1) hpr a b hab hPa ⟨A, B⟩ hPA hN hA
    have hre' : A = a * a * a - 3 * (a * (b * b)) := hre
    rw [hS3, hS1, hre', ← hab]; lib_int

end CubeAp

#print axioms CubeAp.castP
#print axioms CubeAp.castTwo
#print axioms CubeAp.small
#print axioms CubeAp.cube_ap

At inert \(p\equiv 7\pmod {12}\) both sides vanish: \((p^3-1)/2\) is odd, so \(\chi (-1)=-1\) over \(\mathbb F_{p^3}\) and \(x\mapsto -x\) gives \(\sum \chi (x^3-x)=0\), while \(a_p=0\). So \(a(\mathbb F_{p^3})=a_p^3-3pa_p\) at every \(p\equiv 1\pmod 3\).

Listing 86: lang/CubeInert.lean

lang/CubeInert.lean

/-
  lang/CubeInert — T43, cycle 164: a(𝔽_{p³}) = 0 = a_p³ − 3p a_p for y² = x³ − x at inert p ≡ 7 (mod 12).

    odd_vanish     over any finite field with χ(−1) = −1:  Σ_x χ(x³ − x) = 0          (x ↦ −x)
    negone_odd     (−1)^(2s+1) = −1
    chi_m1_cube    χ(−1) = −1 over 𝔽_{p³}, p = 4j + 3 ((p³ − 1)/2 odd)
    cube_inert     a(𝔽_{p³}) = a_p³ − 3p a_p (both sides 0)
  With CubeAp (p ≡ 1 mod 12) the identity holds at every p ≡ 1 (mod 3) where 𝔽_{p³} = 𝔽_p[∛c].

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.CubeAp
import lang.InertFq

namespace CubeInert

open FTAChain ZMod YMChain FiniteRecChain LibInt

variable {R : Type} [DecidableEq R]

theorem odd_vanish (F : FinField.FF R) (hm1 : FinField.chi F (F.K.neg F.K.one) = -1) :
    sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els = 0 := by
  have hre := reindex LibInt.intR F.els F.comp F.K.neg F.K.neg (fun x => DetChain.neg_neg' F.K x) (fun x => DetChain.neg_neg' F.K x)
    (fun x => FinField.chi F (FinSign.f F x))
  have hodd : ∀ x, FinField.chi F (FinSign.f F (F.K.neg x)) = LibInt.intR.mul (-1) (FinField.chi F (FinSign.f F x)) := fun x => by
    rw [FinSign.f_neg, show F.K.neg (FinSign.f F x) = F.K.mul (F.K.neg F.K.one) (FinSign.f F x) by
      rw [DetChain.neg_mul_left, F.K.one_mul], FinField.chi_mul, hm1]
    rfl
  rw [sumK_ext LibInt.intR _ _ hodd, ← mul_sumK] at hre
  generalize sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els = S at hre ⊢
  have hre' : -1 * S = S := hre
  have h2 : (2 : Int) * S = 2 * 0 := by
    rw [show (2 : Int) * S = S + -(-1 * S) by lib_int, hre']; lib_int
  exact CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) S 0 h2

theorem chi_m1_odd (F : FinField.FF R) (s : Nat) (hh : F.h = 2 * s + 1) : FinField.chi F (F.K.neg F.K.one) = -1 := by
  apply FinField.chi_neg_of
  · intro e
    apply F.ne_pm
    rw [e, ← DetChain.neg_neg' F.K F.K.one, e]
    exact DetChain.neg_unique F.K _ _ (F.K.zero_add _)
  · rw [hh, HasseCM.pw_add', SplitHasse.negone_even, F.K.one_mul]
    show F.K.mul F.K.one (F.K.neg F.K.one) ≠ F.K.one
    rw [F.K.one_mul]; exact F.ne_pm

theorem hF7 (j : Nat) : 4 * (2 * j) * (2 * j) * (2 * j) + 18 * (2 * j) * (2 * j) + 27 * (2 * j) + 12 + 1 = 2 * (16 * j * j * j + 36 * j * j + 27 * j + 6) + 1 := by lib_nat

/-- **a(𝔽_{p³}) = a_p³ − 3p a_p at p = 4j + 3 ≡ 7 (mod 12)** (both sides vanish). -/
theorem cube_inert (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (c : Zp (2 * (2 * j) + 1 + 1)) (m : Nat)
    (hJ : 2 * (2 * j) + 1 + 1 = 3 * m) (hc0 : c ≠ (ring (2 * (2 * j) + 1 + 1)).zero) (hω : Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) c m ≠ (ring (2 * (2 * j) + 1 + 1)).one) :
    -(sumK LibInt.intR (fun x => FinField.chi (CubicInst.ffc (2 * j) c m hpr hJ hc0 hω) (FinSign.f (CubicInst.ffc (2 * j) c m hpr hJ hc0 hω) x)) (CubicInst.ffc (2 * j) c m hpr hJ hc0 hω).els) = (-(sumK LibInt.intR (fun x => QChar.chi (2 * j) (CMInert.cub (ring (2 * (2 * j) + 1 + 1)) x)) (ConicSum.L (2 * j)))) * (-(sumK LibInt.intR (fun x => QChar.chi (2 * j) (CMInert.cub (ring (2 * (2 * j) + 1 + 1  -- (+315 chars; full line in the Zenodo source)
  rw [odd_vanish (CubicInst.ffc (2 * j) c m hpr hJ hc0 hω) (chi_m1_odd (CubicInst.ffc (2 * j) c m hpr hJ hc0 hω) (16 * j * j * j + 36 * j * j + 27 * j + 6) (hF7 j)), InertFq.s1_inert j hpr]
  rfl

end CubeInert
#print axioms CubeInert.odd_vanish
#print axioms CubeInert.chi_m1_odd
#print axioms CubeInert.hF7
#print axioms CubeInert.cube_inert

The same division works in \(\mathbb Z[\sqrt {-3}]\) above \(p\ne 3\): for \(x^2+3y^2=p\) and \(p\mid X^2+3Y^2\),

\[ (Xx+3Yy)(Xx-3Yy)=x^2(X^2+3Y^2)-3Y^2p,\qquad (Xx+3Yy)^2+3(Yx-Xy)^2=p\,(X^2+3Y^2), \]
so \(X+Y\sqrt {-3}=\pi \gamma \) or \(\bar \pi \gamma \).

Listing 87: lang/EisenDiv.lean

lang/EisenDiv.lean

/-
  lang/EisenDiv — T43, cycle 165: π = x + y√−3 of prime norm p ≠ 3 is prime in ℤ[√−3] above p.

  If x² + 3y² = p and p | X² + 3Y², then X + Y√−3 = π γ or π̄ γ:
    quot_exact3    p | Xx + 3Yy and p | Yx − Xy ⇒ X = x u − 3 y v, Y = x v + y u
    div_plus3      p | Xx + 3Yy ⇒ π | X + Y√−3          ((Xx+3Yy)² + 3(Yx−Xy)² = p(X² + 3Y²), p ≠ 3)
    eisen_div      the dichotomy                         ((Xx+3Yy)(Xx−3Yy) = x²(X²+3Y²) − 3Y² p)
  The ℤ[√−3]-analogue of GaussDiv (p is prime to the conductor 2 of ℤ[√−3] ⊂ ℤ[ω]).

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.EisenUnique
import lang.GaussDiv

namespace EisenDiv

open FTAChain LibInt FermatUnique

theorem quot_exact3 (p : Nat) (x y X Y u v : Int) (hxy : x * x + 3 * (y * y) = Int.ofNat (p + 1 + 1))
    (hu : X * x + 3 * (Y * y) = Int.ofNat (p + 1 + 1) * u) (hv : Y * x - X * y = Int.ofNat (p + 1 + 1) * v) :
    X = x * u - 3 * (y * v) ∧ Y = x * v + y * u := by
  constructor
  · apply cancelP p
    rw [show Int.ofNat (p + 1 + 1) * (x * u - 3 * (y * v)) = x * (Int.ofNat (p + 1 + 1) * u) - 3 * (y * (Int.ofNat (p + 1 + 1) * v)) by lib_int,
      ← hu, ← hv, ← hxy]; lib_int
  · apply cancelP p
    rw [show Int.ofNat (p + 1 + 1) * (x * v + y * u) = x * (Int.ofNat (p + 1 + 1) * v) + y * (Int.ofNat (p + 1 + 1) * u) by lib_int,
      ← hu, ← hv, ← hxy]; lib_int

/-- P | n as integers with n > 0 gives a natural quotient. -/
theorem nat_dvd (p n : Nat) (w : Int) (h : Int.ofNat (n + 1) = Int.ofNat (p + 1 + 1) * w) : ∃ q : Nat, n + 1 = q * (p + 1 + 1) := by
  cases w with
  | ofNat q => exact ⟨q, Int.ofNat.inj (h.trans (by show Int.ofNat ((p + 1 + 1) * q) = Int.ofNat (q * (p + 1 + 1)); rw [Nat.mul_comm]))⟩
  | negSucc q =>
    exfalso
    have e : Int.ofNat (p + 1 + 1) * Int.negSucc q = Int.negSucc ((p + 1 + 1) * q + p + 1) := by
      show -(Int.ofNat (p + 1 + 1) * Int.ofNat (q + 1)) = _
      rw [show Int.ofNat (p + 1 + 1) * Int.ofNat (q + 1) = Int.ofNat ((p + 1 + 1) * q + p + 1 + 1) by
        show Int.ofNat ((p + 1 + 1) * (q + 1)) = _; rw [show (p + 1 + 1) * (q + 1) = (p + 1 + 1) * q + p + 1 + 1 by lib_nat]]
      rfl
    rw [e] at h
    exact Int.noConfusion h

theorem div_plus3 (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (x y X Y n u : Int)
    (hxy : x * x + 3 * (y * y) = Int.ofNat (p + 1 + 1)) (hn : X * X + 3 * (Y * Y) = Int.ofNat (p + 1 + 1) * n)
    (hu : X * x + 3 * (Y * y) = Int.ofNat (p + 1 + 1) * u) :
    ∃ a b : Int, X = x * a - 3 * (y * b) ∧ Y = x * b + y * a := by
  have e : (X * x + 3 * (Y * y)) * (X * x + 3 * (Y * y)) + 3 * ((Y * x - X * y) * (Y * x - X * y))
      = (X * X + 3 * (Y * Y)) * (x * x + 3 * (y * y)) := by lib_int
  rw [hu, hn, hxy] at e
  have h3sq : 3 * ((Y * x - X * y) * (Y * x - X * y)) = Int.ofNat (p + 1 + 1) * (Int.ofNat (p + 1 + 1) * n + -(Int.ofNat (p + 1 + 1) * u * u)) := by
    rw [show 3 * ((Y * x - X * y) * (Y * x - X * y)) = (Int.ofNat (p + 1 + 1) * u * (Int.ofNat (p + 1 + 1) * u) + 3 * ((Y * x - X * y) * (Y * x - X * y)))
      + -(Int.ofNat (p + 1 + 1) * u * (Int.ofNat (p + 1 + 1) * u)) by lib_int, e]; lib_int
  have hz : ∃ v, Y * x - X * y = Int.ofNat (p + 1 + 1) * v := by
    cases int_euclid p hp 3 _ _ h3sq with
    | inl h =>
      exfalso
      match h with
      | ⟨w, hw⟩ =>
        match nat_dvd p 2 w hw with
        | ⟨q, hq⟩ =>
          cases Eisen.prime3 (p + 1 + 1) ⟨q, hq⟩ with
          | inl e => exact Nat.noConfusion (Nat.succ.inj e)
          | inr e => exact h3 e
    | inr h => match h with | ⟨w, hw⟩ => exact GaussDiv.sq_div p hp _ w hw
  match hz with
  | ⟨v, hv⟩ =>
    match quot_exact3 p x y X Y u v hxy hu hv with
    | ⟨h1, h2⟩ => exact ⟨u, v, h1, h2⟩

/-- **π or π̄ divides every X + Y√−3 with p | X² + 3Y².** -/
theorem eisen_div (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (x y X Y n : Int)
    (hxy : x * x + 3 * (y * y) = Int.ofNat (p + 1 + 1)) (hn : X * X + 3 * (Y * Y) = Int.ofNat (p + 1 + 1) * n) :
    (∃ a b : Int, X = x * a - 3 * (y * b) ∧ Y = x * b + y * a) ∨ (∃ a b : Int, X = x * a + 3 * (y * b) ∧ Y = x * b - y * a) := by
  have key : (X * x + 3 * (Y * y)) * (X * x + 3 * (Y * (-y))) = Int.ofNat (p + 1 + 1) * (x * x * n + -3 * (Y * Y)) := by
    rw [show (X * x + 3 * (Y * y)) * (X * x + 3 * (Y * (-y))) = x * x * (X * X + 3 * (Y * Y)) + -3 * (Y * Y) * (x * x + 3 * (y * y)) by lib_int, hn, hxy]
    lib_int
  cases int_euclid p hp _ _ _ key with
  | inl h => match h with | ⟨u, hu⟩ => exact Or.inl (div_plus3 p hp h3 x y X Y n u hxy hn hu)
  | inr h =>
    match h with
    | ⟨u, hu⟩ =>
      match div_plus3 p hp h3 x (-y) X Y n u (by rw [← hxy]; lib_int) hn hu with
      | ⟨a, b, h1, h2⟩ => exact Or.inr ⟨a, b, by rw [h1]; lib_int, by rw [h2]; lib_int⟩

end EisenDiv

#print axioms EisenDiv.quot_exact3
#print axioms EisenDiv.nat_dvd
#print axioms EisenDiv.div_plus3
#print axioms EisenDiv.eisen_div

By the same induction, \(N(\alpha )=p^k\) in \(\mathbb Z[\sqrt {-3}]\) forces \(\alpha =\pm \pi ^j\bar \pi ^{\,l}\) with \(j+l=k\).

Listing 88: lang/EisenFactor.lean

lang/EisenFactor.lean

/-
  lang/EisenFactor — T43, cycle 166: factorisation in ℤ[√−3] above a split prime p ≠ 3.

  π = x + y√−3 with x² + 3y² = p prime. Every α ∈ ℤ[√−3] with N(α) = p^k is ±π^j π̄^l, j + l = k:
    emul, enorm, epow, e_ext, emul_comm, emul_assoc, enorm_mul     ℤ[√−3] on pairs (re, im)
    enorm_one                                                        N(α) = 1 ⇒ α = ±1
    efactor                                                          induction on k with EisenDiv.eisen_div
  The input for listing the elements of norm p³ (y² = x³ + 1 over 𝔽_{p³}).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.EisenDiv
import lang.GaussFactor

namespace EisenFactor

open FTAChain LibInt FermatUnique GaussInt GaussFactor

def emul (x y : GI) : GI := ⟨x.re * y.re - 3 * (x.im * y.im), x.re * y.im + x.im * y.re⟩
def enorm (x : GI) : Int := x.re * x.re + 3 * (x.im * x.im)

def epow (x : GI) : Nat → GI
  | 0 => gone
  | n + 1 => emul (epow x n) x

theorem emul_comm (x y : GI) : emul x y = emul y x :=
  gi_ext _ _ (by show x.re * y.re - 3 * (x.im * y.im) = y.re * x.re - 3 * (y.im * x.im); lib_int)
    (by show x.re * y.im + x.im * y.re = y.re * x.im + y.im * x.re; lib_int)

theorem emul_assoc (x y z : GI) : emul (emul x y) z = emul x (emul y z) :=
  gi_ext _ _ (by show (x.re * y.re - 3 * (x.im * y.im)) * z.re - 3 * ((x.re * y.im + x.im * y.re) * z.im)
      = x.re * (y.re * z.re - 3 * (y.im * z.im)) - 3 * (x.im * (y.re * z.im + y.im * z.re)); lib_int)
    (by show (x.re * y.re - 3 * (x.im * y.im)) * z.im + (x.re * y.im + x.im * y.re) * z.re
      = x.re * (y.re * z.im + y.im * z.re) + x.im * (y.re * z.re - 3 * (y.im * z.im)); lib_int)

theorem enorm_mul (x y : GI) : enorm (emul x y) = enorm x * enorm y := by
  show (x.re * y.re - 3 * (x.im * y.im)) * (x.re * y.re - 3 * (x.im * y.im)) + 3 * ((x.re * y.im + x.im * y.re) * (x.re * y.im + x.im * y.re))
    = (x.re * x.re + 3 * (x.im * x.im)) * (y.re * y.re + 3 * (y.im * y.im))
  lib_int

theorem gone_emul (x : GI) : emul gone x = x :=
  gi_ext _ _ (by show 1 * x.re - 3 * (0 * x.im) = x.re; lib_int) (by show 1 * x.im + 0 * x.re = x.im; lib_int)

def IsUnit3 (u : GI) : Prop := u = ⟨1, 0⟩ ∨ u = ⟨-1, 0⟩

theorem enorm_one (x : GI) (h : enorm x = 1) : IsUnit3 x := by
  match StrictHasse.sq_nat x.re, StrictHasse.sq_nat x.im with
  | ⟨n, hn⟩, ⟨m, hm⟩ =>
    have e : n * n + 3 * (m * m) = 1 := Int.ofNat.inj (show Int.ofNat (n * n) + 3 * Int.ofNat (m * m) = 1 by rw [← hn, ← hm]; exact h)
    have hm0 : m * m = 0 := by
      cases hmm : m * m with
      | zero => rfl
      | succ t =>
        rw [hmm] at e
        exact Nat.noConfusion (Nat.succ.inj ((show n * n + 3 * (t + 1) = (n * n + 3 * t + 1) + 1 + 1 by lib_nat).symm.trans e))
    have hi : x.im = 0 := HasseDavenport.sq_zero _ (by rw [hm, hm0]; rfl)
    have hn1 : n * n = 1 := (show n * n = n * n + 3 * (m * m) by rw [hm0]; rfl).trans e
    cases QuarticFamily.sq_eq x.re 1 (by rw [hn, hn1]; rfl) with
    | inl q => exact Or.inl (gi_ext _ _ q hi)
    | inr q => exact Or.inr (gi_ext _ _ q hi)

/-- **Factorisation above p in ℤ[√−3].** -/
theorem efactor (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (a b : Int)
    (hab : a * a + 3 * (b * b) = Int.ofNat (p + 1 + 1)) : ∀ (k : Nat) (x : GI), enorm x = ipow (Int.ofNat (p + 1 + 1)) k →
    ∃ u j l, IsUnit3 u ∧ j + l = k ∧ x = emul u (emul (epow ⟨a, b⟩ j) (epow ⟨a, -b⟩ l))
  | 0, x, h => ⟨x, 0, 0, enorm_one x h, rfl, by
      show x = emul x (emul gone gone)
      rw [gone_emul, emul_comm, gone_emul]⟩
  | k + 1, x, h => by
    have hn : x.re * x.re + 3 * (x.im * x.im) = Int.ofNat (p + 1 + 1) * ipow (Int.ofNat (p + 1 + 1)) k := by
      rw [show Int.ofNat (p + 1 + 1) * ipow (Int.ofNat (p + 1 + 1)) k = ipow (Int.ofNat (p + 1 + 1)) (k + 1) by
        show _ = ipow _ k * _; lib_int]; exact h
    have normq : ∀ (q : GI) (x' y' : Int), enorm q = Int.ofNat (p + 1 + 1) → x = emul q ⟨x', y'⟩ →
        enorm ⟨x', y'⟩ = ipow (Int.ofNat (p + 1 + 1)) k := fun q x' y' hq e => by
      apply cancelP p
      rw [← hn, ← hq, ← enorm_mul, ← e]; rfl
    cases EisenDiv.eisen_div p hp h3 a b x.re x.im _ hab hn with
    | inl h1 =>
      match h1 with
      | ⟨x', y', e1, e2⟩ =>
        have ex : x = emul ⟨a, b⟩ ⟨x', y'⟩ := gi_ext _ _ e1 e2
        match efactor p hp h3 a b hab k ⟨x', y'⟩ (normq ⟨a, b⟩ x' y' hab ex) with
        | ⟨u, j, l, hu, hjl, hg⟩ =>
          refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
          rw [ex, hg]
          show emul ⟨a, b⟩ (emul u (emul (epow ⟨a, b⟩ j) (epow ⟨a, -b⟩ l))) = emul u (emul (emul (epow ⟨a, b⟩ j) ⟨a, b⟩) (epow ⟨a, -b⟩ l))
          rw [← emul_assoc, emul_comm ⟨a, b⟩ u, emul_assoc, emul_assoc, ← emul_assoc ⟨a, b⟩, emul_comm ⟨a, b⟩ (epow ⟨a, b⟩ j), emul_assoc]
    | inr h1 =>
      match h1 with
      | ⟨x', y', e1, e2⟩ =>
        have ex : x = emul ⟨a, -b⟩ ⟨x', y'⟩ :=
          gi_ext _ _ (by rw [e1]; show _ = a * x' - 3 * (-b * y'); lib_int) (by rw [e2]; show _ = a * y' + -b * x'; lib_int)
        match efactor p hp h3 a b hab k ⟨x', y'⟩ (normq ⟨a, -b⟩ x' y' (by rw [← hab]; show a * a + 3 * (-b * -b) = _; lib_int) ex) with
        | ⟨u, j, l, hu, hjl, hg⟩ =>
          refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
          rw [ex, hg]
          show emul ⟨a, -b⟩ (emul u (emul (epow ⟨a, b⟩ j) (epow ⟨a, -b⟩ l))) = emul u (emul (epow ⟨a, b⟩ j) (emul (epow ⟨a, -b⟩ l) ⟨a, -b⟩))
          rw [← emul_assoc, emul_comm ⟨a, -b⟩ u, emul_assoc, ← emul_assoc ⟨a, -b⟩, emul_comm ⟨a, -b⟩ (epow ⟨a, b⟩ j),
            emul_assoc, emul_comm ⟨a, -b⟩ (epow ⟨a, -b⟩ l)]

end EisenFactor

#print axioms EisenFactor.emul
#print axioms EisenFactor.enorm
#print axioms EisenFactor.epow
#print axioms EisenFactor.emul_comm
#print axioms EisenFactor.emul_assoc
#print axioms EisenFactor.enorm_mul
#print axioms EisenFactor.gone_emul
#print axioms EisenFactor.enorm_one
#print axioms EisenFactor.efactor

So for \(\pi =x+y\sqrt {-3}\), \(x\equiv 1\ (3)\): an element \(U+W\sqrt {-3}\) of norm \(p^3\) with \(p\nmid U\) and \(U\equiv 1\ (3)\) has \(U=x^3-9xy^2=\mathrm {Re}\,\pi ^3\).

Listing 89: lang/EisenCube.lean

lang/EisenCube.lean

/-
  lang/EisenCube — T43, cycle 167: elements of norm p³ in ℤ[√−3] prime to p.

  π = x + y√−3, x² + 3y² = p ≠ 3, x ≡ 1 (mod 3). If U² + 3W² = p³, p ∤ U and U ≡ 1 (mod 3), then U = x³ − 9xy² = Re π³:
    ecube_re, e12, e21      Re π³, π π̄² = p π̄, π² π̄ = p π
    eisen_cube              EisenFactor gives ±π^j π̄^(3−j); j = 1, 2 give p | U; the sign − gives U ≡ −1 (mod 3).
  With ψ(1) ≡ 1 (mod 3) over 𝔽_{p³}: a(𝔽_{p³}) = 2 Re π³ = a_p³ − 3p a_p for y² = x³ + 1.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.EisenFactor
import lang.EisenAp

namespace EisenCube

open FTAChain LibInt FermatUnique GaussInt GaussFactor EisenFactor

theorem ecube_re (x : GI) : (epow x 3).re = x.re * x.re * x.re - 9 * (x.re * (x.im * x.im)) := by
  show ((1 * x.re - 3 * (0 * x.im)) * x.re - 3 * ((1 * x.im + 0 * x.re) * x.im)) * x.re
    - 3 * (((1 * x.re - 3 * (0 * x.im)) * x.im + (1 * x.im + 0 * x.re) * x.re) * x.im) = _
  lib_int

theorem e12 (a b : Int) : emul (epow ⟨a, b⟩ 1) (epow ⟨a, -b⟩ 2) = ⟨(a * a + 3 * (b * b)) * a, -((a * a + 3 * (b * b)) * b)⟩ := by
  apply gi_ext
  · show (1 * a - 3 * (0 * b)) * ((1 * a - 3 * (0 * -b)) * a - 3 * ((1 * -b + 0 * a) * -b))
      - 3 * ((1 * b + 0 * a) * ((1 * a - 3 * (0 * -b)) * -b + (1 * -b + 0 * a) * a)) = (a * a + 3 * (b * b)) * a
    lib_int
  · show (1 * a - 3 * (0 * b)) * ((1 * a - 3 * (0 * -b)) * -b + (1 * -b + 0 * a) * a)
      + (1 * b + 0 * a) * ((1 * a - 3 * (0 * -b)) * a - 3 * ((1 * -b + 0 * a) * -b)) = -((a * a + 3 * (b * b)) * b)
    lib_int

theorem e21 (a b : Int) : emul (epow ⟨a, b⟩ 2) (epow ⟨a, -b⟩ 1) = ⟨(a * a + 3 * (b * b)) * a, (a * a + 3 * (b * b)) * b⟩ := by
  apply gi_ext
  · show ((1 * a - 3 * (0 * b)) * a - 3 * ((1 * b + 0 * a) * b)) * (1 * a - 3 * (0 * -b))
      - 3 * (((1 * a - 3 * (0 * b)) * b + (1 * b + 0 * a) * a) * (1 * -b + 0 * a)) = (a * a + 3 * (b * b)) * a
    lib_int
  · show ((1 * a - 3 * (0 * b)) * a - 3 * ((1 * b + 0 * a) * b)) * (1 * -b + 0 * a)
      + ((1 * a - 3 * (0 * b)) * b + (1 * b + 0 * a) * a) * (1 * a - 3 * (0 * -b)) = (a * a + 3 * (b * b)) * b
    lib_int

theorem sign_re (u z : GI) (hu : IsUnit3 u) : (emul u z).re = z.re ∨ (emul u z).re = -z.re := by
  cases hu with
  | inl h => subst h; left; show 1 * z.re - 3 * (0 * z.im) = z.re; lib_int
  | inr h => subst h; right; show -1 * z.re - 3 * (0 * z.im) = -z.re; lib_int

/-- **U = Re π³.** -/
theorem eisen_cube (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (h3 : p + 1 + 1 ≠ 3) (a b : Int)
    (hab : a * a + 3 * (b * b) = Int.ofNat (p + 1 + 1)) (ha : ∃ s, a = 3 * s + 1) (z : GI)
    (hN : enorm z = ipow (Int.ofNat (p + 1 + 1)) 3) (hU : ¬ ∃ w, z.re = Int.ofNat (p + 1 + 1) * w)
    (hU1 : ∃ t, z.re = 3 * t + 1) : z.re = a * a * a - 9 * (a * (b * b)) := by
  match efactor p hp h3 a b hab 3 z hN with
  | ⟨u, j, l, hu, hjl, hz⟩ =>
    have pm : z.re = a * a * a - 9 * (a * (b * b)) ∨ z.re = -(a * a * a - 9 * (a * (b * b))) → z.re = a * a * a - 9 * (a * (b * b)) :=
      fun h => by
        cases h with
        | inl h => exact h
        | inr h =>
          exfalso
          match ha, hU1 with
          | ⟨s, hs⟩, ⟨t, ht⟩ =>
            apply EisenAp.three_ne_r (t + 9 * s * s * s + 9 * s * s + 3 * s - 3 * ((3 * s + 1) * (b * b)) + 1) 1
              (fun e => Nat.noConfusion e) (Nat.lt_succ_of_lt (Nat.lt_succ_self 1))
            show _ = 1
            have e : 3 * t + 1 = -(a * a * a - 9 * (a * (b * b))) := ht.symm.trans h
            rw [hs] at e
            rw [show 3 * (t + 9 * s * s * s + 9 * s * s + 3 * s - 3 * ((3 * s + 1) * (b * b)) + 1)
              = (3 * t + 1) + 27 * s * s * s + 27 * s * s + 9 * s - 9 * ((3 * s + 1) * (b * b)) + 2 by lib_int, e]; lib_int
    match j with
    | 0 =>
      have hl : l = 3 := (Nat.zero_add l).symm.trans hjl
      subst hl
      apply pm
      rw [hz, show emul (epow ⟨a, b⟩ 0) (epow ⟨a, -b⟩ 3) = epow ⟨a, -b⟩ 3 from gone_emul _]
      have hc : (epow ⟨a, -b⟩ 3).re = a * a * a - 9 * (a * (b * b)) := by
        rw [ecube_re]; show a * a * a - 9 * (a * (-b * -b)) = _; lib_int
      rw [← hc]; exact sign_re u _ hu
    | 3 =>
      have hl : l = 0 := add_cancel 3 l 0 (hjl.trans rfl)
      subst hl
      apply pm
      rw [hz, show emul (epow ⟨a, b⟩ 3) (epow ⟨a, -b⟩ 0) = epow ⟨a, b⟩ 3 by
        show emul (epow ⟨a, b⟩ 3) gone = _; rw [emul_comm, gone_emul]]
      rw [← ecube_re ⟨a, b⟩]; exact sign_re u _ hu
    | 1 =>
      have hl : l = 2 := Nat.succ.inj ((Nat.add_comm l 1).trans hjl)
      subst hl
      exfalso; apply hU
      rw [hz, e12, ← hab]
      cases sign_re u ⟨(a * a + 3 * (b * b)) * a, -((a * a + 3 * (b * b)) * b)⟩ hu with
      | inl h => exact ⟨a, by rw [h]⟩
      | inr h => exact ⟨-a, by rw [h]; show -((a * a + 3 * (b * b)) * a) = _; lib_int⟩
    | 2 =>
      have hl : l = 1 := Nat.succ.inj (Nat.succ.inj ((show l + 1 + 1 = 2 + l by lib_nat).trans hjl))
      subst hl
      exfalso; apply hU
      rw [hz, e21, ← hab]
      cases sign_re u ⟨(a * a + 3 * (b * b)) * a, (a * a + 3 * (b * b)) * b⟩ hu with
      | inl h => exact ⟨a, by rw [h]⟩
      | inr h => exact ⟨-a, by rw [h]; show -((a * a + 3 * (b * b)) * a) = _; lib_int⟩
    | j + 4 =>
      exfalso
      exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show j + l + 1 + 1 + 1 + 1 = j + 4 + l by lib_nat).trans hjl))))

end EisenCube

#print axioms EisenCube.ecube_re
#print axioms EisenCube.e12
#print axioms EisenCube.e21
#print axioms EisenCube.sign_re
#print axioms EisenCube.eisen_cube

78.26 \(a(\mathbb F_{p^3})\) for \(y^2=x^3+1\)

The third zeta coefficient of \(y^2=x^3+1\) at \(p\equiv 1\pmod 6\) is that of the Hecke \(L\)-factor. \(a(\mathbb F_{p^3})=a_p^3-3p\,a_p\).

lang/CubeAp3.lean

/-
  lang/CubeAp3 — T43, cycle 169: a(𝔽_{p³}) = a_p³ − 3p a_p for y² = x³ + 1 at every p ≡ 1 (mod 6).

  𝔽_{p³} = 𝔽_p[∛c], p = 6t + 7. FinCubicExact over 𝔽_{p³}: ψ₃(1) = −2Z, Z² + 3W² = p³, Z ≡ 1 (mod 3); EisenAp over 𝔽_p:
  ψ₁(1) = −2x, x² + 3y² = p, x ≡ 1 (mod 3). CubeHasse3: ψ₃ ≡ ψ₁³ (mod p), so p | Z would give p | x, impossible. EisenCube:
  Z = x³ − 9xy², i.e.
    cube_ap3     a(𝔽_{p³}) = a_p³ − 3p a_p

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.CubeHasse3
import lang.EisenCube
import lang.CubeAp

namespace CubeAp3

open FTAChain ZMod YMChain FiniteRecChain LibRing GaussInt GaussFactor EisenFactor

theorem small3 (p : Nat) (a b q : Int) (hab : a * a + 3 * (b * b) = Int.ofNat (p + 1 + 1)) (hq : a = Int.ofNat (p + 1 + 1) * q) :
    q * q = 0 := by
  match StrictHasse.sq_nat q, StrictHasse.sq_nat b with
  | ⟨nq, hnq⟩, ⟨nb, hnb⟩ =>
    have e : (p + 1 + 1) * (p + 1 + 1) * (nq * nq) + 3 * (nb * nb) = p + 1 + 1 := Int.ofNat.inj (by
      show Int.ofNat (p + 1 + 1) * Int.ofNat (p + 1 + 1) * Int.ofNat (nq * nq) + 3 * Int.ofNat (nb * nb) = Int.ofNat (p + 1 + 1)
      have hab' := hab
      rw [hq] at hab'
      rw [← hnq, ← hnb]
      exact (show Int.ofNat (p + 1 + 1) * Int.ofNat (p + 1 + 1) * (q * q) + 3 * (b * b) = Int.ofNat (p + 1 + 1) * q * (Int.ofNat (p + 1 + 1) * q) + 3 * (b * b) by lib_int).trans hab')
    cases hn : nq * nq with
    | zero => rw [hnq, hn]; rfl
    | succ n =>
      rw [hn] at e
      exfalso
      have e2 := FermatUnique.add_cancel (p + 1 + 1) _ 0 ((show (p + 1 + 1) + ((p * p + 3 * p + 1) + 1 + (p + 1 + 1) * (p + 1 + 1) * n + 3 * (nb * nb))
        = (p + 1 + 1) * (p + 1 + 1) * (n + 1) + 3 * (nb * nb) by lib_nat).trans e)
      exact Nat.noConfusion ((show (p * p + 3 * p + 1) + 1 + (p + 1 + 1) * (p + 1 + 1) * n + 3 * (nb * nb)
        = (p * p + 3 * p + 1 + (p + 1 + 1) * (p + 1 + 1) * n + 3 * (nb * nb)) + 1 by lib_nat).symm.trans e2)

theorem psi_same (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one = CubicJacob.psi m (ring (2 * m + 1 + 1)).one := rfl

theorem h_gen (K : Nat) : 2 * (3 * K + 3) = 3 * (2 * K + 2) := by lib_nat

/-- **a(𝔽_{p³}) = a_p³ − 3p a_p for y² = x³ + 1** (p = 6t + 7, 𝔽_{p³} = 𝔽_p[∛c]). -/
theorem cube_ap3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) (c : Zp (2 * (3 * t + 2) + 1 + 1)) (m : Nat)
    (hJ : 2 * (3 * t + 2) + 1 + 1 = 3 * m) (hc0 : c ≠ (ring (2 * (3 * t + 2) + 1 + 1)).zero) (hω : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) c m ≠ (ring (2 * (3 * t + 2) + 1 + 1)).one) :
    -(FinCubic.psi (CubicInst.ffc (3 * t + 2) c m hpr hJ hc0 hω) (CubicInst.ffc (3 * t + 2) c m hpr hJ hc0 hω).K.one) = (-(FinCubic.psi (FinField.ffp (3 * t + 2) hpr) (FinField.ffp (3 * t + 2) hpr).K.one)) * (-(FinCubic.psi (FinField.ffp (3 * t + 2) hpr) (FinField.ffp (3 * t + 2) hpr).K.one)) * (-(FinCubic.psi (FinField.ffp (3 * t + 2) hpr) (FinField.ffp (3 * t + 2) hpr).K.one)) - 3 * Int.ofNat (2  -- (+121 chars; full line in the Zenodo source)
  have hN1 : 2 * ((3 * t + 2) + 1) = 3 * (2 * t + 2) := by lib_nat
  have hk : (CubicInst.ffc (3 * t + 2) c m hpr hJ hc0 hω).h = 3 * (t * (1 + (2 * (3 * t + 2) + 1 + 1 + 1) + (2 * (3 * t + 2) + 1 + 1 + 1) * (2 * (3 * t + 2) + 1 + 1 + 1)) + (2 * (3 * t + 2) + 1 + 1 + 1) + (2 * (3 * t + 2) + 1 + 1 + 1) * (2 * (3 * t + 2) + 1 + 1 + 1)) + 3 := CubeHasse3.hF3' t
  have hN3 : 2 * (CubicInst.ffc (3 * t + 2) c m hpr hJ hc0 hω).h = 3 * (2 * (t * (1 + (2 * (3 * t + 2) + 1 + 1 + 1) + (2 * (3 * t + 2) + 1 + 1 + 1) * (2 * (3 * t + 2) + 1 + 1 + 1)) + (2 * (3 * t + 2) + 1 + 1 + 1) + (2 * (3 * t + 2) + 1 + 1 + 1) * (2 * (3 * t + 2) + 1 + 1 + 1)) + 2) := by rw [hk]; exact h_gen _
  have h3 := EisenAp.p_ne_three (3 * t + 2) (2 * t + 2) hN1
  have hc := CubeHasse3.cube_hasse3 t hpr c m hJ hc0 hω
  rw [← psi_same (3 * t + 2) hpr] at hc
  match EisenAp.eisen_exists (3 * t + 2) hpr (2 * t + 2) hN1,
    FinCubicExact.fin_cubic_trace (CubicInst.ffc (3 * t + 2) c m hpr hJ hc0 hω) (2 * (t * (1 + (2 * (3 * t + 2) + 1 + 1 + 1) + (2 * (3 * t + 2) + 1 + 1 + 1) * (2 * (3 * t + 2) + 1 + 1 + 1)) + (2 * (3 * t + 2) + 1 + 1 + 1) + (2 * (3 * t + 2) + 1 + 1 + 1) * (2 * (3 * t + 2) + 1 + 1 + 1)) + 2) hN3, FinCubicTrace.psi_one_mod3_ff (CubicInst.ffc (3 * t + 2) c m hpr hJ hc0 hω) (2 * (t * (1 + (2 * (3 * t   -- (+193 chars; full line in the Zenodo source)
  | ⟨x, y, hxy, hx, hS1⟩, ⟨U, W, hUW, hpsi⟩, ⟨T, hT⟩ =>
    generalize (FinCubic.psi (CubicInst.ffc (3 * t + 2) c m hpr hJ hc0 hω) (CubicInst.ffc (3 * t + 2) c m hpr hJ hc0 hω).K.one) = s3 at hc hpsi hT ⊢
    generalize (FinCubic.psi (FinField.ffp (3 * t + 2) hpr) (FinField.ffp (3 * t + 2) hpr).K.one) = s1 at hc hS1 ⊢
    -- choose Z with s3 = −2Z
    have hZ : ∃ Z : Int, s3 = -2 * Z ∧ Z * Z = Int.ofNat U * Int.ofNat U := by
      cases hpsi with
      | inl h => exact ⟨-Int.ofNat U, by rw [h]; show 2 * Int.ofNat U = _; lib_int, by lib_int⟩
      | inr h => exact ⟨Int.ofNat U, by rw [h]; show -(2 * Int.ofNat U) = _; lib_int, rfl⟩
    match hZ with
    | ⟨Z, hZs, hZZ⟩ =>
    have hZ1 : ∃ s, Z = 3 * s + 1 := ⟨Z + T, by
      have : -2 * Z = 1 + 3 * T := hZs.symm.trans hT
      rw [show 3 * (Z + T) + 1 = 3 * Z + (1 + 3 * T) by lib_int, ← this]; lib_int⟩
    have hNorm : enorm ⟨Z, Int.ofNat W⟩ = ipow (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) 3 := by
      show Z * Z + 3 * (Int.ofNat W * Int.ofNat W) = 1 * Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)
      rw [hZZ]
      have hq : Int.ofNat U * Int.ofNat U + 3 * (Int.ofNat W * Int.ofNat W) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) :=
        congrArg Int.ofNat (hUW.trans ((congrArg (fun z => 2 * z + 1) hk).trans (CubeHasse3.cube_gen t (2 * (3 * t + 2) + 1 + 1 + 1) (by lib_nat))))
      rw [hq]; lib_int
    have hU : ¬ ∃ w, Z = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * w := fun hex => match hex with
      | ⟨w, hw⟩ => by
        have h0 : castZ (ring (2 * (3 * t + 2) + 1 + 1)) s3 = (ring (2 * (3 * t + 2) + 1 + 1)).zero := by
          rw [hZs, hw, show -2 * (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * w) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * (-2 * w) by lib_int,
            HasseDavenport.castZ_mul, CubeAp.castP, DetChain.zero_mul']
        rw [h0] at hc
        have h1 : castZ (ring (2 * (3 * t + 2) + 1 + 1)) s1 = (ring (2 * (3 * t + 2) + 1 + 1)).zero := by
          cases ZMod.nozd (2 * (3 * t + 2) + 1) hpr _ _ hc.symm with
          | inl h => cases ZMod.nozd (2 * (3 * t + 2) + 1) hpr _ _ h with
            | inl h' => exact h'
            | inr h' => exact h'
          | inr h => exact h
        have hx0 : castZ (ring (2 * (3 * t + 2) + 1 + 1)) x = (ring (2 * (3 * t + 2) + 1 + 1)).zero := by
          rw [hS1, show -2 * x = 2 * -x by lib_int, HasseDavenport.castZ_mul, CubeAp.castTwo, HasseDavenport.castZ_neg] at h1
          cases ZMod.nozd (2 * (3 * t + 2) + 1) hpr _ _ h1 with
          | inl h => exact absurd h (Eisen.two_ne_zero' (2 * (3 * t + 2) + 1) (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le _))))
          | inr h =>
            rw [← DetChain.neg_neg' (ring (2 * (3 * t + 2) + 1 + 1)) (castZ (ring (2 * (3 * t + 2) + 1 + 1)) x), h]
            exact (DetChain.neg_unique (ring (2 * (3 * t + 2) + 1 + 1)) _ _ ((ring (2 * (3 * t + 2) + 1 + 1)).zero_add _)).symm
        match HasseDavenport.castZ_zero_dvd _ x hx0, hx with
        | ⟨q, hq⟩, ⟨s, hs⟩ =>
          have q0 : q = 0 := HasseDavenport.sq_zero q (small3 (2 * (3 * t + 2) + 1) x y q hxy hq)
          rw [q0] at hq
          exact EisenAp.three_ne_r (-s) 1 (fun e => Nat.noConfusion e) (Nat.lt_succ_of_lt (Nat.lt_succ_self 1))
            (by show _ = 1; rw [show 3 * -s = -(3 * s + 1) + 1 by lib_int, ← hs, hq]; lib_int)
    have hre := EisenCube.eisen_cube (2 * (3 * t + 2) + 1) hpr h3 x y hxy hx ⟨Z, Int.ofNat W⟩ hNorm hU hZ1
    have hre' : Z = x * x * x - 9 * (x * (y * y)) := hre
    rw [hZs, hS1, hre', ← hxy]; lib_int

end CubeAp3

#print axioms CubeAp3.small3
#print axioms CubeAp3.psi_same
#print axioms CubeAp3.h_gen
#print axioms CubeAp3.cube_ap3

#print axioms: 4 theorems, no axioms.

78.27 \(a(\mathbb F_{p^N})\) for \(y^2=x^3-x\)

Over every finite field of order \(p^N\) containing \(i\) (\(p\equiv 1\pmod 4\)) the trace is that of the Hecke \(L\)-factor. \(-\sum _{z\in \mathbb F_{p^N}}\chi (z^3-z)=2\,\mathrm {Re}\,\pi ^N=\alpha ^N+\beta ^N\).

lang/FinPow.lean

/-
  lang/FinPow — T43, cycle 172: a(𝔽_q) = 2 Re π^N = α^N + β^N for y² = x³ − x over every finite field of order q = p^N
  containing i, at every split prime p = 4k + 5.

  Hypotheses on the field F (any FF): characteristic p (p·1 = 0), ℤ → F has kernel pℤ, h = (q − 1)/2 = rep (2k + 2) N, an
  injective key and i² = −1. Then
    rep_two, rep_pow       2·rep(k+1) = rep(2k+2);  2 rep(2k+2) N + 1 = p^N
    castN_nsm, pw_ne_zero   ℤ → F on naturals; x ≠ 0 ⇒ x^n ≠ 0
    fin_pow                −Σ_F χ(z³ − z) = 2 Re π^N, π = a + bi primary of norm p, a_p = 2a
  Proof: FinHasse + LucasIter give S_F ≡ ±C(2k+2, k+1)^N (mod p), nonzero since p ∤ a_p; FinPrimary gives S_F = −2A with
  A + Bi primary of norm q; GaussPow gives A = Re π^N.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.GaussPow
import lang.LucasIter
import lang.CubeAp

namespace FinPow

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor LucasIter
open LibRing (castZ castN)

theorem rep_two (P c : Nat) : ∀ N, 2 * rep P c N = rep P (2 * c) N
  | 0 => rfl
  | N + 1 => by
    show 2 * (c + rep P c N * P) = 2 * c + rep P (2 * c) N * P
    rw [← rep_two P c N]; lib_nat

theorem rep_pow (k : Nat) : ∀ N, Int.ofNat (2 * rep (4 * k + 5) (2 * k + 2) N + 1) = ipow (Int.ofNat (4 * k + 5)) N
  | 0 => rfl
  | N + 1 => by
    show Int.ofNat (2 * ((2 * k + 2) + rep (4 * k + 5) (2 * k + 2) N * (4 * k + 5)) + 1) = ipow (Int.ofNat (4 * k + 5)) N * Int.ofNat (4 * k + 5)
    rw [← rep_pow k N]
    show Int.ofNat (2 * ((2 * k + 2) + rep (4 * k + 5) (2 * k + 2) N * (4 * k + 5)) + 1)
      = Int.ofNat ((2 * rep (4 * k + 5) (2 * k + 2) N + 1) * (4 * k + 5))
    exact congrArg Int.ofNat (by lib_nat)

theorem castN_nsm {R : Type} (K : DetChain.CRing R) : ∀ n, castN K n = nsm K n K.one
  | 0 => rfl
  | n + 1 => by
    show K.add K.one (castN K n) = K.add (nsm K n K.one) K.one
    rw [castN_nsm K n, K.add_comm]

theorem pw_ne_zero {R : Type} (K : DetChain.CRing R) (hK : DetChain.NoZD K) (x : R) (hx : x ≠ K.zero) : ∀ n, pw K x n ≠ K.zero
  | 0 => fun e => by
    have h1 : K.one = K.zero := e
    exact hx (by rw [← K.one_mul x, h1, DetChain.zero_mul'])
  | n + 1 => fun e => by
    cases hK _ _ e with
    | inl h => exact pw_ne_zero K hK x hx n h
    | inr h => exact hx h

theorem hr (k N : Nat) : rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) N = 2 * rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N := by
  rw [rep_two, show 2 * (k + 1) = 2 * k + 2 by lib_nat]

theorem P_eq (k : Nat) : (2 * (2 * k + 1) + 1 + 1 + 1) = 4 * k + 5 := by lib_nat

variable {R : Type} [DecidableEq R]

/-- **a(F) = 2 Re π^(N+1)** for y² = x³ − x over every F of order p^(N+1) ∋ i (p = 4k + 5 split). -/
theorem fin_pow (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * k + 1) + 1 + 1 + 1)) (F : FinField.FF R)
    (hchar : nsm F.K (2 * (2 * k + 1) + 1 + 1 + 1) F.K.one = F.K.zero)
    (hcast : ∀ z : Int, castZ F.K z = F.K.zero → ∃ w, z = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * w)
    (N : Nat) (hh : F.h = rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) (N + 1))
    (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) :
    ∃ a b : Int, a * a + b * b = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) ∧ Primary ⟨a, b⟩ ∧ -(sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1))) = 2 * a ∧
      -(sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = 2 * (gpow ⟨a, b⟩ (N + 1)).re := by
  match SignCM3.ap_primary k hpr, FinPrimary.fin_ap_primary F key hkey i hi with
  | ⟨a, b, hab, hPa, hS1⟩, ⟨A, B, hAB, hPA, hSF⟩ =>
    refine ⟨a, b, hab, hPa, hS1, ?_⟩
    -- the Hasse congruence over F
    have hK : F.h = 2 * (k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1)) + 2 := by
      rw [hh]; show (2 * k + 2) + rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) N * (2 * (2 * k + 1) + 1 + 1 + 1) = _; rw [hr]; lib_nat
    have hc1 : castZ F.K (sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = F.K.neg (nsm F.K (C (rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) (N + 1)) (rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) (N + 1)))
        (pw F.K (F.K.neg F.K.one) (k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1) + 1))) := by
      rw [FinCast.cast_sum_ff, hK]
      refine (FinHasse.fin_hasse F _ hK).trans ?_
      rw [show 2 * (k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1)) + 2 = rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) (N + 1) by
        show _ = (2 * k + 2) + rep (2 * (2 * k + 1) + 1 + 1 + 1) (2 * k + 2) N * (2 * (2 * k + 1) + 1 + 1 + 1); rw [hr]; lib_nat]
      rw [show k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1) + 1 = rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) (N + 1) by
        show _ = (k + 1) + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1); lib_nat]
    rw [Frobenius.nsm_eq_mul, LucasIter.lucas_rep F.K (2 * (2 * k + 1) + 1) hpr hchar (2 * k + 2) (k + 1)
      (SplitHasse.lt_p k) (SplitHasse.lt_p' k) (N + 1)] at hc1
    -- the base congruence: C(2k+2, k+1) ≢ 0 mod p
    have hX : nsm F.K (Frobenius.C (2 * k + 2) (k + 1)) F.K.one ≠ F.K.zero := fun e => by
      have e1 : castZ F.K (Int.ofNat (Frobenius.C (2 * k + 2) (k + 1))) = F.K.zero := by
        show castN F.K _ = _; rw [castN_nsm]; exact e
      match hcast _ e1 with
      | ⟨w, hw⟩ =>
        have e2 : nsm (ring (2 * (2 * k + 1) + 1 + 1)) (Frobenius.C (2 * k + 2) (k + 1)) (ring (2 * (2 * k + 1) + 1 + 1)).one = (ring (2 * (2 * k + 1) + 1 + 1)).zero := by
          rw [← castN_nsm]; show castZ (ring (2 * (2 * k + 1) + 1 + 1)) (Int.ofNat (Frobenius.C (2 * k + 2) (k + 1))) = _
          rw [hw, HasseDavenport.castZ_mul, CubeAp.castP, DetChain.zero_mul']
        have e3 := SplitHasse.cast_S1 k hpr
        rw [Frobenius.nsm_eq_mul, e2, DetChain.zero_mul'] at e3
        have e4 : castZ (ring (2 * (2 * k + 1) + 1 + 1)) (sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1))) = (ring (2 * (2 * k + 1) + 1 + 1)).zero := e3.trans (DetChain.neg_unique (ring (2 * (2 * k + 1) + 1 + 1)) _ _ ((ring (2 * (2 * k + 1) + 1 + 1)).zero_add _)).symm
        generalize (sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1))) = s1 at e4 hS1
        have ha : castZ (ring (2 * (2 * k + 1) + 1 + 1)) a = (ring (2 * (2 * k + 1) + 1 + 1)).zero := by
          rw [show s1 = 2 * -a by rw [show s1 = -(-s1) by lib_int, hS1]; lib_int, HasseDavenport.castZ_mul, CubeAp.castTwo,
            HasseDavenport.castZ_neg] at e4
          cases ZMod.nozd (2 * (2 * k + 1) + 1) hpr _ _ e4 with
          | inl h => exact absurd h (Eisen.two_ne_zero' (2 * (2 * k + 1) + 1) (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le _))))
          | inr h =>
            rw [← DetChain.neg_neg' (ring (2 * (2 * k + 1) + 1 + 1)) (castZ (ring (2 * (2 * k + 1) + 1 + 1)) a), h]
            exact (DetChain.neg_unique (ring (2 * (2 * k + 1) + 1 + 1)) _ _ ((ring (2 * (2 * k + 1) + 1 + 1)).zero_add _)).symm
        match HasseDavenport.castZ_zero_dvd _ a ha, hPa with
        | ⟨q, hq⟩, ⟨⟨e, he⟩, ⟨t, ht⟩⟩ =>
          have q0 : q = 0 := HasseDavenport.sq_zero q (CubeAp.small (2 * (2 * k + 1) + 1) a b q hab hq)
          rw [q0] at hq
          have he' : b = 2 * e := he
          have ht' : a + b - 1 = 4 * t := ht
          exact GaussInt.two_ne_one (e - 2 * t) (by rw [show 2 * (e - 2 * t) = 2 * e - 4 * t by lib_int, ← he', ← ht', hq]; lib_int)
    -- p ∤ A
    have hA : ¬ ∃ w, A = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * w := fun hex => match hex with
      | ⟨w, hw⟩ => by
        have z0 : castZ F.K (sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = F.K.zero := by
          rw [show (sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * (-2 * w) by rw [show (sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = -(-(sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els)) by lib_int, hSF, hw]; lib_int,
            HasseDavenport.castZ_mul, show castZ F.K (Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1)) = F.K.zero by
              show castN F.K _ = _; rw [castN_nsm]; exact hchar, DetChain.zero_mul']
        rw [z0] at hc1
        have hs : F.K.mul (pw F.K (F.K.neg F.K.one) (k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1) + 1)) (pw F.K (F.K.neg F.K.one) (k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1) + 1)) = F.K.one := by
          rw [← Frobenius.pw_mul, show F.K.mul (F.K.neg F.K.one) (F.K.neg F.K.one) = F.K.one by lib_ring, TwoPower.pwone]
        generalize pw F.K (F.K.neg F.K.one) (k + rep (2 * (2 * k + 1) + 1 + 1 + 1) (k + 1) N * (2 * (2 * k + 1) + 1 + 1 + 1) + 1) = s at hc1 hs
        have hY : pw F.K (nsm F.K (Frobenius.C (2 * k + 2) (k + 1)) F.K.one) (N + 1) = F.K.zero := by
          have h0 : F.K.mul (pw F.K (nsm F.K (Frobenius.C (2 * k + 2) (k + 1)) F.K.one) (N + 1)) s = F.K.zero := by
            rw [← DetChain.neg_neg' F.K (F.K.mul _ s), ← hc1]; exact (DetChain.neg_unique F.K _ _ (F.K.zero_add _)).symm
          generalize Frobenius.pw F.K (nsm F.K (C (2 * k + 2) (k + 1)) F.K.one) (N + 1) = Y at h0 ⊢
          calc Y = F.K.mul (F.K.mul Y s) s := by rw [F.K.mul_assoc, hs, F.K.mul_comm, F.K.one_mul]
            _ = F.K.zero := by rw [h0, DetChain.zero_mul']
        exact pw_ne_zero F.K F.nozd _ hX (N + 1) hY
    have hN : gnorm ⟨A, B⟩ = ipow (Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1)) (N + 1) := by
      show A * A + B * B = _
      rw [hAB, hh, P_eq k]; exact rep_pow k (N + 1)
    have hre : A = (gpow ⟨a, b⟩ (N + 1)).re := GaussPow.pow_primary (2 * (2 * k + 1) + 1) hpr a b hab hPa (N + 1) ⟨A, B⟩ hPA hN hA
    rw [hSF, hre]

end FinPow

#print axioms FinPow.rep_two
#print axioms FinPow.rep_pow
#print axioms FinPow.castN_nsm
#print axioms FinPow.pw_ne_zero
#print axioms FinPow.hr
#print axioms FinPow.P_eq
#print axioms FinPow.fin_pow

#print axioms: 7 theorems, no axioms.

78.28 \(a(\mathbb F_{p^N})\) for \(y^2=x^3+1\)

Over every finite field of order \(p^N\) (\(p\equiv 1\pmod 6\)) the trace of \(y^2=x^3+1\) is that of the Hecke \(L\)-factor of \(\mathbb Q(\sqrt {-3})\). \(-\sum _{z}\chi (z^3+1)=2\,\mathrm {Re}\,\pi ^N\), \(\pi =x+y\sqrt {-3}\), \(x\equiv 1\ (3)\).

lang/FinPow3.lean

/-
  lang/FinPow3 — T43, cycle 176: a(𝔽_q) = 2 Re π^N = α^N + β^N for y² = x³ + 1 over every finite field of order q = p^N,
  p = 6t + 7.

  Hypotheses on F: characteristic p, ker(ℤ → F) = pℤ, 2h + 1 = p^(N+1).
    rep_mul, rep_pow_gen     rep (c·a) = c·rep a;  2a + 1 = P ⇒ 2 rep a n + 1 = P^n
    fin_pow3                 ψ_F(1) = −2 Re π^(N+1), π = x + y√−3, x ≡ 1 (3), ψ_p(1) = −2x
  Proof: FinHasse3 + LucasIter: ψ_F ≡ −C(3t+3, 2t+2)^(N+1) ≢ 0 (mod p); FinCubicExact + mod 3: ψ_F = −2Z, Z ≡ 1 (3),
  Z² + 3W² = p^(N+1); EisenPow: Z = Re π^(N+1).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.FinPow2
import lang.EisenPow
import lang.CubeAp3

namespace FinPow3

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor EisenFactor LucasIter
open LibRing (castZ castN)

theorem rep_mul (P c a : Nat) : ∀ n, c * rep P a n = rep P (c * a) n
  | 0 => rfl
  | n + 1 => by
    show c * (a + rep P a n * P) = c * a + rep P (c * a) n * P
    rw [← rep_mul P c a n]; lib_nat

theorem rep_pow_gen (P a : Nat) (h : 2 * a + 1 = P) : ∀ n, Int.ofNat (2 * rep P a n + 1) = ipow (Int.ofNat P) n
  | 0 => rfl
  | n + 1 => by
    show Int.ofNat (2 * (a + rep P a n * P) + 1) = ipow (Int.ofNat P) n * Int.ofNat P
    rw [← rep_pow_gen P a h n]
    show _ = Int.ofNat ((2 * rep P a n + 1) * P)
    exact congrArg Int.ofNat (by rw [show 2 * (a + rep P a n * P) + 1 = (2 * a + 1) + 2 * rep P a n * P by lib_nat, h]; lib_nat)

theorem rep3 (P t : Nat) (n : Nat) : rep P (3 * t + 3) n = 3 * rep P (t + 1) n := by
  rw [rep_mul, show 3 * (t + 1) = 3 * t + 3 by lib_nat]

theorem rep2 (P t : Nat) (n : Nat) : rep P (2 * t + 2) n = 2 * rep P (t + 1) n := by
  rw [rep_mul, show 2 * (t + 1) = 2 * t + 2 by lib_nat]

variable {R : Type} [DecidableEq R]

theorem fin_pow3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) (F : FinField.FF R)
    (hchar : nsm F.K (2 * (3 * t + 2) + 1 + 1 + 1) F.K.one = F.K.zero)
    (hcast : ∀ z : Int, castZ F.K z = F.K.zero → ∃ w, z = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * w)
    (N : Nat) (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) (N + 1)) :
    ∃ x y : Int, x * x + 3 * (y * y) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) ∧ (∃ s, x = 3 * s + 1) ∧ (FinCubic.psi (FinField.ffp (3 * t + 2) hpr) (FinField.ffp (3 * t + 2) hpr).K.one) = -2 * x ∧
      (FinCubic.psi F F.K.one) = -2 * (epow ⟨x, y⟩ (N + 1)).re := by
  have hN1 : 2 * ((3 * t + 2) + 1) = 3 * (2 * t + 2) := by lib_nat
  have h3 := EisenAp.p_ne_three (3 * t + 2) (2 * t + 2) hN1
  have hh : F.h = rep (2 * (3 * t + 2) + 1 + 1 + 1) (3 * t + 3) (N + 1) := by
    apply CubeHasse3.two_cancel
    apply Int.ofNat.inj
    rw [hq, ← rep_pow_gen (2 * (3 * t + 2) + 1 + 1 + 1) (3 * t + 3) (by lib_nat) (N + 1)]
  have hN3 : 2 * F.h = 3 * (2 * rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) (N + 1)) := by rw [hh, rep3]; lib_nat
  match EisenAp.eisen_exists (3 * t + 2) hpr (2 * t + 2) hN1,
    FinCubicExact.fin_cubic_trace F _ hN3, FinCubicTrace.psi_one_mod3_ff F _ hN3 with
  | ⟨x, y, hxy, hx, hS1⟩, ⟨U, W, hUW, hpsi⟩, ⟨T, hT⟩ =>
    refine ⟨x, y, hxy, hx, hS1, ?_⟩
    -- Hasse over F
    have hk : F.h = 3 * (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) + 3 := by
      rw [hh]; show (3 * t + 3) + rep (2 * (3 * t + 2) + 1 + 1 + 1) (3 * t + 3) N * (2 * (3 * t + 2) + 1 + 1 + 1) = _; rw [rep3]; lib_nat
    have e1 : castZ F.K (FinCubic.psi F F.K.one) = F.K.neg (nsm F.K (Frobenius.C (3 * (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) + 3) (2 * (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) + 2)) F.K.one) := by
      show castZ F.K (sumK LibInt.intR (fun x => FinField.chi F (F.K.add (FinCubic.cube F x) F.K.one)) F.els) = _
      rw [FinCast.cast_sum_ff, hk]
      rw [sumK_ext F.K _ (fun x => Frobenius.pw F.K (F.K.add (Frobenius.pw F.K x 3) F.K.one) (3 * (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) + 3)) (fun x => by
        rw [show FinCubic.cube F x = Frobenius.pw F.K x 3 by
          show F.K.mul x (F.K.mul x x) = F.K.mul (F.K.mul (F.K.mul F.K.one x) x) x; lib_ring])]
      exact FinHasse3.fin_hasse3 F (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) hk
    rw [show 3 * (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) + 3 = rep (2 * (3 * t + 2) + 1 + 1 + 1) (3 * t + 3) (N + 1) by
        show _ = (3 * t + 3) + rep (2 * (3 * t + 2) + 1 + 1 + 1) (3 * t + 3) N * (2 * (3 * t + 2) + 1 + 1 + 1); rw [rep3]; lib_nat,
      show 2 * (t + rep (2 * (3 * t + 2) + 1 + 1 + 1) (t + 1) N * (2 * (3 * t + 2) + 1 + 1 + 1)) + 2 = rep (2 * (3 * t + 2) + 1 + 1 + 1) (2 * t + 2) (N + 1) by
        show _ = (2 * t + 2) + rep (2 * (3 * t + 2) + 1 + 1 + 1) (2 * t + 2) N * (2 * (3 * t + 2) + 1 + 1 + 1); rw [rep2]; lib_nat,
      lucas_rep F.K (2 * (3 * t + 2) + 1) hpr hchar (3 * t + 3) (2 * t + 2) (SplitHasse3.lt3 t) (SplitHasse3.lt3' t) (N + 1)] at e1
    have hcP : castZ F.K (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) = F.K.zero := by
      show castN F.K _ = _; rw [FinPow.castN_nsm]; exact hchar
    have hX : nsm F.K (Frobenius.C (3 * t + 3) (2 * t + 2)) F.K.one ≠ F.K.zero := fun e => by
      have e1' : castZ F.K (Int.ofNat (Frobenius.C (3 * t + 3) (2 * t + 2))) = F.K.zero := by
        show castN F.K _ = _; rw [FinPow.castN_nsm]; exact e
      match hcast _ e1' with
      | ⟨w, hw⟩ =>
        have e2 : nsm (ring (2 * (3 * t + 2) + 1 + 1)) (Frobenius.C (3 * t + 3) (2 * t + 2)) (ring (2 * (3 * t + 2) + 1 + 1)).one = (ring (2 * (3 * t + 2) + 1 + 1)).zero := by
          rw [← FinPow.castN_nsm]; show castZ (ring (2 * (3 * t + 2) + 1 + 1)) (Int.ofNat (Frobenius.C (3 * t + 3) (2 * t + 2))) = _
          rw [hw, HasseDavenport.castZ_mul, CubeAp.castP, DetChain.zero_mul']
        have e3 := SplitHasse3.cast_S1_3 t hpr
        rw [e2, ← CubeAp3.psi_same (3 * t + 2) hpr, hS1] at e3
        have e4 : castZ (ring (2 * (3 * t + 2) + 1 + 1)) (-2 * x) = (ring (2 * (3 * t + 2) + 1 + 1)).zero := e3.trans (DetChain.neg_unique (ring (2 * (3 * t + 2) + 1 + 1)) _ _ ((ring (2 * (3 * t + 2) + 1 + 1)).zero_add _)).symm
        have hx0 : castZ (ring (2 * (3 * t + 2) + 1 + 1)) x = (ring (2 * (3 * t + 2) + 1 + 1)).zero := by
          rw [show -2 * x = 2 * -x by lib_int, HasseDavenport.castZ_mul, CubeAp.castTwo, HasseDavenport.castZ_neg] at e4
          cases ZMod.nozd (2 * (3 * t + 2) + 1) hpr _ _ e4 with
          | inl h => exact absurd h (Eisen.two_ne_zero' (2 * (3 * t + 2) + 1) (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le _))))
          | inr h =>
            rw [← DetChain.neg_neg' (ring (2 * (3 * t + 2) + 1 + 1)) (castZ (ring (2 * (3 * t + 2) + 1 + 1)) x), h]
            exact (DetChain.neg_unique (ring (2 * (3 * t + 2) + 1 + 1)) _ _ ((ring (2 * (3 * t + 2) + 1 + 1)).zero_add _)).symm
        match HasseDavenport.castZ_zero_dvd _ x hx0, hx with
        | ⟨q, hq⟩, ⟨s, hs⟩ =>
          have q0 : q = 0 := HasseDavenport.sq_zero q (CubeAp3.small3 (2 * (3 * t + 2) + 1) x y q hxy hq)
          rw [q0] at hq
          exact EisenAp.three_ne_r (-s) 1 (fun e => Nat.noConfusion e) (Nat.lt_succ_of_lt (Nat.lt_succ_self 1))
            (by show _ = 1; rw [show 3 * -s = -(3 * s + 1) + 1 by lib_int, ← hs, hq]; lib_int)
    generalize (FinCubic.psi F F.K.one) = sF at e1 hpsi hT ⊢
    have hZ : ∃ Z : Int, sF = -2 * Z ∧ Z * Z = Int.ofNat U * Int.ofNat U := by
      cases hpsi with
      | inl h => exact ⟨-Int.ofNat U, by rw [h]; show 2 * Int.ofNat U = _; lib_int, by lib_int⟩
      | inr h => exact ⟨Int.ofNat U, by rw [h]; show -(2 * Int.ofNat U) = _; lib_int, rfl⟩
    match hZ with
    | ⟨Z, hZs, hZZ⟩ =>
    have hZ1 : ∃ s, Z = 3 * s + 1 := ⟨Z + T, by
      have : -2 * Z = 1 + 3 * T := hZs.symm.trans hT
      rw [show 3 * (Z + T) + 1 = 3 * Z + (1 + 3 * T) by lib_int, ← this]; lib_int⟩
    have hNorm : enorm ⟨Z, Int.ofNat W⟩ = ipow (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) (N + 1) := by
      show Z * Z + 3 * (Int.ofNat W * Int.ofNat W) = _
      rw [hZZ, ← hq]
      exact congrArg Int.ofNat hUW
    have hU : ¬ ∃ w, Z = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * w := fun hex => match hex with
      | ⟨w, hw⟩ => by
        have h0 : castZ F.K sF = F.K.zero := by
          rw [hZs, hw, show -2 * (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * w) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * (-2 * w) by lib_int,
            HasseDavenport.castZ_mul, hcP, DetChain.zero_mul']
        rw [h0] at e1
        have hY : Frobenius.pw F.K (nsm F.K (Frobenius.C (3 * t + 3) (2 * t + 2)) F.K.one) (N + 1) = F.K.zero := by
          rw [← DetChain.neg_neg' F.K (Frobenius.pw F.K _ _), ← e1]
          exact (DetChain.neg_unique F.K _ _ (F.K.zero_add _)).symm
        exact FinPow.pw_ne_zero F.K F.nozd _ hX (N + 1) hY
    have hre := EisenPow.epow_primary (2 * (3 * t + 2) + 1) hpr h3 x y hxy hx (N + 1) ⟨Z, Int.ofNat W⟩ hNorm hU hZ1
    have hre' : Z = (epow ⟨x, y⟩ (N + 1)).re := hre
    rw [hZs, hre']

end FinPow3

#print axioms FinPow3.rep_mul
#print axioms FinPow3.rep_pow_gen
#print axioms FinPow3.rep3
#print axioms FinPow3.rep2
#print axioms FinPow3.fin_pow3

#print axioms: 5 theorems, no axioms.

78.29 \(a(\mathbb F_{p^{2M}})\) at inert \(p\)

At inert \(p\equiv 3\pmod 4\) the trace over every field of order \(p^{2M}\) containing \(i\) is that of the factor \(1+pT^2\). \(-\sum _z\chi (z^3-z)=2(-p)^M=(i\sqrt p)^{2M}+(-i\sqrt p)^{2M}\).

lang/InertPow.lean

/-
  lang/InertPow — T43, cycle 178: y² = x³ − x over every field of order p^(2M) ∋ i at inert p ≡ 3 (mod 4): a = 2(−p)^M.

    inert_div      p | c² + d² ⇒ p | c and p | d        (−1 is not a square mod p: Euler, (p − 1)/2 odd)
    inert_desc     c² + d² = p^(2M) ⇒ c + di = p^M · unit
    inert_pow      over F with |F| = p^(2M), i ∈ F: −Σ χ(z³ − z) = 2(−p)^M      (FinPrimary: the primary unit is (−1)^M)
  α = i√p, β = −i√p: α^(2M) + β^(2M) = 2(−p)^M — the Hecke L-factor 1 + pT² at inert p governs every even-degree field.

  Lean 4 core only, proofs by hand (lib_int, lib_ring), no axioms.
-/
import lang.FinPow2

namespace InertPow

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor
open LibRing (castZ castN)

theorem inert_div (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (c d n : Int)
    (h : c * c + d * d = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * n) : (∃ u, c = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * u) ∧ (∃ v, d = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * v) := by
  have h0 : (ring (2 * (2 * j) + 1 + 1)).add ((ring (2 * (2 * j) + 1 + 1)).mul (castZ (ring (2 * (2 * j) + 1 + 1)) c) (castZ (ring (2 * (2 * j) + 1 + 1)) c)) ((ring (2 * (2 * j) + 1 + 1)).mul (castZ (ring (2 * (2 * j) + 1 + 1)) d) (castZ (ring (2 * (2 * j) + 1 + 1)) d)) = (ring (2 * (2 * j) + 1 + 1)).zero := by
    rw [← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul, ← IntCast.castZ_add, h, HasseDavenport.castZ_mul,
      CubeAp.castP, DetChain.zero_mul']
  generalize hCd : castZ (ring (2 * (2 * j) + 1 + 1)) c = C at h0
  generalize hDd : castZ (ring (2 * (2 * j) + 1 + 1)) d = D at h0
  have hCC : (ring (2 * (2 * j) + 1 + 1)).mul C C = (ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) ((ring (2 * (2 * j) + 1 + 1)).mul D D) := by
    rw [DetChain.neg_mul_left, (ring (2 * (2 * j) + 1 + 1)).one_mul]
    exact DetChain.neg_unique (ring (2 * (2 * j) + 1 + 1)) _ _ h0
  have hD : D = (ring (2 * (2 * j) + 1 + 1)).zero := by
    by_cases hD : D = (ring (2 * (2 * j) + 1 + 1)).zero
    · exact hD
    · exfalso
      have e1 := Euler.euler_square (2 * j) hpr D hD
      by_cases hC : C = (ring (2 * (2 * j) + 1 + 1)).zero
      · apply hD
        have h1 : (ring (2 * (2 * j) + 1 + 1)).mul D D = (ring (2 * (2 * j) + 1 + 1)).zero := by
          have h2 := h0
          rw [hC, DetChain.mul_zero, (ring (2 * (2 * j) + 1 + 1)).zero_add] at h2
          exact h2
        cases ZMod.nozd (2 * (2 * j) + 1) hpr _ _ h1 with
        | inl h => exact h
        | inr h => exact h
      · have e2 := Euler.euler_square (2 * j) hpr C hC
        rw [hCC, Frobenius.pw_mul, e1, TwoPower.negone_odd' (2 * (2 * j) + 1) j, (ring (2 * (2 * j) + 1 + 1)).mul_comm, (ring (2 * (2 * j) + 1 + 1)).one_mul] at e2
        exact QChar.ne_pm (2 * j) e2
  have hC : C = (ring (2 * (2 * j) + 1 + 1)).zero := by
    rw [hD, DetChain.mul_zero, DetChain.mul_zero] at hCC
    cases ZMod.nozd (2 * (2 * j) + 1) hpr _ _ hCC with
    | inl h => exact h
    | inr h => exact h
  exact ⟨HasseDavenport.castZ_zero_dvd (2 * (2 * j) + 1 + 1) c (hCd.trans hC), HasseDavenport.castZ_zero_dvd (2 * (2 * j) + 1 + 1) d (hDd.trans hD)⟩

theorem inert_desc (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) : ∀ (M : Nat) (c d : Int),
    c * c + d * d = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M) →
    ∃ c0 d0 : Int, c = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M * c0 ∧ d = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M * d0 ∧ c0 * c0 + d0 * d0 = 1
  | 0, c, d, h => ⟨c, d, by show c = 1 * c; lib_int, by show d = 1 * d; lib_int, h⟩
  | M + 1, c, d, h => by
    have h1 : c * c + d * d = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * (ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M) * Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) := by
      rw [h]; show ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M) * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = _; lib_int
    match inert_div j hpr c d _ h1 with
    | ⟨⟨c1, hc1⟩, ⟨d1, hd1⟩⟩ =>
      have h2 : c1 * c1 + d1 * d1 = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M) := by
        apply FermatUnique.cancelP (2 * (2 * j) + 1)
        apply FermatUnique.cancelP (2 * (2 * j) + 1)
        rw [show Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * (Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * (c1 * c1 + d1 * d1)) = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * c1 * (Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * c1) + Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * d1 * (Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * d1) by lib_int,
          ← hc1, ← hd1, h]
        show ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M) * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = _; lib_int
      match inert_desc j hpr M c1 d1 h2 with
      | ⟨c0, d0, e1, e2, e3⟩ =>
        refine ⟨c0, d0, ?_, ?_, e3⟩
        · rw [hc1, e1]; show _ = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * c0; lib_int
        · rw [hd1, e2]; show _ = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * d0; lib_int

theorem odd_pow (P : Int) (e : Int) (hP : P = 2 * e + 1) : ∀ M, ∃ f, ipow P M = 2 * f + 1
  | 0 => ⟨0, rfl⟩
  | M + 1 => match odd_pow P e hP M with
    | ⟨f, hf⟩ => ⟨2 * f * e + f + e, by show ipow P M * P = _; rw [hf, hP]; lib_int⟩

theorem one_mod4_pow (Q : Int) (e : Int) (hQ : Q = 4 * e + 1) : ∀ M, ∃ f, ipow Q M = 4 * f + 1
  | 0 => ⟨0, rfl⟩
  | M + 1 => match one_mod4_pow Q e hQ M with
    | ⟨f, hf⟩ => ⟨4 * f * e + f + e, by show ipow Q M * Q = _; rw [hf, hQ]; lib_int⟩

theorem ipow_neg_sq (P : Int) : ∀ M, ipow (-P) M * ipow (-P) M = ipow P (2 * M)
  | 0 => rfl
  | M + 1 => by
    show ipow (-P) M * -P * (ipow (-P) M * -P) = ipow P (2 * M) * P * P
    rw [← ipow_neg_sq P M]; lib_int

variable {R : Type} [DecidableEq R]

/-- **Inert p ≡ 3 (mod 4): a(F) = 2(−p)^M over every F of order p^(2M) containing i.** -/
theorem inert_pow (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (F : FinField.FF R) (M : Nat)
    (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (2 * M))
    (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) :
    -(sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = 2 * ipow (-Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M := by
  match FinPrimary.fin_ap_primary F key hkey i hi with
  | ⟨A, B, hAB, ⟨⟨e, he⟩, ⟨t, ht⟩⟩, hS⟩ =>
    rw [hS]
    have he' : B = 2 * e := he
    have ht' : A + B - 1 = 4 * t := ht
    match inert_desc j hpr M A B (hAB.trans hq), odd_pow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (Int.ofNat (2 * j + 1)) (by
        show Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = 2 * Int.ofNat (2 * j + 1) + 1; exact congrArg Int.ofNat (by lib_nat)) M with
    | ⟨c0, d0, e1, e2, e3⟩, ⟨f, hf⟩ =>
      have hu := norm_one ⟨c0, d0⟩ e3
      have hd0 : d0 = 0 := by
        rcases hu with h | h | h | h
        · exact congrArg GI.im h
        · exact congrArg GI.im h
        · exfalso
          have : d0 = 1 := congrArg GI.im h
          rw [this, hf, he'] at e2
          exact GaussInt.int_odd_ne_even f e (by rw [e2]; lib_int)
        · exfalso
          have : d0 = -1 := congrArg GI.im h
          rw [this, hf, he'] at e2
          exact GaussInt.int_odd_ne_even (-f - 1) e (by rw [e2]; lib_int)
      have hB : B = 0 := by rw [e2, hd0]; lib_int
      have hA2 : A * A = ipow (-Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M * ipow (-Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) M := by
        rw [ipow_neg_sq, ← hq, ← hAB, hB]; lib_int
      match one_mod4_pow (-Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (-Int.ofNat (j + 1)) (by
          show -Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = 4 * -Int.ofNat (j + 1) + 1
          have h4 : Int.ofNat (2 * (2 * j) + 1 + 1 + 1) + 1 = 4 * Int.ofNat (j + 1) := congrArg Int.ofNat (show (2 * (2 * j) + 1 + 1 + 1) + 1 = 4 * (j + 1) by lib_nat)
          rw [show -Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = -(Int.ofNat (2 * (2 * j) + 1 + 1 + 1) + 1) + 1 by lib_int, h4]; lib_int) M with
      | ⟨g, hg⟩ =>
        cases QuarticFamily.sq_eq A _ hA2 with
        | inl h => rw [h]
        | inr h =>
          exfalso
          rw [h, hg, hB] at ht'
          exact PrimaryUnique.four_bad (-g - t) (by rw [show 4 * (-g - t) = (-(4 * g + 1) + 0 - 1) - 4 * t + 2 by lib_int, ht']; lib_int)

end InertPow

#print axioms InertPow.inert_div
#print axioms InertPow.inert_desc
#print axioms InertPow.odd_pow
#print axioms InertPow.one_mod4_pow
#print axioms InertPow.ipow_neg_sq
#print axioms InertPow.inert_pow

#print axioms: 6 theorems, no axioms.

78.30 \(a(\mathbb F_{p^{2M}})\) for \(y^2=x^3+1\) at inert \(p\)

At inert \(p\equiv 2\pmod 3\), over every field of order \(p^{2M}\) the trace of \(y^2=x^3+1\) is that of \(1+pT^2\). \(-\sum _z\chi (z^3+1)=2(-p)^M\).

lang/InertPow3.lean

/-
  lang/InertPow3 — T43, cycle 180: y² = x³ + 1 over every field of order p^(2M) at inert p ≡ 2 (mod 3), p = 6t + 5:
  a = 2(−p)^M.

    not_three_dvd   3 ∤ p − 1
    inert_div3      p | U² + 3W² ⇒ p | U, W         (−3 is a non-square mod p: Eisen.inert_neg3)
    inert_desc3     U² + 3W² = p^(2M) ⇒ U = ±p^M, W = 0
    inert_pow3      ψ_F(1) = −2(−p)^M                (FinCubicExact; ψ ≡ 1 mod 3 and (−p)^M ≡ 1 mod 3 fix the sign)

  Lean 4 core only, proofs by hand (lib_int, lib_ring), no axioms.
-/
import lang.InertPow
import lang.FinPow3

namespace InertPow3

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor EisenFactor
open LibRing (castZ castN)

theorem not_three_dvd (t : Nat) (h : Dv (1 + 2) (2 * (3 * t + 1) + 1 + 1)) : False := by
  match h with
  | ⟨q, hq⟩ =>
    apply EisenAp.three_ne_r (Int.ofNat q - 2 * Int.ofNat t - 1) 1 (fun e => Nat.noConfusion e) (Nat.lt_succ_of_lt (Nat.lt_succ_self 1))
    have e : Int.ofNat (6 * t + 4) = Int.ofNat (q * 3) := congrArg Int.ofNat ((show 6 * t + 4 = 2 * (3 * t + 1) + 1 + 1 by lib_nat).trans hq)
    show _ = 1
    have e1 : 6 * Int.ofNat t + 4 = Int.ofNat q * 3 := e
    rw [show 3 * (Int.ofNat q - 2 * Int.ofNat t - 1) = Int.ofNat q * 3 - (6 * Int.ofNat t + 4) + 1 by lib_int, ← e1]; lib_int

theorem castThree {R : Type} (K : DetChain.CRing R) : castZ K 3 = K.add K.one (K.add K.one K.one) := by
  show K.add K.one (K.add K.one (K.add K.one K.zero)) = _
  rw [DetChain.add_zero']

theorem inert_div3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 1) + 1 + 1 + 1)) (c d n : Int)
    (h : c * c + 3 * (d * d) = Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * n) : (∃ u, c = Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * u) ∧ (∃ v, d = Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * v) := by
  have h0 : (ring (2 * (3 * t + 1) + 1 + 1)).add ((ring (2 * (3 * t + 1) + 1 + 1)).mul (castZ (ring (2 * (3 * t + 1) + 1 + 1)) c) (castZ (ring (2 * (3 * t + 1) + 1 + 1)) c)) ((ring (2 * (3 * t + 1) + 1 + 1)).mul (Eisen.three (2 * (3 * t + 1) + 1)) ((ring (2 * (3 * t + 1) + 1 + 1)).mul (castZ (ring (2 * (3 * t + 1) + 1 + 1)) d) (castZ (ring (2 * (3 * t + 1) + 1 + 1)) d))) = (ring (2 * (3 * t + 1) +  -- (+19 chars; full line in the Zenodo source)
    rw [show Eisen.three (2 * (3 * t + 1) + 1) = castZ (ring (2 * (3 * t + 1) + 1 + 1)) 3 from (castThree (ring (2 * (3 * t + 1) + 1 + 1))).symm,
      ← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul, ← HasseDavenport.castZ_mul, ← IntCast.castZ_add, h,
      HasseDavenport.castZ_mul, CubeAp.castP, DetChain.zero_mul']
  generalize hCd : castZ (ring (2 * (3 * t + 1) + 1 + 1)) c = C at h0
  generalize hDd : castZ (ring (2 * (3 * t + 1) + 1 + 1)) d = D at h0
  have hD : D = (ring (2 * (3 * t + 1) + 1 + 1)).zero := by
    by_cases hD : D = (ring (2 * (3 * t + 1) + 1 + 1)).zero
    · exact hD
    · exfalso
      have hinv := ZMod.inv_exists (2 * (3 * t + 1) + 1) hpr D hD
      generalize Frobenius.pw (ring (2 * (3 * t + 1) + 1 + 1)) D (2 * (3 * t + 1) + 1) = Di at hinv
      apply not_three_dvd t
      apply Eisen.inert_neg3 (2 * (3 * t + 1) + 1) hpr (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le _))) ((ring (2 * (3 * t + 1) + 1 + 1)).mul C Di)
      have hCC : (ring (2 * (3 * t + 1) + 1 + 1)).mul C C = (ring (2 * (3 * t + 1) + 1 + 1)).neg ((ring (2 * (3 * t + 1) + 1 + 1)).mul (Eisen.three (2 * (3 * t + 1) + 1)) ((ring (2 * (3 * t + 1) + 1 + 1)).mul D D)) := DetChain.neg_unique (ring (2 * (3 * t + 1) + 1 + 1)) _ _ h0
      rw [show (ring (2 * (3 * t + 1) + 1 + 1)).mul ((ring (2 * (3 * t + 1) + 1 + 1)).mul C Di) ((ring (2 * (3 * t + 1) + 1 + 1)).mul C Di) = (ring (2 * (3 * t + 1) + 1 + 1)).mul ((ring (2 * (3 * t + 1) + 1 + 1)).mul C C) ((ring (2 * (3 * t + 1) + 1 + 1)).mul Di Di) by lib_ring, hCC,
        show (ring (2 * (3 * t + 1) + 1 + 1)).mul ((ring (2 * (3 * t + 1) + 1 + 1)).neg ((ring (2 * (3 * t + 1) + 1 + 1)).mul (Eisen.three (2 * (3 * t + 1) + 1)) ((ring (2 * (3 * t + 1) + 1 + 1)).mul D D))) ((ring (2 * (3 * t + 1) + 1 + 1)).mul Di Di)
          = (ring (2 * (3 * t + 1) + 1 + 1)).neg ((ring (2 * (3 * t + 1) + 1 + 1)).mul (Eisen.three (2 * (3 * t + 1) + 1)) ((ring (2 * (3 * t + 1) + 1 + 1)).mul ((ring (2 * (3 * t + 1) + 1 + 1)).mul Di D) ((ring (2 * (3 * t + 1) + 1 + 1)).mul Di D))) by lib_ring, hinv]
      rw [show (ring (2 * (3 * t + 1) + 1 + 1)).mul (ring (2 * (3 * t + 1) + 1 + 1)).one (ring (2 * (3 * t + 1) + 1 + 1)).one = (ring (2 * (3 * t + 1) + 1 + 1)).one from (ring (2 * (3 * t + 1) + 1 + 1)).one_mul _, (ring (2 * (3 * t + 1) + 1 + 1)).mul_comm, (ring (2 * (3 * t + 1) + 1 + 1)).one_mul]
  have hC : C = (ring (2 * (3 * t + 1) + 1 + 1)).zero := by
    rw [hD, DetChain.mul_zero, DetChain.mul_zero, DetChain.add_zero'] at h0
    cases ZMod.nozd (2 * (3 * t + 1) + 1) hpr _ _ h0 with
    | inl h => exact h
    | inr h => exact h
  exact ⟨HasseDavenport.castZ_zero_dvd _ c (hCd.trans hC), HasseDavenport.castZ_zero_dvd _ d (hDd.trans hD)⟩

theorem inert_desc3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 1) + 1 + 1 + 1)) : ∀ (M : Nat) (c d : Int),
    c * c + 3 * (d * d) = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M) →
    ∃ c0 d0 : Int, c = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M * c0 ∧ d = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M * d0 ∧ c0 * c0 + 3 * (d0 * d0) = 1
  | 0, c, d, h => ⟨c, d, by show c = 1 * c; lib_int, by show d = 1 * d; lib_int, h⟩
  | M + 1, c, d, h => by
    have h1 : c * c + 3 * (d * d) = Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * (ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) := by
      rw [h]; show ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) = _; lib_int
    match inert_div3 t hpr c d _ h1 with
    | ⟨⟨c1, hc1⟩, ⟨d1, hd1⟩⟩ =>
      have h2 : c1 * c1 + 3 * (d1 * d1) = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M) := by
        apply FermatUnique.cancelP (2 * (3 * t + 1) + 1)
        apply FermatUnique.cancelP (2 * (3 * t + 1) + 1)
        rw [show Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * (c1 * c1 + 3 * (d1 * d1))) = Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * c1 * (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * c1) + 3 * (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * d1 * (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * d1)) by lib_int,
          ← hc1, ← hd1, h]
        show ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) = _; lib_int
      match inert_desc3 t hpr M c1 d1 h2 with
      | ⟨c0, d0, e1, e2, e3⟩ =>
        refine ⟨c0, d0, ?_, ?_, e3⟩
        · rw [hc1, e1]; show _ = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * c0; lib_int
        · rw [hd1, e2]; show _ = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * d0; lib_int

theorem one_mod3_pow (Q e : Int) (hQ : Q = 3 * e + 1) : ∀ M, ∃ f, ipow Q M = 3 * f + 1
  | 0 => ⟨0, rfl⟩
  | M + 1 => match one_mod3_pow Q e hQ M with
    | ⟨f, hf⟩ => ⟨3 * f * e + f + e, by show ipow Q M * Q = _; rw [hf, hQ]; lib_int⟩

theorem ipow_two (P : Int) : ∀ M, ipow P (2 * M) = ipow (P * P) M
  | 0 => rfl
  | M + 1 => by
    show ipow P (2 * M) * P * P = ipow (P * P) M * (P * P)
    rw [ipow_two P M]; lib_int

variable {R : Type} [DecidableEq R]

/-- **Inert p ≡ 2 (mod 3): a(F) = 2(−p)^M for y² = x³ + 1 over every F of order p^(2M).** -/
theorem inert_pow3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 1) + 1 + 1 + 1)) (F : FinField.FF R) (M : Nat)
    (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M)) :
    (FinCubic.psi F F.K.one) = -2 * ipow (-Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M := by
  have hN : ∃ N, 2 * F.h = 3 * N := by
    match one_mod3_pow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (Int.ofNat (12 * t * t + 20 * t + 8)) (by
        show Int.ofNat ((2 * (3 * t + 1) + 1 + 1 + 1) * (2 * (3 * t + 1) + 1 + 1 + 1)) = Int.ofNat (3 * (12 * t * t + 20 * t + 8) + 1)
        exact congrArg Int.ofNat (by lib_nat)) M with
    | ⟨f, hf⟩ =>
      rw [← ipow_two, ← hq] at hf
      have e : Int.ofNat (2 * F.h) = 3 * f := by
        have h1 : Int.ofNat (2 * F.h) + 1 = 3 * f + 1 := hf
        rw [show Int.ofNat (2 * F.h) = (Int.ofNat (2 * F.h) + 1) + -1 by lib_int, h1]; lib_int
      match EisenUnique.nat_of_three (2 * F.h) f e with
      | ⟨q, hq'⟩ => exact ⟨q, by rw [hq']; lib_nat⟩
  match hN with
  | ⟨N, hN⟩ =>
  match FinCubicExact.fin_cubic_trace F N hN, FinCubicTrace.psi_one_mod3_ff F N hN with
  | ⟨U, W, hUW, hpsi⟩, ⟨T, hT⟩ =>
    generalize FinCubic.psi F F.K.one = sF at hpsi hT ⊢
    have hZ : ∃ Z : Int, sF = -2 * Z ∧ Z * Z = Int.ofNat U * Int.ofNat U := by
      cases hpsi with
      | inl h => exact ⟨-Int.ofNat U, by rw [h]; show 2 * Int.ofNat U = _; lib_int, by lib_int⟩
      | inr h => exact ⟨Int.ofNat U, by rw [h]; show -(2 * Int.ofNat U) = _; lib_int, rfl⟩
    match hZ with
    | ⟨Z, hZs, hZZ⟩ =>
    have hZ1 : ∃ s, Z = 3 * s + 1 := ⟨Z + T, by
      have : -2 * Z = 1 + 3 * T := hZs.symm.trans hT
      rw [show 3 * (Z + T) + 1 = 3 * Z + (1 + 3 * T) by lib_int, ← this]; lib_int⟩
    have hnorm : Z * Z + 3 * (Int.ofNat W * Int.ofNat W) = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (2 * M) := by
      rw [hZZ, ← hq]; exact congrArg Int.ofNat hUW
    match inert_desc3 t hpr M Z (Int.ofNat W) hnorm with
    | ⟨c0, d0, e1, e2, e3⟩ =>
      have hd0 : d0 = 0 := by
        cases enorm_one ⟨c0, d0⟩ e3 with
        | inl h => exact congrArg GI.im h
        | inr h => exact congrArg GI.im h
      have hW : Int.ofNat W = 0 := by rw [e2, hd0]; lib_int
      have hZ2 : Z * Z = ipow (-Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M * ipow (-Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M := by
        rw [InertPow.ipow_neg_sq, ← hnorm, hW]; lib_int
      have hQ1 : ∃ f, ipow (-Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) M = 3 * f + 1 :=
        one_mod3_pow (-Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (-Int.ofNat (2 * t + 2)) (by
          have h4 : Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) + 1 = 3 * Int.ofNat (2 * t + 2) := congrArg Int.ofNat (show (2 * (3 * t + 1) + 1 + 1 + 1) + 1 = 3 * (2 * t + 2) by lib_nat)
          rw [show -Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) = -(Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) + 1) + 1 by lib_int, h4]; lib_int) M
      rw [hZs, EisenPow.sign_ok Z _ hZ1 hQ1 (QuarticFamily.sq_eq Z _ hZ2)]

end InertPow3

#print axioms InertPow3.not_three_dvd
#print axioms InertPow3.castThree
#print axioms InertPow3.inert_div3
#print axioms InertPow3.inert_desc3
#print axioms InertPow3.one_mod3_pow
#print axioms InertPow3.ipow_two
#print axioms InertPow3.inert_pow3

#print axioms: 7 theorems, no axioms.

78.31 The local zeta function of \(y^2=x^3-x\)

At every odd prime \(p\) and over every field of order \(p^N\) of characteristic \(p\), the trace is \(s_N\) with \(s_{N+2}=a_ps_{N+1}-ps_N\). \(Z(E/\mathbb F_p,T)=\dfrac {1-a_pT+pT^2}{(1-T)(1-pT)}\).

lang/ZetaAll.lean

/-
  lang/ZetaAll — T43, cycle 190: the local zeta function of y² = x³ − x at every odd prime p, over every field of order p^N.

    s A P N         s_0 = 2, s_1 = A, s_{N+2} = A s_{N+1} − P s_N   (s_N = α^N + β^N, α + β = A, αβ = P)
    split_seq       2 Re π^N = s(2a, a² + b²) N
    inert_seq       s(0, P) (2M) = 2(−P)^M, s(0, P) (2M + 1) = 0
    zeta_all        for every odd prime p and every F of order p^(N+1) with characteristic p (ker ℤ → F = pℤ):
                    a(F) = s(a_p, p)(N + 1),   a_p = −Σ_{𝔽_p} χ(x³ − x)
  i.e. Z(E/𝔽_p, T) = (1 − a_p T + pT²)/((1 − T)(1 − pT)) on every such field — the GL2 L-factor of the CM form at p.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.SplitAll
import lang.InertAll
import lang.ZetaRec
import lang.EveryPrime

namespace ZetaAll

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor
open LibRing (castZ castN)

def s (A P : Int) : Nat → Int
  | 0 => 2
  | 1 => A
  | n + 2 => A * s A P (n + 1) - P * s A P n

theorem split_seq (a b : Int) : ∀ N, 2 * (gpow ⟨a, b⟩ N).re = s (2 * a) (a * a + b * b) N
  | 0 => rfl
  | 1 => by show 2 * (1 * a - 0 * b) = 2 * a; lib_int
  | N + 2 => by
    rw [ZetaRec.trace_rec, split_seq a b (N + 1), split_seq a b N]; rfl

theorem inert_seq (P : Int) : ∀ M, s 0 P (2 * M) = 2 * ipow (-P) M ∧ s 0 P (2 * M + 1) = 0
  | 0 => ⟨by show (2 : Int) = 2 * 1; rfl, rfl⟩
  | M + 1 => by
    have ih := inert_seq P M
    constructor
    · show 0 * s 0 P (2 * M + 1) - P * s 0 P (2 * M) = 2 * (ipow (-P) M * -P)
      rw [ih.1, ih.2]; lib_int
    · show 0 * s 0 P (2 * M + 1 + 1) - P * s 0 P (2 * M + 1) = 0
      rw [ih.2]; lib_int

variable {R : Type} [DecidableEq R]

/-- **The local zeta function of y² = x³ − x at every odd prime, over every field of order p^(N+1).** -/
theorem zeta_all (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = (2 * m + 1 + 1 + 1)) (F : FinField.FF R)
    (hchar : nsm F.K (2 * m + 1 + 1 + 1) F.K.one = F.K.zero)
    (hcast : ∀ z : Int, castZ F.K z = F.K.zero → ∃ w, z = Int.ofNat (2 * m + 1 + 1 + 1) * w)
    (N : Nat) (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * m + 1 + 1 + 1)) (N + 1))
    (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) :
    -(sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els) = s (-(sumK LibInt.intR (fun x => QChar.chi m (CMInert.cub (ring (2 * m + 1 + 1)) x)) (ConicSum.L m))) (Int.ofNat (2 * m + 1 + 1 + 1)) (N + 1) := by
  cases EveryPrime.nat_even_odd m with
  | inl h =>
    match h with
    | ⟨j, hj⟩ =>
      subst hj
      rw [InertFq.s1_inert j hpr, show (-0 : Int) = 0 from rfl]
      cases EveryPrime.nat_even_odd (N + 1) with
      | inl h2 =>
        match h2 with
        | ⟨M, hM⟩ =>
          rw [hM] at hq ⊢
          rw [InertAll.inert_even j hpr F M hq key hkey, (inert_seq _ M).1]
      | inr h2 =>
        match h2 with
        | ⟨M, hM⟩ =>
          rw [hM] at hq ⊢
          rw [InertAll.inert_odd j F M hq, (inert_seq _ M).2]
  | inr h =>
    match h with
    | ⟨k, hk⟩ =>
      subst hk
      match SplitAll.split_all k hpr F hchar hcast N hq key hkey with
      | ⟨a, b, hab, _, hS1, hSF⟩ =>
        rw [hSF, hS1, ← hab, split_seq]

end ZetaAll

#print axioms ZetaAll.s
#print axioms ZetaAll.split_seq
#print axioms ZetaAll.inert_seq
#print axioms ZetaAll.zeta_all

#print axioms: 4 theorems, no axioms.

78.32 The local zeta function of \(y^2=x^3+1\)

At every prime \(p>3\) and over every field of order \(p^N\) of characteristic \(p\), the trace of \(y^2=x^3+1\) is \(s_N(a_p,p)\). \(Z(E/\mathbb F_p,T)=\dfrac {1-a_pT+pT^2}{(1-T)(1-pT)}\).

lang/ZetaAll3.lean

/-
  lang/ZetaAll3 — T43, cycle 191: the local zeta function of y² = x³ + 1 at every prime p > 3, over every field of order p^N.

    mod3_cases     m = 3t, 3t + 1 or 3t + 2
    split_seq3     2 Re π^N = s(2x, x² + 3y²) N        (ℤ[√−3])
    psi_p_inert    ψ_p(1) = 0 at p ≡ 2 (mod 3)
    zeta_all3      for p = 2m + 3 > 3 prime and F of order p^(N+1), characteristic p, ker(ℤ → F) = pℤ:
                   −ψ_F(1) = s(a_p, p)(N + 1),   a_p = −ψ_p(1)

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.ZetaAll
import lang.FinPow3
import lang.InertPow3
import lang.InertOdd3

namespace ZetaAll3

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor EisenFactor ZetaAll
open LibRing (castZ castN)

theorem mod3_cases : ∀ m : Nat, (∃ t, m = 3 * t) ∨ (∃ t, m = 3 * t + 1) ∨ (∃ t, m = 3 * t + 2)
  | 0 => Or.inl ⟨0, rfl⟩
  | m + 1 => by
    rcases mod3_cases m with ⟨t, ht⟩ | ⟨t, ht⟩ | ⟨t, ht⟩
    · exact Or.inr (Or.inl ⟨t, by rw [ht]⟩)
    · exact Or.inr (Or.inr ⟨t, by rw [ht]⟩)
    · exact Or.inl ⟨t + 1, by rw [ht]; lib_nat⟩

theorem split_seq3 (x y : Int) : ∀ N, 2 * (epow ⟨x, y⟩ N).re = s (2 * x) (x * x + 3 * (y * y)) N
  | 0 => rfl
  | 1 => by show 2 * (1 * x - 3 * (0 * y)) = 2 * x; lib_int
  | N + 2 => by
    rw [ZetaRec.trace_rec3, split_seq3 x y (N + 1), split_seq3 x y N]; rfl

theorem psi_p_inert (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (3 * t + 1) + 1 + 1 + 1) :
    FinCubic.psi (FinField.ffp (3 * t + 1) hpr) (FinField.ffp (3 * t + 1) hpr).K.one = 0 :=
  InertOdd3.psi_zero_q2 (FinField.ffp (3 * t + 1) hpr) (4 * t + 3) (show 3 * (4 * t + 3) = 4 * (3 * t + 1 + 1) + 1 by lib_nat)

variable {R : Type} [DecidableEq R]

/-- **The local zeta function of y² = x³ + 1 at every prime p > 3.** -/
theorem zeta_all3 (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = (2 * m + 1 + 1 + 1)) (h3 : (2 * m + 1 + 1 + 1) ≠ 3) (F : FinField.FF R)
    (hchar : nsm F.K (2 * m + 1 + 1 + 1) F.K.one = F.K.zero)
    (hcast : ∀ z : Int, castZ F.K z = F.K.zero → ∃ w, z = Int.ofNat (2 * m + 1 + 1 + 1) * w)
    (N : Nat) (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * m + 1 + 1 + 1)) (N + 1)) :
    -(FinCubic.psi F F.K.one) = s (-(FinCubic.psi (FinField.ffp m hpr) (FinField.ffp m hpr).K.one)) (Int.ofNat (2 * m + 1 + 1 + 1)) (N + 1) := by
  rcases mod3_cases m with ⟨t, ht⟩ | ⟨t, ht⟩ | ⟨t, ht⟩
  · exfalso
    subst ht
    cases hpr 3 ⟨2 * t + 1, by lib_nat⟩ with
    | inl e => exact Nat.noConfusion (Nat.succ.inj e)
    | inr e =>
      cases t with
      | zero => exact h3 rfl
      | succ t => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show 2 * (3 * (t + 1)) + 1 + 1 + 1 = (6 * t + 5) + 1 + 1 + 1 + 1 by lib_nat).symm.trans e.symm))))
  · subst ht
    rw [psi_p_inert t hpr, show (-0 : Int) = 0 from rfl]
    cases EveryPrime.nat_even_odd (N + 1) with
    | inl h2 =>
      match h2 with
      | ⟨M, hM⟩ =>
        rw [hM] at hq ⊢
        rw [InertPow3.inert_pow3 t hpr F M hq, (inert_seq _ M).1]; lib_int
    | inr h2 =>
      match h2 with
      | ⟨M, hM⟩ =>
        rw [hM] at hq ⊢
        rw [InertOdd3.inert_odd3 F t M hq, (inert_seq _ M).2]; rfl
  · subst ht
    match FinPow3.fin_pow3 t hpr F hchar hcast N hq with
    | ⟨x, y, hxy, _, hS1, hSF⟩ =>
      rw [hSF, hS1, ← hxy, show -(-2 * x) = 2 * x by lib_int, ← split_seq3]; lib_int

end ZetaAll3

#print axioms ZetaAll3.mod3_cases
#print axioms ZetaAll3.split_seq3
#print axioms ZetaAll3.psi_p_inert
#print axioms ZetaAll3.zeta_all3

#print axioms: 4 theorems, no axioms.

78.33 Theta coefficients at \(p^k\)

The primary elements of norm \(p^k\) are exactly \(\pi ^j\bar \pi ^{k-j}\), and their real parts sum to \(c_k\) with the Hecke recurrence. \(c_0=1,\ c_1=a_p,\ c_{k+2}=a_pc_{k+1}-pc_k\).

lang/ThetaCoef.lean

/-
  lang/ThetaCoef — T43, cycle 196: the coefficients of the theta series θ_ψ at p^k (ψ the Hecke character of ℚ(i)).

  π = a + bi primary of norm p. The primary elements of norm p^k are exactly π^j π̄^(k−j), j = 0..k:
    primary_list     π^j π̄^l is primary of norm p^(j+l)
    primary_only     every primary α with N(α) = p^k is some π^j π̄^l, j + l = k        (GaussFactor + GaussCube.unit_primary)
    T, T_rec, cT_init  c_k = Re T_k = Σ_{j=0..k} Re(π^j π̄^(k−j)) satisfies c_0 = 1, c_1 = 2a, c_{k+2} = 2a c_{k+1} − p c_k
  so the p-part of L(θ_ψ, s) is 1/(1 − a_p p^(−s) + p^(1−2s)), the L-factor of E: y² = x³ − x (ZetaAll).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.GaussPow
import lang.ZetaAll

namespace ThetaCoef

open FTAChain LibInt GaussInt GaussFactor GaussCube GaussPow

theorem gnorm_pow (x : GI) : ∀ n, gnorm (gpow x n) = ipow (gnorm x) n
  | 0 => by show (1 : Int) * 1 + 0 * 0 = 1; rfl
  | n + 1 => by
    show gnorm (gmul (gpow x n) x) = ipow (gnorm x) n * gnorm x
    rw [norm_mul, gnorm_pow x n]

theorem primary_list (a b : Int) (hπ : Primary ⟨a, b⟩) (j l : Nat) :
    Primary (gmul (gpow ⟨a, b⟩ j) (gpow ⟨a, -b⟩ l)) ∧
      gnorm (gmul (gpow ⟨a, b⟩ j) (gpow ⟨a, -b⟩ l)) = ipow (a * a + b * b) (j + l) := by
  refine ⟨prim_mul _ _ (prim_pow _ hπ j) (prim_pow _ (prim_conj ⟨a, b⟩ hπ) l), ?_⟩
  rw [norm_mul, gnorm_pow, gnorm_pow, FinPow2.ipow_add]
  show ipow (a * a + b * b) j * ipow (a * a + -b * -b) l = _
  rw [show a * a + -b * -b = a * a + b * b by lib_int]

theorem primary_only (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (a b : Int)
    (hab : a * a + b * b = Int.ofNat (p + 1 + 1)) (hπ : Primary ⟨a, b⟩) (k : Nat) (x : GI) (hx : Primary x)
    (hN : gnorm x = ipow (Int.ofNat (p + 1 + 1)) k) :
    ∃ j l, j + l = k ∧ x = gmul (gpow ⟨a, b⟩ j) (gpow ⟨a, -b⟩ l) := by
  match factor p hp a b hab k x hN with
  | ⟨u, j, l, hu, hjl, hx'⟩ =>
    have hc := (primary_list a b hπ j l).1
    have h1 := unit_primary u _ hu hc (by rw [← hx']; exact hx)
    exact ⟨j, l, hjl, by rw [hx', h1, gone_mul]⟩

/-- c_k = Σ_{j=0..k} Re(π^j π̄^(k−j)), via the ℤ[i]-valued sum T_k. -/
def T (a b : Int) : Nat → GI
  | 0 => gone
  | k + 1 => gmul ⟨a, b⟩ (T a b k) |> fun t => ⟨t.re + (gpow ⟨a, -b⟩ (k + 1)).re, t.im + (gpow ⟨a, -b⟩ (k + 1)).im⟩

theorem T_rec (a b : Int) (k : Nat) :
    (T a b (k + 2)).re = 2 * a * (T a b (k + 1)).re - (a * a + b * b) * (T a b k).re := by
  show (gmul ⟨a, b⟩ (T a b (k + 1))).re + (gmul (gpow ⟨a, -b⟩ (k + 1)) ⟨a, -b⟩).re = 2 * a * (T a b (k + 1)).re - (a * a + b * b) * (T a b k).re
  show a * (T a b (k + 1)).re - b * (T a b (k + 1)).im + ((gpow ⟨a, -b⟩ (k + 1)).re * a - (gpow ⟨a, -b⟩ (k + 1)).im * -b) = _
  show a * ((gmul ⟨a, b⟩ (T a b k)).re + (gpow ⟨a, -b⟩ (k + 1)).re) - b * ((gmul ⟨a, b⟩ (T a b k)).im + (gpow ⟨a, -b⟩ (k + 1)).im)
    + ((gpow ⟨a, -b⟩ (k + 1)).re * a - (gpow ⟨a, -b⟩ (k + 1)).im * -b)
    = 2 * a * ((gmul ⟨a, b⟩ (T a b k)).re + (gpow ⟨a, -b⟩ (k + 1)).re) - (a * a + b * b) * (T a b k).re
  show a * ((a * (T a b k).re - b * (T a b k).im) + (gpow ⟨a, -b⟩ (k + 1)).re) - b * ((a * (T a b k).im + b * (T a b k).re) + (gpow ⟨a, -b⟩ (k + 1)).im)
    + ((gpow ⟨a, -b⟩ (k + 1)).re * a - (gpow ⟨a, -b⟩ (k + 1)).im * -b)
    = 2 * a * ((a * (T a b k).re - b * (T a b k).im) + (gpow ⟨a, -b⟩ (k + 1)).re) - (a * a + b * b) * (T a b k).re
  lib_int

/-- the coefficients c_k = Re T_k: c_0 = 1, c_1 = 2a, c_{k+2} = 2a c_{k+1} − p c_k (the L-factor 1/(1 − a_p X + p X²)). -/
theorem cT_init (a b : Int) : (T a b 0).re = 1 ∧ (T a b 1).re = 2 * a := by
  refine ⟨rfl, ?_⟩
  show a * 1 - b * 0 + (1 * a - 0 * -b) = 2 * a
  lib_int

end ThetaCoef

#print axioms ThetaCoef.gnorm_pow
#print axioms ThetaCoef.primary_list
#print axioms ThetaCoef.primary_only
#print axioms ThetaCoef.T
#print axioms ThetaCoef.T_rec
#print axioms ThetaCoef.cT_init

#print axioms: 6 theorems, no axioms.

78.34 Reciprocity for the two CM curves

For every finite field \(F\) of characteristic \(p>3\) the trace of Frobenius equals the Hecke data: \(a(F)=s_N(\psi (\pi )+\psi (\bar \pi ),p)\) at split \(p\), \(s_N(0,p)\) at inert \(p\). \(\#E(F)=|F|+1-s_N\), \(s_N=\alpha ^N+\beta ^N\), \(\alpha +\beta =a_p\), \(\alpha \beta =p\).

lang/CMReciprocity.lean

/-
  lang/CMReciprocity — T43, cycle 205: reciprocity for the two CM elliptic curves over ℚ, Hecke side ⇄ point counts.

  For a finite field F of the interface with characteristic p, ker(ℤ → F) = pℤ and |F| = p^(N+1):
    cm_gauss_split   y² = x³ − x, p ≡ 1 (4), π = c + di any primary of norm p:   a(F) = s_{N+1}(ψ(π) + ψ(π̄), p) = s_{N+1}(2c, p)
    cm_gauss_inert   y² = x³ − x, p ≡ 3 (4):                                       a(F) = s_{N+1}(0, p)
    cm_eisen_split   y² = x³ + 1, p ≡ 1 (6), π = x + y√−3 (x ≡ 1 mod 3) of norm p:   a(F) = s_{N+1}(2x, p)
    cm_eisen_inert   y² = x³ + 1, p ≡ 5 (6):                                       a(F) = s_{N+1}(0, p)
  s_n(A, P) = α^n + β^n with α + β = A, αβ = P: the Frobenius traces of E over every such F are the Hecke eigenvalues of θ_ψ
  (ThetaCoef) — statement (A) for these two CM forms, at every prime p > 3 and every finite field of characteristic p.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.ZetaAll3
import lang.GaussAp
import lang.EisenAp

namespace CMReciprocity

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor ZetaAll
open LibRing (castZ castN)

theorem bridge (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK LibInt.intR (fun x => FinField.chi (FinField.ffp m hpr) (FinSign.f (FinField.ffp m hpr) x)) (FinField.ffp m hpr).els
      = sumK LibInt.intR (fun x => QChar.chi m (CMInert.cub (ring (2 * m + 1 + 1)) x)) (ConicSum.L m) := rfl

variable {R : Type} [DecidableEq R]

theorem cm_gauss_split (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * j + 1) + 1 + 1 + 1)
    (c d : Int) (hP : Primary ⟨c, d⟩) (hcd : c * c + d * d = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1))
    (F : FinField.FF R) (hchar : nsm F.K (2 * (2 * j + 1) + 1 + 1 + 1) F.K.one = F.K.zero)
    (hcast : ∀ z : Int, castZ F.K z = F.K.zero → ∃ w, z = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) * w)
    (N : Nat) (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1)) (N + 1))
    (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) :
    -sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els = s (2 * c) (Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1)) (N + 1) := by
  rw [zeta_all (2 * j + 1) hpr F hchar hcast N hq key hkey, ← bridge (2 * j + 1) hpr, GaussAp.gauss_ap4 j hpr c d hP hcd]

theorem cm_gauss_inert (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * j) + 1 + 1 + 1)
    (F : FinField.FF R) (hchar : nsm F.K (2 * (2 * j) + 1 + 1 + 1) F.K.one = F.K.zero)
    (hcast : ∀ z : Int, castZ F.K z = F.K.zero → ∃ w, z = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * w)
    (N : Nat) (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (N + 1))
    (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) :
    -sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els = s 0 (Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) (N + 1) := by
  rw [zeta_all (2 * j) hpr F hchar hcast N hq key hkey, InertFq.s1_inert j hpr]; rfl

theorem cm_eisen_split (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (3 * t + 2) + 1 + 1 + 1)
    (x y : Int) (hxy : x * x + 3 * (y * y) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) (hx : ∃ s, x = 3 * s + 1)
    (F : FinField.FF R) (hchar : nsm F.K (2 * (3 * t + 2) + 1 + 1 + 1) F.K.one = F.K.zero)
    (hcast : ∀ z : Int, castZ F.K z = F.K.zero → ∃ w, z = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * w)
    (N : Nat) (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) (N + 1)) :
    -FinCubic.psi F F.K.one = s (2 * x) (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) (N + 1) := by
  have h3 := EisenAp.p_ne_three (3 * t + 2) (2 * t + 2) (by lib_nat)
  rw [ZetaAll3.zeta_all3 (3 * t + 2) hpr h3 F hchar hcast N hq,
    EisenAp.eisen_ap (3 * t + 2) hpr (2 * t + 2) (by lib_nat) x y hxy hx, show -(-2 * x) = 2 * x by lib_int]

theorem cm_eisen_inert (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (3 * t + 1) + 1 + 1 + 1)
    (F : FinField.FF R) (hchar : nsm F.K (2 * (3 * t + 1) + 1 + 1 + 1) F.K.one = F.K.zero)
    (hcast : ∀ z : Int, castZ F.K z = F.K.zero → ∃ w, z = Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) * w)
    (N : Nat) (hq : Int.ofNat (2 * F.h + 1) = ipow (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (N + 1)) :
    -FinCubic.psi F F.K.one = s 0 (Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1)) (N + 1) := by
  have h3 : 2 * (3 * t + 1) + 1 + 1 + 1 ≠ 3 := fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show 2 * (3 * t + 1) + 1 + 1 + 1 = (6 * t + 1) + 1 + 1 + 1 + 1 by lib_nat).symm.trans e))))
  rw [ZetaAll3.zeta_all3 (3 * t + 1) hpr h3 F hchar hcast N hq, ZetaAll3.psi_p_inert t hpr]; rfl

end CMReciprocity

#print axioms CMReciprocity.bridge
#print axioms CMReciprocity.cm_gauss_split
#print axioms CMReciprocity.cm_gauss_inert
#print axioms CMReciprocity.cm_eisen_split
#print axioms CMReciprocity.cm_eisen_inert

#print axioms: 5 theorems, no axioms.

Listing 90: lang/FinTrace.lean

lang/FinTrace.lean

/-
  lang/FinTrace — T43, cycle 76: y² = x³ − x over any finite field 𝔽_q containing i (i² = −1).

  For every FF of order q with an injective key and i² = −1:
    fin_trace     ∃ u v, u² + v² = q, Σ_x χ(x³ − x) = ±2u, and Σ_x χ(x³ − x) = 7 − q + 8X for an integer X
  i.e. a = −Σ satisfies a² ≤ 4q (Hasse's bound for this curve over every such field) and a ≡ q + 1 (mod 8) — the Frobenius
  trace of the CM curve over 𝔽_q is the trace of a Gaussian integer of norm q, for every q (𝔽_p, 𝔽_{p²}, …).

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FinSign
import lang.CMModular

namespace FinTrace

open YMChain FiniteRecChain FinField FinConic FinMoment

variable {R : Type} [DecidableEq R] (F : FF R)

theorem fin_trace (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) :
    ∃ u v : Nat, u * u + v * v = 2 * F.h + 1 ∧
      (sumK Z (fun x => chi F (FinSign.f F x)) F.els = Int.ofNat (2 * u) ∨
        sumK Z (fun x => chi F (FinSign.f F x)) F.els = -Int.ofNat (2 * u)) ∧
      ∃ X : Int, sumK Z (fun x => chi F (FinSign.f F x)) F.els = 7 + -1 * Int.ofNat (2 * F.h + 1) + 8 * X := by
  have hi0 := FinSign.i_ne_zero F i hi
  have hm1 : chi F (F.K.neg F.K.one) = 1 := by rw [← hi]; exact chi_sq F i hi0
  have hIi : FinSign.I F i = 1 := by
    have h1 : F.K.add F.K.one (F.K.neg i) ≠ F.K.zero := fun e => by
      have := FinMoment.eq_of_sub_zero F _ _ e
      rw [← this, F.K.one_mul] at hi
      exact F.ne_pm hi.symm
    have e : FinSign.f F i = F.K.mul (F.K.add F.K.one (F.K.neg i)) (F.K.add F.K.one (F.K.neg i)) := by
      have id : FinSign.f F i = F.K.add (F.K.mul (F.K.add F.K.one (F.K.neg i)) (F.K.add F.K.one (F.K.neg i)))
          (F.K.mul (F.K.add i (F.K.neg F.K.one)) (F.K.add (F.K.mul i i) F.K.one)) := by
        show F.K.add (F.K.mul (F.K.mul i i) i) (F.K.neg i) = _
        lib_ring
      rw [id, hi, DetChain.add_neg' F.K F.K.one, DetChain.mul_zero, DetChain.add_zero']
    unfold FinSign.I
    rw [e, chi_sq F _ h1, ite_pos' _ _ rfl]
  have hM8 := FinSign.sign_mod8 F key hkey i hi
  rw [hIi] at hM8
  generalize hH : sumK Z (fun x => FinPairing.ltI key x (FinSign.rho F key x) * FinSign.J F key x) F.els = H2 at hM8
  match F.nonres with
  | ⟨n, hn0, hnr⟩ =>
    have hh0 : F.h ≠ 0 := fun e => hnr (by rw [e]; rfl)
    have hchin : chi F n = -1 := chi_neg_of F n hn0 hnr
    have hM := FinMoment.moment F
    have hG := FinClass.grouped F n hn0 hchin
    rw [hm1] at hM
    rw [hM] at hG
    have hP1 : PP F + -1 ≠ 0 := fun e => by
      have e2 : Int.ofNat (2 * F.h + 1) = Int.ofNat 1 := by
        have : PP F = (PP F + -1) + 1 := by lib_int
        show PP F = _; rw [this, e]; rfl
      have e3 : 2 * F.h = 0 := Nat.succ.inj (Int.ofNat.inj e2)
      apply hh0
      cases hh : F.h with
      | zero => rfl
      | succ h' => rw [hh] at e3; exact absurd e3 (fun e => Nat.noConfusion (show Nat.succ (2 * h' + 1) = 0 from by rw [← e]; lib_nat))
    have hAB : phi F F.K.one * phi F F.K.one + phi F n * phi F n = 4 * PP F := by
      have hz : (PP F + -1) * (phi F F.K.one * phi F F.K.one + phi F n * phi F n + -1 * (4 * PP F)) = 0 := by
        have : (PP F + -1) * (phi F F.K.one * phi F F.K.one + phi F n * phi F n + -1 * (4 * PP F))
            = (PP F + -1) * (phi F F.K.one * phi F F.K.one + phi F n * phi F n)
              + -1 * (2 * (PP F * ((1 + 1) * (PP F + -1)))) := by lib_int
        rw [this, ← hG]; lib_int
      cases LibInt.int_noZD _ _ hz with
      | inl h => exact absurd h hP1
      | inr h =>
        have h' : phi F F.K.one * phi F F.K.one + phi F n * phi F n + -1 * (4 * PP F) = 0 := h
        have : phi F F.K.one * phi F F.K.one + phi F n * phi F n
            = (phi F F.K.one * phi F F.K.one + phi F n * phi F n + -1 * (4 * PP F)) + 4 * PP F := by lib_int
        rw [this, h']; lib_int
    match CMModular.nat_sq (phi F F.K.one), CMModular.nat_sq (phi F n) with
    | ⟨a, ha, hsa⟩, ⟨b, hb, _⟩ =>
      rw [ha, hb] at hAB
      have hnat : a * a + b * b = 4 * (2 * F.h + 1) := Int.ofNat.inj hAB
      match CMModular.halves a b _ hnat with
      | ⟨u, v, hau, _, huv⟩ =>
        have hS : sumK Z (fun x => chi F (FinSign.f F x)) F.els = chi F i * phi F F.K.one := by
          rw [← FinConic.phi_scale F F.K.one _ hi0, F.K.one_mul, hi]
          exact sumK_ext Z _ _ (fun x => by
            show chi F (F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x)) = chi F (F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)))
            rw [show F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x) = F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)) by lib_ring]) _
        refine ⟨u, v, huv, ?_, ⟨H2, ?_⟩⟩
        · rw [hS]
          have hchi : chi F i = 1 ∨ chi F i = -1 := by
            cases FinClass.chi_cases F i with
            | inl h0 => exact absurd h0 (FinClass.chi_ne_zero F _ hi0)
            | inr h => exact h
          rw [← hau]
          cases hchi with
          | inl e => cases hsa with
            | inl e' => left; rw [e, e']; lib_int
            | inr e' => right; rw [e, e']; lib_int
          | inr e => cases hsa with
            | inl e' => right; rw [e, e']; lib_int
            | inr e' => left; rw [e, e']; lib_int
        · rw [hM8]
          show 3 + -1 * PP F + 4 * 1 + 8 * H2 = 7 + -1 * PP F + 8 * H2
          lib_int

end FinTrace

#print axioms FinTrace.fin_trace

namespace FinTrace

open YMChain FiniteRecChain FinField

/-- **Hasse's bound** for y² = x³ − x over every 𝔽_q containing i: (Σ_x χ(x³ − x))² ≤ 4q. -/
theorem hasse_bound {R : Type} [DecidableEq R] (F : FF R) (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y)
    (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) :
    ∃ s : Nat, (sumK FinConic.Z (fun x => chi F (FinSign.f F x)) F.els = Int.ofNat s ∨
      sumK FinConic.Z (fun x => chi F (FinSign.f F x)) F.els = -Int.ofNat s) ∧ s * s ≤ 4 * (2 * F.h + 1) := by
  match fin_trace F key hkey i hi with
  | ⟨u, v, huv, hS, _⟩ =>
    refine ⟨2 * u, hS, ?_⟩
    rw [← huv, show 2 * u * (2 * u) = 4 * (u * u) by lib_nat]
    exact Nat.mul_le_mul_left 4 (Nat.le_add_right _ _)

/-- the value order on 𝔽_p is an injective key -/
theorem key_p (m : Nat) (x y : ZMod.Zp (2 * m + 1 + 1)) (h : x.val = y.val) : x = y := Subtype.ext h

end FinTrace

#print axioms FinTrace.hasse_bound
#print axioms FinTrace.key_p

Listing 91: lang/FinPrimary.lean

lang/FinPrimary.lean

/-
  lang/FinPrimary — T43, cycle 86: the exact sign for y² = x³ − x over every finite field 𝔽_q containing i.

    primary_of_mod8   a² + v² = 7 + 2a + 8X  ⇒  v even and a + v ≡ 1 (mod 4)   (pure integer lemma)
    fin_ap_primary    ∃ a b ∈ ℤ, a² + b² = q, a + b i primary, −Σ_x χ(x³ − x) = 2a
  With FinTrace (Σ = ±2u, q = u² + v², Σ ≡ 7 − q mod 8): the Frobenius trace of y² = x³ − x over 𝔽_q is ψ(π) + ψ(π̄) = 2 Re π
  for the primary Gaussian integer π of norm q — the Hecke character of ℚ(i) at every prime power q ≡ 1 (mod 4)
  (q = p, p², …), generalising SignCM3.ap_primary from 𝔽_p.

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.FinTrace
import lang.SignCM3

namespace FinPrimary

open YMChain FiniteRecChain FinField FinConic FinMoment

theorem two_cancel (x y : Int) (h : 2 * x = 2 * y) : x = y := CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) x y h

theorem primary_of_mod8 (a v X : Int) (h : a * a + v * v = 7 + 2 * a + 8 * X) :
    (∃ c, v = 2 * c) ∧ ∃ t, a + v - 1 = 4 * t := by
  match GaussInt.int_even_odd a, GaussInt.int_even_odd v with
  | Or.inl ⟨d, hd⟩, Or.inl ⟨c, hc⟩ =>
    exfalso; rw [hd, hc] at h
    exact GaussInt.int_odd_ne_even (3 + 2 * d + 4 * X) (2 * d * d + 2 * c * c) (by rw [show 2 * (2 * d * d + 2 * c * c) = 2 * d * (2 * d) + 2 * c * (2 * c) by lib_int, h]; lib_int)
  | Or.inl ⟨d, hd⟩, Or.inr ⟨c, hc⟩ =>
    exfalso; rw [hd, hc] at h
    -- 4(d² + c² + c − d − 2X) = 6
    have h2 : 2 * (2 * (d * d + c * c + c + -1 * d + -2 * X)) = 2 * (2 * 1 + 1) := by
      rw [show 2 * (2 * (d * d + c * c + c + -1 * d + -2 * X)) = 2 * d * (2 * d) + (2 * c + 1) * (2 * c + 1) + -1 * (7 + 2 * (2 * d) + 8 * X) + 6 by lib_int, h]
      lib_int
    exact GaussInt.int_odd_ne_even 1 _ (two_cancel _ _ h2).symm
  | Or.inr ⟨d, hd⟩, Or.inr ⟨c, hc⟩ =>
    exfalso; rw [hd, hc] at h
    exact GaussInt.int_odd_ne_even (4 * X + 4 + 2 * d) (2 * d * d + 2 * d + 2 * c * c + 2 * c + 1)
      (by rw [show 2 * (2 * d * d + 2 * d + 2 * c * c + 2 * c + 1) = (2 * d + 1) * (2 * d + 1) + (2 * c + 1) * (2 * c + 1) by lib_int, h]; lib_int)
  | Or.inr ⟨d, hd⟩, Or.inl ⟨c, hc⟩ =>
    refine ⟨⟨c, hc⟩, ?_⟩
    rw [hd, hc] at h
    -- d² + c² = 2 + 2X
    have hdc : 2 * (d * d + c * c) = 2 * (2 + 2 * X) := by
      apply two_cancel
      rw [show 2 * (2 * (d * d + c * c)) = (2 * d + 1) * (2 * d + 1) + 2 * c * (2 * c) + -1 * (4 * d + 1) by lib_int, h]
      lib_int
    have hdc' := two_cancel _ _ hdc
    rw [hd, hc]
    match GaussInt.int_even_odd d, GaussInt.int_even_odd c with
    | Or.inl ⟨e, he⟩, Or.inl ⟨f, hf⟩ => exact ⟨e + f, by rw [he, hf]; lib_int⟩
    | Or.inr ⟨e, he⟩, Or.inr ⟨f, hf⟩ => exact ⟨e + f + 1, by rw [he, hf]; lib_int⟩
    | Or.inl ⟨e, he⟩, Or.inr ⟨f, hf⟩ =>
      exfalso; rw [he, hf] at hdc'
      exact GaussInt.int_odd_ne_even (2 * e * e + 2 * f * f + 2 * f) (1 + X)
        (by rw [show 2 * (2 * e * e + 2 * f * f + 2 * f) + 1 = 2 * e * (2 * e) + (2 * f + 1) * (2 * f + 1) by lib_int, hdc']; lib_int)
    | Or.inr ⟨e, he⟩, Or.inl ⟨f, hf⟩ =>
      exfalso; rw [he, hf] at hdc'
      exact GaussInt.int_odd_ne_even (2 * e * e + 2 * e + 2 * f * f) (1 + X)
        (by rw [show 2 * (2 * e * e + 2 * e + 2 * f * f) + 1 = (2 * e + 1) * (2 * e + 1) + 2 * f * (2 * f) by lib_int, hdc']; lib_int)

variable {R : Type} [DecidableEq R] (F : FF R)

/-- **a = 2 Re π for the primary π of norm q** (y² = x³ − x over every 𝔽_q ∋ i). -/
theorem fin_ap_primary (key : R → Nat) (hkey : ∀ x y, key x = key y → x = y) (i : R) (hi : F.K.mul i i = F.K.neg F.K.one) :
    ∃ a b : Int, a * a + b * b = Int.ofNat (2 * F.h + 1) ∧ GaussInt.Primary ⟨a, b⟩ ∧
      -sumK Z (fun x => chi F (FinSign.f F x)) F.els = 2 * a := by
  match FinTrace.fin_trace F key hkey i hi with
  | ⟨u, v, huv, hs, X, hX⟩ =>
    generalize hS : sumK Z (fun x => chi F (FinSign.f F x)) F.els = S at hs hX ⊢
    have hq : Int.ofNat u * Int.ofNat u + Int.ofNat v * Int.ofNat v = Int.ofNat (2 * F.h + 1) := congrArg Int.ofNat huv
    -- a = −S/2 = ∓u
    have ha : ∃ a : Int, S = -(2 * a) ∧ a * a = Int.ofNat u * Int.ofNat u := by
      cases hs with
      | inl e => exact ⟨-Int.ofNat u, by rw [e]; show 2 * Int.ofNat u = _; lib_int, by lib_int⟩
      | inr e => exact ⟨Int.ofNat u, by rw [e]; rfl, rfl⟩
    match ha with
    | ⟨a, hSa, haa⟩ =>
      have hab : a * a + Int.ofNat v * Int.ofNat v = Int.ofNat (2 * F.h + 1) := by rw [haa]; exact hq
      have h8 : a * a + Int.ofNat v * Int.ofNat v = 7 + 2 * a + 8 * X := by
        rw [hab]
        have : -(2 * a) = 7 + -1 * Int.ofNat (2 * F.h + 1) + 8 * X := hSa.symm.trans hX
        calc Int.ofNat (2 * F.h + 1) = -1 * (7 + -1 * Int.ofNat (2 * F.h + 1) + 8 * X) + 7 + 8 * X := by lib_int
          _ = 7 + 2 * a + 8 * X := by rw [← this]; lib_int
      match primary_of_mod8 a (Int.ofNat v) X h8 with
      | ⟨⟨c, hc⟩, ⟨t, ht⟩⟩ =>
        exact ⟨a, Int.ofNat v, hab, ⟨⟨c, hc⟩, ⟨t, ht⟩⟩, by rw [hSa]; lib_int⟩

end FinPrimary

#print axioms FinPrimary.two_cancel
#print axioms FinPrimary.primary_of_mod8
#print axioms FinPrimary.fin_ap_primary

Listing 92: lang/SuperSing.lean

lang/SuperSing.lean

/-
  lang/SuperSing — T43, cycle 75: base change of the CM form of y² = x³ − x to ℚ(i) at the inert primes.

  p = 4j + 3, 𝔽_{p²} = 𝔽_p[i] (Fp2Inst.ff2). Then
    supersingular     Σ_{z ∈ 𝔽_{p²}} χ(z³ − z) = 2p,  i.e.  #E(𝔽_{p²}) = p² + 1 + 2p = (p + 1)²
  so over the CM field ℚ(i) the base-changed curve has a_{(p)} = −2p at every inert prime (p), the trace of the Hecke
  character of ℚ(i) at (p) (ψ((p)) = −p, counted twice) — GL2 over the CM field ℚ(i) at its inert primes.
  Proof: over 𝔽_{p²} the Jacobsthal chain (FinMoment, FinClass) gives φ(1)² + φ(n)² = 4p², so Σ = χ(i) φ(1) ∈ {0, ±2p}
  (p inert in ℤ[i], Fp2Count.inert_norm); the mod-8 count (FinSign) gives Σ ≡ 7 − p² ≡ 6 (mod 8), leaving only 2p.

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Fp2Count
import lang.CubicTrace
import lang.GaussInt

namespace SuperSing

open FTAChain ZMod YMChain FiniteRecChain FinField FinConic FinMoment

theorem two_zero (Y : Int) (h : 2 * Y = 0) : Y = 0 :=
  CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) Y 0 (h.trans (by lib_int))

theorem not_8m2 (X : Int) (h : 8 * X + -2 = 0) : False := by
  have h1 : 4 * X + -1 = 0 := two_zero _ (by rw [show (2 : Int) * (4 * X + -1) = 8 * X + -2 by lib_int, h])
  exact GaussInt.two_ne_one (2 * X) (by
    rw [show (2 : Int) * (2 * X) = (4 * X + -1) + 1 by lib_int, h1]; lib_int)

theorem not_8p4 (X : Int) (h : 8 * X + 4 = 0) : False := by
  have h1 : 2 * X + 1 = 0 := two_zero _ (two_zero _ (by rw [show (2 : Int) * (2 * (2 * X + 1)) = 8 * X + 4 by lib_int, h]))
  exact GaussInt.two_ne_one (-X) (by
    rw [show (2 : Int) * -X = -(2 * X + 1) + 1 by lib_int, h1]; lib_int)

variable (j : Nat)

/-- **The supersingular count**: Σ_{z ∈ 𝔽_{p²}} χ(z³ − z) = 2p for p = 4j + 3. -/
theorem supersingular (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) (2 * j + 1) = (ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) :
    sumK Z (fun x => chi (Fp2Inst.ff2 (2 * j) hpr hn) (FinSign.f (Fp2Inst.ff2 (2 * j) hpr hn) x)) (Fp2Inst.ff2 (2 * j) hpr hn).els = Int.ofNat (2 * (2 * (2 * j) + 1 + 1 + 1)) := by
  -- i, χ(−1) = 1, I(i) = 1
  have hi : (Fp2Inst.ff2 (2 * j) hpr hn).K.mul (Fp2Count.i2 j) (Fp2Count.i2 j) = (Fp2Inst.ff2 (2 * j) hpr hn).K.neg (Fp2Inst.ff2 (2 * j) hpr hn).K.one := Fp2Count.hi2 j
  have hi0 := FinSign.i_ne_zero (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Count.i2 j) hi
  have hm1 : chi (Fp2Inst.ff2 (2 * j) hpr hn) ((Fp2Inst.ff2 (2 * j) hpr hn).K.neg (Fp2Inst.ff2 (2 * j) hpr hn).K.one) = 1 := by rw [← hi]; exact chi_sq (Fp2Inst.ff2 (2 * j) hpr hn) _ hi0
  have hIi : FinSign.I (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Count.i2 j) = 1 := by
    have h1 : (Fp2Inst.ff2 (2 * j) hpr hn).K.add (Fp2Inst.ff2 (2 * j) hpr hn).K.one ((Fp2Inst.ff2 (2 * j) hpr hn).K.neg (Fp2Count.i2 j)) ≠ (Fp2Inst.ff2 (2 * j) hpr hn).K.zero := fun e => by
      have := FinMoment.eq_of_sub_zero (Fp2Inst.ff2 (2 * j) hpr hn) _ _ e
      rw [← this, (Fp2Inst.ff2 (2 * j) hpr hn).K.one_mul] at hi
      exact (Fp2Inst.ff2 (2 * j) hpr hn).ne_pm hi.symm
    have e : FinSign.f (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Count.i2 j) = (Fp2Inst.ff2 (2 * j) hpr hn).K.mul ((Fp2Inst.ff2 (2 * j) hpr hn).K.add (Fp2Inst.ff2 (2 * j) hpr hn).K.one ((Fp2Inst.ff2 (2 * j) hpr hn).K.neg (Fp2Count.i2 j)))
        ((Fp2Inst.ff2 (2 * j) hpr hn).K.add (Fp2Inst.ff2 (2 * j) hpr hn).K.one ((Fp2Inst.ff2 (2 * j) hpr hn).K.neg (Fp2Count.i2 j))) := by
      have id : FinSign.f (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Count.i2 j) = (Fp2Inst.ff2 (2 * j) hpr hn).K.add ((Fp2Inst.ff2 (2 * j) hpr hn).K.mul ((Fp2Inst.ff2 (2 * j) hpr hn).K.add (Fp2Inst.ff2 (2 * j) hpr hn).K.one ((Fp2Inst.ff2 (2 * j) hpr hn).K.neg (Fp2Count.i2 j)))
          ((Fp2Inst.ff2 (2 * j) hpr hn).K.add (Fp2Inst.ff2 (2 * j) hpr hn).K.one ((Fp2Inst.ff2 (2 * j) hpr hn).K.neg (Fp2Count.i2 j))))
          ((Fp2Inst.ff2 (2 * j) hpr hn).K.mul ((Fp2Inst.ff2 (2 * j) hpr hn).K.add (Fp2Count.i2 j) ((Fp2Inst.ff2 (2 * j) hpr hn).K.neg (Fp2Inst.ff2 (2 * j) hpr hn).K.one))
            ((Fp2Inst.ff2 (2 * j) hpr hn).K.add ((Fp2Inst.ff2 (2 * j) hpr hn).K.mul (Fp2Count.i2 j) (Fp2Count.i2 j)) (Fp2Inst.ff2 (2 * j) hpr hn).K.one)) := by
        show (Fp2Inst.ff2 (2 * j) hpr hn).K.add ((Fp2Inst.ff2 (2 * j) hpr hn).K.mul ((Fp2Inst.ff2 (2 * j) hpr hn).K.mul (Fp2Count.i2 j) (Fp2Count.i2 j)) (Fp2Count.i2 j)) ((Fp2Inst.ff2 (2 * j) hpr hn).K.neg (Fp2Count.i2 j)) = _
        lib_ring
      rw [id, hi, DetChain.add_neg' (Fp2Inst.ff2 (2 * j) hpr hn).K (Fp2Inst.ff2 (2 * j) hpr hn).K.one, DetChain.mul_zero, DetChain.add_zero']
    unfold FinSign.I
    rw [e, chi_sq (Fp2Inst.ff2 (2 * j) hpr hn) _ h1, ite_pos' _ _ rfl]
  -- the mod-8 formula
  have hM8 := FinSign.sign_mod8 (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Count.key2 j) (Fp2Count.key2_inj j) (Fp2Count.i2 j) hi
  rw [hIi] at hM8
  generalize hH : sumK Z (fun x => FinPairing.ltI (Fp2Count.key2 j) x (FinSign.rho (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Count.key2 j) x)
    * FinSign.J (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Count.key2 j) x) (Fp2Inst.ff2 (2 * j) hpr hn).els = H2 at hM8
  have hPP : PP (Fp2Inst.ff2 (2 * j) hpr hn) = Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) := by
    show Int.ofNat (2 * ((2 * (2 * j) + 1 + 1 + 1 + 1) * (2 * j + 1)) + 1) = _
    rw [show 2 * ((2 * (2 * j) + 1 + 1 + 1 + 1) * (2 * j + 1)) + 1 = (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1) by lib_nat]
  have hP2 : Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) = 16 * (Int.ofNat j * Int.ofNat j) + 24 * Int.ofNat j + 9 := by
    rw [show (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1) = 16 * (j * j) + 24 * j + 9 by lib_nat]; rfl
  rw [hPP, hP2] at hM8
  -- second moment: φ(1)² + φ(n)² = 4p²
  match (Fp2Inst.ff2 (2 * j) hpr hn).nonres with
  | ⟨n, hn0, hnr⟩ =>
    have hchin : chi (Fp2Inst.ff2 (2 * j) hpr hn) n = -1 := chi_neg_of (Fp2Inst.ff2 (2 * j) hpr hn) n hn0 hnr
    have hM := FinMoment.moment (Fp2Inst.ff2 (2 * j) hpr hn)
    have hG := FinClass.grouped (Fp2Inst.ff2 (2 * j) hpr hn) n hn0 hchin
    rw [hm1] at hM
    rw [hM, hPP] at hG
    have hP1 : Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) + -1 ≠ 0 := fun e => by
      have e2 : Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) = Int.ofNat 1 := by
        have : Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) = (Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) + -1) + 1 := by lib_int
        rw [this, e]; rfl
      have e3 := Int.ofNat.inj e2
      rw [show (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1) = (16 * (j * j) + 24 * j + 7) + 1 + 1 by lib_nat] at e3
      exact Nat.noConfusion (Nat.succ.inj e3)
    have hAB : phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one * phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one + phi (Fp2Inst.ff2 (2 * j) hpr hn) n * phi (Fp2Inst.ff2 (2 * j) hpr hn) n = 4 * Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) := by
      have hz : (Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) + -1) * (phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one * phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one + phi (Fp2Inst.ff2 (2 * j) hpr hn) n * phi (Fp2Inst.ff2 (2 * j) hpr hn) n
          + -1 * (4 * Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)))) = 0 := by
        have : (Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) + -1) * (phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one * phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one + phi (Fp2Inst.ff2 (2 * j) hpr hn) n * phi (Fp2Inst.ff2 (2 * j) hpr hn) n
            + -1 * (4 * Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1))))
            = (Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) + -1) * (phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one * phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one + phi (Fp2Inst.ff2 (2 * j) hpr hn) n * phi (Fp2Inst.ff2 (2 * j) hpr hn) n)
              + -1 * (2 * (Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) * ((1 + 1) * (Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) + -1)))) := by lib_int
        rw [this, ← hG]; lib_int
      cases LibInt.int_noZD _ _ hz with
      | inl h => exact absurd h hP1
      | inr h =>
        have h' : phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one * phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one + phi (Fp2Inst.ff2 (2 * j) hpr hn) n * phi (Fp2Inst.ff2 (2 * j) hpr hn) n + -1 * (4 * Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1))) = 0 := h
        have : phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one * phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one + phi (Fp2Inst.ff2 (2 * j) hpr hn) n * phi (Fp2Inst.ff2 (2 * j) hpr hn) n
            = (phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one * phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one + phi (Fp2Inst.ff2 (2 * j) hpr hn) n * phi (Fp2Inst.ff2 (2 * j) hpr hn) n + -1 * (4 * Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1))))
              + 4 * Int.ofNat ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) := by lib_int
        rw [this, h']; lib_int
    match CMModular.nat_sq (phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one), CMModular.nat_sq (phi (Fp2Inst.ff2 (2 * j) hpr hn) n) with
    | ⟨a, ha, hsa⟩, ⟨b, hb, _⟩ =>
      rw [ha, hb] at hAB
      have hnat : a * a + b * b = 4 * ((2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) := Int.ofNat.inj hAB
      match CMModular.halves a b _ hnat with
      | ⟨u, v, hau, _, huv⟩ =>
        -- Σ = φ(−1) = χ(i) φ(1)
        have hS : sumK Z (fun x => chi (Fp2Inst.ff2 (2 * j) hpr hn) (FinSign.f (Fp2Inst.ff2 (2 * j) hpr hn) x)) (Fp2Inst.ff2 (2 * j) hpr hn).els = chi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Count.i2 j) * phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one := by
          rw [← FinConic.phi_scale (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one _ hi0, (Fp2Inst.ff2 (2 * j) hpr hn).K.one_mul, hi]
          exact sumK_ext Z _ _ (fun x => by
            show chi (Fp2Inst.ff2 (2 * j) hpr hn) ((Fp2Inst.ff2 (2 * j) hpr hn).K.add ((Fp2Inst.ff2 (2 * j) hpr hn).K.mul ((Fp2Inst.ff2 (2 * j) hpr hn).K.mul x x) x) ((Fp2Inst.ff2 (2 * j) hpr hn).K.neg x)) = chi (Fp2Inst.ff2 (2 * j) hpr hn) ((Fp2Inst.ff2 (2 * j) hpr hn).K.mul x ((Fp2Inst.ff2 (2 * j) hpr hn).K.add ((Fp2Inst.ff2 (2 * j) hpr hn).K.mul x x) ((Fp2Inst.ff2 (2 * j) hpr hn).K.neg (Fp2Inst  -- (+29 chars; full line in the Zenodo source)
            rw [show (Fp2Inst.ff2 (2 * j) hpr hn).K.add ((Fp2Inst.ff2 (2 * j) hpr hn).K.mul ((Fp2Inst.ff2 (2 * j) hpr hn).K.mul x x) x) ((Fp2Inst.ff2 (2 * j) hpr hn).K.neg x) = (Fp2Inst.ff2 (2 * j) hpr hn).K.mul x ((Fp2Inst.ff2 (2 * j) hpr hn).K.add ((Fp2Inst.ff2 (2 * j) hpr hn).K.mul x x) ((Fp2Inst.ff2 (2 * j) hpr hn).K.neg (Fp2Inst.ff2 (2 * j) hpr hn).K.one)) by lib_ring]) _
        rw [hS] at hM8 ⊢
        have hX : chi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Count.i2 j) * phi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Inst.ff2 (2 * j) hpr hn).K.one
            = 8 * (H2 + -2 * (Int.ofNat j * Int.ofNat j) + -3 * Int.ofNat j) + -2 := by rw [hM8]; lib_int
        have hval : ∀ s : Int, (s = 1 ∨ s = -1) → ∀ t : Int, (t = Int.ofNat a ∨ t = -Int.ofNat a) → s * t = 1 * Int.ofNat a ∨ s * t = -1 * Int.ofNat a := by
          intro s hs t ht
          cases hs with
          | inl e => cases ht with
            | inl e' => left; rw [e, e']
            | inr e' => right; rw [e, e']; lib_int
          | inr e => cases ht with
            | inl e' => right; rw [e, e']
            | inr e' => left; rw [e, e']; lib_int
        have hchi : chi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Count.i2 j) = 1 ∨ chi (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Count.i2 j) = -1 := by
          cases FinClass.chi_cases (Fp2Inst.ff2 (2 * j) hpr hn) (Fp2Count.i2 j) with
          | inl h0 => exact absurd h0 (FinClass.chi_ne_zero (Fp2Inst.ff2 (2 * j) hpr hn) _ hi0)
          | inr h => exact h
        cases Fp2Count.inert_norm j hpr u v huv with
        | inl e =>
          have hv := hval _ hchi _ hsa
          rw [hau, e.1] at hv
          cases hv with
          | inl h1 => rw [h1]; lib_int
          | inr h1 =>
            exfalso
            rw [h1] at hX
            apply not_8p4 (H2 + -2 * (Int.ofNat j * Int.ofNat j) + -3 * Int.ofNat j + Int.ofNat j)
            have : -1 * Int.ofNat (2 * (2 * (2 * j) + 1 + 1 + 1)) = -8 * Int.ofNat j + -6 := by
              show -1 * Int.ofNat (2 * (2 * (2 * j) + 1 + 1 + 1)) = _
              rw [show 2 * (2 * (2 * j) + 1 + 1 + 1) = 8 * j + 6 by lib_nat]; show -1 * (8 * Int.ofNat j + 6) = _; lib_int
            rw [this] at hX
            rw [show 8 * (H2 + -2 * (Int.ofNat j * Int.ofNat j) + -3 * Int.ofNat j + Int.ofNat j) + 4
              = (8 * (H2 + -2 * (Int.ofNat j * Int.ofNat j) + -3 * Int.ofNat j) + -2) + -1 * (-8 * Int.ofNat j + -6) by lib_int, ← hX]
            lib_int
        | inr e =>
          exfalso
          have hv := hval _ hchi _ hsa
          rw [hau, e.1] at hv
          apply not_8m2 (H2 + -2 * (Int.ofNat j * Int.ofNat j) + -3 * Int.ofNat j)
          rw [← hX]
          cases hv with
          | inl h1 => rw [h1]; rfl
          | inr h1 => rw [h1]; rfl

end SuperSing

#print axioms SuperSing.two_zero
#print axioms SuperSing.not_8m2
#print axioms SuperSing.not_8p4
#print axioms SuperSing.supersingular

78.35 GL\(_2\) over \(\mathbb Q(\sqrt {-3})\): \(y^2=x^3+1\) over every \(\mathbb F_q\) with \(q\equiv 1\ (3)\)

For every finite field \(\mathbb F_q\) of odd order with \(3\mid q-1\), and for primes \(p\equiv 2\ (3)\), \(p>3\),

\[ \begin {aligned} -\textstyle \sum _{x\in \mathbb F_q}\chi (x^3+1)&=2a,\quad q=a^2+3b^2,\quad a\equiv 1\ (3),\quad (2a)^2\le 4q,\\ \textstyle \sum _{z\in \mathbb F_{p^2}}\chi (z^3+1)&=2p,\qquad \#E(\mathbb F_{p^2})=(p+1)^2,\qquad a_{(p)}=-2p . \end {aligned} \]
The steps are
\[ \begin {aligned} &\mathbb F_q^\times =\langle g\rangle \quad (\text {orders of coprime order multiply, a distinct list of units has}\le q-1\ \text {entries}),\\ &\textstyle \sum _a F(a)=F(0)+\sum _{k<q-1}F(g^k),\\ &A_0^2+A_1^2+A_2^2=6q,\quad A_0-A_1+A_2=0\ \Rightarrow \ \psi (1)=\pm 2u,\ q=u^2+3w^2,\\ &\mathbb F_{p^2}=\mathbb F_p[\sqrt n]\ \text {for any non-residue}\ n,\\ &q=p^2,\ -3\ \text {a non-residue mod}\ p\ \Rightarrow \ u=p,\ w=0,\\ &\psi (1)=1+3T,\ \psi (1)\ \text {even}\Rightarrow T=2s-1,\ \psi (1)=2(3s-1),\ a=1-3s,\\ &-2p\equiv 2\not \equiv 1\ (3)\ \Rightarrow \ \psi (1)=2p . \end {aligned} \]

Listing 93: lang/FinCyclic.lean

lang/FinCyclic.lean

/-
  lang/FinCyclic — T43, cycle 79: every finite field of the interface has a primitive root (𝔽_q^× is cyclic).

  For an FF of order q = 2h + 1:
    fermat_ff          x ≠ 0 ⇒ x^(q−1) = 1                       (Euler: x^h = ±1)
    exists_nonroot_ff  d + 2 ≤ q − 1 ⇒ some g ≠ 0 has g^(d+1) ≠ 1  (RootBound with the q − 1 distinct units, FinList)
    split_pow_ff       ℓ^(s+1) | q − 1 ⇒ an element y with y^(ℓ^(s+1)) = 1, y^(ℓ^s) ≠ 1
    ord_prime_pow_ff   … of exact order ℓ^(s+1)
    ord_of_dvd_ff      every divisor of q − 1 is an order
    primitive_root_ff  some g has order q − 1
  Ports of PowerSum/CycloPow/OrdMul/PrimRoot from ℤ/p to every FF instance (in particular 𝔽_{p²}).

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.FinList
import lang.FinConic
import lang.PrimRoot

namespace FinCyclic

open FTAChain YMChain FiniteRecChain FinField OrdMul TwoPower Order PrimeOneModQ PrimePowerOne PrimRoot

variable {R : Type} [DecidableEq R] (F : FF R)

theorem h_pos : 1 ≤ F.h := by
  cases hh : F.h with
  | zero => exact absurd (by match F.nonres with | ⟨n, _, hnr⟩ => exact absurd (by rw [hh]; rfl) hnr) (fun x => x)
  | succ h' => exact Nat.succ_le_succ (Nat.zero_le _)

theorem fermat_ff (x : R) (hx : x ≠ F.K.zero) : Frobenius.pw F.K x (2 * F.h) = F.K.one := by
  rw [Nat.two_mul, HasseCM.pw_add']
  cases F.euler_pm x hx with
  | inl h => rw [h, F.K.one_mul]
  | inr h => rw [h, DetChain.neg_mul_left, F.K.one_mul, DetChain.neg_neg']

theorem find_nonroot (k : Nat) : ∀ l : List R,
    (∃ g, memL g l ∧ Frobenius.pw F.K g (k + 1) ≠ F.K.one) ∨ (∀ g, memL g l → Frobenius.pw F.K g (k + 1) = F.K.one)
  | [] => Or.inr (fun _ h => False.elim h)
  | a :: l => by
    by_cases ha : Frobenius.pw F.K a (k + 1) = F.K.one
    · cases find_nonroot k l with
      | inl h => match h with | ⟨g, hg, e⟩ => exact Or.inl ⟨g, Or.inr hg, e⟩
      | inr h => exact Or.inr (fun g hg => by cases hg with
          | inl e => rw [e]; exact ha
          | inr e => exact h g e)
    · exact Or.inl ⟨a, Or.inl rfl, ha⟩

theorem exists_nonroot_ff (k : Nat) (hk : k + 1 + 1 ≤ 2 * F.h) :
    ∃ g, g ≠ F.K.zero ∧ Frobenius.pw F.K g (k + 1) ≠ F.K.one := by
  have hnz := FinList.nz_list F
  cases find_nonroot F k (FinList.nzL F.K.zero F.els) with
  | inl h => match h with | ⟨g, hg, e⟩ => exact ⟨g, hnz.2.2 g hg, e⟩
  | inr h =>
    exfalso
    have := RootBound.root_bound F.K F.nozd (k + 1) F.K.one (EulerHard.binomL.zerosThen F.K k)
      (FinConic.one_ne_zero F) (EulerHard.len_zerosThen _ k) _ hnz.1
      (fun r hr => by
        show RootBound.evH F.K r (EulerHard.binomL F.K k) = _
        rw [EulerHard.ev_binom, h r hr, F.K.add_neg])
    rw [hnz.2.1] at this
    exact Nat.not_succ_le_self _ (Nat.le_trans hk this)

theorem f_le' (N f t : Nat) (h : N = f * (t + 2)) (hf : 1 ≤ f) : f + 1 ≤ N := by
  rw [h, show f * (t + 2) = f * t + f + f by lib_nat]
  exact Nat.add_le_add (Nat.le_add_left f (f * t)) hf

theorem split_pow_ff (t s : Nat) (hd : Dv ((t + 1 + 1) ^ (s + 1)) (2 * F.h)) :
    ∃ y, Frobenius.pw F.K y ((t + 1 + 1) ^ (s + 1)) = F.K.one ∧ Frobenius.pw F.K y ((t + 1 + 1) ^ s) ≠ F.K.one := by
  match hd, pow_succ_form (t + 1) s with
  | ⟨0, h⟩, _ =>
    exfalso
    have : 2 * 1 ≤ 2 * F.h := Nat.mul_le_mul_left 2 (h_pos F)
    rw [h, Nat.zero_mul] at this
    exact Nat.not_succ_le_zero 1 this
  | ⟨f' + 1, h⟩, ⟨n, hn⟩ =>
    have hF : 2 * F.h = ((f' + 1) * (t + 1 + 1) ^ s) * (t + 1 + 1) := by rw [h, pow_succ', nmul_assoc]
    have hF1 : (f' + 1) * (t + 1 + 1) ^ s = ((f' + 1) * n + f') + 1 := by rw [hn]; rfl
    have hFJ := f_le' (2 * F.h) ((f' + 1) * (t + 1 + 1) ^ s) t hF (by rw [hF1]; exact Nat.succ_le_succ (Nat.zero_le _))
    rw [hF1] at hFJ
    match exists_nonroot_ff F ((f' + 1) * n + f') hFJ with
    | ⟨g, hg0, hg⟩ =>
      refine ⟨Frobenius.pw F.K g (f' + 1), ?_, ?_⟩
      · rw [pwpow, ← h]; exact fermat_ff F g hg0
      · rw [pwpow, hF1]; exact hg

theorem ord_prime_pow_ff (t s : Nat) (hq : ∀ d, Dv d (t + 1 + 1) → d = 1 ∨ d = t + 1 + 1)
    (hd : Dv ((t + 1 + 1) ^ (s + 1)) (2 * F.h)) : ∃ y, Ord F.K y ((t + 1 + 1) ^ (s + 1)) := by
  match split_pow_ff F t s hd with
  | ⟨x, hxfull, hX1⟩ =>
    obtain ⟨n, hn⟩ := pow_succ_form (t + 1) (s + 1)
    rw [hn] at hxfull
    obtain ⟨d, hd1, hdmin⟩ := ord_exists F.K x n hxfull
    obtain ⟨c, hc⟩ := ord_dvd F.K x d hd1 hdmin (n + 1) hxfull
    have hdq : d + 1 = (t + 1 + 1) ^ (s + 1) := by
      cases c with
      | zero => rw [nzero_mul] at hc; exact absurd hc (Nat.noConfusion)
      | succ c =>
        cases c with
        | zero => rw [hn, hc, nmul_comm, nmul_one]
        | succ c =>
          exfalso
          apply hX1
          have hpd := proper_div t s (d + 1) c hq (by rw [hn, hc, nmul_comm])
          obtain ⟨m', hm'⟩ := hpd
          rw [hm']
          exact pw_qe F.K x (d + 1) 0 hd1 m'
    exact ⟨x, hdq ▸ ord_of_min F.K x d hd1 hdmin⟩

theorem ord_one_ff : Ord F.K F.K.one 1 := ⟨pwone F.K 1, fun r _ => LibGcd.one_dvd' r⟩

theorem ord_of_dvd_ff : ∀ N n, n ≤ N → n ∣ 2 * F.h → ∃ g, Ord F.K g n
  | _, 0, _, h => by
    match h with
    | ⟨c, e⟩ =>
      exfalso
      have := h_pos F
      rw [Nat.zero_mul] at e
      cases hh : F.h with
      | zero => rw [hh] at this; exact Nat.not_succ_le_zero 0 this
      | succ h' => rw [hh] at e; exact Nat.noConfusion (show Nat.succ (2 * h' + 1) = 0 from by rw [← e]; lib_nat)
  | _, 1, _, _ => ⟨F.K.one, ord_one_ff F⟩
  | 0, k + 2, hN, _ => absurd hN (Nat.not_succ_le_zero _)
  | N + 1, k + 2, hN, hd => by
    match lp_exists k with
    | ⟨l, hl, hmin⟩ =>
      have hlp := lp_prime _ l hl hmin
      match extract l (k + 2) (k + 2) (Nat.le_refl _) (Nat.succ_le_succ (Nat.zero_le _)) with
      | ⟨0, n', he, hn'⟩ =>
        exfalso; apply hn'
        rw [Nat.pow_zero, Nat.one_mul] at he
        rw [← he]; exact dvd_of_dv hl
      | ⟨s + 1, n', he, hn'⟩ =>
        have hn1 : 1 ≤ n' := by
          cases n' with
          | zero => rw [Nat.mul_zero] at he; exact absurd he (fun e => Nat.noConfusion e)
          | succ _ => exact Nat.succ_le_succ (Nat.zero_le _)
        have hP2 : 2 ≤ (l + 1 + 1) ^ (s + 1) := by
          rw [Nat.pow_succ]
          have hp1 : 1 ≤ (l + 1 + 1) ^ s := match pow_succ_form (l + 1) s with
            | ⟨r, hr⟩ => by rw [hr]; exact Nat.succ_le_succ (Nat.zero_le r)
          have := Nat.mul_le_mul hp1 (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le l)))
          rw [Nat.one_mul] at this
          exact this
        have hlt : n' < k + 2 := by
          rw [he]
          have a : 2 * n' ≤ (l + 1 + 1) ^ (s + 1) * n' := Nat.mul_le_mul_right n' hP2
          have b : n' < 2 * n' := by rw [Nat.two_mul]; exact Nat.lt_add_of_pos_right hn1
          exact Nat.lt_of_lt_of_le b a
        have hPd : (l + 1 + 1) ^ (s + 1) ∣ 2 * F.h := LibGcd.dvd_trans' ⟨n', he⟩ hd
        have hnd : n' ∣ 2 * F.h := LibGcd.dvd_trans' ⟨(l + 1 + 1) ^ (s + 1), by rw [he, Nat.mul_comm]⟩ hd
        match ord_prime_pow_ff F l s hlp (dv_of_dvd hPd),
          ord_of_dvd_ff N n' (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hlt hN)) hnd with
        | ⟨y, hy⟩, ⟨z, hz⟩ =>
          refine ⟨F.K.mul y z, ?_⟩
          rw [he]
          exact ord_mul_coprime F.K y z _ _ hy hz (coprime_pow l s n' hlp hn1 hn')

/-- **𝔽_q^× is cyclic** for every FF. -/
theorem primitive_root_ff : ∃ g, Ord F.K g (2 * F.h) :=
  ord_of_dvd_ff F (2 * F.h) (2 * F.h) (Nat.le_refl _) (LibGcd.dvd_refl' _)

end FinCyclic

#print axioms FinCyclic.h_pos
#print axioms FinCyclic.fermat_ff
#print axioms FinCyclic.find_nonroot
#print axioms FinCyclic.exists_nonroot_ff
#print axioms FinCyclic.f_le'
#print axioms FinCyclic.split_pow_ff
#print axioms FinCyclic.ord_prime_pow_ff
#print axioms FinCyclic.ord_one_ff
#print axioms FinCyclic.ord_of_dvd_ff
#print axioms FinCyclic.primitive_root_ff

Listing 94: lang/FinDLog.lean

lang/FinDLog.lean

/-
  lang/FinDLog — T43, cycle 80: discrete logarithms in any finite field of the interface.

  g a primitive root (FinCyclic), q − 1 = 2h.
    sumN, sumN_cnt     Σ_{z ∈ 𝔽_q} cnt z l = |l|            (each element of a list is counted once)
    len_le             a distinct list of nonzero elements has at most q − 1 entries
    pows, pows_dist    g⁰, …, g^(q−2) are distinct
    every_unit_ff      every unit is a power of g
    sum_split_ff       Σ_a F(a) = F(0) + Σ_k F(g^k)
  Port of lang/DLog to FinField.FF (used for the cubic chain over 𝔽_{p²}).

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.FinCyclic
import lang.FinMoment

namespace FinDLog

open FTAChain YMChain FiniteRecChain FinField OrdMul FinList

variable {R : Type} [DecidableEq R] (F : FF R)

def sumN {A : Type} (f : A → Nat) : List A → Nat
  | [] => 0
  | a :: l => f a + sumN f l

theorem sumN_add {A : Type} (f g : A → Nat) : ∀ l : List A, sumN (fun a => f a + g a) l = sumN f l + sumN g l
  | [] => rfl
  | a :: l => by show f a + g a + sumN _ l = (f a + sumN f l) + (g a + sumN g l); rw [sumN_add f g l]; lib_nat

theorem sumN_le {A : Type} (f g : A → Nat) (h : ∀ a, f a ≤ g a) : ∀ l : List A, sumN f l ≤ sumN g l
  | [] => Nat.le_refl 0
  | a :: l => Nat.add_le_add (h a) (sumN_le f g h l)

theorem cnt_eq_sumN (z : R) : ∀ l : List R, cnt z l = sumN (fun y => if y = z then 1 else 0) l
  | [] => rfl
  | a :: l => by show _ + cnt z l = _ + sumN _ l; rw [cnt_eq_sumN z l]

theorem cnt_els (a : R) : sumN (fun z => if a = z then 1 else 0) F.els = 1 := by
  have h1 : cnt a F.els = 1 := Int.ofNat.inj ((cnt_int a F.els).symm.trans (F.comp a))
  rw [cnt_eq_sumN] at h1
  have : ∀ l : List R, sumN (fun z => if a = z then 1 else 0) l = sumN (fun y => if y = a then 1 else 0) l := by
    intro l; induction l with
    | nil => rfl
    | cons b l ih =>
      show (if a = b then 1 else 0) + sumN (fun z => if a = z then 1 else 0) l
        = (if b = a then 1 else 0) + sumN (fun y => if y = a then 1 else 0) l
      rw [ih]
      by_cases e : a = b
      · rw [ite_pos' _ _ e, ite_pos' _ _ e.symm]
      · rw [ite_neg' _ _ e, ite_neg' _ _ (fun e' => e e'.symm)]
  exact (this _).trans h1

theorem sumN_cnt : ∀ l : List R, sumN (fun z => cnt z l) F.els = l.length
  | [] => by
    show sumN (fun _ => 0) F.els = 0
    have : ∀ l : List R, sumN (fun _ => (0 : Nat)) l = 0 := by
      intro l; induction l with
      | nil => rfl
      | cons _ l ih => show 0 + _ = 0; rw [ih]
    exact this _
  | a :: l => by
    show sumN (fun z => (if a = z then 1 else 0) + cnt z l) F.els = l.length + 1
    rw [sumN_add, cnt_els F a, sumN_cnt l]; lib_nat

theorem cnt_le_one_of_dist (z : R) : ∀ l : List R, RootBound.Dist l → cnt z l ≤ 1
  | [], _ => Nat.zero_le 1
  | a :: l, hd => by
    show (if a = z then 1 else 0) + cnt z l ≤ 1
    by_cases e : a = z
    · rw [ite_pos' _ _ e]
      have : cnt z l = 0 := by
        cases hc : cnt z l with
        | zero => rfl
        | succ c =>
          exfalso
          have hm : memL z l := by
            have : ∀ l' : List R, 1 ≤ cnt z l' → memL z l' := by
              intro l'; induction l' with
              | nil => intro h; exact absurd h (Nat.not_succ_le_zero 0)
              | cons b l' ih =>
                intro h
                by_cases eb : b = z
                · exact Or.inl eb.symm
                · right; apply ih
                  have : cnt z (b :: l') = cnt z l' := by show (if b = z then 1 else 0) + cnt z l' = _; rw [ite_neg' _ _ eb, Nat.zero_add]
                  rw [← this]; exact h
            exact this l (by rw [hc]; exact Nat.succ_le_succ (Nat.zero_le c))
          exact hd.1 z hm e.symm
      rw [this]; exact Nat.le_refl _
    · rw [ite_neg' _ _ e, Nat.zero_add]; exact cnt_le_one_of_dist z l hd.2

/-- **A distinct list of nonzero elements has at most q − 1 entries.** -/
theorem len_le (l : List R) (hd : RootBound.Dist l) (hz : ∀ s, memL s l → s ≠ F.K.zero) : l.length ≤ 2 * F.h := by
  rw [← sumN_cnt F l]
  have h0 : cnt F.K.zero l = 0 := by
    have : ∀ l' : List R, (∀ s, memL s l' → s ≠ F.K.zero) → cnt F.K.zero l' = 0 := by
      intro l'; induction l' with
      | nil => intro _; rfl
      | cons b l' ih =>
        intro h
        show (if b = F.K.zero then 1 else 0) + cnt F.K.zero l' = 0
        rw [ite_neg' _ _ (h b (Or.inl rfl)), ih (fun s hs => h s (Or.inr hs))]
    exact this l hz
  have hb : ∀ z, cnt z l ≤ (if z = F.K.zero then 0 else 1) := fun z => by
    by_cases e : z = F.K.zero
    · rw [ite_pos' _ _ e, e, h0]; exact Nat.le_refl 0
    · rw [ite_neg' _ _ e]; exact cnt_le_one_of_dist z l hd
  have hs := sumN_le _ _ hb F.els
  have hcount : sumN (fun z => if z = F.K.zero then 0 else 1) F.els + cnt F.K.zero F.els = F.els.length := by
    have : ∀ l' : List R, sumN (fun z => if z = F.K.zero then 0 else 1) l' + cnt F.K.zero l' = l'.length := by
      intro l'; induction l' with
      | nil => rfl
      | cons b l' ih =>
        show ((if b = F.K.zero then 0 else 1) + sumN (fun z => if z = F.K.zero then 0 else 1) l') + ((if b = F.K.zero then 1 else 0) + cnt F.K.zero l') = l'.length + 1
        by_cases e : b = F.K.zero
        · rw [ite_pos' _ _ e, ite_pos' _ _ e, ← ih]; lib_nat
        · rw [ite_neg' _ _ e, ite_neg' _ _ e, ← ih]; lib_nat
    exact this _
  have h1 : cnt F.K.zero F.els = 1 := Int.ofNat.inj ((cnt_int F.K.zero F.els).symm.trans (F.comp F.K.zero))
  rw [h1, len_els F] at hcount
  have : sumN (fun z => if z = F.K.zero then 0 else 1) F.els = 2 * F.h := Nat.succ.inj hcount
  rw [this] at hs
  exact hs

def pows (g : R) : Nat → List R
  | 0 => []
  | n + 1 => Frobenius.pw F.K g n :: pows g n

theorem pw_ne_zero (g : R) (hg : g ≠ F.K.zero) : ∀ k, Frobenius.pw F.K g k ≠ F.K.zero
  | 0 => FinConic.one_ne_zero F
  | k + 1 => fun e => by
    cases F.nozd _ _ e with
    | inl h => exact pw_ne_zero g hg k h
    | inr h => exact hg h

theorem mem_pows (g s : R) : ∀ n, memL s (pows F g n) → ∃ k, k < n ∧ s = Frobenius.pw F.K g k
  | 0, h => False.elim h
  | n + 1, h => by
    cases h with
    | inl e => exact ⟨n, Nat.lt_succ_self n, e⟩
    | inr e => match mem_pows g s n e with | ⟨k, hk, hs⟩ => exact ⟨k, Nat.lt_succ_of_lt hk, hs⟩

theorem g_ne_zero (g : R) (hg : Ord F.K g (2 * F.h)) : g ≠ F.K.zero := fun h => by
  have := hg.1
  have hh := FinCyclic.h_pos F
  cases hq : F.h with
  | zero => rw [hq] at hh; exact Nat.not_succ_le_zero 0 hh
  | succ h' =>
    rw [h, hq, show 2 * (h' + 1) = (2 * h' + 1) + 1 by lib_nat, HasseCM.pw_zero_succ] at this
    exact FinConic.one_ne_zero F this.symm

theorem pw_inj (g : R) (hg : Ord F.K g (2 * F.h)) (k l : Nat) (hkl : k < l) (hl : l < 2 * F.h) :
    Frobenius.pw F.K g k ≠ Frobenius.pw F.K g l := fun e => by
  have hg0 := g_ne_zero F g hg
  match Nat.le.dest hkl with
  | ⟨d, hd⟩ =>
    have hl' : l = k + (d + 1) := by rw [← hd]; exact (Nat.succ_add k d)
    rw [hl', HasseCM.pw_add'] at e
    have h0 : F.K.mul (Frobenius.pw F.K g k) (F.K.add (Frobenius.pw F.K g (d + 1)) (F.K.neg F.K.one)) = F.K.zero := by
      rw [F.K.distrib, ← e, mul_neg_r F.K, F.K.mul_comm _ F.K.one, F.K.one_mul, F.K.add_neg]
    cases F.nozd _ _ h0 with
    | inl h => exact pw_ne_zero F g hg0 k h
    | inr h =>
      match hg.2 (d + 1) (FinMoment.eq_of_sub_zero F _ _ h) with
      | ⟨c, hc⟩ =>
        cases c with
        | zero => rw [Nat.mul_zero] at hc; exact Nat.noConfusion hc
        | succ c =>
          have : 2 * F.h ≤ d + 1 := by rw [hc, Nat.mul_succ]; exact Nat.le_add_left _ _
          have hdl : d + 1 ≤ l := by rw [hl']; exact Nat.le_add_left _ _
          exact Nat.lt_irrefl l (Nat.lt_of_lt_of_le hl (Nat.le_trans this hdl))

theorem pows_dist (g : R) (hg : Ord F.K g (2 * F.h)) : ∀ n, n ≤ 2 * F.h → RootBound.Dist (pows F g n)
  | 0, _ => trivial
  | n + 1, hn => ⟨fun s hs => by
      match mem_pows F g s n hs with
      | ⟨k, hk, e⟩ => rw [e]; exact pw_inj F g hg k n hk (Nat.lt_of_succ_le hn),
    pows_dist g hg n (Nat.le_of_succ_le hn)⟩

theorem len_pows (g : R) : ∀ n, (pows F g n).length = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (len_pows g n)

theorem find_mem (a : R) : ∀ l : List R, memL a l ∨ ¬ memL a l
  | [] => Or.inr (fun h => h)
  | b :: l => by
    by_cases h : a = b
    · exact Or.inl (Or.inl h)
    · cases find_mem a l with
      | inl h' => exact Or.inl (Or.inr h')
      | inr h' => exact Or.inr (fun hh => by cases hh with
        | inl e => exact h e
        | inr e => exact h' e)

/-- **Every unit is a power of the primitive root.** -/
theorem every_unit_ff (g : R) (hg : Ord F.K g (2 * F.h)) (a : R) (ha : a ≠ F.K.zero) : memL a (pows F g (2 * F.h)) := by
  have hg0 := g_ne_zero F g hg
  cases find_mem a (pows F g (2 * F.h)) with
  | inl h => exact h
  | inr h =>
    exfalso
    have hd : RootBound.Dist (a :: pows F g (2 * F.h)) := ⟨fun s hs e => h (e ▸ hs), pows_dist F g hg _ (Nat.le_refl _)⟩
    have hz : ∀ s, memL s (a :: pows F g (2 * F.h)) → s ≠ F.K.zero := fun s hs => by
      cases hs with
      | inl e => rw [e]; exact ha
      | inr e => match mem_pows F g s _ e with | ⟨k, _, e'⟩ => rw [e']; exact pw_ne_zero F g hg0 k
    have := len_le F _ hd hz
    rw [show (a :: pows F g (2 * F.h)).length = 2 * F.h + 1 from congrArg Nat.succ (len_pows F g _)] at this
    exact Nat.not_succ_le_self _ this

section Sums
variable {S : Type} (Rg : DetChain.CRing S)

theorem count_out (a : R) : ∀ l : List R, ¬ memL a l → sumK Rg (fun s => delta Rg a s) l = Rg.zero
  | [], _ => rfl
  | b :: l, h => by
    show Rg.add (delta Rg a b) (sumK Rg (fun s => delta Rg a s) l) = Rg.zero
    rw [show delta Rg a b = Rg.zero from ite_neg' _ _ (fun e => h (Or.inl e)), count_out a l (fun h' => h (Or.inr h')),
      Rg.zero_add]

theorem count_in (a : R) : ∀ l : List R, RootBound.Dist l → memL a l → sumK Rg (fun s => delta Rg a s) l = Rg.one
  | [], _, h => False.elim h
  | b :: l, hd, h => by
    show Rg.add (delta Rg a b) (sumK Rg (fun s => delta Rg a s) l) = Rg.one
    by_cases e : a = b
    · rw [show delta Rg a b = Rg.one from ite_pos' _ _ e, count_out Rg a l (fun h' => hd.1 a h' e), DetChain.add_zero']
    · cases h with
      | inl e' => exact absurd e' e
      | inr h' => rw [show delta Rg a b = Rg.zero from ite_neg' _ _ e, count_in a l hd.2 h', Rg.zero_add]

/-- **Σ_a F(a) = F(0) + Σ_k F(g^k).** -/
theorem sum_split_ff (g : R) (hg : Ord F.K g (2 * F.h)) (Fn : R → S) (hc : Complete Rg F.els) :
    sumK Rg Fn F.els = Rg.add (Fn F.K.zero) (sumK Rg Fn (pows F g (2 * F.h))) := by
  have hg0 := g_ne_zero F g hg
  have hd := pows_dist F g hg (2 * F.h) (Nat.le_refl _)
  have pt : ∀ a, Fn a = Rg.add (Rg.mul (delta Rg a F.K.zero) (Fn a))
      (sumK Rg (fun s => Rg.mul (delta Rg a s) (Fn a)) (pows F g (2 * F.h))) := fun a => by
    rw [← sumK_mul, ← DetChain.right_distrib]
    by_cases ha : a = F.K.zero
    · rw [show delta Rg a F.K.zero = Rg.one from ite_pos' _ _ ha,
        count_out Rg a _ (fun h => by
          match mem_pows F g a _ h with
          | ⟨k, _, e⟩ => exact pw_ne_zero F g hg0 k (e.symm.trans ha)),
        DetChain.add_zero', Rg.one_mul]
    · rw [show delta Rg a F.K.zero = Rg.zero from ite_neg' _ _ ha, count_in Rg a _ hd (every_unit_ff F g hg a ha),
        Rg.zero_add, Rg.one_mul]
  rw [sumK_ext Rg _ _ pt, sumK_add, pick Rg _ hc Fn F.K.zero, sumK_swap]
  congr 1
  exact sumK_ext Rg _ _ (fun s => pick Rg _ hc Fn s) _

end Sums

end FinDLog

#print axioms FinDLog.sumN_add
#print axioms FinDLog.sumN_le
#print axioms FinDLog.cnt_eq_sumN
#print axioms FinDLog.cnt_els
#print axioms FinDLog.sumN_cnt
#print axioms FinDLog.cnt_le_one_of_dist
#print axioms FinDLog.len_le
#print axioms FinDLog.pw_ne_zero
#print axioms FinDLog.mem_pows
#print axioms FinDLog.g_ne_zero
#print axioms FinDLog.pw_inj
#print axioms FinDLog.pows_dist
#print axioms FinDLog.len_pows
#print axioms FinDLog.find_mem
#print axioms FinDLog.every_unit_ff
#print axioms FinDLog.count_out
#print axioms FinDLog.count_in
#print axioms FinDLog.sum_split_ff

Listing 95: lang/FinCubic.lean

lang/FinCubic.lean

/-
  lang/FinCubic — T43, cycle 81: cubic Jacobsthal sums ψ(a) = Σ_x χ(x³ + a) over any finite field (ports of CubicJacob
  and CubicMoment): ψ(a t³) = χ(t) ψ(a), Σ χψ = 0, and Σ ψ² = 2q(q − 1) when F has a primitive cube root of unity.

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FinDLog
import lang.FinClass

namespace FinCubic

open YMChain FiniteRecChain FinField FinConic FinMoment

theorem cube_factor {R : Type} [DecidableEq R] (F : FF R) (y : R) :
    F.K.mul (F.K.add y (F.K.neg F.K.one)) (F.K.add (F.K.add (F.K.mul y y) y) F.K.one)
      = F.K.add (F.K.mul (F.K.mul y y) y) (F.K.neg F.K.one) := by lib_ring

variable {R : Type} [DecidableEq R] (F : FF R)

def cube (x : R) : R := F.K.mul x (F.K.mul x x)

def psi (a : R) : Int := sumK Z (fun x => chi F (F.K.add (cube F x) a)) F.els

theorem cube_mul (t x : R) : cube F (F.K.mul t x) = F.K.mul (cube F t) (cube F x) := by
  unfold cube; lib_ring

/-- **ψ(a t³) = χ(t) ψ(a).** -/
theorem psi_scale (a t : R)
    (ht : t ≠ F.K.zero) : psi F (F.K.mul a (cube F t)) = chi F t * psi F a := by
  have h := FinConic.reindex_mul F t ht (fun x => chi F (F.K.add (cube F x) (F.K.mul a (cube F t))))
  show sumK Z _ F.els = _
  rw [← h]
  have pt : ∀ x, chi F (F.K.add (cube F (F.K.mul t x)) (F.K.mul a (cube F t)))
      = Z.mul (chi F t) (chi F (F.K.add (cube F x) a)) := fun x => by
    have e : F.K.add (cube F (F.K.mul t x)) (F.K.mul a (cube F t)) = F.K.mul (cube F t) (F.K.add (cube F x) a) := by
      unfold cube; lib_ring
    rw [e, chi_mul F, show cube F t = F.K.mul t (F.K.mul t t) from rfl, FinConic.chi_cube F]
    rfl
  rw [sumK_ext Z _ _ pt, ← mul_sumK]
  rfl

theorem chi_psi_const (a t : R)
    (ht : t ≠ F.K.zero) : chi F (F.K.mul a (cube F t)) * psi F (F.K.mul a (cube F t)) = chi F a * psi F a := by
  rw [psi_scale F a t ht, chi_mul F, show cube F t = F.K.mul t (F.K.mul t t) from rfl, FinConic.chi_cube F]
  have := FinClass.chi_sq_val F t ht
  calc chi F a * chi F t * (chi F t * psi F a) = (chi F t * chi F t) * (chi F a * psi F a) := by lib_int
    _ = chi F a * psi F a := by rw [this]; lib_int

theorem psi_sq_const (a t : R)
    (ht : t ≠ F.K.zero) : psi F (F.K.mul a (cube F t)) * psi F (F.K.mul a (cube F t)) = psi F a * psi F a := by
  rw [psi_scale F a t ht]
  have := FinClass.chi_sq_val F t ht
  calc chi F t * psi F a * (chi F t * psi F a) = (chi F t * chi F t) * (psi F a * psi F a) := by lib_int
    _ = psi F a * psi F a := by rw [this]; lib_int

/-- **Σ_a χ(a) ψ(a) = 0.** -/
theorem twisted :
    sumK Z (fun a => chi F a * psi F a) F.els = 0 := by
  have e1 : ∀ a, chi F a * psi F a = sumK Z (fun x => chi F (F.K.mul (F.K.add a F.K.zero) (F.K.add a (cube F x)))) F.els :=
    fun a => by
      show Z.mul (chi F a) (sumK Z _ F.els) = _
      rw [mul_sumK]
      apply sumK_ext
      intro x
      show chi F a * chi F (F.K.add (cube F x) a) = _
      rw [← chi_mul F]
      have : F.K.mul a (F.K.add (cube F x) a) = F.K.mul (F.K.add a F.K.zero) (F.K.add a (cube F x)) := by lib_ring
      rw [this]
  rw [sumK_ext Z _ _ e1, sumK_swap, sumK_ext Z _ _ (fun x => FinMoment.inner F F.K.zero (cube F x))]
  -- Σ_x (−1 + p δ(0, x³)) = −p + p · 1
  have hd : ∀ x, delta Z F.K.zero (cube F x) = delta Z x F.K.zero := fun x => by
    by_cases hx : x = F.K.zero
    · rw [hx, show cube F F.K.zero = F.K.zero by unfold cube; rw [DetChain.zero_mul'], FinMoment.delta_one _ _ rfl]
    · have hc : F.K.zero ≠ cube F x := fun e => by
        have h0 : F.K.mul x (F.K.mul x x) = F.K.zero := e.symm
        cases F.nozd _ _ h0 with
        | inl h => exact hx h
        | inr h =>
          cases F.nozd _ _ h with
          | inl h' => exact hx h'
          | inr h' => exact hx h'
      rw [FinMoment.delta_zero _ _ hc, FinMoment.delta_zero _ _ hx]
  have pt : ∀ x, -1 + PP F * delta Z F.K.zero (cube F x) = Z.add (-1) (Z.mul (PP F) (delta Z x F.K.zero)) := fun x => by
    rw [hd]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, ← mul_sumK, show sumK Z (fun x => delta Z x F.K.zero) F.els = 1 from F.comp F.K.zero]
  have : sumK Z (fun _ => (-1 : Int)) F.els = -1 * PP F := by
    rw [← FinMoment.sum_one F]
    show _ = Z.mul (-1) (sumK Z (fun _ => (1 : Int)) F.els)
    rw [mul_sumK]
    rfl
  rw [this]
  show -1 * PP F + PP F * 1 = 0
  lib_int



theorem omega_rel (w : R)
    (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) : F.K.add (F.K.add (F.K.mul w w) w) F.K.one = F.K.zero := by
  have hc := cube_factor F w
  have : F.K.mul (F.K.mul w w) w = cube F w := by unfold cube; lib_ring
  rw [this, hw3, F.K.add_neg] at hc
  cases F.nozd _ _ hc with
  | inl h => exact absurd (FinMoment.eq_of_sub_zero F w F.K.one h) hw1
  | inr h => exact h

theorem factor3 (z w : R) (hs : F.K.add (F.K.add (F.K.mul w w) w) F.K.one = F.K.zero)
    (hw3 : cube F w = F.K.one) :
    F.K.mul (F.K.mul (F.K.add z (F.K.neg F.K.one)) (F.K.add z (F.K.neg w))) (F.K.add z (F.K.neg (F.K.mul w w)))
      = F.K.add (cube F z) (F.K.neg F.K.one) := by
  have e : F.K.mul (F.K.mul (F.K.add z (F.K.neg F.K.one)) (F.K.add z (F.K.neg w))) (F.K.add z (F.K.neg (F.K.mul w w)))
      = F.K.add (F.K.add (F.K.add (cube F z) (F.K.neg (F.K.mul (F.K.add (F.K.add (F.K.mul w w) w) F.K.one) (F.K.mul z z))))
          (F.K.mul (F.K.mul w (F.K.add (F.K.add (F.K.mul w w) w) F.K.one)) z)) (F.K.neg (cube F w)) := by
    unfold cube; lib_ring
  rw [e, hs, hw3, DetChain.zero_mul', DetChain.mul_zero, DetChain.zero_mul']
  have : F.K.neg F.K.zero = F.K.zero := (DetChain.neg_unique F.K _ _ (F.K.zero_add F.K.zero)).symm
  rw [this, DetChain.add_zero', DetChain.add_zero']

theorem cube_zero : cube F F.K.zero = F.K.zero := by unfold cube; rw [DetChain.zero_mul']

theorem roots (z w : R)
    (hs : F.K.add (F.K.add (F.K.mul w w) w) F.K.one = F.K.zero) (hw3 : cube F w = F.K.one) (hz : cube F z = F.K.one) :
    z = F.K.one ∨ z = w ∨ z = F.K.mul w w := by
  have h := factor3 F z w hs hw3
  rw [hz, F.K.add_neg] at h
  cases F.nozd _ _ h with
  | inl h1 =>
    cases F.nozd _ _ h1 with
    | inl h2 => exact Or.inl (FinMoment.eq_of_sub_zero F _ _ h2)
    | inr h2 => exact Or.inr (Or.inl (FinMoment.eq_of_sub_zero F _ _ h2))
  | inr h1 => exact Or.inr (Or.inr (FinMoment.eq_of_sub_zero F _ _ h1))

theorem w_ne_ww (w : R)
    (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) : w ≠ F.K.mul w w := fun e => by
  have h : F.K.mul w (F.K.add w (F.K.neg F.K.one)) = F.K.zero := by
    have : F.K.mul w (F.K.add w (F.K.neg F.K.one)) = F.K.add (F.K.mul w w) (F.K.neg w) := by lib_ring
    rw [this, ← e, F.K.add_neg]
  cases F.nozd _ _ h with
  | inl h0 => rw [h0, cube_zero] at hw3; exact FinConic.one_ne_zero F hw3.symm
  | inr h1 => exact hw1 (FinMoment.eq_of_sub_zero F _ _ h1)

theorem one_ne_ww (w : R) (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) : F.K.one ≠ F.K.mul w w :=
  fun e => hw1 (by
    have : cube F w = F.K.mul w (F.K.mul w w) := rfl
    rw [← e, F.K.mul_comm, F.K.one_mul] at this
    exact this.symm.trans hw3)

theorem cube_ww (w : R) (hw3 : cube F w = F.K.one) : cube F (F.K.mul w w) = F.K.one := by
  have : cube F (F.K.mul w w) = F.K.mul (cube F w) (cube F w) := by unfold cube; lib_ring
  rw [this, hw3, F.K.one_mul]

theorem cube_one : cube F F.K.one = F.K.one := by unfold cube; rw [F.K.one_mul, F.K.one_mul]

/-- **Exactly three cube roots of unity.** -/
theorem count_roots (w : R)
    (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) :
    sumK Z (fun z => delta Z (cube F z) F.K.one) F.els = 3 := by
  have hs := omega_rel F w hw1 hw3
  have d1 := w_ne_ww F w hw1 hw3
  have d2 := one_ne_ww F w hw1 hw3
  have pt : ∀ z, delta Z (cube F z) F.K.one
      = Z.add (delta Z z F.K.one) (Z.add (delta Z z w) (delta Z z (F.K.mul w w))) := fun z => by
    by_cases h1 : z = F.K.one
    · rw [h1, cube_one, FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ (fun e => hw1 e.symm),
        FinMoment.delta_zero _ _ d2]; rfl
    by_cases h2 : z = w
    · rw [h2, hw3, FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ hw1, FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ d1]; rfl
    by_cases h3 : z = F.K.mul w w
    · rw [h3, cube_ww F w hw3, FinMoment.delta_one _ _ rfl, FinMoment.delta_zero _ _ (fun e => d2 e.symm),
        FinMoment.delta_zero _ _ (fun e => d1 e.symm), FinMoment.delta_one _ _ rfl]; rfl
    · have hc : cube F z ≠ F.K.one := fun e => by
        cases roots F z w hs hw3 e with
        | inl h => exact h1 h
        | inr h => cases h with
          | inl h => exact h2 h
          | inr h => exact h3 h
      rw [FinMoment.delta_zero _ _ hc, FinMoment.delta_zero _ _ h1, FinMoment.delta_zero _ _ h2, FinMoment.delta_zero _ _ h3]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, sumK_add,
    show sumK Z (fun z => delta Z z F.K.one) F.els = 1 from F.comp F.K.one,
    show sumK Z (fun z => delta Z z w) F.els = 1 from F.comp w,
    show sumK Z (fun z => delta Z z (F.K.mul w w)) F.els = 1 from F.comp _]
  rfl

theorem cube_ne_zero (x : R)
    (hx : x ≠ F.K.zero) : cube F x ≠ F.K.zero := fun e => by
  cases F.nozd _ _ e with
  | inl h => exact hx h
  | inr h =>
    cases F.nozd _ _ h with
    | inl h' => exact hx h'
    | inr h' => exact hx h'

theorem count_x (w : R)
    (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) (x : R) :
    sumK Z (fun y => delta Z (cube F x) (cube F y)) F.els = 3 + -2 * delta Z x F.K.zero := by
  by_cases hx : x = F.K.zero
  · rw [hx, cube_zero, FinMoment.delta_one _ _ rfl]
    have pt : ∀ y, delta Z F.K.zero (cube F y) = delta Z y F.K.zero := fun y => by
      by_cases hy : y = F.K.zero
      · rw [hy, cube_zero, FinMoment.delta_one _ _ rfl]
      · rw [FinMoment.delta_zero _ _ (fun e => cube_ne_zero F y hy e.symm), FinMoment.delta_zero _ _ hy]
    rw [sumK_ext Z _ _ pt, show sumK Z (fun y => delta Z y F.K.zero) F.els = 1 from F.comp F.K.zero]
    rfl
  · rw [FinMoment.delta_zero _ _ hx, ← FinConic.reindex_mul F x hx (fun y => delta Z (cube F x) (cube F y))]
    have pt : ∀ z, delta Z (cube F x) (cube F (F.K.mul x z)) = delta Z (cube F z) F.K.one := fun z => by
      rw [cube_mul]
      by_cases hz : cube F z = F.K.one
      · rw [hz, F.K.mul_comm, F.K.one_mul, FinMoment.delta_one _ _ rfl, FinMoment.delta_one _ _ rfl]
      · have hne : cube F x ≠ F.K.mul (cube F x) (cube F z) := fun e => by
          have h0 : F.K.mul (cube F x) (F.K.add (cube F z) (F.K.neg F.K.one)) = F.K.zero := by
            have : F.K.mul (cube F x) (F.K.add (cube F z) (F.K.neg F.K.one))
                = F.K.add (F.K.mul (cube F x) (cube F z)) (F.K.neg (cube F x)) := by lib_ring
            rw [this, ← e, F.K.add_neg]
          cases F.nozd _ _ h0 with
          | inl h => exact cube_ne_zero F x hx h
          | inr h => exact hz (FinMoment.eq_of_sub_zero F _ _ h)
        rw [FinMoment.delta_zero _ _ hne, FinMoment.delta_zero _ _ hz]
    rw [sumK_ext Z _ _ pt, count_roots F w hw1 hw3]
    rfl

/-- **Σ_a ψ(a)² = 2p(p − 1)** for p ≡ 1 (mod 3). -/
theorem moment_c (w : R)
    (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) :
    sumK Z (fun a => psi F a * psi F a) F.els = 2 * PP F * (PP F + -1) := by
  have e1 : ∀ a, psi F a * psi F a = sumK Z (fun x => sumK Z (fun y =>
      chi F (F.K.add (cube F x) a) * chi F (F.K.add (cube F y) a)) F.els) F.els := fun a => by
    show Z.mul (sumK Z _ F.els) (sumK Z _ F.els) = _
    rw [sumK_mul]
    exact sumK_ext Z _ _ (fun x => mul_sumK Z _ _ F.els) _
  rw [sumK_ext Z _ _ e1, sumK_swap]
  have e2 : ∀ x, sumK Z (fun a => sumK Z (fun y =>
      chi F (F.K.add (cube F x) a) * chi F (F.K.add (cube F y) a)) F.els) F.els
      = Z.add (Z.mul (-1) (PP F)) (Z.mul (PP F) (3 + -2 * delta Z x F.K.zero)) := fun x => by
    rw [sumK_swap]
    have e3 : ∀ y, sumK Z (fun a => chi F (F.K.add (cube F x) a) * chi F (F.K.add (cube F y) a)) F.els
        = Z.add (-1) (Z.mul (PP F) (delta Z (cube F x) (cube F y))) := fun y => by
      have pt : ∀ a, chi F (F.K.add (cube F x) a) * chi F (F.K.add (cube F y) a)
          = chi F (F.K.mul (F.K.add a (cube F x)) (F.K.add a (cube F y))) := fun a => by
        rw [← chi_mul F, F.K.add_comm (cube F x), F.K.add_comm (cube F y)]
      rw [sumK_ext Z _ _ pt, FinMoment.inner F]
      rfl
    rw [sumK_ext Z _ _ e3, sumK_add, ← mul_sumK, count_x F w hw1 hw3 x]
    have : sumK Z (fun _ => (-1 : Int)) F.els = Z.mul (-1) (PP F) := by
      rw [← FinMoment.sum_one F, mul_sumK]; rfl
    rw [this]
  rw [sumK_ext Z _ _ e2, sumK_add, ← mul_sumK]
  have hs : sumK Z (fun x => Z.mul (PP F) (3 + -2 * delta Z x F.K.zero)) F.els = PP F * (3 * PP F + -2) := by
    rw [← mul_sumK]
    have pt : ∀ x, (3 : Int) + -2 * delta Z x F.K.zero = Z.add 3 (Z.mul (-2) (delta Z x F.K.zero)) := fun _ => rfl
    rw [sumK_ext Z _ _ pt, sumK_add, ← mul_sumK, show sumK Z (fun x => delta Z x F.K.zero) F.els = 1 from F.comp F.K.zero]
    have : sumK Z (fun _ => (3 : Int)) F.els = 3 * PP F := by
      rw [← FinMoment.sum_one F]
      show _ = Z.mul 3 (sumK Z (fun _ => (1 : Int)) F.els)
      rw [mul_sumK]; rfl
    rw [this]
    show PP F * (3 * PP F + -2 * 1) = _
    lib_int
  rw [hs]
  have h1 : sumK Z (fun _ => PP F) F.els = Z.mul (PP F) (sumK Z (fun _ => (1 : Int)) F.els) := by
    rw [mul_sumK]
    exact sumK_ext Z _ _ (fun _ => by show PP F = PP F * 1; lib_int) _
  rw [h1, sum_one]
  show -1 * (PP F * PP F) + PP F * (3 * PP F + -2) = _
  lib_int

end FinCubic

#print axioms FinCubic.cube_factor
#print axioms FinCubic.cube_mul
#print axioms FinCubic.psi_scale
#print axioms FinCubic.chi_psi_const
#print axioms FinCubic.psi_sq_const
#print axioms FinCubic.twisted
#print axioms FinCubic.omega_rel
#print axioms FinCubic.factor3
#print axioms FinCubic.cube_zero
#print axioms FinCubic.roots
#print axioms FinCubic.w_ne_ww
#print axioms FinCubic.one_ne_ww
#print axioms FinCubic.cube_ww
#print axioms FinCubic.cube_one
#print axioms FinCubic.count_roots
#print axioms FinCubic.cube_ne_zero
#print axioms FinCubic.count_x
#print axioms FinCubic.moment_c

Listing 96: lang/FinCubicTrace.lean

lang/FinCubicTrace.lean

/-
  lang/FinCubicTrace — T43, cycle 82: y² = x³ + 1 over any finite field 𝔽_q with q ≡ 1 (mod 3).

  Ports of CubicClass, CubicTrace and CubicSign to the interface:
    nq, chi_split, delta_cube_neg, psi_one_even     ψ(1) = 2((h − 1) − Σ nq)  (q = 2h + 1)
    cb, pw_split_ff, group3_ff                      sums over the cubic classes of 𝔽_q^×
    cube_sum_ff, psi_one_mod3_ff                    ψ(1) ≡ 1 (mod 3)
  (the trace theorem itself is assembled in the next file).

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.FinCubic
import lang.CubicSign

namespace FinCubicTrace

open FTAChain YMChain FiniteRecChain FinField FinConic FinMoment FinCubic FinDLog OrdMul

variable {R : Type} [DecidableEq R] (F : FF R)

def nq (z : R) : Int := if chi F z = -1 then 1 else 0

theorem chi_split (z : R) : chi F z = 1 + -1 * delta Z z F.K.zero + -2 * nq F z := by
  by_cases hz : z = F.K.zero
  · rw [hz, chi_zero, FinMoment.delta_one _ _ rfl]
    unfold nq
    rw [chi_zero, ite_neg' _ _ (fun e => Int.noConfusion e)]; rfl
  · rw [FinMoment.delta_zero _ _ hz]
    unfold nq
    cases FinClass.chi_cases F z with
    | inl h0 => exact absurd h0 (FinClass.chi_ne_zero F z hz)
    | inr h =>
      cases h with
      | inl h1 => rw [h1, ite_neg' _ _ (fun e => Int.noConfusion e)]; rfl
      | inr h1 => rw [h1, ite_pos' _ _ rfl]; rfl

theorem delta_cube_neg (z : R) :
    delta Z (F.K.add (cube F z) F.K.one) F.K.zero = delta Z (cube F (F.K.neg z)) F.K.one := by
  have ec : cube F (F.K.neg z) = F.K.neg (cube F z) := by unfold cube; lib_ring
  rw [ec]
  by_cases h : F.K.add (cube F z) F.K.one = F.K.zero
  · have h' : F.K.neg (cube F z) = F.K.one := by
      have := DetChain.neg_unique F.K _ _ h
      rw [this, DetChain.neg_neg']
    rw [FinMoment.delta_one _ _ h, FinMoment.delta_one _ _ h']
  · have h' : F.K.neg (cube F z) ≠ F.K.one := fun e => h (by rw [← e]; exact F.K.add_neg _)
    rw [FinMoment.delta_zero _ _ h, FinMoment.delta_zero _ _ h']

theorem psi_one_even (w : R) (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) :
    psi F F.K.one = 2 * (Int.ofNat F.h + -1 + -1 * sumK Z (fun x => nq F (F.K.add (cube F x) F.K.one)) F.els) := by
  show sumK Z (fun x => chi F (F.K.add (cube F x) F.K.one)) F.els = _
  have pt : ∀ x, chi F (F.K.add (cube F x) F.K.one) = Z.add (Z.add 1 (Z.mul (-1) (delta Z (cube F (F.K.neg x)) F.K.one)))
      (Z.mul (-2) (nq F (F.K.add (cube F x) F.K.one))) := fun x => by
    rw [chi_split F, delta_cube_neg F]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, FinMoment.sum_one F]
  have hneg : sumK Z (fun x => delta Z (cube F (F.K.neg x)) F.K.one) F.els = 3 := by
    have hr := reindex Z F.els F.comp F.K.neg F.K.neg (fun x => DetChain.neg_neg' F.K x)
      (fun x => DetChain.neg_neg' F.K x) (fun y => delta Z (cube F y) F.K.one)
    rw [hr]
    exact count_roots F w hw1 hw3
  rw [hneg]
  show Int.ofNat (2 * F.h + 1) + -1 * 3 + -2 * _ = _
  show 2 * Int.ofNat F.h + 1 + -1 * 3 + -2 * _ = _
  lib_int

def cb (t : R) : R := F.K.mul t (F.K.mul t t)

theorem pw_split_ff (g : R) (N i : Nat) :
    Frobenius.pw F.K g (3 * N + i) = F.K.mul (Frobenius.pw F.K g i) (cb F (Frobenius.pw F.K g N)) := by
  rw [show 3 * N + i = i + N * 3 by lib_nat, HasseCM.pw_add', ← TwoPower.pwpow]
  show F.K.mul (Frobenius.pw F.K g i) (F.K.mul (F.K.mul (F.K.mul F.K.one (Frobenius.pw F.K g N)) (Frobenius.pw F.K g N))
    (Frobenius.pw F.K g N)) = _
  unfold cb; lib_ring

theorem group3_ff (g : R) (hg0 : g ≠ F.K.zero) (G : R → Int) (hG : ∀ a t, t ≠ F.K.zero → G (F.K.mul a (cb F t)) = G a) :
    ∀ N, sumK LibInt.intR G (pows F g (3 * N)) = Int.ofNat N * (G F.K.one + G g + G (F.K.mul g g))
  | 0 => by show (0 : Int) = 0 * _; lib_int
  | N + 1 => by
    show G (Frobenius.pw F.K g (3 * N + 2)) + (G (Frobenius.pw F.K g (3 * N + 1)) + (G (Frobenius.pw F.K g (3 * N + 0))
      + sumK LibInt.intR G (pows F g (3 * N)))) = _
    have hgN := pw_ne_zero F g hg0 N
    rw [pw_split_ff F g N 2, pw_split_ff F g N 1, pw_split_ff F g N 0, hG _ _ hgN, hG _ _ hgN, hG _ _ hgN, group3_ff g hg0 G hG N]
    have e2 : Frobenius.pw F.K g 2 = F.K.mul g g := by show F.K.mul (F.K.mul F.K.one g) g = _; rw [F.K.one_mul]
    have e1 : Frobenius.pw F.K g 1 = g := F.K.one_mul g
    rw [e2, e1]
    show G (F.K.mul g g) + (G g + (G F.K.one + Int.ofNat N * (G F.K.one + G g + G (F.K.mul g g))))
      = (Int.ofNat N + 1) * (G F.K.one + G g + G (F.K.mul g g))
    lib_int

theorem sumR_pows_ff (g : R) (G : R → Int) : ∀ n,
    sumK LibInt.intR G (pows F g n) = CubicSign.sumR (fun k => G (Frobenius.pw F.K g k)) n
  | 0 => rfl
  | n + 1 => by
    show G (Frobenius.pw F.K g n) + sumK LibInt.intR G (pows F g n) = _
    rw [sumR_pows_ff g G n]; rfl

theorem cube_sum_ff (g : R) (hg : Ord F.K g (2 * F.h)) (N : Nat) (hN : 2 * F.h = 3 * N) (G : R → Int) :
    sumK LibInt.intR (fun x => G (cb F x)) F.els
      = G F.K.zero + 3 * CubicSign.sumR (fun k => G (cb F (Frobenius.pw F.K g k))) N := by
  rw [sum_split_ff F LibInt.intR g hg _ F.comp, sumR_pows_ff, congrArg (CubicSign.sumR _) hN, CubicSign.sumR_triple]
  · have : cb F F.K.zero = F.K.zero := by unfold cb; rw [DetChain.zero_mul']
    rw [this]; rfl
  · intro k
    show G (cb F (Frobenius.pw F.K g (k + N))) = G (cb F (Frobenius.pw F.K g k))
    have : cb F (Frobenius.pw F.K g (k + N)) = cb F (Frobenius.pw F.K g k) := by
      unfold cb
      rw [HasseCM.pw_add']
      have h3 : F.K.mul (Frobenius.pw F.K g N) (F.K.mul (Frobenius.pw F.K g N) (Frobenius.pw F.K g N)) = F.K.one := by
        have this : Frobenius.pw F.K g (3 * N) = F.K.one := (congrArg (Frobenius.pw F.K g) hN).symm.trans hg.1
        rw [show 3 * N = N * 3 by lib_nat, ← TwoPower.pwpow] at this
        rw [← this]
        show _ = F.K.mul (F.K.mul (F.K.mul F.K.one (Frobenius.pw F.K g N)) (Frobenius.pw F.K g N)) (Frobenius.pw F.K g N)
        lib_ring
      have e : F.K.mul (F.K.mul (Frobenius.pw F.K g k) (Frobenius.pw F.K g N))
          (F.K.mul (F.K.mul (Frobenius.pw F.K g k) (Frobenius.pw F.K g N)) (F.K.mul (Frobenius.pw F.K g k) (Frobenius.pw F.K g N)))
          = F.K.mul (F.K.mul (Frobenius.pw F.K g k) (F.K.mul (Frobenius.pw F.K g k) (Frobenius.pw F.K g k)))
            (F.K.mul (Frobenius.pw F.K g N) (F.K.mul (Frobenius.pw F.K g N) (Frobenius.pw F.K g N))) := by lib_ring
      rw [e, h3, F.K.mul_comm, F.K.one_mul]
    rw [this]

theorem psi_one_mod3_ff (N : Nat) (hN : 2 * F.h = 3 * N) : ∃ T : Int, psi F F.K.one = 1 + 3 * T := by
  match FinCyclic.primitive_root_ff F with
  | ⟨g, hg⟩ =>
    refine ⟨CubicSign.sumR (fun k => chi F (F.K.add (cb F (Frobenius.pw F.K g k)) F.K.one)) N, ?_⟩
    have h := cube_sum_ff F g hg N hN (fun y => chi F (F.K.add y F.K.one))
    show sumK Z (fun x => chi F (F.K.add (cube F x) F.K.one)) F.els = _
    rw [show (fun x => chi F (F.K.add (cube F x) F.K.one)) = (fun x => chi F (F.K.add (cb F x) F.K.one)) from rfl, h,
      F.K.zero_add, FinConic.chi_one F]

end FinCubicTrace

#print axioms FinCubicTrace.chi_split
#print axioms FinCubicTrace.delta_cube_neg
#print axioms FinCubicTrace.psi_one_even
#print axioms FinCubicTrace.pw_split_ff
#print axioms FinCubicTrace.group3_ff
#print axioms FinCubicTrace.sumR_pows_ff
#print axioms FinCubicTrace.cube_sum_ff
#print axioms FinCubicTrace.psi_one_mod3_ff

Listing 97: lang/FinCubicExact.lean

lang/FinCubicExact.lean

/-
  lang/FinCubicExact — T43, cycle 83: y² = x³ + 1 over every finite field 𝔽_q with q ≡ 1 (mod 3).

    psi_zero, chi_g, chi_gg     ψ(0) = 0, χ(g) = −1 for a primitive root, χ(g²) = 1
    fin_cubic_trace             ∃ u w, u² + 3w² = q ∧ ψ(1) = ±2u
  Port of CubicTrace to the interface (FinCubic, FinCubicTrace, FinCyclic, FinDLog).

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.FinCubicTrace
import lang.CMModular
import lang.EisenNorm

namespace FinCubicExact

open FTAChain YMChain FiniteRecChain FinField FinConic FinMoment FinCubic FinDLog OrdMul

variable {R : Type} [DecidableEq R] (F : FF R)

theorem cancel_N (N : Nat) (hN : N ≠ 0) (X Y : Int) (h : Int.ofNat N * X = Int.ofNat N * Y) : X = Y :=
  CubicTrace.cancel_N N hN X Y h

theorem psi_zero : psi F F.K.zero = 0 := by
  show sumK Z (fun x => chi F (F.K.add (cube F x) F.K.zero)) F.els = 0
  rw [sumK_ext Z _ (chi F) (fun x => by rw [DetChain.add_zero']; exact FinConic.chi_cube F x)]
  exact chi_sum F

theorem chi_g (g : R) (hg : Ord F.K g (2 * F.h)) : chi F g = -1 := by
  have hg0 := FinDLog.g_ne_zero F g hg
  cases FinClass.chi_cases F g with
  | inl h0 => exact absurd h0 (FinClass.chi_ne_zero F g hg0)
  | inr h =>
    cases h with
    | inr h1 => exact h1
    | inl h1 =>
      exfalso
      have hp := FinClass.chi_pw_one F g h1
      match hg.2 F.h hp with
      | ⟨c, hc⟩ =>
        have hpos := FinCyclic.h_pos F
        cases c with
        | zero => rw [Nat.mul_zero] at hc; rw [hc] at hpos; exact Nat.not_succ_le_zero 0 hpos
        | succ c =>
          have : 2 * F.h ≤ F.h := by
            calc 2 * F.h ≤ 2 * F.h * c + 2 * F.h := Nat.le_add_left _ _
              _ = F.h := hc.symm
          have h2 : F.h < 2 * F.h := by rw [Nat.two_mul]; exact Nat.lt_add_of_pos_right hpos
          exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h2 this)

theorem chi_gg (g : R) (hg0 : g ≠ F.K.zero) : chi F (F.K.mul g g) = 1 := chi_sq F g hg0

theorem fin_cubic_trace (N : Nat) (hN : 2 * F.h = 3 * N) :
    ∃ u w : Nat, u * u + 3 * (w * w) = 2 * F.h + 1 ∧
      (psi F F.K.one = Int.ofNat (2 * u) ∨ psi F F.K.one = -Int.ofNat (2 * u)) := by
  have hN0 : N ≠ 0 := fun e => by
    rw [e] at hN
    have := FinCyclic.h_pos F
    have h2 : 2 * 1 ≤ 2 * F.h := Nat.mul_le_mul_left 2 this
    rw [hN] at h2; exact Nat.not_succ_le_zero 1 h2
  match FinCyclic.primitive_root_ff F, FinCyclic.split_pow_ff F 1 0 ⟨N, by rw [hN]; lib_nat⟩ with
  | ⟨g, hg⟩, ⟨w, hw3', hw1'⟩ =>
    have hw1 : w ≠ F.K.one := fun e => hw1' (by rw [e]; exact TwoPower.pwone F.K _)
    have hw3 : cube F w = F.K.one := by
      rw [← hw3']; show _ = F.K.mul (F.K.mul (F.K.mul F.K.one w) w) w; unfold cube; lib_ring
    have hg0 : g ≠ F.K.zero := FinDLog.g_ne_zero F g hg
    have hP : PP F = 3 * Int.ofNat N + 1 := by
      show Int.ofNat (2 * F.h + 1) = _
      rw [hN]; rfl
    -- second moment, grouped
    have hpw : pows F g (2 * F.h) = pows F g (3 * N) := congrArg (pows F g) hN
    have M := moment_c F w hw1 hw3
    rw [sum_split_ff F Z g hg _ F.comp, hpw,
      FinCubicTrace.group3_ff F g hg0 (fun a => psi F a * psi F a) (fun a t ht => psi_sq_const F a t ht) N,
      psi_zero F] at M
    have S2 : psi F F.K.one * psi F F.K.one + psi F g * psi F g + psi F (F.K.mul g g) * psi F (F.K.mul g g)
        = 6 * PP F := by
      apply cancel_N N hN0
      have : Int.ofNat N * (6 * PP F) = 2 * PP F * (PP F + -1) := by rw [hP]; lib_int
      rw [this, ← M]
      show Int.ofNat N * _ = (0 * 0 : Int) + Int.ofNat N * _
      lib_int
    -- twisted sum, grouped
    have T := twisted F
    rw [sum_split_ff F Z g hg _ F.comp, hpw,
      FinCubicTrace.group3_ff F g hg0 (fun a => chi F a * psi F a) (fun a t ht => chi_psi_const F a t ht) N,
      chi_zero, FinConic.chi_one F, chi_g F g hg, chi_gg F g hg0] at T
    have S1 : psi F F.K.one + -1 * psi F g + psi F (F.K.mul g g) = 0 := by
      apply cancel_N N hN0
      rw [show Int.ofNat N * 0 = 0 by lib_int, ← T]
      show _ = 0 * psi F F.K.zero + _
      lib_int
    -- ψ(1) = 2c
    have hE := FinCubicTrace.psi_one_even F w hw1 hw3
    generalize hc : Int.ofNat F.h + -1 + -1 * sumK Z (fun x => FinCubicTrace.nq F (F.K.add (cube F x) F.K.one)) F.els = c at hE
    -- (A₂ + c)² + 3c² = 3p
    have key : (psi F (F.K.mul g g) + c) * (psi F (F.K.mul g g) + c) + 3 * (c * c) = 3 * PP F := by
      have h1 : psi F g = psi F F.K.one + psi F (F.K.mul g g) := by
        have e : psi F F.K.one + psi F (F.K.mul g g)
            = (psi F F.K.one + -1 * psi F g + psi F (F.K.mul g g)) + psi F g := by lib_int
        rw [S1] at e
        rw [e]; lib_int
      rw [h1, hE] at S2
      apply cancel_N 2 (fun e => Nat.noConfusion e)
      show (2 : Int) * _ = 2 * _
      rw [show (2 : Int) * (3 * PP F) = 6 * PP F by lib_int, ← S2]
      lib_int
    match CMModular.nat_sq (psi F (F.K.mul g g) + c), CMModular.nat_sq c with
    | ⟨n, hn, _⟩, ⟨u, hu, hsu⟩ =>
      rw [hn, hu] at key
      have key' : Int.ofNat (n * n + 3 * (u * u)) = Int.ofNat (3 * (2 * F.h + 1)) := key
      have hnat := Int.ofNat.inj key'
      have h3 : 3 ∣ n * n := by
        have a : Dv (2 + 1) (3 * (u * u) + n * n) := ⟨2 * F.h + 1, by rw [Nat.add_comm, hnat]; lib_nat⟩
        have b : Dv (2 + 1) (3 * (u * u)) := ⟨u * u, by lib_nat⟩
        exact PrimRoot.dvd_of_dv (dv_sub 2 _ _ b a)
      have h3' : 3 ∣ n := by
        cases LibGcd.euclid_prime EisenNorm.prime3' h3 with
        | inl x => exact x
        | inr x => exact x
      match h3' with
      | ⟨wv, ewv⟩ =>
        refine ⟨u, wv, ?_, ?_⟩
        · apply Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 2)
          rw [← hnat, ewv]; lib_nat
        · rw [hE]
          cases hsu with
          | inl e => left; rw [e]; show 2 * Int.ofNat u = Int.ofNat (2 * u); rfl
          | inr e => right; rw [e]; show 2 * -Int.ofNat u = -Int.ofNat (2 * u)
                     show 2 * -Int.ofNat u = -(2 * Int.ofNat u); lib_int


end FinCubicExact

#print axioms FinCubicExact.cancel_N
#print axioms FinCubicExact.psi_zero
#print axioms FinCubicExact.chi_g
#print axioms FinCubicExact.chi_gg
#print axioms FinCubicExact.fin_cubic_trace

Listing 98: lang/FinCubicSign.lean

lang/FinCubicSign.lean

/-
  lang/FinCubicSign — T43, cycle 85: the exact sign and Hasse's bound for y² = x³ + 1 over every 𝔽_q with q ≡ 1 (mod 3).

    fin_cubic_exact   ∃ a b ∈ ℤ, a² + 3b² = q, a ≡ 1 (mod 3), −Σ_x χ(x³ + 1) = 2a
    fin_hasse3        (Σ_x χ(x³ + 1))² ≤ 4q                    (Hasse's bound for this curve over every such field)
  Port of CubicSign.cubic_exact to the interface: the Frobenius trace of y² = x³ + 1 over 𝔽_q is the trace 2a of the
  primary Eisenstein integer a + b√−3 of norm q (q = p, p², … — the Hecke character of ℚ(√−3) at every prime power).

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.FinCubicExact
import lang.CubicSign

namespace FinCubicSign

open FTAChain YMChain FiniteRecChain FinField FinConic FinMoment FinCubic

variable {R : Type} [DecidableEq R] (F : FF R)

theorem fin_cubic_exact (N : Nat) (hN : 2 * F.h = 3 * N) :
    ∃ a b : Int, a * a + 3 * (b * b) = Int.ofNat (2 * F.h + 1) ∧ LibGcd.IDv 3 (a - 1) ∧ -psi F F.K.one = 2 * a := by
  match FinCubicExact.fin_cubic_trace F N hN, FinCubicTrace.psi_one_mod3_ff F N hN with
  | ⟨u, w, huw, hs⟩, ⟨T, hT⟩ =>
    have hs' : psi F F.K.one = 2 * Int.ofNat u ∨ psi F F.K.one = 2 * -Int.ofNat u := by
      cases hs with
      | inl e => left; rw [e]; rfl
      | inr e => right; rw [e]; show -(2 * Int.ofNat u) = 2 * -Int.ofNat u; lib_int
    -- 1 + T is even
    have hev : ∃ s, 1 + T = 2 * s := by
      match GaussInt.int_even_odd (1 + T) with
      | Or.inl h => exact h
      | Or.inr ⟨s0, h0⟩ =>
        exfalso
        have hodd : psi F F.K.one = 2 * (s0 + T) + 1 := by
          rw [hT]
          calc 1 + 3 * T = (1 + T) + 2 * T := by lib_int
            _ = 2 * (s0 + T) + 1 := by rw [h0]; lib_int
        cases hs' with
        | inl e => exact GaussInt.int_odd_ne_even (s0 + T) (Int.ofNat u) (hodd.symm.trans e)
        | inr e => exact GaussInt.int_odd_ne_even (s0 + T) (-Int.ofNat u) (hodd.symm.trans e)
    match hev with
    | ⟨s0, hs2⟩ =>
      have hTs : T = 2 * s0 + -1 := by
        calc T = (1 + T) + -1 := by lib_int
          _ = 2 * s0 + -1 := by rw [hs2]
      have hpsi : psi F F.K.one = 2 * (3 * s0 + -1) := by rw [hT, hTs]; lib_int
      have hu : 3 * s0 + -1 = Int.ofNat u ∨ 3 * s0 + -1 = -Int.ofNat u := by
        cases hs' with
        | inl e => left; exact CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) _ _ (hpsi.symm.trans e)
        | inr e => right; exact CubicTrace.cancel_N 2 (fun e => Nat.noConfusion e) _ _ (hpsi.symm.trans e)
      refine ⟨1 + -3 * s0, Int.ofNat w, ?_, ⟨-s0, by lib_int⟩, ?_⟩
      · have hsq : (1 + -3 * s0) * (1 + -3 * s0) = Int.ofNat u * Int.ofNat u := by
          cases hu with
          | inl e => rw [← e]; lib_int
          | inr e =>
            have : Int.ofNat u = -(3 * s0 + -1) := by rw [e]; lib_int
            rw [this]; lib_int
        rw [hsq]
        show Int.ofNat (u * u + 3 * (w * w)) = _
        rw [huw]
      · rw [hpsi]; lib_int


theorem sq_nn : ∀ x : Int, ∃ n : Nat, x * x = Int.ofNat n
  | Int.ofNat n => ⟨n * n, rfl⟩
  | Int.negSucc n => ⟨(n + 1) * (n + 1), rfl⟩

/-- **Hasse's bound**: a² ≤ 4q for a = −Σ_x χ(x³ + 1). -/
theorem fin_hasse3 (N : Nat) (hN : 2 * F.h = 3 * N) :
    ∃ s : Nat, psi F F.K.one * psi F F.K.one + Int.ofNat s = 4 * Int.ofNat (2 * F.h + 1) := by
  match fin_cubic_exact F N hN with
  | ⟨a, b, hab, _, hs⟩ =>
    match sq_nn b with
    | ⟨n, hn⟩ =>
      refine ⟨12 * n, ?_⟩
      have e : psi F F.K.one = -(2 * a) := by rw [← hs]; lib_int
      rw [e, ← hab, show Int.ofNat (12 * n) = 12 * Int.ofNat n from rfl, ← hn]
      lib_int

end FinCubicSign

#print axioms FinCubicSign.fin_cubic_exact
#print axioms FinCubicSign.sq_nn
#print axioms FinCubicSign.fin_hasse3

Listing 99: lang/CubicSuper.lean

lang/CubicSuper.lean

/-
  lang/CubicSuper — T43, cycle 84: base change of y² = x³ + 1 to ℚ(√−3) at inert primes p ≡ 11 (mod 12).

  p = 12i + 11 = 4j + 3 (j = 3i + 2), 𝔽_{p²} = 𝔽_p[i] (Fp2Inst.ff2), q = p² ≡ 1 (mod 3).
    inert_norm3     u² + 3w² = p² ⇒ u = p, w = 0            (−3 is not a square mod p: Eisen.inert_neg3)
    cubic_super     Σ_{z ∈ 𝔽_{p²}} χ(z³ + 1) = 2p,  i.e. #E(𝔽_{p²}) = (p + 1)²
  With FinCubicExact (ψ(1) = ±2u, u² + 3w² = p²) and ψ(1) ≡ 1 (mod 3) (FinCubicTrace) — the base change of the CM form of
  y² = x³ + 1 to ℚ(√−3) has a_{(p)} = −2p at these inert primes.

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.FinCubicExact
import lang.Fp2Count
import lang.Eisen

namespace CubicSuper

open FTAChain ZMod YMChain FiniteRecChain FinField FinConic FinMoment FinCubic

variable (j : Nat)

theorem inert_norm3 (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (h3 : ¬ Dv (1 + 2) ((2 * (2 * j) + 1) + 1)) (hj : 2 ≤ 2 * (2 * j) + 1) (u w : Nat)
    (h : u * u + 3 * (w * w) = (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1)) : u = (2 * (2 * j) + 1 + 1 + 1) ∧ w = 0 := by
  have hz : (ring (2 * (2 * j) + 1 + 1)).add ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (mk _ u)) ((ring (2 * (2 * j) + 1 + 1)).mul (Eisen.three (2 * (2 * j) + 1)) ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ w) (mk _ w))) = (ring (2 * (2 * j) + 1 + 1)).zero := by
    rw [show Eisen.three (2 * (2 * j) + 1) = mk _ 3 from EisenNorm.three_mk _, TwoPower.mk_mul, TwoPower.mk_mul, TwoPower.mk_mul,
      PrimeOneModQ.mk_add, h]
    exact Subtype.ext (dv_md_zero _ _ ⟨(2 * (2 * j) + 1 + 1 + 1), rfl⟩)
  have hw : mk ((2 * (2 * j) + 1) + 1) w = (ring (2 * (2 * j) + 1 + 1)).zero := by
    by_cases hw0 : mk ((2 * (2 * j) + 1) + 1) w = (ring (2 * (2 * j) + 1 + 1)).zero
    · exact hw0
    · exfalso
      apply h3
      let t := (ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (ConicSum.iv (2 * j) (mk _ w))
      have hwi := ConicSum.iv_mul_self (2 * j) hpr _ hw0
      apply Eisen.inert_neg3 (2 * (2 * j) + 1) hpr hj t
      have hu2 : (ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (mk _ u) = (ring (2 * (2 * j) + 1 + 1)).neg ((ring (2 * (2 * j) + 1 + 1)).mul (Eisen.three (2 * (2 * j) + 1)) ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ w) (mk _ w))) := DetChain.neg_unique (ring (2 * (2 * j) + 1 + 1)) _ _ hz
      show (ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (ConicSum.iv (2 * j) (mk _ w))) ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (ConicSum.iv (2 * j) (mk _ w))) = _
      rw [show (ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (ConicSum.iv (2 * j) (mk _ w))) ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (ConicSum.iv (2 * j) (mk _ w)))
          = (ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ u) (mk _ u)) ((ring (2 * (2 * j) + 1 + 1)).mul (ConicSum.iv (2 * j) (mk _ w)) (ConicSum.iv (2 * j) (mk _ w))) by lib_ring, hu2]
      rw [show (ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).neg ((ring (2 * (2 * j) + 1 + 1)).mul (Eisen.three (2 * (2 * j) + 1)) ((ring (2 * (2 * j) + 1 + 1)).mul (mk _ w) (mk _ w)))) ((ring (2 * (2 * j) + 1 + 1)).mul (ConicSum.iv (2 * j) (mk _ w)) (ConicSum.iv (2 * j) (mk _ w)))
          = (ring (2 * (2 * j) + 1 + 1)).neg ((ring (2 * (2 * j) + 1 + 1)).mul (Eisen.three (2 * (2 * j) + 1)) ((ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).mul (ConicSum.iv (2 * j) (mk _ w)) (mk _ w)) ((ring (2 * (2 * j) + 1 + 1)).mul (ConicSum.iv (2 * j) (mk _ w)) (mk _ w)))) by lib_ring, hwi]
      lib_ring
  have hu : mk ((2 * (2 * j) + 1) + 1) u = (ring (2 * (2 * j) + 1 + 1)).zero := by
    rw [hw, DetChain.mul_zero, DetChain.mul_zero, DetChain.add_zero'] at hz
    cases nozd _ hpr _ _ hz with
    | inl e => exact e
    | inr e => exact e
  match md_zero_dv _ _ (congrArg Subtype.val hu), md_zero_dv _ _ (congrArg Subtype.val hw) with
  | ⟨u1, hu1⟩, ⟨w1, hw1⟩ =>
    have h1 : (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1) * (u1 * u1 + 3 * (w1 * w1)) = (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1) * 1 := by
      have e1 : (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1) * (u1 * u1 + 3 * (w1 * w1)) = u * u + 3 * (w * w) := by rw [hu1, hw1]; lib_nat
      rw [e1, h, Nat.mul_one]
    have h2 := Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) h1
    have hw0 : w1 = 0 := by
      cases w1 with
      | zero => rfl
      | succ w2 =>
        exfalso
        have : 3 ≤ u1 * u1 + 3 * ((w2 + 1) * (w2 + 1)) :=
          Nat.le_trans (Nat.mul_le_mul_left 3 (Nat.mul_le_mul (Nat.succ_le_succ (Nat.zero_le w2)) (Nat.succ_le_succ (Nat.zero_le w2)))) (Nat.le_add_left _ _)
        rw [h2] at this; exact Nat.not_succ_le_self 1 (Nat.le_trans (Nat.le_succ 2) this)
    rw [hw0] at h2
    have hu1' : u1 = 1 := by
      cases u1 with
      | zero => exact absurd h2 (fun e => Nat.noConfusion e)
      | succ u2 =>
        cases u2 with
        | zero => rfl
        | succ u3 =>
          exfalso
          have : 4 ≤ (u3 + 1 + 1) * (u3 + 1 + 1) := Nat.mul_le_mul (Nat.le_add_left 2 u3) (Nat.le_add_left 2 u3)
          rw [show (u3 + 1 + 1) * (u3 + 1 + 1) = (u3 + 1 + 1) * (u3 + 1 + 1) + 3 * (0 * 0) by lib_nat, h2] at this
          exact Nat.not_succ_le_self 1 (Nat.le_trans (Nat.le_add_left 2 2) this)
    rw [hu1, hw1, hu1', hw0]; exact ⟨by lib_nat, by lib_nat⟩

theorem three_ne_one : ∀ X : Int, 3 * X ≠ 1
  | Int.ofNat 0 => fun e => Int.noConfusion e (fun h => Nat.noConfusion h)
  | Int.ofNat (n + 1) => fun e => by
    have h : 3 * n + 2 + 1 = 0 + 1 := by
      have := Int.ofNat.inj e
      rw [show 3 * n + 2 + 1 = 3 * (n + 1) by lib_nat]; exact this
    exact Nat.noConfusion (Nat.succ.inj h)
  | Int.negSucc n => fun e => Int.noConfusion e

theorem h_eq (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    (Fp2Inst.ff2 k hpr hn).h = (2 * k + 1 + 1 + 1 + 1) * (k + 1) := rfl

theorem sq_id (k : Nat) : 2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) + 1 = (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) := by lib_nat

theorem not_dv3 (a c q : Nat) (hc : a + 2 = 3 * c) (hq : a = q * (1 + 2)) : False := by
  have e1 : Int.ofNat a + 2 = 3 * Int.ofNat c := congrArg Int.ofNat hc
  have e2 : Int.ofNat a = Int.ofNat q * 3 := congrArg Int.ofNat hq
  apply three_ne_one (Int.ofNat q + -1 * Int.ofNat c + 1)
  calc 3 * (Int.ofNat q + -1 * Int.ofNat c + 1) = Int.ofNat q * 3 + -1 * (3 * Int.ofNat c) + 3 := by lib_int
    _ = 1 := by rw [← e1, ← e2]; lib_int

theorem sign3 (a c : Nat) (hc : a + 2 = 3 * c) (T : Int) (e : -Int.ofNat (2 * (a + 1)) = 1 + 3 * T) : False := by
  have e1 : Int.ofNat a + 2 = 3 * Int.ofNat c := congrArg Int.ofNat hc
  have e3 : Int.ofNat (2 * (a + 1)) = 2 * (Int.ofNat a + 1) := rfl
  rw [e3] at e
  apply three_ne_one (T + 2 * Int.ofNat c)
  calc 3 * (T + 2 * Int.ofNat c) = (1 + 3 * T) + 2 * (3 * Int.ofNat c) + -1 := by lib_int
    _ = 1 := by rw [← e, ← e1]; lib_int

/-- **Base change of y² = x³ + 1 to ℚ(√−3) at p = 12i + 11**: over 𝔽_{p²}, Σ_z χ(z³ + 1) = 2p. -/
theorem cubic_super (i : Nat) (hpr : ∀ d, Dv d (2 * (2 * (3 * i + 2)) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * (3 * i + 2)) + 1 + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * (3 * i + 2)) + 1 + 1)) ((ring (2 * (2 * (3 * i + 2)) + 1 + 1)).neg (ring (2 * (2 * (3 * i + 2)) + 1 + 1)).one) (2 * (3 * i + 2) + 1) = (ring (2 * (2 * (3 * i + 2)) + 1 + 1)).neg (ring (2 * (2 * (3 * i + 2)) + 1 + 1)).one) :
    psi (Fp2Inst.ff2 (2 * (3 * i + 2)) hpr hn) (Fp2Inst.ff2 (2 * (3 * i + 2)) hpr hn).K.one = Int.ofNat (2 * (2 * (2 * (3 * i + 2)) + 1 + 1 + 1)) := by
  have hc : (2 * (2 * (3 * i + 2)) + 1) + 1 + 2 = 3 * (4 * i + 4) := by lib_nat
  have h3 : ¬ Dv (1 + 2) ((2 * (2 * (3 * i + 2)) + 1) + 1) := fun ⟨q, hq⟩ => not_dv3 _ _ q hc hq
  have hj : 2 ≤ (2 * (2 * (3 * i + 2)) + 1) := Nat.succ_le_succ (Nat.le_add_left 1 _)
  have hN : 2 * (Fp2Inst.ff2 (2 * (3 * i + 2)) hpr hn).h = 3 * (2 * ((4 * i + 4) * (2 * (3 * i + 2) + 1))) := by
    rw [h_eq]; lib_nat
  match FinCubicExact.fin_cubic_trace (Fp2Inst.ff2 (2 * (3 * i + 2)) hpr hn) _ hN, FinCubicTrace.psi_one_mod3_ff (Fp2Inst.ff2 (2 * (3 * i + 2)) hpr hn) _ hN with
  | ⟨u, w, huw, hs⟩, ⟨T, hT⟩ =>
    have huw' : u * u + 3 * (w * w) = (2 * (2 * (3 * i + 2)) + 1 + 1 + 1) * (2 * (2 * (3 * i + 2)) + 1 + 1 + 1) := by
      rw [h_eq] at huw; exact huw.trans (sq_id (2 * (3 * i + 2)))
    match inert_norm3 (3 * i + 2) hpr h3 hj u w huw' with
    | ⟨hu, _⟩ =>
      rw [hu] at hs
      cases hs with
      | inl e => exact e
      | inr e =>
        exact absurd (e.symm.trans hT) (fun h => sign3 _ _ hc T h)

end CubicSuper

#print axioms CubicSuper.inert_norm3
#print axioms CubicSuper.three_ne_one
#print axioms CubicSuper.h_eq
#print axioms CubicSuper.sq_id
#print axioms CubicSuper.not_dv3
#print axioms CubicSuper.sign3
#print axioms CubicSuper.cubic_super

Listing 100: lang/CubicSuper2.lean

lang/CubicSuper2.lean

/-
  lang/CubicSuper2 — T43, cycle 91: base change of y² = x³ + 1 to ℚ(√−3) at every inert prime p ≡ 2 (mod 3).

  p = 2k + 3 with 3 | p + 1, 𝔽_{p²} = 𝔽_p[√n] for any non-residue n (Fq2Inst.ffq), q = p² ≡ 1 (mod 3).
    inert_norm3'    u² + 3w² = p² ⇒ u = p, w = 0                   (−3 is not a square mod p: Eisen.inert_neg3)
    cubic_super2    Σ_{z ∈ 𝔽_{p²}} χ(z³ + 1) = 2p,  #E(𝔽_{p²}) = (p + 1)²,  a_{(p)} = −2p
    base_change2    a_{(p)} = a_p² − 2p                              (a_p = 0: SexticFamily.psi_inert)
  Extends CubicSuper (p ≡ 11 mod 12, 𝔽_p[i]) to p ≡ 5 (mod 12) as well, so to every inert prime of ℚ(√−3) with p > 3.

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.CubicSuper
import lang.Fq2Inst
import lang.BaseChange

namespace CubicSuper2

open FTAChain ZMod YMChain FiniteRecChain FinField FinConic FinMoment FinCubic

variable (k : Nat)

theorem inert_norm3' (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (h3 : ¬ Dv (1 + 2) ((2 * k + 1) + 1)) (hj : 2 ≤ 2 * k + 1) (u w : Nat)
    (h : u * u + 3 * (w * w) = (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1)) : u = (2 * k + 1 + 1 + 1) ∧ w = 0 := by
  have hz : (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul (mk _ u) (mk _ u)) ((ring (2 * k + 1 + 1)).mul (Eisen.three (2 * k + 1)) ((ring (2 * k + 1 + 1)).mul (mk _ w) (mk _ w))) = (ring (2 * k + 1 + 1)).zero := by
    rw [show Eisen.three (2 * k + 1) = mk _ 3 from EisenNorm.three_mk _, TwoPower.mk_mul, TwoPower.mk_mul, TwoPower.mk_mul,
      PrimeOneModQ.mk_add, h]
    exact Subtype.ext (dv_md_zero _ _ ⟨(2 * k + 1 + 1 + 1), rfl⟩)
  have hw : mk ((2 * k + 1) + 1) w = (ring (2 * k + 1 + 1)).zero := by
    by_cases hw0 : mk ((2 * k + 1) + 1) w = (ring (2 * k + 1 + 1)).zero
    · exact hw0
    · exfalso
      apply h3
      let t := (ring (2 * k + 1 + 1)).mul (mk _ u) (ConicSum.iv k (mk _ w))
      have hwi := ConicSum.iv_mul_self k hpr _ hw0
      apply Eisen.inert_neg3 (2 * k + 1) hpr hj t
      have hu2 : (ring (2 * k + 1 + 1)).mul (mk _ u) (mk _ u) = (ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul (Eisen.three (2 * k + 1)) ((ring (2 * k + 1 + 1)).mul (mk _ w) (mk _ w))) := DetChain.neg_unique (ring (2 * k + 1 + 1)) _ _ hz
      show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul (mk _ u) (ConicSum.iv k (mk _ w))) ((ring (2 * k + 1 + 1)).mul (mk _ u) (ConicSum.iv k (mk _ w))) = _
      rw [show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul (mk _ u) (ConicSum.iv k (mk _ w))) ((ring (2 * k + 1 + 1)).mul (mk _ u) (ConicSum.iv k (mk _ w)))
          = (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul (mk _ u) (mk _ u)) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k (mk _ w)) (ConicSum.iv k (mk _ w))) by lib_ring, hu2]
      rw [show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul (Eisen.three (2 * k + 1)) ((ring (2 * k + 1 + 1)).mul (mk _ w) (mk _ w)))) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k (mk _ w)) (ConicSum.iv k (mk _ w)))
          = (ring (2 * k + 1 + 1)).neg ((ring (2 * k + 1 + 1)).mul (Eisen.three (2 * k + 1)) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k (mk _ w)) (mk _ w)) ((ring (2 * k + 1 + 1)).mul (ConicSum.iv k (mk _ w)) (mk _ w)))) by lib_ring, hwi]
      lib_ring
  have hu : mk ((2 * k + 1) + 1) u = (ring (2 * k + 1 + 1)).zero := by
    rw [hw, DetChain.mul_zero, DetChain.mul_zero, DetChain.add_zero'] at hz
    cases nozd _ hpr _ _ hz with
    | inl e => exact e
    | inr e => exact e
  match md_zero_dv _ _ (congrArg Subtype.val hu), md_zero_dv _ _ (congrArg Subtype.val hw) with
  | ⟨u1, hu1⟩, ⟨w1, hw1⟩ =>
    have h1 : (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) * (u1 * u1 + 3 * (w1 * w1)) = (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) * 1 := by
      have e1 : (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) * (u1 * u1 + 3 * (w1 * w1)) = u * u + 3 * (w * w) := by rw [hu1, hw1]; lib_nat
      rw [e1, h, Nat.mul_one]
    have h2 := Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) h1
    have hw0 : w1 = 0 := by
      cases w1 with
      | zero => rfl
      | succ w2 =>
        exfalso
        have : 3 ≤ u1 * u1 + 3 * ((w2 + 1) * (w2 + 1)) :=
          Nat.le_trans (Nat.mul_le_mul_left 3 (Nat.mul_le_mul (Nat.succ_le_succ (Nat.zero_le w2)) (Nat.succ_le_succ (Nat.zero_le w2)))) (Nat.le_add_left _ _)
        rw [h2] at this; exact Nat.not_succ_le_self 1 (Nat.le_trans (Nat.le_succ 2) this)
    rw [hw0] at h2
    have hu1' : u1 = 1 := by
      cases u1 with
      | zero => exact absurd h2 (fun e => Nat.noConfusion e)
      | succ u2 =>
        cases u2 with
        | zero => rfl
        | succ u3 =>
          exfalso
          have : 4 ≤ (u3 + 1 + 1) * (u3 + 1 + 1) := Nat.mul_le_mul (Nat.le_add_left 2 u3) (Nat.le_add_left 2 u3)
          rw [show (u3 + 1 + 1) * (u3 + 1 + 1) = (u3 + 1 + 1) * (u3 + 1 + 1) + 3 * (0 * 0) by lib_nat, h2] at this
          exact Nat.not_succ_le_self 1 (Nat.le_trans (Nat.le_add_left 2 2) this)
    rw [hu1, hw1, hu1', hw0]; exact ⟨by lib_nat, by lib_nat⟩

theorem h_eq (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) : (Fq2Inst.ffq k hpr n hn).h = (2 * k + 1 + 1 + 1 + 1) * (k + 1) := rfl

theorem hN_form (c : Nat) (hc : 2 * k + 1 + 1 + 1 + 1 = 3 * c) :
    2 * ((2 * k + 1 + 1 + 1 + 1) * (k + 1)) = 3 * (2 * (c * (k + 1))) := by rw [hc]; lib_nat

theorem k_pos (c : Nat) (hc : 2 * k + 1 + 1 + 1 + 1 = 3 * c) : 2 ≤ 2 * k + 1 := by
  cases k with
  | zero =>
    exfalso
    have e : (4 : Int) = 3 * Int.ofNat c := congrArg Int.ofNat hc
    apply CubicSuper.three_ne_one (Int.ofNat c + -1)
    calc 3 * (Int.ofNat c + -1) = 3 * Int.ofNat c + -3 := by lib_int
      _ = 1 := by rw [← e]; rfl
  | succ k' => exact Nat.succ_le_succ (Nat.le_add_left 1 _)

/-- **Base change of y² = x³ + 1 at every inert prime p ≡ 2 (mod 3)**: over 𝔽_{p²}, Σ_z χ(z³ + 1) = 2p. -/
theorem cubic_super2 (c : Nat) (hc : 2 * k + 1 + 1 + 1 + 1 = 3 * c) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1))
    (n : Zp (2 * k + 1 + 1)) (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    psi (Fq2Inst.ffq k hpr n hn) (Fq2Inst.ffq k hpr n hn).K.one = Int.ofNat (2 * (2 * k + 1 + 1 + 1)) := by
  have hc' : (2 * k + 1 + 1) + 2 = 3 * c := hc
  have h3 : ¬ Dv (1 + 2) ((2 * k + 1) + 1) := fun ⟨q, hq⟩ => CubicSuper.not_dv3 _ _ q hc' hq
  have hj := k_pos k c hc
  have hN : 2 * (Fq2Inst.ffq k hpr n hn).h = 3 * (2 * (c * (k + 1))) := by rw [h_eq]; exact hN_form k c hc
  match FinCubicExact.fin_cubic_trace (Fq2Inst.ffq k hpr n hn) _ hN, FinCubicTrace.psi_one_mod3_ff (Fq2Inst.ffq k hpr n hn) _ hN with
  | ⟨u, w, huw, hs⟩, ⟨T, hT⟩ =>
    have huw' : u * u + 3 * (w * w) = (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) := by
      rw [h_eq] at huw; exact huw.trans (CubicSuper.sq_id k)
    match inert_norm3' k hpr h3 hj u w huw' with
    | ⟨hu, _⟩ =>
      rw [hu] at hs
      cases hs with
      | inl e => exact e
      | inr e => exact absurd (e.symm.trans hT) (fun h => CubicSuper.sign3 _ _ hc' T h)

theorem e_form (c : Nat) (hc : 2 * k + 1 + 1 + 1 + 1 = 3 * c) : ∃ e, 3 * e = 2 * (2 * k + 1 + 1) + 1 := by
  cases c with
  | zero => exact absurd hc (fun h => Nat.noConfusion h)
  | succ c' =>
    have h' : 2 * k + 1 + 1 + 1 + 1 = 3 * c' + 1 + 1 + 1 := by rw [hc]; lib_nat
    have h1 : 2 * k + 1 = 3 * c' := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h'))
    refine ⟨2 * c' + 1, ?_⟩
    rw [show 2 * (2 * k + 1 + 1) + 1 = 2 * (2 * k + 1) + 3 by lib_nat, h1]; lib_nat

/-- **a_{(p)} = a_p² − 2p at every inert prime of ℚ(√−3)**, p > 3. -/
theorem base_change2 (c : Nat) (hc : 2 * k + 1 + 1 + 1 + 1 = 3 * c) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1))
    (n : Zp (2 * k + 1 + 1)) (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    -psi (Fq2Inst.ffq k hpr n hn) (Fq2Inst.ffq k hpr n hn).K.one = (-CubicJacob.psi k (ring (2 * k + 1 + 1)).one) * (-CubicJacob.psi k (ring (2 * k + 1 + 1)).one) + -2 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  match e_form k c hc with
  | ⟨e, he⟩ =>
  rw [cubic_super2 k c hc hpr n hn, SexticFamily.psi_inert k hpr e he]
  show -(2 * Int.ofNat (2 * k + 1 + 1 + 1)) = _
  lib_int

end CubicSuper2

#print axioms CubicSuper2.inert_norm3'
#print axioms CubicSuper2.h_eq
#print axioms CubicSuper2.hN_form
#print axioms CubicSuper2.k_pos
#print axioms CubicSuper2.cubic_super2
#print axioms CubicSuper2.e_form
#print axioms CubicSuper2.base_change2

78.36 Base change at inert primes: \(a_{(p)}=a_p^2-2p\)

With \(a_p=-\sum _{x\in \mathbb F_p}\chi (\cdot )\) and \(a_{(p)}=-\sum _{z\in \mathbb F_{p^2}}\chi (\cdot )\),

\[ \begin {aligned} y^2=x^3-x,\ p\equiv 3\ (4)&:\ a_{(p)}=a_p^2-2p,\\ y^2=x^3+1,\ p\equiv 2\ (3),\ p>3&:\ a_{(p)}=a_p^2-2p . \end {aligned} \]
This is \(\alpha ^2+\beta ^2=(\alpha +\beta )^2-2\alpha \beta \) for the Frobenius eigenvalues, the trace of \(\mathrm {Frob}_p^2\), which is the Frobenius at the inert prime \((p)\). The steps are
\[ \begin {aligned} \chi (-1)&=(-1)^{(p-1)/2}=-1\ \Rightarrow \ \textstyle \sum _x\chi (x^3-x)=0,\\ 3e&=2(p-1)+1\ \Rightarrow \ x\mapsto x^3\ \text {bijective}\ \Rightarrow \ \textstyle \sum _x\chi (x^3+1)=0,\\ a_{(p)}&=-2p\quad (\text {sections}~78.24,~78.35),\\ -2p&=0^2-2p . \end {aligned} \]

Listing 101: lang/BaseChange.lean

lang/BaseChange.lean

/-
  lang/BaseChange — T43, cycle 87: base change of GL2 at an inert prime, a_{(p)} = a_p² − 2p, for both CM curves.

  Over ℚ the Frobenius at p has eigenvalues α, β with α + β = a_p, αβ = p; over the quadratic field where p is inert the
  Frobenius at (p) is Frob_p², with trace α² + β² = a_p² − 2p. Proved as integer identities between the point counts over
  𝔽_p and over 𝔽_{p²}:
    chi_negone            χ(−1) = −1 in 𝔽_p for p = 4j + 3
    base_change_i         y² = x³ − x, p = 4j + 3:   −Σ_{𝔽_{p²}} χ(z³ − z) = (−Σ_{𝔽_p} χ(x³ − x))² − 2p
    base_change_omega     y² = x³ + 1, p = 12i + 11: −Σ_{𝔽_{p²}} χ(z³ + 1) = (−Σ_{𝔽_p} χ(x³ + 1))² − 2p
  (QuarticFamily.phi_inert, SexticFamily.psi_inert give a_p = 0; SuperSing, CubicSuper give a_{(p)} = −2p.)

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.SuperSing
import lang.CubicSuper
import lang.QuarticFamily
import lang.SexticFamily

namespace BaseChange

open FTAChain ZMod YMChain FiniteRecChain FinConic

theorem chi_negone (j : Nat)
    (hn : Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) (2 * j + 1) = (ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) :
    QChar.chi (2 * j) ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) = -1 := by
  have h0 : (ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one ≠ (ring (2 * (2 * j) + 1 + 1)).zero := fun e => by
    apply PowerSum.one_ne_zero' (2 * (2 * j) + 1)
    have := congrArg (fun z => (ring (2 * (2 * j) + 1 + 1)).add (ring (2 * (2 * j) + 1 + 1)).one z) e
    rw [(ring (2 * (2 * j) + 1 + 1)).add_neg, DetChain.add_zero'] at this
    exact this.symm
  unfold QChar.chi
  rw [ite_neg' _ _ h0, ite_neg' _ _ (by rw [hn]; exact fun e => GaussSplit.one_ne_negone _ e.symm)]

/-- **Base change at an inert prime of ℚ(i).** -/
theorem base_change_i (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) (2 * j + 1) = (ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) :
    -sumK Z (fun x => FinField.chi (Fp2Inst.ff2 (2 * j) hpr hn) (FinSign.f (Fp2Inst.ff2 (2 * j) hpr hn) x)) (Fp2Inst.ff2 (2 * j) hpr hn).els
      = (-Jacobsthal.phi (2 * j) ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one)) * (-Jacobsthal.phi (2 * j) ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one)) + -2 * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) := by
  rw [SuperSing.supersingular j hpr hn, QuarticFamily.phi_inert (2 * j) hpr (chi_negone j hn)]
  show -(2 * Int.ofNat (2 * (2 * j) + 1 + 1 + 1)) = _
  lib_int

/-- **Base change at an inert prime of ℚ(√−3)** (p ≡ 11 mod 12). -/
theorem base_change_omega (i : Nat) (hpr : ∀ d, Dv d (2 * (2 * (3 * i + 2)) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * (3 * i + 2)) + 1 + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * (3 * i + 2)) + 1 + 1)) ((ring (2 * (2 * (3 * i + 2)) + 1 + 1)).neg (ring (2 * (2 * (3 * i + 2)) + 1 + 1)).one) (2 * (3 * i + 2) + 1) = (ring (2 * (2 * (3 * i + 2)) + 1 + 1)).neg (ring (2 * (2 * (3 * i + 2)) + 1 + 1)).one) :
    -FinCubic.psi (Fp2Inst.ff2 (2 * (3 * i + 2)) hpr hn) (Fp2Inst.ff2 (2 * (3 * i + 2)) hpr hn).K.one
      = (-CubicJacob.psi (2 * (3 * i + 2)) (ring (2 * (2 * (3 * i + 2)) + 1 + 1)).one) * (-CubicJacob.psi (2 * (3 * i + 2)) (ring (2 * (2 * (3 * i + 2)) + 1 + 1)).one) + -2 * Int.ofNat (2 * (2 * (3 * i + 2)) + 1 + 1 + 1) := by
  rw [CubicSuper.cubic_super i hpr hn,
    SexticFamily.psi_inert (2 * (3 * i + 2)) hpr (8 * i + 7) (by lib_nat)]
  show -(2 * Int.ofNat (2 * (2 * (3 * i + 2)) + 1 + 1 + 1)) = _
  lib_int

end BaseChange

#print axioms BaseChange.chi_negone
#print axioms BaseChange.base_change_i
#print axioms BaseChange.base_change_omega

78.37 Hasse–Davenport at split primes: \(a(\mathbb F_{p^2})=a_p^2-2p\)

For \(y^2=x^3-x\) and every prime \(p\equiv 1\ (4)\), with \(S_1=\sum _{x\in \mathbb F_p}\chi (x^3-x)\) and \(S_2=\sum _{z\in \mathbb F_{p^2}}\chi (z^3-z)\),

\[ -S_2=S_1^2-2p,\qquad \text {i.e.}\qquad a(\mathbb F_{p^2})=a_p^2-2p=\alpha ^2+\beta ^2 . \]
The steps are
\[ \begin {aligned} \textstyle \sum _{x\in \mathbb F_q}x^e&=0\ (1\le e\le q-2),\quad -1\ (e=q-1),\quad q\cdot 1=0,\\ S_2&\equiv \textstyle \sum _z(z^3-z)^{(q-1)/2}=-\tbinom {(p^2-1)/2}{(p^2-1)/4}\quad (\text {mod }p),\\ \tbinom {a+ap}{c+cp}&\equiv \tbinom ac^2\quad (\text {Lucas, }(1+X)^p=1+X^p),\qquad S_1\equiv -(-1)^{k+1}\tbinom {2k+2}{k+1},\\ S_2&\equiv -S_1^2\ (p),\quad S_1=-2a,\ S_2=-2A\ \Rightarrow \ A\equiv 2a^2\equiv a^2-b^2\ (p),\\ A,\ a^2-b^2\ \text {odd},\ |A|,|a^2-b^2|\le p&\Rightarrow A-(a^2-b^2)\in \{0,\pm 2p\},\ \pm 2p\ \text {forces}\ A=-p,\ B=0,\ \text {not primary}. \end {aligned} \]

Listing 102: lang/FinPowerSum.lean

lang/FinPowerSum.lean

/-
  lang/FinPowerSum — T43, cycle 96: power sums over any finite field of the interface.

  For an FF of order q = 2h + 1, sums with values in the field itself:
    compK           the element list is complete over K (from the integer count)
    sum_one_K       Σ_x 1 = q·1
    char_q          q·1 = 0                                  (Σ (x + 1) = Σ x: the shift is a bijection)
    ps_zero_ff      Σ_x x^e = 0 for 1 ≤ e ≤ q − 2            (x ↦ g x with g^e ≠ 1: root bound)
    ps_top_ff       Σ_x x^(q−1) = −1                          (Fermat on the q − 1 units)
  The input of the Hasse invariant over every 𝔽_q (𝔽_{p²} included): Σ_x f(x)^h ≡ −(coefficient of x^(q−1)).

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.FinCyclic
import lang.IntCast
import lang.FinMoment

namespace FinPowerSum

open FTAChain YMChain FiniteRecChain FinField FinList LibRing

variable {R : Type} [DecidableEq R] (F : FF R)

theorem delta_count (z : R) : ∀ l : List R, sumK F.K (fun y => delta F.K y z) l = castN F.K (cnt z l)
  | [] => rfl
  | a :: l => by
    show F.K.add (delta F.K a z) (sumK F.K (fun y => delta F.K y z) l) = castN F.K ((if a = z then 1 else 0) + cnt z l)
    rw [delta_count z l, IntCast.castN_add]
    by_cases e : a = z
    · rw [show delta F.K a z = F.K.one from ite_pos' _ _ e, ite_pos' _ _ e]
      show _ = F.K.add (F.K.add F.K.one F.K.zero) _
      rw [DetChain.add_zero']
    · rw [show delta F.K a z = F.K.zero from ite_neg' _ _ e, ite_neg' _ _ e]
      rfl

theorem compK : Complete F.K F.els := fun z => by
  rw [delta_count F z]
  rw [show cnt z F.els = 1 from Int.ofNat.inj ((cnt_int z F.els).symm.trans (F.comp z))]
  show F.K.add F.K.one F.K.zero = F.K.one
  exact DetChain.add_zero' F.K _

theorem sum_one_K : ∀ l : List R, sumK F.K (fun _ => F.K.one) l = castN F.K l.length
  | [] => rfl
  | _ :: l => by
    show F.K.add F.K.one (sumK F.K (fun _ => F.K.one) l) = F.K.add F.K.one (castN F.K l.length)
    rw [sum_one_K l]

/-- **q · 1 = 0.** -/
theorem char_q : castN F.K (2 * F.h + 1) = F.K.zero := by
  have hre := reindex F.K F.els (compK F) (fun x => F.K.add x F.K.one) (fun x => F.K.add x (F.K.neg F.K.one))
    (fun x => by lib_ring) (fun x => by lib_ring) (fun x => x)
  have e : sumK F.K (fun x => F.K.add x F.K.one) F.els = F.K.add (sumK F.K (fun x => x) F.els) (sumK F.K (fun _ => F.K.one) F.els) :=
    sumK_add F.K _ _ _
  rw [e, sum_one_K, len_els F] at hre
  have := congrArg (fun z => F.K.add z (F.K.neg (sumK F.K (fun x => x) F.els))) hre
  rw [show F.K.add (F.K.add (sumK F.K (fun x => x) F.els) (castN F.K (2 * F.h + 1))) (F.K.neg (sumK F.K (fun x => x) F.els))
      = castN F.K (2 * F.h + 1) by lib_ring, F.K.add_neg] at this
  exact this

theorem ps_zero_ff (k : Nat) (hk : k + 1 + 1 ≤ 2 * F.h) :
    sumK F.K (fun x => Frobenius.pw F.K x (k + 1)) F.els = F.K.zero := by
  match FinCyclic.exists_nonroot_ff F k hk with
  | ⟨g, hg0, hg⟩ =>
    have hre := reindex F.K F.els (compK F) (F.K.mul g) (F.K.mul (F.iv g))
      (fun x => by rw [← F.K.mul_assoc, F.iv_mul g hg0, F.K.one_mul])
      (fun y => by rw [← F.K.mul_assoc, F.K.mul_comm g, F.iv_mul g hg0, F.K.one_mul])
      (fun x => Frobenius.pw F.K x (k + 1))
    have e : sumK F.K (fun x => Frobenius.pw F.K (F.K.mul g x) (k + 1)) F.els
        = F.K.mul (Frobenius.pw F.K g (k + 1)) (sumK F.K (fun x => Frobenius.pw F.K x (k + 1)) F.els) := by
      rw [mul_sumK]
      exact sumK_ext _ _ _ (fun x => Frobenius.pw_mul _ g x (k + 1)) _
    rw [e] at hre
    have e2 : F.K.mul (F.K.add (Frobenius.pw F.K g (k + 1)) (F.K.neg F.K.one)) (sumK F.K (fun x => Frobenius.pw F.K x (k + 1)) F.els)
        = F.K.zero := by
      rw [DetChain.right_distrib, hre, DetChain.neg_mul_left, F.K.one_mul, F.K.add_neg]
    cases F.nozd _ _ e2 with
    | inl h => exact absurd (FinMoment.eq_of_sub_zero F _ _ h) hg
    | inr h => exact h

/-- **Σ_x x^(q−1) = −1.** -/
theorem ps_top_ff : sumK F.K (fun x => Frobenius.pw F.K x (2 * F.h)) F.els = F.K.neg F.K.one := by
  have hpos := FinCyclic.h_pos F
  have pt : ∀ x, Frobenius.pw F.K x (2 * F.h) = F.K.add F.K.one (F.K.neg (delta F.K x F.K.zero)) := fun x => by
    by_cases hx : x = F.K.zero
    · rw [show delta F.K x F.K.zero = F.K.one from ite_pos' _ _ hx, F.K.add_neg, hx]
      cases hh : 2 * F.h with
      | zero =>
        have h2 : 2 * 1 ≤ 2 * F.h := Nat.mul_le_mul_left 2 hpos
        rw [hh] at h2; exact absurd h2 (Nat.not_succ_le_zero 1)
      | succ r => exact HasseCM.pw_zero_succ F.K r
    · rw [show delta F.K x F.K.zero = F.K.zero from ite_neg' _ _ hx, FinCyclic.fermat_ff F x hx]
      lib_ring
  rw [sumK_ext F.K _ _ pt, sumK_add, sum_one_K, len_els F, char_q, F.K.zero_add]
  have := compK F F.K.zero
  rw [show sumK F.K (fun x => F.K.neg (delta F.K x F.K.zero)) F.els = F.K.neg (sumK F.K (fun x => delta F.K x F.K.zero) F.els)
    from CMInert.sum_neg_fun F.K _ _, this]

end FinPowerSum

#print axioms FinPowerSum.delta_count
#print axioms FinPowerSum.compK
#print axioms FinPowerSum.sum_one_K
#print axioms FinPowerSum.char_q
#print axioms FinPowerSum.ps_zero_ff
#print axioms FinPowerSum.ps_top_ff

Listing 103: lang/FinHasse.lean

lang/FinHasse.lean

/-
  lang/FinHasse — T43, cycle 97: the Hasse invariant of y² = x³ − x over every finite field with q ≡ 1 (mod 4).

  For an FF with h = (q − 1)/2 = 2k + 2:
    psF, ps_low, ps_top, ps_high   Σ_x x^e = 0 (1 ≤ e ≤ q − 2), −1 (e = q − 1), 0 (q ≤ e ≤ 2q − 3)    (FinPowerSum)
    TF, T_cons, T_zero, T_skip, skip_many   one binomial coefficient at a time (port of HasseCM)
    fin_hasse      Σ_x (x³ − x)^h = −C(h, h/2)·(−1)^(h/2)          in the field
  So the Frobenius trace over 𝔽_q is ≡ ±C((q−1)/2, (q−1)/4) (mod p) for every q ≡ 1 (mod 4) — for 𝔽_{p²} this is the step
  towards a(𝔽_{p²}) ≢ 0 (mod p) at split primes (ordinarity), with Lucas' theorem next.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.FinPowerSum
import lang.HasseCM

namespace FinHasse

open FTAChain YMChain Frobenius FinField HasseCM

variable {R : Type} [DecidableEq R] (F : FF R)

def psF (e : Nat) : R := sumK F.K (fun x => pw F.K x e) F.els

theorem two_h (k : Nat) (hk : F.h = 2 * k + 2) : 2 * F.h = 2 * (2 * k + 1) + 1 + 1 := by rw [hk]; lib_nat

theorem ps_low (k : Nat) (hk : F.h = 2 * k + 2) (e : Nat) (h1 : 1 ≤ e) (h2 : e ≤ 2 * (2 * k + 1) + 1) : psF F e = F.K.zero := by
  cases e with
  | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
  | succ e' => exact FinPowerSum.ps_zero_ff F e' (by rw [two_h F k hk]; exact Nat.succ_le_succ h2)

theorem ps_top (k : Nat) (hk : F.h = 2 * k + 2) : psF F (2 * (2 * k + 1) + 1 + 1) = F.K.neg F.K.one := by
  rw [← two_h F k hk]; exact FinPowerSum.ps_top_ff F

theorem ps_high (k : Nat) (hk : F.h = 2 * k + 2) (r : Nat) (h1 : 1 ≤ r) (h2 : r ≤ 2 * (2 * k + 1) + 1) :
    psF F (2 * (2 * k + 1) + 1 + 1 + r) = F.K.zero := by
  have e : psF F (2 * (2 * k + 1) + 1 + 1 + r) = psF F r := sumK_ext F.K _ _ (fun x => by
    show pw F.K x (2 * (2 * k + 1) + 1 + 1 + r) = pw F.K x r
    by_cases hx : x = F.K.zero
    · rw [hx]
      cases r with
      | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
      | succ r' => rw [pw_zero_succ, ← Nat.add_assoc, pw_zero_succ]
    · rw [pw_add' F.K x _ r, ← two_h F k hk, FinCyclic.fermat_ff F x hx, F.K.one_mul]) _
  rw [e]
  exact ps_low F k hk r h1 h2

theorem sumK_zero' {A : Type} : ∀ l : List A, sumK F.K (fun _ => F.K.zero) l = F.K.zero
  | [] => rfl
  | _ :: l => by
    show F.K.add F.K.zero (sumK F.K (fun _ => F.K.zero) l) = F.K.zero
    rw [sumK_zero' l, F.K.zero_add]

def evsF (x : R) (cs : List Nat) : R := ev F.K (F.K.mul x x) (F.K.neg F.K.one) cs

def TF (e : Nat) (cs : List Nat) : R :=
  sumK F.K (fun x => F.K.mul (pw F.K x e) (evsF F x cs)) F.els

theorem T_cons (e c : Nat) (cs : List Nat) :
    TF F e (c :: cs) = F.K.add (F.K.mul (nsm F.K c (pw F.K (F.K.neg F.K.one) cs.length)) (psF F e)) (TF F (e + 2) cs) := by
  have pt : ∀ x : R, F.K.mul (pw F.K x e) (evsF F x (c :: cs))
      = F.K.add (F.K.mul (nsm F.K c (pw F.K (F.K.neg F.K.one) cs.length)) (pw F.K x e)) (F.K.mul (pw F.K x (e + 2)) (evsF F x cs)) :=
    fun x => by
      show F.K.mul (pw F.K x e) (F.K.add (nsm F.K c (pw F.K (F.K.neg F.K.one) cs.length)) (F.K.mul (F.K.mul x x) (evsF F x cs))) = _
      rw [F.K.distrib, F.K.mul_comm (pw F.K x e) (nsm F.K c _)]
      show _ = F.K.add _ (F.K.mul (F.K.mul (F.K.mul (pw F.K x e) x) x) (evsF F x cs))
      rw [F.K.mul_assoc (F.K.mul (pw F.K x e) x) x, F.K.mul_assoc (pw F.K x e) x, ← F.K.mul_assoc x x]
  show sumK F.K _ _ = _
  rw [sumK_ext F.K _ _ pt, sumK_add, ← mul_sumK]
  rfl

theorem idx2' (e i : Nat) : e + 2 + 2 * i = e + 2 * (i + 1) := by
  rw [Nat.add_assoc, Nat.add_comm 2 (2 * i)]
  rfl

theorem T_nil (e : Nat) : TF F e [] = F.K.zero := by
  show sumK F.K (fun x => F.K.mul (pw F.K x e) F.K.zero) _ = _
  rw [sumK_ext F.K _ (fun _ => F.K.zero) (fun x => DetChain.mul_zero F.K _), sumK_zero' F]

theorem T_zero : ∀ (cs : List Nat) (e : Nat), (∀ i, i < cs.length → psF F (e + 2 * i) = F.K.zero) → TF F e cs = F.K.zero
  | [], e, _ => T_nil F e
  | c :: cs, e, h => by
    have h0 : psF F e = F.K.zero := h 0 (Nat.zero_lt_succ _)
    rw [T_cons, h0, DetChain.mul_zero, F.K.zero_add]
    exact T_zero cs (e + 2) (fun i hi => by rw [idx2']; exact h (i + 1) (Nat.succ_lt_succ hi))

theorem T_skip (e c : Nat) (cs : List Nat) (h : psF F e = F.K.zero) : TF F e (c :: cs) = TF F (e + 2) cs := by
  rw [T_cons, h, DetChain.mul_zero, F.K.zero_add]

theorem skip_many (n : Nat) (m : Nat) : ∀ (i j e : Nat), (∀ i', i' < i → psF F (e + 2 * i') = F.K.zero) →
    TF F e (Frobenius.listC n j (i + m)) = TF F (e + 2 * i) (Frobenius.listC n (j + i) m)
  | 0, j, e, _ => by rw [Nat.zero_add]; rfl
  | i + 1, j, e, h => by
    rw [Nat.succ_add]
    show TF F e (Frobenius.C n j :: Frobenius.listC n (j + 1) (i + m)) = _
    rw [T_skip F e _ _ (h 0 (Nat.zero_lt_succ _) : psF F e = _),
      skip_many n m i (j + 1) (e + 2) (fun i' hi' => by rw [idx2']; exact h (i' + 1) (Nat.succ_lt_succ hi')),
      idx2', Nat.add_assoc j 1 i, Nat.add_comm 1 i]

theorem cub_factor (x : R) :
    CMInert.cub F.K x = F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)) := by
  show F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x) = _
  rw [F.K.distrib, mul_neg_r F.K, F.K.mul_comm x F.K.one, F.K.one_mul, F.K.mul_comm (F.K.mul x x) x]

theorem cub_factorF (x : R) :
    CMInert.cub F.K x = F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)) := by
  show F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x) = _
  lib_ring

theorem fin_hasse (k : Nat) (hk : F.h = 2 * k + 2) :
    sumK F.K (fun x => pw F.K (CMInert.cub F.K x) (2 * k + 2)) F.els
      = F.K.neg (nsm F.K (Frobenius.C (2 * k + 2) (k + 1)) (pw F.K (F.K.neg F.K.one) (k + 1))) := by
  have eA : sumK F.K (fun x => pw F.K (CMInert.cub F.K x) (2 * k + 2)) F.els
      = TF F (2 * k + 2) (Frobenius.row (2 * k + 2)) :=
    sumK_ext F.K _ _ (fun x => by
      rw [cub_factorF F x, pw_mul, ← ev_row]
      rfl) _
  rw [eA, Frobenius.row_eq, ar1 k,
    skip_many F (2 * k + 2) (k + 2) (k + 1) 0 (2 * k + 2) (fun i' hi' =>
      ps_low F k hk _ (Nat.le_trans (Nat.succ_le_succ (Nat.zero_le _)) (Nat.le_add_right _ _))
        (ar3 k i' (Nat.le_of_lt_succ hi'))),
    Nat.zero_add]
  show TF F (2 * k + 2 + 2 * (k + 1))
    (Frobenius.C (2 * k + 2) (k + 1) :: Frobenius.listC (2 * k + 2) (k + 1 + 1) (k + 1)) = _
  rw [T_cons, Frobenius.len_listC, ar2 k, ps_top F k hk,
    T_zero F _ _ (fun i hi => by
      rw [Frobenius.len_listC] at hi
      rw [Nat.add_assoc (2 * (2 * k + 1) + 1 + 1) 2 (2 * i)]
      exact ps_high F k hk _ (Nat.le_trans (Nat.succ_le_succ (Nat.zero_le 1)) (Nat.le_add_right 2 _))
        (ar4 k i (Nat.le_of_lt_succ hi))),
    DetChain.add_zero', mul_neg_r F.K, F.K.mul_comm _ F.K.one, F.K.one_mul]

end FinHasse

#print axioms FinHasse.two_h
#print axioms FinHasse.ps_low
#print axioms FinHasse.ps_top
#print axioms FinHasse.ps_high
#print axioms FinHasse.sumK_zero'
#print axioms FinHasse.T_cons
#print axioms FinHasse.idx2'
#print axioms FinHasse.T_nil
#print axioms FinHasse.T_zero
#print axioms FinHasse.T_skip
#print axioms FinHasse.skip_many
#print axioms FinHasse.cub_factor
#print axioms FinHasse.cub_factorF
#print axioms FinHasse.fin_hasse

Listing 104: lang/Lucas.lean

lang/Lucas.lean

/-
  lang/Lucas — T43, cycle 98: Lucas' theorem for two digits, in every commutative ring of prime characteristic p.

  Binomial coefficients as Satake coefficients: rp n = [−1, …, −1] (n entries), cf(rp n) = coefficients of (1 + X)^n.
    B_C          cf(rp n)_k = C(n, k)·1
    sRn, sRn_low, sRn_shift, conv_sRn, conv_delta_right     shifts by p and Cauchy products
    B_p          in characteristic p: (1 + X)^p = 1 + X^p            (p | C(p, k), 0 < k < p: Frobenius.dv_choose)
    B_step       (1 + X)^(n + p) = (1 + X)^n + X^p (1 + X)^n
    lucas        b, d < p:  C(b + a p, c p + d)·1 = C(a, c) C(b, d)·1
  Used for C((p²−1)/2, (p²−1)/4) ≡ C((p−1)/2, (p−1)/4)² (mod p): the Hasse invariant over 𝔽_{p²} is the square of the one over
  𝔽_p (FinHasse), the ordinarity input for a(𝔽_{p²}) = a_p² − 2p at split primes.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.SymNLocal
import lib.RingTac
import lib.Semi

namespace Lucas

open FTAChain GLnLocal SymNLocal Frobenius

variable {R : Type} (K : DetChain.CRing R)

def rp : Nat → List R
  | 0 => []
  | n + 1 => K.neg K.one :: rp n

theorem B_C : ∀ n k, cf K (rp K n) k = nsm K (C n k) K.one
  | 0, 0 => (K.zero_add _).symm
  | 0, _ + 1 => rfl
  | n + 1, 0 => by
    show cf K (rp K n) 0 = nsm K (C (n + 1) 0) K.one
    rw [B_C n 0, C_zero_right, C_zero_right]
  | n + 1, k + 1 => by
    show K.add (cf K (rp K n) (k + 1)) (K.neg (K.mul (K.neg K.one) (cf K (rp K n) k))) = nsm K (C n k + C n (k + 1)) K.one
    rw [B_C n (k + 1), B_C n k, nsm_add]
    lib_ring

def sRn : Nat → (Nat → R) → Nat → R
  | 0, f => f
  | n + 1, f => sR K (sRn n f)

theorem sRn_low (f : Nat → R) : ∀ n k, k < n → sRn K n f k = K.zero
  | 0, k, h => absurd h (Nat.not_lt_zero k)
  | _ + 1, 0, _ => rfl
  | n + 1, k + 1, h => sRn_low f n k (Nat.lt_of_succ_lt_succ h)

theorem sRn_shift (f : Nat → R) : ∀ n k, sRn K n f (k + n) = f k
  | 0, _ => rfl
  | n + 1, k => sRn_shift f n k

theorem sRn_ext (f g : Nat → R) (hfg : ∀ i, f i = g i) : ∀ n k, sRn K n f k = sRn K n g k
  | 0, k => hfg k
  | n + 1, k => sR_ext K _ _ (fun i => sRn_ext f g hfg n i) k

theorem conv_sRn (c h : Nat → R) : ∀ n k, conv K c (sRn K n h) k = sRn K n (conv K c h) k
  | 0, _ => rfl
  | n + 1, k => by
    show conv K c (sR K (sRn K n h)) k = sR K (sRn K n (conv K c h)) k
    rw [conv_sR]
    exact sR_ext K _ _ (fun i => conv_sRn c h n i) k

theorem conv_delta_right (c : Nat → R) : ∀ k, conv K c (delta K) k = c k := by
  intro k
  induction k generalizing c with
  | zero => show K.mul (c 0) K.one = c 0; rw [K.mul_comm, K.one_mul]
  | succ k ih =>
    show K.add (K.mul (c 0) K.zero) (conv K (fun i => c (i + 1)) (delta K) k) = c (k + 1)
    rw [DetChain.mul_zero, K.zero_add, ih]

theorem rp_add : ∀ m n, rp K (m + n) = rp K m ++ rp K n
  | 0, n => by rw [Nat.zero_add]; rfl
  | m + 1, n => by
    rw [Nat.succ_add]
    show K.neg K.one :: rp K (m + n) = K.neg K.one :: (rp K m ++ rp K n)
    rw [rp_add m n]


/-- **(1 + X)^p = 1 + X^p in characteristic p.** -/
theorem B_p (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hp : nsm K (j + 1 + 1) K.one = K.zero) (k : Nat) : cf K (rp K (j + 1 + 1)) k = K.add (delta K k) (sRn K (j + 1 + 1) (delta K) k) := by
  rw [B_C]
  cases Nat.lt_or_ge k (j + 1 + 1) with
  | inl hk =>
    rw [sRn_low K _ _ k hk, DetChain.add_zero']
    cases k with
    | zero => rw [C_zero_right]; exact K.zero_add _
    | succ k' =>
      match dv_choose j k' hpr (Nat.le.dest hk) with
      | ⟨q, hq⟩ => rw [hq, nsm_char K _ q hp]; rfl
  | inr hk =>
    match Nat.le.dest hk with
    | ⟨m, hm⟩ =>
      rw [← hm, Nat.add_comm (j + 1 + 1) m, sRn_shift]
      cases m with
      | zero =>
        rw [Nat.zero_add, C_self]
        show K.add K.zero K.one = K.add K.zero K.one
        rfl
      | succ m' =>
        rw [show m' + 1 + (j + 1 + 1) = m' + (j + 1 + 1) + 1 by lib_nat, C_above]
        show K.zero = K.add K.zero K.zero
        exact (K.zero_add _).symm

/-- **(1 + X)^(n + p) = (1 + X)^n + X^p (1 + X)^n.** -/
theorem B_step (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hp : nsm K (j + 1 + 1) K.one = K.zero) (n k : Nat) :
    cf K (rp K (n + (j + 1 + 1))) k = K.add (cf K (rp K n) k) (sRn K (j + 1 + 1) (cf K (rp K n)) k) := by
  rw [rp_add, cf_append, conv_ext K _ _ _ (B_p K j hpr hp) k, conv_add, conv_delta_right, conv_sRn]
  rw [sRn_ext K _ _ (conv_delta_right K _) (j + 1 + 1) k]

/-- **Lucas' theorem for two digits**: C(b + a p, c p + d) ≡ C(a, c) C(b, d) (mod p) for b, d < p. -/
theorem lucas (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (hp : nsm K (j + 1 + 1) K.one = K.zero) (b d : Nat) (hb : b < j + 1 + 1) (hd : d < j + 1 + 1) : ∀ a c,
    nsm K (C (b + a * (j + 1 + 1)) (c * (j + 1 + 1) + d)) K.one = nsm K (C a c * C b d) K.one
  | 0, c => by
    rw [Nat.zero_mul, Nat.add_zero]
    cases c with
    | zero => rw [Nat.zero_mul, Nat.zero_add]; show _ = nsm K (1 * C b d) K.one; rw [Nat.one_mul]
    | succ c' =>
      show nsm K (C b ((c' + 1) * (j + 1 + 1) + d)) K.one = nsm K (0 * C b d) K.one
      have hle : b + 1 ≤ (c' + 1) * (j + 1 + 1) + d :=
        Nat.le_trans hb (Nat.le_trans (Nat.le_add_left _ (c' * (j + 1 + 1))) (by rw [Nat.succ_mul]; exact Nat.le_add_right _ _))
      match Nat.le.dest hle with
      | ⟨m, hm⟩ =>
        rw [← hm, show b + 1 + m = m + b + 1 by lib_nat, C_above, Nat.zero_mul]
  | a + 1, c => by
    have e1 : b + (a + 1) * (j + 1 + 1) = (b + a * (j + 1 + 1)) + (j + 1 + 1) := by lib_nat
    rw [← B_C, e1, B_step K j hpr hp]
    cases c with
    | zero =>
      rw [sRn_low K _ _ _ (by rw [Nat.zero_mul, Nat.zero_add]; exact hd), DetChain.add_zero', B_C,
        lucas j hpr hp b d hb hd a 0, C_zero_right, C_zero_right]
    | succ c' =>
      rw [show (c' + 1) * (j + 1 + 1) + d = (c' * (j + 1 + 1) + d) + (j + 1 + 1) by lib_nat, sRn_shift,
        show c' * (j + 1 + 1) + d + (j + 1 + 1) = (c' + 1) * (j + 1 + 1) + d by lib_nat, B_C, B_C,
        lucas j hpr hp b d hb hd a (c' + 1), lucas j hpr hp b d hb hd a c', ← nsm_add]
      show _ = nsm K ((C a c' + C a (c' + 1)) * C b d) K.one
      rw [show (C a c' + C a (c' + 1)) * C b d = C a (c' + 1) * C b d + C a c' * C b d by lib_nat]

end Lucas

#print axioms Lucas.B_C
#print axioms Lucas.sRn_low
#print axioms Lucas.sRn_shift
#print axioms Lucas.sRn_ext
#print axioms Lucas.conv_sRn
#print axioms Lucas.conv_delta_right
#print axioms Lucas.rp_add
#print axioms Lucas.B_p
#print axioms Lucas.B_step
#print axioms Lucas.lucas

Listing 105: lang/FinCast.lean

lang/FinCast.lean

/-
  lang/FinCast — T43, cycle 99: integer character sums reduced into the field, over every finite field.

    cast_chi_ff     castZ(χ(z)) = z^h                                     (Euler's criterion in FF)
    cast_sum_ff     castZ(Σ_x χ(g(x))) = Σ_x g(x)^h
    castN_quad      in 𝔽_p[√n], m·1 = (m·1, 0)                             (the prime field sits in the first coordinate)
    castZ_quad      castZ z = (castZ z, 0)
  So the Frobenius trace over 𝔽_q reduces mod p to the Hasse sum (FinHasse) — over 𝔽_{p²} as well, whose prime field is 𝔽_p.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.FinCyclic
import lang.IntCast
import lang.Fq2

namespace FinCast

open FTAChain YMChain FiniteRecChain FinField LibRing LibInt

variable {R : Type} [DecidableEq R] (F : FF R)

theorem cast_chi_ff (z : R) : castZ F.K (chi F z) = Frobenius.pw F.K z F.h := by
  have hpos := FinCyclic.h_pos F
  by_cases hz : z = F.K.zero
  · rw [hz, chi_zero]
    cases hh : F.h with
    | zero => rw [hh] at hpos; exact absurd hpos (Nat.not_succ_le_zero 0)
    | succ r => exact (HasseCM.pw_zero_succ F.K r).symm
  · cases F.euler_pm z hz with
    | inl h =>
      rw [chi_one_of F z hz h, h]
      show F.K.add F.K.one F.K.zero = F.K.one
      exact DetChain.add_zero' F.K _
    | inr h =>
      have h' : Frobenius.pw F.K z F.h ≠ F.K.one := fun e => F.ne_pm (h.symm.trans e)
      rw [chi_neg_of F z hz h', h]
      exact IntCast.castZ_neg_nat F.K 1 |>.trans (by show F.K.neg (F.K.add F.K.one F.K.zero) = _; rw [DetChain.add_zero'])

theorem cast_sum_ff (g : R → R) :
    castZ F.K (sumK intR (fun x => chi F (g x)) F.els) = sumK F.K (fun x => Frobenius.pw F.K (g x) F.h) F.els := by
  rw [IntCast.castZ_sum]
  exact sumK_ext F.K _ _ (fun x => cast_chi_ff F (g x)) _

section Quad
variable {S : Type} (K : DetChain.CRing S) (n : S)

theorem castN_quad : ∀ m, castN (Quad.quad K n) m = (castN K m, K.zero)
  | 0 => rfl
  | m + 1 => by
    show (Quad.quad K n).add (Quad.quad K n).one (castN (Quad.quad K n) m) = (K.add K.one (castN K m), K.zero)
    rw [castN_quad m]
    apply Prod.ext
    · rfl
    · show K.add K.zero K.zero = K.zero; exact K.zero_add _

theorem castZ_quad (z : Int) : castZ (Quad.quad K n) z = (castZ K z, K.zero) := by
  rw [IntCast.castZ_repr, IntCast.castZ_repr K z, castN_quad, castN_quad]
  apply Prod.ext
  · rfl
  · show K.add K.zero (K.neg K.zero) = K.zero; lib_ring

end Quad

end FinCast

#print axioms FinCast.cast_chi_ff
#print axioms FinCast.cast_sum_ff
#print axioms FinCast.castN_quad
#print axioms FinCast.castZ_quad

Listing 106: lang/SplitHasse.lean

lang/SplitHasse.lean

/-
  lang/SplitHasse — T43, cycle 100: at a split prime p = 4k + 5, the trace over 𝔽_{p²} is −(trace over 𝔽_p)² mod p.

  S₁ = Σ_{x ∈ 𝔽_p} χ(x³ − x),  S₂ = Σ_{z ∈ 𝔽_{p²}} χ(z³ − z)  (𝔽_{p²} = 𝔽_p[√n], Fq2Inst.ffq).
    negone_even     (−1)^(2s) = 1
    hF              h(𝔽_{p²}) = (p² − 1)/2 = 2m + 2,  m = 4k² + 10k + 5
    cast_S2         S₂ ≡ −C(2m + 2, m + 1)                         (FinCast + FinHasse over 𝔽_{p²})
    lucas_S2        C(2m + 2, m + 1) ≡ C(2k + 2, k + 1)² (mod p)    (Lucas: (p²−1)/2 = a + a p, (p²−1)/4 = c + c p)
    cast_S1         S₁ ≡ −(−1)^(k+1) C(2k + 2, k + 1)               (HasseCM, GaussCong)
    split_hasse     S₂ ≡ −S₁² (mod p)
  With S₁ = −a_p, S₂ = −a(𝔽_{p²}): a(𝔽_{p²}) ≡ a_p² (mod p), the shadow mod p of a(𝔽_{p²}) = a_p² − 2p (Hasse–Davenport).

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.FinHasse
import lang.FinCast
import lang.Lucas
import lang.Fq2Inst
import lang.GaussCong
import lang.FinSign

namespace SplitHasse

open FTAChain ZMod YMChain FiniteRecChain LibRing Frobenius

theorem negone_even {R : Type} (K : DetChain.CRing R) : ∀ s, Frobenius.pw K (K.neg K.one) (2 * s) = K.one
  | 0 => rfl
  | s + 1 => by
    rw [show 2 * (s + 1) = 2 * s + 1 + 1 by lib_nat]
    show K.mul (K.mul (Frobenius.pw K (K.neg K.one) (2 * s)) (K.neg K.one)) (K.neg K.one) = K.one
    rw [negone_even K s]
    lib_ring

theorem hF (k : Nat) : (2 * (2 * k + 1) + 1 + 1 + 1 + 1) * (2 * k + 1 + 1) = 2 * (4 * k * k + 10 * k + 5) + 2 := by lib_nat

theorem idx_a (k : Nat) : 2 * (4 * k * k + 10 * k + 5) + 2 = (2 * k + 2) + (2 * k + 2) * (2 * (2 * k + 1) + 1 + 1 + 1) := by lib_nat
theorem idx_c (k : Nat) : 4 * k * k + 10 * k + 5 + 1 = (k + 1) * (2 * (2 * k + 1) + 1 + 1 + 1) + (k + 1) := by lib_nat
theorem idx_e (k : Nat) : 4 * k * k + 10 * k + 5 + 1 = 2 * (2 * k * k + 5 * k + 3) := by lib_nat

theorem lt_p (k : Nat) : 2 * k + 2 < 2 * (2 * k + 1) + 1 + 1 + 1 := Nat.le.intro (k := 2 * k + 2) (by lib_nat)
theorem lt_p' (k : Nat) : k + 1 < 2 * (2 * k + 1) + 1 + 1 + 1 := Nat.le.intro (k := 3 * k + 3) (by lib_nat)

variable (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1)
  (n : Zp (2 * (2 * k + 1) + 1 + 1))
  (hn : Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) n (2 * k + 1 + 1) = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one)

/-- **S₂ ≡ −C(2k + 2, k + 1)² in 𝔽_{p²}.** -/
theorem cast_S2 :
    castZ (Fq2Inst.ffq (2 * k + 1) hpr n hn).K
      (sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq (2 * k + 1) hpr n hn) (FinSign.f (Fq2Inst.ffq (2 * k + 1) hpr n hn) x)) (Fq2Inst.ffq (2 * k + 1) hpr n hn).els)
      = (Fq2Inst.ffq (2 * k + 1) hpr n hn).K.neg
          (nsm (Fq2Inst.ffq (2 * k + 1) hpr n hn).K (C (2 * k + 2) (k + 1) * C (2 * k + 2) (k + 1)) (Fq2Inst.ffq (2 * k + 1) hpr n hn).K.one) := by
  have hk : (Fq2Inst.ffq (2 * k + 1) hpr n hn).h = 2 * (4 * k * k + 10 * k + 5) + 2 := hF k
  rw [FinCast.cast_sum_ff, hk]
  refine (FinHasse.fin_hasse (Fq2Inst.ffq (2 * k + 1) hpr n hn) (4 * k * k + 10 * k + 5) hk).trans ?_
  rw [idx_e k, negone_even, idx_a k, ← idx_e k, idx_c k]
  rw [Lucas.lucas (Fq2Inst.ffq (2 * k + 1) hpr n hn).K (2 * (2 * k + 1) + 1) hpr
    (QuadFrob.char_quad (2 * (2 * k + 1) + 1) n) (2 * k + 2) (k + 1) (lt_p k) (lt_p' k)]

/-- the same in 𝔽_p (first coordinate). -/
theorem cast_S2_p :
    castZ (ring (2 * (2 * k + 1) + 1 + 1))
      (sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq (2 * k + 1) hpr n hn) (FinSign.f (Fq2Inst.ffq (2 * k + 1) hpr n hn) x)) (Fq2Inst.ffq (2 * k + 1) hpr n hn).els)
      = (ring (2 * (2 * k + 1) + 1 + 1)).neg (nsm (ring (2 * (2 * k + 1) + 1 + 1)) (C (2 * k + 2) (k + 1) * C (2 * k + 2) (k + 1)) (ring (2 * (2 * k + 1) + 1 + 1)).one) := by
  have h := congrArg Prod.fst (cast_S2 k hpr n hn)
  rw [show castZ (Fq2Inst.ffq (2 * k + 1) hpr n hn).K = castZ (Quad.quad (ring (2 * (2 * k + 1) + 1 + 1)) n) from rfl,
    FinCast.castZ_quad] at h
  refine h.trans ?_
  show (ring (2 * (2 * k + 1) + 1 + 1)).neg (nsm (Quad.quad (ring (2 * (2 * k + 1) + 1 + 1)) n) _ ((ring (2 * (2 * k + 1) + 1 + 1)).one, (ring (2 * (2 * k + 1) + 1 + 1)).zero)).1 = _
  rw [QuadFrob.nsm_quad]

theorem cast_S1 (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1) :
    castZ (ring (2 * (2 * k + 1) + 1 + 1)) (sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1)))
      = (ring (2 * (2 * k + 1) + 1 + 1)).neg (nsm (ring (2 * (2 * k + 1) + 1 + 1)) (C (2 * k + 2) (k + 1)) (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1))) := by
  rw [IntCast.castZ_sum]
  rw [sumK_ext (ring (2 * (2 * k + 1) + 1 + 1)) _ _ (fun x => GaussCong.cast_chi k hpr (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x))]
  exact HasseCM.hasse_cm k hpr

/-- **At a split prime p = 4k + 5: S₂ ≡ −S₁² (mod p).** -/
theorem split_hasse :
    castZ (ring (2 * (2 * k + 1) + 1 + 1))
      (sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq (2 * k + 1) hpr n hn) (FinSign.f (Fq2Inst.ffq (2 * k + 1) hpr n hn) x)) (Fq2Inst.ffq (2 * k + 1) hpr n hn).els)
      = (ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).mul
          (castZ (ring (2 * (2 * k + 1) + 1 + 1)) (sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1))))
          (castZ (ring (2 * (2 * k + 1) + 1 + 1)) (sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1))))) := by
  rw [cast_S2_p, cast_S1 k hpr]
  have e1 : nsm (ring (2 * (2 * k + 1) + 1 + 1)) (C (2 * k + 2) (k + 1) * C (2 * k + 2) (k + 1)) (ring (2 * (2 * k + 1) + 1 + 1)).one
      = (ring (2 * (2 * k + 1) + 1 + 1)).mul (nsm (ring (2 * (2 * k + 1) + 1 + 1)) (C (2 * k + 2) (k + 1)) (ring (2 * (2 * k + 1) + 1 + 1)).one) (nsm (ring (2 * (2 * k + 1) + 1 + 1)) (C (2 * k + 2) (k + 1)) (ring (2 * (2 * k + 1) + 1 + 1)).one) := by
    rw [nsm_mul_nat]; exact nsm_eq_mul _ _ _
  have e2 : nsm (ring (2 * (2 * k + 1) + 1 + 1)) (C (2 * k + 2) (k + 1)) (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1))
      = (ring (2 * (2 * k + 1) + 1 + 1)).mul (nsm (ring (2 * (2 * k + 1) + 1 + 1)) (C (2 * k + 2) (k + 1)) (ring (2 * (2 * k + 1) + 1 + 1)).one) (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1)) := nsm_eq_mul _ _ _
  have hs : (ring (2 * (2 * k + 1) + 1 + 1)).mul (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1)) (Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1)) = (ring (2 * (2 * k + 1) + 1 + 1)).one := by
    rw [← Frobenius.pw_mul, show (ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) = (ring (2 * (2 * k + 1) + 1 + 1)).one by lib_ring, TwoPower.pwone]
  rw [e1, e2]
  generalize Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) (k + 1) = P at hs
  generalize nsm (ring (2 * (2 * k + 1) + 1 + 1)) (C (2 * k + 2) (k + 1)) (ring (2 * (2 * k + 1) + 1 + 1)).one = N
  rw [show (ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).mul N P)) ((ring (2 * (2 * k + 1) + 1 + 1)).neg ((ring (2 * (2 * k + 1) + 1 + 1)).mul N P)) = (ring (2 * (2 * k + 1) + 1 + 1)).mul ((ring (2 * (2 * k + 1) + 1 + 1)).mul N N) ((ring (2 * (2 * k + 1) + 1 + 1)).mul P P) by lib_ring, hs]
  lib_ring

end SplitHasse

#print axioms SplitHasse.negone_even
#print axioms SplitHasse.hF
#print axioms SplitHasse.idx_a
#print axioms SplitHasse.idx_c
#print axioms SplitHasse.idx_e
#print axioms SplitHasse.lt_p
#print axioms SplitHasse.lt_p'
#print axioms SplitHasse.cast_S2
#print axioms SplitHasse.cast_S2_p
#print axioms SplitHasse.cast_S1
#print axioms SplitHasse.split_hasse

Listing 107: lang/HasseDavenport.lean

lang/HasseDavenport.lean

/-
  lang/HasseDavenport — T43, cycle 101: a(𝔽_{p²}) = a_p² − 2p for y² = x³ − x at every split prime p ≡ 1 (mod 4).

  S₁ = Σ_{𝔽_p} χ(x³ − x) = −2a (a + bi primary, a² + b² = p: SignCM3), S₂ = Σ_{𝔽_{p²}} χ(z³ − z) = −2A (A + Bi primary,
  A² + B² = p²: Fq2Trace), and S₂ ≡ −S₁² (mod p) (SplitHasse). Then A ≡ 2a² ≡ a² − b² (mod p); A and a² − b² are odd, so
  they differ by 2p·s, and |A|, |a² − b²| ≤ p leaves s = 0 unless A = −p with B = 0, which is not primary. Hence A = a² − b²:
    castZ_neg, castZ_mul       the cast ℤ → K is a ring map
    castZ_zero_dvd             castZ_p z = 0 ⇒ p | z
    finish                     the integer argument above
    hasse_davenport_split      −S₂ = S₁² − 2p,  i.e.  a(𝔽_{p²}) = a_p² − 2p = α² + β²  (α + β = a_p, αβ = p)
  With BaseChange (inert primes) the Frobenius-square identity holds at every odd prime: the zeta function of the CM curve
  over 𝔽_{p²} is the one predicted by the Hecke character (π² and π̄²) — Hasse–Davenport in this case, without Jacobi sums.

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.SplitHasse
import lang.Fq2Trace
import lang.SignCM3

namespace HasseDavenport

open FTAChain ZMod YMChain FiniteRecChain LibRing LibInt

section Cast
variable {R : Type} (K : DetChain.CRing R)

theorem castZ_neg (z : Int) : castZ K (-z) = K.neg (castZ K z) := by
  apply DetChain.neg_unique
  rw [← IntCast.castZ_add, show -z + z = 0 by lib_int]
  rfl

theorem castZ_mul_nat (x : Int) : ∀ n : Nat, castZ K (x * Int.ofNat n) = K.mul (castZ K x) (castN K n)
  | 0 => by rw [show x * Int.ofNat 0 = 0 from Int.mul_zero x]; show K.zero = K.mul _ K.zero; rw [DetChain.mul_zero]
  | n + 1 => by
    rw [show x * Int.ofNat (n + 1) = x * Int.ofNat n + x by show x * (Int.ofNat n + 1) = x * Int.ofNat n + x; lib_int, IntCast.castZ_add,
      castZ_mul_nat x n]
    show _ = K.mul (castZ K x) (K.add K.one (castN K n))
    lib_ring

theorem castZ_mul (x y : Int) : castZ K (x * y) = K.mul (castZ K x) (castZ K y) := by
  cases y with
  | ofNat n => exact castZ_mul_nat K x n
  | negSucc n =>
    rw [show x * Int.negSucc n = -(x * Int.ofNat (n + 1)) by show x * -(Int.ofNat (n + 1)) = -(x * Int.ofNat (n + 1)); lib_int, castZ_neg,
      castZ_mul_nat]
    show _ = K.mul (castZ K x) (K.neg (castN K (n + 1)))
    lib_ring

end Cast

theorem castZ_zero_dvd (m : Nat) (z : Int) (h : castZ (ring m) z = (ring m).zero) : ∃ q : Int, z = Int.ofNat (m + 1) * q := by
  cases z with
  | ofNat a =>
    have h' : mk m a = (ring m).zero := (IntCast.castN_mk m a).symm.trans h
    match md_zero_dv m a (congrArg Subtype.val h') with
    | ⟨q, hq⟩ => exact ⟨Int.ofNat q, by rw [hq]; show Int.ofNat q * Int.ofNat (m + 1) = _; lib_int⟩
  | negSucc b =>
    have h1 : castN (ring m) (b + 1) = (ring m).zero := by
      have := congrArg (ring m).neg h
      rw [show (ring m).neg (castZ (ring m) (Int.negSucc b)) = castN (ring m) (b + 1) from DetChain.neg_neg' _ _] at this
      rw [this]; exact (DetChain.neg_unique (ring m) _ _ ((ring m).zero_add _)).symm
    have h' : mk m (b + 1) = (ring m).zero := (IntCast.castN_mk m (b + 1)).symm.trans h1
    match md_zero_dv m (b + 1) (congrArg Subtype.val h') with
    | ⟨q, hq⟩ =>
      refine ⟨-Int.ofNat q, ?_⟩
      show -(Int.ofNat (b + 1)) = _
      rw [hq]; show -(Int.ofNat q * Int.ofNat (m + 1)) = _; lib_int

theorem two_mul_zero (n : Nat) (h : 2 * n = 0) : n = 0 := by
  cases n with
  | zero => rfl
  | succ n => exact Nat.noConfusion h

theorem nat_sum_zero (a b : Nat) (h : a + b = 0) : a = 0 ∧ b = 0 := by
  cases b with
  | zero => exact ⟨h, rfl⟩
  | succ b => exact Nat.noConfusion h

theorem sq_zero (x : Int) (h : x * x = 0) : x = 0 := by
  cases int_noZD x x h with
  | inl h => exact h
  | inr h => exact h

/-- **The integer finish.** -/
theorem finish (P a b A B r c : Int) (hP4 : P = 4 * c + 1) (hab : a * a + b * b = P) (hb : ∃ e, b = 2 * e)
    (ha4 : ∃ t, a + b - 1 = 4 * t) (hAB : A * A + B * B = P * P) (hB : ∃ g, B = 2 * g) (hA4 : ∃ t, A + B - 1 = 4 * t)
    (hr : A = 2 * a * a - P * r) : A = a * a - b * b := by
  match hb, ha4, hB, hA4 with
  | ⟨e, he⟩, ⟨t, ht⟩, ⟨g, hg⟩, ⟨t', ht'⟩ =>
    have haodd : a = 2 * (2 * t - e) + 1 := by
      calc a = (a + b - 1) - b + 1 := by lib_int
        _ = 2 * (2 * t - e) + 1 := by rw [ht, he]; lib_int
    have hAodd : A = 2 * (2 * t' - g) + 1 := by
      calc A = (A + B - 1) - B + 1 := by lib_int
        _ = 2 * (2 * t' - g) + 1 := by rw [ht', hg]; lib_int
    have hdiff : A - (a * a - b * b) = P * (1 - r) := by
      calc A - (a * a - b * b) = (2 * a * a - P * r) - (a * a - b * b) := by rw [hr]
        _ = (a * a + b * b) - P * r := by lib_int
        _ = P * (1 - r) := by rw [hab]; lib_int
    match GaussInt.int_even_odd (1 - r) with
    | Or.inr ⟨s, hs⟩ =>
      exfalso
      apply GaussInt.int_odd_ne_even (4 * c * s + 2 * c + s) ((2 * t' - g) - (2 * (2 * t - e) * (2 * t - e) + 2 * (2 * t - e) - 2 * e * e))
      rw [show 2 * ((2 * t' - g) - (2 * (2 * t - e) * (2 * t - e) + 2 * (2 * t - e) - 2 * e * e))
          = A - (a * a - b * b) by rw [hAodd, haodd, he]; lib_int, hdiff, hs, hP4]
      lib_int
    | Or.inl ⟨s, hs⟩ =>
      match FinCubicSign.sq_nn s with
      | ⟨0, h0⟩ =>
        have : s = 0 := sq_zero s h0
        calc A = (A - (a * a - b * b)) + (a * a - b * b) := by lib_int
          _ = a * a - b * b := by rw [hdiff, hs, this]; lib_int
      | ⟨n' + 1, hn⟩ =>
        exfalso
        match FinCubicSign.sq_nn B, FinCubicSign.sq_nn (2 * a * b), FinCubicSign.sq_nn (A + (a * a - b * b)), FinCubicSign.sq_nn P with
        | ⟨nB, hnB⟩, ⟨n2, hn2⟩, ⟨m, hm⟩, ⟨pp, hpp⟩ =>
          have key : Int.ofNat (4 * pp * n' + m + 2 * nB + 2 * n2) = 0 := by
            show 4 * Int.ofNat pp * Int.ofNat n' + Int.ofNat m + 2 * Int.ofNat nB + 2 * Int.ofNat n2 = 0
            have e1 : (A - (a * a - b * b)) * (A - (a * a - b * b)) = 4 * (P * P) * (s * s) := by rw [hdiff, hs]; lib_int
            have e2 : (a * a - b * b) * (a * a - b * b) + (2 * a * b) * (2 * a * b) = P * P := by
              rw [← hab]; lib_int
            rw [← hpp, ← hm, ← hnB, ← hn2]
            have : s * s = Int.ofNat n' + 1 := hn
            calc 4 * (P * P) * Int.ofNat n' + (A + (a * a - b * b)) * (A + (a * a - b * b)) + 2 * (B * B) + 2 * (2 * a * b * (2 * a * b))
                = ((A - (a * a - b * b)) * (A - (a * a - b * b)) + (A + (a * a - b * b)) * (A + (a * a - b * b)))
                  + 2 * (B * B) + 2 * (2 * a * b * (2 * a * b)) - 4 * (P * P) * (s * s) + 4 * (P * P) * Int.ofNat n' := by rw [e1]; lib_int
              _ = 2 * (A * A + B * B) + 2 * ((a * a - b * b) * (a * a - b * b) + (2 * a * b) * (2 * a * b)) - 4 * (P * P) * (s * s) + 4 * (P * P) * Int.ofNat n' := by lib_int
              _ = 0 := by rw [hAB, e2, this]; lib_int
          have hk := Int.ofNat.inj key
          match nat_sum_zero _ _ hk with
          | ⟨h123, h4⟩ =>
            match nat_sum_zero _ _ h123 with
            | ⟨h12, h3⟩ =>
              match nat_sum_zero _ _ h12 with
              | ⟨_, h2⟩ =>
                have hB0 : B = 0 := sq_zero B (hnB.trans (by rw [two_mul_zero nB h3]; rfl))
                have hab0 : 2 * a * b = 0 := sq_zero _ (hn2.trans (by rw [two_mul_zero n2 h4]; rfl))
                have hm0 : A + (a * a - b * b) = 0 := sq_zero _ (hm.trans (by rw [h2]; rfl))
                have hb0 : b = 0 := by
                  cases int_noZD (2 * a) b hab0 with
                  | inr h => exact h
                  | inl h =>
                    exfalso
                    have ha0 : a = 0 := FinPrimary.two_cancel a 0 (h.trans (show (0 : Int) = 2 * 0 by lib_int))
                    exact GaussInt.int_odd_ne_even (2 * t - e) 0 ((haodd.symm.trans ha0).trans (by lib_int))
                have hAP : A = -P := by
                  calc A = (A + (a * a - b * b)) - (a * a + b * b) + 2 * (b * b) := by lib_int
                    _ = -P := by rw [hm0, hab, hb0]; lib_int
                have h4 : A + B - 1 = 4 * t' := ht'
                rw [hAP, hB0, hP4] at h4
                exact GaussInt.int_odd_ne_even (t' + c) 0 (FinPrimary.two_cancel _ _ (by
                  calc 2 * (2 * (t' + c) + 1) = 4 * t' + (4 * c + 2) := by lib_int
                    _ = (-(4 * c + 1) + 0 - 1) + (4 * c + 2) := by rw [h4]
                    _ = 2 * (2 * 0) := by lib_int))

/-- **a(𝔽_{p²}) = a_p² − 2p at every split prime p = 4k + 5** (y² = x³ − x). -/
theorem hasse_davenport_split (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * k + 1) + 1 + 1 + 1)) (n : Zp (2 * (2 * k + 1) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * k + 1) + 1 + 1)) n (2 * k + 1 + 1) = (ring (2 * (2 * k + 1) + 1 + 1)).neg (ring (2 * (2 * k + 1) + 1 + 1)).one) :
    -sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq (2 * k + 1) hpr n hn) (FinSign.f (Fq2Inst.ffq (2 * k + 1) hpr n hn) x)) (Fq2Inst.ffq (2 * k + 1) hpr n hn).els = (-sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1))) * (-sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x))  -- (+79 chars; full line in the Zenodo source)
  match Fq2Trace.fq_ap_primary (2 * k + 1) hpr n hn, SignCM3.ap_primary k hpr with
  | ⟨A, B, hAB, ⟨hB, hA4⟩, hS2⟩, ⟨a, b, hab, ⟨hb, ha4⟩, hS1⟩ =>
    have hsp := SplitHasse.split_hasse k hpr n hn
    generalize sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq (2 * k + 1) hpr n hn) (FinSign.f (Fq2Inst.ffq (2 * k + 1) hpr n hn) x)) (Fq2Inst.ffq (2 * k + 1) hpr n hn).els = T2 at hS2 hsp ⊢
    generalize sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1)) = T1 at hS1 hsp ⊢
    have h0 : castZ (ring (2 * (2 * k + 1) + 1 + 1)) (T2 + T1 * T1) = (ring (2 * (2 * k + 1) + 1 + 1)).zero := by
      rw [IntCast.castZ_add, castZ_mul, hsp]; lib_ring
    match castZ_zero_dvd (2 * (2 * k + 1) + 1 + 1) _ h0 with
    | ⟨q, hq⟩ =>
      have hP : Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) = 4 * (Int.ofNat k + 1) + 1 := by
        show 2 * (2 * Int.ofNat k + 1) + 1 + 1 + 1 = _; lib_int
      have e2 : T2 = -(2 * A) := by rw [← hS2]; lib_int
      have e1 : T1 = -(2 * a) := by rw [← hS1]; lib_int
      have hX : 2 * (2 * a * a - A) = Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * q := by rw [← hq, e2, e1]; lib_int
      have hr : A = 2 * a * a - Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1) * ((2 * Int.ofNat k + 3) * q - (2 * a * a - A)) := by
        calc A = 2 * a * a - ((2 * Int.ofNat k + 3) * (2 * (2 * a * a - A)) - (4 * Int.ofNat k + 5) * (2 * a * a - A)) := by lib_int
          _ = _ := by rw [hX, hP]; lib_int
      have hfin := finish (Int.ofNat (2 * (2 * k + 1) + 1 + 1 + 1)) a b A B _ (Int.ofNat k + 1) hP hab hb ha4 hAB hB hA4 hr
      rw [e2, e1, hfin, ← hab]
      lib_int

end HasseDavenport

#print axioms HasseDavenport.castZ_neg
#print axioms HasseDavenport.castZ_mul_nat
#print axioms HasseDavenport.castZ_mul
#print axioms HasseDavenport.castZ_zero_dvd
#print axioms HasseDavenport.two_mul_zero
#print axioms HasseDavenport.nat_sum_zero
#print axioms HasseDavenport.sq_zero
#print axioms HasseDavenport.finish
#print axioms HasseDavenport.hasse_davenport_split

At \(p\equiv 3\ (4)\) the same model \(\mathbb F_p[\sqrt n]\) gives \(-S_2=-2p=0^2-2p\): \(A+Bi\) primary of norm \(p^2\) with \(p\) inert forces \(\{|A|,|B|\}=\{p,0\}\), \(B\) even, \(A\equiv 1\ (4)\), so \(A=-p\). Splitting \(p=2k+3\) by the parity of \(k\),

\[ -\textstyle \sum _{z\in \mathbb F_p[\sqrt n]}\chi (z^3-z)=\Bigl (\sum _{x\in \mathbb F_p}\chi (x^3-x)\Bigr )^2-2p\qquad \text {for every odd prime }p . \]

Listing 108: lang/InertFq.lean

lang/InertFq.lean

/-
  lang/InertFq — T43, cycle 105: y² = x³ − x at inert p ≡ 3 (mod 4) over 𝔽_p[√n] for any non-residue n.

    s1_inert        Σ_{𝔽_p} χ(x³ − x) = 0                              (QuarticFamily.phi_inert, χ(−1) = −1)
    inert_fq        −Σ_{𝔽_p[√n]} χ(z³ − z) = 0² − 2p                   (Fq2Trace: A primary, A² + B² = p²; p inert ⇒ {|A|, |B|} = {p, 0};
                                                                       B even ⇒ B = 0; A ≡ 1 (mod 4) ⇒ A = −p)
  The same 𝔽_{p²} model as the split case (HasseDavenport), so a(𝔽_{p²}) = a_p² − 2p is stated with one construction at
  every odd prime (next file).

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.HasseDavenport
import lang.BaseChange

namespace InertFq

open FTAChain ZMod YMChain FiniteRecChain LibInt

theorem s1_inert (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) : sumK LibInt.intR (fun x => QChar.chi (2 * j) (CMInert.cub (ring (2 * (2 * j) + 1 + 1)) x)) (ConicSum.L (2 * j)) = 0 := by
  have hm1 := BaseChange.chi_negone j (TwoPower.negone_odd' (2 * (2 * j) + 1) j)
  have h := QuarticFamily.phi_inert (2 * j) hpr hm1 ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one)
  refine Eq.trans ?_ h
  apply sumK_ext
  intro x
  show QChar.chi (2 * j) (CMInert.cub (ring (2 * (2 * j) + 1 + 1)) x) = QChar.chi (2 * j) ((ring (2 * (2 * j) + 1 + 1)).mul x ((ring (2 * (2 * j) + 1 + 1)).add ((ring (2 * (2 * j) + 1 + 1)).mul x x) ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one)))
  rw [show CMInert.cub (ring (2 * (2 * j) + 1 + 1)) x = (ring (2 * (2 * j) + 1 + 1)).mul x ((ring (2 * (2 * j) + 1 + 1)).add ((ring (2 * (2 * j) + 1 + 1)).mul x x) ((ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one)) by
    show (ring (2 * (2 * j) + 1 + 1)).add ((ring (2 * (2 * j) + 1 + 1)).mul ((ring (2 * (2 * j) + 1 + 1)).mul x x) x) ((ring (2 * (2 * j) + 1 + 1)).neg x) = _; lib_ring]

theorem odd_ne_even4 (j t : Int) (h : 4 * j + 3 - 1 = 4 * t) : False :=
  GaussInt.int_odd_ne_even j t (FinPrimary.two_cancel _ _ (by
    rw [show (2 : Int) * (2 * t) = 4 * t by lib_int, ← h]; show (2 : Int) * (2 * j + 1) = 4 * j + 3 - 1; lib_int))

/-- **Inert p ≡ 3 (mod 4): −S(𝔽_p[√n]) = S(𝔽_p)² − 2p for every non-residue n.** -/
theorem inert_fq (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (n : Zp (2 * (2 * j) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) n (2 * j + 1) = (ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) :
    -sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq (2 * j) hpr n hn) (FinSign.f (Fq2Inst.ffq (2 * j) hpr n hn) x)) (Fq2Inst.ffq (2 * j) hpr n hn).els = (-sumK LibInt.intR (fun x => QChar.chi (2 * j) (CMInert.cub (ring (2 * (2 * j) + 1 + 1)) x)) (ConicSum.L (2 * j))) * (-sumK LibInt.intR (fun x => QChar.chi (2 * j) (CMInert.cub (ring (2 * (2 * j) + 1 + 1)) x)) (ConicSum.L (2 * j))) + -2 * In  -- (+39 chars; full line in the Zenodo source)
  rw [s1_inert j hpr]
  match Fq2Trace.fq_ap_primary (2 * j) hpr n hn with
  | ⟨A, B, hAB, ⟨⟨g, hg⟩, ⟨t, ht⟩⟩, hS2⟩ =>
    rw [hS2]
    have hP : Int.ofNat (2 * (2 * j) + 1 + 1 + 1) = 4 * Int.ofNat j + 3 := by show 2 * (2 * Int.ofNat j) + 1 + 1 + 1 = _; lib_int
    match CMModular.nat_sq A, CMModular.nat_sq B with
    | ⟨u, hu, hAu⟩, ⟨v, hv, hBv⟩ =>
      have huv : u * u + v * v = (2 * (2 * j) + 1 + 1 + 1) * (2 * (2 * j) + 1 + 1 + 1) := Int.ofNat.inj (by
        show Int.ofNat (u * u) + Int.ofNat (v * v) = Int.ofNat (2 * (2 * j) + 1 + 1 + 1) * Int.ofNat (2 * (2 * j) + 1 + 1 + 1)
        rw [← hu, ← hv]; exact hAB)
      cases Fp2Count.inert_norm j hpr u v huv with
      | inr h =>
        exfalso
        rw [h.2] at hBv
        cases hBv with
        | inl e =>
          exact GaussInt.int_odd_ne_even (2 * Int.ofNat j + 1) g (by rw [← hg, e, hP]; show (2 : Int) * (2 * Int.ofNat j + 1) + 1 = 4 * Int.ofNat j + 3; lib_int)
        | inr e =>
          exact GaussInt.int_odd_ne_even (2 * Int.ofNat j + 1) (-g) (by
            rw [show 2 * -g = -(2 * g) by lib_int, ← hg, e, hP]; show (2 : Int) * (2 * Int.ofNat j + 1) + 1 = -(-(4 * Int.ofNat j + 3)); lib_int)
      | inl h =>
        rw [h.1] at hAu; rw [h.2] at hBv
        have hB0 : B = 0 := by cases hBv with | inl e => exact e | inr e => rw [e]; rfl
        rw [hB0] at ht
        cases hAu with
        | inl e =>
          exfalso
          rw [e, hP] at ht
          exact odd_ne_even4 (Int.ofNat j) t (by rw [← ht]; show (4 : Int) * Int.ofNat j + 3 - 1 = 4 * Int.ofNat j + 3 + 0 - 1; lib_int)
        | inr e => rw [e]; lib_int

end InertFq

#print axioms InertFq.s1_inert
#print axioms InertFq.odd_ne_even4
#print axioms InertFq.inert_fq

Listing 109: lang/EveryPrime.lean

lang/EveryPrime.lean

/-
  lang/EveryPrime — T43, cycle 106: a(𝔽_{p²}) = a_p² − 2p for y² = x³ − x at every odd prime p.

  One construction 𝔽_{p²} = 𝔽_p[√n] (any non-residue n, Fq2Inst.ffq) for every p = 2k + 3:
    nat_even_odd       k = 2j or k = 2j + 1
    every_prime        −Σ_{𝔽_{p²}} χ(z³ − z) = (Σ_{𝔽_p} χ(x³ − x))² − 2p
  (p ≡ 3 mod 4: InertFq; p ≡ 1 mod 4: HasseDavenport). With α + β = a_p, αβ = p this is a(𝔽_{p²}) = α² + β²: the second
  coefficient of the local zeta function of the CM curve at every odd prime is the one of its Hecke L-factor.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.InertFq

namespace EveryPrime

open FTAChain ZMod YMChain FiniteRecChain

theorem nat_even_odd : ∀ k : Nat, (∃ j, k = 2 * j) ∨ (∃ j, k = 2 * j + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | k + 1 => by
    cases nat_even_odd k with
    | inl h => match h with | ⟨j, hj⟩ => exact Or.inr ⟨j, by rw [hj]⟩
    | inr h => match h with | ⟨j, hj⟩ => exact Or.inl ⟨j + 1, by rw [hj]; lib_nat⟩

/-- **a(𝔽_{p²}) = a_p² − 2p for y² = x³ − x at every odd prime.** -/
theorem every_prime (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    -sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els = (-sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k)) * (-sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k)) + -2 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases nat_even_odd k with
  | inl h =>
    match h with
    | ⟨j, hj⟩ => subst hj; exact InertFq.inert_fq j hpr n hn
  | inr h =>
    match h with
    | ⟨j, hj⟩ => subst hj; exact HasseDavenport.hasse_davenport_split j hpr n hn

end EveryPrime

#print axioms EveryPrime.nat_even_odd
#print axioms EveryPrime.every_prime

Consequently the \(\mathrm {Sym}^2\) (GL\(_3\)) Satake polynomial at every odd \(p\) is read off from the point counts:

\[ 1-(a_p^2-p)X+p(a_p^2-p)X^2-p^3X^3=(1-pX)\,(1-A X+p^2X^2),\qquad A=a(\mathbb F_{p^2})=a_p^2-2p . \]

Listing 110: lang/Sym2Count.lean

lang/Sym2Count.lean

/-
  lang/Sym2Count — T43, cycle 133: the Sym² (GL3) local factor of the CM form of y² = x³ − x at every odd prime, from point
  counts over 𝔽_p and 𝔽_{p²}.

  With a_p = −S(𝔽_p), A = a(𝔽_{p²}) = −S(𝔽_{p²}) and d = p, the Sym² Satake polynomial cLift(a_p, p) (Sym2Local) is
      1 − (A + p) X + p (A + p) X² − p³ X³ = (1 − pX)(1 − A X + p² X²),
  i.e. L_p(Sym² E, s) = ζ_p(s − 1) · L(E/𝔽_{p²}, 2s) locally: the GL3 lift at p is read off from #E(𝔽_p) and #E(𝔽_{p²})
  (EveryPrime: A = a_p² − 2p).
    sym2_count   the three coefficients

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.EveryPrime
import lang.Sym2Local

namespace Sym2Count

open FTAChain ZMod YMChain FiniteRecChain LibInt

theorem sym2_count (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    Sym2Local.cLift LibInt.intR (-sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k)) (Int.ofNat (2 * k + 1 + 1 + 1)) 1 = -((-sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els) + Int.ofNat (2 * k + 1 + 1 + 1)) ∧
    Sym2Local.cLift LibInt.intR (-sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k)) (Int.ofNat (2 * k + 1 + 1 + 1)) 2 = Int.ofNat (2 * k + 1 + 1 + 1) * ((-sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els) + Int.ofNat (2 * k + 1 + 1 + 1)) ∧
    Sym2Local.cLift LibInt.intR (-sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k)) (Int.ofNat (2 * k + 1 + 1 + 1)) 3 = -(Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1)) := by
  have h := EveryPrime.every_prime k hpr n hn
  generalize sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq k hpr n hn) (FinSign.f (Fq2Inst.ffq k hpr n hn) x)) (Fq2Inst.ffq k hpr n hn).els = T2 at h ⊢
  generalize sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k) = T1 at h ⊢
  refine ⟨?_, ?_, ?_⟩
  · show -((-T1) * (-T1) + -(Int.ofNat (2 * k + 1 + 1 + 1))) = _
    rw [h]; lib_int
  · show Int.ofNat (2 * k + 1 + 1 + 1) * ((-T1) * (-T1) + -(Int.ofNat (2 * k + 1 + 1 + 1))) = _
    rw [h]; lib_int
  · show -(Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1)) = _
    rfl

end Sym2Count

#print axioms Sym2Count.sym2_count

For every quartic twist \(y^2=x^3+Dx\) with \(D\in \mathbb F_p^\times \), with \(\varphi _q(D)=\sum _{x\in \mathbb F_q}\chi (x(x^2+D))\),

\[ -\varphi _{p^2}(D)=\varphi _p(D)^2-2p\qquad \text {for every odd prime }p . \]
The steps are
\[ \begin {aligned} &-D=\tau ^2\ \text {in}\ \mathbb F_{p^2},\quad \varphi _{p^2}(D)=\chi (\tau )\,\varphi _{p^2}(-1),\quad \chi (\tau )=\bigl ((-D)^{(p-1)/2}\bigr )^{(p+1)/2},\\ &p\equiv 3\ (4):\ \chi (\tau )=1,\ \varphi _p(D)=0;\qquad p\equiv 1\ (4):\ \chi (\tau )=\chi _p(-D)=\chi _p(D),\\ &\varphi _p(D)^2=\varphi _p(1)^2\ (\chi (D)=1),\quad \varphi _p(n)^2=4p-\varphi _p(1)^2\ (\chi (D)=-1),\quad \varphi _p(-1)^2=\varphi _p(1)^2 . \end {aligned} \]

Listing 111: lang/FamilyFq.lean

lang/FamilyFq.lean

/-
  lang/FamilyFq — T43, cycle 110: the quartic twists y² = x³ + Dx (D ∈ 𝔽_p^×) over 𝔽_{p²} = 𝔽_p[√n].

  φ_q(a) = Σ_{x ∈ 𝔽_q} χ(x(x² + a)). In 𝔽_{p²}, −D is a square τ² (every element of 𝔽_p is), so φ(D) = χ(τ) φ(−1)
  (FinConic.phi_scale), and τ^((p²−1)/2) = ((−D)^((p−1)/2))^((p+1)/2):
    family_scale     ∃ τ ≠ 0, φ_{p²}(D) = χ(τ) φ_{p²}(−1),  τ^h = ((−D)^((p−1)/2))^((p+1)/2)
    family_inert     p ≡ 3 (mod 4):  φ_{p²}(D) = φ_{p²}(−1)              ((p + 1)/2 even)
    family_split     p ≡ 1 (mod 4):  φ_{p²}(D) = χ_p(−D) φ_{p²}(−1)       ((p + 1)/2 odd)
  The twist by D over 𝔽_{p²} is the twist by the quartic-residue sign of D, the base change of the quartic character.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.EveryPrime
import lang.SplitHasse
import lang.CubicSuper2

namespace FamilyFq

open FTAChain ZMod YMChain FiniteRecChain Quad

theorem h_two (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1)) (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) : (Fq2Inst.ffq k hpr n hn).h = 2 * ((k + 1) * (k + 2)) := by
  rw [CubicSuper2.h_eq k hpr n hn]; lib_nat

theorem family_scale (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1)) (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    ∃ τ, τ ≠ (Fq2.G2 (2 * k + 1) n).zero ∧
      FinConic.phi (Fq2Inst.ffq k hpr n hn) (D, (ring (2 * k + 1 + 1)).zero) = FinField.chi (Fq2Inst.ffq k hpr n hn) τ * FinConic.phi (Fq2Inst.ffq k hpr n hn) ((Fq2.G2 (2 * k + 1) n).neg (Fq2.G2 (2 * k + 1) n).one) ∧
      Frobenius.pw (Fq2.G2 (2 * k + 1) n) τ (Fq2Inst.ffq k hpr n hn).h = (Frobenius.pw (ring (2 * k + 1 + 1)) (Frobenius.pw (ring (2 * k + 1 + 1)) ((ring (2 * k + 1 + 1)).neg D) (k + 1)) (k + 2), (ring (2 * k + 1 + 1)).zero) := by
  have hnD : (ring (2 * k + 1 + 1)).neg D ≠ (ring (2 * k + 1 + 1)).zero := fun e => hD (by rw [← DetChain.neg_neg' (ring (2 * k + 1 + 1)) D, e]; exact (DetChain.neg_unique (ring (2 * k + 1 + 1)) _ _ ((ring (2 * k + 1 + 1)).zero_add _)).symm)
  have hc0 : ((ring (2 * k + 1 + 1)).neg D, (ring (2 * k + 1 + 1)).zero) ≠ (Fq2.G2 (2 * k + 1) n).zero := fun e => hnD (congrArg Prod.fst e)
  have hch : Frobenius.pw (Fq2Inst.ffq k hpr n hn).K ((ring (2 * k + 1 + 1)).neg D, (ring (2 * k + 1 + 1)).zero) (Fq2Inst.ffq k hpr n hn).h = (Fq2Inst.ffq k hpr n hn).K.one := by
    show Frobenius.pw (Fq2.G2 (2 * k + 1) n) ((ring (2 * k + 1 + 1)).neg D, (ring (2 * k + 1 + 1)).zero) (Fq2Inst.ffq k hpr n hn).h = _
    rw [CubicSuper2.h_eq k hpr n hn, QuadFrob.pw_embed (2 * k + 1) n,
      show (2 * k + 1 + 1 + 1 + 1) * (k + 1) = (2 * k + 1 + 1) * (k + 2) by lib_nat, ← TwoPower.pwpow,
      ZMod.unit_pow (2 * k + 1) hpr _ hnD, TwoPower.pwone]
    rfl
  match (Fq2Inst.ffq k hpr n hn).euler_hard _ hch with
  | ⟨τ, hτ⟩ =>
    have hτ' : (Fq2.G2 (2 * k + 1) n).mul τ τ = ((ring (2 * k + 1 + 1)).neg D, (ring (2 * k + 1 + 1)).zero) := hτ
    have hτ0 : τ ≠ (Fq2.G2 (2 * k + 1) n).zero := fun e => hc0 (by rw [← hτ', e]; exact DetChain.mul_zero (Fq2.G2 (2 * k + 1) n) _)
    refine ⟨τ, hτ0, ?_, ?_⟩
    · have e : (D, (ring (2 * k + 1 + 1)).zero) = (Fq2Inst.ffq k hpr n hn).K.mul ((Fq2.G2 (2 * k + 1) n).neg (Fq2.G2 (2 * k + 1) n).one) ((Fq2Inst.ffq k hpr n hn).K.mul τ τ) := by
        show (D, (ring (2 * k + 1 + 1)).zero) = (Fq2.G2 (2 * k + 1) n).mul ((Fq2.G2 (2 * k + 1) n).neg (Fq2.G2 (2 * k + 1) n).one) ((Fq2.G2 (2 * k + 1) n).mul τ τ)
        rw [hτ']
        apply Prod.ext
        · show D = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) ((ring (2 * k + 1 + 1)).neg D)) ((ring (2 * k + 1 + 1)).mul n ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).zero) (ring (2 * k + 1 + 1)).zero)); lib_ring
        · show (ring (2 * k + 1 + 1)).zero = (ring (2 * k + 1 + 1)).add ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (ring (2 * k + 1 + 1)).zero) ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).zero) ((ring (2 * k + 1 + 1)).neg D)); lib_ring
      rw [e]
      exact FinConic.phi_scale (Fq2Inst.ffq k hpr n hn) _ τ hτ0
    · rw [h_two k hpr n hn, ← TwoPower.pwpow,
        show Frobenius.pw (Fq2.G2 (2 * k + 1) n) τ 2 = (Fq2.G2 (2 * k + 1) n).mul τ τ by show (Fq2.G2 (2 * k + 1) n).mul ((Fq2.G2 (2 * k + 1) n).mul (Fq2.G2 (2 * k + 1) n).one τ) τ = _; rw [(Fq2.G2 (2 * k + 1) n).one_mul],
        hτ', QuadFrob.pw_embed (2 * k + 1) n, ← TwoPower.pwpow]

theorem family_inert (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (n : Zp (2 * (2 * j) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) n (2 * j + 1) = (ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) (D : Zp (2 * (2 * j) + 1 + 1)) (hD : D ≠ (ring (2 * (2 * j) + 1 + 1)).zero) :
    FinConic.phi (Fq2Inst.ffq (2 * j) hpr n hn) (D, (ring (2 * (2 * j) + 1 + 1)).zero) = FinConic.phi (Fq2Inst.ffq (2 * j) hpr n hn) ((Fq2.G2 (2 * (2 * j) + 1) n).neg (Fq2.G2 (2 * (2 * j) + 1) n).one) := by
  have hnD : (ring (2 * (2 * j) + 1 + 1)).neg D ≠ (ring (2 * (2 * j) + 1 + 1)).zero := fun e => hD (by rw [← DetChain.neg_neg' (ring (2 * (2 * j) + 1 + 1)) D, e]; exact (DetChain.neg_unique (ring (2 * (2 * j) + 1 + 1)) _ _ ((ring (2 * (2 * j) + 1 + 1)).zero_add _)).symm)
  match family_scale (2 * j) hpr n hn D hD with
  | ⟨τ, hτ0, hphi, hpow⟩ =>
    have he : Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) (Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) ((ring (2 * (2 * j) + 1 + 1)).neg D) (2 * j + 1)) (2 * j + 2) = (ring (2 * (2 * j) + 1 + 1)).one := by
      cases Euler.euler_pm (2 * j) hpr _ hnD with
      | inl h => rw [h, TwoPower.pwone]
      | inr h => rw [h, show 2 * j + 2 = 2 * (j + 1) by lib_nat, SplitHasse.negone_even]
    rw [he] at hpow
    rw [hphi, FinField.chi_one_of _ τ hτ0 hpow]
    lib_int

theorem family_split (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j + 1) + 1 + 1 + 1)) (n : Zp (2 * (2 * j + 1) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * j + 1) + 1 + 1)) n (2 * j + 1 + 1) = (ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) (D : Zp (2 * (2 * j + 1) + 1 + 1)) (hD : D ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero) :
    FinConic.phi (Fq2Inst.ffq (2 * j + 1) hpr n hn) (D, (ring (2 * (2 * j + 1) + 1 + 1)).zero) = QChar.chi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg D) * FinConic.phi (Fq2Inst.ffq (2 * j + 1) hpr n hn) ((Fq2.G2 (2 * (2 * j + 1) + 1) n).neg (Fq2.G2 (2 * (2 * j + 1) + 1) n).one) := by
  have hnD : (ring (2 * (2 * j + 1) + 1 + 1)).neg D ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero := fun e => hD (by rw [← DetChain.neg_neg' (ring (2 * (2 * j + 1) + 1 + 1)) D, e]; exact (DetChain.neg_unique (ring (2 * (2 * j + 1) + 1 + 1)) _ _ ((ring (2 * (2 * j + 1) + 1 + 1)).zero_add _)).symm)
  match family_scale (2 * j + 1) hpr n hn D hD with
  | ⟨τ, hτ0, hphi, hpow⟩ =>
    rw [hphi]
    cases Euler.euler_pm (2 * j + 1) hpr _ hnD with
    | inl h =>
      rw [h, TwoPower.pwone] at hpow
      rw [FinField.chi_one_of _ τ hτ0 hpow, QChar.chi_one_of (2 * j + 1) _ hnD h]
    | inr h =>
      rw [h, show 2 * j + 1 + 2 = 2 * (j + 1) + 1 by lib_nat, TwoPower.negone_odd' (2 * (2 * j + 1) + 1) (j + 1)] at hpow
      have hne : Frobenius.pw (Fq2Inst.ffq (2 * j + 1) hpr n hn).K τ (Fq2Inst.ffq (2 * j + 1) hpr n hn).h ≠ (Fq2Inst.ffq (2 * j + 1) hpr n hn).K.one := fun e =>
        QChar.ne_pm (2 * j + 1) (congrArg Prod.fst (hpow.symm.trans e))
      have hne' : Frobenius.pw (ring (2 * (2 * j + 1) + 1 + 1)) ((ring (2 * (2 * j + 1) + 1 + 1)).neg D) (2 * j + 1 + 1) ≠ (ring (2 * (2 * j + 1) + 1 + 1)).one := fun e => QChar.ne_pm (2 * j + 1) (h.symm.trans e)
      rw [FinField.chi_neg_of _ τ hτ0 hne, QChar.chi_neg_of (2 * j + 1) _ hnD hne']

end FamilyFq

#print axioms FamilyFq.h_two
#print axioms FamilyFq.family_scale
#print axioms FamilyFq.family_inert
#print axioms FamilyFq.family_split

Listing 112: lang/FamilyHD.lean

lang/FamilyHD.lean

/-
  lang/FamilyHD — T43, cycle 111: a(𝔽_{p²}) = a_p² − 2p for every quartic twist y² = x³ + Dx, D ∈ 𝔽_p^×, at every odd p.

  φ_q(D) = Σ_{x ∈ 𝔽_q} χ(x(x² + D)), a_q(E_D) = −φ_q(D).
    four_p          φ_p(1)² + φ_p(n)² = 4p at p ≡ 1 (mod 4)                   (JacobMoment, JacobClass; as in CMModular)
    phi_sq_class    φ_p(D)² = φ_p(1)² if χ(D) = 1,  φ_p(n)² if χ(D) = −1        (JacobClass.phi_class)
    family_hd_split p ≡ 1 (mod 4): −φ_{p²}(D) = φ_p(D)² − 2p  (χ(−D) = χ(D); FamilyFq.family_split + HasseDavenport)
    family_hd_inert p ≡ 3 (mod 4): −φ_{p²}(D) = φ_p(D)² − 2p = −2p
    family_hd       every odd p
  So every curve y² = x³ + Dx has the Frobenius-square identity of a CM form at every good prime: the quartic twists of the
  Hecke character of ℚ(i), base-changed to 𝔽_{p²}.

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FamilyFq
import lang.CMModular

namespace FamilyHD

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal JacobMoment JacobClass LibInt

theorem four_p (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j + 1) + 1 + 1 + 1)) (n : Zp (2 * (2 * j + 1) + 1 + 1))
    (hn0 : n ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero) (hn : chi (2 * j + 1) n = -1) :
    phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one * phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one + phi (2 * j + 1) n * phi (2 * j + 1) n = 4 * P (2 * j + 1) := by
  match GaussSplit.split_one_mod_four j hpr with
  | ⟨i, hi⟩ =>
    have hi0 : i ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero := fun e => by
      rw [e, DetChain.zero_mul'] at hi
      apply PowerSum.one_ne_zero' (2 * (2 * j + 1) + 1)
      have := congrArg (fun z => (ring (2 * (2 * j + 1) + 1 + 1)).add z (ring (2 * (2 * j + 1) + 1 + 1)).one) hi
      exact (((ring (2 * (2 * j + 1) + 1 + 1)).zero_add (ring (2 * (2 * j + 1) + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * (2 * j + 1) + 1 + 1)) (ring (2 * (2 * j + 1) + 1 + 1)).one)))
    have hchi : chi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) = 1 := by rw [← hi]; exact chi_sq (2 * j + 1) hpr i hi0
    have hM := moment (2 * j + 1) hpr
    have hG := grouped (2 * j + 1) hpr n hn0 hn
    rw [hchi] at hM
    rw [hM] at hG
    have hz : (P (2 * j + 1) + -1) * (phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one * phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one + phi (2 * j + 1) n * phi (2 * j + 1) n + -1 * (4 * P (2 * j + 1))) = 0 := by
      have : (P (2 * j + 1) + -1) * (phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one * phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one + phi (2 * j + 1) n * phi (2 * j + 1) n + -1 * (4 * P (2 * j + 1)))
          = (P (2 * j + 1) + -1) * (phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one * phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one + phi (2 * j + 1) n * phi (2 * j + 1) n) + -1 * (2 * (P (2 * j + 1) * ((1 + 1) * (P (2 * j + 1) + -1)))) := by
        lib_int
      rw [this, ← hG]; lib_int
    have hP1 : P (2 * j + 1) + -1 ≠ 0 := fun e => by
      have e1 : P (2 * j + 1) = 1 := by
        have : P (2 * j + 1) = (P (2 * j + 1) + -1) + 1 := by lib_int
        rw [this, e]; rfl
      have e3 := Int.ofNat.inj (show Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) = Int.ofNat 1 from e1)
      exact Nat.noConfusion (Nat.succ.inj e3)
    cases LibInt.int_noZD _ _ hz with
    | inl h => exact absurd h hP1
    | inr h =>
      calc phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one * phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one + phi (2 * j + 1) n * phi (2 * j + 1) n
          = (phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one * phi (2 * j + 1) (ring (2 * (2 * j + 1) + 1 + 1)).one + phi (2 * j + 1) n * phi (2 * j + 1) n + -1 * (4 * P (2 * j + 1))) + 4 * P (2 * j + 1) := by lib_int
        _ = 4 * P (2 * j + 1) := by rw [h]; exact LibInt.intR.zero_add _

theorem phi_sq_one (mm : Nat) (hpr : ∀ d, Dv d (2 * mm + 1 + 1 + 1) → d = 1 ∨ d = 2 * mm + 1 + 1 + 1) (n : Zp (2 * mm + 1 + 1))
    (hn0 : n ≠ (ring (2 * mm + 1 + 1)).zero) (hn : chi mm n = -1) (D : Zp (2 * mm + 1 + 1)) (hD : chi mm D = 1) :
    phi mm D * phi mm D = phi mm (ring (2 * mm + 1 + 1)).one * phi mm (ring (2 * mm + 1 + 1)).one := by
  have hc := phi_class mm hpr n hn0 hn D
  rw [hD] at hc
  exact FinPrimary.two_cancel _ _ (hc.trans (by lib_int))

theorem phi_sq_neg (mm : Nat) (hpr : ∀ d, Dv d (2 * mm + 1 + 1 + 1) → d = 1 ∨ d = 2 * mm + 1 + 1 + 1) (n : Zp (2 * mm + 1 + 1))
    (hn0 : n ≠ (ring (2 * mm + 1 + 1)).zero) (hn : chi mm n = -1) (D : Zp (2 * mm + 1 + 1)) (hD : chi mm D = -1) :
    phi mm D * phi mm D = phi mm n * phi mm n := by
  have hc := phi_class mm hpr n hn0 hn D
  rw [hD] at hc
  exact FinPrimary.two_cancel _ _ (hc.trans (by lib_int))

theorem s2_phi {R : Type} [DecidableEq R] (F : FinField.FF R) :
    sumK LibInt.intR (fun x => FinField.chi F (FinSign.f F x)) F.els = FinConic.phi F (F.K.neg F.K.one) := by
  apply sumK_ext
  intro x
  show FinField.chi F (CMInert.cub F.K x) = FinField.chi F (F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)))
  rw [show CMInert.cub F.K x = F.K.mul x (F.K.add (F.K.mul x x) (F.K.neg F.K.one)) by
    show F.K.add (F.K.mul (F.K.mul x x) x) (F.K.neg x) = _; lib_ring]

theorem s1_phi (mm : Nat) :
    sumK LibInt.intR (fun x => chi mm (CMInert.cub (ring (2 * mm + 1 + 1)) x)) (L mm) = phi mm ((ring (2 * mm + 1 + 1)).neg (ring (2 * mm + 1 + 1)).one) := by
  apply sumK_ext
  intro x
  show chi mm (CMInert.cub (ring (2 * mm + 1 + 1)) x) = chi mm ((ring (2 * mm + 1 + 1)).mul x ((ring (2 * mm + 1 + 1)).add ((ring (2 * mm + 1 + 1)).mul x x) ((ring (2 * mm + 1 + 1)).neg (ring (2 * mm + 1 + 1)).one)))
  rw [show CMInert.cub (ring (2 * mm + 1 + 1)) x = (ring (2 * mm + 1 + 1)).mul x ((ring (2 * mm + 1 + 1)).add ((ring (2 * mm + 1 + 1)).mul x x) ((ring (2 * mm + 1 + 1)).neg (ring (2 * mm + 1 + 1)).one)) by
    show (ring (2 * mm + 1 + 1)).add ((ring (2 * mm + 1 + 1)).mul ((ring (2 * mm + 1 + 1)).mul x x) x) ((ring (2 * mm + 1 + 1)).neg x) = _; lib_ring]

/-- **p ≡ 1 (mod 4).** -/
theorem family_hd_split (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j + 1) + 1 + 1 + 1)) (n : Zp (2 * (2 * j + 1) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * j + 1) + 1 + 1)) n (2 * j + 1 + 1) = (ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) (D : Zp (2 * (2 * j + 1) + 1 + 1)) (hD : D ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero) :
    -FinConic.phi (Fq2Inst.ffq (2 * j + 1) hpr n hn) (D, (ring (2 * (2 * j + 1) + 1 + 1)).zero) = phi (2 * j + 1) D * phi (2 * j + 1) D + -2 * Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) := by
  have hn0 : n ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero := fun e => by
    rw [e, HasseCM.pw_zero_succ] at hn
    apply PowerSum.one_ne_zero' (2 * (2 * j + 1) + 1)
    have := congrArg (fun z => (ring (2 * (2 * j + 1) + 1 + 1)).add z (ring (2 * (2 * j + 1) + 1 + 1)).one) hn
    exact (((ring (2 * (2 * j + 1) + 1 + 1)).zero_add (ring (2 * (2 * j + 1) + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * (2 * j + 1) + 1 + 1)) (ring (2 * (2 * j + 1) + 1 + 1)).one)))
  have hnc : chi (2 * j + 1) n = -1 := chi_neg_of (2 * j + 1) n hn0 (fun e => ne_pm (2 * j + 1) (hn.symm.trans e))
  match GaussSplit.split_one_mod_four j hpr with
  | ⟨i, hi⟩ =>
    have hi0 : i ≠ (ring (2 * (2 * j + 1) + 1 + 1)).zero := fun e => by
      rw [e, DetChain.zero_mul'] at hi
      apply PowerSum.one_ne_zero' (2 * (2 * j + 1) + 1)
      have := congrArg (fun z => (ring (2 * (2 * j + 1) + 1 + 1)).add z (ring (2 * (2 * j + 1) + 1 + 1)).one) hi
      exact (((ring (2 * (2 * j + 1) + 1 + 1)).zero_add (ring (2 * (2 * j + 1) + 1 + 1)).one).symm.trans (this.trans (DetChain.add_neg' (ring (2 * (2 * j + 1) + 1 + 1)) (ring (2 * (2 * j + 1) + 1 + 1)).one)))
    have hm1 : chi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) = 1 := by rw [← hi]; exact chi_sq (2 * j + 1) hpr i hi0
    have hHD := HasseDavenport.hasse_davenport_split j hpr n hn
    rw [s2_phi, s1_phi] at hHD
    have h4 := four_p j hpr n hn0 hnc
    have hP : P (2 * j + 1) = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) := rfl
    have hm1sq := phi_sq_one (2 * j + 1) hpr n hn0 hnc _ hm1
    rw [FamilyFq.family_split j hpr n hn D hD]
    have hcD : chi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg D) = chi (2 * j + 1) D := by
      rw [show (ring (2 * (2 * j + 1) + 1 + 1)).neg D = (ring (2 * (2 * j + 1) + 1 + 1)).mul ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) D by lib_ring, chi_mul (2 * j + 1) hpr, hm1]; lib_int
    rw [hcD]
    have hneg : (Fq2.G2 (2 * (2 * j + 1) + 1) n).neg (Fq2.G2 (2 * (2 * j + 1) + 1) n).one = (Fq2Inst.ffq (2 * j + 1) hpr n hn).K.neg (Fq2Inst.ffq (2 * j + 1) hpr n hn).K.one := rfl
    rw [hneg]
    generalize FinConic.phi (Fq2Inst.ffq (2 * j + 1) hpr n hn) ((Fq2Inst.ffq (2 * j + 1) hpr n hn).K.neg (Fq2Inst.ffq (2 * j + 1) hpr n hn).K.one) = S2 at hHD ⊢
    cases JacobClass.chi_cases (2 * j + 1) D with
    | inl h0 => exact absurd h0 (chi_ne_zero (2 * j + 1) D hD)
    | inr h =>
      cases h with
      | inl h1 =>
        rw [h1, phi_sq_one (2 * j + 1) hpr n hn0 hnc D h1, ← hm1sq]
        rw [show -(1 * S2) = -S2 by lib_int, hHD]; lib_int
      | inr h1 =>
        rw [h1, phi_sq_neg (2 * j + 1) hpr n hn0 hnc D h1]
        have e : phi (2 * j + 1) n * phi (2 * j + 1) n = 4 * Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) - phi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) * phi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one) := by
          rw [hm1sq, ← hP, ← h4]; lib_int
        rw [e]
        calc -(-1 * S2) = -(-S2) := by lib_int
          _ = -((-phi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one)) * (-phi (2 * j + 1) ((ring (2 * (2 * j + 1) + 1 + 1)).neg (ring (2 * (2 * j + 1) + 1 + 1)).one)) + -2 * Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1)) := by rw [hHD]
          _ = _ := by lib_int

/-- **p ≡ 3 (mod 4).** -/
theorem family_hd_inert (j : Nat) (hpr : ∀ d, Dv d (2 * (2 * j) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * j) + 1 + 1 + 1)) (n : Zp (2 * (2 * j) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (2 * j) + 1 + 1)) n (2 * j + 1) = (ring (2 * (2 * j) + 1 + 1)).neg (ring (2 * (2 * j) + 1 + 1)).one) (D : Zp (2 * (2 * j) + 1 + 1)) (hD : D ≠ (ring (2 * (2 * j) + 1 + 1)).zero) :
    -FinConic.phi (Fq2Inst.ffq (2 * j) hpr n hn) (D, (ring (2 * (2 * j) + 1 + 1)).zero) = phi (2 * j) D * phi (2 * j) D + -2 * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) := by
  rw [QuarticFamily.phi_inert (2 * j) hpr (BaseChange.chi_negone j (TwoPower.negone_odd' (2 * (2 * j) + 1) j)) D,
    FamilyFq.family_inert j hpr n hn D hD]
  have h := InertFq.inert_fq j hpr n hn
  rw [s2_phi, InertFq.s1_inert j hpr] at h
  exact h

/-- **a(𝔽_{p²}) = a_p² − 2p for y² = x³ + Dx, every D ∈ 𝔽_p^×, every odd prime p.** -/
theorem family_hd (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    -FinConic.phi (Fq2Inst.ffq k hpr n hn) (D, (ring (2 * k + 1 + 1)).zero) = phi k D * phi k D + -2 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases EveryPrime.nat_even_odd k with
  | inl h => match h with | ⟨j, hj⟩ => subst hj; exact family_hd_inert j hpr n hn D hD
  | inr h => match h with | ⟨j, hj⟩ => subst hj; exact family_hd_split j hpr n hn D hD

end FamilyHD

#print axioms FamilyHD.four_p
#print axioms FamilyHD.phi_sq_one
#print axioms FamilyHD.phi_sq_neg
#print axioms FamilyHD.s2_phi
#print axioms FamilyHD.s1_phi
#print axioms FamilyHD.family_hd_split
#print axioms FamilyHD.family_hd_inert
#print axioms FamilyHD.family_hd

For the sextic twists \(y^2=x^3+D\) at \(p\equiv 2\ (3)\): \(D=s^3\) with \(s\in \mathbb F_p\), \(s\) is a square in \(\mathbb F_{p^2}\), so \(\psi _{p^2}(D)=\chi (s)\psi _{p^2}(1)=2p\) and \(\psi _p(D)=0\), giving \(-\psi _{p^2}(D)=\psi _p(D)^2-2p\).

Listing 113: lang/SexticInert.lean

lang/SexticInert.lean

/-
  lang/SexticInert — T43, cycle 112: the sextic twists y² = x³ + D (D ∈ 𝔽_p^×) at inert primes p ≡ 2 (mod 3), over 𝔽_{p²}.

  For p ≡ 2 (mod 3) every D ∈ 𝔽_p is a cube s³ (s = D^e, 3e = 2(p − 1) + 1: CMEisen), and s is a square in 𝔽_{p²}, so
    psi_twist_inert    ψ_{𝔽_{p²}}(D) = ψ_{𝔽_{p²}}(1) = 2p                     (FinCubic.psi_scale)
    sextic_hd_inert    −ψ_{𝔽_{p²}}(D) = ψ_{𝔽_p}(D)² − 2p,  ψ_{𝔽_p}(D) = 0       (SexticFamily.psi_inert)
  The Frobenius-square identity for every sextic twist at the inert primes of ℚ(√−3).

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CubicSuper2
import lang.FamilyFq

namespace SexticInert

open FTAChain ZMod YMChain FiniteRecChain

theorem psi_twist_inert (k c : Nat) (hc : 2 * k + 1 + 1 + 1 + 1 = 3 * c) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1))
    (n : Zp (2 * k + 1 + 1)) (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    FinCubic.psi (Fq2Inst.ffq k hpr n hn) (D, (ring (2 * k + 1 + 1)).zero) = Int.ofNat (2 * (2 * k + 1 + 1 + 1)) := by
  match CubicSuper2.e_form k c hc with
  | ⟨e, he⟩ =>
    have he' : 3 * e = 2 * (2 * k + 1 + 1) + 1 := he
    match (⟨Frobenius.pw (ring (2 * k + 1 + 1)) D e, CMEisen.inv_cube (2 * k + 1) hpr e he' D⟩ : ∃ s, Frobenius.pw (ring (2 * k + 1 + 1)) s 3 = D) with
    | ⟨s, hs3⟩ =>
      have hs0 : s ≠ (ring (2 * k + 1 + 1)).zero := fun h0 => hD (by rw [← hs3, h0]; exact HasseCM.pw_zero_succ (ring (2 * k + 1 + 1)) 2)
      have ht0 : (s, (ring (2 * k + 1 + 1)).zero) ≠ (Fq2.G2 (2 * k + 1) n).zero := fun h0 => hs0 (congrArg Prod.fst h0)
      have hD' : (D, (ring (2 * k + 1 + 1)).zero) = (Fq2Inst.ffq k hpr n hn).K.mul (Fq2Inst.ffq k hpr n hn).K.one (FinCubic.cube (Fq2Inst.ffq k hpr n hn) (s, (ring (2 * k + 1 + 1)).zero)) := by
        rw [(Fq2Inst.ffq k hpr n hn).K.one_mul]
        show (D, (ring (2 * k + 1 + 1)).zero) = (Fq2.G2 (2 * k + 1) n).mul (s, (ring (2 * k + 1 + 1)).zero) ((Fq2.G2 (2 * k + 1) n).mul (s, (ring (2 * k + 1 + 1)).zero) (s, (ring (2 * k + 1 + 1)).zero))
        rw [Fq2.embed_mul, Fq2.embed_mul]
        apply Prod.ext
        · show D = (ring (2 * k + 1 + 1)).mul s ((ring (2 * k + 1 + 1)).mul s s)
          rw [← hs3]; show (ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul ((ring (2 * k + 1 + 1)).mul (ring (2 * k + 1 + 1)).one s) s) s = _; lib_ring
        · rfl
      have hchi : FinField.chi (Fq2Inst.ffq k hpr n hn) (s, (ring (2 * k + 1 + 1)).zero) = 1 := by
        apply FinField.chi_one_of _ _ ht0
        show Frobenius.pw (Fq2.G2 (2 * k + 1) n) (s, (ring (2 * k + 1 + 1)).zero) (Fq2Inst.ffq k hpr n hn).h = (Fq2.G2 (2 * k + 1) n).one
        rw [CubicSuper2.h_eq k hpr n hn, QuadFrob.pw_embed (2 * k + 1) n,
          show (2 * k + 1 + 1 + 1 + 1) * (k + 1) = (2 * k + 1 + 1) * (k + 2) by lib_nat, ← TwoPower.pwpow,
          ZMod.unit_pow (2 * k + 1) hpr _ hs0, TwoPower.pwone]
        rfl
      rw [hD', FinCubic.psi_scale (Fq2Inst.ffq k hpr n hn) _ _ ht0, hchi, CubicSuper2.cubic_super2 k c hc hpr n hn]
      show 1 * Int.ofNat (2 * (2 * k + 1 + 1 + 1)) = _
      exact LibInt.intR.one_mul _

/-- **Sextic twists at inert p ≡ 2 (mod 3): −ψ_{p²}(D) = ψ_p(D)² − 2p.** -/
theorem sextic_hd_inert (k c : Nat) (hc : 2 * k + 1 + 1 + 1 + 1 = 3 * c) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1))
    (n : Zp (2 * k + 1 + 1)) (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    -FinCubic.psi (Fq2Inst.ffq k hpr n hn) (D, (ring (2 * k + 1 + 1)).zero) = CubicJacob.psi k D * CubicJacob.psi k D + -2 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  match CubicSuper2.e_form k c hc with
  | ⟨e, he⟩ =>
    rw [psi_twist_inert k c hc hpr n hn D hD, SexticFamily.psi_inert k hpr e he D]
    show -(2 * Int.ofNat (2 * k + 1 + 1 + 1)) = _
    lib_int

end SexticInert

#print axioms SexticInert.psi_twist_inert
#print axioms SexticInert.sextic_hd_inert

At split \(p=6t+7\), with \(X=-\psi _{p^2}(D)\), \(Y=\psi _p(D)\), \(X'=Y^2-2p\):

\[ \begin {aligned} X&\equiv X'\ (p)\quad (\textstyle \sum (x^3+D)^h=-\tbinom {h}{(q-1)/3}D^{(q-1)/6},\ D^{(p^2-1)/6}=u^{p+1}=u^2,\ \text {Lucas}),\\ X&\equiv X'\ (3)\quad (\psi \equiv \chi \ (3)),\qquad X\equiv X'\ (2)\quad (\psi \equiv 1+\#\{x^3=-D\}\ (2),\ \text {equal counts over }\mathbb F_p,\mathbb F_{p^2}),\\ X^2+3B^2&=4p^2,\quad X'^2+3(Yb)^2=4p^2\quad (\psi (D)^2+3B^2=4q\ \text {for every }D),\\ (X-X')^2+(X+X')^2+6B^2+6(Yb)^2&=16p^2,\ X-X'=6ps\ \Rightarrow \ s=0 . \end {aligned} \]

Listing 114: lang/FinCubicNorm.lean

lang/FinCubicNorm.lean

/-
  lang/FinCubicNorm — T43, cycle 115: every sextic twist over any 𝔽_q with q ≡ 1 (mod 3): ψ(D) ≡ χ(D) (mod 3) and
  ψ(D)² + 3B² = 4q.

  ψ(D) = Σ_x χ(x³ + D), g a primitive root, A_i = ψ(g^i):
    psi_mod3        ψ(D) = χ(D) + 3T                                   (cube_sum_ff with G(y) = χ(y + D))
    rels            A₀ − A₁ + A₂ = 0,  A₀² + A₁² + A₂² = 6q              (as in FinCubicExact)
    class_sq        D ≠ 0 ⇒ ψ(D)² ∈ {A₀², A₁², A₂²}                       (D = g^i t³, psi_sq_const)
    norm_twist      D ≠ 0 ⇒ ∃ B, ψ(D)² + 3B² = 4q                         (3A_i² + (A_j ± 2A_k)² = 12q, A_i ≡ χ(g^i) mod 3)
  So the Frobenius trace of every curve y² = x³ + D over 𝔽_q is the trace of an Eisenstein integer of norm q, and
  ψ(D)² ≤ 4q (Hasse's bound for the whole sextic family over every such field).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.FinCubicExact
import lang.EveryPrime3

namespace FinCubicNorm

open FTAChain YMChain FiniteRecChain FinField FinConic FinMoment FinCubic FinDLog OrdMul LibInt

variable {R : Type} [DecidableEq R] (F : FF R)

theorem psi_mod3 (N : Nat) (hN : 2 * F.h = 3 * N) (a : R) : ∃ T : Int, psi F a = chi F a + 3 * T := by
  match FinCyclic.primitive_root_ff F with
  | ⟨g, hg⟩ =>
    refine ⟨CubicSign.sumR (fun k => chi F (F.K.add (FinCubicTrace.cb F (Frobenius.pw F.K g k)) a)) N, ?_⟩
    have h := FinCubicTrace.cube_sum_ff F g hg N hN (fun y => chi F (F.K.add y a))
    show sumK Z (fun x => chi F (F.K.add (cube F x) a)) F.els = _
    rw [show (fun x => chi F (F.K.add (cube F x) a)) = (fun x => chi F (F.K.add (FinCubicTrace.cb F x) a)) from rfl, h,
      F.K.zero_add]

theorem rels (N : Nat) (hN : 2 * F.h = 3 * N) (g : R) (hg : Ord F.K g (2 * F.h)) :
    psi F F.K.one + -1 * psi F g + psi F (F.K.mul g g) = 0 ∧
    psi F F.K.one * psi F F.K.one + psi F g * psi F g + psi F (F.K.mul g g) * psi F (F.K.mul g g) = 6 * PP F := by
  have hN0 : N ≠ 0 := fun e => by
    rw [e] at hN
    have := FinCyclic.h_pos F
    have h2 : 2 * 1 ≤ 2 * F.h := Nat.mul_le_mul_left 2 this
    rw [hN] at h2; exact Nat.not_succ_le_zero 1 h2
  match FinCyclic.split_pow_ff F 1 0 ⟨N, by rw [hN]; lib_nat⟩ with
  | ⟨w, hw3', hw1'⟩ =>
    have hw1 : w ≠ F.K.one := fun e => hw1' (by rw [e]; exact TwoPower.pwone F.K _)
    have hw3 : cube F w = F.K.one := by
      rw [← hw3']; show _ = F.K.mul (F.K.mul (F.K.mul F.K.one w) w) w; unfold cube; lib_ring
    have hg0 : g ≠ F.K.zero := FinDLog.g_ne_zero F g hg
    have hP : PP F = 3 * Int.ofNat N + 1 := by
      show Int.ofNat (2 * F.h + 1) = _
      rw [hN]; rfl
    have hpw : pows F g (2 * F.h) = pows F g (3 * N) := congrArg (pows F g) hN
    have M := moment_c F w hw1 hw3
    rw [sum_split_ff F Z g hg _ F.comp, hpw,
      FinCubicTrace.group3_ff F g hg0 (fun a => psi F a * psi F a) (fun a t ht => psi_sq_const F a t ht) N,
      FinCubicExact.psi_zero F] at M
    have T := twisted F
    rw [sum_split_ff F Z g hg _ F.comp, hpw,
      FinCubicTrace.group3_ff F g hg0 (fun a => chi F a * psi F a) (fun a t ht => chi_psi_const F a t ht) N,
      chi_zero, FinConic.chi_one F, FinCubicExact.chi_g F g hg, FinCubicExact.chi_gg F g hg0] at T
    constructor
    · apply FinCubicExact.cancel_N N hN0
      rw [show Int.ofNat N * 0 = 0 by lib_int, ← T]
      show _ = 0 * psi F F.K.zero + _
      lib_int
    · apply FinCubicExact.cancel_N N hN0
      have : Int.ofNat N * (6 * PP F) = 2 * PP F * (PP F + -1) := by rw [hP]; lib_int
      rw [this, ← M]
      show Int.ofNat N * _ = (0 * 0 : Int) + Int.ofNat N * _
      lib_int

theorem class_sq (g : R) (hg : Ord F.K g (2 * F.h)) (D : R) (hD : D ≠ F.K.zero) :
    psi F D * psi F D = psi F F.K.one * psi F F.K.one ∨ psi F D * psi F D = psi F g * psi F g ∨
      psi F D * psi F D = psi F (F.K.mul g g) * psi F (F.K.mul g g) := by
  have hg0 := FinDLog.g_ne_zero F g hg
  match mem_pows F g D _ (every_unit_ff F g hg D hD) with
  | ⟨k, _, hk⟩ =>
    have step : ∀ i j, k = 3 * j + i → psi F D * psi F D = psi F (Frobenius.pw F.K g i) * psi F (Frobenius.pw F.K g i) := by
      intro i j e
      rw [hk, e, FinCubicTrace.pw_split_ff F g j i]
      exact psi_sq_const F _ _ (pw_ne_zero F g hg0 j)
    cases EveryPrime3.nat_mod3 k with
    | inl h => match h with
      | ⟨j, hj⟩ => exact Or.inl ((step 0 j hj).trans (by rfl))
    | inr h =>
      cases h with
      | inl h => match h with
        | ⟨j, hj⟩ => exact Or.inr (Or.inl ((step 1 j hj).trans (by rw [show Frobenius.pw F.K g 1 = g from F.K.one_mul g])))
      | inr h => match h with
        | ⟨j, hj⟩ =>
          exact Or.inr (Or.inr ((step 2 j hj).trans (by
            rw [show Frobenius.pw F.K g 2 = F.K.mul g g by show F.K.mul (F.K.mul F.K.one g) g = _; rw [F.K.one_mul]])))

theorem norm3 (X B A0 A2 P : Int)
    (key : A0 * A0 + A0 * A2 + A2 * A2 = 3 * P) (hX : 3 * (X * X + 3 * (B * B)) = 4 * (A0 * A0 + A0 * A2 + A2 * A2)) :
    X * X + 3 * (B * B) = 4 * P := by
  apply FinCubicExact.cancel_N 3 (fun e => Nat.noConfusion e)
  show (3 : Int) * _ = 3 * _
  rw [hX, key]; lib_int

theorem norm_twist (N : Nat) (hN : 2 * F.h = 3 * N) (D : R) (hD : D ≠ F.K.zero) :
    ∃ B : Int, psi F D * psi F D + 3 * (B * B) = 4 * PP F := by
  match FinCyclic.primitive_root_ff F with
  | ⟨g, hg⟩ =>
    have hg0 := FinDLog.g_ne_zero F g hg
    have hcl := class_sq F g hg D hD
    match rels F N hN g hg, psi_mod3 F N hN F.K.one, psi_mod3 F N hN (F.K.mul g g) with
    | ⟨S1, S2⟩, ⟨T0, h0⟩, ⟨T2, h2⟩ =>
      rw [FinConic.chi_one F] at h0
      rw [FinCubicExact.chi_gg F g hg0] at h2
      have h1 : psi F g = psi F F.K.one + psi F (F.K.mul g g) := by
        calc psi F g = psi F g + (psi F F.K.one + -1 * psi F g + psi F (F.K.mul g g)) := by rw [S1]; lib_int
          _ = _ := by lib_int
      rw [h1] at S2 hcl
      generalize psi F D = X at hcl ⊢
      generalize psi F F.K.one = A0 at h0 S2 hcl
      generalize psi F (F.K.mul g g) = A2 at h2 S2 hcl
      have key : A0 * A0 + A0 * A2 + A2 * A2 = 3 * PP F := by
        apply FinPrimary.two_cancel
        rw [show 2 * (3 * PP F) = 6 * PP F by lib_int, ← S2]; lib_int
      cases hcl with
      | inl e =>
        exact ⟨1 + T0 + 2 * T2, norm3 X _ A0 A2 (PP F) key (by rw [e, h0, h2]; lib_int)⟩
      | inr e =>
        cases e with
        | inl e => exact ⟨T0 - T2, norm3 X _ A0 A2 (PP F) key (by rw [e, h0, h2]; lib_int)⟩
        | inr e => exact ⟨1 + 2 * T0 + T2, norm3 X _ A0 A2 (PP F) key (by rw [e, h0, h2]; lib_int)⟩

end FinCubicNorm

#print axioms FinCubicNorm.psi_mod3
#print axioms FinCubicNorm.rels
#print axioms FinCubicNorm.class_sq
#print axioms FinCubicNorm.norm3
#print axioms FinCubicNorm.norm_twist

Listing 115: lang/FinHasseD.lean

lang/FinHasseD.lean

/-
  lang/FinHasseD — T43, cycle 116: the Hasse invariant of every sextic twist y² = x³ + D over 𝔽_q, q ≡ 1 (mod 6).

  For an FF with h = (q − 1)/2 = 3k + 3:
    ps_zero, ps_low3, ps_top3, ps_high3   Σ_x x^e = 0 (e = 0 and 1 ≤ e ≤ q − 2), −1 (e = q − 1), 0 (q ≤ e ≤ 2q − 3)
    T3F, T3_cons, T3_zero, skip3_many     one binomial coefficient at a time (port of HasseEisen)
    fin_hasseD     Σ_x (x³ + D)^h = −C(3k + 3, 2k + 2) D^(k+1)   in the field  (D^((q−1)/6))
  Over 𝔽_{p²} (p ≡ 1 mod 3) this is the input, with Lucas, for a(𝔽_{p²}) = a_p² − 2p for y² = x³ + 1.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.FinHasse3

namespace FinHasseD

open FTAChain YMChain Frobenius FinField HasseCM

variable {R : Type} [DecidableEq R] (F : FF R)

def ev3F (D x : R) (cs : List Nat) : R := ev F.K (pw F.K x 3) D cs

def T3F (D : R) (e : Nat) (cs : List Nat) : R :=
  sumK F.K (fun x => F.K.mul (pw F.K x e) (ev3F F D x cs)) F.els

theorem T3_cons (e c : Nat) (cs : List Nat) :
    T3F F D e (c :: cs) = F.K.add (F.K.mul (nsm F.K c (pw F.K D cs.length)) (FinHasse.psF F e)) (T3F F D (e + 3) cs) := by
  have pt : ∀ x : R, F.K.mul (pw F.K x e) (ev3F F D x (c :: cs))
      = F.K.add (F.K.mul (nsm F.K c (pw F.K D cs.length)) (pw F.K x e)) (F.K.mul (pw F.K x (e + 3)) (ev3F F D x cs)) := fun x => by
    show F.K.mul (pw F.K x e) (F.K.add (nsm F.K c (pw F.K D cs.length)) (F.K.mul (pw F.K x 3) (ev3F F D x cs))) = _
    rw [F.K.distrib, F.K.mul_comm (pw F.K x e) (nsm F.K c _), ← F.K.mul_assoc, ← pw_add' F.K x e 3]
  show sumK F.K _ _ = _
  rw [sumK_ext F.K _ _ pt, sumK_add, ← mul_sumK]
  rfl

theorem T3_nil (e : Nat) : T3F F D e [] = F.K.zero := by
  show sumK F.K (fun x => F.K.mul (pw F.K x e) F.K.zero) _ = _
  rw [sumK_ext F.K _ (fun _ => F.K.zero) (fun x => DetChain.mul_zero F.K _), FinHasse.sumK_zero' F]

theorem idx3' (e i : Nat) : e + 3 + 3 * i = e + 3 * (i + 1) := by lib_nat

theorem T3_zero : ∀ (cs : List Nat) (e : Nat), (∀ i, i < cs.length → FinHasse.psF F (e + 3 * i) = F.K.zero) → T3F F D e cs = F.K.zero
  | [], e, _ => T3_nil F e
  | c :: cs, e, h => by
    have h0 : FinHasse.psF F e = F.K.zero := h 0 (Nat.zero_lt_succ _)
    rw [T3_cons, h0, DetChain.mul_zero, F.K.zero_add]
    exact T3_zero cs (e + 3) (fun i hi => by rw [idx3']; exact h (i + 1) (Nat.succ_lt_succ hi))

theorem skip3_many (n m : Nat) : ∀ (i j e : Nat), (∀ i', i' < i → FinHasse.psF F (e + 3 * i') = F.K.zero) →
    T3F F D e (Frobenius.listC n j (i + m)) = T3F F D (e + 3 * i) (Frobenius.listC n (j + i) m)
  | 0, j, e, _ => by rw [Nat.zero_add]; rfl
  | i + 1, j, e, h => by
    rw [Nat.succ_add]
    show T3F F D e (Frobenius.C n j :: Frobenius.listC n (j + 1) (i + m)) = _
    have h0 : FinHasse.psF F e = F.K.zero := h 0 (Nat.zero_lt_succ _)
    rw [T3_cons, h0, DetChain.mul_zero, F.K.zero_add,
      skip3_many n m i (j + 1) (e + 3) (fun i' hi' => by rw [idx3']; exact h (i' + 1) (Nat.succ_lt_succ hi')),
      idx3', Nat.add_assoc j 1 i, Nat.add_comm 1 i]

theorem fin_hasseD (D : R) (k : Nat) (hk : F.h = 3 * k + 3) :
    sumK F.K (fun x => pw F.K (F.K.add (pw F.K x 3) D) (3 * k + 3)) F.els
      = F.K.neg (nsm F.K (Frobenius.C (3 * k + 3) (2 * k + 2)) (pw F.K D (k + 1))) := by
  have eA : sumK F.K (fun x => pw F.K (F.K.add (pw F.K x 3) D) (3 * k + 3)) F.els
      = T3F F D 0 (Frobenius.row (3 * k + 3)) :=
    sumK_ext F.K _ _ (fun x => by
      show _ = F.K.mul F.K.one (ev3F F D x (Frobenius.row (3 * k + 3)))
      rw [F.K.one_mul, ev3F, ev_row]) _
  rw [eA, Frobenius.row_eq, show 3 * k + 3 + 1 = (2 * k + 2) + (k + 2) by lib_nat,
    skip3_many F (3 * k + 3) (k + 2) (2 * k + 2) 0 0 (fun i' hi' => by
      cases i' with
      | zero => exact FinHasse3.ps_zero F
      | succ i'' =>
        apply FinHasse3.ps_low3 F k hk
        · rw [show 0 + 3 * (i'' + 1) = 1 + (3 * i'' + 2) by lib_nat]; exact Nat.le_add_right 1 _
        · have hi : i'' ≤ 2 * k := Nat.le_of_succ_le_succ (Nat.le_of_lt_succ hi')
          have a : 0 + 3 * (i'' + 1) ≤ 0 + 3 * (2 * k + 1) := Nat.add_le_add_left (Nat.mul_le_mul_left 3 (Nat.succ_le_succ hi)) 0
          exact Nat.le_trans a (by rw [show 0 + 3 * (2 * k + 1) = 6 * k + 3 by lib_nat]; exact Nat.le_succ_of_le (Nat.le_succ _)))]
  rw [show 0 + (2 * k + 2) = 2 * k + 2 by lib_nat]
  show T3F F D (0 + 3 * (2 * k + 2)) (Frobenius.C (3 * k + 3) (2 * k + 2) :: Frobenius.listC (3 * k + 3) (2 * k + 2 + 1) (k + 1)) = _
  rw [T3_cons, show 0 + 3 * (2 * k + 2) = (6 * k + 5) + 1 by lib_nat, FinHasse3.ps_top3 F k hk,
    T3_zero F _ _ (fun i hi => by
      rw [Frobenius.len_listC] at hi
      rw [show (6 * k + 5) + 1 + 3 + 3 * i = (6 * k + 5) + 1 + (3 * i + 3) by lib_nat]
      apply FinHasse3.ps_high3 F k hk
      · exact Nat.le_add_left 1 _
      · have a : 3 * i + 3 ≤ 3 * k + 3 := Nat.add_le_add_right (Nat.mul_le_mul_left 3 (Nat.le_of_lt_succ hi)) 3
        exact Nat.le_trans a (by rw [show 6 * k + 5 = (3 * k + 3) + (3 * k + 2) by lib_nat]; exact Nat.le_add_right _ _)),
    DetChain.add_zero', mul_neg_r F.K, F.K.mul_comm _ F.K.one, F.K.one_mul, Frobenius.len_listC]

end FinHasseD

#print axioms FinHasseD.T3_cons
#print axioms FinHasseD.T3_nil
#print axioms FinHasseD.idx3'
#print axioms FinHasseD.T3_zero
#print axioms FinHasseD.skip3_many
#print axioms FinHasseD.fin_hasseD

Listing 116: lang/SplitHasseD.lean

lang/SplitHasseD.lean

/-
  lang/SplitHasseD — T43, cycle 117: every sextic twist y² = x³ + D at split p = 6t + 7: ψ_{p²}(D) ≡ −ψ_p(D)² (mod p).

  Over 𝔽_{p²}: ψ ≡ −C((p²−1)/2, (p²−1)/3) D^((p²−1)/6) (FinHasseD), Lucas gives C ≡ C(3t + 3, 2t + 2)², and with u = D^((p−1)/6),
  D^((p²−1)/6) = u^(p+1) = u² (Fermat). Over 𝔽_p (the FF instance ffp): ψ ≡ −C(3t + 3, 2t + 2) u. Hence
    split_hasseD     castZ ψ_{p²}(D) = −(castZ ψ_p(D))²   in 𝔽_p.

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.FinHasseD
import lang.SplitHasse3

namespace SplitHasseD

open FTAChain ZMod YMChain FiniteRecChain LibRing Frobenius

theorem hK (t : Nat) : (2 * (3 * t + 2) + 1 + 1 + 1 + 1) * (3 * t + 2 + 1) = 3 * (6 * t * t + 14 * t + 7) + 3 := by lib_nat
theorem iK (t : Nat) : 6 * t * t + 14 * t + 7 + 1 = (t + 1) * ((2 * (3 * t + 2) + 1 + 1 + 1) + 1) := by lib_nat

theorem cast_cube {R : Type} [DecidableEq R] (F : FinField.FF R) (a : R) :
    sumK LibInt.intR (fun x => FinField.chi F (F.K.add (FinCubic.cube F x) a)) F.els
      = sumK LibInt.intR (fun x => FinField.chi F (F.K.add (Frobenius.pw F.K x 3) a)) F.els := by
  apply sumK_ext; intro x
  rw [show FinCubic.cube F x = Frobenius.pw F.K x 3 by show F.K.mul x (F.K.mul x x) = F.K.mul (F.K.mul (F.K.mul F.K.one x) x) x; lib_ring]

theorem split_hasseD (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) (n : Zp (2 * (3 * t + 2) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) n (3 * t + 2 + 1) = (ring (2 * (3 * t + 2) + 1 + 1)).neg (ring (2 * (3 * t + 2) + 1 + 1)).one) (D : Zp (2 * (3 * t + 2) + 1 + 1)) :
    castZ (ring (2 * (3 * t + 2) + 1 + 1)) (FinCubic.psi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero))
      = (ring (2 * (3 * t + 2) + 1 + 1)).neg ((ring (2 * (3 * t + 2) + 1 + 1)).mul (castZ (ring (2 * (3 * t + 2) + 1 + 1)) (FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D)) (castZ (ring (2 * (3 * t + 2) + 1 + 1)) (FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D))) := by
  -- 𝔽_p side
  have h1 : castZ (ring (2 * (3 * t + 2) + 1 + 1)) (FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D) = (ring (2 * (3 * t + 2) + 1 + 1)).neg (nsm (ring (2 * (3 * t + 2) + 1 + 1)) (C (3 * t + 3) (2 * t + 2)) (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) D (t + 1))) := by
    show castZ (FinField.ffp (3 * t + 2) hpr).K (sumK LibInt.intR (fun x => FinField.chi (FinField.ffp (3 * t + 2) hpr) ((FinField.ffp (3 * t + 2) hpr).K.add (FinCubic.cube (FinField.ffp (3 * t + 2) hpr) x) D)) (FinField.ffp (3 * t + 2) hpr).els) = _
    rw [cast_cube, FinCast.cast_sum_ff]
    exact FinHasseD.fin_hasseD (FinField.ffp (3 * t + 2) hpr) D t rfl
  -- 𝔽_{p²} side
  have hk : (Fq2Inst.ffq (3 * t + 2) hpr n hn).h = 3 * (6 * t * t + 14 * t + 7) + 3 := SplitHasse3.hF3 t
  have h2 : castZ (Fq2Inst.ffq (3 * t + 2) hpr n hn).K (FinCubic.psi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero))
      = (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.neg ((Fq2Inst.ffq (3 * t + 2) hpr n hn).K.mul (nsm (Fq2Inst.ffq (3 * t + 2) hpr n hn).K (C (3 * t + 3) (2 * t + 2) * C (3 * t + 3) (2 * t + 2)) (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.one)
          (Frobenius.pw (Fq2Inst.ffq (3 * t + 2) hpr n hn).K (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) (6 * t * t + 14 * t + 7 + 1))) := by
    show castZ (Fq2Inst.ffq (3 * t + 2) hpr n hn).K (sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq (3 * t + 2) hpr n hn) ((Fq2Inst.ffq (3 * t + 2) hpr n hn).K.add (FinCubic.cube (Fq2Inst.ffq (3 * t + 2) hpr n hn) x) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero))) (Fq2Inst.ffq (3 * t + 2) hpr n hn).els) = _
    rw [cast_cube, FinCast.cast_sum_ff, hk, FinHasseD.fin_hasseD (Fq2Inst.ffq (3 * t + 2) hpr n hn) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) (6 * t * t + 14 * t + 7) hk,
      nsm_eq_mul, SplitHasse3.ia3 t, SplitHasse3.ic3 t,
      Lucas.lucas (Fq2Inst.ffq (3 * t + 2) hpr n hn).K (2 * (3 * t + 2) + 1) hpr (QuadFrob.char_quad (2 * (3 * t + 2) + 1) n) (3 * t + 3) (2 * t + 2)
        (SplitHasse3.lt3 t) (SplitHasse3.lt3' t)]
  have hu : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) D (6 * t * t + 14 * t + 7 + 1)
      = (ring (2 * (3 * t + 2) + 1 + 1)).mul (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) D (t + 1)) (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) D (t + 1)) := by
    rw [iK t, ← TwoPower.pwpow]
    show (ring (2 * (3 * t + 2) + 1 + 1)).mul (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) D (t + 1)) (2 * (3 * t + 2) + 1 + 1 + 1)) (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) D (t + 1)) = _
    rw [ZMod.fermat (2 * (3 * t + 2) + 1) hpr]
  have h2p := congrArg Prod.fst h2
  rw [show castZ (Fq2Inst.ffq (3 * t + 2) hpr n hn).K = castZ (Quad.quad (ring (2 * (3 * t + 2) + 1 + 1)) n) from rfl, FinCast.castZ_quad] at h2p
  refine h2p.trans ?_
  show (ring (2 * (3 * t + 2) + 1 + 1)).neg ((Fq2.G2 (2 * (3 * t + 2) + 1) n).mul (nsm (Fq2.G2 (2 * (3 * t + 2) + 1) n) _ (Fq2.G2 (2 * (3 * t + 2) + 1) n).one) (Frobenius.pw (Fq2.G2 (2 * (3 * t + 2) + 1) n) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) _)).1 = _
  rw [QuadFrob.pw_embed (2 * (3 * t + 2) + 1) n, show nsm (Fq2.G2 (2 * (3 * t + 2) + 1) n) (C (3 * t + 3) (2 * t + 2) * C (3 * t + 3) (2 * t + 2)) (Fq2.G2 (2 * (3 * t + 2) + 1) n).one
      = (nsm (ring (2 * (3 * t + 2) + 1 + 1)) (C (3 * t + 3) (2 * t + 2) * C (3 * t + 3) (2 * t + 2)) (ring (2 * (3 * t + 2) + 1 + 1)).one, nsm (ring (2 * (3 * t + 2) + 1 + 1)) (C (3 * t + 3) (2 * t + 2) * C (3 * t + 3) (2 * t + 2)) (ring (2 * (3 * t + 2) + 1 + 1)).zero)
      from QuadFrob.nsm_quad (2 * (3 * t + 2) + 1) n _ _ _]
  rw [show nsm (ring (2 * (3 * t + 2) + 1 + 1)) (C (3 * t + 3) (2 * t + 2) * C (3 * t + 3) (2 * t + 2)) (ring (2 * (3 * t + 2) + 1 + 1)).zero = (ring (2 * (3 * t + 2) + 1 + 1)).zero from QuadFrob.nsm_zero' (2 * (3 * t + 2) + 1) _,
    Fq2.embed_mul, hu, h1]
  show (ring (2 * (3 * t + 2) + 1 + 1)).neg ((ring (2 * (3 * t + 2) + 1 + 1)).mul (nsm (ring (2 * (3 * t + 2) + 1 + 1)) ((C (3 * t + 3) (2 * t + 2)) * (C (3 * t + 3) (2 * t + 2))) (ring (2 * (3 * t + 2) + 1 + 1)).one) ((ring (2 * (3 * t + 2) + 1 + 1)).mul (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) D (t + 1)) (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) D (t + 1)))) = _
  rw [nsm_mul_nat, nsm_eq_mul (ring (2 * (3 * t + 2) + 1 + 1)) (C (3 * t + 3) (2 * t + 2)) (nsm (ring (2 * (3 * t + 2) + 1 + 1)) (C (3 * t + 3) (2 * t + 2)) (ring (2 * (3 * t + 2) + 1 + 1)).one), nsm_eq_mul (ring (2 * (3 * t + 2) + 1 + 1)) (C (3 * t + 3) (2 * t + 2)) (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) D (t + 1))]
  lib_ring

end SplitHasseD

#print axioms SplitHasseD.hK
#print axioms SplitHasseD.iK
#print axioms SplitHasseD.cast_cube
#print axioms SplitHasseD.split_hasseD

Listing 117: lang/CubeCount.lean

lang/CubeCount.lean

/-
  lang/CubeCount — T43, cycle 118: the parity of ψ(a) = Σ_x χ(x³ + a) through the number of cube roots of −a.

  For every FF with a primitive cube root of unity w:
    psi_parity    ψ(a) = q − R(a) − 2N(a),  R(a) = #{x : x³ = −a}                (χ = 1 − δ₀ − 2·[χ = −1])
    R_three       −a = x₀³, x₀ ≠ 0 ⇒ R(a) = 3                                    (FinCubic.count_x)
    R_zero        −a not a cube ⇒ R(a) = 0
  So ψ(a) ≡ 1 + R(a) (mod 2): ψ(a) is even exactly when −a is a nonzero cube. Input for the integer finish of the sextic
  Hasse–Davenport (ψ over 𝔽_p and over 𝔽_{p²} have the same parity).

  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.FinCubicNorm

namespace CubeCount

open FTAChain YMChain FiniteRecChain FinField FinConic FinMoment FinCubic LibInt

variable {R : Type} [DecidableEq R] (F : FF R)

def Rc (a : R) : Int := sumK Z (fun x => delta Z (cube F x) (F.K.neg a)) F.els

theorem delta_shift (x a : R) : delta Z (F.K.add (cube F x) a) F.K.zero = delta Z (cube F x) (F.K.neg a) := by
  by_cases h : cube F x = F.K.neg a
  · rw [FinMoment.delta_one _ _ h, FinMoment.delta_one _ _ (by rw [h]; exact DetChain.add_neg' F.K a)]
  · rw [FinMoment.delta_zero _ _ h, FinMoment.delta_zero _ _ (fun e => h (DetChain.neg_unique F.K _ _ e))]

theorem psi_parity (a : R) :
    psi F a = PP F + -1 * Rc F a + -2 * sumK Z (fun x => FinCubicTrace.nq F (F.K.add (cube F x) a)) F.els := by
  show sumK Z (fun x => chi F (F.K.add (cube F x) a)) F.els = _
  have pt : ∀ x, chi F (F.K.add (cube F x) a) = Z.add (Z.add 1 (Z.mul (-1) (delta Z (cube F x) (F.K.neg a))))
      (Z.mul (-2) (FinCubicTrace.nq F (F.K.add (cube F x) a))) := fun x => by
    rw [FinCubicTrace.chi_split F, delta_shift]; rfl
  rw [sumK_ext Z _ _ pt, sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, FinMoment.sum_one F]
  rfl

theorem R_three (w : R) (hw1 : w ≠ F.K.one) (hw3 : cube F w = F.K.one) (a x0 : R) (hx0 : x0 ≠ F.K.zero)
    (h : cube F x0 = F.K.neg a) : Rc F a = 3 := by
  unfold Rc
  rw [← h]
  have := count_x F w hw1 hw3 x0
  rw [FinMoment.delta_zero _ _ hx0] at this
  rw [sumK_ext Z _ _ (fun y => FiniteRecChain.delta_symm Z (cube F y) (cube F x0)), this]
  rfl

theorem R_zero (a : R) (h : ∀ x, cube F x ≠ F.K.neg a) : Rc F a = 0 := by
  unfold Rc
  rw [sumK_ext Z _ (fun _ => (0 : Int)) (fun x => FinMoment.delta_zero _ _ (h x))]
  exact FinMoment.zsum _

end CubeCount

#print axioms CubeCount.delta_shift
#print axioms CubeCount.psi_parity
#print axioms CubeCount.R_three
#print axioms CubeCount.R_zero

Listing 118: lang/CubeLift.lean

lang/CubeLift.lean

/-
  lang/CubeLift — T43, cycle 119: cubes at a split prime p = 6t + 7: in 𝔽_p and in 𝔽_{p²}.

    cubic_euler     c ∈ 𝔽_p^×, c^((p−1)/3) = 1 ⇒ c = s³ for some s ∈ 𝔽_p                 (primitive root of 𝔽_p, Ord)
    lift_cube       (c, 0) = z³ in 𝔽_{p²} ⇒ c^((p−1)/3) = 1                              (v = c^((p−1)/3): v³ = 1, v^(p+1) = v² = 1)
    cube_down       (c, 0) is a cube in 𝔽_{p²} ⇒ c is a cube in 𝔽_p
  So −D has a cube root in 𝔽_{p²} exactly when it has one in 𝔽_p, and ψ_{𝔽_{p²}}(D) ≡ ψ_{𝔽_p}(D) (mod 2) (CubeCount).

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CubeCount
import lang.SplitHasseD

namespace CubeLift

open FTAChain ZMod YMChain FiniteRecChain OrdMul

theorem cubic_euler (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) (c : Zp (2 * (3 * t + 2) + 1 + 1))
    (hc : c ≠ (ring (2 * (3 * t + 2) + 1 + 1)).zero) (h : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) c (2 * t + 2) = (ring (2 * (3 * t + 2) + 1 + 1)).one) :
    ∃ s, s ≠ (ring (2 * (3 * t + 2) + 1 + 1)).zero ∧ (ring (2 * (3 * t + 2) + 1 + 1)).mul s ((ring (2 * (3 * t + 2) + 1 + 1)).mul s s) = c := by
  match FinCyclic.primitive_root_ff (FinField.ffp (3 * t + 2) hpr) with
  | ⟨g, hg⟩ =>
    have hg0 := FinDLog.g_ne_zero (FinField.ffp (3 * t + 2) hpr) g hg
    match FinDLog.mem_pows (FinField.ffp (3 * t + 2) hpr) g c _ (FinDLog.every_unit_ff (FinField.ffp (3 * t + 2) hpr) g hg c hc) with
    | ⟨k, _, hk⟩ =>
      have hk' : c = Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) g k := hk
      have hdiv : (2 * (FinField.ffp (3 * t + 2) hpr).h) ∣ k * (2 * t + 2) := by
        apply hg.2
        show Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) g (k * (2 * t + 2)) = (ring (2 * (3 * t + 2) + 1 + 1)).one
        rw [← TwoPower.pwpow, ← hk']; exact h
      match hdiv with
      | ⟨m', hm⟩ =>
        have hk3 : k = 3 * m' := by
          apply Nat.eq_of_mul_eq_mul_right (Nat.zero_lt_succ (2 * t + 1))
          rw [hm]; show 2 * (3 * t + 2 + 1) * m' = 3 * m' * (2 * t + 1 + 1); lib_nat
        refine ⟨Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) g m', FinDLog.pw_ne_zero (FinField.ffp (3 * t + 2) hpr) g hg0 m', ?_⟩
        rw [hk', hk3, show 3 * m' = m' * 3 by lib_nat, ← TwoPower.pwpow]
        show _ = (ring (2 * (3 * t + 2) + 1 + 1)).mul ((ring (2 * (3 * t + 2) + 1 + 1)).mul ((ring (2 * (3 * t + 2) + 1 + 1)).mul (ring (2 * (3 * t + 2) + 1 + 1)).one (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) g m')) (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) g m')) (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) g m')
        lib_ring

theorem lift_cube (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) (n : Zp (2 * (3 * t + 2) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) n (3 * t + 2 + 1) = (ring (2 * (3 * t + 2) + 1 + 1)).neg (ring (2 * (3 * t + 2) + 1 + 1)).one) (c : Zp (2 * (3 * t + 2) + 1 + 1)) (hc : c ≠ (ring (2 * (3 * t + 2) + 1 + 1)).zero)
    (z : Zp (2 * (3 * t + 2) + 1 + 1) × Zp (2 * (3 * t + 2) + 1 + 1)) (hz : (Fq2.G2 (2 * (3 * t + 2) + 1) n).mul z ((Fq2.G2 (2 * (3 * t + 2) + 1) n).mul z z) = (c, (ring (2 * (3 * t + 2) + 1 + 1)).zero)) :
    Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) c (2 * t + 2) = (ring (2 * (3 * t + 2) + 1 + 1)).one := by
  have hz0 : z ≠ (Fq2.G2 (2 * (3 * t + 2) + 1) n).zero := fun e => hc (by
    have h' : (c, (ring (2 * (3 * t + 2) + 1 + 1)).zero) = (Fq2.G2 (2 * (3 * t + 2) + 1) n).zero := by
      rw [← hz, show z = (Fq2.G2 (2 * (3 * t + 2) + 1) n).zero from e]; exact DetChain.zero_mul' (Fq2.G2 (2 * (3 * t + 2) + 1) n) _
    exact congrArg Prod.fst h')
  have hf := FinCyclic.fermat_ff (Fq2Inst.ffq (3 * t + 2) hpr n hn) z hz0
  have h2h : 2 * (Fq2Inst.ffq (3 * t + 2) hpr n hn).h = 3 * ((2 * t + 2) * (2 * (3 * t + 2) + 1 + 1 + 1 + 1)) := by
    rw [CubicSuper2.h_eq (3 * t + 2) hpr n hn]; lib_nat
  rw [h2h, ← TwoPower.pwpow,
    show Frobenius.pw (Fq2Inst.ffq (3 * t + 2) hpr n hn).K z 3 = (c, (ring (2 * (3 * t + 2) + 1 + 1)).zero) by
      show (Fq2.G2 (2 * (3 * t + 2) + 1) n).mul ((Fq2.G2 (2 * (3 * t + 2) + 1) n).mul ((Fq2.G2 (2 * (3 * t + 2) + 1) n).mul (Fq2.G2 (2 * (3 * t + 2) + 1) n).one z) z) z = _
      rw [← hz]; show _ = (Fq2.G2 (2 * (3 * t + 2) + 1) n).mul z ((Fq2.G2 (2 * (3 * t + 2) + 1) n).mul z z); rw [(Fq2.G2 (2 * (3 * t + 2) + 1) n).one_mul, (Fq2.G2 (2 * (3 * t + 2) + 1) n).mul_assoc],
    show Frobenius.pw (Fq2Inst.ffq (3 * t + 2) hpr n hn).K (c, (ring (2 * (3 * t + 2) + 1 + 1)).zero) _ = Frobenius.pw (Fq2.G2 (2 * (3 * t + 2) + 1) n) (c, (ring (2 * (3 * t + 2) + 1 + 1)).zero) _ from rfl,
    QuadFrob.pw_embed (2 * (3 * t + 2) + 1) n, ← TwoPower.pwpow] at hf
  have hv := congrArg Prod.fst hf
  generalize hvdef : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) c (2 * t + 2) = v at hv
  have hv2 : (ring (2 * (3 * t + 2) + 1 + 1)).mul v v = (ring (2 * (3 * t + 2) + 1 + 1)).one := by
    have e : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) v (2 * (3 * t + 2) + 1 + 1 + 1 + 1) = (ring (2 * (3 * t + 2) + 1 + 1)).mul (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) v (2 * (3 * t + 2) + 1 + 1 + 1)) v := rfl
    rw [e, ZMod.fermat (2 * (3 * t + 2) + 1) hpr] at hv
    exact hv
  have hv3 : (ring (2 * (3 * t + 2) + 1 + 1)).mul v ((ring (2 * (3 * t + 2) + 1 + 1)).mul v v) = (ring (2 * (3 * t + 2) + 1 + 1)).one := by
    rw [← hvdef]
    show (ring (2 * (3 * t + 2) + 1 + 1)).mul (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) c (2 * t + 2)) ((ring (2 * (3 * t + 2) + 1 + 1)).mul (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) c (2 * t + 2)) (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) c (2 * t + 2))) = _
    rw [show (ring (2 * (3 * t + 2) + 1 + 1)).mul (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) c (2 * t + 2)) ((ring (2 * (3 * t + 2) + 1 + 1)).mul (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) c (2 * t + 2)) (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) c (2 * t + 2)))
        = Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) c (2 * t + 2)) 3 by show _ = (ring (2 * (3 * t + 2) + 1 + 1)).mul ((ring (2 * (3 * t + 2) + 1 + 1)).mul ((ring (2 * (3 * t + 2) + 1 + 1)).mul (ring (2 * (3 * t + 2) + 1 + 1)).one _) _) _; lib_ring,
      TwoPower.pwpow, show (2 * t + 2) * 3 = 2 * (3 * t + 2) + 1 + 1 by lib_nat, ZMod.unit_pow (2 * (3 * t + 2) + 1) hpr c hc]
  rw [hv2, show (ring (2 * (3 * t + 2) + 1 + 1)).mul v (ring (2 * (3 * t + 2) + 1 + 1)).one = v by rw [(ring (2 * (3 * t + 2) + 1 + 1)).mul_comm, (ring (2 * (3 * t + 2) + 1 + 1)).one_mul]] at hv3
  exact hv3

/-- **(c, 0) a cube in 𝔽_{p²} ⇒ c a cube in 𝔽_p.** -/
theorem cube_down (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) (n : Zp (2 * (3 * t + 2) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) n (3 * t + 2 + 1) = (ring (2 * (3 * t + 2) + 1 + 1)).neg (ring (2 * (3 * t + 2) + 1 + 1)).one) (c : Zp (2 * (3 * t + 2) + 1 + 1)) (hc : c ≠ (ring (2 * (3 * t + 2) + 1 + 1)).zero)
    (z : Zp (2 * (3 * t + 2) + 1 + 1) × Zp (2 * (3 * t + 2) + 1 + 1)) (hz : (Fq2.G2 (2 * (3 * t + 2) + 1) n).mul z ((Fq2.G2 (2 * (3 * t + 2) + 1) n).mul z z) = (c, (ring (2 * (3 * t + 2) + 1 + 1)).zero)) :
    ∃ s, s ≠ (ring (2 * (3 * t + 2) + 1 + 1)).zero ∧ (ring (2 * (3 * t + 2) + 1 + 1)).mul s ((ring (2 * (3 * t + 2) + 1 + 1)).mul s s) = c :=
  cubic_euler t hpr c hc (lift_cube t hpr n hn c hc z hz)

end CubeLift

#print axioms CubeLift.cubic_euler
#print axioms CubeLift.lift_cube
#print axioms CubeLift.cube_down

Listing 119: lang/SexticHD.lean

lang/SexticHD.lean

/-
  lang/SexticHD — T43, cycle 120: a(𝔽_{p²}) = a_p² − 2p for every sextic twist y² = x³ + D at every split p ≡ 1 (mod 3).

  X = −ψ_{𝔽_{p²}}(D), Y = ψ_{𝔽_p}(D), X' = Y² − 2p:
    X ≡ X' (mod p)        SplitHasseD
    X ≡ X' (mod 3)        ψ ≡ χ (mod 3) (FinCubicNorm), χ_{p²}(D) = 1, χ_p(D)² = 1, p ≡ 1 (mod 3)
    X ≡ X' (mod 2)        ψ ≡ 1 + #{x³ = −D} (mod 2) (CubeCount), equal counts over 𝔽_p and 𝔽_{p²} (CubeLift)
    X² + 3B² = 4p², X'² + 3(Yb)² = 4p²   (FinCubicNorm)
  so X − X' = 6ps and (X − X')² + (X + X')² + 6B² + 6(Yb)² = 16p² forces s = 0:
    sextic_hd_split   −ψ_{p²}(D) = ψ_p(D)² − 2p.
  With SexticInert every curve y² = x³ + D (D ∈ 𝔽_p^×) has the Frobenius-square identity at every prime p > 3.

  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.CubeLift
import lang.SexticInert

namespace SexticHD

open FTAChain ZMod YMChain FiniteRecChain LibRing LibInt HasseDavenport

/-- the integer finish. -/
theorem finish6 (P X Y B b q T2 T1 c R N2 N1 : Int) (hP0 : P ≠ 0) (hP : P = 3 * c + 1) (hPo : ∃ e, P = 2 * e + 1)
    (hq : -X + Y * Y = P * q)
    (hX3 : -X = 1 + 3 * T2) (hY3 : (Y = 1 + 3 * T1) ∨ (Y = -1 + 3 * T1))
    (hX2 : -X = P * P - R - 2 * N2) (hY2 : Y = P - R - 2 * N1)
    (hXn : X * X + 3 * (B * B) = 4 * (P * P)) (hYn : Y * Y + 3 * (b * b) = 4 * P) :
    X = Y * Y - 2 * P := by
  match hPo with
  | ⟨e, he⟩ =>
  have hd : X - (Y * Y - 2 * P) = P * (2 - q) := by
    calc X - (Y * Y - 2 * P) = 2 * P - (-X + Y * Y) := by lib_int
      _ = P * (2 - q) := by rw [hq]; lib_int
  -- 3 | 2 − q
  have h3 : ∃ s3, 2 - q = 3 * s3 := by
    have hY2' : ∃ T, Y * Y = 1 + 3 * T := by
      cases hY3 with
      | inl h => exact ⟨2 * T1 + 3 * T1 * T1, by rw [h]; lib_int⟩
      | inr h => exact ⟨-2 * T1 + 3 * T1 * T1, by rw [h]; lib_int⟩
    match hY2' with
    | ⟨T, hT⟩ =>
      have hXv : X = -1 - 3 * T2 := by
        calc X = -(-X) := by lib_int
          _ = -(1 + 3 * T2) := by rw [hX3]
          _ = -1 - 3 * T2 := by lib_int
      refine ⟨-T2 - T + 2 * c + (2 - q) * (-c), ?_⟩
      calc 2 - q = P * (2 - q) - 3 * c * (2 - q) := by rw [hP]; lib_int
        _ = (X - (Y * Y - 2 * P)) - 3 * c * (2 - q) := by rw [hd]
        _ = _ := by
          rw [hXv, hT, hP]; lib_int
  -- 2 | 2 − q
  have hev : ∃ W, X - (Y * Y - 2 * P) = 2 * W := by
    have base : X - (Y * Y - 2 * P) = -4 * (e * e) + R * (3 - R) + 2 * N2 - 4 * (e - N1) * (1 - R) - 4 * ((e - N1) * (e - N1)) := by
      have hXv : X = -(P * P - R - 2 * N2) := by
        calc X = -(-X) := by lib_int
          _ = _ := by rw [hX2]
      rw [hXv, hY2, he]; lib_int
    match GaussInt.int_even_odd R with
    | Or.inl ⟨r, hr⟩ =>
      exact ⟨-2 * (e * e) + (3 * r - 2 * r * r) + N2 - 2 * (e - N1) * (1 - R) - 2 * ((e - N1) * (e - N1)),
        by rw [base, hr]; lib_int⟩
    | Or.inr ⟨r, hr⟩ =>
      exact ⟨-2 * (e * e) + (2 * r + 1) * (1 - r) + N2 - 2 * (e - N1) * (1 - R) - 2 * ((e - N1) * (e - N1)),
        by rw [base, hr]; lib_int⟩
  have h2 : ∃ s2, 2 - q = 2 * s2 := by
    match hev, GaussInt.int_even_odd (2 - q) with
    | _, Or.inl h => exact h
    | ⟨W, hW⟩, Or.inr ⟨u, hu⟩ =>
      exfalso
      exact GaussInt.int_odd_ne_even (2 * e * u + e + u) W (by rw [← hW, hd, hu, he]; lib_int)
  match h3, h2 with
  | ⟨s3, hs3⟩, ⟨s2, hs2⟩ =>
    have hs : ∃ s, 2 - q = 6 * s := by
      match GaussInt.int_even_odd s3 with
      | Or.inl ⟨s, hs⟩ => exact ⟨s, by rw [hs3, hs]; lib_int⟩
      | Or.inr ⟨s, hs⟩ =>
        exfalso
        exact GaussInt.int_odd_ne_even (3 * s + 1) s2 (by rw [← hs2, hs3, hs]; lib_int)
    match hs with
    | ⟨s, hs⟩ =>
      match FinCubicSign.sq_nn s with
      | ⟨0, h0⟩ =>
        have : s = 0 := sq_zero s h0
        calc X = (X - (Y * Y - 2 * P)) + (Y * Y - 2 * P) := by lib_int
          _ = Y * Y - 2 * P := by rw [hd, hs, this]; lib_int
      | ⟨n' + 1, hn⟩ =>
        exfalso
        match FinCubicSign.sq_nn B, FinCubicSign.sq_nn (Y * b), FinCubicSign.sq_nn (X + (Y * Y - 2 * P)), FinCubicSign.sq_nn P with
        | ⟨nB, hnB⟩, ⟨n2, hn2⟩, ⟨m, hm⟩, ⟨pp, hpp⟩ =>
          have key : Int.ofNat (20 * pp + 36 * pp * n' + m + 6 * nB + 6 * n2) = 0 := by
            show 20 * Int.ofNat pp + 36 * Int.ofNat pp * Int.ofNat n' + Int.ofNat m + 6 * Int.ofNat nB + 6 * Int.ofNat n2 = 0
            have e1 : (X - (Y * Y - 2 * P)) * (X - (Y * Y - 2 * P)) = 36 * (P * P) * (s * s) := by rw [hd, hs]; lib_int
            have e2 : (Y * Y - 2 * P) * (Y * Y - 2 * P) + 3 * ((Y * b) * (Y * b)) = 4 * (P * P) := by
              calc (Y * Y - 2 * P) * (Y * Y - 2 * P) + 3 * ((Y * b) * (Y * b))
                  = (Y * Y - 2 * P) * (Y * Y - 2 * P) + Y * Y * (Y * Y + 3 * (b * b)) - Y * Y * (Y * Y) := by lib_int
                _ = 4 * (P * P) := by rw [hYn]; lib_int
            have hss : s * s = Int.ofNat n' + 1 := hn
            rw [← hpp, ← hm, ← hnB, ← hn2]
            calc 20 * (P * P) + 36 * (P * P) * Int.ofNat n' + (X + (Y * Y - 2 * P)) * (X + (Y * Y - 2 * P)) + 6 * (B * B) + 6 * (Y * b * (Y * b))
                = ((X - (Y * Y - 2 * P)) * (X - (Y * Y - 2 * P)) + (X + (Y * Y - 2 * P)) * (X + (Y * Y - 2 * P)))
                  + 6 * (B * B) + 6 * (Y * b * (Y * b)) - 36 * (P * P) * (s * s) + 36 * (P * P) * Int.ofNat n' + 20 * (P * P) := by rw [e1]; lib_int
              _ = 2 * (X * X + 3 * (B * B)) + 2 * ((Y * Y - 2 * P) * (Y * Y - 2 * P) + 3 * ((Y * b) * (Y * b)))
                  - 36 * (P * P) * (s * s) + 36 * (P * P) * Int.ofNat n' + 20 * (P * P) := by lib_int
              _ = 0 := by rw [hXn, e2, hss]; lib_int
          have hk := Int.ofNat.inj key
          match nat_sum_zero _ _ hk with
          | ⟨h1, _⟩ =>
            match nat_sum_zero _ _ h1 with
            | ⟨h2', _⟩ =>
              match nat_sum_zero _ _ h2' with
              | ⟨h3', _⟩ =>
                match nat_sum_zero _ _ h3' with
                | ⟨h4, _⟩ =>
                  have hpp0 : pp = 0 := by
                    cases pp with
                    | zero => rfl
                    | succ q => exact Nat.noConfusion h4
                  exact hP0 (sq_zero P (hpp.trans (by rw [hpp0]; rfl)))

theorem cube_unity {R : Type} [DecidableEq R] (F : FinField.FF R) (N : Nat) (hN : 2 * F.h = 3 * N) :
    ∃ w, w ≠ F.K.one ∧ FinCubic.cube F w = F.K.one :=
  match FinCyclic.split_pow_ff F 1 0 ⟨N, by rw [hN]; lib_nat⟩ with
  | ⟨w, hw3', hw1'⟩ => ⟨w, fun e => hw1' (by rw [e]; exact TwoPower.pwone F.K _),
      by rw [← hw3']; show _ = F.K.mul (F.K.mul (F.K.mul F.K.one w) w) w; unfold FinCubic.cube; lib_ring⟩

theorem neg_pair (t : Nat) (n : Zp (2 * (3 * t + 2) + 1 + 1)) (D : Zp (2 * (3 * t + 2) + 1 + 1)) :
    (Fq2.G2 (2 * (3 * t + 2) + 1) n).neg (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) = ((ring (2 * (3 * t + 2) + 1 + 1)).neg D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) :=
  Prod.ext rfl (DetChain.neg_unique (ring (2 * (3 * t + 2) + 1 + 1)) _ _ ((ring (2 * (3 * t + 2) + 1 + 1)).zero_add _)).symm

/-- **Sextic twists at split p = 6t + 7: −ψ_{p²}(D) = ψ_p(D)² − 2p.** -/
theorem sextic_hd_split (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) (n : Zp (2 * (3 * t + 2) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) n (3 * t + 2 + 1) = (ring (2 * (3 * t + 2) + 1 + 1)).neg (ring (2 * (3 * t + 2) + 1 + 1)).one) (D : Zp (2 * (3 * t + 2) + 1 + 1)) (hD : D ≠ (ring (2 * (3 * t + 2) + 1 + 1)).zero) :
    -FinCubic.psi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) = FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D * FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D + -2 * Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) := by
  have hnD : (ring (2 * (3 * t + 2) + 1 + 1)).neg D ≠ (ring (2 * (3 * t + 2) + 1 + 1)).zero := fun e => hD (by rw [← DetChain.neg_neg' (ring (2 * (3 * t + 2) + 1 + 1)) D, e]; exact (DetChain.neg_unique (ring (2 * (3 * t + 2) + 1 + 1)) _ _ ((ring (2 * (3 * t + 2) + 1 + 1)).zero_add _)).symm)
  have hD2 : (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) ≠ (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.zero := fun e => hD (congrArg Prod.fst e)
  have hFh : (Fq2Inst.ffq (3 * t + 2) hpr n hn).h = 3 * (6 * t * t + 14 * t + 7) + 3 := SplitHasse3.hF3 t
  have hN : 2 * (Fq2Inst.ffq (3 * t + 2) hpr n hn).h = 3 * (2 * (6 * t * t + 14 * t + 7) + 2) := by rw [hFh]; lib_nat
  have hNp : 2 * (FinField.ffp (3 * t + 2) hpr).h = 3 * (2 * t + 2) := by show 2 * (3 * t + 2 + 1) = _; lib_nat
  have hPPF : FinMoment.PP (Fq2Inst.ffq (3 * t + 2) hpr n hn) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) := by
    show Int.ofNat (2 * (Fq2Inst.ffq (3 * t + 2) hpr n hn).h + 1) = _
    rw [CubicSuper2.h_eq (3 * t + 2) hpr n hn, CubicSuper.sq_id]; rfl
  have hPPp : FinMoment.PP (FinField.ffp (3 * t + 2) hpr) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) := rfl
  -- mod p
  have h0 : castZ (ring (2 * (3 * t + 2) + 1 + 1)) (FinCubic.psi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) + FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D * FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D) = (ring (2 * (3 * t + 2) + 1 + 1)).zero := by
    rw [IntCast.castZ_add, castZ_mul, SplitHasseD.split_hasseD t hpr n hn D]; lib_ring
  match castZ_zero_dvd (2 * (3 * t + 2) + 1 + 1) _ h0 with
  | ⟨q, hq⟩ =>
  -- mod 3
  match FinCubicNorm.psi_mod3 (Fq2Inst.ffq (3 * t + 2) hpr n hn) _ hN (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero), FinCubicNorm.psi_mod3 (FinField.ffp (3 * t + 2) hpr) _ hNp D with
  | ⟨T2, hT2⟩, ⟨T1, hT1⟩ =>
  have hchi2 : FinField.chi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) = 1 := by
    apply FinField.chi_one_of _ _ hD2
    show Frobenius.pw (Fq2.G2 (2 * (3 * t + 2) + 1) n) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) (Fq2Inst.ffq (3 * t + 2) hpr n hn).h = (Fq2.G2 (2 * (3 * t + 2) + 1) n).one
    rw [CubicSuper2.h_eq (3 * t + 2) hpr n hn, QuadFrob.pw_embed (2 * (3 * t + 2) + 1) n,
      show (2 * (3 * t + 2) + 1 + 1 + 1 + 1) * (3 * t + 2 + 1) = (2 * (3 * t + 2) + 1 + 1) * (3 * t + 2 + 2) by lib_nat, ← TwoPower.pwpow,
      ZMod.unit_pow (2 * (3 * t + 2) + 1) hpr _ hD, TwoPower.pwone]
    rfl
  rw [hchi2] at hT2
  have hY3 : (FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D = 1 + 3 * T1) ∨ (FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D = -1 + 3 * T1) := by
    cases FinClass.chi_cases (FinField.ffp (3 * t + 2) hpr) D with
    | inl h => exact absurd h (FinClass.chi_ne_zero (FinField.ffp (3 * t + 2) hpr) D hD)
    | inr h =>
      cases h with
      | inl h1 => left; rw [hT1, h1]
      | inr h1 => right; rw [hT1, h1]
  -- parity: equal cube-root counts
  match cube_unity (Fq2Inst.ffq (3 * t + 2) hpr n hn) _ hN, cube_unity (FinField.ffp (3 * t + 2) hpr) _ hNp with
  | ⟨w2, hw21, hw23⟩, ⟨w1, hw11, hw13⟩ =>
  have hR : CubeCount.Rc (Fq2Inst.ffq (3 * t + 2) hpr n hn) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) = CubeCount.Rc (FinField.ffp (3 * t + 2) hpr) D := by
    by_cases hv : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) ((ring (2 * (3 * t + 2) + 1 + 1)).neg D) (2 * t + 2) = (ring (2 * (3 * t + 2) + 1 + 1)).one
    · match CubeLift.cubic_euler t hpr _ hnD hv with
      | ⟨s0, hs0, hs3⟩ =>
        rw [CubeCount.R_three (FinField.ffp (3 * t + 2) hpr) w1 hw11 hw13 D s0 hs0 hs3,
          CubeCount.R_three (Fq2Inst.ffq (3 * t + 2) hpr n hn) w2 hw21 hw23 (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) (s0, (ring (2 * (3 * t + 2) + 1 + 1)).zero) (fun e => hs0 (congrArg Prod.fst e)) (by
            show (Fq2.G2 (2 * (3 * t + 2) + 1) n).mul (s0, (ring (2 * (3 * t + 2) + 1 + 1)).zero) ((Fq2.G2 (2 * (3 * t + 2) + 1) n).mul (s0, (ring (2 * (3 * t + 2) + 1 + 1)).zero) (s0, (ring (2 * (3 * t + 2) + 1 + 1)).zero)) = (Fq2.G2 (2 * (3 * t + 2) + 1) n).neg (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero)
            rw [Fq2.embed_mul, Fq2.embed_mul, hs3, neg_pair])]
    · rw [CubeCount.R_zero (FinField.ffp (3 * t + 2) hpr) D (fun x hx => hv (by
          have hx' : (ring (2 * (3 * t + 2) + 1 + 1)).mul x ((ring (2 * (3 * t + 2) + 1 + 1)).mul x x) = (ring (2 * (3 * t + 2) + 1 + 1)).neg D := hx
          have hx0 : x ≠ (ring (2 * (3 * t + 2) + 1 + 1)).zero := fun e => hnD (by rw [← hx', e]; show (ring (2 * (3 * t + 2) + 1 + 1)).mul (ring (2 * (3 * t + 2) + 1 + 1)).zero _ = _; exact DetChain.zero_mul' (ring (2 * (3 * t + 2) + 1 + 1)) _)
          rw [show (ring (2 * (3 * t + 2) + 1 + 1)).neg D = Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) x 3 by rw [← hx']; show _ = (ring (2 * (3 * t + 2) + 1 + 1)).mul ((ring (2 * (3 * t + 2) + 1 + 1)).mul ((ring (2 * (3 * t + 2) + 1 + 1)).mul (ring (2 * (3 * t + 2) + 1 + 1)).one x) x) x; lib_ring,
            TwoPower.pwpow, show 3 * (2 * t + 2) = 2 * (3 * t + 2) + 1 + 1 by lib_nat]
          exact ZMod.unit_pow (2 * (3 * t + 2) + 1) hpr x hx0)),
        CubeCount.R_zero (Fq2Inst.ffq (3 * t + 2) hpr n hn) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) (fun z hz => hv (CubeLift.lift_cube t hpr n hn _ hnD z (by
          have : (Fq2.G2 (2 * (3 * t + 2) + 1) n).mul z ((Fq2.G2 (2 * (3 * t + 2) + 1) n).mul z z) = (Fq2.G2 (2 * (3 * t + 2) + 1) n).neg (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) := hz
          rw [this, neg_pair])))]
  have hpar2 := CubeCount.psi_parity (Fq2Inst.ffq (3 * t + 2) hpr n hn) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero)
  have hpar1 := CubeCount.psi_parity (FinField.ffp (3 * t + 2) hpr) D
  rw [hPPF, hR] at hpar2
  rw [hPPp] at hpar1
  match FinCubicNorm.norm_twist (Fq2Inst.ffq (3 * t + 2) hpr n hn) _ hN (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) hD2, FinCubicNorm.norm_twist (FinField.ffp (3 * t + 2) hpr) _ hNp D hD with
  | ⟨B, hB⟩, ⟨b, hb⟩ =>
  rw [hPPF] at hB
  rw [hPPp] at hb
  generalize FinCubic.psi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero) = S2 at hq hT2 hpar2 hB ⊢
  generalize FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D = S1 at hq hY3 hpar1 hb ⊢
  have hP : Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) = 3 * (2 * Int.ofNat t + 2) + 1 := by show 2 * (3 * Int.ofNat t + 2) + 1 + 1 + 1 = _; lib_int
  have hPo : ∃ e, Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) = 2 * e + 1 := ⟨3 * Int.ofNat t + 3, by rw [hP]; lib_int⟩
  have hP0 : Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  have hfin := finish6 (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) (-S2) S1 B b q T2 T1 (2 * Int.ofNat t + 2) (CubeCount.Rc (FinField.ffp (3 * t + 2) hpr) D) (sumK FinConic.Z (fun x => FinCubicTrace.nq (Fq2Inst.ffq (3 * t + 2) hpr n hn) ((Fq2Inst.ffq (3 * t + 2) hpr n hn).K.add (FinCubic.cube (Fq2Inst.ffq (3 * t + 2) hpr n hn) x) (D, (ring (2 * (3 * t + 2) + 1 + 1)).zero))) (Fq2Inst.ffq (3 * t + 2)   -- (+227 chars; full line in the Zenodo source)
    (by rw [← hq]; lib_int) (by rw [hT2]; lib_int) hY3 (by rw [hpar2]; lib_int) (by rw [hpar1]; lib_int)
    (by rw [← hB]; lib_int) hb
  rw [hfin]; lib_int

/-- **Every sextic twist y² = x³ + D, D ∈ 𝔽_p^×, at every prime p > 3: −ψ_{p²}(D) = ψ_p(D)² − 2p.** -/
theorem every_sextic (k : Nat) (hk : 1 ≤ k) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    -FinCubic.psi (Fq2Inst.ffq k hpr n hn) (D, (ring (2 * k + 1 + 1)).zero) = FinCubic.psi (FinField.ffp k hpr) D * FinCubic.psi (FinField.ffp k hpr) D + -2 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases EveryPrime3.nat_mod3 k with
  | inl h =>
    exfalso
    match h with
    | ⟨t, ht⟩ =>
      cases hpr 3 ⟨2 * t + 1, by rw [ht]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj (show (2 : Nat) + 1 = 0 + 1 from e))
      | inr e =>
        have e' : 2 * k = 0 := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj e.symm))
        cases k with
        | zero => exact Nat.not_succ_le_zero 0 hk
        | succ k' => exact Nat.noConfusion (show 2 * k' + 1 + 1 = 0 from e')
  | inr h =>
    cases h with
    | inl h =>
      match h with
      | ⟨t, ht⟩ =>
        subst ht
        exact SexticInert.sextic_hd_inert (3 * t + 1) (2 * t + 2) (by lib_nat) hpr n hn D hD
    | inr h =>
      match h with
      | ⟨t, ht⟩ => subst ht; exact sextic_hd_split t hpr n hn D hD

end SexticHD

#print axioms SexticHD.finish6
#print axioms SexticHD.cube_unity
#print axioms SexticHD.neg_pair
#print axioms SexticHD.sextic_hd_split
#print axioms SexticHD.every_sextic

For \(y^2=x^3+1\) and every prime \(p\equiv 1\ (3)\), with \(\psi _q(1)=\sum _{x\in \mathbb F_q}\chi (x^3+1)\),

\[ -\psi _{p^2}(1)=\psi _p(1)^2-2p . \]
The steps are
\[ \begin {aligned} \textstyle \sum _x(x^3+1)^{(q-1)/2}&=-\tbinom {(q-1)/2}{(q-1)/3},\qquad \tbinom {a+ap}{c+cp}\equiv \tbinom ac^2\ (p),\\ -\psi _p(1)&=2a,\ p=a^2+3b^2,\ a\equiv 1\ (3);\qquad -\psi _{p^2}(1)=2A,\ p^2=A^2+3B^2,\ A\equiv 1\ (3),\\ A&\equiv 2a^2\equiv a^2-3b^2\ (p),\quad A-(a^2-3b^2)=3ps\ (p\equiv 1\ (3)),\\ (A-A')^2+(A+A')^2+6B^2+6(2ab)^2&=4p^2\ \Rightarrow \ 9p^2s^2\le 4p^2\ \Rightarrow \ s=0 . \end {aligned} \]

Listing 120: lang/FinHasse3.lean

lang/FinHasse3.lean

/-
  lang/FinHasse3 — T43, cycle 102: the Hasse invariant of y² = x³ + 1 over every finite field with q ≡ 1 (mod 6).

  For an FF with h = (q − 1)/2 = 3k + 3:
    ps_zero, ps_low3, ps_top3, ps_high3   Σ_x x^e = 0 (e = 0 and 1 ≤ e ≤ q − 2), −1 (e = q − 1), 0 (q ≤ e ≤ 2q − 3)
    T3F, T3_cons, T3_zero, skip3_many     one binomial coefficient at a time (port of HasseEisen)
    fin_hasse3     Σ_x (x³ + 1)^h = −C(3k + 3, 2k + 2)   in the field
  Over 𝔽_{p²} (p ≡ 1 mod 3) this is the input, with Lucas, for a(𝔽_{p²}) = a_p² − 2p for y² = x³ + 1.

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.FinHasse
import lang.HasseEisen

namespace FinHasse3

open FTAChain YMChain Frobenius FinField HasseCM

variable {R : Type} [DecidableEq R] (F : FF R)

theorem two_h3 (k : Nat) (hk : F.h = 3 * k + 3) : 2 * F.h = (6 * k + 5) + 1 := by rw [hk]; lib_nat

theorem ps_zero : FinHasse.psF F 0 = F.K.zero := by
  show sumK F.K (fun _ => F.K.one) F.els = F.K.zero
  rw [FinPowerSum.sum_one_K, FinList.len_els F, FinPowerSum.char_q]

theorem ps_low3 (k : Nat) (hk : F.h = 3 * k + 3) (e : Nat) (h1 : 1 ≤ e) (h2 : e ≤ 6 * k + 5) : FinHasse.psF F e = F.K.zero := by
  cases e with
  | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
  | succ e' => exact FinPowerSum.ps_zero_ff F e' (by rw [two_h3 F k hk]; exact Nat.succ_le_succ h2)

theorem ps_top3 (k : Nat) (hk : F.h = 3 * k + 3) : FinHasse.psF F ((6 * k + 5) + 1) = F.K.neg F.K.one := by
  rw [← two_h3 F k hk]; exact FinPowerSum.ps_top_ff F

theorem ps_high3 (k : Nat) (hk : F.h = 3 * k + 3) (r : Nat) (h1 : 1 ≤ r) (h2 : r ≤ 6 * k + 5) :
    FinHasse.psF F ((6 * k + 5) + 1 + r) = F.K.zero := by
  have e : FinHasse.psF F ((6 * k + 5) + 1 + r) = FinHasse.psF F r := sumK_ext F.K _ _ (fun x => by
    show pw F.K x ((6 * k + 5) + 1 + r) = pw F.K x r
    by_cases hx : x = F.K.zero
    · rw [hx]
      cases r with
      | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
      | succ r' => rw [pw_zero_succ, ← Nat.add_assoc, pw_zero_succ]
    · rw [pw_add' F.K x _ r, ← two_h3 F k hk, FinCyclic.fermat_ff F x hx, F.K.one_mul]) _
  rw [e]
  exact ps_low3 F k hk r h1 h2

def ev3F (x : R) (cs : List Nat) : R := ev F.K (pw F.K x 3) F.K.one cs

def T3F (e : Nat) (cs : List Nat) : R :=
  sumK F.K (fun x => F.K.mul (pw F.K x e) (ev3F F x cs)) F.els

theorem T3_cons (e c : Nat) (cs : List Nat) :
    T3F F e (c :: cs) = F.K.add (F.K.mul (nsm F.K c F.K.one) (FinHasse.psF F e)) (T3F F (e + 3) cs) := by
  have pt : ∀ x : R, F.K.mul (pw F.K x e) (ev3F F x (c :: cs))
      = F.K.add (F.K.mul (nsm F.K c F.K.one) (pw F.K x e)) (F.K.mul (pw F.K x (e + 3)) (ev3F F x cs)) := fun x => by
    show F.K.mul (pw F.K x e) (F.K.add (nsm F.K c (pw F.K F.K.one cs.length)) (F.K.mul (pw F.K x 3) (ev3F F x cs))) = _
    rw [TwoPower.pwone, F.K.distrib, F.K.mul_comm (pw F.K x e) (nsm F.K c _), ← F.K.mul_assoc, ← pw_add' F.K x e 3]
  show sumK F.K _ _ = _
  rw [sumK_ext F.K _ _ pt, sumK_add, ← mul_sumK]
  rfl

theorem T3_nil (e : Nat) : T3F F e [] = F.K.zero := by
  show sumK F.K (fun x => F.K.mul (pw F.K x e) F.K.zero) _ = _
  rw [sumK_ext F.K _ (fun _ => F.K.zero) (fun x => DetChain.mul_zero F.K _), FinHasse.sumK_zero' F]

theorem idx3' (e i : Nat) : e + 3 + 3 * i = e + 3 * (i + 1) := by lib_nat

theorem T3_zero : ∀ (cs : List Nat) (e : Nat), (∀ i, i < cs.length → FinHasse.psF F (e + 3 * i) = F.K.zero) → T3F F e cs = F.K.zero
  | [], e, _ => T3_nil F e
  | c :: cs, e, h => by
    have h0 : FinHasse.psF F e = F.K.zero := h 0 (Nat.zero_lt_succ _)
    rw [T3_cons, h0, DetChain.mul_zero, F.K.zero_add]
    exact T3_zero cs (e + 3) (fun i hi => by rw [idx3']; exact h (i + 1) (Nat.succ_lt_succ hi))

theorem skip3_many (n m : Nat) : ∀ (i j e : Nat), (∀ i', i' < i → FinHasse.psF F (e + 3 * i') = F.K.zero) →
    T3F F e (Frobenius.listC n j (i + m)) = T3F F (e + 3 * i) (Frobenius.listC n (j + i) m)
  | 0, j, e, _ => by rw [Nat.zero_add]; rfl
  | i + 1, j, e, h => by
    rw [Nat.succ_add]
    show T3F F e (Frobenius.C n j :: Frobenius.listC n (j + 1) (i + m)) = _
    have h0 : FinHasse.psF F e = F.K.zero := h 0 (Nat.zero_lt_succ _)
    rw [T3_cons, h0, DetChain.mul_zero, F.K.zero_add,
      skip3_many n m i (j + 1) (e + 3) (fun i' hi' => by rw [idx3']; exact h (i' + 1) (Nat.succ_lt_succ hi')),
      idx3', Nat.add_assoc j 1 i, Nat.add_comm 1 i]

theorem fin_hasse3 (k : Nat) (hk : F.h = 3 * k + 3) :
    sumK F.K (fun x => pw F.K (F.K.add (pw F.K x 3) F.K.one) (3 * k + 3)) F.els
      = F.K.neg (nsm F.K (Frobenius.C (3 * k + 3) (2 * k + 2)) F.K.one) := by
  have eA : sumK F.K (fun x => pw F.K (F.K.add (pw F.K x 3) F.K.one) (3 * k + 3)) F.els
      = T3F F 0 (Frobenius.row (3 * k + 3)) :=
    sumK_ext F.K _ _ (fun x => by
      show _ = F.K.mul F.K.one (ev3F F x (Frobenius.row (3 * k + 3)))
      rw [F.K.one_mul, ev3F, ev_row]) _
  rw [eA, Frobenius.row_eq, show 3 * k + 3 + 1 = (2 * k + 2) + (k + 2) by lib_nat,
    skip3_many F (3 * k + 3) (k + 2) (2 * k + 2) 0 0 (fun i' hi' => by
      cases i' with
      | zero => exact ps_zero F
      | succ i'' =>
        apply ps_low3 F k hk
        · rw [show 0 + 3 * (i'' + 1) = 1 + (3 * i'' + 2) by lib_nat]; exact Nat.le_add_right 1 _
        · have hi : i'' ≤ 2 * k := Nat.le_of_succ_le_succ (Nat.le_of_lt_succ hi')
          have a : 0 + 3 * (i'' + 1) ≤ 0 + 3 * (2 * k + 1) := Nat.add_le_add_left (Nat.mul_le_mul_left 3 (Nat.succ_le_succ hi)) 0
          exact Nat.le_trans a (by rw [show 0 + 3 * (2 * k + 1) = 6 * k + 3 by lib_nat]; exact Nat.le_succ_of_le (Nat.le_succ _)))]
  rw [show 0 + (2 * k + 2) = 2 * k + 2 by lib_nat]
  show T3F F (0 + 3 * (2 * k + 2)) (Frobenius.C (3 * k + 3) (2 * k + 2) :: Frobenius.listC (3 * k + 3) (2 * k + 2 + 1) (k + 1)) = _
  rw [T3_cons, show 0 + 3 * (2 * k + 2) = (6 * k + 5) + 1 by lib_nat, ps_top3 F k hk,
    T3_zero F _ _ (fun i hi => by
      rw [Frobenius.len_listC] at hi
      rw [show (6 * k + 5) + 1 + 3 + 3 * i = (6 * k + 5) + 1 + (3 * i + 3) by lib_nat]
      apply ps_high3 F k hk
      · exact Nat.le_add_left 1 _
      · have a : 3 * i + 3 ≤ 3 * k + 3 := Nat.add_le_add_right (Nat.mul_le_mul_left 3 (Nat.le_of_lt_succ hi)) 3
        exact Nat.le_trans a (by rw [show 6 * k + 5 = (3 * k + 3) + (3 * k + 2) by lib_nat]; exact Nat.le_add_right _ _)),
    DetChain.add_zero', mul_neg_r F.K, F.K.mul_comm _ F.K.one, F.K.one_mul]

end FinHasse3

#print axioms FinHasse3.two_h3
#print axioms FinHasse3.ps_zero
#print axioms FinHasse3.ps_low3
#print axioms FinHasse3.ps_top3
#print axioms FinHasse3.ps_high3
#print axioms FinHasse3.T3_cons
#print axioms FinHasse3.T3_nil
#print axioms FinHasse3.idx3'
#print axioms FinHasse3.T3_zero
#print axioms FinHasse3.skip3_many
#print axioms FinHasse3.fin_hasse3

Listing 121: lang/SplitHasse3.lean

lang/SplitHasse3.lean

/-
  lang/SplitHasse3 — T43, cycle 103: y² = x³ + 1 at a split prime p = 6t + 7: S(𝔽_{p²}) ≡ −S(𝔽_p)² (mod p).

  S₁ = Σ_{𝔽_p} χ(x³ + 1) = ψ(1),  S₂ = Σ_{𝔽_{p²}} χ(z³ + 1)  (𝔽_{p²} = 𝔽_p[√n]).
    cast_chi_p        castZ(χ(z)) = z^((p−1)/2) in 𝔽_p, any odd p
    hasse_eisen'      HasseEisen.hasse_eisen transported to the index form p = 2m + 3
    cast_S1_3         S₁ ≡ −C(3t + 3, 2t + 2)
    cast_S2_3         S₂ ≡ −C(3t + 3, 2t + 2)²       (FinHasse3 over 𝔽_{p²}, Lucas: (p²−1)/2 = a + ap, (p²−1)/3 = c + cp)
    split_hasse3      S₂ ≡ −S₁² (mod p)

  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.FinHasse3
import lang.SplitHasse
import lang.FinCubicExact

namespace SplitHasse3

open FTAChain ZMod YMChain FiniteRecChain LibRing Frobenius

theorem cast_chi_p (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) :
    castZ (ring (2 * m + 1 + 1)) (QChar.chi m z) = Frobenius.pw (ring (2 * m + 1 + 1)) z (m + 1) := by
  by_cases hz : z = (ring (2 * m + 1 + 1)).zero
  · rw [hz]
    show castZ (ring (2 * m + 1 + 1)) (QChar.chi m (ring (2 * m + 1 + 1)).zero) = _
    rw [QChar.chi_zero]
    exact (HasseCM.pw_zero_succ (ring (2 * m + 1 + 1)) m).symm
  · cases Euler.euler_pm m hpr z hz with
    | inl h =>
      rw [QChar.chi_one_of m z hz h, h]
      show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).zero = _
      exact DetChain.add_zero' (ring (2 * m + 1 + 1)) _
    | inr h =>
      have h' : Frobenius.pw (ring (2 * m + 1 + 1)) z (m + 1) ≠ (ring (2 * m + 1 + 1)).one := fun e => QChar.ne_pm m (h.symm.trans e)
      rw [QChar.chi_neg_of m z hz h', h]
      show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).zero) = _
      rw [DetChain.add_zero']

theorem hasse_eisen' (t J : Nat) (hJ : 6 * t + 5 = J) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) :
    sumK (ring (J + 1)) (fun x => Frobenius.pw (ring (J + 1)) ((ring (J + 1)).add (Frobenius.pw (ring (J + 1)) x 3) (ring (J + 1)).one) (3 * t + 3)) (CMInert.allZ (J + 1) (J + 1 + 1))
      = (ring (J + 1)).neg (nsm (ring (J + 1)) (C (3 * t + 3) (2 * t + 2)) (ring (J + 1)).one) := by
  subst hJ
  exact HasseEisen.hasse_eisen t hpr

theorem cast_S1_3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) :
    castZ (ring (2 * (3 * t + 2) + 1 + 1)) (CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one) = (ring (2 * (3 * t + 2) + 1 + 1)).neg (nsm (ring (2 * (3 * t + 2) + 1 + 1)) (C (3 * t + 3) (2 * t + 2)) (ring (2 * (3 * t + 2) + 1 + 1)).one) := by
  show castZ (ring (2 * (3 * t + 2) + 1 + 1)) (sumK LibInt.intR (fun x => QChar.chi (3 * t + 2) ((ring (2 * (3 * t + 2) + 1 + 1)).add (CubicJacob.cube (3 * t + 2) x) (ring (2 * (3 * t + 2) + 1 + 1)).one)) (ConicSum.L (3 * t + 2))) = _
  rw [IntCast.castZ_sum, sumK_ext (ring (2 * (3 * t + 2) + 1 + 1)) _ _ (fun x => cast_chi_p (3 * t + 2) hpr _)]
  rw [sumK_ext (ring (2 * (3 * t + 2) + 1 + 1)) _ (fun x => Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) ((ring (2 * (3 * t + 2) + 1 + 1)).add (Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) x 3) (ring (2 * (3 * t + 2) + 1 + 1)).one) (3 * t + 3)) (fun x => by
    rw [show CubicJacob.cube (3 * t + 2) x = Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) x 3 by show (ring (2 * (3 * t + 2) + 1 + 1)).mul x ((ring (2 * (3 * t + 2) + 1 + 1)).mul x x) = (ring (2 * (3 * t + 2) + 1 + 1)).mul ((ring (2 * (3 * t + 2) + 1 + 1)).mul ((ring (2 * (3 * t + 2) + 1 + 1)).mul (ring (2 * (3 * t + 2) + 1 + 1)).one x) x) x; lib_ring])]
  exact hasse_eisen' t (2 * (3 * t + 2) + 1) (by lib_nat) hpr

theorem hF3 (t : Nat) : (2 * (3 * t + 2) + 1 + 1 + 1 + 1) * (3 * t + 2 + 1) = 3 * (6 * t * t + 14 * t + 7) + 3 := by lib_nat
theorem ia3 (t : Nat) : 3 * (6 * t * t + 14 * t + 7) + 3 = (3 * t + 3) + (3 * t + 3) * (2 * (3 * t + 2) + 1 + 1 + 1) := by lib_nat
theorem ic3 (t : Nat) : 2 * (6 * t * t + 14 * t + 7) + 2 = (2 * t + 2) * (2 * (3 * t + 2) + 1 + 1 + 1) + (2 * t + 2) := by lib_nat
theorem lt3 (t : Nat) : 3 * t + 3 < 2 * (3 * t + 2) + 1 + 1 + 1 := Nat.le.intro (k := 3 * t + 3) (by lib_nat)
theorem lt3' (t : Nat) : 2 * t + 2 < 2 * (3 * t + 2) + 1 + 1 + 1 := Nat.le.intro (k := 4 * t + 4) (by lib_nat)

theorem cast_S2_3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) (n : Zp (2 * (3 * t + 2) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) n (3 * t + 2 + 1) = (ring (2 * (3 * t + 2) + 1 + 1)).neg (ring (2 * (3 * t + 2) + 1 + 1)).one) :
    castZ (ring (2 * (3 * t + 2) + 1 + 1)) (FinCubic.psi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.one) = (ring (2 * (3 * t + 2) + 1 + 1)).neg (nsm (ring (2 * (3 * t + 2) + 1 + 1)) (C (3 * t + 3) (2 * t + 2) * C (3 * t + 3) (2 * t + 2)) (ring (2 * (3 * t + 2) + 1 + 1)).one) := by
  have hk : (Fq2Inst.ffq (3 * t + 2) hpr n hn).h = 3 * (6 * t * t + 14 * t + 7) + 3 := hF3 t
  have h2 : castZ (Fq2Inst.ffq (3 * t + 2) hpr n hn).K (FinCubic.psi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.one)
      = (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.neg (nsm (Fq2Inst.ffq (3 * t + 2) hpr n hn).K (C (3 * t + 3) (2 * t + 2) * C (3 * t + 3) (2 * t + 2)) (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.one) := by
    show castZ (Fq2Inst.ffq (3 * t + 2) hpr n hn).K (sumK LibInt.intR (fun x => FinField.chi (Fq2Inst.ffq (3 * t + 2) hpr n hn) ((Fq2Inst.ffq (3 * t + 2) hpr n hn).K.add (FinCubic.cube (Fq2Inst.ffq (3 * t + 2) hpr n hn) x) (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.one)) (Fq2Inst.ffq (3 * t + 2) hpr n hn).els) = _
    rw [FinCast.cast_sum_ff, hk]
    rw [sumK_ext (Fq2Inst.ffq (3 * t + 2) hpr n hn).K _ (fun x => Frobenius.pw (Fq2Inst.ffq (3 * t + 2) hpr n hn).K ((Fq2Inst.ffq (3 * t + 2) hpr n hn).K.add (Frobenius.pw (Fq2Inst.ffq (3 * t + 2) hpr n hn).K x 3) (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.one) (3 * (6 * t * t + 14 * t + 7) + 3)) (fun x => by
      rw [show FinCubic.cube (Fq2Inst.ffq (3 * t + 2) hpr n hn) x = Frobenius.pw (Fq2Inst.ffq (3 * t + 2) hpr n hn).K x 3 by show (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.mul x ((Fq2Inst.ffq (3 * t + 2) hpr n hn).K.mul x x) = (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.mul ((Fq2Inst.ffq (3 * t + 2) hpr n hn).K.mul ((Fq2Inst.ffq (3 * t + 2) hpr n hn).K.mul (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.one x) x) x; lib  -- (+8 chars; full line in the Zenodo source)
    rw [FinHasse3.fin_hasse3 (Fq2Inst.ffq (3 * t + 2) hpr n hn) (6 * t * t + 14 * t + 7) hk, ia3 t, ic3 t,
      Lucas.lucas (Fq2Inst.ffq (3 * t + 2) hpr n hn).K (2 * (3 * t + 2) + 1) hpr (QuadFrob.char_quad (2 * (3 * t + 2) + 1) n) (3 * t + 3) (2 * t + 2) (lt3 t) (lt3' t)]
  have h := congrArg Prod.fst h2
  rw [show castZ (Fq2Inst.ffq (3 * t + 2) hpr n hn).K = castZ (Quad.quad (ring (2 * (3 * t + 2) + 1 + 1)) n) from rfl, FinCast.castZ_quad] at h
  refine h.trans ?_
  show (ring (2 * (3 * t + 2) + 1 + 1)).neg (nsm (Quad.quad (ring (2 * (3 * t + 2) + 1 + 1)) n) _ ((ring (2 * (3 * t + 2) + 1 + 1)).one, (ring (2 * (3 * t + 2) + 1 + 1)).zero)).1 = _
  rw [QuadFrob.nsm_quad]

/-- **y² = x³ + 1 at split p = 6t + 7: S₂ ≡ −S₁² (mod p).** -/
theorem split_hasse3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) (n : Zp (2 * (3 * t + 2) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) n (3 * t + 2 + 1) = (ring (2 * (3 * t + 2) + 1 + 1)).neg (ring (2 * (3 * t + 2) + 1 + 1)).one) :
    castZ (ring (2 * (3 * t + 2) + 1 + 1)) (FinCubic.psi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.one)
      = (ring (2 * (3 * t + 2) + 1 + 1)).neg ((ring (2 * (3 * t + 2) + 1 + 1)).mul (castZ (ring (2 * (3 * t + 2) + 1 + 1)) (CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one)) (castZ (ring (2 * (3 * t + 2) + 1 + 1)) (CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one))) := by
  rw [cast_S2_3 t hpr n hn, cast_S1_3 t hpr, nsm_mul_nat, nsm_eq_mul _ _ (nsm _ _ _)]
  lib_ring

end SplitHasse3

#print axioms SplitHasse3.cast_chi_p
#print axioms SplitHasse3.hasse_eisen'
#print axioms SplitHasse3.cast_S1_3
#print axioms SplitHasse3.hF3
#print axioms SplitHasse3.ia3
#print axioms SplitHasse3.ic3
#print axioms SplitHasse3.lt3
#print axioms SplitHasse3.lt3'
#print axioms SplitHasse3.cast_S2_3
#print axioms SplitHasse3.split_hasse3

Listing 122: lang/HasseDavenport3.lean

lang/HasseDavenport3.lean

/-
  lang/HasseDavenport3 — T43, cycle 104: a(𝔽_{p²}) = a_p² − 2p for y² = x³ + 1 at every split prime p ≡ 1 (mod 3).

  −ψ_p(1) = 2a with p = a² + 3b², a ≡ 1 (mod 3) (CubicSign); −ψ_{p²}(1) = 2A with p² = A² + 3B², A ≡ 1 (mod 3) (FinCubicSign
  over 𝔽_{p²}); ψ_{p²} ≡ −ψ_p² (mod p) (SplitHasse3). Then A ≡ 2a² ≡ a² − 3b² (mod p); both are ≡ 1 (mod 3) and p ≡ 1 (mod 3),
  so they differ by 3ps, and (A − A')² + (A + A')² + 6B² + 6(2ab)² = 4p² leaves s = 0:
    finish3                  the integer argument
    hasse_davenport_split3   −ψ_{p²}(1) = ψ_p(1)² − 2p, i.e. a(𝔽_{p²}) = a_p² − 2p
  With CubicSuper2 (inert p) this holds at every prime p > 3: the Frobenius-square identity for the CM curve of ℚ(√−3).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.SplitHasse3
import lang.HasseDavenport
import lang.FinCubicSign
import lang.CubicSuper2

namespace HasseDavenport3

open FTAChain ZMod YMChain FiniteRecChain LibRing LibInt HasseDavenport

theorem finish3 (P a b A B r t : Int) (hP : P = 6 * t + 7) (hP0 : P ≠ 0) (hab : a * a + 3 * (b * b) = P)
    (ha3 : ∃ u, a - 1 = 3 * u) (hAB : A * A + 3 * (B * B) = P * P) (hA3 : ∃ v, A - 1 = 3 * v)
    (hr : A = 2 * a * a - P * r) : A = a * a - 3 * (b * b) := by
  match ha3, hA3 with
  | ⟨u, hu⟩, ⟨v, hv⟩ =>
    have hdiff : A - (a * a - 3 * (b * b)) = P * (1 - r) := by
      calc A - (a * a - 3 * (b * b)) = (2 * a * a - P * r) - (a * a - 3 * (b * b)) := by rw [hr]
        _ = (a * a + 3 * (b * b)) - P * r := by lib_int
        _ = P * (1 - r) := by rw [hab]; lib_int
    have ha' : a = 3 * u + 1 := by calc a = (a - 1) + 1 := by lib_int
                                         _ = 3 * u + 1 := by rw [hu]
    have hA' : A = 3 * v + 1 := by calc A = (A - 1) + 1 := by lib_int
                                        _ = 3 * v + 1 := by rw [hv]
    -- 1 − r = 3 s'
    have hs : 1 - r = 3 * (v - 3 * u * u - 2 * u + b * b - (2 * t + 2) * (1 - r)) := by
      calc 1 - r = P * (1 - r) - (6 * t + 6) * (1 - r) := by rw [hP]; lib_int
        _ = (A - (a * a - 3 * (b * b))) - (6 * t + 6) * (1 - r) := by rw [hdiff]
        _ = _ := by rw [hA', ha']; lib_int
    generalize v - 3 * u * u - 2 * u + b * b - (2 * t + 2) * (1 - r) = s at hs
    match FinCubicSign.sq_nn s with
    | ⟨0, h0⟩ =>
      have : s = 0 := sq_zero s h0
      calc A = (A - (a * a - 3 * (b * b))) + (a * a - 3 * (b * b)) := by lib_int
        _ = a * a - 3 * (b * b) := by rw [hdiff, hs, this]; lib_int
    | ⟨n' + 1, hn⟩ =>
      exfalso
      match FinCubicSign.sq_nn B, FinCubicSign.sq_nn (2 * a * b), FinCubicSign.sq_nn (A + (a * a - 3 * (b * b))), FinCubicSign.sq_nn P with
      | ⟨nB, hnB⟩, ⟨n2, hn2⟩, ⟨m, hm⟩, ⟨pp, hpp⟩ =>
        have key : Int.ofNat (5 * pp + 9 * pp * n' + m + 6 * nB + 6 * n2) = 0 := by
          show 5 * Int.ofNat pp + 9 * Int.ofNat pp * Int.ofNat n' + Int.ofNat m + 6 * Int.ofNat nB + 6 * Int.ofNat n2 = 0
          have e1 : (A - (a * a - 3 * (b * b))) * (A - (a * a - 3 * (b * b))) = 9 * (P * P) * (s * s) := by rw [hdiff, hs]; lib_int
          have e2 : (a * a - 3 * (b * b)) * (a * a - 3 * (b * b)) + 3 * ((2 * a * b) * (2 * a * b)) = P * P := by
            rw [← hab]; lib_int
          have hss : s * s = Int.ofNat n' + 1 := hn
          rw [← hpp, ← hm, ← hnB, ← hn2]
          calc 5 * (P * P) + 9 * (P * P) * Int.ofNat n' + (A + (a * a - 3 * (b * b))) * (A + (a * a - 3 * (b * b))) + 6 * (B * B) + 6 * (2 * a * b * (2 * a * b))
              = ((A - (a * a - 3 * (b * b))) * (A - (a * a - 3 * (b * b))) + (A + (a * a - 3 * (b * b))) * (A + (a * a - 3 * (b * b))))
                + 6 * (B * B) + 6 * (2 * a * b * (2 * a * b)) - 9 * (P * P) * (s * s) + 9 * (P * P) * Int.ofNat n' + 5 * (P * P) := by rw [e1]; lib_int
            _ = 2 * (A * A + 3 * (B * B)) + 2 * ((a * a - 3 * (b * b)) * (a * a - 3 * (b * b)) + 3 * ((2 * a * b) * (2 * a * b)))
                - 9 * (P * P) * (s * s) + 9 * (P * P) * Int.ofNat n' + 5 * (P * P) := by lib_int
            _ = 0 := by rw [hAB, e2, hss]; lib_int
        have hk := Int.ofNat.inj key
        match nat_sum_zero _ _ hk with
        | ⟨h1, _⟩ =>
          match nat_sum_zero _ _ h1 with
          | ⟨h2, _⟩ =>
            match nat_sum_zero _ _ h2 with
            | ⟨h3, _⟩ =>
              match nat_sum_zero _ _ h3 with
              | ⟨h4, _⟩ =>
                have hpp0 : pp = 0 := by
                  cases pp with
                  | zero => rfl
                  | succ q => exact Nat.noConfusion h4
                exact hP0 (sq_zero P (hpp.trans (by rw [hpp0]; rfl)))

/-- **a(𝔽_{p²}) = a_p² − 2p for y² = x³ + 1 at every split prime p = 6t + 7.** -/
theorem hasse_davenport_split3 (t : Nat) (hpr : ∀ d, Dv d (2 * (3 * t + 2) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * t + 2) + 1 + 1 + 1)) (n : Zp (2 * (3 * t + 2) + 1 + 1))
    (hn : Frobenius.pw (ring (2 * (3 * t + 2) + 1 + 1)) n (3 * t + 2 + 1) = (ring (2 * (3 * t + 2) + 1 + 1)).neg (ring (2 * (3 * t + 2) + 1 + 1)).one) :
    -FinCubic.psi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.one = (-CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one) * (-CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one) + -2 * Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) := by
  have hN' : 2 * (Fq2Inst.ffq (3 * t + 2) hpr n hn).h = 3 * (2 * (6 * t * t + 14 * t + 7) + 2) := by
    rw [CubicSuper2.h_eq (3 * t + 2) hpr n hn, SplitHasse3.hF3]; lib_nat
  match FinCubicSign.fin_cubic_exact (Fq2Inst.ffq (3 * t + 2) hpr n hn) _ hN', CubicSign.cubic_exact (3 * t + 2) hpr (2 * t + 2) (by lib_nat) with
  | ⟨A, B, hAB, hA3, hS2⟩, ⟨a, b, hab, ha3, hS1⟩ =>
    have hAB' : A * A + 3 * (B * B) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) := by
      rw [hAB, CubicSuper2.h_eq (3 * t + 2) hpr n hn, CubicSuper.sq_id]; rfl
    have hsp := SplitHasse3.split_hasse3 t hpr n hn
    generalize FinCubic.psi (Fq2Inst.ffq (3 * t + 2) hpr n hn) (Fq2Inst.ffq (3 * t + 2) hpr n hn).K.one = T2 at hS2 hsp ⊢
    generalize CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one = T1 at hS1 hsp ⊢
    have h0 : castZ (ring (2 * (3 * t + 2) + 1 + 1)) (T2 + T1 * T1) = (ring (2 * (3 * t + 2) + 1 + 1)).zero := by
      rw [IntCast.castZ_add, castZ_mul, hsp]; lib_ring
    match castZ_zero_dvd (2 * (3 * t + 2) + 1 + 1) _ h0 with
    | ⟨q, hq⟩ =>
      have hP : Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) = 6 * Int.ofNat t + 7 := by
        show 2 * (3 * Int.ofNat t + 2) + 1 + 1 + 1 = _; lib_int
      have e2 : T2 = -(2 * A) := by rw [← hS2]; lib_int
      have e1 : T1 = -(2 * a) := by rw [← hS1]; lib_int
      have hX : 2 * (2 * a * a - A) = Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * q := by rw [← hq, e2, e1]; lib_int
      have hr : A = 2 * a * a - Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) * ((3 * Int.ofNat t + 4) * q - (2 * a * a - A)) := by
        calc A = 2 * a * a - ((3 * Int.ofNat t + 4) * (2 * (2 * a * a - A)) - (6 * Int.ofNat t + 7) * (2 * a * a - A)) := by lib_int
          _ = _ := by rw [hX, hP]; lib_int
      have hP0 : Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
      have hfin := finish3 (Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1)) a b A B _ (Int.ofNat t) hP hP0 hab ha3 hAB' hA3 hr
      rw [e2, e1, hfin, ← hab]
      lib_int

end HasseDavenport3

#print axioms HasseDavenport3.finish3
#print axioms HasseDavenport3.hasse_davenport_split3

With the inert primes \(p\equiv 2\ (3)\) (where \(-\psi _{p^2}(1)=-2p\) and \(\psi _p(1)=0\)) and the split ones, splitting \(p=2k+3\) by \(k\bmod 3\) (\(k\equiv 0\) gives \(3\mid p\)):

\[ -\psi _{\mathbb F_p[\sqrt n]}(1)=\psi _{\mathbb F_p}(1)^2-2p\qquad \text {for every prime }p>3 . \]

Listing 123: lang/EveryPrime3.lean

lang/EveryPrime3.lean

/-
  lang/EveryPrime3 — T43, cycle 107: a(𝔽_{p²}) = a_p² − 2p for y² = x³ + 1 at every prime p > 3.

  One model 𝔽_{p²} = 𝔽_p[√n] for p = 2k + 3, k ≥ 1:
    nat_mod3        k = 3t, 3t + 1 or 3t + 2
    every_prime3    −ψ_{𝔽_{p²}}(1) = ψ_{𝔽_p}(1)² − 2p
  (k = 3t: p = 3(2t + 1) is not prime for t ≥ 1; k = 3t + 1: p ≡ 2 (mod 3), CubicSuper2; k = 3t + 2: p ≡ 1 (mod 3),
  HasseDavenport3.)

  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.HasseDavenport3
import lang.EveryPrime

namespace EveryPrime3

open FTAChain ZMod YMChain FiniteRecChain

theorem nat_mod3 : ∀ k : Nat, (∃ t, k = 3 * t) ∨ (∃ t, k = 3 * t + 1) ∨ (∃ t, k = 3 * t + 2)
  | 0 => Or.inl ⟨0, rfl⟩
  | k + 1 => by
    cases nat_mod3 k with
    | inl h => match h with | ⟨t, ht⟩ => exact Or.inr (Or.inl ⟨t, by rw [ht]⟩)
    | inr h =>
      cases h with
      | inl h => match h with | ⟨t, ht⟩ => exact Or.inr (Or.inr ⟨t, by rw [ht]⟩)
      | inr h => match h with | ⟨t, ht⟩ => exact Or.inl ⟨t + 1, by rw [ht]; lib_nat⟩

/-- **a(𝔽_{p²}) = a_p² − 2p for y² = x³ + 1 at every prime p > 3.** -/
theorem every_prime3 (k : Nat) (hk : 1 ≤ k) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = (2 * k + 1 + 1 + 1)) (n : Zp (2 * k + 1 + 1))
    (hn : Frobenius.pw (ring (2 * k + 1 + 1)) n (k + 1) = (ring (2 * k + 1 + 1)).neg (ring (2 * k + 1 + 1)).one) :
    -FinCubic.psi (Fq2Inst.ffq k hpr n hn) (Fq2Inst.ffq k hpr n hn).K.one = (-CubicJacob.psi k (ring (2 * k + 1 + 1)).one) * (-CubicJacob.psi k (ring (2 * k + 1 + 1)).one) + -2 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases nat_mod3 k with
  | inl h =>
    exfalso
    match h with
    | ⟨t, ht⟩ =>
      cases hpr 3 ⟨2 * t + 1, by rw [ht]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj (show (2 : Nat) + 1 = 0 + 1 from e))
      | inr e =>
        have e' : 2 * k = 0 := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj e.symm))
        cases k with
        | zero => exact Nat.not_succ_le_zero 0 hk
        | succ k' => exact Nat.noConfusion (show 2 * k' + 1 + 1 = 0 from e')
  | inr h =>
    cases h with
    | inl h =>
      match h with
      | ⟨t, ht⟩ =>
        subst ht
        exact CubicSuper2.base_change2 (3 * t + 1) (2 * t + 2) (by lib_nat) hpr n hn
    | inr h =>
      match h with
      | ⟨t, ht⟩ => subst ht; exact HasseDavenport3.hasse_davenport_split3 t hpr n hn

end EveryPrime3

#print axioms EveryPrime3.nat_mod3
#print axioms EveryPrime3.every_prime3

78.38 Deuring’s criterion: supersingular exactly at the inert primes

For every odd prime \(p\), and every prime \(p>3\) respectively,

\[ p\mid \textstyle \sum _{x\in \mathbb F_p}\chi (x^3-x)\iff p\equiv 3\ (4),\qquad p\mid \textstyle \sum _{x\in \mathbb F_p}\chi (x^3+1)\iff p\equiv 2\ (3). \]
The steps are
\[ \begin {aligned} &\text {inert }p:\ \text {the sum is }0,\\ &\text {split }p:\ \text {the sum is }-2a,\ a^2+cb^2=p\ (c=1,3),\ a\ne 0\ (a\ \text {odd, resp. }a\equiv 1\ (3)),\\ &p\mid 2a,\ p\ \text {odd}\Rightarrow a=-pq',\ q'\ne 0\Rightarrow p^2(n+1)+c\,b^2=p,\ \text {impossible for }p\ge 2 . \end {aligned} \]

Listing 124: lang/Deuring.lean

lang/Deuring.lean

/-
  lang/Deuring — T43, cycle 108: supersingular ⇔ inert for the two CM curves (Deuring's criterion, both directions).

  p = 2k + 3 prime, a_p = −S with S = Σ_{𝔽_p} χ(x³ − x), resp. ψ(1) = Σ_{𝔽_p} χ(x³ + 1).
    big_sq, not_div   a ≠ 0, a² + c b² = p ⇒ p ∤ 2a          (|a| < p)
    deuring_i         p | S  ⇔  p ≡ 3 (mod 4)                 (y² = x³ − x; split: S = −2a, a odd, a² + b² = p)
    deuring_omega     p | ψ(1)  ⇔  p ≡ 2 (mod 3), p > 3        (y² = x³ + 1; split: ψ(1) = −2a, a ≡ 1 (mod 3), a² + 3b² = p)
  The reduction is supersingular (a_p ≡ 0 mod p) exactly at the primes inert in the CM field — the GL2 statement whose
  GL1 side is the splitting law of ℚ(i), ℚ(√−3) (GaussSplit, Eisen).

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.EveryPrime3

namespace Deuring

open FTAChain ZMod YMChain FiniteRecChain LibInt

theorem succ_add_ne (a X : Nat) (h : a + (X + 1) = a) : False :=
  Nat.not_succ_le_self (a + X) (by
    show Nat.succ (a + X) ≤ a + X
    rw [show Nat.succ (a + X) = a + (X + 1) from rfl, h]; exact Nat.le_add_right a X)

theorem big_sq (k n m : Nat) (h : (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) * (n + 1) + m = 2 * k + 1 + 1 + 1) : False :=
  succ_add_ne (2 * k + 1 + 1 + 1) (4 * k * k + 10 * k + 5 + (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) * n + m) (by
    rw [show (2 * k + 1 + 1 + 1) + (4 * k * k + 10 * k + 5 + (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) * n + m + 1)
        = (2 * k + 1 + 1 + 1) * (2 * k + 1 + 1 + 1) * (n + 1) + m by lib_nat]
    exact h)

/-- **p ∤ 2a when a ≠ 0 and a² + c b² = p.** -/
theorem not_div (k c : Nat) (a b q : Int) (ha : a ≠ 0) (hab : a * a + Int.ofNat c * (b * b) = Int.ofNat (2 * k + 1 + 1 + 1))
    (hq : -(2 * a) = Int.ofNat (2 * k + 1 + 1 + 1) * q) : False := by
  have hP : Int.ofNat (2 * k + 1 + 1 + 1) = 2 * (Int.ofNat k + 1) + 1 := by show 2 * Int.ofNat k + 1 + 1 + 1 = _; lib_int
  match GaussInt.int_even_odd q with
  | Or.inr ⟨q', hq'⟩ =>
    exact GaussInt.int_odd_ne_even (2 * (Int.ofNat k + 1) * q' + Int.ofNat k + 1 + q') (-a)
      (by rw [show 2 * -a = -(2 * a) by lib_int, hq, hq', hP]; lib_int)
  | Or.inl ⟨q', hq'⟩ =>
    have ha' : a = -(Int.ofNat (2 * k + 1 + 1 + 1) * q') :=
      FinPrimary.two_cancel _ _ (by
        calc 2 * a = -(-(2 * a)) := by lib_int
          _ = 2 * -(Int.ofNat (2 * k + 1 + 1 + 1) * q') := by rw [hq, hq']; lib_int)
    match FinCubicSign.sq_nn q', FinCubicSign.sq_nn b with
    | ⟨0, h0⟩, _ => exact ha (by rw [ha', HasseDavenport.sq_zero q' h0]; lib_int)
    | ⟨n + 1, hn⟩, ⟨nb, hnb⟩ =>
      apply big_sq k n (c * nb)
      apply Int.ofNat.inj
      show Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1) * (Int.ofNat n + 1) + Int.ofNat c * Int.ofNat nb = _
      have : q' * q' = Int.ofNat n + 1 := hn
      calc Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1) * (Int.ofNat n + 1) + Int.ofNat c * Int.ofNat nb
          = Int.ofNat (2 * k + 1 + 1 + 1) * Int.ofNat (2 * k + 1 + 1 + 1) * (q' * q') + Int.ofNat c * (b * b) := by rw [this, hnb]
        _ = a * a + Int.ofNat c * (b * b) := by rw [ha']; lib_int
        _ = _ := hab


/-- **Deuring for y² = x³ − x: p | a_p ⇔ p ≡ 3 (mod 4).** -/
theorem deuring_i (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) :
    (∃ q : Int, sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k) = Int.ofNat (2 * k + 1 + 1 + 1) * q)
      ↔ ∃ j, k = 2 * j := by
  constructor
  · intro ⟨q, hq⟩
    cases EveryPrime.nat_even_odd k with
    | inl h => exact h
    | inr h =>
      exfalso
      match h with
      | ⟨j, hj⟩ =>
        subst hj
        match SignCM3.ap_primary j hpr with
        | ⟨a, b, hab, ⟨⟨e, he⟩, ⟨t, ht⟩⟩, hS⟩ =>
          have he' : b = 2 * e := he
          have ht' : a + b - 1 = 4 * t := ht
          have hS' : -sumK LibInt.intR (fun x => QChar.chi (2 * j + 1) (CMInert.cub (ring (2 * (2 * j + 1) + 1 + 1)) x)) (ConicSum.L (2 * j + 1)) = 2 * a := hS
          have ha : a ≠ 0 := fun h0 => GaussInt.int_odd_ne_even (2 * t - e) 0 (by
            calc 2 * (2 * t - e) + 1 = (a + b - 1) - b + 1 := by rw [ht', he']; lib_int
              _ = a := by lib_int
              _ = 2 * 0 := by rw [h0]; lib_int)
          generalize sumK LibInt.intR (fun x => QChar.chi (2 * j + 1) (CMInert.cub (ring (2 * (2 * j + 1) + 1 + 1)) x)) (ConicSum.L (2 * j + 1)) = S at hq hS'
          exact not_div (2 * j + 1) 1 a b q ha (by show a * a + 1 * (b * b) = _; rw [← hab]; lib_int) (by rw [← hS', hq]; lib_int)
  · intro ⟨j, hj⟩
    subst hj
    exact ⟨0, by rw [InertFq.s1_inert j hpr]; lib_int⟩

/-- **Deuring for y² = x³ + 1: p | a_p ⇔ p ≡ 2 (mod 3)** (p = 2k + 3 > 3). -/
theorem deuring_omega (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) (hk : 1 ≤ k) :
    (∃ q : Int, CubicJacob.psi k (ring (2 * k + 1 + 1)).one = Int.ofNat (2 * k + 1 + 1 + 1) * q) ↔ ∃ t, k = 3 * t + 1 := by
  constructor
  · intro ⟨q, hq⟩
    cases EveryPrime3.nat_mod3 k with
    | inl h =>
      exfalso
      match h with
      | ⟨t, ht⟩ =>
        cases hpr 3 ⟨2 * t + 1, by rw [ht]; lib_nat⟩ with
        | inl e => exact Nat.noConfusion (Nat.succ.inj (show (2 : Nat) + 1 = 0 + 1 from e))
        | inr e =>
          have e' : 2 * k = 0 := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj e.symm))
          cases k with
          | zero => exact Nat.not_succ_le_zero 0 hk
          | succ k' => exact Nat.noConfusion (show 2 * k' + 1 + 1 = 0 from e')
    | inr h =>
      cases h with
      | inl h => exact h
      | inr h =>
        exfalso
        match h with
        | ⟨t, ht⟩ =>
          subst ht
          match CubicSign.cubic_exact (3 * t + 2) hpr (2 * t + 2) (by lib_nat) with
          | ⟨a, b, hab, ⟨u, hu⟩, hS⟩ =>
            have hS' : -CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one = 2 * a := hS
            have ha : a ≠ 0 := fun h0 => CubicSuper.three_ne_one (-u) (by
              calc 3 * -u = -(3 * u) := by lib_int
                _ = -(a - 1) := by rw [hu]
                _ = 1 := by rw [h0]; lib_int)
            generalize CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one = S at hq hS'
            exact not_div (3 * t + 2) 3 a b q ha hab (by rw [← hS', hq]; lib_int)
  · intro ⟨t, ht⟩
    subst ht
    exact ⟨0, by rw [SexticFamily.psi_inert (3 * t + 1) hpr (4 * t + 3) (by lib_nat)]; lib_int⟩

end Deuring

#print axioms Deuring.succ_add_ne
#print axioms Deuring.big_sq
#print axioms Deuring.not_div
#print axioms Deuring.deuring_i
#print axioms Deuring.deuring_omega

At every good prime also \(a_p^2+s=4p\) with \(s\ge 0\): \(s=4p\) at inert \(p\), and \(s=4b^2\), resp. \(12b^2\), at split \(p\), so \(|a_p|\le 2\sqrt p\).

Listing 125: lang/HasseBoundP.lean

lang/HasseBoundP.lean

/-
  lang/HasseBoundP — T43, cycle 109: Hasse's bound a_p² ≤ 4p (Ramanujan for the CM forms) at every prime, both curves.

    hasse_i       ∃ s, S² + s = 4p,  S = Σ_{𝔽_p} χ(x³ − x),  every odd p        (inert: S = 0; split: S = −2a, s = 4b²)
    hasse_omega   ∃ s, ψ(1)² + s = 4p, ψ(1) = Σ_{𝔽_p} χ(x³ + 1), every p > 3    (inert: 0; split: ψ(1) = −2a, s = 12b²)
  |a_p| ≤ 2√p, i.e. the Satake parameters of the CM newforms have absolute value √p (Ramanujan–Petersson for these
  forms), at every good prime, as integer statements.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Deuring

namespace HasseBoundP

open FTAChain ZMod YMChain FiniteRecChain LibInt

theorem hasse_i (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) :
    ∃ s : Nat, sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k)
      * sumK LibInt.intR (fun x => QChar.chi k (CMInert.cub (ring (2 * k + 1 + 1)) x)) (ConicSum.L k)
      + Int.ofNat s = 4 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases EveryPrime.nat_even_odd k with
  | inl h =>
    match h with
    | ⟨j, hj⟩ => subst hj; exact ⟨4 * (2 * (2 * j) + 1 + 1 + 1), by rw [InertFq.s1_inert j hpr, show Int.ofNat (4 * (2 * (2 * j) + 1 + 1 + 1)) = 4 * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) from rfl]; lib_int⟩
  | inr h =>
    match h with
    | ⟨j, hj⟩ =>
      subst hj
      match SignCM3.ap_primary j hpr with
      | ⟨a, b, hab, _, hS⟩ =>
        have hS' : -sumK LibInt.intR (fun x => QChar.chi (2 * j + 1) (CMInert.cub (ring (2 * (2 * j + 1) + 1 + 1)) x)) (ConicSum.L (2 * j + 1)) = 2 * a := hS
        generalize sumK LibInt.intR (fun x => QChar.chi (2 * j + 1) (CMInert.cub (ring (2 * (2 * j + 1) + 1 + 1)) x)) (ConicSum.L (2 * j + 1)) = S at hS' ⊢
        match FinCubicSign.sq_nn b with
        | ⟨nb, hnb⟩ =>
          refine ⟨4 * nb, ?_⟩
          have e : S = -(2 * a) := by rw [← hS']; lib_int
          rw [e, ← hab, show Int.ofNat (4 * nb) = 4 * Int.ofNat nb from rfl, ← hnb]; lib_int

theorem hasse_omega (k : Nat) (hk : 1 ≤ k) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1) :
    ∃ s : Nat, CubicJacob.psi k (ring (2 * k + 1 + 1)).one * CubicJacob.psi k (ring (2 * k + 1 + 1)).one
      + Int.ofNat s = 4 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases EveryPrime3.nat_mod3 k with
  | inl h =>
    exfalso
    match h with
    | ⟨t, ht⟩ =>
      cases hpr 3 ⟨2 * t + 1, by rw [ht]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj (show (2 : Nat) + 1 = 0 + 1 from e))
      | inr e =>
        have e' : 2 * k = 0 := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj e.symm))
        cases k with
        | zero => exact Nat.not_succ_le_zero 0 hk
        | succ k' => exact Nat.noConfusion (show 2 * k' + 1 + 1 = 0 from e')
  | inr h =>
    cases h with
    | inl h =>
      match h with
      | ⟨t, ht⟩ =>
        subst ht
        exact ⟨4 * (2 * (3 * t + 1) + 1 + 1 + 1), by
          rw [SexticFamily.psi_inert (3 * t + 1) hpr (4 * t + 3) (by lib_nat), show Int.ofNat (4 * (2 * (3 * t + 1) + 1 + 1 + 1)) = 4 * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) from rfl]; lib_int⟩
    | inr h =>
      match h with
      | ⟨t, ht⟩ =>
        subst ht
        match CubicSign.cubic_exact (3 * t + 2) hpr (2 * t + 2) (by lib_nat) with
        | ⟨a, b, hab, _, hS⟩ =>
          have hS' : -CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one = 2 * a := hS
          generalize CubicJacob.psi (3 * t + 2) (ring (2 * (3 * t + 2) + 1 + 1)).one = S at hS' ⊢
          match FinCubicSign.sq_nn b with
          | ⟨nb, hnb⟩ =>
            refine ⟨12 * nb, ?_⟩
            have e : S = -(2 * a) := by rw [← hS']; lib_int
            rw [e, ← hab, show Int.ofNat (12 * nb) = 12 * Int.ofNat nb from rfl, ← hnb]; lib_int

end HasseBoundP

#print axioms HasseBoundP.hasse_i
#print axioms HasseBoundP.hasse_omega

The same bound holds for every twist: \(\varphi _p(D)^2+s=4p\) for \(y^2=x^3+Dx\) (\(s=4u^2\) or \(4v^2\) from the norm form \(u^2+v^2=p\)) and \(\psi _p(D)^2+s=4p\) for \(y^2=x^3+D\) (\(s=3B^2\) from \(\psi (D)^2+3B^2=4p\)).

Listing 126: lang/FamilyBound.lean

lang/FamilyBound.lean

/-
  lang/FamilyBound — T43, cycle 121: Hasse's bound for every quartic and every sextic twist over 𝔽_p.

    quartic_bound   φ_p(D)² + s = 4p   (y² = x³ + Dx, every odd p, D ≠ 0)          (QuarticFamily.family_trace; inert: 0)
    sextic_bound    ψ_p(D)² + s = 4p   (y² = x³ + D, every p > 3, D ≠ 0)           (FinCubicNorm over 𝔽_p; inert: 0)
  |a_p| ≤ 2√p for all CM curves with j = 1728 or j = 0 over ℚ at every good prime — Ramanujan–Petersson for the
  corresponding CM newforms, as integer statements.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.SexticHD
import lang.HasseBoundP

namespace FamilyBound

open FTAChain ZMod YMChain FiniteRecChain LibInt

theorem quartic_bound (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    ∃ s : Nat, Jacobsthal.phi k D * Jacobsthal.phi k D + Int.ofNat s = 4 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases EveryPrime.nat_even_odd k with
  | inl h =>
    match h with
    | ⟨j, hj⟩ =>
      subst hj
      refine ⟨4 * (2 * (2 * j) + 1 + 1 + 1), ?_⟩
      rw [QuarticFamily.phi_inert (2 * j) hpr (BaseChange.chi_negone j (TwoPower.negone_odd' (2 * (2 * j) + 1) j)) D,
        show Int.ofNat (4 * (2 * (2 * j) + 1 + 1 + 1)) = 4 * Int.ofNat (2 * (2 * j) + 1 + 1 + 1) from rfl]
      lib_int
  | inr h =>
    match h with
    | ⟨j, hj⟩ =>
      subst hj
      match QuarticFamily.family_trace j hpr D hD with
      | ⟨u, v, huv, hc⟩ =>
        have hP : Int.ofNat u * Int.ofNat u + Int.ofNat v * Int.ofNat v = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) :=
          congrArg Int.ofNat huv
        generalize Jacobsthal.phi (2 * j + 1) D = S at hc ⊢
        have h4 : Int.ofNat (4 * (v * v)) = 4 * (Int.ofNat v * Int.ofNat v) := rfl
        have h4' : Int.ofNat (4 * (u * u)) = 4 * (Int.ofNat u * Int.ofNat u) := rfl
        cases hc with
        | inl e => exact ⟨4 * (v * v), by rw [e, h4, ← hP]; show (2 * Int.ofNat u) * (2 * Int.ofNat u) + _ = _; lib_int⟩
        | inr hc =>
          cases hc with
          | inl e => exact ⟨4 * (v * v), by rw [e, h4, ← hP]; show -(2 * Int.ofNat u) * -(2 * Int.ofNat u) + _ = _; lib_int⟩
          | inr hc =>
            cases hc with
            | inl e => exact ⟨4 * (u * u), by rw [e, h4', ← hP]; show (2 * Int.ofNat v) * (2 * Int.ofNat v) + _ = _; lib_int⟩
            | inr e => exact ⟨4 * (u * u), by rw [e, h4', ← hP]; show -(2 * Int.ofNat v) * -(2 * Int.ofNat v) + _ = _; lib_int⟩

theorem sextic_bound (k : Nat) (hk : 1 ≤ k) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    ∃ s : Nat, FinCubic.psi (FinField.ffp k hpr) D * FinCubic.psi (FinField.ffp k hpr) D + Int.ofNat s
      = 4 * Int.ofNat (2 * k + 1 + 1 + 1) := by
  cases EveryPrime3.nat_mod3 k with
  | inl h =>
    exfalso
    match h with
    | ⟨t, ht⟩ =>
      cases hpr 3 ⟨2 * t + 1, by rw [ht]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj (show (2 : Nat) + 1 = 0 + 1 from e))
      | inr e =>
        have e' : 2 * k = 0 := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj e.symm))
        cases k with
        | zero => exact Nat.not_succ_le_zero 0 hk
        | succ k' => exact Nat.noConfusion (show 2 * k' + 1 + 1 = 0 from e')
  | inr h =>
    cases h with
    | inl h =>
      match h with
      | ⟨t, ht⟩ =>
        subst ht
        refine ⟨4 * (2 * (3 * t + 1) + 1 + 1 + 1), ?_⟩
        rw [show FinCubic.psi (FinField.ffp (3 * t + 1) hpr) D = CubicJacob.psi (3 * t + 1) D from rfl,
          SexticFamily.psi_inert (3 * t + 1) hpr (4 * t + 3) (by lib_nat) D,
          show Int.ofNat (4 * (2 * (3 * t + 1) + 1 + 1 + 1)) = 4 * Int.ofNat (2 * (3 * t + 1) + 1 + 1 + 1) from rfl]
        lib_int
    | inr h =>
      match h with
      | ⟨t, ht⟩ =>
        subst ht
        match FinCubicNorm.norm_twist (FinField.ffp (3 * t + 2) hpr) (2 * t + 2) (by show 2 * (3 * t + 2 + 1) = _; lib_nat) D hD with
        | ⟨b, hb⟩ =>
          match FinCubicSign.sq_nn b with
          | ⟨nb, hnb⟩ =>
            refine ⟨3 * nb, ?_⟩
            rw [show Int.ofNat (3 * nb) = 3 * Int.ofNat nb from rfl, ← hnb]
            exact hb

end FamilyBound

#print axioms FamilyBound.quartic_bound
#print axioms FamilyBound.sextic_bound

Deuring’s criterion holds for every twist: at split \(p\), \(\varphi _p(D)=\pm 2w\) with \(w^2+w'^2=p\) (\(w\ne 0\) since \(p\) is not a square) and \(\psi _p(D)^2+3B^2=4p\) (\(\psi \ne 0\) since \(3\nmid p\)), so \(p\nmid a_p\); at inert \(p\), \(a_p=0\).

Listing 127: lang/DeuringTwist.lean

lang/DeuringTwist.lean

/-
  lang/DeuringTwist — T43, cycle 132: Deuring's criterion for every twist y² = x³ + Dx and y² = x³ + D (D ≠ 0).

    deuring_quartic   p | φ_p(D)  ⇔  p ≡ 3 (mod 4)          (split: φ_p(D) = ±2w, w² + w'² = p, w ≠ 0)
    deuring_sextic    p | ψ_p(D)  ⇔  p ≡ 2 (mod 3), p > 3   (split: ψ_p(D)² + 3B² = 4p, so ψ ≠ 0 and |ψ| < p)
  Every CM curve over ℚ with j = 1728 or j = 0 has supersingular reduction exactly at the primes inert in its CM field.

  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.FamilyBound
import lang.Deuring

namespace DeuringTwist

open FTAChain ZMod YMChain FiniteRecChain LibInt

theorem deuring_quartic (k : Nat) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    (∃ q : Int, Jacobsthal.phi k D = Int.ofNat (2 * k + 1 + 1 + 1) * q) ↔ ∃ j, k = 2 * j := by
  constructor
  · intro ⟨q, hq⟩
    cases EveryPrime.nat_even_odd k with
    | inl h => exact h
    | inr h =>
      exfalso
      match h with
      | ⟨j, hj⟩ =>
        subst hj
        match QuarticFamily.family_trace j hpr D hD with
        | ⟨u, v, huv, hc⟩ =>
          have hP : Int.ofNat u * Int.ofNat u + 1 * (Int.ofNat v * Int.ofNat v) = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) := by
            rw [show (1 : Int) * (Int.ofNat v * Int.ofNat v) = Int.ofNat v * Int.ofNat v by lib_int]; exact congrArg Int.ofNat huv
          have hP' : Int.ofNat v * Int.ofNat v + 1 * (Int.ofNat u * Int.ofNat u) = Int.ofNat (2 * (2 * j + 1) + 1 + 1 + 1) := by
            rw [← hP]; lib_int
          have hu0 : ∀ w w' : Nat, w * w + w' * w' = 2 * (2 * j + 1) + 1 + 1 + 1 → Int.ofNat w ≠ 0 := fun w w' hw e => by
            have hw0 : w = 0 := Int.ofNat.inj e
            rw [hw0] at hw
            match w' with
            | 0 => exact Nat.noConfusion hw
            | w'' + 1 =>
              exact TwoSquares.not_square (2 * (2 * j + 1) + 1 + 1) w'' (Nat.le_add_left 1 _) (fun d hd => hpr d hd)
                ((show (w'' + 1) * (w'' + 1) = 0 * 0 + (w'' + 1) * (w'' + 1) by lib_nat).trans hw)
          have huv' : v * v + u * u = 2 * (2 * j + 1) + 1 + 1 + 1 := by rw [← huv]; lib_nat
          generalize Jacobsthal.phi (2 * j + 1) D = S at hq hc
          rcases hc with e | e | e | e
          · exact Deuring.not_div (2 * j + 1) 1 (-Int.ofNat u) (Int.ofNat v) q
              (fun h0 => hu0 u v huv (by rw [show Int.ofNat u = -(-Int.ofNat u) by lib_int, h0]; rfl))
              (by show (-Int.ofNat u) * (-Int.ofNat u) + 1 * (Int.ofNat v * Int.ofNat v) = _; rw [← hP]; lib_int) (by rw [← hq, e]; show -(2 * -Int.ofNat u) = 2 * Int.ofNat u; lib_int)
          · exact Deuring.not_div (2 * j + 1) 1 (Int.ofNat u) (Int.ofNat v) q (hu0 u v huv)
              hP (by rw [← hq, e]; show -(2 * Int.ofNat u) = -(2 * Int.ofNat u); rfl)
          · exact Deuring.not_div (2 * j + 1) 1 (-Int.ofNat v) (Int.ofNat u) q
              (fun h0 => hu0 v u huv' (by rw [show Int.ofNat v = -(-Int.ofNat v) by lib_int, h0]; rfl))
              (by show (-Int.ofNat v) * (-Int.ofNat v) + 1 * (Int.ofNat u * Int.ofNat u) = _; rw [← hP']; lib_int) (by rw [← hq, e]; show -(2 * -Int.ofNat v) = 2 * Int.ofNat v; lib_int)
          · exact Deuring.not_div (2 * j + 1) 1 (Int.ofNat v) (Int.ofNat u) q (hu0 v u huv')
              hP' (by rw [← hq, e]; show -(2 * Int.ofNat v) = -(2 * Int.ofNat v); rfl)
  · intro ⟨j, hj⟩
    subst hj
    exact ⟨0, by rw [QuarticFamily.phi_inert (2 * j) hpr (BaseChange.chi_negone j (TwoPower.negone_odd' (2 * (2 * j) + 1) j)) D]; lib_int⟩

theorem deuring_sextic (k : Nat) (hk : 1 ≤ k) (hpr : ∀ d, Dv d (2 * k + 1 + 1 + 1) → d = 1 ∨ d = 2 * k + 1 + 1 + 1)
    (D : Zp (2 * k + 1 + 1)) (hD : D ≠ (ring (2 * k + 1 + 1)).zero) :
    (∃ q : Int, FinCubic.psi (FinField.ffp k hpr) D = Int.ofNat (2 * k + 1 + 1 + 1) * q) ↔ ∃ t, k = 3 * t + 1 := by
  constructor
  · intro ⟨q, hq⟩
    cases EveryPrime3.nat_mod3 k with
    | inl h =>
      exfalso
      match h with
      | ⟨t, ht⟩ =>
        cases hpr 3 ⟨2 * t + 1, by rw [ht]; lib_nat⟩ with
        | inl e => exact Nat.noConfusion (Nat.succ.inj (show (2 : Nat) + 1 = 0 + 1 from e))
        | inr e =>
          have e' : 2 * k = 0 := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj e.symm))
          cases k with
          | zero => exact Nat.not_succ_le_zero 0 hk
          | succ k' => exact Nat.noConfusion (show 2 * k' + 1 + 1 = 0 from e')
    | inr h =>
      cases h with
      | inl h => exact h
      | inr h =>
        exfalso
        match h with
        | ⟨t, ht⟩ =>
          subst ht
          match FinCubicNorm.norm_twist (FinField.ffp (3 * t + 2) hpr) (2 * t + 2) (by show 2 * (3 * t + 2 + 1) = _; lib_nat) D hD with
          | ⟨B, hB⟩ =>
            have hB' : FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D * FinCubic.psi (FinField.ffp (3 * t + 2) hpr) D + 3 * (B * B)
                = 4 * Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) := hB
            rw [hq] at hB'
            match FinCubicSign.sq_nn q, FinCubicSign.sq_nn B with
            | ⟨0, h0⟩, ⟨nb, hnb⟩ =>
              rw [HasseDavenport.sq_zero q h0] at hB'
              exact CubicSuper.three_ne_one (B * B - 8 * Int.ofNat t - 9) (by
                have hP : Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) = 6 * Int.ofNat t + 7 := by show 2 * (3 * Int.ofNat t + 2) + 1 + 1 + 1 = _; lib_int
                rw [hP] at hB'
                calc 3 * (B * B - 8 * Int.ofNat t - 9) = ((6 * Int.ofNat t + 7) * 0 * ((6 * Int.ofNat t + 7) * 0) + 3 * (B * B)) - 24 * Int.ofNat t - 27 := by lib_int
                  _ = 1 := by rw [hB']; lib_int)
            | ⟨n + 1, hn⟩, ⟨nb, hnb⟩ =>
              apply Deuring.succ_add_ne (4 * (6 * t + 7)) ((36 * t * t + 60 * t + 20) + (6 * t + 7) * (6 * t + 7) * n + 3 * nb)
              apply Int.ofNat.inj
              show 4 * (6 * Int.ofNat t + 7) + ((36 * Int.ofNat t * Int.ofNat t + 60 * Int.ofNat t + 20) + (6 * Int.ofNat t + 7) * (6 * Int.ofNat t + 7) * Int.ofNat n + 3 * Int.ofNat nb + 1)
                = 4 * (6 * Int.ofNat t + 7)
              have hP : Int.ofNat (2 * (3 * t + 2) + 1 + 1 + 1) = 6 * Int.ofNat t + 7 := by show 2 * (3 * Int.ofNat t + 2) + 1 + 1 + 1 = _; lib_int
              rw [hP] at hB'
              have hqq : q * q = Int.ofNat n + 1 := hn
              calc 4 * (6 * Int.ofNat t + 7) + ((36 * Int.ofNat t * Int.ofNat t + 60 * Int.ofNat t + 20) + (6 * Int.ofNat t + 7) * (6 * Int.ofNat t + 7) * Int.ofNat n + 3 * Int.ofNat nb + 1)
                  = (6 * Int.ofNat t + 7) * (6 * Int.ofNat t + 7) * (q * q) + 3 * (B * B) := by rw [hqq, hnb]; lib_int
                _ = 4 * (6 * Int.ofNat t + 7) := by rw [← hB']; lib_int
  · intro ⟨t, ht⟩
    subst ht
    exact ⟨0, by
      rw [show FinCubic.psi (FinField.ffp (3 * t + 1) hpr) D = CubicJacob.psi (3 * t + 1) D from rfl,
        SexticFamily.psi_inert (3 * t + 1) hpr (4 * t + 3) (by lib_nat) D]; lib_int⟩

end DeuringTwist

#print axioms DeuringTwist.deuring_quartic
#print axioms DeuringTwist.deuring_sextic

78.39 GL\(_2\) CM: the trace of an induced representation

For \(H\subset G\) of index \(2\), \(\sigma \in G\setminus H\) and a class function \(\psi \) on \(H\),

\[ \operatorname {Ind}\psi (x)=|H|\bigl (\psi (x)+\psi (\sigma x\sigma ^{-1})\bigr )\ (x\in H),\qquad \operatorname {Ind}\psi (x)=0\ (x\notin H). \]
The steps are
\[ \begin {aligned} \operatorname {Ind}\psi (x)&=\textstyle \sum _{g\in G}\psi ^\circ (gxg^{-1}),\quad G=H\cup \sigma H,\\ x\notin H&\Rightarrow gxg^{-1}\notin H\ \text {for all}\ g,\\ x\in H&\Rightarrow \textstyle \sum _{h\in H}\psi (hxh^{-1})+\sum _{h\in H}\psi (\sigma h x h^{-1}\sigma ^{-1})=|H|\psi (x)+|H|\psi (\sigma x\sigma ^{-1}). \end {aligned} \]

Listing 128: lang/Dihedral.lean

lang/Dihedral.lean

/-
  lang/Dihedral — T43, cycle 5 (GL2 over ℚ, CM / dihedral case from GL1 over K).

  G = Gal(L/ℚ), H = Gal(L/K) of index 2 (K imaginary quadratic), σ ∈ G \ H. For a character ψ of H
  (a Hecke character of K, through GL1 over K), the induced representation Ind_H^G ψ has trace
      split p  (Frob_p ∈ H):   ψ(Frob) + ψ(σ Frob σ⁻¹)      = ψ(p) + ψ(p̄)
      inert p  (Frob_p ∉ H):   0
  which is the theta series a(p) of ψ. In the normalisation of FiniteRecChain (Ind = |H| × trace):
    ind_split    x ∈ H  ⇒  ind ψ x = |H| ψ(x) + |H| ψ(σ x σ⁻¹)
    ind_inert    x ∉ H  ⇒  ind ψ x = 0
  Hypotheses: G = H ∪ σH as lists (hsplit), H normal and closed under conjugation, ψ a class
  function on H. Checked first on y² = x³ − x (CM by ℚ(i)): a_p = π + π̄ (π ≡ 1 mod 2+2i) for
  p ≡ 1 (4) and a_p = 0 for p ≡ 3 (4), all p < 400, exactly.

  Lean 4 core only, proofs by hand, no axioms.
-/
import FiniteRecChain

namespace Dihedral

open YMChain FiniteRecChain

variable {R : Type} (Rg : DetChain.CRing R) {G : Type} [DecidableEq G] (K : Grp G)

def conjG (a y : G) : G := K.mul (K.mul a y) (K.inv a)

omit [DecidableEq G] in
theorem sum_const {A : Type} (c : R) (f : A → R) : ∀ l : List A, (∀ a, memL a l → f a = c) →
    sumK Rg f l = Rg.mul (sumK Rg (fun _ => Rg.one) l) c
  | [], _ => (DetChain.zero_mul' Rg c).symm
  | a :: l, h => by
    show Rg.add (f a) (sumK Rg f l) = Rg.mul (Rg.add Rg.one (sumK Rg (fun _ => Rg.one) l)) c
    rw [h a (Or.inl rfl), sum_const c f l (fun b hb => h b (Or.inr hb)), DetChain.right_distrib, Rg.one_mul]

variable (inH : G → Bool)

omit [DecidableEq G] in
/-- **Split primes.** -/
theorem ind_split (els Hl : List G) (σ : G) (ψ : G → R)
    (hsplit : ∀ F : G → R, sumK Rg F els = Rg.add (sumK Rg F Hl) (sumK Rg (fun h => F (K.mul σ h)) Hl))
    (hHl : ∀ h, memL h Hl → inH h = true)
    (hcl : ∀ h y, inH h = true → inH y = true → inH (conjG K h y) = true)
    (hnorm : ∀ y, inH y = true → inH (conjG K σ y) = true)
    (hcls : ∀ h y, inH h = true → inH y = true → ψ (conjG K h y) = ψ y)
    (x : G) (hx : inH x = true) :
    ind Rg K els inH ψ x
      = Rg.add (Rg.mul (sumK Rg (fun _ => Rg.one) Hl) (ψ x))
               (Rg.mul (sumK Rg (fun _ => Rg.one) Hl) (ψ (conjG K σ x))) := by
  unfold ind
  rw [hsplit]
  have e1 : sumK Rg (fun g => ext0 Rg inH ψ (K.mul (K.mul g x) (K.inv g))) Hl
      = Rg.mul (sumK Rg (fun _ => Rg.one) Hl) (ψ x) :=
    sum_const Rg (ψ x) _ Hl (fun h hh => by
      show (if inH (conjG K h x) = true then ψ (conjG K h x) else Rg.zero) = ψ x
      rw [ite_pos' _ _ (hcl h x (hHl h hh) hx)]
      exact hcls h x (hHl h hh) hx)
  have e2 : sumK Rg (fun h => ext0 Rg inH ψ (K.mul (K.mul (K.mul σ h) x) (K.inv (K.mul σ h)))) Hl
      = Rg.mul (sumK Rg (fun _ => Rg.one) Hl) (ψ (conjG K σ x)) :=
    sum_const Rg (ψ (conjG K σ x)) _ Hl (fun h hh => by
      -- (σh) x (σh)⁻¹ = (σ h σ⁻¹) (σ x σ⁻¹) (σ h σ⁻¹)⁻¹
      have hc : K.mul (K.mul (K.mul σ h) x) (K.inv (K.mul σ h)) = conjG K (conjG K σ h) (conjG K σ x) := by
        have cc := conj_conj K (conjG K σ h) σ x
        have e : K.mul (conjG K σ h) σ = K.mul σ h := by
          show K.mul (K.mul (K.mul σ h) (K.inv σ)) σ = K.mul σ h
          rw [K.assoc, K.inv_mul, K.mul_one]
        rw [e] at cc
        exact cc.symm
      show ext0 Rg inH ψ (K.mul (K.mul (K.mul σ h) x) (K.inv (K.mul σ h))) = ψ (conjG K σ x)
      rw [hc]
      have hh' := hnorm h (hHl h hh)
      have hx' := hnorm x hx
      show (if inH (conjG K (conjG K σ h) (conjG K σ x)) = true then ψ (conjG K (conjG K σ h) (conjG K σ x))
        else Rg.zero) = ψ (conjG K σ x)
      rw [ite_pos' _ _ (hcl _ _ hh' hx')]
      exact hcls _ _ hh' hx')
  rw [e1, e2]

omit [DecidableEq G] in
/-- **Inert primes.** -/
theorem ind_inert (els : List G) (ψ : G → R)
    (hout : ∀ g y, inH y = false → inH (conjG K g y) = false) (x : G) (hx : inH x = false) :
    ind Rg K els inH ψ x = Rg.zero := by
  unfold ind
  have : ∀ l : List G, sumK Rg (fun g => ext0 Rg inH ψ (K.mul (K.mul g x) (K.inv g))) l = Rg.zero := by
    intro l
    induction l with
    | nil => rfl
    | cons g l ih =>
      have hf : ¬ inH (conjG K g x) = true := by rw [hout g x hx]; exact Bool.noConfusion
      have hz : ext0 Rg inH ψ (conjG K g x) = Rg.zero := by
        show (if inH (conjG K g x) = true then ψ (conjG K g x) else Rg.zero) = Rg.zero
        rw [ite_neg' _ _ hf]
      show Rg.add (ext0 Rg inH ψ (conjG K g x)) _ = Rg.zero
      rw [hz, ih, Rg.zero_add]
  exact this els

end Dihedral

#print axioms Dihedral.sum_const
#print axioms Dihedral.ind_split
#print axioms Dihedral.ind_inert

78.40 Local: GL\(_2\) and GL\(_n\) at an unramified prime

With \(t=\alpha +\beta \), \(d=\alpha \beta \) and Satake data \(\alpha _1,\dots ,\alpha _n\),

\[ a(p^n)=\textstyle \sum _{i\le n}\alpha ^i\beta ^{n-i}=\operatorname {tr}\mathrm {Sym}^n\rho (\mathrm {Frob}_p),\qquad \textstyle \sum _k h_k(\alpha )X^k=\dfrac 1{\prod _i(1-\alpha _iX)} . \]
The steps are
\[ \begin {aligned} a(p^{n+2})&=t\,a(p^{n+1})-d\,a(p^n),\quad a(1)=1,\ a(p)=t,\\ h_{k+1}(a::\alpha )&=h_{k+1}(\alpha )+a\,h_k(a::\alpha ),\quad c_{k+1}(a::\alpha )=c_{k+1}(\alpha )-a\,c_k(\alpha ),\\ \textstyle \sum _{i+j=k}c_i h_j&=\delta _{k0},\ \text {and}\ c_0=1\ \text {makes}\ h\ \text {unique}. \end {aligned} \]

Listing 129: lang/GL2Local.lean

lang/GL2Local.lean

/-
  lang/GL2Local — T43, cycle 4 (GL2 over ℚ): the place/read split at one prime.

  Automorphic side at p: sequences a(p^n) obeying the Hecke recursion
      a(p^(n+2)) = t · a(p^(n+1)) − d · a(p^n),   a(1) = 1,  a(p) = t,     (d = p^(k−1) χ(p))
  Galois side at p: the characteristic data (t, d) = (tr, det) of ρ(Frob_p), with eigenvalues α, β.
    place (t, d)          = the sequence h t d  (h 0 = 1, h 1 = t, Hecke recursion)
    read  a               = (a(p), a(p)^2 − a(p^2))
    read_place            read (place (t, d)) = (t, d)                       (unit iso)
    fix_iff               a = place (read a)  ↔  a obeys the Hecke recursion  (counit iso on Fix)
    h_symm                with t = α + β, d = α β:  h t d n = Σ_{i ≤ n} α^i β^(n−i) = tr Sym^n ρ(Frob_p)
  So at each unramified p "Hecke eigenvalues a(p^n)" and "traces of Sym^n of Frobenius" are the same
  data, in every commutative ring. Checked exactly on Δ (τ(p^n), p ≤ 7) before the proof.

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain
import lang.Frobenius

namespace GL2Local

open Frobenius

variable {R : Type} (K : DetChain.CRing R)

def sub (x y : R) : R := K.add x (K.neg y)

/-- `place (t, d)`: the Hecke-recursive sequence. -/
def h (t d : R) : Nat → R
  | 0 => K.one
  | 1 => t
  | n + 2 => sub K (K.mul t (h t d (n + 1))) (K.mul d (h t d n))

/-- `read a = (a 1, a 1 ^ 2 − a 2)`. -/
def readTD (a : Nat → R) : R × R := (a 1, sub K (K.mul (a 1) (a 1)) (a 2))

def Hecke (a : Nat → R) : Prop :=
  a 0 = K.one ∧ ∀ n, a (n + 2) = sub K (K.mul (a 1) (a (n + 1))) (K.mul (sub K (K.mul (a 1) (a 1)) (a 2)) (a n))

theorem sub_sub_self (x y : R) : sub K x (sub K x y) = y := by
  show K.add x (K.neg (K.add x (K.neg y))) = y
  have e : K.neg (K.add x (K.neg y)) = K.add (K.neg x) y := by
    apply (DetChain.neg_unique K _ _ _).symm
    rw [K.add_assoc, ← K.add_assoc y x (K.neg y), K.add_comm y x, K.add_assoc x y, K.add_neg y,
      DetChain.add_zero', DetChain.add_neg']
  rw [e, ← K.add_assoc, K.add_neg, K.zero_add]

theorem read_place (t d : R) : readTD K (h K t d) = (t, d) := by
  show (t, sub K (K.mul t t) (h K t d 2)) = (t, d)
  have h2 : h K t d 2 = sub K (K.mul t t) (K.mul d K.one) := rfl
  rw [h2, K.mul_comm d K.one, K.one_mul, sub_sub_self]

theorem place_hecke (t d : R) : Hecke K (h K t d) := by
  refine ⟨rfl, fun n => ?_⟩
  have e := congrArg Prod.snd (read_place K t d)
  show h K t d (n + 2) = sub K (K.mul t (h K t d (n + 1))) (K.mul (sub K (K.mul t t) (h K t d 2)) (h K t d n))
  have e' : sub K (K.mul t t) (h K t d 2) = d := e
  rw [e']
  rfl

theorem hecke_eq_place (a : Nat → R) (ha : Hecke K a) :
    ∀ n, a n = h K (readTD K a).1 (readTD K a).2 n
  | 0 => ha.1
  | 1 => rfl
  | n + 2 => by
    rw [ha.2 n, hecke_eq_place a ha (n + 1), hecke_eq_place a ha n]
    rfl

/-- **Counit iso on the fixed points**: `a` is `place (read a)` iff it obeys the Hecke recursion. -/
theorem fix_iff (a : Nat → R) : (∀ n, a n = h K (readTD K a).1 (readTD K a).2 n) ↔ Hecke K a := by
  constructor
  · intro hf
    have hp := place_hecke K (readTD K a).1 (readTD K a).2
    refine ⟨(hf 0).trans hp.1, fun n => ?_⟩
    rw [hf (n + 2), hf (n + 1), hf n, hp.2 n]
    have r := read_place K (readTD K a).1 (readTD K a).2
    have r1 : h K (readTD K a).1 (readTD K a).2 1 = (readTD K a).1 := rfl
    have r2 : sub K (K.mul (h K (readTD K a).1 (readTD K a).2 1) (h K (readTD K a).1 (readTD K a).2 1))
        (h K (readTD K a).1 (readTD K a).2 2) = (readTD K a).2 := congrArg Prod.snd r
    rw [r1] at r2 |-
    rw [r2]
    show sub K (K.mul (a 1) (h K (readTD K a).1 (readTD K a).2 (n + 1))) (K.mul (readTD K a).2 (h K (readTD K a).1 (readTD K a).2 n))
      = sub K (K.mul (a 1) (h K (readTD K a).1 (readTD K a).2 (n + 1))) (K.mul (sub K (K.mul (a 1) (a 1)) (a 2)) (h K (readTD K a).1 (readTD K a).2 n))
    rfl
  · exact hecke_eq_place K a

/-- `Σ_{i ≤ n} α^i β^(n−i)`, by `s (n+1) = α^(n+1) + β · s n`. -/
def symTr (α β : R) : Nat → R
  | 0 => K.one
  | n + 1 => K.add (pw K α (n + 1)) (K.mul β (symTr α β n))

/-- **Hecke eigenvalues are symmetric-power traces of Frobenius.** -/
theorem h_symm (α β : R) : ∀ n, h K (K.add α β) (K.mul α β) n = symTr K α β n
  | 0 => rfl
  | 1 => by
    show K.add α β = K.add (K.mul K.one α) (K.mul β K.one)
    rw [K.one_mul, K.mul_comm β K.one, K.one_mul]
  | n + 2 => by
    show sub K (K.mul (K.add α β) (h K (K.add α β) (K.mul α β) (n + 1)))
        (K.mul (K.mul α β) (h K (K.add α β) (K.mul α β) n))
      = K.add (pw K α (n + 2)) (K.mul β (symTr K α β (n + 1)))
    rw [h_symm α β (n + 1), h_symm α β n]
    -- α s(n+1) = α^(n+2) + αβ s(n)
    have e1 : K.mul α (symTr K α β (n + 1)) = K.add (pw K α (n + 2)) (K.mul (K.mul α β) (symTr K α β n)) := by
      show K.mul α (K.add (pw K α (n + 1)) (K.mul β (symTr K α β n))) = _
      rw [K.distrib, K.mul_comm α (pw K α (n + 1)), ← K.mul_assoc]
      rfl
    show K.add (K.mul (K.add α β) (symTr K α β (n + 1))) (K.neg (K.mul (K.mul α β) (symTr K α β n))) = _
    rw [DetChain.right_distrib, e1, K.add_assoc, K.add_assoc, K.add_comm (K.mul β (symTr K α β (n + 1))),
      ← K.add_assoc (K.mul (K.mul α β) (symTr K α β n)), K.add_neg, K.zero_add]

end GL2Local

#print axioms GL2Local.sub_sub_self
#print axioms GL2Local.read_place
#print axioms GL2Local.place_hecke
#print axioms GL2Local.hecke_eq_place
#print axioms GL2Local.fix_iff
#print axioms GL2Local.h_symm

Listing 130: lang/GLnLocal.lean

lang/GLnLocal.lean

/-
  lang/GLnLocal — T43, cycle 6 (GL_n at an unramified place, every n).

  Satake / Frobenius data α = (α_1, …, α_n) (a list in any commutative ring).
    cf α     coefficients of Π (1 − α_i X)        (cf (a :: α) (k+1) = cf α (k+1) − a · cf α k)
    hk α     complete homogeneous h_k(α)          (hk (a :: α) (k+1) = hk α (k+1) + a · hk (a :: α) k)
             = tr Sym^k of diag(α) = the Hecke eigenvalue of T_(p^k) for GL_n
    conv     Cauchy product of sequences, by conv c h (k+1) = c 0 · h (k+1) + conv (c ∘ succ) h k
  Proved:
    conv_cf_hk     conv (cf α) (hk α) = δ_0, i.e.  Σ_k h_k X^k = 1 / Π (1 − α_i X)  (the local L-factor)
    conv_unique    if c 0 = 1, the sequence h with conv c h = δ_0 is unique
  So at an unramified p the Hecke eigen-system (a(p^k))_k and the Satake polynomial Π (1 − α_i X)
  determine each other (place = solve conv c h = δ, read = the polynomial), for every n.

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain

namespace GLnLocal

variable {R : Type} (K : DetChain.CRing R)

def cf : List R → Nat → R
  | [], 0 => K.one
  | [], _ + 1 => K.zero
  | _ :: α, 0 => cf α 0
  | a :: α, k + 1 => K.add (cf α (k + 1)) (K.neg (K.mul a (cf α k)))

/-- One more variable: `hkc a H 0 = H 0`, `hkc a H (k+1) = H (k+1) + a · hkc a H k`. -/
def hkc (a : R) (H : Nat → R) : Nat → R
  | 0 => H 0
  | k + 1 => K.add (H (k + 1)) (K.mul a (hkc a H k))

def hk : List R → Nat → R
  | [] => fun k => match k with
    | 0 => K.one
    | _ + 1 => K.zero
  | a :: α => hkc K a (hk α)

/-- Cauchy product. -/
def conv : (Nat → R) → (Nat → R) → Nat → R
  | c, h, 0 => K.mul (c 0) (h 0)
  | c, h, k + 1 => K.add (K.mul (c 0) (h (k + 1))) (conv (fun i => c (i + 1)) h k)

/-- Shift right: `(sR h) 0 = 0`, `(sR h) (k+1) = h k`. -/
def sR (h : Nat → R) : Nat → R
  | 0 => K.zero
  | k + 1 => h k

def delta : Nat → R
  | 0 => K.one
  | _ + 1 => K.zero

theorem conv_ext (c h g : Nat → R) (hg : ∀ i, h i = g i) : ∀ k, conv K c h k = conv K c g k := by
  intro k
  induction k generalizing c with
  | zero => show K.mul (c 0) (h 0) = K.mul (c 0) (g 0); rw [hg 0]
  | succ k ih =>
    show K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k)
      = K.add (K.mul (c 0) (g (k + 1))) (conv K (fun i => c (i + 1)) g k)
    rw [hg (k + 1), ih]

theorem conv_ext_left (c d h : Nat → R) (hcd : ∀ i, c i = d i) : ∀ k, conv K c h k = conv K d h k := by
  intro k
  induction k generalizing c d with
  | zero => show K.mul (c 0) (h 0) = K.mul (d 0) (h 0); rw [hcd 0]
  | succ k ih =>
    show K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k)
      = K.add (K.mul (d 0) (h (k + 1))) (conv K (fun i => d (i + 1)) h k)
    rw [hcd 0, ih (fun i => c (i + 1)) (fun i => d (i + 1)) (fun i => hcd (i + 1))]

theorem conv_add (c h g : Nat → R) : ∀ k, conv K c (fun i => K.add (h i) (g i)) k = K.add (conv K c h k) (conv K c g k) := by
  intro k
  induction k generalizing c with
  | zero => exact K.distrib _ _ _
  | succ k ih =>
    show K.add (K.mul (c 0) (K.add (h (k + 1)) (g (k + 1)))) (conv K (fun i => c (i + 1)) (fun i => K.add (h i) (g i)) k)
      = K.add (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
          (K.add (K.mul (c 0) (g (k + 1))) (conv K (fun i => c (i + 1)) g k))
    rw [ih, K.distrib, DetChain.add_mid4]

theorem conv_add_left (c d h : Nat → R) : ∀ k, conv K (fun i => K.add (c i) (d i)) h k = K.add (conv K c h k) (conv K d h k) := by
  intro k
  induction k generalizing c d with
  | zero => exact DetChain.right_distrib K _ _ _
  | succ k ih =>
    show K.add (K.mul (K.add (c 0) (d 0)) (h (k + 1))) (conv K (fun i => K.add (c (i + 1)) (d (i + 1))) h k)
      = K.add (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
          (K.add (K.mul (d 0) (h (k + 1))) (conv K (fun i => d (i + 1)) h k))
    rw [ih (fun i => c (i + 1)) (fun i => d (i + 1)), DetChain.right_distrib, DetChain.add_mid4]

theorem conv_smul_left (c h : Nat → R) (a : R) : ∀ k, conv K (fun i => K.mul a (c i)) h k = K.mul a (conv K c h k) := by
  intro k
  induction k generalizing c with
  | zero => show K.mul (K.mul a (c 0)) (h 0) = K.mul a (K.mul (c 0) (h 0)); exact K.mul_assoc _ _ _
  | succ k ih =>
    show K.add (K.mul (K.mul a (c 0)) (h (k + 1))) (conv K (fun i => K.mul a (c (i + 1))) h k)
      = K.mul a (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
    rw [ih (fun i => c (i + 1)), K.distrib, K.mul_assoc]

theorem conv_smul (c h : Nat → R) (a : R) : ∀ k, conv K c (fun i => K.mul a (h i)) k = K.mul a (conv K c h k) := by
  intro k
  induction k generalizing c with
  | zero =>
    show K.mul (c 0) (K.mul a (h 0)) = K.mul a (K.mul (c 0) (h 0))
    rw [← K.mul_assoc, K.mul_comm (c 0) a, K.mul_assoc]
  | succ k ih =>
    show K.add (K.mul (c 0) (K.mul a (h (k + 1)))) (conv K (fun i => c (i + 1)) (fun i => K.mul a (h i)) k)
      = K.mul a (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
    rw [ih, K.distrib, ← K.mul_assoc, K.mul_comm (c 0) a, K.mul_assoc]

theorem conv_zero_left (c h : Nat → R) (hc : ∀ i, c i = K.zero) : ∀ k, conv K c h k = K.zero := by
  intro k
  induction k generalizing c with
  | zero => show K.mul (c 0) (h 0) = K.zero; rw [hc 0, DetChain.zero_mul']
  | succ k ih =>
    show K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k) = K.zero
    rw [hc 0, DetChain.zero_mul', ih (fun i => c (i + 1)) (fun i => hc (i + 1)), K.zero_add]

theorem conv_sR_right (c h : Nat → R) : ∀ k, conv K c (sR K h) (k + 1) = conv K c h k := by
  intro k
  induction k generalizing c with
  | zero =>
    show K.add (K.mul (c 0) (h 0)) (K.mul (c 1) K.zero) = K.mul (c 0) (h 0)
    rw [DetChain.mul_zero, DetChain.add_zero']
  | succ k ih =>
    show K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) (sR K h) (k + 1))
      = K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k)
    rw [ih]

theorem conv_sR_right0 (c h : Nat → R) : conv K c (sR K h) 0 = K.zero := DetChain.mul_zero K _

/-- `conv c (sR h) = sR (conv c h)` pointwise. -/
theorem conv_sR (c h : Nat → R) : ∀ k, conv K c (sR K h) k = sR K (conv K c h) k
  | 0 => conv_sR_right0 K c h
  | k + 1 => conv_sR_right K c h k

theorem cf_cons (a : R) (α : List R) (k : Nat) :
    cf K (a :: α) k = K.add (cf K α k) (K.neg (K.mul a (sR K (cf K α) k))) := by
  cases k with
  | zero =>
    show cf K α 0 = K.add (cf K α 0) (K.neg (K.mul a K.zero))
    rw [DetChain.mul_zero]
    have : K.neg K.zero = K.zero := (DetChain.neg_unique K _ _ (K.zero_add K.zero)).symm
    rw [this, DetChain.add_zero']
  | succ k => rfl

theorem hk_cons (a : R) (α : List R) (k : Nat) :
    hk K (a :: α) k = K.add (hk K α k) (K.mul a (sR K (hk K (a :: α)) k)) := by
  cases k with
  | zero =>
    show hk K α 0 = K.add (hk K α 0) (K.mul a K.zero)
    rw [DetChain.mul_zero, DetChain.add_zero']
  | succ k => rfl

theorem neg_as_mul (x : R) : K.neg x = K.mul (K.neg K.one) x := by
  rw [DetChain.neg_mul_left, K.one_mul]

/-- **The local L-factor**: `Σ h_k(α) X^k · Π (1 − α_i X) = 1`. -/
theorem conv_cf_hk : ∀ (α : List R) (k : Nat), conv K (cf K α) (hk K α) k = delta K k
  | [], 0 => K.one_mul K.one
  | [], k + 1 => by
    show K.add (K.mul K.one K.zero) (conv K (fun i => cf K [] (i + 1)) (hk K []) k) = K.zero
    rw [DetChain.mul_zero, K.zero_add]
    exact conv_zero_left K _ _ (fun i => rfl) k
  | a :: α, k => by
    -- D = conv (cf α) (hk (a::α)) satisfies D = δ + a sR D
    have hD : ∀ j, conv K (cf K α) (hk K (a :: α)) j
        = K.add (delta K j) (K.mul a (sR K (conv K (cf K α) (hk K (a :: α))) j)) := fun j => by
      rw [conv_ext K _ _ _ (hk_cons K a α) j, conv_add, conv_smul, conv_cf_hk α j, conv_sR]
    -- conv (cf (a::α)) H' = D − a sR D
    rw [conv_ext_left K _ _ _ (cf_cons K a α) k, conv_add_left,
      conv_ext_left K (fun i => K.neg (K.mul a (sR K (cf K α) i))) (fun i => K.mul (K.neg a) (sR K (cf K α) i)) _
        (fun i => by rw [neg_as_mul K, ← K.mul_assoc, ← neg_as_mul K]) k,
      conv_smul_left]
    -- conv (sR c) h = sR (conv c h)
    have hsl : ∀ j, conv K (sR K (cf K α)) (hk K (a :: α)) j = sR K (conv K (cf K α) (hk K (a :: α))) j := by
      intro j
      cases j with
      | zero => show K.mul K.zero _ = K.zero; exact DetChain.zero_mul' K _
      | succ j =>
        show K.add (K.mul K.zero _) (conv K (fun i => sR K (cf K α) (i + 1)) (hk K (a :: α)) j) = _
        rw [DetChain.zero_mul', K.zero_add]
        exact conv_ext_left K _ _ _ (fun i => rfl) j
    rw [hsl k, hD k, K.add_assoc, DetChain.neg_mul_left K a, K.add_neg, DetChain.add_zero']

/-- `conv d h m` depends only on `h 0, …, h m`. -/
theorem conv_dep (d h g : Nat → R) : ∀ m, (∀ j, j ≤ m → h j = g j) → conv K d h m = conv K d g m := by
  intro m
  induction m generalizing d with
  | zero => intro hj; show K.mul (d 0) (h 0) = K.mul (d 0) (g 0); rw [hj 0 (Nat.le_refl 0)]
  | succ m ih =>
    intro hj
    show K.add (K.mul (d 0) (h (m + 1))) (conv K (fun i => d (i + 1)) h m)
      = K.add (K.mul (d 0) (g (m + 1))) (conv K (fun i => d (i + 1)) g m)
    rw [hj (m + 1) (Nat.le_refl _), ih (fun i => d (i + 1)) (fun j hjm => hj j (Nat.le_succ_of_le hjm))]

/-- **Uniqueness**: with `c 0 = 1`, `conv c h = δ` determines `h`. -/
theorem conv_unique (c h g : Nat → R) (hc : c 0 = K.one)
    (hh : ∀ k, conv K c h k = delta K k) (hg : ∀ k, conv K c g k = delta K k) : ∀ k, ∀ j, j ≤ k → h j = g j := by
  intro k
  induction k with
  | zero =>
    intro j hj
    have hj0 : j = 0 := Nat.le_zero.mp hj
    rw [hj0]
    have e := (hh 0).trans (hg 0).symm
    show h 0 = g 0
    have e' : K.mul (c 0) (h 0) = K.mul (c 0) (g 0) := e
    rw [hc, K.one_mul, K.one_mul] at e'
    exact e'
  | succ k ih =>
    intro j hj
    cases Nat.lt_or_eq_of_le hj with
    | inl hlt => exact ih j (Nat.le_of_lt_succ hlt)
    | inr heq =>
      rw [heq]
      have e := (hh (k + 1)).trans (hg (k + 1)).symm
      have e' : K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k)
          = K.add (K.mul (c 0) (g (k + 1))) (conv K (fun i => c (i + 1)) g k) := e
      rw [conv_dep K _ h g k ih, hc, K.one_mul, K.one_mul] at e'
      have := congrArg (fun t => K.add t (K.neg (conv K (fun i => c (i + 1)) g k))) e'
      show h (k + 1) = g (k + 1)
      have t1 : K.add (K.add (h (k + 1)) (conv K (fun i => c (i + 1)) g k)) (K.neg (conv K (fun i => c (i + 1)) g k))
          = K.add (K.add (g (k + 1)) (conv K (fun i => c (i + 1)) g k)) (K.neg (conv K (fun i => c (i + 1)) g k)) := this
      rw [K.add_assoc, K.add_neg, DetChain.add_zero', K.add_assoc, K.add_neg, DetChain.add_zero'] at t1
      exact t1

end GLnLocal

#print axioms GLnLocal.conv_ext
#print axioms GLnLocal.conv_ext_left
#print axioms GLnLocal.conv_add
#print axioms GLnLocal.conv_add_left
#print axioms GLnLocal.conv_smul_left
#print axioms GLnLocal.conv_smul
#print axioms GLnLocal.conv_zero_left
#print axioms GLnLocal.conv_sR_right
#print axioms GLnLocal.conv_sR_right0
#print axioms GLnLocal.conv_sR
#print axioms GLnLocal.cf_cons
#print axioms GLnLocal.hk_cons
#print axioms GLnLocal.neg_as_mul
#print axioms GLnLocal.conv_cf_hk
#print axioms GLnLocal.conv_dep
#print axioms GLnLocal.conv_unique

78.41 Local functoriality: \(\mathrm {Sym}^n\) and twists

At an unramified prime the lift of \((t,d)\) along \(\mathrm {Sym}^n:\mathrm {GL}_2\to \mathrm {GL}_{n+1}\) and along \(\mathrm {GL}_n\times \mathrm {GL}_1\to \mathrm {GL}_n\) is

\[ \begin {aligned} \textstyle \prod _{i=0}^n(1-\alpha ^{n-i}\beta ^iX)&=Q_n(t,d;X),\qquad Q_{n+2}=(1-s_{n+2}X+d^{n+2}X^2)\,Q_n(dX),\\ h_k(c\,\alpha )&=c^k\,h_k(\alpha ). \end {aligned} \]
The steps are
\[ \begin {aligned} \mathrm {symL}(n+2)&=[\alpha ^{n+2},\beta ^{n+2}]\mathbin {+\!\!+}(\alpha \beta )\cdot \mathrm {symL}(n),\\ c(l_1\mathbin {+\!\!+}l_2)&=c(l_1)*c(l_2),\qquad c(d\cdot l)_k=d^k c(l)_k,\\ s_{n+2}&=t\,s_{n+1}-d\,s_n\quad (s_n=\alpha ^n+\beta ^n). \end {aligned} \]

Listing 131: lang/Sym2Local.lean

lang/Sym2Local.lean

/-
  lang/Sym2Local — T43, cycle 7 (functoriality, local, unramified): Sym² : GL2 → GL3.

  The L-homomorphism Sym² sends Satake data (α, β) of GL2 to (α², αβ, β²) of GL3. Its Satake polynomial
      Π (1 − λ X) over λ ∈ {α², αβ, β²}  =  1 − (t² − d) X + d (t² − d) X² − d³ X³,   t = α + β, d = αβ,
  depends only on (t, d) = (a(p), p^(k−1)χ(p)) — the GL2 eigen-system at p (GL2Local). With
  GLnLocal.conv_unique the GL3 eigen-system of the lift at p is therefore determined by that of π:
    cf3_k            cf [a, b, c] = 1, −(a + b + c), ab + ac + bc, −abc, 0, 0, …
    sym2_poly        cf [α², αβ, β²] = 1, −(t² − d), d(t² − d), −d³, 0, …
    sym2_lift        the lifted Hecke eigen-system h_k(α², αβ, β²) is the unique solution of
                     conv (c_lift(t, d)) h = δ  — local Sym² functoriality at unramified p
  Checked first on 200 random integer pairs (α, β).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.GLnLocal

namespace Sym2Local

open GLnLocal

variable {R : Type} (K : DetChain.CRing R)

def neg' (x : R) : R := K.neg x

theorem neg_zero' : K.neg K.zero = K.zero := (DetChain.neg_unique K _ _ (K.zero_add K.zero)).symm

theorem neg_add_distrib (x y : R) : K.neg (K.add x y) = K.add (K.neg x) (K.neg y) := by
  apply (DetChain.neg_unique K _ _ _).symm
  rw [DetChain.add_mid4, DetChain.add_neg', DetChain.add_neg', K.zero_add]

theorem neg_mul_neg (x y : R) : K.mul (K.neg x) (K.neg y) = K.mul x y := by
  rw [DetChain.neg_mul_left, K.mul_comm x, DetChain.neg_mul_left, DetChain.neg_neg', K.mul_comm]

theorem mul_neg' (x y : R) : K.mul x (K.neg y) = K.neg (K.mul x y) := by
  rw [K.mul_comm, DetChain.neg_mul_left, K.mul_comm]

/-- `cf [a, b, c]` in closed form. -/
theorem cf3_0 (a b c : R) : cf K [a, b, c] 0 = K.one := rfl

theorem cf3_1 (a b c : R) : cf K [a, b, c] 1 = K.neg (K.add (K.add a b) c) := by
  show K.add (K.add (K.add K.zero (K.neg (K.mul c K.one))) (K.neg (K.mul b K.one))) (K.neg (K.mul a K.one)) = _
  rw [K.mul_comm c, K.mul_comm b, K.mul_comm a, K.one_mul, K.one_mul, K.one_mul, K.zero_add,
    neg_add_distrib, neg_add_distrib, K.add_comm (K.neg c) (K.neg b), K.add_assoc, K.add_comm]
  rw [K.add_comm (K.neg c) (K.neg a)]
  rw [K.add_assoc, K.add_comm (K.neg c) (K.neg b), ← K.add_assoc]

theorem cf3_2 (a b c : R) : cf K [a, b, c] 2 = K.add (K.add (K.mul a b) (K.mul a c)) (K.mul b c) := by
  show K.add (K.add (K.add K.zero (K.neg (K.mul c K.zero)))
      (K.neg (K.mul b (K.add K.zero (K.neg (K.mul c K.one))))))
      (K.neg (K.mul a (K.add (K.add K.zero (K.neg (K.mul c K.one))) (K.neg (K.mul b K.one))))) = _
  rw [DetChain.mul_zero, neg_zero', K.zero_add, K.zero_add, K.mul_comm c K.one, K.mul_comm b K.one,
    K.one_mul, K.one_mul, K.zero_add, mul_neg', DetChain.neg_neg', K.distrib, mul_neg', mul_neg',
    neg_add_distrib, DetChain.neg_neg', DetChain.neg_neg']
  rw [K.add_comm (K.mul a c) (K.mul a b), K.add_comm (K.mul b c), K.add_assoc]

theorem cf3_3 (a b c : R) : cf K [a, b, c] 3 = K.neg (K.mul (K.mul a b) c) := by
  show K.add (K.add (K.add K.zero (K.neg (K.mul c K.zero)))
      (K.neg (K.mul b (K.add K.zero (K.neg (K.mul c K.zero))))))
      (K.neg (K.mul a (K.add (K.add K.zero (K.neg (K.mul c K.zero)))
        (K.neg (K.mul b (K.add K.zero (K.neg (K.mul c K.one)))))))) = _
  rw [DetChain.mul_zero, neg_zero', K.zero_add, DetChain.mul_zero, neg_zero', K.zero_add, K.zero_add,
    K.mul_comm c K.one, K.one_mul, K.zero_add, mul_neg', DetChain.neg_neg', K.zero_add, K.mul_assoc]
  rw [mul_neg' K b c, mul_neg' K a]

theorem cf3_high (a b c : R) (k : Nat) : cf K [a, b, c] (k + 4) = K.zero := by
  show K.add (K.add (K.add K.zero (K.neg (K.mul c K.zero)))
      (K.neg (K.mul b (K.add K.zero (K.neg (K.mul c K.zero))))))
      (K.neg (K.mul a (K.add (K.add K.zero (K.neg (K.mul c K.zero)))
        (K.neg (K.mul b (K.add K.zero (K.neg (K.mul c K.zero)))))))) = K.zero
  rw [DetChain.mul_zero, neg_zero', K.zero_add, DetChain.mul_zero, neg_zero', K.zero_add,
    DetChain.mul_zero, neg_zero', K.zero_add]

/-- `t·t − d = α² + αβ + β²` for `t = α + β`, `d = αβ`. -/
theorem tt_sub (α β : R) :
    K.add (K.mul (K.add α β) (K.add α β)) (K.neg (K.mul α β))
      = K.add (K.add (K.mul α α) (K.mul α β)) (K.mul β β) := by
  rw [DetChain.right_distrib, K.distrib, K.distrib, K.mul_comm β α, K.add_assoc,
    K.add_assoc (K.mul α β) (K.mul β β) (K.neg (K.mul α β)), K.add_comm (K.mul β β) (K.neg (K.mul α β)),
    ← K.add_assoc (K.mul α β) (K.neg (K.mul α β)), K.add_neg, K.zero_add]

/-- The Sym² Satake polynomial in terms of `(t, d)`. -/
def cLift (t d : R) : Nat → R
  | 0 => K.one
  | 1 => K.neg (K.add (K.mul t t) (K.neg d))
  | 2 => K.mul d (K.add (K.mul t t) (K.neg d))
  | 3 => K.neg (K.mul (K.mul d d) d)
  | _ + 4 => K.zero

/-- **The lift's Satake polynomial depends only on (t, d).** -/
theorem sym2_poly (α β : R) : ∀ k,
    cf K [K.mul α α, K.mul α β, K.mul β β] k = cLift K (K.add α β) (K.mul α β) k
  | 0 => rfl
  | 1 => by
    rw [cf3_1]
    show _ = K.neg (K.add (K.mul (K.add α β) (K.add α β)) (K.neg (K.mul α β)))
    rw [tt_sub]
  | 2 => by
    rw [cf3_2]
    show _ = K.mul (K.mul α β) (K.add (K.mul (K.add α β) (K.add α β)) (K.neg (K.mul α β)))
    rw [tt_sub, K.distrib, K.distrib]
    -- α²·αβ + α²·β² + αβ·β²  =  αβ·α² + αβ·αβ + αβ·β²
    have e1 : K.mul (K.mul α α) (K.mul α β) = K.mul (K.mul α β) (K.mul α α) := K.mul_comm _ _
    have e2 : K.mul (K.mul α α) (K.mul β β) = K.mul (K.mul α β) (K.mul α β) := by
      rw [K.mul_assoc, ← K.mul_assoc α β β, K.mul_comm (K.mul α β) β, ← K.mul_assoc, ← K.mul_assoc,
        K.mul_assoc (K.mul α β)]
    rw [e1, e2]
  | 3 => by
    rw [cf3_3]
    show _ = K.neg (K.mul (K.mul (K.mul α β) (K.mul α β)) (K.mul α β))
    have e : K.mul (K.mul (K.mul α α) (K.mul α β)) (K.mul β β)
        = K.mul (K.mul (K.mul α β) (K.mul α β)) (K.mul α β) := by
      rw [K.mul_assoc (K.mul α α) (K.mul α β), K.mul_comm (K.mul α β) (K.mul β β), ← K.mul_assoc (K.mul α α)]
      have e2 : K.mul (K.mul α α) (K.mul β β) = K.mul (K.mul α β) (K.mul α β) := by
        rw [K.mul_assoc, ← K.mul_assoc α β β, K.mul_comm (K.mul α β) β, ← K.mul_assoc, ← K.mul_assoc,
          K.mul_assoc (K.mul α β)]
      rw [e2]
    rw [e]
  | k + 4 => cf3_high K _ _ _ k

/-- **Local Sym² functoriality.** The lifted GL3 eigen-system at `p`, `h_k(α², αβ, β²)`, is the unique
    solution of `conv (cLift t d) h = δ`; so it is a function of the GL2 data `(t, d)` at `p`. -/
theorem sym2_lift (α β : R) (g : Nat → R)
    (hg : ∀ k, conv K (cLift K (K.add α β) (K.mul α β)) g k = delta K k) :
    ∀ k, hk K [K.mul α α, K.mul α β, K.mul β β] k = g k := fun k =>
  conv_unique K (cLift K (K.add α β) (K.mul α β)) _ g rfl
    (fun j => (conv_ext_left K _ _ _ (fun i => (sym2_poly K α β i).symm) j).trans
      (conv_cf_hk K [K.mul α α, K.mul α β, K.mul β β] j)) hg k k (Nat.le_refl k)

end Sym2Local

#print axioms Sym2Local.neg_zero'
#print axioms Sym2Local.neg_add_distrib
#print axioms Sym2Local.neg_mul_neg
#print axioms Sym2Local.mul_neg'
#print axioms Sym2Local.cf3_0
#print axioms Sym2Local.cf3_1
#print axioms Sym2Local.cf3_2
#print axioms Sym2Local.cf3_3
#print axioms Sym2Local.cf3_high
#print axioms Sym2Local.tt_sub
#print axioms Sym2Local.sym2_poly
#print axioms Sym2Local.sym2_lift

Listing 132: lang/SymNLocal.lean

lang/SymNLocal.lean

/-
  lang/SymNLocal — T43, cycle 8 (functoriality, local, unramified): Sym^n : GL2 → GL_(n+1), every n.

  Satake data of Sym^n: the multiset {α^(n−i) β^i}, listed recursively as
      symL 0 = [1],  symL 1 = [α, β],  symL (n+2) = [α^(n+2), β^(n+2)] ++ (αβ) · symL n.
  General facts about Satake polynomials (any commutative ring):
    cf_append    cf (l₁ ++ l₂) = cf l₁ * cf l₂        (Cauchy product)
    cf_scale     cf (d · l) k = d^k · cf l k
    psum_eq      power sums α^n + β^n satisfy s(n+2) = t s(n+1) − d s(n), t = α + β, d = αβ
  Hence (symn_poly) the Sym^n Satake polynomial equals Q_n(t, d), a polynomial in (t, d) alone:
      Q_(n+2) = (1 − s_(n+2) X + d^(n+2) X²) · Q_n(d X),
  and (symn_lift) the lifted GL_(n+1) eigen-system at p is the unique solution of conv (Q_n) h = δ:
  determined by the GL2 data (a(p), p^(k−1)χ(p)). Checked first for n ≤ 6 on random integers.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.GLnLocal
import lang.Frobenius
import lang.Sym2Local

namespace SymNLocal

open GLnLocal

variable {R : Type} (K : DetChain.CRing R)

abbrev pw := Frobenius.pw K

theorem cf_nil (k : Nat) : cf K [] k = delta K k := by cases k <;> rfl

theorem conv_delta_left (h : Nat → R) : ∀ k, conv K (delta K) h k = h k
  | 0 => K.one_mul _
  | k + 1 => by
    show K.add (K.mul K.one (h (k + 1))) (conv K (fun i => delta K (i + 1)) h k) = h (k + 1)
    rw [K.one_mul, show conv K (fun i => delta K (i + 1)) h k = K.zero from conv_zero_left K _ h (fun _ => rfl) k,
      DetChain.add_zero']

theorem conv_sR_left (c h : Nat → R) : ∀ k, conv K (sR K c) h k = sR K (conv K c h) k
  | 0 => DetChain.zero_mul' K _
  | k + 1 => by
    show K.add (K.mul K.zero (h (k + 1))) (conv K (fun i => sR K c (i + 1)) h k) = conv K c h k
    rw [DetChain.zero_mul', K.zero_add]
    exact conv_ext_left K _ _ _ (fun i => rfl) k

theorem sR_ext (f g : Nat → R) (hfg : ∀ i, f i = g i) : ∀ k, sR K f k = sR K g k
  | 0 => rfl
  | k + 1 => hfg k

theorem mid4' (a b c d : R) : K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := by
  rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]

theorem neg_mul_eq (a x : R) : K.neg (K.mul a x) = K.mul (K.neg a) x := (DetChain.neg_mul_left K a x).symm

/-- **The Satake polynomial of a union is the product.** -/
theorem cf_append : ∀ (l₁ l₂ : List R) (k : Nat), cf K (l₁ ++ l₂) k = conv K (cf K l₁) (cf K l₂) k
  | [], l₂, k => by
    rw [conv_ext_left K _ _ _ (cf_nil K) k, conv_delta_left]
    rfl
  | a :: l₁, l₂, k => by
    show cf K (a :: (l₁ ++ l₂)) k = _
    rw [cf_cons, conv_ext_left K _ _ _ (cf_cons K a l₁) k, conv_add_left,
      conv_ext_left K (fun i => K.neg (K.mul a (sR K (cf K l₁) i))) (fun i => K.mul (K.neg a) (sR K (cf K l₁) i)) _
        (fun i => neg_mul_eq K _ _) k, conv_smul_left, conv_sR_left, cf_append l₁ l₂ k,
      sR_ext K _ _ (fun i => cf_append l₁ l₂ i) k, neg_mul_eq]

/-- **Scaling the Satake data by d multiplies the k-th coefficient by d^k.** -/
theorem cf_scale (d : R) : ∀ (l : List R) (k : Nat), cf K (l.map (K.mul d)) k = K.mul (pw K d k) (cf K l k)
  | [], k => by
    cases k with
    | zero => exact (K.one_mul K.one).symm
    | succ k => exact (DetChain.mul_zero K _).symm
  | a :: l, k => by
    cases k with
    | zero => show cf K (l.map (K.mul d)) 0 = K.mul K.one (cf K l 0); rw [cf_scale d l 0]; rfl
    | succ k =>
      show K.add (cf K (l.map (K.mul d)) (k + 1)) (K.neg (K.mul (K.mul d a) (cf K (l.map (K.mul d)) k)))
        = K.mul (K.mul (pw K d k) d) (K.add (cf K l (k + 1)) (K.neg (K.mul a (cf K l k))))
      rw [cf_scale d l (k + 1), cf_scale d l k, K.distrib]
      show K.add (K.mul (K.mul (pw K d k) d) (cf K l (k + 1))) (K.neg (K.mul (K.mul d a) (K.mul (pw K d k) (cf K l k))))
        = K.add (K.mul (K.mul (pw K d k) d) (cf K l (k + 1))) (K.mul (K.mul (pw K d k) d) (K.neg (K.mul a (cf K l k))))
      congr 1
      rw [K.mul_comm (K.mul (pw K d k) d) (K.neg _), DetChain.neg_mul_left]
      congr 1
      rw [K.mul_comm d a, mid4' K a d, mid4' K a (cf K l k), K.mul_comm d (cf K l k)]

theorem neg_zero'' : K.neg K.zero = K.zero := (DetChain.neg_unique K _ _ (K.zero_add K.zero)).symm

/-- Two parameters: `cf [a, b] = 1, −(a + b), ab, 0, …`. -/
def B2 (s p : R) : Nat → R
  | 0 => K.one
  | 1 => K.neg s
  | 2 => p
  | _ + 3 => K.zero

theorem cf2 (a b : R) : ∀ k, cf K [a, b] k = B2 K (K.add a b) (K.mul a b) k
  | 0 => rfl
  | 1 => by
    show K.add (K.add K.zero (K.neg (K.mul b K.one))) (K.neg (K.mul a K.one)) = K.neg (K.add a b)
    rw [K.zero_add, K.mul_comm b K.one, K.mul_comm a K.one, K.one_mul, K.one_mul,
      Sym2Local.neg_add_distrib K a b, K.add_comm]
  | 2 => by
    show K.add (K.add K.zero (K.neg (K.mul b K.zero))) (K.neg (K.mul a (K.add K.zero (K.neg (K.mul b K.one)))))
      = K.mul a b
    rw [DetChain.mul_zero, neg_zero'', K.zero_add, K.zero_add, K.mul_comm b K.one, K.one_mul, K.zero_add,
      Sym2Local.mul_neg' K a b, DetChain.neg_neg']
  | k + 3 => by
    show K.add (K.add K.zero (K.neg (K.mul b K.zero))) (K.neg (K.mul a (K.add K.zero (K.neg (K.mul b K.zero)))))
      = K.zero
    rw [DetChain.mul_zero, neg_zero'', K.zero_add, DetChain.mul_zero, neg_zero'', K.zero_add]

/-- Power sums from `(t, d)`: `s 0 = 2`, `s 1 = t`, `s (n+2) = t s(n+1) − d s(n)`. -/
def psum (t d : R) : Nat → R
  | 0 => K.add K.one K.one
  | 1 => t
  | n + 2 => K.add (K.mul t (psum t d (n + 1))) (K.neg (K.mul d (psum t d n)))

theorem cancel4 (P U V Q : R) : K.add (K.add (K.add P U) (K.add V Q)) (K.neg (K.add V U)) = K.add P Q := by
  rw [K.add_comm V Q, DetChain.add_mid4, K.add_comm U V, K.add_assoc, K.add_neg, DetChain.add_zero']

theorem psum_eq (α β : R) : ∀ n, psum K (K.add α β) (K.mul α β) n = K.add (pw K α n) (pw K β n)
  | 0 => rfl
  | 1 => by show K.add α β = K.add (K.mul K.one α) (K.mul K.one β); rw [K.one_mul, K.one_mul]
  | n + 2 => by
    show K.add (K.mul (K.add α β) (psum K (K.add α β) (K.mul α β) (n + 1)))
        (K.neg (K.mul (K.mul α β) (psum K (K.add α β) (K.mul α β) n)))
      = K.add (K.mul (K.mul (pw K α n) α) α) (K.mul (K.mul (pw K β n) β) β)
    rw [psum_eq α β (n + 1), psum_eq α β n]
    show K.add (K.mul (K.add α β) (K.add (K.mul (pw K α n) α) (K.mul (pw K β n) β)))
        (K.neg (K.mul (K.mul α β) (K.add (pw K α n) (pw K β n)))) = _
    rw [DetChain.right_distrib, K.distrib, K.distrib, K.distrib]
    have u : K.mul α (K.mul (pw K β n) β) = K.mul (K.mul α β) (pw K β n) := by
      rw [K.mul_comm (pw K β n) β, ← K.mul_assoc]
    have v : K.mul β (K.mul (pw K α n) α) = K.mul (K.mul α β) (pw K α n) := by
      rw [K.mul_comm (pw K α n) α, ← K.mul_assoc, K.mul_comm β α]
    rw [u, v, K.mul_comm α (K.mul (pw K α n) α), K.mul_comm β (K.mul (pw K β n) β)]
    exact cancel4 K _ _ _ _

def symL (α β : R) : Nat → List R
  | 0 => [K.one]
  | 1 => [α, β]
  | n + 2 => [pw K α (n + 2), pw K β (n + 2)] ++ (symL α β n).map (K.mul (K.mul α β))

/-- The Sym^n Satake polynomial as a function of `(t, d)` only. -/
def Q (t d : R) : Nat → Nat → R
  | 0 => cf K [K.one]
  | 1 => B2 K t d
  | n + 2 => conv K (B2 K (psum K t d (n + 2)) (pw K d (n + 2))) (fun j => K.mul (pw K d j) (Q t d n j))

/-- **The Sym^n Satake polynomial depends only on (t, d).** -/
theorem symn_poly (α β : R) : ∀ n k, cf K (symL K α β n) k = Q K (K.add α β) (K.mul α β) n k
  | 0, _ => rfl
  | 1, k => cf2 K α β k
  | n + 2, k => by
    show cf K ([pw K α (n + 2), pw K β (n + 2)] ++ (symL K α β n).map (K.mul (K.mul α β))) k
      = conv K (B2 K (psum K (K.add α β) (K.mul α β) (n + 2)) (pw K (K.mul α β) (n + 2)))
          (fun j => K.mul (pw K (K.mul α β) j) (Q K (K.add α β) (K.mul α β) n j)) k
    rw [cf_append]
    have hB : ∀ j, cf K [pw K α (n + 2), pw K β (n + 2)] j
        = B2 K (psum K (K.add α β) (K.mul α β) (n + 2)) (pw K (K.mul α β) (n + 2)) j := fun j => by
      rw [cf2, psum_eq, show pw K (K.mul α β) (n + 2) = K.mul (pw K α (n + 2)) (pw K β (n + 2)) from
        Frobenius.pw_mul K α β (n + 2)]
    rw [conv_ext_left K _ _ _ hB k]
    exact conv_ext K _ _ _ (fun j => by rw [cf_scale, symn_poly α β n j]) k

theorem cf_zero : ∀ l : List R, cf K l 0 = K.one
  | [] => rfl
  | _ :: l => cf_zero l

/-- **Local Sym^n functoriality.** The lifted GL_(n+1) eigen-system at `p` is the unique solution of
    `conv (Q_n(t, d)) h = δ`, hence a function of the GL2 data `(t, d)`. -/
theorem symn_lift (α β : R) (n : Nat) (g : Nat → R)
    (hg : ∀ k, conv K (Q K (K.add α β) (K.mul α β) n) g k = delta K k) :
    ∀ k, hk K (symL K α β n) k = g k := fun k =>
  conv_unique K (Q K (K.add α β) (K.mul α β) n) _ g
    ((symn_poly K α β n 0).symm.trans (cf_zero K _))
    (fun j => (conv_ext_left K _ _ _ (fun i => (symn_poly K α β n i).symm) j).trans
      (conv_cf_hk K (symL K α β n) j)) hg k k (Nat.le_refl k)

end SymNLocal

#print axioms SymNLocal.cf_nil
#print axioms SymNLocal.conv_delta_left
#print axioms SymNLocal.conv_sR_left
#print axioms SymNLocal.sR_ext
#print axioms SymNLocal.mid4'
#print axioms SymNLocal.neg_mul_eq
#print axioms SymNLocal.cf_append
#print axioms SymNLocal.cf_scale
#print axioms SymNLocal.neg_zero''
#print axioms SymNLocal.cf2
#print axioms SymNLocal.cancel4
#print axioms SymNLocal.psum_eq
#print axioms SymNLocal.symn_poly
#print axioms SymNLocal.cf_zero
#print axioms SymNLocal.symn_lift

Listing 133: lang/TwistLocal.lean

lang/TwistLocal.lean

/-
  lang/TwistLocal — T43, cycle 9 (functoriality, local unramified): twisting GL_n by a character of GL_1.

  The L-homomorphism GL_n × GL_1 → GL_n, (g, c) ↦ c g, multiplies the Satake data by c = χ(p).
    conv_scale     conv (d^• c) (d^• h) k = d^k · conv c h k
    twist_lift     h_k(c α) = c^k · h_k(α): the twisted eigen-system at p is a_twist(p^k) = χ(p)^k a(p^k)
  (from SymNLocal.cf_scale and GLnLocal.conv_unique). Checked numerically before the proof.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.SymNLocal

namespace TwistLocal

open GLnLocal SymNLocal

variable {R : Type} (K : DetChain.CRing R)

def scl (d : R) (c : Nat → R) (k : Nat) : R := K.mul (SymNLocal.pw K d k) (c k)

theorem conv_scale (d : R) : ∀ (c h : Nat → R) (k : Nat),
    conv K (scl K d c) (scl K d h) k = K.mul (SymNLocal.pw K d k) (conv K c h k)
  | c, h, 0 => by
    show K.mul (K.mul K.one (c 0)) (K.mul K.one (h 0)) = K.mul K.one (K.mul (c 0) (h 0))
    rw [K.one_mul, K.one_mul, K.one_mul]
  | c, h, k + 1 => by
    show K.add (K.mul (K.mul K.one (c 0)) (scl K d h (k + 1))) (conv K (fun i => scl K d c (i + 1)) (scl K d h) k)
      = K.mul (SymNLocal.pw K d (k + 1)) (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
    -- scl d c (i+1) = d · scl d (c ∘ succ) i
    have e : ∀ i, scl K d c (i + 1) = K.mul d (scl K d (fun j => c (j + 1)) i) := fun i => by
      show K.mul (K.mul (SymNLocal.pw K d i) d) (c (i + 1)) = K.mul d (K.mul (SymNLocal.pw K d i) (c (i + 1)))
      rw [K.mul_comm (SymNLocal.pw K d i) d, K.mul_assoc]
    rw [conv_ext_left K _ _ _ e k, conv_smul_left, conv_scale d (fun j => c (j + 1)) h k, K.one_mul, K.distrib]
    show K.add (K.mul (c 0) (K.mul (SymNLocal.pw K d (k + 1)) (h (k + 1))))
        (K.mul d (K.mul (SymNLocal.pw K d k) (conv K (fun i => c (i + 1)) h k)))
      = K.add (K.mul (K.mul (SymNLocal.pw K d k) d) (K.mul (c 0) (h (k + 1))))
          (K.mul (K.mul (SymNLocal.pw K d k) d) (conv K (fun i => c (i + 1)) h k))
    rw [← K.mul_assoc d, K.mul_comm d (SymNLocal.pw K d k)]
    congr 1
    show K.mul (c 0) (K.mul (K.mul (SymNLocal.pw K d k) d) (h (k + 1))) = K.mul (K.mul (SymNLocal.pw K d k) d) (K.mul (c 0) (h (k + 1)))
    rw [← K.mul_assoc, K.mul_comm (c 0), K.mul_assoc]

/-- **Twisting.** `h_k(c α) = c^k h_k(α)`. -/
theorem twist_lift (c : R) (α : List R) (k : Nat) :
    hk K (α.map (K.mul c)) k = K.mul (SymNLocal.pw K c k) (hk K α k) := by
  have hc : ∀ j, conv K (cf K (α.map (K.mul c))) (scl K c (hk K α)) j = delta K j := fun j => by
    rw [conv_ext_left K _ (scl K c (cf K α)) _ (fun i => cf_scale K c α i) j, conv_scale, conv_cf_hk]
    cases j with
    | zero => exact K.one_mul K.one
    | succ j => exact DetChain.mul_zero K _
  exact conv_unique K (cf K (α.map (K.mul c))) _ _ (cf_zero K _) (conv_cf_hk K _) hc k k (Nat.le_refl k)

end TwistLocal

#print axioms TwistLocal.conv_scale
#print axioms TwistLocal.twist_lift

78.42 Local functoriality: the tensor product \(\mathrm {GL}_2\times \mathrm {GL}_2\to \mathrm {GL}_4\)

With \(t_1=\alpha +\beta \), \(d_1=\alpha \beta \), \(t_2=\gamma +\delta \), \(d_2=\gamma \delta \),

\[ \prod _{\lambda \in \{\alpha \gamma ,\alpha \delta ,\beta \gamma ,\beta \delta \}}(1-\lambda X) =1-t_1t_2X+(d_2t_1^2+d_1t_2^2-2d_1d_2)X^2-d_1d_2t_1t_2X^3+d_1^2d_2^2X^4 , \]
so the GL\(_4\) eigen-system of \(\pi \boxtimes \pi '\) at an unramified \(p\) is the unique solution of \(\sum _{i+j=k}c_ih_j=\delta _{k0}\) with these coefficients. The steps are
\[ \begin {aligned} e_2&=d_2(\alpha ^2+\beta ^2)+d_1(\gamma ^2+\delta ^2)+2d_1d_2=d_2t_1^2+d_1t_2^2-2d_1d_2,\\ e_3&=e_4\sum _\lambda \lambda ^{-1}=d_1^2d_2^2\cdot \frac {t_1}{d_1}\cdot \frac {t_2}{d_2}=d_1d_2t_1t_2,\\ e_4&=d_1^2d_2^2,\qquad c(l)_{k+1}=c(\mathrm {tail})_{k+1}-a\,c(\mathrm {tail})_k\ \text {unfolded for}\ k\le 4. \end {aligned} \]

Listing 134: lang/TensorLocal.lean

lang/TensorLocal.lean

/-
  lang/TensorLocal — T43, cycle 94 (functoriality, local, unramified): the tensor product GL2 × GL2 → GL4.

  The L-homomorphism (g, h) ↦ g ⊗ h sends Satake data (α, β), (γ, δ) to (αγ, αδ, βγ, βδ). With t₁ = α + β, d₁ = αβ,
  t₂ = γ + δ, d₂ = γδ its Satake polynomial is
      Π (1 − λX) = 1 − t₁t₂ X + (d₂t₁² + d₁t₂² − 2d₁d₂) X² − d₁d₂t₁t₂ X³ + d₁²d₂² X⁴,
  a function of the two GL2 eigen-systems at p alone:
    cT              the polynomial in (t₁, d₁, t₂, d₂)
    tensor_poly     cf [αγ, αδ, βγ, βδ] = cT (t₁, d₁, t₂, d₂)
    tensor_lift     the GL4 eigen-system h_k(αγ, αδ, βγ, βδ) is the unique solution of conv (cT) h = δ  — local
                    Rankin–Selberg functoriality at unramified p (the local factor of L(s, π × π'))
  Checked first on 200 random integer quadruples (α, β, γ, δ).

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Sym2Local
import lib.RingTac

namespace TensorLocal

open GLnLocal

variable {R : Type} (K : DetChain.CRing R)

def cT (t₁ d₁ t₂ d₂ : R) : Nat → R
  | 0 => K.one
  | 1 => K.neg (K.mul t₁ t₂)
  | 2 => K.add (K.add (K.mul d₂ (K.mul t₁ t₁)) (K.mul d₁ (K.mul t₂ t₂))) (K.neg (K.add (K.mul d₁ d₂) (K.mul d₁ d₂)))
  | 3 => K.neg (K.mul (K.mul d₁ d₂) (K.mul t₁ t₂))
  | 4 => K.mul (K.mul d₁ d₁) (K.mul d₂ d₂)
  | _ + 5 => K.zero

/-- **The tensor-product Satake polynomial depends only on the two GL2 data.** -/
theorem tensor_poly (α β γ δ : R) : ∀ k,
    cf K [K.mul α γ, K.mul α δ, K.mul β γ, K.mul β δ] k = cT K (K.add α β) (K.mul α β) (K.add γ δ) (K.mul γ δ) k
  | 0 => by
    show K.one = K.one
    lib_ring
  | 1 => by
    show K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.one)))) (K.neg (K.mul (K.mul β γ) (K.one)))) (K.neg (K.mul (K.mul α δ) (K.one)))) (K.neg (K.mul (K.mul α γ) (K.one))) = K.neg (K.mul (K.add α β) (K.add γ δ))
    lib_ring
  | 2 => by
    show K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.zero)))) (K.neg (K.mul (K.mul β γ) (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.one))))))) (K.neg (K.mul (K.mul α δ) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.one)))) (K.neg (K.mul (K.mul β γ) (K.one))))))) (K.neg (K.mul (K.mul α γ) (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.one)))) (K.neg (K.mul (K  -- (+259 chars; full line in the Zenodo source)
    lib_ring
  | 3 => by
    show K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.zero)))) (K.neg (K.mul (K.mul β γ) (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.zero))))))) (K.neg (K.mul (K.mul α δ) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.zero)))) (K.neg (K.mul (K.mul β γ) (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.one)))))))))) (K.neg (K.mul (K.mul α γ) (K.add (K.add (K.add (K.zero) (K.neg  -- (+321 chars; full line in the Zenodo source)
    lib_ring
  | 4 => by
    show K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.zero)))) (K.neg (K.mul (K.mul β γ) (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.zero))))))) (K.neg (K.mul (K.mul α δ) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.zero)))) (K.neg (K.mul (K.mul β γ) (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.zero)))))))))) (K.neg (K.mul (K.mul α γ) (K.add (K.add (K.add (K.zero) (K.ne  -- (+361 chars; full line in the Zenodo source)
    lib_ring
  | k + 5 => by
    show K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.zero)))) (K.neg (K.mul (K.mul β γ) (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.zero))))))) (K.neg (K.mul (K.mul α δ) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.zero)))) (K.neg (K.mul (K.mul β γ) (K.add (K.zero) (K.neg (K.mul (K.mul β δ) (K.zero)))))))))) (K.neg (K.mul (K.mul α γ) (K.add (K.add (K.add (K.zero) (K.ne  -- (+299 chars; full line in the Zenodo source)
    lib_ring

/-- **Local GL2 × GL2 → GL4 functoriality.** -/
theorem tensor_lift (α β γ δ : R) (g : Nat → R)
    (hg : ∀ k, conv K (cT K (K.add α β) (K.mul α β) (K.add γ δ) (K.mul γ δ)) g k = delta K k) :
    ∀ k, hk K [K.mul α γ, K.mul α δ, K.mul β γ, K.mul β δ] k = g k := fun k =>
  conv_unique K (cT K (K.add α β) (K.mul α β) (K.add γ δ) (K.mul γ δ)) _ g rfl
    (fun j => (conv_ext_left K _ _ _ (fun i => (tensor_poly K α β γ δ i).symm) j).trans
      (conv_cf_hk K [K.mul α γ, K.mul α δ, K.mul β γ, K.mul β δ] j)) hg k k (Nat.le_refl k)

end TensorLocal

#print axioms TensorLocal.tensor_poly
#print axioms TensorLocal.tensor_lift

78.43 Local functoriality: the exterior square \(\wedge ^2:\mathrm {GL}_4\to \mathrm {GL}_6\)

With \(e_1,\dots ,e_4\) the elementary symmetric functions of the Satake data \(a,b,c,d\),

\[ \prod _{i<j}(1-a_ia_jX)=1-e_2X+(e_1e_3-e_4)X^2-(e_1^2e_4+e_3^2-2e_2e_4)X^3+e_4(e_1e_3-e_4)X^4-e_2e_4^2X^5+e_4^3X^6 , \]
so the GL\(_6\) eigen-system of \(\wedge ^2\pi \) at an unramified \(p\) is determined by that of \(\pi \). The steps are
\[ \begin {aligned} c_k&=(-1)^k e_k(ab,ac,ad,bc,bd,cd),\\ e_6(\wedge ^2)&=(abcd)^3=e_4^3,\qquad e_{6-k}(\wedge ^2)=e_4^{3}\,e_k(\wedge ^2)(a^{-1},\dots )=e_4^{3-k}e_k(\wedge ^2),\\ c(l)_{k+1}&=c(\mathrm {tail})_{k+1}-a\,c(\mathrm {tail})_k\ \text {unfolded for}\ k\le 6,\ \text {each coefficient an identity of polynomials}. \end {aligned} \]

Listing 135: lang/ExtSquareLocal.lean

lang/ExtSquareLocal.lean

/-
  lang/ExtSquareLocal — T43, cycle 95 (functoriality, local, unramified): the exterior square GL4 → GL6.

  ∧² sends Satake data (a, b, c, d) of GL4 to the six products {ab, ac, ad, bc, bd, cd}. With e₁, …, e₄ the elementary
  symmetric functions (the GL4 eigen-system at p, GLnLocal), its Satake polynomial is
      1 − e₂X + (e₁e₃ − e₄)X² − (e₁²e₄ + e₃² − 2e₂e₄)X³ + e₄(e₁e₃ − e₄)X⁴ − e₂e₄²X⁵ + e₄³X⁶,
  a function of the GL4 data alone:
    cE              the polynomial in (e₁, e₂, e₃, e₄)
    ext2_poly       cf [ab, ac, ad, bc, bd, cd] = cE (e₁, e₂, e₃, e₄)
    ext2_lift       the GL6 eigen-system is the unique solution of conv (cE) h = δ — the local form of Kim's ∧² lift
  Checked first on 300 random integer quadruples.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.TensorLocal

namespace ExtSquareLocal

open GLnLocal

variable {R : Type} (K : DetChain.CRing R)

def cE (e₁ e₂ e₃ e₄ : R) : Nat → R
  | 0 => K.one
  | 1 => K.neg e₂
  | 2 => K.add (K.mul e₁ e₃) (K.neg e₄)
  | 3 => K.neg (K.add (K.add (K.mul (K.mul e₁ e₁) e₄) (K.mul e₃ e₃)) (K.neg (K.add (K.mul e₂ e₄) (K.mul e₂ e₄))))
  | 4 => K.mul e₄ (K.add (K.mul e₁ e₃) (K.neg e₄))
  | 5 => K.neg (K.mul e₂ (K.mul e₄ e₄))
  | 6 => K.mul e₄ (K.mul e₄ e₄)
  | _ + 7 => K.zero

/-- **The ∧² Satake polynomial depends only on the GL4 data.** -/
theorem ext2_poly (a b c d : R) : ∀ k,
    cf K [K.mul a b, K.mul a c, K.mul a d, K.mul b c, K.mul b d, K.mul c d] k = cE K (K.add (K.add (K.add a b) c) d) (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d)) (K.add (K.add (K.add (K.mul (K.mul a b) c) (K.mul (K.mul a b) d)) (K.mul (K.mul a c) d)) (K.mul (K.mul b c) d)) (K.mul (K.mul (K.mul a b) c) d) k
  | 0 => by
    show K.one = K.one
    lib_ring
  | 1 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.one)))) (K.neg (K.mul (K.mul b d) (K.one)))) (K.neg (K.mul (K.mul b c) (K.one)))) (K.neg (K.mul (K.mul a d) (K.one)))) (K.neg (K.mul (K.mul a c) (K.one)))) (K.neg (K.mul (K.mul a b) (K.one))) = K.neg (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d))
    lib_ring
  | 2 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.one))))))) (K.neg (K.mul (K.mul b c) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.one)))) (K.neg (K.mul (K.mul b d) (K.one))))))) (K.neg (K.mul (K.mul a d) (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.one)))) (K  -- (+745 chars; full line in the Zenodo source)
    lib_ring
  | 3 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero))))))) (K.neg (K.mul (K.mul b c) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.one)))))))))) (K.neg (K.mul (K.mul a d) (K.add (K.add (K.add (  -- (+2128 chars; full line in the Zenodo source)
    lib_ring
  | 4 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero))))))) (K.neg (K.mul (K.mul b c) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))))))))) (K.neg (K.mul (K.mul a d) (K.add (K.add (K.add   -- (+2360 chars; full line in the Zenodo source)
    lib_ring
  | 5 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero))))))) (K.neg (K.mul (K.mul b c) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))))))))) (K.neg (K.mul (K.mul a d) (K.add (K.add (K.add   -- (+2599 chars; full line in the Zenodo source)
    lib_ring
  | 6 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero))))))) (K.neg (K.mul (K.mul b c) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))))))))) (K.neg (K.mul (K.mul a d) (K.add (K.add (K.add   -- (+2561 chars; full line in the Zenodo source)
    lib_ring
  | k + 7 => by
    show K.add (K.add (K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero))))))) (K.neg (K.mul (K.mul b c) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))) (K.neg (K.mul (K.mul b d) (K.add (K.zero) (K.neg (K.mul (K.mul c d) (K.zero)))))))))) (K.neg (K.mul (K.mul a d) (K.add (K.add (K.add   -- (+2459 chars; full line in the Zenodo source)
    lib_ring

/-- **Local ∧² : GL4 → GL6 functoriality.** -/
theorem ext2_lift (a b c d : R) (g : Nat → R)
    (hg : ∀ k, conv K (cE K (K.add (K.add (K.add a b) c) d) (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d)) (K.add (K.add (K.add (K.mul (K.mul a b) c) (K.mul (K.mul a b) d)) (K.mul (K.mul a c) d)) (K.mul (K.mul b c) d)) (K.mul (K.mul (K.mul a b) c) d)) g k = delta K k) :
    ∀ k, hk K [K.mul a b, K.mul a c, K.mul a d, K.mul b c, K.mul b d, K.mul c d] k = g k := fun k =>
  conv_unique K (cE K (K.add (K.add (K.add a b) c) d) (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d)) (K.add (K.add (K.add (K.mul (K.mul a b) c) (K.mul (K.mul a b) d)) (K.mul (K.mul a c) d)) (K.mul (K.mul b c) d)) (K.mul (K.mul (K.mul a b) c) d)) _ g rfl
    (fun j => (conv_ext_left K _ _ _ (fun i => (ext2_poly K a b c d i).symm) j).trans
      (conv_cf_hk K [K.mul a b, K.mul a c, K.mul a d, K.mul b c, K.mul b d, K.mul c d] j)) hg k k (Nat.le_refl k)

end ExtSquareLocal

#print axioms ExtSquareLocal.ext2_poly
#print axioms ExtSquareLocal.ext2_lift

78.44 Local functoriality: base change to the unramified quadratic extension

At a prime inert in a quadratic extension the Frobenius of the residue field is \(\mathrm {Frob}_p^2\), so the Satake data become \((\alpha _i^2)\):

\[ \begin {aligned} \mathrm {GL}_2&:\ \prod (1-\alpha _i^2X)=1-(t^2-2d)X+d^2X^2,\\ \mathrm {GL}_3&:\ 1-(e_1^2-2e_2)X+(e_2^2-2e_1e_3)X^2-e_3^2X^3,\\ \mathrm {GL}_4&:\ 1-(e_1^2-2e_2)X+(e_2^2-2e_1e_3+2e_4)X^2-(e_3^2-2e_2e_4)X^3+e_4^2X^4 . \end {aligned} \]
The steps are
\[ \begin {aligned} e_k(\alpha ^2)&=\textstyle \sum _{i+j=2k}(-1)^{i+k}e_ie_j\quad (\prod (1-\alpha _iX)(1+\alpha _iX)=\prod (1-\alpha _i^2X^2)),\\ c(l)_{k+1}&=c(\mathrm {tail})_{k+1}-a\,c(\mathrm {tail})_k\ \text {unfolded, each coefficient an identity of polynomials}. \end {aligned} \]
For \(\mathrm {GL}_2\) with \(d=p\) the first coefficient is \(a(\mathbb F_{p^2})=a_p^2-2p\), proved above for the CM curves at every prime.

Listing 136: lang/BaseChangeLocal.lean

lang/BaseChangeLocal.lean

/-
  lang/BaseChangeLocal — T43, cycle 113 (functoriality, local): base change to the unramified quadratic extension.

  At a prime inert in a quadratic extension, base change sends Satake data (α_i) to (α_i²) — the Frobenius of the residue
  field 𝔽_{p²} is Frob_p². Its Satake polynomial Π(1 − α_i² X) depends only on the elementary symmetric functions e_k:
    bc2_poly   GL2: 1 − (t² − 2d) X + d² X²
    bc3_poly   GL3: 1 − (e₁² − 2e₂) X + (e₂² − 2e₁e₃) X² − e₃² X³
    bc4_poly   GL4: 1 − (e₁² − 2e₂) X + (e₂² − 2e₁e₃ + 2e₄) X² − (e₃² − 2e₂e₄) X³ + e₄² X⁴
  For GL2 the first coefficient is a(𝔽_{p²}) = a_p² − 2p (with d = p) — the identity proved for the CM curves at every
  prime (EveryPrime, EveryPrime3, FamilyHD). Checked first on 200 random integer quadruples.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.TensorLocal

namespace BaseChangeLocal

open GLnLocal

variable {R : Type} (K : DetChain.CRing R)

theorem bc2_poly (α β : R) : ∀ k, cf K [K.mul α α, K.mul β β] k =
    (match k with
     | 0 => K.one
     | 1 => K.neg (K.add (K.mul (K.add α β) (K.add α β)) (K.neg (K.add (K.mul α β) (K.mul α β))))
     | 2 => K.mul (K.mul α β) (K.mul α β)
     | _ + 3 => K.zero)
  | 0 => by
    show K.one = K.one
    lib_ring
  | 1 => by
    show K.add (K.add (K.zero) (K.neg (K.mul (K.mul β β) (K.one)))) (K.neg (K.mul (K.mul α α) (K.one))) = K.neg (K.add (K.mul (K.add α β) (K.add α β)) (K.neg (K.add (K.mul α β) (K.mul α β))))
    lib_ring
  | 2 => by
    show K.add (K.add (K.zero) (K.neg (K.mul (K.mul β β) (K.zero)))) (K.neg (K.mul (K.mul α α) (K.add (K.zero) (K.neg (K.mul (K.mul β β) (K.one)))))) = K.mul (K.mul α β) (K.mul α β)
    lib_ring
  | k + 3 => by
    show K.add (K.add (K.zero) (K.neg (K.mul (K.mul β β) (K.zero)))) (K.neg (K.mul (K.mul α α) (K.add (K.zero) (K.neg (K.mul (K.mul β β) (K.zero)))))) = K.zero
    lib_ring

theorem bc3_poly (a b c : R) : ∀ k, cf K [K.mul a a, K.mul b b, K.mul c c] k =
    (match k with
     | 0 => K.one
     | 1 => K.neg (K.add (K.mul (K.add (K.add a b) c) (K.add (K.add a b) c)) (K.neg (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul b c)) (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul b c)))))
     | 2 => K.add (K.mul (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul b c)) (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul b c))) (K.neg (K.add (K.mul (K.add (K.add a b) c) (K.mul (K.mul a b) c)) (K.mul (K.add (K.add a b) c) (K.mul (K.mul a b) c))))
     | 3 => K.neg (K.mul (K.mul (K.mul a b) c) (K.mul (K.mul a b) c))
     | _ + 4 => K.zero)
  | 0 => by
    show K.one = K.one
    lib_ring
  | 1 => by
    show K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c c) (K.one)))) (K.neg (K.mul (K.mul b b) (K.one)))) (K.neg (K.mul (K.mul a a) (K.one))) = K.neg (K.add (K.mul (K.add (K.add a b) c) (K.add (K.add a b) c)) (K.neg (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul b c)) (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul b c)))))
    lib_ring
  | 2 => by
    show K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c c) (K.zero)))) (K.neg (K.mul (K.mul b b) (K.add (K.zero) (K.neg (K.mul (K.mul c c) (K.one))))))) (K.neg (K.mul (K.mul a a) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c c) (K.one)))) (K.neg (K.mul (K.mul b b) (K.one)))))) = K.add (K.mul (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul b c)) (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul b c)  -- (+122 chars; full line in the Zenodo source)
    lib_ring
  | 3 => by
    show K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c c) (K.zero)))) (K.neg (K.mul (K.mul b b) (K.add (K.zero) (K.neg (K.mul (K.mul c c) (K.zero))))))) (K.neg (K.mul (K.mul a a) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c c) (K.zero)))) (K.neg (K.mul (K.mul b b) (K.add (K.zero) (K.neg (K.mul (K.mul c c) (K.one))))))))) = K.neg (K.mul (K.mul (K.mul a b) c) (K.mul (K.mul a b) c))
    lib_ring
  | k + 4 => by
    show K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c c) (K.zero)))) (K.neg (K.mul (K.mul b b) (K.add (K.zero) (K.neg (K.mul (K.mul c c) (K.zero))))))) (K.neg (K.mul (K.mul a a) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul c c) (K.zero)))) (K.neg (K.mul (K.mul b b) (K.add (K.zero) (K.neg (K.mul (K.mul c c) (K.zero))))))))) = K.zero
    lib_ring

theorem bc4_poly (a b c d : R) : ∀ k, cf K [K.mul a a, K.mul b b, K.mul c c, K.mul d d] k =
    (match k with
     | 0 => K.one
     | 1 => K.neg (K.add (K.mul (K.add (K.add (K.add a b) c) d) (K.add (K.add (K.add a b) c) d)) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d)) (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d)))))
     | 2 => K.add (K.add (K.mul (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d)) (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d))) (K.neg (K.add (K.mul (K.add (K.add (K.add a b) c) d) (K.add (K.add (K.add (K.mul (K.mul a b) c) (K.mul (K.mul a b) d)) (K.mul (K.mul a c) d)) (  -- (+249 chars; full line in the Zenodo source)
     | 3 => K.neg (K.add (K.mul (K.add (K.add (K.add (K.mul (K.mul a b) c) (K.mul (K.mul a b) d)) (K.mul (K.mul a c) d)) (K.mul (K.mul b c) d)) (K.add (K.add (K.add (K.mul (K.mul a b) c) (K.mul (K.mul a b) d)) (K.mul (K.mul a c) d)) (K.mul (K.mul b c) d))) (K.neg (K.add (K.mul (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c d)) (K.mul (K.  -- (+177 chars; full line in the Zenodo source)
     | 4 => K.mul (K.mul (K.mul (K.mul a b) c) d) (K.mul (K.mul (K.mul a b) c) d)
     | _ + 5 => K.zero)
  | 0 => by
    show K.one = K.one
    lib_ring
  | 1 => by
    show K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.one)))) (K.neg (K.mul (K.mul c c) (K.one)))) (K.neg (K.mul (K.mul b b) (K.one)))) (K.neg (K.mul (K.mul a a) (K.one))) = K.neg (K.add (K.mul (K.add (K.add (K.add a b) c) d) (K.add (K.add (K.add a b) c) d)) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a b) (K.mul a c)) (K.mul a d)) (K.mul b c)) (K.mul b d)) (K.mul c  -- (+119 chars; full line in the Zenodo source)
    lib_ring
  | 2 => by
    show K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.zero)))) (K.neg (K.mul (K.mul c c) (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.one))))))) (K.neg (K.mul (K.mul b b) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.one)))) (K.neg (K.mul (K.mul c c) (K.one))))))) (K.neg (K.mul (K.mul a a) (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.one)))) (K.neg (K.mul (K  -- (+699 chars; full line in the Zenodo source)
    lib_ring
  | 3 => by
    show K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.zero)))) (K.neg (K.mul (K.mul c c) (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.zero))))))) (K.neg (K.mul (K.mul b b) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.zero)))) (K.neg (K.mul (K.mul c c) (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.one)))))))))) (K.neg (K.mul (K.mul a a) (K.add (K.add (K.add (K.zero) (K.neg  -- (+809 chars; full line in the Zenodo source)
    lib_ring
  | 4 => by
    show K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.zero)))) (K.neg (K.mul (K.mul c c) (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.zero))))))) (K.neg (K.mul (K.mul b b) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.zero)))) (K.neg (K.mul (K.mul c c) (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.zero)))))))))) (K.neg (K.mul (K.mul a a) (K.add (K.add (K.add (K.zero) (K.ne  -- (+361 chars; full line in the Zenodo source)
    lib_ring
  | k + 5 => by
    show K.add (K.add (K.add (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.zero)))) (K.neg (K.mul (K.mul c c) (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.zero))))))) (K.neg (K.mul (K.mul b b) (K.add (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.zero)))) (K.neg (K.mul (K.mul c c) (K.add (K.zero) (K.neg (K.mul (K.mul d d) (K.zero)))))))))) (K.neg (K.mul (K.mul a a) (K.add (K.add (K.add (K.zero) (K.ne  -- (+299 chars; full line in the Zenodo source)
    lib_ring

end BaseChangeLocal

#print axioms BaseChangeLocal.bc2_poly
#print axioms BaseChangeLocal.bc3_poly
#print axioms BaseChangeLocal.bc4_poly

For every \(n\), with \(c=\mathrm {cf}(\alpha )\),

\[ \mathrm {cf}(\alpha ^2)_k=\sum _{i+j=2k}c_i\,(-1)^jc_j , \]
from \(\prod (1-\alpha _iX)(1+\alpha _iX)=\prod (1-\alpha _i^2X^2)\) (the interleaved data \(\alpha _1,-\alpha _1,\alpha _2,-\alpha _2,\dots \)) and the independence of \(\mathrm {cf}\) from the order of the data.

Listing 137: lang/BaseChangeGLn.lean

lang/BaseChangeGLn.lean

/-
  lang/BaseChangeGLn — T43, cycle 114 (functoriality, local, every n): base change to the unramified quadratic extension.

  For Satake data l = (α_i) of GL_n, the base change at an inert prime has data (α_i²), and
      Π(1 − α_i² X²) = Π(1 − α_i X) · Π(1 + α_i X):
    il, cf_il_even, cf_il_odd    the interleaved list (α₁, −α₁, α₂, −α₂, …): its coefficients are those of (α_i²) at even
                                 degree and 0 at odd degree
    cf_swap, cf_move             the Satake coefficients do not depend on the order of the data
    cf_il_app                    cf(il l) = cf(l ++ (−l))
    base_change_gln              cf(α_i²)_k = Σ_{i + j = 2k} c_i · (−1)^j c_j    (c = cf l)
  So the base-changed Hecke eigen-system at p is determined by the GL_n eigen-system at p, for every n (BaseChangeLocal
  gives n ≤ 4 in closed form).

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.BaseChangeLocal
import lang.SymNLocal

namespace BaseChangeGLn

open GLnLocal SymNLocal

variable {R : Type} (K : DetChain.CRing R)

def il : List R → List R
  | [] => []
  | a :: l => a :: K.neg a :: il l

def sq : List R → List R
  | [] => []
  | a :: l => K.mul a a :: sq l

theorem cf2 (a : R) (L : List R) : ∀ m, cf K (a :: K.neg a :: L) (m + 2) = K.add (cf K L (m + 2)) (K.neg (K.mul (K.mul a a) (cf K L m)))
  | 0 => by
    show K.add (K.add (cf K L 2) (K.neg (K.mul (K.neg a) (cf K L 1)))) (K.neg (K.mul a (K.add (cf K L 1) (K.neg (K.mul (K.neg a) (cf K L 0)))))) = K.add (cf K L 2) (K.neg (K.mul (K.mul a a) (cf K L 0)))
    lib_ring
  | m + 1 => by
    show K.add (K.add (cf K L (m + 3)) (K.neg (K.mul (K.neg a) (cf K L (m + 2))))) (K.neg (K.mul a (K.add (cf K L (m + 2)) (K.neg (K.mul (K.neg a) (cf K L (m + 1))))))) = K.add (cf K L (m + 3)) (K.neg (K.mul (K.mul a a) (cf K L (m + 1))))
    lib_ring

theorem cf1 (a : R) (L : List R) : cf K (a :: K.neg a :: L) 1 = cf K L 1 := by
  show K.add (K.add (cf K L 1) (K.neg (K.mul (K.neg a) (cf K L 0)))) (K.neg (K.mul a (cf K L 0))) = cf K L 1
  lib_ring

theorem cf_il : ∀ (l : List R) (k : Nat), cf K (il K l) (2 * k) = cf K (sq K l) k ∧ cf K (il K l) (2 * k + 1) = K.zero
  | [], 0 => ⟨rfl, rfl⟩
  | [], _ + 1 => ⟨rfl, rfl⟩
  | a :: l, 0 => ⟨(cf_il l 0).1, by show cf K (a :: K.neg a :: il K l) 1 = _; rw [cf1]; exact (cf_il l 0).2⟩
  | a :: l, k + 1 => by
    constructor
    · rw [show 2 * (k + 1) = 2 * k + 2 by rfl]
      show cf K (a :: K.neg a :: il K l) (2 * k + 2) = K.add (cf K (sq K l) (k + 1)) (K.neg (K.mul (K.mul a a) (cf K (sq K l) k)))
      rw [cf2, show 2 * k + 2 = 2 * (k + 1) by rfl, (cf_il l (k + 1)).1, (cf_il l k).1]
    · rw [show 2 * (k + 1) + 1 = (2 * k + 1) + 2 by rfl]
      show cf K (a :: K.neg a :: il K l) ((2 * k + 1) + 2) = K.zero
      rw [cf2, show (2 * k + 1) + 2 = 2 * (k + 1) + 1 by rfl, (cf_il l (k + 1)).2, (cf_il l k).2, DetChain.mul_zero]
      lib_ring

/-- **Π(1 − α X)(1 + α X) = Π(1 − α² X²).** -/
theorem cf_il_even (l : List R) (k : Nat) : cf K (il K l) (2 * k) = cf K (sq K l) k := (cf_il K l k).1

theorem cf_ext_cons (a : R) (L1 L2 : List R) (h : ∀ k, cf K L1 k = cf K L2 k) : ∀ k, cf K (a :: L1) k = cf K (a :: L2) k
  | 0 => h 0
  | k + 1 => by
    show K.add (cf K L1 (k + 1)) (K.neg (K.mul a (cf K L1 k))) = K.add (cf K L2 (k + 1)) (K.neg (K.mul a (cf K L2 k)))
    rw [h, h]

theorem cf_swap (a b : R) (L : List R) : ∀ k, cf K (a :: b :: L) k = cf K (b :: a :: L) k
  | 0 => rfl
  | 1 => by
    show K.add (K.add (cf K L 1) (K.neg (K.mul b (cf K L 0)))) (K.neg (K.mul a (cf K L 0)))
      = K.add (K.add (cf K L 1) (K.neg (K.mul a (cf K L 0)))) (K.neg (K.mul b (cf K L 0)))
    lib_ring
  | k + 2 => by
    show K.add (K.add (cf K L (k + 2)) (K.neg (K.mul b (cf K L (k + 1))))) (K.neg (K.mul a (K.add (cf K L (k + 1)) (K.neg (K.mul b (cf K L k))))))
      = K.add (K.add (cf K L (k + 2)) (K.neg (K.mul a (cf K L (k + 1))))) (K.neg (K.mul b (K.add (cf K L (k + 1)) (K.neg (K.mul a (cf K L k))))))
    lib_ring

/-- moving one datum to the front does not change the coefficients. -/
theorem cf_move (a : R) : ∀ (l1 l2 : List R) (k : Nat), cf K (l1 ++ a :: l2) k = cf K (a :: (l1 ++ l2)) k
  | [], _, _ => rfl
  | b :: l1, l2, k => by
    show cf K (b :: (l1 ++ a :: l2)) k = cf K (a :: b :: (l1 ++ l2)) k
    rw [cf_ext_cons K b _ _ (fun k => cf_move a l1 l2 k) k, cf_swap]

theorem cf_il_app : ∀ (l : List R) (k : Nat), cf K (il K l) k = cf K (l ++ l.map K.neg) k
  | [], _ => rfl
  | a :: l, k => by
    show cf K (a :: K.neg a :: il K l) k = cf K (a :: (l ++ K.neg a :: l.map K.neg)) k
    apply cf_ext_cons
    intro k
    rw [cf_move]
    apply cf_ext_cons
    exact cf_il_app l

theorem map_neg (l : List R) : l.map K.neg = l.map (K.mul (K.neg K.one)) := by
  induction l with
  | nil => rfl
  | cons a l ih =>
    show K.neg a :: l.map K.neg = K.mul (K.neg K.one) a :: l.map (K.mul (K.neg K.one))
    rw [ih, show K.mul (K.neg K.one) a = K.neg a by lib_ring]

/-- **Local base change for GL_n**: cf(α_i²)_k = Σ_{i+j=2k} c_i (−1)^j c_j. -/
theorem base_change_gln (l : List R) (k : Nat) :
    cf K (sq K l) k = conv K (cf K l) (fun j => K.mul (pw K (K.neg K.one) j) (cf K l j)) (2 * k) := by
  rw [← cf_il_even, cf_il_app, cf_append, map_neg]
  exact conv_ext K _ _ _ (fun j => cf_scale K (K.neg K.one) l j) _

end BaseChangeGLn

#print axioms BaseChangeGLn.cf2
#print axioms BaseChangeGLn.cf1
#print axioms BaseChangeGLn.cf_il
#print axioms BaseChangeGLn.cf_il_even
#print axioms BaseChangeGLn.cf_ext_cons
#print axioms BaseChangeGLn.cf_swap
#print axioms BaseChangeGLn.cf_move
#print axioms BaseChangeGLn.cf_il_app
#print axioms BaseChangeGLn.map_neg
#print axioms BaseChangeGLn.base_change_gln

Base change commutes with \(\mathrm {Sym}^n\): the squares of the \(\mathrm {Sym}^n\) data of \((\alpha ,\beta )\) are the \(\mathrm {Sym}^n\) data of \((\alpha ^2,\beta ^2)\), so with \(Q_n(t,d)\) the \(\mathrm {Sym}^n\) Satake polynomial,

\[ Q_n(t^2-2d,\,d^2)_k=\sum _{i+j=2k}Q_n(t,d)_i\,(-1)^jQ_n(t,d)_j . \]

Listing 138: lang/SymBC.lean

lang/SymBC.lean

/-
  lang/SymBC — T43, cycle 122 (functoriality, local): Sym^n commutes with unramified quadratic base change.

  At a prime inert in a quadratic extension, base change squares the Satake data; Sym^n takes (α, β) to (α^(n−i) β^i):
    sq_append, sq_map            squaring data lists
    sym_bc                       (Sym^n data of (α, β))² = Sym^n data of (α², β²)
    sym_bc_poly                  Q_n(t² − 2d, d²)_k = Σ_{i+j=2k} Q_n(t, d)_i (−1)^j Q_n(t, d)_j
  i.e. BC ∘ Sym^n = Sym^n ∘ BC on unramified eigen-systems, in terms of (t, d) = (a(p), p^(k−1)χ(p)) alone.

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.BaseChangeGLn

namespace SymBC

open GLnLocal SymNLocal BaseChangeGLn

variable {R : Type} (K : DetChain.CRing R)

theorem sq_append : ∀ l1 l2 : List R, sq K (l1 ++ l2) = sq K l1 ++ sq K l2
  | [], _ => rfl
  | a :: l1, l2 => by
    show K.mul a a :: sq K (l1 ++ l2) = K.mul a a :: (sq K l1 ++ sq K l2)
    rw [sq_append l1 l2]

theorem sq_map (c : R) : ∀ l : List R, sq K (l.map (K.mul c)) = (sq K l).map (K.mul (K.mul c c))
  | [] => rfl
  | a :: l => by
    show K.mul (K.mul c a) (K.mul c a) :: sq K (l.map (K.mul c)) = K.mul (K.mul c c) (K.mul a a) :: (sq K l).map (K.mul (K.mul c c))
    rw [sq_map c l, show K.mul (K.mul c a) (K.mul c a) = K.mul (K.mul c c) (K.mul a a) by lib_ring]

theorem sym_bc (α β : R) : ∀ n, sq K (symL K α β n) = symL K (K.mul α α) (K.mul β β) n
  | 0 => by show [K.mul K.one K.one] = [K.one]; rw [K.one_mul]
  | 1 => rfl
  | n + 2 => by
    show sq K ([pw K α (n + 2), pw K β (n + 2)] ++ (symL K α β n).map (K.mul (K.mul α β)))
      = [pw K (K.mul α α) (n + 2), pw K (K.mul β β) (n + 2)] ++ (symL K (K.mul α α) (K.mul β β) n).map (K.mul (K.mul (K.mul α α) (K.mul β β)))
    rw [sq_append, sq_map, sym_bc α β n,
      show pw K (K.mul α α) (n + 2) = K.mul (pw K α (n + 2)) (pw K α (n + 2)) from Frobenius.pw_mul K α α (n + 2),
      show pw K (K.mul β β) (n + 2) = K.mul (pw K β (n + 2)) (pw K β (n + 2)) from Frobenius.pw_mul K β β (n + 2),
      show K.mul (K.mul α β) (K.mul α β) = K.mul (K.mul α α) (K.mul β β) by lib_ring]
    rfl

/-- **BC ∘ Sym^n = Sym^n ∘ BC**, as an identity of Satake polynomials in (t, d). -/
theorem sym_bc_poly (α β : R) (n k : Nat) :
    Q K (K.add (K.mul α α) (K.mul β β)) (K.mul (K.mul α α) (K.mul β β)) n k
      = conv K (Q K (K.add α β) (K.mul α β) n)
          (fun j => K.mul (pw K (K.neg K.one) j) (Q K (K.add α β) (K.mul α β) n j)) (2 * k) := by
  rw [← symn_poly, ← sym_bc, base_change_gln]
  rw [conv_ext_left K _ _ _ (fun i => symn_poly K α β n i) (2 * k)]
  exact conv_ext K _ _ _ (fun j => by rw [symn_poly]) _

end SymBC

#print axioms SymBC.sq_append
#print axioms SymBC.sq_map
#print axioms SymBC.sym_bc
#print axioms SymBC.sym_bc_poly

The adjoint lift \(\mathrm {Ad}\,\pi =\mathrm {Sym}^2\pi \otimes \det ^{-1}\) has data \(e\cdot (\alpha ^2,\alpha \beta ,\beta ^2)\), \(e=(\alpha \beta )^{-1}\), and

\[ \prod (1-\lambda X)=1-(t^2e-1)X+(t^2e-1)X^2-X^3=(1-X)\bigl (1-(t^2e-2)X+X^2\bigr ). \]

Listing 139: lang/AdjointLocal.lean

lang/AdjointLocal.lean

/-
  lang/AdjointLocal — T43, cycle 142 (functoriality, local, unramified): the adjoint lift Ad : GL2 → GL3 (Gelbart–Jacquet).

  Ad π = Sym² π ⊗ det⁻¹ has Satake data (α/β, 1, β/α) = e·(α², αβ, β²) with e = (αβ)⁻¹. With t = α + β, d = αβ, e d = 1:
      Π(1 − λX) = 1 − (t²e − 1)X + (t²e − 1)X² − X³ = (1 − X)(1 − (t²e − 2)X + X²),
  a function of (t, e) only:
    ad_poly      cf [eα², eαβ, eβ²] = 1, −(t²e − 1), t²e − 1, −1, 0, …          (Sym2Local.sym2_poly + SymNLocal.cf_scale)

  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Sym2Local
import lang.SymNLocal
import lib.RingTac

namespace AdjointLocal

open GLnLocal SymNLocal

variable {R : Type} (K : DetChain.CRing R)

def cAd (t e : R) : Nat → R
  | 0 => K.one
  | 1 => K.neg (K.add (K.mul (K.mul t t) e) (K.neg K.one))
  | 2 => K.add (K.mul (K.mul t t) e) (K.neg K.one)
  | 3 => K.neg K.one
  | _ + 4 => K.zero

/-- **The adjoint Satake polynomial depends only on (t, d⁻¹).** -/
theorem ad_poly (α β e : R) (he : K.mul e (K.mul α β) = K.one) : ∀ k,
    cf K ([K.mul α α, K.mul α β, K.mul β β].map (K.mul e)) k = cAd K (K.add α β) e k
  | 0 => rfl
  | 1 => by
    rw [cf_scale, Sym2Local.sym2_poly]
    show K.mul (K.mul K.one e) (K.neg (K.add (K.mul (K.add α β) (K.add α β)) (K.neg (K.mul α β)))) = _
    rw [show K.mul (K.mul K.one e) (K.neg (K.add (K.mul (K.add α β) (K.add α β)) (K.neg (K.mul α β))))
        = K.neg (K.add (K.mul (K.mul (K.add α β) (K.add α β)) e) (K.neg (K.mul e (K.mul α β)))) by lib_ring, he]
    rfl
  | 2 => by
    rw [cf_scale, Sym2Local.sym2_poly]
    show K.mul (K.mul (K.mul K.one e) e) (K.mul (K.mul α β) (K.add (K.mul (K.add α β) (K.add α β)) (K.neg (K.mul α β)))) = _
    rw [show K.mul (K.mul (K.mul K.one e) e) (K.mul (K.mul α β) (K.add (K.mul (K.add α β) (K.add α β)) (K.neg (K.mul α β))))
        = K.mul (K.mul e (K.mul α β)) (K.add (K.mul (K.mul (K.add α β) (K.add α β)) e) (K.neg (K.mul e (K.mul α β)))) by lib_ring, he]
    show _ = K.add (K.mul (K.mul (K.add α β) (K.add α β)) e) (K.neg K.one)
    lib_ring
  | 3 => by
    rw [cf_scale, Sym2Local.sym2_poly]
    show K.mul (K.mul (K.mul (K.mul K.one e) e) e) (K.neg (K.mul (K.mul (K.mul α β) (K.mul α β)) (K.mul α β))) = _
    rw [show K.mul (K.mul (K.mul (K.mul K.one e) e) e) (K.neg (K.mul (K.mul (K.mul α β) (K.mul α β)) (K.mul α β)))
        = K.neg (K.mul (K.mul (K.mul e (K.mul α β)) (K.mul e (K.mul α β))) (K.mul e (K.mul α β))) by lib_ring, he]
    show _ = K.neg K.one; lib_ring
  | k + 4 => by
    rw [cf_scale, Sym2Local.sym2_poly]
    show K.mul _ K.zero = K.zero
    exact DetChain.mul_zero K _

end AdjointLocal

#print axioms AdjointLocal.ad_poly

78.45 Local functoriality in a finite model: Frobenius reciprocity

For a finite group \(G\), a subgroup \(H\) and a class function \(\chi \) on \(G\),

\[ \langle \operatorname {Ind}f,\chi \rangle _G=|G|\textstyle \sum _{y\in H}f(y)\,\chi (y^{-1}). \]
The steps are
\[ \begin {aligned} F(\sigma x)&=\textstyle \sum _y[y=\sigma x]F(y)\ \Rightarrow \ \sum _x F(\sigma x)=\sum _x F(x),\\ \operatorname {Ind}f(kxk^{-1})&=\operatorname {Ind}f(x)\quad (g\mapsto gk),\\ \textstyle \sum _x\sum _g f^\circ (gxg^{-1})\chi (x^{-1})&=\textstyle \sum _g\sum _y f^\circ (y)\chi (y^{-1})=|G|\sum _{y\in H}f(y)\chi (y^{-1}). \end {aligned} \]

Listing 140: FiniteRecChain.lean

FiniteRecChain.lean

/-
  FiniteRecChain — T31 R6: a two-sided finite model of reciprocity and functoriality.

  Both sides are finite: a finite group `G` (listed once each, `Complete`), a subgroup `H`
  (a Boolean membership test), and functions with values in any commutative ring.

  "Automorphic side": class functions on G (and on H).  "Galois side": the same functions read
  as characters.  Functoriality between H and G is induction / restriction.

    delta_sum         Σ_y [y = z] F(y) = (Σ_y [y = z]) · F(z)
    reindex           a bijection σ of G (inverse τ) leaves Σ_x F(x) unchanged
    ind_class         the induced function Ind f is a class function on G
    frobenius         ⟨Ind f, χ⟩_G = |G| · ⟨f, Res χ⟩_H  for every class function χ on G
                      (Frobenius reciprocity: transfer H → G is adjoint to restriction)

  Lean 4 core only, proofs by hand, no axioms.
-/
import YMChain

namespace FiniteRecChain

open YMChain

variable {R : Type} (Rg : DetChain.CRing R) {G : Type} [DecidableEq G]

theorem ite_pos' {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : c) : @ite α c d a b = a := by
  cases d with
  | isTrue _ => rfl
  | isFalse hn => exact absurd h hn

theorem ite_neg' {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : ¬ c) : @ite α c d a b = b := by
  cases d with
  | isTrue hc => exact absurd hc h
  | isFalse _ => rfl

/-- `[y = z]` as a ring element. -/
def delta (y z : G) : R := if y = z then Rg.one else Rg.zero

/-- Every element of `G` occurs exactly once in `els` (ring form of "count = 1"). -/
def Complete (els : List G) : Prop := ∀ z : G, sumK Rg (fun y => delta Rg y z) els = Rg.one

theorem delta_symm (y z : G) : delta Rg y z = delta Rg (R := R) z y := by
  unfold delta
  by_cases h : y = z
  · rw [ite_pos' _ _ h, ite_pos' _ _ h.symm]
  · rw [ite_neg' _ _ h, ite_neg' _ _ (fun h' => h h'.symm)]

theorem delta_mul (F : G → R) (y z : G) : Rg.mul (delta Rg y z) (F y) = Rg.mul (delta Rg y z) (F z) := by
  unfold delta
  by_cases h : y = z
  · rw [ite_pos' _ _ h, h]
  · rw [ite_neg' _ _ h, DetChain.zero_mul', DetChain.zero_mul']

theorem delta_sum (F : G → R) (z : G) : ∀ l : List G,
    sumK Rg (fun y => Rg.mul (delta Rg y z) (F y)) l = Rg.mul (sumK Rg (fun y => delta Rg y z) l) (F z)
  | [] => (DetChain.zero_mul' Rg (F z)).symm
  | y :: l => by
    show Rg.add (Rg.mul (delta Rg y z) (F y)) (sumK Rg (fun y => Rg.mul (delta Rg y z) (F y)) l)
      = Rg.mul (Rg.add (delta Rg y z) (sumK Rg (fun y => delta Rg y z) l)) (F z)
    rw [delta_sum F z l, delta_mul Rg F y z, Rg.mul_comm (Rg.add _ _), Rg.distrib,
      Rg.mul_comm (F z), Rg.mul_comm (F z)]

theorem pick (els : List G) (hc : Complete Rg els) (F : G → R) (z : G) :
    sumK Rg (fun y => Rg.mul (delta Rg y z) (F y)) els = F z := by
  rw [delta_sum, hc z, Rg.one_mul]

/-- **Reindexing.** A bijection `σ` (inverse `τ`) of a completely listed finite set
    leaves sums unchanged. -/
theorem reindex (els : List G) (hc : Complete Rg els) (σ τ : G → G)
    (hστ : ∀ x, τ (σ x) = x) (hτσ : ∀ y, σ (τ y) = y) (F : G → R) :
    sumK Rg (fun x => F (σ x)) els = sumK Rg F els := by
  have e1 : sumK Rg (fun x => F (σ x)) els
      = sumK Rg (fun x => sumK Rg (fun y => Rg.mul (delta Rg y (σ x)) (F y)) els) els :=
    sumK_ext Rg _ _ (fun x => (pick Rg els hc F (σ x)).symm) els
  rw [e1, sumK_swap]
  refine sumK_ext Rg _ _ (fun y => ?_) els
  -- Σ_x [y = σ x] F y = F y
  have e2 : sumK Rg (fun x => Rg.mul (delta Rg y (σ x)) (F y)) els
      = sumK Rg (fun x => Rg.mul (delta Rg x (τ y)) (F y)) els :=
    sumK_ext Rg _ _ (fun x => by
      show Rg.mul (delta Rg y (σ x)) (F y) = Rg.mul (delta Rg x (τ y)) (F y)
      have : delta Rg (R := R) y (σ x) = delta Rg x (τ y) := by
        unfold delta
        by_cases h : y = σ x
        · have h' : x = τ y := by rw [h, hστ]
          rw [ite_pos' _ _ h, ite_pos' _ _ h']
        · have h' : ¬ x = τ y := fun h' => h (by rw [h', hτσ])
          rw [ite_neg' _ _ h, ite_neg' _ _ h']
      rw [this]) els
  rw [e2, ← sumK_mul, hc (τ y), Rg.one_mul]

/-- `f` extended by zero outside `H`. -/
def ext0 (inH : G → Bool) (f : G → R) (x : G) : R := if inH x = true then f x else Rg.zero

section Frob
variable (K : Grp G) (els : List G) (inH : G → Bool)

/-- Class function on `G`, ring-valued. -/
def ClassR (χ : G → R) : Prop := ∀ g y, χ (K.mul (K.mul g y) (K.inv g)) = χ y

/-- Induction: `(Ind f)(x) = Σ_g f°(g x g⁻¹)` (that is `|H|` times the usual normalisation). -/
def ind (f : G → R) (x : G) : R := sumK Rg (fun g => ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) els

/-- Ring-valued pairing `⟨a, b⟩ = Σ_x a(x) b(x⁻¹)`. -/
def pair (a b : G → R) : R := sumK Rg (fun x => Rg.mul (a x) (b (K.inv x))) els

omit [DecidableEq G] in
theorem conj_conj (g h x : G) :
    K.mul (K.mul g (K.mul (K.mul h x) (K.inv h))) (K.inv g)
      = K.mul (K.mul (K.mul g h) x) (K.inv (K.mul g h)) := by
  rw [inv_mul_rev, ← K.assoc, ← K.assoc, ← K.assoc, K.assoc g h x]

/-- **The induced function is a class function.** -/
theorem ind_class (hc : Complete Rg els) (f : G → R) (h x : G) :
    ind Rg K els inH f (K.mul (K.mul h x) (K.inv h)) = ind Rg K els inH f x := by
  show sumK Rg (fun g => ext0 Rg inH f (K.mul (K.mul g (K.mul (K.mul h x) (K.inv h))) (K.inv g))) els
    = sumK Rg (fun g => ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) els
  have e : sumK Rg (fun g => ext0 Rg inH f (K.mul (K.mul g (K.mul (K.mul h x) (K.inv h))) (K.inv g))) els
      = sumK Rg (fun g => (fun k => ext0 Rg inH f (K.mul (K.mul k x) (K.inv k))) (K.mul g h)) els :=
    sumK_ext Rg _ _ (fun g => by show ext0 Rg inH f _ = ext0 Rg inH f _; rw [conj_conj]) els
  rw [e]
  exact reindex Rg els hc (fun g => K.mul g h) (fun k => K.mul k (K.inv h))
    (fun g => by show K.mul (K.mul g h) (K.inv h) = g; rw [K.assoc, K.mul_inv, K.mul_one])
    (fun k => by show K.mul (K.mul k (K.inv h)) h = k; rw [K.assoc, K.inv_mul, K.mul_one])
    (fun k => ext0 Rg inH f (K.mul (K.mul k x) (K.inv k)))

omit [DecidableEq G] in
theorem conj_inv_conj (g x : G) :
    K.inv (K.mul (K.mul g x) (K.inv g)) = K.mul (K.mul g (K.inv x)) (K.inv g) := by
  rw [inv_mul_rev, inv_mul_rev, inv_inv, K.assoc]

/-- **Frobenius reciprocity** (transfer from `H` to `G` is adjoint to restriction):
    `⟨Ind f, χ⟩_G = |G| · Σ_{y ∈ H} f(y) χ(y⁻¹)` for every class function `χ` on `G`,
    with `|G| = Σ_g 1`. -/
theorem frobenius (hc : Complete Rg els) (f χ : G → R) (hχ : ClassR K χ) :
    pair Rg K els (ind Rg K els inH f) χ
      = Rg.mul (sumK Rg (fun _ => Rg.one) els)
          (sumK Rg (fun y => Rg.mul (ext0 Rg inH f y) (χ (K.inv y))) els) := by
  unfold pair ind
  have e1 : sumK Rg (fun x => Rg.mul (sumK Rg (fun g => ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) els) (χ (K.inv x))) els
      = sumK Rg (fun x => sumK Rg (fun g => Rg.mul (ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) (χ (K.inv x))) els) els :=
    sumK_ext Rg _ _ (fun x => sumK_mul Rg _ _ els) els
  rw [e1, sumK_swap]
  -- for each g: reindex x ↦ g x g⁻¹
  have e2 : ∀ g, sumK Rg (fun x => Rg.mul (ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) (χ (K.inv x))) els
      = sumK Rg (fun y => Rg.mul (ext0 Rg inH f y) (χ (K.inv y))) els := by
    intro g
    have e3 : sumK Rg (fun x => Rg.mul (ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) (χ (K.inv x))) els
        = sumK Rg (fun x => (fun y => Rg.mul (ext0 Rg inH f y) (χ (K.inv y))) (K.mul (K.mul g x) (K.inv g))) els :=
      sumK_ext Rg _ _ (fun x => by
        show Rg.mul (ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) (χ (K.inv x))
          = Rg.mul (ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) (χ (K.inv (K.mul (K.mul g x) (K.inv g))))
        rw [conj_inv_conj, hχ]) els
    rw [e3]
    exact reindex Rg els hc (fun x => K.mul (K.mul g x) (K.inv g)) (fun y => K.mul (K.mul (K.inv g) y) g)
      (fun x => by
        show K.mul (K.mul (K.inv g) (K.mul (K.mul g x) (K.inv g))) g = x
        rw [← K.assoc, ← K.assoc, K.inv_mul, K.one_mul, K.assoc, K.inv_mul, K.mul_one])
      (fun y => by
        show K.mul (K.mul g (K.mul (K.mul (K.inv g) y) g)) (K.inv g) = y
        rw [← K.assoc, ← K.assoc, K.mul_inv, K.one_mul, K.assoc, K.mul_inv, K.mul_one])
      (fun y => Rg.mul (ext0 Rg inH f y) (χ (K.inv y)))
  rw [sumK_ext Rg _ _ e2 els, sumK_mul Rg]
  exact sumK_ext Rg _ _ (fun _ => (Rg.one_mul _).symm) els

end Frob

end FiniteRecChain

#print axioms FiniteRecChain.ite_pos'
#print axioms FiniteRecChain.ite_neg'
#print axioms FiniteRecChain.delta_symm
#print axioms FiniteRecChain.delta_mul
#print axioms FiniteRecChain.delta_sum
#print axioms FiniteRecChain.pick
#print axioms FiniteRecChain.reindex
#print axioms FiniteRecChain.conj_conj
#print axioms FiniteRecChain.ind_class
#print axioms FiniteRecChain.conj_inv_conj
#print axioms FiniteRecChain.frobenius

78.46 Finite models: abelian and \(S_3\) reciprocity

For distinct odd primes \(p,q\), and for the \(S_3\)-extension cut out by \(x^3-x-1\),

\[ \Bigl (\frac pq\Bigr )\Bigl (\frac qp\Bigr )=(-1)^{\frac {p-1}2\frac {q-1}2},\qquad a_p\bigl (\eta (z)\eta (23z)\bigr )=r_p-1 , \]
with \(r_p\) the number of roots of \(x^3-x-1\) mod \(p\). The steps are
\[ \begin {aligned} a^h&\equiv \pm 1\ (p),\quad \text {a polynomial of degree }n\ \text {has}\le n\ \text {roots},\\ \Bigl (\frac ap\Bigr )&=(-1)^{\mu (h,a)}\quad (\text {Gauss's lemma}),\ \text {lattice-point count for}\ \mu ,\\ \chi& =\#\mathrm {Fix}-1\ \text {on}\ S_3,\ \text {checked prime by prime against the }q\text {-expansion}. \end {aligned} \]

Listing 141: QRChain.lean

QRChain.lean

/-
  QRChain.lean
  Quadratic reciprocity for all distinct odd primes p, q (links 42-46).

  Lean 4 core plus FTAChain and FrobChain. Proofs are hand-written terms
  and plain rewriting; no omega / simp / ring / decide / norm_num /
  linarith. Divisibility and congruence are FTAChain's `Dv` and `rem`.

  An odd prime is written `h + h + 1`, so that (p-1)/2 is `h`.
  Signs are Booleans: `qr p a = true` means "a is a square mod p",
  and the sign (-1)^n is read from the parity `par n`.

  Proved here:
    roots_le        a polynomial over F_p with leading coefficient
                    not ≡ 0 has at most (degree) distinct roots
    euler_pm        p ∤ a → a^h ≡ 1 or a^h ≡ p-1
    euler_iff       p ∤ a → (a^h ≡ 1 ↔ a is a square mod p)
    gauss_lemma     p ∤ a → (a is a square ↔ μ is even)
    eisenstein_par  a odd, p ∤ a → μ ≡ Σ_{k=1}^{h} ⌊k a / p⌋ (mod 2)
    lattice         Σ_{k=1}^{h} ⌊k q/p⌋ + Σ_{k=1}^{g} ⌊k p/q⌋ = h g
    qr_law          (p|q)(q|p) = (-1)^(h g)
-/
import FrobChain

namespace QRChain
open FTAChain FrobChain

/-! ## Order and cancellation -/

theorem add_left_cancel' : ∀ (a b c : Nat), a + b = a + c → b = c
  | 0, b, c, h => by rw [nzero_add, nzero_add] at h; exact h
  | a + 1, b, c, h => by
    rw [Nat.add_right_comm a 1 b, Nat.add_right_comm a 1 c] at h
    exact add_left_cancel' a b c (succ_inj' h)

theorem add_right_cancel' (a b c : Nat) (h : b + a = c + a) : b = c := by
  rw [Nat.add_comm b a, Nat.add_comm c a] at h
  exact add_left_cancel' a b c h

theorem le_of_add_le_add_left' (a b c : Nat) (h : a + b ≤ a + c) : b ≤ c :=
  match Nat.le.dest h with
  | ⟨k, hk⟩ => Nat.le.intro (add_left_cancel' a _ _ (by rw [← Nat.add_assoc]; exact hk))

theorem lt_of_add_lt_add_left' (a b c : Nat) (h : a + b < a + c) : b < c :=
  le_of_add_le_add_left' a (b + 1) c h

theorem add_sub_of_le' : ∀ (a b : Nat), a ≤ b → a + (b - a) = b
  | 0, b, _ => by rw [nzero_add]; rfl
  | a + 1, 0, h => absurd h (Nat.not_succ_le_zero a)
  | a + 1, b + 1, h => by
    rw [Nat.succ_sub_succ, Nat.add_right_comm]
    exact congrArg Nat.succ (add_sub_of_le' a b (Nat.le_of_succ_le_succ h))

theorem lt_irrefl_of_le (a b : Nat) (h1 : a < b) (h2 : b ≤ a) : False :=
  Nat.lt_irrefl a (Nat.lt_of_lt_of_le h1 h2)

theorem eq_zero_of_add_eq_self (a b : Nat) (h : a + b = a) : b = 0 :=
  add_left_cancel' a b 0 h

theorem lt_of_le_of_ne' (a b : Nat) (h1 : a ≤ b) (h2 : a ≠ b) : a < b :=
  match Nat.eq_or_lt_of_le h1 with
  | Or.inl e => absurd e h2
  | Or.inr l => l

/-! ## Parity -/

def par : Nat → Bool
  | 0 => false
  | n + 1 => !par n

def bx : Bool → Bool → Bool
  | true, b => !b
  | false, b => b

theorem par_add (a : Nat) : ∀ b : Nat, par (a + b) = bx (par a) (par b)
  | 0 => by cases par a <;> rfl
  | b + 1 => by
    show (!par (a + b)) = bx (par a) (!par b)
    rw [par_add a b]
    cases par a <;> cases par b <;> rfl

theorem par_mul (a : Nat) : ∀ b : Nat, par (a * b) = (par a && par b)
  | 0 => by show false = (par a && false); cases par a <;> rfl
  | b + 1 => by
    show par (a * b + a) = (par a && !par b)
    rw [par_add, par_mul a b]
    cases par a <;> cases par b <;> rfl

theorem par_double (a : Nat) : par (a + a) = false := by
  rw [par_add]; cases par a <;> rfl

theorem par_split : ∀ n : Nat,
    (par n = false → ∃ k, n = k + k) ∧ (par n = true → ∃ k, n = k + k + 1)
  | 0 => ⟨fun _ => ⟨0, rfl⟩, fun h => Bool.noConfusion h⟩
  | n + 1 => by
    have ih := par_split n
    constructor
    · intro h
      have h' : par n = true := by
        revert h; show (!par n) = false → par n = true
        cases par n <;> intro h <;> first | rfl | exact Bool.noConfusion h
      match ih.2 h' with
      | ⟨k, hk⟩ => exact ⟨k + 1, by rw [hk, ← Nat.add_assoc, Nat.add_right_comm k 1 k]⟩
    · intro h
      have h' : par n = false := by
        revert h; show (!par n) = true → par n = false
        cases par n <;> intro h <;> first | rfl | exact Bool.noConfusion h
      match ih.1 h' with
      | ⟨k, hk⟩ => exact ⟨k, by rw [hk]⟩

theorem bx_self (a : Bool) : bx a a = false := by cases a <;> rfl
theorem bx_false (a : Bool) : bx a false = a := by cases a <;> rfl
theorem bx_comm (a b : Bool) : bx a b = bx b a := by cases a <;> cases b <;> rfl
theorem bx_assoc (a b c : Bool) : bx (bx a b) c = bx a (bx b c) := by
  cases a <;> cases b <;> cases c <;> rfl

/-! ## Finite sums and products over 1..n -/

def sum1 : Nat → (Nat → Nat) → Nat
  | 0, _ => 0
  | n + 1, f => sum1 n f + f (n + 1)

def prod1 : Nat → (Nat → Nat) → Nat
  | 0, _ => 1
  | n + 1, f => prod1 n f * f (n + 1)

theorem sum1_ext : ∀ (n : Nat) (f g : Nat → Nat), (∀ k, 0 < k → k ≤ n → f k = g k) →
    sum1 n f = sum1 n g
  | 0, _, _, _ => rfl
  | n + 1, f, g, h => by
    show sum1 n f + f (n + 1) = sum1 n g + g (n + 1)
    rw [sum1_ext n f g (fun k h0 hk => h k h0 (Nat.le_succ_of_le hk)),
      h (n + 1) (Nat.zero_lt_succ n) (Nat.le_refl _)]

theorem prod1_ext : ∀ (n : Nat) (f g : Nat → Nat), (∀ k, 0 < k → k ≤ n → f k = g k) →
    prod1 n f = prod1 n g
  | 0, _, _, _ => rfl
  | n + 1, f, g, h => by
    show prod1 n f * f (n + 1) = prod1 n g * g (n + 1)
    rw [prod1_ext n f g (fun k h0 hk => h k h0 (Nat.le_succ_of_le hk)),
      h (n + 1) (Nat.zero_lt_succ n) (Nat.le_refl _)]

theorem sum1_add : ∀ (n : Nat) (f g : Nat → Nat),
    sum1 n (fun k => f k + g k) = sum1 n f + sum1 n g
  | 0, _, _ => rfl
  | n + 1, f, g => by
    show sum1 n (fun k => f k + g k) + (f (n + 1) + g (n + 1))
      = (sum1 n f + f (n + 1)) + (sum1 n g + g (n + 1))
    rw [sum1_add n f g]
    exact mid4 _ _ _ _

theorem sum1_mul_left : ∀ (n : Nat) (f : Nat → Nat) (c : Nat),
    c * sum1 n f = sum1 n (fun k => c * f k)
  | 0, _, _ => rfl
  | n + 1, f, c => by
    show c * (sum1 n f + f (n + 1)) = sum1 n (fun k => c * f k) + c * f (n + 1)
    rw [nleft_distrib, sum1_mul_left n f c]

theorem sum1_const : ∀ (n c : Nat), sum1 n (fun _ => c) = n * c
  | 0, c => (nzero_mul c).symm
  | n + 1, c => by
    show sum1 n (fun _ => c) + c = (n + 1) * c
    rw [sum1_const n c, nsucc_mul]

theorem sum1_swap (f : Nat → Nat → Nat) (B : Nat) : ∀ A : Nat,
    sum1 A (fun x => sum1 B (fun y => f x y)) = sum1 B (fun y => sum1 A (fun x => f x y))
  | 0 => by
    show 0 = sum1 B (fun _ => 0)
    rw [sum1_const, Nat.mul_zero]
  | A + 1 => by
    show sum1 A (fun x => sum1 B (fun y => f x y)) + sum1 B (fun y => f (A + 1) y)
      = sum1 B (fun y => sum1 A (fun x => f x y) + f (A + 1) y)
    rw [sum1_swap f B A, sum1_add]

theorem prod1_mul : ∀ (n : Nat) (f g : Nat → Nat),
    prod1 n (fun k => f k * g k) = prod1 n f * prod1 n g
  | 0, _, _ => rfl
  | n + 1, f, g => by
    show prod1 n (fun k => f k * g k) * (f (n + 1) * g (n + 1))
      = (prod1 n f * f (n + 1)) * (prod1 n g * g (n + 1))
    rw [prod1_mul n f g]
    exact mmid4 _ _ _ _

theorem pow_add' (a m : Nat) : ∀ n : Nat, a ^ (m + n) = a ^ m * a ^ n
  | 0 => (Nat.mul_one _).symm
  | n + 1 => by
    show a ^ (m + n) * a = a ^ m * (a ^ n * a)
    rw [pow_add' a m n, nmul_assoc]

theorem pow_mul' (a m : Nat) : ∀ n : Nat, a ^ (m * n) = (a ^ m) ^ n
  | 0 => rfl
  | n + 1 => by
    show a ^ (m * n + m) = (a ^ m) ^ n * a ^ m
    rw [pow_add', pow_mul' a m n]

theorem prod1_const : ∀ (n c : Nat), prod1 n (fun _ => c) = c ^ n
  | 0, _ => rfl
  | n + 1, c => by
    show prod1 n (fun _ => c) * c = c ^ n * c
    rw [prod1_const n c]

theorem prod1_pow (c : Nat) : ∀ (n : Nat) (e : Nat → Nat),
    prod1 n (fun k => c ^ e k) = c ^ sum1 n e
  | 0, _ => rfl
  | n + 1, e => by
    show prod1 n (fun k => c ^ e k) * c ^ e (n + 1) = c ^ (sum1 n e + e (n + 1))
    rw [prod1_pow c n e, pow_add']

/-! ## Congruence modulo m' + 1 -/

theorem rem_of_lt (m' x : Nat) (h : x < m' + 1) : rem (m' + 1) x = x :=
  match Nat.le.dest h with
  | ⟨k, hk⟩ => by
    have hk' : x + (k + 1) = m' + 1 := by rw [Nat.add_comm k 1, ← Nat.add_assoc]; exact hk
    have e := rem_div m' 0 x k hk'
    rw [nzero_mul, nzero_add] at e
    exact e

theorem rem_lt' (m' n : Nat) : rem (m' + 1) n < m' + 1 :=
  match rem_lt m' n with
  | ⟨k, hk⟩ => by
    show rem (m' + 1) n + 1 ≤ m' + 1
    exact Nat.le.intro (by rw [Nat.add_assoc, Nat.add_comm 1 k]; exact hk)

theorem rem_zero_of_dv (m' n : Nat) (h : Dv (m' + 1) n) : rem (m' + 1) n = 0 :=
  match h with
  | ⟨q, hq⟩ => by
    rw [hq]
    exact rem_div m' q 0 m' (nzero_add (m' + 1))

theorem dv_of_rem_zero (m' n : Nat) (h : rem (m' + 1) n = 0) : Dv (m' + 1) n :=
  match rem_spec m' n with
  | ⟨q, r, _, hn, _, hr⟩ => by
    have r0 : r = 0 := hr.symm.trans h
    exact ⟨q, by rw [hn, r0]; rfl⟩

theorem rem_mul_left (m' a c : Nat) :
    rem (m' + 1) (a * c) = rem (m' + 1) (rem (m' + 1) a * c) :=
  match rem_spec m' a with
  | ⟨q, r, _, ha, _, hr⟩ => by
    rw [hr, ha, nright_distrib, nmul_assoc q (m' + 1) c, nmul_comm (m' + 1) c,
      ← nmul_assoc q c (m' + 1), rem_add_mul]

theorem rem_mul_congr (m' a b c : Nat) (h : rem (m' + 1) a = rem (m' + 1) b) :
    rem (m' + 1) (a * c) = rem (m' + 1) (b * c) := by
  rw [rem_mul_left m' a c, h, ← rem_mul_left m' b c]

theorem rem_mul_congr' (m' a b c : Nat) (h : rem (m' + 1) a = rem (m' + 1) b) :
    rem (m' + 1) (c * a) = rem (m' + 1) (c * b) := by
  rw [nmul_comm c a, nmul_comm c b]; exact rem_mul_congr m' a b c h

theorem rem_mul_congr2 (m' a b c d : Nat) (h1 : rem (m' + 1) a = rem (m' + 1) b)
    (h2 : rem (m' + 1) c = rem (m' + 1) d) : rem (m' + 1) (a * c) = rem (m' + 1) (b * d) :=
  Eq.trans (rem_mul_congr m' a b c h1) (rem_mul_congr' m' c d b h2)

theorem rem_add_congr' (m' a b c : Nat) (h : rem (m' + 1) a = rem (m' + 1) b) :
    rem (m' + 1) (c + a) = rem (m' + 1) (c + b) := by
  rw [Nat.add_comm c a, Nat.add_comm c b]; exact rem_add_congr m' a b c h

theorem rem_add_congr2 (m' a b c d : Nat) (h1 : rem (m' + 1) a = rem (m' + 1) b)
    (h2 : rem (m' + 1) c = rem (m' + 1) d) : rem (m' + 1) (a + c) = rem (m' + 1) (b + d) :=
  Eq.trans (rem_add_congr m' a b c h1) (rem_add_congr' m' c d b h2)

theorem rem_pow_congr (m' a b : Nat) (h : rem (m' + 1) a = rem (m' + 1) b) :
    ∀ n : Nat, rem (m' + 1) (a ^ n) = rem (m' + 1) (b ^ n)
  | 0 => rfl
  | n + 1 => rem_mul_congr2 m' _ _ _ _ (rem_pow_congr m' a b h n) h

theorem rem_cancel_add (m' a b c : Nat) (h : rem (m' + 1) (a + c) = rem (m' + 1) (b + c)) :
    rem (m' + 1) a = rem (m' + 1) b := by
  have h2 := rem_add_congr m' _ _ (m' * c) h
  have e : ∀ x, x + c + m' * c = c * (m' + 1) + x := fun x => by
    rw [Nat.add_assoc x c (m' * c), Nat.add_comm c (m' * c), ← nsucc_mul m' c,
      nmul_comm (m' + 1) c, Nat.add_comm x (c * (m' + 1))]
  rw [e a, e b, rem_add_mul, rem_add_mul] at h2
  exact h2

theorem dv_of_rem_add (m' u v : Nat) (h : rem (m' + 1) (u + v) = rem (m' + 1) u) :
    Dv (m' + 1) v := by
  apply dv_of_rem_zero
  have h2 : rem (m' + 1) (v + u) = rem (m' + 1) (0 + u) := by
    rw [Nat.add_comm v u, nzero_add]; exact h
  exact rem_cancel_add m' v 0 u h2

theorem rem_add_dv (m' u v : Nat) (h : Dv (m' + 1) v) : rem (m' + 1) (u + v) = rem (m' + 1) u :=
  match h with
  | ⟨q, hq⟩ => by rw [hq, Nat.add_comm u, rem_add_mul]

theorem rem_prod1_congr (m' : Nat) (f g : Nat → Nat) : ∀ n : Nat,
    (∀ k, 0 < k → k ≤ n → rem (m' + 1) (f k) = rem (m' + 1) (g k)) →
    rem (m' + 1) (prod1 n f) = rem (m' + 1) (prod1 n g)
  | 0, _ => rfl
  | n + 1, h =>
    rem_mul_congr2 m' _ _ _ _
      (rem_prod1_congr m' f g n (fun k h0 hk => h k h0 (Nat.le_succ_of_le hk)))
      (h (n + 1) (Nat.zero_lt_succ n) (Nat.le_refl _))

/-! ## Primes -/

theorem euclid (p a b : Nat) (hp : Pr p) (h : Dv p (a * b)) : Dv p a ∨ Dv p b :=
  match hp, a, h with
  | _, 0, _ => Or.inl ⟨0, (nzero_mul p).symm⟩
  | ⟨⟨j, hj⟩, hpr⟩, a + 1, h => by
    subst hj; exact euclid_core j a b hpr h

theorem eq_zero_of_dv_lt (d t : Nat) (h : Dv d t) (hl : t < d) : t = 0 :=
  match h with
  | ⟨0, hq⟩ => by rw [hq, nzero_mul]
  | ⟨q + 1, hq⟩ => by
    rw [hq, nsucc_mul] at hl
    exact absurd (Nat.lt_of_le_of_lt (Nat.le_add_left d (q * d)) hl) (Nat.lt_irrefl d)

theorem not_dv_of_lt (d k : Nat) (h0 : 0 < k) (hk : k < d) (h : Dv d k) : False := by
  have e := eq_zero_of_dv_lt d k h hk
  rw [e] at h0
  exact Nat.lt_irrefl 0 h0

theorem not_dv_prod1 (p : Nat) (hp : Pr p) (f : Nat → Nat) : ∀ n : Nat,
    (∀ k, 0 < k → k ≤ n → ¬ Dv p (f k)) → ¬ Dv p (prod1 n f)
  | 0, _, hd =>
    match hp with
    | ⟨⟨j, hj⟩, _⟩ => by subst hj; exact dv_one_absurd j hd
  | n + 1, h, hd =>
    match euclid p _ _ hp hd with
    | Or.inl h1 => not_dv_prod1 p hp f n (fun k h0 hk => h k h0 (Nat.le_succ_of_le hk)) h1
    | Or.inr h2 => h (n + 1) (Nat.zero_lt_succ n) (Nat.le_refl _) h2

theorem cancel_core (m' a u v : Nat) (hp : Pr (m' + 1)) (ha : ¬ Dv (m' + 1) a)
    (hv : v < m' + 1) (huv : u ≤ v)
    (h : rem (m' + 1) (a * u) = rem (m' + 1) (a * v)) : u = v :=
  match Nat.le.dest huv with
  | ⟨t, ht⟩ => by
    rw [← ht, nleft_distrib] at h
    have hd := dv_of_rem_add m' _ _ h.symm
    match euclid _ _ _ hp hd with
    | Or.inl hda => exact absurd hda ha
    | Or.inr hdt =>
      have htv : t ≤ v := Nat.le.intro (by rw [Nat.add_comm]; exact ht)
      have ht0 : t = 0 := eq_zero_of_dv_lt _ _ hdt (Nat.lt_of_le_of_lt htv hv)
      rw [← ht, ht0]; rfl

theorem rem_cancel_mul (m' a x y : Nat) (hp : Pr (m' + 1)) (ha : ¬ Dv (m' + 1) a)
    (h : rem (m' + 1) (a * x) = rem (m' + 1) (a * y)) : rem (m' + 1) x = rem (m' + 1) y := by
  have hx := rem_mul_congr' m' x (rem (m' + 1) x) a (rem_idem m' x).symm
  have hy := rem_mul_congr' m' y (rem (m' + 1) y) a (rem_idem m' y).symm
  have e1 := (hx.symm.trans h).trans hy
  match Nat.le_total (rem (m' + 1) x) (rem (m' + 1) y) with
  | Or.inl hl => exact cancel_core m' a _ _ hp ha (rem_lt' m' y) hl e1
  | Or.inr hl => exact (cancel_core m' a _ _ hp ha (rem_lt' m' x) hl e1.symm).symm

/-! ## Lists -/

def memL (v : Nat) : List Nat → Prop
  | [] => False
  | x :: xs => v = x ∨ memL v xs

def nodupL : List Nat → Prop
  | [] => True
  | x :: xs => ¬ memL x xs ∧ nodupL xs

def allL (P : Nat → Prop) : List Nat → Prop
  | [] => True
  | x :: xs => P x ∧ allL P xs

theorem allL_mono (P Q : Nat → Prop) (r : Nat) (hPQ : ∀ x, P x → x ≠ r → Q x) :
    ∀ L : List Nat, allL P L → ¬ memL r L → allL Q L
  | [], _, _ => True.intro
  | x :: xs, ⟨hx, hxs⟩, hm =>
    ⟨hPQ x hx (fun e => hm (Or.inl e.symm)),
      allL_mono P Q r hPQ xs hxs (fun h => hm (Or.inr h))⟩

/-! ## Polynomials over F_p have at most (degree) roots -/

/-- Evaluation of a coefficient list, leading coefficient first. -/
def ev (x : Nat) : List Nat → Nat
  | [] => 0
  | c :: cs => c * x ^ cs.length + ev x cs

/-- Synthetic division by `X - r`: the quotient's coefficients. -/
def qd (r : Nat) : Nat → List Nat → List Nat
  | _, [] => []
  | b, c :: cs => b :: qd r (b * r + c) cs

theorem qd_length (r : Nat) : ∀ (b : Nat) (cs : List Nat), (qd r b cs).length = cs.length
  | _, [] => rfl
  | _, _ :: cs => congrArg Nat.succ (qd_length r _ cs)

/-- The remainder theorem, without subtraction:
    `P(x) + r Q(x) = x Q(x) + P(r)`. -/
theorem qd_spec (x r : Nat) : ∀ (b : Nat) (cs : List Nat),
    ev x (b :: cs) + r * ev x (qd r b cs) = x * ev x (qd r b cs) + ev r (b :: cs)
  | b, [] => (nzero_add (b * 1)).symm
  | b, c :: cs => by
    have ih := qd_spec x r (b * r + c) cs
    have t1 : r * (b * x ^ cs.length) = b * r * x ^ cs.length := by
      rw [← nmul_assoc r b, nmul_comm r b]
    have t2 : x * (b * x ^ cs.length) = b * x ^ (cs.length + 1) := by
      rw [nmul_comm x, nmul_assoc]; rfl
    have t3 : b * r ^ (cs.length + 1) = b * r * r ^ cs.length := by
      rw [nmul_assoc]; show b * (r ^ cs.length * r) = _; rw [nmul_comm (r ^ cs.length) r]
    show b * x ^ (cs.length + 1) + (c * x ^ cs.length + ev x cs)
        + r * (b * x ^ (qd r (b * r + c) cs).length + ev x (qd r (b * r + c) cs))
      = x * (b * x ^ (qd r (b * r + c) cs).length + ev x (qd r (b * r + c) cs))
        + (b * r ^ (cs.length + 1) + (c * r ^ cs.length + ev r cs))
    rw [qd_length, nleft_distrib, nleft_distrib, t1, t2, t3]
    rw [Nat.add_assoc, Nat.add_assoc (b * x ^ (cs.length + 1))]
    apply congrArg (fun t => b * x ^ (cs.length + 1) + t)
    have hR : b * r * r ^ cs.length + (c * r ^ cs.length + ev r cs)
        = (b * r + c) * r ^ cs.length + ev r cs := by
      rw [← Nat.add_assoc, ← nright_distrib]
    have hL : (c * x ^ cs.length + ev x cs) + (b * r * x ^ cs.length + r * ev x (qd r (b * r + c) cs))
        = (b * r + c) * x ^ cs.length + ev x cs + r * ev x (qd r (b * r + c) cs) := by
      rw [nright_distrib, ← Nat.add_assoc (c * x ^ cs.length + ev x cs),
        Nat.add_right_comm (c * x ^ cs.length) (ev x cs), Nat.add_comm (c * x ^ cs.length)]
    rw [hR, hL]
    exact ih

/-- A root of `P` other than `r` is a root of the quotient by `X - r`. -/
theorem root_quot (m' : Nat) (hp : Pr (m' + 1)) (r s b : Nat) (cs : List Nat)
    (hr : r < m' + 1) (hs : s < m' + 1) (hrs : s ≠ r)
    (hPr : rem (m' + 1) (ev r (b :: cs)) = 0) (hPs : rem (m' + 1) (ev s (b :: cs)) = 0) :
    rem (m' + 1) (ev s (qd r b cs)) = 0 := by
  have hq := qd_spec s r b cs
  have h1 : rem (m' + 1) (0 + r * ev s (qd r b cs)) = rem (m' + 1) (0 + s * ev s (qd r b cs)) := by
    have a1 : rem (m' + 1) (ev s (b :: cs) + r * ev s (qd r b cs))
        = rem (m' + 1) (0 + r * ev s (qd r b cs)) := rem_add_congr m' _ 0 _ hPs
    have a2 : rem (m' + 1) (s * ev s (qd r b cs) + ev r (b :: cs))
        = rem (m' + 1) (s * ev s (qd r b cs) + 0) := rem_add_congr' m' _ 0 _ hPr
    rw [← a1, hq, a2, nzero_add]; rfl
  rw [nzero_add, nzero_add, nmul_comm r, nmul_comm s] at h1
  match dv_dec m' (ev s (qd r b cs)) with
  | Or.inl hd => exact rem_zero_of_dv m' _ hd
  | Or.inr hnd =>
    have e := rem_cancel_mul m' _ r s hp hnd h1
    rw [rem_of_lt m' r hr, rem_of_lt m' s hs] at e
    exact absurd e.symm hrs

theorem roots_le_n (m' : Nat) (hp : Pr (m' + 1)) : ∀ (n : Nat) (cs : List Nat) (c : Nat)
    (R : List Nat), cs.length = n → ¬ Dv (m' + 1) c → nodupL R →
    allL (fun x => x < m' + 1 ∧ rem (m' + 1) (ev x (c :: cs)) = 0) R →
    R.length ≤ n
  | n, _, _, [], _, _, _, _ => Nat.zero_le n
  | 0, [], c, x :: _, _, hc, _, ⟨⟨_, hx⟩, _⟩ => by
    have e : rem (m' + 1) c = 0 := by
      have hx' : rem (m' + 1) (c * 1 + 0) = 0 := hx
      rw [Nat.mul_one] at hx'; exact hx'
    exact absurd (dv_of_rem_zero m' c e) hc
  | 0, _ :: _, _, _ :: _, hl, _, _, _ => Nat.noConfusion hl
  | n + 1, [], _, _ :: _, hl, _, _, _ => Nat.noConfusion hl
  | n + 1, d :: ds, c, r :: R', hl, hc, ⟨hnm, hnd⟩, ⟨⟨hr, hPr⟩, hall⟩ => by
    have hds : (qd r (c * r + d) ds).length = n := by
      rw [qd_length]; exact succ_inj' hl
    have hall' : allL (fun x => x < m' + 1 ∧
        rem (m' + 1) (ev x (c :: qd r (c * r + d) ds)) = 0) R' :=
      allL_mono _ _ r
        (fun x ⟨hx, hPx⟩ hxr => ⟨hx, root_quot m' hp r x c (d :: ds) hr hx hxr hPr hPx⟩)
        R' hall hnm
    exact Nat.succ_le_succ (roots_le_n m' hp n _ c R' hds hc hnd hall')

/-- **Roots.** A polynomial over F_p whose leading coefficient is not `≡ 0`
    has at most (degree) distinct roots in `0..p-1`. -/
theorem roots_le (m' : Nat) (hp : Pr (m' + 1)) (c : Nat) (cs R : List Nat)
    (hc : ¬ Dv (m' + 1) c) (hR : nodupL R)
    (hroots : allL (fun x => x < m' + 1 ∧ rem (m' + 1) (ev x (c :: cs)) = 0) R) :
    R.length ≤ cs.length :=
  roots_le_n m' hp cs.length cs c R rfl hc hR hroots

/-! ## Euler's criterion.  Here p = h + h + 1. -/

theorem h_pos (h : Nat) (hp : Pr (h + h + 1)) : 0 < h :=
  match h, hp with
  | 0, ⟨⟨j, hj⟩, _⟩ => absurd (succ_inj' hj).symm (succ_ne_zero' j)
  | h + 1, _ => Nat.zero_lt_succ h

theorem rem_one (m' : Nat) (h0 : 0 < m') : rem (m' + 1) 1 = 1 :=
  rem_of_lt m' 1 (Nat.succ_le_succ h0)

theorem hh_pos (h : Nat) (hp : Pr (h + h + 1)) : 0 < h + h :=
  Nat.lt_of_lt_of_le (h_pos h hp) (Nat.le_add_right h h)

/-- Fermat in the form `y^(p-1) ≡ 1` for `p ∤ y`. -/
theorem fermat1 (h y : Nat) (hp : Pr (h + h + 1)) (hy : ¬ Dv (h + h + 1) y) :
    rem (h + h + 1) (y ^ (h + h)) = 1 := by
  have f := fermat (h + h + 1) hp y
  have f' : rem (h + h + 1) (y * y ^ (h + h)) = rem (h + h + 1) (y * 1) := by
    rw [nmul_comm, Nat.mul_one]; exact f
  rw [rem_cancel_mul (h + h) y _ 1 hp hy f']
  exact rem_one (h + h) (hh_pos h hp)

theorem sq_pow (x : Nat) : ∀ n : Nat, (x * x) ^ n = x ^ (n + n)
  | 0 => rfl
  | n + 1 => by
    show (x * x) ^ n * (x * x) = x ^ (n + 1 + (n + 1))
    rw [sq_pow x n, ← Nat.add_assoc (n + 1) n 1, Nat.add_right_comm n 1 n]
    exact (nmul_assoc _ x x).symm

/-- `x² ≡ 1` forces `x ≡ 1` or `x ≡ -1`. -/
theorem sq_one (m' : Nat) (hp : Pr (m' + 1)) (h0 : 0 < m') (x : Nat)
    (hx : rem (m' + 1) (x * x) = 1) : rem (m' + 1) x = 1 ∨ rem (m' + 1) x = m' := by
  have hrr : rem (m' + 1) (rem (m' + 1) x * rem (m' + 1) x) = 1 :=
    (rem_mul_congr2 m' _ _ _ _ (rem_idem m' x).symm (rem_idem m' x).symm).symm.trans hx
  have hlt := rem_lt' m' x
  revert hrr hlt
  cases rem (m' + 1) x with
  | zero =>
    intro hrr _
    have e0 : (0 : Nat) = 1 := hrr
    exact absurd e0 (fun e => Nat.noConfusion e)
  | succ t =>
    intro hrr hlt
    have e : (t + 1) * (t + 1) = t * (t + 1 + 1) + 1 := by rw [nsucc_mul]; rfl
    rw [e] at hrr
    have h2 : rem (m' + 1) (t * (t + 1 + 1) + 1) = rem (m' + 1) (0 + 1) := by
      rw [hrr, nzero_add]; exact (rem_one m' h0).symm
    have hd := dv_of_rem_zero m' _ (rem_cancel_add m' _ 0 1 h2)
    match euclid _ _ _ hp hd with
    | Or.inl ht =>
      have t0 : t = 0 := eq_zero_of_dv_lt _ _ ht (Nat.lt_trans (Nat.lt_succ_self t) hlt)
      rw [t0]; exact Or.inl rfl
    | Or.inr ht2 =>
      have hle : t + 1 + 1 ≤ m' + 1 := hlt
      match Nat.eq_or_lt_of_le hle with
      | Or.inl e2 => exact Or.inr (succ_inj' e2)
      | Or.inr l2 => exact absurd (eq_zero_of_dv_lt _ _ ht2 l2) (fun e => Nat.noConfusion e)

/-- **Euler, part 1.** For `p ∤ a`: `a^h ≡ 1` or `a^h ≡ p - 1`. -/
theorem euler_pm (h a : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a) :
    rem (h + h + 1) (a ^ h) = 1 ∨ rem (h + h + 1) (a ^ h) = (h + h + 1) - 1 := by
  have e : rem (h + h + 1) (a ^ h * a ^ h) = 1 := by
    rw [← pow_add']; exact fermat1 h a hp ha
  exact sq_one (h + h) hp (hh_pos h hp) _ e

/-- A square (of a unit) satisfies `a^h ≡ 1`. -/
theorem euler_of_sq (h a x : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a)
    (hx : rem (h + h + 1) (x * x) = rem (h + h + 1) a) : rem (h + h + 1) (a ^ h) = 1 := by
  have hnx : ¬ Dv (h + h + 1) x := fun hd =>
    ha (dv_of_rem_zero _ _ (hx.symm.trans (rem_zero_of_dv _ _ (dv_mul_right _ _ x hd))))
  rw [← rem_pow_congr (h + h) _ _ hx h, sq_pow]
  exact fermat1 h x hp hnx

def tailL : Nat → Nat → List Nat
  | 0, c => [c]
  | n + 1, c => 0 :: tailL n c

theorem tailL_length (c : Nat) : ∀ n : Nat, (tailL n c).length = n + 1
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (tailL_length c n)

theorem ev_tailL (x c : Nat) : ∀ n : Nat, ev x (tailL n c) = c
  | 0 => Nat.mul_one c
  | n + 1 => by
    show 0 * x ^ (tailL n c).length + ev x (tailL n c) = c
    rw [nzero_mul, nzero_add, ev_tailL x c n]

def sqL (m : Nat) : Nat → List Nat
  | 0 => []
  | n + 1 => rem m ((n + 1) * (n + 1)) :: sqL m n

theorem sqL_length (m : Nat) : ∀ n : Nat, (sqL m n).length = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (sqL_length m n)

theorem sqL_mem (m v : Nat) : ∀ n : Nat, memL v (sqL m n) →
    ∃ y, 0 < y ∧ y ≤ n ∧ v = rem m (y * y)
  | 0, hm => False.elim hm
  | n + 1, Or.inl e => ⟨n + 1, Nat.zero_lt_succ n, Nat.le_refl _, e⟩
  | n + 1, Or.inr hm =>
    match sqL_mem m v n hm with
    | ⟨y, h0, hy, e⟩ => ⟨y, h0, Nat.le_succ_of_le hy, e⟩

theorem sq_expand (y t : Nat) : (y + t) * (y + t) = y * y + t * (y + (y + t)) := by
  rw [nleft_distrib (y + t) y t, nright_distrib y t y, nright_distrib y t t,
    nleft_distrib t y (y + t), nleft_distrib t y t, nmul_comm y t, Nat.add_assoc (y * y)]

theorem lt_p_of_le_h (h z : Nat) (hz : z ≤ h) : z < h + h + 1 :=
  Nat.lt_succ_of_le (Nat.le_trans hz (Nat.le_add_right h h))

/-- The squares of `1, ..., h` are distinct mod p. -/
theorem sq_distinct (h : Nat) (hp : Pr (h + h + 1)) (y z : Nat) (h0 : 0 < y) (hyz : y < z)
    (hz : z ≤ h) : rem (h + h + 1) (y * y) ≠ rem (h + h + 1) (z * z) := fun he =>
  match Nat.le.dest hyz with
  | ⟨k, hk⟩ => by
    have hz' : z = y + (k + 1) := hk.symm.trans (Nat.succ_add y k)
    rw [hz', sq_expand] at he
    have hd := dv_of_rem_add (h + h) _ _ he.symm
    match euclid _ _ _ hp hd with
    | Or.inl ht =>
      have hkz : k + 1 ≤ z := Nat.le.intro (by rw [Nat.add_comm, ← hz'])
      exact not_dv_of_lt _ (k + 1) (Nat.zero_lt_succ k)
        (lt_p_of_le_h h _ (Nat.le_trans hkz hz)) ht
    | Or.inr hs =>
      have hs' : y + (y + (k + 1)) ≤ h + h :=
        Nat.add_le_add (Nat.le_trans (Nat.le_of_lt hyz) hz) (by rw [← hz']; exact hz)
      exact not_dv_of_lt _ _ (Nat.lt_of_lt_of_le h0 (Nat.le_add_right y _))
        (Nat.lt_succ_of_le hs') hs

theorem sqL_nodup (h : Nat) (hp : Pr (h + h + 1)) : ∀ n : Nat, n ≤ h →
    nodupL (sqL (h + h + 1) n)
  | 0, _ => True.intro
  | n + 1, hn =>
    ⟨fun hm =>
      match sqL_mem _ _ n hm with
      | ⟨y, h0, hy, e⟩ =>
        sq_distinct h hp y (n + 1) h0 (Nat.lt_succ_of_le hy) hn e.symm,
      sqL_nodup h hp n (Nat.le_trans (Nat.le_succ n) hn)⟩

/-- `X^h - 1`, written `X^h + (p - 1)`; its roots are the `v` with `v^h ≡ 1`. -/
theorem root_of_pow (h' v : Nat) (hv : rem (h' + 1 + (h' + 1) + 1) (v ^ (h' + 1)) = 1) :
    rem (h' + 1 + (h' + 1) + 1) (ev v (1 :: tailL h' (h' + 1 + (h' + 1)))) = 0 := by
  show rem _ (1 * v ^ (tailL h' (h' + 1 + (h' + 1))).length + ev v (tailL h' _)) = 0
  rw [tailL_length, ev_tailL, Nat.one_mul]
  have e1 := rem_add_congr (h' + 1 + (h' + 1)) _ 1 (h' + 1 + (h' + 1))
    (hv.trans (rem_one _ (Nat.zero_lt_succ _)).symm)
  rw [e1, Nat.add_comm 1]
  exact rem_zero_of_dv _ _ (dv_refl _)

theorem sq_search (m a : Nat) : ∀ n : Nat,
    (∃ y, rem m (y * y) = rem m a) ∨ (∀ y, y ≤ n → rem m (y * y) ≠ rem m a)
  | 0 =>
    match Nat.decEq (rem m (0 * 0)) (rem m a) with
    | isTrue e => Or.inl ⟨0, e⟩
    | isFalse ne => Or.inr (fun y hy => by
        have y0 : y = 0 := Nat.le_antisymm hy (Nat.zero_le y)
        rw [y0]; exact ne)
  | n + 1 =>
    match sq_search m a n with
    | Or.inl ex => Or.inl ex
    | Or.inr hall =>
      match Nat.decEq (rem m ((n + 1) * (n + 1))) (rem m a) with
      | isTrue e => Or.inl ⟨n + 1, e⟩
      | isFalse ne => Or.inr (fun y hy =>
          match Nat.eq_or_lt_of_le hy with
          | Or.inl e => by rw [e]; exact ne
          | Or.inr l => hall y (Nat.le_of_lt_succ l))

/-- **Euler, part 2.** For `p ∤ a`: `a^h ≡ 1` exactly when `a` is a square mod p. -/
theorem euler_iff (h a : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a) :
    rem (h + h + 1) (a ^ h) = 1 ↔ ∃ x, rem (h + h + 1) (x * x) = rem (h + h + 1) a := by
  constructor
  · intro hah
    match sq_search (h + h + 1) a h with
    | Or.inl ex => exact ex
    | Or.inr hall =>
      exfalso
      match h, hp, ha, hah, hall with
      | 0, hp, _, _, _ => exact Nat.lt_irrefl 0 (h_pos 0 hp)
      | h' + 1, hp, ha, hah, hall =>
        have hnm : ¬ memL (rem (h' + 1 + (h' + 1) + 1) a) (sqL (h' + 1 + (h' + 1) + 1) (h' + 1)) :=
          fun hm =>
            match sqL_mem _ _ _ hm with
            | ⟨y, _, hy, e⟩ => hall y hy e.symm
        have hroots : allL (fun x => x < h' + 1 + (h' + 1) + 1 ∧
            rem (h' + 1 + (h' + 1) + 1) (ev x (1 :: tailL h' (h' + 1 + (h' + 1)))) = 0)
            (rem (h' + 1 + (h' + 1) + 1) a :: sqL (h' + 1 + (h' + 1) + 1) (h' + 1)) := by
          refine ⟨⟨rem_lt' _ a, root_of_pow h' _ ?_⟩, ?_⟩
          · rw [rem_pow_congr _ _ _ (rem_idem _ a)]; exact hah
          · have gen : ∀ n, n ≤ h' + 1 → allL (fun x => x < h' + 1 + (h' + 1) + 1 ∧
                rem (h' + 1 + (h' + 1) + 1) (ev x (1 :: tailL h' (h' + 1 + (h' + 1)))) = 0)
                (sqL (h' + 1 + (h' + 1) + 1) n) := by
              intro n
              induction n with
              | zero => intro _; exact True.intro
              | succ n ih =>
                intro hn
                refine ⟨⟨rem_lt' _ _, root_of_pow h' _ ?_⟩, ih (Nat.le_trans (Nat.le_succ n) hn)⟩
                rw [rem_pow_congr _ _ _ (rem_idem _ _), sq_pow]
                apply fermat1 (h' + 1) (n + 1) hp
                exact not_dv_of_lt _ _ (Nat.zero_lt_succ n) (lt_p_of_le_h _ _ hn)
            exact gen (h' + 1) (Nat.le_refl _)
        have hle := roots_le (h' + 1 + (h' + 1)) hp 1 (tailL h' (h' + 1 + (h' + 1)))
          (rem (h' + 1 + (h' + 1) + 1) a :: sqL (h' + 1 + (h' + 1) + 1) (h' + 1)) (dv_one_absurd _)
          ⟨hnm, sqL_nodup (h' + 1) hp (h' + 1) (Nat.le_refl _)⟩ hroots
        rw [tailL_length] at hle
        show False
        have hle' : h' + 1 + 1 ≤ h' + 1 := by
          have l := hle; revert l
          show (sqL _ (h' + 1)).length + 1 ≤ h' + 1 → h' + 1 + 1 ≤ h' + 1
          rw [sqL_length]; exact id
        exact Nat.not_succ_le_self _ hle'
  · intro ⟨x, hx⟩
    exact euler_of_sq h a x hp ha hx

/-! ## A list of distinct numbers in 1..n of length n: product n!, sum 1+...+n -/

def prodL : List Nat → Nat
  | [] => 1
  | x :: xs => x * prodL xs

def sumL : List Nat → Nat
  | [] => 0
  | x :: xs => x + sumL xs

def eraseL (w : Nat) : List Nat → List Nat
  | [] => []
  | x :: xs => if x = w then xs else x :: eraseL w xs

theorem memL_dec (v : Nat) : ∀ L : List Nat, memL v L ∨ ¬ memL v L
  | [] => Or.inr id
  | x :: xs =>
    match Nat.decEq v x with
    | isTrue e => Or.inl (Or.inl e)
    | isFalse ne =>
      match memL_dec v xs with
      | Or.inl h => Or.inl (Or.inr h)
      | Or.inr h => Or.inr (fun hm =>
          match hm with
          | Or.inl e => ne e
          | Or.inr h' => h h')

theorem eraseL_eq (w x : Nat) (xs : List Nat) (e : x = w) : eraseL w (x :: xs) = xs :=
  ite_eq_left e

theorem eraseL_ne (w x : Nat) (xs : List Nat) (ne : ¬ x = w) :
    eraseL w (x :: xs) = x :: eraseL w xs :=
  ite_eq_right ne

theorem erase_mem (w v : Nat) : ∀ L : List Nat, memL v (eraseL w L) → memL v L
  | [], h => h
  | x :: xs, h =>
    match Nat.decEq x w with
    | isTrue e => by rw [eraseL_eq w x xs e] at h; exact Or.inr h
    | isFalse ne => by
      rw [eraseL_ne w x xs ne] at h
      match h with
      | Or.inl e => exact Or.inl e
      | Or.inr h' => exact Or.inr (erase_mem w v xs h')

theorem erase_ne (w v : Nat) : ∀ L : List Nat, nodupL L → memL v (eraseL w L) → v ≠ w
  | [], _, h => False.elim h
  | x :: xs, ⟨hnm, hnd⟩, h =>
    match Nat.decEq x w with
    | isTrue e => by
      rw [eraseL_eq w x xs e] at h
      intro hv
      rw [hv, ← e] at h
      exact hnm h
    | isFalse ne => by
      rw [eraseL_ne w x xs ne] at h
      match h with
      | Or.inl e => rw [e]; exact ne
      | Or.inr h' => exact erase_ne w v xs hnd h'

theorem erase_nodup (w : Nat) : ∀ L : List Nat, nodupL L → nodupL (eraseL w L)
  | [], _ => True.intro
  | x :: xs, ⟨hnm, hnd⟩ =>
    match Nat.decEq x w with
    | isTrue e => by rw [eraseL_eq w x xs e]; exact hnd
    | isFalse ne => by
      rw [eraseL_ne w x xs ne]
      exact ⟨fun hm => hnm (erase_mem w x xs hm), erase_nodup w xs hnd⟩

theorem erase_count (w : Nat) : ∀ L : List Nat, memL w L →
    (eraseL w L).length + 1 = L.length ∧ prodL L = w * prodL (eraseL w L) ∧
      sumL L = w + sumL (eraseL w L)
  | [], h => False.elim h
  | x :: xs, hm =>
    match Nat.decEq x w with
    | isTrue e => by
      rw [eraseL_eq w x xs e]
      exact ⟨rfl, by rw [← e]; rfl, by rw [← e]; rfl⟩
    | isFalse ne => by
      rw [eraseL_ne w x xs ne]
      match hm with
      | Or.inl e => exact absurd e.symm ne
      | Or.inr h' =>
        have ⟨h1, h2, h3⟩ := erase_count w xs h'
        refine ⟨congrArg Nat.succ h1, ?_, ?_⟩
        · show x * prodL xs = w * (x * prodL (eraseL w xs))
          rw [h2]; exact mul_left_comm' _ _ _
        · show x + sumL xs = w + (x + sumL (eraseL w xs))
          rw [h3]; exact Nat.add_left_comm _ _ _

theorem erase_not_mem (w : Nat) : ∀ L : List Nat, ¬ memL w L → eraseL w L = L
  | [], _ => rfl
  | x :: xs, hn =>
    match Nat.decEq x w with
    | isTrue e => absurd (Or.inl e.symm) hn
    | isFalse ne => by
      rw [eraseL_ne w x xs ne, erase_not_mem w xs (fun h => hn (Or.inr h))]

theorem perm_count : ∀ (n : Nat) (L : List Nat), nodupL L →
    (∀ v, memL v L → 0 < v ∧ v ≤ n) →
    L.length ≤ n ∧
      (L.length = n → prodL L = prod1 n (fun k => k) ∧ sumL L = sum1 n (fun k => k))
  | 0, [], _, _ => ⟨Nat.le_refl 0, fun _ => ⟨rfl, rfl⟩⟩
  | 0, x :: _, _, hb =>
    match hb x (Or.inl rfl) with
    | ⟨h0, hx⟩ => absurd (Nat.lt_of_lt_of_le h0 hx) (Nat.lt_irrefl 0)
  | n + 1, L, hnd, hb => by
    have hb' : ∀ v, memL v (eraseL (n + 1) L) → 0 < v ∧ v ≤ n := fun v hv =>
      match hb v (erase_mem _ _ L hv) with
      | ⟨h0, hle⟩ =>
        ⟨h0, Nat.le_of_lt_succ (lt_of_le_of_ne' v (n + 1) hle (erase_ne (n + 1) v L hnd hv))⟩
    have ih := perm_count n (eraseL (n + 1) L) (erase_nodup (n + 1) L hnd) hb'
    match memL_dec (n + 1) L with
    | Or.inl hm =>
      have ⟨hlen, hprod, hsum⟩ := erase_count (n + 1) L hm
      refine ⟨?_, fun hl => ?_⟩
      · rw [← hlen]; exact Nat.succ_le_succ ih.1
      · have hl' : (eraseL (n + 1) L).length = n := succ_inj' (hlen.trans hl)
        have ⟨p1, s1⟩ := ih.2 hl'
        refine ⟨?_, ?_⟩
        · rw [hprod, p1]; exact nmul_comm _ _
        · rw [hsum, s1]; exact Nat.add_comm _ _
    | Or.inr hn =>
      rw [erase_not_mem (n + 1) L hn] at ih
      refine ⟨Nat.le_succ_of_le ih.1, fun hl => ?_⟩
      have h1 := ih.1
      rw [hl] at h1
      exact absurd h1 (Nat.not_succ_le_self n)

def mkL (f : Nat → Nat) : Nat → List Nat
  | 0 => []
  | n + 1 => f (n + 1) :: mkL f n

theorem mkL_length (f : Nat → Nat) : ∀ n : Nat, (mkL f n).length = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (mkL_length f n)

theorem prodL_mkL (f : Nat → Nat) : ∀ n : Nat, prodL (mkL f n) = prod1 n f
  | 0 => rfl
  | n + 1 => by
    show f (n + 1) * prodL (mkL f n) = prod1 n f * f (n + 1)
    rw [prodL_mkL f n]; exact nmul_comm _ _

theorem sumL_mkL (f : Nat → Nat) : ∀ n : Nat, sumL (mkL f n) = sum1 n f
  | 0 => rfl
  | n + 1 => by
    show f (n + 1) + sumL (mkL f n) = sum1 n f + f (n + 1)
    rw [sumL_mkL f n]; exact Nat.add_comm _ _

theorem mkL_mem (f : Nat → Nat) (v : Nat) : ∀ n : Nat, memL v (mkL f n) →
    ∃ k, 0 < k ∧ k ≤ n ∧ v = f k
  | 0, h => False.elim h
  | n + 1, Or.inl e => ⟨n + 1, Nat.zero_lt_succ n, Nat.le_refl _, e⟩
  | n + 1, Or.inr h =>
    match mkL_mem f v n h with
    | ⟨k, h0, hk, e⟩ => ⟨k, h0, Nat.le_succ_of_le hk, e⟩

theorem mkL_nodup (f : Nat → Nat) : ∀ n : Nat,
    (∀ i j, 0 < i → i < j → j ≤ n → f i ≠ f j) → nodupL (mkL f n)
  | 0, _ => True.intro
  | n + 1, hinj =>
    ⟨fun hm =>
      match mkL_mem f _ n hm with
      | ⟨k, h0, hk, e⟩ => hinj k (n + 1) h0 (Nat.lt_succ_of_le hk) (Nat.le_refl _) e.symm,
      mkL_nodup f n (fun i j h0 hij hj => hinj i j h0 hij (Nat.le_succ_of_le hj))⟩

/-- A map of `1..n` into `1..n` without repeats has the same product and sum
    as the identity. -/
theorem perm_fun (f : Nat → Nat) (n : Nat) (hb : ∀ k, 0 < k → k ≤ n → 0 < f k ∧ f k ≤ n)
    (hinj : ∀ i j, 0 < i → i < j → j ≤ n → f i ≠ f j) :
    prod1 n f = prod1 n (fun k => k) ∧ sum1 n f = sum1 n (fun k => k) := by
  have hpc := perm_count n (mkL f n) (mkL_nodup f n hinj)
    (fun v hv => match mkL_mem f v n hv with
      | ⟨k, h0, hk, e⟩ => by rw [e]; exact hb k h0 hk)
  have ⟨p1, s1⟩ := hpc.2 (mkL_length f n)
  rw [prodL_mkL] at p1
  rw [sumL_mkL] at s1
  exact ⟨p1, s1⟩

/-! ## Floor division -/

/-- Quotient and remainder by `m' + 1`, counting up from 0. -/
def dm (m' : Nat) : Nat → Nat × Nat
  | 0 => (0, 0)
  | n + 1 =>
    if (dm m' n).2 = m' then ((dm m' n).1 + 1, 0) else ((dm m' n).1, (dm m' n).2 + 1)

/-- `fl m' n = ⌊n / (m' + 1)⌋`. -/
def fl (m' n : Nat) : Nat := (dm m' n).1

theorem dm_spec (m' : Nat) : ∀ n : Nat,
    n = (dm m' n).1 * (m' + 1) + (dm m' n).2 ∧ (dm m' n).2 ≤ m'
  | 0 => ⟨by show 0 = 0 * (m' + 1) + 0; rw [nzero_mul], Nat.zero_le _⟩
  | n + 1 => by
    have ⟨ih1, ih2⟩ := dm_spec m' n
    match Nat.decEq (dm m' n).2 m' with
    | isTrue e =>
      have hd : dm m' (n + 1) = ((dm m' n).1 + 1, 0) := ite_eq_left e
      rw [hd]
      refine ⟨?_, Nat.zero_le _⟩
      show n + 1 = ((dm m' n).1 + 1) * (m' + 1) + 0
      rw [nsucc_mul]
      exact congrArg Nat.succ (ih1.trans (congrArg (fun t => (dm m' n).1 * (m' + 1) + t) e))
    | isFalse ne =>
      have hd : dm m' (n + 1) = ((dm m' n).1, (dm m' n).2 + 1) := ite_eq_right ne
      rw [hd]
      exact ⟨congrArg Nat.succ ih1, lt_of_le_of_ne' _ _ ih2 ne⟩

theorem fl_spec (m' n : Nat) : n = fl m' n * (m' + 1) + rem (m' + 1) n := by
  have ⟨h1, h2⟩ := dm_spec m' n
  have ⟨k, hk⟩ := Nat.le.dest h2
  have hr : rem (m' + 1) n = (dm m' n).2 := by
    have hk' : (dm m' n).2 + (k + 1) = m' + 1 := congrArg Nat.succ hk
    rw [h1]
    rw [rem_div m' _ _ k hk']
    rw [← h1]
  rw [hr]
  exact h1

theorem fl_values : fl 6 23 = 3 ∧ fl 4 17 = 3 ∧ fl 2 2 = 0 := ⟨rfl, rfl, rfl⟩

/-! ## Gauss's lemma.  p = h + h + 1, p ∤ a. -/

/-- `a k` reduced mod p. -/
def rk (h a k : Nat) : Nat := rem (h + h + 1) (a * k)
/-- the reduced value folded into `1..h`. -/
def sk (h a k : Nat) : Nat := if rk h a k ≤ h then rk h a k else (h + h + 1) - rk h a k
/-- 1 when `a k` reduces above `p/2`, else 0. -/
def lk (h a k : Nat) : Nat := if rk h a k ≤ h then 0 else 1
/-- μ: how many of `a, 2a, ..., h a` reduce above `p/2`. -/
def mu (h a : Nat) : Nat := sum1 h (lk h a)

theorem rk_cases (h a k : Nat) :
    (rk h a k ≤ h ∧ sk h a k = rk h a k ∧ lk h a k = 0) ∨
    (h < rk h a k ∧ rk h a k + sk h a k = h + h + 1 ∧ lk h a k = 1) :=
  match Nat.lt_or_ge h (rk h a k) with
  | Or.inl hl =>
    have hn : ¬ rk h a k ≤ h := Nat.not_le_of_gt hl
    Or.inr ⟨hl,
      by
        show rk h a k + (if rk h a k ≤ h then rk h a k else (h + h + 1) - rk h a k) = _
        rw [ite_eq_right hn]
        exact add_sub_of_le' _ _ (Nat.le_of_lt (rem_lt' (h + h) (a * k))),
      ite_eq_right hn⟩
  | Or.inr hg => Or.inl ⟨hg, ite_eq_left hg, ite_eq_left hg⟩

theorem rk_pos (h a k : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a)
    (h0 : 0 < k) (hk : k ≤ h) : 0 < rk h a k := by
  apply Nat.pos_of_ne_zero
  intro e
  match euclid _ _ _ hp (dv_of_rem_zero (h + h) _ e) with
  | Or.inl hd => exact ha hd
  | Or.inr hd => exact not_dv_of_lt _ _ h0 (lt_p_of_le_h h k hk) hd

theorem sk_bounds (h a k : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a)
    (h0 : 0 < k) (hk : k ≤ h) : 0 < sk h a k ∧ sk h a k ≤ h :=
  match rk_cases h a k with
  | Or.inl ⟨hle, hs, _⟩ => by rw [hs]; exact ⟨rk_pos h a k hp ha h0 hk, hle⟩
  | Or.inr ⟨hl, hsum, _⟩ => by
    constructor
    · apply Nat.pos_of_ne_zero
      intro e
      rw [e] at hsum
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (rem_lt' (h + h) (a * k)) (Nat.le_of_eq hsum.symm))
    · apply le_of_add_le_add_left' (h + 1)
      rw [← Nat.add_right_comm h h 1, ← hsum]
      exact Nat.add_le_add_right hl _

theorem rk_sign (h a k : Nat) :
    rem (h + h + 1) (a * k) = rem (h + h + 1) ((h + h) ^ lk h a k * sk h a k) :=
  match rk_cases h a k with
  | Or.inl ⟨_, hs, hl⟩ => by
    rw [hl, hs, Nat.pow_zero, Nat.one_mul]
    exact (rem_idem (h + h) (a * k)).symm
  | Or.inr ⟨_, hsum, hl⟩ => by
    rw [hl, show (h + h) ^ 1 = h + h from Nat.one_mul (h + h)]
    apply Eq.trans (rem_idem (h + h) (a * k)).symm
    apply rem_cancel_add (h + h) _ _ (sk h a k)
    show rem (h + h + 1) (rk h a k + sk h a k) = _
    rw [hsum, ← nsucc_mul, nmul_comm]
    rw [rem_zero_of_dv _ _ (dv_refl _), rem_zero_of_dv _ _ ⟨sk h a k, rfl⟩]

theorem rk_no_eq (h a i j : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a)
    (hi : i < j) (hj : j ≤ h) (e : rk h a i = rk h a j) : False := by
  have e2 := rem_cancel_mul (h + h) a i j hp ha e
  rw [rem_of_lt _ _ (lt_p_of_le_h h i (Nat.le_trans (Nat.le_of_lt hi) hj)),
    rem_of_lt _ _ (lt_p_of_le_h h j hj)] at e2
  rw [e2] at hi
  exact Nat.lt_irrefl j hi

theorem rk_no_sum (h a i j : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a)
    (h0 : 0 < i) (hi : i ≤ h) (hj : j ≤ h) (e : rk h a i + rk h a j = h + h + 1) : False := by
  have e2 : rem (h + h + 1) (a * i + a * j) = 0 := by
    rw [rem_add_congr2 (h + h) _ (rk h a i) _ (rk h a j) (rem_idem _ _).symm (rem_idem _ _).symm,
      e]
    exact rem_zero_of_dv _ _ (dv_refl _)
  rw [← nleft_distrib] at e2
  match euclid _ _ _ hp (dv_of_rem_zero _ _ e2) with
  | Or.inl hd => exact ha hd
  | Or.inr hd =>
    exact not_dv_of_lt _ _ (Nat.lt_of_lt_of_le h0 (Nat.le_add_right i j))
      (Nat.lt_succ_of_le (Nat.add_le_add hi hj)) hd

theorem sk_inj (h a i j : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a)
    (h0 : 0 < i) (hij : i < j) (hj : j ≤ h) : sk h a i ≠ sk h a j := fun e =>
  have hi : i ≤ h := Nat.le_trans (Nat.le_of_lt hij) hj
  have h0j : 0 < j := Nat.lt_trans h0 hij
  match rk_cases h a i, rk_cases h a j with
  | Or.inl ⟨_, hsi, _⟩, Or.inl ⟨_, hsj, _⟩ =>
    rk_no_eq h a i j hp ha hij hj (hsi.symm.trans (e.trans hsj))
  | Or.inr ⟨_, hsi, _⟩, Or.inr ⟨_, hsj, _⟩ =>
    rk_no_eq h a i j hp ha hij hj
      (add_right_cancel' (sk h a i) _ _ (hsi.trans (by rw [e]; exact hsj.symm)))
  | Or.inl ⟨_, hsi, _⟩, Or.inr ⟨_, hsj, _⟩ =>
    rk_no_sum h a j i hp ha h0j hj hi
      (by rw [← hsi, e]; exact hsj)
  | Or.inr ⟨_, hsi, _⟩, Or.inl ⟨_, hsj, _⟩ =>
    rk_no_sum h a i j hp ha h0 hi hj
      (by rw [← hsj, ← e]; exact hsi)

theorem neg_sq (m'' : Nat) : rem (m'' + 1 + 1) ((m'' + 1) * (m'' + 1)) = 1 := by
  have e : (m'' + 1) * (m'' + 1) = m'' * (m'' + 1 + 1) + 1 := by rw [nsucc_mul]; rfl
  rw [e, rem_add_mul (m'' + 1) 1 m'']
  exact rem_one (m'' + 1) (Nat.zero_lt_succ m'')

theorem neg_sq' (h : Nat) (hp : Pr (h + h + 1)) : rem (h + h + 1) ((h + h) * (h + h)) = 1 :=
  match h, hp with
  | 0, hp => absurd (h_pos 0 hp) (Nat.lt_irrefl 0)
  | h' + 1, _ => neg_sq (h' + 1 + h')

theorem neg_pow_even (h n : Nat) (hp : Pr (h + h + 1)) : rem (h + h + 1) ((h + h) ^ (n + n)) = 1 := by
  rw [← sq_pow, rem_pow_congr (h + h) _ 1 (by rw [neg_sq' h hp]; exact (rem_one _ (hh_pos h hp)).symm),
    Nat.one_pow]
  exact rem_one _ (hh_pos h hp)

theorem neg_pow_odd (h n : Nat) (hp : Pr (h + h + 1)) :
    rem (h + h + 1) ((h + h) ^ (n + n + 1)) = h + h := by
  show rem (h + h + 1) ((h + h) ^ (n + n) * (h + h)) = h + h
  rw [rem_mul_congr (h + h) _ 1 _ (by rw [neg_pow_even h n hp]; exact (rem_one _ (hh_pos h hp)).symm),
    Nat.one_mul]
  exact rem_of_lt _ _ (Nat.lt_succ_self _)

/-- `a^h ≡ (-1)^μ`, with `-1` written `p - 1 = h + h`. -/
theorem gauss_pow (h a : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a) :
    rem (h + h + 1) (a ^ h) = rem (h + h + 1) ((h + h) ^ mu h a) := by
  have hperm := perm_fun (sk h a) h (fun k h0 hk => sk_bounds h a k hp ha h0 hk)
    (fun i j h0 hij hj => sk_inj h a i j hp ha h0 hij hj)
  have hprod : rem (h + h + 1) (prod1 h (fun k => a * k))
      = rem (h + h + 1) ((h + h) ^ mu h a * prod1 h (fun k => k)) := by
    rw [rem_prod1_congr (h + h) _ (fun k => (h + h) ^ lk h a k * sk h a k) h
      (fun k _ _ => rk_sign h a k)]
    rw [prod1_mul h (fun k => (h + h) ^ lk h a k) (sk h a), prod1_pow, hperm.1]
    rfl
  have hl : prod1 h (fun k => a * k) = a ^ h * prod1 h (fun k => k) := by
    rw [prod1_mul h (fun _ => a) (fun k => k), prod1_const]
  rw [hl, nmul_comm (a ^ h), nmul_comm ((h + h) ^ mu h a)] at hprod
  exact rem_cancel_mul (h + h) _ _ _ hp
    (not_dv_prod1 _ hp _ h (fun k h0 hk => not_dv_of_lt _ _ h0 (lt_p_of_le_h h k hk))) hprod

theorem par_one_ne (h : Nat) (e : h + h = 1) : False := by
  have e2 := congrArg par e
  rw [par_double] at e2
  exact Bool.noConfusion e2

/-- **Gauss's lemma.** For `p ∤ a`: `a` is a square mod p exactly when μ is even,
    i.e. `(a|p) = (-1)^μ`. -/
theorem gauss_lemma (h a : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a) :
    (∃ x, rem (h + h + 1) (x * x) = rem (h + h + 1) a) ↔ par (mu h a) = false := by
  have key : rem (h + h + 1) (a ^ h) = 1 ↔ par (mu h a) = false := by
    rw [gauss_pow h a hp ha]
    have hs := par_split (mu h a)
    constructor
    · intro e
      revert hs
      cases par (mu h a) with
      | false => intro _; rfl
      | true =>
        intro hs
        have ⟨n, hn⟩ := hs.2 rfl
        rw [hn, neg_pow_odd h n hp] at e
        exact False.elim (par_one_ne h e)
    · intro hf
      have ⟨n, hn⟩ := hs.1 hf
      rw [hn]
      exact neg_pow_even h n hp
  exact (euler_iff h a hp ha).symm.trans key

/-! ## Eisenstein: μ has the parity of Σ ⌊a k / p⌋ (a odd) -/

theorem bx_solve (x y z : Bool) (h : y = bx x (bx y z)) : x = z := by
  cases x <;> cases y <;> cases z <;> first | rfl | exact Bool.noConfusion h

theorem par_p (h : Nat) : par (h + h + 1) = true := by
  rw [par_add, par_double]; rfl

theorem eisenstein_par (h a : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a)
    (hodd : par a = true) :
    par (sum1 h (fun k => fl (h + h) (a * k))) = par (mu h a) := by
  have hperm := perm_fun (sk h a) h (fun k h0 hk => sk_bounds h a k hp ha h0 hk)
    (fun i j h0 hij hj => sk_inj h a i j hp ha h0 hij hj)
  -- a S = p F + R
  have h1 : a * sum1 h (fun k => k)
      = (h + h + 1) * sum1 h (fun k => fl (h + h) (a * k)) + sum1 h (rk h a) := by
    rw [sum1_mul_left, sum1_mul_left, ← sum1_add]
    apply sum1_ext
    intro k _ _
    show a * k = (h + h + 1) * fl (h + h) (a * k) + rem (h + h + 1) (a * k)
    rw [nmul_comm (h + h + 1)]
    exact fl_spec (h + h) (a * k)
  -- R + 2X = S + p μ
  have h2 : sum1 h (rk h a) + (sum1 h (fun k => lk h a k * sk h a k)
      + sum1 h (fun k => lk h a k * sk h a k))
      = sum1 h (fun k => k) + (h + h + 1) * mu h a := by
    rw [← sum1_add, ← sum1_add, ← hperm.2]
    show _ = sum1 h (sk h a) + (h + h + 1) * sum1 h (lk h a)
    rw [sum1_mul_left, ← sum1_add]
    apply sum1_ext
    intro k _ _
    show rk h a k + (lk h a k * sk h a k + lk h a k * sk h a k)
      = sk h a k + (h + h + 1) * lk h a k
    match rk_cases h a k with
    | Or.inl ⟨_, hs, hl⟩ =>
      rw [hl, hs, nzero_mul, Nat.mul_zero]
    | Or.inr ⟨_, hsum, hl⟩ =>
      rw [hl, Nat.one_mul, Nat.mul_one, ← Nat.add_assoc, hsum]
      exact Nat.add_comm _ _
  have p1 := congrArg par h1
  have p2 := congrArg par h2
  rw [par_mul, hodd, par_add, par_mul, par_p] at p1
  rw [par_add, par_double, bx_false, par_add, par_mul, par_p] at p2
  rw [p2] at p1
  exact bx_solve _ _ _ p1

/-- Gauss and Eisenstein together: for odd `a` with `p ∤ a`,
    `a` is a square mod p exactly when `Σ_{k=1}^{h} ⌊a k / p⌋` is even. -/
theorem qr_by_floor (h a : Nat) (hp : Pr (h + h + 1)) (ha : ¬ Dv (h + h + 1) a)
    (hodd : par a = true) :
    (∃ x, rem (h + h + 1) (x * x) = rem (h + h + 1) a) ↔
      par (sum1 h (fun k => fl (h + h) (a * k))) = false := by
  rw [eisenstein_par h a hp ha hodd]
  exact gauss_lemma h a hp ha

/-! ## Eisenstein's lattice count -/

/-- `[a < b]` as 0 or 1. -/
def ltI (a b : Nat) : Nat := if a < b then 1 else 0

theorem ltI_one (a b : Nat) (h : a < b) : ltI a b = 1 := ite_eq_left h

theorem ltI_zero (a b : Nat) (h : b ≤ a) : ltI a b = 0 := ite_eq_right (Nat.not_lt_of_le h)

theorem ltI_add (a b : Nat) (hne : a ≠ b) : ltI a b + ltI b a = 1 :=
  match Nat.lt_or_ge a b with
  | Or.inl hl => by rw [ltI_one a b hl, ltI_zero b a (Nat.le_of_lt hl)]
  | Or.inr hg => by
    rw [ltI_zero a b hg, ltI_one b a (lt_of_le_of_ne' b a hg (fun e => hne e.symm))]

theorem count_low (m' N Q r : Nat) (hN : N = Q * (m' + 1) + r) (hr : 0 < r) :
    ∀ Y : Nat, Y ≤ Q → sum1 Y (fun y => ltI ((m' + 1) * y) N) = Y
  | 0, _ => rfl
  | Y + 1, hY => by
    show sum1 Y (fun y => ltI ((m' + 1) * y) N) + ltI ((m' + 1) * (Y + 1)) N = Y + 1
    rw [count_low m' N Q r hN hr Y (Nat.le_of_succ_le hY), ltI_one]
    rw [hN, nmul_comm (m' + 1) (Y + 1)]
    exact Nat.lt_of_le_of_lt (Nat.mul_le_mul_right (m' + 1) hY) (Nat.lt_add_of_pos_right hr)

theorem count_high (m' N Q r : Nat) (hN : N = Q * (m' + 1) + r) (hr0 : 0 < r)
    (hr : r < m' + 1) : ∀ j : Nat, sum1 (Q + j) (fun y => ltI ((m' + 1) * y) N) = Q
  | 0 => count_low m' N Q r hN hr0 Q (Nat.le_refl Q)
  | j + 1 => by
    show sum1 (Q + j) (fun y => ltI ((m' + 1) * y) N) + ltI ((m' + 1) * (Q + j + 1)) N = Q
    rw [count_high m' N Q r hN hr0 hr j, ltI_zero]
    · rfl
    rw [hN, nmul_comm (m' + 1) (Q + j + 1)]
    apply Nat.le_trans (Nat.add_le_add_left (Nat.le_of_lt hr) _)
    rw [← nsucc_mul]
    exact Nat.mul_le_mul_right _ (Nat.succ_le_succ (Nat.le_add_right Q j))

/-- `⌊N/m⌋` counts the `y` in `1..Y` with `m y < N`, once `⌊N/m⌋ ≤ Y` and `m ∤ N`. -/
theorem fl_count (m' N Y : Nat) (hr0 : rem (m' + 1) N ≠ 0) (hY : fl m' N ≤ Y) :
    sum1 Y (fun y => ltI ((m' + 1) * y) N) = fl m' N :=
  match Nat.le.dest hY with
  | ⟨j, hj⟩ => by
    rw [← hj]
    exact count_high m' N (fl m' N) (rem (m' + 1) N) (fl_spec m' N)
      (Nat.pos_of_ne_zero hr0) (rem_lt' m' N) j

theorem double_inj (h g : Nat) (e : h + h = g + g) : h = g :=
  match Nat.lt_trichotomy h g with
  | Or.inl hl => absurd (Nat.lt_of_lt_of_le (Nat.add_lt_add hl hl) (Nat.le_of_eq e.symm))
      (Nat.lt_irrefl _)
  | Or.inr (Or.inl he) => he
  | Or.inr (Or.inr hl) => absurd (Nat.lt_of_lt_of_le (Nat.add_lt_add hl hl) (Nat.le_of_eq e))
      (Nat.lt_irrefl _)

theorem not_dv_qx (h g x : Nat) (hp : Pr (h + h + 1)) (hq : Pr (g + g + 1)) (hne : h ≠ g)
    (h0 : 0 < x) (hx : x ≤ h) : ¬ Dv (h + h + 1) ((g + g + 1) * x) := fun hd =>
  match euclid _ _ _ hp hd with
  | Or.inr hdx => not_dv_of_lt _ _ h0 (lt_p_of_le_h h x hx) hdx
  | Or.inl hdq =>
    match hq.2 _ hdq with
    | Or.inl e1 => by
      have e2 : h + h = 0 := succ_inj' e1
      have l := hh_pos h hp
      rw [e2] at l
      exact Nat.lt_irrefl 0 l
    | Or.inr e2 => hne (double_inj h g (succ_inj' e2))

theorem fl_le (h g x : Nat) (hx : x ≤ h) : fl (h + h) ((g + g + 1) * x) ≤ g :=
  match Nat.lt_or_ge g (fl (h + h) ((g + g + 1) * x)) with
  | Or.inr hge => hge
  | Or.inl hlt => by
    have e := fl_spec (h + h) ((g + g + 1) * x)
    have i1 : g + (h + h + 1) = h + (h + g + 1) :=
      congrArg Nat.succ ((Nat.add_comm g (h + h)).trans (Nat.add_assoc h h g))
    have i2 : (g + 1) * (h + h + 1) = (g + g + 1) * h + (h + g + 1) := by
      rw [nsucc_mul g, nsucc_mul (g + g) h, nright_distrib g g h]
      show g * (h + h) + g + (h + h + 1) = g * h + g * h + h + (h + g + 1)
      rw [nleft_distrib g h h, Nat.add_assoc (g * h + g * h) g, Nat.add_assoc (g * h + g * h) h, i1]
    have c1 : (g + 1) * (h + h + 1) ≤ (g + g + 1) * h :=
      Nat.le_trans (Nat.mul_le_mul_right _ hlt)
        (Nat.le_trans (Nat.le_add_right _ (rem (h + h + 1) ((g + g + 1) * x)))
          (Nat.le_trans (Nat.le_of_eq e.symm) (Nat.mul_le_mul_left _ hx)))
    rw [i2] at c1
    exact absurd (Nat.lt_of_lt_of_le (Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)) c1)
      (Nat.lt_irrefl _)

theorem fl_as_count (h g x : Nat) (hp : Pr (h + h + 1)) (hq : Pr (g + g + 1)) (hne : h ≠ g)
    (h0 : 0 < x) (hx : x ≤ h) :
    fl (h + h) ((g + g + 1) * x) = sum1 g (fun y => ltI ((h + h + 1) * y) ((g + g + 1) * x)) :=
  (fl_count (h + h) _ g (fun e => not_dv_qx h g x hp hq hne h0 hx (dv_of_rem_zero _ _ e))
    (fl_le h g x hx)).symm

/-- **Eisenstein's lattice count.**
    `Σ_{k=1}^{h} ⌊k q/p⌋ + Σ_{k=1}^{g} ⌊k p/q⌋ = h g`. -/
theorem lattice (h g : Nat) (hp : Pr (h + h + 1)) (hq : Pr (g + g + 1)) (hne : h ≠ g) :
    sum1 h (fun x => fl (h + h) ((g + g + 1) * x)) + sum1 g (fun y => fl (g + g) ((h + h + 1) * y))
      = h * g :=
  calc sum1 h (fun x => fl (h + h) ((g + g + 1) * x)) + sum1 g (fun y => fl (g + g) ((h + h + 1) * y))
      = sum1 h (fun x => sum1 g (fun y => ltI ((h + h + 1) * y) ((g + g + 1) * x)))
        + sum1 g (fun y => sum1 h (fun x => ltI ((g + g + 1) * x) ((h + h + 1) * y))) := by
        rw [sum1_ext h _ _ (fun x h0 hx => fl_as_count h g x hp hq hne h0 hx),
          sum1_ext g _ _ (fun y h0 hy => fl_as_count g h y hq hp (fun e => hne e.symm) h0 hy)]
    _ = sum1 h (fun x => sum1 g (fun y => ltI ((h + h + 1) * y) ((g + g + 1) * x)))
        + sum1 h (fun x => sum1 g (fun y => ltI ((g + g + 1) * x) ((h + h + 1) * y))) :=
        congrArg (fun t => sum1 h (fun x => sum1 g (fun y => ltI ((h + h + 1) * y) ((g + g + 1) * x))) + t)
          (sum1_swap (fun x y => ltI ((g + g + 1) * x) ((h + h + 1) * y)) g h).symm
    _ = sum1 h (fun x => sum1 g (fun y => ltI ((h + h + 1) * y) ((g + g + 1) * x))
          + sum1 g (fun y => ltI ((g + g + 1) * x) ((h + h + 1) * y))) := (sum1_add h _ _).symm
    _ = sum1 h (fun x => sum1 g (fun y => ltI ((h + h + 1) * y) ((g + g + 1) * x)
          + ltI ((g + g + 1) * x) ((h + h + 1) * y))) :=
        sum1_ext h _ _ (fun x _ _ => (sum1_add g _ _).symm)
    _ = sum1 h (fun _ => sum1 g (fun _ => 1)) :=
        sum1_ext h _ _ (fun x h0 hx => sum1_ext g _ _ (fun y _ _ =>
          ltI_add _ _ (fun e => not_dv_qx h g x hp hq hne h0 hx
            ⟨y, e.symm.trans (nmul_comm _ _)⟩)))
    _ = h * g := by rw [sum1_const g 1, sum1_const h, Nat.mul_one]

/-! ## Quadratic reciprocity -/

theorem beq_refl' : ∀ n : Nat, Nat.beq n n = true
  | 0 => rfl
  | n + 1 => beq_refl' n

def sqS (m a : Nat) : Nat → Bool
  | 0 => Nat.beq (rem m (0 * 0)) (rem m a)
  | n + 1 => sqS m a n || Nat.beq (rem m ((n + 1) * (n + 1))) (rem m a)

/-- The Legendre symbol as a Boolean: `true` for +1 (a square mod m),
    `false` for -1. -/
def legB (m a : Nat) : Bool := sqS m a m

theorem or_true_cases (b c : Bool) (h : (b || c) = true) : b = true ∨ c = true := by
  cases b <;> cases c <;> first | exact Or.inl rfl | exact Or.inr rfl | exact Bool.noConfusion h

theorem or_true_right (b : Bool) : (b || true) = true := by cases b <;> rfl

theorem sqS_sound (m a : Nat) : ∀ n : Nat, sqS m a n = true → ∃ x, rem m (x * x) = rem m a
  | 0, h => ⟨0, Nat.eq_of_beq_eq_true h⟩
  | n + 1, h =>
    match or_true_cases _ _ h with
    | Or.inl h1 => sqS_sound m a n h1
    | Or.inr h2 => ⟨n + 1, Nat.eq_of_beq_eq_true h2⟩

theorem sqS_complete (m a : Nat) : ∀ n x : Nat, x ≤ n → rem m (x * x) = rem m a →
    sqS m a n = true
  | 0, x, hx, e => by
    have x0 : x = 0 := Nat.le_antisymm hx (Nat.zero_le x)
    rw [x0] at e
    show Nat.beq (rem m (0 * 0)) (rem m a) = true
    rw [e]; exact beq_refl' _
  | n + 1, x, hx, e =>
    match Nat.eq_or_lt_of_le hx with
    | Or.inl ex => by
      show (sqS m a n || Nat.beq (rem m ((n + 1) * (n + 1))) (rem m a)) = true
      rw [← ex, e, beq_refl']
      exact or_true_right _
    | Or.inr lx => by
      show (sqS m a n || Nat.beq (rem m ((n + 1) * (n + 1))) (rem m a)) = true
      rw [sqS_complete m a n x (Nat.le_of_lt_succ lx) e]
      rfl

theorem legB_iff (m' a : Nat) :
    legB (m' + 1) a = true ↔ ∃ x, rem (m' + 1) (x * x) = rem (m' + 1) a :=
  ⟨sqS_sound (m' + 1) a (m' + 1), fun ⟨x, e⟩ =>
    sqS_complete (m' + 1) a (m' + 1) (rem (m' + 1) x) (Nat.le_of_lt (rem_lt' m' x))
      ((rem_mul_congr2 m' _ _ _ _ (rem_idem m' x) (rem_idem m' x)).trans e)⟩

theorem b_of_iff (x y : Bool) (h : x = true ↔ y = false) : x = !y := by
  cases x <;> cases y
  · exact absurd (h.mpr rfl) (fun e => Bool.noConfusion e)
  · rfl
  · rfl
  · exact absurd (h.mp rfl) (fun e => Bool.noConfusion e)

/-- **Quadratic reciprocity.** For distinct odd primes `p = 2h+1`, `q = 2g+1`
    (written `h + h + 1`, `g + g + 1`):
    `(p|q)(q|p) = (-1)^(h g)`, with the product of signs read as
    "different" (`bx` = exclusive or) and `(-1)^n` read as `par n`. -/
theorem qr_law (h g : Nat) (hp : Pr (h + h + 1)) (hq : Pr (g + g + 1)) (hne : h ≠ g) :
    bx (legB (h + h + 1) (g + g + 1)) (legB (g + g + 1) (h + h + 1)) = par (h * g) := by
  have nq : ¬ Dv (h + h + 1) (g + g + 1) := fun hd =>
    not_dv_qx h g 1 hp hq hne Nat.zero_lt_one (h_pos h hp) (by rw [Nat.mul_one]; exact hd)
  have np : ¬ Dv (g + g + 1) (h + h + 1) := fun hd =>
    not_dv_qx g h 1 hq hp (fun e => hne e.symm) Nat.zero_lt_one (h_pos g hq)
      (by rw [Nat.mul_one]; exact hd)
  have hA := (legB_iff (h + h) (g + g + 1)).trans (qr_by_floor h (g + g + 1) hp nq (par_p g))
  have hB := (legB_iff (g + g) (h + h + 1)).trans (qr_by_floor g (h + h + 1) hq np (par_p h))
  rw [b_of_iff _ _ hA, b_of_iff _ _ hB, ← lattice h g hp hq hne, par_add]
  cases par (sum1 h (fun k => fl (h + h) ((g + g + 1) * k))) <;>
    cases par (sum1 g (fun k => fl (g + g) ((h + h + 1) * k))) <;> rfl

/-- The same, as a statement about squares: `q` is a square mod `p` exactly as
    `p` is a square mod `q`, unless both `h` and `g` are odd. -/
theorem iff_bx (a b : Bool) : ((a = true ↔ b = true) ↔ bx a b = false) := by
  cases a <;> cases b
  · exact ⟨fun _ => rfl, fun _ => Iff.rfl⟩
  · exact ⟨fun h => absurd (h.mpr rfl) (fun e => Bool.noConfusion e), fun e => Bool.noConfusion e⟩
  · exact ⟨fun h => absurd (h.mp rfl) (fun e => Bool.noConfusion e), fun e => Bool.noConfusion e⟩
  · exact ⟨fun _ => rfl, fun _ => Iff.rfl⟩

theorem qr_law_iff (h g : Nat) (hp : Pr (h + h + 1)) (hq : Pr (g + g + 1)) (hne : h ≠ g) :
    ((∃ x, rem (h + h + 1) (x * x) = rem (h + h + 1) (g + g + 1)) ↔
      (∃ y, rem (g + g + 1) (y * y) = rem (g + g + 1) (h + h + 1))) ↔ par (h * g) = false := by
  have e := iff_bx (legB (h + h + 1) (g + g + 1)) (legB (g + g + 1) (h + h + 1))
  rw [qr_law h g hp hq hne] at e
  have iA := legB_iff (h + h) (g + g + 1)
  have iB := legB_iff (g + g) (h + h + 1)
  exact ⟨fun hAB => e.mp ⟨fun a => iB.mpr (hAB.mp (iA.mp a)), fun b => iA.mpr (hAB.mpr (iB.mp b))⟩,
    fun hf => ⟨fun a => iB.mp ((e.mpr hf).mp (iA.mpr a)), fun b => iA.mp ((e.mpr hf).mpr (iB.mpr b))⟩⟩

/-! ## Checks by computation -/

set_option maxRecDepth 100000 in
theorem leg_values : legB 7 2 = true ∧ legB 7 3 = false ∧ legB 11 5 = true ∧ legB 11 2 = false :=
  ⟨rfl, rfl, rfl, rfl⟩

set_option maxRecDepth 100000 in
theorem qr_example : bx (legB 7 11) (legB 11 7) = par (3 * 5) := rfl

end QRChain
#print axioms QRChain.add_left_cancel'
#print axioms QRChain.add_right_cancel'
#print axioms QRChain.le_of_add_le_add_left'
#print axioms QRChain.lt_of_add_lt_add_left'
#print axioms QRChain.add_sub_of_le'
#print axioms QRChain.lt_irrefl_of_le
#print axioms QRChain.eq_zero_of_add_eq_self
#print axioms QRChain.lt_of_le_of_ne'
#print axioms QRChain.par_add
#print axioms QRChain.par_mul
#print axioms QRChain.par_double
#print axioms QRChain.par_split
#print axioms QRChain.bx_self
#print axioms QRChain.bx_false
#print axioms QRChain.bx_comm
#print axioms QRChain.bx_assoc
#print axioms QRChain.sum1_ext
#print axioms QRChain.prod1_ext
#print axioms QRChain.sum1_add
#print axioms QRChain.sum1_mul_left
#print axioms QRChain.sum1_const
#print axioms QRChain.sum1_swap
#print axioms QRChain.prod1_mul
#print axioms QRChain.pow_add'
#print axioms QRChain.pow_mul'
#print axioms QRChain.prod1_const
#print axioms QRChain.prod1_pow
#print axioms QRChain.rem_of_lt
#print axioms QRChain.rem_lt'
#print axioms QRChain.rem_zero_of_dv
#print axioms QRChain.dv_of_rem_zero
#print axioms QRChain.rem_mul_left
#print axioms QRChain.rem_mul_congr
#print axioms QRChain.rem_mul_congr'
#print axioms QRChain.rem_mul_congr2
#print axioms QRChain.rem_add_congr'
#print axioms QRChain.rem_add_congr2
#print axioms QRChain.rem_pow_congr
#print axioms QRChain.rem_cancel_add
#print axioms QRChain.dv_of_rem_add
#print axioms QRChain.rem_add_dv
#print axioms QRChain.rem_prod1_congr
#print axioms QRChain.euclid
#print axioms QRChain.eq_zero_of_dv_lt
#print axioms QRChain.not_dv_of_lt
#print axioms QRChain.not_dv_prod1
#print axioms QRChain.cancel_core
#print axioms QRChain.rem_cancel_mul
#print axioms QRChain.allL_mono
#print axioms QRChain.qd_length
#print axioms QRChain.qd_spec
#print axioms QRChain.root_quot
#print axioms QRChain.roots_le_n
#print axioms QRChain.roots_le
#print axioms QRChain.h_pos
#print axioms QRChain.rem_one
#print axioms QRChain.hh_pos
#print axioms QRChain.fermat1
#print axioms QRChain.sq_pow
#print axioms QRChain.sq_one
#print axioms QRChain.euler_pm
#print axioms QRChain.euler_of_sq
#print axioms QRChain.tailL_length
#print axioms QRChain.ev_tailL
#print axioms QRChain.sqL_length
#print axioms QRChain.sqL_mem
#print axioms QRChain.sq_expand
#print axioms QRChain.lt_p_of_le_h
#print axioms QRChain.sq_distinct
#print axioms QRChain.sqL_nodup
#print axioms QRChain.root_of_pow
#print axioms QRChain.sq_search
#print axioms QRChain.euler_iff
#print axioms QRChain.memL_dec
#print axioms QRChain.eraseL_eq
#print axioms QRChain.eraseL_ne
#print axioms QRChain.erase_mem
#print axioms QRChain.erase_ne
#print axioms QRChain.erase_nodup
#print axioms QRChain.erase_count
#print axioms QRChain.erase_not_mem
#print axioms QRChain.perm_count
#print axioms QRChain.mkL_length
#print axioms QRChain.prodL_mkL
#print axioms QRChain.sumL_mkL
#print axioms QRChain.mkL_mem
#print axioms QRChain.mkL_nodup
#print axioms QRChain.perm_fun
#print axioms QRChain.dm_spec
#print axioms QRChain.fl_spec
#print axioms QRChain.fl_values
#print axioms QRChain.rk_cases
#print axioms QRChain.rk_pos
#print axioms QRChain.sk_bounds
#print axioms QRChain.rk_sign
#print axioms QRChain.rk_no_eq
#print axioms QRChain.rk_no_sum
#print axioms QRChain.sk_inj
#print axioms QRChain.neg_sq
#print axioms QRChain.neg_sq'
#print axioms QRChain.neg_pow_even
#print axioms QRChain.neg_pow_odd
#print axioms QRChain.gauss_pow
#print axioms QRChain.par_one_ne
#print axioms QRChain.gauss_lemma
#print axioms QRChain.bx_solve
#print axioms QRChain.beq_refl'
#print axioms QRChain.par_p
#print axioms QRChain.eisenstein_par
#print axioms QRChain.qr_by_floor
#print axioms QRChain.ltI_one
#print axioms QRChain.ltI_zero
#print axioms QRChain.ltI_add
#print axioms QRChain.count_low
#print axioms QRChain.count_high
#print axioms QRChain.fl_count
#print axioms QRChain.double_inj
#print axioms QRChain.not_dv_qx
#print axioms QRChain.fl_le
#print axioms QRChain.fl_as_count
#print axioms QRChain.lattice
#print axioms QRChain.or_true_cases
#print axioms QRChain.or_true_right
#print axioms QRChain.sqS_sound
#print axioms QRChain.sqS_complete
#print axioms QRChain.legB_iff
#print axioms QRChain.b_of_iff
#print axioms QRChain.qr_law
#print axioms QRChain.iff_bx
#print axioms QRChain.qr_law_iff
#print axioms QRChain.leg_values
#print axioms QRChain.qr_example

Listing 142: NonAbChain.lean

NonAbChain.lean

/-
  NonAbChain.lean
  Non-abelian reciprocity for S3 (link 73).

  Lean 4 core plus the earlier work/ files (ESChain for `mul1m`, `oneS`, `nth`,
  `primes200`; HeckeChain for `intEq`). Proofs are hand-written terms;
  no omega / simp / ring / decide / norm_num / linarith. Concrete checks use `rfl`.

  S3 is listed as the six permutations of Fin 3. The 2-dimensional
  irreducible character is χ = (fixed points) - 1. The weight-1 form
  η(z)η(23z) = q Π (1-q^n)(1-q^(23n)) has a_p = r_p - 1, where r_p is the
  number of roots of x^3 - x - 1 mod p (the splitting field of that cubic has
  Galois group S3 and discriminant -23).

  Proved here:
    perms_list, fix_values, chi_values      the six permutations, fixed points, χ
    comp_check, inv_check                   composition and inverse are right
    chi_class                               χ(h g h⁻¹) = χ(g) for all 36 pairs
    chi_norm                                Σ χ(g)^2 = 6, i.e. ⟨χ, χ⟩ = 1
    noncomm_bool, noncomm                   στ ≠ τσ
    a23List_eq, a23_values                  coefficients of η(z)η(23z), n ≤ 200
    r_values                                r_p for small p
    ap_eq_rp                                a_p = r_p - 1, primes p < 200, p ≠ 23
    rp_013                                  r_p ∈ {0, 1, 3} for the same p
-/
import HeckeChain

namespace NonAbChain
open FTAChain HeckeChain

/-! ## S3 as the permutations of Fin 3 -/

/-- A map `Fin 3 → Fin 3` given by its values at 0, 1, 2. -/
structure P3 where
  a : Fin 3
  b : Fin 3
  c : Fin 3

def f0 : Fin 3 := ⟨0, Nat.zero_lt_succ 2⟩
def f1 : Fin 3 := ⟨1, Nat.succ_lt_succ (Nat.zero_lt_succ 1)⟩
def f2 : Fin 3 := ⟨2, Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ 0))⟩

def app (s : P3) (i : Fin 3) : Fin 3 :=
  match i.val with
  | 0 => s.a
  | 1 => s.b
  | _ + 2 => s.c

/-- `(s ∘ t)(i) = s (t i)`. -/
def comp (s t : P3) : P3 := ⟨app s (app t f0), app s (app t f1), app s (app t f2)⟩

def finEq (i j : Fin 3) : Bool := Nat.beq i.val j.val

def p3Eq (s t : P3) : Bool := finEq s.a t.a && finEq s.b t.b && finEq s.c t.c

/-- The preimage of `j` under a permutation. -/
def pre (s : P3) (j : Fin 3) : Fin 3 :=
  match finEq s.a j with
  | true => f0
  | false => match finEq s.b j with
    | true => f1
    | false => f2

def inv (s : P3) : P3 := ⟨pre s f0, pre s f1, pre s f2⟩

def e   : P3 := ⟨f0, f1, f2⟩
def t01 : P3 := ⟨f1, f0, f2⟩
def t02 : P3 := ⟨f2, f1, f0⟩
def t12 : P3 := ⟨f0, f2, f1⟩
def c1  : P3 := ⟨f1, f2, f0⟩
def c2  : P3 := ⟨f2, f0, f1⟩

/-- **The six elements of S3.** -/
def S3 : List P3 := [e, t01, t02, t12, c1, c2]

def vals (s : P3) : List Nat := [s.a.val, s.b.val, s.c.val]

theorem perms_list : S3.map vals =
    [[0, 1, 2], [1, 0, 2], [2, 1, 0], [0, 2, 1], [1, 2, 0], [2, 0, 1]] := rfl

/-- Number of fixed points. -/
def fix (s : P3) : Nat :=
  (match finEq s.a f0 with | true => 1 | false => 0) +
  (match finEq s.b f1 with | true => 1 | false => 0) +
  (match finEq s.c f2 with | true => 1 | false => 0)

/-- The character χ = fixed points - 1. -/
def chi (s : P3) : Int := Int.ofNat (fix s) - 1

theorem fix_values : S3.map fix = [3, 1, 1, 1, 0, 0] := rfl

theorem chi_values : S3.map chi = [2, 0, 0, 0, -1, -1] := rfl

/-- `allL L P` : `P` holds on every element of `L`. -/
def allL {A : Type} (P : A → Bool) : List A → Bool
  | [] => true
  | x :: xs => P x && allL P xs

/-- Every element of S3 is a permutation: `s ∘ s⁻¹ = s⁻¹ ∘ s = e`. -/
theorem inv_check : allL (fun s => p3Eq (comp s (inv s)) e && p3Eq (comp (inv s) s) e) S3 = true :=
  rfl

/-- `anyL L P` : `P` holds on some element of `L`. -/
def anyL {A : Type} (P : A → Bool) : List A → Bool
  | [] => false
  | x :: xs => P x || anyL P xs

/-- S3 is closed under composition: every `s ∘ t` is in the list. -/
theorem comp_check : allL (fun s => allL (fun t =>
    anyL (fun u => p3Eq (comp s t) u) S3) S3) S3 = true := rfl

/-- **χ is a class function**: `χ(h g h⁻¹) = χ(g)` for all 6 × 6 pairs. -/
theorem chi_class : allL (fun g => allL (fun h =>
    intEq (chi (comp (comp h g) (inv h))) (chi g)) S3) S3 = true := rfl

def sumI : List Int → Int
  | [] => 0
  | x :: xs => x + sumI xs

/-- **Σ_g χ(g)^2 = 6 = |S3|**, i.e. ⟨χ, χ⟩ = 1: χ is irreducible. -/
theorem chi_norm : sumI (S3.map (fun g => chi g * chi g)) = 6 := rfl

/-- **S3 is non-abelian** (Bool form). -/
theorem noncomm_bool : p3Eq (comp t01 t12) (comp t12 t01) = false := rfl

/-- **S3 is non-abelian**: `t01 ∘ t12 ≠ t12 ∘ t01` (they send 0 to 1 and 2). -/
theorem noncomm : comp t01 t12 ≠ comp t12 t01 := fun h =>
  have h12 : (1 : Nat) = 2 := congrArg (fun s => s.a.val) h
  succ_ne_zero' 0 (succ_inj' h12).symm

/-! ## η(z) η(23z) -/

/-- Multiply by `Π_{n=1}^{N} (1 - q^n)`. -/
def prodA : Nat → List Int → List Int
  | 0, l => l
  | n + 1, l => prodA n (ESChain.mul1m (n + 1) l)

/-- Multiply by `Π_{n=1}^{N} (1 - q^(23n))`. -/
def prodB : Nat → List Int → List Int
  | 0, l => l
  | n + 1, l => prodB n (ESChain.mul1m (23 * (n + 1)) l)

/-- Coefficients of `q^0, ..., q^(N-1)` of `Π (1-q^n)(1-q^(23n))`. -/
def eta23 (N : Nat) : List Int := prodB N (prodA N (ESChain.oneS N))

/-- Coefficients of q^1, ..., q^200 of `η(z)η(23z) = q Π (1-q^n)(1-q^(23n))`. -/
def a23Table : List Int :=
  [1, -1, -1, 0, 0, 1, 0, 1, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1, -1, 1, 1, 1, 0, -1, 0,
   -1, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, -1, -1, 1, 1, -1, 0, 0, 0, -1, 0, 0, 0, 1, 2, 0,
   0, 1, 0, 1, 0, 0, 0, 0, -1, 0, -1, 0, -1, 0, -1, 0, 0, -1, 0, 0, -1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
   0, 0, 1, 1, 0, 0, 0, -1, 0, 0, 2, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 1,
   0, 1, 0, 0, 0, -1, -1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1, -1, 0, 1, 1, 0, 0, 0, 1, -1, 0, 0, 1, -1,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, -1, 0, 0, -2, 0, -1, 0, 0,
   0, 0, 1, 0, -1, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, -1, 0, 0, 1]

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem a23List_eq : eta23 200 = a23Table := by rfl

/-- `a_n` for `1 ≤ n ≤ 200`. -/
def a23 (n : Nat) : Int := ESChain.nth a23Table (n - 1)

theorem a23_values : a23 1 = 1 ∧ a23 2 = -1 ∧ a23 3 = -1 ∧ a23 6 = 1 ∧ a23 8 = 1 :=
  ⟨rfl, rfl, rfl, rfl, rfl⟩

/-! ## Roots of x^3 - x - 1 mod p -/

/-- `x^3 - x - 1 ≡ 0 (mod p)`, i.e. `x^3 ≡ x + 1`. -/
def isRoot (p x : Nat) : Nat :=
  match Nat.beq ((x * x * x) % p) ((x + 1) % p) with
  | true => 1
  | false => 0

/-- `Σ_{x < k} isRoot p x`. -/
def cntTo (p : Nat) : Nat → Nat
  | 0 => 0
  | k + 1 => cntTo p k + isRoot p k

/-- `r_p = #{x mod p : x^3 - x - 1 ≡ 0}`. -/
def r (p : Nat) : Nat := cntTo p p

theorem r_values : r 2 = 0 ∧ r 3 = 0 ∧ r 5 = 1 ∧ r 7 = 1 ∧ r 59 = 3 ∧ r 101 = 3 :=
  ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩

/-- The primes below 200 other than 23. -/
def primesNo23 : List Nat := ESChain.primes200.filter (fun p => !(Nat.beq p 23))

theorem primesNo23_length : primesNo23.length = 45 := rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Non-abelian reciprocity for S3**: for every prime p < 200, p ≠ 23,
    the q^p coefficient of η(z)η(23z) is `r_p - 1`. -/
theorem ap_eq_rp : allL (fun p => intEq (a23 p) (Int.ofNat (r p) - 1)) primesNo23 = true := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- For the same p, `r_p ∈ {0, 1, 3}`: there is no p with exactly two roots. -/
theorem rp_013 : allL (fun p => Nat.ble (r p) 3 && !(Nat.beq (r p) 2)) primesNo23 = true := by rfl

end NonAbChain

#print axioms NonAbChain.perms_list
#print axioms NonAbChain.fix_values
#print axioms NonAbChain.chi_values
#print axioms NonAbChain.inv_check
#print axioms NonAbChain.comp_check
#print axioms NonAbChain.chi_class
#print axioms NonAbChain.chi_norm
#print axioms NonAbChain.noncomm_bool
#print axioms NonAbChain.noncomm
#print axioms NonAbChain.a23List_eq
#print axioms NonAbChain.a23_values
#print axioms NonAbChain.r_values
#print axioms NonAbChain.primesNo23_length
#print axioms NonAbChain.ap_eq_rp
#print axioms NonAbChain.rp_013

78.47 Range and Koszul regularity, \(\ell _0\le 2\)

Over a commutative ring,

\[ H_1(x)=0\iff x\ \text {regular},\qquad x\ \text {regular}\Rightarrow \bigl (H_1(x,y)=0\iff (x,y)\ \text {regular}\bigr ). \]
The steps are
\[ \begin {aligned} H_1(x)&=\{a: xa=0\},\\ H_2(x,y)&=\{a: xa=0,\ ya=0\}=0\ \text {when}\ x\ \text {is regular},\\ H_1(x,y)=0&\iff \bigl (xa+yb=0\Rightarrow (a,b)=(-yc,xc)\ \text {for some}\ c\bigr ),\\ (x,y)\ \text {regular}&\iff \bigl (yb\in (x)\Rightarrow b\in (x)\bigr ),\\ yb=xa'&\Rightarrow x(-a')+yb=0,\qquad b=xc'\Rightarrow x(a+yc')=0\Rightarrow a=-yc'. \end {aligned} \]

Listing 143: KoszulRangeChain.lean

KoszulRangeChain.lean

/-
  KoszulRangeChain — T35 R1: "cohomology vanishes outside the range" ↔ "Koszul form with a
  regular sequence", for l0 = 1 and l0 = 2.

  The patched complex of Calegari–Geraghty is, in the good case, the Koszul complex of
  f_1, …, f_{l0} over the patched ring; its cohomology is concentrated in the top degree of
  the range [q0, q0 + l0] exactly when the lower Koszul homology vanishes.

    l0 = 1:  range1_iff         H_1(x) = 0  ↔  x regular
    l0 = 2:  range2_of_regular  (x, y) regular  ⇒  H_2 = 0 and H_1 = 0       (DetChain.koszul2_*)
             regular_of_range2  x regular and H_1 = 0  ⇒  (x, y) regular
             range2_iff         for x regular:  H_1 = 0  ↔  (x, y) regular
  The hypothesis "x regular" cannot be dropped in a general ring: over ℤ/6 with (x, y) = (2, 3)
  the Koszul complex is exact (the ideal is the unit ideal) while 2·3 = 0.
  In a Noetherian local ring with x, y in the maximal ideal it can be dropped (Matsumura,
  Commutative Ring Theory, Thm 16.5); that step is cited, not proved here.

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain

namespace KoszulRangeChain

open DetChain

variable {R : Type} (K : CRing R)

/-- `H_1` of the length-one Koszul complex `0 → R --x--> R → 0` vanishes. -/
def H1zero1 (x : R) : Prop := ∀ r, K.mul r x = K.zero → r = K.zero

/-- `H_2` of the length-two Koszul complex vanishes. -/
def H2zero (x y : R) : Prop := ∀ r, d2 K x y r = (K.zero, K.zero) → r = K.zero

/-- `H_1` of the length-two Koszul complex vanishes. -/
def H1zero (x y : R) : Prop := ∀ a b, d1 K x y (a, b) = K.zero → ∃ r, (a, b) = d2 K x y r

theorem range1_iff (x : R) : H1zero1 K x ↔ Regular1 K x := Iff.rfl

theorem range2_of_regular (x y : R) (h : Regular2 K x y) : H2zero K x y ∧ H1zero K x y :=
  ⟨fun r hr => koszul2_H2 K x y h r hr, fun a b hab => koszul2_H1 K x y h a b hab⟩

theorem regular_of_range2 (x y : R) (hx : Regular1 K x) (h1 : H1zero K x y) : Regular2 K x y := by
  refine ⟨hx, fun b c hbc => ?_⟩
  -- (−c) x + b y = 0
  have hd : d1 K x y (K.neg c, b) = K.zero := by
    show K.add (K.mul (K.neg c) x) (K.mul b y) = K.zero
    rw [neg_mul_left, hbc, add_neg']
  obtain ⟨r, hr⟩ := h1 (K.neg c) b hd
  exact ⟨r, congrArg Prod.snd hr⟩

theorem range2_iff (x y : R) (hx : Regular1 K x) : H1zero K x y ↔ Regular2 K x y :=
  ⟨regular_of_range2 K x y hx, fun h => (range2_of_regular K x y h).2⟩

end KoszulRangeChain

#print axioms KoszulRangeChain.range1_iff
#print axioms KoszulRangeChain.range2_of_regular
#print axioms KoszulRangeChain.regular_of_range2
#print axioms KoszulRangeChain.range2_iff

78.48 Quadratic Gauss sum

In the group ring \(\mathbb Z[C_p]\), \(g\star g=(-1/p)(p\,\zeta ^0-N)\) with \(g=\sum _a(a/p)\zeta ^a\), \(N=\sum _a\zeta ^a\).

\[ g^2=(-1/p)\,p \quad \text {in } \mathbb Z[\zeta _p]=\mathbb Z[C_p]/(N). \]

lang/GaussSum.lean

/-
  lang/GaussSum — G1 (T43): the quadratic Gauss sum over ℤ[ζ_p], algebraically.

  p = 2m + 3 prime, χ the quadratic character of 𝔽_p (QChar). The group ring ℤ[C_p] is the ring of functions 𝔽_p → ℤ with the
  convolution (f ⋆ h)(c) = Σ_a f(a) h(c − a); ζ^a is the point mass at a, and ℤ[ζ_p] = ℤ[C_p]/(N) with N = Σ_a ζ^a (the constant
  function 1). The Gauss sum is g = Σ_a χ(a) ζ^a, i.e. the function χ itself.
    term          χ(a) χ(c − a) = χ(−1) χ((a + 0)(a − c))
    gauss_ne      c ≠ 0:  (g ⋆ g)(c) = −χ(−1)                    (ConicSum.conic_sum)
    gauss_zero    (g ⋆ g)(0) = χ(−1)(p − 1)                      (χ(a²) = 1 for a ≠ 0)
    gauss_sq      g ⋆ g = χ(−1)·(p·ζ⁰ − N)                       in ℤ[C_p]
    gauss_N       g ⋆ N = 0                                       (QChar.chi_sum)
    gauss_star    g ⋆ g + χ(−1)·N = p*·ζ⁰ with p* = χ(−1)·p:  g² = p* in ℤ[ζ_p] = ℤ[C_p]/(N)
  So √p* = g ∈ ℤ[ζ_p]: the quadratic field ℚ(√p*) lies in the cyclotomic field ℚ(ζ_p) (Kronecker–Weber for the quadratic
  character of conductor p).

  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.ConicSum

namespace GaussSum

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum

variable (m : Nat)

/-- convolution in ℤ[C_p]. -/
def conv (f h : Zp (2 * m + 1 + 1) → Int) (c : Zp (2 * m + 1 + 1)) : Int :=
  sumK LibInt.intR (fun a => f a * h ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (L m)

/-- the norm element N = Σ_a ζ^a. -/
def N : Zp (2 * m + 1 + 1) → Int := fun _ => 1

/-- ζ⁰ = 1. -/
def d0 (c : Zp (2 * m + 1 + 1)) : Int := delta LibInt.intR c (ring (2 * m + 1 + 1)).zero

theorem sum_one : ∀ k, sumK LibInt.intR (fun _ : Zp (2 * m + 1 + 1) => (1 : Int)) (CMInert.allZ (2 * m + 1 + 1) k) = Int.ofNat k
  | 0 => rfl
  | k + 1 => by
    show sumK LibInt.intR (fun _ : Zp (2 * m + 1 + 1) => (1 : Int)) (CMInert.allZ (2 * m + 1 + 1) k ++ [mk (2 * m + 1 + 1) k]) = _
    rw [sumK_append, sum_one k]
    show Int.ofNat k + (1 + 0) = Int.ofNat (k + 1)
    rfl

theorem term (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c a : Zp (2 * m + 1 + 1)) :
    chi m a * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg c))) := by
  rw [← chi_mul m hpr, ← chi_mul m hpr]
  congr 1
  lib_ring

theorem neg_ne (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).zero ≠ (ring (2 * m + 1 + 1)).neg c := fun e => by
  apply hc
  calc c = (ring (2 * m + 1 + 1)).add c (ring (2 * m + 1 + 1)).zero := (DetChain.add_zero' (ring (2 * m + 1 + 1)) c).symm
    _ = (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg c) := by rw [e]
    _ = (ring (2 * m + 1 + 1)).zero := (ring (2 * m + 1 + 1)).add_neg c

/-- **c ≠ 0: (g ⋆ g)(c) = −χ(−1).** -/
theorem gauss_ne (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) :
    conv m (chi m) (chi m) c = - chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  show sumK LibInt.intR (fun a => chi m a * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (L m) = _
  rw [sumK_ext LibInt.intR _ _ (term m hpr c)]
  refine (mul_sumK LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) _ (L m)).symm.trans ?_
  rw [conic_sum m hpr _ _ (neg_ne m c hc)]
  show chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * -1 = _
  lib_int

/-- **(g ⋆ g)(0) = χ(−1)(p − 1).** -/
theorem gauss_zero (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : conv m (chi m) (chi m) (ring (2 * m + 1 + 1)).zero = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (Int.ofNat (2 * m + 1 + 1 + 1) - 1) := by
  have pt : ∀ a, chi m a * chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a)) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (1 + -1 * d0 m a) := fun a => by
    rw [show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) a by lib_ring, chi_mul m hpr]
    by_cases ha : a = (ring (2 * m + 1 + 1)).zero
    · rw [ha, chi_zero]
      show (0 : Int) * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * 0) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (1 + -1 * (if (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero then 1 else 0))
      rw [ite_pos' _ _ rfl]; lib_int
    · have h1 : chi m a * chi m a = 1 := by rw [← chi_mul m hpr]; exact chi_sq m hpr a ha
      show chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * chi m a) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (1 + -1 * (if a = (ring (2 * m + 1 + 1)).zero then 1 else 0))
      rw [ite_neg' _ _ ha, show chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * chi m a) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (chi m a * chi m a) by lib_int, h1]
      lib_int
  show sumK LibInt.intR (fun a => chi m a * chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a))) (L m) = _
  rw [sumK_ext LibInt.intR _ _ pt]
  refine (mul_sumK LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (fun a => LibInt.intR.add 1 (LibInt.intR.mul (-1) (d0 m a))) (L m)).symm.trans ?_
  rw [sumK_add, ← mul_sumK]
  have hc := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero
  rw [show sumK LibInt.intR (fun a => d0 m a) (L m) = 1 from hc]
  show chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (sumK LibInt.intR (fun _ : Zp (2 * m + 1 + 1) => (1 : Int)) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)) + -1 * 1) = _
  rw [sum_one]
  lib_int

/-- **g² = χ(−1)·(p − N) in ℤ[C_p].** -/
theorem gauss_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) :
    conv m (chi m) (chi m) c = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (Int.ofNat (2 * m + 1 + 1 + 1) * d0 m c - N m c) := by
  by_cases hc : c = (ring (2 * m + 1 + 1)).zero
  · rw [hc, gauss_zero m hpr]
    show _ = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (Int.ofNat (2 * m + 1 + 1 + 1) * (if (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero then 1 else 0) - 1)
    rw [ite_pos' _ _ rfl]; lib_int
  · rw [gauss_ne m hpr c hc]
    show _ = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (Int.ofNat (2 * m + 1 + 1 + 1) * (if c = (ring (2 * m + 1 + 1)).zero then 1 else 0) - 1)
    rw [ite_neg' _ _ hc]; lib_int

/-- **g ⋆ N = 0**: g lies in the augmentation ideal. -/
theorem gauss_N (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) : conv m (chi m) (N m) c = 0 := by
  show sumK LibInt.intR (fun a => chi m a * 1) (L m) = 0
  rw [sumK_ext LibInt.intR _ (chi m) (fun a => by lib_int)]
  exact chi_sum m hpr

/-- **g² = p* in ℤ[ζ_p] = ℤ[C_p]/(N)**, p* = χ(−1)·p: (g ⋆ g)(c) + χ(−1)·N(c) = p*·ζ⁰(c) for every c. -/
theorem gauss_star (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) :
    conv m (chi m) (chi m) c + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * N m c = (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * Int.ofNat (2 * m + 1 + 1 + 1)) * d0 m c := by
  rw [gauss_sq m hpr c]; lib_int

end GaussSum

#print axioms GaussSum.conv
#print axioms GaussSum.sum_one
#print axioms GaussSum.term
#print axioms GaussSum.neg_ne
#print axioms GaussSum.gauss_ne
#print axioms GaussSum.gauss_zero
#print axioms GaussSum.gauss_sq
#print axioms GaussSum.gauss_N
#print axioms GaussSum.gauss_star

#print axioms: 9 theorems, no axioms.

78.49 Quadratic reciprocity from the Gauss sum

In the group ring \(\mathbb F_q[C_p]\) the Gauss sum satisfies \(g^q=(q/p)\,g\) and \(g^q=(g^2)^{(q-1)/2}g=(p^*)^{(q-1)/2}g\).

\[ \Bigl (\frac qp\Bigr )=\Bigl (\frac {p^*}q\Bigr ),\qquad p^*=(-1/p)\,p. \]

lang/GaussQR.lean

/-
  lang/GaussQR — G1 (T43): quadratic reciprocity from the Gauss sum.

  p = 2m + 3, q = 2r + 3 distinct odd primes, p* = (−1/p)·p, g = Σ χ_p(a) ζ^a ∈ 𝔽_q[C_p]. In 𝔽_q[C_p]:
    g ⋆ g = p*·ζ⁰ − (−1/p)·N   (GaussSum.gauss_sq, cast to 𝔽_q),   g ⋆ N = 0   (Σ χ_p = 0)
  so g^q = (g ⋆ g)^((q−1)/2) ⋆ g = (p*)^((q−1)/2) g, while g^q = (q/p) g (GaussFrob.gauss_frob). The ζ¹ coefficient of g is 1, so
  (q/p) ≡ (p*)^((q−1)/2) ≡ (p*/q) (mod q) (Euler), and both are ±1:
      qr_gauss     (q/p) = (p*/q)
  This is the law of quadratic reciprocity, proved through ℚ(√p*) ⊂ ℚ(ζ_p): the Frobenius of q acts on √p* = g as ζ ↦ ζ^q.
    sq_coef, gB_coef, sq_split, pow_kill, pw_odd, nsm_zero', mono0_mul, castZ_one', cast_chi_q, chi_pm, qr_gauss
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.GaussFrob

namespace GaussQR

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing GroupRingFrob Frobenius QChar ConicSum GaussSum GaussFrob
open LibRing (castZ castN)

variable (m r : Nat)

/-- p* as an element of 𝔽_q. -/
def P : Zp (2 * r + 1 + 1) := castZ (ring (2 * r + 1 + 1)) (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * Int.ofNat (2 * m + 1 + 1 + 1))
/-- (−1/p) in 𝔽_q. -/
def E : Zp (2 * r + 1 + 1) := castZ (ring (2 * r + 1 + 1)) (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))
/-- (−1/p)·N. -/
def Bv : V (Zp (2 * r + 1 + 1)) ((2 * m + 1 + 1) + 1) := tb (2 * m + 1 + 1) (fun _ => E m r)
/-- p*·ζ⁰. -/
def Av : V (Zp (2 * r + 1 + 1)) ((2 * m + 1 + 1) + 1) := mono (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (ring (2 * m + 1 + 1)).zero (P m r)

theorem castZ_one' {S : Type} (Kr : CRing S) : castZ Kr 1 = Kr.one := add_zero' Kr Kr.one

theorem gq_coef (a : Zp (2 * m + 1 + 1)) : gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) a = castZ (ring (2 * r + 1 + 1)) (chi m a) := by unfold gq; rw [gt_tb]

theorem sq_coef (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) :
    gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) (gq m r)) c = (ring (2 * r + 1 + 1)).add ((ring (2 * r + 1 + 1)).mul (P m r) (delta (ring (2 * r + 1 + 1)) c (ring (2 * m + 1 + 1)).zero)) ((ring (2 * r + 1 + 1)).neg (E m r)) := by
  rw [gt_mul]
  have e : sumK (ring (2 * r + 1 + 1)) (fun a => (ring (2 * r + 1 + 1)).mul (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) a) (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)))) (L (2 * m + 1 + 1))
      = castZ (ring (2 * r + 1 + 1)) (conv m (chi m) (chi m) c) := by
    rw [show conv m (chi m) (chi m) c = sumK LibInt.intR (fun a => chi m a * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (ConicSum.L m) from rfl,
      IntCast.castZ_sum]
    exact sumK_ext (ring (2 * r + 1 + 1)) _ _ (fun a => by rw [gq_coef, gq_coef, HasseDavenport.castZ_mul]) _
  rw [e]
  by_cases hc : c = (ring (2 * m + 1 + 1)).zero
  · rw [hc, gauss_zero m hpr]
    unfold delta; rw [ite_pos' _ _ rfl]
    unfold P E
    rw [show chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (Int.ofNat (2 * m + 1 + 1 + 1) - 1) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * Int.ofNat (2 * m + 1 + 1 + 1) + -chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) by lib_int,
      IntCast.castZ_add, HasseDavenport.castZ_neg]
    lib_ring
  · rw [gauss_ne m hpr c hc]
    unfold delta; rw [ite_neg' _ _ hc]
    unfold E
    rw [HasseDavenport.castZ_neg]
    lib_ring

theorem gB_coef (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) : gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) (Bv m r)) c = (ring (2 * r + 1 + 1)).zero := by
  rw [gt_mul]
  have e : sumK (ring (2 * r + 1 + 1)) (fun a => (ring (2 * r + 1 + 1)).mul (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) a) (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (Bv m r) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)))) (L (2 * m + 1 + 1))
      = (ring (2 * r + 1 + 1)).mul (castZ (ring (2 * r + 1 + 1)) (sumK LibInt.intR (chi m) (ConicSum.L m))) (E m r) := by
    rw [IntCast.castZ_sum, sumK_mul]
    exact sumK_ext (ring (2 * r + 1 + 1)) _ _ (fun a => by unfold Bv; rw [gq_coef, gt_tb]) _
  rw [e, show sumK LibInt.intR (chi m) (ConicSum.L m) = 0 from chi_sum m hpr]
  show (ring (2 * r + 1 + 1)).mul (ring (2 * r + 1 + 1)).zero (E m r) = (ring (2 * r + 1 + 1)).zero
  exact zero_mul' (ring (2 * r + 1 + 1)) _

theorem sq_split (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) (gq m r) = gadd (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (Av m r) (gneg (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (Bv m r)) :=
  ext (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) _ _ (fun c => by
    rw [sq_coef m r hpr]
    unfold gadd gneg Av Bv
    rw [gt_tb, gt_mono, gt_tb, gt_tb])

theorem pow_kill {S : Type} (G : CRing S) (A B g : S) (h : G.mul B g = G.zero) : ∀ k,
    G.mul (pw G (G.add A (G.neg B)) k) g = G.mul (pw G A k) g
  | 0 => rfl
  | k + 1 => by
    show G.mul (G.mul (pw G (G.add A (G.neg B)) k) (G.add A (G.neg B))) g = G.mul (G.mul (pw G A k) A) g
    have e1 : G.mul (G.mul (pw G (G.add A (G.neg B)) k) (G.add A (G.neg B))) g
        = G.add (G.mul A (G.mul (pw G (G.add A (G.neg B)) k) g)) (G.neg (G.mul (pw G (G.add A (G.neg B)) k) (G.mul B g))) := by lib_ring
    rw [e1, h, pow_kill G A B g h k]
    lib_ring

theorem pw_odd {S : Type} (G : CRing S) (x : S) : ∀ k, pw G x (2 * k + 1) = G.mul (pw G (G.mul x x) k) x
  | 0 => rfl
  | k + 1 => by
    show G.mul (G.mul (pw G x (2 * k + 1)) x) x = G.mul (G.mul (pw G (G.mul x x) k) (G.mul x x)) x
    rw [pw_odd G x k]; lib_ring

theorem nsm_zero' {S : Type} (G : CRing S) : ∀ k, nsm G k G.zero = G.zero
  | 0 => rfl
  | k + 1 => by show G.add (nsm G k G.zero) G.zero = G.zero; rw [nsm_zero' G k]; exact add_zero' G _

theorem mono0_mul (s : Zp (2 * r + 1 + 1)) (u : V (Zp (2 * r + 1 + 1)) ((2 * m + 1 + 1) + 1)) (c : Zp (2 * m + 1 + 1)) :
    gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (mono (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (ring (2 * m + 1 + 1)).zero s) u) c = (ring (2 * r + 1 + 1)).mul s (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) u c) := by
  rw [gt_mul]
  have e : sumK (ring (2 * r + 1 + 1)) (fun a => (ring (2 * r + 1 + 1)).mul (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (mono (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (ring (2 * m + 1 + 1)).zero s) a) (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) u ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)))) (L (2 * m + 1 + 1))
      = sumK (ring (2 * r + 1 + 1)) (fun a => (ring (2 * r + 1 + 1)).mul (delta (ring (2 * r + 1 + 1)) a (ring (2 * m + 1 + 1)).zero) ((fun b => (ring (2 * r + 1 + 1)).mul s (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) u ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg b)))) a)) (L (2 * m + 1 + 1)) :=
    sumK_ext (ring (2 * r + 1 + 1)) _ _ (fun a => by
      rw [gt_mono]
      show (ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).mul s (delta (ring (2 * r + 1 + 1)) a (ring (2 * m + 1 + 1)).zero)) (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) u ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) = (ring (2 * r + 1 + 1)).mul (delta (ring (2 * r + 1 + 1)) a (ring (2 * m + 1 + 1)).zero) ((ring (2 * r + 1 + 1)).mul s (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)  -- (+66 chars; full line in the Zenodo source)
      lib_ring) _
  rw [e]
  refine (pick (ring (2 * r + 1 + 1)) _ (CMInert.complete_allZ (2 * m + 1 + 1) (ring (2 * r + 1 + 1))) _ (ring (2 * m + 1 + 1)).zero).trans ?_
  show (ring (2 * r + 1 + 1)).mul s (gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) u ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero))) = _
  rw [show (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero) = c by lib_ring]

theorem cast_chi_q (hq : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = 2 * r + 1 + 1 + 1) (x : Zp (2 * r + 1 + 1)) (hx : x ≠ (ring (2 * r + 1 + 1)).zero) : castZ (ring (2 * r + 1 + 1)) (chi r x) = pw (ring (2 * r + 1 + 1)) x (r + 1) := by
  cases Euler.euler_pm r hq x hx with
  | inl h => rw [chi_one_of r x hx h, h]; exact castZ_one' (ring (2 * r + 1 + 1))
  | inr h =>
    rw [chi_neg_of r x hx (by rw [h]; exact ne_pm r), h]
    show (ring (2 * r + 1 + 1)).neg (castN (ring (2 * r + 1 + 1)) 1) = _
    rw [show castN (ring (2 * r + 1 + 1)) 1 = (ring (2 * r + 1 + 1)).one from add_zero' (ring (2 * r + 1 + 1)) (ring (2 * r + 1 + 1)).one]

theorem chi_pm (n : Nat) (x : Zp (2 * n + 1 + 1)) (hx : x ≠ (ring (2 * n + 1 + 1)).zero) : chi n x = 1 ∨ chi n x = -1 := by
  by_cases h : pw (ring (2 * n + 1 + 1)) x (n + 1) = (ring (2 * n + 1 + 1)).one
  · exact Or.inl (chi_one_of n x hx h)
  · exact Or.inr (chi_neg_of n x hx h)

/-- **Quadratic reciprocity**: (q/p) = (p*/q), p* = (−1/p)·p, through the Gauss sum √p* ∈ ℤ[ζ_p]. -/
theorem qr_gauss (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hq : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = 2 * r + 1 + 1 + 1) (hQ : nsm (ring (2 * m + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hP : P m r ≠ (ring (2 * r + 1 + 1)).zero) :
    chi m (nsm (ring (2 * m + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one) = chi r (P m r) := by
  have hB : (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)).mul (Bv m r) (gq m r) = (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)).zero :=
    ext (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) _ _ (fun c => by
      show gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (Bv m r) (gq m r)) c = gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (tb (2 * m + 1 + 1) (fun _ => (ring (2 * r + 1 + 1)).zero)) c
      rw [gm_comm, gB_coef m r hpr, gt_tb])
  have key : gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (pw (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) (gq m r) (2 * r + 1 + 1 + 1)) (ring (2 * m + 1 + 1)).one = (ring (2 * r + 1 + 1)).mul (pw (ring (2 * r + 1 + 1)) (P m r) (r + 1)) (ring (2 * r + 1 + 1)).one := by
    rw [show 2 * r + 1 + 1 + 1 = 2 * (r + 1) + 1 by lib_nat, pw_odd (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1))]
    show gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) ((gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)).mul (pw (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) (gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gq m r) (gq m r)) (r + 1)) (gq m r)) (ring (2 * m + 1 + 1)).one = _
    rw [sq_split m r hpr]
    show gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) ((gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)).mul (pw (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) ((gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)).add (Av m r) ((gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)).neg (Bv m r))) (r + 1)) (gq m r)) (ring (2 * m + 1 + 1)).one = _
    rw [pow_kill (gring (ring (2 * r + 1 + 1)) (2 * m + 1 + 1)) _ _ _ hB]
    unfold Av
    rw [mono_pow, nsm_zero']
    show gt (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (gmul (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (mono (ring (2 * r + 1 + 1)) (2 * m + 1 + 1) (ring (2 * m + 1 + 1)).zero (pw (ring (2 * r + 1 + 1)) (P m r) (r + 1))) (gq m r)) (ring (2 * m + 1 + 1)).one = _
    rw [mono0_mul, gq_coef, chi_one m, castZ_one']
  rw [gauss_frob m r hpr hq hQ, gq_coef, chi_one m, castZ_one', (ring (2 * r + 1 + 1)).mul_comm, (ring (2 * r + 1 + 1)).one_mul, (ring (2 * r + 1 + 1)).mul_comm, (ring (2 * r + 1 + 1)).one_mul,
    ← cast_chi_q r hq _ hP] at key
  cases chi_pm m _ (fun e => hQ e) with
  | inl h1 =>
    cases chi_pm r _ hP with
    | inl h2 => rw [h1, h2]
    | inr h2 =>
      rw [h1, h2, castZ_one'] at key
      exact absurd key (fun e => GaussSplit.one_ne_negone (2 * r) (e.trans (by show (ring (2 * r + 1 + 1)).neg (castN (ring (2 * r + 1 + 1)) 1) = _; rw [show castN (ring (2 * r + 1 + 1)) 1 = (ring (2 * r + 1 + 1)).one from add_zero' (ring (2 * r + 1 + 1)) (ring (2 * r + 1 + 1)).one])))
  | inr h1 =>
    cases chi_pm r _ hP with
    | inr h2 => rw [h1, h2]
    | inl h2 =>
      rw [h1, h2, castZ_one'] at key
      exact absurd key.symm (fun e => GaussSplit.one_ne_negone (2 * r) (e.trans (by show (ring (2 * r + 1 + 1)).neg (castN (ring (2 * r + 1 + 1)) 1) = _; rw [show castN (ring (2 * r + 1 + 1)) 1 = (ring (2 * r + 1 + 1)).one from add_zero' (ring (2 * r + 1 + 1)) (ring (2 * r + 1 + 1)).one])))

end GaussQR

#print axioms GaussQR.castZ_one'
#print axioms GaussQR.gq_coef
#print axioms GaussQR.sq_coef
#print axioms GaussQR.gB_coef
#print axioms GaussQR.sq_split
#print axioms GaussQR.pow_kill
#print axioms GaussQR.pw_odd
#print axioms GaussQR.nsm_zero'
#print axioms GaussQR.mono0_mul
#print axioms GaussQR.cast_chi_q
#print axioms GaussQR.chi_pm
#print axioms GaussQR.qr_gauss

#print axioms: 12 theorems, no axioms.

78.50 Cubic Jacobi sum

In \(\mathbb Z[\omega ][C_p]\): \(g\star g=J\,\bar g\), \(\bar g\star \bar g=\bar J\,g\), \(g\star \bar g=p\zeta ^0-N\), so \(J\bar J=p\).

\[ J=\sum _{t\in \mathbb F_p}\chi (t)\chi (1-t)=a+b\omega ,\qquad a^2-ab+b^2=p. \]

lang/JacobiNorm.lean

/-
  lang/JacobiNorm — G2 (T43): the cubic Jacobi sum has norm p.

  p = 6n + 7, χ = chi3 w, χ̄ = chi3 w² (= χ², cb_eq), g = Σ χ(a) ζ^a, ḡ = Σ χ̄(a) ζ^a in ℤ[ω][C_p] (GroupRing, K = ℤ[ω]).
    g ⋆ g = J ḡ,  ḡ ⋆ ḡ = J̄ g,  g ⋆ ḡ = X := pζ⁰ − N,  X ⋆ X = p X                  (JacobiSum)
  so J J̄ X = (g ḡ)² = p X; at ζ⁰: (p − 1) J J̄ = (p − 1) p, hence
      jacobi_norm   J = a + bω  ⇒  a² − ab + b² = p
  (the cubic Jacobi sum is an element of norm p in ℤ[ω]: p = ππ̄ with π = J).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.JacobiSum
import lang.GroupRingFrob

namespace JacobiNorm

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum GroupRing GroupRingFrob

variable (n : Nat)

def cconj (z : Int × Int) : Int × Int := (z.1 - z.2, -z.2)

theorem w2_ne (_hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : (ring (6 * n + 5 + 1)).mul w w ≠ (ring (6 * n + 5 + 1)).one := ww_ne_one n w hw1 hw3

theorem w2_3 (w : Zp (6 * n + 5 + 1)) (_hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : pw (ring (6 * n + 5 + 1)) ((ring (6 * n + 5 + 1)).mul w w) 3 = (ring (6 * n + 5 + 1)).one := by
  have h3 : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) w = (ring (6 * n + 5 + 1)).one := by
    have : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w) w) w := rfl
    rw [this, (ring (6 * n + 5 + 1)).one_mul] at hw3; exact hw3
  show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).mul w w)) ((ring (6 * n + 5 + 1)).mul w w)) ((ring (6 * n + 5 + 1)).mul w w) = (ring (6 * n + 5 + 1)).one
  rw [show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).mul w w)) ((ring (6 * n + 5 + 1)).mul w w)) ((ring (6 * n + 5 + 1)).mul w w) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) w) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) w) by lib_ring, h3]
  lib_ring

theorem w4 (w : Zp (6 * n + 5 + 1)) (_hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).mul w w) = w := by
  have h3 : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) w = (ring (6 * n + 5 + 1)).one := by
    have : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w) w) w := rfl
    rw [this, (ring (6 * n + 5 + 1)).one_mul] at hw3; exact hw3
  rw [show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).mul w w) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) w) w by lib_ring, h3]; lib_ring

/-- χ² = χ_{w²}, and χ_{w²} = conj ∘ χ. -/
theorem cb_eq (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (x : Zp (6 * n + 5 + 1)) : cb n w x = chi3 n ((ring (6 * n + 5 + 1)).mul w w) x ∧ chi3 n ((ring (6 * n + 5 + 1)).mul w w) x = cconj (chi3 n w x) := by
  by_cases hx : x = (ring (6 * n + 5 + 1)).zero
  · rw [hx]; unfold cb; rw [chi3_zero, chi3_zero]; exact ⟨rfl, rfl⟩
  match exists_i n hpr w hw1 hw3 x hx with
  | ⟨i, hi, ei⟩ =>
    have v1 := val_of n hpr w hw1 hw3 x hx i hi ei
    unfold cb; rw [v1]
    have h3 : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul w w) w = (ring (6 * n + 5 + 1)).one := by
      have : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w) w) w := rfl
      rw [this, (ring (6 * n + 5 + 1)).one_mul] at hw3; exact hw3
    match i, hi with
    | 0, _ =>
      rw [val_of n hpr ((ring (6 * n + 5 + 1)).mul w w) (w2_ne n hpr w hw1 hw3) (w2_3 n w hw1 hw3) x hx 0 (Nat.zero_lt_succ 2) (ei.trans rfl)]; exact ⟨rfl, rfl⟩
    | 1, _ =>
      have e : pw (ring (6 * n + 5 + 1)) x (2 * n + 2) = pw (ring (6 * n + 5 + 1)) ((ring (6 * n + 5 + 1)).mul w w) 2 := by
        rw [ei]; show (ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).mul w w)) ((ring (6 * n + 5 + 1)).mul w w); rw [(ring (6 * n + 5 + 1)).one_mul, (ring (6 * n + 5 + 1)).one_mul, w4 n w hw1 hw3]
      rw [val_of n hpr ((ring (6 * n + 5 + 1)).mul w w) (w2_ne n hpr w hw1 hw3) (w2_3 n w hw1 hw3) x hx 2 (Nat.lt_succ_self 2) e]; exact ⟨rfl, rfl⟩
    | 2, _ =>
      have e : pw (ring (6 * n + 5 + 1)) x (2 * n + 2) = pw (ring (6 * n + 5 + 1)) ((ring (6 * n + 5 + 1)).mul w w) 1 := by
        rw [ei]; show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w) w = (ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).mul w w); rw [(ring (6 * n + 5 + 1)).one_mul, (ring (6 * n + 5 + 1)).one_mul]
      rw [val_of n hpr ((ring (6 * n + 5 + 1)).mul w w) (w2_ne n hpr w hw1 hw3) (w2_3 n w hw1 hw3) x hx 1 (Nat.succ_lt_succ (Nat.zero_lt_succ 1)) e]; exact ⟨rfl, rfl⟩
    | k + 3, hk => exact absurd hk (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) h))

theorem cconj_mul (x y : Int × Int) : cconj ((Cyclo3.cyc3 LibInt.intR).mul x y) = (Cyclo3.cyc3 LibInt.intR).mul (cconj x) (cconj y) := by
  apply Prod.ext
  · show (x.1 * y.1 + -(x.2 * y.2)) - (x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) = (x.1 - x.2) * (y.1 - y.2) + -(-x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) = (x.1 - x.2) * -y.2 + -x.2 * (y.1 - y.2) + -(-x.2 * -y.2)
    lib_int

theorem cconj_sum {A : Type} (f : A → Int × Int) : ∀ l : List A, cconj (sumK (Cyclo3.cyc3 LibInt.intR) f l) = sumK (Cyclo3.cyc3 LibInt.intR) (fun a => cconj (f a)) l
  | [] => rfl
  | a :: l => by
    show cconj ((Cyclo3.cyc3 LibInt.intR).add (f a) (sumK (Cyclo3.cyc3 LibInt.intR) f l)) = (Cyclo3.cyc3 LibInt.intR).add (cconj (f a)) (sumK (Cyclo3.cyc3 LibInt.intR) (fun a => cconj (f a)) l)
    rw [← cconj_sum f l]
    apply Prod.ext
    · show ((f a).1 + (sumK (Cyclo3.cyc3 LibInt.intR) f l).1) - ((f a).2 + (sumK (Cyclo3.cyc3 LibInt.intR) f l).2) = ((f a).1 - (f a).2) + ((sumK (Cyclo3.cyc3 LibInt.intR) f l).1 - (sumK (Cyclo3.cyc3 LibInt.intR) f l).2)
      lib_int
    · show -((f a).2 + (sumK (Cyclo3.cyc3 LibInt.intR) f l).2) = -(f a).2 + -(sumK (Cyclo3.cyc3 LibInt.intR) f l).2
      lib_int

theorem J2_conj (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : J n ((ring (6 * n + 5 + 1)).mul w w) = cconj (J n w) := by
  unfold J; rw [cconj_sum]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => by rw [cconj_mul, (cb_eq n hpr w hw1 hw3 t).2, (cb_eq n hpr w hw1 hw3 _).2]) _

theorem norm_form (a b : Int) : (Cyclo3.cyc3 LibInt.intR).mul (a, b) (cconj (a, b)) = (a * a - a * b + b * b, 0) := by
  apply Prod.ext
  · show a * (a - b) + -(b * -b) = a * a - a * b + b * b
    lib_int
  · show a * -b + b * (a - b) + -(b * -b) = 0
    lib_int

section Grp
variable {R : Type} (K : CRing R) (m : Nat)
theorem mono0_mulg (s : R) (u : V R (m + 1)) (c : Zp m) :
    gt K m (gmul K m (mono K m (ring m).zero s) u) c = K.mul s (gt K m u c) := by
  rw [gt_mul]
  have e : sumK K (fun a => K.mul (gt K m (mono K m (ring m).zero s) a) (gt K m u ((ring m).add c ((ring m).neg a)))) (GroupRing.L m)
      = sumK K (fun a => K.mul (delta K a (ring m).zero) (K.mul s (gt K m u ((ring m).add c ((ring m).neg a))))) (GroupRing.L m) :=
    sumK_ext K _ _ (fun a => by
      rw [gt_mono]
      show K.mul (K.mul s (delta K a (ring m).zero)) _ = _
      lib_ring) _
  rw [e]
  refine (pick K _ (CMInert.complete_allZ m K) (fun a => K.mul s (gt K m u ((ring m).add c ((ring m).neg a)))) (ring m).zero).trans ?_
  show K.mul s (gt K m u ((ring m).add c ((ring m).neg (ring m).zero))) = _
  rw [show (ring m).add c ((ring m).neg (ring m).zero) = c by lib_ring]
end Grp

def G1 (w : Zp (6 * n + 5 + 1)) : V (Int × Int) ((6 * n + 5 + 1) + 1) := tb (6 * n + 5 + 1) (chi3 n w)
def Nv : V (Int × Int) ((6 * n + 5 + 1) + 1) := tb (6 * n + 5 + 1) (fun _ => (Cyclo3.cyc3 LibInt.intR).one)
def Pe : Int × Int := (Int.ofNat (6 * n + 5 + 1 + 1), 0)
def Xv : V (Int × Int) ((6 * n + 5 + 1) + 1) := gadd (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) (gneg (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Nv n))

theorem conv_eq (w : Zp (6 * n + 5 + 1)) (f : Zp (6 * n + 5 + 1) → Int × Int) (c : Zp (6 * n + 5 + 1)) :
    gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (G1 n w) (tb (6 * n + 5 + 1) f)) c = sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (f ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n) := by
  rw [gt_mul]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by unfold G1; rw [gt_tb, gt_tb]) _

/-- g ⋆ g = J ḡ. -/
theorem sq1 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (G1 n w) (G1 n w) = gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).  -- (+56 chars; full line in the Zenodo source)
  ext (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) _ _ (fun c => by
    refine (conv_eq n w (chi3 n w) c).trans ?_
    rw [mono0_mulg]
    show _ = (Cyclo3.cyc3 LibInt.intR).mul (J n w) (gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (tb (6 * n + 5 + 1) (chi3 n ((ring (6 * n + 5 + 1)).mul w w))) c)
    rw [gt_tb]
    by_cases hc : c = (ring (6 * n + 5 + 1)).zero
    · rw [hc, S_zero n hpr w hw1 hw3, chi3_zero]; exact (mul_zero (Cyclo3.cyc3 LibInt.intR) _).symm
    · rw [S_ne n hpr w hw1 hw3 c hc, (cb_eq n hpr w hw1 hw3 c).1, (Cyclo3.cyc3 LibInt.intR).mul_comm])

theorem sq2 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (G1 n ((ring (6 * n + 5 + 1)).mul w w)) (G1 n ((ring (6 * n + 5 + 1)).mul w w)) = gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cy  -- (+121 chars; full line in the Zenodo source)
  have h := sq1 n hpr ((ring (6 * n + 5 + 1)).mul w w) (w2_ne n hpr w hw1 hw3) (w2_3 n w hw1 hw3)
  rw [w4 n w hw1 hw3] at h
  exact h

theorem neg_b (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (b : Zp (6 * n + 5 + 1)) : cb n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg b)) = cb n w b := by
  rw [show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg b) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) b by lib_ring, cb_mul n hpr w hw1 hw3]
  unfold cb; rw [chi3_neg1 n hpr w hw1 hw3]; lib_ring

/-- g ⋆ ḡ = pζ⁰ − N. -/
theorem gg (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (G1 n w) (G1 n ((ring (6 * n + 5 + 1)).mul w w)) = Xv n :=
  ext (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) _ _ (fun c => by
    refine (conv_eq n w (chi3 n ((ring (6 * n + 5 + 1)).mul w w)) c).trans ?_
    have e1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n)
        = sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).add c b)) (cb n w b)) (LL n) := by
      refine (GroupRing.shift (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) c).symm.trans ?_
      exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun b => by
        show (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).add b c)) (chi3 n ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add b c)))) = _
        rw [← (cb_eq n hpr w hw1 hw3 _).1, show (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add b c)) = (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg b) by lib_ring, neg_b n hpr w hw1 hw3,
          (ring (6 * n + 5 + 1)).add_comm b c]) _
    rw [e1]
    unfold Xv gadd gneg Nv
    rw [gt_tb, gt_mono, gt_tb]
    by_cases hc : c = (ring (6 * n + 5 + 1)).zero
    · rw [hc, show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero = fun b => (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero b from rfl]
      have e2 : sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero b)) (cb n w b)) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w b) (cb n w b)) (LL n) :=
        sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun b => by rw [(ring (6 * n + 5 + 1)).zero_add]) _
      rw [e2, D_zero n hpr w hw1 hw3]
      unfold delta; rw [ite_pos' _ _ rfl, (Cyclo3.cyc3 LibInt.intR).mul_comm, (Cyclo3.cyc3 LibInt.intR).one_mul]
      unfold Pe
      exact Prod.ext (by show Int.ofNat (6 * n + 5 + 1 + 1) - 1 = Int.ofNat (6 * n + 5 + 1 + 1) + -1; rfl) (by show (0 : Int) = 0 + -0; rfl)
    · rw [D_ne n hpr w hw1 hw3 c hc]
      unfold delta; rw [ite_neg' _ _ hc]
      rw [gt_tb]
      lib_ring)

theorem NN : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Nv n) (Nv n) = gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) (Nv n) :=
  ext (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) _ _ (fun c => by
    rw [gt_mul, mono0_mulg]
    unfold Nv
    rw [gt_tb]
    have e : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (tb (6 * n + 5 + 1) (fun _ => (Cyclo3.cyc3 LibInt.intR).one)) a) (gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (tb (6 * n + 5 + 1) (fun _ => (Cyclo3.cyc3 LibInt.intR).one)) ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (GroupRing.L (6 * n + 5 + 1))
        = sumK (Cyclo3.cyc3 LibInt.intR) (fun _ => (Cyclo3.cyc3 LibInt.intR).one) (CMInert.allZ (6 * n + 5 + 1) ((6 * n + 5 + 1) + 1)) :=
      sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by rw [gt_tb, gt_tb]; exact (Cyclo3.cyc3 LibInt.intR).one_mul _) _
    rw [e, sum_one, (Cyclo3.cyc3 LibInt.intR).mul_comm, (Cyclo3.cyc3 LibInt.intR).one_mul]
    rfl)

theorem XX : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Xv n) (Xv n) = gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) (Xv n) := by
  have hN := NN n
  have hc := gm_comm (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Nv n) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n))
  show (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).neg (Nv n))) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n  -- (+66 chars; full line in the Zenodo source)
    = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).neg (Nv n)))
  have hN' : (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (Nv n) (Nv n) = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) (Nv n) := hN
  rw [show (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).neg (Nv n))) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (  -- (+70 chars; full line in the Zenodo source)
      = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).neg (Nv n  -- (+4 chars; full line in the Zenodo source)
          ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).add ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).neg ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) (Nv n))) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (Nv n) (Nv n))) by lib_ring, hN']
  lib_ring

theorem int_cancel (u v : Int) (k : Nat) (h : u * Int.ofNat (k + 1) = v * Int.ofNat (k + 1)) : u = v := by
  have h0 : (u - v) * Int.ofNat (k + 1) = 0 := by rw [show (u - v) * Int.ofNat (k + 1) = u * Int.ofNat (k + 1) - v * Int.ofNat (k + 1) by lib_int, h]; lib_int
  cases LibInt.int_noZD _ _ h0 with
  | inl h1 => calc u = (u - v) + v := by lib_int
      _ = 0 + v := by rw [h1]; rfl
      _ = v := by lib_int
  | inr h1 => exact absurd (Int.ofNat.inj h1) (fun e => Nat.noConfusion e)

/-- **N(J) = p**: the cubic Jacobi sum J = a + bω has a² − ab + b² = p. -/
theorem jacobi_norm (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : (J n w).1 * (J n w).1 - (J n w).1 * (J n w).2 + (J n w).2 * (J n w).2 = Int.ofNat (6 * n + 5 + 1 + 1) := by
  have h1 := sq1 n hpr w hw1 hw3
  have h2 := sq2 n hpr w hw1 hw3
  have h3 := gg n hpr w hw1 hw3
  have key : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero ((Cyclo3.cyc3 LibInt.intR).mul (J n w) (J n ((ring (6 * n + 5 + 1)).mul w w)))) (Xv n) = gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (Pe n)) (Xv n) := by
    rw [← XX n, ← h3]
    have hm : gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n w)) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n ((ring (6 * n + 5 + 1)).mul w w))) = mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero ((Cyclo3.cyc3 LibInt.intR).mul (J n w) (J n ((ring (6 * n + 5  -- (+23 chars; full line in the Zenodo source)
      rw [mono_mul, show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero (ring (6 * n + 5 + 1)).zero = (ring (6 * n + 5 + 1)).zero by lib_ring]
    rw [← hm]
    show (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n w)) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n ((ring (6 * n + 5 + 1)).mul w w)))) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n ((ring (6   -- (+24 chars; full line in the Zenodo source)
      = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n ((ring (6 * n + 5 + 1)).mul w w))) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n ((ring (6 * n + 5 + 1)).mul w w)))
    have h1' : (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n w) = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n w)) (G1 n ((ring (6 * n + 5 + 1)).mul w w)) := h1
    have h2' : (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n ((ring (6 * n + 5 + 1)).mul w w)) (G1 n ((ring (6 * n + 5 + 1)).mul w w)) = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n ((ring (6 * n + 5 + 1)).mul w w))) (G1 n w) := h2
    rw [show (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n ((ring (6 * n + 5 + 1)).mul w w))) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n ((ring (6 * n + 5 + 1)).mul w w)))
        = (gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n w) (G1 n w)) ((gring (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1)).mul (G1 n ((ring (6 * n + 5 + 1)).mul w w)) (G1 n ((ring (6 * n + 5 + 1)).mul w w))) by lib_ring, h1', h2']
    lib_ring
  have k0 := congrArg (fun u => gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) u (ring (6 * n + 5 + 1)).zero) key
  have k1 : (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (J n w) (J n ((ring (6 * n + 5 + 1)).mul w w))) (gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Xv n) (ring (6 * n + 5 + 1)).zero) = (Cyclo3.cyc3 LibInt.intR).mul (Pe n) (gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Xv n) (ring (6 * n + 5 + 1)).zero) := by
    have := k0
    rw [mono0_mulg, mono0_mulg] at this
    exact this
  have hX0 : gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Xv n) (ring (6 * n + 5 + 1)).zero = (Int.ofNat (6 * n + 5 + 1), 0) := by
    unfold Xv gadd gneg Nv
    rw [gt_tb, gt_mono, gt_tb]
    unfold delta; rw [ite_pos' _ _ rfl, (Cyclo3.cyc3 LibInt.intR).mul_comm, (Cyclo3.cyc3 LibInt.intR).one_mul]
    unfold Pe
    exact Prod.ext (by show Int.ofNat (6 * n + 5 + 1) + 1 + -1 = Int.ofNat (6 * n + 5 + 1); lib_int) (by show (0 : Int) + -0 = 0; rfl)
  have hN : (Cyclo3.cyc3 LibInt.intR).mul (J n w) (J n ((ring (6 * n + 5 + 1)).mul w w)) = ((J n w).1 * (J n w).1 - (J n w).1 * (J n w).2 + (J n w).2 * (J n w).2, 0) := by
    rw [J2_conj n hpr w hw1 hw3]
    apply Prod.ext
    · show (J n w).1 * ((J n w).1 - (J n w).2) + -((J n w).2 * -(J n w).2) = (J n w).1 * (J n w).1 - (J n w).1 * (J n w).2 + (J n w).2 * (J n w).2
      lib_int
    · show (J n w).1 * -(J n w).2 + (J n w).2 * ((J n w).1 - (J n w).2) + -((J n w).2 * -(J n w).2) = 0
      lib_int
  rw [hX0, hN] at k1
  have k2 := congrArg Prod.fst k1
  change _ * Int.ofNat (6 * n + 5 + 1) + -(0 * 0) = Int.ofNat (6 * n + 5 + 1 + 1) * Int.ofNat (6 * n + 5 + 1) + -(0 * 0) at k2
  apply int_cancel _ _ (6 * n + 5)
  rw [show ((J n w).1 * (J n w).1 - (J n w).1 * (J n w).2 + (J n w).2 * (J n w).2) * Int.ofNat (6 * n + 5 + 1)
      = ((J n w).1 * (J n w).1 - (J n w).1 * (J n w).2 + (J n w).2 * (J n w).2) * Int.ofNat (6 * n + 5 + 1) + -(0 * 0) by lib_int, k2]
  lib_int

end JacobiNorm

#print axioms JacobiNorm.cconj
#print axioms JacobiNorm.w2_ne
#print axioms JacobiNorm.w2_3
#print axioms JacobiNorm.w4
#print axioms JacobiNorm.cb_eq
#print axioms JacobiNorm.cconj_mul
#print axioms JacobiNorm.cconj_sum
#print axioms JacobiNorm.J2_conj
#print axioms JacobiNorm.norm_form
#print axioms JacobiNorm.mono0_mulg
#print axioms JacobiNorm.conv_eq
#print axioms JacobiNorm.sq1
#print axioms JacobiNorm.sq2
#print axioms JacobiNorm.neg_b
#print axioms JacobiNorm.gg
#print axioms JacobiNorm.NN
#print axioms JacobiNorm.XX
#print axioms JacobiNorm.int_cancel
#print axioms JacobiNorm.jacobi_norm

#print axioms: 19 theorems, no axioms.

78.51 Cubic reciprocity at inert primes

In \(\mathbb F_q[\omega ][C_p]\), \(q=3k+2\): \(g^{q+1}=\chi (q)\,\bar g g=\chi (q)X\) and \(g^{q+1}=(JX)^{k+1}=J^{k+1}p^kX\), \(X=p\zeta ^0-N\).

\[ \chi _p(q)=J^{k+1}p^k\quad \text {in }\mathbb F_q[\omega ]. \]

lang/CubicRecip.lean

/-
  lang/CubicRecip — G2 (T43): cubic reciprocity at an inert prime q ≡ 2 (mod 3).

  p = 6n + 7, χ the cubic character of 𝔽_p (CubicChar), J = J(χ, χ) the primary prime of ℤ[ω] above p (JacobiNorm, JacobiPrimary),
  q = 3k + 2 prime, q ≠ p. In 𝔽_q[ω][C_p] (Cyclo3 over 𝔽_q, GroupRing), with φ the reduction ℤ[ω] → 𝔽_q[ω]:
    g^q = Σ χ(a)^q ζ^{qa} = Σ χ̄(a) ζ^{qa} = χ(q) ḡ                 (ω^q = ω², Frobenius additive, reindex a ↦ qa)
    g^{q+1} = χ(q) ḡ g = χ(q) X,   X = pζ⁰ − N
    g^{q+1} = (g³)^{k+1} = (J X)^{k+1} = J^{k+1} p^k X          (X² = pX)
  and the ζ¹ coefficient of X is −1, so
      cubic_recip_inert    χ_p(q) = J^{k+1} p^k  in 𝔽_q[ω]
  i.e. the cubic residue character of q modulo the primary prime J is read off from J modulo q (with p = J J̄,
  J^{k+1} p^k = J^{k+1}(J J̄)^k ≡ J^{(q²−1)/3}: Eisenstein's cubic reciprocity for a rational prime q ≡ 2 (mod 3)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.JacobiPrimary

namespace CubicRecip

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm GroupRing GroupRingFrob
open LibRing (castZ castN)

variable (n k : Nat)

section Gen
variable {R : Type} (T : CRing R)

def ph (z : Int × Int) : R × R := (castZ T z.1, castZ T z.2)

theorem ph_add (x y : Int × Int) : ph T ((Cyclo3.cyc3 LibInt.intR).add x y) = (Cyclo3.cyc3 T).add (ph T x) (ph T y) := by
  apply Prod.ext
  · exact IntCast.castZ_add T x.1 y.1
  · exact IntCast.castZ_add T x.2 y.2

theorem ph_mul (x y : Int × Int) : ph T ((Cyclo3.cyc3 LibInt.intR).mul x y) = (Cyclo3.cyc3 T).mul (ph T x) (ph T y) := by
  apply Prod.ext
  · show castZ T (x.1 * y.1 + -(x.2 * y.2)) = T.add (T.mul (castZ T x.1) (castZ T y.1)) (T.neg (T.mul (castZ T x.2) (castZ T y.2)))
    rw [IntCast.castZ_add, HasseDavenport.castZ_neg, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]
  · show castZ T (x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) = T.add (T.add (T.mul (castZ T x.1) (castZ T y.2)) (T.mul (castZ T x.2) (castZ T y.1))) (T.neg (T.mul (castZ T x.2) (castZ T y.2)))
    rw [IntCast.castZ_add, IntCast.castZ_add, HasseDavenport.castZ_neg, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]

theorem ph_neg (x : Int × Int) : ph T ((Cyclo3.cyc3 LibInt.intR).neg x) = (Cyclo3.cyc3 T).neg (ph T x) := by
  apply Prod.ext
  · exact HasseDavenport.castZ_neg T x.1
  · exact HasseDavenport.castZ_neg T x.2

theorem ph_one : ph T (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 T).one := by
  apply Prod.ext
  · show castN T 1 = T.one
    exact add_zero' T _
  · rfl

theorem ph_zero : ph T (Cyclo3.cyc3 LibInt.intR).zero = (Cyclo3.cyc3 T).zero := rfl

theorem ph_sum {A : Type} (f : A → Int × Int) : ∀ l : List A, ph T (sumK (Cyclo3.cyc3 LibInt.intR) f l) = sumK (Cyclo3.cyc3 T) (fun a => ph T (f a)) l
  | [] => rfl
  | a :: l => by
    show ph T ((Cyclo3.cyc3 LibInt.intR).add (f a) (sumK _ f l)) = (Cyclo3.cyc3 T).add (ph T (f a)) (sumK (Cyclo3.cyc3 T) (fun a => ph T (f a)) l)
    rw [ph_add, ph_sum f l]

theorem ph_delta {G : Type} [DecidableEq G] (x y : G) : ph T (delta (Cyclo3.cyc3 LibInt.intR) x y) = delta (Cyclo3.cyc3 T) x y := by
  unfold delta
  by_cases h : x = y
  · rw [ite_pos' _ _ h, ite_pos' _ _ h]; exact ph_one T
  · rw [ite_neg' _ _ h, ite_neg' _ _ h]; exact ph_zero T

/-- (S Y)^{m+1} = S^{m+1} P^m Y when Y² = P Y. -/
theorem pow_SY {S' : Type} (G : CRing S') (S P Y : S') (h : G.mul Y Y = G.mul P Y) : ∀ m,
    pw G (G.mul S Y) (m + 1) = G.mul (G.mul (pw G S (m + 1)) (pw G P m)) Y
  | 0 => by show G.mul G.one (G.mul S Y) = G.mul (G.mul (G.mul G.one S) G.one) Y; lib_ring
  | m + 1 => by
    show G.mul (pw G (G.mul S Y) (m + 1)) (G.mul S Y) = G.mul (G.mul (G.mul (pw G S (m + 1)) S) (G.mul (pw G P m) P)) Y
    rw [pow_SY G S P Y h m]
    rw [show G.mul (G.mul (G.mul (pw G S (m + 1)) (pw G P m)) Y) (G.mul S Y) = G.mul (G.mul (G.mul (pw G S (m + 1)) (pw G P m)) S) (G.mul Y Y) by lib_ring, h]
    lib_ring

theorem pw_mul_split {S' : Type} (G : CRing S') (x : S') (a b : Nat) : pw G x (a * b) = pw G (pw G x a) b := (TwoPower.pwpow G x a b).symm
end Gen

def mp (u : V (Int × Int) ((6 * n + 5 + 1) + 1)) : V (Zp (3 * k + 1) × Zp (3 * k + 1)) ((6 * n + 5 + 1) + 1) := tb (6 * n + 5 + 1) (fun a => ph (ring (3 * k + 1)) (gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) u a))

theorem mp_mul (u v : V (Int × Int) ((6 * n + 5 + 1) + 1)) : gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (mp n k u) (mp n k v) = mp n k (gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) u v) :=
  ext (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) _ _ (fun c => by
    unfold mp
    rw [gt_tb, gt_mul, gt_mul, ph_sum]
    exact sumK_ext (Cyclo3.cyc3 (ring (3 * k + 1))) _ _ (fun a => by rw [gt_tb, gt_tb, ph_mul]) _)

theorem mp_mono (s : Int × Int) (a : Zp (6 * n + 5 + 1)) : mp n k (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) a s) = mono (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) a (ph (ring (3 * k + 1)) s) :=
  ext (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) _ _ (fun c => by unfold mp; rw [gt_tb, gt_mono, gt_mono, ph_mul, ph_delta])

theorem charq : nsm (Cyclo3.cyc3 (ring (3 * k + 1))) (3 * k + 1 + 1) (Cyclo3.cyc3 (ring (3 * k + 1))).one = (Cyclo3.cyc3 (ring (3 * k + 1))).zero := by
  have h := char_p (3 * k + 1)
  have hn : ∀ m, nsm (Cyclo3.cyc3 (ring (3 * k + 1))) m (Cyclo3.cyc3 (ring (3 * k + 1))).one = (nsm (ring (3 * k + 1)) m (ring (3 * k + 1)).one, nsm (ring (3 * k + 1)) m (ring (3 * k + 1)).zero) := fun m => by
    induction m with
    | zero => rfl
    | succ m ih => show (Cyclo3.cyc3 (ring (3 * k + 1))).add (nsm (Cyclo3.cyc3 (ring (3 * k + 1))) m (Cyclo3.cyc3 (ring (3 * k + 1))).one) (Cyclo3.cyc3 (ring (3 * k + 1))).one = _; rw [ih]; rfl
  rw [hn, h]
  apply Prod.ext
  · rfl
  · show nsm (ring (3 * k + 1)) (3 * k + 1 + 1) (ring (3 * k + 1)).zero = (ring (3 * k + 1)).zero
    rw [nsm_eq_mul, mul_zero]

theorem om_pow (z : Zp (3 * k + 1) × Zp (3 * k + 1)) (h3 : pw (Cyclo3.cyc3 (ring (3 * k + 1))) z 3 = (Cyclo3.cyc3 (ring (3 * k + 1))).one) : ∀ m, pw (Cyclo3.cyc3 (ring (3 * k + 1))) z (3 * m + 2) = (Cyclo3.cyc3 (ring (3 * k + 1))).mul z z
  | 0 => by show (Cyclo3.cyc3 (ring (3 * k + 1))).mul ((Cyclo3.cyc3 (ring (3 * k + 1))).mul (Cyclo3.cyc3 (ring (3 * k + 1))).one z) z = _; rw [(Cyclo3.cyc3 (ring (3 * k + 1))).one_mul]
  | m + 1 => by
    rw [show 3 * (m + 1) + 2 = (3 * m + 2) + 3 by lib_nat, HasseCM.pw_add', om_pow z h3 m, h3]; lib_ring

theorem coefK (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (Q : Zp (6 * n + 5 + 1)) (hQ : Q ≠ (ring (6 * n + 5 + 1)).zero) (s : Zp (6 * n + 5 + 1) → Zp (3 * k + 1) × Zp (3 * k + 1)) (c : Zp (6 * n + 5 + 1)) :
    sumK (Cyclo3.cyc3 (ring (3 * k + 1))) (fun a => (Cyclo3.cyc3 (ring (3 * k + 1))).mul (s a) (delta (Cyclo3.cyc3 (ring (3 * k + 1))) c ((ring (6 * n + 5 + 1)).mul Q a))) (GroupRing.L (6 * n + 5 + 1)) = s ((ring (6 * n + 5 + 1)).mul (iv n Q) c) := by
  have hQQ := iv_mul n hpr Q hQ
  have e := reindex (Cyclo3.cyc3 (ring (3 * k + 1))) _ (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 (ring (3 * k + 1)))) (fun a => (ring (6 * n + 5 + 1)).mul Q a) (fun b => (ring (6 * n + 5 + 1)).mul (iv n Q) b)
    (fun x => by rw [show (ring (6 * n + 5 + 1)).mul (iv n Q) ((ring (6 * n + 5 + 1)).mul Q x) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (iv n Q) Q) x by lib_ring, hQQ]; lib_ring)
    (fun y => by rw [show (ring (6 * n + 5 + 1)).mul Q ((ring (6 * n + 5 + 1)).mul (iv n Q) y) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (iv n Q) Q) y by lib_ring, hQQ]; lib_ring)
    (fun b => (Cyclo3.cyc3 (ring (3 * k + 1))).mul (delta (Cyclo3.cyc3 (ring (3 * k + 1))) b c) (s ((ring (6 * n + 5 + 1)).mul (iv n Q) b)))
  have e2 : sumK (Cyclo3.cyc3 (ring (3 * k + 1))) (fun a => (Cyclo3.cyc3 (ring (3 * k + 1))).mul (s a) (delta (Cyclo3.cyc3 (ring (3 * k + 1))) c ((ring (6 * n + 5 + 1)).mul Q a))) (GroupRing.L (6 * n + 5 + 1))
      = sumK (Cyclo3.cyc3 (ring (3 * k + 1))) (fun a => (fun b => (Cyclo3.cyc3 (ring (3 * k + 1))).mul (delta (Cyclo3.cyc3 (ring (3 * k + 1))) b c) (s ((ring (6 * n + 5 + 1)).mul (iv n Q) b))) ((ring (6 * n + 5 + 1)).mul Q a)) (GroupRing.L (6 * n + 5 + 1)) :=
    sumK_ext (Cyclo3.cyc3 (ring (3 * k + 1))) _ _ (fun a => by
      show _ = (Cyclo3.cyc3 (ring (3 * k + 1))).mul (delta (Cyclo3.cyc3 (ring (3 * k + 1))) ((ring (6 * n + 5 + 1)).mul Q a) c) (s ((ring (6 * n + 5 + 1)).mul (iv n Q) ((ring (6 * n + 5 + 1)).mul Q a)))
      rw [show (ring (6 * n + 5 + 1)).mul (iv n Q) ((ring (6 * n + 5 + 1)).mul Q a) = a by rw [show (ring (6 * n + 5 + 1)).mul (iv n Q) ((ring (6 * n + 5 + 1)).mul Q a) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (iv n Q) Q) a by lib_ring, hQQ]; lib_ring,
        delta_symm, (Cyclo3.cyc3 (ring (3 * k + 1))).mul_comm]) _
  rw [e2]
  exact e.trans (pick (Cyclo3.cyc3 (ring (3 * k + 1))) _ (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 (ring (3 * k + 1)))) _ c)

theorem ph_pow (z : Int × Int) : ∀ m, pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) z) m = ph (ring (3 * k + 1)) (pw (Cyclo3.cyc3 LibInt.intR) z m)
  | 0 => (ph_one (ring (3 * k + 1))).symm
  | m + 1 => by show (Cyclo3.cyc3 (ring (3 * k + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) z) m) (ph (ring (3 * k + 1)) z) = ph (ring (3 * k + 1)) ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z m) z); rw [ph_pow z m, ph_mul]

theorem nsm_zero'' {S' : Type} (G' : CRing S') : ∀ m, nsm G' m G'.zero = G'.zero
  | 0 => rfl
  | m + 1 => by show G'.add (nsm G' m G'.zero) G'.zero = G'.zero; rw [nsm_zero'' G' m]; exact add_zero' G' _

theorem mono0_pow {S' : Type} (K' : CRing S') (m' : Nat) (s : S') (j : Nat) :
    pw (gring K' m') (mono K' m' (ring m').zero s) j = mono K' m' (ring m').zero (pw K' s j) := by
  rw [mono_pow, nsm_zero'']

theorem mono0_mul2 {S' : Type} (K' : CRing S') (m' : Nat) (s t : S') :
    gmul K' m' (mono K' m' (ring m').zero s) (mono K' m' (ring m').zero t) = mono K' m' (ring m').zero (K'.mul s t) := by
  rw [mono_mul, show (ring m').add (ring m').zero (ring m').zero = (ring m').zero by lib_ring]

/-- (φχ(x))^q = φχ̄(x). -/
theorem frob_val (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (x : Zp (6 * n + 5 + 1)) : pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (chi3 n w x)) (3 * k + 1 + 1) = ph (ring (3 * k + 1)) (cb n w x) := by
  by_cases hx : x = (ring (6 * n + 5 + 1)).zero
  · rw [hx]; unfold cb; rw [chi3_zero]
    show (Cyclo3.cyc3 (ring (3 * k + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (Cyclo3.cyc3 LibInt.intR).zero) (3 * k + 1)) (ph (ring (3 * k + 1)) (Cyclo3.cyc3 LibInt.intR).zero) = ph (ring (3 * k + 1)) ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (Cyclo3.cyc3 LibInt.intR).zero)
    rw [ph_zero, mul_zero, zero_mul']; rfl
  · have h3 : pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (chi3 n w x)) 3 = (Cyclo3.cyc3 (ring (3 * k + 1))).one := by
      rw [ph_pow]
      match exists_i n hpr w hw1 hw3 x hx with
      | ⟨i, hi, ei⟩ =>
        rw [val_of n hpr w hw1 hw3 x hx i hi ei]
        match i, hi with
        | 0, _ => exact ph_one (ring (3 * k + 1))
        | 1, _ => exact ph_one (ring (3 * k + 1))
        | 2, _ => exact ph_one (ring (3 * k + 1))
        | j + 3, hj => exact absurd hj (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 j) h))
    rw [show 3 * k + 1 + 1 = 3 * k + 2 by lib_nat, om_pow k _ h3 k]
    unfold cb; rw [ph_mul]

theorem cb_iv (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (x : Zp (6 * n + 5 + 1)) (hx : x ≠ (ring (6 * n + 5 + 1)).zero) : cb n w (iv n x) = chi3 n w x := by
  have hix : iv n x ≠ (ring (6 * n + 5 + 1)).zero := fun e => by
    have := iv_mul n hpr x hx
    rw [e, zero_mul'] at this
    exact one_ne_zero'' n this.symm
  rw [← chi_iv n hpr w hw1 hw3 (iv n x) hix, iv_iv n hpr]

/-- **Cubic reciprocity at an inert q = 3k + 2**: χ_p(q) = J^{k+1} p^k in 𝔽_q[ω]. -/
theorem cubic_recip_inert (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (hq : ∀ d, Dv d (3 * k + 1 + 1) → d = 1 ∨ d = 3 * k + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (hQ : nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero) :
    ph (ring (3 * k + 1)) (chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one)) = (Cyclo3.cyc3 (ring (3 * k + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (J n w)) (k + 1)) (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (Pe n)) k) := by
  -- g^q = χ(q) ḡ
  have hfrob : pw (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) (mp n k (G1 n w)) (3 * k + 1 + 1) = gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (ph (ring (3 * k + 1)) (chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one)))) (mp n k (G1 n ((ring (6 * n + 5 + 1)).mul w   -- (+7 chars; full line in the Zenodo source)
    ext (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) _ _ (fun c => by
      have hd := decomp (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (mp n k (G1 n w))
      refine (congrArg (fun u => gt (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (pw (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) u (3 * k + 1 + 1)) c) hd).trans ?_
      show gt (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (pw (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) (sumK (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) _ (GroupRing.L (6 * n + 5 + 1))) (3 * k + 1 + 1)) c = _
      rw [frob_sum (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (3 * k) hq (charq k), gt_sum, mono0_mulg]
      have e2 : sumK (Cyclo3.cyc3 (ring (3 * k + 1))) (fun a => gt (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (pw (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) (mono (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) a (gt (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (mp n k (G1 n w)) a)) (3 * k + 1 + 1)) c) (GroupRing.L (6 * n + 5 + 1))
          = sumK (Cyclo3.cyc3 (ring (3 * k + 1))) (fun a => (Cyclo3.cyc3 (ring (3 * k + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (chi3 n w a)) (3 * k + 1 + 1)) (delta (Cyclo3.cyc3 (ring (3 * k + 1))) c ((ring (6 * n + 5 + 1)).mul (nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one) a))) (GroupRing.L (6 * n + 5 + 1)) :=
        sumK_ext (Cyclo3.cyc3 (ring (3 * k + 1))) _ _ (fun a => by
          rw [mono_pow, gt_mono, nsm_eq_mul]
          unfold mp G1; rw [gt_tb, gt_tb]) _
      rw [e2, coefK n k hpr _ hQ (fun a => pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (chi3 n w a)) (3 * k + 1 + 1)) c, frob_val n k hpr w hw1 hw3, cb_mul n hpr w hw1 hw3, cb_iv n hpr w hw1 hw3 _ hQ,
        ph_mul, (cb_eq n hpr w hw1 hw3 c).1]
      unfold mp G1; rw [gt_tb, gt_tb])
  -- g^{q+1} = χ(q) X
  have hY : gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (mp n k (G1 n ((ring (6 * n + 5 + 1)).mul w w))) (mp n k (G1 n w)) = mp n k (Xv n) := by
    rw [mp_mul, gm_comm, gg n hpr w hw1 hw3]
  have L1 : pw (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) (mp n k (G1 n w)) (3 * k + 1 + 1 + 1) = gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (ph (ring (3 * k + 1)) (chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one)))) (mp n k (Xv n)) := by
    show gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (pw (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) (mp n k (G1 n w)) (3 * k + 1 + 1)) (mp n k (G1 n w)) = _
    rw [hfrob, gm_assoc, hY]
  -- g^{q+1} = (g³)^{k+1} = J^{k+1} p^k X
  have hg3 : pw (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) (mp n k (G1 n w)) 3 = gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (ph (ring (3 * k + 1)) (J n w))) (mp n k (Xv n)) := by
    show gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (gone (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) (mp n k (G1 n w))) (mp n k (G1 n w))) (mp n k (G1 n w)) = _
    rw [gm_one, mp_mul, mp_mul, sq1 n hpr w hw1 hw3,
      show gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n w)) (G1 n ((ring (6 * n + 5 + 1)).mul w w))) (G1 n w)
        = gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (J n w)) (gmul (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (G1 n w) (G1 n ((ring (6 * n + 5 + 1)).mul w w))) by
          rw [gm_assoc, gm_comm (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (G1 n ((ring (6 * n + 5 + 1)).mul w w))],
      gg n hpr w hw1 hw3, ← mp_mul, mp_mono]
  have hYY : (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)).mul (mp n k (Xv n)) (mp n k (Xv n)) = (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (ph (ring (3 * k + 1)) (Pe n))) (mp n k (Xv n)) := by
    show gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) _ _ = gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) _ _
    rw [mp_mul, XX, ← mp_mul, mp_mono]
  have L2 : pw (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) (mp n k (G1 n w)) (3 * k + 1 + 1 + 1) = gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero ((Cyclo3.cyc3 (ring (3 * k + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (J n w)) (k + 1)) (pw (Cyclo3.cyc3 (ring (3 * k + 1))  -- (+59 chars; full line in the Zenodo source)
    rw [show 3 * k + 1 + 1 + 1 = 3 * (k + 1) by lib_nat, pw_mul_split, hg3]
    rw [show gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (mono (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (ph (ring (3 * k + 1)) (J n w))) (mp n k (Xv n)) = (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)).mul (mono (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (ph (ring (3 * k + 1)) (J n w))) (mp n k (Xv n)) from r  -- (+3 chars; full line in the Zenodo source)
      pow_SY (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) _ _ _ hYY k]
    show gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (gmul (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (pw (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) (mono (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (ring (6 * n + 5 + 1)).zero (ph (ring (3 * k + 1)) (J n w))) (k + 1)) (pw (gring (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1)) (mono (Cyclo3.cyc3 (ring (3 * k + 1  -- (+103 chars; full line in the Zenodo source)
    rw [mono0_pow, mono0_pow, mono0_mul2]
  have key := L1.symm.trans L2
  have k1 := congrArg (fun u => gt (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) u (ring (6 * n + 5 + 1)).one) key
  have k2 : (Cyclo3.cyc3 (ring (3 * k + 1))).mul (ph (ring (3 * k + 1)) (chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one))) (gt (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (mp n k (Xv n)) (ring (6 * n + 5 + 1)).one)
      = (Cyclo3.cyc3 (ring (3 * k + 1))).mul ((Cyclo3.cyc3 (ring (3 * k + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (J n w)) (k + 1)) (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (Pe n)) k)) (gt (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (mp n k (Xv n)) (ring (6 * n + 5 + 1)).one) := by
    have := k1
    rw [mono0_mulg, mono0_mulg] at this
    exact this
  have hX1 : gt (Cyclo3.cyc3 (ring (3 * k + 1))) (6 * n + 5 + 1) (mp n k (Xv n)) (ring (6 * n + 5 + 1)).one = (Cyclo3.cyc3 (ring (3 * k + 1))).neg (Cyclo3.cyc3 (ring (3 * k + 1))).one := by
    unfold mp; rw [gt_tb]
    have : gt (Cyclo3.cyc3 LibInt.intR) (6 * n + 5 + 1) (Xv n) (ring (6 * n + 5 + 1)).one = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by
      unfold Xv gadd gneg Nv
      rw [gt_tb, gt_mono, gt_tb, gt_tb]
      unfold delta; rw [ite_neg' _ _ (one_ne_zero'' n)]
      lib_ring
    rw [this, ph_neg, ph_one]
  rw [hX1] at k2
  calc ph (ring (3 * k + 1)) (chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one))
      = (Cyclo3.cyc3 (ring (3 * k + 1))).mul ((Cyclo3.cyc3 (ring (3 * k + 1))).mul (ph (ring (3 * k + 1)) (chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one))) ((Cyclo3.cyc3 (ring (3 * k + 1))).neg (Cyclo3.cyc3 (ring (3 * k + 1))).one)) ((Cyclo3.cyc3 (ring (3 * k + 1))).neg (Cyclo3.cyc3 (ring (3 * k + 1))).one) := by lib_ring
    _ = (Cyclo3.cyc3 (ring (3 * k + 1))).mul ((Cyclo3.cyc3 (ring (3 * k + 1))).mul ((Cyclo3.cyc3 (ring (3 * k + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (J n w)) (k + 1)) (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (Pe n)) k)) ((Cyclo3.cyc3 (ring (3 * k + 1))).neg (Cyclo3.cyc3 (ring (3 * k + 1))).one)) ((Cyclo3.cyc3 (ring (3 * k + 1))).neg (Cyclo3.cyc3 (  -- (+37 chars; full line in the Zenodo source)
    _ = _ := by lib_ring

end CubicRecip

#print axioms CubicRecip.ph_add
#print axioms CubicRecip.ph_mul
#print axioms CubicRecip.ph_neg
#print axioms CubicRecip.ph_one
#print axioms CubicRecip.ph_zero
#print axioms CubicRecip.ph_sum
#print axioms CubicRecip.ph_delta
#print axioms CubicRecip.pow_SY
#print axioms CubicRecip.pw_mul_split
#print axioms CubicRecip.mp_mul
#print axioms CubicRecip.mp_mono
#print axioms CubicRecip.charq
#print axioms CubicRecip.om_pow
#print axioms CubicRecip.coefK
#print axioms CubicRecip.ph_pow
#print axioms CubicRecip.nsm_zero''
#print axioms CubicRecip.mono0_pow
#print axioms CubicRecip.mono0_mul2
#print axioms CubicRecip.frob_val
#print axioms CubicRecip.cb_iv
#print axioms CubicRecip.cubic_recip_inert

#print axioms: 21 theorems, no axioms.

78.52 Quartic Jacobi sum

In \(\mathbb Z[i][C_p]\): \(g\star g=J\,g_\rho \), \(\bar g\star \bar g=\bar J\,g_\rho \), \(g_\rho \star g_\rho =X\), \(g\star \bar g=\chi (-1)X\), so \(J\bar J=p\).

\[ J=\sum _{t\in \mathbb F_p}\chi (t)\chi (1-t)=a+bi,\qquad a^2+b^2=p. \]

lang/QuartJacobi.lean

/-
  lang/QuartJacobi — G2 (T43): the quartic Jacobi sum J = Σ χ(t)χ(1 − t) ∈ ℤ[i] has norm p.

  p = 4n + 5, χ = QuartChar.chi4, χ̄(x) = χ(x⁻¹) = conj χ(x), ρ = χ² (the quadratic character). In ℤ[i][C_p] (CharSumGen):
    g ⋆ ḡ = χ(−1) X,  g ⋆ g = J g_ρ,  ḡ ⋆ ḡ = J̄ g_ρ,  g_ρ ⋆ g_ρ = X,  X ⋆ X = p X
  so J J̄ X = (g ḡ)² = p X and, at ζ⁰, J = a + bi has a² + b² = p:
      quart_norm
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.QuartChar
import lang.CharSumGen
import lang.JacobiNorm

namespace QuartJacobi

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar GroupRing GroupRingFrob

variable (n : Nat)

def ivq (x : Zp (4 * n + 3 + 1)) : Zp (4 * n + 3 + 1) := pw (ring (4 * n + 3 + 1)) x (4 * n + 3)
def Jq (ψ : Zp (4 * n + 3 + 1) → Int × Int) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun t => (Cyclo4.cyc4 LibInt.intR).mul (ψ t) (ψ ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg t)))) (LL n)
def Ppq : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n)
def gconj (z : Int × Int) : Int × Int := (z.1, -z.2)

theorem ivq_mul (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) : (ring (4 * n + 3 + 1)).mul (ivq n x) x = (ring (4 * n + 3 + 1)).one := inv_exists (4 * n + 3) hpr x hx
theorem ivq_iv (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (x : Zp (4 * n + 3 + 1)) : ivq n (ivq n x) = x := CharSumGen.iv_iv (4 * n + 2) hpr x
theorem ivq_zero : ivq n (ring (4 * n + 3 + 1)).zero = (ring (4 * n + 3 + 1)).zero := HasseCM.pw_zero_succ (ring (4 * n + 3 + 1)) (4 * n + 2)
theorem ivq_one : ivq n (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).one := CubicChar.pw_one'' (ring (4 * n + 3 + 1)) _
theorem ivq_mulmul (x y : Zp (4 * n + 3 + 1)) : ivq n ((ring (4 * n + 3 + 1)).mul x y) = (ring (4 * n + 3 + 1)).mul (ivq n x) (ivq n y) := pw_mul (ring (4 * n + 3 + 1)) x y _

theorem S_ne' (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ψ : Zp (4 * n + 3 + 1) → Int × Int) (hm : ∀ x y, ψ ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ x) (ψ y)) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (ψ a) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (ψ c) (ψ c)) (Jq n ψ) :=
  CharSumGen.S_ne (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr ψ hm c hc
theorem S_zero' (ψ : Zp (4 * n + 3 + 1) → Int × Int) (hm : ∀ x y, ψ ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ x) (ψ y)) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (ψ a) (ψ ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (ψ ((ring (4 * n + 3 + 1)).neg (ring (4 * n  -- (+112 chars; full line in the Zenodo source)
  CharSumGen.S_zero (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) ψ hm
theorem D_ne' (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ψ : Zp (4 * n + 3 + 1) → Int × Int) (hm : ∀ x y, ψ ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ x) (ψ y)) (h1 : ψ (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one) (h0 : ψ (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero) (hs : sumK (Cyclo4.cyc4 LibInt.intR) ψ (LL n) =   -- (+97 chars; full line in the Zenodo source)
    sumK (Cyclo4.cyc4 LibInt.intR) (fun b => (Cyclo4.cyc4 LibInt.intR).mul (ψ ((ring (4 * n + 3 + 1)).add c b)) (ψ (ivq n b))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one :=
  CharSumGen.D_ne (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr ψ hm h1 h0 hs c hc
theorem D_zero' (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ψ : Zp (4 * n + 3 + 1) → Int × Int) (hm : ∀ x y, ψ ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ x) (ψ y)) (h1 : ψ (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one) (h0 : ψ (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun b => (Cyclo4.cyc4 LibInt.intR).mul (ψ b) (ψ (ivq n b))) (LL n) = (Cyclo4.cyc4 LibInt.intR).add (Ppq n) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) :=
  CharSumGen.D_zero (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr ψ hm h1 h0

theorem chi_one (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : chi4 n ι (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one := val4 n hpr ι hi (ring (4 * n + 3 + 1)).one (PowerSum.one_ne_zero' _) 0 (Nat.zero_lt_succ 3) (CubicChar.pw_one'' (ring (4 *  -- (+15 chars; full line in the Zenodo source)

/-- χ(x⁻¹) = conj χ(x). -/
theorem chi_ivq (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) : chi4 n ι (ivq n x) = gconj (chi4 n ι x) := by
  by_cases hx : x = (ring (4 * n + 3 + 1)).zero
  · rw [hx, ivq_zero, chi4_zero]; rfl
  have h1 : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι (ivq n x)) (chi4 n ι x) = (Cyclo4.cyc4 LibInt.intR).one := by rw [← chi4_mul n hpr ι hi, ivq_mul n hpr x hx, chi_one n hpr ι hi]
  have h2 : (Cyclo4.cyc4 LibInt.intR).mul (gconj (chi4 n ι x)) (chi4 n ι x) = (Cyclo4.cyc4 LibInt.intR).one := by
    match exists_j n hpr ι hi x hx with
    | ⟨j, hj, ej⟩ =>
      rw [val4 n hpr ι hi x hx j hj ej]
      match j, hj with
      | 0, _ => rfl
      | 1, _ => rfl
      | 2, _ => rfl
      | 3, _ => rfl
      | k + 4, hk => exact absurd hk (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 k) h))
  calc chi4 n ι (ivq n x) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι (ivq n x)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chi4 n ι x)) (chi4 n ι x)) := by rw [h2]; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι (ivq n x)) (chi4 n ι x)) (gconj (chi4 n ι x)) := by lib_ring
    _ = gconj (chi4 n ι x) := by rw [h1]; lib_ring

def rho (ι x : Zp (4 * n + 3 + 1)) : Int × Int := (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι x) (chi4 n ι x)
def chib (ι x : Zp (4 * n + 3 + 1)) : Int × Int := chi4 n ι (ivq n x)

theorem rho_mul (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x y : Zp (4 * n + 3 + 1)) : rho n ι ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι x) (rho n ι y) := by
  unfold rho; rw [chi4_mul n hpr ι hi]; lib_ring
theorem chib_mul (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x y : Zp (4 * n + 3 + 1)) : chib n ι ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (chib n ι x) (chib n ι y) := by
  unfold chib; rw [ivq_mulmul, chi4_mul n hpr ι hi]

/-- ρ(x) is real: ρ(x⁻¹) = ρ(x). -/
theorem rho_iv (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) : rho n ι (ivq n x) = rho n ι x := by
  unfold rho; rw [chi_ivq n hpr ι hi]
  by_cases hx : x = (ring (4 * n + 3 + 1)).zero
  · rw [hx, chi4_zero]; rfl
  match exists_j n hpr ι hi x hx with
  | ⟨j, hj, ej⟩ =>
    rw [val4 n hpr ι hi x hx j hj ej]
    match j, hj with
    | 0, _ => rfl
    | 1, _ => rfl
    | 2, _ => rfl
    | 3, _ => rfl
    | k + 4, hk => exact absurd hk (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 k) h))

theorem rho_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
  match PowerSum.exists_nonroot (4 * n + 3) hpr (2 * n + 1) (by rw [show 4 * n + 3 = (2 * n + 1 + 1) + (2 * n + 1) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := CharSumGen.sum_scale (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr (rho n ι) x0 hx0
    have e : sumK (Cyclo4.cyc4 LibInt.intR) (fun x => rho n ι ((ring (4 * n + 3 + 1)).mul x0 x)) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι x0) (sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n)) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => rho_mul n hpr ι hi x0 x) _
    have hre' : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι x0) (sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n)) = sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) := e.symm.trans hre
    match exists_j n hpr ι hi x0 hx0 with
    | ⟨j, hj, ej⟩ =>
      have hr : rho n ι x0 = (-1, 0) := by
        unfold rho; rw [val4 n hpr ι hi x0 hx0 j hj ej]
        match j, hj with
        | 0, _ => exact absurd (by rw [show 2 * n + 1 + 1 = (n + 1) * 2 by lib_nat, ← TwoPower.pwpow, ej]; rfl) hn
        | 1, _ => rfl
        | 2, _ =>
          exfalso; apply hn
          rw [show 2 * n + 1 + 1 = (n + 1) * 2 by lib_nat, ← TwoPower.pwpow, ej, p2, p2, hi]; lib_ring
        | 3, _ => rfl
        | k + 4, hk => exact absurd hk (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 k) h))
      rw [hr] at hre'
      generalize sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = S at hre' ⊢
      match S with
      | (a, b) =>
        have h1 : (Cyclo4.c4mul LibInt.intR (-1, 0) (a, b)).1 = a := congrArg Prod.fst hre'
        have h2 : (Cyclo4.c4mul LibInt.intR (-1, 0) (a, b)).2 = b := congrArg Prod.snd hre'
        change -1 * a + -(0 * b) = a at h1
        change -1 * b + 0 * a = b at h2
        have ha : a = 0 := int2 a (by rw [show a + a = a - (-1 * a + -(0 * b)) by lib_int, h1]; lib_int)
        have hb : b = 0 := int2 b (by rw [show b + b = b - (-1 * b + 0 * a) by lib_int, h2]; lib_int)
        rw [ha, hb]; rfl

def Gq (ψ : Zp (4 * n + 3 + 1) → Int × Int) : V (Int × Int) ((4 * n + 3 + 1) + 1) := tb (4 * n + 3 + 1) ψ
def Nq : V (Int × Int) ((4 * n + 3 + 1) + 1) := tb (4 * n + 3 + 1) (fun _ => (Cyclo4.cyc4 LibInt.intR).one)
def Xq : V (Int × Int) ((4 * n + 3 + 1) + 1) := gadd (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (gneg (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Nq n))

theorem gt_Xq (c : Zp (4 * n + 3 + 1)) : gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Xq n) c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Ppq n) (delta (Cyclo4.cyc4 LibInt.intR) c (ring (4 * n + 3 + 1)).zero)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := CharSumGen.gt_X (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) c
theorem XXq : gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Xq n) (Xq n) = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (Xq n) := CharSumGen.XX (Cyclo4.cyc4 LibInt.intR) (4 * n + 2)
theorem convq (ψ ψ' : Zp (4 * n + 3 + 1) → Int × Int) (hm' : ∀ x y, ψ' ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ' x) (ψ' y)) (c : Zp (4 * n + 3 + 1)) :
    gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n ψ) (Gq n ψ')) c = (Cyclo4.cyc4 LibInt.intR).mul (ψ' ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (sumK (Cyclo4.cyc4 LibInt.intR) (fun b => (Cyclo4.cyc4 LibInt.intR).mul (ψ ((ring (4 * n + 3 + 1)).add c b)) (ψ' b)) (LL n)) :=
  CharSumGen.conv_D (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) ψ ψ' hm' c

theorem gt_sq (ψ : Zp (4 * n + 3 + 1) → Int × Int) (c : Zp (4 * n + 3 + 1)) :
    gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n ψ) (Gq n ψ)) c = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (ψ a) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n) := by
  rw [gt_mul]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by unfold Gq; rw [gt_tb, gt_tb]) _

theorem chib_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
  have hre := reindex (Cyclo4.cyc4 LibInt.intR) _ (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (ivq n) (ivq n) (ivq_iv n hpr) (ivq_iv n hpr) (chi4 n ι)
  exact hre.trans (chi4_sum n hpr ι hi)

/-- the X-shape of ψ ⋆ ψ' when ψ'(b) = ψ(b⁻¹). -/
theorem conv_X (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ψ ψ' : Zp (4 * n + 3 + 1) → Int × Int) (hm : ∀ x y, ψ ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ x) (ψ y)) (hm' : ∀ x y, ψ' ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ' x) (ψ' y))
    (h1 : ψ (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one) (h0 : ψ (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero) (hs : sumK (Cyclo4.cyc4 LibInt.intR) ψ (LL n) = (Cyclo4.cyc4 LibInt.intR).zero) (hiv : ∀ b, ψ' b = ψ (ivq n b)) :
    gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n ψ) (Gq n ψ') = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ψ' ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (Xq n) :=
  ext (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) _ _ (fun c => by
    rw [convq n ψ ψ' hm' c, JacobiNorm.mono0_mulg, gt_Xq]
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun b => by rw [hiv b])]
    by_cases hc : c = (ring (4 * n + 3 + 1)).zero
    · rw [hc]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun b => by rw [(ring (4 * n + 3 + 1)).zero_add]), D_zero' n hpr ψ hm h1 h0]
      unfold delta; rw [ite_pos' _ _ rfl]; lib_ring
    · rw [D_ne' n hpr ψ hm h1 h0 hs c hc]
      unfold delta; rw [ite_neg' _ _ hc]; lib_ring)

theorem chib_one (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : chib n ι (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one := by unfold chib; rw [ivq_one, chi_one n hpr ι hi]
theorem chib_zero (ι : Zp (4 * n + 3 + 1)) : chib n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero := by unfold chib; rw [ivq_zero, chi4_zero]; rfl
theorem rho_one (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : rho n ι (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one := by unfold rho; rw [chi_one n hpr ι hi]; lib_ring
theorem rho_zero (ι : Zp (4 * n + 3 + 1)) : rho n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero := by unfold rho; rw [chi4_zero]; rfl
theorem rho_neg1 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : rho n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (Cyclo4.cyc4 LibInt.intR).one := by
  unfold rho; rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring, chi_one n hpr ι hi]

theorem E1 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n (chi4 n ι)) (Gq n (chib n ι)) = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 +  -- (+87 chars; full line in the Zenodo source)
  conv_X n hpr _ _ (chi4_mul n hpr ι hi) (chib_mul n hpr ι hi) (chi_one n hpr ι hi) (chi4_zero n ι) (chi4_sum n hpr ι hi) (fun _ => rfl)

theorem E4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n (rho n ι)) (Gq n (rho n ι)) = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1  -- (+51 chars; full line in the Zenodo source)
  rw [conv_X n hpr _ _ (rho_mul n hpr ι hi) (rho_mul n hpr ι hi) (rho_one n hpr ι hi) (rho_zero n ι) (rho_sum n hpr ι hi) (fun b => (rho_iv n hpr ι hi b).symm), rho_neg1 n hpr ι hi]

theorem E2gen (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ψ : Zp (4 * n + 3 + 1) → Int × Int) (hm : ∀ x y, ψ ((ring (4 * n + 3 + 1)).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (ψ x) (ψ y)) (r : Zp (4 * n + 3 + 1) → Int × Int)
    (hr : ∀ c, (Cyclo4.cyc4 LibInt.intR).mul (ψ c) (ψ c) = r c) (hr0 : r (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero) (hsr : sumK (Cyclo4.cyc4 LibInt.intR) r (LL n) = (Cyclo4.cyc4 LibInt.intR).zero) :
    gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n ψ) (Gq n ψ) = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n ψ)) (Gq n r) :=
  ext (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) _ _ (fun c => by
    rw [gt_sq, JacobiNorm.mono0_mulg]
    unfold Gq; rw [gt_tb]
    by_cases hc : c = (ring (4 * n + 3 + 1)).zero
    · rw [hc, S_zero' n ψ hm, hr0, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hr, hsr]; lib_ring
    · rw [S_ne' n hpr ψ hm c hc, hr]; lib_ring)

theorem gconj_mul (x y : Int × Int) : gconj ((Cyclo4.cyc4 LibInt.intR).mul x y) = (Cyclo4.cyc4 LibInt.intR).mul (gconj x) (gconj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2) = x.1 * y.1 + -(-x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1) = x.1 * -y.2 + -x.2 * y.1
    lib_int

theorem gconj_sum {A : Type} (f : A → Int × Int) : ∀ l : List A, gconj (sumK (Cyclo4.cyc4 LibInt.intR) f l) = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => gconj (f a)) l
  | [] => rfl
  | a :: l => by
    show gconj ((Cyclo4.cyc4 LibInt.intR).add (f a) (sumK (Cyclo4.cyc4 LibInt.intR) f l)) = (Cyclo4.cyc4 LibInt.intR).add (gconj (f a)) (sumK (Cyclo4.cyc4 LibInt.intR) (fun a => gconj (f a)) l)
    rw [← gconj_sum f l]
    apply Prod.ext
    · rfl
    · show -((f a).2 + (sumK (Cyclo4.cyc4 LibInt.intR) f l).2) = -(f a).2 + -(sumK (Cyclo4.cyc4 LibInt.intR) f l).2
      lib_int

theorem Jb_conj (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : Jq n (chib n ι) = gconj (Jq n (chi4 n ι)) := by
  unfold Jq; rw [gconj_sum]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by unfold chib; rw [gconj_mul, chi_ivq n hpr ι hi, chi_ivq n hpr ι hi]) _

theorem sum_oneq : ∀ k, sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (CMInert.allZ (4 * n + 3 + 1) k) = (Int.ofNat k, 0)
  | 0 => rfl
  | k + 1 => by
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (CMInert.allZ (4 * n + 3 + 1) k ++ [mk (4 * n + 3 + 1) k]) = _
    rw [sumK_append, sum_oneq k]
    rfl

/-- **N(J) = p**: the quartic Jacobi sum J = a + bi has a² + b² = p. -/
theorem quart_norm (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2 = Int.ofNat (4 * n + 3 + 1 + 1) := by
  have h2 := E2gen n hpr _ (chi4_mul n hpr ι hi) (rho n ι) (fun c => rfl) (rho_zero n ι) (rho_sum n hpr ι hi)
  have h3 := E2gen n hpr _ (chib_mul n hpr ι hi) (rho n ι) (fun c => rho_iv n hpr ι hi c) (rho_zero n ι) (rho_sum n hpr ι hi)
  have h1 := E1 n hpr ι hi
  have h4 := E4 n hpr ι hi
  have hcm : (Cyclo4.cyc4 LibInt.intR).mul (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    unfold chib; rw [show (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι (ivq n ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (chi4 n ι (ivq n ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) = rho n ι (ivq n ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) from rfl, rho_iv n hpr ι hi, rho_neg1 n hpr ι hi]
  let G := gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)
  have key : gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 LibInt.intR).mul (Jq n (chi4 n ι)) (Jq n (chib n ι)))) (Xq n) = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (Xq n) := by
    have L : G.mul (G.mul (Gq n (chi4 n ι)) (Gq n (chi4 n ι))) (G.mul (Gq n (chib n ι)) (Gq n (chib n ι)))
        = G.mul (G.mul (Gq n (chi4 n ι)) (Gq n (chib n ι))) (G.mul (Gq n (chi4 n ι)) (Gq n (chib n ι))) := by lib_ring
    have h2' : G.mul (Gq n (chi4 n ι)) (Gq n (chi4 n ι)) = G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n (chi4 n ι))) (Gq n (rho n ι)) := h2
    have h3' : G.mul (Gq n (chib n ι)) (Gq n (chib n ι)) = G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n (chib n ι))) (Gq n (rho n ι)) := h3
    have h1' : G.mul (Gq n (chi4 n ι)) (Gq n (chib n ι)) = G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (Xq n) := h1
    have h4' : G.mul (Gq n (rho n ι)) (Gq n (rho n ι)) = G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Cyclo4.cyc4 LibInt.intR).one) (Xq n) := h4
    have hX' : G.mul (Xq n) (Xq n) = G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (Xq n) := XXq n
    have m2 : ∀ s t, G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero s) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero t) = mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 LibInt.intR).mul s t) := fun s t => by show gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) _ _ = _; rw [mono_mul, show (ring  -- (+120 chars; full line in the Zenodo source)
    rw [h2', h3', h1'] at L
    rw [show G.mul (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n (chi4 n ι))) (Gq n (rho n ι))) (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n (chib n ι))) (Gq n (rho n ι)))
        = G.mul (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n (chi4 n ι))) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Jq n (chib n ι)))) (G.mul (Gq n (rho n ι)) (Gq n (rho n ι))) by lib_ring,
      h4', m2, show G.mul (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (Xq n)) (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (Xq n))
        = G.mul (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)))) (G.mul (Xq n) (Xq n)) by lib_ring,
      hX', m2, hcm, show G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 LibInt.intR).mul (Jq n (chi4 n ι)) (Jq n (chib n ι)))) (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Cyclo4.cyc4 LibInt.intR).one) (Xq n))
        = G.mul (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 LibInt.intR).mul (Jq n (chi4 n ι)) (Jq n (chib n ι)))) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Cyclo4.cyc4 LibInt.intR).one)) (Xq n) by lib_ring,
      m2, show G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Cyclo4.cyc4 LibInt.intR).one) (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (Xq n)) = G.mul (G.mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Cyclo4.cyc4 LibInt.intR).one) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (r  -- (+55 chars; full line in the Zenodo source)
      m2, (Cyclo4.cyc4 LibInt.intR).mul_comm _ (Cyclo4.cyc4 LibInt.intR).one, (Cyclo4.cyc4 LibInt.intR).one_mul, (Cyclo4.cyc4 LibInt.intR).one_mul] at L
    exact L
  have k0 := congrArg (fun u => gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) u (ring (4 * n + 3 + 1)).zero) key
  have k1 : (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Jq n (chi4 n ι)) (Jq n (chib n ι))) (gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Xq n) (ring (4 * n + 3 + 1)).zero) = (Cyclo4.cyc4 LibInt.intR).mul (Ppq n) (gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Xq n) (ring (4 * n + 3 + 1)).zero) := by
    have := k0
    rw [JacobiNorm.mono0_mulg, JacobiNorm.mono0_mulg] at this
    exact this
  have hP : Ppq n = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := sum_oneq n _
  have hX0 : gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Xq n) (ring (4 * n + 3 + 1)).zero = (Int.ofNat (4 * n + 3 + 1), 0) := by
    rw [gt_Xq]; unfold delta; rw [ite_pos' _ _ rfl, (Cyclo4.cyc4 LibInt.intR).mul_comm, (Cyclo4.cyc4 LibInt.intR).one_mul, hP]
    exact Prod.ext (by show Int.ofNat (4 * n + 3 + 1) + 1 + -1 = Int.ofNat (4 * n + 3 + 1); lib_int) (by show (0 : Int) + -0 = 0; rfl)
  have hN : (Cyclo4.cyc4 LibInt.intR).mul (Jq n (chi4 n ι)) (Jq n (chib n ι)) = ((Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2, 0) := by
    rw [Jb_conj n hpr ι hi]
    apply Prod.ext
    · show (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + -((Jq n (chi4 n ι)).2 * -(Jq n (chi4 n ι)).2) = (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2
      lib_int
    · show (Jq n (chi4 n ι)).1 * -(Jq n (chi4 n ι)).2 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).1 = 0
      lib_int
  rw [hX0, hN, hP] at k1
  have k2 := congrArg Prod.fst k1
  change _ * Int.ofNat (4 * n + 3 + 1) + -(0 * 0) = Int.ofNat (4 * n + 3 + 1 + 1) * Int.ofNat (4 * n + 3 + 1) + -(0 * 0) at k2
  apply JacobiNorm.int_cancel _ _ (4 * n + 3)
  rw [show ((Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2) * Int.ofNat (4 * n + 3 + 1)
      = ((Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2) * Int.ofNat (4 * n + 3 + 1) + -(0 * 0) by lib_int, k2]
  lib_int

end QuartJacobi

#print axioms QuartJacobi.ivq_mul
#print axioms QuartJacobi.ivq_iv
#print axioms QuartJacobi.S_ne'
#print axioms QuartJacobi.S_zero'
#print axioms QuartJacobi.D_ne'
#print axioms QuartJacobi.D_zero'
#print axioms QuartJacobi.chi_one
#print axioms QuartJacobi.chi_ivq
#print axioms QuartJacobi.rho_mul
#print axioms QuartJacobi.chib_mul
#print axioms QuartJacobi.rho_iv
#print axioms QuartJacobi.rho_sum
#print axioms QuartJacobi.gt_Xq
#print axioms QuartJacobi.XXq
#print axioms QuartJacobi.convq
#print axioms QuartJacobi.gt_sq
#print axioms QuartJacobi.chib_sum
#print axioms QuartJacobi.conv_X
#print axioms QuartJacobi.E1
#print axioms QuartJacobi.E4
#print axioms QuartJacobi.E2gen
#print axioms QuartJacobi.gconj_mul
#print axioms QuartJacobi.gconj_sum
#print axioms QuartJacobi.Jb_conj
#print axioms QuartJacobi.sum_oneq
#print axioms QuartJacobi.quart_norm

#print axioms: 26 theorems, no axioms.

78.53 Quartic reciprocity

In \(\mathbb F_q[i][C_p]\): \(g^4=J^2X\), and \(g^{q+1}=\chi (q)\chi (-1)X\) (\(q=4k+3\)) or \(g^q=\bar \chi (q)g\) (\(q=4k+5\)).

\[ \chi (q)\bar \chi (-1)=J^{2(k+1)}p^k\ (q=4k+3),\qquad \bar \chi (q)=(J^2p)^{k+1}\ (q=4k+5)\quad \text {in }\mathbb F_q[i]. \]

lang/QuartRecip.lean

/-
  lang/QuartRecip — G2 (T43): quartic reciprocity for the primary-type prime J of ℤ[i] above p ≡ 1 (mod 4), at every rational prime q.

  p = 4n + 5, χ quartic character, J = Σ χ(t)χ(1 − t) (N(J) = p, QuartJacobi), φ : ℤ[i] → 𝔽_q[i] reduction. In 𝔽_q[i][C_p]:
    inert q = 4k + 3 (i^q = −i):  g^q = χ(q) ḡ,  g^{q+1} = χ(q)χ(−1) X,  g⁴ = J² X,  so
        quart_recip_inert    χ(q) χ(−1) = J^{2k+2} p^k       in 𝔽_q[i]
    split q = 4k + 5 (i^q = i):   g^q = χ̄(q) g,  g^q = (g⁴)^{k+1} g = J^{2k+2} p^{k+1} g, so
        quart_recip_split    χ̄(q) = J^{2k+2} p^{k+1}         in 𝔽_q[i]
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.QuartJacobi
import lang.CubicRecip

namespace QuartRecip

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi GroupRing GroupRingFrob
open LibRing (castZ castN)

variable (n : Nat)

section Gen
variable {R : Type} (T : CRing R)

def ph4 (z : Int × Int) : R × R := (castZ T z.1, castZ T z.2)

theorem ph4_add (x y : Int × Int) : ph4 T ((Cyclo4.cyc4 LibInt.intR).add x y) = (Cyclo4.cyc4 T).add (ph4 T x) (ph4 T y) := by
  apply Prod.ext
  · exact IntCast.castZ_add T x.1 y.1
  · exact IntCast.castZ_add T x.2 y.2

theorem ph4_mul (x y : Int × Int) : ph4 T ((Cyclo4.cyc4 LibInt.intR).mul x y) = (Cyclo4.cyc4 T).mul (ph4 T x) (ph4 T y) := by
  apply Prod.ext
  · show castZ T (x.1 * y.1 + -(x.2 * y.2)) = T.add (T.mul (castZ T x.1) (castZ T y.1)) (T.neg (T.mul (castZ T x.2) (castZ T y.2)))
    rw [IntCast.castZ_add, HasseDavenport.castZ_neg, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]
  · show castZ T (x.1 * y.2 + x.2 * y.1) = T.add (T.mul (castZ T x.1) (castZ T y.2)) (T.mul (castZ T x.2) (castZ T y.1))
    rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]

theorem ph4_neg (x : Int × Int) : ph4 T ((Cyclo4.cyc4 LibInt.intR).neg x) = (Cyclo4.cyc4 T).neg (ph4 T x) := by
  apply Prod.ext
  · exact HasseDavenport.castZ_neg T x.1
  · exact HasseDavenport.castZ_neg T x.2

theorem ph4_one : ph4 T (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 T).one := by
  apply Prod.ext
  · show castN T 1 = T.one
    exact add_zero' T _
  · rfl

theorem ph4_zero : ph4 T (Cyclo4.cyc4 LibInt.intR).zero = (Cyclo4.cyc4 T).zero := rfl

theorem ph4_sum {A : Type} (f : A → Int × Int) : ∀ l : List A, ph4 T (sumK (Cyclo4.cyc4 LibInt.intR) f l) = sumK (Cyclo4.cyc4 T) (fun a => ph4 T (f a)) l
  | [] => rfl
  | a :: l => by
    show ph4 T ((Cyclo4.cyc4 LibInt.intR).add (f a) (sumK _ f l)) = (Cyclo4.cyc4 T).add (ph4 T (f a)) (sumK (Cyclo4.cyc4 T) (fun a => ph4 T (f a)) l)
    rw [ph4_add, ph4_sum f l]

theorem ph4_delta {G : Type} [DecidableEq G] (x y : G) : ph4 T (delta (Cyclo4.cyc4 LibInt.intR) x y) = delta (Cyclo4.cyc4 T) x y := by
  unfold delta
  by_cases h : x = y
  · rw [ite_pos' _ _ h, ite_pos' _ _ h]; exact ph4_one T
  · rw [ite_neg' _ _ h, ite_neg' _ _ h]; exact ph4_zero T

theorem ph4_pow (z : Int × Int) : ∀ j, pw (Cyclo4.cyc4 T) (ph4 T z) j = ph4 T (pw (Cyclo4.cyc4 LibInt.intR) z j)
  | 0 => (ph4_one T).symm
  | j + 1 => by show (Cyclo4.cyc4 T).mul (pw _ (ph4 T z) j) (ph4 T z) = ph4 T ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) z j) z); rw [ph4_pow z j, ph4_mul]

def mp4 (u : V (Int × Int) ((4 * n + 3 + 1) + 1)) : V (R × R) ((4 * n + 3 + 1) + 1) := tb (4 * n + 3 + 1) (fun a => ph4 T (gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) u a))

theorem mp4_mul (u v : V (Int × Int) ((4 * n + 3 + 1) + 1)) : gmul (Cyclo4.cyc4 T) (4 * n + 3 + 1) (mp4 n T u) (mp4 n T v) = mp4 n T (gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) u v) :=
  ext (Cyclo4.cyc4 T) (4 * n + 3 + 1) _ _ (fun c => by
    unfold mp4
    rw [gt_tb, gt_mul, gt_mul, ph4_sum]
    exact sumK_ext (Cyclo4.cyc4 T) _ _ (fun a => by rw [gt_tb, gt_tb, ph4_mul]) _)

theorem mp4_mono (s : Int × Int) (a : Zp (4 * n + 3 + 1)) : mp4 n T (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) a s) = mono (Cyclo4.cyc4 T) (4 * n + 3 + 1) a (ph4 T s) :=
  ext (Cyclo4.cyc4 T) (4 * n + 3 + 1) _ _ (fun c => by unfold mp4; rw [gt_tb, gt_mono, gt_mono, ph4_mul, ph4_delta])

theorem coef4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (Q : Zp (4 * n + 3 + 1)) (hQ : Q ≠ (ring (4 * n + 3 + 1)).zero) (s : Zp (4 * n + 3 + 1) → R × R) (c : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 T) (fun a => (Cyclo4.cyc4 T).mul (s a) (delta (Cyclo4.cyc4 T) c ((ring (4 * n + 3 + 1)).mul Q a))) (GroupRing.L (4 * n + 3 + 1)) = s ((ring (4 * n + 3 + 1)).mul (ivq n Q) c) := by
  have hQQ := ivq_mul n hpr Q hQ
  have e := reindex (Cyclo4.cyc4 T) _ (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 T)) (fun a => (ring (4 * n + 3 + 1)).mul Q a) (fun b => (ring (4 * n + 3 + 1)).mul (ivq n Q) b)
    (fun x => by rw [show (ring (4 * n + 3 + 1)).mul (ivq n Q) ((ring (4 * n + 3 + 1)).mul Q x) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ivq n Q) Q) x by lib_ring, hQQ]; lib_ring)
    (fun y => by rw [show (ring (4 * n + 3 + 1)).mul Q ((ring (4 * n + 3 + 1)).mul (ivq n Q) y) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ivq n Q) Q) y by lib_ring, hQQ]; lib_ring)
    (fun b => (Cyclo4.cyc4 T).mul (delta (Cyclo4.cyc4 T) b c) (s ((ring (4 * n + 3 + 1)).mul (ivq n Q) b)))
  have e2 : sumK (Cyclo4.cyc4 T) (fun a => (Cyclo4.cyc4 T).mul (s a) (delta (Cyclo4.cyc4 T) c ((ring (4 * n + 3 + 1)).mul Q a))) (GroupRing.L (4 * n + 3 + 1))
      = sumK (Cyclo4.cyc4 T) (fun a => (fun b => (Cyclo4.cyc4 T).mul (delta (Cyclo4.cyc4 T) b c) (s ((ring (4 * n + 3 + 1)).mul (ivq n Q) b))) ((ring (4 * n + 3 + 1)).mul Q a)) (GroupRing.L (4 * n + 3 + 1)) :=
    sumK_ext (Cyclo4.cyc4 T) _ _ (fun a => by
      show _ = (Cyclo4.cyc4 T).mul (delta (Cyclo4.cyc4 T) ((ring (4 * n + 3 + 1)).mul Q a) c) (s ((ring (4 * n + 3 + 1)).mul (ivq n Q) ((ring (4 * n + 3 + 1)).mul Q a)))
      rw [show (ring (4 * n + 3 + 1)).mul (ivq n Q) ((ring (4 * n + 3 + 1)).mul Q a) = a by rw [show (ring (4 * n + 3 + 1)).mul (ivq n Q) ((ring (4 * n + 3 + 1)).mul Q a) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ivq n Q) Q) a by lib_ring, hQQ]; lib_ring,
        delta_symm, (Cyclo4.cyc4 T).mul_comm]) _
  rw [e2]
  exact e.trans (pick (Cyclo4.cyc4 T) _ (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 T)) _ c)

theorem charq4 (j : Nat) (hchar : nsm T j T.one = T.zero) : nsm (Cyclo4.cyc4 T) j (Cyclo4.cyc4 T).one = (Cyclo4.cyc4 T).zero := by
  have hn : ∀ m, nsm (Cyclo4.cyc4 T) m (Cyclo4.cyc4 T).one = (nsm T m T.one, nsm T m T.zero) := fun m => by
    induction m with
    | zero => rfl
    | succ m ih => show (Cyclo4.cyc4 T).add (nsm (Cyclo4.cyc4 T) m (Cyclo4.cyc4 T).one) (Cyclo4.cyc4 T).one = _; rw [ih]; rfl
  rw [hn, hchar]
  apply Prod.ext
  · rfl
  · show nsm T j T.zero = T.zero
    rw [nsm_eq_mul, mul_zero]
end Gen

theorem chi4_4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) : pw (Cyclo4.cyc4 LibInt.intR) (chi4 n ι x) 4 = (Cyclo4.cyc4 LibInt.intR).one ∧ pw (Cyclo4.cyc4 LibInt.intR) (chi4 n ι x) 3 = chib n  -- (+10 chars; full line in the Zenodo source)
  unfold chib; rw [chi_ivq n hpr ι hi]
  match exists_j n hpr ι hi x hx with
  | ⟨j, hj, ej⟩ =>
    rw [val4 n hpr ι hi x hx j hj ej]
    match j, hj with
    | 0, _ => exact ⟨rfl, rfl⟩
    | 1, _ => exact ⟨rfl, rfl⟩
    | 2, _ => exact ⟨rfl, rfl⟩
    | 3, _ => exact ⟨rfl, rfl⟩
    | k + 4, hk => exact absurd hk (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 k) h))

theorem pow4 {S' : Type} (G : CRing S') (z : S') (h4 : pw G z 4 = G.one) (r : Nat) : ∀ m, pw G z (4 * m + r) = pw G z r
  | 0 => by rw [show 4 * 0 + r = r by lib_nat]
  | m + 1 => by rw [show 4 * (m + 1) + r = (4 * m + r) + 4 by lib_nat, HasseCM.pw_add', pow4 G z h4 r m, h4]; lib_ring


/-- **Quartic reciprocity at an inert q = 4k + 3**: χ(q) χ̄(−1) = (J² )^{k+1} p^k in 𝔽_q[i]. -/
theorem quart_recip_inert (k : Nat) (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (hq : ∀ d, Dv d (4 * k + 1 + 1 + 1) → d = 1 ∨ d = 4 * k + 1 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hQ : nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).z  -- (+6 chars; full line in the Zenodo source)
    (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one))) (ph4 (ring (4 * k + 1 + 1)) (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)))
      = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι)))) (ph4 (ring (4 * k + 1 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (k + 1)) (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1  -- (+24 chars; full line in the Zenodo source)

  have hP2 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) 2 = gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι)))) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (rho n ι))) := by
    show gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (gone (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι)))) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) = _
    rw [gm_one, mp4_mul, E2gen n hpr _ (chi4_mul n hpr ι hi) (rho n ι) (fun c => rfl) (rho_zero n ι) (rho_sum n hpr ι hi), ← mp4_mul, mp4_mono]
  have hRR : (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mp4 n (ring (4 * k + 1 + 1)) (Gq n (rho n ι))) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (rho n ι))) = (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (mp4 n (ring  -- (+31 chars; full line in the Zenodo source)
    show gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) _ _ = gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) _ _
    rw [mp4_mul, E4 n hpr ι hi, ← mp4_mul, mp4_mono]
  have hYY : (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mp4 n (ring (4 * k + 1 + 1)) (Xq n)) (mp4 n (ring (4 * k + 1 + 1)) (Xq n)) = (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Ppq n))) (mp4 n (ring (4 * k + 1 + 1)) (Xq n)) := by
    show gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) _ _ = gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) _ _
    rw [mp4_mul, XXq, ← mp4_mul, mp4_mono]
  have m2 : ∀ s t, (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero s) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero t) = mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul s t) :  -- (+1 chars; full line in the Zenodo source)
    fun s t => CubicRecip.mono0_mul2 (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) s t
  have hP4 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) 4 = (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (Jq n   -- (+165 chars; full line in the Zenodo source)
    rw [(CubicRecip.pw_mul_split (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) 2 2 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) 4 = _), hP2]
    show (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).one ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n   -- (+309 chars; full line in the Zenodo source)
    rw [show (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).one ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi  -- (+309 chars; full line in the Zenodo source)
        = (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι)))) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (Jq n (ch  -- (+187 chars; full line in the Zenodo source)
      hRR, m2, show (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))))) ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring  -- (+161 chars; full line in the Zenodo source)
        = (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))))) (mono (Cyclo4.cyc4 (ring (4 * k +   -- (+168 chars; full line in the Zenodo source)
  have hfv : ∀ x, pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι x)) (4 * k + 1 + 1 + 1) = ph4 (ring (4 * k + 1 + 1)) (chib n ι x) := fun x => by
    by_cases hx : x = (ring (4 * n + 3 + 1)).zero
    · rw [hx, chi4_zero]
      show (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1)) (Cyclo4.cyc4 LibInt.intR).zero) (4 * k + 1 + 1)) (ph4 (ring (4 * k + 1 + 1)) (Cyclo4.cyc4 LibInt.intR).zero) = ph4 (ring (4 * k + 1 + 1)) (chib n ι (ring (4 * n + 3 + 1)).zero)
      rw [ph4_zero, mul_zero, chib_zero]; rfl
    · rw [ph4_pow, show (4 * k + 1 + 1 + 1) = 4 * k + 3 by lib_nat, pow4 (Cyclo4.cyc4 LibInt.intR) _ (chi4_4 n hpr ι hi x hx).1 3 k]
      rw [← ph4_pow, ph4_pow]
      exact congrArg (ph4 (ring (4 * k + 1 + 1))) (chi4_4 n hpr ι hi x hx).2
  have hfrob : pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) (4 * k + 1 + 1 + 1) = gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 +   -- (+63 chars; full line in the Zenodo source)
    ext (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) _ _ (fun c => by
      have hd := decomp (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι)))
      refine (congrArg (fun u => gt (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) u (4 * k + 1 + 1 + 1)) c) hd).trans ?_
      show gt (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (sumK (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) _ (GroupRing.L (4 * n + 3 + 1))) (4 * k + 1 + 1 + 1)) c = _
      rw [frob_sum (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (4 * k + 1) hq (charq4 (ring (4 * k + 1 + 1)) _ (char_p _)), gt_sum, JacobiNorm.mono0_mulg]
      have e2 : sumK (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (fun a => gt (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) a (gt (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) a)) (4 * k + 1 + 1 + 1)) c) (GroupRing.L (4   -- (+13 chars; full line in the Zenodo source)
          = sumK (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (fun a => (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι a)) (4 * k + 1 + 1 + 1)) (delta (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) c ((ring (4 * n + 3 + 1)).mul (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one) a))) (GroupRing.L (4 * n + 3 + 1)) :=
        sumK_ext (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) _ _ (fun a => by
          rw [mono_pow, gt_mono, nsm_eq_mul]
          unfold mp4 Gq; rw [gt_tb, gt_tb]) _
      rw [e2, coef4 n (ring (4 * k + 1 + 1)) hpr _ hQ (fun a => pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι a)) (4 * k + 1 + 1 + 1)) c, hfv]
      rw [chib_mul n hpr ι hi, show chib n ι (ivq n (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one)) = chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one) by unfold chib; rw [ivq_iv n hpr], ph4_mul]
      unfold mp4 Gq; rw [gt_tb, gt_tb])

  have L1 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) ((4 * k + 1 + 1 + 1) + 1) = gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι (nsm (ring (4 * n + 3   -- (+197 chars; full line in the Zenodo source)
    show gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) (4 * k + 1 + 1 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) = _
    rw [hfrob, gm_assoc, mp4_mul, gm_comm (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n (chib n ι)), E1 n hpr ι hi, ← mp4_mul, mp4_mono, ← gm_assoc]
    exact congrArg (fun u => gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) u (mp4 n (ring (4 * k + 1 + 1)) (Xq n))) (m2 _ _)
  have L2 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 1 + 1)) (Gq n (chi4 n ι))) ((4 * k + 1 + 1 + 1) + 1) = gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) ((Cyclo4.cyc4 (ring  -- (+355 chars; full line in the Zenodo source)
    rw [show (4 * k + 1 + 1 + 1) + 1 = 4 * (k + 1) by lib_nat, CubicRecip.pw_mul_split, hP4, CubicRecip.pow_SY (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) _ _ _ hYY k]
    show gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (gmul (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero _) (k + 1)) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4  -- (+125 chars; full line in the Zenodo source)
    rw [CubicRecip.mono0_pow, CubicRecip.mono0_pow, CubicRecip.mono0_mul2]
  have key := L1.symm.trans L2
  have k1 := congrArg (fun u => gt (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) u (ring (4 * n + 3 + 1)).one) key
  have k2 := k1
  rw [JacobiNorm.mono0_mulg, JacobiNorm.mono0_mulg] at k2
  have hX1 : gt (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * n + 3 + 1) (mp4 n (ring (4 * k + 1 + 1)) (Xq n)) (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).neg (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).one := by
    unfold mp4; rw [gt_tb, gt_Xq]; unfold delta; rw [ite_neg' _ _ (PowerSum.one_ne_zero' _)]
    rw [ph4_add, ph4_mul, ph4_zero, mul_zero, ph4_neg, ph4_one]; lib_ring
  rw [hX1] at k2
  calc _ = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one))) (ph4 (ring (4 * k + 1 + 1)) (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)))) ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).neg (Cyclo4.c  -- (+132 chars; full line in the Zenodo source)
    _ = _ := by rw [k2]; lib_ring

/-- **Quartic reciprocity at a split q = 4k + 5**: χ̄(q) = (J²)^{k+1} p^{k+1} in 𝔽_q[i]. -/
theorem quart_recip_split (k : Nat) (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (hq : ∀ d, Dv d (4 * k + 3 + 1 + 1) → d = 1 ∨ d = 4 * k + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hQ : nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).z  -- (+6 chars; full line in the Zenodo source)
    ph4 (ring (4 * k + 3 + 1)) (chib n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one))
      = (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (k + 1)) (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (ph4 (ring (4 * k + 3  -- (+30 chars; full line in the Zenodo source)

  have hP2 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) 2 = gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (rho n ι))) := by
    show gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (gone (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι)))) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) = _
    rw [gm_one, mp4_mul, E2gen n hpr _ (chi4_mul n hpr ι hi) (rho n ι) (fun c => rfl) (rho_zero n ι) (rho_sum n hpr ι hi), ← mp4_mul, mp4_mono]
  have hRR : (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mp4 n (ring (4 * k + 3 + 1)) (Gq n (rho n ι))) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (rho n ι))) = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (mp4 n (ring  -- (+31 chars; full line in the Zenodo source)
    show gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _ = gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _
    rw [mp4_mul, E4 n hpr ι hi, ← mp4_mul, mp4_mono]
  have hYY : (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mp4 n (ring (4 * k + 3 + 1)) (Xq n)) (mp4 n (ring (4 * k + 3 + 1)) (Xq n)) = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Ppq n))) (mp4 n (ring (4 * k + 3 + 1)) (Xq n)) := by
    show gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _ = gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _
    rw [mp4_mul, XXq, ← mp4_mul, mp4_mono]
  have m2 : ∀ s t, (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero s) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero t) = mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul s t) :  -- (+1 chars; full line in the Zenodo source)
    fun s t => CubicRecip.mono0_mul2 (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) s t
  have hP4 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) 4 = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n   -- (+165 chars; full line in the Zenodo source)
    rw [(CubicRecip.pw_mul_split (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) 2 2 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) 4 = _), hP2]
    show (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).one ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n   -- (+309 chars; full line in the Zenodo source)
    rw [show (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).one ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi  -- (+309 chars; full line in the Zenodo source)
        = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Jq n (ch  -- (+187 chars; full line in the Zenodo source)
      hRR, m2, show (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))))) ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring  -- (+161 chars; full line in the Zenodo source)
        = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))))) (mono (Cyclo4.cyc4 (ring (4 * k +   -- (+168 chars; full line in the Zenodo source)
  have hfv : ∀ x, pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (ph4 (ring (4 * k + 3 + 1)) (chi4 n ι x)) (4 * k + 3 + 1 + 1) = ph4 (ring (4 * k + 3 + 1)) (chi4 n ι x) := fun x => by
    by_cases hx : x = (ring (4 * n + 3 + 1)).zero
    · rw [hx, chi4_zero]
      show (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).zero) (4 * k + 3 + 1)) (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).zero) = ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).zero
      rw [ph4_zero, mul_zero]
    · rw [ph4_pow, show (4 * k + 3 + 1 + 1) = 4 * (k + 1) + 1 by lib_nat, pow4 (Cyclo4.cyc4 LibInt.intR) _ (chi4_4 n hpr ι hi x hx).1 1 (k + 1)]
      exact congrArg (ph4 (ring (4 * k + 3 + 1))) (((Cyclo4.cyc4 LibInt.intR)).one_mul _)
  have hfrob : pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) (4 * k + 3 + 1 + 1) = gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (chib n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 +   -- (+63 chars; full line in the Zenodo source)
    ext (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _ (fun c => by
      have hd := decomp (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι)))
      refine (congrArg (fun u => gt (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) u (4 * k + 3 + 1 + 1)) c) hd).trans ?_
      show gt (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (sumK (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) _ (GroupRing.L (4 * n + 3 + 1))) (4 * k + 3 + 1 + 1)) c = _
      rw [frob_sum (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (4 * k + 3) hq (charq4 (ring (4 * k + 3 + 1)) _ (char_p _)), gt_sum, JacobiNorm.mono0_mulg]
      have e2 : sumK (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (fun a => gt (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) a (gt (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) a)) (4 * k + 3 + 1 + 1)) c) (GroupRing.L (4   -- (+13 chars; full line in the Zenodo source)
          = sumK (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (fun a => (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (ph4 (ring (4 * k + 3 + 1)) (chi4 n ι a)) (4 * k + 3 + 1 + 1)) (delta (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) c ((ring (4 * n + 3 + 1)).mul (nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one) a))) (GroupRing.L (4 * n + 3 + 1)) :=
        sumK_ext (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) _ _ (fun a => by
          rw [mono_pow, gt_mono, nsm_eq_mul]
          unfold mp4 Gq; rw [gt_tb, gt_tb]) _
      rw [e2, coef4 n (ring (4 * k + 3 + 1)) hpr _ hQ (fun a => pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (ph4 (ring (4 * k + 3 + 1)) (chi4 n ι a)) (4 * k + 3 + 1 + 1)) c, hfv]
      rw [chi4_mul n hpr ι hi, ph4_mul]
      unfold mp4 Gq chib; rw [gt_tb, gt_tb])

  have hNg : gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Nq n) (Gq n (chi4 n ι)) = gzero (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) :=
    ext (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) _ _ (fun c => by
      rw [gt_mul]
      unfold gzero; rw [gt_tb]
      have e : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Nq n) a) (gt (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Gq n (chi4 n ι)) ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (GroupRing.L (4 * n + 3 + 1))
          = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a))) (GroupRing.L (4 * n + 3 + 1)) :=
        sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by unfold Nq Gq; rw [gt_tb, gt_tb]; exact (Cyclo4.cyc4 LibInt.intR).one_mul _) _
      rw [e]
      exact (flip (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (chi4 n ι) c).trans (chi4_sum n hpr ι hi))
  have hXg : (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mp4 n (ring (4 * k + 3 + 1)) (Xq n)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (ph4 (ring (4 * k + 3 + 1)) (Ppq n))) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n  -- (+11 chars; full line in the Zenodo source)
    show gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _ = gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) _ _
    have hXgE : gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (Xq n) (Gq n (chi4 n ι)) = gmul (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (Gq n (chi4 n ι)) := by
      have h' : (gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).mul (Nq n) (Gq n (chi4 n ι)) = (gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).zero := hNg
      show (gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).add (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).neg (Nq n))) (Gq n (chi4 n ι)) = (gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4   -- (+45 chars; full line in the Zenodo source)
      rw [show (gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).add (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).neg (Nq n))) (Gq n (chi4 n ι))
          = (gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).add ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).mul (mono (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero (Ppq n)) (Gq n (chi4 n ι))) ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).neg ((gring (Cyclo4.cyc4 LibInt.intR) (4 * n + 3 + 1)).mul (Nq n) (Gq n (chi4 n ι)))) by lib_ring, h']
      lib_ring
    rw [mp4_mul, hXgE, ← mp4_mul, mp4_mono]
  have L2 : pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) (4 * k + 3 + 1 + 1) = gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) ((Cyclo4.cyc4 (ring (4 *   -- (+366 chars; full line in the Zenodo source)
    rw [show (4 * k + 3 + 1 + 1) = 4 * (k + 1) + 1 by lib_nat]
    show (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) (4 * (k + 1))) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) = _
    rw [CubicRecip.pw_mul_split, hP4, CubicRecip.pow_SY (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) _ _ _ hYY k]
    rw [show (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).  -- (+492 chars; full line in the Zenodo source)
        = (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul ((gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)).mul (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 +  -- (+507 chars; full line in the Zenodo source)
    show gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (gmul (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (ring (4 * n + 3 + 1)).zero _) (k + 1)) (pw (gring (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1)) (mono (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4  -- (+321 chars; full line in the Zenodo source)
    rw [CubicRecip.mono0_pow, CubicRecip.mono0_pow, CubicRecip.mono0_mul2, ← gm_assoc, CubicRecip.mono0_mul2]
    rw [show (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (k + 1)) (pw (Cyclo4.cyc  -- (+103 chars; full line in the Zenodo source)
        = (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (k + 1)) ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (  -- (+113 chars; full line in the Zenodo source)
    rfl
  have key := hfrob.symm.trans L2
  have k1 := congrArg (fun u => gt (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) u (ring (4 * n + 3 + 1)).one) key
  have k2 := k1
  rw [JacobiNorm.mono0_mulg, JacobiNorm.mono0_mulg] at k2
  have hg1 : gt (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (4 * n + 3 + 1) (mp4 n (ring (4 * k + 3 + 1)) (Gq n (chi4 n ι))) (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).one := by
    unfold mp4 Gq; rw [gt_tb, gt_tb, chi_one n hpr ι hi, ph4_one]
  rw [hg1, (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul_comm, (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).one_mul, (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul_comm, (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).one_mul] at k2
  exact k2

end QuartRecip

#print axioms QuartRecip.ph4_add
#print axioms QuartRecip.ph4_mul
#print axioms QuartRecip.ph4_neg
#print axioms QuartRecip.ph4_one
#print axioms QuartRecip.ph4_zero
#print axioms QuartRecip.ph4_sum
#print axioms QuartRecip.ph4_delta
#print axioms QuartRecip.ph4_pow
#print axioms QuartRecip.mp4_mul
#print axioms QuartRecip.mp4_mono
#print axioms QuartRecip.coef4
#print axioms QuartRecip.charq4
#print axioms QuartRecip.chi4_4
#print axioms QuartRecip.pow4
#print axioms QuartRecip.quart_recip_inert
#print axioms QuartRecip.quart_recip_split

#print axioms: 16 theorems, no axioms.

78.54 Multiplicativity of the Hecke coefficients of \(\mathbb Q(i)\)

For coprime odd \(m,n\), primary elements of norm \(mn\) factor uniquely as products of primary elements of norms \(m\) and \(n\).

\[ c_{mn}=c_m\,c_n,\qquad c_n=\sum _{\alpha \ \text {primary},\ N(\alpha )=n}\alpha . \]

lang/HeckeMult.lean

/-
  lang/HeckeMult — G3 (T43): the coefficients c_n = Σ_{α primary, N(α) = n} α of the theta series of the Hecke character of ℚ(i)
  are multiplicative: c_{mn} = c_m c_n for coprime odd m, n.

  Finite sums: zl n = [0, 1, −1, …, n, −n], box n = zl n × zl n (every z with |z.1|, |z.2| ≤ n exactly once), P n z ⇔ N(z) = n ∧ z primary
  (decidable through md: IDv (k + 1) x ⇔ md k |x| = 0).
    count_zl, sumK_pairs, count_box, abs_le     the counting facts
    idv_iff, primDec                            primary is decidable
    c, hecke_mult                               c_{mn} = c_m c_n            (GaussUnique.prim_split, uniq_split)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.GaussUnique
import lang.ZMod

namespace HeckeMult

open FTAChain DetChain LibInt GaussEuclid GaussCoprime GaussInt GaussFactor GaussUnique LibGcd YMChain FiniteRecChain

def zl : Nat → List Int
  | 0 => [0]
  | k + 1 => zl k ++ [Int.ofNat (k + 1), Int.negSucc k]

theorem count_zl {R : Type} (Rg : CRing R) (a : Int) : ∀ n, Int.natAbs a ≤ n → sumK Rg (fun x => delta Rg x a) (zl n) = Rg.one
  | 0, h => by
    have : a = 0 := GaussEuclid.natAbs_zero a (Nat.le_antisymm h (Nat.zero_le _))
    show Rg.add (delta Rg 0 a) Rg.zero = Rg.one
    rw [this]; unfold delta; rw [ite_pos' _ _ rfl, add_zero']
  | k + 1, h => by
    show sumK Rg (fun x => delta Rg x a) (zl k ++ [Int.ofNat (k + 1), Int.negSucc k]) = Rg.one
    rw [sumK_append]
    show Rg.add _ (Rg.add (delta Rg (Int.ofNat (k + 1)) a) (Rg.add (delta Rg (Int.negSucc k) a) Rg.zero)) = Rg.one
    cases Nat.lt_or_ge (Int.natAbs a) (k + 1) with
    | inl hl =>
      rw [count_zl Rg a k (Nat.le_of_lt_succ hl)]
      have h1 : ¬ Int.ofNat (k + 1) = a := fun e => by rw [← e] at hl; exact Nat.lt_irrefl _ hl
      have h2 : ¬ Int.negSucc k = a := fun e => by rw [← e] at hl; exact Nat.lt_irrefl _ hl
      unfold delta; rw [ite_neg' _ _ h1, ite_neg' _ _ h2]; lib_ring
    | inr hg =>
      have he : Int.natAbs a = k + 1 := Nat.le_antisymm h hg
      have h0 : sumK Rg (fun x => delta Rg x a) (zl k) = Rg.zero := zero_count Rg a k (by rw [he]; exact Nat.lt_succ_self k)
      rw [h0]
      cases a with
      | ofNat t =>
        have ht : t = k + 1 := he
        rw [ht]; unfold delta; rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e => Int.noConfusion e)]; lib_ring
      | negSucc t =>
        have ht : t = k := Nat.succ.inj he
        rw [ht]; unfold delta; rw [ite_neg' _ _ (fun e => Int.noConfusion e), ite_pos' _ _ rfl]; lib_ring
where
  zero_count {R : Type} (Rg : CRing R) (a : Int) : ∀ n, n < Int.natAbs a → sumK Rg (fun x => delta Rg x a) (zl n) = Rg.zero
  | 0, h => by
    show Rg.add (delta Rg 0 a) Rg.zero = Rg.zero
    have : ¬ (0 : Int) = a := fun e => by rw [← e] at h; exact Nat.lt_irrefl 0 h
    unfold delta; rw [ite_neg' _ _ this]; lib_ring
  | k + 1, h => by
    show sumK Rg (fun x => delta Rg x a) (zl k ++ [Int.ofNat (k + 1), Int.negSucc k]) = Rg.zero
    rw [sumK_append, zero_count Rg a k (Nat.lt_of_succ_lt h)]
    show Rg.add Rg.zero (Rg.add (delta Rg (Int.ofNat (k + 1)) a) (Rg.add (delta Rg (Int.negSucc k) a) Rg.zero)) = Rg.zero
    have h1 : ¬ Int.ofNat (k + 1) = a := fun e => by rw [← e] at h; exact Nat.lt_irrefl _ h
    have h2 : ¬ Int.negSucc k = a := fun e => by rw [← e] at h; exact Nat.lt_irrefl _ h
    unfold delta; rw [ite_neg' _ _ h1, ite_neg' _ _ h2]; lib_ring

def pairs : List Int → List Int → List (Int × Int)
  | [], _ => []
  | a :: as, bs => bs.map (fun b => (a, b)) ++ pairs as bs

theorem sumK_pairs {R : Type} (Rg : CRing R) (f : Int × Int → R) (bs : List Int) : ∀ as : List Int,
    sumK Rg f (pairs as bs) = sumK Rg (fun a => sumK Rg (fun b => f (a, b)) bs) as
  | [] => rfl
  | a :: as => by
    show sumK Rg f (bs.map (fun b => (a, b)) ++ pairs as bs) = Rg.add _ _
    rw [sumK_append, sumK_map, sumK_pairs Rg f bs as]

def box (n : Nat) : List (Int × Int) := pairs (zl n) (zl n)

theorem delta_pair {R : Type} (Rg : CRing R) (x y : Int × Int) : delta Rg x y = Rg.mul (delta Rg x.1 y.1) (delta Rg x.2 y.2) := by
  unfold delta
  by_cases h1 : x.1 = y.1
  · by_cases h2 : x.2 = y.2
    · rw [ite_pos' _ _ (Prod.ext h1 h2), ite_pos' _ _ h1, ite_pos' _ _ h2]; lib_ring
    · rw [ite_neg' _ _ (fun e => h2 (congrArg Prod.snd e)), ite_pos' _ _ h1, ite_neg' _ _ h2]; lib_ring
  · rw [ite_neg' _ _ (fun e => h1 (congrArg Prod.fst e)), ite_neg' _ _ h1]; lib_ring

theorem count_box {R : Type} (Rg : CRing R) (z : Int × Int) (n : Nat) (h1 : Int.natAbs z.1 ≤ n) (h2 : Int.natAbs z.2 ≤ n) :
    sumK Rg (fun x => delta Rg x z) (box n) = Rg.one := by
  unfold box; rw [sumK_pairs]
  have e : sumK Rg (fun a => sumK Rg (fun b => delta Rg (a, b) z) (zl n)) (zl n)
      = sumK Rg (fun a => Rg.mul (delta Rg a z.1) (sumK Rg (fun b => delta Rg b z.2) (zl n))) (zl n) :=
    sumK_ext Rg _ _ (fun a => by rw [mul_sumK]; exact sumK_ext Rg _ _ (fun b => delta_pair Rg (a, b) z) _) _
  rw [e, sumK_ext Rg _ _ (fun a => by rw [count_zl Rg z.2 n h2]), ← sumK_mul, count_zl Rg z.1 n h1]; lib_ring

theorem pick_box {R : Type} (Rg : CRing R) (F : Int × Int → R) (z : Int × Int) (n : Nat) (h1 : Int.natAbs z.1 ≤ n) (h2 : Int.natAbs z.2 ≤ n) :
    sumK Rg (fun x => Rg.mul (delta Rg x z) (F x)) (box n) = F z := by
  rw [delta_sum, count_box Rg z n h1 h2, Rg.one_mul]

theorem le_sq (a : Nat) : a ≤ a * a := by
  cases a with
  | zero => exact Nat.le_refl 0
  | succ k => exact Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.succ_le_succ (Nat.zero_le k)))

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ n. -/
theorem abs_le (z : Int × Int) (n : Nat) (h : gn z = Int.ofNat n) : Int.natAbs z.1 ≤ n ∧ Int.natAbs z.2 ≤ n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  constructor
  · exact Nat.le_trans (le_sq _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq hn))
  · exact Nat.le_trans (le_sq _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq hn))

theorem natAbs_negOfNat (j : Nat) : Int.natAbs (Int.negOfNat j) = j := by
  cases j with
  | zero => rfl
  | succ j => rfl

theorem idv_iff (k : Nat) (x : Int) : IDv (Int.ofNat (k + 1)) x ↔ ZMod.md k (Int.natAbs x) = 0 := by
  constructor
  · intro ⟨c, hc⟩
    apply ZMod.dv_md_zero k
    refine ⟨Int.natAbs c, ?_⟩
    rw [hc]
    cases c with
    | ofNat t => show (k + 1) * t = t * (k + 1); lib_nat
    | negSucc t => show Int.natAbs (Int.negOfNat ((k + 1) * (t + 1))) = (t + 1) * (k + 1); rw [natAbs_negOfNat]; lib_nat
  · intro h
    match ZMod.md_zero_dv k _ h with
    | ⟨c, hc⟩ =>
      cases x with
      | ofNat t => exact ⟨Int.ofNat c, by show Int.ofNat t = Int.ofNat ((k + 1) * c); exact congrArg Int.ofNat (hc.trans (by lib_nat))⟩
      | negSucc t =>
        refine ⟨-Int.ofNat c, ?_⟩
        have e : Int.ofNat (t + 1) = Int.ofNat (k + 1) * Int.ofNat c := congrArg Int.ofNat (hc.trans (by lib_nat))
        show -Int.ofNat (t + 1) = _
        rw [e]; lib_int

def PD (n : Nat) (z : Int × Int) : Prop := gn z = Int.ofNat n ∧ Prim z

theorem pd_iff (n : Nat) (z : Int × Int) :
    (gn z = Int.ofNat n ∧ (ZMod.md 1 (Int.natAbs z.2) = 0 ∧ ZMod.md 3 (Int.natAbs (z.1 + z.2 - 1)) = 0)) ↔ PD n z := by
  constructor
  · intro ⟨h1, h2, h3⟩
    exact ⟨h1, (idv_iff 1 z.2).mpr h2, (idv_iff 3 (z.1 + z.2 - 1)).mpr h3⟩
  · intro ⟨h1, h2, h3⟩
    exact ⟨h1, (idv_iff 1 z.2).mp h2, (idv_iff 3 (z.1 + z.2 - 1)).mp h3⟩

instance (n : Nat) (z : Int × Int) : Decidable (PD n z) := decidable_of_iff _ (pd_iff n z)

theorem zsum {A : Type} : ∀ l : List A, sumK (Cyclo4.cyc4 LibInt.intR) (fun _ => (Cyclo4.cyc4 LibInt.intR).zero) l = (Cyclo4.cyc4 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).zero (sumK (Cyclo4.cyc4 LibInt.intR) (fun _ => (Cyclo4.cyc4 LibInt.intR).zero) l) = _; rw [zsum l]; exact (Cyclo4.cyc4 LibInt.intR).zero_add _

def I (n : Nat) (z : Int × Int) : Int × Int := if PD n z then (Cyclo4.cyc4 LibInt.intR).one else (Cyclo4.cyc4 LibInt.intR).zero

/-- c_n = Σ_{α primary, N(α) = n} α. -/
def c (n : Nat) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun z => (Cyclo4.cyc4 LibInt.intR).mul (I n z) z) (box n)

theorem I_pos (n : Nat) (z : Int × Int) (h : PD n z) : I n z = (Cyclo4.cyc4 LibInt.intR).one := ite_pos' _ _ h
theorem I_neg (n : Nat) (z : Int × Int) (h : ¬ PD n z) : I n z = (Cyclo4.cyc4 LibInt.intR).zero := ite_neg' _ _ h

/-- **c_{mn} = c_m c_n** for coprime odd m, n. -/
theorem hecke_mult (m n : Nat) (s t km kn : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : Int.ofNat (m + 1) = 2 * km + 1) (hn : Int.ofNat (n + 1) = 2 * kn + 1) :
    c ((m + 1) * (n + 1)) = (Cyclo4.cyc4 LibInt.intR).mul (c (m + 1)) (c (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD (m + 1) α → PD (n + 1) β → PD ((m + 1) * (n + 1)) ((Cyclo4.cyc4 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [GaussEuclid.norm_mul, h1, h2]; rfl, prim_mulP _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Cyclo4.cyc4 LibInt.intR).mul (c (m + 1)) (c (n + 1))
      = sumK (Cyclo4.cyc4 LibInt.intR) (fun α => sumK (Cyclo4.cyc4 LibInt.intR) (fun β => sumK (Cyclo4.cyc4 LibInt.intR) (fun γ => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) γ) (box ((m + 1) * (n + 1)))) (box (n + 1))) (box (m + 1)) := by
    unfold c; rw [sumK_mul]
    refine sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD (m + 1) α
    · by_cases hβ : PD (n + 1) β
      · rw [I_pos _ _ hα, I_pos _ _ hβ]
        have hb := abs_le _ _ (hprod α β hα hβ).1
        have e := pick_box (Cyclo4.cyc4 LibInt.intR) (fun γ => γ) ((Cyclo4.cyc4 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Cyclo4.cyc4 LibInt.intR) (fun γ => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).one (Cyclo4.cyc4 LibInt.intR).one) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) γ) (box ((m + 1) * (n + 1)))
            = sumK (Cyclo4.cyc4 LibInt.intR) (fun γ => (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β)) γ) (box ((m + 1) * (n + 1))) from sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => by lib_ring) _, e]
        lib_ring
      · rw [I_neg _ _ hβ]
        rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (Cyclo4.cyc4 LibInt.intR).zero) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) γ = (Cyclo4.cyc4 LibInt.intR).zero by lib_ring)]
        rw [zsum]; lib_ring
    · rw [I_neg _ _ hα]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) γ = (Cyclo4.cyc4 LibInt.intR).zero by lib_ring)]
      rw [zsum]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => sumK_swap (Cyclo4.cyc4 LibInt.intR) _ _ _)]
  rw [sumK_swap (Cyclo4.cyc4 LibInt.intR)]
  unfold c
  refine sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun α => sumK (Cyclo4.cyc4 LibInt.intR) (fun β => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) γ) (box (n + 1))) (box (m + 1))
      = (Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (fun α => sumK (Cyclo4.cyc4 LibInt.intR) (fun β => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (box (n + 1))) (box (m + 1))) γ := by
    rw [sumK_mul]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD ((m + 1) * (n + 1)) γ
  · rw [I_pos _ _ hγ]
    match prim_split γ m n s t km kn hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β)) = (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (delta (Cyclo4.cyc4 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD (m + 1) α ∧ PD (n + 1) β ∧ γ = (Cyclo4.cyc4 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I_pos _ _ hα, I_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (delta (Cyclo4.cyc4 LibInt.intR) β β0) = (Cyclo4.cyc4 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD (m + 1) α
            · by_cases hβ : PD (n + 1) β
              · have hg : ¬ γ = (Cyclo4.cyc4 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I_neg _ _ hβ]; lib_ring
            · rw [I_neg _ _ hα]; lib_ring
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le α0 _ nα0
      have bb := abs_le β0 _ nβ0
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, count_box (Cyclo4.cyc4 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => show (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (Cyclo4.cyc4 LibInt.intR).one from rfl), ← sumK_mul,
        count_box (Cyclo4.cyc4 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I_neg _ _ hγ]
    have pt : ∀ α β, (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β)) = (Cyclo4.cyc4 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD (m + 1) α
      · by_cases hβ : PD (n + 1) β
        · have hg : ¬ γ = (Cyclo4.cyc4 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I_neg _ _ hβ]; lib_ring
      · rw [I_neg _ _ hα]; lib_ring
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => zsum _), zsum]

end HeckeMult

#print axioms HeckeMult.count_zl
#print axioms HeckeMult.sumK_pairs
#print axioms HeckeMult.delta_pair
#print axioms HeckeMult.count_box
#print axioms HeckeMult.pick_box
#print axioms HeckeMult.abs_le
#print axioms HeckeMult.idv_iff
#print axioms HeckeMult.pd_iff
#print axioms HeckeMult.c
#print axioms HeckeMult.hecke_mult

#print axioms: 15 theorems, no axioms.

78.55 Euler product for the Hecke character of \(\mathbb Q(i)\)

For pairwise coprime odd \(n_1,\dots ,n_r\) the coefficients multiply; with the prime-power values this is the Euler product.

\[ c_{n_1\cdots n_r}=c_{n_1}\cdots c_{n_r},\qquad c_1=1. \]

lang/HeckeEuler.lean

/-
  lang/HeckeEuler — G3 (T43): the Euler product of the theta series of the Hecke character of ℚ(i), coefficientwise for all odd n.

    c_one       c_1 = 1
    mult2       c_{mn} = c_m c_n for coprime odd m, n ≥ 1                     (HeckeMult.hecke_mult with Bezout from lib/Gcd)
    euler       for odd, pairwise coprime n₁, …, n_r:  c_{n₁⋯n_r} = c_{n₁} ⋯ c_{n_r}
  Taking the nᵢ to be the prime powers of n, c_n = ∏ c_{p^e}, with c_{p^e} from HeckePrime (split p: Σ_{j+l=e} π^j π̄^l, the
  recursion c_{p^{e+2}} = 2a c_{p^{e+1}} − p c_{p^e}) and HeckeInert (inert q: (−q)^{e/2} or 0): the Euler product
  L(θ_ψ, s) = ∏_{p split} (1 − 2a_p p^{−s} + p^{1−2s})^{−1} ∏_{q inert} (1 + q^{1−2s})^{−1} over odd n.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.HeckeInert

namespace HeckeEuler

open FTAChain DetChain LibInt GaussEuclid GaussInt GaussFactor GaussUnique HeckeMult YMChain FiniteRecChain LibGcd

theorem c_one : c 1 = (Cyclo4.cyc4 LibInt.intR).one := by
  have hI : ∀ z, I 1 z = delta (Cyclo4.cyc4 LibInt.intR) z (Cyclo4.cyc4 LibInt.intR).one := fun z => by
    by_cases hz : PD 1 z
    · rw [I_pos _ _ hz]
      have hu : IsUnit (toGI z) := norm_one _ hz.1
      have h1 := GaussCube.unit_primary (toGI z) gone hu GaussCube.prim_one (by rw [HeckeInert.gone_right]; exact hz.2)
      unfold delta; rw [ite_pos' _ _ (toGI_inj z (Cyclo4.cyc4 LibInt.intR).one h1)]
    · rw [I_neg _ _ hz]
      unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact ⟨rfl, GaussCube.prim_one⟩))]
  unfold c
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun z => by rw [hI z])]
  exact pick_box (Cyclo4.cyc4 LibInt.intR) (fun z => z) (Cyclo4.cyc4 LibInt.intR).one 1 (Nat.le_refl 1) (Nat.zero_le 1)

def Cop (m n : Nat) : Prop := ∀ d, d ∣ m → d ∣ n → d = 1
def Odd' (n : Nat) : Prop := ∃ k, Int.ofNat n = 2 * k + 1

/-- **c_{mn} = c_m c_n** for coprime odd m, n. -/
theorem mult2 (m n : Nat) (hmo : Odd' m) (hno : Odd' n) (hc : Cop m n) : c (m * n) = (Cyclo4.cyc4 LibInt.intR).mul (c m) (c n) := by
  match m, n, hmo, hno with
  | 0, _, ⟨k, hk⟩, _ => exact absurd (show (0 : Int) = 2 * k + 1 from hk) (fun e => GaussInt.int_odd_ne_even k 0 (e.symm.trans rfl))
  | _, 0, _, ⟨k, hk⟩ => exact absurd (show (0 : Int) = 2 * k + 1 from hk) (fun e => GaussInt.int_odd_ne_even k 0 (e.symm.trans rfl))
  | m' + 1, n' + 1, ⟨km, hm⟩, ⟨kn, hn⟩ =>
    match coprime_bezout hc with
    | ⟨s, t, hst⟩ => exact hecke_mult m' n' s t km kn hst hm hn

def prodN : List Nat → Nat
  | [] => 1
  | n :: ns => n * prodN ns

def prodE : List (Int × Int) → Int × Int
  | [] => (Cyclo4.cyc4 LibInt.intR).one
  | z :: zs => (Cyclo4.cyc4 LibInt.intR).mul z (prodE zs)

def Good : List Nat → Prop
  | [] => True
  | n :: ns => Odd' n ∧ Cop n (prodN ns) ∧ Good ns

theorem odd_mul (m n : Nat) (hm : Odd' m) (hn : Odd' n) : Odd' (m * n) :=
  match hm, hn with
  | ⟨a, ha⟩, ⟨b, hb⟩ => ⟨2 * a * b + a + b, by
      show Int.ofNat m * Int.ofNat n = _; rw [ha, hb]; lib_int⟩

theorem good_odd : ∀ ns : List Nat, Good ns → Odd' (prodN ns)
  | [], _ => ⟨0, rfl⟩
  | _ :: ns, ⟨hn, _, hg⟩ => odd_mul _ _ hn (good_odd ns hg)

/-- **The Euler product, coefficientwise**: c_{n₁⋯n_r} = c_{n₁}⋯c_{n_r} for odd pairwise coprime nᵢ. -/
theorem euler : ∀ ns : List Nat, Good ns → c (prodN ns) = prodE (ns.map c)
  | [], _ => c_one
  | n :: ns, ⟨hn, hc, hg⟩ => by
    show c (n * prodN ns) = (Cyclo4.cyc4 LibInt.intR).mul (c n) (prodE (ns.map c))
    rw [mult2 n _ hn (good_odd ns hg) hc, euler ns hg]

end HeckeEuler

#print axioms HeckeEuler.c_one
#print axioms HeckeEuler.mult2
#print axioms HeckeEuler.odd_mul
#print axioms HeckeEuler.good_odd
#print axioms HeckeEuler.euler

#print axioms: 5 theorems, no axioms.

78.56 Cubic character of \(2\)

Cubic reciprocity at the inert prime \(q=2\) gives \(\chi (2)=J\) in \(\mathbb F_2[\omega ]\), and \(J=a+b\omega \) with \(3\mid b\).

\[ 2 \text { a cube mod } p \;\Longleftrightarrow \; p=x^2+27y^2 . \]

lang/CubicTwo.lean

/-
  lang/CubicTwo — T43, cycle 216: Gauss's cubic character of 2.

  p = 6n + 7, χ the cubic character of 𝔽_p, J = J(χ, χ) = a + bω (N(J) = p, J ≡ −1 mod 3). CubicRecip at the inert prime q = 2
  (k = 0) says χ(2) = J in 𝔽₂[ω]. If 2 is a cube mod p, J ≡ 1 (mod 2), so 2 | b; with 3 | b (JacobiPrimary), b = 6t and
      p = a² − ab + b² = (a − 3t)² + 27t²:
    cast_zero_gen    castZ x = 0 in ℤ/(m + 1) ⇒ (m + 1) | x
    chi_two          φ(χ(2)) = φ(J) in 𝔽₂[ω]
    cubic_two        2 a cube mod p ⇒ p = x² + 27y²
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.CubicRecip
import lang.HeckeMult
import lang.StrongMult1
import lang.JacobiPrimary
import lang.EisenUnique
import lang.EisenAp
import lang.CubeAp

namespace CubicTwo

open FTAChain ZMod DetChain Frobenius CubicChar JacobiSum JacobiNorm CubicRecip LibGcd
open LibRing (castZ castN)

theorem cast_zero_gen (m : Nat) (x : Int) (h : castZ (ring m) x = (ring m).zero) : IDv (Int.ofNat (m + 1)) x := by
  apply (HeckeMult.idv_iff m x).mpr
  cases x with
  | ofNat t =>
    have : mk m t = (ring m).zero := (IntCast.castN_mk _ t).symm.trans h
    exact congrArg Subtype.val this
  | negSucc t =>
    have h1 : (ring m).neg (mk m (t + 1)) = (ring m).zero := by rw [← IntCast.castN_mk]; exact h
    have h2 : mk m (t + 1) = (ring m).zero := by
      calc mk m (t + 1) = (ring m).neg ((ring m).neg (mk m (t + 1))) := by lib_ring
        _ = (ring m).zero := by rw [h1]; lib_ring
    exact congrArg Subtype.val h2

variable (n : Nat)

theorem two_ne (_hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) :
    nsm (ring (6 * n + 5 + 1)) (0 + 1 + 1) (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero := fun e => by
  apply Eisen.two_ne_zero' (6 * n + 5) (by rw [show 6 * n + 5 = (6 * n + 3) + 2 by lib_nat]; exact Nat.le_add_left 2 _)
  rw [← e]; show _ = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero (ring (6 * n + 5 + 1)).one) (ring (6 * n + 5 + 1)).one
  lib_ring

/-- **χ(2) = J in 𝔽₂[ω].** -/
theorem chi_two (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1))
    (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    ph (ring (3 * 0 + 1)) (chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * 0 + 1 + 1) (ring (6 * n + 5 + 1)).one)) = ph (ring (3 * 0 + 1)) (J n w) := by
  rw [cubic_recip_inert n 0 hpr (fun d h => StrongMult1.two_prime d h) w hw1 hw3 (two_ne n hpr)]
  show (Cyclo3.cyc3 (ring (3 * 0 + 1))).mul ((Cyclo3.cyc3 (ring (3 * 0 + 1))).mul (Cyclo3.cyc3 (ring (3 * 0 + 1))).one (ph (ring (3 * 0 + 1)) (J n w))) (Cyclo3.cyc3 (ring (3 * 0 + 1))).one = _
  lib_ring

/-- **Gauss**: if 2 is a cube mod p ≡ 1 (mod 3), then p = x² + 27y². -/
theorem cubic_two (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1))
    (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one)
    (h2 : chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * 0 + 1 + 1) (ring (6 * n + 5 + 1)).one) = (1, 0)) :
    ∃ x y : Int, x * x + 27 * (y * y) = Int.ofNat (6 * n + 5 + 1 + 1) := by
  have hc := chi_two n hpr w hw1 hw3
  rw [h2] at hc
  have hb2 : castZ (ring (3 * 0 + 1)) (J n w).2 = (ring (3 * 0 + 1)).zero := (congrArg Prod.snd hc).symm
  match cast_zero_gen (3 * 0 + 1) _ hb2, cast_zero_gen 2 _ (JacobiPrimary.jacobi_primary n hpr w hw1 hw3).2 with
  | ⟨u, hu⟩, ⟨v, hv⟩ =>
    have huv : 2 * u = 3 * v := by
      have e1 : (J n w).2 = 2 * u := hu
      have e2 : (J n w).2 = 3 * v := hv
      rw [← e1, ← e2]
    match GaussInt.int_even_odd v with
    | Or.inl ⟨t, ht⟩ =>
      have hb : (J n w).2 = 6 * t := by rw [show (J n w).2 = 3 * v from hv, ht]; lib_int
      refine ⟨(J n w).1 - 3 * t, t, ?_⟩
      rw [← jacobi_norm n hpr w hw1 hw3, hb]; lib_int
    | Or.inr ⟨t, ht⟩ =>
      exfalso
      exact GaussInt.int_odd_ne_even (3 * t + 1) u (by rw [show 2 * (3 * t + 1) + 1 = 3 * v by rw [ht]; lib_int, ← huv])

theorem not3 (B m k : Int) (h1 : B = 3 * k) (h2 : B = 3 * m + 2) : False :=
  EisenAp.three_ne_r (k - m) 2 (fun e => Nat.noConfusion e) (Nat.lt_succ_self 2) (show (3 : Int) * (k - m) = 2 by rw [show (3 : Int) * (k - m) = 3 * k - 3 * m by lib_int, ← h1, h2]; lib_int)

theorem cast_even (c : Int) : castZ (ring (3 * 0 + 1)) (2 * c) = (ring (3 * 0 + 1)).zero := by
  rw [HasseDavenport.castZ_mul, show (2 : Int) = Int.ofNat (0 + 1 + 1) from rfl, CubeAp.castP, zero_mul']

theorem cast_odd (c : Int) : castZ (ring (3 * 0 + 1)) (2 * c + 1) = (ring (3 * 0 + 1)).one := by
  rw [IntCast.castZ_add, cast_even]; show (ring (3 * 0 + 1)).add (ring (3 * 0 + 1)).zero ((ring (3 * 0 + 1)).add (ring (3 * 0 + 1)).one (ring (3 * 0 + 1)).zero) = _; lib_ring

/-- **Gauss, converse**: if p = x² + 27y² then 2 is a cube mod p. -/
theorem cubic_two_conv (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (x y : Int) (hxy : x * x + 27 * (y * y) = Int.ofNat (6 * n + 5 + 1 + 1)) :
    chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * 0 + 1 + 1) (ring (6 * n + 5 + 1)).one) = (1, 0) := by
  have hN := jacobi_norm n hpr w hw1 hw3
  have hP := JacobiPrimary.jacobi_primary n hpr w hw1 hw3
  generalize hJ : J n w = Jw at hN hP
  match Jw, hJ with
  | (a, b), hJ =>
  have hN' : a * a - a * b + b * b = Int.ofNat (6 * n + 5 + 1 + 1) := hN
  -- 3 | b, a ≡ −1 (mod 3)
  match cast_zero_gen 2 b hP.2, cast_zero_gen 2 (a + 1) (by rw [IntCast.castZ_add, hP.1]; show (ring 2).add _ ((ring 2).add (ring 2).one (ring 2).zero) = _; lib_ring) with
  | ⟨kb, hkb⟩, ⟨ka, hka⟩ =>
  have hb3 : b = 3 * kb := hkb
  have ha3 : a = 3 * (ka - 1) + 2 := by rw [show a = (a + 1) - 1 by lib_int, show a + 1 = 3 * ka from hka]; lib_int
  have hx3 : x * x + 3 * ((3 * y) * (3 * y)) = Int.ofNat (6 * n + 5 + 1 + 1) := by rw [← hxy]; lib_int
  -- an associate A + 2B₀ω with norm p forces 3 | B₀
  have assoc : ∀ A B0 : Int, A * A - A * (2 * B0) + (2 * B0) * (2 * B0) = Int.ofNat (6 * n + 5 + 1 + 1) → ∃ k, 2 * B0 = 3 * k := fun A B0 h => by
    have h1 : (A - B0) * (A - B0) + 3 * (B0 * B0) = Int.ofNat (6 * n + 5 + 1) + 1 := by rw [← show Int.ofNat (6 * n + 5 + 1 + 1) = Int.ofNat (6 * n + 5 + 1) + 1 from rfl, ← h]; lib_int
    have hu := EisenUnique.eisen_unique (6 * n + 5) hpr (fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj e)))) x (3 * y) (A - B0) B0 hx3 h1
    have e3 : 3 * (B0 * B0) = 3 * ((3 * y) * (3 * y)) := by
      have := h1.trans hx3.symm
      rw [hu] at this
      calc 3 * (B0 * B0) = (x * x + 3 * (B0 * B0)) - x * x := by lib_int
        _ = 3 * ((3 * y) * (3 * y)) := by rw [this]; lib_int
    have e4 : (B0 - 3 * y) * (B0 + 3 * y) = 0 := by
      have : 3 * ((B0 - 3 * y) * (B0 + 3 * y)) = 0 := by rw [show 3 * ((B0 - 3 * y) * (B0 + 3 * y)) = 3 * (B0 * B0) - 3 * ((3 * y) * (3 * y)) by lib_int, e3]; lib_int
      cases LibInt.int_noZD _ _ this with
      | inl h => exact absurd h (fun e => Nat.noConfusion (Int.ofNat.inj e))
      | inr h => exact h
    cases LibInt.int_noZD _ _ e4 with
    | inl h => exact ⟨2 * y, by rw [show B0 = (B0 - 3 * y) + 3 * y by lib_int, show B0 - 3 * y = 0 from h]; lib_int⟩
    | inr h => exact ⟨-2 * y, by rw [show B0 = (B0 + 3 * y) - 3 * y by lib_int, show B0 + 3 * y = 0 from h]; lib_int⟩
  -- b is even
  have hbe : ∃ c, b = 2 * c := by
    match GaussInt.int_even_odd b with
    | Or.inl h => exact h
    | Or.inr ⟨c, hc⟩ =>
      exfalso
      match GaussInt.int_even_odd a with
      | Or.inr ⟨d, hd⟩ =>
        -- ωJ = −b + (a − b)ω, a − b even
        match assoc (-b) (d - c) (by rw [show 2 * (d - c) = a - b by rw [hd, hc]; lib_int, ← hN']; lib_int) with
        | ⟨k, hk⟩ => exact not3 (2 * (d - c)) (ka - 1 - kb) k hk (by rw [show 2 * (d - c) = a - b by rw [hd, hc]; lib_int, ha3, hb3]; lib_int)
      | Or.inl ⟨d, hd⟩ =>
        -- ω²J = (b − a) + (−a)ω, −a even
        match assoc (b - a) (-d) (by rw [show 2 * (-d) = -a by rw [hd]; lib_int, ← hN']; lib_int) with
        | ⟨k, hk⟩ => exact not3 (-(2 * (-d))) (ka - 1) (-k) (by rw [hk]; lib_int) (by rw [show -(2 * (-d)) = a by rw [hd]; lib_int, ha3])
  match hbe with
  | ⟨c, hc⟩ =>
  have hao : ∃ d, a = 2 * d + 1 := by
    match GaussInt.int_even_odd a with
    | Or.inr h => exact h
    | Or.inl ⟨d, hd⟩ =>
      exfalso
      have hp' : Int.ofNat (6 * n + 5 + 1 + 1) = 2 * (3 * Int.ofNat n + 3) + 1 := by show 6 * Int.ofNat n + 5 + 1 + 1 = _; lib_int
      exact GaussInt.int_odd_ne_even (3 * Int.ofNat n + 3) (2 * d * d - 2 * d * c + 2 * c * c) (by rw [← hp', ← hN', hd, hc]; lib_int)
  match hao with
  | ⟨d, hd⟩ =>
  have hph : ph (ring (3 * 0 + 1)) (J n w) = (Cyclo3.cyc3 (ring (3 * 0 + 1))).one := by
    rw [hJ]; apply Prod.ext
    · show castZ (ring (3 * 0 + 1)) a = (ring (3 * 0 + 1)).one; rw [hd]; exact cast_odd d
    · show castZ (ring (3 * 0 + 1)) b = (ring (3 * 0 + 1)).zero; rw [hc]; exact cast_even c
  have hc2 := chi_two n hpr w hw1 hw3
  rw [hph] at hc2
  have h20 : nsm (ring (6 * n + 5 + 1)) (3 * 0 + 1 + 1) (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero := two_ne n hpr
  match exists_i n hpr w hw1 hw3 _ h20 with
  | ⟨i, hi, ei⟩ =>
    rw [val_of n hpr w hw1 hw3 _ h20 i hi ei] at hc2 ⊢
    match i, hi with
    | 0, _ => rfl
    | 1, _ => exact absurd (congrArg Prod.fst hc2) (fun e => Nat.noConfusion (congrArg Subtype.val e))
    | 2, _ => exact absurd (congrArg Prod.snd hc2) (fun e => Nat.noConfusion (congrArg Subtype.val e))
    | k + 3, hk => exact absurd hk (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) h))

end CubicTwo

#print axioms CubicTwo.cast_zero_gen
#print axioms CubicTwo.two_ne
#print axioms CubicTwo.chi_two
#print axioms CubicTwo.cubic_two
#print axioms CubicTwo.not3
#print axioms CubicTwo.cast_even
#print axioms CubicTwo.cast_odd
#print axioms CubicTwo.cubic_two_conv

#print axioms: 8 theorems, no axioms.

78.57 Points on the Fermat cubic

The number of points of \(x^3+y^3=1\) over \(\mathbb F_p\) is read off the cubic Jacobi sum.

\[ \#\{x^3+y^3=1\}=p-2+(2a-b),\qquad (2a-b)^2+3b^2=4p . \]

lang/FermatCubic.lean

/-
  lang/FermatCubic — T43, cycle 218: points of the Fermat cubic x³ + y³ = 1 over 𝔽_p through the cubic Jacobi sum.

  p = 6n + 7, χ the cubic character, J = a + bω (N(J) = p). N(a) = #{x : x³ = a} = 1 + χ(a) + χ̄(a), so
      #{(x, y) : x³ + y³ = 1} = Σ_{a+b=1} N(a)N(b) = p − 2 + J + J̄ = p − 2 + 2a − b,
  and (2a − b)² + 3b² = 4p: the trace of Frobenius on the Fermat cubic is J + J̄ with |J + J̄| ≤ 2√p.
    cube_root, three_roots, cube_count, step1, step2, fermat_count, fermat_hasse
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.JacobiNorm
import lang.DLog
import lang.PrimRoot
import lang.HeckePrime

namespace FermatCubic

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm

variable (n : Nat)

/-- a^N = 1, a ≠ 0 ⇒ a is a cube. -/
theorem cube_root (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (a : Zp (6 * n + 5 + 1)) (ha0 : a ≠ (ring (6 * n + 5 + 1)).zero) (ha : pw (ring (6 * n + 5 + 1)) a (2 * n + 2) = (ring (6 * n + 5 + 1)).one) : ∃ c, (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul c c) c = a := by
  match PrimRoot.primitive_root (6 * n + 5) hpr with
  | ⟨g, hg⟩ =>
    match DLog.mem_pows (6 * n + 5) g a _ (DLog.every_unit (6 * n + 5) hpr g hg a ha0) with
    | ⟨k, _, hk⟩ =>
      have h1 : pw (ring (6 * n + 5 + 1)) g (k * (2 * n + 2)) = (ring (6 * n + 5 + 1)).one := by rw [← TwoPower.pwpow, ← hk, ha]
      match hg.2 _ h1 with
      | ⟨c, hc⟩ =>
        have h3 : k = 3 * c := by
          apply Nat.eq_of_mul_eq_mul_right (Nat.zero_lt_succ (2 * n + 1))
          rw [hc]; lib_nat
        refine ⟨pw (ring (6 * n + 5 + 1)) g c, ?_⟩
        rw [hk, h3, show 3 * c = c * 3 by lib_nat, ← TwoPower.pwpow]
        show _ = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one _) _) _
        lib_ring

def cube (x : Zp (6 * n + 5 + 1)) : Zp (6 * n + 5 + 1) := (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul x x) x

theorem cube_mul (x y : Zp (6 * n + 5 + 1)) : cube n ((ring (6 * n + 5 + 1)).mul x y) = (ring (6 * n + 5 + 1)).mul (cube n x) (cube n y) := by unfold cube; lib_ring

theorem w3' (w : Zp (6 * n + 5 + 1)) (_hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : cube n w = (ring (6 * n + 5 + 1)).one := by
  have : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w) w) w := rfl
  rw [this, (ring (6 * n + 5 + 1)).one_mul] at hw3; exact hw3

theorem cube_zero (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (x : Zp (6 * n + 5 + 1)) (h : cube n x = (ring (6 * n + 5 + 1)).zero) : x = (ring (6 * n + 5 + 1)).zero := by
  by_cases hx : x = (ring (6 * n + 5 + 1)).zero
  · exact hx
  · exfalso
    have h1 : (ring (6 * n + 5 + 1)).mul x x ≠ (ring (6 * n + 5 + 1)).zero := fun e => hx (cancel n hpr x x hx e)
    exact hx (cancel n hpr _ _ h1 (by unfold cube at h; exact h))

/-- the three cube roots of c³. -/
theorem three_roots (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) (x : Zp (6 * n + 5 + 1)) :
    delta (Cyclo3.cyc3 LibInt.intR) (cube n x) (cube n c) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (delta (Cyclo3.cyc3 LibInt.intR) x c) (delta (Cyclo3.cyc3 LibInt.intR) x ((ring (6 * n + 5 + 1)).mul c w))) (delta (Cyclo3.cyc3 LibInt.intR) x ((ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul w w))) := by
  have hw0 : w ≠ (ring (6 * n + 5 + 1)).zero := w_ne_zero n w hw1 hw3
  have d1 : c ≠ (ring (6 * n + 5 + 1)).mul c w := fun e => hw1 (by
    have := cancel n hpr c ((ring (6 * n + 5 + 1)).add w ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) hc (by rw [show (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).add w ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul c w) ((ring (6 * n + 5 + 1)).neg c) by lib_ring, ← e]; lib_ring)
    exact eq_of_sub n _ _ this)
  have d2 : c ≠ (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul w w) := fun e => ww_ne_one n w hw1 hw3 (by
    have := cancel n hpr c ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) hc (by rw [show (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul w w))  -- (+60 chars; full line in the Zenodo source)
    exact eq_of_sub n _ _ this)
  have d3 : (ring (6 * n + 5 + 1)).mul c w ≠ (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul w w) := fun e => ww_ne_w n hpr w hw1 hw3 (by
    have := cancel n hpr c ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).neg w)) hc (by rw [show (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul w w) ((ring (6 * n + 5 + 1)).neg w)) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul w w)) ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1  -- (+41 chars; full line in the Zenodo source)
    exact eq_of_sub n _ _ this)
  have hcw : cube n ((ring (6 * n + 5 + 1)).mul c w) = cube n c := by rw [cube_mul, w3' n w hw1 hw3]; lib_ring
  have hcww : cube n ((ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul w w)) = cube n c := by
    rw [cube_mul, cube_mul, w3' n w hw1 hw3]; lib_ring
  unfold delta
  by_cases hx : cube n x = cube n c
  · rw [ite_pos' _ _ hx]
    have hv := inv_exists (6 * n + 5) hpr c hc
    have ht : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5))) ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5)))) ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5))) = (ring (6 * n + 5 + 1)).one := by
      have : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5))) ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5)))) ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5)))
          = (ring (6 * n + 5 + 1)).mul (cube n x) (cube n (pw (ring (6 * n + 5 + 1)) c (6 * n + 5))) := by unfold cube; lib_ring
      rw [this, hx, ← cube_mul, (ring (6 * n + 5 + 1)).mul_comm, hv]; unfold cube; lib_ring
    have back : ∀ t, (ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5)) = t → x = (ring (6 * n + 5 + 1)).mul c t := fun t e => by
      rw [← e, show (ring (6 * n + 5 + 1)).mul c ((ring (6 * n + 5 + 1)).mul x (pw (ring (6 * n + 5 + 1)) c (6 * n + 5))) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (pw (ring (6 * n + 5 + 1)) c (6 * n + 5)) c) x by lib_ring, hv]; lib_ring
    rcases cube_cases n hpr w hw1 hw3 _ ht with h | h | h
    · have e := back _ h; rw [show (ring (6 * n + 5 + 1)).mul c (ring (6 * n + 5 + 1)).one = c by lib_ring] at e
      rw [ite_pos' _ _ e, ite_neg' _ _ (by rw [e]; exact d1), ite_neg' _ _ (by rw [e]; exact d2)]; lib_ring
    · have e := back _ h
      rw [ite_neg' _ _ (by rw [e]; exact fun e' => d1 e'.symm), ite_pos' _ _ e, ite_neg' _ _ (by rw [e]; exact d3)]; lib_ring
    · have e := back _ h
      rw [ite_neg' _ _ (by rw [e]; exact fun e' => d2 e'.symm), ite_neg' _ _ (by rw [e]; exact fun e' => d3 e'.symm), ite_pos' _ _ e]; lib_ring
  · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by rw [e])), ite_neg' _ _ (fun e => hx (by rw [e, hcw])),
      ite_neg' _ _ (fun e => hx (by rw [e, hcww]))]; lib_ring

theorem sum_delta1 (c : Zp (6 * n + 5 + 1)) : sumK (Cyclo3.cyc3 LibInt.intR) (fun x => delta (Cyclo3.cyc3 LibInt.intR) x c) (LL n) = (Cyclo3.cyc3 LibInt.intR).one := CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR) c

/-- **N(a) = #{x : x³ = a} = 1 + χ(a) + χ̄(a).** -/
theorem cube_count (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (a : Zp (6 * n + 5 + 1)) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun x => delta (Cyclo3.cyc3 LibInt.intR) (cube n x) a) (LL n) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (chi3 n w a)) (cb n w a) := by
  by_cases ha : a = (ring (6 * n + 5 + 1)).zero
  · rw [ha]
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun x => delta (Cyclo3.cyc3 LibInt.intR) x (ring (6 * n + 5 + 1)).zero) (fun x => by
      unfold delta
      by_cases hx : x = (ring (6 * n + 5 + 1)).zero
      · rw [ite_pos' _ _ hx, ite_pos' _ _ (by rw [hx]; unfold cube; lib_ring)]
      · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (cube_zero n hpr x e))]), sum_delta1]
    unfold cb; rw [chi3_zero]; show _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).zero) ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (Cyclo3.cyc3 LibInt.intR).zero); lib_ring
  · by_cases hN : pw (ring (6 * n + 5 + 1)) a (2 * n + 2) = (ring (6 * n + 5 + 1)).one
    · match cube_root n hpr a ha hN with
      | ⟨c, hc⟩ =>
        have hc0 : c ≠ (ring (6 * n + 5 + 1)).zero := fun e => ha (by rw [← hc, e]; lib_ring)
        have hc' : cube n c = a := hc
        rw [← hc']
        rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => three_roots n hpr w hw1 hw3 c hc0 x), sumK_add, sumK_add, sum_delta1, sum_delta1, sum_delta1]
        have h1 : chi3 n w (cube n c) = (Cyclo3.cyc3 LibInt.intR).one := by
          rw [← hc'] at hN; exact val_of n hpr w hw1 hw3 _ (by rw [hc']; exact ha) 0 (Nat.zero_lt_succ 2) hN
        unfold cb; rw [h1]; lib_ring
    · have hz : ∀ x, delta (Cyclo3.cyc3 LibInt.intR) (cube n x) a = (Cyclo3.cyc3 LibInt.intR).zero := fun x => by
        unfold delta
        rw [ite_neg' _ _ (fun e => hN (by
          have hx0 : x ≠ (ring (6 * n + 5 + 1)).zero := fun e0 => ha (by rw [← e, e0]; unfold cube; lib_ring)
          rw [← e, show cube n x = pw (ring (6 * n + 5 + 1)) x 3 by unfold cube; show _ = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one x) x) x; lib_ring,
            TwoPower.pwpow, show 3 * (2 * n + 2) = 6 * n + 5 + 1 by lib_nat]
          exact unit_pow (6 * n + 5) hpr x hx0))]
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hz, HeckePrime.zsumR (Cyclo3.cyc3 LibInt.intR)]
      match exists_i n hpr w hw1 hw3 a ha with
      | ⟨i, hi, ei⟩ =>
        unfold cb; rw [val_of n hpr w hw1 hw3 a ha i hi ei]
        match i, hi with
        | 0, _ => exact absurd ei hN
        | 1, _ => rfl
        | 2, _ => rfl
        | k + 3, hk => exact absurd hk (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) h))

def g3 (w a : Zp (6 * n + 5 + 1)) : Int × Int := (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (chi3 n w a)) (cb n w a)
def om1 (a : Zp (6 * n + 5 + 1)) : Zp (6 * n + 5 + 1) := (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg a)

/-- the number of points on x³ + y³ = 1 (in ℤ[ω], first component). -/
def Fc : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun x => sumK (Cyclo3.cyc3 LibInt.intR) (fun y => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).add (cube n x) (cube n y)) (ring (6 * n + 5 + 1)).one) (LL n)) (LL n)

theorem refl1 (G : Zp (6 * n + 5 + 1) → Int × Int) : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => G (om1 n a)) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) G (LL n) :=
  reindex (Cyclo3.cyc3 LibInt.intR) _ (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (om1 n) (om1 n) (fun x => by unfold om1; lib_ring) (fun y => by unfold om1; lib_ring) G

theorem step1 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (x : Zp (6 * n + 5 + 1)) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun y => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).add (cube n x) (cube n y)) (ring (6 * n + 5 + 1)).one) (LL n) = g3 n w (om1 n (cube n x)) := by
  unfold g3; rw [← cube_count n hpr w hw1 hw3]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun y => by
    unfold delta om1
    by_cases h : (ring (6 * n + 5 + 1)).add (cube n x) (cube n y) = (ring (6 * n + 5 + 1)).one
    · rw [ite_pos' _ _ h, ite_pos' _ _ (by rw [← h]; lib_ring)]
    · rw [ite_neg' _ _ h, ite_neg' _ _ (fun e => h (by rw [e]; lib_ring))]) _

theorem step2 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (h : Zp (6 * n + 5 + 1) → Int × Int) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun x => h (cube n x)) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (h a)) (LL n) := by
  have e1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun x => h (cube n x)) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun x => sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (cube n x)) (h a)) (LL n)) (LL n) :=
    sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => (pick (Cyclo3.cyc3 LibInt.intR) _ (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) h (cube n x)).symm) _
  rw [e1, sumK_swap]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by
    rw [← sumK_mul]
    unfold g3; rw [← cube_count n hpr w hw1 hw3 a]
    exact congrArg (fun z => (Cyclo3.cyc3 LibInt.intR).mul z (h a)) (sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => delta_symm (Cyclo3.cyc3 LibInt.intR) a (cube n x)) _)) _

/-- **#(x³ + y³ = 1) = p − 2 + J + J̄**, in ℤ[ω]. -/
theorem fermat_count (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    Fc n = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).add (J n w) (cconj (J n w)))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one)) := by
  unfold Fc
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => step1 n hpr w hw1 hw3 x), step2 n hpr w hw1 hw3 (fun a => g3 n w (om1 n a))]
  -- expand (1 + A + B)(1 + A' + B')
  have hexp : ∀ a, (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (g3 n w (om1 n a)) =
      (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w (om1 n a)) (cb n w (om1 n a)))) ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w a) (cb n w a)))
        ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w (om1 n a))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w (om1 n a)))))
        ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w (om1 n a))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w (om1 n a)))) := fun a => by unfold g3; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hexp]
  rw [sumK_add, sumK_add, sumK_add, sumK_add, sumK_add, sumK_add, sumK_add, sumK_add]
  have S1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun _ : Zp (6 * n + 5 + 1) => (Cyclo3.cyc3 LibInt.intR).one) (LL n) = (Int.ofNat (6 * n + 5 + 1 + 1), 0) := JacobiSum.sum_one n _
  have SA' : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => chi3 n w (om1 n a)) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := (refl1 n (chi3 n w)).trans (chi3_sum n hpr w hw1 hw3)
  have SB' : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => cb n w (om1 n a)) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := (refl1 n (cb n w)).trans (cb_sum n hpr w hw1 hw3)
  have SAA : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w (om1 n a))) (LL n) = J n w := rfl
  have SBB : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w (om1 n a))) (LL n) = cconj (J n w) := by
    rw [← J2_conj n hpr w hw1 hw3]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by rw [(cb_eq n hpr w hw1 hw3 a).1, (cb_eq n hpr w hw1 hw3 _).1]; rfl) _
  have SAB : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w (om1 n a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by
    rw [← JacobiSum.shift1 n (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w (om1 n a)))]
    rw [← D_ne n hpr w hw1 hw3 (ring (6 * n + 5 + 1)).one (PowerSum.one_ne_zero' _)]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun y => by
      show (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one y)) (cb n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one y)))) = _
      rw [show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one y)) = (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg y) by lib_ring, neg_b n hpr w hw1 hw3]) _
  have SBA : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w (om1 n a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by
    rw [← refl1 n (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w (om1 n a))), ← SAB]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by
      show (Cyclo3.cyc3 LibInt.intR).mul (cb n w (om1 n a)) (chi3 n w (om1 n (om1 n a))) = _
      rw [show om1 n (om1 n a) = a by unfold om1; lib_ring, (Cyclo3.cyc3 LibInt.intR).mul_comm]) _
  have SA : sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := chi3_sum n hpr w hw1 hw3
  rw [S1, SA', SB', SA, cb_sum n hpr w hw1 hw3, SAA, SBB, SAB, SBA]
  lib_ring

/-- **The Hasse bound for the Fermat cubic**: with J = a + bω, #(x³ + y³ = 1) = p − 2 + (2a − b) and (2a − b)² + 3b² = 4p. -/
theorem fermat_hasse (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    (Fc n).1 = Int.ofNat (6 * n + 5 + 1 + 1) + (2 * (J n w).1 - (J n w).2) - 2 ∧
    (2 * (J n w).1 - (J n w).2) * (2 * (J n w).1 - (J n w).2) + 3 * ((J n w).2 * (J n w).2) = 4 * Int.ofNat (6 * n + 5 + 1 + 1) := by
  refine ⟨?_, ?_⟩
  · rw [fermat_count n hpr w hw1 hw3]
    show Int.ofNat (6 * n + 5 + 1 + 1) + ((J n w).1 + ((J n w).1 - (J n w).2)) + -(1 + 1) = _
    lib_int
  · rw [← jacobi_norm n hpr w hw1 hw3]; lib_int

end FermatCubic

#print axioms FermatCubic.cube_root
#print axioms FermatCubic.cube_mul
#print axioms FermatCubic.w3'
#print axioms FermatCubic.cube_zero
#print axioms FermatCubic.three_roots
#print axioms FermatCubic.sum_delta1
#print axioms FermatCubic.cube_count
#print axioms FermatCubic.refl1
#print axioms FermatCubic.step1
#print axioms FermatCubic.step2
#print axioms FermatCubic.fermat_count
#print axioms FermatCubic.fermat_hasse

#print axioms: 12 theorems, no axioms.

78.58 Cubic characters of \(5\), \(7\), \(11\), \(13\)

Cubic reciprocity at \(q=5,7,11,13\) and a \(q^2\)-case table over \(\mathbb F_q\) give Jacobi’s criteria.

\[ q \text { a cube mod } p \;\Longleftrightarrow \; 5\mid LM,\ 7\mid LM,\ 11\mid LM(L^2-16M^2),\ 13\mid LM(L^2-M^2). \]

lang/CubicFive.lean

/-
  lang/CubicFive — T43, cycle 220: the cubic character of 5.

  p = 6n + 7, χ the cubic character of 𝔽_p, J = J(χ, χ) = a + bω, N(J) = a² − ab + b² = p. CubicRecip at the inert prime q = 5
  (k = 1) says χ(5) = J² p = J³ J̄ in 𝔽₅[ω]. Over 𝔽₅ (25 pairs (a, b)) J² (a² − ab + b²) = 1 iff b(2a − b) ≡ 0 (when
  a² − ab + b² ≢ 0), so with 4p = L² + 27M², L = 2a − b, b = 3M:
      5 is a cube mod p  ⇔  5 | b(2a − b)  ⇔  5 | LM:
    five_prime     5 is prime
    five_ne        5 ≠ 0 in 𝔽_p
    table5, table5'   the 25-case table over 𝔽₅ (both directions)
    cubic_five        χ(5) = 1 ⇒ 5 | b(2a − b)
    cubic_five_conv   5 | b(2a − b) ⇒ χ(5) = 1
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat; the table by rfl), no axioms.
-/
import lang.CubicTwo

namespace CubicFive

open FTAChain ZMod DetChain Frobenius CubicChar JacobiSum JacobiNorm CubicRecip LibGcd
open LibRing (castZ castN)

theorem ndv (m' q : Nat) (h : 5 = q * (m' + 1)) (hr : rem (m' + 1) 5 = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem five_prime : ∀ d, Dv d (3 * 1 + 1 + 1) → d = 1 ∨ d = 3 * 1 + 1 + 1 := fun d hd =>
  match dv_le d 4 hd, hd with
  | ⟨k, hk⟩, ⟨q, hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndv 1 q hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact (ndv 2 q hq (fun e => Nat.noConfusion e)).elim
    | 4 => exact (ndv 3 q hq (fun e => Nat.noConfusion e)).elim
    | 5 => exact Or.inr rfl
    | d + 6 => exact absurd hk (fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show d + 6 + k = d + k + 1 + 5 by lib_nat).symm.trans e)))))))

variable (n : Nat)

theorem five_ne : nsm (ring (6 * n + 5 + 1)) (3 * 1 + 1 + 1) (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 5 (by rw [show 6 * n + 5 + 1 + 1 = 5 + (6 * n + 1) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 5 _))] at h
  exact Nat.noConfusion h

abbrev F5 := ring (3 * 1 + 1)
abbrev C5 := Cyclo3.cyc3 F5

def lhs (X Y : Zp (3 * 1 + 1)) : Zp (3 * 1 + 1) × Zp (3 * 1 + 1) :=
  C5.mul (pw C5 (X, Y) (1 + 1)) (pw C5 (F5.add (F5.add (F5.mul X X) (F5.neg (F5.mul X Y))) (F5.mul Y Y), F5.zero) 1)
def rhs (X Y : Zp (3 * 1 + 1)) : Zp (3 * 1 + 1) := F5.mul Y (F5.add (F5.add X X) (F5.neg Y))

def code (z : Zp (3 * 1 + 1) × Zp (3 * 1 + 1)) : Nat := z.1.val + 5 * z.2.val
def nrm (X Y : Zp (3 * 1 + 1)) : Zp (3 * 1 + 1) := F5.add (F5.add (F5.mul X X) (F5.neg (F5.mul X Y))) (F5.mul Y Y)
def chk (X Y : Zp (3 * 1 + 1)) : Bool := (!(Nat.beq (code (lhs X Y)) 1) || Nat.beq (rhs X Y).val 0) &&
  (!(Nat.beq (rhs X Y).val 0) || Nat.beq (nrm X Y).val 0 || Nat.beq (code (lhs X Y)) 1)

theorem chk_nat : ∀ x y : Nat, x < 5 → y < 5 → chk (mk (3 * 1 + 1) x) (mk (3 * 1 + 1) y) = true
  | 0, 0, _, _ => rfl
  | 0, 1, _, _ => rfl
  | 0, 2, _, _ => rfl
  | 0, 3, _, _ => rfl
  | 0, 4, _, _ => rfl
  | 1, 0, _, _ => rfl
  | 1, 1, _, _ => rfl
  | 1, 2, _, _ => rfl
  | 1, 3, _, _ => rfl
  | 1, 4, _, _ => rfl
  | 2, 0, _, _ => rfl
  | 2, 1, _, _ => rfl
  | 2, 2, _, _ => rfl
  | 2, 3, _, _ => rfl
  | 2, 4, _, _ => rfl
  | 3, 0, _, _ => rfl
  | 3, 1, _, _ => rfl
  | 3, 2, _, _ => rfl
  | 3, 3, _, _ => rfl
  | 3, 4, _, _ => rfl
  | 4, 0, _, _ => rfl
  | 4, 1, _, _ => rfl
  | 4, 2, _, _ => rfl
  | 4, 3, _, _ => rfl
  | 4, 4, _, _ => rfl
  | k + 5, _, h, _ => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))
  | _, k + 5, _, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem mk_val (X : Zp (3 * 1 + 1)) : X = mk (3 * 1 + 1) X.val := Subtype.ext (md_small _ _ X.2).symm

theorem chk_all (X Y : Zp (3 * 1 + 1)) : chk X Y = true := by
  rw [mk_val X, mk_val Y]; exact chk_nat X.val Y.val X.2 Y.2

theorem beq_self : ∀ m : Nat, Nat.beq m m = true
  | 0 => rfl
  | m + 1 => beq_self m

theorem beq_eq : ∀ a b : Nat, Nat.beq a b = true → a = b
  | 0, 0, _ => rfl
  | 0, _ + 1, h => Bool.noConfusion h
  | _ + 1, 0, h => Bool.noConfusion h
  | a + 1, b + 1, h => congrArg Nat.succ (beq_eq a b h)

theorem band_l : ∀ u v : Bool, (u && v) = true → u = true
  | true, _, _ => rfl
  | false, _, h => h
theorem band_r : ∀ u v : Bool, (u && v) = true → v = true
  | true, _, h => h
  | false, _, h => Bool.noConfusion h

/-- **The table over 𝔽₅**: J² N(J) = 1 in 𝔽₅[ω] ⇒ b(2a − b) = 0. -/
theorem table5 (X Y : Zp (3 * 1 + 1)) (h : lhs X Y = C5.one) : rhs X Y = F5.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [h] at hc
  have e : rhs X Y = ⟨0, Nat.zero_lt_succ _⟩ := Subtype.ext (beq_eq _ _ hc)
  exact e

/-- **and back**: b(2a − b) = 0, N(J) ≠ 0 ⇒ J² N(J) = 1 in 𝔽₅[ω]. -/
theorem table5' (X Y : Zp (3 * 1 + 1)) (h : rhs X Y = F5.zero) (hn : nrm X Y ≠ F5.zero) : lhs X Y = C5.one := by
  have hc := band_r _ _ (chk_all X Y)
  rw [h] at hc
  cases hb : Nat.beq (nrm X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrm X Y = F5.zero) hn
  | false =>
    rw [hb] at hc
    have e := beq_eq _ _ hc
    match hl : lhs X Y, e with
    | (u, v), e =>
      have e' : u.val + 5 * v.val = 1 := e
      have hv : v.val = 0 := by
        cases hv' : v.val with
        | zero => rfl
        | succ t => rw [hv', show 5 * (t + 1) = 5 * t + 4 + 1 by lib_nat, show u.val + (5 * t + 4 + 1) = u.val + 5 * t + 4 + 1 by lib_nat] at e'; exact Nat.noConfusion (Nat.succ.inj e')
      have hu : u.val = 1 := by rw [hv] at e'; exact e'
      exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem cast_norm (x y : Int) : castZ F5 (x * x - x * y + y * y) = F5.add (F5.add (F5.mul (castZ F5 x) (castZ F5 x)) (F5.neg (F5.mul (castZ F5 x) (castZ F5 y)))) (F5.mul (castZ F5 y) (castZ F5 y)) := by
  rw [show x * x - x * y + y * y = x * x + -(x * y) + y * y from rfl, IntCast.castZ_add, IntCast.castZ_add, HasseDavenport.castZ_neg,
    HasseDavenport.castZ_mul, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]

theorem cast_rhs (x y : Int) : castZ F5 (y * (x + x - y)) = rhs (castZ F5 x) (castZ F5 y) := by
  rw [show y * (x + x - y) = y * (x + x + -y) from rfl, HasseDavenport.castZ_mul, IntCast.castZ_add, IntCast.castZ_add, HasseDavenport.castZ_neg]; rfl

/-- **5 a cube mod p ⇒ 5 | b(2a − b)**, J = a + bω (4p = (2a − b)² + 3b²). -/
theorem cubic_five (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1))
    (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one)
    (h5 : chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * 1 + 1 + 1) (ring (6 * n + 5 + 1)).one) = (1, 0)) :
    IDv (Int.ofNat 5) ((J n w).2 * ((J n w).1 + (J n w).1 - (J n w).2)) := by
  have hc := cubic_recip_inert n 1 hpr five_prime w hw1 hw3 (five_ne n)
  rw [h5] at hc
  have hP : ph F5 (Pe n) = (F5.add (F5.add (F5.mul (castZ F5 (J n w).1) (castZ F5 (J n w).1)) (F5.neg (F5.mul (castZ F5 (J n w).1) (castZ F5 (J n w).2)))) (F5.mul (castZ F5 (J n w).2) (castZ F5 (J n w).2)), F5.zero) := by
    unfold Pe; rw [← jacobi_norm n hpr w hw1 hw3]; unfold ph; rw [cast_norm]; rfl
  rw [hP] at hc
  have hl : lhs (castZ F5 (J n w).1) (castZ F5 (J n w).2) = C5.one := hc.symm.trans (ph_one F5)
  exact CubicTwo.cast_zero_gen (3 * 1 + 1) _ ((cast_rhs _ _).trans (table5 _ _ hl))

theorem not_dv5 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (h : Dv (3 * 1 + 1 + 1) (6 * n + 5 + 1 + 1)) : False :=
  match hpr 5 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (e.trans (show 6 * n + 5 + 1 + 1 = 6 * n + 2 + 5 by lib_nat)))))))

theorem chi_cases (w x : Zp (6 * n + 5 + 1)) : chi3 n w x = (0, 0) ∨ chi3 n w x = (1, 0) ∨ chi3 n w x = (0, 1) ∨ chi3 n w x = (-1, -1) := by
  unfold chi3
  by_cases h1 : x = (ring (6 * n + 5 + 1)).zero
  · exact Or.inl (ite_t _ _ h1)
  · rw [ite_f _ _ h1]
    by_cases h2 : pw (ring (6 * n + 5 + 1)) x (2 * n + 2) = (ring (6 * n + 5 + 1)).one
    · exact Or.inr (Or.inl (ite_t _ _ h2))
    · rw [ite_f _ _ h2]
      by_cases h3 : pw (ring (6 * n + 5 + 1)) x (2 * n + 2) = w
      · exact Or.inr (Or.inr (Or.inl (ite_t _ _ h3)))
      · exact Or.inr (Or.inr (Or.inr (ite_f _ _ h3)))

/-- **5 | b(2a − b) ⇒ 5 is a cube mod p.** -/
theorem cubic_five_conv (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1))
    (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one)
    (hd : IDv (Int.ofNat 5) ((J n w).2 * ((J n w).1 + (J n w).1 - (J n w).2))) :
    chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * 1 + 1 + 1) (ring (6 * n + 5 + 1)).one) = (1, 0) := by
  have hc := cubic_recip_inert n 1 hpr five_prime w hw1 hw3 (five_ne n)
  have hP : ph F5 (Pe n) = (nrm (castZ F5 (J n w).1) (castZ F5 (J n w).2), F5.zero) := by
    unfold Pe; rw [← jacobi_norm n hpr w hw1 hw3]; unfold ph; rw [cast_norm]; rfl
  rw [hP] at hc
  have hr : rhs (castZ F5 (J n w).1) (castZ F5 (J n w).2) = F5.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ => rw [hc', HasseDavenport.castZ_mul, show Int.ofNat 5 = Int.ofNat (3 + 1 + 1) from rfl, CubeAp.castP, zero_mul']
  have hn : nrm (castZ F5 (J n w).1) (castZ F5 (J n w).2) ≠ F5.zero := fun e => by
    have e1 : castZ F5 (Int.ofNat (6 * n + 5 + 1 + 1)) = F5.zero := by rw [← jacobi_norm n hpr w hw1 hw3, cast_norm]; exact e
    have e2 : mk (3 * 1 + 1) (6 * n + 5 + 1 + 1) = F5.zero := (IntCast.castN_mk _ _).symm.trans e1
    exact not_dv5 n hpr (md_zero_dv _ _ (congrArg Subtype.val e2))
  have h1 : ph F5 (chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * 1 + 1 + 1) (ring (6 * n + 5 + 1)).one)) = C5.one := hc.trans (table5' _ _ hr hn)
  rcases chi_cases n w (nsm (ring (6 * n + 5 + 1)) (3 * 1 + 1 + 1) (ring (6 * n + 5 + 1)).one) with e | e | e | e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion (Nat.succ.inj e'))

end CubicFive

#print axioms CubicFive.ndv
#print axioms CubicFive.five_prime
#print axioms CubicFive.five_ne
#print axioms CubicFive.chk_nat
#print axioms CubicFive.mk_val
#print axioms CubicFive.chk_all
#print axioms CubicFive.beq_self
#print axioms CubicFive.beq_eq
#print axioms CubicFive.band_l
#print axioms CubicFive.band_r
#print axioms CubicFive.table5
#print axioms CubicFive.table5'
#print axioms CubicFive.cast_norm
#print axioms CubicFive.cast_rhs
#print axioms CubicFive.cubic_five
#print axioms CubicFive.not_dv5
#print axioms CubicFive.chi_cases
#print axioms CubicFive.cubic_five_conv

lang/CubicSeven.lean

/-
  lang/CubicSeven — T43, cycle 221: the cubic character of 7.

  p = 6(n + 1) + 7 (so p ≠ 7), χ the cubic character of 𝔽_p, J = J(χ, χ) = a + bω, N(J) = a² − ab + b² = p. CubicRecipSplit at the
  split prime q = 7 (m = 1) says χ̄(7) = (J p)² in 𝔽₇[ω]. Over 𝔽₇ (49 pairs (a, b)) (J N(J))² = 1 iff b(2a − b) ≡ 0 (when
  N(J) ≢ 0), so with 4p = L² + 27M², L = 2a − b, b = 3M:
      7 is a cube mod p  ⇔  7 | b(2a − b)  ⇔  7 | LM:
    seven_prime, seven_ne, not_dv7
    table7, table7'     the 49-case table over 𝔽₇ (both directions)
    cubic_seven         χ(7) = 1 ⇒ 7 | b(2a − b)
    cubic_seven_conv    7 | b(2a − b) ⇒ χ(7) = 1
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.CubicFive
import lang.CubicRecipSplit

namespace CubicSeven

open FTAChain ZMod DetChain Frobenius CubicChar JacobiSum JacobiNorm CubicRecip CubicRecipSplit LibGcd CubicFive
open LibRing (castZ castN)

theorem ndv7 (m' q : Nat) (h : 7 = q * (m' + 1)) (hr : rem (m' + 1) 7 = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem seven_prime : ∀ d, Dv d (3 * 1 + 2 + 1 + 1) → d = 1 ∨ d = 3 * 1 + 2 + 1 + 1 := fun d hd =>
  match dv_le d 6 hd, hd with
  | ⟨k, hk⟩, ⟨q, hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndv7 1 q hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact (ndv7 2 q hq (fun e => Nat.noConfusion e)).elim
    | 4 => exact (ndv7 3 q hq (fun e => Nat.noConfusion e)).elim
    | 5 => exact (ndv7 4 q hq (fun e => Nat.noConfusion e)).elim
    | 6 => exact (ndv7 5 q hq (fun e => Nat.noConfusion e)).elim
    | 7 => exact Or.inr rfl
    | d + 8 => exact absurd hk (fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((show d + 8 + k = d + k + 1 + 7 by lib_nat).symm.trans e)))))))))

variable (n : Nat)

theorem seven_ne : nsm (ring (6 * (n + 1) + 5 + 1)) (3 * 1 + 2 + 1 + 1) (ring (6 * (n + 1) + 5 + 1)).one ≠ (ring (6 * (n + 1) + 5 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 7 (by rw [show 6 * (n + 1) + 5 + 1 + 1 = 7 + (6 * n + 5) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 7 _))] at h
  exact Nat.noConfusion h

theorem not_dv7 (hpr : ∀ d, Dv d (6 * (n + 1) + 5 + 1 + 1) → d = 1 ∨ d = 6 * (n + 1) + 5 + 1 + 1) (h : Dv (3 * 1 + 2 + 1 + 1) (6 * (n + 1) + 5 + 1 + 1)) : False :=
  match hpr 7 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (e.trans (show 6 * (n + 1) + 5 + 1 + 1 = 6 * n + 6 + 7 by lib_nat)))))))))

abbrev F7 := ring (3 * 1 + 2 + 1)
abbrev C7 := Cyclo3.cyc3 F7

def nrm (X Y : Zp (3 * 1 + 2 + 1)) : Zp (3 * 1 + 2 + 1) := F7.add (F7.add (F7.mul X X) (F7.neg (F7.mul X Y))) (F7.mul Y Y)
def lhs (X Y : Zp (3 * 1 + 2 + 1)) : Zp (3 * 1 + 2 + 1) × Zp (3 * 1 + 2 + 1) :=
  C7.mul (pw C7 (X, Y) (1 + 1)) (pw C7 (nrm X Y, F7.zero) (1 + 1))
def rhs (X Y : Zp (3 * 1 + 2 + 1)) : Zp (3 * 1 + 2 + 1) := F7.mul Y (F7.add (F7.add X X) (F7.neg Y))

def code (z : Zp (3 * 1 + 2 + 1) × Zp (3 * 1 + 2 + 1)) : Nat := z.1.val + 7 * z.2.val
def chk (X Y : Zp (3 * 1 + 2 + 1)) : Bool := (!(Nat.beq (code (lhs X Y)) 1) || Nat.beq (rhs X Y).val 0) &&
  (!(Nat.beq (rhs X Y).val 0) || Nat.beq (nrm X Y).val 0 || Nat.beq (code (lhs X Y)) 1)

theorem chk_nat : ∀ x y : Nat, x < 7 → y < 7 → chk (mk (3 * 1 + 2 + 1) x) (mk (3 * 1 + 2 + 1) y) = true
  | 0, 0, _, _ => rfl
  | 0, 1, _, _ => rfl
  | 0, 2, _, _ => rfl
  | 0, 3, _, _ => rfl
  | 0, 4, _, _ => rfl
  | 0, 5, _, _ => rfl
  | 0, 6, _, _ => rfl
  | 1, 0, _, _ => rfl
  | 1, 1, _, _ => rfl
  | 1, 2, _, _ => rfl
  | 1, 3, _, _ => rfl
  | 1, 4, _, _ => rfl
  | 1, 5, _, _ => rfl
  | 1, 6, _, _ => rfl
  | 2, 0, _, _ => rfl
  | 2, 1, _, _ => rfl
  | 2, 2, _, _ => rfl
  | 2, 3, _, _ => rfl
  | 2, 4, _, _ => rfl
  | 2, 5, _, _ => rfl
  | 2, 6, _, _ => rfl
  | 3, 0, _, _ => rfl
  | 3, 1, _, _ => rfl
  | 3, 2, _, _ => rfl
  | 3, 3, _, _ => rfl
  | 3, 4, _, _ => rfl
  | 3, 5, _, _ => rfl
  | 3, 6, _, _ => rfl
  | 4, 0, _, _ => rfl
  | 4, 1, _, _ => rfl
  | 4, 2, _, _ => rfl
  | 4, 3, _, _ => rfl
  | 4, 4, _, _ => rfl
  | 4, 5, _, _ => rfl
  | 4, 6, _, _ => rfl
  | 5, 0, _, _ => rfl
  | 5, 1, _, _ => rfl
  | 5, 2, _, _ => rfl
  | 5, 3, _, _ => rfl
  | 5, 4, _, _ => rfl
  | 5, 5, _, _ => rfl
  | 5, 6, _, _ => rfl
  | 6, 0, _, _ => rfl
  | 6, 1, _, _ => rfl
  | 6, 2, _, _ => rfl
  | 6, 3, _, _ => rfl
  | 6, 4, _, _ => rfl
  | 6, 5, _, _ => rfl
  | 6, 6, _, _ => rfl
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))
  | _, k + 7, _, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))
theorem chk_all (X Y : Zp (3 * 1 + 2 + 1)) : chk X Y = true := by
  rw [show X = mk (3 * 1 + 2 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (3 * 1 + 2 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

/-- **The table over 𝔽₇**: (J N(J))² = 1 in 𝔽₇[ω] ⇒ b(2a − b) = 0. -/
theorem table7 (X Y : Zp (3 * 1 + 2 + 1)) (h : lhs X Y = C7.one) : rhs X Y = F7.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [h] at hc
  exact (Subtype.ext (beq_eq _ _ hc) : rhs X Y = ⟨0, Nat.zero_lt_succ _⟩)

/-- **and back**: b(2a − b) = 0, N(J) ≠ 0 ⇒ (J N(J))² = 1 in 𝔽₇[ω]. -/
theorem table7' (X Y : Zp (3 * 1 + 2 + 1)) (h : rhs X Y = F7.zero) (hn : nrm X Y ≠ F7.zero) : lhs X Y = C7.one := by
  have hc := band_r _ _ (chk_all X Y)
  rw [h] at hc
  cases hb : Nat.beq (nrm X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrm X Y = F7.zero) hn
  | false =>
    rw [hb] at hc
    have e := beq_eq _ _ hc
    match lhs X Y, e with
    | (u, v), e =>
      have e' : u.val + 7 * v.val = 1 := e
      have hv : v.val = 0 := by
        cases hv' : v.val with
        | zero => rfl
        | succ t => rw [hv', show 7 * (t + 1) = 7 * t + 6 + 1 by lib_nat, show u.val + (7 * t + 6 + 1) = u.val + 7 * t + 6 + 1 by lib_nat] at e'; exact Nat.noConfusion (Nat.succ.inj e')
      have hu : u.val = 1 := by rw [hv] at e'; exact e'
      exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem cast_norm (x y : Int) : castZ F7 (x * x - x * y + y * y) = nrm (castZ F7 x) (castZ F7 y) := by
  rw [show x * x - x * y + y * y = x * x + -(x * y) + y * y from rfl, IntCast.castZ_add, IntCast.castZ_add, HasseDavenport.castZ_neg,
    HasseDavenport.castZ_mul, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F7 (y * (x + x - y)) = rhs (castZ F7 x) (castZ F7 y) := by
  rw [show y * (x + x - y) = y * (x + x + -y) from rfl, HasseDavenport.castZ_mul, IntCast.castZ_add, IntCast.castZ_add, HasseDavenport.castZ_neg]; rfl

theorem hP (hpr : ∀ d, Dv d (6 * (n + 1) + 5 + 1 + 1) → d = 1 ∨ d = 6 * (n + 1) + 5 + 1 + 1) (w : Zp (6 * (n + 1) + 5 + 1))
    (hw1 : w ≠ (ring (6 * (n + 1) + 5 + 1)).one) (hw3 : pw (ring (6 * (n + 1) + 5 + 1)) w 3 = (ring (6 * (n + 1) + 5 + 1)).one) :
    ph F7 (Pe (n + 1)) = (nrm (castZ F7 (J (n + 1) w).1) (castZ F7 (J (n + 1) w).2), F7.zero) := by
  unfold Pe; rw [← jacobi_norm (n + 1) hpr w hw1 hw3]; unfold ph; rw [cast_norm]; rfl

/-- **7 a cube mod p ⇒ 7 | b(2a − b)**, J = a + bω (4p = (2a − b)² + 3b²). -/
theorem cubic_seven (hpr : ∀ d, Dv d (6 * (n + 1) + 5 + 1 + 1) → d = 1 ∨ d = 6 * (n + 1) + 5 + 1 + 1) (w : Zp (6 * (n + 1) + 5 + 1))
    (hw1 : w ≠ (ring (6 * (n + 1) + 5 + 1)).one) (hw3 : pw (ring (6 * (n + 1) + 5 + 1)) w 3 = (ring (6 * (n + 1) + 5 + 1)).one)
    (h7 : chi3 (n + 1) w (nsm (ring (6 * (n + 1) + 5 + 1)) (3 * 1 + 2 + 1 + 1) (ring (6 * (n + 1) + 5 + 1)).one) = (1, 0)) :
    IDv (Int.ofNat 7) ((J (n + 1) w).2 * ((J (n + 1) w).1 + (J (n + 1) w).1 - (J (n + 1) w).2)) := by
  have hc := cubic_recip_split (n + 1) 1 hpr seven_prime w hw1 hw3 (seven_ne n)
  unfold cb at hc
  rw [h7, hP n hpr w hw1 hw3] at hc
  have hl : lhs (castZ F7 (J (n + 1) w).1) (castZ F7 (J (n + 1) w).2) = C7.one := hc.symm.trans (ph_one F7)
  exact CubicTwo.cast_zero_gen (3 * 1 + 2 + 1) _ ((cast_rhs _ _).trans (table7 _ _ hl))

/-- **7 | b(2a − b) ⇒ 7 is a cube mod p.** -/
theorem cubic_seven_conv (hpr : ∀ d, Dv d (6 * (n + 1) + 5 + 1 + 1) → d = 1 ∨ d = 6 * (n + 1) + 5 + 1 + 1) (w : Zp (6 * (n + 1) + 5 + 1))
    (hw1 : w ≠ (ring (6 * (n + 1) + 5 + 1)).one) (hw3 : pw (ring (6 * (n + 1) + 5 + 1)) w 3 = (ring (6 * (n + 1) + 5 + 1)).one)
    (hd : IDv (Int.ofNat 7) ((J (n + 1) w).2 * ((J (n + 1) w).1 + (J (n + 1) w).1 - (J (n + 1) w).2))) :
    chi3 (n + 1) w (nsm (ring (6 * (n + 1) + 5 + 1)) (3 * 1 + 2 + 1 + 1) (ring (6 * (n + 1) + 5 + 1)).one) = (1, 0) := by
  have hc := cubic_recip_split (n + 1) 1 hpr seven_prime w hw1 hw3 (seven_ne n)
  rw [hP n hpr w hw1 hw3] at hc
  have hr : rhs (castZ F7 (J (n + 1) w).1) (castZ F7 (J (n + 1) w).2) = F7.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F7 (Int.ofNat 7) = F7.zero := CubeAp.castP 5
      rw [h0, zero_mul']
  have hn : nrm (castZ F7 (J (n + 1) w).1) (castZ F7 (J (n + 1) w).2) ≠ F7.zero := fun e => by
    have e1 : castZ F7 (Int.ofNat (6 * (n + 1) + 5 + 1 + 1)) = F7.zero := by rw [← jacobi_norm (n + 1) hpr w hw1 hw3, cast_norm]; exact e
    have e2 : mk (3 * 1 + 2 + 1) (6 * (n + 1) + 5 + 1 + 1) = F7.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hd7 : ∀ P : Nat, mk (3 * 1 + 2 + 1) P = F7.zero → Dv (3 * 1 + 2 + 1 + 1) P := fun P h => md_zero_dv (3 * 1 + 2 + 1) P (congrArg Subtype.val h)
    exact not_dv7 n hpr (hd7 _ e2)
  have h1 : ph F7 (cb (n + 1) w (nsm (ring (6 * (n + 1) + 5 + 1)) (3 * 1 + 2 + 1 + 1) (ring (6 * (n + 1) + 5 + 1)).one)) = C7.one := hc.trans (table7' _ _ hr hn)
  unfold cb at h1
  rcases chi_cases (n + 1) w (nsm (ring (6 * (n + 1) + 5 + 1)) (3 * 1 + 2 + 1 + 1) (ring (6 * (n + 1) + 5 + 1)).one) with e | e | e | e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion (Nat.succ.inj e'))
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')

end CubicSeven

#print axioms CubicSeven.ndv7
#print axioms CubicSeven.seven_prime
#print axioms CubicSeven.seven_ne
#print axioms CubicSeven.not_dv7
#print axioms CubicSeven.chk_nat
#print axioms CubicSeven.chk_all
#print axioms CubicSeven.table7
#print axioms CubicSeven.table7'
#print axioms CubicSeven.cast_norm
#print axioms CubicSeven.cast_rhs
#print axioms CubicSeven.hP
#print axioms CubicSeven.cubic_seven
#print axioms CubicSeven.cubic_seven_conv

lang/CubicEleven.lean

/-
  lang/CubicEleven — T43: the cubic character of 11 (generated by work/gen_cubic_table.py).

  p = 6(n + 1) + 7 (so p > 11), χ the cubic character of 𝔽_p, J = J(χ, χ) = a + bω, N(J) = a² − ab + b² = p.
  CubicRecip at the inert prime q = 11 gives χ(q) = J^4 p^3 in 𝔽_11[ω], and a 121-case rfl table over 𝔽_11
  shows that this is 1 iff b(2a − b)·(9(2a − b)² − 16b²) ≡ 0 (when N(J) ≢ 0); with 4p = L² + 27M², L = 2a − b, b = 3M this is Jacobi's criterion
      11 is a cube mod p  ⇔  11 | b(2a − b)·(9(2a − b)² − 16b²).
    table, table'      the 121-case table over 𝔽_11 (both directions)
    cubic_eleven, cubic_eleven_conv
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.CubicSeven

namespace CubicEleven

open FTAChain ZMod DetChain Frobenius CubicChar JacobiSum JacobiNorm CubicRecip CubicRecipSplit LibGcd CubicFive
open LibRing (castZ castN)

theorem lt_wrap : ∀ q a : Nat, a + 1 + q = q → False
  | 0, _, h => Nat.noConfusion h
  | q + 1, a, h => lt_wrap q a (Nat.succ.inj h)

theorem ndvN (N m' q : Nat) (h : N = q * (m' + 1)) (hr : rem (m' + 1) N = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem q_prime : ∀ d, Dv d (3 * 3 + 1 + 1) → d = 1 ∨ d = 3 * 3 + 1 + 1 := fun d hd =>
  match dv_le d 10 hd, hd with
  | ⟨k, hk⟩, ⟨q', hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndvN 11 1 q' hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact (ndvN 11 2 q' hq (fun e => Nat.noConfusion e)).elim
    | 4 => exact (ndvN 11 3 q' hq (fun e => Nat.noConfusion e)).elim
    | 5 => exact (ndvN 11 4 q' hq (fun e => Nat.noConfusion e)).elim
    | 6 => exact (ndvN 11 5 q' hq (fun e => Nat.noConfusion e)).elim
    | 7 => exact (ndvN 11 6 q' hq (fun e => Nat.noConfusion e)).elim
    | 8 => exact (ndvN 11 7 q' hq (fun e => Nat.noConfusion e)).elim
    | 9 => exact (ndvN 11 8 q' hq (fun e => Nat.noConfusion e)).elim
    | 10 => exact (ndvN 11 9 q' hq (fun e => Nat.noConfusion e)).elim
    | 11 => exact Or.inr rfl
    | d + 12 => exact (lt_wrap 11 (d + k) ((show d + k + 1 + 11 = d + 12 + k by lib_nat).trans hk)).elim

variable (n : Nat)

theorem q_ne : nsm (ring (6 * (n + 1) + 5 + 1)) (3 * 3 + 1 + 1) (ring (6 * (n + 1) + 5 + 1)).one ≠ (ring (6 * (n + 1) + 5 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 11 (by rw [show 6 * (n + 1) + 5 + 1 + 1 = 11 + (6 * n + 1) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 11 _))] at h
  exact Nat.noConfusion h

theorem not_dvq (hpr : ∀ d, Dv d (6 * (n + 1) + 5 + 1 + 1) → d = 1 ∨ d = 6 * (n + 1) + 5 + 1 + 1) (h : Dv (3 * 3 + 1 + 1) (6 * (n + 1) + 5 + 1 + 1)) : False :=
  match hpr 11 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => lt_wrap 11 (6 * n + 1) ((show 6 * n + 1 + 1 + 11 = 6 * (n + 1) + 5 + 1 + 1 by lib_nat).trans e.symm)

abbrev F := ring (3 * 3 + 1)
abbrev C := Cyclo3.cyc3 F

def nrm (X Y : Zp (3 * 3 + 1)) : Zp (3 * 3 + 1) := F.add (F.add (F.mul X X) (F.neg (F.mul X Y))) (F.mul Y Y)
def lhs (X Y : Zp (3 * 3 + 1)) : Zp (3 * 3 + 1) × Zp (3 * 3 + 1) :=
  C.mul (pw C (X, Y) 4) (pw C (nrm X Y, F.zero) 3)
def Lf (X Y : Zp (3 * 3 + 1)) : Zp (3 * 3 + 1) := F.add (F.add X X) (F.neg Y)
def rhs (X Y : Zp (3 * 3 + 1)) : Zp (3 * 3 + 1) :=
  F.mul (F.mul Y (Lf X Y)) (F.add (F.mul (castZ F 9) (F.mul (Lf X Y) (Lf X Y))) (F.neg (F.mul (castZ F 16) (F.mul Y Y))))

-- fast arithmetic through the builtin Nat.mod, equal to F pointwise (tables proved by md_add_mul)
theorem md_red (a r k : Nat) (h : a = r + k * (3 * 3 + 1 + 1)) (hr : r < 3 * 3 + 1 + 1) : md (3 * 3 + 1) a = r := by
  rw [h, md_add_mul]; exact md_small _ r hr

def fm (a : Nat) : Zp (3 * 3 + 1) := ⟨a % 11, Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
def fadd (x y : Zp (3 * 3 + 1)) : Zp (3 * 3 + 1) := fm (x.val + y.val)
def fmul (x y : Zp (3 * 3 + 1)) : Zp (3 * 3 + 1) := fm (x.val * y.val)
def fneg (x : Zp (3 * 3 + 1)) : Zp (3 * 3 + 1) := fm ((3 * 3 + 1) * x.val)

theorem add_nat : ∀ x y : Nat, x < 11 → y < 11 → md (3 * 3 + 1) (x + y) = (x + y) % 11
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 0, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 0, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 0, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 0, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 0, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 0, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 0, 7, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 0, 8, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 0, 9, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 0, 10, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 1, 0, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 1, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 1, 2, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 1, 3, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 1, 4, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 1, 5, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 1, 6, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 1, 7, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 1, 8, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 1, 9, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 1, 10, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 2, 0, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 2, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 2, 3, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 2, 4, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 2, 5, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 2, 6, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 2, 7, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 2, 8, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 2, 9, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 2, 10, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 3, 0, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 3, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 3, 2, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 3, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 3, 4, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 3, 5, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 3, 6, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 3, 7, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 3, 8, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 3, 9, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 3, 10, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 4, 0, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 4, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 4, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 4, 3, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 4, 4, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 4, 5, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 4, 6, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 4, 7, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 4, 8, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 4, 9, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 4, 10, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 5, 0, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 5, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 5, 2, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 5, 3, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 5, 4, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 5, 5, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 5, 6, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 5, 7, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 5, 8, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 5, 9, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 5, 10, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 6, 0, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 6, 1, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 6, 2, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 6, 3, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 6, 4, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 6, 5, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 6, 6, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 6, 7, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 6, 8, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 6, 9, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 6, 10, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 7, 0, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 7, 1, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 7, 2, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 7, 3, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 7, 4, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 7, 5, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 7, 6, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 7, 7, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 7, 8, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 7, 9, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 7, 10, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 8, 0, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 8, 1, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 8, 2, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 8, 3, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 8, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 8, 5, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 8, 6, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 8, 7, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 8, 8, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 8, 9, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 8, 10, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 9, 0, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 9, 1, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 9, 2, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 9, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 9, 4, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 9, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 9, 6, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 9, 7, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 9, 8, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 9, 9, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 9, 10, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 10, 0, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 10, 1, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 10, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 10, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 10, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 10, 5, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 10, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 10, 7, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 10, 8, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 10, 9, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 10, 10, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | k + 11, _, h, _ => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))
  | _, k + 11, _, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem mul_nat : ∀ x y : Nat, x < 11 → y < 11 → md (3 * 3 + 1) (x * y) = (x * y) % 11
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 0, 1, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 0, 2, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 0, 3, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 0, 4, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 0, 5, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 0, 6, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 0, 7, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 0, 8, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 0, 9, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 0, 10, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 1, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 1, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 1, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 1, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 1, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 1, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 1, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 1, 7, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 1, 8, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 1, 9, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 1, 10, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 2, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 2, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 2, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 2, 4, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 2, 5, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 2, 6, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 2, 7, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 2, 8, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 2, 9, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 2, 10, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 3, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 3, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 3, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 3, 3, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 3, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 3, 5, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 3, 6, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 3, 7, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 3, 8, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 3, 9, _, _ => md_red _ 5 2 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 3, 10, _, _ => md_red _ 8 2 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 4, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 4, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 4, 2, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 4, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 4, 4, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 4, 5, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 4, 6, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 4, 7, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 4, 8, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 4, 9, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 4, 10, _, _ => md_red _ 7 3 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 5, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 5, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 5, 2, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 5, 3, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 5, 4, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 5, 5, _, _ => md_red _ 3 2 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 5, 6, _, _ => md_red _ 8 2 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 5, 7, _, _ => md_red _ 2 3 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 5, 8, _, _ => md_red _ 7 3 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 5, 9, _, _ => md_red _ 1 4 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 5, 10, _, _ => md_red _ 6 4 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 6, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 6, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 6, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 6, 3, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 6, 4, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 6, 5, _, _ => md_red _ 8 2 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 6, 6, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 6, 7, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 6, 8, _, _ => md_red _ 4 4 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 6, 9, _, _ => md_red _ 10 4 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 6, 10, _, _ => md_red _ 5 5 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 7, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 7, 1, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 7, 2, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 7, 3, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 7, 4, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 7, 5, _, _ => md_red _ 2 3 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 7, 6, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 7, 7, _, _ => md_red _ 5 4 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 7, 8, _, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 7, 9, _, _ => md_red _ 8 5 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 7, 10, _, _ => md_red _ 4 6 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 8, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 8, 1, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 8, 2, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 8, 3, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 8, 4, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 8, 5, _, _ => md_red _ 7 3 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 8, 6, _, _ => md_red _ 4 4 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 8, 7, _, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 8, 8, _, _ => md_red _ 9 5 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 8, 9, _, _ => md_red _ 6 6 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 8, 10, _, _ => md_red _ 3 7 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 9, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 9, 1, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 9, 2, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 9, 3, _, _ => md_red _ 5 2 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 9, 4, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 9, 5, _, _ => md_red _ 1 4 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | 9, 6, _, _ => md_red _ 10 4 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 9, 7, _, _ => md_red _ 8 5 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 9, 8, _, _ => md_red _ 6 6 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 9, 9, _, _ => md_red _ 4 7 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 9, 10, _, _ => md_red _ 2 8 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 10, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 10, 1, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 10, 2, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 10, 3, _, _ => md_red _ 8 2 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 10, 4, _, _ => md_red _ 7 3 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 10, 5, _, _ => md_red _ 6 4 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 10, 6, _, _ => md_red _ 5 5 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 10, 7, _, _ => md_red _ 4 6 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 10, 8, _, _ => md_red _ 3 7 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 10, 9, _, _ => md_red _ 2 8 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 10, 10, _, _ => md_red _ 1 9 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | k + 11, _, h, _ => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))
  | _, k + 11, _, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem neg_nat : ∀ x : Nat, x < 11 → md (3 * 3 + 1) ((3 * 3 + 1) * x) = ((3 * 3 + 1) * x) % 11
  | 0, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 10 : 0 < 3 * 3 + 1 + 1)
  | 1, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 0 : 10 < 3 * 3 + 1 + 1)
  | 2, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 1 : 9 < 3 * 3 + 1 + 1)
  | 3, _ => md_red _ 8 2 rfl (Nat.le_add_right 9 2 : 8 < 3 * 3 + 1 + 1)
  | 4, _ => md_red _ 7 3 rfl (Nat.le_add_right 8 3 : 7 < 3 * 3 + 1 + 1)
  | 5, _ => md_red _ 6 4 rfl (Nat.le_add_right 7 4 : 6 < 3 * 3 + 1 + 1)
  | 6, _ => md_red _ 5 5 rfl (Nat.le_add_right 6 5 : 5 < 3 * 3 + 1 + 1)
  | 7, _ => md_red _ 4 6 rfl (Nat.le_add_right 5 6 : 4 < 3 * 3 + 1 + 1)
  | 8, _ => md_red _ 3 7 rfl (Nat.le_add_right 4 7 : 3 < 3 * 3 + 1 + 1)
  | 9, _ => md_red _ 2 8 rfl (Nat.le_add_right 3 8 : 2 < 3 * 3 + 1 + 1)
  | 10, _ => md_red _ 1 9 rfl (Nat.le_add_right 2 9 : 1 < 3 * 3 + 1 + 1)
  | k + 11, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem addE (x y : Zp (3 * 3 + 1)) : F.add x y = fadd x y := Subtype.ext (add_nat x.val y.val x.2 y.2)
theorem mulE (x y : Zp (3 * 3 + 1)) : F.mul x y = fmul x y := Subtype.ext (mul_nat x.val y.val x.2 y.2)
theorem negE (x : Zp (3 * 3 + 1)) : F.neg x = fneg x := Subtype.ext (by show md (3 * 3 + 1) ((3 * 3 + 1) * x.val) = ((3 * 3 + 1) * x.val) % 11; exact neg_nat x.val x.2)

def c3f (u v : Zp (3 * 3 + 1) × Zp (3 * 3 + 1)) : Zp (3 * 3 + 1) × Zp (3 * 3 + 1) :=
  (fadd (fmul u.1 v.1) (fneg (fmul u.2 v.2)), fadd (fadd (fmul u.1 v.2) (fmul u.2 v.1)) (fneg (fmul u.2 v.2)))
theorem c3E (u v : Zp (3 * 3 + 1) × Zp (3 * 3 + 1)) : C.mul u v = c3f u v := by
  show Cyclo3.c3mul F u v = _
  unfold Cyclo3.c3mul
  rw [mulE, mulE, mulE, mulE, negE, addE, addE, addE]; rfl
def pwf (z : Zp (3 * 3 + 1) × Zp (3 * 3 + 1)) : Nat → Zp (3 * 3 + 1) × Zp (3 * 3 + 1)
  | 0 => C.one
  | k + 1 => c3f (pwf z k) z
theorem pwE (z : Zp (3 * 3 + 1) × Zp (3 * 3 + 1)) : ∀ k, pw C z k = pwf z k
  | 0 => rfl
  | k + 1 => by show C.mul (pw C z k) z = _; rw [c3E, pwE z k]; rfl

def nrmf (X Y : Zp (3 * 3 + 1)) : Zp (3 * 3 + 1) := fadd (fadd (fmul X X) (fneg (fmul X Y))) (fmul Y Y)
def lhsf (X Y : Zp (3 * 3 + 1)) : Zp (3 * 3 + 1) × Zp (3 * 3 + 1) := c3f (pwf (X, Y) 4) (pwf (nrmf X Y, F.zero) 3)
def Lff (X Y : Zp (3 * 3 + 1)) : Zp (3 * 3 + 1) := fadd (fadd X X) (fneg Y)
def rhsf (X Y : Zp (3 * 3 + 1)) : Zp (3 * 3 + 1) :=
  fmul (fmul Y (Lff X Y)) (fadd (fmul (castZ F 9) (fmul (Lff X Y) (Lff X Y))) (fneg (fmul (castZ F 16) (fmul Y Y))))

theorem nrmE (X Y : Zp (3 * 3 + 1)) : nrm X Y = nrmf X Y := by
  unfold nrm nrmf; rw [mulE, mulE, mulE, negE, addE, addE]
theorem lhsE (X Y : Zp (3 * 3 + 1)) : lhs X Y = lhsf X Y := by
  unfold lhs lhsf; rw [nrmE, pwE, pwE, c3E]
theorem rhsE (X Y : Zp (3 * 3 + 1)) : rhs X Y = rhsf X Y := by
  unfold rhs rhsf Lf Lff
  repeat (first | rw [mulE] | rw [addE] | rw [negE])

def code (z : Zp (3 * 3 + 1) × Zp (3 * 3 + 1)) : Nat := z.1.val + 11 * z.2.val
def chk (X Y : Zp (3 * 3 + 1)) : Bool := (!(Nat.beq (code (lhsf X Y)) 1) || Nat.beq (rhsf X Y).val 0) &&
  (!(Nat.beq (rhsf X Y).val 0) || Nat.beq (nrmf X Y).val 0 || Nat.beq (code (lhsf X Y)) 1)

theorem row0 : ∀ y : Nat, y < 11 → chk (mk (3 * 3 + 1) 0) (mk (3 * 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | k + 11, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem row1 : ∀ y : Nat, y < 11 → chk (mk (3 * 3 + 1) 1) (mk (3 * 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | k + 11, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem row2 : ∀ y : Nat, y < 11 → chk (mk (3 * 3 + 1) 2) (mk (3 * 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | k + 11, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem row3 : ∀ y : Nat, y < 11 → chk (mk (3 * 3 + 1) 3) (mk (3 * 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | k + 11, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem row4 : ∀ y : Nat, y < 11 → chk (mk (3 * 3 + 1) 4) (mk (3 * 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | k + 11, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem row5 : ∀ y : Nat, y < 11 → chk (mk (3 * 3 + 1) 5) (mk (3 * 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | k + 11, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem row6 : ∀ y : Nat, y < 11 → chk (mk (3 * 3 + 1) 6) (mk (3 * 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | k + 11, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem row7 : ∀ y : Nat, y < 11 → chk (mk (3 * 3 + 1) 7) (mk (3 * 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | k + 11, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem row8 : ∀ y : Nat, y < 11 → chk (mk (3 * 3 + 1) 8) (mk (3 * 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | k + 11, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem row9 : ∀ y : Nat, y < 11 → chk (mk (3 * 3 + 1) 9) (mk (3 * 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | k + 11, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem row10 : ∀ y : Nat, y < 11 → chk (mk (3 * 3 + 1) 10) (mk (3 * 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | k + 11, h => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem chk_nat : ∀ x y : Nat, x < 11 → y < 11 → chk (mk (3 * 3 + 1) x) (mk (3 * 3 + 1) y) = true
  | 0, y, _, h => row0 y h
  | 1, y, _, h => row1 y h
  | 2, y, _, h => row2 y h
  | 3, y, _, h => row3 y h
  | 4, y, _, h => row4 y h
  | 5, y, _, h => row5 y h
  | 6, y, _, h => row6 y h
  | 7, y, _, h => row7 y h
  | 8, y, _, h => row8 y h
  | 9, y, _, h => row9 y h
  | 10, y, _, h => row10 y h
  | k + 11, _, h, _ => absurd h (fun e => Nat.lt_irrefl 11 (Nat.lt_of_le_of_lt (Nat.le_add_left 11 k) e))

theorem chk_all (X Y : Zp (3 * 3 + 1)) : chk X Y = true := by
  rw [show X = mk (3 * 3 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (3 * 3 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

/-- **The table over 𝔽_11**, forward. -/
theorem table (X Y : Zp (3 * 3 + 1)) (h : lhs X Y = C.one) : rhs X Y = F.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [← lhsE, h] at hc
  rw [rhsE]
  exact (Subtype.ext (beq_eq _ _ hc) : rhsf X Y = ⟨0, Nat.zero_lt_succ _⟩)

/-- **The table over 𝔽_11**, backward (N(J) ≢ 0). -/
theorem table' (X Y : Zp (3 * 3 + 1)) (h : rhs X Y = F.zero) (hn : nrm X Y ≠ F.zero) : lhs X Y = C.one := by
  have hc := band_r _ _ (chk_all X Y)
  rw [← rhsE, h] at hc
  rw [lhsE]
  rw [nrmE] at hn
  cases hb : Nat.beq (nrmf X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrmf X Y = F.zero) hn
  | false =>
    rw [hb] at hc
    have e := beq_eq _ _ hc
    match lhsf X Y, e with
    | (u, v), e =>
      have e' : u.val + 11 * v.val = 1 := e
      have hv : v.val = 0 := by
        cases hv' : v.val with
        | zero => rfl
        | succ t => rw [hv', show 11 * (t + 1) = 11 * t + 10 + 1 by lib_nat, show u.val + (11 * t + 10 + 1) = u.val + 11 * t + 10 + 1 by lib_nat] at e'; exact Nat.noConfusion (Nat.succ.inj e')
      have hu : u.val = 1 := by rw [hv] at e'; exact e'
      exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem cast_norm (x y : Int) : castZ F (x * x - x * y + y * y) = nrm (castZ F x) (castZ F y) := by
  rw [show x * x - x * y + y * y = x * x + -(x * y) + y * y from rfl, IntCast.castZ_add, IntCast.castZ_add, HasseDavenport.castZ_neg,
    HasseDavenport.castZ_mul, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F (y * (x + x - y) * (9 * ((x + x - y) * (x + x - y)) - 16 * (y * y))) = rhs (castZ F x) (castZ F y) := by
  rw [show y * (x + x - y) * (9 * ((x + x - y) * (x + x - y)) - 16 * (y * y)) = y * (x + x + -y) * (9 * ((x + x + -y) * (x + x + -y)) + -(16 * (y * y))) from rfl,
    ]
  repeat (first | rw [HasseDavenport.castZ_mul] | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg])
  rfl

theorem hP (hpr : ∀ d, Dv d (6 * (n + 1) + 5 + 1 + 1) → d = 1 ∨ d = 6 * (n + 1) + 5 + 1 + 1) (w : Zp (6 * (n + 1) + 5 + 1))
    (hw1 : w ≠ (ring (6 * (n + 1) + 5 + 1)).one) (hw3 : pw (ring (6 * (n + 1) + 5 + 1)) w 3 = (ring (6 * (n + 1) + 5 + 1)).one) :
    ph F (Pe (n + 1)) = (nrm (castZ F (J (n + 1) w).1) (castZ F (J (n + 1) w).2), F.zero) := by
  unfold Pe; rw [← jacobi_norm (n + 1) hpr w hw1 hw3]; unfold ph; rw [cast_norm]; rfl

/-- **11 a cube mod p ⇒ 11 | b(2a − b)·(9(2a − b)² − 16b²)**. -/
theorem cubic_eleven (hpr : ∀ d, Dv d (6 * (n + 1) + 5 + 1 + 1) → d = 1 ∨ d = 6 * (n + 1) + 5 + 1 + 1) (w : Zp (6 * (n + 1) + 5 + 1))
    (hw1 : w ≠ (ring (6 * (n + 1) + 5 + 1)).one) (hw3 : pw (ring (6 * (n + 1) + 5 + 1)) w 3 = (ring (6 * (n + 1) + 5 + 1)).one)
    (hq : chi3 (n + 1) w (nsm (ring (6 * (n + 1) + 5 + 1)) (3 * 3 + 1 + 1) (ring (6 * (n + 1) + 5 + 1)).one) = (1, 0)) :
    IDv (Int.ofNat 11) ((J (n + 1) w).2 * ((J (n + 1) w).1 + (J (n + 1) w).1 - (J (n + 1) w).2) * (9 * (((J (n + 1) w).1 + (J (n + 1) w).1 - (J (n + 1) w).2) * ((J (n + 1) w).1 + (J (n + 1) w).1 - (J (n + 1) w).2)) - 16 * ((J (n + 1) w).2 * (J (n + 1) w).2))) := by
  have hc := cubic_recip_inert (n + 1) 3 hpr q_prime w hw1 hw3 (q_ne n)
  
  rw [hq, hP n hpr w hw1 hw3] at hc
  have hl : lhs (castZ F (J (n + 1) w).1) (castZ F (J (n + 1) w).2) = C.one := hc.symm.trans (ph_one F)
  exact CubicTwo.cast_zero_gen (3 * 3 + 1) _ ((cast_rhs _ _).trans (table _ _ hl))

/-- **11 | b(2a − b)·(9(2a − b)² − 16b²) ⇒ 11 is a cube mod p.** -/
theorem cubic_eleven_conv (hpr : ∀ d, Dv d (6 * (n + 1) + 5 + 1 + 1) → d = 1 ∨ d = 6 * (n + 1) + 5 + 1 + 1) (w : Zp (6 * (n + 1) + 5 + 1))
    (hw1 : w ≠ (ring (6 * (n + 1) + 5 + 1)).one) (hw3 : pw (ring (6 * (n + 1) + 5 + 1)) w 3 = (ring (6 * (n + 1) + 5 + 1)).one)
    (hd : IDv (Int.ofNat 11) ((J (n + 1) w).2 * ((J (n + 1) w).1 + (J (n + 1) w).1 - (J (n + 1) w).2) * (9 * (((J (n + 1) w).1 + (J (n + 1) w).1 - (J (n + 1) w).2) * ((J (n + 1) w).1 + (J (n + 1) w).1 - (J (n + 1) w).2)) - 16 * ((J (n + 1) w).2 * (J (n + 1) w).2)))) :
    chi3 (n + 1) w (nsm (ring (6 * (n + 1) + 5 + 1)) (3 * 3 + 1 + 1) (ring (6 * (n + 1) + 5 + 1)).one) = (1, 0) := by
  have hc := cubic_recip_inert (n + 1) 3 hpr q_prime w hw1 hw3 (q_ne n)
  rw [hP n hpr w hw1 hw3] at hc
  have hr : rhs (castZ F (J (n + 1) w).1) (castZ F (J (n + 1) w).2) = F.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F (Int.ofNat 11) = F.zero := CubeAp.castP 9
      rw [h0, zero_mul']
  have hn : nrm (castZ F (J (n + 1) w).1) (castZ F (J (n + 1) w).2) ≠ F.zero := fun e => by
    have e1 : castZ F (Int.ofNat (6 * (n + 1) + 5 + 1 + 1)) = F.zero := by rw [← jacobi_norm (n + 1) hpr w hw1 hw3, cast_norm]; exact e
    have e2 : mk (3 * 3 + 1) (6 * (n + 1) + 5 + 1 + 1) = F.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hdq : ∀ P' : Nat, mk (3 * 3 + 1) P' = F.zero → Dv (3 * 3 + 1 + 1) P' := fun P' h => md_zero_dv (3 * 3 + 1) P' (congrArg Subtype.val h)
    exact not_dvq n hpr (hdq _ e2)
  have h1 := hc.trans (table' _ _ hr hn)
  
  rcases chi_cases (n + 1) w (nsm (ring (6 * (n + 1) + 5 + 1)) (3 * 3 + 1 + 1) (ring (6 * (n + 1) + 5 + 1)).one) with e | e | e | e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion (Nat.succ.inj e'))

end CubicEleven
#print axioms CubicEleven.lt_wrap
#print axioms CubicEleven.md_red
#print axioms CubicEleven.add_nat
#print axioms CubicEleven.mul_nat
#print axioms CubicEleven.neg_nat
#print axioms CubicEleven.addE
#print axioms CubicEleven.mulE
#print axioms CubicEleven.negE
#print axioms CubicEleven.c3E
#print axioms CubicEleven.pwE
#print axioms CubicEleven.nrmE
#print axioms CubicEleven.lhsE
#print axioms CubicEleven.rhsE
#print axioms CubicEleven.row0
#print axioms CubicEleven.row1
#print axioms CubicEleven.row2
#print axioms CubicEleven.row3
#print axioms CubicEleven.row4
#print axioms CubicEleven.row5
#print axioms CubicEleven.row6
#print axioms CubicEleven.row7
#print axioms CubicEleven.row8
#print axioms CubicEleven.row9
#print axioms CubicEleven.row10
#print axioms CubicEleven.ndvN
#print axioms CubicEleven.q_prime
#print axioms CubicEleven.q_ne
#print axioms CubicEleven.not_dvq
#print axioms CubicEleven.chk_nat
#print axioms CubicEleven.chk_all
#print axioms CubicEleven.table
#print axioms CubicEleven.table'
#print axioms CubicEleven.cast_norm
#print axioms CubicEleven.cast_rhs
#print axioms CubicEleven.hP
#print axioms CubicEleven.cubic_eleven
#print axioms CubicEleven.cubic_eleven_conv

lang/CubicThirteen.lean

/-
  lang/CubicThirteen — T43: the cubic character of 13 (generated by work/gen_cubic_table.py).

  p = 6(n + 2) + 7 (so p > 13), χ the cubic character of 𝔽_p, J = J(χ, χ) = a + bω, N(J) = a² − ab + b² = p.
  CubicRecipSplit at the split prime q = 13 gives χ̄(q) = J^4 p^4 in 𝔽_13[ω], and a 169-case rfl table over 𝔽_13
  shows that this is 1 iff b(2a − b)·(9(2a − b)² − 1b²) ≡ 0 (when N(J) ≢ 0); with 4p = L² + 27M², L = 2a − b, b = 3M this is Jacobi's criterion
      13 is a cube mod p  ⇔  13 | b(2a − b)·(9(2a − b)² − 1b²).
    table, table'      the 169-case table over 𝔽_13 (both directions)
    cubic_thirteen, cubic_thirteen_conv
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.CubicSeven

namespace CubicThirteen

open FTAChain ZMod DetChain Frobenius CubicChar JacobiSum JacobiNorm CubicRecip CubicRecipSplit LibGcd CubicFive
open LibRing (castZ castN)

theorem lt_wrap : ∀ q a : Nat, a + 1 + q = q → False
  | 0, _, h => Nat.noConfusion h
  | q + 1, a, h => lt_wrap q a (Nat.succ.inj h)

theorem ndvN (N m' q : Nat) (h : N = q * (m' + 1)) (hr : rem (m' + 1) N = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem q_prime : ∀ d, Dv d (3 * 3 + 2 + 1 + 1) → d = 1 ∨ d = 3 * 3 + 2 + 1 + 1 := fun d hd =>
  match dv_le d 12 hd, hd with
  | ⟨k, hk⟩, ⟨q', hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndvN 13 1 q' hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact (ndvN 13 2 q' hq (fun e => Nat.noConfusion e)).elim
    | 4 => exact (ndvN 13 3 q' hq (fun e => Nat.noConfusion e)).elim
    | 5 => exact (ndvN 13 4 q' hq (fun e => Nat.noConfusion e)).elim
    | 6 => exact (ndvN 13 5 q' hq (fun e => Nat.noConfusion e)).elim
    | 7 => exact (ndvN 13 6 q' hq (fun e => Nat.noConfusion e)).elim
    | 8 => exact (ndvN 13 7 q' hq (fun e => Nat.noConfusion e)).elim
    | 9 => exact (ndvN 13 8 q' hq (fun e => Nat.noConfusion e)).elim
    | 10 => exact (ndvN 13 9 q' hq (fun e => Nat.noConfusion e)).elim
    | 11 => exact (ndvN 13 10 q' hq (fun e => Nat.noConfusion e)).elim
    | 12 => exact (ndvN 13 11 q' hq (fun e => Nat.noConfusion e)).elim
    | 13 => exact Or.inr rfl
    | d + 14 => exact (lt_wrap 13 (d + k) ((show d + k + 1 + 13 = d + 14 + k by lib_nat).trans hk)).elim

variable (n : Nat)

theorem q_ne : nsm (ring (6 * (n + 2) + 5 + 1)) (3 * 3 + 2 + 1 + 1) (ring (6 * (n + 2) + 5 + 1)).one ≠ (ring (6 * (n + 2) + 5 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 13 (by rw [show 6 * (n + 2) + 5 + 1 + 1 = 13 + (6 * n + 5) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 13 _))] at h
  exact Nat.noConfusion h

theorem not_dvq (hpr : ∀ d, Dv d (6 * (n + 2) + 5 + 1 + 1) → d = 1 ∨ d = 6 * (n + 2) + 5 + 1 + 1) (h : Dv (3 * 3 + 2 + 1 + 1) (6 * (n + 2) + 5 + 1 + 1)) : False :=
  match hpr 13 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => lt_wrap 13 (6 * n + 5) ((show 6 * n + 5 + 1 + 13 = 6 * (n + 2) + 5 + 1 + 1 by lib_nat).trans e.symm)

abbrev F := ring (3 * 3 + 2 + 1)
abbrev C := Cyclo3.cyc3 F

def nrm (X Y : Zp (3 * 3 + 2 + 1)) : Zp (3 * 3 + 2 + 1) := F.add (F.add (F.mul X X) (F.neg (F.mul X Y))) (F.mul Y Y)
def lhs (X Y : Zp (3 * 3 + 2 + 1)) : Zp (3 * 3 + 2 + 1) × Zp (3 * 3 + 2 + 1) :=
  C.mul (pw C (X, Y) 4) (pw C (nrm X Y, F.zero) 4)
def Lf (X Y : Zp (3 * 3 + 2 + 1)) : Zp (3 * 3 + 2 + 1) := F.add (F.add X X) (F.neg Y)
def rhs (X Y : Zp (3 * 3 + 2 + 1)) : Zp (3 * 3 + 2 + 1) :=
  F.mul (F.mul Y (Lf X Y)) (F.add (F.mul (castZ F 9) (F.mul (Lf X Y) (Lf X Y))) (F.neg (F.mul (castZ F 1) (F.mul Y Y))))

-- fast arithmetic through the builtin Nat.mod, equal to F pointwise (tables proved by md_add_mul)
theorem md_red (a r k : Nat) (h : a = r + k * (3 * 3 + 2 + 1 + 1)) (hr : r < 3 * 3 + 2 + 1 + 1) : md (3 * 3 + 2 + 1) a = r := by
  rw [h, md_add_mul]; exact md_small _ r hr

def fm (a : Nat) : Zp (3 * 3 + 2 + 1) := ⟨a % 13, Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
def fadd (x y : Zp (3 * 3 + 2 + 1)) : Zp (3 * 3 + 2 + 1) := fm (x.val + y.val)
def fmul (x y : Zp (3 * 3 + 2 + 1)) : Zp (3 * 3 + 2 + 1) := fm (x.val * y.val)
def fneg (x : Zp (3 * 3 + 2 + 1)) : Zp (3 * 3 + 2 + 1) := fm ((3 * 3 + 2 + 1) * x.val)

theorem add_nat : ∀ x y : Nat, x < 13 → y < 13 → md (3 * 3 + 2 + 1) (x + y) = (x + y) % 13
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 0, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 0, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 0, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 0, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 0, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 0, 7, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 0, 8, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 0, 9, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 0, 10, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 0, 11, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 0, 12, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 1, 0, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 1, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 1, 2, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 1, 3, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 1, 4, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 1, 5, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 1, 6, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 1, 7, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 1, 8, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 1, 9, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 1, 10, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 1, 11, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 1, 12, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 2, 0, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 2, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 2, 3, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 2, 4, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 2, 5, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 2, 6, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 2, 7, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 2, 8, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 2, 9, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 2, 10, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 2, 11, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 2, 12, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 3, 0, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 3, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 3, 2, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 3, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 3, 4, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 3, 5, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 3, 6, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 3, 7, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 3, 8, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 3, 9, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 3, 10, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 3, 11, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 3, 12, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 4, 0, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 4, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 4, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 4, 3, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 4, 4, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 4, 5, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 4, 6, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 4, 7, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 4, 8, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 4, 9, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 4, 10, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 4, 11, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 4, 12, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 5, 0, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 5, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 5, 2, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 5, 3, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 5, 4, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 5, 5, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 5, 6, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 5, 7, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 5, 8, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 5, 9, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 5, 10, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 5, 11, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 5, 12, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 6, 0, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 6, 1, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 6, 2, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 6, 3, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 6, 4, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 6, 5, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 6, 6, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 6, 7, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 6, 8, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 6, 9, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 6, 10, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 6, 11, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 6, 12, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 7, 0, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 7, 1, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 7, 2, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 7, 3, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 7, 4, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 7, 5, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 7, 6, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 7, 7, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 7, 8, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 7, 9, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 7, 10, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 7, 11, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 7, 12, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 8, 0, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 8, 1, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 8, 2, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 8, 3, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 8, 4, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 8, 5, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 8, 6, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 8, 7, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 8, 8, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 8, 9, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 8, 10, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 8, 11, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 8, 12, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 9, 0, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 9, 1, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 9, 2, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 9, 3, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 9, 4, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 9, 5, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 9, 6, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 9, 7, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 9, 8, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 9, 9, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 9, 10, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 9, 11, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 9, 12, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 10, 0, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 10, 1, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 10, 2, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 10, 3, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 10, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 10, 5, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 10, 6, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 10, 7, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 10, 8, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 10, 9, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 10, 10, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 10, 11, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 10, 12, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 11, 0, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 11, 1, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 11, 2, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 11, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 11, 4, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 11, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 11, 6, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 11, 7, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 11, 8, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 11, 9, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 11, 10, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 11, 11, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 11, 12, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 12, 0, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 12, 1, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 12, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 12, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 12, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 12, 5, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 12, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 12, 7, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 12, 8, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 12, 9, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 12, 10, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 12, 11, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 12, 12, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | k + 13, _, h, _ => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))
  | _, k + 13, _, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem mul_nat : ∀ x y : Nat, x < 13 → y < 13 → md (3 * 3 + 2 + 1) (x * y) = (x * y) % 13
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 1, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 2, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 3, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 4, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 5, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 6, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 7, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 8, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 9, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 10, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 11, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 0, 12, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 1, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 1, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 1, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 1, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 1, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 1, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 1, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 1, 7, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 1, 8, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 1, 9, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 1, 10, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 1, 11, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 1, 12, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 2, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 2, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 2, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 2, 4, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 2, 5, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 2, 6, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 2, 7, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 2, 8, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 2, 9, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 2, 10, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 2, 11, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 2, 12, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 3, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 3, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 3, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 3, 3, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 3, 4, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 3, 5, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 3, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 3, 7, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 3, 8, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 3, 9, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 3, 10, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 3, 11, _, _ => md_red _ 7 2 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 3, 12, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 4, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 4, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 4, 2, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 4, 3, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 4, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 4, 5, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 4, 6, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 4, 7, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 4, 8, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 4, 9, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 4, 10, _, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 4, 11, _, _ => md_red _ 5 3 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 4, 12, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 5, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 5, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 5, 2, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 5, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 5, 4, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 5, 5, _, _ => md_red _ 12 1 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 5, 6, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 5, 7, _, _ => md_red _ 9 2 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 5, 8, _, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 5, 9, _, _ => md_red _ 6 3 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 5, 10, _, _ => md_red _ 11 3 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 5, 11, _, _ => md_red _ 3 4 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 5, 12, _, _ => md_red _ 8 4 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 6, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 6, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 6, 2, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 6, 3, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 6, 4, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 6, 5, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 6, 6, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 6, 7, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 6, 8, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 6, 9, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 6, 10, _, _ => md_red _ 8 4 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 6, 11, _, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 6, 12, _, _ => md_red _ 7 5 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 7, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 7, 1, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 7, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 7, 3, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 7, 4, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 7, 5, _, _ => md_red _ 9 2 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 7, 6, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 7, 7, _, _ => md_red _ 10 3 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 7, 8, _, _ => md_red _ 4 4 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 7, 9, _, _ => md_red _ 11 4 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 7, 10, _, _ => md_red _ 5 5 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 7, 11, _, _ => md_red _ 12 5 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 7, 12, _, _ => md_red _ 6 6 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 8, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 8, 1, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 8, 2, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 8, 3, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 8, 4, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 8, 5, _, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 8, 6, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 8, 7, _, _ => md_red _ 4 4 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 8, 8, _, _ => md_red _ 12 4 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 8, 9, _, _ => md_red _ 7 5 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 8, 10, _, _ => md_red _ 2 6 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 8, 11, _, _ => md_red _ 10 6 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 8, 12, _, _ => md_red _ 5 7 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 9, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 9, 1, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 9, 2, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 9, 3, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 9, 4, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 9, 5, _, _ => md_red _ 6 3 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 9, 6, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 9, 7, _, _ => md_red _ 11 4 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 9, 8, _, _ => md_red _ 7 5 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 9, 9, _, _ => md_red _ 3 6 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 9, 10, _, _ => md_red _ 12 6 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 9, 11, _, _ => md_red _ 8 7 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 9, 12, _, _ => md_red _ 4 8 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 10, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 10, 1, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 10, 2, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 10, 3, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 10, 4, _, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 10, 5, _, _ => md_red _ 11 3 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 10, 6, _, _ => md_red _ 8 4 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 10, 7, _, _ => md_red _ 5 5 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 10, 8, _, _ => md_red _ 2 6 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 10, 9, _, _ => md_red _ 12 6 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 10, 10, _, _ => md_red _ 9 7 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 10, 11, _, _ => md_red _ 6 8 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 10, 12, _, _ => md_red _ 3 9 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 11, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 11, 1, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 11, 2, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 11, 3, _, _ => md_red _ 7 2 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 11, 4, _, _ => md_red _ 5 3 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 11, 5, _, _ => md_red _ 3 4 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 11, 6, _, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | 11, 7, _, _ => md_red _ 12 5 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 11, 8, _, _ => md_red _ 10 6 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 11, 9, _, _ => md_red _ 8 7 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 11, 10, _, _ => md_red _ 6 8 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 11, 11, _, _ => md_red _ 4 9 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 11, 12, _, _ => md_red _ 2 10 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 12, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 12, 1, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 12, 2, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 12, 3, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 12, 4, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 12, 5, _, _ => md_red _ 8 4 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 12, 6, _, _ => md_red _ 7 5 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 12, 7, _, _ => md_red _ 6 6 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 12, 8, _, _ => md_red _ 5 7 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 12, 9, _, _ => md_red _ 4 8 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 12, 10, _, _ => md_red _ 3 9 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 12, 11, _, _ => md_red _ 2 10 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 12, 12, _, _ => md_red _ 1 11 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | k + 13, _, h, _ => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))
  | _, k + 13, _, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem neg_nat : ∀ x : Nat, x < 13 → md (3 * 3 + 2 + 1) ((3 * 3 + 2 + 1) * x) = ((3 * 3 + 2 + 1) * x) % 13
  | 0, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 3 * 3 + 2 + 1 + 1)
  | 1, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 3 * 3 + 2 + 1 + 1)
  | 2, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 3 * 3 + 2 + 1 + 1)
  | 3, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 2 : 10 < 3 * 3 + 2 + 1 + 1)
  | 4, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 3 : 9 < 3 * 3 + 2 + 1 + 1)
  | 5, _ => md_red _ 8 4 rfl (Nat.le_add_right 9 4 : 8 < 3 * 3 + 2 + 1 + 1)
  | 6, _ => md_red _ 7 5 rfl (Nat.le_add_right 8 5 : 7 < 3 * 3 + 2 + 1 + 1)
  | 7, _ => md_red _ 6 6 rfl (Nat.le_add_right 7 6 : 6 < 3 * 3 + 2 + 1 + 1)
  | 8, _ => md_red _ 5 7 rfl (Nat.le_add_right 6 7 : 5 < 3 * 3 + 2 + 1 + 1)
  | 9, _ => md_red _ 4 8 rfl (Nat.le_add_right 5 8 : 4 < 3 * 3 + 2 + 1 + 1)
  | 10, _ => md_red _ 3 9 rfl (Nat.le_add_right 4 9 : 3 < 3 * 3 + 2 + 1 + 1)
  | 11, _ => md_red _ 2 10 rfl (Nat.le_add_right 3 10 : 2 < 3 * 3 + 2 + 1 + 1)
  | 12, _ => md_red _ 1 11 rfl (Nat.le_add_right 2 11 : 1 < 3 * 3 + 2 + 1 + 1)
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem addE (x y : Zp (3 * 3 + 2 + 1)) : F.add x y = fadd x y := Subtype.ext (add_nat x.val y.val x.2 y.2)
theorem mulE (x y : Zp (3 * 3 + 2 + 1)) : F.mul x y = fmul x y := Subtype.ext (mul_nat x.val y.val x.2 y.2)
theorem negE (x : Zp (3 * 3 + 2 + 1)) : F.neg x = fneg x := Subtype.ext (by show md (3 * 3 + 2 + 1) ((3 * 3 + 2 + 1) * x.val) = ((3 * 3 + 2 + 1) * x.val) % 13; exact neg_nat x.val x.2)

def c3f (u v : Zp (3 * 3 + 2 + 1) × Zp (3 * 3 + 2 + 1)) : Zp (3 * 3 + 2 + 1) × Zp (3 * 3 + 2 + 1) :=
  (fadd (fmul u.1 v.1) (fneg (fmul u.2 v.2)), fadd (fadd (fmul u.1 v.2) (fmul u.2 v.1)) (fneg (fmul u.2 v.2)))
theorem c3E (u v : Zp (3 * 3 + 2 + 1) × Zp (3 * 3 + 2 + 1)) : C.mul u v = c3f u v := by
  show Cyclo3.c3mul F u v = _
  unfold Cyclo3.c3mul
  rw [mulE, mulE, mulE, mulE, negE, addE, addE, addE]; rfl
def pwf (z : Zp (3 * 3 + 2 + 1) × Zp (3 * 3 + 2 + 1)) : Nat → Zp (3 * 3 + 2 + 1) × Zp (3 * 3 + 2 + 1)
  | 0 => C.one
  | k + 1 => c3f (pwf z k) z
theorem pwE (z : Zp (3 * 3 + 2 + 1) × Zp (3 * 3 + 2 + 1)) : ∀ k, pw C z k = pwf z k
  | 0 => rfl
  | k + 1 => by show C.mul (pw C z k) z = _; rw [c3E, pwE z k]; rfl

def nrmf (X Y : Zp (3 * 3 + 2 + 1)) : Zp (3 * 3 + 2 + 1) := fadd (fadd (fmul X X) (fneg (fmul X Y))) (fmul Y Y)
def lhsf (X Y : Zp (3 * 3 + 2 + 1)) : Zp (3 * 3 + 2 + 1) × Zp (3 * 3 + 2 + 1) := c3f (pwf (X, Y) 4) (pwf (nrmf X Y, F.zero) 4)
def Lff (X Y : Zp (3 * 3 + 2 + 1)) : Zp (3 * 3 + 2 + 1) := fadd (fadd X X) (fneg Y)
def rhsf (X Y : Zp (3 * 3 + 2 + 1)) : Zp (3 * 3 + 2 + 1) :=
  fmul (fmul Y (Lff X Y)) (fadd (fmul (castZ F 9) (fmul (Lff X Y) (Lff X Y))) (fneg (fmul (castZ F 1) (fmul Y Y))))

theorem nrmE (X Y : Zp (3 * 3 + 2 + 1)) : nrm X Y = nrmf X Y := by
  unfold nrm nrmf; rw [mulE, mulE, mulE, negE, addE, addE]
theorem lhsE (X Y : Zp (3 * 3 + 2 + 1)) : lhs X Y = lhsf X Y := by
  unfold lhs lhsf; rw [nrmE, pwE, pwE, c3E]
theorem rhsE (X Y : Zp (3 * 3 + 2 + 1)) : rhs X Y = rhsf X Y := by
  unfold rhs rhsf Lf Lff
  repeat (first | rw [mulE] | rw [addE] | rw [negE])

def code (z : Zp (3 * 3 + 2 + 1) × Zp (3 * 3 + 2 + 1)) : Nat := z.1.val + 13 * z.2.val
def chk (X Y : Zp (3 * 3 + 2 + 1)) : Bool := (!(Nat.beq (code (lhsf X Y)) 1) || Nat.beq (rhsf X Y).val 0) &&
  (!(Nat.beq (rhsf X Y).val 0) || Nat.beq (nrmf X Y).val 0 || Nat.beq (code (lhsf X Y)) 1)

theorem row0 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 0) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row1 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 1) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row2 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 2) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row3 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 3) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row4 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 4) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row5 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 5) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row6 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 6) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row7 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 7) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row8 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 8) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row9 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 9) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row10 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 10) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row11 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 11) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row12 : ∀ y : Nat, y < 13 → chk (mk (3 * 3 + 2 + 1) 12) (mk (3 * 3 + 2 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem chk_nat : ∀ x y : Nat, x < 13 → y < 13 → chk (mk (3 * 3 + 2 + 1) x) (mk (3 * 3 + 2 + 1) y) = true
  | 0, y, _, h => row0 y h
  | 1, y, _, h => row1 y h
  | 2, y, _, h => row2 y h
  | 3, y, _, h => row3 y h
  | 4, y, _, h => row4 y h
  | 5, y, _, h => row5 y h
  | 6, y, _, h => row6 y h
  | 7, y, _, h => row7 y h
  | 8, y, _, h => row8 y h
  | 9, y, _, h => row9 y h
  | 10, y, _, h => row10 y h
  | 11, y, _, h => row11 y h
  | 12, y, _, h => row12 y h
  | k + 13, _, h, _ => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem chk_all (X Y : Zp (3 * 3 + 2 + 1)) : chk X Y = true := by
  rw [show X = mk (3 * 3 + 2 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (3 * 3 + 2 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

/-- **The table over 𝔽_13**, forward. -/
theorem table (X Y : Zp (3 * 3 + 2 + 1)) (h : lhs X Y = C.one) : rhs X Y = F.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [← lhsE, h] at hc
  rw [rhsE]
  exact (Subtype.ext (beq_eq _ _ hc) : rhsf X Y = ⟨0, Nat.zero_lt_succ _⟩)

/-- **The table over 𝔽_13**, backward (N(J) ≢ 0). -/
theorem table' (X Y : Zp (3 * 3 + 2 + 1)) (h : rhs X Y = F.zero) (hn : nrm X Y ≠ F.zero) : lhs X Y = C.one := by
  have hc := band_r _ _ (chk_all X Y)
  rw [← rhsE, h] at hc
  rw [lhsE]
  rw [nrmE] at hn
  cases hb : Nat.beq (nrmf X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrmf X Y = F.zero) hn
  | false =>
    rw [hb] at hc
    have e := beq_eq _ _ hc
    match lhsf X Y, e with
    | (u, v), e =>
      have e' : u.val + 13 * v.val = 1 := e
      have hv : v.val = 0 := by
        cases hv' : v.val with
        | zero => rfl
        | succ t => rw [hv', show 13 * (t + 1) = 13 * t + 12 + 1 by lib_nat, show u.val + (13 * t + 12 + 1) = u.val + 13 * t + 12 + 1 by lib_nat] at e'; exact Nat.noConfusion (Nat.succ.inj e')
      have hu : u.val = 1 := by rw [hv] at e'; exact e'
      exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem cast_norm (x y : Int) : castZ F (x * x - x * y + y * y) = nrm (castZ F x) (castZ F y) := by
  rw [show x * x - x * y + y * y = x * x + -(x * y) + y * y from rfl, IntCast.castZ_add, IntCast.castZ_add, HasseDavenport.castZ_neg,
    HasseDavenport.castZ_mul, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F (y * (x + x - y) * (9 * ((x + x - y) * (x + x - y)) - 1 * (y * y))) = rhs (castZ F x) (castZ F y) := by
  rw [show y * (x + x - y) * (9 * ((x + x - y) * (x + x - y)) - 1 * (y * y)) = y * (x + x + -y) * (9 * ((x + x + -y) * (x + x + -y)) + -(1 * (y * y))) from rfl,
    ]
  repeat (first | rw [HasseDavenport.castZ_mul] | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg])
  rfl

theorem hP (hpr : ∀ d, Dv d (6 * (n + 2) + 5 + 1 + 1) → d = 1 ∨ d = 6 * (n + 2) + 5 + 1 + 1) (w : Zp (6 * (n + 2) + 5 + 1))
    (hw1 : w ≠ (ring (6 * (n + 2) + 5 + 1)).one) (hw3 : pw (ring (6 * (n + 2) + 5 + 1)) w 3 = (ring (6 * (n + 2) + 5 + 1)).one) :
    ph F (Pe (n + 2)) = (nrm (castZ F (J (n + 2) w).1) (castZ F (J (n + 2) w).2), F.zero) := by
  unfold Pe; rw [← jacobi_norm (n + 2) hpr w hw1 hw3]; unfold ph; rw [cast_norm]; rfl

/-- **13 a cube mod p ⇒ 13 | b(2a − b)·(9(2a − b)² − 1b²)**. -/
theorem cubic_thirteen (hpr : ∀ d, Dv d (6 * (n + 2) + 5 + 1 + 1) → d = 1 ∨ d = 6 * (n + 2) + 5 + 1 + 1) (w : Zp (6 * (n + 2) + 5 + 1))
    (hw1 : w ≠ (ring (6 * (n + 2) + 5 + 1)).one) (hw3 : pw (ring (6 * (n + 2) + 5 + 1)) w 3 = (ring (6 * (n + 2) + 5 + 1)).one)
    (hq : chi3 (n + 2) w (nsm (ring (6 * (n + 2) + 5 + 1)) (3 * 3 + 2 + 1 + 1) (ring (6 * (n + 2) + 5 + 1)).one) = (1, 0)) :
    IDv (Int.ofNat 13) ((J (n + 2) w).2 * ((J (n + 2) w).1 + (J (n + 2) w).1 - (J (n + 2) w).2) * (9 * (((J (n + 2) w).1 + (J (n + 2) w).1 - (J (n + 2) w).2) * ((J (n + 2) w).1 + (J (n + 2) w).1 - (J (n + 2) w).2)) - 1 * ((J (n + 2) w).2 * (J (n + 2) w).2))) := by
  have hc := cubic_recip_split (n + 2) 3 hpr q_prime w hw1 hw3 (q_ne n)
  unfold cb at hc
  rw [hq, hP n hpr w hw1 hw3] at hc
  have hl : lhs (castZ F (J (n + 2) w).1) (castZ F (J (n + 2) w).2) = C.one := hc.symm.trans (ph_one F)
  exact CubicTwo.cast_zero_gen (3 * 3 + 2 + 1) _ ((cast_rhs _ _).trans (table _ _ hl))

/-- **13 | b(2a − b)·(9(2a − b)² − 1b²) ⇒ 13 is a cube mod p.** -/
theorem cubic_thirteen_conv (hpr : ∀ d, Dv d (6 * (n + 2) + 5 + 1 + 1) → d = 1 ∨ d = 6 * (n + 2) + 5 + 1 + 1) (w : Zp (6 * (n + 2) + 5 + 1))
    (hw1 : w ≠ (ring (6 * (n + 2) + 5 + 1)).one) (hw3 : pw (ring (6 * (n + 2) + 5 + 1)) w 3 = (ring (6 * (n + 2) + 5 + 1)).one)
    (hd : IDv (Int.ofNat 13) ((J (n + 2) w).2 * ((J (n + 2) w).1 + (J (n + 2) w).1 - (J (n + 2) w).2) * (9 * (((J (n + 2) w).1 + (J (n + 2) w).1 - (J (n + 2) w).2) * ((J (n + 2) w).1 + (J (n + 2) w).1 - (J (n + 2) w).2)) - 1 * ((J (n + 2) w).2 * (J (n + 2) w).2)))) :
    chi3 (n + 2) w (nsm (ring (6 * (n + 2) + 5 + 1)) (3 * 3 + 2 + 1 + 1) (ring (6 * (n + 2) + 5 + 1)).one) = (1, 0) := by
  have hc := cubic_recip_split (n + 2) 3 hpr q_prime w hw1 hw3 (q_ne n)
  rw [hP n hpr w hw1 hw3] at hc
  have hr : rhs (castZ F (J (n + 2) w).1) (castZ F (J (n + 2) w).2) = F.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F (Int.ofNat 13) = F.zero := CubeAp.castP 11
      rw [h0, zero_mul']
  have hn : nrm (castZ F (J (n + 2) w).1) (castZ F (J (n + 2) w).2) ≠ F.zero := fun e => by
    have e1 : castZ F (Int.ofNat (6 * (n + 2) + 5 + 1 + 1)) = F.zero := by rw [← jacobi_norm (n + 2) hpr w hw1 hw3, cast_norm]; exact e
    have e2 : mk (3 * 3 + 2 + 1) (6 * (n + 2) + 5 + 1 + 1) = F.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hdq : ∀ P' : Nat, mk (3 * 3 + 2 + 1) P' = F.zero → Dv (3 * 3 + 2 + 1 + 1) P' := fun P' h => md_zero_dv (3 * 3 + 2 + 1) P' (congrArg Subtype.val h)
    exact not_dvq n hpr (hdq _ e2)
  have h1 := hc.trans (table' _ _ hr hn)
  unfold cb at h1
  rcases chi_cases (n + 2) w (nsm (ring (6 * (n + 2) + 5 + 1)) (3 * 3 + 2 + 1 + 1) (ring (6 * (n + 2) + 5 + 1)).one) with e | e | e | e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion (Nat.succ.inj e'))
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')

end CubicThirteen
#print axioms CubicThirteen.lt_wrap
#print axioms CubicThirteen.md_red
#print axioms CubicThirteen.add_nat
#print axioms CubicThirteen.mul_nat
#print axioms CubicThirteen.neg_nat
#print axioms CubicThirteen.addE
#print axioms CubicThirteen.mulE
#print axioms CubicThirteen.negE
#print axioms CubicThirteen.c3E
#print axioms CubicThirteen.pwE
#print axioms CubicThirteen.nrmE
#print axioms CubicThirteen.lhsE
#print axioms CubicThirteen.rhsE
#print axioms CubicThirteen.row0
#print axioms CubicThirteen.row1
#print axioms CubicThirteen.row2
#print axioms CubicThirteen.row3
#print axioms CubicThirteen.row4
#print axioms CubicThirteen.row5
#print axioms CubicThirteen.row6
#print axioms CubicThirteen.row7
#print axioms CubicThirteen.row8
#print axioms CubicThirteen.row9
#print axioms CubicThirteen.row10
#print axioms CubicThirteen.row11
#print axioms CubicThirteen.row12
#print axioms CubicThirteen.ndvN
#print axioms CubicThirteen.q_prime
#print axioms CubicThirteen.q_ne
#print axioms CubicThirteen.not_dvq
#print axioms CubicThirteen.chk_nat
#print axioms CubicThirteen.chk_all
#print axioms CubicThirteen.table
#print axioms CubicThirteen.table'
#print axioms CubicThirteen.cast_norm
#print axioms CubicThirteen.cast_rhs
#print axioms CubicThirteen.hP
#print axioms CubicThirteen.cubic_thirteen
#print axioms CubicThirteen.cubic_thirteen_conv

#print axioms: 107 theorems, no axioms.

78.59 Quartic characters of \(5\) and \(13\)

Quartic reciprocity at the split primes \(5,13\) and a table over \(\mathbb F_q\).

\[ 5 \text { quartic residue} \Leftrightarrow 5\mid b,\qquad 13 \text { quartic residue} \Leftrightarrow 13\mid b(b^2-3a^2). \]

lang/QuartFive.lean

/-
  lang/QuartFive — T43: the quartic character of 5 (generated by work/gen_quart_table.py).

  p = 4(n + 1) + 5 (so p > 5), χ the quartic character of 𝔽_p, J = J(χ, χ) = a + bi, N(J) = a² + b² = p. QuartRecip at the split
  prime q = 5 (k = 0) gives χ̄(q) = (J²)^1 p^1 in 𝔽_5[i], and a 25-case table over 𝔽_5 (through the builtin Nat.mod, proved equal
  to ring q pointwise) shows that this is 1 iff b(b² − 2a²) ≡ 0 (when N(J) ≢ 0):
      5 is a quartic residue mod p  ⇔  5 | b(b² − 2a²).
    table, table'      the table (both directions)
    quart_five, quart_five_conv
  Lean 4 core only, proofs by hand (lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.QuartRecip
import lang.CubicThirteen

namespace QuartFive

open FTAChain ZMod DetChain Frobenius QuartChar QuartJacobi QuartRecip LibGcd CubicFive
open LibRing (castZ castN)

theorem lt_wrap : ∀ q a : Nat, a + 1 + q = q → False
  | 0, _, h => Nat.noConfusion h
  | q + 1, a, h => lt_wrap q a (Nat.succ.inj h)

theorem ndvN (N m' q : Nat) (h : N = q * (m' + 1)) (hr : rem (m' + 1) N = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem q_prime : ∀ d, Dv d (4 * 0 + 3 + 1 + 1) → d = 1 ∨ d = 4 * 0 + 3 + 1 + 1 := fun d hd =>
  match dv_le d 4 hd, hd with
  | ⟨k, hk⟩, ⟨q', hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndvN 5 1 q' hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact (ndvN 5 2 q' hq (fun e => Nat.noConfusion e)).elim
    | 4 => exact (ndvN 5 3 q' hq (fun e => Nat.noConfusion e)).elim
    | 5 => exact Or.inr rfl
    | d + 6 => exact (lt_wrap 5 (d + k) ((show d + k + 1 + 5 = d + 6 + k by lib_nat).trans hk)).elim

variable (n : Nat)

theorem q_ne : nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one ≠ (ring (4 * (n + 1) + 3 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 5 (by rw [show 4 * (n + 1) + 3 + 1 + 1 = 5 + (4 * n + 3) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 5 _))] at h
  exact Nat.noConfusion h

theorem not_dvq (hpr : ∀ d, Dv d (4 * (n + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 1) + 3 + 1 + 1) (h : Dv (4 * 0 + 3 + 1 + 1) (4 * (n + 1) + 3 + 1 + 1)) : False :=
  match hpr 5 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => lt_wrap 5 (4 * n + 3) ((show 4 * n + 3 + 1 + 5 = 4 * (n + 1) + 3 + 1 + 1 by lib_nat).trans e.symm)

abbrev F := ring (4 * 0 + 3 + 1)
abbrev C := Cyclo4.cyc4 F

def nrm (X Y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := F.add (F.mul X X) (F.mul Y Y)
def lhs (X Y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1) :=
  C.mul (pw C (C.mul (C.mul (X, Y) (X, Y)) C.one) 1) (pw C (nrm X Y, F.zero) 1)
def rhs (X Y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := F.mul Y (F.add (F.mul Y Y) (F.neg (F.mul (castZ F 2) (F.mul X X))))

theorem md_red (a r k : Nat) (h : a = r + k * (4 * 0 + 3 + 1 + 1)) (hr : r < 4 * 0 + 3 + 1 + 1) : md (4 * 0 + 3 + 1) a = r := by
  rw [h, md_add_mul]; exact md_small _ r hr

def fm (a : Nat) : Zp (4 * 0 + 3 + 1) := ⟨a % 5, Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
def fadd (x y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := fm (x.val + y.val)
def fmul (x y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := fm (x.val * y.val)
def fneg (x : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := fm ((4 * 0 + 3 + 1) * x.val)

theorem add_nat : ∀ x y : Nat, x < 5 → y < 5 → md (4 * 0 + 3 + 1) (x + y) = (x + y) % 5
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 0, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 0, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 0, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 0, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 1, 0, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 1, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 1, 2, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 1, 3, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 1, 4, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 2, 0, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 2, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 2, 3, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 2, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 3, 0, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 3, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 3, 2, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 3, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 3, 4, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 4, 0, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 4, 1, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 4, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 4, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 4, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | k + 5, _, h, _ => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))
  | _, k + 5, _, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem mul_nat : ∀ x y : Nat, x < 5 → y < 5 → md (4 * 0 + 3 + 1) (x * y) = (x * y) % 5
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 0, 1, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 0, 2, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 0, 3, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 0, 4, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 1, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 1, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 1, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 1, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 1, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 2, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 2, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 2, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 2, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 3, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 3, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 3, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | 3, 3, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 3, 4, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 4, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 4, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 4, 2, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 4, 3, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 4, 4, _, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | k + 5, _, h, _ => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))
  | _, k + 5, _, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem neg_nat : ∀ x : Nat, x < 5 → md (4 * 0 + 3 + 1) ((4 * 0 + 3 + 1) * x) = ((4 * 0 + 3 + 1) * x) % 5
  | 0, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 4 : 0 < 4 * 0 + 3 + 1 + 1)
  | 1, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 0 : 4 < 4 * 0 + 3 + 1 + 1)
  | 2, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 1 : 3 < 4 * 0 + 3 + 1 + 1)
  | 3, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 2 : 2 < 4 * 0 + 3 + 1 + 1)
  | 4, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 3 : 1 < 4 * 0 + 3 + 1 + 1)
  | k + 5, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem addE (x y : Zp (4 * 0 + 3 + 1)) : F.add x y = fadd x y := Subtype.ext (add_nat x.val y.val x.2 y.2)
theorem mulE (x y : Zp (4 * 0 + 3 + 1)) : F.mul x y = fmul x y := Subtype.ext (mul_nat x.val y.val x.2 y.2)
theorem negE (x : Zp (4 * 0 + 3 + 1)) : F.neg x = fneg x := Subtype.ext (by show md (4 * 0 + 3 + 1) ((4 * 0 + 3 + 1) * x.val) = ((4 * 0 + 3 + 1) * x.val) % 5; exact neg_nat x.val x.2)

def c4f (u v : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1) :=
  (fadd (fmul u.1 v.1) (fneg (fmul u.2 v.2)), fadd (fmul u.1 v.2) (fmul u.2 v.1))
theorem c4E (u v : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1)) : C.mul u v = c4f u v := by
  show Cyclo4.c4mul F u v = _
  unfold Cyclo4.c4mul
  rw [mulE, mulE, mulE, mulE, negE, addE, addE]; rfl
def pwf (z : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1)) : Nat → Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1)
  | 0 => C.one
  | k + 1 => c4f (pwf z k) z
theorem pwE (z : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1)) : ∀ k, pw C z k = pwf z k
  | 0 => rfl
  | k + 1 => by show C.mul (pw C z k) z = _; rw [c4E, pwE z k]; rfl

def nrmf (X Y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := fadd (fmul X X) (fmul Y Y)
def lhsf (X Y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1) := c4f (pwf (c4f (c4f (X, Y) (X, Y)) C.one) 1) (pwf (nrmf X Y, F.zero) 1)
def rhsf (X Y : Zp (4 * 0 + 3 + 1)) : Zp (4 * 0 + 3 + 1) := fmul Y (fadd (fmul Y Y) (fneg (fmul (castZ F 2) (fmul X X))))

theorem nrmE (X Y : Zp (4 * 0 + 3 + 1)) : nrm X Y = nrmf X Y := by
  unfold nrm nrmf; rw [mulE, mulE, addE]
theorem lhsE (X Y : Zp (4 * 0 + 3 + 1)) : lhs X Y = lhsf X Y := by
  unfold lhs lhsf; rw [nrmE, c4E, c4E, pwE, pwE, c4E]
theorem rhsE (X Y : Zp (4 * 0 + 3 + 1)) : rhs X Y = rhsf X Y := by
  unfold rhs rhsf
  repeat (first | rw [mulE] | rw [addE] | rw [negE])

def code (z : Zp (4 * 0 + 3 + 1) × Zp (4 * 0 + 3 + 1)) : Nat := z.1.val + 5 * z.2.val
def chk (X Y : Zp (4 * 0 + 3 + 1)) : Bool := (!(Nat.beq (code (lhsf X Y)) 1) || Nat.beq (rhsf X Y).val 0) &&
  (!(Nat.beq (rhsf X Y).val 0) || Nat.beq (nrmf X Y).val 0 || Nat.beq (code (lhsf X Y)) 1)

theorem row0 : ∀ y : Nat, y < 5 → chk (mk (4 * 0 + 3 + 1) 0) (mk (4 * 0 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem row1 : ∀ y : Nat, y < 5 → chk (mk (4 * 0 + 3 + 1) 1) (mk (4 * 0 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem row2 : ∀ y : Nat, y < 5 → chk (mk (4 * 0 + 3 + 1) 2) (mk (4 * 0 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem row3 : ∀ y : Nat, y < 5 → chk (mk (4 * 0 + 3 + 1) 3) (mk (4 * 0 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem row4 : ∀ y : Nat, y < 5 → chk (mk (4 * 0 + 3 + 1) 4) (mk (4 * 0 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, h => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem chk_nat : ∀ x y : Nat, x < 5 → y < 5 → chk (mk (4 * 0 + 3 + 1) x) (mk (4 * 0 + 3 + 1) y) = true
  | 0, y, _, h => row0 y h
  | 1, y, _, h => row1 y h
  | 2, y, _, h => row2 y h
  | 3, y, _, h => row3 y h
  | 4, y, _, h => row4 y h
  | k + 5, _, h, _ => absurd h (fun e => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) e))

theorem chk_all (X Y : Zp (4 * 0 + 3 + 1)) : chk X Y = true := by
  rw [show X = mk (4 * 0 + 3 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (4 * 0 + 3 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

theorem table (X Y : Zp (4 * 0 + 3 + 1)) (h : lhs X Y = C.one) : rhs X Y = F.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [← lhsE, h] at hc
  rw [rhsE]
  exact (Subtype.ext (beq_eq _ _ hc) : rhsf X Y = ⟨0, Nat.zero_lt_succ _⟩)

theorem table' (X Y : Zp (4 * 0 + 3 + 1)) (h : rhs X Y = F.zero) (hn : nrm X Y ≠ F.zero) : lhs X Y = C.one := by
  have hc := band_r _ _ (chk_all X Y)
  rw [← rhsE, h] at hc
  rw [lhsE]
  rw [nrmE] at hn
  cases hb : Nat.beq (nrmf X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrmf X Y = F.zero) hn
  | false =>
    rw [hb] at hc
    have e := beq_eq _ _ hc
    match lhsf X Y, e with
    | (u, v), e =>
      have e' : u.val + 5 * v.val = 1 := e
      have hv : v.val = 0 := by
        cases hv' : v.val with
        | zero => rfl
        | succ t => rw [hv', show 5 * (t + 1) = 5 * t + 4 + 1 by lib_nat, show u.val + (5 * t + 4 + 1) = u.val + 5 * t + 4 + 1 by lib_nat] at e'; exact Nat.noConfusion (Nat.succ.inj e')
      have hu : u.val = 1 := by rw [hv] at e'; exact e'
      exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem cast_norm (x y : Int) : castZ F (x * x + y * y) = nrm (castZ F x) (castZ F y) := by
  rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F (y * (y * y - 2 * (x * x))) = rhs (castZ F x) (castZ F y) := by
  rw [show y * (y * y - 2 * (x * x)) = y * (y * y + -(2 * (x * x))) from rfl]
  repeat (first | rw [HasseDavenport.castZ_mul] | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg])
  rfl

theorem hP (hpr : ∀ d, Dv d (4 * (n + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 1) + 3 + 1 + 1) (ι : Zp (4 * (n + 1) + 3 + 1)) (hi : (ring (4 * (n + 1) + 3 + 1)).mul ι ι = (ring (4 * (n + 1) + 3 + 1)).neg (ring (4 * (n + 1) + 3 + 1)).one) :
    ph4 F (Ppq (n + 1)) = (nrm (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).1) (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).2), F.zero) := by
  rw [show Ppq (n + 1) = (Int.ofNat (4 * (n + 1) + 3 + 1 + 1), 0) from sum_oneq (n + 1) _, ← quart_norm (n + 1) hpr ι hi]; unfold ph4; rw [cast_norm]; rfl

theorem chi4_cases (ι x : Zp (4 * (n + 1) + 3 + 1)) : chi4 (n + 1) ι x = (0, 0) ∨ chi4 (n + 1) ι x = (1, 0) ∨ chi4 (n + 1) ι x = (0, 1) ∨ chi4 (n + 1) ι x = (-1, 0) ∨ chi4 (n + 1) ι x = (0, -1) := by
  unfold chi4
  by_cases h1 : x = (ring (4 * (n + 1) + 3 + 1)).zero
  · exact Or.inl (ite_t _ _ h1)
  · rw [ite_f _ _ h1]
    by_cases h2 : pw (ring (4 * (n + 1) + 3 + 1)) x ((n + 1) + 1) = (ring (4 * (n + 1) + 3 + 1)).one
    · exact Or.inr (Or.inl (ite_t _ _ h2))
    · rw [ite_f _ _ h2]
      by_cases h3 : pw (ring (4 * (n + 1) + 3 + 1)) x ((n + 1) + 1) = ι
      · exact Or.inr (Or.inr (Or.inl (ite_t _ _ h3)))
      · rw [ite_f _ _ h3]
        by_cases h4 : pw (ring (4 * (n + 1) + 3 + 1)) x ((n + 1) + 1) = (ring (4 * (n + 1) + 3 + 1)).neg (ring (4 * (n + 1) + 3 + 1)).one
        · exact Or.inr (Or.inr (Or.inr (Or.inl (ite_t _ _ h4))))
        · exact Or.inr (Or.inr (Or.inr (Or.inr (ite_f _ _ h4))))

theorem lhs_one (hpr : ∀ d, Dv d (4 * (n + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 1) + 3 + 1 + 1) (ι : Zp (4 * (n + 1) + 3 + 1)) (hi : (ring (4 * (n + 1) + 3 + 1)).mul ι ι = (ring (4 * (n + 1) + 3 + 1)).neg (ring (4 * (n + 1) + 3 + 1)).one) :
    ph4 F (chib (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one)) = lhs (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).1) (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).2) := by
  rw [quart_recip_split (n + 1) 0 hpr q_prime ι hi (q_ne n), hP n hpr ι hi, ph4_one]; rfl

/-- **5 a quartic residue mod p ⇒ 5 | b(b² − 2a²)**. -/
theorem quart_five (hpr : ∀ d, Dv d (4 * (n + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 1) + 3 + 1 + 1) (ι : Zp (4 * (n + 1) + 3 + 1)) (hi : (ring (4 * (n + 1) + 3 + 1)).mul ι ι = (ring (4 * (n + 1) + 3 + 1)).neg (ring (4 * (n + 1) + 3 + 1)).one)
    (hq : chi4 (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one) = (1, 0)) : IDv (Int.ofNat 5) ((Jq (n + 1) (chi4 (n + 1) ι)).2 * ((Jq (n + 1) (chi4 (n + 1) ι)).2 * (Jq (n + 1) (chi4 (n + 1) ι)).2 - 2 * ((Jq (n + 1) (chi4 (n + 1) ι)).1 * (Jq (n + 1) (chi4 (n + 1) ι)).1))) := by
  have hc := lhs_one n hpr ι hi
  rw [show chib (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one) = gconj (chi4 (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one)) from chi_ivq (n + 1) hpr ι hi _, hq] at hc
  have hl : lhs (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).1) (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).2) = C.one := hc.symm.trans (Prod.ext (rfl : castZ F 1 = F.one) (rfl : castZ F (-0) = F.zero))
  exact CubicTwo.cast_zero_gen (4 * 0 + 3 + 1) _ ((cast_rhs _ _).trans (table _ _ hl))

/-- **5 | b(b² − 2a²) ⇒ 5 is a quartic residue mod p.** -/
theorem quart_five_conv (hpr : ∀ d, Dv d (4 * (n + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 1) + 3 + 1 + 1) (ι : Zp (4 * (n + 1) + 3 + 1)) (hi : (ring (4 * (n + 1) + 3 + 1)).mul ι ι = (ring (4 * (n + 1) + 3 + 1)).neg (ring (4 * (n + 1) + 3 + 1)).one)
    (hd : IDv (Int.ofNat 5) ((Jq (n + 1) (chi4 (n + 1) ι)).2 * ((Jq (n + 1) (chi4 (n + 1) ι)).2 * (Jq (n + 1) (chi4 (n + 1) ι)).2 - 2 * ((Jq (n + 1) (chi4 (n + 1) ι)).1 * (Jq (n + 1) (chi4 (n + 1) ι)).1)))) : chi4 (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one) = (1, 0) := by
  have hc := lhs_one n hpr ι hi
  have hr : rhs (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).1) (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).2) = F.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F (Int.ofNat 5) = F.zero := CubeAp.castP 3
      rw [h0, zero_mul']
  have hn : nrm (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).1) (castZ F (Jq (n + 1) (chi4 (n + 1) ι)).2) ≠ F.zero := fun e => by
    have e1 : castZ F (Int.ofNat (4 * (n + 1) + 3 + 1 + 1)) = F.zero := by rw [← quart_norm (n + 1) hpr ι hi, cast_norm]; exact e
    have e2 : mk (4 * 0 + 3 + 1) (4 * (n + 1) + 3 + 1 + 1) = F.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hdq : ∀ P' : Nat, mk (4 * 0 + 3 + 1) P' = F.zero → Dv (4 * 0 + 3 + 1 + 1) P' := fun P' h => md_zero_dv (4 * 0 + 3 + 1) P' (congrArg Subtype.val h)
    exact not_dvq n hpr (hdq _ e2)
  have h1 := hc.trans (table' _ _ hr hn)
  rw [show chib (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one) = gconj (chi4 (n + 1) ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one)) from chi_ivq (n + 1) hpr ι hi _] at h1
  rcases chi4_cases n ι (nsm (ring (4 * (n + 1) + 3 + 1)) (4 * 0 + 3 + 1 + 1) (ring (4 * (n + 1) + 3 + 1)).one) with e | e | e | e | e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion (Nat.succ.inj e'))
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')

end QuartFive
#print axioms QuartFive.lt_wrap
#print axioms QuartFive.ndvN
#print axioms QuartFive.q_prime
#print axioms QuartFive.q_ne
#print axioms QuartFive.not_dvq
#print axioms QuartFive.md_red
#print axioms QuartFive.add_nat
#print axioms QuartFive.mul_nat
#print axioms QuartFive.neg_nat
#print axioms QuartFive.addE
#print axioms QuartFive.mulE
#print axioms QuartFive.negE
#print axioms QuartFive.c4E
#print axioms QuartFive.pwE
#print axioms QuartFive.nrmE
#print axioms QuartFive.lhsE
#print axioms QuartFive.rhsE
#print axioms QuartFive.row0
#print axioms QuartFive.row1
#print axioms QuartFive.row2
#print axioms QuartFive.row3
#print axioms QuartFive.row4
#print axioms QuartFive.chk_nat
#print axioms QuartFive.chk_all
#print axioms QuartFive.table
#print axioms QuartFive.table'
#print axioms QuartFive.cast_norm
#print axioms QuartFive.cast_rhs
#print axioms QuartFive.hP
#print axioms QuartFive.chi4_cases
#print axioms QuartFive.lhs_one
#print axioms QuartFive.quart_five
#print axioms QuartFive.quart_five_conv

lang/QuartThirteen.lean

/-
  lang/QuartThirteen — T43: the quartic character of 13 (generated by work/gen_quart_table.py).

  p = 4(n + 3) + 5 (so p > 13), χ the quartic character of 𝔽_p, J = J(χ, χ) = a + bi, N(J) = a² + b² = p. QuartRecip at the split
  prime q = 13 (k = 2) gives χ̄(q) = (J²)^3 p^3 in 𝔽_13[i], and a 169-case table over 𝔽_13 (through the builtin Nat.mod, proved equal
  to ring q pointwise) shows that this is 1 iff b(b² − 3a²) ≡ 0 (when N(J) ≢ 0):
      13 is a quartic residue mod p  ⇔  13 | b(b² − 3a²).
    table, table'      the table (both directions)
    quart_thirteen, quart_thirteen_conv
  Lean 4 core only, proofs by hand (lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.QuartRecip
import lang.CubicThirteen

namespace QuartThirteen

open FTAChain ZMod DetChain Frobenius QuartChar QuartJacobi QuartRecip LibGcd CubicFive
open LibRing (castZ castN)

theorem lt_wrap : ∀ q a : Nat, a + 1 + q = q → False
  | 0, _, h => Nat.noConfusion h
  | q + 1, a, h => lt_wrap q a (Nat.succ.inj h)

theorem ndvN (N m' q : Nat) (h : N = q * (m' + 1)) (hr : rem (m' + 1) N = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem q_prime : ∀ d, Dv d (4 * 2 + 3 + 1 + 1) → d = 1 ∨ d = 4 * 2 + 3 + 1 + 1 := fun d hd =>
  match dv_le d 12 hd, hd with
  | ⟨k, hk⟩, ⟨q', hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndvN 13 1 q' hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact (ndvN 13 2 q' hq (fun e => Nat.noConfusion e)).elim
    | 4 => exact (ndvN 13 3 q' hq (fun e => Nat.noConfusion e)).elim
    | 5 => exact (ndvN 13 4 q' hq (fun e => Nat.noConfusion e)).elim
    | 6 => exact (ndvN 13 5 q' hq (fun e => Nat.noConfusion e)).elim
    | 7 => exact (ndvN 13 6 q' hq (fun e => Nat.noConfusion e)).elim
    | 8 => exact (ndvN 13 7 q' hq (fun e => Nat.noConfusion e)).elim
    | 9 => exact (ndvN 13 8 q' hq (fun e => Nat.noConfusion e)).elim
    | 10 => exact (ndvN 13 9 q' hq (fun e => Nat.noConfusion e)).elim
    | 11 => exact (ndvN 13 10 q' hq (fun e => Nat.noConfusion e)).elim
    | 12 => exact (ndvN 13 11 q' hq (fun e => Nat.noConfusion e)).elim
    | 13 => exact Or.inr rfl
    | d + 14 => exact (lt_wrap 13 (d + k) ((show d + k + 1 + 13 = d + 14 + k by lib_nat).trans hk)).elim

variable (n : Nat)

theorem q_ne : nsm (ring (4 * (n + 3) + 3 + 1)) (4 * 2 + 3 + 1 + 1) (ring (4 * (n + 3) + 3 + 1)).one ≠ (ring (4 * (n + 3) + 3 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 13 (by rw [show 4 * (n + 3) + 3 + 1 + 1 = 13 + (4 * n + 3) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 13 _))] at h
  exact Nat.noConfusion h

theorem not_dvq (hpr : ∀ d, Dv d (4 * (n + 3) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 3) + 3 + 1 + 1) (h : Dv (4 * 2 + 3 + 1 + 1) (4 * (n + 3) + 3 + 1 + 1)) : False :=
  match hpr 13 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => lt_wrap 13 (4 * n + 3) ((show 4 * n + 3 + 1 + 13 = 4 * (n + 3) + 3 + 1 + 1 by lib_nat).trans e.symm)

abbrev F := ring (4 * 2 + 3 + 1)
abbrev C := Cyclo4.cyc4 F

def nrm (X Y : Zp (4 * 2 + 3 + 1)) : Zp (4 * 2 + 3 + 1) := F.add (F.mul X X) (F.mul Y Y)
def lhs (X Y : Zp (4 * 2 + 3 + 1)) : Zp (4 * 2 + 3 + 1) × Zp (4 * 2 + 3 + 1) :=
  C.mul (pw C (C.mul (C.mul (X, Y) (X, Y)) C.one) 3) (pw C (nrm X Y, F.zero) 3)
def rhs (X Y : Zp (4 * 2 + 3 + 1)) : Zp (4 * 2 + 3 + 1) := F.mul Y (F.add (F.mul Y Y) (F.neg (F.mul (castZ F 3) (F.mul X X))))

theorem md_red (a r k : Nat) (h : a = r + k * (4 * 2 + 3 + 1 + 1)) (hr : r < 4 * 2 + 3 + 1 + 1) : md (4 * 2 + 3 + 1) a = r := by
  rw [h, md_add_mul]; exact md_small _ r hr

def fm (a : Nat) : Zp (4 * 2 + 3 + 1) := ⟨a % 13, Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
def fadd (x y : Zp (4 * 2 + 3 + 1)) : Zp (4 * 2 + 3 + 1) := fm (x.val + y.val)
def fmul (x y : Zp (4 * 2 + 3 + 1)) : Zp (4 * 2 + 3 + 1) := fm (x.val * y.val)
def fneg (x : Zp (4 * 2 + 3 + 1)) : Zp (4 * 2 + 3 + 1) := fm ((4 * 2 + 3 + 1) * x.val)

theorem add_nat : ∀ x y : Nat, x < 13 → y < 13 → md (4 * 2 + 3 + 1) (x + y) = (x + y) % 13
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 0, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 0, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 0, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 0, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 0, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 0, 7, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 0, 8, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 0, 9, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 0, 10, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 0, 11, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 0, 12, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 1, 0, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 1, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 1, 2, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 1, 3, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 1, 4, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 1, 5, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 1, 6, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 1, 7, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 1, 8, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 1, 9, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 1, 10, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 1, 11, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 1, 12, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 2, 0, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 2, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 2, 3, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 2, 4, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 2, 5, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 2, 6, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 2, 7, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 2, 8, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 2, 9, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 2, 10, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 2, 11, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 2, 12, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 3, 0, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 3, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 3, 2, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 3, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 3, 4, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 3, 5, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 3, 6, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 3, 7, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 3, 8, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 3, 9, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 3, 10, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 3, 11, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 3, 12, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 4, 0, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 4, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 4, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 4, 3, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 4, 4, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 4, 5, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 4, 6, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 4, 7, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 4, 8, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 4, 9, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 4, 10, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 4, 11, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 4, 12, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 5, 0, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 5, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 5, 2, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 5, 3, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 5, 4, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 5, 5, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 5, 6, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 5, 7, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 5, 8, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 5, 9, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 5, 10, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 5, 11, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 5, 12, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 6, 0, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 6, 1, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 6, 2, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 6, 3, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 6, 4, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 6, 5, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 6, 6, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 6, 7, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 6, 8, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 6, 9, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 6, 10, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 6, 11, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 6, 12, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 7, 0, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 7, 1, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 7, 2, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 7, 3, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 7, 4, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 7, 5, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 7, 6, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 7, 7, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 7, 8, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 7, 9, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 7, 10, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 7, 11, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 7, 12, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 8, 0, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 8, 1, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 8, 2, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 8, 3, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 8, 4, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 8, 5, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 8, 6, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 8, 7, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 8, 8, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 8, 9, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 8, 10, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 8, 11, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 8, 12, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 9, 0, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 9, 1, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 9, 2, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 9, 3, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 9, 4, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 9, 5, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 9, 6, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 9, 7, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 9, 8, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 9, 9, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 9, 10, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 9, 11, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 9, 12, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 10, 0, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 10, 1, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 10, 2, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 10, 3, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 10, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 10, 5, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 10, 6, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 10, 7, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 10, 8, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 10, 9, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 10, 10, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 10, 11, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 10, 12, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 11, 0, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 11, 1, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 11, 2, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 11, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 11, 4, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 11, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 11, 6, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 11, 7, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 11, 8, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 11, 9, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 11, 10, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 11, 11, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 11, 12, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 12, 0, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 12, 1, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 12, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 12, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 12, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 12, 5, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 12, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 12, 7, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 12, 8, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 12, 9, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 12, 10, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 12, 11, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 12, 12, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | k + 13, _, h, _ => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))
  | _, k + 13, _, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem mul_nat : ∀ x y : Nat, x < 13 → y < 13 → md (4 * 2 + 3 + 1) (x * y) = (x * y) % 13
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 1, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 2, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 3, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 4, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 5, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 6, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 7, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 8, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 9, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 10, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 11, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 0, 12, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 1, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 1, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 1, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 1, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 1, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 1, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 1, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 1, 7, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 1, 8, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 1, 9, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 1, 10, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 1, 11, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 1, 12, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 2, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 2, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 2, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 2, 4, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 2, 5, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 2, 6, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 2, 7, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 2, 8, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 2, 9, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 2, 10, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 2, 11, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 2, 12, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 3, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 3, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 3, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 3, 3, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 3, 4, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 3, 5, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 3, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 3, 7, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 3, 8, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 3, 9, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 3, 10, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 3, 11, _, _ => md_red _ 7 2 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 3, 12, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 4, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 4, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 4, 2, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 4, 3, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 4, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 4, 5, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 4, 6, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 4, 7, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 4, 8, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 4, 9, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 4, 10, _, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 4, 11, _, _ => md_red _ 5 3 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 4, 12, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 5, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 5, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 5, 2, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 5, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 5, 4, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 5, 5, _, _ => md_red _ 12 1 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 5, 6, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 5, 7, _, _ => md_red _ 9 2 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 5, 8, _, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 5, 9, _, _ => md_red _ 6 3 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 5, 10, _, _ => md_red _ 11 3 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 5, 11, _, _ => md_red _ 3 4 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 5, 12, _, _ => md_red _ 8 4 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 6, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 6, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 6, 2, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 6, 3, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 6, 4, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 6, 5, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 6, 6, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 6, 7, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 6, 8, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 6, 9, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 6, 10, _, _ => md_red _ 8 4 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 6, 11, _, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 6, 12, _, _ => md_red _ 7 5 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 7, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 7, 1, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 7, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 7, 3, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 7, 4, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 7, 5, _, _ => md_red _ 9 2 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 7, 6, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 7, 7, _, _ => md_red _ 10 3 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 7, 8, _, _ => md_red _ 4 4 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 7, 9, _, _ => md_red _ 11 4 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 7, 10, _, _ => md_red _ 5 5 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 7, 11, _, _ => md_red _ 12 5 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 7, 12, _, _ => md_red _ 6 6 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 8, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 8, 1, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 8, 2, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 8, 3, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 8, 4, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 8, 5, _, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 8, 6, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 8, 7, _, _ => md_red _ 4 4 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 8, 8, _, _ => md_red _ 12 4 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 8, 9, _, _ => md_red _ 7 5 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 8, 10, _, _ => md_red _ 2 6 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 8, 11, _, _ => md_red _ 10 6 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 8, 12, _, _ => md_red _ 5 7 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 9, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 9, 1, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 9, 2, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 9, 3, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 9, 4, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 9, 5, _, _ => md_red _ 6 3 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 9, 6, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 9, 7, _, _ => md_red _ 11 4 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 9, 8, _, _ => md_red _ 7 5 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 9, 9, _, _ => md_red _ 3 6 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 9, 10, _, _ => md_red _ 12 6 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 9, 11, _, _ => md_red _ 8 7 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 9, 12, _, _ => md_red _ 4 8 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 10, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 10, 1, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 10, 2, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 10, 3, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 10, 4, _, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 10, 5, _, _ => md_red _ 11 3 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 10, 6, _, _ => md_red _ 8 4 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 10, 7, _, _ => md_red _ 5 5 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 10, 8, _, _ => md_red _ 2 6 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 10, 9, _, _ => md_red _ 12 6 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 10, 10, _, _ => md_red _ 9 7 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 10, 11, _, _ => md_red _ 6 8 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 10, 12, _, _ => md_red _ 3 9 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 11, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 11, 1, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 11, 2, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 11, 3, _, _ => md_red _ 7 2 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 11, 4, _, _ => md_red _ 5 3 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 11, 5, _, _ => md_red _ 3 4 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 11, 6, _, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | 11, 7, _, _ => md_red _ 12 5 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 11, 8, _, _ => md_red _ 10 6 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 11, 9, _, _ => md_red _ 8 7 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 11, 10, _, _ => md_red _ 6 8 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 11, 11, _, _ => md_red _ 4 9 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 11, 12, _, _ => md_red _ 2 10 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 12, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 12, 1, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 12, 2, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 12, 3, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 12, 4, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 12, 5, _, _ => md_red _ 8 4 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 12, 6, _, _ => md_red _ 7 5 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 12, 7, _, _ => md_red _ 6 6 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 12, 8, _, _ => md_red _ 5 7 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 12, 9, _, _ => md_red _ 4 8 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 12, 10, _, _ => md_red _ 3 9 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 12, 11, _, _ => md_red _ 2 10 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 12, 12, _, _ => md_red _ 1 11 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | k + 13, _, h, _ => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))
  | _, k + 13, _, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem neg_nat : ∀ x : Nat, x < 13 → md (4 * 2 + 3 + 1) ((4 * 2 + 3 + 1) * x) = ((4 * 2 + 3 + 1) * x) % 13
  | 0, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 12 : 0 < 4 * 2 + 3 + 1 + 1)
  | 1, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 0 : 12 < 4 * 2 + 3 + 1 + 1)
  | 2, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 1 : 11 < 4 * 2 + 3 + 1 + 1)
  | 3, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 2 : 10 < 4 * 2 + 3 + 1 + 1)
  | 4, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 3 : 9 < 4 * 2 + 3 + 1 + 1)
  | 5, _ => md_red _ 8 4 rfl (Nat.le_add_right 9 4 : 8 < 4 * 2 + 3 + 1 + 1)
  | 6, _ => md_red _ 7 5 rfl (Nat.le_add_right 8 5 : 7 < 4 * 2 + 3 + 1 + 1)
  | 7, _ => md_red _ 6 6 rfl (Nat.le_add_right 7 6 : 6 < 4 * 2 + 3 + 1 + 1)
  | 8, _ => md_red _ 5 7 rfl (Nat.le_add_right 6 7 : 5 < 4 * 2 + 3 + 1 + 1)
  | 9, _ => md_red _ 4 8 rfl (Nat.le_add_right 5 8 : 4 < 4 * 2 + 3 + 1 + 1)
  | 10, _ => md_red _ 3 9 rfl (Nat.le_add_right 4 9 : 3 < 4 * 2 + 3 + 1 + 1)
  | 11, _ => md_red _ 2 10 rfl (Nat.le_add_right 3 10 : 2 < 4 * 2 + 3 + 1 + 1)
  | 12, _ => md_red _ 1 11 rfl (Nat.le_add_right 2 11 : 1 < 4 * 2 + 3 + 1 + 1)
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem addE (x y : Zp (4 * 2 + 3 + 1)) : F.add x y = fadd x y := Subtype.ext (add_nat x.val y.val x.2 y.2)
theorem mulE (x y : Zp (4 * 2 + 3 + 1)) : F.mul x y = fmul x y := Subtype.ext (mul_nat x.val y.val x.2 y.2)
theorem negE (x : Zp (4 * 2 + 3 + 1)) : F.neg x = fneg x := Subtype.ext (by show md (4 * 2 + 3 + 1) ((4 * 2 + 3 + 1) * x.val) = ((4 * 2 + 3 + 1) * x.val) % 13; exact neg_nat x.val x.2)

def c4f (u v : Zp (4 * 2 + 3 + 1) × Zp (4 * 2 + 3 + 1)) : Zp (4 * 2 + 3 + 1) × Zp (4 * 2 + 3 + 1) :=
  (fadd (fmul u.1 v.1) (fneg (fmul u.2 v.2)), fadd (fmul u.1 v.2) (fmul u.2 v.1))
theorem c4E (u v : Zp (4 * 2 + 3 + 1) × Zp (4 * 2 + 3 + 1)) : C.mul u v = c4f u v := by
  show Cyclo4.c4mul F u v = _
  unfold Cyclo4.c4mul
  rw [mulE, mulE, mulE, mulE, negE, addE, addE]; rfl
def pwf (z : Zp (4 * 2 + 3 + 1) × Zp (4 * 2 + 3 + 1)) : Nat → Zp (4 * 2 + 3 + 1) × Zp (4 * 2 + 3 + 1)
  | 0 => C.one
  | k + 1 => c4f (pwf z k) z
theorem pwE (z : Zp (4 * 2 + 3 + 1) × Zp (4 * 2 + 3 + 1)) : ∀ k, pw C z k = pwf z k
  | 0 => rfl
  | k + 1 => by show C.mul (pw C z k) z = _; rw [c4E, pwE z k]; rfl

def nrmf (X Y : Zp (4 * 2 + 3 + 1)) : Zp (4 * 2 + 3 + 1) := fadd (fmul X X) (fmul Y Y)
def lhsf (X Y : Zp (4 * 2 + 3 + 1)) : Zp (4 * 2 + 3 + 1) × Zp (4 * 2 + 3 + 1) := c4f (pwf (c4f (c4f (X, Y) (X, Y)) C.one) 3) (pwf (nrmf X Y, F.zero) 3)
def rhsf (X Y : Zp (4 * 2 + 3 + 1)) : Zp (4 * 2 + 3 + 1) := fmul Y (fadd (fmul Y Y) (fneg (fmul (castZ F 3) (fmul X X))))

theorem nrmE (X Y : Zp (4 * 2 + 3 + 1)) : nrm X Y = nrmf X Y := by
  unfold nrm nrmf; rw [mulE, mulE, addE]
theorem lhsE (X Y : Zp (4 * 2 + 3 + 1)) : lhs X Y = lhsf X Y := by
  unfold lhs lhsf; rw [nrmE, c4E, c4E, pwE, pwE, c4E]
theorem rhsE (X Y : Zp (4 * 2 + 3 + 1)) : rhs X Y = rhsf X Y := by
  unfold rhs rhsf
  repeat (first | rw [mulE] | rw [addE] | rw [negE])

def code (z : Zp (4 * 2 + 3 + 1) × Zp (4 * 2 + 3 + 1)) : Nat := z.1.val + 13 * z.2.val
def chk (X Y : Zp (4 * 2 + 3 + 1)) : Bool := (!(Nat.beq (code (lhsf X Y)) 1) || Nat.beq (rhsf X Y).val 0) &&
  (!(Nat.beq (rhsf X Y).val 0) || Nat.beq (nrmf X Y).val 0 || Nat.beq (code (lhsf X Y)) 1)

theorem row0 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 0) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row1 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 1) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row2 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 2) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row3 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 3) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row4 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 4) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row5 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 5) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row6 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 6) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row7 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 7) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row8 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 8) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row9 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 9) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row10 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 10) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row11 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 11) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem row12 : ∀ y : Nat, y < 13 → chk (mk (4 * 2 + 3 + 1) 12) (mk (4 * 2 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | k + 13, h => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem chk_nat : ∀ x y : Nat, x < 13 → y < 13 → chk (mk (4 * 2 + 3 + 1) x) (mk (4 * 2 + 3 + 1) y) = true
  | 0, y, _, h => row0 y h
  | 1, y, _, h => row1 y h
  | 2, y, _, h => row2 y h
  | 3, y, _, h => row3 y h
  | 4, y, _, h => row4 y h
  | 5, y, _, h => row5 y h
  | 6, y, _, h => row6 y h
  | 7, y, _, h => row7 y h
  | 8, y, _, h => row8 y h
  | 9, y, _, h => row9 y h
  | 10, y, _, h => row10 y h
  | 11, y, _, h => row11 y h
  | 12, y, _, h => row12 y h
  | k + 13, _, h, _ => absurd h (fun e => Nat.lt_irrefl 13 (Nat.lt_of_le_of_lt (Nat.le_add_left 13 k) e))

theorem chk_all (X Y : Zp (4 * 2 + 3 + 1)) : chk X Y = true := by
  rw [show X = mk (4 * 2 + 3 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (4 * 2 + 3 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

theorem table (X Y : Zp (4 * 2 + 3 + 1)) (h : lhs X Y = C.one) : rhs X Y = F.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [← lhsE, h] at hc
  rw [rhsE]
  exact (Subtype.ext (beq_eq _ _ hc) : rhsf X Y = ⟨0, Nat.zero_lt_succ _⟩)

theorem table' (X Y : Zp (4 * 2 + 3 + 1)) (h : rhs X Y = F.zero) (hn : nrm X Y ≠ F.zero) : lhs X Y = C.one := by
  have hc := band_r _ _ (chk_all X Y)
  rw [← rhsE, h] at hc
  rw [lhsE]
  rw [nrmE] at hn
  cases hb : Nat.beq (nrmf X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrmf X Y = F.zero) hn
  | false =>
    rw [hb] at hc
    have e := beq_eq _ _ hc
    match lhsf X Y, e with
    | (u, v), e =>
      have e' : u.val + 13 * v.val = 1 := e
      have hv : v.val = 0 := by
        cases hv' : v.val with
        | zero => rfl
        | succ t => rw [hv', show 13 * (t + 1) = 13 * t + 12 + 1 by lib_nat, show u.val + (13 * t + 12 + 1) = u.val + 13 * t + 12 + 1 by lib_nat] at e'; exact Nat.noConfusion (Nat.succ.inj e')
      have hu : u.val = 1 := by rw [hv] at e'; exact e'
      exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem cast_norm (x y : Int) : castZ F (x * x + y * y) = nrm (castZ F x) (castZ F y) := by
  rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F (y * (y * y - 3 * (x * x))) = rhs (castZ F x) (castZ F y) := by
  rw [show y * (y * y - 3 * (x * x)) = y * (y * y + -(3 * (x * x))) from rfl]
  repeat (first | rw [HasseDavenport.castZ_mul] | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg])
  rfl

theorem hP (hpr : ∀ d, Dv d (4 * (n + 3) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 3) + 3 + 1 + 1) (ι : Zp (4 * (n + 3) + 3 + 1)) (hi : (ring (4 * (n + 3) + 3 + 1)).mul ι ι = (ring (4 * (n + 3) + 3 + 1)).neg (ring (4 * (n + 3) + 3 + 1)).one) :
    ph4 F (Ppq (n + 3)) = (nrm (castZ F (Jq (n + 3) (chi4 (n + 3) ι)).1) (castZ F (Jq (n + 3) (chi4 (n + 3) ι)).2), F.zero) := by
  rw [show Ppq (n + 3) = (Int.ofNat (4 * (n + 3) + 3 + 1 + 1), 0) from sum_oneq (n + 3) _, ← quart_norm (n + 3) hpr ι hi]; unfold ph4; rw [cast_norm]; rfl

theorem chi4_cases (ι x : Zp (4 * (n + 3) + 3 + 1)) : chi4 (n + 3) ι x = (0, 0) ∨ chi4 (n + 3) ι x = (1, 0) ∨ chi4 (n + 3) ι x = (0, 1) ∨ chi4 (n + 3) ι x = (-1, 0) ∨ chi4 (n + 3) ι x = (0, -1) := by
  unfold chi4
  by_cases h1 : x = (ring (4 * (n + 3) + 3 + 1)).zero
  · exact Or.inl (ite_t _ _ h1)
  · rw [ite_f _ _ h1]
    by_cases h2 : pw (ring (4 * (n + 3) + 3 + 1)) x ((n + 3) + 1) = (ring (4 * (n + 3) + 3 + 1)).one
    · exact Or.inr (Or.inl (ite_t _ _ h2))
    · rw [ite_f _ _ h2]
      by_cases h3 : pw (ring (4 * (n + 3) + 3 + 1)) x ((n + 3) + 1) = ι
      · exact Or.inr (Or.inr (Or.inl (ite_t _ _ h3)))
      · rw [ite_f _ _ h3]
        by_cases h4 : pw (ring (4 * (n + 3) + 3 + 1)) x ((n + 3) + 1) = (ring (4 * (n + 3) + 3 + 1)).neg (ring (4 * (n + 3) + 3 + 1)).one
        · exact Or.inr (Or.inr (Or.inr (Or.inl (ite_t _ _ h4))))
        · exact Or.inr (Or.inr (Or.inr (Or.inr (ite_f _ _ h4))))

theorem lhs_one (hpr : ∀ d, Dv d (4 * (n + 3) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 3) + 3 + 1 + 1) (ι : Zp (4 * (n + 3) + 3 + 1)) (hi : (ring (4 * (n + 3) + 3 + 1)).mul ι ι = (ring (4 * (n + 3) + 3 + 1)).neg (ring (4 * (n + 3) + 3 + 1)).one) :
    ph4 F (chib (n + 3) ι (nsm (ring (4 * (n + 3) + 3 + 1)) (4 * 2 + 3 + 1 + 1) (ring (4 * (n + 3) + 3 + 1)).one)) = lhs (castZ F (Jq (n + 3) (chi4 (n + 3) ι)).1) (castZ F (Jq (n + 3) (chi4 (n + 3) ι)).2) := by
  rw [quart_recip_split (n + 3) 2 hpr q_prime ι hi (q_ne n), hP n hpr ι hi, ph4_one]; rfl

/-- **13 a quartic residue mod p ⇒ 13 | b(b² − 3a²)**. -/
theorem quart_thirteen (hpr : ∀ d, Dv d (4 * (n + 3) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 3) + 3 + 1 + 1) (ι : Zp (4 * (n + 3) + 3 + 1)) (hi : (ring (4 * (n + 3) + 3 + 1)).mul ι ι = (ring (4 * (n + 3) + 3 + 1)).neg (ring (4 * (n + 3) + 3 + 1)).one)
    (hq : chi4 (n + 3) ι (nsm (ring (4 * (n + 3) + 3 + 1)) (4 * 2 + 3 + 1 + 1) (ring (4 * (n + 3) + 3 + 1)).one) = (1, 0)) : IDv (Int.ofNat 13) ((Jq (n + 3) (chi4 (n + 3) ι)).2 * ((Jq (n + 3) (chi4 (n + 3) ι)).2 * (Jq (n + 3) (chi4 (n + 3) ι)).2 - 3 * ((Jq (n + 3) (chi4 (n + 3) ι)).1 * (Jq (n + 3) (chi4 (n + 3) ι)).1))) := by
  have hc := lhs_one n hpr ι hi
  rw [show chib (n + 3) ι (nsm (ring (4 * (n + 3) + 3 + 1)) (4 * 2 + 3 + 1 + 1) (ring (4 * (n + 3) + 3 + 1)).one) = gconj (chi4 (n + 3) ι (nsm (ring (4 * (n + 3) + 3 + 1)) (4 * 2 + 3 + 1 + 1) (ring (4 * (n + 3) + 3 + 1)).one)) from chi_ivq (n + 3) hpr ι hi _, hq] at hc
  have hl : lhs (castZ F (Jq (n + 3) (chi4 (n + 3) ι)).1) (castZ F (Jq (n + 3) (chi4 (n + 3) ι)).2) = C.one := hc.symm.trans (Prod.ext (rfl : castZ F 1 = F.one) (rfl : castZ F (-0) = F.zero))
  exact CubicTwo.cast_zero_gen (4 * 2 + 3 + 1) _ ((cast_rhs _ _).trans (table _ _ hl))

/-- **13 | b(b² − 3a²) ⇒ 13 is a quartic residue mod p.** -/
theorem quart_thirteen_conv (hpr : ∀ d, Dv d (4 * (n + 3) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 3) + 3 + 1 + 1) (ι : Zp (4 * (n + 3) + 3 + 1)) (hi : (ring (4 * (n + 3) + 3 + 1)).mul ι ι = (ring (4 * (n + 3) + 3 + 1)).neg (ring (4 * (n + 3) + 3 + 1)).one)
    (hd : IDv (Int.ofNat 13) ((Jq (n + 3) (chi4 (n + 3) ι)).2 * ((Jq (n + 3) (chi4 (n + 3) ι)).2 * (Jq (n + 3) (chi4 (n + 3) ι)).2 - 3 * ((Jq (n + 3) (chi4 (n + 3) ι)).1 * (Jq (n + 3) (chi4 (n + 3) ι)).1)))) : chi4 (n + 3) ι (nsm (ring (4 * (n + 3) + 3 + 1)) (4 * 2 + 3 + 1 + 1) (ring (4 * (n + 3) + 3 + 1)).one) = (1, 0) := by
  have hc := lhs_one n hpr ι hi
  have hr : rhs (castZ F (Jq (n + 3) (chi4 (n + 3) ι)).1) (castZ F (Jq (n + 3) (chi4 (n + 3) ι)).2) = F.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F (Int.ofNat 13) = F.zero := CubeAp.castP 11
      rw [h0, zero_mul']
  have hn : nrm (castZ F (Jq (n + 3) (chi4 (n + 3) ι)).1) (castZ F (Jq (n + 3) (chi4 (n + 3) ι)).2) ≠ F.zero := fun e => by
    have e1 : castZ F (Int.ofNat (4 * (n + 3) + 3 + 1 + 1)) = F.zero := by rw [← quart_norm (n + 3) hpr ι hi, cast_norm]; exact e
    have e2 : mk (4 * 2 + 3 + 1) (4 * (n + 3) + 3 + 1 + 1) = F.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hdq : ∀ P' : Nat, mk (4 * 2 + 3 + 1) P' = F.zero → Dv (4 * 2 + 3 + 1 + 1) P' := fun P' h => md_zero_dv (4 * 2 + 3 + 1) P' (congrArg Subtype.val h)
    exact not_dvq n hpr (hdq _ e2)
  have h1 := hc.trans (table' _ _ hr hn)
  rw [show chib (n + 3) ι (nsm (ring (4 * (n + 3) + 3 + 1)) (4 * 2 + 3 + 1 + 1) (ring (4 * (n + 3) + 3 + 1)).one) = gconj (chi4 (n + 3) ι (nsm (ring (4 * (n + 3) + 3 + 1)) (4 * 2 + 3 + 1 + 1) (ring (4 * (n + 3) + 3 + 1)).one)) from chi_ivq (n + 3) hpr ι hi _] at h1
  rcases chi4_cases n ι (nsm (ring (4 * (n + 3) + 3 + 1)) (4 * 2 + 3 + 1 + 1) (ring (4 * (n + 3) + 3 + 1)).one) with e | e | e | e | e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion (Nat.succ.inj e'))
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')

end QuartThirteen
#print axioms QuartThirteen.lt_wrap
#print axioms QuartThirteen.ndvN
#print axioms QuartThirteen.q_prime
#print axioms QuartThirteen.q_ne
#print axioms QuartThirteen.not_dvq
#print axioms QuartThirteen.md_red
#print axioms QuartThirteen.add_nat
#print axioms QuartThirteen.mul_nat
#print axioms QuartThirteen.neg_nat
#print axioms QuartThirteen.addE
#print axioms QuartThirteen.mulE
#print axioms QuartThirteen.negE
#print axioms QuartThirteen.c4E
#print axioms QuartThirteen.pwE
#print axioms QuartThirteen.nrmE
#print axioms QuartThirteen.lhsE
#print axioms QuartThirteen.rhsE
#print axioms QuartThirteen.row0
#print axioms QuartThirteen.row1
#print axioms QuartThirteen.row2
#print axioms QuartThirteen.row3
#print axioms QuartThirteen.row4
#print axioms QuartThirteen.row5
#print axioms QuartThirteen.row6
#print axioms QuartThirteen.row7
#print axioms QuartThirteen.row8
#print axioms QuartThirteen.row9
#print axioms QuartThirteen.row10
#print axioms QuartThirteen.row11
#print axioms QuartThirteen.row12
#print axioms QuartThirteen.chk_nat
#print axioms QuartThirteen.chk_all
#print axioms QuartThirteen.table
#print axioms QuartThirteen.table'
#print axioms QuartThirteen.cast_norm
#print axioms QuartThirteen.cast_rhs
#print axioms QuartThirteen.hP
#print axioms QuartThirteen.chi4_cases
#print axioms QuartThirteen.lhs_one
#print axioms QuartThirteen.quart_thirteen
#print axioms QuartThirteen.quart_thirteen_conv

#print axioms: 74 theorems, no axioms.

78.60 Quartic characters of \(3\) and \(7\)

Quartic reciprocity at the inert primes \(3,7\) with \(\chi (-1)=(-1)^{(p-1)/4}\) and a table over \(\mathbb F_q\).

\[ p\equiv 1\ (8):\ 3\mid b,\ 7\mid ab;\qquad p\equiv 5\ (8):\ 3\mid a,\ 7\mid a^2-b^2 . \]

lang/QuartThree1.lean

/-
  lang/QuartThree1 — T43: the quartic character of 3 for p ≡ 1 (mod 8) (generated by work/gen_quart_inert.py).

  p = 4N + 5 with N = (2 * m + 1) (p ≡ 1 mod 8, so χ(−1) = 1), χ the quartic character of 𝔽_p, J = J(χ, χ) = a + bi, N(J) = p.
  QuartRecip at the inert prime q = 3 (k = 0) gives χ(q) χ̄(−1) = (J²)^1 p^0 in 𝔽_3[i], and a 9-case table over 𝔽_3 (through the
  builtin Nat.mod, proved equal to ring q pointwise) shows (J²)^1 N(J)^0 = χ(−1) iff b ≡ 0 (when N(J) ≢ 0):
      3 is a quartic residue mod p  ⇔  3 | b.
    chi_m1             χ(−1) = 1
    table, table'      the table (both directions)
    quart_three_1, quart_three_1_conv
  Lean 4 core only, proofs by hand (lib_ring, lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.QuartFive

namespace QuartThree1

open FTAChain ZMod DetChain Frobenius QuartChar QuartJacobi QuartRecip LibGcd CubicFive CubicRecip
open LibRing (castZ castN)

theorem lt_wrap : ∀ q a : Nat, a + 1 + q = q → False
  | 0, _, h => Nat.noConfusion h
  | q + 1, a, h => lt_wrap q a (Nat.succ.inj h)

theorem ndvN (N m' q : Nat) (h : N = q * (m' + 1)) (hr : rem (m' + 1) N = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem q_prime : ∀ d, Dv d (4 * 0 + 1 + 1 + 1) → d = 1 ∨ d = 4 * 0 + 1 + 1 + 1 := fun d hd =>
  match dv_le d 2 hd, hd with
  | ⟨k, hk⟩, ⟨q', hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndvN 3 1 q' hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact Or.inr rfl
    | d + 4 => exact (lt_wrap 3 (d + k) ((show d + k + 1 + 3 = d + 4 + k by lib_nat).trans hk)).elim

variable (m : Nat)

theorem q_ne : nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 0 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one ≠ (ring (4 * (2 * m + 1) + 3 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 3 (by rw [show 4 * (2 * m + 1) + 3 + 1 + 1 = 3 + (8 * m + 5) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 3 _))] at h
  exact Nat.noConfusion h

theorem not_dvq (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (h : Dv (4 * 0 + 1 + 1 + 1) (4 * (2 * m + 1) + 3 + 1 + 1)) : False :=
  match hpr 3 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => lt_wrap 3 (8 * m + 5) ((show 8 * m + 5 + 1 + 3 = 4 * (2 * m + 1) + 3 + 1 + 1 by lib_nat).trans e.symm)

/-- **χ(−1) = 1** for p ≡ 1 (mod 8). -/
theorem chi_m1 (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) : chi4 (2 * m + 1) ι ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) = (1, 0) := by
  have hm : ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) ≠ (ring (4 * (2 * m + 1) + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' _ (by
    show (ring (4 * (2 * m + 1) + 3 + 1)).one = (ring (4 * (2 * m + 1) + 3 + 1)).zero
    calc (ring (4 * (2 * m + 1) + 3 + 1)).one = (ring (4 * (2 * m + 1) + 3 + 1)).neg ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) := by lib_ring
      _ = (ring (4 * (2 * m + 1) + 3 + 1)).zero := by rw [e]; lib_ring)
  have h : pw (ring (4 * (2 * m + 1) + 3 + 1)) ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) ((2 * m + 1) + 1) = pw (ring (4 * (2 * m + 1) + 3 + 1)) ι 0 := by
    rw [show (2 * m + 1) + 1 = 2 * (m + 1) by lib_nat, pw_mul_split, show pw (ring (4 * (2 * m + 1) + 3 + 1)) ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) 2 = (ring (4 * (2 * m + 1) + 3 + 1)).one by show (ring (4 * (2 * m + 1) + 3 + 1)).mul ((ring (4 * (2 * m + 1) + 3 + 1)).mul (ring (4 * (2 * m + 1) + 3 + 1)).one _) _ = _; lib_ring, ZMod.pw_one_r]; rfl
  exact val4 (2 * m + 1) hpr ι hi _ hm 0 (Nat.zero_lt_succ 3) h

abbrev F := ring (4 * 0 + 1 + 1)
abbrev C := Cyclo4.cyc4 F

def sC : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1) := (castZ F 1, castZ F (-0))
def nrm (X Y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := F.add (F.mul X X) (F.mul Y Y)
def lhs (X Y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1) :=
  C.mul (pw C (C.mul (C.mul (X, Y) (X, Y)) C.one) 1) (pw C (nrm X Y, F.zero) 0)
def rhs (_X Y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := Y

theorem md_red (a r k : Nat) (h : a = r + k * (4 * 0 + 1 + 1 + 1)) (hr : r < 4 * 0 + 1 + 1 + 1) : md (4 * 0 + 1 + 1) a = r := by
  rw [h, md_add_mul]; exact md_small _ r hr

def fm (a : Nat) : Zp (4 * 0 + 1 + 1) := ⟨a % 3, Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
def fadd (x y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := fm (x.val + y.val)
def fmul (x y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := fm (x.val * y.val)
def fneg (x : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := fm ((4 * 0 + 1 + 1) * x.val)

theorem add_nat : ∀ x y : Nat, x < 3 → y < 3 → md (4 * 0 + 1 + 1) (x + y) = (x + y) % 3
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 0, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 1 : 1 < 4 * 0 + 1 + 1 + 1)
  | 0, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 0 : 2 < 4 * 0 + 1 + 1 + 1)
  | 1, 0, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 1 : 1 < 4 * 0 + 1 + 1 + 1)
  | 1, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 0 : 2 < 4 * 0 + 1 + 1 + 1)
  | 1, 2, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 2, 0, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 0 : 2 < 4 * 0 + 1 + 1 + 1)
  | 2, 1, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 2, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 1 : 1 < 4 * 0 + 1 + 1 + 1)
  | k + 3, _, h, _ => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))
  | _, k + 3, _, h => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem mul_nat : ∀ x y : Nat, x < 3 → y < 3 → md (4 * 0 + 1 + 1) (x * y) = (x * y) % 3
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 0, 1, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 0, 2, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 1, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 1, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 1 : 1 < 4 * 0 + 1 + 1 + 1)
  | 1, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 0 : 2 < 4 * 0 + 1 + 1 + 1)
  | 2, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 2, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 0 : 2 < 4 * 0 + 1 + 1 + 1)
  | 2, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 1 : 1 < 4 * 0 + 1 + 1 + 1)
  | k + 3, _, h, _ => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))
  | _, k + 3, _, h => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem neg_nat : ∀ x : Nat, x < 3 → md (4 * 0 + 1 + 1) ((4 * 0 + 1 + 1) * x) = ((4 * 0 + 1 + 1) * x) % 3
  | 0, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 1, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 0 : 2 < 4 * 0 + 1 + 1 + 1)
  | 2, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 1 : 1 < 4 * 0 + 1 + 1 + 1)
  | k + 3, h => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem addE (x y : Zp (4 * 0 + 1 + 1)) : F.add x y = fadd x y := Subtype.ext (add_nat x.val y.val x.2 y.2)
theorem mulE (x y : Zp (4 * 0 + 1 + 1)) : F.mul x y = fmul x y := Subtype.ext (mul_nat x.val y.val x.2 y.2)
theorem negE (x : Zp (4 * 0 + 1 + 1)) : F.neg x = fneg x := Subtype.ext (by show md (4 * 0 + 1 + 1) ((4 * 0 + 1 + 1) * x.val) = ((4 * 0 + 1 + 1) * x.val) % 3; exact neg_nat x.val x.2)

def c4f (u v : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1) :=
  (fadd (fmul u.1 v.1) (fneg (fmul u.2 v.2)), fadd (fmul u.1 v.2) (fmul u.2 v.1))
theorem c4E (u v : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)) : C.mul u v = c4f u v := by
  show Cyclo4.c4mul F u v = _
  unfold Cyclo4.c4mul
  rw [mulE, mulE, mulE, mulE, negE, addE, addE]; rfl
def pwf (z : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)) : Nat → Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)
  | 0 => C.one
  | k + 1 => c4f (pwf z k) z
theorem pwE (z : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)) : ∀ k, pw C z k = pwf z k
  | 0 => rfl
  | k + 1 => by show C.mul (pw C z k) z = _; rw [c4E, pwE z k]; rfl

def nrmf (X Y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := fadd (fmul X X) (fmul Y Y)
def lhsf (X Y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1) := c4f (pwf (c4f (c4f (X, Y) (X, Y)) C.one) 1) (pwf (nrmf X Y, F.zero) 0)
def rhsf (_X Y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := Y

theorem nrmE (X Y : Zp (4 * 0 + 1 + 1)) : nrm X Y = nrmf X Y := by
  unfold nrm nrmf; rw [mulE, mulE, addE]
theorem lhsE (X Y : Zp (4 * 0 + 1 + 1)) : lhs X Y = lhsf X Y := by
  unfold lhs lhsf; rw [nrmE, c4E, c4E, pwE, pwE, c4E]
theorem rhsE (X Y : Zp (4 * 0 + 1 + 1)) : rhs X Y = rhsf X Y := by
  unfold rhs rhsf
  repeat (first | rw [mulE] | rw [addE] | rw [negE])
  try rfl

def code (z : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)) : Nat := z.1.val + 3 * z.2.val
def chk (X Y : Zp (4 * 0 + 1 + 1)) : Bool := (!(Nat.beq (code (lhsf X Y)) 1) || Nat.beq (rhsf X Y).val 0) &&
  (!(Nat.beq (rhsf X Y).val 0) || Nat.beq (nrmf X Y).val 0 || Nat.beq (code (lhsf X Y)) 1)

theorem row0 : ∀ y : Nat, y < 3 → chk (mk (4 * 0 + 1 + 1) 0) (mk (4 * 0 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | k + 3, h => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem row1 : ∀ y : Nat, y < 3 → chk (mk (4 * 0 + 1 + 1) 1) (mk (4 * 0 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | k + 3, h => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem row2 : ∀ y : Nat, y < 3 → chk (mk (4 * 0 + 1 + 1) 2) (mk (4 * 0 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | k + 3, h => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem chk_nat : ∀ x y : Nat, x < 3 → y < 3 → chk (mk (4 * 0 + 1 + 1) x) (mk (4 * 0 + 1 + 1) y) = true
  | 0, y, _, h => row0 y h
  | 1, y, _, h => row1 y h
  | 2, y, _, h => row2 y h
  | k + 3, _, h, _ => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem chk_all (X Y : Zp (4 * 0 + 1 + 1)) : chk X Y = true := by
  rw [show X = mk (4 * 0 + 1 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (4 * 0 + 1 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

theorem ne_of_beq (a b : Nat) (h : Nat.beq a b = false) (e : a = b) : False := by
  rw [e, beq_self] at h; exact Bool.noConfusion h

theorem code_eq (z : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)) (h : code z = 1) : z = sC := by
  match z, h with
  | (u, v), h =>
    have e' : u.val + 3 * v.val = 1 := h
    have hv : v.val = 0 := by
      cases hv' : v.val with
      | zero => rfl
      | succ t =>
        rw [hv'] at e'
        exact (lt_wrap 3 (u.val + 3 * t + 1) ((show u.val + 3 * t + 1 + 1 + 3 = u.val + 3 * (t + 1) + 2 by lib_nat).trans
          (by rw [show u.val + 3 * (t + 1) + 2 = (u.val + 3 * (t + 1)) + 2 from rfl, e']))).elim
    have hu : u.val = 1 := by rw [hv] at e'; exact e'
    exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem table (X Y : Zp (4 * 0 + 1 + 1)) (h : lhs X Y = sC) : rhs X Y = F.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [← lhsE, h] at hc
  rw [rhsE]
  exact (Subtype.ext (beq_eq _ _ hc) : rhsf X Y = ⟨0, Nat.zero_lt_succ _⟩)

theorem table' (X Y : Zp (4 * 0 + 1 + 1)) (h : rhs X Y = F.zero) (hn : nrm X Y ≠ F.zero) : lhs X Y = sC := by
  have hc := band_r _ _ (chk_all X Y)
  rw [← rhsE, h] at hc
  rw [lhsE]
  rw [nrmE] at hn
  cases hb : Nat.beq (nrmf X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrmf X Y = F.zero) hn
  | false =>
    rw [hb] at hc
    exact code_eq _ (beq_eq _ _ hc)

theorem cast_norm (x y : Int) : castZ F (x * x + y * y) = nrm (castZ F x) (castZ F y) := by
  rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F (y) = rhs (castZ F x) (castZ F y) := by
  rw [show y = y from rfl]
  repeat (first | rw [HasseDavenport.castZ_mul] | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg])
  rfl

theorem hP (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) :
    ph4 F (Ppq (2 * m + 1)) = (nrm (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2), F.zero) := by
  rw [show Ppq (2 * m + 1) = (Int.ofNat (4 * (2 * m + 1) + 3 + 1 + 1), 0) from sum_oneq (2 * m + 1) _, ← quart_norm (2 * m + 1) hpr ι hi]; unfold ph4; rw [cast_norm]; rfl

theorem chi4_cases (ι x : Zp (4 * (2 * m + 1) + 3 + 1)) : chi4 (2 * m + 1) ι x = (0, 0) ∨ chi4 (2 * m + 1) ι x = (1, 0) ∨ chi4 (2 * m + 1) ι x = (0, 1) ∨ chi4 (2 * m + 1) ι x = (-1, 0) ∨ chi4 (2 * m + 1) ι x = (0, -1) := by
  unfold chi4
  by_cases h1 : x = (ring (4 * (2 * m + 1) + 3 + 1)).zero
  · exact Or.inl (ite_t _ _ h1)
  · rw [ite_f _ _ h1]
    by_cases h2 : pw (ring (4 * (2 * m + 1) + 3 + 1)) x ((2 * m + 1) + 1) = (ring (4 * (2 * m + 1) + 3 + 1)).one
    · exact Or.inr (Or.inl (ite_t _ _ h2))
    · rw [ite_f _ _ h2]
      by_cases h3 : pw (ring (4 * (2 * m + 1) + 3 + 1)) x ((2 * m + 1) + 1) = ι
      · exact Or.inr (Or.inr (Or.inl (ite_t _ _ h3)))
      · rw [ite_f _ _ h3]
        by_cases h4 : pw (ring (4 * (2 * m + 1) + 3 + 1)) x ((2 * m + 1) + 1) = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one
        · exact Or.inr (Or.inr (Or.inr (Or.inl (ite_t _ _ h4))))
        · exact Or.inr (Or.inr (Or.inr (Or.inr (ite_f _ _ h4))))

theorem recip (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) :
    C.mul (ph4 F (chi4 (2 * m + 1) ι (nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 0 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one))) sC = lhs (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) := by
  have hc := quart_recip_inert (2 * m + 1) 0 hpr q_prime ι hi (q_ne m)
  rw [show chib (2 * m + 1) ι ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) = gconj (chi4 (2 * m + 1) ι ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one)) from chi_ivq (2 * m + 1) hpr ι hi _, chi_m1 m hpr ι hi, hP m hpr ι hi, ph4_one] at hc
  exact hc

/-- **3 a quartic residue mod p ⇒ 3 | b**. -/
theorem quart_three_1 (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one)
    (hq : chi4 (2 * m + 1) ι (nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 0 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one) = (1, 0)) : IDv (Int.ofNat 3) ((Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) := by
  have hc := recip m hpr ι hi
  rw [hq] at hc
  have hl : lhs (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) = sC := hc.symm.trans (by rw [c4E]; rfl)
  exact CubicTwo.cast_zero_gen (4 * 0 + 1 + 1) _ ((cast_rhs _ _).trans (table _ _ hl))

/-- **3 | b ⇒ 3 is a quartic residue mod p.** -/
theorem quart_three_1_conv (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one)
    (hd : IDv (Int.ofNat 3) ((Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2)) : chi4 (2 * m + 1) ι (nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 0 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one) = (1, 0) := by
  have hc := recip m hpr ι hi
  have hr : rhs (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) = F.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F (Int.ofNat 3) = F.zero := CubeAp.castP 1
      rw [h0, zero_mul']
  have hn : nrm (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) ≠ F.zero := fun e => by
    have e1 : castZ F (Int.ofNat (4 * (2 * m + 1) + 3 + 1 + 1)) = F.zero := by rw [← quart_norm (2 * m + 1) hpr ι hi, cast_norm]; exact e
    have e2 : mk (4 * 0 + 1 + 1) (4 * (2 * m + 1) + 3 + 1 + 1) = F.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hdq : ∀ P' : Nat, mk (4 * 0 + 1 + 1) P' = F.zero → Dv (4 * 0 + 1 + 1 + 1) P' := fun P' h => md_zero_dv (4 * 0 + 1 + 1) P' (congrArg Subtype.val h)
    exact not_dvq m hpr (hdq _ e2)
  have h1 := hc.trans (table' _ _ hr hn)
  rcases chi4_cases m ι (nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 0 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one) with e | e | e | e | e
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)

end QuartThree1
#print axioms QuartThree1.lt_wrap
#print axioms QuartThree1.ndvN
#print axioms QuartThree1.q_prime
#print axioms QuartThree1.q_ne
#print axioms QuartThree1.not_dvq
#print axioms QuartThree1.chi_m1
#print axioms QuartThree1.md_red
#print axioms QuartThree1.add_nat
#print axioms QuartThree1.mul_nat
#print axioms QuartThree1.neg_nat
#print axioms QuartThree1.addE
#print axioms QuartThree1.mulE
#print axioms QuartThree1.negE
#print axioms QuartThree1.c4E
#print axioms QuartThree1.pwE
#print axioms QuartThree1.nrmE
#print axioms QuartThree1.lhsE
#print axioms QuartThree1.rhsE
#print axioms QuartThree1.row0
#print axioms QuartThree1.row1
#print axioms QuartThree1.row2
#print axioms QuartThree1.chk_nat
#print axioms QuartThree1.chk_all
#print axioms QuartThree1.ne_of_beq
#print axioms QuartThree1.code_eq
#print axioms QuartThree1.table
#print axioms QuartThree1.table'
#print axioms QuartThree1.cast_norm
#print axioms QuartThree1.cast_rhs
#print axioms QuartThree1.hP
#print axioms QuartThree1.chi4_cases
#print axioms QuartThree1.recip
#print axioms QuartThree1.quart_three_1
#print axioms QuartThree1.quart_three_1_conv

lang/QuartThree5.lean

/-
  lang/QuartThree5 — T43: the quartic character of 3 for p ≡ 5 (mod 8) (generated by work/gen_quart_inert.py).

  p = 4N + 5 with N = (2 * m + 0) (p ≡ 5 mod 8, so χ(−1) = -1), χ the quartic character of 𝔽_p, J = J(χ, χ) = a + bi, N(J) = p.
  QuartRecip at the inert prime q = 3 (k = 0) gives χ(q) χ̄(−1) = (J²)^1 p^0 in 𝔽_3[i], and a 9-case table over 𝔽_3 (through the
  builtin Nat.mod, proved equal to ring q pointwise) shows (J²)^1 N(J)^0 = χ(−1) iff a ≡ 0 (when N(J) ≢ 0):
      3 is a quartic residue mod p  ⇔  3 | a.
    chi_m1             χ(−1) = -1
    table, table'      the table (both directions)
    quart_three_5, quart_three_5_conv
  Lean 4 core only, proofs by hand (lib_ring, lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.QuartFive

namespace QuartThree5

open FTAChain ZMod DetChain Frobenius QuartChar QuartJacobi QuartRecip LibGcd CubicFive CubicRecip
open LibRing (castZ castN)

theorem lt_wrap : ∀ q a : Nat, a + 1 + q = q → False
  | 0, _, h => Nat.noConfusion h
  | q + 1, a, h => lt_wrap q a (Nat.succ.inj h)

theorem ndvN (N m' q : Nat) (h : N = q * (m' + 1)) (hr : rem (m' + 1) N = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem q_prime : ∀ d, Dv d (4 * 0 + 1 + 1 + 1) → d = 1 ∨ d = 4 * 0 + 1 + 1 + 1 := fun d hd =>
  match dv_le d 2 hd, hd with
  | ⟨k, hk⟩, ⟨q', hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndvN 3 1 q' hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact Or.inr rfl
    | d + 4 => exact (lt_wrap 3 (d + k) ((show d + k + 1 + 3 = d + 4 + k by lib_nat).trans hk)).elim

variable (m : Nat)

theorem q_ne : nsm (ring (4 * (2 * m + 0) + 3 + 1)) (4 * 0 + 1 + 1 + 1) (ring (4 * (2 * m + 0) + 3 + 1)).one ≠ (ring (4 * (2 * m + 0) + 3 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 3 (by rw [show 4 * (2 * m + 0) + 3 + 1 + 1 = 3 + (8 * m + 1) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 3 _))] at h
  exact Nat.noConfusion h

theorem not_dvq (hpr : ∀ d, Dv d (4 * (2 * m + 0) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 0) + 3 + 1 + 1) (h : Dv (4 * 0 + 1 + 1 + 1) (4 * (2 * m + 0) + 3 + 1 + 1)) : False :=
  match hpr 3 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => lt_wrap 3 (8 * m + 1) ((show 8 * m + 1 + 1 + 3 = 4 * (2 * m + 0) + 3 + 1 + 1 by lib_nat).trans e.symm)

/-- **χ(−1) = -1** for p ≡ 5 (mod 8). -/
theorem chi_m1 (hpr : ∀ d, Dv d (4 * (2 * m + 0) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 0) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 0) + 3 + 1)) (hi : (ring (4 * (2 * m + 0) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one) : chi4 (2 * m + 0) ι ((ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one) = (-1, 0) := by
  have hm : ((ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one) ≠ (ring (4 * (2 * m + 0) + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' _ (by
    show (ring (4 * (2 * m + 0) + 3 + 1)).one = (ring (4 * (2 * m + 0) + 3 + 1)).zero
    calc (ring (4 * (2 * m + 0) + 3 + 1)).one = (ring (4 * (2 * m + 0) + 3 + 1)).neg ((ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one) := by lib_ring
      _ = (ring (4 * (2 * m + 0) + 3 + 1)).zero := by rw [e]; lib_ring)
  have h : pw (ring (4 * (2 * m + 0) + 3 + 1)) ((ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one) ((2 * m + 0) + 1) = pw (ring (4 * (2 * m + 0) + 3 + 1)) ι 2 := by
    rw [show (2 * m + 0) + 1 = 2 * (m + 0) + 1 by lib_nat]
    show (ring (4 * (2 * m + 0) + 3 + 1)).mul (pw (ring (4 * (2 * m + 0) + 3 + 1)) ((ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one) (2 * (m + 0))) ((ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one) = (ring (4 * (2 * m + 0) + 3 + 1)).mul ((ring (4 * (2 * m + 0) + 3 + 1)).mul (ring (4 * (2 * m + 0) + 3 + 1)).one ι) ι
    rw [pw_mul_split, show pw (ring (4 * (2 * m + 0) + 3 + 1)) ((ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one) 2 = (ring (4 * (2 * m + 0) + 3 + 1)).one by show (ring (4 * (2 * m + 0) + 3 + 1)).mul ((ring (4 * (2 * m + 0) + 3 + 1)).mul (ring (4 * (2 * m + 0) + 3 + 1)).one _) _ = _; lib_ring, ZMod.pw_one_r]
    rw [show (ring (4 * (2 * m + 0) + 3 + 1)).mul ((ring (4 * (2 * m + 0) + 3 + 1)).mul (ring (4 * (2 * m + 0) + 3 + 1)).one ι) ι = (ring (4 * (2 * m + 0) + 3 + 1)).mul ι ι by lib_ring, hi]; lib_ring
  exact val4 (2 * m + 0) hpr ι hi _ hm 2 (Nat.lt_succ_of_le (Nat.le_succ 2)) h

abbrev F := ring (4 * 0 + 1 + 1)
abbrev C := Cyclo4.cyc4 F

def sC : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1) := (castZ F (-1), castZ F (-0))
def nrm (X Y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := F.add (F.mul X X) (F.mul Y Y)
def lhs (X Y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1) :=
  C.mul (pw C (C.mul (C.mul (X, Y) (X, Y)) C.one) 1) (pw C (nrm X Y, F.zero) 0)
def rhs (X _Y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := X

theorem md_red (a r k : Nat) (h : a = r + k * (4 * 0 + 1 + 1 + 1)) (hr : r < 4 * 0 + 1 + 1 + 1) : md (4 * 0 + 1 + 1) a = r := by
  rw [h, md_add_mul]; exact md_small _ r hr

def fm (a : Nat) : Zp (4 * 0 + 1 + 1) := ⟨a % 3, Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
def fadd (x y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := fm (x.val + y.val)
def fmul (x y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := fm (x.val * y.val)
def fneg (x : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := fm ((4 * 0 + 1 + 1) * x.val)

theorem add_nat : ∀ x y : Nat, x < 3 → y < 3 → md (4 * 0 + 1 + 1) (x + y) = (x + y) % 3
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 0, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 1 : 1 < 4 * 0 + 1 + 1 + 1)
  | 0, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 0 : 2 < 4 * 0 + 1 + 1 + 1)
  | 1, 0, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 1 : 1 < 4 * 0 + 1 + 1 + 1)
  | 1, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 0 : 2 < 4 * 0 + 1 + 1 + 1)
  | 1, 2, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 2, 0, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 0 : 2 < 4 * 0 + 1 + 1 + 1)
  | 2, 1, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 2, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 1 : 1 < 4 * 0 + 1 + 1 + 1)
  | k + 3, _, h, _ => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))
  | _, k + 3, _, h => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem mul_nat : ∀ x y : Nat, x < 3 → y < 3 → md (4 * 0 + 1 + 1) (x * y) = (x * y) % 3
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 0, 1, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 0, 2, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 1, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 1, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 1 : 1 < 4 * 0 + 1 + 1 + 1)
  | 1, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 0 : 2 < 4 * 0 + 1 + 1 + 1)
  | 2, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 2, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 0 : 2 < 4 * 0 + 1 + 1 + 1)
  | 2, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 1 : 1 < 4 * 0 + 1 + 1 + 1)
  | k + 3, _, h, _ => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))
  | _, k + 3, _, h => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem neg_nat : ∀ x : Nat, x < 3 → md (4 * 0 + 1 + 1) ((4 * 0 + 1 + 1) * x) = ((4 * 0 + 1 + 1) * x) % 3
  | 0, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 2 : 0 < 4 * 0 + 1 + 1 + 1)
  | 1, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 0 : 2 < 4 * 0 + 1 + 1 + 1)
  | 2, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 1 : 1 < 4 * 0 + 1 + 1 + 1)
  | k + 3, h => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem addE (x y : Zp (4 * 0 + 1 + 1)) : F.add x y = fadd x y := Subtype.ext (add_nat x.val y.val x.2 y.2)
theorem mulE (x y : Zp (4 * 0 + 1 + 1)) : F.mul x y = fmul x y := Subtype.ext (mul_nat x.val y.val x.2 y.2)
theorem negE (x : Zp (4 * 0 + 1 + 1)) : F.neg x = fneg x := Subtype.ext (by show md (4 * 0 + 1 + 1) ((4 * 0 + 1 + 1) * x.val) = ((4 * 0 + 1 + 1) * x.val) % 3; exact neg_nat x.val x.2)

def c4f (u v : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1) :=
  (fadd (fmul u.1 v.1) (fneg (fmul u.2 v.2)), fadd (fmul u.1 v.2) (fmul u.2 v.1))
theorem c4E (u v : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)) : C.mul u v = c4f u v := by
  show Cyclo4.c4mul F u v = _
  unfold Cyclo4.c4mul
  rw [mulE, mulE, mulE, mulE, negE, addE, addE]; rfl
def pwf (z : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)) : Nat → Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)
  | 0 => C.one
  | k + 1 => c4f (pwf z k) z
theorem pwE (z : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)) : ∀ k, pw C z k = pwf z k
  | 0 => rfl
  | k + 1 => by show C.mul (pw C z k) z = _; rw [c4E, pwE z k]; rfl

def nrmf (X Y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := fadd (fmul X X) (fmul Y Y)
def lhsf (X Y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1) := c4f (pwf (c4f (c4f (X, Y) (X, Y)) C.one) 1) (pwf (nrmf X Y, F.zero) 0)
def rhsf (X _Y : Zp (4 * 0 + 1 + 1)) : Zp (4 * 0 + 1 + 1) := X

theorem nrmE (X Y : Zp (4 * 0 + 1 + 1)) : nrm X Y = nrmf X Y := by
  unfold nrm nrmf; rw [mulE, mulE, addE]
theorem lhsE (X Y : Zp (4 * 0 + 1 + 1)) : lhs X Y = lhsf X Y := by
  unfold lhs lhsf; rw [nrmE, c4E, c4E, pwE, pwE, c4E]
theorem rhsE (X Y : Zp (4 * 0 + 1 + 1)) : rhs X Y = rhsf X Y := by
  unfold rhs rhsf
  repeat (first | rw [mulE] | rw [addE] | rw [negE])
  try rfl

def code (z : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)) : Nat := z.1.val + 3 * z.2.val
def chk (X Y : Zp (4 * 0 + 1 + 1)) : Bool := (!(Nat.beq (code (lhsf X Y)) 2) || Nat.beq (rhsf X Y).val 0) &&
  (!(Nat.beq (rhsf X Y).val 0) || Nat.beq (nrmf X Y).val 0 || Nat.beq (code (lhsf X Y)) 2)

theorem row0 : ∀ y : Nat, y < 3 → chk (mk (4 * 0 + 1 + 1) 0) (mk (4 * 0 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | k + 3, h => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem row1 : ∀ y : Nat, y < 3 → chk (mk (4 * 0 + 1 + 1) 1) (mk (4 * 0 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | k + 3, h => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem row2 : ∀ y : Nat, y < 3 → chk (mk (4 * 0 + 1 + 1) 2) (mk (4 * 0 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | k + 3, h => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem chk_nat : ∀ x y : Nat, x < 3 → y < 3 → chk (mk (4 * 0 + 1 + 1) x) (mk (4 * 0 + 1 + 1) y) = true
  | 0, y, _, h => row0 y h
  | 1, y, _, h => row1 y h
  | 2, y, _, h => row2 y h
  | k + 3, _, h, _ => absurd h (fun e => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) e))

theorem chk_all (X Y : Zp (4 * 0 + 1 + 1)) : chk X Y = true := by
  rw [show X = mk (4 * 0 + 1 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (4 * 0 + 1 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

theorem ne_of_beq (a b : Nat) (h : Nat.beq a b = false) (e : a = b) : False := by
  rw [e, beq_self] at h; exact Bool.noConfusion h

theorem code_eq (z : Zp (4 * 0 + 1 + 1) × Zp (4 * 0 + 1 + 1)) (h : code z = 2) : z = sC := by
  match z, h with
  | (u, v), h =>
    have e' : u.val + 3 * v.val = 2 := h
    have hv : v.val = 0 := by
      cases hv' : v.val with
      | zero => rfl
      | succ t =>
        rw [hv'] at e'
        exact (lt_wrap 3 (u.val + 3 * t + 0) ((show u.val + 3 * t + 0 + 1 + 3 = u.val + 3 * (t + 1) + 1 by lib_nat).trans
          (by rw [show u.val + 3 * (t + 1) + 1 = (u.val + 3 * (t + 1)) + 1 from rfl, e']))).elim
    have hu : u.val = 2 := by rw [hv] at e'; exact e'
    exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem table (X Y : Zp (4 * 0 + 1 + 1)) (h : lhs X Y = sC) : rhs X Y = F.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [← lhsE, h] at hc
  rw [rhsE]
  exact (Subtype.ext (beq_eq _ _ hc) : rhsf X Y = ⟨0, Nat.zero_lt_succ _⟩)

theorem table' (X Y : Zp (4 * 0 + 1 + 1)) (h : rhs X Y = F.zero) (hn : nrm X Y ≠ F.zero) : lhs X Y = sC := by
  have hc := band_r _ _ (chk_all X Y)
  rw [← rhsE, h] at hc
  rw [lhsE]
  rw [nrmE] at hn
  cases hb : Nat.beq (nrmf X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrmf X Y = F.zero) hn
  | false =>
    rw [hb] at hc
    exact code_eq _ (beq_eq _ _ hc)

theorem cast_norm (x y : Int) : castZ F (x * x + y * y) = nrm (castZ F x) (castZ F y) := by
  rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F (x) = rhs (castZ F x) (castZ F y) := by
  rw [show x = x from rfl]
  repeat (first | rw [HasseDavenport.castZ_mul] | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg])
  rfl

theorem hP (hpr : ∀ d, Dv d (4 * (2 * m + 0) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 0) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 0) + 3 + 1)) (hi : (ring (4 * (2 * m + 0) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one) :
    ph4 F (Ppq (2 * m + 0)) = (nrm (castZ F (Jq (2 * m + 0) (chi4 (2 * m + 0) ι)).1) (castZ F (Jq (2 * m + 0) (chi4 (2 * m + 0) ι)).2), F.zero) := by
  rw [show Ppq (2 * m + 0) = (Int.ofNat (4 * (2 * m + 0) + 3 + 1 + 1), 0) from sum_oneq (2 * m + 0) _, ← quart_norm (2 * m + 0) hpr ι hi]; unfold ph4; rw [cast_norm]; rfl

theorem chi4_cases (ι x : Zp (4 * (2 * m + 0) + 3 + 1)) : chi4 (2 * m + 0) ι x = (0, 0) ∨ chi4 (2 * m + 0) ι x = (1, 0) ∨ chi4 (2 * m + 0) ι x = (0, 1) ∨ chi4 (2 * m + 0) ι x = (-1, 0) ∨ chi4 (2 * m + 0) ι x = (0, -1) := by
  unfold chi4
  by_cases h1 : x = (ring (4 * (2 * m + 0) + 3 + 1)).zero
  · exact Or.inl (ite_t _ _ h1)
  · rw [ite_f _ _ h1]
    by_cases h2 : pw (ring (4 * (2 * m + 0) + 3 + 1)) x ((2 * m + 0) + 1) = (ring (4 * (2 * m + 0) + 3 + 1)).one
    · exact Or.inr (Or.inl (ite_t _ _ h2))
    · rw [ite_f _ _ h2]
      by_cases h3 : pw (ring (4 * (2 * m + 0) + 3 + 1)) x ((2 * m + 0) + 1) = ι
      · exact Or.inr (Or.inr (Or.inl (ite_t _ _ h3)))
      · rw [ite_f _ _ h3]
        by_cases h4 : pw (ring (4 * (2 * m + 0) + 3 + 1)) x ((2 * m + 0) + 1) = (ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one
        · exact Or.inr (Or.inr (Or.inr (Or.inl (ite_t _ _ h4))))
        · exact Or.inr (Or.inr (Or.inr (Or.inr (ite_f _ _ h4))))

theorem recip (hpr : ∀ d, Dv d (4 * (2 * m + 0) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 0) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 0) + 3 + 1)) (hi : (ring (4 * (2 * m + 0) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one) :
    C.mul (ph4 F (chi4 (2 * m + 0) ι (nsm (ring (4 * (2 * m + 0) + 3 + 1)) (4 * 0 + 1 + 1 + 1) (ring (4 * (2 * m + 0) + 3 + 1)).one))) sC = lhs (castZ F (Jq (2 * m + 0) (chi4 (2 * m + 0) ι)).1) (castZ F (Jq (2 * m + 0) (chi4 (2 * m + 0) ι)).2) := by
  have hc := quart_recip_inert (2 * m + 0) 0 hpr q_prime ι hi (q_ne m)
  rw [show chib (2 * m + 0) ι ((ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one) = gconj (chi4 (2 * m + 0) ι ((ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one)) from chi_ivq (2 * m + 0) hpr ι hi _, chi_m1 m hpr ι hi, hP m hpr ι hi, ph4_one] at hc
  exact hc

/-- **3 a quartic residue mod p ⇒ 3 | a**. -/
theorem quart_three_5 (hpr : ∀ d, Dv d (4 * (2 * m + 0) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 0) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 0) + 3 + 1)) (hi : (ring (4 * (2 * m + 0) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one)
    (hq : chi4 (2 * m + 0) ι (nsm (ring (4 * (2 * m + 0) + 3 + 1)) (4 * 0 + 1 + 1 + 1) (ring (4 * (2 * m + 0) + 3 + 1)).one) = (1, 0)) : IDv (Int.ofNat 3) ((Jq (2 * m + 0) (chi4 (2 * m + 0) ι)).1) := by
  have hc := recip m hpr ι hi
  rw [hq] at hc
  have hl : lhs (castZ F (Jq (2 * m + 0) (chi4 (2 * m + 0) ι)).1) (castZ F (Jq (2 * m + 0) (chi4 (2 * m + 0) ι)).2) = sC := hc.symm.trans (by rw [c4E]; rfl)
  exact CubicTwo.cast_zero_gen (4 * 0 + 1 + 1) _ ((cast_rhs _ _).trans (table _ _ hl))

/-- **3 | a ⇒ 3 is a quartic residue mod p.** -/
theorem quart_three_5_conv (hpr : ∀ d, Dv d (4 * (2 * m + 0) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 0) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 0) + 3 + 1)) (hi : (ring (4 * (2 * m + 0) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 0) + 3 + 1)).neg (ring (4 * (2 * m + 0) + 3 + 1)).one)
    (hd : IDv (Int.ofNat 3) ((Jq (2 * m + 0) (chi4 (2 * m + 0) ι)).1)) : chi4 (2 * m + 0) ι (nsm (ring (4 * (2 * m + 0) + 3 + 1)) (4 * 0 + 1 + 1 + 1) (ring (4 * (2 * m + 0) + 3 + 1)).one) = (1, 0) := by
  have hc := recip m hpr ι hi
  have hr : rhs (castZ F (Jq (2 * m + 0) (chi4 (2 * m + 0) ι)).1) (castZ F (Jq (2 * m + 0) (chi4 (2 * m + 0) ι)).2) = F.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F (Int.ofNat 3) = F.zero := CubeAp.castP 1
      rw [h0, zero_mul']
  have hn : nrm (castZ F (Jq (2 * m + 0) (chi4 (2 * m + 0) ι)).1) (castZ F (Jq (2 * m + 0) (chi4 (2 * m + 0) ι)).2) ≠ F.zero := fun e => by
    have e1 : castZ F (Int.ofNat (4 * (2 * m + 0) + 3 + 1 + 1)) = F.zero := by rw [← quart_norm (2 * m + 0) hpr ι hi, cast_norm]; exact e
    have e2 : mk (4 * 0 + 1 + 1) (4 * (2 * m + 0) + 3 + 1 + 1) = F.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hdq : ∀ P' : Nat, mk (4 * 0 + 1 + 1) P' = F.zero → Dv (4 * 0 + 1 + 1 + 1) P' := fun P' h => md_zero_dv (4 * 0 + 1 + 1) P' (congrArg Subtype.val h)
    exact not_dvq m hpr (hdq _ e2)
  have h1 := hc.trans (table' _ _ hr hn)
  rcases chi4_cases m ι (nsm (ring (4 * (2 * m + 0) + 3 + 1)) (4 * 0 + 1 + 1 + 1) (ring (4 * (2 * m + 0) + 3 + 1)).one) with e | e | e | e | e
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)

end QuartThree5
#print axioms QuartThree5.lt_wrap
#print axioms QuartThree5.ndvN
#print axioms QuartThree5.q_prime
#print axioms QuartThree5.q_ne
#print axioms QuartThree5.not_dvq
#print axioms QuartThree5.chi_m1
#print axioms QuartThree5.md_red
#print axioms QuartThree5.add_nat
#print axioms QuartThree5.mul_nat
#print axioms QuartThree5.neg_nat
#print axioms QuartThree5.addE
#print axioms QuartThree5.mulE
#print axioms QuartThree5.negE
#print axioms QuartThree5.c4E
#print axioms QuartThree5.pwE
#print axioms QuartThree5.nrmE
#print axioms QuartThree5.lhsE
#print axioms QuartThree5.rhsE
#print axioms QuartThree5.row0
#print axioms QuartThree5.row1
#print axioms QuartThree5.row2
#print axioms QuartThree5.chk_nat
#print axioms QuartThree5.chk_all
#print axioms QuartThree5.ne_of_beq
#print axioms QuartThree5.code_eq
#print axioms QuartThree5.table
#print axioms QuartThree5.table'
#print axioms QuartThree5.cast_norm
#print axioms QuartThree5.cast_rhs
#print axioms QuartThree5.hP
#print axioms QuartThree5.chi4_cases
#print axioms QuartThree5.recip
#print axioms QuartThree5.quart_three_5
#print axioms QuartThree5.quart_three_5_conv

lang/QuartSeven1.lean

/-
  lang/QuartSeven1 — T43: the quartic character of 7 for p ≡ 1 (mod 8) (generated by work/gen_quart_inert.py).

  p = 4N + 5 with N = (2 * m + 1) (p ≡ 1 mod 8, so χ(−1) = 1), χ the quartic character of 𝔽_p, J = J(χ, χ) = a + bi, N(J) = p.
  QuartRecip at the inert prime q = 7 (k = 1) gives χ(q) χ̄(−1) = (J²)^2 p^1 in 𝔽_7[i], and a 49-case table over 𝔽_7 (through the
  builtin Nat.mod, proved equal to ring q pointwise) shows (J²)^2 N(J)^1 = χ(−1) iff a * b ≡ 0 (when N(J) ≢ 0):
      7 is a quartic residue mod p  ⇔  7 | a * b.
    chi_m1             χ(−1) = 1
    table, table'      the table (both directions)
    quart_seven_1, quart_seven_1_conv
  Lean 4 core only, proofs by hand (lib_ring, lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.QuartFive

namespace QuartSeven1

open FTAChain ZMod DetChain Frobenius QuartChar QuartJacobi QuartRecip LibGcd CubicFive CubicRecip
open LibRing (castZ castN)

theorem lt_wrap : ∀ q a : Nat, a + 1 + q = q → False
  | 0, _, h => Nat.noConfusion h
  | q + 1, a, h => lt_wrap q a (Nat.succ.inj h)

theorem ndvN (N m' q : Nat) (h : N = q * (m' + 1)) (hr : rem (m' + 1) N = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem q_prime : ∀ d, Dv d (4 * 1 + 1 + 1 + 1) → d = 1 ∨ d = 4 * 1 + 1 + 1 + 1 := fun d hd =>
  match dv_le d 6 hd, hd with
  | ⟨k, hk⟩, ⟨q', hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndvN 7 1 q' hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact (ndvN 7 2 q' hq (fun e => Nat.noConfusion e)).elim
    | 4 => exact (ndvN 7 3 q' hq (fun e => Nat.noConfusion e)).elim
    | 5 => exact (ndvN 7 4 q' hq (fun e => Nat.noConfusion e)).elim
    | 6 => exact (ndvN 7 5 q' hq (fun e => Nat.noConfusion e)).elim
    | 7 => exact Or.inr rfl
    | d + 8 => exact (lt_wrap 7 (d + k) ((show d + k + 1 + 7 = d + 8 + k by lib_nat).trans hk)).elim

variable (m : Nat)

theorem q_ne : nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one ≠ (ring (4 * (2 * m + 1) + 3 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 7 (by rw [show 4 * (2 * m + 1) + 3 + 1 + 1 = 7 + (8 * m + 1) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 7 _))] at h
  exact Nat.noConfusion h

theorem not_dvq (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (h : Dv (4 * 1 + 1 + 1 + 1) (4 * (2 * m + 1) + 3 + 1 + 1)) : False :=
  match hpr 7 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => lt_wrap 7 (8 * m + 1) ((show 8 * m + 1 + 1 + 7 = 4 * (2 * m + 1) + 3 + 1 + 1 by lib_nat).trans e.symm)

/-- **χ(−1) = 1** for p ≡ 1 (mod 8). -/
theorem chi_m1 (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) : chi4 (2 * m + 1) ι ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) = (1, 0) := by
  have hm : ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) ≠ (ring (4 * (2 * m + 1) + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' _ (by
    show (ring (4 * (2 * m + 1) + 3 + 1)).one = (ring (4 * (2 * m + 1) + 3 + 1)).zero
    calc (ring (4 * (2 * m + 1) + 3 + 1)).one = (ring (4 * (2 * m + 1) + 3 + 1)).neg ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) := by lib_ring
      _ = (ring (4 * (2 * m + 1) + 3 + 1)).zero := by rw [e]; lib_ring)
  have h : pw (ring (4 * (2 * m + 1) + 3 + 1)) ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) ((2 * m + 1) + 1) = pw (ring (4 * (2 * m + 1) + 3 + 1)) ι 0 := by
    rw [show (2 * m + 1) + 1 = 2 * (m + 1) by lib_nat, pw_mul_split, show pw (ring (4 * (2 * m + 1) + 3 + 1)) ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) 2 = (ring (4 * (2 * m + 1) + 3 + 1)).one by show (ring (4 * (2 * m + 1) + 3 + 1)).mul ((ring (4 * (2 * m + 1) + 3 + 1)).mul (ring (4 * (2 * m + 1) + 3 + 1)).one _) _ = _; lib_ring, ZMod.pw_one_r]; rfl
  exact val4 (2 * m + 1) hpr ι hi _ hm 0 (Nat.zero_lt_succ 3) h

abbrev F := ring (4 * 1 + 1 + 1)
abbrev C := Cyclo4.cyc4 F

def sC : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) := (castZ F 1, castZ F (-0))
def nrm (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := F.add (F.mul X X) (F.mul Y Y)
def lhs (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) :=
  C.mul (pw C (C.mul (C.mul (X, Y) (X, Y)) C.one) 2) (pw C (nrm X Y, F.zero) 1)
def rhs (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := F.mul X Y

theorem md_red (a r k : Nat) (h : a = r + k * (4 * 1 + 1 + 1 + 1)) (hr : r < 4 * 1 + 1 + 1 + 1) : md (4 * 1 + 1 + 1) a = r := by
  rw [h, md_add_mul]; exact md_small _ r hr

def fm (a : Nat) : Zp (4 * 1 + 1 + 1) := ⟨a % 7, Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
def fadd (x y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fm (x.val + y.val)
def fmul (x y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fm (x.val * y.val)
def fneg (x : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fm ((4 * 1 + 1 + 1) * x.val)

theorem add_nat : ∀ x y : Nat, x < 7 → y < 7 → md (4 * 1 + 1 + 1) (x + y) = (x + y) % 7
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 0, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 0, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 0, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 0, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 0, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 1, 0, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 1, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 1, 2, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 1, 3, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 1, 4, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 1, 5, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 1, 6, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 2, 0, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 2, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 2, 3, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 2, 4, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, 5, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 2, 6, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 3, 0, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 3, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 3, 2, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 3, 4, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 3, 5, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 3, 6, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 4, 0, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 4, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 4, 3, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 4, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 4, 5, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 4, 6, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 0, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 5, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 5, 2, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 5, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 5, 4, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 5, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 6, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 6, 0, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 6, 1, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 6, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 6, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 6, 5, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 6, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))
  | _, k + 7, _, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem mul_nat : ∀ x y : Nat, x < 7 → y < 7 → md (4 * 1 + 1 + 1) (x * y) = (x * y) % 7
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 1, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 2, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 3, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 4, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 5, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 6, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 1, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 1, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 1, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 1, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 1, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 1, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 1, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 2, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 2, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 2, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 2, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 3, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 3, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 3, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 3, 4, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, 5, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 3, 6, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 4, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 4, 3, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 4, 4, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 4, 5, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 4, 6, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 5, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 5, 2, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 3, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 5, 4, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 5, 5, _, _ => md_red _ 4 3 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 5, 6, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 6, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 6, 2, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 6, 3, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 6, 4, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 6, 5, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, 6, _, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))
  | _, k + 7, _, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem neg_nat : ∀ x : Nat, x < 7 → md (4 * 1 + 1 + 1) ((4 * 1 + 1 + 1) * x) = ((4 * 1 + 1 + 1) * x) % 7
  | 0, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 1, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem addE (x y : Zp (4 * 1 + 1 + 1)) : F.add x y = fadd x y := Subtype.ext (add_nat x.val y.val x.2 y.2)
theorem mulE (x y : Zp (4 * 1 + 1 + 1)) : F.mul x y = fmul x y := Subtype.ext (mul_nat x.val y.val x.2 y.2)
theorem negE (x : Zp (4 * 1 + 1 + 1)) : F.neg x = fneg x := Subtype.ext (by show md (4 * 1 + 1 + 1) ((4 * 1 + 1 + 1) * x.val) = ((4 * 1 + 1 + 1) * x.val) % 7; exact neg_nat x.val x.2)

def c4f (u v : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) :=
  (fadd (fmul u.1 v.1) (fneg (fmul u.2 v.2)), fadd (fmul u.1 v.2) (fmul u.2 v.1))
theorem c4E (u v : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : C.mul u v = c4f u v := by
  show Cyclo4.c4mul F u v = _
  unfold Cyclo4.c4mul
  rw [mulE, mulE, mulE, mulE, negE, addE, addE]; rfl
def pwf (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : Nat → Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)
  | 0 => C.one
  | k + 1 => c4f (pwf z k) z
theorem pwE (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : ∀ k, pw C z k = pwf z k
  | 0 => rfl
  | k + 1 => by show C.mul (pw C z k) z = _; rw [c4E, pwE z k]; rfl

def nrmf (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fadd (fmul X X) (fmul Y Y)
def lhsf (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) := c4f (pwf (c4f (c4f (X, Y) (X, Y)) C.one) 2) (pwf (nrmf X Y, F.zero) 1)
def rhsf (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fmul X Y

theorem nrmE (X Y : Zp (4 * 1 + 1 + 1)) : nrm X Y = nrmf X Y := by
  unfold nrm nrmf; rw [mulE, mulE, addE]
theorem lhsE (X Y : Zp (4 * 1 + 1 + 1)) : lhs X Y = lhsf X Y := by
  unfold lhs lhsf; rw [nrmE, c4E, c4E, pwE, pwE, c4E]
theorem rhsE (X Y : Zp (4 * 1 + 1 + 1)) : rhs X Y = rhsf X Y := by
  unfold rhs rhsf
  repeat (first | rw [mulE] | rw [addE] | rw [negE])
  try rfl

def code (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : Nat := z.1.val + 7 * z.2.val
def chk (X Y : Zp (4 * 1 + 1 + 1)) : Bool := (!(Nat.beq (code (lhsf X Y)) 1) || Nat.beq (rhsf X Y).val 0) &&
  (!(Nat.beq (rhsf X Y).val 0) || Nat.beq (nrmf X Y).val 0 || Nat.beq (code (lhsf X Y)) 1)

theorem row0 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 0) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row1 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 1) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row2 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 2) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row3 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 3) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row4 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 4) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row5 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 5) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row6 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 6) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem chk_nat : ∀ x y : Nat, x < 7 → y < 7 → chk (mk (4 * 1 + 1 + 1) x) (mk (4 * 1 + 1 + 1) y) = true
  | 0, y, _, h => row0 y h
  | 1, y, _, h => row1 y h
  | 2, y, _, h => row2 y h
  | 3, y, _, h => row3 y h
  | 4, y, _, h => row4 y h
  | 5, y, _, h => row5 y h
  | 6, y, _, h => row6 y h
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem chk_all (X Y : Zp (4 * 1 + 1 + 1)) : chk X Y = true := by
  rw [show X = mk (4 * 1 + 1 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (4 * 1 + 1 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

theorem ne_of_beq (a b : Nat) (h : Nat.beq a b = false) (e : a = b) : False := by
  rw [e, beq_self] at h; exact Bool.noConfusion h

theorem code_eq (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) (h : code z = 1) : z = sC := by
  match z, h with
  | (u, v), h =>
    have e' : u.val + 7 * v.val = 1 := h
    have hv : v.val = 0 := by
      cases hv' : v.val with
      | zero => rfl
      | succ t =>
        rw [hv'] at e'
        exact (lt_wrap 7 (u.val + 7 * t + 5) ((show u.val + 7 * t + 5 + 1 + 7 = u.val + 7 * (t + 1) + 6 by lib_nat).trans
          (by rw [show u.val + 7 * (t + 1) + 6 = (u.val + 7 * (t + 1)) + 6 from rfl, e']))).elim
    have hu : u.val = 1 := by rw [hv] at e'; exact e'
    exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem table (X Y : Zp (4 * 1 + 1 + 1)) (h : lhs X Y = sC) : rhs X Y = F.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [← lhsE, h] at hc
  rw [rhsE]
  exact (Subtype.ext (beq_eq _ _ hc) : rhsf X Y = ⟨0, Nat.zero_lt_succ _⟩)

theorem table' (X Y : Zp (4 * 1 + 1 + 1)) (h : rhs X Y = F.zero) (hn : nrm X Y ≠ F.zero) : lhs X Y = sC := by
  have hc := band_r _ _ (chk_all X Y)
  rw [← rhsE, h] at hc
  rw [lhsE]
  rw [nrmE] at hn
  cases hb : Nat.beq (nrmf X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrmf X Y = F.zero) hn
  | false =>
    rw [hb] at hc
    exact code_eq _ (beq_eq _ _ hc)

theorem cast_norm (x y : Int) : castZ F (x * x + y * y) = nrm (castZ F x) (castZ F y) := by
  rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F (x * y) = rhs (castZ F x) (castZ F y) := by
  rw [show x * y = x * y from rfl]
  repeat (first | rw [HasseDavenport.castZ_mul] | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg])
  rfl

theorem hP (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) :
    ph4 F (Ppq (2 * m + 1)) = (nrm (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2), F.zero) := by
  rw [show Ppq (2 * m + 1) = (Int.ofNat (4 * (2 * m + 1) + 3 + 1 + 1), 0) from sum_oneq (2 * m + 1) _, ← quart_norm (2 * m + 1) hpr ι hi]; unfold ph4; rw [cast_norm]; rfl

theorem chi4_cases (ι x : Zp (4 * (2 * m + 1) + 3 + 1)) : chi4 (2 * m + 1) ι x = (0, 0) ∨ chi4 (2 * m + 1) ι x = (1, 0) ∨ chi4 (2 * m + 1) ι x = (0, 1) ∨ chi4 (2 * m + 1) ι x = (-1, 0) ∨ chi4 (2 * m + 1) ι x = (0, -1) := by
  unfold chi4
  by_cases h1 : x = (ring (4 * (2 * m + 1) + 3 + 1)).zero
  · exact Or.inl (ite_t _ _ h1)
  · rw [ite_f _ _ h1]
    by_cases h2 : pw (ring (4 * (2 * m + 1) + 3 + 1)) x ((2 * m + 1) + 1) = (ring (4 * (2 * m + 1) + 3 + 1)).one
    · exact Or.inr (Or.inl (ite_t _ _ h2))
    · rw [ite_f _ _ h2]
      by_cases h3 : pw (ring (4 * (2 * m + 1) + 3 + 1)) x ((2 * m + 1) + 1) = ι
      · exact Or.inr (Or.inr (Or.inl (ite_t _ _ h3)))
      · rw [ite_f _ _ h3]
        by_cases h4 : pw (ring (4 * (2 * m + 1) + 3 + 1)) x ((2 * m + 1) + 1) = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one
        · exact Or.inr (Or.inr (Or.inr (Or.inl (ite_t _ _ h4))))
        · exact Or.inr (Or.inr (Or.inr (Or.inr (ite_f _ _ h4))))

theorem recip (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) :
    C.mul (ph4 F (chi4 (2 * m + 1) ι (nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one))) sC = lhs (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) := by
  have hc := quart_recip_inert (2 * m + 1) 1 hpr q_prime ι hi (q_ne m)
  rw [show chib (2 * m + 1) ι ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one) = gconj (chi4 (2 * m + 1) ι ((ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one)) from chi_ivq (2 * m + 1) hpr ι hi _, chi_m1 m hpr ι hi, hP m hpr ι hi, ph4_one] at hc
  exact hc

/-- **7 a quartic residue mod p ⇒ 7 | a * b**. -/
theorem quart_seven_1 (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one)
    (hq : chi4 (2 * m + 1) ι (nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one) = (1, 0)) : IDv (Int.ofNat 7) ((Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1 * (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) := by
  have hc := recip m hpr ι hi
  rw [hq] at hc
  have hl : lhs (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) = sC := hc.symm.trans (by rw [c4E]; rfl)
  exact CubicTwo.cast_zero_gen (4 * 1 + 1 + 1) _ ((cast_rhs _ _).trans (table _ _ hl))

/-- **7 | a * b ⇒ 7 is a quartic residue mod p.** -/
theorem quart_seven_1_conv (hpr : ∀ d, Dv d (4 * (2 * m + 1) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 1) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 1) + 3 + 1)) (hi : (ring (4 * (2 * m + 1) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 1) + 3 + 1)).neg (ring (4 * (2 * m + 1) + 3 + 1)).one)
    (hd : IDv (Int.ofNat 7) ((Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1 * (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2)) : chi4 (2 * m + 1) ι (nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one) = (1, 0) := by
  have hc := recip m hpr ι hi
  have hr : rhs (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) = F.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F (Int.ofNat 7) = F.zero := CubeAp.castP 5
      rw [h0, zero_mul']
  have hn : nrm (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).1) (castZ F (Jq (2 * m + 1) (chi4 (2 * m + 1) ι)).2) ≠ F.zero := fun e => by
    have e1 : castZ F (Int.ofNat (4 * (2 * m + 1) + 3 + 1 + 1)) = F.zero := by rw [← quart_norm (2 * m + 1) hpr ι hi, cast_norm]; exact e
    have e2 : mk (4 * 1 + 1 + 1) (4 * (2 * m + 1) + 3 + 1 + 1) = F.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hdq : ∀ P' : Nat, mk (4 * 1 + 1 + 1) P' = F.zero → Dv (4 * 1 + 1 + 1 + 1) P' := fun P' h => md_zero_dv (4 * 1 + 1 + 1) P' (congrArg Subtype.val h)
    exact not_dvq m hpr (hdq _ e2)
  have h1 := hc.trans (table' _ _ hr hn)
  rcases chi4_cases m ι (nsm (ring (4 * (2 * m + 1) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 1) + 3 + 1)).one) with e | e | e | e | e
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)

end QuartSeven1
#print axioms QuartSeven1.lt_wrap
#print axioms QuartSeven1.ndvN
#print axioms QuartSeven1.q_prime
#print axioms QuartSeven1.q_ne
#print axioms QuartSeven1.not_dvq
#print axioms QuartSeven1.chi_m1
#print axioms QuartSeven1.md_red
#print axioms QuartSeven1.add_nat
#print axioms QuartSeven1.mul_nat
#print axioms QuartSeven1.neg_nat
#print axioms QuartSeven1.addE
#print axioms QuartSeven1.mulE
#print axioms QuartSeven1.negE
#print axioms QuartSeven1.c4E
#print axioms QuartSeven1.pwE
#print axioms QuartSeven1.nrmE
#print axioms QuartSeven1.lhsE
#print axioms QuartSeven1.rhsE
#print axioms QuartSeven1.row0
#print axioms QuartSeven1.row1
#print axioms QuartSeven1.row2
#print axioms QuartSeven1.row3
#print axioms QuartSeven1.row4
#print axioms QuartSeven1.row5
#print axioms QuartSeven1.row6
#print axioms QuartSeven1.chk_nat
#print axioms QuartSeven1.chk_all
#print axioms QuartSeven1.ne_of_beq
#print axioms QuartSeven1.code_eq
#print axioms QuartSeven1.table
#print axioms QuartSeven1.table'
#print axioms QuartSeven1.cast_norm
#print axioms QuartSeven1.cast_rhs
#print axioms QuartSeven1.hP
#print axioms QuartSeven1.chi4_cases
#print axioms QuartSeven1.recip
#print axioms QuartSeven1.quart_seven_1
#print axioms QuartSeven1.quart_seven_1_conv

lang/QuartSeven5.lean

/-
  lang/QuartSeven5 — T43: the quartic character of 7 for p ≡ 5 (mod 8) (generated by work/gen_quart_inert.py).

  p = 4N + 5 with N = (2 * m + 2) (p ≡ 5 mod 8, so χ(−1) = -1), χ the quartic character of 𝔽_p, J = J(χ, χ) = a + bi, N(J) = p.
  QuartRecip at the inert prime q = 7 (k = 1) gives χ(q) χ̄(−1) = (J²)^2 p^1 in 𝔽_7[i], and a 49-case table over 𝔽_7 (through the
  builtin Nat.mod, proved equal to ring q pointwise) shows (J²)^2 N(J)^1 = χ(−1) iff a * a - b * b ≡ 0 (when N(J) ≢ 0):
      7 is a quartic residue mod p  ⇔  7 | a * a - b * b.
    chi_m1             χ(−1) = -1
    table, table'      the table (both directions)
    quart_seven_5, quart_seven_5_conv
  Lean 4 core only, proofs by hand (lib_ring, lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.QuartFive

namespace QuartSeven5

open FTAChain ZMod DetChain Frobenius QuartChar QuartJacobi QuartRecip LibGcd CubicFive CubicRecip
open LibRing (castZ castN)

theorem lt_wrap : ∀ q a : Nat, a + 1 + q = q → False
  | 0, _, h => Nat.noConfusion h
  | q + 1, a, h => lt_wrap q a (Nat.succ.inj h)

theorem ndvN (N m' q : Nat) (h : N = q * (m' + 1)) (hr : rem (m' + 1) N = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem q_prime : ∀ d, Dv d (4 * 1 + 1 + 1 + 1) → d = 1 ∨ d = 4 * 1 + 1 + 1 + 1 := fun d hd =>
  match dv_le d 6 hd, hd with
  | ⟨k, hk⟩, ⟨q', hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndvN 7 1 q' hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact (ndvN 7 2 q' hq (fun e => Nat.noConfusion e)).elim
    | 4 => exact (ndvN 7 3 q' hq (fun e => Nat.noConfusion e)).elim
    | 5 => exact (ndvN 7 4 q' hq (fun e => Nat.noConfusion e)).elim
    | 6 => exact (ndvN 7 5 q' hq (fun e => Nat.noConfusion e)).elim
    | 7 => exact Or.inr rfl
    | d + 8 => exact (lt_wrap 7 (d + k) ((show d + k + 1 + 7 = d + 8 + k by lib_nat).trans hk)).elim

variable (m : Nat)

theorem q_ne : nsm (ring (4 * (2 * m + 2) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 2) + 3 + 1)).one ≠ (ring (4 * (2 * m + 2) + 3 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 7 (by rw [show 4 * (2 * m + 2) + 3 + 1 + 1 = 7 + (8 * m + 5) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 7 _))] at h
  exact Nat.noConfusion h

theorem not_dvq (hpr : ∀ d, Dv d (4 * (2 * m + 2) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 2) + 3 + 1 + 1) (h : Dv (4 * 1 + 1 + 1 + 1) (4 * (2 * m + 2) + 3 + 1 + 1)) : False :=
  match hpr 7 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => lt_wrap 7 (8 * m + 5) ((show 8 * m + 5 + 1 + 7 = 4 * (2 * m + 2) + 3 + 1 + 1 by lib_nat).trans e.symm)

/-- **χ(−1) = -1** for p ≡ 5 (mod 8). -/
theorem chi_m1 (hpr : ∀ d, Dv d (4 * (2 * m + 2) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 2) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 2) + 3 + 1)) (hi : (ring (4 * (2 * m + 2) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) : chi4 (2 * m + 2) ι ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) = (-1, 0) := by
  have hm : ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) ≠ (ring (4 * (2 * m + 2) + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' _ (by
    show (ring (4 * (2 * m + 2) + 3 + 1)).one = (ring (4 * (2 * m + 2) + 3 + 1)).zero
    calc (ring (4 * (2 * m + 2) + 3 + 1)).one = (ring (4 * (2 * m + 2) + 3 + 1)).neg ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) := by lib_ring
      _ = (ring (4 * (2 * m + 2) + 3 + 1)).zero := by rw [e]; lib_ring)
  have h : pw (ring (4 * (2 * m + 2) + 3 + 1)) ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) ((2 * m + 2) + 1) = pw (ring (4 * (2 * m + 2) + 3 + 1)) ι 2 := by
    rw [show (2 * m + 2) + 1 = 2 * (m + 1) + 1 by lib_nat]
    show (ring (4 * (2 * m + 2) + 3 + 1)).mul (pw (ring (4 * (2 * m + 2) + 3 + 1)) ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) (2 * (m + 1))) ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) = (ring (4 * (2 * m + 2) + 3 + 1)).mul ((ring (4 * (2 * m + 2) + 3 + 1)).mul (ring (4 * (2 * m + 2) + 3 + 1)).one ι) ι
    rw [pw_mul_split, show pw (ring (4 * (2 * m + 2) + 3 + 1)) ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) 2 = (ring (4 * (2 * m + 2) + 3 + 1)).one by show (ring (4 * (2 * m + 2) + 3 + 1)).mul ((ring (4 * (2 * m + 2) + 3 + 1)).mul (ring (4 * (2 * m + 2) + 3 + 1)).one _) _ = _; lib_ring, ZMod.pw_one_r]
    rw [show (ring (4 * (2 * m + 2) + 3 + 1)).mul ((ring (4 * (2 * m + 2) + 3 + 1)).mul (ring (4 * (2 * m + 2) + 3 + 1)).one ι) ι = (ring (4 * (2 * m + 2) + 3 + 1)).mul ι ι by lib_ring, hi]; lib_ring
  exact val4 (2 * m + 2) hpr ι hi _ hm 2 (Nat.lt_succ_of_le (Nat.le_succ 2)) h

abbrev F := ring (4 * 1 + 1 + 1)
abbrev C := Cyclo4.cyc4 F

def sC : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) := (castZ F (-1), castZ F (-0))
def nrm (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := F.add (F.mul X X) (F.mul Y Y)
def lhs (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) :=
  C.mul (pw C (C.mul (C.mul (X, Y) (X, Y)) C.one) 2) (pw C (nrm X Y, F.zero) 1)
def rhs (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := F.add (F.mul X X) (F.neg (F.mul Y Y))

theorem md_red (a r k : Nat) (h : a = r + k * (4 * 1 + 1 + 1 + 1)) (hr : r < 4 * 1 + 1 + 1 + 1) : md (4 * 1 + 1 + 1) a = r := by
  rw [h, md_add_mul]; exact md_small _ r hr

def fm (a : Nat) : Zp (4 * 1 + 1 + 1) := ⟨a % 7, Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
def fadd (x y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fm (x.val + y.val)
def fmul (x y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fm (x.val * y.val)
def fneg (x : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fm ((4 * 1 + 1 + 1) * x.val)

theorem add_nat : ∀ x y : Nat, x < 7 → y < 7 → md (4 * 1 + 1 + 1) (x + y) = (x + y) % 7
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 0, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 0, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 0, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 0, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 0, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 1, 0, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 1, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 1, 2, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 1, 3, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 1, 4, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 1, 5, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 1, 6, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 2, 0, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 2, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 2, 3, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 2, 4, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, 5, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 2, 6, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 3, 0, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 3, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 3, 2, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 3, 4, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 3, 5, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 3, 6, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 4, 0, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 4, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 4, 3, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 4, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 4, 5, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 4, 6, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 0, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 5, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 5, 2, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 5, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 5, 4, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 5, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 6, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 6, 0, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 6, 1, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 6, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 6, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 6, 5, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 6, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))
  | _, k + 7, _, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem mul_nat : ∀ x y : Nat, x < 7 → y < 7 → md (4 * 1 + 1 + 1) (x * y) = (x * y) % 7
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 1, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 2, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 3, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 4, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 5, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 0, 6, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 1, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 1, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 1, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 1, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 1, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 1, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 1, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 2, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 2, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 2, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 2, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 3, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 3, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 3, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 3, 4, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, 5, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 3, 6, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 4, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 4, 3, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 4, 4, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 4, 5, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 4, 6, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 5, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 5, 2, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, 3, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | 5, 4, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 5, 5, _, _ => md_red _ 4 3 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 5, 6, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 6, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 6, 2, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 6, 3, _, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 6, 4, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 6, 5, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, 6, _, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))
  | _, k + 7, _, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem neg_nat : ∀ x : Nat, x < 7 → md (4 * 1 + 1 + 1) ((4 * 1 + 1 + 1) * x) = ((4 * 1 + 1 + 1) * x) % 7
  | 0, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 6 : 0 < 4 * 1 + 1 + 1 + 1)
  | 1, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 0 : 6 < 4 * 1 + 1 + 1 + 1)
  | 2, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 1 : 5 < 4 * 1 + 1 + 1 + 1)
  | 3, _ => md_red _ 4 2 rfl (Nat.le_add_right 5 2 : 4 < 4 * 1 + 1 + 1 + 1)
  | 4, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 3 : 3 < 4 * 1 + 1 + 1 + 1)
  | 5, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 4 : 2 < 4 * 1 + 1 + 1 + 1)
  | 6, _ => md_red _ 1 5 rfl (Nat.le_add_right 2 5 : 1 < 4 * 1 + 1 + 1 + 1)
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem addE (x y : Zp (4 * 1 + 1 + 1)) : F.add x y = fadd x y := Subtype.ext (add_nat x.val y.val x.2 y.2)
theorem mulE (x y : Zp (4 * 1 + 1 + 1)) : F.mul x y = fmul x y := Subtype.ext (mul_nat x.val y.val x.2 y.2)
theorem negE (x : Zp (4 * 1 + 1 + 1)) : F.neg x = fneg x := Subtype.ext (by show md (4 * 1 + 1 + 1) ((4 * 1 + 1 + 1) * x.val) = ((4 * 1 + 1 + 1) * x.val) % 7; exact neg_nat x.val x.2)

def c4f (u v : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) :=
  (fadd (fmul u.1 v.1) (fneg (fmul u.2 v.2)), fadd (fmul u.1 v.2) (fmul u.2 v.1))
theorem c4E (u v : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : C.mul u v = c4f u v := by
  show Cyclo4.c4mul F u v = _
  unfold Cyclo4.c4mul
  rw [mulE, mulE, mulE, mulE, negE, addE, addE]; rfl
def pwf (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : Nat → Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)
  | 0 => C.one
  | k + 1 => c4f (pwf z k) z
theorem pwE (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : ∀ k, pw C z k = pwf z k
  | 0 => rfl
  | k + 1 => by show C.mul (pw C z k) z = _; rw [c4E, pwE z k]; rfl

def nrmf (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fadd (fmul X X) (fmul Y Y)
def lhsf (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1) := c4f (pwf (c4f (c4f (X, Y) (X, Y)) C.one) 2) (pwf (nrmf X Y, F.zero) 1)
def rhsf (X Y : Zp (4 * 1 + 1 + 1)) : Zp (4 * 1 + 1 + 1) := fadd (fmul X X) (fneg (fmul Y Y))

theorem nrmE (X Y : Zp (4 * 1 + 1 + 1)) : nrm X Y = nrmf X Y := by
  unfold nrm nrmf; rw [mulE, mulE, addE]
theorem lhsE (X Y : Zp (4 * 1 + 1 + 1)) : lhs X Y = lhsf X Y := by
  unfold lhs lhsf; rw [nrmE, c4E, c4E, pwE, pwE, c4E]
theorem rhsE (X Y : Zp (4 * 1 + 1 + 1)) : rhs X Y = rhsf X Y := by
  unfold rhs rhsf
  repeat (first | rw [mulE] | rw [addE] | rw [negE])
  try rfl

def code (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) : Nat := z.1.val + 7 * z.2.val
def chk (X Y : Zp (4 * 1 + 1 + 1)) : Bool := (!(Nat.beq (code (lhsf X Y)) 6) || Nat.beq (rhsf X Y).val 0) &&
  (!(Nat.beq (rhsf X Y).val 0) || Nat.beq (nrmf X Y).val 0 || Nat.beq (code (lhsf X Y)) 6)

theorem row0 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 0) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row1 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 1) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row2 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 2) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row3 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 3) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row4 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 4) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row5 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 5) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem row6 : ∀ y : Nat, y < 7 → chk (mk (4 * 1 + 1 + 1) 6) (mk (4 * 1 + 1 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | k + 7, h => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem chk_nat : ∀ x y : Nat, x < 7 → y < 7 → chk (mk (4 * 1 + 1 + 1) x) (mk (4 * 1 + 1 + 1) y) = true
  | 0, y, _, h => row0 y h
  | 1, y, _, h => row1 y h
  | 2, y, _, h => row2 y h
  | 3, y, _, h => row3 y h
  | 4, y, _, h => row4 y h
  | 5, y, _, h => row5 y h
  | 6, y, _, h => row6 y h
  | k + 7, _, h, _ => absurd h (fun e => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) e))

theorem chk_all (X Y : Zp (4 * 1 + 1 + 1)) : chk X Y = true := by
  rw [show X = mk (4 * 1 + 1 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (4 * 1 + 1 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

theorem ne_of_beq (a b : Nat) (h : Nat.beq a b = false) (e : a = b) : False := by
  rw [e, beq_self] at h; exact Bool.noConfusion h

theorem code_eq (z : Zp (4 * 1 + 1 + 1) × Zp (4 * 1 + 1 + 1)) (h : code z = 6) : z = sC := by
  match z, h with
  | (u, v), h =>
    have e' : u.val + 7 * v.val = 6 := h
    have hv : v.val = 0 := by
      cases hv' : v.val with
      | zero => rfl
      | succ t =>
        rw [hv'] at e'
        exact (lt_wrap 7 (u.val + 7 * t + 0) ((show u.val + 7 * t + 0 + 1 + 7 = u.val + 7 * (t + 1) + 1 by lib_nat).trans
          (by rw [show u.val + 7 * (t + 1) + 1 = (u.val + 7 * (t + 1)) + 1 from rfl, e']))).elim
    have hu : u.val = 6 := by rw [hv] at e'; exact e'
    exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem table (X Y : Zp (4 * 1 + 1 + 1)) (h : lhs X Y = sC) : rhs X Y = F.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [← lhsE, h] at hc
  rw [rhsE]
  exact (Subtype.ext (beq_eq _ _ hc) : rhsf X Y = ⟨0, Nat.zero_lt_succ _⟩)

theorem table' (X Y : Zp (4 * 1 + 1 + 1)) (h : rhs X Y = F.zero) (hn : nrm X Y ≠ F.zero) : lhs X Y = sC := by
  have hc := band_r _ _ (chk_all X Y)
  rw [← rhsE, h] at hc
  rw [lhsE]
  rw [nrmE] at hn
  cases hb : Nat.beq (nrmf X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrmf X Y = F.zero) hn
  | false =>
    rw [hb] at hc
    exact code_eq _ (beq_eq _ _ hc)

theorem cast_norm (x y : Int) : castZ F (x * x + y * y) = nrm (castZ F x) (castZ F y) := by
  rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F (x * x - y * y) = rhs (castZ F x) (castZ F y) := by
  rw [show x * x - y * y = x * x + -(y * y) from rfl]
  repeat (first | rw [HasseDavenport.castZ_mul] | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg])
  rfl

theorem hP (hpr : ∀ d, Dv d (4 * (2 * m + 2) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 2) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 2) + 3 + 1)) (hi : (ring (4 * (2 * m + 2) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) :
    ph4 F (Ppq (2 * m + 2)) = (nrm (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1) (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2), F.zero) := by
  rw [show Ppq (2 * m + 2) = (Int.ofNat (4 * (2 * m + 2) + 3 + 1 + 1), 0) from sum_oneq (2 * m + 2) _, ← quart_norm (2 * m + 2) hpr ι hi]; unfold ph4; rw [cast_norm]; rfl

theorem chi4_cases (ι x : Zp (4 * (2 * m + 2) + 3 + 1)) : chi4 (2 * m + 2) ι x = (0, 0) ∨ chi4 (2 * m + 2) ι x = (1, 0) ∨ chi4 (2 * m + 2) ι x = (0, 1) ∨ chi4 (2 * m + 2) ι x = (-1, 0) ∨ chi4 (2 * m + 2) ι x = (0, -1) := by
  unfold chi4
  by_cases h1 : x = (ring (4 * (2 * m + 2) + 3 + 1)).zero
  · exact Or.inl (ite_t _ _ h1)
  · rw [ite_f _ _ h1]
    by_cases h2 : pw (ring (4 * (2 * m + 2) + 3 + 1)) x ((2 * m + 2) + 1) = (ring (4 * (2 * m + 2) + 3 + 1)).one
    · exact Or.inr (Or.inl (ite_t _ _ h2))
    · rw [ite_f _ _ h2]
      by_cases h3 : pw (ring (4 * (2 * m + 2) + 3 + 1)) x ((2 * m + 2) + 1) = ι
      · exact Or.inr (Or.inr (Or.inl (ite_t _ _ h3)))
      · rw [ite_f _ _ h3]
        by_cases h4 : pw (ring (4 * (2 * m + 2) + 3 + 1)) x ((2 * m + 2) + 1) = (ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one
        · exact Or.inr (Or.inr (Or.inr (Or.inl (ite_t _ _ h4))))
        · exact Or.inr (Or.inr (Or.inr (Or.inr (ite_f _ _ h4))))

theorem recip (hpr : ∀ d, Dv d (4 * (2 * m + 2) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 2) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 2) + 3 + 1)) (hi : (ring (4 * (2 * m + 2) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) :
    C.mul (ph4 F (chi4 (2 * m + 2) ι (nsm (ring (4 * (2 * m + 2) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 2) + 3 + 1)).one))) sC = lhs (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1) (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2) := by
  have hc := quart_recip_inert (2 * m + 2) 1 hpr q_prime ι hi (q_ne m)
  rw [show chib (2 * m + 2) ι ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one) = gconj (chi4 (2 * m + 2) ι ((ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one)) from chi_ivq (2 * m + 2) hpr ι hi _, chi_m1 m hpr ι hi, hP m hpr ι hi, ph4_one] at hc
  exact hc

/-- **7 a quartic residue mod p ⇒ 7 | a * a - b * b**. -/
theorem quart_seven_5 (hpr : ∀ d, Dv d (4 * (2 * m + 2) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 2) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 2) + 3 + 1)) (hi : (ring (4 * (2 * m + 2) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one)
    (hq : chi4 (2 * m + 2) ι (nsm (ring (4 * (2 * m + 2) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 2) + 3 + 1)).one) = (1, 0)) : IDv (Int.ofNat 7) ((Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1 * (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1 - (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2 * (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2) := by
  have hc := recip m hpr ι hi
  rw [hq] at hc
  have hl : lhs (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1) (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2) = sC := hc.symm.trans (by rw [c4E]; rfl)
  exact CubicTwo.cast_zero_gen (4 * 1 + 1 + 1) _ ((cast_rhs _ _).trans (table _ _ hl))

/-- **7 | a * a - b * b ⇒ 7 is a quartic residue mod p.** -/
theorem quart_seven_5_conv (hpr : ∀ d, Dv d (4 * (2 * m + 2) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (2 * m + 2) + 3 + 1 + 1) (ι : Zp (4 * (2 * m + 2) + 3 + 1)) (hi : (ring (4 * (2 * m + 2) + 3 + 1)).mul ι ι = (ring (4 * (2 * m + 2) + 3 + 1)).neg (ring (4 * (2 * m + 2) + 3 + 1)).one)
    (hd : IDv (Int.ofNat 7) ((Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1 * (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1 - (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2 * (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2)) : chi4 (2 * m + 2) ι (nsm (ring (4 * (2 * m + 2) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 2) + 3 + 1)).one) = (1, 0) := by
  have hc := recip m hpr ι hi
  have hr : rhs (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1) (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2) = F.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F (Int.ofNat 7) = F.zero := CubeAp.castP 5
      rw [h0, zero_mul']
  have hn : nrm (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).1) (castZ F (Jq (2 * m + 2) (chi4 (2 * m + 2) ι)).2) ≠ F.zero := fun e => by
    have e1 : castZ F (Int.ofNat (4 * (2 * m + 2) + 3 + 1 + 1)) = F.zero := by rw [← quart_norm (2 * m + 2) hpr ι hi, cast_norm]; exact e
    have e2 : mk (4 * 1 + 1 + 1) (4 * (2 * m + 2) + 3 + 1 + 1) = F.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hdq : ∀ P' : Nat, mk (4 * 1 + 1 + 1) P' = F.zero → Dv (4 * 1 + 1 + 1 + 1) P' := fun P' h => md_zero_dv (4 * 1 + 1 + 1) P' (congrArg Subtype.val h)
    exact not_dvq m hpr (hdq _ e2)
  have h1 := hc.trans (table' _ _ hr hn)
  rcases chi4_cases m ι (nsm (ring (4 * (2 * m + 2) + 3 + 1)) (4 * 1 + 1 + 1 + 1) (ring (4 * (2 * m + 2) + 3 + 1)).one) with e | e | e | e | e
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)
  · rw [e, c4E] at h1; exact absurd (congrArg code h1) (ne_of_beq _ _ rfl)

end QuartSeven5
#print axioms QuartSeven5.lt_wrap
#print axioms QuartSeven5.ndvN
#print axioms QuartSeven5.q_prime
#print axioms QuartSeven5.q_ne
#print axioms QuartSeven5.not_dvq
#print axioms QuartSeven5.chi_m1
#print axioms QuartSeven5.md_red
#print axioms QuartSeven5.add_nat
#print axioms QuartSeven5.mul_nat
#print axioms QuartSeven5.neg_nat
#print axioms QuartSeven5.addE
#print axioms QuartSeven5.mulE
#print axioms QuartSeven5.negE
#print axioms QuartSeven5.c4E
#print axioms QuartSeven5.pwE
#print axioms QuartSeven5.nrmE
#print axioms QuartSeven5.lhsE
#print axioms QuartSeven5.rhsE
#print axioms QuartSeven5.row0
#print axioms QuartSeven5.row1
#print axioms QuartSeven5.row2
#print axioms QuartSeven5.row3
#print axioms QuartSeven5.row4
#print axioms QuartSeven5.row5
#print axioms QuartSeven5.row6
#print axioms QuartSeven5.chk_nat
#print axioms QuartSeven5.chk_all
#print axioms QuartSeven5.ne_of_beq
#print axioms QuartSeven5.code_eq
#print axioms QuartSeven5.table
#print axioms QuartSeven5.table'
#print axioms QuartSeven5.cast_norm
#print axioms QuartSeven5.cast_rhs
#print axioms QuartSeven5.hP
#print axioms QuartSeven5.chi4_cases
#print axioms QuartSeven5.recip
#print axioms QuartSeven5.quart_seven_5
#print axioms QuartSeven5.quart_seven_5_conv

#print axioms: 144 theorems, no axioms.

78.61 Residue criteria for every prime

The reduction of \(\chi (q)\) to \(\mathbb F_q[\omega ]\) or \(\mathbb F_q[i]\) is injective on the character values, so reciprocity becomes a criterion.

\[ q \text { a cube mod } p \Leftrightarrow J^{k+1}p^k=1 \text { in } \mathbb F_q[\omega ]\ (q=3k+2),\qquad q \text { a quartic residue} \Leftrightarrow J^{2k+2}p^{k+1}=1 \text { in } \mathbb F_q[i]\ (q=4k+5). \]

lang/ResidueCriterion.lean

/-
  lang/ResidueCriterion — T43, cycle 228: the cubic and quartic residue criteria for every prime q, in both directions.

  CubicRecip, CubicRecipSplit and QuartRecip compute the image of χ(q) (or χ̄(q), χ(q)χ̄(−1)) in 𝔽_q[ω], 𝔽_q[i]. Since the reduction
  map is injective on the values {0, 1, ω, ω²} resp. {0, ±1, ±i} (1 ≠ 0 in 𝔽_q, and −1 ≠ 1 for q odd), these become criteria:
    cubic_inert_iff    q = 3k + 2:  q is a cube mod p        ⇔  J^{k+1} p^k = 1           in 𝔽_q[ω]
    cubic_split_iff    q = 3m + 4:  q is a cube mod p        ⇔  (J p)^{m+1} = 1           in 𝔽_q[ω]
    quart_split_iff    q = 4k + 5:  q is a quartic residue   ⇔  J^{2k+2} p^{k+1} = 1      in 𝔽_q[i]
    quart_inert_iff    q = 4k + 3:  q is a quartic residue   ⇔  J^{2k+2} p^k = χ(−1)      in 𝔽_q[i]
  (p = 6n + 7 resp. 4n + 5, q ≠ p.) The tables of CubicFive … QuartSeven5 are these criteria evaluated over 𝔽_q.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.QuartFive

namespace ResidueCriterion

open FTAChain ZMod DetChain Frobenius CubicChar JacobiSum JacobiNorm CubicRecip CubicRecipSplit QuartChar QuartJacobi QuartRecip
open LibRing (castZ castN)

section Gen
variable {R : Type} (T : CRing R)

theorem castZ_zero : castZ T 0 = T.zero := rfl

theorem castZ_one : castZ T 1 = T.one := add_zero' T T.one

theorem castZ_m1 : castZ T (-1) = T.neg T.one := by
  rw [HasseDavenport.castZ_neg, castZ_one]

theorem neg_one_ne (hT : T.one ≠ T.zero) : T.neg T.one ≠ T.zero := fun e => hT (by
  calc T.one = T.neg (T.neg T.one) := by lib_ring
    _ = T.zero := by rw [e]; lib_ring)

/-- the three non-trivial values of a cubic character reduce to elements ≠ 1. -/
theorem ph_00 (hT : T.one ≠ T.zero) : ph T (0, 0) ≠ (Cyclo3.cyc3 T).one := fun e => hT (congrArg Prod.fst e).symm
theorem ph_01 (hT : T.one ≠ T.zero) : ph T (0, 1) ≠ (Cyclo3.cyc3 T).one := fun e => hT ((castZ_one T).symm.trans (congrArg Prod.snd e))
theorem ph_mm (hT : T.one ≠ T.zero) : ph T (-1, -1) ≠ (Cyclo3.cyc3 T).one := fun e => neg_one_ne T hT ((castZ_m1 T).symm.trans (congrArg Prod.snd e))

theorem ph4_00 (hT : T.one ≠ T.zero) : ph4 T (0, 0) ≠ (Cyclo4.cyc4 T).one := fun e => hT (congrArg Prod.fst e).symm
theorem ph4_0m (hT : T.one ≠ T.zero) : ph4 T (0, -1) ≠ (Cyclo4.cyc4 T).one := fun e => neg_one_ne T hT ((castZ_m1 T).symm.trans (congrArg Prod.snd e))
theorem ph4_0p (hT : T.one ≠ T.zero) : ph4 T (0, 1) ≠ (Cyclo4.cyc4 T).one := fun e => hT ((castZ_one T).symm.trans (congrArg Prod.snd e))
theorem ph4_m0 (h2 : T.add T.one T.one ≠ T.zero) : ph4 T (-1, 0) ≠ (Cyclo4.cyc4 T).one := fun e => h2 (by
  have e1 : T.neg T.one = T.one := (castZ_m1 T).symm.trans (congrArg Prod.fst e)
  calc T.add T.one T.one = T.add T.one (T.neg T.one) := by rw [e1]
    _ = T.zero := T.add_neg _)

end Gen

theorem two_ne (J : Nat) (h : 1 ≤ J) : (ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one ≠ (ring (J + 1)).zero := fun e => by
  have hv := congrArg Subtype.val e
  have h1 : md (J + 1) 1 = 1 := md_small _ 1 (Nat.lt_succ_of_le (Nat.le_succ_of_le h))
  have v : ((ring (J + 1)).add (ring (J + 1)).one (ring (J + 1)).one).val = 2 := by
    show md (J + 1) (md (J + 1) 1 + md (J + 1) 1) = 2
    rw [h1]; exact md_small _ 2 (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.lt_of_lt_of_le (Nat.zero_lt_succ 0) h)))
  rw [v] at hv
  exact Nat.noConfusion hv

variable (n k : Nat)

/-- **Cubic residue criterion at an inert prime q = 3k + 2**: q is a cube mod p ⇔ J^{k+1} p^k = 1 in 𝔽_q[ω]. -/
theorem cubic_inert_iff (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (hq : ∀ d, Dv d (3 * k + 1 + 1) → d = 1 ∨ d = 3 * k + 1 + 1)
    (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one)
    (hQ : nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero) :
    chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one) = (1, 0) ↔
    (Cyclo3.cyc3 (ring (3 * k + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (J n w)) (k + 1)) (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (Pe n)) k) = (Cyclo3.cyc3 (ring (3 * k + 1))).one := by
  have hc := cubic_recip_inert n k hpr hq w hw1 hw3 hQ
  have hT := PowerSum.one_ne_zero' (3 * k)
  constructor
  · intro h; rw [h] at hc; exact hc.symm.trans (ph_one _)
  · intro h
    have h1 := hc.trans h
    rcases CubicFive.chi_cases n w (nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one) with e | e | e | e
    · rw [e] at h1; exact absurd h1 (ph_00 _ hT)
    · exact e
    · rw [e] at h1; exact absurd h1 (ph_01 _ hT)
    · rw [e] at h1; exact absurd h1 (ph_mm _ hT)

/-- **Cubic residue criterion at a split prime q = 3k + 4**: q is a cube mod p ⇔ (J p)^{k+1} = 1 in 𝔽_q[ω]. -/
theorem cubic_split_iff (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (hq : ∀ d, Dv d (3 * k + 2 + 1 + 1) → d = 1 ∨ d = 3 * k + 2 + 1 + 1)
    (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one)
    (hQ : nsm (ring (6 * n + 5 + 1)) (3 * k + 2 + 1 + 1) (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero) :
    chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * k + 2 + 1 + 1) (ring (6 * n + 5 + 1)).one) = (1, 0) ↔
    (Cyclo3.cyc3 (ring (3 * k + 2 + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * k + 2 + 1))) (ph (ring (3 * k + 2 + 1)) (J n w)) (k + 1)) (pw (Cyclo3.cyc3 (ring (3 * k + 2 + 1))) (ph (ring (3 * k + 2 + 1)) (Pe n)) (k + 1)) = (Cyclo3.cyc3 (ring (3 * k + 2 + 1))).one := by
  have hc := cubic_recip_split n k hpr hq w hw1 hw3 hQ
  have hT := PowerSum.one_ne_zero' (3 * k + 2)
  unfold cb at hc
  constructor
  · intro h; rw [h] at hc; exact hc.symm.trans (ph_one _)
  · intro h
    have h1 := hc.trans h
    rcases CubicFive.chi_cases n w (nsm (ring (6 * n + 5 + 1)) (3 * k + 2 + 1 + 1) (ring (6 * n + 5 + 1)).one) with e | e | e | e
    · rw [e] at h1; exact absurd h1 (ph_00 _ hT)
    · exact e
    · rw [e] at h1; exact absurd h1 (ph_mm _ hT)
    · rw [e] at h1; exact absurd h1 (ph_01 _ hT)

theorem chi4_cases (ι x : Zp (4 * n + 3 + 1)) : chi4 n ι x = (0, 0) ∨ chi4 n ι x = (1, 0) ∨ chi4 n ι x = (0, 1) ∨ chi4 n ι x = (-1, 0) ∨ chi4 n ι x = (0, -1) := by
  unfold chi4
  by_cases h1 : x = (ring (4 * n + 3 + 1)).zero
  · exact Or.inl (ite_t _ _ h1)
  · rw [ite_f _ _ h1]
    by_cases h2 : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).one
    · exact Or.inr (Or.inl (ite_t _ _ h2))
    · rw [ite_f _ _ h2]
      by_cases h3 : pw (ring (4 * n + 3 + 1)) x (n + 1) = ι
      · exact Or.inr (Or.inr (Or.inl (ite_t _ _ h3)))
      · rw [ite_f _ _ h3]
        by_cases h4 : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one
        · exact Or.inr (Or.inr (Or.inr (Or.inl (ite_t _ _ h4))))
        · exact Or.inr (Or.inr (Or.inr (Or.inr (ite_f _ _ h4))))

/-- the reduction of χ(x) is 1 only if χ(x) = 1 (q odd). -/
theorem ph4_eq_one (J' : Nat) (hJ : 1 ≤ J') (ι x : Zp (4 * n + 3 + 1)) (h : ph4 (ring (J' + 1)) (chi4 n ι x) = (Cyclo4.cyc4 (ring (J' + 1))).one) : chi4 n ι x = (1, 0) := by
  have hT := PowerSum.one_ne_zero' J'
  rcases chi4_cases n ι x with e | e | e | e | e
  · rw [e] at h; exact absurd h (ph4_00 _ hT)
  · exact e
  · rw [e] at h; exact absurd h (ph4_0p _ hT)
  · rw [e] at h; exact absurd h (ph4_m0 _ (two_ne J' hJ))
  · rw [e] at h; exact absurd h (ph4_0m _ hT)

/-- the same for the conjugate χ̄(x). -/
theorem ph4c_eq_one (J' : Nat) (hJ : 1 ≤ J') (ι x : Zp (4 * n + 3 + 1)) (h : ph4 (ring (J' + 1)) (gconj (chi4 n ι x)) = (Cyclo4.cyc4 (ring (J' + 1))).one) : chi4 n ι x = (1, 0) := by
  have hT := PowerSum.one_ne_zero' J'
  rcases chi4_cases n ι x with e | e | e | e | e
  · rw [e] at h; exact absurd h (ph4_00 _ hT)
  · exact e
  · rw [e] at h; exact absurd h (ph4_0m _ hT)
  · rw [e] at h; exact absurd h (ph4_m0 _ (two_ne J' hJ))
  · rw [e] at h; exact absurd h (ph4_0p _ hT)

/-- **Quartic residue criterion at a split prime q = 4k + 5**: q is a quartic residue mod p ⇔ J^{2k+2} p^{k+1} = 1 in 𝔽_q[i]. -/
theorem quart_split_iff (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (hq : ∀ d, Dv d (4 * k + 3 + 1 + 1) → d = 1 ∨ d = 4 * k + 3 + 1 + 1)
    (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)
    (hQ : nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero) :
    chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one) = (1, 0) ↔
    (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (ph4 (ring (4 * k + 3 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (k + 1)) (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) (ph4 (ring (4 * k + 3 + 1  -- (+69 chars; full line in the Zenodo source)
  have hc := quart_recip_split n k hpr hq ι hi hQ
  rw [show chib n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one) = gconj (chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one)) from chi_ivq n hpr ι hi _] at hc
  constructor
  · intro h; rw [h] at hc; exact hc.symm.trans (ph4_one _)
  · intro h
    exact ph4c_eq_one n (4 * k + 3) (Nat.le_add_left 1 _) ι _ (hc.trans h)

/-- **Quartic residue criterion at an inert prime q = 4k + 3**: q is a quartic residue mod p ⇔ J^{2k+2} p^k = χ(−1) in 𝔽_q[i]. -/
theorem quart_inert_iff (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (hq : ∀ d, Dv d (4 * k + 1 + 1 + 1) → d = 1 ∨ d = 4 * k + 1 + 1 + 1)
    (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)
    (hQ : nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero) :
    chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one) = (1, 0) ↔
    (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι)))) (ph4 (ring (4 * k + 1 + 1)) (Cyclo4.cyc4 LibInt.intR).one)) (k + 1)) (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1  -- (+14 chars; full line in the Zenodo source)
      = ph4 (ring (4 * k + 1 + 1)) (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) := by
  have hc := quart_recip_inert n k hpr hq ι hi hQ
  -- s = χ̄(−1) is a unit: s² = 1
  have hs : (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (ph4 (ring (4 * k + 1 + 1)) (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).one := by
    rw [← ph4_mul, ← chib_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring,
      chib_one n hpr ι hi, ph4_one]
  generalize ph4 (ring (4 * k + 1 + 1)) (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = s at hc hs ⊢
  constructor
  · intro h
    rw [h, show ph4 (ring (4 * k + 1 + 1)) ((1 : Int), (0 : Int)) = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).one from ph4_one _] at hc
    rw [← hc]; exact (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).one_mul s
  · intro h
    have h1 := hc.trans h
    apply ph4_eq_one n (4 * k + 1) (Nat.le_add_left 1 _) ι
    calc _ = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one))) s) s := by
            rw [(Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul_assoc, hs]; lib_ring
      _ = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).one := by rw [h1, hs]

end ResidueCriterion

#print axioms ResidueCriterion.castZ_zero
#print axioms ResidueCriterion.castZ_one
#print axioms ResidueCriterion.castZ_m1
#print axioms ResidueCriterion.neg_one_ne
#print axioms ResidueCriterion.ph_00
#print axioms ResidueCriterion.ph_01
#print axioms ResidueCriterion.ph_mm
#print axioms ResidueCriterion.ph4_00
#print axioms ResidueCriterion.ph4_0m
#print axioms ResidueCriterion.ph4_0p
#print axioms ResidueCriterion.ph4_m0
#print axioms ResidueCriterion.two_ne
#print axioms ResidueCriterion.cubic_inert_iff
#print axioms ResidueCriterion.cubic_split_iff
#print axioms ResidueCriterion.chi4_cases
#print axioms ResidueCriterion.ph4_eq_one
#print axioms ResidueCriterion.ph4c_eq_one
#print axioms ResidueCriterion.quart_split_iff
#print axioms ResidueCriterion.quart_inert_iff

#print axioms: 19 theorems, no axioms.

78.62 Quartic character of \(17\)

Quartic reciprocity at the split prime \(17\) and a \(289\)-case table over \(\mathbb F_{17}\).

\[ 17 \text { a quartic residue mod } p \;\Longleftrightarrow \; 17\mid ab(a^2-b^2). \]

lang/QuartSeventeen.lean

/-
  lang/QuartSeventeen — T43: the quartic character of 17 (generated by work/gen_quart_table.py).

  p = 4(n + 4) + 5 (so p > 17), χ the quartic character of 𝔽_p, J = J(χ, χ) = a + bi, N(J) = a² + b² = p. QuartRecip at the split
  prime q = 17 (k = 3) gives χ̄(q) = (J²)^4 p^4 in 𝔽_17[i], and a 289-case table over 𝔽_17 (through the builtin Nat.mod, proved equal
  to ring q pointwise) shows that this is 1 iff ab(a² − b²) ≡ 0 (when N(J) ≢ 0):
      17 is a quartic residue mod p  ⇔  17 | ab(a² − b²).
    table, table'      the table (both directions)
    quart_seventeen, quart_seventeen_conv
  Lean 4 core only, proofs by hand (lib_nat; the table by rfl on closed terms), no axioms.
-/
import lang.QuartRecip
import lang.CubicThirteen

namespace QuartSeventeen

open FTAChain ZMod DetChain Frobenius QuartChar QuartJacobi QuartRecip LibGcd CubicFive
open LibRing (castZ castN)

theorem lt_wrap : ∀ q a : Nat, a + 1 + q = q → False
  | 0, _, h => Nat.noConfusion h
  | q + 1, a, h => lt_wrap q a (Nat.succ.inj h)

theorem ndvN (N m' q : Nat) (h : N = q * (m' + 1)) (hr : rem (m' + 1) N = 0 → False) : False :=
  hr (by rw [h, show q * (m' + 1) = q * (m' + 1) + 0 from rfl, rem_add_mul]; rfl)

theorem q_prime : ∀ d, Dv d (4 * 3 + 3 + 1 + 1) → d = 1 ∨ d = 4 * 3 + 3 + 1 + 1 := fun d hd =>
  match dv_le d 16 hd, hd with
  | ⟨k, hk⟩, ⟨q', hq⟩ => by
    match d with
    | 0 => exact absurd hq (fun e => Nat.noConfusion e)
    | 1 => exact Or.inl rfl
    | 2 => exact (ndvN 17 1 q' hq (fun e => Nat.noConfusion e)).elim
    | 3 => exact (ndvN 17 2 q' hq (fun e => Nat.noConfusion e)).elim
    | 4 => exact (ndvN 17 3 q' hq (fun e => Nat.noConfusion e)).elim
    | 5 => exact (ndvN 17 4 q' hq (fun e => Nat.noConfusion e)).elim
    | 6 => exact (ndvN 17 5 q' hq (fun e => Nat.noConfusion e)).elim
    | 7 => exact (ndvN 17 6 q' hq (fun e => Nat.noConfusion e)).elim
    | 8 => exact (ndvN 17 7 q' hq (fun e => Nat.noConfusion e)).elim
    | 9 => exact (ndvN 17 8 q' hq (fun e => Nat.noConfusion e)).elim
    | 10 => exact (ndvN 17 9 q' hq (fun e => Nat.noConfusion e)).elim
    | 11 => exact (ndvN 17 10 q' hq (fun e => Nat.noConfusion e)).elim
    | 12 => exact (ndvN 17 11 q' hq (fun e => Nat.noConfusion e)).elim
    | 13 => exact (ndvN 17 12 q' hq (fun e => Nat.noConfusion e)).elim
    | 14 => exact (ndvN 17 13 q' hq (fun e => Nat.noConfusion e)).elim
    | 15 => exact (ndvN 17 14 q' hq (fun e => Nat.noConfusion e)).elim
    | 16 => exact (ndvN 17 15 q' hq (fun e => Nat.noConfusion e)).elim
    | 17 => exact Or.inr rfl
    | d + 18 => exact (lt_wrap 17 (d + k) ((show d + k + 1 + 17 = d + 18 + k by lib_nat).trans hk)).elim

variable (n : Nat)

theorem q_ne : nsm (ring (4 * (n + 4) + 3 + 1)) (4 * 3 + 3 + 1 + 1) (ring (4 * (n + 4) + 3 + 1)).one ≠ (ring (4 * (n + 4) + 3 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ 17 (by rw [show 4 * (n + 4) + 3 + 1 + 1 = 17 + (4 * n + 3) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right 17 _))] at h
  exact Nat.noConfusion h

theorem not_dvq (hpr : ∀ d, Dv d (4 * (n + 4) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 4) + 3 + 1 + 1) (h : Dv (4 * 3 + 3 + 1 + 1) (4 * (n + 4) + 3 + 1 + 1)) : False :=
  match hpr 17 h with
  | Or.inl e => Nat.noConfusion (Nat.succ.inj e)
  | Or.inr e => lt_wrap 17 (4 * n + 3) ((show 4 * n + 3 + 1 + 17 = 4 * (n + 4) + 3 + 1 + 1 by lib_nat).trans e.symm)

abbrev F := ring (4 * 3 + 3 + 1)
abbrev C := Cyclo4.cyc4 F

def nrm (X Y : Zp (4 * 3 + 3 + 1)) : Zp (4 * 3 + 3 + 1) := F.add (F.mul X X) (F.mul Y Y)
def lhs (X Y : Zp (4 * 3 + 3 + 1)) : Zp (4 * 3 + 3 + 1) × Zp (4 * 3 + 3 + 1) :=
  C.mul (pw C (C.mul (C.mul (X, Y) (X, Y)) C.one) 4) (pw C (nrm X Y, F.zero) 4)
def rhs (X Y : Zp (4 * 3 + 3 + 1)) : Zp (4 * 3 + 3 + 1) := F.mul (F.mul X Y) (F.add (F.mul X X) (F.neg (F.mul Y Y)))

theorem md_red (a r k : Nat) (h : a = r + k * (4 * 3 + 3 + 1 + 1)) (hr : r < 4 * 3 + 3 + 1 + 1) : md (4 * 3 + 3 + 1) a = r := by
  rw [h, md_add_mul]; exact md_small _ r hr

def fm (a : Nat) : Zp (4 * 3 + 3 + 1) := ⟨a % 17, Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
def fadd (x y : Zp (4 * 3 + 3 + 1)) : Zp (4 * 3 + 3 + 1) := fm (x.val + y.val)
def fmul (x y : Zp (4 * 3 + 3 + 1)) : Zp (4 * 3 + 3 + 1) := fm (x.val * y.val)
def fneg (x : Zp (4 * 3 + 3 + 1)) : Zp (4 * 3 + 3 + 1) := fm ((4 * 3 + 3 + 1) * x.val)

theorem add_nat : ∀ x y : Nat, x < 17 → y < 17 → md (4 * 3 + 3 + 1) (x + y) = (x + y) % 17
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 0, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 0, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 0, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 0, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 0, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 0, 7, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 0, 8, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 0, 9, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 0, 10, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 0, 11, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 0, 12, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 0, 13, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 0, 14, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 0, 15, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 0, 16, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 1, 0, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 1, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 1, 2, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 1, 3, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 1, 4, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 1, 5, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 1, 6, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 1, 7, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 1, 8, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 1, 9, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 1, 10, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 1, 11, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 1, 12, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 1, 13, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 1, 14, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 1, 15, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 1, 16, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 2, 0, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 2, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 2, 3, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 2, 4, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 2, 5, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 2, 6, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 2, 7, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 2, 8, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 2, 9, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 2, 10, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 2, 11, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 2, 12, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 2, 13, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 2, 14, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 2, 15, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 2, 16, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 3, 0, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 3, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 3, 2, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 3, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 3, 4, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 3, 5, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 3, 6, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 3, 7, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 3, 8, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 3, 9, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 3, 10, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 3, 11, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 3, 12, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 3, 13, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 3, 14, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 3, 15, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 3, 16, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 4, 0, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 4, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 4, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 4, 3, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 4, 4, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 4, 5, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 4, 6, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 4, 7, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 4, 8, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 4, 9, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 4, 10, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 4, 11, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 4, 12, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 4, 13, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 4, 14, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 4, 15, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 4, 16, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 5, 0, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 5, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 5, 2, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 5, 3, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 5, 4, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 5, 5, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 5, 6, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 5, 7, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 5, 8, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 5, 9, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 5, 10, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 5, 11, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 5, 12, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 5, 13, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 5, 14, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 5, 15, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 5, 16, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 6, 0, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 6, 1, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 6, 2, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 6, 3, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 6, 4, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 6, 5, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 6, 6, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 6, 7, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 6, 8, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 6, 9, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 6, 10, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 6, 11, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 6, 12, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 6, 13, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 6, 14, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 6, 15, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 6, 16, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 7, 0, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 7, 1, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 7, 2, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 7, 3, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 7, 4, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 7, 5, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 7, 6, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 7, 7, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 7, 8, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 7, 9, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 7, 10, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 7, 11, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 7, 12, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 7, 13, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 7, 14, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 7, 15, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 7, 16, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 8, 0, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 8, 1, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 8, 2, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 8, 3, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 8, 4, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 8, 5, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 8, 6, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 8, 7, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 8, 8, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 8, 9, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 8, 10, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 8, 11, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 8, 12, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 8, 13, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 8, 14, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 8, 15, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 8, 16, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 9, 0, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 9, 1, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 9, 2, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 9, 3, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 9, 4, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 9, 5, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 9, 6, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 9, 7, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 9, 8, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 9, 9, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 9, 10, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 9, 11, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 9, 12, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 9, 13, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 9, 14, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 9, 15, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 9, 16, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 10, 0, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 10, 1, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 10, 2, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 10, 3, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 10, 4, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 10, 5, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 10, 6, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 10, 7, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 10, 8, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 10, 9, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 10, 10, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 10, 11, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 10, 12, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 10, 13, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 10, 14, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 10, 15, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 10, 16, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 11, 0, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 11, 1, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 11, 2, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 11, 3, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 11, 4, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 11, 5, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 11, 6, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 11, 7, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 11, 8, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 11, 9, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 11, 10, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 11, 11, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 11, 12, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 11, 13, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 11, 14, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 11, 15, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 11, 16, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 12, 0, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 12, 1, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 12, 2, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 12, 3, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 12, 4, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 12, 5, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 12, 6, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 12, 7, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 12, 8, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 12, 9, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 12, 10, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 12, 11, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 12, 12, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 12, 13, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 12, 14, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 12, 15, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 12, 16, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 13, 0, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 13, 1, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 13, 2, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 13, 3, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 13, 4, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 13, 5, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 13, 6, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 13, 7, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 13, 8, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 13, 9, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 13, 10, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 13, 11, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 13, 12, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 13, 13, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 13, 14, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 13, 15, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 13, 16, _, _ => md_red _ 12 1 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 14, 0, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 14, 1, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 14, 2, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 14, 3, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 14, 4, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 14, 5, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 14, 6, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 14, 7, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 14, 8, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 14, 9, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 14, 10, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 14, 11, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 14, 12, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 14, 13, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 14, 14, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 14, 15, _, _ => md_red _ 12 1 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 14, 16, _, _ => md_red _ 13 1 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 15, 0, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 15, 1, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 15, 2, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 15, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 15, 4, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 15, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 15, 6, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 15, 7, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 15, 8, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 15, 9, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 15, 10, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 15, 11, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 15, 12, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 15, 13, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 15, 14, _, _ => md_red _ 12 1 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 15, 15, _, _ => md_red _ 13 1 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 15, 16, _, _ => md_red _ 14 1 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 16, 0, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 16, 1, _, _ => md_red _ 0 1 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 16, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 16, 3, _, _ => md_red _ 2 1 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 16, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 16, 5, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 16, 6, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 16, 7, _, _ => md_red _ 6 1 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 16, 8, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 16, 9, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 16, 10, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 16, 11, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 16, 12, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 16, 13, _, _ => md_red _ 12 1 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 16, 14, _, _ => md_red _ 13 1 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 16, 15, _, _ => md_red _ 14 1 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 16, 16, _, _ => md_red _ 15 1 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | k + 17, _, h, _ => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))
  | _, k + 17, _, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem mul_nat : ∀ x y : Nat, x < 17 → y < 17 → md (4 * 3 + 3 + 1) (x * y) = (x * y) % 17
  | 0, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 1, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 2, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 3, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 4, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 5, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 6, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 7, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 8, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 9, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 10, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 11, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 12, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 13, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 14, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 15, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 0, 16, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 1, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 1, 1, _, _ => md_red _ 1 0 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 1, 2, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 1, 3, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 1, 4, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 1, 5, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 1, 6, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 1, 7, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 1, 8, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 1, 9, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 1, 10, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 1, 11, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 1, 12, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 1, 13, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 1, 14, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 1, 15, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 1, 16, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 2, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 2, 1, _, _ => md_red _ 2 0 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 2, 2, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 2, 3, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 2, 4, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 2, 5, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 2, 6, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 2, 7, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 2, 8, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 2, 9, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 2, 10, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 2, 11, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 2, 12, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 2, 13, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 2, 14, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 2, 15, _, _ => md_red _ 13 1 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 2, 16, _, _ => md_red _ 15 1 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 3, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 3, 1, _, _ => md_red _ 3 0 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 3, 2, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 3, 3, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 3, 4, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 3, 5, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 3, 6, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 3, 7, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 3, 8, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 3, 9, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 3, 10, _, _ => md_red _ 13 1 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 3, 11, _, _ => md_red _ 16 1 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 3, 12, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 3, 13, _, _ => md_red _ 5 2 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 3, 14, _, _ => md_red _ 8 2 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 3, 15, _, _ => md_red _ 11 2 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 3, 16, _, _ => md_red _ 14 2 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 4, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 4, 1, _, _ => md_red _ 4 0 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 4, 2, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 4, 3, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 4, 4, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 4, 5, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 4, 6, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 4, 7, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 4, 8, _, _ => md_red _ 15 1 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 4, 9, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 4, 10, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 4, 11, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 4, 12, _, _ => md_red _ 14 2 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 4, 13, _, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 4, 14, _, _ => md_red _ 5 3 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 4, 15, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 4, 16, _, _ => md_red _ 13 3 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 5, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 5, 1, _, _ => md_red _ 5 0 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 5, 2, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 5, 3, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 5, 4, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 5, 5, _, _ => md_red _ 8 1 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 5, 6, _, _ => md_red _ 13 1 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 5, 7, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 5, 8, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 5, 9, _, _ => md_red _ 11 2 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 5, 10, _, _ => md_red _ 16 2 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 5, 11, _, _ => md_red _ 4 3 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 5, 12, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 5, 13, _, _ => md_red _ 14 3 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 5, 14, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 5, 15, _, _ => md_red _ 7 4 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 5, 16, _, _ => md_red _ 12 4 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 6, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 6, 1, _, _ => md_red _ 6 0 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 6, 2, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 6, 3, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 6, 4, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 6, 5, _, _ => md_red _ 13 1 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 6, 6, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 6, 7, _, _ => md_red _ 8 2 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 6, 8, _, _ => md_red _ 14 2 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 6, 9, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 6, 10, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 6, 11, _, _ => md_red _ 15 3 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 6, 12, _, _ => md_red _ 4 4 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 6, 13, _, _ => md_red _ 10 4 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 6, 14, _, _ => md_red _ 16 4 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 6, 15, _, _ => md_red _ 5 5 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 6, 16, _, _ => md_red _ 11 5 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 7, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 7, 1, _, _ => md_red _ 7 0 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 7, 2, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 7, 3, _, _ => md_red _ 4 1 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 7, 4, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 7, 5, _, _ => md_red _ 1 2 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 7, 6, _, _ => md_red _ 8 2 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 7, 7, _, _ => md_red _ 15 2 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 7, 8, _, _ => md_red _ 5 3 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 7, 9, _, _ => md_red _ 12 3 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 7, 10, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 7, 11, _, _ => md_red _ 9 4 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 7, 12, _, _ => md_red _ 16 4 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 7, 13, _, _ => md_red _ 6 5 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 7, 14, _, _ => md_red _ 13 5 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 7, 15, _, _ => md_red _ 3 6 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 7, 16, _, _ => md_red _ 10 6 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 8, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 8, 1, _, _ => md_red _ 8 0 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 8, 2, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 8, 3, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 8, 4, _, _ => md_red _ 15 1 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 8, 5, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 8, 6, _, _ => md_red _ 14 2 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 8, 7, _, _ => md_red _ 5 3 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 8, 8, _, _ => md_red _ 13 3 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 8, 9, _, _ => md_red _ 4 4 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 8, 10, _, _ => md_red _ 12 4 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 8, 11, _, _ => md_red _ 3 5 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 8, 12, _, _ => md_red _ 11 5 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 8, 13, _, _ => md_red _ 2 6 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 8, 14, _, _ => md_red _ 10 6 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 8, 15, _, _ => md_red _ 1 7 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 8, 16, _, _ => md_red _ 9 7 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 9, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 9, 1, _, _ => md_red _ 9 0 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 9, 2, _, _ => md_red _ 1 1 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 9, 3, _, _ => md_red _ 10 1 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 9, 4, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 9, 5, _, _ => md_red _ 11 2 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 9, 6, _, _ => md_red _ 3 3 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 9, 7, _, _ => md_red _ 12 3 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 9, 8, _, _ => md_red _ 4 4 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 9, 9, _, _ => md_red _ 13 4 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 9, 10, _, _ => md_red _ 5 5 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 9, 11, _, _ => md_red _ 14 5 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 9, 12, _, _ => md_red _ 6 6 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 9, 13, _, _ => md_red _ 15 6 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 9, 14, _, _ => md_red _ 7 7 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 9, 15, _, _ => md_red _ 16 7 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 9, 16, _, _ => md_red _ 8 8 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 10, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 10, 1, _, _ => md_red _ 10 0 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 10, 2, _, _ => md_red _ 3 1 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 10, 3, _, _ => md_red _ 13 1 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 10, 4, _, _ => md_red _ 6 2 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 10, 5, _, _ => md_red _ 16 2 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 10, 6, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 10, 7, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 10, 8, _, _ => md_red _ 12 4 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 10, 9, _, _ => md_red _ 5 5 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 10, 10, _, _ => md_red _ 15 5 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 10, 11, _, _ => md_red _ 8 6 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 10, 12, _, _ => md_red _ 1 7 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 10, 13, _, _ => md_red _ 11 7 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 10, 14, _, _ => md_red _ 4 8 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 10, 15, _, _ => md_red _ 14 8 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 10, 16, _, _ => md_red _ 7 9 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 11, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 11, 1, _, _ => md_red _ 11 0 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 11, 2, _, _ => md_red _ 5 1 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 11, 3, _, _ => md_red _ 16 1 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 11, 4, _, _ => md_red _ 10 2 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 11, 5, _, _ => md_red _ 4 3 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 11, 6, _, _ => md_red _ 15 3 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 11, 7, _, _ => md_red _ 9 4 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 11, 8, _, _ => md_red _ 3 5 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 11, 9, _, _ => md_red _ 14 5 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 11, 10, _, _ => md_red _ 8 6 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 11, 11, _, _ => md_red _ 2 7 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 11, 12, _, _ => md_red _ 13 7 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 11, 13, _, _ => md_red _ 7 8 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 11, 14, _, _ => md_red _ 1 9 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 11, 15, _, _ => md_red _ 12 9 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 11, 16, _, _ => md_red _ 6 10 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 12, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 12, 1, _, _ => md_red _ 12 0 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 12, 2, _, _ => md_red _ 7 1 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 12, 3, _, _ => md_red _ 2 2 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 12, 4, _, _ => md_red _ 14 2 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 12, 5, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 12, 6, _, _ => md_red _ 4 4 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 12, 7, _, _ => md_red _ 16 4 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 12, 8, _, _ => md_red _ 11 5 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 12, 9, _, _ => md_red _ 6 6 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 12, 10, _, _ => md_red _ 1 7 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 12, 11, _, _ => md_red _ 13 7 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 12, 12, _, _ => md_red _ 8 8 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 12, 13, _, _ => md_red _ 3 9 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 12, 14, _, _ => md_red _ 15 9 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 12, 15, _, _ => md_red _ 10 10 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 12, 16, _, _ => md_red _ 5 11 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 13, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 13, 1, _, _ => md_red _ 13 0 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 13, 2, _, _ => md_red _ 9 1 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 13, 3, _, _ => md_red _ 5 2 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 13, 4, _, _ => md_red _ 1 3 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 13, 5, _, _ => md_red _ 14 3 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 13, 6, _, _ => md_red _ 10 4 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 13, 7, _, _ => md_red _ 6 5 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 13, 8, _, _ => md_red _ 2 6 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 13, 9, _, _ => md_red _ 15 6 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 13, 10, _, _ => md_red _ 11 7 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 13, 11, _, _ => md_red _ 7 8 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 13, 12, _, _ => md_red _ 3 9 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 13, 13, _, _ => md_red _ 16 9 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 13, 14, _, _ => md_red _ 12 10 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 13, 15, _, _ => md_red _ 8 11 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 13, 16, _, _ => md_red _ 4 12 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 14, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 14, 1, _, _ => md_red _ 14 0 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 14, 2, _, _ => md_red _ 11 1 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 14, 3, _, _ => md_red _ 8 2 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 14, 4, _, _ => md_red _ 5 3 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 14, 5, _, _ => md_red _ 2 4 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 14, 6, _, _ => md_red _ 16 4 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 14, 7, _, _ => md_red _ 13 5 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 14, 8, _, _ => md_red _ 10 6 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 14, 9, _, _ => md_red _ 7 7 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 14, 10, _, _ => md_red _ 4 8 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 14, 11, _, _ => md_red _ 1 9 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 14, 12, _, _ => md_red _ 15 9 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 14, 13, _, _ => md_red _ 12 10 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 14, 14, _, _ => md_red _ 9 11 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 14, 15, _, _ => md_red _ 6 12 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 14, 16, _, _ => md_red _ 3 13 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 15, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 15, 1, _, _ => md_red _ 15 0 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 15, 2, _, _ => md_red _ 13 1 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 15, 3, _, _ => md_red _ 11 2 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 15, 4, _, _ => md_red _ 9 3 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 15, 5, _, _ => md_red _ 7 4 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 15, 6, _, _ => md_red _ 5 5 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 15, 7, _, _ => md_red _ 3 6 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 15, 8, _, _ => md_red _ 1 7 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | 15, 9, _, _ => md_red _ 16 7 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 15, 10, _, _ => md_red _ 14 8 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 15, 11, _, _ => md_red _ 12 9 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 15, 12, _, _ => md_red _ 10 10 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 15, 13, _, _ => md_red _ 8 11 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 15, 14, _, _ => md_red _ 6 12 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 15, 15, _, _ => md_red _ 4 13 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 15, 16, _, _ => md_red _ 2 14 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 16, 0, _, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 16, 1, _, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 16, 2, _, _ => md_red _ 15 1 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 16, 3, _, _ => md_red _ 14 2 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 16, 4, _, _ => md_red _ 13 3 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 16, 5, _, _ => md_red _ 12 4 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 16, 6, _, _ => md_red _ 11 5 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 16, 7, _, _ => md_red _ 10 6 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 16, 8, _, _ => md_red _ 9 7 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 16, 9, _, _ => md_red _ 8 8 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 16, 10, _, _ => md_red _ 7 9 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 16, 11, _, _ => md_red _ 6 10 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 16, 12, _, _ => md_red _ 5 11 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 16, 13, _, _ => md_red _ 4 12 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 16, 14, _, _ => md_red _ 3 13 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 16, 15, _, _ => md_red _ 2 14 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 16, 16, _, _ => md_red _ 1 15 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | k + 17, _, h, _ => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))
  | _, k + 17, _, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem neg_nat : ∀ x : Nat, x < 17 → md (4 * 3 + 3 + 1) ((4 * 3 + 3 + 1) * x) = ((4 * 3 + 3 + 1) * x) % 17
  | 0, _ => md_red _ 0 0 rfl (Nat.le_add_right 1 16 : 0 < 4 * 3 + 3 + 1 + 1)
  | 1, _ => md_red _ 16 0 rfl (Nat.le_add_right 17 0 : 16 < 4 * 3 + 3 + 1 + 1)
  | 2, _ => md_red _ 15 1 rfl (Nat.le_add_right 16 1 : 15 < 4 * 3 + 3 + 1 + 1)
  | 3, _ => md_red _ 14 2 rfl (Nat.le_add_right 15 2 : 14 < 4 * 3 + 3 + 1 + 1)
  | 4, _ => md_red _ 13 3 rfl (Nat.le_add_right 14 3 : 13 < 4 * 3 + 3 + 1 + 1)
  | 5, _ => md_red _ 12 4 rfl (Nat.le_add_right 13 4 : 12 < 4 * 3 + 3 + 1 + 1)
  | 6, _ => md_red _ 11 5 rfl (Nat.le_add_right 12 5 : 11 < 4 * 3 + 3 + 1 + 1)
  | 7, _ => md_red _ 10 6 rfl (Nat.le_add_right 11 6 : 10 < 4 * 3 + 3 + 1 + 1)
  | 8, _ => md_red _ 9 7 rfl (Nat.le_add_right 10 7 : 9 < 4 * 3 + 3 + 1 + 1)
  | 9, _ => md_red _ 8 8 rfl (Nat.le_add_right 9 8 : 8 < 4 * 3 + 3 + 1 + 1)
  | 10, _ => md_red _ 7 9 rfl (Nat.le_add_right 8 9 : 7 < 4 * 3 + 3 + 1 + 1)
  | 11, _ => md_red _ 6 10 rfl (Nat.le_add_right 7 10 : 6 < 4 * 3 + 3 + 1 + 1)
  | 12, _ => md_red _ 5 11 rfl (Nat.le_add_right 6 11 : 5 < 4 * 3 + 3 + 1 + 1)
  | 13, _ => md_red _ 4 12 rfl (Nat.le_add_right 5 12 : 4 < 4 * 3 + 3 + 1 + 1)
  | 14, _ => md_red _ 3 13 rfl (Nat.le_add_right 4 13 : 3 < 4 * 3 + 3 + 1 + 1)
  | 15, _ => md_red _ 2 14 rfl (Nat.le_add_right 3 14 : 2 < 4 * 3 + 3 + 1 + 1)
  | 16, _ => md_red _ 1 15 rfl (Nat.le_add_right 2 15 : 1 < 4 * 3 + 3 + 1 + 1)
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem addE (x y : Zp (4 * 3 + 3 + 1)) : F.add x y = fadd x y := Subtype.ext (add_nat x.val y.val x.2 y.2)
theorem mulE (x y : Zp (4 * 3 + 3 + 1)) : F.mul x y = fmul x y := Subtype.ext (mul_nat x.val y.val x.2 y.2)
theorem negE (x : Zp (4 * 3 + 3 + 1)) : F.neg x = fneg x := Subtype.ext (by show md (4 * 3 + 3 + 1) ((4 * 3 + 3 + 1) * x.val) = ((4 * 3 + 3 + 1) * x.val) % 17; exact neg_nat x.val x.2)

def c4f (u v : Zp (4 * 3 + 3 + 1) × Zp (4 * 3 + 3 + 1)) : Zp (4 * 3 + 3 + 1) × Zp (4 * 3 + 3 + 1) :=
  (fadd (fmul u.1 v.1) (fneg (fmul u.2 v.2)), fadd (fmul u.1 v.2) (fmul u.2 v.1))
theorem c4E (u v : Zp (4 * 3 + 3 + 1) × Zp (4 * 3 + 3 + 1)) : C.mul u v = c4f u v := by
  show Cyclo4.c4mul F u v = _
  unfold Cyclo4.c4mul
  rw [mulE, mulE, mulE, mulE, negE, addE, addE]; rfl
def pwf (z : Zp (4 * 3 + 3 + 1) × Zp (4 * 3 + 3 + 1)) : Nat → Zp (4 * 3 + 3 + 1) × Zp (4 * 3 + 3 + 1)
  | 0 => C.one
  | k + 1 => c4f (pwf z k) z
theorem pwE (z : Zp (4 * 3 + 3 + 1) × Zp (4 * 3 + 3 + 1)) : ∀ k, pw C z k = pwf z k
  | 0 => rfl
  | k + 1 => by show C.mul (pw C z k) z = _; rw [c4E, pwE z k]; rfl

def nrmf (X Y : Zp (4 * 3 + 3 + 1)) : Zp (4 * 3 + 3 + 1) := fadd (fmul X X) (fmul Y Y)
def lhsf (X Y : Zp (4 * 3 + 3 + 1)) : Zp (4 * 3 + 3 + 1) × Zp (4 * 3 + 3 + 1) := c4f (pwf (c4f (c4f (X, Y) (X, Y)) C.one) 4) (pwf (nrmf X Y, F.zero) 4)
def rhsf (X Y : Zp (4 * 3 + 3 + 1)) : Zp (4 * 3 + 3 + 1) := fmul (fmul X Y) (fadd (fmul X X) (fneg (fmul Y Y)))

theorem nrmE (X Y : Zp (4 * 3 + 3 + 1)) : nrm X Y = nrmf X Y := by
  unfold nrm nrmf; rw [mulE, mulE, addE]
theorem lhsE (X Y : Zp (4 * 3 + 3 + 1)) : lhs X Y = lhsf X Y := by
  unfold lhs lhsf; rw [nrmE, c4E, c4E, pwE, pwE, c4E]
theorem rhsE (X Y : Zp (4 * 3 + 3 + 1)) : rhs X Y = rhsf X Y := by
  unfold rhs rhsf
  repeat (first | rw [mulE] | rw [addE] | rw [negE])

def code (z : Zp (4 * 3 + 3 + 1) × Zp (4 * 3 + 3 + 1)) : Nat := z.1.val + 17 * z.2.val
def chk (X Y : Zp (4 * 3 + 3 + 1)) : Bool := (!(Nat.beq (code (lhsf X Y)) 1) || Nat.beq (rhsf X Y).val 0) &&
  (!(Nat.beq (rhsf X Y).val 0) || Nat.beq (nrmf X Y).val 0 || Nat.beq (code (lhsf X Y)) 1)

theorem row0 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 0) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row1 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 1) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row2 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 2) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row3 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 3) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row4 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 4) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row5 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 5) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row6 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 6) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row7 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 7) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row8 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 8) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row9 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 9) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row10 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 10) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row11 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 11) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row12 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 12) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row13 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 13) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row14 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 14) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row15 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 15) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem row16 : ∀ y : Nat, y < 17 → chk (mk (4 * 3 + 3 + 1) 16) (mk (4 * 3 + 3 + 1) y) = true
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | 8, _ => rfl
  | 9, _ => rfl
  | 10, _ => rfl
  | 11, _ => rfl
  | 12, _ => rfl
  | 13, _ => rfl
  | 14, _ => rfl
  | 15, _ => rfl
  | 16, _ => rfl
  | k + 17, h => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem chk_nat : ∀ x y : Nat, x < 17 → y < 17 → chk (mk (4 * 3 + 3 + 1) x) (mk (4 * 3 + 3 + 1) y) = true
  | 0, y, _, h => row0 y h
  | 1, y, _, h => row1 y h
  | 2, y, _, h => row2 y h
  | 3, y, _, h => row3 y h
  | 4, y, _, h => row4 y h
  | 5, y, _, h => row5 y h
  | 6, y, _, h => row6 y h
  | 7, y, _, h => row7 y h
  | 8, y, _, h => row8 y h
  | 9, y, _, h => row9 y h
  | 10, y, _, h => row10 y h
  | 11, y, _, h => row11 y h
  | 12, y, _, h => row12 y h
  | 13, y, _, h => row13 y h
  | 14, y, _, h => row14 y h
  | 15, y, _, h => row15 y h
  | 16, y, _, h => row16 y h
  | k + 17, _, h, _ => absurd h (fun e => Nat.lt_irrefl 17 (Nat.lt_of_le_of_lt (Nat.le_add_left 17 k) e))

theorem chk_all (X Y : Zp (4 * 3 + 3 + 1)) : chk X Y = true := by
  rw [show X = mk (4 * 3 + 3 + 1) X.val from Subtype.ext (md_small _ _ X.2).symm, show Y = mk (4 * 3 + 3 + 1) Y.val from Subtype.ext (md_small _ _ Y.2).symm]
  exact chk_nat X.val Y.val X.2 Y.2

theorem table (X Y : Zp (4 * 3 + 3 + 1)) (h : lhs X Y = C.one) : rhs X Y = F.zero := by
  have hc := band_l _ _ (chk_all X Y)
  rw [← lhsE, h] at hc
  rw [rhsE]
  exact (Subtype.ext (beq_eq _ _ hc) : rhsf X Y = ⟨0, Nat.zero_lt_succ _⟩)

theorem table' (X Y : Zp (4 * 3 + 3 + 1)) (h : rhs X Y = F.zero) (hn : nrm X Y ≠ F.zero) : lhs X Y = C.one := by
  have hc := band_r _ _ (chk_all X Y)
  rw [← rhsE, h] at hc
  rw [lhsE]
  rw [nrmE] at hn
  cases hb : Nat.beq (nrmf X Y).val 0 with
  | true => exact absurd (Subtype.ext (beq_eq _ _ hb) : nrmf X Y = F.zero) hn
  | false =>
    rw [hb] at hc
    have e := beq_eq _ _ hc
    match lhsf X Y, e with
    | (u, v), e =>
      have e' : u.val + 17 * v.val = 1 := e
      have hv : v.val = 0 := by
        cases hv' : v.val with
        | zero => rfl
        | succ t => rw [hv', show 17 * (t + 1) = 17 * t + 16 + 1 by lib_nat, show u.val + (17 * t + 16 + 1) = u.val + 17 * t + 16 + 1 by lib_nat] at e'; exact Nat.noConfusion (Nat.succ.inj e')
      have hu : u.val = 1 := by rw [hv] at e'; exact e'
      exact Prod.ext (Subtype.ext hu) (Subtype.ext hv)

theorem cast_norm (x y : Int) : castZ F (x * x + y * y) = nrm (castZ F x) (castZ F y) := by
  rw [IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_mul]; rfl

theorem cast_rhs (x y : Int) : castZ F (x * y * (x * x - y * y)) = rhs (castZ F x) (castZ F y) := by
  rw [show x * y * (x * x - y * y) = x * y * (x * x + -(y * y)) from rfl]
  repeat (first | rw [HasseDavenport.castZ_mul] | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg])
  rfl

theorem hP (hpr : ∀ d, Dv d (4 * (n + 4) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 4) + 3 + 1 + 1) (ι : Zp (4 * (n + 4) + 3 + 1)) (hi : (ring (4 * (n + 4) + 3 + 1)).mul ι ι = (ring (4 * (n + 4) + 3 + 1)).neg (ring (4 * (n + 4) + 3 + 1)).one) :
    ph4 F (Ppq (n + 4)) = (nrm (castZ F (Jq (n + 4) (chi4 (n + 4) ι)).1) (castZ F (Jq (n + 4) (chi4 (n + 4) ι)).2), F.zero) := by
  rw [show Ppq (n + 4) = (Int.ofNat (4 * (n + 4) + 3 + 1 + 1), 0) from sum_oneq (n + 4) _, ← quart_norm (n + 4) hpr ι hi]; unfold ph4; rw [cast_norm]; rfl

theorem chi4_cases (ι x : Zp (4 * (n + 4) + 3 + 1)) : chi4 (n + 4) ι x = (0, 0) ∨ chi4 (n + 4) ι x = (1, 0) ∨ chi4 (n + 4) ι x = (0, 1) ∨ chi4 (n + 4) ι x = (-1, 0) ∨ chi4 (n + 4) ι x = (0, -1) := by
  unfold chi4
  by_cases h1 : x = (ring (4 * (n + 4) + 3 + 1)).zero
  · exact Or.inl (ite_t _ _ h1)
  · rw [ite_f _ _ h1]
    by_cases h2 : pw (ring (4 * (n + 4) + 3 + 1)) x ((n + 4) + 1) = (ring (4 * (n + 4) + 3 + 1)).one
    · exact Or.inr (Or.inl (ite_t _ _ h2))
    · rw [ite_f _ _ h2]
      by_cases h3 : pw (ring (4 * (n + 4) + 3 + 1)) x ((n + 4) + 1) = ι
      · exact Or.inr (Or.inr (Or.inl (ite_t _ _ h3)))
      · rw [ite_f _ _ h3]
        by_cases h4 : pw (ring (4 * (n + 4) + 3 + 1)) x ((n + 4) + 1) = (ring (4 * (n + 4) + 3 + 1)).neg (ring (4 * (n + 4) + 3 + 1)).one
        · exact Or.inr (Or.inr (Or.inr (Or.inl (ite_t _ _ h4))))
        · exact Or.inr (Or.inr (Or.inr (Or.inr (ite_f _ _ h4))))

theorem lhs_one (hpr : ∀ d, Dv d (4 * (n + 4) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 4) + 3 + 1 + 1) (ι : Zp (4 * (n + 4) + 3 + 1)) (hi : (ring (4 * (n + 4) + 3 + 1)).mul ι ι = (ring (4 * (n + 4) + 3 + 1)).neg (ring (4 * (n + 4) + 3 + 1)).one) :
    ph4 F (chib (n + 4) ι (nsm (ring (4 * (n + 4) + 3 + 1)) (4 * 3 + 3 + 1 + 1) (ring (4 * (n + 4) + 3 + 1)).one)) = lhs (castZ F (Jq (n + 4) (chi4 (n + 4) ι)).1) (castZ F (Jq (n + 4) (chi4 (n + 4) ι)).2) := by
  rw [quart_recip_split (n + 4) 3 hpr q_prime ι hi (q_ne n), hP n hpr ι hi, ph4_one]; rfl

/-- **17 a quartic residue mod p ⇒ 17 | ab(a² − b²)**. -/
theorem quart_seventeen (hpr : ∀ d, Dv d (4 * (n + 4) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 4) + 3 + 1 + 1) (ι : Zp (4 * (n + 4) + 3 + 1)) (hi : (ring (4 * (n + 4) + 3 + 1)).mul ι ι = (ring (4 * (n + 4) + 3 + 1)).neg (ring (4 * (n + 4) + 3 + 1)).one)
    (hq : chi4 (n + 4) ι (nsm (ring (4 * (n + 4) + 3 + 1)) (4 * 3 + 3 + 1 + 1) (ring (4 * (n + 4) + 3 + 1)).one) = (1, 0)) : IDv (Int.ofNat 17) ((Jq (n + 4) (chi4 (n + 4) ι)).1 * (Jq (n + 4) (chi4 (n + 4) ι)).2 * ((Jq (n + 4) (chi4 (n + 4) ι)).1 * (Jq (n + 4) (chi4 (n + 4) ι)).1 - (Jq (n + 4) (chi4 (n + 4) ι)).2 * (Jq (n + 4) (chi4 (n + 4) ι)).2)) := by
  have hc := lhs_one n hpr ι hi
  rw [show chib (n + 4) ι (nsm (ring (4 * (n + 4) + 3 + 1)) (4 * 3 + 3 + 1 + 1) (ring (4 * (n + 4) + 3 + 1)).one) = gconj (chi4 (n + 4) ι (nsm (ring (4 * (n + 4) + 3 + 1)) (4 * 3 + 3 + 1 + 1) (ring (4 * (n + 4) + 3 + 1)).one)) from chi_ivq (n + 4) hpr ι hi _, hq] at hc
  have hl : lhs (castZ F (Jq (n + 4) (chi4 (n + 4) ι)).1) (castZ F (Jq (n + 4) (chi4 (n + 4) ι)).2) = C.one := hc.symm.trans (Prod.ext (rfl : castZ F 1 = F.one) (rfl : castZ F (-0) = F.zero))
  exact CubicTwo.cast_zero_gen (4 * 3 + 3 + 1) _ ((cast_rhs _ _).trans (table _ _ hl))

/-- **17 | ab(a² − b²) ⇒ 17 is a quartic residue mod p.** -/
theorem quart_seventeen_conv (hpr : ∀ d, Dv d (4 * (n + 4) + 3 + 1 + 1) → d = 1 ∨ d = 4 * (n + 4) + 3 + 1 + 1) (ι : Zp (4 * (n + 4) + 3 + 1)) (hi : (ring (4 * (n + 4) + 3 + 1)).mul ι ι = (ring (4 * (n + 4) + 3 + 1)).neg (ring (4 * (n + 4) + 3 + 1)).one)
    (hd : IDv (Int.ofNat 17) ((Jq (n + 4) (chi4 (n + 4) ι)).1 * (Jq (n + 4) (chi4 (n + 4) ι)).2 * ((Jq (n + 4) (chi4 (n + 4) ι)).1 * (Jq (n + 4) (chi4 (n + 4) ι)).1 - (Jq (n + 4) (chi4 (n + 4) ι)).2 * (Jq (n + 4) (chi4 (n + 4) ι)).2))) : chi4 (n + 4) ι (nsm (ring (4 * (n + 4) + 3 + 1)) (4 * 3 + 3 + 1 + 1) (ring (4 * (n + 4) + 3 + 1)).one) = (1, 0) := by
  have hc := lhs_one n hpr ι hi
  have hr : rhs (castZ F (Jq (n + 4) (chi4 (n + 4) ι)).1) (castZ F (Jq (n + 4) (chi4 (n + 4) ι)).2) = F.zero := by
    rw [← cast_rhs]
    match hd with
    | ⟨c, hc'⟩ =>
      rw [hc', HasseDavenport.castZ_mul]
      have h0 : castZ F (Int.ofNat 17) = F.zero := CubeAp.castP 15
      rw [h0, zero_mul']
  have hn : nrm (castZ F (Jq (n + 4) (chi4 (n + 4) ι)).1) (castZ F (Jq (n + 4) (chi4 (n + 4) ι)).2) ≠ F.zero := fun e => by
    have e1 : castZ F (Int.ofNat (4 * (n + 4) + 3 + 1 + 1)) = F.zero := by rw [← quart_norm (n + 4) hpr ι hi, cast_norm]; exact e
    have e2 : mk (4 * 3 + 3 + 1) (4 * (n + 4) + 3 + 1 + 1) = F.zero := (IntCast.castN_mk _ _).symm.trans e1
    have hdq : ∀ P' : Nat, mk (4 * 3 + 3 + 1) P' = F.zero → Dv (4 * 3 + 3 + 1 + 1) P' := fun P' h => md_zero_dv (4 * 3 + 3 + 1) P' (congrArg Subtype.val h)
    exact not_dvq n hpr (hdq _ e2)
  have h1 := hc.trans (table' _ _ hr hn)
  rw [show chib (n + 4) ι (nsm (ring (4 * (n + 4) + 3 + 1)) (4 * 3 + 3 + 1 + 1) (ring (4 * (n + 4) + 3 + 1)).one) = gconj (chi4 (n + 4) ι (nsm (ring (4 * (n + 4) + 3 + 1)) (4 * 3 + 3 + 1 + 1) (ring (4 * (n + 4) + 3 + 1)).one)) from chi_ivq (n + 4) hpr ι hi _] at h1
  rcases chi4_cases n ι (nsm (ring (4 * (n + 4) + 3 + 1)) (4 * 3 + 3 + 1 + 1) (ring (4 * (n + 4) + 3 + 1)).one) with e | e | e | e | e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · exact e
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion (Nat.succ.inj e'))
  · rw [e] at h1; exact absurd (congrArg (fun z => z.1.val) h1) (fun e' => Nat.noConfusion e')

end QuartSeventeen
#print axioms QuartSeventeen.lt_wrap
#print axioms QuartSeventeen.ndvN
#print axioms QuartSeventeen.q_prime
#print axioms QuartSeventeen.q_ne
#print axioms QuartSeventeen.not_dvq
#print axioms QuartSeventeen.md_red
#print axioms QuartSeventeen.add_nat
#print axioms QuartSeventeen.mul_nat
#print axioms QuartSeventeen.neg_nat
#print axioms QuartSeventeen.addE
#print axioms QuartSeventeen.mulE
#print axioms QuartSeventeen.negE
#print axioms QuartSeventeen.c4E
#print axioms QuartSeventeen.pwE
#print axioms QuartSeventeen.nrmE
#print axioms QuartSeventeen.lhsE
#print axioms QuartSeventeen.rhsE
#print axioms QuartSeventeen.row0
#print axioms QuartSeventeen.row1
#print axioms QuartSeventeen.row2
#print axioms QuartSeventeen.row3
#print axioms QuartSeventeen.row4
#print axioms QuartSeventeen.row5
#print axioms QuartSeventeen.row6
#print axioms QuartSeventeen.row7
#print axioms QuartSeventeen.row8
#print axioms QuartSeventeen.row9
#print axioms QuartSeventeen.row10
#print axioms QuartSeventeen.row11
#print axioms QuartSeventeen.row12
#print axioms QuartSeventeen.row13
#print axioms QuartSeventeen.row14
#print axioms QuartSeventeen.row15
#print axioms QuartSeventeen.row16
#print axioms QuartSeventeen.chk_nat
#print axioms QuartSeventeen.chk_all
#print axioms QuartSeventeen.table
#print axioms QuartSeventeen.table'
#print axioms QuartSeventeen.cast_norm
#print axioms QuartSeventeen.cast_rhs
#print axioms QuartSeventeen.hP
#print axioms QuartSeventeen.chi4_cases
#print axioms QuartSeventeen.lhs_one
#print axioms QuartSeventeen.quart_seventeen
#print axioms QuartSeventeen.quart_seventeen_conv

#print axioms: 45 theorems, no axioms.

78.63 The quartic Jacobi sum modulo \(4\)

Pairing \(t\leftrightarrow 1-t\) in \(J=\sum _t\chi (t(1-t))\), with \(J(\rho ,\rho )=-1\) and \(\rho (1/2)=\chi (-1)\).

\[ J=a+bi,\qquad a\equiv \chi (-1)+2n,\quad b\equiv 2n+2 \pmod 4\qquad (p=4n+5). \]

lang/QuartPrimary.lean

/-
  lang/QuartPrimary — T43, cycle 230: the quartic Jacobi sum mod 4 (primary normalisation).

  p = 4n + 5, χ quartic, J = J(χ, χ) = Σ_t f(t), f(t) = χ(t)χ(1 − t) = χ(t(1 − t)), J = a + bi. The involution t ↦ 1 − t fixes
  only h = 1/2 (Pairing), f(h) = χ(1/4) = ρ(h) is real, and Re f ≡ [f real], Im f ≡ [f imaginary] (mod 2). With
  Σ_t [f real] − [f imaginary] = Σ_t ρ(t(1 − t)) = −1 and Σ_t [f ≠ 0] = p − 2:
      quart_primary   a = Re ρ(h) + 2n + 4K,  b = 2n + 2 + 4K'      (so a ≡ −1, b ≡ (p − 1)/2 mod 4 once ρ(h) = χ(−1))
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.ResidueCriterion

namespace QuartPrimary

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi Pairing
open LibRing (castZ castN)

variable (n : Nat)

def om (t : Zp (4 * n + 3 + 1)) : Zp (4 * n + 3 + 1) := (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg t)
def f (ι t : Zp (4 * n + 3 + 1)) : Int × Int := (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chi4 n ι (om n t))
def h : Zp (4 * n + 3 + 1) := mk (4 * n + 3 + 1) (2 * n + 3)

theorem om_om (t : Zp (4 * n + 3 + 1)) : om n (om n t) = t := by unfold om; lib_ring

theorem f_om (ι t : Zp (4 * n + 3 + 1)) : f n ι (om n t) = f n ι t := by
  unfold f; rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _

theorem hh : (ring (4 * n + 3 + 1)).add (h n) (h n) = (ring (4 * n + 3 + 1)).one := Subtype.ext (by
  show md (4 * n + 3 + 1) (md (4 * n + 3 + 1) (2 * n + 3) + md (4 * n + 3 + 1) (2 * n + 3)) = md (4 * n + 3 + 1) 1
  rw [← md_add2, show 2 * n + 3 + (2 * n + 3) = 1 + (4 * n + 3 + 1 + 1) by lib_nat, md_add_p])

theorem om_h : om n (h n) = h n := by
  unfold om; rw [← hh n]; lib_ring

theorem fix_h (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (t : Zp (4 * n + 3 + 1)) (e : om n t = t) : t = h n := by
  have e2 : (ring (4 * n + 3 + 1)).add t t = (ring (4 * n + 3 + 1)).add (h n) (h n) := by
    rw [hh]; calc (ring (4 * n + 3 + 1)).add t t = (ring (4 * n + 3 + 1)).add t (om n t) := by rw [e]
      _ = (ring (4 * n + 3 + 1)).one := by unfold om; lib_ring
  have e3 : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).add t ((ring (4 * n + 3 + 1)).neg (h n))) = (ring (4 * n + 3 + 1)).zero := by
    calc _ = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add t t) ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add (h n) (h n))) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e2]; lib_ring
  cases nozd (4 * n + 3) hpr _ _ e3 with
  | inl e4 => exact absurd e4 (Eisen.two_ne_zero' (4 * n + 3) (by rw [show 4 * n + 3 = (4 * n + 1) + 2 by lib_nat]; exact Nat.le_add_left 2 _))
  | inr e4 => calc t = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add t ((ring (4 * n + 3 + 1)).neg (h n))) (h n) := by lib_ring
      _ = h n := by rw [e4]; lib_ring

theorem sum_fst {A : Type} (g : A → Int × Int) : ∀ l : List A, (sumK (Cyclo4.cyc4 LibInt.intR) g l).1 = sumK LibInt.intR (fun x => (g x).1) l
  | [] => rfl
  | a :: as => by show (g a).1 + (sumK (Cyclo4.cyc4 LibInt.intR) g as).1 = _; rw [sum_fst g as]; rfl

theorem sum_snd {A : Type} (g : A → Int × Int) : ∀ l : List A, (sumK (Cyclo4.cyc4 LibInt.intR) g l).2 = sumK LibInt.intR (fun x => (g x).2) l
  | [] => rfl
  | a :: as => by show (g a).2 + (sumK (Cyclo4.cyc4 LibInt.intR) g as).2 = _; rw [sum_snd g as]; rfl

/-- the fixed-point part of the pairing. -/
theorem fixed_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (F : Zp (4 * n + 3 + 1) → Int) :
    sumK LibInt.intR (fun x => eqI (4 * n + 3) (om n x) x * F x) (LL n) = F (h n) := by
  rw [← FiniteRecChain.pick LibInt.intR (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) LibInt.intR) F (h n)]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    unfold eqI delta
    by_cases hx : x = h n
    · rw [ite_pos' _ _ (show om n x = x by rw [hx]; exact om_h n), ite_pos' _ _ hx]; rfl
    · rw [ite_neg' _ _ (fun e => hx (fix_h n hpr x e)), ite_neg' _ _ hx]; rfl) _

theorem pair (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (F : Zp (4 * n + 3 + 1) → Int) (hF : ∀ x, F (om n x) = F x) :
    sumK LibInt.intR F (LL n) = F (h n) + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * F x) (LL n) := by
  rw [Pairing.pairing (4 * n + 3) (om n) (om_om n) F hF (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) LibInt.intR), fixed_sum n hpr]

/-! ## sums of integers -/

theorem sI_add {A : Type} (F G : A → Int) (l : List A) : sumK LibInt.intR (fun x => F x + G x) l = sumK LibInt.intR F l + sumK LibInt.intR G l :=
  sumK_add LibInt.intR F G l
theorem sI_mul {A : Type} (c : Int) (F : A → Int) (l : List A) : sumK LibInt.intR (fun x => c * F x) l = c * sumK LibInt.intR F l :=
  (mul_sumK LibInt.intR c F l).symm
theorem sI_neg {A : Type} (F : A → Int) : ∀ l : List A, sumK LibInt.intR (fun x => -F x) l = -sumK LibInt.intR F l
  | [] => rfl
  | a :: as => by show -F a + sumK LibInt.intR (fun x => -F x) as = -(F a + sumK LibInt.intR F as); rw [sI_neg F as]; lib_int

/-! ## the summand f(t) = χ(t(1 − t)) -/

theorem f_eq (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (t : Zp (4 * n + 3 + 1)) : f n ι t = chi4 n ι ((ring (4 * n + 3 + 1)).mul t (om n t)) :=
  (chi4_mul n hpr ι hi t (om n t)).symm

def Ri (ι t : Zp (4 * n + 3 + 1)) : Int := (f n ι t).1 * (f n ι t).1
def Ii (ι t : Zp (4 * n + 3 + 1)) : Int := (f n ι t).2 * (f n ι t).2
def c1 (ι t : Zp (4 * n + 3 + 1)) : Int := -(((f n ι t).1 * (f n ι t).1 - (f n ι t).1) / 2)
def c2 (ι t : Zp (4 * n + 3 + 1)) : Int := -(((f n ι t).2 * (f n ι t).2 - (f n ι t).2) / 2)

theorem dec1 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (t : Zp (4 * n + 3 + 1)) : (f n ι t).1 = Ri n ι t + 2 * c1 n ι t := by
  unfold Ri c1; rw [f_eq n hpr ι hi]
  rcases ResidueCriterion.chi4_cases n ι ((ring (4 * n + 3 + 1)).mul t (om n t)) with e | e | e | e | e <;> rw [e] <;> rfl

theorem dec2 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (t : Zp (4 * n + 3 + 1)) : (f n ι t).2 = Ii n ι t + 2 * c2 n ι t := by
  unfold Ii c2; rw [f_eq n hpr ι hi]
  rcases ResidueCriterion.chi4_cases n ι ((ring (4 * n + 3 + 1)).mul t (om n t)) with e | e | e | e | e <;> rw [e] <;> rfl

/-- Ri − Ii is the real part of f² = ρ(t)ρ(1 − t). -/
theorem RmI (ι t : Zp (4 * n + 3 + 1)) : Ri n ι t + -Ii n ι t = ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (rho n ι (om n t))).1 := by
  have e : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (rho n ι (om n t)) = (Cyclo4.cyc4 LibInt.intR).mul (f n ι t) (f n ι t) := by unfold rho f; lib_ring
  rw [e]; rfl

/-- chi4 x ≠ 0 for x ≠ 0. -/
theorem chi4_ne (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) : chi4 n ι x ≠ (0, 0) := fun e => by
  have h1 : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι x) (chi4 n ι (ivq n x)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, (ring (4 * n + 3 + 1)).mul_comm, ivq_mul n hpr x hx]; exact chi_one n hpr ι hi
  rw [e] at h1
  have h2 : (Cyclo4.cyc4 LibInt.intR).zero = (Cyclo4.cyc4 LibInt.intR).one := (show (Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero (chi4 n ι (ivq n x)) = (Cyclo4.cyc4 LibInt.intR).zero by lib_ring).symm.trans h1
  exact absurd (congrArg Prod.fst h2) (fun e' => Nat.noConfusion (Int.ofNat.inj e'))

/-- Ri + Ii = [t ≠ 0][t ≠ 1]. -/
theorem RpI (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (t : Zp (4 * n + 3 + 1)) :
    Ri n ι t + Ii n ι t = 1 + -(delta LibInt.intR t (ring (4 * n + 3 + 1)).zero) + -(delta LibInt.intR t (ring (4 * n + 3 + 1)).one) := by
  unfold Ri Ii; rw [f_eq n hpr ι hi]
  by_cases h0 : t = (ring (4 * n + 3 + 1)).zero
  · rw [h0, show (ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).zero (om n (ring (4 * n + 3 + 1)).zero) = (ring (4 * n + 3 + 1)).zero by lib_ring, chi4_zero]
    unfold delta; rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e => PowerSum.one_ne_zero' (4 * n + 3) e.symm)]; rfl
  · by_cases h1 : t = (ring (4 * n + 3 + 1)).one
    · rw [h1, show (ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one (om n (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).zero by unfold om; lib_ring, chi4_zero]
      unfold delta; rw [ite_neg' _ _ (fun e => PowerSum.one_ne_zero' (4 * n + 3) e), ite_pos' _ _ rfl]; rfl
    · have hx : (ring (4 * n + 3 + 1)).mul t (om n t) ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
        cases nozd (4 * n + 3) hpr _ _ e with
        | inl e1 => exact h0 e1
        | inr e1 => exact h1 (by
            calc t = (ring (4 * n + 3 + 1)).add t (om n t) := by rw [e1]; lib_ring
              _ = (ring (4 * n + 3 + 1)).one := by unfold om; lib_ring)
      unfold delta; rw [ite_neg' _ _ h0, ite_neg' _ _ h1]
      rcases ResidueCriterion.chi4_cases n ι ((ring (4 * n + 3 + 1)).mul t (om n t)) with e | e | e | e | e
      · exact absurd e (chi4_ne n hpr ι hi _ hx)
      all_goals (rw [e]; rfl)

/-! ## the sums -/

theorem sum_one : sumK LibInt.intR (fun _ : Zp (4 * n + 3 + 1) => (1 : Int)) (LL n) = Int.ofNat (4 * n + 3 + 1 + 1) :=
  (sum_fst (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n)).symm.trans (congrArg Prod.fst (sum_oneq n _))

theorem sum_delta (z : Zp (4 * n + 3 + 1)) : sumK LibInt.intR (fun t => delta LibInt.intR t z) (LL n) = 1 :=
  CMInert.complete_allZ (4 * n + 3 + 1) LibInt.intR z

theorem sum_RpI (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    sumK LibInt.intR (Ri n ι) (LL n) + sumK LibInt.intR (Ii n ι) (LL n) = Int.ofNat (4 * n + 3 + 1 + 1) + -2 := by
  rw [← sI_add, sumK_ext LibInt.intR _ _ (RpI n hpr ι hi), sI_add, sI_add, sI_neg, sI_neg, sum_one, sum_delta, sum_delta]; lib_int

/-- J(ρ, ρ) = −1. -/
theorem J_rho (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun t => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (rho n ι (om n t))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one := by
  rw [← FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) ((ring (4 * n + 3 + 1)).neg) ((ring (4 * n + 3 + 1)).neg) (fun x => by lib_ring) (fun x => by lib_ring)]
  rw [← D_ne' n hpr (rho n ι) (rho_mul n hpr ι hi) (rho_one n hpr ι hi) (rho_zero n ι) (rho_sum n hpr ι hi) (ring (4 * n + 3 + 1)).one (fun e => PowerSum.one_ne_zero' (4 * n + 3) e)]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun b => by
    show (Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).neg b)) (rho n ι (om n ((ring (4 * n + 3 + 1)).neg b))) = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one b)) (rho n ι (ivq n b))
    rw [rho_iv n hpr ι hi, show om n ((ring (4 * n + 3 + 1)).neg b) = (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one b by unfold om; lib_ring,
      show (ring (4 * n + 3 + 1)).neg b = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) b by lib_ring, rho_mul n hpr ι hi, rho_neg1 n hpr ι hi]
    lib_ring) _

theorem sum_RmI (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    sumK LibInt.intR (Ri n ι) (LL n) + -sumK LibInt.intR (Ii n ι) (LL n) = -1 := by
  rw [← sI_neg, ← sI_add, sumK_ext LibInt.intR _ _ (RmI n ι), ← sum_fst, J_rho n hpr ι hi]; rfl

theorem two_cancel (x y : Int) (h : 2 * x = 2 * y) : x = y := by
  have h2 : 2 * (x + -y) = 0 := by
    calc 2 * (x + -y) = 2 * x + -(2 * y) := by lib_int
      _ = 2 * y + -(2 * y) := by rw [h]
      _ = 0 := by lib_int
  cases LibInt.int_noZD _ _ h2 with
  | inl e => exact absurd e (fun e' => Nat.noConfusion (Int.ofNat.inj e'))
  | inr e => calc x = (x + -y) + y := by lib_int
      _ = y := by rw [e]; exact LibInt.intR.zero_add y

theorem sum_Ri (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK LibInt.intR (Ri n ι) (LL n) = 2 * Int.ofNat n + 1 := by
  apply two_cancel
  have e1 := sum_RpI n hpr ι hi
  have e2 := sum_RmI n hpr ι hi
  rw [show Int.ofNat (4 * n + 3 + 1 + 1) = 4 * Int.ofNat n + 5 from rfl] at e1
  calc 2 * sumK LibInt.intR (Ri n ι) (LL n) = (sumK LibInt.intR (Ri n ι) (LL n) + sumK LibInt.intR (Ii n ι) (LL n)) + (sumK LibInt.intR (Ri n ι) (LL n) + -sumK LibInt.intR (Ii n ι) (LL n)) := by lib_int
    _ = 2 * (2 * Int.ofNat n + 1) := by rw [e1, e2]; lib_int

theorem sum_Ii (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK LibInt.intR (Ii n ι) (LL n) = 2 * Int.ofNat n + 2 := by
  apply two_cancel
  have e1 := sum_RpI n hpr ι hi
  have e2 := sum_RmI n hpr ι hi
  rw [show Int.ofNat (4 * n + 3 + 1 + 1) = 4 * Int.ofNat n + 5 from rfl] at e1
  calc 2 * sumK LibInt.intR (Ii n ι) (LL n) = (sumK LibInt.intR (Ri n ι) (LL n) + sumK LibInt.intR (Ii n ι) (LL n)) + -(sumK LibInt.intR (Ri n ι) (LL n) + -sumK LibInt.intR (Ii n ι) (LL n)) := by lib_int
    _ = 2 * (2 * Int.ofNat n + 2) := by rw [e1, e2]; lib_int

/-! ## the fixed point h = 1/2 -/

theorem h_ne (_hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) : h n ≠ (ring (4 * n + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' (4 * n + 3) (by rw [← hh n, e]; lib_ring)

theorem f_h (ι : Zp (4 * n + 3 + 1)) : f n ι (h n) = rho n ι (h n) := by unfold f; rw [om_h]; rfl

theorem rho_real (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) :
    rho n ι x = (1, 0) ∨ rho n ι x = (-1, 0) := by
  unfold rho
  rcases ResidueCriterion.chi4_cases n ι x with e | e | e | e | e
  · exact absurd e (chi4_ne n hpr ι hi x hx)
  · rw [e]; exact Or.inl rfl
  · rw [e]; exact Or.inr rfl
  · rw [e]; exact Or.inl rfl
  · rw [e]; exact Or.inr rfl

/-- **The quartic Jacobi sum mod 4**: J = a + bi with a = Re ρ(1/2) + 2n + 4K and b = 2n + 2 + 4K' (p = 4n + 5). -/
theorem quart_mod4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    ∃ K K' : Int, (Jq n (chi4 n ι)).1 = (rho n ι (h n)).1 + 2 * Int.ofNat n + 4 * K ∧ (Jq n (chi4 n ι)).2 = 2 * Int.ofNat n + 2 + 4 * K' := by
  have hJ : Jq n (chi4 n ι) = sumK (Cyclo4.cyc4 LibInt.intR) (f n ι) (LL n) := rfl
  have hRi : ∀ x, Ri n ι (om n x) = Ri n ι x := fun x => by unfold Ri; rw [f_om]
  have hIi : ∀ x, Ii n ι (om n x) = Ii n ι x := fun x => by unfold Ii; rw [f_om]
  have pa := pair n hpr (fun t => (f n ι t).1) (fun x => by show (f n ι (om n x)).1 = _; rw [f_om])
  have pb := pair n hpr (fun t => (f n ι t).2) (fun x => by show (f n ι (om n x)).2 = _; rw [f_om])
  have pR := pair n hpr (Ri n ι) hRi
  have pI := pair n hpr (Ii n ι) hIi
  have da : sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * (f n ι x).1) (LL n) = sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ri n ι x) (LL n) + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * c1 n ι x) (LL n) := by
    rw [← sI_mul, ← sI_add]; exact sumK_ext LibInt.intR _ _ (fun x => by show _ * (f n ι x).1 = _; rw [dec1 n hpr ι hi]; lib_int) _
  have db : sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * (f n ι x).2) (LL n) = sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ii n ι x) (LL n) + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * c2 n ι x) (LL n) := by
    rw [← sI_mul, ← sI_add]; exact sumK_ext LibInt.intR _ _ (fun x => by show _ * (f n ι x).2 = _; rw [dec2 n hpr ι hi]; lib_int) _
  have hfh : (f n ι (h n)).1 * (f n ι (h n)).1 = 1 ∧ (f n ι (h n)).2 = 0 := by
    rw [f_h]; rcases rho_real n hpr ι hi (h n) (h_ne n hpr) with e | e <;> rw [e] <;> exact ⟨rfl, rfl⟩
  refine ⟨sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * c1 n ι x) (LL n), sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * c2 n ι x) (LL n), ?_, ?_⟩
  · rw [hJ, sum_fst, pa, da, ← f_h]
    have e1 : 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ri n ι x) (LL n) = 2 * Int.ofNat n := by
      have := pR; rw [sum_Ri n hpr ι hi] at this
      have hr : Ri n ι (h n) = 1 := hfh.1
      rw [hr] at this
      calc _ = (1 + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ri n ι x) (LL n)) + -1 := by lib_int
        _ = 2 * Int.ofNat n := by rw [← this]; lib_int
    show (f n ι (h n)).1 + 2 * (sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ri n ι x) (LL n) + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * c1 n ι x) (LL n)) = _
    rw [show ∀ u v w : Int, u + 2 * (v + 2 * w) = u + 2 * v + 4 * w from fun u v w => by lib_int, e1]
  · rw [hJ, sum_snd, pb, db, hfh.2]
    have e1 : 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ii n ι x) (LL n) = 2 * Int.ofNat n + 2 := by
      have := pI; rw [sum_Ii n hpr ι hi] at this
      have hr : Ii n ι (h n) = 0 := by unfold Ii; rw [hfh.2]; rfl
      rw [hr] at this
      calc _ = 0 + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ii n ι x) (LL n) := by lib_int
        _ = 2 * Int.ofNat n + 2 := by rw [← this]
    show 0 + 2 * (sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * Ii n ι x) (LL n) + 2 * sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * c2 n ι x) (LL n)) = _
    rw [show ∀ v w : Int, 0 + 2 * (v + 2 * w) = 2 * v + 4 * w from fun v w => by lib_int, e1]

/-! ## ρ(1/2) = χ(−1) -/

theorem rho_sq (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (y : Zp (4 * n + 3 + 1)) (hy : y ≠ (ring (4 * n + 3 + 1)).zero) : rho n ι ((ring (4 * n + 3 + 1)).mul y y) = (Cyclo4.cyc4 LibInt.intR).one := by
  rw [rho_mul n hpr ι hi]
  rcases rho_real n hpr ι hi y hy with e | e <;> rw [e] <;> rfl

theorem two_eq (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg ι) ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ι) ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ι)) := by
  calc (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one))) ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add ι ((ring (4 * n + 3 + 1)).mul   -- (+75 chars; full line in the Zenodo source)
    _ = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ι ι) ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one))) ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add ι ((ring (4 * n + 3 + 1)).mul ι ((ring (4 * n + 3 + 1)).mul ι ι)))) := by rw [hi]
    _ = _ := by lib_ring

theorem rho_two (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
  have h1 : (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ι ≠ (ring (4 * n + 3 + 1)).zero := fun e => Eisen.two_ne_zero' (4 * n + 3) (by rw [show 4 * n + 3 = (4 * n + 1) + 2 by lib_nat]; exact Nat.le_add_left 2 _) (by
    have ei : ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := by calc ι = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ι) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
                                              _ = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := by rw [e]; lib_ring
    have := hi; rw [ei] at this
    calc (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (ring (4 * n + 3 + 1)).one := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [this]; lib_ring)
  rw [two_eq n ι hi, rho_mul n hpr ι hi, rho_sq n hpr ι hi _ h1, show (ring (4 * n + 3 + 1)).neg ι = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ι by lib_ring, rho_mul n hpr ι hi, rho_neg1 n hpr ι hi]
  show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ι) (chi4 n ι ι))) (Cyclo4.cyc4 LibInt.intR).one = _
  rw [← chi4_mul n hpr ι hi, hi]; lib_ring

theorem rho_h (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : rho n ι (h n) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
  have h2 : (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := Eisen.two_ne_zero' (4 * n + 3) (by rw [show 4 * n + 3 = (4 * n + 1) + 2 by lib_nat]; exact Nat.le_add_left 2 _)
  have e1 : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι (h n)) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← rho_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul (h n) ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).add (h n) (h n) by lib_ring, hh, rho_one n hpr ι hi]
  have e2 : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one)) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← rho_mul n hpr ι hi]; exact rho_sq n hpr ι hi _ h2
  rw [← rho_two n hpr ι hi]
  calc rho n ι (h n) = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι (h n)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one)) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one))) := by rw [e2]; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι (h n)) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one))) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one)) := by lib_ring
    _ = _ := by rw [e1]; lib_ring

/-- **−χ(−1)J is primary**: J = a + bi with a = χ(−1) + 2n + 4K, b = 2n + 2 + 4K' (p = 4n + 5), i.e. a ≡ −1 and b ≡ (p − 1)/2 (mod 4). -/
theorem quart_primary (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    ∃ K K' : Int, (Jq n (chi4 n ι)).1 = (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)).1 + 2 * Int.ofNat n + 4 * K ∧ (Jq n (chi4 n ι)).2 = 2 * Int.ofNat n + 2 + 4 * K' := by
  rw [← rho_h n hpr ι hi]; exact quart_mod4 n hpr ι hi

end QuartPrimary

#print axioms QuartPrimary.om_om
#print axioms QuartPrimary.f_om
#print axioms QuartPrimary.hh
#print axioms QuartPrimary.om_h
#print axioms QuartPrimary.fix_h
#print axioms QuartPrimary.sum_fst
#print axioms QuartPrimary.sum_snd
#print axioms QuartPrimary.fixed_sum
#print axioms QuartPrimary.pair
#print axioms QuartPrimary.sI_add
#print axioms QuartPrimary.sI_mul
#print axioms QuartPrimary.sI_neg
#print axioms QuartPrimary.f_eq
#print axioms QuartPrimary.dec1
#print axioms QuartPrimary.dec2
#print axioms QuartPrimary.RmI
#print axioms QuartPrimary.chi4_ne
#print axioms QuartPrimary.RpI
#print axioms QuartPrimary.sum_one
#print axioms QuartPrimary.sum_delta
#print axioms QuartPrimary.sum_RpI
#print axioms QuartPrimary.J_rho
#print axioms QuartPrimary.sum_RmI
#print axioms QuartPrimary.two_cancel
#print axioms QuartPrimary.sum_Ri
#print axioms QuartPrimary.sum_Ii
#print axioms QuartPrimary.h_ne
#print axioms QuartPrimary.f_h
#print axioms QuartPrimary.rho_real
#print axioms QuartPrimary.quart_mod4
#print axioms QuartPrimary.rho_sq
#print axioms QuartPrimary.two_eq
#print axioms QuartPrimary.rho_two
#print axioms QuartPrimary.rho_h
#print axioms QuartPrimary.quart_primary

#print axioms: 35 theorems, no axioms.

78.64 Duplication for the quartic Jacobi sum

Counting square roots and substituting \(4t(1-t)=1-(2t-1)^2\).

\[ J(\chi ,\chi )=\chi (-1)\sum _t\rho (t)\chi (1-t). \]

lang/QuartDup.lean

/-
  lang/QuartDup — T43, cycle 231: the duplication formula J(χ, χ) = χ(−1) J(ρ, χ) for the quartic character.

  p = 4n + 5, χ quartic, ρ = χ². #{x : x² = a} = 1 + ρ(a) (sq_root, two_roots, sq_count); with 4t(1 − t) = 1 − (2t − 1)² and the
  bijection t ↦ 2t − 1:  χ(4) J(χ, χ) = Σ_s χ(1 − s²) = Σ_v (1 + ρ(v)) χ(1 − v) = J(ρ, χ), and χ(4) = ρ(2) = χ(−1) = ±1:
      quart_dup    J(χ, χ) = χ(−1) · Σ_t ρ(t) χ(1 − t)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.QuartPrimary
import lang.HeckePrime

namespace QuartDup

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary
open LibRing (castZ castN)

variable (n : Nat)

/-- a^{(p−1)/2} = 1, a ≠ 0 ⇒ a is a square. -/
theorem sq_root (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (a : Zp (4 * n + 3 + 1)) (ha0 : a ≠ (ring (4 * n + 3 + 1)).zero) (ha : pw (ring (4 * n + 3 + 1)) a (2 * n + 2) = (ring (4 * n + 3 + 1)).one) : ∃ c, (ring (4 * n + 3 + 1)).mul c c = a := by
  match PrimRoot.primitive_root (4 * n + 3) hpr with
  | ⟨g, hg⟩ =>
    match DLog.mem_pows (4 * n + 3) g a _ (DLog.every_unit (4 * n + 3) hpr g hg a ha0) with
    | ⟨k, _, hk⟩ =>
      have h1 : pw (ring (4 * n + 3 + 1)) g (k * (2 * n + 2)) = (ring (4 * n + 3 + 1)).one := by rw [← TwoPower.pwpow, ← hk, ha]
      match hg.2 _ h1 with
      | ⟨c, hc⟩ =>
        have h3 : k = 2 * c := by
          apply Nat.eq_of_mul_eq_mul_right (Nat.zero_lt_succ (2 * n + 1))
          rw [hc]; lib_nat
        refine ⟨pw (ring (4 * n + 3 + 1)) g c, ?_⟩
        rw [hk, h3, show 2 * c = c * 2 by lib_nat, ← TwoPower.pwpow]
        show _ = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one _) _
        lib_ring

def mtwo : Zp (4 * n + 3 + 1) := (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one

theorem mtwo_ne : mtwo n ≠ (ring (4 * n + 3 + 1)).zero := Eisen.two_ne_zero' (4 * n + 3) (by rw [show 4 * n + 3 = (4 * n + 1) + 2 by lib_nat]; exact Nat.le_add_left 2 _)

/-- the two square roots of c². -/
theorem two_roots (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) (x : Zp (4 * n + 3 + 1)) :
    delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).mul x x) ((ring (4 * n + 3 + 1)).mul c c) = (Cyclo4.cyc4 LibInt.intR).add (delta (Cyclo4.cyc4 LibInt.intR) x c) (delta (Cyclo4.cyc4 LibInt.intR) x ((ring (4 * n + 3 + 1)).neg c)) := by
  have d : c ≠ (ring (4 * n + 3 + 1)).neg c := fun e => hc (by
    have e2 : (ring (4 * n + 3 + 1)).mul (mtwo n) c = (ring (4 * n + 3 + 1)).zero := by
      calc (ring (4 * n + 3 + 1)).mul (mtwo n) c = (ring (4 * n + 3 + 1)).add c c := by unfold mtwo; lib_ring
        _ = (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg c) := congrArg ((ring (4 * n + 3 + 1)).add c) e
        _ = (ring (4 * n + 3 + 1)).zero := by lib_ring
    cases nozd (4 * n + 3) hpr _ _ e2 with
    | inl h => exact absurd h (mtwo_ne n)
    | inr h => exact h)
  unfold delta
  by_cases hx : (ring (4 * n + 3 + 1)).mul x x = (ring (4 * n + 3 + 1)).mul c c
  · rw [ite_pos' _ _ hx]
    have e : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg c)) ((ring (4 * n + 3 + 1)).add x c) = (ring (4 * n + 3 + 1)).zero := by
      rw [show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg c)) ((ring (4 * n + 3 + 1)).add x c) = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul x x) ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c c)) by lib_ring, hx]; lib_ring
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h =>
      have ex : x = c := by calc x = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg c)) c := by lib_ring
                                _ = c := by rw [h]; lib_ring
      rw [ite_pos' _ _ ex, ite_neg' _ _ (by rw [ex]; exact d)]; lib_ring
    | inr h =>
      have ex : x = (ring (4 * n + 3 + 1)).neg c := by calc x = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add x c) ((ring (4 * n + 3 + 1)).neg c) := by lib_ring
                                      _ = (ring (4 * n + 3 + 1)).neg c := by rw [h]; lib_ring
      rw [ite_neg' _ _ (by rw [ex]; exact fun e => d e.symm), ite_pos' _ _ ex]; lib_ring
  · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by rw [e])), ite_neg' _ _ (fun e => hx (by rw [e]; lib_ring))]; lib_ring

theorem chi4_one_pw (ι x : Zp (4 * n + 3 + 1)) (h : chi4 n ι x = (1, 0)) : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).one := by
  unfold chi4 at h
  by_cases h0 : x = (ring (4 * n + 3 + 1)).zero
  · rw [ite_pos' _ _ h0] at h; exact absurd (congrArg Prod.fst h) (fun e => Nat.noConfusion (Int.ofNat.inj e))
  · rw [ite_neg' _ _ h0] at h
    by_cases h1 : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).one
    · exact h1
    · rw [ite_neg' _ _ h1] at h
      by_cases h2 : pw (ring (4 * n + 3 + 1)) x (n + 1) = ι
      · rw [ite_pos' _ _ h2] at h; exact absurd (congrArg Prod.fst h) (fun e => Nat.noConfusion (Int.ofNat.inj e))
      · rw [ite_neg' _ _ h2] at h
        by_cases h3 : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one
        · rw [ite_pos' _ _ h3] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)
        · rw [ite_neg' _ _ h3] at h; exact absurd (congrArg Prod.fst h) (fun e => Nat.noConfusion (Int.ofNat.inj e))

theorem chi4_m1_pw (ι x : Zp (4 * n + 3 + 1)) (h : chi4 n ι x = (-1, 0)) : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := by
  unfold chi4 at h
  by_cases h0 : x = (ring (4 * n + 3 + 1)).zero
  · rw [ite_pos' _ _ h0] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)
  · rw [ite_neg' _ _ h0] at h
    by_cases h1 : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).one
    · rw [ite_pos' _ _ h1] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)
    · rw [ite_neg' _ _ h1] at h
      by_cases h2 : pw (ring (4 * n + 3 + 1)) x (n + 1) = ι
      · rw [ite_pos' _ _ h2] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)
      · rw [ite_neg' _ _ h2] at h
        by_cases h3 : pw (ring (4 * n + 3 + 1)) x (n + 1) = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one
        · exact h3
        · rw [ite_neg' _ _ h3] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)

/-- ρ(a) = 1 ⇒ a^{(p−1)/2} = 1. -/
theorem rho_one_pw (_hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (_hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) (h : rho n ι a = (1, 0)) : pw (ring (4 * n + 3 + 1)) a (2 * n + 2) = (ring (4 * n + 3 + 1)).one := by
  rw [show 2 * n + 2 = (n + 1) * 2 by lib_nat, ← TwoPower.pwpow]
  unfold rho at h
  rcases ResidueCriterion.chi4_cases n ι a with e | e | e | e | e
  · rw [e] at h; exact absurd (congrArg Prod.fst h) (fun e => Nat.noConfusion (Int.ofNat.inj e))
  · rw [chi4_one_pw n ι a e]; show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) (ring (4 * n + 3 + 1)).one = _; lib_ring
  · rw [e] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)
  · rw [chi4_m1_pw n ι a e]; show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = _; lib_ring
  · rw [e] at h; exact absurd (congrArg Prod.fst h) (fun e => Int.noConfusion e)

/-- **#{x : x² = a} = 1 + ρ(a).** -/
theorem sq_count (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).mul x x) a) (LL n) = (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one (rho n ι a) := by
  by_cases ha : a = (ring (4 * n + 3 + 1)).zero
  · rw [ha, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun x => delta (Cyclo4.cyc4 LibInt.intR) x (ring (4 * n + 3 + 1)).zero) (fun x => by
      unfold delta
      by_cases hx : x = (ring (4 * n + 3 + 1)).zero
      · rw [ite_pos' _ _ hx, ite_pos' _ _ (by rw [hx]; lib_ring)]
      · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by
          cases nozd (4 * n + 3) hpr _ _ e with
          | inl h => exact h
          | inr h => exact h))]), show sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) x (ring (4 * n + 3 + 1)).zero) (LL n) = (Cyclo4.cyc4 LibInt.intR).one from CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) (ring (4 * n + 3 + 1)).zero, rho_zero]; lib_ring
  · rcases QuartPrimary.rho_real n hpr ι hi a ha with e | e
    · match sq_root n hpr a ha (rho_one_pw n hpr ι hi a e) with
      | ⟨c, hc⟩ =>
        have hc0 : c ≠ (ring (4 * n + 3 + 1)).zero := fun e0 => ha (by rw [← hc, e0]; lib_ring)
        rw [← hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => two_roots n hpr c hc0 x), sumK_add, show sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) x c) (LL n) = (Cyclo4.cyc4 LibInt.intR).one from CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) c,
          show sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) x ((ring (4 * n + 3 + 1)).neg c)) (LL n) = (Cyclo4.cyc4 LibInt.intR).one from CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).neg c), hc, e]; rfl
    · have hz : ∀ x, delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).mul x x) a = (Cyclo4.cyc4 LibInt.intR).zero := fun x => by
        unfold delta
        rw [ite_neg' _ _ (fun ex => by
          have hx0 : x ≠ (ring (4 * n + 3 + 1)).zero := fun e0 => ha (by rw [← ex, e0]; lib_ring)
          have h1 := QuartPrimary.rho_sq n hpr ι hi x hx0
          rw [ex, e] at h1
          exact absurd (congrArg Prod.fst h1) (fun e' => Int.noConfusion e'))]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hz, HeckePrime.zsumR (Cyclo4.cyc4 LibInt.intR), e]; rfl

/-- J(ρ, χ) = Σ_t ρ(t) χ(1 − t). -/
def J2 (ι : Zp (4 * n + 3 + 1)) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun t => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chi4 n ι (om n t))) (LL n)

def sh (t : Zp (4 * n + 3 + 1)) : Zp (4 * n + 3 + 1) := (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add t t) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)
def shi (u : Zp (4 * n + 3 + 1)) : Zp (4 * n + 3 + 1) := (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add u (ring (4 * n + 3 + 1)).one) (h n)

theorem sh_shi (u : Zp (4 * n + 3 + 1)) : sh n (shi n u) = u := by
  unfold sh shi
  calc _ = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add u (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).add (h n) (h n))) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
    _ = u := by rw [hh]; lib_ring

theorem shi_sh (t : Zp (4 * n + 3 + 1)) : shi n (sh n t) = t := by
  unfold sh shi
  calc _ = (ring (4 * n + 3 + 1)).mul t ((ring (4 * n + 3 + 1)).add (h n) (h n)) := by lib_ring
    _ = t := by rw [hh]; lib_ring

theorem four_id (t : Zp (4 * n + 3 + 1)) : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) ((ring (4 * n + 3 + 1)).mul t (om n t)) = om n ((ring (4 * n + 3 + 1)).mul (sh n t) (sh n t)) := by
  unfold mtwo om sh; lib_ring

/-- **Duplication**: J(χ, χ) = χ(−1) J(ρ, χ). -/
theorem quart_dup (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    Jq n (chi4 n ι) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (J2 n ι) := by
  -- χ(4) J = Σ_s χ(1 − s²)
  have eA : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n))) (Jq n (chi4 n ι)) = sumK (Cyclo4.cyc4 LibInt.intR) (fun s => chi4 n ι (om n ((ring (4 * n + 3 + 1)).mul s s))) (LL n) := by
    rw [show Jq n (chi4 n ι) = sumK (Cyclo4.cyc4 LibInt.intR) (f n ι) (LL n) from rfl, mul_sumK]
    rw [← FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (sh n) (shi n) (shi_sh n) (sh_shi n) (fun s => chi4 n ι (om n ((ring (4 * n + 3 + 1)).mul s s)))]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by rw [f_eq n hpr ι hi, ← chi4_mul n hpr ι hi, four_id]) _
  -- Σ_s χ(1 − s²) = Σ_v (1 + ρ(v)) χ(1 − v)
  have eB : sumK (Cyclo4.cyc4 LibInt.intR) (fun s => chi4 n ι (om n ((ring (4 * n + 3 + 1)).mul s s))) (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun v => (Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (fun s => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).mul s s) v) (LL n)) (chi4 n ι (om n v))) (LL n) := by
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun s => sumK (Cyclo4.cyc4 LibInt.intR) (fun v => (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) v ((ring (4 * n + 3 + 1)).mul s s)) (chi4 n ι (om n v))) (LL n))
      (fun s => (FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (fun v => chi4 n ι (om n v)) _).symm), sumK_swap]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun v => by
      rw [sumK_mul]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun s => by rw [delta_symm]) _) _
  have eC : sumK (Cyclo4.cyc4 LibInt.intR) (fun v => (Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (fun s => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).mul s s) v) (LL n)) (chi4 n ι (om n v))) (LL n) = J2 n ι := by
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun v => (Cyclo4.cyc4 LibInt.intR).add (chi4 n ι (om n v)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι v) (chi4 n ι (om n v)))) (fun v => by rw [sq_count n hpr ι hi]; lib_ring), sumK_add]
    have e0 : sumK (Cyclo4.cyc4 LibInt.intR) (fun v => chi4 n ι (om n v)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
      rw [FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (om n) (om n) (om_om n) (om_om n) (chi4 n ι)]
      exact chi4_sum n hpr ι hi
    rw [e0]; unfold J2; lib_ring
  have e4 : chi4 n ι ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
    rw [chi4_mul n hpr ι hi]; exact QuartPrimary.rho_two n hpr ι hi
  have hs : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring]; exact chi_one n hpr ι hi
  rw [e4, eB, eC] at eA
  calc Jq n (chi4 n ι) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (Jq n (chi4 n ι)) := by rw [hs]; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι))) := by lib_ring
    _ = _ := by rw [eA]

end QuartDup

#print axioms QuartDup.sq_root
#print axioms QuartDup.mtwo_ne
#print axioms QuartDup.two_roots
#print axioms QuartDup.chi4_one_pw
#print axioms QuartDup.chi4_m1_pw
#print axioms QuartDup.rho_one_pw
#print axioms QuartDup.sq_count
#print axioms QuartDup.sh_shi
#print axioms QuartDup.shi_sh
#print axioms QuartDup.four_id
#print axioms QuartDup.quart_dup

#print axioms: 11 theorems, no axioms.

78.65 Points on the Fermat quartic

Fourth roots counted by \(1+\chi +\rho +\bar \chi \) and the sixteen Jacobi sums evaluated.

\[ \#\{x^4+y^4=1\}=p-1-2\chi (-1)+(2+4\chi (-1))\,a . \]

lang/FermatQuartic.lean

/-
  lang/FermatQuartic — T43, cycle 232: points on the Fermat quartic x⁴ + y⁴ = 1 over 𝔽_p, p ≡ 1 (mod 4).

  N(a) = #{x : x⁴ = a} = 1 + χ(a) + ρ(a) + χ̄(a) (fourth roots c, cι, −c, −cι), and Σ_{a+b=1} N(a)N(b) is a sum of Jacobi sums:
  J(χ,χ) + J(χ̄,χ̄) + J(ρ,ρ) + 2J(χ,χ̄) + 2J(ρ,χ) + 2J(ρ,χ̄), with J(ρ,ρ) = −1, J(χ,χ̄) = −χ(−1) and (QuartDup) J(ρ,χ) = χ(−1)J:
      fermat_quartic   #{x⁴ + y⁴ = 1} = p − 1 − 2χ(−1) + (2 + 4χ(−1)) a,   J = a + bi
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.QuartDup

namespace FermatQuartic

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup
open LibRing (castZ castN)

variable (n : Nat)

def q4 (x : Zp (4 * n + 3 + 1)) : Zp (4 * n + 3 + 1) := (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul x x) ((ring (4 * n + 3 + 1)).mul x x)

theorem q4_mul (x y : Zp (4 * n + 3 + 1)) : q4 n ((ring (4 * n + 3 + 1)).mul x y) = (ring (4 * n + 3 + 1)).mul (q4 n x) (q4 n y) := by unfold q4; lib_ring

/-- a^{(p−1)/4} = 1, a ≠ 0 ⇒ a is a fourth power. -/
theorem q4_root (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (a : Zp (4 * n + 3 + 1)) (ha0 : a ≠ (ring (4 * n + 3 + 1)).zero) (ha : pw (ring (4 * n + 3 + 1)) a (n + 1) = (ring (4 * n + 3 + 1)).one) : ∃ c, q4 n c = a := by
  match PrimRoot.primitive_root (4 * n + 3) hpr with
  | ⟨g, hg⟩ =>
    match DLog.mem_pows (4 * n + 3) g a _ (DLog.every_unit (4 * n + 3) hpr g hg a ha0) with
    | ⟨k, _, hk⟩ =>
      have h1 : pw (ring (4 * n + 3 + 1)) g (k * (n + 1)) = (ring (4 * n + 3 + 1)).one := by rw [← TwoPower.pwpow, ← hk, ha]
      match hg.2 _ h1 with
      | ⟨c, hc⟩ =>
        have h3 : k = 4 * c := by
          apply Nat.eq_of_mul_eq_mul_right (Nat.zero_lt_succ n)
          rw [hc]; lib_nat
        refine ⟨pw (ring (4 * n + 3 + 1)) g c, ?_⟩
        rw [hk, h3, show 4 * c = c * 4 by lib_nat, ← TwoPower.pwpow]
        show _ = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one _) _) _) _
        unfold q4; lib_ring

theorem cancelK (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (a b : Zp (4 * n + 3 + 1)) (ha : a ≠ (ring (4 * n + 3 + 1)).zero) (h : (ring (4 * n + 3 + 1)).mul a b = (ring (4 * n + 3 + 1)).zero) : b = (ring (4 * n + 3 + 1)).zero := by
  cases nozd (4 * n + 3) hpr _ _ h with
  | inl e => exact absurd e ha
  | inr e => exact e

theorem eq_of_sub' (x y : Zp (4 * n + 3 + 1)) (h : (ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg y) = (ring (4 * n + 3 + 1)).zero) : x = y := by
  calc x = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg y)) y := by lib_ring
    _ = y := by rw [h]; lib_ring

/-- the four fourth roots of c⁴. -/
theorem four_roots (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) (x : Zp (4 * n + 3 + 1)) :
    delta (Cyclo4.cyc4 LibInt.intR) (q4 n x) (q4 n c) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (delta (Cyclo4.cyc4 LibInt.intR) x c) (delta (Cyclo4.cyc4 LibInt.intR) x ((ring (4 * n + 3 + 1)).mul c ι))) (delta (Cyclo4.cyc4 LibInt.intR) x ((ring (4 * n + 3 + 1)).neg c))) (delta (Cyclo4.cyc4 LibInt.intR) x ((ring (4 * n + 3 + 1)).neg ((ring (4 * n  -- (+27 chars; full line in the Zenodo source)
  have two : (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := mtwo_ne n
  have hi0 : ι ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by rw [← hi, e]; lib_ring
    exact PowerSum.one_ne_zero' (4 * n + 3) (by calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
                                                  _ = (ring (4 * n + 3 + 1)).zero := by rw [this]; lib_ring)
  -- distinctness: u ≠ v whenever u − v = c·(unit)
  have dist : ∀ u : Zp (4 * n + 3 + 1), u ≠ (ring (4 * n + 3 + 1)).zero → ∀ y z : Zp (4 * n + 3 + 1), (ring (4 * n + 3 + 1)).add y ((ring (4 * n + 3 + 1)).neg z) = (ring (4 * n + 3 + 1)).mul c u → y ≠ z := fun u hu y z e eq => by
    rw [eq, show (ring (4 * n + 3 + 1)).add z ((ring (4 * n + 3 + 1)).neg z) = (ring (4 * n + 3 + 1)).zero by lib_ring] at e
    exact hu (cancelK n hpr c u hc e.symm)
  have u1 : (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg ι) ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : ι = (ring (4 * n + 3 + 1)).one := (eq_of_sub' n _ _ e).symm
    rw [this] at hi
    exact two (by calc (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) (ring (4 * n + 3 + 1)).one := by lib_ring
                  _ = (ring (4 * n + 3 + 1)).zero := by rw [hi]; lib_ring)
  have u2 : (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ι ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := by calc ι = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ι) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
                                          _ = _ := by rw [e]; lib_ring
    rw [this] at hi
    exact two (by calc (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (ring (4 * n + 3 + 1)).one := by lib_ring
                  _ = (ring (4 * n + 3 + 1)).zero := by rw [hi]; lib_ring)
  have d12 : c ≠ (ring (4 * n + 3 + 1)).mul c ι := dist _ u1 _ _ (by lib_ring)
  have d13 : c ≠ (ring (4 * n + 3 + 1)).neg c := dist _ two _ _ (by lib_ring)
  have d14 : c ≠ (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι) := dist _ u2 _ _ (by lib_ring)
  have d23 : (ring (4 * n + 3 + 1)).mul c ι ≠ (ring (4 * n + 3 + 1)).neg c := dist _ u2 _ _ (by lib_ring)
  have d24 : (ring (4 * n + 3 + 1)).mul c ι ≠ (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι) := dist ((ring (4 * n + 3 + 1)).add ι ι) (fun e => hi0 (cancelK n hpr _ ι two (by rw [show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one) ι = (ring (4 * n + 3 + 1)).add ι ι by lib_ring]; exact e))) _ _ (by lib_ring)
  have u3 : (ring (4 * n + 3 + 1)).add ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ≠ (ring (4 * n + 3 + 1)).zero := fun (e : (ring (4 * n + 3 + 1)).add ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).zero) => u1 (by
    calc (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg ι) = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  have d34 : (ring (4 * n + 3 + 1)).neg c ≠ (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι) := dist _ u3 _ _ (by lib_ring)
  have r2 : q4 n ((ring (4 * n + 3 + 1)).mul c ι) = q4 n c := by
    unfold q4; rw [show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul c ι) ((ring (4 * n + 3 + 1)).mul c ι)) ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul c ι) ((ring (4 * n + 3 + 1)).mul c ι)) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul c c) ((ring (4 * n + 3 + 1)).mul c c)) ((ring (4 * n + 3 + 1)).mul ((ring   -- (+86 chars; full line in the Zenodo source)
  have r3 : q4 n ((ring (4 * n + 3 + 1)).neg c) = q4 n c := by unfold q4; lib_ring
  have r4 : q4 n ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι)) = q4 n c := by
    rw [show q4 n ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι)) = q4 n ((ring (4 * n + 3 + 1)).mul c ι) by unfold q4; lib_ring, r2]
  unfold delta
  by_cases hx : q4 n x = q4 n c
  · rw [ite_pos' _ _ hx]
    -- (x − c)(x + c)(x − cι)(x + cι) = x⁴ − c⁴ = 0
    have e : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg c)) ((ring (4 * n + 3 + 1)).add x c)) ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι))) ((ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).mul c ι))) = (ring (4 * n + 3 + 1)).zero := by
      calc _ = (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add (q4 n x) ((ring (4 * n + 3 + 1)).neg (q4 n c))) ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul c c) ((ring (4 * n + 3 + 1)).mul c c)) ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul x x) ((ring (4 * n + 3 + 1)).mul c c)))) ((ring  -- (+109 chars; full line in the Zenodo source)
        _ = (ring (4 * n + 3 + 1)).zero := by rw [hx, hi]; lib_ring
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl e1 =>
      cases nozd (4 * n + 3) hpr _ _ e1 with
      | inl e2 =>
        have ex := eq_of_sub' n _ _ e2
        rw [ite_pos' _ _ ex, ite_neg' _ _ (by rw [ex]; exact d12), ite_neg' _ _ (by rw [ex]; exact d13), ite_neg' _ _ (by rw [ex]; exact d14)]; lib_ring
      | inr e2 =>
        have ex : x = (ring (4 * n + 3 + 1)).neg c := eq_of_sub' n _ _ (by rw [show (ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg c)) = (ring (4 * n + 3 + 1)).add x c by lib_ring]; exact e2)
        rw [ite_neg' _ _ (by rw [ex]; exact fun e => d13 e.symm), ite_neg' _ _ (by rw [ex]; exact fun e => d23 e.symm), ite_pos' _ _ ex, ite_neg' _ _ (by rw [ex]; exact d34)]; lib_ring
    | inr e1 =>
      cases nozd (4 * n + 3) hpr _ _ e1 with
      | inl e2 =>
        have ex := eq_of_sub' n _ _ e2
        rw [ite_neg' _ _ (by rw [ex]; exact fun e => d12 e.symm), ite_pos' _ _ ex, ite_neg' _ _ (by rw [ex]; exact d23), ite_neg' _ _ (by rw [ex]; exact d24)]; lib_ring
      | inr e2 =>
        have ex : x = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι) := eq_of_sub' n _ _ (by rw [show (ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι))) = (ring (4 * n + 3 + 1)).add x ((ring (4 * n + 3 + 1)).mul c ι) by lib_ring]; exact e2)
        rw [ite_neg' _ _ (by rw [ex]; exact fun e => d14 e.symm), ite_neg' _ _ (by rw [ex]; exact fun e => d24 e.symm), ite_neg' _ _ (by rw [ex]; exact fun e => d34 e.symm), ite_pos' _ _ ex]; lib_ring
  · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by rw [e])), ite_neg' _ _ (fun e => hx (by rw [e, r2])),
      ite_neg' _ _ (fun e => hx (by rw [e, r3])), ite_neg' _ _ (fun e => hx (by rw [e, r4]))]; lib_ring

/-! ## N(a) = 1 + χ(a) + ρ(a) + χ̄(a) -/

def g4 (ι a : Zp (4 * n + 3 + 1)) : Int × Int := (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one (chi4 n ι a)) (rho n ι a)) (chib n ι a)

theorem chib_eq (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) : chib n ι a = gconj (chi4 n ι a) := by
  unfold chib; exact chi_ivq n hpr ι hi a

theorem chi_q4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) : chi4 n ι (q4 n x) = (1, 0) := by
  unfold q4; rw [chi4_mul n hpr ι hi, chi4_mul n hpr ι hi]
  rcases ResidueCriterion.chi4_cases n ι x with e | e | e | e | e
  · exact absurd e (chi4_ne n hpr ι hi x hx)
  all_goals (rw [e]; rfl)

theorem q4_zero (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (x : Zp (4 * n + 3 + 1)) (h : q4 n x = (ring (4 * n + 3 + 1)).zero) : x = (ring (4 * n + 3 + 1)).zero := by
  unfold q4 at h
  cases nozd (4 * n + 3) hpr _ _ h with
  | inl e => cases nozd (4 * n + 3) hpr _ _ e with
    | inl e' => exact e'
    | inr e' => exact e'
  | inr e => cases nozd (4 * n + 3) hpr _ _ e with
    | inl e' => exact e'
    | inr e' => exact e'

theorem q4_count (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) (q4 n x) a) (LL n) = g4 n ι a := by
  unfold g4
  by_cases ha : a = (ring (4 * n + 3 + 1)).zero
  · rw [ha, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun x => delta (Cyclo4.cyc4 LibInt.intR) x (ring (4 * n + 3 + 1)).zero) (fun x => by
      unfold delta
      by_cases hx : x = (ring (4 * n + 3 + 1)).zero
      · rw [ite_pos' _ _ hx, ite_pos' _ _ (by rw [hx]; unfold q4; lib_ring)]
      · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (q4_zero n hpr x e))]),
      show sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) x (ring (4 * n + 3 + 1)).zero) (LL n) = (Cyclo4.cyc4 LibInt.intR).one from CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) (ring (4 * n + 3 + 1)).zero,
      chi4_zero, rho_zero, chib_zero]; rfl
  · rw [chib_eq n hpr ι hi]; unfold rho
    rcases ResidueCriterion.chi4_cases n ι a with e | e | e | e | e
    · exact absurd e (chi4_ne n hpr ι hi a ha)
    · match q4_root n hpr a ha (QuartDup.chi4_one_pw n ι a e) with
      | ⟨c, hc⟩ =>
        have hc0 : c ≠ (ring (4 * n + 3 + 1)).zero := fun e0 => ha (by rw [← hc, e0]; unfold q4; lib_ring)
        have hcomp : ∀ z, sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) x z) (LL n) = (Cyclo4.cyc4 LibInt.intR).one := fun z => CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) z
        rw [← hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => four_roots n hpr ι hi c hc0 x), sumK_add, sumK_add, sumK_add,
          hcomp c, hcomp ((ring (4 * n + 3 + 1)).mul c ι), hcomp ((ring (4 * n + 3 + 1)).neg c), hcomp ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c ι)), hc, e]; rfl
    all_goals
      have hz : ∀ x, delta (Cyclo4.cyc4 LibInt.intR) (q4 n x) a = (Cyclo4.cyc4 LibInt.intR).zero := fun x => by
        unfold delta
        rw [ite_neg' _ _ (fun ex => by
          have hx0 : x ≠ (ring (4 * n + 3 + 1)).zero := fun e0 => ha (by rw [← ex, e0]; unfold q4; lib_ring)
          have h1 := chi_q4 n hpr ι hi x hx0
          rw [ex, e] at h1
          first
          | exact absurd (congrArg Prod.fst h1) (fun e' => Nat.noConfusion (Int.ofNat.inj e'))
          | exact absurd (congrArg Prod.fst h1) (fun e' => Int.noConfusion e'))]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hz, HeckePrime.zsumR (Cyclo4.cyc4 LibInt.intR), e]; rfl

/-! ## the double sum -/

def Fq : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun x => sumK (Cyclo4.cyc4 LibInt.intR) (fun y => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).add (q4 n x) (q4 n y)) (ring (4 * n + 3 + 1)).one) (LL n)) (LL n)

theorem step1 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun y => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).add (q4 n x) (q4 n y)) (ring (4 * n + 3 + 1)).one) (LL n) = g4 n ι (om n (q4 n x)) := by
  rw [← q4_count n hpr ι hi]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun y => by
    unfold delta om
    by_cases h : (ring (4 * n + 3 + 1)).add (q4 n x) (q4 n y) = (ring (4 * n + 3 + 1)).one
    · rw [ite_pos' _ _ h, ite_pos' _ _ (by rw [← h]; lib_ring)]
    · rw [ite_neg' _ _ h, ite_neg' _ _ (fun e => h (by rw [e]; lib_ring))]) _

theorem step2 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hh : Zp (4 * n + 3 + 1) → Int × Int) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun x => hh (q4 n x)) (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (hh a)) (LL n) := by
  have e1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun x => hh (q4 n x)) (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun x => sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (q4 n x)) (hh a)) (LL n)) (LL n) :=
    sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => (FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) _ (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) hh (q4 n x)).symm) _
  rw [e1, sumK_swap]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by
    rw [← sumK_mul, ← q4_count n hpr ι hi a]
    exact congrArg (fun z => (Cyclo4.cyc4 LibInt.intR).mul z (hh a)) (sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => delta_symm (Cyclo4.cyc4 LibInt.intR) a (q4 n x)) _)) _

theorem refl_om (G : Zp (4 * n + 3 + 1) → Int × Int) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => G (om n a)) (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) G (LL n) :=
  FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (om n) (om n) (om_om n) (om_om n) G

theorem Fq_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    Fq n = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (g4 n ι (om n a))) (LL n) := by
  unfold Fq
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => step1 n hpr ι hi x)]
  exact step2 n hpr ι hi (fun a => g4 n ι (om n a))

/-! ## the sixteen character sums -/

theorem m1_real (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (1, 0) ∨ chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (-1, 0) := by
  have hs : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring]; exact chi_one n hpr ι hi
  rcases ResidueCriterion.chi4_cases n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) with e | e | e | e | e
  · rw [e] at hs; exact absurd (congrArg Prod.fst hs) (fun e' => Nat.noConfusion (Int.ofNat.inj e'))
  · exact Or.inl e
  · rw [e] at hs; exact absurd (congrArg Prod.fst hs) (fun e' => Int.noConfusion e')
  · exact Or.inr e
  · rw [e] at hs; exact absurd (congrArg Prod.fst hs) (fun e' => Int.noConfusion e')

theorem gconj_rho (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) : gconj (rho n ι x) = rho n ι x := by
  by_cases hx : x = (ring (4 * n + 3 + 1)).zero
  · rw [hx, rho_zero]; rfl
  · rcases QuartPrimary.rho_real n hpr ι hi x hx with e | e <;> rw [e] <;> rfl

/-- J(χ, χ̄) = −χ(−1). -/
theorem T13 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := by
  rw [← FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (fun b => (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one b) (fun b => (ring (4 * n + 3 + 1)).add b ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (fun x => by lib_ring) (fun y => by lib_ring)]
  rw [← D_ne' n hpr (chi4 n ι) (chi4_mul n hpr ι hi) (chi_one n hpr ι hi) (chi4_zero n ι) (chi4_sum n hpr ι hi) (ring (4 * n + 3 + 1)).one (fun e => PowerSum.one_ne_zero' (4 * n + 3) e), mul_sumK]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun b => by
    show (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one b)) (chib n ι (om n ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one b))) = _
    have hreal : gconj (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
      rcases m1_real n hpr ι hi with e | e <;> rw [e] <;> rfl
    rw [show om n ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one b) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) b by unfold om; lib_ring, chib_mul n hpr ι hi,
      chib_eq n hpr ι hi ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one), hreal]
    unfold chib; lib_ring) _

/-- **Fq as a combination of Jacobi sums.** -/
theorem Fq_C (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    Fq n = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) (Jq n (chi4 n ι))) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one))
      ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (J2 n ι) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).add (gconj (J2 n ι)) (gconj (J2 n ι))))) := by
  rw [Fq_sum n hpr ι hi]
  have hexp : ∀ a, (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (g4 n ι (om n a)) =
      (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chi4 n ι (om n a)) (rho n ι (om n a))) (chib n ι (om n a))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chi4 n ι a) (rho n ι a)) (chib n ι a)))
        ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι (om n a))))
        ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι (om n a))))
        ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι (om n a))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a)))) := fun a => by unfold g4; lib_ring
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hexp]
  repeat rw [sumK_add]
  have S1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := sum_oneq n _
  have SA : sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chi4_sum n hpr ι hi
  have SR : sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := rho_sum n hpr ι hi
  have SB : sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chib_sum n hpr ι hi
  have SA' : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => chi4 n ι (om n a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := (refl_om n _).trans SA
  have SR' : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => rho n ι (om n a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := (refl_om n _).trans SR
  have SB' : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => chib n ι (om n a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := (refl_om n _).trans SB
  have SAA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι (om n a))) (LL n) = Jq n (chi4 n ι) := rfl
  have SRR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one := J_rho n hpr ι hi
  have SBB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a))) (LL n) = gconj (Jq n (chi4 n ι)) := Jb_conj n hpr ι hi
  have SAB := T13 n hpr ι hi
  have SBA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := by
    rw [← SAB, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have SRA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι (om n a))) (LL n) = J2 n ι := rfl
  have SAR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a))) (LL n) = J2 n ι := by
    rw [← SRA, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have SRB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a))) (LL n) = gconj (J2 n ι) := by
    unfold J2; rw [gconj_sum]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [gconj_mul, gconj_rho n hpr ι hi, chib_eq n hpr ι hi]) _
  have SBR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a))) (LL n) = gconj (J2 n ι) := by
    rw [← SRB, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  rw [S1, SA, SR, SB, SA', SR', SB', SAA, SRR, SBB, SAB, SBA, SAR, SRA, SRB, SBR]
  lib_ring

/-- **Points on the Fermat quartic**: #{x⁴ + y⁴ = 1} = p − 1 − 2s + (2 + 4s) a, s = χ(−1), J = a + bi. -/
theorem fermat_quartic (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    (Fq n).1 = Int.ofNat (4 * n + 3 + 1 + 1) - 1 - 2 * (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)).1 + (2 + 4 * (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)).1) * (Jq n (chi4 n ι)).1 := by
  have hd := QuartDup.quart_dup n hpr ι hi
  have hs : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring]; exact chi_one n hpr ι hi
  have hJ2 : J2 n ι = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)) := by
    rw [hd]; calc J2 n ι = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (J2 n ι) := by rw [hs]; lib_ring
      _ = _ := by lib_ring
  rw [Fq_C n hpr ι hi, hJ2]
  generalize Jq n (chi4 n ι) = Jv
  have fa : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).add u v).1 = u.1 + v.1 := fun _ _ => rfl
  have fm : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).mul u v).1 = u.1 * v.1 + -(u.2 * v.2) := fun _ _ => rfl
  have fg : ∀ u : Int × Int, (gconj u).1 = u.1 := fun _ => rfl
  have fn : ∀ u : Int × Int, ((Cyclo4.cyc4 LibInt.intR).neg u).1 = -u.1 := fun _ => rfl
  have fn2 : ∀ u : Int × Int, ((Cyclo4.cyc4 LibInt.intR).neg u).2 = -u.2 := fun _ => rfl
  have o1 : ((Cyclo4.cyc4 LibInt.intR).one).1 = 1 := rfl
  have o2 : ((Cyclo4.cyc4 LibInt.intR).one).2 = 0 := rfl
  repeat rw [fa]
  repeat rw [fm]
  repeat rw [fg]
  rw [fm, fn, fn2, o1, o2]
  rcases m1_real n hpr ι hi with e | e <;> rw [e]
  · show Int.ofNat (4 * n + 3 + 1 + 1) + Jv.1 + Jv.1 + -1 + ((1 * -1 + -(0 * -0) + (1 * -1 + -(0 * -0))) + (1 * Jv.1 + -(0 * Jv.2) + (1 * Jv.1 + -(0 * Jv.2)) + (1 * Jv.1 + -(0 * Jv.2) + (1 * Jv.1 + -(0 * Jv.2))))) = Int.ofNat (4 * n + 3 + 1 + 1) - 1 - 2 * 1 + (2 + 4 * 1) * Jv.1
    lib_int
  · show Int.ofNat (4 * n + 3 + 1 + 1) + Jv.1 + Jv.1 + -1 + ((-1 * -1 + -(0 * -0) + (-1 * -1 + -(0 * -0))) + (-1 * Jv.1 + -(0 * Jv.2) + (-1 * Jv.1 + -(0 * Jv.2)) + (-1 * Jv.1 + -(0 * Jv.2) + (-1 * Jv.1 + -(0 * Jv.2))))) = Int.ofNat (4 * n + 3 + 1 + 1) - 1 - 2 * -1 + (2 + 4 * -1) * Jv.1
    lib_int

end FermatQuartic

#print axioms FermatQuartic.q4_mul
#print axioms FermatQuartic.q4_root
#print axioms FermatQuartic.cancelK
#print axioms FermatQuartic.eq_of_sub'
#print axioms FermatQuartic.four_roots
#print axioms FermatQuartic.chib_eq
#print axioms FermatQuartic.chi_q4
#print axioms FermatQuartic.q4_zero
#print axioms FermatQuartic.q4_count
#print axioms FermatQuartic.step1
#print axioms FermatQuartic.step2
#print axioms FermatQuartic.refl_om
#print axioms FermatQuartic.Fq_sum
#print axioms FermatQuartic.m1_real
#print axioms FermatQuartic.gconj_rho
#print axioms FermatQuartic.T13
#print axioms FermatQuartic.Fq_C
#print axioms FermatQuartic.fermat_quartic

#print axioms: 18 theorems, no axioms.

78.66 Quartic character of \(2\)

Pairing \(a\leftrightarrow 1-a\) in the Fermat quartic gives \(\#\{x^4+y^4=1\}\equiv 8+16\,[\chi (2)=1]\pmod {32}\).

\[ 2 \text { a quartic residue mod } p \;\Longleftrightarrow \; 8\mid b . \]

lang/QuartTwo.lean

/-
  lang/QuartTwo — T43, cycle 233: Gauss's quartic character of 2.

  p = 4n + 5, J = a + bi. N(a) = #{x⁴ = a} ∈ {1, 4, 0}, so #{x⁴ + y⁴ = 1} = 4 + 4 + 16 M, M = #{a : a, 1 − a nonzero fourth
  powers}; pairing a ↔ 1 − a (fixed point 1/2) gives M ≡ [2 is a fourth power] (mod 2):
      fq_mod32     #{x⁴ + y⁴ = 1} = 8 + 16 [χ(2) = 1] + 32 K
  With FermatQuartic, QuartPrimary (a, b mod 4) and a² + b² = p:
      quart_two    2 is a quartic residue mod p  ⇔  8 | b
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.FermatQuartic
import lang.FermatUnique

namespace QuartTwo

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup FermatQuartic Pairing
open LibRing (castZ castN)

variable (n : Nat)

def Q (ι a : Zp (4 * n + 3 + 1)) : Int := if chi4 n ι a = (1, 0) then 1 else 0

theorem g4_val (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) :
    g4 n ι a = (delta LibInt.intR a (ring (4 * n + 3 + 1)).zero + 4 * Q n ι a, 0) := by
  unfold g4 Q
  by_cases ha : a = (ring (4 * n + 3 + 1)).zero
  · rw [ha, chi4_zero, rho_zero, chib_zero, ite_neg' _ _ (fun e => Nat.noConfusion (Int.ofNat.inj (congrArg Prod.fst e)))]
    unfold delta; rw [ite_pos' _ _ rfl]; rfl
  · unfold delta; rw [ite_neg' _ _ ha, chib_eq n hpr ι hi]; unfold rho
    rcases ResidueCriterion.chi4_cases n ι a with e | e | e | e | e
    · exact absurd e (chi4_ne n hpr ι hi a ha)
    · rw [e, ite_pos' _ _ rfl]; rfl
    · rw [e, ite_neg' _ _ (fun e' => Nat.noConfusion (Int.ofNat.inj (congrArg Prod.fst e')))]; rfl
    · rw [e, ite_neg' _ _ (fun e' => Int.noConfusion (congrArg Prod.fst e'))]; rfl
    · rw [e, ite_neg' _ _ (fun e' => Nat.noConfusion (Int.ofNat.inj (congrArg Prod.fst e')))]; rfl

theorem Q_one (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : Q n ι (ring (4 * n + 3 + 1)).one = 1 := by
  unfold Q; rw [chi_one n hpr ι hi]; exact ite_pos' _ _ rfl

theorem om_zero : om n (ring (4 * n + 3 + 1)).zero = (ring (4 * n + 3 + 1)).one := by unfold om; lib_ring
theorem om_one : om n (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by unfold om; lib_ring

theorem Qsq (ι a : Zp (4 * n + 3 + 1)) : Q n ι a * Q n ι a = Q n ι a := by
  unfold Q
  by_cases h : chi4 n ι a = (1, 0)
  · rw [ite_pos' _ _ h]; rfl
  · rw [ite_neg' _ _ h]; rfl

theorem sum_dX (X : Zp (4 * n + 3 + 1) → Int) (z : Zp (4 * n + 3 + 1)) : sumK LibInt.intR (fun a => delta LibInt.intR a z * X a) (LL n) = X z :=
  FiniteRecChain.pick LibInt.intR (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) LibInt.intR) X z

/-- #{x⁴ + y⁴ = 1} = 8 + 16 M, M = #{a : a, 1 − a nonzero fourth powers}. -/
theorem fq_eq (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    (Fq n).1 = 8 + 16 * sumK LibInt.intR (fun a => Q n ι a * Q n ι (om n a)) (LL n) := by
  have e0 : (Fq n).1 = sumK LibInt.intR (fun a => (delta LibInt.intR a (ring (4 * n + 3 + 1)).zero + 4 * Q n ι a) * (delta LibInt.intR (om n a) (ring (4 * n + 3 + 1)).zero + 4 * Q n ι (om n a))) (LL n) := by
    rw [Fq_sum n hpr ι hi, sum_fst]
    exact sumK_ext LibInt.intR _ _ (fun a => by
      rw [g4_val n hpr ι hi a, g4_val n hpr ι hi (om n a)]
      show (delta LibInt.intR a (ring (4 * n + 3 + 1)).zero + 4 * Q n ι a) * (delta LibInt.intR (om n a) (ring (4 * n + 3 + 1)).zero + 4 * Q n ι (om n a)) + -(0 * 0) = _; lib_int) _
  -- expand
  have hd1 : ∀ a, delta LibInt.intR (om n a) (ring (4 * n + 3 + 1)).zero = delta LibInt.intR a (ring (4 * n + 3 + 1)).one := fun a => by
    unfold delta
    by_cases h1 : a = (ring (4 * n + 3 + 1)).one
    · rw [ite_pos' _ _ h1, ite_pos' _ _ (by rw [h1]; exact om_one n)]
    · rw [ite_neg' _ _ h1, ite_neg' _ _ (fun e => h1 (by rw [← om_om n a, e]; exact om_zero n))]
  have hdd : ∀ a, delta LibInt.intR a (ring (4 * n + 3 + 1)).zero * delta LibInt.intR a (ring (4 * n + 3 + 1)).one = 0 := fun a => by
    unfold delta
    by_cases h0 : a = (ring (4 * n + 3 + 1)).zero
    · rw [ite_pos' _ _ h0, ite_neg' _ _ (fun e => PowerSum.one_ne_zero' (4 * n + 3) (h0 ▸ e).symm)]; rfl
    · rw [ite_neg' _ _ h0]; show 0 * _ = 0; lib_int
  have ex : ∀ a, (delta LibInt.intR a (ring (4 * n + 3 + 1)).zero + 4 * Q n ι a) * (delta LibInt.intR (om n a) (ring (4 * n + 3 + 1)).zero + 4 * Q n ι (om n a))
      = delta LibInt.intR a (ring (4 * n + 3 + 1)).zero * delta LibInt.intR a (ring (4 * n + 3 + 1)).one + 4 * (delta LibInt.intR a (ring (4 * n + 3 + 1)).zero * Q n ι (om n a)) + 4 * (delta LibInt.intR a (ring (4 * n + 3 + 1)).one * Q n ι a)
        + 16 * (Q n ι a * Q n ι (om n a)) := fun a => by rw [hd1]; lib_int
  rw [sumK_ext LibInt.intR _ _ ex, QuartPrimary.sI_add, QuartPrimary.sI_add, QuartPrimary.sI_add, QuartPrimary.sI_mul, QuartPrimary.sI_mul, QuartPrimary.sI_mul,
    sumK_ext LibInt.intR _ _ hdd, show sumK LibInt.intR (fun _ => (0 : Int)) (LL n) = 0 from HeckePrime.zsumR LibInt.intR (LL n), sum_dX n (fun a => Q n ι (om n a)), sum_dX n (Q n ι), om_zero, Q_one n hpr ι hi] at e0
  rw [e0]; lib_int

/-- **#{x⁴ + y⁴ = 1} = 8 + 16 Q(1/2) + 32 K.** -/
theorem fq_mod32 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    ∃ K : Int, (Fq n).1 = 8 + 16 * Q n ι (h n) + 32 * K := by
  have e0 := fq_eq n hpr ι hi
  have hp := QuartPrimary.pair n hpr (fun a => Q n ι a * Q n ι (om n a)) (fun x => by show Q n ι (om n x) * Q n ι (om n (om n x)) = _; rw [om_om]; lib_int)
  rw [hp, show Q n ι (h n) * Q n ι (om n (h n)) = Q n ι (h n) by rw [om_h]; exact Qsq n ι (h n)] at e0
  exact ⟨sumK LibInt.intR (fun x => ltI (4 * n + 3) x (om n x) * (Q n ι x * Q n ι (om n x))) (LL n), by rw [e0]; lib_int⟩

/-! ## χ(2) and the parity of n -/

theorem Qh_iff (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : Q n ι (h n) = 1 ↔ chi4 n ι (mtwo n) = (1, 0) := by
  have e : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι (h n)) (chi4 n ι (mtwo n)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul (h n) (mtwo n) = (ring (4 * n + 3 + 1)).add (h n) (h n) by unfold mtwo; lib_ring, hh]; exact chi_one n hpr ι hi
  unfold Q
  constructor
  · intro hq
    by_cases hc : chi4 n ι (h n) = (1, 0)
    · rw [hc] at e; show chi4 n ι (mtwo n) = (Cyclo4.cyc4 LibInt.intR).one; rw [← e]; show _ = (Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).one _; lib_ring
    · rw [ite_neg' _ _ hc] at hq; exact absurd hq (fun e' => Nat.noConfusion (Int.ofNat.inj e'))
  · intro h2
    rw [h2] at e
    have : chi4 n ι (h n) = (1, 0) := by show chi4 n ι (h n) = (Cyclo4.cyc4 LibInt.intR).one; rw [← e]; show _ = (Cyclo4.cyc4 LibInt.intR).mul _ (Cyclo4.cyc4 LibInt.intR).one; lib_ring
    rw [ite_pos' _ _ this]

theorem Q01 (ι a : Zp (4 * n + 3 + 1)) : Q n ι a = 0 ∨ Q n ι a = 1 := by
  unfold Q
  by_cases h : chi4 n ι a = (1, 0)
  · rw [ite_pos' _ _ h]; exact Or.inr rfl
  · rw [ite_neg' _ _ h]; exact Or.inl rfl

theorem nat_parity : ∀ k : Nat, (∃ m, k = 2 * m) ∨ (∃ m, k = 2 * m + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | k + 1 => match nat_parity k with
    | Or.inl ⟨m, hm⟩ => Or.inr ⟨m, by rw [hm]⟩
    | Or.inr ⟨m, hm⟩ => Or.inl ⟨m + 1, by rw [hm]; lib_nat⟩

theorem pw_m1_even (k : Nat) : pw (ring (4 * n + 3 + 1)) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (2 * k) = (ring (4 * n + 3 + 1)).one := by
  rw [CubicRecip.pw_mul_split, show pw (ring (4 * n + 3 + 1)) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) 2 = (ring (4 * n + 3 + 1)).one by show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (ring (4 * n + 3 + 1)).one _) _ = _; lib_ring, ZMod.pw_one_r]

theorem m1_ne_one : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).one := fun e => mtwo_ne n (by
  unfold mtwo; calc (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by rw [e]
    _ = (ring (4 * n + 3 + 1)).zero := by lib_ring)

theorem chi_m1_odd (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (m : Nat) (hm : n = 2 * m + 1) : chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (1, 0) := by
  rcases FermatQuartic.m1_real n hpr ι hi with e | e
  · exact e
  · have h1 := QuartDup.chi4_m1_pw n ι _ e
    rw [show n + 1 = 2 * (m + 1) by rw [hm]; lib_nat, pw_m1_even] at h1
    exact absurd h1.symm (m1_ne_one n)

theorem chi_m1_even (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (m : Nat) (hm : n = 2 * m) : chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (-1, 0) := by
  rcases FermatQuartic.m1_real n hpr ι hi with e | e
  · have h1 := QuartDup.chi4_one_pw n ι _ e
    rw [show n + 1 = 2 * m + 1 by rw [hm]] at h1
    have h2 : pw (ring (4 * n + 3 + 1)) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (2 * m + 1) = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one := by
      show (ring (4 * n + 3 + 1)).mul (pw (ring (4 * n + 3 + 1)) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (2 * m)) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = _; rw [pw_m1_even]; lib_ring
    exact absurd (h2.symm.trans h1) (m1_ne_one n)
  · exact e

/-! ## integer arithmetic -/

theorem consec (u : Int) : ∃ w, u * u + u = 2 * w := by
  match GaussInt.int_even_odd u with
  | Or.inl ⟨t, ht⟩ => exact ⟨t * (2 * t + 1), by rw [ht]; lib_int⟩
  | Or.inr ⟨t, ht⟩ => exact ⟨(2 * t + 1) * (t + 1), by rw [ht]; lib_int⟩

theorem cancel16 (x y : Int) (h : 16 * x = 16 * y) : x = y :=
  QuartPrimary.two_cancel _ _ (QuartPrimary.two_cancel _ _ (QuartPrimary.two_cancel _ _ (QuartPrimary.two_cancel _ _ (by
    rw [show 2 * (2 * (2 * (2 * x))) = 16 * x by lib_int, show 2 * (2 * (2 * (2 * y))) = 16 * y by lib_int]; exact h))))

theorem odd_ne_even (x y : Int) (h : 2 * x + 1 = 2 * y) : False := GaussInt.int_odd_ne_even x y h

theorem arith_odd (A B M K K1 K2 q : Int) (hq : q = 0 ∨ q = 1) (ha : A = 1 + 2 * (2 * M + 1) + 4 * K1) (hb : B = 2 * (2 * M + 1) + 2 + 4 * K2)
    (hN : A * A + B * B = 4 * (2 * M + 1) + 5) (hf : 8 + 16 * q + 32 * K = 4 * (2 * M + 1) + 5 - 1 - 2 * 1 + (2 + 4 * 1) * A) :
    q = 1 ↔ ∃ c, B = 8 * c := by
  match consec (M + K1) with
  | ⟨w, hw⟩ =>
  have key : q + 2 * K = 2 * w + 2 * (M + K1) + 1 + (M + 1 + K2) * (M + 1 + K2) := by
    apply cancel16
    have e1 : 16 * (q + 2 * K) = A * A + B * B - 3 + 6 * A - 8 := by rw [hN]; rw [show 16 * (q + 2 * K) = (8 + 16 * q + 32 * K) - 8 by lib_int, hf]; lib_int
    rw [e1, ha, hb, show 16 * (2 * w + 2 * (M + K1) + 1 + (M + 1 + K2) * (M + 1 + K2)) = 16 * ((M + K1) * (M + K1) + (M + K1)) + 16 * (2 * (M + K1) + 1 + (M + 1 + K2) * (M + 1 + K2)) by rw [hw]; lib_int]
    lib_int
  constructor
  · intro h1
    rw [h1] at key
    match GaussInt.int_even_odd (M + 1 + K2) with
    | Or.inl ⟨t, ht⟩ => exact ⟨t, by rw [hb, show 2 * (2 * M + 1) + 2 + 4 * K2 = 4 * (M + 1 + K2) by lib_int, ht]; lib_int⟩
    | Or.inr ⟨t, ht⟩ =>
      exfalso; rw [ht] at key
      exact odd_ne_even (K - w - (M + K1) - 2 * t * t - 2 * t - 1) 0 (by
        calc 2 * (K - w - (M + K1) - 2 * t * t - 2 * t - 1) + 1 = (1 + 2 * K) - (2 * w + 2 * (M + K1) + 1 + (2 * t + 1) * (2 * t + 1)) := by lib_int
          _ = 2 * 0 := by rw [key]; lib_int)
  · intro ⟨c, hc⟩
    have hv : M + 1 + K2 = 2 * c := QuartPrimary.two_cancel _ _ (QuartPrimary.two_cancel _ _ (by
      rw [show 2 * (2 * (M + 1 + K2)) = 2 * (2 * M + 1) + 2 + 4 * K2 by lib_int, ← hb, hc]; lib_int))
    rw [hv] at key
    cases hq with
    | inr h => exact h
    | inl h =>
      rw [h] at key
      exact absurd (odd_ne_even (w + (M + K1) + 2 * c * c) K (by rw [show 2 * K = 0 + 2 * K by lib_int, key]; lib_int)) id

theorem arith_even (A B M K K1 K2 q : Int) (_hq : q = 0 ∨ q = 1) (ha : A = -1 + 2 * (2 * M) + 4 * K1) (hb : B = 2 * (2 * M) + 2 + 4 * K2)
    (hN : A * A + B * B = 4 * (2 * M) + 5) (hf : 8 + 16 * q + 32 * K = 4 * (2 * M) + 5 - 1 - 2 * -1 + (2 + 4 * -1) * A) :
    q = 1 ↔ ∃ c, B = 8 * c := by
  match consec (M + K1 - 1), consec (M + K2) with
  | ⟨w1, hw1⟩, ⟨w2, hw2⟩ =>
  have key : q + 2 * K = 2 * w1 + 2 * w2 := by
    apply cancel16
    have e1 : 16 * (q + 2 * K) = A * A + B * B + 1 - 2 * A - 8 := by rw [hN]; rw [show 16 * (q + 2 * K) = (8 + 16 * q + 32 * K) - 8 by lib_int, hf]; lib_int
    rw [e1, ha, hb, show 16 * (2 * w1 + 2 * w2) = 16 * ((M + K1 - 1) * (M + K1 - 1) + (M + K1 - 1)) + 16 * ((M + K2) * (M + K2) + (M + K2)) by rw [hw1, hw2]; lib_int]
    lib_int
  constructor
  · intro h1
    rw [h1] at key
    exact absurd (odd_ne_even K (w1 + w2) (by rw [show 2 * K + 1 = 1 + 2 * K by lib_int, key]; lib_int)) id
  · intro ⟨c, hc⟩
    exfalso
    exact odd_ne_even (M + K2) (2 * c) (QuartPrimary.two_cancel _ _ (by
      rw [show 2 * (2 * (M + K2) + 1) = 2 * (2 * M) + 2 + 4 * K2 by lib_int, ← hb, hc]; lib_int))

/-- **Gauss**: 2 is a quartic residue mod p = 4n + 5 iff 8 | b, J = a + bi. -/
theorem quart_two_Q (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    Q n ι (h n) = 1 ↔ ∃ c : Int, (Jq n (chi4 n ι)).2 = 8 * c := by
  have hq01 := Q01 n ι (h n)
  match fq_mod32 n hpr ι hi, QuartPrimary.quart_primary n hpr ι hi with
  | ⟨K, hK⟩, ⟨K1, K2, ha, hb⟩ =>
  have hf := FermatQuartic.fermat_quartic n hpr ι hi
  have hN := quart_norm n hpr ι hi
  rw [hK] at hf
  have hp : Int.ofNat (4 * n + 3 + 1 + 1) = 4 * Int.ofNat n + 5 := rfl
  rw [hp] at hf hN
  rcases nat_parity n with ⟨m, hm⟩ | ⟨m, hm⟩
  · have hs : (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)).1 = -1 := by rw [chi_m1_even n hpr ι hi m hm]
    have hn : Int.ofNat n = 2 * Int.ofNat m := by rw [hm]; rfl
    rw [hs, hn] at hf ha; rw [hn] at hb hN
    exact arith_even _ _ _ _ _ _ _ hq01 ha hb hN hf
  · have hs : (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)).1 = 1 := by rw [chi_m1_odd n hpr ι hi m hm]
    have hn : Int.ofNat n = 2 * Int.ofNat m + 1 := by rw [hm]; rfl
    rw [hs, hn] at hf ha; rw [hn] at hb hN
    exact arith_odd _ _ _ _ _ _ _ hq01 ha hb hN hf

/-- **Gauss's quartic character of 2**: 2 is a quartic residue mod p = 4n + 5 ⇔ 8 | b, J = a + bi. -/
theorem quart_two (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    chi4 n ι (mtwo n) = (1, 0) ↔ ∃ c : Int, (Jq n (chi4 n ι)).2 = 8 * c :=
  (Qh_iff n hpr ι hi).symm.trans (quart_two_Q n hpr ι hi)

/-- **Gauss's form**: 2 is a quartic residue mod p ≡ 1 (4) ⇔ p = x² + 64y². -/
theorem quart_two_form (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    chi4 n ι (mtwo n) = (1, 0) ↔ ∃ x y : Int, x * x + 64 * (y * y) = Int.ofNat (4 * n + 3 + 1 + 1) := by
  have hN := quart_norm n hpr ι hi
  constructor
  · intro h2
    match (quart_two n hpr ι hi).mp h2 with
    | ⟨c, hc⟩ => exact ⟨(Jq n (chi4 n ι)).1, c, by rw [← hN, hc]; lib_int⟩
  · intro ⟨x, y, hxy⟩
    apply (quart_two n hpr ι hi).mpr
    have ha_odd : ∃ t, (Jq n (chi4 n ι)).1 = 2 * t + 1 := by
      match QuartPrimary.quart_primary n hpr ι hi with
      | ⟨K1, _, ha, _⟩ =>
        rcases FermatQuartic.m1_real n hpr ι hi with e | e
        · exact ⟨Int.ofNat n + 2 * K1, by rw [ha, e]; show 1 + _ + _ = _; lib_int⟩
        · exact ⟨Int.ofNat n + 2 * K1 - 1, by rw [ha, e]; show -1 + _ + _ = _; lib_int⟩
    have h2 : (8 * y) * (8 * y) + x * x = Int.ofNat (4 * n + 3 + 1 + 1) := by rw [← hxy]; lib_int
    rcases FermatUnique.squares_unique (4 * n + 3) hpr _ _ _ _ hN h2 with e | e
    · exfalso
      match ha_odd with
      | ⟨t, ht⟩ =>
        rw [ht] at e
        exact odd_ne_even (2 * t * t + 2 * t) (32 * (y * y)) (by rw [show 2 * (32 * (y * y)) = (8 * y) * (8 * y) by lib_int, e]; lib_int)
    · have e2 : ((Jq n (chi4 n ι)).2 + -(8 * y)) * ((Jq n (chi4 n ι)).2 + 8 * y) = 0 := by
        rw [show ((Jq n (chi4 n ι)).2 + -(8 * y)) * ((Jq n (chi4 n ι)).2 + 8 * y) = (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2 + -((8 * y) * (8 * y)) by lib_int, e]; lib_int
      cases LibInt.int_noZD _ _ e2 with
      | inl h => exact ⟨y, by calc (Jq n (chi4 n ι)).2 = ((Jq n (chi4 n ι)).2 + -(8 * y)) + 8 * y := by lib_int
                                  _ = 8 * y := by rw [h]; exact LibInt.intR.zero_add _⟩
      | inr h => exact ⟨-y, by calc (Jq n (chi4 n ι)).2 = ((Jq n (chi4 n ι)).2 + 8 * y) + -(8 * y) := by lib_int
                                   _ = -(8 * y) := by rw [h]; exact LibInt.intR.zero_add _
                                   _ = 8 * -y := by lib_int⟩

/-- **Gauss's cyclotomic number (0, 0) of order 4**: 16·#{a : a, 1 − a nonzero fourth powers} = p − 9 − 2s + (2 + 4s)a,
    s = χ(−1), J = a + bi (for p ≡ 1 mod 8: (p − 11 + 6a)/16). -/
theorem cyclo00 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    16 * sumK LibInt.intR (fun a => Q n ι a * Q n ι (om n a)) (LL n)
      = Int.ofNat (4 * n + 3 + 1 + 1) - 9 - 2 * (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)).1 + (2 + 4 * (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)).1) * (Jq n (chi4 n ι)).1 := by
  have e1 := fq_eq n hpr ι hi
  have e2 := FermatQuartic.fermat_quartic n hpr ι hi
  rw [e1] at e2
  calc _ = (8 + 16 * sumK LibInt.intR (fun a => Q n ι a * Q n ι (om n a)) (LL n)) - 8 := by lib_int
    _ = _ := by rw [e2]; lib_int

end QuartTwo

#print axioms QuartTwo.g4_val
#print axioms QuartTwo.Q_one
#print axioms QuartTwo.om_zero
#print axioms QuartTwo.om_one
#print axioms QuartTwo.Qsq
#print axioms QuartTwo.sum_dX
#print axioms QuartTwo.fq_eq
#print axioms QuartTwo.fq_mod32
#print axioms QuartTwo.Qh_iff
#print axioms QuartTwo.Q01
#print axioms QuartTwo.nat_parity
#print axioms QuartTwo.pw_m1_even
#print axioms QuartTwo.m1_ne_one
#print axioms QuartTwo.chi_m1_odd
#print axioms QuartTwo.chi_m1_even
#print axioms QuartTwo.consec
#print axioms QuartTwo.cancel16
#print axioms QuartTwo.odd_ne_even
#print axioms QuartTwo.arith_odd
#print axioms QuartTwo.arith_even
#print axioms QuartTwo.quart_two_Q
#print axioms QuartTwo.quart_two
#print axioms QuartTwo.quart_two_form
#print axioms QuartTwo.cyclo00

#print axioms: 24 theorems, no axioms.

78.67 Exact traces of the sextic twists

Cube roots counted by \(1+\chi +\bar \chi \) and the substitution \(u=-Dt\).

\[ \sum _x\rho (x^3+D)=\rho (D)\,\mathrm {Tr}\big (\chi (-D)\,J(\chi ,\rho )\big )\qquad (D\ne 0). \]

lang/CubicSextic.lean

/-
  lang/CubicSextic — T43, cycles 235–236: the exact Frobenius trace of every curve y² = x³ + D at p ≡ 1 (mod 3).

  p = 6n + 7, χ the cubic character (values in ℤ[ω]), ρ the quadratic character with values ±1 ∈ ℤ[ω] (rho3: Euler's criterion
  x^{(p−1)/2} = ±1), K = J(χ, ρ) = Σ_t χ(t)ρ(1 − t). With #{x³ = u} = 1 + χ(u) + χ̄(u) (FermatCubic.cube_count) and u = −Dt:
      sextic_sum    Σ_x ρ(x³ + D) = ρ(D) (z + z̄),  z = χ(−D) K        (D ≠ 0)
  so a_p(y² = x³ + D) = −ρ(D) Tr χ(−D) J(χ, ρ): the Hecke character of ℚ(√−3) attached to the sextic twist, for every D.
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.FermatCubic
import lang.CharSumGen

namespace CubicSextic

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm FermatCubic
open LibRing (castZ castN)

variable (n : Nat)

def rho3 (x : Zp (6 * n + 5 + 1)) : Int × Int :=
  if x = (ring (6 * n + 5 + 1)).zero then (0, 0) else if pw (ring (6 * n + 5 + 1)) x (3 * n + 3) = (ring (6 * n + 5 + 1)).one then (1, 0) else (-1, 0)

theorem two_ne : (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero :=
  Eisen.two_ne_zero' (6 * n + 5) (by rw [show 6 * n + 5 = (6 * n + 3) + 2 by lib_nat]; exact Nat.le_add_left 2 _)

theorem m1_ne : (ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).one := fun e => two_ne n (by
  calc (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one (ring (6 * n + 5 + 1)).one = (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) := by rw [e]
    _ = (ring (6 * n + 5 + 1)).zero := by lib_ring)

/-- Euler: x^{(p−1)/2} = ±1 for x ≠ 0. -/
theorem euler_pm (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (x : Zp (6 * n + 5 + 1)) (hx : x ≠ (ring (6 * n + 5 + 1)).zero) :
    pw (ring (6 * n + 5 + 1)) x (3 * n + 3) = (ring (6 * n + 5 + 1)).one ∨ pw (ring (6 * n + 5 + 1)) x (3 * n + 3) = (ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one := by
  have h1 : (ring (6 * n + 5 + 1)).mul (pw (ring (6 * n + 5 + 1)) x (3 * n + 3)) (pw (ring (6 * n + 5 + 1)) x (3 * n + 3)) = (ring (6 * n + 5 + 1)).one := by
    rw [← HasseCM.pw_add', show 3 * n + 3 + (3 * n + 3) = 6 * n + 5 + 1 by lib_nat]; exact unit_pow (6 * n + 5) hpr x hx
  have h2 : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (pw (ring (6 * n + 5 + 1)) x (3 * n + 3)) ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) ((ring (6 * n + 5 + 1)).add (pw (ring (6 * n + 5 + 1)) x (3 * n + 3)) (ring (6 * n + 5 + 1)).one) = (ring (6 * n + 5 + 1)).zero := by
    rw [show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (pw (ring (6 * n + 5 + 1)) x (3 * n + 3)) ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) ((ring (6 * n + 5 + 1)).add (pw (ring (6 * n + 5 + 1)) x (3 * n + 3)) (ring (6 * n + 5 + 1)).one) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul (pw (ring (6 * n + 5 + 1)) x (3 * n + 3)) (pw (ring (6 * n + 5 + 1)) x (3 * n   -- (+89 chars; full line in the Zenodo source)
  cases nozd (6 * n + 5) hpr _ _ h2 with
  | inl e => exact Or.inl (eq_of_sub n _ _ e)
  | inr e => exact Or.inr (by calc pw (ring (6 * n + 5 + 1)) x (3 * n + 3) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (pw (ring (6 * n + 5 + 1)) x (3 * n + 3)) (ring (6 * n + 5 + 1)).one) ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) := by lib_ring
                                _ = _ := by rw [e]; lib_ring)

theorem rho3_zero : rho3 n (ring (6 * n + 5 + 1)).zero = (0, 0) := by unfold rho3; rw [ite_pos' _ _ rfl]

theorem rho3_val (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (x : Zp (6 * n + 5 + 1)) (hx : x ≠ (ring (6 * n + 5 + 1)).zero) :
    (pw (ring (6 * n + 5 + 1)) x (3 * n + 3) = (ring (6 * n + 5 + 1)).one ∧ rho3 n x = (1, 0)) ∨ (pw (ring (6 * n + 5 + 1)) x (3 * n + 3) = (ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one ∧ rho3 n x = (-1, 0)) := by
  unfold rho3; rw [ite_neg' _ _ hx]
  rcases euler_pm n hpr x hx with e | e
  · exact Or.inl ⟨e, ite_pos' _ _ e⟩
  · exact Or.inr ⟨e, ite_neg' _ _ (by rw [e]; exact m1_ne n)⟩

theorem rho3_p (z : Zp (6 * n + 5 + 1)) (hz : z ≠ (ring (6 * n + 5 + 1)).zero) (h : pw (ring (6 * n + 5 + 1)) z (3 * n + 3) = (ring (6 * n + 5 + 1)).one) : rho3 n z = (1, 0) := by
  unfold rho3; rw [ite_neg' _ _ hz, ite_pos' _ _ h]
theorem rho3_m (z : Zp (6 * n + 5 + 1)) (hz : z ≠ (ring (6 * n + 5 + 1)).zero) (h : pw (ring (6 * n + 5 + 1)) z (3 * n + 3) = (ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) : rho3 n z = (-1, 0) := by
  unfold rho3; rw [ite_neg' _ _ hz, ite_neg' _ _ (by rw [h]; exact m1_ne n)]

theorem rho3_mul (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (x y : Zp (6 * n + 5 + 1)) : rho3 n ((ring (6 * n + 5 + 1)).mul x y) = (Cyclo3.cyc3 LibInt.intR).mul (rho3 n x) (rho3 n y) := by
  by_cases hx : x = (ring (6 * n + 5 + 1)).zero
  · rw [hx, show (ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).zero y = (ring (6 * n + 5 + 1)).zero by lib_ring, rho3_zero]; show (Cyclo3.cyc3 LibInt.intR).zero = (Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero _; lib_ring
  by_cases hy : y = (ring (6 * n + 5 + 1)).zero
  · rw [hy, show (ring (6 * n + 5 + 1)).mul x (ring (6 * n + 5 + 1)).zero = (ring (6 * n + 5 + 1)).zero by lib_ring, rho3_zero]; show (Cyclo3.cyc3 LibInt.intR).zero = (Cyclo3.cyc3 LibInt.intR).mul _ (Cyclo3.cyc3 LibInt.intR).zero; lib_ring
  have hxy : (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun e => hy (cancel n hpr x y hx e)
  have hp : pw (ring (6 * n + 5 + 1)) ((ring (6 * n + 5 + 1)).mul x y) (3 * n + 3) = (ring (6 * n + 5 + 1)).mul (pw (ring (6 * n + 5 + 1)) x (3 * n + 3)) (pw (ring (6 * n + 5 + 1)) y (3 * n + 3)) := pw_mul (ring (6 * n + 5 + 1)) x y _
  rcases euler_pm n hpr x hx with ex | ex <;> rcases euler_pm n hpr y hy with ey | ey
  · rw [rho3_p n x hx ex, rho3_p n y hy ey, rho3_p n _ hxy (by rw [hp, ex, ey]; lib_ring)]; rfl
  · rw [rho3_p n x hx ex, rho3_m n y hy ey, rho3_m n _ hxy (by rw [hp, ex, ey]; lib_ring)]; rfl
  · rw [rho3_m n x hx ex, rho3_p n y hy ey, rho3_m n _ hxy (by rw [hp, ex, ey]; lib_ring)]; rfl
  · rw [rho3_m n x hx ex, rho3_m n y hy ey, rho3_p n _ hxy (by rw [hp, ex, ey]; lib_ring)]; rfl

theorem rho3_conj (x : Zp (6 * n + 5 + 1)) : cconj (rho3 n x) = rho3 n x := by
  unfold rho3
  by_cases h0 : x = (ring (6 * n + 5 + 1)).zero
  · rw [ite_pos' _ _ h0]; rfl
  · rw [ite_neg' _ _ h0]
    by_cases h1 : pw (ring (6 * n + 5 + 1)) x (3 * n + 3) = (ring (6 * n + 5 + 1)).one
    · rw [ite_pos' _ _ h1]; rfl
    · rw [ite_neg' _ _ h1]; rfl

/-- Σ_x ρ(x) = 0. -/
theorem rho3_sum (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) : sumK (Cyclo3.cyc3 LibInt.intR) (rho3 n) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := by
  match PowerSum.exists_nonroot (6 * n + 5) hpr (3 * n + 2) (by rw [show 6 * n + 5 = (3 * n + 2 + 1) + (3 * n + 2) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hr : rho3 n x0 = (-1, 0) := by
      rcases rho3_val n hpr x0 hx0 with ⟨e, _⟩ | ⟨_, e⟩
      · exact absurd e hn
      · exact e
    have hs : sumK (Cyclo3.cyc3 LibInt.intR) (rho3 n) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul (rho3 n x0) (sumK (Cyclo3.cyc3 LibInt.intR) (rho3 n) (LL n)) := by
      have hsc : sumK (Cyclo3.cyc3 LibInt.intR) (fun x => rho3 n ((ring (6 * n + 5 + 1)).mul x0 x)) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (rho3 n) (LL n) := CharSumGen.sum_scale (Cyclo3.cyc3 LibInt.intR) (6 * n + 4) hpr (rho3 n) x0 hx0
      rw [mul_sumK, ← hsc]
      exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => rho3_mul n hpr x0 x) _
    rw [hr] at hs
    generalize sumK (Cyclo3.cyc3 LibInt.intR) (rho3 n) (LL n) = S at hs ⊢
    match S, hs with
    | (s1, s2), hs =>
      have e1 : s1 = -1 * s1 + -(0 * s2) := congrArg Prod.fst hs
      have e2 : s2 = -1 * s2 + 0 * s1 + -(0 * s2) := congrArg Prod.snd hs
      have two0 : ∀ t : Int, 2 * t = 0 → t = 0 := fun t h2 => by
        cases LibInt.int_noZD _ _ h2 with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Int.ofNat.inj e'))
        | inr e => exact e
      have h1 : s1 = 0 := two0 s1 (by
        calc 2 * s1 = s1 + -(-1 * s1 + -(0 * s2)) := by lib_int
          _ = 0 := by rw [← e1]; lib_int)
      have h2 : s2 = 0 := two0 s2 (by
        calc 2 * s2 = s2 + -(-1 * s2 + 0 * s1 + -(0 * s2)) := by lib_int
          _ = 0 := by rw [← e2]; lib_int)
      rw [h1, h2]; rfl

/-! ## the sextic character sum -/

/-- K = J(χ, ρ) = Σ_t χ(t) ρ(1 − t). -/
def Kx (w : Zp (6 * n + 5 + 1)) : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (rho3 n (om1 n t))) (LL n)

def Sx (D : Zp (6 * n + 5 + 1)) : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun x => rho3 n ((ring (6 * n + 5 + 1)).add (cube n x) D)) (LL n)

theorem cb_cconj (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (x : Zp (6 * n + 5 + 1)) : cb n w x = cconj (chi3 n w x) :=
  (cb_eq n hpr w hw1 hw3 x).1.trans (cb_eq n hpr w hw1 hw3 x).2

/-- **Σ_x ρ(x³ + D) = ρ(D)(z + z̄), z = χ(−D) J(χ, ρ).** -/
theorem sextic_sum (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) :
    Sx n D = (Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w)) (cconj ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w)))) := by
  have hD' : (ring (6 * n + 5 + 1)).neg D ≠ (ring (6 * n + 5 + 1)).zero := fun e => hD (by calc D = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg D) := by lib_ring
                                                         _ = (ring (6 * n + 5 + 1)).zero := by rw [e]; lib_ring)
  unfold Sx
  rw [FermatCubic.step2 n hpr w hw1 hw3 (fun a => rho3 n ((ring (6 * n + 5 + 1)).add a D))]
  have hexp : ∀ a, (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (rho3 n ((ring (6 * n + 5 + 1)).add a D)) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (rho3 n ((ring (6 * n + 5 + 1)).add a D)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (rho3 n ((ring (6 * n + 5 + 1)).add a D)))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (rho3 n ((ring (6 * n + 5 + 1)).add a D))) := fun a => by unfol  -- (+14 chars; full line in the Zenodo source)
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hexp, sumK_add, sumK_add]
  -- Σ ρ(a + D) = 0
  have S0 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => rho3 n ((ring (6 * n + 5 + 1)).add a D)) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := by
    rw [FiniteRecChain.reindex (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (fun a => (ring (6 * n + 5 + 1)).add a D) (fun a => (ring (6 * n + 5 + 1)).add a ((ring (6 * n + 5 + 1)).neg D)) (fun x => by lib_ring) (fun y => by lib_ring) (rho3 n)]
    exact rho3_sum n hpr
  -- the scaling a = −D t
  have scale : ∀ F : Zp (6 * n + 5 + 1) → Int × Int, sumK (Cyclo3.cyc3 LibInt.intR) F (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun t => F ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg D) t)) (LL n) := fun F =>
    (CharSumGen.sum_scale (Cyclo3.cyc3 LibInt.intR) (6 * n + 4) hpr F ((ring (6 * n + 5 + 1)).neg D) hD').symm
  have S1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (rho3 n ((ring (6 * n + 5 + 1)).add a D))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (rho3 n D)) (Kx n w) := by
    rw [scale, Kx, mul_sumK]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => by
      rw [chi3_mul n hpr w hw1 hw3, show (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg D) t) D = (ring (6 * n + 5 + 1)).mul D (om1 n t) by unfold om1; lib_ring, rho3_mul n hpr]
      lib_ring) _
  have S2 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (rho3 n ((ring (6 * n + 5 + 1)).add a D))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg D))) (rho3 n D)) (cconj (Kx n w)) := by
    rw [scale, Kx, cconj_sum, mul_sumK]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => by
      rw [cb_mul n hpr w hw1 hw3, show (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg D) t) D = (ring (6 * n + 5 + 1)).mul D (om1 n t) by unfold om1; lib_ring, rho3_mul n hpr,
        cconj_mul, rho3_conj, cb_cconj n hpr w hw1 hw3, cb_cconj n hpr w hw1 hw3]
      lib_ring) _
  rw [S0, S1, S2, cconj_mul]
  have hr : cconj (rho3 n D) = rho3 n D := rho3_conj n D
  lib_ring

end CubicSextic

#print axioms CubicSextic.two_ne
#print axioms CubicSextic.m1_ne
#print axioms CubicSextic.euler_pm
#print axioms CubicSextic.rho3_zero
#print axioms CubicSextic.rho3_val
#print axioms CubicSextic.rho3_p
#print axioms CubicSextic.rho3_m
#print axioms CubicSextic.rho3_mul
#print axioms CubicSextic.rho3_conj
#print axioms CubicSextic.rho3_sum
#print axioms CubicSextic.cb_cconj
#print axioms CubicSextic.sextic_sum

#print axioms: 12 theorems, no axioms.

78.68 Cubic duplication

Square roots counted by \(1+\rho \) and \(4t(1-t)=1-(2t-1)^2\).

\[ J(\chi ,\rho )=\chi (4)\,J(\chi ,\chi ),\qquad \sum _x\rho (x^3+D)=\rho (D)\,\mathrm {Tr}\big (\chi (-4D)J\big ). \]

lang/CubicDup.lean

/-
  lang/CubicDup — T43, cycle 237: cubic duplication J(χ, ρ) = χ(4) J(χ, χ) and the traces of y² = x³ + D through J.

  p = 6n + 7. #{x² = a} = 1 + ρ(a); with 4t(1 − t) = 1 − (2t − 1)²: χ(4)J(χ,χ) = Σ_s χ(1 − s²) = Σ_v (1 + ρ(v))χ(1 − v) = J(ρ, χ).
      cubic_dup     J(χ, ρ) = χ(4) J
      sextic_J      Σ_x ρ(x³ + D) = ρ(D) Tr(χ(−4D) J)       (D ≠ 0; a_p(y² = x³ + D) = −ρ(D) Tr χ(−4D)J)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CubicSextic

namespace CubicDup

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm FermatCubic CubicSextic
open LibRing (castZ castN)

variable (n : Nat)

theorem sq_root (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (a : Zp (6 * n + 5 + 1)) (ha0 : a ≠ (ring (6 * n + 5 + 1)).zero) (ha : pw (ring (6 * n + 5 + 1)) a (3 * n + 3) = (ring (6 * n + 5 + 1)).one) : ∃ c, (ring (6 * n + 5 + 1)).mul c c = a := by
  match PrimRoot.primitive_root (6 * n + 5) hpr with
  | ⟨g, hg⟩ =>
    match DLog.mem_pows (6 * n + 5) g a _ (DLog.every_unit (6 * n + 5) hpr g hg a ha0) with
    | ⟨k, _, hk⟩ =>
      have h1 : pw (ring (6 * n + 5 + 1)) g (k * (3 * n + 3)) = (ring (6 * n + 5 + 1)).one := by rw [← TwoPower.pwpow, ← hk, ha]
      match hg.2 _ h1 with
      | ⟨c, hc⟩ =>
        have h3 : k = 2 * c := by
          apply Nat.eq_of_mul_eq_mul_right (Nat.zero_lt_succ (3 * n + 2))
          rw [hc]; lib_nat
        refine ⟨pw (ring (6 * n + 5 + 1)) g c, ?_⟩
        rw [hk, h3, show 2 * c = c * 2 by lib_nat, ← TwoPower.pwpow]
        show _ = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one _) _
        lib_ring

def mtwo : Zp (6 * n + 5 + 1) := (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one (ring (6 * n + 5 + 1)).one

theorem two_roots (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) (x : Zp (6 * n + 5 + 1)) :
    delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).mul x x) ((ring (6 * n + 5 + 1)).mul c c) = (Cyclo3.cyc3 LibInt.intR).add (delta (Cyclo3.cyc3 LibInt.intR) x c) (delta (Cyclo3.cyc3 LibInt.intR) x ((ring (6 * n + 5 + 1)).neg c)) := by
  have d : c ≠ (ring (6 * n + 5 + 1)).neg c := fun e => hc (by
    have e2 : (ring (6 * n + 5 + 1)).mul (mtwo n) c = (ring (6 * n + 5 + 1)).zero := by
      calc (ring (6 * n + 5 + 1)).mul (mtwo n) c = (ring (6 * n + 5 + 1)).add c c := by unfold mtwo; lib_ring
        _ = (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg c) := congrArg ((ring (6 * n + 5 + 1)).add c) e
        _ = (ring (6 * n + 5 + 1)).zero := by lib_ring
    exact cancel n hpr _ _ (CubicSextic.two_ne n) e2)
  unfold delta
  by_cases hx : (ring (6 * n + 5 + 1)).mul x x = (ring (6 * n + 5 + 1)).mul c c
  · rw [ite_pos' _ _ hx]
    have e : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add x ((ring (6 * n + 5 + 1)).neg c)) ((ring (6 * n + 5 + 1)).add x c) = (ring (6 * n + 5 + 1)).zero := by
      rw [show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add x ((ring (6 * n + 5 + 1)).neg c)) ((ring (6 * n + 5 + 1)).add x c) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul x x) ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul c c)) by lib_ring, hx]; lib_ring
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h =>
      have ex : x = c := eq_of_sub n _ _ h
      rw [ite_pos' _ _ ex, ite_neg' _ _ (by rw [ex]; exact d)]; lib_ring
    | inr h =>
      have ex : x = (ring (6 * n + 5 + 1)).neg c := by calc x = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add x c) ((ring (6 * n + 5 + 1)).neg c) := by lib_ring
                                      _ = (ring (6 * n + 5 + 1)).neg c := by rw [h]; lib_ring
      rw [ite_neg' _ _ (by rw [ex]; exact fun e => d e.symm), ite_pos' _ _ ex]; lib_ring
  · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by rw [e])), ite_neg' _ _ (fun e => hx (by rw [e]; lib_ring))]; lib_ring

theorem rho3_sq (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (y : Zp (6 * n + 5 + 1)) (hy : y ≠ (ring (6 * n + 5 + 1)).zero) : rho3 n ((ring (6 * n + 5 + 1)).mul y y) = (Cyclo3.cyc3 LibInt.intR).one := by
  rw [rho3_mul n hpr]
  rcases rho3_val n hpr y hy with ⟨_, e⟩ | ⟨_, e⟩ <;> rw [e] <;> rfl

/-- **#{x : x² = a} = 1 + ρ(a).** -/
theorem sq_count (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (a : Zp (6 * n + 5 + 1)) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun x => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).mul x x) a) (LL n) = (Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (rho3 n a) := by
  by_cases ha : a = (ring (6 * n + 5 + 1)).zero
  · rw [ha, sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun x => delta (Cyclo3.cyc3 LibInt.intR) x (ring (6 * n + 5 + 1)).zero) (fun x => by
      unfold delta
      by_cases hx : x = (ring (6 * n + 5 + 1)).zero
      · rw [ite_pos' _ _ hx, ite_pos' _ _ (by rw [hx]; lib_ring)]
      · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by
          cases nozd (6 * n + 5) hpr _ _ e with
          | inl h => exact h
          | inr h => exact h))]), show sumK (Cyclo3.cyc3 LibInt.intR) (fun x => delta (Cyclo3.cyc3 LibInt.intR) x (ring (6 * n + 5 + 1)).zero) (LL n) = (Cyclo3.cyc3 LibInt.intR).one from CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR) (ring (6 * n + 5 + 1)).zero, rho3_zero]; show (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc  -- (+29 chars; full line in the Zenodo source)
  · rcases rho3_val n hpr a ha with ⟨ep, e⟩ | ⟨_, e⟩
    · match sq_root n hpr a ha ep with
      | ⟨c, hc⟩ =>
        have hc0 : c ≠ (ring (6 * n + 5 + 1)).zero := fun e0 => ha (by rw [← hc, e0]; lib_ring)
        rw [← hc, sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => two_roots n hpr c hc0 x), sumK_add, show sumK (Cyclo3.cyc3 LibInt.intR) (fun x => delta (Cyclo3.cyc3 LibInt.intR) x c) (LL n) = (Cyclo3.cyc3 LibInt.intR).one from CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR) c,
          show sumK (Cyclo3.cyc3 LibInt.intR) (fun x => delta (Cyclo3.cyc3 LibInt.intR) x ((ring (6 * n + 5 + 1)).neg c)) (LL n) = (Cyclo3.cyc3 LibInt.intR).one from CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).neg c), hc, e]; rfl
    · have hz : ∀ x, delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).mul x x) a = (Cyclo3.cyc3 LibInt.intR).zero := fun x => by
        unfold delta
        rw [ite_neg' _ _ (fun ex => by
          have hx0 : x ≠ (ring (6 * n + 5 + 1)).zero := fun e0 => ha (by rw [← ex, e0]; lib_ring)
          have h1 := rho3_sq n hpr x hx0
          rw [ex, e] at h1
          exact absurd (congrArg Prod.fst h1) (fun e' => Int.noConfusion e'))]
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hz, HeckePrime.zsumR (Cyclo3.cyc3 LibInt.intR), e]; rfl

def hf : Zp (6 * n + 5 + 1) := mk (6 * n + 5 + 1) (3 * n + 4)

theorem hh : (ring (6 * n + 5 + 1)).add (hf n) (hf n) = (ring (6 * n + 5 + 1)).one := Subtype.ext (by
  show md (6 * n + 5 + 1) (md (6 * n + 5 + 1) (3 * n + 4) + md (6 * n + 5 + 1) (3 * n + 4)) = md (6 * n + 5 + 1) 1
  rw [← md_add2, show 3 * n + 4 + (3 * n + 4) = 1 + (6 * n + 5 + 1 + 1) by lib_nat, md_add_p])

def sh (t : Zp (6 * n + 5 + 1)) : Zp (6 * n + 5 + 1) := (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add t t) ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)
def shi (u : Zp (6 * n + 5 + 1)) : Zp (6 * n + 5 + 1) := (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add u (ring (6 * n + 5 + 1)).one) (hf n)

theorem sh_shi (u : Zp (6 * n + 5 + 1)) : sh n (shi n u) = u := by
  unfold sh shi
  calc _ = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add u (ring (6 * n + 5 + 1)).one) ((ring (6 * n + 5 + 1)).add (hf n) (hf n))) ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) := by lib_ring
    _ = u := by rw [hh]; lib_ring

theorem shi_sh (t : Zp (6 * n + 5 + 1)) : shi n (sh n t) = t := by
  unfold sh shi
  calc _ = (ring (6 * n + 5 + 1)).mul t ((ring (6 * n + 5 + 1)).add (hf n) (hf n)) := by lib_ring
    _ = t := by rw [hh]; lib_ring

theorem four_id (t : Zp (6 * n + 5 + 1)) : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul t (om1 n t)) = om1 n ((ring (6 * n + 5 + 1)).mul (sh n t) (sh n t)) := by
  unfold mtwo om1 sh; lib_ring

theorem om1_om1 (t : Zp (6 * n + 5 + 1)) : om1 n (om1 n t) = t := by unfold om1; lib_ring

/-- **Cubic duplication**: J(χ, ρ) = χ(4) J(χ, χ). -/
theorem cubic_dup (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    Kx n w = (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n))) (J n w) := by
  have eA : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n))) (J n w) = sumK (Cyclo3.cyc3 LibInt.intR) (fun s => chi3 n w (om1 n ((ring (6 * n + 5 + 1)).mul s s))) (LL n) := by
    rw [show J n w = sumK (Cyclo3.cyc3 LibInt.intR) (fun t => chi3 n w ((ring (6 * n + 5 + 1)).mul t (om1 n t))) (LL n) from sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => (chi3_mul n hpr w hw1 hw3 t (om1 n t)).symm) _, mul_sumK]
    rw [← FiniteRecChain.reindex (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (sh n) (shi n) (shi_sh n) (sh_shi n) (fun s => chi3 n w (om1 n ((ring (6 * n + 5 + 1)).mul s s)))]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => by rw [← chi3_mul n hpr w hw1 hw3, four_id]) _
  have eB : sumK (Cyclo3.cyc3 LibInt.intR) (fun s => chi3 n w (om1 n ((ring (6 * n + 5 + 1)).mul s s))) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun v => (Cyclo3.cyc3 LibInt.intR).mul (sumK (Cyclo3.cyc3 LibInt.intR) (fun s => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).mul s s) v) (LL n)) (chi3 n w (om1 n v))) (LL n) := by
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun s => sumK (Cyclo3.cyc3 LibInt.intR) (fun v => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) v ((ring (6 * n + 5 + 1)).mul s s)) (chi3 n w (om1 n v))) (LL n))
      (fun s => (FiniteRecChain.pick (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (fun v => chi3 n w (om1 n v)) _).symm), sumK_swap]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun v => by
      rw [sumK_mul]; exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun s => by rw [delta_symm]) _) _
  have eC : sumK (Cyclo3.cyc3 LibInt.intR) (fun v => (Cyclo3.cyc3 LibInt.intR).mul (sumK (Cyclo3.cyc3 LibInt.intR) (fun s => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).mul s s) v) (LL n)) (chi3 n w (om1 n v))) (LL n) = Kx n w := by
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun v => (Cyclo3.cyc3 LibInt.intR).add (chi3 n w (om1 n v)) ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n v) (chi3 n w (om1 n v)))) (fun v => by rw [sq_count n hpr]; lib_ring), sumK_add]
    have e0 : sumK (Cyclo3.cyc3 LibInt.intR) (fun v => chi3 n w (om1 n v)) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := by
      rw [FiniteRecChain.reindex (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (om1 n) (om1 n) (om1_om1 n) (om1_om1 n) (chi3 n w)]
      exact chi3_sum n hpr w hw1 hw3
    rw [e0]; unfold Kx
    rw [← FiniteRecChain.reindex (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (om1 n) (om1 n) (om1_om1 n) (om1_om1 n) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (rho3 n (om1 n t)))]
    show _ = sumK (Cyclo3.cyc3 LibInt.intR) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w (om1 n t)) (rho3 n (om1 n (om1 n t)))) (LL n)
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w (om1 n t)) (rho3 n (om1 n (om1 n t)))) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (rho3 n t) (chi3 n w (om1 n t))) (fun t => by rw [om1_om1]; exact (Cyclo3.cyc3 LibInt.intR).mul_comm _ _)]
    lib_ring
  rw [eA, eB, eC]

/-- **a_p of y² = x³ + D through J**: Σ_x ρ(x³ + D) = ρ(D) Tr(χ(−4D) J). -/
theorem sextic_J (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) :
    Sx n D = (Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))) (J n w)) (cconj ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))) (  -- (+15 chars; full line in the Zenodo source)
  rw [sextic_sum n hpr w hw1 hw3 D hD, cubic_dup n hpr w hw1 hw3,
    show (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg D) ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) by lib_ring, chi3_mul n hpr w hw1 hw3 ((ring (6 * n + 5 + 1)).neg D) ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n))]
  repeat rw [cconj_mul]
  lib_ring

end CubicDup
#print axioms CubicDup.sq_root
#print axioms CubicDup.two_roots
#print axioms CubicDup.rho3_sq
#print axioms CubicDup.sq_count
#print axioms CubicDup.hh
#print axioms CubicDup.sh_shi
#print axioms CubicDup.shi_sh
#print axioms CubicDup.four_id
#print axioms CubicDup.om1_om1
#print axioms CubicDup.cubic_dup
#print axioms CubicDup.sextic_J

#print axioms: 11 theorems, no axioms.

78.69 Exact traces of the quartic twists

Square roots counted by \(1+\rho \), \(\rho \chi =\bar \chi \), the substitution \(u=Dt\) and duplication.

\[ \sum _x\rho (x^3-Dx)=\mathrm {Tr}\big (\bar \chi (D)\chi (-1)J\big )\qquad (D\ne 0). \]

lang/QuartTwist.lean

/-
  lang/QuartTwist — T43, cycle 238: the exact Frobenius trace of every quartic twist y² = x³ − Dx at p ≡ 1 (mod 4).

  p = 4n + 5. ρ(x(x² − D)) = χ(x²)ρ(x² − D); #{x² = u} = 1 + ρ(u) (QuartDup), ρχ = χ̄, χ̄ρ = χ, and u = Dt:
      Σ_x ρ(x³ − Dx) = χ̄(D) J(χ, ρ) + χ(D) J(χ̄, ρ),   J(χ, ρ) = χ(−1) J      (QuartDup)
      quart_twist    Σ_x ρ(x³ − Dx) = z + z̄,  z = χ̄(D) χ(−1) J          (D ≠ 0)
  so a_p(y² = x³ − Dx) = −Tr(χ̄(−D) J): the Hecke character of ℚ(i) of every quartic twist, through the primary J.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.FermatQuartic

namespace QuartTwist

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup FermatQuartic
open LibRing (castZ castN)

variable (n : Nat)

theorem chi_rho (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι x) (rho n ι x) = chib n ι x := by
  rw [chib_eq n hpr ι hi]; unfold rho
  rcases ResidueCriterion.chi4_cases n ι x with e | e | e | e | e <;> rw [e] <;> rfl

theorem chib_rho (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) : (Cyclo4.cyc4 LibInt.intR).mul (chib n ι x) (rho n ι x) = chi4 n ι x := by
  rw [chib_eq n hpr ι hi]; unfold rho
  rcases ResidueCriterion.chi4_cases n ι x with e | e | e | e | e <;> rw [e] <;> rfl

theorem gconj_chib (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) : gconj (chib n ι x) = chi4 n ι x := by
  rw [chib_eq n hpr ι hi]; exact Prod.ext rfl (by show -(-(chi4 n ι x).2) = _; lib_int)

def Sq (D : Zp (4 * n + 3 + 1)) (ι : Zp (4 * n + 3 + 1)) : Int × Int :=
  sumK (Cyclo4.cyc4 LibInt.intR) (fun x => rho n ι ((ring (4 * n + 3 + 1)).mul x ((ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul x x) ((ring (4 * n + 3 + 1)).neg D)))) (LL n)

/-- J(χ, ρ) = Σ_t χ(t) ρ(1 − t). -/
def Jcr (ι : Zp (4 * n + 3 + 1)) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun t => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (rho n ι (om n t))) (LL n)

theorem Jcr_eq (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : Jcr n ι = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)) := by
  have hs : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring]; exact chi_one n hpr ι hi
  have e1 : Jcr n ι = J2 n ι := by
    unfold Jcr J2
    rw [← FermatQuartic.refl_om n (fun t => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chi4 n ι (om n t)))]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by show _ = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι (om n t)) (chi4 n ι (om n (om n t))); rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  rw [e1, QuartDup.quart_dup n hpr ι hi]
  calc J2 n ι = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (J2 n ι) := by rw [hs]; lib_ring
    _ = _ := by lib_ring

/-- **The quartic twists**: Σ_x ρ(x³ − Dx) = z + z̄, z = χ̄(D) χ(−1) J. -/
theorem quart_twist (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero) :
    Sq n D ι = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (Jcr n ι)) (gconj ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (Jcr n ι))) := by
  unfold Sq
  -- ρ(x(x² − D)) = h(x²)
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun x => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).mul x x)) (rho n ι ((ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul x x) ((ring (4 * n + 3 + 1)).neg D)))) (fun x => by
    rw [rho_mul n hpr ι hi]; unfold rho; rw [chi4_mul n hpr ι hi])]
  -- Σ_x h(x²) = Σ_u (1 + ρ(u)) h(u)
  have eB : sumK (Cyclo4.cyc4 LibInt.intR) (fun x => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).mul x x)) (rho n ι ((ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul x x) ((ring (4 * n + 3 + 1)).neg D)))) (LL n)
      = sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (fun s => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).mul s s) u) (LL n)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add u ((ring (4 * n + 3 + 1)).neg D))))) (LL n) := by
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun s => sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) u ((ring (4 * n + 3 + 1)).mul s s)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add u ((ring (4 * n + 3 + 1)).neg D))))) (LL n))
      (fun s => (FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add u ((ring (4 * n + 3 + 1)).neg D)))) _).symm), sumK_swap]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun u => by
      rw [sumK_mul]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun s => by rw [delta_symm]) _) _
  rw [eB, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun u => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add u ((ring (4 * n + 3 + 1)).neg D)))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add u ((ring (4 * n + 3 + 1)).neg D)))))
    (fun u => by rw [QuartDup.sq_count n hpr ι hi, ← chi_rho n hpr ι hi]; lib_ring), sumK_add]
  have scale : ∀ F : Zp (4 * n + 3 + 1) → Int × Int, sumK (Cyclo4.cyc4 LibInt.intR) F (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun t => F ((ring (4 * n + 3 + 1)).mul D t)) (LL n) := fun F =>
    (CharSumGen.sum_scale (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr F D hD).symm
  have hsub : ∀ t, (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul D t) ((ring (4 * n + 3 + 1)).neg D) = (ring (4 * n + 3 + 1)).mul D ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (om n t)) := fun t => by unfold om; lib_ring
  have S1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add u ((ring (4 * n + 3 + 1)).neg D)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (Jcr n ι) := by
    rw [scale, Jcr, mul_sumK]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by
      rw [hsub, chi4_mul n hpr ι hi, rho_mul n hpr ι hi, rho_mul n hpr ι hi, rho_neg1 n hpr ι hi, ← chi_rho n hpr ι hi D]; lib_ring) _
  have S2 : sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add u ((ring (4 * n + 3 + 1)).neg D)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι D) (gconj (Jcr n ι)) := by
    rw [scale, Jcr, gconj_sum, mul_sumK]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by
      rw [hsub, chib_mul n hpr ι hi, rho_mul n hpr ι hi, rho_mul n hpr ι hi, rho_neg1 n hpr ι hi, ← chib_rho n hpr ι hi D,
        gconj_mul, FermatQuartic.gconj_rho n hpr ι hi, ← chib_eq n hpr ι hi]; lib_ring) _
  rw [S1, S2, gconj_mul, gconj_chib n hpr ι hi]

end QuartTwist
#print axioms QuartTwist.chi_rho
#print axioms QuartTwist.chib_rho
#print axioms QuartTwist.gconj_chib
#print axioms QuartTwist.Jcr_eq
#print axioms QuartTwist.quart_twist

#print axioms: 5 theorems, no axioms.

78.70 Cyclotomic number \((0,0)\) of order \(3\)

The Fermat cubic counted through \(\#\{x^3=u\}\in \{1,3,0\}\).

\[ 9\,\#\{a: a,\ 1-a \text { nonzero cubes}\}=p-8+(2a-b). \]

lang/CubicCyclo.lean

/-
  lang/CubicCyclo — T43, cycle 240: Gauss's cyclotomic number (0, 0) of order 3.

  p = 6n + 7, J = a + bω. N(u) = #{x³ = u} ∈ {1, 3, 0}, so #{x³ + y³ = 1} = 3 + 3 + 9 M, M = #{a : a, 1 − a nonzero cubes}; with
  FermatCubic (#{x³ + y³ = 1} = p − 2 + (2a − b)):
      cyclo00_3    9 M = p − 8 + (2a − b)         (Gauss: (0,0)₃ = (p − 8 + L)/9, 4p = L² + 27M², L ≡ 1 mod 3)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.CubicDup
import lang.QuartPrimary

namespace CubicCyclo

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm FermatCubic
open LibRing (castZ castN)

variable (n : Nat)

def Q3 (w a : Zp (6 * n + 5 + 1)) : Int := if chi3 n w a = (1, 0) then 1 else 0

theorem g3_val (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (a : Zp (6 * n + 5 + 1)) : g3 n w a = (delta LibInt.intR a (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w a, 0) := by
  unfold g3 Q3
  rw [CubicSextic.cb_cconj n hpr w hw1 hw3]
  by_cases ha : a = (ring (6 * n + 5 + 1)).zero
  · rw [ha, chi3_zero, ite_neg' _ _ (fun e => Nat.noConfusion (Int.ofNat.inj (congrArg Prod.fst e)))]
    unfold delta; rw [ite_pos' _ _ rfl]; rfl
  · unfold delta; rw [ite_neg' _ _ ha]
    rcases CubicFive.chi_cases n w a with e | e | e | e
    · rw [e, ite_neg' _ _ (fun e' => Nat.noConfusion (Int.ofNat.inj (congrArg Prod.fst e')))]
      exact absurd e (fun e' => by
        have h1 := CubicSextic.rho3_val n hpr a ha
        unfold chi3 at e'; rw [ite_neg' _ _ ha] at e'
        by_cases c1 : pw (ring (6 * n + 5 + 1)) a (2 * n + 2) = (ring (6 * n + 5 + 1)).one
        · rw [ite_pos' _ _ c1] at e'; exact Nat.noConfusion (Int.ofNat.inj (congrArg Prod.fst e'))
        · rw [ite_neg' _ _ c1] at e'
          by_cases c2 : pw (ring (6 * n + 5 + 1)) a (2 * n + 2) = w
          · rw [ite_pos' _ _ c2] at e'; exact Nat.noConfusion (Int.ofNat.inj (congrArg Prod.snd e'))
          · rw [ite_neg' _ _ c2] at e'; exact Int.noConfusion (congrArg Prod.fst e'))
    · rw [e, ite_pos' _ _ rfl]; rfl
    · rw [e, ite_neg' _ _ (fun e' => Nat.noConfusion (Int.ofNat.inj (congrArg Prod.fst e')))]; rfl
    · rw [e, ite_neg' _ _ (fun e' => Int.noConfusion (congrArg Prod.fst e'))]; rfl

theorem Fc_sum (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : Fc n = sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (g3 n w (om1 n a))) (LL n) := by
  unfold Fc
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => step1 n hpr w hw1 hw3 x)]
  exact step2 n hpr w hw1 hw3 (fun a => g3 n w (om1 n a))

theorem sum_fst {A : Type} (g : A → Int × Int) : ∀ l : List A, (sumK (Cyclo3.cyc3 LibInt.intR) g l).1 = sumK LibInt.intR (fun x => (g x).1) l
  | [] => rfl
  | a :: as => by show (g a).1 + (sumK (Cyclo3.cyc3 LibInt.intR) g as).1 = _; rw [sum_fst g as]; rfl

theorem sum_dX (X : Zp (6 * n + 5 + 1) → Int) (z : Zp (6 * n + 5 + 1)) : sumK LibInt.intR (fun a => delta LibInt.intR a z * X a) (LL n) = X z :=
  FiniteRecChain.pick LibInt.intR (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) LibInt.intR) X z

/-- **#{x³ + y³ = 1} = 6 + 9 M.** -/
theorem fc_eq (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    (Fc n).1 = 6 + 9 * sumK LibInt.intR (fun a => Q3 n w a * Q3 n w (om1 n a)) (LL n) := by
  have e0 : (Fc n).1 = sumK LibInt.intR (fun a => (delta LibInt.intR a (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w a) * (delta LibInt.intR (om1 n a) (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w (om1 n a))) (LL n) := by
    rw [Fc_sum n hpr w hw1 hw3, sum_fst]
    exact sumK_ext LibInt.intR _ _ (fun a => by
      rw [g3_val n hpr w hw1 hw3 a, g3_val n hpr w hw1 hw3 (om1 n a)]
      show (delta LibInt.intR a (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w a) * (delta LibInt.intR (om1 n a) (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w (om1 n a)) + -(0 * 0) = _; lib_int) _
  have om0 : om1 n (ring (6 * n + 5 + 1)).zero = (ring (6 * n + 5 + 1)).one := by unfold om1; lib_ring
  have om1' : om1 n (ring (6 * n + 5 + 1)).one = (ring (6 * n + 5 + 1)).zero := by unfold om1; lib_ring
  have hd1 : ∀ a, delta LibInt.intR (om1 n a) (ring (6 * n + 5 + 1)).zero = delta LibInt.intR a (ring (6 * n + 5 + 1)).one := fun a => by
    unfold delta
    by_cases h1 : a = (ring (6 * n + 5 + 1)).one
    · rw [ite_pos' _ _ h1, ite_pos' _ _ (by rw [h1]; exact om1')]
    · rw [ite_neg' _ _ h1, ite_neg' _ _ (fun e => h1 (by rw [← CubicDup.om1_om1 n a, e]; exact om0))]
  have hdd : ∀ a, delta LibInt.intR a (ring (6 * n + 5 + 1)).zero * delta LibInt.intR a (ring (6 * n + 5 + 1)).one = 0 := fun a => by
    unfold delta
    by_cases h0 : a = (ring (6 * n + 5 + 1)).zero
    · rw [ite_pos' _ _ h0, ite_neg' _ _ (fun e => PowerSum.one_ne_zero' (6 * n + 5) (h0 ▸ e).symm)]; rfl
    · rw [ite_neg' _ _ h0]; show 0 * _ = 0; lib_int
  have ex : ∀ a, (delta LibInt.intR a (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w a) * (delta LibInt.intR (om1 n a) (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w (om1 n a))
      = delta LibInt.intR a (ring (6 * n + 5 + 1)).zero * delta LibInt.intR a (ring (6 * n + 5 + 1)).one + 3 * (delta LibInt.intR a (ring (6 * n + 5 + 1)).zero * Q3 n w (om1 n a)) + 3 * (delta LibInt.intR a (ring (6 * n + 5 + 1)).one * Q3 n w a)
        + 9 * (Q3 n w a * Q3 n w (om1 n a)) := fun a => by rw [hd1]; lib_int
  have Q1 : Q3 n w (ring (6 * n + 5 + 1)).one = 1 := by
    unfold Q3; rw [ite_pos' _ _ (by
      have := val_of n hpr w hw1 hw3 (ring (6 * n + 5 + 1)).one (PowerSum.one_ne_zero' _) 0 (Nat.zero_lt_succ 2) (CubicChar.pw_one'' (ring (6 * n + 5 + 1)) _)
      exact this)]
  rw [sumK_ext LibInt.intR _ _ ex, QuartPrimary.sI_add, QuartPrimary.sI_add, QuartPrimary.sI_add, QuartPrimary.sI_mul, QuartPrimary.sI_mul, QuartPrimary.sI_mul,
    sumK_ext LibInt.intR _ _ hdd, show sumK LibInt.intR (fun _ => (0 : Int)) (LL n) = 0 from HeckePrime.zsumR LibInt.intR (LL n), sum_dX n (fun a => Q3 n w (om1 n a)), sum_dX n (Q3 n w), om0, Q1] at e0
  rw [e0]; lib_int

/-- **Gauss's cyclotomic number (0, 0) of order 3**: 9·#{a : a, 1 − a nonzero cubes} = p − 8 + (2a − b), J = a + bω. -/
theorem cyclo00_3 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    9 * sumK LibInt.intR (fun a => Q3 n w a * Q3 n w (om1 n a)) (LL n) = Int.ofNat (6 * n + 5 + 1 + 1) - 8 + (2 * (J n w).1 - (J n w).2) := by
  have e1 := fc_eq n hpr w hw1 hw3
  have e2 := (fermat_hasse n hpr w hw1 hw3).1
  rw [e1] at e2
  calc _ = (6 + 9 * sumK LibInt.intR (fun a => Q3 n w a * Q3 n w (om1 n a)) (LL n)) - 6 := by lib_int
    _ = _ := by rw [e2]; lib_int

/-! ## all nine cyclotomic numbers of order 3 -/

def Iv (w : Zp (6 * n + 5 + 1)) (v : Int × Int) (x : Zp (6 * n + 5 + 1)) : Int := if chi3 n w x = v then 1 else 0

def E (w : Zp (6 * n + 5 + 1)) (v : Int × Int) (x : Zp (6 * n + 5 + 1)) : Int × Int := (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).mul (cconj v) (chi3 n w x))) ((Cyclo3.cyc3 LibInt.intR).mul v (cb n w x))

def CR (v : Int × Int) : Prop := v = (1, 0) ∨ v = (0, 1) ∨ v = (-1, -1)

theorem E_val (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (v : Int × Int) (hv : CR v) (x : Zp (6 * n + 5 + 1)) : E n w v x = (3 * Iv n w v x + delta LibInt.intR x (ring (6 * n + 5 + 1)).zero, 0) := by
  unfold E Iv
  rw [CubicSextic.cb_cconj n hpr w hw1 hw3]
  by_cases hx : x = (ring (6 * n + 5 + 1)).zero
  · rw [hx, chi3_zero]; unfold delta; rw [ite_pos' _ _ rfl]
    rcases hv with e | e | e <;> rw [e] <;> rfl
  · unfold delta; rw [ite_neg' _ _ hx]
    rcases CubicFive.chi_cases n w x with e | e | e | e
    · exfalso
      unfold chi3 at e; rw [ite_neg' _ _ hx] at e
      by_cases c1 : pw (ring (6 * n + 5 + 1)) x (2 * n + 2) = (ring (6 * n + 5 + 1)).one
      · rw [ite_pos' _ _ c1] at e; exact Nat.noConfusion (Int.ofNat.inj (congrArg Prod.fst e))
      · rw [ite_neg' _ _ c1] at e
        by_cases c2 : pw (ring (6 * n + 5 + 1)) x (2 * n + 2) = w
        · rw [ite_pos' _ _ c2] at e; exact Nat.noConfusion (Int.ofNat.inj (congrArg Prod.snd e))
        · rw [ite_neg' _ _ c2] at e; exact Int.noConfusion (congrArg Prod.fst e)
    all_goals (rw [e]; rcases hv with f | f | f <;> rw [f] <;> rfl)

theorem pair_sum {A : Type} (g : A → Int) : ∀ l : List A, sumK (Cyclo3.cyc3 LibInt.intR) (fun x => (g x, (0 : Int))) l = (sumK LibInt.intR g l, 0)
  | [] => rfl
  | a :: as => by show (Cyclo3.cyc3 LibInt.intR).add (g a, 0) (sumK (Cyclo3.cyc3 LibInt.intR) (fun x => (g x, (0 : Int))) as) = _; rw [pair_sum g as]; exact Prod.ext rfl (show (0 : Int) + 0 = 0 from rfl)

theorem sC_neg (f : Zp (6 * n + 5 + 1) → Int × Int) : ∀ l : List (Zp (6 * n + 5 + 1)), sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).neg (f a)) l = (Cyclo3.cyc3 LibInt.intR).neg (sumK (Cyclo3.cyc3 LibInt.intR) f l)
  | [] => by show (Cyclo3.cyc3 LibInt.intR).zero = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).zero; lib_ring
  | a :: as => by show (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (f a)) (sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).neg (f a)) as) = (Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (f a) (sumK (Cyclo3.cyc3 LibInt.intR) f as)); rw [sC_neg f as]; lib_ring

theorem SAB (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w (om1 n a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by
    rw [← JacobiSum.shift1 n (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w (om1 n a)))]
    rw [← D_ne n hpr w hw1 hw3 (ring (6 * n + 5 + 1)).one (PowerSum.one_ne_zero' _)]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun y => by
      show (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one y)) (cb n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one y)))) = _
      rw [show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one y)) = (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg y) by lib_ring, neg_b n hpr w hw1 hw3]) _

theorem SBA (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w (om1 n a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by
    rw [← refl1 n (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w (om1 n a))), ← SAB n hpr w hw1 hw3]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by
      show (Cyclo3.cyc3 LibInt.intR).mul (cb n w (om1 n a)) (chi3 n w (om1 n (om1 n a))) = _
      rw [show om1 n (om1 n a) = a by unfold om1; lib_ring, (Cyclo3.cyc3 LibInt.intR).mul_comm]) _

theorem SBB (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w (om1 n a))) (LL n) = cconj (J n w) := by
    rw [← J2_conj n hpr w hw1 hw3]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by rw [(cb_eq n hpr w hw1 hw3 a).1, (cb_eq n hpr w hw1 hw3 _).1]; rfl) _

/-- **Gauss's cyclotomic numbers of order 3**: for cube roots of unity v, u,
    9·#{a : χ(a) = v, χ(1 − a) = u} = p + v̄ū J + vu J̄ − v̄u − vū − (1 + v̄ + v) − (1 + ū + u). -/
theorem cyclo_vu (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (v u : Int × Int) (hv : CR v) (hu : CR u) :
    (9 * sumK LibInt.intR (fun a => Iv n w v a * Iv n w u (om1 n a)) (LL n), (0 : Int)) =
      (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj v) (cconj u)) (J n w)))
        ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul v u) (cconj (J n w)))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (cconj v) u) ((Cyclo3.cyc3 LibInt.intR).mul v (cconj u)))))
        ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (cconj v)) v))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (cconj u)) u)) := by
  have om0 : om1 n (ring (6 * n + 5 + 1)).zero = (ring (6 * n + 5 + 1)).one := by unfold om1; lib_ring
  have om1' : om1 n (ring (6 * n + 5 + 1)).one = (ring (6 * n + 5 + 1)).zero := by unfold om1; lib_ring
  -- left side as a sum of (E − δ)(E' − δ')
  have hL : (9 * sumK LibInt.intR (fun a => Iv n w v a * Iv n w u (om1 n a)) (LL n), (0 : Int))
      = sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (E n w v a) ((Cyclo3.cyc3 LibInt.intR).neg (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).zero))) ((Cyclo3.cyc3 LibInt.intR).add (E n w u (om1 n a)) ((Cyclo3.cyc3 LibInt.intR).neg (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one)))) (LL n) := by
    rw [← QuartPrimary.sI_mul, ← pair_sum]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by
      rw [E_val n hpr w hw1 hw3 v hv, E_val n hpr w hw1 hw3 u hu]
      have d1 : delta LibInt.intR (om1 n a) (ring (6 * n + 5 + 1)).zero = delta LibInt.intR a (ring (6 * n + 5 + 1)).one := by
        unfold delta
        by_cases h1 : a = (ring (6 * n + 5 + 1)).one
        · rw [ite_pos' _ _ h1, ite_pos' _ _ (by rw [h1]; exact om1')]
        · rw [ite_neg' _ _ h1, ite_neg' _ _ (fun e => h1 (by rw [← CubicDup.om1_om1 n a, e]; exact om0))]
      rw [d1]
      have dC : ∀ z : Zp (6 * n + 5 + 1), delta (Cyclo3.cyc3 LibInt.intR) a z = (delta LibInt.intR a z, 0) := fun z => by
        unfold delta; by_cases hz : a = z
        · rw [ite_pos' _ _ hz, ite_pos' _ _ hz]; rfl
        · rw [ite_neg' _ _ hz, ite_neg' _ _ hz]; rfl
      rw [dC, dC]
      apply Prod.ext
      · show 9 * (Iv n w v a * Iv n w u (om1 n a)) = (3 * Iv n w v a + delta LibInt.intR a (ring (6 * n + 5 + 1)).zero + -delta LibInt.intR a (ring (6 * n + 5 + 1)).zero) * (3 * Iv n w u (om1 n a) + delta LibInt.intR a (ring (6 * n + 5 + 1)).one + -delta LibInt.intR a (ring (6 * n + 5 + 1)).one) + -((0 + -0) * (0 + -0))
        lib_int
      · show (0 : Int) = (3 * Iv n w v a + delta LibInt.intR a (ring (6 * n + 5 + 1)).zero + -delta LibInt.intR a (ring (6 * n + 5 + 1)).zero) * (0 + -0) + (0 + -0) * (3 * Iv n w u (om1 n a) + delta LibInt.intR a (ring (6 * n + 5 + 1)).one + -delta LibInt.intR a (ring (6 * n + 5 + 1)).one) + -((0 + -0) * (0 + -0))
        lib_int) _
  rw [hL]
  have hexp : ∀ a, (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (E n w v a) ((Cyclo3.cyc3 LibInt.intR).neg (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).zero))) ((Cyclo3.cyc3 LibInt.intR).add (E n w u (om1 n a)) ((Cyclo3.cyc3 LibInt.intR).neg (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one)))
      = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one
          ((Cyclo3.cyc3 LibInt.intR).mul (cconj u) (chi3 n w (om1 n a)))) ((Cyclo3.cyc3 LibInt.intR).mul u (cb n w (om1 n a))))
          ((Cyclo3.cyc3 LibInt.intR).mul (cconj v) (chi3 n w a))) ((Cyclo3.cyc3 LibInt.intR).mul v (cb n w a)))
          ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj v) (cconj u)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w (om1 n a)))))
          ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul v u) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w (om1 n a)))))
          ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj v) u) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w (om1 n a)))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul v (cconj u)) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w (om1 n a))))))
        ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).zero) (E n w u (om1 n a)))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one) (E n w v a))))
          ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).zero) (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one))) := fun a => by unfold E; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hexp]
  repeat rw [sumK_add]
  have hp : ∀ (c : Int × Int) (f : Zp (6 * n + 5 + 1) → Int × Int), sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul c (f a)) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul c (sumK (Cyclo3.cyc3 LibInt.intR) f (LL n)) := fun c f => (mul_sumK (Cyclo3.cyc3 LibInt.intR) c f (LL n)).symm
  have hcomp : Complete (Cyclo3.cyc3 LibInt.intR) (LL n) := CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)
  have SA : sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := chi3_sum n hpr w hw1 hw3
  have SB : sumK (Cyclo3.cyc3 LibInt.intR) (cb n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := cb_sum n hpr w hw1 hw3
  have c1 : chi3 n w (ring (6 * n + 5 + 1)).one = (Cyclo3.cyc3 LibInt.intR).one := val_of n hpr w hw1 hw3 (ring (6 * n + 5 + 1)).one (PowerSum.one_ne_zero' _) 0 (Nat.zero_lt_succ 2) (CubicChar.pw_one'' (ring (6 * n + 5 + 1)) _)
  have cb1 : cb n w (ring (6 * n + 5 + 1)).one = (Cyclo3.cyc3 LibInt.intR).one := by rw [CubicSextic.cb_cconj n hpr w hw1 hw3, c1]; rfl
  have E1 : ∀ x : Int × Int, E n w x (ring (6 * n + 5 + 1)).one = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (cconj x)) x := fun x => by unfold E; rw [c1, cb1]; lib_ring
  have T1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun _ : Zp (6 * n + 5 + 1) => (Cyclo3.cyc3 LibInt.intR).one) (LL n) = (Int.ofNat (6 * n + 5 + 1 + 1), 0) := JacobiSum.sum_one n _
  have T2 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cconj u) (chi3 n w (om1 n a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := by rw [hp, refl1 n (chi3 n w), SA]; lib_ring
  have T3 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul u (cb n w (om1 n a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := by rw [hp, refl1 n (cb n w), SB]; lib_ring
  have T4 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cconj v) (chi3 n w a)) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := by rw [hp, SA]; lib_ring
  have T5 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul v (cb n w a)) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := by rw [hp, SB]; lib_ring
  have T6 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj v) (cconj u)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w (om1 n a)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj v) (cconj u)) (J n w) := by rw [hp]; rfl
  have T7 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul v u) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w (om1 n a)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul v u) (cconj (J n w)) := by rw [hp, SBB n hpr w hw1 hw3]
  have T8 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj v) u) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w (om1 n a)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj v) u) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) := by rw [hp, SAB n hpr w hw1 hw3]
  have T9 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul v (cconj u)) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w (om1 n a)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul v (cconj u)) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) := by rw [hp, SBA n hpr w hw1 hw3]
  have T10 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).zero) (E n w u (om1 n a)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (E n w u (ring (6 * n + 5 + 1)).one) := by
    rw [sC_neg, FiniteRecChain.pick (Cyclo3.cyc3 LibInt.intR) (LL n) hcomp (fun a => E n w u (om1 n a)) (ring (6 * n + 5 + 1)).zero, om0]
  have T11 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one) (E n w v a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (E n w v (ring (6 * n + 5 + 1)).one) := by
    rw [sC_neg, FiniteRecChain.pick (Cyclo3.cyc3 LibInt.intR) (LL n) hcomp (E n w v) (ring (6 * n + 5 + 1)).one]
  have T12 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).zero) (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one)) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := by
    rw [FiniteRecChain.pick (Cyclo3.cyc3 LibInt.intR) (LL n) hcomp (fun a => delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one) (ring (6 * n + 5 + 1)).zero]
    unfold delta; exact ite_neg' _ _ (fun e => PowerSum.one_ne_zero' (6 * n + 5) e.symm)
  rw [T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, E1, E1]
  lib_ring

end CubicCyclo
#print axioms CubicCyclo.g3_val
#print axioms CubicCyclo.Fc_sum
#print axioms CubicCyclo.sum_fst
#print axioms CubicCyclo.sum_dX
#print axioms CubicCyclo.fc_eq
#print axioms CubicCyclo.cyclo00_3
#print axioms CubicCyclo.E_val
#print axioms CubicCyclo.pair_sum
#print axioms CubicCyclo.sC_neg
#print axioms CubicCyclo.SAB
#print axioms CubicCyclo.SBA
#print axioms CubicCyclo.SBB
#print axioms CubicCyclo.cyclo_vu

#print axioms: 13 theorems, no axioms.

78.71 Cyclotomic numbers of order \(4\)

The class indicator \(4[\chi (x)=v]+[x=0]=1+\bar v\chi (x)+\bar v^2\rho (x)+v\bar \chi (x)\) against the sixteen Jacobi sums.

\[ 16\,\#\{a:\chi (a)=v,\ \chi (1-a)=u\}=p+\bar v\bar uJ+vu\bar J-\bar v^2\bar u^2-\chi (-1)(\bar vu+v\bar u)+(\bar v\bar u^2+\bar v^2\bar u)K+(v\bar u^2+\bar v^2u)\bar K-\cdots \]

lang/QuartCyclo.lean

/-
  lang/QuartCyclo — T43, cycle 242: all sixteen cyclotomic numbers of order 4.

  p = 4n + 5, χ quartic, ρ = χ², J = J(χ,χ), K = J(ρ,χ) = χ(−1)J (QuartDup). For fourth roots of unity v, u:
      4[χ(x) = v] + [x = 0] = 1 + v̄χ(x) + v̄²ρ(x) + vχ̄(x)      (E4_val)
      cyclo4_vu   16·#{a : χ(a) = v, χ(1 − a) = u} = p + v̄ūJ + vuJ̄ − v̄²ū² − χ(−1)(v̄u + vū) + (v̄ū² + v̄²ū)K + (vū² + v̄²u)K̄
                                                     − (1 + v̄ + v̄² + v) − (1 + ū + ū² + u)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FermatQuartic

namespace QuartCyclo

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup FermatQuartic
open LibRing (castZ castN)

variable (n : Nat)

def Iv (ι : Zp (4 * n + 3 + 1)) (v : Int × Int) (x : Zp (4 * n + 3 + 1)) : Int := if chi4 n ι x = v then 1 else 0

def E4 (ι : Zp (4 * n + 3 + 1)) (v : Int × Int) (x : Zp (4 * n + 3 + 1)) : Int × Int :=
  (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (chi4 n ι x))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (rho n ι x))) ((Cyclo4.cyc4 LibInt.intR).mul v (chib n ι x))

def R4 (v : Int × Int) : Prop := v = (1, 0) ∨ v = (0, 1) ∨ v = (-1, 0) ∨ v = (0, -1)

theorem E4_val (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (v : Int × Int) (hv : R4 v) (x : Zp (4 * n + 3 + 1)) :
    E4 n ι v x = (4 * Iv n ι v x + delta LibInt.intR x (ring (4 * n + 3 + 1)).zero, 0) := by
  unfold E4 Iv rho
  rw [chib_eq n hpr ι hi]
  by_cases hx : x = (ring (4 * n + 3 + 1)).zero
  · rw [hx, chi4_zero]; unfold delta; rw [ite_pos' _ _ rfl]
    rcases hv with e | e | e | e <;> rw [e] <;> rfl
  · unfold delta; rw [ite_neg' _ _ hx]
    rcases ResidueCriterion.chi4_cases n ι x with e | e | e | e | e
    · exact absurd e (chi4_ne n hpr ι hi x hx)
    all_goals (rw [e]; rcases hv with f | f | f | f <;> rw [f] <;> rfl)

theorem pair_sum {A : Type} (g : A → Int) : ∀ l : List A, sumK (Cyclo4.cyc4 LibInt.intR) (fun x => (g x, (0 : Int))) l = (sumK LibInt.intR g l, 0)
  | [] => rfl
  | a :: as => by show (Cyclo4.cyc4 LibInt.intR).add (g a, 0) (sumK (Cyclo4.cyc4 LibInt.intR) (fun x => (g x, (0 : Int))) as) = _; rw [pair_sum g as]; exact Prod.ext rfl (show (0 : Int) + 0 = 0 from rfl)

theorem sC_neg (f : Zp (4 * n + 3 + 1) → Int × Int) : ∀ l : List (Zp (4 * n + 3 + 1)), sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).neg (f a)) l = (Cyclo4.cyc4 LibInt.intR).neg (sumK (Cyclo4.cyc4 LibInt.intR) f l)
  | [] => by show (Cyclo4.cyc4 LibInt.intR).zero = (Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).zero; lib_ring
  | a :: as => by show (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (f a)) (sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).neg (f a)) as) = (Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add (f a) (sumK (Cyclo4.cyc4 LibInt.intR) f as)); rw [sC_neg f as]; lib_ring

/-- **Gauss's cyclotomic numbers of order 4.** -/
theorem cyclo4_vu (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (v u : Int × Int) (hv : R4 v) (hu : R4 u) :
    (16 * sumK LibInt.intR (fun a => Iv n ι v a * Iv n ι u (om n a)) (LL n), (0 : Int)) =
      (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0)
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj u)) (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v u) (gconj (Jq n (chi4 n ι)))))
        ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u)))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) u) ((Cyclo4.cyc4 LibInt.intR).mul v (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (gconj u))) (J2 n ι)))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul v ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) u)) (gconj (J2 n ι))))
        ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one (gconj v)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v))) v)))
        ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) u)) := by
  have om0 : om n (ring (4 * n + 3 + 1)).zero = (ring (4 * n + 3 + 1)).one := by unfold om; lib_ring
  have om1' : om n (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by unfold om; lib_ring
  have hL : (16 * sumK LibInt.intR (fun a => Iv n ι v a * Iv n ι u (om n a)) (LL n), (0 : Int))
      = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (E4 n ι v a) ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero))) ((Cyclo4.cyc4 LibInt.intR).add (E4 n ι u (om n a)) ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one)))) (LL n) := by
    rw [← QuartPrimary.sI_mul, ← pair_sum]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by
      rw [E4_val n hpr ι hi v hv, E4_val n hpr ι hi u hu]
      have d1 : delta LibInt.intR (om n a) (ring (4 * n + 3 + 1)).zero = delta LibInt.intR a (ring (4 * n + 3 + 1)).one := by
        unfold delta
        by_cases h1 : a = (ring (4 * n + 3 + 1)).one
        · rw [ite_pos' _ _ h1, ite_pos' _ _ (by rw [h1]; exact om1')]
        · rw [ite_neg' _ _ h1, ite_neg' _ _ (fun e => h1 (by rw [← om_om n a, e]; exact om0))]
      rw [d1]
      have dC : ∀ z : Zp (4 * n + 3 + 1), delta (Cyclo4.cyc4 LibInt.intR) a z = (delta LibInt.intR a z, 0) := fun z => by
        unfold delta; by_cases hz : a = z
        · rw [ite_pos' _ _ hz, ite_pos' _ _ hz]; rfl
        · rw [ite_neg' _ _ hz, ite_neg' _ _ hz]; rfl
      rw [dC, dC]
      apply Prod.ext
      · show 16 * (Iv n ι v a * Iv n ι u (om n a)) = (4 * Iv n ι v a + delta LibInt.intR a (ring (4 * n + 3 + 1)).zero + -delta LibInt.intR a (ring (4 * n + 3 + 1)).zero) * (4 * Iv n ι u (om n a) + delta LibInt.intR a (ring (4 * n + 3 + 1)).one + -delta LibInt.intR a (ring (4 * n + 3 + 1)).one) + -((0 + -0) * (0 + -0))
        lib_int
      · show (0 : Int) = (4 * Iv n ι v a + delta LibInt.intR a (ring (4 * n + 3 + 1)).zero + -delta LibInt.intR a (ring (4 * n + 3 + 1)).zero) * (0 + -0) + (0 + -0) * (4 * Iv n ι u (om n a) + delta LibInt.intR a (ring (4 * n + 3 + 1)).one + -delta LibInt.intR a (ring (4 * n + 3 + 1)).one)
        lib_int) _
  rw [hL]
  -- expand into the sixteen sums and the boundary terms
  have hexp : ∀ a, (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (E4 n ι v a) ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero))) ((Cyclo4.cyc4 LibInt.intR).add (E4 n ι u (om n a)) ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one)))
      = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (chi4 n ι (om n a))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u)) (rho n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul u (chib n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (chi4 n ι a)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (rho n ι a))) ((Cyclo4.cyc4 LibInt.intR).mul v (chib n ι a))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) u) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a))))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (ch  -- (+20 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) u) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a))  -- (+4 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero) (E4 n ι u (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one) (E4 n ι v a))))
        ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero) (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one)) := fun a => by unfold E4; lib_ring
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hexp]
  repeat rw [sumK_add]
  have hp : ∀ (c : Int × Int) (f : Zp (4 * n + 3 + 1) → Int × Int), sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul c (f a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul c (sumK (Cyclo4.cyc4 LibInt.intR) f (LL n)) := fun c f => (mul_sumK (Cyclo4.cyc4 LibInt.intR) c f (LL n)).symm
  have hcomp : Complete (Cyclo4.cyc4 LibInt.intR) (LL n) := CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)
  have SA : sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chi4_sum n hpr ι hi
  have SR : sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := rho_sum n hpr ι hi
  have SB : sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chib_sum n hpr ι hi
  have c1 : chi4 n ι (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one := chi_one n hpr ι hi
  have r1 : rho n ι (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one := rho_one n hpr ι hi
  have b1 : chib n ι (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).one := chib_one n hpr ι hi
  have E1 : ∀ x : Int × Int, E4 n ι x (ring (4 * n + 3 + 1)).one = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one (gconj x)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj x) (gconj x))) x := fun x => by unfold E4; rw [c1, r1, b1]; lib_ring
  have T1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := sum_oneq n _
  have Tu1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (gconj u) (chi4 n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by rw [hp, refl_om n (chi4 n ι), SA]; lib_ring
  have Tu2 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u)) (rho n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by rw [hp, refl_om n (rho n ι), SR]; lib_ring
  have Tu3 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul u (chib n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by rw [hp, refl_om n (chib n ι), SB]; lib_ring
  have Tv1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (gconj v) (chi4 n ι a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by rw [hp, SA]; lib_ring
  have Tv2 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (rho n ι a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by rw [hp, SR]; lib_ring
  have Tv3 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul v (chib n ι a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by rw [hp, SB]; lib_ring
  have TAA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj u)) (Jq n (chi4 n ι)) := by rw [hp]; rfl
  have TBB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v u) (gconj (Jq n (chi4 n ι))) := by
    rw [hp, ← Jb_conj n hpr ι hi]; rfl
  have TRR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gcon  -- (+64 chars; full line in the Zenodo source)
    rw [hp, J_rho n hpr ι hi]; lib_ring
  have SAB := FermatQuartic.T13 n hpr ι hi
  have SBA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := by
    rw [← SAB, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have TAB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) u) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) u) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR  -- (+56 chars; full line in the Zenodo source)
  have TBA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR  -- (+56 chars; full line in the Zenodo source)
  have SRA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι (om n a))) (LL n) = J2 n ι := rfl
  have SAR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a))) (LL n) = J2 n ι := by
    rw [← SRA, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have SRB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a))) (LL n) = gconj (J2 n ι) := by
    unfold J2; rw [gconj_sum]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [gconj_mul, gconj_rho n hpr ι hi, chib_eq n hpr ι hi]) _
  have SBR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a))) (LL n) = gconj (J2 n ι) := by
    rw [← SRB, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have TAR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) (J2 n ι) := by rw [hp, S  -- (+3 chars; full line in the Zenodo source)
  have TRA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (gconj u)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) (gconj u)) (J2 n ι) := by rw [hp];   -- (+3 chars; full line in the Zenodo source)
  have TBR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul v ((Cyclo4.cyc4 LibInt.intR).mul (gconj u) (gconj u))) (gconj (J2 n ι)) := by rw [hp, SBR]
  have TRB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) u) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj v) (gconj v)) u) (gconj (J2 n ι)) := by rw [hp, SRB]
  have T10 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero) (E4 n ι u (om n a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg (E4 n ι u (ring (4 * n + 3 + 1)).one) := by
    rw [sC_neg, FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) hcomp (fun a => E4 n ι u (om n a)) (ring (4 * n + 3 + 1)).zero, om0]
  have T11 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one) (E4 n ι v a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg (E4 n ι v (ring (4 * n + 3 + 1)).one) := by
    rw [sC_neg, FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) hcomp (E4 n ι v) (ring (4 * n + 3 + 1)).one]
  have T12 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero) (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
    rw [FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) hcomp (fun a => delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one) (ring (4 * n + 3 + 1)).zero]
    unfold delta; exact ite_neg' _ _ (fun e => PowerSum.one_ne_zero' (4 * n + 3) e.symm)
  rw [T1, Tu1, Tu2, Tu3, Tv1, Tv2, Tv3, TAA, TBB, TRR, TAB, TBA, TAR, TRA, TBR, TRB, T10, T11, T12, E1, E1]
  lib_ring

end QuartCyclo
#print axioms QuartCyclo.E4_val
#print axioms QuartCyclo.pair_sum
#print axioms QuartCyclo.sC_neg
#print axioms QuartCyclo.cyclo4_vu

#print axioms: 4 theorems, no axioms.

78.72 The quartic curves \(y^2=x^4+D\)

Fourth roots counted by \(1+\chi +\rho +\bar \chi \) and the substitution \(u=-Dt\).

\[ \sum _x\rho (x^4+D)=-1+\rho (D)\,\mathrm {Tr}\big (\chi (-D)J(\chi ,\rho )\big ). \]

lang/QuartQuartic.lean

/-
  lang/QuartQuartic — T43, cycle 243: the trace of y² = x⁴ + D at p ≡ 1 (mod 4).

  p = 4n + 5. #{x⁴ = u} = 1 + χ(u) + ρ(u) + χ̄(u) (FermatQuartic.q4_count), and u = −Dt:
      quartic_sum   Σ_x ρ(x⁴ + D) = −1 + ρ(D)(z + z̄),  z = χ(−D) J(χ, ρ)        (D ≠ 0)
  (J(ρ, ρ) = −1 gives the constant; J(χ, ρ) = χ(−1)J by QuartDup.)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.QuartTwist
import lang.QuartTwistCount

namespace QuartQuartic

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup FermatQuartic QuartTwist
open LibRing (castZ castN)

variable (n : Nat)

def S4 (ι D : Zp (4 * n + 3 + 1)) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun x => rho n ι ((ring (4 * n + 3 + 1)).add (q4 n x) D)) (LL n)

theorem rho_unit (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero) :
    (Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).neg D)) (rho n ι D) = (Cyclo4.cyc4 LibInt.intR).one := by
  rw [show (ring (4 * n + 3 + 1)).neg D = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) D by lib_ring, rho_mul n hpr ι hi, rho_neg1 n hpr ι hi]
  rcases QuartPrimary.rho_real n hpr ι hi D hD with e | e <;> rw [e] <;> rfl

/-- **Σ_x ρ(x⁴ + D) = −1 + ρ(D)(z + z̄), z = χ(−D) J(χ, ρ).** -/
theorem quartic_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero) :
    S4 n ι D = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (Jcr n ι)) (gconj ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (Jcr n ι))))) := by
  have hD' : (ring (4 * n + 3 + 1)).neg D ≠ (ring (4 * n + 3 + 1)).zero := fun e => hD (by calc D = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg D) := by lib_ring
                                                         _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  unfold S4
  rw [FermatQuartic.step2 n hpr ι hi (fun a => rho n ι ((ring (4 * n + 3 + 1)).add a D))]
  have hexp : ∀ a, (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (rho n ι ((ring (4 * n + 3 + 1)).add a D))
      ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D)))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D)))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D))) := fun a => by unfold g4; lib_ring
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hexp, sumK_add, sumK_add, sumK_add]
  have S0 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => rho n ι ((ring (4 * n + 3 + 1)).add a D)) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
    rw [FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (fun a => (ring (4 * n + 3 + 1)).add a D) (fun a => (ring (4 * n + 3 + 1)).add a ((ring (4 * n + 3 + 1)).neg D)) (fun x => by lib_ring) (fun y => by lib_ring) (rho n ι)]
    exact rho_sum n hpr ι hi
  have scale : ∀ F : Zp (4 * n + 3 + 1) → Int × Int, sumK (Cyclo4.cyc4 LibInt.intR) F (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun t => F ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg D) t)) (LL n) := fun F =>
    (CharSumGen.sum_scale (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr F ((ring (4 * n + 3 + 1)).neg D) hD').symm
  have hsub : ∀ t, (ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg D) t) D = (ring (4 * n + 3 + 1)).mul D (om n t) := fun t => by unfold om; lib_ring
  have S1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (rho n ι D)) (Jcr n ι) := by
    rw [scale, Jcr, mul_sumK]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by rw [hsub, chi4_mul n hpr ι hi, rho_mul n hpr ι hi]; lib_ring) _
  have S2 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D))) (LL n) = (Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one := by
    rw [scale, ← rho_unit n hpr ι hi D hD]
    rw [show (Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).neg D)) (rho n ι D)) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι ((ring (4 * n + 3 + 1)).neg D)) (rho n ι D)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) by lib_ring,
      ← J_rho n hpr ι hi, mul_sumK]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by rw [hsub, rho_mul n hpr ι hi, rho_mul n hpr ι hi]; lib_ring) _
  have S3 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add a D))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι ((ring (4 * n + 3 + 1)).neg D)) (rho n ι D)) (gconj (Jcr n ι)) := by
    rw [scale, Jcr, gconj_sum, mul_sumK]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun t => by
      rw [hsub, chib_mul n hpr ι hi, rho_mul n hpr ι hi, gconj_mul, FermatQuartic.gconj_rho n hpr ι hi, ← chib_eq n hpr ι hi]; lib_ring) _
  rw [S0, S1, S2, S3, gconj_mul, ← gconj_chib n hpr ι hi ((ring (4 * n + 3 + 1)).neg D)]
  have hc : gconj (gconj (chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) = chi4 n ι ((ring (4 * n + 3 + 1)).neg D) := Prod.ext rfl (by show -(-(chi4 n ι ((ring (4 * n + 3 + 1)).neg D)).2) = _; lib_int)
  rw [chib_eq n hpr ι hi ((ring (4 * n + 3 + 1)).neg D), hc]
  lib_ring

/-- **Hasse's bound for y² = x⁴ + D**: (Σ_x ρ(x⁴ + D) + 1)² ≤ 4p. -/
theorem quartic_hasse (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero) :
    ∃ s : Nat, ((S4 n ι D).1 + 1) * ((S4 n ι D).1 + 1) + Int.ofNat s = 4 * Int.ofNat (4 * n + 3 + 1 + 1) := by
  have hD' : (ring (4 * n + 3 + 1)).neg D ≠ (ring (4 * n + 3 + 1)).zero := fun e => hD (by calc D = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg D) := by lib_ring
                                                         _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  rw [quartic_sum n hpr ι hi D hD, Jcr_eq n hpr ι hi]
  have hN := quart_norm n hpr ι hi
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' (4 * n + 3) (by
    calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  have h1 := (QuartTwistCount.unit_norm n hpr ι hi _ hD').2
  have h2 := (QuartTwistCount.unit_norm n hpr ι hi _ m1).2
  have hm := QuartTwistCount.norm_mul4 (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)))
  rw [QuartTwistCount.norm_mul4 (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)), h1, h2, hN] at hm
  have hr : rho n ι D = (1, 0) ∨ rho n ι D = (-1, 0) := QuartPrimary.rho_real n hpr ι hi D hD
  generalize (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι))) = z at hm ⊢
  match StrictHasse.sq_nat z.2 with
  | ⟨k, hk⟩ =>
    refine ⟨4 * (k * k), ?_⟩
    have e1 : Int.ofNat (4 * (k * k)) = 4 * (z.2 * z.2) := by rw [hk]; rfl
    rw [e1, show 4 * Int.ofNat (4 * n + 3 + 1 + 1) = 4 * (1 * (1 * Int.ofNat (4 * n + 3 + 1 + 1))) by lib_int, ← hm]
    rcases hr with e | e <;> rw [e]
    · show (-1 + (1 * (z.1 + z.1) + -(0 * (z.2 + -z.2))) + 1) * (-1 + (1 * (z.1 + z.1) + -(0 * (z.2 + -z.2))) + 1) + 4 * (z.2 * z.2) = 4 * (z.1 * z.1 + z.2 * z.2)
      lib_int
    · show (-1 + (-1 * (z.1 + z.1) + -(0 * (z.2 + -z.2))) + 1) * (-1 + (-1 * (z.1 + z.1) + -(0 * (z.2 + -z.2))) + 1) + 4 * (z.2 * z.2) = 4 * (z.1 * z.1 + z.2 * z.2)
      lib_int

/-- **y² = x⁴ + D and y² = x³ − 4Dx have the same trace**: Σ_x ρ(x⁴ + D) + 1 = Σ_x ρ(x³ − 4Dx). -/
theorem quartic_cubic (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero) :
    (Cyclo4.cyc4 LibInt.intR).add (S4 n ι D) (Cyclo4.cyc4 LibInt.intR).one = Sq n ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D) ι := by
  have h2 := mtwo_ne n
  have h4 : (ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n) ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact h2 h
    | inr h => exact h2 h
  have h4D : (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact h4 h
    | inr h => exact hD h
  rw [quartic_sum n hpr ι hi D hD, quart_twist n hpr ι hi _ h4D]
  -- the two coefficients agree: ρ(D)χ(−D) = χ(−1)χ̄(D) = χ̄(4D)
  have hs4 : chi4 n ι ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
    rw [chi4_mul n hpr ι hi]; exact QuartPrimary.rho_two n hpr ι hi
  have breal : chib n ι ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
    rw [chib_eq n hpr ι hi, hs4]; rcases FermatQuartic.m1_real n hpr ι hi with e | e <;> rw [e] <;> rfl
  have hc : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) = chib n ι ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D) := by
    rw [chib_mul n hpr ι hi, breal, show (ring (4 * n + 3 + 1)).neg D = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) D by lib_ring, chi4_mul n hpr ι hi]
    calc _ = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι D) (rho n ι D)) := by lib_ring
      _ = _ := by rw [QuartTwist.chi_rho n hpr ι hi D]
  have rreal : gconj (rho n ι D) = rho n ι D := FermatQuartic.gconj_rho n hpr ι hi D
  rw [← hc]
  have g1 : gconj ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) (Jcr n ι)) = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (gconj ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (Jcr n ι))) := by
    rw [gconj_mul, gconj_mul, gconj_mul, rreal]; lib_ring
  rw [g1]
  lib_ring

/-- **2-isogeny**: y² = x³ − Dx and y² = x³ + 4Dx have the same trace. -/
theorem isog2 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero) :
    Sq n D ι = Sq n ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D)) ι := by
  have h2 := mtwo_ne n
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have h4D : (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D) ≠ (ring (4 * n + 3 + 1)).zero := fun e => nz _ _ (nz _ _ h2 h2) hD (by
    calc _ = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D)) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  rw [quart_twist n hpr ι hi D hD, quart_twist n hpr ι hi _ h4D]
  have hs4 : chi4 n ι ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
    rw [chi4_mul n hpr ι hi]; exact QuartPrimary.rho_two n hpr ι hi
  have hss : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring]; exact chi_one n hpr ι hi
  have breal : ∀ x, chi4 n ι x = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) → chib n ι x = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := fun x hx => by
    rw [chib_eq n hpr ι hi, hx]; rcases FermatQuartic.m1_real n hpr ι hi with e | e <;> rw [e] <;> rfl
  have hc : chib n ι ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D)) = chib n ι D := by
    rw [show (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n)) D) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).mul (mtwo n) (mtwo n))) D by lib_ring,
      chib_mul n hpr ι hi, chib_mul n hpr ι hi, breal _ rfl, breal _ hs4, hss]; lib_ring
  rw [hc]

/-- **The four quartic twists cancel**: if χ̄(v) = i then Σ_{k=0}^{3} Σ_x ρ(x³ − v^k D x) = 0. -/
theorem twist4_cancel (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chib n ι v = ((0 : Int), (1 : Int))) :
    (Cyclo4.cyc4 LibInt.intR).add (Sq n D ι) ((Cyclo4.cyc4 LibInt.intR).add (Sq n ((ring (4 * n + 3 + 1)).mul v D) ι) ((Cyclo4.cyc4 LibInt.intR).add (Sq n ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul v v) D) ι)
      (Sq n ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul v v) v) D) ι))) = (Cyclo4.cyc4 LibInt.intR).zero := by
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have h2 := nz _ _ hv0 hv0
  have h3 := nz _ _ h2 hv0
  rw [quart_twist n hpr ι hi D hD, quart_twist n hpr ι hi _ (nz _ _ hv0 hD), quart_twist n hpr ι hi _ (nz _ _ h2 hD),
    quart_twist n hpr ι hi _ (nz _ _ h3 hD), chib_mul n hpr ι hi v D, chib_mul n hpr ι hi ((ring (4 * n + 3 + 1)).mul v v) D,
    chib_mul n hpr ι hi ((ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).mul v v) v) D, chib_mul n hpr ι hi ((ring (4 * n + 3 + 1)).mul v v) v, chib_mul n hpr ι hi v v, hv]
  have m1 : (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (((0 : Int), (1 : Int))) = (((-1 : Int)), ((0 : Int))) := rfl
  have m2 : (Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (((0 : Int), (1 : Int))) = (((0 : Int)), ((-1 : Int))) := rfl
  rw [m1, m2]
  generalize chib n ι D = c
  repeat rw [gconj_mul]
  have E : (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).add (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).add (((-1 : Int)), ((0 : Int))) (((0 : Int)), ((-1 : Int))))) = (Cyclo4.cyc4 LibInt.intR).zero := rfl
  have E' : (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).add (gconj (((0 : Int), (1 : Int)))) ((Cyclo4.cyc4 LibInt.intR).add (gconj (((-1 : Int)), ((0 : Int)))) (gconj (((0 : Int)), ((-1 : Int)))))) = (Cyclo4.cyc4 LibInt.intR).zero := rfl
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul c (Jcr n ι)) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).add (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).add (((-1 : Int)), ((0 : Int))) (((0 : Int)), ((-1 : Int)))))))
          ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj c) (gconj (Jcr n ι))) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).add (gconj (((0 : Int), (1 : Int)))) ((Cyclo4.cyc4 LibInt.intR).add (gconj (((-1 : Int)), ((0 : Int)))) (gconj (((0 : Int)), ((-1 : Int)))))))) := by
        generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; generalize (((0 : Int)), ((-1 : Int))) = d; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).zero := by rw [E, E']; lib_ring

/-- **The four quartic twists of y² = x⁴ + D**: if χ(v) = i, ρ(v) = −1 then Σ_{k=0}^{3} Σ_x ρ(x⁴ + v^k D) = −4. -/
theorem quartic4_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chi4 n ι v = (((0 : Int), (1 : Int)))) (hr : rho n ι v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι ((ring (4 * n + 3 + 1)).mul v (D))) ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)))) (S4 n ι ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))))))) = (((-4 : Int)), ((0 : Int))) := by
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (4 * n + 3 + 1)).mul v (D) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  rw [quartic_sum n hpr ι hi _ n0, quartic_sum n hpr ι hi _ n1, quartic_sum n hpr ι hi _ n2, quartic_sum n hpr ι hi _ n3]
  have hc : ∀ E : Zp (4 * n + 3 + 1), chi4 n ι ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul v E)) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi4 n ι ((ring (4 * n + 3 + 1)).neg E)) := fun E => by
    rw [show (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul v E) = (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).neg E) by lib_ring, chi4_mul n hpr ι hi, hv]
  have hρ : ∀ E : Zp (4 * n + 3 + 1), rho n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho n ι E) := fun E => by rw [rho_mul n hpr ι hi, hr]
  repeat rw [hc]
  repeat rw [hρ]
  generalize chi4 n ι ((ring (4 * n + 3 + 1)).neg D) = c
  generalize rho n ι D = r
  repeat rw [gconj_mul]
  have EP : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibI  -- (+831 chars; full line in the Zenodo source)
  have EQ : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (((0 : Int), (1 : Int)))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.c  -- (+879 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 L  -- (+1306 chars; full line in the Zenodo source)
          ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj c) (gconj (Jcr n ι))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibI  -- (+939 chars; full line in the Zenodo source)
        generalize (gconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one))) := by rw [EP, EQ]; lib_ring
    _ = (((-4 : Int)), ((0 : Int))) := rfl

theorem gnorm (a b : Int) : (Cyclo4.cyc4 LibInt.intR).mul (a, b) (gconj (a, b)) = (a * a + b * b, 0) := by
  apply Prod.ext
  · show a * a + -(b * -b) = a * a + b * b
    lib_int
  · show a * -b + b * a = 0
    lib_int

/-- **Second moment over the quartic twists**: χ̄(v) = i ⇒ Σ_{k=0}^{3} (Σ_x ρ(x³ − v^k D x))² = 8p. -/
theorem quart_moment (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chib n ι v = (((0 : Int), (1 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Sq n (D) ι) (Sq n (D) ι)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι) (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ι) (Sq n ((ring (4 * n   -- (+891 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (4 * n + 3 + 1)).mul v (D) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  have hbb : (Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (gconj (chib n ι D)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [show (chib n ι D) = ((chib n ι D).1, (chib n ι D).2) from rfl, gnorm, (QuartTwistCount.unit_norm n hpr ι hi D hD).1]; rfl
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by
      calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
        _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring
    exact PowerSum.one_ne_zero' (4 * n + 3) this
  have hJJ : (Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (gconj (Jcr n ι)) = (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) := by
    rw [show (Jcr n ι) = ((Jcr n ι).1, (Jcr n ι).2) from rfl, gnorm, Jcr_eq n hpr ι hi, QuartTwistCount.norm_mul4,
      (QuartTwistCount.unit_norm n hpr ι hi _ m1).2, quart_norm n hpr ι hi]
    show (1 * Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = _
    rw [Int.one_mul]
  rw [quart_twist n hpr ι hi _ n0, quart_twist n hpr ι hi _ n1, quart_twist n hpr ι hi _ n2, quart_twist n hpr ι hi _ n3]
  have hbv : ∀ E : Zp (4 * n + 3 + 1), chib n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chib n ι E) := fun E => by rw [chib_mul n hpr ι hi, hv]
  repeat rw [hbv]
  repeat rw [gconj_mul]
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.  -- (+813 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (((0 : Int), (1 : Int)))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4  -- (+1052 chars; full line in the Zenodo source)
  have E3 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (((0 : Int), (1 : Int)))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (((0 : Int), (1 : Int)))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyc  -- (+909 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (chib n ι D)) ((Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (Jcr n ι))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).m  -- (+978 chars; full line in the Zenodo source)
          ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (gconj (chib n ι D))) ((Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (gconj (Jcr n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (gconj (chib n ι D))) ((Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (  -- (+1232 chars; full line in the Zenodo source)
            ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (chib n ι D))) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (Jcr n ι)) (gconj (Jcr n ι)))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR  -- (+1084 chars; full line in the Zenodo source)
        generalize (gconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4  -- (+195 chars; full line in the Zenodo source)
        rw [E1, E2, E3, hbb, hJJ]; generalize (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = q; lib_ring

/-- **Fourth moment over the quartic twists**: χ̄(v) = i ⇒ Σ_{k=0}^{3} a_k⁴ = 24p² + 4(J⁴ + J̄⁴), a_k = Σ_x ρ(x³ − v^k D x). -/
theorem quart_moment4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chib n ι v = (((0 : Int), (1 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Sq n (D) ι) (Sq n (D) ι)) ((Cyclo4.cyc4 LibInt.intR).mul (Sq n (D) ι) (Sq n (D) ι))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι) (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι)) ((Cyclo4.cyc4 LibInt.intR).mul (Sq n  -- (+6111 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (4 * n + 3 + 1)).mul v (D) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  have hbb : (Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) (gconj (chib n ι D)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [show (chib n ι D) = ((chib n ι D).1, (chib n ι D).2) from rfl, gnorm, (QuartTwistCount.unit_norm n hpr ι hi D hD).1]; rfl
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by
      calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
        _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring
    exact PowerSum.one_ne_zero' (4 * n + 3) this
  have hJJ : (Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (gconj (Jcr n ι)) = (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) := by
    rw [show (Jcr n ι) = ((Jcr n ι).1, (Jcr n ι).2) from rfl, gnorm, Jcr_eq n hpr ι hi, QuartTwistCount.norm_mul4,
      (QuartTwistCount.unit_norm n hpr ι hi _ m1).2, quart_norm n hpr ι hi]
    show (1 * Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = _
    rw [Int.one_mul]
  have hb4 : (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((chib n ι D)) ((chib n ι D))) ((Cyclo4.cyc4 LibInt.intR).mul ((chib n ι D)) ((chib n ι D))) = (Cyclo4.cyc4 LibInt.intR).one ∧ (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (chib n ι D))) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (chib n ι D))) = (Cyclo4.cyc4 LibInt  -- (+16 chars; full line in the Zenodo source)
    rw [chib_eq n hpr ι hi]
    rcases ResidueCriterion.chi4_cases n ι D with e | e | e | e | e
    · exact absurd e (chi4_ne n hpr ι hi D hD)
    all_goals (rw [e]; exact ⟨rfl, rfl⟩)
  rw [quart_twist n hpr ι hi _ n0, quart_twist n hpr ι hi _ n1, quart_twist n hpr ι hi _ n2, quart_twist n hpr ι hi _ n3]
  have hbv : ∀ E : Zp (4 * n + 3 + 1), chib n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chib n ι E) := fun E => by rw [chib_mul n hpr ι hi, hv]
  repeat rw [hbv]
  repeat rw [gconj_mul]
  have E0 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4  -- (+2648 chars; full line in the Zenodo source)
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 Li  -- (+2153 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int  -- (+2296 chars; full line in the Zenodo source)
  have E3 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int  -- (+2057 chars; full line in the Zenodo source)
  have E4 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4  -- (+2456 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (chib n ι D))) ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (chib n ι D)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (Jcr n ι)) (gconj (Jcr n ι))) ((Cyclo4.cyc4 LibI  -- (+18694 chars; full line in the Zenodo source)
        generalize (gconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (I  -- (+4683 chars; full line in the Zenodo source)
        rw [E0, E1, E2, E3, E4, hbb, hJJ, hb4.1, hb4.2]; generalize (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = q; lib_ring

/-- **Third moment over the quartic twists vanishes**: χ̄(v) = i ⇒ Σ_{k=0}^{3} (Σ_x ρ(x³ − v^k D x))³ = 0. -/
theorem quart_moment3 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chib n ι v = (((0 : Int), (1 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Sq n (D) ι) (Sq n (D) ι)) (Sq n (D) ι)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι) (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι)) (Sq n ((ring (4 * n + 3 + 1)).mul v (D)) ι)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyc  -- (+706 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (4 * n + 3 + 1)).mul v (D) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  rw [quart_twist n hpr ι hi _ n0, quart_twist n hpr ι hi _ n1, quart_twist n hpr ι hi _ n2, quart_twist n hpr ι hi _ n3]
  have hbv : ∀ E : Zp (4 * n + 3 + 1), chib n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chib n ι E) := fun E => by rw [chib_mul n hpr ι hi, hv]
  repeat rw [hbv]
  repeat rw [gconj_mul]
  have E0 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.  -- (+1811 chars; full line in the Zenodo source)
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 Li  -- (+1507 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 Li  -- (+1459 chars; full line in the Zenodo source)
  have E3 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int  -- (+1667 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (Jcr n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (Jcr n ι)))) (((Cyclo4.cyc4 LibInt.intR).mul (gconj (chib n ι D)) (gconj (Jcr n ι)))))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4   -- (+9886 chars; full line in the Zenodo source)
        generalize (gconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).zero := by rw [E0, E1, E2, E3]; lib_ring

end QuartQuartic
#print axioms QuartQuartic.rho_unit
#print axioms QuartQuartic.quartic_sum
#print axioms QuartQuartic.quartic_hasse
#print axioms QuartQuartic.quartic_cubic
#print axioms QuartQuartic.isog2
#print axioms QuartQuartic.twist4_cancel
#print axioms QuartQuartic.quartic4_sum
#print axioms QuartQuartic.gnorm
#print axioms QuartQuartic.quart_moment
#print axioms QuartQuartic.quart_moment4
#print axioms QuartQuartic.quart_moment3

#print axioms: 11 theorems, no axioms.

78.73 The genus-two curves \(y^2=x^6+D\)

Sixth roots counted by \((1+\rho )(1+\chi +\bar \chi )\), then two Jacobi sums.

\[ \sum _x\rho (x^6+D)=-1+\rho (D)\,\mathrm {Tr}\big (\chi (-D)J(\chi ,\rho )\big )+\rho (D)\,\mathrm {Tr}\big (\psi (-D)J(\psi ,\rho )\big ). \]

lang/CubicSix.lean

/-
  lang/CubicSix — T43, cycle 244: the genus-2 curves y² = x⁶ + D at p ≡ 1 (mod 3).

  p = 6n + 7, χ cubic, ρ quadratic, ψ = χρ (a sextic character). #{x⁶ = u} = (1 + ρ(u))(1 + χ(u) + χ̄(u)) (six_count: x⁶ = (x³)²,
  the square roots ±c of u and χ(c) = χ(u)²), J(ρ, ρ) = −ρ(−1) (rho_rho), and u = −Dt:
      six_sum   Σ_x ρ(x⁶ + D) = −1 + ρ(D) Tr(χ(−D) J(χ, ρ)) + ρ(D) Tr(ψ(−D) J(ψ, ρ))          (D ≠ 0)
  The trace of Frobenius on the Jacobian of y² = x⁶ + D splits into the two Hecke characters (an isogeny to E × E').
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.CubicDup
import lang.CubicFive

namespace CubicSix

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm FermatCubic CubicSextic CubicDup
open LibRing (castZ castN)

variable (n : Nat)

def psi (w x : Zp (6 * n + 5 + 1)) : Int × Int := (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w x) (rho3 n x)

theorem rho_sq1 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (x : Zp (6 * n + 5 + 1)) (hx : x ≠ (ring (6 * n + 5 + 1)).zero) : (Cyclo3.cyc3 LibInt.intR).mul (rho3 n x) (rho3 n x) = (Cyclo3.cyc3 LibInt.intR).one := by
  rcases rho3_val n hpr x hx with ⟨_, e⟩ | ⟨_, e⟩ <;> rw [e] <;> rfl

theorem rho_iv (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (x : Zp (6 * n + 5 + 1)) : rho3 n (CharSumGen.iv (6 * n + 4) x) = rho3 n x := by
  by_cases hx : x = (ring (6 * n + 5 + 1)).zero
  · rw [hx]; exact congrArg (rho3 n) (CharSumGen.iv_zero (6 * n + 4))
  · have h1 : (ring (6 * n + 5 + 1)).mul (CharSumGen.iv (6 * n + 4) x) x = (ring (6 * n + 5 + 1)).one := CharSumGen.iv_mul (6 * n + 4) hpr x hx
    have h2 : (Cyclo3.cyc3 LibInt.intR).mul (rho3 n (CharSumGen.iv (6 * n + 4) x)) (rho3 n x) = (Cyclo3.cyc3 LibInt.intR).one := by
      rw [← rho3_mul n hpr, h1]; exact rho3_p n _ (PowerSum.one_ne_zero' _) (CubicChar.pw_one'' (ring (6 * n + 5 + 1)) _)
    calc rho3 n (CharSumGen.iv (6 * n + 4) x) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n (CharSumGen.iv (6 * n + 4) x)) (rho3 n x)) (rho3 n x) := by
            rw [(Cyclo3.cyc3 LibInt.intR).mul_assoc, rho_sq1 n hpr x hx]; lib_ring
      _ = rho3 n x := by rw [h2]; lib_ring

theorem rho3_one (_hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) : rho3 n (ring (6 * n + 5 + 1)).one = (Cyclo3.cyc3 LibInt.intR).one := rho3_p n _ (PowerSum.one_ne_zero' _) (CubicChar.pw_one'' (ring (6 * n + 5 + 1)) _)

/-- J(ρ, ρ) = −ρ(−1). -/
theorem rho_rho (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) : sumK (Cyclo3.cyc3 LibInt.intR) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (rho3 n t) (rho3 n (om1 n t))) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) := by
  have hD := CharSumGen.D_ne (Cyclo3.cyc3 LibInt.intR) (6 * n + 4) hpr (rho3 n) (rho3_mul n hpr) (rho3_one n hpr) (rho3_zero n) (rho3_sum n hpr) (ring (6 * n + 5 + 1)).one (PowerSum.one_ne_zero' _)
  have hD' : sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one b)) (rho3 n b)) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one :=
    (sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun b => by rw [rho_iv n hpr]) _).symm.trans hD
  rw [← FiniteRecChain.reindex (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) ((ring (6 * n + 5 + 1)).neg) ((ring (6 * n + 5 + 1)).neg) (fun x => by lib_ring) (fun x => by lib_ring)]
  rw [show (Cyclo3.cyc3 LibInt.intR).neg (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) = (Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) by lib_ring, ← hD', mul_sumK]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun b => by
    show (Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).neg b)) (rho3 n (om1 n ((ring (6 * n + 5 + 1)).neg b))) = _
    rw [show (ring (6 * n + 5 + 1)).neg b = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) b by lib_ring, rho3_mul n hpr, show om1 n ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) b) = (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one b by unfold om1; lib_ring]
    lib_ring) _

theorem g3_neg (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) : g3 n w ((ring (6 * n + 5 + 1)).neg c) = g3 n w c := by
  unfold g3 cb
  rw [show (ring (6 * n + 5 + 1)).neg c = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) c by lib_ring, chi3_mul n hpr w hw1 hw3, chi3_neg1 n hpr w hw1 hw3]; lib_ring

theorem g3_sq (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) : g3 n w ((ring (6 * n + 5 + 1)).mul c c) = g3 n w c := by
  unfold g3 cb
  rw [chi3_mul n hpr w hw1 hw3]
  rcases CubicFive.chi_cases n w c with e | e | e | e
  · exfalso
    unfold chi3 at e; rw [ite_neg' _ _ hc] at e
    by_cases c1 : pw (ring (6 * n + 5 + 1)) c (2 * n + 2) = (ring (6 * n + 5 + 1)).one
    · rw [ite_pos' _ _ c1] at e; exact Nat.noConfusion (Int.ofNat.inj (congrArg Prod.fst e))
    · rw [ite_neg' _ _ c1] at e
      by_cases c2 : pw (ring (6 * n + 5 + 1)) c (2 * n + 2) = w
      · rw [ite_pos' _ _ c2] at e; exact Nat.noConfusion (Int.ofNat.inj (congrArg Prod.snd e))
      · rw [ite_neg' _ _ c2] at e; exact Int.noConfusion (congrArg Prod.fst e)
  all_goals (rw [e]; rfl)

/-- **#{x⁶ = u} = (1 + ρ(u))(1 + χ(u) + χ̄(u)).** -/
theorem six_count (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (u : Zp (6 * n + 5 + 1)) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun x => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).mul (cube n x) (cube n x)) u) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (rho3 n u)) (g3 n w u) := by
  rw [FermatCubic.step2 n hpr w hw1 hw3 (fun y => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).mul y y) u)]
  have pk : ∀ z, sumK (Cyclo3.cyc3 LibInt.intR) (fun y => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w y) (delta (Cyclo3.cyc3 LibInt.intR) y z)) (LL n) = g3 n w z := fun z => by
    rw [← FiniteRecChain.pick (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (g3 n w) z]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun y => (Cyclo3.cyc3 LibInt.intR).mul_comm _ _) _
  by_cases hu : u = (ring (6 * n + 5 + 1)).zero
  · rw [hu, sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun y => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w y) (delta (Cyclo3.cyc3 LibInt.intR) y (ring (6 * n + 5 + 1)).zero)) (fun y => by
      congr 1; unfold delta
      by_cases hy : y = (ring (6 * n + 5 + 1)).zero
      · rw [ite_pos' _ _ hy, ite_pos' _ _ (by rw [hy]; lib_ring)]
      · rw [ite_neg' _ _ hy, ite_neg' _ _ (fun e => hy (by
          cases nozd (6 * n + 5) hpr _ _ e with
          | inl h => exact h
          | inr h => exact h))]), pk, rho3_zero]; show _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).zero) _; lib_ring
  · rcases rho3_val n hpr u hu with ⟨ep, e⟩ | ⟨_, e⟩
    · match CubicDup.sq_root n hpr u hu ep with
      | ⟨c, hc⟩ =>
        have hc0 : c ≠ (ring (6 * n + 5 + 1)).zero := fun e0 => hu (by rw [← hc, e0]; lib_ring)
        rw [← hc, sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun y => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w y) (delta (Cyclo3.cyc3 LibInt.intR) y c)) ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w y) (delta (Cyclo3.cyc3 LibInt.intR) y ((ring (6 * n + 5 + 1)).neg c)))) (fun y => by
          rw [CubicDup.two_roots n hpr c hc0 y]; lib_ring), sumK_add, pk, pk, g3_neg n hpr w hw1 hw3, g3_sq n hpr w hw1 hw3 c hc0, hc, e]
        show _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one) _; lib_ring
    · have hz : ∀ y, (Cyclo3.cyc3 LibInt.intR).mul (g3 n w y) (delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).mul y y) u) = (Cyclo3.cyc3 LibInt.intR).zero := fun y => by
        unfold delta
        rw [ite_neg' _ _ (fun ex => by
          have hy0 : y ≠ (ring (6 * n + 5 + 1)).zero := fun e0 => hu (by rw [← ex, e0]; lib_ring)
          have h1 := CubicDup.rho3_sq n hpr y hy0
          rw [ex, e] at h1
          exact absurd (congrArg Prod.fst h1) (fun e' => Int.noConfusion e'))]; lib_ring
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hz, HeckePrime.zsumR (Cyclo3.cyc3 LibInt.intR), e]
      show (Cyclo3.cyc3 LibInt.intR).zero = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) _; lib_ring

def S6 (D : Zp (6 * n + 5 + 1)) : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun x => rho3 n ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul (cube n x) (cube n x)) D)) (LL n)

/-- L = J(ψ, ρ) = Σ_t ψ(t) ρ(1 − t). -/
def Lx (w : Zp (6 * n + 5 + 1)) : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (psi n w t) (rho3 n (om1 n t))) (LL n)

/-- **Σ_x ρ(x⁶ + D) = −1 + ρ(D) Tr(χ(−D) K) + ρ(D) Tr(ψ(−D) L).** -/
theorem six_sum (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) :
    S6 n D = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)
      ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w)) (cconj ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w))))))
      ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (psi n w ((ring (6 * n + 5 + 1)).neg D)) (Lx n w)) (cconj ((Cyclo3.cyc3 LibInt.intR).mul (psi n w ((ring (6 * n + 5 + 1)).neg D)) (Lx n w))))) := by
  have hD' : (ring (6 * n + 5 + 1)).neg D ≠ (ring (6 * n + 5 + 1)).zero := fun e => hD (by calc D = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg D) := by lib_ring
                                                         _ = (ring (6 * n + 5 + 1)).zero := by rw [e]; lib_ring)
  unfold S6
  -- Σ_x F(x⁶) = Σ_u #{x⁶ = u} F(u)
  have eB : sumK (Cyclo3.cyc3 LibInt.intR) (fun x => rho3 n ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul (cube n x) (cube n x)) D)) (LL n)
      = sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (sumK (Cyclo3.cyc3 LibInt.intR) (fun x => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).mul (cube n x) (cube n x)) u) (LL n)) (rho3 n ((ring (6 * n + 5 + 1)).add u D))) (LL n) := by
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun x => sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) u ((ring (6 * n + 5 + 1)).mul (cube n x) (cube n x))) (rho3 n ((ring (6 * n + 5 + 1)).add u D))) (LL n))
      (fun x => (FiniteRecChain.pick (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (fun u => rho3 n ((ring (6 * n + 5 + 1)).add u D)) _).symm), sumK_swap]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun u => by
      rw [sumK_mul]; exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun s => by rw [delta_symm]) _) _
  rw [eB, sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun u => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (rho3 n ((ring (6 * n + 5 + 1)).add u D))
      ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w u) (rho3 n ((ring (6 * n + 5 + 1)).add u D)))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w u) (rho3 n ((ring (6 * n + 5 + 1)).add u D))))
      ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n u) (rho3 n ((ring (6 * n + 5 + 1)).add u D)))) ((Cyclo3.cyc3 LibInt.intR).mul (psi n w u) (rho3 n ((ring (6 * n + 5 + 1)).add u D)))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cb n w u) (rho3 n u)) (rho3 n ((ring (6 * n + 5 + 1)).add u D))))
    (fun u => by rw [six_count n hpr w hw1 hw3]; unfold g3 psi; lib_ring)]
  repeat rw [sumK_add]
  have S0 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => rho3 n ((ring (6 * n + 5 + 1)).add a D)) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := by
    rw [FiniteRecChain.reindex (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (fun a => (ring (6 * n + 5 + 1)).add a D) (fun a => (ring (6 * n + 5 + 1)).add a ((ring (6 * n + 5 + 1)).neg D)) (fun x => by lib_ring) (fun y => by lib_ring) (rho3 n)]
    exact rho3_sum n hpr
  have scale : ∀ F : Zp (6 * n + 5 + 1) → Int × Int, sumK (Cyclo3.cyc3 LibInt.intR) F (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun t => F ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg D) t)) (LL n) := fun F =>
    (CharSumGen.sum_scale (Cyclo3.cyc3 LibInt.intR) (6 * n + 4) hpr F ((ring (6 * n + 5 + 1)).neg D) hD').symm
  have hsub : ∀ t, (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg D) t) D = (ring (6 * n + 5 + 1)).mul D (om1 n t) := fun t => by unfold om1; lib_ring
  have S1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (rho3 n ((ring (6 * n + 5 + 1)).add a D))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (rho3 n D)) (Kx n w) := by
    rw [scale, Kx, mul_sumK]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => by rw [chi3_mul n hpr w hw1 hw3, hsub, rho3_mul n hpr]; lib_ring) _
  have S2 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (rho3 n ((ring (6 * n + 5 + 1)).add a D))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg D))) (rho3 n D)) (cconj (Kx n w)) := by
    rw [scale, Kx, cconj_sum, mul_sumK]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => by
      rw [cb_mul n hpr w hw1 hw3, hsub, rho3_mul n hpr, cconj_mul, rho3_conj, cb_cconj n hpr w hw1 hw3, cb_cconj n hpr w hw1 hw3]; lib_ring) _
  have S3 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (rho3 n a) (rho3 n ((ring (6 * n + 5 + 1)).add a D))) (LL n) = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by
    rw [scale]
    have hr : (Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).neg D)) (rho3 n D) = rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) := by
      rw [show (ring (6 * n + 5 + 1)).neg D = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) D by lib_ring, rho3_mul n hpr, (Cyclo3.cyc3 LibInt.intR).mul_assoc, rho_sq1 n hpr D hD]; lib_ring
    have hm1 : (Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) = (Cyclo3.cyc3 LibInt.intR).one := rho_sq1 n hpr _ (fun e => PowerSum.one_ne_zero' (6 * n + 5) (by
      calc (ring (6 * n + 5 + 1)).one = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) := by lib_ring
        _ = (ring (6 * n + 5 + 1)).zero := by rw [e]; lib_ring))
    calc _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).neg D)) (rho3 n D)) (sumK (Cyclo3.cyc3 LibInt.intR) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (rho3 n t) (rho3 n (om1 n t))) (LL n)) := by
            rw [mul_sumK]; exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => by rw [hsub, rho3_mul n hpr, rho3_mul n hpr]; lib_ring) _
      _ = (Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one := by rw [hr, rho_rho n hpr]; rw [show (Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) ((Cyclo3.cyc3 LibInt.intR).neg (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one))) = (Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).neg   -- (+112 chars; full line in the Zenodo source)
  have S4 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (psi n w a) (rho3 n ((ring (6 * n + 5 + 1)).add a D))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (psi n w ((ring (6 * n + 5 + 1)).neg D)) (rho3 n D)) (Lx n w) := by
    rw [scale, Lx, mul_sumK]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => by unfold psi; rw [chi3_mul n hpr w hw1 hw3, rho3_mul n hpr, hsub, rho3_mul n hpr]; lib_ring) _
  have S5 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (rho3 n a)) (rho3 n ((ring (6 * n + 5 + 1)).add a D))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj (psi n w ((ring (6 * n + 5 + 1)).neg D))) (rho3 n D)) (cconj (Lx n w)) := by
    rw [scale, Lx, cconj_sum, mul_sumK]
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => by
      unfold psi
      rw [cb_mul n hpr w hw1 hw3, rho3_mul n hpr, hsub, rho3_mul n hpr, cb_cconj n hpr w hw1 hw3 ((ring (6 * n + 5 + 1)).neg D), cb_cconj n hpr w hw1 hw3 t]
      repeat rw [cconj_mul]
      repeat rw [rho3_conj]
      lib_ring) _
  rw [S0, S1, S2, S3, S4, S5, cconj_mul, cconj_mul]
  lib_ring

/-! ## J(ψ, ρ) through J -/

theorem chi_cb (_hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (_hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (_hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (t : Zp (6 * n + 5 + 1)) (ht : t ≠ (ring (6 * n + 5 + 1)).zero) : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (cb n w t) = (Cyclo3.cyc3 LibInt.intR).one := by
  unfold cb
  rcases CubicFive.chi_cases n w t with e | e | e | e
  · exfalso
    unfold chi3 at e; rw [ite_neg' _ _ ht] at e
    by_cases c1 : pw (ring (6 * n + 5 + 1)) t (2 * n + 2) = (ring (6 * n + 5 + 1)).one
    · rw [ite_pos' _ _ c1] at e; exact Nat.noConfusion (Int.ofNat.inj (congrArg Prod.fst e))
    · rw [ite_neg' _ _ c1] at e
      by_cases c2 : pw (ring (6 * n + 5 + 1)) t (2 * n + 2) = w
      · rw [ite_pos' _ _ c2] at e; exact Nat.noConfusion (Int.ofNat.inj (congrArg Prod.snd e))
      · rw [ite_neg' _ _ c2] at e; exact Int.noConfusion (congrArg Prod.fst e)
  all_goals (rw [e]; rfl)

theorem chi_iv (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (t : Zp (6 * n + 5 + 1)) : chi3 n w (CharSumGen.iv (6 * n + 4) t) = cb n w t := by
  by_cases ht : t = (ring (6 * n + 5 + 1)).zero
  · rw [ht, show CharSumGen.iv (6 * n + 4) (ring (6 * n + 5 + 1)).zero = (ring (6 * n + 5 + 1)).zero from CharSumGen.iv_zero (6 * n + 4), chi3_zero]; unfold cb; rw [chi3_zero]; rfl
  · have h1 : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w (CharSumGen.iv (6 * n + 4) t)) (chi3 n w t) = (Cyclo3.cyc3 LibInt.intR).one := by
      rw [← chi3_mul n hpr w hw1 hw3, CharSumGen.iv_mul (6 * n + 4) hpr t ht]
      exact val_of n hpr w hw1 hw3 (ring (6 * n + 5 + 1)).one (PowerSum.one_ne_zero' _) 0 (Nat.zero_lt_succ 2) (CubicChar.pw_one'' (ring (6 * n + 5 + 1)) _)
    calc chi3 n w (CharSumGen.iv (6 * n + 4) t) = (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w (CharSumGen.iv (6 * n + 4) t)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (cb n w t)) := by
            rw [chi_cb n hpr w hw1 hw3 t ht]; lib_ring
      _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w (CharSumGen.iv (6 * n + 4) t)) (chi3 n w t)) (cb n w t) := by lib_ring
      _ = cb n w t := by rw [h1]; lib_ring

/-- **J(ψ, ρ) = ρ(−1) χ̄(4) J̄** (t ↦ 1/t and cubic duplication). -/
theorem L_eq (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    Lx n w = (Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) (cconj ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n))) (J n w))) := by
  rw [← cubic_dup n hpr w hw1 hw3]
  unfold Lx Kx
  rw [← FiniteRecChain.reindex (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (CharSumGen.iv (6 * n + 4)) (CharSumGen.iv (6 * n + 4))
    (CharSumGen.iv_iv (6 * n + 4) hpr) (CharSumGen.iv_iv (6 * n + 4) hpr) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (psi n w t) (rho3 n (om1 n t))), cconj_sum, mul_sumK]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun t => by
    show (Cyclo3.cyc3 LibInt.intR).mul (psi n w (CharSumGen.iv (6 * n + 4) t)) (rho3 n (om1 n (CharSumGen.iv (6 * n + 4) t))) = _
    by_cases ht : t = (ring (6 * n + 5 + 1)).zero
    · rw [ht, show CharSumGen.iv (6 * n + 4) (ring (6 * n + 5 + 1)).zero = (ring (6 * n + 5 + 1)).zero from CharSumGen.iv_zero (6 * n + 4)]
      unfold psi; rw [chi3_zero, rho3_zero]
      have z : ∀ x, cconj ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero x) = (Cyclo3.cyc3 LibInt.intR).zero := fun x => by rw [show (Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero x = (Cyclo3.cyc3 LibInt.intR).zero by lib_ring]; rfl
      show (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (Cyclo3.cyc3 LibInt.intR).zero) _ = (Cyclo3.cyc3 LibInt.intR).mul _ (cconj ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero _))
      rw [z]; lib_ring
    · have hi := CharSumGen.iv_mul (6 * n + 4) hpr t ht
      have h1 : om1 n (CharSumGen.iv (6 * n + 4) t) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) (om1 n t)) (CharSumGen.iv (6 * n + 4) t) := by
        unfold om1
        calc (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg (CharSumGen.iv (6 * n + 4) t)) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul (CharSumGen.iv (6 * n + 4) t) t) ((ring (6 * n + 5 + 1)).neg (CharSumGen.iv (6 * n + 4) t)) := by rw [hi]
          _ = _ := by lib_ring
      unfold psi
      rw [h1, rho3_mul n hpr, rho3_mul n hpr, rho_iv n hpr, chi_iv n hpr w hw1 hw3, cconj_mul, rho3_conj, cb_cconj n hpr w hw1 hw3]
      have hr := rho_sq1 n hpr t ht
      calc _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) ((Cyclo3.cyc3 LibInt.intR).mul (cconj (chi3 n w t)) (rho3 n (om1 n t)))) ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n t) (rho3 n t)) := by lib_ring
        _ = _ := by rw [hr]; lib_ring) _

/-- **The six sextic twists of y² = x⁶ + D**: χ(v) = ω, ρ(v) = −1 ⇒ Σ_{k=0}^{5} Σ_x ρ(x⁶ + v^k D) = −6. -/
theorem six_twist_sum (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D v : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) (hv0 : v ≠ (ring (6 * n + 5 + 1)).zero)
    (hv : chi3 n w v = (((0 : Int), (1 : Int)))) (hr : rho3 n v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo3.cyc3 LibInt.intR).add (S6 n (D)) ((Cyclo3.cyc3 LibInt.intR).add (S6 n ((ring (6 * n + 5 + 1)).mul v (D))) ((Cyclo3.cyc3 LibInt.intR).add (S6 n ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)))) ((Cyclo3.cyc3 LibInt.intR).add (S6 n ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))))) ((Cyclo3.cyc3 LibInt.intR).add (S6 n ((ring  -- (+830 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (6 * n + 5 + 1)).mul v (D) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n2
  have n4 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n3
  have n5 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n4
  rw [six_sum n hpr w hw1 hw3 _ n0, six_sum n hpr w hw1 hw3 _ n1, six_sum n hpr w hw1 hw3 _ n2, six_sum n hpr w hw1 hw3 _ n3, six_sum n hpr w hw1 hw3 _ n4, six_sum n hpr w hw1 hw3 _ n5]
  unfold psi
  have hn : ∀ E : Zp (6 * n + 5 + 1), (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul v E) = (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).neg E) := fun E => by lib_ring
  repeat rw [hn]
  have hcv : ∀ E : Zp (6 * n + 5 + 1), chi3 n w ((ring (6 * n + 5 + 1)).mul v E) = (Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi3 n w E) := fun E => by rw [chi3_mul n hpr w hw1 hw3, hv]
  have hρ : ∀ E : Zp (6 * n + 5 + 1), rho3 n ((ring (6 * n + 5 + 1)).mul v E) = (Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho3 n E) := fun E => by rw [rho3_mul n hpr, hr]
  repeat rw [hcv]
  repeat rw [hρ]
  generalize chi3 n w ((ring (6 * n + 5 + 1)).neg D) = c
  generalize rho3 n ((ring (6 * n + 5 + 1)).neg D) = s
  generalize rho3 n D = r
  repeat rw [cconj_mul]
  have E1 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibI  -- (+2140 chars; full line in the Zenodo source)
  have E2 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul (cconj (((0 : Int), (1 : Int)))) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.c  -- (+2260 chars; full line in the Zenodo source)
  have E3 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3  -- (+3424 chars; full line in the Zenodo source)
  have E4 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3  -- (+3664 chars; full line in the Zenodo source)
  calc _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibIn  -- (+5536 chars; full line in the Zenodo source)
          ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul c s) (Lx n w)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mu  -- (+7681 chars; full line in the Zenodo source)
        generalize (cconj (((0 : Int), (1 : Int)))) = a2; generalize (cconj (((-1 : Int)), ((0 : Int)))) = b2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.  -- (+173 chars; full line in the Zenodo source)

end CubicSix
#print axioms CubicSix.rho_sq1
#print axioms CubicSix.rho_iv
#print axioms CubicSix.rho3_one
#print axioms CubicSix.rho_rho
#print axioms CubicSix.g3_neg
#print axioms CubicSix.g3_sq
#print axioms CubicSix.six_count
#print axioms CubicSix.six_sum
#print axioms CubicSix.chi_cb
#print axioms CubicSix.chi_iv
#print axioms CubicSix.L_eq
#print axioms CubicSix.six_twist_sum

#print axioms: 12 theorems, no axioms.

78.74 The cubic twists \(x^3+y^3=c\)

The substitution \(a=ct\) in \(\sum _aN(a)N(c-a)\).

\[ \#\{x^3+y^3=c\}=p-2+\bar \chi (c)J+\chi (c)\bar J\qquad (c\ne 0). \]

lang/CubicTwistCount.lean

/-
  lang/CubicTwistCount — T43, cycle 246: the cubic twists x³ + y³ = c over 𝔽_p, p ≡ 1 (mod 3).

  #{x³ + y³ = c} = Σ_a N(a)N(c − a), N = 1 + χ + χ̄; with a = ct the nine sums become J, J̄ and J(χ, χ̄) = −1:
      twist_count   #{x³ + y³ = c} = p − 2 + χ̄(c)J + χ(c)J̄       (c ≠ 0)
  (c = 1 is FermatCubic.) The Hecke character χ̄(c)·J of every cubic twist (the Selmer curves x³ + y³ = c).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CubicSix
import lang.StrictHasse
import lang.CubicCyclo

namespace CubicTwistCount

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm FermatCubic CubicSextic CubicDup CubicCyclo CubicSix
open LibRing (castZ castN)

variable (n : Nat)

def Ft (c : Zp (6 * n + 5 + 1)) : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun x => sumK (Cyclo3.cyc3 LibInt.intR) (fun y => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).add (cube n x) (cube n y)) c) (LL n)) (LL n)

theorem step1c (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c x : Zp (6 * n + 5 + 1)) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun y => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).add (cube n x) (cube n y)) c) (LL n) = g3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg (cube n x))) := by
  unfold g3; rw [← cube_count n hpr w hw1 hw3]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun y => by
    unfold delta
    by_cases h : (ring (6 * n + 5 + 1)).add (cube n x) (cube n y) = c
    · rw [ite_pos' _ _ h, ite_pos' _ _ (by rw [← h]; lib_ring)]
    · rw [ite_neg' _ _ h, ite_neg' _ _ (fun e => h (by rw [e]; lib_ring))]) _

/-- **#{x³ + y³ = c} = p − 2 + χ̄(c)J + χ(c)J̄.** -/
theorem twist_count (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) :
    Ft n c = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (J n w)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cconj (J n w))))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one)) := by
  unfold Ft
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => step1c n hpr w hw1 hw3 c x), FermatCubic.step2 n hpr w hw1 hw3 (fun a => g3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))]
  have scale : ∀ F : Zp (6 * n + 5 + 1) → Int × Int, sumK (Cyclo3.cyc3 LibInt.intR) F (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun t => F ((ring (6 * n + 5 + 1)).mul c t)) (LL n) := fun F =>
    (CharSumGen.sum_scale (Cyclo3.cyc3 LibInt.intR) (6 * n + 4) hpr F c hc).symm
  rw [scale]
  have hct : ∀ t, (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul c t)) = (ring (6 * n + 5 + 1)).mul c (om1 n t) := fun t => by unfold om1; lib_ring
  have hcc := chi_cb n hpr w hw1 hw3 c hc
  have hexp : ∀ t, (Cyclo3.cyc3 LibInt.intR).mul (g3 n w ((ring (6 * n + 5 + 1)).mul c t)) (g3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul c t))))
      = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one
          ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w (om1 n t))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w (om1 n t)))))
          ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w t)) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w t))))
          ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (chi3 n w (om1 n t)))))
          ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w t) (cb n w (om1 n t)))))
          ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cb n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (cb n w (om1 n t)))))
          ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (chi3 n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w t) (chi3 n w (om1 n t)))) := fun t => by
    rw [hct]; unfold g3; rw [chi3_mul n hpr w hw1 hw3, chi3_mul n hpr w hw1 hw3, cb_mul n hpr w hw1 hw3, cb_mul n hpr w hw1 hw3]; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hexp]
  repeat rw [sumK_add]
  have hp : ∀ (k : Int × Int) (f : Zp (6 * n + 5 + 1) → Int × Int), sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul k (f a)) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul k (sumK (Cyclo3.cyc3 LibInt.intR) f (LL n)) := fun k f => (mul_sumK (Cyclo3.cyc3 LibInt.intR) k f (LL n)).symm
  have SA : sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := chi3_sum n hpr w hw1 hw3
  have SB : sumK (Cyclo3.cyc3 LibInt.intR) (cb n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := cb_sum n hpr w hw1 hw3
  have T1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun _ : Zp (6 * n + 5 + 1) => (Cyclo3.cyc3 LibInt.intR).one) (LL n) = (Int.ofNat (6 * n + 5 + 1 + 1), 0) := JacobiSum.sum_one n _
  rw [T1, hp, hp, hp, hp, hp, hp, hp, hp, refl1 n (chi3 n w), refl1 n (cb n w), SA, SB,
    show sumK (Cyclo3.cyc3 LibInt.intR) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (chi3 n w (om1 n t))) (LL n) = J n w from rfl, SBB n hpr w hw1 hw3, SAB n hpr w hw1 hw3, SBA n hpr w hw1 hw3]
  have e1 : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w c) = cb n w c := rfl
  have e2 : (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w c) = chi3 n w c := by
    calc (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w c) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cb n w c)) (chi3 n w c) := by unfold cb; lib_ring
      _ = chi3 n w c := by rw [hcc]; lib_ring
  rw [e1, e2, hcc, show (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (chi3 n w c) = (Cyclo3.cyc3 LibInt.intR).one by rw [(Cyclo3.cyc3 LibInt.intR).mul_comm]; exact hcc]
  lib_ring

/-- norm in ℤ[ω]: (2z₁ − z₂)² + 3z₂² = 4N(z), and N is multiplicative. -/
theorem norm_mul (u v : Int × Int) :
    (2 * ((Cyclo3.cyc3 LibInt.intR).mul u v).1 - ((Cyclo3.cyc3 LibInt.intR).mul u v).2) * (2 * ((Cyclo3.cyc3 LibInt.intR).mul u v).1 - ((Cyclo3.cyc3 LibInt.intR).mul u v).2) + 3 * (((Cyclo3.cyc3 LibInt.intR).mul u v).2 * ((Cyclo3.cyc3 LibInt.intR).mul u v).2)
      = 4 * ((u.1 * u.1 - u.1 * u.2 + u.2 * u.2) * (v.1 * v.1 - v.1 * v.2 + v.2 * v.2)) := by
  show (2 * (u.1 * v.1 + -(u.2 * v.2)) + -(u.1 * v.2 + u.2 * v.1 + -(u.2 * v.2))) * (2 * (u.1 * v.1 + -(u.2 * v.2)) + -(u.1 * v.2 + u.2 * v.1 + -(u.2 * v.2)))
      + 3 * ((u.1 * v.2 + u.2 * v.1 + -(u.2 * v.2)) * (u.1 * v.2 + u.2 * v.1 + -(u.2 * v.2)))
      = 4 * ((u.1 * u.1 + -(u.1 * u.2) + u.2 * u.2) * (v.1 * v.1 + -(v.1 * v.2) + v.2 * v.2))
  lib_int

theorem cb_norm (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) : (cb n w c).1 * (cb n w c).1 - (cb n w c).1 * (cb n w c).2 + (cb n w c).2 * (cb n w c).2 = 1 := by
  have h1 := chi_cb n hpr w hw1 hw3 c hc
  unfold cb at h1 ⊢
  rcases CubicFive.chi_cases n w c with e | e | e | e
  · rw [e] at h1; exact absurd (congrArg Prod.fst h1) (fun h => Nat.noConfusion (Int.ofNat.inj h))
  all_goals (rw [e]; rfl)

/-- **Hasse's bound for every cubic twist**: (#{x³ + y³ = c} − p + 2)² ≤ 4p. -/
theorem twist_hasse (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) :
    ∃ s : Nat, ((Ft n c).1 + -Int.ofNat (6 * n + 5 + 1 + 1) + 2) * ((Ft n c).1 + -Int.ofNat (6 * n + 5 + 1 + 1) + 2) + Int.ofNat s = 4 * Int.ofNat (6 * n + 5 + 1 + 1) := by
  rw [twist_count n hpr w hw1 hw3 c hc]
  have hN := jacobi_norm n hpr w hw1 hw3
  have hu := cb_norm n hpr w hw1 hw3 c hc
  have hm := norm_mul (cb n w c) (J n w)
  rw [hu, hN] at hm
  have cc : ∀ z : Int × Int, cconj (cconj z) = z := fun z => Prod.ext (show z.1 - z.2 - -z.2 = z.1 by
      show z.1 + -z.2 + -(-z.2) = z.1; lib_int) (show - -z.2 = z.2 by lib_int)
  have hcc : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cconj (J n w)) = cconj ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (J n w)) := by
    rw [cconj_mul, cb_cconj n hpr w hw1 hw3 c, cc]
  rw [hcc]
  generalize (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (J n w) = z at hm ⊢
  match StrictHasse.sq_nat z.2 with
  | ⟨k, hk⟩ =>
    refine ⟨3 * (k * k), ?_⟩
    have e1 : Int.ofNat (3 * (k * k)) = 3 * (z.2 * z.2) := by rw [hk]; rfl
    rw [e1]
    show (Int.ofNat (6 * n + 5 + 1 + 1) + (z.1 + (z.1 - z.2)) + -(1 + 1) + -Int.ofNat (6 * n + 5 + 1 + 1) + 2) * (Int.ofNat (6 * n + 5 + 1 + 1) + (z.1 + (z.1 - z.2)) + -(1 + 1) + -Int.ofNat (6 * n + 5 + 1 + 1) + 2) + 3 * (z.2 * z.2) = _
    rw [show 4 * Int.ofNat (6 * n + 5 + 1 + 1) = 4 * (1 * Int.ofNat (6 * n + 5 + 1 + 1)) by lib_int, ← hm]
    lib_int

theorem chi_norm (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) : (chi3 n w c).1 * (chi3 n w c).1 - (chi3 n w c).1 * (chi3 n w c).2 + (chi3 n w c).2 * (chi3 n w c).2 = 1 := by
  have h1 := chi_cb n hpr w hw1 hw3 c hc
  unfold cb at h1
  rcases CubicFive.chi_cases n w c with e | e | e | e
  · rw [e] at h1; exact absurd (congrArg Prod.fst h1) (fun h => Nat.noConfusion (Int.ofNat.inj h))
  all_goals (rw [e]; rfl)

/-- **Hasse's bound for every sextic twist** y² = x³ + D: (Σ_x ρ(x³ + D))² ≤ 4p. -/
theorem sextic_hasse (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) :
    ∃ s : Nat, (Sx n D).1 * (Sx n D).1 + Int.ofNat s = 4 * Int.ofNat (6 * n + 5 + 1 + 1) := by
  have h4 : (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D) ≠ (ring (6 * n + 5 + 1)).zero := fun e => by
    have h2 := CubicSextic.two_ne n
    have : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D = (ring (6 * n + 5 + 1)).zero := by
      calc _ = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)) := by lib_ring
        _ = (ring (6 * n + 5 + 1)).zero := by rw [e]; lib_ring
    cases nozd (6 * n + 5) hpr _ _ this with
    | inl h => cases nozd (6 * n + 5) hpr _ _ h with
      | inl h' => exact h2 h'
      | inr h' => exact h2 h'
    | inr h => exact hD h
  rw [sextic_J n hpr w hw1 hw3 D hD]
  have hN := jacobi_norm n hpr w hw1 hw3
  have hu := chi_norm n hpr w hw1 hw3 _ h4
  have hm := norm_mul (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))) (J n w)
  rw [hu, hN] at hm
  have hr : rho3 n D = (1, 0) ∨ rho3 n D = (-1, 0) := by
    rcases rho3_val n hpr D hD with ⟨_, e⟩ | ⟨_, e⟩
    · exact Or.inl e
    · exact Or.inr e
  generalize (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))) (J n w) = z at hm ⊢
  match StrictHasse.sq_nat z.2 with
  | ⟨k, hk⟩ =>
    refine ⟨3 * (k * k), ?_⟩
    have e1 : Int.ofNat (3 * (k * k)) = 3 * (z.2 * z.2) := by rw [hk]; rfl
    rw [e1, show 4 * Int.ofNat (6 * n + 5 + 1 + 1) = 4 * (1 * Int.ofNat (6 * n + 5 + 1 + 1)) by lib_int, ← hm]
    rcases hr with e | e <;> rw [e]
    · show (1 * (z.1 + (z.1 - z.2)) + -(0 * (z.2 + -z.2))) * (1 * (z.1 + (z.1 - z.2)) + -(0 * (z.2 + -z.2))) + 3 * (z.2 * z.2) = (2 * z.1 - z.2) * (2 * z.1 - z.2) + 3 * (z.2 * z.2)
      show (1 * (z.1 + (z.1 + -z.2)) + -(0 * (z.2 + -z.2))) * (1 * (z.1 + (z.1 + -z.2)) + -(0 * (z.2 + -z.2))) + 3 * (z.2 * z.2) = (2 * z.1 + -z.2) * (2 * z.1 + -z.2) + 3 * (z.2 * z.2)
      lib_int
    · show (-1 * (z.1 + (z.1 + -z.2)) + -(0 * (z.2 + -z.2))) * (-1 * (z.1 + (z.1 + -z.2)) + -(0 * (z.2 + -z.2))) + 3 * (z.2 * z.2) = (2 * z.1 + -z.2) * (2 * z.1 + -z.2) + 3 * (z.2 * z.2)
      lib_int

theorem norm_conj (z : Int × Int) : (cconj z).1 * (cconj z).1 - (cconj z).1 * (cconj z).2 + (cconj z).2 * (cconj z).2 = z.1 * z.1 - z.1 * z.2 + z.2 * z.2 := by
  show (z.1 - z.2) * (z.1 - z.2) - (z.1 - z.2) * -z.2 + -z.2 * -z.2 = _
  show (z.1 + -z.2) * (z.1 + -z.2) + -((z.1 + -z.2) * -z.2) + -z.2 * -z.2 = z.1 * z.1 + -(z.1 * z.2) + z.2 * z.2
  lib_int

theorem norm_mul' (u v : Int × Int) : ((Cyclo3.cyc3 LibInt.intR).mul u v).1 * ((Cyclo3.cyc3 LibInt.intR).mul u v).1 - ((Cyclo3.cyc3 LibInt.intR).mul u v).1 * ((Cyclo3.cyc3 LibInt.intR).mul u v).2 + ((Cyclo3.cyc3 LibInt.intR).mul u v).2 * ((Cyclo3.cyc3 LibInt.intR).mul u v).2
    = (u.1 * u.1 - u.1 * u.2 + u.2 * u.2) * (v.1 * v.1 - v.1 * v.2 + v.2 * v.2) := by
  show (u.1 * v.1 + -(u.2 * v.2)) * (u.1 * v.1 + -(u.2 * v.2)) + -((u.1 * v.1 + -(u.2 * v.2)) * (u.1 * v.2 + u.2 * v.1 + -(u.2 * v.2))) + (u.1 * v.2 + u.2 * v.1 + -(u.2 * v.2)) * (u.1 * v.2 + u.2 * v.1 + -(u.2 * v.2))
    = (u.1 * u.1 + -(u.1 * u.2) + u.2 * u.2) * (v.1 * v.1 + -(v.1 * v.2) + v.2 * v.2)
  lib_int

theorem tr_sq (z : Int × Int) : (2 * z.1 - z.2) * (2 * z.1 - z.2) + 3 * (z.2 * z.2) = 4 * (z.1 * z.1 - z.1 * z.2 + z.2 * z.2) := by
  show (2 * z.1 + -z.2) * (2 * z.1 + -z.2) + 3 * (z.2 * z.2) = 4 * (z.1 * z.1 + -(z.1 * z.2) + z.2 * z.2); lib_int

theorem rho_norm (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (x : Zp (6 * n + 5 + 1)) (hx : x ≠ (ring (6 * n + 5 + 1)).zero) : (rho3 n x).1 * (rho3 n x).1 - (rho3 n x).1 * (rho3 n x).2 + (rho3 n x).2 * (rho3 n x).2 = 1 := by
  rcases rho3_val n hpr x hx with ⟨_, e⟩ | ⟨_, e⟩ <;> rw [e] <;> rfl

/-- **Weil's bound for the genus-2 curves y² = x⁶ + D**: (Σ_x ρ(x⁶ + D) + 1)² ≤ 16p. -/
theorem six_weil (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) :
    ∃ s : Nat, ((S6 n D).1 + 1) * ((S6 n D).1 + 1) + Int.ofNat s = 16 * Int.ofNat (6 * n + 5 + 1 + 1) := by
  have hD' : (ring (6 * n + 5 + 1)).neg D ≠ (ring (6 * n + 5 + 1)).zero := fun e => hD (by calc D = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg D) := by lib_ring
                                                         _ = (ring (6 * n + 5 + 1)).zero := by rw [e]; lib_ring)
  have m1 : (ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero := fun e => PowerSum.one_ne_zero' (6 * n + 5) (by
    calc (ring (6 * n + 5 + 1)).one = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) := by lib_ring
      _ = (ring (6 * n + 5 + 1)).zero := by rw [e]; lib_ring)
  have four0 : (ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n) ≠ (ring (6 * n + 5 + 1)).zero := fun e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact CubicSextic.two_ne n h
    | inr h => exact CubicSextic.two_ne n h
  rw [six_sum n hpr w hw1 hw3 D hD]
  have hN := jacobi_norm n hpr w hw1 hw3
  -- z1 = χ(−D)K, K = χ(4)J;  z2 = ψ(−D)L, L = ρ(−1) conj(χ(4)J)
  have nK : (Kx n w).1 * (Kx n w).1 - (Kx n w).1 * (Kx n w).2 + (Kx n w).2 * (Kx n w).2 = Int.ofNat (6 * n + 5 + 1 + 1) := by
    rw [cubic_dup n hpr w hw1 hw3, norm_mul', chi_norm n hpr w hw1 hw3 _ four0, hN]; lib_int
  have nL : (Lx n w).1 * (Lx n w).1 - (Lx n w).1 * (Lx n w).2 + (Lx n w).2 * (Lx n w).2 = Int.ofNat (6 * n + 5 + 1 + 1) := by
    rw [L_eq n hpr w hw1 hw3, norm_mul', rho_norm n hpr _ m1, norm_conj, norm_mul', chi_norm n hpr w hw1 hw3 _ four0, hN]; lib_int
  have n1 : ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w)).1 * ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w)).1 - ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w)).1 * ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w)).2
      + ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w)).2 * ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w)).2 = Int.ofNat (6 * n + 5 + 1 + 1) := by
    rw [norm_mul', chi_norm n hpr w hw1 hw3 _ hD', nK]; lib_int
  have n2 : ((Cyclo3.cyc3 LibInt.intR).mul (psi n w ((ring (6 * n + 5 + 1)).neg D)) (Lx n w)).1 * ((Cyclo3.cyc3 LibInt.intR).mul (psi n w ((ring (6 * n + 5 + 1)).neg D)) (Lx n w)).1 - ((Cyclo3.cyc3 LibInt.intR).mul (psi n w ((ring (6 * n + 5 + 1)).neg D)) (Lx n w)).1 * ((Cyclo3.cyc3 LibInt.intR).mul (psi n w ((ring (6 * n + 5 + 1)).neg D)) (Lx n w)).2
      + ((Cyclo3.cyc3 LibInt.intR).mul (psi n w ((ring (6 * n + 5 + 1)).neg D)) (Lx n w)).2 * ((Cyclo3.cyc3 LibInt.intR).mul (psi n w ((ring (6 * n + 5 + 1)).neg D)) (Lx n w)).2 = Int.ofNat (6 * n + 5 + 1 + 1) := by
    rw [norm_mul', show psi n w ((ring (6 * n + 5 + 1)).neg D) = (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (rho3 n ((ring (6 * n + 5 + 1)).neg D)) from rfl, norm_mul', chi_norm n hpr w hw1 hw3 _ hD', rho_norm n hpr _ hD', nL]; lib_int
  have hr : rho3 n D = (1, 0) ∨ rho3 n D = (-1, 0) := by
    rcases rho3_val n hpr D hD with ⟨_, e⟩ | ⟨_, e⟩
    · exact Or.inl e
    · exact Or.inr e
  generalize (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w) = z1 at n1 ⊢
  generalize (Cyclo3.cyc3 LibInt.intR).mul (psi n w ((ring (6 * n + 5 + 1)).neg D)) (Lx n w) = z2 at n2 ⊢
  have t1 := tr_sq z1
  have t2 := tr_sq z2
  rw [n1] at t1; rw [n2] at t2
  match StrictHasse.sq_nat (2 * z1.1 - z1.2 - (2 * z2.1 - z2.2)), StrictHasse.sq_nat z1.2, StrictHasse.sq_nat z2.2 with
  | ⟨a, ha⟩, ⟨b, hb⟩, ⟨c, hc⟩ =>
    refine ⟨a * a + 6 * (b * b) + 6 * (c * c), ?_⟩
    have e1 : Int.ofNat (a * a + 6 * (b * b) + 6 * (c * c)) = (2 * z1.1 - z1.2 - (2 * z2.1 - z2.2)) * (2 * z1.1 - z1.2 - (2 * z2.1 - z2.2)) + 6 * (z1.2 * z1.2) + 6 * (z2.2 * z2.2) := by
      rw [ha, hb, hc]; rfl
    have e16 : 16 * Int.ofNat (6 * n + 5 + 1 + 1) = 2 * ((2 * z1.1 - z1.2) * (2 * z1.1 - z1.2) + 3 * (z1.2 * z1.2)) + 2 * ((2 * z2.1 - z2.2) * (2 * z2.1 - z2.2) + 3 * (z2.2 * z2.2)) := by
      rw [t1, t2]; lib_int
    rw [e1, e16]
    rcases hr with e | e <;> rw [e]
    · show (-1 + (1 * (z1.1 + (z1.1 + -z1.2)) + -(0 * (z1.2 + -z1.2))) + (1 * (z2.1 + (z2.1 + -z2.2)) + -(0 * (z2.2 + -z2.2))) + 1)
          * (-1 + (1 * (z1.1 + (z1.1 + -z1.2)) + -(0 * (z1.2 + -z1.2))) + (1 * (z2.1 + (z2.1 + -z2.2)) + -(0 * (z2.2 + -z2.2))) + 1)
          + ((2 * z1.1 + -z1.2 + -(2 * z2.1 + -z2.2)) * (2 * z1.1 + -z1.2 + -(2 * z2.1 + -z2.2)) + 6 * (z1.2 * z1.2) + 6 * (z2.2 * z2.2))
        = 2 * ((2 * z1.1 + -z1.2) * (2 * z1.1 + -z1.2) + 3 * (z1.2 * z1.2)) + 2 * ((2 * z2.1 + -z2.2) * (2 * z2.1 + -z2.2) + 3 * (z2.2 * z2.2))
      lib_int
    · show (-1 + (-1 * (z1.1 + (z1.1 + -z1.2)) + -(0 * (z1.2 + -z1.2))) + (-1 * (z2.1 + (z2.1 + -z2.2)) + -(0 * (z2.2 + -z2.2))) + 1)
          * (-1 + (-1 * (z1.1 + (z1.1 + -z1.2)) + -(0 * (z1.2 + -z1.2))) + (-1 * (z2.1 + (z2.1 + -z2.2)) + -(0 * (z2.2 + -z2.2))) + 1)
          + ((2 * z1.1 + -z1.2 + -(2 * z2.1 + -z2.2)) * (2 * z1.1 + -z1.2 + -(2 * z2.1 + -z2.2)) + 6 * (z1.2 * z1.2) + 6 * (z2.2 * z2.2))
        = 2 * ((2 * z1.1 + -z1.2) * (2 * z1.1 + -z1.2) + 3 * (z1.2 * z1.2)) + 2 * ((2 * z2.1 + -z2.2) * (2 * z2.1 + -z2.2) + 3 * (z2.2 * z2.2))
      lib_int

/-! ## the Fermat cubic and y² = x³ − 432 -/

def three : Zp (6 * n + 5 + 1) := (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one (ring (6 * n + 5 + 1)).one) (ring (6 * n + 5 + 1)).one
def D432 : Zp (6 * n + 5 + 1) := (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n))) ((ring (6 * n + 5 + 1)).mul (three n) ((ring (6 * n + 5 + 1)).mul (three n) (three n))))

theorem three_ne (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : three n ≠ (ring (6 * n + 5 + 1)).zero := fun e => by
  -- 3 = 0 would give w² + w + 1 = 0 with w = 1 (the cube roots collapse): use 1 + 2w ≠ 0 instead
  have hs : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) = (ring (6 * n + 5 + 1)).neg (three n) := by
    have hw : (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one w) ((ring (6 * n + 5 + 1)).mul w w) = (ring (6 * n + 5 + 1)).zero := by
      have h3 : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add w ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one w) ((ring (6 * n + 5 + 1)).mul w w)) = (ring (6 * n + 5 + 1)).zero := by
        rw [show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add w ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one w) ((ring (6 * n + 5 + 1)).mul w w)) = (ring (6 * n + 5 + 1)).add (pw (ring (6 * n + 5 + 1)) w 3) ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) by
          show _ = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w) w) w) _; lib_ring, hw3]; lib_ring
      cases nozd (6 * n + 5) hpr _ _ h3 with
      | inl h => exact absurd (eq_of_sub n _ _ h) hw1
      | inr h => exact h
    calc _ = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).neg (three n)) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one (ring (6 * n + 5 + 1)).one) ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one (ring (6 * n + 5 + 1)).one)) ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one w) ((ring (6 *   -- (+51 chars; full line in the Zenodo source)
      _ = _ := by rw [hw]; lib_ring
  rw [e] at hs
  have : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) = (ring (6 * n + 5 + 1)).zero := by rw [hs]; lib_ring
  cases nozd (6 * n + 5) hpr _ _ this with
  | _ h =>
    -- 1 + 2w = 0 and 3 = 0: then 2(1 + 2w) − 2·... contradiction via w = 1
    have h2 : (ring (6 * n + 5 + 1)).add w w = (ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one := by calc (ring (6 * n + 5 + 1)).add w w = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) := by lib_ring
                                                    _ = _ := by rw [h]; lib_ring
    apply hw1
    calc w = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul (three n) w) ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add w w)) := by unfold three; lib_ring
      _ = (ring (6 * n + 5 + 1)).one := by rw [e, h2]; lib_ring
theorem sq_m3 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 *   -- (+66 chars; full line in the Zenodo source)
  have hw : (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one w) ((ring (6 * n + 5 + 1)).mul w w) = (ring (6 * n + 5 + 1)).zero := by
    have h3 : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add w ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one w) ((ring (6 * n + 5 + 1)).mul w w)) = (ring (6 * n + 5 + 1)).zero := by
      rw [show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add w ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one w) ((ring (6 * n + 5 + 1)).mul w w)) = (ring (6 * n + 5 + 1)).add (pw (ring (6 * n + 5 + 1)) w 3) ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) by
        show _ = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (ring (6 * n + 5 + 1)).one w) w) w) _; lib_ring, hw3]; lib_ring
    cases nozd (6 * n + 5) hpr _ _ h3 with
    | inl h => exact absurd (eq_of_sub n _ _ h) hw1
    | inr h => exact h
  calc _ = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).neg (three n)) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one (ring (6 * n + 5 + 1)).one) ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one (ring (6 * n + 5 + 1)).one)) ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one w) ((ring (6 * n   -- (+49 chars; full line in the Zenodo source)
    _ = _ := by rw [hw]; lib_ring

theorem chi_cube (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (x : Zp (6 * n + 5 + 1)) (hx : x ≠ (ring (6 * n + 5 + 1)).zero) : chi3 n w (cube n x) = (Cyclo3.cyc3 LibInt.intR).one := by
  unfold cube; rw [chi3_mul n hpr w hw1 hw3, chi3_mul n hpr w hw1 hw3]
  have h := chi_cb n hpr w hw1 hw3 x hx
  unfold cb at h
  calc _ = (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w x) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w x) (chi3 n w x)) := by lib_ring
    _ = _ := h

/-- **The Fermat cubic and y² = x³ − 432 have the same Frobenius trace**: #(x³ + y³ = 1) + 3 = p + 1 + Σ_x ρ(x³ − 432). -/
theorem fermat_432 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : (Fc n).1 + 3 = Int.ofNat (6 * n + 5 + 1 + 1) + 1 + (Sx n (D432 n)).1 := by
  have h2 := CubicSextic.two_ne n
  have h3 := three_ne n hpr w hw1 hw3
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have h4 : (ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ h2 h2
  have h12 : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) (three n) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ h4 h3
  have hu : (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w) ≠ (ring (6 * n + 5 + 1)).zero := fun e => h3 (by
    have := sq_m3 n hpr w hw1 hw3
    rw [e] at this
    calc three n = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg (three n)) := by lib_ring
      _ = (ring (6 * n + 5 + 1)).zero := by rw [← this]; lib_ring)
  have hy : (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) (three n)) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hu h12
  have hD : D432 n = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) (three n)))
      ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) (three n))) := by
    rw [show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) (three n))) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) ((ring (6 * n + 5 + 1)).mul ((ring (6 *  -- (+47 chars; full line in the Zenodo source)
      = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w))) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) (three n)) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n +   -- (+55 chars; full line in the Zenodo source)
      sq_m3 n hpr w hw1 hw3]
    unfold D432; lib_ring
  have hD0 : D432 n ≠ (ring (6 * n + 5 + 1)).zero := by rw [hD]; exact nz _ _ hy hy
  have hrho : rho3 n (D432 n) = (Cyclo3.cyc3 LibInt.intR).one := by rw [hD]; exact CubicDup.rho3_sq n hpr _ hy
  have hchi : chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) (D432 n))) = (Cyclo3.cyc3 LibInt.intR).one := by
    rw [show (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) (D432 n)) = cube n ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) (three n)) by unfold D432 cube; lib_ring]
    exact chi_cube n hpr w hw1 hw3 _ h12
  rw [sextic_J n hpr w hw1 hw3 _ hD0, hrho, hchi, (fermat_hasse n hpr w hw1 hw3).1]
  show _ = _ + (((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one (J n w)) (cconj ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one (J n w))))).1)
  rw [show (Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one (J n w)) (cconj ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one (J n w)))) = (Cyclo3.cyc3 LibInt.intR).add (J n w) (cconj (J n w)) by
    rw [show (Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one (J n w) = J n w by lib_ring]; lib_ring]
  show _ = _ + ((J n w).1 + ((J n w).1 - (J n w).2))
  show Int.ofNat (6 * n + 5 + 1 + 1) + (2 * (J n w).1 + -(J n w).2) + -2 + 3 = Int.ofNat (6 * n + 5 + 1 + 1) + 1 + ((J n w).1 + ((J n w).1 + -(J n w).2))
  lib_int

/-- **3-isogeny**: y² = x³ + D and y² = x³ − 27D have the same trace. -/
theorem isog3 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) :
    Sx n D = Sx n ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (three n) ((ring (6 * n + 5 + 1)).mul (three n) (three n))) D)) := by
  have h3 := three_ne n hpr w hw1 hw3
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have h27 : (ring (6 * n + 5 + 1)).mul (three n) ((ring (6 * n + 5 + 1)).mul (three n) (three n)) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ h3 (nz _ _ h3 h3)
  have hD' : (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (three n) ((ring (6 * n + 5 + 1)).mul (three n) (three n))) D) ≠ (ring (6 * n + 5 + 1)).zero := fun e => nz _ _ h27 hD (by
    calc _ = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (three n) ((ring (6 * n + 5 + 1)).mul (three n) (three n))) D)) := by lib_ring
      _ = (ring (6 * n + 5 + 1)).zero := by rw [e]; lib_ring)
  rw [sextic_J n hpr w hw1 hw3 D hD, sextic_J n hpr w hw1 hw3 _ hD']
  have hu : (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w) ≠ (ring (6 * n + 5 + 1)).zero := fun e => h3 (by
    have := sq_m3 n hpr w hw1 hw3
    rw [e] at this
    calc three n = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg (three n)) := by lib_ring
      _ = (ring (6 * n + 5 + 1)).zero := by rw [← this]; lib_ring)
  -- ρ(−27D) = ρ(D): −27 = (1 + 2ω)²·9
  have hr : rho3 n ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (three n) ((ring (6 * n + 5 + 1)).mul (three n) (three n))) D)) = rho3 n D := by
    rw [show (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (three n) ((ring (6 * n + 5 + 1)).mul (three n) (three n))) D)
        = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) (three n)) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) (three n))) D by
        rw [show (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) (three n)) ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) (three n))
          = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w)) ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).add w w))) ((ring (6 * n + 5 + 1)).mul (three n) (three n)) by lib_ring, sq_m3 n hpr w hw1 hw3]; lib_ring,
      rho3_mul n hpr, CubicDup.rho3_sq n hpr _ (nz _ _ hu h3)]; lib_ring
  -- χ(4·27D) = χ(4D)·χ(27) and χ(27) = χ(3³) = 1, χ(−1) = 1
  have hc : chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (three n) ((ring (6 * n + 5 + 1)).mul (three n) (three n))) D))))
      = chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)) := by
    rw [show (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (three n) ((ring (6 * n + 5 + 1)).mul (three n) (three n))) D)))
        = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) (cube n (three n))) ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)) by unfold cube; lib_ring,
      chi3_mul n hpr w hw1 hw3, chi3_mul n hpr w hw1 hw3, chi3_neg1 n hpr w hw1 hw3, chi_cube n hpr w hw1 hw3 _ h3]; lib_ring
  rw [hr, hc]

/-- **The three cubic twists cancel**: if χ(v) = ω, then Σρ(x³ + D) + Σρ(x³ + v²D) + Σρ(x³ + v⁴D) = 0. -/
theorem twist_cancel (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D v : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) (hv0 : v ≠ (ring (6 * n + 5 + 1)).zero)
    (hv : chi3 n w v = ((0 : Int), (1 : Int))) :
    (Cyclo3.cyc3 LibInt.intR).add (Sx n D) ((Cyclo3.cyc3 LibInt.intR).add (Sx n ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul v v) D)) (Sx n ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul v v) ((ring (6 * n + 5 + 1)).mul v v)) D))) = (Cyclo3.cyc3 LibInt.intR).zero := by
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have h2 := nz _ _ hv0 hv0
  have h4 := nz _ _ h2 h2
  rw [sextic_J n hpr w hw1 hw3 D hD, sextic_J n hpr w hw1 hw3 _ (nz _ _ h2 hD), sextic_J n hpr w hw1 hw3 _ (nz _ _ h4 hD),
    rho3_mul n hpr ((ring (6 * n + 5 + 1)).mul v v) D, rho3_mul n hpr ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul v v) ((ring (6 * n + 5 + 1)).mul v v)) D,
    CubicDup.rho3_sq n hpr _ hv0, CubicDup.rho3_sq n hpr _ h2]
  have e2 : ∀ u : Zp (6 * n + 5 + 1), (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul u D)) = (ring (6 * n + 5 + 1)).mul u ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)) :=
    fun u => by lib_ring
  rw [e2, e2, chi3_mul n hpr w hw1 hw3 ((ring (6 * n + 5 + 1)).mul v v), chi3_mul n hpr w hw1 hw3 ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul v v) ((ring (6 * n + 5 + 1)).mul v v)),
    chi3_mul n hpr w hw1 hw3 ((ring (6 * n + 5 + 1)).mul v v), chi3_mul n hpr w hw1 hw3 v v, hv]
  have m1 : (Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) (((0 : Int), (1 : Int))) = (((-1 : Int)), ((-1 : Int))) := rfl
  have m2 : (Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((-1 : Int))) (((-1 : Int)), ((-1 : Int))) = (((0 : Int), (1 : Int))) := rfl
  have c1 : cconj (((-1 : Int)), ((-1 : Int))) = (((0 : Int), (1 : Int))) := rfl
  have c2 : cconj (((0 : Int), (1 : Int))) = (((-1 : Int)), ((-1 : Int))) := rfl
  rw [m1, m2]
  generalize chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)) = c
  generalize rho3 n D = r
  repeat rw [cconj_mul]
  rw [c1, c2]
  have E : (Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).add (((-1 : Int)), ((-1 : Int))) (((0 : Int), (1 : Int)))) = (Cyclo3.cyc3 LibInt.intR).zero := rfl
  calc _ = (Cyclo3.cyc3 LibInt.intR).mul r ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul c (J n w)) ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).add (((-1 : Int)), ((-1 : Int))) (((0 : Int), (1 : Int))))))
          ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj c) (cconj (J n w))) ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).add (((-1 : Int)), ((-1 : Int))) (((0 : Int), (1 : Int))))))) := by
        generalize (((-1 : Int)), ((-1 : Int))) = a; generalize (((0 : Int), (1 : Int))) = b; lib_ring
    _ = (Cyclo3.cyc3 LibInt.intR).zero := by rw [E]; lib_ring

/-- **The six sextic twists cancel**: if χ(v) = ω and ρ(v) = −1 then Σ_{k=0}^{5} Σ_x ρ(x³ + v^k D) = 0. -/
theorem six_cancel (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D v : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) (hv0 : v ≠ (ring (6 * n + 5 + 1)).zero)
    (hv : chi3 n w v = (((0 : Int), (1 : Int)))) (hr : rho3 n v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo3.cyc3 LibInt.intR).add (Sx n (D)) ((Cyclo3.cyc3 LibInt.intR).add (Sx n ((ring (6 * n + 5 + 1)).mul v (D))) ((Cyclo3.cyc3 LibInt.intR).add (Sx n ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)))) ((Cyclo3.cyc3 LibInt.intR).add (Sx n ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))))) ((Cyclo3.cyc3 LibInt.intR).add (Sx n ((ring  -- (+331 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (6 * n + 5 + 1)).mul v (D) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n2
  have n4 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n3
  have n5 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n4
  rw [sextic_J n hpr w hw1 hw3 _ n0, sextic_J n hpr w hw1 hw3 _ n1, sextic_J n hpr w hw1 hw3 _ n2, sextic_J n hpr w hw1 hw3 _ n3, sextic_J n hpr w hw1 hw3 _ n4, sextic_J n hpr w hw1 hw3 _ n5]
  have hc : ∀ E : Zp (6 * n + 5 + 1), chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul v E))) = (Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) E))) := fun E => by
    rw [show (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul v E)) = (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) E)) by lib_ring,
      chi3_mul n hpr w hw1 hw3, hv]
  have hρ : ∀ E : Zp (6 * n + 5 + 1), rho3 n ((ring (6 * n + 5 + 1)).mul v E) = (Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho3 n E) := fun E => by rw [rho3_mul n hpr, hr]
  repeat rw [hc]
  repeat rw [hρ]
  generalize chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)) = c
  generalize rho3 n D = r
  repeat rw [cconj_mul]
  have EP : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibI  -- (+2140 chars; full line in the Zenodo source)
  have EQ : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul (cconj (((0 : Int), (1 : Int)))) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.c  -- (+2260 chars; full line in the Zenodo source)
  calc _ = (Cyclo3.cyc3 LibInt.intR).mul r ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul c (J n w)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyc  -- (+2238 chars; full line in the Zenodo source)
          ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj c) (cconj (J n w))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt  -- (+2317 chars; full line in the Zenodo source)
        generalize (cconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo3.cyc3 LibInt.intR).zero := by rw [EP, EQ]; lib_ring

/-- **The three cubic twists of the Fermat curve**: χ(v) = ω ⇒ Σ_{k=0}^{2} #(x³ + y³ = v^k c) = 3(p − 2). -/
theorem fermat_twist_sum (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c v : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) (hv0 : v ≠ (ring (6 * n + 5 + 1)).zero) (hv : chi3 n w v = (((0 : Int), (1 : Int)))) :
    (Cyclo3.cyc3 LibInt.intR).add (Ft n (c)) ((Cyclo3.cyc3 LibInt.intR).add (Ft n ((ring (6 * n + 5 + 1)).mul v (c))) (Ft n ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (c))))) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), 0)) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), 0)) ((Int.ofNat (6 * n + 5 + 1 + 1), 0)))) (  -- (+376 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hc
  have n1 : (ring (6 * n + 5 + 1)).mul v (c) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (c)) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n1
  rw [twist_count n hpr w hw1 hw3 _ n0, twist_count n hpr w hw1 hw3 _ n1, twist_count n hpr w hw1 hw3 _ n2]
  unfold cb
  have hcv : ∀ E : Zp (6 * n + 5 + 1), chi3 n w ((ring (6 * n + 5 + 1)).mul v E) = (Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi3 n w E) := fun E => by rw [chi3_mul n hpr w hw1 hw3, hv]
  repeat rw [hcv]
  generalize chi3 n w c = t
  have EP : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.  -- (+343 chars; full line in the Zenodo source)
  have EQ : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)))) = (Cyclo3.cyc3 LibInt.intR).zero := rfl
  calc _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), 0)) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), 0)) ((Int.ofNat (6 * n + 5 + 1 + 1), 0)))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one)  -- (+1472 chars; full line in the Zenodo source)
        generalize (((0 : Int), (1 : Int))) = a; lib_ring
    _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), 0)) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), 0)) ((Int.ofNat (6 * n + 5 + 1 + 1), 0)))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).add ((  -- (+212 chars; full line in the Zenodo source)

/-- **Second moment over the six sextic twists**: χ(v) = ω, ρ(v) = −1 ⇒ Σ_{k=0}^{5} (Σ_x ρ(x³ + v^k D))² = 12p. -/
theorem six_moment (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D v : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) (hv0 : v ≠ (ring (6 * n + 5 + 1)).zero)
    (hv : chi3 n w v = (((0 : Int), (1 : Int)))) (hr : rho3 n v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Sx n (D)) (Sx n (D))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Sx n ((ring (6 * n + 5 + 1)).mul v (D))) (Sx n ((ring (6 * n + 5 + 1)).mul v (D)))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Sx n ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)))) (Sx n ((ring (6 * n + 5 + 1)).  -- (+1913 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (6 * n + 5 + 1)).mul v (D) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n2
  have n4 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n3
  have n5 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n4
  have h4D : (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D) ≠ (ring (6 * n + 5 + 1)).zero := fun e => nz _ _ (nz _ _ (show mtwo n ≠ (ring (6 * n + 5 + 1)).zero from CubicSextic.two_ne n) (show mtwo n ≠ (ring (6 * n + 5 + 1)).zero from CubicSextic.two_ne n)) hD (by
    calc _ = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)) := by lib_ring
      _ = (ring (6 * n + 5 + 1)).zero := by rw [e]; lib_ring)
  have hrr : (Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) (rho3 n D) = (Cyclo3.cyc3 LibInt.intR).one := by rw [← rho3_mul n hpr]; exact CubicDup.rho3_sq n hpr D hD
  have hcc : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))) (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)))) = (Cyclo3.cyc3 LibInt.intR).one := by
    rw [show (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))) = ((chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))).1, (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))).2) from rfl, norm_form, chi  -- (+33 chars; full line in the Zenodo source)
  have hJJ : (Cyclo3.cyc3 LibInt.intR).mul (J n w) (cconj (J n w)) = (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) := by
    rw [show J n w = ((J n w).1, (J n w).2) from rfl, norm_form, jacobi_norm n hpr w hw1 hw3]
  rw [sextic_J n hpr w hw1 hw3 _ n0, sextic_J n hpr w hw1 hw3 _ n1, sextic_J n hpr w hw1 hw3 _ n2, sextic_J n hpr w hw1 hw3 _ n3, sextic_J n hpr w hw1 hw3 _ n4, sextic_J n hpr w hw1 hw3 _ n5]
  have hc : ∀ E : Zp (6 * n + 5 + 1), chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul v E))) = (Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) E))) := fun E => by
    rw [show (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul v E)) = (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) E)) by lib_ring,
      chi3_mul n hpr w hw1 hw3, hv]
  have hρ : ∀ E : Zp (6 * n + 5 + 1), rho3 n ((ring (6 * n + 5 + 1)).mul v E) = (Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho3 n E) := fun E => by rw [rho3_mul n hpr, hr]
  repeat rw [hc]
  repeat rw [hρ]
  repeat rw [cconj_mul]
  have E1 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : In  -- (+4663 chars; full line in the Zenodo source)
  have E2 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : In  -- (+5102 chars; full line in the Zenodo source)
  have E3 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : In  -- (+4903 chars; full line in the Zenodo source)
  calc _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) (rho3 n D)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))) (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).m  -- (+5119 chars; full line in the Zenodo source)
          ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))) (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n  -- (+5566 chars; full line in the Zenodo source)
            ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)))) (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))))) ((Cyclo3.cyc3 LibInt.intR).mul (cconj (J n  -- (+5285 chars; full line in the Zenodo source)
        generalize (cconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3  -- (+503 chars; full line in the Zenodo source)
        rw [E1, E2, E3, hrr, hcc, hJJ]; generalize (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) = q; lib_ring

/-- **Fourth moment over the six sextic twists**: χ(v) = ω, ρ(v) = −1 ⇒ Σ_{k=0}^{5} (Σ_x ρ(x³ + v^k D))⁴ = 36p². -/
theorem six_moment4 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D v : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) (hv0 : v ≠ (ring (6 * n + 5 + 1)).zero)
    (hv : chi3 n w v = (((0 : Int), (1 : Int)))) (hr : rho3 n v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (Sx n (D)) (Sx n (D))) ((Cyclo3.cyc3 LibInt.intR).mul (Sx n (D)) (Sx n (D)))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (Sx n ((ring (6 * n + 5 + 1)).mul v (D))) (Sx n ((ring (6 * n + 5 + 1)).mul v (D)))) ((Cyclo3.cyc3 LibInt.intR).mul (Sx n ((ring (6 *  -- (+7835 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (6 * n + 5 + 1)).mul v (D) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n2
  have n4 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n3
  have n5 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n4
  have h4D : (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D) ≠ (ring (6 * n + 5 + 1)).zero := fun e => nz _ _ (nz _ _ (show mtwo n ≠ (ring (6 * n + 5 + 1)).zero from CubicSextic.two_ne n) (show mtwo n ≠ (ring (6 * n + 5 + 1)).zero from CubicSextic.two_ne n)) hD (by
    calc _ = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)) := by lib_ring
      _ = (ring (6 * n + 5 + 1)).zero := by rw [e]; lib_ring)
  have hrr : (Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) (rho3 n D) = (Cyclo3.cyc3 LibInt.intR).one := by rw [← rho3_mul n hpr]; exact CubicDup.rho3_sq n hpr D hD
  have hcc : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))) (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)))) = (Cyclo3.cyc3 LibInt.intR).one := by
    rw [show (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))) = ((chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))).1, (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))).2) from rfl, norm_form, chi  -- (+33 chars; full line in the Zenodo source)
  have hJJ : (Cyclo3.cyc3 LibInt.intR).mul (J n w) (cconj (J n w)) = (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) := by
    rw [show J n w = ((J n w).1, (J n w).2) from rfl, norm_form, jacobi_norm n hpr w hw1 hw3]
  rw [sextic_J n hpr w hw1 hw3 _ n0, sextic_J n hpr w hw1 hw3 _ n1, sextic_J n hpr w hw1 hw3 _ n2, sextic_J n hpr w hw1 hw3 _ n3, sextic_J n hpr w hw1 hw3 _ n4, sextic_J n hpr w hw1 hw3 _ n5]
  have hc : ∀ E : Zp (6 * n + 5 + 1), chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul v E))) = (Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) E))) := fun E => by
    rw [show (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul v E)) = (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) E)) by lib_ring,
      chi3_mul n hpr w hw1 hw3, hv]
  have hρ : ∀ E : Zp (6 * n + 5 + 1), rho3 n ((ring (6 * n + 5 + 1)).mul v E) = (Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho3 n E) := fun E => by rw [rho3_mul n hpr, hr]
  repeat rw [hc]
  repeat rw [hρ]
  repeat rw [cconj_mul]
  have E0 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.  -- (+10573 chars; full line in the Zenodo source)
  have E1 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.  -- (+10069 chars; full line in the Zenodo source)
  have E2 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.  -- (+10268 chars; full line in the Zenodo source)
  have E3 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.  -- (+9829 chars; full line in the Zenodo source)
  have E4 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.  -- (+10093 chars; full line in the Zenodo source)
  calc _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) (rho3 n D)) ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) (rho3 n D))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((Cyclo3.cyc3 LibInt.intR).mul (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1  -- (+64934 chars; full line in the Zenodo source)
        generalize (cconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Int.ofNat (6 * n + 5 + 1 + 1), (  -- (+4979 chars; full line in the Zenodo source)
        rw [E0, E1, E2, E3, E4, hrr, hcc, hJJ]; generalize (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) = q; lib_ring

/-- **Third moment over the six sextic twists vanishes**: χ(v) = ω, ρ(v) = −1 ⇒ Σ_{k=0}^{5} (Σ_x ρ(x³ + v^k D))³ = 0. -/
theorem six_moment3 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D v : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) (hv0 : v ≠ (ring (6 * n + 5 + 1)).zero)
    (hv : chi3 n w v = (((0 : Int), (1 : Int)))) (hr : rho3 n v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (Sx n (D)) (Sx n (D))) (Sx n (D))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (Sx n ((ring (6 * n + 5 + 1)).mul v (D))) (Sx n ((ring (6 * n + 5 + 1)).mul v (D)))) (Sx n ((ring (6 * n + 5 + 1)).mul v (D)))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 Lib  -- (+1777 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (6 * n + 5 + 1)).mul v (D) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n2
  have n4 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n3
  have n5 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n4
  rw [sextic_J n hpr w hw1 hw3 _ n0, sextic_J n hpr w hw1 hw3 _ n1, sextic_J n hpr w hw1 hw3 _ n2, sextic_J n hpr w hw1 hw3 _ n3, sextic_J n hpr w hw1 hw3 _ n4, sextic_J n hpr w hw1 hw3 _ n5]
  have hc : ∀ E : Zp (6 * n + 5 + 1), chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul v E))) = (Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) E))) := fun E => by
    rw [show (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul v E)) = (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) E)) by lib_ring,
      chi3_mul n hpr w hw1 hw3, hv]
  have hρ : ∀ E : Zp (6 * n + 5 + 1), rho3 n ((ring (6 * n + 5 + 1)).mul v E) = (Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho3 n E) := fun E => by rw [rho3_mul n hpr, hr]
  repeat rw [hc]
  repeat rw [hρ]
  repeat rw [cconj_mul]
  have E0 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.c  -- (+7930 chars; full line in the Zenodo source)
  have E1 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))) ((Cycl  -- (+7426 chars; full line in the Zenodo source)
  have E2 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cycl  -- (+7306 chars; full line in the Zenodo source)
  have E3 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.c  -- (+7570 chars; full line in the Zenodo source)
  calc _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) (rho3 n D)) (rho3 n D)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((Cyclo3.cyc3 LibInt.intR).mul (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)))) (cconj (J n  -- (+36275 chars; full line in the Zenodo source)
        generalize (cconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo3.cyc3 LibInt.intR).zero := by rw [E0, E1, E2, E3]; lib_ring

/-- **Sixth moment over the six sextic twists**: χ(v) = ω, ρ(v) = −1 ⇒ Σ_{k=0}^{5} a_k⁶ = 120p³ + 6(J⁶ + J̄⁶). -/
theorem six_moment6 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D v : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) (hv0 : v ≠ (ring (6 * n + 5 + 1)).zero)
    (hv : chi3 n w v = (((0 : Int), (1 : Int)))) (hr : rho3 n v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (Sx n (D)) ((Cyclo3.cyc3 LibInt.intR).mul (Sx n (D)) (Sx n (D)))) ((Cyclo3.cyc3 LibInt.intR).mul (Sx n (D)) ((Cyclo3.cyc3 LibInt.intR).mul (Sx n (D)) (Sx n (D))))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (Sx n ((ring (6 * n + 5 + 1)).mul v (D))) ((Cyc  -- (+34841 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (6 * n + 5 + 1)).mul v (D) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n2
  have n4 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n3
  have n5 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n4
  have h4D : (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D) ≠ (ring (6 * n + 5 + 1)).zero := fun e => nz _ _ (nz _ _ (show mtwo n ≠ (ring (6 * n + 5 + 1)).zero from CubicSextic.two_ne n) (show mtwo n ≠ (ring (6 * n + 5 + 1)).zero from CubicSextic.two_ne n)) hD (by
    calc _ = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)) := by lib_ring
      _ = (ring (6 * n + 5 + 1)).zero := by rw [e]; lib_ring)
  have hrr : (Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) (rho3 n D) = (Cyclo3.cyc3 LibInt.intR).one := by rw [← rho3_mul n hpr]; exact CubicDup.rho3_sq n hpr D hD
  have hcc : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))) (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)))) = (Cyclo3.cyc3 LibInt.intR).one := by
    rw [show (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))) = ((chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))).1, (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D))).2) from rfl, norm_form, chi  -- (+33 chars; full line in the Zenodo source)
  have hJJ : (Cyclo3.cyc3 LibInt.intR).mul (J n w) (cconj (J n w)) = (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) := by
    rw [show J n w = ((J n w).1, (J n w).2) from rfl, norm_form, jacobi_norm n hpr w hw1 hw3]
  have h3 : (Cyclo3.cyc3 LibInt.intR).mul ((chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)))) ((Cyclo3.cyc3 LibInt.intR).mul ((chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)))) ((chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 +   -- (+71 chars; full line in the Zenodo source)
    have := CubicSix.chi_cb n hpr w hw1 hw3 _ h4D
    unfold cb at this; exact this
  have h3' : (Cyclo3.cyc3 LibInt.intR).mul (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)))) ((Cyclo3.cyc3 LibInt.intR).mul (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) D)))) (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul (  -- (+90 chars; full line in the Zenodo source)
    rw [← cconj_mul, ← cconj_mul, h3]; rfl
  rw [sextic_J n hpr w hw1 hw3 _ n0, sextic_J n hpr w hw1 hw3 _ n1, sextic_J n hpr w hw1 hw3 _ n2, sextic_J n hpr w hw1 hw3 _ n3, sextic_J n hpr w hw1 hw3 _ n4, sextic_J n hpr w hw1 hw3 _ n5]
  have hc : ∀ E : Zp (6 * n + 5 + 1), chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul v E))) = (Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi3 n w ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) E))) := fun E => by
    rw [show (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) ((ring (6 * n + 5 + 1)).mul v E)) = (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)) E)) by lib_ring,
      chi3_mul n hpr w hw1 hw3, hv]
  have hρ : ∀ E : Zp (6 * n + 5 + 1), rho3 n ((ring (6 * n + 5 + 1)).mul v E) = (Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho3 n E) := fun E => by rw [rho3_mul n hpr, hr]
  repeat rw [hc]
  repeat rw [hρ]
  repeat rw [cconj_mul]
  have E0 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc  -- (+16178 chars; full line in the Zenodo source)
  have E1 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc  -- (+15355 chars; full line in the Zenodo source)
  have E2 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc  -- (+15235 chars; full line in the Zenodo source)
  have E3 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc  -- (+15434 chars; full line in the Zenodo source)
  have E4 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc  -- (+14995 chars; full line in the Zenodo source)
  have E5 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc  -- (+14875 chars; full line in the Zenodo source)
  have E6 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc  -- (+15458 chars; full line in the Zenodo source)
  calc _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) (rho3 n D)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) (rho3 n D)) ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) (rho3 n D)))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cconj (c  -- (+185654 chars; full line in the Zenodo source)
        generalize (cconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).mul ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR  -- (+30539 chars; full line in the Zenodo source)
        rw [E0, E1, E2, E3, E4, E5, E6, hrr, hcc, hJJ, h3, h3']; generalize (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) = q; lib_ring

theorem cconj_cconj (z : Int × Int) : cconj (cconj z) = z := by
  apply Prod.ext
  · show (z.1 - z.2) - -z.2 = z.1
    show (z.1 + -z.2) + - -z.2 = z.1
    lib_int
  · show - -z.2 = z.2
    lib_int

theorem unit_cc (u : Int × Int) (h : u.1 * u.1 - u.1 * u.2 + u.2 * u.2 = 1) : (Cyclo3.cyc3 LibInt.intR).mul u (cconj u) = (Cyclo3.cyc3 LibInt.intR).one := by
  rw [show u = (u.1, u.2) from rfl, norm_form, h]; rfl

/-- **Second moment of the genus-2 sextic twists**: χ(v) = ω, ρ(v) = −1 ⇒ Σ_{k=0}^{5} (1 + Σ_x ρ(x⁶ + v^k D))² = 24p. -/
theorem genus2_moment (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (D v : Zp (6 * n + 5 + 1)) (hD : D ≠ (ring (6 * n + 5 + 1)).zero) (hv0 : v ≠ (ring (6 * n + 5 + 1)).zero)
    (hv : chi3 n w v = (((0 : Int), (1 : Int)))) (hr : rho3 n v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (S6 n (D)) (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).add (S6 n (D)) (Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (S6 n ((ring (6 * n + 5 + 1)).mul v (D))) (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.i  -- (+3581 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (6 * n + 5 + 1)).mul v (D) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n2
  have n4 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D)))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n3
  have n5 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (D))))) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n4
  have hneg : ∀ x : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).neg x ≠ (ring (6 * n + 5 + 1)).zero := fun x hx e => hx (by
    calc x = (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).neg x) := by lib_ring
      _ = (ring (6 * n + 5 + 1)).zero := by rw [e]; lib_ring)
  have h1 : (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero := fun e => by
    have h := hneg _ (show mtwo n ≠ (ring (6 * n + 5 + 1)).zero from CubicSextic.two_ne n)
    exact (show mtwo n ≠ (ring (6 * n + 5 + 1)).zero from CubicSextic.two_ne n) (by unfold mtwo; rw [e]; lib_ring)
  have hrr : (Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) (rho3 n D) = (Cyclo3.cyc3 LibInt.intR).one := by rw [← rho3_mul n hpr]; exact CubicDup.rho3_sq n hpr D hD
  have hcc : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (cconj (chi3 n w ((ring (6 * n + 5 + 1)).neg D))) = (Cyclo3.cyc3 LibInt.intR).one := unit_cc _ (chi_norm n hpr w hw1 hw3 _ (hneg D hD))
  have hss : (Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).neg D)) (cconj (rho3 n ((ring (6 * n + 5 + 1)).neg D))) = (Cyclo3.cyc3 LibInt.intR).one := unit_cc _ (rho_norm n hpr _ (hneg D hD))
  have h4 : (ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ (show mtwo n ≠ (ring (6 * n + 5 + 1)).zero from CubicSextic.two_ne n) (show mtwo n ≠ (ring (6 * n + 5 + 1)).zero from CubicSextic.two_ne n)
  have hc4 : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n))) (cconj (chi3 n w ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)))) = (Cyclo3.cyc3 LibInt.intR).one := unit_cc _ (chi_norm n hpr w hw1 hw3 _ h4)
  have hr1 : (Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) (cconj (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one))) = (Cyclo3.cyc3 LibInt.intR).one := unit_cc _ (rho_norm n hpr _ (hneg _ h1))
  have hJJ : (Cyclo3.cyc3 LibInt.intR).mul (J n w) (cconj (J n w)) = (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) := by
    rw [show J n w = ((J n w).1, (J n w).2) from rfl, norm_form, jacobi_norm n hpr w hw1 hw3]
  have hKK : (Cyclo3.cyc3 LibInt.intR).mul (Kx n w) (cconj (Kx n w)) = (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) := by
    rw [cubic_dup n hpr w hw1 hw3, cconj_mul]
    calc _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n))) (cconj (chi3 n w ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n))))) ((Cyclo3.cyc3 LibInt.intR).mul (J n w) (cconj (J n w))) := by lib_ring
      _ = _ := by rw [hc4, hJJ]; lib_ring
  have hLL : (Cyclo3.cyc3 LibInt.intR).mul (Lx n w) (cconj (Lx n w)) = (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) := by
    rw [CubicSix.L_eq n hpr w hw1 hw3]
    repeat rw [cconj_mul]
    repeat rw [cconj_cconj]
    calc _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)) (cconj (rho3 n ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one)))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n))) (cconj (chi3 n w ((ring (6 * n + 5 + 1)).mul (mtwo n) (mtwo n)))))   -- (+71 chars; full line in the Zenodo source)
      _ = _ := by rw [hr1, hc4, hJJ]; lib_ring
  rw [CubicSix.six_sum n hpr w hw1 hw3 _ n0, CubicSix.six_sum n hpr w hw1 hw3 _ n1, CubicSix.six_sum n hpr w hw1 hw3 _ n2, CubicSix.six_sum n hpr w hw1 hw3 _ n3, CubicSix.six_sum n hpr w hw1 hw3 _ n4, CubicSix.six_sum n hpr w hw1 hw3 _ n5]
  unfold CubicSix.psi
  have hn : ∀ E : Zp (6 * n + 5 + 1), (ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul v E) = (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).neg E) := fun E => by lib_ring
  repeat rw [hn]
  have hcv : ∀ E : Zp (6 * n + 5 + 1), chi3 n w ((ring (6 * n + 5 + 1)).mul v E) = (Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi3 n w E) := fun E => by rw [chi3_mul n hpr w hw1 hw3, hv]
  have hρ : ∀ E : Zp (6 * n + 5 + 1), rho3 n ((ring (6 * n + 5 + 1)).mul v E) = (Cyclo3.cyc3 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho3 n E) := fun E => by rw [rho3_mul n hpr, hr]
  repeat rw [hcv]
  repeat rw [hρ]
  repeat rw [cconj_mul]
  have E0 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : In  -- (+4663 chars; full line in the Zenodo source)
  have E1 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : In  -- (+5102 chars; full line in the Zenodo source)
  have E2 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((-1 : In  -- (+4903 chars; full line in the Zenodo source)
  have E3 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))))) ((Cycl  -- (+7231 chars; full line in the Zenodo source)
  have E4 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))))) ((Cycl  -- (+7790 chars; full line in the Zenodo source)
  have E5 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))))) ((Cycl  -- (+7711 chars; full line in the Zenodo source)
  have E6 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3  -- (+5947 chars; full line in the Zenodo source)
  have E7 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3  -- (+6187 chars; full line in the Zenodo source)
  have E8 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3  -- (+6067 chars; full line in the Zenodo source)
  have E9 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3  -- (+6307 chars; full line in the Zenodo source)
  calc _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (rho3 n D) (rho3 n D)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).neg D)) (Kx n w))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3   -- (+69920 chars; full line in the Zenodo source)
        generalize (cconj (((0 : Int), (1 : Int)))) = a2; generalize (cconj (((-1 : Int)), ((0 : Int)))) = b2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3  -- (+1427 chars; full line in the Zenodo source)
        rw [E0, E1, E2, E3, E4, E5, E6, E7, E8, E9, hrr, hcc, hss, hKK, hLL]; generalize (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) = q; lib_ring

end CubicTwistCount
#print axioms CubicTwistCount.step1c
#print axioms CubicTwistCount.twist_count
#print axioms CubicTwistCount.norm_mul
#print axioms CubicTwistCount.cb_norm
#print axioms CubicTwistCount.twist_hasse
#print axioms CubicTwistCount.chi_norm
#print axioms CubicTwistCount.sextic_hasse
#print axioms CubicTwistCount.norm_conj
#print axioms CubicTwistCount.norm_mul'
#print axioms CubicTwistCount.tr_sq
#print axioms CubicTwistCount.rho_norm
#print axioms CubicTwistCount.six_weil
#print axioms CubicTwistCount.three_ne
#print axioms CubicTwistCount.sq_m3
#print axioms CubicTwistCount.chi_cube
#print axioms CubicTwistCount.fermat_432
#print axioms CubicTwistCount.isog3
#print axioms CubicTwistCount.twist_cancel
#print axioms CubicTwistCount.six_cancel
#print axioms CubicTwistCount.fermat_twist_sum
#print axioms CubicTwistCount.six_moment
#print axioms CubicTwistCount.six_moment4
#print axioms CubicTwistCount.six_moment3
#print axioms CubicTwistCount.six_moment6
#print axioms CubicTwistCount.cconj_cconj
#print axioms CubicTwistCount.unit_cc
#print axioms CubicTwistCount.genus2_moment

#print axioms: 27 theorems, no axioms.

78.75 The quartic twists \(x^4+y^4=c\)

The sixteen sums of the Fermat quartic, scaled by \(\chi ^{r+s}(c)\).

\[ \#\{x^4+y^4=c\}=p-1-2\chi (-1)+\rho (c)(J+\bar J)+2\big (\bar \chi (c)K+\chi (c)\bar K\big ). \]

lang/QuartTwistCount.lean

/-
  lang/QuartTwistCount — T43, cycle 247: the quartic twists x⁴ + y⁴ = c over 𝔽_p, p ≡ 1 (mod 4).

  #{x⁴ + y⁴ = c} = Σ_a N(a)N(c − a), N = 1 + χ + ρ + χ̄; with a = ct the sixteen sums of FermatQuartic get the factors χ^{r+s}(c):
      quart_twist_count   #{x⁴ + y⁴ = c} = p − 1 − 2χ(−1) + ρ(c)(J + J̄) + 2(χ̄(c)K + χ(c)K̄),   K = J(ρ, χ) = χ(−1)J   (c ≠ 0)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.QuartCyclo
import lang.QuartTwist
import lang.StrictHasse

namespace QuartTwistCount

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup FermatQuartic QuartTwist
open LibRing (castZ castN)

variable (n : Nat)

def Fqc (c : Zp (4 * n + 3 + 1)) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun x => sumK (Cyclo4.cyc4 LibInt.intR) (fun y => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).add (q4 n x) (q4 n y)) c) (LL n)) (LL n)

theorem step1c (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c x : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun y => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).add (q4 n x) (q4 n y)) c) (LL n) = g4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg (q4 n x))) := by
  rw [← q4_count n hpr ι hi]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun y => by
    unfold delta
    by_cases h : (ring (4 * n + 3 + 1)).add (q4 n x) (q4 n y) = c
    · rw [ite_pos' _ _ h, ite_pos' _ _ (by rw [← h]; lib_ring)]
    · rw [ite_neg' _ _ h, ite_neg' _ _ (fun e => h (by rw [e]; lib_ring))]) _

theorem vals (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chib n ι c) = (Cyclo4.cyc4 LibInt.intR).one ∧ (Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chib n ι c) = rho n ι c ∧ (Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι c) = (Cyclo4.cyc4 LibInt.intR).one := by
  rw [chib_eq n hpr ι hi]; unfold rho
  rcases ResidueCriterion.chi4_cases n ι c with e | e | e | e | e
  · exact absurd e (chi4_ne n hpr ι hi c hc)
  all_goals (rw [e]; exact ⟨rfl, rfl, rfl⟩)

/-- **#{x⁴ + y⁴ = c}.** -/
theorem quart_twist_count (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    Fqc n c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one))
      ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι))))))
      ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one))))
      ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (gconj (J2 n ι))))) := by
  unfold Fqc
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => step1c n hpr ι hi c x), FermatQuartic.step2 n hpr ι hi (fun a => g4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))]
  have scale : ∀ F : Zp (4 * n + 3 + 1) → Int × Int, sumK (Cyclo4.cyc4 LibInt.intR) F (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun t => F ((ring (4 * n + 3 + 1)).mul c t)) (LL n) := fun F =>
    (CharSumGen.sum_scale (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr F c hc).symm
  rw [scale]
  have hct : ∀ t, (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c t)) = (ring (4 * n + 3 + 1)).mul c (om n t) := fun t => by unfold om; lib_ring
  have hexp : ∀ t, (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι ((ring (4 * n + 3 + 1)).mul c t)) (g4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c t))))
      = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι (om n t))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι (om n t)))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chib n ι (om n t)))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι t)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι t))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chib n ι t))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chi4 n ι (om n t)))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (rho n ι (om n t)))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (chib n ι (om n t)))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chib n ι (om n t)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (chi4 n ι (om n t))))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (rho n ι (om n t)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chi4 n ι (om n t))))))
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (rho n ι (om n t)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chib n ι (om n t))))) := fun t => by
    rw [hct]; unfold g4
    rw [chi4_mul n hpr ι hi, chi4_mul n hpr ι hi, rho_mul n hpr ι hi, rho_mul n hpr ι hi, chib_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hexp]
  repeat rw [sumK_add]
  have hp : ∀ (k : Int × Int) (f : Zp (4 * n + 3 + 1) → Int × Int), sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul k (f a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul k (sumK (Cyclo4.cyc4 LibInt.intR) f (LL n)) := fun k f => (mul_sumK (Cyclo4.cyc4 LibInt.intR) k f (LL n)).symm
  have T1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := sum_oneq n _
  have SA : sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chi4_sum n hpr ι hi
  have SR : sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := rho_sum n hpr ι hi
  have SB : sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chib_sum n hpr ι hi
  have SAB := FermatQuartic.T13 n hpr ι hi
  have SBA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := by
    rw [← SAB, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have SAR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a))) (LL n) = J2 n ι := by
    rw [← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have SRB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a))) (LL n) = gconj (J2 n ι) := by
    unfold J2; rw [gconj_sum]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [gconj_mul, gconj_rho n hpr ι hi, chib_eq n hpr ι hi]) _
  have SBR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a))) (LL n) = gconj (J2 n ι) := by
    rw [← SRB, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  rw [T1]
  repeat rw [hp]
  rw [refl_om n (chi4 n ι), refl_om n (rho n ι), refl_om n (chib n ι), SA, SR, SB,
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι (om n a))) (LL n) = Jq n (chi4 n ι) from rfl, J_rho n hpr ι hi,
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a))) (LL n) = gconj (Jq n (chi4 n ι)) by rw [← Jb_conj n hpr ι hi]; rfl,
    SAB, SBA, SAR, show sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι (om n a))) (LL n) = J2 n ι from rfl, SBR, SRB]
  obtain ⟨v1, v2, v3⟩ := vals n hpr ι hi c hc
  rw [show (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι c) = rho n ι c from rfl, v1, v2, v3, chi_rho n hpr ι hi c, chib_rho n hpr ι hi c]
  lib_ring

theorem norm_mul4 (u v : Int × Int) : ((Cyclo4.cyc4 LibInt.intR).mul u v).1 * ((Cyclo4.cyc4 LibInt.intR).mul u v).1 + ((Cyclo4.cyc4 LibInt.intR).mul u v).2 * ((Cyclo4.cyc4 LibInt.intR).mul u v).2 = (u.1 * u.1 + u.2 * u.2) * (v.1 * v.1 + v.2 * v.2) := by
  show (u.1 * v.1 + -(u.2 * v.2)) * (u.1 * v.1 + -(u.2 * v.2)) + (u.1 * v.2 + u.2 * v.1) * (u.1 * v.2 + u.2 * v.1) = _
  lib_int

theorem unit_norm (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (x : Zp (4 * n + 3 + 1)) (hx : x ≠ (ring (4 * n + 3 + 1)).zero) :
    (chib n ι x).1 * (chib n ι x).1 + (chib n ι x).2 * (chib n ι x).2 = 1 ∧ (chi4 n ι x).1 * (chi4 n ι x).1 + (chi4 n ι x).2 * (chi4 n ι x).2 = 1 := by
  rw [chib_eq n hpr ι hi]
  rcases ResidueCriterion.chi4_cases n ι x with e | e | e | e | e
  · exact absurd e (chi4_ne n hpr ι hi x hx)
  all_goals (rw [e]; exact ⟨rfl, rfl⟩)

/-- **Hasse's bound for every quartic twist** y² = x³ − Dx: (Σ_x ρ(x³ − Dx))₁² ≤ 4p. -/
theorem quart_twist_hasse (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero) :
    ∃ s : Nat, (Sq n D ι).1 * (Sq n D ι).1 + Int.ofNat s = 4 * Int.ofNat (4 * n + 3 + 1 + 1) := by
  rw [quart_twist n hpr ι hi D hD, Jcr_eq n hpr ι hi]
  have hN := quart_norm n hpr ι hi
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' (4 * n + 3) (by
    calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  have h1 := (unit_norm n hpr ι hi D hD).1
  have h2 := (unit_norm n hpr ι hi _ m1).2
  have hm := norm_mul4 (chib n ι D) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)))
  rw [norm_mul4 (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)), h1, h2, hN] at hm
  generalize (Cyclo4.cyc4 LibInt.intR).mul (chib n ι D) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι))) = z at hm ⊢
  match StrictHasse.sq_nat z.2 with
  | ⟨k, hk⟩ =>
    refine ⟨4 * (k * k), ?_⟩
    have e1 : Int.ofNat (4 * (k * k)) = 4 * (z.2 * z.2) := by rw [hk]; rfl
    rw [e1, show 4 * Int.ofNat (4 * n + 3 + 1 + 1) = 4 * (1 * (1 * Int.ofNat (4 * n + 3 + 1 + 1))) by lib_int, ← hm]
    show (z.1 + z.1) * (z.1 + z.1) + 4 * (z.2 * z.2) = 4 * (z.1 * z.1 + z.2 * z.2)
    lib_int

theorem J2_eq' (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : J2 n ι = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)) := by
  have hs : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring]; exact chi_one n hpr ι hi
  rw [QuartDup.quart_dup n hpr ι hi]
  calc J2 n ι = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (J2 n ι) := by rw [hs]; lib_ring
    _ = _ := by lib_ring

/-- **Weil's bound for the quartic twists** x⁴ + y⁴ = c (genus 3): (# − (p − 1 − 2χ(−1)))² ≤ 36p. -/
theorem quart_twist_weil (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    ∃ s : Nat, ((Fqc n c).1 + -(Int.ofNat (4 * n + 3 + 1 + 1)) + 1 + 2 * (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)).1) * ((Fqc n c).1 + -(Int.ofNat (4 * n + 3 + 1 + 1)) + 1 + 2 * (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)).1)
      + Int.ofNat s = 36 * Int.ofNat (4 * n + 3 + 1 + 1) := by
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => PowerSum.one_ne_zero' (4 * n + 3) (by
    calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  rw [quart_twist_count n hpr ι hi c hc, J2_eq' n hpr ι hi]
  have hN := quart_norm n hpr ι hi
  have hz : ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)))).1 * ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)))).1
      + ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)))).2 * ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)))).2
      = Int.ofNat (4 * n + 3 + 1 + 1) := by
    rw [norm_mul4, norm_mul4, (unit_norm n hpr ι hi c hc).1, (unit_norm n hpr ι hi _ m1).2, hN]; lib_int
  have hgc : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (gconj ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)))) = gconj ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)))) := by
    rw [gconj_mul (chib n ι c), QuartTwist.gconj_chib n hpr ι hi c]
  rw [hgc]
  have hr := QuartPrimary.rho_real n hpr ι hi c hc
  generalize (Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι))) = z at hz ⊢
  generalize Jq n (chi4 n ι) = Jv at hN ⊢
  generalize Int.ofNat (4 * n + 3 + 1 + 1) = P at hN hz ⊢
  match Jv, z, hN, hz with
  | (a, b), (z1, z2), hN, hz =>
  have hN' : a * a + b * b = P := hN
  have hz' : z1 * z1 + z2 * z2 = P := hz
  have pa : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).add u v).1 = u.1 + v.1 := fun _ _ => rfl
  have pm : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).mul u v).1 = u.1 * v.1 + -(u.2 * v.2) := fun _ _ => rfl
  have pn : ∀ u : Int × Int, ((Cyclo4.cyc4 LibInt.intR).neg u).1 = -u.1 := fun _ => rfl
  have pn2 : ∀ u : Int × Int, ((Cyclo4.cyc4 LibInt.intR).neg u).2 = -u.2 := fun _ => rfl
  have pg : ∀ u : Int × Int, (gconj u).1 = u.1 := fun _ => rfl
  have pg2 : ∀ u : Int × Int, (gconj u).2 = -u.2 := fun _ => rfl
  have pa2 : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).add u v).2 = u.2 + v.2 := fun _ _ => rfl
  have pf : ∀ x y : Int, (x, y).1 = x := fun _ _ => rfl
  have ps : ∀ x y : Int, (x, y).2 = y := fun _ _ => rfl
  have o1 : ((Cyclo4.cyc4 LibInt.intR).one).1 = 1 := rfl
  have o2 : ((Cyclo4.cyc4 LibInt.intR).one).2 = 0 := rfl
  have e36 : 36 * P = 12 * (a * a + b * b) + 24 * (z1 * z1 + z2 * z2) := by rw [hN', hz']; lib_int
  rw [e36]
  repeat (first | rw [pa] | rw [pm] | rw [pn] | rw [pg] | rw [pg2] | rw [pn2] | rw [pa2])
  rcases hr with e | e <;> rw [e] <;> rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> rw [f] <;>
    (repeat rw [o1]) <;> (repeat rw [o2]) <;> (repeat rw [pf]) <;> (repeat rw [ps])
  · match StrictHasse.sq_nat (a + -z1), StrictHasse.sq_nat b, StrictHasse.sq_nat z2 with
    | ⟨u, hu⟩, ⟨v, hv⟩, ⟨w', hw'⟩ =>
      refine ⟨8 * (u * u) + 12 * (v * v) + 24 * (w' * w'), ?_⟩
      rw [show Int.ofNat (8 * (u * u) + 12 * (v * v) + 24 * (w' * w')) = 8 * ((a + -z1) * (a + -z1)) + 12 * (b * b) + 24 * (z2 * z2) by rw [hu, hv, hw']; rfl]
      lib_int
  · match StrictHasse.sq_nat (a + -z1), StrictHasse.sq_nat b, StrictHasse.sq_nat z2 with
    | ⟨u, hu⟩, ⟨v, hv⟩, ⟨w', hw'⟩ =>
      refine ⟨8 * (u * u) + 12 * (v * v) + 24 * (w' * w'), ?_⟩
      rw [show Int.ofNat (8 * (u * u) + 12 * (v * v) + 24 * (w' * w')) = 8 * ((a + -z1) * (a + -z1)) + 12 * (b * b) + 24 * (z2 * z2) by rw [hu, hv, hw']; rfl]
      lib_int
  · match StrictHasse.sq_nat (a + z1), StrictHasse.sq_nat b, StrictHasse.sq_nat z2 with
    | ⟨u, hu⟩, ⟨v, hv⟩, ⟨w', hw'⟩ =>
      refine ⟨8 * (u * u) + 12 * (v * v) + 24 * (w' * w'), ?_⟩
      rw [show Int.ofNat (8 * (u * u) + 12 * (v * v) + 24 * (w' * w')) = 8 * ((a + z1) * (a + z1)) + 12 * (b * b) + 24 * (z2 * z2) by rw [hu, hv, hw']; rfl]
      lib_int
  · match StrictHasse.sq_nat (a + z1), StrictHasse.sq_nat b, StrictHasse.sq_nat z2 with
    | ⟨u, hu⟩, ⟨v, hv⟩, ⟨w', hw'⟩ =>
      refine ⟨8 * (u * u) + 12 * (v * v) + 24 * (w' * w'), ?_⟩
      rw [show Int.ofNat (8 * (u * u) + 12 * (v * v) + 24 * (w' * w')) = 8 * ((a + z1) * (a + z1)) + 12 * (b * b) + 24 * (z2 * z2) by rw [hu, hv, hw']; rfl]
      lib_int

/-- **The quartic twists of the Fermat quartic**: χ(v) = i, ρ(v) = −1 ⇒ Σ_{k=0}^{3} #(x⁴ + y⁴ = v^k c) = 4(p − 1) − 8χ(−1). -/
theorem fermat4_twist_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c v : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chi4 n ι v = (((0 : Int), (1 : Int)))) (hr : rho n ι v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add (Fqc n (c)) ((Cyclo4.cyc4 LibInt.intR).add (Fqc n ((ring (4 * n + 3 + 1)).mul v (c))) ((Cyclo4.cyc4 LibInt.intR).add (Fqc n ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (c)))) (Fqc n ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (c))))))) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyc  -- (+2093 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hc
  have n1 : (ring (4 * n + 3 + 1)).mul v (c) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (c)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (c))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  rw [quart_twist_count n hpr ι hi _ n0, quart_twist_count n hpr ι hi _ n1, quart_twist_count n hpr ι hi _ n2, quart_twist_count n hpr ι hi _ n3]
  have hcv : ∀ E : Zp (4 * n + 3 + 1), chi4 n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi4 n ι E) := fun E => by rw [chi4_mul n hpr ι hi, hv]
  have hbv : ∀ E : Zp (4 * n + 3 + 1), chib n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) (chib n ι E) := fun E => by
    rw [chib_mul n hpr ι hi, chib_eq n hpr ι hi v, hv]; rfl
  have hρ : ∀ E : Zp (4 * n + 3 + 1), rho n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho n ι E) := fun E => by rw [rho_mul n hpr ι hi, hr]
  repeat rw [hcv]
  repeat rw [hbv]
  repeat rw [hρ]
  generalize chi4 n ι c = t
  generalize chib n ι c = tb
  generalize rho n ι c = r
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt  -- (+233 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).mu  -- (+215 chars; full line in the Zenodo source)
  have E3 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt  -- (+233 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.  -- (+2553 chars; full line in the Zenodo source)
          ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul tb ((Cyclo4.cyc4 LibInt.intR).add (J2 n ι) (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR)  -- (+1047 chars; full line in the Zenodo source)
        generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; generalize (((0 : Int)), ((-1 : Int))) = d; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.c  -- (+1793 chars; full line in the Zenodo source)

end QuartTwistCount
#print axioms QuartTwistCount.step1c
#print axioms QuartTwistCount.vals
#print axioms QuartTwistCount.quart_twist_count
#print axioms QuartTwistCount.norm_mul4
#print axioms QuartTwistCount.unit_norm
#print axioms QuartTwistCount.quart_twist_hasse
#print axioms QuartTwistCount.J2_eq'
#print axioms QuartTwistCount.quart_twist_weil
#print axioms QuartTwistCount.fermat4_twist_sum

#print axioms: 9 theorems, no axioms.

78.76 Weil’s diagonal cubic surface

The count reduces to \(\sum _aN(a)\,\#\{y^3+z^3=1-a\}\) and the cubic twists.

\[ \#\{x^3+y^3+z^3=1\}=p^2+6p-(J+\bar J). \]

lang/CubicSurface.lean

/-
  lang/CubicSurface — T43, cycle 248: Weil's diagonal cubic surface x³ + y³ + z³ = 1 over 𝔽_p, p ≡ 1 (mod 3).

  #{x³ + y³ + z³ = 1} = Σ_a N(a) T(1 − a), T(c) = Σ_b N(b)N(c − b) = #{y³ + z³ = c}: T(c) = p − 2 + Tr χ̄(c)J (c ≠ 0,
  CubicTwistCount) and T(0) = 3p − 2 (Σ N² = 3Σ N − 2). Then Σ_a N(a)χ̄(1 − a) = J̄ − 1, Σ_a N(a)χ(1 − a) = J − 1 and J J̄ = p:
      cubic_surface   #{x³ + y³ + z³ = 1} = p² + 6p − (J + J̄)
  (Weil 1949: the eight Jacobi sums J(χ^i, χ^j, χ^k) of the diagonal surface: six equal p, two equal −J and −J̄.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.CubicTwistCount

namespace CubicSurface

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm FermatCubic CubicSextic CubicDup CubicCyclo CubicSix CubicTwistCount
open LibRing (castZ castN)

variable (n : Nat)

def Ns : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun x => sumK (Cyclo3.cyc3 LibInt.intR) (fun y => sumK (Cyclo3.cyc3 LibInt.intR) (fun z => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (cube n x) (cube n y)) (cube n z)) (ring (6 * n + 5 + 1)).one) (LL n)) (LL n)) (LL n)

theorem JJ (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : (Cyclo3.cyc3 LibInt.intR).mul (J n w) (cconj (J n w)) = (Int.ofNat (6 * n + 5 + 1 + 1), 0) := by
  have h := jacobi_norm n hpr w hw1 hw3
  generalize J n w = Jv at h ⊢
  match Jv, h with
  | (a, b), h =>
    apply Prod.ext
    · have h' : a * a + -(a * b) + b * b = Int.ofNat (6 * n + 5 + 1 + 1) := h
      show a * (a + -b) + -(b * -b) = Int.ofNat (6 * n + 5 + 1 + 1); rw [← h']; lib_int
    · show a * -b + b * (a + -b) + -(b * -b) = 0; lib_int

/-- T(c) = Σ_b N(b)N(c − b). -/
def T (w c : Zp (6 * n + 5 + 1)) : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun b => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w b) (g3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg b)))) (LL n)

theorem T_ne (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) :
    T n w c = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (J n w)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cconj (J n w))))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one)) := by
  rw [← twist_count n hpr w hw1 hw3 c hc]
  unfold Ft T
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => step1c n hpr w hw1 hw3 c x), FermatCubic.step2 n hpr w hw1 hw3 (fun a => g3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))]

theorem g3_sum (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : sumK (Cyclo3.cyc3 LibInt.intR) (g3 n w) (LL n) = (Int.ofNat (6 * n + 5 + 1 + 1), 0) := by
  unfold g3
  have T1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun _ : Zp (6 * n + 5 + 1) => (Cyclo3.cyc3 LibInt.intR).one) (LL n) = (Int.ofNat (6 * n + 5 + 1 + 1), 0) := JacobiSum.sum_one n _
  rw [sumK_add, sumK_add, T1, show sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero from chi3_sum n hpr w hw1 hw3, cb_sum n hpr w hw1 hw3]
  apply Prod.ext
  · show Int.ofNat (6 * n + 5 + 1 + 1) + 0 + 0 = Int.ofNat (6 * n + 5 + 1 + 1); lib_int
  · show (0 : Int) + 0 + 0 = 0; rfl

theorem T_zero (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : T n w (ring (6 * n + 5 + 1)).zero = (3 * Int.ofNat (6 * n + 5 + 1 + 1) + -2, 0) := by
  unfold T
  have pt : ∀ b, (Cyclo3.cyc3 LibInt.intR).mul (g3 n w b) (g3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg b))) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (3, 0) (g3 n w b)) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (2, 0) (delta (Cyclo3.cyc3 LibInt.intR) b (ring (6 * n + 5 + 1)).zero))) := fun b => by
    rw [show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg b) = (ring (6 * n + 5 + 1)).neg b by lib_ring, g3_neg n hpr w hw1 hw3, g3_val n hpr w hw1 hw3]
    have dC : delta (Cyclo3.cyc3 LibInt.intR) b (ring (6 * n + 5 + 1)).zero = (delta LibInt.intR b (ring (6 * n + 5 + 1)).zero, 0) := by
      unfold delta; by_cases hz : b = (ring (6 * n + 5 + 1)).zero
      · rw [ite_pos' _ _ hz, ite_pos' _ _ hz]; rfl
      · rw [ite_neg' _ _ hz, ite_neg' _ _ hz]; rfl
    rw [dC]
    have hq : ∀ x : Zp (6 * n + 5 + 1), (delta LibInt.intR x (ring (6 * n + 5 + 1)).zero) * (delta LibInt.intR x (ring (6 * n + 5 + 1)).zero) = delta LibInt.intR x (ring (6 * n + 5 + 1)).zero ∧ Q3 n w x * Q3 n w x = Q3 n w x ∧ delta LibInt.intR x (ring (6 * n + 5 + 1)).zero * Q3 n w x = 0 := fun x => by
      unfold delta Q3
      by_cases hx : x = (ring (6 * n + 5 + 1)).zero
      · rw [ite_pos' _ _ hx, hx, chi3_zero, ite_neg' _ _ (fun e => Nat.noConfusion (Int.ofNat.inj (congrArg Prod.fst e)))]; exact ⟨rfl, rfl, rfl⟩
      · rw [ite_neg' _ _ hx]
        by_cases h1 : chi3 n w x = (1, 0)
        · rw [ite_pos' _ _ h1]; exact ⟨rfl, rfl, rfl⟩
        · rw [ite_neg' _ _ h1]; exact ⟨rfl, rfl, rfl⟩
    obtain ⟨h1, h2, h3⟩ := hq b
    apply Prod.ext
    · show (delta LibInt.intR b (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w b) * (delta LibInt.intR b (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w b) + -(0 * 0) = 3 * (delta LibInt.intR b (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w b) + -(0 * 0) + -(2 * delta LibInt.intR b (ring (6 * n + 5 + 1)).zero + -(0 * 0))
      rw [show (delta LibInt.intR b (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w b) * (delta LibInt.intR b (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w b) = delta LibInt.intR b (ring (6 * n + 5 + 1)).zero * delta LibInt.intR b (ring (6 * n + 5 + 1)).zero + 6 * (delta LibInt.intR b (ring (6 * n + 5 + 1)).zero * Q3 n w b) + 9 * (Q3 n w b * Q3 n w b) by lib_int, h1, h2, h3]
      lib_int
    · show (delta LibInt.intR b (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w b) * 0 + 0 * (delta LibInt.intR b (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w b) + -(0 * 0) = 3 * 0 + 0 * (delta LibInt.intR b (ring (6 * n + 5 + 1)).zero + 3 * Q3 n w b) + -(0 * 0) + -(2 * 0 + 0 * delta LibInt.intR b (ring (6 * n + 5 + 1)).zero + -(0 * 0))
      lib_int
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ pt, sumK_add, ← mul_sumK, sC_neg, ← mul_sumK, g3_sum n hpr w hw1 hw3,
    show sumK (Cyclo3.cyc3 LibInt.intR) (fun b => delta (Cyclo3.cyc3 LibInt.intR) b (ring (6 * n + 5 + 1)).zero) (LL n) = (Cyclo3.cyc3 LibInt.intR).one from CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR) (ring (6 * n + 5 + 1)).zero]
  apply Prod.ext
  · show 3 * Int.ofNat (6 * n + 5 + 1 + 1) + -(0 * 0) + -(2 * 1 + -(0 * 0)) = 3 * Int.ofNat (6 * n + 5 + 1 + 1) + -2; lib_int
  · show 3 * 0 + 0 * Int.ofNat (6 * n + 5 + 1 + 1) + -(0 * 0) + -(2 * 0 + 0 * 1 + -(0 * 0)) = 0; lib_int

theorem stepz (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (X : Zp (6 * n + 5 + 1)) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun z => delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).add X (cube n z)) (ring (6 * n + 5 + 1)).one) (LL n) = g3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg X)) := by
  unfold g3; rw [← cube_count n hpr w hw1 hw3]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun z => by
    unfold delta
    by_cases h : (ring (6 * n + 5 + 1)).add X (cube n z) = (ring (6 * n + 5 + 1)).one
    · rw [ite_pos' _ _ h, ite_pos' _ _ (by rw [← h]; lib_ring)]
    · rw [ite_neg' _ _ h, ite_neg' _ _ (fun e => h (by rw [e]; lib_ring))]) _

def F (w a : Zp (6 * n + 5 + 1)) : Int × Int :=
  (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (cb n w (om1 n a)) (J n w)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w (om1 n a)) (cconj (J n w))))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one))

theorem T_split (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (a : Zp (6 * n + 5 + 1)) :
    T n w (om1 n a) = (Cyclo3.cyc3 LibInt.intR).add (F n w a) ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) (Int.ofNat (6 * n + 5 + 1 + 1), 0))) := by
  by_cases ha : a = (ring (6 * n + 5 + 1)).one
  · rw [ha, show om1 n (ring (6 * n + 5 + 1)).one = (ring (6 * n + 5 + 1)).zero by unfold om1; lib_ring, T_zero n hpr w hw1 hw3]
    unfold F delta; rw [ite_pos' _ _ rfl, show om1 n (ring (6 * n + 5 + 1)).one = (ring (6 * n + 5 + 1)).zero by unfold om1; lib_ring]
    unfold cb; rw [chi3_zero]
    apply Prod.ext
    · show 3 * Int.ofNat (6 * n + 5 + 1 + 1) + -2 = Int.ofNat (6 * n + 5 + 1 + 1) + ((0 * 0 + -(0 * 0)) * (J n w).1 + -((0 * 0 + 0 * 0 + -(0 * 0)) * (J n w).2) + (0 * (cconj (J n w)).1 + -(0 * (cconj (J n w)).2))) + -(1 + 1) + (1 * (Int.ofNat (6 * n + 5 + 1 + 1) + Int.ofNat (6 * n + 5 + 1 + 1)) + -(0 * (0 + 0)))
      lib_int
    · show (0 : Int) = 0 + ((0 * 0 + -(0 * 0)) * (J n w).2 + (0 * 0 + 0 * 0 + -(0 * 0)) * (J n w).1 + -((0 * 0 + 0 * 0 + -(0 * 0)) * (J n w).2) + (0 * (cconj (J n w)).2 + 0 * (cconj (J n w)).1 + -(0 * (cconj (J n w)).2))) + -(0 + 0) + (1 * (0 + 0) + 0 * (Int.ofNat (6 * n + 5 + 1 + 1) + Int.ofNat (6 * n + 5 + 1 + 1)) + -(0 * (0 + 0)))
      lib_int
  · have h1 : om1 n a ≠ (ring (6 * n + 5 + 1)).zero := fun e => ha (by calc a = (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg (om1 n a)) := by unfold om1; lib_ring
                                                         _ = (ring (6 * n + 5 + 1)).one := by rw [e]; lib_ring)
    rw [T_ne n hpr w hw1 hw3 _ h1]
    unfold F delta; rw [ite_neg' _ _ ha]; lib_ring

/-- **Weil's cubic surface**: #{x³ + y³ + z³ = 1} = p² + 6p − (J + J̄). -/
theorem cubic_surface (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    Ns n = (Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1) * Int.ofNat (6 * n + 5 + 1 + 1) + 6 * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (J n w) (cconj (J n w)))) := by
  unfold Ns
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun x => sumK (Cyclo3.cyc3 LibInt.intR) (fun y => g3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add (cube n x) (cube n y))))) (LL n))
    (fun x => sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun y => stepz n hpr w hw1 hw3 _) _)]
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun x => T n w (om1 n (cube n x))) (fun x => by
    rw [FermatCubic.step2 n hpr w hw1 hw3 (fun b => g3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add (cube n x) b))))]
    unfold T
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun b => by rw [show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add (cube n x) b)) = (ring (6 * n + 5 + 1)).add (om1 n (cube n x)) ((ring (6 * n + 5 + 1)).neg b) by unfold om1; lib_ring]) _)]
  rw [FermatCubic.step2 n hpr w hw1 hw3 (fun a => T n w (om1 n a)), sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by rw [T_split n hpr w hw1 hw3 a])]
  have hexp : ∀ a, (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) ((Cyclo3.cyc3 LibInt.intR).add (F n w a) ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) (Int.ofNat (6 * n + 5 + 1 + 1), 0))))
      = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one))))
        ((Cyclo3.cyc3 LibInt.intR).mul (J n w) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (cb n w (om1 n a)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w (om1 n a)))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w (om1 n a))))))
        ((Cyclo3.cyc3 LibInt.intR).mul (cconj (J n w)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w (om1 n a)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w (om1 n a)))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w (om1 n a))))))
        ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one) (g3 n w a)) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) (Int.ofNat (6 * n + 5 + 1 + 1), 0))) := fun a => by unfold F g3; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hexp, sumK_add, sumK_add, sumK_add]
  have hp : ∀ (k : Int × Int) (f : Zp (6 * n + 5 + 1) → Int × Int), sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul k (f a)) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul k (sumK (Cyclo3.cyc3 LibInt.intR) f (LL n)) := fun k f => (mul_sumK (Cyclo3.cyc3 LibInt.intR) k f (LL n)).symm
  have hq : ∀ (k : Int × Int) (f : Zp (6 * n + 5 + 1) → Int × Int), sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (f a) k) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul (sumK (Cyclo3.cyc3 LibInt.intR) f (LL n)) k := fun k f => (sumK_mul (Cyclo3.cyc3 LibInt.intR) k f (LL n)).symm
  rw [hq, g3_sum n hpr w hw1 hw3, hp, hp, sumK_add, sumK_add, sumK_add, sumK_add, hq,
    FiniteRecChain.pick (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (g3 n w) (ring (6 * n + 5 + 1)).one,
    refl1 n (cb n w), refl1 n (chi3 n w), cb_sum n hpr w hw1 hw3, show sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero from chi3_sum n hpr w hw1 hw3,
    SAB n hpr w hw1 hw3, SBB n hpr w hw1 hw3, SBA n hpr w hw1 hw3, show sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w (om1 n a))) (LL n) = J n w from rfl]
  have g1 : g3 n w (ring (6 * n + 5 + 1)).one = (3, 0) := by
    unfold g3 cb; rw [val_of n hpr w hw1 hw3 (ring (6 * n + 5 + 1)).one (PowerSum.one_ne_zero' _) 0 (Nat.zero_lt_succ 2) (CubicChar.pw_one'' (ring (6 * n + 5 + 1)) _)]; rfl
  rw [g1]
  have hJ := JJ n hpr w hw1 hw3
  have hJ' : (Cyclo3.cyc3 LibInt.intR).mul (cconj (J n w)) (J n w) = (Int.ofNat (6 * n + 5 + 1 + 1), 0) := by rw [(Cyclo3.cyc3 LibInt.intR).mul_comm]; exact hJ
  generalize J n w = Jv at hJ hJ' ⊢
  generalize cconj Jv = Kv at hJ hJ' ⊢
  generalize Int.ofNat (6 * n + 5 + 1 + 1) = P at hJ hJ' ⊢
  have e1 : ∀ Y : Int × Int, Y = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (P, 0) (P, 0)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (3, 0) ((Cyclo3.cyc3 LibInt.intR).add (P, 0) (P, 0))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (P, 0) (P, 0)))))
      ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul Jv Kv) ((Cyclo3.cyc3 LibInt.intR).mul Kv Jv))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add Jv Kv)) → Y = (Cyclo3.cyc3 LibInt.intR).add (P * P + 6 * P, 0) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add Jv Kv)) := fun Y hY => by
    rw [hY, hJ, hJ']
    have ad : ∀ x y : Int, (Cyclo3.cyc3 LibInt.intR).add (x, 0) (y, 0) = (x + y, 0) := fun x y => Prod.ext rfl (show (0 : Int) + 0 = 0 from rfl)
    have mu : ∀ x y : Int, (Cyclo3.cyc3 LibInt.intR).mul (x, 0) (y, 0) = (x * y, 0) := fun x y => Prod.ext (show x * y + -(0 * 0) = x * y by lib_int) (show x * 0 + 0 * y + -(0 * 0) = 0 by lib_int)
    have ng : ∀ x : Int, (Cyclo3.cyc3 LibInt.intR).neg (x, 0) = (-x, 0) := fun x => Prod.ext rfl (show -(0 : Int) = 0 from rfl)
    repeat (first | rw [mu] | rw [ad] | rw [ng])
    exact congrArg (fun z => (Cyclo3.cyc3 LibInt.intR).add (z, (0 : Int)) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add Jv Kv))) (show P * P + (3 * (P + P) + -(P + P)) + (P + P) = P * P + 6 * P by lib_int)
  apply e1
  lib_ring

end CubicSurface
#print axioms CubicSurface.JJ
#print axioms CubicSurface.T_ne
#print axioms CubicSurface.g3_sum
#print axioms CubicSurface.T_zero
#print axioms CubicSurface.stepz
#print axioms CubicSurface.T_split
#print axioms CubicSurface.cubic_surface

#print axioms: 7 theorems, no axioms.

78.77 The diagonal quartic surface

The fibres \(x^4=a\) and the quartic twists \(y^4+z^4=1-a\), with \(\#\{y^4+z^4=0\}=4p-3\) or \(1\).

\[ \#\{x^4+y^4+z^4=1\}=p^2+17p+4a^2-6a\ \ (p\equiv 1\ (8)),\qquad p^2-3p-4a^2-6a\ \ (p\equiv 5\ (8)). \]

lang/QuartSurface.lean

/-
  lang/QuartSurface — T43, cycles 249–250: the diagonal quartic (K3) surface x⁴ + y⁴ + z⁴ = 1 over 𝔽_p, p ≡ 1 (mod 4).

  #{x⁴ + y⁴ + z⁴ = 1} = Σ_a N(a) T(1 − a), T(c) = #{y⁴ + z⁴ = c} (QuartTwistCount for c ≠ 0; T(0) = 4p − 3 or 1 as χ(−1) = ±1).
      quart_surface_odd    p ≡ 1 (mod 8):  #{x⁴ + y⁴ + z⁴ = 1} = p² + 17p + 4a² − 6a
      quart_surface_even   p ≡ 5 (mod 8):  #{x⁴ + y⁴ + z⁴ = 1} = p² − 3p − 4a² − 6a          (J = a + bi)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.QuartTwistCount
import lang.QuartTwo

namespace QuartSurface

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup FermatQuartic QuartTwist QuartTwistCount
open LibRing (castZ castN)

variable (n : Nat)

def N4 : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun x => sumK (Cyclo4.cyc4 LibInt.intR) (fun y => sumK (Cyclo4.cyc4 LibInt.intR) (fun z => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).add ((ring (4 * n + 3 + 1)).add (q4 n x) (q4 n y)) (q4 n z)) (ring (4 * n + 3 + 1)).one) (LL n)) (LL n)) (LL n)

def T (ι c : Zp (4 * n + 3 + 1)) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun b => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι b) (g4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg b)))) (LL n)

theorem T_eq (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) :
    T n ι c = Fqc n c := by
  unfold Fqc T
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun x => step1c n hpr ι hi c x), FermatQuartic.step2 n hpr ι hi (fun a => g4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))]

theorem g4_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (g4 n ι) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := by
  unfold g4
  have T1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := sum_oneq n _
  rw [sumK_add, sumK_add, sumK_add, T1, show sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from chi4_sum n hpr ι hi,
    show sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from rho_sum n hpr ι hi, show sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from chib_sum n hpr ι hi]
  apply Prod.ext
  · show Int.ofNat (4 * n + 3 + 1 + 1) + 0 + 0 + 0 = Int.ofNat (4 * n + 3 + 1 + 1); lib_int
  · show (0 : Int) + 0 + 0 + 0 = 0; rfl

/-- g4(b)g4(−b) by the sign of χ(−1). -/
theorem gg_odd (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hs : chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (1, 0)) (b : Zp (4 * n + 3 + 1)) :
    (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι b) (g4 n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg b))) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (4, 0) (g4 n ι b)) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul (3, 0) (delta (Cyclo4.cyc4 LibInt.intR) b (ring (4 * n + 3 + 1)).zero))) := by
  rw [show (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg b) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) b by lib_ring]
  unfold g4
  rw [chi4_mul n hpr ι hi, rho_mul n hpr ι hi, chib_mul n hpr ι hi, rho_neg1 n hpr ι hi, chib_eq n hpr ι hi ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one), hs]
  unfold rho; rw [chib_eq n hpr ι hi]
  by_cases hb : b = (ring (4 * n + 3 + 1)).zero
  · rw [hb, chi4_zero]; unfold delta; rw [ite_pos' _ _ rfl]; rfl
  · unfold delta; rw [ite_neg' _ _ hb]
    rcases ResidueCriterion.chi4_cases n ι b with e | e | e | e | e
    · exact absurd e (chi4_ne n hpr ι hi b hb)
    all_goals (rw [e]; rfl)

theorem gg_even (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hs : chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (-1, 0)) (b : Zp (4 * n + 3 + 1)) :
    (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι b) (g4 n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg b))) = delta (Cyclo4.cyc4 LibInt.intR) b (ring (4 * n + 3 + 1)).zero := by
  rw [show (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg b) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) b by lib_ring]
  unfold g4
  rw [chi4_mul n hpr ι hi, rho_mul n hpr ι hi, chib_mul n hpr ι hi, rho_neg1 n hpr ι hi, chib_eq n hpr ι hi ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one), hs]
  unfold rho; rw [chib_eq n hpr ι hi]
  by_cases hb : b = (ring (4 * n + 3 + 1)).zero
  · rw [hb, chi4_zero]; unfold delta; rw [ite_pos' _ _ rfl]; rfl
  · unfold delta; rw [ite_neg' _ _ hb]
    rcases ResidueCriterion.chi4_cases n ι b with e | e | e | e | e
    · exact absurd e (chi4_ne n hpr ι hi b hb)
    all_goals (rw [e]; rfl)

theorem T_zero_odd (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hs : chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (1, 0)) :
    T n ι (ring (4 * n + 3 + 1)).zero = (4 * Int.ofNat (4 * n + 3 + 1 + 1) + -3, 0) := by
  unfold T
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (gg_odd n hpr ι hi hs), sumK_add, ← mul_sumK, QuartCyclo.sC_neg, ← mul_sumK, g4_sum n hpr ι hi,
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun b => delta (Cyclo4.cyc4 LibInt.intR) b (ring (4 * n + 3 + 1)).zero) (LL n) = (Cyclo4.cyc4 LibInt.intR).one from CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) (ring (4 * n + 3 + 1)).zero]
  apply Prod.ext
  · show 4 * Int.ofNat (4 * n + 3 + 1 + 1) + -(0 * 0) + -(3 * 1 + -(0 * 0)) = 4 * Int.ofNat (4 * n + 3 + 1 + 1) + -3; lib_int
  · show 4 * 0 + 0 * Int.ofNat (4 * n + 3 + 1 + 1) + -(3 * 0 + 0 * 1) = 0; lib_int

theorem T_zero_even (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hs : chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (-1, 0)) :
    T n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).one := by
  unfold T
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (gg_even n hpr ι hi hs)]
  exact CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) (ring (4 * n + 3 + 1)).zero

theorem stepz (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (X : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun z => delta (Cyclo4.cyc4 LibInt.intR) ((ring (4 * n + 3 + 1)).add X (q4 n z)) (ring (4 * n + 3 + 1)).one) (LL n) = g4 n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg X)) := by
  rw [← q4_count n hpr ι hi]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun z => by
    unfold delta
    by_cases h : (ring (4 * n + 3 + 1)).add X (q4 n z) = (ring (4 * n + 3 + 1)).one
    · rw [ite_pos' _ _ h, ite_pos' _ _ (by rw [← h]; lib_ring)]
    · rw [ite_neg' _ _ h, ite_neg' _ _ (fun e => h (by rw [e]; lib_ring))]) _

theorem N4_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    N4 n = sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (T n ι (om n a))) (LL n) := by
  unfold N4
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun x => sumK (Cyclo4.cyc4 LibInt.intR) (fun y => g4 n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add (q4 n x) (q4 n y))))) (LL n))
    (fun x => sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun y => stepz n hpr ι hi _) _)]
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun x => T n ι (om n (q4 n x))) (fun x => by
    rw [FermatQuartic.step2 n hpr ι hi (fun b => g4 n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add (q4 n x) b))))]
    unfold T
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun b => by rw [show (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add (q4 n x) b)) = (ring (4 * n + 3 + 1)).add (om n (q4 n x)) ((ring (4 * n + 3 + 1)).neg b) by unfold om; lib_ring]) _)]
  exact FermatQuartic.step2 n hpr ι hi (fun a => T n ι (om n a))

/-- the twist count as a function of a, with c = 1 − a. -/
def F4 (ι a : Zp (4 * n + 3 + 1)) : Int × Int :=
  (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one))
      ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι (om n a)) ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι))))))
      ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one))))
      ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι (om n a)) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι (om n a)) (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι (om n a)) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι (om n a)) (gconj (J2 n ι)))))

def A0 (ι : Zp (4 * n + 3 + 1)) : Int × Int :=
  (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring  -- (+114 chars; full line in the Zenodo source)

theorem T_split (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) :
    T n ι (om n a) = (Cyclo4.cyc4 LibInt.intR).add (F4 n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one) ((Cyclo4.cyc4 LibInt.intR).add (T n ι (ring (4 * n + 3 + 1)).zero) ((Cyclo4.cyc4 LibInt.intR).neg (A0 n ι)))) := by
  by_cases ha : a = (ring (4 * n + 3 + 1)).one
  · rw [ha, show om n (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero by unfold om; lib_ring]
    have c0 : chi4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero := chi4_zero n ι
    unfold F4 A0 delta; rw [ite_pos' _ _ rfl, show om n (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero by unfold om; lib_ring, rho_zero, chib_zero, c0]
    show _ = (Cyclo4.cyc4 LibInt.intR).add _ ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).one _); lib_ring
  · have h1 : om n a ≠ (ring (4 * n + 3 + 1)).zero := fun e => ha (by calc a = (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).one ((ring (4 * n + 3 + 1)).neg (om n a)) := by unfold om; lib_ring
                                                        _ = (ring (4 * n + 3 + 1)).one := by rw [e]; lib_ring)
    rw [T_eq n hpr ι hi, quart_twist_count n hpr ι hi _ h1]
    unfold F4 delta; rw [ite_neg' _ _ ha]; lib_ring

theorem N4_formula (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    N4 n = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), 0) (A0 n ι))
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (J2 n ι) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one))))
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (J2 n ι) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) (gconj (J2 n ι))) (gconj (Jq n (chi4 n ι))))))
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (gconj (J2 n ι)) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)))))
      ((Cyclo4.cyc4 LibInt.intR).mul (4, 0) ((Cyclo4.cyc4 LibInt.intR).add (T n ι (ring (4 * n + 3 + 1)).zero) ((Cyclo4.cyc4 LibInt.intR).neg (A0 n ι)))) := by
  rw [N4_sum n hpr ι hi, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [T_split n hpr ι hi a])]
  have hexp : ∀ a, (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) ((Cyclo4.cyc4 LibInt.intR).add (F4 n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one) ((Cyclo4.cyc4 LibInt.intR).add (T n ι (ring (4 * n + 3 + 1)).zero) ((Cyclo4.cyc4 LibInt.intR).neg (A0 n ι)))))
      = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (A0 n ι))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (rho n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (J2 n ι) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chib n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (gconj (J2 n ι)) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chi4 n ι (om n a)))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).one) (g4 n ι a)) ((Cyclo4.cyc4 LibInt.intR).add (T n ι (ring (4 * n + 3 + 1)).zero) ((Cyclo4.cyc4 LibInt.intR).neg (A0 n ι)))) := fun a => by unfold F4 A0; lib_ring
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hexp, sumK_add, sumK_add, sumK_add, sumK_add]
  have hp : ∀ (k : Int × Int) (f : Zp (4 * n + 3 + 1) → Int × Int), sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul k (f a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul k (sumK (Cyclo4.cyc4 LibInt.intR) f (LL n)) := fun k f => (mul_sumK (Cyclo4.cyc4 LibInt.intR) k f (LL n)).symm
  have hq : ∀ (k : Int × Int) (f : Zp (4 * n + 3 + 1) → Int × Int), sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (f a) k) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) f (LL n)) k := fun k f => (sumK_mul (Cyclo4.cyc4 LibInt.intR) k f (LL n)).symm
  have SA : sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chi4_sum n hpr ι hi
  have SR : sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := rho_sum n hpr ι hi
  have SB : sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chib_sum n hpr ι hi
  have SAB := FermatQuartic.T13 n hpr ι hi
  have SBA : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := by
    rw [← SAB, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have SAR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a))) (LL n) = J2 n ι := by
    rw [← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have SRB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a))) (LL n) = gconj (J2 n ι) := by
    unfold J2; rw [gconj_sum]
    exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [gconj_mul, gconj_rho n hpr ι hi, chib_eq n hpr ι hi]) _
  have SBR : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a))) (LL n) = gconj (J2 n ι) := by
    rw [← SRB, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
  have SBB : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a))) (LL n) = gconj (Jq n (chi4 n ι)) := by rw [← Jb_conj n hpr ι hi]; rfl
  have R1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (rho n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (J2 n ι) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := by
    unfold g4
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun a => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (rho n ι (om n a)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a)))) (fun a => by lib_ring),
      sumK_add, sumK_add, sumK_add, refl_om n (rho n ι), SR, SAR, J_rho n hpr ι hi, SBR]; lib_ring
  have R2 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chib n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) (gconj (J2 n ι))) (gconj (Jq n (chi4 n ι))) := by
    unfold g4
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun a => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chib n ι (om n a)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a)))) (fun a => by lib_ring),
      sumK_add, sumK_add, sumK_add, refl_om n (chib n ι), SB, SAB, SRB, SBB]; lib_ring
  have R3 : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chi4 n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) := by
    unfold g4
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun a => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chi4 n ι (om n a)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι (om n a)))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a)))) (fun a => by lib_ring),
      sumK_add, sumK_add, sumK_add, refl_om n (chi4 n ι), SA, show sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι (om n a))) (LL n) = Jq n (chi4 n ι) from rfl,
      show sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι (om n a))) (LL n) = J2 n ι from rfl, SBA]; lib_ring
  have g1 : g4 n ι (ring (4 * n + 3 + 1)).one = (4, 0) := by unfold g4; rw [chi_one n hpr ι hi, rho_one n hpr ι hi, chib_one n hpr ι hi]; rfl
  rw [hq, g4_sum n hpr ι hi, hp, R1, hp, R2, hp, R3, hq, FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (g4 n ι) (ring (4 * n + 3 + 1)).one, g1]

theorem J2_eq (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : J2 n ι = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (Jq n (chi4 n ι)) := by
  have hs : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [← chi4_mul n hpr ι hi, show (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = (ring (4 * n + 3 + 1)).one by lib_ring]; exact chi_one n hpr ι hi
  rw [QuartDup.quart_dup n hpr ι hi]
  calc J2 n ι = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one))) (J2 n ι) := by rw [hs]; lib_ring
    _ = _ := by lib_ring

theorem proj (u v : Int × Int) : ((Cyclo4.cyc4 LibInt.intR).add u v).1 = u.1 + v.1 ∧ ((Cyclo4.cyc4 LibInt.intR).add u v).2 = u.2 + v.2 ∧ ((Cyclo4.cyc4 LibInt.intR).mul u v).1 = u.1 * v.1 + -(u.2 * v.2) ∧
    ((Cyclo4.cyc4 LibInt.intR).mul u v).2 = u.1 * v.2 + u.2 * v.1 ∧ ((Cyclo4.cyc4 LibInt.intR).neg u).1 = -u.1 ∧ ((Cyclo4.cyc4 LibInt.intR).neg u).2 = -u.2 ∧ (gconj u).1 = u.1 ∧ (gconj u).2 = -u.2 :=
  ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩

/-- **p ≡ 1 (mod 8)**: #{x⁴ + y⁴ + z⁴ = 1} = p² + 17p + 4a² − 6a. -/
theorem quart_surface_odd (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (m : Nat) (hm : n = 2 * m + 1) :
    (N4 n).1 = Int.ofNat (4 * n + 3 + 1 + 1) * Int.ofNat (4 * n + 3 + 1 + 1) + 17 * Int.ofNat (4 * n + 3 + 1 + 1)
      + 4 * ((Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1) + -(6 * (Jq n (chi4 n ι)).1) := by
  have hs := QuartTwo.chi_m1_odd n hpr ι hi m hm
  have hN := quart_norm n hpr ι hi
  rw [N4_formula n hpr ι hi, T_zero_odd n hpr ι hi hs, J2_eq n hpr ι hi]
  unfold A0
  rw [hs]
  generalize Jq n (chi4 n ι) = Jv at hN ⊢
  generalize Int.ofNat (4 * n + 3 + 1 + 1) = P at hN ⊢
  match Jv, hN with
  | (a, b), hN =>
  subst hN
  have pa : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).add u v).1 = u.1 + v.1 := fun u v => (proj u v).1
  have pa2 : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).add u v).2 = u.2 + v.2 := fun u v => (proj u v).2.1
  have pm : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).mul u v).1 = u.1 * v.1 + -(u.2 * v.2) := fun u v => (proj u v).2.2.1
  have pm2 : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).mul u v).2 = u.1 * v.2 + u.2 * v.1 := fun u v => (proj u v).2.2.2.1
  have pn : ∀ u : Int × Int, ((Cyclo4.cyc4 LibInt.intR).neg u).1 = -u.1 := fun u => (proj u u).2.2.2.2.1
  have pn2 : ∀ u : Int × Int, ((Cyclo4.cyc4 LibInt.intR).neg u).2 = -u.2 := fun u => (proj u u).2.2.2.2.2.1
  have pg : ∀ u : Int × Int, (gconj u).1 = u.1 := fun u => (proj u u).2.2.2.2.2.2.1
  have pg2 : ∀ u : Int × Int, (gconj u).2 = -u.2 := fun u => (proj u u).2.2.2.2.2.2.2
  repeat (first | rw [pa] | rw [pa2] | rw [pm] | rw [pm2] | rw [pn] | rw [pn2] | rw [pg] | rw [pg2])
  have pf : ∀ x y : Int, (x, y).1 = x := fun _ _ => rfl
  have ps : ∀ x y : Int, (x, y).2 = y := fun _ _ => rfl
  have o1 : ((Cyclo4.cyc4 LibInt.intR).one).1 = 1 := rfl
  have o2 : ((Cyclo4.cyc4 LibInt.intR).one).2 = 0 := rfl
  repeat rw [o1]
  repeat rw [o2]
  repeat rw [pf]
  repeat rw [ps]
  lib_int


/-- **p ≡ 5 (mod 8)**: #{x⁴ + y⁴ + z⁴ = 1} = p² − 3p − 4a² − 6a. -/
theorem quart_surface_even (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (m : Nat) (hm : n = 2 * m) :
    (N4 n).1 = Int.ofNat (4 * n + 3 + 1 + 1) * Int.ofNat (4 * n + 3 + 1 + 1) + -(3 * Int.ofNat (4 * n + 3 + 1 + 1))
      + -(4 * ((Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1)) + -(6 * (Jq n (chi4 n ι)).1) := by
  have hs := QuartTwo.chi_m1_even n hpr ι hi m hm
  have hN := quart_norm n hpr ι hi
  rw [N4_formula n hpr ι hi, T_zero_even n hpr ι hi hs, J2_eq n hpr ι hi]
  unfold A0
  rw [hs]
  generalize Jq n (chi4 n ι) = Jv at hN ⊢
  generalize Int.ofNat (4 * n + 3 + 1 + 1) = P at hN ⊢
  match Jv, hN with
  | (a, b), hN =>
  subst hN
  have pa : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).add u v).1 = u.1 + v.1 := fun u v => (proj u v).1
  have pa2 : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).add u v).2 = u.2 + v.2 := fun u v => (proj u v).2.1
  have pm : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).mul u v).1 = u.1 * v.1 + -(u.2 * v.2) := fun u v => (proj u v).2.2.1
  have pm2 : ∀ u v : Int × Int, ((Cyclo4.cyc4 LibInt.intR).mul u v).2 = u.1 * v.2 + u.2 * v.1 := fun u v => (proj u v).2.2.2.1
  have pn : ∀ u : Int × Int, ((Cyclo4.cyc4 LibInt.intR).neg u).1 = -u.1 := fun u => (proj u u).2.2.2.2.1
  have pn2 : ∀ u : Int × Int, ((Cyclo4.cyc4 LibInt.intR).neg u).2 = -u.2 := fun u => (proj u u).2.2.2.2.2.1
  have pg : ∀ u : Int × Int, (gconj u).1 = u.1 := fun u => (proj u u).2.2.2.2.2.2.1
  have pg2 : ∀ u : Int × Int, (gconj u).2 = -u.2 := fun u => (proj u u).2.2.2.2.2.2.2
  repeat (first | rw [pa] | rw [pa2] | rw [pm] | rw [pm2] | rw [pn] | rw [pn2] | rw [pg] | rw [pg2])
  have pf : ∀ x y : Int, (x, y).1 = x := fun _ _ => rfl
  have ps : ∀ x y : Int, (x, y).2 = y := fun _ _ => rfl
  have o1 : ((Cyclo4.cyc4 LibInt.intR).one).1 = 1 := rfl
  have o2 : ((Cyclo4.cyc4 LibInt.intR).one).2 = 0 := rfl
  repeat rw [o1]
  repeat rw [o2]
  repeat rw [pf]
  repeat rw [ps]
  lib_int

end QuartSurface
#print axioms QuartSurface.T_eq
#print axioms QuartSurface.g4_sum
#print axioms QuartSurface.gg_odd
#print axioms QuartSurface.gg_even
#print axioms QuartSurface.T_zero_odd
#print axioms QuartSurface.T_zero_even
#print axioms QuartSurface.stepz
#print axioms QuartSurface.N4_sum
#print axioms QuartSurface.T_split
#print axioms QuartSurface.N4_formula
#print axioms QuartSurface.J2_eq
#print axioms QuartSurface.proj
#print axioms QuartSurface.quart_surface_odd
#print axioms QuartSurface.quart_surface_even

#print axioms: 14 theorems, no axioms.

78.78 The diagonal cubic threefold

Fibres over the last coordinate: the surfaces \(x^3+y^3+z^3=c\), then the threefold.

\[ \#\{x^3+y^3+z^3=c\}=p^2-\mathrm {Tr}\,J+3p(\chi (c)+\bar \chi (c)),\qquad \#\{x^3+y^3+z^3+u^3=1\}=p^3+5p\,\mathrm {Tr}\,J-6p. \]

lang/CubicThreefold.lean

/-
  lang/CubicThreefold — T43, cycles 251–252: the diagonal cubic surfaces x³ + y³ + z³ = c and the cubic threefold
  x³ + y³ + z³ + u³ = 1 over 𝔽_p, p ≡ 1 (mod 3).

      surface_c      #{x³ + y³ + z³ = c} = p² − Tr J + 3p(χ(c) + χ̄(c))     (c ≠ 0)
      surface_0      #{x³ + y³ + z³ = 0} = p² + (p − 1) Tr J
      threefold      #{x³ + y³ + z³ + u³ = 1} = p³ + 5p Tr J − 6p
  (fibres over the last coordinate, CubicSurface.T for the twists y³ + z³ = c).
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.CubicSurface

namespace CubicThreefold

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm FermatCubic CubicSextic CubicDup CubicCyclo CubicSix CubicTwistCount CubicSurface
open LibRing (castZ castN)

variable (n : Nat)

/-- U(c) = Σ_a N(a) T(c − a) = #{x³ + y³ + z³ = c}. -/
def U (w c : Zp (6 * n + 5 + 1)) : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (T n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n)

theorem hp (k : Int × Int) (f : Zp (6 * n + 5 + 1) → Int × Int) : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul k (f a)) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul k (sumK (Cyclo3.cyc3 LibInt.intR) f (LL n)) := (mul_sumK (Cyclo3.cyc3 LibInt.intR) k f (LL n)).symm
theorem hq (k : Int × Int) (f : Zp (6 * n + 5 + 1) → Int × Int) : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (f a) k) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul (sumK (Cyclo3.cyc3 LibInt.intR) f (LL n)) k := (sumK_mul (Cyclo3.cyc3 LibInt.intR) k f (LL n)).symm

theorem T_split_c (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c a : Zp (6 * n + 5 + 1)) :
    T n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a))) (J n w)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n +   -- (+190 chars; full line in the Zenodo source)
      ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a c) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) (Int.ofNat (6 * n + 5 + 1 + 1), 0))) := by
  by_cases ha : a = c
  · rw [ha, show (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg c) = (ring (6 * n + 5 + 1)).zero by lib_ring, T_zero n hpr w hw1 hw3]
    unfold cb delta; rw [ite_pos' _ _ rfl, chi3_zero]
    apply Prod.ext
    · show 3 * Int.ofNat (6 * n + 5 + 1 + 1) + -2 = Int.ofNat (6 * n + 5 + 1 + 1) + ((0 * 0 + -(0 * 0)) * (J n w).1 + -((0 * 0 + 0 * 0 + -(0 * 0)) * (J n w).2) + (0 * (cconj (J n w)).1 + -(0 * (cconj (J n w)).2))) + -(1 + 1) + (1 * (Int.ofNat (6 * n + 5 + 1 + 1) + Int.ofNat (6 * n + 5 + 1 + 1)) + -(0 * (0 + 0)))
      lib_int
    · show (0 : Int) = 0 + ((0 * 0 + -(0 * 0)) * (J n w).2 + (0 * 0 + 0 * 0 + -(0 * 0)) * (J n w).1 + -((0 * 0 + 0 * 0 + -(0 * 0)) * (J n w).2) + (0 * (cconj (J n w)).2 + 0 * (cconj (J n w)).1 + -(0 * (cconj (J n w)).2))) + -(0 + 0) + (1 * (0 + 0) + 0 * (Int.ofNat (6 * n + 5 + 1 + 1) + Int.ofNat (6 * n + 5 + 1 + 1)) + -(0 * (0 + 0)))
      lib_int
  · have h1 : (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a) ≠ (ring (6 * n + 5 + 1)).zero := fun e => ha (by calc a = (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a))) := by lib_ring
                                                                  _ = c := by rw [e]; lib_ring)
    rw [T_ne n hpr w hw1 hw3 _ h1]
    unfold delta; rw [ite_neg' _ _ ha]; lib_ring

/-- **x³ + y³ + z³ = c**, c ≠ 0. -/
theorem surface_c (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) :
    U n w c = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1) * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (J n w) (cconj (J n w)))))
      ((Cyclo3.cyc3 LibInt.intR).mul (3 * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w c) (cb n w c))) := by
  unfold U
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by rw [T_split_c n hpr w hw1 hw3 c a])]
  have hexp : ∀ a, (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a))) (J n w)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w ((ring (6 * n + 5 + 1)).add c  -- (+177 chars; full line in the Zenodo source)
      ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a c) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) (Int.ofNat (6 * n + 5 + 1 + 1), 0))))
      = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one))))
        ((Cyclo3.cyc3 LibInt.intR).mul (J n w) ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a))))))
        ((Cyclo3.cyc3 LibInt.intR).mul (cconj (J n w)) ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a))))))
        ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a c) (g3 n w a)) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) (Int.ofNat (6 * n + 5 + 1 + 1), 0))) := fun a => by lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hexp, sumK_add, sumK_add, sumK_add]
  have scale : ∀ F : Zp (6 * n + 5 + 1) → Int × Int, sumK (Cyclo3.cyc3 LibInt.intR) F (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun t => F ((ring (6 * n + 5 + 1)).mul c t)) (LL n) := fun F =>
    (CharSumGen.sum_scale (Cyclo3.cyc3 LibInt.intR) (6 * n + 4) hpr F c hc).symm
  have hct : ∀ t, (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul c t)) = (ring (6 * n + 5 + 1)).mul c (om1 n t) := fun t => by unfold om1; lib_ring
  have hcc := chi_cb n hpr w hw1 hw3 c hc
  have SA : sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := chi3_sum n hpr w hw1 hw3
  have SB : sumK (Cyclo3.cyc3 LibInt.intR) (cb n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := cb_sum n hpr w hw1 hw3
  have R1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cconj (J n w))) := by
    rw [scale]
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun t => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w (om1 n t))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cb n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (cb n w (om1 n t)))))
        ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w t) (cb n w (om1 n t))))) (fun t => by
      rw [hct]; unfold g3; rw [chi3_mul n hpr w hw1 hw3, cb_mul n hpr w hw1 hw3, cb_mul n hpr w hw1 hw3]; lib_ring)]
    rw [sumK_add, sumK_add, hp, hp, hp, refl1 n (cb n w), SB, hcc, SAB n hpr w hw1 hw3, SBB n hpr w hw1 hw3,
      show (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w c) = chi3 n w c by
        calc (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w c) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cb n w c)) (chi3 n w c) := by unfold cb; lib_ring
          _ = chi3 n w c := by rw [hcc]; lib_ring]
    lib_ring
  have R2 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (J n w)) := by
    rw [scale]
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun t => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w (om1 n t))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (chi3 n w (om1 n t)))))
        ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (chi3 n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w t) (chi3 n w (om1 n t))))) (fun t => by
      rw [hct]; unfold g3; rw [chi3_mul n hpr w hw1 hw3, chi3_mul n hpr w hw1 hw3, cb_mul n hpr w hw1 hw3]; lib_ring)]
    rw [sumK_add, sumK_add, hp, hp, hp, refl1 n (chi3 n w), SA, show sumK (Cyclo3.cyc3 LibInt.intR) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (chi3 n w (om1 n t))) (LL n) = J n w from rfl,
      SBA n hpr w hw1 hw3, show (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w c) = cb n w c from rfl, show (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (chi3 n w c) = (Cyclo3.cyc3 LibInt.intR).one by rw [(Cyclo3.cyc3 LibInt.intR).mul_comm]; exact hcc]
    lib_ring
  rw [hq, g3_sum n hpr w hw1 hw3, hp, R1, hp, R2, hq, FiniteRecChain.pick (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (g3 n w) c]
  have hJ := JJ n hpr w hw1 hw3
  have hJ' : (Cyclo3.cyc3 LibInt.intR).mul (cconj (J n w)) (J n w) = (Int.ofNat (6 * n + 5 + 1 + 1), 0) := by rw [(Cyclo3.cyc3 LibInt.intR).mul_comm]; exact hJ
  unfold g3
  generalize J n w = Jv at hJ hJ' ⊢
  generalize cconj Jv = Kv at hJ hJ' ⊢
  generalize Int.ofNat (6 * n + 5 + 1 + 1) = P at hJ hJ' ⊢
  have mu : (Cyclo3.cyc3 LibInt.intR).mul (P, 0) (P, 0) = (P * P, 0) := Prod.ext (show P * P + -(0 * 0) = P * P by lib_int) (show P * 0 + 0 * P + -(0 * 0) = 0 by lib_int)
  have ad3 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (P, 0) (P, 0)) (P, 0) = (3 * P, 0) := Prod.ext (show P + P + P = 3 * P by lib_int) (show (0 : Int) + 0 + 0 = 0 from rfl)
  rw [← mu, ← ad3]
  have e1 : ∀ Y0 : Int × Int, Y0 = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (P, 0) (P, 0)) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add Jv Kv)))
      ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) ((Cyclo3.cyc3 LibInt.intR).mul Jv Kv)) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) ((Cyclo3.cyc3 LibInt.intR).mul Kv Jv))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (P, 0) (P, 0)) (chi3 n w c)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3   -- (+47 chars; full line in the Zenodo source)
      Y0 = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (P, 0) (P, 0)) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add Jv Kv))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (P, 0) (P, 0)) (P, 0)) ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w c) (cb n w c))) := fun Y0 h => by
    rw [h, hJ, hJ']; lib_ring
  apply e1
  lib_ring

theorem chi_neg (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (a : Zp (6 * n + 5 + 1)) : chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg a)) = chi3 n w a ∧ cb n w ((ring (6 * n + 5 + 1)).add (ring (6   -- (+67 chars; full line in the Zenodo source)
  rw [show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg a) = (ring (6 * n + 5 + 1)).mul ((ring (6 * n + 5 + 1)).neg (ring (6 * n + 5 + 1)).one) a by lib_ring]
  constructor
  · rw [chi3_mul n hpr w hw1 hw3, chi3_neg1 n hpr w hw1 hw3]; lib_ring
  · unfold cb; rw [chi3_mul n hpr w hw1 hw3, chi3_neg1 n hpr w hw1 hw3]; lib_ring

theorem chicb_sum (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w a)) (LL n) = (Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.  -- (+58 chars; full line in the Zenodo source)
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun a => (Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).neg (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).zero))) (fun a => by
    by_cases ha : a = (ring (6 * n + 5 + 1)).zero
    · rw [ha]; unfold cb delta; rw [chi3_zero, ite_pos' _ _ rfl]; show (Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (Cyclo3.cyc3 LibInt.intR).zero) = _; lib_ring
    · rw [chi_cb n hpr w hw1 hw3 a ha]; unfold delta; rw [ite_neg' _ _ ha]; lib_ring)]
  have T1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun _ : Zp (6 * n + 5 + 1) => (Cyclo3.cyc3 LibInt.intR).one) (LL n) = (Int.ofNat (6 * n + 5 + 1 + 1), 0) := JacobiSum.sum_one n _
  rw [sumK_add, CubicCyclo.sC_neg n, T1, show sumK (Cyclo3.cyc3 LibInt.intR) (fun a => delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).zero) (LL n) = (Cyclo3.cyc3 LibInt.intR).one from CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR) (ring (6 * n + 5 + 1)).zero]

/-- **x³ + y³ + z³ = 0**: p² + (p − 1) Tr J. -/
theorem surface_0 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    U n w (ring (6 * n + 5 + 1)).zero = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Int.ofNat (6 * n + 5 + 1 + 1), 0) (Int.ofNat (6 * n + 5 + 1 + 1), 0))
      ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).add (J n w) (cconj (J n w)))) := by
  unfold U
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by rw [T_split_c n hpr w hw1 hw3 (ring (6 * n + 5 + 1)).zero a, (chi_neg n hpr w hw1 hw3 a).1, (chi_neg n hpr w hw1 hw3 a).2])]
  have hexp : ∀ a, (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (J n w)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cconj (J n w))))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (Cycl  -- (+57 chars; full line in the Zenodo source)
      ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).zero) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) (Int.ofNat (6 * n + 5 + 1 + 1), 0))))
      = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one))))
        ((Cyclo3.cyc3 LibInt.intR).mul (J n w) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (cb n w a) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w a))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w a)))))
        ((Cyclo3.cyc3 LibInt.intR).mul (cconj (J n w)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w a) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w a))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w a)))))
        ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).zero) (g3 n w a)) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) (Int.ofNat (6 * n + 5 + 1 + 1), 0))) := fun a => by unfold g3; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hexp, sumK_add, sumK_add, sumK_add]
  have SA : sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := chi3_sum n hpr w hw1 hw3
  have SB : sumK (Cyclo3.cyc3 LibInt.intR) (cb n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := cb_sum n hpr w hw1 hw3
  have cbcb : ∀ a, (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w a) = chi3 n w a := fun a => by
    by_cases ha : a = (ring (6 * n + 5 + 1)).zero
    · rw [ha]; unfold cb; rw [chi3_zero]; rfl
    · calc (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w a) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w a)) (chi3 n w a) := by unfold cb; lib_ring
        _ = chi3 n w a := by rw [chi_cb n hpr w hw1 hw3 a ha]; lib_ring
  have R1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (cb n w a) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w a))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) := by
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by rw [cbcb]), sumK_add, sumK_add, SB, SA, chicb_sum n hpr w hw1 hw3]; lib_ring
  have R2 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w a) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w a))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) := by
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun a => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w a) (cb n w a)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w a))) (fun a => by unfold cb; lib_ring),
      sumK_add, sumK_add, SB, SA, chicb_sum n hpr w hw1 hw3]; lib_ring
  have g0 : g3 n w (ring (6 * n + 5 + 1)).zero = (Cyclo3.cyc3 LibInt.intR).one := by unfold g3 cb; rw [chi3_zero]; show (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).zero) ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (Cyclo3.cyc3 LibInt.intR).zero) = _; lib_ring
  rw [hq, g3_sum n hpr w hw1 hw3, hp, R1, hp, R2, hq, FiniteRecChain.pick (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (g3 n w) (ring (6 * n + 5 + 1)).zero, g0]
  lib_ring

def V : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun x => sumK (Cyclo3.cyc3 LibInt.intR) (fun y => sumK (Cyclo3.cyc3 LibInt.intR) (fun z => sumK (Cyclo3.cyc3 LibInt.intR) (fun u =>
  delta (Cyclo3.cyc3 LibInt.intR) ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (cube n x) (cube n y)) (cube n z)) (cube n u)) (ring (6 * n + 5 + 1)).one) (LL n)) (LL n)) (LL n)) (LL n)

theorem V_sum (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : V n = sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (U n w (om1 n a))) (LL n) := by
  unfold V
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun x => sumK (Cyclo3.cyc3 LibInt.intR) (fun y => sumK (Cyclo3.cyc3 LibInt.intR) (fun z => g3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (cube n x) (cube n y)) (cube n z))))) (LL n)) (LL n))
    (fun x => sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun y => sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun z => stepz n hpr w hw1 hw3 _) _) _)]
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun x => sumK (Cyclo3.cyc3 LibInt.intR) (fun y => T n w ((ring (6 * n + 5 + 1)).add (om1 n (cube n x)) ((ring (6 * n + 5 + 1)).neg (cube n y)))) (LL n)) (fun x => sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun y => by
    rw [FermatCubic.step2 n hpr w hw1 hw3 (fun b => g3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (cube n x) (cube n y)) b))))]
    unfold T
    exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun b => by rw [show (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (cube n x) (cube n y)) b)) = (ring (6 * n + 5 + 1)).add ((ring (6 * n + 5 + 1)).add (om1 n (cube n x)) ((ring (6 * n + 5 + 1)).neg (cube n y))) ((ring (6 * n + 5 + 1)).neg b) by unfold om1; l  -- (+16 chars; full line in the Zenodo source)
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun x => U n w (om1 n (cube n x))) (fun x => by
    rw [FermatCubic.step2 n hpr w hw1 hw3 (fun b => T n w ((ring (6 * n + 5 + 1)).add (om1 n (cube n x)) ((ring (6 * n + 5 + 1)).neg b)))]; rfl)]
  exact FermatCubic.step2 n hpr w hw1 hw3 (fun a => U n w (om1 n a))

/-- **The cubic threefold**: #{x³ + y³ + z³ + u³ = 1} = p³ + 5p Tr J − 6p. -/
theorem threefold (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    V n = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (Int.ofNat (6 * n + 5 + 1 + 1), 0) (Int.ofNat (6 * n + 5 + 1 + 1), 0)) (Int.ofNat (6 * n + 5 + 1 + 1), 0))
      ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (5, 0) (Int.ofNat (6 * n + 5 + 1 + 1), 0)) ((Cyclo3.cyc3 LibInt.intR).add (J n w) (cconj (J n w))))) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (6, 0) (Int.ofNat (6 * n + 5 + 1 + 1), 0))) := by
  rw [V_sum n hpr w hw1 hw3]
  have split : ∀ a, U n w (om1 n a) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1) * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (J n w) (cconj (J n w)))))
      ((Cyclo3.cyc3 LibInt.intR).mul (3 * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w (om1 n a)) (cb n w (om1 n a)))))
      ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one) ((Cyclo3.cyc3 LibInt.intR).add (U n w (ring (6 * n + 5 + 1)).zero) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1) * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (J n w) (cconj (J n w)))))))) := fun a =>   -- (+2 chars; full line in the Zenodo source)
    by_cases ha : a = (ring (6 * n + 5 + 1)).one
    · rw [ha, show om1 n (ring (6 * n + 5 + 1)).one = (ring (6 * n + 5 + 1)).zero by unfold om1; lib_ring]
      unfold delta cb; rw [ite_pos' _ _ rfl, chi3_zero]
      show _ = (Cyclo3.cyc3 LibInt.intR).add _ ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one _)
      rw [show (Cyclo3.cyc3 LibInt.intR).mul (3 * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).add (0, 0) ((Cyclo3.cyc3 LibInt.intR).mul (0, 0) (0, 0))) = (Cyclo3.cyc3 LibInt.intR).zero from
        Prod.ext (show 3 * Int.ofNat (6 * n + 5 + 1 + 1) * (0 + (0 * 0 + -(0 * 0))) + -(0 * (0 + (0 * 0 + 0 * 0 + -(0 * 0)))) = 0 by lib_int)
          (show 3 * Int.ofNat (6 * n + 5 + 1 + 1) * (0 + (0 * 0 + 0 * 0 + -(0 * 0))) + 0 * (0 + (0 * 0 + -(0 * 0))) + -(0 * (0 + (0 * 0 + 0 * 0 + -(0 * 0)))) = 0 by lib_int)]
      lib_ring
    · have h1 : om1 n a ≠ (ring (6 * n + 5 + 1)).zero := fun e => ha (by calc a = (ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).one ((ring (6 * n + 5 + 1)).neg (om1 n a)) := by unfold om1; lib_ring
                                                            _ = (ring (6 * n + 5 + 1)).one := by rw [e]; lib_ring)
      rw [surface_c n hpr w hw1 hw3 _ h1]
      unfold delta; rw [ite_neg' _ _ ha]; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by rw [split a])]
  have hexp : ∀ a, (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1) * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (J n w) (cconj (J n w)))))
      ((Cyclo3.cyc3 LibInt.intR).mul (3 * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w (om1 n a)) (cb n w (om1 n a)))))
      ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one) ((Cyclo3.cyc3 LibInt.intR).add (U n w (ring (6 * n + 5 + 1)).zero) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1) * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (J n w) (cconj (J n w)))))))))
      = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1) * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (J n w) (cconj (J n w))))))
        ((Cyclo3.cyc3 LibInt.intR).mul (3 * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (chi3 n w (om1 n a)))))
        ((Cyclo3.cyc3 LibInt.intR).mul (3 * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (cb n w (om1 n a)))))
        ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a (ring (6 * n + 5 + 1)).one) (g3 n w a)) ((Cyclo3.cyc3 LibInt.intR).add (U n w (ring (6 * n + 5 + 1)).zero) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1) * Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).a  -- (+56 chars; full line in the Zenodo source)
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ hexp, sumK_add, sumK_add, sumK_add]
  have SA : sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := chi3_sum n hpr w hw1 hw3
  have SB : sumK (Cyclo3.cyc3 LibInt.intR) (cb n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := cb_sum n hpr w hw1 hw3
  have R2 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (chi3 n w (om1 n a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).add (J n w) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) := by
    unfold g3
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun a => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w (om1 n a)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w (om1 n a)))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w (om1 n a)))) (fun a => by lib_ring),
      sumK_add, sumK_add, refl1 n (chi3 n w), SA, show sumK (Cyclo3.cyc3 LibInt.intR) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (chi3 n w (om1 n t))) (LL n) = J n w from rfl, SBA n hpr w hw1 hw3]; lib_ring
  have R1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (cb n w (om1 n a))) (LL n) = (Cyclo3.cyc3 LibInt.intR).add (cconj (J n w)) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) := by
    unfold g3
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun a => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (cb n w (om1 n a)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w (om1 n a)))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w (om1 n a)))) (fun a => by lib_ring),
      sumK_add, sumK_add, refl1 n (cb n w), SB, SAB n hpr w hw1 hw3, SBB n hpr w hw1 hw3]; lib_ring
  have g1 : g3 n w (ring (6 * n + 5 + 1)).one = (3, 0) := by
    unfold g3 cb; rw [val_of n hpr w hw1 hw3 (ring (6 * n + 5 + 1)).one (PowerSum.one_ne_zero' _) 0 (Nat.zero_lt_succ 2) (CubicChar.pw_one'' (ring (6 * n + 5 + 1)) _)]; rfl
  rw [hq, g3_sum n hpr w hw1 hw3, hp, R2, hp, R1, hq, FiniteRecChain.pick (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (g3 n w) (ring (6 * n + 5 + 1)).one, g1, surface_0 n hpr w hw1 hw3]
  generalize J n w = Jv
  generalize cconj Jv = Kv
  generalize Int.ofNat (6 * n + 5 + 1 + 1) = P
  have mu : (Cyclo3.cyc3 LibInt.intR).mul (P, 0) (P, 0) = (P * P, 0) := Prod.ext (show P * P + -(0 * 0) = P * P by lib_int) (show P * 0 + 0 * P + -(0 * 0) = 0 by lib_int)
  have ad3 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (P, 0) (P, 0)) (P, 0) = (3 * P, 0) := Prod.ext (show P + P + P = 3 * P by lib_int) (show (0 : Int) + 0 + 0 = 0 from rfl)
  have t3 : ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one) (Cyclo3.cyc3 LibInt.intR).one : Int × Int) = (3, 0) := rfl
  have f5 : ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one) (Cyclo3.cyc3 LibInt.intR).one) (Cyclo3.cyc3 LibInt.intR).one) (Cyclo3.cyc3 LibInt.intR).one : Int × Int) = (5, 0) := rfl
  have f6 : ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one) (Cyclo3.cyc3 LibInt.intR).one) (Cyclo3.cyc3 LibInt.intR).one) (Cyclo3.cyc3 LibInt.intR).one) (Cyclo3.cyc3 LibInt.intR).one : Int × Int) = (6, 0) := rfl
  rw [← mu, ← ad3, ← t3, ← f5, ← f6]
  lib_ring

end CubicThreefold
#print axioms CubicThreefold.hp
#print axioms CubicThreefold.hq
#print axioms CubicThreefold.T_split_c
#print axioms CubicThreefold.surface_c
#print axioms CubicThreefold.chi_neg
#print axioms CubicThreefold.chicb_sum
#print axioms CubicThreefold.surface_0
#print axioms CubicThreefold.V_sum
#print axioms CubicThreefold.threefold

#print axioms: 9 theorems, no axioms.

78.79 Diagonal cubic hypersurfaces in every dimension

One step \(N_{k+1}(c)=\sum _aN(a)N_k(c-a)\) preserves the shape \(A+B\bar \chi (c)+B'\chi (c)\).

\[ A_{k+1}=(p-1)A_k-B_k-B'_k+C_k,\ \ B_{k+1}=B'_kJ+C_k-A_k,\ \ B'_{k+1}=B_k\bar J+C_k-A_k,\ \ C_{k+1}=(p-1)(A_k+B_k+B'_k)+C_k. \]

lang/CubicDiagonal.lean

/-
  lang/CubicDiagonal — T43, cycle 253: the diagonal cubic hypersurfaces x₁³ + … + x_k³ = c in every dimension.

  N_0(c) = [c = 0], N_{k+1}(c) = Σ_a N(a) N_k(c − a) (N(a) = #{x³ = a} = 1 + χ(a) + χ̄(a)), so N_k(c) = #{x₁³ + … + x_k³ = c}.
  For every k:  N_k(c) = A_k + B_k χ̄(c) + B'_k χ(c)  (c ≠ 0),  N_k(0) = C_k, with
      A_{k+1} = (p − 1)A_k − B_k − B'_k + C_k,   B_{k+1} = B'_k J + C_k − A_k,   B'_{k+1} = B_k J̄ + C_k − A_k,
      C_{k+1} = (p − 1)(A_k + B_k + B'_k) + C_k,     A_0 = B_0 = B'_0 = 0, C_0 = 1.
      diagonal      the closed recursion for all k (Weil's diagonal hypersurfaces of degree 3, all dimensions)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CubicThreefold

namespace CubicDiagonal

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm FermatCubic CubicSextic CubicDup CubicCyclo CubicSix CubicTwistCount CubicSurface CubicThreefold
open LibRing (castZ castN)

variable (n : Nat)

def Nk (w : Zp (6 * n + 5 + 1)) : Nat → Zp (6 * n + 5 + 1) → Int × Int
  | 0, c => delta (Cyclo3.cyc3 LibInt.intR) c (ring (6 * n + 5 + 1)).zero
  | k + 1, c => sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (Nk w k ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n)

theorem R1c (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cconj (J n w))) := by
  have scale : ∀ F : Zp (6 * n + 5 + 1) → Int × Int, sumK (Cyclo3.cyc3 LibInt.intR) F (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun t => F ((ring (6 * n + 5 + 1)).mul c t)) (LL n) := fun F =>
    (CharSumGen.sum_scale (Cyclo3.cyc3 LibInt.intR) (6 * n + 4) hpr F c hc).symm
  have hct : ∀ t, (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul c t)) = (ring (6 * n + 5 + 1)).mul c (om1 n t) := fun t => by unfold om1; lib_ring
  have hcc := chi_cb n hpr w hw1 hw3 c hc
  have SB : sumK (Cyclo3.cyc3 LibInt.intR) (cb n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := cb_sum n hpr w hw1 hw3
  rw [scale]
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun t => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w (om1 n t))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cb n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (cb n w (om1 n t)))))
      ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w t) (cb n w (om1 n t))))) (fun t => by
    rw [hct]; unfold g3; rw [chi3_mul n hpr w hw1 hw3, cb_mul n hpr w hw1 hw3, cb_mul n hpr w hw1 hw3]; lib_ring)]
  rw [sumK_add, sumK_add, hp, hp, hp, refl1 n (cb n w), SB, hcc, SAB n hpr w hw1 hw3, SBB n hpr w hw1 hw3,
    show (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w c) = chi3 n w c by
      calc (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w c) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cb n w c)) (chi3 n w c) := by unfold cb; lib_ring
        _ = chi3 n w c := by rw [hcc]; lib_ring]
  lib_ring

theorem R2c (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (J n w)) := by
  have scale : ∀ F : Zp (6 * n + 5 + 1) → Int × Int, sumK (Cyclo3.cyc3 LibInt.intR) F (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun t => F ((ring (6 * n + 5 + 1)).mul c t)) (LL n) := fun F =>
    (CharSumGen.sum_scale (Cyclo3.cyc3 LibInt.intR) (6 * n + 4) hpr F c hc).symm
  have hct : ∀ t, (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul c t)) = (ring (6 * n + 5 + 1)).mul c (om1 n t) := fun t => by unfold om1; lib_ring
  have hcc := chi_cb n hpr w hw1 hw3 c hc
  have SA : sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := chi3_sum n hpr w hw1 hw3
  rw [scale]
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun t => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w (om1 n t))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (chi3 n w (om1 n t)))))
      ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (chi3 n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w t) (chi3 n w (om1 n t))))) (fun t => by
    rw [hct]; unfold g3; rw [chi3_mul n hpr w hw1 hw3, chi3_mul n hpr w hw1 hw3, cb_mul n hpr w hw1 hw3]; lib_ring)]
  rw [sumK_add, sumK_add, hp, hp, hp, refl1 n (chi3 n w), SA, show sumK (Cyclo3.cyc3 LibInt.intR) (fun t => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (chi3 n w (om1 n t))) (LL n) = J n w from rfl,
    SBA n hpr w hw1 hw3, show (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w c) = cb n w c from rfl, show (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (chi3 n w c) = (Cyclo3.cyc3 LibInt.intR).one by rw [(Cyclo3.cyc3 LibInt.intR).mul_comm]; exact hcc]
  lib_ring

theorem R1z (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (cb n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg a)))) (LL n  -- (+136 chars; full line in the Zenodo source)
  have SA : sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := chi3_sum n hpr w hw1 hw3
  have SB : sumK (Cyclo3.cyc3 LibInt.intR) (cb n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := cb_sum n hpr w hw1 hw3
  have cbcb : ∀ a, (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w a) = chi3 n w a := fun a => by
    by_cases ha : a = (ring (6 * n + 5 + 1)).zero
    · rw [ha]; unfold cb; rw [chi3_zero]; rfl
    · calc (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w a) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w a)) (chi3 n w a) := by unfold cb; lib_ring
        _ = chi3 n w a := by rw [chi_cb n hpr w hw1 hw3 a ha]; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun a => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (cb n w a) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w a))) (chi3 n w a)) (fun a => by
    rw [(chi_neg n hpr w hw1 hw3 a).2]; unfold g3
    rw [show (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (chi3 n w a)) (cb n w a)) (cb n w a) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (cb n w a) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w a))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w a)) by lib_ring, cbcb a]), sumK_add, sumK_add,   -- (+44 chars; full line in the Zenodo source)

theorem R2z (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg a)))) (LL  -- (+138 chars; full line in the Zenodo source)
  have SA : sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := chi3_sum n hpr w hw1 hw3
  have SB : sumK (Cyclo3.cyc3 LibInt.intR) (cb n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := cb_sum n hpr w hw1 hw3
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun a => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w a) (cb n w a)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w a))) (fun a => by
    rw [(chi_neg n hpr w hw1 hw3 a).1]; unfold g3 cb; lib_ring), sumK_add, sumK_add, SB, SA, chicb_sum n hpr w hw1 hw3]; lib_ring

/-- **One step of the recursion.** -/
theorem step (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (k : Nat) (A B B' Cc : Int × Int)
    (hne : ∀ c, c ≠ (ring (6 * n + 5 + 1)).zero → Nk n w k c = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add A ((Cyclo3.cyc3 LibInt.intR).mul B (cb n w c))) ((Cyclo3.cyc3 LibInt.intR).mul B' (chi3 n w c)))
    (h0 : Nk n w k (ring (6 * n + 5 + 1)).zero = Cc) :
    (∀ c, c ≠ (ring (6 * n + 5 + 1)).zero → Nk n w (k + 1) c = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add
        ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) A) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add B B'))) Cc)
        ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul B' (J n w)) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A))) (cb n w c)))
        ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul B (cconj (J n w))) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A))) (chi3 n w c))) ∧
    Nk n w (k + 1) (ring (6 * n + 5 + 1)).zero = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add A B) B')) Cc := by
  have split : ∀ c a, Nk n w k ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add A ((Cyclo3.cyc3 LibInt.intR).mul B (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a))))) ((Cyclo3.cyc3 LibInt.intR).mul B' (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a))  -- (+3 chars; full line in the Zenodo source)
      ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a c) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A))) := fun c a => by
    by_cases ha : a = c
    · rw [ha, show (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg c) = (ring (6 * n + 5 + 1)).zero by lib_ring, h0]; unfold cb delta; rw [ite_pos' _ _ rfl, chi3_zero]
      show Cc = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add A ((Cyclo3.cyc3 LibInt.intR).mul B ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (Cyclo3.cyc3 LibInt.intR).zero))) ((Cyclo3.cyc3 LibInt.intR).mul B' (Cyclo3.cyc3 LibInt.intR).zero)) ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).add Cc  -- (+46 chars; full line in the Zenodo source)
    · have h1 : (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a) ≠ (ring (6 * n + 5 + 1)).zero := fun e => ha (by calc a = (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a))) := by lib_ring
                                                                    _ = c := by rw [e]; lib_ring)
      rw [hne _ h1]; unfold delta; rw [ite_neg' _ _ ha]; lib_ring
  have gen : ∀ c, Nk n w (k + 1) c = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (sumK (Cyclo3.cyc3 LibInt.intR) (g3 n w) (LL n)) A)
      ((Cyclo3.cyc3 LibInt.intR).mul B (sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n))))
      ((Cyclo3.cyc3 LibInt.intR).mul B' (sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n))))
      ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w c) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A))) := fun c => by
    show sumK (Cyclo3.cyc3 LibInt.intR) (fun a => (Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (Nk n w k ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))) (LL n) = _
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun a => by rw [split c a])]
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun a => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) A) ((Cyclo3.cyc3 LibInt.intR).mul B ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a))))))
        ((Cyclo3.cyc3 LibInt.intR).mul B' ((Cyclo3.cyc3 LibInt.intR).mul (g3 n w a) (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg a)))))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) a c) (g3 n w a)) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A)))) (fun a => by lib_ring)]
    rw [sumK_add, sumK_add, sumK_add, hq, hp, hp, hq, FiniteRecChain.pick (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (g3 n w) c]
  constructor
  · intro c hc
    rw [gen c, g3_sum n hpr w hw1 hw3, R1c n hpr w hw1 hw3 c hc, R2c n hpr w hw1 hw3 c hc]
    unfold g3; lib_ring
  · rw [gen (ring (6 * n + 5 + 1)).zero, g3_sum n hpr w hw1 hw3, R1z n hpr w hw1 hw3, R2z n hpr w hw1 hw3]
    have g0 : g3 n w (ring (6 * n + 5 + 1)).zero = (Cyclo3.cyc3 LibInt.intR).one := by unfold g3 cb; rw [chi3_zero]; show (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).zero) ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (Cyclo3.cyc3 LibInt.intR).zero) = _; lib_ring
    rw [g0]; lib_ring

/-- (A_k, B_k, B'_k, C_k). -/
def Sq (w : Zp (6 * n + 5 + 1)) : Nat → (Int × Int) × (Int × Int) × (Int × Int) × (Int × Int)
  | 0 => ((Cyclo3.cyc3 LibInt.intR).zero, (Cyclo3.cyc3 LibInt.intR).zero, (Cyclo3.cyc3 LibInt.intR).zero, (Cyclo3.cyc3 LibInt.intR).one)
  | k + 1 =>
    let A := (Sq w k).1
    let B := (Sq w k).2.1
    let B' := (Sq w k).2.2.1
    let Cc := (Sq w k).2.2.2
    ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) A) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add B B'))) Cc,
     (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul B' (J n w)) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A)),
     (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul B (cconj (J n w))) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A)),
     (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add A B) B')) Cc)

/-- **Diagonal cubic hypersurfaces in every dimension**: #{x₁³ + … + x_k³ = c} = A_k + B_k χ̄(c) + B'_k χ(c) for c ≠ 0, = C_k for c = 0. -/
theorem diagonal (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : ∀ k : Nat,
    (∀ c, c ≠ (ring (6 * n + 5 + 1)).zero → Nk n w k c = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Sq n w k).1 ((Cyclo3.cyc3 LibInt.intR).mul (Sq n w k).2.1 (cb n w c))) ((Cyclo3.cyc3 LibInt.intR).mul (Sq n w k).2.2.1 (chi3 n w c))) ∧
    Nk n w k (ring (6 * n + 5 + 1)).zero = (Sq n w k).2.2.2
  | 0 => by
    constructor
    · intro c hc
      show delta (Cyclo3.cyc3 LibInt.intR) c (ring (6 * n + 5 + 1)).zero = _
      unfold delta; rw [ite_neg' _ _ hc]; show (Cyclo3.cyc3 LibInt.intR).zero = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).zero ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero _)) ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero _); lib_ring
    · show delta (Cyclo3.cyc3 LibInt.intR) (ring (6 * n + 5 + 1)).zero (ring (6 * n + 5 + 1)).zero = (Cyclo3.cyc3 LibInt.intR).one
      unfold delta; exact ite_pos' _ _ rfl
  | k + 1 => by
    have ih := diagonal hpr w hw1 hw3 k
    exact step n hpr w hw1 hw3 k _ _ _ _ ih.1 ih.2

/-! ## diagonal cubic forms with coefficients -/

theorem scale' (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) (F : Zp (6 * n + 5 + 1) → Int × Int) : sumK (Cyclo3.cyc3 LibInt.intR) F (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun t => F ((ring (6 * n + 5 + 1)).mul c t)) (LL n) :=
  (CharSumGen.sum_scale (Cyclo3.cyc3 LibInt.intR) (6 * n + 4) hpr F c hc).symm
theorem hct' (c t : Zp (6 * n + 5 + 1)) : (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul c t)) = (ring (6 * n + 5 + 1)).mul c (om1 n t) := by unfold om1; lib_ring

def Nf (w : Zp (6 * n + 5 + 1)) : List (Zp (6 * n + 5 + 1)) → Zp (6 * n + 5 + 1) → Int × Int
  | [], c => delta (Cyclo3.cyc3 LibInt.intR) c (ring (6 * n + 5 + 1)).zero
  | a :: as, c => sumK (Cyclo3.cyc3 LibInt.intR) (fun x => Nf w as ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul a (cube n x))))) (LL n)

/-- the weight #{a x³ = u} = 1 + χ̄(a)χ(u) + χ(a)χ̄(u). -/
def wt (w a u : Zp (6 * n + 5 + 1)) : Int × Int := (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w u))) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w u))

theorem conv_a (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (a : Zp (6 * n + 5 + 1)) (ha : a ≠ (ring (6 * n + 5 + 1)).zero) (F : Zp (6 * n + 5 + 1) → Int × Int) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun x => F ((ring (6 * n + 5 + 1)).mul a (cube n x))) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (wt n w a u) (F u)) (LL n) := by
  rw [FermatCubic.step2 n hpr w hw1 hw3 (fun v => F ((ring (6 * n + 5 + 1)).mul a v))]
  have hi := CharSumGen.iv_mul (6 * n + 4) hpr a ha
  rw [← FiniteRecChain.reindex (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) ((ring (6 * n + 5 + 1)).mul a) ((ring (6 * n + 5 + 1)).mul (CharSumGen.iv (6 * n + 4) a))
    (fun x => by rw [← (ring (6 * n + 5 + 1)).mul_assoc, hi]; lib_ring) (fun y => by rw [← (ring (6 * n + 5 + 1)).mul_assoc, (ring (6 * n + 5 + 1)).mul_comm a, hi]; lib_ring) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (wt n w a u) (F u))]
  exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun v => by
    show (Cyclo3.cyc3 LibInt.intR).mul (g3 n w v) (F ((ring (6 * n + 5 + 1)).mul a v)) = (Cyclo3.cyc3 LibInt.intR).mul (wt n w a ((ring (6 * n + 5 + 1)).mul a v)) (F ((ring (6 * n + 5 + 1)).mul a v))
    unfold wt g3
    rw [chi3_mul n hpr w hw1 hw3, cb_mul n hpr w hw1 hw3]
    have h1 := chi_cb n hpr w hw1 hw3 a ha
    rw [show (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w v)) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w a)) (chi3 n w v) by lib_ring,
      show (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w v)) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w a)) (cb n w v) by lib_ring, h1]; lib_ring) _

theorem P1 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) : sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w u) (cb n w ((ring (6 * n + 5 + 1)).add c   -- (+108 chars; full line in the Zenodo source)
  rw [scale' n hpr c hc]; rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun t => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cb n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (cb n w (om1 n t)))) (fun t => by
    rw [hct' n, chi3_mul n hpr w hw1 hw3, cb_mul n hpr w hw1 hw3]; lib_ring), hp, chi_cb n hpr w hw1 hw3 c hc, SAB n hpr w hw1 hw3]; lib_ring
theorem P2 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) : sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (cb n w u) (cb n w ((ring (6 * n + 5 + 1)).add c ((  -- (+105 chars; full line in the Zenodo source)
  rw [scale' n hpr c hc]; rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun t => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w t) (cb n w (om1 n t)))) (fun t => by
    rw [hct' n, cb_mul n hpr w hw1 hw3, cb_mul n hpr w hw1 hw3]; lib_ring), hp, SBB n hpr w hw1 hw3,
    show (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w c) = chi3 n w c by
      calc (Cyclo3.cyc3 LibInt.intR).mul (cb n w c) (cb n w c) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cb n w c)) (chi3 n w c) := by unfold cb; lib_ring
        _ = chi3 n w c := by rw [chi_cb n hpr w hw1 hw3 c hc]; lib_ring]
theorem P3 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) : sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w u) (chi3 n w ((ring (6 * n + 5 + 1)).add   -- (+99 chars; full line in the Zenodo source)
  rw [scale' n hpr c hc]; rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun t => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w t) (chi3 n w (om1 n t)))) (fun t => by
    rw [hct' n, chi3_mul n hpr w hw1 hw3, chi3_mul n hpr w hw1 hw3]; lib_ring), hp]; rfl
theorem P4 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) : sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (cb n w u) (chi3 n w ((ring (6 * n + 5 + 1)).add c   -- (+108 chars; full line in the Zenodo source)
  rw [scale' n hpr c hc]; rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun t => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (cb n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w t) (chi3 n w (om1 n t)))) (fun t => by
    rw [hct' n, chi3_mul n hpr w hw1 hw3, cb_mul n hpr w hw1 hw3]; lib_ring), hp, chi_cb n hpr w hw1 hw3 c hc, SBA n hpr w hw1 hw3]; lib_ring

theorem shift0 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c : Zp (6 * n + 5 + 1)) : sumK (Cyclo3.cyc3 LibInt.intR) (fun u => chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u))) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero ∧ su  -- (+154 chars; full line in the Zenodo source)
  have r : ∀ G : Zp (6 * n + 5 + 1) → Int × Int, sumK (Cyclo3.cyc3 LibInt.intR) (fun u => G ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u))) (LL n) = sumK (Cyclo3.cyc3 LibInt.intR) G (LL n) := fun G =>
    FiniteRecChain.reindex (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (fun u => (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u)) (fun u => (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u)) (fun x => by lib_ring) (fun y => by lib_ring) G
  exact ⟨(r (chi3 n w)).trans (chi3_sum n hpr w hw1 hw3), (r (cb n w)).trans (cb_sum n hpr w hw1 hw3)⟩

theorem wt_sum (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (a : Zp (6 * n + 5 + 1)) : sumK (Cyclo3.cyc3 LibInt.intR) (wt n w a) (LL n) = (Int.ofNat (6 * n + 5 + 1 + 1), 0) := by
  unfold wt
  have T1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun _ : Zp (6 * n + 5 + 1) => (Cyclo3.cyc3 LibInt.intR).one) (LL n) = (Int.ofNat (6 * n + 5 + 1 + 1), 0) := JacobiSum.sum_one n _
  rw [sumK_add, sumK_add, T1, hp, hp, show sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero from chi3_sum n hpr w hw1 hw3, cb_sum n hpr w hw1 hw3]
  lib_ring

theorem cbcb (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (u : Zp (6 * n + 5 + 1)) : (Cyclo3.cyc3 LibInt.intR).mul (cb n w u) (cb n w u) = chi3 n w u := by
  by_cases hu : u = (ring (6 * n + 5 + 1)).zero
  · rw [hu]; unfold cb; rw [chi3_zero]; rfl
  · calc (Cyclo3.cyc3 LibInt.intR).mul (cb n w u) (cb n w u) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w u) (cb n w u)) (chi3 n w u) := by unfold cb; lib_ring
      _ = chi3 n w u := by rw [chi_cb n hpr w hw1 hw3 u hu]; lib_ring

theorem Z0 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (a : Zp (6 * n + 5 + 1)) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (wt n w a u) (cb n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg u)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul (cb n w a) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) ∧
    sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (wt n w a u) (chi3 n w ((ring (6 * n + 5 + 1)).add (ring (6 * n + 5 + 1)).zero ((ring (6 * n + 5 + 1)).neg u)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) := by
  have SA : sumK (Cyclo3.cyc3 LibInt.intR) (chi3 n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := chi3_sum n hpr w hw1 hw3
  have SB : sumK (Cyclo3.cyc3 LibInt.intR) (cb n w) (LL n) = (Cyclo3.cyc3 LibInt.intR).zero := cb_sum n hpr w hw1 hw3
  have CC := chicb_sum n hpr w hw1 hw3
  constructor
  · rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun u => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (cb n w u) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w u) (cb n w u)))) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w u))) (fun u => by
      rw [(chi_neg n hpr w hw1 hw3 u).2]; unfold wt; rw [show (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (chi3 n w u))) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (cb n w u))) (cb n w u)
        = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (cb n w u) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w u) (cb n w u)))) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w u) (cb n w u))) by lib_ring, cbcb n hpr w hw1 hw3])]
    rw [sumK_add, sumK_add, hp, hp, SB, CC, SA]; lib_ring
  · rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun u => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w u) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w u))) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w u) (cb n w u)))) (fun u => by
      rw [(chi_neg n hpr w hw1 hw3 u).1]; unfold wt cb; lib_ring)]
    rw [sumK_add, sumK_add, hp, hp, SB, CC, SA]; lib_ring

theorem step_f (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (a : Zp (6 * n + 5 + 1)) (ha : a ≠ (ring (6 * n + 5 + 1)).zero) (as : List (Zp (6 * n + 5 + 1))) (A B B' Cc : Int × Int)
    (hne : ∀ c, c ≠ (ring (6 * n + 5 + 1)).zero → Nf n w as c = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add A ((Cyclo3.cyc3 LibInt.intR).mul B (cb n w c))) ((Cyclo3.cyc3 LibInt.intR).mul B' (chi3 n w c)))
    (h0 : Nf n w as (ring (6 * n + 5 + 1)).zero = Cc) :
    (∀ c, c ≠ (ring (6 * n + 5 + 1)).zero → Nf n w (a :: as) c = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add
        ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Int.ofNat (6 * n + 5 + 1 + 1), 0) A) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) B) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) B')))) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A)))
        ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul B' (cb n w a)) (J n w)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A)))) (cb n w c)))
        ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul B (chi3 n w a)) (cconj (J n w))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A)))) (chi3 n w c))) ∧
    Nf n w (a :: as) (ring (6 * n + 5 + 1)).zero = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Int.ofNat (6 * n + 5 + 1 + 1), 0) A)
      ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one))) B))
      ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one))) B')) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A)) := by
  have split : ∀ c u, Nf n w as ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u)) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add A ((Cyclo3.cyc3 LibInt.intR).mul B (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u))))) ((Cyclo3.cyc3 LibInt.intR).mul B' (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u)  -- (+4 chars; full line in the Zenodo source)
      ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) u c) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A))) := fun c u => by
    by_cases hu : u = c
    · rw [hu, show (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg c) = (ring (6 * n + 5 + 1)).zero by lib_ring, h0]; unfold cb delta; rw [ite_pos' _ _ rfl, chi3_zero]
      show Cc = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add A ((Cyclo3.cyc3 LibInt.intR).mul B ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (Cyclo3.cyc3 LibInt.intR).zero))) ((Cyclo3.cyc3 LibInt.intR).mul B' (Cyclo3.cyc3 LibInt.intR).zero)) ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).add Cc  -- (+46 chars; full line in the Zenodo source)
    · have h1 : (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u) ≠ (ring (6 * n + 5 + 1)).zero := fun e => hu (by calc u = (ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u))) := by lib_ring
                                                                    _ = c := by rw [e]; lib_ring)
      rw [hne _ h1]; unfold delta; rw [ite_neg' _ _ hu]; lib_ring
  have gen : ∀ c, Nf n w (a :: as) c = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (sumK (Cyclo3.cyc3 LibInt.intR) (wt n w a) (LL n)) A)
      ((Cyclo3.cyc3 LibInt.intR).mul B (sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (wt n w a u) (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u)))) (LL n))))
      ((Cyclo3.cyc3 LibInt.intR).mul B' (sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (wt n w a u) (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u)))) (LL n))))
      ((Cyclo3.cyc3 LibInt.intR).mul (wt n w a c) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A))) := fun c => by
    show sumK (Cyclo3.cyc3 LibInt.intR) (fun x => Nf n w as ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg ((ring (6 * n + 5 + 1)).mul a (cube n x))))) (LL n) = _
    rw [conv_a n hpr w hw1 hw3 a ha (fun u => Nf n w as ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u)))]
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun u => by rw [split c u])]
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun u => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (wt n w a u) A) ((Cyclo3.cyc3 LibInt.intR).mul B ((Cyclo3.cyc3 LibInt.intR).mul (wt n w a u) (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u))))))
        ((Cyclo3.cyc3 LibInt.intR).mul B' ((Cyclo3.cyc3 LibInt.intR).mul (wt n w a u) (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u)))))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) u c) (wt n w a u)) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A)))) (fun u => by lib_ring)]
    rw [sumK_add, sumK_add, sumK_add, hq, hp, hp, hq, FiniteRecChain.pick (Cyclo3.cyc3 LibInt.intR) (LL n) (CMInert.complete_allZ (6 * n + 5 + 1) (Cyclo3.cyc3 LibInt.intR)) (wt n w a) c]
  constructor
  · intro c hc
    have e1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (wt n w a u) (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (cb n w a)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) (chi3 n w c)) (cconj (J n w))) := by
      unfold wt
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun u => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w u) (cb n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u)))))) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) ((Cyclo  -- (+132 chars; full line in the Zenodo source)
        sumK_add, sumK_add, hp, hp, (shift0 n hpr w hw1 hw3 c).2, P1 n hpr w hw1 hw3 c hc, P2 n hpr w hw1 hw3 c hc]; lib_ring
    have e2 : sumK (Cyclo3.cyc3 LibInt.intR) (fun u => (Cyclo3.cyc3 LibInt.intR).mul (wt n w a u) (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u)))) (LL n) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) (cb n w c)) (J n w)) ((Cyclo3.cyc3 LibInt.intR).neg (chi3 n w a)) := by
      unfold wt
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun u => (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w u) (chi3 n w ((ring (6 * n + 5 + 1)).add c ((ring (6 * n + 5 + 1)).neg u)))))) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) ((C  -- (+138 chars; full line in the Zenodo source)
        sumK_add, sumK_add, hp, hp, (shift0 n hpr w hw1 hw3 c).1, P3 n hpr w hw1 hw3 c hc, P4 n hpr w hw1 hw3 c hc]; lib_ring
    rw [gen c, wt_sum n hpr w hw1 hw3, e1, e2]; unfold wt; lib_ring
  · rw [gen (ring (6 * n + 5 + 1)).zero, wt_sum n hpr w hw1 hw3, (Z0 n hpr w hw1 hw3 a).1, (Z0 n hpr w hw1 hw3 a).2]
    have w0 : wt n w a (ring (6 * n + 5 + 1)).zero = (Cyclo3.cyc3 LibInt.intR).one := by unfold wt cb; rw [chi3_zero]; show (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one ((Cyclo3.cyc3 LibInt.intR).mul _ (Cyclo3.cyc3 LibInt.intR).zero)) ((Cyclo3.cyc3 LibInt.intR).mul _ ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (Cyclo3.cyc3 LibInt.intR  -- (+22 chars; full line in the Zenodo source)
    rw [w0]; lib_ring

/-- (A, B, B', C) along a coefficient list. -/
def Sf (w : Zp (6 * n + 5 + 1)) : List (Zp (6 * n + 5 + 1)) → (Int × Int) × (Int × Int) × (Int × Int) × (Int × Int)
  | [] => ((Cyclo3.cyc3 LibInt.intR).zero, (Cyclo3.cyc3 LibInt.intR).zero, (Cyclo3.cyc3 LibInt.intR).zero, (Cyclo3.cyc3 LibInt.intR).one)
  | a :: as =>
    let A := (Sf w as).1
    let B := (Sf w as).2.1
    let B' := (Sf w as).2.2.1
    let Cc := (Sf w as).2.2.2
    ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Int.ofNat (6 * n + 5 + 1 + 1), 0) A) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) B) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) B')))) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A)),
     (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul B' (cb n w a)) (J n w)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A))),
     (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul B (chi3 n w a)) (cconj (J n w))) ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A))),
     (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Int.ofNat (6 * n + 5 + 1 + 1), 0) A) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cb n w a) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one))) B))
       ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w a) ((Cyclo3.cyc3 LibInt.intR).add (Int.ofNat (6 * n + 5 + 1 + 1), 0) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one))) B')) ((Cyclo3.cyc3 LibInt.intR).add Cc ((Cyclo3.cyc3 LibInt.intR).neg A)))

/-- **Every diagonal cubic form Σ a_i x_i³ over 𝔽_p** (a_i ≠ 0). -/
theorem diag_form (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : ∀ as : List (Zp (6 * n + 5 + 1)), (∀ a, memL a as → a ≠ (ring (6 * n + 5 + 1)).zero) →
    (∀ c, c ≠ (ring (6 * n + 5 + 1)).zero → Nf n w as c = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Sf n w as).1 ((Cyclo3.cyc3 LibInt.intR).mul (Sf n w as).2.1 (cb n w c))) ((Cyclo3.cyc3 LibInt.intR).mul (Sf n w as).2.2.1 (chi3 n w c))) ∧
    Nf n w as (ring (6 * n + 5 + 1)).zero = (Sf n w as).2.2.2
  | [], _ => by
    constructor
    · intro c hc
      show delta (Cyclo3.cyc3 LibInt.intR) c (ring (6 * n + 5 + 1)).zero = _
      unfold delta; rw [ite_neg' _ _ hc]; show (Cyclo3.cyc3 LibInt.intR).zero = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).zero ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero _)) ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero _); lib_ring
    · show delta (Cyclo3.cyc3 LibInt.intR) (ring (6 * n + 5 + 1)).zero (ring (6 * n + 5 + 1)).zero = (Cyclo3.cyc3 LibInt.intR).one
      unfold delta; exact ite_pos' _ _ rfl
  | a :: as, h => by
    have ih := diag_form hpr w hw1 hw3 as (fun b hb => h b (Or.inr hb))
    exact step_f n hpr w hw1 hw3 a (h a (Or.inl rfl)) as _ _ _ _ ih.1 ih.2

end CubicDiagonal
#print axioms CubicDiagonal.R1c
#print axioms CubicDiagonal.R2c
#print axioms CubicDiagonal.R1z
#print axioms CubicDiagonal.R2z
#print axioms CubicDiagonal.step
#print axioms CubicDiagonal.diagonal
#print axioms CubicDiagonal.scale'
#print axioms CubicDiagonal.hct'
#print axioms CubicDiagonal.conv_a
#print axioms CubicDiagonal.P1
#print axioms CubicDiagonal.P2
#print axioms CubicDiagonal.P3
#print axioms CubicDiagonal.P4
#print axioms CubicDiagonal.shift0
#print axioms CubicDiagonal.wt_sum
#print axioms CubicDiagonal.cbcb
#print axioms CubicDiagonal.Z0
#print axioms CubicDiagonal.step_f
#print axioms CubicDiagonal.diag_form

#print axioms: 19 theorems, no axioms.

78.80 Diagonal quartic hypersurfaces in every dimension

Convolution with \(1+\chi +\rho +\bar \chi \) preserves the shape \(A+B\bar \chi +B^{(2)}\rho +B^{(3)}\chi \).

\[ \#\{x_1^4+\dots +x_k^4=c\}=A_k+B_k\bar \chi (c)+B^{(2)}_k\rho (c)+B^{(3)}_k\chi (c)\quad (c\ne 0),\qquad C_k\quad (c=0). \]

lang/QuartDiagonal.lean

/-
  lang/QuartDiagonal — T43, cycle 254: the diagonal quartic hypersurfaces x₁⁴ + … + x_k⁴ = c in every dimension.

  N_0(c) = [c = 0], N_{k+1}(c) = Σ_a N(a)N_k(c − a), N = 1 + χ + ρ + χ̄. For every k, with s = χ(−1), K = J(ρ, χ):
      N_k(c) = A_k + B_k χ̄(c) + B2_k ρ(c) + B3_k χ(c)   (c ≠ 0),   N_k(0) = C_k,
      A' = (p − 1)A − sB − B2 − sB3 + C,  B' = (B2 + B3)K + C − A,  B2' = B J̄ + B3 J + C − A,  B3' = (B + B2)K̄ + C − A,
      C' = (p − 1)(A + sB + B2 + sB3) + C.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.QuartSurface

namespace QuartDiagonal

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup FermatQuartic QuartTwist QuartTwistCount QuartSurface
open LibRing (castZ castN)

variable (n : Nat)

def Nk (ι : Zp (4 * n + 3 + 1)) : Nat → Zp (4 * n + 3 + 1) → Int × Int
  | 0, c => delta (Cyclo4.cyc4 LibInt.intR) c (ring (4 * n + 3 + 1)).zero
  | k + 1, c => sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (Nk ι k ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n)

theorem hp (k : Int × Int) (f : Zp (4 * n + 3 + 1) → Int × Int) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul k (f a)) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul k (sumK (Cyclo4.cyc4 LibInt.intR) f (LL n)) := (mul_sumK (Cyclo4.cyc4 LibInt.intR) k f (LL n)).symm
theorem hq (k : Int × Int) (f : Zp (4 * n + 3 + 1) → Int × Int) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (f a) k) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) f (LL n)) k := (sumK_mul (Cyclo4.cyc4 LibInt.intR) k f (LL n)).symm

theorem SBA (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι (om n a))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n +   -- (+81 chars; full line in the Zenodo source)
  rw [← FermatQuartic.T13 n hpr ι hi, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
theorem SAR (ι : Zp (4 * n + 3 + 1)) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι (om n a))) (LL n) = J2 n ι := by
  rw [← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
theorem SRB (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι (om n a))) (LL n) = gconj (J2 n ι) := by
  unfold J2; rw [gconj_sum]
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [gconj_mul, gconj_rho n hpr ι hi, chib_eq n hpr ι hi]) _
theorem SBR (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι (om n a))) (LL n) = gconj (J2 n ι) := by
  rw [← SRB n hpr ι hi, ← refl_om n]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [om_om]; exact (Cyclo4.cyc4 LibInt.intR).mul_comm _ _) _
theorem SBB (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι (om n a))) (LL n) = gconj (Jq n (chi4 n ι)) := by rw [← Jb_conj n hpr ι hi]; rfl

theorem scale (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) (F : Zp (4 * n + 3 + 1) → Int × Int) :
    sumK (Cyclo4.cyc4 LibInt.intR) F (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun t => F ((ring (4 * n + 3 + 1)).mul c t)) (LL n) := (CharSumGen.sum_scale (Cyclo4.cyc4 LibInt.intR) (4 * n + 2) hpr F c hc).symm

theorem hct (c t : Zp (4 * n + 3 + 1)) : (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul c t)) = (ring (4 * n + 3 + 1)).mul c (om n t) := by unfold om; lib_ring

theorem Q1c (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n)
      = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (Jq n (chi4 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) := by
  obtain ⟨v1, _, _⟩ := vals n hpr ι hi c hc
  rw [scale n hpr c hc]
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι (om n t))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chi4 n ι (om n t)))))
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chi4 n ι (om n t))))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (chi4 n ι (om n t))))) (fun t => by
    rw [hct]; unfold g4; rw [chi4_mul n hpr ι hi, chi4_mul n hpr ι hi, rho_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring)]
  rw [sumK_add, sumK_add, sumK_add, hp, hp, hp, hp, refl_om n (chi4 n ι), show sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from chi4_sum n hpr ι hi,
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun t => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chi4 n ι (om n t))) (LL n) = Jq n (chi4 n ι) from rfl,
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun t => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chi4 n ι (om n t))) (LL n) = J2 n ι from rfl, SBA n hpr ι hi,
    show (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι c) = rho n ι c from rfl, show (Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chi4 n ι c) = chib n ι c by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm]; exact chi_rho n hpr ι hi c,
    show (Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chi4 n ι c) = (Cyclo4.cyc4 LibInt.intR).one by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm]; exact v1]
  lib_ring

theorem Q2c (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n)
      = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (gconj (J2 n ι))) := by
  obtain ⟨_, _, v3⟩ := vals n hpr ι hi c hc
  rw [scale n hpr c hc]
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι (om n t))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (rho n ι (om n t)))))
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (rho n ι (om n t))))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (rho n ι (om n t))))) (fun t => by
    rw [hct]; unfold g4; rw [chi4_mul n hpr ι hi, rho_mul n hpr ι hi, rho_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring)]
  rw [sumK_add, sumK_add, sumK_add, hp, hp, hp, hp, refl_om n (rho n ι), show sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from rho_sum n hpr ι hi,
    SAR n ι, J_rho n hpr ι hi, SBR n hpr ι hi, chi_rho n hpr ι hi c, chib_rho n hpr ι hi c, v3]
  lib_ring

theorem Q3c (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n)
      = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (gconj (J2 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (gconj (Jq n (chi4 n ι)))) := by
  obtain ⟨v1, v2, _⟩ := vals n hpr ι hi c hc
  rw [scale n hpr c hc]
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chib n ι (om n t))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chib n ι (om n t)))))
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chib n ι (om n t))))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (chib n ι (om n t))))) (fun t => by
    rw [hct]; unfold g4; rw [chi4_mul n hpr ι hi, rho_mul n hpr ι hi, chib_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring)]
  rw [sumK_add, sumK_add, sumK_add, hp, hp, hp, hp, refl_om n (chib n ι), show sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from chib_sum n hpr ι hi,
    FermatQuartic.T13 n hpr ι hi, SRB n hpr ι hi, SBB n hpr ι hi, v1, v2,
    show (Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chib n ι c) = chi4 n ι c by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm]; exact chib_rho n hpr ι hi c]
  lib_ring

theorem one_minus (_hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) : sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero))) (LL n) = (Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo  -- (+30 chars; full line in the Zenodo source)
  have T1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := sum_oneq n _
  rw [sumK_add, QuartCyclo.sC_neg n, T1, show sumK (Cyclo4.cyc4 LibInt.intR) (fun a => delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero) (LL n) = (Cyclo4.cyc4 LibInt.intR).one from CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR) (ring (4 * n + 3 + 1)).zero]

theorem pt_vals (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) :
    (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι a) = (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero)) ∧
    (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι a) = (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero)) ∧
    (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι a) = rho n ι a := by
  by_cases ha : a = (ring (4 * n + 3 + 1)).zero
  · rw [ha, chib_zero, rho_zero, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero from chi4_zero n ι]; unfold delta; rw [ite_pos' _ _ rfl]
    exact ⟨by lib_ring, by lib_ring, by lib_ring⟩
  · obtain ⟨v1, v2, v3⟩ := vals n hpr ι hi a ha
    unfold delta; rw [ite_neg' _ _ ha]
    exact ⟨by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm, v1]; lib_ring, by rw [v3]; lib_ring, v2⟩

theorem Zs (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chi4 n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc  -- (+22 chars; full line in the Zenodo source)
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) ∧
    sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chib n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg a)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc  -- (+26 chars; full line in the Zenodo source)
  have hm : ∀ a, (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg a) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) a := fun a => by lib_ring
  have SA : sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chi4_sum n hpr ι hi
  have SR : sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := rho_sum n hpr ι hi
  have SB : sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chib_sum n hpr ι hi
  have breal : chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
    rw [chib_eq n hpr ι hi]; rcases FermatQuartic.m1_real n hpr ι hi with e | e <;> rw [e] <;> rfl
  refine ⟨?_, ?_, ?_⟩
  · rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chi4 n ι a) (rho n ι a)) (chib n ι a)) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 Lib  -- (+57 chars; full line in the Zenodo source)
      rw [hm, chi4_mul n hpr ι hi]; unfold g4 rho; rw [chib_eq n hpr ι hi a]
      by_cases ha : a = (ring (4 * n + 3 + 1)).zero
      · rw [ha, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (0, 0) from chi4_zero n ι]; unfold delta; rw [ite_pos' _ _ rfl]
        rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> (try rw [f]) <;> rfl
      · unfold delta; rw [ite_neg' _ _ ha]
        rcases ResidueCriterion.chi4_cases n ι a with e | e | e | e | e
        · exact absurd e (chi4_ne n hpr ι hi a ha)
        all_goals (rw [e]; rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> (try rw [f]) <;> rfl))]
    rw [hp, sumK_add, sumK_add, sumK_add, SA, SR, SB, one_minus n hpr]; lib_ring
  · rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun a => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (rho n ι a) (chib n ι a)) (chi4 n ι a)) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) a (ring (4 * n + 3 + 1)).zero)))) (fun a => by
      rw [hm, rho_mul n hpr ι hi, rho_neg1 n hpr ι hi]; unfold g4 rho; rw [chib_eq n hpr ι hi a]
      by_cases ha : a = (ring (4 * n + 3 + 1)).zero
      · rw [ha, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (0, 0) from chi4_zero n ι]; unfold delta; rw [ite_pos' _ _ rfl]
        rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> (try rw [f]) <;> rfl
      · unfold delta; rw [ite_neg' _ _ ha]
        rcases ResidueCriterion.chi4_cases n ι a with e | e | e | e | e
        · exact absurd e (chi4_ne n hpr ι hi a ha)
        all_goals (rw [e]; rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> (try rw [f]) <;> rfl))]
    rw [sumK_add, sumK_add, sumK_add, SA, SR, SB, one_minus n hpr]; lib_ring
  · rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun a => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chib n ι a) (chi4 n ι a)) (rho n ι a)) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 Lib  -- (+57 chars; full line in the Zenodo source)
      rw [hm, chib_mul n hpr ι hi, breal]; unfold g4 rho; rw [chib_eq n hpr ι hi a]
      by_cases ha : a = (ring (4 * n + 3 + 1)).zero
      · rw [ha, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (0, 0) from chi4_zero n ι]; unfold delta; rw [ite_pos' _ _ rfl]
        rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> (try rw [f]) <;> rfl
      · unfold delta; rw [ite_neg' _ _ ha]
        rcases ResidueCriterion.chi4_cases n ι a with e | e | e | e | e
        · exact absurd e (chi4_ne n hpr ι hi a ha)
        all_goals (rw [e]; rcases FermatQuartic.m1_real n hpr ι hi with f | f <;> (try rw [f]) <;> rfl))]
    rw [hp, sumK_add, sumK_add, sumK_add, SA, SR, SB, one_minus n hpr]; lib_ring

def sP : Int × Int := (Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)

theorem step (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (k : Nat) (A B B2 B3 Cc : Int × Int)
    (hne : ∀ c, c ≠ (ring (4 * n + 3 + 1)).zero → Nk n ι k c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).mul B (chib n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul B2 (rho n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul B3 (chi4 n ι c)))
    (h0 : Nk n ι k (ring (4 * n + 3 + 1)).zero = Cc) :
    (∀ c, c ≠ (ring (4 * n + 3 + 1)).zero → Nk n ι (k + 1) c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (sP n) A) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) B) B2) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1))  -- (+17 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add B2 B3) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) (chib n ι c)))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul B (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul B3 (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) (rho n ι c)))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add B B2) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) (chi4 n ι c))) ∧
    Nk n ι (k + 1) (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (sP n) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) B)) B2) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n   -- (+30 chars; full line in the Zenodo source)
  have split : ∀ c a, Nk n ι k ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).mul B (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))))
      ((Cyclo4.cyc4 LibInt.intR).mul B2 (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a))))) ((Cyclo4.cyc4 LibInt.intR).mul B3 (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))))
      ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a c) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) := fun c a => by
    by_cases ha : a = c
    · rw [ha, show (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg c) = (ring (4 * n + 3 + 1)).zero by lib_ring, h0, chib_zero, rho_zero, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero from chi4_zero n ι]
      unfold delta; rw [ite_pos' _ _ rfl]; lib_ring
    · have h1 : (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a) ≠ (ring (4 * n + 3 + 1)).zero := fun e => ha (by calc a = (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a))) := by lib_ring
                                                                    _ = c := by rw [e]; lib_ring)
      rw [hne _ h1]; unfold delta; rw [ite_neg' _ _ ha]; lib_ring
  have gen : ∀ c, Nk n ι (k + 1) c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (g4 n ι) (LL n)) A)
      ((Cyclo4.cyc4 LibInt.intR).mul B (sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n))))
      ((Cyclo4.cyc4 LibInt.intR).mul B2 (sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n))))
      ((Cyclo4.cyc4 LibInt.intR).mul B3 (sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n))))
      ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι c) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) := fun c => by
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun a => (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (Nk n ι k ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))) (LL n) = _
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun a => by rw [split c a])]
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun a => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) A) ((Cyclo4.cyc4 LibInt.intR).mul B ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a))))))
        ((Cyclo4.cyc4 LibInt.intR).mul B2 ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a)))))) ((Cyclo4.cyc4 LibInt.intR).mul B3 ((Cyclo4.cyc4 LibInt.intR).mul (g4 n ι a) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg a))))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) a c) (g4 n ι a)) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)))) (fun a => by lib_ring)]
    rw [sumK_add, sumK_add, sumK_add, sumK_add, hq, hp, hp, hp, hq, FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (g4 n ι) c]
  constructor
  · intro c hc
    rw [gen c, QuartSurface.g4_sum n hpr ι hi, Q3c n hpr ι hi c hc, Q2c n hpr ι hi c hc, Q1c n hpr ι hi c hc]
    unfold g4 sP; lib_ring
  · obtain ⟨z1, z2, z3⟩ := Zs n hpr ι hi
    rw [gen (ring (4 * n + 3 + 1)).zero, QuartSurface.g4_sum n hpr ι hi, z1, z2, z3]
    have g0 : g4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).one := by
      unfold g4; rw [chib_zero, rho_zero, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero from chi4_zero n ι]; lib_ring
    rw [g0]; unfold sP; lib_ring

/-- (A, B, B2, B3, C). -/
def Sq (ι : Zp (4 * n + 3 + 1)) : Nat → (Int × Int) × (Int × Int) × (Int × Int) × (Int × Int) × (Int × Int)
  | 0 => ((Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).one)
  | k + 1 =>
    let A := (Sq ι k).1
    let B := (Sq ι k).2.1
    let B2 := (Sq ι k).2.2.1
    let B3 := (Sq ι k).2.2.2.1
    let Cc := (Sq ι k).2.2.2.2
    ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (sP n) A) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) B) B2) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one  -- (+13 chars; full line in the Zenodo source)
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add B2 B3) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)),
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul B (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul B3 (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)),
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add B B2) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)),
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (sP n) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) B)) B2) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) B3))) Cc)

/-- **Diagonal quartic hypersurfaces in every dimension.** -/
theorem diagonal (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : ∀ k : Nat,
    (∀ c, c ≠ (ring (4 * n + 3 + 1)).zero → Nk n ι k c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Sq n ι k).1 ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ι k).2.1 (chib n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ι k).2.2.1 (rho n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul (Sq n ι k).2.2.2.1 (chi4 n ι c))) ∧
    Nk n ι k (ring (4 * n + 3 + 1)).zero = (Sq n ι k).2.2.2.2
  | 0 => by
    constructor
    · intro c hc
      show delta (Cyclo4.cyc4 LibInt.intR) c (ring (4 * n + 3 + 1)).zero = _
      unfold delta; rw [ite_neg' _ _ hc]; show (Cyclo4.cyc4 LibInt.intR).zero = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).zero ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero _)) ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero _)) ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero _);  -- (+9 chars; full line in the Zenodo source)
    · show delta (Cyclo4.cyc4 LibInt.intR) (ring (4 * n + 3 + 1)).zero (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).one
      unfold delta; exact ite_pos' _ _ rfl
  | k + 1 => by
    have ih := diagonal hpr ι hi k
    exact step n hpr ι hi k _ _ _ _ _ ih.1 ih.2

/-! ## diagonal quartic forms with coefficients: the nine basic sums -/

theorem PAA (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι c)) (Jq n (chi4 n ι)) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chi4 n ι (om n t)))) (fun t => by
    rw [hct, chi4_mul n hpr ι hi, chi4_mul n hpr ι hi]; lib_ring), hp]
  rfl

theorem PAR (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (rho n ι c)) (J2 n ι) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (rho n ι (om n t)))) (fun t => by
    rw [hct, chi4_mul n hpr ι hi, rho_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact SAR n ι

theorem PAB (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4  -- (+25 chars; full line in the Zenodo source)
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι t) (chib n ι (om n t)))) (fun t => by
    rw [hct, chi4_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact FermatQuartic.T13 n hpr ι hi

theorem PRA (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chi4 n ι c)) (J2 n ι) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chi4 n ι (om n t)))) (fun t => by
    rw [hct, rho_mul n hpr ι hi, chi4_mul n hpr ι hi]; lib_ring), hp]
  rfl

theorem PRR (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (rho n ι (om n t)))) (fun t => by
    rw [hct, rho_mul n hpr ι hi, rho_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact J_rho n hpr ι hi

theorem PRB (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chib n ι c)) (gconj (J2 n ι)) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι t) (chib n ι (om n t)))) (fun t => by
    rw [hct, rho_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact SRB n hpr ι hi

theorem PBA (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4  -- (+25 chars; full line in the Zenodo source)
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chi4 n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (chi4 n ι (om n t)))) (fun t => by
    rw [hct, chib_mul n hpr ι hi, chi4_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact SBA n hpr ι hi

theorem PBR (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (rho n ι c)) (gconj (J2 n ι)) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (rho n ι (om n t)))) (fun t => by
    rw [hct, chib_mul n hpr ι hi, rho_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact SBR n hpr ι hi

theorem PBB (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chib n ι c)) (gconj (Jq n (chi4 n ι))) := by
  rw [scale n hpr c hc, sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun t => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι c) (chib n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι t) (chib n ι (om n t)))) (fun t => by
    rw [hct, chib_mul n hpr ι hi, chib_mul n hpr ι hi]; lib_ring), hp]
  congr 1
  exact SBB n hpr ι hi

/-! ## forms with coefficients -/

def Nf (ι : Zp (4 * n + 3 + 1)) : List (Zp (4 * n + 3 + 1)) → Zp (4 * n + 3 + 1) → Int × Int
  | [], c => delta (Cyclo4.cyc4 LibInt.intR) c (ring (4 * n + 3 + 1)).zero
  | a :: as, c => sumK (Cyclo4.cyc4 LibInt.intR) (fun x => Nf ι as ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul a (q4 n x))))) (LL n)

/-- #{a x⁴ = u} = 1 + χ̄(a)χ(u) + ρ(a)ρ(u) + χ(a)χ̄(u). -/
def wt (ι a u : Zp (4 * n + 3 + 1)) : Int × Int :=
  (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chi4 n ι u))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι u))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι u))

theorem conv_a (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) (ha : a ≠ (ring (4 * n + 3 + 1)).zero) (F : Zp (4 * n + 3 + 1) → Int × Int) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun x => F ((ring (4 * n + 3 + 1)).mul a (q4 n x))) (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (F u)) (LL n) := by
  rw [FermatQuartic.step2 n hpr ι hi (fun v => F ((ring (4 * n + 3 + 1)).mul a v))]
  have hiv := CharSumGen.iv_mul (4 * n + 2) hpr a ha
  rw [← FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) ((ring (4 * n + 3 + 1)).mul a) ((ring (4 * n + 3 + 1)).mul (CharSumGen.iv (4 * n + 2) a))
    (fun x => by rw [← (ring (4 * n + 3 + 1)).mul_assoc, hiv]; lib_ring) (fun y => by rw [← (ring (4 * n + 3 + 1)).mul_assoc, (ring (4 * n + 3 + 1)).mul_comm a, hiv]; lib_ring) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (F u))]
  obtain ⟨v1, _, v3⟩ := vals n hpr ι hi a ha
  exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun v => by
    show (Cyclo4.cyc4 LibInt.intR).mul (g4 n ι v) (F ((ring (4 * n + 3 + 1)).mul a v)) = (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a ((ring (4 * n + 3 + 1)).mul a v)) (F ((ring (4 * n + 3 + 1)).mul a v))
    unfold wt g4
    rw [chi4_mul n hpr ι hi, rho_mul n hpr ι hi, chib_mul n hpr ι hi]
    rw [show (Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι v)) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι a)) (chi4 n ι v) by lib_ring,
      show (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι v)) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (rho n ι a)) (rho n ι v) by lib_ring,
      show (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι v)) = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chib n ι a)) (chib n ι v) by lib_ring, v1, v3]; lib_ring) _

theorem wt_sum (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) : sumK (Cyclo4.cyc4 LibInt.intR) (wt n ι a) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := by
  unfold wt
  have T1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun _ : Zp (4 * n + 3 + 1) => (Cyclo4.cyc4 LibInt.intR).one) (LL n) = (Int.ofNat (4 * n + 3 + 1 + 1), 0) := sum_oneq n _
  rw [sumK_add, sumK_add, sumK_add, T1, hp, hp, hp, show sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from chi4_sum n hpr ι hi,
    show sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from rho_sum n hpr ι hi, show sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero from chib_sum n hpr ι hi]
  lib_ring

theorem idents (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (u : Zp (4 * n + 3 + 1)) :
    (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (chib n ι u) = (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) u (ring (4 * n + 3 + 1)).zero)) ∧
    (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (rho n ι u) = (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) u (ring (4 * n + 3 + 1)).zero)) ∧
    (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (chib n ι u) = rho n ι u ∧ (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (chi4 n ι u) = rho n ι u ∧
    (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (rho n ι u) = chib n ι u ∧ (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (chi4 n ι u) = chib n ι u ∧
    (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (rho n ι u) = chi4 n ι u ∧ (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (chib n ι u) = chi4 n ι u ∧
    (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (chi4 n ι u) = (Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) u (ring (4 * n + 3 + 1)).zero)) := by
  obtain ⟨p1, p2, p3⟩ := pt_vals n hpr ι hi u
  exact ⟨by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm]; exact p1, p2, p3, rfl, chi_rho n hpr ι hi u, by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm]; exact chi_rho n hpr ι hi u,
    chib_rho n hpr ι hi u, by rw [(Cyclo4.cyc4 LibInt.intR).mul_comm]; exact chib_rho n hpr ι hi u, p1⟩

theorem ZW (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (chib n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (sP n)) ∧
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (rho n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (sP n) ∧
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (chi4 n ι ((ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg u)))) (LL n) = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (sP n)) := by
  have hm : ∀ u, (ring (4 * n + 3 + 1)).add (ring (4 * n + 3 + 1)).zero ((ring (4 * n + 3 + 1)).neg u) = (ring (4 * n + 3 + 1)).mul ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) u := fun u => by lib_ring
  have SA : sumK (Cyclo4.cyc4 LibInt.intR) (chi4 n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chi4_sum n hpr ι hi
  have SR : sumK (Cyclo4.cyc4 LibInt.intR) (rho n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := rho_sum n hpr ι hi
  have SB : sumK (Cyclo4.cyc4 LibInt.intR) (chib n ι) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := chib_sum n hpr ι hi
  have breal : chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) = chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by
    rw [chib_eq n hpr ι hi]; rcases FermatQuartic.m1_real n hpr ι hi with e | e <;> rw [e] <;> rfl
  have OM := one_minus n hpr
  refine ⟨?_, ?_, ?_⟩
  · rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chib n ι u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta  -- (+61 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chi4 n ι u))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (rho n ι u)))) (fun u => by
      obtain ⟨i1, _, i3, _, _, _, _, i8, _⟩ := idents n hpr ι hi u
      rw [hm, chib_mul n hpr ι hi, breal]; unfold wt
      calc _ = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chib n ι u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (chib n ι u))))
                ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (chib n ι u)))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (chib n ι u)))) := by lib_ring
        _ = _ := by rw [i1, i8, i3])]
    rw [hp, sumK_add, sumK_add, sumK_add, hp, hp, hp, SB, OM, SA, SR]; unfold sP; lib_ring
  · rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun u => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (rho n ι u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (chib n ι u))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) u (ring (4 * n  -- (+20 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (chi4 n ι u))) (fun u => by
      obtain ⟨_, i2, _, _, i5, _, i7, _, _⟩ := idents n hpr ι hi u
      rw [hm, rho_mul n hpr ι hi, rho_neg1 n hpr ι hi]; unfold wt
      calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (rho n ι u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (rho n ι u))))
                ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (rho n ι u)))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (rho n ι u))) := by lib_ring
        _ = _ := by rw [i5, i2, i7])]
    rw [sumK_add, sumK_add, sumK_add, hp, hp, hp, SB, OM, SA, SR]; unfold sP; lib_ring
  · rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chi4 n ι u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (rho n ι u)))
        ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (chib n ι u))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).one ((Cyclo4.cyc4 LibInt.intR).neg (delta (Cyclo4.cyc4 LibInt.intR) u (ring (4 * n + 3 + 1)).zero)))))) (fun u => by
      obtain ⟨_, _, _, i4, _, i6, _, _, i9⟩ := idents n hpr ι hi u
      rw [hm, chi4_mul n hpr ι hi]; unfold wt
      calc _ = (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (chi4 n ι u) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (chi4 n ι u))))
                ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (chi4 n ι u)))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (chi4 n ι u)))) := by lib_ring
        _ = _ := by rw [i4, i6, i9])]
    rw [hp, sumK_add, sumK_add, sumK_add, hp, hp, hp, SA, SR, SB, OM]; unfold sP; lib_ring

theorem shift0 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c : Zp (4 * n + 3 + 1)) :
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero ∧ sumK (Cyclo4.cyc4 LibInt.intR) (fun u => rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero ∧
    sumK (Cyclo4.cyc4 LibInt.intR) (fun u => chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) (LL n) = (Cyclo4.cyc4 LibInt.intR).zero := by
  have r : ∀ G : Zp (4 * n + 3 + 1) → Int × Int, sumK (Cyclo4.cyc4 LibInt.intR) (fun u => G ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) (LL n) = sumK (Cyclo4.cyc4 LibInt.intR) G (LL n) := fun G =>
    FiniteRecChain.reindex (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (fun u => (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)) (fun u => (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)) (fun x => by lib_ring) (fun y => by lib_ring) G
  exact ⟨(r _).trans (chi4_sum n hpr ι hi), (r _).trans (rho_sum n hpr ι hi), (r _).trans (chib_sum n hpr ι hi)⟩

theorem step_f (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (a : Zp (4 * n + 3 + 1)) (ha : a ≠ (ring (4 * n + 3 + 1)).zero) (as : List (Zp (4 * n + 3 + 1))) (A B B2 B3 Cc : Int × Int)
    (hne : ∀ c, c ≠ (ring (4 * n + 3 + 1)).zero → Nf n ι as c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).mul B (chib n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul B2 (rho n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul B3 (chi4 n ι c)))
    (h0 : Nf n ι as (ring (4 * n + 3 + 1)).zero = Cc) :
    (∀ c, c ≠ (ring (4 * n + 3 + 1)).zero → Nf n ι (a :: as) c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add
        ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), 0) A) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chib n ι a)) B) ((Cyclo4.cyc4 LibInt.i  -- (+245 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B2 (chib n ι a)) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B3 (rho n ι a)) (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)))) (chib   -- (+8 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B (chi4 n ι a)) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B3 (chib n ι a)) (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibI  -- (+32 chars; full line in the Zenodo source)
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B (rho n ι a)) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B2 (chi4 n ι a)) (gconj (J2 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).n  -- (+25 chars; full line in the Zenodo source)
    Nf n ι (a :: as) (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), 0) A)
      ((Cyclo4.cyc4 LibInt.intR).mul B ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (sP n)))))
      ((Cyclo4.cyc4 LibInt.intR).mul B2 ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (sP n)))) ((Cyclo4.cyc4 LibInt.intR).mul B3 ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (sP n))))) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)) := by
  have split : ∀ c u, Nf n ι as ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add A ((Cyclo4.cyc4 LibInt.intR).mul B (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))))
      ((Cyclo4.cyc4 LibInt.intR).mul B2 (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))))) ((Cyclo4.cyc4 LibInt.intR).mul B3 (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))))
      ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) u c) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) := fun c u => by
    by_cases hu : u = c
    · rw [hu, show (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg c) = (ring (4 * n + 3 + 1)).zero by lib_ring, h0, chib_zero, rho_zero, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero from chi4_zero n ι]
      unfold delta; rw [ite_pos' _ _ rfl]; lib_ring
    · have h1 : (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u) ≠ (ring (4 * n + 3 + 1)).zero := fun e => hu (by calc u = (ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) := by lib_ring
                                                                    _ = c := by rw [e]; lib_ring)
      rw [hne _ h1]; unfold delta; rw [ite_neg' _ _ hu]; lib_ring
  have gen : ∀ c, Nf n ι (a :: as) c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (wt n ι a) (LL n)) A)
      ((Cyclo4.cyc4 LibInt.intR).mul B (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n))))
      ((Cyclo4.cyc4 LibInt.intR).mul B2 (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n))))
      ((Cyclo4.cyc4 LibInt.intR).mul B3 (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n))))
      ((Cyclo4.cyc4 LibInt.intR).mul (wt n ι a c) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))) := fun c => by
    show sumK (Cyclo4.cyc4 LibInt.intR) (fun x => Nf n ι as ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul a (q4 n x))))) (LL n) = _
    rw [conv_a n hpr ι hi a ha (fun u => Nf n ι as ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))]
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun u => by rw [split c u])]
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun u => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) A) ((Cyclo4.cyc4 LibInt.intR).mul B ((Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (chib n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))))))
        ((Cyclo4.cyc4 LibInt.intR).mul B2 ((Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (rho n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))))) ((Cyclo4.cyc4 LibInt.intR).mul B3 ((Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (chi4 n ι ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))))))
        ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) u c) (wt n ι a u)) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)))) (fun u => by lib_ring)]
    rw [sumK_add, sumK_add, sumK_add, sumK_add, hq, hp, hp, hp, hq, FiniteRecChain.pick (Cyclo4.cyc4 LibInt.intR) (LL n) (CMInert.complete_allZ (4 * n + 3 + 1) (Cyclo4.cyc4 LibInt.intR)) (wt n ι a) c]
  have expand : ∀ c (ψ : Zp (4 * n + 3 + 1) → Int × Int), sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (wt n ι a u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n) =
      (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) (LL n))
        ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n))))
        ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n))))
        ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (sumK (Cyclo4.cyc4 LibInt.intR) (fun u => (Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))) (LL n))) := fun c ψ => by
    unfold wt
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun u => (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u))))))
        ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι u) (ψ ((ring (4 * n + 3 + 1)).add c ((ring (4 * n + 3 + 1)).neg u)))))) (fun u => by lib_ring),
      sumK_add, sumK_add, sumK_add, hp, hp, hp]
  constructor
  · intro c hc
    obtain ⟨sA, sR, sB⟩ := shift0 n hpr ι hi c
    have dz : delta (Cyclo4.cyc4 LibInt.intR) c (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero := ite_neg' _ _ hc
    obtain ⟨i1, i2, i3, i4, i5, i6, i7, i8, i9⟩ := idents n hpr ι hi c
    rw [dz] at i1 i2 i9
    rw [gen c, wt_sum n hpr ι hi a, expand c (chib n ι), expand c (rho n ι), expand c (chi4 n ι), sA, sR, sB,
      PAB n hpr ι hi c hc, PRB n hpr ι hi c hc, PBB n hpr ι hi c hc, PAR n hpr ι hi c hc, PRR n hpr ι hi c hc, PBR n hpr ι hi c hc,
      PAA n hpr ι hi c hc, PRA n hpr ι hi c hc, PBA n hpr ι hi c hc, i1, i2, i3, i4, i5, i6, i7, i8, i9]
    unfold wt; lib_ring
  · obtain ⟨z1, z2, z3⟩ := ZW n hpr ι hi a
    rw [gen (ring (4 * n + 3 + 1)).zero, wt_sum n hpr ι hi a, z1, z2, z3]
    have w0 : wt n ι a (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).one := by
      unfold wt; rw [chib_zero, rho_zero, show chi4 n ι (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).zero from chi4_zero n ι]; lib_ring
    rw [w0]; lib_ring

def Sf (ι : Zp (4 * n + 3 + 1)) : List (Zp (4 * n + 3 + 1)) → (Int × Int) × (Int × Int) × (Int × Int) × (Int × Int) × (Int × Int)
  | [] => ((Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).zero, (Cyclo4.cyc4 LibInt.intR).one)
  | a :: as =>
    let A := (Sf ι as).1
    let B := (Sf ι as).2.1
    let B2 := (Sf ι as).2.2.1
    let B3 := (Sf ι as).2.2.2.1
    let Cc := (Sf ι as).2.2.2.2
    ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), 0) A) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) (chib n ι a)) B) ((Cyclo4.cyc4 LibInt.intR)  -- (+241 chars; full line in the Zenodo source)
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B2 (chib n ι a)) (J2 n ι)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B3 (rho n ι a)) (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))),
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B (chi4 n ι a)) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B3 (chib n ι a)) (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))),
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B (rho n ι a)) (gconj (J2 n ι))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul B2 (chi4 n ι a)) (gconj (J2 n ι)))) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A))),
     (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), 0) A)
      ((Cyclo4.cyc4 LibInt.intR).mul B ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chib n ι a) (sP n)))))
      ((Cyclo4.cyc4 LibInt.intR).mul B2 ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι a) (sP n)))) ((Cyclo4.cyc4 LibInt.intR).mul B3 ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι a) (sP n))))) ((Cyclo4.cyc4 LibInt.intR).add Cc ((Cyclo4.cyc4 LibInt.intR).neg A)))

/-- **Every diagonal quartic form Σ a_i x_i⁴ over 𝔽_p** (a_i ≠ 0). -/
theorem diag_form (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : ∀ as : List (Zp (4 * n + 3 + 1)), (∀ a, memL a as → a ≠ (ring (4 * n + 3 + 1)).zero) →
    (∀ c, c ≠ (ring (4 * n + 3 + 1)).zero → Nf n ι as c = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Sf n ι as).1 ((Cyclo4.cyc4 LibInt.intR).mul (Sf n ι as).2.1 (chib n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul (Sf n ι as).2.2.1 (rho n ι c))) ((Cyclo4.cyc4 LibInt.intR).mul (Sf n ι as).2.2.2.1 (chi4 n ι c))) ∧
    Nf n ι as (ring (4 * n + 3 + 1)).zero = (Sf n ι as).2.2.2.2
  | [], _ => by
    constructor
    · intro c hc
      show delta (Cyclo4.cyc4 LibInt.intR) c (ring (4 * n + 3 + 1)).zero = _
      unfold delta; rw [ite_neg' _ _ hc]; show (Cyclo4.cyc4 LibInt.intR).zero = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Cyclo4.cyc4 LibInt.intR).zero ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero _)) ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero _)) ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero _);  -- (+9 chars; full line in the Zenodo source)
    · show delta (Cyclo4.cyc4 LibInt.intR) (ring (4 * n + 3 + 1)).zero (ring (4 * n + 3 + 1)).zero = (Cyclo4.cyc4 LibInt.intR).one
      unfold delta; exact ite_pos' _ _ rfl
  | a :: as, h => by
    have ih := diag_form hpr ι hi as (fun b hb => h b (Or.inr hb))
    exact step_f n hpr ι hi a (h a (Or.inl rfl)) as _ _ _ _ _ ih.1 ih.2

end QuartDiagonal
#print axioms QuartDiagonal.hp
#print axioms QuartDiagonal.hq
#print axioms QuartDiagonal.SBA
#print axioms QuartDiagonal.SAR
#print axioms QuartDiagonal.SRB
#print axioms QuartDiagonal.SBR
#print axioms QuartDiagonal.SBB
#print axioms QuartDiagonal.scale
#print axioms QuartDiagonal.hct
#print axioms QuartDiagonal.Q1c
#print axioms QuartDiagonal.Q2c
#print axioms QuartDiagonal.Q3c
#print axioms QuartDiagonal.one_minus
#print axioms QuartDiagonal.pt_vals
#print axioms QuartDiagonal.Zs
#print axioms QuartDiagonal.step
#print axioms QuartDiagonal.diagonal
#print axioms QuartDiagonal.PAA
#print axioms QuartDiagonal.PAR
#print axioms QuartDiagonal.PAB
#print axioms QuartDiagonal.PRA
#print axioms QuartDiagonal.PRR
#print axioms QuartDiagonal.PRB
#print axioms QuartDiagonal.PBA
#print axioms QuartDiagonal.PBR
#print axioms QuartDiagonal.PBB
#print axioms QuartDiagonal.conv_a
#print axioms QuartDiagonal.wt_sum
#print axioms QuartDiagonal.idents
#print axioms QuartDiagonal.ZW
#print axioms QuartDiagonal.shift0
#print axioms QuartDiagonal.step_f
#print axioms QuartDiagonal.diag_form

#print axioms: 33 theorems, no axioms.

78.81 Sums of squares over \(\mathbb F_p\)

\(\#\{x^2=a\}=1+\chi (a)\) and \(\sum _a\chi (a)\chi (c-a)=-\chi (-1)\) give a recursion valid for every odd prime.

\[ A_{k+1}=(p-1)A_k-eB_k+C_k,\quad B_{k+1}=C_k-A_k,\quad C_{k+1}=(p-1)(A_k+eB_k)+C_k,\qquad e=\chi (-1). \]

lang/QuadDiagonal.lean

/-
  lang/QuadDiagonal — T43, cycle 255: the quadrics x₁² + … + x_k² = c over 𝔽_p for every odd prime p and every k.

  p = 2m + 3, χ the Legendre symbol (QChar, integer values), e = χ(−1). #{x² = a} = 1 + χ(a) (sq_count), and
  Σ_a χ(a)χ(c − a) = −e for c ≠ 0 (ConicSum.sum_xxd), so N_{k+1}(c) = Σ_x N_k(c − x²) keeps the shape:
      N_k(c) = A_k + B_k χ(c)  (c ≠ 0),   N_k(0) = C_k,
      A' = (p − 1)A − eB + C,   B' = C − A,   C' = (p − 1)(A + eB) + C,     A_0 = B_0 = 0, C_0 = 1.
      quad_diagonal   for every k (Gauss–Jordan: the number of representations by a sum of k squares over 𝔽_p)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.ConicSum
import lang.DLog
import lang.PrimRoot
import lang.GaussSum

namespace QuadDiagonal

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QChar ConicSum
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

theorem sq_root (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ha0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ha : pw (ring (2 * m + 1 + 1)) a (m + 1) = (ring (2 * m + 1 + 1)).one) : ∃ c, (ring (2 * m + 1 + 1)).mul c c = a := by
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    match DLog.mem_pows (2 * m + 1) g a _ (DLog.every_unit (2 * m + 1) hpr g hg a ha0) with
    | ⟨k, _, hk⟩ =>
      have h1 : pw (ring (2 * m + 1 + 1)) g (k * (m + 1)) = (ring (2 * m + 1 + 1)).one := by rw [← TwoPower.pwpow, ← hk, ha]
      match hg.2 _ h1 with
      | ⟨c, hc⟩ =>
        have h3 : k = 2 * c := by
          apply Nat.eq_of_mul_eq_mul_right (Nat.zero_lt_succ m)
          rw [hc]; lib_nat
        refine ⟨pw (ring (2 * m + 1 + 1)) g c, ?_⟩
        rw [hk, h3, show 2 * c = c * 2 by lib_nat, ← TwoPower.pwpow]
        show _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _
        lib_ring

theorem sI_add {A : Type} (F G : A → Int) (l : List A) : sumK LibInt.intR (fun x => F x + G x) l = sumK LibInt.intR F l + sumK LibInt.intR G l := sumK_add LibInt.intR F G l
theorem sI_mul {A : Type} (c : Int) (F : A → Int) (l : List A) : sumK LibInt.intR (fun x => c * F x) l = c * sumK LibInt.intR F l := (mul_sumK LibInt.intR c F l).symm
theorem sI_mulr {A : Type} (c : Int) (F : A → Int) (l : List A) : sumK LibInt.intR (fun x => F x * c) l = sumK LibInt.intR F l * c := (sumK_mul LibInt.intR c F l).symm
theorem zsum {A : Type} : ∀ l : List A, sumK LibInt.intR (fun _ => (0 : Int)) l = 0
  | [] => rfl
  | _ :: as => by show 0 + sumK LibInt.intR (fun _ => (0 : Int)) as = 0; rw [zsum as]; rfl

theorem two_ne : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero := fun e => ne_pm m (by
  calc (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) := by rw [e]; lib_ring
    _ = (ring (2 * m + 1 + 1)).one := by lib_ring)

theorem nozd' (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul x y = (ring (2 * m + 1 + 1)).zero) : x = (ring (2 * m + 1 + 1)).zero ∨ y = (ring (2 * m + 1 + 1)).zero := nozd (2 * m + 1) hpr x y h

theorem two_roots (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) (x : Zp (2 * m + 1 + 1)) :
    delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul c c) = delta LibInt.intR x c + delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg c) := by
  have d : c ≠ (ring (2 * m + 1 + 1)).neg c := fun e => hc (by
    have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) c = (ring (2 * m + 1 + 1)).zero := by
      calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) c = (ring (2 * m + 1 + 1)).add c c := by lib_ring
        _ = (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg c) := congrArg ((ring (2 * m + 1 + 1)).add c) e
        _ = (ring (2 * m + 1 + 1)).zero := by lib_ring
    cases nozd' m hpr _ _ e2 with
    | inl h => exact absurd h (two_ne m)
    | inr h => exact h)
  unfold delta
  by_cases hx : (ring (2 * m + 1 + 1)).mul x x = (ring (2 * m + 1 + 1)).mul c c
  · rw [ite_pos' _ _ hx]
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg c)) ((ring (2 * m + 1 + 1)).add x c) = (ring (2 * m + 1 + 1)).zero := by
      rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg c)) ((ring (2 * m + 1 + 1)).add x c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul c c)) by lib_ring, hx]; lib_ring
    cases nozd' m hpr _ _ e with
    | inl h =>
      have ex : x = c := by calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg c)) c := by lib_ring
                                _ = c := by rw [h]; lib_ring
      rw [ite_pos' _ _ ex, ite_neg' _ _ (by rw [ex]; exact d)]; rfl
    | inr h =>
      have ex : x = (ring (2 * m + 1 + 1)).neg c := by calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x c) ((ring (2 * m + 1 + 1)).neg c) := by lib_ring
                                      _ = (ring (2 * m + 1 + 1)).neg c := by rw [h]; lib_ring
      rw [ite_neg' _ _ (by rw [ex]; exact fun e => d e.symm), ite_pos' _ _ ex]; rfl
  · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by rw [e])), ite_neg' _ _ (fun e => hx (by rw [e]; lib_ring))]; rfl

/-- **#{x² = a} = 1 + χ(a).** -/
theorem sq_count (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) : sumK LibInt.intR (fun x => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) a) (L m) = 1 + chi m a := by
  have hcomp : ∀ z, sumK LibInt.intR (fun x => delta LibInt.intR x z) (L m) = 1 := fun z => CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR z
  by_cases ha : a = (ring (2 * m + 1 + 1)).zero
  · rw [ha, sumK_ext LibInt.intR _ (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (fun x => by
      unfold delta
      by_cases hx : x = (ring (2 * m + 1 + 1)).zero
      · rw [ite_pos' _ _ hx, ite_pos' _ _ (by rw [hx]; lib_ring)]
      · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by
          cases nozd' m hpr _ _ e with
          | inl h => exact h
          | inr h => exact h))]), hcomp, chi_zero]; rfl
  · cases Euler.euler_pm m hpr a ha with
    | inl ep =>
      match sq_root m hpr a ha ep with
      | ⟨c, hc⟩ =>
        have hc0 : c ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => ha (by rw [← hc, e0]; lib_ring)
        rw [← hc, sumK_ext LibInt.intR _ _ (fun x => two_roots m hpr c hc0 x), sI_add, hcomp, hcomp, hc, chi_one_of m a ha ep]
    | inr en =>
      have en' : pw (ring (2 * m + 1 + 1)) a (m + 1) ≠ (ring (2 * m + 1 + 1)).one := by rw [en]; exact ne_pm m
      have hz : ∀ x, delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) a = 0 := fun x => by
        unfold delta
        rw [ite_neg' _ _ (fun ex => by
          have hx0 : x ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => ha (by rw [← ex, e0]; lib_ring)
          have h1 := chi_sq m hpr x hx0
          rw [ex, chi_neg_of m a ha en'] at h1
          exact Int.noConfusion h1)]; rfl
      rw [sumK_ext LibInt.intR _ _ hz, zsum, chi_neg_of m a ha en']; rfl

/-- N_k(c) = #{x₁² + … + x_k² = c}. -/
def Nk : Nat → Zp (2 * m + 1 + 1) → Int
  | 0, c => delta LibInt.intR c (ring (2 * m + 1 + 1)).zero
  | k + 1, c => sumK LibInt.intR (fun x => Nk k ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) (L m)

theorem hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR

theorem conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (F : Zp (2 * m + 1 + 1) → Int) : sumK LibInt.intR (fun x => F ((ring (2 * m + 1 + 1)).mul x x)) (L m) = sumK LibInt.intR (fun a => (1 + chi m a) * F a) (L m) := by
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun a => delta LibInt.intR a ((ring (2 * m + 1 + 1)).mul x x) * F a) (L m)) (fun x => (FiniteRecChain.pick LibInt.intR (L m) (hcomp m) F _).symm), sumK_swap]
  exact sumK_ext LibInt.intR _ _ (fun a => by
    rw [← sq_count m hpr a, ← sI_mulr]
    exact sumK_ext LibInt.intR _ _ (fun x => by rw [delta_symm]) _) _

theorem e_val (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 1 ∨ chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = -1 := by
  have h : (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * m + 1) (by
    calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero := by rw [e]
      _ = (ring (2 * m + 1 + 1)).zero := by lib_ring)
  cases Euler.euler_pm m hpr _ h with
  | inl ep => exact Or.inl (chi_one_of m _ h ep)
  | inr en => exact Or.inr (chi_neg_of m _ h (by rw [en]; exact ne_pm m))


theorem T1 : sumK LibInt.intR (fun _ : Zp (2 * m + 1 + 1) => (1 : Int)) (L m) = (Int.ofNat (2 * m + 1 + 1 + 1)) := GaussSum.sum_one m _

theorem S0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (chi m) (L m) = 0 := chi_sum m hpr

theorem S1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) : sumK LibInt.intR (fun a => chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (L m) = 0 := by
  rw [FiniteRecChain.reindex LibInt.intR (L m) (hcomp m) (fun a => (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)) (fun a => (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)) (fun x => by lib_ring) (fun y => by lib_ring) (chi m)]
  exact S0 m hpr

theorem S2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) : sumK LibInt.intR (fun a => chi m a * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (L m) = -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) := by
  have hnc : (ring (2 * m + 1 + 1)).neg c ≠ (ring (2 * m + 1 + 1)).zero := fun h => hc (by calc c = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg c) := by lib_ring
                                                        _ = (ring (2 * m + 1 + 1)).zero := by rw [h]; lib_ring)
  rw [sumK_ext LibInt.intR _ (fun a => (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * chi m ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg c)))) (fun a => by
    rw [show (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg c)) by lib_ring, chi_mul m hpr, chi_mul m hpr]; lib_int),
    sI_mul, sum_xxd m hpr _ hnc]; lib_int

theorem sqchi (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) : chi m a * chi m a = 1 + -(delta LibInt.intR a (ring (2 * m + 1 + 1)).zero) := by
  unfold delta
  by_cases ha : a = (ring (2 * m + 1 + 1)).zero
  · rw [ha, chi_zero, ite_pos' _ _ rfl]; rfl
  · rw [ite_neg' _ _ ha]
    cases Euler.euler_pm m hpr a ha with
    | inl ep => rw [chi_one_of m a ha ep]; rfl
    | inr en => rw [chi_neg_of m a ha (by rw [en]; exact ne_pm m)]; rfl

theorem S3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun a => chi m a * chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a))) (L m) = (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) := by
  rw [sumK_ext LibInt.intR _ (fun a => (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR a (ring (2 * m + 1 + 1)).zero))) (fun a => by
    rw [show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) a by lib_ring, chi_mul m hpr, ← sqchi m hpr a]; lib_int),
    sI_mul, sI_add, T1 m, show sumK LibInt.intR (fun a => -(delta LibInt.intR a (ring (2 * m + 1 + 1)).zero)) (L m) = -1 by
      rw [sumK_ext LibInt.intR _ (fun a => -1 * delta LibInt.intR a (ring (2 * m + 1 + 1)).zero) (fun a => by lib_int), sI_mul, hcomp m]; rfl]

theorem Psum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun a => 1 + chi m a) (L m) = (Int.ofNat (2 * m + 1 + 1 + 1)) := by
  rw [sI_add, T1 m, S0 m hpr]; lib_int

/-- **One step**. -/
theorem step (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (k : Nat) (A B Cc : Int) (hne : ∀ c, c ≠ (ring (2 * m + 1 + 1)).zero → Nk m k c = A + B * chi m c) (h0 : Nk m k (ring (2 * m + 1 + 1)).zero = Cc) :
    (∀ c, c ≠ (ring (2 * m + 1 + 1)).zero → Nk m (k + 1) c = ((Int.ofNat (2 * m + 1 + 1 + 1)) * A + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * B) + Cc + -A) + (Cc + -A) * chi m c) ∧
    Nk m (k + 1) (ring (2 * m + 1 + 1)).zero = (Int.ofNat (2 * m + 1 + 1 + 1)) * A + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * B * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + Cc + -A := by
  have split : ∀ c a, Nk m k ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)) = A + B * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)) + delta LibInt.intR a c * (Cc + -A) := fun c a => by
    by_cases ha : a = c
    · rw [ha, show (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg c) = (ring (2 * m + 1 + 1)).zero by lib_ring, h0, chi_zero, show delta LibInt.intR c c = 1 from ite_pos' _ _ rfl]; lib_int
    · have h1 : (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a) ≠ (ring (2 * m + 1 + 1)).zero := fun h => ha (by calc a = (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) := by lib_ring
                                                                    _ = c := by rw [h]; lib_ring)
      rw [hne _ h1, show delta LibInt.intR a c = 0 from ite_neg' _ _ ha]; lib_int
  have gen : ∀ c, Nk m (k + 1) c = A * (Int.ofNat (2 * m + 1 + 1 + 1)) + B * (sumK LibInt.intR (fun a => chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (L m) + sumK LibInt.intR (fun a => chi m a * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) (L m))
      + (1 + chi m c) * (Cc + -A) := fun c => by
    show sumK LibInt.intR (fun x => Nk m k ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) (L m) = _
    rw [conv m hpr (fun a => Nk m k ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)))]
    rw [sumK_ext LibInt.intR _ (fun a => A * (1 + chi m a) + B * (chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a)) + chi m a * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg a))) + delta LibInt.intR a c * ((1 + chi m a) * (Cc + -A))) (fun a => by
      rw [split c a]; lib_int)]
    have hpk : sumK LibInt.intR (fun a => delta LibInt.intR a c * ((1 + chi m a) * (Cc + -A))) (L m) = (1 + chi m c) * (Cc + -A) :=
      FiniteRecChain.pick LibInt.intR (L m) (hcomp m) (fun a => (1 + chi m a) * (Cc + -A)) c
    rw [sI_add, sI_add, sI_mul, sI_mul, sI_add, sI_add, T1 m, S0 m hpr, hpk]
    lib_int
  constructor
  · intro c hc
    rw [gen c, S1 m hpr c, S2 m hpr c hc]; lib_int
  · rw [gen (ring (2 * m + 1 + 1)).zero, S1 m hpr (ring (2 * m + 1 + 1)).zero, S3 m hpr, chi_zero]; lib_int

def Sq : Nat → Int × Int × Int
  | 0 => (0, 0, 1)
  | k + 1 => ((Int.ofNat (2 * m + 1 + 1 + 1)) * (Sq k).1 + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sq k).2.1) + (Sq k).2.2 + -(Sq k).1, (Sq k).2.2 + -(Sq k).1,
              (Int.ofNat (2 * m + 1 + 1 + 1)) * (Sq k).1 + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sq k).2.1 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + (Sq k).2.2 + -(Sq k).1)

/-- **Sums of k squares over 𝔽_p, every k.** -/
theorem quad_diagonal (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∀ k : Nat,
    (∀ c, c ≠ (ring (2 * m + 1 + 1)).zero → Nk m k c = (Sq m k).1 + (Sq m k).2.1 * chi m c) ∧ Nk m k (ring (2 * m + 1 + 1)).zero = (Sq m k).2.2
  | 0 => by
    constructor
    · intro c hc; show delta LibInt.intR c (ring (2 * m + 1 + 1)).zero = 0 + 0 * chi m c; rw [show delta LibInt.intR c (ring (2 * m + 1 + 1)).zero = 0 from ite_neg' _ _ hc]; lib_int
    · show delta LibInt.intR (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero = 1; unfold delta; exact ite_pos' _ _ rfl
  | k + 1 => by
    have ih := quad_diagonal hpr k
    exact step m hpr k _ _ _ ih.1 ih.2

/-! ## diagonal forms with coefficients -/

/-- N_as(c) = #{Σ a_i x_i² = c} for a list of coefficients. -/
def Nf : List (Zp (2 * m + 1 + 1)) → Zp (2 * m + 1 + 1) → Int
  | [], c => delta LibInt.intR c (ring (2 * m + 1 + 1)).zero
  | a :: as, c => sumK LibInt.intR (fun x => Nf as ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))))) (L m)

theorem chi_sq_val (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) : chi m a * chi m a = 1 := by
  rw [sqchi m hpr a, show delta LibInt.intR a (ring (2 * m + 1 + 1)).zero = 0 from ite_neg' _ _ ha]; rfl

/-- #{a x² = u} = 1 + χ(a)χ(u). -/
theorem conv_a (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (F : Zp (2 * m + 1 + 1) → Int) :
    sumK LibInt.intR (fun x => F ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) (L m) = sumK LibInt.intR (fun u => (1 + chi m a * chi m u) * F u) (L m) := by
  rw [conv m hpr (fun v => F ((ring (2 * m + 1 + 1)).mul a v))]
  have hi := ConicSum.iv_mul_self m hpr a ha
  rw [← FiniteRecChain.reindex LibInt.intR (L m) (hcomp m) ((ring (2 * m + 1 + 1)).mul a) ((ring (2 * m + 1 + 1)).mul (ConicSum.iv m a))
    (fun x => by rw [← (ring (2 * m + 1 + 1)).mul_assoc, hi]; lib_ring)
    (fun y => by rw [← (ring (2 * m + 1 + 1)).mul_assoc, (ring (2 * m + 1 + 1)).mul_comm a, hi]; lib_ring) (fun u => (1 + chi m a * chi m u) * F u)]
  exact sumK_ext LibInt.intR _ _ (fun v => by
    show (1 + chi m v) * F ((ring (2 * m + 1 + 1)).mul a v) = (1 + chi m a * chi m ((ring (2 * m + 1 + 1)).mul a v)) * F ((ring (2 * m + 1 + 1)).mul a v)
    rw [chi_mul m hpr, show chi m a * (chi m a * chi m v) = (chi m a * chi m a) * chi m v by lib_int, chi_sq_val m hpr a ha]; lib_int) _

/-- **One step for a coefficient a ≠ 0** (δ = χ(a)). -/
theorem step_a (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (as : List (Zp (2 * m + 1 + 1))) (A B Cc : Int)
    (hne : ∀ c, c ≠ (ring (2 * m + 1 + 1)).zero → Nf m as c = A + B * chi m c) (h0 : Nf m as (ring (2 * m + 1 + 1)).zero = Cc) :
    (∀ c, c ≠ (ring (2 * m + 1 + 1)).zero → Nf m (a :: as) c = ((Int.ofNat (2 * m + 1 + 1 + 1)) * A + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * B) + Cc + -A) + chi m a * (Cc + -A) * chi m c) ∧
    Nf m (a :: as) (ring (2 * m + 1 + 1)).zero = (Int.ofNat (2 * m + 1 + 1 + 1)) * A + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * B * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + Cc + -A := by
  have split : ∀ c u, Nf m as ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u)) = A + B * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u)) + delta LibInt.intR u c * (Cc + -A) := fun c u => by
    by_cases hu : u = c
    · rw [hu, show (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg c) = (ring (2 * m + 1 + 1)).zero by lib_ring, h0, chi_zero, show delta LibInt.intR c c = 1 from ite_pos' _ _ rfl]; lib_int
    · have h1 : (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u) ≠ (ring (2 * m + 1 + 1)).zero := fun h => hu (by calc u = (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u))) := by lib_ring
                                                                    _ = c := by rw [h]; lib_ring)
      rw [hne _ h1, show delta LibInt.intR u c = 0 from ite_neg' _ _ hu]; lib_int
  have gen : ∀ c, Nf m (a :: as) c = A * (Int.ofNat (2 * m + 1 + 1 + 1)) + chi m a * B * (sumK LibInt.intR (fun u => chi m u * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u))) (L m))
      + B * sumK LibInt.intR (fun u => chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u))) (L m) + (1 + chi m a * chi m c) * (Cc + -A) := fun c => by
    show sumK LibInt.intR (fun x => Nf m as ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))))) (L m) = _
    rw [conv_a m hpr a ha (fun u => Nf m as ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u)))]
    rw [sumK_ext LibInt.intR _ (fun u => A * 1 + A * (chi m a * chi m u) + B * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u)) + chi m a * B * (chi m u * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg u)))
        + delta LibInt.intR u c * ((1 + chi m a * chi m u) * (Cc + -A))) (fun u => by rw [split c u]; lib_int)]
    have hpk : sumK LibInt.intR (fun u => delta LibInt.intR u c * ((1 + chi m a * chi m u) * (Cc + -A))) (L m) = (1 + chi m a * chi m c) * (Cc + -A) :=
      FiniteRecChain.pick LibInt.intR (L m) (hcomp m) (fun u => (1 + chi m a * chi m u) * (Cc + -A)) c
    rw [sI_add, sI_add, sI_add, sI_add, sI_mul, sI_mul, sI_mul, sI_mul, sI_mul, T1 m, S0 m hpr, hpk]
    lib_int
  constructor
  · intro c hc
    rw [gen c, S1 m hpr c, S2 m hpr c hc]; lib_int
  · rw [gen (ring (2 * m + 1 + 1)).zero, S1 m hpr (ring (2 * m + 1 + 1)).zero, S3 m hpr, chi_zero]; lib_int

/-- (A, B, C) along a coefficient list. -/
def Sf : List (Zp (2 * m + 1 + 1)) → Int × Int × Int
  | [] => (0, 0, 1)
  | a :: as => ((Int.ofNat (2 * m + 1 + 1 + 1)) * (Sf as).1 + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sf as).2.1) + (Sf as).2.2 + -(Sf as).1, chi m a * ((Sf as).2.2 + -(Sf as).1),
                (Int.ofNat (2 * m + 1 + 1 + 1)) * (Sf as).1 + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sf as).2.1 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + (Sf as).2.2 + -(Sf as).1)

/-- **Every nondegenerate diagonal quadratic form over 𝔽_p**: #{Σ a_i x_i² = c} = A + Bχ(c) (c ≠ 0), C (c = 0). -/
theorem diag_form (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∀ as : List (Zp (2 * m + 1 + 1)), (∀ a, memL a as → a ≠ (ring (2 * m + 1 + 1)).zero) →
    (∀ c, c ≠ (ring (2 * m + 1 + 1)).zero → Nf m as c = (Sf m as).1 + (Sf m as).2.1 * chi m c) ∧ Nf m as (ring (2 * m + 1 + 1)).zero = (Sf m as).2.2
  | [], _ => by
    constructor
    · intro c hc; show delta LibInt.intR c (ring (2 * m + 1 + 1)).zero = 0 + 0 * chi m c; rw [show delta LibInt.intR c (ring (2 * m + 1 + 1)).zero = 0 from ite_neg' _ _ hc]; lib_int
    · show delta LibInt.intR (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero = 1; unfold delta; exact ite_pos' _ _ rfl
  | a :: as, h => by
    have ih := diag_form hpr as (fun b hb => h b (Or.inr hb))
    have st := step_a m hpr a (h a (Or.inl rfl)) as _ _ _ ih.1 ih.2
    exact st

/-! ## closed forms (Gauss–Lebesgue–Jordan) -/

theorem Sq_succ (k : Nat) : Sq m (k + 1) = ((Int.ofNat (2 * m + 1 + 1 + 1)) * (Sq m k).1 + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sq m k).2.1) + (Sq m k).2.2 + -(Sq m k).1, (Sq m k).2.2 + -(Sq m k).1,
    (Int.ofNat (2 * m + 1 + 1 + 1)) * (Sq m k).1 + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sq m k).2.1 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + (Sq m k).2.2 + -(Sq m k).1) := rfl

theorem ee (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = 1 := by
  rcases e_val m hpr with h | h <;> rw [h] <;> rfl

/-- With Q = p^j and E = χ(−1)^j:  Sq(2j+1) = (Q², EQ, Q²),  Sq(2j+2) = (Q²p − Eχ(−1)Q, 0, Q²p + Eχ(−1)(Qp − Q)). -/
theorem closed (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∀ j : Nat,
    Sq m (2 * j + 1) = (pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j, pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j, pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j) ∧
    Sq m (2 * j + 2) = (pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j), 0,
      pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) + pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(pw LibInt.intR (Int.o  -- (+30 chars; full line in the Zenodo source)
  | 0 => by
    have h1 : Sq m (2 * 0 + 1) = (1, 1, 1) := by
      show Sq m (0 + 1) = _; rw [Sq_succ]; show ((Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0) + 1 + -0, 1 + -0, (Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + 1 + -0) = _
      apply Prod.ext; · show (Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0) + 1 + -0 = 1; lib_int
      apply Prod.ext; · show (1 : Int) + -0 = 1; lib_int
      · show (Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + 1 + -0 = 1; lib_int
    refine ⟨by rw [h1]; show ((1 : Int), (1 : Int), (1 : Int)) = (1 * 1, 1 * 1, 1 * 1); rfl, ?_⟩
    show Sq m (2 * 0 + 1 + 1) = _
    rw [Sq_succ, h1]
    show _ = (1 * 1 * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(1 * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 1), 0, 1 * 1 * (Int.ofNat (2 * m + 1 + 1 + 1)) + 1 * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (1 * (Int.ofNat (2 * m + 1 + 1 + 1)) + -1))
    apply Prod.ext; · show (Int.ofNat (2 * m + 1 + 1 + 1)) * 1 + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 1) + 1 + -1 = 1 * 1 * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(1 * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 1); lib_int
    apply Prod.ext; · show (1 : Int) + -1 = 0; rfl
    · show (Int.ofNat (2 * m + 1 + 1 + 1)) * 1 + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 1 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + 1 + -1 = 1 * 1 * (Int.ofNat (2 * m + 1 + 1 + 1)) + 1 * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (1 * (Int.ofNat (2 * m + 1 + 1 + 1)) + -1); lib_int
  | j + 1 => by
    have ih := closed hpr j
    have he := ee m hpr
    have hQ : pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) (j + 1) = pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) := rfl
    have hE : pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (j + 1) = pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := rfl
    rw [hQ, hE]
    generalize pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j = Q at ih ⊢
    generalize pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j = E at ih ⊢
    have odd : Sq m (2 * (j + 1) + 1) = (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1))), E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1))), Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)))) := by
      rw [show 2 * (j + 1) + 1 = (2 * j + 2) + 1 by lib_nat, Sq_succ, ih.2]
      apply Prod.ext; · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * Q)) + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0) + (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 * m + 1 + 1  -- (+222 chars; full line in the Zenodo source)
      apply Prod.ext; · show (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -Q)) + -(Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * Q)) = E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 *  -- (+26 chars; full line in the Zenodo source)
      · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * Q)) + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (In  -- (+244 chars; full line in the Zenodo source)
    refine ⟨odd, ?_⟩
    rw [show 2 * (j + 1) + 2 = (2 * (j + 1) + 1) + 1 by lib_nat, Sq_succ, odd]
    apply Prod.ext
    · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)))) + -((chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1))))) + Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1))) + -(Q * (Int.ofNat  -- (+353 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1))) + -(Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)))) = 0; lib_int
    · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)))) + (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)))) * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2  -- (+100 chars; full line in the Zenodo source)
        = Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1))) * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(Q * (Int.ofNat (2 * m + 1 + 1 + 1))))
      lib_int

/-! ## closed forms for every diagonal form -/

def dprod : List (Zp (2 * m + 1 + 1)) → Int
  | [] => 1
  | a :: as => chi m a * dprod as

theorem Sf_cons (a : Zp (2 * m + 1 + 1)) (as : List (Zp (2 * m + 1 + 1))) : Sf m (a :: as) =
    ((Int.ofNat (2 * m + 1 + 1 + 1)) * (Sf m as).1 + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sf m as).2.1) + (Sf m as).2.2 + -(Sf m as).1, chi m a * ((Sf m as).2.2 + -(Sf m as).1),
     (Int.ofNat (2 * m + 1 + 1 + 1)) * (Sf m as).1 + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (Sf m as).2.1 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + (Sf m as).2.2 + -(Sf m as).1) := rfl

/-- **Closed forms for Σ a_i x_i²** with d = Π (a_i/p): length 2j+1 gives (p^{2j}, e^j d p^j, p^{2j}); length 2j+2 gives
    (p^{2j+1} − e^{j+1} d p^j, 0, p^{2j+1} + e^{j+1} d (p^{j+1} − p^j)). -/
theorem form_closed : ∀ as : List (Zp (2 * m + 1 + 1)),
    (∀ j, as.length = 2 * j + 1 → Sf m as = (pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j, pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * dprod m as * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j, pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j)) ∧
    (∀ j, as.length = 2 * j + 2 → Sf m as = (pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * dprod m as * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j), 0,
      pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) + pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * dprod m as * (pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(pw LibIn  -- (+44 chars; full line in the Zenodo source)
  | [] => ⟨fun j h => Nat.noConfusion h, fun j h => Nat.noConfusion h⟩
  | a :: as => by
    have ih := form_closed as
    constructor
    · intro j hj
      have hl : as.length = 2 * j := Nat.succ.inj hj
      cases j with
      | zero =>
        have he : as = [] := by
          cases as with
          | nil => rfl
          | cons b bs => exact Nat.noConfusion hl
        rw [he]
        show Sf m [a] = _
        rw [Sf_cons]
        show ((Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0) + 1 + -0, chi m a * (1 + -0), (Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + 1 + -0) = (1 * 1, 1 * (chi m a * 1) * 1, 1 * 1)
        apply Prod.ext; · show (Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0) + 1 + -0 = 1 * 1; lib_int
        apply Prod.ext; · show chi m a * (1 + -0) = 1 * (chi m a * 1) * 1; lib_int
        · show (Int.ofNat (2 * m + 1 + 1 + 1)) * 0 + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + 1 + -0 = 1 * 1; lib_int
      | succ i =>
        have h2 := ih.2 i (by rw [hl]; lib_nat)
        rw [Sf_cons, h2]
        have hQ : pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) (i + 1) = pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) i * (Int.ofNat (2 * m + 1 + 1 + 1)) := rfl
        have hE : pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (i + 1) = pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) i * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := rfl
        rw [hQ, hE, show dprod m (a :: as) = chi m a * dprod m as from rfl]
        generalize pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) i = Q
        generalize pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) i = E
        generalize dprod m as = d
        apply Prod.ext
        · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * d * Q)) + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0) + (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * d * (Q * (Int.ofNat (2 * m + 1  -- (+221 chars; full line in the Zenodo source)
          lib_int
        apply Prod.ext
        · show chi m a * ((Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * d * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -Q)) + -(Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * d * Q))) = E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (chi m a * d)   -- (+39 chars; full line in the Zenodo source)
          lib_int
        · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * d * Q)) + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0 * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + (Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).  -- (+259 chars; full line in the Zenodo source)
          lib_int
    · intro j hj
      have h1 := ih.1 j (Nat.succ.inj hj)
      rw [Sf_cons, h1, show dprod m (a :: as) = chi m a * dprod m as from rfl]
      generalize pw LibInt.intR (Int.ofNat (2 * m + 1 + 1 + 1)) j = Q
      generalize pw LibInt.intR (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) j = E
      generalize dprod m as = d
      apply Prod.ext
      · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * Q) + -(chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (E * d * Q)) + Q * Q + -(Q * Q) = Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -(E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (chi m a * d) * Q); lib_int
      apply Prod.ext
      · show chi m a * (Q * Q + -(Q * Q)) = 0; lib_int
      · show (Int.ofNat (2 * m + 1 + 1 + 1)) * (Q * Q) + chi m a * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (E * d * Q) * ((Int.ofNat (2 * m + 1 + 1 + 1)) + -1) + Q * Q + -(Q * Q) = Q * Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + E * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (chi m a * d) * (Q * (Int.ofNat (2 * m + 1 + 1 + 1)) + -Q); lib_int

end QuadDiagonal
#print axioms QuadDiagonal.sI_add
#print axioms QuadDiagonal.sI_mul
#print axioms QuadDiagonal.sI_mulr
#print axioms QuadDiagonal.zsum
#print axioms QuadDiagonal.sq_root
#print axioms QuadDiagonal.two_ne
#print axioms QuadDiagonal.nozd'
#print axioms QuadDiagonal.two_roots
#print axioms QuadDiagonal.sq_count
#print axioms QuadDiagonal.hcomp
#print axioms QuadDiagonal.conv
#print axioms QuadDiagonal.e_val
#print axioms QuadDiagonal.T1
#print axioms QuadDiagonal.S0
#print axioms QuadDiagonal.S1
#print axioms QuadDiagonal.S2
#print axioms QuadDiagonal.sqchi
#print axioms QuadDiagonal.S3
#print axioms QuadDiagonal.Psum
#print axioms QuadDiagonal.step
#print axioms QuadDiagonal.quad_diagonal
#print axioms QuadDiagonal.chi_sq_val
#print axioms QuadDiagonal.conv_a
#print axioms QuadDiagonal.step_a
#print axioms QuadDiagonal.diag_form
#print axioms QuadDiagonal.Sq_succ
#print axioms QuadDiagonal.ee
#print axioms QuadDiagonal.closed
#print axioms QuadDiagonal.Sf_cons
#print axioms QuadDiagonal.form_closed

#print axioms: 30 theorems, no axioms.

78.82 Modularity check for the Fermat cubic

The coefficients of \(\eta (3z)^2\eta (9z)^2\) against the point counts of \(x^3+y^3=z^3\), by evaluation.

\[ a_p\big (\eta (3z)^2\eta (9z)^2\big )=p+1-\#X(\mathbb F_p)\qquad (p<200,\ p\ne 3). \]

lang/Fermat27a.lean

/-
  lang/Fermat27a — T43, cycle 271: modularity check for the Fermat cubic (the curve 27a, conductor 27).

  f = η(3z)²η(9z)² = q Π_{n≥1} (1 − q^{3n})² (1 − q^{9n})², the weight-2 newform of level 27.
  For every prime p < 200, p ≠ 3, the coefficient of q^p equals p + 1 − #X(𝔽_p) for the projective Fermat cubic
  X : x³ + y³ = z³ (affine points of x³ + y³ = 1 plus the points [x : 1 : 0] with x³ = −1):
      modular_27a     checked by evaluation (rfl), as ESChain.modular_check_fast does for 11a.
  Together with FermatCubic / CubicTwistCount (#X = p + 1 + Tr J for p ≡ 1 mod 3), this identifies the coefficients of f with
  −Tr J at those primes.
  Lean 4 core only, evaluation by rfl, no axioms.
-/
import ESChain

namespace Fermat27a

open FTAChain FrobChain QRChain ESChain

def affF (p : Nat) : Nat := sumTo p (fun x => sumTo p (fun y => b2n (Nat.beq ((x * x * x + y * y * y) % p) (1 % p))))
def infF (p : Nat) : Nat := sumTo p (fun x => b2n (Nat.beq ((x * x * x + 1) % p) 0))

/-- a_p = p + 1 − #X(𝔽_p). -/
def apF (p : Nat) : Int := Int.ofNat (p + 1) - Int.ofNat (affF p + infF p)

def prod3 : Nat → List Int → List Int
  | 0, l => l
  | n + 1, l => prod3 n (mul1m (3 * (n + 1)) (mul1m (3 * (n + 1)) l))
def prod9 : Nat → List Int → List Int
  | 0, l => l
  | n + 1, l => prod9 n (mul1m (9 * (n + 1)) (mul1m (9 * (n + 1)) l))

/-- coefficients of q⁰ … q^{N−1} of Π (1 − q^{3n})²(1 − q^{9n})². -/
def eta27 (N : Nat) : List Int := prod9 N (prod3 N (oneS N))

def coef27 (p : Nat) : Int := nth (eta27 200) (p - 1)

def primes200' : List Nat :=
  [2, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
   89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
   181, 191, 193, 197, 199]

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Modularity of the Fermat cubic, checked at every prime p < 200, p ≠ 3.** -/
theorem modular_27a : primes200'.map coef27 = primes200'.map apF := by rfl

end Fermat27a

#print axioms Fermat27a.modular_27a

#print axioms: 1 theorem, no axioms.

78.83 Modularity checks for 32a and 36a

The \(\eta \)-products of levels \(32\) and \(36\) against the point counts, by evaluation.

\[ a_p\big (\eta (4z)^2\eta (8z)^2\big )=p+1-\#\{y^2=x^3-x\},\qquad a_p\big (\eta (6z)^4\big )=p+1-\#\{y^2=x^3+1\}\qquad (p<200). \]

lang/CMModularCheck.lean

/-
  lang/CMModularCheck — T43, cycle 272: modularity checks for the CM curves 32a and 36a.

    32a  y² = x³ − x   ↔  η(4z)²η(8z)² = q Π (1 − q^{4n})²(1 − q^{8n})²     every prime 2 < p < 200
    36a  y² = x³ + 1   ↔  η(6z)⁴      = q Π (1 − q^{6n})⁴                    every prime 3 < p < 200
  The coefficient of q^p equals p + 1 − #E(𝔽_p) (projective count: affine solutions + 1), by evaluation (rfl).
  With CMModular / SignCM3 (32a) and CubicSign / CubeAp (36a), whose a_p are the Hecke-character traces, these are the
  "modularity" identities on both sides for p < 200.
  Lean 4 core only, evaluation by rfl, no axioms.
-/
import lang.Fermat27a

namespace CMModularCheck

open FTAChain FrobChain QRChain ESChain Fermat27a

def cntE (p a b : Nat) : Nat := sumTo p (fun x => sumTo p (fun y => b2n (Nat.beq ((y * y) % p) ((x * x * x + a * x + b) % p))))

/-- y² = x³ − x written as y² = x³ + (p − 1)x. -/
def ap32 (p : Nat) : Int := Int.ofNat (p + 1) - Int.ofNat (cntE p (p - 1) 0 + 1)
def ap36 (p : Nat) : Int := Int.ofNat (p + 1) - Int.ofNat (cntE p 0 1 + 1)

def prodK (k : Nat) : Nat → List Int → List Int
  | 0, l => l
  | n + 1, l => prodK k n (mul1m (k * (n + 1)) (mul1m (k * (n + 1)) l))

def eta32 (N : Nat) : List Int := prodK 8 N (prodK 4 N (oneS N))
def eta36 (N : Nat) : List Int := prodK 6 N (prodK 6 N (oneS N))

def coef32 (p : Nat) : Int := nth (eta32 200) (p - 1)
def coef36 (p : Nat) : Int := nth (eta36 200) (p - 1)

def odd200 : List Nat :=
  [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
   89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
   181, 191, 193, 197, 199]
def big200 : List Nat :=
  [5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
   89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
   181, 191, 193, 197, 199]

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **32a is modular at every odd prime p < 200.** -/
theorem modular_32a : odd200.map coef32 = odd200.map ap32 := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **36a is modular at every prime 3 < p < 200.** -/
theorem modular_36a : big200.map coef36 = big200.map ap36 := by rfl

end CMModularCheck

#print axioms CMModularCheck.modular_32a
#print axioms CMModularCheck.modular_36a

#print axioms: 2 theorems, no axioms.

78.84 Hecke theta series as \(\eta \)-products

The theta series of the Hecke characters of \(\mathbb Q(i)\) and \(\mathbb Q(\sqrt {-3})\) agree with the \(\eta \)-products of 32a, 27a, 36a to 199 terms.

\[ \eta (4z)^2\eta (8z)^2=\sum _{a+bi\ \mathrm {primary}}a\,q^{a^2+b^2},\quad \eta (3z)^2\eta (9z)^2=\sum _{x\equiv 1,\,y\equiv 0\,(3)}x\,q^{x^2-xy+y^2},\quad \eta (6z)^4=\sum _{x\equiv 1\,(3)}x\,q^{x^2+3y^2}. \]

lang/ThetaEta.lean

/-
  lang/ThetaEta — T43, cycle 273: Hecke theta series = η-products, coefficientwise for n < 200.

    32a   η(4z)²η(8z)²  = Σ_{α = a + bi primary} a q^{a² + b²}            (primary: a ≡ 1, b ≡ 0 or a ≡ 3, b ≡ 2 mod 4)
    27a   η(3z)²η(9z)²  = Σ_{x ≡ 1 (3), y ≡ 0 (3)} x q^{x² − xy + y²}
    36a   η(6z)⁴        = Σ_{x ≡ 1 (3)} x q^{x² + 3y²}
  All 199 coefficients of q¹ … q^{199} agree (evaluation, rfl). The left sides are the newforms of the curves 32a, 27a, 36a
  (CMModularCheck, Fermat27a); the right sides are the theta series of the Hecke characters of ℚ(i) and ℚ(√−3) — the
  automorphic induction of the Grössencharacter, i.e. the CM modularity identity as q-series, to 199 terms.
  Lean 4 core only, evaluation by rfl, no axioms.
-/
import lang.CMModularCheck

namespace ThetaEta

open FTAChain FrobChain QRChain ESChain Fermat27a CMModularCheck

/-- the integers −k … k (boxes large enough for every representation of n < 200). -/
def rng (k : Nat) : List Int := (List.range (2 * k + 1)).map (fun i => Int.ofNat i - Int.ofNat k)

def sumI (l : List Int) (f : Int → Int) : Int := l.foldr (fun x acc => f x + acc) 0

def ind (b : Bool) (v : Int) : Int := if b then v else 0

/-- Boolean equality of integers by cases (the `==` of `DecidableEq Int` brings propext, choice and Quot.sound). -/
def ieq (a b : Int) : Bool :=
  match a with
  | Int.ofNat x => (match b with
    | Int.ofNat y => Nat.beq x y
    | Int.negSucc _ => false)
  | Int.negSucc x => (match b with
    | Int.ofNat _ => false
    | Int.negSucc y => Nat.beq x y)

def m4 (x : Int) : Int := x % 4
def m3 (x : Int) : Int := x % 3

def theta32 (n : Nat) : Int := sumI (rng 14) (fun a => sumI (rng 14) (fun b =>
  ind (ieq (a * a + b * b) (Int.ofNat n) && ((ieq (m4 a) 1 && ieq (m4 b) 0) || (ieq (m4 a) 3 && ieq (m4 b) 2))) a))

def theta27 (n : Nat) : Int := sumI (rng 16) (fun x => sumI (rng 16) (fun y =>
  ind (ieq (x * x - x * y + y * y) (Int.ofNat n) && ieq (m3 x) 1 && ieq (m3 y) 0) x))

def theta36 (n : Nat) : Int := sumI (rng 14) (fun x => sumI (rng 8) (fun y =>
  ind (ieq (x * x + 3 * (y * y)) (Int.ofNat n) && ieq (m3 x) 1) x))

def idx : List Nat := (List.range 199).map (· + 1)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem theta_32a : idx.map (fun n => nth (eta32 200) (n - 1)) = idx.map theta32 := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem theta_27a : idx.map (fun n => nth (eta27 200) (n - 1)) = idx.map theta27 := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem theta_36a : idx.map (fun n => nth (eta36 200) (n - 1)) = idx.map theta36 := by rfl

end ThetaEta

#print axioms ThetaEta.theta_32a
#print axioms ThetaEta.theta_27a
#print axioms ThetaEta.theta_36a

#print axioms: 3 theorems, no axioms.

78.85 Hecke relations for the CM \(\eta \)-products

The coefficients of the \(\eta \)-products of 32a, 27a, 36a satisfy the Hecke relations for \(n<200\).

\[ a(mn)=a(m)a(n)\ (\gcd (m,n)=1),\qquad a(p^2)=a(p)^2-p,\qquad a(p^3)=a(p)a(p^2)-p\,a(p). \]

lang/HeckeCheck.lean

/-
  lang/HeckeCheck — T43, cycle 331: the η-products of 32a, 27a, 36a are Hecke eigenforms, coefficientwise for n < 200.

  For f = η(4z)²η(8z)², η(3z)²η(9z)², η(6z)⁴ with coefficients a(n) (n < 200):
    cNN_eq      the coefficient list equals an explicit literal list (one evaluation of the η-product, rfl)
    multNN      a(mn) = a(m)a(n) for every coprime pair m, n ≥ 2 with mn < 200
    heckeNN     a(p²) = a(p)² − p for every good prime p ≤ 13
    heckeNN_3   a(p³) = a(p)a(p²) − p·a(p) for the good primes with p³ < 200
  These are the Hecke relations T_m T_n = T_mn and T_p² = T_p² − p of a weight-2 newform with trivial character.
  Lean 4 core only, evaluation by rfl, no axioms. (Kamiak: the cNN_eq evaluations are slow.)
-/
import lang.CMModularCheck

namespace HeckeCheck

open FTAChain FrobChain QRChain ESChain Fermat27a CMModularCheck

def get (l : List Int) (n : Nat) : Int := nth l (n - 1)

def c32 : List Int := [1,0,0,0,-2,0,0,0,-3,0,0,0,6,0,0,0,2,0,0,0,0,0,0,0,-1,0,0,0,-10,0,0,0,0,0,0,0,-2,0,0,0,10,0,0,0,6,0,0,0,-7,0,0,0,14,0,0,0,0,0,0,0,-10,0,0,0,-12,0,0,0,0,0,0,0,-6,0,0,0,0,0,0,0,9,0,0,0,-4,0,0,0,10,0,0,0,0,0,0,0,18,0,0,0,-2,0,0,0,0,0,0,0,6,0,0,0,-14,0,0,0,-18,0,0,0,-11,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0,-22,0,0,0,0,0,0,0,20,0,0,0,14,0,0,0,-6,0,0,0,22,0,0,0,0,0,0,0,0,0,0,0,23,0,0,0,-  -- (+60 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem c32_eq : (List.range 199).map (fun n => nth (eta32 200) n) = c32 := by rfl

def pairs32 : List (Nat × Nat) := [(2, 3), (2, 5), (2, 7), (2, 9), (2, 11), (2, 13), (2, 15), (2, 17), (2, 19), (2, 21), (2, 23), (2, 25), (2, 27), (2, 29), (2, 31), (2, 33), (2, 35), (2, 37), (2, 39), (2, 41), (2, 43), (2, 45), (2, 47), (2, 49), (2, 51), (2, 53), (2, 55), (2, 57), (2, 59), (2, 61), (2, 63), (2, 65), (2, 67), (2, 69), (2, 71), (2, 73), (2, 75), (2, 77), (2, 79), (2, 81), (2, 83),   -- (+1429 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Multiplicativity**: a(mn) = a(m)a(n) for coprime m, n, mn < 200. -/
theorem mult32 : pairs32.map (fun q => get c32 (q.1 * q.2)) = pairs32.map (fun q => get c32 q.1 * get c32 q.2) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Hecke at p²**: a(p²) = a(p)² − p for the good primes p ≤ 13. -/
theorem hecke32 : ([3, 5, 7, 11, 13] : List Nat).map (fun p => get c32 (p * p))
    = ([3, 5, 7, 11, 13] : List Nat).map (fun p => get c32 p * get c32 p - Int.ofNat p) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Hecke at p³**: a(p³) = a(p)a(p²) − p·a(p) for the good primes with p³ < 200. -/
theorem hecke32_3 : ([3, 5] : List Nat).map (fun p => get c32 (p * p * p))
    = ([3, 5] : List Nat).map (fun p => get c32 p * get c32 (p * p) - Int.ofNat p * get c32 p) := by rfl

def c27 : List Int := [1,0,0,-2,0,0,-1,0,0,0,0,0,5,0,0,4,0,0,-7,0,0,0,0,0,-5,0,0,2,0,0,-4,0,0,0,0,0,11,0,0,0,0,0,8,0,0,0,0,0,-6,0,0,-10,0,0,0,0,0,0,0,0,-1,0,0,-8,0,0,5,0,0,0,0,0,-7,0,0,14,0,0,17,0,0,0,0,0,0,0,0,0,0,0,-5,0,0,0,0,0,-19,0,0,10,0,0,-13,0,0,0,0,0,2,0,0,-4,0,0,0,0,0,0,0,0,-11,0,0,8,0,0,20,0,0,0,0,0,7,0,0,0,0,0,23,0,0,0,0,0,0,0,0,-22,0,0,-19,0,0,0,0,0,14,0,0,0,0,0,-25,0,0,0,0,0,12,0,0,-1  -- (+60 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem c27_eq : (List.range 199).map (fun n => nth (eta27 200) n) = c27 := by rfl

def pairs27 : List (Nat × Nat) := [(2, 3), (2, 5), (2, 7), (2, 9), (2, 11), (2, 13), (2, 15), (2, 17), (2, 19), (2, 21), (2, 23), (2, 25), (2, 27), (2, 29), (2, 31), (2, 33), (2, 35), (2, 37), (2, 39), (2, 41), (2, 43), (2, 45), (2, 47), (2, 49), (2, 51), (2, 53), (2, 55), (2, 57), (2, 59), (2, 61), (2, 63), (2, 65), (2, 67), (2, 69), (2, 71), (2, 73), (2, 75), (2, 77), (2, 79), (2, 81), (2, 83),   -- (+1429 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Multiplicativity**: a(mn) = a(m)a(n) for coprime m, n, mn < 200. -/
theorem mult27 : pairs27.map (fun q => get c27 (q.1 * q.2)) = pairs27.map (fun q => get c27 q.1 * get c27 q.2) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Hecke at p²**: a(p²) = a(p)² − p for the good primes p ≤ 13. -/
theorem hecke27 : ([2, 5, 7, 11, 13] : List Nat).map (fun p => get c27 (p * p))
    = ([2, 5, 7, 11, 13] : List Nat).map (fun p => get c27 p * get c27 p - Int.ofNat p) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Hecke at p³**: a(p³) = a(p)a(p²) − p·a(p) for the good primes with p³ < 200. -/
theorem hecke27_3 : ([2, 5] : List Nat).map (fun p => get c27 (p * p * p))
    = ([2, 5] : List Nat).map (fun p => get c27 p * get c27 (p * p) - Int.ofNat p * get c27 p) := by rfl

def c36 : List Int := [1,0,0,0,0,0,-4,0,0,0,0,0,2,0,0,0,0,0,8,0,0,0,0,0,-5,0,0,0,0,0,-4,0,0,0,0,0,-10,0,0,0,0,0,8,0,0,0,0,0,9,0,0,0,0,0,0,0,0,0,0,0,14,0,0,0,0,0,-16,0,0,0,0,0,-10,0,0,0,0,0,-4,0,0,0,0,0,0,0,0,0,0,0,-8,0,0,0,0,0,14,0,0,0,0,0,20,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,-11,0,0,0,0,0,20,0,0,0,0,0,-32,0,0,0,0,0,-16,0,0,0,0,0,0,0,0,0,0,0,-4,0,0,0,0,0,14,0,0,0,0,0,8,0,0,0,0,0,-9,0,0,0,0,0,20,0,  -- (+49 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem c36_eq : (List.range 199).map (fun n => nth (eta36 200) n) = c36 := by rfl

def pairs36 : List (Nat × Nat) := [(2, 3), (2, 5), (2, 7), (2, 9), (2, 11), (2, 13), (2, 15), (2, 17), (2, 19), (2, 21), (2, 23), (2, 25), (2, 27), (2, 29), (2, 31), (2, 33), (2, 35), (2, 37), (2, 39), (2, 41), (2, 43), (2, 45), (2, 47), (2, 49), (2, 51), (2, 53), (2, 55), (2, 57), (2, 59), (2, 61), (2, 63), (2, 65), (2, 67), (2, 69), (2, 71), (2, 73), (2, 75), (2, 77), (2, 79), (2, 81), (2, 83),   -- (+1429 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Multiplicativity**: a(mn) = a(m)a(n) for coprime m, n, mn < 200. -/
theorem mult36 : pairs36.map (fun q => get c36 (q.1 * q.2)) = pairs36.map (fun q => get c36 q.1 * get c36 q.2) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Hecke at p²**: a(p²) = a(p)² − p for the good primes p ≤ 13. -/
theorem hecke36 : ([5, 7, 11, 13] : List Nat).map (fun p => get c36 (p * p))
    = ([5, 7, 11, 13] : List Nat).map (fun p => get c36 p * get c36 p - Int.ofNat p) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
/-- **Hecke at p³**: a(p³) = a(p)a(p²) − p·a(p) for the good primes with p³ < 200. -/
theorem hecke36_3 : ([5] : List Nat).map (fun p => get c36 (p * p * p))
    = ([5] : List Nat).map (fun p => get c36 p * get c36 (p * p) - Int.ofNat p * get c36 p) := by rfl

end HeckeCheck

#print axioms HeckeCheck.c32_eq
#print axioms HeckeCheck.mult32
#print axioms HeckeCheck.hecke32
#print axioms HeckeCheck.hecke32_3
#print axioms HeckeCheck.c27_eq
#print axioms HeckeCheck.mult27
#print axioms HeckeCheck.hecke27
#print axioms HeckeCheck.hecke27_3
#print axioms HeckeCheck.c36_eq
#print axioms HeckeCheck.mult36
#print axioms HeckeCheck.hecke36
#print axioms HeckeCheck.hecke36_3

#print axioms: 12 theorems, no axioms.

78.86 Hecke relations for 11a

The coefficients of \(\eta (z)^2\eta (11z)^2\) satisfy the Hecke relations for \(n<200\) (the first non-CM case).

\[ a(mn)=a(m)a(n),\quad a(p^2)=a(p)^2-p,\quad a(p^3)=a(p)a(p^2)-p\,a(p),\quad a(121)=a(11)^2. \]

lang/HeckeCheck11.lean

/-
  lang/HeckeCheck11 — T43, cycle 348: the η-product of 11a is a Hecke eigenform, coefficientwise for n < 200 (the first non-CM case).

  f = η(z)²η(11z)² = q Π (1 − qⁿ)²(1 − q¹¹ⁿ)² with coefficients a(n), n < 200 (ESChain.etaProd):
    c11_eq     the coefficient list equals an explicit literal list (one evaluation, rfl)
    mult11     a(mn) = a(m)a(n) for all 200 coprime pairs m, n ≥ 2, mn < 200
    hecke11    a(p²) = a(p)² − p for the good primes p ≤ 13
    hecke11_3  a(p³) = a(p)a(p²) − p·a(p) for p = 2, 3, 5
    bad11      a(121) = a(11)² (the bad prime: a(11ᵏ) = a(11)ᵏ, here a(11) = 1)
  With ESChain.modular_check_fast (a(p) = p + 1 − #E(𝔽_p)) these are the Hecke relations of the newform attached to 11a.
  Lean 4 core only, evaluation by rfl, no axioms. (Kamiak: c11_eq is slow.)
-/
import ESChain

namespace HeckeCheck11

open FTAChain FrobChain QRChain ESChain

def get (l : List Int) (n : Nat) : Int := nth l (n - 1)

def c11 : List Int := [1,-2,-1,2,1,2,-2,0,-2,-2,1,-2,4,4,-1,-4,-2,4,0,2,2,-2,-1,0,-4,-8,5,-4,0,2,7,8,-1,4,-2,-4,3,0,-4,0,-8,-4,-6,2,-2,2,8,4,-3,8,2,8,-6,-10,1,0,0,0,5,-2,12,-14,4,-8,4,2,-7,-4,1,4,-3,0,4,-6,4,0,-2,8,-10,-4,1,16,-6,4,-2,12,0,0,15,4,-8,-2,-7,-16,0,-8,-7,6,-2,-8,2,-4,-16,0,2,12,18,10,10,-2,-3,8,9,0,-1,0,-8,-10,4,0,1,-24,8,14,-9,-8,8,0,6,-8,-18,-2,0,14,5,0,-7,-2,10,-4,-8,6,4,8,0,-8,3,6  -- (+139 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem c11_eq : (List.range 199).map (fun n => nth (etaProd 200) n) = c11 := by rfl

def pairs11 : List (Nat × Nat) := [(2, 3), (2, 5), (2, 7), (2, 9), (2, 11), (2, 13), (2, 15), (2, 17), (2, 19), (2, 21), (2, 23), (2, 25), (2, 27), (2, 29), (2, 31), (2, 33), (2, 35), (2, 37), (2, 39), (2, 41), (2, 43), (2, 45), (2, 47), (2, 49), (2, 51), (2, 53), (2, 55), (2, 57), (2, 59), (2, 61), (2, 63), (2, 65), (2, 67), (2, 69), (2, 71), (2, 73), (2, 75), (2, 77), (2, 79), (2, 81), (2, 83),   -- (+1429 chars; full line in the Zenodo source)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem mult11 : pairs11.map (fun q => get c11 (q.1 * q.2)) = pairs11.map (fun q => get c11 q.1 * get c11 q.2) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem hecke11 : ([2, 3, 5, 7, 13] : List Nat).map (fun p => get c11 (p * p))
    = ([2, 3, 5, 7, 13] : List Nat).map (fun p => get c11 p * get c11 p - Int.ofNat p) := by rfl

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem hecke11_3 : ([2, 3, 5] : List Nat).map (fun p => get c11 (p * p * p))
    = ([2, 3, 5] : List Nat).map (fun p => get c11 p * get c11 (p * p) - Int.ofNat p * get c11 p) := by rfl

theorem bad11 : get c11 121 = get c11 11 * get c11 11 := by rfl

end HeckeCheck11

#print axioms HeckeCheck11.c11_eq
#print axioms HeckeCheck11.mult11
#print axioms HeckeCheck11.hecke11
#print axioms HeckeCheck11.hecke11_3
#print axioms HeckeCheck11.bad11

#print axioms: 5 theorems, no axioms.

78.87 The Ramanujan bound for 11a

For every prime \(p<200\) the coefficient \(a(p)\) of \(\eta (z)^2\eta (11z)^2\) satisfies \(a(p)^2\le 4p\).

\[ 4p-a(p)^2\in \mathbb N\qquad (p<200\ \text {prime}). \]

lang/Ramanujan11.lean

/-
  lang/Ramanujan11 — T43, cycle 350: the Ramanujan–Hasse bound for 11a at every prime p < 200.

  For the coefficients a(p) of η(z)²η(11z)² (the literal list c11 = HeckeCheck11.c11, pinned to the η-product by HeckeCheck11.c11_eq):
    ramanujan11   4p − a(p)² = k_p with k_p ∈ ℕ for every prime p < 200 (so |a(p)| ≤ 2√p)
    ramanujanNN   the same for the CM forms 32a, 27a, 36a at their good primes (lists from HeckeCheck)
    recursionNN   a(p^(k+1)) = a(p)a(p^k) − p·a(p^(k−1)) at every good prime power < 200, for 11a, 32a, 27a, 36a
    cong5_11      1 + p − a(p) ≡ 0 (mod 5) for 11a, every prime p ≠ 11 below 200
  (Hasse's theorem for E = 11a; with ESChain.modular_check_fast the same numbers are p + 1 − #E(𝔽_p).)
  Lean 4 core only, evaluation by rfl, no axioms.
-/
import ESChain

namespace Ramanujan11

open ESChain

/-- the literal coefficient list of η(z)²η(11z)² (identical to HeckeCheck11.c11, which is pinned to ESChain.etaProd by c11_eq). -/
def c11 : List Int := [1,-2,-1,2,1,2,-2,0,-2,-2,1,-2,4,4,-1,-4,-2,4,0,2,2,-2,-1,0,-4,-8,5,-4,0,2,7,8,-1,4,-2,-4,3,0,-4,0,-8,-4,-6,2,-2,2,8,4,-3,8,2,8,-6,-10,1,0,0,0,5,-2,12,-14,4,-8,4,2,-7,-4,1,4,-3,0,4,-6,4,0,-2,8,-10,-4,1,16,-6,4,-2,12,0,0,15,4,-8,-2,-7,-16,0,-8,-7,6,-2,-8,2,-4,-16,0,2,12,18,10,10,-2,-3,8,9,0,-1,0,-8,-10,4,0,1,-24,8,14,-9,-8,8,0,6,-8,-18,-2,0,14,5,0,-7,-2,10,-4,-8,6,4,8,0,-8,3,6  -- (+139 chars; full line in the Zenodo source)

def get (l : List Int) (n : Nat) : Int := nth l (n - 1)

def primes : List Nat := [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]

/-- **|a(p)| ≤ 2√p for p < 200**: each 4p − a(p)² is the image of a natural number. -/
theorem ramanujan11 : primes.map (fun p => 4 * Int.ofNat p - get c11 p * get c11 p)
    = ([4, 11, 19, 24, 43, 36, 64, 76, 91, 116, 75, 139, 100, 136, 124, 176, 211, 100, 219, 275, 276, 216, 296, 131, 339, 400, 156, 104, 336, 371, 444, 200, 499, 456, 496, 600, 579, 636, 524, 656, 491, 675, 475, 756, 784, 796] : List Nat).map Int.ofNat := by rfl

/-- the literal coefficient list of the 32a η-product (identical to HeckeCheck.c32). -/
def c32 : List Int := [1,0,0,0,-2,0,0,0,-3,0,0,0,6,0,0,0,2,0,0,0,0,0,0,0,-1,0,0,0,-10,0,0,0,0,0,0,0,-2,0,0,0,10,0,0,0,6,0,0,0,-7,0,0,0,14,0,0,0,0,0,0,0,-10,0,0,0,-12,0,0,0,0,0,0,0,-6,0,0,0,0,0,0,0,9,0,0,0,-4,0,0,0,10,0,0,0,0,0,0,0,18,0,0,0,-2,0,0,0,0,0,0,0,6,0,0,0,-14,0,0,0,-18,0,0,0,-11,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0,-22,0,0,0,0,0,0,0,20,0,0,0,14,0,0,0,-6,0,0,0,22,0,0,0,0,0,0,0,0,0,0,0,23,0,0,0,-  -- (+60 chars; full line in the Zenodo source)

/-- **|a(p)| ≤ 2√p for 32a, every good prime p < 200.** -/
theorem ramanujan32 : ([3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199] : List Nat).map (fun p => 4 * Int.ofNat p - get c32 p * get c32 p)
    = ([12, 16, 28, 44, 16, 64, 76, 92, 16, 124, 144, 64, 172, 188, 16, 236, 144, 268, 284, 256, 316, 332, 256, 64, 400, 412, 428, 400, 256, 508, 524, 64, 556, 400, 604, 144, 652, 668, 16, 716, 400, 764, 576, 784, 796] : List Nat).map Int.ofNat := by rfl

/-- the literal coefficient list of the 27a η-product (identical to HeckeCheck.c27). -/
def c27 : List Int := [1,0,0,-2,0,0,-1,0,0,0,0,0,5,0,0,4,0,0,-7,0,0,0,0,0,-5,0,0,2,0,0,-4,0,0,0,0,0,11,0,0,0,0,0,8,0,0,0,0,0,-6,0,0,-10,0,0,0,0,0,0,0,0,-1,0,0,-8,0,0,5,0,0,0,0,0,-7,0,0,14,0,0,17,0,0,0,0,0,0,0,0,0,0,0,-5,0,0,0,0,0,-19,0,0,10,0,0,-13,0,0,0,0,0,2,0,0,-4,0,0,0,0,0,0,0,0,-11,0,0,8,0,0,20,0,0,0,0,0,7,0,0,0,0,0,23,0,0,0,0,0,0,0,0,-22,0,0,-19,0,0,0,0,0,14,0,0,0,0,0,-25,0,0,0,0,0,12,0,0,-1  -- (+60 chars; full line in the Zenodo source)

/-- **|a(p)| ≤ 2√p for 27a, every good prime p < 200.** -/
theorem ramanujan27 : ([2, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199] : List Nat).map (fun p => 4 * Int.ofNat p - get c27 p * get c27 p)
    = ([8, 20, 27, 44, 27, 68, 27, 92, 116, 108, 27, 164, 108, 188, 212, 236, 243, 243, 284, 243, 27, 332, 356, 27, 404, 243, 428, 432, 452, 108, 524, 548, 27, 596, 243, 432, 27, 668, 692, 716, 675, 764, 243, 788, 675] : List Nat).map Int.ofNat := by rfl

/-- the literal coefficient list of the 36a η-product (identical to HeckeCheck.c36). -/
def c36 : List Int := [1,0,0,0,0,0,-4,0,0,0,0,0,2,0,0,0,0,0,8,0,0,0,0,0,-5,0,0,0,0,0,-4,0,0,0,0,0,-10,0,0,0,0,0,8,0,0,0,0,0,9,0,0,0,0,0,0,0,0,0,0,0,14,0,0,0,0,0,-16,0,0,0,0,0,-10,0,0,0,0,0,-4,0,0,0,0,0,0,0,0,0,0,0,-8,0,0,0,0,0,14,0,0,0,0,0,20,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,-11,0,0,0,0,0,20,0,0,0,0,0,-32,0,0,0,0,0,-16,0,0,0,0,0,0,0,0,0,0,0,-4,0,0,0,0,0,14,0,0,0,0,0,8,0,0,0,0,0,-9,0,0,0,0,0,20,0,  -- (+49 chars; full line in the Zenodo source)

/-- **|a(p)| ≤ 2√p for 36a, every good prime p < 200.** -/
theorem ramanujan36 : ([5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199] : List Nat).map (fun p => 4 * Int.ofNat p - get c36 p * get c36 p)
    = ([20, 12, 44, 48, 68, 12, 92, 116, 108, 48, 164, 108, 188, 212, 236, 48, 12, 284, 192, 300, 332, 356, 192, 404, 12, 428, 432, 452, 108, 524, 548, 300, 596, 588, 432, 588, 668, 692, 716, 48, 764, 768, 788, 12] : List Nat).map Int.ofNat := by rfl

/-- **Hecke recursion for 11a**: a(p^(k+1)) = a(p)a(p^k) − p·a(p^(k−1)) at every good prime power p^(k+1) < 200 (a(1) = 1). -/
theorem recursion11 : ([(2, 2, 4, 1), (2, 4, 8, 2), (2, 8, 16, 4), (2, 16, 32, 8), (2, 32, 64, 16), (2, 64, 128, 32), (3, 3, 9, 1), (3, 9, 27, 3), (3, 27, 81, 9), (5, 5, 25, 1), (5, 25, 125, 5), (7, 7, 49, 1), (13, 13, 169, 1)] : List (Nat × Nat × Nat × Nat)).map (fun t => get c11 t.2.2.1)
    = ([(2, 2, 4, 1), (2, 4, 8, 2), (2, 8, 16, 4), (2, 16, 32, 8), (2, 32, 64, 16), (2, 64, 128, 32), (3, 3, 9, 1), (3, 9, 27, 3), (3, 27, 81, 9), (5, 5, 25, 1), (5, 25, 125, 5), (7, 7, 49, 1), (13, 13, 169, 1)] : List (Nat × Nat × Nat × Nat)).map (fun t => get c11 t.1 * get c11 t.2.1 - Int.ofNat t.1 * get c11 t.2.2.2) := by rfl

/-- **Hecke recursion for 32a**: a(p^(k+1)) = a(p)a(p^k) − p·a(p^(k−1)) at every good prime power p^(k+1) < 200 (a(1) = 1). -/
theorem recursion32 : ([(3, 3, 9, 1), (3, 9, 27, 3), (3, 27, 81, 9), (5, 5, 25, 1), (5, 25, 125, 5), (7, 7, 49, 1), (11, 11, 121, 1), (13, 13, 169, 1)] : List (Nat × Nat × Nat × Nat)).map (fun t => get c32 t.2.2.1)
    = ([(3, 3, 9, 1), (3, 9, 27, 3), (3, 27, 81, 9), (5, 5, 25, 1), (5, 25, 125, 5), (7, 7, 49, 1), (11, 11, 121, 1), (13, 13, 169, 1)] : List (Nat × Nat × Nat × Nat)).map (fun t => get c32 t.1 * get c32 t.2.1 - Int.ofNat t.1 * get c32 t.2.2.2) := by rfl

/-- **Hecke recursion for 27a**: a(p^(k+1)) = a(p)a(p^k) − p·a(p^(k−1)) at every good prime power p^(k+1) < 200 (a(1) = 1). -/
theorem recursion27 : ([(2, 2, 4, 1), (2, 4, 8, 2), (2, 8, 16, 4), (2, 16, 32, 8), (2, 32, 64, 16), (2, 64, 128, 32), (5, 5, 25, 1), (5, 25, 125, 5), (7, 7, 49, 1), (11, 11, 121, 1), (13, 13, 169, 1)] : List (Nat × Nat × Nat × Nat)).map (fun t => get c27 t.2.2.1)
    = ([(2, 2, 4, 1), (2, 4, 8, 2), (2, 8, 16, 4), (2, 16, 32, 8), (2, 32, 64, 16), (2, 64, 128, 32), (5, 5, 25, 1), (5, 25, 125, 5), (7, 7, 49, 1), (11, 11, 121, 1), (13, 13, 169, 1)] : List (Nat × Nat × Nat × Nat)).map (fun t => get c27 t.1 * get c27 t.2.1 - Int.ofNat t.1 * get c27 t.2.2.2) := by rfl

/-- **Hecke recursion for 36a**: a(p^(k+1)) = a(p)a(p^k) − p·a(p^(k−1)) at every good prime power p^(k+1) < 200 (a(1) = 1). -/
theorem recursion36 : ([(5, 5, 25, 1), (5, 25, 125, 5), (7, 7, 49, 1), (11, 11, 121, 1), (13, 13, 169, 1)] : List (Nat × Nat × Nat × Nat)).map (fun t => get c36 t.2.2.1)
    = ([(5, 5, 25, 1), (5, 25, 125, 5), (7, 7, 49, 1), (11, 11, 121, 1), (13, 13, 169, 1)] : List (Nat × Nat × Nat × Nat)).map (fun t => get c36 t.1 * get c36 t.2.1 - Int.ofNat t.1 * get c36 t.2.2.2) := by rfl

/-- **The 5-torsion congruence of 11a, p < 200**: 1 + p − a(p) = 5k_p for every prime p ≠ 11 below 200 (#E(𝔽_p) ≡ 0 mod 5; the
    full statement for all p is user goal G4, still open here: it needs the group law). -/
theorem cong5_11 : ([2, 3, 5, 7, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199] : List Nat).map (fun p => 1 + Int.ofNat p - get c11 p)
    = ([1, 1, 1, 2, 2, 4, 4, 5, 6, 5, 7, 10, 10, 8, 12, 11, 10, 15, 15, 14, 18, 18, 15, 21, 20, 24, 18, 20, 21, 24, 30, 29, 26, 32, 30, 33, 32, 36, 36, 39, 35, 35, 38, 40, 40] : List Int).map (fun k => 5 * k) := by rfl

end Ramanujan11

#print axioms Ramanujan11.ramanujan11
#print axioms Ramanujan11.ramanujan32
#print axioms Ramanujan11.ramanujan27
#print axioms Ramanujan11.ramanujan36
#print axioms Ramanujan11.recursion11
#print axioms Ramanujan11.recursion32
#print axioms Ramanujan11.recursion27
#print axioms Ramanujan11.recursion36
#print axioms Ramanujan11.cong5_11

#print axioms: 9 theorems, no axioms.

78.88 Moments of the quartic twists of \(y^2=x^4+D\)

The second moment of \(1+\sum _x\rho (x^4+v^kD)\) over the four quartic twists.

\[ \chi (v)=i,\ \rho (v)=-1\ \Longrightarrow \ \sum _{k=0}^{3}\Big (1+\sum _x\rho (x^4+v^kD)\Big )^2=8p. \]

lang/QuartMoments.lean

/-
  lang/QuartMoments — T43, cycle 289: moments of the quartic twists of y² = x⁴ + D (p ≡ 1 mod 4).

    quartic_moment2   χ(v) = i, ρ(v) = −1 ⇒ Σ_{k=0}^{3} (1 + Σ_x ρ(x⁴ + v^k D))² = 8p
    quartic_moment4   … and Σ_{k=0}^{3} (1 + Σ_x ρ(x⁴ + v^k D))⁴ = 24p² + 4(J⁴ + J̄⁴)
    fermat4_moment2   Σ_{k=0}^{3} (#(x⁴ + y⁴ = v^k c) − (p − 1 − 2χ(−1)))² = 4(J + J̄)² + 32p
  (quartic_sum: 1 + Σ_x ρ(x⁴ + D) = ρ(D) Tr(χ(−D) J); the twist by v multiplies the trace argument by −i; |J|² = p).
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.QuartQuartic

namespace QuartMoments

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartPrimary QuartDup FermatQuartic QuartTwist QuartQuartic
open LibRing (castZ castN)

variable (n : Nat)

/-- **Second moment over the quartic twists of y² = x⁴ + D**: Σ_{k=0}^{3} (1 + Σ_x ρ(x⁴ + v^k D))² = 8p. -/
theorem quartic_moment2 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chi4 n ι v = (((0 : Int), (1 : Int)))) (hr : rho n ι v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι ((ring (4 * n + 3 + 1)).mul v (D))) (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 Li  -- (+1387 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (4 * n + 3 + 1)).mul v (D) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  have hneg : ∀ x : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).neg x ≠ (ring (4 * n + 3 + 1)).zero := fun x hx e => hx (by
    calc x = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg x) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  have hcc : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (gconj (chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [show (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) = ((chi4 n ι ((ring (4 * n + 3 + 1)).neg D)).1, (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)).2) from rfl, gnorm, (QuartTwistCount.unit_norm n hpr ι hi _ (hneg D hD)).2]; rfl
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by
      calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
        _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring
    exact PowerSum.one_ne_zero' (4 * n + 3) this
  have hJJ : (Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (gconj (Jcr n ι)) = (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) := by
    rw [show (Jcr n ι) = ((Jcr n ι).1, (Jcr n ι).2) from rfl, gnorm, Jcr_eq n hpr ι hi, QuartTwistCount.norm_mul4,
      (QuartTwistCount.unit_norm n hpr ι hi _ m1).2, quart_norm n hpr ι hi]
    show (1 * Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = _
    rw [Int.one_mul]
  have hrr : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (rho n ι D) = (Cyclo4.cyc4 LibInt.intR).one := by rw [← rho_mul n hpr ι hi]; exact QuartPrimary.rho_sq n hpr ι hi D hD
  rw [quartic_sum n hpr ι hi _ n0, quartic_sum n hpr ι hi _ n1, quartic_sum n hpr ι hi _ n2, quartic_sum n hpr ι hi _ n3]
  have hn : ∀ E : Zp (4 * n + 3 + 1), (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul v E) = (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).neg E) := fun E => by lib_ring
  repeat rw [hn]
  have hcv : ∀ E : Zp (4 * n + 3 + 1), chi4 n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi4 n ι E) := fun E => by rw [chi4_mul n hpr ι hi, hv]
  have hρ : ∀ E : Zp (4 * n + 3 + 1), rho n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho n ι E) := fun E => by rw [rho_mul n hpr ι hi, hr]
  repeat rw [hcv]
  repeat rw [hρ]
  repeat rw [gconj_mul]
  have E0 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : In  -- (+2045 chars; full line in the Zenodo source)
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : In  -- (+2284 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : In  -- (+2141 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (rho n ι D)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (Jcr n ι)) ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (Jcr n ι))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.c  -- (+8090 chars; full line in the Zenodo source)
        generalize (gconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4  -- (+195 chars; full line in the Zenodo source)
        rw [E0, E1, E2, hrr, hcc, hJJ]; generalize (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = q; lib_ring

/-- **Fourth moment over the quartic twists of y² = x⁴ + D**: Σ_{k=0}^{3} (1 + Σ_x ρ(x⁴ + v^k D))⁴ = 24p² + 4(J⁴ + J̄⁴). -/
theorem quartic_moment4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (D v : Zp (4 * n + 3 + 1)) (hD : D ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chi4 n ι v = (((0 : Int), (1 : Int)))) (hr : rho n ι v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) (Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).add (S4 n ι (D)) (Cy  -- (+7103 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hD
  have n1 : (ring (4 * n + 3 + 1)).mul v (D) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (D))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  have hneg : ∀ x : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).neg x ≠ (ring (4 * n + 3 + 1)).zero := fun x hx e => hx (by
    calc x = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg x) := by lib_ring
      _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring)
  have hcc : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) (gconj (chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [show (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)) = ((chi4 n ι ((ring (4 * n + 3 + 1)).neg D)).1, (chi4 n ι ((ring (4 * n + 3 + 1)).neg D)).2) from rfl, gnorm, (QuartTwistCount.unit_norm n hpr ι hi _ (hneg D hD)).2]; rfl
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by
      calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
        _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring
    exact PowerSum.one_ne_zero' (4 * n + 3) this
  have hJJ : (Cyclo4.cyc4 LibInt.intR).mul (Jcr n ι) (gconj (Jcr n ι)) = (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) := by
    rw [show (Jcr n ι) = ((Jcr n ι).1, (Jcr n ι).2) from rfl, gnorm, Jcr_eq n hpr ι hi, QuartTwistCount.norm_mul4,
      (QuartTwistCount.unit_norm n hpr ι hi _ m1).2, quart_norm n hpr ι hi]
    show (1 * Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = _
    rw [Int.one_mul]
  have hrr : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (rho n ι D) = (Cyclo4.cyc4 LibInt.intR).one := by rw [← rho_mul n hpr ι hi]; exact QuartPrimary.rho_sq n hpr ι hi D hD
  have hc4 : (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) ((chi4 n ι ((ring (4 * n + 3 + 1)).neg D)))) ((Cyclo4.cyc4 LibInt.intR).mul ((chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) ((chi4 n ι ((ring (4 * n + 3 + 1)).neg D)))) = (Cyclo4.cyc4 LibInt.intR).one ∧ (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chi4 n ι ((rin  -- (+248 chars; full line in the Zenodo source)
    rcases ResidueCriterion.chi4_cases n ι ((ring (4 * n + 3 + 1)).neg D) with e | e | e | e | e
    · exact absurd e (chi4_ne n hpr ι hi _ (hneg D hD))
    all_goals (rw [e]; exact ⟨rfl, rfl⟩)
  rw [quartic_sum n hpr ι hi _ n0, quartic_sum n hpr ι hi _ n1, quartic_sum n hpr ι hi _ n2, quartic_sum n hpr ι hi _ n3]
  have hn : ∀ E : Zp (4 * n + 3 + 1), (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).mul v E) = (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).neg E) := fun E => by lib_ring
  repeat rw [hn]
  have hcv : ∀ E : Zp (4 * n + 3 + 1), chi4 n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi4 n ι E) := fun E => by rw [chi4_mul n hpr ι hi, hv]
  have hρ : ∀ E : Zp (4 * n + 3 + 1), rho n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho n ι E) := fun E => by rw [rho_mul n hpr ι hi, hr]
  repeat rw [hcv]
  repeat rw [hρ]
  repeat rw [gconj_mul]
  have E0 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.  -- (+5112 chars; full line in the Zenodo source)
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.  -- (+4617 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.  -- (+4760 chars; full line in the Zenodo source)
  have E3 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.  -- (+4521 chars; full line in the Zenodo source)
  have E4 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)))) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.  -- (+4920 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (rho n ι D)) ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι D) (rho n ι D))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (gconj (chi4 n ι ((ring (4 * n + 3 + 1)).neg D))) (gconj (ch  -- (+33046 chars; full line in the Zenodo source)
        generalize (gconj (((0 : Int), (1 : Int)))) = a2; generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = b; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (I  -- (+4683 chars; full line in the Zenodo source)
        rw [E0, E1, E2, E3, E4, hrr, hcc, hJJ, hc4.1, hc4.2]; generalize (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = q; lib_ring

/-- **Second moment over the quartic twists of the Fermat quartic**: χ(v) = i, ρ(v) = −1 ⇒
    Σ_{k=0}^{3} (#(x⁴ + y⁴ = v^k c) − (p − 1 − 2χ(−1)))² = 4T² + 32p, T = J + J̄. -/
theorem fermat4_moment2 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (c v : Zp (4 * n + 3 + 1)) (hc : c ≠ (ring (4 * n + 3 + 1)).zero)
    (hv0 : v ≠ (ring (4 * n + 3 + 1)).zero) (hv : chi4 n ι v = (((0 : Int), (1 : Int)))) (hr : rho n ι v = (((-1 : Int)), ((0 : Int)))) :
    (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (QuartTwistCount.Fqc n (c)) ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add (Int.ofNat (4 * n + 3 + 1 + 1), 0) ((Cyclo4.cyc4 LibInt.intR).neg (Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι ((ring (4 * n  -- (+8355 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (4 * n + 3 + 1), x ≠ (ring (4 * n + 3 + 1)).zero → y ≠ (ring (4 * n + 3 + 1)).zero → (ring (4 * n + 3 + 1)).mul x y ≠ (ring (4 * n + 3 + 1)).zero := fun x y hx hy e => by
    cases nozd (4 * n + 3) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hc
  have n1 : (ring (4 * n + 3 + 1)).mul v (c) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (c)) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n1
  have n3 : (ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v ((ring (4 * n + 3 + 1)).mul v (c))) ≠ (ring (4 * n + 3 + 1)).zero := nz _ _ hv0 n2
  have m1 : (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero := fun e => by
    have : (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).zero := by
      calc (ring (4 * n + 3 + 1)).one = (ring (4 * n + 3 + 1)).neg ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) := by lib_ring
        _ = (ring (4 * n + 3 + 1)).zero := by rw [e]; lib_ring
    exact PowerSum.one_ne_zero' (4 * n + 3) this
  have hJJ : (Cyclo4.cyc4 LibInt.intR).mul (J2 n ι) (gconj (J2 n ι)) = (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) := by
    rw [show (J2 n ι) = ((J2 n ι).1, (J2 n ι).2) from rfl, gnorm, QuartTwistCount.J2_eq' n hpr ι hi, QuartTwistCount.norm_mul4,
      (QuartTwistCount.unit_norm n hpr ι hi _ m1).2, quart_norm n hpr ι hi]
    show (1 * Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = _
    rw [Int.one_mul]
  have htb : (Cyclo4.cyc4 LibInt.intR).mul (chi4 n ι c) (chib n ι c) = (Cyclo4.cyc4 LibInt.intR).one := by
    rw [chib_eq n hpr ι hi, show (chi4 n ι c) = ((chi4 n ι c).1, (chi4 n ι c).2) from rfl, gnorm, (QuartTwistCount.unit_norm n hpr ι hi _ hc).2]; rfl
  have hrr : (Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι c) = (Cyclo4.cyc4 LibInt.intR).one := by rw [← rho_mul n hpr ι hi]; exact QuartPrimary.rho_sq n hpr ι hi c hc
  rw [QuartTwistCount.quart_twist_count n hpr ι hi _ n0, QuartTwistCount.quart_twist_count n hpr ι hi _ n1, QuartTwistCount.quart_twist_count n hpr ι hi _ n2, QuartTwistCount.quart_twist_count n hpr ι hi _ n3]
  have hcv : ∀ E : Zp (4 * n + 3 + 1), chi4 n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi4 n ι E) := fun E => by rw [chi4_mul n hpr ι hi, hv]
  have hbv : ∀ E : Zp (4 * n + 3 + 1), chib n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) (chib n ι E) := fun E => by
    rw [chib_mul n hpr ι hi, chib_eq n hpr ι hi v, hv]; rfl
  have hρ : ∀ E : Zp (4 * n + 3 + 1), rho n ι ((ring (4 * n + 3 + 1)).mul v E) = (Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) (rho n ι E) := fun E => by rw [rho_mul n hpr ι hi, hr]
  repeat rw [hcv]
  repeat rw [hbv]
  repeat rw [hρ]
  have E0 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 L  -- (+1040 chars; full line in the Zenodo source)
  have E1 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 L  -- (+849 chars; full line in the Zenodo source)
  have E2 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibInt.  -- (+813 chars; full line in the Zenodo source)
  have E3 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 L  -- (+849 chars; full line in the Zenodo source)
  have E4 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((-1 : Int)), ((0 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibI  -- (+831 chars; full line in the Zenodo source)
  have E5 : (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).one) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int)), ((-1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one)) ((Cyclo4.cyc4 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo4.cyc4 LibInt.intR).one))) ((Cyclo4.cyc4 LibI  -- (+1022 chars; full line in the Zenodo source)
  calc _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (rho n ι c) (rho n ι c)) ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι)))))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR  -- (+12022 chars; full line in the Zenodo source)
        generalize (((0 : Int), (1 : Int))) = a; generalize (((-1 : Int)), ((0 : Int))) = bb; generalize (((0 : Int)), ((-1 : Int))) = d; generalize (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = q; lib_ring
    _ = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι))))) ((Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (Jq n (chi4 n ι)) (gconj (Jq n (chi4 n ι)))) ((Cyclo4.  -- (+2907 chars; full line in the Zenodo source)
        rw [E0, E1, E2, E3, E4, E5, hrr, htb, hJJ]; generalize (Int.ofNat (4 * n + 3 + 1 + 1), (0 : Int)) = q; lib_ring

end QuartMoments

#print axioms QuartMoments.quartic_moment2

#print axioms QuartMoments.quartic_moment4
#print axioms QuartMoments.fermat4_moment2

#print axioms: 3 theorems, no axioms.

78.89 Moments of the cubic twists of the Fermat curve

The second moment of \(\#\{x^3+y^3=v^kc\}-p+2\) over the three cubic twists.

\[ \chi (v)=\omega \ \Longrightarrow \ \sum _{k=0}^{2}\big (\#\{x^3+y^3=v^kc\}-p+2\big )^2=6p. \]

lang/CubicMoments.lean

/-
  lang/CubicMoments — T43, cycle 291: moments of the cubic twists x³ + y³ = c (p ≡ 1 mod 3).

    fermat_moment2   χ(v) = ω ⇒ Σ_{k=0}^{2} (#(x³ + y³ = v^k c) − p + 2)² = 6p
    fermat_moment3   χ(v) = ω ⇒ Σ_{k=0}^{2} (#(x³ + y³ = v^k c) − p + 2)³ = 3(J³ + J̄³)
  (twist_count: # − p + 2 = χ̄(c)J + χ(c)J̄; the cross term carries χ(c)³ = 1, the squares carry ω^{±k} and cancel).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CubicTwistCount

namespace CubicMoments

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm FermatCubic CubicSextic CubicDup CubicCyclo CubicSix CubicTwistCount
open LibRing (castZ castN)

variable (n : Nat)

/-- **Second moment over the cubic twists of the Fermat curve**: Σ_{k=0}^{2} (#(x³ + y³ = v^k c) − p + 2)² = 6p. -/
theorem fermat_moment2 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c v : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) (hv0 : v ≠ (ring (6 * n + 5 + 1)).zero) (hv : chi3 n w v = (((0 : Int), (1 : Int)))) :
    (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (Ft n (c)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add (Ft n (c)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt  -- (+1835 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hc
  have n1 : (ring (6 * n + 5 + 1)).mul v (c) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (c)) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n1
  have hJJ : (Cyclo3.cyc3 LibInt.intR).mul (J n w) (cconj (J n w)) = (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) := by
    rw [show J n w = ((J n w).1, (J n w).2) from rfl, norm_form, jacobi_norm n hpr w hw1 hw3]
  have h3 : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w c)) = (Cyclo3.cyc3 LibInt.intR).one := by
    have := CubicSix.chi_cb n hpr w hw1 hw3 _ hc
    unfold cb at this; exact this
  rw [twist_count n hpr w hw1 hw3 _ n0, twist_count n hpr w hw1 hw3 _ n1, twist_count n hpr w hw1 hw3 _ n2]
  unfold cb
  have hcv : ∀ E : Zp (6 * n + 5 + 1), chi3 n w ((ring (6 * n + 5 + 1)).mul v E) = (Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi3 n w E) := fun E => by rw [chi3_mul n hpr w hw1 hw3, hv]
  repeat rw [hcv]
  have E4 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 Li  -- (+1069 chars; full line in the Zenodo source)
  have E3 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 Li  -- (+833 chars; full line in the Zenodo source)
  have E2 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.  -- (+343 chars; full line in the Zenodo source)
  calc _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w c)) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w c))) ((Cyclo3.cyc3 LibInt.intR).mul (J n w) (J n w))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cycl  -- (+3741 chars; full line in the Zenodo source)
        generalize (((0 : Int), (1 : Int))) = a; generalize (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) = q; lib_ring
    _ = (Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add ((Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Int.ofNat (  -- (+41 chars; full line in the Zenodo source)
        rw [E4, E3, E2, h3, hJJ]; generalize (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) = q; lib_ring

/-- **Third moment over the cubic twists of the Fermat curve**: χ(v) = ω ⇒ Σ_{k=0}^{2} (#(x³ + y³ = v^k c) − p + 2)³ = 3(J³ + J̄³). -/
theorem fermat_moment3 (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (c v : Zp (6 * n + 5 + 1)) (hc : c ≠ (ring (6 * n + 5 + 1)).zero) (hv0 : v ≠ (ring (6 * n + 5 + 1)).zero) (hv : chi3 n w v = (((0 : Int), (1 : Int)))) :
    (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (Ft n (c)) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).neg (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int))) ((Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add (Ft n (c)) ((Cyclo3.cyc3 LibInt  -- (+3080 chars; full line in the Zenodo source)
  have nz : ∀ x y : Zp (6 * n + 5 + 1), x ≠ (ring (6 * n + 5 + 1)).zero → y ≠ (ring (6 * n + 5 + 1)).zero → (ring (6 * n + 5 + 1)).mul x y ≠ (ring (6 * n + 5 + 1)).zero := fun x y hx hy e => by
    cases nozd (6 * n + 5) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  have n0 := hc
  have n1 : (ring (6 * n + 5 + 1)).mul v (c) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n0
  have n2 : (ring (6 * n + 5 + 1)).mul v ((ring (6 * n + 5 + 1)).mul v (c)) ≠ (ring (6 * n + 5 + 1)).zero := nz _ _ hv0 n1
  have h3 : (Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w c)) = (Cyclo3.cyc3 LibInt.intR).one := by
    have := CubicSix.chi_cb n hpr w hw1 hw3 _ hc
    unfold cb at this; exact this
  rw [twist_count n hpr w hw1 hw3 _ n0, twist_count n hpr w hw1 hw3 _ n1, twist_count n hpr w hw1 hw3 _ n2]
  unfold cb
  have hcv : ∀ E : Zp (6 * n + 5 + 1), chi3 n w ((ring (6 * n + 5 + 1)).mul v E) = (Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) (chi3 n w E) := fun E => by rw [chi3_mul n hpr w hw1 hw3, hv]
  repeat rw [hcv]
  have E0 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 LibInt.intR).one)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 Li  -- (+833 chars; full line in the Zenodo source)
  have E1 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (((0 : Int), (1 : Int))) ((Cyclo3.cyc3 Li  -- (+1069 chars; full line in the Zenodo source)
  have E2 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one))))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3  -- (+1432 chars; full line in the Zenodo source)
  have E3 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).one) ((Cyclo3.cyc3 LibInt.intR).one)))))) ((Cycl  -- (+1922 chars; full line in the Zenodo source)
  calc _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) ((Cyclo3.cyc3 LibInt.intR).mul (chi3 n w c) (chi3 n w c))) ((Cyclo3.cyc3 LibInt.intR).mul (cconj (J n w)) ((Cyclo3.cyc3 LibInt.intR).mul (cconj (J n w)) (cconj (J n w))))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibIn  -- (+8828 chars; full line in the Zenodo source)
        generalize (((0 : Int), (1 : Int))) = a; generalize (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) = q; lib_ring
    _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (J n w) ((Cyclo3.cyc3 LibInt.intR).mul (J n w) (J n w))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (J n w) ((Cyclo3.cyc3 LibInt.intR).mul (J n w) (J n w))) ((Cyclo3.cyc3 LibInt.intR).mul (J n w) ((Cyclo3.cyc3 LibInt.intR).mul (J n w) (J n w))))) ((Cyclo3.cyc3 LibInt.intR).add ((C  -- (+371 chars; full line in the Zenodo source)
        rw [E0, E1, E2, E3, h3]; lib_ring

end CubicMoments

#print axioms CubicMoments.fermat_moment2

#print axioms CubicMoments.fermat_moment3

#print axioms: 2 theorems, no axioms.

78.90 Quadratic twists of every elliptic curve

The character sum of \(y^2=x^3+ax+b\) changes by \(\chi (t)\) under the twist \((a,b)\mapsto (t^2a,t^3b)\), every odd \(p\).

\[ \sum _x\chi (x^3+t^2ax+t^3b)=\chi (t)\sum _x\chi (x^3+ax+b)\qquad (t\ne 0). \]

lang/QuadTwist.lean

/-
  lang/QuadTwist — T43, cycle 294: quadratic twists of an arbitrary Weierstrass curve, every odd p.

  p = 2m + 3, χ the Legendre symbol (QChar), W(a, b) = Σ_x χ(x³ + ax + b) (so a_p(y² = x³ + ax + b) = −W(a, b)).
    quad_twist   t ≠ 0 ⇒ W(t²a, t³b) = χ(t) W(a, b)              (x ↦ t x, χ(t³) = χ(t))
    twist_sq     t ≠ 0 ⇒ W(t⁴a, t⁶b) = W(a, b)                     (isomorphic curves)
    sum_b        Σ_b W(a, b) = 0                                   (the a_p of the pencil b ↦ E_{a,b} average to 0)
  The twist by a non-square multiplies a_p by −1 — for every curve, not only the CM ones.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Jacobsthal

namespace QuadTwist

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal

variable (m : Nat)

def W (a b : Zp (2 * m + 1 + 1)) : Int :=
  sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b))) (L m)

/-- **Quadratic twist**: W(t²a, t³b) = χ(t) W(a, b). -/
theorem quad_twist (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b t : Zp (2 * m + 1 + 1)) (ht : t ≠ (ring (2 * m + 1 + 1)).zero) :
    W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) b) = chi m t * W m a b := by
  unfold W
  rw [← reindex_mul m hpr t ht]
  have h : ∀ x, chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t x) ((ring (2 * m + 1 + 1)).mul t x)))
      ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul t x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) b)))
      = chi m t * chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b)) := fun x => by
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t x) ((ring (2 * m + 1 + 1)).mul t x)))
        ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul t x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) b))
        = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b)) by lib_ring,
      chi_mul m hpr, chi_cube m hpr]
  rw [sumK_ext LibInt.intR _ _ h, show chi m t * sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b))) (L m)
      = LibInt.intR.mul (sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b))) (L m)) (chi m t) by
        exact LibInt.intR.mul_comm _ _, sumK_mul]
  exact sumK_ext LibInt.intR _ _ (fun x => LibInt.intR.mul_comm _ _) _

/-- **Twisting by a square does nothing**: W(t⁴a, t⁶b) = W(a, b). -/
theorem twist_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b t : Zp (2 * m + 1 + 1)) (ht : t ≠ (ring (2 * m + 1 + 1)).zero) :
    W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) ((ring (2 * m + 1 + 1)).mul t t)) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) ((ring (2 * m + 1 + 1)).mul t t))) b)
      = W m a b := by
  have htt : (ring (2 * m + 1 + 1)).mul t t ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
    cases nozd (2 * m + 1) hpr _ _ e with
    | inl h => exact ht h
    | inr h => exact ht h
  have h := quad_twist m hpr a b ((ring (2 * m + 1 + 1)).mul t t) htt
  rw [chi_sq m hpr t ht] at h
  calc _ = 1 * W m a b := h
    _ = W m a b := by lib_int

theorem sum_zero : ∀ l : List (Zp (2 * m + 1 + 1)), sumK LibInt.intR (fun _ => (0 : Int)) l = 0
  | [] => rfl
  | _ :: l => by
    show LibInt.intR.add 0 (sumK LibInt.intR (fun _ => (0 : Int)) l) = 0
    rw [sum_zero l]; rfl

/-- **The traces of the curves y² = x³ + ax + b sum to zero over b**: Σ_b W(a, b) = 0. -/
theorem sum_b (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) : sumK LibInt.intR (fun b => W m a b) (L m) = 0 := by
  unfold W
  rw [← sumK_swap LibInt.intR (fun x b => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b))) (L m) (L m)]
  have h : ∀ x, sumK LibInt.intR (fun b => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b))) (L m) = 0 := fun x => by
    have e := shift_sum m (chi m) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x))
    rw [sumK_ext LibInt.intR _ (fun b => chi m ((ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)))) (fun b => by
          rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b) = (ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)) by lib_ring]) _,
      e]
    exact chi_sum m hpr
  rw [sumK_ext LibInt.intR _ _ h]
  exact sum_zero m _

end QuadTwist

#print axioms QuadTwist.quad_twist
#print axioms QuadTwist.twist_sq
#print axioms QuadTwist.sum_zero
#print axioms QuadTwist.sum_b

#print axioms: 4 theorems, no axioms.

78.91 Second moment over a pencil of elliptic curves

The squares of the traces of \(y^2=x^3+ax+b\), summed over \(b\), every odd \(p\).

\[ \sum _b\Big (\sum _x\chi (x^3+ax+b)\Big )^2=-p^2+p\cdot \#\{(x,y):x^3+ax=y^3+ay\}. \]

lang/PencilMoment.lean

/-
  lang/PencilMoment — T43, cycle 296: the second moment of the traces of the pencil b ↦ y² = x³ + ax + b, every odd p.

  p = 2m + 3, W(a, b) = Σ_x χ(x³ + ax + b) (QuadTwist), u(x) = x³ + ax.
    sq_expand       W(a, b)² = Σ_x Σ_y χ((b + u(x))(b + u(y)))
    pencil_moment   Σ_b W(a, b)² = −p² + p · #{(x, y) : u(x) = u(y)}
    inner_a         Σ_a δ(x³ + ax, y³ + ay) = 1 + (p − 1)δ(x, y)
    birch_moment2   Σ_a Σ_b W(a, b)² = p³ − p²                        (Birch's second moment)
    birch_moment3   Σ_a Σ_b W(a, b)³ = 0                              ((a, b) ↦ (t²a, t³b), χ(t) = −1)
    birch_odd       Σ_a Σ_b W(a, b)^(2k+1) = 0 for every k
    sum_a           Σ_a W(a, b) = p χ(b)
    pencil_cov      Σ_b W(a, b) W(a', b) = −p² + p · #{(x, y) : x³ + ax = y³ + a'y}
    sum_a_sq        Σ_b (Σ_a W(a, b))² = p²(p − 1)
  (JacobMoment.inner: Σ_b χ((b + s)(b + t)) = −1 + p δ(s, t).) With sum_b (Σ_b W = 0) this is the variance of a_p over the pencil.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.QuadTwist
import lang.JacobMoment
import lang.JacobiNorm

namespace PencilMoment

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal QuadTwist JacobMoment

variable (m : Nat)

def u (a x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)

theorem W_eq (a b : Zp (2 * m + 1 + 1)) : W m a b = sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add b (u m a x))) (L m) :=
  sumK_ext LibInt.intR _ _ (fun x => by unfold u; rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b)
    = (ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)) by lib_ring]) _

theorem sq_expand (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) :
    W m a b * W m a b = sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add b (u m a x)) ((ring (2 * m + 1 + 1)).add b (u m a y)))) (L m)) (L m) := by
  rw [W_eq]
  show LibInt.intR.mul _ _ = _
  rw [sumK_mul]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    rw [mul_sumK]
    exact sumK_ext LibInt.intR _ _ (fun y => (chi_mul m hpr _ _).symm) _) _

theorem sum_lin (c s : Int) (f : Zp (2 * m + 1 + 1) → Int) :
    sumK LibInt.intR (fun y => c + s * f y) (L m) = c * P m + s * sumK LibInt.intR f (L m) := by
  rw [sumK_ext LibInt.intR _ (fun y => LibInt.intR.add (LibInt.intR.mul c 1) (LibInt.intR.mul s (f y))) (fun y => by show _ = c * 1 + s * f y; lib_int),
    sumK_add, ← mul_sumK, ← mul_sumK, sum_one]
  rfl

/-- **Second moment over the pencil**: Σ_b W(a, b)² = −p² + p · #{(x, y) : x³ + ax = y³ + ay}. -/
theorem pencil_moment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun b => W m a b * W m a b) (L m)
      = -(P m * P m) + P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a y)) (L m)) (L m) := by
  rw [sumK_ext LibInt.intR _ _ (sq_expand m hpr a)]
  rw [← sumK_swap LibInt.intR (fun x b => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add b (u m a x)) ((ring (2 * m + 1 + 1)).add b (u m a y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => -1 + P m * delta LibInt.intR (u m a x) (u m a y)) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y b => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add b (u m a x)) ((ring (2 * m + 1 + 1)).add b (u m a y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => inner m hpr _ _) _)]
  rw [sumK_ext LibInt.intR _ _ (fun x => sum_lin m (-1) (P m) _), sum_lin]
  lib_int

theorem nz2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul x y = (ring (2 * m + 1 + 1)).zero) : x = (ring (2 * m + 1 + 1)).zero ∨ y = (ring (2 * m + 1 + 1)).zero :=
  nozd (2 * m + 1) hpr _ _ h

/-- For x ≠ y, x³ + ax = y³ + ay exactly when a = −(x² + xy + y²). -/
theorem inner_a (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun a => delta LibInt.intR (u m a x) (u m a y)) (L m) = 1 + (P m + -1) * delta LibInt.intR y x := by
  by_cases hxy : y = x
  · rw [hxy, sumK_ext LibInt.intR _ (fun _ => (1 : Int)) (fun a => delta_one m _ _ rfl), sum_one, delta_one m _ _ rfl]; lib_int
  · rw [delta_zero m _ _ hxy]
    have hs : ∀ a, delta LibInt.intR (u m a x) (u m a y) = delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero := fun a => by
      by_cases ha : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y))) = (ring (2 * m + 1 + 1)).zero
      · rw [delta_one m _ _ ha, delta_one m _ _ (by
          unfold u
          calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)
              = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).mul a y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)  -- (+62 chars; full line in the Zenodo source)
            _ = _ := by rw [ha]; lib_ring)]
      · rw [delta_zero m _ _ ha, delta_zero m _ _ (fun e => by
          unfold u at e
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) = (ring (2 * m + 1 + 1)).zero := by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).mul a y))) := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact hxy (by
              calc y = (ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg y)) := by rw [h]; lib_ring
                _ = x := by lib_ring)
          | inr h => exact ha h)]
    rw [sumK_ext LibInt.intR _ _ hs, sum_delta_shift]; lib_int

/-- **Birch's second moment**: Σ_a Σ_b (Σ_x χ(x³ + ax + b))² = p³ − p², every odd p. -/
theorem birch_moment2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * W m a b) (L m)) (L m) = P m * P m * P m + -(P m * P m) := by
  rw [sumK_ext LibInt.intR _ _ (fun a => pencil_moment m hpr a)]
  rw [show (fun a => -(P m * P m) + P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a y)) (L m)) (L m))
      = (fun a => -(P m * P m) + P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a y)) (L m)) (L m)) from rfl,
    sum_lin]
  rw [sumK_swap LibInt.intR (fun a x => sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a y)) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => 1 + (P m + -1) * delta LibInt.intR y x) (L m)) (fun x => by
    rw [sumK_swap LibInt.intR (fun a y => delta LibInt.intR (u m a x) (u m a y))]
    exact sumK_ext LibInt.intR _ _ (fun y => inner_a m hpr x y) _)]
  rw [sumK_ext LibInt.intR _ (fun _ => 1 * P m + (P m + -1) * 1) (fun x => by rw [sum_lin, show sumK LibInt.intR (fun y => delta LibInt.intR y x) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR x])]
  rw [sumK_ext LibInt.intR _ (fun _ => LibInt.intR.mul (1 * P m + (P m + -1) * 1) 1) (fun _ => by show (1 * P m + (P m + -1) * 1 : Int) = (1 * P m + (P m + -1) * 1) * 1; lib_int), ← mul_sumK, sum_one]
  show -(P m * P m) * P m + P m * ((1 * P m + (P m + -1) * 1) * P m) = _
  lib_int

/-- **Birch's third moment vanishes**: Σ_a Σ_b (Σ_x χ(x³ + ax + b))³ = 0 (twist by a non-residue). -/
theorem birch_moment3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * W m a b * W m a b) (L m)) (L m) = 0 := by
  match exists_nonres m hpr with
  | ⟨t, ht0, ht⟩ =>
    have nz : ∀ x y : Zp (2 * m + 1 + 1), x ≠ (ring (2 * m + 1 + 1)).zero → y ≠ (ring (2 * m + 1 + 1)).zero → (ring (2 * m + 1 + 1)).mul x y ≠ (ring (2 * m + 1 + 1)).zero := fun x y hx hy e => by
      cases nz2 m hpr _ _ e with
      | inl h => exact hx h
      | inr h => exact hy h
    have h2 := nz _ _ ht0 ht0
    have h3 := nz _ _ ht0 h2
    have hneg : sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * W m a b * W m a b) (L m)) (L m)
        = LibInt.intR.mul (-1) (sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * W m a b * W m a b) (L m)) (L m)) := by
      calc _ = sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) b) * W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) b) * W  -- (+181 chars; full line in the Zenodo source)
            rw [← reindex_mul m hpr ((ring (2 * m + 1 + 1)).mul t t) h2 (fun a => sumK LibInt.intR (fun b => W m a b * W m a b * W m a b) (L m))]
            exact sumK_ext LibInt.intR _ _ (fun a => (reindex_mul m hpr ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) h3
              (fun b => W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) b * W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) b * W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) b)).symm) _
        _ = _ := by
          rw [mul_sumK]
          exact sumK_ext LibInt.intR _ _ (fun a => by
            rw [mul_sumK]
            exact sumK_ext LibInt.intR _ _ (fun b => by
              rw [quad_twist m hpr a b t ht0, ht]
              show (-1 * W m a b) * (-1 * W m a b) * (-1 * W m a b) = -1 * (W m a b * W m a b * W m a b)
              lib_int) _) _
    exact JacobiNorm.int_cancel _ _ 1 (by
      have e : ∀ X : Int, X = LibInt.intR.mul (-1) X → X * Int.ofNat (1 + 1) = 0 * Int.ofNat (1 + 1) := fun X hX => by
        calc X * Int.ofNat (1 + 1) = X + X := by show X * 2 = X + X; lib_int
          _ = X + -1 * X := by
            have : X = -1 * X := hX
            rw [← this]
          _ = 0 * Int.ofNat (1 + 1) := by show X + -1 * X = 0 * 2; lib_int
      exact e _ hneg)

def ipow (x : Int) : Nat → Int
  | 0 => 1
  | k + 1 => x * ipow x k

/-- **Every odd Birch moment vanishes**: Σ_a Σ_b (Σ_x χ(x³ + ax + b))^(2k+1) = 0. -/
theorem birch_odd (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (k : Nat) :
    sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * ipow (W m a b * W m a b) k) (L m)) (L m) = 0 := by
  match exists_nonres m hpr with
  | ⟨t, ht0, ht⟩ =>
    have nz : ∀ x y : Zp (2 * m + 1 + 1), x ≠ (ring (2 * m + 1 + 1)).zero → y ≠ (ring (2 * m + 1 + 1)).zero → (ring (2 * m + 1 + 1)).mul x y ≠ (ring (2 * m + 1 + 1)).zero := fun x y hx hy e => by
      cases nz2 m hpr _ _ e with
      | inl h => exact hx h
      | inr h => exact hy h
    have h2 := nz _ _ ht0 ht0
    have h3 := nz _ _ ht0 h2
    have hneg : sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * ipow (W m a b * W m a b) k) (L m)) (L m)
        = LibInt.intR.mul (-1) (sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m a b * ipow (W m a b * W m a b) k) (L m)) (L m)) := by
      calc _ = sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) b) * ipow (W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t))   -- (+190 chars; full line in the Zenodo source)
            rw [← reindex_mul m hpr ((ring (2 * m + 1 + 1)).mul t t) h2 (fun a => sumK LibInt.intR (fun b => W m a b * ipow (W m a b * W m a b) k) (L m))]
            exact sumK_ext LibInt.intR _ _ (fun a => (reindex_mul m hpr ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) h3
              (fun b => W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) b * ipow (W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) b * W m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) a) b) k)).symm) _
        _ = _ := by
          rw [mul_sumK]
          exact sumK_ext LibInt.intR _ _ (fun a => by
            rw [mul_sumK]
            exact sumK_ext LibInt.intR _ _ (fun b => by
              rw [quad_twist m hpr a b t ht0, ht]
              rw [show (-1 * W m a b) * (-1 * W m a b) = W m a b * W m a b by lib_int]
              show -1 * W m a b * ipow (W m a b * W m a b) k = -1 * (W m a b * ipow (W m a b * W m a b) k)
              lib_int) _) _
    exact JacobiNorm.int_cancel _ _ 1 (by
      have e : ∀ X : Int, X = LibInt.intR.mul (-1) X → X * Int.ofNat (1 + 1) = 0 * Int.ofNat (1 + 1) := fun X hX => by
        calc X * Int.ofNat (1 + 1) = X + X := by show X * 2 = X + X; lib_int
          _ = X + -1 * X := by
            have : X = -1 * X := hX
            rw [← this]
          _ = 0 * Int.ofNat (1 + 1) := by show X + -1 * X = 0 * 2; lib_int
      exact e _ hneg)

/-- **Summing over a**: Σ_a Σ_x χ(x³ + ax + b) = p χ(b) (only x = 0 survives). -/
theorem sum_a (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b : Zp (2 * m + 1 + 1)) : sumK LibInt.intR (fun a => W m a b) (L m) = P m * chi m b := by
  unfold W
  rw [sumK_swap LibInt.intR (fun a x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b)))]
  have h : ∀ x, sumK LibInt.intR (fun a => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b))) (L m)
      = LibInt.intR.mul (P m * chi m b) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) := fun x => by
    by_cases hx : x = (ring (2 * m + 1 + 1)).zero
    · rw [hx, delta_one m _ _ rfl]
      rw [sumK_ext LibInt.intR _ (fun _ => LibInt.intR.mul (chi m b) 1) (fun a => by
        rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a (ring (2 * m + 1 + 1)).zero) b) = b by lib_ring]
        show _ = chi m b * 1; lib_int), ← mul_sumK, sum_one]
      show chi m b * P m = P m * chi m b * 1; lib_int
    · rw [delta_zero m _ _ hx]
      rw [sumK_ext LibInt.intR _ (fun a => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x a) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) b))) (fun a => by
        rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) b) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x a) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) b) by lib_ring]),
        reindex_mul m hpr x hx (fun y => chi m ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) b))),
        shift_sum m (chi m), show sumK LibInt.intR (chi m) (L m) = 0 from chi_sum m hpr]
      show (0 : Int) = P m * chi m b * 0; lib_int
  rw [sumK_ext LibInt.intR _ _ h, ← mul_sumK, show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
  show P m * chi m b * 1 = _; lib_int

theorem cov_expand (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a a' b : Zp (2 * m + 1 + 1)) :
    W m a b * W m a' b = sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add b (u m a x)) ((ring (2 * m + 1 + 1)).add b (u m a' y)))) (L m)) (L m) := by
  rw [W_eq, W_eq]
  show LibInt.intR.mul _ _ = _
  rw [sumK_mul]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    rw [mul_sumK]
    exact sumK_ext LibInt.intR _ _ (fun y => (chi_mul m hpr _ _).symm) _) _

/-- **Covariance over the pencil**: Σ_b W(a, b) W(a', b) = −p² + p · #{(x, y) : x³ + ax = y³ + a'y}. -/
theorem pencil_cov (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a a' : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun b => W m a b * W m a' b) (L m)
      = -(P m * P m) + P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a' y)) (L m)) (L m) := by
  rw [sumK_ext LibInt.intR _ _ (cov_expand m hpr a a')]
  rw [← sumK_swap LibInt.intR (fun x b => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add b (u m a x)) ((ring (2 * m + 1 + 1)).add b (u m a' y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => -1 + P m * delta LibInt.intR (u m a x) (u m a' y)) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y b => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add b (u m a x)) ((ring (2 * m + 1 + 1)).add b (u m a' y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => inner m hpr _ _) _)]
  rw [sumK_ext LibInt.intR _ _ (fun x => sum_lin m (-1) (P m) _), sum_lin]
  lib_int

/-- **Σ_b (Σ_a W(a, b))² = p²(p − 1).** -/
theorem sum_a_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK LibInt.intR (fun b => sumK LibInt.intR (fun a => W m a b) (L m) * sumK LibInt.intR (fun a => W m a b) (L m)) (L m) = P m * P m * P m + -(P m * P m) := by
  rw [sumK_ext LibInt.intR _ (fun b => -(P m * P m) * delta LibInt.intR b (ring (2 * m + 1 + 1)).zero + P m * P m) (fun b => by
    rw [sum_a m hpr b]
    show P m * chi m b * (P m * chi m b) = _
    rw [show P m * chi m b * (P m * chi m b) = P m * P m * (chi m b * chi m b) by lib_int, ← chi_mul m hpr, sq_delta m hpr]
    show P m * P m * (1 + -1 * delta LibInt.intR b (ring (2 * m + 1 + 1)).zero) = -(P m * P m) * delta LibInt.intR b (ring (2 * m + 1 + 1)).zero + P m * P m
    lib_int)]
  rw [sumK_ext LibInt.intR _ (fun b => LibInt.intR.add (LibInt.intR.mul (-(P m * P m)) (delta LibInt.intR b (ring (2 * m + 1 + 1)).zero)) (LibInt.intR.mul (P m * P m) 1)) (fun b => by
    show _ = -(P m * P m) * delta LibInt.intR b (ring (2 * m + 1 + 1)).zero + P m * P m * 1; lib_int), sumK_add, ← mul_sumK, ← mul_sumK, sum_one,
    show sumK LibInt.intR (fun b => delta LibInt.intR b (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
  show -(P m * P m) * 1 + P m * P m * P m = _
  lib_int

end PencilMoment

#print axioms PencilMoment.W_eq
#print axioms PencilMoment.sq_expand
#print axioms PencilMoment.sum_lin
#print axioms PencilMoment.pencil_moment
#print axioms PencilMoment.nz2
#print axioms PencilMoment.inner_a
#print axioms PencilMoment.birch_moment2
#print axioms PencilMoment.birch_moment3
#print axioms PencilMoment.birch_odd
#print axioms PencilMoment.sum_a
#print axioms PencilMoment.cov_expand
#print axioms PencilMoment.pencil_cov
#print axioms PencilMoment.sum_a_sq

#print axioms: 13 theorems, no axioms.

78.92 Infinitely many primes \(p\equiv 9\pmod {16}\)

Euclid’s argument with \(M=\big ((2O^2+1)^4+1\big )/2\equiv 9\pmod {16}\), \(O=1\cdot 3\cdots (2B-1)\).

\[ \forall B\ \exists c:\ 16c+9\ \text {prime and}\ 16c+9>B. \]

lang/Inf9mod16.lean

/-
  lang/Inf9mod16 — T43, cycle 303: infinitely many primes p ≡ 9 (mod 16).

  O = 1·3·5···(2B − 1) (odd factorial), O² = 8C + 1, x = 2O² + 1 = 16C + 3, x⁴ + 1 = 2M with M ≡ 9 (mod 16), M odd.
  Every prime factor q of M has x⁴ = −1 ≠ 1, x⁸ = 1 in 𝔽_q, so 8 | q − 1 (CycloPow.dvd_of_pow): q ≡ 1 or 9 (mod 16). The product
  is ≡ 9, so some factor is ≡ 9 (descent has16, as Inf17mod24.has_gen). An odd q ≤ 2B − 1 divides O, hence x⁴ + 1 − 2, hence 2.
    oddf, oddf_sq, oddf_dv, uniq_rem16, mod16n, cofactor16, has16, allowed16, nM, inf_9mod16
  The class 9 mod 16: split in ℚ(ζ₈), inert in ℚ(ζ₁₆)/ℚ(ζ₈) (a² ≡ 1 mod 16, so Euclid's method applies).
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Inf17mod24

namespace Inf9mod16

open FTAChain ZMod InfSplit

/-- O(B) = 1·3·5···(2B − 1). -/
def oddf : Nat → Nat
  | 0 => 1
  | k + 1 => oddf k * (2 * k + 1)

theorem odd_sq : ∀ n, ∃ T, (2 * n + 1) * (2 * n + 1) = 8 * T + 1
  | 0 => ⟨0, rfl⟩
  | n + 1 => by
    match odd_sq n with
    | ⟨T, hT⟩ => exact ⟨T + n + 1, by
        rw [show (2 * (n + 1) + 1) * (2 * (n + 1) + 1) = (2 * n + 1) * (2 * n + 1) + 8 * (n + 1) by lib_nat, hT]; lib_nat⟩

theorem oddf_sq : ∀ B, ∃ C, oddf B * oddf B = 8 * C + 1
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    match oddf_sq k, odd_sq k with
    | ⟨C, hC⟩, ⟨T, hT⟩ =>
      have e1 : oddf (k + 1) * oddf (k + 1) = (oddf k * oddf k) * ((2 * k + 1) * (2 * k + 1)) := by
        show (oddf k * (2 * k + 1)) * (oddf k * (2 * k + 1)) = _
        lib_nat
      rw [e1, hC, hT]
      exact ⟨8 * C * T + C + T, by lib_nat⟩

theorem oddf_dv : ∀ B k, k < B → Dv (2 * k + 1) (oddf B)
  | 0, _, h => absurd h (Nat.not_succ_le_zero _)
  | B + 1, k, h => by
    show Dv (2 * k + 1) (oddf B * (2 * B + 1))
    cases Nat.lt_or_ge k B with
    | inl hk => exact Inf17mod24.dv_mulr _ _ _ (oddf_dv B k hk)
    | inr hk =>
      have e : k = B := Nat.le_antisymm (Nat.le_of_succ_le_succ h) hk
      rw [e]
      exact ⟨oddf B, by lib_nat⟩

theorem uniq_rem16 (x y r c : Nat) (hr : r < 16) (hc : c < 16) (h : x * 16 + r = y * 16 + c) : r = c := by
  have big : ∀ x y r c : Nat, r < 16 → x < y → x * 16 + r = y * 16 + c → False := fun x y r c hr hxy h => by
    have h1 : x * 16 + r < (x + 1) * 16 := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * 16 ≤ y * 16 := Nat.mul_le_mul_right 16 hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * 16 + Int.ofNat r = Int.ofNat x * 16 + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * 16 + Int.ofNat r) - Int.ofNat x * 16 := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

theorem mod16n (n : Nat) : ∃ a r, n = a * 16 + r ∧ r < 16 :=
  match div_exists 15 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (16 : Nat) = 15 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor16 (b N' e : Nat) (h : (b * 16 + 1) * N' = e * 16 + 9) : ∃ c, N' = c * 16 + 9 :=
  match mod16n N' with
  | ⟨c, r, hc, hr⟩ => by
    have e' : (b * 16 + 1) * N' = (b * 16 * c + b * r + c) * 16 + r := by rw [hc]; lib_nat
    rw [e'] at h
    exact ⟨c, by rw [hc, uniq_rem16 _ _ r 9 hr (Nat.le_add_left 10 6) h]⟩

/-- **Descent.** If every prime factor of N₀ is ≡ 1 or 9 (mod 16), a divisor N ≡ 9 has a prime factor ≡ 9. -/
theorem has16 (N0 : Nat)
    (hall : ∀ l, (∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) → Dv (l + 1 + 1) N0 →
      ∃ c t, l + 1 + 1 = c * 16 + t ∧ (t = 1 ∨ t = 9)) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ e, N = e * 16 + 9) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ c, q = c * 16 + 9) ∧ Dv q N
  | 0, N, hN, _, ⟨e, he⟩ => absurd (he ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨e, he⟩ => by
    match lp_exists (e * 16 + 7) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [he]; exact hl
    match hall l hq (dv_trans _ _ _ hlN hN0) with
    | ⟨c, t, hc, ht⟩ =>
      cases ht with
      | inr h9 => exact ⟨l + 1 + 1, hq, ⟨c, by rw [hc, h9]⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactor16 c N' e (by rw [← he, hN', hc, h1]; lib_nat) with
        | ⟨d, hd⟩ =>
        have hlt : N' ≤ G := by
          have e' : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hd]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e']; exact hN))
        have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match has16 N0 hall G N' hlt hN'0 ⟨d, hd⟩ with
        | ⟨q, hq', hqa, hqN⟩ => exact ⟨q, hq', hqa, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

theorem x2g (t C : Nat) : (t * C + 3) * (t * C + 3) = t * (t * C * C + 6 * C) + 9 := by lib_nat
theorem x2 (C : Nat) : (16 * C + 3) * (16 * C + 3) = 16 * (16 * C * C + 6 * C) + 9 := x2g 16 C
theorem x4g (t D : Nat) : (t * D + 9) * (t * D + 9) + 1 = t * (t * (D * D) + 18 * D) + 82 := by lib_nat
theorem x4l (h E D : Nat) : 2 * h * (2 * h * E + 18 * D) + (8 * h + 18) = 2 * ((h * E + 9 * D + 2) * (2 * h) + 9) := by lib_nat
theorem x4 (D : Nat) : (16 * D + 9) * (16 * D + 9) + 1 = 2 * ((8 * (D * D) + 9 * D + 2) * 16 + 9) := by
  rw [x4g 16 D]
  exact x4l 8 (D * D) D

/-- x = 16C + 3 ⟹ x⁴ + 1 = 2M, M = 16K + 9. -/
theorem nM (C : Nat) : ∃ K, (16 * C + 3) ^ 4 + 1 = 2 * (K * 16 + 9) :=
  ⟨8 * ((16 * C * C + 6 * C) * (16 * C * C + 6 * C)) + 9 * (16 * C * C + 6 * C) + 2, by
    show 1 * (16 * C + 3) * (16 * C + 3) * (16 * C + 3) * (16 * C + 3) + 1 = _
    rw [show 1 * (16 * C + 3) * (16 * C + 3) * (16 * C + 3) * (16 * C + 3) = ((16 * C + 3) * (16 * C + 3)) * ((16 * C + 3) * (16 * C + 3)) by
      generalize 16 * C + 3 = y; lib_nat, x2, x4]⟩

theorem odd16 (K r : Nat) (e : K * 16 + 9 = 2 * r) : False :=
  odd_not_dv2 (K * 8 + 4) r (by rw [show 2 * (K * 8 + 4) + 1 = K * 16 + 9 by lib_nat, e]; lib_nat)

theorem keyg (t s : Nat) : (t * s + 1) * (t * s + 1) * (t * s + 1) * (t * s + 1) + 1
    = s * (4 * t + 6 * (t * t) * s + 4 * (t * t * t) * (s * s) + (t * t * t * t) * (s * s * s)) + 2 := by lib_nat

/-- (2s + 1)⁴ + 1 = 2 + O·(…), s = O². -/
theorem key (O : Nat) : (2 * (O * O) + 1) ^ 4 + 1
    = (O * O) * (4 * 2 + 6 * (2 * 2) * (O * O) + 4 * (2 * 2 * 2) * ((O * O) * (O * O)) + (2 * 2 * 2 * 2) * ((O * O) * (O * O) * (O * O))) + 2 := by
  show 1 * (2 * (O * O) + 1) * (2 * (O * O) + 1) * (2 * (O * O) + 1) * (2 * (O * O) + 1) + 1 = _
  rw [show 1 * (2 * (O * O) + 1) * (2 * (O * O) + 1) * (2 * (O * O) + 1) * (2 * (O * O) + 1)
      = (2 * (O * O) + 1) * (2 * (O * O) + 1) * (2 * (O * O) + 1) * (2 * (O * O) + 1) by generalize 2 * (O * O) + 1 = y; lib_nat,
    keyg 2 (O * O)]

theorem par : ∀ k, ∃ j, k = 2 * j ∨ k = 2 * j + 1
  | 0 => ⟨0, Or.inl rfl⟩
  | k + 1 => by
    match par k with
    | ⟨j, Or.inl h⟩ => exact ⟨j, Or.inr (by rw [h])⟩
    | ⟨j, Or.inr h⟩ => exact ⟨j + 1, Or.inl (by rw [h]; lib_nat)⟩

/-- prime factors of M (odd, 2M = x⁴ + 1) are ≡ 1 or 9 (mod 16). -/
theorem allowed16 (x M l : Nat) (hM : x ^ 4 + 1 = 2 * M) (hodd : ∀ r, M ≠ 2 * r) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hdv : Dv (l + 1 + 1) M) : ∃ c t, l + 1 + 1 = c * 16 + t ∧ (t = 1 ∨ t = 9) := by
  have hq2 : l + 1 + 1 ≠ 2 := fun e => by
    rw [e] at hdv
    match hdv with
    | ⟨w, hw⟩ => exact hodd w (by rw [hw]; lib_nat)
  have hdv' : Dv (l + 1 + 1) (x ^ 4 + 1) := by
    match hdv with
    | ⟨w, hw⟩ => exact ⟨2 * w, by rw [hM, hw]; lib_nat⟩
  have hxfull : Frobenius.pw (ring (l + 1)) (mk (l + 1) x) ((0 + 1 + 1) ^ (2 + 1)) = (ring (l + 1)).one := by
    show Frobenius.pw (ring (l + 1)) (mk (l + 1) x) 8 = _
    rw [PrimeOneModQ.mk_pow, Inf1mod24.pow8]; exact Inf1mod24.sq_one l _ hdv'
  have hX1 : Frobenius.pw (ring (l + 1)) (mk (l + 1) x) ((0 + 1 + 1) ^ 2) ≠ (ring (l + 1)).one := by
    show Frobenius.pw (ring (l + 1)) (mk (l + 1) x) 4 ≠ _
    rw [PrimeOneModQ.mk_pow]; exact Inf1mod24.ne_one l _ hq2 hdv'
  match CycloPow.dvd_of_pow l hq 0 2 StrongMult1.two_prime _ hxfull hX1 with
  | ⟨k, hk⟩ =>
  have hk8 : l + 1 + 1 = k * 8 + 1 := by rw [hk]
  match par k with
  | ⟨j, hj⟩ =>
    cases hj with
    | inl h => exact ⟨j, 1, by rw [hk8, h]; lib_nat, Or.inl rfl⟩
    | inr h => exact ⟨j, 9, by rw [hk8, h]; lib_nat, Or.inr rfl⟩

/-- **Infinitely many primes p ≡ 9 (mod 16).** -/
theorem inf_9mod16 (B : Nat) : ∃ c, (∀ d, Dv d (c * 16 + 9) → d = 1 ∨ d = c * 16 + 9) ∧ B < c * 16 + 9 := by
  match oddf_sq B with
  | ⟨C, hC⟩ =>
  have hx : 2 * (oddf B * oddf B) + 1 = 16 * C + 3 := by rw [hC]; lib_nat
  match nM C with
  | ⟨K, hK⟩ =>
  match has16 (K * 16 + 9) (fun l hq hdv => allowed16 (16 * C + 3) (K * 16 + 9) l hK (fun r e => odd16 K r e) hq hdv)
      (K * 16 + 9) (K * 16 + 9) (Nat.le_refl _) ⟨1, by lib_nat⟩ ⟨K, rfl⟩ with
  | ⟨q, hq, ⟨c, hc⟩, hqM⟩ =>
    refine ⟨c, by rw [← hc]; exact hq, ?_⟩
    rw [← hc]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      have hk : 8 * c + 4 < B := by
        have h1 : 2 * (8 * c + 4) + 1 ≤ B := by rw [show 2 * (8 * c + 4) + 1 = c * 16 + 9 by lib_nat, ← hc]; exact h
        have h2 : 8 * c + 4 < 2 * (8 * c + 4) + 1 :=
          Nat.lt_succ_of_le (by rw [show 2 * (8 * c + 4) = (8 * c + 4) + (8 * c + 4) by lib_nat]; exact Nat.le_add_left _ _)
        exact Nat.lt_of_lt_of_le h2 h1
      have hqO : Dv q (oddf B) := by rw [hc, show c * 16 + 9 = 2 * (8 * c + 4) + 1 by lib_nat]; exact oddf_dv B _ hk
      -- q | 2M = x⁴ + 1 = O·Y + 2 and q | O·Y, so q | 2
      have hq2M : Dv q ((2 * (oddf B * oddf B) + 1) ^ 4 + 1) := by
        rw [hx, hK]
        match hqM with
        | ⟨w, hw⟩ => exact ⟨2 * w, by rw [hw]; lib_nat⟩
      rw [key] at hq2M
      have hqOY : Dv q ((oddf B * oddf B) * (4 * 2 + 6 * (2 * 2) * (oddf B * oddf B) + 4 * (2 * 2 * 2) * ((oddf B * oddf B) * (oddf B * oddf B)) + (2 * 2 * 2 * 2) * ((oddf B * oddf B) * (oddf B * oddf B) * (oddf B * oddf B)))) :=
        Inf17mod24.dv_mulr _ _ _ (Inf17mod24.dv_mulr _ _ _ hqO)
      have hq' : q = (c * 16 + 8) + 1 := hc
      rw [hq'] at hqOY hq2M
      have h2 := dv_sub (c * 16 + 8) _ 2 hqOY hq2M
      cases StrongMult1.two_prime _ h2 with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e |> fun e' => (show c * 16 + 8 = 0 from e'))
      | inr e => exact absurd (Nat.succ.inj e) (fun e' => Nat.noConfusion (Nat.succ.inj (show c * 16 + 7 + 1 = 0 + 1 from e')))

end Inf9mod16

#print axioms Inf9mod16.odd_sq
#print axioms Inf9mod16.oddf_sq
#print axioms Inf9mod16.oddf_dv
#print axioms Inf9mod16.uniq_rem16
#print axioms Inf9mod16.mod16n
#print axioms Inf9mod16.cofactor16
#print axioms Inf9mod16.has16
#print axioms Inf9mod16.x2g
#print axioms Inf9mod16.x2
#print axioms Inf9mod16.x4g
#print axioms Inf9mod16.x4l
#print axioms Inf9mod16.x4
#print axioms Inf9mod16.nM
#print axioms Inf9mod16.odd16
#print axioms Inf9mod16.keyg
#print axioms Inf9mod16.key
#print axioms Inf9mod16.par
#print axioms Inf9mod16.allowed16
#print axioms Inf9mod16.inf_9mod16

#print axioms: 19 theorems, no axioms.

78.93 Infinitely many primes \(p\equiv 2^{s+2}+1\pmod {2^{s+3}}\)

Euclid’s argument with \(M=\big ((2O^2+1)^{2^{s+1}}+1\big )/2\) and lifting the exponent at \(2\), for every \(s\).

\[ \forall s\ \forall B\ \exists c:\ c\cdot 2^{s+3}+2^{s+2}+1\ \text {prime and}\ >B. \]

lang/Inf2e1.lean

/-
  lang/Inf2e1 — T43, cycle 304: infinitely many primes p ≡ 2^(s+2) + 1 (mod 2^(s+3)), for every s (5 mod 8, 9 mod 16, 17 mod 32, …).

  x = 2O² + 1 ≡ 3 (mod 8) (O the odd factorial). Lifting the exponent: x^(2^(k+1)) = 8·2^k·(2u + 1) + 1 (lte), so
  x^(2^(s+1)) + 1 = 2M with M = u·2^(s+3) + 2^(s+2) + 1. A prime factor q of M has x^(2^(s+1)) = −1, x^(2^(s+2)) = 1 in 𝔽_q, so
  2^(s+2) | q − 1 (CycloPow.dvd_of_pow): q ≡ 1 or 2^(s+2) + 1 (mod 2^(s+3)). Descent (hasG) gives a factor in the class, and an odd
  q ≤ B divides O, hence x^n − 1, hence 2.
    pow_add', sq_pow, lte, one_pow_dv, uniq_remG, modG, cofactorG, hasG, allowedG, inf_2e1
  The classes of the primes that split in ℚ(ζ_{2^(s+2)}) but not in ℚ(ζ_{2^(s+3)}).
  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.Inf9mod16

namespace Inf2e1

open FTAChain ZMod InfSplit Inf9mod16

theorem pow_add' (a m : Nat) : ∀ n, a ^ (m + n) = a ^ m * a ^ n
  | 0 => by show a ^ m = a ^ m * 1; generalize a ^ m = y; lib_nat
  | n + 1 => by
    show a ^ (m + n) * a = a ^ m * (a ^ n * a)
    rw [pow_add' a m n]; generalize a ^ m = y; generalize a ^ n = z; lib_nat

theorem two_pow (k : Nat) : 2 ^ (k + 1) = 2 ^ k + 2 ^ k := by
  show 2 ^ k * 2 = _; generalize 2 ^ k = t; lib_nat

theorem sq_pow (a k : Nat) : a ^ (2 ^ (k + 1)) = a ^ (2 ^ k) * a ^ (2 ^ k) := by
  rw [two_pow, pow_add']

theorem pos_pow : ∀ s, ∃ R', 2 ^ s = R' + 1
  | 0 => ⟨0, rfl⟩
  | s + 1 => by
    match pos_pow s with
    | ⟨R', h⟩ => exact ⟨R' + R' + 1, by show 2 ^ s * 2 = _; rw [h]; lib_nat⟩

theorem base_sq (w : Nat) : (8 * w + 3) * (8 * w + 3) = 8 * 1 * (2 * (4 * (w * w) + 3 * w) + 1) + 1 := by
  rw [x2g 8 w]
  show 8 * (8 * w * w + 6 * w) + 9 = _
  rw [show 8 * w * w = 8 * (w * w) by lib_nat]
  generalize w * w = W
  lib_nat

theorem step_sq (R u : Nat) : (8 * R * (2 * u + 1) + 1) * (8 * R * (2 * u + 1) + 1)
    = 8 * (R * 2) * (2 * (2 * R * ((2 * u + 1) * (2 * u + 1)) + u) + 1) + 1 := by lib_nat

/-- **Lifting the exponent**: x ≡ 3 (mod 8) ⟹ x^(2^(k+1)) = 8·2^k·(2u + 1) + 1. -/
theorem lte (w : Nat) : ∀ k, ∃ u, (8 * w + 3) ^ (2 ^ (k + 1)) = 8 * 2 ^ k * (2 * u + 1) + 1
  | 0 => ⟨4 * (w * w) + 3 * w, by
      show 1 * (8 * w + 3) * (8 * w + 3) = 8 * 1 * _ + 1
      rw [show 1 * (8 * w + 3) * (8 * w + 3) = (8 * w + 3) * (8 * w + 3) by generalize 8 * w + 3 = y; lib_nat]
      exact base_sq w⟩
  | k + 1 => by
    match lte w k with
    | ⟨u, hu⟩ =>
      refine ⟨2 * 2 ^ k * ((2 * u + 1) * (2 * u + 1)) + u, ?_⟩
      rw [sq_pow, hu]
      exact step_sq (2 ^ k) u

/-- (O·Z + 1)^n = O·Y + 1. -/
theorem one_pow_dv (O Z : Nat) : ∀ n, ∃ Y, (O * Z + 1) ^ n = O * Y + 1
  | 0 => ⟨0, by show 1 = _; lib_nat⟩
  | n + 1 => by
    match one_pow_dv O Z n with
    | ⟨Y, hY⟩ => exact ⟨Y * Z * O + Y + Z, by show (O * Z + 1) ^ n * (O * Z + 1) = _; rw [hY]; lib_nat⟩

theorem uniq_remG (Nm x y r c : Nat) (hr : r < Nm) (hc : c < Nm) (h : x * Nm + r = y * Nm + c) : r = c := by
  have big : ∀ x y r c : Nat, r < Nm → x < y → x * Nm + r = y * Nm + c → False := fun x y r c hr hxy h => by
    have h1 : x * Nm + r < (x + 1) * Nm := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * Nm ≤ y * Nm := Nat.mul_le_mul_right Nm hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * Int.ofNat Nm + Int.ofNat r = Int.ofNat x * Int.ofNat Nm + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * Int.ofNat Nm + Int.ofNat r) - Int.ofNat x * Int.ofNat Nm := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

theorem modG (M' n : Nat) : ∃ a r, n = a * (M' + 1) + r ∧ r < M' + 1 :=
  match div_exists M' n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactorG (M' A b N' e : Nat) (hA : A < M' + 1) (h : (b * (M' + 1) + 1) * N' = e * (M' + 1) + A) : ∃ c, N' = c * (M' + 1) + A :=
  match modG M' N' with
  | ⟨c, r, hc, hr⟩ => by
    have e' : (b * (M' + 1) + 1) * N' = (b * (M' + 1) * c + b * r + c) * (M' + 1) + r := by rw [hc]; lib_nat
    rw [e'] at h
    exact ⟨c, by rw [hc, uniq_remG (M' + 1) _ _ r A hr hA h]⟩

/-- **Descent** for the class a' + 2 modulo M' + 1. -/
theorem hasG (M' a' N0 : Nat) (ha : a' + 1 + 1 < M' + 1)
    (hall : ∀ l, (∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) → Dv (l + 1 + 1) N0 →
      ∃ c t, l + 1 + 1 = c * (M' + 1) + t ∧ (t = 1 ∨ t = a' + 1 + 1)) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ e, N = e * (M' + 1) + (a' + 1 + 1)) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ c, q = c * (M' + 1) + (a' + 1 + 1)) ∧ Dv q N
  | 0, N, hN, _, ⟨e, he⟩ => absurd (he ▸ hN) (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hN, hN0, ⟨e, he⟩ => by
    match lp_exists (e * (M' + 1) + a') with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [he]; exact hl
    match hall l hq (dv_trans _ _ _ hlN hN0) with
    | ⟨c, t, hc, ht⟩ =>
      cases ht with
      | inr ha' => exact ⟨l + 1 + 1, hq, ⟨c, by rw [hc, ha']⟩, hlN⟩
      | inl h1 =>
        match hlN with
        | ⟨N', hN'⟩ =>
        match cofactorG M' (a' + 1 + 1) c N' e ha (by rw [← he, hN', hc, h1]; lib_nat) with
        | ⟨d, hd⟩ =>
        have hlt : N' ≤ G := by
          have e' : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
          have h1' : 1 ≤ N' * (l + 1) := by rw [hd]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e']; exact hN))
        have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
        match hasG M' a' N0 ha hall G N' hlt hN'0 ⟨d, hd⟩ with
        | ⟨q, hq', hqa, hqN⟩ => exact ⟨q, hq', hqa, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩

/-- prime factors of M (odd, 2M = x^(2^(s+1)) + 1) are ≡ 1 or 4·2^s + 1 (mod 8·2^s). -/
theorem allowedG (s x M l : Nat) (hM : x ^ (2 ^ (s + 1)) + 1 = 2 * M) (hodd : ∀ r, M ≠ 2 * r)
    (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (hdv : Dv (l + 1 + 1) M) :
    ∃ c t, l + 1 + 1 = c * (8 * 2 ^ s) + t ∧ (t = 1 ∨ t = 4 * 2 ^ s + 1) := by
  have hq2 : l + 1 + 1 ≠ 2 := fun e => by
    rw [e] at hdv
    match hdv with
    | ⟨w, hw⟩ => exact hodd w (by rw [hw]; lib_nat)
  have hdv' : Dv (l + 1 + 1) (x ^ (2 ^ (s + 1)) + 1) := by
    match hdv with
    | ⟨w, hw⟩ => exact ⟨2 * w, by rw [hM, hw]; lib_nat⟩
  have hxfull : Frobenius.pw (ring (l + 1)) (mk (l + 1) x) ((0 + 1 + 1) ^ (s + 1 + 1)) = (ring (l + 1)).one := by
    show Frobenius.pw (ring (l + 1)) (mk (l + 1) x) (2 ^ (s + 1 + 1)) = _
    rw [PrimeOneModQ.mk_pow, sq_pow]; exact Inf1mod24.sq_one l _ hdv'
  have hX1 : Frobenius.pw (ring (l + 1)) (mk (l + 1) x) ((0 + 1 + 1) ^ (s + 1)) ≠ (ring (l + 1)).one := by
    show Frobenius.pw (ring (l + 1)) (mk (l + 1) x) (2 ^ (s + 1)) ≠ _
    rw [PrimeOneModQ.mk_pow]; exact Inf1mod24.ne_one l _ hq2 hdv'
  match CycloPow.dvd_of_pow l hq 0 (s + 1) StrongMult1.two_prime _ hxfull hX1 with
  | ⟨k, hk⟩ =>
  have hk' : l + 1 + 1 = k * (4 * 2 ^ s) + 1 := by
    rw [hk]; show k * (2 ^ s * 2 * 2) + 1 = _; generalize 2 ^ s = R; lib_nat
  match par k with
  | ⟨j, hj⟩ =>
    cases hj with
    | inl h => exact ⟨j, 1, by rw [hk', h]; generalize 2 ^ s = R; lib_nat, Or.inl rfl⟩
    | inr h => exact ⟨j, 4 * 2 ^ s + 1, by rw [hk', h]; generalize 2 ^ s = R; lib_nat, Or.inr rfl⟩

/-- **Infinitely many primes p ≡ 2^(s+2) + 1 (mod 2^(s+3))**, written with R = 2^s: p = c·8R + 4R + 1. -/
theorem inf_2e1 (s B : Nat) : ∃ c, (∀ d, Dv d (c * (8 * 2 ^ s) + (4 * 2 ^ s + 1)) → d = 1 ∨ d = c * (8 * 2 ^ s) + (4 * 2 ^ s + 1))
    ∧ B < c * (8 * 2 ^ s) + (4 * 2 ^ s + 1) := by
  match oddf_sq B, pos_pow s with
  | ⟨C, hC⟩, ⟨R', hR⟩ =>
  have hx : 2 * (oddf B * oddf B) + 1 = 8 * (2 * C) + 3 := by rw [hC]; lib_nat
  match lte (2 * C) s with
  | ⟨u, hu⟩ =>
  have hM : (8 * (2 * C) + 3) ^ (2 ^ (s + 1)) + 1 = 2 * (u * (8 * 2 ^ s) + (4 * 2 ^ s + 1)) := by
    rw [hu]; generalize 2 ^ s = R; lib_nat
  have hodd : ∀ r, u * (8 * 2 ^ s) + (4 * 2 ^ s + 1) ≠ 2 * r := fun r e =>
    odd_not_dv2 (u * (4 * 2 ^ s) + 2 * 2 ^ s) r (by rw [show 2 * (u * (4 * 2 ^ s) + 2 * 2 ^ s) + 1 = u * (8 * 2 ^ s) + (4 * 2 ^ s + 1) by generalize 2 ^ s = R; lib_nat, e]; lib_nat)
  have hM' : 8 * 2 ^ s = (8 * R' + 7) + 1 := by rw [hR]; lib_nat
  have hA' : 4 * 2 ^ s + 1 = (4 * R' + 3) + 1 + 1 := by rw [hR]; lib_nat
  have hlt : (4 * R' + 3) + 1 + 1 < (8 * R' + 7) + 1 := Nat.succ_le_succ (by
    show 4 * R' + 3 + 1 + 1 ≤ 8 * R' + 7
    rw [show 8 * R' + 7 = (4 * R' + 3 + 1 + 1) + (4 * R' + 2) by lib_nat]; exact Nat.le_add_right _ _)
  match hasG (8 * R' + 7) (4 * R' + 3) (u * (8 * 2 ^ s) + (4 * 2 ^ s + 1)) hlt
      (fun l hq hdv => by
        match allowedG s _ _ l hM hodd hq hdv with
        | ⟨c, t, hc, ht⟩ => exact ⟨c, t, by rw [hc, hM'], by rw [← hA']; exact ht⟩)
      (u * (8 * 2 ^ s) + (4 * 2 ^ s + 1)) (u * (8 * 2 ^ s) + (4 * 2 ^ s + 1)) (Nat.le_refl _) ⟨1, by lib_nat⟩ ⟨u, by rw [hM', hA']⟩ with
  | ⟨q, hq, ⟨c, hc⟩, hqM⟩ =>
    have hc' : q = c * (8 * 2 ^ s) + (4 * 2 ^ s + 1) := by rw [hc, hM', hA']
    refine ⟨c, by rw [← hc']; exact hq, ?_⟩
    rw [← hc']
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      have hq2k : q = 2 * (c * (4 * 2 ^ s) + 2 * 2 ^ s) + 1 := by rw [hc']; generalize 2 ^ s = R; lib_nat
      have hk : c * (4 * 2 ^ s) + 2 * 2 ^ s < B := by
        have h2 : c * (4 * 2 ^ s) + 2 * 2 ^ s < 2 * (c * (4 * 2 ^ s) + 2 * 2 ^ s) + 1 :=
          Nat.lt_succ_of_le (by rw [show 2 * (c * (4 * 2 ^ s) + 2 * 2 ^ s) = (c * (4 * 2 ^ s) + 2 * 2 ^ s) + (c * (4 * 2 ^ s) + 2 * 2 ^ s) by generalize c * (4 * 2 ^ s) + 2 * 2 ^ s = y; lib_nat]; exact Nat.le_add_left _ _)
        exact Nat.lt_of_lt_of_le h2 (by rw [← hq2k]; exact h)
      have hqO : Dv q (oddf B) := by rw [hq2k]; exact oddf_dv B _ hk
      match one_pow_dv (oddf B) (2 * oddf B) (2 ^ (s + 1)) with
      | ⟨Y, hY⟩ =>
      have hq2M : Dv q (oddf B * Y + 1 + 1) := by
        rw [← hY, show oddf B * (2 * oddf B) + 1 = 8 * (2 * C) + 3 by rw [← hx]; generalize oddf B = o; lib_nat, hM]
        match hqM with
        | ⟨w, hw⟩ => exact ⟨2 * w, by rw [hw]; lib_nat⟩
      have hqOY : Dv q (oddf B * Y) := Inf17mod24.dv_mulr _ _ _ hqO
      have hq' : q = (c * (8 * 2 ^ s) + 4 * 2 ^ s) + 1 := by rw [hc']; generalize 2 ^ s = R; lib_nat
      rw [hq'] at hqOY hq2M
      have h2 := dv_sub (c * (8 * 2 ^ s) + 4 * 2 ^ s) _ 2 hqOY (by rw [show oddf B * Y + 2 = oddf B * Y + 1 + 1 from rfl]; exact hq2M)
      have hge : c * (8 * 2 ^ s) + 4 * 2 ^ s = (c * (8 * R' + 8) + 4 * R' + 2) + 1 + 1 := by rw [hR]; lib_nat
      rw [hge] at h2
      cases StrongMult1.two_prime _ h2 with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj e))

end Inf2e1

#print axioms Inf2e1.pow_add'
#print axioms Inf2e1.two_pow
#print axioms Inf2e1.sq_pow
#print axioms Inf2e1.pos_pow
#print axioms Inf2e1.base_sq
#print axioms Inf2e1.step_sq
#print axioms Inf2e1.lte
#print axioms Inf2e1.one_pow_dv
#print axioms Inf2e1.uniq_remG
#print axioms Inf2e1.modG
#print axioms Inf2e1.cofactorG
#print axioms Inf2e1.hasG
#print axioms Inf2e1.allowedG
#print axioms Inf2e1.inf_2e1

#print axioms: 14 theorems, no axioms.

78.94 The pencil second moment in closed form

The squares of the traces of \(y^2=x^3+ax+b\) summed over \(b\), for \(a\ne 0\) and \(p>3\).

\[ \sum _b\Big (\sum _x\chi (x^3+ax+b)\Big )^2=p^2-p-p\,\chi (-3)-p\,\chi (-3a). \]

lang/PencilClosed.lean

/-
  lang/PencilClosed — T43, cycle 305: the second moment over the pencil b ↦ y² = x³ + ax + b in closed form (p > 3, a ≠ 0).

  p = 2m + 3 ≠ 3, χ the Legendre symbol.
    sum_y2c      Σ_y χ(y² + c) = −1 for c ≠ 0                       (QuadDiagonal.conv, ConicSum.sum_xxd)
    delta_sub    δ(z − d, 0) = δ(z, d)
    quad_count   #{x : x² + xy + y² + a = 0} = 1 + χ(−3y² − 4a)      (complete the square)
    conic_count  #{(x, y) : x² + xy + y² + a = 0} = p − χ(−3)       (a ≠ 0)
    diag_count   #{x : 3x² + a = 0} = 1 + χ(−3a)
    N_closed     #{(x, y) : x³ + ax = y³ + ay} = 2p − 1 − χ(−3) − χ(−3a)
    pencil_closed  Σ_b (Σ_x χ(x³ + ax + b))² = p² − p − p χ(−3) − p χ(−3a)
    pencil_zero    a = 0, p ≡ 2 (mod 3): Σ_b (Σ_x χ(x³ + b))² = 0
    pencil_zero_all  a = 0, every p > 3: Σ_b (Σ_x χ(x³ + b))² = p(p − 1)(1 + χ(−3))
    quad_sum, quad_sum0  Σ_x χ(x² + bx + c) = −1 if b² − 4c ≠ 0, p − 1 if b² − 4c = 0
    conic_points, conic_points0  #{y² = x² + bx + c} = p − 1, resp. 2p − 1
    quad_sum_a, quad_sum_a0, conic_points_a   a ≠ 0: Σ_x χ(ax² + bx + c) = −χ(a), resp. (p − 1)χ(a); #{y² = ax² + bx + c} = p − χ(a)
    binary_form   a, b, c ≠ 0: #{ax² + by² = c} = p − χ(−ab)
    binary_form0  b ≠ 0: #{ax² + by² = 0} = p + (p − 1)χ(−ab)
    ternary_form  a, b, c ≠ 0, any d: #{ax² + by² + cz² = d} = p² + pχ(−abcd)
    quaternary_form  a, b, c, d, e ≠ 0: #{ax² + by² + cz² + dw² = e} = p³ − pχ(abcd)
    quaternary_form0  a, b, c, d ≠ 0: #{ax² + by² + cz² + dw² = 0} = p³ + (p² − p)χ(abcd)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.PencilMoment
import lang.QuadDiagonal
import lang.CMEisen

namespace PencilClosed

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal QuadTwist JacobMoment PencilMoment
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

theorem nzm (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).mul x y ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  cases nz2 m hpr _ _ e with
  | inl h => exact hx h
  | inr h => exact hy h

theorem negnz (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).neg x ≠ (ring (2 * m + 1 + 1)).zero := fun e => hx (by
  calc x = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg x) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring)

theorem chi_unit (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (t : Zp (2 * m + 1 + 1)) (ht : t ≠ (ring (2 * m + 1 + 1)).zero) : chi m t * chi m t = 1 :=
  QuadDiagonal.chi_sq_val m hpr t ht

theorem chi_iv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (t : Zp (2 * m + 1 + 1)) (ht : t ≠ (ring (2 * m + 1 + 1)).zero) : chi m (iv m t) = chi m t := by
  have h1 : chi m (iv m t) * chi m t = 1 := by rw [← chi_mul m hpr, iv_mul_self m hpr t ht]; exact chi_one m
  calc chi m (iv m t) = chi m (iv m t) * (chi m t * chi m t) := by rw [chi_unit m hpr t ht]; lib_int
    _ = (chi m (iv m t) * chi m t) * chi m t := by lib_int
    _ = chi m t := by rw [h1]; lib_int

/-- **Σ_y χ(y² + c) = −1** for c ≠ 0. -/
theorem sum_y2c (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) c)) (L m) = -1 := by
  rw [QuadDiagonal.conv m hpr (fun t => chi m ((ring (2 * m + 1 + 1)).add t c))]
  rw [sumK_ext LibInt.intR _ (fun t => chi m ((ring (2 * m + 1 + 1)).add t c) + chi m ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).add t c))) (fun t => by
    rw [chi_mul m hpr]; show (1 + chi m t) * chi m ((ring (2 * m + 1 + 1)).add t c) = _; lib_int)]
  rw [QuadDiagonal.sI_add, shift_sum m (chi m) c, show sumK LibInt.intR (chi m) (L m) = 0 from chi_sum m hpr, sum_xxd m hpr c hc]
  lib_int

theorem delta_sub (z d : Zp (2 * m + 1 + 1)) : delta LibInt.intR ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg d)) (ring (2 * m + 1 + 1)).zero = delta LibInt.intR z d := by
  by_cases h : z = d
  · rw [delta_one m _ _ h, delta_one m _ _ (by rw [h]; lib_ring)]
  · rw [delta_zero m _ _ h, delta_zero m _ _ (fun e => h (by
      calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg d)) d := by lib_ring
        _ = d := by rw [e]; lib_ring))]

theorem iv_nz (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (t : Zp (2 * m + 1 + 1)) (ht : t ≠ (ring (2 * m + 1 + 1)).zero) : iv m t ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  have h := iv_mul_self m hpr t ht
  rw [e] at h
  exact PowerSum.one_ne_zero' (2 * m + 1) (by rw [← h]; lib_ring)

/-- **#{y : y² + xy + x² + a = 0} = 1 + χ(−3x² − 4a).** -/
theorem quad_count (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) (L m) = 1 + chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (rin  -- (+403 chars; full line in the Zenodo source)
  have h2 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := QuadDiagonal.two_ne m
  have e2 : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h2
  have hS : ∀ y, (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)  -- (+164 chars; full line in the Zenodo source)
      ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) x) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 *  -- (+42 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y))) = (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).  -- (+237 chars; full line in the Zenodo source)
      _ = _ := by lib_ring
  rw [sumK_ext LibInt.intR _ _ (fun y => by rw [hS y, delta_sub])]
  rw [shift_sum m (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) x) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) x)) ((ring (2 * m + 1 + 1)  -- (+73 chars; full line in the Zenodo source)
  rw [QuadDiagonal.sq_count m hpr]
  have h4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)  -- (+226 chars; full line in the Zenodo source)
      = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).  -- (+125 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m   -- (+134 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) a))) := by lib_ring
      _ = _ := by rw [e2]; lib_ring
  rw [← h4, chi_mul m hpr, chi_sq m hpr _ h2]
  lib_int

/-- **#{(x, y) : x² + xy + y² + a = 0} = p − χ(−3)** for a ≠ 0, p ≠ 3. -/
theorem conic_count (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) (L m)) (L m) = P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).  -- (+94 chars; full line in the Zenodo source)
  have h2 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := QuadDiagonal.two_ne m
  have e3 : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h3
  have hc : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul a (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1  -- (+117 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ _ (fun x => quad_count m hpr a x)]
  have hT : ∀ x, chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (  -- (+798 chars; full line in the Zenodo source)
    have hTe : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1  -- (+792 chars; full line in the Zenodo source)
      symm
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1  -- (+484 chars; full line in the Zenodo source)
        _ = _ := by rw [e3]; lib_ring
    rw [hTe, chi_mul m hpr]
  rw [sumK_ext LibInt.intR _ (fun x => 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) * chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ri  -- (+313 chars; full line in the Zenodo source)
    QuadDiagonal.sI_add, QuadDiagonal.sI_mul, sum_one, sum_y2c m hpr _ hc]
  lib_int

/-- **#{x : 3x² + a = 0} = 1 + χ(−3a).** -/
theorem diag_count (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (a : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun x => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)))) (ring (2 * m + 1 + 1)).zero) (L m) = 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((rin  -- (+88 chars; full line in the Zenodo source)
  have e3 : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h3
  have hi3 := iv_nz m hpr _ h3
  have hpt : ∀ x, delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)))) (ring (2 * m + 1 + 1)).zero = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (iv m ((ring (2 * m + 1 + 1)).add (ri  -- (+125 chars; full line in the Zenodo source)
    have hS : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ri  -- (+270 chars; full line in the Zenodo source)
      symm
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ) ((ring (2 *   -- (+159 chars; full line in the Zenodo source)
        _ = _ := by rw [e3]; lib_ring
    rw [hS, ← delta_sub m ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))))]
    by_cases hz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))))) = (ring (2 * m + 1 + 1)).zero
    · rw [hz, delta_one m _ _ rfl, delta_one m _ _ (by lib_ring)]
    · rw [delta_zero m _ _ hz, delta_zero m _ _ (nzm m hpr _ _ h3 hz)]
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sq_count m hpr]
  have : (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 +   -- (+367 chars; full line in the Zenodo source)
    symm
    calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 +  -- (+174 chars; full line in the Zenodo source)
      _ = _ := by rw [e3]; lib_ring
  rw [this, chi_mul m hpr, chi_mul m hpr, chi_unit m hpr _ hi3]
  lib_int

theorem split (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) :
    delta LibInt.intR (u m a x) (u m a y) = delta LibInt.intR y x + delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero + -(delta LibInt.intR y x * delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)  -- (+170 chars; full line in the Zenodo source)
  by_cases hxy : y = x
  · rw [hxy, delta_one m _ _ rfl, delta_one m _ _ rfl]
    generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)))) (ring (2 * m + 1 + 1)).zero = d
    lib_int
  · rw [delta_zero m _ _ hxy]
    have hs : delta LibInt.intR (u m a x) (u m a y) = delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero := by
      by_cases ha : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y))) = (ring (2 * m + 1 + 1)).zero
      · rw [delta_one m _ _ ha, delta_one m _ _ (by
          unfold u
          calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)
              = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).mul a y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)  -- (+62 chars; full line in the Zenodo source)
            _ = _ := by rw [ha]; lib_ring)]
      · rw [delta_zero m _ _ ha, delta_zero m _ _ (fun e => by
          unfold u at e
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) = (ring (2 * m + 1 + 1)).zero := by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).mul a y))) := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact hxy (by
              calc y = (ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg y)) := by rw [h]; lib_ring
                _ = x := by lib_ring)
          | inr h => exact ha h)]
    rw [hs]
    generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero = d
    lib_int

theorem inner_eq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a y)) (L m)
      = 1 + sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) (L m) + -(delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring   -- (+124 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ _ (split m hpr a x)]
  rw [sumK_ext LibInt.intR _ (fun y => (delta LibInt.intR y x + delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) + (-1) * LibInt.intR.mul (delta LibInt.intR y x) (delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((r  -- (+197 chars; full line in the Zenodo source)
    show _ = _ + -1 * (delta LibInt.intR y x * delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero); generalize delta LibInt.intR y x = e; generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 +   -- (+180 chars; full line in the Zenodo source)
  rw [QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_mul,
    FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) x,
    show sumK LibInt.intR (fun y => delta LibInt.intR y x) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR x]
  lib_int

/-- **#{(x, y) : x³ + ax = y³ + ay} = 2p − 1 − χ(−3) − χ(−3a)** for a ≠ 0, p ≠ 3. -/
theorem N_closed (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (u m a x) (u m a y)) (L m)) (L m)
      = P m + P m + -1 + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).on  -- (+13 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ _ (inner_eq m hpr a)]
  rw [sumK_ext LibInt.intR _ (fun x => (1 + sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) (L m)) + (-1) * delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((  -- (+169 chars; full line in the Zenodo source)
    show _ = _ + -1 * _; generalize sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x y) ((ring (2 * m + 1 + 1)).mul y y)))) (ring (2 * m + 1 + 1)).zero) (L m) = c; generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).add ((ri  -- (+168 chars; full line in the Zenodo source)
  rw [QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_mul, sum_one, conic_count m hpr h3 a ha, diag_count m hpr h3 a]
  lib_int

/-- **The second moment over the pencil, in closed form**: Σ_b (Σ_x χ(x³ + ax + b))² = p² − p − pχ(−3) − pχ(−3a), a ≠ 0, p > 3. -/
theorem pencil_closed (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun b => W m a b * W m a b) (L m)
      = P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring   -- (+33 chars; full line in the Zenodo source)
  rw [pencil_moment m hpr a, N_closed m hpr h3 a ha]
  lib_int

theorem cube_pw (z : Zp (2 * m + 1 + 1)) : Frobenius.pw (ring (2 * m + 1 + 1)) z 3 = (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z) := by
  show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one z) z) z = _; lib_ring

/-- **The pencil b ↦ y² = x³ + b at p ≡ 2 (mod 3)**: Σ_b (Σ_x χ(x³ + b))² = 0 (x ↦ x³ is a bijection, 3e = 2(p − 1) + 1). -/
theorem pencil_zero (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (e : Nat) (he : 3 * e = 2 * (2 * m + 1 + 1) + 1) :
    sumK LibInt.intR (fun b => W m (ring (2 * m + 1 + 1)).zero b * W m (ring (2 * m + 1 + 1)).zero b) (L m) = 0 := by
  rw [pencil_moment m hpr (ring (2 * m + 1 + 1)).zero]
  have hpt : ∀ x y, delta LibInt.intR (u m (ring (2 * m + 1 + 1)).zero x) (u m (ring (2 * m + 1 + 1)).zero y) = delta LibInt.intR y x := fun x y => by
    by_cases hxy : y = x
    · rw [hxy, delta_one m _ _ rfl, delta_one m _ _ rfl]
    · rw [delta_zero m _ _ hxy, delta_zero m _ _ (fun h => hxy (by
        have h' : Frobenius.pw (ring (2 * m + 1 + 1)) x 3 = Frobenius.pw (ring (2 * m + 1 + 1)) y 3 := by
          rw [cube_pw, cube_pw]
          calc (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) = u m (ring (2 * m + 1 + 1)).zero x := by unfold u; lib_ring
            _ = u m (ring (2 * m + 1 + 1)).zero y := h
            _ = _ := by unfold u; lib_ring
        calc y = Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) y 3) e := (CMEisen.cube_inv (2 * m + 1) hpr e he y).symm
          _ = Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) x 3) e := by rw [h']
          _ = x := CMEisen.cube_inv (2 * m + 1) hpr e he x))]
  rw [sumK_ext LibInt.intR _ (fun _ => (1 : Int)) (fun x => by
    rw [sumK_ext LibInt.intR _ _ (hpt x)]; exact CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR x), sum_one]
  lib_int

/-- **The pencil y² = x³ + b, every p > 3**: Σ_b (Σ_x χ(x³ + b))² = p(p − 1)(1 + χ(−3)) (Birch's total minus the closed forms a ≠ 0). -/
theorem pencil_zero_all (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun b => W m (ring (2 * m + 1 + 1)).zero b * W m (ring (2 * m + 1 + 1)).zero b) (L m) = (P m * P m + -(P m)) * (1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) := by
  have hb := birch_moment2 m hpr
  generalize hS0 : sumK LibInt.intR (fun b => W m (ring (2 * m + 1 + 1)).zero b * W m (ring (2 * m + 1 + 1)).zero b) (L m) = S0
  have hpt : ∀ a, sumK LibInt.intR (fun b => W m a b * W m a b) (L m)
      = (P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring  -- (+552 chars; full line in the Zenodo source)
    by_cases ha : a = (ring (2 * m + 1 + 1)).zero
    · rw [ha, delta_one m _ _ rfl, hS0]; show S0 = _ + 1 * (S0 + -_); lib_int
    · rw [pencil_closed m hpr h3 a ha, delta_zero m _ _ ha]; show _ = _ + 0 * _; lib_int
  rw [sumK_ext LibInt.intR _ (fun a => LibInt.intR.add ((P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 *   -- (+614 chars; full line in the Zenodo source)
  rw [sumK_add, ← sumK_mul, show sumK LibInt.intR (fun a => delta LibInt.intR a (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero] at hb
  have hf : sumK LibInt.intR (fun a => P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (r  -- (+285 chars; full line in the Zenodo source)
    rw [sumK_ext LibInt.intR _ (fun a => (P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))))) * 1 + (-(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)  -- (+61 chars; full line in the Zenodo source)
      show _ = _
      rw [show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) a) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)  -- (+49 chars; full line in the Zenodo source)
    rw [QuadDiagonal.sI_add, QuadDiagonal.sI_mul, QuadDiagonal.sI_mul, sum_one, show sumK LibInt.intR (chi m) (L m) = 0 from chi_sum m hpr]
    lib_int
  rw [hf] at hb
  have hf0 : (P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one   -- (+285 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero by lib_ring, show (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero by lib_ring, chi_zero]
  rw [hf0] at hb
  have hb' : (P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))))) * P m + 1 * (S0 + -(P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m +   -- (+96 chars; full line in the Zenodo source)
  calc S0 = ((P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))))) * P m + 1 * (S0 + -(P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m +   -- (+510 chars; full line in the Zenodo source)
    _ = (P m * P m * P m + -(P m * P m)) + -((P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))))) * P m) + (P m * P m + -(P m) + -(P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1  -- (+93 chars; full line in the Zenodo source)
    _ = _ := by lib_int

theorem quad_shift (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x : Zp (2 * m + 1 + 1)) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m  -- (+455 chars; full line in the Zenodo source)
  have e2 : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ (QuadDiagonal.two_ne m)
  symm
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b x) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) c) := by lib_ring
    _ = _ := by rw [e2]; lib_ring

theorem disc4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c : Zp (2 * m + 1 + 1)) : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2  -- (+588 chars; full line in the Zenodo source)
  have e2 : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ (QuadDiagonal.two_ne m)
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m +   -- (+402 chars; full line in the Zenodo source)
    _ = _ := by rw [e2]; lib_ring

/-- **Σ_x χ(x² + bx + c) = −1** when b² − 4c ≠ 0. -/
theorem quad_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c : Zp (2 * m + 1 + 1)) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).on  -- (+67 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m) = -1 := by
  have hd : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)) ((ring (2 * m + 1 + 1)).neg c) ≠ (ring (2 * m + 1 + 1)).zero := fun e => hD (by rw [← disc  -- (+26 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ _ (fun x => by rw [quad_shift m hpr b c x])]
  rw [shift_sum m (fun z => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 *   -- (+178 chars; full line in the Zenodo source)
  exact sum_y2c m hpr _ (negnz m _ hd)

/-- **Σ_x χ(x² + bx + c) = p − 1** when b² − 4c = 0. -/
theorem quad_sum0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c : Zp (2 * m + 1 + 1)) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).o  -- (+68 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m) = P m + -1 := by
  have hd : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)) ((ring (2 * m + 1 + 1)).neg c) = (ring (2 * m + 1 + 1)).zero := by
    have h4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := nzm m hpr _ _ (QuadDiagonal.two_ne m) (QuadDiagonal.two_ne m)
    have := disc4 m hpr b c
    rw [hD] at this
    cases nz2 m hpr _ _ this with
    | inl h => exact absurd h h4
    | inr h => exact h
  rw [sumK_ext LibInt.intR _ _ (fun x => by rw [quad_shift m hpr b c x, hd])]
  rw [shift_sum m (fun z => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)]
  rw [sumK_ext LibInt.intR _ (fun z => 1 + -1 * delta LibInt.intR z (ring (2 * m + 1 + 1)).zero) (fun z => by
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero) = (ring (2 * m + 1 + 1)).mul z z by lib_ring]
    exact sq_delta m hpr z)]
  rw [QuadDiagonal.sI_add, QuadDiagonal.sI_mul, sum_one, show sumK LibInt.intR (fun z => delta LibInt.intR z (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
  lib_int

/-- **#{(x, y) : y² = x² + bx + c} = p − 1** when b² − 4c ≠ 0 (a smooth affine conic with two points at infinity). -/
theorem conic_points (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c : Zp (2 * m + 1 + 1)) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)  -- (+71 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m)) (L m) = P m + -1 := by
  rw [sumK_ext LibInt.intR _ _ (fun x => QuadDiagonal.sq_count m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))), QuadDiagonal.sI_add, sum_one, quad_sum m hpr b c hD]

/-- **#{(x, y) : y² = x² + bx + c} = 2p − 1** when b² = 4c (two crossing lines). -/
theorem conic_points0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c : Zp (2 * m + 1 + 1)) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1  -- (+72 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m)) (L m) = P m + P m + -1 := by
  rw [sumK_ext LibInt.intR _ _ (fun x => QuadDiagonal.sq_count m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))), QuadDiagonal.sI_add, sum_one, quad_sum0 m hpr b c hD]
  lib_int

theorem scale_q (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c x : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c) = (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m a) b) x) ((ring (2 * m + 1 + 1)).mul (iv m a) c))) := by
  have e : (ring (2 * m + 1 + 1)).mul (iv m a) a = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr a ha
  symm
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a (iv m a)) ((ring (2 * m + 1 + 1)).mul b x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a (iv m a)) c)) := by lib_ring
    _ = _ := by rw [show (ring (2 * m + 1 + 1)).mul a (iv m a) = (ring (2 * m + 1 + 1)).one by rw [((ring (2 * m + 1 + 1))).mul_comm]; exact e]; lib_ring

theorem disc_scale (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m a) b) ((ring (2 * m + 1 + 1)).mul (iv m a) b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2  -- (+462 chars; full line in the Zenodo source)
  have e : (ring (2 * m + 1 + 1)).mul (iv m a) a = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr a ha
  symm
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m a) (iv m a)) ((ring (2 * m + 1 + 1)).mul b b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((  -- (+123 chars; full line in the Zenodo source)
    _ = _ := by rw [show (ring (2 * m + 1 + 1)).mul a (iv m a) = (ring (2 * m + 1 + 1)).one by rw [((ring (2 * m + 1 + 1))).mul_comm]; exact e]; lib_ring

/-- **Σ_x χ(ax² + bx + c) = −χ(a)** when a ≠ 0 and b² − 4ac ≠ 0. -/
theorem quad_sum_a (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2   -- (+141 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m) = -chi m a := by
  rw [sumK_ext LibInt.intR _ _ (fun x => by rw [scale_q m hpr a b c x ha, chi_mul m hpr]), QuadDiagonal.sI_mul,
    quad_sum m hpr _ _ (by rw [disc_scale m hpr a b c ha]; exact nzm m hpr _ _ (nzm m hpr _ _ (iv_nz m hpr a ha) (iv_nz m hpr a ha)) hD)]
  lib_int

/-- **Σ_x χ(ax² + bx + c) = (p − 1)χ(a)** when a ≠ 0 and b² = 4ac. -/
theorem quad_sum_a0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2  -- (+142 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m) = (P m + -1) * chi m a := by
  rw [sumK_ext LibInt.intR _ _ (fun x => by rw [scale_q m hpr a b c x ha, chi_mul m hpr]), QuadDiagonal.sI_mul,
    quad_sum0 m hpr _ _ (by rw [disc_scale m hpr a b c ha, hD]; lib_ring)]
  lib_int

/-- **#{(x, y) : y² = ax² + bx + c} = p − χ(a)** when a ≠ 0, b² ≠ 4ac. -/
theorem conic_points_a (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring  -- (+145 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))) (L m)) (L m) = P m + -chi m a := by
  rw [sumK_ext LibInt.intR _ _ (fun x => QuadDiagonal.sq_count m hpr ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c))), QuadDiagonal.sI_add, sum_one, quad_sum_a m hpr a b c ha hD]

theorem delta_scale (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b z t : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) :
    delta LibInt.intR ((ring (2 * m + 1 + 1)).mul b z) t = delta LibInt.intR z ((ring (2 * m + 1 + 1)).mul (iv m b) t) := by
  have e : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  by_cases h : z = (ring (2 * m + 1 + 1)).mul (iv m b) t
  · rw [delta_one m _ _ h, delta_one m _ _ (by
      rw [h]
      calc (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (iv m b) t) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m b) b) t := by lib_ring
        _ = t := by rw [e]; lib_ring)]
  · rw [delta_zero m _ _ h, delta_zero m _ _ (fun h' => h (by
      calc z = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m b) b) z := by rw [e]; lib_ring
        _ = (ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).mul b z) := by lib_ring
        _ = _ := by rw [h']))]

/-- **#{(x, y) : ax² + by² = c} = p − χ(−ab)** for a, b, c ≠ 0. -/
theorem binary_form (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) c) (L m)) (L m)
      = P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) := by
  have hib := iv_nz m hpr b hb
  have hpt : ∀ x y, delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) c
      = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero x) ((ring (2 * m + 1 + 1)).mul (iv m b) c))) := fun x y => by
    rw [← delta_sub, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).neg c)
        = (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))))))) by
          have e : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
          symm
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m b) b) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)))))) := by lib_ring
            _ = _ := by rw [e]; lib_ring]
    by_cases hz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)))))) = (ring (2 * m + 1 + 1)).zero
    · rw [hz, delta_one m _ _ (by lib_ring), delta_one m _ _ (by
        calc (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))))))) ((ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).add c ((ring   -- (+102 chars; full line in the Zenodo source)
          _ = _ := by rw [hz]; lib_ring)]
    · rw [delta_zero m _ _ (nzm m hpr _ _ hb hz), delta_zero m _ _ (fun e => hz (by rw [e]; lib_ring))]
  rw [sumK_ext LibInt.intR _ _ (fun x => by rw [sumK_ext LibInt.intR _ _ (hpt x), QuadDiagonal.sq_count m hpr])]
  rw [QuadDiagonal.sI_add, sum_one]
  have hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).m  -- (+150 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).  -- (+115 chars; full line in the Zenodo source)
        = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m b) (iv m b)) ((ring (2 * m + 1 + 1)).mul a c)) by lib_ring]
    exact nzm m hpr _ _ (nzm m hpr _ _ (QuadDiagonal.two_ne m) (QuadDiagonal.two_ne m)) (nzm m hpr _ _ (nzm m hpr _ _ hib hib) (nzm m hpr _ _ ha hc))
  rw [quad_sum_a m hpr _ _ _ (nzm m hpr _ _ (negnz m _ hib) ha) hD]
  rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) ((ring (2 * m + 1 + 1)).mul (iv m b) (iv m b)) by
    have e : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
    symm
    calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).mul (iv m b) b))) := by lib_ring
      _ = _ := by rw [e]; lib_ring, chi_mul m hpr, chi_mul m hpr, chi_unit m hpr _ hib]
  lib_int

/-- **#{(x, y) : ax² + by² = 0} = p + (p − 1)χ(−ab)** for b ≠ 0. -/
theorem binary_form0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) (ring (2 * m + 1 + 1)).zero) (L m)) (L m)
      = P m + (P m + -1) * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) := by
  have hib := iv_nz m hpr b hb
  have e : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  have hpt : ∀ x y, delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) (ring (2 * m + 1 + 1)).zero
      = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a) ((ring (2 * m + 1 + 1)).mul x x)) := fun x y => by
    have hS : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))
        = (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a) ((ring (2 * m + 1 + 1)).mul x x)))) := by
      symm
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m b) b) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y)) := by lib_ring
        _ = _ := by rw [e]; lib_ring
    rw [hS, ← delta_sub m ((ring (2 * m + 1 + 1)).mul y y)]
    by_cases hz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a) ((ring (2 * m + 1 + 1)).mul x x))) = (ring (2 * m + 1 + 1)).zero
    · rw [hz, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl]
    · rw [delta_zero m _ _ (nzm m hpr _ _ hb hz), delta_zero m _ _ hz]
  have hc : chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a) = chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) := by
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (iv m b)) a = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) ((ring (2 * m + 1 + 1)).mul (iv m b) (iv m b)) by
      symm
      calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (iv m b) ((ring (2 * m + 1 + 1)).mul (iv m b) b))) := by lib_ring
        _ = _ := by rw [e]; lib_ring, chi_mul m hpr, chi_mul m hpr (iv m b) (iv m b), chi_unit m hpr _ hib]
    lib_int
  rw [sumK_ext LibInt.intR _ (fun x => 1 * 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * 1 + (-chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (fun x => by
    rw [sumK_ext LibInt.intR _ _ (hpt x), QuadDiagonal.sq_count m hpr, chi_mul m hpr, hc, sq_delta m hpr]
    show 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * (1 + -1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) = _
    generalize chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) = t; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = d; lib_int)]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (1 * 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * 1) 1) (LibInt.intR.mul (-chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero))) (fun x => by
    show _ = (1 * 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * 1) * 1 + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero; lib_int),
    sumK_add, ← mul_sumK, ← mul_sumK, sum_one, show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
  show (1 * 1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * 1) * P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * 1 = _
  lib_int

/-- **#{(x, y, z) : ax² + by² + cz² = d} = p² + pχ(−abcd)** for a, b, c ≠ 0 (any d). -/
theorem ternary_form (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c d : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun z => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) d) (L m)) (L m)) (L m)
      = P m * P m + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c d))) := by
  have hz : ∀ z, sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) d) (L m)) (L m)
      = (P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * delta LibInt.intR ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z)) d := fun z => by
    have hpt : ∀ x y, delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) d = delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x   -- (+209 chars; full line in the Zenodo source)
      rw [← delta_sub m _ d, ← delta_sub m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).add d ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z)))), show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+237 chars; full line in the Zenodo source)
        = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add d ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))))) by lib_ring]
    rw [sumK_ext LibInt.intR _ _ (fun x => sumK_ext LibInt.intR _ _ (hpt x) _)]
    by_cases h0 : (ring (2 * m + 1 + 1)).add d ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) = (ring (2 * m + 1 + 1)).zero
    · rw [h0, binary_form0 m hpr a b hb, delta_one m _ _ (by
        calc (ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z) = (ring (2 * m + 1 + 1)).add d ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add d ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))))) := by lib_ring
          _ = d := by rw [h0]; lib_ring)]
      lib_int
    · rw [binary_form m hpr a b _ ha hb h0, delta_zero m _ _ (fun e => h0 (by rw [e]; lib_ring))]
      lib_int
  rw [sumK_ext LibInt.intR _ _ hz]
  rw [sumK_ext LibInt.intR _ (fun z => LibInt.intR.add (LibInt.intR.mul (P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) 1) (LibInt.intR.mul (P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) (delta LibInt.intR ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z)) d))) (fun z => by
    show _ = (P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) * 1 + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * _; lib_int), sumK_add, ← mul_sumK, ← mul_sumK, sum_one]
  have hcnt : sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z)) d) (L m) = 1 + chi m ((ring (2 * m + 1 + 1)).mul c d) := by
    rw [sumK_ext LibInt.intR _ _ (fun z => delta_scale m hpr c _ d hc), QuadDiagonal.sq_count m hpr, chi_mul m hpr, chi_iv m hpr c hc, ← chi_mul m hpr]
  rw [hcnt]
  rw [show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c d)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) ((ring (2 * m + 1 + 1)).mul c d) by lib_ring, chi_mul m hpr ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) ((ring (2 * m + 1 + 1)).mul c d)]
  show (P m + -chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b))) * P m + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a b)) * (1 + chi m ((ring (2 * m + 1 + 1)).mul c d)) = _
  lib_int

/-- **#{(x, y, z, w) : ax² + by² + cz² + dw² = e} = p³ − pχ(abcd)** for a, b, c, d, e ≠ 0. -/
theorem quaternary_form (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c d e : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hc : c ≠ (ring (2 * m + 1 + 1)).zero)
    (hd : d ≠ (ring (2 * m + 1 + 1)).zero) (he : e ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun w => sumK LibInt.intR (fun z => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul  -- (+102 chars; full line in the Zenodo source)
      = P m * P m * P m + -(P m * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c d))) := by
  have hw : ∀ w, sumK LibInt.intR (fun z => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring  -- (+81 chars; full line in the Zenodo source)
      = P m * P m + P m * (chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) * chi m ((ring (2 * m + 1 + 1)).add e ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))))) := fun w => by
    have hpt : ∀ z x y, delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))) e = delta LibIn  -- (+399 chars; full line in the Zenodo source)
      rw [← delta_sub m _ e, ← delta_sub m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).add e ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 +  -- (+423 chars; full line in the Zenodo source)
        = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add e ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul  -- (+52 chars; full line in the Zenodo source)
    rw [sumK_ext LibInt.intR _ _ (fun z => sumK_ext LibInt.intR _ _ (fun x => sumK_ext LibInt.intR _ _ (hpt z x) _) _), ternary_form m hpr a b c _ ha hb hc,
      show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).add e ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w)))))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) ((ring (2 * m + 1 + 1))  -- (+221 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ (fun w => LibInt.intR.add (LibInt.intR.mul (P m * P m) 1) (LibInt.intR.mul (P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c))) (chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg d) ((ring (2 * m + 1 + 1)).mul w w)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring  -- (+45 chars; full line in the Zenodo source)
      rw [hw w, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg d) ((ring (2 * m + 1 + 1)).mul w w)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero w) e) = (ring (2 * m + 1 + 1)).add e ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))) by lib_ring]
      show _ = P m * P m * 1 + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) * chi m ((ring (2 * m + 1 + 1)).add e ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w)))); lib_int),
    sumK_add, ← mul_sumK, ← mul_sumK, sum_one,
    quad_sum_a m hpr _ _ _ (negnz m _ hd) (by
      rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)  -- (+311 chars; full line in the Zenodo source)
      exact nzm m hpr _ _ (nzm m hpr _ _ (QuadDiagonal.two_ne m) (QuadDiagonal.two_ne m)) (nzm m hpr _ _ hd he))]
  rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c d) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) ((ring (2 * m + 1 + 1)).neg d) by lib_ring, chi_mul m hpr ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) ((ring (2 * m + 1 + 1))  -- (+8 chars; full line in the Zenodo source)
  show P m * P m * P m + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) * -chi m ((ring (2 * m + 1 + 1)).neg d) = _
  lib_int

/-- **#{(x, y, z, w) : ax² + by² + cz² + dw² = 0} = p³ + (p² − p)χ(abcd)** for a, b, c, d ≠ 0. -/
theorem quaternary_form0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c d : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hc : c ≠ (ring (2 * m + 1 + 1)).zero)
    (hd : d ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun w => sumK LibInt.intR (fun z => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul  -- (+128 chars; full line in the Zenodo source)
      = P m * P m * P m + (P m * P m + -(P m)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c d)) := by
  have hw : ∀ w, sumK LibInt.intR (fun z => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring  -- (+107 chars; full line in the Zenodo source)
      = P m * P m + P m * (chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w)))) := fun w => by
    have hpt : ∀ z x y, delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))) (ring (2 * m +   -- (+394 chars; full line in the Zenodo source)
      rw [← delta_sub m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y y))) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))), show (ring (2 * m + 1 + 1)).add   -- (+727 chars; full line in the Zenodo source)
    rw [sumK_ext LibInt.intR _ _ (fun z => sumK_ext LibInt.intR _ _ (fun x => sumK_ext LibInt.intR _ _ (hpt z x) _) _), ternary_form m hpr a b c _ ha hb hc,
      show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mu  -- (+159 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ (fun w => LibInt.intR.add (LibInt.intR.mul (P m * P m) 1) (LibInt.intR.mul (P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c))) (chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg d) ((ring (2 * m + 1 + 1)).mul w w)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring  -- (+71 chars; full line in the Zenodo source)
      rw [hw w, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg d) ((ring (2 * m + 1 + 1)).mul w w)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero w) (ring (2 * m + 1 + 1)).zero) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w)) by lib_ring]
      show _ = P m * P m * 1 + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul w w))); lib_int),
    sumK_add, ← mul_sumK, ← mul_sumK, sum_one, quad_sum_a0 m hpr _ _ _ (negnz m _ hd) (by lib_ring)]
  rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) ((ring (2 * m + 1 + 1)).mul c d) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) ((ring (2 * m + 1 + 1)).neg d) by lib_ring, chi_mul m hpr ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) ((ring (2 * m + 1 + 1))  -- (+8 chars; full line in the Zenodo source)
  show P m * P m * P m + P m * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a b) c)) * ((P m + -1) * chi m ((ring (2 * m + 1 + 1)).neg d)) = _
  lib_int

end PencilClosed

#print axioms PencilClosed.nzm
#print axioms PencilClosed.negnz
#print axioms PencilClosed.chi_unit
#print axioms PencilClosed.chi_iv
#print axioms PencilClosed.sum_y2c
#print axioms PencilClosed.delta_sub
#print axioms PencilClosed.iv_nz
#print axioms PencilClosed.quad_count
#print axioms PencilClosed.conic_count
#print axioms PencilClosed.diag_count
#print axioms PencilClosed.split
#print axioms PencilClosed.inner_eq
#print axioms PencilClosed.N_closed
#print axioms PencilClosed.pencil_closed
#print axioms PencilClosed.cube_pw
#print axioms PencilClosed.pencil_zero
#print axioms PencilClosed.pencil_zero_all
#print axioms PencilClosed.quad_shift
#print axioms PencilClosed.disc4
#print axioms PencilClosed.quad_sum
#print axioms PencilClosed.quad_sum0
#print axioms PencilClosed.conic_points
#print axioms PencilClosed.conic_points0
#print axioms PencilClosed.scale_q
#print axioms PencilClosed.disc_scale
#print axioms PencilClosed.quad_sum_a
#print axioms PencilClosed.quad_sum_a0
#print axioms PencilClosed.conic_points_a
#print axioms PencilClosed.delta_scale
#print axioms PencilClosed.binary_form
#print axioms PencilClosed.binary_form0
#print axioms PencilClosed.ternary_form
#print axioms PencilClosed.quaternary_form
#print axioms PencilClosed.quaternary_form0

#print axioms: 34 theorems, no axioms.

78.95 Second moment of the Legendre family

The squared traces of \(y^2=x(x-1)(x-\lambda )\), summed over \(\lambda \), every odd \(p\).

\[ \sum _\lambda \Big (\sum _x\chi \big (x(x-1)(x-\lambda )\big )\Big )^2=p^2-2p-1. \]

lang/LegendreMoment.lean

/-
  lang/LegendreMoment — T43, cycle 316: the second moment of the Legendre family y² = x(x − 1)(x − λ), every odd p.

  p = 2m + 3, χ the Legendre symbol, f(x) = x(x − 1), a(λ) = Σ_x χ(f(x)(x − λ)) (= −a_p of E_λ).
    sq_expand   a(λ)² = Σ_x Σ_y χ(f x)χ(f y)χ((λ − x)(λ − y))
    sum_f       Σ_x χ(x(x − 1)) = −1                              (ConicSum.sum_xxd)
    sum_f2      Σ_x χ(x(x − 1))² = p − 2
    legendre_moment   Σ_λ a(λ)² = p² − 2p − 1                    (JacobMoment.inner)
    refl_one, refl_inv, sum_A   a(1 − λ) = χ(−1)a(λ), a(1/λ) = χ(λ)a(λ), Σ_λ a(λ) = 0
    twisted_sum   Σ_λ χ(λ)a(λ) = χ(−1)
    adjacent      Σ_λ a(λ)a(λ + 1) = −1 − p(1 + χ(−1))
    odd_moments   p ≡ 3 (mod 4): Σ_λ a(λ)^(2k+1) = 0
    family_moment every g: Σ_λ (Σ_x χ(g(x)(x − λ)))² = −(Σ χ(g))² + p Σ χ(g)²
    root_count    a ≠ 0: #{ax² + bx + c = 0} = 1 + χ(b² − 4ac)
    quad_family   a ≠ 0, b² ≠ 4ac: Σ_λ (Σ_x χ((ax² + bx + c)(x − λ)))² = −1 + p(p − 1 − χ(b² − 4ac))
    translation_moment  every h: Σ_λ (Σ_x χ(h(x) + λ))² = −p² + p·#{h(x) = h(y)}
    cube_pairs    p > 3: #{x³ = y³} = p + (p − 1)(1 + χ(−3))   (two moments compared)
    cube_roots    p > 3: #{z : z³ = 1} = 2 + χ(−3)
    fourth_roots  #{z : z⁴ = 1} = 3 + χ(−1)
    sixth_roots   p > 3: #{z : z⁶ = 1} = 4 + 2χ(−3)
    quartic_m1    #{z : z⁴ = −1} = (1 + χ(−1))(1 + χ(2))
    eighth_roots  #{z : z⁸ = 1} = 3 + χ(−1) + (1 + χ(−1))(1 + χ(2))
    cube_m1, sixth_m1   #{t³ = −1} = 2 + χ(−3), #{z⁶ = −1} = (1 + χ(−1))(2 + χ(−3))
    twelfth_roots #{z : z¹² = 1} = (2 + χ(−3))(3 + χ(−1))
    pair_sum      Σ_x (1 + χ(x))(1 + χ(x + d)) = p − 1 (d ≠ 0)
    translation_cov  Σ_λ S_h(λ)S_k(λ) = −p² + p·#{h(x) = k(y)}
    jacobi_quad   J(χ, χ) = Σ_x χ(x)χ(1 − x) = −χ(−1)
    legendre_even λ ≠ 0, 1: a(λ) is even
    char_parity   every g: Σ_x χ(g x) + #{g = 0} ≡ p (mod 2)
    three_div     χ(−3) = 1 ⇒ 3 | p − 1
    three_div_conv  3 | p − 1 ⇒ χ(−3) = 1
    four_div, four_div_conv   χ(−1) = 1 ⟺ 4 | p − 1
    eight_div     χ(−1) = χ(2) = 1 ⇒ 8 | p − 1
    eight_div_conv  8 | p − 1 ⇒ χ(2) = 1
    chi3_one, chi3_eleven   p ≡ ±1 (mod 12) ⇒ χ(3) = 1
    chi3_five, chi3_seven   p ≡ ±5 (mod 12) ⇒ χ(3) = −1
    gauss5, five_div_conv   (z − z² − z³ + z⁴)² = 5 for z⁵ = 1 ≠ z; 5 | p − 1 ⇒ χ(5) = 1
    gauss7, seven_div_conv  (z + z² − z³ + z⁴ − z⁵ − z⁶)² = −7; 7 | p − 1 ⇒ χ(−7) = 1
    gauss11, gauss13, eleven_div_conv, thirteen_div_conv   11 | p − 1 ⇒ χ(−11) = 1, 13 | p − 1 ⇒ χ(13) = 1
    gauss17, gauss19, seventeen_div_conv, nineteen_div_conv   17 | p − 1 ⇒ χ(17) = 1, 19 | p − 1 ⇒ χ(−19) = 1
    square_pairs, square_family   #{x² = y²} = 2p − 1; Σ_λ (Σ_x χ(x² + λ))² = p² − p
    stick_root, stick_three   Stickelberger for cubics with a root: χ(Δ) = χ(disc of the quadratic factor); three roots ⇒ χ(Δ) = 1
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.PencilClosed
import lang.JacobiNorm
import lang.Inf17mod24
import lang.Inf2e1

namespace LegendreMoment

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal QuadTwist JacobMoment PencilMoment PencilClosed
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def f (x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))

def A (l : Zp (2 * m + 1 + 1)) : Int := sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m)

theorem pt (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x y : Zp (2 * m + 1 + 1)) :
    chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m y) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg l)))
      = (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))) := by
  rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg l)) by lib_ring,
    chi_mul m hpr (f m x), chi_mul m hpr (f m y), chi_mul m hpr ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))]
  generalize chi m (f m x) = a; generalize chi m (f m y) = b
  generalize chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) = c; generalize chi m ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg l)) = d
  lib_int

theorem sq_expand (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l : Zp (2 * m + 1 + 1)) :
    A m l * A m l = sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y)))) (L m)) (L m) := by
  unfold A
  show LibInt.intR.mul _ _ = _
  rw [sumK_mul]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    rw [mul_sumK]
    exact sumK_ext LibInt.intR _ _ (fun y => pt m hpr l x y) _) _

theorem neg_delta (x y : Zp (2 * m + 1 + 1)) : delta LibInt.intR ((ring (2 * m + 1 + 1)).neg x) ((ring (2 * m + 1 + 1)).neg y) = delta LibInt.intR y x := by
  by_cases h : y = x
  · rw [h, delta_one m _ _ rfl, delta_one m _ _ rfl]
  · rw [delta_zero m _ _ h, delta_zero m _ _ (fun e => h (by
      calc y = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg y) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg x) := by rw [e]
        _ = x := by lib_ring))]

theorem sum_f (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun x => chi m (f m x)) (L m) = -1 :=
  sum_xxd m hpr _ (negnz m _ (PowerSum.one_ne_zero' (2 * m + 1)))

theorem f_delta (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) :
    delta LibInt.intR (f m x) (ring (2 * m + 1 + 1)).zero = delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + delta LibInt.intR x (ring (2 * m + 1 + 1)).one := by
  have h10 : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * m + 1)
  by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
  · rw [h0, delta_one m _ _ (by unfold f; lib_ring), delta_one m _ _ rfl, delta_zero m _ _ h10.symm]; rfl
  · by_cases h1 : x = (ring (2 * m + 1 + 1)).one
    · rw [h1, delta_one m _ _ (by unfold f; lib_ring), delta_zero m _ _ h10, delta_one m _ _ rfl]; rfl
    · rw [delta_zero m _ _ (fun e => by
          unfold f at e
          cases nz2 m hpr _ _ e with
          | inl h => exact h0 h
          | inr h => exact h1 (by
              calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
                _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring)),
        delta_zero m _ _ h0, delta_zero m _ _ h1]; rfl

theorem sum_f2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun x => chi m (f m x) * chi m (f m x)) (L m) = P m + -1 + -1 := by
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 1 1) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero)) (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).one)))) (fun x => by
    rw [QuadDiagonal.sqchi m hpr, f_delta m hpr]
    show 1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + delta LibInt.intR x (ring (2 * m + 1 + 1)).one) = 1 * 1 + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + -1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).one)
    generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).one = v; lib_int)]
  rw [sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, ← mul_sumK, sum_one,
    show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero,
    show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).one) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).one]
  show 1 * P m + (-1 * 1 + -1 * 1) = _
  lib_int

/-- **Second moment of the Legendre family**: Σ_λ (Σ_x χ(x(x − 1)(x − λ)))² = p² − 2p − 1. -/
theorem legendre_moment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun l => A m l * A m l) (L m) = P m * P m + -(P m + P m) + -1 := by
  rw [sumK_ext LibInt.intR _ _ (sq_expand m hpr)]
  rw [← sumK_swap LibInt.intR (fun x l => sumK LibInt.intR (fun y => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => LibInt.intR.add (LibInt.intR.mul (-(chi m (f m x))) (chi m (f m y))) (LibInt.intR.mul (P m * chi m (f m x)) (LibInt.intR.mul (delta LibInt.intR y x) (chi m (f m y))))) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y l => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => by
      rw [show (fun l => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))))
          = (fun l => LibInt.intR.mul (chi m (f m x) * chi m (f m y)) (chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))))) from rfl,
        ← mul_sumK, inner m hpr, neg_delta]
      show (chi m (f m x) * chi m (f m y)) * (-1 + P m * delta LibInt.intR y x) = -(chi m (f m x)) * chi m (f m y) + P m * chi m (f m x) * (delta LibInt.intR y x * chi m (f m y))
      generalize chi m (f m x) = a; generalize chi m (f m y) = b; generalize delta LibInt.intR y x = d; lib_int) _)]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (-(chi m (f m x))) (-1)) (LibInt.intR.mul (P m) (chi m (f m x) * chi m (f m x)))) (fun x => by
    rw [sumK_add, ← mul_sumK, ← mul_sumK, FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun y => chi m (f m y)) x, sum_f m hpr]
    show -(chi m (f m x)) * -1 + P m * chi m (f m x) * chi m (f m x) = _
    generalize chi m (f m x) = a; show _ = -a * -1 + P m * (a * a); lib_int)]
  rw [sumK_add, ← sumK_mul, ← mul_sumK, sum_f2 m hpr]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.mul (-1) (chi m (f m x))) (fun x => by show -(chi m (f m x)) = -1 * chi m (f m x); generalize chi m (f m x) = a; lib_int)]
  rw [← mul_sumK, sum_f m hpr]
  show -1 * -1 * -1 + P m * (P m + -1 + -1) = _
  lib_int

/-- **A(1 − λ) = χ(−1)A(λ)** (x ↦ 1 − x). -/
theorem refl_one (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l : Zp (2 * m + 1 + 1)) : A m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l)) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * A m l := by
  unfold A
  refine Eq.trans (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun x => (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) (fun x => (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))
    (fun x => by lib_ring) (fun y => by lib_ring) (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l))))))).symm ?_
  show _ = LibInt.intR.mul _ _
  rw [mul_sumK]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    show chi m ((ring (2 * m + 1 + 1)).mul (f m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l))))) = _
    rw [show (ring (2 * m + 1 + 1)).mul (f m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l))))
      = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) by unfold f; lib_ring, chi_mul m hpr]; rfl) _

/-- **A(λ⁻¹) = χ(λ)A(λ)** for λ ≠ 0 (x ↦ x/λ). -/
theorem refl_inv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l : Zp (2 * m + 1 + 1)) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) : A m (iv m l) = chi m l * A m l := by
  have e : (ring (2 * m + 1 + 1)).mul (iv m l) l = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr l hl
  have hil := iv_nz m hpr l hl
  unfold A
  refine Eq.trans (reindex_mul m hpr (iv m l) hil (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (iv m l)))))).symm ?_
  show _ = LibInt.intR.mul _ _
  rw [mul_sumK]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    show chi m ((ring (2 * m + 1 + 1)).mul (f m ((ring (2 * m + 1 + 1)).mul (iv m l) x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m l) x) ((ring (2 * m + 1 + 1)).neg (iv m l)))) = _
    have hx : (ring (2 * m + 1 + 1)).mul (f m ((ring (2 * m + 1 + 1)).mul (iv m l) x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m l) x) ((ring (2 * m + 1 + 1)).neg (iv m l)))
        = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m l) ((ring (2 * m + 1 + 1)).mul (iv m l) (iv m l))) ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) := by
      have h1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m l) x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).mul (iv m l) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) := by rw [← e]; lib_ring
      unfold f
      rw [h1]
      lib_ring
    rw [hx, chi_mul m hpr, Jacobsthal.chi_cube m hpr, chi_iv m hpr l hl]; rfl) _

/-- **Σ_λ A(λ) = 0.** -/
theorem sum_A (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun l => A m l) (L m) = 0 := by
  unfold A
  rw [← sumK_swap LibInt.intR (fun x l => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))))]
  rw [sumK_ext LibInt.intR _ (fun _ => (0 : Int)) (fun x => by
    rw [sumK_ext LibInt.intR _ (fun l => LibInt.intR.mul (chi m (f m x)) (chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (fun l => chi_mul m hpr _ _), ← mul_sumK, QuadDiagonal.S1 m hpr x]
    show chi m (f m x) * 0 = 0; generalize chi m (f m x) = a; lib_int)]
  exact QuadDiagonal.zsum _

/-- **Σ_λ χ(λ) a(λ) = χ(−1).** -/
theorem twisted_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun l => chi m l * A m l) (L m) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  unfold A
  rw [sumK_ext LibInt.intR _ (fun l => sumK LibInt.intR (fun x => LibInt.intR.mul (chi m (f m x)) (chi m ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))))) (L m)) (fun l => by
    show LibInt.intR.mul _ _ = _
    rw [mul_sumK]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      rw [chi_mul m hpr (f m x), chi_mul m hpr l]
      show chi m l * (chi m (f m x) * chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) = chi m (f m x) * (chi m l * chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))
      generalize chi m l = a; generalize chi m (f m x) = b; generalize chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) = c; lib_int) _)]
  rw [sumK_swap LibInt.intR (fun l x => LibInt.intR.mul (chi m (f m x)) (chi m ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))))]
  have hin : ∀ x, sumK LibInt.intR (fun l => chi m ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m)
      = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (-1 + P m * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) := fun x => by
    rw [sumK_ext LibInt.intR _ (fun l => LibInt.intR.mul (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (chi m ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x))))) (fun l => by
      rw [show (ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x))) by lib_ring, chi_mul m hpr]; rfl)]
    rw [← mul_sumK]
    by_cases hx : x = (ring (2 * m + 1 + 1)).zero
    · rw [hx, delta_one m _ _ rfl]
      rw [sumK_ext LibInt.intR _ (fun l => 1 + -1 * delta LibInt.intR l (ring (2 * m + 1 + 1)).zero) (fun l => by
        rw [show (ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero)) = (ring (2 * m + 1 + 1)).mul l l by lib_ring]; exact sq_delta m hpr l)]
      rw [QuadDiagonal.sI_add, QuadDiagonal.sI_mul, sum_one, show sumK LibInt.intR (fun l => delta LibInt.intR l (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
      show chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (P m + -1 * 1) = _; lib_int
    · rw [sum_xxd m hpr _ (negnz m _ hx), delta_zero m _ _ hx]
      show chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * -1 = _; lib_int
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.mul (chi m (f m x)) (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (-1 + P m * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero))) (fun x => by rw [← mul_sumK, hin x])]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (-(chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) (chi m (f m x))) (LibInt.intR.mul (P m * chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (LibInt.intR.mul (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (chi m (f m x))))) (fun x => by
    show chi m (f m x) * (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (-1 + P m * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero)) = -(chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * chi m (f m x) + P m * chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero * chi m (f m x))
    generalize chi m (f m x) = a; generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = d; lib_int)]
  rw [sumK_add, ← mul_sumK, ← mul_sumK, sum_f m hpr, FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun x => chi m (f m x)) (ring (2 * m + 1 + 1)).zero,
    show f m (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero by unfold f; lib_ring, chi_zero]
  show -(chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * -1 + P m * chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * 0 = _
  generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int

theorem pt1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x y : Zp (2 * m + 1 + 1)) :
    chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m y) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add l (ring (2 * m + 1 + 1)).one))))
      = (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))))) := by
  rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg ((ri  -- (+68 chars; full line in the Zenodo source)
    chi_mul m hpr (f m x), chi_mul m hpr (f m y), chi_mul m hpr ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))]
  generalize chi m (f m x) = a; generalize chi m (f m y) = b
  generalize chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) = c; generalize chi m ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add l (ring (2 * m + 1 + 1)).one))) = d
  lib_int

theorem delta_shift1 (x y : Zp (2 * m + 1 + 1)) : delta LibInt.intR ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) x = delta LibInt.intR y ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one) := by
  by_cases h : y = (ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one
  · rw [delta_one m _ _ h, delta_one m _ _ (by rw [h]; lib_ring)]
  · rw [delta_zero m _ _ h, delta_zero m _ _ (fun e => h (by
      calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
        _ = _ := by rw [e]))]

theorem ff1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) :
    chi m (f m x) * chi m (f m ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)) = chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero * chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by
  rw [← chi_mul m hpr, show (ring (2 * m + 1 + 1)).mul (f m x) (f m ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) by unfold f; lib_ring,
    chi_mul m hpr, sq_delta m hpr]
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, delta_one m _ _ rfl, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one by lib_ring]
    generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int
  · rw [delta_zero m _ _ hx]
    generalize chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = e; lib_int

/-- **Σ_λ a(λ)a(λ + 1) = −1 − p(1 + χ(−1)).** -/
theorem adjacent (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun l => A m l * A m ((ring (2 * m + 1 + 1)).add l (ring (2 * m + 1 + 1)).one)) (L m) = -1 + -(P m * (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) := by
  have hexp : ∀ l, A m l * A m ((ring (2 * m + 1 + 1)).add l (ring (2 * m + 1 + 1)).one) = sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2   -- (+51 chars; full line in the Zenodo source)
    unfold A
    show LibInt.intR.mul _ _ = _
    rw [sumK_mul]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      rw [mul_sumK]
      exact sumK_ext LibInt.intR _ _ (fun y => pt1 m hpr l x y) _) _
  rw [sumK_ext LibInt.intR _ _ hexp]
  rw [← sumK_swap LibInt.intR (fun x l => sumK LibInt.intR (fun y => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)))))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => LibInt.intR.add (LibInt.intR.mul (-(chi m (f m x))) (chi m (f m y))) (LibInt.intR.mul (P m * chi m (f m x)) (LibInt.intR.mul (delta LibInt.intR y ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)) (chi m (f m y))))) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y l => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))))))]
    exact sumK_ext LibInt.intR _ _ (fun y => by
      rw [show (fun l => (chi m (f m x) * chi m (f m y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)))))) = (fun l => LibInt.intR.mul (chi m (f m x) * chi m (f m y)) (chi m ((ring (2 * m + 1 + 1)).mul (  -- (+220 chars; full line in the Zenodo source)
        ← mul_sumK, inner m hpr, neg_delta, delta_shift1]
      show (chi m (f m x) * chi m (f m y)) * (-1 + P m * delta LibInt.intR y ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)) = -(chi m (f m x)) * chi m (f m y) + P m * chi m (f m x) * (delta LibInt.intR y ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one) * chi m (f m y))
      generalize chi m (f m x) = a; generalize chi m (f m y) = b; generalize delta LibInt.intR y ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one) = d; lib_int) _)]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (-1) (LibInt.intR.mul (chi m (f m x)) (-1))) (LibInt.intR.mul (P m) (chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero * chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))))) (fun x => by
    rw [sumK_add, ← mul_sumK, ← mul_sumK, FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun y => chi m (f m y)) _, sum_f m hpr, ← ff1 m hpr x]
    show -(chi m (f m x)) * -1 + P m * chi m (f m x) * chi m (f m ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)) = -1 * (chi m (f m x) * -1) + P m * (chi m (f m x) * chi m (f m ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)))
    generalize chi m (f m x) = a; generalize chi m (f m ((ring (2 * m + 1 + 1)).add x (ring (2 * m + 1 + 1)).one)) = b; lib_int)]
  rw [sumK_add, ← mul_sumK, ← mul_sumK, ← sumK_mul, sum_f m hpr]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) (LibInt.intR.mul (-(chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero))) (fun x => by
    show _ = _ + -(chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * _; generalize chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = d; generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int),
    sumK_add, ← mul_sumK, sum_y2c m hpr _ (negnz m _ (PowerSum.one_ne_zero' (2 * m + 1))),
    show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
  show -1 * (-1 * -1) + P m * (-1 + -(chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 1) = _
  generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int

/-- **Odd moments vanish at p ≡ 3 (mod 4)**: χ(−1) = −1 ⇒ Σ_λ a(λ)·(a(λ)²)^k = 0. -/
theorem odd_moments (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hm : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = -1) (k : Nat) :
    sumK LibInt.intR (fun l => A m l * PencilMoment.ipow (A m l * A m l) k) (L m) = 0 := by
  have hneg : sumK LibInt.intR (fun l => A m l * PencilMoment.ipow (A m l * A m l) k) (L m)
      = LibInt.intR.mul (-1) (sumK LibInt.intR (fun l => A m l * PencilMoment.ipow (A m l * A m l) k) (L m)) := by
    calc _ = sumK LibInt.intR (fun l => A m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l)) * PencilMoment.ipow (A m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l)) * A m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg l))) k) (L m) :=
          (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun x => (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) (fun x => (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))
            (fun x => by lib_ring) (fun y => by lib_ring) (fun l => A m l * PencilMoment.ipow (A m l * A m l) k)).symm
      _ = _ := by
        rw [mul_sumK]
        exact sumK_ext LibInt.intR _ _ (fun l => by
          rw [refl_one m hpr l, hm, show (-1 * A m l) * (-1 * A m l) = A m l * A m l by generalize A m l = a; lib_int]
          show -1 * A m l * PencilMoment.ipow (A m l * A m l) k = -1 * (A m l * PencilMoment.ipow (A m l * A m l) k)
          generalize PencilMoment.ipow (A m l * A m l) k = q; generalize A m l = a; lib_int) _
  exact JacobiNorm.int_cancel _ _ 1 (by
    have e : ∀ X : Int, X = LibInt.intR.mul (-1) X → X * Int.ofNat (1 + 1) = 0 * Int.ofNat (1 + 1) := fun X hX => by
      calc X * Int.ofNat (1 + 1) = X + X := by show X * 2 = X + X; lib_int
        _ = X + -1 * X := by
          have : X = -1 * X := hX
          rw [← this]
        _ = 0 * Int.ofNat (1 + 1) := by show X + -1 * X = 0 * 2; lib_int
    exact e _ hneg)

/-- **Second moment of any family y² = g(x)(x − λ)**: Σ_λ (Σ_x χ(g(x)(x − λ)))² = −(Σ_x χ(g x))² + p Σ_x χ(g x)². -/
theorem family_moment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (g : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun l => sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (g x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m) * sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (g x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m)) (L m)
      = -(sumK LibInt.intR (fun x => chi m (g x)) (L m) * sumK LibInt.intR (fun x => chi m (g x)) (L m)) + P m * sumK LibInt.intR (fun x => chi m (g x) * chi m (g x)) (L m) := by
  have hpt : ∀ l x y, chi m ((ring (2 * m + 1 + 1)).mul (g x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (g y) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg l)))
      = (chi m (g x) * chi m (g y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))) := fun l x y => by
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg l)) by lib_ring,
      chi_mul m hpr (g x), chi_mul m hpr (g y), chi_mul m hpr ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))]
    generalize chi m (g x) = a; generalize chi m (g y) = b
    generalize chi m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) = c; generalize chi m ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg l)) = d
    lib_int
  rw [sumK_ext LibInt.intR _ (fun l => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => (chi m (g x) * chi m (g y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y)))) (L m)) (L m)) (fun l => by
    show LibInt.intR.mul _ _ = _
    rw [sumK_mul]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      rw [mul_sumK]
      exact sumK_ext LibInt.intR _ _ (fun y => hpt l x y) _) _)]
  rw [← sumK_swap LibInt.intR (fun x l => sumK LibInt.intR (fun y => (chi m (g x) * chi m (g y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => LibInt.intR.add (LibInt.intR.mul (-(chi m (g x))) (chi m (g y))) (LibInt.intR.mul (P m * chi m (g x)) (LibInt.intR.mul (delta LibInt.intR y x) (chi m (g y))))) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y l => (chi m (g x) * chi m (g y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => by
      rw [show (fun l => (chi m (g x) * chi m (g y)) * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))))
          = (fun l => LibInt.intR.mul (chi m (g x) * chi m (g y)) (chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).neg y))))) from rfl,
        ← mul_sumK, inner m hpr, neg_delta]
      show (chi m (g x) * chi m (g y)) * (-1 + P m * delta LibInt.intR y x) = -(chi m (g x)) * chi m (g y) + P m * chi m (g x) * (delta LibInt.intR y x * chi m (g y))
      generalize chi m (g x) = a; generalize chi m (g y) = b; generalize delta LibInt.intR y x = d; lib_int) _)]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (chi m (g x)) (-(sumK LibInt.intR (fun y => chi m (g y)) (L m)))) (LibInt.intR.mul (P m) (chi m (g x) * chi m (g x)))) (fun x => by
    rw [sumK_add, ← mul_sumK, ← mul_sumK, FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun y => chi m (g y)) x]
    show -(chi m (g x)) * sumK LibInt.intR (fun y => chi m (g y)) (L m) + P m * chi m (g x) * chi m (g x) = _
    generalize sumK LibInt.intR (fun y => chi m (g y)) (L m) = S; generalize chi m (g x) = a; show _ = a * -S + P m * (a * a); lib_int)]
  rw [sumK_add, ← sumK_mul, ← mul_sumK]
  show sumK LibInt.intR (fun x => chi m (g x)) (L m) * -(sumK LibInt.intR (fun y => chi m (g y)) (L m)) + P m * sumK LibInt.intR (fun x => chi m (g x) * chi m (g x)) (L m) = _
  generalize sumK LibInt.intR (fun x => chi m (g x)) (L m) = S
  generalize sumK LibInt.intR (fun x => chi m (g x) * chi m (g x)) (L m) = T
  lib_int

/-- **#{x : ax² + bx + c = 0} = 1 + χ(b² − 4ac)** for a ≠ 0. -/
theorem root_count (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c)) (ring (2 * m + 1 + 1)).zero) (L m) = 1 + chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((  -- (+206 chars; full line in the Zenodo source)
  have hpt : ∀ x, delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c)) (ring (2 * m + 1 + 1)).zero = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m  -- (+709 chars; full line in the Zenodo source)
    rw [scale_q m hpr a b c x ha, quad_shift m hpr ((ring (2 * m + 1 + 1)).mul (iv m a) b) ((ring (2 * m + 1 + 1)).mul (iv m a) c) x]
    by_cases hz : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m a) b))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1  -- (+469 chars; full line in the Zenodo source)
    · rw [delta_one m _ _ hz, delta_one m _ _ (by rw [hz]; lib_ring)]
    · rw [delta_zero m _ _ hz, delta_zero m _ _ (nzm m hpr _ _ ha (fun e => hz (by
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m a) b))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1  -- (+1017 chars; full line in the Zenodo source)
            _ = _ := by rw [e]; lib_ring)))]
  rw [sumK_ext LibInt.intR _ _ hpt, shift_sum m (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m a) b)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m +  -- (+343 chars; full line in the Zenodo source)
  have h4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := nzm m hpr _ _ (QuadDiagonal.two_ne m) (QuadDiagonal.two_ne m)
  have hia := iv_nz m hpr a ha
  have e1 : chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m +  -- (+745 chars; full line in the Zenodo source)
    rw [chi_mul m hpr, chi_sq m hpr _ (QuadDiagonal.two_ne m)]; lib_int
  have e2 : chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m a) (iv m a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).o  -- (+403 chars; full line in the Zenodo source)
    rw [chi_mul m hpr, chi_sq m hpr _ hia]; lib_int
  rw [← e1, disc4 m hpr ((ring (2 * m + 1 + 1)).mul (iv m a) b) ((ring (2 * m + 1 + 1)).mul (iv m a) c), disc_scale m hpr a b c ha, e2]

/-- **Every quadratic family**: for a ≠ 0, b² ≠ 4ac, Σ_λ (Σ_x χ((ax² + bx + c)(x − λ)))² = −1 + p(p − 1 − χ(b² − 4ac)). -/
theorem quad_family (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2  -- (+142 chars; full line in the Zenodo source)
    sumK LibInt.intR (fun l => sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m) * sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1   -- (+215 chars; full line in the Zenodo source)
      = -1 + P m * (P m + -1 + -chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul a c))))) := by
  rw [family_moment m hpr (fun x => (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c)), quad_sum_a m hpr a b c ha hD]
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 1 1) (LibInt.intR.mul (-1) (delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c)) (ring (2 * m + 1 + 1)).zero))) (fun x => by
    rw [QuadDiagonal.sqchi m hpr]; show _ = 1 * 1 + -1 * _; generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b x) c)) (ring (2 * m + 1 + 1)).zero = u; lib_int),
    sumK_add, ← mul_sumK, ← mul_sumK, sum_one, root_count m hpr a b c ha]
  have hca : chi m a * chi m a = 1 := chi_unit m hpr a ha
  show -(-chi m a * -chi m a) + P m * (1 * P m + -1 * (1 + chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1))  -- (+18 chars; full line in the Zenodo source)
  rw [show -chi m a * -chi m a = chi m a * chi m a by generalize chi m a = t; lib_int, hca]
  generalize chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul a c)))) = e; lib_int

/-- **Second moment of any translation family y² = h(x) + λ**: Σ_λ (Σ_x χ(h(x) + λ))² = −p² + p·#{(x, y) : h(x) = h(y)}. -/
theorem translation_moment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun l => sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add l (h x))) (L m) * sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add l (h x))) (L m)) (L m)
      = -(P m * P m) + P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (h x) (h y)) (L m)) (L m) := by
  rw [sumK_ext LibInt.intR _ (fun l => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y)))) (L m)) (L m)) (fun l => by
    show LibInt.intR.mul _ _ = _
    rw [sumK_mul]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      rw [mul_sumK]
      exact sumK_ext LibInt.intR _ _ (fun y => (chi_mul m hpr _ _).symm) _) _)]
  rw [← sumK_swap LibInt.intR (fun x l => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => -1 + P m * delta LibInt.intR (h x) (h y)) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y l => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => inner m hpr _ _) _)]
  rw [sumK_ext LibInt.intR _ _ (fun x => PencilMoment.sum_lin m (-1) (P m) _), PencilMoment.sum_lin]
  lib_int

/-- **#{(x, y) : x³ = y³} = p + (p − 1)(1 + χ(−3))** for p > 3 — read off from two second moments (translation_moment, pencil_zero_all):
    the number of cube roots of unity is 2 + χ(−3). -/
theorem cube_pairs (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y))) (L m)) (L m)
      = P m + (P m + -1) * (1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) := by
  have ht := translation_moment m hpr (fun x => (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x))
  have hz := pencil_zero_all m hpr h3
  have hW : ∀ b, W m (ring (2 * m + 1 + 1)).zero b = sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)))) (L m) := fun b =>
    sumK_ext LibInt.intR _ _ (fun x => by
      rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero x) b) = (ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) by lib_ring]) _
  rw [sumK_ext LibInt.intR _ _ (fun b => by rw [hW b])] at hz
  rw [hz] at ht
  generalize sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y))) (L m)) (L m) = N at ht ⊢
  have hP : P m = Int.ofNat (2 * m + 1 + 1 + 1) := rfl
  apply JacobiNorm.int_cancel _ _ (2 * m + 1 + 1)
  rw [← hP]
  calc N * P m = (P m * P m + -(P m)) * (1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) + P m * P m := by rw [ht]; lib_int
    _ = _ := by lib_int

theorem row (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y))) (L m) = delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero)) * sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((r  -- (+43 chars; full line in the Zenodo source)
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, delta_one m _ _ rfl]
    rw [sumK_ext LibInt.intR _ (fun y => delta LibInt.intR y (ring (2 * m + 1 + 1)).zero) (fun y => by
      by_cases hy : y = (ring (2 * m + 1 + 1)).zero
      · rw [hy, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl]
      · rw [delta_zero m _ _ hy, delta_zero m _ _ (fun e => hy (by
          cases nz2 m hpr _ _ e.symm with
          | inl h => exact h
          | inr h => cases nz2 m hpr _ _ h with
            | inl h' => exact h'
            | inr h' => exact h')) ])]
    rw [show sumK LibInt.intR (fun y => delta LibInt.intR y (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
    generalize sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m) = r; lib_int
  · rw [delta_zero m _ _ hx]
    have hx3 : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) ≠ (ring (2 * m + 1 + 1)).zero := nzm m hpr _ _ hx (nzm m hpr _ _ hx hx)
    refine Eq.trans (reindex_mul m hpr x hx (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul y y)))).symm ?_
    rw [sumK_ext LibInt.intR _ (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (fun z => by
      show delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x z) ((ring (2 * m + 1 + 1)).mul x z))) = _
      by_cases hz : (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)
      · rw [delta_one m _ _ hz, delta_one m _ _ (by
          calc (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) (ring (2 * m + 1 + 1)).one := by lib_ring
            _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) := by rw [hz]
            _ = _ := by lib_ring)]
      · rw [delta_zero m _ _ hz, delta_zero m _ _ (fun e => hz (by
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) = (ring (2 * m + 1 + 1)).zero := by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x z) ((ring (2 * m + 1 + 1)).mul x z)))) := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact absurd h hx3
          | inr h => calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) := by lib_ring
              _ = _ := by rw [h]; lib_ring))])]
    generalize sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m) = r; lib_int

/-- **𝔽_p has exactly 2 + χ(−3) cube roots of unity** (p > 3). -/
theorem cube_roots (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) : sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z  -- (+195 chars; full line in the Zenodo source)
  have hc := cube_pairs m hpr h3
  rw [sumK_ext LibInt.intR _ _ (row m hpr)] at hc
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m)) 1) (LibInt.intR.mul (1 + -(sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m))) (delta LibI  -- (+53 chars; full line in the Zenodo source)
    show _ = sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m) * 1 + (1 + -(sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m))) * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero; generalize sumK LibInt.intR (fun z   -- (+197 chars; full line in the Zenodo source)
    sumK_add, ← mul_sumK, ← mul_sumK, sum_one, show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero] at hc
  have hc' : sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m) * P m + (1 + -(sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m))) * 1 = P m + (P m + -1) * (1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 +   -- (+126 chars; full line in the Zenodo source)
  generalize sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m) = r at hc' ⊢
  generalize chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = e at hc' ⊢
  have hP : P m + -1 = Int.ofNat (2 * m + 1 + 1) := by show Int.ofNat (2 * m + 1 + 1) + 1 + -1 = Int.ofNat (2 * m + 1 + 1); generalize Int.ofNat (2 * m + 1 + 1) = q; lib_int
  apply JacobiNorm.int_cancel _ _ (2 * m + 1)
  rw [← hP]
  calc r * (P m + -1) = (r * P m + (1 + -r) * 1) + -1 := by lib_int
    _ = (P m + (P m + -1) * (1 + e)) + -1 := by rw [hc']
    _ = _ := by lib_int

/-- **𝔽_p has exactly 3 + χ(−1) fourth roots of unity.** -/
theorem fourth_roots (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one) (L m) = 3 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  have h1 : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := fun e => QuadDiagonal.two_ne m (by
    calc (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by rw [← e]
      _ = (ring (2 * m + 1 + 1)).zero := by lib_ring)
  have hpt : ∀ z, delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul z z) (ring (2 * m + 1 + 1)).one + delta LibInt.intR ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := fun z => by
    by_cases ha : (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).one
    · rw [ha, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl, delta_zero m _ _ h1]; rfl
    · by_cases hb : (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one
      · rw [hb, delta_one m _ _ (by lib_ring), delta_zero m _ _ (fun e => h1 e.symm), delta_one m _ _ rfl]; rfl
      · rw [delta_zero m _ _ ha, delta_zero m _ _ hb, delta_zero m _ _ (fun e => by
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact ha (by
              calc (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
                _ = _ := by rw [h]; lib_ring)
          | inr h => exact hb (by
              calc (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
                _ = _ := by rw [h]; lib_ring))]; rfl
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sq_count m hpr, QuadDiagonal.sq_count m hpr, chi_one m]
  generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int

/-- **𝔽_p has exactly 4 + 2χ(−3) sixth roots of unity** (p > 3). -/
theorem sixth_roots (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) : sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z  -- (+293 chars; full line in the Zenodo source)
  have h1 : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := fun e => QuadDiagonal.two_ne m (by
    calc (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by rw [← e]
      _ = (ring (2 * m + 1 + 1)).zero := by lib_ring)
  have hpt : ∀ z, delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (ring (2 * m + 1 + 1)).one = delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) + delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)  -- (+85 chars; full line in the Zenodo source)
    by_cases ha : (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)
    · rw [← ha, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl, delta_zero m _ _ h1]; rfl
    · by_cases hb : (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))
      · rw [delta_one m _ _ (by
            calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) := by lib_rin  -- (+1 chars; full line in the Zenodo source)
              _ = (ring (2 * m + 1 + 1)).one := by rw [← hb]; lib_ring), delta_zero m _ _ ha, delta_one m _ _ hb]; rfl
      · rw [delta_zero m _ _ ha, delta_zero m _ _ hb, delta_zero m _ _ (fun e => by
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact ha (by
              calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by rw [h]; lib_ring
                _ = _ := by lib_ring)
          | inr h => exact hb (by
              calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one)) := by rw [h]; lib_ring
                _ = _ := by lib_ring))]; rfl
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, cube_roots m hpr h3]
  have hneg : sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) (L m) = sumK LibInt.intR (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (L m) := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun x => (ring (2 * m + 1 + 1)).neg x) (fun x => (ring (2 * m + 1 + 1)).neg x)
      (fun x => by lib_ring) (fun y => by lib_ring) (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))))
    exact sumK_ext LibInt.intR _ _ (fun z => by
      show _ = delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg z) ((ring (2 * m + 1 + 1)).neg z)))
      rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg z) ((ring (2 * m + 1 + 1)).neg z)) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) by lib_ring]) _
  rw [hneg, cube_roots m hpr h3]
  generalize chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = e; lib_int

theorem int_m1_ne : (-1 : Int) ≠ 1 := fun h => Int.noConfusion h

/-- **#{z : z⁴ = −1} = (1 + χ(−1))(1 + χ(2))** (four solutions exactly when p ≡ 1 mod 8). -/
theorem quartic_m1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (L m) = (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (1 + chi m ((ring (2 * m + 1 + 1))  -- (+66 chars; full line in the Zenodo source)
  have hm10 : ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := negnz m _ (PowerSum.one_ne_zero' (2 * m + 1))
  cases QuadDiagonal.e_val m hpr with
  | inr hneg =>
    rw [hneg, sumK_ext LibInt.intR _ (fun _ => (0 : Int)) (fun z => delta_zero m _ _ (fun e => by
      have hz : (ring (2 * m + 1 + 1)).mul z z ≠ (ring (2 * m + 1 + 1)).zero := fun h0 => hm10 (by rw [← e, h0]; lib_ring)
      have := chi_sq m hpr _ hz
      rw [e, hneg] at this
      exact int_m1_ne this)), QuadDiagonal.zsum]
    generalize chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = t; lib_int
  | inl hpos =>
    have hpw : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (m + 1) = (ring (2 * m + 1 + 1)).one := by
      by_cases h : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (m + 1) = (ring (2 * m + 1 + 1)).one
      · exact h
      · exact absurd (hpos.symm.trans (chi_neg_of m _ hm10 h)) (fun h' => int_m1_ne h'.symm)
    match QuadDiagonal.sq_root m hpr _ hm10 hpw with
    | ⟨i, hi⟩ =>
      have hi0 : i ≠ (ring (2 * m + 1 + 1)).zero := fun h0 => hm10 (by rw [← hi, h0]; lib_ring)
      have hii : i ≠ (ring (2 * m + 1 + 1)).neg i := fun h => hi0 (by
        cases nz2 m hpr ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) i (by
          calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) i = (ring (2 * m + 1 + 1)).add i ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg i)) := by lib_ring
            _ = (ring (2 * m + 1 + 1)).zero := by rw [← h]; lib_ring) with
        | inl h2 => exact absurd h2 (QuadDiagonal.two_ne m)
        | inr h2 => exact h2)
      have hpt : ∀ z, delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul z z) i + delta LibInt.intR ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg i) := fun z => by
        by_cases ha : (ring (2 * m + 1 + 1)).mul z z = i
        · rw [ha, delta_one m _ _ (by rw [hi]), delta_one m _ _ rfl, delta_zero m _ _ hii]; rfl
        · by_cases hb : (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).neg i
          · rw [hb, delta_one m _ _ (by rw [← hi]; lib_ring), delta_zero m _ _ (fun e => hii e.symm), delta_one m _ _ rfl]; rfl
          · rw [delta_zero m _ _ ha, delta_zero m _ _ hb, delta_zero m _ _ (fun e => by
              have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg i)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) i) = (ring (2 * m + 1 + 1)).zero := by
                calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i i)) := by lib_ring
                  _ = (ring (2 * m + 1 + 1)).zero := by rw [e, hi]; lib_ring
              cases nz2 m hpr _ _ e2 with
              | inl h => exact ha (by
                  calc (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).neg i)) i := by lib_ring
                    _ = _ := by rw [h]; lib_ring)
              | inr h => exact hb (by
                  calc (ring (2 * m + 1 + 1)).mul z z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) i) ((ring (2 * m + 1 + 1)).neg i) := by lib_ring
                    _ = _ := by rw [h]; lib_ring))]; rfl
      rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sq_count m hpr, QuadDiagonal.sq_count m hpr,
        show (ring (2 * m + 1 + 1)).neg i = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) i by lib_ring, chi_mul m hpr, hpos]
      -- χ(i) = χ(2): (1 + i)² = 2i
      have h1i : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i ≠ (ring (2 * m + 1 + 1)).zero := fun h => ne_pm m (by
        calc (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul i i := hi.symm
          _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
            rw [show i = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) by lib_ring, h]; lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by lib_ring)
      have hsq : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) * chi m i = 1 := by
        rw [← chi_mul m hpr, show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) i = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) by
          calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) i = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add i i)) ((ring (2 * m + 1 + 1)).mul i i) := by
                rw [hi]; lib_ring
            _ = _ := by lib_ring]
        exact chi_sq m hpr _ h1i
      have h22 : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) * chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = 1 := chi_unit m hpr _ (QuadDiagonal.two_ne m)
      have hci : chi m i = chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) := by
        calc chi m i = (chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) * chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) * chi m i := by rw [h22]; lib_int
          _ = chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) * (chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) * chi m i) := by lib_int
          _ = chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) := by rw [hsq]; lib_int
      rw [hci]
      generalize chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = t; lib_int

/-- **𝔽_p has exactly 3 + χ(−1) + (1 + χ(−1))(1 + χ(2)) eighth roots of unity.** -/
theorem eighth_roots (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z))) (ring (2 * m + 1 + 1)).one) (L m) = 3 + chi  -- (+231 chars; full line in the Zenodo source)
  have h1 : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := fun e => QuadDiagonal.two_ne m (by
    calc (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by rw [← e]
      _ = (ring (2 * m + 1 + 1)).zero := by lib_ring)
  have hpt : ∀ z, delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z))) (ring (2 * m + 1 + 1)).one = delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) (ri  -- (+209 chars; full line in the Zenodo source)
    by_cases ha : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z) = (ring (2 * m + 1 + 1)).one
    · rw [ha, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl, delta_zero m _ _ h1]; rfl
    · by_cases hb : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one
      · rw [hb, delta_one m _ _ (by lib_ring), delta_zero m _ _ (fun e => h1 e.symm), delta_one m _ _ rfl]; rfl
      · rw [delta_zero m _ _ ha, delta_zero m _ _ hb, delta_zero m _ _ (fun e => by
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1  -- (+13 chars; full line in the Zenodo source)
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact ha (by
              calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
                _ = _ := by rw [h]; lib_ring)
          | inr h => exact hb (by
              calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
                _ = _ := by rw [h]; lib_ring))]; rfl
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, fourth_roots m hpr, quartic_m1 m hpr]

theorem cube_m1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) : sumK LibInt.intR (fun t => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) ((ring (2 * m + 1 + 1)).neg  -- (+221 chars; full line in the Zenodo source)
  rw [← cube_roots m hpr h3]
  refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun x => (ring (2 * m + 1 + 1)).neg x) (fun x => (ring (2 * m + 1 + 1)).neg x)
    (fun x => by lib_ring) (fun y => by lib_ring) (fun z => delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))))
  exact sumK_ext LibInt.intR _ _ (fun t => by
    show _ = delta LibInt.intR (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg t) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg t) ((ring (2 * m + 1 + 1)).neg t)))
    by_cases h : (ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one
    · rw [delta_one m _ _ h, delta_one m _ _ (by
        calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) := by rw [h]
          _ = _ := by lib_ring)]
    · rw [delta_zero m _ _ h, delta_zero m _ _ (fun e => h (by
        calc (ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg t) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg t) ((ring (2 * m + 1 + 1)).neg t))) := by lib_ring
          _ = _ := by rw [← e]))]) _

/-- **#{z : z⁶ = −1} = (1 + χ(−1))(2 + χ(−3))** (p > 3). -/
theorem sixth_m1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (L m) = (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * (2 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 +   -- (+126 chars; full line in the Zenodo source)
  rw [sumK_ext LibInt.intR _ (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (fun z => by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m +   -- (+219 chars; full line in the Zenodo source)
  rw [QuadDiagonal.conv m hpr (fun t => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))]
  rw [sumK_ext LibInt.intR _ (fun t => LibInt.intR.mul (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (delta LibInt.intR ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) (fun t => by
    by_cases h : (ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).mul t t) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one
    · rw [delta_one m _ _ h, ← Jacobsthal.chi_cube m hpr t, h]; rfl
    · rw [delta_zero m _ _ h]; show (1 + chi m t) * 0 = (1 + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) * 0; generalize chi m t = a; generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = b; lib_int)]
  rw [← mul_sumK, cube_m1 m hpr h3]; rfl


/-- **𝔽_p has exactly (2 + χ(−3))(3 + χ(−1)) twelfth roots of unity** (p > 3; = gcd(12, p − 1)). -/
theorem twelfth_roots (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) (ring (2  -- (+283 chars; full line in the Zenodo source)
  have h1 : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := fun e => QuadDiagonal.two_ne m (by
    calc (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by rw [← e]
      _ = (ring (2 * m + 1 + 1)).zero := by lib_ring)
  have hpt : ∀ z, delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) (ring (2 * m + 1 + 1)  -- (+457 chars; full line in the Zenodo source)
    by_cases ha : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) = (ring (2 * m + 1 + 1)).one
    · rw [ha, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl, delta_zero m _ _ h1]; rfl
    · by_cases hb : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one
      · rw [hb, delta_one m _ _ (by lib_ring), delta_zero m _ _ (fun e => h1 e.symm), delta_one m _ _ rfl]; rfl
      · rw [delta_zero m _ _ ha, delta_zero m _ _ hb, delta_zero m _ _ (fun e => by
          have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).m  -- (+137 chars; full line in the Zenodo source)
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) ((ring (2  -- (+61 chars; full line in the Zenodo source)
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring
          cases nz2 m hpr _ _ e2 with
          | inl h => exact ha (by
              calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) ((ring (2 *   -- (+86 chars; full line in the Zenodo source)
                _ = _ := by rw [h]; lib_ring)
          | inr h => exact hb (by
              calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (ring (2 * m  -- (+86 chars; full line in the Zenodo source)
                _ = _ := by rw [h]; lib_ring))]; rfl
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, sixth_m1 m hpr h3]
  rw [show sumK LibInt.intR (fun z => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z))) (ring (2 * m + 1 + 1)).one) (L m) = 4 + 2 * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one  -- (+57 chars; full line in the Zenodo source)
  generalize chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = c; generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = d; lib_int

/-- **Residue pairs at distance d**: Σ_x (1 + χ(x))(1 + χ(x + d)) = p − 1 for d ≠ 0
    (the boundary terms x = 0, −d give 4·#{x : x, x + d both nonzero squares} = p − 3 − χ(d) − χ(−d)). -/
theorem pair_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (d : Zp (2 * m + 1 + 1)) (hd : d ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun x => (1 + chi m x) * (1 + chi m ((ring (2 * m + 1 + 1)).add x d))) (L m) = P m + -1 := by
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.add (LibInt.intR.mul 1 1) (chi m x)) (LibInt.intR.add (chi m ((ring (2 * m + 1 + 1)).add x d)) (chi m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add x d))))) (fun x => by
    rw [chi_mul m hpr]; show _ = 1 * 1 + chi m x + (chi m ((ring (2 * m + 1 + 1)).add x d) + chi m x * chi m ((ring (2 * m + 1 + 1)).add x d))
    generalize chi m x = a; generalize chi m ((ring (2 * m + 1 + 1)).add x d) = b; lib_int)]
  rw [sumK_add, sumK_add, sumK_add, ← mul_sumK, sum_one, show sumK LibInt.intR (chi m) (L m) = 0 from chi_sum m hpr,
    shift_sum m (chi m) d, show sumK LibInt.intR (chi m) (L m) = 0 from chi_sum m hpr, sum_xxd m hpr d hd]
  show 1 * P m + 0 + (0 + -1) = _
  lib_int

/-- **Covariance of two translation families**: Σ_λ (Σ_x χ(h(x) + λ))(Σ_y χ(k(y) + λ)) = −p² + p·#{(x, y) : h(x) = k(y)}. -/
theorem translation_cov (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h k : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun l => sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add l (h x))) (L m) * sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).add l (k y))) (L m)) (L m)
      = -(P m * P m) + P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR (h x) (k y)) (L m)) (L m) := by
  rw [sumK_ext LibInt.intR _ (fun l => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (k y)))) (L m)) (L m)) (fun l => by
    show LibInt.intR.mul _ _ = _
    rw [sumK_mul]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      rw [mul_sumK]
      exact sumK_ext LibInt.intR _ _ (fun y => (chi_mul m hpr _ _).symm) _) _)]
  rw [← sumK_swap LibInt.intR (fun x l => sumK LibInt.intR (fun y => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (k y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => -1 + P m * delta LibInt.intR (h x) (k y)) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y l => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (k y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => inner m hpr _ _) _)]
  rw [sumK_ext LibInt.intR _ _ (fun x => PencilMoment.sum_lin m (-1) (P m) _), PencilMoment.sum_lin]
  lib_int

/-- **The quadratic Jacobi sum**: J(χ, χ) = Σ_x χ(x)χ(1 − x) = −χ(−1). -/
theorem jacobi_quad (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : sumK LibInt.intR (fun x => chi m x * chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) (L m) = -chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.mul (chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (chi m (f m x))) (fun x => by
    rw [← chi_mul m hpr, show (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (f m x) by unfold f; lib_ring, chi_mul m hpr]; rfl)]
  rw [← mul_sumK, sum_f m hpr]
  show chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * -1 = _
  generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int

theorem chi_cases (t : Zp (2 * m + 1 + 1)) : chi m t = 0 ∨ chi m t = 1 ∨ chi m t = -1 := by
  unfold chi
  by_cases h0 : t = (ring (2 * m + 1 + 1)).zero
  · rw [ite_pos' _ _ h0]; exact Or.inl rfl
  · rw [ite_neg' _ _ h0]
    by_cases h1 : Frobenius.pw (ring (2 * m + 1 + 1)) t (m + 1) = (ring (2 * m + 1 + 1)).one
    · rw [ite_pos' _ _ h1]; exact Or.inr (Or.inl rfl)
    · rw [ite_neg' _ _ h1]; exact Or.inr (Or.inr rfl)

theorem even_sum : ∀ (l : List (Zp (2 * m + 1 + 1))) (F : Zp (2 * m + 1 + 1) → Int), (∀ x, ∃ k : Int, F x = 2 * k) → ∃ k : Int, sumK LibInt.intR F l = 2 * k
  | [], _, _ => ⟨0, rfl⟩
  | x :: xs, F, h => by
    match h x, even_sum xs F h with
    | ⟨a, ha⟩, ⟨b, hb⟩ => exact ⟨a + b, by show F x + sumK LibInt.intR F xs = _; rw [ha, hb]; lib_int⟩

theorem cubic_delta (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x : Zp (2 * m + 1 + 1)) (h0 : l ≠ (ring (2 * m + 1 + 1)).zero) (h1 : l ≠ (ring (2 * m + 1 + 1)).one) :
    delta LibInt.intR ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) (ring (2 * m + 1 + 1)).zero = delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + delta LibInt.intR x (ring (2 * m + 1 + 1)).one + delta LibInt.intR x l := by
  have h10 : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * m + 1)
  by_cases hx0 : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx0, delta_one m _ _ (by unfold f; lib_ring), delta_one m _ _ rfl, delta_zero m _ _ h10.symm, delta_zero m _ _ (fun e => h0 e.symm)]; rfl
  · by_cases hx1 : x = (ring (2 * m + 1 + 1)).one
    · rw [hx1, delta_one m _ _ (by unfold f; lib_ring), delta_zero m _ _ h10, delta_one m _ _ rfl, delta_zero m _ _ (fun e => h1 e.symm)]; rfl
    · by_cases hxl : x = l
      · rw [hxl, delta_one m _ _ (by unfold f; lib_ring), delta_zero m _ _ h0, delta_zero m _ _ h1, delta_one m _ _ rfl]; rfl
      · rw [delta_zero m _ _ hx0, delta_zero m _ _ hx1, delta_zero m _ _ hxl, delta_zero m _ _ (fun e => by
          unfold f at e
          cases nz2 m hpr _ _ e with
          | inl h => cases nz2 m hpr _ _ h with
            | inl h' => exact hx0 h'
            | inr h' => exact hx1 (by
                calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
                  _ = (ring (2 * m + 1 + 1)).one := by rw [h']; lib_ring)
          | inr h => exact hxl (by
              calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)) l := by lib_ring
                _ = l := by rw [h]; lib_ring))]; rfl

/-- **The Legendre traces are even**: for λ ≠ 0, 1, a(λ) = 2k (2-torsion: a(λ) ≡ p − 3 mod 2). -/
theorem legendre_even (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l : Zp (2 * m + 1 + 1)) (h0 : l ≠ (ring (2 * m + 1 + 1)).zero) (h1 : l ≠ (ring (2 * m + 1 + 1)).one) : ∃ k : Int, A m l = 2 * k := by
  -- a = Σχ² + Σ(χ − χ²), Σχ² = p − 3, and χ − χ² ∈ {0, −2}
  have hsq : sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m) = P m + -1 + -1 + -1 := by
    rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 1 1) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero)) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) (LibInt.intR.mul (-1) (delta LibInt.intR x l))))) (fun x => by
      rw [QuadDiagonal.sqchi m hpr, cubic_delta m hpr l x h0 h1]
      show 1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + delta LibInt.intR x (ring (2 * m + 1 + 1)).one + delta LibInt.intR x l) = 1 * 1 + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).one + -1 * delta LibInt.intR x l))
      generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).one = v; generalize delta LibInt.intR x l = w; lib_int)]
    rw [sumK_add, sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, ← mul_sumK, ← mul_sumK, sum_one,
      show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero,
      show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).one) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).one,
      show sumK LibInt.intR (fun x => delta LibInt.intR x l) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR l]
    show 1 * P m + (-1 * 1 + (-1 * 1 + -1 * 1)) = _
    lib_int
  match even_sum m (L m) (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) + -(chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))))) (fun x => by
    rcases chi_cases m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) with e | e | e
    · exact ⟨0, by rw [e]; rfl⟩
    · exact ⟨0, by rw [e]; rfl⟩
    · exact ⟨-1, by rw [e]; rfl⟩) with
  | ⟨k, hk⟩ =>
    have hA : A m l = sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m) + sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))  -- (+231 chars; full line in the Zenodo source)
      unfold A
      rw [← QuadDiagonal.sI_add]
      exact sumK_ext LibInt.intR _ _ (fun x => by generalize chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) = a; lib_int) _
    refine ⟨Int.ofNat m + k, ?_⟩
    rw [hA, hsq, hk]
    show Int.ofNat (2 * m + 1 + 1 + 1) + -1 + -1 + -1 + 2 * k = _
    rw [show Int.ofNat (2 * m + 1 + 1 + 1) = 2 * Int.ofNat m + 3 from rfl]
    lib_int

/-- **Parity of any character sum**: Σ_x χ(g x) + #{x : g x = 0} = p + 2k (so a_p of y² = g(x) ≡ #roots of g + p mod 2). -/
theorem char_parity (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (g : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) :
    ∃ k : Int, sumK LibInt.intR (fun x => chi m (g x)) (L m) + sumK LibInt.intR (fun x => delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero) (L m) = P m + 2 * k := by
  match even_sum m (L m) (fun x => chi m (g x) + -(chi m (g x) * chi m (g x))) (fun x => by
    rcases chi_cases m (g x) with e | e | e
    · exact ⟨0, by rw [e]; rfl⟩
    · exact ⟨0, by rw [e]; rfl⟩
    · exact ⟨-1, by rw [e]; rfl⟩) with
  | ⟨k, hk⟩ =>
    refine ⟨k, ?_⟩
    have h1 : sumK LibInt.intR (fun x => chi m (g x)) (L m)
        = sumK LibInt.intR (fun x => chi m (g x) + -(chi m (g x) * chi m (g x))) (L m) + sumK LibInt.intR (fun x => 1 + -(delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero)) (L m) := by
      rw [← QuadDiagonal.sI_add]
      exact sumK_ext LibInt.intR _ _ (fun x => by rw [← QuadDiagonal.sqchi m hpr]; generalize chi m (g x) = a; lib_int) _
    have h2 : sumK LibInt.intR (fun x => 1 + -(delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero)) (L m) = P m + -(sumK LibInt.intR (fun x => delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero) (L m)) := by
      rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 1 1) (LibInt.intR.mul (-1) (delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero))) (fun x => by
        show _ = 1 * 1 + -1 * _; generalize delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero = d; lib_int), sumK_add, ← mul_sumK, ← mul_sumK, sum_one]
      show 1 * P m + -1 * _ = _
      generalize sumK LibInt.intR (fun x => delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero) (L m) = D; lib_int
    rw [h1, h2, hk]
    generalize sumK LibInt.intR (fun x => delta LibInt.intR (g x) (ring (2 * m + 1 + 1)).zero) (L m) = D; lib_int

/-- **χ(−3) = 1 ⟹ 3 | p − 1**: z = (−1 + √−3)/2 is a primitive cube root of unity. -/
theorem three_div (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (hc : chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m   -- (+99 chars; full line in the Zenodo source)
  have hm3 : ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := negnz m _ h3
  have hpw : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) (m + 1) = (ring (2 * m + 1 + 1)).one := by
    by_cases h : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) (m + 1) = (ring (2 * m + 1 + 1)).one
    · exact h
    · exact absurd (hc.symm.trans (chi_neg_of m _ hm3 h)) (fun h' => int_m1_ne h'.symm)
  match QuadDiagonal.sq_root m hpr _ hm3 hpw with
  | ⟨s, hs⟩ =>
    have e2 : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ (QuadDiagonal.two_ne m)
    -- 2z + 1 = s, so 4(z² + z + 1) = s² + 3 = 0
    have h2z : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s))) (ring (2 * m + 1 + 1)).one = s := by
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) (ring (2 * m + 1 + 1)).one := by lib_rin  -- (+1 chars; full line in the Zenodo source)
        _ = s := by rw [e2]; lib_ring
    have hq : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 +   -- (+603 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1   -- (+572 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [h2z, hs]; lib_ring
    have hq' : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring  -- (+380 chars; full line in the Zenodo source)
      cases nz2 m hpr _ _ hq with
      | inl h => exact absurd h (nzm m hpr _ _ (QuadDiagonal.two_ne m) (QuadDiagonal.two_ne m))
      | inr h => exact h
    have hz3 : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) ((1 + 1 + 1) ^ (0 + 1)) = (ring (2 * m + 1 + 1)).one := by
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s))) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 *  -- (+369 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 *  -- (+735 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).one := by rw [hq']; lib_ring
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) ((1 + 1 + 1) ^ 0) ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      have e' : ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) = (ring (2 * m + 1 + 1)).one := by
        calc ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) s)) = (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring   -- (+95 chars; full line in the Zenodo source)
          _ = (ring (2 * m + 1 + 1)).one := e
      apply h3
      calc ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 +   -- (+505 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := hq'
    exact CycloPow.dvd_of_pow (2 * m + 1) hpr 1 0 StrongMult1.three_prime _ hz3 hz1

theorem no_grow (a b : Nat) (h : a = a + b + 1) : False := by
  have h' : a < a + b + 1 := Nat.lt_succ_of_le (Nat.le_add_right a b)
  rw [← h] at h'
  exact Nat.lt_irrefl a h'

/-- **3 | p − 1 ⟹ χ(−3) = 1**: with z = g^((p−1)/3), (2z + 1)² = −3. -/
theorem three_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (c : Nat) (hc : 2 * m + 1 + 1 = 3 * c) : chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (  -- (+93 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz3 : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) = (ring (2 * m + 1 + 1)).one := by
      have : Frobenius.pw (ring (2 * m + 1 + 1)) g (c * 3) = (ring (2 * m + 1 + 1)).one := by rw [show c * 3 = 2 * m + 1 + 1 by rw [hc]; lib_nat]; exact hg.1
      rw [← TwoPower.pwpow] at this
      rw [← this]; show _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _) _; lib_ring
    have hz1 : (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            have e1 : c' + 1 = 3 * (c' + 1) * (k' + 1) := by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 3 * (c' + 1) * (k' + 1) := by rw [hc1]
            exact no_grow (c' + 1) (2 * c' + 1 + 3 * (c' + 1) * k') (by
              calc c' + 1 = 3 * (c' + 1) * (k' + 1) := e1
                _ = _ := by lib_nat)
    have hq : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
      have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (ring (2 * m + 1 + 1  -- (+44 chars; full line in the Zenodo source)
        calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).zero := by rw [hz3]; lib_ring
      cases nz2 m hpr _ _ e with
      | inl h => exact absurd (by
          calc (Frobenius.pw (ring (2 * m + 1 + 1)) g c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
            _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
      | inr h => exact h
    have hsq : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (Frobeniu  -- (+235 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1))   -- (+288 chars; full line in the Zenodo source)
        _ = _ := by rw [hq]; lib_ring
    have hne : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero := fun e => negnz m _ h3 (by rw [← hsq, e]; lib_ring)
    rw [← hsq]; exact chi_sq m hpr _ hne

/-- **χ(−1) = 1 ⟹ 4 | p − 1** (i² = −1 has order 4). -/
theorem four_div (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hc : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 1) : Dv ((0 + 1 + 1) ^ (1 + 1)) (2 * m + 1 + 1) := by
  have hm10 : ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := negnz m _ (PowerSum.one_ne_zero' (2 * m + 1))
  have hpw : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (m + 1) = (ring (2 * m + 1 + 1)).one := by
    by_cases h : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (m + 1) = (ring (2 * m + 1 + 1)).one
    · exact h
    · exact absurd (hc.symm.trans (chi_neg_of m _ hm10 h)) (fun h' => int_m1_ne h'.symm)
  match QuadDiagonal.sq_root m hpr _ hm10 hpw with
  | ⟨i, hi⟩ =>
    have h4 : Frobenius.pw (ring (2 * m + 1 + 1)) i ((0 + 1 + 1) ^ (1 + 1)) = (ring (2 * m + 1 + 1)).one := by
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one i) i) i) i = (ring (2 * m + 1 + 1)).one
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i i) ((ring (2 * m + 1 + 1)).mul i i) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [hi]; lib_ring
    have h2 : Frobenius.pw (ring (2 * m + 1 + 1)) i ((0 + 1 + 1) ^ 1) ≠ (ring (2 * m + 1 + 1)).one := fun e => ne_pm m (by
      calc (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul i i := hi.symm
        _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one i) i := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := e)
    exact CycloPow.dvd_of_pow (2 * m + 1) hpr 0 1 StrongMult1.two_prime _ h4 h2

/-- **4 | p − 1 ⟹ χ(−1) = 1** (z = g^((p−1)/4) has z² = −1). -/
theorem four_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Nat) (hc : 2 * m + 1 + 1 = 4 * c) : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 1 := by
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hw : Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c) ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 (2 * c) e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show 2 * (c' + 1) = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (2 * (c' + 1)) (2 * c' + 1 + 4 * (c' + 1) * k') (by
              calc 2 * (c' + 1) = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 4 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hsq : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) = (ring (2 * m + 1 + 1)).one := by
      have : Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c * 2) = (ring (2 * m + 1 + 1)).one := by rw [show 2 * c * 2 = 2 * m + 1 + 1 by rw [hc]; lib_nat]; exact hg.1
      rw [← TwoPower.pwpow] at this
      rw [← this]; show _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _; lib_ring
    have hm : Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
      have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
        calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).zero := by rw [hsq]; lib_ring
      cases nz2 m hpr _ _ e with
      | inl h => exact absurd (by
          calc Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
            _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hw
      | inr h => calc Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (2 * c)) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = _ := by rw [h]; lib_ring
    have hg0 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
      have := hm
      rw [show 2 * c = c * 2 by lib_nat, ← TwoPower.pwpow, e] at this
      exact negnz m _ (PowerSum.one_ne_zero' (2 * m + 1)) (by rw [← this]; show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero = _; lib_ring)
    rw [← hm, show 2 * c = c * 2 by lib_nat, ← TwoPower.pwpow, show Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) g c) 2 = (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c) by
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _ = _; lib_ring]
    exact chi_sq m hpr _ hg0

theorem chi_one_pw (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hc : chi m x = 1) : Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1) = (ring (2 * m + 1 + 1)).one := by
  by_cases h : Frobenius.pw (ring (2 * m + 1 + 1)) x (m + 1) = (ring (2 * m + 1 + 1)).one
  · exact h
  · exact absurd (hc.symm.trans (chi_neg_of m _ hx h)) (fun h' => int_m1_ne h'.symm)

/-- **χ(−1) = χ(2) = 1 ⟹ 8 | p − 1**: z = (1 + i)/√2 has z² = i, order 8. -/
theorem eight_div (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hc1 : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 1) (hc2 : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = 1) : Dv ((0 + 1 + 1) ^ (2 + 1)) (2 * m + 1 + 1) := by
  have hm10 : ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := negnz m _ (PowerSum.one_ne_zero' (2 * m + 1))
  match QuadDiagonal.sq_root m hpr _ hm10 (chi_one_pw m _ hm10 hc1), QuadDiagonal.sq_root m hpr _ (QuadDiagonal.two_ne m) (chi_one_pw m _ (QuadDiagonal.two_ne m) hc2) with
  | ⟨i, hi⟩, ⟨r, hr⟩ =>
    have hr0 : r ≠ (ring (2 * m + 1 + 1)).zero := fun h0 => QuadDiagonal.two_ne m (by rw [← hr, h0]; lib_ring)
    have er : (ring (2 * m + 1 + 1)).mul (iv m r) r = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr r hr0
    -- z = (1 + i)·r⁻¹, z² = 2i·r⁻² = i
    have hz2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r)) = i := by
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul i i)) ((ring (2 * m + 1 + 1)).add i i)) ((ring (2 * m + 1 + 1)).mul (iv m r) (iv m r)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul r r) i) ((ring (2 * m + 1 + 1)).mul (iv m r) (iv m r)) := by rw [hi, hr]; lib_ring
        _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m r) r) ((ring (2 * m + 1 + 1)).mul (iv m r) r)) i := by lib_ring
        _ = i := by rw [er]; lib_ring
    have h8 : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r)) ((0 + 1 + 1) ^ (2 + 1)) = (ring (2 * m + 1 + 1)).one := by
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _) _) _) _) _) _) _ = _
      generalize hz : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r) = z at hz2 ⊢
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [hz2, hi]; lib_ring
    have h4 : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r)) ((0 + 1 + 1) ^ 2) ≠ (ring (2 * m + 1 + 1)).one := fun e => ne_pm m (by
      have e' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).  -- (+174 chars; full line in the Zenodo source)
      generalize hz : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) (iv m r) = z at hz2 e'
      calc (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul i i := hi.symm
        _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z) := by rw [hz2]
        _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one z) z) z) z := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := e')
    exact CycloPow.dvd_of_pow (2 * m + 1) hpr 0 2 StrongMult1.two_prime _ h8 h4

theorem pw_mul_add (x : Zp (2 * m + 1 + 1)) (a b : Nat) : Frobenius.pw (ring (2 * m + 1 + 1)) x (a + b) = (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) x a) (Frobenius.pw (ring (2 * m + 1 + 1)) x b) := by
  induction b with
  | zero => show Frobenius.pw (ring (2 * m + 1 + 1)) x a = (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) x a) (ring (2 * m + 1 + 1)).one; lib_ring
  | succ b ih => show (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) x (a + b)) x = (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) x a) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) x b) x); rw [ih]; lib_ring

/-- **8 | p − 1 ⟹ χ(2) = 1**: z = g^((p−1)/8) has z⁴ = −1 and (z + z⁷)² = 2. -/
theorem eight_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Nat) (hc : 2 * m + 1 + 1 = 8 * c) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = 1 := by
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hw : Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c) ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 (4 * c) e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show 4 * (c' + 1) = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (4 * (c' + 1)) (4 * c' + 3 + 8 * (c' + 1) * k') (by
              calc 4 * (c' + 1) = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 8 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have h8 : Frobenius.pw (ring (2 * m + 1 + 1)) g (8 * c) = (ring (2 * m + 1 + 1)).one := by rw [← hc]; exact hg.1
    have hsq : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) = (ring (2 * m + 1 + 1)).one := by
      rw [← pw_mul_add, show 4 * c + 4 * c = 8 * c by lib_nat, h8]
    have h4 : Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
      have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
        calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).zero := by rw [hsq]; lib_ring
      cases nz2 m hpr _ _ e with
      | inl h => exact absurd (by
          calc Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
            _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hw
      | inr h => calc Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c)) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = _ := by rw [h]; lib_ring
    -- powers of z = g^c
    have p4 : Frobenius.pw (ring (2 * m + 1 + 1)) g (4 * c) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) := by
      rw [show 4 * c = c + c + (c + c) by lib_nat, pw_mul_add, pw_mul_add]
    have p8 : Frobenius.pw (ring (2 * m + 1 + 1)) g (8 * c) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul   -- (+204 chars; full line in the Zenodo source)
      rw [show 8 * c = 4 * c + 4 * c by lib_nat, pw_mul_add, p4]
    have hw2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring  -- (+180 chars; full line in the Zenodo source)
        ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul (Frobenius.p  -- (+237 chars; full line in the Zenodo source)
      have a4 := h4; rw [p4] at a4
      have a8 := h8; rw [p8] at a8
      generalize (Frobenius.pw (ring (2 * m + 1 + 1)) g c) = z at a4 a8 ⊢
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ri  -- (+66 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul z z)) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (ring (2 * m +  -- (+27 chars; full line in the Zenodo source)
        _ = ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) := by lib_ring
    have hne : (ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)) ((ring (2 * m + 1 + 1)).mul (Frobe  -- (+193 chars; full line in the Zenodo source)
      QuadDiagonal.two_ne m (by rw [← hw2, e]; lib_ring)
    rw [← hw2]; exact chi_sq m hpr _ hne

theorem chi3_split (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2  -- (+81 chars; full line in the Zenodo source)
  rw [← chi_mul m hpr, show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2   -- (+32 chars; full line in the Zenodo source)

theorem chi_pm (t : Zp (2 * m + 1 + 1)) (ht : t ≠ (ring (2 * m + 1 + 1)).zero) (hn : chi m t ≠ 1) : chi m t = -1 := by
  rcases chi_cases m t with e | e | e
  · unfold chi at e; rw [ite_neg' _ _ ht] at e
    by_cases h : Frobenius.pw (ring (2 * m + 1 + 1)) t (m + 1) = (ring (2 * m + 1 + 1)).one
    · rw [ite_pos' _ _ h] at e; exact absurd (Int.ofNat.inj e) (fun h' => Nat.noConfusion h')
    · rw [ite_neg' _ _ h] at e; exact absurd e (fun h' => Int.noConfusion h')
  · exact absurd e hn
  · exact e

/-- **p ≡ 1 (mod 12) ⟹ χ(3) = 1.** -/
theorem chi3_one (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (c : Nat) (hc : 2 * m + 1 + 1 = 12 * c) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring   -- (+59 chars; full line in the Zenodo source)
  rw [chi3_split m hpr, four_div_conv m hpr (3 * c) (by rw [hc]; lib_nat), three_div_conv m hpr h3 (4 * c) (by rw [hc]; lib_nat)]; rfl

/-- **p ≡ 11 (mod 12) ⟹ χ(3) = 1** (both −1 and −3 are non-squares). -/
theorem chi3_eleven (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (c : Nat) (hc : 2 * m + 1 + 1 = 12 * c + 10) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).ad  -- (+67 chars; full line in the Zenodo source)
  have hm1 : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = -1 := chi_pm m _ (negnz m _ (PowerSum.one_ne_zero' (2 * m + 1))) (fun e => by
    match four_div m hpr e with
    | ⟨k, hk⟩ =>
      have : (3 * c + 2) * 4 + 2 = k * 4 + 0 := by
        calc (3 * c + 2) * 4 + 2 = 2 * m + 1 + 1 := by rw [hc]; lib_nat
          _ = k * ((0 + 1 + 1) ^ (1 + 1)) := hk
          _ = k * 4 + 0 := rfl
      exact Nat.noConfusion (Inf2e1.uniq_remG 4 _ _ 2 0 (Nat.le_add_left 3 1) (Nat.le_add_left 1 3) this))
  have hm3 : chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = -1 := chi_pm m _ (negnz m _ h3) (fun e => by
    match three_div m hpr h3 e with
    | ⟨k, hk⟩ =>
      have : (4 * c + 3) * 3 + 1 = k * 3 + 0 := by
        calc (4 * c + 3) * 3 + 1 = 2 * m + 1 + 1 := by rw [hc]; lib_nat
          _ = k * ((1 + 1 + 1) ^ (0 + 1)) := hk
          _ = k * 3 + 0 := rfl
      exact Nat.noConfusion (Inf2e1.uniq_remG 3 _ _ 1 0 (Nat.le_add_left 2 1) (Nat.le_add_left 1 2) this))
  rw [chi3_split m hpr, hm1, hm3]; rfl

/-- **p ≡ 5 (mod 12) ⟹ χ(3) = −1.** -/
theorem chi3_five (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (c : Nat) (hc : 2 * m + 1 + 1 = 12 * c + 4) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (  -- (+65 chars; full line in the Zenodo source)
  have hm3 : chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = -1 := chi_pm m _ (negnz m _ h3) (fun e => by
    match three_div m hpr h3 e with
    | ⟨k, hk⟩ =>
      have : (4 * c + 1) * 3 + 1 = k * 3 + 0 := by
        calc (4 * c + 1) * 3 + 1 = 2 * m + 1 + 1 := by rw [hc]; lib_nat
          _ = k * ((1 + 1 + 1) ^ (0 + 1)) := hk
          _ = k * 3 + 0 := rfl
      exact Nat.noConfusion (Inf2e1.uniq_remG 3 _ _ 1 0 (Nat.le_add_left 2 1) (Nat.le_add_left 1 2) this))
  rw [chi3_split m hpr, four_div_conv m hpr (3 * c + 1) (by rw [hc]; lib_nat), hm3]; rfl

/-- **p ≡ 7 (mod 12) ⟹ χ(3) = −1.** -/
theorem chi3_seven (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (c : Nat) (hc : 2 * m + 1 + 1 = 12 * c + 6) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add   -- (+66 chars; full line in the Zenodo source)
  have hm1 : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = -1 := chi_pm m _ (negnz m _ (PowerSum.one_ne_zero' (2 * m + 1))) (fun e => by
    match four_div m hpr e with
    | ⟨k, hk⟩ =>
      have : (3 * c + 1) * 4 + 2 = k * 4 + 0 := by
        calc (3 * c + 1) * 4 + 2 = 2 * m + 1 + 1 := by rw [hc]; lib_nat
          _ = k * ((0 + 1 + 1) ^ (1 + 1)) := hk
          _ = k * 4 + 0 := rfl
      exact Nat.noConfusion (Inf2e1.uniq_remG 4 _ _ 2 0 (Nat.le_add_left 3 1) (Nat.le_add_left 1 3) this))
  rw [chi3_split m hpr, hm1, three_div_conv m hpr h3 (4 * c + 2) (by rw [hc]; lib_nat)]; rfl

/-- The quadratic Gauss sum of order 5 inside 𝔽_p: z⁵ = 1, z ≠ 1 ⟹ (z − z² − z³ + z⁴)² = 5. -/
theorem gauss5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hz5 : ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) = (ring (2 * m + 1 + 1)).one) (hz1 : z ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z (  -- (+877 chars; full line in the Zenodo source)
  have hphi : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))))) = (ring (2 * m + 1 + 1)).zero := by
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)) ((ring (2 * m + 1 + 1)).mul z ((rin  -- (+99 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z z)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hz5]; lib_ring
    cases nz2 m hpr _ _ e with
    | inl h => exact absurd (by
        calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
    | inr h => exact h
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one   -- (+1089 chars; full line in the Zenodo source)
    _ = ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) := by rw [hz5, hphi]; lib_ring

/-- **5 | p − 1 ⟹ χ(5) = 1** (z = g^((p−1)/5) and the Gauss sum of order 5). -/
theorem five_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h5 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) ≠ (ring (2 * m + 1 + 1)).zero) (c : Nat) (hc : 2 *   -- (+287 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (c' + 1) (4 * c' + 3 + 5 * (c' + 1) * k') (by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 5 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hz5 : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) = (ring (2 * m + 1 + 1)).one := by
      rw [← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, show c + (c + (c + (c + c))) = 2 * m + 1 + 1 by rw [hc]; lib_nat]
      exact hg.1
    have hs := gauss5 m hpr _ hz5 hz1
    have hs0 : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) (Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1   -- (+426 chars; full line in the Zenodo source)
    rw [← hs]; exact chi_sq m hpr _ hs0

/-- The quadratic Gauss sum of order 7 inside 𝔽_p: z⁷ = 1 ≠ z ⟹ (z + z² − z³ + z⁴ − z⁵ − z⁶)² = −7. -/
theorem gauss7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hz7 : (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z)))))) = (ring (2 * m + 1 + 1)).one) (hz1 : z ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).m  -- (+2552 chars; full line in the Zenodo source)
  have hphi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z)))) ((ring (2 * m +   -- (+339 chars; full line in the Zenodo source)
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add  -- (+454 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hz7]; lib_ring
    cases nz2 m hpr _ _ e with
    | inl h => exact absurd (by
        calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
    | inr h => exact h
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1  -- (+2219 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+42 chars; full line in the Zenodo source)

/-- **7 | p − 1 ⟹ χ(−7) = 1.** -/
theorem seven_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h7 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1  -- (+567 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (c' + 1) (6 * c' + 5 + 7 * (c' + 1) * k') (by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 7 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hz7 : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobeni  -- (+117 chars; full line in the Zenodo source)
      rw [← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add,
        show c + (c + (c + (c + (c + (c + c))))) = 2 * m + 1 + 1 by rw [hc]; lib_nat]
      exact hg.1
    have hs := gauss7 m hpr _ hz7 hz1
    have hs0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul  -- (+1604 chars; full line in the Zenodo source)
    rw [← hs]; exact chi_sq m hpr _ hs0

/-- The quadratic Gauss sum of order 11 inside 𝔽_p: z^11 = 1 ≠ z ⟹ (Σ_k (k/11) z^k)² = -11. -/
theorem gauss11 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hzl : (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m  -- (+5613 chars; full line in the Zenodo source)
  have hphi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z)))) ((ring (2 * m +   -- (+1401 chars; full line in the Zenodo source)
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add  -- (+1516 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))))))))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1  -- (+26 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hzl]; lib_ring
    cases nz2 m hpr _ _ e with
    | inl h => exact absurd (by
        calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
    | inr h => exact h
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1  -- (+5170 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+274 chars; full line in the Zenodo source)

/-- **11 | p − 1 ⟹ χ(-11) = 1.** -/
theorem eleven_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m +   -- (+1033 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (c' + 1) (10 * c' + 9 + 11 * (c' + 1) * k') (by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 11 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hzl : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobeni  -- (+401 chars; full line in the Zenodo source)
      rw [← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add,
        show c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c)))))))))) = 2 * m + 1 + 1 by rw [hc]; lib_nat]
      exact hg.1
    have hs := gauss11 m hpr _ hzl hz1
    have hs0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) ((ring (2 * m + 1 + 1)).ad  -- (+4316 chars; full line in the Zenodo source)
    rw [← hs]; exact chi_sq m hpr _ hs0

/-- The quadratic Gauss sum of order 13 inside 𝔽_p: z^13 = 1 ≠ z ⟹ (Σ_k (k/13) z^k)² = 13. -/
theorem gauss13 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hzl : (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m  -- (+7486 chars; full line in the Zenodo source)
  have hphi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z)))) ((ring (2 * m +   -- (+2118 chars; full line in the Zenodo source)
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add  -- (+2233 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul   -- (+88 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hzl]; lib_ring
    cases nz2 m hpr _ _ e with
    | inl h => exact absurd (by
        calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
    | inr h => exact h
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1  -- (+7331 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1))  -- (+361 chars; full line in the Zenodo source)

/-- **13 | p − 1 ⟹ χ(13) = 1.** -/
theorem thirteen_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m   -- (+1238 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (c' + 1) (12 * c' + 11 + 13 * (c' + 1) * k') (by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 13 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hzl : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobeni  -- (+543 chars; full line in the Zenodo source)
      rw [← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add,
        show c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c)))))))))))) = 2 * m + 1 + 1 by rw [hc]; lib_nat]
      exact hg.1
    have hs := gauss13 m hpr _ hzl hz1
    have hs0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) ((ring (2 * m + 1 + 1)).ad  -- (+6088 chars; full line in the Zenodo source)
    rw [← hs]; exact chi_sq m hpr _ hs0

/-- **Stickelberger for a cubic with a root**: for x³ + ax + b = (x − r)(x² + rx + c), Δ = −4a³ − 27b² = (r² − 4c)(c + 2r²)²,
    so χ(Δ) = χ(r² − 4c) when r is not a root of the quadratic factor. -/
theorem stick_root (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (r c : Zp (2 * m + 1 + 1)) (hw : (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul r r)) ≠ (ring (2 * m + 1 + 1)).zero) :
    chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).m  -- (+1245 chars; full line in the Zenodo source)
  rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).m  -- (+1687 chars; full line in the Zenodo source)
  generalize chi m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul r r) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) c))) = e; lib_int

/-- **Three roots in 𝔽_p ⟹ the discriminant is a square**: roots r₁, r₂, −r₁ − r₂ pairwise distinct ⟹ χ(Δ) = 1. -/
theorem stick_three (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (r1 r2 : Zp (2 * m + 1 + 1)) (hd : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add r1 ((ring (2 * m + 1 + 1)).neg r2)) ((ring (2 * m + 1 + 1)).add r1 ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add r1 r2))))) ((ring (2 * m + 1 + 1)).add r2  -- (+2214 chars; full line in the Zenodo source)
  rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul r1 r2) ((ring (2 * m + 1 +   -- (+2465 chars; full line in the Zenodo source)
  exact chi_sq m hpr _ hd

/-- The quadratic Gauss sum of order 17 inside 𝔽_p: (Σ_k (k/17) z^k)² = 17. -/
theorem gauss17 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hzl : (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m  -- (+12034 chars; full line in the Zenodo source)
  have hphi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z)))) ((ring (2 * m +   -- (+3924 chars; full line in the Zenodo source)
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add  -- (+4039 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul   -- (+212 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hzl]; lib_ring
    cases nz2 m hpr _ _ e with
    | inl h => exact absurd (by
        calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
    | inr h => exact h
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1  -- (+11313 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1))  -- (+593 chars; full line in the Zenodo source)

/-- **17 | p − 1 ⟹ χ(17) = 1.** -/
theorem seventeen_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m  -- (+1703 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (c' + 1) (16 * c' + 15 + 17 * (c' + 1) * k') (by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 17 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hzl : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobeni  -- (+827 chars; full line in the Zenodo source)
      rw [← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add,
        show c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c)))))))))))))))) = 2 * m + 1 + 1 by rw [hc]; lib_nat]
      exact hg.1
    have hs := gauss17 m hpr _ hzl hz1
    have hs0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul  -- (+10504 chars; full line in the Zenodo source)
    rw [← hs]; exact chi_sq m hpr _ hs0

/-- The quadratic Gauss sum of order 19 inside 𝔽_p: (Σ_k (k/19) z^k)² = -19. -/
theorem gauss19 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hzl : (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m  -- (+14709 chars; full line in the Zenodo source)
  have hphi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z)))) ((ring (2 * m +   -- (+5013 chars; full line in the Zenodo source)
    have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (z) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z (z)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z (z))) ((ring (2 * m + 1 + 1)).add  -- (+5128 chars; full line in the Zenodo source)
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).mul   -- (+274 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [hzl]; lib_ring
    cases nz2 m hpr _ _ e with
    | inl h => exact absurd (by
        calc z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [h]; lib_ring) hz1
    | inr h => exact h
  calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1  -- (+14714 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1))  -- (+738 chars; full line in the Zenodo source)

/-- **19 | p − 1 ⟹ χ(-19) = 1.** -/
theorem nineteen_div_conv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hl : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m   -- (+1963 chars; full line in the Zenodo source)
  match PrimRoot.primitive_root (2 * m + 1) hpr with
  | ⟨g, hg⟩ =>
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) g c ≠ (ring (2 * m + 1 + 1)).one := fun e => by
      match hg.2 c e with
      | ⟨k, hk⟩ =>
        revert hk hc
        cases c with
        | zero => intro hc0 _; exact Nat.noConfusion hc0
        | succ c' =>
          intro hc1 hk
          cases k with
          | zero => exact Nat.noConfusion (show c' + 1 = 0 by rw [hk]; lib_nat)
          | succ k' =>
            exact no_grow (c' + 1) (18 * c' + 17 + 19 * (c' + 1) * k') (by
              calc c' + 1 = (2 * m + 1 + 1) * (k' + 1) := hk
                _ = 19 * (c' + 1) * (k' + 1) := by rw [hc1]
                _ = _ := by lib_nat)
    have hzl : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((ring (2 * m + 1 + 1)).mul (Frobeni  -- (+969 chars; full line in the Zenodo source)
      rw [← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add, ← pw_mul_add,
        show c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c + (c)))))))))))))))))) = 2 * m + 1 + 1 by rw [hc]; lib_nat]
      exact hg.1
    have hs := gauss19 m hpr _ hzl hz1
    have hs0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) g c) ((Frobenius.pw (ring (2 * m + 1 + 1)) g c)))) ((ring (2 * m + 1 + 1)).ad  -- (+13148 chars; full line in the Zenodo source)
    rw [← hs]; exact chi_sq m hpr _ hs0

/-- **#{(x, y) : x² = y²} = 2p − 1.** -/
theorem square_pairs (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y)) (L m)) (L m) = P m + P m + -1 := by
  have hrow : ∀ x, sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y y)) (L m) = 2 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) := fun x => by
    by_cases hx : x = (ring (2 * m + 1 + 1)).zero
    · rw [hx, delta_one m _ _ rfl]
      rw [sumK_ext LibInt.intR _ (fun y => delta LibInt.intR y (ring (2 * m + 1 + 1)).zero) (fun y => by
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [hy, delta_one m _ _ (by lib_ring), delta_one m _ _ rfl]
        · rw [delta_zero m _ _ hy, delta_zero m _ _ (fun e => hy (by
            cases nz2 m hpr _ _ e.symm with
            | inl h => exact h
            | inr h => exact h))]),
        show sumK LibInt.intR (fun y => delta LibInt.intR y (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
      rfl
    · rw [sumK_ext LibInt.intR _ _ (fun y => roots_sq m hpr x y hx), QuadDiagonal.sI_add,
        show sumK LibInt.intR (fun y => delta LibInt.intR y x) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR x,
        show sumK LibInt.intR (fun y => delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg x)) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR ((ring (2 * m + 1 + 1)).neg x),
        delta_zero m _ _ hx]
      rfl
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 2 1) (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero))) (fun x => by
    rw [hrow x]; show _ = 2 * 1 + -1 * _; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = d; lib_int), sumK_add, ← mul_sumK, ← mul_sumK, sum_one,
    show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero]
  show 2 * P m + -1 * 1 = _
  lib_int

/-- **Σ_λ (Σ_x χ(x² + λ))² = p² − p** (translation_moment with #{x² = y²} = 2p − 1). -/
theorem square_family (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    sumK LibInt.intR (fun l => sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).mul x x))) (L m) * sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).add l ((ring (2 * m + 1 + 1)).mul x x))) (L m)) (L m)
      = P m * P m + -(P m) := by
  rw [translation_moment m hpr (fun x => (ring (2 * m + 1 + 1)).mul x x), square_pairs m hpr]
  lib_int

end LegendreMoment

#print axioms LegendreMoment.pt
#print axioms LegendreMoment.sq_expand
#print axioms LegendreMoment.neg_delta
#print axioms LegendreMoment.sum_f
#print axioms LegendreMoment.f_delta
#print axioms LegendreMoment.sum_f2
#print axioms LegendreMoment.legendre_moment
#print axioms LegendreMoment.refl_one
#print axioms LegendreMoment.refl_inv
#print axioms LegendreMoment.sum_A
#print axioms LegendreMoment.twisted_sum
#print axioms LegendreMoment.pt1
#print axioms LegendreMoment.delta_shift1
#print axioms LegendreMoment.ff1
#print axioms LegendreMoment.adjacent
#print axioms LegendreMoment.odd_moments
#print axioms LegendreMoment.family_moment
#print axioms LegendreMoment.root_count
#print axioms LegendreMoment.quad_family
#print axioms LegendreMoment.translation_moment
#print axioms LegendreMoment.cube_pairs
#print axioms LegendreMoment.row
#print axioms LegendreMoment.cube_roots
#print axioms LegendreMoment.fourth_roots
#print axioms LegendreMoment.sixth_roots
#print axioms LegendreMoment.int_m1_ne
#print axioms LegendreMoment.quartic_m1
#print axioms LegendreMoment.eighth_roots
#print axioms LegendreMoment.cube_m1
#print axioms LegendreMoment.sixth_m1
#print axioms LegendreMoment.twelfth_roots
#print axioms LegendreMoment.pair_sum
#print axioms LegendreMoment.translation_cov
#print axioms LegendreMoment.jacobi_quad
#print axioms LegendreMoment.chi_cases
#print axioms LegendreMoment.even_sum
#print axioms LegendreMoment.cubic_delta
#print axioms LegendreMoment.legendre_even
#print axioms LegendreMoment.char_parity
#print axioms LegendreMoment.three_div
#print axioms LegendreMoment.no_grow
#print axioms LegendreMoment.three_div_conv
#print axioms LegendreMoment.four_div
#print axioms LegendreMoment.four_div_conv
#print axioms LegendreMoment.chi_one_pw
#print axioms LegendreMoment.eight_div
#print axioms LegendreMoment.pw_mul_add
#print axioms LegendreMoment.eight_div_conv
#print axioms LegendreMoment.chi3_split
#print axioms LegendreMoment.chi_pm
#print axioms LegendreMoment.chi3_one
#print axioms LegendreMoment.chi3_eleven
#print axioms LegendreMoment.chi3_five
#print axioms LegendreMoment.chi3_seven
#print axioms LegendreMoment.gauss5
#print axioms LegendreMoment.five_div_conv
#print axioms LegendreMoment.gauss7
#print axioms LegendreMoment.seven_div_conv
#print axioms LegendreMoment.gauss11
#print axioms LegendreMoment.eleven_div_conv
#print axioms LegendreMoment.gauss13
#print axioms LegendreMoment.thirteen_div_conv
#print axioms LegendreMoment.stick_root
#print axioms LegendreMoment.stick_three
#print axioms LegendreMoment.gauss17
#print axioms LegendreMoment.seventeen_div_conv
#print axioms LegendreMoment.gauss19
#print axioms LegendreMoment.nineteen_div_conv
#print axioms LegendreMoment.square_pairs
#print axioms LegendreMoment.square_family

#print axioms: 70 theorems, no axioms.

78.96 The law for 5, the remaining class

If \(q\equiv 4\pmod 5\) then the Gauss sum \(g=\zeta -\zeta ^2-\zeta ^3+\zeta ^4\) in \(\mathbb F_q[\zeta _5]\) is fixed by Frobenius, so \(5\) is a square.

\[ q\equiv 4\pmod 5\ \Longrightarrow \ \chi (5)=1. \]

lang/Gauss5Conv.lean

/-
  lang/Gauss5Conv — T43, cycle 347: q ≡ 4 (mod 5) ⟹ 5 is a square mod q (the missing direction of the law for 5).

  In C = 𝔽_q[ζ]/(Φ₅) (Gauss5): g = ζ − ζ² − ζ³ + ζ⁴ has g² = 5, and g^q = ζ^q − ζ^(2q) − ζ^(3q) + ζ^(4q). For q = 5s + 4 the exponents
  are 4, 3, 2, 1 mod 5, so g^q = g (combo4). Writing g^q = (g²)^((q−1)/2)·g = (5^((q−1)/2), 0, 0, 0)·g and comparing first
  coordinates (g₁ = −1) gives 5^((q−1)/2) = 1, i.e. χ(5) = 1.
    sq5_conv     q = 5s + 4 ⟹ χ(5) = 1
  With Gauss5.sq5_mod5 (χ(5) = 1 ⟹ q ≡ ±1) and LegendreMoment.five_div_conv (q ≡ 1 ⟹ χ(5) = 1): χ(5) = 1 ⟺ q ≡ ±1 (mod 5).
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Gauss5
import lang.Inf2e1

namespace Gauss5Conv

open FTAChain ZMod Frobenius Gauss5

variable (r : Nat)

theorem sq5_conv (hpr : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = (2 * r + 1 + 1 + 1)) (s : Nat) (h : 2 * r + 1 + 1 + 1 = 5 * s + 4) :
    QChar.chi r ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one (ring (2 * r + 1 + 1)).one)))) = 1 := by
  have h5 : (2 * r + 1 + 1 + 1) ≠ 5 := fun e => by
    have : s * 5 + 4 = 1 * 5 + 0 := by rw [show s * 5 + 4 = 5 * s + 4 by lib_nat, ← h, e]
    exact Nat.noConfusion (Inf2e1.uniq_remG 5 _ _ 4 0 (Nat.le_add_left 5 0) (Nat.le_add_left 1 4) this)
  have hG := frob_g r hpr
  rw [show 1 * (2 * r + 1 + 1 + 1) = 5 * s + 4 by rw [h]; lib_nat, show 2 * (2 * r + 1 + 1 + 1) = 5 * (2 * s + 1) + 3 by rw [h]; lib_nat,
    show 3 * (2 * r + 1 + 1 + 1) = 5 * (3 * s + 2) + 2 by rw [h]; lib_nat, show 4 * (2 * r + 1 + 1 + 1) = 5 * (4 * s + 3) + 1 by rw [h]; lib_nat,
    z_mod, z_mod, z_mod, z_mod, combo4] at hG
  -- g^q = (g²)^(r+1)·g = (5^(r+1), 0, 0, 0)·g
  rw [show (2 * r + 1 + 1 + 1) = 2 * (r + 1) + 1 by lib_nat] at hG
  have hG' : (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (gg r) (2 * (r + 1))) (gg r) = gg r := hG
  rw [← TwoPower.pwpow, show pw (Cyclo5.cyc5 (ring (2 * r + 1 + 1))) (gg r) 2 = (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (gg r) (gg r) from by
      show (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul ((Cyclo5.cyc5 (ring (2 * r + 1 + 1))).mul (Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one (gg r)) (gg r) = _
      rw [(Cyclo5.cyc5 (ring (2 * r + 1 + 1))).one_mul],
    g_sq, pw_embed5, scal_mul] at hG'
  have e1 : (ring (2 * r + 1 + 1)).mul (pw (ring (2 * r + 1 + 1)) ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one (ring (2 * r + 1 + 1)).one)))) (r + 1)) ((ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).one)
      = (ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).one := congrArg Prod.fst hG'
  apply QChar.chi_one_of r _ (five_ne_zero r h5)
  generalize pw (ring (2 * r + 1 + 1)) ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one ((ring (2 * r + 1 + 1)).add (ring (2 * r + 1 + 1)).one (ring (2 * r + 1 + 1)).one)))) (r + 1) = t at e1 ⊢
  calc t = (ring (2 * r + 1 + 1)).neg ((ring (2 * r + 1 + 1)).mul t ((ring (2 * r + 1 + 1)).neg (ring (2 * r + 1 + 1)).one)) := by lib_ring
    _ = (ring (2 * r + 1 + 1)).one := by rw [e1]; lib_ring

end Gauss5Conv

#print axioms Gauss5Conv.sq5_conv

#print axioms: 1 theorem, no axioms.

78.97 The Legendre curves have \(\#E\equiv 0\pmod 4\)

An involution \(x\mapsto \lambda /x\) and an ordering of the representatives turn the pairing argument into sum identities.

\[ p+1+\sum _x\chi \big (x(x-1)(x-\lambda )\big )\equiv 0\pmod 4\qquad (\lambda \ne 0,1). \]

lang/LegendreFour.lean

/-
  lang/LegendreFour — T43, cycle 354: #E_λ(𝔽_p) ≡ 0 (mod 4) for the Legendre curves, by an involution and an ordering.

  p = 2m + 3, λ ≠ 0, 1, g(x) = x(x − 1)(x − λ), a(λ) = Σ_x χ(g x), F(x) = χ(g x)² + χ(g x) ∈ {0, 2}.
    sig_inv       σ(x) = λ/x is an involution of 𝔽_p (σ(0) = 0)
    F_sig         F(σx) = F(x)                                       (g(λ/x) = λ² g(x)/x⁴)
    split3        Σ_x H(x) = Σ_x H(x)[x < σx] + Σ_x H(x)[σx < x] + Σ_x H(x)[σx = x]   (order on the representatives)
    swap_half     Σ_x F(x)[σx < x] = Σ_x F(x)[x < σx]                (reindex by σ)
    fixed_pts     σx = x ⟺ x = 0 or x² = λ
    legendre_four p + 1 + a(λ) = 4k                                  (#E_λ ≡ 0 mod 4: the rational 2-torsion has order 4)
  The pairing argument (orbits of a fixed-point-free involution) is written as sum identities, without finite sets.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.LegendreMoment

namespace LegendreFour

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal QuadTwist JacobMoment PencilMoment PencilClosed LegendreMoment
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def sig (l x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).mul l (iv m x)

theorem sig_inv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x : Zp (2 * m + 1 + 1)) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) : sig m l (sig m l x) = x := by
  unfold sig
  rw [inv_mul, inv_inv m hpr]
  calc (ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).mul (iv m l) x) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m l) l) x := by lib_ring
    _ = x := by rw [iv_mul_self m hpr l hl]; lib_ring

def ltI (a b : Zp (2 * m + 1 + 1)) : Int := if a.val < b.val then 1 else 0

theorem trich (a b : Zp (2 * m + 1 + 1)) : ltI m a b + ltI m b a + delta LibInt.intR b a = 1 := by
  unfold ltI
  cases Nat.lt_or_ge a.val b.val with
  | inl h =>
    rw [ite_pos' _ _ h, ite_neg' _ _ (fun h' => Nat.lt_asymm h h'), delta_zero m _ _ (fun e => Nat.lt_irrefl a.val (by rw [e] at h; exact h))]
    rfl
  | inr h =>
    cases Nat.lt_or_ge b.val a.val with
    | inl h' =>
      rw [ite_neg' _ _ (fun h'' => Nat.lt_asymm h' h''), ite_pos' _ _ h', delta_zero m _ _ (fun e => Nat.lt_irrefl a.val (by rw [e] at h'; exact h'))]
      rfl
    | inr h' =>
      have e : b = a := Subtype.ext (Nat.le_antisymm h h')
      rw [ite_neg' _ _ (fun h'' => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h'' h')), ite_neg' _ _ (fun h'' => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h'' h)),
        delta_one m _ _ e]
      rfl

theorem split3 (sg : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) (H : Zp (2 * m + 1 + 1) → Int) :
    sumK LibInt.intR H (L m) = sumK LibInt.intR (fun x => H x * ltI m x (sg x)) (L m) + sumK LibInt.intR (fun x => H x * ltI m (sg x) x) (L m)
      + sumK LibInt.intR (fun x => H x * delta LibInt.intR (sg x) x) (L m) := by
  rw [← QuadDiagonal.sI_add, ← QuadDiagonal.sI_add]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    have t := trich m x (sg x)
    calc H x = H x * (ltI m x (sg x) + ltI m (sg x) x + delta LibInt.intR (sg x) x) := by rw [t]; generalize H x = h; lib_int
      _ = _ := by generalize H x = h; generalize ltI m x (sg x) = u; generalize ltI m (sg x) x = v; generalize delta LibInt.intR (sg x) x = d; lib_int) _

theorem swap_half (sg : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) (hs : ∀ x, sg (sg x) = x) (H : Zp (2 * m + 1 + 1) → Int) (hH : ∀ x, H (sg x) = H x) :
    sumK LibInt.intR (fun x => H x * ltI m (sg x) x) (L m) = sumK LibInt.intR (fun x => H x * ltI m x (sg x)) (L m) := by
  refine (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) sg sg hs hs (fun x => H x * ltI m (sg x) x)).symm.trans ?_
  exact sumK_ext LibInt.intR _ _ (fun x => by show H (sg x) * ltI m (sg (sg x)) (sg x) = H x * ltI m x (sg x); rw [hs, hH]) _

theorem g_sig (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    (ring (2 * m + 1 + 1)).mul (f m (sig m l x)) ((ring (2 * m + 1 + 1)).add (sig m l x) ((ring (2 * m + 1 + 1)).neg l)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l l) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)) ((ring (2 * m +  -- (+39 chars; full line in the Zenodo source)
  have e : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold sig f
  symm
  calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l l) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m x) x) ((ring (2 * m + 1 + 1)).neg (iv m x))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m x) x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul l (iv  -- (+66 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l l) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (iv m x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul l (iv m x))))))) (ring (2 * m + 1   -- (+22 chars; full line in the Zenodo source)
    _ = _ := by lib_ring

/-- F(x) = χ(g x)² + χ(g x). -/
def F (l x : Zp (2 * m + 1 + 1)) : Int := chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) + chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))

theorem F_sig (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x : Zp (2 * m + 1 + 1)) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) : F m l (sig m l x) = F m l x := by
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, show sig m l (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero by unfold sig; rw [show iv m (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero from iv_zero m]; lib_ring]
  · have hix := iv_nz m hpr x hx
    have hc : chi m ((ring (2 * m + 1 + 1)).mul (f m (sig m l x)) ((ring (2 * m + 1 + 1)).add (sig m l x) ((ring (2 * m + 1 + 1)).neg l))) = chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) := by
      rw [g_sig m hpr l x hx, chi_mul m hpr ((ring (2 * m + 1 + 1)).mul l l), chi_mul m hpr ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))), chi_mul m hpr ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)),
        chi_sq m hpr _ hl, chi_sq m hpr _ hix]
      generalize chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) = t; lib_int
    unfold F; rw [hc]

theorem fixed_delta (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l x : Zp (2 * m + 1 + 1)) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) :
    delta LibInt.intR (sig m l x) x = delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) l := by
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, show sig m l (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero by unfold sig; rw [show iv m (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).zero from iv_zero m]; lib_ring,
      delta_one m _ _ rfl, delta_zero m _ _ (fun e => hl (by rw [← e]; lib_ring))]; rfl
  · have e : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
    rw [delta_zero m _ _ hx]
    by_cases h : (ring (2 * m + 1 + 1)).mul x x = l
    · rw [delta_one m _ _ h, delta_one m _ _ (by
        unfold sig; rw [← h]
        calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) (iv m x) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
          _ = x := by rw [e]; lib_ring)]; rfl
    · rw [delta_zero m _ _ h, delta_zero m _ _ (fun e' => h (by
        unfold sig at e'
        calc (ring (2 * m + 1 + 1)).mul x x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l (iv m x)) x := by rw [e']
          _ = (ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
          _ = l := by rw [e]; lib_ring))]; rfl

theorem F_zero (l : Zp (2 * m + 1 + 1)) : F m l (ring (2 * m + 1 + 1)).zero = 0 := by
  unfold F; rw [show (ring (2 * m + 1 + 1)).mul (f m (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg l)) = (ring (2 * m + 1 + 1)).zero by unfold f; lib_ring, chi_zero]; rfl

theorem F_even (x l : Zp (2 * m + 1 + 1)) : ∃ k : Int, F m l x = 2 * k := by
  unfold F
  rcases chi_cases m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) with e | e | e
  · exact ⟨0, by rw [e]; rfl⟩
  · exact ⟨1, by rw [e]; rfl⟩
  · exact ⟨0, by rw [e]; rfl⟩

/-- the fixed points of σ contribute 0 or 4. -/
theorem fixed_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l : Zp (2 * m + 1 + 1)) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) (hl1 : l ≠ (ring (2 * m + 1 + 1)).one) :
    ∃ k : Int, sumK LibInt.intR (fun x => F m l x * delta LibInt.intR (sig m l x) x) (L m) = 4 * k := by
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (F m l x)) (LibInt.intR.mul (delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) l) (F m l x))) (fun x => by
    rw [fixed_delta m hpr l x hl]; show _ = delta LibInt.intR x (ring (2 * m + 1 + 1)).zero * F m l x + delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) l * F m l x
    generalize F m l x = a; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) l = v; lib_int), sumK_add,
    FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (F m l) (ring (2 * m + 1 + 1)).zero, F_zero]
  by_cases hp : chi m l = 1
  case neg =>
      have hm := chi_pm m l hl hp
      refine ⟨0, ?_⟩
      rw [sumK_ext LibInt.intR _ (fun _ => (0 : Int)) (fun x => by
        rw [delta_zero m _ _ (fun e => by
          have hx0 : x ≠ (ring (2 * m + 1 + 1)).zero := fun h0 => hl (by rw [← e, h0]; lib_ring)
          have := chi_sq m hpr x hx0
          rw [e, hm] at this
          exact int_m1_ne this)]; show 0 * F m l x = 0; generalize F m l x = a; lib_int), QuadDiagonal.zsum]
      rfl
  case pos =>
      match QuadDiagonal.sq_root m hpr l hl (chi_one_pw m l hl hp) with
      | ⟨r, hr⟩ =>
        have hr0 : r ≠ (ring (2 * m + 1 + 1)).zero := fun h0 => hl (by rw [← hr, h0]; lib_ring)
        rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul (delta LibInt.intR x r) (F m l x)) (LibInt.intR.mul (delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg r)) (F m l x))) (fun x => by
          rw [show delta LibInt.intR ((ring (2 * m + 1 + 1)).mul x x) l = delta LibInt.intR x r + delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg r) from by
            rw [← hr, FiniteRecChain.delta_symm, roots_sq m hpr r x hr0]]
          show (delta LibInt.intR x r + delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg r)) * F m l x = delta LibInt.intR x r * F m l x + delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg r) * F m l x
          generalize F m l x = a; generalize delta LibInt.intR x r = u; generalize delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg r) = v; lib_int), sumK_add,
          FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (F m l) r, FiniteRecChain.pick LibInt.intR (L m) (QuadDiagonal.hcomp m) (F m l) ((ring (2 * m + 1 + 1)).neg r)]
        -- F(±r) = χ(−1)² + χ(−1): g(±r) = −r²(r ∓ 1)²
        have hgr : ∀ t : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul t t = l → t ≠ (ring (2 * m + 1 + 1)).zero → (ring (2 * m + 1 + 1)).add t ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero →
            F m l t = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) + chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := fun t ht ht0 ht1 => by
          have hg : (ring (2 * m + 1 + 1)).mul (f m t) ((ring (2 * m + 1 + 1)).add t ((ring (2 * m + 1 + 1)).neg l)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)).add t ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul t ((ring (2 * m + 1 + 1)  -- (+72 chars; full line in the Zenodo source)
            unfold f; rw [← ht]; lib_ring
          have hc : chi m ((ring (2 * m + 1 + 1)).mul (f m t) ((ring (2 * m + 1 + 1)).add t ((ring (2 * m + 1 + 1)).neg l))) = chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
            rw [hg, chi_mul m hpr, chi_sq m hpr _ (nzm m hpr _ _ ht0 ht1)]; generalize chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = e; lib_int
          unfold F; rw [hc]
        have hne1 : ∀ t : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul t t = l → (ring (2 * m + 1 + 1)).add t ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := fun t ht h1 => hl1 (by
          rw [← ht, show t = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add t ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one by lib_ring, h1]; lib_ring)
        rw [hgr r hr hr0 (hne1 r hr), hgr ((ring (2 * m + 1 + 1)).neg r) (by rw [← hr]; lib_ring) (negnz m _ hr0) (hne1 _ (by rw [← hr]; lib_ring))]
        cases QuadDiagonal.e_val m hpr with
        | inl e1 => exact ⟨1, by rw [e1]; rfl⟩
        | inr e1 => exact ⟨0, by rw [e1]; rfl⟩

/-- **#E_λ(𝔽_p) ≡ 0 (mod 4)**: p + 1 + a(λ) = 4k for λ ≠ 0, 1 (a(λ) = Σ_x χ(x(x − 1)(x − λ))). -/
theorem legendre_four (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (l : Zp (2 * m + 1 + 1)) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) (hl1 : l ≠ (ring (2 * m + 1 + 1)).one) :
    ∃ k : Int, P m + 1 + A m l = 4 * k := by
  have hsq : sumK LibInt.intR (fun x => chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l))) * chi m ((ring (2 * m + 1 + 1)).mul (f m x) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg l)))) (L m) = P m + -1 + -1 + -1 := by
    rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 1 1) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero)) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) (LibInt.intR.mul (-1) (delta LibInt.intR x l))))) (fun x => by
      rw [QuadDiagonal.sqchi m hpr, cubic_delta m hpr l x hl hl1]
      show 1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + delta LibInt.intR x (ring (2 * m + 1 + 1)).one + delta LibInt.intR x l) = 1 * 1 + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).one + -1 * delta LibInt.intR x l))
      generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).one = v; generalize delta LibInt.intR x l = w; lib_int)]
    rw [sumK_add, sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, ← mul_sumK, ← mul_sumK, sum_one,
      show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero,
      show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).one) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).one,
      show sumK LibInt.intR (fun x => delta LibInt.intR x l) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR l]
    show 1 * P m + (-1 * 1 + (-1 * 1 + -1 * 1)) = _
    lib_int
  have hF : sumK LibInt.intR (F m l) (L m) = (P m + -1 + -1 + -1) + A m l := by
    rw [← hsq]; unfold A; rw [← QuadDiagonal.sI_add]; rfl
  have hsplit := split3 m (sig m l) (F m l)
  rw [swap_half m (sig m l) (fun x => sig_inv m hpr l x hl) (F m l) (fun x => F_sig m hpr l x hl)] at hsplit
  match even_sum m (L m) (fun x => F m l x * ltI m x (sig m l x)) (fun x => by
    match F_even m x l with
    | ⟨k, hk⟩ => exact ⟨k * ltI m x (sig m l x), by rw [hk]; generalize ltI m x (sig m l x) = t; lib_int⟩), fixed_sum m hpr l hl hl1 with
  | ⟨k1, hk1⟩, ⟨k2, hk2⟩ =>
    refine ⟨k1 + k2 + 1, ?_⟩
    have : (P m + -1 + -1 + -1) + A m l = 2 * k1 + 2 * k1 + 4 * k2 := by rw [← hF, hsplit, hk1, hk2]
    calc P m + 1 + A m l = ((P m + -1 + -1 + -1) + A m l) + 4 := by generalize A m l = a; lib_int
      _ = _ := by rw [this]; lib_int

end LegendreFour

#print axioms LegendreFour.sig_inv
#print axioms LegendreFour.trich
#print axioms LegendreFour.split3
#print axioms LegendreFour.swap_half
#print axioms LegendreFour.g_sig
#print axioms LegendreFour.F_sig
#print axioms LegendreFour.fixed_delta
#print axioms LegendreFour.F_zero
#print axioms LegendreFour.F_even
#print axioms LegendreFour.fixed_sum
#print axioms LegendreFour.legendre_four

#print axioms: 11 theorems, no axioms.

78.98 Orbits of order three

Orbit counting for maps of order 3 written as sum identities, applied to \(x\mapsto 1/(1-x)\).

\[ p-3-\chi (-3)\equiv 0\pmod 3. \]

lang/Orbit3.lean

/-
  lang/Orbit3 — T43, cycle 355: counting orbits of an order-3 map by sums, and the law for −3 by counting.

    min3            for pairwise distinct a, b, c ∈ ℕ exactly one is the smallest
    orbit3          τ³ = id, H∘τ = H, H vanishes on fixed points ⟹ Σ_x H(x) = 3·Σ_x H(x)[x is the least of its orbit]
    mobius_count    τ(x) = 1/(1 − x) on 𝔽_p ∖ {0, 1}: p − 2 − #{x² − x + 1 = 0} ≡ 0 (mod 3), i.e. 3 | p − 3 − χ(−3)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.LegendreFour

namespace Orbit3

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Jacobsthal QuadTwist JacobMoment PencilMoment PencilClosed LegendreMoment LegendreFour
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

/-- [a is the least of a, b, c] -/
def least (a b c : Nat) : Int := if a < b then (if a < c then 1 else 0) else 0

theorem min3 (a b c : Nat) (hab : a ≠ b) (hbc : b ≠ c) (hca : c ≠ a) : least a b c + least b c a + least c a b = 1 := by
  unfold least
  cases Nat.lt_or_ge a b with
  | inl h1 =>
    cases Nat.lt_or_ge b c with
    | inl h2 =>
      rw [ite_pos' _ _ h1, ite_pos' _ _ (Nat.lt_trans h1 h2), ite_pos' _ _ h2, ite_neg' _ _ (fun h => Nat.lt_asymm h h1),
        ite_neg' _ _ (fun h => Nat.lt_asymm h (Nat.lt_trans h1 h2))]; rfl
    | inr h2 =>
      have h2' : c < b := Nat.lt_of_le_of_ne h2 (fun e => hbc e.symm)
      cases Nat.lt_or_ge a c with
      | inl h3 =>
        rw [ite_pos' _ _ h1, ite_pos' _ _ h3, ite_neg' _ _ (fun h => Nat.lt_asymm h h2'), ite_neg' _ _ (fun h => Nat.lt_asymm h h3)]; rfl
      | inr h3 =>
        have h3' : c < a := Nat.lt_of_le_of_ne h3 hca
        rw [ite_pos' _ _ h1, ite_neg' _ _ (fun h => Nat.lt_asymm h h3'), ite_neg' _ _ (fun h => Nat.lt_asymm h h2'), ite_pos' _ _ h3', ite_pos' _ _ h2']; rfl
  | inr h1 =>
    have h1' : b < a := Nat.lt_of_le_of_ne h1 (fun e => hab e.symm)
    cases Nat.lt_or_ge b c with
    | inl h2 =>
      rw [ite_neg' _ _ (fun h => Nat.lt_asymm h h1'), ite_pos' _ _ h2, ite_pos' _ _ h1', ite_neg' _ _ (fun h => Nat.lt_asymm h h2)]
      by_cases hc : c < a
      · rw [ite_pos' _ _ hc]; rfl
      · rw [ite_neg' _ _ hc]; rfl
    | inr h2 =>
      have h2' : c < b := Nat.lt_of_le_of_ne h2 (fun e => hbc e.symm)
      rw [ite_neg' _ _ (fun h => Nat.lt_asymm h h1'), ite_neg' _ _ (fun h => Nat.lt_asymm h h2'), ite_pos' _ _ (Nat.lt_trans h2' h1'), ite_pos' _ _ h2']; rfl

def Lst (sg : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) (x : Zp (2 * m + 1 + 1)) : Int := least x.val (sg x).val (sg (sg x)).val

/-- **Orbits of an order-3 map**: Σ_x H(x) = 3·Σ_x H(x)·[x least in its orbit], when τ³ = id, H∘τ = H and H = 0 at fixed points. -/
theorem orbit3 (sg : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) (h3 : ∀ x, sg (sg (sg x)) = x) (H : Zp (2 * m + 1 + 1) → Int)
    (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg x = x → H x = 0) :
    sumK LibInt.intR H (L m) = 3 * sumK LibInt.intR (fun x => H x * Lst m sg x) (L m) := by
  have hpt : ∀ x, H x = H x * Lst m sg x + H x * Lst m sg (sg x) + H x * Lst m sg (sg (sg x)) := fun x => by
    by_cases hx : sg x = x
    · rw [hfix x hx]; generalize Lst m sg x = u; generalize Lst m sg (sg x) = v; generalize Lst m sg (sg (sg x)) = w; lib_int
    · have h2 : sg (sg x) ≠ x := fun e => hx (by
        calc sg x = sg (sg (sg x)) := by rw [e]
          _ = x := h3 x)
      have h12 : sg (sg x) ≠ sg x := fun e => h2 (by
        have : x = sg (sg x) := by
          calc x = sg (sg (sg x)) := (h3 x).symm
            _ = sg (sg x) := by rw [e]; exact e
        exact this.symm)
      have val_ne : ∀ a b : Zp (2 * m + 1 + 1), a ≠ b → a.val ≠ b.val := fun a b h e => h (Subtype.ext e)
      have hm := min3 x.val (sg x).val (sg (sg x)).val (val_ne _ _ (fun e => hx e.symm)) (val_ne _ _ (fun e => h12 e.symm)) (val_ne _ _ h2)
      have e1 : Lst m sg (sg x) = least (sg x).val (sg (sg x)).val x.val := by unfold Lst; rw [h3]
      have e2 : Lst m sg (sg (sg x)) = least (sg (sg x)).val x.val (sg x).val := by unfold Lst; rw [h3]
      rw [e1, e2]
      unfold Lst
      calc H x = H x * (least x.val (sg x).val (sg (sg x)).val + least (sg x).val (sg (sg x)).val x.val + least (sg (sg x)).val x.val (sg x).val) := by
            rw [hm]; generalize H x = h; lib_int
        _ = _ := by
            generalize H x = h; generalize least x.val (sg x).val (sg (sg x)).val = u
            generalize least (sg x).val (sg (sg x)).val x.val = v; generalize least (sg (sg x)).val x.val (sg x).val = w; lib_int
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add]
  have r1 : sumK LibInt.intR (fun x => H x * Lst m sg (sg x)) (L m) = sumK LibInt.intR (fun x => H x * Lst m sg x) (L m) := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) sg (fun y => sg (sg y)) h3 (fun y => h3 y) (fun y => H y * Lst m sg y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lst m sg (sg x) = H (sg x) * Lst m sg (sg x); rw [hH]) _
  have r2 : sumK LibInt.intR (fun x => H x * Lst m sg (sg (sg x))) (L m) = sumK LibInt.intR (fun x => H x * Lst m sg x) (L m) := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR (L m) (QuadDiagonal.hcomp m) (fun y => sg (sg y)) sg (fun y => h3 y) h3 (fun y => H y * Lst m sg y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Lst m sg (sg (sg x)) = H (sg (sg x)) * Lst m sg (sg (sg x)); rw [hH, hH]) _
  rw [r1, r2]
  generalize sumK LibInt.intR (fun x => H x * Lst m sg x) (L m) = S
  lib_int

/-- τ(x) = 1/(1 − x) on 𝔽_p ∖ {0, 1}, identity on {0, 1}. -/
def tau (x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) :=
  if x = (ring (2 * m + 1 + 1)).zero then x else if x = (ring (2 * m + 1 + 1)).one then x else iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))

theorem one_ne : ((ring (2 * m + 1 + 1)).one : Zp (2 * m + 1 + 1)) ≠ (ring (2 * m + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * m + 1)

theorem sub_ne (x : Zp (2 * m + 1 + 1)) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x) ≠ (ring (2 * m + 1 + 1)).zero := fun e => h1 (by
  calc x = (ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) := by rw [e]; lib_ring
    _ = (ring (2 * m + 1 + 1)).one := by lib_ring)

theorem tau_gen (x : Zp (2 * m + 1 + 1)) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : tau m x = iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) := by
  unfold tau; rw [ite_neg' _ _ h0, ite_neg' _ _ h1]

theorem tau_ne0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : tau m x ≠ (ring (2 * m + 1 + 1)).zero := by
  rw [tau_gen m x h0 h1]; exact iv_nz m hpr _ (sub_ne m x h1)

theorem tau_ne1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : tau m x ≠ (ring (2 * m + 1 + 1)).one := by
  rw [tau_gen m x h0 h1]; intro e
  have := iv_mul_self m hpr _ (sub_ne m x h1)
  rw [e] at this
  exact h0 (by calc x = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)))) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).zero := by rw [this]; lib_ring)

theorem tau3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) : tau m (tau m (tau m x)) = x := by
  by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
  · have : tau m x = x := by unfold tau; rw [ite_pos' _ _ h0]
    rw [this, this, this]
  by_cases h1 : x = (ring (2 * m + 1 + 1)).one
  · have : tau m x = x := by unfold tau; rw [ite_neg' _ _ h0, ite_pos' _ _ h1]
    rw [this, this, this]
  have y0 := tau_ne0 m hpr x h0 h1
  have y1 := tau_ne1 m hpr x h0 h1
  have z0 := tau_ne0 m hpr _ y0 y1
  have z1 := tau_ne1 m hpr _ y0 y1
  rw [tau_gen m _ z0 z1, tau_gen m _ y0 y1, tau_gen m x h0 h1]
  have ex : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x h0
  have eu : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ (sub_ne m x h1)
  -- 1 − 1/(1 − x) = −x/(1 − x)
  have s1 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg x) (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).neg (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)))) := by rw [eu]
      _ = _ := by lib_ring
  rw [s1, inv_mul, inv_inv m hpr]
  -- 1 − (1/(−x))(1 − x) = 1/x
  have s2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)))) = iv m x := by
    rw [show (ring (2 * m + 1 + 1)).neg x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x by lib_ring, inv_mul,
      show iv m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one by
        have e1 := iv_mul_self m hpr _ (negnz m _ (one_ne m))
        calc iv m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by lib_ring
          _ = _ := by rw [e1]]
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (iv m x) x) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) := by
          rw [ex]; lib_ring
      _ = iv m x := by lib_ring
  rw [s2, inv_inv m hpr]

theorem fix_iff (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) : tau m (tau m x) = tau m x → tau m x = x := fun e => by
  calc tau m x = tau m (tau m (tau m x)) := by rw [e, e]
    _ = x := tau3 m hpr x

theorem tau_fixed (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) :
    (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * delta LibInt.intR (tau m x) x = delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x) (ri  -- (+59 chars; full line in the Zenodo source)
  by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
  · rw [h0, delta_one m _ _ rfl, delta_zero m _ _ (one_ne m).symm, delta_zero m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).zero) (ring (2 *  -- (+69 chars; full line in the Zenodo source)
        calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).zero := e))]
    generalize delta LibInt.intR (tau m (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero = d; lib_int
  by_cases h1 : x = (ring (2 * m + 1 + 1)).one
  · rw [h1, delta_zero m _ _ (one_ne m), delta_one m _ _ rfl, delta_zero m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 +  -- (+61 chars; full line in the Zenodo source)
        calc (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).zero := e))]
    generalize delta LibInt.intR (tau m (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one = d; lib_int
  rw [delta_zero m _ _ h0, delta_zero m _ _ h1, tau_gen m x h0 h1]
  have eu : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ (sub_ne m x h1)
  by_cases h : iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) = x
  · rw [delta_one m _ _ h, delta_one m _ _ (by
      calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m +   -- (+87 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)))) := by rw [h]
        _ = (ring (2 * m + 1 + 1)).zero := by rw [eu]; lib_ring)]; rfl
  · rw [delta_zero m _ _ h, delta_zero m _ _ (fun e => h (by
      have e2 : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) = (ring (2 * m + 1 + 1)).one := by
        calc _ = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x) (ring (2 * m + 1 + 1)).one))) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := by rw [e]; lib_ring
      calc iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x)) = (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) := by rw [e2]; lib_ring
        _ = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg x))) := by lib_ring
        _ = x := by rw [eu]; lib_ring))]; rfl

/-- **The law for −3 by counting orbits**: p − 3 − χ(−3) ≡ 0 (mod 3) (τ(x) = 1/(1 − x) has order 3 on 𝔽_p ∖ {0, 1}). -/
theorem mobius_count (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∃ k : Int, P m + -3 + -(chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) = 3 * k := by
  let H : Zp (2 * m + 1 + 1) → Int := fun x => (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR (tau m x) x))
  have hH : ∀ x, H (tau m x) = H x := fun x => by
    show (1 + -(delta LibInt.intR (tau m x) (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR (tau m x) (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR (tau m (tau m x)) (tau m x)))
      = (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR (tau m x) x))
    by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
    · have : tau m x = x := by unfold tau; rw [ite_pos' _ _ h0]
      rw [this, this]
    by_cases h1 : x = (ring (2 * m + 1 + 1)).one
    · have : tau m x = x := by unfold tau; rw [ite_neg' _ _ h0, ite_pos' _ _ h1]
      rw [this, this]
    rw [delta_zero m _ _ h0, delta_zero m _ _ h1, delta_zero m _ _ (tau_ne0 m hpr x h0 h1), delta_zero m _ _ (tau_ne1 m hpr x h0 h1)]
    by_cases hf : tau m x = x
    · rw [hf, hf]
    · rw [delta_zero m _ _ hf, delta_zero m _ _ (fun e => hf (fix_iff m hpr x e))]
  have hfix : ∀ x, tau m x = x → H x = 0 := fun x e => by
    show (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR (tau m x) x)) = 0
    rw [delta_one m _ _ e]; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).one = v; lib_int
  have ho := orbit3 m (tau m) (tau3 m hpr) H hH hfix
  have hval : sumK LibInt.intR H (L m) = P m + -3 + -(chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) := by
    rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.mul 1 1) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).zero)) (LibInt.intR.add (LibInt.intR.mul (-1) (delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) (LibInt.intR.mul (-1) (delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1   -- (+205 chars; full line in the Zenodo source)
      show (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR (tau m x) x)) = _
      rw [show (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * (1 + -(delta LibInt.intR (tau m x) x))
          = (1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) + -((1 + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) + -(delta LibInt.intR x (ring (2 * m + 1 + 1)).one)) * delta LibInt.intR (tau m x) x) by
            generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).one = v; generalize delta LibInt.intR (tau m x) x = w; lib_int,
        tau_fixed m hpr x]
      show _ = 1 * 1 + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero + (-1 * delta LibInt.intR x (ring (2 * m + 1 + 1)).one + -1 * delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x) (ring (2 *   -- (+47 chars; full line in the Zenodo source)
      generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).zero = u; generalize delta LibInt.intR x (ring (2 * m + 1 + 1)).one = v; generalize delta LibInt.intR ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x) (ring   -- (+65 chars; full line in the Zenodo source)
    rw [sumK_add, sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, ← mul_sumK, ← mul_sumK, sum_one,
      show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).zero,
      show sumK LibInt.intR (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).one) (L m) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR (ring (2 * m + 1 + 1)).one,
      root_count m hpr (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one (one_ne m)]
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).o  -- (+297 chars; full line in the Zenodo source)
    show 1 * P m + (-1 * 1 + (-1 * 1 + -1 * (1 + chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))))) = _
    generalize chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) = e; lib_int
  exact ⟨sumK LibInt.intR (fun x => H x * Lst m (tau m) x) (L m), by rw [← hval, ho]⟩

end Orbit3

#print axioms Orbit3.min3
#print axioms Orbit3.orbit3
#print axioms Orbit3.tau3
#print axioms Orbit3.fix_iff
#print axioms Orbit3.tau_fixed
#print axioms Orbit3.mobius_count

#print axioms: 6 theorems, no axioms.

78.99 Orbits of order five

For a map of order 5 on a finite set, invariant weights vanishing at fixed points sum to a multiple of 5.

\[ \tau ^5=\mathrm {id},\ H\circ \tau =H,\ H|_{\mathrm {Fix}\,\tau }=0\ \Longrightarrow \ \sum _xH(x)=5\sum _xH(x)\,[x=\min \text { of its orbit}]. \]

lang/Orbit5.lean

/-
  lang/Orbit5 — T43, cycle 356: counting orbits of an order-5 map by sums (the orbit tool needed for G4).

    min_or       min a b = a ∨ min a b = b
    eqI          [a = b] as an integer (Nat equality, by cases)
    one_min5     for pairwise distinct a, b, c, d, e: exactly one of them equals min a (min b (min c (min d e)))
    orbit5       τ⁵ = id, H∘τ = H, H = 0 at fixed points ⟹ Σ_x H(x) = 5·Σ_x H(x)·[x is the least of its orbit]
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Orbit3

namespace Orbit5

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def mn (a b : Nat) : Nat := if a ≤ b then a else b

theorem mn_or (a b : Nat) : mn a b = a ∨ mn a b = b := by
  unfold mn
  by_cases h : a ≤ b
  · rw [ite_pos' _ _ h]; exact Or.inl rfl
  · rw [ite_neg' _ _ h]; exact Or.inr rfl

theorem mn_le_l (a b : Nat) : mn a b ≤ a := by
  unfold mn
  by_cases h : a ≤ b
  · rw [ite_pos' _ _ h]; exact Nat.le_refl a
  · rw [ite_neg' _ _ h]; exact Nat.le_of_lt (Nat.lt_of_not_le h)

theorem mn_le_r (a b : Nat) : mn a b ≤ b := by
  unfold mn
  by_cases h : a ≤ b
  · rw [ite_pos' _ _ h]; exact h
  · rw [ite_neg' _ _ h]; exact Nat.le_refl b

def eqI (a b : Nat) : Int := if a = b then 1 else 0

def m5 (a b c d e : Nat) : Nat := mn a (mn b (mn c (mn d e)))

/-- **Exactly one of five distinct numbers is the minimum.** -/
theorem one_min5 (a b c d e : Nat) (hab : a ≠ b) (hac : a ≠ c) (had : a ≠ d) (hae : a ≠ e) (hbc : b ≠ c) (hbd : b ≠ d) (hbe : b ≠ e)
    (hcd : c ≠ d) (hce : c ≠ e) (hde : d ≠ e) :
    eqI a (m5 a b c d e) + eqI b (m5 a b c d e) + eqI c (m5 a b c d e) + eqI d (m5 a b c d e) + eqI e (m5 a b c d e) = 1 := by
  unfold eqI
  have hmem : m5 a b c d e = a ∨ m5 a b c d e = b ∨ m5 a b c d e = c ∨ m5 a b c d e = d ∨ m5 a b c d e = e := by
    unfold m5
    rcases mn_or a (mn b (mn c (mn d e))) with h | h
    · exact Or.inl h
    rw [h]
    rcases mn_or b (mn c (mn d e)) with h | h
    · exact Or.inr (Or.inl h)
    rw [h]
    rcases mn_or c (mn d e) with h | h
    · exact Or.inr (Or.inr (Or.inl h))
    rw [h]
    rcases mn_or d e with h | h
    · exact Or.inr (Or.inr (Or.inr (Or.inl h)))
    · exact Or.inr (Or.inr (Or.inr (Or.inr h)))
  rcases hmem with h | h | h | h | h <;> rw [h]
  · rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => hab e'.symm), ite_neg' _ _ (fun e' => hac e'.symm), ite_neg' _ _ (fun e' => had e'.symm), ite_neg' _ _ (fun e' => hae e'.symm)]; rfl
  · rw [ite_neg' _ _ hab, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => hbc e'.symm), ite_neg' _ _ (fun e' => hbd e'.symm), ite_neg' _ _ (fun e' => hbe e'.symm)]; rfl
  · rw [ite_neg' _ _ hac, ite_neg' _ _ hbc, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => hcd e'.symm), ite_neg' _ _ (fun e' => hce e'.symm)]; rfl
  · rw [ite_neg' _ _ had, ite_neg' _ _ hbd, ite_neg' _ _ hcd, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => hde e'.symm)]; rfl
  · rw [ite_neg' _ _ hae, ite_neg' _ _ hbe, ite_neg' _ _ hce, ite_neg' _ _ hde, ite_pos' _ _ rfl]; rfl

theorem m5_le (a b c d e : Nat) : m5 a b c d e ≤ a ∧ m5 a b c d e ≤ b ∧ m5 a b c d e ≤ c ∧ m5 a b c d e ≤ d ∧ m5 a b c d e ≤ e := by
  unfold m5
  refine ⟨mn_le_l _ _, Nat.le_trans (mn_le_r _ _) (mn_le_l _ _), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)),
    Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _))),
    Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_r _ _)))⟩

theorem m5_mem (a b c d e : Nat) : m5 a b c d e = a ∨ m5 a b c d e = b ∨ m5 a b c d e = c ∨ m5 a b c d e = d ∨ m5 a b c d e = e := by
  unfold m5
  rcases mn_or a (mn b (mn c (mn d e))) with h | h
  · exact Or.inl h
  rw [h]
  rcases mn_or b (mn c (mn d e)) with h | h
  · exact Or.inr (Or.inl h)
  rw [h]
  rcases mn_or c (mn d e) with h | h
  · exact Or.inr (Or.inr (Or.inl h))
  rw [h]
  rcases mn_or d e with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inl h)))
  · exact Or.inr (Or.inr (Or.inr (Or.inr h)))

/-- the minimum of five numbers does not depend on the cyclic order. -/
theorem m5_rot (a b c d e : Nat) : m5 b c d e a = m5 a b c d e := by
  have l1 := m5_le a b c d e
  have l2 := m5_le b c d e a
  apply Nat.le_antisymm
  · rcases m5_mem a b c d e with h | h | h | h | h <;> rw [h]
    · exact l2.2.2.2.2
    · exact l2.1
    · exact l2.2.1
    · exact l2.2.2.1
    · exact l2.2.2.2.1
  · rcases m5_mem b c d e a with h | h | h | h | h <;> rw [h]
    · exact l1.2.1
    · exact l1.2.2.1
    · exact l1.2.2.2.1
    · exact l1.2.2.2.2
    · exact l1.1

section General
variable {G : Type} [DecidableEq G]

/-- **Orbits of an order-5 map**, on any completely listed type with an injective code: Σ H = 5·Σ H·[x least in its orbit]. -/
theorem orbit5 (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (sg : G → G) (h5 : ∀ x, sg (sg (sg (sg (sg (x))))) = x) (H : G → Int) (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg x = x → H x = 0) :
    sumK LibInt.intR H els = 5 * sumK LibInt.intR (fun x => H x * eqI (code x) (m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))))) els := by
  let Ls : G → Int := fun x => eqI (code x) (m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))))
  have f2 : ∀ x, sg (sg (x)) = x → sg x = x := fun x e => by
    calc sg (x) = sg (sg (sg (sg (sg (sg (x)))))) := by rw [h5]
      _ = x := by rw [e, e, e]
  have f3 : ∀ x, sg (sg (sg (x))) = x → sg x = x := fun x e => by
    calc sg (x) = sg (sg (sg (sg (sg (sg (x)))))) := by rw [h5]
      _ = x := by rw [e, e]
  have f4 : ∀ x, sg (sg (sg (sg (x)))) = x → sg x = x := fun x e => by
    calc sg (x) = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))))) := by rw [h5, h5, h5]
      _ = x := by rw [e, e, e, e]
  have hpt : ∀ x, H x = H x * Ls x + H x * Ls (sg (x)) + H x * Ls (sg (sg (x))) + H x * Ls (sg (sg (sg (x)))) + H x * Ls (sg (sg (sg (sg (x))))) := fun x => by
    by_cases hx : sg x = x
    · rw [hfix x hx]; lib_int
    · have ne : ∀ u v : G, u ≠ v → code u ≠ code v := fun u v h e => h (hinj u v e)
      -- the five orbit elements are pairwise distinct
      have d01 : sg (x) ≠ x := hx
      have d02 : sg (sg (x)) ≠ x := fun e => hx (f2 x e)
      have d03 : sg (sg (sg (x))) ≠ x := fun e => hx (f3 x e)
      have d04 : sg (sg (sg (sg (x)))) ≠ x := fun e => hx (f4 x e)
      have shift : ∀ y z : G, y ≠ z → sg y ≠ sg z := fun y z h e => h (by
        calc y = sg (sg (sg (sg (sg (y))))) := (h5 y).symm
          _ = sg (sg (sg (sg (sg (z))))) := by rw [show sg (y) = sg (z) from e]
          _ = z := h5 z)
      have d12 := shift _ _ d01; have d13 := shift _ _ d02; have d14 := shift _ _ d03
      have d23 := shift _ _ d12; have d24 := shift _ _ d13; have d34 := shift _ _ d23
      have hm := one_min5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x))))))
        (ne _ _ (fun e => d01 e.symm)) (ne _ _ (fun e => d02 e.symm)) (ne _ _ (fun e => d03 e.symm)) (ne _ _ (fun e => d04 e.symm))
        (ne _ _ (fun e => d12 e.symm)) (ne _ _ (fun e => d13 e.symm)) (ne _ _ (fun e => d14 e.symm))
        (ne _ _ (fun e => d23 e.symm)) (ne _ _ (fun e => d24 e.symm)) (ne _ _ (fun e => d34 e.symm))
      have r1 : Ls (sg (x)) = eqI (code (sg (x))) (m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x))))))) := by
        show eqI _ (m5 _ _ _ _ (code (sg (sg (sg (sg (sg (x)))))))) = _; rw [h5, m5_rot]
      have r2 : Ls (sg (sg (x))) = eqI (code (sg (sg (x)))) (m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x))))))) := by
        show eqI _ (m5 _ _ _ (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) = _; rw [h5, m5_rot, m5_rot]
      have r3 : Ls (sg (sg (sg (x)))) = eqI (code (sg (sg (sg (x))))) (m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x))))))) := by
        show eqI _ (m5 _ _ (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))) = _; rw [h5, m5_rot, m5_rot, m5_rot]
      have r4 : Ls (sg (sg (sg (sg (x))))) = eqI (code (sg (sg (sg (sg (x)))))) (m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x))))))) := by
        show eqI _ (m5 _ (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) = _; rw [h5, m5_rot, m5_rot, m5_rot, m5_rot]
      rw [r1, r2, r3, r4]
      show H x = H x * eqI (code x) _ + _ + _ + _ + _
      generalize m5 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) = μ at hm ⊢
      generalize eqI (code x) μ = u0 at hm ⊢; generalize eqI (code (sg (x))) μ = u1 at hm ⊢
      generalize eqI (code (sg (sg (x)))) μ = u2 at hm ⊢; generalize eqI (code (sg (sg (sg (x))))) μ = u3 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (x)))))) μ = u4 at hm ⊢
      calc H x = H x * (u0 + u1 + u2 + u3 + u4) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (sg (x))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (x)) (fun x => sg (sg (sg (sg (x))))) (fun x => h5 x) (fun y => by show sg (sg (sg (sg (sg y)))) = y; exact h5 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (x)) = H (sg (x)) * Ls (sg (x)); rw [hH]) _
  have q2 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (x)))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (x))) (fun x => sg (sg (sg (x)))) (fun x => h5 x) (fun y => by show sg (sg (sg (sg (sg y)))) = y; exact h5 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (x))) = H (sg (sg (x))) * Ls (sg (sg (x))); rw [hH, hH]) _
  have q3 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (x))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (x)))) (fun x => sg (sg (x))) (fun x => h5 x) (fun y => by show sg (sg (sg (sg (sg y)))) = y; exact h5 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (x)))) = H (sg (sg (sg (x)))) * Ls (sg (sg (sg (x)))); rw [hH, hH, hH]) _
  have q4 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (x)))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (x))))) (fun x => sg (x)) (fun x => h5 x) (fun y => by show sg (sg (sg (sg (sg y)))) = y; exact h5 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (x))))) = H (sg (sg (sg (sg (x))))) * Ls (sg (sg (sg (sg (x))))); rw [hH, hH, hH, hH]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, q1, q2, q3, q4]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

end Orbit5

#print axioms Orbit5.mn_or
#print axioms Orbit5.mn_le_l
#print axioms Orbit5.mn_le_r
#print axioms Orbit5.one_min5
#print axioms Orbit5.m5_le
#print axioms Orbit5.m5_mem
#print axioms Orbit5.m5_rot
#print axioms Orbit5.orbit5

#print axioms: 8 theorems, no axioms.

78.100 Orbits of order seven

For a map of order 7 on a finite set, invariant weights vanishing at fixed points sum to a multiple of 7.

\[ \tau ^7=\mathrm {id},\ H\circ \tau =H,\ H|_{\mathrm {Fix}\,\tau }=0\ \Longrightarrow \ \sum _xH(x)=7\sum _xH(x)\,[x=\min \text { of its orbit}]. \]

lang/Orbit7.lean

/-
  lang/Orbit7 — T43, cycle 372: counting orbits of an order-7 map by sums (G5 order 7, counting tool).

    m7, m7_mem, m7_le, m7_rot   the nested minimum of seven numbers and its rotation invariance
    one_min7                    exactly one of seven distinct naturals equals their minimum
    orbit7                      τ⁷ = id, H∘τ = H, H = 0 at fixed points ⟹ Σ H = 7·Σ H·[x least in its orbit]
                                (τ^d x = x with 0 < d < 7 forces τx = x since 7 is prime: τx = τ^(de)x with de ≡ 1 mod 7)
  Generated by a std-lib Python script from the Orbit5 pattern. Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Orbit5

namespace Orbit7

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum Orbit5
open FiniteRecChain (ite_pos' ite_neg')

def m7 (v0 v1 v2 v3 v4 v5 v6 : Nat) : Nat := mn v0 (mn v1 (mn v2 (mn v3 (mn v4 (mn v5 (v6))))))
theorem m7_mem (v0 v1 v2 v3 v4 v5 v6 : Nat) : m7 v0 v1 v2 v3 v4 v5 v6 = v0 ∨ m7 v0 v1 v2 v3 v4 v5 v6 = v1 ∨ m7 v0 v1 v2 v3 v4 v5 v6 = v2 ∨ m7 v0 v1 v2 v3 v4 v5 v6 = v3 ∨ m7 v0 v1 v2 v3 v4 v5 v6 = v4 ∨ m7 v0 v1 v2 v3 v4 v5 v6 = v5 ∨ m7 v0 v1 v2 v3 v4 v5 v6 = v6 := by
  unfold m7
  rcases mn_or v0 (mn v1 (mn v2 (mn v3 (mn v4 (mn v5 (v6)))))) with h | h
  · exact Or.inl h
  rw [h]
  rcases mn_or v1 (mn v2 (mn v3 (mn v4 (mn v5 (v6))))) with h | h
  · exact Or.inr (Or.inl h)
  rw [h]
  rcases mn_or v2 (mn v3 (mn v4 (mn v5 (v6)))) with h | h
  · exact Or.inr (Or.inr (Or.inl h))
  rw [h]
  rcases mn_or v3 (mn v4 (mn v5 (v6))) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inl h)))
  rw [h]
  rcases mn_or v4 (mn v5 (v6)) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))
  rw [h]
  rcases mn_or v5 (v6) with h | h
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))
  · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h)))))

theorem m7_le (v0 v1 v2 v3 v4 v5 v6 : Nat) : m7 v0 v1 v2 v3 v4 v5 v6 ≤ v0 ∧ m7 v0 v1 v2 v3 v4 v5 v6 ≤ v1 ∧ m7 v0 v1 v2 v3 v4 v5 v6 ≤ v2 ∧ m7 v0 v1 v2 v3 v4 v5 v6 ≤ v3 ∧ m7 v0 v1 v2 v3 v4 v5 v6 ≤ v4 ∧ m7 v0 v1 v2 v3 v4 v5 v6 ≤ v5 ∧ m7 v0 v1 v2 v3 v4 v5 v6 ≤ v6 := by
  unfold m7
  exact ⟨mn_le_l _ _, Nat.le_trans (mn_le_r _ _) (mn_le_l _ _), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _))), Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (Nat.le_trans (mn_le_r _ _) (mn_le_l _ _)))), Nat.le_trans (mn_le_r _ _) (Nat.le_t  -- (+279 chars; full line in the Zenodo source)

theorem m7_rot (v0 v1 v2 v3 v4 v5 v6 : Nat) : m7 v1 v2 v3 v4 v5 v6 v0 = m7 v0 v1 v2 v3 v4 v5 v6 := by
  have l1 := m7_le v0 v1 v2 v3 v4 v5 v6
  have l2 := m7_le v1 v2 v3 v4 v5 v6 v0
  apply Nat.le_antisymm
  · rcases m7_mem v0 v1 v2 v3 v4 v5 v6 with h | h | h | h | h | h | h <;> rw [h]
    · exact l2.2.2.2.2.2.2
    · exact l2.1
    · exact l2.2.1
    · exact l2.2.2.1
    · exact l2.2.2.2.1
    · exact l2.2.2.2.2.1
    · exact l2.2.2.2.2.2.1
  · rcases m7_mem v1 v2 v3 v4 v5 v6 v0 with h | h | h | h | h | h | h <;> rw [h]
    · exact l1.2.1
    · exact l1.2.2.1
    · exact l1.2.2.2.1
    · exact l1.2.2.2.2.1
    · exact l1.2.2.2.2.2.1
    · exact l1.2.2.2.2.2.2
    · exact l1.1

theorem one_min7 (v0 v1 v2 v3 v4 v5 v6 : Nat) (h01 : v0 ≠ v1) (h02 : v0 ≠ v2) (h03 : v0 ≠ v3) (h04 : v0 ≠ v4) (h05 : v0 ≠ v5) (h06 : v0 ≠ v6) (h12 : v1 ≠ v2) (h13 : v1 ≠ v3) (h14 : v1 ≠ v4) (h15 : v1 ≠ v5) (h16 : v1 ≠ v6) (h23 : v2 ≠ v3) (h24 : v2 ≠ v4) (h25 : v2 ≠ v5) (h26 : v2 ≠ v6) (h34 : v3 ≠ v4) (h35 : v3 ≠ v5) (h36 : v3 ≠ v6) (h45 : v4 ≠ v5) (h46 : v4 ≠ v6) (h56 : v5 ≠ v6) :
    eqI v0 (m7 v0 v1 v2 v3 v4 v5 v6) + eqI v1 (m7 v0 v1 v2 v3 v4 v5 v6) + eqI v2 (m7 v0 v1 v2 v3 v4 v5 v6) + eqI v3 (m7 v0 v1 v2 v3 v4 v5 v6) + eqI v4 (m7 v0 v1 v2 v3 v4 v5 v6) + eqI v5 (m7 v0 v1 v2 v3 v4 v5 v6) + eqI v6 (m7 v0 v1 v2 v3 v4 v5 v6) = 1 := by
  unfold eqI
  rcases m7_mem v0 v1 v2 v3 v4 v5 v6 with h | h | h | h | h | h | h <;> rw [h]
  · rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h01 e'.symm), ite_neg' _ _ (fun e' => h02 e'.symm), ite_neg' _ _ (fun e' => h03 e'.symm), ite_neg' _ _ (fun e' => h04 e'.symm), ite_neg' _ _ (fun e' => h05 e'.symm), ite_neg' _ _ (fun e' => h06 e'.symm)]; rfl
  · rw [ite_neg' _ _ h01, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h12 e'.symm), ite_neg' _ _ (fun e' => h13 e'.symm), ite_neg' _ _ (fun e' => h14 e'.symm), ite_neg' _ _ (fun e' => h15 e'.symm), ite_neg' _ _ (fun e' => h16 e'.symm)]; rfl
  · rw [ite_neg' _ _ h02, ite_neg' _ _ h12, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h23 e'.symm), ite_neg' _ _ (fun e' => h24 e'.symm), ite_neg' _ _ (fun e' => h25 e'.symm), ite_neg' _ _ (fun e' => h26 e'.symm)]; rfl
  · rw [ite_neg' _ _ h03, ite_neg' _ _ h13, ite_neg' _ _ h23, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h34 e'.symm), ite_neg' _ _ (fun e' => h35 e'.symm), ite_neg' _ _ (fun e' => h36 e'.symm)]; rfl
  · rw [ite_neg' _ _ h04, ite_neg' _ _ h14, ite_neg' _ _ h24, ite_neg' _ _ h34, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h45 e'.symm), ite_neg' _ _ (fun e' => h46 e'.symm)]; rfl
  · rw [ite_neg' _ _ h05, ite_neg' _ _ h15, ite_neg' _ _ h25, ite_neg' _ _ h35, ite_neg' _ _ h45, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h56 e'.symm)]; rfl
  · rw [ite_neg' _ _ h06, ite_neg' _ _ h16, ite_neg' _ _ h26, ite_neg' _ _ h36, ite_neg' _ _ h46, ite_neg' _ _ h56, ite_pos' _ _ rfl]; rfl

section General
variable {G : Type} [DecidableEq G]

/-- **Orbits of an order-7 map** on any completely listed type with an injective code. -/
theorem orbit7 (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (sg : G → G) (h7 : ∀ x, sg (sg (sg (sg (sg (sg (sg (x))))))) = x) (H : G → Int) (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg x = x → H x = 0) :
    sumK LibInt.intR H els = 7 * sumK LibInt.intR (fun x => H x * eqI (code x) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))))) els := by
  let Ls : G → Int := fun x => eqI (code x) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))))
  have hpt : ∀ x, H x = H x * Ls (x) + H x * Ls (sg (x)) + H x * Ls (sg (sg (x))) + H x * Ls (sg (sg (sg (x)))) + H x * Ls (sg (sg (sg (sg (x))))) + H x * Ls (sg (sg (sg (sg (sg (x)))))) + H x * Ls (sg (sg (sg (sg (sg (sg (x))))))) := fun x => by
    by_cases hx : sg x = x
    · rw [hfix x hx]; lib_int
    · have ne : ∀ u v : G, u ≠ v → code u ≠ code v := fun u v h e => h (hinj u v e)
      have f2 : ∀ z, sg (sg (z)) = z → sg z = z := fun z hz => by
        calc sg z = sg (sg (sg (sg (sg (sg (sg (sg (z)))))))) := by rw [h7]
          _ = z := by rw [hz, hz, hz, hz]
      have f3 : ∀ z, sg (sg (sg (z))) = z → sg z = z := fun z hz => by
        calc sg z = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (z))))))))))))))) := by rw [h7, h7]
          _ = z := by rw [hz, hz, hz, hz, hz]
      have f4 : ∀ z, sg (sg (sg (sg (z)))) = z → sg z = z := fun z hz => by
        calc sg z = sg (sg (sg (sg (sg (sg (sg (sg (z)))))))) := by rw [h7]
          _ = z := by rw [hz, hz]
      have f5 : ∀ z, sg (sg (sg (sg (sg (z))))) = z → sg z = z := fun z hz => by
        calc sg z = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (z))))))))))))))) := by rw [h7, h7]
          _ = z := by rw [hz, hz, hz]
      have f6 : ∀ z, sg (sg (sg (sg (sg (sg (z)))))) = z → sg z = z := fun z hz => by
        calc sg z = sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (z)))))))))))))))))))))))))))))))))))) := by rw [h7, h7, h7, h7, h7]
          _ = z := by rw [hz, hz, hz, hz, hz, hz]
      have shift : ∀ y z : G, y ≠ z → sg y ≠ sg z := fun y z h e => h (by
        calc y = sg (sg (sg (sg (sg (sg (sg (y))))))) := (h7 y).symm
          _ = sg (sg (sg (sg (sg (sg (sg (z))))))) := by rw [show sg (y) = sg (z) from e]
          _ = z := h7 z)
      have d01 : sg (x) ≠ x := hx
      have d02 : sg (sg (x)) ≠ x := fun e => hx (f2 x e)
      have d03 : sg (sg (sg (x))) ≠ x := fun e => hx (f3 x e)
      have d04 : sg (sg (sg (sg (x)))) ≠ x := fun e => hx (f4 x e)
      have d05 : sg (sg (sg (sg (sg (x))))) ≠ x := fun e => hx (f5 x e)
      have d06 : sg (sg (sg (sg (sg (sg (x)))))) ≠ x := fun e => hx (f6 x e)
      have d12 := shift _ _ d01
      have d13 := shift _ _ d02
      have d14 := shift _ _ d03
      have d15 := shift _ _ d04
      have d16 := shift _ _ d05
      have d23 := shift _ _ d12
      have d24 := shift _ _ d13
      have d25 := shift _ _ d14
      have d26 := shift _ _ d15
      have d34 := shift _ _ d23
      have d35 := shift _ _ d24
      have d36 := shift _ _ d25
      have d45 := shift _ _ d34
      have d46 := shift _ _ d35
      have d56 := shift _ _ d45
      have hm := one_min7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))
        (ne _ _ (fun e => d01 e.symm)) (ne _ _ (fun e => d02 e.symm)) (ne _ _ (fun e => d03 e.symm)) (ne _ _ (fun e => d04 e.symm)) (ne _ _ (fun e => d05 e.symm)) (ne _ _ (fun e => d06 e.symm)) (ne _ _ (fun e => d12 e.symm)) (ne _ _ (fun e => d13 e.symm)) (ne _ _ (fun e => d14 e.symm)) (ne _ _ (fun e => d15 e.symm)) (ne _ _ (fun e => d16 e.symm)) (ne _ _ (fun e => d23 e.symm)) (ne _ _ (fun e => d2  -- (+258 chars; full line in the Zenodo source)
      have r1 : Ls (sg (x)) = eqI (code (sg (x))) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) := by
        show eqI _ (m7 (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x)))))))))) = _
        rw [h7, m7_rot]
      have r2 : Ls (sg (sg (x))) = eqI (code (sg (sg (x)))) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) := by
        show eqI _ (m7 (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) = _
        rw [h7, m7_rot, m7_rot]
      have r3 : Ls (sg (sg (sg (x)))) = eqI (code (sg (sg (sg (x))))) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) := by
        show eqI _ (m7 (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) = _
        rw [h7, m7_rot, m7_rot, m7_rot]
      have r4 : Ls (sg (sg (sg (sg (x))))) = eqI (code (sg (sg (sg (sg (x)))))) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) := by
        show eqI _ (m7 (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) = _
        rw [h7, m7_rot, m7_rot, m7_rot, m7_rot]
      have r5 : Ls (sg (sg (sg (sg (sg (x)))))) = eqI (code (sg (sg (sg (sg (sg (x))))))) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) := by
        show eqI _ (m7 (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))))) = _
        rw [h7, m7_rot, m7_rot, m7_rot, m7_rot, m7_rot]
      have r6 : Ls (sg (sg (sg (sg (sg (sg (x))))))) = eqI (code (sg (sg (sg (sg (sg (sg (x)))))))) (m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x))))))))) := by
        show eqI _ (m7 (code (sg (sg (sg (sg (sg (sg (x)))))))) (code (sg (sg (sg (sg (sg (sg (sg (x))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x)))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x))))))))))))) (code (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (sg (x  -- (+19 chars; full line in the Zenodo source)
        rw [h7, m7_rot, m7_rot, m7_rot, m7_rot, m7_rot, m7_rot]
      rw [r1, r2, r3, r4, r5, r6]
      show H x = H x * eqI (code x) _ + _ + _ + _ + _ + _ + _
      generalize m7 (code x) (code (sg (x))) (code (sg (sg (x)))) (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x)))))) (code (sg (sg (sg (sg (sg (x))))))) (code (sg (sg (sg (sg (sg (sg (x)))))))) = μ at hm ⊢
      generalize eqI (code x) μ = u0 at hm ⊢
      generalize eqI (code (sg (x))) μ = u1 at hm ⊢
      generalize eqI (code (sg (sg (x)))) μ = u2 at hm ⊢
      generalize eqI (code (sg (sg (sg (x))))) μ = u3 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (x)))))) μ = u4 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (sg (x))))))) μ = u5 at hm ⊢
      generalize eqI (code (sg (sg (sg (sg (sg (sg (x)))))))) μ = u6 at hm ⊢
      calc H x = H x * (u0 + u1 + u2 + u3 + u4 + u5 + u6) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (sg (x))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (x)) (fun x => sg (sg (sg (sg (sg (sg (x))))))) (fun x => h7 x) (fun y => by show sg (sg (sg (sg (sg (sg (sg y)))))) = y; exact h7 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (x)) = H (sg (x)) * Ls (sg (x)); rw [hH]) _
  have q2 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (x)))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (x))) (fun x => sg (sg (sg (sg (sg (x)))))) (fun x => h7 x) (fun y => by show sg (sg (sg (sg (sg (sg (sg y)))))) = y; exact h7 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (x))) = H (sg (sg (x))) * Ls (sg (sg (x))); rw [hH, hH]) _
  have q3 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (x))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (x)))) (fun x => sg (sg (sg (sg (x))))) (fun x => h7 x) (fun y => by show sg (sg (sg (sg (sg (sg (sg y)))))) = y; exact h7 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (x)))) = H (sg (sg (sg (x)))) * Ls (sg (sg (sg (x)))); rw [hH, hH, hH]) _
  have q4 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (x)))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (x))))) (fun x => sg (sg (sg (x)))) (fun x => h7 x) (fun y => by show sg (sg (sg (sg (sg (sg (sg y)))))) = y; exact h7 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (x))))) = H (sg (sg (sg (sg (x))))) * Ls (sg (sg (sg (sg (x))))); rw [hH, hH, hH, hH]) _
  have q5 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (sg (x))))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (sg (x)))))) (fun x => sg (sg (x))) (fun x => h7 x) (fun y => by show sg (sg (sg (sg (sg (sg (sg y)))))) = y; exact h7 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (sg (x)))))) = H (sg (sg (sg (sg (sg (x)))))) * Ls (sg (sg (sg (sg (sg (x)))))); rw [hH, hH, hH, hH, hH]) _
  have q6 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (sg (sg (sg (sg (x)))))))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (sg (sg (sg (sg (x))))))) (fun x => sg (x)) (fun x => h7 x) (fun y => by show sg (sg (sg (sg (sg (sg (sg y)))))) = y; exact h7 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (sg (sg (sg (sg (x))))))) = H (sg (sg (sg (sg (sg (sg (x))))))) * Ls (sg (sg (sg (sg (sg (sg (x))))))); rw [hH, hH, hH, hH, hH, hH]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, QuadDiagonal.sI_add, q1, q2, q3, q4, q5, q6]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

end Orbit7

#print axioms Orbit7.m7_mem
#print axioms Orbit7.m7_le
#print axioms Orbit7.m7_rot
#print axioms Orbit7.one_min7
#print axioms Orbit7.orbit7

#print axioms: 5 theorems, no axioms.

78.101 The set \(\{O\}\cup \mathbb F_p^2\) as a listed type

Product lists, completeness for pairs and for the extra point, and an injective code to \(\mathbb N\).

\[ \sum _{u\in \{O\}\cup \mathbb F_p^2}\delta (u,z)=1\ \text { for every } z. \]

lang/Pairs.lean

/-
  lang/Pairs — T43, cycle 357: the finite set {O} ∪ 𝔽_p² as a completely listed type with an injective code (G4 tool, part 2).

    prodL, sum_prodL     the product list and Σ over it = double sum
    complete_prod        Complete for 𝔽_p × 𝔽_p
    ptsL, complete_pts   Complete for Option (𝔽_p × 𝔽_p) (none = the point at infinity)
    code_inj             code(none) = 0, code(some (x, y)) = 1 + x + N·y is injective (N = p)
  With Orbit5.orbit5 this lets one count orbits of maps on E(𝔽_p) ∪ {O} ⊂ {O} ∪ 𝔽_p².
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Orbit5
import lang.JacobiNorm

namespace Pairs

open FTAChain ZMod YMChain FiniteRecChain JacobMoment
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def prodL {A B : Type} : List A → List B → List (A × B)
  | [], _ => []
  | a :: as, bs => bs.map (fun b => (a, b)) ++ prodL as bs

theorem sum_map {A B : Type} (f : B → Int) (g : A → B) : ∀ l : List A, sumK LibInt.intR f (l.map g) = sumK LibInt.intR (fun a => f (g a)) l
  | [] => rfl
  | a :: as => by show LibInt.intR.add (f (g a)) (sumK LibInt.intR f (as.map g)) = LibInt.intR.add (f (g a)) _; rw [sum_map f g as]

theorem sum_prodL {A B : Type} (f : A × B → Int) (bs : List B) : ∀ as : List A,
    sumK LibInt.intR f (prodL as bs) = sumK LibInt.intR (fun a => sumK LibInt.intR (fun b => f (a, b)) bs) as
  | [] => rfl
  | a :: as => by
    show sumK LibInt.intR f (bs.map (fun b => (a, b)) ++ prodL as bs) = LibInt.intR.add _ _
    rw [sumK_append, sum_map, sum_prodL f bs as]

theorem dg_one {G : Type} [DecidableEq G] (a b : G) (h : a = b) : delta LibInt.intR a b = 1 := ite_pos' _ _ h
theorem dg_zero {G : Type} [DecidableEq G] (a b : G) (h : a ≠ b) : delta LibInt.intR a b = 0 := ite_neg' _ _ h

def pL : List (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) := prodL (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1))

theorem delta_pair (u v : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) :
    delta LibInt.intR u v = delta LibInt.intR u.1 v.1 * delta LibInt.intR u.2 v.2 := by
  by_cases h1 : u.1 = v.1
  · by_cases h2 : u.2 = v.2
    · rw [dg_one u.1 v.1 h1, dg_one u.2 v.2 h2, dg_one u v (Prod.ext h1 h2)]; rfl
    · rw [dg_zero u.2 v.2 h2, dg_zero u v (fun e => h2 (congrArg Prod.snd e))]; show (0 : Int) = _ * 0; generalize delta LibInt.intR u.1 v.1 = a; lib_int
  · rw [dg_zero u.1 v.1 h1, dg_zero u v (fun e => h1 (congrArg Prod.fst e))]; show (0 : Int) = 0 * _; generalize delta LibInt.intR u.2 v.2 = a; lib_int

theorem complete_prod : Complete LibInt.intR (pL m) := fun z => by
  unfold pL
  rw [sum_prodL]
  rw [sumK_ext LibInt.intR _ (fun a => LibInt.intR.mul (delta LibInt.intR a z.1) 1) (fun a => by
    rw [sumK_ext LibInt.intR _ (fun b => LibInt.intR.mul (delta LibInt.intR a z.1) (delta LibInt.intR b z.2)) (fun b => delta_pair m (a, b) z), ← mul_sumK,
      show sumK LibInt.intR (fun b => delta LibInt.intR b z.2) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR z.2])]
  rw [← sumK_mul, show sumK LibInt.intR (fun a => delta LibInt.intR a z.1) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)) = 1 from CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR z.1]
  rfl

theorem some_ne_none (u : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : (some u : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) ≠ none :=
  fun h => by cases h

def ptsL : List (Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) := none :: (pL m).map some

theorem complete_pts : Complete LibInt.intR (ptsL m) := fun z => by
  show LibInt.intR.add (delta LibInt.intR none z) (sumK LibInt.intR (fun P => delta LibInt.intR P z) ((pL m).map some)) = LibInt.intR.one
  rw [sum_map]
  cases z with
  | none =>
    rw [dg_one _ _ rfl, sumK_ext LibInt.intR _ (fun _ => (0 : Int)) (fun u => dg_zero _ _ (some_ne_none m u)), QuadDiagonal.zsum]
    rfl
  | some q =>
    rw [dg_zero _ _ (fun e => some_ne_none m q e.symm), sumK_ext LibInt.intR _ (fun u => delta LibInt.intR u q) (fun u => by
      by_cases h : u = q
      · rw [dg_one u q h, dg_one (some u) (some q) (by rw [h])]
      · rw [dg_zero u q h, dg_zero (some u) (some q) (fun e => h (Option.some.inj e))]), complete_prod m q]
    rfl

def code : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Nat
  | none => 0
  | some (x, y) => 1 + x.val + (2 * m + 1 + 1 + 1) * y.val

theorem code_inj (u v : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : code m u = code m v) : u = v := by
  cases u with
  | none => cases v with
    | none => rfl
    | some q =>
      have e : 0 = 1 + q.1.val + (2 * m + 1 + 1 + 1) * q.2.val := h
      exact absurd (e.trans (show 1 + q.1.val + (2 * m + 1 + 1 + 1) * q.2.val = Nat.succ (q.1.val + (2 * m + 1 + 1 + 1) * q.2.val) by lib_nat)) (fun e' => Nat.noConfusion e')
  | some p => cases v with
    | none =>
      have e : 1 + p.1.val + (2 * m + 1 + 1 + 1) * p.2.val = 0 := h
      exact absurd ((show Nat.succ (p.1.val + (2 * m + 1 + 1 + 1) * p.2.val) = 1 + p.1.val + (2 * m + 1 + 1 + 1) * p.2.val by lib_nat).trans e) (fun e' => Nat.noConfusion e')
    | some q =>
      have h' : Nat.succ (p.2.val * (2 * m + 1 + 1 + 1) + p.1.val) = Nat.succ (q.2.val * (2 * m + 1 + 1 + 1) + q.1.val) := by
        calc Nat.succ (p.2.val * (2 * m + 1 + 1 + 1) + p.1.val) = 1 + p.1.val + (2 * m + 1 + 1 + 1) * p.2.val := by lib_nat
          _ = 1 + q.1.val + (2 * m + 1 + 1 + 1) * q.2.val := h
          _ = _ := by lib_nat
      have e := Nat.succ.inj h'
      have h1 : p.1.val = q.1.val := Inf2e1.uniq_remG _ _ _ _ _ p.1.property q.1.property e
      have h2 : p.2.val = q.2.val := by
        rw [h1] at e
        have ei : Int.ofNat p.2.val * Int.ofNat (2 * m + 1 + 1 + 1) = Int.ofNat q.2.val * Int.ofNat (2 * m + 1 + 1 + 1) := by
          have := congrArg Int.ofNat e
          have e' : Int.ofNat p.2.val * Int.ofNat (2 * m + 1 + 1 + 1) + Int.ofNat q.1.val = Int.ofNat q.2.val * Int.ofNat (2 * m + 1 + 1 + 1) + Int.ofNat q.1.val := this
          calc _ = (Int.ofNat p.2.val * Int.ofNat (2 * m + 1 + 1 + 1) + Int.ofNat q.1.val) + -Int.ofNat q.1.val := by lib_int
            _ = (Int.ofNat q.2.val * Int.ofNat (2 * m + 1 + 1 + 1) + Int.ofNat q.1.val) + -Int.ofNat q.1.val := by rw [e']
            _ = _ := by lib_int
        exact Int.ofNat.inj (JacobiNorm.int_cancel _ _ (2 * m + 1 + 1) ei)
      exact congrArg some (Prod.ext (Subtype.ext h1) (Subtype.ext h2))

end Pairs

#print axioms Pairs.sum_map
#print axioms Pairs.dg_one
#print axioms Pairs.dg_zero
#print axioms Pairs.some_ne_none
#print axioms Pairs.sum_prodL
#print axioms Pairs.delta_pair
#print axioms Pairs.complete_prod
#print axioms Pairs.complete_pts
#print axioms Pairs.code_inj

#print axioms: 9 theorems, no axioms.

78.102 Translation by the 5-torsion point of 11a

On \(E:y^2+y=x^3-x^2\) the translate of \(P=(x,y)\), \(x\ne 0\), by \(T=(0,0)\) is again on \(E\).

\[ P+T=\Big (-\frac {y}{x^2},\ \frac {y^2}{x^3}-1\Big )\in E. \]

lang/G4Trans.lean

/-
  lang/G4Trans — T43, cycle 358: the translation by the 5-torsion point T = (0, 0) on 11a: E: y² + y = x³ − x² (G4, part 3).

  For P = (x, y) ∈ E with x ≠ 0 the chord through P and T gives
      P + T = (−y/x², y²/x³ − 1),        P − T = ((y + 1)/x², −(y + 1)²/x³),
      P + 2T = (x(x² + y)/y², −(x² + y)²/y³ − 1),   P − 2T = (x(x² − y − 1)/(y + 1)², −(x² − y − 1)²/(y + 1)³),
  and on the torsion O → T → (1, −1) → (1, 0) → (0, −1) → O.
    cE             the curve equation as a predicate
    tau_on         P ∈ E, x ≠ 0 ⟹ P + T ∈ E
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Pairs

namespace G4Trans

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed

variable (m : Nat)

def cE (x y : Zp (2 * m + 1 + 1)) : Prop := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))

/-- **P + T stays on E**: (−y/x², y²/x³ − 1) satisfies the curve equation. -/
theorem tau_on (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    cE m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) := by
  have e : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold cE at hc ⊢
  generalize hi : iv m x = i at e ⊢
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul i ((ring  -- (+2356 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul i i))))) ((ring (2 * m + 1 + 1)  -- (+252 chars; full line in the Zenodo source)

end G4Trans

#print axioms G4Trans.tau_on

#print axioms: 1 theorem, no axioms.

78.103 Two steps of the translation on 11a

Applying the translation by \(T=(0,0)\) twice to \(P=(x,y)\) with \(x,y\ne 0\) gives a closed form.

\[ \tau (\tau P)=\Big (\frac {x(x^2+y)}{y^2},\ -\frac {(x^2+y)^2}{y^3}-1\Big ). \]

lang/G4Step.lean

/-
  lang/G4Step — T43, cycle 359: the step P + T ↦ P + 2T on 11a: y² + y = x³ − x² (G4, part 4).

    iv_eq     a·b = 1 ⟹ 1/a = b
    iv_tx     1/x(P + T) = −x²/y
    tau2      for P = (x, y) ∈ E, x ≠ 0, y ≠ 0: τ(τ P) = (x(x² + y)/y², −(x² + y)²/y³ − 1)
  Polynomial certificates (multipliers of i·x − 1, j·y − 1 and the curve equation) found by a std-lib Python reduction.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Trans

namespace G4Step

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans

variable (m : Nat)

def tx (x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))
def ty (x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)

theorem iv_eq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul a b = (ring (2 * m + 1 + 1)).one) : iv m a = b := by
  by_cases ha : a = (ring (2 * m + 1 + 1)).zero
  · rw [ha, ConicSum.iv_zero]
    rw [ha] at h
    calc (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero b) := by lib_ring
      _ = b := by rw [h]; lib_ring
  · have e := iv_mul_self m hpr a ha
    generalize iv m a = c at e ⊢
    calc c = (ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul a b) := by rw [h]; lib_ring
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul c a) b := by lib_ring
      _ = b := by rw [e]; lib_ring

theorem iv_tx (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) :
    iv m (tx m x y) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) (iv m y)) := by
  apply iv_eq m hpr
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  unfold cE at hc
  unfold tx
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j y) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) j)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j (y)) ((r  -- (+405 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gjy]; lib_ring

/-- **P + 2T.** -/
theorem tau2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) :
    tx m (tx m x y) (ty m x y) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y))) ∧
    ty m (tx m x y) (ty m x y) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).mul (iv m y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m  -- (+21 chars; full line in the Zenodo source)
  have hX := iv_tx m hpr x y hc hx hy
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  unfold cE at hc
  rw [show tx m (tx m x y) (ty m x y) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (ty m x y) ((ring (2 * m + 1 + 1)).mul (iv m (tx m x y)) (iv m (tx m x y)))) from rfl,
    show ty m (tx m x y) (ty m x y) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ty m x y) (ty m x y)) ((ring (2 * m + 1 + 1)).mul (iv m (tx m x y)) ((ring (2 * m + 1 + 1)).mul (iv m (tx m x y)) (iv m (tx m x y))))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) from rfl, hX]
  unfold ty
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j y) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  constructor
  · calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) j)) ((ri  -- (+3284 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul j j)) := by rw [gix, gjy, gcv]; lib_ring
  · calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (  -- (+13604 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).mul j ((ring (2 * m + 1 + 1)).mul j j)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by rw [gix, gjy, gcv];   -- (+8 chars; full line in the Zenodo source)

end G4Step

#print axioms G4Step.iv_eq
#print axioms G4Step.iv_tx
#print axioms G4Step.tau2

#print axioms: 3 theorems, no axioms.

78.104 Three steps of the translation on 11a

The third translate of \(P=(x,y)\) by \(T=(0,0)\) equals \(P-2T\), for \(x,y,y+1,x^2+y\ne 0\).

\[ \tau (P+2T)=\Big (\frac {x(x^2-y-1)}{(y+1)^2},\ -\frac {(x^2-y-1)^2}{(y+1)^3}\Big ). \]

lang/G4Step3.lean

/-
  lang/G4Step3 — T43, cycle 360: the step P + 2T ↦ P + 3T = P − 2T on 11a: y² + y = x³ − x² (G4, part 5).

    ty_tx     X·b = 1 ⟹ y(τQ) = x(τQ)²·X − 1 (for Q = (X, Y))
    mul_x2, iv_x2   x(P + 2T) · y²/(x(x² + y)) = 1
    tau3      for P = (x, y) ∈ E with x, y, y + 1, x² + y ≠ 0:
              τ(P + 2T) = (x(x² − y − 1)/(y + 1)², −(x² − y − 1)²/(y + 1)³) = P − 2T
  Certificates (multipliers of i·x − 1, j·y − 1, k(y + 1) − 1, l(x² + y) − 1 and the curve) from a std-lib Python reduction.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Step

namespace G4Step3

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans G4Step

variable (m : Nat)

theorem ty_tx (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (X Y b : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul X b = (ring (2 * m + 1 + 1)).one) :
    ty m X Y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (tx m X Y) (tx m X Y)) X) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  unfold tx ty
  rw [iv_eq m hpr X b h]
  have e : (ring (2 * m + 1 + 1)).mul b X = (ring (2 * m + 1 + 1)).one := by rw [← h]; lib_ring
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul Y Y) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b b))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)
      = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul Y ((ring (2 * m + 1 + 1)).mul b b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul Y ((ring (2 * m + 1 + 1)).mul b b)))) X) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))
          ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul Y Y) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b b))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b X)))) := by lib_ring
    _ = _ := by rw [e]; lib_ring

theorem mul_x2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hs : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y ≠ (ring (2 * m + 1 + 1)).zero) :
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)))) = (ring (2 * m + 1 + 1)).on  -- (+7 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
  have el : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hs
  unfold cE at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) = l at el ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j y) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gky : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring
  have gls : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (j) (j)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (i) (l))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+1353 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gjy, gls]; lib_ring

theorem iv_x2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hs : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y ≠ (ring (2 * m + 1 + 1)).zero) :
    iv m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y)))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y))) := iv_eq m hpr _ _ (mul_x2 m hpr x y hc hx hy hy1  -- (+4 chars; full line in the Zenodo source)

/-- **P + 3T = P − 2T.** -/
theorem tau3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hs : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y ≠ (ring (2 * m + 1 + 1)).zero) :
    tx m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m +   -- (+563 chars; full line in the Zenodo source)
    ty m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m +   -- (+866 chars; full line in the Zenodo source)
  have hX := iv_x2 m hpr x y hc hx hy hy1 hs
  have h1 : tx m ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).add ((ring (  -- (+575 chars; full line in the Zenodo source)
    unfold tx
    rw [hX]
    have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
    have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
    have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
    have el : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hs
    unfold cE at hc
    generalize iv m x = i at ei ⊢
    generalize iv m y = j at ej ⊢
    generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) = l at el ⊢
    have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
    have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j y) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
    have gky : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring
    have gls : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
    have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
    calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y)) ((ring (2 * m + 1 + 1)).mul (j) ((ring (2 * m + 1 + 1)).mul (j) (j))))) ((ring (2 * m + 1 + 1))  -- (+38884 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (k) (k))) := by rw [gix, gjy, gky, gls, gcv]; lib_ring
  refine ⟨h1, ?_⟩
  rw [ty_tx m hpr _ _ _ (mul_x2 m hpr x y hc hx hy hy1 hs), h1]
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
  unfold cE at hc
  generalize iv m y = j at ej ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
  obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul j y) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul k w) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
  have gzz : (ring (2 * m + 1 + 1)).add w ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (k) (k)))) ((ring (2 * m + 1 + 1)).mul (x) (  -- (+59141 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) ((ring (2   -- (+158 chars; full line in the Zenodo source)

end G4Step3

#print axioms G4Step3.ty_tx
#print axioms G4Step3.mul_x2
#print axioms G4Step3.iv_x2
#print axioms G4Step3.tau3

#print axioms: 4 theorems, no axioms.

78.105 The translation on 11a has order five

The fourth and fifth translates of \(P=(x,y)\) by \(T=(0,0)\) are \(P-T\) and \(P\), for \(x,y+1,x^2-y-1\ne 0\).

\[ \tau (P-2T)=\Big (\frac {y+1}{x^2},\ -\frac {(y+1)^2}{x^3}\Big ),\qquad \tau (P-T)=P. \]

lang/G4Step4.lean

/-
  lang/G4Step4 — T43, cycle 361: the last two steps P − 2T ↦ P − T ↦ P on 11a: y² + y = x³ − x² (G4, part 6).

    mul_x3          x(P − 2T)·(y + 1)²/(x(x² − y − 1)) = 1, i.e. 1/x(P − 2T) = (y + 1)²/(x(x² − y − 1))
    tau4            τ(P − 2T) = P − T = ((y + 1)/x², −(y + 1)²/x³)        (x, y + 1, x² − y − 1 ≠ 0)
    mul_x4          x(P − T)·x²/(y + 1) = 1
    tau5            τ(P − T) = P                                             (x, y + 1 ≠ 0)
  Certificates from the phased std-lib Python reduction (clear i, j, k; w = y + 1; then the curve).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Step3

namespace G4Step4

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans G4Step G4Step3

variable (m : Nat)

theorem mul_x3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hw0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring  -- (+84 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) (iv m ((ring (2 * m + 1 + 1)).  -- (+485 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
  have ej : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m  -- (+73 chars; full line in the Zenodo source)
  unfold cE at hc
  generalize iv m x = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) = j at ej ⊢
  obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
  have gzz : (ring (2 * m + 1 + 1)).add (w) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gjw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (k) (k)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).a  -- (+3537 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gkz, gjw, gzz]; lib_ring

/-- **P + 4T = P − T.** -/
theorem tau4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hw0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (  -- (+82 chars; full line in the Zenodo source)
    tx m ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1  -- (+2293 chars; full line in the Zenodo source)
  have hX := iv_eq m hpr _ _ (mul_x3 m hpr x y hc hx hy1 hw0)
  have h1 : tx m ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) (iv m ((ring (2 * m + 1 + 1)).add y (ring (2  -- (+879 chars; full line in the Zenodo source)
    unfold tx
    rw [hX]
    have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
    have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
    have ej : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 *  -- (+75 chars; full line in the Zenodo source)
    unfold cE at hc
    generalize iv m x = i at ei ⊢
    generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) = j at ej ⊢
    obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
    have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
    have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
    have gzz : (ring (2 * m + 1 + 1)).add (w) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
    have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
    have gjw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
    calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2   -- (+1649 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (i) (i)) := by rw [ej, ek]; lib_ring
  refine ⟨h1, ?_⟩
  rw [ty_tx m hpr _ _ _ (mul_x3 m hpr x y hc hx hy1 hw0), h1]
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
  have ej : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m  -- (+73 chars; full line in the Zenodo source)
  unfold cE at hc
  generalize iv m x = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) = j at ej ⊢
  obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
  have gzz : (ring (2 * m + 1 + 1)).add (w) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have gjw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (i) (i))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1  -- (+15549 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (i) (i)))) := by rw [gix, gkz, gzz, gcv]; lib_ring

theorem mul_x4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) :
    (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).one := by
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
  unfold cE at hc
  generalize iv m x = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
  obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
  have gzz : (ring (2 * m + 1 + 1)).add (w) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (i) (i))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) (k)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).ad  -- (+772 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gkz, gzz]; lib_ring

/-- **P + 5T = P.** -/
theorem tau5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy1 : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) :
    tx m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring   -- (+497 chars; full line in the Zenodo source)
  have hX := iv_eq m hpr _ _ (mul_x4 m hpr x y hc hx hy1)
  have h1 : tx m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (iv m x)  -- (+60 chars; full line in the Zenodo source)
    unfold tx
    rw [hX]
    have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
    have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
    unfold cE at hc
    generalize iv m x = i at ei ⊢
    generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
    obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
    have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
    have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
    have gzz : (ring (2 * m + 1 + 1)).add (w) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
    have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
    calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (i) (i))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul  -- (+3188 chars; full line in the Zenodo source)
      _ = x := by rw [gix, gkz, gzz]; lib_ring
  refine ⟨h1, ?_⟩
  rw [ty_tx m hpr _ _ _ (mul_x4 m hpr x y hc hx hy1), h1]
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hy1
  unfold cE at hc
  generalize iv m x = i at ei ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = k at ek ⊢
  obtain ⟨w, hw⟩ : ∃ w, w = (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one := ⟨_, rfl⟩
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gkz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hw, ek]; lib_ring
  have gzz : (ring (2 * m + 1 + 1)).add (w) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by rw [hw]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).o  -- (+307 chars; full line in the Zenodo source)
    _ = y := by rw [gix]; lib_ring

end G4Step4

#print axioms G4Step4.mul_x3
#print axioms G4Step4.tau4
#print axioms G4Step4.mul_x4
#print axioms G4Step4.tau5

#print axioms: 4 theorems, no axioms.

78.106 Generic points of 11a

On \(y^2+y=x^3-x^2\), every point with \(x\notin \{0,1\}\) meets all side conditions of the five translation steps.

\[ x\notin \{0,1\}\ \Longrightarrow \ y\ne 0,\ y+1\ne 0,\ x^2+y\ne 0,\ x^2-y-1\ne 0. \]

lang/G4Gen.lean

/-
  lang/G4Gen — T43, cycle 362: the generic points of 11a: y² + y = x³ − x² (G4, part 7).

    mul_eq_zero   a·b = 0 ⟹ a = 0 ∨ b = 0 in 𝔽_p
    x01, x01_2, x01_3   x^k(x − 1) = 0 ⟹ x = 0 ∨ x = 1 (k = 1, 2, 3)
    gen_y, gen_y1, gen_s, gen_w
                  for (x, y) ∈ E with x ≠ 0, 1: y ≠ 0, y + 1 ≠ 0, x² + y ≠ 0, x² − y − 1 ≠ 0
  So every point with x ∉ {0, 1} satisfies all the side conditions of G4Trans … G4Step4.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Step4

namespace G4Gen

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans

variable (m : Nat)

theorem mul_eq_zero (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul a b = (ring (2 * m + 1 + 1)).zero) : a = (ring (2 * m + 1 + 1)).zero ∨ b = (ring (2 * m + 1 + 1)).zero := by
  by_cases ha : a = (ring (2 * m + 1 + 1)).zero
  · exact Or.inl ha
  · refine Or.inr ?_
    have e := iv_mul_self m hpr a ha
    generalize iv m a = c at e
    calc b = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul c a) b := by rw [e]; lib_ring
      _ = (ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul a b) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [h]; lib_ring

theorem x01 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero) :
    x = (ring (2 * m + 1 + 1)).zero ∨ x = (ring (2 * m + 1 + 1)).one := by
  rcases mul_eq_zero m hpr _ _ h with e | e
  · exact Or.inl e
  · exact Or.inr (by calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [e]; lib_ring)

theorem x01_2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero) :
    x = (ring (2 * m + 1 + 1)).zero ∨ x = (ring (2 * m + 1 + 1)).one := by
  rcases mul_eq_zero m hpr _ _ h with e | e
  · exact Or.inl e
  · exact x01 m hpr x e

theorem x01_3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)))) = (ring (2 * m + 1 + 1)).zero) :
    x = (ring (2 * m + 1 + 1)).zero ∨ x = (ring (2 * m + 1 + 1)).one := by
  rcases mul_eq_zero m hpr _ _ h with e | e
  · exact Or.inl e
  · exact x01_2 m hpr x e

theorem gen_y (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : y ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  unfold cE at hc; rw [e] at hc
  rcases x01_2 m hpr x (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))) (ring (2 * m + 1 + 1)).zero := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [← hc]; lib_ring) with e' | e'
  · exact h0 e'
  · exact h1 e'

theorem gen_y1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  unfold cE at hc
  have hy : y = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
    calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
      _ = _ := by rw [e]; lib_ring
  rw [hy] at hc
  rcases x01_2 m hpr x (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [← hc]; lib_ring) with e' | e'
  · exact h0 e'
  · exact h1 e'

theorem gen_s (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  unfold cE at hc
  have hy : y = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x) := by
    calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)) := by lib_ring
      _ = _ := by rw [e]; lib_ring
  rw [hy] at hc
  rcases x01_3 m hpr x (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e' | e'
  · exact h0 e'
  · exact h1 e'

theorem gen_w (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  unfold cE at hc
  have hy : y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
    calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) := by lib_ring
      _ = _ := by rw [e]; lib_ring
  rw [hy] at hc
  rcases x01_3 m hpr x (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring  -- (+51 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)))) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e' | e'
  · exact h0 e'
  · exact h1 e'

end G4Gen

#print axioms G4Gen.mul_eq_zero
#print axioms G4Gen.x01
#print axioms G4Gen.x01_2
#print axioms G4Gen.x01_3
#print axioms G4Gen.gen_y
#print axioms G4Gen.gen_y1
#print axioms G4Gen.gen_s
#print axioms G4Gen.gen_w

#print axioms: 8 theorems, no axioms.

78.107 The translation map of 11a

Translation by \(T=(0,0)\), extended by the identity off the curve, is a map of order dividing five on \(\{O\}\cup \mathbb F_p^2\).

\[ \tau ^5=\mathrm {id},\qquad O\mapsto (0,0)\mapsto (1,-1)\mapsto (1,0)\mapsto (0,-1)\mapsto O. \]

lang/G4Map.lean

/-
  lang/G4Map — T43, cycle 363: the translation τ = (· + T) on {O} ∪ 𝔽_p² for 11a: y² + y = x³ − x², and τ⁵ = id (G4, part 8).

    tau          O ↦ (0, 0); (0, 0) ↦ (1, −1); (0, y ≠ 0) ↦ O on E; (x ≠ 0, y) ↦ (−y/x², y²/x³ − 1) on E; identity off E
    tau_tors     O → (0, 0) → (1, −1) → (1, 0) → (0, −1) → O
    tau_gen5     τ⁵ P = P for P ∈ E with x ∉ {0, 1} (the chain G4Trans → G4Step → G4Step3 → G4Step4)
    tau5         τ⁵ = id on all of {O} ∪ 𝔽_p²
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Gen

namespace G4Map

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans G4Step G4Step3 G4Step4 G4Gen
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

abbrev Pt := Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))

def tau : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))
  | none => some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)
  | some p => if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.2 p.2) p.2 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.1 ((ring (2 * m + 1 + 1)).mul p.1 p.1)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul p.1 p.1)) then
      (if p.1 = (ring (2 * m + 1 + 1)).zero then (if p.2 = (ring (2 * m + 1 + 1)).zero then some ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) else none) else some (tx m p.1 p.2, ty m p.1 p.2))
    else some p

theorem tau_some (x y : Zp (2 * m + 1 + 1)) : tau m (some (x, y)) =
    if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x)) then
      (if x = (ring (2 * m + 1 + 1)).zero then (if y = (ring (2 * m + 1 + 1)).zero then some ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) else none) else some (tx m x y, ty m x y))
    else some (x, y) := rfl

theorem tau_gen (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : tau m (some (x, y)) = some (tx m x y, ty m x y) := by
  unfold cE at hc
  rw [tau_some, ite_pos' _ _ hc, ite_neg' _ _ hx]

theorem tau_off (x y : Zp (2 * m + 1 + 1)) (hc : ¬ cE m x y) : tau m (some (x, y)) = some (x, y) := by
  unfold cE at hc
  rw [tau_some, ite_neg' _ _ hc]

theorem on_tx (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : cE m (tx m x y) (ty m x y) :=
  tau_on m hpr x y hc hx

theorem mul_ne (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).mul a b ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  rcases mul_eq_zero m hpr a b e with h | h
  · exact ha h
  · exact hb h

theorem neg_ne (a : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).neg a ≠ (ring (2 * m + 1 + 1)).zero := fun e => ha (by
  calc a = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg a) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring)

theorem one_ne : (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * m + 1)

theorem iv_one (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : iv m (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).one := iv_eq m hpr _ _ (by lib_ring)

theorem tau_O : tau m none = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero) := rfl

theorem tau_T : tau m (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = some ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  rw [tau_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mu  -- (+109 chars; full line in the Zenodo source)

theorem tau_2T (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : tau m (some ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = some ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).zero) := by
  rw [tau_gen m _ _ (by unfold cE; lib_ring) (one_ne m)]
  unfold tx ty; rw [iv_one m hpr]
  exact congrArg some (Prod.ext (by show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one; lib_ring)
    (by show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).  -- (+46 chars; full line in the Zenodo source)

theorem tau_3T (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : tau m (some ((ring (2 * m + 1 + 1)).one, (ring (2 * m + 1 + 1)).zero)) = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  rw [tau_gen m _ _ (by unfold cE; lib_ring) (one_ne m)]
  unfold tx ty; rw [iv_one m hpr]
  exact congrArg some (Prod.ext (by show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero; lib_ring)
    (by show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1))  -- (+16 chars; full line in the Zenodo source)

theorem tau_4T : tau m (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = none := by
  rw [tau_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero   -- (+213 chars; full line in the Zenodo source)

theorem tau_tors (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : tau m (tau m (tau m (tau m (tau m none)))) = none := by
  rw [tau_O, tau_T, tau_2T m hpr, tau_3T m hpr, tau_4T]

theorem tau_gen5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (h1 : x ≠ (ring (2 * m + 1 + 1)).one) :
    tau m (tau m (tau m (tau m (tau m (some (x, y)))))) = some (x, y) := by
  have hy := gen_y m hpr x y hc h0 h1
  have hy1 := gen_y1 m hpr x y hc h0 h1
  have hs := gen_s m hpr x y hc h0 h1
  have hw := gen_w m hpr x y hc h0 h1
  have ix := iv_nz m hpr x h0
  have iy := iv_nz m hpr y hy
  have iy1 := iv_nz m hpr _ hy1
  -- step 1
  have c1 := on_tx m hpr x y hc h0
  have n1 : tx m x y ≠ (ring (2 * m + 1 + 1)).zero := neg_ne m _ (mul_ne m hpr _ _ hy (mul_ne m hpr _ _ ix ix))
  -- step 2
  have t2 := tau2 m hpr x y hc h0 hy
  have c2 := on_tx m hpr _ _ c1 n1
  rw [t2.1, t2.2] at c2
  have n2 := mul_ne m hpr _ _ h0 (mul_ne m hpr _ _ hs (mul_ne m hpr _ _ iy iy))
  -- step 3
  have t3 := tau3 m hpr x y hc h0 hy hy1 hs
  have c3 := on_tx m hpr _ _ c2 n2
  rw [t3.1, t3.2] at c3
  have n3 := mul_ne m hpr _ _ h0 (mul_ne m hpr _ _ hw (mul_ne m hpr _ _ iy1 iy1))
  -- step 4
  have t4 := tau4 m hpr x y hc h0 hy1 hw
  have c4 := on_tx m hpr _ _ c3 n3
  rw [t4.1, t4.2] at c4
  have n4 := mul_ne m hpr _ _ hy1 (mul_ne m hpr _ _ ix ix)
  have t5 := tau5 m hpr x y hc h0 hy1
  rw [tau_gen m x y hc h0, tau_gen m _ _ c1 n1, t2.1, t2.2, tau_gen m _ _ c2 n2, t3.1, t3.2, tau_gen m _ _ c3 n3, t4.1, t4.2,
    tau_gen m _ _ c4 n4, t5.1, t5.2]

theorem y_tors (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (y : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero) :
    y = (ring (2 * m + 1 + 1)).zero ∨ y = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
  rcases mul_eq_zero m hpr _ _ h with e | e
  · exact Or.inl e
  · exact Or.inr (by calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
      _ = _ := by rw [e]; lib_ring)

/-- **τ⁵ = id** on {O} ∪ 𝔽_p². -/
theorem tau5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    tau m (tau m (tau m (tau m (tau m P)))) = P := by
  cases P with
  | none => exact tau_tors m hpr
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))
    · by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · have hy : (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
          unfold cE at hc; rw [h0] at hc
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y := by lib_ring
            _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
        rw [h0]
        rcases y_tors m hpr y hy with e | e <;> rw [e]
        · rw [tau_T, tau_2T m hpr, tau_3T m hpr, tau_4T, tau_O]
        · rw [tau_4T, tau_O, tau_T, tau_2T m hpr, tau_3T m hpr]
      · by_cases h1 : x = (ring (2 * m + 1 + 1)).one
        · have hy : (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add y (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
            unfold cE at hc; rw [h1] at hc
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y := by lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
          rw [h1]
          rcases y_tors m hpr y hy with e | e <;> rw [e]
          · rw [tau_3T m hpr, tau_4T, tau_O, tau_T, tau_2T m hpr]
          · rw [tau_2T m hpr, tau_3T m hpr, tau_4T, tau_O, tau_T]
        · exact tau_gen5 m hpr x y hc h0 h1
    · rw [tau_off m x y hc, tau_off m x y hc, tau_off m x y hc, tau_off m x y hc, tau_off m x y hc]

end G4Map

#print axioms G4Map.tau_some
#print axioms G4Map.tau_gen
#print axioms G4Map.tau_off
#print axioms G4Map.on_tx
#print axioms G4Map.mul_ne
#print axioms G4Map.neg_ne
#print axioms G4Map.one_ne
#print axioms G4Map.iv_one
#print axioms G4Map.tau_O
#print axioms G4Map.tau_T
#print axioms G4Map.tau_2T
#print axioms G4Map.tau_3T
#print axioms G4Map.tau_4T
#print axioms G4Map.tau_tors
#print axioms G4Map.tau_gen5
#print axioms G4Map.y_tors
#print axioms G4Map.tau5

#print axioms: 17 theorems, no axioms.

78.108 No fixed points of the translation

For \(p\ne 11\) the translation by \(T\) moves every point of \(E\cup \{O\}\) and preserves \(E\cup \{O\}\).

\[ \tau (x,y)=(x,y),\ (x,y)\in E,\ x\ne 0\ \Longrightarrow \ 11=-9(4x^2-3)+4(3x+2)(3x-2)=0. \]

lang/G4Fix.lean

/-
  lang/G4Fix — T43, cycle 364: τ has no fixed point on E ∪ {O} for 11a when p ≠ 11, and τ preserves E ∪ {O} (G4, part 9).

    fixed_alg    x ≠ 0, ix = 1, τ(x, y) = (x, y), (x, y) ∈ E ⟹ 11 = 0
                 (−y/x² = x gives y = −x³; then y(2y + 1) = 0, x² = −y² − 2y, x(y + 2) = 1, so 4x² = 3, 3x = 2 and
                  11 = −9(4x² − 3) + 4(3x + 2)(3x − 2))
    no_fix       τP ≠ P for P ∈ E ∪ {O}
    onE, onE_tau the indicator of E ∪ {O} is τ-invariant
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Map

namespace G4Fix

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans G4Step G4Gen G4Map
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def eleven : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2  -- (+221 chars; full line in the Zenodo source)

theorem fixed_alg (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x y i : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (ei : (ring (2 * m + 1 + 1)).mul i x = (ring (2 * m + 1 + 1)).one)
    (fx : (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)) = x) (fy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = y)
    (hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))) : eleven m = (ring (2 * m + 1 + 1)).zero := by
  have gi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gX : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).neg (x)) = (ring (2 * m + 1 + 1)).zero := by rw [fx]; lib_ring
  have gY : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg (y)) = (ring (2 * m + 1 + 1)).zero := by rw [fy]; lib_ring
  have gc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))))) ((ring (2 * m + 1 + 1)).mul (x) (x)) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  have g1 : (ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).neg (x))))) ((ring (2  -- (+313 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gX, gi]; lib_ring
  have g2 : (ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)) (y)) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)) (y)) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring   -- (+441 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).mul (i) (x))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1   -- (+95 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gY, g1, gi]; lib_ring
  have hy : y ≠ (ring (2 * m + 1 + 1)).zero := fun e => hx (by
    rw [e] at g1
    rcases mul_eq_zero m hpr _ _ (show (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) = (ring (2 * m + 1 + 1)).zero by rw [← g1]; lib_ring) with h | h
    · exact h
    · rcases mul_eq_zero m hpr _ _ h with h' | h'
      · exact h'
      · exact h')
  have g2y : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)) (y)) ((ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
    rcases mul_eq_zero m hpr _ _ g2 with h | h
    · exact absurd h hy
    · exact h
  have g3 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul (y) (y))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)) (y)) = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul (y) (y))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)) (y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y  -- (+267 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gc, g1]; lib_ring
  have g4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by
    have h : (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by
      calc (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).add (y) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring   -- (+334 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by rw [g3, g1]; lib_ring
    rcases mul_eq_zero m hpr _ _ h with h' | h'
    · exact absurd h' hy
    · exact h'
  have gA : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m +  -- (+107 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1   -- (+1110 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [g3, g2y]; lib_ring
  have gB : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +  -- (+555 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [g4, g2y]; lib_ring
  calc eleven m = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).  -- (+1507 chars; full line in the Zenodo source)
        unfold eleven; lib_ring
    _ = (ring (2 * m + 1 + 1)).zero := by rw [gA, gB]; lib_ring

theorem some_ne_none' (u : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : (some u : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) ≠ none :=
  fun h => by cases h

/-- **No fixed points** of τ on E ∪ {O} when p ≠ 11. -/
theorem no_fix (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h11 : eleven m ≠ (ring (2 * m + 1 + 1)).zero) (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) :
    tau m (some (x, y)) ≠ some (x, y) := fun e => by
  by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
  · have hc' := hc
    unfold cE at hc'
    rw [tau_some, ite_pos' _ _ hc', ite_pos' _ _ h0] at e
    by_cases hy : y = (ring (2 * m + 1 + 1)).zero
    · rw [ite_pos' _ _ hy] at e
      exact one_ne m ((congrArg Prod.fst (Option.some.inj e)).trans h0)
    · rw [ite_neg' _ _ hy] at e
      exact some_ne_none' m _ e.symm
  · rw [tau_gen m x y hc h0] at e
    have e' := Option.some.inj e
    have fx : tx m x y = x := congrArg Prod.fst e'
    have fy : ty m x y = y := congrArg Prod.snd e'
    unfold tx at fx; unfold ty at fy
    exact h11 (fixed_alg m hpr x y (iv m x) h0 (iv_mul_self m hpr x h0) fx fy hc)

theorem no_fix_O : tau m none ≠ none := fun h => by cases h

def onE : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Int
  | none => 1
  | some p => if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.2 p.2) p.2 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.1 ((ring (2 * m + 1 + 1)).mul p.1 p.1)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul p.1 p.1)) then 1 else 0

theorem onE_on (x y : Zp (2 * m + 1 + 1)) (hc : cE m x y) : onE m (some (x, y)) = 1 := by
  unfold cE at hc; show (if _ then _ else _) = _; rw [ite_pos' _ _ hc]

theorem onE_off (x y : Zp (2 * m + 1 + 1)) (hc : ¬ cE m x y) : onE m (some (x, y)) = 0 := by
  unfold cE at hc; show (if _ then _ else _) = _; rw [ite_neg' _ _ hc]

/-- **τ preserves E ∪ {O}.** -/
theorem onE_tau (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : onE m (tau m P) = onE m P := by
  cases P with
  | none => rw [tau_O]; exact onE_on m _ _ (by unfold cE; lib_ring)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))
    · rw [onE_on m x y hc]
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tau_some, ite_pos' _ _ hc, ite_pos' _ _ h0]
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [ite_pos' _ _ hy]; exact onE_on m _ _ (by unfold cE; lib_ring)
        · rw [ite_neg' _ _ hy]; rfl
      · rw [tau_gen m x y hc h0]; exact onE_on m _ _ (on_tx m hpr x y hc h0)
    · rw [tau_off m x y hc]

/-- H = 0 at the fixed points of τ. -/
theorem onE_fix (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h11 : eleven m ≠ (ring (2 * m + 1 + 1)).zero) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : tau m P = P) :
    onE m P = 0 := by
  cases P with
  | none => exact absurd h (no_fix_O m)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))
    · exact absurd h (no_fix m hpr h11 x y hc)
    · exact onE_off m x y hc

end G4Fix

#print axioms G4Fix.fixed_alg
#print axioms G4Fix.some_ne_none'
#print axioms G4Fix.no_fix
#print axioms G4Fix.no_fix_O
#print axioms G4Fix.onE_on
#print axioms G4Fix.onE_off
#print axioms G4Fix.onE_tau
#print axioms G4Fix.onE_fix

#print axioms: 8 theorems, no axioms.

78.109 Five divides the point count of 11a

For every odd prime \(p\ne 11\) the curve \(y^2+y=x^3-x^2\) has a multiple of five points over \(\mathbb F_p\).

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2+y=x^3-x^2\}\equiv 0\pmod 5,\qquad a_p\equiv p+1\pmod 5. \]

lang/G4Count.lean

/-
  lang/G4Count — T43, cycle 365: 5 | #E(𝔽_p) for 11a: y² + y = x³ − x², every odd prime p ≠ 11 (G4).

    g4_points   Σ_{P ∈ {O} ∪ 𝔽_p²} [P ∈ E ∪ {O}] = 5k
    g4          1 + #{(x, y) ∈ 𝔽_p² : y² + y = x³ − x²} = 5k
  The translation τ by the 5-torsion point T = (0, 0) (G4Map: τ⁵ = id; G4Fix: τ preserves E ∪ {O} and has no fixed point there
  for p ≠ 11) and the orbit count Orbit5.orbit5 on the completely listed set {O} ∪ 𝔽_p² (Pairs).
  Since #E(𝔽_p) = p + 1 − a_p, this is a_p ≡ p + 1 (mod 5) for the newform 11a.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.G4Fix

namespace G4Count

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Trans G4Map G4Fix Pairs

variable (m : Nat)

/-- **5 | #(E(𝔽_p) ∪ {O})** for 11a, p ≠ 11. -/
theorem g4_points (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h11 : eleven m ≠ (ring (2 * m + 1 + 1)).zero) : ∃ k : Int, sumK LibInt.intR (onE m) (ptsL m) = 5 * k :=
  ⟨_, Orbit5.orbit5 (ptsL m) (complete_pts m) (code m) (code_inj m) (tau m) (tau5 m hpr) (onE m) (onE_tau m hpr) (onE_fix m hpr h11)⟩

def cnt (q : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : Int :=
  if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul q.2 q.2) q.2 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul q.1 ((ring (2 * m + 1 + 1)).mul q.1 q.1)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul q.1 q.1)) then 1 else 0

theorem pts_split : sumK LibInt.intR (onE m) (ptsL m) = 1 + sumK LibInt.intR (cnt m) (pL m) := by
  show LibInt.intR.add 1 (sumK LibInt.intR (onE m) ((pL m).map some)) = _
  rw [sum_map]; rfl

/-- **G4: 5 | 1 + #E_aff(𝔽_p)** for y² + y = x³ − x² and every odd prime p ≠ 11. -/
theorem g4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h11 : eleven m ≠ (ring (2 * m + 1 + 1)).zero) : ∃ k : Int, 1 + sumK LibInt.intR (cnt m) (pL m) = 5 * k := by
  rw [← pts_split]; exact g4_points m hpr h11

end G4Count

#print axioms G4Count.g4_points
#print axioms G4Count.pts_split
#print axioms G4Count.g4

#print axioms: 3 theorems, no axioms.

78.110 Three divides the point count of the Tate family

Every curve \(y^2+axy+by=x^3\) with \(b\ne 0\), \(a^3\ne 27b\) has a multiple of three points over \(\mathbb F_p\).

\[ \tau (x,y)=\Big (-\frac {by}{x^2},-\frac {b^2y}{x^3}\Big ),\ \tau ^3=\mathrm {id}\ \Longrightarrow \ 1+\#\{y^2+axy+by=x^3\}\equiv 0\pmod 3. \]

lang/G5Tate3.lean

/-
  lang/G5Tate3 — T43, cycle 366: 3 | #E(𝔽_p) for every curve y² + axy + by = x³ with b ≠ 0, a³ ≠ 27b (G5, order 3).

  The point T = (0, 0) has order 3 (Tate normal form); translation by T is
      τ(x, y) = (−by/x², −b²y/x³)  (x ≠ 0),   (0, 0) ↦ (0, −b) ↦ O ↦ (0, 0),
  with τ²(x, y) = (bx/y, −bx³/y²) and τ³ = id; a fixed point forces b²(a³ − 27b) = 0.
    one_min3, m3_rot, orbit3g   orbit counting for an order-3 map on any listed type with an injective code
    on3, step2, step3, tau3     the translation and τ³ = id on {O} ∪ 𝔽_p²
    fixed_alg3, onT_fix, onT_tau
    g5                          1 + #{(x, y) : y² + axy + by = x³} = 3k
  Certificates from the std-lib Python reduction. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Count

namespace G5Tate3

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map Pairs Orbit5
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def m3 (a b c : Nat) : Nat := mn a (mn b c)

theorem m3_mem (a b c : Nat) : m3 a b c = a ∨ m3 a b c = b ∨ m3 a b c = c := by
  unfold m3
  rcases mn_or a (mn b c) with h | h
  · exact Or.inl h
  rw [h]
  rcases mn_or b c with h | h
  · exact Or.inr (Or.inl h)
  · exact Or.inr (Or.inr h)

theorem m3_le (a b c : Nat) : m3 a b c ≤ a ∧ m3 a b c ≤ b ∧ m3 a b c ≤ c :=
  ⟨mn_le_l _ _, Nat.le_trans (mn_le_r _ _) (mn_le_l _ _), Nat.le_trans (mn_le_r _ _) (mn_le_r _ _)⟩

theorem m3_rot (a b c : Nat) : m3 b c a = m3 a b c := by
  have l1 := m3_le a b c
  have l2 := m3_le b c a
  apply Nat.le_antisymm
  · rcases m3_mem a b c with h | h | h <;> rw [h]
    · exact l2.2.2
    · exact l2.1
    · exact l2.2.1
  · rcases m3_mem b c a with h | h | h <;> rw [h]
    · exact l1.2.1
    · exact l1.2.2
    · exact l1.1

theorem one_min3 (a b c : Nat) (hab : a ≠ b) (hac : a ≠ c) (hbc : b ≠ c) :
    eqI a (m3 a b c) + eqI b (m3 a b c) + eqI c (m3 a b c) = 1 := by
  unfold eqI
  rcases m3_mem a b c with h | h | h <;> rw [h]
  · rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => hab e'.symm), ite_neg' _ _ (fun e' => hac e'.symm)]; rfl
  · rw [ite_neg' _ _ hab, ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => hbc e'.symm)]; rfl
  · rw [ite_neg' _ _ hac, ite_neg' _ _ hbc, ite_pos' _ _ rfl]; rfl

section General
variable {G : Type} [DecidableEq G]

/-- **Orbits of an order-3 map** on any completely listed type with an injective code. -/
theorem orbit3g (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (sg : G → G) (h3 : ∀ x, sg (sg (sg (x))) = x) (H : G → Int) (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg x = x → H x = 0) :
    sumK LibInt.intR H els = 3 * sumK LibInt.intR (fun x => H x * eqI (code x) (m3 (code x) (code (sg (x))) (code (sg (sg (x)))))) els := by
  let Ls : G → Int := fun x => eqI (code x) (m3 (code x) (code (sg (x))) (code (sg (sg (x)))))
  have hpt : ∀ x, H x = H x * Ls x + H x * Ls (sg (x)) + H x * Ls (sg (sg (x))) := fun x => by
    by_cases hx : sg x = x
    · rw [hfix x hx]; lib_int
    · have ne : ∀ u v : G, u ≠ v → code u ≠ code v := fun u v h e => h (hinj u v e)
      have d01 : sg (x) ≠ x := hx
      have d02 : sg (sg (x)) ≠ x := fun e => hx (by
        calc sg (x) = sg (sg (sg (sg (x)))) := by rw [h3]
          _ = x := by rw [e, e])
      have d12 : sg (sg (x)) ≠ sg (x) := fun e => hx (by
        calc sg (x) = sg (sg (x)) := e.symm
          _ = sg (sg (sg (x))) := congrArg sg e.symm
          _ = x := h3 x)
      have hm := one_min3 (code x) (code (sg (x))) (code (sg (sg (x))))
        (ne _ _ (fun e => d01 e.symm)) (ne _ _ (fun e => d02 e.symm)) (ne _ _ (fun e => d12 e.symm))
      have r1 : Ls (sg (x)) = eqI (code (sg (x))) (m3 (code x) (code (sg (x))) (code (sg (sg (x))))) := by
        show eqI _ (m3 _ _ (code (sg (sg (sg (x)))))) = _; rw [h3, m3_rot]
      have r2 : Ls (sg (sg (x))) = eqI (code (sg (sg (x)))) (m3 (code x) (code (sg (x))) (code (sg (sg (x))))) := by
        show eqI _ (m3 _ (code (sg (sg (sg (x))))) (code (sg (sg (sg (sg (x))))))) = _; rw [h3, m3_rot, m3_rot]
      rw [r1, r2]
      show H x = H x * eqI (code x) _ + _ + _
      generalize m3 (code x) (code (sg (x))) (code (sg (sg (x)))) = μ at hm ⊢
      generalize eqI (code x) μ = u0 at hm ⊢; generalize eqI (code (sg (x))) μ = u1 at hm ⊢
      generalize eqI (code (sg (sg (x)))) μ = u2 at hm ⊢
      calc H x = H x * (u0 + u1 + u2) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (sg (x))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (x)) (fun x => sg (sg (x))) (fun x => h3 x) (fun y => by show sg (sg (sg y)) = y; exact h3 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (x)) = H (sg (x)) * Ls (sg (x)); rw [hH]) _
  have q2 : sumK LibInt.intR (fun x => H x * Ls (sg (sg (x)))) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc (fun x => sg (sg (x))) (fun x => sg (x)) (fun x => h3 x) (fun y => by show sg (sg (sg y)) = y; exact h3 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg (sg (x))) = H (sg (sg (x))) * Ls (sg (sg (x))); rw [hH, hH]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, QuadDiagonal.sI_add, q1, q2]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

def cT (a b x y : Zp (2 * m + 1 + 1)) : Prop := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).mul b y) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)

def tx3 (_a b x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x))))
def ty3 (_a b x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))))

/-- **P + T stays on the curve.** -/
theorem on3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : cT m a b (tx3 m a b x y) (ty3 m a b x y) := by
  unfold tx3 ty3
  show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (iv m x) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ri  -- (+784 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold cT at hc
  generalize iv m x = i at ei ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).mul (b) (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (i) (i)))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 +  -- (+3218 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m  -- (+89 chars; full line in the Zenodo source)

theorem mul1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).mul (tx3 m a b x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 *  -- (+72 chars; full line in the Zenodo source)
  unfold tx3
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have ec : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold cT at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = c at ec ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gcb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (c) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ec]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).mul (b) (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul (j) (c)))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1  -- (+836 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gjy, gcb]; lib_ring

/-- **τ²P = (bx/y, −bx³/y²).** -/
theorem step2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : tx3 m a b (tx3 m a b x y) (ty3 m a b x y) = (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (iv m y)) ∧ ty3 m a b (tx3 m a b x y) (  -- (+228 chars; full line in the Zenodo source)
  have hX := iv_eq m hpr _ _ (mul1 m hpr a b x y hc hx hy hb)
  rw [show tx3 m a b (tx3 m a b x y) (ty3 m a b x y) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (ty3 m a b x y) ((ring (2 * m + 1 + 1)).mul (iv m (tx3 m a b x y)) (iv m (tx3 m a b x y))))) from rfl,
    show ty3 m a b (tx3 m a b x y) (ty3 m a b x y) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).mul (ty3 m a b x y) ((ring (2 * m + 1 + 1)).mul (iv m (tx3 m a b x y)) ((ring (2 * m + 1 + 1)).mul (iv m (tx3 m a b x y)) (iv m (tx3 m a b x y)))))) from rfl, hX]
  unfold ty3
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have ec : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold cT at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = c at ec ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gcb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (c) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ec]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).mul (b) (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  constructor
  · calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (i) (i)))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)  -- (+2232 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (j)) := by rw [gix, gjy, gcb]; lib_ring
  · calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (i) (i)))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1  -- (+3875 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).mul (j) (j)))) := by rw [gix, gjy, gcb]; lib_ring

theorem mul2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (iv m y))) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m  -- (+69 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have ec : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold cT at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = c at ec ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gcb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (c) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ec]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).mul (b) (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (j))) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (c))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).  -- (+524 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gjy, gcb]; lib_ring

/-- **τ³P = P.** -/
theorem step3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : tx3 m a b ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (iv m y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (  -- (+455 chars; full line in the Zenodo source)
  have hX := iv_eq m hpr _ _ (mul2 m hpr a b x y hc hx hy hb)
  unfold tx3 ty3
  rw [hX]
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have ec : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold cT at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = c at ec ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have gcb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (c) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ec]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).mul (b) (y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x)))) = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
  constructor
  · calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).mul (j) (j))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (c))) ((ring (  -- (+1670 chars; full line in the Zenodo source)
      _ = x := by rw [gix, gjy, gcb]; lib_ring
  · calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) (b)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).mul (j) (j))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m  -- (+2674 chars; full line in the Zenodo source)
      _ = y := by rw [gix, gjy, gcb]; lib_ring

def tau (a b : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))
  | none => some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)
  | some p => if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.2 p.2) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul p.1 p.2))) ((ring (2 * m + 1 + 1)).mul b p.2) = (ring (2 * m + 1 + 1)).mul p.1 ((ring (2 * m + 1 + 1)).mul p.1 p.1) then
      (if p.1 = (ring (2 * m + 1 + 1)).zero then (if p.2 = (ring (2 * m + 1 + 1)).zero then some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).neg b) else none) else some (tx3 m a b p.1 p.2, ty3 m a b p.1 p.2))
    else some p

theorem tau_some (a b x y : Zp (2 * m + 1 + 1)) : tau m a b (some (x, y)) =
    if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).mul b y) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) then
      (if x = (ring (2 * m + 1 + 1)).zero then (if y = (ring (2 * m + 1 + 1)).zero then some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).neg b) else none) else some (tx3 m a b x y, ty3 m a b x y))
    else some (x, y) := rfl

theorem tau_gen (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : tau m a b (some (x, y)) = some (tx3 m a b x y, ty3 m a b x y) := by
  unfold cT at hc
  rw [tau_some, ite_pos' _ _ hc, ite_neg' _ _ hx]

theorem tau_off (a b x y : Zp (2 * m + 1 + 1)) (hc : ¬ cT m a b x y) : tau m a b (some (x, y)) = some (x, y) := by
  unfold cT at hc
  rw [tau_some, ite_neg' _ _ hc]

theorem tau_T (a b : Zp (2 * m + 1 + 1)) : tau m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).neg b) := by
  rw [tau_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul b (ring (2 * m + 1 + 1)).zero) = (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1  -- (+106 chars; full line in the Zenodo source)
    ite_pos' _ _ rfl, ite_pos' _ _ rfl]

theorem tau_2T (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : tau m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).neg b)) = none := by
  rw [tau_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg b) ((ring (2 * m + 1 + 1)).neg b)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg b)))) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).neg b)) = (ring (2 * m + 1 + 1)).mul (ring (2  -- (+118 chars; full line in the Zenodo source)
    ite_pos' _ _ rfl, ite_neg' _ _ (neg_ne m _ hb)]

theorem gen_y (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : y ≠ (ring (2 * m + 1 + 1)).zero := fun e => hx (by
  unfold cT at hc; rw [e] at hc
  rcases mul_eq_zero m hpr _ _ (show (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x) = (ring (2 * m + 1 + 1)).zero by rw [← hc]; lib_ring) with h | h
  · exact h
  · rcases mul_eq_zero m hpr _ _ h with h' | h'
    · exact h'
    · exact h')

theorem tau_gen3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    tau m a b (tau m a b (tau m a b (some (x, y)))) = some (x, y) := by
  have hy := gen_y m hpr a b x y hc hx
  have ix := iv_nz m hpr x hx
  have iy := iv_nz m hpr y hy
  have c1 := on3 m hpr a b x y hc hx
  have n1 : tx3 m a b x y ≠ (ring (2 * m + 1 + 1)).zero := neg_ne m _ (mul_ne m hpr _ _ hb (mul_ne m hpr _ _ hy (mul_ne m hpr _ _ ix ix)))
  have s2 := step2 m hpr a b x y hc hx hy hb
  have c2 := on3 m hpr a b _ _ c1 n1
  rw [s2.1, s2.2] at c2
  have n2 := mul_ne m hpr _ _ hb (mul_ne m hpr _ _ hx iy)
  have s3 := step3 m hpr a b x y hc hx hy hb
  rw [tau_gen m a b x y hc hx, tau_gen m a b _ _ c1 n1, s2.1, s2.2, tau_gen m a b _ _ c2 n2, s3.1, s3.2]

theorem y_tors3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b y : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add y b) = (ring (2 * m + 1 + 1)).zero) :
    y = (ring (2 * m + 1 + 1)).zero ∨ y = (ring (2 * m + 1 + 1)).neg b := by
  rcases mul_eq_zero m hpr _ _ h with e | e
  · exact Or.inl e
  · exact Or.inr (by calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y b) ((ring (2 * m + 1 + 1)).neg b) := by lib_ring
      _ = _ := by rw [e]; lib_ring)

/-- **τ³ = id** on {O} ∪ 𝔽_p². -/
theorem tau3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    tau m a b (tau m a b (tau m a b P)) = P := by
  cases P with
  | none => show tau m a b (tau m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero))) = none; rw [tau_T, tau_2T m a b hb]
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).mul b y) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)
    · by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · have hy : (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add y b) = (ring (2 * m + 1 + 1)).zero := by
          rw [h0] at hc
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero y))) ((ring (2 * m + 1 + 1)).mul b y) := by lib_ring
            _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring
        rw [h0]
        rcases y_tors3 m hpr b y hy with e | e <;> rw [e]
        · rw [tau_T, tau_2T m a b hb]; rfl
        · rw [tau_2T m a b hb]; show tau m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = _; rw [tau_T]
      · exact tau_gen3 m hpr a b x y hb hc h0
    · rw [tau_off m a b x y hc, tau_off m a b x y hc, tau_off m a b x y hc]

def d27 (a b : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (  -- (+1294 chars; full line in the Zenodo source)

/-- a fixed point with x ≠ 0 forces b²(a³ − 27b) = 0. -/
theorem fixed_alg3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hc : cT m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero)
    (fx : tx3 m a b x y = x) (fy : ty3 m a b x y = y) : d27 m a b = (ring (2 * m + 1 + 1)).zero := by
  have hy := gen_y m hpr a b x y hc hx
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold tx3 at fx; unfold ty3 at fy; unfold cT at hc
  generalize iv m x = i at ei fx fy
  have gi : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gX : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)))) ((ring (2 * m + 1 + 1)).neg x) = (ring (2 * m + 1 + 1)).zero := by rw [fx]; lib_ring
  have gY : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))))) ((ring (2 * m + 1 + 1)).neg y) = (ring (2 * m + 1 + 1)).zero := by rw [fy]; lib_ring
  have g1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b y) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)))) ((ring (2 * m + 1 + 1)).neg x))))
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))))) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gX, gi]; lib_ring
  have g2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b y) ((ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).neg y)) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))))  -- (+35 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x)) ((ring (2 * m + 1 + 1)).mul i x)) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (  -- (+92 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b y)))) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [gY, gi, g1]; lib_ring
  have hyb : y = b := by
    rcases mul_eq_zero m hpr _ _ g2 with h | h
    · exact absurd h (mul_ne m hpr _ _ hb hy)
    · calc y = (ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).neg y))) := by lib_ring
        _ = b := by rw [h]; lib_ring
  rw [hyb] at g1 hc
  have g3 : (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) b)) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x b))) ((ring (2 * m + 1 + 1)).mul b b)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1))  -- (+90 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc, g1]; lib_ring
  have g4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) b) = (ring (2 * m + 1 + 1)).zero := by
    rcases mul_eq_zero m hpr _ _ g3 with h | h
    · exact absurd h hb
    · exact h
  have g5 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) (d27 m a b) = (ring (2 * m + 1 + 1)).zero := by
    unfold d27
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul b b)))
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).one))) b))
            ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a x) ((ring (2 * m + 1 + 1)).mul a x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2   -- (+417 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [g1, g4]; lib_ring
  rcases mul_eq_zero m hpr _ _ g5 with h | h
  · exact absurd h (mul_ne m hpr _ _ hb hb)
  · exact h

def onT (a b : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Int
  | none => 1
  | some p => if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.2 p.2) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul p.1 p.2))) ((ring (2 * m + 1 + 1)).mul b p.2) = (ring (2 * m + 1 + 1)).mul p.1 ((ring (2 * m + 1 + 1)).mul p.1 p.1) then 1 else 0

theorem onT_on (a b x y : Zp (2 * m + 1 + 1)) (hc : cT m a b x y) : onT m a b (some (x, y)) = 1 := by
  unfold cT at hc; show (if _ then _ else _) = _; rw [ite_pos' _ _ hc]

theorem onT_off (a b x y : Zp (2 * m + 1 + 1)) (hc : ¬ cT m a b x y) : onT m a b (some (x, y)) = 0 := by
  unfold cT at hc; show (if _ then _ else _) = _; rw [ite_neg' _ _ hc]

theorem onT_tau (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : onT m a b (tau m a b P) = onT m a b P := by
  cases P with
  | none => exact onT_on m a b _ _ (by unfold cT; lib_ring)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).mul b y) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)
    · rw [onT_on m a b x y hc]
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tau_some, ite_pos' _ _ hc, ite_pos' _ _ h0]
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [ite_pos' _ _ hy]; exact onT_on m a b _ _ (by unfold cT; lib_ring)
        · rw [ite_neg' _ _ hy]; rfl
      · rw [tau_gen m a b x y hc h0]; exact onT_on m a b _ _ (on3 m hpr a b x y hc h0)
    · rw [tau_off m a b x y hc]

theorem onT_fix (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hd : d27 m a b ≠ (ring (2 * m + 1 + 1)).zero)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : tau m a b P = P) : onT m a b P = 0 := by
  cases P with
  | none => exact absurd h (fun e => by cases e)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).mul b y) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)
    · exfalso
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tau_some, ite_pos' _ _ hc, ite_pos' _ _ h0] at h
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [ite_pos' _ _ hy] at h
          exact neg_ne m b hb ((congrArg Prod.snd (Option.some.inj h)).trans hy)
        · rw [ite_neg' _ _ hy] at h
          cases h
      · rw [tau_gen m a b x y hc h0] at h
        have e' := Option.some.inj h
        exact hd (fixed_alg3 m hpr a b x y hb hc h0 (congrArg Prod.fst e') (congrArg Prod.snd e'))
    · exact onT_off m a b x y hc

def cnt3 (a b : Zp (2 * m + 1 + 1)) (q : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : Int :=
  if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul q.2 q.2) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul q.1 q.2))) ((ring (2 * m + 1 + 1)).mul b q.2) = (ring (2 * m + 1 + 1)).mul q.1 ((ring (2 * m + 1 + 1)).mul q.1 q.1) then 1 else 0

/-- **G5 (order 3): 3 | 1 + #E_aff(𝔽_p)** for y² + axy + by = x³, b ≠ 0, a³ ≠ 27b. -/
theorem g5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hd : d27 m a b ≠ (ring (2 * m + 1 + 1)).zero) :
    ∃ k : Int, 1 + sumK LibInt.intR (cnt3 m a b) (pL m) = 3 * k := by
  have e : sumK LibInt.intR (onT m a b) (ptsL m) = 1 + sumK LibInt.intR (cnt3 m a b) (pL m) := by
    show LibInt.intR.add 1 (sumK LibInt.intR (onT m a b) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, orbit3g (ptsL m) (complete_pts m) (code m) (code_inj m) (tau m a b) (tau3 m hpr a b hb) (onT m a b) (onT_tau m hpr a b) (onT_fix m hpr a b hb hd)⟩

end G5Tate3

#print axioms G5Tate3.m3_mem
#print axioms G5Tate3.m3_le
#print axioms G5Tate3.m3_rot
#print axioms G5Tate3.one_min3
#print axioms G5Tate3.orbit3g
#print axioms G5Tate3.on3
#print axioms G5Tate3.mul1
#print axioms G5Tate3.step2
#print axioms G5Tate3.mul2
#print axioms G5Tate3.step3
#print axioms G5Tate3.tau_some
#print axioms G5Tate3.tau_gen
#print axioms G5Tate3.tau_off
#print axioms G5Tate3.tau_T
#print axioms G5Tate3.tau_2T
#print axioms G5Tate3.gen_y
#print axioms G5Tate3.tau_gen3
#print axioms G5Tate3.y_tors3
#print axioms G5Tate3.tau3
#print axioms G5Tate3.fixed_alg3
#print axioms G5Tate3.onT_on
#print axioms G5Tate3.onT_off
#print axioms G5Tate3.onT_tau
#print axioms G5Tate3.onT_fix
#print axioms G5Tate3.g5

#print axioms: 25 theorems, no axioms.

78.111 Two divides the point count of curves with a 2-torsion point

Every curve \(y^2=x^3+ax^2+bx\) with \(b\ne 0\), \(a^2\ne 4b\) has an even number of points over \(\mathbb F_p\), \(p\) odd.

\[ \tau (x,y)=\Big (\frac bx,-\frac {by}{x^2}\Big ),\ \tau ^2=\mathrm {id}\ \Longrightarrow \ 1+\#\{y^2=x^3+ax^2+bx\}\equiv 0\pmod 2. \]

lang/G5Two.lean

/-
  lang/G5Two — T43, cycle 375: 2 | #E(𝔽_p) for every curve y² = x³ + ax² + bx with b ≠ 0, a² ≠ 4b (G5, order 2).

  Translation by the 2-torsion point T = (0, 0): (x, y) ↦ (b/x, −by/x²) (x ≠ 0), (0, 0) ↦ O ↦ (0, 0); identity off the curve.
    one_min2, orbit2g   orbit counting for an involution on any listed type with an injective code
    on2, inv2, tau2     the translation stays on E and is an involution on {O} ∪ 𝔽_p²
    fix2                a fixed point (x ≠ 0) forces y = 0, b = x², 2x + a = 0, hence a² = 4b
    g2                  1 + #{(x, y) : y² = x³ + ax² + bx} = 2k        (p odd, b ≠ 0, a² ≠ 4b)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.G7Count

namespace G5Two

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map Pairs Orbit5
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

theorem one_min2 (u v : Nat) (h : u ≠ v) : eqI u (mn u v) + eqI v (mn u v) = 1 := by
  unfold eqI
  rcases mn_or u v with e | e <;> rw [e]
  · rw [ite_pos' _ _ rfl, ite_neg' _ _ (fun e' => h e'.symm)]; rfl
  · rw [ite_neg' _ _ h, ite_pos' _ _ rfl]; rfl

theorem mn_comm (u v : Nat) : mn v u = mn u v := by
  apply Nat.le_antisymm
  · rcases mn_or u v with e | e <;> rw [e]
    · exact mn_le_r _ _
    · exact mn_le_l _ _
  · rcases mn_or v u with e | e <;> rw [e]
    · exact mn_le_r _ _
    · exact mn_le_l _ _

section General
variable {G : Type} [DecidableEq G]

/-- **Orbits of an involution** on any completely listed type with an injective code. -/
theorem orbit2g (els : List G) (hc : Complete LibInt.intR els) (code : G → Nat) (hinj : ∀ u v, code u = code v → u = v)
    (sg : G → G) (h2 : ∀ x, sg (sg x) = x) (H : G → Int) (hH : ∀ x, H (sg x) = H x) (hfix : ∀ x, sg x = x → H x = 0) :
    sumK LibInt.intR H els = 2 * sumK LibInt.intR (fun x => H x * eqI (code x) (mn (code x) (code (sg x)))) els := by
  let Ls : G → Int := fun x => eqI (code x) (mn (code x) (code (sg x)))
  have hpt : ∀ x, H x = H x * Ls x + H x * Ls (sg x) := fun x => by
    by_cases hx : sg x = x
    · rw [hfix x hx]; lib_int
    · have hm := one_min2 (code x) (code (sg x)) (fun e => hx (hinj _ _ e).symm)
      have r1 : Ls (sg x) = eqI (code (sg x)) (mn (code x) (code (sg x))) := by
        show eqI _ (mn _ (code (sg (sg x)))) = _; rw [h2, mn_comm]
      rw [r1]
      show H x = H x * eqI (code x) _ + _
      generalize mn (code x) (code (sg x)) = μ at hm ⊢
      generalize eqI (code x) μ = u0 at hm ⊢; generalize eqI (code (sg x)) μ = u1 at hm ⊢
      calc H x = H x * (u0 + u1) := by rw [hm]; lib_int
        _ = _ := by lib_int
  have q1 : sumK LibInt.intR (fun x => H x * Ls (sg x)) els = sumK LibInt.intR (fun x => H x * Ls x) els := by
    refine Eq.trans ?_ (FiniteRecChain.reindex LibInt.intR els hc sg sg (fun x => h2 x) (fun y => h2 y) (fun y => H y * Ls y))
    exact sumK_ext LibInt.intR _ _ (fun x => by show H x * Ls (sg x) = H (sg x) * Ls (sg x); rw [hH]) _
  rw [sumK_ext LibInt.intR _ _ hpt, QuadDiagonal.sI_add, q1]
  generalize sumK LibInt.intR (fun x => H x * Ls x) els = T
  lib_int

end General

def c2 (a b x y : Zp (2 * m + 1 + 1)) : Prop := (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul b x)

def t2x (b x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).mul b (iv m x)
def t2y (b x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x))))

theorem on2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : c2 m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    c2 m a b (t2x m b x) (t2y m b x y) := by
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold c2 at hc ⊢; unfold t2x t2y
  generalize iv m x = i at ei
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i))))
      = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i)))) ((ring (2 * m + 1 + 1)).mul y y) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1))  -- (+24 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b b) i) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x))))
          ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul b b)) ((ring (2 * m + 1 + 1)).mul i i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x))))
          ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul i ((ring (2 * m + 1 + 1)).mul i i))))) ((ring (2 * m + 1 + 1)).mul i x)) := by lib_ring
    _ = _ := by rw [ei]; lib_ring

theorem inv2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b x y : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) :
    t2x m b (t2x m b x) = x ∧ t2y m b (t2x m b x) (t2y m b x y) = y := by
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  have hX : iv m (t2x m b x) = (ring (2 * m + 1 + 1)).mul x (iv m b) := iv_eq m hpr _ _ (by
    unfold t2x
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b (iv m x)) ((ring (2 * m + 1 + 1)).mul x (iv m b)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m x) x) ((ring (2 * m + 1 + 1)).mul (iv m b) b) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [ei, el]; lib_ring)
  rw [show t2x m b (t2x m b x) = (ring (2 * m + 1 + 1)).mul b (iv m (t2x m b x)) from rfl,
    show t2y m b (t2x m b x) (t2y m b x y) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (t2y m b x y) ((ring (2 * m + 1 + 1)).mul (iv m (t2x m b x)) (iv m (t2x m b x))))) from rfl, hX]
  unfold t2y
  generalize iv m x = i at ei
  generalize iv m b = l at el
  constructor
  · calc (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul x l) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul l b) := by lib_ring
      _ = x := by rw [el]; lib_ring
  · calc (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x l) ((ring (2 * m + 1 + 1)).mul x l))))
        = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l b) ((ring (2 * m + 1 + 1)).mul l b)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x))) := by lib_ring
      _ = y := by rw [el, ei]; lib_ring

def tau (a b : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))
  | none => some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)
  | some p => if (ring (2 * m + 1 + 1)).mul p.2 p.2 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.1 ((ring (2 * m + 1 + 1)).mul p.1 p.1)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul p.1 p.1))) ((ring (2 * m + 1 + 1)).mul b p.1) then
      (if p.1 = (ring (2 * m + 1 + 1)).zero then none else some (t2x m b p.1, t2y m b p.1 p.2))
    else some p

theorem tau_some (a b x y : Zp (2 * m + 1 + 1)) : tau m a b (some (x, y)) =
    if (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul b x) then
      (if x = (ring (2 * m + 1 + 1)).zero then none else some (t2x m b x, t2y m b x y))
    else some (x, y) := rfl

theorem tau_gen (a b x y : Zp (2 * m + 1 + 1)) (hc : c2 m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : tau m a b (some (x, y)) = some (t2x m b x, t2y m b x y) := by
  unfold c2 at hc; rw [tau_some, ite_pos' _ _ hc, ite_neg' _ _ hx]

theorem tau_off (a b x y : Zp (2 * m + 1 + 1)) (hc : ¬ c2 m a b x y) : tau m a b (some (x, y)) = some (x, y) := by
  unfold c2 at hc; rw [tau_some, ite_neg' _ _ hc]

theorem y0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (y : Zp (2 * m + 1 + 1)) (h : (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).zero) : y = (ring (2 * m + 1 + 1)).zero := by
  rcases mul_eq_zero m hpr y y h with e | e <;> exact e

theorem tau_T (a b : Zp (2 * m + 1 + 1)) : tau m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = none := by
  rw [tau_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).ze  -- (+124 chars; full line in the Zenodo source)

/-- **τ² = id.** -/
theorem tau2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    tau m a b (tau m a b P) = P := by
  cases P with
  | none => exact tau_T m a b
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul b x)
    · by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [h0] at hc ⊢
        have hy := y0 m hpr y (by rw [hc]; lib_ring)
        rw [hy, tau_T]; rfl
      · rw [tau_gen m a b x y hc h0, tau_gen m a b _ _ (on2 m hpr a b x y hc h0) (fun e => hb (by
            have ei := iv_mul_self m hpr x h0
            unfold t2x at e
            calc b = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b (iv m x)) x := by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b (iv m x)) x = (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (iv m x) x) by lib_ring, ei]; lib_ring
              _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring)), (inv2 m hpr b x y h0 hb).1, (inv2 m hpr b x y h0 hb).2]
    · rw [tau_off m a b x y hc, tau_off m a b x y hc]

def onE (a b : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Int
  | none => 1
  | some p => if (ring (2 * m + 1 + 1)).mul p.2 p.2 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul p.1 ((ring (2 * m + 1 + 1)).mul p.1 p.1)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul p.1 p.1))) ((ring (2 * m + 1 + 1)).mul b p.1) then 1 else 0

theorem onE_on (a b x y : Zp (2 * m + 1 + 1)) (hc : c2 m a b x y) : onE m a b (some (x, y)) = 1 := by
  unfold c2 at hc; show (if _ then _ else _) = _; rw [ite_pos' _ _ hc]

theorem onE_off (a b x y : Zp (2 * m + 1 + 1)) (hc : ¬ c2 m a b x y) : onE m a b (some (x, y)) = 0 := by
  unfold c2 at hc; show (if _ then _ else _) = _; rw [ite_neg' _ _ hc]

theorem onE_tau (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    onE m a b (tau m a b P) = onE m a b P := by
  cases P with
  | none => exact onE_on m a b _ _ (by unfold c2; lib_ring)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul b x)
    · rw [onE_on m a b x y hc]
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tau_some, ite_pos' _ _ hc, ite_pos' _ _ h0]; rfl
      · rw [tau_gen m a b x y hc h0]; exact onE_on m a b _ _ (on2 m hpr a b x y hc h0)
    · rw [tau_off m a b x y hc]

/-- a fixed point with x ≠ 0 forces a² = 4b. -/
theorem fix2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b x y : Zp (2 * m + 1 + 1)) (hc : c2 m a b x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero)
    (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (fx : t2x m b x = x) (fy : t2y m b x y = y) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)) = (ring (2 * m + 1 + 1)).zero := by
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold c2 at hc; unfold t2x at fx; unfold t2y at fy
  generalize iv m x = i at ei fx fy
  have hb : b = (ring (2 * m + 1 + 1)).mul x x := by
    calc b = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b i) x := by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b i) x = (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul i x) by lib_ring, ei]; lib_ring
      _ = (ring (2 * m + 1 + 1)).mul x x := by rw [fx]
  have e2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) y = (ring (2 * m + 1 + 1)).zero := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)))) y := by rw [fy]; lib_ring
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i i)))) y := by rw [hb]
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x)))) y := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).zero := by
    rcases mul_eq_zero m hpr _ _ e2 with e | e
    · exact absurd e h2
    · exact e
  rw [hy, hb] at hc
  have e3 : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) x) a)) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zer  -- (+18 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [← hc]; lib_ring
  have e4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) x) a = (ring (2 * m + 1 + 1)).zero := by
    rcases mul_eq_zero m hpr _ _ e3 with e | e
    · exact absurd e hx
    · rcases mul_eq_zero m hpr _ _ e with e' | e'
      · exact absurd e' hx
      · exact e'
  rw [hb]
  calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) x))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) x) a) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).zero := by rw [e4]; lib_ring

def cnt2 (a b : Zp (2 * m + 1 + 1)) (q : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : Int :=
  if (ring (2 * m + 1 + 1)).mul q.2 q.2 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul q.1 ((ring (2 * m + 1 + 1)).mul q.1 q.1)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul q.1 q.1))) ((ring (2 * m + 1 + 1)).mul b q.1) then 1 else 0

theorem onE_fix (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero)
    (hd : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)) ≠ (ring (2 * m + 1 + 1)).zero)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : tau m a b P = P) : onE m a b P = 0 := by
  cases P with
  | none => exact absurd h (fun e => by cases e)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul b x)
    · exfalso
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tau_some, ite_pos' _ _ hc, ite_pos' _ _ h0] at h
        cases h
      · rw [tau_gen m a b x y hc h0] at h
        have e' := Option.some.inj h
        exact hd (fix2 m hpr a b x y hc h0 h2 (congrArg Prod.fst e') (congrArg Prod.snd e'))
    · exact onE_off m a b x y hc

/-- **G5, order 2: 2 | #E(𝔽_p)** for y² = x³ + ax² + bx, b ≠ 0, a² ≠ 4b, p odd. -/
theorem g2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero)
    (hd : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) b)) ≠ (ring (2 * m + 1 + 1)).zero) :
    ∃ k : Int, 1 + sumK LibInt.intR (cnt2 m a b) (pL m) = 2 * k := by
  have e : sumK LibInt.intR (onE m a b) (ptsL m) = 1 + sumK LibInt.intR (cnt2 m a b) (pL m) := by
    show LibInt.intR.add 1 (sumK LibInt.intR (onE m a b) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, orbit2g (ptsL m) (complete_pts m) (code m) (code_inj m) (tau m a b) (tau2 m hpr a b hb)
    (onE m a b) (onE_tau m hpr a b) (onE_fix m hpr a b h2 hd)⟩

end G5Two

#print axioms G5Two.one_min2
#print axioms G5Two.mn_comm
#print axioms G5Two.orbit2g
#print axioms G5Two.on2
#print axioms G5Two.inv2
#print axioms G5Two.tau_some
#print axioms G5Two.tau_gen
#print axioms G5Two.tau_off
#print axioms G5Two.y0
#print axioms G5Two.tau_T
#print axioms G5Two.tau2
#print axioms G5Two.onE_on
#print axioms G5Two.onE_off
#print axioms G5Two.onE_tau
#print axioms G5Two.fix2
#print axioms G5Two.onE_fix
#print axioms G5Two.g2

#print axioms: 17 theorems, no axioms.

78.112 Four divides the point count with full two-torsion

For \(y^2=x(x-a)(x-b)\) with \(a,b\ne 0\), \(a\ne b\) and \(p\) odd, the translations by \((0,0)\) and \((a,0)\) generate a Klein four group acting freely on \(E\cup \{O\}\).

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2=x(x-a)(x-b)\}\equiv 0\pmod 4. \]

lang/FullTwo.lean

/-
  lang/FullTwo — T43, cycle 394: translations by the 2-torsion points of y² = x(x − a)(x − b) (4 | #E(𝔽_p) by the Klein four group of translations by 2-torsion points).

  For y² = x³ + Ax² + Bx and a shift d with x³ + Ax² + Bx = X³ + A'X² + B'X (X = x − d), the involution of lang/G5Two on the
  shifted curve, conjugated back, is translation by (d, 0):  TT d A' B' = (shift by +d) ∘ G5Two.tau A' B' ∘ (shift by −d).
    sh, ush, sh_ush, ush_sh     shifting points of {O} ∪ 𝔽_p²
    c2_shift, onE_shift         the curve and its indicator are compatible with the shift
    TT2, onE_TT, onE_TTfix      TT is an involution preserving E ∪ {O}, without fixed points on it when A'² ≠ 4B'
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G5Two
import lang.OrbitV4

namespace FullTwo

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map Pairs G5Two
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

def sh (d : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))
  | none => none
  | some p => some ((ring (2 * m + 1 + 1)).add p.1 ((ring (2 * m + 1 + 1)).neg d), p.2)

def ush (d : Zp (2 * m + 1 + 1)) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))
  | none => none
  | some p => some ((ring (2 * m + 1 + 1)).add p.1 d, p.2)

theorem sh_ush (d : Zp (2 * m + 1 + 1)) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : sh m d (ush m d P) = P := by
  cases P with
  | none => rfl
  | some p => exact congrArg some (Prod.ext (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add p.1 d) ((ring (2 * m + 1 + 1)).neg d) = p.1 by lib_ring) rfl)

theorem ush_sh (d : Zp (2 * m + 1 + 1)) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : ush m d (sh m d P) = P := by
  cases P with
  | none => rfl
  | some p => exact congrArg some (Prod.ext (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add p.1 ((ring (2 * m + 1 + 1)).neg d)) d = p.1 by lib_ring) rfl)

section Shift
variable (A B A' B' d : Zp (2 * m + 1 + 1))
  (hp : ∀ x : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul A ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul B x) =
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))))
      ((ring (2 * m + 1 + 1)).mul A' ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))))) ((ring (2 * m + 1 + 1)).mul B' ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))))

def TT (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) :=
  ush m d (tau m A' B' (sh m d P))

include hp in
theorem c2_shift (x y : Zp (2 * m + 1 + 1)) : c2 m A B x y ↔ c2 m A' B' ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) y := by
  unfold c2; rw [hp x]

include hp in
theorem onE_shift (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : onE m A B P = onE m A' B' (sh m d P) := by
  cases P with
  | none => rfl
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul A ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul B x)
    · rw [onE_on m A B x y hc]
      exact (onE_on m A' B' _ _ ((c2_shift m A B A' B' d hp x y).mp hc)).symm
    · rw [onE_off m A B x y hc]
      exact (onE_off m A' B' _ _ (fun h' => hc ((c2_shift m A B A' B' d hp x y).mpr h'))).symm

theorem TT2 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hb : B' ≠ (ring (2 * m + 1 + 1)).zero) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    TT m A' B' d (TT m A' B' d P) = P := by
  unfold TT
  rw [sh_ush, tau2 m hpr A' B' hb, ush_sh]

include hp in
theorem onE_TT (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    onE m A B (TT m A' B' d P) = onE m A B P := by
  unfold TT
  rw [onE_shift m A B A' B' d hp, sh_ush, onE_tau m hpr A' B', ← onE_shift m A B A' B' d hp]

include hp in
theorem onE_TTfix (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero)
    (hd : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul A' A') ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) B')) ≠ (ring (2 * m + 1 + 1)).zero)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : TT m A' B' d P = P) : onE m A B P = 0 := by
  rw [onE_shift m A B A' B' d hp]
  apply onE_fix m hpr A' B' h2 hd
  unfold TT at h
  calc tau m A' B' (sh m d P) = sh m d (ush m d (tau m A' B' (sh m d P))) := (sh_ush m d _).symm
    _ = sh m d P := by rw [h]

end Shift

section Legendre
variable (a b : Zp (2 * m + 1 + 1))

/-- σ: translation by (0, 0); τa: translation by (a, 0); τb: translation by (b, 0) on y² = x(x − a)(x − b). -/
def sig : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) := tau m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b))
def ta : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) := TT m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (a)) ((ring (2 * m + 1 + 1)).neg (b))) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) a
def tb : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) → Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) := TT m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (b)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg (a)))) b

theorem hpa : ∀ x : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) x) = (ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+835 chars; full line in the Zenodo source)
theorem hpb : ∀ x : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) x) = (ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+835 chars; full line in the Zenodo source)

theorem pt (p q p' q' : Zp (2 * m + 1 + 1)) (h1 : p = p') (h2 : q = q') :
    (some (p, q) : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) = some (p', q') := by rw [h1, h2]

theorem iv_neg' (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hz : z ≠ (ring (2 * m + 1 + 1)).zero) : iv m ((ring (2 * m + 1 + 1)).neg z) = (ring (2 * m + 1 + 1)).neg (iv m z) := by
  have e := iv_mul_self m hpr z hz
  exact iv_eq m hpr _ _ (by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg z) ((ring (2 * m + 1 + 1)).neg (iv m z)) = (ring (2 * m + 1 + 1)).mul (iv m z) z by lib_ring, e])

end Legendre

section Key
variable (a b : Zp (2 * m + 1 + 1))

theorem hba (hab : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b) ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).neg a) ≠ (ring (2 * m + 1 + 1)).zero := fun e => hab (by
  calc (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).neg a)) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring)

/-- generic shifted translation: for x ≠ d on the curve, TT d (x, y) = (B'/(x − d) + d, …). -/
theorem TT_gen (A B A' B' d : Zp (2 * m + 1 + 1))
    (hp : ∀ x : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul A ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul B x) =
      (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))))
        ((ring (2 * m + 1 + 1)).mul A' ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))))) ((ring (2 * m + 1 + 1)).mul B' ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))))
    (x y : Zp (2 * m + 1 + 1)) (hc : c2 m A B x y) (hx : (ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d) ≠ (ring (2 * m + 1 + 1)).zero) :
    TT m A' B' d (some (x, y)) = some ((ring (2 * m + 1 + 1)).add (t2x m B' ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d))) d, t2y m B' ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d)) y) := by
  unfold TT
  show ush m d (tau m A' B' (some ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg d), y))) = _
  rw [tau_gen m A' B' _ _ ((c2_shift m A B A' B' d hp x y).mp hc) hx]; rfl

theorem TT_T (A' B' d : Zp (2 * m + 1 + 1)) : TT m A' B' d (some (d, (ring (2 * m + 1 + 1)).zero)) = none := by
  unfold TT
  rw [show sh m d (some (d, (ring (2 * m + 1 + 1)).zero)) = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero) from pt m ((ring (2 * m + 1 + 1)).add d ((ring (2 * m + 1 + 1)).neg d)) (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero (by lib_ring) rfl, G5Two.tau_T]; rfl

theorem TT_O (A' B' d : Zp (2 * m + 1 + 1)) : TT m A' B' d none = some (d, (ring (2 * m + 1 + 1)).zero) := by
  unfold TT
  show ush m d (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = _
  exact pt m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero d) (ring (2 * m + 1 + 1)).zero d (ring (2 * m + 1 + 1)).zero (by lib_ring) rfl

theorem sig_O : sig m a b none = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero) := rfl

theorem sig_T : sig m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = none := G5Two.tau_T m _ _

theorem sig_pt (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c d' : Zp (2 * m + 1 + 1)) (hcd : (ring (2 * m + 1 + 1)).mul c d' = (ring (2 * m + 1 + 1)).mul a b) (hc0 : c ≠ (ring (2 * m + 1 + 1)).zero)
    (hcurve : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).zero) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (c) ((ring (2 * m + 1 + 1)).mul (c) (c))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (c) (c)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 *  -- (+130 chars; full line in the Zenodo source)
  unfold sig
  rw [tau_gen m _ _ _ _ hcurve hc0]
  unfold t2x t2y
  have e := iv_mul_self m hpr c hc0
  exact pt m _ _ _ _ (by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) (iv m c) = (ring (2 * m + 1 + 1)).mul d' ((ring (2 * m + 1 + 1)).mul (iv m c) c) := by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) (iv m c) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul c d') (iv m c) by rw [hcd]]; lib_ring
      _ = d' := by rw [e]; lib_ring) (by lib_ring)

theorem TT_pt (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (A B A' B' dd : Zp (2 * m + 1 + 1))
    (hp : ∀ x : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul A ((ring (2 * m + 1 + 1)).mul x x))) ((ring (2 * m + 1 + 1)).mul B x) =
      (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg dd)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg dd)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg dd))))
        ((ring (2 * m + 1 + 1)).mul A' ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg dd)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg dd))))) ((ring (2 * m + 1 + 1)).mul B' ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg dd))))
    (c d' : Zp (2 * m + 1 + 1)) (hc : c2 m A B c (ring (2 * m + 1 + 1)).zero) (hx : (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd) ≠ (ring (2 * m + 1 + 1)).zero)
    (hv : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add d' ((ring (2 * m + 1 + 1)).neg dd)) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd)) = B') :
    TT m A' B' dd (some (c, (ring (2 * m + 1 + 1)).zero)) = some (d', (ring (2 * m + 1 + 1)).zero) := by
  rw [TT_gen m A B A' B' dd hp c _ hc hx]
  unfold t2x t2y
  have e := iv_mul_self m hpr _ hx
  exact pt m _ _ _ _ (by
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul B' (iv m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd)))) dd
        = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add d' ((ring (2 * m + 1 + 1)).neg dd)) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd))) (iv m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd)))) dd := by rw [hv]
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add d' ((ring (2 * m + 1 + 1)).neg dd)) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd))) ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg dd)))) dd := by lib_ring
      _ = d' := by rw [e]; lib_ring) (by lib_ring)

end Key

section Key2
variable (a b : Zp (2 * m + 1 + 1))

theorem key_gen (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (x y : Zp (2 * m + 1 + 1)) (hc : c2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b)) x y)
    (hxa : (ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a) ≠ (ring (2 * m + 1 + 1)).zero) (hxb : (ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b) ≠ (ring (2 * m + 1 + 1)).zero) :
    sig m a b (ta m a b (some (x, y))) = tb m a b (some (x, y)) := by
  unfold ta tb
  rw [TT_gen m _ _ _ _ a (hpa m a b) x y hc hxa, TT_gen m _ _ _ _ b (hpb m a b) x y hc hxb]
  have hcs : c2 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (a)) ((ring (2 * m + 1 + 1)).neg (b))) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)) y := (c2_shift m _ _ _ _ a (hpa m a b)   -- (+10 chars; full line in the Zenodo source)
  have hon := on2 m hpr _ _ _ _ hcs hxa
  have hcX : c2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).add (t2x m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a))) a) (t2y m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (  -- (+97 chars; full line in the Zenodo source)
    apply (c2_shift m _ _ _ _ a (hpa m a b) _ _).mpr
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (t2x m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a))) a) ((ring (2 * m + 1 + 1)).neg a) = t2x m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)  -- (+63 chars; full line in the Zenodo source)
  have e1 := iv_mul_self m hpr _ hxa
  have e2 := iv_mul_self m hpr _ hxb
  have ea := iv_mul_self m hpr _ ha
  have hXv : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (t2x m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a))) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)) ((ring (2 * m + 1 + 1)).mul (iv m a) (iv m ((ring (2 * m + 1 + 1)).add  -- (+72 chars; full line in the Zenodo source)
    unfold t2x
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)).mul (iv m a) (iv m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b))))) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)))   -- (+307 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m a) a) ((ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b))) := by rw [e1]; lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [ea, e2]; lib_ring
  have hX0 : (ring (2 * m + 1 + 1)).add (t2x m ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b)))) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a))) a ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
    rw [e] at hXv
    exact PowerSum.one_ne_zero' (2 * m + 1) (by rw [← hXv]; lib_ring)
  have hiX := iv_eq m hpr _ _ hXv
  unfold sig
  rw [tau_gen m _ _ _ _ hcX hX0]
  unfold t2x t2y
  unfold t2x at hiX
  rw [hiX]
  generalize iv m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)) = i1 at e1 ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b)) = i2 at e2 ⊢
  generalize iv m a = ia at ea ⊢
  exact pt m _ _ _ _
    (by calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)) ((ring (2 * m + 1 + 1)).mul ia i2)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)) i2)) ((ring (2 * m + 1 + 1)).mul ia a) := by lib  -- (+5 chars; full line in the Zenodo source)
          _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg (a)))) i2) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul i2 ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b)))) := by rw [ea]; lib_ring
          _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg (a)))) i2) b := by rw [e2]; lib_ring)
    (by calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b)) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i2 i2)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ia a) ((ring (2 * m + 1 + 1)).mul ia a)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +  -- (+189 chars; full line in the Zenodo source)
          _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg (a)))) ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul i2 i2))) := by rw [ea, e1]; lib_ring)

end Key2

section Key3
variable (a b : Zp (2 * m + 1 + 1))

theorem key (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hab : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b) ≠ (ring (2 * m + 1 + 1)).zero)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : sig m a b (ta m a b P) = tb m a b P := by
  have hca : c2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b)) a (ring (2 * m + 1 + 1)).zero := by unfold c2; lib_ring
  have hcb : c2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b)) b (ring (2 * m + 1 + 1)).zero := by unfold c2; lib_ring
  have hc0 : c2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b)) (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero := by unfold c2; lib_ring
  have na : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a) ≠ (ring (2 * m + 1 + 1)).zero := fun e => ha (by
    calc a = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg a)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring)
  have nb : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg b) ≠ (ring (2 * m + 1 + 1)).zero := fun e => hb (by
    calc b = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg b)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [e]; lib_ring)
  have hba' := hba m a b hab
  -- the three points of order 2 and O
  have taO : ta m a b none = some (a, (ring (2 * m + 1 + 1)).zero) := TT_O m _ _ a
  have tbO : tb m a b none = some (b, (ring (2 * m + 1 + 1)).zero) := TT_O m _ _ b
  have ta0 : ta m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = some (b, (ring (2 * m + 1 + 1)).zero) :=
    TT_pt m hpr _ _ _ _ a (hpa m a b) (ring (2 * m + 1 + 1)).zero b hc0 na (by lib_ring)
  have tb0 : tb m a b (some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero)) = some (a, (ring (2 * m + 1 + 1)).zero) :=
    TT_pt m hpr _ _ _ _ b (hpb m a b) (ring (2 * m + 1 + 1)).zero a hc0 nb (by lib_ring)
  have taa : ta m a b (some (a, (ring (2 * m + 1 + 1)).zero)) = none := TT_T m _ _ a
  have tbb : tb m a b (some (b, (ring (2 * m + 1 + 1)).zero)) = none := TT_T m _ _ b
  have tab : ta m a b (some (b, (ring (2 * m + 1 + 1)).zero)) = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero) :=
    TT_pt m hpr _ _ _ _ a (hpa m a b) b (ring (2 * m + 1 + 1)).zero hcb hba' (by lib_ring)
  have tba : tb m a b (some (a, (ring (2 * m + 1 + 1)).zero)) = some ((ring (2 * m + 1 + 1)).zero, (ring (2 * m + 1 + 1)).zero) :=
    TT_pt m hpr _ _ _ _ b (hpb m a b) a (ring (2 * m + 1 + 1)).zero hca hab (by lib_ring)
  have sa : sig m a b (some (a, (ring (2 * m + 1 + 1)).zero)) = some (b, (ring (2 * m + 1 + 1)).zero) := sig_pt m a b hpr a b (by lib_ring) ha hca
  have sb : sig m a b (some (b, (ring (2 * m + 1 + 1)).zero)) = some (a, (ring (2 * m + 1 + 1)).zero) := sig_pt m a b hpr b a (by lib_ring) hb hcb
  cases P with
  | none => rw [taO, sa, tbO]
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).mul y y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (x) (x)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) (x))
    · by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [h0] at hc ⊢
        have hy := y0 m hpr y (by rw [hc]; lib_ring)
        rw [hy, ta0, sb, tb0]
      · by_cases hxa : x = a
        · rw [hxa] at hc ⊢
          have hy := y0 m hpr y (by rw [hc]; lib_ring)
          rw [hy, taa, sig_O, tba]
        · by_cases hxb : x = b
          · rw [hxb] at hc ⊢
            have hy := y0 m hpr y (by rw [hc]; lib_ring)
            rw [hy, tab, sig_T, tbb]
          · exact key_gen m a b hpr ha x y hc (G7LF.sub_ne m x a hxa) (G7LF.sub_ne m x b hxb)
    · have hoff : ¬ c2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b)) x y := hc
      have ta_off : ta m a b (some (x, y)) = some (x, y) := by
        unfold ta TT
        show ush m a (tau m _ _ (some ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a), y))) = _
        rw [tau_off m _ _ _ _ (fun h' => hoff ((c2_shift m _ _ _ _ a (hpa m a b) x y).mpr h'))]
        exact pt m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg a)) a) y x y (by lib_ring) rfl
      have tb_off : tb m a b (some (x, y)) = some (x, y) := by
        unfold tb TT
        show ush m b (tau m _ _ (some ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b), y))) = _
        rw [tau_off m _ _ _ _ (fun h' => hoff ((c2_shift m _ _ _ _ b (hpb m a b) x y).mpr h'))]
        exact pt m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg b)) b) y x y (by lib_ring) rfl
      rw [ta_off, tb_off]
      unfold sig
      exact tau_off m _ _ x y hoff

end Key3

section Count
variable (a b : Zp (2 * m + 1 + 1))

theorem comm (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hab : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b) ≠ (ring (2 * m + 1 + 1)).zero)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : sig m a b (ta m a b P) = ta m a b (sig m a b P) := by
  have hB : (ring (2 * m + 1 + 1)).mul (a) (b) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ ha hb
  have hBa : (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b))) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ ha hab
  have hBb : (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg (a))) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ hb (hba m a b hab)
  have k1 := key m a b hpr ha hb hab (ta m a b (sig m a b P))
  unfold ta at k1
  rw [TT2 m _ _ a hpr hBa] at k1
  have s2 : sig m a b (sig m a b P) = P := tau2 m hpr _ _ hB P
  rw [s2] at k1
  -- k1 : P = tb (ta (sig P))
  rw [key m a b hpr ha hb hab P]
  calc tb m a b P = tb m a b (tb m a b (ta m a b (sig m a b P))) := by
        show tb m a b P = tb m a b (tb m a b (TT m _ _ a (sig m a b P))); rw [← k1]
    _ = ta m a b (sig m a b P) := TT2 m _ _ b hpr hBb _

/-- **4 | #E(𝔽_p)** for y² = x(x − a)(x − b), a, b ≠ 0, a ≠ b, p odd. -/
theorem g4full (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hab : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b) ≠ (ring (2 * m + 1 + 1)).zero)
    (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) :
    ∃ k : Int, 1 + sumK LibInt.intR (cnt2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b))) (pL m) = 4 * k := by
  have hB : (ring (2 * m + 1 + 1)).mul (a) (b) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ ha hb
  have hBa : (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b))) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ ha hab
  have hd0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (rin  -- (+126 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (rin  -- (+256 chars; full line in the Zenodo source)
    exact mul_ne m hpr _ _ hab hab
  have hda : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (a)) ((ring (2 * m + 1 + 1)).neg (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (a)) ((  -- (+420 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (a)) ((ring (2 * m + 1 + 1)).neg (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (a)) ((  -- (+460 chars; full line in the Zenodo source)
  have hdb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (b)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (b)) ((  -- (+420 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (b)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (b)) ((  -- (+460 chars; full line in the Zenodo source)
  have e : sumK LibInt.intR (onE m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b))) (ptsL m) = 1 + sumK LibInt.intR (cnt2 m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b))) (pL m) := by
    show LibInt.intR.add 1 (sumK LibInt.intR (onE m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b))) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, OrbitV4.orbitV4 (ptsL m) (complete_pts m) (code m) (code_inj m) (sig m a b) (ta m a b)
    (fun P => tau2 m hpr _ _ hB P) (fun P => TT2 m _ _ a hpr hBa P) (fun P => comm m a b hpr ha hb hab P)
    (onE m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (a) (b))) ((ring (2 * m + 1 + 1)).mul (a) (b))) (fun P => onE_tau m hpr _ _ P) (fun P => onE_TT m _ _ _ _ a (hpa m a b) hpr P)
    (fun P h => by
      rcases h with h | h | h
      · exact onE_fix m hpr _ _ h2 hd0 P h
      · exact onE_TTfix m _ _ _ _ a (hpa m a b) hpr h2 hda P h
      · rw [key m a b hpr ha hb hab P] at h
        exact onE_TTfix m _ _ _ _ b (hpb m a b) hpr h2 hdb P h)⟩

end Count

end FullTwo

#print axioms FullTwo.sh_ush
#print axioms FullTwo.ush_sh
#print axioms FullTwo.c2_shift
#print axioms FullTwo.onE_shift
#print axioms FullTwo.TT2
#print axioms FullTwo.onE_TT
#print axioms FullTwo.onE_TTfix
#print axioms FullTwo.hpa
#print axioms FullTwo.hpb
#print axioms FullTwo.pt
#print axioms FullTwo.iv_neg'
#print axioms FullTwo.hba
#print axioms FullTwo.TT_gen
#print axioms FullTwo.TT_T
#print axioms FullTwo.TT_O
#print axioms FullTwo.sig_O
#print axioms FullTwo.sig_T
#print axioms FullTwo.sig_pt
#print axioms FullTwo.TT_pt
#print axioms FullTwo.key_gen
#print axioms FullTwo.key
#print axioms FullTwo.comm
#print axioms FullTwo.g4full

#print axioms: 23 theorems, no axioms.

78.113 Primes congruent to six mod seven

Every prime factor of \(f(n)=n^3+n^2-2n-1\) is \(7\) or \(\equiv \pm 1\pmod 7\), and \(f(7\cdot B!)\equiv 6\pmod 7\).

\[ \forall B\ \exists b:\ 7b+6 \text { prime},\ B<7b+6. \]

lang/Seven7c.lean

/-
  lang/Seven7c — T43, cycle 397: **infinitely many primes p ≡ 6 (mod 7)** (Euclid-type, via f(X) = X³ + X² − 2X − 1).

  For j = 7·F + 5 with B! = F + 1, n = j + 2 = 7·B! and N₀ = f(n) = j³ + 7j² + 14j + 7 ≡ 6 (mod 7). Every prime factor q of N₀
  is odd, and f has the root n mod q, so (Seven7b.root7) 7 | q − 1, 7 | q + 1 or q = 7; q = 7 is impossible (N₀ ≡ 6), so
  q ≡ ±1 (mod 7). A product of primes ≡ 1 is ≡ 1, so N₀ has a prime factor ≡ 6; it does not divide n, hence exceeds B.
    uniq_rem7, mod7, cofactor7   residues mod 7
    seven_zero                   7 = 0 in 𝔽_q (q = 2m + 3) ⟹ q = 7
    allowed7                     q prime, q | N, N + (2n + 1) = n³ + n²  ⟹  q ≡ 1, q ≡ 6 (mod 7) or q = 7
    has6                         a prime factor ≡ 6 (mod 7) of every divisor N ≡ 6 of N₀ ≡ 6
    inf_6mod7                    ∀ B, ∃ prime p = 7b + 6 > B
  Lean 4 core only, proofs by hand (lib_ring, lib_nat, lib_int), no axioms.
-/
import lang.Seven7b
import lang.Fp2Sqrt
import lang.PrimeOneModQ
import lang.TwoPower
import lang.Inf7mod8

namespace Seven

open FTAChain ZMod LibInt InfSplit

theorem uniq_rem7 (x y r c : Nat) (hr : r < 7) (hc : c < 7) (h : x * 7 + r = y * 7 + c) : r = c := by
  have big : ∀ x y r c : Nat, r < 7 → x < y → x * 7 + r = y * 7 + c → False := fun x y r c hr hxy h => by
    have h1 : x * 7 + r < (x + 1) * 7 := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * 7 ≤ y * 7 := Nat.mul_le_mul_right 7 hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * 7 + Int.ofNat r = Int.ofNat x * 7 + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * 7 + Int.ofNat r) - Int.ofNat x * 7 := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

theorem mod7 (n : Nat) : ∃ a r, n = a * 7 + r ∧ r < 7 :=
  match div_exists 6 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (7 : Nat) = 6 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor7 (b N' a : Nat) (h : (b * 7 + 1) * N' = a * 7 + 6) : ∃ c, N' = c * 7 + 6 :=
  match mod7 N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 7 + 1) * N' = (b * 7 * c + b * r + c) * 7 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_rem7 _ _ r 6 hr (Nat.lt_succ_self 6) h]⟩

theorem seven_zero (m : Nat) (h : seven m = (ring (2 * m + 1 + 1)).zero) : 2 * m + 1 + 1 + 1 = 7 := by
  have e : seven m = mk (2 * m + 1 + 1) 7 := by
    rw [show mk (2 * m + 1 + 1) 7 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) (  -- (+56 chars; full line in the Zenodo source)
      rw [show (ring (2 * m + 1 + 1)).zero = mk (2 * m + 1 + 1) 0 from rfl, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one,
        TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one]]
    unfold seven; lib_ring
  rw [e] at h
  have h0 : md (2 * m + 1 + 1) 7 = 0 := congrArg Subtype.val h
  match m, h0 with
  | 0, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 1, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 2, _ => rfl
  | k + 3, h0 =>
    rw [md_small _ 7 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 7) (by
      show 8 ≤ 2 * (k + 3) + 1 + 1 + 1
      exact Nat.le_trans (Nat.le_add_left 8 (2 * k + 1)) (Nat.le_of_eq (by lib_nat))))] at h0
    exact Nat.noConfusion h0

theorem allowed7 (n N l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hN : N + (n + n + 1) = n * n * n + n * n) (hdv : Dv (l + 1 + 1) N) :
    (∃ a, l + 1 + 1 = a * 7 + 1) ∨ (∃ a, l + 1 + 1 = a * 7 + 6) ∨ l + 1 + 1 = 7 := by
  rcases EveryPrime.nat_even_odd l with ⟨j, hj⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨j + 1, by rw [hj]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    obtain ⟨r, hr⟩ := hdv
    rw [hr] at hN
    rcases EveryPrime.nat_even_odd n with ⟨i, hi⟩ | ⟨i, hi⟩
    · rw [hi] at hN
      exact odd_not_dv2 (r + 2 * i) (4 * i * i * i + 2 * i * i) (by rw [show (4 * i * i * i + 2 * i * i) * 2 = 2 * i * (2 * i) * (2 * i) + 2 * i * (2 * i) by lib_nat, ← hN]; lib_nat)
    · rw [hi] at hN
      exact odd_not_dv2 (r + 2 * i + 1) ((2 * i + 1) * (2 * i + 1) * (i + 1)) (by rw [show (2 * i + 1) * (2 * i + 1) * (i + 1) * 2 = (2 * i + 1) * (2 * i + 1) * (2 * i + 1) + (2 * i + 1) * (2 * i + 1) by lib_nat, ← hN]; lib_nat)
  · subst hm
    have hx : Seven.fr m (mk (2 * m + 1 + 1) n) = (ring (2 * m + 1 + 1)).zero := by
      have h1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (mk (2 * m + 1 + 1) n) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) ((ring (2 * m + 1 + 1)).mul (mk (2 * m + 1 + 1) n) (mk (2 * m + 1 + 1) n)) = mk (2 * m + 1 + 1) (n * n * n + n * n) := by
        rw [TwoPower.mk_mul, TwoPower.mk_mul, PrimeOneModQ.mk_add]
      have h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (mk (2 * m + 1 + 1) n) (mk (2 * m + 1 + 1) n)) (ring (2 * m + 1 + 1)).one = mk (2 * m + 1 + 1) (n + n + 1) := by
        rw [PrimeOneModQ.mk_add, TwoPower.mk_add_one]
      have h0 : mk (2 * m + 1 + 1) N = (ring (2 * m + 1 + 1)).zero := Subtype.ext (by
        show md (2 * m + 1 + 1) N = md (2 * m + 1 + 1) 0
        rw [dv_md_zero _ N hdv]; rfl)
      have h3 : (ring (2 * m + 1 + 1)).add (mk (2 * m + 1 + 1) N) (mk (2 * m + 1 + 1) (n + n + 1)) = mk (2 * m + 1 + 1) (n * n * n + n * n) := by
        rw [PrimeOneModQ.mk_add, hN]
      rw [h0, ← h1, ← h2] at h3
      unfold Seven.fr
      generalize mk (2 * m + 1 + 1) n = x at h3
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x x) (ring (2 * m + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x x) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x x) (ring (2 * m + 1 + 1)).one)) := by rw [h3]
        _ = (ring (2 * m + 1 + 1)).zero := by lib_ring
    rcases root7 m hq (Fp2Sqrt.two_ne m) _ hx with ⟨a, ha⟩ | ⟨a, ha⟩ | h7
    · exact Or.inl ⟨a, by rw [← ha]⟩
    · match a, ha with
      | 0, ha => exact absurd ha (fun h' => Nat.noConfusion h')
      | a + 1, ha => exact Or.inr (Or.inl ⟨a, Nat.succ.inj (by show 2 * m + 1 + 1 + 1 + 1 = a * 7 + 6 + 1; rw [ha, Nat.succ_mul])⟩)
    · exact Or.inr (Or.inr (seven_zero m h7))

/-- **A prime factor ≡ 6 (mod 7).** -/
theorem has6 (n N0 : Nat) (hN : N0 + (n + n + 1) = n * n * n + n * n) (h6 : ∃ c, N0 = c * 7 + 6) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ a, N = a * 7 + 6) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 7 + 6) ∧ Dv q N
  | 0, N, hN', _, ⟨a, ha⟩ => absurd (ha ▸ hN') (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hNG, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 7 + 4) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    have hlN0 := dv_trans _ _ _ hlN hN0
    rcases allowed7 n N0 l hq hN hlN0 with ⟨b, hb⟩ | ⟨b, hb⟩ | h7
    · match hlN with
      | ⟨N', hN'⟩ =>
      match cofactor7 b N' a (by rw [← hb, ← ha, hN']; lib_nat) with
      | ⟨c, hc⟩ =>
      have hlt : N' ≤ G := by
        have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
        have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
        exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hNG))
      have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
      match has6 n N0 hN h6 G N' hlt hN'0 ⟨c, hc⟩ with
      | ⟨q, hq', hq6, hqN⟩ => exact ⟨q, hq', hq6, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l + 1 + 1, hq, ⟨b, hb⟩, hlN⟩
    · exfalso
      obtain ⟨c, hc⟩ := h6
      obtain ⟨r, hr⟩ := hlN0
      rw [h7] at hr
      exact absurd (uniq_rem7 c r 6 0 (Nat.lt_succ_self 6) (Nat.lt_of_lt_of_le (Nat.zero_lt_succ 6) (Nat.le_refl 7))
        (by rw [← hc, hr]; rfl)) (fun h' => Nat.noConfusion h')

/-- **Infinitely many primes p ≡ 6 (mod 7).** -/
theorem inf_6mod7 (B : Nat) : ∃ b, (∀ d, Dv d (b * 7 + 6) → d = 1 ∨ d = b * 7 + 6) ∧ B < b * 7 + 6 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  have hN : (7 * f + 5) * (7 * f + 5) * (7 * f + 5) + 7 * ((7 * f + 5) * (7 * f + 5)) + 14 * (7 * f + 5) + 7
      + ((7 * f + 5 + 2) + (7 * f + 5 + 2) + 1) = (7 * f + 5 + 2) * (7 * f + 5 + 2) * (7 * f + 5 + 2) + (7 * f + 5 + 2) * (7 * f + 5 + 2) := by
    generalize 7 * f + 5 = j; lib_nat
  have h6 : ∃ c, (7 * f + 5) * (7 * f + 5) * (7 * f + 5) + 7 * ((7 * f + 5) * (7 * f + 5)) + 14 * (7 * f + 5) + 7 = c * 7 + 6 := by
    have e1 : (7 * f + 5) * (7 * f + 5) = 7 * (7 * f * f + 10 * f + 3) + 4 := by lib_nat
    have e2 : (7 * f + 5) * (7 * f + 5) * (7 * f + 5) = 7 * (7 * (7 * f * f + 10 * f + 3) * f + 5 * (7 * f * f + 10 * f + 3) + 4 * f + 2) + 6 := by
      rw [e1]; generalize 7 * f * f + 10 * f + 3 = u; lib_nat
    rw [e2]
    exact ⟨7 * (7 * f * f + 10 * f + 3) * f + 5 * (7 * f * f + 10 * f + 3) + 4 * f + 2 + (7 * f + 5) * (7 * f + 5) + 2 * (7 * f + 5) + 1, by
      generalize 7 * (7 * f * f + 10 * f + 3) * f + 5 * (7 * f * f + 10 * f + 3) + 4 * f + 2 = v
      generalize 7 * f + 5 = j; lib_nat⟩
  obtain ⟨N0, hN0⟩ : ∃ N0, N0 = (7 * f + 5) * (7 * f + 5) * (7 * f + 5) + 7 * ((7 * f + 5) * (7 * f + 5)) + 14 * (7 * f + 5) + 7 := ⟨_, rfl⟩
  rw [← hN0] at hN h6
  match h6 with
  | ⟨c, hc⟩ =>
  match has6 (7 * f + 5 + 2) N0 hN ⟨c, hc⟩ N0 N0 (Nat.le_refl _) (dv_refl _) ⟨c, hc⟩ with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      match dv_fac B q (by rw [hb]; exact Nat.le_add_left 1 _) h with
      | ⟨r, hr⟩ =>
        have hn : Dv q (7 * f + 5 + 2) := ⟨7 * r, by
          rw [show 7 * f + 5 + 2 = 7 * (f + 1) by lib_nat, ← hf, hr]; lib_nat⟩
        obtain ⟨s, hs⟩ := hn
        have hq1 : q = (b * 7 + 5) + 1 := hb
        rw [hq1] at hqN hs
        have hsum : Dv (b * 7 + 5 + 1) (N0 + ((7 * f + 5 + 2) + (7 * f + 5 + 2) + 1)) := by
          rw [hN, hs]; exact ⟨s * (b * 7 + 5 + 1) * s * (b * 7 + 5 + 1) * s + s * (b * 7 + 5 + 1) * s, by generalize b * 7 + 5 + 1 = q; lib_nat⟩
        have h1 := dv_sub (b * 7 + 5) _ _ hqN hsum
        have h2 : Dv (b * 7 + 5 + 1) ((7 * f + 5 + 2) + (7 * f + 5 + 2)) := ⟨s + s, by rw [hs]; lib_nat⟩
        exact dv_one_absurd (b * 7 + 4) (dv_sub (b * 7 + 5) _ 1 h2 h1)

end Seven

#print axioms Seven.uniq_rem7
#print axioms Seven.mod7
#print axioms Seven.cofactor7
#print axioms Seven.seven_zero
#print axioms Seven.allowed7
#print axioms Seven.has6
#print axioms Seven.inf_6mod7

#print axioms: 7 theorems, no axioms.

78.114 Primes congruent to eight mod nine

Every prime factor of \(g(n)=n^3-3n-1\) is \(3\) or \(\equiv \pm 1\pmod 9\), and \(g(9\cdot B!)\equiv 8\pmod 9\).

\[ \forall B\ \exists b:\ 9b+8 \text { prime},\ B<9b+8. \]

lang/Nine9c.lean

/-
  lang/Nine9c — T43, cycle 398: **infinitely many primes p ≡ 8 (mod 9)** (Euclid-type, via f(X) = X³ − 3X + 1).

  g(X) = −f(−X) = X³ − 3X − 1. For j = 9·F + 7 with B! = F + 1, n = j + 2 = 9·B! and N₀ = g(n) = j³ + 6j² + 9j + 1 ≡ 8 (mod 9).
  Every prime factor q of N₀ is odd and −n is a root of f mod q, so (Nine9b.root9) 9 | q − 1, 9 | q + 1 or q = 3; q = 3 is
  impossible (N₀ ≡ 2 mod 3). A product of primes ≡ 1 (mod 9) is ≡ 1, so N₀ has a prime factor ≡ 8; it does not divide n.
    uniq_rem, mod9, cofactor9   residues
    three_zero                  3 = 0 in 𝔽_q (q = 2m + 3) ⟹ q = 3
    allowed9                    q prime, q | N, N + (3n + 1) = n³  ⟹  q ≡ 1, q ≡ 8 (mod 9) or q = 3
    has8                        a prime factor ≡ 8 (mod 9) of every divisor N ≡ 8 of N₀ ≡ 8
    inf_8mod9                   ∀ B, ∃ prime p = 9b + 8 > B
  Lean 4 core only, proofs by hand (lib_ring, lib_nat, lib_int), no axioms.
-/
import lang.Nine9b
import lang.Fp2Sqrt
import lang.PrimeOneModQ
import lang.TwoPower
import lang.Inf7mod8

namespace Nine

open FTAChain ZMod LibInt InfSplit

theorem uniq_rem (d x y r c : Nat) (hr : r < d) (hc : c < d) (h : x * d + r = y * d + c) : r = c := by
  have big : ∀ x y r c : Nat, r < d → x < y → x * d + r = y * d + c → False := fun x y r c hr hxy h => by
    have h1 : x * d + r < (x + 1) * d := by rw [Nat.succ_mul]; exact Nat.add_lt_add_left hr _
    have h2 : (x + 1) * d ≤ y * d := Nat.mul_le_mul_right d hxy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (h ▸ h1) (Nat.le_trans h2 (Nat.le_add_right _ _)))
  cases Nat.lt_or_ge x y with
  | inl hxy => exact absurd h (fun h' => big x y r c hr hxy h')
  | inr hxy =>
    cases Nat.lt_or_ge y x with
    | inl hyx => exact absurd h.symm (fun h' => big y x c r hc hyx h')
    | inr hyx =>
      have e : x = y := Nat.le_antisymm hyx hxy
      subst e
      apply Int.ofNat.inj
      have := congrArg Int.ofNat h
      have e' : Int.ofNat x * Int.ofNat d + Int.ofNat r = Int.ofNat x * Int.ofNat d + Int.ofNat c := this
      calc Int.ofNat r = (Int.ofNat x * Int.ofNat d + Int.ofNat r) - Int.ofNat x * Int.ofNat d := by lib_int
        _ = Int.ofNat c := by rw [e']; lib_int

theorem mod9 (n : Nat) : ∃ a r, n = a * 9 + r ∧ r < 9 :=
  match div_exists 8 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (9 : Nat) = 8 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor9 (b N' a : Nat) (h : (b * 9 + 1) * N' = a * 9 + 8) : ∃ c, N' = c * 9 + 8 :=
  match mod9 N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 9 + 1) * N' = (b * 9 * c + b * r + c) * 9 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, uniq_rem 9 _ _ r 8 hr (Nat.lt_succ_self 8) h]⟩

theorem three_zero (m : Nat) (h : three m = (ring (2 * m + 1 + 1)).zero) : 2 * m + 1 + 1 + 1 = 3 := by
  have e : three m = mk (2 * m + 1 + 1) 3 := by
    rw [show mk (2 * m + 1 + 1) 3 = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one by
      rw [show (ring (2 * m + 1 + 1)).zero = mk (2 * m + 1 + 1) 0 from rfl, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one]]
    unfold three; lib_ring
  rw [e] at h
  have h0 : md (2 * m + 1 + 1) 3 = 0 := congrArg Subtype.val h
  match m, h0 with
  | 0, _ => rfl
  | k + 1, h0 =>
    rw [md_small _ 3 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (by
      show 4 ≤ 2 * (k + 1) + 1 + 1 + 1
      exact Nat.le_trans (Nat.le_add_left 4 (2 * k + 1)) (Nat.le_of_eq (by lib_nat))))] at h0
    exact Nat.noConfusion h0

theorem allowed9 (n N l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hN : N + (n + n + n + 1) = n * n * n) (hdv : Dv (l + 1 + 1) N) :
    (∃ a, l + 1 + 1 = a * 9 + 1) ∨ (∃ a, l + 1 + 1 = a * 9 + 8) ∨ l + 1 + 1 = 3 := by
  rcases EveryPrime.nat_even_odd l with ⟨j, hj⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨j + 1, by rw [hj]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    obtain ⟨r, hr⟩ := hdv
    rw [hr] at hN
    rcases EveryPrime.nat_even_odd n with ⟨i, hi⟩ | ⟨i, hi⟩
    · rw [hi] at hN
      exact odd_not_dv2 (r + 3 * i) (4 * i * i * i) (by rw [show (4 * i * i * i) * 2 = 2 * i * (2 * i) * (2 * i) by lib_nat, ← hN]; lib_nat)
    · rw [hi] at hN
      exact odd_not_dv2 (4 * i * i * i + 6 * i * i + 3 * i) (r + 3 * i + 2) (by
        rw [show 2 * (4 * i * i * i + 6 * i * i + 3 * i) + 1 = (2 * i + 1) * (2 * i + 1) * (2 * i + 1) by lib_nat, ← hN]; lib_nat)
  · subst hm
    have hx : Nine.fr m ((ring (2 * m + 1 + 1)).neg (mk (2 * m + 1 + 1) n)) = (ring (2 * m + 1 + 1)).zero := by
      have h1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (mk (2 * m + 1 + 1) n) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n) = mk (2 * m + 1 + 1) (n * n * n) := by
        rw [TwoPower.mk_mul, TwoPower.mk_mul]
      have h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (mk (2 * m + 1 + 1) n) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) (ring (2 * m + 1 + 1)).one = mk (2 * m + 1 + 1) (n + n + n + 1) := by
        rw [PrimeOneModQ.mk_add, PrimeOneModQ.mk_add, TwoPower.mk_add_one]
      have h0 : mk (2 * m + 1 + 1) N = (ring (2 * m + 1 + 1)).zero := Subtype.ext (by
        show md (2 * m + 1 + 1) N = md (2 * m + 1 + 1) 0
        rw [dv_md_zero _ N hdv]; rfl)
      have h3 : (ring (2 * m + 1 + 1)).add (mk (2 * m + 1 + 1) N) (mk (2 * m + 1 + 1) (n + n + n + 1)) = mk (2 * m + 1 + 1) (n * n * n) := by
        rw [PrimeOneModQ.mk_add, hN]
      rw [h0, ← h1, ← h2] at h3
      unfold Nine.fr
      generalize mk (2 * m + 1 + 1) n = x at h3
      calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) x) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x x) x) (ring (2 * m + 1 + 1)).one))) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x x) x) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x x) x) (ring (2 * m + 1 + 1)).one))) := by   -- (+7 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by lib_ring
    rcases root9 m hq (Fp2Sqrt.two_ne m) _ hx with ⟨a, ha⟩ | ⟨a, ha⟩ | h3
    · exact Or.inl ⟨a, by rw [← ha]⟩
    · match a, ha with
      | 0, ha => exact absurd ha (fun h' => Nat.noConfusion h')
      | a + 1, ha => exact Or.inr (Or.inl ⟨a, Nat.succ.inj (by show 2 * m + 1 + 1 + 1 + 1 = a * 9 + 8 + 1; rw [ha, Nat.succ_mul])⟩)
    · exact Or.inr (Or.inr (three_zero m h3))

/-- **A prime factor ≡ 8 (mod 9).** -/
theorem has8 (n N0 : Nat) (hN : N0 + (n + n + n + 1) = n * n * n) (h8 : ∃ c, N0 = c * 9 + 8) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ a, N = a * 9 + 8) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 9 + 8) ∧ Dv q N
  | 0, N, hN', _, ⟨a, ha⟩ => absurd (ha ▸ hN') (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hNG, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 9 + 6) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    have hlN0 := dv_trans _ _ _ hlN hN0
    rcases allowed9 n N0 l hq hN hlN0 with ⟨b, hb⟩ | ⟨b, hb⟩ | h3
    · match hlN with
      | ⟨N', hN'⟩ =>
      match cofactor9 b N' a (by rw [← hb, ← ha, hN']; lib_nat) with
      | ⟨c, hc⟩ =>
      have hlt : N' ≤ G := by
        have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
        have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
        exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hNG))
      have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
      match has8 n N0 hN h8 G N' hlt hN'0 ⟨c, hc⟩ with
      | ⟨q, hq', hq8, hqN⟩ => exact ⟨q, hq', hq8, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l + 1 + 1, hq, ⟨b, hb⟩, hlN⟩
    · exfalso
      obtain ⟨c, hc⟩ := h8
      obtain ⟨r, hr⟩ := hlN0
      rw [h3] at hr
      exact absurd (uniq_rem 3 (3 * c + 2) r 2 0 (Nat.lt_succ_self 2) (Nat.zero_lt_succ 2)
        (by rw [show (3 * c + 2) * 3 + 2 = c * 9 + 8 by lib_nat, ← hc, hr]; rfl)) (fun h' => Nat.noConfusion h')

/-- **Infinitely many primes p ≡ 8 (mod 9).** -/
theorem inf_8mod9 (B : Nat) : ∃ b, (∀ d, Dv d (b * 9 + 8) → d = 1 ∨ d = b * 9 + 8) ∧ B < b * 9 + 8 := by
  match fac_pos B with
  | ⟨f, hf⟩ =>
  have hN : (9 * f + 7) * (9 * f + 7) * (9 * f + 7) + 6 * ((9 * f + 7) * (9 * f + 7)) + 9 * (9 * f + 7) + 1
      + ((9 * f + 7 + 2) + (9 * f + 7 + 2) + (9 * f + 7 + 2) + 1) = (9 * f + 7 + 2) * (9 * f + 7 + 2) * (9 * f + 7 + 2) := by
    generalize 9 * f + 7 = j; lib_nat
  have h8 : ∃ c, (9 * f + 7) * (9 * f + 7) * (9 * f + 7) + 6 * ((9 * f + 7) * (9 * f + 7)) + 9 * (9 * f + 7) + 1 = c * 9 + 8 := by
    have e1 : (9 * f + 7) * (9 * f + 7) = 9 * (9 * f * f + 14 * f + 5) + 4 := by lib_nat
    have e2 : (9 * f + 7) * (9 * f + 7) * (9 * f + 7) = 9 * (9 * (9 * f * f + 14 * f + 5) * f + 7 * (9 * f * f + 14 * f + 5) + 4 * f + 3) + 1 := by
      rw [e1]; generalize 9 * f * f + 14 * f + 5 = u; lib_nat
    rw [e2, e1]
    exact ⟨9 * (9 * f * f + 14 * f + 5) * f + 7 * (9 * f * f + 14 * f + 5) + 4 * f + 3 + 6 * (9 * f * f + 14 * f + 5) + (9 * f + 7) + 2, by
      generalize 9 * (9 * f * f + 14 * f + 5) * f + 7 * (9 * f * f + 14 * f + 5) + 4 * f + 3 = v
      generalize 9 * f * f + 14 * f + 5 = u
      generalize 9 * f + 7 = j; lib_nat⟩
  obtain ⟨N0, hN0⟩ : ∃ N0, N0 = (9 * f + 7) * (9 * f + 7) * (9 * f + 7) + 6 * ((9 * f + 7) * (9 * f + 7)) + 9 * (9 * f + 7) + 1 := ⟨_, rfl⟩
  rw [← hN0] at hN h8
  match h8 with
  | ⟨c, hc⟩ =>
  match has8 (9 * f + 7 + 2) N0 hN ⟨c, hc⟩ N0 N0 (Nat.le_refl _) (dv_refl _) ⟨c, hc⟩ with
  | ⟨q, hq, ⟨b, hb⟩, hqN⟩ =>
    refine ⟨b, by rw [← hb]; exact hq, ?_⟩
    rw [← hb]
    cases Nat.lt_or_ge B q with
    | inl h => exact h
    | inr h =>
      exfalso
      match dv_fac B q (by rw [hb]; exact Nat.le_add_left 1 _) h with
      | ⟨r, hr⟩ =>
        have hn : Dv q (9 * f + 7 + 2) := ⟨9 * r, by
          rw [show 9 * f + 7 + 2 = 9 * (f + 1) by lib_nat, ← hf, hr]; lib_nat⟩
        obtain ⟨s, hs⟩ := hn
        have hq1 : q = (b * 9 + 7) + 1 := hb
        rw [hq1] at hqN hs
        have hsum : Dv (b * 9 + 7 + 1) (N0 + ((9 * f + 7 + 2) + (9 * f + 7 + 2) + (9 * f + 7 + 2) + 1)) := by
          rw [hN, hs]; exact ⟨s * (b * 9 + 7 + 1) * s * (b * 9 + 7 + 1) * s, by generalize b * 9 + 7 + 1 = q; lib_nat⟩
        have h1 := dv_sub (b * 9 + 7) _ _ hqN hsum
        have h2 : Dv (b * 9 + 7 + 1) ((9 * f + 7 + 2) + (9 * f + 7 + 2) + (9 * f + 7 + 2)) := ⟨s + s + s, by rw [hs]; lib_nat⟩
        exact dv_one_absurd (b * 9 + 6) (dv_sub (b * 9 + 7) _ 1 h2 h1)

end Nine

#print axioms Nine.uniq_rem
#print axioms Nine.mod9
#print axioms Nine.cofactor9
#print axioms Nine.three_zero
#print axioms Nine.allowed9
#print axioms Nine.has8
#print axioms Nine.inf_8mod9

#print axioms: 7 theorems, no axioms.

78.115 Primes congruent to ten mod eleven

Every prime factor of \(g(n)=n^5-n^4-4n^3+3n^2+3n-1\) is \(11\) or \(\equiv \pm 1\pmod {11}\), and \(g(11\cdot B!)\equiv 10\pmod {11}\).

\[ \forall B\ \exists b:\ 11b+10 \text { prime},\ B<11b+10. \]

lang/Ell11c.lean

/-
  lang/Ell11c — T43: **infinitely many primes p ≡ 10 (mod 11)** (Euclid-type, f = minimal polynomial of 2cos(2π/11)).
  g(n) = −f(−n) = Q(n) − P(n) with Q = 1n^5 + 3n^2 + 3n^1,
  P = 1n^4 + 4n^3 + 1. For n = 11·B!, N₀ = g(n) ≡ −1 (mod 11); every prime factor q is odd, f has a
  root mod q, so (Ell11b.root) q ≡ ±1 (mod 11) or q = 11, and q = 11 is impossible; some factor is ≡ 10 and it does not divide n.
    mod11, cofactor, rem_minus, l_zero, allowed, has, inf    (generated)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Ell11b
import lang.Fp2Sqrt
import lang.PrimeOneModQ
import lang.TwoPower
import lang.Nine9c

namespace Ell11

open FTAChain ZMod InfSplit

theorem add_cancel (a b : Nat) : ∀ c, a + c = b + c → a = b
  | 0, h => h
  | c + 1, h => add_cancel a b c (Nat.succ.inj h)

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem le_mul_self (a n : Nat) (hn : 1 ≤ n) : a ≤ a * n :=
  match n, hn with
  | n' + 1, _ => Nat.le_trans (Nat.le_add_left a (a * n')) (Nat.le_of_eq (by lib_nat))

theorem modL (n : Nat) : ∃ a r, n = a * 11 + r ∧ r < 11 :=
  match div_exists 10 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (11 : Nat) = 10 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor (b N' a : Nat) (h : (b * 11 + 1) * N' = a * 11 + 10) : ∃ c, N' = c * 11 + 10 :=
  match modL N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 11 + 1) * N' = (b * 11 * c + b * r + c) * 11 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, Nine.uniq_rem 11 _ _ r 10 hr (Nat.lt_succ_self 10) h]⟩

theorem rem_minus (N A B : Nat) (h : N + (A * 11 + 1) = B * 11) : ∃ c, N = c * 11 + 10 := by
  obtain ⟨c, r, hc, hr⟩ := modL N
  cases Nat.lt_or_ge (r + 1) 11 with
  | inl hlt =>
    have e : (c + A) * 11 + (r + 1) = B * 11 + 0 := by rw [Nat.add_zero, ← h, hc]; lib_nat
    exact absurd (Nine.uniq_rem 11 _ _ _ _ hlt (Nat.zero_lt_succ 10) e) (fun h' => Nat.noConfusion h')
  | inr hge =>
    have e : r = 10 := Nat.le_antisymm (Nat.le_of_lt_succ hr) (Nat.le_of_succ_le_succ hge)
    exact ⟨c, by rw [hc, e]⟩

theorem l_zero (m : Nat) (h : lnum m = (ring (2 * m + 1 + 1)).zero) : 2 * m + 1 + 1 + 1 = 11 := by
  have e : lnum m = mk (2 * m + 1 + 1) 11 := by
    unfold lnum
    rw [show (ring (2 * m + 1 + 1)).zero = mk (2 * m + 1 + 1) 0 from rfl, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one]
  rw [e, show (ring (2 * m + 1 + 1)).zero = mk (2 * m + 1 + 1) 0 from rfl] at h
  unfold mk at h
  have h0 : md (2 * m + 1 + 1) 11 = md (2 * m + 1 + 1) 0 := Subtype.mk.inj h
  match m, h0 with
  | 0, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 1, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 2, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 3, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 4, _ => rfl
  | k + 5, h0 =>
    rw [md_small _ 11 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 11) (Nat.le_trans (Nat.le_add_left 12 (2 * k + 1)) (Nat.le_of_eq (by lib_nat))))] at h0
    exact Nat.noConfusion h0

theorem allowed (n N l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hN : N + (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) = n * n * n * n * n + n * n + n * n + n * n + n + n + n) (hdv : Dv (l + 1 + 1) N) :
    (∃ a, l + 1 + 1 = a * 11 + 1) ∨ (∃ a, l + 1 + 1 = a * 11 + 10) ∨ l + 1 + 1 = 11 := by
  rcases EveryPrime.nat_even_odd l with ⟨j, hj⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨j + 1, by rw [hj]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    obtain ⟨r, hr⟩ := hdv
    rw [hr] at hN
    rcases EveryPrime.nat_even_odd n with ⟨i, hi⟩ | ⟨i, hi⟩
    · have hQe : n * n * n * n * n + n * n + n * n + n * n + n + n + n = (n * n * n * n * i + n * i + n * i + n * i + i + i + i) * 2 := by
        calc _ = n * n * n * n * (2 * i) + n * (2 * i) + n * (2 * i) + n * (2 * i) + (2 * i) + (2 * i) + (2 * i) := by rw [← hi]
          _ = _ := by lib_nat
      have hPe : n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = (n * n * n * i + n * n * i + n * n * i + n * n * i + n * n * i) * 2 := by
        calc _ = n * n * n * (2 * i) + n * n * (2 * i) + n * n * (2 * i) + n * n * (2 * i) + n * n * (2 * i) := by rw [← hi]
          _ = _ := by lib_nat
      exact odd_not_dv2 (r + (n * n * n * i + n * n * i + n * n * i + n * n * i + n * n * i)) (n * n * n * n * i + n * i + n * i + n * i + i + i + i) (by
        calc _ = r * 2 + (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) := by rw [hPe]; lib_nat
          _ = _ := hN
          _ = _ := hQe)
    · have hQo : n * n * n * n * n + n * n + n * n + n * n + n + n + n = 7 + (i * ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1))) * 2 := add_cancel _ _ ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1)) (by
        calc n * n * n * n * n + n * n + n * n + n * n + n + n + n + ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1)) = 7 + n * ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1)) := by lib_nat
          _ = 7 + (2 * i + 1) * ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1)) := by rw [hi]
          _ = _ := by lib_nat)
      have hPo : n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = 5 + (i * ((1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n))) * 2 := add_cancel _ _ ((1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) (by
        calc n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + ((1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) = 5 + n * ((1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) := by lib_nat
          _ = 5 + (2 * i + 1) * ((1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) := by rw [hi]
          _ = _ := by lib_nat)
      exact odd_not_dv2 (3 + i * ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1))) (r + i * ((1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) + 3) (by
        calc _ = 7 + (i * ((1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1))) * 2 := by lib_nat
          _ = n * n * n * n * n + n * n + n * n + n * n + n + n + n := hQo.symm
          _ = r * 2 + (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) := hN.symm
          _ = _ := by rw [hPo]; lib_nat)
  · subst hm
    have hx : fr m ((ring (2 * m + 1 + 1)).neg (mk (2 * m + 1 + 1) n)) = (ring (2 * m + 1 + 1)).zero := by
      have h1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) (mk (2 *  -- (+409 chars; full line in the Zenodo source)
        repeat (first | rw [TwoPower.mk_mul] | rw [PrimeOneModQ.mk_add] | rw [TwoPower.mk_add_one])
      have h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((mk (2   -- (+577 chars; full line in the Zenodo source)
        repeat (first | rw [TwoPower.mk_mul] | rw [PrimeOneModQ.mk_add] | rw [TwoPower.mk_add_one])
      have h0 : mk (2 * m + 1 + 1) N = (ring (2 * m + 1 + 1)).zero := Subtype.ext (by
        show md (2 * m + 1 + 1) N = md (2 * m + 1 + 1) 0
        rw [dv_md_zero _ N hdv]; rfl)
      have h3 : (ring (2 * m + 1 + 1)).add (mk (2 * m + 1 + 1) N) (mk (2 * m + 1 + 1) (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1)) = mk (2 * m + 1 + 1) (n * n * n * n * n + n * n + n * n + n * n + n + n + n) := by
        rw [PrimeOneModQ.mk_add, hN]
      rw [h0, ← h1, ← h2] at h3
      unfold fr
      generalize mk (2 * m + 1 + 1) n = x at h3
      calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) x) x) x) x) ((ring (2 * m + 1 + 1)).mul (x) x)  -- (+666 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) x) x) x) ((ring (2 * m + 1 + 1)).mul ((ring (2 *   -- (+833 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by lib_ring
    rcases root m hq (Fp2Sqrt.two_ne m) _ hx with ⟨a, ha⟩ | ⟨a, ha⟩ | hl
    · exact Or.inl ⟨a, by rw [← ha]⟩
    · match a, ha with
      | 0, ha => exact absurd ha (fun h' => Nat.noConfusion h')
      | a + 1, ha => exact Or.inr (Or.inl ⟨a, Nat.succ.inj (by show 2 * m + 1 + 1 + 1 + 1 = a * 11 + 10 + 1; rw [ha, Nat.succ_mul])⟩)
    · exact Or.inr (Or.inr (l_zero m hl))

theorem has (n N0 : Nat) (hN : N0 + (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) = n * n * n * n * n + n * n + n * n + n * n + n + n + n) (hc0 : ∃ c, N0 = c * 11 + 10) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ a, N = a * 11 + 10) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 11 + 10) ∧ Dv q N
  | 0, N, hN', _, ⟨a, ha⟩ => absurd (ha ▸ hN') (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hNG, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 11 + 8) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    have hlN0 := dv_trans _ _ _ hlN hN0
    rcases allowed n N0 l hq hN hlN0 with ⟨b, hb⟩ | ⟨b, hb⟩ | hL
    · match hlN with
      | ⟨N', hN'⟩ =>
      match cofactor b N' a (by rw [← hb, ← ha, hN']; lib_nat) with
      | ⟨c, hc⟩ =>
      have hlt : N' ≤ G := by
        have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
        have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
        exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hNG))
      have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
      match has n N0 hN hc0 G N' hlt hN'0 ⟨c, hc⟩ with
      | ⟨q, hq', hqL, hqN⟩ => exact ⟨q, hq', hqL, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l + 1 + 1, hq, ⟨b, hb⟩, hlN⟩
    · exfalso
      obtain ⟨c, hc⟩ := hc0
      obtain ⟨r, hr⟩ := hlN0
      rw [hL] at hr
      exact absurd (Nine.uniq_rem 11 c r 10 0 (Nat.lt_succ_self 10) (Nat.zero_lt_succ 10)
        (by rw [← hc, hr]; rfl)) (fun h' => Nat.noConfusion h')

/-- **Infinitely many primes p ≡ 10 (mod 11).** -/
theorem inf (B : Nat) : ∃ b, (∀ d, Dv d (b * 11 + 10) → d = 1 ∨ d = b * 11 + 10) ∧ B < b * 11 + 10 := by
  obtain ⟨f, hf⟩ := fac_pos B
  obtain ⟨n, hk⟩ : ∃ n, n = (f + 1) * 11 := ⟨_, rfl⟩
  have hlen : 11 ≤ n := by rw [hk]; exact Nat.le_trans (Nat.le_add_left 11 (f * 11)) (Nat.le_of_eq (by lib_nat))
  have hM : 6 ≤ n := Nat.le_trans (Nat.le_add_left 6 5) hlen
  have hn : 1 ≤ n := Nat.le_trans (Nat.le_add_left 1 5) hM
  have hle : n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1 ≤ n * n * n * n * n + n * n + n * n + n * n + n + n + n := by
    have h1 : n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1 ≤ (n * n * n * n) + (n * n * n * n) + (n * n * n * n) + (n * n * n * n) + (n * n * n * n) + (n * n * n * n) := Nat.add_le_add (Nat.add_le_add (Nat.add_le_add (Nat.add_le_add (Nat.add_le_add (Nat.le_refl _) (Nat.le_trans (le_mul_self (n * n * n) n hn) (Nat.le_refl _))) (Nat.le_trans (le_mul_self (n * n * n) n hn) (Nat.l  -- (+297 chars; full line in the Zenodo source)
    calc _ ≤ (n * n * n * n) + (n * n * n * n) + (n * n * n * n) + (n * n * n * n) + (n * n * n * n) + (n * n * n * n) := h1
      _ = 6 * (n * n * n * n) := by lib_nat
      _ ≤ n * (n * n * n * n) := Nat.mul_le_mul_right _ hM
      _ = n * n * n * n * n := by lib_nat
      _ ≤ _ := by rw [show n * n * n * n * n + n * n + n * n + n * n + n + n + n = n * n * n * n * n + (n * n + n * n + n * n + n + n + n) by lib_nat]; exact Nat.le_add_right _ _
  obtain ⟨N0, hN⟩ := le_dest _ _ hle
  generalize f + 1 = k at hf hk
  have hA : n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = (n * n * n * k + n * n * k + n * n * k + n * n * k + n * n * k) * 11 := by
    calc _ = n * n * n * (k * 11) + n * n * (k * 11) + n * n * (k * 11) + n * n * (k * 11) + n * n * (k * 11) := by rw [← hk]
      _ = _ := by lib_nat
  have hB : n * n * n * n * n + n * n + n * n + n * n + n + n + n = (n * n * n * n * k + n * k + n * k + n * k + k + k + k) * 11 := by
    calc _ = n * n * n * n * (k * 11) + n * (k * 11) + n * (k * 11) + n * (k * 11) + (k * 11) + (k * 11) + (k * 11) := by rw [← hk]
      _ = _ := by lib_nat
  obtain ⟨c, hc⟩ := rem_minus N0 (n * n * n * k + n * n * k + n * n * k + n * n * k + n * n * k) (n * n * n * n * k + n * k + n * k + n * k + k + k + k) (by rw [← hA, ← hB]; exact hN)
  obtain ⟨q, hq, ⟨b, hb⟩, hqN⟩ := has n N0 hN ⟨c, hc⟩ N0 N0 (Nat.le_refl _) (dv_refl _) ⟨c, hc⟩
  refine ⟨b, by rw [← hb]; exact hq, ?_⟩
  rw [← hb]
  cases Nat.lt_or_ge B q with
  | inl h => exact h
  | inr h =>
    exfalso
    obtain ⟨r, hr⟩ := dv_fac B q (by rw [hb]; exact Nat.le_add_left 1 _) h
    obtain ⟨s, hs⟩ : ∃ s, n = s * q := ⟨r * 11, by rw [hk, ← hf, hr]; lib_nat⟩
    have hQ : n * n * n * n * n + n * n + n * n + n * n + n + n + n = (n * n * n * n * s + n * s + n * s + n * s + s + s + s) * q := by
      calc _ = n * n * n * n * (s * q) + n * (s * q) + n * (s * q) + n * (s * q) + (s * q) + (s * q) + (s * q) := by rw [← hs]
        _ = _ := by lib_nat
    have hP : n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = (n * n * n * s + n * n * s + n * n * s + n * n * s + n * n * s) * q := by
      calc _ = n * n * n * (s * q) + n * n * (s * q) + n * n * (s * q) + n * n * (s * q) + n * n * (s * q) := by rw [← hs]
        _ = _ := by lib_nat
    have hsum : Dv q (N0 + (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1)) := ⟨_, by rw [hN, hQ]⟩
    have h2 : Dv q (n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n) := ⟨_, hP⟩
    have hq1 : q = (b * 11 + 9) + 1 := hb
    rw [hq1] at hqN hsum h2
    exact dv_one_absurd (b * 11 + 8) (dv_sub (b * 11 + 9) _ 1 h2 (dv_sub (b * 11 + 9) _ _ hqN hsum))

end Ell11

#print axioms Ell11.add_cancel
#print axioms Ell11.le_dest
#print axioms Ell11.le_mul_self
#print axioms Ell11.modL
#print axioms Ell11.cofactor
#print axioms Ell11.rem_minus
#print axioms Ell11.l_zero
#print axioms Ell11.allowed
#print axioms Ell11.has
#print axioms Ell11.inf

#print axioms: 10 theorems, no axioms.

78.116 Primes congruent to twelve mod thirteen

Every prime factor of \(f(n)=n^6+n^5-5n^4-4n^3+6n^2+3n-1\) is \(13\) or \(\equiv \pm 1\pmod {13}\), and \(f(13\cdot B!)\equiv 12\pmod {13}\).

\[ \forall B\ \exists b:\ 13b+12 \text { prime},\ B<13b+12. \]

lang/Ell13c.lean

/-
  lang/Ell13c — T43: **infinitely many primes p ≡ 12 (mod 13)** (Euclid-type, f = minimal polynomial of 2cos(2π/13)).
  g(n) = f(n) = Q(n) − P(n) with Q = 1n^6 + 1n^5 + 6n^2 + 3n^1,
  P = 5n^4 + 4n^3 + 1. For n = 13·B!, N₀ = g(n) ≡ −1 (mod 13); every prime factor q is odd, f has a
  root mod q, so (Ell13b.root) q ≡ ±1 (mod 13) or q = 13, and q = 13 is impossible; some factor is ≡ 12 and it does not divide n.
    mod13, cofactor, rem_minus, l_zero, allowed, has, inf    (generated)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Ell13b
import lang.Fp2Sqrt
import lang.PrimeOneModQ
import lang.TwoPower
import lang.Nine9c

namespace Ell13

open FTAChain ZMod InfSplit

theorem add_cancel (a b : Nat) : ∀ c, a + c = b + c → a = b
  | 0, h => h
  | c + 1, h => add_cancel a b c (Nat.succ.inj h)

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem le_mul_self (a n : Nat) (hn : 1 ≤ n) : a ≤ a * n :=
  match n, hn with
  | n' + 1, _ => Nat.le_trans (Nat.le_add_left a (a * n')) (Nat.le_of_eq (by lib_nat))

theorem modL (n : Nat) : ∃ a r, n = a * 13 + r ∧ r < 13 :=
  match div_exists 12 n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [show (13 : Nat) = 12 + 1 from rfl, ← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactor (b N' a : Nat) (h : (b * 13 + 1) * N' = a * 13 + 12) : ∃ c, N' = c * 13 + 12 :=
  match modL N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * 13 + 1) * N' = (b * 13 * c + b * r + c) * 13 + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, Nine.uniq_rem 13 _ _ r 12 hr (Nat.lt_succ_self 12) h]⟩

theorem rem_minus (N A B : Nat) (h : N + (A * 13 + 1) = B * 13) : ∃ c, N = c * 13 + 12 := by
  obtain ⟨c, r, hc, hr⟩ := modL N
  cases Nat.lt_or_ge (r + 1) 13 with
  | inl hlt =>
    have e : (c + A) * 13 + (r + 1) = B * 13 + 0 := by rw [Nat.add_zero, ← h, hc]; lib_nat
    exact absurd (Nine.uniq_rem 13 _ _ _ _ hlt (Nat.zero_lt_succ 12) e) (fun h' => Nat.noConfusion h')
  | inr hge =>
    have e : r = 12 := Nat.le_antisymm (Nat.le_of_lt_succ hr) (Nat.le_of_succ_le_succ hge)
    exact ⟨c, by rw [hc, e]⟩

theorem l_zero (m : Nat) (h : lnum m = (ring (2 * m + 1 + 1)).zero) : 2 * m + 1 + 1 + 1 = 13 := by
  have e : lnum m = mk (2 * m + 1 + 1) 13 := by
    unfold lnum
    rw [show (ring (2 * m + 1 + 1)).zero = mk (2 * m + 1 + 1) 0 from rfl, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one, TwoPower.mk_add_one]
  rw [e, show (ring (2 * m + 1 + 1)).zero = mk (2 * m + 1 + 1) 0 from rfl] at h
  unfold mk at h
  have h0 : md (2 * m + 1 + 1) 13 = md (2 * m + 1 + 1) 0 := Subtype.mk.inj h
  match m, h0 with
  | 0, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 1, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 2, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 3, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 4, h0 => exact absurd h0 (fun h' => Nat.noConfusion h')
  | 5, _ => rfl
  | k + 6, h0 =>
    rw [md_small _ 13 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 13) (Nat.le_trans (Nat.le_add_left 14 (2 * k + 1)) (Nat.le_of_eq (by lib_nat))))] at h0
    exact Nat.noConfusion h0

theorem allowed (n N l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1)
    (hN : N + (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) = n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n) (hdv : Dv (l + 1 + 1) N) :
    (∃ a, l + 1 + 1 = a * 13 + 1) ∨ (∃ a, l + 1 + 1 = a * 13 + 12) ∨ l + 1 + 1 = 13 := by
  rcases EveryPrime.nat_even_odd l with ⟨j, hj⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨j + 1, by rw [hj]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    obtain ⟨r, hr⟩ := hdv
    rw [hr] at hN
    rcases EveryPrime.nat_even_odd n with ⟨i, hi⟩ | ⟨i, hi⟩
    · have hQe : n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n = (n * n * n * n * n * i + n * n * n * n * i + n * i + n * i + n * i + n * i + n * i + n * i + i + i + i) * 2 := by
        calc _ = n * n * n * n * n * (2 * i) + n * n * n * n * (2 * i) + n * (2 * i) + n * (2 * i) + n * (2 * i) + n * (2 * i) + n * (2 * i) + n * (2 * i) + (2 * i) + (2 * i) + (2 * i) := by rw [← hi]
          _ = _ := by lib_nat
      have hPe : n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = (n * n * n * i + n * n * n * i + n * n * n * i + n * n * n * i + n * n * n * i + n * n * i + n * n * i + n * n * i + n * n * i) * 2 := by
        calc _ = n * n * n * (2 * i) + n * n * n * (2 * i) + n * n * n * (2 * i) + n * n * n * (2 * i) + n * n * n * (2 * i) + n * n * (2 * i) + n * n * (2 * i) + n * n * (2 * i) + n * n * (2 * i) := by rw [← hi]
          _ = _ := by lib_nat
      exact odd_not_dv2 (r + (n * n * n * i + n * n * n * i + n * n * n * i + n * n * n * i + n * n * n * i + n * n * i + n * n * i + n * n * i + n * n * i)) (n * n * n * n * n * i + n * n * n * n * i + n * i + n * i + n * i + n * i + n * i + n * i + i + i + i) (by
        calc _ = r * 2 + (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) := by rw [hPe]; lib_nat
          _ = _ := hN
          _ = _ := hQe)
    · have hQo : n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n = 11 + (i * ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n * n * n) + (1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1))) * 2 := add_cancel _ _ ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n   -- (+137 chars; full line in the Zenodo source)
        calc n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n + ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n * n * n) + (1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1)) = 11 + n * ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n * n * n) + (1 + n + n * n   -- (+121 chars; full line in the Zenodo source)
          _ = 11 + (2 * i + 1) * ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n * n * n) + (1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1)) := by rw [hi]
          _ = _ := by lib_nat)
      have hPo : n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = 9 + (i * ((1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n))) * 2 := add_cancel _ _ (  -- (+224 chars; full line in the Zenodo source)
        calc n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + ((1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) = 9 + n * ((1 + n + n * n + n * n * n  -- (+208 chars; full line in the Zenodo source)
          _ = 9 + (2 * i + 1) * ((1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n) + (1 + n + n * n)) := by rw [hi]
          _ = _ := by lib_nat)
      exact odd_not_dv2 (5 + i * ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n * n * n) + (1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1))) (r + i * ((1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n + n * n * n) + (1 + n + n * n)   -- (+63 chars; full line in the Zenodo source)
        calc _ = 11 + (i * ((1 + n + n * n + n * n * n + n * n * n * n + n * n * n * n * n) + (1 + n + n * n + n * n * n + n * n * n * n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1 + n) + (1) + (1) + (1))) * 2 := by lib_nat
          _ = n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n := hQo.symm
          _ = r * 2 + (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) := hN.symm
          _ = _ := by rw [hPo]; lib_nat)
  · subst hm
    have hx : fr m (mk (2 * m + 1 + 1) n) = (ring (2 * m + 1 + 1)).zero := by
      have h1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1  -- (+1089 chars; full line in the Zenodo source)
        repeat (first | rw [TwoPower.mk_mul] | rw [PrimeOneModQ.mk_add] | rw [TwoPower.mk_add_one])
      have h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((mk (2 * m + 1 + 1) n)) (mk (2 * m + 1 + 1) n))   -- (+1481 chars; full line in the Zenodo source)
        repeat (first | rw [TwoPower.mk_mul] | rw [PrimeOneModQ.mk_add] | rw [TwoPower.mk_add_one])
      have h0 : mk (2 * m + 1 + 1) N = (ring (2 * m + 1 + 1)).zero := Subtype.ext (by
        show md (2 * m + 1 + 1) N = md (2 * m + 1 + 1) 0
        rw [dv_md_zero _ N hdv]; rfl)
      have h3 : (ring (2 * m + 1 + 1)).add (mk (2 * m + 1 + 1) N) (mk (2 * m + 1 + 1) (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1)) = mk (2 * m + 1 + 1) (n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n) := by
        rw [PrimeOneModQ.mk_add, hN]
      rw [h0, ← h1, ← h2] at h3
      unfold fr
      generalize mk (2 * m + 1 + 1) n = x at h3
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)  -- (+1521 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).m  -- (+1812 chars; full line in the Zenodo source)
        _ = (ring (2 * m + 1 + 1)).zero := by lib_ring
    rcases root m hq (Fp2Sqrt.two_ne m) _ hx with ⟨a, ha⟩ | ⟨a, ha⟩ | hl
    · exact Or.inl ⟨a, by rw [← ha]⟩
    · match a, ha with
      | 0, ha => exact absurd ha (fun h' => Nat.noConfusion h')
      | a + 1, ha => exact Or.inr (Or.inl ⟨a, Nat.succ.inj (by show 2 * m + 1 + 1 + 1 + 1 = a * 13 + 12 + 1; rw [ha, Nat.succ_mul])⟩)
    · exact Or.inr (Or.inr (l_zero m hl))

theorem has (n N0 : Nat) (hN : N0 + (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1) = n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n) (hc0 : ∃ c, N0 = c * 13 + 12) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ a, N = a * 13 + 12) →
    ∃ q, (∀ d, Dv d q → d = 1 ∨ d = q) ∧ (∃ b, q = b * 13 + 12) ∧ Dv q N
  | 0, N, hN', _, ⟨a, ha⟩ => absurd (ha ▸ hN') (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hNG, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * 13 + 10) with
    | ⟨l, hl, hmin⟩ =>
    have hq := lp_prime _ l hl hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl
    have hlN0 := dv_trans _ _ _ hlN hN0
    rcases allowed n N0 l hq hN hlN0 with ⟨b, hb⟩ | ⟨b, hb⟩ | hL
    · match hlN with
      | ⟨N', hN'⟩ =>
      match cofactor b N' a (by rw [← hb, ← ha, hN']; lib_nat) with
      | ⟨c, hc⟩ =>
      have hlt : N' ≤ G := by
        have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
        have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
        exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hNG))
      have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
      match has n N0 hN hc0 G N' hlt hN'0 ⟨c, hc⟩ with
      | ⟨q, hq', hqL, hqN⟩ => exact ⟨q, hq', hqL, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l + 1 + 1, hq, ⟨b, hb⟩, hlN⟩
    · exfalso
      obtain ⟨c, hc⟩ := hc0
      obtain ⟨r, hr⟩ := hlN0
      rw [hL] at hr
      exact absurd (Nine.uniq_rem 13 c r 12 0 (Nat.lt_succ_self 12) (Nat.zero_lt_succ 12)
        (by rw [← hc, hr]; rfl)) (fun h' => Nat.noConfusion h')

/-- **Infinitely many primes p ≡ 12 (mod 13).** -/
theorem inf (B : Nat) : ∃ b, (∀ d, Dv d (b * 13 + 12) → d = 1 ∨ d = b * 13 + 12) ∧ B < b * 13 + 12 := by
  obtain ⟨f, hf⟩ := fac_pos B
  obtain ⟨n, hk⟩ : ∃ n, n = (f + 1) * 13 := ⟨_, rfl⟩
  have hlen : 13 ≤ n := by rw [hk]; exact Nat.le_trans (Nat.le_add_left 13 (f * 13)) (Nat.le_of_eq (by lib_nat))
  have hM : 10 ≤ n := Nat.le_trans (Nat.le_add_left 10 3) hlen
  have hn : 1 ≤ n := Nat.le_trans (Nat.le_add_left 1 9) hM
  have hle : n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1 ≤ n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n := by
    have h1 : n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1 ≤ (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) := Nat.add_le_add (Nat.add_le_add (N  -- (+1105 chars; full line in the Zenodo source)
    calc _ ≤ (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) + (n * n * n * n * n) := h1
      _ = 10 * (n * n * n * n * n) := by lib_nat
      _ ≤ n * (n * n * n * n * n) := Nat.mul_le_mul_right _ hM
      _ = n * n * n * n * n * n := by lib_nat
      _ ≤ _ := by rw [show n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n = n * n * n * n * n * n + (n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n) by lib_nat]; exact Nat.le_add_right _ _
  obtain ⟨N0, hN⟩ := le_dest _ _ hle
  generalize f + 1 = k at hf hk
  have hA : n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = (n * n * n * k + n * n * n * k + n * n * n * k + n * n * n * k + n * n * n * k + n * n * k + n * n * k + n * n * k + n * n * k) * 13 := by
    calc _ = n * n * n * (k * 13) + n * n * n * (k * 13) + n * n * n * (k * 13) + n * n * n * (k * 13) + n * n * n * (k * 13) + n * n * (k * 13) + n * n * (k * 13) + n * n * (k * 13) + n * n * (k * 13) := by rw [← hk]
      _ = _ := by lib_nat
  have hB : n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n = (n * n * n * n * n * k + n * n * n * n * k + n * k + n * k + n * k + n * k + n * k + n * k + k + k + k) * 13 := by
    calc _ = n * n * n * n * n * (k * 13) + n * n * n * n * (k * 13) + n * (k * 13) + n * (k * 13) + n * (k * 13) + n * (k * 13) + n * (k * 13) + n * (k * 13) + (k * 13) + (k * 13) + (k * 13) := by rw [← hk]
      _ = _ := by lib_nat
  obtain ⟨c, hc⟩ := rem_minus N0 (n * n * n * k + n * n * n * k + n * n * n * k + n * n * n * k + n * n * n * k + n * n * k + n * n * k + n * n * k + n * n * k) (n * n * n * n * n * k + n * n * n * n * k + n * k + n * k + n * k + n * k + n * k + n * k + k + k + k) (by rw [← hA, ← hB]; exact hN)
  obtain ⟨q, hq, ⟨b, hb⟩, hqN⟩ := has n N0 hN ⟨c, hc⟩ N0 N0 (Nat.le_refl _) (dv_refl _) ⟨c, hc⟩
  refine ⟨b, by rw [← hb]; exact hq, ?_⟩
  rw [← hb]
  cases Nat.lt_or_ge B q with
  | inl h => exact h
  | inr h =>
    exfalso
    obtain ⟨r, hr⟩ := dv_fac B q (by rw [hb]; exact Nat.le_add_left 1 _) h
    obtain ⟨s, hs⟩ : ∃ s, n = s * q := ⟨r * 13, by rw [hk, ← hf, hr]; lib_nat⟩
    have hQ : n * n * n * n * n * n + n * n * n * n * n + n * n + n * n + n * n + n * n + n * n + n * n + n + n + n = (n * n * n * n * n * s + n * n * n * n * s + n * s + n * s + n * s + n * s + n * s + n * s + s + s + s) * q := by
      calc _ = n * n * n * n * n * (s * q) + n * n * n * n * (s * q) + n * (s * q) + n * (s * q) + n * (s * q) + n * (s * q) + n * (s * q) + n * (s * q) + (s * q) + (s * q) + (s * q) := by rw [← hs]
        _ = _ := by lib_nat
    have hP : n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n = (n * n * n * s + n * n * n * s + n * n * n * s + n * n * n * s + n * n * n * s + n * n * s + n * n * s + n * n * s + n * n * s) * q := by
      calc _ = n * n * n * (s * q) + n * n * n * (s * q) + n * n * n * (s * q) + n * n * n * (s * q) + n * n * n * (s * q) + n * n * (s * q) + n * n * (s * q) + n * n * (s * q) + n * n * (s * q) := by rw [← hs]
        _ = _ := by lib_nat
    have hsum : Dv q (N0 + (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n + 1)) := ⟨_, by rw [hN, hQ]⟩
    have h2 : Dv q (n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n * n + n * n * n + n * n * n + n * n * n + n * n * n) := ⟨_, hP⟩
    have hq1 : q = (b * 13 + 11) + 1 := hb
    rw [hq1] at hqN hsum h2
    exact dv_one_absurd (b * 13 + 10) (dv_sub (b * 13 + 11) _ 1 h2 (dv_sub (b * 13 + 11) _ _ hqN hsum))

end Ell13

#print axioms Ell13.add_cancel
#print axioms Ell13.le_dest
#print axioms Ell13.le_mul_self
#print axioms Ell13.modL
#print axioms Ell13.cofactor
#print axioms Ell13.rem_minus
#print axioms Ell13.l_zero
#print axioms Ell13.allowed
#print axioms Ell13.has
#print axioms Ell13.inf

#print axioms: 10 theorems, no axioms.

78.117 Primes congruent to minus one mod \(\ell \)

For every prime \(\ell =2d+3\not \equiv 1\pmod 8\), with \(f\) the minimal polynomial of \(2\cos (2\pi /\ell )\), every prime factor of \(\pm f(\pm \ell \cdot (B+2)!)\) is \(\equiv \pm 1\pmod \ell \) and the number is \(\equiv -1\).

\[ \ell \ \text {prime},\ \ell \not \equiv 1\ (8)\ \Longrightarrow \ \forall B\ \exists b:\ \ell b+\ell -1 \text { prime},\ B<\ell b+\ell -1. \]

lang/ChebInf.lean

/-
  lang/ChebInf — T43, cycle 406: **for every prime ℓ ≢ 1 (mod 8) there are infinitely many primes p ≡ −1 (mod ℓ).**

  ℓ = 2d + 3 (f = f_{d+1}, the minimal polynomial of 2cos(2π/ℓ)). n = ℓ·(B + 2)!, t = n − 2,
    d + 1 ≡ 2, 3 (mod 4): N₀ = f(n) = N⁺ (f(0) = −1, root n);
    d + 1 ≡ 1 (mod 4):    N₀ = −f(−n) = N⁻ (f(0) = 1, root −n).
  Then n | N₀ + 1 (ChebCong), so N₀ is odd, N₀ ≡ −1 (mod ℓ) and prime to every q ≤ B + 2; every prime factor q of N₀ has a root of f
  mod q, hence (ChebRoot.root) q ≡ ±1 (mod ℓ) or q = ℓ (impossible); a product of primes ≡ 1 is ≡ 1, so some factor is ≡ −1.
    geo_one_emb, modC, cofactorC, allowedC, hasC, infC     the descent for an abstract N₀
    inf_m1                                                  the theorem
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.ChebRoot
import lang.ChebCong
import lang.Fp2Sqrt
import lang.Nine9c

namespace Cheb

open FTAChain ZMod InfSplit

theorem geo_one_emb {R : Type} (K : DetChain.CRing R) : ∀ k, geo K K.one k = emb K k
  | 0 => rfl
  | k + 1 => by
    show K.add (geo K K.one k) (Frobenius.pw K K.one k) = K.add (emb K k) K.one
    rw [geo_one_emb K k, TwoPower.pwone]

theorem modC (L' n : Nat) : ∃ a r, n = a * (L' + 1) + r ∧ r < L' + 1 :=
  match div_exists L' n with
  | ⟨a, r, k, hn, hr⟩ => ⟨a, r, hn, Nat.lt_of_lt_of_le (Nat.lt_succ_self r) (by rw [← hr]; exact Nat.succ_le_succ (Nat.le_add_right r k))⟩

theorem cofactorC (L' b N' a : Nat) (h : (b * (L' + 1) + 1) * N' = a * (L' + 1) + L') : ∃ c, N' = c * (L' + 1) + L' :=
  match modC L' N' with
  | ⟨c, r, hc, hr⟩ => by
    have e : (b * (L' + 1) + 1) * N' = (b * (L' + 1) * c + b * r + c) * (L' + 1) + r := by rw [hc]; lib_nat
    rw [e] at h
    exact ⟨c, by rw [hc, Nine.uniq_rem (L' + 1) _ _ r L' hr (Nat.lt_succ_self L') h]⟩

/-- prime factors of N₀ (given a root of f_{d+1} mod each odd prime factor): ≡ 1, ≡ −1 (mod ℓ), or ℓ. -/
theorem allowedC (d N0 l : Nat) (hl : ∀ e, Dv e (2 * d + 1 + 1 + 1) → e = 1 ∨ e = 2 * d + 1 + 1 + 1)
    (hodd : ∀ r, N0 ≠ r * 2)
    (hrt : ∀ m, (∀ e, Dv e (2 * m + 1 + 1 + 1) → e = 1 ∨ e = 2 * m + 1 + 1 + 1) → Dv (2 * m + 1 + 1 + 1) N0 →
      ∃ x : Zp (2 * m + 1 + 1), fd (ring (2 * m + 1 + 1)) x (d + 1) = (ring (2 * m + 1 + 1)).zero)
    (hq : ∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) (hdv : Dv (l + 1 + 1) N0) :
    (∃ a, l + 1 + 1 = a * (2 * d + 1 + 1 + 1) + 1) ∨ (∃ a, l + 1 + 1 = a * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1)) ∨
      l + 1 + 1 = 2 * d + 1 + 1 + 1 := by
  rcases EveryPrime.nat_even_odd l with ⟨j, hj⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨j + 1, by rw [hj]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hdv
    obtain ⟨r, hr⟩ := hdv
    exact hodd r hr
  · subst hm
    obtain ⟨x, hx⟩ := hrt m hq hdv
    rcases root m hq (2 * d + 1) (d + 1) hl (by lib_nat) (Fp2Sqrt.two_ne m) x hx with ⟨a, ha⟩ | ⟨a, ha⟩ | h3
    · exact Or.inl ⟨a, by rw [← ha]⟩
    · match a, ha with
      | 0, ha => exact absurd (ha.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
      | a + 1, ha => exact Or.inr (Or.inl ⟨a, Nat.succ.inj (by
          show 2 * m + 1 + 1 + 1 + 1 = a * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1) + 1
          rw [ha, Nat.succ_mul]; rfl)⟩)
    · rw [geo_one_emb] at h3
      have hd := dv_of_emb (2 * m + 1 + 1) _ h3
      cases hl _ hd with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact Or.inr (Or.inr e)

/-- **A prime factor ≡ −1 (mod ℓ).** -/
theorem hasC (d N0 : Nat) (hl : ∀ e, Dv e (2 * d + 1 + 1 + 1) → e = 1 ∨ e = 2 * d + 1 + 1 + 1)
    (hodd : ∀ r, N0 ≠ r * 2)
    (hrt : ∀ m, (∀ e, Dv e (2 * m + 1 + 1 + 1) → e = 1 ∨ e = 2 * m + 1 + 1 + 1) → Dv (2 * m + 1 + 1 + 1) N0 →
      ∃ x : Zp (2 * m + 1 + 1), fd (ring (2 * m + 1 + 1)) x (d + 1) = (ring (2 * m + 1 + 1)).zero)
    (hc0 : ∃ c, N0 = c * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1)) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ a, N = a * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1)) →
    ∃ q, (∀ e, Dv e q → e = 1 ∨ e = q) ∧ (∃ b, q = b * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1)) ∧ Dv q N
  | 0, N, hN', _, ⟨a, ha⟩ => absurd (ha ▸ hN') (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hNG, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * (2 * d + 1 + 1 + 1) + 2 * d) with
    | ⟨l, hl', hmin⟩ =>
    have hq := lp_prime _ l hl' hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl'
    have hlN0 := dv_trans _ _ _ hlN hN0
    rcases allowedC d N0 l hl hodd hrt hq hlN0 with ⟨b, hb⟩ | ⟨b, hb⟩ | hL
    · match hlN with
      | ⟨N', hN'⟩ =>
      match cofactorC (2 * d + 1 + 1) b N' a (by rw [← hb, ← ha, hN']; lib_nat) with
      | ⟨c, hc⟩ =>
      have hlt : N' ≤ G := by
        have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
        have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
        exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hNG))
      have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
      match hasC d N0 hl hodd hrt hc0 G N' hlt hN'0 ⟨c, hc⟩ with
      | ⟨q, hq', hqL, hqN⟩ => exact ⟨q, hq', hqL, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l + 1 + 1, hq, ⟨b, hb⟩, hlN⟩
    · exfalso
      obtain ⟨c, hc⟩ := hc0
      obtain ⟨r, hr⟩ := hlN0
      rw [hL] at hr
      exact absurd (Nine.uniq_rem (2 * d + 1 + 1 + 1) c r (2 * d + 1 + 1) 0 (Nat.lt_succ_self _) (Nat.zero_lt_succ _)
        (by rw [← hc, hr]; rfl)) (fun h' => Nat.noConfusion h')

/-- The Euclid step for an abstract N₀ with n | N₀ + 1. -/
theorem infC (d : Nat) (hl : ∀ e, Dv e (2 * d + 1 + 1 + 1) → e = 1 ∨ e = 2 * d + 1 + 1 + 1) (B N0 n : Nat)
    (hn2 : Dv 2 n) (hnB : ∀ q, 1 ≤ q → q ≤ B → Dv q n) (hln : Dv (2 * d + 1 + 1 + 1) n) (hcong : Dv n (N0 + 1))
    (hrt : ∀ m, (∀ e, Dv e (2 * m + 1 + 1 + 1) → e = 1 ∨ e = 2 * m + 1 + 1 + 1) → Dv (2 * m + 1 + 1 + 1) N0 →
      ∃ x : Zp (2 * m + 1 + 1), fd (ring (2 * m + 1 + 1)) x (d + 1) = (ring (2 * m + 1 + 1)).zero) :
    ∃ b, (∀ e, Dv e (b * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1)) → e = 1 ∨ e = b * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1)) ∧
      B < b * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1) := by
  have hodd : ∀ r, N0 ≠ r * 2 := fun r hr =>
    dv_one_absurd 0 (dv_sub 1 N0 1 ⟨r, hr⟩ (dv_trans _ _ _ hn2 hcong))
  obtain ⟨c, hc⟩ := dv_trans _ _ _ hln hcong
  have hc0 : ∃ c, N0 = c * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1) := by
    match c, hc with
    | 0, hc => exact absurd (hc.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
    | c + 1, hc => exact ⟨c, Nat.succ.inj (by show N0 + 1 = c * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1) + 1; rw [hc, Nat.succ_mul]; rfl)⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqN⟩ := hasC d N0 hl hodd hrt hc0 N0 N0 (Nat.le_refl _) (dv_refl _) hc0
  refine ⟨b, by rw [← hb]; exact hq, ?_⟩
  rw [← hb]
  cases Nat.lt_or_ge B q with
  | inl h => exact h
  | inr h =>
    exfalso
    have hqn := hnB q (by rw [hb]; exact Nat.le_add_left 1 _) h
    have hq1 : q = (b * (2 * d + 1 + 1 + 1) + (2 * d + 1)) + 1 := hb
    have h1 := dv_trans _ _ _ hqn hcong
    rw [hq1] at hqN h1
    exact dv_one_absurd (b * (2 * d + 1 + 1 + 1) + 2 * d) (dv_sub _ N0 1 hqN h1)

theorem emb_dv (m N : Nat) (h : Dv (m + 1) N) : emb (ring m) N = (ring m).zero := by
  rw [emb_mk]
  exact Subtype.ext (by show md m N = md m 0; rw [dv_md_zero m N h]; rfl)

/-- **Dirichlet for p ≡ −1 (mod ℓ), every prime ℓ = 2d + 3 ≢ 1 (mod 8).** -/
theorem inf_m1 (d : Nat) (hl : ∀ e, Dv e (2 * d + 1 + 1 + 1) → e = 1 ∨ e = 2 * d + 1 + 1 + 1)
    (hd : ∃ i, d + 1 = 4 * i + 1 ∨ d + 1 = 4 * i + 2 ∨ d + 1 = 4 * i + 3) (B : Nat) :
    ∃ b, (∀ e, Dv e (b * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1)) → e = 1 ∨ e = b * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1)) ∧
      B < b * (2 * d + 1 + 1 + 1) + (2 * d + 1 + 1) := by
  obtain ⟨f, hf⟩ := fac_pos (B + 2)
  -- n = ℓ·(B+2)! = t + 2
  have ht : (2 * d + 1 + 1 + 1) * (f + 1) = (f * (2 * d + 1 + 1 + 1) + (2 * d + 1)) + 2 := by lib_nat
  generalize f * (2 * d + 1 + 1 + 1) + (2 * d + 1) = t at ht
  have hn2 : Dv 2 (t + 2) := by
    obtain ⟨r, hr⟩ := dv_fac (B + 2) 2 (Nat.le_add_left 1 1) (Nat.le_add_left 2 B)
    exact ⟨(2 * d + 1 + 1 + 1) * r, by rw [← ht, ← hf, hr]; lib_nat⟩
  have hnB : ∀ q, 1 ≤ q → q ≤ B → Dv q (t + 2) := fun q h1 h2 => by
    obtain ⟨r, hr⟩ := dv_fac (B + 2) q h1 (Nat.le_trans h2 (Nat.le_add_right B 2))
    exact ⟨(2 * d + 1 + 1 + 1) * r, by rw [← ht, ← hf, hr]; lib_nat⟩
  have hln : Dv (2 * d + 1 + 1 + 1) (t + 2) := ⟨f + 1, by rw [← ht]; lib_nat⟩
  obtain ⟨i, hi⟩ := hd
  rcases hi with h1 | h23
  · -- d + 1 = 4i + 1 = 2(2i) + 1: N₀ = N⁻
    have hfz : fd (ring (t + 1)) (ring (t + 1)).zero (2 * (2 * i) + 1) = (ring (t + 1)).one := by
      rw [show 2 * (2 * i) + 1 = 4 * i + 1 by lib_nat, fz_per, fz1]
    refine infC d hl B (Nneg t (2 * i)) (t + 2) hn2 hnB hln (neg_cong t (2 * i) hfz) (fun m _ hq => ?_)
    refine ⟨(ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) (t + 2)), ?_⟩
    rw [h1, show 4 * i + 1 = 2 * (2 * i) + 1 by lib_nat, fd_neg, emb_dv _ _ hq]
    lib_ring
  · have hfz : fd (ring (t + 1)) (ring (t + 1)).zero (d + 1) = (ring (t + 1)).neg (ring (t + 1)).one := by
      rcases h23 with h2 | h3
      · rw [h2, fz_per, fz2]
      · rw [h3, fz_per, fz3]
    refine infC d hl B (Npos t (d + 1)) (t + 2) hn2 hnB hln (pos_cong t (d + 1) hfz) (fun m _ hq => ?_)
    exact ⟨emb (ring (2 * m + 1 + 1)) (t + 2), by rw [fd_pos, emb_dv _ _ hq]⟩

end Cheb

#print axioms Cheb.geo_one_emb
#print axioms Cheb.modC
#print axioms Cheb.cofactorC
#print axioms Cheb.allowedC
#print axioms Cheb.hasC
#print axioms Cheb.infC
#print axioms Cheb.emb_dv
#print axioms Cheb.inf_m1

#print axioms: 8 theorems, no axioms.

78.118 Primes congruent to minus one mod every odd prime

For \(\ell \equiv 1\pmod 8\) the argument uses \(n\equiv c+c^{-1}\pmod \ell \) (\(c\) a non-residue) and \(n\equiv 0\) modulo the primes \(q\le B+2\), \(q\ne \ell \).

\[ \ell =2m+3\ \text {prime}\ \Longrightarrow \ \forall B\ \exists b:\ \ell b+\ell -1 \text { prime},\ B<\ell b+\ell -1. \]

lang/ChebAll.lean

/-
  lang/ChebAll — T43, cycle 408: **for every odd prime ℓ there are infinitely many primes p ≡ −1 (mod ℓ).**

  ℓ = 2m + 3. For m + 1 ≢ 0 (mod 4) this is ChebInf.inf_m1. For m + 1 ≡ 0 (mod 4) (ℓ ≡ 1 mod 8, f(0) = +1):
    x ∈ 𝔽_ℓ with f(x) = −1 (ChebM1), P = ∏_{k ≤ B+2, ℓ ∤ k} k, n = (x + ℓ)·P^(ℓ−1) = t + 2, N₀ = f(n) = N⁺ ∈ ℕ.
    In 𝔽_ℓ: n ≡ x (P^(ℓ−1) = 1), so N₀ ≡ −1. In ℤ/n: N₀ ≡ f(0) = 1, so N₀ = c·n + 1: N₀ is odd and prime to every q ≤ B + 2,
    q ≠ ℓ (q | P | n). Every prime factor has a root of f, so ChebInf.hasC gives a prime factor ≡ −1 (mod ℓ), and it exceeds B.
    pfac, pfac_pos, dv_pfac, pfac_ne, pow_pos', le_dest2, inf_m1_8, inf_all
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.ChebInf
import lang.ChebM1
import lang.PrimeOneModQ

namespace Cheb

open FTAChain ZMod YMChain ConicSum G4Gen G4Map InfSplit

def pfac (L' : Nat) : Nat → Nat
  | 0 => 1
  | k + 1 => if md L' (k + 1) = 0 then pfac L' k else (k + 1) * pfac L' k

theorem pfac_pos (L' : Nat) : ∀ k, 1 ≤ pfac L' k
  | 0 => Nat.le_refl 1
  | k + 1 => by
    show 1 ≤ (if md L' (k + 1) = 0 then pfac L' k else (k + 1) * pfac L' k)
    by_cases h : md L' (k + 1) = 0
    · rw [ite_t _ _ h]; exact pfac_pos L' k
    · rw [ite_f _ _ h]; exact Nat.mul_le_mul (Nat.le_add_left 1 k) (pfac_pos L' k)

theorem dv_pfac (L' : Nat) : ∀ B q, 1 ≤ q → q ≤ B → md L' q ≠ 0 → Dv q (pfac L' B)
  | 0, _, h1, h2, _ => absurd (Nat.le_trans h1 h2) (Nat.not_succ_le_zero 0)
  | B + 1, q, h1, h2, hq => by
    show Dv q (if md L' (B + 1) = 0 then pfac L' B else (B + 1) * pfac L' B)
    cases Nat.lt_or_ge q (B + 1) with
    | inl h =>
      obtain ⟨r, hr⟩ := dv_pfac L' B q h1 (Nat.le_of_lt_succ h) hq
      by_cases hm : md L' (B + 1) = 0
      · rw [ite_t _ _ hm]; exact ⟨r, hr⟩
      · rw [ite_f _ _ hm]; exact ⟨(B + 1) * r, by rw [hr]; lib_nat⟩
    | inr h =>
      have e : q = B + 1 := Nat.le_antisymm h2 h
      subst e
      rw [ite_f _ _ hq]
      exact ⟨pfac L' B, Nat.mul_comm _ _⟩

variable (m : Nat)

theorem pfac_ne (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∀ B, mk (2 * m + 1 + 1) (pfac (2 * m + 1 + 1) B) ≠ (ring (2 * m + 1 + 1)).zero
  | 0 => one_ne m
  | B + 1 => by
    show mk (2 * m + 1 + 1) (if md (2 * m + 1 + 1) (B + 1) = 0 then pfac (2 * m + 1 + 1) B else (B + 1) * pfac (2 * m + 1 + 1) B) ≠ _
    by_cases hm : md (2 * m + 1 + 1) (B + 1) = 0
    · rw [ite_t _ _ hm]; exact pfac_ne hpr B
    · rw [ite_f _ _ hm, ← TwoPower.mk_mul]
      intro e
      rcases mul_eq_zero m hpr _ _ e with e1 | e2
      · rw [show (ring (2 * m + 1 + 1)).zero = mk (2 * m + 1 + 1) 0 from rfl] at e1
        unfold mk at e1
        exact hm (Subtype.mk.inj e1)
      · exact pfac_ne hpr B e2

theorem pow_pos' (P : Nat) (hP : 1 ≤ P) : ∀ e, 1 ≤ P ^ e
  | 0 => Nat.le_refl 1
  | e + 1 => Nat.mul_le_mul (pow_pos' P hP e) hP

theorem le_dest2 (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

/-- **p ≡ −1 (mod ℓ) for ℓ = 2m + 3 prime with m + 1 ≡ 0 (mod 4)** (ℓ ≡ 1 mod 8). -/
theorem inf_m1_8 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hd : ∃ i, m + 1 = 4 * i) (B : Nat) :
    ∃ b, (∀ e, Dv e (b * (2 * m + 1 + 1 + 1) + (2 * m + 1 + 1)) → e = 1 ∨ e = b * (2 * m + 1 + 1 + 1) + (2 * m + 1 + 1)) ∧
      B < b * (2 * m + 1 + 1 + 1) + (2 * m + 1 + 1) := by
  obtain ⟨x, hx⟩ := exists_m1 m hpr
  have hP := pfac_pos (2 * m + 1 + 1) (B + 2)
  generalize hPdef : pfac (2 * m + 1 + 1) (B + 2) = P at hP
  have hPne : mk (2 * m + 1 + 1) P ≠ (ring (2 * m + 1 + 1)).zero := by rw [← hPdef]; exact pfac_ne m hpr (B + 2)
  have hge : 2 ≤ (x.val + (2 * m + 1 + 1 + 1)) * P ^ (2 * m + 1 + 1) :=
    Nat.le_trans (Nat.le_trans (Nat.le_add_left 2 (2 * m + 1)) (Nat.le_add_left _ x.val))
      (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm) (Nat.mul_le_mul (Nat.le_refl _) (pow_pos' P hP _)))
  obtain ⟨t, ht⟩ := le_dest2 _ _ hge
  -- n = t + 2 ≡ x (mod ℓ)
  have hemb : emb (ring (2 * m + 1 + 1)) (t + 2) = x := by
    rw [emb_mk, show t + 2 = (x.val + (2 * m + 1 + 1 + 1)) * P ^ (2 * m + 1 + 1) from ht, ← TwoPower.mk_mul,
      ← PrimeOneModQ.mk_add, ← PrimeOneModQ.mk_pow, mk_val,
      show mk (2 * m + 1 + 1) (2 * m + 1 + 1 + 1) = (ring (2 * m + 1 + 1)).zero from Subtype.ext (by show md (2 * m + 1 + 1) (2 * m + 1 + 1 + 1) = md (2 * m + 1 + 1) 0; rw [md_self]; rfl)]
    have hF : Frobenius.pw (ring (2 * m + 1 + 1)) (mk (2 * m + 1 + 1) P) (2 * m + 1 + 1) = (ring (2 * m + 1 + 1)).one := inv_exists (2 * m + 1) hpr _ hPne
    rw [hF]; lib_ring
  -- N₀ ≡ −1 (mod ℓ)
  have hcong : Dv (2 * m + 1 + 1 + 1) (Npos t (m + 1) + 1) := by
    apply dv_of_emb
    show (ring (2 * m + 1 + 1)).add (emb (ring (2 * m + 1 + 1)) (Npos t (m + 1))) (ring (2 * m + 1 + 1)).one = _
    rw [← fd_pos, hemb, hx]; lib_ring
  have hc0 : ∃ c, Npos t (m + 1) = c * (2 * m + 1 + 1 + 1) + (2 * m + 1 + 1) := by
    obtain ⟨c, hc⟩ := hcong
    match c, hc with
    | 0, hc => exact absurd (hc.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
    | c + 1, hc => exact ⟨c, Nat.succ.inj (by show Npos t (m + 1) + 1 = c * (2 * m + 1 + 1 + 1) + (2 * m + 1 + 1) + 1; rw [hc, Nat.succ_mul]; rfl)⟩
  -- N₀ ≡ f(0) = 1 (mod n)
  have hN1 : ∃ c, Npos t (m + 1) = 1 + c * (t + 1 + 1) := by
    have h := fd_pos (ring (t + 1)) t (m + 1)
    obtain ⟨i, hi⟩ := hd
    rw [emb_self, hi, show 4 * i = 4 * i + 0 from rfl, fz_per, emb_mk] at h
    have h' : mk (t + 1) 1 = mk (t + 1) (Npos t (4 * i + 0)) := h
    unfold mk at h'
    have h1 := Subtype.mk.inj h'
    rw [md_small (t + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ t))] at h1
    obtain ⟨c, hc⟩ := md_spec (t + 1) (Npos t (4 * i + 0))
    rw [← h1] at hc
    exact ⟨c, by rw [hi]; exact hc⟩
  obtain ⟨c1, hc1⟩ := hN1
  have hPn : Dv P (t + 2) := ⟨(x.val + (2 * m + 1 + 1 + 1)) * P ^ (2 * m + 1), by
    rw [ht]; show (x.val + (2 * m + 1 + 1 + 1)) * (P ^ (2 * m + 1) * P) = _
    generalize P ^ (2 * m + 1) = Y; lib_nat⟩
  -- small primes q ≠ ℓ divide n, hence not N₀
  have hsmall : ∀ q, 1 ≤ q → q ≤ B + 2 → md (2 * m + 1 + 1) q ≠ 0 → Dv q (Npos t (m + 1)) → Dv q 1 := by
    intro q h1 h2 hq hqN
    obtain ⟨s, hs⟩ := dv_trans _ _ _ (hPdef ▸ dv_pfac (2 * m + 1 + 1) (B + 2) q h1 h2 hq) hPn
    obtain ⟨r, hr⟩ := hqN
    match q, h1 with
    | q' + 1, _ =>
      exact dv_sub q' (c1 * s * (q' + 1)) 1 ⟨c1 * s, by lib_nat⟩ ⟨r, by rw [← hr, hc1, show t + 1 + 1 = t + 2 from rfl, hs]; lib_nat⟩
  have hodd : ∀ r, Npos t (m + 1) ≠ r * 2 := fun r hr => by
    have h2 : md (2 * m + 1 + 1) 2 ≠ 0 := by
      rw [md_small _ 2 (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ _)))]; exact fun h' => Nat.noConfusion h'
    exact dv_one_absurd 0 (hsmall 2 (Nat.le_add_left 1 1) (Nat.le_add_left 2 B) h2 ⟨r, hr⟩)
  have hrt : ∀ m', (∀ e, Dv e (2 * m' + 1 + 1 + 1) → e = 1 ∨ e = 2 * m' + 1 + 1 + 1) → Dv (2 * m' + 1 + 1 + 1) (Npos t (m + 1)) →
      ∃ x' : Zp (2 * m' + 1 + 1), fd (ring (2 * m' + 1 + 1)) x' (m + 1) = (ring (2 * m' + 1 + 1)).zero :=
    fun m' _ hq => ⟨emb (ring (2 * m' + 1 + 1)) (t + 2), by rw [fd_pos, emb_dv _ _ hq]⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqN⟩ := hasC m (Npos t (m + 1)) hpr hodd hrt hc0 _ _ (Nat.le_refl _) (dv_refl _) hc0
  refine ⟨b, by rw [← hb]; exact hq, ?_⟩
  rw [← hb]
  cases Nat.lt_or_ge B q with
  | inl h => exact h
  | inr h =>
    exfalso
    have hq1 : q = (b * (2 * m + 1 + 1 + 1) + (2 * m + 1)) + 1 := hb
    have hmq : md (2 * m + 1 + 1) q ≠ 0 := fun hm => by
      have hlq := md_zero_dv (2 * m + 1 + 1) q hm
      cases hq _ hlq with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e =>
        rw [← e] at hqN
        exact dv_one_absurd (2 * m + 1) (dv_sub (2 * m + 1 + 1) _ 1 hqN hcong)
    have h1 := hsmall q (by rw [hq1]; exact Nat.le_add_left 1 _) (Nat.le_trans h (Nat.le_add_right B 2)) hmq hqN
    rw [hq1] at h1
    exact dv_one_absurd (b * (2 * m + 1 + 1 + 1) + 2 * m) h1

/-- **Dirichlet for p ≡ −1 (mod ℓ), every odd prime ℓ = 2m + 3.** -/
theorem inf_all (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (B : Nat) :
    ∃ b, (∀ e, Dv e (b * (2 * m + 1 + 1 + 1) + (2 * m + 1 + 1)) → e = 1 ∨ e = b * (2 * m + 1 + 1 + 1) + (2 * m + 1 + 1)) ∧
      B < b * (2 * m + 1 + 1 + 1) + (2 * m + 1 + 1) := by
  obtain ⟨i, r, hir, hr⟩ := Order.divmod 3 (m + 1)
  match r, hr, hir with
  | 0, _, hir => exact inf_m1_8 m hpr ⟨i, by rw [hir]; lib_nat⟩ B
  | 1, _, hir => exact inf_m1 m hpr ⟨i, Or.inl (by rw [hir]; lib_nat)⟩ B
  | 2, _, hir => exact inf_m1 m hpr ⟨i, Or.inr (Or.inl (by rw [hir]; lib_nat))⟩ B
  | 3, _, hir => exact inf_m1 m hpr ⟨i, Or.inr (Or.inr (by rw [hir]; lib_nat))⟩ B
  | r + 4, hr, _ => exact absurd hr (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 r) h))

end Cheb

#print axioms Cheb.pfac_pos
#print axioms Cheb.dv_pfac
#print axioms Cheb.pfac_ne
#print axioms Cheb.pow_pos'
#print axioms Cheb.le_dest2
#print axioms Cheb.inf_m1_8
#print axioms Cheb.inf_all

#print axioms: 7 theorems, no axioms.

78.119 Primes congruent to minus one mod a prime power

With \(F=f_d\circ C_{\ell ^e}\) a root of \(F\) modulo \(q\) has order \(\ell ^{e+1}\), and \(\pm F(\pm \ell ^{e+1}(B+2)!)\equiv -1\).

\[ \ell =2d+3\ \text {prime},\ \ell \not \equiv 1\ (8)\ \Longrightarrow \ \forall B\ \exists p\ \text {prime}:\ B<p,\ \ell ^{e+1}\mid p+1. \]

lang/ChebPowInf.lean

/-
  lang/ChebPowInf — T43, cycle 411: **p ≡ −1 (mod ℓ^(e+1)) for every prime ℓ ≢ 1 (mod 8) and every e.**

  ℓ = 2d + 3, M = ℓ^e (odd), F = f_{d+1} ∘ C_M (the minimal polynomial of 2cos(2π/ℓ^(e+1))), L = ℓ^(e+1), n = L·(B + 2)! = t + 2.
  C_M(n) = c = t' + 2 ∈ ℕ (c_ge), C_M(−x) = −C_M(x) and C_M(0) = 0 (M odd), so N₀ = F(n) = N⁺(t', d+1) or −F(−n) = N⁻(t', ·) as in
  ChebInf, with F(0) = f_{d+1}(0) = ∓1 and n | N₀ + 1. Prime factors of N₀ are ≡ ±1 (mod L) (ChebRootPow.rootp) and ≠ ℓ.
    c_ge, pow_odd, C_neg_odd, Cz_odd       ℕ-facts and odd M
    hasG                                   descent for an abstract modulus L = K' + 3 and an abstract "allowed" hypothesis
    inf_pow                                ∀ B ∃ prime p > B, ℓ^(e+1) | p + 1
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.ChebRootPow
import lang.ChebAll

namespace Cheb

open FTAChain ZMod YMChain ConicSum G4Gen G4Map InfSplit

theorem c_ge (t : Nat) : ∀ k, ∃ t', (cd t k).1 = t' + 2
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    obtain ⟨t', ht'⟩ := c_ge t k
    exact ⟨t' + (cd t k).2, by show (cd t k).1 + (cd t k).2 = _; rw [ht']; lib_nat⟩

theorem pow_odd (d : Nat) : ∀ e, ∃ a, (2 * d + 1 + 1 + 1) ^ e = 2 * a + 1
  | 0 => ⟨0, rfl⟩
  | e + 1 => by
    obtain ⟨a, ha⟩ := pow_odd d e
    exact ⟨2 * a * d + 3 * a + d + 1, by show (2 * d + 1 + 1 + 1) ^ e * (2 * d + 1 + 1 + 1) = _; rw [ha]; lib_nat⟩

theorem C_neg_odd {R : Type} (K : DetChain.CRing R) (s : R) (a : Nat) : C K (K.neg s) (2 * a + 1) = K.neg (C K s (2 * a + 1)) := by
  rw [(C_neg2 K s (2 * a + 1)).1, pw_m1]; lib_ring

theorem Cz_odd {R : Type} (K : DetChain.CRing R) : ∀ a, C K K.zero (2 * a + 1) = K.zero
  | 0 => rfl
  | a + 1 => by
    rw [show 2 * (a + 1) + 1 = 2 * a + 1 + 2 by lib_nat, Cz2, Cz_odd K a]; lib_ring

/-- Descent modulo L = K' + 3 for an abstract N₀ whose prime factors are ≡ ±1 (mod L). -/
theorem hasG (K' N0 : Nat)
    (hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) N0 →
      (∃ a, l + 1 + 1 = a * (K' + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (K' + 3) + (K' + 2))) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ a, N = a * (K' + 3) + (K' + 2)) →
    ∃ q, (∀ e, Dv e q → e = 1 ∨ e = q) ∧ (∃ b, q = b * (K' + 3) + (K' + 2)) ∧ Dv q N
  | 0, N, hN', _, ⟨a, ha⟩ => absurd (ha ▸ hN') (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hNG, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * (K' + 3) + K') with
    | ⟨l, hl', hmin⟩ =>
    have hq := lp_prime _ l hl' hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl'
    have hlN0 := dv_trans _ _ _ hlN hN0
    rcases hall l hq hlN0 with ⟨b, hb⟩ | ⟨b, hb⟩
    · match hlN with
      | ⟨N', hN'⟩ =>
      match cofactorC (K' + 2) b N' a (by rw [← hb, ← ha, hN']; lib_nat) with
      | ⟨c, hc⟩ =>
      have hlt : N' ≤ G := by
        have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
        have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
        exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hNG))
      have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
      match hasG K' N0 hall G N' hlt hN'0 ⟨c, hc⟩ with
      | ⟨q, hq', hqL, hqN⟩ => exact ⟨q, hq', hqL, dv_trans q N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l + 1 + 1, hq, ⟨b, hb⟩, hlN⟩

/-- **Dirichlet for p ≡ −1 (mod ℓ^(e+1)), ℓ = 2d + 3 prime, d + 1 ≢ 0 (mod 4).** -/
theorem inf_pow (d e : Nat) (hl : ∀ k, Dv k (2 * d + 1 + 1 + 1) → k = 1 ∨ k = 2 * d + 1 + 1 + 1)
    (hd : ∃ i, d + 1 = 4 * i + 1 ∨ d + 1 = 4 * i + 2 ∨ d + 1 = 4 * i + 3) (B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ Dv ((2 * d + 1 + 1 + 1) ^ (e + 1)) (p + 1) := by
  -- L = ℓ^(e+1) = K' + 3
  have hL3 : 3 ≤ (2 * d + 1 + 1 + 1) ^ (e + 1) :=
    Nat.le_trans (Nat.le_add_left 3 (2 * d)) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm)
      (Nat.mul_le_mul (pow_pos' _ (Nat.le_add_left 1 _) e) (Nat.le_refl _)))
  obtain ⟨K', hK'⟩ := le_dest2 _ _ hL3
  obtain ⟨f, hf⟩ := fac_pos (B + 2)
  have hge : 2 ≤ (K' + 3) * (f + 1) := Nat.le_trans (Nat.le_add_left 2 (K' + 1)) (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm)
    (Nat.mul_le_mul (Nat.le_refl _) (Nat.le_add_left 1 f)))
  obtain ⟨t, ht⟩ := le_dest2 _ _ hge
  have hn2 : Dv 2 (t + 2) := by
    obtain ⟨r, hr⟩ := dv_fac (B + 2) 2 (Nat.le_add_left 1 1) (Nat.le_add_left 2 B)
    exact ⟨(K' + 3) * r, by rw [ht, ← hf, hr]; lib_nat⟩
  have hnB : ∀ q, 1 ≤ q → q ≤ B → Dv q (t + 2) := fun q h1 h2 => by
    obtain ⟨r, hr⟩ := dv_fac (B + 2) q h1 (Nat.le_trans h2 (Nat.le_add_right B 2))
    exact ⟨(K' + 3) * r, by rw [ht, ← hf, hr]; lib_nat⟩
  have hLn : Dv (K' + 3) (t + 2) := ⟨f + 1, by rw [ht]; lib_nat⟩
  have hln : Dv (2 * d + 1 + 1 + 1) (t + 2) := dv_trans _ _ _ ⟨(2 * d + 1 + 1 + 1) ^ e, by
    rw [hK']; rfl⟩ hLn
  obtain ⟨a, ha⟩ := pow_odd d e
  obtain ⟨t', ht'⟩ := c_ge t ((2 * d + 1 + 1 + 1) ^ e)
  -- the value N₀ and its root, in both sign cases
  have hcase : ∃ N0, Dv (t + 2) (N0 + 1) ∧ ∀ m, Dv (2 * m + 1 + 1 + 1) N0 →
      ∃ x : Zp (2 * m + 1 + 1), fd (ring (2 * m + 1 + 1)) (C (ring (2 * m + 1 + 1)) x ((2 * d + 1 + 1 + 1) ^ e)) (d + 1) = (ring (2 * m + 1 + 1)).zero := by
    obtain ⟨i, hi⟩ := hd
    rcases hi with h1 | h23
    · refine ⟨Nneg t' (2 * i), ?_, fun m hq => ⟨(ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) (t + 2)), ?_⟩⟩
      · apply dv_of_emb
        have h := fd_neg (ring (t + 1)) t' (2 * i)
        rw [← ht', ← C_emb, emb_self, ha, Cz_odd, show (ring (t + 1)).neg (ring (t + 1)).zero = (ring (t + 1)).zero by lib_ring,
          show 2 * (2 * i) + 1 = 4 * i + 1 by lib_nat, fz_per, fz1] at h
        show (ring (t + 1)).add (emb (ring (t + 1)) (Nneg t' (2 * i))) (ring (t + 1)).one = _
        rw [h]; lib_ring
      · rw [ha, C_neg_odd, ← ha, C_emb, ht', h1, show 4 * i + 1 = 2 * (2 * i) + 1 by lib_nat, fd_neg, emb_dv _ _ hq]
        lib_ring
    · refine ⟨Npos t' (d + 1), ?_, fun m hq => ⟨emb (ring (2 * m + 1 + 1)) (t + 2), ?_⟩⟩
      · apply dv_of_emb
        have h := fd_pos (ring (t + 1)) t' (d + 1)
        rw [← ht', ← C_emb, emb_self, ha, Cz_odd] at h
        have hfz : fd (ring (t + 1)) (ring (t + 1)).zero (d + 1) = (ring (t + 1)).neg (ring (t + 1)).one := by
          rcases h23 with h2 | h3
          · rw [h2, fz_per, fz2]
          · rw [h3, fz_per, fz3]
        rw [hfz] at h
        show (ring (t + 1)).add (emb (ring (t + 1)) (Npos t' (d + 1))) (ring (t + 1)).one = _
        rw [← h]; lib_ring
      · rw [C_emb, ht', fd_pos, emb_dv _ _ hq]
  obtain ⟨N0, hcong, hrt⟩ := hcase
  have hodd : ∀ r, N0 ≠ r * 2 := fun r hr =>
    dv_one_absurd 0 (dv_sub 1 N0 1 ⟨r, hr⟩ (dv_trans _ _ _ hn2 hcong))
  have hall : ∀ l, (∀ k, Dv k (l + 1 + 1) → k = 1 ∨ k = l + 1 + 1) → Dv (l + 1 + 1) N0 →
      (∃ b, l + 1 + 1 = b * (K' + 3) + 1) ∨ (∃ b, l + 1 + 1 = b * (K' + 3) + (K' + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨j, hj⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨j + 1, by rw [hj]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv
      obtain ⟨r, hr⟩ := hdv
      exact hodd r hr
    · subst hm
      obtain ⟨x, hx⟩ := hrt m hdv
      rcases rootp m hq (2 * d + 1) (d + 1) e hl (by lib_nat) (Fp2Sqrt.two_ne m) x hx with ⟨b, hb⟩ | ⟨b, hb⟩ | h3
      · exact Or.inl ⟨b, by rw [← hK'] at hb; rw [← hb]⟩
      · rw [← hK'] at hb
        match b, hb with
        | 0, hb => exact absurd (hb.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
        | b + 1, hb => exact Or.inr ⟨b, Nat.succ.inj (by
            show 2 * m + 1 + 1 + 1 + 1 = b * (K' + 3) + (K' + 2) + 1
            rw [hb, Nat.succ_mul]; rfl)⟩
      · exfalso
        rw [geo_one_emb] at h3
        have hdl := dv_of_emb (2 * m + 1 + 1) _ h3
        cases hl _ hdl with
        | inl e => exact Nat.noConfusion (Nat.succ.inj e)
        | inr e =>
          rw [e] at hdv
          exact dv_one_absurd (2 * d + 1) (dv_sub (2 * d + 1 + 1) N0 1 hdv (dv_trans _ _ _ hln hcong))
  obtain ⟨c, hc⟩ := dv_trans _ _ _ hLn hcong
  have hc0 : ∃ c, N0 = c * (K' + 3) + (K' + 2) := by
    match c, hc with
    | 0, hc => exact absurd (hc.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
    | c + 1, hc => exact ⟨c, Nat.succ.inj (by show N0 + 1 = c * (K' + 3) + (K' + 2) + 1; rw [hc, Nat.succ_mul]; rfl)⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqN⟩ := hasG K' N0 hall N0 N0 (Nat.le_refl _) (dv_refl _) hc0
  refine ⟨q, hq, ?_, ⟨b + 1, by rw [← hK', hb]; lib_nat⟩⟩
  cases Nat.lt_or_ge B q with
  | inl h => exact h
  | inr h =>
    exfalso
    have hqn := hnB q (by rw [hb]; exact Nat.le_add_left 1 _) h
    have hq1 : q = (b * (K' + 3) + (K' + 1)) + 1 := hb
    have h1 := dv_trans _ _ _ hqn hcong
    rw [hq1] at hqN h1
    exact dv_one_absurd (b * (K' + 3) + K') (dv_sub _ N0 1 hqN h1)

end Cheb

#print axioms Cheb.c_ge
#print axioms Cheb.pow_odd
#print axioms Cheb.C_neg_odd
#print axioms Cheb.Cz_odd
#print axioms Cheb.hasG
#print axioms Cheb.inf_pow

#print axioms: 6 theorems, no axioms.

78.120 Primes congruent to minus one mod every odd prime power

Prime factors are \(\equiv \pm 1\pmod {\ell ^{e+1}}\), so \(N_0\equiv -1\pmod \ell \) already forces a factor \(\equiv -1\pmod {\ell ^{e+1}}\).

\[ \ell =2m+3\ \text {prime}\ \Longrightarrow \ \forall e\ \forall B\ \exists p\ \text {prime}:\ B<p,\ \ell ^{e+1}\mid p+1. \]

lang/ChebPowAll.lean

/-
  lang/ChebPowAll — T43, cycle 412: **p ≡ −1 (mod ℓ^(e+1)) for every odd prime ℓ and every e.**

  Adds ℓ ≡ 1 (mod 8) to ChebPowInf. It is enough that N₀ ≡ −1 (mod ℓ): prime factors are ≡ ±1 (mod L = ℓ^(e+1)), and if all were
  ≡ 1 (mod L) then N₀ ≡ 1 (mod ℓ). So the descent runs on N ≡ −1 (mod ℓ) (hasM) and stops at a factor ≡ −1 (mod L).
  n = (x + ℓ)·P^(ℓ−1) as in ChebAll with x = c + c⁻¹ (c a non-residue): in 𝔽_ℓ, C_{ℓ^e}(x) = c^(ℓ^e) + c^(−ℓ^e) = x (frob_pow),
  so F(n) = f(C_{ℓ^e}(n)) ≡ f(x) = −1 (mod ℓ); in ℤ/n, F(n) ≡ f(C_M(0)) = f(0) = 1.
    frob_pow, exists_m1c, hasM, inf_pow8, inf_powall
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.ChebPowInf

namespace Cheb

open FTAChain ZMod YMChain ConicSum G4Gen G4Map InfSplit

variable (m : Nat)

theorem frob_pow (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∀ e (y : Zp (2 * m + 1 + 1)), Frobenius.pw (ring (2 * m + 1 + 1)) y ((2 * m + 1 + 1 + 1) ^ e) = y
  | 0, y => by show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one y = y; lib_ring
  | e + 1, y => by
    show Frobenius.pw (ring (2 * m + 1 + 1)) y ((2 * m + 1 + 1 + 1) ^ e * (2 * m + 1 + 1 + 1)) = y
    rw [← TwoPower.pwpow, frob_pow hpr e]
    exact ZMod.fermat (2 * m + 1) hpr y

theorem exists_m1c (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : ∃ c : Zp (2 * m + 1 + 1), c ≠ (ring (2 * m + 1 + 1)).zero ∧ fd (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).add c (iv m c)) (m + 1) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
  obtain ⟨c, hc0, hc1⟩ := PowerSum.exists_nonroot (2 * m + 1) hpr m (by rw [Nat.two_mul]; exact Nat.succ_le_succ (Nat.le_add_left m m))
  have hcm : Frobenius.pw (ring (2 * m + 1 + 1)) c (m + 1) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
    rcases Euler.euler_pm m hpr c hc0 with h | h
    · exact absurd h hc1
    · exact h
  have ei : (ring (2 * m + 1 + 1)).mul (iv m c) c = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hc0
  have hp := phi_fd (ring (2 * m + 1 + 1)) c (iv m c) (by rw [(ring (2 * m + 1 + 1)).mul_comm]; exact ei) (m + 1)
  have hg := geo_mul (ring (2 * m + 1 + 1)) c (2 * (m + 1) + 1)
  have hF : Frobenius.pw (ring (2 * m + 1 + 1)) c (2 * (m + 1) + 1) = c := by
    rw [show 2 * (m + 1) + 1 = 2 * m + 1 + 1 + 1 by lib_nat]
    exact ZMod.fermat (2 * m + 1) hpr c
  rw [hF] at hg
  have hc1' : (ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
    have : c = (ring (2 * m + 1 + 1)).one := by
      calc c = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [e]; lib_ring
    rw [this, TwoPower.pwone] at hc1
    exact hc1 rfl
  have hz : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add (geo (ring (2 * m + 1 + 1)) c (2 * (m + 1) + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (geo (ring (2 * m + 1 + 1)) c (2 * (m + 1) + 1))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hg]; lib_ring
  have hg1 : geo (ring (2 * m + 1 + 1)) c (2 * (m + 1) + 1) = (ring (2 * m + 1 + 1)).one := by
    rcases mul_eq_zero m hpr _ _ hz with e | e
    · exact absurd e hc1'
    · calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (geo (ring (2 * m + 1 + 1)) c (2 * (m + 1) + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
        _ = (ring (2 * m + 1 + 1)).one := by rw [e]; lib_ring
  rw [hg1, hcm] at hp
  refine ⟨c, hc0, ?_⟩
  calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (fd (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).add c (iv m c)) (m + 1))) := by lib_ring
    _ = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by rw [hp]


/-- Descent on N ≡ −1 (mod ℓ) to a prime factor ≡ −1 (mod L), when every prime factor of N₀ is ≡ ±1 (mod L) and ℓ | L. -/
theorem hasM (L N0 : Nat) (hLl : Dv (2 * m + 1 + 1 + 1) L)
    (hall : ∀ l, (∀ k, Dv k (l + 1 + 1) → k = 1 ∨ k = l + 1 + 1) → Dv (l + 1 + 1) N0 → Dv L (l + 1) ∨ Dv L (l + 1 + 1 + 1)) :
    ∀ G N, N ≤ G → Dv N N0 → (∃ a, N = a * (2 * m + 1 + 1 + 1) + (2 * m + 1 + 1)) →
    ∃ l, (∀ k, Dv k (l + 1 + 1) → k = 1 ∨ k = l + 1 + 1) ∧ Dv L (l + 1 + 1 + 1) ∧ Dv (l + 1 + 1) N
  | 0, N, hN', _, ⟨a, ha⟩ => absurd (ha ▸ hN') (fun h => Nat.not_succ_le_zero _ (Nat.le_trans (Nat.le_add_left _ _) h))
  | G + 1, N, hNG, hN0, ⟨a, ha⟩ => by
    match lp_exists (a * (2 * m + 1 + 1 + 1) + 2 * m) with
    | ⟨l, hl', hmin⟩ =>
    have hq := lp_prime _ l hl' hmin
    have hlN : Dv (l + 1 + 1) N := by rw [ha]; exact hl'
    have hlN0 := dv_trans _ _ _ hlN hN0
    rcases hall l hq hlN0 with ⟨r, hr⟩ | h1
    · obtain ⟨s, hs⟩ := hLl
      match hlN with
      | ⟨N', hN'⟩ =>
      match cofactorC (2 * m + 1 + 1) (r * s) N' a (by
          rw [show (r * s) * (2 * m + 1 + 1 + 1) + 1 = l + 1 + 1 by rw [show l + 1 + 1 = (l + 1) + 1 from rfl, hr, hs]; lib_nat, ← ha, hN']
          lib_nat) with
      | ⟨c, hc⟩ =>
      have hlt : N' ≤ G := by
        have e : N = N' + (N' * (l + 1)) := by rw [hN']; lib_nat
        have h1' : 1 ≤ N' * (l + 1) := by rw [hc]; exact Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _)
        exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' N') (by rw [← e]; exact hNG))
      have hN'0 : Dv N' N0 := dv_trans _ _ _ ⟨l + 1 + 1, by rw [hN']; lib_nat⟩ hN0
      match hasM L N0 ⟨s, hs⟩ hall G N' hlt hN'0 ⟨c, hc⟩ with
      | ⟨q, hq', hqL, hqN⟩ => exact ⟨q, hq', hqL, dv_trans _ N' N hqN ⟨l + 1 + 1, by rw [hN']; lib_nat⟩⟩
    · exact ⟨l, hq, h1, hlN⟩

/-- **p ≡ −1 (mod ℓ^(e+1)) for ℓ = 2m + 3 prime with m + 1 ≡ 0 (mod 4)** (ℓ ≡ 1 mod 8). -/
theorem inf_pow8 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hd : ∃ i, m + 1 = 4 * i) (e B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ Dv ((2 * m + 1 + 1 + 1) ^ (e + 1)) (p + 1) := by
  obtain ⟨c, hc0', hx⟩ := exists_m1c m hpr
  generalize hxdef : (ring (2 * m + 1 + 1)).add c (iv m c) = x at hx
  have hP := pfac_pos (2 * m + 1 + 1) (B + 2)
  generalize hPdef : pfac (2 * m + 1 + 1) (B + 2) = P at hP
  have hPne : mk (2 * m + 1 + 1) P ≠ (ring (2 * m + 1 + 1)).zero := by rw [← hPdef]; exact pfac_ne m hpr (B + 2)
  have hge : 2 ≤ (x.val + (2 * m + 1 + 1 + 1)) * P ^ (2 * m + 1 + 1) :=
    Nat.le_trans (Nat.le_trans (Nat.le_add_left 2 (2 * m + 1)) (Nat.le_add_left _ x.val))
      (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm) (Nat.mul_le_mul (Nat.le_refl _) (pow_pos' P hP _)))
  obtain ⟨t, ht⟩ := le_dest2 _ _ hge
  -- n = t + 2 ≡ x (mod ℓ)
  have hemb : emb (ring (2 * m + 1 + 1)) (t + 2) = x := by
    rw [emb_mk, show t + 2 = (x.val + (2 * m + 1 + 1 + 1)) * P ^ (2 * m + 1 + 1) from ht, ← TwoPower.mk_mul,
      ← PrimeOneModQ.mk_add, ← PrimeOneModQ.mk_pow, mk_val,
      show mk (2 * m + 1 + 1) (2 * m + 1 + 1 + 1) = (ring (2 * m + 1 + 1)).zero from Subtype.ext (by show md (2 * m + 1 + 1) (2 * m + 1 + 1 + 1) = md (2 * m + 1 + 1) 0; rw [md_self]; rfl)]
    have hF : Frobenius.pw (ring (2 * m + 1 + 1)) (mk (2 * m + 1 + 1) P) (2 * m + 1 + 1) = (ring (2 * m + 1 + 1)).one := inv_exists (2 * m + 1) hpr _ hPne
    rw [hF]; lib_ring
  have hCx : C (ring (2 * m + 1 + 1)) x ((2 * m + 1 + 1 + 1) ^ e) = x := by
    rw [← hxdef, ← cheb (ring (2 * m + 1 + 1)) c (iv m c) (by rw [(ring (2 * m + 1 + 1)).mul_comm]; exact iv_mul_self m hpr _ hc0') _, frob_pow m hpr, frob_pow m hpr]
  obtain ⟨a, ha⟩ := pow_odd m e
  obtain ⟨t', ht'⟩ := c_ge t ((2 * m + 1 + 1 + 1) ^ e)
  -- N₀ ≡ −1 (mod ℓ)
  have hcong : Dv (2 * m + 1 + 1 + 1) (Npos t' (m + 1) + 1) := by
    apply dv_of_emb
    show (ring (2 * m + 1 + 1)).add (emb (ring (2 * m + 1 + 1)) (Npos t' (m + 1))) (ring (2 * m + 1 + 1)).one = _
    rw [← fd_pos, ← ht', ← C_emb, hemb, hCx, hx]; lib_ring
  have hc0 : ∃ c, Npos t' (m + 1) = c * (2 * m + 1 + 1 + 1) + (2 * m + 1 + 1) := by
    obtain ⟨c, hc⟩ := hcong
    match c, hc with
    | 0, hc => exact absurd (hc.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
    | c + 1, hc => exact ⟨c, Nat.succ.inj (by show Npos t' (m + 1) + 1 = c * (2 * m + 1 + 1 + 1) + (2 * m + 1 + 1) + 1; rw [hc, Nat.succ_mul]; rfl)⟩
  -- N₀ ≡ f(0) = 1 (mod n)
  have hN1 : ∃ c, Npos t' (m + 1) = 1 + c * (t + 1 + 1) := by
    have h := fd_pos (ring (t + 1)) t' (m + 1)
    obtain ⟨i, hi⟩ := hd
    rw [← ht', ← C_emb, emb_self, ha, Cz_odd, hi, show 4 * i = 4 * i + 0 from rfl, fz_per, emb_mk] at h
    have h' : mk (t + 1) 1 = mk (t + 1) (Npos t' (4 * i + 0)) := h
    unfold mk at h'
    have h1 := Subtype.mk.inj h'
    rw [md_small (t + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ t))] at h1
    obtain ⟨c, hc⟩ := md_spec (t + 1) (Npos t' (4 * i + 0))
    rw [← h1] at hc
    exact ⟨c, by rw [hi]; exact hc⟩
  obtain ⟨c1, hc1⟩ := hN1
  have hPn : Dv P (t + 2) := ⟨(x.val + (2 * m + 1 + 1 + 1)) * P ^ (2 * m + 1), by
    rw [ht]; show (x.val + (2 * m + 1 + 1 + 1)) * (P ^ (2 * m + 1) * P) = _
    generalize P ^ (2 * m + 1) = Y; lib_nat⟩
  -- small primes q ≠ ℓ divide n, hence not N₀
  have hsmall : ∀ q, 1 ≤ q → q ≤ B + 2 → md (2 * m + 1 + 1) q ≠ 0 → Dv q (Npos t' (m + 1)) → Dv q 1 := by
    intro q h1 h2 hq hqN
    obtain ⟨s, hs⟩ := dv_trans _ _ _ (hPdef ▸ dv_pfac (2 * m + 1 + 1) (B + 2) q h1 h2 hq) hPn
    obtain ⟨r, hr⟩ := hqN
    match q, h1 with
    | q' + 1, _ =>
      exact dv_sub q' (c1 * s * (q' + 1)) 1 ⟨c1 * s, by lib_nat⟩ ⟨r, by rw [← hr, hc1, show t + 1 + 1 = t + 2 from rfl, hs]; lib_nat⟩
  have hodd : ∀ r, Npos t' (m + 1) ≠ r * 2 := fun r hr => by
    have h2 : md (2 * m + 1 + 1) 2 ≠ 0 := by
      rw [md_small _ 2 (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ _)))]; exact fun h' => Nat.noConfusion h'
    exact dv_one_absurd 0 (hsmall 2 (Nat.le_add_left 1 1) (Nat.le_add_left 2 B) h2 ⟨r, hr⟩)
  have hall : ∀ l, (∀ k, Dv k (l + 1 + 1) → k = 1 ∨ k = l + 1 + 1) → Dv (l + 1 + 1) (Npos t' (m + 1)) →
      Dv ((2 * m + 1 + 1 + 1) ^ (e + 1)) (l + 1) ∨ Dv ((2 * m + 1 + 1 + 1) ^ (e + 1)) (l + 1 + 1 + 1) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨j, hj⟩ | ⟨m', hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨j + 1, by rw [hj]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv
      obtain ⟨r, hr⟩ := hdv
      exact hodd r hr
    · subst hm
      have hx' : fd (ring (2 * m' + 1 + 1)) (C (ring (2 * m' + 1 + 1)) (emb (ring (2 * m' + 1 + 1)) (t + 2)) ((2 * m + 1 + 1 + 1) ^ e)) (m + 1)
          = (ring (2 * m' + 1 + 1)).zero := by rw [C_emb, ht', fd_pos, emb_dv _ _ hdv]
      rcases rootp m' hq (2 * m + 1) (m + 1) e hpr (by lib_nat) (Fp2Sqrt.two_ne m') _ hx' with h | h | h3
      · exact Or.inl h
      · exact Or.inr h
      · exfalso
        rw [geo_one_emb] at h3
        have hdl := dv_of_emb (2 * m' + 1 + 1) _ h3
        cases hpr _ hdl with
        | inl e => exact Nat.noConfusion (Nat.succ.inj e)
        | inr e =>
          rw [e] at hdv
          exact dv_one_absurd (2 * m + 1) (dv_sub (2 * m + 1 + 1) _ 1 hdv hcong)
  obtain ⟨q', hq, hqL, hqN⟩ := hasM m _ (Npos t' (m + 1)) ⟨(2 * m + 1 + 1 + 1) ^ e, rfl⟩ hall _ _ (Nat.le_refl _) (dv_refl _) hc0
  refine ⟨q' + 1 + 1, hq, ?_, hqL⟩
  cases Nat.lt_or_ge B (q' + 1 + 1) with
  | inl h => exact h
  | inr h =>
    exfalso
    have hmq : md (2 * m + 1 + 1) (q' + 1 + 1) ≠ 0 := fun hm => by
      have hlq := md_zero_dv (2 * m + 1 + 1) (q' + 1 + 1) hm
      cases hq _ hlq with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e =>
        rw [← e] at hqN
        exact dv_one_absurd (2 * m + 1) (dv_sub (2 * m + 1 + 1) _ 1 hqN hcong)
    have h1 := hsmall (q' + 1 + 1) (Nat.le_add_left 1 _) (Nat.le_trans h (Nat.le_add_right B 2)) hmq hqN
    exact dv_one_absurd q' h1

/-- **Dirichlet for p ≡ −1 (mod ℓ^(e+1)), every odd prime ℓ = 2m + 3, every e.** -/
theorem inf_powall (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (e B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ Dv ((2 * m + 1 + 1 + 1) ^ (e + 1)) (p + 1) := by
  obtain ⟨i, r, hir, hr⟩ := Order.divmod 3 (m + 1)
  match r, hr, hir with
  | 0, _, hir => exact inf_pow8 m hpr ⟨i, by rw [hir]; lib_nat⟩ e B
  | 1, _, hir => exact inf_pow m e hpr ⟨i, Or.inl (by rw [hir]; lib_nat)⟩ B
  | 2, _, hir => exact inf_pow m e hpr ⟨i, Or.inr (Or.inl (by rw [hir]; lib_nat))⟩ B
  | 3, _, hir => exact inf_pow m e hpr ⟨i, Or.inr (Or.inr (by rw [hir]; lib_nat))⟩ B
  | r + 4, hr, _ => exact absurd hr (fun h => Nat.lt_irrefl 4 (Nat.lt_of_le_of_lt (Nat.le_add_left 4 r) h))

end Cheb

#print axioms Cheb.frob_pow
#print axioms Cheb.exists_m1c
#print axioms Cheb.hasM
#print axioms Cheb.inf_pow8
#print axioms Cheb.inf_powall

#print axioms: 5 theorems, no axioms.

78.121 Primes congruent to minus one mod a power of two

With \(v_0=n=3\,O^2\) (\(O\) the odd factorial) and \(v_{k+1}=v_k^2-2\), every prime factor of \(v_k\) is \(\equiv \pm 1\pmod {2^{k+2}}\) and \(v_k\equiv -1\).

\[ \forall j\ \forall B\ \exists p\ \text {prime}:\ B<p,\ 2^{j+3}\mid p+1. \]

lang/Two2Inf.lean

/-
  lang/Two2Inf — T43, cycle 415: **p ≡ −1 (mod 2^e) for every e** (Euclid with v_{k+1} = v_k² − 2).

  n = 3·O² (O = 1·3·5·…·(2B + 1)), so n ≡ 3 (mod 4) and every odd q ≤ B divides n. N₀ = v_k(n), k = j + 1:
    v + 1 = (u + 1)(u + 3) for v = u² + 4u + 2 = (u + 2)² − 2, so 2^(k+2) | N₀ + 1 (doubling from n + 1 ≡ 0 mod 4);
    v_k(0) = ±2, so N₀ ≡ ±2 (mod n) and no odd q ≤ B divides N₀;
    every prime factor of N₀ is odd with a root of v_k, hence ≡ ±1 (mod 2^(k+2)) (Two2Root.root2q); ChebPowInf.hasG.
    uv, it_emb, uv_mod, itz, ofac, ofac_odd, dv_ofac, inf_2pow
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Two2Root
import lang.ChebPowInf

namespace Two2

open FTAChain ZMod YMChain ConicSum G4Gen G4Map InfSplit Cheb

def uv (u0 : Nat) : Nat → Nat
  | 0 => u0
  | k + 1 => uv u0 k * uv u0 k + (uv u0 k + uv u0 k + uv u0 k + uv u0 k)

theorem it_emb {R : Type} (K : DetChain.CRing R) (u0 : Nat) : ∀ k, it K (emb K (u0 + 2)) k = emb K (uv u0 k + 2)
  | 0 => rfl
  | k + 1 => by
    show K.add (K.mul (it K (emb K (u0 + 2)) k) (it K (emb K (u0 + 2)) k)) (K.neg (K.add K.one K.one)) =
      emb K (uv u0 k * uv u0 k + (uv u0 k + uv u0 k + uv u0 k + uv u0 k) + 2)
    rw [it_emb K u0 k]
    generalize uv u0 k = u
    rw [emb_add K (u * u + (u + u + u + u)) 2, emb_add K (u * u) (u + u + u + u), emb_mul K u u, emb_add K (u + u + u) u,
      emb_add K (u + u) u, emb_add K u u, emb_add K u 2]
    generalize emb K u = a
    show K.add (K.mul (K.add a (K.add (K.add K.zero K.one) K.one)) (K.add a (K.add (K.add K.zero K.one) K.one))) (K.neg (K.add K.one K.one)) =
      K.add (K.add (K.mul a a) (K.add (K.add (K.add a a) a) a)) (K.add (K.add K.zero K.one) K.one)
    lib_ring

theorem uv_mod (u0 : Nat) (h0 : ∃ r, u0 + 3 = r * 4) : ∀ k, ∃ r, uv u0 k + 3 = r * 2 ^ (k + 2)
  | 0 => h0
  | k + 1 => by
    obtain ⟨r, hr⟩ := uv_mod u0 h0 k
    show ∃ r, uv u0 k * uv u0 k + (uv u0 k + uv u0 k + uv u0 k + uv u0 k) + 3 = r * (2 ^ (k + 2) * 2)
    generalize uv u0 k = u at hr
    rcases EveryPrime.nat_even_odd u with ⟨s, hs⟩ | ⟨s, hs⟩
    · exfalso
      rw [hs] at hr
      exact odd_not_dv2 (s + 1) (r * 2 ^ (k + 1)) (by
        rw [show r * 2 ^ (k + 1) * 2 = r * (2 ^ (k + 1) * 2) by lib_nat]
        show 2 * (s + 1) + 1 = r * 2 ^ (k + 2); rw [← hr]; lib_nat)
    · refine ⟨(s + 1) * r, ?_⟩
      rw [hs] at hr ⊢
      generalize 2 ^ (k + 2) = X at hr ⊢
      calc _ = (2 * s + 2) * (2 * s + 1 + 3) := by lib_nat
        _ = (2 * s + 2) * (r * X) := by rw [hr]
        _ = _ := by lib_nat

theorem itz {R : Type} (K : DetChain.CRing R) : ∀ j,
    it K K.zero (j + 1) = K.neg (K.add K.one K.one) ∨ it K K.zero (j + 1) = K.add K.one K.one
  | 0 => Or.inl (by show K.add (K.mul K.zero K.zero) (K.neg (K.add K.one K.one)) = _; lib_ring)
  | j + 1 => by
    have e : it K K.zero (j + 1 + 1) = K.add (K.mul (it K K.zero (j + 1)) (it K K.zero (j + 1))) (K.neg (K.add K.one K.one)) := rfl
    rw [e]
    rcases itz K j with h | h
    · exact Or.inr (by rw [h]; lib_ring)
    · exact Or.inr (by rw [h]; lib_ring)

def ofac : Nat → Nat
  | 0 => 1
  | k + 1 => ofac k * (2 * k + 1)

theorem ofac_odd : ∀ k, ∃ a, ofac k = 2 * a + 1
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    obtain ⟨a, ha⟩ := ofac_odd k
    exact ⟨2 * a * k + a + k, by show ofac k * (2 * k + 1) = _; rw [ha]; lib_nat⟩

theorem dv_ofac : ∀ B i, i + 1 ≤ B → Dv (2 * i + 1) (ofac B)
  | 0, _, h => absurd h (Nat.not_succ_le_zero _)
  | B + 1, i, h => by
    cases Nat.lt_or_ge i B with
    | inl hl =>
      obtain ⟨r, hr⟩ := dv_ofac B i hl
      exact ⟨r * (2 * B + 1), by show ofac B * (2 * B + 1) = _; rw [hr]; lib_nat⟩
    | inr hg =>
      have e : i = B := Nat.le_antisymm (Nat.le_of_succ_le_succ h) hg
      subst e
      exact ⟨ofac i, rfl⟩

/-- **Dirichlet for p ≡ −1 (mod 2^(j+3)), every j.** -/
theorem inf_2pow (j B : Nat) : ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ Dv (2 ^ (j + 3)) (p + 1) := by
  have hL3 : 3 ≤ 2 ^ (j + 3) := Nat.le_trans (Nat.le_add_left 3 5)
    (Nat.mul_le_mul (Nat.mul_le_mul (Nat.mul_le_mul (Cheb.pow_pos' 2 (Nat.le_add_left 1 1) j) (Nat.le_refl 2)) (Nat.le_refl 2)) (Nat.le_refl 2))
  obtain ⟨K', hK'⟩ := le_dest2 _ _ hL3
  obtain ⟨a, ha⟩ := ofac_odd (B + 1)
  -- n = 3·O², O = ofac (B + 1) = 2a + 1
  have hge : 2 ≤ 3 * (ofac (B + 1) * ofac (B + 1)) := by
    rw [ha]; exact Nat.le_trans (Nat.le_add_left 2 1) (Nat.le_trans (Nat.le_of_eq (Nat.mul_one 3).symm)
      (Nat.mul_le_mul (Nat.le_refl 3) (Nat.mul_le_mul (Nat.le_add_left 1 _) (Nat.le_add_left 1 _))))
  obtain ⟨t, ht⟩ := le_dest2 _ _ hge
  have h4 : ∃ r, t + 3 = r * 4 := ⟨3 * (a * a + a) + 1, by
    have e : t + 2 = 3 * ((2 * a + 1) * (2 * a + 1)) := by rw [ht, ha]
    calc t + 3 = (t + 2) + 1 := rfl
      _ = _ := by rw [e]; lib_nat⟩
  obtain ⟨r0, hr0⟩ := uv_mod t h4 (j + 1)
  -- N₀ = v_{j+1}(n) = uv t (j+1) + 2
  have hcong : Dv (K' + 3) (uv t (j + 1) + 2 + 1) := ⟨r0, by rw [hK']; exact hr0⟩
  have hodd : ∀ r, uv t (j + 1) + 2 ≠ r * 2 := fun r hr => by
    obtain ⟨c, hc⟩ := hcong
    rw [hr] at hc
    exact odd_not_dv2 r (c * (2 ^ (j + 2))) (by
      rw [show c * 2 ^ (j + 2) * 2 = c * (2 ^ (j + 2) * 2) by lib_nat]
      show 2 * r + 1 = c * 2 ^ (j + 3); rw [← hK', ← hc]; lib_nat)
  -- small odd primes divide n, not N₀
  have hsmall : ∀ q', Dv (q' + 1) (t + 2) → Dv (q' + 1) (uv t (j + 1) + 2) → Dv (q' + 1) 2 := by
    intro q' hqn hqN
    have h := it_emb (ring (t + 1)) t (j + 1)
    rw [emb_self] at h
    rcases itz (ring (t + 1)) j with hz | hz
    · rw [hz] at h
      have hd := dv_of_emb (t + 1) (uv t (j + 1) + 2 + 2) (by
        rw [emb_add, ← h]; show (ring (t + 1)).add _ ((ring (t + 1)).add ((ring (t + 1)).add (ring (t + 1)).zero (ring (t + 1)).one) (ring (t + 1)).one) = _
        lib_ring)
      exact dv_sub q' _ 2 hqN (dv_trans _ _ _ hqn hd)
    · rw [hz] at h
      have hd := dv_of_emb (t + 1) (uv t (j + 1) + 2 + t) (by
        rw [emb_add, ← h]
        calc _ = emb (ring (t + 1)) (t + 2) := by
              rw [emb_add (ring (t + 1)) t 2]
              show (ring (t + 1)).add ((ring (t + 1)).add (ring (t + 1)).one (ring (t + 1)).one) (emb (ring (t + 1)) t) =
                (ring (t + 1)).add (emb (ring (t + 1)) t) ((ring (t + 1)).add ((ring (t + 1)).add (ring (t + 1)).zero (ring (t + 1)).one) (ring (t + 1)).one)
              lib_ring
          _ = _ := emb_self (t + 1))
      have ht' := dv_sub q' _ t hqN (dv_trans _ _ _ hqn hd)
      exact dv_sub q' t 2 ht' hqn
  have hall : ∀ l, (∀ k, Dv k (l + 1 + 1) → k = 1 ∨ k = l + 1 + 1) → Dv (l + 1 + 1) (uv t (j + 1) + 2) →
      (∃ b, l + 1 + 1 = b * (K' + 3) + 1) ∨ (∃ b, l + 1 + 1 = b * (K' + 3) + (K' + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv
      obtain ⟨r, hr⟩ := hdv
      exact hodd r hr
    · subst hm
      have hx : it (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) (t + 2)) (j + 1) = (ring (2 * m + 1 + 1)).zero := by rw [it_emb, emb_dv _ _ hdv]
      have eL : 2 ^ (j + 1 + 1 + 1) = K' + 3 := hK'.symm
      rcases root2q m hq (j + 1) (Fp2Sqrt.two_ne m) _ hx with hb | hb
      · rw [eL] at hb
        obtain ⟨b, hb⟩ := hb
        exact Or.inl ⟨b, by rw [← hb]⟩
      · rw [eL] at hb
        obtain ⟨b, hb⟩ := hb
        match b, hb with
        | 0, hb => exact absurd (hb.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
        | b + 1, hb => exact Or.inr ⟨b, Nat.succ.inj (by
            show 2 * m + 1 + 1 + 1 + 1 = b * (K' + 3) + (K' + 2) + 1
            rw [hb, Nat.succ_mul]; rfl)⟩
  obtain ⟨c, hc⟩ := hcong
  have hc0 : ∃ c, uv t (j + 1) + 2 = c * (K' + 3) + (K' + 2) := by
    match c, hc with
    | 0, hc => exact absurd (hc.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
    | c + 1, hc => exact ⟨c, Nat.succ.inj (by show uv t (j + 1) + 2 + 1 = c * (K' + 3) + (K' + 2) + 1; rw [hc, Nat.succ_mul]; rfl)⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqN⟩ := hasG K' _ hall _ _ (Nat.le_refl _) (dv_refl _) hc0
  refine ⟨q, hq, ?_, ⟨b + 1, by rw [← hK', hb]; lib_nat⟩⟩
  cases Nat.lt_or_ge B q with
  | inl h => exact h
  | inr h =>
    exfalso
    rcases EveryPrime.nat_even_odd q with ⟨i, hi⟩ | ⟨i, hi⟩
    · obtain ⟨r, hr⟩ := dv_trans 2 q _ ⟨i, by rw [hi]; lib_nat⟩ hqN
      exact hodd r hr
    · have hqO : Dv q (ofac (B + 1)) := by
        have hB : 2 * i + 1 ≤ B := by rw [hi] at h; exact h
        have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        rw [hi]; exact dv_ofac (B + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
      have hqn : Dv q (t + 2) := by
        obtain ⟨r, hr⟩ := hqO
        exact ⟨3 * ofac (B + 1) * r, by rw [ht, hr]; lib_nat⟩
      rw [hi] at hqn hqN
      have h2 := hsmall (2 * i) hqn hqN
      have hq2 : 2 ≤ 2 * i + 1 := by
        rw [← hi, hb]; exact Nat.le_trans (Nat.le_add_left 2 K') (Nat.le_add_left _ _)
      obtain ⟨r, hr⟩ := h2
      match r, hr with
      | 0, hr => exact absurd (hr.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
      | r + 1, hr =>
        have hle : 2 * i + 1 ≤ 2 := Nat.le_trans (Nat.le_add_left (2 * i + 1) (r * (2 * i + 1))) (Nat.le_of_eq ((Nat.succ_mul r (2 * i + 1)).symm.trans hr.symm))
        exact odd_not_dv2 i 1 (Nat.le_antisymm hle hq2)

end Two2

#print axioms Two2.it_emb
#print axioms Two2.uv_mod
#print axioms Two2.itz
#print axioms Two2.ofac_odd
#print axioms Two2.dv_ofac
#print axioms Two2.inf_2pow

#print axioms: 6 theorems, no axioms.

78.122 Primes congruent to minus one mod every prime power

The odd and the even cases combined in one statement.

\[ \ell \ \text {prime}\ \Longrightarrow \ \forall e\ \forall B\ \exists p\ \text {prime}:\ B<p,\ \ell ^{e+1}\mid p+1. \]

lang/MinusOnePP.lean

/-
  lang/MinusOnePP — T43, cycle 416: **for every prime power ℓ^(e+1) there are infinitely many primes p ≡ −1 (mod ℓ^(e+1)).**

  ℓ odd: ChebPowAll.inf_powall. ℓ = 2: e + 1 ≤ 2 from InfInert.inf_inert (p ≡ 3 mod 4), e + 1 ≥ 3 from Two2Inf.inf_2pow.
  One statement for all prime powers (the Euclid-type class −1; with InfCycloPow the class +1 as well).
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.ChebPowAll
import lang.Two2Inf
import lang.InfInert
import lib.Semi

namespace MinusOnePP

open FTAChain

theorem inf_m1_pp (l e : Nat) (h2 : 2 ≤ l) (hpr : ∀ k, Dv k l → k = 1 ∨ k = l) (B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ Dv (l ^ (e + 1)) (p + 1) := by
  rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨i, hi⟩
  · -- l = 2
    have hl : l = 2 := by
      cases hpr 2 ⟨i, by rw [hi]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    subst hl
    match e with
    | 0 =>
      obtain ⟨k, hk, hB⟩ := InfInert.inf_inert B
      exact ⟨_, hk, hB, ⟨2 * k + 2, by show 2 * (2 * k) + 1 + 1 + 1 + 1 = (2 * k + 2) * (1 * 2); lib_nat⟩⟩
    | 1 =>
      obtain ⟨k, hk, hB⟩ := InfInert.inf_inert B
      exact ⟨_, hk, hB, ⟨k + 1, by show 2 * (2 * k) + 1 + 1 + 1 + 1 = (k + 1) * (1 * 2 * 2); lib_nat⟩⟩
    | j + 2 => exact Two2.inf_2pow j B
  · match i, hi with
    | 0, hi => rw [hi] at h2; exact absurd h2 (Nat.not_succ_le_self 1)
    | m + 1, hi =>
      have hl : l = 2 * m + 1 + 1 + 1 := by rw [hi]; lib_nat
      subst hl
      exact Cheb.inf_powall m hpr e B

end MinusOnePP

#print axioms MinusOnePP.inf_m1_pp

#print axioms: 1 theorem, no axioms.

78.123 Primes congruent to one mod \(2^a\ell ^b\)

Every prime factor of \(\Phi _\ell (-y)\), \(y=x^{2^{a-1}\ell ^{b-1}}\), \(x=\ell (B+2)!\), is \(\equiv 1\) modulo \(2^{a}\) and \(\ell ^{b}\).

\[ \ell =2d+3\ \text {prime}\ \Longrightarrow \ \forall a,b,B\ \exists p\ \text {prime}>B:\ 2^{a+1}\mid p-1,\ \ell ^{b+1}\mid p-1. \]

lang/Cyclo2l.lean

/-
  lang/Cyclo2l — T43, cycle 417: **p ≡ 1 (mod 2^a) and p ≡ 1 (mod ℓ^b) at once** (ℓ odd prime, a, b ≥ 1), i.e. p ≡ 1 (mod 2^a ℓ^b).

  Φ_{2^a ℓ^b}(x) = Φ_{ℓ^b}(−x^(2^(a−1))), and Φ_ℓ(−y) = 1 + (y − 1)(y + y³ + … + y^(ℓ−2)) is a natural number. With
  x = ℓ·(B + 2)!, y = x^(2^(a−1) ℓ^(b−1)), N₀ = Φ_ℓ(−y) ≡ 1 (mod x). For a prime q | N₀: z = −x^(2^(a−1)) has Φ_ℓ(z^(ℓ^(b−1))) = 0, so
  ℓ^b | q − 1 (ChebPow.ordpow); z^(ℓ^b) = 1 gives (x^(ℓ^b))^(2^(a−1)) = −1, so 2^a | q − 1 (Two2.ord2).
    oddsum, oddsumR, emb_pow, emb_oddsum, geo_neg, ferm1, dv_oddsum, inf_2l
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.ChebPowAll
import lang.Two2Inf

namespace Cyclo2l

open FTAChain ZMod YMChain ConicSum G4Gen G4Map InfSplit Cheb

def oddsum (y : Nat) : Nat → Nat
  | 0 => 0
  | k + 1 => oddsum y k + y ^ (2 * k + 1)

def oddsumR {R : Type} (K : DetChain.CRing R) (u : R) : Nat → R
  | 0 => K.zero
  | k + 1 => K.add (oddsumR K u k) (Frobenius.pw K u (2 * k + 1))

theorem emb_pow {R : Type} (K : DetChain.CRing R) (a : Nat) : ∀ n, emb K (a ^ n) = Frobenius.pw K (emb K a) n
  | 0 => by show K.add K.zero K.one = K.one; lib_ring
  | n + 1 => by
    show emb K (a ^ n * a) = K.mul (Frobenius.pw K (emb K a) n) (emb K a)
    rw [emb_mul, emb_pow K a n]

theorem emb_oddsum {R : Type} (K : DetChain.CRing R) (y : Nat) : ∀ k, emb K (oddsum y k) = oddsumR K (emb K y) k
  | 0 => rfl
  | k + 1 => by
    show emb K (oddsum y k + y ^ (2 * k + 1)) = _
    rw [emb_add, emb_oddsum K y k, emb_pow]; rfl

theorem pw_neg {R : Type} (K : DetChain.CRing R) (u : R) (n : Nat) :
    Frobenius.pw K (K.neg u) n = K.mul (Frobenius.pw K (K.neg K.one) n) (Frobenius.pw K u n) := by
  rw [show K.neg u = K.mul (K.neg K.one) u by lib_ring, Frobenius.pw_mul]

theorem geo_neg {R : Type} (K : DetChain.CRing R) (u : R) : ∀ k,
    geo K (K.neg u) (2 * k + 1) = K.add K.one (K.mul (K.add u (K.neg K.one)) (oddsumR K u k))
  | 0 => by show K.add K.zero K.one = K.add K.one (K.mul (K.add u (K.neg K.one)) K.zero); lib_ring
  | k + 1 => by
    rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]
    show K.add (K.add (geo K (K.neg u) (2 * k + 1)) (Frobenius.pw K (K.neg u) (2 * k + 1))) (Frobenius.pw K (K.neg u) (2 * k + 1 + 1)) =
      K.add K.one (K.mul (K.add u (K.neg K.one)) (K.add (oddsumR K u k) (Frobenius.pw K u (2 * k + 1))))
    rw [geo_neg K u k, pw_neg K u (2 * k + 1), pw_neg K u (2 * k + 1 + 1)]
    show K.add (K.add _ (K.mul (Frobenius.pw K (K.neg K.one) (2 * k + 1)) (Frobenius.pw K u (2 * k + 1))))
      (K.mul (K.mul (Frobenius.pw K (K.neg K.one) (2 * k + 1)) (K.neg K.one)) (K.mul (Frobenius.pw K u (2 * k + 1)) u)) = _
    rw [pw_m1]
    lib_ring

theorem dv_oddsum (x y : Nat) (h : Dv x y) : ∀ k, Dv x (oddsum y k)
  | 0 => ⟨0, by show 0 = 0 * x; rw [Nat.zero_mul]⟩
  | k + 1 => by
    obtain ⟨r, hr⟩ := dv_oddsum x y h k
    obtain ⟨s, hs⟩ := h
    exact ⟨r + y ^ (2 * k) * s, by show oddsum y k + y ^ (2 * k) * y = _; rw [hr, hs]; generalize y ^ (2 * k) = Y; lib_nat⟩

variable (m : Nat)

theorem ferm1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (z : Zp (2 * m + 1 + 1)) (hz : z ≠ (ring (2 * m + 1 + 1)).zero) : Frobenius.pw (ring (2 * m + 1 + 1)) z (2 * m + 1 + 1) = (ring (2 * m + 1 + 1)).one := by
  have hF := ZMod.fermat (2 * m + 1) hpr z
  have hzz : (ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) z (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) z (2 * m + 1 + 1)) z) ((ring (2 * m + 1 + 1)).neg z) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [show (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) z (2 * m + 1 + 1)) z = z from hF]; lib_ring
  rcases mul_eq_zero m hpr _ _ hzz with e | e
  · exact absurd e hz
  · calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) z (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [e]; lib_ring

/-- **Primes p ≡ 1 (mod 2^(a+1)) and ≡ 1 (mod ℓ^(b+1)) beyond every bound** (ℓ = 2d + 3 prime). -/
theorem inf_2l (d a b B : Nat) (hl : ∀ k, Dv k (2 * d + 1 + 1 + 1) → k = 1 ∨ k = 2 * d + 1 + 1 + 1) :
    ∃ p, (∀ k, Dv k (p + 1) → k = 1 ∨ k = p + 1) ∧ B < p + 1 ∧ Dv (2 ^ (a + 1)) p ∧ Dv ((2 * d + 1 + 1 + 1) ^ (b + 1)) p := by
  obtain ⟨f, hf⟩ := fac_pos (B + 2)
  -- x = ℓ·(B+2)!
  have hx2 : Dv 2 ((2 * d + 1 + 1 + 1) * (f + 1)) := by
    obtain ⟨r, hr⟩ := dv_fac (B + 2) 2 (Nat.le_add_left 1 1) (Nat.le_add_left 2 B)
    exact ⟨(2 * d + 1 + 1 + 1) * r, by rw [← hf, hr]; lib_nat⟩
  have hxL : Dv (2 * d + 1 + 1 + 1) ((2 * d + 1 + 1 + 1) * (f + 1)) := ⟨f + 1, Nat.mul_comm _ _⟩
  have hxB : ∀ q, 1 ≤ q → q ≤ B → Dv q ((2 * d + 1 + 1 + 1) * (f + 1)) := fun q h1 h2 => by
    obtain ⟨r, hr⟩ := dv_fac (B + 2) q h1 (Nat.le_trans h2 (Nat.le_add_right B 2))
    exact ⟨(2 * d + 1 + 1 + 1) * r, by rw [← hf, hr]; lib_nat⟩
  generalize hxdef : (2 * d + 1 + 1 + 1) * (f + 1) = x at hx2 hxL hxB
  have hx3 : 3 ≤ x := by
    rw [← hxdef]; exact Nat.le_trans (Nat.le_add_left 3 (2 * d)) (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm)
      (Nat.mul_le_mul (Nat.le_refl _) (Nat.le_add_left 1 f)))
  -- y = x^E, E = 2^a ℓ^b
  have hE : 1 ≤ 2 ^ a * (2 * d + 1 + 1 + 1) ^ b :=
    Nat.mul_le_mul (Cheb.pow_pos' 2 (Nat.le_add_left 1 1) a) (Cheb.pow_pos' _ (Nat.le_add_left 1 _) b)
  obtain ⟨E', hE'⟩ := le_dest2 _ _ hE
  have hxy : Dv x (x ^ (2 ^ a * (2 * d + 1 + 1 + 1) ^ b)) := by rw [← hE']; exact ⟨x ^ E', rfl⟩
  have hy2 : 2 ≤ x ^ (2 ^ a * (2 * d + 1 + 1 + 1) ^ b) := by
    rw [← hE']; exact Nat.le_trans (Nat.le_trans (Nat.le_add_left 2 1) hx3)
      (Nat.le_trans (Nat.le_of_eq (Nat.one_mul x).symm) (Nat.mul_le_mul (Cheb.pow_pos' x (Nat.le_trans (Nat.le_add_left 1 2) hx3) E') (Nat.le_refl x)))
  obtain ⟨y', hy'⟩ := le_dest2 1 _ (Nat.le_trans (Nat.le_add_left 1 1) hy2)
  rw [← hy'] at hxy hy2
  -- N₀ = 1 + y'·S, S = y + y³ + … + y^(2d+1)
  obtain ⟨r, hr⟩ := dv_oddsum x _ hxy (d + 1)
  have hN0x : 1 + y' * oddsum (y' + 1) (d + 1) = 1 + (y' * r) * x := by rw [hr]; lib_nat
  have hS1 : 1 ≤ oddsum (y' + 1) (d + 1) :=
    Nat.le_trans (Cheb.pow_pos' _ (Nat.le_add_left 1 y') _) (Nat.le_add_left _ (oddsum (y' + 1) d))
  have hy'1 : 1 ≤ y' := Nat.le_of_succ_le_succ hy2
  have hN2 : 2 ≤ 1 + y' * oddsum (y' + 1) (d + 1) :=
    Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.mul_one 1).symm) (Nat.mul_le_mul hy'1 hS1)) 1
  generalize hNdef : 1 + y' * oddsum (y' + 1) (d + 1) = N0 at hN0x hN2
  -- q | N₀, q | x ⟹ q | 1
  have hcop : ∀ q', Dv (q' + 1 + 1) N0 → Dv (q' + 1 + 1) x → False := fun q' h1 h2 => by
    obtain ⟨s, hs⟩ := h2
    have h3 : Dv (q' + 1 + 1) ((y' * r) * x) := ⟨y' * r * s, by rw [hs]; lib_nat⟩
    have h4 : Dv (q' + 1 + 1) ((y' * r) * x + 1) := by rw [show (y' * r) * x + 1 = N0 by rw [hN0x]; lib_nat]; exact h1
    exact dv_one_absurd q' (dv_sub (q' + 1) _ 1 h3 h4)
  obtain ⟨n', hn'⟩ := le_dest2 2 N0 hN2
  obtain ⟨l, hl', hmin⟩ := lp_exists n'
  have hq := lp_prime _ l hl' hmin
  have hqN : Dv (l + 1 + 1) N0 := by rw [← hn']; exact hl'
  rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hqN
    exact hcop 0 hqN hx2
  · subst hm
    have hN0 : emb (ring (2 * m + 1 + 1)) N0 = (ring (2 * m + 1 + 1)).zero := emb_dv _ _ hqN
    have hX0 : emb (ring (2 * m + 1 + 1)) x ≠ (ring (2 * m + 1 + 1)).zero := fun e => hcop (2 * m + 1) hqN (dv_of_emb _ _ e)
    have hXq := ferm1 m hq _ hX0
    -- z = −x^(2^a)
    obtain ⟨c, hc⟩ := pow_odd d b
    have hgeo : geo (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) x) (2 ^ a))) ((2 * d + 1 + 1 + 1) ^ b)) (2 * d + 1 + 1 + 1) = (ring (2 * m + 1 + 1)).zero := by
      rw [pw_neg, hc, pw_m1, ← hc, TwoPower.pwpow, ← emb_pow, ← hy', show 2 * d + 1 + 1 + 1 = 2 * (d + 1) + 1 by lib_nat]
      have e1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (emb (ring (2 * m + 1 + 1)) (y' + 1)) = (ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) (y' + 1)) := by lib_ring
      rw [e1, geo_neg, ← emb_oddsum, ← hN0, ← hNdef, emb_add (ring (2 * m + 1 + 1)) 1 (y' * oddsum (y' + 1) (d + 1)), emb_mul (ring (2 * m + 1 + 1)) y', emb_add (ring (2 * m + 1 + 1)) y' 1]
      show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (emb (ring (2 * m + 1 + 1)) y') ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) _) =
        (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).one) _
      lib_ring
    have hz1 : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) x) (2 ^ a))) (2 * m + 1 + 1) = (ring (2 * m + 1 + 1)).one := by
      rw [pw_neg, TwoPower.pwpow, Nat.mul_comm (2 ^ a) (2 * m + 1 + 1), ← TwoPower.pwpow, hXq, TwoPower.pwone]
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (2 * m + 1)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one = _
      rw [pw_m1]; lib_ring
    have hLpart : Dv ((2 * d + 1 + 1 + 1) ^ (b + 1)) (2 * m + 1 + 1) := by
      rcases ordpow (ring (2 * m + 1 + 1)) (2 * d + 1) hl b _ _ hgeo hz1 with h | h
      · exact h
      · exfalso
        rw [geo_one_emb] at h
        have hdl := dv_of_emb _ _ h
        cases hl _ hdl with
        | inl e => exact Nat.noConfusion (Nat.succ.inj e)
        | inr e =>
          rw [e] at hqN
          exact hcop (2 * d + 1) hqN hxL
    have h2part : Dv (2 ^ (a + 1)) (2 * m + 1 + 1) := by
      have hl1 := ellth (ring (2 * m + 1 + 1)) _ _ hgeo
      rw [TwoPower.pwpow, show (2 * d + 1 + 1 + 1) ^ b * (2 * d + 1 + 1 + 1) = 2 * (c * (2 * d + 1 + 1 + 1) + d + 1) + 1 by
        rw [hc]; lib_nat, pw_neg, pw_m1, TwoPower.pwpow] at hl1
      have hw : Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) x) (2 * (c * (2 * d + 1 + 1 + 1) + d + 1) + 1)) (2 ^ a) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
        rw [TwoPower.pwpow, Nat.mul_comm (2 * (c * (2 * d + 1 + 1 + 1) + d + 1) + 1) (2 ^ a)]
        calc _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) x) (2 ^ a * (2 * (c * (2 * d + 1 + 1 + 1) + d + 1) + 1)))) := by lib_ring
          _ = _ := by rw [hl1]
      refine Two2.ord2 (ring (2 * m + 1 + 1)) (Fp2Sqrt.two_ne m) a _ _ hw ?_
      rw [TwoPower.pwpow, Nat.mul_comm (2 * (c * (2 * d + 1 + 1 + 1) + d + 1) + 1) (2 * m + 1 + 1), ← TwoPower.pwpow, hXq, TwoPower.pwone]
    refine ⟨2 * m + 1 + 1, hq, ?_, h2part, hLpart⟩
    cases Nat.lt_or_ge B (2 * m + 1 + 1 + 1) with
    | inl h => exact h
    | inr h => exact absurd (hxB _ (Nat.le_add_left 1 _) h) (fun hd => hcop (2 * m + 1) hqN hd)

end Cyclo2l

#print axioms Cyclo2l.emb_pow
#print axioms Cyclo2l.emb_oddsum
#print axioms Cyclo2l.pw_neg
#print axioms Cyclo2l.geo_neg
#print axioms Cyclo2l.dv_oddsum
#print axioms Cyclo2l.ferm1
#print axioms Cyclo2l.inf_2l

#print axioms: 7 theorems, no axioms.

78.124 Primes congruent to one mod \(\ell _1^a\ell _2^b\)

\(A=\Phi _{\ell _1}(y^{\ell _2})\) and \(C=\Phi _{\ell _2}(y^{\ell _1})\) have a common prime factor because \(A\,\Phi _{\ell _2}(y)=\Phi _{\ell _1}(y)\,C\) and \(A>\Phi _{\ell _1}(y)\).

\[ \ell _1,\ell _2\ \text {prime}\ \Longrightarrow \ \forall a,b,B\ \exists p\ \text {prime}>B:\ \ell _1^{a+1}\mid p-1,\ \ell _2^{b+1}\mid p-1. \]

lang/TwoPrimeInf.lean

/-
  lang/TwoPrimeInf — T43, cycle 419: **p ≡ 1 (mod ℓ₁^(a+1)) and p ≡ 1 (mod ℓ₂^(b+1)) beyond every bound**, ℓ₁, ℓ₂ any primes.

  x = ℓ₁ℓ₂·(B + 2)!, y = x^(ℓ₁^a ℓ₂^b), A = Φ_{ℓ₁}(y^{ℓ₂}), C = Φ_{ℓ₂}(y^{ℓ₁}), D = Φ_{ℓ₁}(y), B' = Φ_{ℓ₂}(y) (as geometric sums).
  A·B' = gN(y, ℓ₁ℓ₂) = D·C and A > D, so (TwoPrime.copr_div) some prime r divides both A and C. A ≡ 1 (mod x), so r ∤ x, r > B.
  In 𝔽_r: Φ_{ℓ₁}((x^(ℓ₂^(b+1)))^(ℓ₁^a)) = 0 gives ℓ₁^(a+1) | r − 1, and symmetrically ℓ₂^(b+1) | r − 1 (ChebPow.ordpow).
    pow_mono, gN_gt, gN_one, inf_two
  With Cyclo2l (2^a ℓ^b) this gives p ≡ 1 modulo any modulus with at most two prime factors, without Φ_N.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.TwoPrime

namespace TwoPrime

open FTAChain ZMod YMChain ConicSum G4Gen G4Map InfSplit Cheb

theorem pow_mono (y δ : Nat) : ∀ n, ∃ e, (y + δ) ^ n = y ^ n + e
  | 0 => ⟨0, rfl⟩
  | n + 1 => by
    obtain ⟨e, he⟩ := pow_mono y δ n
    exact ⟨e * y + (y ^ n + e) * δ, by show (y + δ) ^ n * (y + δ) = y ^ n * y + _; rw [he]; generalize y ^ n = U; lib_nat⟩

theorem gN_gt (y δ : Nat) : ∀ k, ∃ e, gN (y + δ) (k + 1 + 1) = gN y (k + 1 + 1) + δ + e
  | 0 => ⟨0, by show 0 + (y + δ) ^ 0 + (y + δ) ^ 1 = 0 + y ^ 0 + y ^ 1 + δ + 0; show 0 + 1 + 1 * (y + δ) = 0 + 1 + 1 * y + δ + 0; lib_nat⟩
  | k + 1 => by
    obtain ⟨e, he⟩ := gN_gt y δ k
    obtain ⟨e', he'⟩ := pow_mono y δ (k + 1 + 1)
    exact ⟨e + e', by show gN (y + δ) (k + 1 + 1) + (y + δ) ^ (k + 1 + 1) = gN y (k + 1 + 1) + y ^ (k + 1 + 1) + δ + (e + e'); rw [he, he']; lib_nat⟩

theorem gN_one (w : Nat) (p : Nat) : gN w (1 + p) = 1 + w * gN w p := by
  rw [gN_add]; show 0 + 1 + w ^ 1 * gN w p = _; rw [show w ^ 1 = 1 * w from rfl, Nat.one_mul]

/-- A·B = D·C, D ≥ 1: either a prime divides both A and C, or A | D (constructive, deciding divisibility at each prime). -/
theorem common_or_dv (C B : Nat) : ∀ G A D, A ≤ G → 1 ≤ A → 1 ≤ D → A * B = D * C →
    (∃ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) ∧ Dv (l + 1 + 1) A ∧ Dv (l + 1 + 1) C) ∨ Dv A D
  | 0, _, _, hAG, hA1, _, _ => absurd (Nat.le_trans hA1 hAG) (Nat.not_succ_le_zero 0)
  | G + 1, A, D, hAG, hA1, hD1, hABDC => by
    obtain ⟨A', hA'⟩ := le_dest2 1 A hA1
    match A', hA' with
    | 0, hA' => exact Or.inr ⟨D, by rw [← hA']; lib_nat⟩
    | n + 1, hA' =>
      obtain ⟨l, hl, hmin⟩ := lp_exists n
      have hpr := lp_prime _ l hl hmin
      have hlA : Dv (l + 1 + 1) A := by rw [← hA']; show Dv (l + 1 + 1) (n + 1 + 1); exact hl
      rcases dv_dec (l + 1) C with hC | hC
      · exact Or.inl ⟨l, hpr, hlA, hC⟩
      · obtain ⟨D', hD'⟩ := le_dest2 1 D hD1
        have hDC : Dv (l + 1 + 1) ((D' + 1) * C) := by
          obtain ⟨s, hs⟩ := hlA
          exact ⟨s * B, by rw [hD', ← hABDC, hs]; lib_nat⟩
        rcases euclid_core (l) D' C hpr hDC with hD | hC'
        · obtain ⟨A2, hA2⟩ := hlA
          obtain ⟨D2, hD2⟩ := hD
          rw [hD'] at hD2
          have hcan : A2 * B = D2 * C := by
            apply mul_cancel (l + 1)
            calc (l + 1 + 1) * (A2 * B) = A * B := by rw [hA2]; lib_nat
              _ = D * C := hABDC
              _ = _ := by rw [hD2]; lib_nat
          have hA2pos : 1 ≤ A2 := by
            match A2, hA2 with
            | 0, hA2 => exact absurd (hA2.trans (Nat.zero_mul _)) (fun h => by rw [← hA'] at h; exact Nat.noConfusion h)
            | k + 1, _ => exact Nat.le_add_left 1 k
          have hD2pos : 1 ≤ D2 := by
            match D2, hD2 with
            | 0, hD2 => rw [hD2, Nat.zero_mul] at hD1; exact absurd hD1 (Nat.not_succ_le_zero 0)
            | k + 1, _ => exact Nat.le_add_left 1 k
          have hlt : A2 ≤ G := by
            have e : A = A2 + A2 * (l + 1) := by rw [hA2]; lib_nat
            have h1' : 1 ≤ A2 * (l + 1) := Nat.mul_le_mul hA2pos (Nat.le_add_left 1 l)
            exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1' A2) (by rw [← e]; exact hAG))
          rcases common_or_dv C B G A2 D2 hlt hA2pos hD2pos hcan with ⟨l', hp', h1, h2⟩ | ⟨r, hr⟩
          · exact Or.inl ⟨l', hp', dv_trans _ _ _ h1 ⟨l + 1 + 1, by rw [hA2]; lib_nat⟩, h2⟩
          · exact Or.inr ⟨r, by rw [hD2, hA2, hr]; lib_nat⟩
        · exact absurd hC' hC

/-- **Primes ≡ 1 modulo ℓ₁^(a+1) and ℓ₂^(b+1) beyond every bound.** -/
theorem inf_two (j1 j2 a b B : Nat) (hl1 : ∀ k, Dv k (j1 + 1 + 1) → k = 1 ∨ k = j1 + 1 + 1)
    (hl2 : ∀ k, Dv k (j2 + 1 + 1) → k = 1 ∨ k = j2 + 1 + 1) :
    ∃ p, (∀ k, Dv k (p + 1) → k = 1 ∨ k = p + 1) ∧ B < p + 1 ∧ Dv ((j1 + 1 + 1) ^ (a + 1)) p ∧ Dv ((j2 + 1 + 1) ^ (b + 1)) p := by
  obtain ⟨f, hf⟩ := fac_pos (B + 2)
  have hx2 : Dv 2 ((j1 + 1 + 1) * (j2 + 1 + 1) * (f + 1)) := by
    obtain ⟨r, hr⟩ := dv_fac (B + 2) 2 (Nat.le_add_left 1 1) (Nat.le_add_left 2 B)
    exact ⟨(j1 + 1 + 1) * (j2 + 1 + 1) * r, by rw [← hf, hr]; lib_nat⟩
  have hx1 : Dv (j1 + 1 + 1) ((j1 + 1 + 1) * (j2 + 1 + 1) * (f + 1)) := ⟨(j2 + 1 + 1) * (f + 1), by lib_nat⟩
  have hxl2 : Dv (j2 + 1 + 1) ((j1 + 1 + 1) * (j2 + 1 + 1) * (f + 1)) := ⟨(j1 + 1 + 1) * (f + 1), by lib_nat⟩
  have hxB : ∀ q, 1 ≤ q → q ≤ B + 2 → Dv q ((j1 + 1 + 1) * (j2 + 1 + 1) * (f + 1)) := fun q h1 h2 => by
    obtain ⟨r, hr⟩ := dv_fac (B + 2) q h1 h2
    exact ⟨(j1 + 1 + 1) * (j2 + 1 + 1) * r, by rw [← hf, hr]; lib_nat⟩
  generalize hxdef : (j1 + 1 + 1) * (j2 + 1 + 1) * (f + 1) = x at hx2 hx1 hxl2 hxB
  have hx4 : 2 ≤ x := by
    rw [← hxdef]; exact Nat.le_trans (Nat.le_add_left 2 j1) (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm)
      (Nat.mul_le_mul (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm) (Nat.mul_le_mul (Nat.le_refl _) (Nat.le_add_left 1 (j2 + 1))))
        (Nat.le_add_left 1 f)))
  have hE : 1 ≤ (j1 + 1 + 1) ^ a * (j2 + 1 + 1) ^ b :=
    Nat.mul_le_mul (Cheb.pow_pos' _ (Nat.le_add_left 1 _) a) (Cheb.pow_pos' _ (Nat.le_add_left 1 _) b)
  obtain ⟨E', hE'⟩ := le_dest2 _ _ hE
  have hxy : Dv x (x ^ ((j1 + 1 + 1) ^ a * (j2 + 1 + 1) ^ b)) := by rw [← hE']; exact ⟨x ^ E', rfl⟩
  have hy2 : 2 ≤ x ^ ((j1 + 1 + 1) ^ a * (j2 + 1 + 1) ^ b) := by
    rw [← hE']; exact Nat.le_trans hx4 (Nat.le_trans (Nat.le_of_eq (Nat.one_mul x).symm)
      (Nat.mul_le_mul (Cheb.pow_pos' x (Nat.le_trans (Nat.le_add_left 1 1) hx4) E') (Nat.le_refl x)))
  generalize hydef : x ^ ((j1 + 1 + 1) ^ a * (j2 + 1 + 1) ^ b) = y at hxy hy2
  -- A·B' = D·C
  have hABDC : gN (y ^ (j2 + 1 + 1)) (j1 + 1 + 1) * gN y (j2 + 1 + 1) = gN y (j1 + 1 + 1) * gN (y ^ (j1 + 1 + 1)) (j2 + 1 + 1) := by
    calc _ = gN y (j2 + 1 + 1) * gN (y ^ (j2 + 1 + 1)) (j1 + 1 + 1) := Nat.mul_comm _ _
      _ = gN y ((j2 + 1 + 1) * (j1 + 1 + 1)) := (gN_split y _ _).symm
      _ = gN y ((j1 + 1 + 1) * (j2 + 1 + 1)) := by rw [Nat.mul_comm (j2 + 1 + 1) (j1 + 1 + 1)]
      _ = _ := gN_split y _ _
  -- A ≡ 1 (mod x)
  have hA1 : ∃ K, gN (y ^ (j2 + 1 + 1)) (j1 + 1 + 1) = 1 + K * x := by
    obtain ⟨s, hs⟩ := hxy
    refine ⟨y ^ (j2 + 1) * s * gN (y ^ (j2 + 1 + 1)) (j1 + 1), ?_⟩
    rw [show j1 + 1 + 1 = 1 + (j1 + 1) by lib_nat, gN_one]
    show 1 + y ^ (j2 + 1) * y * gN (y ^ (j2 + 1 + 1)) (j1 + 1) = _
    rw [hs]; generalize y ^ (j2 + 1) = U; generalize gN (y ^ (j2 + 1 + 1)) (j1 + 1) = V; lib_nat
  obtain ⟨K, hK⟩ := hA1
  have hcop : ∀ q', Dv (q' + 1 + 1) (gN (y ^ (j2 + 1 + 1)) (j1 + 1 + 1)) → Dv (q' + 1 + 1) x → False := fun q' h1 h2 => by
    obtain ⟨s, hs⟩ := h2
    rw [hK] at h1
    exact dv_one_absurd q' (dv_sub (q' + 1) (K * x) 1 ⟨K * s, by rw [hs]; lib_nat⟩ (by rw [Nat.add_comm (K * x) 1]; exact h1))
  -- a common prime of A and C
  obtain ⟨y0, hy0⟩ := le_dest2 2 y hy2
  have hD1 : 1 ≤ gN y (j1 + 1 + 1) := by
    rw [show j1 + 1 + 1 = 1 + (j1 + 1) by lib_nat, gN_one]; exact Nat.le_add_right 1 _
  obtain ⟨e1, he1⟩ := le_dest2 1 (y ^ j2) (Cheb.pow_pos' y (Nat.le_trans (Nat.le_add_left 1 1) hy2) j2)
  have hyl : y ^ (j2 + 1 + 1) = y + ((e1 * y * y) + y * (y0 + 1)) := by
    show y ^ j2 * y * y = _; rw [← he1]; rw [← hy0]; lib_nat
  obtain ⟨g, hg⟩ := gN_gt y ((e1 * y * y) + y * (y0 + 1)) j1
  rw [← hyl] at hg
  have hcommon : ∃ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) ∧ Dv (l + 1 + 1) (gN (y ^ (j2 + 1 + 1)) (j1 + 1 + 1)) ∧
      Dv (l + 1 + 1) (gN (y ^ (j1 + 1 + 1)) (j2 + 1 + 1)) := by
    rcases common_or_dv _ _ _ _ _ (Nat.le_refl _) (Nat.le_trans hD1 (by rw [hg]; exact Nat.le_trans (Nat.le_add_right _ _) (Nat.le_add_right _ _))) hD1 hABDC with h | ⟨k, hk⟩
    · exact h
    · exfalso
      match k, hk with
      | 0, hk => rw [hk, Nat.zero_mul] at hD1; exact absurd hD1 (Nat.not_succ_le_zero 0)
      | k + 1, hk =>
        have h1 : gN (y ^ (j2 + 1 + 1)) (j1 + 1 + 1) ≤ gN y (j1 + 1 + 1) := by
          rw [hk]; exact Nat.le_trans (Nat.le_add_left _ (k * _)) (Nat.le_of_eq (Nat.succ_mul k _).symm)
        rw [hg] at h1
        have h2 : 1 ≤ (e1 * y * y) + y * (y0 + 1) := by
          rw [← hy0]; exact Nat.le_trans (Nat.le_trans (Nat.le_of_eq (Nat.mul_one 1).symm) (Nat.mul_le_mul (Nat.le_add_left 1 (y0 + 1)) (Nat.le_add_left 1 y0))) (Nat.le_add_left _ _)
        exact Nat.not_succ_le_self _ (Nat.le_trans (Nat.add_le_add_left h2 _) (Nat.le_trans (Nat.le_add_right _ g) h1))
  obtain ⟨l, hq, hlA, hlC⟩ := hcommon
  rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : l + 1 + 1 = 2 := by
      cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hlA
    exact hcop 0 hlA hx2
  · subst hm
    have hX0 : emb (ring (2 * m + 1 + 1)) x ≠ (ring (2 * m + 1 + 1)).zero := fun e => hcop (2 * m + 1) hlA (dv_of_emb _ _ e)
    have hXq := Cyclo2l.ferm1 m hq _ hX0
    have hpowz : ∀ n, Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) x) n) (2 * m + 1 + 1) = (ring (2 * m + 1 + 1)).one := fun n => by
      rw [TwoPower.pwpow, Nat.mul_comm n (2 * m + 1 + 1), ← TwoPower.pwpow, hXq, TwoPower.pwone]
    have hembA : emb (ring (2 * m + 1 + 1)) (y ^ (j2 + 1 + 1)) = Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) x) ((j2 + 1 + 1) ^ (b + 1))) ((j1 + 1 + 1) ^ a) := by
      rw [← hydef, Cyclo2l.emb_pow, Cyclo2l.emb_pow, TwoPower.pwpow, TwoPower.pwpow]
      congr 1
      show (j1 + 1 + 1) ^ a * (j2 + 1 + 1) ^ b * (j2 + 1 + 1) = (j2 + 1 + 1) ^ b * (j2 + 1 + 1) * (j1 + 1 + 1) ^ a
      generalize (j1 + 1 + 1) ^ a = P; generalize (j2 + 1 + 1) ^ b = Q; lib_nat
    have hembC : emb (ring (2 * m + 1 + 1)) (y ^ (j1 + 1 + 1)) = Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) x) ((j1 + 1 + 1) ^ (a + 1))) ((j2 + 1 + 1) ^ b) := by
      rw [← hydef, Cyclo2l.emb_pow, Cyclo2l.emb_pow, TwoPower.pwpow, TwoPower.pwpow]
      congr 1
      show (j1 + 1 + 1) ^ a * (j2 + 1 + 1) ^ b * (j1 + 1 + 1) = (j1 + 1 + 1) ^ a * (j1 + 1 + 1) * (j2 + 1 + 1) ^ b
      generalize (j1 + 1 + 1) ^ a = P; generalize (j2 + 1 + 1) ^ b = Q; lib_nat
    have hgA : geo (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) x) ((j2 + 1 + 1) ^ (b + 1))) ((j1 + 1 + 1) ^ a)) (j1 + 1 + 1) = (ring (2 * m + 1 + 1)).zero := by
      rw [← hembA, ← emb_gN]; exact emb_dv _ _ hlA
    have hgC : geo (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) x) ((j1 + 1 + 1) ^ (a + 1))) ((j2 + 1 + 1) ^ b)) (j2 + 1 + 1) = (ring (2 * m + 1 + 1)).zero := by
      rw [← hembC, ← emb_gN]; exact emb_dv _ _ hlC
    have hpart1 : Dv ((j1 + 1 + 1) ^ (a + 1)) (2 * m + 1 + 1) := by
      rcases ordpow (ring (2 * m + 1 + 1)) j1 hl1 a _ _ hgA (hpowz _) with h | h
      · exact h
      · exfalso
        rw [geo_one_emb] at h
        cases hl1 _ (dv_of_emb _ _ h) with
        | inl e => exact Nat.noConfusion (Nat.succ.inj e)
        | inr e => rw [e] at hlA; exact hcop j1 hlA hx1
    have hpart2 : Dv ((j2 + 1 + 1) ^ (b + 1)) (2 * m + 1 + 1) := by
      rcases ordpow (ring (2 * m + 1 + 1)) j2 hl2 b _ _ hgC (hpowz _) with h | h
      · exact h
      · exfalso
        rw [geo_one_emb] at h
        cases hl2 _ (dv_of_emb _ _ h) with
        | inl e => exact Nat.noConfusion (Nat.succ.inj e)
        | inr e =>
          rw [e] at hlA
          exact hcop j2 hlA hxl2
    refine ⟨2 * m + 1 + 1, hq, ?_, hpart1, hpart2⟩
    cases Nat.lt_or_ge B (2 * m + 1 + 1 + 1) with
    | inl h => exact h
    | inr h => exact absurd (hxB _ (Nat.le_add_left 1 _) (Nat.le_trans h (Nat.le_add_right B 2))) (fun hd => hcop (2 * m + 1) hlA hd)

end TwoPrime

#print axioms TwoPrime.pow_mono
#print axioms TwoPrime.gN_gt
#print axioms TwoPrime.gN_one
#print axioms TwoPrime.common_or_dv
#print axioms TwoPrime.inf_two

#print axioms: 5 theorems, no axioms.

78.125 Primes congruent to one mod a squarefree \(n\)

For \(n=q_1\cdots q_k\) with distinct primes \(q_i\), every prime factor of the primitive part \(F\) of \(x^n-1\), \(x=n\,(B+c+2)!\), is \(\equiv 1\pmod n\) and larger than \(B\).

\[ q_1,\dots ,q_k\ \text {distinct primes},\ n=q_1\cdots q_k\ \Longrightarrow \ \forall B\ \exists p\ \text {prime}>B:\ n\mid p-1. \]

lang/PhiSqInf.lean

/-
  lang/PhiSqInf — T43, cycle 488: **p ≡ 1 (mod n) beyond every bound, for every squarefree n ≥ 2** (step I6).

  n = Π q over a list q :: L of distinct primes (PhiSq.Good), c = PhiSq.cs L, x = n·(B + c + 2)!.
  PhiSq.sqfree gives F with Inv(n, x, F) and x^φ ≤ c·F; x^φ ≥ x > c gives F ≥ 2 (PhiSize.two_le), so F has a prime factor r.
  Inv: x has order n in 𝔽_r and r | gN(x, n) = 1 + x·gN(x, n − 1), so r ∤ x, hence r > B + c + 2 and r odd;
  Fermat (Cyclo2l.ferm1) gives x^(r−1) = 1, so n | r − 1.
    allDv_mul, allDv_pr, ph_pos, pow_ge, dv_pow1   small facts
    inf_sq                                          ∀ B ∃ prime p + 1 > B with n | p
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PhiSq
import lang.TwoPrimeInf
import lang.Cyclo2l

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv InfSplit

theorem allDv_mul (z : Nat) : ∀ L y, AllDv L y → AllDv L (z * y)
  | [], _, _ => trivial
  | _ :: L, y, ⟨h, hL⟩ => ⟨dv_mul_left _ _ _ h, allDv_mul z L y hL⟩

theorem allDv_pr : ∀ L, AllDv L (pr L)
  | [] => trivial
  | j :: L => ⟨⟨pr L, rfl⟩, by
      show AllDv L (pr L * (j + 1 + 1))
      rw [Nat.mul_comm]; exact allDv_mul _ L _ (allDv_pr L)⟩

theorem ph_pos (j : Nat) : ∀ L, 1 ≤ ph j L
  | [] => Nat.le_add_left 1 j
  | k :: L => Nat.le_trans (ph_pos k L) (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm)
      (Nat.mul_le_mul_left _ (Nat.le_add_left 1 j)))

theorem pow_ge (x : Nat) (hx : 1 ≤ x) (e : Nat) (he : 1 ≤ e) : x ≤ x ^ e := by
  obtain ⟨e', he'⟩ := le_dest2 1 e he
  rw [← he']
  show x ≤ x ^ e' * x
  exact Nat.le_trans (Nat.le_of_eq (Nat.one_mul x).symm) (Nat.mul_le_mul (pow_pos' x hx e') (Nat.le_refl x))

theorem dv_pow1 (r F : Nat) (h : Dv r F) : Dv (r ^ (0 + 1)) F := by
  obtain ⟨q, hq⟩ := h
  exact ⟨q, hq.trans (congrArg (q * ·) (Nat.one_mul r).symm)⟩

theorem inf_sq (j : Nat) (L : List Nat) (hG : Good (j :: L)) (B : Nat) :
    ∃ p, (∀ k, Dv k (p + 1) → k = 1 ∨ k = p + 1) ∧ B < p + 1 ∧ Dv (pr (j :: L)) p := by
  obtain ⟨f, hf⟩ := fac_pos (B + cs L + 2)
  have hxB : ∀ q, 1 ≤ q → q ≤ B + cs L + 2 → Dv q (pr (j :: L) * (f + 1)) := fun q h1 h2 => by
    rw [← hf]; exact dv_mul_left _ _ _ (dv_fac _ q h1 h2)
  have hxA : AllDv (j :: L) (pr (j :: L) * (f + 1)) := by
    rw [Nat.mul_comm]; exact allDv_mul _ _ _ (allDv_pr (j :: L))
  have hx1 : 1 ≤ pr (j :: L) * (f + 1) := Nat.mul_le_mul (pr_pos (j :: L)) (Nat.le_add_left 1 f)
  generalize hxdef : pr (j :: L) * (f + 1) = x at hxB hxA hx1
  -- c + 2 ≤ x
  have hcx : cs L + 2 ≤ x := by
    obtain ⟨x', hx'⟩ := le_dest2 1 x hx1
    have hd := hxB (cs L + 2) (Nat.le_add_left 1 (cs L + 1)) (Nat.le_add_left _ B)
    rw [← hx'] at hd
    obtain ⟨k, hk⟩ := dv_le _ x' hd
    rw [← hx', ← hk]; exact Nat.le_add_right _ _
  obtain ⟨x'', hx''⟩ := le_dest2 2 x (Nat.le_trans (Nat.le_add_left 2 (cs L)) hcx)
  rw [← hx''] at hxA
  obtain ⟨F, I, l, _⟩ := sqfree j L hG x'' hxA
  rw [show x'' + 1 + 1 = x from hx''] at I l
  have hC : cs L < x ^ ph j L :=
    Nat.le_trans (Nat.le_add_right (cs L + 1) 1) (Nat.le_trans hcx (pow_ge x hx1 _ (ph_pos j L)))
  have hF2 := PhiSize.two_le _ _ _ l hC
  obtain ⟨F', hF'⟩ := le_dest2 2 F hF2
  obtain ⟨r, hrF, hmin⟩ := lp_exists F'
  have hpr := lp_prime _ r hrF hmin
  rw [hF'] at hrF
  obtain ⟨hO, hg⟩ := (I r 0 hpr).1 (dv_pow1 _ _ hrF)
  -- r ∤ x
  have hn2 : 2 ≤ pr (j :: L) := Nat.le_trans (Nat.le_add_left 2 j) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm)
    (Nat.mul_le_mul_right (j + 1 + 1) (pr_pos L)))
  obtain ⟨n', hn'⟩ := le_dest2 1 _ (Nat.le_trans (Nat.le_add_left 1 1) hn2)
  have hrx : Dv (r + 1 + 1) x → False := fun hd => by
    have hg1 : Dv (r + 1 + 1) (gN x (pr (j :: L))) := dv_base _ 0 _ hg
    rw [← hn', Nat.add_comm n' 1, gN_one] at hg1
    exact dv_one_absurd r (dv_sub (r + 1) (x * gN x n') 1 (dv_trans _ _ _ hd ⟨gN x n', Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (x * gN x n') 1]; exact hg1))
  rcases EveryPrime.nat_even_odd r with ⟨i, hi⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : r + 1 + 1 = 2 := by
      cases hpr 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hrx
    exact hrx (hxB 2 (Nat.le_add_left 1 1) (Nat.le_add_left 2 _))
  · subst hm
    have hX0 : emb (ring (2 * m + 1 + 1)) x ≠ (ring (2 * m + 1 + 1)).zero := fun e => hrx (dv_of_emb _ _ e)
    have hXq := Cyclo2l.ferm1 m hpr _ hX0
    refine ⟨2 * m + 1 + 1, hpr, ?_, hO.2 _ hXq⟩
    cases Nat.lt_or_ge B (2 * m + 1 + 1 + 1) with
    | inl h => exact h
    | inr h => exact absurd (hxB _ (Nat.le_add_left 1 _) (Nat.le_trans h (Nat.le_trans (Nat.le_add_right B (cs L)) (Nat.le_add_right _ 2)))) hrx

end PhiSq

#print axioms PhiSq.allDv_mul
#print axioms PhiSq.allDv_pr
#print axioms PhiSq.ph_pos
#print axioms PhiSq.pow_ge
#print axioms PhiSq.dv_pow1
#print axioms PhiSq.inf_sq

#print axioms: 6 theorems, no axioms.

78.126 Primes congruent to one mod \(n^{a+1}\), \(n\) squarefree

The squarefree construction at \(y=x^{n^a}\) gives primes \(r\) with \(\operatorname {ord}_r y=n\), hence \(\operatorname {ord}_r x=n^{a+1}\).

\[ n\ \text {squarefree},\ n\ge 2\ \Longrightarrow \ \forall a,B\ \exists p\ \text {prime}>B:\ n^{a+1}\mid p-1. \]

lang/PhiPow.lean

/-
  lang/PhiPow — T43, cycle 489: **p ≡ 1 (mod n^(a+1)) beyond every bound, for every squarefree n ≥ 2 and every a.**

  Run the squarefree construction at y = x^(n^a), x = n·(B + c + 2)!. A prime r | F has ord_r y = n; then ord_r x = n^(a+1)
  (ord_full: if d = ord_r x is a proper divisor of N = n^(a+1), a prime s | N/d divides n, n = t·s, and x^(n^a·t) = 1 gives
  y^t = 1, so n | t < n). r ∤ x as before, and Fermat gives n^(a+1) | r − 1.
    dv_pow_prime    s prime, s | m^k ⇒ s | m
    no_big          n ≥ 1, n = h·n·s, s ≥ 2 is impossible
    ord_full        ord(x^(n^a)) = n ⇒ ord x = n^(a+1)
    inf_pp          ∀ B ∃ prime p + 1 > B with n^(a+1) | p
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PhiSqInf

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv InfSplit

theorem dv_pow_prime (s m : Nat) (hs : ∀ d, Dv d (s + 1 + 1) → d = 1 ∨ d = s + 1 + 1) (hm : 1 ≤ m) :
    ∀ k, Dv (s + 1 + 1) (m ^ k) → Dv (s + 1 + 1) m
  | 0, h => absurd h (dv_one_absurd s)
  | k + 1, h => by
    obtain ⟨u, hu⟩ := le_dest2 1 (m ^ k) (pow_pos' m hm k)
    have h' : Dv (s + 1 + 1) ((u + 1) * m) := by rw [hu]; exact h
    rcases euclid_core s u m hs h' with e | e
    · rw [hu] at e; exact dv_pow_prime s m hs hm k e
    · exact e

theorem no_big (n h s : Nat) (hn : 1 ≤ n) (hs : 2 ≤ s) (e : n = h * n * s) : False := by
  match h, e with
  | 0, e =>
    rw [Nat.zero_mul, Nat.zero_mul] at e
    rw [e] at hn; exact Nat.not_succ_le_zero 0 hn
  | h + 1, e =>
    have h1 : n * 2 ≤ n := by
      calc n * 2 ≤ n * s := Nat.mul_le_mul_left n hs
        _ ≤ (h + 1) * n * s := Nat.mul_le_mul_right s (Nat.le_trans (Nat.le_of_eq (Nat.one_mul n).symm) (Nat.mul_le_mul_right n (Nat.le_add_left 1 h)))
        _ = n := e.symm
    have h2 : n + 1 ≤ n := Nat.le_trans (Nat.add_le_add_left hn n) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) h1)
    exact Nat.not_succ_le_self n h2

theorem ord_full {R : Type} [DecidableEq R] (K : DetChain.CRing R) (x : R) (n a : Nat) (hn : 1 ≤ n)
    (hy : OrdQ.IsOrd K (Frobenius.pw K x (n ^ a)) n) : OrdQ.IsOrd K x (n ^ (a + 1)) := by
  have hN : Frobenius.pw K x (n ^ (a + 1)) = K.one := by
    show Frobenius.pw K x (n ^ a * n) = K.one
    rw [← OrdQ.pw_pw]; exact hy.1
  obtain ⟨N', hN'⟩ := le_dest2 1 (n ^ (a + 1)) (pow_pos' n hn (a + 1))
  rw [← hN'] at hN
  obtain ⟨d, hd, hmin⟩ := Order.ord_exists K x N' hN
  have hO : OrdQ.IsOrd K x (d + 1) := ⟨hd, fun f hf => Order.ord_dvd K x d hd hmin f hf⟩
  obtain ⟨e, he⟩ := hO.2 _ hN
  match e, he with
  | 0, he => rw [Nat.zero_mul] at he; exact Nat.noConfusion he
  | 1, he => rw [show n ^ (a + 1) = d + 1 from hN'.symm.trans (he.trans (Nat.one_mul _))]; exact hO
  | e' + 2, he =>
    exfalso
    obtain ⟨s, hsE, hsmin⟩ := lp_exists e'
    have hspr := lp_prime _ s hsE hsmin
    obtain ⟨g, hg⟩ := hsE
    have hsN : Dv (s + 1 + 1) (n ^ (a + 1)) := ⟨g * (d + 1), by rw [← hN', he, hg]; lib_nat⟩
    obtain ⟨t, ht⟩ := dv_pow_prime s n hspr hn (a + 1) hsN
    -- n^a·t·s = g·s·(d+1), so n^a·t = g·(d+1)
    have hcan : (s + 1 + 1) * (n ^ a * t) = (s + 1 + 1) * (g * (d + 1)) := by
      have e1 : n ^ (a + 1) = n ^ a * n := rfl
      have he2 : n ^ a * (t * (s + 1 + 1)) = (g * (s + 1 + 1)) * (d + 1) := by
        rw [← ht, ← hg, ← e1, ← hN']; exact he
      calc _ = n ^ a * (t * (s + 1 + 1)) := by generalize n ^ a = P; lib_nat
        _ = _ := by rw [he2]; lib_nat
    have hc := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (s + 1)) hcan
    have hxt : Frobenius.pw K (Frobenius.pw K x (n ^ a)) t = K.one := by
      rw [OrdQ.pw_pw, hc, Nat.mul_comm, ← OrdQ.pw_pw, hd, OrdQ.pw_one1]
    obtain ⟨h, hh⟩ := hy.2 _ hxt
    rw [hh] at ht
    exact no_big n h (s + 1 + 1) hn (Nat.le_add_left 2 s) ht

theorem inf_pp (j : Nat) (L : List Nat) (hG : Good (j :: L)) (a B : Nat) :
    ∃ p, (∀ k, Dv k (p + 1) → k = 1 ∨ k = p + 1) ∧ B < p + 1 ∧ Dv (pr (j :: L) ^ (a + 1)) p := by
  obtain ⟨f, hf⟩ := fac_pos (B + cs L + 2)
  have hxB : ∀ q, 1 ≤ q → q ≤ B + cs L + 2 → Dv q (pr (j :: L) * (f + 1)) := fun q h1 h2 => by
    rw [← hf]; exact dv_mul_left _ _ _ (dv_fac _ q h1 h2)
  have hxA : AllDv (j :: L) (pr (j :: L) * (f + 1)) := by
    rw [Nat.mul_comm]; exact allDv_mul _ _ _ (allDv_pr (j :: L))
  have hx1 : 1 ≤ pr (j :: L) * (f + 1) := Nat.mul_le_mul (pr_pos (j :: L)) (Nat.le_add_left 1 f)
  generalize hxdef : pr (j :: L) * (f + 1) = x at hxB hxA hx1
  have hcx : cs L + 2 ≤ x := by
    obtain ⟨x', hx'⟩ := le_dest2 1 x hx1
    have hd := hxB (cs L + 2) (Nat.le_add_left 1 (cs L + 1)) (Nat.le_add_left _ B)
    rw [← hx'] at hd
    obtain ⟨k, hk⟩ := dv_le _ x' hd
    rw [← hx', ← hk]; exact Nat.le_add_right _ _
  -- y = x^(n^a)
  have hna : 1 ≤ pr (j :: L) ^ a := pow_pos' _ (pr_pos (j :: L)) a
  obtain ⟨E, hE⟩ := le_dest2 1 _ hna
  have hyA : AllDv (j :: L) (x ^ (E + 1)) := allDv_pow E _ _ hxA
  have hxy : Dv x (x ^ (E + 1)) := ⟨x ^ E, rfl⟩
  have hxy' : x ≤ x ^ (E + 1) := pow_ge x hx1 _ (Nat.le_add_left 1 E)
  generalize hydef : x ^ (E + 1) = y at hyA hxy hxy'
  obtain ⟨y'', hy''⟩ := le_dest2 2 y (Nat.le_trans (Nat.le_add_left 2 (cs L)) (Nat.le_trans hcx hxy'))
  rw [← hy''] at hyA
  obtain ⟨F, I, l, _⟩ := sqfree j L hG y'' hyA
  rw [show y'' + 1 + 1 = y from hy''] at I l
  have hy1 : 1 ≤ y := Nat.le_trans hx1 hxy'
  have hC : cs L < y ^ ph j L :=
    Nat.le_trans (Nat.le_add_right (cs L + 1) 1) (Nat.le_trans (Nat.le_trans hcx hxy') (pow_ge y hy1 _ (ph_pos j L)))
  have hF2 := PhiSize.two_le _ _ _ l hC
  obtain ⟨F', hF'⟩ := le_dest2 2 F hF2
  obtain ⟨r, hrF, hmin⟩ := lp_exists F'
  have hpr := lp_prime _ r hrF hmin
  rw [hF'] at hrF
  obtain ⟨hO, hg⟩ := (I r 0 hpr).1 (dv_pow1 _ _ hrF)
  have hn2 : 2 ≤ pr (j :: L) := Nat.le_trans (Nat.le_add_left 2 j) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm)
    (Nat.mul_le_mul_right (j + 1 + 1) (pr_pos L)))
  obtain ⟨n', hn'⟩ := le_dest2 1 _ (Nat.le_trans (Nat.le_add_left 1 1) hn2)
  have hry : Dv (r + 1 + 1) y → False := fun hd => by
    have hg1 : Dv (r + 1 + 1) (gN y (pr (j :: L))) := dv_base _ 0 _ hg
    rw [← hn', Nat.add_comm n' 1, gN_one] at hg1
    exact dv_one_absurd r (dv_sub (r + 1) (y * gN y n') 1 (dv_trans _ _ _ hd ⟨gN y n', Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (y * gN y n') 1]; exact hg1))
  have hrx : Dv (r + 1 + 1) x → False := fun hd => hry (dv_trans _ _ _ hd hxy)
  rcases EveryPrime.nat_even_odd r with ⟨i, hi⟩ | ⟨m, hm⟩
  · exfalso
    have h2 : r + 1 + 1 = 2 := by
      cases hpr 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
      | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
      | inr e => exact e.symm
    rw [h2] at hrx
    exact hrx (hxB 2 (Nat.le_add_left 1 1) (Nat.le_add_left 2 _))
  · subst hm
    have hX0 : emb (ring (2 * m + 1 + 1)) x ≠ (ring (2 * m + 1 + 1)).zero := fun e => hrx (dv_of_emb _ _ e)
    have hXq := Cyclo2l.ferm1 m hpr _ hX0
    rw [← hydef, Cyclo2l.emb_pow, hE] at hO
    have hfull := ord_full (ring (2 * m + 1 + 1)) _ (pr (j :: L)) a (pr_pos (j :: L)) hO
    refine ⟨2 * m + 1 + 1, hpr, ?_, hfull.2 _ hXq⟩
    cases Nat.lt_or_ge B (2 * m + 1 + 1 + 1) with
    | inl h => exact h
    | inr h => exact absurd (hxB _ (Nat.le_add_left 1 _) (Nat.le_trans h (Nat.le_trans (Nat.le_add_right B (cs L)) (Nat.le_add_right _ 2)))) hrx

end PhiSq

#print axioms PhiSq.dv_pow_prime
#print axioms PhiSq.no_big
#print axioms PhiSq.ord_full
#print axioms PhiSq.inf_pp

#print axioms: 4 theorems, no axioms.

78.127 Primes congruent to one mod every \(N\)

Every \(N\ge 1\) divides a power of a product of distinct primes, so the squarefree-power case gives every modulus.

\[ \forall N\ge 1\ \forall B\ \exists p\ \text {prime}>B:\ N\mid p-1. \]

lang/PhiAll.lean

/-
  lang/PhiAll — T43, cycle 490: **Dirichlet for 1 mod N: for every N ≥ 1 there are primes p ≡ 1 (mod N) beyond every bound.**

  rad: every N ≥ 1 divides (Π Q)^k for a list Q of distinct primes (strong induction through a bound: N = M·s with s the least
  prime of N, M < N; s is appended to Q unless it already divides Π Q). Then PhiPow.inf_pp with n = Π Q (or Q = [2] for N = 1).
    mul_pow', dv_mul_mul    (ab)^k = a^k b^k;  a | b, c | d ⇒ ac | bd
    rad_bound, rad          ∀ N ≥ 1 ∃ Q k, Good Q ∧ N | (Π Q)^k
    inf_one_mod             ∀ N B ∃ prime p + 1 > B with N + 1 | p
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.PhiPow
import lang.StrongMult1

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv InfSplit

theorem mul_pow' (a b : Nat) : ∀ k, (a * b) ^ k = a ^ k * b ^ k
  | 0 => rfl
  | k + 1 => by
    show (a * b) ^ k * (a * b) = a ^ k * a * (b ^ k * b)
    rw [mul_pow' a b k]; generalize a ^ k = P; generalize b ^ k = Q; lib_nat

theorem dv_mul_mul (a b c d : Nat) (h1 : Dv a b) (h2 : Dv c d) : Dv (a * c) (b * d) := by
  obtain ⟨q, hq⟩ := h1; obtain ⟨q', hq'⟩ := h2
  exact ⟨q * q', by rw [hq, hq']; lib_nat⟩

theorem one_pow' : ∀ k, 1 ^ k = 1
  | 0 => rfl
  | k + 1 => (Nat.mul_one _).trans (one_pow' k)

theorem rad_bound : ∀ B N, N + 1 ≤ B → ∃ Q k, Good Q ∧ Dv (N + 1) (pr Q ^ k)
  | 0, _, h => absurd h (Nat.not_succ_le_zero _)
  | B + 1, 0, _ => ⟨[], 0, trivial, ⟨1, rfl⟩⟩
  | B + 1, N'' + 1, hB => by
    obtain ⟨s, hsN, hsmin⟩ := lp_exists N''
    have hspr := lp_prime _ s hsN hsmin
    obtain ⟨M, hM⟩ := hsN
    -- M ≥ 1 and M + 1 ≤ N'' + 2
    have hM1 : 1 ≤ M := by
      match M, hM with
      | 0, hM => rw [Nat.zero_mul] at hM; exact Nat.noConfusion hM
      | M + 1, _ => exact Nat.le_add_left 1 M
    obtain ⟨M', hM'⟩ := le_dest2 1 M hM1
    have hlt : M' + 1 + 1 ≤ B + 1 := by
      have h2 : (M' + 1) * 2 ≤ N'' + 1 + 1 := by
        rw [hM, ← hM']; exact Nat.mul_le_mul_left _ (Nat.le_add_left 2 s)
      have h3 : M' + 1 + 1 ≤ (M' + 1) * 2 := Nat.le_trans (Nat.le_add_right (M' + 1 + 1) M') (Nat.le_of_eq (by lib_nat))
      exact Nat.le_trans h3 (Nat.le_trans h2 hB)
    obtain ⟨Q, k, hQ, hdv⟩ := rad_bound B M' (Nat.le_of_succ_le_succ hlt)
    rw [hM'] at hdv
    rcases dv_dec (s + 1) (pr Q) with hs | hs
    · refine ⟨Q, k + 1, hQ, ?_⟩
      rw [hM]
      exact dv_mul_mul _ _ _ _ hdv hs
    · refine ⟨s :: Q, k + 1, ⟨hspr, hs, hQ⟩, ?_⟩
      rw [hM]
      show Dv (M * (s + 1 + 1)) ((pr Q * (s + 1 + 1)) ^ k * (pr Q * (s + 1 + 1)))
      rw [mul_pow']
      obtain ⟨u, hu⟩ := dv_mul_mul _ _ _ _ hdv (dv_refl (s + 1 + 1))
      refine ⟨u * ((s + 1 + 1) ^ k * pr Q), ?_⟩
      have e : pr Q ^ k * (s + 1 + 1) ^ k * (pr Q * (s + 1 + 1)) = (pr Q ^ k * (s + 1 + 1)) * ((s + 1 + 1) ^ k * pr Q) := by
        generalize pr Q ^ k = P; generalize (s + 1 + 1) ^ k = S; generalize pr Q = R; lib_nat
      rw [e, hu]; generalize (s + 1 + 1) ^ k = S; generalize pr Q = R; lib_nat

theorem rad (N : Nat) : ∃ Q k, Good Q ∧ Dv (N + 1) (pr Q ^ k) := rad_bound (N + 1) N (Nat.le_refl _)

theorem inf_one_mod (N B : Nat) :
    ∃ p, (∀ k, Dv k (p + 1) → k = 1 ∨ k = p + 1) ∧ B < p + 1 ∧ Dv (N + 1) p := by
  obtain ⟨Q, k, hQ, hdv⟩ := rad N
  match Q, hQ, hdv with
  | [], _, hdv =>
    obtain ⟨p, hp, hB, _⟩ := inf_pp 0 [] ⟨StrongMult1.two_prime, fun h => dv_one_absurd 0 h, trivial⟩ 0 B
    have hN : N + 1 = 1 := by
      obtain ⟨q, hq⟩ := hdv
      obtain ⟨c, hc⟩ := dv_le (N + 1) 0 ⟨q, (one_pow' k).symm.trans hq⟩
      match N, c, hc with
      | 0, _, _ => rfl
      | N + 1, c, hc => exact absurd (show N + 1 + 1 + c = 0 + 1 by exact hc) (fun h => two_add_ne_one N c h)
    exact ⟨p, hp, hB, ⟨p, by rw [hN, Nat.mul_one]⟩⟩
  | j :: L, hQ, hdv =>
    obtain ⟨p, hp, hB, hd⟩ := inf_pp j L hQ k B
    exact ⟨p, hp, hB, dv_trans _ _ _ hdv (dv_trans _ _ _ ⟨pr (j :: L), Nat.mul_comm _ _⟩ hd)⟩

end PhiSq

#print axioms PhiSq.mul_pow'
#print axioms PhiSq.dv_mul_mul
#print axioms PhiSq.one_pow'
#print axioms PhiSq.rad_bound
#print axioms PhiSq.rad
#print axioms PhiSq.inf_one_mod

#print axioms: 6 theorems, no axioms.

78.128 Primes congruent to \(2^{j+2}-1\) mod \(2^{j+3}\)

Prime factors of \(v_j(O^2+2)\), \(v_{k+1}=v_k^2-2\), \(O\) the odd factorial, are \(\equiv 1\) or \(\equiv 2^{j+2}-1\), and the value is in the second class.

\[ \forall j,B\ \exists p\ \text {prime}>B:\ 2^{j+3}\mid p+1+2^{j+2}. \]

lang/Inf2em1.lean

/-
  lang/Inf2em1 — T43, cycle 505: **p ≡ 2^(j+2) − 1 (mod 2^(j+3)) beyond every bound, for every j** (7 mod 16, 15 mod 32, …).

  x = O (odd factorial up to B), N₀ = v_j(x² + 2) = uv(x², j) + 2 ≡ 2^(j+2) − 1 (mod 2^(j+3)) and odd (Inf2em1a.uv_exact).
  Every prime factor q of N₀ is odd with a root of v_j(x² + 2), so q ≡ 1 or 2^(j+2) − 1 (Two2m.root2m); some factor is in the
  second class (hasGc). An odd q ≤ B divides x, and then N₀ ≡ v_j(2) = 2 (mod q), impossible.
  With Inf3mod8 (j = 0) and InfCycloPow/Inf2e1/Two2Inf this gives every class a mod 2^k with a² ≡ 1, i.e. every Euclidean class.
    inf_2em1
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inf2em1a
import lang.Two2m
import lang.Fp2Sqrt

namespace Two2

open FTAChain ZMod Cheb

theorem inf_2em1 (j B : Nat) : ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ Dv (2 ^ (j + 3)) (p + 1 + 2 ^ (j + 2)) := by
  have h3 : 3 ≤ 2 ^ (j + 2) := Nat.le_trans (Nat.le_add_left 3 1)
    (Nat.mul_le_mul (Nat.mul_le_mul (pow_pos' 2 (Nat.le_add_left 1 1) j) (Nat.le_refl 2)) (Nat.le_refl 2))
  obtain ⟨c', hc'⟩ := le_dest2 3 _ h3
  have hL : 2 ^ (j + 3) = 2 * c' + 5 + 1 := by
    show 2 ^ (j + 2) * 2 = _; rw [← hc']; lib_nat
  obtain ⟨a, ha⟩ := ofac_odd (B + 1)
  obtain ⟨e, f, he, hf⟩ := uv_exact a j
  rw [← ha] at he hf
  generalize hx : ofac (B + 1) = x at he hf ha
  -- N₀ = uv(x², j) + 2
  have hcls : uv (x * x) j + 2 = e * (2 * c' + 5 + 1) + (c' + 2) := by
    apply add_rc 1
    calc uv (x * x) j + 2 + 1 = 2 ^ (j + 2) * (2 * e + 1) := he
      _ = (c' + 3) * (2 * e + 1) := by rw [hc']
      _ = _ := by lib_nat
  have hodd : ∀ r, uv (x * x) j + 2 = r * 2 → False := fun r hr => by
    apply InfSplit.odd_not_dv2 (2 * f + 1) r
    rw [← hr]
    calc 2 * (2 * f + 1) + 1 = uv (x * x) j + 1 + 1 := by rw [hf]
      _ = _ := rfl
  -- q odd prime dividing N₀ has the root
  have hall : ∀ l, (∀ k, Dv k (l + 1 + 1) → k = 1 ∨ k = l + 1 + 1) → Dv (l + 1 + 1) (uv (x * x) j + 2) →
      (∃ b, l + 1 + 1 = b * (2 * c' + 5 + 1) + 1) ∨ (∃ b, l + 1 + 1 = b * (2 * c' + 5 + 1) + (c' + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv
      obtain ⟨r, hr⟩ := hdv
      exact hodd r hr
    · subst hm
      have hit := it_emb (ring (2 * m + 1 + 1)) (x * x) j
      rw [emb_dv _ _ hdv, emb_add, emb_mul] at hit
      have hx2 : emb (ring (2 * m + 1 + 1)) 2 = (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one := by
        show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one = _
        lib_ring
      rw [hx2] at hit
      rcases root2m m hq j (Fp2Sqrt.two_ne m) _ hit with hb | hb
      · rw [hL] at hb
        obtain ⟨b, hb⟩ := hb
        exact Or.inl ⟨b, by rw [← hb]⟩
      · rw [hL, ← hc'] at hb
        obtain ⟨b, hb⟩ := hb
        match b, hb with
        | 0, hb => exact absurd (hb.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
        | b + 1, hb =>
          refine Or.inr ⟨b, add_rc (c' + 3 + 1) _ _ ?_⟩
          calc 2 * m + 1 + 1 + 1 + (c' + 3 + 1) = 2 * m + 1 + 1 + 1 + 1 + (c' + 3) := by lib_nat
            _ = (b + 1) * (2 * c' + 5 + 1) := hb
            _ = _ := by lib_nat
  have hcL : c' + 2 < 2 * c' + 5 + 1 := Nat.succ_le_succ (Nat.le_trans (Nat.le_add_right (c' + 2) (c' + 3)) (Nat.le_of_eq (by lib_nat)))
  obtain ⟨q, hq, ⟨b, hb⟩, hqN⟩ := hasGc (2 * c' + 5) c' _ hcL hall _ _ (Nat.le_refl _) (dv_refl _) ⟨e, hcls⟩
  refine ⟨q, hq, ?_, ⟨b + 1, by rw [hL, hb, ← hc']; lib_nat⟩⟩
  cases Nat.lt_or_ge B q with
  | inl h => exact h
  | inr h =>
    exfalso
    rcases EveryPrime.nat_even_odd q with ⟨i, hi⟩ | ⟨i, hi⟩
    · obtain ⟨r, hr⟩ := dv_trans 2 q _ ⟨i, by rw [hi]; lib_nat⟩ hqN
      exact hodd r hr
    · have hqx : Dv q x := by
        have hB : 2 * i + 1 ≤ B := by rw [hi] at h; exact h
        have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        rw [← hx, hi]; exact dv_ofac (B + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
      rw [hi] at hqx hqN
      have hit := it_emb (ring (2 * i)) (x * x) j
      rw [emb_dv _ _ hqN, emb_add, emb_mul, emb_dv _ _ hqx] at hit
      have h2 : emb (ring (2 * i)) 2 = (ring (2 * i)).zero := by
        have e2 : (ring (2 * i)).add ((ring (2 * i)).mul (ring (2 * i)).zero (ring (2 * i)).zero) (emb (ring (2 * i)) 2) =
            (ring (2 * i)).add (ring (2 * i)).one (ring (2 * i)).one := by
          show _ = (ring (2 * i)).add (ring (2 * i)).one (ring (2 * i)).one
          show (ring (2 * i)).add _ ((ring (2 * i)).add ((ring (2 * i)).add (ring (2 * i)).zero (ring (2 * i)).one) (ring (2 * i)).one) = _
          lib_ring
        rw [e2, it_two] at hit
        rw [show emb (ring (2 * i)) 2 = (ring (2 * i)).add (ring (2 * i)).one (ring (2 * i)).one by
          show (ring (2 * i)).add ((ring (2 * i)).add (ring (2 * i)).zero (ring (2 * i)).one) (ring (2 * i)).one = _; lib_ring]
        exact hit
      obtain ⟨r, hr⟩ := dv_of_emb (2 * i) 2 h2
      have hq3 : 2 ≤ 2 * i := by
        have h22 : 2 ≤ 2 * i + 1 := Nat.le_trans (Nat.le_add_left 2 c') (by rw [← hi, hb]; exact Nat.le_add_left _ _)
        match i, h22 with
        | 0, h22 => exact absurd h22 (Nat.not_succ_le_self 1)
        | i + 1, _ => exact Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.mul_le_mul_left 2 (Nat.le_add_left 1 i))
      match r, hr with
      | 0, hr => exact absurd (hr.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
      | r + 1, hr =>
        have hle : 2 * i + 1 ≤ 2 := Nat.le_trans (Nat.le_add_left (2 * i + 1) (r * (2 * i + 1))) (Nat.le_of_eq ((Nat.succ_mul r (2 * i + 1)).symm.trans hr.symm))
        exact Nat.not_succ_le_self 2 (Nat.le_trans (Nat.succ_le_succ hq3) hle)

end Two2

#print axioms Two2.inf_2em1

#print axioms: 1 theorem, no axioms.

78.129 Primes congruent to \(1\) mod \(4\) and \(-1\) mod \(\ell \), \(\ell \equiv 3\) mod \(4\)

Prime factors of \(|\psi _\ell (2+t(m+i))|^2\) are \(\equiv 1\ (4)\) and \(\equiv \pm 1\ (\ell )\), and the value is \(\equiv -1\ (\ell )\) because \(\psi _\ell \equiv (x-2)^{(\ell -1)/2}\pmod \ell \).

\[ \ell \equiv 3\ (4)\ \text {prime}\ \Longrightarrow \ \forall B\ \exists p\ \text {prime}>B:\ p\equiv 1\ (4),\ \ell \mid p+1. \]

lang/InfMix.lean

/-
  lang/InfMix — T43, cycle 530: **for every prime ℓ ≡ 3 (mod 4) there are primes p ≡ 1 (mod 4), p ≡ −1 (mod ℓ) beyond every bound**
  (the class a ≡ 1 mod 4, a ≡ −1 mod ℓ of (ℤ/4ℓ)^×; a² ≡ 1).

  O = odd factorial up to B, O = ℓ^v·w (Val.split), t = 2w, m with (m² + 1)^d = −1 (LucNonRes), x = 2 + t(m + i),
  F = |ψ_ℓ(x)|² = N ∈ ℕ (LucModQ.natF). Then N ≡ −1 (mod ℓ) (LucModL.modl); no q | t divides N (LucModQ.modq); a prime r ≡ 3 (mod 4)
  does not divide N (LucMixT.no_r3_t); a prime r ≡ 1 (mod 4) dividing N is ≡ ±1 (mod ℓ) (LucMixT.k2_t). The descent
  ChebPowInf.hasG gives a prime factor ≡ −1 (mod ℓ); it is ≡ 1 (mod 4) and does not divide O, so it exceeds B.
    eq_l, cls_m1, inf_mix
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.LucModQ
import lang.ChebPowInf
import lang.Two2Inf
import lang.PhiPow
import lang.LucMul

namespace Cheb

open FTAChain ZMod LibRing LibInt

/-- A prime dividing ℓ² is ℓ. -/
theorem eq_l (l L : Nat) (hr : ∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1)
    (hL : ∀ e, Dv e (L + 1 + 1) → e = 1 ∨ e = L + 1 + 1) (h : Dv (l + 1 + 1) ((L + 1 + 1) * (L + 1 + 1))) : l + 1 + 1 = L + 1 + 1 := by
  have h' : Dv (l + 1 + 1) (L + 1 + 1) := by
    rcases euclid_core l (L + 1) (L + 1 + 1) hr h with e | e
    · exact e
    · exact e
  rcases hL _ h' with e | e
  · exact absurd (Nat.succ.inj e) (fun h => Nat.noConfusion h)
  · exact e

/-- emb N = −1 in ℤ/ℓ gives N = qℓ + (ℓ − 1). -/
theorem cls_m1 (M N : Nat) (h : emb (ring (M + 1)) N = (ring (M + 1)).neg (ring (M + 1)).one) :
    ∃ q, N = q * (M + 1 + 1) + (M + 1) := by
  obtain ⟨q, ρ, hq, hρ⟩ := Order.divmod (M + 1) N
  have he : emb (ring (M + 1)) N = mk (M + 1) ρ := by
    rw [hq, emb_add, emb_mul, emb_self, ← emb_mk]
    show (ring (M + 1)).add ((ring (M + 1)).mul _ (ring (M + 1)).zero) _ = _
    lib_ring
  rw [he] at h
  have hval : (mk (M + 1) ρ).val = ρ := md_small _ ρ hρ
  have h1 : ρ = M + 1 := by
    rw [← hval, h]
    show md (M + 1) ((M + 1) * md (M + 1) 1) = _
    rw [md_small _ 1 (Nat.succ_le_succ (Nat.le_add_left 1 _)), Nat.mul_one]
    exact md_small _ _ (Nat.lt_succ_self _)
  exact ⟨q, by rw [hq, h1]⟩

theorem inf_mix (c B : Nat) (hl : ∀ e, Dv e (2 * (2 * c) + 1 + 1 + 1) → e = 1 ∨ e = 2 * (2 * c) + 1 + 1 + 1) :
    ∃ p, (∀ e, Dv e p → e = 1 ∨ e = p) ∧ B < p ∧ (∃ k, p = 4 * k + 1) ∧ Dv (2 * (2 * c) + 1 + 1 + 1) (p + 1) := by
  obtain ⟨m, hm⟩ := nonres c hl
  -- O = ℓ^v · w, t = 2w
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + 1)
  have hO1 : 1 ≤ Two2.ofac (B + 1) := by rw [ha]; exact Nat.le_add_left 1 _
  obtain ⟨v, w, hvw, hw⟩ := Val.split (2 * (2 * c) + 1) _ _ (Nat.le_refl _) hO1
  have hl2 : Dv (2 * (2 * c) + 1 + 1 + 1) 2 → False := fun h => by
    obtain ⟨k, hk⟩ := dv_le _ 1 h
    have e : 2 * (2 * c) + k + 1 + 1 + 1 = 0 + 1 + 1 := (by lib_nat : 2 * (2 * c) + k + 1 + 1 + 1 = 2 * (2 * c) + 1 + 1 + 1 + k).trans hk
    exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj e))
  have hlt : Dv (2 * (2 * c) + 1 + 1 + 1) (2 * w) → False := Val.ndv_mul _ 2 w hl hl2 hw
  generalize ht : 2 * w = t at hlt
  -- N
  obtain ⟨N, hN⟩ := natF (fd (Cyclo4.cyc4 intR) (Int.ofNat (2 + m * t), Int.ofNat t) (2 * c + 1)).1
    (fd (Cyclo4.cyc4 intR) (Int.ofNat (2 + m * t), Int.ofNat t) (2 * c + 1)).2
  have hNl := modl c m t N hl hm hlt hN
  have hlN : Dv (2 * (2 * c) + 1 + 1 + 1) N → False := fun h => by
    rw [emb_dv _ _ h] at hNl
    exact Fp2Sqrt.two_ne (2 * c) (by
      calc (ring (2 * (2 * c) + 1 + 1)).add (ring (2 * (2 * c) + 1 + 1)).one (ring (2 * (2 * c) + 1 + 1)).one
          = (ring (2 * (2 * c) + 1 + 1)).add ((ring (2 * (2 * c) + 1 + 1)).neg ((ring (2 * (2 * c) + 1 + 1)).neg (ring (2 * (2 * c) + 1 + 1)).one))
              ((ring (2 * (2 * c) + 1 + 1)).neg ((ring (2 * (2 * c) + 1 + 1)).neg (ring (2 * (2 * c) + 1 + 1)).one)) := by lib_ring
        _ = (ring (2 * (2 * c) + 1 + 1)).add ((ring (2 * (2 * c) + 1 + 1)).neg (ring (2 * (2 * c) + 1 + 1)).zero) ((ring (2 * (2 * c) + 1 + 1)).neg (ring (2 * (2 * c) + 1 + 1)).zero) := by rw [← hNl]
        _ = _ := by lib_ring)
  -- a prime dividing t and N is ℓ, impossible
  have hqtN : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) t → Dv (l + 1 + 1) N → False := fun l hr h1 h2 => by
    have hq := modq (l + 1) m t (2 * c + 1) N h1 hN
    rw [emb_dv _ _ h2] at hq
    have h3 := dv_of_emb (l + 1) _ hq.symm
    have e := eq_l l (2 * (2 * c) + 1) hr hl (by rw [show 2 * (2 * c + 1) + 1 = 2 * (2 * c) + 1 + 1 + 1 by lib_nat] at h3; exact h3)
    rw [e] at h2; exact hlN h2
  have h2t : Dv 2 t := ⟨w, by rw [← ht]; lib_nat⟩
  have hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) N →
      (∃ a, l + 1 + 1 = a * (2 * (2 * c) + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (2 * (2 * c) + 3) + (2 * (2 * c) + 2)) := by
    intro l hr hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m', hm'⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hr 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      exact hqtN l hr (by rw [h2]; exact h2t) hdv
    · subst hm'
      have hlr : Dv (2 * (2 * c) + 1 + 1 + 1) (2 * m' + 1 + 1 + 1) → False := fun h => by
        rcases hr _ h with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · rw [← e] at hdv; exact hlN hdv
      have hrt : Dv (2 * m' + 1 + 1 + 1) t → False := fun h => hqtN (2 * m' + 1) hr h hdv
      rcases EveryPrime.nat_even_odd m' with ⟨e, he⟩ | ⟨e, he⟩
      · exfalso
        subst he
        exact no_r3_t e c (2 + m * t) t N hr hl hlr hrt hN hdv
      · rcases k2_t m' (2 + m * t) t (2 * c + 1) N (2 * (2 * c) + 1) hr ⟨e + 1, by rw [he]; lib_nat⟩ hl (by lib_nat) hN hdv with h | h | h
        · obtain ⟨b, hb⟩ := h
          exact Or.inl ⟨b, by rw [hb]⟩
        · obtain ⟨b, hb⟩ := h
          match b, hb with
          | 0, hb => exact absurd (hb.trans (Nat.zero_mul _)) (fun h' => Nat.noConfusion h')
          | b + 1, hb => exact Or.inr ⟨b, add_rc 1 _ _ (hb.trans (by lib_nat))⟩
        · exfalso
          rw [geo_one_emb] at h
          rcases hl _ (dv_of_emb _ _ h) with e | e
          · exact Nat.noConfusion (Nat.succ.inj e)
          · exact hlr ⟨1, by rw [e]; lib_nat⟩
  obtain ⟨q0, hq0⟩ := cls_m1 (2 * (2 * c) + 1) N hNl
  obtain ⟨q, hq, ⟨b, hb⟩, hqN⟩ := hasG (2 * (2 * c)) N hall N N (Nat.le_refl _) (dv_refl _) ⟨q0, by rw [hq0]⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_trans (Nat.le_add_left 2 (2 * (2 * c))) (Nat.le_add_left _ _)
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqN hb
  have hq'' : ∀ e, Dv e (q' + 1 + 1) → e = 1 ∨ e = q' + 1 + 1 := hq
  have hqt : Dv (q' + 1 + 1) t → False := fun h => hqtN q' hq'' h hqN
  have hql : Dv (2 * (2 * c) + 1 + 1 + 1) (q' + 1 + 1) → False := fun h => by
    rcases hq'' _ h with e | e
    · exact Nat.noConfusion (Nat.succ.inj e)
    · rw [← e] at hqN; exact hlN hqN
  refine ⟨q' + 2, hq, ?_, ?_, ⟨b + 1, by show q' + 1 + 1 + 1 = _; rw [hb]; lib_nat⟩⟩
  · -- q > B
    cases Nat.lt_or_ge B (q' + 2) with
    | inl h => exact h
    | inr h =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · rcases hq'' 2 ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · exact hqt (by rw [← e]; exact h2t)
      · have hqO : Dv (q' + 1 + 1) (Two2.ofac (B + 1)) := by
          have hB : 2 * i + 1 ≤ B := by rw [hi] at h; exact h
          have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
          show Dv (q' + 2) _
          rw [hi]; exact Two2.dv_ofac (B + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        rw [hvw] at hqO
        have hP : 1 ≤ (2 * (2 * c) + 1 + 1 + 1) ^ v := pow_pos' _ (Nat.le_add_left 1 _) v
        obtain ⟨P, hP'⟩ := le_dest2 1 _ hP
        rw [← hP'] at hqO
        rcases euclid_core q' P w hq'' hqO with e | e
        · rw [hP'] at e
          have e' := PhiSq.dv_pow_prime q' _ hq'' (Nat.le_add_left 1 _) v e
          have e2 := eq_l q' (2 * (2 * c) + 1) hq'' hl (dv_trans _ _ _ e' ⟨2 * (2 * c) + 1 + 1 + 1, rfl⟩)
          rw [e2] at hqN; exact hlN hqN
        · exact hqt (by rw [← ht]; exact dv_mul_left 2 _ _ e)
  · -- q ≡ 1 (mod 4)
    rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m', hm'⟩
    · exfalso
      rcases hq'' 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · exact hqt (by rw [← e]; exact h2t)
    · subst hm'
      rcases EveryPrime.nat_even_odd m' with ⟨e, he⟩ | ⟨e, he⟩
      · exfalso
        subst he
        exact no_r3_t e c (2 + m * t) t N hq'' hl hql hqt hN hqN
      · refine ⟨e + 1, ?_⟩
        rw [he]
        lib_nat

end Cheb

#print axioms Cheb.eq_l
#print axioms Cheb.cls_m1
#print axioms Cheb.inf_mix

#print axioms: 3 theorems, no axioms.

78.130 Primes congruent to \(3\) mod \(4\) and \(1\) mod \(\ell \)

\(\Phi _\ell (2\ell O)\equiv 3\pmod 4\) for \(O\) odd, and its prime factors are \(\equiv 1\pmod \ell \) and larger than \(B\).

\[ \ell \ \text {odd prime}\ \Longrightarrow \ \forall B\ \exists p\ \text {prime}>B:\ p\equiv 3\ (4),\ p\equiv 1\ (\ell ). \]

lang/Inf3m4.lean

/-
  lang/Inf3m4 — T43, cycle 531: **p ≡ 3 (mod 4), p ≡ 1 (mod ℓ) beyond every bound, for every odd prime ℓ**
  (the class a ≡ −1 mod 4, a ≡ 1 mod ℓ of (ℤ/4ℓ)^×; Euclidean polynomial Φ_ℓ).

  y = 2ℓ·O (O the odd factorial), G = Φ_ℓ(y) = gN(y, ℓ) = 1 + y + y²·X ≡ 3 (mod 4) since y ≡ 2 (mod 4). A prime r | G has
  ord_r y = ℓ (PhiInv.ord_prime), so ℓ | r − 1; r ∤ y (G ≡ 1 mod y), so r is odd and > B. Prime factors are odd, so ≡ 1 or 3 (mod 4),
  and ChebPowInf.hasG (modulus 4) gives one ≡ 3 (mod 4).
    g_mod4, inf_3m4
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PhiInv
import lang.ChebPowInf
import lang.Two2Inf
import lang.TwoPrimeInf
import lang.Cyclo2l

namespace Cheb

open FTAChain ZMod TwoPrime

theorem g_mod4 (u j : Nat) : ∃ k, gN (2 * (2 * u + 1)) (j + 1 + 1) = k * 4 + 3 := by
  rw [show j + 1 + 1 = 1 + (1 + j) by lib_nat, gN_one, gN_one]
  generalize gN (2 * (2 * u + 1)) j = X
  exact ⟨u + (2 * u + 1) * (2 * u + 1) * X, by lib_nat⟩

theorem inf_3m4 (c B : Nat) (hl : ∀ e, Dv e (2 * c + 1 + 1 + 1) → e = 1 ∨ e = 2 * c + 1 + 1 + 1) :
    ∃ p, (∀ e, Dv e p → e = 1 ∨ e = p) ∧ B < p ∧ (∃ k, p = 4 * k + 3) ∧ ∃ q, p = q * (2 * c + 1 + 1 + 1) + 1 := by
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + 1)
  -- y = 2·(ℓ·O), ℓ·O odd
  have hodd : (2 * c + 1 + 1 + 1) * Two2.ofac (B + 1) = 2 * ((2 * c + 1 + 1 + 1) * a + c + 1) + 1 := by rw [ha]; lib_nat
  generalize hy : 2 * ((2 * c + 1 + 1 + 1) * Two2.ofac (B + 1)) = y
  have hy' : y = 2 * (2 * ((2 * c + 1 + 1 + 1) * a + c + 1) + 1) := by rw [← hy, hodd]
  obtain ⟨k0, hk0⟩ := g_mod4 ((2 * c + 1 + 1 + 1) * a + c + 1) (2 * c + 1)
  rw [← hy'] at hk0
  have hly : Dv (2 * c + 1 + 1 + 1) y := ⟨2 * Two2.ofac (B + 1), by rw [← hy]; lib_nat⟩
  -- G ≡ 1 (mod y): no prime factor divides y
  have hG1 : gN y (2 * c + 1 + 1 + 1) = 1 + y * gN y (2 * c + 1 + 1) := by
    show gN y ((2 * c + 1 + 1) + 1) = _
    rw [Nat.add_comm _ 1, gN_one]
  have hcop : ∀ l, Dv (l + 1 + 1) (gN y (2 * c + 1 + 1 + 1)) → Dv (l + 1 + 1) y → False := fun l h1 h2 => by
    rw [hG1] at h1
    exact dv_one_absurd l (dv_sub (l + 1) (y * gN y (2 * c + 1 + 1)) 1 (dv_trans _ _ _ h2 ⟨gN y (2 * c + 1 + 1), Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (y * gN y (2 * c + 1 + 1)) 1]; exact h1))
  have h2y : Dv 2 y := ⟨(2 * c + 1 + 1 + 1) * Two2.ofac (B + 1), by rw [← hy]; lib_nat⟩
  -- every prime factor is odd: ≡ 1 or 3 (mod 4)
  have hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) (gN y (2 * c + 1 + 1 + 1)) →
      (∃ a, l + 1 + 1 = a * (1 + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (1 + 3) + (1 + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      exact hcop l hdv (by rw [h2]; exact h2y)
    · rcases EveryPrime.nat_even_odd m with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact Or.inr ⟨i, by rw [hm, hi]; lib_nat⟩
      · exact Or.inl ⟨i + 1, by rw [hm, hi]; lib_nat⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqG⟩ := hasG 1 _ hall _ _ (Nat.le_refl _) (dv_refl _) ⟨k0, by rw [hk0]⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_add_left _ _
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqG hb
  have hq'' : ∀ e, Dv e (q' + 1 + 1) → e = 1 ∨ e = q' + 1 + 1 := hq
  have hqy : Dv (q' + 1 + 1) y → False := hcop q' hqG
  refine ⟨q' + 2, hq, ?_, ⟨b, by show q' + 1 + 1 = _; rw [hb]; lib_nat⟩, ?_⟩
  · cases Nat.lt_or_ge B (q' + 2) with
    | inl h => exact h
    | inr h =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · rcases hq'' 2 ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · exact hqy (by rw [← e]; exact h2y)
      · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        have hB : 2 * i + 1 ≤ B := by rw [hi] at h; exact h
        have hqO := Two2.dv_ofac (B + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        apply hqy
        show Dv (q' + 2) y
        rw [hi, ← hy]
        exact dv_trans _ _ _ hqO ⟨2 * (2 * c + 1 + 1 + 1), by lib_nat⟩
  · -- ℓ | q − 1
    have hO := PhiInv.ord_prime (2 * c + 1) q' y hl hly hqG
    rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      rcases hq'' 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · exact hqy (by rw [← e]; exact h2y)
    · subst hm
      have hX0 : emb (ring (2 * m + 1 + 1)) y ≠ (ring (2 * m + 1 + 1)).zero := fun e => hqy (dv_of_emb _ _ e)
      obtain ⟨r, hr⟩ := hO.2 _ (Cyclo2l.ferm1 m hq'' _ hX0)
      exact ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩

end Cheb

#print axioms Cheb.g_mod4
#print axioms Cheb.inf_3m4

#print axioms: 2 theorems, no axioms.

78.131 Primes congruent to \(3\) mod \(4\) and \(1\) mod \(\ell ^{e+1}\), \(\ell \equiv 3\) mod \(4\)

\(\Phi _\ell (y)\) with \(y=((\ell O)^2)^{\ell ^e}\equiv 1\pmod 4\) is \(\equiv \ell \equiv 3\pmod 4\), and its prime factors are \(\equiv 1\pmod {\ell ^{e+1}}\).

\[ \ell \equiv 3\ (4)\ \text {prime}\ \Longrightarrow \ \forall e,B\ \exists p\ \text {prime}>B:\ p\equiv 3\ (4),\ \ell ^{e+1}\mid p-1. \]

lang/Inf3m4p.lean

/-
  lang/Inf3m4p — T43, cycle 532: **p ≡ 3 (mod 4), p ≡ 1 (mod ℓ^(e+1)) beyond every bound, for every prime ℓ ≡ 3 (mod 4) and every e.**

  z = (ℓO)² ≡ 1 (mod 4), y = z^(ℓ^e) ≡ 1 (mod 4), G = Φ_ℓ(y) = gN(y, ℓ) ≡ ℓ ≡ 3 (mod 4). A prime r | G has z of order ℓ^(e+1)
  in 𝔽_r (ChebPow.ordpow, r ≠ ℓ), so ℓ^(e+1) | r − 1; r ∤ z (G ≡ 1 mod y and z | y), so r is odd and > B; hasG mod 4.
    pow41, gN41, inf_3m4p
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inf3m4
import lang.ChebPow

namespace Cheb

open FTAChain ZMod TwoPrime

theorem pow41 (s : Nat) : ∀ k, ∃ t, (4 * s + 1) ^ k = 4 * t + 1
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    obtain ⟨t, ht⟩ := pow41 s k
    exact ⟨4 * t * s + t + s, by show (4 * s + 1) ^ k * (4 * s + 1) = _; rw [ht]; lib_nat⟩

theorem gN41 (s : Nat) : ∀ n, ∃ k, gN (4 * s + 1) n = 4 * k + n
  | 0 => ⟨0, rfl⟩
  | n + 1 => by
    obtain ⟨k, hk⟩ := gN41 s n
    obtain ⟨t, ht⟩ := pow41 s n
    exact ⟨k + t, by show gN (4 * s + 1) n + (4 * s + 1) ^ n = _; rw [hk, ht]; lib_nat⟩

theorem inf_3m4p (c e B : Nat) (hl : ∀ d, Dv d (2 * (2 * c) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * c) + 1 + 1 + 1) :
    ∃ p, (∀ d, Dv d p → d = 1 ∨ d = p) ∧ B < p ∧ (∃ k, p = 4 * k + 3) ∧
      ∃ q, p = q * (2 * (2 * c) + 1 + 1 + 1) ^ (e + 1) + 1 := by
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + 1)
  -- u = ℓ·O odd, z = u², y = z^(ℓ^e)
  have hu : (2 * (2 * c) + 1 + 1 + 1) * Two2.ofac (B + 1) = 2 * ((2 * (2 * c) + 1 + 1 + 1) * a + 2 * c + 1) + 1 := by rw [ha]; lib_nat
  generalize hU : (2 * (2 * c) + 1 + 1 + 1) * Two2.ofac (B + 1) = u at hu
  generalize hv : (2 * (2 * c) + 1 + 1 + 1) * a + 2 * c + 1 = v at hu
  have hz : u * u = 4 * (v * v + v) + 1 := by rw [hu]; lib_nat
  obtain ⟨t, ht⟩ := pow41 (v * v + v) ((2 * (2 * c) + 1 + 1 + 1) ^ e)
  rw [← hz] at ht
  generalize hY : (u * u) ^ ((2 * (2 * c) + 1 + 1 + 1) ^ e) = y at ht
  obtain ⟨k0, hk0⟩ := gN41 t (2 * (2 * c) + 1 + 1 + 1)
  rw [← ht] at hk0
  have hE : 1 ≤ (2 * (2 * c) + 1 + 1 + 1) ^ e := pow_pos' _ (Nat.le_add_left 1 _) e
  obtain ⟨E, hE'⟩ := le_dest2 1 _ hE
  have huy : Dv u y := ⟨(u * u) ^ E * u, by rw [← hY, ← hE']; show (u * u) ^ E * (u * u) = _; lib_nat⟩
  have hlu : Dv (2 * (2 * c) + 1 + 1 + 1) u := ⟨Two2.ofac (B + 1), by rw [← hU]; lib_nat⟩
  have hG1 : gN y (2 * (2 * c) + 1 + 1 + 1) = 1 + y * gN y (2 * (2 * c) + 1 + 1) := by
    show gN y ((2 * (2 * c) + 1 + 1) + 1) = _
    rw [Nat.add_comm _ 1, gN_one]
  have hcop : ∀ l, Dv (l + 1 + 1) (gN y (2 * (2 * c) + 1 + 1 + 1)) → Dv (l + 1 + 1) u → False := fun l h1 h2 => by
    rw [hG1] at h1
    exact dv_one_absurd l (dv_sub (l + 1) (y * gN y (2 * (2 * c) + 1 + 1)) 1
      (dv_trans _ _ _ (dv_trans _ _ _ h2 huy) ⟨gN y (2 * (2 * c) + 1 + 1), Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (y * gN y (2 * (2 * c) + 1 + 1)) 1]; exact h1))
  have hGodd : ∀ r, gN y (2 * (2 * c) + 1 + 1 + 1) = r * 2 → False := fun r hr => by
    rw [hk0] at hr
    exact InfSplit.odd_not_dv2 (2 * k0 + 2 * c + 1) r (by rw [← hr]; lib_nat)
  have hall : ∀ l, (∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) → Dv (l + 1 + 1) (gN y (2 * (2 * c) + 1 + 1 + 1)) →
      (∃ a, l + 1 + 1 = a * (1 + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (1 + 3) + (1 + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv; obtain ⟨r, hr⟩ := hdv; exact hGodd r hr
    · rcases EveryPrime.nat_even_odd m with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact Or.inr ⟨i, by rw [hm, hi]; lib_nat⟩
      · exact Or.inl ⟨i + 1, by rw [hm, hi]; lib_nat⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqG⟩ := hasG 1 _ hall _ _ (Nat.le_refl _) (dv_refl _) ⟨k0 + c, by rw [hk0]; lib_nat⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_add_left _ _
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqG hb
  have hq'' : ∀ d, Dv d (q' + 1 + 1) → d = 1 ∨ d = q' + 1 + 1 := hq
  have hqu : Dv (q' + 1 + 1) u → False := hcop q' hqG
  refine ⟨q' + 2, hq, ?_, ⟨b, by show q' + 1 + 1 = _; rw [hb]; lib_nat⟩, ?_⟩
  · cases Nat.lt_or_ge B (q' + 2) with
    | inl h => exact h
    | inr h =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · obtain ⟨r, hr⟩ := dv_trans 2 (q' + 1 + 1) _ ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ hqG
        exact hGodd r hr
      · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        have hB : 2 * i + 1 ≤ B := by rw [hi] at h; exact h
        have hqO := Two2.dv_ofac (B + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        apply hqu
        show Dv (q' + 2) u
        rw [hi, ← hU]
        exact dv_trans _ _ _ hqO ⟨2 * (2 * c) + 1 + 1 + 1, by lib_nat⟩
  · rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      obtain ⟨r, hr⟩ := dv_trans 2 (q' + 1 + 1) _ ⟨i + 1, by rw [hi]; lib_nat⟩ hqG
      exact hGodd r hr
    · subst hm
      have hz0 : emb (ring (2 * m + 1 + 1)) (u * u) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => by
        have := dv_of_emb _ _ e0
        rcases euclid_core (2 * m + 1) (2 * v) u hq'' (by rw [← hu]; exact this) with h | h
        · exact hqu (by rw [hu]; exact h)
        · exact hqu h
      have hg : geo (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) (u * u)) ((2 * (2 * c) + 1 + 1 + 1) ^ e))
          (2 * (2 * c) + 1 + 1 + 1) = (ring (2 * m + 1 + 1)).zero := by
        rw [← Cyclo2l.emb_pow, hY, ← emb_gN]; exact emb_dv _ _ hqG
      rcases ordpow (ring (2 * m + 1 + 1)) (2 * (2 * c) + 1) hl e _ _ hg (Cyclo2l.ferm1 m hq'' _ hz0) with ⟨r, hr⟩ | h
      · exact ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩
      · exfalso
        rw [geo_one_emb] at h
        rcases hl _ (dv_of_emb _ _ h) with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · exact hqu (by rw [e]; exact hlu)

end Cheb

#print axioms Cheb.pow41
#print axioms Cheb.gN41
#print axioms Cheb.inf_3m4p

#print axioms: 3 theorems, no axioms.

78.132 Primes congruent to \(3\) mod \(4\) and \(1\) mod a squarefree \(n\)

The primitive part of \(y^n-1\) at \(y=2nO\) is \(\equiv 3\pmod 4\); its prime factors are \(\equiv 1\pmod n\) and larger than \(B\).

\[ n\ \text {odd squarefree}\ \Longrightarrow \ \forall B\ \exists p\ \text {prime}>B:\ p\equiv 3\ (4),\ n\mid p-1. \]

lang/InfSq3.lean

/-
  lang/InfSq3 — T43, cycle 534: **p ≡ 3 (mod 4), p ≡ 1 (mod n) beyond every bound, for every odd squarefree n ≥ 3**
  (n = Π of a list of distinct odd primes).

  y = 2·n·O with O = odd factorial up to B + c (c = PhiSq.cs L), so y ≡ 2 (mod 4), all q | y, and y > c. PhiSq4.sqfree4 gives F
  with Inv(n, y, F), y^φ ≤ cF and F ≡ 3 (mod 4); prime factors r of F have ord_r y = n, r ∤ y, so r is odd, > B, and n | r − 1;
  ChebPowInf.hasG (modulus 4) picks one ≡ 3 (mod 4).
    ofac_ge, inf_sq3
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.PhiSq4
import lang.PhiSqInf

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv

theorem ofac_ge : ∀ n, n + 1 ≤ Two2.ofac (n + 1)
  | 0 => Nat.le_refl 1
  | n + 1 => by
    show n + 1 + 1 ≤ Two2.ofac (n + 1) * (2 * (n + 1) + 1)
    have h1 : 1 ≤ Two2.ofac (n + 1) := Nat.le_trans (Nat.le_add_left 1 n) (ofac_ge n)
    exact Nat.le_trans (Nat.le_trans (Nat.le_add_left (n + 1 + 1) (n + 1)) (Nat.le_of_eq (by lib_nat)))
      (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ h1))

theorem inf_sq3 (j : Nat) (L : List Nat) (hG : Good (j :: L)) (h0 : ∃ h, pr (j :: L) = 2 * h + 1) (B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ (∃ k, p = 4 * k + 3) ∧ ∃ q, p = q * pr (j :: L) + 1 := by
  obtain ⟨h, hh⟩ := h0
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + cs L + 1)
  have hOge := ofac_ge (B + cs L)
  -- y = 2·n·O = 2·(2u + 1)
  have hu : pr (j :: L) * Two2.ofac (B + cs L + 1) = 2 * (2 * h * a + h + a) + 1 := by rw [hh, ha]; lib_nat
  generalize hy : 2 * (pr (j :: L) * Two2.ofac (B + cs L + 1)) = y
  have hy4 : y = 4 * (2 * h * a + h + a) + 2 := by rw [← hy, hu]; lib_nat
  have hA : AllDv (j :: L) y := by
    rw [← hy]; exact allDv_mul 2 _ _ (by rw [Nat.mul_comm]; exact allDv_mul _ _ _ (allDv_pr (j :: L)))
  have hcy : cs L + 2 ≤ y := by
    rw [← hy]
    have h1 : 1 ≤ pr (j :: L) := pr_pos _
    have e1 : cs L + 2 ≤ 2 * (B + cs L + 1) :=
      Nat.le_trans (Nat.le_add_left (cs L + 2) (2 * B + cs L)) (Nat.le_of_eq (by lib_nat))
    exact Nat.le_trans e1 (Nat.mul_le_mul_left 2 (Nat.le_trans hOge
      (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ h1))))
  obtain ⟨y', hy'⟩ := le_dest2 2 y (Nat.le_trans (Nat.le_add_left 2 (cs L)) hcy)
  rw [← hy'] at hA
  obtain ⟨F, I, l, _, hR⟩ := sqfree4 j L hG y' hA
  rw [show y' + 1 + 1 = y from hy'] at I l hR
  obtain ⟨f, hf⟩ := hR.1 _ hy4
  have hy1 : 1 ≤ y := Nat.le_trans (Nat.le_add_left 1 (cs L + 1)) hcy
  have hC : cs L < y ^ ph j L := Nat.le_trans (Nat.le_add_right (cs L + 1) 1) (Nat.le_trans hcy (pow_ge y hy1 _ (ph_pos j L)))
  have hF2 := PhiSize.two_le _ _ _ l hC
  -- F odd
  have hFodd : ∀ r, F = r * 2 → False := fun r hr => InfSplit.odd_not_dv2 (2 * f + 1) r (by rw [← hr, hf]; lib_nat)
  have hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) F →
      (∃ a, l + 1 + 1 = a * (1 + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (1 + 3) + (1 + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv; obtain ⟨r, hr⟩ := hdv; exact hFodd r hr
    · rcases EveryPrime.nat_even_odd m with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact Or.inr ⟨i, by rw [hm, hi]; lib_nat⟩
      · exact Or.inl ⟨i + 1, by rw [hm, hi]; lib_nat⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqF⟩ := hasG 1 F hall F F (Nat.le_refl _) (dv_refl _) ⟨f, by rw [hf]; lib_nat⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_add_left _ _
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqF hb
  have hq'' : ∀ e, Dv e (q' + 1 + 1) → e = 1 ∨ e = q' + 1 + 1 := hq
  obtain ⟨hO, hg⟩ := (I q' 0 hq'').1 (dv_pow1 _ _ hqF)
  have hn2 : 2 ≤ pr (j :: L) := Nat.le_trans (Nat.le_add_left 2 j) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm)
    (Nat.mul_le_mul_right (j + 1 + 1) (pr_pos L)))
  obtain ⟨n', hn'⟩ := le_dest2 1 _ (Nat.le_trans (Nat.le_add_left 1 1) hn2)
  have hqy : Dv (q' + 1 + 1) y → False := fun hd => by
    have hg1 : Dv (q' + 1 + 1) (gN y (pr (j :: L))) := dv_base _ 0 _ hg
    rw [← hn', Nat.add_comm n' 1, gN_one] at hg1
    exact dv_one_absurd q' (dv_sub (q' + 1) (y * gN y n') 1 (dv_trans _ _ _ hd ⟨gN y n', Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (y * gN y n') 1]; exact hg1))
  refine ⟨q' + 2, hq, ?_, ⟨b, by show q' + 1 + 1 = _; rw [hb]; lib_nat⟩, ?_⟩
  · cases Nat.lt_or_ge B (q' + 2) with
    | inl hlt => exact hlt
    | inr hge =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · obtain ⟨r, hr⟩ := dv_trans 2 (q' + 1 + 1) _ ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ hqF
        exact hFodd r hr
      · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        have hB : 2 * i + 1 ≤ B + cs L := by rw [hi] at hge; exact Nat.le_trans hge (Nat.le_add_right B _)
        have hqO := Two2.dv_ofac (B + cs L + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        apply hqy
        show Dv (q' + 2) y
        rw [hi, ← hy]
        exact dv_trans _ _ _ hqO ⟨2 * pr (j :: L), by lib_nat⟩
  · rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      obtain ⟨r, hr⟩ := dv_trans 2 (q' + 1 + 1) _ ⟨i + 1, by rw [hi]; lib_nat⟩ hqF
      exact hFodd r hr
    · subst hm
      have hX0 : emb (ring (2 * m + 1 + 1)) y ≠ (ring (2 * m + 1 + 1)).zero := fun e => hqy (dv_of_emb _ _ e)
      obtain ⟨r, hr⟩ := hO.2 _ (Cyclo2l.ferm1 m hq'' _ hX0)
      exact ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩

end PhiSq

#print axioms PhiSq.ofac_ge
#print axioms PhiSq.inf_sq3

#print axioms: 2 theorems, no axioms.

78.133 Primes congruent to \(5\) mod \(8\) and \(1\) mod \(4n\), \(n\) squarefree

The primitive part of \(y^{2n}-1\) at \(y=(2nO)^2\) is \(\equiv 5\pmod 8\), and its prime factors are \(\equiv 1\pmod {4n}\) and larger than \(B\).

\[ n\ \text {odd squarefree}\ \Longrightarrow \ \forall B\ \exists p\ \text {prime}>B:\ p\equiv 5\ (8),\ 4n\mid p-1. \]

lang/Inf5m8.lean

/-
  lang/Inf5m8 — T43, cycle 537: **p ≡ 5 (mod 8), p ≡ 1 (mod 4n) beyond every bound, for every odd squarefree n ≥ 3.**

  x = 2nO (O odd factorial up to B + c), y = x² ≡ 4 (mod 8). PhiSq8.sqfree8 for the primes of 2n (2 prepended) gives F with
  Inv(2n, y, F), y^φ ≤ cF and F ≡ 5 (mod 8). A prime r | F has ord_r y = 2n, hence ord_r x = 4n (OrdQ.ord_of_pow with q = 2;
  ord x = 2n would give y^n = 1), so 4n | r − 1 and r ≡ 1 or 5 (mod 8); r ∤ y, so r > B. Two2.hasGc (modulus 8) picks r ≡ 5.
    no_half, inf_5m8
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PhiSq8
import lang.InfSq3
import lang.Inf2em1a
import lang.StrongMult1

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv

/-- 2n | n is impossible for n ≥ 1. -/
theorem no_half (n : Nat) (hn : 1 ≤ n) (h : Dv (n * 2) n) : False := by
  obtain ⟨c, hc⟩ := h
  match c, hc with
  | 0, hc => rw [Nat.zero_mul] at hc; rw [hc] at hn; exact Nat.not_succ_le_zero 0 hn
  | c + 1, hc =>
    have h1 : n + 1 ≤ n := Nat.le_trans (Nat.add_le_add_left hn n) (Nat.le_trans (Nat.le_of_eq (by lib_nat))
      (Nat.le_trans (Nat.le_add_left (n * 2) (c * (n * 2))) (Nat.le_of_eq ((Nat.succ_mul c (n * 2)).symm.trans hc.symm))))
    exact Nat.not_succ_le_self n h1

theorem inf_5m8 (j : Nat) (L : List Nat) (hG : Good (j :: L)) (h0 : ∃ h, pr (j :: L) = 2 * h + 1) (B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ (∃ k, p = 8 * k + 5) ∧ ∃ s, p = s * (pr (j :: L) * 2 * 2) + 1 := by
  obtain ⟨h, hh⟩ := h0
  have hG2 : Good (0 :: j :: L) := ⟨StrongMult1.two_prime, fun hd => by
    obtain ⟨c, hc⟩ := hd; rw [hh] at hc; exact InfSplit.odd_not_dv2 h c hc, hG⟩
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + cs (j :: L) + 1)
  have hOge := ofac_ge (B + cs (j :: L))
  have hu : pr (j :: L) * Two2.ofac (B + cs (j :: L) + 1) = 2 * (2 * h * a + h + a) + 1 := by rw [hh, ha]; lib_nat
  generalize hU : pr (j :: L) * Two2.ofac (B + cs (j :: L) + 1) = u at hu
  generalize hv : 2 * h * a + h + a = v at hu
  generalize hy : (2 * u) * (2 * u) = y
  have hy8 : y = 8 * (2 * v * v + 2 * v) + 4 := by rw [← hy, hu]; lib_nat
  have hxA : AllDv (j :: L) (2 * u) := by
    rw [← hU]; exact allDv_mul 2 _ _ (by rw [Nat.mul_comm]; exact allDv_mul _ _ _ (allDv_pr (j :: L)))
  have hA : AllDv (0 :: j :: L) y := ⟨⟨2 * u * u, by rw [← hy]; lib_nat⟩, by rw [← hy]; exact allDv_mul _ _ _ hxA⟩
  have hu1 : 1 ≤ u := by rw [hu]; exact Nat.le_add_left 1 _
  have hcy : cs (j :: L) + 2 ≤ y := by
    have h1 : 1 ≤ pr (j :: L) := pr_pos _
    have e1 : cs (j :: L) + 2 ≤ 2 * (B + cs (j :: L) + 1) :=
      Nat.le_trans (Nat.le_add_left (cs (j :: L) + 2) (2 * B + cs (j :: L))) (Nat.le_of_eq (by lib_nat))
    have e2 : 2 * (B + cs (j :: L) + 1) ≤ 2 * u := Nat.mul_le_mul_left 2 (by
      rw [← hU]; exact Nat.le_trans hOge (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ h1)))
    have e3 : 2 * u ≤ y := by
      have h2u : 1 ≤ 2 * u := Nat.le_trans hu1 (Nat.le_trans (Nat.le_add_left u u) (Nat.le_of_eq (by lib_nat)))
      rw [← hy]; exact Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm) (Nat.mul_le_mul_left _ h2u)
    exact Nat.le_trans e1 (Nat.le_trans e2 e3)
  obtain ⟨y', hy'⟩ := le_dest2 2 y (Nat.le_trans (Nat.le_add_left 2 _) hcy)
  rw [← hy'] at hA
  obtain ⟨F, I, l, _, hR⟩ := sqfree8 0 (j :: L) hG2 y' hA
  rw [show y' + 1 + 1 = y from hy'] at I l hR
  obtain ⟨f, hf⟩ := hR.1 _ hy8
  have hy1 : 1 ≤ y := Nat.le_trans (Nat.le_add_left 1 _) hcy
  have hC : cs (j :: L) < y ^ ph 0 (j :: L) :=
    Nat.le_trans (Nat.le_add_right (cs (j :: L) + 1) 1) (Nat.le_trans hcy (pow_ge y hy1 _ (ph_pos 0 (j :: L))))
  have hF2 := PhiSize.two_le _ _ _ l hC
  have hn1 : 1 ≤ pr (j :: L) := pr_pos _
  -- facts about a prime factor of F
  have hfac : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) F →
      (Dv (l + 1 + 1) y → False) ∧ ∃ s, l + 1 + 1 = s * (pr (j :: L) * 2 * 2) + 1 := by
    intro l hq hdv
    obtain ⟨hO, hg⟩ := (I l 0 hq).1 (dv_pow1 _ _ hdv)
    have hpr2 : pr (0 :: j :: L) = pr (j :: L) * 2 := rfl
    rw [hpr2] at hO hg
    obtain ⟨n', hn'⟩ := le_dest2 1 (pr (j :: L) * 2) (Nat.le_trans hn1 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm) (Nat.mul_le_mul_left _ (Nat.le_add_left 1 1))))
    have hqy : Dv (l + 1 + 1) y → False := fun hd => by
      have hg1 : Dv (l + 1 + 1) (gN y (pr (j :: L) * 2)) := dv_base _ 0 _ hg
      rw [← hn', Nat.add_comm n' 1, gN_one] at hg1
      exact dv_one_absurd l (dv_sub (l + 1) (y * gN y n') 1 (dv_trans _ _ _ hd ⟨gN y n', Nat.mul_comm _ _⟩)
        (by rw [Nat.add_comm (y * gN y n') 1]; exact hg1))
    refine ⟨hqy, ?_⟩
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      rcases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · exact hqy (by rw [← e, ← hy]; exact ⟨2 * u * u, by lib_nat⟩)
    · subst hm
      have hey : emb (ring (2 * m + 1 + 1)) y = Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) (2 * u)) (0 + 1 + 1) := by
        rw [← hy, emb_mul]; show _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one _) _; lib_ring
      rw [hey] at hO
      rcases OrdQ.ord_of_pow (ring (2 * m + 1 + 1)) _ _ 0 hO (Nat.le_trans hn1 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm) (Nat.mul_le_mul_left _ (Nat.le_add_left 1 1)))) StrongMult1.two_prime with h1 | h1
      · exfalso
        have hyn : Frobenius.pw (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) (2 * u)) (0 + 1 + 1)) (pr (j :: L)) = (ring (2 * m + 1 + 1)).one := by
          rw [OrdQ.pw_pw, Nat.mul_comm (0 + 1 + 1) (pr (j :: L))]; exact h1.1
        exact no_half _ hn1 (hO.2 _ hyn)
      · have hX0 : emb (ring (2 * m + 1 + 1)) (2 * u) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 =>
          hqy (dv_trans _ _ _ (dv_of_emb _ _ e0) ⟨2 * u, hy.symm⟩)
        obtain ⟨r, hr⟩ := h1.2 _ (Cyclo2l.ferm1 m hq _ hX0)
        exact ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩
  have hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) F →
      (∃ a, l + 1 + 1 = a * (7 + 1) + 1) ∨ (∃ a, l + 1 + 1 = a * (7 + 1) + (3 + 2)) := by
    intro l hq hdv
    obtain ⟨_, s0, hs0⟩ := hfac l hq hdv
    rw [hs0, hh]
    rcases EveryPrime.nat_even_odd s0 with ⟨t, ht⟩ | ⟨t, ht⟩
    · exact Or.inl ⟨t * (2 * h + 1), by rw [ht]; lib_nat⟩
    · exact Or.inr ⟨t * (2 * h + 1) + h, by rw [ht]; lib_nat⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqF⟩ := Two2.hasGc 7 3 F (Nat.succ_le_succ (Nat.le_add_left 5 2)) hall F F (Nat.le_refl _) (dv_refl _) ⟨f, by rw [hf]; lib_nat⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_trans (Nat.le_add_left 2 3) (Nat.le_add_left _ _)
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqF hb
  have hq'' : ∀ e, Dv e (q' + 1 + 1) → e = 1 ∨ e = q' + 1 + 1 := hq
  obtain ⟨hqy, s, hs⟩ := hfac q' hq'' hqF
  refine ⟨q' + 2, hq, ?_, ⟨b, by show q' + 1 + 1 = _; rw [hb]; lib_nat⟩, ⟨s, hs⟩⟩
  cases Nat.lt_or_ge B (q' + 2) with
  | inl hlt => exact hlt
  | inr hge =>
    exfalso
    rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
    · rcases hq'' 2 ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · exact hqy (by rw [← e, ← hy]; exact ⟨2 * u * u, by lib_nat⟩)
    · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
      have hB : 2 * i + 1 ≤ B + cs (j :: L) := by rw [hi] at hge; exact Nat.le_trans hge (Nat.le_add_right B _)
      have hqO := Two2.dv_ofac (B + cs (j :: L) + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
      apply hqy
      show Dv (q' + 2) y
      rw [hi, ← hy, ← hU]
      exact dv_trans _ _ _ hqO ⟨2 * (2 * (pr (j :: L) * Two2.ofac (B + cs (j :: L) + 1))) * pr (j :: L), by lib_nat⟩

end PhiSq

#print axioms PhiSq.no_half
#print axioms PhiSq.inf_5m8

#print axioms: 2 theorems, no axioms.

78.134 Primes congruent to \(7\) mod \(8\) and \(1\) mod \(n\), \(n\) squarefree

At \(y=2w^2\) every prime factor \(r\) of the primitive part of \(y^n-1\) makes \(y\), hence \(2\), a square, so \(r\equiv \pm 1\pmod 8\); the value is \(\equiv 3\pmod 4\).

\[ n\ \text {odd squarefree}\ \Longrightarrow \ \forall B\ \exists p\ \text {prime}>B:\ p\equiv 7\ (8),\ n\mid p-1. \]

lang/Inf7m8.lean

/-
  lang/Inf7m8 — T43, cycle 538: **p ≡ 7 (mod 8), p ≡ 1 (mod n) beyond every bound, for every odd squarefree n ≥ 3.**

  y = 2w², w = nO (odd), so y ≡ 2 (mod 4) and PhiSq4.sqfree4 gives F ≡ 3 (mod 4) with Inv(n, y, F). For a prime r | F, y has odd
  order n, so y = (y^((n+1)/2))² and 2 = y·(w⁻¹)² is a square mod r, hence r ≡ ±1 (mod 8) (TwoSupp); r ≡ 1 (mod n) by Fermat.
  ChebPowInf.hasG (modulus 4) gives a factor ≡ 3 (mod 4), which is then ≡ 7 (mod 8).
    two_pm, inf_7m8
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.InfSq3
import lang.Inf3mod8
import lang.HasseCM

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv

/-- 2 a square mod an odd prime r ⇒ r ≡ ±1 (mod 8). -/
theorem two_pm (l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (hodd : ∀ c, l + 1 + 1 = 2 * c → False)
    (z : Zp (l + 1)) (hz : (ring (l + 1)).mul z z = (ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one) :
    ∃ b, l + 1 + 1 = b * 8 + 1 ∨ l + 1 + 1 = b * 8 + 7 := by
  have hPr : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, hq⟩
  have h2 : (ring (l + 1)).mul (mk (l + 1) z.val) (mk (l + 1) z.val) = mk (l + 1) 2 := by
    rw [mk_val, Inf3mod8.two_eq]; exact hz
  have hsq := Inf3mod8.sq_rem l z.val h2
  obtain ⟨b, r, hb, hr⟩ := Inf7mod8.mod8 (l + 1 + 1)
  match r, hr with
  | 0, _ => exact absurd hb (fun e => hodd (4 * b) (by rw [e]; lib_nat))
  | 1, _ => exact ⟨b, Or.inl hb⟩
  | 2, _ => exact absurd hb (fun e => hodd (4 * b + 1) (by rw [e]; lib_nat))
  | 3, _ =>
    exfalso
    have e : (b + b) + ((b + b) + 1) + ((b + b) + ((b + b) + 1)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
    exact TwoSupp.two_8m3 b (by rw [e]; exact hPr) ⟨z.val, by rw [e]; exact hsq⟩
  | 4, _ => exact absurd hb (fun e => hodd (4 * b + 2) (by rw [e]; lib_nat))
  | 5, _ =>
    exfalso
    have e : (b + b + 1) + ((b + b + 1) + 0) + ((b + b + 1) + ((b + b + 1) + 0)) + 1 = l + 1 + 1 := by rw [hb]; lib_nat
    exact TwoSupp.two_8m5 b (by rw [e]; exact hPr) ⟨z.val, by rw [e]; exact hsq⟩
  | 6, _ => exact absurd hb (fun e => hodd (4 * b + 3) (by rw [e]; lib_nat))
  | 7, _ => exact ⟨b, Or.inr hb⟩
  | r + 8, hr => exact absurd hr (fun h => Nat.lt_irrefl 8 (Nat.lt_of_le_of_lt (Nat.le_add_left 8 r) h))

theorem inf_7m8 (j : Nat) (L : List Nat) (hG : Good (j :: L)) (h0 : ∃ h, pr (j :: L) = 2 * h + 1) (B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ (∃ k, p = 8 * k + 7) ∧ ∃ q, p = q * pr (j :: L) + 1 := by
  obtain ⟨h, hh⟩ := h0
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + cs L + 1)
  have hOge := ofac_ge (B + cs L)
  have hw : pr (j :: L) * Two2.ofac (B + cs L + 1) = 2 * (2 * h * a + h + a) + 1 := by rw [hh, ha]; lib_nat
  generalize hW : pr (j :: L) * Two2.ofac (B + cs L + 1) = w at hw
  generalize hv : 2 * h * a + h + a = v at hw
  generalize hy : 2 * (w * w) = y
  have hy4 : y = 4 * (2 * (v * v + v)) + 2 := by rw [← hy, hw]; lib_nat
  have hwA : AllDv (j :: L) w := by
    rw [← hW, Nat.mul_comm]; exact allDv_mul _ _ _ (allDv_pr (j :: L))
  have hA : AllDv (j :: L) y := by
    rw [← hy]; exact allDv_mul 2 _ _ (allDv_mul w _ _ hwA)
  have hw1 : 1 ≤ w := by rw [hw]; exact Nat.le_add_left 1 _
  have hcy : cs L + 2 ≤ y := by
    have h1 : 1 ≤ pr (j :: L) := pr_pos _
    have e1 : cs L + 2 ≤ B + cs L + 1 + 1 := Nat.le_trans (Nat.le_add_left (cs L + 2) B) (Nat.le_of_eq (by lib_nat))
    have e2 : B + cs L + 1 ≤ w := by
      rw [← hW]; exact Nat.le_trans hOge (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ h1))
    have e3 : w + 1 ≤ y := by
      rw [← hy]
      exact Nat.le_trans (Nat.add_le_add_left hw1 w) (Nat.le_trans (Nat.le_of_eq (by lib_nat))
        (Nat.mul_le_mul_left 2 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one w).symm) (Nat.mul_le_mul_left w hw1))))
    exact Nat.le_trans e1 (Nat.le_trans (Nat.succ_le_succ e2) e3)
  obtain ⟨y', hy'⟩ := le_dest2 2 y (Nat.le_trans (Nat.le_add_left 2 (cs L)) hcy)
  rw [← hy'] at hA
  obtain ⟨F, I, l, _, hR⟩ := sqfree4 j L hG y' hA
  rw [show y' + 1 + 1 = y from hy'] at I l hR
  obtain ⟨f, hf⟩ := hR.1 _ hy4
  have hy1 : 1 ≤ y := Nat.le_trans (Nat.le_add_left 1 (cs L + 1)) hcy
  have hC : cs L < y ^ ph j L := Nat.le_trans (Nat.le_add_right (cs L + 1) 1) (Nat.le_trans hcy (pow_ge y hy1 _ (ph_pos j L)))
  have hF2 := PhiSize.two_le _ _ _ l hC
  have hFodd : ∀ r, F = r * 2 → False := fun r hr => InfSplit.odd_not_dv2 (2 * f + 1) r (by rw [← hr, hf]; lib_nat)
  have hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) F →
      (∃ a, l + 1 + 1 = a * (1 + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (1 + 3) + (1 + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv; obtain ⟨r, hr⟩ := hdv; exact hFodd r hr
    · rcases EveryPrime.nat_even_odd m with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact Or.inr ⟨i, by rw [hm, hi]; lib_nat⟩
      · exact Or.inl ⟨i + 1, by rw [hm, hi]; lib_nat⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqF⟩ := hasG 1 F hall F F (Nat.le_refl _) (dv_refl _) ⟨f, by rw [hf]; lib_nat⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_add_left _ _
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqF hb
  have hq'' : ∀ e, Dv e (q' + 1 + 1) → e = 1 ∨ e = q' + 1 + 1 := hq
  obtain ⟨hO, hg⟩ := (I q' 0 hq'').1 (dv_pow1 _ _ hqF)
  have hn2 : 2 ≤ pr (j :: L) := Nat.le_trans (Nat.le_add_left 2 j) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm)
    (Nat.mul_le_mul_right (j + 1 + 1) (pr_pos L)))
  obtain ⟨n', hn'⟩ := le_dest2 1 _ (Nat.le_trans (Nat.le_add_left 1 1) hn2)
  have hqy : Dv (q' + 1 + 1) y → False := fun hd => by
    have hg1 : Dv (q' + 1 + 1) (gN y (pr (j :: L))) := dv_base _ 0 _ hg
    rw [← hn', Nat.add_comm n' 1, gN_one] at hg1
    exact dv_one_absurd q' (dv_sub (q' + 1) (y * gN y n') 1 (dv_trans _ _ _ hd ⟨gN y n', Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (y * gN y n') 1]; exact hg1))
  have hqw : Dv (q' + 1 + 1) w → False := fun hd => hqy (by rw [← hy]; exact dv_mul_left 2 _ _ (dv_mul_left w _ _ hd))
  have hodd : ∀ c, q' + 1 + 1 = 2 * c → False := fun c hc => by
    rcases hq'' 2 ⟨c, hc.trans (Nat.mul_comm 2 c)⟩ with e | e
    · exact Nat.noConfusion (Nat.succ.inj e)
    · exact hqy (by rw [← e, ← hy]; exact ⟨w * w, by lib_nat⟩)
  refine ⟨q' + 2, hq, ?_, ?_, ?_⟩
  · cases Nat.lt_or_ge B (q' + 2) with
    | inl hlt => exact hlt
    | inr hge =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact hodd i hi
      · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        have hB : 2 * i + 1 ≤ B + cs L := by rw [hi] at hge; exact Nat.le_trans hge (Nat.le_add_right B _)
        have hqO := Two2.dv_ofac (B + cs L + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        apply hqw
        show Dv (q' + 2) w
        rw [hi, ← hW]
        exact dv_trans _ _ _ hqO ⟨pr (j :: L), rfl⟩
  · -- q ≡ 7 (mod 8): q ≡ 3 (mod 4) and 2 is a square mod q
    rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m, hm⟩
    · exact absurd (show q' + 1 + 1 = 2 * (i + 1) by rw [hi]; lib_nat) (hodd (i + 1))
    · subst hm
      have hW0 : emb (ring (2 * m + 1 + 1)) w ≠ (ring (2 * m + 1 + 1)).zero := fun e => hqw (dv_of_emb _ _ e)
      have hinv := ZMod.inv_exists (2 * m + 1) hq'' _ hW0
      generalize Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) w) (2 * m + 1) = iw at hinv
      have hYn := hO.1
      rw [hh] at hYn
      have hY : emb (ring (2 * m + 1 + 1)) y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)
          ((ring (2 * m + 1 + 1)).mul (emb (ring (2 * m + 1 + 1)) w) (emb (ring (2 * m + 1 + 1)) w)) := by
        rw [← hy, emb_mul, emb_mul]; show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) _ = _; lib_ring
      have hsq : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (h + 1))
          (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (h + 1)) = emb (ring (2 * m + 1 + 1)) y := by
        rw [← HasseCM.pw_add', show h + 1 + (h + 1) = (2 * h + 1) + 1 by lib_nat]
        show (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) _ (2 * h + 1)) _ = _
        rw [hYn]; lib_ring
      generalize Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (h + 1) = t at hsq
      have hz : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t iw) ((ring (2 * m + 1 + 1)).mul t iw) =
          (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one := by
        calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) ((ring (2 * m + 1 + 1)).mul iw iw) := by lib_ring
          _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)
                ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul iw (emb (ring (2 * m + 1 + 1)) w)) ((ring (2 * m + 1 + 1)).mul iw (emb (ring (2 * m + 1 + 1)) w))) := by
              rw [hsq, hY]; lib_ring
          _ = _ := by rw [hinv]; lib_ring
      obtain ⟨b8, hb8⟩ := two_pm (2 * m + 1) hq'' hodd _ hz
      rcases hb8 with e | e
      · exfalso
        have := Inf3mod8.uniq_rem 3 (2 * b8) b 1 3 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 2)) (Nat.lt_succ_self 3)
          ((show (2 * b8) * (3 + 1) + 1 = b8 * 8 + 1 by lib_nat).trans (e.symm.trans (hb.trans (by lib_nat))))
        exact Nat.noConfusion (Nat.succ.inj this)
      · exact ⟨b8, by show 2 * m + 1 + 1 + 1 = _; rw [e]; lib_nat⟩
  · rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m, hm⟩
    · exact absurd (show q' + 1 + 1 = 2 * (i + 1) by rw [hi]; lib_nat) (hodd (i + 1))
    · subst hm
      have hX0 : emb (ring (2 * m + 1 + 1)) y ≠ (ring (2 * m + 1 + 1)).zero := fun e => hqy (dv_of_emb _ _ e)
      obtain ⟨r, hr⟩ := hO.2 _ (Cyclo2l.ferm1 m hq'' _ hX0)
      exact ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩

end PhiSq

#print axioms PhiSq.two_pm
#print axioms PhiSq.inf_7m8

#print axioms: 2 theorems, no axioms.

78.135 Primes congruent to \(3\) mod \(8\) and \(1\) mod \(2n\), \(n\) squarefree

At \(y=2w^2\) the prime factors \(r\) of the primitive part of \(y^{2n}-1\) have \(y^n\equiv -1\), so \(-2\) is a square and \(r\equiv 1,3\pmod 8\); the value is \(\equiv 3\pmod 4\).

\[ n\ \text {odd squarefree}\ \Longrightarrow \ \forall B\ \exists p\ \text {prime}>B:\ p\equiv 3\ (8),\ 2n\mid p-1. \]

lang/Inf3m8.lean

/-
  lang/Inf3m8 — T43, cycle 539: **p ≡ 3 (mod 8), p ≡ 1 (mod 2n) beyond every bound, for every odd squarefree n ≥ 3.**

  The primitive part F of y^(2n) − 1 at y = 2w², w = nO: y ≡ 2 (mod 4), so F ≡ 3 (mod 4) (PhiSq4 with the list 2 :: primes of n).
  A prime r | F has ord_r y = 2n, so y^n = −1 and (−y)^n = 1 with n odd: −y is a square, hence −2 = −y·w⁻² is a square and
  r ≡ 1 or 3 (mod 8) (Inf3mod8.allowed3); also 2n | r − 1. A factor ≡ 3 (mod 4) is ≡ 3 (mod 8).
    mtwo, inf_3m8
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inf7m8
import lang.Inf5m8

namespace PhiSq

open FTAChain ZMod Cheb TwoPrime PhiInv

/-- −2 a square mod an odd prime r ⇒ r ≡ 1 or 3 (mod 8). -/
theorem mtwo (l : Nat) (hq : ∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) (hodd : ∀ c, l + 1 + 1 = 2 * c → False)
    (z : Zp (l + 1)) (hz : (ring (l + 1)).mul z z = (ring (l + 1)).neg ((ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one)) :
    ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 3) := by
  have hx : ∀ x, mk (l + 1) x = z → (∃ y, x = 2 * y + 1) → ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 3) := fun x hxz hxo => by
    apply Inf3mod8.allowed3 x l hxo hq
    apply md_zero_dv (l + 1)
    have h1 : (ring (l + 1)).add ((ring (l + 1)).mul (mk (l + 1) x) (mk (l + 1) x)) ((ring (l + 1)).add (ring (l + 1)).one (ring (l + 1)).one) = (ring (l + 1)).zero := by
      rw [hxz, hz]; lib_ring
    rw [TwoPower.mk_mul, ← Inf3mod8.two_eq, PrimeOneModQ.mk_add] at h1
    exact congrArg Subtype.val h1
  rcases EveryPrime.nat_even_odd z.val with ⟨i, hi⟩ | ⟨i, hi⟩
  · obtain ⟨m, hm⟩ : ∃ m, l + 1 + 1 = 2 * m + 1 := by
      rcases EveryPrime.nat_even_odd (l + 1 + 1) with ⟨c, hc⟩ | ⟨c, hc⟩
      · exact absurd hc (hodd c)
      · exact ⟨c, hc⟩
    refine hx (z.val + (l + 1 + 1)) ?_ ⟨i + m, by rw [hi, hm]; lib_nat⟩
    exact Subtype.ext ((md_add_p (l + 1) z.val).trans (md_small (l + 1) z.val z.property))
  · exact hx z.val (mk_val _ z) ⟨i, hi⟩

theorem inf_3m8 (j : Nat) (L : List Nat) (hG : Good (j :: L)) (h0 : ∃ h, pr (j :: L) = 2 * h + 1) (B : Nat) :
    ∃ p, (∀ k, Dv k p → k = 1 ∨ k = p) ∧ B < p ∧ (∃ k, p = 8 * k + 3) ∧ ∃ s, p = s * (pr (j :: L) * 2) + 1 := by
  obtain ⟨h, hh⟩ := h0
  have hG2 : Good (0 :: j :: L) := ⟨StrongMult1.two_prime, fun hd => by
    obtain ⟨c, hc⟩ := hd; rw [hh] at hc; exact InfSplit.odd_not_dv2 h c hc, hG⟩
  obtain ⟨a, ha⟩ := Two2.ofac_odd (B + cs (j :: L) + 1)
  have hOge := ofac_ge (B + cs (j :: L))
  have hw : pr (j :: L) * Two2.ofac (B + cs (j :: L) + 1) = 2 * (2 * h * a + h + a) + 1 := by rw [hh, ha]; lib_nat
  generalize hW : pr (j :: L) * Two2.ofac (B + cs (j :: L) + 1) = w at hw
  generalize hv : 2 * h * a + h + a = v at hw
  generalize hy : 2 * (w * w) = y
  have hy4 : y = 4 * (2 * (v * v + v)) + 2 := by rw [← hy, hw]; lib_nat
  have hwA : AllDv (j :: L) w := by
    rw [← hW, Nat.mul_comm]; exact allDv_mul _ _ _ (allDv_pr (j :: L))
  have hA : AllDv (0 :: j :: L) y := ⟨⟨w * w, by rw [← hy]; lib_nat⟩, by rw [← hy]; exact allDv_mul 2 _ _ (allDv_mul w _ _ hwA)⟩
  have hw1 : 1 ≤ w := by rw [hw]; exact Nat.le_add_left 1 _
  have hcy : cs (j :: L) + 2 ≤ y := by
    have h1 : 1 ≤ pr (j :: L) := pr_pos _
    have e1 : cs (j :: L) + 2 ≤ B + cs (j :: L) + 1 + 1 := Nat.le_trans (Nat.le_add_left (cs (j :: L) + 2) B) (Nat.le_of_eq (by lib_nat))
    have e2 : B + cs (j :: L) + 1 ≤ w := by
      rw [← hW]; exact Nat.le_trans hOge (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ h1))
    have e3 : w + 1 ≤ y := by
      rw [← hy]
      exact Nat.le_trans (Nat.add_le_add_left hw1 w) (Nat.le_trans (Nat.le_of_eq (by lib_nat))
        (Nat.mul_le_mul_left 2 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one w).symm) (Nat.mul_le_mul_left w hw1))))
    exact Nat.le_trans e1 (Nat.le_trans (Nat.succ_le_succ e2) e3)
  obtain ⟨y', hy'⟩ := le_dest2 2 y (Nat.le_trans (Nat.le_add_left 2 _) hcy)
  rw [← hy'] at hA
  obtain ⟨F, I, l, _, hR⟩ := sqfree4 0 (j :: L) hG2 y' hA
  rw [show y' + 1 + 1 = y from hy'] at I l hR
  obtain ⟨f, hf⟩ := hR.1 _ hy4
  have hy1 : 1 ≤ y := Nat.le_trans (Nat.le_add_left 1 _) hcy
  have hC : cs (j :: L) < y ^ ph 0 (j :: L) :=
    Nat.le_trans (Nat.le_add_right (cs (j :: L) + 1) 1) (Nat.le_trans hcy (pow_ge y hy1 _ (ph_pos 0 (j :: L))))
  have hF2 := PhiSize.two_le _ _ _ l hC
  have hn1 : 1 ≤ pr (j :: L) := pr_pos _
  have hFodd : ∀ r, F = r * 2 → False := fun r hr => InfSplit.odd_not_dv2 (2 * f + 1) r (by rw [← hr, hf]; lib_nat)
  -- facts about a prime factor of F
  have hfac : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) F →
      (Dv (l + 1 + 1) y → False) ∧ (∃ s, l + 1 + 1 = s * (pr (j :: L) * 2) + 1) ∧ ∃ b r, l + 1 + 1 = b * 8 + r ∧ (r = 1 ∨ r = 3) := by
    intro l hq hdv
    obtain ⟨hO, hg⟩ := (I l 0 hq).1 (dv_pow1 _ _ hdv)
    have hpr2 : pr (0 :: j :: L) = pr (j :: L) * 2 := rfl
    rw [hpr2] at hO hg
    obtain ⟨n', hn'⟩ := le_dest2 1 (pr (j :: L) * 2) (Nat.le_trans hn1 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one _).symm) (Nat.mul_le_mul_left _ (Nat.le_add_left 1 1))))
    have hqy : Dv (l + 1 + 1) y → False := fun hd => by
      have hg1 : Dv (l + 1 + 1) (gN y (pr (j :: L) * 2)) := dv_base _ 0 _ hg
      rw [← hn', Nat.add_comm n' 1, gN_one] at hg1
      exact dv_one_absurd l (dv_sub (l + 1) (y * gN y n') 1 (dv_trans _ _ _ hd ⟨gN y n', Nat.mul_comm _ _⟩)
        (by rw [Nat.add_comm (y * gN y n') 1]; exact hg1))
    have hodd : ∀ c, l + 1 + 1 = 2 * c → False := fun c hc => by
      rcases hq 2 ⟨c, hc.trans (Nat.mul_comm 2 c)⟩ with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · exact hqy (by rw [← e, ← hy]; exact ⟨w * w, by lib_nat⟩)
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exact absurd (show l + 1 + 1 = 2 * (i + 1) by rw [hi]; lib_nat) (hodd (i + 1))
    · subst hm
      have hX0 : emb (ring (2 * m + 1 + 1)) y ≠ (ring (2 * m + 1 + 1)).zero := fun e => hqy (dv_of_emb _ _ e)
      obtain ⟨r, hr⟩ := hO.2 _ (Cyclo2l.ferm1 m hq _ hX0)
      refine ⟨hqy, ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩, ?_⟩
      -- Y^n = −1
      have hYn : Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L)) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by
        have h2n : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))
            ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L))) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero := by
          have e1 := hO.1
          rw [show pr (j :: L) * 2 = pr (j :: L) + pr (j :: L) by lib_nat, HasseCM.pw_add'] at e1
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L))) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L)))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
            _ = _ := by rw [e1]; lib_ring
        rcases ZMod.nozd (2 * m + 1) hq _ _ h2n with e | e
        · exfalso
          have e' : Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L)) = (ring (2 * m + 1 + 1)).one := by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (ring (2 * m + 1 + 1)).one := by lib_ring
              _ = _ := by rw [e]; lib_ring
          exact no_half _ hn1 (hO.2 _ e')
        · calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) y) (pr (j :: L))) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
            _ = _ := by rw [e]; lib_ring
      -- (−Y)^(h+1) squared is −Y
      have hmY : Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) y)) (pr (j :: L)) = (ring (2 * m + 1 + 1)).one := by
        rw [show (ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) y) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (emb (ring (2 * m + 1 + 1)) y) by lib_ring, Frobenius.pw_mul, hYn, hh, pw_m1]
        lib_ring
      rw [hh] at hmY
      have hsq : (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) y)) (h + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) y)) (h + 1)) = (ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) y) := by
        rw [← HasseCM.pw_add', show h + 1 + (h + 1) = (2 * h + 1) + 1 by lib_nat]
        show (ring (2 * m + 1 + 1)).mul (Frobenius.pw (ring (2 * m + 1 + 1)) _ (2 * h + 1)) _ = _
        rw [hmY]; lib_ring
      have hW0 : emb (ring (2 * m + 1 + 1)) w ≠ (ring (2 * m + 1 + 1)).zero := fun e => hqy (by rw [← hy]; exact dv_mul_left 2 _ _ (dv_mul_left w _ _ (dv_of_emb _ _ e)))
      have hinv := ZMod.inv_exists (2 * m + 1) hq _ hW0
      generalize Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) w) (2 * m + 1) = iw at hinv
      generalize Frobenius.pw (ring (2 * m + 1 + 1)) ((ring (2 * m + 1 + 1)).neg (emb (ring (2 * m + 1 + 1)) y)) (h + 1) = t at hsq
      have hY : emb (ring (2 * m + 1 + 1)) y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (emb (ring (2 * m + 1 + 1)) w) (emb (ring (2 * m + 1 + 1)) w)) := by
        rw [← hy, emb_mul, emb_mul]; show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one) _ = _; lib_ring
      apply mtwo (2 * m + 1) hq hodd ((ring (2 * m + 1 + 1)).mul t iw)
      calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul t t) ((ring (2 * m + 1 + 1)).mul iw iw) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul iw (emb (ring (2 * m + 1 + 1)) w)) ((ring (2 * m + 1 + 1)).mul iw (emb (ring (2 * m + 1 + 1)) w)))) := by
            rw [hsq, hY]; lib_ring
        _ = _ := by rw [hinv]; lib_ring
  have hall : ∀ l, (∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1) → Dv (l + 1 + 1) F →
      (∃ a, l + 1 + 1 = a * (1 + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (1 + 3) + (1 + 2)) := by
    intro l hq hdv
    rcases EveryPrime.nat_even_odd l with ⟨i, hi⟩ | ⟨m, hm⟩
    · exfalso
      have h2 : l + 1 + 1 = 2 := by
        cases hq 2 ⟨i + 1, by rw [hi]; lib_nat⟩ with
        | inl e => exact absurd e (fun e' => Nat.noConfusion (Nat.succ.inj e'))
        | inr e => exact e.symm
      rw [h2] at hdv; obtain ⟨r, hr⟩ := hdv; exact hFodd r hr
    · rcases EveryPrime.nat_even_odd m with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact Or.inr ⟨i, by rw [hm, hi]; lib_nat⟩
      · exact Or.inl ⟨i + 1, by rw [hm, hi]; lib_nat⟩
  obtain ⟨q, hq, ⟨b, hb⟩, hqF⟩ := hasG 1 F hall F F (Nat.le_refl _) (dv_refl _) ⟨f, by rw [hf]; lib_nat⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_add_left _ _
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqF hb
  have hq'' : ∀ e, Dv e (q' + 1 + 1) → e = 1 ∨ e = q' + 1 + 1 := hq
  obtain ⟨hqy, ⟨s, hs⟩, b8, r8, hb8, hr8⟩ := hfac q' hq'' hqF
  refine ⟨q' + 2, hq, ?_, ?_, ⟨s, hs⟩⟩
  · cases Nat.lt_or_ge B (q' + 2) with
    | inl hlt => exact hlt
    | inr hge =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · rcases hq'' 2 ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · exact hqy (by rw [← e, ← hy]; exact ⟨w * w, by lib_nat⟩)
      · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        have hB : 2 * i + 1 ≤ B + cs (j :: L) := by rw [hi] at hge; exact Nat.le_trans hge (Nat.le_add_right B _)
        have hqO := Two2.dv_ofac (B + cs (j :: L) + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        apply hqy
        show Dv (q' + 2) y
        rw [hi, ← hy, ← hW]
        exact dv_trans _ _ _ hqO ⟨2 * (pr (j :: L) * Two2.ofac (B + cs (j :: L) + 1)) * pr (j :: L), by lib_nat⟩
  · rcases hr8 with e | e
    · exfalso
      rw [e] at hb8
      have := Inf3mod8.uniq_rem 3 (2 * b8) b 1 3 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 2)) (Nat.lt_succ_self 3)
        ((show (2 * b8) * (3 + 1) + 1 = b8 * 8 + 1 by lib_nat).trans (hb8.symm.trans (hb.trans (by lib_nat))))
      exact Nat.noConfusion (Nat.succ.inj this)
    · exact ⟨b8, by show q' + 1 + 1 = _; rw [hb8, e]; lib_nat⟩

end PhiSq

#print axioms PhiSq.mtwo
#print axioms PhiSq.inf_3m8

#print axioms: 2 theorems, no axioms.

78.136 Primes congruent to \(2\) mod \(3\) and \(1\) mod \(\ell ^{e+1}\), \(\ell \equiv 5\) mod \(6\)

\(\Phi _\ell (y)\) with \(y=((2\ell O')^2)^{\ell ^e}\equiv 1\pmod 3\) is \(\equiv \ell \equiv 2\pmod 3\); its prime factors are \(\equiv 1\pmod {\ell ^{e+1}}\) and larger than \(B\).

\[ \ell \equiv 5\ (6)\ \text {prime}\ \Longrightarrow \ \forall e,B\ \exists p\ \text {prime}>B:\ p\equiv 2\ (3),\ \ell ^{e+1}\mid p-1. \]

lang/Inf2m3.lean

/-
  lang/Inf2m3 — T43, cycle 540: **p ≡ 2 (mod 3), p ≡ 1 (mod ℓ^(e+1)) beyond every bound, for every prime ℓ ≡ 5 (mod 6) and every e.**

  (ℤ/3)^× has two elements, so a value ≡ 2 (mod 3) has a prime factor ≡ 2 (mod 3). O = 3^v·O′ (Val.split), u = 2ℓO′ (3 ∤ u),
  z = u² ≡ 1 (mod 3), y = z^(ℓ^e) ≡ 1 (mod 3), G = Φ_ℓ(y) ≡ ℓ ≡ 2 (mod 3). Prime factors r of G: r ∤ u, so r ≠ 2, r > B unless r = 3,
  and r ≠ 3 since 3 ∤ G; ChebPow.ordpow gives ℓ^(e+1) | r − 1. ChebPowInf.hasG (modulus 3).
    pow31, gN31, sq31, inf_2m3
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inf3m4p
import lang.Val
import lang.PhiPow
import lang.StrongMult1

namespace Cheb

open FTAChain ZMod TwoPrime

theorem pow31 (s : Nat) : ∀ k, ∃ t, (3 * s + 1) ^ k = 3 * t + 1
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    obtain ⟨t, ht⟩ := pow31 s k
    exact ⟨3 * t * s + t + s, by show (3 * s + 1) ^ k * (3 * s + 1) = _; rw [ht]; lib_nat⟩

theorem gN31 (s : Nat) : ∀ n, ∃ k, gN (3 * s + 1) n = 3 * k + n
  | 0 => ⟨0, rfl⟩
  | n + 1 => by
    obtain ⟨k, hk⟩ := gN31 s n
    obtain ⟨t, ht⟩ := pow31 s n
    exact ⟨k + t, by show gN (3 * s + 1) n + (3 * s + 1) ^ n = _; rw [hk, ht]; lib_nat⟩

theorem sq31 (u : Nat) (h3 : Dv 3 u → False) : ∃ s, u * u = 3 * s + 1 := by
  obtain ⟨t, ρ, ht, hρ⟩ := Order.divmod 2 u
  match ρ, hρ with
  | 0, _ => exact absurd ⟨t, by rw [ht]; lib_nat⟩ h3
  | 1, _ => exact ⟨3 * t * t + 2 * t, by rw [ht]; lib_nat⟩
  | 2, _ => exact ⟨3 * t * t + 4 * t + 1, by rw [ht]; lib_nat⟩
  | ρ + 3, hρ => exact absurd hρ (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 ρ) h))

theorem inf_2m3 (c e B : Nat) (hl : ∀ d, Dv d (2 * (3 * c + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (3 * c + 1) + 1 + 1 + 1)) :
    ∃ p, (∀ d, Dv d p → d = 1 ∨ d = p) ∧ B < p ∧ (∃ k, p = 3 * k + 2) ∧ ∃ q, p = q * (2 * (3 * c + 1) + 1 + 1 + 1) ^ (e + 1) + 1 := by
  have h3p : ∀ d, Dv d (1 + 1 + 1) → d = 1 ∨ d = 1 + 1 + 1 := StrongMult1.three_prime
  -- O = 3^v · O′
  have hO1 : 1 ≤ Two2.ofac (B + 1) := by obtain ⟨a, ha⟩ := Two2.ofac_odd (B + 1); rw [ha]; exact Nat.le_add_left 1 _
  obtain ⟨v, O', hvO, hO'⟩ := Val.split 1 _ _ (Nat.le_refl _) hO1
  have h32 : Dv (1 + 1 + 1) 2 → False := fun h => by
    obtain ⟨k, hk⟩ := dv_le _ 1 h
    exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj ((show k + 1 + 1 + 1 = 1 + 1 + 1 + k by lib_nat).trans hk)))
  have h3l : Dv (1 + 1 + 1) (2 * (3 * c + 1) + 1 + 1 + 1) → False := fun h => by
    obtain ⟨k, hk⟩ := h
    exact Nat.noConfusion ((Inf3mod8.uniq_rem 2 k (2 * c + 1) 0 2 (Nat.zero_lt_succ 2) (Nat.lt_succ_self 2)
      ((show k * (2 + 1) + 0 = k * (1 + 1 + 1) by lib_nat).trans (hk.symm.trans (by lib_nat)))))
  generalize hu : 2 * (2 * (3 * c + 1) + 1 + 1 + 1) * O' = u
  have h3u : Dv (1 + 1 + 1) u → False := by
    rw [← hu]; exact Val.ndv_mul 1 _ O' h3p (Val.ndv_mul 1 2 _ h3p h32 h3l) hO'
  obtain ⟨s0, hs0⟩ := sq31 u h3u
  obtain ⟨t, ht⟩ := pow31 s0 ((2 * (3 * c + 1) + 1 + 1 + 1) ^ e)
  rw [← hs0] at ht
  generalize hY : (u * u) ^ ((2 * (3 * c + 1) + 1 + 1 + 1) ^ e) = y at ht
  obtain ⟨k0, hk0⟩ := gN31 t (2 * (3 * c + 1) + 1 + 1 + 1)
  rw [← ht] at hk0
  have hE : 1 ≤ (2 * (3 * c + 1) + 1 + 1 + 1) ^ e := pow_pos' _ (Nat.le_add_left 1 _) e
  obtain ⟨E, hE'⟩ := le_dest2 1 _ hE
  have huy : Dv u y := ⟨(u * u) ^ E * u, by rw [← hY, ← hE']; show (u * u) ^ E * (u * u) = _; lib_nat⟩
  have hlu : Dv (2 * (3 * c + 1) + 1 + 1 + 1) u := ⟨2 * O', by rw [← hu]; lib_nat⟩
  have h2u : Dv 2 u := ⟨(2 * (3 * c + 1) + 1 + 1 + 1) * O', by rw [← hu]; lib_nat⟩
  have hG1 : gN y (2 * (3 * c + 1) + 1 + 1 + 1) = 1 + y * gN y (2 * (3 * c + 1) + 1 + 1) := by
    show gN y ((2 * (3 * c + 1) + 1 + 1) + 1) = _
    rw [Nat.add_comm _ 1, gN_one]
  have hcop : ∀ l, Dv (l + 1 + 1) (gN y (2 * (3 * c + 1) + 1 + 1 + 1)) → Dv (l + 1 + 1) u → False := fun l h1 h2 => by
    rw [hG1] at h1
    exact dv_one_absurd l (dv_sub (l + 1) (y * gN y (2 * (3 * c + 1) + 1 + 1)) 1
      (dv_trans _ _ _ (dv_trans _ _ _ h2 huy) ⟨gN y (2 * (3 * c + 1) + 1 + 1), Nat.mul_comm _ _⟩)
      (by rw [Nat.add_comm (y * gN y (2 * (3 * c + 1) + 1 + 1)) 1]; exact h1))
  have hG2 : gN y (2 * (3 * c + 1) + 1 + 1 + 1) = (k0 + 2 * c + 1) * 3 + 2 := by rw [hk0]; lib_nat
  have hG3 : Dv (1 + 1 + 1) (gN y (2 * (3 * c + 1) + 1 + 1 + 1)) → False := fun h => by
    obtain ⟨k, hk⟩ := h
    exact Nat.noConfusion (Inf3mod8.uniq_rem 2 k (k0 + 2 * c + 1) 0 2 (Nat.zero_lt_succ 2) (Nat.lt_succ_self 2)
      ((show k * (2 + 1) + 0 = k * (1 + 1 + 1) by lib_nat).trans (hk.symm.trans (by rw [hG2]))))
  have hall : ∀ l, (∀ d, Dv d (l + 1 + 1) → d = 1 ∨ d = l + 1 + 1) → Dv (l + 1 + 1) (gN y (2 * (3 * c + 1) + 1 + 1 + 1)) →
      (∃ a, l + 1 + 1 = a * (0 + 3) + 1) ∨ (∃ a, l + 1 + 1 = a * (0 + 3) + (0 + 2)) := by
    intro l hq hdv
    obtain ⟨a, ρ, ha, hρ⟩ := Order.divmod 2 (l + 1 + 1)
    match ρ, hρ, ha with
    | 0, _, ha =>
      exfalso
      rcases hq 3 ⟨a, by rw [ha]; lib_nat⟩ with e | e
      · exact Nat.noConfusion (Nat.succ.inj e)
      · exact hG3 (by rw [show 1 + 1 + 1 = l + 1 + 1 from e]; exact hdv)
    | 1, _, ha => exact Or.inl ⟨a, by rw [ha]⟩
    | 2, _, ha => exact Or.inr ⟨a, by rw [ha]⟩
    | ρ + 3, hρ, _ => exact absurd hρ (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 ρ) h))
  obtain ⟨q, hq, ⟨b, hb⟩, hqG⟩ := hasG 0 _ hall _ _ (Nat.le_refl _) (dv_refl _) ⟨k0 + 2 * c + 1, by rw [hG2]⟩
  have hq2 : 2 ≤ q := by rw [hb]; exact Nat.le_add_left _ _
  obtain ⟨q', hq'⟩ := le_dest2 2 q hq2
  rw [← hq'] at hq hqG hb
  have hq'' : ∀ d, Dv d (q' + 1 + 1) → d = 1 ∨ d = q' + 1 + 1 := hq
  have hqu : Dv (q' + 1 + 1) u → False := hcop q' hqG
  have hodd : ∀ i, q' + 1 + 1 = 2 * i → False := fun i hi => by
    rcases hq'' 2 ⟨i, hi.trans (Nat.mul_comm 2 i)⟩ with e | e
    · exact Nat.noConfusion (Nat.succ.inj e)
    · exact hqu (by rw [← e]; exact h2u)
  refine ⟨q' + 2, hq, ?_, ⟨b, by show q' + 1 + 1 = _; rw [hb]; lib_nat⟩, ?_⟩
  · cases Nat.lt_or_ge B (q' + 2) with
    | inl h => exact h
    | inr h =>
      exfalso
      rcases EveryPrime.nat_even_odd (q' + 2) with ⟨i, hi⟩ | ⟨i, hi⟩
      · exact hodd i hi
      · have hle : i ≤ 2 * i + 1 := Nat.le_succ_of_le (Nat.le_trans (Nat.le_add_left i i) (Nat.le_of_eq (by lib_nat)))
        have hB : 2 * i + 1 ≤ B := by rw [hi] at h; exact h
        have hqO := Two2.dv_ofac (B + 1) i (Nat.succ_le_succ (Nat.le_trans hle hB))
        rw [← hi, hvO] at hqO
        have hP : 1 ≤ (1 + 1 + 1) ^ v := pow_pos' _ (Nat.le_add_left 1 _) v
        obtain ⟨P, hP'⟩ := le_dest2 1 _ hP
        rw [← hP'] at hqO
        rcases euclid_core q' P O' hq'' hqO with e | e
        · rw [hP'] at e
          have e' := PhiSq.dv_pow_prime q' _ hq'' (Nat.le_add_left 1 _) v e
          rcases h3p _ e' with e2 | e2
          · exact Nat.noConfusion (Nat.succ.inj e2)
          · rw [e2] at hb
            exact Nat.noConfusion (Inf3mod8.uniq_rem 2 1 b 0 2 (Nat.zero_lt_succ 2) (Nat.lt_succ_self 2) (by rw [← hb]))
        · exact hqu (by rw [← hu]; exact dv_mul_left _ _ _ e)
  · rcases EveryPrime.nat_even_odd q' with ⟨i, hi⟩ | ⟨m, hm⟩
    · exact absurd (show q' + 1 + 1 = 2 * (i + 1) by rw [hi]; lib_nat) (hodd (i + 1))
    · subst hm
      have hz0 : emb (ring (2 * m + 1 + 1)) (u * u) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => by
        have h1 := dv_of_emb _ _ e0
        match u, h3u, hqu, h1 with
        | 0, h3u, _, _ => exact h3u ⟨0, rfl⟩
        | u' + 1, _, hqu, h1 =>
          rcases euclid_core (2 * m + 1) u' (u' + 1) hq'' h1 with h | h
          · exact hqu h
          · exact hqu h
      have hg : geo (ring (2 * m + 1 + 1)) (Frobenius.pw (ring (2 * m + 1 + 1)) (emb (ring (2 * m + 1 + 1)) (u * u)) ((2 * (3 * c + 1) + 1 + 1 + 1) ^ e))
          (2 * (3 * c + 1) + 1 + 1 + 1) = (ring (2 * m + 1 + 1)).zero := by
        rw [← Cyclo2l.emb_pow, hY, ← emb_gN]; exact emb_dv _ _ hqG
      rcases ordpow (ring (2 * m + 1 + 1)) (2 * (3 * c + 1) + 1) hl e _ _ hg (Cyclo2l.ferm1 m hq'' _ hz0) with ⟨r, hr⟩ | h
      · exact ⟨r, by show 2 * m + 1 + 1 + 1 = _; rw [hr]⟩
      · exfalso
        rw [geo_one_emb] at h
        rcases hl _ (dv_of_emb _ _ h) with e | e
        · exact Nat.noConfusion (Nat.succ.inj e)
        · exact hqu (by rw [e]; exact hlu)

end Cheb

#print axioms Cheb.pow31
#print axioms Cheb.gN31
#print axioms Cheb.sq31
#print axioms Cheb.inf_2m3

#print axioms: 4 theorems, no axioms.

78.137 Gauss’s identity at every degree

In every commutative ring with \(q^M=0\) the finite triple product at \(z=-1\) gives Gauss’s identity.

\[ \sum _{m\le 4M}(-1)^mq^{(m-2M)^2}=(q^2;q^2)_M\prod _{k<2M}\bigl (-(1-q^{2k+1})^2\bigr ). \]

lang/GaussMod.lean

/-
  lang/GaussMod — T47, cycle 549: **Gauss's identity modulo q^M**: in any commutative ring with q^M = 0,
    Σ_{m ≤ 4M} (−1)^m q^((m − 2M)²) = (q²; q²)_M · ∏_{k<2M} (−(1 − q^(2k+1))²),
  i.e. Σ_j (−1)^j q^(j²) = ∏(1 − q^(2k))(1 − q^(2k−1))² = ∏(1 − q^n)²/(1 − q^(2n)) coefficientwise below degree M.

    pw_ge, pp_stable, unit_sub, pp_unit    nilpotent bookkeeping: q^(M+t) = 0, (Q;Q)_(T+t) = (Q;Q)_T, 1 − x and (Q;Q)_n are units
    ex_ge                                   Ex N m ≥ N − m and ≥ m − N
    es_scale                                c·Σ f = Σ g if c·f = g termwise
    gauss_mod
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.FinJTP3
import lang.ChebKey

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

theorem pw_ge (x : R) (M : Nat) (h : Frobenius.pw K x M = K.zero) : ∀ t, Frobenius.pw K x (M + t) = K.zero
  | 0 => h
  | t + 1 => by show K.mul (Frobenius.pw K x (M + t)) x = K.zero; rw [pw_ge x M h t]; lib_ring

theorem pp_stable (Q : R) (T : Nat) (h : Frobenius.pw K Q T = K.zero) : ∀ t, pp K Q (T + t) = pp K Q T
  | 0 => rfl
  | t + 1 => by
    show K.mul (pp K Q (T + t)) (K.add K.one (K.neg (Frobenius.pw K Q (T + t + 1)))) = _
    rw [pp_stable Q T h t, show T + t + 1 = T + (t + 1) from rfl, pw_ge K Q T h]; lib_ring

theorem unit_sub (x : R) (M : Nat) (h : Frobenius.pw K x M = K.zero) : ∃ y, K.mul (K.add K.one (K.neg x)) y = K.one :=
  ⟨Cheb.geo K x M, by
    have g := Cheb.geo_mul K x M
    rw [h] at g
    calc _ = K.neg (K.mul (K.add x (K.neg K.one)) (Cheb.geo K x M)) := by lib_ring
      _ = _ := by rw [g]; lib_ring⟩

theorem pp_unit (Q : R) (T : Nat) (h : Frobenius.pw K Q T = K.zero) : ∀ n, ∃ y, K.mul (pp K Q n) y = K.one
  | 0 => ⟨K.one, by show K.mul K.one K.one = K.one; lib_ring⟩
  | n + 1 => by
    obtain ⟨y, hy⟩ := pp_unit Q T h n
    have hx : Frobenius.pw K (Frobenius.pw K Q (n + 1)) T = K.zero := by
      rw [TwoPower.pwpow, show (n + 1) * T = T + n * T by lib_nat, pw_ge K Q T h]
    obtain ⟨u, hu⟩ := unit_sub K _ T hx
    refine ⟨K.mul y u, ?_⟩
    show K.mul (K.mul (pp K Q n) (K.add K.one (K.neg (Frobenius.pw K Q (n + 1))))) (K.mul y u) = K.one
    calc _ = K.mul (K.mul (pp K Q n) y) (K.mul (K.add K.one (K.neg (Frobenius.pw K Q (n + 1)))) u) := by lib_ring
      _ = _ := by rw [hy, hu]; lib_ring

theorem le_sq' (a : Nat) : a ≤ a * a := by
  cases a with
  | zero => exact Nat.le_refl 0
  | succ k => exact Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.succ_le_succ (Nat.zero_le k)))

theorem ex_ge_l (N m : Nat) : N - m ≤ Ex N m := Nat.le_trans (le_sq' _) (Nat.le_add_left _ _)
theorem ex_ge_r (N m : Nat) : m - N ≤ Ex N m := Nat.le_trans (le_sq' _) (Nat.le_add_right _ _)

theorem es_scale (c : R) (f g : Nat → R) (z : R) : ∀ L, (∀ m, m < L → K.mul c (f m) = g m) → K.mul c (es K f z L) = es K g z L
  | 0, _ => by show K.mul c K.zero = K.zero; lib_ring
  | L + 1, h => by
    show K.mul c (K.add (es K f z L) (K.mul (f L) (Frobenius.pw K z L))) = K.add (es K g z L) (K.mul (g L) (Frobenius.pw K z L))
    rw [← es_scale c f g z L (fun m hm => h m (Nat.lt_succ_of_lt hm)), ← h L (Nat.lt_succ_self L)]; lib_ring

theorem pw_zero_of_le (x : R) (M E : Nat) (h : Frobenius.pw K x M = K.zero) (hE : M ≤ E) : Frobenius.pw K x E = K.zero := by
  obtain ⟨t, ht⟩ := Cheb.le_dest2 M E hE
  rw [← ht, Nat.add_comm]; exact pw_ge K x M h t

/-- **Gauss's identity mod q^M** (N = 2M, Q = q², P = (Q; Q)_M). -/
theorem gauss_mod (q : R) (M : Nat) (hq : Frobenius.pw K q M = K.zero) :
    es K (fun m => Frobenius.pw K q (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1) =
      K.mul (pp K (K.mul q q) M) (jf K q (K.neg K.one) (2 * M)) := by
  have hQ : Frobenius.pw K (K.mul q q) M = K.zero := by
    rw [pwQ]; exact pw_zero_of_le K q M _ hq (Nat.le_trans (Nat.le_add_left M M) (Nat.le_of_eq (by lib_nat)))
  have hst := pp_stable K (K.mul q q) M hQ
  generalize hP : pp K (K.mul q q) M = P at hst
  -- termwise: P·P·bj N m = P·q^(Ex N m)
  have hterm : ∀ m, m < 2 * (2 * M) + 1 → K.mul (K.mul P P) (bj K q (2 * M) m) = K.mul P (Frobenius.pw K q (Ex (2 * M) m)) := by
    intro m hm
    rw [fjtp]
    cases Nat.lt_or_ge m M with
    | inl h1 =>
      have hz : Frobenius.pw K q (Ex (2 * M) m) = K.zero := by
        apply pw_zero_of_le K q M _ hq
        apply Nat.le_trans _ (ex_ge_l (2 * M) m)
        obtain ⟨d, hd⟩ := Cheb.le_dest2 (m + 1) M h1
        rw [show 2 * M = m + (d + 1 + M) by rw [← hd]; lib_nat, sub_add_l]
        exact Nat.le_add_left M (d + 1)
      rw [hz]; lib_ring
    | inr h1 =>
      cases Nat.lt_or_ge (3 * M) m with
      | inl h2 =>
        have hz : Frobenius.pw K q (Ex (2 * M) m) = K.zero := by
          apply pw_zero_of_le K q M _ hq
          apply Nat.le_trans _ (ex_ge_r (2 * M) m)
          obtain ⟨d, hd⟩ := Cheb.le_dest2 (3 * M + 1) m h2
          rw [show m = 2 * M + (d + M + 1) by rw [← hd]; lib_nat, sub_add_l]
          exact Nat.le_trans (Nat.le_add_left M d) (Nat.le_succ _)
        rw [hz]; lib_ring
      | inr h2 =>
        have hm2 : m ≤ 2 * (2 * M) := Nat.le_trans h2 (Nat.le_trans (Nat.le_add_right (3 * M) M) (Nat.le_of_eq (by lib_nat)))
        have hg := gb_pp K (K.mul q q) (2 * (2 * M)) m hm2
        obtain ⟨a, ha⟩ := Cheb.le_dest2 M m h1
        obtain ⟨c, hc⟩ := Cheb.le_dest2 m (3 * M) h2
        have h4 : 2 * (2 * M) = m + (M + c) := by
          rw [show 2 * (2 * M) = (c + m) + M by rw [hc]; lib_nat]; lib_nat
        have hs : 2 * (2 * M) - m = M + c := by rw [h4, sub_add_l]
        have e1 : pp K (K.mul q q) m = P := by rw [← ha, Nat.add_comm, hst]
        have e2 : pp K (K.mul q q) (2 * (2 * M) - m) = P := by rw [hs, hst]
        have e3 : pp K (K.mul q q) (2 * (2 * M)) = P := by rw [show 2 * (2 * M) = M + 3 * M by lib_nat, hst]
        rw [e1, e2, e3] at hg
        generalize gb K (K.mul q q) (2 * (2 * M)) m = G at hg ⊢
        calc _ = K.mul (K.mul (K.mul G P) P) (Frobenius.pw K q (Ex (2 * M) m)) := by lib_ring
          _ = _ := by rw [hg]
  have hes := es_scale K (K.mul P P) (bj K q (2 * M)) _ (K.neg K.one) _ hterm
  have hes2 := es_scale K P (fun m => Frobenius.pw K q (Ex (2 * M) m)) _ (K.neg K.one) (2 * (2 * M) + 1) (fun m _ => rfl)
  rw [ev_jtp, ← hes2] at hes
  -- cancel P
  obtain ⟨y, hy⟩ := pp_unit K (K.mul q q) M hQ M
  rw [hP] at hy
  calc es K (fun m => Frobenius.pw K q (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1)
      = K.mul (K.mul P y) (es K (fun m => Frobenius.pw K q (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1)) := by rw [hy]; lib_ring
    _ = K.mul y (K.mul P (es K (fun m => Frobenius.pw K q (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1))) := by lib_ring
    _ = K.mul y (K.mul (K.mul P P) (jf K q (K.neg K.one) (2 * M))) := by rw [← hes]
    _ = K.mul (K.mul P y) (K.mul P (jf K q (K.neg K.one) (2 * M))) := by lib_ring
    _ = _ := by rw [hy]; lib_ring

end QBinom

#print axioms QBinom.pw_ge
#print axioms QBinom.pp_stable
#print axioms QBinom.unit_sub
#print axioms QBinom.pp_unit
#print axioms QBinom.le_sq'
#print axioms QBinom.ex_ge_l
#print axioms QBinom.ex_ge_r
#print axioms QBinom.es_scale
#print axioms QBinom.pw_zero_of_le
#print axioms QBinom.gauss_mod

#print axioms: 10 theorems, no axioms.

78.138 Jacobi’s identity for \(\eta ^3\) at every degree

In every commutative ring with \(q^M=0\), from \(x\,d/dx\) of \(\prod _{k\le N}(1+xq^{2k})(x+q^{2k-2})\) at \(x=-1\).

\[ \sum _{j<2M+2}(-1)^j(2j+1)q^{j^2+j}=\bigl ((q^2;q^2)_{M+1}\bigr )^3. \]

lang/JacobiP.lean

/-
  lang/JacobiP — T47, cycle 556: **Jacobi's identity for η³ at every degree**: in every commutative ring with q^M = 0,
    Σ_{j < 2M+2} (−1)^j (2j + 1) q^(j² + j) = ((q²; q²)_(M+1))³,
  i.e. ∏_{n ≥ 1} (1 − Q^n)³ = Σ_{j ≥ 0} (−1)^j (2j + 1) Q^(j(j+1)/2) coefficientwise (Q = q²).

    es_congr, em_congr, es_shift, em_shift    pointwise congruence and index shift of the sums
    e2_shift                                  Ex2 (N+1) (m+1) = Ex2 N m
    pm_even, pm_sq                            (−1)^(2N) = 1, ((−1)^N)² = 1
    w_zero                                    Σ_{m<2N} (−1)^m q^(Ex2 N m) = 0
    v_pair                                    Σ_{m<2N} m (−1)^m q^(Ex2 N m) = (−1)^N · T N,  T N = Σ_{j<N} (−1)^j (2j+1) q^(j²+j)
    jacobi_eta3
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.JacobiL

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

theorem es_congr (f g : Nat → R) (z : R) (h : ∀ m, f m = g m) : ∀ L, es K f z L = es K g z L
  | 0 => rfl
  | L + 1 => by
    show K.add (es K f z L) (K.mul (f L) (Frobenius.pw K z L)) = K.add (es K g z L) (K.mul (g L) (Frobenius.pw K z L))
    rw [es_congr f g z h L, h L]

theorem em_congr (f g : Nat → R) (z : R) (h : ∀ m, f m = g m) : ∀ L, em K f z L = em K g z L
  | 0 => rfl
  | L + 1 => by
    show K.add (em K f z L) (K.mul (Cheb.emb K L) (K.mul (f L) (Frobenius.pw K z L))) =
      K.add (em K g z L) (K.mul (Cheb.emb K L) (K.mul (g L) (Frobenius.pw K z L)))
    rw [em_congr f g z h L, h L]

theorem es_shift (f : Nat → R) (z : R) : ∀ L, es K f z (L + 1) = K.add (f 0) (K.mul z (es K (fun m => f (m + 1)) z L))
  | 0 => by show K.add K.zero (K.mul (f 0) K.one) = K.add (f 0) (K.mul z K.zero); lib_ring
  | L + 1 => by
    show K.add (es K f z (L + 1)) (K.mul (f (L + 1)) (Frobenius.pw K z (L + 1))) =
      K.add (f 0) (K.mul z (K.add (es K (fun m => f (m + 1)) z L) (K.mul (f (L + 1)) (Frobenius.pw K z L))))
    rw [es_shift f z L, show Frobenius.pw K z (L + 1) = K.mul (Frobenius.pw K z L) z from rfl]
    lib_ring

theorem em_shift (f : Nat → R) (z : R) : ∀ L,
    em K f z (L + 1) = K.mul z (K.add (em K (fun m => f (m + 1)) z L) (es K (fun m => f (m + 1)) z L))
  | 0 => by show K.add K.zero (K.mul K.zero (K.mul (f 0) K.one)) = K.mul z (K.add K.zero K.zero); lib_ring
  | L + 1 => by
    show K.add (em K f z (L + 1)) (K.mul (Cheb.emb K (L + 1)) (K.mul (f (L + 1)) (Frobenius.pw K z (L + 1)))) =
      K.mul z (K.add (K.add (em K (fun m => f (m + 1)) z L) (K.mul (Cheb.emb K L) (K.mul (f (L + 1)) (Frobenius.pw K z L))))
        (K.add (es K (fun m => f (m + 1)) z L) (K.mul (f (L + 1)) (Frobenius.pw K z L))))
    rw [em_shift f z L, show Frobenius.pw K z (L + 1) = K.mul (Frobenius.pw K z L) z from rfl,
      show Cheb.emb K (L + 1) = K.add (Cheb.emb K L) K.one from rfl]
    lib_ring

theorem e2_shift (N m : Nat) : Ex2 (N + 1) (m + 1) = Ex2 N m := by
  show (m + 1 - (N + 1)) * (m + 1 - (N + 1) + 1) + (N + 1 - (m + 1 + 1)) * (N + 1 - (m + 1 + 1) + 1) =
    (m - N) * (m - N + 1) + (N - (m + 1)) * (N - (m + 1) + 1)
  rw [LibInt.nsub_succ_succ, LibInt.nsub_succ_succ]

theorem pm_even : ∀ N, Frobenius.pw K (K.neg K.one) (2 * N) = K.one
  | 0 => rfl
  | N + 1 => by
    rw [show 2 * (N + 1) = 2 * N + 1 + 1 by lib_nat]
    show K.mul (K.mul (Frobenius.pw K (K.neg K.one) (2 * N)) (K.neg K.one)) (K.neg K.one) = K.one
    rw [pm_even N]; lib_ring

theorem pm_sq : ∀ N, K.mul (Frobenius.pw K (K.neg K.one) N) (Frobenius.pw K (K.neg K.one) N) = K.one
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | N + 1 => by
    show K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one)) (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one)) = K.one
    calc _ = K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (Frobenius.pw K (K.neg K.one) N)) (K.mul (K.neg K.one) (K.neg K.one)) := by lib_ring
      _ = _ := by rw [pm_sq N]; lib_ring

theorem sg_odd : ∀ M, sg K (2 * M + 1) = K.one
  | 0 => by show K.neg (K.neg K.one) = K.one; lib_ring
  | M + 1 => by
    rw [show 2 * (M + 1) + 1 = 2 * M + 1 + 1 + 1 by lib_nat]
    show K.neg (K.neg (sg K (2 * M + 1))) = K.one
    rw [sg_odd M]; lib_ring

variable (q : R)

theorem a_dn (N : Nat) : Ex2 (N + 1) 0 = N * N + N := by rw [show N + 1 = 0 + 1 + N by lib_nat, e2_dn]
theorem a_up (N : Nat) : Ex2 N (2 * N) = N * N + N := by rw [show 2 * N = N + N by lib_nat, e2_up]

theorem w_zero : ∀ N, es K (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one) (2 * N) = K.zero
  | 0 => rfl
  | N + 1 => by
    have hc := es_congr K (fun m => Frobenius.pw K q (Ex2 (N + 1) (m + 1))) (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one)
      (fun m => congrArg (Frobenius.pw K q) (e2_shift N m)) (2 * N + 1)
    rw [show 2 * (N + 1) = 2 * N + 1 + 1 by lib_nat, es_shift, hc]
    show K.add (Frobenius.pw K q (Ex2 (N + 1) 0)) (K.mul (K.neg K.one) (K.add (es K (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one) (2 * N))
      (K.mul (Frobenius.pw K q (Ex2 N (2 * N))) (Frobenius.pw K (K.neg K.one) (2 * N))))) = K.zero
    rw [w_zero N, pm_even, a_dn, a_up]; lib_ring

def T : Nat → R
  | 0 => K.zero
  | N + 1 => K.add (T N) (K.mul (Frobenius.pw K (K.neg K.one) N) (K.mul (Cheb.emb K (2 * N + 1)) (Frobenius.pw K q (N * N + N))))

theorem v_pair : ∀ N, em K (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one) (2 * N) =
    K.mul (Frobenius.pw K (K.neg K.one) N) (T K q N)
  | 0 => by show K.zero = K.mul K.one K.zero; lib_ring
  | N + 1 => by
    have hc := es_congr K (fun m => Frobenius.pw K q (Ex2 (N + 1) (m + 1))) (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one)
      (fun m => congrArg (Frobenius.pw K q) (e2_shift N m)) (2 * N + 1)
    have hc2 := em_congr K (fun m => Frobenius.pw K q (Ex2 (N + 1) (m + 1))) (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one)
      (fun m => congrArg (Frobenius.pw K q) (e2_shift N m)) (2 * N + 1)
    rw [show 2 * (N + 1) = 2 * N + 1 + 1 by lib_nat, em_shift, hc, hc2]
    show K.mul (K.neg K.one) (K.add (K.add (em K (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one) (2 * N))
        (K.mul (Cheb.emb K (2 * N)) (K.mul (Frobenius.pw K q (Ex2 N (2 * N))) (Frobenius.pw K (K.neg K.one) (2 * N)))))
      (K.add (es K (fun m => Frobenius.pw K q (Ex2 N m)) (K.neg K.one) (2 * N))
        (K.mul (Frobenius.pw K q (Ex2 N (2 * N))) (Frobenius.pw K (K.neg K.one) (2 * N))))) =
      K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one))
        (K.add (T K q N) (K.mul (Frobenius.pw K (K.neg K.one) N) (K.mul (K.add (Cheb.emb K (2 * N)) K.one) (Frobenius.pw K q (N * N + N)))))
    rw [v_pair N, w_zero, pm_even, a_up]
    have hs := pm_sq K N
    generalize Frobenius.pw K (K.neg K.one) N = s at hs ⊢
    calc _ = K.mul (K.neg K.one) (K.add (K.mul s (T K q N)) (K.mul (K.add (Cheb.emb K (2 * N)) K.one) (Frobenius.pw K q (N * N + N)))) := by lib_ring
      _ = K.mul (K.neg K.one) (K.add (K.mul s (T K q N)) (K.mul (K.mul s s) (K.mul (K.add (Cheb.emb K (2 * N)) K.one) (Frobenius.pw K q (N * N + N))))) := by
          rw [hs]; lib_ring
      _ = _ := by lib_ring

/-- **Jacobi's η³ identity mod q^M**: Σ_{j < 2M+2} (−1)^j (2j+1) q^(j²+j) = (q²; q²)_(M+1)³. -/
theorem jacobi_eta3 (M : Nat) (hq : Frobenius.pw K q M = K.zero) :
    T K q (2 * M + 2) = K.mul (K.mul (pp K (K.mul q q) (M + 1)) (pp K (K.mul q q) (M + 1))) (pp K (K.mul q q) (M + 1)) := by
  have h := jacobi3_mod K q M hq
  have hz : Frobenius.pw K q (Ex2 (2 * M + 2) (2 * (2 * M + 2))) = K.zero := by
    rw [a_up]
    exact pw_zero_of_le K q M _ hq (Nat.le_trans (Nat.le_trans (Nat.le_add_left M (M + 2)) (Nat.le_of_eq (by lib_nat))) (Nat.le_add_left _ _))
  have e : em K (fun m => Frobenius.pw K q (Ex2 (2 * M + 2) m)) (K.neg K.one) (2 * (2 * M + 2) + 1) =
      K.add (em K (fun m => Frobenius.pw K q (Ex2 (2 * M + 2) m)) (K.neg K.one) (2 * (2 * M + 2)))
        (K.mul (Cheb.emb K (2 * (2 * M + 2))) (K.mul (Frobenius.pw K q (Ex2 (2 * M + 2) (2 * (2 * M + 2)))) (Frobenius.pw K (K.neg K.one) (2 * (2 * M + 2))))) := rfl
  rw [e, hz, v_pair, sg_odd, show 2 * M + 2 = 2 * (M + 1) by lib_nat, pm_even] at h
  rw [show 2 * M + 2 = 2 * (M + 1) by lib_nat]
  calc _ = K.add (K.mul K.one (T K q (2 * (M + 1)))) (K.mul (Cheb.emb K (2 * (2 * (M + 1)))) (K.mul K.zero (Frobenius.pw K (K.neg K.one) (2 * (2 * (M + 1)))))) := by lib_ring
    _ = _ := by rw [h]; lib_ring

end QBinom

#print axioms QBinom.es_congr
#print axioms QBinom.em_congr
#print axioms QBinom.es_shift
#print axioms QBinom.em_shift
#print axioms QBinom.e2_shift
#print axioms QBinom.pm_even
#print axioms QBinom.pm_sq
#print axioms QBinom.sg_odd
#print axioms QBinom.a_dn
#print axioms QBinom.a_up
#print axioms QBinom.w_zero
#print axioms QBinom.v_pair
#print axioms QBinom.jacobi_eta3

#print axioms: 13 theorems, no axioms.

78.139 The eta product of 32a as a theta product

With \(x=q^4\) and \(q^M=0\), Gauss’s and Jacobi’s identities give the product \(\eta (4z)^2\eta (8z)^2\) at every coefficient.

\[ q\,(x;x)_M^2(x^2;x^2)_M^2=q\sum _{m\le 4M}(-1)^mx^{(m-2M)^2}\sum _{j<2M+2}(-1)^j(2j+1)x^{j^2+j}. \]

lang/Eta32.lean

/-
  lang/Eta32 — T47, cycle 557: **the 32a eta product as a product of two theta series, at every degree**.
  In every commutative ring with q^M = 0, with x = q^4:
    q·(x; x)_M²·(x²; x²)_M² = q · G(x) · T(x),
    G(x) = Σ_{m ≤ 4M} (−1)^m x^((m − 2M)²)  (Gauss),   T(x) = Σ_{j < 2M+2} (−1)^j (2j+1) x^(j² + j)  (Jacobi),
  i.e. η(4z)²η(8z)² = Σ_{n ∈ ℤ, j ≥ 0} (−1)^(n+j) (2j+1) q^(4n² + (2j+1)²) coefficientwise.

    od x T          ∏_{k<T} (1 − x^(2k+1))
    pp_split        (x; x)_(2T) = (x²; x²)_T · od x T
    jf_neg          the Gauss product ∏_{k<N}(q^(2k+1) − (1 + q^(4k+2)) + q^(2k+1)) = (−1)^N od q N²
    gauss_prod      G(q)·(q²; q²)_M = (q; q)_M²  when q^M = 0
    eta32
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.JacobiP

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

def od (x : R) : Nat → R
  | 0 => K.one
  | T + 1 => K.mul (od x T) (K.add K.one (K.neg (Frobenius.pw K x (2 * T + 1))))

theorem pp_split (x : R) : ∀ T, pp K x (2 * T) = K.mul (pp K (K.mul x x) T) (od K x T)
  | 0 => by show K.one = K.mul K.one K.one; lib_ring
  | T + 1 => by
    rw [show 2 * (T + 1) = 2 * T + 1 + 1 by lib_nat]
    show K.mul (K.mul (pp K x (2 * T)) (K.add K.one (K.neg (Frobenius.pw K x (2 * T + 1))))) (K.add K.one (K.neg (Frobenius.pw K x (2 * T + 1 + 1)))) =
      K.mul (K.mul (pp K (K.mul x x) T) (K.add K.one (K.neg (Frobenius.pw K (K.mul x x) (T + 1)))))
        (K.mul (od K x T) (K.add K.one (K.neg (Frobenius.pw K x (2 * T + 1)))))
    rw [pp_split x T, pwQ, show 2 * (T + 1) = 2 * T + 1 + 1 by lib_nat]
    lib_ring

theorem jf_neg (q : R) : ∀ N, jf K q (K.neg K.one) N = K.mul (Frobenius.pw K (K.neg K.one) N) (K.mul (od K q N) (od K q N))
  | 0 => by show K.one = K.mul K.one (K.mul K.one K.one); lib_ring
  | N + 1 => by
    show K.mul (jf K q (K.neg K.one) N) (K.add (K.add (Frobenius.pw K q (2 * N + 1)) (K.mul (K.add K.one (Frobenius.pw K q (4 * N + 2))) (K.neg K.one)))
        (K.mul (Frobenius.pw K q (2 * N + 1)) (K.mul (K.neg K.one) (K.neg K.one)))) =
      K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one))
        (K.mul (K.mul (od K q N) (K.add K.one (K.neg (Frobenius.pw K q (2 * N + 1))))) (K.mul (od K q N) (K.add K.one (K.neg (Frobenius.pw K q (2 * N + 1))))))
    have e : Frobenius.pw K q (4 * N + 2) = K.mul (Frobenius.pw K q (2 * N + 1)) (Frobenius.pw K q (2 * N + 1)) :=
      (pw_join K q _ _ _ (by lib_nat)).symm
    rw [jf_neg q N, e]
    generalize Frobenius.pw K q (2 * N + 1) = A
    lib_ring

theorem gauss_prod (q : R) (M : Nat) (hq : Frobenius.pw K q M = K.zero) :
    K.mul (es K (fun m => Frobenius.pw K q (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1)) (pp K (K.mul q q) M) =
      K.mul (pp K q M) (pp K q M) := by
  have hQ : Frobenius.pw K (K.mul q q) M = K.zero := by
    rw [pwQ]; exact pw_zero_of_le K q M _ hq (Nat.le_trans (Nat.le_add_left M M) (Nat.le_of_eq (by lib_nat)))
  have s1 := pp_split K q (2 * M)
  have s2 : pp K (K.mul q q) (2 * M) = pp K (K.mul q q) M := by rw [show 2 * M = M + M by lib_nat, pp_stable K _ M hQ M]
  have s3 : pp K q (2 * (2 * M)) = pp K q M := by rw [show 2 * (2 * M) = M + 3 * M by lib_nat, pp_stable K q M hq]
  rw [s2, s3] at s1
  rw [gauss_mod K q M hq, jf_neg, pm_even, s1]
  lib_ring

/-- **η(4z)²η(8z)² = Gauss(q⁴)·Jacobi(q⁴)·q at every coefficient** (x = q⁴, x² = q⁸). -/
theorem eta32 (q : R) (M : Nat) (hq : Frobenius.pw K q M = K.zero) :
    K.mul q (K.mul (K.mul (pp K (Frobenius.pw K q 4) M) (pp K (Frobenius.pw K q 4) M))
      (K.mul (pp K (K.mul (Frobenius.pw K q 4) (Frobenius.pw K q 4)) M) (pp K (K.mul (Frobenius.pw K q 4) (Frobenius.pw K q 4)) M))) =
    K.mul q (K.mul (es K (fun m => Frobenius.pw K (Frobenius.pw K q 4) (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1))
      (T K (Frobenius.pw K q 4) (2 * M + 2))) := by
  have hx : Frobenius.pw K (Frobenius.pw K q 4) M = K.zero := by
    rw [TwoPower.pwpow]; exact pw_zero_of_le K q M _ hq (Nat.le_trans (Nat.le_add_left M (3 * M)) (Nat.le_of_eq (by lib_nat)))
  generalize Frobenius.pw K q 4 = x at hx ⊢
  have hX : Frobenius.pw K (K.mul x x) M = K.zero := by
    rw [pwQ]; exact pw_zero_of_le K x M _ hx (Nat.le_trans (Nat.le_add_left M M) (Nat.le_of_eq (by lib_nat)))
  have gp := gauss_prod K x M hx
  rw [jacobi_eta3 K x M hx, pp_stable K (K.mul x x) M hX 1]
  generalize es K (fun m => Frobenius.pw K x (Ex (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1) = G at gp ⊢
  generalize pp K (K.mul x x) M = P at gp ⊢
  calc _ = K.mul q (K.mul (K.mul (pp K x M) (pp K x M)) (K.mul P P)) := rfl
    _ = K.mul q (K.mul (K.mul G P) (K.mul P P)) := by rw [gp]
    _ = _ := by lib_ring

end QBinom

#print axioms QBinom.pp_split
#print axioms QBinom.jf_neg
#print axioms QBinom.gauss_prod
#print axioms QBinom.eta32

#print axioms: 4 theorems, no axioms.

78.140 The eta product of 32a is the Hecke theta series of \(\mathbb Q(i)\)

In \(\mathbb Z[q]/(q^{n+1})\) the coefficient of \(q^n\) in \(q(q^4;q^4)^2(q^8;q^8)^2\) equals the real part of the Hecke coefficient, for every \(n\).

\[ [q^n]\,\eta (4z)^2\eta (8z)^2=\operatorname {Re}\sum _{\alpha \ \text {primary},\ N\alpha =n}\alpha . \]

lang/Hecke32.lean

/-
  lang/Hecke32 — T47, cycle 565: **η(4z)²η(8z)² = Σ_n Re(c_n) qⁿ at every n**: the coefficient of qⁿ in the eta product of 32a
  (computed in ℤ[q]/(q^(n+1))) equals the real part of the ℚ(i) Hecke coefficient c_n = Σ_{α primary, N(α) = n} α (HeckeMult.c).

    shn_add, shn_smul, shn_zero, shn_shn, shn_delta1, shn_delta0     shifts are linear, compose, and pick one index
    pw_neg_one, emb_int, sq_natAbs, gn_phi_nat                        bookkeeping in ℤ
    term, inner                                                       one e at a time
    eta32_hecke
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Re32
import lang.Eta32C

namespace Hecke32

open DetChain YMChain HeckeMult GaussEuclid GaussUnique

theorem shn_add : ∀ E (f g : Nat → Int) k, Trunc.shn Z E (fun i => Z.add (f i) (g i)) k = Z.add (Trunc.shn Z E f k) (Trunc.shn Z E g k)
  | 0, _, _, _ => rfl
  | _ + 1, _, _, 0 => by show Z.zero = Z.add Z.zero Z.zero; lib_ring
  | E + 1, f, g, k + 1 => shn_add E f g k

theorem shn_smul (c : Int) : ∀ E (g : Nat → Int) k, Trunc.shn Z E (fun i => Z.mul c (g i)) k = Z.mul c (Trunc.shn Z E g k)
  | 0, _, _ => rfl
  | _ + 1, _, 0 => by show Z.zero = Z.mul c Z.zero; lib_ring
  | E + 1, g, k + 1 => shn_smul c E g k

theorem shn_zero : ∀ E k, Trunc.shn Z E (fun _ => Z.zero) k = Z.zero
  | 0, _ => rfl
  | _ + 1, 0 => rfl
  | E + 1, k + 1 => shn_zero E k

theorem shn_shn (b : Nat) (f : Nat → Int) : ∀ a k, Trunc.shn Z a (Trunc.shn Z b f) k = Trunc.shn Z (b + a) f k
  | 0, _ => rfl
  | _ + 1, 0 => rfl
  | a + 1, k + 1 => shn_shn b f a k

theorem shn_delta1 (D : Nat) : Trunc.shn Z D (GLnLocal.delta Z) D = Z.one := Trunc.shn_ge Z D _ 0

theorem shn_delta0 (D n : Nat) (h : ¬ D = n) : Trunc.shn Z D (GLnLocal.delta Z) n = Z.zero := by
  cases Nat.lt_or_ge n D with
  | inl hl => exact Trunc.shn_lt Z D _ n hl
  | inr hg =>
    obtain ⟨t, ht⟩ := Cheb.le_dest2 D n hg
    rw [← ht, Nat.add_comm, Trunc.shn_ge]
    cases t with
    | zero => exact absurd (show D = 0 + D by lib_nat) (fun e => h (e.trans ht))
    | succ t => rfl

theorem pw_neg_one : ∀ k, Frobenius.pw Z (Z.neg Z.one) k = sg2 k
  | 0 => rfl
  | k + 1 => by
    show Frobenius.pw Z (Z.neg Z.one) k * (-1) = -sg2 k
    rw [pw_neg_one k]; lib_int

theorem emb_int : ∀ m, Cheb.emb Z m = Int.ofNat m
  | 0 => rfl
  | m + 1 => by show Cheb.emb Z m + 1 = Int.ofNat m + 1; rw [emb_int m]

theorem sq_natAbs (e : Int) : e * e = Int.ofNat (Int.natAbs e) * Int.ofNat (Int.natAbs e) := by
  cases e with
  | ofNat k => rfl
  | negSucc k => show (-(Int.ofNat k + 1)) * (-(Int.ofNat k + 1)) = (Int.ofNat k + 1) * (Int.ofNat k + 1); lib_int

theorem gn_phi_nat (e : Int) (j : Nat) :
    gn (phi e j) = Int.ofNat (4 * (Int.natAbs e * Int.natAbs e) + 1 + 4 * (j * j + j)) := by
  rw [phi_norm, sq_natAbs]
  show _ = 4 * (Int.ofNat (Int.natAbs e) * Int.ofNat (Int.natAbs e)) + 1 + 4 * (Int.ofNat j * Int.ofNat j + Int.ofNat j)
  lib_int

theorem term (n : Nat) (e : Int) (J : Nat) :
    Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs e)) (Z.mul (Frobenius.pw Z (Z.neg Z.one) J) (Z.mul (Cheb.emb Z (2 * J + 1))
      (Trunc.shn Z (4 * (Int.natAbs e * Int.natAbs e) + 1 + 4 * (J * J + J)) (GLnLocal.delta Z) n))) = F n (phi e J) := by
  rw [pw_neg_one, pw_neg_one, emb_int]
  by_cases hD : 4 * (Int.natAbs e * Int.natAbs e) + 1 + 4 * (J * J + J) = n
  · have hp : PD n (phi e J) := ⟨by rw [gn_phi_nat, hD], phi_prim e J⟩
    rw [F_pos n _ hp, hD, shn_delta1]
    show sg2 (Int.natAbs e) * (sg2 J * ((2 * Int.ofNat J + 1) * 1)) = sg2 (Int.natAbs e + J) * (2 * Int.ofNat J + 1)
    rw [sg2_add]; lib_int
  · have hp : ¬ PD n (phi e J) := fun h => hD (Int.ofNat.inj (by rw [← gn_phi_nat]; exact h.1))
    rw [F_neg n _ hp, shn_delta0 _ n hD]; lib_ring

theorem inner (n : Nat) (e : Int) : ∀ J,
    Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs e)) (Trunc.shn Z (4 * (Int.natAbs e * Int.natAbs e) + 1) (Trunc.tc Z J) n) =
      sumK Z (fun j => F n (phi e j)) (jl J)
  | 0 => by
    show Z.mul _ (Trunc.shn Z _ (fun _ => Z.zero) n) = Z.zero
    rw [shn_zero]; lib_ring
  | J + 1 => by
    rw [show jl (J + 1) = jl J ++ [J] from rfl, sumK_append, ← inner n e J]
    show _ = Z.add _ (Z.add (F n (phi e J)) Z.zero)
    rw [← term n e J]
    show Z.mul _ (Trunc.shn Z _ (fun i => Z.add (Trunc.tc Z J i) (Z.mul (Frobenius.pw Z (Z.neg Z.one) J)
      (Z.mul (Cheb.emb Z (2 * J + 1)) (Trunc.shn Z (4 * (J * J + J)) (GLnLocal.delta Z) i)))) n) = _
    rw [shn_add, shn_smul, shn_smul, shn_shn, show 4 * (J * J + J) + (4 * (Int.natAbs e * Int.natAbs e) + 1) =
      4 * (Int.natAbs e * Int.natAbs e) + 1 + 4 * (J * J + J) by lib_nat]
    lib_ring

/-- **The 32a eta product is the ℚ(i) Hecke theta series at every coefficient.** -/
theorem eta32_hecke (n : Nat) :
    Trunc.nth Z (n + 1) ((Trunc.TR Z (n + 1)).mul (Trunc.qe Z (n + 1)) ((Trunc.TR Z (n + 1)).mul
      ((Trunc.TR Z (n + 1)).mul (QBinom.pp (Trunc.TR Z (n + 1)) (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4) (n + 1))
        (QBinom.pp (Trunc.TR Z (n + 1)) (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4) (n + 1)))
      ((Trunc.TR Z (n + 1)).mul
        (QBinom.pp (Trunc.TR Z (n + 1)) ((Trunc.TR Z (n + 1)).mul (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4)
          (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4)) (n + 1))
        (QBinom.pp (Trunc.TR Z (n + 1)) ((Trunc.TR Z (n + 1)).mul (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4)
          (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4)) (n + 1))))) n = (c n).1 := by
  rw [Trunc.eta32_coef Z (n + 1) n (Nat.lt_succ_self n),
    re_c n (2 * (n + 1)) (2 * (n + 1) + 2) (Nat.le_trans (Nat.le_add_right n (n + 2)) (Nat.le_of_eq (by lib_nat)))
      (Nat.le_trans (Nat.le_add_right n (n + 4)) (Nat.le_of_eq (by lib_nat)))]
  exact sumK_ext Z _ _ (fun e => inner n e _) _

end Hecke32

#print axioms Hecke32.shn_add
#print axioms Hecke32.shn_smul
#print axioms Hecke32.shn_zero
#print axioms Hecke32.shn_shn
#print axioms Hecke32.shn_delta1
#print axioms Hecke32.shn_delta0
#print axioms Hecke32.pw_neg_one
#print axioms Hecke32.emb_int
#print axioms Hecke32.sq_natAbs
#print axioms Hecke32.gn_phi_nat
#print axioms Hecke32.term
#print axioms Hecke32.inner
#print axioms Hecke32.eta32_hecke

#print axioms: 13 theorems, no axioms.

78.141 32a: \(a_p\) equals the eta coefficient at every prime

With \(c_p=(2a,0)\) for the primary \(a+bi\) of norm \(p\) and \(a_p=2a\), the eta coefficient at every split prime is \(a_p\); at inert primes it is \(0\).

\[ p\equiv 1\ (4):\ [q^p]\,\eta (4z)^2\eta (8z)^2=-\sum _{x\in \mathbb F_p}\chi (x^3-x),\qquad p\equiv 3\ (4):\ [q^p]\,\eta (4z)^2\eta (8z)^2=0. \]

lang/Ap32.lean

/-
  lang/Ap32 — T47, cycle 566: **a_p(y² = x³ − x) = [q^p] η(4z)²η(8z)² at every odd prime p** (32a, all p, not only p < 200).

    etaC n          the coefficient of qⁿ in η(4z)²η(8z)² (in ℤ[q]/(q^(n+1)))
    etaC_c          etaC n = Re c_n                                   (Hecke32.eta32_hecke)
    ap32_split      p = 4k + 5 prime: etaC p = −Σ_x χ(x³ − x) = a_p   (HeckePrime.c_p, SignCM3.ap_primary)
    etaC_inert      p = 4r + 3 prime: etaC p = 0                       (HeckeInert.c_odd; a_p = 0 is CMInert.char_sum_cm)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Hecke32
import lang.SignCM3
import lang.HeckeInert

namespace Hecke32

open FTAChain

def etaC (n : Nat) : Int :=
  Trunc.nth Z (n + 1) ((Trunc.TR Z (n + 1)).mul (Trunc.qe Z (n + 1)) ((Trunc.TR Z (n + 1)).mul
    ((Trunc.TR Z (n + 1)).mul (QBinom.pp (Trunc.TR Z (n + 1)) (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4) (n + 1))
      (QBinom.pp (Trunc.TR Z (n + 1)) (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4) (n + 1)))
    ((Trunc.TR Z (n + 1)).mul
      (QBinom.pp (Trunc.TR Z (n + 1)) ((Trunc.TR Z (n + 1)).mul (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4)
        (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4)) (n + 1))
      (QBinom.pp (Trunc.TR Z (n + 1)) ((Trunc.TR Z (n + 1)).mul (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4)
        (Frobenius.pw (Trunc.TR Z (n + 1)) (Trunc.qe Z (n + 1)) 4)) (n + 1))))) n

theorem etaC_c (n : Nat) : etaC n = (HeckeMult.c n).1 := eta32_hecke n

/-- **32a at every split prime**: the eta coefficient is a_p = −Σ_x χ(x³ − x). -/
theorem ap32_split (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1) :
    etaC (2 * (2 * k + 1) + 1 + 1 + 1) =
      -YMChain.sumK LibInt.intR (fun x => QChar.chi (2 * k + 1) (CMInert.cub (ZMod.ring (2 * (2 * k + 1) + 1 + 1)) x)) (ConicSum.L (2 * k + 1)) := by
  obtain ⟨a, b, hab, hP, hS⟩ := SignCM3.ap_primary k hpr
  rw [etaC_c, HeckePrime.c_p (2 * (2 * k + 1) + 1) hpr a b hab hP]
  exact hS.symm

/-- **32a at every inert prime**: the eta coefficient is 0 (and a_p = 0 by CMInert.char_sum_cm). -/
theorem etaC_inert (r : Nat) (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * r) + 1 + 1 + 1) :
    etaC (2 * (2 * r) + 1 + 1 + 1) = 0 := by
  have h := HeckeInert.c_odd r hq 0
  rw [show (2 * (2 * r) + 1 + 1 + 1) ^ (2 * 0 + 1) = 2 * (2 * r) + 1 + 1 + 1 from Nat.one_mul _] at h
  rw [etaC_c, h]; rfl

end Hecke32

#print axioms Hecke32.etaC_c
#print axioms Hecke32.ap32_split
#print axioms Hecke32.etaC_inert

#print axioms: 3 theorems, no axioms.

78.142 32a at inert primes

At \(p\equiv 3\pmod 4\) the substitution \(x\mapsto -x\) and \(\chi (-1)=-1\) give \(\sum \chi (x^3-x)=0\) in \(\mathbb Z\), equal to the eta coefficient.

\[ p\equiv 3\ (4):\ [q^p]\,\eta (4z)^2\eta (8z)^2=-\sum _{x\in \mathbb F_p}\chi (x^3-x)=0. \]

lang/Ap32i.lean

/-
  lang/Ap32i — T47, cycle 567: 32a at inert primes in the same form as at split primes:
  p = 4k + 3 prime ⇒ Σ_x χ(x³ − x) = 0 in ℤ, hence [q^p]η(4z)²η(8z)² = −Σ_x χ(x³ − x) = a_p at every odd prime p.

    chi_sum_inert   x ↦ −x: χ((−x)³ − (−x)) = χ(−1)χ(x³ − x) = −χ(x³ − x), so S = −S
    ap32_inert      etaC p = −Σχ(x³ − x)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Ap32
import lang.BaseChange

namespace Hecke32

open FTAChain

theorem chi_sum_inert (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k) + 1 + 1 + 1) :
    YMChain.sumK LibInt.intR (fun x => QChar.chi (2 * k) (CMInert.cub (ZMod.ring (2 * (2 * k) + 1 + 1)) x)) (ConicSum.L (2 * k)) = 0 := by
  let K := ZMod.ring (2 * (2 * k) + 1 + 1)
  let F := fun x => QChar.chi (2 * k) (CMInert.cub K x)
  have hc := CMInert.complete_allZ (2 * (2 * k) + 1 + 1) LibInt.intR
  have hre := FiniteRecChain.reindex LibInt.intR (CMInert.allZ _ _) hc K.neg K.neg (fun x => DetChain.neg_neg' K x)
    (fun x => DetChain.neg_neg' K x) F
  have hm1 : QChar.chi (2 * k) (K.neg K.one) = -1 := BaseChange.chi_negone k (TwoPower.negone_odd' (2 * (2 * k) + 1) k)
  have hodd : ∀ x, F (K.neg x) = LibInt.intR.neg (F x) := fun x => by
    show QChar.chi (2 * k) (CMInert.cub K (K.neg x)) = -QChar.chi (2 * k) (CMInert.cub K x)
    rw [CMInert.cub_neg]
    have e : K.neg (CMInert.cub K x) = K.mul (K.neg K.one) (CMInert.cub K x) := by rw [DetChain.neg_mul_left, K.one_mul]
    rw [e, QChar.chi_mul (2 * k) hpr, hm1]; lib_int
  rw [YMChain.sumK_ext LibInt.intR _ _ hodd, CMInert.sum_neg_fun] at hre
  show YMChain.sumK LibInt.intR F (CMInert.allZ (2 * (2 * k) + 1 + 1) (2 * (2 * k) + 1 + 1 + 1)) = 0
  generalize YMChain.sumK LibInt.intR F (CMInert.allZ (2 * (2 * k) + 1 + 1) (2 * (2 * k) + 1 + 1 + 1)) = S at hre ⊢
  have h2 : 2 * S = 2 * 0 := by
    calc 2 * S = S + S := by lib_int
      _ = -S + S := by rw [show (-S : Int) = LibInt.intR.neg S from rfl, hre]
      _ = 2 * 0 := by lib_int
  exact two_cancel S 0 h2

/-- **32a at every inert prime**: [q^p]η(4z)²η(8z)² = −Σ_x χ(x³ − x) (both 0). -/
theorem ap32_inert (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k) + 1 + 1 + 1) :
    etaC (2 * (2 * k) + 1 + 1 + 1) =
      -YMChain.sumK LibInt.intR (fun x => QChar.chi (2 * k) (CMInert.cub (ZMod.ring (2 * (2 * k) + 1 + 1)) x)) (ConicSum.L (2 * k)) := by
  rw [etaC_inert k hpr, chi_sum_inert k hpr]; rfl

end Hecke32

#print axioms Hecke32.chi_sum_inert
#print axioms Hecke32.ap32_inert

#print axioms: 2 theorems, no axioms.

78.143 32a: multiplicativity of the eta coefficients

Conjugation permutes the primary elements of norm \(n\), so \(c_n\) is real; with \(c_{mn}=c_mc_n\) the eta coefficients are multiplicative.

\[ \gcd (m,n)=1,\ m,n\ \text {odd}:\quad [q^{mn}]\,\eta (4z)^2\eta (8z)^2=[q^m]\,\eta (4z)^2\eta (8z)^2\cdot [q^n]\,\eta (4z)^2\eta (8z)^2. \]

lang/Mult32.lean

/-
  lang/Mult32 — T47/T50, cycle 583: **the 32a eta coefficients are multiplicative at every pair of coprime odd m, n**:
    [q^(mn)]η(4z)²η(8z)² = [q^m]·[q^n]   (m, n odd, gcd(m, n) = 1),
  from c_(mn) = c_m c_n (HeckeEuler.mult2) and Im c_n = 0 (b ↦ −b permutes the primary elements of norm n).

    zl_neg, pd_conj, im_c     Σ over zl is symmetric; PD n (a, −b) ⇔ PD n (a, b); Im c_n = 0
    eta32_mult
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Ap32
import lang.HeckeEuler

namespace Hecke32

open DetChain YMChain FiniteRecChain HeckeMult GaussEuclid GaussUnique GaussInt LibGcd

theorem zl_neg (g : Int → Int) : ∀ N, sumK Z g (zl N) = sumK Z (fun b => g (-b)) (zl N)
  | 0 => by show Z.add (g 0) Z.zero = Z.add (g (-0)) Z.zero; rfl
  | N + 1 => by
    show sumK Z g (zl N ++ [Int.ofNat (N + 1), Int.negSucc N]) = sumK Z (fun b => g (-b)) (zl N ++ [Int.ofNat (N + 1), Int.negSucc N])
    rw [sumK_append, sumK_append, zl_neg g N]
    show Z.add _ (Z.add (g (Int.ofNat (N + 1))) (Z.add (g (Int.negSucc N)) Z.zero)) =
      Z.add _ (Z.add (g (-Int.ofNat (N + 1))) (Z.add (g (-Int.negSucc N)) Z.zero))
    rw [show -Int.ofNat (N + 1) = Int.negSucc N from rfl, show -Int.negSucc N = Int.ofNat (N + 1) from rfl]
    lib_ring

theorem prim_conj (a b : Int) (h : Prim (a, b)) : Prim (a, -b) := by
  obtain ⟨⟨c, hc⟩, ⟨e, he⟩⟩ := h
  have hb : b = 2 * c := hc
  refine ⟨⟨-c, by show -b = 2 * -c; rw [hb]; lib_int⟩, ⟨e - c, ?_⟩⟩
  show a + -b - 1 = 4 * (e - c)
  have he' : a + b - 1 = 4 * e := he
  calc a + -b - 1 = (a + b - 1) - 2 * b := by lib_int
    _ = _ := by rw [he', hb]; lib_int

theorem pd_conj (n : Nat) (a b : Int) (h : PD n (a, b)) : PD n (a, -b) :=
  ⟨(show a * a + -b * -b = a * a + b * b by lib_int).trans h.1, prim_conj a b h.2⟩

theorem snd_I (n : Nat) (z : Int × Int) : ((Cyclo4.cyc4 Z).mul (I n z) z).2 = if PD n z then z.2 else 0 := by
  by_cases h : PD n z
  · rw [I_pos n z h, ite_pos' _ _ h]; show Z.add (Z.mul Z.one z.2) (Z.mul Z.zero z.1) = z.2; lib_ring
  · rw [I_neg n z h, ite_neg' _ _ h]; show Z.add (Z.mul Z.zero z.2) (Z.mul Z.zero z.1) = Z.zero; lib_ring

theorem snd_sumK {A : Type} (f : A → Int × Int) : ∀ l : List A, (sumK (Cyclo4.cyc4 Z) f l).2 = sumK Z (fun a => (f a).2) l
  | [] => rfl
  | a :: l => by
    show Z.add (f a).2 (sumK (Cyclo4.cyc4 Z) f l).2 = Z.add (f a).2 (sumK Z (fun a => (f a).2) l)
    rw [snd_sumK f l]

/-- **Im c_n = 0.** -/
theorem im_c (n : Nat) : (c n).2 = 0 := by
  have h1 : (c n).2 = sumK Z (fun a => sumK Z (fun b => if PD n (a, b) then b else 0) (zl n)) (zl n) := by
    show (sumK (Cyclo4.cyc4 Z) _ (box n)).2 = _
    rw [snd_sumK]
    unfold box; rw [sumK_pairs]
    exact sumK_ext Z _ _ (fun a => sumK_ext Z _ _ (fun b => snd_I n (a, b)) _) _
  have h2 : ∀ a, sumK Z (fun b => if PD n (a, b) then b else 0) (zl n) = -sumK Z (fun b => if PD n (a, b) then b else 0) (zl n) := by
    intro a
    have hs := zl_neg (fun b => if PD n (a, b) then b else 0) n
    have hneg : ∀ b, (if PD n (a, -b) then -b else (0 : Int)) = -(if PD n (a, b) then b else 0) := fun b => by
      by_cases h : PD n (a, b)
      · rw [ite_pos' _ _ (pd_conj n a b h), ite_pos' _ _ h]
      · have h' : ¬ PD n (a, -b) := fun e => h (by have := pd_conj n a (-b) e; rw [show - -b = b by lib_int] at this; exact this)
        rw [ite_neg' _ _ h', ite_neg' _ _ h]; rfl
    rw [sumK_ext Z _ _ hneg] at hs
    have hs' : sumK Z (fun b => if PD n (a, b) then b else 0) (zl n) = sumK Z (fun b => Z.neg (if PD n (a, b) then b else 0)) (zl n) := hs
    rw [CMInert.sum_neg_fun Z (fun b => if PD n (a, b) then b else 0)] at hs'
    exact hs'
  rw [h1]
  have h3 : ∀ a, sumK Z (fun b => if PD n (a, b) then b else 0) (zl n) = 0 := fun a => by
    have := h2 a
    generalize sumK Z (fun b => if PD n (a, b) then b else 0) (zl n) = S at this
    exact two_cancel S 0 (by calc 2 * S = S + S := by lib_int
      _ = -S + S := by rw [← this]
      _ = 2 * 0 := by lib_int)
  rw [sumK_ext Z _ (fun _ => Z.zero) h3]
  exact HeckePrime.zsumR Z _

/-- **Multiplicativity of the 32a coefficients.** -/
theorem eta32_mult (m n : Nat) (hm : HeckeEuler.Odd' m) (hn : HeckeEuler.Odd' n) (hc : HeckeEuler.Cop m n) :
    etaC (m * n) = etaC m * etaC n := by
  rw [etaC_c, etaC_c, etaC_c, HeckeEuler.mult2 m n hm hn hc]
  show Z.add (Z.mul (c m).1 (c n).1) (Z.neg (Z.mul (c m).2 (c n).2)) = (c m).1 * (c n).1
  rw [im_c, im_c]; show (c m).1 * (c n).1 + -(0 * 0) = (c m).1 * (c n).1; lib_int

end Hecke32

#print axioms Hecke32.zl_neg
#print axioms Hecke32.prim_conj
#print axioms Hecke32.pd_conj
#print axioms Hecke32.snd_I
#print axioms Hecke32.snd_sumK
#print axioms Hecke32.im_c
#print axioms Hecke32.eta32_mult

#print axioms: 7 theorems, no axioms.

78.144 32a: the Hecke recursion at odd prime powers

From \(c_{p^{k+2}}=2a\,c_{p^{k+1}}-p\,c_{p^k}\) at split \(p\) and \(c_{q^{k+2}}=-q\,c_{q^k}\) at inert \(q\).

\[ [q^{p^{k+2}}]\,f=[q^p]\,f\cdot [q^{p^{k+1}}]\,f-p\,[q^{p^k}]\,f,\qquad f=\eta (4z)^2\eta (8z)^2,\ p\ \text {odd}. \]

lang/Rec32.lean

/-
  lang/Rec32 — T47/T50, cycle 584: **the Hecke recursion for 32a at every odd prime power**:
    [q^(p^(k+2))] = [q^p]·[q^(p^(k+1))] − p·[q^(p^k)]      (η(4z)²η(8z)², every odd prime p).
  Split p (a primary a + bi of norm p): HeckePrime.hecke_rec; inert p = 4r + 3: HeckeInert.c_inert_rec and [q^p] = 0.
  With Mult32 the eta product is a Hecke eigenform away from 2 at every coefficient (the n < 200 hecke-recursion row for 32a).
  Lean 4 core only, proofs by hand (lib_int, lib_ring), no axioms.
-/
import lang.Mult32
import lang.Ap32i

namespace Hecke32

open FTAChain DetChain HeckeMult GaussUnique

theorem rec32_split (P : Nat) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1))
    (hπ : Prim (a, b)) (k : Nat) :
    etaC ((P + 1 + 1) ^ (k + 2)) = etaC (P + 1 + 1) * etaC ((P + 1 + 1) ^ (k + 1)) - Int.ofNat (P + 1 + 1) * etaC ((P + 1 + 1) ^ k) := by
  rw [etaC_c, etaC_c, etaC_c, etaC_c, HeckePrime.hecke_rec P hp a b hab hπ k, HeckePrime.c_p P hp a b hab hπ]
  show Z.add (Z.add (Z.mul (2 * a) (c ((P + 1 + 1) ^ (k + 1))).1) (Z.neg (Z.mul 0 (c ((P + 1 + 1) ^ (k + 1))).2)))
      (Z.neg (Z.add (Z.mul (Int.ofNat (P + 1 + 1)) (c ((P + 1 + 1) ^ k)).1) (Z.neg (Z.mul 0 (c ((P + 1 + 1) ^ k)).2)))) = _
  show (2 * a * (c ((P + 1 + 1) ^ (k + 1))).1 + -(0 * (c ((P + 1 + 1) ^ (k + 1))).2)) +
      -(Int.ofNat (P + 1 + 1) * (c ((P + 1 + 1) ^ k)).1 + -(0 * (c ((P + 1 + 1) ^ k)).2)) =
    2 * a * (c ((P + 1 + 1) ^ (k + 1))).1 - Int.ofNat (P + 1 + 1) * (c ((P + 1 + 1) ^ k)).1
  lib_int

theorem rec32_inert (r : Nat) (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * r) + 1 + 1 + 1) (k : Nat) :
    etaC ((2 * (2 * r) + 1 + 1 + 1) ^ (k + 2)) = etaC (2 * (2 * r) + 1 + 1 + 1) * etaC ((2 * (2 * r) + 1 + 1 + 1) ^ (k + 1))
      - Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * etaC ((2 * (2 * r) + 1 + 1 + 1) ^ k) := by
  rw [etaC_inert r hq, etaC_c ((2 * (2 * r) + 1 + 1 + 1) ^ (k + 2)), etaC_c ((2 * (2 * r) + 1 + 1 + 1) ^ k), HeckeInert.c_inert_rec r hq k]
  show Z.add (Z.mul (-Int.ofNat (2 * (2 * r) + 1 + 1 + 1)) (c ((2 * (2 * r) + 1 + 1 + 1) ^ k)).1)
      (Z.neg (Z.mul 0 (c ((2 * (2 * r) + 1 + 1 + 1) ^ k)).2)) =
    0 * etaC ((2 * (2 * r) + 1 + 1 + 1) ^ (k + 1)) - Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * (c ((2 * (2 * r) + 1 + 1 + 1) ^ k)).1
  show -Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * (c ((2 * (2 * r) + 1 + 1 + 1) ^ k)).1 + -(0 * (c ((2 * (2 * r) + 1 + 1 + 1) ^ k)).2) =
    0 * etaC ((2 * (2 * r) + 1 + 1 + 1) ^ (k + 1)) - Int.ofNat (2 * (2 * r) + 1 + 1 + 1) * (c ((2 * (2 * r) + 1 + 1 + 1) ^ k)).1
  lib_int

end Hecke32

#print axioms Hecke32.rec32_split
#print axioms Hecke32.rec32_inert

#print axioms: 2 theorems, no axioms.

78.145 The Ramanujan bound for 32a and 36a at every good prime

At split primes the coefficient is \(2a\) with \(a^2+b^2=p\) resp. \(a^2+3b^2=p\); at inert primes it is \(0\).

\[ |[q^p]\,\eta (4z)^2\eta (8z)^2|^2\le 4p\ (p\ \text {odd}),\qquad |[q^p]\,\eta (6z)^4|^2\le 4p\ (p>3). \]

lang/Raman.lean

/-
  lang/Raman — T47, cycle 585: **the Ramanujan bound at every good prime** for the CM forms 32a and 36a:
    |[q^p]η(4z)²η(8z)²|² ≤ 4p (p odd),   |[q^p]η(6z)⁴|² ≤ 4p (p > 3),
  i.e. |a_p| ≤ 2√p (from a_p = 2a with a² + b² = p, resp. a² + 3b² = p; 0 at inert p). Stated in ℕ through natAbs.
  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.Rec32
import lang.Ap36i

namespace Raman

open FTAChain HeckeMult GaussUnique Hecke32

theorem natsq (a : Int) : Int.natAbs (2 * a) * Int.natAbs (2 * a) = 4 * (Int.natAbs a * Int.natAbs a) := by
  rw [natAbs_two]; lib_nat

theorem sq_le (a b : Int) (c P : Nat) (h : a * a + Int.ofNat c * (b * b) = Int.ofNat P) :
    Int.natAbs (2 * a) * Int.natAbs (2 * a) ≤ 4 * P := by
  have hN : Int.natAbs a * Int.natAbs a + c * (Int.natAbs b * Int.natAbs b) = P :=
    Int.ofNat.inj (show Int.ofNat (Int.natAbs a) * Int.ofNat (Int.natAbs a) + Int.ofNat c * (Int.ofNat (Int.natAbs b) * Int.ofNat (Int.natAbs b)) = Int.ofNat P by
      rw [← sq_natAbs, ← sq_natAbs]; exact h)
  rw [natsq, ← hN]
  exact Nat.mul_le_mul_left 4 (Nat.le_add_right _ _)

/-- **32a, split p.** -/
theorem raman32_split (k : Nat) (hpr : ∀ d, Dv d (2 * (2 * k + 1) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * k + 1) + 1 + 1 + 1) :
    Int.natAbs (etaC (2 * (2 * k + 1) + 1 + 1 + 1)) * Int.natAbs (etaC (2 * (2 * k + 1) + 1 + 1 + 1)) ≤ 4 * (2 * (2 * k + 1) + 1 + 1 + 1) := by
  obtain ⟨a, b, hab, hP, _⟩ := SignCM3.ap_primary k hpr
  rw [etaC_c, HeckePrime.c_p (2 * (2 * k + 1) + 1) hpr a b hab hP]
  exact sq_le a b 1 _ ((show a * a + Int.ofNat 1 * (b * b) = a * a + b * b by show a * a + 1 * (b * b) = _; lib_int).trans hab)

/-- **32a, inert p.** -/
theorem raman32_inert (r : Nat) (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (2 * r) + 1 + 1 + 1) :
    Int.natAbs (etaC (2 * (2 * r) + 1 + 1 + 1)) * Int.natAbs (etaC (2 * (2 * r) + 1 + 1 + 1)) ≤ 4 * (2 * (2 * r) + 1 + 1 + 1) := by
  rw [etaC_inert r hq]; exact Nat.zero_le _

/-- **36a, split p.** -/
theorem raman36_split (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (N : Nat) (hN : 2 * m + 1 + 1 = 3 * N) :
    Int.natAbs (Hecke36.etaC36 (2 * m + 1 + 1 + 1)) * Int.natAbs (Hecke36.etaC36 (2 * m + 1 + 1 + 1)) ≤ 4 * (2 * m + 1 + 1 + 1) := by
  obtain ⟨a, b, hab, _, hS⟩ := CubicSign.cubic_exact m hpr N hN
  rw [Hecke36.ap36_split m hpr N hN, hS]
  exact sq_le a b 3 _ hab

/-- **36a, inert p.** -/
theorem raman36_inert (m c : Nat) (hc : 2 * m + 1 + 1 + 1 + 1 = 3 * (c + 1)) :
    Int.natAbs (Hecke36.etaC36 (2 * m + 1 + 1 + 1)) * Int.natAbs (Hecke36.etaC36 (2 * m + 1 + 1 + 1)) ≤ 4 * (2 * m + 1 + 1 + 1) := by
  rw [Hecke36.etaC36_inert m c hc]; exact Nat.zero_le _

end Raman

#print axioms Raman.natsq
#print axioms Raman.sq_le
#print axioms Raman.raman32_split
#print axioms Raman.raman32_inert
#print axioms Raman.raman36_split
#print axioms Raman.raman36_inert

#print axioms: 6 theorems, no axioms.

78.146 32a: the even coefficients vanish

A primary \(a+bi\) has \(a\) odd and \(b\) even, so its norm is odd and \(c_{2n}=0\).

\[ [q^{2n}]\,\eta (4z)^2\eta (8z)^2=0. \]

lang/Even32.lean

/-
  lang/Even32 — T47, cycle 586: **the even coefficients of η(4z)²η(8z)² vanish**: [q^(2n)] = 0 for every n
  (a primary a + bi has a odd, b even, so its norm is odd; hence c_(2n) = 0). With Mult32/Rec32 the eigenvalue of T_2 is 0.
  Lean 4 core only, proofs by hand (lib_int, lib_ring), no axioms.
-/
import lang.Mult32

namespace Hecke32

open DetChain YMChain FiniteRecChain HeckeMult GaussEuclid GaussUnique GaussInt

theorem not_pd_even (n : Nat) (z : Int × Int) : ¬ PD (2 * n) z := fun h => by
  obtain ⟨w, c', ha, hb⟩ := HeckePrime.odd_a z.1 z.2 h.2
  have hn : z.1 * z.1 + z.2 * z.2 = Int.ofNat (2 * n) := h.1
  rw [ha, hb] at hn
  exact int_odd_ne_even (2 * w * w + 2 * w + 2 * c' * c') (Int.ofNat n) (by
    calc 2 * (2 * w * w + 2 * w + 2 * c' * c') + 1 = (2 * w + 1) * (2 * w + 1) + 2 * c' * (2 * c') := by lib_int
      _ = 2 * Int.ofNat n := by rw [hn]; rfl)

theorem c_even (n : Nat) : c (2 * n) = (Cyclo4.cyc4 Z).zero := by
  show sumK (Cyclo4.cyc4 Z) (fun z => (Cyclo4.cyc4 Z).mul (I (2 * n) z) z) (box (2 * n)) = _
  rw [sumK_ext (Cyclo4.cyc4 Z) _ (fun _ => (Cyclo4.cyc4 Z).zero) (fun z => by
    rw [I_neg _ z (not_pd_even n z)]; exact zero_mul' _ z)]
  exact HeckePrime.zsumR _ _

theorem etaC_even (n : Nat) : etaC (2 * n) = 0 := by
  rw [etaC_c, c_even]; rfl

end Hecke32

#print axioms Hecke32.not_pd_even
#print axioms Hecke32.c_even
#print axioms Hecke32.etaC_even

#print axioms: 3 theorems, no axioms.

78.147 36a: the eta product is the theta series at every odd coefficient

The pairs \((x,y)\), \(x\equiv 1\pmod 3\), of odd norm \(n\) correspond to the terms \((6j+1,2i+1)=(x-3y,\pm (x+y))\) of \(\eta (6z)^4\), with weight \(x+y\); \(\sum y=0\).

\[ n\ \text {odd}:\quad [q^n]\,\eta (6z)^4=\sum _{x\equiv 1\,(3),\ x^2+3y^2=n}x. \]

lang/All36.lean

/-
  lang/All36 — T47, cycle 589: **η(6z)⁴ = Σ_{x ≡ 1 (3)} x q^(x² + 3y²) at every odd coefficient**:
    etaC36 n = c3 n   for every odd n
  (the n < 200 evaluation row theta-eta for 36a, now at every odd n).

    eta36_sum      etaC36 (n+1) = Σ_{j ∈ zl(2M)} Σ_{i < 2M+2} T(j, i)       (Coef36, GzGen; M = 2(n+1) + 1)
    inner_R        Σ_{(x,y) ∈ box} [R x y j i]·H3 = T(j, i)                  (the inverse x = (u ± 3v)/4, y = (±v − u)/4)
    eta36_odd
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Bij36

namespace Theta36

open DetChain YMChain FiniteRecChain HeckeMult GaussInt LibGcd Hecke32 Hecke36

def T36 (n : Nat) (j : Int) (i : Nat) : Int :=
  Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs j)) (Z.mul (Frobenius.pw Z (Z.neg Z.one) i) (Z.mul (Cheb.emb Z (2 * i + 1))
    (delta Z (6 * QBinom.Gj g36 j + 3 * (i * i + i)) n)))

theorem eta36_sum (n : Nat) : etaC36 (n + 1) =
    sumK Z (fun j => sumK Z (fun i => T36 n j i) (jl (2 * (2 * (n + 1) + 1) + 2))) (zl (2 * (2 * (n + 1) + 1))) := by
  have hn : n + 1 < 2 * (n + 1) + 1 := Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_trans (Nat.le_add_right _ (n + 1)) (Nat.le_of_eq (by lib_nat)))
  let F : Nat → Int := fun e => sumK Z (fun i => Z.mul (Frobenius.pw Z (Z.neg Z.one) i) (Z.mul (Cheb.emb Z (2 * i + 1))
    (delta Z (6 * e + 3 * (i * i + i)) n))) (jl (2 * (2 * (n + 1) + 1) + 2))
  calc etaC36 (n + 1)
      = sumK Z (fun m' => Z.mul (F (g36 (2 * (2 * (n + 1) + 1)) m')) (Frobenius.pw Z (Z.neg Z.one) m'))
          (jl (2 * (2 * (2 * (n + 1) + 1)) + 1)) := coef36 _ n hn
    _ = QBinom.es Z (fun m' => F (g36 (2 * (2 * (n + 1) + 1)) m')) (Z.neg Z.one) (2 * (2 * (2 * (n + 1) + 1)) + 1) := (es_jl _ _).symm
    _ = Z.mul (Frobenius.pw Z (Z.neg Z.one) (2 * (2 * (n + 1) + 1))) (QBinom.Bg Z F g36 (2 * (2 * (n + 1) + 1))) := QBinom.gzg Z F g36 g36_shift _
    _ = QBinom.Bg Z F g36 (2 * (2 * (n + 1) + 1)) := by rw [QBinom.pm_even]; lib_ring
    _ = _ := by
      apply sumK_ext; intro j
      show Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs j)) (F (QBinom.Gj g36 j)) = _
      rw [mul_sumK]; rfl

theorem cancel4 (a b : Int) (h : 4 * a = 4 * b) : a = b := HeckeInert.int_cancel' a b 3 (by show a * 4 = b * 4; calc a * 4 = 4 * a := by lib_int
  _ = 4 * b := h
  _ = b * 4 := by lib_int)

theorem inner_R (s : Nat) (N : Nat) (hN : 2 * s + 1 ≤ N) (j : Int) (i : Nat) :
    sumK Z (fun x => sumK Z (fun y => Z.mul (rI x y j i) (H3 (2 * s + 1) (x, y))) (zl N)) (zl N) = T36 (2 * s) j i := by
  unfold T36
  rw [pw_neg_one, pw_neg_one, emb_int, show Int.ofNat (2 * i + 1) = 2 * Int.ofNat i + 1 from rfl]
  by_cases hcnd : 6 * QBinom.Gj g36 j + 3 * (i * i + i) = 2 * s
  · have hd : delta Z (6 * QBinom.Gj g36 j + 3 * (i * i + i)) (2 * s) = Z.one := by unfold delta; rw [ite_pos' _ _ hcnd]
    rw [hd]
    have hI := (cond_iff j i (2 * s)).mp hcnd
    -- the unique (x0, y0)
    have key : ∃ x0 y0 : Int, (∀ x y, R x y j i ↔ (x = x0 ∧ y = y0)) ∧ x0 * x0 + 3 * (y0 * y0) = Int.ofNat (2 * s + 1) ∧
        IDv 3 (x0 - 1) ∧ sg2 (Int.natAbs j) * (sg2 i * (2 * Int.ofNat i + 1)) = x0 + y0 := by
      cases int_even_odd (3 * j - Int.ofNat i) with
      | inl he =>
        obtain ⟨e, he⟩ := he
        have hu : 6 * j + 1 = 4 * e + (2 * Int.ofNat i + 1) := by calc 6 * j + 1 = 2 * (3 * j - Int.ofNat i) + (2 * Int.ofNat i + 1) := by lib_int
          _ = _ := by rw [he]; lib_int
        refine ⟨e + (2 * Int.ofNat i + 1), -e, fun x y => ⟨fun hr => ?_, fun hxy => ?_⟩, ?_, ⟨2 * j - e, by rw [show e + (2 * Int.ofNat i + 1) = 6 * j + 1 - 3 * e by rw [hu]; lib_int]; lib_int⟩, ?_⟩
        · obtain ⟨h1, h2⟩ := hr
          rcases h2 with h2 | h2
          · have hy : y = -e := cancel4 y (-e) (by calc 4 * y = (x + y) - (x - 3 * y) := by lib_int
              _ = (2 * Int.ofNat i + 1) - (6 * j + 1) := by rw [h1, h2]
              _ = _ := by rw [hu]; lib_int)
            exact ⟨by calc x = (x + y) - y := by lib_int
              _ = _ := by rw [← h2, hy]; lib_int, hy⟩
          · exfalso
            have hX : 2 * Int.ofNat i + 1 = -(4 * e + (2 * Int.ofNat i + 1)) - 4 * y := by
              calc 2 * Int.ofNat i + 1 = -(x + y) := h2
                _ = -(x - 3 * y) - 4 * y := by lib_int
                _ = _ := by rw [← h1, hu]
            exact int_odd_ne_even (Int.ofNat i) (-e - y) (two_cancel _ _ (by
              calc 2 * (2 * Int.ofNat i + 1) = (2 * Int.ofNat i + 1) + (2 * Int.ofNat i + 1) := by lib_int
                _ = (2 * Int.ofNat i + 1) + (-(4 * e + (2 * Int.ofNat i + 1)) - 4 * y) := by rw [← hX]
                _ = _ := by lib_int))
        · obtain ⟨hx, hy⟩ := hxy
          rw [hx, hy]
          exact ⟨by rw [hu]; lib_int, Or.inl (by lib_int)⟩
        · have e1 := rot (e + (2 * Int.ofNat i + 1)) (-e)
          rw [show e + (2 * Int.ofNat i + 1) - 3 * -e = 6 * j + 1 by rw [hu]; lib_int,
            show e + (2 * Int.ofNat i + 1) + -e = 2 * Int.ofNat i + 1 by lib_int, hI] at e1
          exact cancel4 _ _ (e1.symm.trans rfl)
        · exact weight j i _ (Int.ofNat i) (by lib_int) ⟨e, by rw [hu]; lib_int⟩ (Or.inl (by lib_int))
      | inr he =>
        obtain ⟨e, he⟩ := he
        have hu : 6 * j + 1 = 4 * (e + Int.ofNat i + 1) - (2 * Int.ofNat i + 1) := by
          calc 6 * j + 1 = 2 * (3 * j - Int.ofNat i) + 2 * Int.ofNat i + 1 := by lib_int
            _ = _ := by rw [he]; lib_int
        generalize e + Int.ofNat i + 1 = f at hu
        refine ⟨f - (2 * Int.ofNat i + 1), -f, fun x y => ⟨fun hr => ?_, fun hxy => ?_⟩, ?_, ⟨2 * j - f, by rw [show f - (2 * Int.ofNat i + 1) = 6 * j + 1 - 3 * f by rw [hu]; lib_int]; lib_int⟩, ?_⟩
        · obtain ⟨h1, h2⟩ := hr
          rcases h2 with h2 | h2
          · exfalso
            have hX : 2 * Int.ofNat i + 1 = (4 * f - (2 * Int.ofNat i + 1)) + 4 * y := by
              calc 2 * Int.ofNat i + 1 = x + y := h2
                _ = (x - 3 * y) + 4 * y := by lib_int
                _ = _ := by rw [← h1, hu]
            exact int_odd_ne_even (Int.ofNat i) (f + y) (two_cancel _ _ (by
              calc 2 * (2 * Int.ofNat i + 1) = (2 * Int.ofNat i + 1) + (2 * Int.ofNat i + 1) := by lib_int
                _ = (2 * Int.ofNat i + 1) + ((4 * f - (2 * Int.ofNat i + 1)) + 4 * y) := by rw [← hX]
                _ = _ := by lib_int))
          · have hy : y = -f := cancel4 y (-f) (by calc 4 * y = (x + y) - (x - 3 * y) := by lib_int
              _ = -(2 * Int.ofNat i + 1) - (6 * j + 1) := by rw [h1, show x + y = -(2 * Int.ofNat i + 1) by rw [h2]; lib_int]
              _ = _ := by rw [hu]; lib_int)
            exact ⟨by calc x = (x + y) - y := by lib_int
              _ = _ := by rw [show x + y = -(2 * Int.ofNat i + 1) by rw [h2]; lib_int, hy]; lib_int, hy⟩
        · obtain ⟨hx, hy⟩ := hxy
          rw [hx, hy]
          exact ⟨by rw [hu]; lib_int, Or.inr (by lib_int)⟩
        · have e1 := rot (f - (2 * Int.ofNat i + 1)) (-f)
          rw [show f - (2 * Int.ofNat i + 1) - 3 * -f = 6 * j + 1 by rw [hu]; lib_int,
            show (f - (2 * Int.ofNat i + 1) + -f) * (f - (2 * Int.ofNat i + 1) + -f) = (2 * Int.ofNat i + 1) * (2 * Int.ofNat i + 1) by lib_int, hI] at e1
          exact cancel4 _ _ (e1.symm.trans rfl)
        · exact weight j i _ (-Int.ofNat i - 1) (by lib_int) ⟨f, by rw [hu]; lib_int⟩ (Or.inr (by lib_int))
    obtain ⟨x0, y0, hR, hn0, h30, hw⟩ := key
    have hP : P3 (2 * s + 1) (x0, y0) := ⟨hn0, h30⟩
    have hb := abs_bound x0 y0 _ hn0
    have hpt : ∀ x y, Z.mul (rI x y j i) (H3 (2 * s + 1) (x, y)) = Z.mul (delta Z x x0) (Z.mul (delta Z y y0) (H3 (2 * s + 1) (x, y))) := fun x y => by
      unfold rI
      by_cases hx : x = x0
      · by_cases hy : y = y0
        · rw [ite_pos' _ _ ((hR x y).mpr ⟨hx, hy⟩)]; unfold delta; rw [ite_pos' _ _ hx, ite_pos' _ _ hy]; lib_ring
        · rw [ite_neg' _ _ (fun hh => hy ((hR x y).mp hh).2)]; unfold delta; rw [ite_neg' _ _ hy]; lib_ring
      · rw [ite_neg' _ _ (fun hh => hx ((hR x y).mp hh).1)]; unfold delta; rw [ite_neg' _ _ hx]; lib_ring
    rw [sumK_ext Z _ _ (fun x => sumK_ext Z _ _ (fun y => hpt x y) _)]
    rw [sumK_ext Z _ (fun x => Z.mul (delta Z x x0) (H3 (2 * s + 1) (x, y0))) (fun x => by
      rw [← mul_sumK, delta_sum, count_zl Z y0 N (Nat.le_trans hb.2 hN)]; lib_ring)]
    rw [delta_sum, count_zl Z x0 N (Nat.le_trans hb.1 hN)]
    unfold H3; rw [ite_pos' _ _ hP]
    show _ = sg2 (Int.natAbs j) * (sg2 i * ((2 * Int.ofNat i + 1) * 1))
    rw [show (2 * Int.ofNat i + 1) * 1 = 2 * Int.ofNat i + 1 by lib_int, hw]; show 1 * (x0 + y0) = x0 + y0; lib_int
  · have hd : delta Z (6 * QBinom.Gj g36 j + 3 * (i * i + i)) (2 * s) = Z.zero := by unfold delta; rw [ite_neg' _ _ hcnd]
    rw [hd]
    have hz : ∀ x y, Z.mul (rI x y j i) (H3 (2 * s + 1) (x, y)) = Z.zero := fun x y => by
      unfold rI
      by_cases hr : R x y j i
      · rw [ite_pos' _ _ hr]
        have hnP : ¬ P3 (2 * s + 1) (x, y) := fun hP => hcnd ((cond_iff j i (2 * s)).mpr (by
          obtain ⟨h1, h2⟩ := hr
          have e1 := rot x y
          have hP1 : x * x + 3 * (y * y) = Int.ofNat (2 * s + 1) := hP.1
          rw [hP1, ← h1] at e1
          rw [show (2 * Int.ofNat i + 1) * (2 * Int.ofNat i + 1) = (x + y) * (x + y) by rcases h2 with h2 | h2 <;> rw [h2] <;> lib_int]
          exact e1.trans rfl))
        unfold H3; rw [ite_neg' _ _ hnP]; exact DetChain.mul_zero Z _
      · rw [ite_neg' _ _ hr]; lib_ring
    rw [sumK_ext Z _ (fun _ => Z.zero) (fun x => by rw [sumK_ext Z _ (fun _ => Z.zero) (fun y => hz x y)]; exact HeckePrime.zsumR Z _),
      HeckePrime.zsumR Z]
    lib_ring

/-- **36a: η(6z)⁴ = Σ_{x≡1 (3)} x q^(x²+3y²) at every odd n.** -/
theorem eta36_odd (s : Nat) : etaC36 (2 * s + 1) = c3 (2 * s + 1) := by
  have hN : 4 * (2 * s + 1) ≤ 2 * (2 * (2 * s + 1) + 1) := Nat.le_trans (Nat.le_add_right _ 2) (Nat.le_of_eq (by lib_nat))
  have hJ : 4 * (2 * s + 1) < 2 * (2 * (2 * s + 1) + 1) + 2 := Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_trans (Nat.le_add_right _ 3) (Nat.le_of_eq (by lib_nat)))
  rw [eta36_sum, c3_xy]
  -- c3 = Σ_x Σ_y H·(Σ_j Σ_i rI)
  have hpt : ∀ x y, H3 (2 * s + 1) (x, y) = sumK Z (fun j => sumK Z (fun i => Z.mul (rI x y j i) (H3 (2 * s + 1) (x, y)))
      (jl (2 * (2 * (2 * s + 1) + 1) + 2))) (zl (2 * (2 * (2 * s + 1) + 1))) := fun x y => by
    rw [sumK_ext Z _ (fun j => Z.mul (sumK Z (fun i => rI x y j i) (jl (2 * (2 * (2 * s + 1) + 1) + 2))) (H3 (2 * s + 1) (x, y)))
      (fun j => (sumK_mul Z _ _ _).symm), ← sumK_mul]
    by_cases hP : P3 (2 * s + 1) (x, y)
    · rw [count_R s _ _ hN hJ x y hP]; exact (Z.one_mul _).symm
    · unfold H3; rw [ite_neg' _ _ hP]; exact (DetChain.mul_zero Z _).symm
  rw [sumK_ext Z _ _ (fun x => sumK_ext Z _ _ (fun y => hpt x y) _)]
  let G : Int → Int → Int → Nat → Int := fun x y j i => Z.mul (rI x y j i) (H3 (2 * s + 1) (x, y))
  let Nj := 2 * (2 * (2 * s + 1) + 1)
  let Ji := 2 * (2 * (2 * s + 1) + 1) + 2
  let Nx := 2 * s + 1
  have s1 : sumK Z (fun x => sumK Z (fun y => sumK Z (fun j => sumK Z (fun i => G x y j i) (jl Ji)) (zl Nj)) (zl Nx)) (zl Nx) =
      sumK Z (fun x => sumK Z (fun j => sumK Z (fun y => sumK Z (fun i => G x y j i) (jl Ji)) (zl Nx)) (zl Nj)) (zl Nx) :=
    sumK_ext Z _ _ (fun x => sumK_swap Z (fun y j => sumK Z (fun i => G x y j i) (jl Ji)) (zl Nj) (zl Nx)) _
  have s2 : sumK Z (fun x => sumK Z (fun j => sumK Z (fun y => sumK Z (fun i => G x y j i) (jl Ji)) (zl Nx)) (zl Nj)) (zl Nx) =
      sumK Z (fun j => sumK Z (fun x => sumK Z (fun y => sumK Z (fun i => G x y j i) (jl Ji)) (zl Nx)) (zl Nx)) (zl Nj) :=
    sumK_swap Z (fun x j => sumK Z (fun y => sumK Z (fun i => G x y j i) (jl Ji)) (zl Nx)) (zl Nj) (zl Nx)
  have s3 : ∀ j, sumK Z (fun x => sumK Z (fun y => sumK Z (fun i => G x y j i) (jl Ji)) (zl Nx)) (zl Nx) =
      sumK Z (fun i => sumK Z (fun x => sumK Z (fun y => G x y j i) (zl Nx)) (zl Nx)) (jl Ji) := fun j => by
    rw [sumK_ext Z _ _ (fun x => sumK_swap Z (fun y i => G x y j i) (jl Ji) (zl Nx))]
    exact sumK_swap Z (fun x i => sumK Z (fun y => G x y j i) (zl Nx)) (jl Ji) (zl Nx)
  rw [s1, s2, sumK_ext Z _ _ s3]
  apply sumK_ext; intro j
  apply sumK_ext; intro i
  exact (inner_R s (2 * s + 1) (Nat.le_refl _) j i).symm

end Theta36

#print axioms Theta36.eta36_sum
#print axioms Theta36.cancel4
#print axioms Theta36.inner_R
#print axioms Theta36.eta36_odd

#print axioms: 4 theorems, no axioms.

78.148 36a: the eta product is the theta series at every coefficient

At even \(n\) the eta side has no terms, and the rotation by a cube root of unity permutes the pairs with \(x\equiv 1\pmod 3\) while \(x+x'+x''=0\).

\[ [q^n]\,\eta (6z)^4=\sum _{x\equiv 1\,(3),\ x^2+3y^2=n}x\qquad (n\ge 0). \]

lang/Eta36All.lean

/-
  lang/Eta36All — T47, cycle 593: **36a: η(6z)⁴ = Σ_{x ≡ 1 (3)} x q^(x² + 3y²) at every coefficient**:
    etaC36 n = c3 n   for every n
  (odd n: All36; even n ≥ 2: both 0, Even36 and Orb36; n = 0: both 0). The n < 200 evaluation row theta-eta for 36a, now at every n.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Orb36

namespace Theta36

open DetChain YMChain FiniteRecChain HeckeMult Hecke32 Hecke36

theorem nat_parity : ∀ n : Nat, (∃ s, n = 2 * s + 1) ∨ (∃ t, n = 2 * t)
  | 0 => Or.inr ⟨0, rfl⟩
  | n + 1 => by
    rcases nat_parity n with ⟨s, hs⟩ | ⟨t, ht⟩
    · exact Or.inr ⟨s + 1, by rw [hs]; lib_nat⟩
    · exact Or.inl ⟨t, by rw [ht]⟩

theorem etaC36_zero : etaC36 0 = 0 := by
  show Trunc.nth Z (2 * 0 + 1) ((Trunc.TR Z (2 * 0 + 1)).mul (Trunc.qe Z (2 * 0 + 1)) _) 0 = 0
  rw [Trunc.nth_qmul Z (2 * 0 + 1) _ 0 (Nat.zero_lt_succ 0)]; rfl

theorem c3_zero : c3 0 = 0 := by
  show Z.add (Z.add (F3 0 (0, 0)) Z.zero) Z.zero = 0
  unfold F3
  by_cases h : P3 0 (0, 0)
  · rw [ite_pos' _ _ h]; rfl
  · rw [ite_neg' _ _ h]; rfl

/-- **η(6z)⁴ = Σ_{x≡1 (3)} x q^(x²+3y²) at every n.** -/
theorem eta36_all (n : Nat) : etaC36 n = c3 n := by
  rcases nat_parity n with ⟨s, hs⟩ | ⟨t, ht⟩
  · rw [hs]; exact eta36_odd s
  · cases t with
    | zero => rw [ht]; exact etaC36_zero.trans c3_zero.symm
    | succ t =>
      rw [ht, show 2 * (t + 1) = 2 * t + 1 + 1 by lib_nat, etaC36_even, show 2 * t + 1 + 1 = 2 * (t + 1) by lib_nat, c3_even]

end Theta36

#print axioms Theta36.nat_parity
#print axioms Theta36.etaC36_zero
#print axioms Theta36.c3_zero
#print axioms Theta36.eta36_all

#print axioms: 4 theorems, no axioms.

78.149 Theta series of the Hecke characters of \(\mathbb Q(i)\) of every \(\infty \)-type

For every multiplicative \(w\) on \(\mathbb Z[i]\) the coefficients \(c_w(n)=\sum _{\alpha \ \text {primary},\ N\alpha =n}w(\alpha )\) are multiplicative.

\[ \gcd (m,n)=1,\ m,n\ \text {odd}:\quad c_w(mn)=c_w(m)\,c_w(n),\qquad w(\alpha )=\alpha ^k. \]

lang/HeckeMultW.lean

/-
  lang/HeckeMultW — T50, cycle 594: **the theta series of every multiplicative weight on primary Gaussian integers is multiplicative**:
  for w : ℤ[i] → ℤ[i] with w(αβ) = w(α)w(β) (e.g. w(α) = α^k, the Hecke characters of ℚ(i) of ∞-type k),
    c_w(n) = Σ_{α primary, N(α) = n} w(α)   satisfies   c_w(mn) = c_w(m) c_w(n)  for coprime odd m, n.
  (HeckeMult.hecke_mult is w = id; the proof is the same bijection (β, γ) ↦ βγ of primaries, GaussUnique.prim_split/uniq_split.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult

namespace HeckeMult

open FTAChain DetChain LibInt GaussEuclid GaussCoprime GaussInt GaussFactor GaussUnique LibGcd YMChain FiniteRecChain

def cw (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Cyclo4.cyc4 LibInt.intR) (fun z => (Cyclo4.cyc4 LibInt.intR).mul (I n z) (w z)) (box n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime odd m, n and multiplicative w. -/
theorem hecke_mult_w (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t km kn : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : Int.ofNat (m + 1) = 2 * km + 1) (hn : Int.ofNat (n + 1) = 2 * kn + 1) :
    cw w ((m + 1) * (n + 1)) = (Cyclo4.cyc4 LibInt.intR).mul (cw w (m + 1)) (cw w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD (m + 1) α → PD (n + 1) β → PD ((m + 1) * (n + 1)) ((Cyclo4.cyc4 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [GaussEuclid.norm_mul, h1, h2]; rfl, prim_mulP _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Cyclo4.cyc4 LibInt.intR).mul (cw w (m + 1)) (cw w (n + 1))
      = sumK (Cyclo4.cyc4 LibInt.intR) (fun α => sumK (Cyclo4.cyc4 LibInt.intR) (fun β => sumK (Cyclo4.cyc4 LibInt.intR) (fun γ => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (w γ)) (box ((m + 1) * (n + 1)))) (box (n + 1))) (box (m + 1)) := by
    unfold cw; rw [sumK_mul]
    refine sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD (m + 1) α
    · by_cases hβ : PD (n + 1) β
      · rw [I_pos _ _ hα, I_pos _ _ hβ]
        have hb := abs_le _ _ (hprod α β hα hβ).1
        have e := pick_box (Cyclo4.cyc4 LibInt.intR) (fun γ => w γ) ((Cyclo4.cyc4 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Cyclo4.cyc4 LibInt.intR) (fun γ => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).one (Cyclo4.cyc4 LibInt.intR).one) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (w γ)) (box ((m + 1) * (n + 1)))
            = sumK (Cyclo4.cyc4 LibInt.intR) (fun γ => (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β)) (w γ)) (box ((m + 1) * (n + 1))) from sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I_neg _ _ hβ]
        rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (Cyclo4.cyc4 LibInt.intR).zero) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (w γ) = (Cyclo4.cyc4 LibInt.intR).zero by lib_ring)]
        rw [zsum]; lib_ring
    · rw [I_neg _ _ hα]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => show (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (w γ) = (Cyclo4.cyc4 LibInt.intR).zero by lib_ring)]
      rw [zsum]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => sumK_swap (Cyclo4.cyc4 LibInt.intR) _ _ _)]
  rw [sumK_swap (Cyclo4.cyc4 LibInt.intR)]
  unfold cw
  refine sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Cyclo4.cyc4 LibInt.intR) (fun α => sumK (Cyclo4.cyc4 LibInt.intR) (fun β => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (w γ)) (box (n + 1))) (box (m + 1))
      = (Cyclo4.cyc4 LibInt.intR).mul (sumK (Cyclo4.cyc4 LibInt.intR) (fun α => sumK (Cyclo4.cyc4 LibInt.intR) (fun β => (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β))) (box (n + 1))) (box (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD ((m + 1) * (n + 1)) γ
  · rw [I_pos _ _ hγ]
    match prim_split γ m n s t km kn hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β)) = (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (delta (Cyclo4.cyc4 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD (m + 1) α ∧ PD (n + 1) β ∧ γ = (Cyclo4.cyc4 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I_pos _ _ hα, I_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (delta (Cyclo4.cyc4 LibInt.intR) β β0) = (Cyclo4.cyc4 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD (m + 1) α
            · by_cases hβ : PD (n + 1) β
              · have hg : ¬ γ = (Cyclo4.cyc4 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I_neg _ _ hβ]; lib_ring
            · rw [I_neg _ _ hα]; lib_ring
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le α0 _ nα0
      have bb := abs_le β0 _ nβ0
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, count_box (Cyclo4.cyc4 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => show (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).mul (delta (Cyclo4.cyc4 LibInt.intR) α α0) (Cyclo4.cyc4 LibInt.intR).one from rfl), ← sumK_mul,
        count_box (Cyclo4.cyc4 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I_neg _ _ hγ]
    have pt : ∀ α β, (Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (I (m + 1) α) (I (n + 1) β)) (delta (Cyclo4.cyc4 LibInt.intR) γ ((Cyclo4.cyc4 LibInt.intR).mul α β)) = (Cyclo4.cyc4 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD (m + 1) α
      · by_cases hβ : PD (n + 1) β
        · have hg : ¬ γ = (Cyclo4.cyc4 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I_neg _ _ hβ]; lib_ring
      · rw [I_neg _ _ hα]; lib_ring
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun α => zsum _), zsum]

/-- **The Hecke characters α ↦ α^k (∞-type k)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult_pow (k : Nat) (m n : Nat) (s t km kn : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : Int.ofNat (m + 1) = 2 * km + 1) (hn : Int.ofNat (n + 1) = 2 * kn + 1) :
    cw (fun z => Frobenius.pw (Cyclo4.cyc4 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Cyclo4.cyc4 LibInt.intR).mul (cw (fun z => Frobenius.pw (Cyclo4.cyc4 LibInt.intR) z k) (m + 1)) (cw (fun z => Frobenius.pw (Cyclo4.cyc4 LibInt.intR) z k) (n + 1)) :=
  hecke_mult_w _ (fun α β => Frobenius.pw_mul _ α β k) m n s t km kn hst hm hn

end HeckeMult

#print axioms HeckeMult.hecke_mult_w
#print axioms HeckeMult.hecke_mult_pow

#print axioms: 2 theorems, no axioms.

78.150 Hecke characters of \(\mathbb Q(i)\): prime powers and the Hecke recursion

At a split prime \(p=\pi \bar \pi \) the primary elements of norm \(p^k\) are the \(\pi ^j\bar \pi ^l\), so for multiplicative \(W\) the coefficients satisfy the Hecke recursion.

\[ c_W(p^{k+2})=(W\pi +W\bar \pi )\,c_W(p^{k+1})-W\pi \,W\bar \pi \,c_W(p^k). \]

lang/HeckePrimeW.lean

/-
  lang/HeckePrimeW — T50, cycle 595: **Hecke characters of ℚ(i): the coefficients at split prime powers and the Hecke recursion**,
  for every multiplicative weight W on ℤ[i] (W(αβ) = W(α)W(β), W(1) = 1; e.g. W(α) = α^k):
    cw_pk     c_W(p^k) = Σ_{(j,l) ∈ Wl k} W(π^j π̄^l)
    cw_pk'    c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l
    cw_rec    c_W(p^(k+2)) = (W(π) + W(π̄)) c_W(p^(k+1)) − W(π)W(π̄) c_W(p^k)
  (for W(α) = α^k: eigenvalue π^k + π̄^k and W(π)W(π̄) = p^k — the weight k + 1 Hecke recursion).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.HeckeMultW
import lang.HeckePrime

namespace HeckePrime

open FTAChain DetChain LibInt GaussEuclid GaussCoprime GaussInt GaussFactor GaussUnique HeckeMult YMChain FiniteRecChain Frobenius LibGcd

def Tg {R : Type} (K : CRing R) (A B : R) (k : Nat) : R := sumK K (fun x => K.mul (pw K A x.1) (pw K B x.2)) (Wl k)

theorem Tg_succ {R : Type} (K : CRing R) (A B : R) (k : Nat) : Tg K A B (k + 1) = K.add (K.mul A (Tg K A B k)) (pw K B (k + 1)) := by
  show sumK K (fun x => K.mul (pw K A x.1) (pw K B x.2)) ((Wl k).map (fun x => (x.1 + 1, x.2)) ++ [(0, k + 1)]) = _
  rw [sumK_append, sumK_map]
  unfold Tg; rw [mul_sumK]
  have e : sumK K (fun x => K.mul (pw K A (((x.1 + 1, x.2) : Nat × Nat)).1) (pw K B (((x.1 + 1, x.2) : Nat × Nat)).2)) (Wl k) =
      sumK K (fun x => K.mul A (K.mul (pw K A x.1) (pw K B x.2))) (Wl k) :=
    sumK_ext K _ _ (fun x => by show K.mul (K.mul (pw K A x.1) A) (pw K B x.2) = _; lib_ring) _
  rw [e]
  show K.add _ (K.add (K.mul K.one (pw K B (k + 1))) K.zero) = _
  lib_ring

theorem Tg_rec {R : Type} (K : CRing R) (A B : R) (k : Nat) :
    Tg K A B (k + 2) = K.add (K.mul (K.add A B) (Tg K A B (k + 1))) (K.neg (K.mul (K.mul A B) (Tg K A B k))) := by
  have h1 := Tg_succ K A B (k + 1)
  have h2 := Tg_succ K A B k
  have h3 : pw K B (k + 1 + 1) = K.mul B (pw K B (k + 1)) := by show K.mul _ _ = _; lib_ring
  have h4 : pw K B (k + 1) = K.add (Tg K A B (k + 1)) (K.neg (K.mul A (Tg K A B k))) := by rw [h2]; lib_ring
  rw [show k + 2 = k + 1 + 1 from rfl, h1, h3, h4]; lib_ring

variable (P : Nat)

theorem cw_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int)
    (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) (k : Nat) :
    cw W ((P + 1 + 1) ^ k) = sumK (Cyclo4.cyc4 LibInt.intR) (fun x => W (w a b x)) (Wl k) := by
  have hI : ∀ z, I ((P + 1 + 1) ^ k) z = sumK (Cyclo4.cyc4 LibInt.intR) (fun x => delta (Cyclo4.cyc4 LibInt.intR) z (w a b x)) (Wl k) := fun z => by
    by_cases hz : PD ((P + 1 + 1) ^ k) z
    · rw [I_pos _ _ hz]
      have hN : gnorm (toGI z) = ipow (Int.ofNat (P + 1 + 1)) k := by rw [gnorm_toGI, hz.1, ipow_nat]
      match ThetaCoef.primary_only P hp a b hab hπ k (toGI z) hz.2 hN with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w a b (j, l) := toGI_inj _ _ (by unfold w; rw [toGI_mul, toGI_pow, toGI_pow]; exact hx)
        rw [sumK_ext_all (Cyclo4.cyc4 LibInt.intR) k _ (fun x => delta (Cyclo4.cyc4 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct P hp a b hab hπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (allk_Wl k),
          count_Wl (Cyclo4.cyc4 LibInt.intR) k j l hjl]
    · rw [I_neg _ _ hz]
      rw [sumK_ext_all (Cyclo4.cyc4 LibInt.intR) k _ (fun _ => (Cyclo4.cyc4 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w_PD P hp a b hab hπ k x hxk))]) _ (allk_Wl k)]
      exact (zsumR (Cyclo4.cyc4 LibInt.intR) _).symm
  unfold cw
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine sumK_ext_all (Cyclo4.cyc4 LibInt.intR) k _ _ (fun x hxk => ?_) _ (allk_Wl k)
  have hb := abs_le (w a b x) _ (w_PD P hp a b hab hπ k x hxk).1
  exact pick_box (Cyclo4.cyc4 LibInt.intR) (fun z => W z) (w a b x) _ hb.1 hb.2

theorem W_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Cyclo4.cyc4 LibInt.intR) z j) = pw (Cyclo4.cyc4 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) z j) z) = (Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow W hW hW1 z j]

theorem cw_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) (k : Nat) :
    cw W ((P + 1 + 1) ^ k) = Tg (Cyclo4.cyc4 LibInt.intR) (W (a, b)) (W (a, -b)) k := by
  rw [cw_pk P W hp a b hab hπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w; rw [hW, W_pow W hW hW1, W_pow W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight.** -/
theorem cw_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b)) (k : Nat) :
    cw W ((P + 1 + 1) ^ (k + 2)) = (Cyclo4.cyc4 LibInt.intR).add
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (W (a, b)) (W (a, -b))) (cw W ((P + 1 + 1) ^ (k + 1))))
      ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (W (a, b)) (W (a, -b))) (cw W ((P + 1 + 1) ^ k)))) := by
  rw [cw_pk' P W hW hW1 hp a b hab hπ, cw_pk' P W hW hW1 hp a b hab hπ, cw_pk' P W hW hW1 hp a b hab hπ, Tg_rec]

end HeckePrime

#print axioms HeckePrime.Tg_succ
#print axioms HeckePrime.Tg_rec
#print axioms HeckePrime.cw_pk
#print axioms HeckePrime.W_pow
#print axioms HeckePrime.cw_pk'
#print axioms HeckePrime.cw_rec

#print axioms: 6 theorems, no axioms.

78.151 Hecke characters of \(\mathbb Q(i)\) at inert primes

The only primary element of norm \(q^{2j}\) is \((-q)^j\), and none has norm \(q^{2j+1}\).

\[ c_W(q^{2j})=W(-q)^j,\qquad c_W(q^{2j+1})=0\qquad (q\equiv 3\ (4)). \]

lang/HeckeInertW.lean

/-
  lang/HeckeInertW — T50, cycle 596: **Hecke characters of ℚ(i) at inert primes q = 4r + 3**, every weight W:
    c_W(q^(2j)) = W((−q)^j),   c_W(q^(2j+1)) = 0,
  and for multiplicative W with W(1) = 1: c_W(q^(2j)) = W(−q)^j (W(α) = α^k: (−q)^(kj)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.HeckePrimeW
import lang.HeckeInert

namespace HeckeInert

open FTAChain DetChain LibInt GaussEuclid GaussInt GaussFactor GaussUnique HeckeMult YMChain FiniteRecChain Frobenius LibGcd

variable (r : Nat)

theorem cw_even (W : Int × Int → Int × Int) (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (j : Nat) :
    cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j)) = W (pw (Cyclo4.cyc4 LibInt.intR) (mq r) j) := by
  have hI : ∀ z, I ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j)) z = delta (Cyclo4.cyc4 LibInt.intR) z (pw (Cyclo4.cyc4 LibInt.intR) (mq r) j) := fun z => by
    by_cases hz : PD ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j)) z
    · rw [I_pos _ _ hz]
      match inert_all r hq _ z hz.2 hz.1 with
      | ⟨j', hj, hz'⟩ =>
        have e : j' = j := (Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1) hj).symm
        unfold delta; rw [ite_pos' _ _ (by rw [hz', e])]
    · rw [I_neg _ _ hz]
      unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact mq_PD r j))]
  unfold cw
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun z => by rw [hI z])]
  have hb := abs_le _ _ (mq_PD r j).1
  exact pick_box (Cyclo4.cyc4 LibInt.intR) (fun z => W z) _ _ hb.1 hb.2

theorem cw_odd (W : Int × Int → Int × Int) (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (j : Nat) :
    cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j + 1)) = (Cyclo4.cyc4 LibInt.intR).zero := by
  have hI : ∀ z, (Cyclo4.cyc4 LibInt.intR).mul (I ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j + 1)) z) (W z) = (Cyclo4.cyc4 LibInt.intR).zero := fun z => by
    by_cases hz : PD ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j + 1)) z
    · exfalso
      match inert_all r hq _ z hz.2 hz.1 with
      | ⟨j', hj, _⟩ => exact GaussInt.int_odd_ne_even (Int.ofNat j) (Int.ofNat j') (congrArg Int.ofNat hj)
    · rw [I_neg _ _ hz]; lib_ring
  unfold cw
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ hI]
  exact HeckePrime.zsumR (Cyclo4.cyc4 LibInt.intR) _

theorem cw_even' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one)
    (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (j : Nat) :
    cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (2 * j)) = pw (Cyclo4.cyc4 LibInt.intR) (W (mq r)) j := by
  rw [cw_even r W hq j, HeckePrime.W_pow W hW hW1]

end HeckeInert

#print axioms HeckeInert.cw_even
#print axioms HeckeInert.cw_odd
#print axioms HeckeInert.cw_even'

#print axioms: 3 theorems, no axioms.

78.152 The Euler product for every Hecke character of \(\mathbb Q(i)\)

For multiplicative \(W\) with \(W(1)=1\) the coefficients factor over pairwise coprime odd factors.

\[ c_W(1)=1,\qquad c_W(n_1\cdots n_r)=c_W(n_1)\cdots c_W(n_r). \]

lang/HeckeEulerW.lean

/-
  lang/HeckeEulerW — T50, cycle 597: **the Euler product for every Hecke character of ℚ(i)** (multiplicative weight W, W(1) = 1):
    c_W(1) = 1,   c_W(mn) = c_W(m) c_W(n) (coprime odd),   c_W(n₁⋯n_r) = c_W(n₁)⋯c_W(n_r) (pairwise coprime odd).
  With HeckePrimeW/HeckeInertW this determines c_W at every odd n from W(π) at the split primes and W(−q) at the inert ones.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.HeckeInertW
import lang.HeckeEuler

namespace HeckeEuler

open FTAChain DetChain LibInt GaussEuclid GaussInt GaussFactor GaussUnique HeckeMult YMChain FiniteRecChain LibGcd

theorem cw_one (W : Int × Int → Int × Int) (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one) :
    cw W 1 = (Cyclo4.cyc4 LibInt.intR).one := by
  have hI : ∀ z, I 1 z = delta (Cyclo4.cyc4 LibInt.intR) z (Cyclo4.cyc4 LibInt.intR).one := fun z => by
    by_cases hz : PD 1 z
    · rw [I_pos _ _ hz]
      have hu : IsUnit (toGI z) := norm_one _ hz.1
      have h1 := GaussCube.unit_primary (toGI z) gone hu GaussCube.prim_one (by rw [HeckeInert.gone_right]; exact hz.2)
      unfold delta; rw [ite_pos' _ _ (toGI_inj z (Cyclo4.cyc4 LibInt.intR).one h1)]
    · rw [I_neg _ _ hz]
      unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact ⟨rfl, GaussCube.prim_one⟩))]
  unfold cw
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ _ (fun z => by rw [hI z])]
  rw [pick_box (Cyclo4.cyc4 LibInt.intR) (fun z => W z) (Cyclo4.cyc4 LibInt.intR).one 1 (Nat.le_refl 1) (Nat.zero_le 1)]
  exact hW1

theorem mult2_w (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (m n : Nat) (hmo : Odd' m) (hno : Odd' n) (hc : Cop m n) : cw W (m * n) = (Cyclo4.cyc4 LibInt.intR).mul (cw W m) (cw W n) := by
  match m, n, hmo, hno with
  | 0, _, ⟨k, hk⟩, _ => exact absurd (show (0 : Int) = 2 * k + 1 from hk) (fun e => GaussInt.int_odd_ne_even k 0 (e.symm.trans rfl))
  | _, 0, _, ⟨k, hk⟩ => exact absurd (show (0 : Int) = 2 * k + 1 from hk) (fun e => GaussInt.int_odd_ne_even k 0 (e.symm.trans rfl))
  | m' + 1, n' + 1, ⟨km, hm⟩, ⟨kn, hn⟩ =>
    match coprime_bezout hc with
    | ⟨s, t, hst⟩ => exact hecke_mult_w W hW m' n' s t km kn hst hm hn

/-- **The Euler product for c_W.** -/
theorem euler_w (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one) : ∀ ns : List Nat, Good ns → cw W (prodN ns) = prodE (ns.map (cw W))
  | [], _ => cw_one W hW1
  | n :: ns, ⟨hn, hc, hg⟩ => by
    show cw W (n * prodN ns) = (Cyclo4.cyc4 LibInt.intR).mul (cw W n) (prodE (ns.map (cw W)))
    rw [mult2_w W hW n _ hn (good_odd ns hg) hc, euler_w W hW hW1 ns hg]

end HeckeEuler

#print axioms HeckeEuler.cw_one
#print axioms HeckeEuler.mult2_w
#print axioms HeckeEuler.euler_w

#print axioms: 3 theorems, no axioms.

78.153 2-isogenous curves have the same number of points

For \(x\ne 0\), \(\chi (x(x^2+ax+b))=\chi (x+b/x+a)\), and \(x+b/x=u\) has \(1+\chi (u^2-4b)\) solutions.

\[ b\ne 0:\quad \sum _{x\in \mathbb F_p}\chi (x(x^2+ax+b))=\sum _{x\in \mathbb F_p}\chi (x(x^2-2ax+a^2-4b)). \]

lang/Isog2.lean

/-
  lang/Isog2 — T48, cycle 599: **2-isogenous curves have the same point count**: for p = 2m + 3 prime, a, b ∈ 𝔽_p, b ≠ 0,
    Σ_x χ(x(x² + ax + b)) = Σ_x χ(x(x² − 2ax + a² − 4b))
  (y² = x(x² + ax + b) and its 2-isogenous curve). Proof: for x ≠ 0, χ(x(x² + ax + b)) = χ(u + a), u = x + b/x;
  #{x : x + b/x = u} = [u = 0] + 1 + χ(u² − 4b) (completing the square, SqCount); Σχ(u + a) = 0; then u ↦ u + a.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.SqCount

namespace SqCount

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum

variable (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)

theorem complL : Complete LibInt.intR (ConicSum.L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR

/-- Σ_x F(x + c) = Σ_x F(x). -/
theorem shift_sum (F : Zp (2 * m + 1 + 1) → Int) (c : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun x => F ((K m).add x c)) (L m) = sumK LibInt.intR F (L m) :=
  reindex LibInt.intR _ (complL m) (fun x => (K m).add x c) (fun x => (K m).add x ((K m).neg c))
    (fun x => by show (K m).add ((K m).add x c) ((K m).neg c) = x; lib_ring) (fun y => by show (K m).add ((K m).add y ((K m).neg c)) c = y; lib_ring) F

include hpr in
/-- Σ_x F(2x + c) = Σ_x F(x) (2 is a unit). -/
theorem lin_sum (F : Zp (2 * m + 1 + 1) → Int) (c : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun x => F ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) c)) (L m) = sumK LibInt.intR F (L m) := by
  have h2 : (K m).add (K m).one (K m).one ≠ (K m).zero := GaussSplit.two_ne_zero (2 * m)
  have hi := iv_mul_self m hpr _ h2
  exact reindex LibInt.intR _ (complL m) (fun x => (K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) c)
    (fun y => (K m).mul (iv m ((K m).add (K m).one (K m).one)) ((K m).add y ((K m).neg c)))
    (fun x => by
      show (K m).mul (iv m ((K m).add (K m).one (K m).one)) ((K m).add ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) c) ((K m).neg c)) = x
      calc _ = (K m).mul ((K m).mul (iv m ((K m).add (K m).one (K m).one)) ((K m).add (K m).one (K m).one)) x := by lib_ring
        _ = x := by rw [hi]; lib_ring)
    (fun y => by
      show (K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul (iv m ((K m).add (K m).one (K m).one)) ((K m).add y ((K m).neg c)))) c = y
      calc _ = (K m).add ((K m).mul ((K m).mul (iv m ((K m).add (K m).one (K m).one)) ((K m).add (K m).one (K m).one)) ((K m).add y ((K m).neg c))) c := by lib_ring
        _ = y := by rw [hi]; lib_ring) F

include hpr in
/-- #{x : x² − ux + b = 0} = 1 + χ(u² − 4b). -/
theorem quad_count (u b : Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun x => delta LibInt.intR ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul u x))) b) (K m).zero) (L m) =
      1 + chi m ((K m).add ((K m).mul u u) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))) := by
  have h2 : (K m).add (K m).one (K m).one ≠ (K m).zero := GaussSplit.two_ne_zero (2 * m)
  have h4 : (K m).mul ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one) ≠ (K m).zero := fun e => by
    cases nozd (2 * m + 1) hpr _ _ e with
    | inl h => exact h2 h
    | inr h => exact h2 h
  -- 4(x² − ux + b) = (2x − u)² − (u² − 4b)
  have hpt : ∀ x, delta LibInt.intR ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul u x))) b) (K m).zero =
      delta LibInt.intR ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) ((K m).neg u)) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) ((K m).neg u)))
        ((K m).add ((K m).mul u u) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))) := fun x => by
    have key : (K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) ((K m).neg u)) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) x) ((K m).neg u)) =
        (K m).add ((K m).mul ((K m).mul ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul u x))) b))
          ((K m).add ((K m).mul u u) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))) := by lib_ring
    rw [key]
    generalize (K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul u x))) b = Fx
    generalize (K m).add ((K m).mul u u) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)) = D
    generalize (K m).mul ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one) = four at h4 ⊢
    unfold delta
    by_cases hq : Fx = (K m).zero
    · rw [ite_pos' _ _ hq, ite_pos' _ _ (by rw [hq]; lib_ring)]
    · rw [ite_neg' _ _ hq, ite_neg' _ _ (fun e => by
        have e' : (K m).mul four Fx = (K m).zero := by
          calc (K m).mul four Fx = (K m).add ((K m).add ((K m).mul four Fx) D) ((K m).neg D) := by lib_ring
            _ = (K m).add D ((K m).neg D) := by rw [e]
            _ = _ := by lib_ring
        cases nozd (2 * m + 1) hpr _ _ e' with
        | inl h => exact h4 h
        | inr h => exact hq h)]
  rw [sumK_ext _ _ _ hpt]
  rw [lin_sum m hpr (fun t => delta LibInt.intR ((K m).mul t t) ((K m).add ((K m).mul u u) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)))) ((K m).neg u)]
  exact sq_count m hpr _

theorem dpos {G : Type} [DecidableEq G] (y z : G) (h : y = z) : delta LibInt.intR y z = 1 := by unfold delta; rw [ite_pos' _ _ h]; rfl
theorem dneg {G : Type} [DecidableEq G] (y z : G) (h : ¬ y = z) : delta LibInt.intR y z = 0 := by unfold delta; rw [ite_neg' _ _ h]; rfl

def uu (b x : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (K m).add x ((K m).mul b (iv m x))

theorem uu_zero (b : Zp (2 * m + 1 + 1)) : uu m b (K m).zero = (K m).zero := by
  show (K m).add (K m).zero ((K m).mul b (iv m (K m).zero)) = _; rw [iv_zero]; lib_ring

theorem hsub (f g : Zp (2 * m + 1 + 1) → Int) :
    sumK LibInt.intR (fun x => f x - g x) (L m) = sumK LibInt.intR f (L m) - sumK LibInt.intR g (L m) := by
  have h1 := sumK_add LibInt.intR f (fun x => LibInt.intR.neg (g x)) (L m)
  rw [CMInert.sum_neg_fun LibInt.intR g] at h1
  exact h1.trans (by show _ + -_ = _; lib_int)

include hpr in
/-- **2-isogeny**: Σχ(x(x² + ax + b)) = Σχ(x(x² − 2ax + a² − 4b)) for b ≠ 0. -/
theorem isog2 (a b : Zp (2 * m + 1 + 1)) (hb : b ≠ (K m).zero) :
    sumK LibInt.intR (fun x => chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).mul a x)) b))) (L m) =
    sumK LibInt.intR (fun x => chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul a x))))
      ((K m).add ((K m).mul a a) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)))))) (L m) := by
  have hc := complL m
  have p1 : ∀ x, chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).mul a x)) b)) =
      chi m ((K m).add (uu m b x) a) - delta LibInt.intR x (K m).zero * chi m a := fun x => by
    by_cases hx : x = (K m).zero
    · rw [hx, uu_zero, dpos _ _ rfl,
        show (K m).mul (K m).zero ((K m).add ((K m).add ((K m).mul (K m).zero (K m).zero) ((K m).mul a (K m).zero)) b) = (K m).zero by lib_ring,
        show (K m).add (K m).zero a = a by lib_ring, chi_zero]
      lib_int
    · have hi := iv_mul_self m hpr x hx
      have e : (K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).mul a x)) b) = (K m).mul ((K m).mul x x) ((K m).add (uu m b x) a) := by
        show _ = (K m).mul ((K m).mul x x) ((K m).add ((K m).add x ((K m).mul b (iv m x))) a)
        calc _ = (K m).add ((K m).mul ((K m).mul x x) ((K m).add x a)) ((K m).mul (K m).one ((K m).mul b x)) := by lib_ring
          _ = (K m).add ((K m).mul ((K m).mul x x) ((K m).add x a)) ((K m).mul ((K m).mul (iv m x) x) ((K m).mul b x)) := by rw [hi]
          _ = _ := by lib_ring
      rw [e, chi_mul m hpr, chi_sq m hpr x hx, dneg _ _ hx]; lib_int
  have p3 : ∀ v x, delta LibInt.intR v (uu m b x) = delta LibInt.intR x (K m).zero * delta LibInt.intR v (K m).zero +
      delta LibInt.intR ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul v x))) b) (K m).zero := fun v x => by
    by_cases hx : x = (K m).zero
    · rw [hx, uu_zero, dpos _ _ rfl, show (K m).add ((K m).add ((K m).mul (K m).zero (K m).zero) ((K m).neg ((K m).mul v (K m).zero))) b = b by lib_ring,
        dneg _ _ hb]
      lib_int
    · have hi := iv_mul_self m hpr x hx
      have key : (K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul v x))) b = (K m).mul x ((K m).add (uu m b x) ((K m).neg v)) := by
        show _ = (K m).mul x ((K m).add ((K m).add x ((K m).mul b (iv m x))) ((K m).neg v))
        calc _ = (K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul v x))) ((K m).mul (K m).one b) := by lib_ring
          _ = (K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul v x))) ((K m).mul ((K m).mul (iv m x) x) b) := by rw [hi]
          _ = _ := by lib_ring
      rw [key, dneg _ _ hx]
      by_cases hv : v = uu m b x
      · rw [dpos _ _ hv, dpos _ _ (show (K m).mul x ((K m).add (uu m b x) ((K m).neg v)) = (K m).zero by rw [hv]; lib_ring)]; lib_int
      · rw [dneg _ _ hv, dneg ((K m).mul x ((K m).add (uu m b x) ((K m).neg v))) (K m).zero (fun e => by
          cases nozd (2 * m + 1) hpr _ _ e with
          | inl h => exact hx h
          | inr h =>
            apply hv
            calc v = (K m).add (uu m b x) ((K m).neg ((K m).add (uu m b x) ((K m).neg v))) := by lib_ring
              _ = (K m).add (uu m b x) ((K m).neg (K m).zero) := by rw [h]
              _ = _ := by lib_ring)]
        lib_int
  have hA : sumK LibInt.intR (fun x => delta LibInt.intR x (K m).zero * chi m a) (L m) = chi m a := by
    have h := delta_sum LibInt.intR (fun _ => chi m a) (K m).zero (L m)
    rw [hc] at h
    exact h.trans (by show 1 * chi m a = chi m a; lib_int)
  have hT : sumK LibInt.intR (fun x => chi m ((K m).add (uu m b x) a)) (L m) =
      sumK LibInt.intR (fun v => sumK LibInt.intR (fun x => delta LibInt.intR v (uu m b x)) (L m) * chi m ((K m).add v a)) (L m) := by
    have e1 : ∀ x, chi m ((K m).add (uu m b x) a) = sumK LibInt.intR (fun v => delta LibInt.intR v (uu m b x) * chi m ((K m).add v a)) (L m) := fun x => by
      have h := delta_sum LibInt.intR (fun v => chi m ((K m).add v a)) (uu m b x) (L m)
      rw [hc] at h
      exact (h.trans (by show 1 * chi m ((K m).add (uu m b x) a) = chi m ((K m).add (uu m b x) a); lib_int)).symm
    rw [sumK_ext _ _ _ e1, sumK_swap]
    exact sumK_ext _ _ _ (fun v => (sumK_mul LibInt.intR _ _ _).symm) _
  have hN : ∀ v, sumK LibInt.intR (fun x => delta LibInt.intR v (uu m b x)) (L m) = delta LibInt.intR v (K m).zero + (1 + chi m ((K m).add ((K m).mul v v) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)))) := fun v => by
    rw [sumK_ext _ _ _ (p3 v)]
    show sumK LibInt.intR (fun x => LibInt.intR.add (LibInt.intR.mul (delta LibInt.intR x (K m).zero) (delta LibInt.intR v (K m).zero))
      (delta LibInt.intR ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul v x))) b) (K m).zero)) (L m) = _
    rw [sumK_add, ← sumK_mul, hc, quad_count m hpr v b]
    show 1 * delta LibInt.intR v (K m).zero + _ = _; lib_int
  have hS0 : sumK LibInt.intR (fun v => chi m ((K m).add v a)) (L m) = 0 := by
    rw [shift_sum m (chi m) a]; exact chi_sum m hpr
  have hDa : sumK LibInt.intR (fun v => delta LibInt.intR v (K m).zero * chi m ((K m).add v a)) (L m) = chi m a := by
    have h := delta_sum LibInt.intR (fun v => chi m ((K m).add v a)) (K m).zero (L m)
    rw [hc] at h
    exact h.trans (by rw [show (K m).add (K m).zero a = a by lib_ring]; show 1 * chi m a = chi m a; lib_int)
  have e2 : ∀ v, sumK LibInt.intR (fun x => delta LibInt.intR v (uu m b x)) (L m) * chi m ((K m).add v a) =
      LibInt.intR.add (LibInt.intR.add (delta LibInt.intR v (K m).zero * chi m ((K m).add v a)) (chi m ((K m).add v a)))
        (chi m ((K m).mul ((K m).add v a) ((K m).add ((K m).mul v v) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))))) := fun v => by
    rw [hN v, chi_mul m hpr]
    generalize delta LibInt.intR v (K m).zero = d
    generalize chi m ((K m).add ((K m).mul v v) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))) = c1
    generalize chi m ((K m).add v a) = c2
    show (d + (1 + c1)) * c2 = d * c2 + c2 + c2 * c1; lib_int
  have e3 : ∀ v, chi m ((K m).mul ((K m).add v a) ((K m).add ((K m).add ((K m).mul ((K m).add v a) ((K m).add v a)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul a ((K m).add v a)))))
      ((K m).add ((K m).mul a a) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))))) = chi m ((K m).mul ((K m).add v a) ((K m).add ((K m).mul v v) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)))) := fun v => by
    rw [show (K m).add ((K m).add ((K m).mul ((K m).add v a) ((K m).add v a)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul a ((K m).add v a)))))
      ((K m).add ((K m).mul a a) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b))) = (K m).add ((K m).mul v v) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)) by lib_ring]
  rw [sumK_ext _ _ _ p1, hsub, hA, hT, sumK_ext _ _ _ e2, sumK_add, sumK_add, hDa, hS0]
  rw [← shift_sum m (fun x => chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul a x))))
      ((K m).add ((K m).mul a a) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) b)))))) a, sumK_ext _ _ _ e3]
  show chi m a + 0 + _ - chi m a = _; lib_int

end SqCount

#print axioms SqCount.complL
#print axioms SqCount.shift_sum
#print axioms SqCount.lin_sum
#print axioms SqCount.quad_count
#print axioms SqCount.dpos
#print axioms SqCount.hsub
#print axioms SqCount.isog2

#print axioms: 7 theorems, no axioms.

78.154 CM by \(\mathbb Z[\sqrt {-2}]\) at inert primes

The 2-isogeny with \((a,b)=(4,2)\) and \(x\mapsto -2x\) give \(S=\chi (-2)S\) for \(S=\sum _x\chi (x^3+4x^2+2x)\).

\[ \chi (-2)=-1\ \Longrightarrow \ \sum _{x\in \mathbb F_p}\chi (x^3+4x^2+2x)=0. \]

lang/CM2.lean

/-
  lang/CM2 — T48, cycle 600: **the CM curve y² = x³ + 4x² + 2x (CM by ℤ[√−2])**: for every odd prime p,
    S := Σ_x χ(x³ + 4x² + 2x) = χ(−2)·S,
  so a_p = −S = 0 whenever −2 is not a square mod p (p ≡ 5, 7 mod 8: the primes inert in ℚ(√−2)).
  Proof: the 2-isogeny Isog2.isog2 with (a, b) = (4, 2) gives S = Σχ(x(x² − 8x + 8)), and x ↦ −2x turns x(x² − 8x + 8) into −8·x(x² + 4x + 2).
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Isog2
import lang.Prim32

namespace SqCount

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum

variable (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)

include hpr in
/-- Σ_x F(cx) = Σ_x F(x) for c ≠ 0. -/
theorem scale_sum (F : Zp (2 * m + 1 + 1) → Int) (c : Zp (2 * m + 1 + 1)) (hc0 : c ≠ (K m).zero) :
    sumK LibInt.intR (fun x => F ((K m).mul c x)) (L m) = sumK LibInt.intR F (L m) := by
  have hi := iv_mul_self m hpr c hc0
  exact reindex LibInt.intR _ (complL m) (fun x => (K m).mul c x) (fun y => (K m).mul (iv m c) y)
    (fun x => by show (K m).mul (iv m c) ((K m).mul c x) = x
                 calc _ = (K m).mul ((K m).mul (iv m c) c) x := by lib_ring
                   _ = x := by rw [hi]; lib_ring)
    (fun y => by show (K m).mul c ((K m).mul (iv m c) y) = y
                 calc _ = (K m).mul ((K m).mul (iv m c) c) y := by lib_ring
                   _ = y := by rw [hi]; lib_ring) F

include hpr in
/-- **S = χ(−2)·S** for y² = x³ + 4x² + 2x. -/
theorem cm2_sym :
    sumK LibInt.intR (fun x => chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) x)) ((K m).add (K m).one (K m).one)))) (L m) = chi m ((K m).neg ((K m).add (K m).one (K m).one)) * sumK LibInt.intR (fun x => chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).mul ((K m).add ((K m).add (K m).one  -- (+95 chars; full line in the Zenodo source)
  have h2 : (K m).add (K m).one (K m).one ≠ (K m).zero := GaussSplit.two_ne_zero (2 * m)
  have hn2 : (K m).neg ((K m).add (K m).one (K m).one) ≠ (K m).zero := fun e => h2 (by calc (K m).add (K m).one (K m).one = (K m).neg ((K m).neg ((K m).add (K m).one (K m).one)) := by lib_ring
    _ = _ := by rw [e]; lib_ring)
  have e : ∀ t, chi m ((K m).mul ((K m).mul ((K m).neg ((K m).add (K m).one (K m).one)) t) ((K m).add ((K m).add ((K m).mul ((K m).mul ((K m).neg ((K m).add (K m).one (K m).one)) t) ((K m).mul ((K m).neg ((K m).add (K m).one (K m).one)) t))
      ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) ((K m).mul ((K m).neg ((K m).add (K m).one (K m).one)) t)))))
      ((K m).add ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one))) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) ((K m).add (K m).one (K m).one)))))) = chi m ((K m).neg ((K m).add (K m).one (K m).one)) * chi m ((K m).mul t ((K m).add   -- (+166 chars; full line in the Zenodo source)
    rw [show (K m).mul ((K m).mul ((K m).neg ((K m).add (K m).one (K m).one)) t) ((K m).add ((K m).add ((K m).mul ((K m).mul ((K m).neg ((K m).add (K m).one (K m).one)) t) ((K m).mul ((K m).neg ((K m).add (K m).one (K m).one)) t))
      ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) ((K m).mul ((K m).neg ((K m).add (K m).one (K m).one)) t)))))
      ((K m).add ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one))) ((K m).neg ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) ((K m).add (K m).one (K m).one))))) =
      (K m).mul ((K m).neg ((K m).add (K m).one (K m).one)) ((K m).mul ((K m).mul ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) ((K m).mul t ((K m).add ((K m).add ((K m).mul t t) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) t)) ((K m).add (K m).one (K m).one)))) by lib_ring,
      chi_mul m hpr, chi_mul m hpr, chi_sq m hpr _ h2]
    show _ * (1 * _) = _; lib_int
  calc _ = _ := isog2 m hpr ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) ((K m).add (K m).one (K m).one) h2
    _ = _ := (scale_sum m hpr _ ((K m).neg ((K m).add (K m).one (K m).one)) hn2).symm
    _ = _ := sumK_ext _ _ _ e _
    _ = _ := (mul_sumK LibInt.intR _ _ _).symm

include hpr in
/-- **a_p = 0 at the primes where −2 is not a square.** -/
theorem cm2_inert (h : chi m ((K m).neg ((K m).add (K m).one (K m).one)) = -1) : sumK LibInt.intR (fun x => chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) x)) ((K m).add (K m).one (K m).one)))) (L m) = 0 := by
  have hs := cm2_sym m hpr
  rw [h] at hs
  generalize sumK LibInt.intR (fun x => chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) x)) ((K m).add (K m).one (K m).one)))) (L m) = S at hs ⊢
  exact Hecke32.two_cancel S 0 (by calc 2 * S = S + S := by lib_int
    _ = -1 * S + S := by rw [← hs]
    _ = 2 * 0 := by lib_int)

end SqCount

#print axioms SqCount.scale_sum
#print axioms SqCount.cm2_sym
#print axioms SqCount.cm2_inert

#print axioms: 3 theorems, no axioms.

78.155 CM by \(\mathbb Z[\sqrt {-2}]\): \(a_p=0\) at every inert prime

\(\chi (-1)\chi (2)=-1\) at \(p\equiv 5,7\pmod 8\) (the supplementary laws of TwoSupp, read in \(\mathbb F_p\)).

\[ p\equiv 5,7\ (8):\quad \sum _{x\in \mathbb F_p}\chi (x^3+4x^2+2x)=0. \]

lang/Chi2.lean

/-
  lang/Chi2 — T48, cycle 601: **χ(−2) = −1 at the primes p ≡ 5, 7 (mod 8)** in the QChar setting (p = 2m + 3):
  p = 8k + 5: χ(−1) = 1, χ(2) = −1 (TwoSupp.two_8m5); p = 8k + 7: χ(−1) = −1, χ(2) = 1 (TwoSupp.two_8m7), bridged through
  Inf3mod8.sq_rem/md_rem as in Inf7m8. With CM2: a_p(y² = x³ + 4x² + 2x) = 0 at every p ≡ 5, 7 (mod 8).
  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.CM2
import lang.Inf3mod8
import lang.TwoSupp

namespace SqCount

open FTAChain ZMod YMChain FiniteRecChain QChar

variable (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)

theorem neg_one_ne : (K m).neg (K m).one ≠ (K m).one := QChar.ne_pm m

theorem m1_even : ∀ N, Frobenius.pw (K m) ((K m).neg (K m).one) (2 * N) = (K m).one
  | 0 => rfl
  | N + 1 => by
    rw [show 2 * (N + 1) = 2 * N + 1 + 1 by lib_nat]
    show (K m).mul ((K m).mul (Frobenius.pw (K m) ((K m).neg (K m).one) (2 * N)) ((K m).neg (K m).one)) ((K m).neg (K m).one) = _
    rw [m1_even N]; lib_ring

theorem chi_m1_even (N : Nat) (h : m + 1 = 2 * N) : chi m ((K m).neg (K m).one) = 1 := by
  apply chi_one_of
  · exact fun e => PowerSum.one_ne_zero' (2 * m + 1) (by
      calc (K m).one = (K m).neg ((K m).neg (K m).one) := by lib_ring
        _ = _ := by rw [e]; lib_ring)
  · rw [h]; exact m1_even m N

theorem chi_m1_odd (N : Nat) (h : m + 1 = 2 * N + 1) : chi m ((K m).neg (K m).one) = -1 := by
  apply chi_neg_of
  · exact fun e => PowerSum.one_ne_zero' (2 * m + 1) (by
      calc (K m).one = (K m).neg ((K m).neg (K m).one) := by lib_ring
        _ = _ := by rw [e]; lib_ring)
  · rw [h]
    show (K m).mul (Frobenius.pw (K m) ((K m).neg (K m).one) (2 * N)) ((K m).neg (K m).one) ≠ _
    rw [m1_even m N, (K m).one_mul]; exact neg_one_ne m

include hpr in
/-- p = 8k + 5: χ(2) = −1. -/
theorem chi_two_5 (k : Nat) (hp : 2 * m + 1 + 1 + 1 = 8 * k + 5) : chi m ((K m).add (K m).one (K m).one) = -1 := by
  have h2 : (K m).add (K m).one (K m).one ≠ (K m).zero := GaussSplit.two_ne_zero (2 * m)
  apply chi_neg_of m _ h2
  intro hq
  obtain ⟨z, hz⟩ := (NthPow.nth_crit (2 * m + 1) hpr 2 (m + 1) (by lib_nat) _ h2).mpr hq
  have hz' : (ring (2 * m + 1 + 1)).mul (mk (2 * m + 1 + 1) z.val) (mk (2 * m + 1 + 1) z.val) = mk (2 * m + 1 + 1) 2 := by
    rw [mk_val, Inf3mod8.two_eq, ← hz]; show _ = (K m).mul ((K m).mul (K m).one z) z; lib_ring
  have hsq := Inf3mod8.sq_rem (2 * m + 1) z.val hz'
  exact absurd hsq (fun hh => TwoSupp.two_8m5 k (by
      have e : (k + k + 1) + ((k + k + 1) + 0) + ((k + k + 1) + ((k + k + 1) + 0)) + 1 = 2 * m + 1 + 1 + 1 := by rw [hp]; lib_nat
      rw [e]; exact ⟨⟨2 * m + 1, rfl⟩, hpr⟩)
    ⟨z.val, by
      have e : (k + k + 1) + ((k + k + 1) + 0) + ((k + k + 1) + ((k + k + 1) + 0)) + 1 = 2 * m + 1 + 1 + 1 := by rw [hp]; lib_nat
      rw [e]; exact hh⟩)

include hpr in
/-- p = 8k + 7: χ(2) = 1. -/
theorem chi_two_7 (k : Nat) (hp : 2 * m + 1 + 1 + 1 = 8 * k + 7) : chi m ((K m).add (K m).one (K m).one) = 1 := by
  have h2 : (K m).add (K m).one (K m).one ≠ (K m).zero := GaussSplit.two_ne_zero (2 * m)
  have e : (k + k + 1) + ((k + k + 1) + 1) + ((k + k + 1) + ((k + k + 1) + 1)) + 1 = 2 * m + 1 + 1 + 1 := by rw [hp]; lib_nat
  obtain ⟨x, hx⟩ := TwoSupp.two_8m7 k (by rw [e]; exact ⟨⟨2 * m + 1, rfl⟩, hpr⟩)
  rw [e] at hx
  have hmk : mk (2 * m + 1 + 1) (x * x) = mk (2 * m + 1 + 1) 2 :=
    Subtype.ext (by show md (2 * m + 1 + 1) (x * x) = md (2 * m + 1 + 1) 2; rw [Inf3mod8.md_rem, Inf3mod8.md_rem]; exact hx)
  have hsq : (K m).mul (mk (2 * m + 1 + 1) x) (mk (2 * m + 1 + 1) x) = (K m).add (K m).one (K m).one := by
    rw [TwoPower.mk_mul, hmk, Inf3mod8.two_eq]
  have hx0 : mk (2 * m + 1 + 1) x ≠ (K m).zero := fun e0 => h2 (by rw [← hsq, e0]; lib_ring)
  rw [← hsq]; exact chi_sq m hpr _ hx0

include hpr in
/-- **χ(−2) = −1 for p ≡ 5, 7 (mod 8).** -/
theorem chi_m2 (k : Nat) (hp : 2 * m + 1 + 1 + 1 = 8 * k + 5 ∨ 2 * m + 1 + 1 + 1 = 8 * k + 7) : chi m ((K m).neg ((K m).add (K m).one (K m).one)) = -1 := by
  rw [show (K m).neg ((K m).add (K m).one (K m).one) = (K m).mul ((K m).neg (K m).one) ((K m).add (K m).one (K m).one) by lib_ring, chi_mul m hpr]
  rcases hp with h | h
  · have hm : m + 1 = 2 * (2 * k + 1) := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1)
      (Nat.succ.inj ((show 2 * (m + 1) + 1 = 2 * m + 1 + 1 + 1 by lib_nat).trans (h.trans (by lib_nat))))
    rw [chi_m1_even m _ hm, chi_two_5 m hpr k h]; rfl
  · have hm : m + 1 = 2 * (2 * k + 1) + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1)
      (Nat.succ.inj ((show 2 * (m + 1) + 1 = 2 * m + 1 + 1 + 1 by lib_nat).trans (h.trans (by lib_nat))))
    rw [chi_m1_odd m _ hm, chi_two_7 m hpr k h]; rfl

include hpr in
/-- **CM by ℤ[√−2] at the inert primes**: Σ_x χ(x³ + 4x² + 2x) = 0 for p ≡ 5, 7 (mod 8). -/
theorem cm2_mod8 (k : Nat) (hp : 2 * m + 1 + 1 + 1 = 8 * k + 5 ∨ 2 * m + 1 + 1 + 1 = 8 * k + 7) :
    sumK LibInt.intR (fun x => chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) x)) ((K m).add (K m).one (K m).one)))) (ConicSum.L m) = 0 :=
  cm2_inert m hpr (chi_m2 m hpr k hp)

end SqCount

#print axioms SqCount.neg_one_ne
#print axioms SqCount.m1_even
#print axioms SqCount.chi_m1_even
#print axioms SqCount.chi_m1_odd
#print axioms SqCount.chi_two_5
#print axioms SqCount.chi_two_7
#print axioms SqCount.chi_m2
#print axioms SqCount.cm2_mod8

#print axioms: 8 theorems, no axioms.

78.156 Euler’s pentagonal theorem at every degree

In every commutative ring with \(t^M=0\), from the two-parameter finite triple product at \(x=-1\) with \(Q=t^3\), \(a=t^2\), \(b=t\).

\[ \sum _{m\le 4M}(-1)^m\,t^{(3j^2+j)/2}\Big |_{j=m-2M}=(t;t)_M. \]

lang/Pent.lean

/-
  lang/Pent — T47, cycle 571: **Euler's pentagonal theorem at every degree**: in every commutative ring with t^M = 0,
    Σ_{m ≤ 4M} (−1)^m t^(2·Ta(2M, m) + Tb(2M, m)) = (t; t)_M,
  where for j = m − 2M the exponent 2Ta + Tb = j(j+1) + j(j−1)/2 = (3j² + j)/2, i.e. ∏(1 − tⁿ) = Σ_j (−1)^j t^(j(3j+1)/2).

    pq3, od3, pp_split3     (t; t)_(3T) = (t³; t³)_T · ∏_{k<T} (1 − t·t^(3k))(1 − t²·t^(3k))
    pr_od                   the x = −1 product of PentL with Q = t³, a = t², b = t
    pent
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PentL
import lang.JacobiP

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R) (t : R)

theorem pq3 (T : Nat) : Frobenius.pw K (K.mul (K.mul t t) t) T = Frobenius.pw K t (3 * T) := by
  rw [Frobenius.pw_mul, Frobenius.pw_mul, pw_sum K t T T (T + T) rfl, pw_sum K t (T + T) T (3 * T) (by lib_nat)]

def od3 : Nat → R
  | 0 => K.one
  | T + 1 => K.mul (od3 T) (K.mul (K.add K.one (K.neg (K.mul t (Frobenius.pw K (K.mul (K.mul t t) t) T))))
      (K.add K.one (K.neg (K.mul (K.mul t t) (Frobenius.pw K (K.mul (K.mul t t) t) T)))))

theorem pp_split3 : ∀ T, pp K t (3 * T) = K.mul (pp K (K.mul (K.mul t t) t) T) (od3 K t T)
  | 0 => by show K.one = K.mul K.one K.one; lib_ring
  | T + 1 => by
    rw [show 3 * (T + 1) = 3 * T + 1 + 1 + 1 by lib_nat]
    show K.mul (K.mul (K.mul (pp K t (3 * T)) (K.add K.one (K.neg (Frobenius.pw K t (3 * T + 1)))))
        (K.add K.one (K.neg (Frobenius.pw K t (3 * T + 1 + 1))))) (K.add K.one (K.neg (Frobenius.pw K t (3 * T + 1 + 1 + 1)))) =
      K.mul (K.mul (pp K (K.mul (K.mul t t) t) T) (K.add K.one (K.neg (Frobenius.pw K (K.mul (K.mul t t) t) (T + 1)))))
        (K.mul (od3 K t T) (K.mul (K.add K.one (K.neg (K.mul t (Frobenius.pw K (K.mul (K.mul t t) t) T))))
          (K.add K.one (K.neg (K.mul (K.mul t t) (Frobenius.pw K (K.mul (K.mul t t) t) T))))))
    rw [pp_split3 T, show Frobenius.pw K (K.mul (K.mul t t) t) (T + 1) = K.mul (Frobenius.pw K (K.mul (K.mul t t) t) T) (K.mul (K.mul t t) t) from rfl,
      show Frobenius.pw K t (3 * T + 1 + 1 + 1) = K.mul (K.mul (K.mul (Frobenius.pw K t (3 * T)) t) t) t from rfl,
      show Frobenius.pw K t (3 * T + 1 + 1) = K.mul (K.mul (Frobenius.pw K t (3 * T)) t) t from rfl,
      show Frobenius.pw K t (3 * T + 1) = K.mul (Frobenius.pw K t (3 * T)) t from rfl, ← pq3]
    generalize Frobenius.pw K (K.mul (K.mul t t) t) T = P
    lib_ring

theorem pr_od : ∀ N, pr K (K.mul t t) t (K.mul (K.mul t t) t) N = K.mul (Frobenius.pw K (K.neg K.one) N) (od3 K t N)
  | 0 => by show K.one = K.mul K.one K.one; lib_ring
  | N + 1 => by
    show K.mul (pr K (K.mul t t) t (K.mul (K.mul t t) t) N) _ = K.mul (K.mul (Frobenius.pw K (K.neg K.one) N) (K.neg K.one)) (K.mul (od3 K t N) _)
    rw [pr_od N]
    generalize Frobenius.pw K (K.mul (K.mul t t) t) N = P
    lib_ring

/-- **Euler's pentagonal theorem mod t^M.** -/
theorem pent (M : Nat) (ht : Frobenius.pw K t M = K.zero) :
    es K (fun m => Frobenius.pw K t (2 * Ta (2 * M) m + Tb (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1) = pp K t M := by
  have ha : Frobenius.pw K (K.mul t t) M = K.zero := by
    rw [pwQ]; exact pw_zero_of_le K t M _ ht (Nat.le_trans (Nat.le_add_left M M) (Nat.le_of_eq (by lib_nat)))
  have hg := pent_gen K (K.mul t t) t (K.mul (K.mul t t) t) rfl M ha ht
  have hc := es_congr K (fun m => K.mul (Frobenius.pw K (K.mul t t) (Ta (2 * M) m)) (Frobenius.pw K t (Tb (2 * M) m)))
    (fun m => Frobenius.pw K t (2 * Ta (2 * M) m + Tb (2 * M) m)) (K.neg K.one) (fun m => by rw [pwQ, pw_sum K t _ _ _ rfl]) (2 * (2 * M) + 1)
  rw [← hc, hg, pr_od, pm_even]
  have hQ : Frobenius.pw K (K.mul (K.mul t t) t) M = K.zero := by
    rw [pq3]; exact pw_zero_of_le K t M _ ht (Nat.le_trans (Nat.le_add_left M (2 * M)) (Nat.le_of_eq (by lib_nat)))
  have s1 := pp_split3 K t (2 * M)
  rw [show pp K (K.mul (K.mul t t) t) (2 * M) = pp K (K.mul (K.mul t t) t) M by
      rw [show 2 * M = M + M by lib_nat, pp_stable K _ M hQ M],
    show pp K t (3 * (2 * M)) = pp K t M by rw [show 3 * (2 * M) = M + 5 * M by lib_nat, pp_stable K t M ht]] at s1
  rw [s1]; lib_ring

end QBinom

#print axioms QBinom.pq3
#print axioms QBinom.pp_split3
#print axioms QBinom.pr_od
#print axioms QBinom.pent

#print axioms: 4 theorems, no axioms.

78.157 The eta product of 36a as Euler times Jacobi

With \(x=q^6\), \(s=q^3\) and \(q^M=0\), the pentagonal theorem and Jacobi’s identity give \(\eta (6z)^4\) at every coefficient.

\[ q\,(x;x)_M^4=q\sum _{m\le 4M}(-1)^mx^{(3j^2+j)/2}\sum _{i<2M+2}(-1)^i(2i+1)s^{i^2+i}. \]

lang/Eta36.lean

/-
  lang/Eta36 — T47, cycle 572: **the 36a eta product as Euler × Jacobi at every degree**. In every commutative ring with q^M = 0,
  x = q⁶, s = q³:
    q·(x; x)_M⁴ = q · Σ_{m ≤ 4M} (−1)^m x^((3j²+j)/2) · Σ_{i < 2M+2} (−1)^i (2i+1) s^(i² + i)      (j = m − 2M),
  i.e. η(6z)⁴ = Σ_{j ∈ ℤ, i ≥ 0} (−1)^(j+i) (2i+1) q^(1 + 3j(3j+1) + 3i(i+1)) = Σ (−1)^(j+i)(2i+1) q^(((6j+1)² + 3(2i+1)²)/4).
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Pent

namespace QBinom

open FTAChain

variable {R : Type} (K : DetChain.CRing R)

theorem eta36 (q : R) (M : Nat) (hq : Frobenius.pw K q M = K.zero) :
    K.mul q (K.mul (K.mul (pp K (Frobenius.pw K q 6) M) (pp K (Frobenius.pw K q 6) M))
      (K.mul (pp K (Frobenius.pw K q 6) M) (pp K (Frobenius.pw K q 6) M))) =
    K.mul q (K.mul (es K (fun m => Frobenius.pw K (Frobenius.pw K q 6) (2 * Ta (2 * M) m + Tb (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1))
      (T K (Frobenius.pw K q 3) (2 * M + 2))) := by
  have hx : Frobenius.pw K (Frobenius.pw K q 6) M = K.zero := by
    rw [TwoPower.pwpow]; exact pw_zero_of_le K q M _ hq (Nat.le_trans (Nat.le_add_left M (5 * M)) (Nat.le_of_eq (by lib_nat)))
  have hs : Frobenius.pw K (Frobenius.pw K q 3) M = K.zero := by
    rw [TwoPower.pwpow]; exact pw_zero_of_le K q M _ hq (Nat.le_trans (Nat.le_add_left M (2 * M)) (Nat.le_of_eq (by lib_nat)))
  have e6 : K.mul (Frobenius.pw K q 3) (Frobenius.pw K q 3) = Frobenius.pw K q 6 := pw_sum K q 3 3 6 rfl
  rw [pent K _ M hx, jacobi_eta3 K _ M hs, e6, pp_stable K _ M hx 1]
  lib_ring

end QBinom

#print axioms QBinom.eta36

#print axioms: 1 theorem, no axioms.

78.158 36a: \(a_p\) equals the eta coefficient at every split prime

For \(p=a^2+3b^2\), \(a\equiv 1\pmod 3\), the only terms of \([q^p]\eta (6z)^4\) come from \((6j+1,2i+1)=(a-3b,\pm (a+b))\) and \((a+3b,\pm (a-b))\), with weights \(a+b\) and \(a-b\).

\[ p\equiv 1\ (3):\ [q^p]\,\eta (6z)^4=2a=-\sum _{x\in \mathbb F_p}\chi (x^3+1). \]

lang/Ap36.lean

/-
  lang/Ap36 — T47, cycle 578: **36a at every prime p ≡ 1 (mod 3)**: [q^p]η(6z)⁴ = −Σ_x χ(x³ + 1) = a_p(y² = x³ + 1).

    g36, g36_shift, e2G, cond_int, cond_iff     exponent of the pair (j, i) and the condition (6j+1)² + 3(2i+1)² = 4p
    pos_eq, j_eq                                  uniqueness of i, j
    etaC36 p                                      the coefficient of q^p in η(6z)⁴ (computed in ℤ[q]/(q^(2p+1)))
    ap36_split
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Coef36
import lang.GzGen
import lang.W36
import lang.CubicSign

namespace Hecke36

open DetChain YMChain FiniteRecChain Hecke32 FTAChain

def g36 (N m : Nat) : Nat := 2 * QBinom.Ta N m + QBinom.Tb N m

theorem g36_shift (N m : Nat) : g36 (N + 1) (m + 1) = g36 N m := by
  show 2 * QBinom.Ta (N + 1) (m + 1) + QBinom.Tb (N + 1) (m + 1) = 2 * QBinom.Ta N m + QBinom.Tb N m
  rw [QBinom.ta_shift, QBinom.tb_shift]

theorem e2G (j : Int) : 2 * Int.ofNat (QBinom.Gj g36 j) = 3 * (j * j) + j := by
  cases j with
  | ofNat k =>
    have h := e_two 0 k
    calc 2 * Int.ofNat (QBinom.Gj g36 (Int.ofNat k)) = Int.ofNat (2 * (2 * QBinom.Ta 0 k + QBinom.Tb 0 k)) := rfl
      _ = _ := by rw [h]; show 3 * ((Int.ofNat k - 0) * (Int.ofNat k - 0)) + (Int.ofNat k - 0) = 3 * (Int.ofNat k * Int.ofNat k) + Int.ofNat k; lib_int
  | negSucc k =>
    have h := e_two (k + 1) 0
    calc 2 * Int.ofNat (QBinom.Gj g36 (Int.negSucc k)) = Int.ofNat (2 * (2 * QBinom.Ta (k + 1) 0 + QBinom.Tb (k + 1) 0)) := rfl
      _ = _ := by
        rw [h]
        show 3 * ((0 - (Int.ofNat k + 1)) * (0 - (Int.ofNat k + 1))) + (0 - (Int.ofNat k + 1)) =
          3 * (-(Int.ofNat k + 1) * -(Int.ofNat k + 1)) + -(Int.ofNat k + 1)
        lib_int

theorem cond_int (j : Int) (i : Nat) : Int.ofNat (4 * (6 * QBinom.Gj g36 j + 3 * (i * i + i) + 1)) =
    (6 * j + 1) * (6 * j + 1) + 3 * ((2 * Int.ofNat i + 1) * (2 * Int.ofNat i + 1)) := by
  have e := e2G j
  show 4 * (6 * Int.ofNat (QBinom.Gj g36 j) + 3 * (Int.ofNat i * Int.ofNat i + Int.ofNat i) + 1) = _
  calc _ = 12 * (2 * Int.ofNat (QBinom.Gj g36 j)) + 12 * (Int.ofNat i * Int.ofNat i + Int.ofNat i) + 4 := by lib_int
    _ = 12 * (3 * (j * j) + j) + 12 * (Int.ofNat i * Int.ofNat i + Int.ofNat i) + 4 := by rw [e]
    _ = _ := by lib_int

theorem cond_iff (j : Int) (i n : Nat) : 6 * QBinom.Gj g36 j + 3 * (i * i + i) = n ↔
    (6 * j + 1) * (6 * j + 1) + 3 * ((2 * Int.ofNat i + 1) * (2 * Int.ofNat i + 1)) = 4 * Int.ofNat (n + 1) := by
  constructor
  · intro h; rw [← cond_int, h]; rfl
  · intro h
    rw [← cond_int] at h
    have h' : 4 * (6 * QBinom.Gj g36 j + 3 * (i * i + i) + 1) = 4 * (n + 1) := Int.ofNat.inj (show Int.ofNat _ = Int.ofNat (4 * (n + 1)) from h)
    exact Nat.succ.inj (Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 3) h')

theorem pos_eq (i i1 : Nat) (x : Int) (h1 : 2 * Int.ofNat i + 1 = x ∨ 2 * Int.ofNat i + 1 = -x)
    (h2 : 2 * Int.ofNat i1 + 1 = x ∨ 2 * Int.ofNat i1 + 1 = -x) : i = i1 := by
  have same : 2 * Int.ofNat i + 1 = 2 * Int.ofNat i1 + 1 → i = i1 := fun h =>
    Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1) (Nat.succ.inj (Int.ofNat.inj (show Int.ofNat (2 * i + 1) = Int.ofNat (2 * i1 + 1) from h)))
  have opp : 2 * Int.ofNat i + 1 = -(2 * Int.ofNat i1 + 1) → False := fun h => by
    have : Int.ofNat (2 * i + 1 + (2 * i1 + 1)) = Int.ofNat 0 := by
      show (2 * Int.ofNat i + 1) + (2 * Int.ofNat i1 + 1) = 0
      rw [h]; lib_int
    exact Nat.noConfusion (Int.ofNat.inj this)
  rcases h1 with h1 | h1 <;> rcases h2 with h2 | h2
  · exact same (h1.trans h2.symm)
  · exact absurd (h1.trans (by rw [h2]; lib_int)) opp
  · exact absurd (h1.trans (by rw [h2])) opp
  · exact same (h1.trans h2.symm)

theorem j_eq (j j1 : Int) (h : 6 * j + 1 = 6 * j1 + 1) : j = j1 :=
  HeckeInert.int_cancel' j j1 5 (by
    show j * 6 = j1 * 6
    calc j * 6 = (6 * j + 1) - 1 := by lib_int
      _ = (6 * j1 + 1) - 1 := by rw [h]
      _ = _ := by lib_int)

theorem Gj_ge (j : Int) : Int.natAbs j ≤ QBinom.Gj g36 j := by
  cases j with
  | ofNat k => exact Nat.le_trans (QBinom.ta_ge 0 k) (Nat.le_trans (Nat.le_add_left _ (QBinom.Ta 0 k)) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_add_right _ _)))
  | negSucc k => exact Nat.le_trans (QBinom.tb_ge (k + 1) 0) (Nat.le_add_left _ _)

section
variable (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h3 : 2 * m + 1 + 1 + 1 ≠ 3)
  (a b c : Int) (hab : a * a + 3 * (b * b) = Int.ofNat (2 * m + 1 + 1 + 1)) (hc : a - 1 = 3 * c)
  (r1 r2 : Int) (hr1 : a + b = 2 * r1 + 1) (hr2 : a - b = 2 * r2 + 1)
  (j1 j2 : Int) (i1 i2 : Nat) (hj1 : 6 * j1 + 1 = a - 3 * b) (hj2 : 6 * j2 + 1 = a + 3 * b)
  (hi1 : 2 * Int.ofNat i1 + 1 = a + b ∨ 2 * Int.ofNat i1 + 1 = -(a + b)) (hi2 : 2 * Int.ofNat i2 + 1 = a - b ∨ 2 * Int.ofNat i2 + 1 = -(a - b))
  (hne : j1 ≠ j2)
  (c1 : 6 * QBinom.Gj g36 j1 + 3 * (i1 * i1 + i1) = 2 * m + 1 + 1) (c2 : 6 * QBinom.Gj g36 j2 + 3 * (i2 * i2 + i2) = 2 * m + 1 + 1)

include hpr h3 hab hc hr1 hr2 hj1 hj2 hi1 hi2 hne c1 c2 in
theorem point (j : Int) (i : Nat) :
    Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs j)) (Z.mul (Frobenius.pw Z (Z.neg Z.one) i) (Z.mul (Cheb.emb Z (2 * i + 1))
      (delta Z (6 * QBinom.Gj g36 j + 3 * (i * i + i)) (2 * m + 1 + 1)))) =
    Z.add (Z.mul (delta Z j j1) (Z.mul (delta Z i i1) (a + b))) (Z.mul (delta Z j j2) (Z.mul (delta Z i i2) (a - b))) := by
  rw [pw_neg_one, pw_neg_one, emb_int, show Int.ofNat (2 * i + 1) = 2 * Int.ofNat i + 1 from rfl]
  by_cases hcnd : 6 * QBinom.Gj g36 j + 3 * (i * i + i) = 2 * m + 1 + 1
  · have hd : delta Z (6 * QBinom.Gj g36 j + 3 * (i * i + i)) (2 * m + 1 + 1) = Z.one := by unfold delta; rw [ite_pos' _ _ hcnd]
    have hI := (cond_iff j i _).mp hcnd
    rw [hd]
    rcases rep36 (2 * m + 1) hpr h3 a b hab c hc j i hI with ⟨hu, hv⟩ | ⟨hu, hv⟩
    · have hjj : j = j1 := j_eq j j1 (hu.trans hj1.symm)
      have hii : i = i1 := pos_eq i i1 (a + b) hv hi1
      have hw := weight j i (a + b) r1 hr1 ⟨-b, by rw [hu]; lib_int⟩ hv
      have hn2 : ¬ j = j2 := fun e => hne (hjj.symm.trans e)
      rw [show delta Z j j1 = Z.one by unfold delta; rw [ite_pos' _ _ hjj], show delta Z i i1 = Z.one by unfold delta; rw [ite_pos' _ _ hii],
        show delta Z j j2 = Z.zero by unfold delta; rw [ite_neg' _ _ hn2]]
      have e : sg2 (Int.natAbs j) * (sg2 i * ((2 * Int.ofNat i + 1) * 1)) = a + b := by
        rw [show (2 * Int.ofNat i + 1) * 1 = 2 * Int.ofNat i + 1 by lib_int]; exact hw
      show sg2 (Int.natAbs j) * (sg2 i * ((2 * Int.ofNat i + 1) * 1)) = 1 * (1 * (a + b)) + 0 * (delta Z i i2 * (a - b))
      rw [e]; lib_int
    · have hjj : j = j2 := j_eq j j2 (hu.trans hj2.symm)
      have hii : i = i2 := pos_eq i i2 (a - b) hv hi2
      have hw := weight j i (a - b) r2 hr2 ⟨b, by rw [hu]; lib_int⟩ hv
      have hn1 : ¬ j = j1 := fun e => hne (e.symm.trans hjj)
      rw [show delta Z j j2 = Z.one by unfold delta; rw [ite_pos' _ _ hjj], show delta Z i i2 = Z.one by unfold delta; rw [ite_pos' _ _ hii],
        show delta Z j j1 = Z.zero by unfold delta; rw [ite_neg' _ _ hn1]]
      have e : sg2 (Int.natAbs j) * (sg2 i * ((2 * Int.ofNat i + 1) * 1)) = a - b := by
        rw [show (2 * Int.ofNat i + 1) * 1 = 2 * Int.ofNat i + 1 by lib_int]; exact hw
      show sg2 (Int.natAbs j) * (sg2 i * ((2 * Int.ofNat i + 1) * 1)) = 0 * (delta Z i i1 * (a + b)) + 1 * (1 * (a - b))
      rw [e]; lib_int
  · have hd : delta Z (6 * QBinom.Gj g36 j + 3 * (i * i + i)) (2 * m + 1 + 1) = Z.zero := by unfold delta; rw [ite_neg' _ _ hcnd]
    rw [hd]
    have t1 : Z.mul (delta Z j j1) (Z.mul (delta Z i i1) (a + b)) = Z.zero := by
      by_cases hj : j = j1
      · by_cases hi : i = i1
        · exact absurd (by rw [hj, hi]; exact c1) hcnd
        · unfold delta; rw [ite_neg' _ _ hi]; lib_ring
      · unfold delta; rw [ite_neg' _ _ hj]; lib_ring
    have t2 : Z.mul (delta Z j j2) (Z.mul (delta Z i i2) (a - b)) = Z.zero := by
      by_cases hj : j = j2
      · by_cases hi : i = i2
        · exact absurd (by rw [hj, hi]; exact c2) hcnd
        · unfold delta; rw [ite_neg' _ _ hi]; lib_ring
      · unfold delta; rw [ite_neg' _ _ hj]; lib_ring
    rw [t1, t2]; lib_ring
end

theorem sum2 (N J : Nat) (j1 j2 : Int) (i1 i2 : Nat) (b1 : Int.natAbs j1 ≤ N) (b2 : Int.natAbs j2 ≤ N) (d1 : i1 < J) (d2 : i2 < J) (x1 x2 : Int) :
    sumK Z (fun j => sumK Z (fun i => Z.add (Z.mul (delta Z j j1) (Z.mul (delta Z i i1) x1)) (Z.mul (delta Z j j2) (Z.mul (delta Z i i2) x2))) (jl J))
      (HeckeMult.zl N) = Z.add x1 x2 := by
  have inner : ∀ j, sumK Z (fun i => Z.add (Z.mul (delta Z j j1) (Z.mul (delta Z i i1) x1)) (Z.mul (delta Z j j2) (Z.mul (delta Z i i2) x2))) (jl J) =
      Z.add (Z.mul (delta Z j j1) x1) (Z.mul (delta Z j j2) x2) := fun j => by
    rw [sumK_add, ← mul_sumK, ← mul_sumK, ← sumK_mul, ← sumK_mul, count_jl i1 J d1, count_jl i2 J d2]; lib_ring
  rw [sumK_ext Z _ _ inner, sumK_add, ← sumK_mul, ← sumK_mul, HeckeMult.count_zl Z j1 N b1, HeckeMult.count_zl Z j2 N b2]; lib_ring

/-- The coefficient of qⁿ in η(6z)⁴, computed in ℤ[q]/(q^(2n+1)). -/
def etaC36 (n : Nat) : Int :=
  Trunc.nth Z (2 * n + 1) ((Trunc.TR Z (2 * n + 1)).mul (Trunc.qe Z (2 * n + 1)) ((Trunc.TR Z (2 * n + 1)).mul
    ((Trunc.TR Z (2 * n + 1)).mul (QBinom.pp (Trunc.TR Z (2 * n + 1)) (Frobenius.pw (Trunc.TR Z (2 * n + 1)) (Trunc.qe Z (2 * n + 1)) 6) (2 * n + 1))
      (QBinom.pp (Trunc.TR Z (2 * n + 1)) (Frobenius.pw (Trunc.TR Z (2 * n + 1)) (Trunc.qe Z (2 * n + 1)) 6) (2 * n + 1)))
    ((Trunc.TR Z (2 * n + 1)).mul (QBinom.pp (Trunc.TR Z (2 * n + 1)) (Frobenius.pw (Trunc.TR Z (2 * n + 1)) (Trunc.qe Z (2 * n + 1)) 6) (2 * n + 1))
      (QBinom.pp (Trunc.TR Z (2 * n + 1)) (Frobenius.pw (Trunc.TR Z (2 * n + 1)) (Trunc.qe Z (2 * n + 1)) 6) (2 * n + 1))))) n

/-- **36a at every prime p ≡ 1 (mod 3)**: [q^p]η(6z)⁴ = −Σ_x χ(x³ + 1) = a_p(y² = x³ + 1). -/
theorem ap36_split (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (N : Nat) (hN : 2 * m + 1 + 1 = 3 * N) :
    etaC36 (2 * m + 1 + 1 + 1) = -CubicJacob.psi m (ZMod.ring (2 * m + 1 + 1)).one := by
  obtain ⟨a, b, hab, ⟨c, hc⟩, hS⟩ := CubicSign.cubic_exact m hpr N hN
  have h3 : 2 * m + 1 + 1 + 1 ≠ 3 := fun h => by
    have hm : 2 * m = 0 := Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h))
    rw [hm] at hN
    cases N with
    | zero => exact Nat.noConfusion hN
    | succ N => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (hN.trans (by lib_nat : 3 * (N + 1) = 3 * N + 1 + 1 + 1))))
  obtain ⟨r1, hr1⟩ := odd_ab m a b hab
  obtain ⟨r2, hr2'⟩ := odd_ab m a (-b) (by rw [show (-b) * (-b) = b * b by lib_int]; exact hab)
  have hr2 : a - b = 2 * r2 + 1 := by rw [show a - b = a + -b by lib_int]; exact hr2'
  obtain ⟨j1, hj1'⟩ := j_of a b c hc r1 hr1 1 (Or.inl rfl)
  obtain ⟨j2, hj2'⟩ := j_of a b c hc r1 hr1 (-1) (Or.inr rfl)
  have hj1 : 6 * j1 + 1 = a - 3 * b := hj1'.symm.trans (by lib_int)
  have hj2 : 6 * j2 + 1 = a + 3 * b := hj2'.symm.trans (by lib_int)
  obtain ⟨i1, hi1⟩ := i_of (a + b) r1 hr1
  obtain ⟨i2, hi2⟩ := i_of (a - b) r2 hr2
  have c1 : 6 * QBinom.Gj g36 j1 + 3 * (i1 * i1 + i1) = 2 * m + 1 + 1 := (cond_iff j1 i1 _).mpr (by
    rw [hj1]; rcases hi1 with h | h <;> rw [h] <;> (rw [← hab]; lib_int))
  have c2 : 6 * QBinom.Gj g36 j2 + 3 * (i2 * i2 + i2) = 2 * m + 1 + 1 := (cond_iff j2 i2 _).mpr (by
    rw [hj2]; rcases hi2 with h | h <;> rw [h] <;> (rw [← hab]; lib_int))
  have hne : j1 ≠ j2 := fun e => by
    have hb : b = 0 := HeckeInert.int_cancel' b 0 5 (by
      show b * 6 = 0 * 6
      calc b * 6 = (a + 3 * b) - (a - 3 * b) := by lib_int
        _ = (6 * j2 + 1) - (6 * j1 + 1) := by rw [hj1, hj2]
        _ = _ := by rw [e]; lib_int)
    rw [hb] at hab
    have hsq : Int.natAbs a * Int.natAbs a = 2 * m + 1 + 1 + 1 :=
      Int.ofNat.inj (show Int.ofNat (Int.natAbs a) * Int.ofNat (Int.natAbs a) = Int.ofNat (2 * m + 1 + 1 + 1) by
        rw [← sq_natAbs]; exact (show a * a = a * a + 3 * (0 * 0) by lib_int).trans hab)
    cases hpr (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
    | inl h1 => rw [h1] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq.symm |>.trans rfl |> fun e => (show 2 * m + 1 + 1 = 0 from e))
    | inr h1 =>
      rw [h1] at hsq
      have : 1 = 2 * m + 1 + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hsq.trans (Nat.mul_one _).symm).symm
      exact Nat.noConfusion (Nat.succ.inj this |>.symm |> fun e => (show 2 * m + 1 + 1 = 0 from e))
  have hle : ∀ (j : Int) (i : Nat), 6 * QBinom.Gj g36 j + 3 * (i * i + i) = 2 * m + 1 + 1 →
      Int.natAbs j ≤ 2 * (2 * (2 * m + 1 + 1 + 1) + 1) ∧ i < 2 * (2 * (2 * m + 1 + 1 + 1) + 1) + 2 := fun j i h => by
    have hG : QBinom.Gj g36 j ≤ 2 * m + 1 + 1 :=
      Nat.le_trans (Nat.le_add_right _ (5 * QBinom.Gj g36 j + 3 * (i * i + i))) (Nat.le_of_eq ((by lib_nat : QBinom.Gj g36 j + (5 * QBinom.Gj g36 j + 3 * (i * i + i)) = 6 * QBinom.Gj g36 j + 3 * (i * i + i)).trans h))
    have hI : i ≤ 2 * m + 1 + 1 :=
      Nat.le_trans (Nat.le_add_left i (i * i + 2 * (i * i + i) + 6 * QBinom.Gj g36 j)) (Nat.le_of_eq ((by lib_nat : i * i + 2 * (i * i + i) + 6 * QBinom.Gj g36 j + i = 6 * QBinom.Gj g36 j + 3 * (i * i + i)).trans h))
    exact ⟨Nat.le_trans (Gj_ge j) (Nat.le_trans hG (Nat.le_trans (Nat.le_add_right _ (6 * m + 12)) (Nat.le_of_eq (by lib_nat)))),
      Nat.lt_of_le_of_lt hI (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_trans (Nat.le_add_right _ (6 * m + 13)) (Nat.le_of_eq (by lib_nat))))⟩
  have hn : 2 * m + 1 + 1 + 1 < 2 * (2 * m + 1 + 1 + 1) + 1 :=
    Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_trans (Nat.le_add_right _ (2 * m + 3)) (Nat.le_of_eq (by lib_nat)))
  let F : Nat → Int := fun e => sumK Z (fun i => Z.mul (Frobenius.pw Z (Z.neg Z.one) i) (Z.mul (Cheb.emb Z (2 * i + 1))
    (delta Z (6 * e + 3 * (i * i + i)) (2 * m + 1 + 1)))) (jl (2 * (2 * (2 * m + 1 + 1 + 1) + 1) + 2))
  calc etaC36 (2 * m + 1 + 1 + 1)
      = sumK Z (fun m' => Z.mul (F (g36 (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) m')) (Frobenius.pw Z (Z.neg Z.one) m'))
          (jl (2 * (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) + 1)) := coef36 _ (2 * m + 1 + 1) hn
    _ = QBinom.es Z (fun m' => F (g36 (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) m')) (Z.neg Z.one) (2 * (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) + 1) :=
          (es_jl _ _).symm
    _ = Z.mul (Frobenius.pw Z (Z.neg Z.one) (2 * (2 * (2 * m + 1 + 1 + 1) + 1))) (QBinom.Bg Z F g36 (2 * (2 * (2 * m + 1 + 1 + 1) + 1))) :=
          QBinom.gzg Z F g36 g36_shift _
    _ = QBinom.Bg Z F g36 (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) := by rw [QBinom.pm_even]; lib_ring
    _ = sumK Z (fun j => sumK Z (fun i => Z.add (Z.mul (delta Z j j1) (Z.mul (delta Z i i1) (a + b)))
          (Z.mul (delta Z j j2) (Z.mul (delta Z i i2) (a - b)))) (jl (2 * (2 * (2 * m + 1 + 1 + 1) + 1) + 2))) (HeckeMult.zl (2 * (2 * (2 * m + 1 + 1 + 1) + 1))) := by
          apply sumK_ext; intro j
          show Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs j)) (F (QBinom.Gj g36 j)) = _
          rw [mul_sumK]
          exact sumK_ext Z _ _ (fun i => point m hpr h3 a b c hab hc r1 r2 hr1 hr2 j1 j2 i1 i2 hj1 hj2 hi1 hi2 hne c1 c2 j i) _
    _ = Z.add (a + b) (a - b) := sum2 _ _ j1 j2 i1 i2 (hle j1 i1 c1).1 (hle j2 i2 c2).1 (hle j1 i1 c1).2 (hle j2 i2 c2).2 _ _
    _ = _ := by rw [show -CubicJacob.psi m (ZMod.ring (2 * m + 1 + 1)).one = 2 * a from hS]; show (a + b) + (a - b) = 2 * a; lib_int

end Hecke36

#print axioms Hecke36.g36_shift
#print axioms Hecke36.e2G
#print axioms Hecke36.cond_int
#print axioms Hecke36.cond_iff
#print axioms Hecke36.pos_eq
#print axioms Hecke36.j_eq
#print axioms Hecke36.Gj_ge
#print axioms Hecke36.point
#print axioms Hecke36.sum2
#print axioms Hecke36.ap36_split

#print axioms: 10 theorems, no axioms.

78.159 36a at inert primes

At \(p\equiv 2\pmod 3\) no exponent \(6E+3(i^2+i)\) equals \(p-1\), and \(x\mapsto x^3\), \(y\mapsto y+1\) reduce \(\sum \chi (x^3+1)\) to \(\sum \chi =0\).

\[ p\equiv 2\ (3):\ [q^p]\,\eta (6z)^4=0=-\sum _{x\in \mathbb F_p}\chi (x^3+1). \]

lang/Ap36i.lean

/-
  lang/Ap36i — T47, cycle 579: **36a at every inert prime p ≡ 2 (mod 3)**: [q^p]η(6z)⁴ = 0 = −Σ_x χ(x³ + 1) = a_p.

    no3            3a ≠ 3k + 1 in ℕ
    etaC36_inert   no (m, i) has 6E(m) + 3(i² + i) = p − 1 ≡ 1 (mod 3), so the coefficient vanishes
    psi_inert      Σ_x χ(x³ + 1) = 0 in ℤ (x ↦ x³ is a bijection, then y ↦ y + 1, then Σχ = 0)
    ap36_inert
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Ap36
import lang.CMEisen

namespace Hecke36

open DetChain YMChain FiniteRecChain Hecke32 FTAChain

theorem no3 : ∀ k a, 3 * a ≠ 3 * k + 1
  | 0, 0, h => Nat.noConfusion h
  | 0, a + 1, h => Nat.noConfusion (Nat.succ.inj ((show 3 * a + 2 + 1 = 3 * (a + 1) by lib_nat).trans h))
  | _ + 1, 0, h => Nat.noConfusion h.symm
  | k + 1, a + 1, h => no3 k a (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj
      ((show 3 * a + 1 + 1 + 1 = 3 * (a + 1) by lib_nat).trans (h.trans (by lib_nat))))))

theorem etaC36_inert (m c : Nat) (hc : 2 * m + 1 + 1 + 1 + 1 = 3 * (c + 1)) : etaC36 (2 * m + 1 + 1 + 1) = 0 := by
  have hn : 2 * m + 1 + 1 + 1 < 2 * (2 * m + 1 + 1 + 1) + 1 :=
    Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_trans (Nat.le_add_right _ (2 * m + 3)) (Nat.le_of_eq (by lib_nat)))
  show Trunc.nth Z (2 * (2 * m + 1 + 1 + 1) + 1) _ (2 * m + 1 + 1 + 1) = 0
  rw [coef36 _ (2 * m + 1 + 1) hn]
  have hz : ∀ (mm i : Nat), delta Z (6 * (2 * QBinom.Ta (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) mm + QBinom.Tb (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) mm) + 3 * (i * i + i))
      (2 * m + 1 + 1) = Z.zero := fun mm i => by
    unfold delta
    apply ite_neg'
    intro h
    apply no3 c (2 * (2 * QBinom.Ta (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) mm + QBinom.Tb (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) mm) + (i * i + i))
    have h2 : 2 * m + 1 + 1 + 1 + 1 = 3 * c + 3 := hc.trans (by lib_nat)
    have h4 : 2 * m + 1 + 1 = 3 * c + 1 := Nat.succ.inj (Nat.succ.inj h2)
    exact ((by lib_nat : 3 * (2 * (2 * QBinom.Ta (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) mm + QBinom.Tb (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) mm) + (i * i + i)) =
      6 * (2 * QBinom.Ta (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) mm + QBinom.Tb (2 * (2 * (2 * m + 1 + 1 + 1) + 1)) mm) + 3 * (i * i + i))).trans (h.trans h4)
  rw [sumK_ext Z _ (fun _ => Z.zero) (fun mm => by
    rw [sumK_ext Z _ (fun _ => Z.zero) (fun i => by rw [hz]; lib_ring), HeckePrime.zsumR Z]; lib_ring), HeckePrime.zsumR Z]
  rfl

theorem psi_inert (m c : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (hc : 2 * m + 1 + 1 + 1 + 1 = 3 * (c + 1)) : CubicJacob.psi m (ZMod.ring (2 * m + 1 + 1)).one = 0 := by
  have he : 3 * (2 * c + 1) = 2 * (2 * m + 1 + 1) + 1 := by
    have : 2 * (2 * m + 1 + 1 + 1 + 1) = 2 * (3 * (c + 1)) := by rw [hc]
    exact Nat.succ.inj (Nat.succ.inj (Nat.succ.inj ((by lib_nat : 3 * (2 * c + 1) + 1 + 1 + 1 = 2 * (3 * (c + 1))).trans (this.symm.trans (by lib_nat)))))
  let K := ZMod.ring (2 * m + 1 + 1)
  have hc' := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  have r1 := reindex LibInt.intR (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)) hc' (fun x => Frobenius.pw K x 3) (fun x => Frobenius.pw K x (2 * c + 1))
    (CMEisen.cube_inv (2 * m + 1) hpr (2 * c + 1) he) (CMEisen.inv_cube (2 * m + 1) hpr (2 * c + 1) he) (fun y => QChar.chi m (K.add y K.one))
  have r2 := reindex LibInt.intR (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)) hc' (fun x => K.add x K.one) (fun x => K.add x (K.neg K.one))
    (fun x => by show K.add (K.add x K.one) (K.neg K.one) = x; lib_ring) (fun y => by show K.add (K.add y (K.neg K.one)) K.one = y; lib_ring) (QChar.chi m)
  show sumK JacobMoment.Z (fun x => QChar.chi m (K.add (CubicJacob.cube m x) K.one)) (ConicSum.L m) = 0
  have ec : ∀ x, CubicJacob.cube m x = Frobenius.pw K x 3 := fun x => by
    show K.mul x (K.mul x x) = K.mul (K.mul (K.mul K.one x) x) x; lib_ring
  rw [sumK_ext _ _ (fun x => QChar.chi m (K.add (Frobenius.pw K x 3) K.one)) (fun x => by rw [ec])]
  show sumK LibInt.intR (fun x => (fun y => QChar.chi m (K.add y K.one)) (Frobenius.pw K x 3)) (CMInert.allZ (2 * m + 1 + 1) (2 * m + 1 + 1 + 1)) = 0
  rw [r1, r2]
  exact QChar.chi_sum m hpr

/-- **36a at every inert prime**: [q^p]η(6z)⁴ = −Σ_x χ(x³ + 1) (both 0). -/
theorem ap36_inert (m c : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (hc : 2 * m + 1 + 1 + 1 + 1 = 3 * (c + 1)) :
    etaC36 (2 * m + 1 + 1 + 1) = -CubicJacob.psi m (ZMod.ring (2 * m + 1 + 1)).one := by
  rw [etaC36_inert m c hc, psi_inert m c hpr hc]; rfl

end Hecke36

#print axioms Hecke36.no3
#print axioms Hecke36.etaC36_inert
#print axioms Hecke36.psi_inert
#print axioms Hecke36.ap36_inert

#print axioms: 4 theorems, no axioms.

78.160 Euler’s criterion for \(n\)-th powers

For every prime \(p\) and every \(n\mid p-1\), with a primitive root and discrete logarithms.

\[ a\not \equiv 0:\ \exists x\ x^n\equiv a\pmod p\iff a^{(p-1)/n}\equiv 1\pmod p. \]

lang/NthPow.lean

/-
  lang/NthPow — T49, cycle 581: **n-th power residues mod p for every n | p − 1** (Euler's criterion in general).
  p = J + 2 prime, p − 1 = n·k:
    nth_crit      a ≠ 0:  a is an n-th power  ⇔  a^k = 1
    sym_root      the n-th power residue symbol a^k is an n-th root of unity: (a^k)^n = 1
    sym_mul       (ab)^k = a^k b^k
  (The general statement behind the quadratic, cubic and quartic characters used so far.)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.DLog

namespace NthPow

open FTAChain ZMod OrdMul

variable (J : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)

theorem pos_of_mul (n k : Nat) (h : J + 1 = n * k) : (∃ n', n = n' + 1) ∧ (∃ k', k = k' + 1) := by
  constructor
  · cases n with
    | zero => exact absurd (h.trans (Nat.zero_mul k)) (fun e => Nat.noConfusion e)
    | succ n' => exact ⟨n', rfl⟩
  · cases k with
    | zero => exact absurd (h.trans (Nat.mul_zero n)) (fun e => Nat.noConfusion e)
    | succ k' => exact ⟨k', rfl⟩

include hpr in
/-- **Euler's criterion for n-th powers.** -/
theorem nth_crit (n k : Nat) (hnk : J + 1 = n * k) (a : Zp (J + 1)) (ha : a ≠ (ring (J + 1)).zero) :
    (∃ x, Frobenius.pw (ring (J + 1)) x n = a) ↔ Frobenius.pw (ring (J + 1)) a k = (ring (J + 1)).one := by
  obtain ⟨⟨n', hn'⟩, ⟨k', hk'⟩⟩ := pos_of_mul J n k hnk
  constructor
  · intro ⟨x, hx⟩
    have hx0 : x ≠ (ring (J + 1)).zero := fun e => by
      apply ha; rw [← hx, e, hn']
      show (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) _ n') (ring (J + 1)).zero = _
      exact DetChain.mul_zero _ _
    rw [← hx, TwoPower.pwpow, ← hnk]
    exact unit_pow J hpr x hx0
  · intro hak
    obtain ⟨g, hg⟩ := PrimRoot.primitive_root J hpr
    obtain ⟨s, _, hs⟩ := DLog.mem_pows J g a (J + 1) (DLog.every_unit J hpr g hg a ha)
    have h1 : Frobenius.pw (ring (J + 1)) g (s * k) = (ring (J + 1)).one := by rw [← TwoPower.pwpow, ← hs]; exact hak
    obtain ⟨c, hc⟩ := hg.2 _ h1
    -- s·k = (n·k)·c ⇒ s = n·c
    have hsc : s = n * c := by
      rw [hnk] at hc
      apply Nat.eq_of_mul_eq_mul_left (show 0 < k by rw [hk']; exact Nat.zero_lt_succ _)
      exact (show k * s = s * k by lib_nat).trans (hc.trans (by lib_nat))
    refine ⟨Frobenius.pw (ring (J + 1)) g c, ?_⟩
    rw [TwoPower.pwpow, hs, hsc, Nat.mul_comm]

include hpr in
theorem sym_root (n k : Nat) (hnk : J + 1 = n * k) (a : Zp (J + 1)) (ha : a ≠ (ring (J + 1)).zero) :
    Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) a k) n = (ring (J + 1)).one := by
  rw [TwoPower.pwpow, Nat.mul_comm, ← hnk]; exact unit_pow J hpr a ha

theorem sym_mul (k : Nat) (a b : Zp (J + 1)) :
    Frobenius.pw (ring (J + 1)) ((ring (J + 1)).mul a b) k = (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) a k) (Frobenius.pw (ring (J + 1)) b k) :=
  Frobenius.pw_mul _ a b k

end NthPow

#print axioms NthPow.pos_of_mul
#print axioms NthPow.nth_crit
#print axioms NthPow.sym_root
#print axioms NthPow.sym_mul

#print axioms: 4 theorems, no axioms.

78.161 The number of \(n\)-th powers

Counting through a primitive root \(g\): \(g^s\) satisfies \((g^s)^{(p-1)/n}=1\) exactly when \(n\mid s\).

\[ p-1=nk:\quad \#\{a\in \mathbb F_p:\ a^k=1\}=k. \]

lang/NthCount.lean

/-
  lang/NthCount — T49, cycle 582: **exactly (p − 1)/n nonzero n-th powers mod p** (n | p − 1): with p − 1 = n·k,
    #{a ∈ 𝔽_p : a^k = 1} = k,
  i.e. (NthPow.nth_crit) the n-th powers form a subgroup of index n, the kernel of the n-th power residue symbol.

    rs, rs_add, rs_one       finite sums Σ_{s<L} h(s) and their splitting
    pows_rs                  Σ over DLog.pows is a range sum of g^s
    block                    a^k = 1 for a = g^(nt + r), r < n  ⇔  r = 0
    count_nth
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.NthPow
import lang.LucMul
import lang.ChebAll

namespace NthPow

open FTAChain ZMod OrdMul YMChain FiniteRecChain

def rs (h : Nat → Int) : Nat → Int
  | 0 => 0
  | L + 1 => h L + rs h L

theorem rs_add (h : Nat → Int) (a : Nat) : ∀ b, rs h (a + b) = rs (fun s => h (a + s)) b + rs h a
  | 0 => by show rs h a = 0 + rs h a; lib_int
  | b + 1 => by show h (a + b) + rs h (a + b) = (h (a + b) + rs (fun s => h (a + s)) b) + rs h a; rw [rs_add h a b]; lib_int

theorem rs_one : ∀ n, rs (fun r => if r = 0 then 1 else 0) (n + 1) = 1
  | 0 => rfl
  | n + 1 => by
    show (if n + 1 = 0 then (1 : Int) else 0) + rs (fun r => if r = 0 then 1 else 0) (n + 1) = 1
    rw [rs_one n, ite_neg' _ _ (fun e => Nat.noConfusion e)]; lib_int

variable (J : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)

def F1 (k : Nat) (a : Zp (J + 1)) : Int := if Frobenius.pw (ring (J + 1)) a k = (ring (J + 1)).one then 1 else 0

theorem pows_rs (f : Zp (J + 1) → Int) (g : Zp (J + 1)) : ∀ n, sumK LibInt.intR f (DLog.pows J g n) = rs (fun s => f (Frobenius.pw (ring (J + 1)) g s)) n
  | 0 => rfl
  | n + 1 => by
    show f (Frobenius.pw (ring (J + 1)) g n) + sumK LibInt.intR f (DLog.pows J g n) = _
    rw [pows_rs f g n]; rfl

theorem block (n k : Nat) (hnk : J + 1 = n * k) (g : Zp (J + 1)) (hg : Ord (ring (J + 1)) g (J + 1)) (t r : Nat) (hr : r < n) :
    F1 J k (Frobenius.pw (ring (J + 1)) g (n * t + r)) = if r = 0 then 1 else 0 := by
  obtain ⟨⟨n', hn'⟩, ⟨k', hk'⟩⟩ := pos_of_mul J n k hnk
  have e : Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) g (n * t + r)) k = Frobenius.pw (ring (J + 1)) g (r * k) := by
    have one_pw : ∀ t, Frobenius.pw (ring (J + 1)) (ring (J + 1)).one t = (ring (J + 1)).one := by
      intro t; induction t with
      | zero => rfl
      | succ t ih => show (ring (J + 1)).mul (Frobenius.pw (ring (J + 1)) (ring (J + 1)).one t) (ring (J + 1)).one = _; rw [ih]; exact (ring (J + 1)).one_mul _
    rw [TwoPower.pwpow, show (n * t + r) * k = (n * k) * t + r * k by lib_nat, HasseCM.pw_add', ← TwoPower.pwpow, ← hnk, hg.1, one_pw]
    exact (ring (J + 1)).one_mul _
  unfold F1
  rw [e]
  cases r with
  | zero => rw [ite_pos' _ _ (show Frobenius.pw (ring (J + 1)) g (0 * k) = (ring (J + 1)).one by rw [Nat.zero_mul]; rfl), ite_pos' _ _ rfl]
  | succ r =>
    rw [ite_neg' _ _ (fun e => Nat.noConfusion e)]
    apply ite_neg'
    intro h1
    obtain ⟨c, hc⟩ := hg.2 _ h1
    obtain ⟨d, hd⟩ := Cheb.le_dest2 (r + 1 + 1) n hr
    rw [hnk] at hc
    cases c with
    | zero =>
      have h00 : (r + 1) * k = 0 := hc.trans (Nat.mul_zero _)
      rw [hk'] at h00
      exact Nat.noConfusion ((show (r + 1) * (k' + 1) = (r * (k' + 1) + k') + 1 by lib_nat).symm.trans h00)
    | succ c =>
      have this : (r + 1) * k + 0 = (r + 1) * k + ((d + 1) * k + n * k * c) := by
        calc (r + 1) * k + 0 = n * k * (c + 1) := by rw [Nat.add_zero, hc]
          _ = _ := by rw [← hd]; lib_nat
      have h0 : 0 = (d + 1) * k + n * k * c := Cheb.add_rc ((r + 1) * k) 0 _ ((Nat.add_comm 0 _).trans (this.trans (Nat.add_comm _ _)))
      rw [hk'] at h0
      exact Nat.noConfusion (h0.trans (by lib_nat : (d + 1) * (k' + 1) + n * (k' + 1) * c = (d * (k' + 1) + k' + n * (k' + 1) * c) + 1))

theorem rs_blocks (n : Nat) (h : Nat → Int) (hb : ∀ t r, r < n + 1 → h ((n + 1) * t + r) = if r = 0 then 1 else 0) :
    ∀ t, rs h ((n + 1) * t) = Int.ofNat t
  | 0 => rfl
  | t + 1 => by
    rw [show (n + 1) * (t + 1) = (n + 1) * t + (n + 1) by lib_nat, rs_add, rs_blocks n h hb t]
    have : rs (fun s => h ((n + 1) * t + s)) (n + 1) = rs (fun r => if r = 0 then 1 else 0) (n + 1) := by
      have hh : ∀ L, L ≤ n + 1 → rs (fun s => h ((n + 1) * t + s)) L = rs (fun r => if r = 0 then 1 else 0) L := by
        intro L; induction L with
        | zero => intro _; rfl
        | succ L ih =>
          intro hL
          show h ((n + 1) * t + L) + rs _ L = (if L = 0 then (1 : Int) else 0) + rs _ L
          rw [hb t L hL, ih (Nat.le_of_succ_le hL)]
      exact hh (n + 1) (Nat.le_refl _)
    rw [this, rs_one]; show 1 + Int.ofNat t = Int.ofNat t + 1; lib_int

include hpr in
/-- **#{a : a^k = 1} = k** for p − 1 = n·k. -/
theorem count_nth (n k : Nat) (hnk : J + 1 = n * k) :
    sumK LibInt.intR (F1 J k) (CMInert.allZ (J + 1) (J + 1 + 1)) = Int.ofNat k := by
  obtain ⟨⟨n', hn'⟩, ⟨k', hk'⟩⟩ := pos_of_mul J n k hnk
  obtain ⟨g, hg⟩ := PrimRoot.primitive_root J hpr
  rw [DLog.sum_split J LibInt.intR hpr g hg, pows_rs]
  have h0 : F1 J k (ring (J + 1)).zero = 0 := by
    unfold F1; apply ite_neg'; rw [hk', HasseCM.pw_zero_succ]; exact fun e => PowerSum.one_ne_zero' J e.symm
  subst hn'
  rw [h0, congrArg (rs (fun s => F1 J k (Frobenius.pw (ring (J + 1)) g s))) hnk,
    rs_blocks n' _ (fun t r hr => block J (n' + 1) k hnk g hg t r hr) k]
  show 0 + Int.ofNat k = Int.ofNat k; lib_int

end NthPow

#print axioms NthPow.rs_add
#print axioms NthPow.rs_one
#print axioms NthPow.pows_rs
#print axioms NthPow.block
#print axioms NthPow.rs_blocks
#print axioms NthPow.count_nth

#print axioms: 6 theorems, no axioms.

78.162 The Frobenius of a Gauss sum of any order

In \(E[C_p]\) with \(E\) of prime characteristic \(q\) and \(\psi ^q=\psi \), Frobenius and the substitution \(a\mapsto q^{-1}a\) give the reciprocity step for every order.

\[ G_\psi =\sum _a\psi (a)\zeta ^a:\qquad G_\psi ^{\,q}=\psi (q^{-1})\,G_\psi . \]

lang/GaussFrobN.lean

/-
  lang/GaussFrobN — T49, cycle 602: **the Frobenius of a Gauss sum for an arbitrary multiplicative character** (any order n):
  E a commutative ring of prime characteristic q, ψ : 𝔽_p → E multiplicative with ψ^q = ψ (e.g. values in μ_n ⊂ E, q ≡ 1 mod n),
  G_ψ = Σ_a ψ(a) ζ^a ∈ E[C_p]:
    G_ψ^q = ψ(q⁻¹) G_ψ          (Frobenius + reindex a ↦ q⁻¹a)
  The common first step of the cubic and quartic reciprocity proofs (CubicRecipSplit, QuartRecip; GaussFrob for the quadratic character), now for every n.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GroupRingFrob

namespace GroupRingFrob

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing Frobenius

variable {S : Type} (E : CRing S) (j : Nat)

def gauss (ψ : Zp (j + 1) → S) : V S (j + 1 + 1) := sumK (gring E (j + 1)) (fun a => mono E (j + 1) a (ψ a)) (L (j + 1))

theorem gauss_frob (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (k : Nat) (hq : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1)
    (hE : nsm E (k + 1 + 1) E.one = E.zero) (ψ : Zp (j + 1) → S)
    (hmul : ∀ a b, ψ ((ring (j + 1)).mul a b) = E.mul (ψ a) (ψ b)) (hfix : ∀ a, pw E (ψ a) (k + 1 + 1) = ψ a)
    (hQ : nsm (ring (j + 1)) (k + 1 + 1) (ring (j + 1)).one ≠ (ring (j + 1)).zero) :
    pw (gring E (j + 1)) (gauss E j ψ) (k + 1 + 1) =
      gmul E (j + 1) (mono E (j + 1) (ring (j + 1)).zero (ψ (pw (ring (j + 1)) (nsm (ring (j + 1)) (k + 1 + 1) (ring (j + 1)).one) j))) (gauss E j ψ) := by
  generalize hQd : nsm (ring (j + 1)) (k + 1 + 1) (ring (j + 1)).one = Q at hQ
  have hinv := inv_exists j hpr Q hQ
  generalize hQi : pw (ring (j + 1)) Q j = Qi at hinv
  -- LHS = Σ_a mono (Q a) (ψ a)
  have hL : pw (gring E (j + 1)) (gauss E j ψ) (k + 1 + 1) = sumK (gring E (j + 1)) (fun a => mono E (j + 1) ((ring (j + 1)).mul Q a) (ψ a)) (L (j + 1)) := by
    unfold gauss
    rw [frob_sum E (j + 1) k hq hE]
    apply sumK_ext; intro a
    rw [mono_pow, hfix, nsm_eq_mul, hQd]
  -- reindex a = Qi b
  have hc := CMInert.complete_allZ (j + 1) (gring E (j + 1))
  have hre := reindex (gring E (j + 1)) (L (j + 1)) hc (fun b => (ring (j + 1)).mul Qi b) (fun a => (ring (j + 1)).mul Q a)
    (fun b => by show (ring (j + 1)).mul Q ((ring (j + 1)).mul Qi b) = b
                 calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) b := by lib_ring
                   _ = b := by rw [hinv]; lib_ring)
    (fun a => by show (ring (j + 1)).mul Qi ((ring (j + 1)).mul Q a) = a
                 calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) a := by lib_ring
                   _ = a := by rw [hinv]; lib_ring)
    (fun a => mono E (j + 1) ((ring (j + 1)).mul Q a) (ψ a))
  rw [hL, ← hre]
  unfold gauss
  show _ = (gring E (j + 1)).mul (mono E (j + 1) (ring (j + 1)).zero (ψ Qi)) (sumK (gring E (j + 1)) (fun a => mono E (j + 1) a (ψ a)) (L (j + 1)))
  rw [mul_sumK]
  apply sumK_ext; intro b
  show _ = gmul E (j + 1) (mono E (j + 1) (ring (j + 1)).zero (ψ Qi)) (mono E (j + 1) b (ψ b))
  rw [mono_mul, ← hmul]
  have e1 : (ring (j + 1)).mul Q ((ring (j + 1)).mul Qi b) = (ring (j + 1)).add (ring (j + 1)).zero b := by
    calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) b := by lib_ring
      _ = _ := by rw [hinv]; lib_ring
  rw [e1]

end GroupRingFrob

#print axioms GroupRingFrob.gauss_frob

#print axioms: 1 theorem, no axioms.

78.163 The norm of a Gauss sum of any order

For a multiplicative \(\psi \) with \(\sum \psi =0\) and \(\bar \psi =\psi \circ \iota \), coefficientwise in \(E[C_p]\).

\[ (G_\psi G_{\bar \psi })(c)=\bar \psi (-1)\,(p\,\delta _{c,0}-1). \]

lang/GaussNormN.lean

/-
  lang/GaussNormN — T49, cycle 603: **the norm of a Gauss sum of any order**, coefficientwise in E[C_p]:
  for multiplicative ψ : 𝔽_p → E with ψ(1) = 1, ψ(0) = 0, Σψ = 0, and ψ' = ψ ∘ ι (ι(x) = x^(p−2), the inverse),
    (G_ψ · G_ψ')(c) = ψ'(−1) · (p·δ_(c,0) − 1),   i.e.  G_ψ G_ψ̄ = ψ̄(−1)·(p ζ⁰ − N).
  (CharSumGen.conv_D, D_ne, D_zero; the cubic and quartic cases are JacobiNorm.gg and its quartic analogue.)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CharSumGen

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

theorem gauss_norm (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (c : Zp (j + 1 + 1)) :
    gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j ψ) (Gv j (fun x => ψ (iv j x)))) c =
      E.mul (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) (gt E (j + 1 + 1) (Xv E j) c) := by
  have hm' : ∀ x y, (fun x => ψ (iv j x)) ((ring (j + 1 + 1)).mul x y) = E.mul ((fun x => ψ (iv j x)) x) ((fun x => ψ (iv j x)) y) :=
    fun x y => by show ψ (iv j ((ring (j + 1 + 1)).mul x y)) = _; rw [iv_mulmul, hm]
  rw [conv_D E j ψ _ hm' c, gt_X]
  by_cases hc : c = (ring (j + 1 + 1)).zero
  · rw [hc]
    have e : sumK E (fun b => E.mul (ψ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero b)) (ψ (iv j b))) (LL j) =
        sumK E (fun b => E.mul (ψ b) (ψ (iv j b))) (LL j) := sumK_ext E _ _ (fun b => by rw [(ring (j + 1 + 1)).zero_add]) _
    show E.mul _ (sumK E (fun b => E.mul (ψ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero b)) (ψ (iv j b))) (LL j)) = _
    rw [e, D_zero E j hpr ψ hm h1 h0]
    unfold delta; rw [ite_pos' _ _ rfl]; lib_ring
  · show E.mul _ (sumK E (fun b => E.mul (ψ ((ring (j + 1 + 1)).add c b)) (ψ (iv j b))) (LL j)) = _
    rw [D_ne E j hpr ψ hm h1 h0 hs c hc]
    unfold delta; rw [ite_neg' _ _ hc]; lib_ring

end CharSumGen

#print axioms CharSumGen.gauss_norm

#print axioms: 1 theorem, no axioms.

78.164 Gauss sums and Jacobi sums of any order

At \(c\ne 0\) the substitution \(a=ct\) gives \((\chi \lambda )(c)J(\chi ,\lambda )\); at \(c=0\) the sum is \(\lambda (-1)\sum \chi \lambda =0\).

\[ (G_\chi G_\lambda )(c)=J(\chi ,\lambda )\,\chi (c)\lambda (c). \]

lang/JacobiGauss.lean

/-
  lang/JacobiGauss — T49, cycle 604: **G_χ G_λ = J(χ, λ) G_χλ** for multiplicative χ, λ : 𝔽_p → E with χλ nontrivial
  (Σ_a χ(a)λ(a) = 0) and χ(0) = 0 (coefficientwise in E[C_p]); J(χ, λ) = Σ_t χ(t) λ(1 − t).
  Iterated with GaussNormN this expresses G_ψ^n through the Jacobi sums J(ψ, ψ^k): the order-n part of n-th power reciprocity.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GaussNormN

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

def J2 (χ μ : Zp (j + 1 + 1) → S) : S :=
  sumK E (fun t => E.mul (χ t) (μ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))) (LL j)

theorem jacobi_gauss (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (χ μ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, χ ((ring (j + 1 + 1)).mul x y) = E.mul (χ x) (χ y)) (hm' : ∀ x y, μ ((ring (j + 1 + 1)).mul x y) = E.mul (μ x) (μ y))
    (h0 : χ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E (fun a => E.mul (χ a) (μ a)) (LL j) = E.zero) (c : Zp (j + 1 + 1)) :
    gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j χ) (Gv j μ)) c = E.mul (J2 E j χ μ) (E.mul (χ c) (μ c)) := by
  rw [gt_mul]
  have e1 : sumK E (fun a => E.mul (gt E (j + 1 + 1) (Gv j χ) a) (gt E (j + 1 + 1) (Gv j μ) ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a)))) (GroupRing.L (j + 1 + 1))
      = sumK E (fun a => E.mul (χ a) (μ ((ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg a)))) (LL j) :=
    sumK_ext E _ _ (fun a => by unfold Gv; rw [gt_tb, gt_tb]) _
  rw [e1]
  by_cases hc : c = (ring (j + 1 + 1)).zero
  · rw [hc, h0]
    have e2 : ∀ a, E.mul (χ a) (μ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero ((ring (j + 1 + 1)).neg a))) =
        E.mul (μ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (E.mul (χ a) (μ a)) := fun a => by
      rw [show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero ((ring (j + 1 + 1)).neg a) = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) a by lib_ring, hm']
      lib_ring
    rw [sumK_ext E _ _ e2, ← mul_sumK, hs]; lib_ring
  · rw [← sum_scale E j hpr _ c hc]
    unfold J2; rw [sumK_mul]
    apply sumK_ext; intro t
    rw [show (ring (j + 1 + 1)).add c ((ring (j + 1 + 1)).neg ((ring (j + 1 + 1)).mul c t)) =
      (ring (j + 1 + 1)).mul c ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)) by lib_ring, hm, hm']
    lib_ring

end CharSumGen

#print axioms CharSumGen.jacobi_gauss

#print axioms: 1 theorem, no axioms.

78.165 The \(n\)-th power residue criterion for every \(n\)

\(G_\psi ^n=A(p\zeta ^0-N)\) with \(A=\prod _{i\le n-2}J(\psi ^i,\psi )\,\bar \psi (-1)\), \((p\zeta ^0-N)G_\psi =pG_\psi \), and Frobenius \(G_\psi ^q=\psi (q^{-1})G_\psi \).

\[ q=nm+1:\quad \psi (q^{-1})=(A\,p)^m\ \text {in } E,\ \operatorname {char}E=q. \]

lang/NthRecip.lean

/-
  lang/NthRecip — T49, cycle 607: **the n-th power residue criterion for every n** (Gauss sums in E[C_p]):
  p = j + 3 prime, ψ : 𝔽_p → E multiplicative of order n = k + 2 (ψ(1) = 1, ψ(0) = 0, Σψ^i = 0 for 1 ≤ i ≤ n − 1, ψ^(k+1) = ψ∘ι),
  q prime with q = n·m + 1, E of characteristic q, ψ^q = ψ. Then
    ψ(q⁻¹) = (A·p)^m,   A = (∏_{i≤k} J(ψ^i, ψ))·ψ(ι(−1)),
  so q is an n-th power residue mod p (ψ(q) = 1 for a faithful ψ) iff (A·p)^((q−1)/n) = 1 in E — the cubic and quartic criteria
  (ResidueCriterion) for an arbitrary order n.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GaussCritN

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

theorem mono0_one : mono E (j + 1 + 1) (ring (j + 1 + 1)).zero E.one = gone E (j + 1 + 1) :=
  ext E (j + 1 + 1) _ _ (fun c => by rw [gt_mono]; unfold gone; rw [gt_tb]; exact E.one_mul _)

theorem mono0_mul (s t : S) : gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero s) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero t) =
    mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul s t) := by
  rw [mono_mul, show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero (ring (j + 1 + 1)).zero = (ring (j + 1 + 1)).zero by lib_ring]

/-- (mono0 A · X)^t · G = mono0 ((A p)^t) · G when X G = mono0 p · G. -/
theorem powXG (A : S) (G : V S (j + 1 + 1 + 1))
    (hXG : gmul E (j + 1 + 1) (Xv E j) G = gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)) G) : ∀ t,
    (gring E (j + 1 + 1)).mul (pw (gring E (j + 1 + 1)) ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) t) G =
      (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (pw E (E.mul A (Pp E j)) t)) G
  | 0 => by
    show (gring E (j + 1 + 1)).mul (gring E (j + 1 + 1)).one G = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero E.one) G
    rw [mono0_one]; rfl
  | t + 1 => by
    have ih := powXG A G hXG t
    have hXG' : (gring E (j + 1 + 1)).mul (Xv E j) G = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)) G := hXG
    show (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (pw (gring E (j + 1 + 1)) ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) t)
      ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j))) G = _
    generalize pw (gring E (j + 1 + 1)) ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) t = P at ih
    calc _ = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) ((gring E (j + 1 + 1)).mul P ((gring E (j + 1 + 1)).mul (Xv E j) G)) := by lib_ring
      _ = (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)))
            ((gring E (j + 1 + 1)).mul P G) := by rw [hXG']; lib_ring
      _ = (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)))
            ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (pw E (E.mul A (Pp E j)) t)) G) := by rw [ih]
      _ = (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (pw E (E.mul A (Pp E j)) t))
            ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)))) G := by lib_ring
      _ = _ := by
        show gmul E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (pw E (E.mul A (Pp E j)) t))
          (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)))) G = _
        rw [mono0_mul, mono0_mul]; rfl

/-- **The n-th power residue criterion.** -/
theorem nth_recip (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (k : Nat)
    (hnt : ∀ i, i < k → sumK E (fun a => pw E (ψ a) (i + 2)) (LL j) = E.zero) (hinv : ∀ x, pw E (ψ x) (k + 1) = ψ (iv j x))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hE : nsm E (k' + 1 + 1) E.one = E.zero)
    (hfix : ∀ a, pw E (ψ a) (k' + 1 + 1) = ψ a) (hQ : nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (m : Nat) (hqn : k' + 1 + 1 = (k + 2) * m + 1) :
    ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one) (j + 1)) =
      pw E (E.mul (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Pp E j)) m := by
  have hF := gauss_frob E (j + 1) hpr k' hq hE ψ hm hfix hQ
  rw [gauss_Gv] at hF
  have hP : pw (gring E (j + 1 + 1)) (Gv j ψ) (k' + 1 + 1) = (gring E (j + 1 + 1)).mul (pw (gring E (j + 1 + 1)) (pw (gring E (j + 1 + 1)) (Gv j ψ) (k + 2)) m) (Gv j ψ) := by
    rw [hqn, TwoPower.pwpow]; rfl
  rw [hP, gpow_n E j hpr ψ hm h1 h0 hs k hnt hinv] at hF
  have hX := powXG E j (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Gv j ψ) (XG E j ψ hs) m
  have hF' := hX.symm.trans hF
  have hc : gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero
        (pw E (E.mul (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Pp E j)) m)) (Gv j ψ)) (ring (j + 1 + 1)).one =
      gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero
        (ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one) (j + 1)))) (Gv j ψ)) (ring (j + 1 + 1)).one :=
    congrArg (fun u => gt E (j + 1 + 1) u (ring (j + 1 + 1)).one) hF'
  rw [gt_mono0, gt_mono0] at hc
  unfold Gv at hc
  rw [gt_tb, h1] at hc
  calc _ = E.mul (ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one) (j + 1))) E.one := by lib_ring
    _ = E.mul (pw E (E.mul (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Pp E j)) m) E.one := hc.symm
    _ = _ := by lib_ring

end CharSumGen

#print axioms CharSumGen.mono0_one
#print axioms CharSumGen.mono0_mul
#print axioms CharSumGen.powXG
#print axioms CharSumGen.nth_recip

#print axioms: 4 theorems, no axioms.

78.166 The \(n\)-th power residue criterion at \(q\equiv -1\) mod \(n\)

Now \(\psi ^q=\bar \psi \), so \(G_\psi ^q=\psi (q)G_{\bar \psi }\) and \(G_\psi ^{q+1}=\psi (q)\bar \psi (-1)(p\zeta ^0-N)=(G_\psi ^n)^m\).

\[ q+1=nm:\quad \psi (q)\,\bar \psi (-1)=A^m\,p^{m-1}. \]

lang/NthRecipInert.lean

/-
  lang/NthRecipInert — T49, cycle 608: **the n-th power residue criterion at primes q ≡ −1 (mod n)**:
  with ψ as in NthRecip (order n = k + 2), q prime, q + 1 = n·m, E of characteristic q with ψ^q = ψ̄ (= ψ∘ι):
    gauss_frob_bar   G_ψ^q = ψ(q) G_ψ̄
    nth_recip_inert  ψ(q)·ψ(ι(−1)) = A^m p^(m−1),   A = ∏_{i≤k} J(ψ^i, ψ)·ψ(ι(−1))
  (the cubic case is CubicRecip: χ_p(q) = J^(k+1) p^k for q = 3k + 2).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.NthRecip

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

theorem gauss_frob_bar (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1)
    (hE : nsm E (k' + 1 + 1) E.one = E.zero) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ a b, ψ ((ring (j + 1 + 1)).mul a b) = E.mul (ψ a) (ψ b)) (hbar : ∀ a, pw E (ψ a) (k' + 1 + 1) = ψ (iv j a))
    (hQ : nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero) :
    pw (gring E (j + 1 + 1)) (Gv j ψ) (k' + 1 + 1) =
      gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one))) (Gv j (fun x => ψ (iv j x))) := by
  rw [← gauss_Gv E j ψ, ← gauss_Gv E j (fun x => ψ (iv j x))]
  generalize hQd : nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one = Q at hQ ⊢
  have hinv := inv_exists (j + 1) hpr Q hQ
  generalize hQi : pw (ring (j + 1 + 1)) Q (j + 1) = Qi at hinv
  have hL : pw (gring E (j + 1 + 1)) (gauss E (j + 1) ψ) (k' + 1 + 1) = sumK (gring E (j + 1 + 1)) (fun a => mono E (j + 1 + 1) ((ring (j + 1 + 1)).mul Q a) (ψ (iv j a))) (L (j + 1 + 1)) := by
    unfold gauss
    rw [frob_sum E (j + 1 + 1) k' hq hE]
    apply sumK_ext; intro a
    rw [mono_pow, hbar, nsm_eq_mul, hQd]
  have hc := CMInert.complete_allZ (j + 1 + 1) (gring E (j + 1 + 1))
  have hre := reindex (gring E (j + 1 + 1)) (L (j + 1 + 1)) hc (fun b => (ring (j + 1 + 1)).mul Qi b) (fun a => (ring (j + 1 + 1)).mul Q a)
    (fun b => by show (ring (j + 1 + 1)).mul Q ((ring (j + 1 + 1)).mul Qi b) = b
                 calc _ = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul Qi Q) b := by lib_ring
                   _ = b := by rw [hinv]; lib_ring)
    (fun a => by show (ring (j + 1 + 1)).mul Qi ((ring (j + 1 + 1)).mul Q a) = a
                 calc _ = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul Qi Q) a := by lib_ring
                   _ = a := by rw [hinv]; lib_ring)
    (fun a => mono E (j + 1 + 1) ((ring (j + 1 + 1)).mul Q a) (ψ (iv j a)))
  rw [hL, ← hre]
  unfold gauss
  show _ = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ Q)) (sumK (gring E (j + 1 + 1)) (fun a => mono E (j + 1 + 1) a (ψ (iv j a))) (L (j + 1 + 1)))
  rw [mul_sumK]
  apply sumK_ext; intro b
  show _ = gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ Q)) (mono E (j + 1 + 1) b (ψ (iv j b)))
  rw [mono_mul, ← hm]
  have e1 : (ring (j + 1 + 1)).mul Q ((ring (j + 1 + 1)).mul Qi b) = (ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero b := by
    calc _ = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul Qi Q) b := by lib_ring
      _ = _ := by rw [hinv]; lib_ring
  have e2 : iv j ((ring (j + 1 + 1)).mul Qi b) = (ring (j + 1 + 1)).mul Q (iv j b) := by
    rw [iv_mulmul, ← hQi]
    show (ring (j + 1 + 1)).mul (iv j (iv j Q)) (iv j b) = _
    rw [iv_iv j hpr]
  rw [e1, e2]

/-- (mono0 A · X)^(t+1) = mono0 (A^(t+1) p^t) · X. -/
theorem powX (A : S) : ∀ t,
    pw (gring E (j + 1 + 1)) ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) (t + 1) =
      (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (pw E A (t + 1)) (pw E (Pp E j) t))) (Xv E j)
  | 0 => by
    show (gring E (j + 1 + 1)).mul (gring E (j + 1 + 1)).one ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) = _
    rw [(gring E (j + 1 + 1)).one_mul]
    show _ = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (E.mul E.one A) E.one)) (Xv E j)
    rw [show E.mul (E.mul E.one A) E.one = A by lib_ring]
  | t + 1 => by
    show (gring E (j + 1 + 1)).mul (pw (gring E (j + 1 + 1)) ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) (t + 1))
      ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A) (Xv E j)) = _
    rw [powX A t]
    have hXX : (gring E (j + 1 + 1)).mul (Xv E j) (Xv E j) = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j)) (Xv E j) := XX E j
    calc _ = (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (pw E A (t + 1)) (pw E (Pp E j) t))) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A))
          ((gring E (j + 1 + 1)).mul (Xv E j) (Xv E j)) := by lib_ring
      _ = (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (pw E A (t + 1)) (pw E (Pp E j) t))) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A))
          (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j))) (Xv E j) := by rw [hXX]; lib_ring
      _ = _ := by
        show gmul E (j + 1 + 1) (gmul E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero _) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero A)) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (Pp E j))) (Xv E j) = _
        rw [mono0_mul, mono0_mul]
        show _ = gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (E.mul (pw E A (t + 1)) A) (E.mul (pw E (Pp E j) t) (Pp E j)))) (Xv E j)
        rw [show E.mul (E.mul (E.mul (pw E A (t + 1)) (pw E (Pp E j) t)) A) (Pp E j) = E.mul (E.mul (pw E A (t + 1)) A) (E.mul (pw E (Pp E j) t) (Pp E j)) by lib_ring]

/-- **The n-th power residue criterion at q ≡ −1 (mod n)**: ψ(q)·ψ(ι(−1)) = A^m p^(m−1). -/
theorem nth_recip_inert (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (k : Nat)
    (hnt : ∀ i, i < k → sumK E (fun a => pw E (ψ a) (i + 2)) (LL j) = E.zero) (hinv : ∀ x, pw E (ψ x) (k + 1) = ψ (iv j x))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hE : nsm E (k' + 1 + 1) E.one = E.zero)
    (hbar : ∀ a, pw E (ψ a) (k' + 1 + 1) = ψ (iv j a)) (hQ : nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (m : Nat) (hqn : k' + 1 + 1 + 1 = (k + 2) * (m + 1)) :
    E.mul (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one)) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) =
      E.mul (pw E (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (m + 1)) (pw E (Pp E j) m) := by
  have hF := gauss_frob_bar E j hpr k' hq hE ψ hm hbar hQ
  -- G^(q+1) two ways
  have hA : pw (gring E (j + 1 + 1)) (Gv j ψ) (k' + 1 + 1 + 1) =
      (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (pw E (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (m + 1)) (pw E (Pp E j) m))) (Xv E j) := by
    rw [hqn, ← TwoPower.pwpow, gpow_n E j hpr ψ hm h1 h0 hs k hnt hinv]
    exact powX E j _ m
  have hB : pw (gring E (j + 1 + 1)) (Gv j ψ) (k' + 1 + 1 + 1) =
      (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one)) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))) (Xv E j) := by
    show (gring E (j + 1 + 1)).mul (pw (gring E (j + 1 + 1)) (Gv j ψ) (k' + 1 + 1)) (Gv j ψ) = _
    rw [hF]
    have hN : (gring E (j + 1 + 1)).mul (Gv j ψ) (Gv j (fun x => ψ (iv j x))) = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Xv E j) :=
      ext E (j + 1 + 1) _ _ (fun c => by
        show gt E (j + 1 + 1) (gmul E (j + 1 + 1) _ _) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) _ _) c
        rw [gt_mono0, gauss_norm E j hpr ψ hm h1 h0 hs c])
    calc (gring E (j + 1 + 1)).mul (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one))) (Gv j (fun x => ψ (iv j x)))) (Gv j ψ)
        = (gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one))) ((gring E (j + 1 + 1)).mul (Gv j ψ) (Gv j (fun x => ψ (iv j x)))) := by
          show (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul _ _) _ = _; lib_ring
      _ = (gring E (j + 1 + 1)).mul ((gring E (j + 1 + 1)).mul (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one))) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))) (Xv E j) := by rw [hN]; lib_ring
      _ = _ := by show gmul E (j + 1 + 1) (gmul E (j + 1 + 1) _ _) _ = _; rw [mono0_mul]; rfl
  have hX1 : gt E (j + 1 + 1) (Xv E j) (ring (j + 1 + 1)).one = E.neg E.one := by
    rw [gt_X]; unfold delta; rw [ite_neg' _ _ (fun e => PowerSum.one_ne_zero' (j + 1) e)]; lib_ring
  have hc' : E.mul (E.mul (pw E (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (m + 1)) (pw E (Pp E j) m)) (E.neg E.one) =
      E.mul (E.mul (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one)) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (E.neg E.one) := by
    have hc0 : gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (pw E (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (m + 1)) (pw E (Pp E j) m))) (Xv E j)) (ring (j + 1 + 1)).one =
        gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero (E.mul (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one)) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))) (Xv E j)) (ring (j + 1 + 1)).one :=
      congrArg (fun u => gt E (j + 1 + 1) u (ring (j + 1 + 1)).one) (hA.symm.trans hB)
    rw [gt_mono0, gt_mono0, hX1] at hc0; exact hc0
  calc _ = E.mul (E.neg E.one) (E.mul (E.mul (ψ (nsm (ring (j + 1 + 1)) (k' + 1 + 1) (ring (j + 1 + 1)).one)) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (E.neg E.one)) := by lib_ring
    _ = _ := by rw [← hc']; lib_ring

end CharSumGen

#print axioms CharSumGen.gauss_frob_bar
#print axioms CharSumGen.powX
#print axioms CharSumGen.nth_recip_inert

#print axioms: 3 theorems, no axioms.

78.167 The criterion at \(n=2\)

With \(\psi \) the Legendre symbol of \(p\) read in \(\mathbb F_q\) every hypothesis of the general criterion holds; it is Euler’s criterion for \(p^*\) modulo \(q\).

\[ \chi _p(q^{-1})=(\chi _p(-1)\,p)^{(q-1)/2}\quad \text {in }\mathbb F_q. \]

lang/NthRecip2.lean

/-
  lang/NthRecip2 — T49, cycle 610: **non-vacuity check of NthRecip: the case n = 2**. With ψ = the Legendre symbol χ_p cast into 𝔽_q
  (q = 2r + 3), every hypothesis of CharSumGen.nth_recip is met (ψ² nontriviality is vacuous for n = 2, ψ = ψ∘ι, Fermat in 𝔽_q), and it gives
    χ_p(q^(−1)) = (χ_p(−1)·p)^((q−1)/2)   in 𝔽_q,
  i.e. Euler's criterion for p* = χ_p(−1)p modulo q: quadratic reciprocity (compare GaussQR.qr_gauss).
  Lean 4 core only, proofs by hand (lib_int, lib_ring), no axioms.
-/
import lang.NthRecip
import lang.GaussQR
import lang.IntCast

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QChar ConicSum
open LibRing (castZ castN)

variable (m r : Nat)

theorem castZ_sum {A : Type} (f : A → Int) : ∀ l : List A,
    castZ (ring (2 * r + 1 + 1)) (sumK LibInt.intR f l) = sumK (ring (2 * r + 1 + 1)) (fun a => castZ (ring (2 * r + 1 + 1)) (f a)) l
  | [] => rfl
  | a :: l => by
    show castZ (ring (2 * r + 1 + 1)) (f a + sumK LibInt.intR f l) = _
    rw [IntCast.castZ_add, castZ_sum f l]; rfl

/-- **n = 2**: χ_p(q⁻¹) = (χ_p(−1)·p)^(r+1) in 𝔽_q, from the general criterion. -/
theorem nth_recip_two (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1)
    (hq : ∀ d, Dv d (2 * r + 1 + 1 + 1) → d = 1 ∨ d = 2 * r + 1 + 1 + 1)
    (hQ : nsm (ring (2 * m + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) :
    castZ (ring (2 * r + 1 + 1)) (chi m (pw (ring (2 * m + 1 + 1)) (nsm (ring (2 * m + 1 + 1)) (2 * r + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one) (2 * m + 1))) =
      pw (ring (2 * r + 1 + 1)) ((ring (2 * r + 1 + 1)).mul ((ring (2 * r + 1 + 1)).mul (ring (2 * r + 1 + 1)).one
        (castZ (ring (2 * r + 1 + 1)) (chi m (iv (2 * m) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))))) (Pp (ring (2 * r + 1 + 1)) (2 * m))) (r + 1) := by
  have h := nth_recip (ring (2 * r + 1 + 1)) (2 * m) hpr (fun a => castZ (ring (2 * r + 1 + 1)) (chi m a))
    (fun x y => by show castZ _ (chi m ((ring (2 * m + 1 + 1)).mul x y)) = _; rw [chi_mul m hpr, HasseDavenport.castZ_mul])
    (by show castZ _ (chi m (ring (2 * m + 1 + 1)).one) = _; rw [chi_one]; exact GaussQR.castZ_one' _)
    (by show castZ _ (chi m (ring (2 * m + 1 + 1)).zero) = _; rw [chi_zero]; rfl)
    (by show sumK _ (fun a => castZ (ring (2 * r + 1 + 1)) (chi m a)) (L m) = _
        rw [← castZ_sum, show sumK LibInt.intR (chi m) (L m) = 0 from chi_sum m hpr]; rfl)
    0 (fun i hi => absurd hi (Nat.not_lt_zero i))
    (fun x => by
      show (ring (2 * r + 1 + 1)).mul (ring (2 * r + 1 + 1)).one (castZ _ (chi m x)) = castZ _ (chi m (pw (ring (2 * m + 1 + 1)) x (2 * m + 1)))
      rw [(ring (2 * r + 1 + 1)).one_mul]
      by_cases hx : x = (ring (2 * m + 1 + 1)).zero
      · rw [hx, show pw (ring (2 * m + 1 + 1)) (ring (2 * m + 1 + 1)).zero (2 * m + 1) = (ring (2 * m + 1 + 1)).zero from ConicSum.iv_zero m]
      · rw [show pw (ring (2 * m + 1 + 1)) x (2 * m + 1) = ConicSum.iv m x from rfl, GaussFrob.chi_inv m hpr x hx])
    (2 * r + 1) hq (char_p (2 * r + 1 + 1)) (fun a => GaussFrob.fermat_q r hq _) hQ (r + 1) (by lib_nat)
  exact h

end CharSumGen

#print axioms CharSumGen.castZ_sum
#print axioms CharSumGen.nth_recip_two

#print axioms: 2 theorems, no axioms.

78.168 The \(n\)-th power residue criterion at every prime

Iterating the Frobenius to \(x\mapsto x^{q^e}\) with \(q^e\equiv 1\) mod \(n\) extends the criterion to every residue class of \(q\).

\[ q^e=nm+1:\quad \psi (q^{-e})=(A\,p)^m. \]

lang/NthRecipPow.lean

/-
  lang/NthRecipPow — T49, cycle 625: **the n-th power residue criterion for every prime q ∤ np** (any residue class of q mod n).
  If q^f ≡ 1 (mod n) (f the order of q, or any multiple), iterate the Frobenius:
    frob_sum_pow     (Σ x_a)^(q^e) = Σ x_a^(q^e) in E[C_p], char E = q
    gauss_frob_pow   ψ^(q^e) = ψ ⇒ G_ψ^(q^e) = ψ(q^(−e)) G_ψ
    nth_recip_pow    q^e = n·m + 1 ⇒ ψ(q^(−e)) = (A·p)^m,  A = (∏_{i≤k} J(ψ^i, ψ))·ψ(ι(−1))
  (NthRecip is e = 1; for q of order f mod n this gives ψ(q)^f, i.e. the n-th power residue symbol of q up to f-th roots of unity,
  and q is an n-th power mod p ⇒ (A·p)^((q^f − 1)/n) = 1 in E = 𝔽_{q^f}.)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.NthRecip

namespace GroupRingFrob

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing Frobenius

variable {S : Type} (E : CRing S) (j : Nat)

theorem frob_sum_pow (k : Nat) (hq : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1) (hE : nsm E (k + 1 + 1) E.one = E.zero) :
    ∀ e {A : Type} (f : A → V S (j + 1 + 1)) (l : List A),
      pw (gring E (j + 1)) (sumK (gring E (j + 1)) f l) ((k + 1 + 1) ^ e) = sumK (gring E (j + 1)) (fun a => pw (gring E (j + 1)) (f a) ((k + 1 + 1) ^ e)) l
  | 0, _, f, l => by
    show (gring E (j + 1)).mul (gring E (j + 1)).one _ = _
    rw [(gring E (j + 1)).one_mul]
    exact (sumK_ext _ _ _ (fun a => (gring E (j + 1)).one_mul (f a)) l).symm
  | e + 1, _, f, l => by
    rw [show (k + 1 + 1) ^ (e + 1) = (k + 1 + 1) ^ e * (k + 1 + 1) from rfl, ← TwoPower.pwpow, frob_sum_pow k hq hE e f l,
      frob_sum E (j + 1) k hq hE]
    exact sumK_ext _ _ _ (fun a => TwoPower.pwpow _ _ _ _) l

theorem gauss_frob_pow (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (k : Nat) (hq : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1)
    (hE : nsm E (k + 1 + 1) E.one = E.zero) (e : Nat) (ψ : Zp (j + 1) → S)
    (hmul : ∀ a b, ψ ((ring (j + 1)).mul a b) = E.mul (ψ a) (ψ b)) (hfix : ∀ a, pw E (ψ a) ((k + 1 + 1) ^ e) = ψ a)
    (hQ : nsm (ring (j + 1)) ((k + 1 + 1) ^ e) (ring (j + 1)).one ≠ (ring (j + 1)).zero) :
    pw (gring E (j + 1)) (gauss E j ψ) ((k + 1 + 1) ^ e) =
      gmul E (j + 1) (mono E (j + 1) (ring (j + 1)).zero (ψ (pw (ring (j + 1)) (nsm (ring (j + 1)) ((k + 1 + 1) ^ e) (ring (j + 1)).one) j))) (gauss E j ψ) := by
  generalize hQd : nsm (ring (j + 1)) ((k + 1 + 1) ^ e) (ring (j + 1)).one = Q at hQ
  have hinv := inv_exists j hpr Q hQ
  generalize hQi : pw (ring (j + 1)) Q j = Qi at hinv
  -- LHS = Σ_a mono (Q a) (ψ a)
  have hL : pw (gring E (j + 1)) (gauss E j ψ) ((k + 1 + 1) ^ e) = sumK (gring E (j + 1)) (fun a => mono E (j + 1) ((ring (j + 1)).mul Q a) (ψ a)) (L (j + 1)) := by
    unfold gauss
    rw [frob_sum_pow E j k hq hE e]
    apply sumK_ext; intro a
    rw [mono_pow, hfix, nsm_eq_mul, hQd]
  -- reindex a = Qi b
  have hc := CMInert.complete_allZ (j + 1) (gring E (j + 1))
  have hre := reindex (gring E (j + 1)) (L (j + 1)) hc (fun b => (ring (j + 1)).mul Qi b) (fun a => (ring (j + 1)).mul Q a)
    (fun b => by show (ring (j + 1)).mul Q ((ring (j + 1)).mul Qi b) = b
                 calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) b := by lib_ring
                   _ = b := by rw [hinv]; lib_ring)
    (fun a => by show (ring (j + 1)).mul Qi ((ring (j + 1)).mul Q a) = a
                 calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) a := by lib_ring
                   _ = a := by rw [hinv]; lib_ring)
    (fun a => mono E (j + 1) ((ring (j + 1)).mul Q a) (ψ a))
  rw [hL, ← hre]
  unfold gauss
  show _ = (gring E (j + 1)).mul (mono E (j + 1) (ring (j + 1)).zero (ψ Qi)) (sumK (gring E (j + 1)) (fun a => mono E (j + 1) a (ψ a)) (L (j + 1)))
  rw [mul_sumK]
  apply sumK_ext; intro b
  show _ = gmul E (j + 1) (mono E (j + 1) (ring (j + 1)).zero (ψ Qi)) (mono E (j + 1) b (ψ b))
  rw [mono_mul, ← hmul]
  have e1 : (ring (j + 1)).mul Q ((ring (j + 1)).mul Qi b) = (ring (j + 1)).add (ring (j + 1)).zero b := by
    calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) b := by lib_ring
      _ = _ := by rw [hinv]; lib_ring
  rw [e1]

end GroupRingFrob

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

/-- **The n-th power residue criterion at q^e ≡ 1 (mod n).** -/
theorem nth_recip_pow (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (k : Nat)
    (hnt : ∀ i, i < k → sumK E (fun a => pw E (ψ a) (i + 2)) (LL j) = E.zero) (hinv : ∀ x, pw E (ψ x) (k + 1) = ψ (iv j x))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hE : nsm E (k' + 1 + 1) E.one = E.zero) (e : Nat)
    (hfix : ∀ a, pw E (ψ a) ((k' + 1 + 1) ^ e) = ψ a) (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (m : Nat) (hqn : (k' + 1 + 1) ^ e = (k + 2) * m + 1) :
    ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) =
      pw E (E.mul (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Pp E j)) m := by
  have hF := gauss_frob_pow E (j + 1) hpr k' hq hE e ψ hm hfix hQ
  rw [gauss_Gv] at hF
  have hP : pw (gring E (j + 1 + 1)) (Gv j ψ) ((k' + 1 + 1) ^ e) = (gring E (j + 1 + 1)).mul (pw (gring E (j + 1 + 1)) (pw (gring E (j + 1 + 1)) (Gv j ψ) (k + 2)) m) (Gv j ψ) := by
    rw [hqn, TwoPower.pwpow]; rfl
  rw [hP, gpow_n E j hpr ψ hm h1 h0 hs k hnt hinv] at hF
  have hX := powXG E j (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Gv j ψ) (XG E j ψ hs) m
  have hF' := hX.symm.trans hF
  have hc : gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero
        (pw E (E.mul (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Pp E j)) m)) (Gv j ψ)) (ring (j + 1 + 1)).one =
      gt E (j + 1 + 1) (gmul E (j + 1 + 1) (mono E (j + 1 + 1) (ring (j + 1 + 1)).zero
        (ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)))) (Gv j ψ)) (ring (j + 1 + 1)).one :=
    congrArg (fun u => gt E (j + 1 + 1) u (ring (j + 1 + 1)).one) hF'
  rw [gt_mono0, gt_mono0] at hc
  unfold Gv at hc
  rw [gt_tb, h1] at hc
  calc _ = E.mul (ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1))) E.one := by lib_ring
    _ = E.mul (pw E (E.mul (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) (Pp E j)) m) E.one := hc.symm
    _ = _ := by lib_ring

end CharSumGen

#print axioms GroupRingFrob.frob_sum_pow
#print axioms GroupRingFrob.gauss_frob_pow
#print axioms CharSumGen.nth_recip_pow

#print axioms: 3 theorems, no axioms.

78.169 Eisenstein reciprocity for \(\ell =3\) at inert \(q\)

For \(q\equiv 2\pmod 3\) prime and \(J\) the primary prime above \(p\), the cubic character of \(q\) mod \(J\) is \(J^{(q^2-1)/3}\) mod \(q\).

\[ \chi _p(q)\equiv J^{(q^2-1)/3}\pmod q\quad \text {in }\mathbb F_q[\omega ]. \]

lang/Eis3.lean

/-
  lang/Eis3 — T49, cycle 744: **Eisenstein reciprocity for ℓ = 3 at an inert prime, in symbol form**: for q = 3k + 2 prime and J the
  primary prime of ℤ[ω] above p = 6n + 7,
      χ_p(q) ≡ J^((q² − 1)/3)  (mod q)   in 𝔽_q[ω],
  i.e. (q/J)₃ = (J/q)₃ (the right side is the cubic residue symbol of J modulo the inert prime q, by Euler's criterion in 𝔽_{q²}).
    sc_mul, sc_pow   𝔽_q ⊂ 𝔽_q[ω]
    frobC            (a + bω)^q = a + bω² = conj(a + bω)       (frob_add, Fermat, ω^q = ω²)
    ph_cconj         reduction mod q commutes with conjugation
    eis3_inert       from CubicRecip.cubic_recip_inert: χ_p(q) = J^(k+1) p^k, p = J J̄ (JacobiNorm), J̄ ≡ J^q
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.CubicRecip

namespace Eis3

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm CubicRecip
open LibRing (castZ castN)

variable (n k : Nat)

def sc (a : Zp (3 * k + 1)) : Zp (3 * k + 1) × Zp (3 * k + 1) := (a, (ring (3 * k + 1)).zero)

theorem sc_mul (a b : Zp (3 * k + 1)) : (Cyclo3.cyc3 (ring (3 * k + 1))).mul (sc k a) (sc k b) = sc k ((ring (3 * k + 1)).mul a b) := by
  apply Prod.ext
  · show (ring (3 * k + 1)).add ((ring (3 * k + 1)).mul a b) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).zero)) = (ring (3 * k + 1)).mul a b; lib_ring
  · show (ring (3 * k + 1)).add ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul a (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero b)) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).zero)) = (ring (3 * k + 1)).zero; lib_ring

theorem sc_pow (a : Zp (3 * k + 1)) : ∀ m, pw (Cyclo3.cyc3 (ring (3 * k + 1))) (sc k a) m = sc k (pw (ring (3 * k + 1)) a m)
  | 0 => rfl
  | m + 1 => by
    show (Cyclo3.cyc3 (ring (3 * k + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * k + 1))) (sc k a) m) (sc k a) = sc k ((ring (3 * k + 1)).mul (pw (ring (3 * k + 1)) a m) a)
    rw [sc_pow a m, sc_mul]

def om : Zp (3 * k + 1) × Zp (3 * k + 1) := ((ring (3 * k + 1)).zero, (ring (3 * k + 1)).one)

theorem om3 : pw (Cyclo3.cyc3 (ring (3 * k + 1))) (om k) 3 = (Cyclo3.cyc3 (ring (3 * k + 1))).one := by
  apply Prod.ext
  · show ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul (ring (3 * k + 1)).one (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).one))) (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul ((ring (3 * k + 1)).add ((ri  -- (+1408 chars; full line in the Zenodo source)
    lib_ring
  · show ((ring (3 * k + 1)).add ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul (ring (3 * k + 1)).one (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).one))) (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul ((ri  -- (+2478 chars; full line in the Zenodo source)
    lib_ring

theorem decomp (a b : Zp (3 * k + 1)) : (a, b) = (Cyclo3.cyc3 (ring (3 * k + 1))).add (sc k a) ((Cyclo3.cyc3 (ring (3 * k + 1))).mul (sc k b) (om k)) := by
  apply Prod.ext
  · show a = ((ring (3 * k + 1)).add a ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul b (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).one))))
    lib_ring
  · show b = ((ring (3 * k + 1)).add (ring (3 * k + 1)).zero ((ring (3 * k + 1)).add ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul b (ring (3 * k + 1)).one) ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).zero)) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).one))))
    lib_ring

theorem frobC (hq : ∀ d, Dv d (3 * k + 1 + 1) → d = 1 ∨ d = 3 * k + 1 + 1) (a b : Zp (3 * k + 1)) :
    pw (Cyclo3.cyc3 (ring (3 * k + 1))) (a, b) (3 * k + 1 + 1) = ((ring (3 * k + 1)).add a ((ring (3 * k + 1)).neg b), (ring (3 * k + 1)).neg b) := by
  rw [decomp k a b, Frobenius.frob_add (Cyclo3.cyc3 (ring (3 * k + 1))) (3 * k) hq (CubicRecip.charq k), Frobenius.pw_mul, sc_pow, sc_pow, ZMod.fermat (3 * k) hq a, ZMod.fermat (3 * k) hq b,
    show (3 * k + 1 + 1) = 3 * k + 2 from rfl, CubicRecip.om_pow k (om k) (om3 k) k]
  apply Prod.ext
  · show ((ring (3 * k + 1)).add a ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul b ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).zero) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).one (ring (3 * k + 1)).one)))) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero ((ring (3 * k + 1)).add ((ring (3 * k + 1)).add   -- (+299 chars; full line in the Zenodo source)
    lib_ring
  · show ((ring (3 * k + 1)).add (ring (3 * k + 1)).zero ((ring (3 * k + 1)).add ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul b ((ring (3 * k + 1)).add ((ring (3 * k + 1)).add ((ring (3 * k + 1)).mul (ring (3 * k + 1)).zero (ring (3 * k + 1)).one) ((ring (3 * k + 1)).mul (ring (3 * k + 1)).one (ring (3 * k + 1)).zero)) ((ring (3 * k + 1)).neg ((ring (3 * k + 1)).mul (ring (3 * k + 1)).one (ring  -- (+658 chars; full line in the Zenodo source)
    lib_ring


theorem ph_cconj (z : Int × Int) : ph (ring (3 * k + 1)) (cconj z) = ((ring (3 * k + 1)).add (ph (ring (3 * k + 1)) z).1 ((ring (3 * k + 1)).neg (ph (ring (3 * k + 1)) z).2), (ring (3 * k + 1)).neg (ph (ring (3 * k + 1)) z).2) := by
  show (castZ (ring (3 * k + 1)) (z.1 + -z.2), castZ (ring (3 * k + 1)) (-z.2)) = _
  rw [IntCast.castZ_add, HasseDavenport.castZ_neg]; rfl

theorem frob_ph (hq : ∀ d, Dv d (3 * k + 1 + 1) → d = 1 ∨ d = 3 * k + 1 + 1) (z : Int × Int) : pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) z) (3 * k + 1 + 1) = ph (ring (3 * k + 1)) (cconj z) := by
  rw [ph_cconj]
  exact frobC k hq (ph (ring (3 * k + 1)) z).1 (ph (ring (3 * k + 1)) z).2

theorem pe_ph (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : ph (ring (3 * k + 1)) (Pe n) = (Cyclo3.cyc3 (ring (3 * k + 1))).mul (ph (ring (3 * k + 1)) (J n w)) (ph (ring (3 * k + 1)) (cconj (J n w))) := by
  rw [← ph_mul]
  congr 1
  have e : (J n w) = ((J n w).1, (J n w).2) := rfl
  rw [e, norm_form]
  show (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) = _
  rw [← jacobi_norm n hpr w hw1 hw3]

/-- **Eisenstein reciprocity, ℓ = 3, inert q: χ_p(q) ≡ J^((q² − 1)/3) (mod q).** -/
theorem eis3_inert (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (hq : ∀ d, Dv d (3 * k + 1 + 1) → d = 1 ∨ d = 3 * k + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (hQ : nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero) :
    ph (ring (3 * k + 1)) (chi3 n w (nsm (ring (6 * n + 5 + 1)) (3 * k + 1 + 1) (ring (6 * n + 5 + 1)).one)) = pw (Cyclo3.cyc3 (ring (3 * k + 1))) (ph (ring (3 * k + 1)) (J n w)) ((3 * k + 1) * (k + 1)) := by
  rw [cubic_recip_inert n k hpr hq w hw1 hw3 hQ, pe_ph n k hpr w hw1 hw3, ← frob_ph k hq, Frobenius.pw_mul, TwoPower.pwpow,
    ← HasseCM.pw_add', ← HasseCM.pw_add']
  congr 1
  lib_nat

end Eis3

#print axioms Eis3.sc
#print axioms Eis3.sc_mul
#print axioms Eis3.sc_pow
#print axioms Eis3.om
#print axioms Eis3.om3
#print axioms Eis3.decomp
#print axioms Eis3.frobC
#print axioms Eis3.ph_cconj
#print axioms Eis3.frob_ph
#print axioms Eis3.pe_ph
#print axioms Eis3.eis3_inert

#print axioms: 11 theorems, no axioms.

78.170 Eisenstein reciprocity for \(\ell =3\) at split \(q\)

For \(q\equiv 1\pmod 3\) and a cube root of unity \(u\in \mathbb F_q\), the cubic character of \(q\) mod \(J\) reduces to \((J/\pi )^2(\bar J/\pi )\).

\[ \bar \chi _p(q)\equiv \Big (\tfrac {J}{\pi }\Big )_3^2\Big (\tfrac {\bar J}{\pi }\Big )_3\pmod \pi . \]

lang/Eis3s.lean

/-
  lang/Eis3s — T49, cycle 745: **Eisenstein reciprocity for ℓ = 3 at a split prime q ≡ 1 (mod 3), symbol form**: with u ∈ 𝔽_q a cube
  root of unity (u² + u + 1 = 0) the evaluation ev_u : 𝔽_q[ω] → 𝔽_q, a + bω ↦ a + bu, is reduction modulo a prime π | q, and
      χ̄_p(q) ≡ (J/π)₃² (J̄/π)₃  (mod π),   (X/π)₃ = X(u)^((q−1)/3),
  i.e. (q/J)₃ = (J/π)(J/π̄) = (J/q)₃ (since (J/π̄) ≡ (J̄/π)² mod π).
    ev_mul, ev_pow   ev_u is multiplicative when u² + u + 1 = 0
    eis3_split       from CubicRecipSplit.cubic_recip_split and p = J J̄
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CubicRecipSplit
import lang.Eis3

namespace Eis3s

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm CubicRecip CubicRecipSplit
open LibRing (castZ castN)

variable (n m : Nat)

def ev (u : Zp (3 * m + 2 + 1)) (z : Zp (3 * m + 2 + 1) × Zp (3 * m + 2 + 1)) : Zp (3 * m + 2 + 1) := (ring (3 * m + 2 + 1)).add z.1 ((ring (3 * m + 2 + 1)).mul z.2 u)

theorem ev_mul (u : Zp (3 * m + 2 + 1)) (hu : (ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul u u) u) (ring (3 * m + 2 + 1)).one = (ring (3 * m + 2 + 1)).zero) (x y : Zp (3 * m + 2 + 1) × Zp (3 * m + 2 + 1)) :
    ev m u ((Cyclo3.cyc3 (ring (3 * m + 2 + 1))).mul x y) = (ring (3 * m + 2 + 1)).mul (ev m u x) (ev m u y) := by
  show (ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul x.1 y.1) ((ring (3 * m + 2 + 1)).neg ((ring (3 * m + 2 + 1)).mul x.2 y.2))) ((ring (3 * m + 2 + 1)).mul ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul x.1 y.2) ((ring (3 * m + 2 + 1)).mul x.2 y.1)) ((ring (3 * m + 2 + 1)).neg ((ring (3 * m + 2 + 1)).mul x.2 y.2))) u) = _
  have e : (ring (3 * m + 2 + 1)).mul (ev m u x) (ev m u y) = (ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul x.1 y.1) ((ring (3 * m + 2 + 1)).neg ((ring (3 * m + 2 + 1)).mul x.2 y.2))) ((ring (3 * m + 2 + 1)).mul ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul x.1 y.2) ((ring (3 * m + 2 + 1)).mul x.  -- (+78 chars; full line in the Zenodo source)
      ((ring (3 * m + 2 + 1)).mul ((ring (3 * m + 2 + 1)).mul x.2 y.2) ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul u u) u) (ring (3 * m + 2 + 1)).one)) := by unfold ev; lib_ring
  rw [e, hu]; lib_ring

theorem ev_one (u : Zp (3 * m + 2 + 1)) : ev m u (Cyclo3.cyc3 (ring (3 * m + 2 + 1))).one = (ring (3 * m + 2 + 1)).one := by
  show (ring (3 * m + 2 + 1)).add (ring (3 * m + 2 + 1)).one ((ring (3 * m + 2 + 1)).mul (ring (3 * m + 2 + 1)).zero u) = (ring (3 * m + 2 + 1)).one; lib_ring

theorem ev_pow (u : Zp (3 * m + 2 + 1)) (hu : (ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).add ((ring (3 * m + 2 + 1)).mul u u) u) (ring (3 * m + 2 + 1)).one = (ring (3 * m + 2 + 1)).zero) (x : Zp (3 * m + 2 + 1) × Zp (3 * m + 2 + 1)) : ∀ j, ev m u (pw (Cyclo3.cyc3 (ring (3 * m + 2 + 1))) x j) = pw (ring (3 * m + 2 + 1)) (ev m u x) j
  | 0 => ev_one m u
  | j + 1 => by
    show ev m u ((Cyclo3.cyc3 (ring (3 * m + 2 + 1))).mul (pw (Cyclo3.cyc3 (ring (3 * m + 2 + 1))) x j) x) = (ring (3 * m + 2 + 1)).mul (pw (ring (3 * m + 2 + 1)) (ev m u x) j) (ev m u x)
    rw [ev_mul m u hu, ev_pow u hu x j]

theorem pe_ph' (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) : ph (ring (3 * m + 2 + 1)) (Pe n) = (Cyclo3.cyc3 (ring (3 * m + 2 + 1))).mul (ph (ring (3 * m + 2 + 1)) (J n w)) (ph (ring (3 * m + 2 + 1)) (cconj (J n w))) := by
  rw [← ph_mul]
  congr 1
  have e : (J n w) = ((J n w).1, (J n w).2) := rfl
  rw [e, norm_form]
  show (Int.ofNat (6 * n + 5 + 1 + 1), (0 : Int)) = _
  rw [← jacobi_norm n hpr w hw1 hw3]

/-- **Eisenstein reciprocity, ℓ = 3, split q: χ̄_p(q) ≡ (J/π)²(J̄/π) (mod π).** -/
theorem eis3_split (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (hq : ∀ d, Dv d (3 * m + 2 + 1 + 1) → d = 1 ∨ d = 3 * m + 2 + 1 + 1) (w : Zp (6 * n + 5 + 1)) (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) (hQ : nsm (ring (6 * n + 5 + 1)) (3 * m + 2 + 1 + 1) (ring (6 * n + 5 + 1)).one ≠ (ring (6 * n + 5 + 1)).zero)   -- (+181 chars; full line in the Zenodo source)
    ev m u (ph (ring (3 * m + 2 + 1)) (cb n w (nsm (ring (6 * n + 5 + 1)) (3 * m + 2 + 1 + 1) (ring (6 * n + 5 + 1)).one)))
      = (ring (3 * m + 2 + 1)).mul (pw (ring (3 * m + 2 + 1)) (ev m u (ph (ring (3 * m + 2 + 1)) (J n w))) (2 * (m + 1))) (pw (ring (3 * m + 2 + 1)) (ev m u (ph (ring (3 * m + 2 + 1)) (cconj (J n w)))) (m + 1)) := by
  rw [cubic_recip_split n m hpr hq w hw1 hw3 hQ, ev_mul m u hu, ev_pow m u hu, ev_pow m u hu, pe_ph' n m hpr w hw1 hw3,
    ev_mul m u hu, Frobenius.pw_mul, show 2 * (m + 1) = (m + 1) + (m + 1) by lib_nat, HasseCM.pw_add' (ring (3 * m + 2 + 1)) (ev m u (ph (ring (3 * m + 2 + 1)) (J n w))) (m + 1) (m + 1)]
  lib_ring

end Eis3s

#print axioms Eis3s.ev
#print axioms Eis3s.ev_mul
#print axioms Eis3s.ev_one
#print axioms Eis3s.ev_pow
#print axioms Eis3s.pe_ph'
#print axioms Eis3s.eis3_split

#print axioms: 6 theorems, no axioms.

78.171 Quartic Eisenstein reciprocity at inert \(q\)

For \(q\equiv 3\pmod 4\) prime, the quartic character of \(q\) mod \(p\) is \(J^{(q^2-1)/4}\) mod \(q\) (up to \(\bar \chi (-1)\)).

\[ \chi (q)\,\bar \chi (-1)\equiv J^{(q^2-1)/4}\pmod q\quad \text {in }\mathbb F_q[i]. \]

lang/Eis4.lean

/-
  lang/Eis4 — T49, cycle 746: **quartic Eisenstein reciprocity at an inert prime, symbol form**: for q = 4k + 3 prime and J the
  Jacobi sum of the quartic character mod p = 4n + 5 (N(J) = p),
      χ(q)·χ̄(−1) ≡ J^((q² − 1)/4)  (mod q)   in 𝔽_q[i]         ((q² − 1)/4 = (4k + 2)(k + 1)),
  from QuartRecip.quart_recip_inert (= J^(2k+2) p^k) with Frobenius of 𝔽_q[i] = conjugation and p = J J̄ (QuartJacobi.quart_norm).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.QuartRecip

namespace Eis4

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartRecip
open LibRing (castZ castN)

variable (n k : Nat)

def sc (a : Zp (4 * k + 1 + 1)) : Zp (4 * k + 1 + 1) × Zp (4 * k + 1 + 1) := (a, (ring (4 * k + 1 + 1)).zero)

theorem sc_mul (a b : Zp (4 * k + 1 + 1)) : (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (sc k a) (sc k b) = sc k ((ring (4 * k + 1 + 1)).mul a b) := by
  apply Prod.ext
  · show (ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul a b) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).zero)) = (ring (4 * k + 1 + 1)).mul a b; lib_ring
  · show (ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul a (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero b) = (ring (4 * k + 1 + 1)).zero; lib_ring

theorem sc_pow (a : Zp (4 * k + 1 + 1)) : ∀ m, pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (sc k a) m = sc k (pw (ring (4 * k + 1 + 1)) a m)
  | 0 => rfl
  | m + 1 => by
    show (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (sc k a) m) (sc k a) = sc k ((ring (4 * k + 1 + 1)).mul (pw (ring (4 * k + 1 + 1)) a m) a)
    rw [sc_pow a m, sc_mul]

def ii : Zp (4 * k + 1 + 1) × Zp (4 * k + 1 + 1) := ((ring (4 * k + 1 + 1)).zero, (ring (4 * k + 1 + 1)).one)

theorem ii4 : pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ii k) 4 = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).one := by
  apply Prod.ext
  · show ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).one (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).one)  -- (+2439 chars; full line in the Zenodo source)
    lib_ring
  · show ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).one (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).one)  -- (+2411 chars; full line in the Zenodo source)
    lib_ring

theorem ii3 : pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ii k) 3 = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).neg (ii k) := by
  apply Prod.ext
  · show ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).one (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).one))) (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).  -- (+997 chars; full line in the Zenodo source)
    lib_ring
  · show ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).one (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).one))) (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).  -- (+967 chars; full line in the Zenodo source)
    lib_ring

theorem ii_q : ∀ j, pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ii k) (4 * j + 3) = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).neg (ii k)
  | 0 => ii3 k
  | j + 1 => by
    rw [show 4 * (j + 1) + 3 = (4 * j + 3) + 4 by lib_nat, HasseCM.pw_add', ii_q j, ii4]; lib_ring

theorem decomp (a b : Zp (4 * k + 1 + 1)) : (a, b) = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).add (sc k a) ((Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (sc k b) (ii k)) := by
  apply Prod.ext
  · show a = ((ring (4 * k + 1 + 1)).add a ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul b (ring (4 * k + 1 + 1)).zero) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).one))))
    lib_ring
  · show b = ((ring (4 * k + 1 + 1)).add (ring (4 * k + 1 + 1)).zero ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul b (ring (4 * k + 1 + 1)).one) ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero (ring (4 * k + 1 + 1)).zero)))
    lib_ring

theorem frobC4 (hq : ∀ d, Dv d (4 * k + 1 + 1 + 1) → d = 1 ∨ d = 4 * k + 1 + 1 + 1) (a b : Zp (4 * k + 1 + 1)) : pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (a, b) (4 * k + 1 + 1 + 1) = (a, (ring (4 * k + 1 + 1)).neg b) := by
  rw [decomp k a b, Frobenius.frob_add (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (4 * k + 1) hq (QuartRecip.charq4 (ring (4 * k + 1 + 1)) (4 * k + 1 + 1 + 1) (ZMod.char_p (4 * k + 1 + 1))), Frobenius.pw_mul, sc_pow, sc_pow,
    ZMod.fermat (4 * k + 1) hq a, ZMod.fermat (4 * k + 1) hq b, show 4 * k + 1 + 1 + 1 = 4 * k + 3 from rfl, ii_q k k]
  apply Prod.ext
  · show ((ring (4 * k + 1 + 1)).add a ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul b ((ring (4 * k + 1 + 1)).neg (ring (4 * k + 1 + 1)).zero)) ((ring (4 * k + 1 + 1)).neg ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero ((ring (4 * k + 1 + 1)).neg (ring (4 * k + 1 + 1)).one))))) = a
    lib_ring
  · show ((ring (4 * k + 1 + 1)).add (ring (4 * k + 1 + 1)).zero ((ring (4 * k + 1 + 1)).add ((ring (4 * k + 1 + 1)).mul b ((ring (4 * k + 1 + 1)).neg (ring (4 * k + 1 + 1)).one)) ((ring (4 * k + 1 + 1)).mul (ring (4 * k + 1 + 1)).zero ((ring (4 * k + 1 + 1)).neg (ring (4 * k + 1 + 1)).zero)))) = (ring (4 * k + 1 + 1)).neg b
    lib_ring

theorem frob_ph4 (hq : ∀ d, Dv d (4 * k + 1 + 1 + 1) → d = 1 ∨ d = 4 * k + 1 + 1 + 1) (z : Int × Int) : pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1)) z) (4 * k + 1 + 1 + 1) = ph4 (ring (4 * k + 1 + 1)) (gconj z) := by
  show pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (castZ (ring (4 * k + 1 + 1)) z.1, castZ (ring (4 * k + 1 + 1)) z.2) _ = (castZ (ring (4 * k + 1 + 1)) z.1, castZ (ring (4 * k + 1 + 1)) (-z.2))
  rw [frobC4 k hq, HasseDavenport.castZ_neg]

theorem pe4 (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : ph4 (ring (4 * k + 1 + 1)) (Ppq n) = (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 1 + 1)) (gconj (Jq n (chi4 n ι)))) := by
  rw [← ph4_mul (ring (4 * k + 1 + 1))]
  congr 1
  rw [show Ppq n = (Int.ofNat (4 * n + 3 + 1 + 1), 0) from sum_oneq n _, ← quart_norm n hpr ι hi]
  apply Prod.ext
  · show (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2 = (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + -((Jq n (chi4 n ι)).2 * -(Jq n (chi4 n ι)).2); lib_int
  · show (0 : Int) = (Jq n (chi4 n ι)).1 * -(Jq n (chi4 n ι)).2 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).1; lib_int

theorem pow_comb {R : Type} (K : CRing R) (x : R) (k : Nat) :
    K.mul (pw K (K.mul (K.mul x x) K.one) (k + 1)) (pw K (K.mul x (pw K x (4 * k + 1 + 1 + 1))) k) = pw K x ((4 * k + 2) * (k + 1)) := by
  rw [show K.mul (K.mul x x) K.one = pw K x 2 by show _ = K.mul (K.mul K.one x) x; lib_ring, TwoPower.pwpow, Frobenius.pw_mul, TwoPower.pwpow,
    ← HasseCM.pw_add', ← HasseCM.pw_add']
  congr 1
  lib_nat

/-- **Quartic Eisenstein reciprocity, inert q: χ(q)χ̄(−1) ≡ J^((q²−1)/4) (mod q).** -/
theorem eis4_inert (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (hq : ∀ d, Dv d (4 * k + 1 + 1 + 1) → d = 1 ∨ d = 4 * k + 1 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hQ : nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero) :
    (Cyclo4.cyc4 (ring (4 * k + 1 + 1))).mul (ph4 (ring (4 * k + 1 + 1)) (chi4 n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 1 + 1 + 1) (ring (4 * n + 3 + 1)).one))) (ph4 (ring (4 * k + 1 + 1)) (chib n ι ((ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one)))
      = pw (Cyclo4.cyc4 (ring (4 * k + 1 + 1))) (ph4 (ring (4 * k + 1 + 1)) (Jq n (chi4 n ι))) ((4 * k + 2) * (k + 1)) := by
  rw [quart_recip_inert n k hpr hq ι hi hQ, pe4 n k hpr ι hi, ← frob_ph4 k hq, ph4_one (ring (4 * k + 1 + 1))]
  exact pow_comb _ _ k

end Eis4
#print axioms Eis4.sc
#print axioms Eis4.sc_mul
#print axioms Eis4.sc_pow
#print axioms Eis4.ii
#print axioms Eis4.ii4
#print axioms Eis4.ii3
#print axioms Eis4.ii_q
#print axioms Eis4.decomp
#print axioms Eis4.frobC4
#print axioms Eis4.frob_ph4
#print axioms Eis4.pe4
#print axioms Eis4.pow_comb
#print axioms Eis4.eis4_inert

#print axioms: 13 theorems, no axioms.

78.172 Quartic Eisenstein reciprocity at split \(q\)

For \(q\equiv 1\pmod 4\) and \(u^2=-1\) in \(\mathbb F_q\), the quartic character of \(q\) mod \(p\) reduces to \((J/\pi )^3(\bar J/\pi )\).

\[ \bar \chi (q)\equiv \Big (\tfrac {J}{\pi }\Big )_4^3\Big (\tfrac {\bar J}{\pi }\Big )_4\pmod \pi . \]

lang/Eis4s.lean

/-
  lang/Eis4s — T49, cycle 747: **quartic Eisenstein reciprocity at a split prime q = 4k + 5, symbol form**: with u ∈ 𝔽_q, u² = −1,
  the evaluation ev_u(a + bi) = a + bu is reduction modulo a prime π | q, and
      χ̄(q) ≡ (J/π)₄³ (J̄/π)₄   (mod π),    (X/π)₄ = X(u)^((q−1)/4)
  (from QuartRecip.quart_recip_split: χ̄(q) = J^(2k+2) p^(k+1), p = J J̄).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Eis4

namespace Eis4s

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius QuartChar QuartJacobi QuartRecip
open LibRing (castZ castN)

variable (n k : Nat)

def ev (u : Zp (4 * k + 3 + 1)) (z : Zp (4 * k + 3 + 1) × Zp (4 * k + 3 + 1)) : Zp (4 * k + 3 + 1) := (ring (4 * k + 3 + 1)).add z.1 ((ring (4 * k + 3 + 1)).mul z.2 u)

theorem ev_mul (u : Zp (4 * k + 3 + 1)) (hu : (ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul u u) (ring (4 * k + 3 + 1)).one = (ring (4 * k + 3 + 1)).zero) (x y : Zp (4 * k + 3 + 1) × Zp (4 * k + 3 + 1)) :
    ev k u ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul x y) = (ring (4 * k + 3 + 1)).mul (ev k u x) (ev k u y) := by
  show (ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul x.1 y.1) ((ring (4 * k + 3 + 1)).neg ((ring (4 * k + 3 + 1)).mul x.2 y.2))) ((ring (4 * k + 3 + 1)).mul ((ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul x.1 y.2) ((ring (4 * k + 3 + 1)).mul x.2 y.1)) u) = _
  have e : (ring (4 * k + 3 + 1)).mul (ev k u x) (ev k u y) = (ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul x.1 y.1) ((ring (4 * k + 3 + 1)).neg ((ring (4 * k + 3 + 1)).mul x.2 y.2))) ((ring (4 * k + 3 + 1)).mul ((ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul x.1 y.2) ((ring (4 * k + 3 + 1)).mul x.2 y.1)) u))
      ((ring (4 * k + 3 + 1)).mul ((ring (4 * k + 3 + 1)).mul x.2 y.2) ((ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul u u) (ring (4 * k + 3 + 1)).one)) := by unfold ev; lib_ring
  rw [e, hu]; lib_ring

theorem ev_one (u : Zp (4 * k + 3 + 1)) : ev k u (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).one = (ring (4 * k + 3 + 1)).one := by
  show (ring (4 * k + 3 + 1)).add (ring (4 * k + 3 + 1)).one ((ring (4 * k + 3 + 1)).mul (ring (4 * k + 3 + 1)).zero u) = (ring (4 * k + 3 + 1)).one; lib_ring

theorem ev_pow (u : Zp (4 * k + 3 + 1)) (hu : (ring (4 * k + 3 + 1)).add ((ring (4 * k + 3 + 1)).mul u u) (ring (4 * k + 3 + 1)).one = (ring (4 * k + 3 + 1)).zero) (x : Zp (4 * k + 3 + 1) × Zp (4 * k + 3 + 1)) : ∀ j, ev k u (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) x j) = pw (ring (4 * k + 3 + 1)) (ev k u x) j
  | 0 => ev_one k u
  | j + 1 => by
    show ev k u ((Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (pw (Cyclo4.cyc4 (ring (4 * k + 3 + 1))) x j) x) = (ring (4 * k + 3 + 1)).mul (pw (ring (4 * k + 3 + 1)) (ev k u x) j) (ev k u x)
    rw [ev_mul k u hu, ev_pow u hu x j]

theorem pe4' (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) : ph4 (ring (4 * k + 3 + 1)) (Ppq n) = (Cyclo4.cyc4 (ring (4 * k + 3 + 1))).mul (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι))) (ph4 (ring (4 * k + 3 + 1)) (gconj (Jq n (chi4 n ι)))) := by
  rw [← ph4_mul (ring (4 * k + 3 + 1))]
  congr 1
  rw [show Ppq n = (Int.ofNat (4 * n + 3 + 1 + 1), 0) from sum_oneq n _, ← quart_norm n hpr ι hi]
  apply Prod.ext
  · show (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).2 = (Jq n (chi4 n ι)).1 * (Jq n (chi4 n ι)).1 + -((Jq n (chi4 n ι)).2 * -(Jq n (chi4 n ι)).2); lib_int
  · show (0 : Int) = (Jq n (chi4 n ι)).1 * -(Jq n (chi4 n ι)).2 + (Jq n (chi4 n ι)).2 * (Jq n (chi4 n ι)).1; lib_int

/-- **Quartic Eisenstein reciprocity, split q: χ̄(q) ≡ (J/π)³(J̄/π) (mod π).** -/
theorem eis4_split (hpr : ∀ d, Dv d (4 * n + 3 + 1 + 1) → d = 1 ∨ d = 4 * n + 3 + 1 + 1) (hq : ∀ d, Dv d (4 * k + 3 + 1 + 1) → d = 1 ∨ d = 4 * k + 3 + 1 + 1) (ι : Zp (4 * n + 3 + 1)) (hi : (ring (4 * n + 3 + 1)).mul ι ι = (ring (4 * n + 3 + 1)).neg (ring (4 * n + 3 + 1)).one) (hQ : nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one ≠ (ring (4 * n + 3 + 1)).zero) (u : Zp (4 *  -- (+138 chars; full line in the Zenodo source)
    ev k u (ph4 (ring (4 * k + 3 + 1)) (chib n ι (nsm (ring (4 * n + 3 + 1)) (4 * k + 3 + 1 + 1) (ring (4 * n + 3 + 1)).one)))
      = (ring (4 * k + 3 + 1)).mul (pw (ring (4 * k + 3 + 1)) (ev k u (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (3 * (k + 1))) (pw (ring (4 * k + 3 + 1)) (ev k u (ph4 (ring (4 * k + 3 + 1)) (gconj (Jq n (chi4 n ι))))) (k + 1)) := by
  rw [quart_recip_split n k hpr hq ι hi hQ, pe4' n k hpr ι hi, ph4_one (ring (4 * k + 3 + 1)), ev_mul k u hu, ev_pow k u hu, ev_pow k u hu, ev_mul k u hu,
    ev_mul k u hu, ev_mul k u hu, ev_one, Frobenius.pw_mul, Frobenius.pw_mul, Frobenius.pw_mul,
    show 3 * (k + 1) = (k + 1) + ((k + 1) + (k + 1)) by lib_nat, HasseCM.pw_add' (ring (4 * k + 3 + 1)) (ev k u (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (k + 1) ((k + 1) + (k + 1)),
    HasseCM.pw_add' (ring (4 * k + 3 + 1)) (ev k u (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (k + 1) (k + 1)]
  generalize Frobenius.pw (ring (4 * k + 3 + 1)) (ev k u (ph4 (ring (4 * k + 3 + 1)) (Jq n (chi4 n ι)))) (k + 1) = A
  generalize Frobenius.pw (ring (4 * k + 3 + 1)) (ev k u (ph4 (ring (4 * k + 3 + 1)) (gconj (Jq n (chi4 n ι))))) (k + 1) = B
  rw [TwoPower.pwone]
  lib_ring

end Eis4s

#print axioms Eis4s.ev
#print axioms Eis4s.ev_mul
#print axioms Eis4s.ev_one
#print axioms Eis4s.ev_pow
#print axioms Eis4s.pe4'
#print axioms Eis4s.eis4_split

#print axioms: 6 theorems, no axioms.

78.173 Fifth roots of unity in \(\mathbb F_p\)

A primitive fifth root of unity \(w\) gives all solutions of \(t^5=1\).

\[ t^5=1\Rightarrow t\in \{1,w,w^2,w^3,w^4\}. \]

lang/Quint1.lean

/-
  lang/Quint1 — T49, cycle 748: **fifth roots of unity in 𝔽_p, p = 10n + 11** (first step of the quintic character, ℓ = 5):
    r_zero      w ≠ 1, w⁵ = 1 ⇒ 1 + w + w² + w³ + w⁴ = 0
    fact5       ∏_{j<5} (t − w^j) = t⁵ − 1 modulo 1 + w + … + w⁴
    five_cases  t⁵ = 1 ⇒ t ∈ {1, w, w², w³, w⁴}
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Gen

namespace Quint1

open FTAChain ZMod YMChain FiniteRecChain DetChain

variable (n : Nat)

theorem r_zero (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w) = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+496 chars; full line in the Zenodo source)
  have e : ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).add w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring  -- (+259 chars; full line in the Zenodo source)
    rw [show ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).add w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ri  -- (+526 chars; full line in the Zenodo source)
  rcases G4Gen.mul_eq_zero (5 * n + 4) hpr _ _ e with e1 | e1
  · exact absurd (by rw [show w = ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).add w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) (ring (2 * (5 * n + 4) + 1 + 1)).one) by lib_ring, e1]; lib_ring) hw1
  · exact e1

theorem fact5 (t w : Zp (2 * (5 * n + 4) + 1 + 1)) (hr : ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1   -- (+1282 chars; full line in the Zenodo source)
  have e : ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).add t ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) ((ring (2 * (5 * n + 4) + 1 + 1)).add t ((ring (2 * (5 * n + 4) + 1 + 1)).neg w))) ((ring (2 * (5 * n + 4) + 1 + 1)).add  -- (+3357 chars; full line in the Zenodo source)
  rw [e, hr]; lib_ring

theorem five_cases (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w) = (ring (2 * (5 * n + 4) + 1 + 1))  -- (+5 chars; full line in the Zenodo source)
    (t : Zp (2 * (5 * n + 4) + 1 + 1)) (ht : ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul t t) t) t) t) = (ring (2 * (5 * n + 4) + 1 + 1)).one) :
    t = (ring (2 * (5 * n + 4) + 1 + 1)).one ∨ t = w ∨ t = ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) ∨ t = ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) ∨ t = ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) := by
  have h0 : ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).add t ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) ((ring (2 * (5 * n + 4) + 1 + 1)).add t ((ring (2 * (5 * n + 4) + 1 + 1)).neg w))) ((ring (2 * (5 * n + 4) + 1 + 1)).ad  -- (+560 chars; full line in the Zenodo source)
  have sub : ∀ a b : Zp (2 * (5 * n + 4) + 1 + 1), ((ring (2 * (5 * n + 4) + 1 + 1)).add a ((ring (2 * (5 * n + 4) + 1 + 1)).neg b)) = (ring (2 * (5 * n + 4) + 1 + 1)).zero → a = b := fun a b e => by
    rw [show a = ((ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).add a ((ring (2 * (5 * n + 4) + 1 + 1)).neg b)) b) by lib_ring, e]; lib_ring
  rcases G4Gen.mul_eq_zero (5 * n + 4) hpr _ _ h0 with e1 | e1
  · rcases G4Gen.mul_eq_zero (5 * n + 4) hpr _ _ e1 with e2 | e2
    · rcases G4Gen.mul_eq_zero (5 * n + 4) hpr _ _ e2 with e3 | e3
      · rcases G4Gen.mul_eq_zero (5 * n + 4) hpr _ _ e3 with e4 | e4
        · exact Or.inl (sub _ _ e4)
        · exact Or.inr (Or.inl (sub _ _ e4))
      · exact Or.inr (Or.inr (Or.inl (sub _ _ e3)))
    · exact Or.inr (Or.inr (Or.inr (Or.inl (sub _ _ e2))))
  · exact Or.inr (Or.inr (Or.inr (Or.inr (sub _ _ e1))))

end Quint1

#print axioms Quint1.r_zero
#print axioms Quint1.fact5
#print axioms Quint1.five_cases

#print axioms: 3 theorems, no axioms.

78.174 Frobenius on \(\mathbb F_q[\zeta _5]\)

For every prime \(q\) the \(q\)-th power in \(\mathbb F_q[\zeta _5]=\mathbb F_q[\zeta ]/(1+\zeta +\zeta ^2+\zeta ^3+\zeta ^4)\) acts on coefficients.

\[ (a_0+a_1\zeta +a_2\zeta ^2+a_3\zeta ^3)^q=a_0+a_1\zeta ^q+a_2\zeta ^{2q}+a_3\zeta ^{3q}. \]

lang/Quint2.lean

/-
  lang/Quint2 — T49, cycle 749 (ℓ = 5 symbol project, step 2): Frobenius on K[ζ₅] = Cyclo5.cyc5.
    sc5, sc5_mul, sc5_pow   K ⊂ K[ζ₅]
    zt                      ζ = (0, 1, 0, 0)
    decomp5                 (a₀, a₁, a₂, a₃) = a₀ + a₁ζ + a₂ζ² + a₃ζ³
    charq5                  q · 1 = 0 in 𝔽_q[ζ₅]
    frob5                   ((a₀, a₁, a₂, a₃))^q = a₀ + a₁ζ^q + a₂(ζ²)^q + a₃(ζ³)^q in 𝔽_q[ζ₅], q prime (frob_add, Fermat)
  Component identities generated (scratchpad c5gen.py) and closed by lib_ring.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Cyclo5
import lang.CubicRecip

namespace Quint2

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

section Gen
variable {R : Type} (K : DetChain.CRing R)

def sc5 (a : R) : R × R × R × R := (a, K.zero, K.zero, K.zero)

def zt : R × R × R × R := (K.zero, K.one, K.zero, K.zero)

theorem sc5_mul (a b : R) : (Cyclo5.cyc5 K).mul (sc5 K a) (sc5 K b) = sc5 K (K.mul a b) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul a b) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero))) = (K.mul a b)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul a K.zero) (K.mul K.zero b)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) (K.mul K.zero K.zero)) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul a K.zero) (K.mul K.zero K.zero)) (K.mul K.zero b)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) = K.zero
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul a K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)) (K.mul K.zero b)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) = K.zero
    lib_ring

theorem sc5_pow (a : R) : ∀ m, pw (Cyclo5.cyc5 K) (sc5 K a) m = sc5 K (pw K a m)
  | 0 => rfl
  | m + 1 => by
    show (Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (sc5 K a) m) (sc5 K a) = sc5 K (K.mul (pw K a m) a)
    rw [sc5_pow a m, sc5_mul]

theorem decomp5 (a0 a1 a2 a3 : R) : ((a0, a1, a2, a3) : R × R × R × R) =
    (Cyclo5.cyc5 K).add ((Cyclo5.cyc5 K).add ((Cyclo5.cyc5 K).add (sc5 K a0) ((Cyclo5.cyc5 K).mul (sc5 K a1) (zt K)))
      ((Cyclo5.cyc5 K).mul (sc5 K a2) ((Cyclo5.cyc5 K).mul (zt K) (zt K))))
      ((Cyclo5.cyc5 K).mul (sc5 K a3) ((Cyclo5.cyc5 K).mul ((Cyclo5.cyc5 K).mul (zt K) (zt K)) (zt K))) := by
  apply Prod.ext
  · show a0 = (K.add (K.add (K.add a0 (K.add (K.add (K.mul a1 K.zero) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)))) (K.add (K.add (K.mul a2 (K.add (K.add (K.mul K.zero K.zero) (K.neg (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) (K.add (K.mul K.zero K.zero) (K.mul K  -- (+9064 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show a1 = (K.add (K.add (K.add K.zero (K.add (K.add (K.add (K.mul a1 K.one) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) (K.mul K.zero K.zero))) (K.add (K.add (K.add (K.mul a2 (K.add (K.add (K.add (K.mul K.zero K.one) (K.mul K.one K.zero)) (K.neg (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one))))  -- (+9097 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show a2 = (K.add (K.add (K.add K.zero (K.add (K.add (K.add (K.mul a1 K.zero) (K.mul K.zero K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one))))) (K.add (K.add (K.add (K.mul a2 (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero))   -- (+8838 chars; full line in the Zenodo source)
    lib_ring
  · show a3 = (K.add (K.add (K.add K.zero (K.add (K.add (K.add (K.add (K.mul a1 K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one))))) (K.add (K.add (K.add (K.add (K.mul a2 (K.add (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.mul K.zero K.one)) (K.mul K.zero K.zero)  -- (+10573 chars; full line in the Zenodo source)
    lib_ring

end Gen

section Fq
variable (j : Nat)

theorem charq5 : Frobenius.nsm (Cyclo5.cyc5 (ring (j + 1))) (j + 1 + 1) (Cyclo5.cyc5 (ring (j + 1))).one = (Cyclo5.cyc5 (ring (j + 1))).zero := by
  have h := char_p (j + 1)
  have hn : ∀ m, Frobenius.nsm (Cyclo5.cyc5 (ring (j + 1))) m (Cyclo5.cyc5 (ring (j + 1))).one =
      (Frobenius.nsm (ring (j + 1)) m (ring (j + 1)).one, Frobenius.nsm (ring (j + 1)) m (ring (j + 1)).zero,
        Frobenius.nsm (ring (j + 1)) m (ring (j + 1)).zero, Frobenius.nsm (ring (j + 1)) m (ring (j + 1)).zero) := fun m => by
    induction m with
    | zero => rfl
    | succ m ih =>
      show (Cyclo5.cyc5 (ring (j + 1))).add (Frobenius.nsm (Cyclo5.cyc5 (ring (j + 1))) m (Cyclo5.cyc5 (ring (j + 1))).one) (Cyclo5.cyc5 (ring (j + 1))).one = _
      rw [ih]; rfl
  rw [hn, h]
  have z : ∀ m, Frobenius.nsm (ring (j + 1)) m (ring (j + 1)).zero = (ring (j + 1)).zero := fun m => by
    induction m with
    | zero => rfl
    | succ m ih => show (ring (j + 1)).add (Frobenius.nsm (ring (j + 1)) m (ring (j + 1)).zero) (ring (j + 1)).zero = _; rw [ih, DetChain.add_zero' (ring (j + 1))]
  rw [z]; rfl

/-- **Frobenius on 𝔽_q[ζ₅]: (a₀ + a₁ζ + a₂ζ² + a₃ζ³)^q = a₀ + a₁ζ^q + a₂(ζ²)^q + a₃(ζ³)^q.** -/
theorem frob5 (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (a0 a1 a2 a3 : Zp (j + 1)) :
    pw (Cyclo5.cyc5 (ring (j + 1))) ((a0, a1, a2, a3) : Zp (j + 1) × Zp (j + 1) × Zp (j + 1) × Zp (j + 1)) (j + 1 + 1) =
      (Cyclo5.cyc5 (ring (j + 1))).add ((Cyclo5.cyc5 (ring (j + 1))).add ((Cyclo5.cyc5 (ring (j + 1))).add (sc5 (ring (j + 1)) a0)
        ((Cyclo5.cyc5 (ring (j + 1))).mul (sc5 (ring (j + 1)) a1) (pw (Cyclo5.cyc5 (ring (j + 1))) (zt (ring (j + 1))) (j + 1 + 1))))
        ((Cyclo5.cyc5 (ring (j + 1))).mul (sc5 (ring (j + 1)) a2)
          (pw (Cyclo5.cyc5 (ring (j + 1))) ((Cyclo5.cyc5 (ring (j + 1))).mul (zt (ring (j + 1))) (zt (ring (j + 1)))) (j + 1 + 1))))
        ((Cyclo5.cyc5 (ring (j + 1))).mul (sc5 (ring (j + 1)) a3)
          (pw (Cyclo5.cyc5 (ring (j + 1))) ((Cyclo5.cyc5 (ring (j + 1))).mul ((Cyclo5.cyc5 (ring (j + 1))).mul (zt (ring (j + 1))) (zt (ring (j + 1)))) (zt (ring (j + 1)))) (j + 1 + 1))) := by
  rw [decomp5 (ring (j + 1)) a0 a1 a2 a3]
  rw [Frobenius.frob_add (Cyclo5.cyc5 (ring (j + 1))) j hq (charq5 j), Frobenius.frob_add (Cyclo5.cyc5 (ring (j + 1))) j hq (charq5 j),
    Frobenius.frob_add (Cyclo5.cyc5 (ring (j + 1))) j hq (charq5 j),
    Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) (sc5 (ring (j + 1)) a1) (zt (ring (j + 1))),
    Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) (sc5 (ring (j + 1)) a2) ((Cyclo5.cyc5 (ring (j + 1))).mul (zt (ring (j + 1))) (zt (ring (j + 1)))),
    Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) (sc5 (ring (j + 1)) a3) ((Cyclo5.cyc5 (ring (j + 1))).mul ((Cyclo5.cyc5 (ring (j + 1))).mul (zt (ring (j + 1))) (zt (ring (j + 1)))) (zt (ring (j + 1)))),
    sc5_pow, sc5_pow, sc5_pow, sc5_pow,
    fermat j hq a0, fermat j hq a1, fermat j hq a2, fermat j hq a3]

end Fq

end Quint2
#print axioms Quint2.sc5
#print axioms Quint2.zt
#print axioms Quint2.sc5_mul
#print axioms Quint2.sc5_pow
#print axioms Quint2.decomp5
#print axioms Quint2.charq5
#print axioms Quint2.frob5

#print axioms: 7 theorems, no axioms.

78.175 Explicit Frobenius on \(\mathbb F_q[\zeta _5]\)

For a prime \(q=5m+r\) the \(q\)-th power on \(\mathbb F_q[\zeta _5]\) is the identity (\(r=1\)) or \(\zeta \mapsto \zeta ^r\), in coordinates.

\[ \zeta ^{5m+r}=\zeta ^r,\qquad (a_0+a_1\zeta +a_2\zeta ^2+a_3\zeta ^3)^q=a_0+a_1\zeta ^r+a_2\zeta ^{2r}+a_3\zeta ^{3r}. \]

lang/Quint3.lean

/-
  lang/Quint3 — T49, cycle 750 (ℓ = 5 symbol project, step 3): ζ^q by q mod 5 and the explicit Frobenius on 𝔽_q[ζ₅].
    zp1 … zp4, z5       ζ^r as explicit tuples, ζ⁵ = 1
    zz_*                ζ^a ζ^b = ζ^(a+b mod 5) for the needed pairs
    scz1 … scz4         a·ζ^k as explicit tuples
    pw_add5, zt_mod     ζ^(5m + r) = ζ^r
    lin_1 … lin_4       a₀ + a₁ζ^r + a₂ζ^(2r) + a₃ζ^(3r) as an explicit tuple
    frob5_1 … frob5_4   q = 5m + r prime: (a₀, a₁, a₂, a₃)^q is the identity (r = 1) or the Galois action ζ ↦ ζ^r
  Small component identities (generated, scratchpad c5gen.py), closed by lib_ring (split from a 25 GB single version).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Quint2

namespace Quint3

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius Quint2

section Gen
variable {R : Type} (K : DetChain.CRing R)

def Zk : Nat → R × R × R × R
  | 0 => (K.one, K.zero, K.zero, K.zero)
  | 1 => (K.zero, K.one, K.zero, K.zero)
  | 2 => (K.zero, K.zero, K.one, K.zero)
  | 3 => (K.zero, K.zero, K.zero, K.one)
  | _ => (K.neg K.one, K.neg K.one, K.neg K.one, K.neg K.one)

theorem zp1 : pw (Cyclo5.cyc5 K) (zt K) 1 = (K.zero, K.one, K.zero, K.zero) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul K.one K.zero) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.one K.one) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) (K.mul K.zero K.zero)) = K.one
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) = K.zero
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) = K.zero
    lib_ring

theorem zp2 : pw (Cyclo5.cyc5 K) (zt K) 2 = (K.zero, K.zero, K.one, K.zero) := by
  show (Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (zt K) 1) (zt K) = _
  rw [zp1]
  apply Prod.ext
  · show (K.add (K.add (K.mul K.zero K.zero) (K.neg (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.one) (K.mul K.one K.zero)) (K.neg (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) (K.mul K.zero K.zero)) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) = K.one
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.mul K.zero K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) = K.zero
    lib_ring

theorem zp3 : pw (Cyclo5.cyc5 K) (zt K) 3 = (K.zero, K.zero, K.zero, K.one) := by
  show (Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (zt K) 2) (zt K) = _
  rw [zp2]
  apply Prod.ext
  · show (K.add (K.add (K.mul K.zero K.zero) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.mul K.zero K.one)))) (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.mul K.zero K.one)))) (K.mul K.zero K.zero)) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.one)) (K.mul K.one K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.mul K.zero K.one)))) = K.zero
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.one K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.mul K.zero K.one)))) = K.one
    lib_ring

theorem zp4 : pw (Cyclo5.cyc5 K) (zt K) 4 = ((K.neg K.one), (K.neg K.one), (K.neg K.one), (K.neg K.one)) := by
  show (Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (zt K) 3) (zt K) = _
  rw [zp3]
  apply Prod.ext
  · show (K.add (K.add (K.mul K.zero K.zero) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.one K.one)))) (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero))) = (K.neg K.one)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.one K.one)))) (K.mul K.one K.zero)) = (K.neg K.one)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.one K.one)))) = (K.neg K.one)
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)) (K.mul K.one K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.one K.one)))) = (K.neg K.one)
    lib_ring

theorem z5 : pw (Cyclo5.cyc5 K) (zt K) 5 = (K.one, K.zero, K.zero, K.zero) := by
  show (Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (zt K) 4) (zt K) = _
  rw [zp4]
  apply Prod.ext
  · show (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.neg (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.zero)) (K.mul (K.neg K.one) K.one)))) (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.zero))) = K.one
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul (K.neg K.one) K.one) (K.mul (K.neg K.one) K.zero)) (K.neg (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.zero)) (K.mul (K.neg K.one) K.one)))) (K.mul (K.neg K.one) K.zero)) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.one)) (K.mul (K.neg K.one) K.zero)) (K.neg (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.zero)) (K.mul (K.neg K.one) K.one)))) = K.zero
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.zero)) (K.mul (K.neg K.one) K.one)) (K.mul (K.neg K.one) K.zero)) (K.neg (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.zero)) (K.mul (K.neg K.one) K.one)))) = K.zero
    lib_ring

theorem zz11 : (Cyclo5.cyc5 K).mul (K.zero, K.one, K.zero, K.zero) (K.zero, K.one, K.zero, K.zero) = (K.zero, K.zero, K.one, K.zero) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul K.zero K.zero) (K.neg (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.one) (K.mul K.one K.zero)) (K.neg (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) (K.mul K.zero K.zero)) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) = K.one
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.mul K.zero K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) = K.zero
    lib_ring

theorem zz13 : (Cyclo5.cyc5 K).mul (K.zero, K.one, K.zero, K.zero) (K.zero, K.zero, K.zero, K.one) = ((K.neg K.one), (K.neg K.one), (K.neg K.one), (K.neg K.one)) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul K.zero K.zero) (K.neg (K.add (K.add (K.mul K.one K.one) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero))) = (K.neg K.one)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.neg (K.add (K.add (K.mul K.one K.one) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) (K.mul K.zero K.one)) = (K.neg K.one)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.one K.one) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) = (K.neg K.one)
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul K.zero K.one) (K.mul K.one K.zero)) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.one K.one) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) = (K.neg K.one)
    lib_ring

theorem zz21 : (Cyclo5.cyc5 K).mul (K.zero, K.zero, K.one, K.zero) (K.zero, K.one, K.zero, K.zero) = (K.zero, K.zero, K.zero, K.one) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul K.zero K.zero) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.mul K.zero K.one)))) (K.add (K.mul K.one K.zero) (K.mul K.zero K.zero))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.mul K.zero K.one)))) (K.mul K.zero K.zero)) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.one)) (K.mul K.one K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.mul K.zero K.one)))) = K.zero
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.one K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.zero)) (K.mul K.zero K.one)))) = K.one
    lib_ring

theorem zz22 : (Cyclo5.cyc5 K).mul (K.zero, K.zero, K.one, K.zero) (K.zero, K.zero, K.one, K.zero) = ((K.neg K.one), (K.neg K.one), (K.neg K.one), (K.neg K.one)) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul K.zero K.zero) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.one)) (K.mul K.zero K.zero)))) (K.add (K.mul K.one K.zero) (K.mul K.zero K.one))) = (K.neg K.one)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.one)) (K.mul K.zero K.zero)))) (K.mul K.zero K.zero)) = (K.neg K.one)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.mul K.one K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.one)) (K.mul K.zero K.zero)))) = (K.neg K.one)
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.one)) (K.mul K.one K.zero)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.one K.one)) (K.mul K.zero K.zero)))) = (K.neg K.one)
    lib_ring

theorem zz33 : (Cyclo5.cyc5 K).mul (K.zero, K.zero, K.zero, K.one) (K.zero, K.zero, K.zero, K.one) = (K.zero, K.one, K.zero, K.zero) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul K.zero K.zero) (K.neg (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.mul K.one K.zero)))) (K.add (K.mul K.zero K.one) (K.mul K.one K.zero))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.mul K.one K.zero)))) (K.mul K.one K.one)) = K.one
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.mul K.one K.zero)))) = K.zero
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)) (K.mul K.one K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.mul K.one K.zero)))) = K.zero
    lib_ring

theorem zz34 : (Cyclo5.cyc5 K).mul (K.zero, K.zero, K.zero, K.one) ((K.neg K.one), (K.neg K.one), (K.neg K.one), (K.neg K.one)) = (K.zero, K.zero, K.one, K.zero) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul K.zero (K.neg K.one)) (K.neg (K.add (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.mul K.one (K.neg K.one))))) (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.one (K.neg K.one)))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.neg (K.add (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.mul K.one (K.neg K.one))))) (K.mul K.one (K.neg K.one))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.mul K.zero (K.neg K.one))) (K.neg (K.add (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.mul K.one (K.neg K.one))))) = K.one
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.mul K.zero (K.neg K.one))) (K.mul K.one (K.neg K.one))) (K.neg (K.add (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.mul K.one (K.neg K.one))))) = K.zero
    lib_ring

theorem zz42 : (Cyclo5.cyc5 K).mul ((K.neg K.one), (K.neg K.one), (K.neg K.one), (K.neg K.one)) (K.zero, K.zero, K.one, K.zero) = (K.zero, K.one, K.zero, K.zero) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.neg (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.one)) (K.mul (K.neg K.one) K.zero)))) (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.one))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.zero)) (K.neg (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.one)) (K.mul (K.neg K.one) K.zero)))) (K.mul (K.neg K.one) K.zero)) = K.one
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul (K.neg K.one) K.one) (K.mul (K.neg K.one) K.zero)) (K.mul (K.neg K.one) K.zero)) (K.neg (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.one)) (K.mul (K.neg K.one) K.zero)))) = K.zero
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.one)) (K.mul (K.neg K.one) K.zero)) (K.mul (K.neg K.one) K.zero)) (K.neg (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul (K.neg K.one) K.one)) (K.mul (K.neg K.one) K.zero)))) = K.zero
    lib_ring

theorem zz44 : (Cyclo5.cyc5 K).mul ((K.neg K.one), (K.neg K.one), (K.neg K.one), (K.neg K.one)) ((K.neg K.one), (K.neg K.one), (K.neg K.one), (K.neg K.one)) = (K.zero, K.zero, K.zero, K.one) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.neg (K.add (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.mul (K.neg K.one) (K.neg K.one))) (K.mul (K.neg K.one) (K.neg K.one))))) (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.mul (K.neg K.one) (K.neg K.one)))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.mul (K.neg K.one) (K.neg K.one))) (K.neg (K.add (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.mul (K.neg K.one) (K.neg K.one))) (K.mul (K.neg K.one) (K.neg K.one))))) (K.mul (K.neg K.one) (K.neg K.one))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.mul (K.neg K.one) (K.neg K.one))) (K.mul (K.neg K.one) (K.neg K.one))) (K.neg (K.add (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.mul (K.neg K.one) (K.neg K.one))) (K.mul (K.neg K.one) (K.neg K.one))))) = K.zero
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.mul (K.neg K.one) (K.neg K.one))) (K.mul (K.neg K.one) (K.neg K.one))) (K.mul (K.neg K.one) (K.neg K.one))) (K.neg (K.add (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.mul (K.neg K.one) (K.neg K.one))) (K.mul (K.neg K.one) (K.neg K.one))))) = K.one
    lib_ring

theorem scz1 (a : R) : (Cyclo5.cyc5 K).mul (sc5 K a) (K.zero, K.one, K.zero, K.zero) = (K.zero, a, K.zero, K.zero) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul a K.zero) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul a K.one) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) (K.mul K.zero K.zero)) = a
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul a K.zero) (K.mul K.zero K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) = K.zero
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul a K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.one)))) = K.zero
    lib_ring

theorem scz2 (a : R) : (Cyclo5.cyc5 K).mul (sc5 K a) (K.zero, K.zero, K.one, K.zero) = (K.zero, K.zero, a, K.zero) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul a K.zero) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.one)) (K.mul K.zero K.zero)))) (K.add (K.mul K.zero K.zero) (K.mul K.zero K.one))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul a K.zero) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.one)) (K.mul K.zero K.zero)))) (K.mul K.zero K.zero)) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul a K.one) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.one)) (K.mul K.zero K.zero)))) = a
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul a K.zero) (K.mul K.zero K.one)) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.one)) (K.mul K.zero K.zero)))) = K.zero
    lib_ring

theorem scz3 (a : R) : (Cyclo5.cyc5 K).mul (sc5 K a) (K.zero, K.zero, K.zero, K.one) = (K.zero, K.zero, K.zero, a) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul a K.zero) (K.neg (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul a K.zero) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) (K.mul K.zero K.one)) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul a K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) = K.zero
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul a K.one) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)) (K.neg (K.add (K.add (K.mul K.zero K.one) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) = a
    lib_ring

theorem scz4 (a : R) : (Cyclo5.cyc5 K).mul (sc5 K a) ((K.neg K.one), (K.neg K.one), (K.neg K.one), (K.neg K.one)) = ((K.neg a), (K.neg a), (K.neg a), (K.neg a)) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul a (K.neg K.one)) (K.neg (K.add (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.mul K.zero (K.neg K.one))))) (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.zero (K.neg K.one)))) = (K.neg a)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul a (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.neg (K.add (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.mul K.zero (K.neg K.one))))) (K.mul K.zero (K.neg K.one))) = (K.neg a)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul a (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.mul K.zero (K.neg K.one))) (K.neg (K.add (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.mul K.zero (K.neg K.one))))) = (K.neg a)
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul a (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.mul K.zero (K.neg K.one))) (K.mul K.zero (K.neg K.one))) (K.neg (K.add (K.add (K.mul K.zero (K.neg K.one)) (K.mul K.zero (K.neg K.one))) (K.mul K.zero (K.neg K.one))))) = (K.neg a)
    lib_ring

theorem pw_add5 {S : Type} (G : DetChain.CRing S) (x : S) (a : Nat) : ∀ b, pw G x (a + b) = G.mul (pw G x a) (pw G x b)
  | 0 => by show pw G x a = G.mul (pw G x a) G.one; rw [G.mul_comm, G.one_mul]
  | b + 1 => by
    show G.mul (pw G x (a + b)) x = G.mul (pw G x a) (G.mul (pw G x b) x)
    rw [pw_add5 G x a b, G.mul_assoc]

theorem zt_mod (m r : Nat) : pw (Cyclo5.cyc5 K) (zt K) (5 * m + r) = pw (Cyclo5.cyc5 K) (zt K) r := by
  rw [pw_add5, CubicRecip.pw_mul_split, z5]
  exact (congrArg (fun t => (Cyclo5.cyc5 K).mul t (pw (Cyclo5.cyc5 K) (zt K) r))
    (CubicChar.pw_one'' (Cyclo5.cyc5 K) m)).trans ((Cyclo5.cyc5 K).one_mul _)

theorem lin_1 (a0 a1 a2 a3 : R) :
    (Cyclo5.cyc5 K).add ((Cyclo5.cyc5 K).add ((Cyclo5.cyc5 K).add (sc5 K a0) ((Cyclo5.cyc5 K).mul (sc5 K a1) (pw (Cyclo5.cyc5 K) (zt K) 1))) ((Cyclo5.cyc5 K).mul (sc5 K a2) ((Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (zt K) 1) (pw (Cyclo5.cyc5 K) (zt K) 1))))
      ((Cyclo5.cyc5 K).mul (sc5 K a3) ((Cyclo5.cyc5 K).mul ((Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (zt K) 1) (pw (Cyclo5.cyc5 K) (zt K) 1)) (pw (Cyclo5.cyc5 K) (zt K) 1))) = (a0, a1, a2, a3) := by
  rw [zp1, zz11, zz21, scz1, scz2, scz3]
  apply Prod.ext
  · show (K.add (K.add (K.add a0 K.zero) K.zero) K.zero) = a0
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add K.zero a1) K.zero) K.zero) = a1
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add K.zero K.zero) a2) K.zero) = a2
    lib_ring
  · show (K.add (K.add (K.add K.zero K.zero) K.zero) a3) = a3
    lib_ring

theorem lin_2 (a0 a1 a2 a3 : R) :
    (Cyclo5.cyc5 K).add ((Cyclo5.cyc5 K).add ((Cyclo5.cyc5 K).add (sc5 K a0) ((Cyclo5.cyc5 K).mul (sc5 K a1) (pw (Cyclo5.cyc5 K) (zt K) 2))) ((Cyclo5.cyc5 K).mul (sc5 K a2) ((Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (zt K) 2) (pw (Cyclo5.cyc5 K) (zt K) 2))))
      ((Cyclo5.cyc5 K).mul (sc5 K a3) ((Cyclo5.cyc5 K).mul ((Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (zt K) 2) (pw (Cyclo5.cyc5 K) (zt K) 2)) (pw (Cyclo5.cyc5 K) (zt K) 2))) = ((K.add a0 (K.neg a2)), (K.add (K.neg a2) a3), (K.add a1 (K.neg a2)), (K.neg a2)) := by
  rw [zp2, zz22, zz42, scz2, scz4, scz1]
  apply Prod.ext
  · show (K.add (K.add (K.add a0 K.zero) (K.neg a2)) K.zero) = (K.add a0 (K.neg a2))
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add K.zero K.zero) (K.neg a2)) a3) = (K.add (K.neg a2) a3)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add K.zero a1) (K.neg a2)) K.zero) = (K.add a1 (K.neg a2))
    lib_ring
  · show (K.add (K.add (K.add K.zero K.zero) (K.neg a2)) K.zero) = (K.neg a2)
    lib_ring

theorem lin_3 (a0 a1 a2 a3 : R) :
    (Cyclo5.cyc5 K).add ((Cyclo5.cyc5 K).add ((Cyclo5.cyc5 K).add (sc5 K a0) ((Cyclo5.cyc5 K).mul (sc5 K a1) (pw (Cyclo5.cyc5 K) (zt K) 3))) ((Cyclo5.cyc5 K).mul (sc5 K a2) ((Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (zt K) 3) (pw (Cyclo5.cyc5 K) (zt K) 3))))
      ((Cyclo5.cyc5 K).mul (sc5 K a3) ((Cyclo5.cyc5 K).mul ((Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (zt K) 3) (pw (Cyclo5.cyc5 K) (zt K) 3)) (pw (Cyclo5.cyc5 K) (zt K) 3))) = ((K.add a0 (K.neg a3)), (K.add a2 (K.neg a3)), (K.neg a3), (K.add a1 (K.neg a3))) := by
  rw [zp3, zz33, zz13, scz3, scz1, scz4]
  apply Prod.ext
  · show (K.add (K.add (K.add a0 K.zero) K.zero) (K.neg a3)) = (K.add a0 (K.neg a3))
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add K.zero K.zero) a2) (K.neg a3)) = (K.add a2 (K.neg a3))
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add K.zero K.zero) K.zero) (K.neg a3)) = (K.neg a3)
    lib_ring
  · show (K.add (K.add (K.add K.zero a1) K.zero) (K.neg a3)) = (K.add a1 (K.neg a3))
    lib_ring

theorem lin_4 (a0 a1 a2 a3 : R) :
    (Cyclo5.cyc5 K).add ((Cyclo5.cyc5 K).add ((Cyclo5.cyc5 K).add (sc5 K a0) ((Cyclo5.cyc5 K).mul (sc5 K a1) (pw (Cyclo5.cyc5 K) (zt K) 4))) ((Cyclo5.cyc5 K).mul (sc5 K a2) ((Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (zt K) 4) (pw (Cyclo5.cyc5 K) (zt K) 4))))
      ((Cyclo5.cyc5 K).mul (sc5 K a3) ((Cyclo5.cyc5 K).mul ((Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (zt K) 4) (pw (Cyclo5.cyc5 K) (zt K) 4)) (pw (Cyclo5.cyc5 K) (zt K) 4))) = ((K.add a0 (K.neg a1)), (K.neg a1), (K.add (K.neg a1) a3), (K.add (K.neg a1) a2)) := by
  rw [zp4, zz44, zz34, scz4, scz3, scz2]
  apply Prod.ext
  · show (K.add (K.add (K.add a0 (K.neg a1)) K.zero) K.zero) = (K.add a0 (K.neg a1))
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add K.zero (K.neg a1)) K.zero) K.zero) = (K.neg a1)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add K.zero (K.neg a1)) K.zero) a3) = (K.add (K.neg a1) a3)
    lib_ring
  · show (K.add (K.add (K.add K.zero (K.neg a1)) a2) K.zero) = (K.add (K.neg a1) a2)
    lib_ring

end Gen

section Fq
variable (j : Nat)

theorem frob5_1 (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (m : Nat) (hm : j + 1 + 1 = 5 * m + 1) (a0 a1 a2 a3 : Zp (j + 1)) :
    pw (Cyclo5.cyc5 (ring (j + 1))) ((a0, a1, a2, a3) : Zp (j + 1) × Zp (j + 1) × Zp (j + 1) × Zp (j + 1)) (j + 1 + 1) = (a0, a1, a2, a3) := by
  rw [frob5 j hq, Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) (zt (ring (j + 1))) (zt (ring (j + 1))), Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) ((Cyclo5.cyc5 (ring (j + 1))).mul (zt (ring (j + 1))) (zt (ring (j + 1)))) (zt (ring (j + 1))), Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) (zt (ring (j + 1))) (zt (ring (j + 1))), hm, zt_mod]
  exact lin_1 (ring (j + 1)) a0 a1 a2 a3

theorem frob5_2 (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (m : Nat) (hm : j + 1 + 1 = 5 * m + 2) (a0 a1 a2 a3 : Zp (j + 1)) :
    pw (Cyclo5.cyc5 (ring (j + 1))) ((a0, a1, a2, a3) : Zp (j + 1) × Zp (j + 1) × Zp (j + 1) × Zp (j + 1)) (j + 1 + 1) = (((ring (j + 1)).add a0 ((ring (j + 1)).neg a2)), ((ring (j + 1)).add ((ring (j + 1)).neg a2) a3), ((ring (j + 1)).add a1 ((ring (j + 1)).neg a2)), ((ring (j + 1)).neg a2)) := by
  rw [frob5 j hq, Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) (zt (ring (j + 1))) (zt (ring (j + 1))), Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) ((Cyclo5.cyc5 (ring (j + 1))).mul (zt (ring (j + 1))) (zt (ring (j + 1)))) (zt (ring (j + 1))), Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) (zt (ring (j + 1))) (zt (ring (j + 1))), hm, zt_mod]
  exact lin_2 (ring (j + 1)) a0 a1 a2 a3

theorem frob5_3 (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (m : Nat) (hm : j + 1 + 1 = 5 * m + 3) (a0 a1 a2 a3 : Zp (j + 1)) :
    pw (Cyclo5.cyc5 (ring (j + 1))) ((a0, a1, a2, a3) : Zp (j + 1) × Zp (j + 1) × Zp (j + 1) × Zp (j + 1)) (j + 1 + 1) = (((ring (j + 1)).add a0 ((ring (j + 1)).neg a3)), ((ring (j + 1)).add a2 ((ring (j + 1)).neg a3)), ((ring (j + 1)).neg a3), ((ring (j + 1)).add a1 ((ring (j + 1)).neg a3))) := by
  rw [frob5 j hq, Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) (zt (ring (j + 1))) (zt (ring (j + 1))), Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) ((Cyclo5.cyc5 (ring (j + 1))).mul (zt (ring (j + 1))) (zt (ring (j + 1)))) (zt (ring (j + 1))), Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) (zt (ring (j + 1))) (zt (ring (j + 1))), hm, zt_mod]
  exact lin_3 (ring (j + 1)) a0 a1 a2 a3

theorem frob5_4 (hq : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (m : Nat) (hm : j + 1 + 1 = 5 * m + 4) (a0 a1 a2 a3 : Zp (j + 1)) :
    pw (Cyclo5.cyc5 (ring (j + 1))) ((a0, a1, a2, a3) : Zp (j + 1) × Zp (j + 1) × Zp (j + 1) × Zp (j + 1)) (j + 1 + 1) = (((ring (j + 1)).add a0 ((ring (j + 1)).neg a1)), ((ring (j + 1)).neg a1), ((ring (j + 1)).add ((ring (j + 1)).neg a1) a3), ((ring (j + 1)).add ((ring (j + 1)).neg a1) a2)) := by
  rw [frob5 j hq, Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) (zt (ring (j + 1))) (zt (ring (j + 1))), Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) ((Cyclo5.cyc5 (ring (j + 1))).mul (zt (ring (j + 1))) (zt (ring (j + 1)))) (zt (ring (j + 1))), Frobenius.pw_mul (Cyclo5.cyc5 (ring (j + 1))) (zt (ring (j + 1))) (zt (ring (j + 1))), hm, zt_mod]
  exact lin_4 (ring (j + 1)) a0 a1 a2 a3

end Fq

end Quint3

#print axioms Quint3.Zk
#print axioms Quint3.zp1
#print axioms Quint3.zp2
#print axioms Quint3.zp3
#print axioms Quint3.zp4
#print axioms Quint3.z5
#print axioms Quint3.zz11
#print axioms Quint3.zz13
#print axioms Quint3.zz21
#print axioms Quint3.zz22
#print axioms Quint3.zz33
#print axioms Quint3.zz34
#print axioms Quint3.zz42
#print axioms Quint3.zz44
#print axioms Quint3.scz1
#print axioms Quint3.scz2
#print axioms Quint3.scz3
#print axioms Quint3.scz4
#print axioms Quint3.pw_add5
#print axioms Quint3.zt_mod
#print axioms Quint3.lin_1
#print axioms Quint3.lin_2
#print axioms Quint3.lin_3
#print axioms Quint3.lin_4
#print axioms Quint3.frob5_1
#print axioms Quint3.frob5_2
#print axioms Quint3.frob5_3
#print axioms Quint3.frob5_4

#print axioms: 28 theorems, no axioms.

78.176 The quintic character of \(\mathbb F_p\)

For \(p=10n+11\) and \(w^5=1\neq w\) the character \(\chi (x)=\zeta ^i\) when \(x^{(p-1)/5}=w^i\) is multiplicative, even, and sums to zero.

\[ \chi (xy)=\chi (x)\chi (y),\qquad \chi (-1)=1,\qquad \sum _{x\in \mathbb F_p}\chi (x)=0. \]

lang/Quint4.lean

/-
  lang/Quint4 — T49, cycle 751 (ℓ = 5 symbol project, step 4): the quintic character of 𝔽_p with values in ℤ[ζ₅].

  p = 10n + 11 prime, N = (p − 1)/5 = 2n + 2, w ∈ 𝔽_p with w ≠ 1, w⁵ = 1. For x ≠ 0, x^N is a fifth root of unity, so
  x^N = w^i with i < 5 (Quint1.five_cases), and χ(x) = ζ^i in ℤ[ζ₅] = Cyclo5.cyc5 ℤ (χ(0) = 0).
    w5, wfix, wk_ne_one       w⁵ = 1, w^(5r+1) = w, w^k ≠ 1 for 0 < k < 5
    pwnz, cancel1, wpow_ne    w^i are pairwise distinct for i < 5
    lg_val                    the index of w^i is i
    exists_i, val_of, reduce  x^N = w^i (i < 5) and then χ(x) = ζ^i
    chi5_mul                  χ(xy) = χ(x)χ(y)
    chi5_neg1                 χ(−1) = 1  (N even)
    zeta_fix, iter            ζS = S ⇒ S = 0 in ℤ[ζ₅]
    chi5_sum                  Σ_x χ(x) = 0
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Quint1
import lang.Quint3
import lang.CubicChar

namespace Quint4

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

variable (n : Nat)

def zeta : Int × Int × Int × Int := Quint2.zt LibInt.intR

def lg (w t : Zp (2 * (5 * n + 4) + 1 + 1)) : Nat :=
  if t = pw (ring (2 * (5 * n + 4) + 1 + 1)) w 0 then 0 else if t = pw (ring (2 * (5 * n + 4) + 1 + 1)) w 1 then 1 else if t = pw (ring (2 * (5 * n + 4) + 1 + 1)) w 2 then 2 else if t = pw (ring (2 * (5 * n + 4) + 1 + 1)) w 3 then 3 else 4

def chi5 (w x : Zp (2 * (5 * n + 4) + 1 + 1)) : Int × Int × Int × Int :=
  if x = (ring (2 * (5 * n + 4) + 1 + 1)).zero then (Cyclo5.cyc5 LibInt.intR).zero else pw (Cyclo5.cyc5 LibInt.intR) zeta (lg n w (pw (ring (2 * (5 * n + 4) + 1 + 1)) x (2 * n + 2)))

theorem w5 (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 5 = (ring (2 * (5 * n + 4) + 1 + 1)).one := by
  show (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul (ring (2 * (5 * n + 4) + 1 + 1)).one w) w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one
  rw [(ring (2 * (5 * n + 4) + 1 + 1)).one_mul]; exact hw5

theorem wfix (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) (r : Nat) : pw (ring (2 * (5 * n + 4) + 1 + 1)) w (5 * r + 1) = w := by
  rw [HasseCM.pw_add', ← TwoPower.pwpow, w5 n w hw5, CubicChar.pw_one'']
  show (ring (2 * (5 * n + 4) + 1 + 1)).mul (ring (2 * (5 * n + 4) + 1 + 1)).one ((ring (2 * (5 * n + 4) + 1 + 1)).mul (ring (2 * (5 * n + 4) + 1 + 1)).one w) = w
  rw [(ring (2 * (5 * n + 4) + 1 + 1)).one_mul, (ring (2 * (5 * n + 4) + 1 + 1)).one_mul]

theorem wk_ne_one (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) (k m r : Nat) (hkm : k * m = 5 * r + 1) : pw (ring (2 * (5 * n + 4) + 1 + 1)) w k ≠ (ri  -- (+53 chars; full line in the Zenodo source)
  have h1 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w (k * m) = (ring (2 * (5 * n + 4) + 1 + 1)).one := by rw [← TwoPower.pwpow, e]; exact CubicChar.pw_one'' (ring (2 * (5 * n + 4) + 1 + 1)) m
  rw [hkm, wfix n w hw5] at h1; exact h1)

theorem w_ne_zero (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).zero := fun e => by
  have h := w5 n w hw5
  rw [e] at h
  have : pw (ring (2 * (5 * n + 4) + 1 + 1)) (ring (2 * (5 * n + 4) + 1 + 1)).zero 5 = (ring (2 * (5 * n + 4) + 1 + 1)).zero := mul_zero (ring (2 * (5 * n + 4) + 1 + 1)) _
  rw [this] at h
  exact PowerSum.one_ne_zero' (2 * (5 * n + 4) + 1) h.symm

theorem pwnz (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (x : Zp (2 * (5 * n + 4) + 1 + 1)) (hx : x ≠ (ring (2 * (5 * n + 4) + 1 + 1)).zero) : ∀ k, pw (ring (2 * (5 * n + 4) + 1 + 1)) x k ≠ (ring (2 * (5 * n + 4) + 1 + 1)).zero
  | 0 => fun e => PowerSum.one_ne_zero' (2 * (5 * n + 4) + 1) e
  | k + 1 => fun e => by
    cases G4Gen.mul_eq_zero (5 * n + 4) hpr (pw (ring (2 * (5 * n + 4) + 1 + 1)) x k) x e with
    | inl h => exact pwnz hpr x hx k h
    | inr h => exact hx h

theorem cancel1 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (a b : Zp (2 * (5 * n + 4) + 1 + 1)) (ha : a ≠ (ring (2 * (5 * n + 4) + 1 + 1)).zero) (h : (ring (2 * (5 * n + 4) + 1 + 1)).mul a b = a) : b = (ring (2 * (5 * n + 4) + 1 + 1)).one := by
  have e : (ring (2 * (5 * n + 4) + 1 + 1)).mul a ((ring (2 * (5 * n + 4) + 1 + 1)).add b ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) = (ring (2 * (5 * n + 4) + 1 + 1)).zero := by
    rw [show (ring (2 * (5 * n + 4) + 1 + 1)).mul a ((ring (2 * (5 * n + 4) + 1 + 1)).add b ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) = (ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).mul a b) ((ring (2 * (5 * n + 4) + 1 + 1)).neg a) by lib_ring, h]; lib_ring
  cases G4Gen.mul_eq_zero (5 * n + 4) hpr _ _ e with
  | inl h' => exact absurd h' ha
  | inr h' =>
    calc b = (ring (2 * (5 * n + 4) + 1 + 1)).add ((ring (2 * (5 * n + 4) + 1 + 1)).add b ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) (ring (2 * (5 * n + 4) + 1 + 1)).one := by lib_ring
      _ = (ring (2 * (5 * n + 4) + 1 + 1)).one := by rw [h']; lib_ring

theorem wpow_ne (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) (i d : Nat) (hd : pw (ring (2 * (5 * n + 4) + 1   -- (+163 chars; full line in the Zenodo source)
  hd (cancel1 n hpr (pw (ring (2 * (5 * n + 4) + 1 + 1)) w i) (pw (ring (2 * (5 * n + 4) + 1 + 1)) w (d + 1)) (pwnz n hpr w (w_ne_zero n w hw5) i) (by rw [← HasseCM.pw_add' (ring (2 * (5 * n + 4) + 1 + 1)) w i (d + 1)]; exact e))

theorem lg_val (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)   -- (+67 chars; full line in the Zenodo source)
  | 0, _ => by unfold lg; rw [ite_pos' _ _ rfl]
  | 1, _ => by
    have n1 := wk_ne_one n w hw1 hw5 1 1 0 rfl
    unfold lg
    rw [ite_neg' _ _ (wpow_ne n hpr w hw5 0 0 n1 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 1 ≠ pw (ring (2 * (5 * n + 4) + 1 + 1)) w 0), ite_pos' _ _ rfl]
  | 2, _ => by
    have n1 := wk_ne_one n w hw1 hw5 1 1 0 rfl
    have n2 := wk_ne_one n w hw1 hw5 2 3 1 rfl
    unfold lg
    rw [ite_neg' _ _ (wpow_ne n hpr w hw5 0 1 n2 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 2 ≠ pw (ring (2 * (5 * n + 4) + 1 + 1)) w 0), ite_neg' _ _ (wpow_ne n hpr w hw5 1 0 n1 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 2 ≠ pw (ring (2 * (5 * n + 4) + 1 + 1)) w 1),
      ite_pos' _ _ rfl]
  | 3, _ => by
    have n1 := wk_ne_one n w hw1 hw5 1 1 0 rfl
    have n2 := wk_ne_one n w hw1 hw5 2 3 1 rfl
    have n3 := wk_ne_one n w hw1 hw5 3 2 1 rfl
    unfold lg
    rw [ite_neg' _ _ (wpow_ne n hpr w hw5 0 2 n3 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 3 ≠ pw (ring (2 * (5 * n + 4) + 1 + 1)) w 0), ite_neg' _ _ (wpow_ne n hpr w hw5 1 1 n2 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 3 ≠ pw (ring (2 * (5 * n + 4) + 1 + 1)) w 1),
      ite_neg' _ _ (wpow_ne n hpr w hw5 2 0 n1 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 3 ≠ pw (ring (2 * (5 * n + 4) + 1 + 1)) w 2), ite_pos' _ _ rfl]
  | 4, _ => by
    have n1 := wk_ne_one n w hw1 hw5 1 1 0 rfl
    have n2 := wk_ne_one n w hw1 hw5 2 3 1 rfl
    have n3 := wk_ne_one n w hw1 hw5 3 2 1 rfl
    have n4 := wk_ne_one n w hw1 hw5 4 4 3 rfl
    unfold lg
    rw [ite_neg' _ _ (wpow_ne n hpr w hw5 0 3 n4 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 4 ≠ pw (ring (2 * (5 * n + 4) + 1 + 1)) w 0), ite_neg' _ _ (wpow_ne n hpr w hw5 1 2 n3 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 4 ≠ pw (ring (2 * (5 * n + 4) + 1 + 1)) w 1),
      ite_neg' _ _ (wpow_ne n hpr w hw5 2 1 n2 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 4 ≠ pw (ring (2 * (5 * n + 4) + 1 + 1)) w 2), ite_neg' _ _ (wpow_ne n hpr w hw5 3 0 n1 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 4 ≠ pw (ring (2 * (5 * n + 4) + 1 + 1)) w 3)]
  | k + 5, hk => absurd hk (fun h => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) h))

theorem xN5 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (x : Zp (2 * (5 * n + 4) + 1 + 1)) (hx : x ≠ (ring (2 * (5 * n + 4) + 1 + 1)).zero) :
    (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul (pw (ring (2 * (5 * n + 4) + 1 + 1)) x (2 * n + 2)) (pw (ring (2 * (5 * n + 4) + 1 + 1)) x (2 * n + 2))) (pw (ring (2 * (5 * n + 4) + 1 + 1)) x (2 * n + 2))) (pw (ring (2 * (5 * n + 4) + 1 + 1)) x (2 * n + 2))) (pw (ring (2 * (5 * n + 4) + 1 + 1  -- (+62 chars; full line in the Zenodo source)
  have e : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul (pw (ring (2 * (5 * n + 4) + 1 + 1)) x (2 * n + 2)) (pw (ring (2 * (5 * n + 4) + 1 + 1)) x (2 * n + 2))) (pw (ring (2 * (5 * n + 4) + 1 + 1)) x (2 * n + 2))) (pw (ring (2 * (5 * n + 4) + 1 + 1)) x (2 * n + 2))) (pw (ring (2 * (5 * n + 4)   -- (+122 chars; full line in the Zenodo source)
    show _ = (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul (ring (2 * (5 * n + 4) + 1 + 1)).one _) _) _) _) _; rw [(ring (2 * (5 * n + 4) + 1 + 1)).one_mul]
  rw [e, TwoPower.pwpow, show (2 * n + 2) * 5 = 2 * (5 * n + 4) + 1 + 1 by lib_nat]
  exact unit_pow (2 * (5 * n + 4) + 1) hpr x hx

theorem exists_i (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+198 chars; full line in the Zenodo source)
  have p1 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 1 = w := (ring (2 * (5 * n + 4) + 1 + 1)).one_mul w
  have p2 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 2 = (ring (2 * (5 * n + 4) + 1 + 1)).mul w w := by show (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul (ring (2 * (5 * n + 4) + 1 + 1)).one w) w = _; rw [(ring (2 * (5 * n + 4) + 1 + 1)).one_mul]
  have p3 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 3 = (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w := by show (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul (ring (2 * (5 * n + 4) + 1 + 1)).one w) w) w = _; rw [(ring (2 * (5 * n + 4) + 1 + 1)).one_mul]
  have p4 : pw (ring (2 * (5 * n + 4) + 1 + 1)) w 4 = (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w := by show (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul (ring (2 * (5 * n + 4) + 1 + 1)).one w) w) w) w = _; rw [(rin  -- (+37 chars; full line in the Zenodo source)
  cases Quint1.five_cases n hpr w hw1 hw5 _ (xN5 n hpr x hx) with
  | inl h => exact ⟨0, Nat.zero_lt_succ 4, h⟩
  | inr h =>
    cases h with
    | inl h => exact ⟨1, Nat.succ_lt_succ (Nat.zero_lt_succ 3), by rw [h, p1]⟩
    | inr h =>
      cases h with
      | inl h => exact ⟨2, Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ 2)), by rw [h, p2]⟩
      | inr h =>
        cases h with
        | inl h => exact ⟨3, Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ 1))), by rw [h, p3]⟩
        | inr h => exact ⟨4, Nat.lt_succ_self 4, by rw [h, p4]⟩

theorem val_of (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)   -- (+206 chars; full line in the Zenodo source)
    chi5 n w x = pw (Cyclo5.cyc5 LibInt.intR) zeta i := by
  unfold chi5
  rw [ite_neg' _ _ hx, h, lg_val n hpr w hw1 hw5 i hi]

theorem reduce (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) : ∀ k, ∃ k', k' < 5 ∧ pw (ring (2 * (5 * n + 4) + 1 + 1)) w k = pw (ring (2 * (5 * n + 4) + 1 + 1)) w k' ∧ pw (Cyclo5.cyc5 LibInt.intR) zet  -- (+42 chars; full line in the Zenodo source)
  | 0 => ⟨0, Nat.zero_lt_succ 4, rfl, rfl⟩
  | k + 1 => by
    match reduce w hw5 k with
    | ⟨k', hk', e1, e2⟩ =>
      match k', hk' with
      | 0, _ => exact ⟨1, Nat.succ_lt_succ (Nat.zero_lt_succ 3), by show (ring (2 * (5 * n + 4) + 1 + 1)).mul _ w = (ring (2 * (5 * n + 4) + 1 + 1)).mul _ w; rw [e1], by show (Cyclo5.cyc5 LibInt.intR).mul _ zeta = (Cyclo5.cyc5 LibInt.intR).mul _ zeta; rw [e2]⟩
      | 1, _ => exact ⟨2, Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ 2)), by show (ring (2 * (5 * n + 4) + 1 + 1)).mul _ w = (ring (2 * (5 * n + 4) + 1 + 1)).mul _ w; rw [e1], by show (Cyclo5.cyc5 LibInt.intR).mul _ zeta = (Cyclo5.cyc5 LibInt.intR).mul _ zeta; rw [e2]⟩
      | 2, _ => exact ⟨3, Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ 1))), by show (ring (2 * (5 * n + 4) + 1 + 1)).mul _ w = (ring (2 * (5 * n + 4) + 1 + 1)).mul _ w; rw [e1], by show (Cyclo5.cyc5 LibInt.intR).mul _ zeta = (Cyclo5.cyc5 LibInt.intR).mul _ zeta; rw [e2]⟩
      | 3, _ => exact ⟨4, Nat.lt_succ_self 4, by show (ring (2 * (5 * n + 4) + 1 + 1)).mul _ w = (ring (2 * (5 * n + 4) + 1 + 1)).mul _ w; rw [e1], by show (Cyclo5.cyc5 LibInt.intR).mul _ zeta = (Cyclo5.cyc5 LibInt.intR).mul _ zeta; rw [e2]⟩
      | 4, _ => exact ⟨0, Nat.zero_lt_succ 4, by show (ring (2 * (5 * n + 4) + 1 + 1)).mul _ w = _; rw [e1]; exact w5 n w hw5, by show (Cyclo5.cyc5 LibInt.intR).mul _ zeta = _; rw [e2]; exact Quint3.z5 LibInt.intR⟩
      | j + 5, hj => exact absurd hj (fun h => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 j) h))

theorem chi5_zero (w : Zp (2 * (5 * n + 4) + 1 + 1)) : chi5 n w (ring (2 * (5 * n + 4) + 1 + 1)).zero = (Cyclo5.cyc5 LibInt.intR).zero := by unfold chi5; rw [ite_pos' _ _ rfl]

/-- **χ(xy) = χ(x)χ(y).** -/
theorem chi5_mul (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+156 chars; full line in the Zenodo source)
  by_cases hx : x = (ring (2 * (5 * n + 4) + 1 + 1)).zero
  · rw [hx, zero_mul', chi5_zero]; exact (zero_mul' (Cyclo5.cyc5 LibInt.intR) _).symm
  by_cases hy : y = (ring (2 * (5 * n + 4) + 1 + 1)).zero
  · rw [hy, mul_zero, chi5_zero]; exact (mul_zero (Cyclo5.cyc5 LibInt.intR) _).symm
  have hxy : (ring (2 * (5 * n + 4) + 1 + 1)).mul x y ≠ (ring (2 * (5 * n + 4) + 1 + 1)).zero := fun e => by
    cases G4Gen.mul_eq_zero (5 * n + 4) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  match exists_i n hpr w hw1 hw5 x hx, exists_i n hpr w hw1 hw5 y hy with
  | ⟨i, hi, ei⟩, ⟨j, hj, ej⟩ =>
    match reduce n w hw5 (i + j) with
    | ⟨k, hk, e1, e2⟩ =>
      have hxyN : pw (ring (2 * (5 * n + 4) + 1 + 1)) ((ring (2 * (5 * n + 4) + 1 + 1)).mul x y) (2 * n + 2) = pw (ring (2 * (5 * n + 4) + 1 + 1)) w k := by
        rw [pw_mul, ei, ej, ← HasseCM.pw_add', e1]
      rw [val_of n hpr w hw1 hw5 _ hxy k hk hxyN, val_of n hpr w hw1 hw5 x hx i hi ei, val_of n hpr w hw1 hw5 y hy j hj ej, ← e2, HasseCM.pw_add']

/-- **χ(−1) = 1.** -/
theorem chi5_neg1 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).on  -- (+127 chars; full line in the Zenodo source)
  have hne : (ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one ≠ (ring (2 * (5 * n + 4) + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * (5 * n + 4) + 1) (by
    calc (ring (2 * (5 * n + 4) + 1 + 1)).one = (ring (2 * (5 * n + 4) + 1 + 1)).neg ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one) := (neg_neg' (ring (2 * (5 * n + 4) + 1 + 1)) _).symm
      _ = (ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).zero := by rw [e]
      _ = (ring (2 * (5 * n + 4) + 1 + 1)).zero := by lib_ring)
  have h : pw (ring (2 * (5 * n + 4) + 1 + 1)) ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one) (2 * n + 2) = pw (ring (2 * (5 * n + 4) + 1 + 1)) w 0 := by
    rw [show 2 * n + 2 = 2 * (n + 1) by lib_nat, ← TwoPower.pwpow]
    rw [show pw (ring (2 * (5 * n + 4) + 1 + 1)) ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one) 2 = (ring (2 * (5 * n + 4) + 1 + 1)).one by show (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul (ring (2 * (5 * n + 4) + 1 + 1)).one _) _ = _; lib_ring]
    exact CubicChar.pw_one'' (ring (2 * (5 * n + 4) + 1 + 1)) (n + 1)
  exact val_of n hpr w hw1 hw5 _ hne 0 (Nat.zero_lt_succ 4) h

theorem int5 (d : Int) (h : 5 * d = 0) : d = 0 := by
  cases LibInt.int_noZD _ _ h with
  | inl h5 => exact absurd (Int.ofNat.inj (h5 : Int.ofNat 5 = Int.ofNat 0)) (fun e => Nat.noConfusion e)
  | inr h' => exact h'

/-- **ζS = S ⇒ S = 0** in ℤ[ζ₅]. -/
theorem zeta_fix : ∀ S : Int × Int × Int × Int, (Cyclo5.cyc5 LibInt.intR).mul zeta S = S → S = (Cyclo5.cyc5 LibInt.intR).zero
  | (a, b, c, d), h => by
    have h0 : ((Cyclo5.cyc5 LibInt.intR).mul zeta (a, b, c, d)).1 = a := congrArg (fun s => s.1) h
    have h1 : ((Cyclo5.cyc5 LibInt.intR).mul zeta (a, b, c, d)).2.1 = b := congrArg (fun s => s.2.1) h
    have h2 : ((Cyclo5.cyc5 LibInt.intR).mul zeta (a, b, c, d)).2.2.1 = c := congrArg (fun s => s.2.2.1) h
    have h3 : ((Cyclo5.cyc5 LibInt.intR).mul zeta (a, b, c, d)).2.2.2 = d := congrArg (fun s => s.2.2.2) h
    change (((0 * a) + (-(((1 * d) + (0 * c)) + (0 * b)))) + ((0 * d) + (0 * c))) = a at h0
    change ((((0 * b) + (1 * a)) + (-(((1 * d) + (0 * c)) + (0 * b)))) + (0 * d)) = b at h1
    change ((((0 * c) + (1 * b)) + (0 * a)) + (-(((1 * d) + (0 * c)) + (0 * b)))) = c at h2
    change (((((0 * d) + (1 * c)) + (0 * b)) + (0 * a)) + (-(((1 * d) + (0 * c)) + (0 * b)))) = d at h3
    have f0 : a + d = 0 := by rw [← h0]; lib_int
    have f1 : a + -d = b := by rw [← h1]; lib_int
    have f2 : b + -d = c := by rw [← h2]; lib_int
    have f3 : c + -d = d := (show c + -d = (((((0 * d) + (1 * c)) + (0 * b)) + (0 * a)) + (-(((1 * d) + (0 * c)) + (0 * b)))) by lib_int).trans h3
    have h5 : 5 * d = 0 := by
      rw [show (5 : Int) * d = (d + -(c + -d)) + (c + -(b + -d)) + (b + -(a + -d)) + (a + d) by lib_int, f3, f2, f1, f0]; lib_int
    have hd : d = 0 := int5 d h5
    have ha : a = 0 := by
      calc a = (a + d) + -d := by lib_int
        _ = 0 := by rw [f0, hd]; lib_int
    have hb : b = 0 := by
      calc b = a + -d := f1.symm
        _ = 0 := by rw [ha, hd]; lib_int
    have hc : c = 0 := by
      calc c = b + -d := f2.symm
        _ = 0 := by rw [hb, hd]; lib_int
    rw [ha, hb, hc, hd]; rfl

theorem iter (u S : Int × Int × Int × Int) (h : (Cyclo5.cyc5 LibInt.intR).mul u S = S) : ∀ k, (Cyclo5.cyc5 LibInt.intR).mul (pw (Cyclo5.cyc5 LibInt.intR) u k) S = S
  | 0 => (Cyclo5.cyc5 LibInt.intR).one_mul S
  | k + 1 => by
    show (Cyclo5.cyc5 LibInt.intR).mul ((Cyclo5.cyc5 LibInt.intR).mul (pw (Cyclo5.cyc5 LibInt.intR) u k) u) S = S
    rw [(Cyclo5.cyc5 LibInt.intR).mul_assoc, h, iter u S h k]

/-- **Σ_x χ(x) = 0.** -/
theorem chi5_sum (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+157 chars; full line in the Zenodo source)
  match PowerSum.exists_nonroot (2 * (5 * n + 4) + 1) hpr (2 * n + 1) (by rw [show 2 * (5 * n + 4) + 1 = (2 * n + 1 + 1) + (8 * n + 7) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := reindex (Cyclo5.cyc5 LibInt.intR) _ (CMInert.complete_allZ (2 * (5 * n + 4) + 1 + 1) (Cyclo5.cyc5 LibInt.intR)) ((ring (2 * (5 * n + 4) + 1 + 1)).mul x0) ((ring (2 * (5 * n + 4) + 1 + 1)).mul (pw (ring (2 * (5 * n + 4) + 1 + 1)) x0 (2 * (5 * n + 4) + 1)))
      (fun x => by rw [← (ring (2 * (5 * n + 4) + 1 + 1)).mul_assoc, inv_exists (2 * (5 * n + 4) + 1) hpr x0 hx0, (ring (2 * (5 * n + 4) + 1 + 1)).one_mul])
      (fun y => by rw [← (ring (2 * (5 * n + 4) + 1 + 1)).mul_assoc, (ring (2 * (5 * n + 4) + 1 + 1)).mul_comm x0, inv_exists (2 * (5 * n + 4) + 1) hpr x0 hx0, (ring (2 * (5 * n + 4) + 1 + 1)).one_mul]) (chi5 n w)
    have e : sumK (Cyclo5.cyc5 LibInt.intR) (fun x => chi5 n w ((ring (2 * (5 * n + 4) + 1 + 1)).mul x0 x)) (CMInert.allZ (2 * (5 * n + 4) + 1 + 1) ((2 * (5 * n + 4) + 1 + 1) + 1))
        = (Cyclo5.cyc5 LibInt.intR).mul (chi5 n w x0) (sumK (Cyclo5.cyc5 LibInt.intR) (chi5 n w) (CMInert.allZ (2 * (5 * n + 4) + 1 + 1) ((2 * (5 * n + 4) + 1 + 1) + 1))) := by
      rw [mul_sumK]
      exact sumK_ext (Cyclo5.cyc5 LibInt.intR) _ _ (fun x => chi5_mul n hpr w hw1 hw5 x0 x) _
    rw [e] at hre
    generalize sumK (Cyclo5.cyc5 LibInt.intR) (chi5 n w) (CMInert.allZ (2 * (5 * n + 4) + 1 + 1) ((2 * (5 * n + 4) + 1 + 1) + 1)) = S at hre ⊢
    match exists_i n hpr w hw1 hw5 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      rw [val_of n hpr w hw1 hw5 x0 hx0 i hi ei] at hre
      match i, hi with
      | 0, _ => exact absurd ei hn
      | 1, _ =>
        have h := iter _ S hre 1
        rw [TwoPower.pwpow, show pw (Cyclo5.cyc5 LibInt.intR) zeta (1 * 1) = zeta from (Cyclo5.cyc5 LibInt.intR).one_mul zeta] at h
        exact zeta_fix S h
      | 2, _ =>
        have h := iter _ S hre 3
        rw [TwoPower.pwpow, show pw (Cyclo5.cyc5 LibInt.intR) zeta (2 * 3) = zeta from (Quint3.zt_mod LibInt.intR 1 1).trans ((Cyclo5.cyc5 LibInt.intR).one_mul zeta)] at h
        exact zeta_fix S h
      | 3, _ =>
        have h := iter _ S hre 2
        rw [TwoPower.pwpow, show pw (Cyclo5.cyc5 LibInt.intR) zeta (3 * 2) = zeta from (Quint3.zt_mod LibInt.intR 1 1).trans ((Cyclo5.cyc5 LibInt.intR).one_mul zeta)] at h
        exact zeta_fix S h
      | 4, _ =>
        have h := iter _ S hre 4
        rw [TwoPower.pwpow, show pw (Cyclo5.cyc5 LibInt.intR) zeta (4 * 4) = zeta from (Quint3.zt_mod LibInt.intR 3 1).trans ((Cyclo5.cyc5 LibInt.intR).one_mul zeta)] at h
        exact zeta_fix S h
      | k + 5, hk => exact absurd hk (fun h => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) h))

end Quint4

#print axioms Quint4.w5
#print axioms Quint4.wfix
#print axioms Quint4.wk_ne_one
#print axioms Quint4.w_ne_zero
#print axioms Quint4.pwnz
#print axioms Quint4.cancel1
#print axioms Quint4.wpow_ne
#print axioms Quint4.lg_val
#print axioms Quint4.xN5
#print axioms Quint4.exists_i
#print axioms Quint4.val_of
#print axioms Quint4.reduce
#print axioms Quint4.chi5_zero
#print axioms Quint4.chi5_mul
#print axioms Quint4.chi5_neg1
#print axioms Quint4.int5
#print axioms Quint4.zeta_fix
#print axioms Quint4.iter
#print axioms Quint4.chi5_sum

#print axioms: 19 theorems, no axioms.

78.177 The quintic Jacobi sum has norm \(p\)

For the quintic character \(\chi \) of \(\mathbb F_p\), \(J=\sum _t\chi (t)\chi (1-t)\in \mathbb Z[\zeta _5]\) times its complex conjugate is \(p\).

\[ J\,\overline {J}=p,\qquad \overline {\zeta }=\zeta ^4. \]

lang/Quint5.lean

/-
  lang/Quint5 — T49, cycle 752 (ℓ = 5 symbol project, step 5): the quintic Jacobi sum has norm p.

  p = 10n + 11, χ = Quint4.chi5 (values 0, ζ^i in ℤ[ζ₅]), ι(x) = x^(p−2), χ̄ = χ∘ι, ρ = χ², ρ̄ = ρ∘ι, conj ζ = ζ⁴ on ℤ[ζ₅].
  J = J(χ, χ) = Σ χ(t)χ(1 − t) (CharSumGen.Jp). In ℤ[ζ₅][C_p] (CharSumGen):
    g_χ ⋆ g_χ = J g_ρ,  g_χ̄ ⋆ g_χ̄ = J̄ g_ρ̄,  g_χ ⋆ g_χ̄ = X,  g_ρ ⋆ g_ρ̄ = X,  X ⋆ X = p X
  so J J̄ X = (g_χ g_χ̄)² = p X and, at ζ⁰, J · conj(J) = p:
    chi_iv          χ(x⁻¹) = conj χ(x)
    rho_sum, kill   Σ ρ = 0 (ζ^i S = S, 0 < i < 5 ⇒ S = 0)
    conv_X, E2gen   the two shapes of the convolutions
    conj5_mul       conj is multiplicative;  Jb_conj: J(χ̄, χ̄) = conj J(χ, χ)
    quint_norm      J · conj(J) = p in ℤ[ζ₅]
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Quint4
import lang.CharSumGen
import lang.JacobiNorm

namespace Quint5

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable (n : Nat)

def conj5 (z : Int × Int × Int × Int) : Int × Int × Int × Int := (z.1 + -z.2.1, -z.2.1, z.2.2.2 + -z.2.1, z.2.2.1 + -z.2.1)
def chib (w x : Zp (2 * (5 * n + 4) + 1 + 1)) : Int × Int × Int × Int := Quint4.chi5 n w (CharSumGen.iv (2 * (5 * n + 4)) x)
def rho (w x : Zp (2 * (5 * n + 4) + 1 + 1)) : Int × Int × Int × Int := (Cyclo5.cyc5 LibInt.intR).mul (Quint4.chi5 n w x) (Quint4.chi5 n w x)
def rhob (w x : Zp (2 * (5 * n + 4) + 1 + 1)) : Int × Int × Int × Int := rho n w (CharSumGen.iv (2 * (5 * n + 4)) x)

theorem chi_one (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)  -- (+90 chars; full line in the Zenodo source)
  Quint4.val_of n hpr w hw1 hw5 (ring (2 * (5 * n + 4) + 1 + 1)).one (PowerSum.one_ne_zero' _) 0 (Nat.zero_lt_succ 4) (CubicChar.pw_one'' (ring (2 * (5 * n + 4) + 1 + 1)) _)

theorem neg1_ne : (ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one ≠ (ring (2 * (5 * n + 4) + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * (5 * n + 4) + 1) (by
  calc (ring (2 * (5 * n + 4) + 1 + 1)).one = (ring (2 * (5 * n + 4) + 1 + 1)).neg ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one) := (neg_neg' (ring (2 * (5 * n + 4) + 1 + 1)) _).symm
    _ = (ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).zero := by rw [e]
    _ = (ring (2 * (5 * n + 4) + 1 + 1)).zero := by lib_ring)

/-- χ(x⁻¹) = conj χ(x). -/
theorem chi_iv (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)   -- (+122 chars; full line in the Zenodo source)
  by_cases hx : x = (ring (2 * (5 * n + 4) + 1 + 1)).zero
  · rw [hx, CharSumGen.iv_zero, Quint4.chi5_zero]; rfl
  have h1 : (Cyclo5.cyc5 LibInt.intR).mul (Quint4.chi5 n w (CharSumGen.iv (2 * (5 * n + 4)) x)) (Quint4.chi5 n w x) = (Cyclo5.cyc5 LibInt.intR).one := by
    rw [← Quint4.chi5_mul n hpr w hw1 hw5, CharSumGen.iv_mul (2 * (5 * n + 4)) hpr x hx, chi_one n hpr w hw1 hw5]
  have h2 : (Cyclo5.cyc5 LibInt.intR).mul (conj5 (Quint4.chi5 n w x)) (Quint4.chi5 n w x) = (Cyclo5.cyc5 LibInt.intR).one := by
    match Quint4.exists_i n hpr w hw1 hw5 x hx with
    | ⟨i, hi, ei⟩ =>
      rw [Quint4.val_of n hpr w hw1 hw5 x hx i hi ei]
      match i, hi with
      | 0, _ => rfl
      | 1, _ => rfl
      | 2, _ => rfl
      | 3, _ => rfl
      | 4, _ => rfl
      | k + 5, hk => exact absurd hk (fun h => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) h))
  calc Quint4.chi5 n w (CharSumGen.iv (2 * (5 * n + 4)) x) = (Cyclo5.cyc5 LibInt.intR).mul (Quint4.chi5 n w (CharSumGen.iv (2 * (5 * n + 4)) x)) ((Cyclo5.cyc5 LibInt.intR).mul (conj5 (Quint4.chi5 n w x)) (Quint4.chi5 n w x)) := by rw [h2]; lib_ring
    _ = (Cyclo5.cyc5 LibInt.intR).mul ((Cyclo5.cyc5 LibInt.intR).mul (Quint4.chi5 n w (CharSumGen.iv (2 * (5 * n + 4)) x)) (Quint4.chi5 n w x)) (conj5 (Quint4.chi5 n w x)) := by lib_ring
    _ = conj5 (Quint4.chi5 n w x) := by rw [h1]; lib_ring

theorem rho_mul (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)  -- (+152 chars; full line in the Zenodo source)
  unfold rho; rw [Quint4.chi5_mul n hpr w hw1 hw5]; lib_ring
theorem chib_mul (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+156 chars; full line in the Zenodo source)
  unfold chib; rw [CharSumGen.iv_mulmul, Quint4.chi5_mul n hpr w hw1 hw5]
theorem rhob_mul (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+156 chars; full line in the Zenodo source)
  unfold rhob; rw [CharSumGen.iv_mulmul, rho_mul n hpr w hw1 hw5]

/-- ζ^i S = S with 0 < i < 5 forces S = 0. -/
theorem kill (S : Int × Int × Int × Int) : ∀ i, i < 5 → i ≠ 0 → (Cyclo5.cyc5 LibInt.intR).mul (pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta i) S = S → S = (Cyclo5.cyc5 LibInt.intR).zero
  | 0, _, h0, _ => absurd rfl h0
  | 1, _, _, hre => by
    have h := Quint4.iter _ S hre 1
    rw [TwoPower.pwpow, show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (1 * 1) = Quint4.zeta from (Cyclo5.cyc5 LibInt.intR).one_mul Quint4.zeta] at h
    exact Quint4.zeta_fix S h
  | 2, _, _, hre => by
    have h := Quint4.iter _ S hre 3
    rw [TwoPower.pwpow, show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (2 * 3) = Quint4.zeta from (Quint3.zt_mod LibInt.intR 1 1).trans ((Cyclo5.cyc5 LibInt.intR).one_mul Quint4.zeta)] at h
    exact Quint4.zeta_fix S h
  | 3, _, _, hre => by
    have h := Quint4.iter _ S hre 2
    rw [TwoPower.pwpow, show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (3 * 2) = Quint4.zeta from (Quint3.zt_mod LibInt.intR 1 1).trans ((Cyclo5.cyc5 LibInt.intR).one_mul Quint4.zeta)] at h
    exact Quint4.zeta_fix S h
  | 4, _, _, hre => by
    have h := Quint4.iter _ S hre 4
    rw [TwoPower.pwpow, show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (4 * 4) = Quint4.zeta from (Quint3.zt_mod LibInt.intR 3 1).trans ((Cyclo5.cyc5 LibInt.intR).one_mul Quint4.zeta)] at h
    exact Quint4.zeta_fix S h
  | k + 5, hk, _, _ => absurd hk (fun h => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) h))

theorem rho_sum (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)  -- (+116 chars; full line in the Zenodo source)
  match PowerSum.exists_nonroot (2 * (5 * n + 4) + 1) hpr (2 * n + 1) (by rw [show 2 * (5 * n + 4) + 1 = (2 * n + 1 + 1) + (8 * n + 7) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := CharSumGen.sum_scale (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) hpr (rho n w) x0 hx0
    have e : sumK (Cyclo5.cyc5 LibInt.intR) (fun x => rho n w ((ring (2 * (5 * n + 4) + 1 + 1)).mul x0 x)) (CharSumGen.LL (2 * (5 * n + 4))) = (Cyclo5.cyc5 LibInt.intR).mul (rho n w x0) (sumK (Cyclo5.cyc5 LibInt.intR) (rho n w) (CharSumGen.LL (2 * (5 * n + 4)))) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo5.cyc5 LibInt.intR) _ _ (fun x => rho_mul n hpr w hw1 hw5 x0 x) _
    have hre' : (Cyclo5.cyc5 LibInt.intR).mul (rho n w x0) (sumK (Cyclo5.cyc5 LibInt.intR) (rho n w) (CharSumGen.LL (2 * (5 * n + 4)))) = sumK (Cyclo5.cyc5 LibInt.intR) (rho n w) (CharSumGen.LL (2 * (5 * n + 4))) := e.symm.trans hre
    generalize sumK (Cyclo5.cyc5 LibInt.intR) (rho n w) (CharSumGen.LL (2 * (5 * n + 4))) = S at hre' ⊢
    match Quint4.exists_i n hpr w hw1 hw5 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      have hr : rho n w x0 = pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (i + i) := by
        unfold rho; rw [Quint4.val_of n hpr w hw1 hw5 x0 hx0 i hi ei, HasseCM.pw_add']
      rw [hr] at hre'
      match i, hi with
      | 0, _ => exact absurd ei hn
      | 1, _ => exact kill S 2 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_right 3 2)) (fun e => Nat.noConfusion e) hre'
      | 2, _ => exact kill S 4 (Nat.lt_succ_self 4) (fun e => Nat.noConfusion e) hre'
      | 3, _ =>
        rw [show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (3 + 3) = pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta 1 from Quint3.zt_mod LibInt.intR 1 1] at hre'
        exact kill S 1 (Nat.succ_lt_succ (Nat.zero_lt_succ 3)) (fun e => Nat.noConfusion e) hre'
      | 4, _ =>
        rw [show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (4 + 4) = pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta 3 from Quint3.zt_mod LibInt.intR 1 3] at hre'
        exact kill S 3 (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ 1)))) (fun e => Nat.noConfusion e) hre'
      | k + 5, hk => exact absurd hk (fun h => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) h))

theorem chib_sum (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+115 chars; full line in the Zenodo source)
  (reindex (Cyclo5.cyc5 LibInt.intR) _ (CMInert.complete_allZ (2 * (5 * n + 4) + 1 + 1) (Cyclo5.cyc5 LibInt.intR)) (CharSumGen.iv (2 * (5 * n + 4))) (CharSumGen.iv (2 * (5 * n + 4))) (CharSumGen.iv_iv (2 * (5 * n + 4)) hpr) (CharSumGen.iv_iv (2 * (5 * n + 4)) hpr) (Quint4.chi5 n w)).trans (Quint4.chi5_sum n hpr w hw1 hw5)
theorem rhob_sum (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+115 chars; full line in the Zenodo source)
  (reindex (Cyclo5.cyc5 LibInt.intR) _ (CMInert.complete_allZ (2 * (5 * n + 4) + 1 + 1) (Cyclo5.cyc5 LibInt.intR)) (CharSumGen.iv (2 * (5 * n + 4))) (CharSumGen.iv (2 * (5 * n + 4))) (CharSumGen.iv_iv (2 * (5 * n + 4)) hpr) (CharSumGen.iv_iv (2 * (5 * n + 4)) hpr) (rho n w)).trans (rho_sum n hpr w hw1 hw5)

theorem chib_one (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+87 chars; full line in the Zenodo source)
  unfold chib; rw [show CharSumGen.iv (2 * (5 * n + 4)) (ring (2 * (5 * n + 4) + 1 + 1)).one = (ring (2 * (5 * n + 4) + 1 + 1)).one from CubicChar.pw_one'' (ring (2 * (5 * n + 4) + 1 + 1)) _, chi_one n hpr w hw1 hw5]
theorem chib_zero (w : Zp (2 * (5 * n + 4) + 1 + 1)) : chib n w (ring (2 * (5 * n + 4) + 1 + 1)).zero = (Cyclo5.cyc5 LibInt.intR).zero := by unfold chib; rw [CharSumGen.iv_zero, Quint4.chi5_zero]
theorem rho_one (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)  -- (+136 chars; full line in the Zenodo source)
theorem rho_zero (w : Zp (2 * (5 * n + 4) + 1 + 1)) : rho n w (ring (2 * (5 * n + 4) + 1 + 1)).zero = (Cyclo5.cyc5 LibInt.intR).zero := by unfold rho; rw [Quint4.chi5_zero]; lib_ring
theorem rhob_one (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+87 chars; full line in the Zenodo source)
  unfold rhob; rw [show CharSumGen.iv (2 * (5 * n + 4)) (ring (2 * (5 * n + 4) + 1 + 1)).one = (ring (2 * (5 * n + 4) + 1 + 1)).one from CubicChar.pw_one'' (ring (2 * (5 * n + 4) + 1 + 1)) _, rho_one n hpr w hw1 hw5]
theorem rhob_zero (w : Zp (2 * (5 * n + 4) + 1 + 1)) : rhob n w (ring (2 * (5 * n + 4) + 1 + 1)).zero = (Cyclo5.cyc5 LibInt.intR).zero := by unfold rhob; rw [CharSumGen.iv_zero, rho_zero]

theorem chib_neg1 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).on  -- (+127 chars; full line in the Zenodo source)
  calc chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one) = (Cyclo5.cyc5 LibInt.intR).mul (chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) (Quint4.chi5 n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) := by rw [Quint4.chi5_neg1 n hpr w hw1 hw5]; lib_ring
    _ = Quint4.chi5 n w ((ring (2 * (5 * n + 4) + 1 + 1)).mul (CharSumGen.iv (2 * (5 * n + 4)) ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) := by unfold chib; rw [Quint4.chi5_mul n hpr w hw1 hw5]
    _ = (Cyclo5.cyc5 LibInt.intR).one := by rw [CharSumGen.iv_mul (2 * (5 * n + 4)) hpr _ (neg1_ne n), chi_one n hpr w hw1 hw5]
theorem rhob_neg1 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).on  -- (+127 chars; full line in the Zenodo source)
  show (Cyclo5.cyc5 LibInt.intR).mul (chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) (chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) = (Cyclo5.cyc5 LibInt.intR).one
  rw [chib_neg1 n hpr w hw1 hw5]; lib_ring

theorem gt_sq (ψ : Zp (2 * (5 * n + 4) + 1 + 1) → Int × Int × Int × Int) (c : Zp (2 * (5 * n + 4) + 1 + 1)) :
    gt (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (gmul (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (CharSumGen.Gv (2 * (5 * n + 4)) (ψ)) (CharSumGen.Gv (2 * (5 * n + 4)) (ψ))) c = sumK (Cyclo5.cyc5 LibInt.intR) (fun a => (Cyclo5.cyc5 LibInt.intR).mul (ψ a) (ψ ((ring (2 * (5 * n + 4) + 1 + 1)).add c ((ring (2 * (5 * n + 4) + 1 + 1)).neg a)))) (CharSumGen.LL (2 * (5 * n + 4))) := b  -- (+1 chars; full line in the Zenodo source)
  rw [gt_mul]; exact sumK_ext (Cyclo5.cyc5 LibInt.intR) _ _ (fun a => by unfold CharSumGen.Gv; rw [gt_tb, gt_tb]) _

/-- the X-shape of ψ ⋆ ψ' when ψ'(b) = ψ(b⁻¹). -/
theorem conv_X (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (ψ ψ' : Zp (2 * (5 * n + 4) + 1 + 1) → Int × Int × Int × Int) (hm : ∀ x y, ψ ((ring (2 * (5 * n + 4) + 1 + 1)).mul x y) = (Cyclo5.cyc5 LibInt.intR).mul (ψ x) (ψ y)) (hm' : ∀ x y, ψ' ((ring (2 * (5 * n + 4) + 1 + 1)).mul x y) = (Cyclo5.cyc5 LibInt.intR).mul (ψ' x) (ψ' y))
    (h1 : ψ (ring (2 * (5 * n + 4) + 1 + 1)).one = (Cyclo5.cyc5 LibInt.intR).one) (h0 : ψ (ring (2 * (5 * n + 4) + 1 + 1)).zero = (Cyclo5.cyc5 LibInt.intR).zero) (hs : sumK (Cyclo5.cyc5 LibInt.intR) ψ (CharSumGen.LL (2 * (5 * n + 4))) = (Cyclo5.cyc5 LibInt.intR).zero) (hiv : ∀ b, ψ' b = ψ (CharSumGen.iv (2 * (5 * n + 4)) b)) :
    gmul (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (CharSumGen.Gv (2 * (5 * n + 4)) (ψ)) (CharSumGen.Gv (2 * (5 * n + 4)) (ψ')) = gmul (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero (ψ' ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one))) (CharSumGen.Xv (Cyclo5.cy  -- (+37 chars; full line in the Zenodo source)
  ext (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) _ _ (fun c => by
    rw [CharSumGen.conv_D (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ψ ψ' hm' c, JacobiNorm.mono0_mulg, CharSumGen.gt_X]
    rw [sumK_ext (Cyclo5.cyc5 LibInt.intR) _ _ (fun b => by rw [hiv b])]
    by_cases hc : c = (ring (2 * (5 * n + 4) + 1 + 1)).zero
    · rw [hc]
      rw [sumK_ext (Cyclo5.cyc5 LibInt.intR) _ _ (fun b => by rw [(ring (2 * (5 * n + 4) + 1 + 1)).zero_add]), CharSumGen.D_zero (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) hpr ψ hm h1 h0]
      unfold delta; rw [ite_pos' _ _ rfl]; lib_ring
    · rw [CharSumGen.D_ne (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) hpr ψ hm h1 h0 hs c hc]
      unfold delta; rw [ite_neg' _ _ hc]; lib_ring)

theorem E2gen (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (ψ : Zp (2 * (5 * n + 4) + 1 + 1) → Int × Int × Int × Int) (hm : ∀ x y, ψ ((ring (2 * (5 * n + 4) + 1 + 1)).mul x y) = (Cyclo5.cyc5 LibInt.intR).mul (ψ x) (ψ y)) (r : Zp (2 * (5 * n + 4) + 1 + 1) → Int × Int × Int × Int)
    (hr : ∀ c, (Cyclo5.cyc5 LibInt.intR).mul (ψ c) (ψ c) = r c) (hr0 : r (ring (2 * (5 * n + 4) + 1 + 1)).zero = (Cyclo5.cyc5 LibInt.intR).zero) (hsr : sumK (Cyclo5.cyc5 LibInt.intR) r (CharSumGen.LL (2 * (5 * n + 4))) = (Cyclo5.cyc5 LibInt.intR).zero) :
    gmul (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (CharSumGen.Gv (2 * (5 * n + 4)) (ψ)) (CharSumGen.Gv (2 * (5 * n + 4)) (ψ)) = gmul (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (ψ)))) (CharSumGen.Gv (2 * (5 * n + 4)) (r)) :=
  ext (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) _ _ (fun c => by
    rw [gt_sq, JacobiNorm.mono0_mulg]
    unfold CharSumGen.Gv; rw [gt_tb]
    by_cases hc : c = (ring (2 * (5 * n + 4) + 1 + 1)).zero
    · rw [hc, CharSumGen.S_zero (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ψ hm, hr0, sumK_ext (Cyclo5.cyc5 LibInt.intR) _ _ hr, hsr]; lib_ring
    · rw [CharSumGen.S_ne (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) hpr ψ hm c hc, hr]; lib_ring)

theorem E1 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) : gm  -- (+461 chars; full line in the Zenodo source)
  conv_X n hpr _ _ (Quint4.chi5_mul n hpr w hw1 hw5) (chib_mul n hpr w hw1 hw5) (chi_one n hpr w hw1 hw5) (Quint4.chi5_zero n w) (Quint4.chi5_sum n hpr w hw1 hw5) (fun _ => rfl)

theorem E4 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) : gm  -- (+401 chars; full line in the Zenodo source)
  rw [conv_X n hpr _ _ (rho_mul n hpr w hw1 hw5) (rhob_mul n hpr w hw1 hw5) (rho_one n hpr w hw1 hw5) (rho_zero n w) (rho_sum n hpr w hw1 hw5) (fun _ => rfl), rhob_neg1 n hpr w hw1 hw5]

theorem conj5_mul : ∀ x y : Int × Int × Int × Int, conj5 ((Cyclo5.cyc5 LibInt.intR).mul x y) = (Cyclo5.cyc5 LibInt.intR).mul (conj5 x) (conj5 y)
  | (a0, a1, a2, a3), (b0, b1, b2, b3) => by
    apply Prod.ext
    · show ((((a0 * b0) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) + ((a2 * b3) + (a3 * b2))) + (-((((a0 * b1) + (a1 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) + (a3 * b3)))) = ((((a0 + (-a1)) * (b0 + (-b1))) + (-((((-a1) * (b2 + (-b1))) + ((a3 + (-a1)) * (b3 + (-b1)))) + ((a2 + (-a1)) * (-b1))))) + (((a3 + (-a1)) * (b2 + (-b1))) + ((a2 + (-a1)) * (b3 + (-b1)))))
      lib_int
    apply Prod.ext
    · show (-((((a0 * b1) + (a1 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) + (a3 * b3))) = (((((a0 + (-a1)) * (-b1)) + ((-a1) * (b0 + (-b1)))) + (-((((-a1) * (b2 + (-b1))) + ((a3 + (-a1)) * (b3 + (-b1)))) + ((a2 + (-a1)) * (-b1))))) + ((a2 + (-a1)) * (b2 + (-b1))))
      lib_int
    apply Prod.ext
    · show ((((((a0 * b3) + (a1 * b2)) + (a2 * b1)) + (a3 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) + (-((((a0 * b1) + (a1 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) + (a3 * b3)))) = (((((a0 + (-a1)) * (b3 + (-b1))) + ((-a1) * (-b1))) + ((a3 + (-a1)) * (b0 + (-b1)))) + (-((((-a1) * (b2 + (-b1))) + ((a3 + (-a1)) * (b3 + (-b1)))) + ((a2 + (-a1)) * (-b1)))))
      lib_int
    · show (((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) + (-((((a0 * b1) + (a1 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) + (a3 * b3)))) = ((((((a0 + (-a1)) * (b2 + (-b1))) + ((-a1) * (b3 + (-b1)))) + ((a3 + (-a1)) * (-b1))) + ((a2 + (-a1)) * (b0 + (-b1)))) + (-((((-a1) * (b2 + (-b1))) + ((a3 + (-a1)) * (b3 + (-b1)))) + ((a2 + (-a1)) * (-b1)))))
      lib_int

theorem conj5_add : ∀ x y : Int × Int × Int × Int, conj5 ((Cyclo5.cyc5 LibInt.intR).add x y) = (Cyclo5.cyc5 LibInt.intR).add (conj5 x) (conj5 y)
  | (a0, a1, a2, a3), (b0, b1, b2, b3) => by
    apply Prod.ext
    · show ((a0 + b0) + (-(a1 + b1))) = ((a0 + (-a1)) + (b0 + (-b1)))
      lib_int
    apply Prod.ext
    · show (-(a1 + b1)) = ((-a1) + (-b1))
      lib_int
    apply Prod.ext
    · show ((a3 + b3) + (-(a1 + b1))) = ((a3 + (-a1)) + (b3 + (-b1)))
      lib_int
    · show ((a2 + b2) + (-(a1 + b1))) = ((a2 + (-a1)) + (b2 + (-b1)))
      lib_int

theorem conj5_sum {A' : Type} (f : A' → Int × Int × Int × Int) : ∀ l : List A', conj5 (sumK (Cyclo5.cyc5 LibInt.intR) f l) = sumK (Cyclo5.cyc5 LibInt.intR) (fun a => conj5 (f a)) l
  | [] => rfl
  | a :: l => by
    show conj5 ((Cyclo5.cyc5 LibInt.intR).add (f a) (sumK (Cyclo5.cyc5 LibInt.intR) f l)) = (Cyclo5.cyc5 LibInt.intR).add (conj5 (f a)) (sumK (Cyclo5.cyc5 LibInt.intR) (fun a => conj5 (f a)) l)
    rw [conj5_add, conj5_sum f l]

theorem Jb_conj (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)  -- (+169 chars; full line in the Zenodo source)
  unfold CharSumGen.Jp; rw [conj5_sum]
  exact sumK_ext (Cyclo5.cyc5 LibInt.intR) _ _ (fun t => by unfold chib; rw [conj5_mul, chi_iv n hpr w hw1 hw5, chi_iv n hpr w hw1 hw5]) _

theorem sum_one5 : ∀ k, sumK (Cyclo5.cyc5 LibInt.intR) (fun _ : Zp (2 * (5 * n + 4) + 1 + 1) => (Cyclo5.cyc5 LibInt.intR).one) (CMInert.allZ (2 * (5 * n + 4) + 1 + 1) k) = (Int.ofNat k, 0, 0, 0)
  | 0 => rfl
  | k + 1 => by
    show sumK (Cyclo5.cyc5 LibInt.intR) (fun _ : Zp (2 * (5 * n + 4) + 1 + 1) => (Cyclo5.cyc5 LibInt.intR).one) (CMInert.allZ (2 * (5 * n + 4) + 1 + 1) k ++ [mk (2 * (5 * n + 4) + 1 + 1) k]) = _
    rw [sumK_append, sum_one5 k]
    rfl

theorem scal_cancel (k : Nat) : ∀ u v : Int × Int × Int × Int, (Cyclo5.cyc5 LibInt.intR).mul u (Int.ofNat (k + 1), 0, 0, 0) = (Cyclo5.cyc5 LibInt.intR).mul v (Int.ofNat (k + 1), 0, 0, 0) → u = v
  | (m0, m1, m2, m3), (r0, r1, r2, r3), h => by
    have h0 : ((Cyclo5.cyc5 LibInt.intR).mul (m0, m1, m2, m3) (Int.ofNat (k + 1), 0, 0, 0)).1 = ((Cyclo5.cyc5 LibInt.intR).mul (r0, r1, r2, r3) (Int.ofNat (k + 1), 0, 0, 0)).1 := congrArg (fun s => s.1) h
    change (((m0 * Int.ofNat (k + 1)) + (-(((m1 * 0) + (m2 * 0)) + (m3 * 0)))) + ((m2 * 0) + (m3 * 0))) = (((r0 * Int.ofNat (k + 1)) + (-(((r1 * 0) + (r2 * 0)) + (r3 * 0)))) + ((r2 * 0) + (r3 * 0))) at h0
    have e0 : m0 = r0 := JacobiNorm.int_cancel _ _ k (by rw [show m0 * Int.ofNat (k + 1) = (((m0 * Int.ofNat (k + 1)) + (-(((m1 * 0) + (m2 * 0)) + (m3 * 0)))) + ((m2 * 0) + (m3 * 0))) by lib_int, h0]; lib_int)
    have h1 : ((Cyclo5.cyc5 LibInt.intR).mul (m0, m1, m2, m3) (Int.ofNat (k + 1), 0, 0, 0)).2.1 = ((Cyclo5.cyc5 LibInt.intR).mul (r0, r1, r2, r3) (Int.ofNat (k + 1), 0, 0, 0)).2.1 := congrArg (fun s => s.2.1) h
    change ((((m0 * 0) + (m1 * Int.ofNat (k + 1))) + (-(((m1 * 0) + (m2 * 0)) + (m3 * 0)))) + (m3 * 0)) = ((((r0 * 0) + (r1 * Int.ofNat (k + 1))) + (-(((r1 * 0) + (r2 * 0)) + (r3 * 0)))) + (r3 * 0)) at h1
    have e1 : m1 = r1 := JacobiNorm.int_cancel _ _ k (by rw [show m1 * Int.ofNat (k + 1) = ((((m0 * 0) + (m1 * Int.ofNat (k + 1))) + (-(((m1 * 0) + (m2 * 0)) + (m3 * 0)))) + (m3 * 0)) by lib_int, h1]; lib_int)
    have h2 : ((Cyclo5.cyc5 LibInt.intR).mul (m0, m1, m2, m3) (Int.ofNat (k + 1), 0, 0, 0)).2.2.1 = ((Cyclo5.cyc5 LibInt.intR).mul (r0, r1, r2, r3) (Int.ofNat (k + 1), 0, 0, 0)).2.2.1 := congrArg (fun s => s.2.2.1) h
    change ((((m0 * 0) + (m1 * 0)) + (m2 * Int.ofNat (k + 1))) + (-(((m1 * 0) + (m2 * 0)) + (m3 * 0)))) = ((((r0 * 0) + (r1 * 0)) + (r2 * Int.ofNat (k + 1))) + (-(((r1 * 0) + (r2 * 0)) + (r3 * 0)))) at h2
    have e2 : m2 = r2 := JacobiNorm.int_cancel _ _ k (by rw [show m2 * Int.ofNat (k + 1) = ((((m0 * 0) + (m1 * 0)) + (m2 * Int.ofNat (k + 1))) + (-(((m1 * 0) + (m2 * 0)) + (m3 * 0)))) by lib_int, h2]; lib_int)
    have h3 : ((Cyclo5.cyc5 LibInt.intR).mul (m0, m1, m2, m3) (Int.ofNat (k + 1), 0, 0, 0)).2.2.2 = ((Cyclo5.cyc5 LibInt.intR).mul (r0, r1, r2, r3) (Int.ofNat (k + 1), 0, 0, 0)).2.2.2 := congrArg (fun s => s.2.2.2) h
    change (((((m0 * 0) + (m1 * 0)) + (m2 * 0)) + (m3 * Int.ofNat (k + 1))) + (-(((m1 * 0) + (m2 * 0)) + (m3 * 0)))) = (((((r0 * 0) + (r1 * 0)) + (r2 * 0)) + (r3 * Int.ofNat (k + 1))) + (-(((r1 * 0) + (r2 * 0)) + (r3 * 0)))) at h3
    have e3 : m3 = r3 := JacobiNorm.int_cancel _ _ k (by rw [show m3 * Int.ofNat (k + 1) = (((((m0 * 0) + (m1 * 0)) + (m2 * 0)) + (m3 * Int.ofNat (k + 1))) + (-(((m1 * 0) + (m2 * 0)) + (m3 * 0)))) by lib_int, h3]; lib_int)
    rw [e0, e1, e2, e3]

/-- **N(J) = p**: the quintic Jacobi sum J = J(χ, χ) ∈ ℤ[ζ₅] satisfies J · conj(J) = p. -/
theorem quint_norm (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).o  -- (+262 chars; full line in the Zenodo source)
  have h2 := E2gen n hpr _ (Quint4.chi5_mul n hpr w hw1 hw5) (rho n w) (fun c => rfl) (rho_zero n w) (rho_sum n hpr w hw1 hw5)
  have h3 := E2gen n hpr _ (chib_mul n hpr w hw1 hw5) (rhob n w) (fun c => rfl) (rhob_zero n w) (rhob_sum n hpr w hw1 hw5)
  have h1 := E1 n hpr w hw1 hw5
  have h4 := E4 n hpr w hw1 hw5
  have hcm : (Cyclo5.cyc5 LibInt.intR).mul (chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) (chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) = (Cyclo5.cyc5 LibInt.intR).one := by rw [chib_neg1 n hpr w hw1 hw5]; lib_ring
  let G := (gring (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1))
  have key : gmul (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((Quint4.chi5 n w))) (CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((chib n w))))) (CharSumGen.Xv (Cyclo5.cyc5 LibInt.intR) (2 * (5  -- (+295 chars; full line in the Zenodo source)
    have L : G.mul (G.mul (CharSumGen.Gv (2 * (5 * n + 4)) ((Quint4.chi5 n w))) (CharSumGen.Gv (2 * (5 * n + 4)) ((Quint4.chi5 n w)))) (G.mul (CharSumGen.Gv (2 * (5 * n + 4)) ((chib n w))) (CharSumGen.Gv (2 * (5 * n + 4)) ((chib n w))))
        = G.mul (G.mul (CharSumGen.Gv (2 * (5 * n + 4)) ((Quint4.chi5 n w))) (CharSumGen.Gv (2 * (5 * n + 4)) ((chib n w)))) (G.mul (CharSumGen.Gv (2 * (5 * n + 4)) ((Quint4.chi5 n w))) (CharSumGen.Gv (2 * (5 * n + 4)) ((chib n w)))) := by lib_ring
    have h2' : G.mul (CharSumGen.Gv (2 * (5 * n + 4)) ((Quint4.chi5 n w))) (CharSumGen.Gv (2 * (5 * n + 4)) ((Quint4.chi5 n w))) = G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((Quint4.chi5 n w))))) (CharSumGen.Gv (2 * (5 * n + 4)) ((rho n w))) := h2
    have h3' : G.mul (CharSumGen.Gv (2 * (5 * n + 4)) ((chib n w))) (CharSumGen.Gv (2 * (5 * n + 4)) ((chib n w))) = G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((chib n w))))) (CharSumGen.Gv (2 * (5 * n + 4)) ((rhob n w))) := h3
    have h1' : G.mul (CharSumGen.Gv (2 * (5 * n + 4)) ((Quint4.chi5 n w))) (CharSumGen.Gv (2 * (5 * n + 4)) ((chib n w))) = G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero (chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one))) (CharSumGen.Xv (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4))) := h1
    have h4' : G.mul (CharSumGen.Gv (2 * (5 * n + 4)) ((rho n w))) (CharSumGen.Gv (2 * (5 * n + 4)) ((rhob n w))) = G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((Cyclo5.cyc5 LibInt.intR).one)) (CharSumGen.Xv (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4))) := h4
    have hX' : G.mul (CharSumGen.Xv (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4))) (CharSumGen.Xv (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4))) = G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((CharSumGen.Pp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4))))) (CharSumGen.Xv (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4))) := CharSumGen.XX (Cyclo5.cyc5 Li  -- (+28 chars; full line in the Zenodo source)
    have m2 : ∀ s t, G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero s) (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero t) = mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((Cyclo5.cyc5 LibInt.intR).mul s t) := fun s t => by
      show gmul (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) _ _ = _; rw [mono_mul, show (ring (2 * (5 * n + 4) + 1 + 1)).add (ring (2 * (5 * n + 4) + 1 + 1)).zero (ring (2 * (5 * n + 4) + 1 + 1)).zero = (ring (2 * (5 * n + 4) + 1 + 1)).zero by lib_ring]
    rw [h2', h3', h1'] at L
    rw [show G.mul (G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((Quint4.chi5 n w))))) (CharSumGen.Gv (2 * (5 * n + 4)) ((rho n w)))) (G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2   -- (+78 chars; full line in the Zenodo source)
        = G.mul (G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((Quint4.chi5 n w))))) (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((chib n w)))))) (G.mul (CharSumGen.Gv (2   -- (+88 chars; full line in the Zenodo source)
      h4', m2, show G.mul (G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero (chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one))) (CharSumGen.Xv (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)))) (G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero (chib n w ((ring (2  -- (+129 chars; full line in the Zenodo source)
        = G.mul (G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero (chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one))) (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero (chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)))) (G.mul (  -- (+132 chars; full line in the Zenodo source)
      hX', m2, hcm, show G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((Quint4.chi5 n w))) (CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((chib n w))))) (G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4)  -- (+109 chars; full line in the Zenodo source)
        = G.mul (G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((Quint4.chi5 n w))) (CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((chib n w))))) (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero  -- (+107 chars; full line in the Zenodo source)
      m2, show G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((Cyclo5.cyc5 LibInt.intR).one)) (G.mul (mono (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).zero ((CharSumGen.Pp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4))))) (CharSumGen.Xv (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)))) = G.mul (G.mul (mono (C  -- (+353 chars; full line in the Zenodo source)
      m2, (Cyclo5.cyc5 LibInt.intR).mul_comm _ (Cyclo5.cyc5 LibInt.intR).one, (Cyclo5.cyc5 LibInt.intR).one_mul, (Cyclo5.cyc5 LibInt.intR).one_mul] at L
    exact L
  have k0 := congrArg (fun u => gt (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) u (ring (2 * (5 * n + 4) + 1 + 1)).zero) key
  have k1 : (Cyclo5.cyc5 LibInt.intR).mul ((Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((Quint4.chi5 n w))) (CharSumGen.Jp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) ((chib n w)))) (gt (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (CharSumGen.Xv (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4))) (ring (2 * (5 * n + 4) + 1 + 1)).zero) = (Cyclo5.cyc5 Lib  -- (+234 chars; full line in the Zenodo source)
    have := k0
    rw [JacobiNorm.mono0_mulg, JacobiNorm.mono0_mulg] at this
    exact this
  have hP : (CharSumGen.Pp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4))) = (Int.ofNat (2 * (5 * n + 4) + 1 + 1 + 1), 0, 0, 0) := sum_one5 n _
  have hX0 : gt (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4) + 1 + 1) (CharSumGen.Xv (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4))) (ring (2 * (5 * n + 4) + 1 + 1)).zero = (Int.ofNat (2 * (5 * n + 4) + 1 + 1), 0, 0, 0) := by
    rw [CharSumGen.gt_X]; unfold delta; rw [ite_pos' _ _ rfl, (Cyclo5.cyc5 LibInt.intR).mul_comm, (Cyclo5.cyc5 LibInt.intR).one_mul, hP]
    apply Prod.ext
    · show Int.ofNat (2 * (5 * n + 4) + 1 + 1) + 1 + -1 = Int.ofNat (2 * (5 * n + 4) + 1 + 1); lib_int
    apply Prod.ext
    · show (0 : Int) + -0 = 0; rfl
    apply Prod.ext
    · show (0 : Int) + -0 = 0; rfl
    · show (0 : Int) + -0 = 0; rfl
  rw [hX0, hP, Jb_conj n hpr w hw1 hw5] at k1
  exact scal_cancel (2 * (5 * n + 4) + 1) _ _ k1

end Quint5

#print axioms Quint5.chi_one
#print axioms Quint5.neg1_ne
#print axioms Quint5.chi_iv
#print axioms Quint5.rho_mul
#print axioms Quint5.chib_mul
#print axioms Quint5.rhob_mul
#print axioms Quint5.kill
#print axioms Quint5.rho_sum
#print axioms Quint5.chib_sum
#print axioms Quint5.rhob_sum
#print axioms Quint5.chib_one
#print axioms Quint5.chib_zero
#print axioms Quint5.rho_one
#print axioms Quint5.rho_zero
#print axioms Quint5.rhob_one
#print axioms Quint5.rhob_zero
#print axioms Quint5.chib_neg1
#print axioms Quint5.rhob_neg1
#print axioms Quint5.gt_sq
#print axioms Quint5.conv_X
#print axioms Quint5.E2gen
#print axioms Quint5.E1
#print axioms Quint5.E4
#print axioms Quint5.conj5_mul
#print axioms Quint5.conj5_add
#print axioms Quint5.conj5_sum
#print axioms Quint5.Jb_conj
#print axioms Quint5.sum_one5
#print axioms Quint5.scal_cancel
#print axioms Quint5.quint_norm

#print axioms: 30 theorems, no axioms.

78.178 Reduction of quintic character values

The componentwise reduction \(\mathbb Z[\zeta _5]\to T[\zeta _5]\) is a ring map, and the powers \(\chi ^2,\chi ^3,\chi ^4\) of the quintic character sum to zero.

\[ \sum _{a}\chi (a)^r=0\ (r=2,3,4),\qquad \chi (x)^4=\chi (x^{-1}). \]

lang/Quint6.lean

/-
  lang/Quint6 — T49, cycle 753 (ℓ = 5 symbol project, step 6): reduction ℤ[ζ₅] → T[ζ₅] and the inputs of the n-th power
  residue criterion (CharSumGen.nth_recip, NthRecipInert) for the quintic character.
    ph5, ph5_add, ph5_mul, ph5_neg, ph5_one, ph5_zero, ph5_sum, ph5_pow, ph5_zeta
                        the componentwise reduction z ↦ (castZ z₀, …, castZ z₃) is a ring map ℤ[ζ₅] → T[ζ₅], ζ ↦ ζ
    chi_pow_sum2/3/4    Σ_a χ(a)^r = 0 for r = 2, 3, 4 (χ^r is a nontrivial character)
    chi_pow4            χ(x)⁴ = conj χ(x) = χ(x⁻¹)
    psi_*               the same for ψ = ph5 ∘ χ with values in T[ζ₅]: multiplicative, ψ(1) = 1, ψ(0) = 0,
                        Σ ψ^(i+2) = 0 (i < 3), ψ⁴ = ψ ∘ ι — the hypotheses of nth_recip / nth_recip_inert with n = 5
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Quint5
import lang.QuartRecip

namespace Quint6

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)

section Gen
variable {R : Type} (T : CRing R)

def ph5 (z : Int × Int × Int × Int) : R × R × R × R := (castZ T z.1, castZ T z.2.1, castZ T z.2.2.1, castZ T z.2.2.2)

theorem ph5_add : ∀ x y : Int × Int × Int × Int, ph5 T ((Cyclo5.cyc5 LibInt.intR).add x y) = (Cyclo5.cyc5 T).add (ph5 T x) (ph5 T y)
  | (a0, a1, a2, a3), (b0, b1, b2, b3) => by
    apply Prod.ext
    · exact IntCast.castZ_add T a0 b0
    apply Prod.ext
    · exact IntCast.castZ_add T a1 b1
    apply Prod.ext
    · exact IntCast.castZ_add T a2 b2
    · exact IntCast.castZ_add T a3 b3

theorem ph5_mul : ∀ x y : Int × Int × Int × Int, ph5 T ((Cyclo5.cyc5 LibInt.intR).mul x y) = (Cyclo5.cyc5 T).mul (ph5 T x) (ph5 T y)
  | (a0, a1, a2, a3), (b0, b1, b2, b3) => by
    apply Prod.ext
    · show castZ T (((a0 * b0) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) + ((a2 * b3) + (a3 * b2))) = (T.add (T.add (T.mul (castZ T a0) (castZ T b0)) (T.neg (T.add (T.add (T.mul (castZ T a1) (castZ T b3)) (T.mul (castZ T a2) (castZ T b2))) (T.mul (castZ T a3) (castZ T b1))))) (T.add (T.mul (castZ T a2) (castZ T b3)) (T.mul (castZ T a3) (castZ T b2))))
      repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    apply Prod.ext
    · show castZ T ((((a0 * b1) + (a1 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) + (a3 * b3)) = (T.add (T.add (T.add (T.mul (castZ T a0) (castZ T b1)) (T.mul (castZ T a1) (castZ T b0))) (T.neg (T.add (T.add (T.mul (castZ T a1) (castZ T b3)) (T.mul (castZ T a2) (castZ T b2))) (T.mul (castZ T a3) (castZ T b1))))) (T.mul (castZ T a3) (castZ T b3)))
      repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    apply Prod.ext
    · show castZ T ((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) = (T.add (T.add (T.add (T.mul (castZ T a0) (castZ T b2)) (T.mul (castZ T a1) (castZ T b1))) (T.mul (castZ T a2) (castZ T b0))) (T.neg (T.add (T.add (T.mul (castZ T a1) (castZ T b3)) (T.mul (castZ T a2) (castZ T b2))) (T.mul (castZ T a3) (castZ T b1)))))
      repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    · show castZ T (((((a0 * b3) + (a1 * b2)) + (a2 * b1)) + (a3 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) = (T.add (T.add (T.add (T.add (T.mul (castZ T a0) (castZ T b3)) (T.mul (castZ T a1) (castZ T b2))) (T.mul (castZ T a2) (castZ T b1))) (T.mul (castZ T a3) (castZ T b0))) (T.neg (T.add (T.add (T.mul (castZ T a1) (castZ T b3)) (T.mul (castZ T a2) (castZ T b2))) (T.mul (castZ T a3) (cast  -- (+11 chars; full line in the Zenodo source)
      repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])

theorem ph5_neg : ∀ x : Int × Int × Int × Int, ph5 T ((Cyclo5.cyc5 LibInt.intR).neg x) = (Cyclo5.cyc5 T).neg (ph5 T x)
  | (a0, a1, a2, a3) => by
    apply Prod.ext
    · exact HasseDavenport.castZ_neg T a0
    apply Prod.ext
    · exact HasseDavenport.castZ_neg T a1
    apply Prod.ext
    · exact HasseDavenport.castZ_neg T a2
    · exact HasseDavenport.castZ_neg T a3

theorem ph5_one : ph5 T (Cyclo5.cyc5 LibInt.intR).one = (Cyclo5.cyc5 T).one := by
  apply Prod.ext
  · show castN T 1 = T.one
    exact add_zero' T _
  · rfl

theorem ph5_zero : ph5 T (Cyclo5.cyc5 LibInt.intR).zero = (Cyclo5.cyc5 T).zero := rfl

theorem ph5_zeta : ph5 T Quint4.zeta = Quint2.zt T := by
  apply Prod.ext
  · rfl
  apply Prod.ext
  · show castN T 1 = T.one
    exact add_zero' T _
  · rfl

theorem ph5_sum {A' : Type} (f : A' → Int × Int × Int × Int) : ∀ l : List A', ph5 T (sumK (Cyclo5.cyc5 LibInt.intR) f l) = sumK (Cyclo5.cyc5 T) (fun a => ph5 T (f a)) l
  | [] => rfl
  | a :: l => by
    show ph5 T ((Cyclo5.cyc5 LibInt.intR).add (f a) (sumK _ f l)) = (Cyclo5.cyc5 T).add (ph5 T (f a)) (sumK (Cyclo5.cyc5 T) (fun a => ph5 T (f a)) l)
    rw [ph5_add, ph5_sum f l]

theorem ph5_pow (z : Int × Int × Int × Int) : ∀ k, pw (Cyclo5.cyc5 T) (ph5 T z) k = ph5 T (pw (Cyclo5.cyc5 LibInt.intR) z k)
  | 0 => (ph5_one T).symm
  | k + 1 => by
    show (Cyclo5.cyc5 T).mul (pw _ (ph5 T z) k) (ph5 T z) = ph5 T ((Cyclo5.cyc5 LibInt.intR).mul (pw (Cyclo5.cyc5 LibInt.intR) z k) z)
    rw [ph5_pow z k, ph5_mul]
end Gen

variable (n : Nat)

theorem chi_pow_sum2 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1))  -- (+173 chars; full line in the Zenodo source)
  match PowerSum.exists_nonroot (2 * (5 * n + 4) + 1) hpr (2 * n + 1) (by rw [show 2 * (5 * n + 4) + 1 = (2 * n + 1 + 1) + (8 * n + 7) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := CharSumGen.sum_scale (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) hpr (fun a => pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w a) 2) x0 hx0
    have e : sumK (Cyclo5.cyc5 LibInt.intR) (fun x => pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w ((ring (2 * (5 * n + 4) + 1 + 1)).mul x0 x)) 2) (CharSumGen.LL (2 * (5 * n + 4))) = (Cyclo5.cyc5 LibInt.intR).mul (pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w x0) 2) (sumK (Cyclo5.cyc5 LibInt.intR) (fun a => pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w a) 2) (CharSumGen.LL (2 * (5 * n + 4)))) :=   -- (+2 chars; full line in the Zenodo source)
      rw [mul_sumK]; exact sumK_ext (Cyclo5.cyc5 LibInt.intR) _ _ (fun x => by rw [Quint4.chi5_mul n hpr w hw1 hw5, pw_mul]) _
    have hre' := e.symm.trans hre
    generalize sumK (Cyclo5.cyc5 LibInt.intR) (fun a => pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w a) 2) (CharSumGen.LL (2 * (5 * n + 4))) = S at hre' ⊢
    match Quint4.exists_i n hpr w hw1 hw5 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      rw [Quint4.val_of n hpr w hw1 hw5 x0 hx0 i hi ei, TwoPower.pwpow] at hre'
      match i, hi with
      | 0, _ => exact absurd ei hn
      | 1, _ =>
        exact Quint5.kill S 2 (show 2 < 5 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_right (2 + 1) 2)) (fun e => Nat.noConfusion e) hre'
      | 2, _ =>
        exact Quint5.kill S 4 (show 4 < 5 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 4) (Nat.le_add_right (4 + 1) 0)) (fun e => Nat.noConfusion e) hre'
      | 3, _ =>
        rw [show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (3 * 2) = pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta 1 from Quint3.zt_mod LibInt.intR 1 1] at hre'
        exact Quint5.kill S 1 (show 1 < 5 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_right (1 + 1) 3)) (fun e => Nat.noConfusion e) hre'
      | 4, _ =>
        rw [show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (4 * 2) = pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta 3 from Quint3.zt_mod LibInt.intR 1 3] at hre'
        exact Quint5.kill S 3 (show 3 < 5 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right (3 + 1) 1)) (fun e => Nat.noConfusion e) hre'
      | k + 5, hk => exact absurd hk (fun h => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) h))

theorem chi_pow_sum3 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1))  -- (+173 chars; full line in the Zenodo source)
  match PowerSum.exists_nonroot (2 * (5 * n + 4) + 1) hpr (2 * n + 1) (by rw [show 2 * (5 * n + 4) + 1 = (2 * n + 1 + 1) + (8 * n + 7) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := CharSumGen.sum_scale (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) hpr (fun a => pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w a) 3) x0 hx0
    have e : sumK (Cyclo5.cyc5 LibInt.intR) (fun x => pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w ((ring (2 * (5 * n + 4) + 1 + 1)).mul x0 x)) 3) (CharSumGen.LL (2 * (5 * n + 4))) = (Cyclo5.cyc5 LibInt.intR).mul (pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w x0) 3) (sumK (Cyclo5.cyc5 LibInt.intR) (fun a => pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w a) 3) (CharSumGen.LL (2 * (5 * n + 4)))) :=   -- (+2 chars; full line in the Zenodo source)
      rw [mul_sumK]; exact sumK_ext (Cyclo5.cyc5 LibInt.intR) _ _ (fun x => by rw [Quint4.chi5_mul n hpr w hw1 hw5, pw_mul]) _
    have hre' := e.symm.trans hre
    generalize sumK (Cyclo5.cyc5 LibInt.intR) (fun a => pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w a) 3) (CharSumGen.LL (2 * (5 * n + 4))) = S at hre' ⊢
    match Quint4.exists_i n hpr w hw1 hw5 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      rw [Quint4.val_of n hpr w hw1 hw5 x0 hx0 i hi ei, TwoPower.pwpow] at hre'
      match i, hi with
      | 0, _ => exact absurd ei hn
      | 1, _ =>
        exact Quint5.kill S 3 (show 3 < 5 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right (3 + 1) 1)) (fun e => Nat.noConfusion e) hre'
      | 2, _ =>
        rw [show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (2 * 3) = pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta 1 from Quint3.zt_mod LibInt.intR 1 1] at hre'
        exact Quint5.kill S 1 (show 1 < 5 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_right (1 + 1) 3)) (fun e => Nat.noConfusion e) hre'
      | 3, _ =>
        rw [show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (3 * 3) = pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta 4 from Quint3.zt_mod LibInt.intR 1 4] at hre'
        exact Quint5.kill S 4 (show 4 < 5 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 4) (Nat.le_add_right (4 + 1) 0)) (fun e => Nat.noConfusion e) hre'
      | 4, _ =>
        rw [show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (4 * 3) = pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta 2 from Quint3.zt_mod LibInt.intR 2 2] at hre'
        exact Quint5.kill S 2 (show 2 < 5 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_right (2 + 1) 2)) (fun e => Nat.noConfusion e) hre'
      | k + 5, hk => exact absurd hk (fun h => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) h))

theorem chi_pow_sum4 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1))  -- (+173 chars; full line in the Zenodo source)
  match PowerSum.exists_nonroot (2 * (5 * n + 4) + 1) hpr (2 * n + 1) (by rw [show 2 * (5 * n + 4) + 1 = (2 * n + 1 + 1) + (8 * n + 7) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := CharSumGen.sum_scale (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) hpr (fun a => pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w a) 4) x0 hx0
    have e : sumK (Cyclo5.cyc5 LibInt.intR) (fun x => pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w ((ring (2 * (5 * n + 4) + 1 + 1)).mul x0 x)) 4) (CharSumGen.LL (2 * (5 * n + 4))) = (Cyclo5.cyc5 LibInt.intR).mul (pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w x0) 4) (sumK (Cyclo5.cyc5 LibInt.intR) (fun a => pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w a) 4) (CharSumGen.LL (2 * (5 * n + 4)))) :=   -- (+2 chars; full line in the Zenodo source)
      rw [mul_sumK]; exact sumK_ext (Cyclo5.cyc5 LibInt.intR) _ _ (fun x => by rw [Quint4.chi5_mul n hpr w hw1 hw5, pw_mul]) _
    have hre' := e.symm.trans hre
    generalize sumK (Cyclo5.cyc5 LibInt.intR) (fun a => pw (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w a) 4) (CharSumGen.LL (2 * (5 * n + 4))) = S at hre' ⊢
    match Quint4.exists_i n hpr w hw1 hw5 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      rw [Quint4.val_of n hpr w hw1 hw5 x0 hx0 i hi ei, TwoPower.pwpow] at hre'
      match i, hi with
      | 0, _ => exact absurd ei hn
      | 1, _ =>
        exact Quint5.kill S 4 (show 4 < 5 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 4) (Nat.le_add_right (4 + 1) 0)) (fun e => Nat.noConfusion e) hre'
      | 2, _ =>
        rw [show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (2 * 4) = pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta 3 from Quint3.zt_mod LibInt.intR 1 3] at hre'
        exact Quint5.kill S 3 (show 3 < 5 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right (3 + 1) 1)) (fun e => Nat.noConfusion e) hre'
      | 3, _ =>
        rw [show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (3 * 4) = pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta 2 from Quint3.zt_mod LibInt.intR 2 2] at hre'
        exact Quint5.kill S 2 (show 2 < 5 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_right (2 + 1) 2)) (fun e => Nat.noConfusion e) hre'
      | 4, _ =>
        rw [show pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta (4 * 4) = pw (Cyclo5.cyc5 LibInt.intR) Quint4.zeta 1 from Quint3.zt_mod LibInt.intR 3 1] at hre'
        exact Quint5.kill S 1 (show 1 < 5 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_right (1 + 1) 3)) (fun e => Nat.noConfusion e) hre'
      | k + 5, hk => exact absurd hk (fun h => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) h))

/-- χ(x)⁴ = χ(x⁻¹). -/
theorem chi_pow4 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+149 chars; full line in the Zenodo source)
  rw [Quint5.chi_iv n hpr w hw1 hw5]
  by_cases hx : x = (ring (2 * (5 * n + 4) + 1 + 1)).zero
  · rw [hx, Quint4.chi5_zero]; rfl
  match Quint4.exists_i n hpr w hw1 hw5 x hx with
  | ⟨i, hi, ei⟩ =>
    rw [Quint4.val_of n hpr w hw1 hw5 x hx i hi ei]
    match i, hi with
    | 0, _ => rfl
    | 1, _ => rfl
    | 2, _ => rfl
    | 3, _ => rfl
    | 4, _ => rfl
    | k + 5, hk => exact absurd hk (fun h => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) h))

section Psi
variable {R : Type} (T : CRing R)

def psi (w x : Zp (2 * (5 * n + 4) + 1 + 1)) : R × R × R × R := ph5 T (Quint4.chi5 n w x)

theorem psi_mul (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)  -- (+148 chars; full line in the Zenodo source)
  unfold psi; rw [Quint4.chi5_mul n hpr w hw1 hw5, ph5_mul]
theorem psi_one (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)  -- (+77 chars; full line in the Zenodo source)
  unfold psi; rw [Quint5.chi_one n hpr w hw1 hw5, ph5_one]
theorem psi_zero (w : Zp (2 * (5 * n + 4) + 1 + 1)) : psi n T w (ring (2 * (5 * n + 4) + 1 + 1)).zero = (Cyclo5.cyc5 T).zero := by
  unfold psi; rw [Quint4.chi5_zero]; rfl
theorem psi_sum (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)  -- (+98 chars; full line in the Zenodo source)
  have hs : sumK (Cyclo5.cyc5 LibInt.intR) (Quint4.chi5 n w) (CharSumGen.LL (2 * (5 * n + 4))) = (Cyclo5.cyc5 LibInt.intR).zero := Quint4.chi5_sum n hpr w hw1 hw5
  unfold psi; rw [← ph5_sum, hs]; rfl
theorem psi_nt (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)   -- (+144 chars; full line in the Zenodo source)
  | 0, _ => by unfold psi; rw [sumK_ext (Cyclo5.cyc5 T) _ _ (fun a => ph5_pow T _ _), ← ph5_sum, chi_pow_sum2 n hpr w hw1 hw5]; rfl
  | 1, _ => by unfold psi; rw [sumK_ext (Cyclo5.cyc5 T) _ _ (fun a => ph5_pow T _ _), ← ph5_sum, chi_pow_sum3 n hpr w hw1 hw5]; rfl
  | 2, _ => by unfold psi; rw [sumK_ext (Cyclo5.cyc5 T) _ _ (fun a => ph5_pow T _ _), ← ph5_sum, chi_pow_sum4 n hpr w hw1 hw5]; rfl
  | k + 3, hk => absurd hk (fun h => Nat.lt_irrefl 3 (Nat.lt_of_le_of_lt (Nat.le_add_left 3 k) h))
theorem psi_inv (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)  -- (+132 chars; full line in the Zenodo source)
  unfold psi; rw [ph5_pow, chi_pow4 n hpr w hw1 hw5]
end Psi

end Quint6

#print axioms Quint6.ph5_add
#print axioms Quint6.ph5_mul
#print axioms Quint6.ph5_neg
#print axioms Quint6.ph5_one
#print axioms Quint6.ph5_zero
#print axioms Quint6.ph5_zeta
#print axioms Quint6.ph5_sum
#print axioms Quint6.ph5_pow
#print axioms Quint6.chi_pow_sum2
#print axioms Quint6.chi_pow_sum3
#print axioms Quint6.chi_pow_sum4
#print axioms Quint6.chi_pow4
#print axioms Quint6.psi_mul
#print axioms Quint6.psi_one
#print axioms Quint6.psi_zero
#print axioms Quint6.psi_sum
#print axioms Quint6.psi_nt
#print axioms Quint6.psi_inv

#print axioms: 18 theorems, no axioms.

78.179 Quintic residue criterion at \(q\equiv 4\pmod 5\)

For a prime \(q=5m+4\neq p\) the reduced quintic character \(\psi \) satisfies, in \(\mathbb F_q[\zeta _5]\),

\[ \psi (q)=A^{m+1}p^{m},\qquad A=J(\psi ,\psi )J(\psi ^2,\psi )J(\psi ^3,\psi ). \]

lang/Quint7.lean

/-
  lang/Quint7 — T49, cycle 754 (ℓ = 5 symbol project, step 7): **the quintic residue criterion at q ≡ 4 (mod 5)**.
  p = 10n + 11, χ the quintic character (Quint4) reduced to ψ = ph5 ∘ χ with values in 𝔽_q[ζ₅] (Quint6), q = 5m + 4 prime, q ≠ p:
    frob_bar            ψ(a)^q = ψ(a)⁴ = ψ(a⁻¹)       (ζ^q = ζ⁴ = ζ̄ in 𝔽_q[ζ₅], Quint3.zt_mod)
    quint_recip_inert   ψ(q) = A^(m+1) p^m  in 𝔽_q[ζ₅],  A = J(ψ, ψ) J(ψ², ψ) J(ψ³, ψ)   (CharSumGen.nth_recip_inert, n = 5)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Quint6
import lang.NthRecipInert

namespace Quint7

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable (n m : Nat)

/-- ψ(a)^q = ψ(a)⁴ for q = 5m + 4. -/
theorem frob_bar (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+236 chars; full line in the Zenodo source)
  by_cases ha : a = (ring (2 * (5 * n + 4) + 1 + 1)).zero
  · rw [ha, Quint6.psi_zero]
    exact (HasseCM.pw_zero_succ (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) (5 * m + 2 + 1)).trans (HasseCM.pw_zero_succ (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) 3).symm
  match Quint4.exists_i n hpr w hw1 hw5 a ha with
  | ⟨i, hi, ei⟩ =>
    have hv : (Quint6.psi n (ring (5 * m + 2 + 1)) w) a = pw (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) (Quint2.zt (ring (5 * m + 2 + 1))) i := by
      unfold Quint6.psi; rw [Quint4.val_of n hpr w hw1 hw5 a ha i hi ei, ← Quint6.ph5_pow, Quint6.ph5_zeta]
    rw [hv, TwoPower.pwpow, TwoPower.pwpow, show i * (5 * m + 2 + 1 + 1) = 5 * (i * m) + i * 4 by lib_nat, Quint3.zt_mod]

theorem psi_iv_neg1 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).  -- (+205 chars; full line in the Zenodo source)
  unfold Quint6.psi
  rw [show Quint4.chi5 n w (CharSumGen.iv (2 * (5 * n + 4)) ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) = Quint5.chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one) from rfl, Quint5.chib_neg1 n hpr w hw1 hw5, Quint6.ph5_one]

/-- **Quintic residue criterion at q = 5m + 4**: ψ(q) = A^(m+1) p^m in 𝔽_q[ζ₅], A = ∏_{i≤3} J(ψ^i, ψ). -/
theorem quint_recip_inert (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1   -- (+224 chars; full line in the Zenodo source)
    (Quint6.psi n (ring (5 * m + 2 + 1)) w) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (5 * m + 2 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) = (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).mul (pw (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) (CharSumGen.jpr (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) (2 * (5 * n + 4)) (Quint6.psi n (ring (5 * m + 2 + 1)) w) 3) (m + 1)) (pw (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) (CharSumGen.  -- (+67 chars; full line in the Zenodo source)
  have h := CharSumGen.nth_recip_inert (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) (2 * (5 * n + 4)) hpr (Quint6.psi n (ring (5 * m + 2 + 1)) w) (Quint6.psi_mul n (ring (5 * m + 2 + 1)) hpr w hw1 hw5) (Quint6.psi_one n (ring (5 * m + 2 + 1)) hpr w hw1 hw5) (Quint6.psi_zero n (ring (5 * m + 2 + 1)) w)
    (Quint6.psi_sum n (ring (5 * m + 2 + 1)) hpr w hw1 hw5) 3 (Quint6.psi_nt n (ring (5 * m + 2 + 1)) hpr w hw1 hw5) (Quint6.psi_inv n (ring (5 * m + 2 + 1)) hpr w hw1 hw5) (5 * m + 2) hq (Quint2.charq5 (5 * m + 2))
    (fun a => (frob_bar n m hpr w hw1 hw5 a).trans (Quint6.psi_inv n (ring (5 * m + 2 + 1)) hpr w hw1 hw5 a)) hQ m (by lib_nat)
  rw [psi_iv_neg1 n m hpr w hw1 hw5] at h
  rw [show (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).mul (CharSumGen.jpr (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) (2 * (5 * n + 4)) (Quint6.psi n (ring (5 * m + 2 + 1)) w) 3) (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).one = CharSumGen.jpr (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) (2 * (5 * n + 4)) (Quint6.psi n (ring (5 * m + 2 + 1)) w) 3 by lib_ring] at h
  exact (show (Quint6.psi n (ring (5 * m + 2 + 1)) w) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (5 * m + 2 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) = (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).mul ((Quint6.psi n (ring (5 * m + 2 + 1)) w) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (5 * m + 2 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one)) (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).one by lib_ring).trans h

end Quint7

#print axioms Quint7.frob_bar
#print axioms Quint7.psi_iv_neg1
#print axioms Quint7.quint_recip_inert

#print axioms: 3 theorems, no axioms.

78.180 Quintic residue criterion at \(q\equiv 1\pmod 5\)

For a prime \(q=5(m+1)+1\neq p\) the reduced quintic character satisfies, in \(\mathbb F_q[\zeta _5]\),

\[ \psi (q^{-1})=(A\,p)^{m+1},\qquad A=J(\psi ,\psi )J(\psi ^2,\psi )J(\psi ^3,\psi ). \]

lang/Quint8.lean

/-
  lang/Quint8 — T49, cycle 755 (ℓ = 5 symbol project, step 8): **the quintic residue criterion at q ≡ 1 (mod 5)**.
  p = 10n + 11, ψ = ph5 ∘ χ with values in 𝔽_q[ζ₅] (Quint6), q = 5(m + 1) + 1 prime, q ≠ p:
    frob_fix           ψ(a)^q = ψ(a)              (ζ^q = ζ in 𝔽_q[ζ₅], Quint3.zt_mod)
    quint_recip_split  ψ(q⁻¹) = (A p)^(m+1)  in 𝔽_q[ζ₅],  A = J(ψ, ψ) J(ψ², ψ) J(ψ³, ψ)   (CharSumGen.nth_recip, n = 5)
  With Quint7 (q ≡ 4) this is the quintic analogue of CubicRecip / QuartRecip in the classes 1 and −1 mod 5.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Quint7

namespace Quint8

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable (n m : Nat)

/-- ψ(a)^q = ψ(a) for q = 5(m + 1) + 1. -/
theorem frob_fix (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+192 chars; full line in the Zenodo source)
  by_cases ha : a = (ring (2 * (5 * n + 4) + 1 + 1)).zero
  · rw [ha, Quint6.psi_zero]
    exact HasseCM.pw_zero_succ (Cyclo5.cyc5 (ring (5 * m + 4 + 1))) (5 * m + 4 + 1)
  match Quint4.exists_i n hpr w hw1 hw5 a ha with
  | ⟨i, hi, ei⟩ =>
    have hv : (Quint6.psi n (ring (5 * m + 4 + 1)) w) a = pw (Cyclo5.cyc5 (ring (5 * m + 4 + 1))) (Quint2.zt (ring (5 * m + 4 + 1))) i := by
      unfold Quint6.psi; rw [Quint4.val_of n hpr w hw1 hw5 a ha i hi ei, ← Quint6.ph5_pow, Quint6.ph5_zeta]
    rw [hv, TwoPower.pwpow, show i * (5 * m + 4 + 1 + 1) = 5 * (i * m + i) + i by lib_nat, Quint3.zt_mod]

theorem psi_iv_neg1 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).  -- (+205 chars; full line in the Zenodo source)
  unfold Quint6.psi
  rw [show Quint4.chi5 n w (CharSumGen.iv (2 * (5 * n + 4)) ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) = Quint5.chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one) from rfl, Quint5.chib_neg1 n hpr w hw1 hw5, Quint6.ph5_one]

/-- **Quintic residue criterion at q = 5(m + 1) + 1**: ψ(q⁻¹) = (A p)^(m+1) in 𝔽_q[ζ₅], A = ∏_{i≤3} J(ψ^i, ψ). -/
theorem quint_recip_split (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1   -- (+224 chars; full line in the Zenodo source)
    (Quint6.psi n (ring (5 * m + 4 + 1)) w) (pw (ring (2 * (5 * n + 4) + 1 + 1)) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (5 * m + 4 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) (2 * (5 * n + 4) + 1)) = pw (Cyclo5.cyc5 (ring (5 * m + 4 + 1))) ((Cyclo5.cyc5 (ring (5 * m + 4 + 1))).mul (CharSumGen.jpr (Cyclo5.cyc5 (ring (5 * m + 4 + 1))) (2 * (5 * n + 4)) (Quint6.psi n (ring (5 * m + 4 + 1)) w) 3) (C  -- (+83 chars; full line in the Zenodo source)
  have h := CharSumGen.nth_recip (Cyclo5.cyc5 (ring (5 * m + 4 + 1))) (2 * (5 * n + 4)) hpr (Quint6.psi n (ring (5 * m + 4 + 1)) w) (Quint6.psi_mul n (ring (5 * m + 4 + 1)) hpr w hw1 hw5) (Quint6.psi_one n (ring (5 * m + 4 + 1)) hpr w hw1 hw5) (Quint6.psi_zero n (ring (5 * m + 4 + 1)) w)
    (Quint6.psi_sum n (ring (5 * m + 4 + 1)) hpr w hw1 hw5) 3 (Quint6.psi_nt n (ring (5 * m + 4 + 1)) hpr w hw1 hw5) (Quint6.psi_inv n (ring (5 * m + 4 + 1)) hpr w hw1 hw5) (5 * m + 4) hq (Quint2.charq5 (5 * m + 4))
    (frob_fix n m hpr w hw1 hw5) hQ (m + 1) (by lib_nat)
  rw [psi_iv_neg1 n m hpr w hw1 hw5] at h
  rw [show (Cyclo5.cyc5 (ring (5 * m + 4 + 1))).mul (CharSumGen.jpr (Cyclo5.cyc5 (ring (5 * m + 4 + 1))) (2 * (5 * n + 4)) (Quint6.psi n (ring (5 * m + 4 + 1)) w) 3) (Cyclo5.cyc5 (ring (5 * m + 4 + 1))).one = CharSumGen.jpr (Cyclo5.cyc5 (ring (5 * m + 4 + 1))) (2 * (5 * n + 4)) (Quint6.psi n (ring (5 * m + 4 + 1)) w) 3 by lib_ring] at h
  exact h

end Quint8

#print axioms Quint8.frob_fix
#print axioms Quint8.psi_iv_neg1
#print axioms Quint8.quint_recip_split

#print axioms: 3 theorems, no axioms.

78.181 Quintic residue criterion at every prime

For a prime \(q\) with \(q^4=5M+1\) (every \(q\neq 5\)) the reduced quintic character satisfies, in \(\mathbb F_q[\zeta _5]\),

\[ \psi (q^{-4})=(A\,p)^{M},\qquad A=J(\psi ,\psi )J(\psi ^2,\psi )J(\psi ^3,\psi ). \]

lang/Quint9.lean

/-
  lang/Quint9 — T49, cycle 756 (ℓ = 5 symbol project, step 9): **the quintic residue criterion at every prime q ≠ 5, p**.
  p = 10n + 11, ψ = ph5 ∘ χ with values in 𝔽_q[ζ₅] (Quint6), q prime with q⁴ = 5M + 1 (every q ≠ 5: q has order 1, 2 or 4 mod 5):
    frob_fix4           ψ(a)^(q⁴) = ψ(a)            (ζ^(q⁴) = ζ, Quint3.zt_mod)
    quint_recip_all     ψ(q⁻⁴) = (A p)^M  in 𝔽_q[ζ₅],  A = J(ψ, ψ) J(ψ², ψ) J(ψ³, ψ)   (CharSumGen.nth_recip_pow, n = 5, e = 4)
  This covers the classes q ≡ 2, 3 (mod 5) left open by Quint7 / Quint8.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Quint8
import lang.NthRecipPow

namespace Quint9

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable (n k : Nat)

/-- ψ(a)^(q⁴) = ψ(a) when q⁴ = 5M + 1. -/
theorem frob_fix4 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).on  -- (+214 chars; full line in the Zenodo source)
  rw [hM]
  by_cases ha : a = (ring (2 * (5 * n + 4) + 1 + 1)).zero
  · rw [ha, Quint6.psi_zero]
    exact HasseCM.pw_zero_succ (Cyclo5.cyc5 (ring (k + 1))) (5 * M)
  match Quint4.exists_i n hpr w hw1 hw5 a ha with
  | ⟨i, hi, ei⟩ =>
    have hv : (Quint6.psi n (ring (k + 1)) w) a = pw (Cyclo5.cyc5 (ring (k + 1))) (Quint2.zt (ring (k + 1))) i := by
      unfold Quint6.psi; rw [Quint4.val_of n hpr w hw1 hw5 a ha i hi ei, ← Quint6.ph5_pow, Quint6.ph5_zeta]
    rw [hv, TwoPower.pwpow, show i * (5 * M + 1) = 5 * (i * M) + i by lib_nat, Quint3.zt_mod]

theorem psi_iv_neg1 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).  -- (+189 chars; full line in the Zenodo source)
  unfold Quint6.psi
  rw [show Quint4.chi5 n w (CharSumGen.iv (2 * (5 * n + 4)) ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) = Quint5.chib n w ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one) from rfl, Quint5.chib_neg1 n hpr w hw1 hw5, Quint6.ph5_one]

/-- **Quintic residue criterion at every prime q with q⁴ = 5M + 1**: ψ(q⁻⁴) = (A p)^M in 𝔽_q[ζ₅], A = ∏_{i≤3} J(ψ^i, ψ). -/
theorem quint_recip_all (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 +   -- (+251 chars; full line in the Zenodo source)
    (Quint6.psi n (ring (k + 1)) w) (pw (ring (2 * (5 * n + 4) + 1 + 1)) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) ((k + 1 + 1) ^ 4) (ring (2 * (5 * n + 4) + 1 + 1)).one) (2 * (5 * n + 4) + 1)) = pw (Cyclo5.cyc5 (ring (k + 1))) ((Cyclo5.cyc5 (ring (k + 1))).mul (CharSumGen.jpr (Cyclo5.cyc5 (ring (k + 1))) (2 * (5 * n + 4)) (Quint6.psi n (ring (k + 1)) w) 3) (CharSumGen.Pp (Cyclo5.cyc5 (ring (k + 1)))   -- (+27 chars; full line in the Zenodo source)
  have h := CharSumGen.nth_recip_pow (Cyclo5.cyc5 (ring (k + 1))) (2 * (5 * n + 4)) hpr (Quint6.psi n (ring (k + 1)) w) (Quint6.psi_mul n (ring (k + 1)) hpr w hw1 hw5) (Quint6.psi_one n (ring (k + 1)) hpr w hw1 hw5) (Quint6.psi_zero n (ring (k + 1)) w)
    (Quint6.psi_sum n (ring (k + 1)) hpr w hw1 hw5) 3 (Quint6.psi_nt n (ring (k + 1)) hpr w hw1 hw5) (Quint6.psi_inv n (ring (k + 1)) hpr w hw1 hw5) k hq (Quint2.charq5 k) 4
    (frob_fix4 n k hpr w hw1 hw5 M hM) hQ M hM
  rw [psi_iv_neg1 n k hpr w hw1 hw5] at h
  rw [show (Cyclo5.cyc5 (ring (k + 1))).mul (CharSumGen.jpr (Cyclo5.cyc5 (ring (k + 1))) (2 * (5 * n + 4)) (Quint6.psi n (ring (k + 1)) w) 3) (Cyclo5.cyc5 (ring (k + 1))).one = CharSumGen.jpr (Cyclo5.cyc5 (ring (k + 1))) (2 * (5 * n + 4)) (Quint6.psi n (ring (k + 1)) w) 3 by lib_ring] at h
  exact h

end Quint9

#print axioms Quint9.frob_fix4
#print axioms Quint9.psi_iv_neg1
#print axioms Quint9.quint_recip_all

#print axioms: 3 theorems, no axioms.

78.182 Reflection formula for Jacobi sums

For multiplicative \(\psi ,\varphi \) on \(\mathbb F_p\) with \(\psi (1)=1\), \(\psi (0)=\varphi (0)=0\), with \(\mu (x)=\psi (x^{-1})\varphi (x^{-1})\):

\[ J(\psi ,\varphi )=\psi (-1)\,J(\psi ,\mu ). \]

lang/JacRefl.lean

/-
  lang/JacRefl — T49, cycle 757: **the reflection formula for Jacobi sums** over 𝔽_p, for multiplicative ψ, φ : 𝔽_p → E (any
  commutative ring E), ψ(1) = φ(1) = 1, ψ(0) = φ(0) = 0:
      J(ψ, φ) = ψ(−1) · J(ψ, μ),   μ(x) = ψ(x⁻¹) φ(x⁻¹)   (μ = conj(ψφ)),
  by the involution t ↦ t/(t − 1) of 𝔽_p ∖ {1} (fl, fixing 1):  fl_fl, fl_sub (fl(s) − 1 = (s − 1)⁻¹), iv_neg1 ((−1)⁻¹ = −1).
  Used for the quintic character: J(χ, χ³) = J(χ, χ) and J(χ², χ) = J(χ², χ²), so A = J(χ,χ)J(χ²,χ)J(χ³,χ) = J(χ,χ)² σ₂(J(χ,χ)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.JacobiGauss

namespace JacRefl

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CharSumGen

variable {S : Type} (E : CRing S) (j : Nat)

theorem neg1_ne : ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) ≠ (ring (j + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (j + 1) (by
  calc (ring (j + 1 + 1)).one = (ring (j + 1 + 1)).neg ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := (neg_neg' (ring (j + 1 + 1)) _).symm
    _ = (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).zero := by rw [e]
    _ = (ring (j + 1 + 1)).zero := by lib_ring)

theorem iv_neg1 (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) : iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := by
  have h := iv_mul j hpr ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) (neg1_ne j)
  calc iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := by lib_ring
    _ = ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := by rw [h]; lib_ring

def fl (s : Zp (j + 1 + 1)) : Zp (j + 1 + 1) := if s = (ring (j + 1 + 1)).one then (ring (j + 1 + 1)).one else (ring (j + 1 + 1)).mul s (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))

theorem sub_ne (s : Zp (j + 1 + 1)) (hs : s ≠ (ring (j + 1 + 1)).one) : (ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) ≠ (ring (j + 1 + 1)).zero := fun e => hs (by
  calc s = (ring (j + 1 + 1)).add ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (ring (j + 1 + 1)).one := by lib_ring
    _ = (ring (j + 1 + 1)).one := by rw [e]; lib_ring)

theorem fl_ne (s : Zp (j + 1 + 1)) (hs : s ≠ (ring (j + 1 + 1)).one) : fl j s = (ring (j + 1 + 1)).mul s (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) := by
  unfold fl; rw [ite_neg' _ _ hs]

theorem fl_sub (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (s : Zp (j + 1 + 1)) (hs : s ≠ (ring (j + 1 + 1)).one) : (ring (j + 1 + 1)).add (fl j s) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) := by
  have hu := iv_mul j hpr _ (sub_ne j s hs)
  rw [fl_ne j s hs]
  calc (ring (j + 1 + 1)).add ((ring (j + 1 + 1)).mul s (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)
      = (ring (j + 1 + 1)).add ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).mul (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := by lib_ring
    _ = iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) := by rw [hu]; lib_ring

theorem fl_ne1 (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (s : Zp (j + 1 + 1)) (hs : s ≠ (ring (j + 1 + 1)).one) : fl j s ≠ (ring (j + 1 + 1)).one := fun e => by
  have h1 := fl_sub j hpr s hs
  rw [e, show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).zero by lib_ring] at h1
  have hu := iv_mul j hpr _ (sub_ne j s hs)
  rw [← h1, zero_mul'] at hu
  exact PowerSum.one_ne_zero' (j + 1) hu.symm

theorem fl_fl (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (s : Zp (j + 1 + 1)) : fl j (fl j s) = s := by
  by_cases hs : s = (ring (j + 1 + 1)).one
  · rw [hs]; unfold fl; rw [ite_pos' _ _ rfl, ite_pos' _ _ rfl]
  · have hu := iv_mul j hpr _ (sub_ne j s hs)
    rw [fl_ne j _ (fl_ne1 j hpr s hs), fl_sub j hpr s hs, iv_iv j hpr, fl_ne j s hs]
    calc (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul s (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))
        = (ring (j + 1 + 1)).mul s ((ring (j + 1 + 1)).mul (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) := by lib_ring
      _ = s := by rw [hu]; lib_ring

/-- **J(ψ, φ) = ψ(−1) J(ψ, μ)**, μ(x) = ψ(x⁻¹)φ(x⁻¹). -/
theorem jac_refl (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ φ : Zp (j + 1 + 1) → S) (hψm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (hφm : ∀ x y, φ ((ring (j + 1 + 1)).mul x y) = E.mul (φ x) (φ y)) (hψ1 : ψ (ring (j + 1 + 1)).one = E.one) (hφ0 : φ (ring (j + 1 + 1)).zero = E.zero) (hψ0 : ψ (ring (j + 1 + 1)).zero = E.zero) :
    J2 E j ψ φ = E.mul (ψ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (J2 E j ψ (fun x => E.mul (ψ (iv j x)) (φ (iv j x)))) := by
  have hpm : E.mul (ψ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (ψ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) = E.one := by
    rw [← hψm, show (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).one by lib_ring, hψ1]
  have hre : sumK E (fun s => E.mul (ψ (fl j s)) (φ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg (fl j s))))) (LL j)
      = sumK E (fun t => E.mul (ψ t) (φ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))) (LL j) :=
    reindex E _ (CMInert.complete_allZ (j + 1 + 1) E) (fl j) (fl j) (fl_fl j hpr) (fl_fl j hpr)
      (fun t => E.mul (ψ t) (φ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))))
  have pt : ∀ s, E.mul (ψ (fl j s)) (φ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg (fl j s))))
      = E.mul (ψ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (E.mul (ψ s) (E.mul (ψ (iv j ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg s)))) (φ (iv j ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg s)))))) := fun s => by
    by_cases hs : s = (ring (j + 1 + 1)).one
    · rw [hs]; unfold fl; rw [ite_pos' _ _ rfl, show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).zero by lib_ring, iv_zero, hφ0, hψ0]; lib_ring
    · have e1 : (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg (fl j s)) = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) := by
        rw [← fl_sub j hpr s hs]; lib_ring
      have e2 : (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg s) = (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) := by lib_ring
      rw [e1, e2, iv_mulmul, iv_neg1 j hpr, fl_ne j s hs, hψm, hψm, hφm]
      calc E.mul (E.mul (ψ s) (ψ (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))) (E.mul (φ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (φ (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))))
          = E.mul (E.mul (ψ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (ψ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) (E.mul (E.mul (ψ s) (ψ (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))) (E.mul (φ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (φ (iv j ((ring (j + 1 + 1)).add s ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))))) := by rw [hpm];   -- (+8 chars; full line in the Zenodo source)
        _ = _ := by lib_ring
  unfold J2
  rw [← hre, sumK_ext E _ _ pt]
  exact (mul_sumK E _ _ _).symm

end JacRefl

#print axioms JacRefl.neg1_ne
#print axioms JacRefl.iv_neg1
#print axioms JacRefl.sub_ne
#print axioms JacRefl.fl_ne
#print axioms JacRefl.fl_sub
#print axioms JacRefl.fl_ne1
#print axioms JacRefl.fl_fl
#print axioms JacRefl.jac_refl

#print axioms: 8 theorems, no axioms.

78.183 The quintic product of Jacobi sums

For the reduced quintic character \(\psi \) the product in the residue criteria collapses to two Jacobi sums.

\[ J(\psi ,\psi )\,J(\psi ^2,\psi )\,J(\psi ^3,\psi )=J(\psi ,\psi )^2\,J(\psi ^2,\psi ^2). \]

lang/Quint10.lean

/-
  lang/Quint10 — T49, cycle 758 (ℓ = 5 symbol project, step 10): **the product A of Quint7–9 in terms of two Jacobi sums**.
  For ψ = ph5 ∘ χ (Quint6, values in T[ζ₅], any commutative ring T), ψ(−1) = 1, ψ⁵ = 1 off 0:
    j2_swap        J(a, b) = J(b, a)                                  (t ↦ 1 − t, any E)
    psi5           ψ(x)⁵ = 1 (x ≠ 0)
    J_31           J(ψ³, ψ) = J(ψ, ψ)                                 (swap, JacRefl with conj(ψ⁴) = ψ)
    J_21           J(ψ², ψ) = J(ψ², ψ²)                               (JacRefl with conj(ψ³) = ψ²)
    quint_A        A = J(ψ, ψ) J(ψ², ψ) J(ψ³, ψ) = J(ψ, ψ)² J(ψ², ψ²)
  so the quintic residue criteria read ψ(q) = (J(ψ,ψ)² J(ψ²,ψ²))^(m+1) p^m etc.; J(ψ²,ψ²) is the image of σ₂ J(χ,χ).
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Quint9
import lang.JacRefl

namespace Quint10

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

section Gen
variable {S : Type} (E : CRing S) (j : Nat)

/-- J(a, b) = J(b, a). -/
theorem j2_swap (a b : Zp (j + 1 + 1) → S) : J2 E j a b = J2 E j b a := by
  have hre : sumK E (fun t => E.mul (a ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))) (b ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))))) (LL j)
      = sumK E (fun t => E.mul (a t) (b ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))) (LL j) :=
    reindex E _ (CMInert.complete_allZ (j + 1 + 1) E) (fun t => (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)) (fun t => (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))
      (fun t => by lib_ring) (fun t => by lib_ring) (fun t => E.mul (a t) (b ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))))
  unfold J2
  rw [← hre]
  exact sumK_ext E _ _ (fun t => by rw [show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))) = t by lib_ring, E.mul_comm]) _
end Gen

variable (n : Nat) {R : Type} (T : CRing R)

theorem psi5 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) (x  -- (+155 chars; full line in the Zenodo source)
  show (Cyclo5.cyc5 T).mul (pw (Cyclo5.cyc5 T) ((Quint6.psi n T w) x) (3 + 1)) ((Quint6.psi n T w) x) = (Cyclo5.cyc5 T).one
  rw [Quint6.psi_inv n T hpr w hw1 hw5, ← Quint6.psi_mul n T hpr w hw1 hw5, CharSumGen.iv_mul (2 * (5 * n + 4)) hpr x hx, Quint6.psi_one n T hpr w hw1 hw5]

theorem psi_neg1 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+126 chars; full line in the Zenodo source)
  unfold Quint6.psi; rw [Quint4.chi5_neg1 n hpr w hw1 hw5, Quint6.ph5_one]

theorem psp_mul (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)  -- (+344 chars; full line in the Zenodo source)
  unfold CharSumGen.psp; rw [Quint6.psi_mul n T hpr w hw1 hw5, pw_mul]

theorem psp_zero (w : Zp (2 * (5 * n + 4) + 1 + 1)) (k : Nat) : (CharSumGen.psp (Cyclo5.cyc5 T) (2 * (5 * n + 4)) (Quint6.psi n T w) (k + 1)) (ring (2 * (5 * n + 4) + 1 + 1)).zero = (Cyclo5.cyc5 T).zero := by
  unfold CharSumGen.psp; rw [Quint6.psi_zero]; exact HasseCM.pw_zero_succ (Cyclo5.cyc5 T) k

theorem psp_neg1 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+189 chars; full line in the Zenodo source)
  unfold CharSumGen.psp; rw [psi_neg1 n T hpr w hw1 hw5]; exact CubicChar.pw_one'' (Cyclo5.cyc5 T) k

theorem muA (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) (x   -- (+249 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [(Cyclo5.cyc5 T).mul_comm, show (Cyclo5.cyc5 T).mul (pw (Cyclo5.cyc5 T) ((Quint6.psi n T w) (CharSumGen.iv (2 * (5 * n + 4)) x)) 3) ((Quint6.psi n T w) (CharSumGen.iv (2 * (5 * n + 4)) x)) = pw (Cyclo5.cyc5 T) ((Quint6.psi n T w) (CharSumGen.iv (2 * (5 * n + 4)) x)) (3 + 1) from rfl,
    Quint6.psi_inv n T hpr w hw1 hw5, CharSumGen.iv_iv (2 * (5 * n + 4)) hpr]

theorem muB (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) (x   -- (+302 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [show (Cyclo5.cyc5 T).mul (pw (Cyclo5.cyc5 T) ((Quint6.psi n T w) (CharSumGen.iv (2 * (5 * n + 4)) x)) 2) ((Quint6.psi n T w) (CharSumGen.iv (2 * (5 * n + 4)) x)) = pw (Cyclo5.cyc5 T) ((Quint6.psi n T w) (CharSumGen.iv (2 * (5 * n + 4)) x)) 3 from rfl]
  by_cases hx : x = (ring (2 * (5 * n + 4) + 1 + 1)).zero
  · rw [hx, CharSumGen.iv_zero, Quint6.psi_zero]
    exact (HasseCM.pw_zero_succ (Cyclo5.cyc5 T) 2).trans (HasseCM.pw_zero_succ (Cyclo5.cyc5 T) 1).symm
  rw [← Quint6.psi_inv n T hpr w hw1 hw5, TwoPower.pwpow, show (3 + 1) * 3 = 5 * 2 + 2 by rfl, HasseCM.pw_add', ← TwoPower.pwpow, psi5 n T hpr w hw1 hw5 x hx,
    CubicChar.pw_one'', (Cyclo5.cyc5 T).one_mul]

theorem J_11 : (CharSumGen.J2 (Cyclo5.cyc5 T) (2 * (5 * n + 4)) (CharSumGen.psp (Cyclo5.cyc5 T) (2 * (5 * n + 4)) (Quint6.psi n T w) 1) (Quint6.psi n T w)) = (CharSumGen.J2 (Cyclo5.cyc5 T) (2 * (5 * n + 4)) (Quint6.psi n T w) (Quint6.psi n T w)) := by
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo5.cyc5 T) _ _ (fun t => by unfold CharSumGen.psp; rw [show pw (Cyclo5.cyc5 T) ((Quint6.psi n T w) t) 1 = (Quint6.psi n T w) t from (Cyclo5.cyc5 T).one_mul _]) _

theorem J_31 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) :   -- (+236 chars; full line in the Zenodo source)
  rw [j2_swap, JacRefl.jac_refl (Cyclo5.cyc5 T) (2 * (5 * n + 4)) hpr (Quint6.psi n T w) (CharSumGen.psp (Cyclo5.cyc5 T) (2 * (5 * n + 4)) (Quint6.psi n T w) 3) (Quint6.psi_mul n T hpr w hw1 hw5) (psp_mul n T hpr w hw1 hw5 3)
    (Quint6.psi_one n T hpr w hw1 hw5) (psp_zero n T w 2) (Quint6.psi_zero n T w), psi_neg1 n T hpr w hw1 hw5, (Cyclo5.cyc5 T).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo5.cyc5 T) _ _ (fun t => congrArg ((Cyclo5.cyc5 T).mul ((Quint6.psi n T w) t)) (muA n T hpr w hw1 hw5 _)) _

theorem J_21 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) :   -- (+342 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo5.cyc5 T) (2 * (5 * n + 4)) hpr (CharSumGen.psp (Cyclo5.cyc5 T) (2 * (5 * n + 4)) (Quint6.psi n T w) 2) (Quint6.psi n T w) (psp_mul n T hpr w hw1 hw5 2) (Quint6.psi_mul n T hpr w hw1 hw5)
    (by unfold CharSumGen.psp; rw [Quint6.psi_one n T hpr w hw1 hw5]; exact CubicChar.pw_one'' (Cyclo5.cyc5 T) 2)
    (Quint6.psi_zero n T w) (psp_zero n T w 1), psp_neg1 n T hpr w hw1 hw5 2, (Cyclo5.cyc5 T).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo5.cyc5 T) _ _ (fun t => congrArg ((Cyclo5.cyc5 T).mul ((CharSumGen.psp (Cyclo5.cyc5 T) (2 * (5 * n + 4)) (Quint6.psi n T w) 2) t)) (muB n T hpr w hw1 hw5 _)) _

/-- **A = J(ψ,ψ)J(ψ²,ψ)J(ψ³,ψ) = J(ψ,ψ)² J(ψ²,ψ²).** -/
theorem quint_A (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)  -- (+492 chars; full line in the Zenodo source)
  show (Cyclo5.cyc5 T).mul ((Cyclo5.cyc5 T).mul ((Cyclo5.cyc5 T).mul (Cyclo5.cyc5 T).one (CharSumGen.J2 (Cyclo5.cyc5 T) (2 * (5 * n + 4)) (CharSumGen.psp (Cyclo5.cyc5 T) (2 * (5 * n + 4)) (Quint6.psi n T w) 1) (Quint6.psi n T w))) (CharSumGen.J2 (Cyclo5.cyc5 T) (2 * (5 * n + 4)) (CharSumGen.psp (Cyclo5.cyc5 T) (2 * (5 * n + 4)) (Quint6.psi n T w) 2) (Quint6.psi n T w))) (CharSumGen.J2 (Cyclo5.cyc5  -- (+117 chars; full line in the Zenodo source)
  rw [J_11, J_21 n T hpr w hw1 hw5, J_31 n T hpr w hw1 hw5]; lib_ring

end Quint10

#print axioms Quint10.j2_swap
#print axioms Quint10.psi5
#print axioms Quint10.psi_neg1
#print axioms Quint10.psp_mul
#print axioms Quint10.psp_zero
#print axioms Quint10.psp_neg1
#print axioms Quint10.muA
#print axioms Quint10.muB
#print axioms Quint10.J_11
#print axioms Quint10.J_31
#print axioms Quint10.J_21
#print axioms Quint10.quint_A

#print axioms: 12 theorems, no axioms.

78.184 The quintic product through one Jacobi sum

With \(\sigma _2:\zeta \mapsto \zeta ^2\) on \(\mathbb Z[\zeta _5]\) and \(J=J(\chi ,\chi )\), the product in the residue criteria is the reduction of \(J^2\sigma _2(J)\).

\[ J(\psi ,\psi )J(\psi ^2,\psi )J(\psi ^3,\psi )=\overline {J^2\,\sigma _2(J)}. \]

lang/Quint11.lean

/-
  lang/Quint11 — T49, cycle 759 (ℓ = 5 symbol project, step 11): **σ₂ on ℤ[ζ₅] and A = ph5(J² σ₂(J))**.
    sig2, sig2_mul, sig2_add, sig2_sum   σ₂ : ζ ↦ ζ² is a ring map of ℤ[ζ₅] (coordinates (a₀ − a₂, a₃ − a₂, a₁ − a₂, −a₂))
    sig2_chi                             σ₂(χ(x)) = χ(x)²
    sig2_J                               σ₂ J(χ, χ) = J(χ², χ²)
    JJ1, JJ2                             J(ψ, ψ) = ph5 J(χ, χ),  J(ψ², ψ²) = ph5 σ₂J(χ, χ)
    quint_A'                             A = J(ψ,ψ)J(ψ²,ψ)J(ψ³,ψ) = ph5(J² · σ₂ J),  J = J(χ, χ) ∈ ℤ[ζ₅]
  So the quintic residue criteria (Quint7–9) are statements about the single element J(χ, χ) of norm p (Quint5).
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Quint10

namespace Quint11

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CharSumGen

def sig2 (z : Int × Int × Int × Int) : Int × Int × Int × Int := (z.1 + -z.2.2.1, z.2.2.2 + -z.2.2.1, z.2.1 + -z.2.2.1, -z.2.2.1)

theorem sig2_mul : ∀ x y : Int × Int × Int × Int, sig2 ((Cyclo5.cyc5 LibInt.intR).mul x y) = (Cyclo5.cyc5 LibInt.intR).mul (sig2 x) (sig2 y)
  | (a0, a1, a2, a3), (b0, b1, b2, b3) => by
    apply Prod.ext
    · show ((((a0 * b0) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) + ((a2 * b3) + (a3 * b2))) + (-((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))))) = ((((a0 + (-a2)) * (b0 + (-b2))) + (-((((a3 + (-a2)) * (-b2)) + ((a1 + (-a2)) * (b1 + (-b2)))) + ((-a2) * (b3 + (-b2)))))) + (((a1 + (-a2)) * (-b2)) + ((-a2) * (b1 + (-b2)))))
      lib_int
    apply Prod.ext
    · show ((((((a0 * b3) + (a1 * b2)) + (a2 * b1)) + (a3 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) + (-((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))))) = (((((a0 + (-a2)) * (b3 + (-b2))) + ((a3 + (-a2)) * (b0 + (-b2)))) + (-((((a3 + (-a2)) * (-b2)) + ((a1 + (-a2)) * (b1 + (-b2)))) + ((-a2) * (b3 + (-b2)))))) + ((-a2) * (-b2)))
      lib_int
    apply Prod.ext
    · show (((((a0 * b1) + (a1 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))) + (a3 * b3)) + (-((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1)))))) = (((((a0 + (-a2)) * (b1 + (-b2))) + ((a3 + (-a2)) * (b3 + (-b2)))) + ((a1 + (-a2)) * (b0 + (-b2)))) + (-((((a3 + (-a2)) * (-b2)) + ((a1 + (-a2)) * (b1 + (-b2)))) + ((-a2) * (b3 + (-b2))))))
      lib_int
    · show (-((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + (-(((a1 * b3) + (a2 * b2)) + (a3 * b1))))) = ((((((a0 + (-a2)) * (-b2)) + ((a3 + (-a2)) * (b1 + (-b2)))) + ((a1 + (-a2)) * (b3 + (-b2)))) + ((-a2) * (b0 + (-b2)))) + (-((((a3 + (-a2)) * (-b2)) + ((a1 + (-a2)) * (b1 + (-b2)))) + ((-a2) * (b3 + (-b2))))))
      lib_int

theorem sig2_add : ∀ x y : Int × Int × Int × Int, sig2 ((Cyclo5.cyc5 LibInt.intR).add x y) = (Cyclo5.cyc5 LibInt.intR).add (sig2 x) (sig2 y)
  | (a0, a1, a2, a3), (b0, b1, b2, b3) => by
    apply Prod.ext
    · show ((a0 + b0) + (-(a2 + b2))) = ((a0 + (-a2)) + (b0 + (-b2)))
      lib_int
    apply Prod.ext
    · show ((a3 + b3) + (-(a2 + b2))) = ((a3 + (-a2)) + (b3 + (-b2)))
      lib_int
    apply Prod.ext
    · show ((a1 + b1) + (-(a2 + b2))) = ((a1 + (-a2)) + (b1 + (-b2)))
      lib_int
    · show (-(a2 + b2)) = ((-a2) + (-b2))
      lib_int

theorem sig2_sum {A' : Type} (f : A' → Int × Int × Int × Int) : ∀ l : List A', sig2 (sumK (Cyclo5.cyc5 LibInt.intR) f l) = sumK (Cyclo5.cyc5 LibInt.intR) (fun a => sig2 (f a)) l
  | [] => rfl
  | a :: l => by
    show sig2 ((Cyclo5.cyc5 LibInt.intR).add (f a) (sumK (Cyclo5.cyc5 LibInt.intR) f l)) = (Cyclo5.cyc5 LibInt.intR).add (sig2 (f a)) (sumK (Cyclo5.cyc5 LibInt.intR) (fun a => sig2 (f a)) l)
    rw [sig2_add, sig2_sum f l]

variable (n : Nat)

theorem sig2_chi (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+156 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  by_cases hx : x = (ring (2 * (5 * n + 4) + 1 + 1)).zero
  · rw [hx, Quint4.chi5_zero]; rfl
  match Quint4.exists_i n hpr w hw1 hw5 x hx with
  | ⟨i, hi, ei⟩ =>
    rw [Quint4.val_of n hpr w hw1 hw5 x hx i hi ei]
    match i, hi with
    | 0, _ => rfl
    | 1, _ => rfl
    | 2, _ => rfl
    | 3, _ => rfl
    | 4, _ => rfl
    | k + 5, hk => exact absurd hk (fun h => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) h))

theorem sig2_J (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one)   -- (+332 chars; full line in the Zenodo source)
  unfold CharSumGen.J2; rw [sig2_sum]
  exact sumK_ext (Cyclo5.cyc5 LibInt.intR) _ _ (fun t => by rw [sig2_mul, sig2_chi n hpr w hw1 hw5, sig2_chi n hpr w hw1 hw5]) _

variable {R : Type} (T : CRing R)

theorem JJ1 (w : Zp (2 * (5 * n + 4) + 1 + 1)) : (CharSumGen.J2 (Cyclo5.cyc5 T) (2 * (5 * n + 4)) (Quint6.psi n T w) (Quint6.psi n T w)) = Quint6.ph5 T (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w)) := by
  unfold CharSumGen.J2; rw [Quint6.ph5_sum]
  exact sumK_ext (Cyclo5.cyc5 T) _ _ (fun t => (Quint6.ph5_mul T _ _).symm) _

theorem JJ2 (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) : (  -- (+316 chars; full line in the Zenodo source)
  rw [sig2_J n hpr w hw1 hw5]
  unfold CharSumGen.J2; rw [Quint6.ph5_sum]
  exact sumK_ext (Cyclo5.cyc5 T) _ _ (fun t => by unfold CharSumGen.psp; rw [Quint6.ph5_mul, ← Quint6.ph5_pow, ← Quint6.ph5_pow]; rfl) _

/-- **A = ph5(J² σ₂J)**, J = J(χ, χ) ∈ ℤ[ζ₅]. -/
theorem quint_A' (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one  -- (+453 chars; full line in the Zenodo source)
  rw [Quint10.quint_A n T hpr w hw1 hw5, JJ1 n T w, JJ2 n T hpr w hw1 hw5, Quint6.ph5_mul, Quint6.ph5_mul]

end Quint11

#print axioms Quint11.sig2_mul
#print axioms Quint11.sig2_add
#print axioms Quint11.sig2_sum
#print axioms Quint11.sig2_chi
#print axioms Quint11.sig2_J
#print axioms Quint11.JJ1
#print axioms Quint11.JJ2
#print axioms Quint11.quint_A'

#print axioms: 8 theorems, no axioms.

78.185 Quintic residue criteria through one Jacobi sum

With \(J=J(\chi ,\chi )\in \mathbb Z[\zeta _5]\), \(N(J)=p\), in \(\mathbb F_q[\zeta _5]\) for primes \(q\neq 5,p\) with \(q^4=5M+1\):

\[ \psi (q^{-4})=\bigl (\overline {J^2\sigma _2(J)}\;p\bigr )^{M}. \]

lang/Quint12.lean

/-
  lang/Quint12 — T49, cycle 760 (ℓ = 5 symbol project, step 12): **the quintic residue criteria through the single Jacobi sum
  J = J(χ, χ) ∈ ℤ[ζ₅]** (N(J) = J·conj(J) = p, Quint5), reduced to 𝔽_q[ζ₅] by ph5 (Quint6), Pp = p·1:
    quint_J_inert   q = 5m + 4:        ψ(q)    = ph5(J² σ₂J)^(m+1) · Pp^m
    quint_J_split   q = 5(m + 1) + 1:  ψ(q⁻¹)  = (ph5(J² σ₂J) · Pp)^(m+1)
    quint_J_all     q⁴ = 5M + 1:       ψ(q⁻⁴)  = (ph5(J² σ₂J) · Pp)^M
  (Quint7, Quint8, Quint9 with Quint11.quint_A'.)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Quint11

namespace Quint12

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

variable (n m k : Nat)

/-- **q ≡ 4 (mod 5)**: ψ(q) = ph5(J² σ₂J)^(m+1) p^m in 𝔽_q[ζ₅]. -/
theorem quint_J_inert (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)  -- (+220 chars; full line in the Zenodo source)
    (Quint6.psi n (ring (5 * m + 2 + 1)) w) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (5 * m + 2 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) = (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).mul (pw (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) (Quint6.ph5 (ring (5 * m + 2 + 1)) ((Cyclo5.cyc5 LibInt.intR).mul ((Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Q  -- (+356 chars; full line in the Zenodo source)
  rw [Quint7.quint_recip_inert n m hpr w hw1 hw5 hq hQ , Quint11.quint_A' n (ring (5 * m + 2 + 1)) hpr w hw1 hw5]

/-- **q ≡ 1 (mod 5)**: ψ(q⁻¹) = (ph5(J² σ₂J) p)^(m+1) in 𝔽_q[ζ₅]. -/
theorem quint_J_split (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)  -- (+220 chars; full line in the Zenodo source)
    (Quint6.psi n (ring (5 * m + 4 + 1)) w) (pw (ring (2 * (5 * n + 4) + 1 + 1)) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (5 * m + 4 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) (2 * (5 * n + 4) + 1)) = pw (Cyclo5.cyc5 (ring (5 * m + 4 + 1))) ((Cyclo5.cyc5 (ring (5 * m + 4 + 1))).mul (Quint6.ph5 (ring (5 * m + 4 + 1)) ((Cyclo5.cyc5 LibInt.intR).mul ((Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.J2 (Cyc  -- (+372 chars; full line in the Zenodo source)
  rw [Quint8.quint_recip_split n m hpr w hw1 hw5 hq hQ , Quint11.quint_A' n (ring (5 * m + 4 + 1)) hpr w hw1 hw5]

/-- **every prime q ≠ 5, p** (q⁴ = 5M + 1): ψ(q⁻⁴) = (ph5(J² σ₂J) p)^M in 𝔽_q[ζ₅]. -/
theorem quint_J_all (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).  -- (+245 chars; full line in the Zenodo source)
    (Quint6.psi n (ring (k + 1)) w) (pw (ring (2 * (5 * n + 4) + 1 + 1)) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) ((k + 1 + 1) ^ 4) (ring (2 * (5 * n + 4) + 1 + 1)).one) (2 * (5 * n + 4) + 1)) = pw (Cyclo5.cyc5 (ring (k + 1))) ((Cyclo5.cyc5 (ring (k + 1))).mul (Quint6.ph5 (ring (k + 1)) ((Cyclo5.cyc5 LibInt.intR).mul ((Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n   -- (+324 chars; full line in the Zenodo source)
  rw [Quint9.quint_recip_all n k hpr w hw1 hw5 hq hQ M hM , Quint11.quint_A' n (ring (k + 1)) hpr w hw1 hw5]

end Quint12

#print axioms Quint12.quint_J_inert
#print axioms Quint12.quint_J_split
#print axioms Quint12.quint_J_all

#print axioms: 3 theorems, no axioms.

78.186 Quintic split case in symbol form

For a prime \(q=5(m+1)+1\) and \(u\in \mathbb F_q\) with \(\Phi _5(u)=0\), evaluation at \(u\) turns the criterion into a product of quintic symbols.

\[ \mathrm {ev}_u\,\psi (q^{-1})=\bigl (J(u)^3J(u^2)J(u^4)\bigr )^{(q-1)/5}. \]

lang/Quint13.lean

/-
  lang/Quint13 — T49, cycle 761 (ℓ = 5 symbol project, step 13): **the split case q ≡ 1 (mod 5) in symbol form**.
  For u ∈ T with 1 + u + u² + u³ + u⁴ = 0 (T any commutative ring), ev_u(a₀ + a₁ζ + a₂ζ² + a₃ζ³) = a₀ + a₁u + a₂u² + a₃u³ is a ring
  map T[ζ₅] → T (ev_mul: generated certificate (Π − ev) = Q·Φ₅(u)), ev_u ∘ σ₂ = ev_(u²), ev_u ∘ conj = ev_(u⁴). On Quint12.quint_J_split
  (q = 5(m + 1) + 1, u ∈ 𝔽_q a root of Φ₅, i.e. a prime π | q of ℤ[ζ₅]) with p = J·conj(J) (Quint5):
      quint_sym_split    ev_u ψ(q⁻¹) = (J(u)³ · J(u²) · J(u⁴))^((q−1)/5)       in 𝔽_q,
  i.e. χ̄_p(q) ≡ (J/π)₅³ (J/π₂)₅ (J/π₄)₅ with (X/π_k)₅ = X(u^k)^((q−1)/5), J = J(χ, χ).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Quint12

namespace Quint13

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius
open LibRing (castZ castN)

section Gen
variable {R : Type} (T : CRing R) (u : R)

def ev (v : R) (x : R × R × R × R) : R := (T.add (T.add (T.add x.1 (T.mul x.2.1 v)) (T.mul x.2.2.1 (T.mul v v))) (T.mul x.2.2.2 (T.mul (T.mul v v) v)))

theorem ev_add (v : R) : ∀ x y : R × R × R × R, ev T v ((Cyclo5.cyc5 T).add x y) = T.add (ev T v x) (ev T v y)
  | (a0, a1, a2, a3), (b0, b1, b2, b3) => by
    show (T.add (T.add (T.add (T.add a0 b0) (T.mul (T.add a1 b1) v)) (T.mul (T.add a2 b2) (T.mul v v))) (T.mul (T.add a3 b3) (T.mul (T.mul v v) v))) = T.add (T.add (T.add (T.add a0 (T.mul a1 v)) (T.mul a2 (T.mul v v))) (T.mul a3 (T.mul (T.mul v v) v))) (T.add (T.add (T.add b0 (T.mul b1 v)) (T.mul b2 (T.mul v v))) (T.mul b3 (T.mul (T.mul v v) v)))
    lib_ring

theorem ev_mul (hu : (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) = T.zero) : ∀ x y : R × R × R × R, ev T u ((Cyclo5.cyc5 T).mul x y) = T.mul (ev T u x) (ev T u y)
  | (a0, a1, a2, a3), (b0, b1, b2, b3) => by
    show (T.add (T.add (T.add (T.add (T.add (T.mul a0 b0) (T.neg (T.add (T.add (T.mul a1 b3) (T.mul a2 b2)) (T.mul a3 b1)))) (T.add (T.mul a2 b3) (T.mul a3 b2))) (T.mul (T.add (T.add (T.add (T.mul a0 b1) (T.mul a1 b0)) (T.neg (T.add (T.add (T.mul a1 b3) (T.mul a2 b2)) (T.mul a3 b1)))) (T.mul a3 b3)) u)) (T.mul (T.add (T.add (T.add (T.mul a0 b2) (T.mul a1 b1)) (T.mul a2 b0)) (T.neg (T.add (T.add (T  -- (+442 chars; full line in the Zenodo source)
    calc (T.add (T.add (T.add (T.add (T.add (T.mul a0 b0) (T.neg (T.add (T.add (T.mul a1 b3) (T.mul a2 b2)) (T.mul a3 b1)))) (T.add (T.mul a2 b3) (T.mul a3 b2))) (T.mul (T.add (T.add (T.add (T.mul a0 b1) (T.mul a1 b0)) (T.neg (T.add (T.add (T.mul a1 b3) (T.mul a2 b2)) (T.mul a3 b1)))) (T.mul a3 b3)) u)) (T.mul (T.add (T.add (T.add (T.mul a0 b2) (T.mul a1 b1)) (T.mul a2 b0)) (T.neg (T.add (T.add (T  -- (+851 chars; full line in the Zenodo source)
      _ = T.mul (T.add (T.add (T.add a0 (T.mul a1 u)) (T.mul a2 (T.mul u u))) (T.mul a3 (T.mul (T.mul u u) u))) (T.add (T.add (T.add b0 (T.mul b1 u)) (T.mul b2 (T.mul u u))) (T.mul b3 (T.mul (T.mul u u) u))) := by rw [hu]; lib_ring

theorem ev_one (v : R) : ev T v (Cyclo5.cyc5 T).one = T.one := by
  show (T.add (T.add (T.add T.one (T.mul T.zero v)) (T.mul T.zero (T.mul v v))) (T.mul T.zero (T.mul (T.mul v v) v))) = T.one
  lib_ring

theorem ev_pow (hu : (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) = T.zero) (x : R × R × R × R) : ∀ k, ev T u (pw (Cyclo5.cyc5 T) x k) = pw T (ev T u x) k
  | 0 => ev_one T u
  | k + 1 => by
    show ev T u ((Cyclo5.cyc5 T).mul (pw (Cyclo5.cyc5 T) x k) x) = T.mul (pw T (ev T u x) k) (ev T u x)
    rw [ev_mul T u hu, ev_pow hu x k]

theorem evph_mul (hu : (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) = T.zero) (a b : Int × Int × Int × Int) :
    ev T u (Quint6.ph5 T ((Cyclo5.cyc5 LibInt.intR).mul a b)) = T.mul (ev T u (Quint6.ph5 T a)) (ev T u (Quint6.ph5 T b)) := by
  rw [Quint6.ph5_mul, ev_mul T u hu]

theorem aux_sig2 (hu : (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) = T.zero) (c0 c1 c2 c3 : R) : (T.add (T.add (T.add (T.add c0 (T.neg c2)) (T.mul (T.add c3 (T.neg c2)) u)) (T.mul (T.add c1 (T.neg c2)) (T.mul u u))) (T.mul (T.neg c2) (T.mul (T.mul u u) u))) = (T.add (T.add (T.add c0 (T.mul c1 (T.mul u u))) (T.mul c2 (T.mul (T.mul u u) (  -- (+83 chars; full line in the Zenodo source)
  calc (T.add (T.add (T.add (T.add c0 (T.neg c2)) (T.mul (T.add c3 (T.neg c2)) u)) (T.mul (T.add c1 (T.neg c2)) (T.mul u u))) (T.mul (T.neg c2) (T.mul (T.mul u u) u))) = T.add (T.add (T.add (T.add c0 (T.mul c1 (T.mul u u))) (T.mul c2 (T.mul (T.mul u u) (T.mul u u)))) (T.mul c3 (T.mul (T.mul (T.mul u u) (T.mul u u)) (T.mul u u)))) (T.neg (T.mul (T.add (T.add c2 (T.neg (T.mul c3 u))) (T.mul c3 (T.mu  -- (+131 chars; full line in the Zenodo source)
    _ = (T.add (T.add (T.add c0 (T.mul c1 (T.mul u u))) (T.mul c2 (T.mul (T.mul u u) (T.mul u u)))) (T.mul c3 (T.mul (T.mul (T.mul u u) (T.mul u u)) (T.mul u u)))) := by rw [hu]; lib_ring

theorem aux_conj (hu : (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) = T.zero) (c0 c1 c2 c3 : R) : (T.add (T.add (T.add (T.add c0 (T.neg c1)) (T.mul (T.neg c1) u)) (T.mul (T.add c3 (T.neg c1)) (T.mul u u))) (T.mul (T.add c2 (T.neg c1)) (T.mul (T.mul u u) u))) = (T.add (T.add (T.add c0 (T.mul c1 (T.mul (T.mul (T.mul u u) u) u))) (T.mul c2   -- (+203 chars; full line in the Zenodo source)
  calc (T.add (T.add (T.add (T.add c0 (T.neg c1)) (T.mul (T.neg c1) u)) (T.mul (T.add c3 (T.neg c1)) (T.mul u u))) (T.mul (T.add c2 (T.neg c1)) (T.mul (T.mul u u) u))) = T.add (T.add (T.add (T.add c0 (T.mul c1 (T.mul (T.mul (T.mul u u) u) u))) (T.mul c2 (T.mul (T.mul (T.mul (T.mul u u) u) u) (T.mul (T.mul (T.mul u u) u) u)))) (T.mul c3 (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) (T.mul (T.mul (T  -- (+551 chars; full line in the Zenodo source)
    _ = (T.add (T.add (T.add c0 (T.mul c1 (T.mul (T.mul (T.mul u u) u) u))) (T.mul c2 (T.mul (T.mul (T.mul (T.mul u u) u) u) (T.mul (T.mul (T.mul u u) u) u)))) (T.mul c3 (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) (T.mul (T.mul (T.mul u u) u) u)) (T.mul (T.mul (T.mul u u) u) u)))) := by rw [hu]; lib_ring

theorem ev_sig2 (hu : (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) = T.zero) : ∀ z : Int × Int × Int × Int, ev T u (Quint6.ph5 T (Quint11.sig2 z)) = ev T (T.mul u u) (Quint6.ph5 T z)
  | (a0, a1, a2, a3) => by
    show (T.add (T.add (T.add (castZ T (a0 + -a2)) (T.mul (castZ T (a3 + -a2)) u)) (T.mul (castZ T (a1 + -a2)) (T.mul u u))) (T.mul (castZ T (-a2)) (T.mul (T.mul u u) u))) = (T.add (T.add (T.add (castZ T a0) (T.mul (castZ T a1) (T.mul u u))) (T.mul (castZ T a2) (T.mul (T.mul u u) (T.mul u u)))) (T.mul (castZ T a3) (T.mul (T.mul (T.mul u u) (T.mul u u)) (T.mul u u))))
    repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    exact aux_sig2 T u hu _ _ _ _

theorem ev_conj (hu : (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) = T.zero) : ∀ z : Int × Int × Int × Int, ev T u (Quint6.ph5 T (Quint5.conj5 z)) = ev T (T.mul (T.mul (T.mul u u) u) u) (Quint6.ph5 T z)
  | (a0, a1, a2, a3) => by
    show (T.add (T.add (T.add (castZ T (a0 + -a1)) (T.mul (castZ T (-a1)) u)) (T.mul (castZ T (a3 + -a1)) (T.mul u u))) (T.mul (castZ T (a2 + -a1)) (T.mul (T.mul u u) u))) = (T.add (T.add (T.add (castZ T a0) (T.mul (castZ T a1) (T.mul (T.mul (T.mul u u) u) u))) (T.mul (castZ T a2) (T.mul (T.mul (T.mul (T.mul u u) u) u) (T.mul (T.mul (T.mul u u) u) u)))) (T.mul (castZ T a3) (T.mul (T.mul (T.mul (T.  -- (+89 chars; full line in the Zenodo source)
    repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    exact aux_conj T u hu _ _ _ _
end Gen

variable (n m : Nat)

theorem Pp_eq (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) {  -- (+333 chars; full line in the Zenodo source)
  have h1 : CharSumGen.Pp (Cyclo5.cyc5 T) (2 * (5 * n + 4)) = Quint6.ph5 T (CharSumGen.Pp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4))) := by
    unfold CharSumGen.Pp; rw [Quint6.ph5_sum]
    exact sumK_ext (Cyclo5.cyc5 T) _ _ (fun _ => (Quint6.ph5_one T).symm) _
  have h2 : CharSumGen.Pp (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) = (Int.ofNat (2 * (5 * n + 4) + 1 + 1 + 1), 0, 0, 0) := Quint5.sum_one5 n _
  have h3 : (Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w)) (Quint5.conj5 (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w))) = (Int.ofNat (2 * (5 * n + 4) + 1 + 1 + 1), 0, 0, 0) := Quint5.quint_norm n hpr w hw1 hw5
  rw [h1, h2, h3]

/-- **Split case q ≡ 1 (mod 5) in symbol form**: ev_u ψ(q⁻¹) = (J(u)³ J(u²) J(u⁴))^((q−1)/5) in 𝔽_q, J = J(χ, χ). -/
theorem quint_sym_split (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 +   -- (+220 chars; full line in the Zenodo source)
    (u : Zp (5 * m + 4 + 1)) (hu : ((ring (5 * m + 4 + 1)).add ((ring (5 * m + 4 + 1)).add ((ring (5 * m + 4 + 1)).add ((ring (5 * m + 4 + 1)).add (ring (5 * m + 4 + 1)).one u) ((ring (5 * m + 4 + 1)).mul u u)) ((ring (5 * m + 4 + 1)).mul ((ring (5 * m + 4 + 1)).mul u u) u)) ((ring (5 * m + 4 + 1)).mul ((ring (5 * m + 4 + 1)).mul ((ring (5 * m + 4 + 1)).mul u u) u) u)) = (ring (5 * m + 4 + 1)).zer  -- (+4 chars; full line in the Zenodo source)
    ev (ring (5 * m + 4 + 1)) u (Quint6.psi n (ring (5 * m + 4 + 1)) w (pw (ring (2 * (5 * n + 4) + 1 + 1)) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (5 * m + 4 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) (2 * (5 * n + 4) + 1)))
      = pw (ring (5 * m + 4 + 1)) ((ring (5 * m + 4 + 1)).mul ((ring (5 * m + 4 + 1)).mul ((ring (5 * m + 4 + 1)).mul (ev (ring (5 * m + 4 + 1)) u (Quint6.ph5 (ring (5 * m + 4 + 1)) (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w)))) (ev (ring (5 * m + 4 + 1)) u (Quint6.ph5 (ring (5 * m + 4 + 1)) (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4  -- (+697 chars; full line in the Zenodo source)
  have h := Quint12.quint_J_split n m hpr w hw1 hw5 hq hQ
  rw [Pp_eq n hpr w hw1 hw5 (ring (5 * m + 4 + 1)), ← Quint6.ph5_mul] at h
  have e := congrArg (ev (ring (5 * m + 4 + 1)) u) h
  rw [ev_pow (ring (5 * m + 4 + 1)) u hu] at e
  repeat rw [evph_mul (ring (5 * m + 4 + 1)) u hu] at e
  rw [ev_sig2 (ring (5 * m + 4 + 1)) u hu, ev_conj (ring (5 * m + 4 + 1)) u hu] at e
  rw [e]
  apply congrArg (fun x => pw (ring (5 * m + 4 + 1)) x (m + 1))
  lib_ring

end Quint13

#print axioms Quint13.ev_add
#print axioms Quint13.ev_mul
#print axioms Quint13.ev_one
#print axioms Quint13.ev_pow
#print axioms Quint13.evph_mul
#print axioms Quint13.aux_sig2
#print axioms Quint13.aux_conj
#print axioms Quint13.ev_sig2
#print axioms Quint13.ev_conj
#print axioms Quint13.Pp_eq
#print axioms Quint13.quint_sym_split

#print axioms: 11 theorems, no axioms.

78.187 Quintic case \(q\equiv 4\pmod 5\) in symbol form

For a prime \(q=5m+4\), a ring \(K\supseteq \mathbb F_q\) and \(u\in K\) with \(\Phi _5(u)=0\):

\[ \mathrm {ev}_u\,\psi (q)=\bigl (J(u)^2J(u^2)\bigr )^{m+1}\bigl (J(u)J(u^4)\bigr )^{m}. \]

lang/Quint14.lean

/-
  lang/Quint14 — T49, cycle 762 (ℓ = 5 symbol project, step 14): **the case q ≡ 4 (mod 5) in symbol form**, over any commutative ring
  K receiving 𝔽_q (ring map ι) and containing a root u of Φ₅ (e.g. 𝔽_(q²), where the primes of ℤ[ζ₅] above q have residue field):
    map5, map5_mul, map5_pow     ι extended coordinatewise is a ring map T[ζ₅] → K[ζ₅]
    iota_castN, iota_castZ       ι ∘ castZ_T = castZ_K, so map5 ∘ ph5_T = ph5_K
    quint_sym_inert              q = 5m + 4:  ev_u ι ψ(q) = (J(u)² J(u²))^(m+1) · (J(u) J(u⁴))^m     in K
  (from Quint12.quint_J_inert, p = J conj(J), Quint13: ev_u ∘ σ₂ = ev_(u²), ev_u ∘ conj = ev_(u⁴)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Quint13

namespace Quint14

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius
open LibRing (castZ castN)

section Gen
variable {R K' : Type} (T : CRing R) (K : CRing K') (ι : R → K')

def map5 (x : R × R × R × R) : K' × K' × K' × K' := (ι x.1, ι x.2.1, ι x.2.2.1, ι x.2.2.2)

theorem map5_mul (hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι (T.neg a) = K.neg (ι a)) (_hone : ι T.one = K.one) (_hzero : ι T.zero = K.zero) : ∀ x y : R × R × R × R, map5 ι ((Cyclo5.cyc5 T).mul x y) = (Cyclo5.cyc5 K).mul (map5 ι x) (map5 ι y)
  | (a0, a1, a2, a3), (b0, b1, b2, b3) => by
    apply Prod.ext
    · show ι (T.add (T.add (T.mul a0 b0) (T.neg (T.add (T.add (T.mul a1 b3) (T.mul a2 b2)) (T.mul a3 b1)))) (T.add (T.mul a2 b3) (T.mul a3 b2))) = (K.add (K.add (K.mul (ι a0) (ι b0)) (K.neg (K.add (K.add (K.mul (ι a1) (ι b3)) (K.mul (ι a2) (ι b2))) (K.mul (ι a3) (ι b1))))) (K.add (K.mul (ι a2) (ι b3)) (K.mul (ι a3) (ι b2))))
      repeat (first | rw [hadd] | rw [hneg] | rw [hmul])
    apply Prod.ext
    · show ι (T.add (T.add (T.add (T.mul a0 b1) (T.mul a1 b0)) (T.neg (T.add (T.add (T.mul a1 b3) (T.mul a2 b2)) (T.mul a3 b1)))) (T.mul a3 b3)) = (K.add (K.add (K.add (K.mul (ι a0) (ι b1)) (K.mul (ι a1) (ι b0))) (K.neg (K.add (K.add (K.mul (ι a1) (ι b3)) (K.mul (ι a2) (ι b2))) (K.mul (ι a3) (ι b1))))) (K.mul (ι a3) (ι b3)))
      repeat (first | rw [hadd] | rw [hneg] | rw [hmul])
    apply Prod.ext
    · show ι (T.add (T.add (T.add (T.mul a0 b2) (T.mul a1 b1)) (T.mul a2 b0)) (T.neg (T.add (T.add (T.mul a1 b3) (T.mul a2 b2)) (T.mul a3 b1)))) = (K.add (K.add (K.add (K.mul (ι a0) (ι b2)) (K.mul (ι a1) (ι b1))) (K.mul (ι a2) (ι b0))) (K.neg (K.add (K.add (K.mul (ι a1) (ι b3)) (K.mul (ι a2) (ι b2))) (K.mul (ι a3) (ι b1)))))
      repeat (first | rw [hadd] | rw [hneg] | rw [hmul])
    · show ι (T.add (T.add (T.add (T.add (T.mul a0 b3) (T.mul a1 b2)) (T.mul a2 b1)) (T.mul a3 b0)) (T.neg (T.add (T.add (T.mul a1 b3) (T.mul a2 b2)) (T.mul a3 b1)))) = (K.add (K.add (K.add (K.add (K.mul (ι a0) (ι b3)) (K.mul (ι a1) (ι b2))) (K.mul (ι a2) (ι b1))) (K.mul (ι a3) (ι b0))) (K.neg (K.add (K.add (K.mul (ι a1) (ι b3)) (K.mul (ι a2) (ι b2))) (K.mul (ι a3) (ι b1)))))
      repeat (first | rw [hadd] | rw [hneg] | rw [hmul])

theorem map5_one (_hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (_hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (_hneg : ∀ a, ι (T.neg a) = K.neg (ι a)) (hone : ι T.one = K.one) (hzero : ι T.zero = K.zero) : map5 ι (Cyclo5.cyc5 T).one = (Cyclo5.cyc5 K).one := by
  show (ι T.one, ι T.zero, ι T.zero, ι T.zero) = (K.one, K.zero, K.zero, K.zero)
  rw [hone, hzero]

theorem map5_pow (hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι (T.neg a) = K.neg (ι a)) (hone : ι T.one = K.one) (hzero : ι T.zero = K.zero) (x : R × R × R × R) : ∀ k, map5 ι (pw (Cyclo5.cyc5 T) x k) = pw (Cyclo5.cyc5 K) (map5 ι x) k
  | 0 => map5_one T K ι hadd hmul hneg hone hzero
  | k + 1 => by
    show map5 ι ((Cyclo5.cyc5 T).mul (pw (Cyclo5.cyc5 T) x k) x) = (Cyclo5.cyc5 K).mul (pw (Cyclo5.cyc5 K) (map5 ι x) k) (map5 ι x)
    rw [map5_mul T K ι hadd hmul hneg hone hzero, map5_pow hadd hmul hneg hone hzero x k]

theorem iota_castN (hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι (T.neg a) = K.neg (ι a)) (hone : ι T.one = K.one) (hzero : ι T.zero = K.zero) : ∀ k, ι (castN T k) = castN K k
  | 0 => hzero
  | k + 1 => by
    show ι (T.add T.one (castN T k)) = K.add K.one (castN K k)
    rw [hadd, hone, iota_castN hadd hmul hneg hone hzero k]

theorem iota_castZ (hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι (T.neg a) = K.neg (ι a)) (hone : ι T.one = K.one) (hzero : ι T.zero = K.zero) : ∀ z : Int, ι (castZ T z) = castZ K z
  | .ofNat k => iota_castN T K ι hadd hmul hneg hone hzero k
  | .negSucc k => by
    show ι (T.neg (castN T (k + 1))) = K.neg (castN K (k + 1))
    rw [hneg, iota_castN T K ι hadd hmul hneg hone hzero]

theorem map5_ph5 (hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι (T.neg a) = K.neg (ι a)) (hone : ι T.one = K.one) (hzero : ι T.zero = K.zero) (z : Int × Int × Int × Int) : map5 ι (Quint6.ph5 T z) = Quint6.ph5 K z := by
  show (ι (castZ T z.1), ι (castZ T z.2.1), ι (castZ T z.2.2.1), ι (castZ T z.2.2.2)) = (castZ K z.1, castZ K z.2.1, castZ K z.2.2.1, castZ K z.2.2.2)
  rw [iota_castZ T K ι hadd hmul hneg hone hzero, iota_castZ T K ι hadd hmul hneg hone hzero, iota_castZ T K ι hadd hmul hneg hone hzero, iota_castZ T K ι hadd hmul hneg hone hzero]
end Gen

variable (n m : Nat)

/-- **q ≡ 4 (mod 5) in symbol form**: ev_u ι ψ(q) = (J(u)² J(u²))^(m+1) (J(u) J(u⁴))^m in any K ⊇ 𝔽_q with Φ₅(u) = 0. -/
theorem quint_sym_inert (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 +   -- (+220 chars; full line in the Zenodo source)
    {K' : Type} (K : CRing K') (ι : Zp (5 * m + 2 + 1) → K') (hadd : ∀ a b, ι ((ring (5 * m + 2 + 1)).add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι ((ring (5 * m + 2 + 1)).mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι ((ring (5 * m + 2 + 1)).neg a) = K.neg (ι a)) (hone : ι (ring (5 * m + 2 + 1)).one = K.one) (hzero : ι (ring (5 * m + 2 + 1)).zero = K.zero) (u : K') (hu : (K.add (K.add (K.add (K.ad  -- (+91 chars; full line in the Zenodo source)
    Quint13.ev K u (map5 ι (Quint6.psi n (ring (5 * m + 2 + 1)) w (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (5 * m + 2 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one)))
      = K.mul (pw K (K.mul (K.mul (Quint13.ev K u (Quint6.ph5 K (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w)))) (Quint13.ev K u (Quint6.ph5 K (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w))))) (Quint13.ev K (K.mul u u) (Quint6.ph5 K (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.ch  -- (+347 chars; full line in the Zenodo source)
  have h := Quint12.quint_J_inert n m hpr w hw1 hw5 hq hQ
  rw [Quint13.Pp_eq n hpr w hw1 hw5 (ring (5 * m + 2 + 1))] at h
  rw [h, map5_mul (ring (5 * m + 2 + 1)) K ι hadd hmul hneg hone hzero, map5_pow (ring (5 * m + 2 + 1)) K ι hadd hmul hneg hone hzero, map5_pow (ring (5 * m + 2 + 1)) K ι hadd hmul hneg hone hzero, map5_ph5 (ring (5 * m + 2 + 1)) K ι hadd hmul hneg hone hzero, map5_ph5 (ring (5 * m + 2 + 1)) K ι hadd hmul hneg hone hzero,
    Quint13.ev_mul K u hu, Quint13.ev_pow K u hu, Quint13.ev_pow K u hu]
  repeat rw [Quint13.evph_mul K u hu]
  rw [Quint13.ev_sig2 K u hu, Quint13.ev_conj K u hu]

end Quint14

#print axioms Quint14.map5_mul
#print axioms Quint14.map5_one
#print axioms Quint14.map5_pow
#print axioms Quint14.iota_castN
#print axioms Quint14.iota_castZ
#print axioms Quint14.map5_ph5
#print axioms Quint14.quint_sym_inert

#print axioms: 7 theorems, no axioms.

78.188 A root of \(\Phi _5\) from \(\sqrt 5\)

If \(2h=1\) and \(s^2=5\) in \(T\), then \(c=h(1+s)\) and \(u=h(-c+\sqrt {c^2-4})\in T[\sqrt {c^2-4}]\) satisfy

\[ c^2-c-1=0,\qquad u^4+u^3+u^2+u+1=0. \]

lang/Quint15.lean

/-
  lang/Quint15 — T49, cycle 763 (ℓ = 5 symbol project, step 15): **a root of Φ₅ over any ring with 1/2 and √5**.
  T a commutative ring, h with 2h = 1, s with s² = 5. c = h(1 + s) satisfies c² − c − 1 = 0 (cc_rel), so
  Φ₅(x) = (x² + cx + 1)(x² + (1 − c)x + 1) + (c² − c − 1)x². In K = T[√D], D = c² − 4 (Quad.quad), u = h(−c + √D) is a root of
  x² + cx + 1 (quad_root), hence Φ₅(u) = 0 (phi5_root), and ι : T → K, a ↦ (a, 0) is a ring map. For q ≡ 4 (mod 5) this supplies the
  K, u of Quint14 (K = 𝔽_q[√D] ≅ 𝔽_(q²)) once 5 is a square mod q:
    quint_sym_inert'   ev_u ι ψ(q) = (J(u)² J(u²))^(m+1) (J(u) J(u⁴))^m   with this explicit K and u.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Quint14

namespace Quint15

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

section Gen
variable {R : Type} (T : CRing R) (h s : R)

def cc : R := (T.mul h (T.add T.one s))
def dd : R := (T.add (T.mul (cc T h s) (cc T h s)) (T.neg (T.add (T.add (T.add T.one T.one) T.one) T.one)))
def uu : R × R := ((T.mul h (T.neg (cc T h s))), h)
def iota (a : R) : R × R := (a, T.zero)

theorem cc_rel (e2 : (T.add (T.add h h) (T.neg T.one)) = T.zero) (e5 : (T.add (T.mul s s) (T.neg (T.add (T.add (T.add (T.add T.one T.one) T.one) T.one) T.one))) = T.zero) : T.add (T.add (T.mul (cc T h s) (cc T h s)) (T.neg (cc T h s))) (T.neg T.one) = T.zero := by
  show (T.add (T.add (T.mul (T.mul h (T.add T.one s)) (T.mul h (T.add T.one s))) (T.neg (T.mul h (T.add T.one s)))) (T.neg T.one)) = T.zero
  calc (T.add (T.add (T.mul (T.mul h (T.add T.one s)) (T.mul h (T.add T.one s))) (T.neg (T.mul h (T.add T.one s)))) (T.neg T.one)) = (T.add (T.mul (T.mul h h) (T.add (T.mul s s) (T.neg (T.add (T.add (T.add (T.add T.one T.one) T.one) T.one) T.one)))) (T.mul (T.add (T.add h h) (T.neg T.one)) (T.add (T.mul h (T.add (T.add (T.add T.one T.one) T.one) s)) T.one))) := by lib_ring
    _ = T.zero := by rw [e2, e5]; lib_ring

theorem quad_root (e2 : (T.add (T.add h h) (T.neg T.one)) = T.zero) : ((Quad.quad T (dd T h s)).add ((Quad.quad T (dd T h s)).add ((Quad.quad T (dd T h s)).mul (uu T h s) (uu T h s)) ((Quad.quad T (dd T h s)).mul (iota T (cc T h s)) (uu T h s))) (Quad.quad T (dd T h s)).one) = (Quad.quad T (dd T h s)).zero := by
  apply Prod.ext
  · show (T.add (T.add (T.add (T.mul (T.mul h (T.neg (cc T h s))) (T.mul h (T.neg (cc T h s)))) (T.mul (T.add (T.mul (cc T h s) (cc T h s)) (T.neg (T.add (T.add (T.add T.one T.one) T.one) T.one))) (T.mul h h))) (T.add (T.mul (cc T h s) (T.mul h (T.neg (cc T h s)))) (T.mul (T.add (T.mul (cc T h s) (cc T h s)) (T.neg (T.add (T.add (T.add T.one T.one) T.one) T.one))) (T.mul T.zero h)))) T.one) = T.ze  -- (+2 chars; full line in the Zenodo source)
    calc (T.add (T.add (T.add (T.mul (T.mul h (T.neg (cc T h s))) (T.mul h (T.neg (cc T h s)))) (T.mul (T.add (T.mul (cc T h s) (cc T h s)) (T.neg (T.add (T.add (T.add T.one T.one) T.one) T.one))) (T.mul h h))) (T.add (T.mul (cc T h s) (T.mul h (T.neg (cc T h s)))) (T.mul (T.add (T.mul (cc T h s) (cc T h s)) (T.neg (T.add (T.add (T.add T.one T.one) T.one) T.one))) (T.mul T.zero h)))) T.one) = T.mu  -- (+140 chars; full line in the Zenodo source)
      _ = T.zero := by rw [e2]; lib_ring
  · show (T.add (T.add (T.add (T.mul (T.mul h (T.neg (cc T h s))) h) (T.mul h (T.mul h (T.neg (cc T h s))))) (T.add (T.mul (cc T h s) h) (T.mul T.zero (T.mul h (T.neg (cc T h s)))))) T.zero) = T.zero
    calc (T.add (T.add (T.add (T.mul (T.mul h (T.neg (cc T h s))) h) (T.mul h (T.mul h (T.neg (cc T h s))))) (T.add (T.mul (cc T h s) h) (T.mul T.zero (T.mul h (T.neg (cc T h s)))))) T.zero) = T.mul (T.add (T.add h h) (T.neg T.one)) (T.neg (T.mul h (cc T h s))) := by lib_ring
      _ = T.zero := by rw [e2]; lib_ring

theorem iota_cc (e2 : (T.add (T.add h h) (T.neg T.one)) = T.zero) (e5 : (T.add (T.mul s s) (T.neg (T.add (T.add (T.add (T.add T.one T.one) T.one) T.one) T.one))) = T.zero) : ((Quad.quad T (dd T h s)).add ((Quad.quad T (dd T h s)).add ((Quad.quad T (dd T h s)).mul (iota T (cc T h s)) (iota T (cc T h s))) ((Quad.quad T (dd T h s)).neg (iota T (cc T h s)))) ((Quad.quad T (dd T h s)).neg (Quad.quad T   -- (+55 chars; full line in the Zenodo source)
  apply Prod.ext
  · show (T.add (T.add (T.add (T.mul (cc T h s) (cc T h s)) (T.mul (T.add (T.mul (cc T h s) (cc T h s)) (T.neg (T.add (T.add (T.add T.one T.one) T.one) T.one))) (T.mul T.zero T.zero))) (T.neg (cc T h s))) (T.neg T.one)) = T.zero
    rw [show (T.add (T.add (T.add (T.mul (cc T h s) (cc T h s)) (T.mul (T.add (T.mul (cc T h s) (cc T h s)) (T.neg (T.add (T.add (T.add T.one T.one) T.one) T.one))) (T.mul T.zero T.zero))) (T.neg (cc T h s))) (T.neg T.one)) = T.add (T.add (T.mul (cc T h s) (cc T h s)) (T.neg (cc T h s))) (T.neg T.one) by lib_ring]
    exact cc_rel T h s e2 e5
  · show (T.add (T.add (T.add (T.mul (cc T h s) T.zero) (T.mul T.zero (cc T h s))) (T.neg T.zero)) (T.neg T.zero)) = T.zero
    lib_ring

/-- **Φ₅(u) = 0** in T[√D]. -/
theorem phi5_root (e2 : (T.add (T.add h h) (T.neg T.one)) = T.zero) (e5 : (T.add (T.mul s s) (T.neg (T.add (T.add (T.add (T.add T.one T.one) T.one) T.one) T.one))) = T.zero) : ((Quad.quad T (dd T h s)).add ((Quad.quad T (dd T h s)).add ((Quad.quad T (dd T h s)).add ((Quad.quad T (dd T h s)).add (Quad.quad T (dd T h s)).one (uu T h s)) ((Quad.quad T (dd T h s)).mul (uu T h s) (uu T h s))) ((Quad.qu  -- (+262 chars; full line in the Zenodo source)
  calc ((Quad.quad T (dd T h s)).add ((Quad.quad T (dd T h s)).add ((Quad.quad T (dd T h s)).add ((Quad.quad T (dd T h s)).add (Quad.quad T (dd T h s)).one (uu T h s)) ((Quad.quad T (dd T h s)).mul (uu T h s) (uu T h s))) ((Quad.quad T (dd T h s)).mul ((Quad.quad T (dd T h s)).mul (uu T h s) (uu T h s)) (uu T h s))) ((Quad.quad T (dd T h s)).mul ((Quad.quad T (dd T h s)).mul ((Quad.quad T (dd T h   -- (+963 chars; full line in the Zenodo source)
    _ = (Quad.quad T (dd T h s)).zero := by rw [quad_root T h s e2, iota_cc T h s e2 e5]; lib_ring

theorem iota_add (a b : R) : iota T (T.add a b) = (Quad.quad T (dd T h s)).add (iota T a) (iota T b) := by
  apply Prod.ext
  · rfl
  · show T.zero = T.add T.zero T.zero; lib_ring
theorem iota_mul (a b : R) : iota T (T.mul a b) = (Quad.quad T (dd T h s)).mul (iota T a) (iota T b) := by
  apply Prod.ext
  · show T.mul a b = T.add (T.mul a b) (T.mul (dd T h s) (T.mul T.zero T.zero)); lib_ring
  · show T.zero = T.add (T.mul a T.zero) (T.mul T.zero b); lib_ring
theorem iota_neg (a : R) : iota T (T.neg a) = (Quad.quad T (dd T h s)).neg (iota T a) := by
  apply Prod.ext
  · rfl
  · show T.zero = T.neg T.zero; lib_ring
theorem iota_one : iota T T.one = (Quad.quad T (dd T h s)).one := rfl
theorem iota_zero : iota T T.zero = (Quad.quad T (dd T h s)).zero := rfl
end Gen

variable (n m : Nat)

/-- **q ≡ 4 (mod 5) in symbol form over 𝔽_q[√D]**, from 1/2 and √5 in 𝔽_q. -/
theorem quint_sym_inert' (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 +  -- (+221 chars; full line in the Zenodo source)
    (h s : Zp (5 * m + 2 + 1)) (e2 : ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add h h) ((ring (5 * m + 2 + 1)).neg (ring (5 * m + 2 + 1)).one)) = (ring (5 * m + 2 + 1)).zero) (e5 : ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).mul s s) ((ring (5 * m + 2 + 1)).neg ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add (ring (  -- (+166 chars; full line in the Zenodo source)
    Quint13.ev (Quad.quad (ring (5 * m + 2 + 1)) (dd (ring (5 * m + 2 + 1)) h s)) (uu (ring (5 * m + 2 + 1)) h s) (Quint14.map5 (iota (ring (5 * m + 2 + 1))) (Quint6.psi n (ring (5 * m + 2 + 1)) w (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (5 * m + 2 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one)))
      = (Quad.quad (ring (5 * m + 2 + 1)) (dd (ring (5 * m + 2 + 1)) h s)).mul (pw (Quad.quad (ring (5 * m + 2 + 1)) (dd (ring (5 * m + 2 + 1)) h s)) ((Quad.quad (ring (5 * m + 2 + 1)) (dd (ring (5 * m + 2 + 1)) h s)).mul ((Quad.quad (ring (5 * m + 2 + 1)) (dd (ring (5 * m + 2 + 1)) h s)).mul (Quint13.ev (Quad.quad (ring (5 * m + 2 + 1)) (dd (ring (5 * m + 2 + 1)) h s)) (uu (ring (5 * m + 2 + 1))   -- (+1914 chars; full line in the Zenodo source)
  Quint14.quint_sym_inert n m hpr w hw1 hw5 hq hQ (Quad.quad (ring (5 * m + 2 + 1)) (dd (ring (5 * m + 2 + 1)) h s)) (iota (ring (5 * m + 2 + 1))) (iota_add (ring (5 * m + 2 + 1)) h s) (iota_mul (ring (5 * m + 2 + 1)) h s) (iota_neg (ring (5 * m + 2 + 1)) h s)
    (iota_one (ring (5 * m + 2 + 1)) h s) (iota_zero (ring (5 * m + 2 + 1)) h s) (uu (ring (5 * m + 2 + 1)) h s) (phi5_root (ring (5 * m + 2 + 1)) h s e2 e5)

end Quint15

#print axioms Quint15.cc_rel
#print axioms Quint15.quad_root
#print axioms Quint15.iota_cc
#print axioms Quint15.phi5_root
#print axioms Quint15.iota_add
#print axioms Quint15.iota_mul
#print axioms Quint15.iota_neg
#print axioms Quint15.iota_one
#print axioms Quint15.iota_zero
#print axioms Quint15.quint_sym_inert'

#print axioms: 10 theorems, no axioms.

78.189 \(\sqrt 5\) modulo primes \(q\equiv 4\pmod 5\)

For every prime \(q=5m+4\), \(5\) is a square in \(\mathbb F_q\), so the symbol form of Quint14 holds over \(\mathbb F_q[\sqrt D]\) without hypotheses.

\[ q\equiv 4\pmod 5\ \Rightarrow \ \exists s\in \mathbb F_q,\ s^2=5. \]

lang/Quint16.lean

/-
  lang/Quint16 — T49, cycle 764 (ℓ = 5 symbol project, step 16): **√5 ∈ 𝔽_q for q ≡ 4 (mod 5), and the q ≡ 4 symbol form without
  hypotheses**.
    g5, g5_sq        g = ζ − ζ² − ζ³ + ζ⁴ = (−1, 0, −2, −2) has g² = 5 in K[ζ₅] (any K)
    g5_frob          q = 5m + 4:  g^q = g      (Quint3.frob5_4: ζ ↦ ζ⁴ fixes g)
    five_inv         5 · (m + 1) = 1 in 𝔽_q   (5(m + 1) = q + 1)
    q_odd            q = 5m + 4 prime ⇒ q − 1 = 2k
    five_pow         5^((q−1)/2) = 1           (g^(q−1) = (g²)^k, cancel g² = 5)
    sqrt5            ∃ s, s² = 5 in 𝔽_q        (Euler's criterion, NthPow.nth_crit)
    quint_sym_inert_full   for every prime q = 5m + 4 ≠ p there are h, s ∈ 𝔽_q (2h = 1, s² = 5) such that, over K = 𝔽_q[√D] with the
                     root u of Φ₅ of Quint15, ev_u ι ψ(q) = (J(u)² J(u²))^(m+1) (J(u) J(u⁴))^m
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Quint15
import lang.NthPow
import lang.CubeHasse

namespace Quint16

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

section Gen
variable {R : Type} (K : CRing R)

def g5 : R × R × R × R := ((K.neg K.one), K.zero, (K.neg (K.add K.one K.one)), (K.neg (K.add K.one K.one)))
def five : R := (K.add (K.add (K.add (K.add K.one K.one) K.one) K.one) K.one)

theorem g5_sq : (Cyclo5.cyc5 K).mul (g5 K) (g5 K) = Quint2.sc5 K (five K) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul (K.neg K.one) (K.neg K.one)) (K.neg (K.add (K.add (K.mul K.zero (K.neg (K.add K.one K.one))) (K.mul (K.neg (K.add K.one K.one)) (K.neg (K.add K.one K.one)))) (K.mul (K.neg (K.add K.one K.one)) K.zero)))) (K.add (K.mul (K.neg (K.add K.one K.one)) (K.neg (K.add K.one K.one))) (K.mul (K.neg (K.add K.one K.one)) (K.neg (K.add K.one K.one))))) = (K.add (K.add (K.add (K.add  -- (+34 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul (K.neg K.one) K.zero) (K.mul K.zero (K.neg K.one))) (K.neg (K.add (K.add (K.mul K.zero (K.neg (K.add K.one K.one))) (K.mul (K.neg (K.add K.one K.one)) (K.neg (K.add K.one K.one)))) (K.mul (K.neg (K.add K.one K.one)) K.zero)))) (K.mul (K.neg (K.add K.one K.one)) (K.neg (K.add K.one K.one)))) = K.zero
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul (K.neg K.one) (K.neg (K.add K.one K.one))) (K.mul K.zero K.zero)) (K.mul (K.neg (K.add K.one K.one)) (K.neg K.one))) (K.neg (K.add (K.add (K.mul K.zero (K.neg (K.add K.one K.one))) (K.mul (K.neg (K.add K.one K.one)) (K.neg (K.add K.one K.one)))) (K.mul (K.neg (K.add K.one K.one)) K.zero)))) = K.zero
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.mul (K.neg K.one) (K.neg (K.add K.one K.one))) (K.mul K.zero (K.neg (K.add K.one K.one)))) (K.mul (K.neg (K.add K.one K.one)) K.zero)) (K.mul (K.neg (K.add K.one K.one)) (K.neg K.one))) (K.neg (K.add (K.add (K.mul K.zero (K.neg (K.add K.one K.one))) (K.mul (K.neg (K.add K.one K.one)) (K.neg (K.add K.one K.one)))) (K.mul (K.neg (K.add K.one K.one)) K.zero))))  -- (+9 chars; full line in the Zenodo source)
    lib_ring
end Gen

variable (m : Nat)

theorem g5_frob (hq : ∀ d, Dv d (5 * m + 2 + 1 + 1) → d = 1 ∨ d = 5 * m + 2 + 1 + 1) : pw (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) (g5 (ring (5 * m + 2 + 1))) (5 * m + 2 + 1 + 1) = g5 (ring (5 * m + 2 + 1)) := by
  unfold g5
  rw [Quint3.frob5_4 (5 * m + 2) hq m (by lib_nat)]
  apply Prod.ext
  · show ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).neg (ring (5 * m + 2 + 1)).one) ((ring (5 * m + 2 + 1)).neg (ring (5 * m + 2 + 1)).zero)) = ((ring (5 * m + 2 + 1)).neg (ring (5 * m + 2 + 1)).one)
    lib_ring
  apply Prod.ext
  · show ((ring (5 * m + 2 + 1)).neg (ring (5 * m + 2 + 1)).zero) = (ring (5 * m + 2 + 1)).zero
    lib_ring
  apply Prod.ext
  · show ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).neg (ring (5 * m + 2 + 1)).zero) ((ring (5 * m + 2 + 1)).neg ((ring (5 * m + 2 + 1)).add (ring (5 * m + 2 + 1)).one (ring (5 * m + 2 + 1)).one))) = ((ring (5 * m + 2 + 1)).neg ((ring (5 * m + 2 + 1)).add (ring (5 * m + 2 + 1)).one (ring (5 * m + 2 + 1)).one))
    lib_ring
  · show ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).neg (ring (5 * m + 2 + 1)).zero) ((ring (5 * m + 2 + 1)).neg ((ring (5 * m + 2 + 1)).add (ring (5 * m + 2 + 1)).one (ring (5 * m + 2 + 1)).one))) = ((ring (5 * m + 2 + 1)).neg ((ring (5 * m + 2 + 1)).add (ring (5 * m + 2 + 1)).one (ring (5 * m + 2 + 1)).one))
    lib_ring

theorem five_inv : (ring (5 * m + 2 + 1)).mul (five (ring (5 * m + 2 + 1))) (nsm (ring (5 * m + 2 + 1)) (m + 1) (ring (5 * m + 2 + 1)).one) = (ring (5 * m + 2 + 1)).one := by
  rw [show five (ring (5 * m + 2 + 1)) = nsm (ring (5 * m + 2 + 1)) 5 (ring (5 * m + 2 + 1)).one by
        show _ = (ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add (ring (5 * m + 2 + 1)).zero (ring (5 * m + 2 + 1)).one) (ring (5 * m + 2 + 1)).one) (ring (5 * m + 2 + 1)).one) (ring (5 * m + 2 + 1)).one) (ring (5 * m + 2 + 1)).one; unfold five; lib_ring,
    ← CubeHasse.nsm_prod, show 5 * (m + 1) = (5 * m + 2 + 1 + 1) + 1 by lib_nat, nsm_add, char_p, nsm_one]
  lib_ring

theorem par : ∀ a : Nat, ∃ t, a = 2 * t ∨ a = 2 * t + 1
  | 0 => ⟨0, Or.inl rfl⟩
  | a + 1 => by
    match par a with
    | ⟨t, Or.inl h⟩ => exact ⟨t, Or.inr (by rw [h])⟩
    | ⟨t, Or.inr h⟩ => exact ⟨t + 1, Or.inl (by rw [h]; lib_nat)⟩

theorem q_odd (hq : ∀ d, Dv d (5 * m + 2 + 1 + 1) → d = 1 ∨ d = 5 * m + 2 + 1 + 1) : ∃ k, 5 * m + 2 + 1 = 2 * k := by
  match par m with
  | ⟨t, Or.inl h⟩ =>
    exfalso
    cases hq 2 ⟨5 * t + 2, by rw [h]; lib_nat⟩ with
    | inl e => exact Nat.noConfusion (Nat.succ.inj e)
    | inr e =>
      rw [h] at e
      exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj e)).symm
  | ⟨t, Or.inr h⟩ => exact ⟨5 * t + 4, by rw [h]; lib_nat⟩

theorem five_pow (hq : ∀ d, Dv d (5 * m + 2 + 1 + 1) → d = 1 ∨ d = 5 * m + 2 + 1 + 1) (k : Nat) (hk : 5 * m + 2 + 1 = 2 * k) : pw (ring (5 * m + 2 + 1)) (five (ring (5 * m + 2 + 1))) k = (ring (5 * m + 2 + 1)).one := by
  have hG := g5_frob m hq
  rw [show 5 * m + 2 + 1 + 1 = 2 * k + 1 by rw [hk]] at hG
  have h1 : (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).mul (pw (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) (g5 (ring (5 * m + 2 + 1))) (2 * k)) (g5 (ring (5 * m + 2 + 1))) = g5 (ring (5 * m + 2 + 1)) := hG
  rw [← TwoPower.pwpow, show pw (Cyclo5.cyc5 (ring (5 * m + 2 + 1))) (g5 (ring (5 * m + 2 + 1))) 2 = (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).mul (g5 (ring (5 * m + 2 + 1))) (g5 (ring (5 * m + 2 + 1))) from by
      show (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).mul ((Cyclo5.cyc5 (ring (5 * m + 2 + 1))).mul (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).one (g5 (ring (5 * m + 2 + 1)))) (g5 (ring (5 * m + 2 + 1))) = _; rw [(Cyclo5.cyc5 (ring (5 * m + 2 + 1))).one_mul], g5_sq, Quint2.sc5_pow] at h1
  have h3 : (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).mul (Quint2.sc5 (ring (5 * m + 2 + 1)) (pw (ring (5 * m + 2 + 1)) (five (ring (5 * m + 2 + 1))) k)) ((Cyclo5.cyc5 (ring (5 * m + 2 + 1))).mul (g5 (ring (5 * m + 2 + 1))) (g5 (ring (5 * m + 2 + 1)))) = (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).mul (g5 (ring (5 * m + 2 + 1))) (g5 (ring (5 * m + 2 + 1))) := by
    rw [← (Cyclo5.cyc5 (ring (5 * m + 2 + 1))).mul_assoc, h1]
  rw [g5_sq, Quint2.sc5_mul] at h3
  have h4 : (ring (5 * m + 2 + 1)).mul (pw (ring (5 * m + 2 + 1)) (five (ring (5 * m + 2 + 1))) k) (five (ring (5 * m + 2 + 1))) = five (ring (5 * m + 2 + 1)) := congrArg (fun x => x.1) h3
  calc pw (ring (5 * m + 2 + 1)) (five (ring (5 * m + 2 + 1))) k = (ring (5 * m + 2 + 1)).mul ((ring (5 * m + 2 + 1)).mul (pw (ring (5 * m + 2 + 1)) (five (ring (5 * m + 2 + 1))) k) (five (ring (5 * m + 2 + 1)))) (nsm (ring (5 * m + 2 + 1)) (m + 1) (ring (5 * m + 2 + 1)).one) := by
        rw [(ring (5 * m + 2 + 1)).mul_assoc, five_inv]; lib_ring
    _ = (ring (5 * m + 2 + 1)).one := by rw [h4, five_inv]

theorem five_ne : five (ring (5 * m + 2 + 1)) ≠ (ring (5 * m + 2 + 1)).zero := fun e => PowerSum.one_ne_zero' (5 * m + 2) (by
  calc (ring (5 * m + 2 + 1)).one = (ring (5 * m + 2 + 1)).mul (five (ring (5 * m + 2 + 1))) (nsm (ring (5 * m + 2 + 1)) (m + 1) (ring (5 * m + 2 + 1)).one) := (five_inv m).symm
    _ = (ring (5 * m + 2 + 1)).mul (ring (5 * m + 2 + 1)).zero (nsm (ring (5 * m + 2 + 1)) (m + 1) (ring (5 * m + 2 + 1)).one) := by rw [e]
    _ = (ring (5 * m + 2 + 1)).zero := by lib_ring)

/-- **5 is a square mod every prime q ≡ 4 (mod 5).** -/
theorem sqrt5 (hq : ∀ d, Dv d (5 * m + 2 + 1 + 1) → d = 1 ∨ d = 5 * m + 2 + 1 + 1) : ∃ s : Zp (5 * m + 2 + 1), (ring (5 * m + 2 + 1)).mul s s = five (ring (5 * m + 2 + 1)) := by
  match q_odd m hq with
  | ⟨k, hk⟩ =>
    match (NthPow.nth_crit (5 * m + 2) hq 2 k hk (five (ring (5 * m + 2 + 1))) (five_ne m)).mpr (five_pow m hq k hk) with
    | ⟨s, hs⟩ => exact ⟨s, by rw [← hs]; show _ = (ring (5 * m + 2 + 1)).mul ((ring (5 * m + 2 + 1)).mul (ring (5 * m + 2 + 1)).one s) s; rw [(ring (5 * m + 2 + 1)).one_mul]⟩

variable (n : Nat)

/-- **q ≡ 4 (mod 5) in symbol form, unconditionally**: over 𝔽_q[√D] with its root u of Φ₅ (Quint15). -/
theorem quint_sym_inert_full (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) +  -- (+227 chars; full line in the Zenodo source)
    ∃ h s : Zp (5 * m + 2 + 1), ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add h h) ((ring (5 * m + 2 + 1)).neg (ring (5 * m + 2 + 1)).one)) = (ring (5 * m + 2 + 1)).zero ∧ ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).mul s s) ((ring (5 * m + 2 + 1)).neg ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add (ring (5 * m + 2   -- (+155 chars; full line in the Zenodo source)
      Quint13.ev (Quad.quad (ring (5 * m + 2 + 1)) (Quint15.dd (ring (5 * m + 2 + 1)) h s)) (Quint15.uu (ring (5 * m + 2 + 1)) h s) (Quint14.map5 (Quint15.iota (ring (5 * m + 2 + 1))) (Quint6.psi n (ring (5 * m + 2 + 1)) w (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (5 * m + 2 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one)))
        = (Quad.quad (ring (5 * m + 2 + 1)) (Quint15.dd (ring (5 * m + 2 + 1)) h s)).mul (pw (Quad.quad (ring (5 * m + 2 + 1)) (Quint15.dd (ring (5 * m + 2 + 1)) h s)) ((Quad.quad (ring (5 * m + 2 + 1)) (Quint15.dd (ring (5 * m + 2 + 1)) h s)).mul ((Quad.quad (ring (5 * m + 2 + 1)) (Quint15.dd (ring (5 * m + 2 + 1)) h s)).mul (Quint13.ev (Quad.quad (ring (5 * m + 2 + 1)) (Quint15.dd (ring (5 * m +  -- (+2151 chars; full line in the Zenodo source)
  match q_odd m hq, sqrt5 m hq with
  | ⟨k, hk⟩, ⟨s, hs⟩ =>
    have e2 : ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add (nsm (ring (5 * m + 2 + 1)) (k + 1) (ring (5 * m + 2 + 1)).one) (nsm (ring (5 * m + 2 + 1)) (k + 1) (ring (5 * m + 2 + 1)).one)) ((ring (5 * m + 2 + 1)).neg (ring (5 * m + 2 + 1)).one)) = (ring (5 * m + 2 + 1)).zero := by
      rw [← nsm_add, show k + 1 + (k + 1) = (5 * m + 2 + 1 + 1) + 1 by rw [show 5 * m + 2 + 1 + 1 = 2 * k + 1 by rw [hk]]; lib_nat, nsm_add, char_p, nsm_one]
      lib_ring
    have e5 : ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).mul s s) ((ring (5 * m + 2 + 1)).neg ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add ((ring (5 * m + 2 + 1)).add (ring (5 * m + 2 + 1)).one (ring (5 * m + 2 + 1)).one) (ring (5 * m + 2 + 1)).one) (ring (5 * m + 2 + 1)).one) (ring (5 * m + 2 + 1)).one))) = (ring (5 * m + 2 + 1)).zero := by rw [hs];  -- (+22 chars; full line in the Zenodo source)
    exact ⟨nsm (ring (5 * m + 2 + 1)) (k + 1) (ring (5 * m + 2 + 1)).one, s, e2, e5, Quint15.quint_sym_inert' n m hpr w hw1 hw5 hq hQ _ s e2 e5⟩

end Quint16

#print axioms Quint16.g5_sq
#print axioms Quint16.g5_frob
#print axioms Quint16.five_inv
#print axioms Quint16.par
#print axioms Quint16.q_odd
#print axioms Quint16.five_pow
#print axioms Quint16.five_ne
#print axioms Quint16.sqrt5
#print axioms Quint16.quint_sym_inert_full

#print axioms: 9 theorems, no axioms.

78.190 Quintic symbol form at every odd prime

Over \(K=\mathbb F_q[\sqrt 5][\sqrt D]\) with \(\Phi _5(u)=0\), for every odd prime \(q\neq 5,p\) with \(q^4=5M+1\):

\[ \mathrm {ev}_u\,\psi (q^{-4})=\bigl (J(u)^3J(u^2)J(u^4)\bigr )^{M}. \]

lang/Quint17.lean

/-
  lang/Quint17 — T49, cycle 765 (ℓ = 5 symbol project, step 17): **the quintic symbol form at every odd prime q ≠ 5, p**.
  T' = 𝔽_q[√5] (Quad.quad, s = √5 = (0, 1)) always contains 1/2 = ((q + 1)/2, 0) and √5, so Quint15 gives K = T'[√D] with a root u of
  Φ₅ and the ring map ι : 𝔽_q → T' → K (iotaD_*: a ↦ (a, 0) is a ring map into any T[√D]). With Quint12.quint_J_all (q⁴ = 5M + 1):
    quint_sym_all     ev_u ι ψ(q⁻⁴) = (J(u)³ J(u²) J(u⁴))^M       in K,  J = J(χ, χ),
  covering q ≡ 2, 3 (mod 5) (and again q ≡ 1, 4).
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Quint16

namespace Quint17

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

section Gen
variable {R : Type} (T : CRing R) (D : R)

theorem iotaD_add (a b : R) : Quint15.iota T (T.add a b) = (Quad.quad T D).add (Quint15.iota T a) (Quint15.iota T b) := by
  apply Prod.ext
  · rfl
  · show T.zero = T.add T.zero T.zero; lib_ring
theorem iotaD_mul (a b : R) : Quint15.iota T (T.mul a b) = (Quad.quad T D).mul (Quint15.iota T a) (Quint15.iota T b) := by
  apply Prod.ext
  · show T.mul a b = T.add (T.mul a b) (T.mul D (T.mul T.zero T.zero)); lib_ring
  · show T.zero = T.add (T.mul a T.zero) (T.mul T.zero b); lib_ring
theorem iotaD_neg (a : R) : Quint15.iota T (T.neg a) = (Quad.quad T D).neg (Quint15.iota T a) := by
  apply Prod.ext
  · rfl
  · show T.zero = T.neg T.zero; lib_ring
end Gen

variable (n k : Nat)

theorem e2' (kk : Nat) (hkk : k + 1 + 1 = 2 * kk + 1) :
    (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)).add ((Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)   -- (+806 chars; full line in the Zenodo source)
  apply Prod.ext
  · show (ring (k + 1)).add ((ring (k + 1)).add (nsm (ring (k + 1)) (kk + 1) (ring (k + 1)).one) (nsm (ring (k + 1)) (kk + 1) (ring (k + 1)).one)) ((ring (k + 1)).neg (ring (k + 1)).one) = (ring (k + 1)).zero
    rw [← nsm_add, show kk + 1 + (kk + 1) = (k + 1 + 1) + 1 by rw [hkk]; lib_nat, nsm_add, char_p, nsm_one]
    lib_ring
  · show (ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).zero (ring (k + 1)).zero) ((ring (k + 1)).neg (ring (k + 1)).zero) = (ring (k + 1)).zero
    lib_ring

theorem e5' : (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)).add ((Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k +  -- (+2448 chars; full line in the Zenodo source)
  apply Prod.ext
  · show (ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).mul (ring (k + 1)).zero (ring (k + 1)).zero) ((ring (k + 1)).mul ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) ((ring (k + 1)).mul (ring (k + 1)).one (ring (k + 1)).one)))
        ((ring (k + 1)).neg ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) = (ring (k + 1)).zero
    lib_ring
  · show (ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).mul (ring (k + 1)).zero (ring (k + 1)).one) ((ring (k + 1)).mul (ring (k + 1)).one (ring (k + 1)).zero))
        ((ring (k + 1)).neg ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).zero (ring (k + 1)).zero) (ring (k + 1)).zero) (ring (k + 1)).zero) (ring (k + 1)).zero)) = (ring (k + 1)).zero
    lib_ring

/-- **The quintic symbol form at every odd prime q ≠ 5, p**: ev_u ι ψ(q⁻⁴) = (J(u)³ J(u²) J(u⁴))^M, q⁴ = 5M + 1. -/
theorem quint_sym_all (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)  -- (+210 chars; full line in the Zenodo source)
    (hM : (k + 1 + 1) ^ 4 = 5 * M + 1) (kk : Nat) (hkk : k + 1 + 1 = 2 * kk + 1) :
    Quint13.ev (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k +   -- (+957 chars; full line in the Zenodo source)
      = pw (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).  -- (+13686 chars; full line in the Zenodo source)
  have hu := Quint15.phi5_root (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (nsm (ring (k + 1)) (kk + 1) (ring (k + 1)).one, (ring (k + 1)).zero) ((ring (k + 1)).zero, (ring (k + 1)).one) (e2' k kk hkk) (e5' k)
  have h := Quint12.quint_J_all n k hpr w hw1 hw5 hq hQ M hM
  rw [Quint13.Pp_eq n hpr w hw1 hw5 (ring (k + 1)), ← Quint6.ph5_mul] at h
  rw [h, Quint14.map5_pow (ring (k + 1)) (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring  -- (+8545 chars; full line in the Zenodo source)
  repeat rw [Quint13.evph_mul (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).o  -- (+513 chars; full line in the Zenodo source)
  rw [Quint13.ev_sig2 (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (rin  -- (+1405 chars; full line in the Zenodo source)
  apply congrArg (fun x => pw (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).o  -- (+183 chars; full line in the Zenodo source)
  lib_ring

end Quint17

#print axioms Quint17.iotaD_add
#print axioms Quint17.iotaD_mul
#print axioms Quint17.iotaD_neg
#print axioms Quint17.e2'
#print axioms Quint17.e5'
#print axioms Quint17.quint_sym_all

#print axioms: 6 theorems, no axioms.

78.191 Primary normalisation of the quintic Jacobi sum

For \(J=J(\chi ,\chi )\in \mathbb Z[\zeta _5]\) the element in the quintic residue criteria is congruent to \(-1\) modulo \(5\).

\[ J^2\,\sigma _2(J)\equiv -1\pmod 5. \]

lang/Quint18.lean

/-
  lang/Quint18 — T49, cycle 766 (ℓ = 5 symbol project, step 18): **J² σ₂(J) ≡ −1 (mod 5)** for the quintic Jacobi sum J = J(χ, χ)
  (the primary normalisation behind Eisenstein's law; ℓ = 3 analogue: JacobiPrimary).
    gauss_frob'    in characteristic q:  G_ψ^q = ψ(q⁻¹)^q · G_(ψ^q)        (GaussFrobN without ψ^q = ψ)
    Q5_ne          5 ≠ 0 in 𝔽_p (p = 10n + 11)
    quint_primary  in 𝔽₅[ζ₅] (ψ = ph5 ∘ χ):  G⁵ = A·X (CharSumGen.gpow_n) has ζ¹-coefficient −A, while G⁵ = ψ(5⁻¹)⁵ G_(ψ⁵) has
                   ζ¹-coefficient ψ⁵(1) = 1 (ψ⁵ = 1 off 0, Quint10.psi5); so A = ph5(J² σ₂J) = −1 in 𝔽₅[ζ₅]
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Quint17
import lang.CubicFive

namespace Quint18

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing GroupRingFrob Frobenius

section Gen
variable {S : Type} (E : CRing S) (j : Nat)

/-- **G_ψ^q = ψ(q⁻¹)^q · G_(ψ^q)** in characteristic q (no assumption ψ^q = ψ). -/
theorem gauss_frob' (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (k : Nat) (hq : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1)
    (hE : nsm E (k + 1 + 1) E.one = E.zero) (ψ : Zp (j + 1) → S)
    (hmul : ∀ a b, ψ ((ring (j + 1)).mul a b) = E.mul (ψ a) (ψ b))
    (hQ : nsm (ring (j + 1)) (k + 1 + 1) (ring (j + 1)).one ≠ (ring (j + 1)).zero) :
    pw (gring E (j + 1)) (gauss E j ψ) (k + 1 + 1) =
      gmul E (j + 1) (mono E (j + 1) (ring (j + 1)).zero (pw E (ψ (pw (ring (j + 1)) (nsm (ring (j + 1)) (k + 1 + 1) (ring (j + 1)).one) j)) (k + 1 + 1)))
        (gauss E j (fun a => pw E (ψ a) (k + 1 + 1))) := by
  generalize hQd : nsm (ring (j + 1)) (k + 1 + 1) (ring (j + 1)).one = Q at hQ
  have hinv := inv_exists j hpr Q hQ
  generalize hQi : pw (ring (j + 1)) Q j = Qi at hinv
  have hL : pw (gring E (j + 1)) (gauss E j ψ) (k + 1 + 1) = sumK (gring E (j + 1)) (fun a => mono E (j + 1) ((ring (j + 1)).mul Q a) (pw E (ψ a) (k + 1 + 1))) (L (j + 1)) := by
    unfold gauss
    rw [frob_sum E (j + 1) k hq hE]
    apply sumK_ext; intro a
    rw [mono_pow, nsm_eq_mul, hQd]
  have hc := CMInert.complete_allZ (j + 1) (gring E (j + 1))
  have hre := reindex (gring E (j + 1)) (L (j + 1)) hc (fun b => (ring (j + 1)).mul Qi b) (fun a => (ring (j + 1)).mul Q a)
    (fun b => by show (ring (j + 1)).mul Q ((ring (j + 1)).mul Qi b) = b
                 calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) b := by lib_ring
                   _ = b := by rw [hinv]; lib_ring)
    (fun a => by show (ring (j + 1)).mul Qi ((ring (j + 1)).mul Q a) = a
                 calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) a := by lib_ring
                   _ = a := by rw [hinv]; lib_ring)
    (fun a => mono E (j + 1) ((ring (j + 1)).mul Q a) (pw E (ψ a) (k + 1 + 1)))
  rw [hL, ← hre]
  unfold gauss
  show _ = (gring E (j + 1)).mul (mono E (j + 1) (ring (j + 1)).zero (pw E (ψ Qi) (k + 1 + 1))) (sumK (gring E (j + 1)) (fun a => mono E (j + 1) a (pw E (ψ a) (k + 1 + 1))) (L (j + 1)))
  rw [mul_sumK]
  apply sumK_ext; intro b
  show _ = gmul E (j + 1) (mono E (j + 1) (ring (j + 1)).zero (pw E (ψ Qi) (k + 1 + 1))) (mono E (j + 1) b (pw E (ψ b) (k + 1 + 1)))
  rw [mono_mul, ← pw_mul, ← hmul]
  have e1 : (ring (j + 1)).mul Q ((ring (j + 1)).mul Qi b) = (ring (j + 1)).add (ring (j + 1)).zero b := by
    calc _ = (ring (j + 1)).mul ((ring (j + 1)).mul Qi Q) b := by lib_ring
      _ = _ := by rw [hinv]; lib_ring
  rw [e1]
end Gen

variable (n : Nat)

theorem Q5_ne : (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (3 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) ≠ (ring (2 * (5 * n + 4) + 1 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ (3 + 1 + 1) (by rw [show 2 * (5 * n + 4) + 1 + 1 + 1 = (3 + 1 + 1) + (10 * n + 5) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right _ _))] at h
  exact Nat.noConfusion h

/-- **J² σ₂(J) ≡ −1 (mod 5)**: the reduction of A = J(χ,χ)² σ₂J(χ,χ) to 𝔽₅[ζ₅] is −1. -/
theorem quint_primary (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)  -- (+457 chars; full line in the Zenodo source)
  have hG := CharSumGen.gpow_n (Cyclo5.cyc5 (ring 4)) (2 * (5 * n + 4)) hpr (Quint6.psi n (ring 4) w) (Quint6.psi_mul n (ring 4) hpr w hw1 hw5) (Quint6.psi_one n (ring 4) hpr w hw1 hw5) (Quint6.psi_zero n (ring 4) w)
    (Quint6.psi_sum n (ring 4) hpr w hw1 hw5) 3 (Quint6.psi_nt n (ring 4) hpr w hw1 hw5) (Quint6.psi_inv n (ring 4) hpr w hw1 hw5)
  have hF := gauss_frob' (Cyclo5.cyc5 (ring 4)) ((2 * (5 * n + 4)) + 1) hpr 3 CubicFive.five_prime (Quint2.charq5 3) (Quint6.psi n (ring 4) w) (Quint6.psi_mul n (ring 4) hpr w hw1 hw5) (Q5_ne n)
  rw [CharSumGen.gauss_Gv, CharSumGen.gauss_Gv] at hF
  have hneg : (Quint6.psi n (ring 4) w) (CharSumGen.iv (2 * (5 * n + 4)) ((ring (2 * (5 * n + 4) + 1 + 1)).neg (ring (2 * (5 * n + 4) + 1 + 1)).one)) = (Cyclo5.cyc5 (ring 4)).one := by rw [JacRefl.iv_neg1 (2 * (5 * n + 4)) hpr, Quint10.psi_neg1 n (ring 4) hpr w hw1 hw5]
  have e1 : gt (Cyclo5.cyc5 (ring 4)) (2 * (5 * n + 4) + 1 + 1) (pw (gring (Cyclo5.cyc5 (ring 4)) (2 * (5 * n + 4) + 1 + 1)) (CharSumGen.Gv (2 * (5 * n + 4)) (Quint6.psi n (ring 4) w)) (3 + 2)) (ring (2 * (5 * n + 4) + 1 + 1)).one
      = (Cyclo5.cyc5 (ring 4)).mul ((Cyclo5.cyc5 (ring 4)).mul (CharSumGen.jpr (Cyclo5.cyc5 (ring 4)) (2 * (5 * n + 4)) (Quint6.psi n (ring 4) w) 3) (Cyclo5.cyc5 (ring 4)).one) ((Cyclo5.cyc5 (ring 4)).neg (Cyclo5.cyc5 (ring 4)).one) := by
    rw [hG, hneg, JacobiNorm.mono0_mulg, CharSumGen.gt_X]
    unfold delta; rw [ite_neg' _ _ (PowerSum.one_ne_zero' _)]; lib_ring
  have e2 : gt (Cyclo5.cyc5 (ring 4)) (2 * (5 * n + 4) + 1 + 1) (pw (gring (Cyclo5.cyc5 (ring 4)) (2 * (5 * n + 4) + 1 + 1)) (CharSumGen.Gv (2 * (5 * n + 4)) (Quint6.psi n (ring 4) w)) (3 + 2)) (ring (2 * (5 * n + 4) + 1 + 1)).one
      = (Cyclo5.cyc5 (ring 4)).mul (pw (Cyclo5.cyc5 (ring 4)) ((Quint6.psi n (ring 4) w) (pw (ring (2 * (5 * n + 4) + 1 + 1)) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (3 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) (2 * (5 * n + 4) + 1))) (3 + 1 + 1)) (pw (Cyclo5.cyc5 (ring 4)) ((Quint6.psi n (ring 4) w) (ring (2 * (5 * n + 4) + 1 + 1)).one) (3 + 1 + 1)) := by
    rw [show pw (gring (Cyclo5.cyc5 (ring 4)) (2 * (5 * n + 4) + 1 + 1)) (CharSumGen.Gv (2 * (5 * n + 4)) (Quint6.psi n (ring 4) w)) (3 + 2) = pw (gring (Cyclo5.cyc5 (ring 4)) (2 * (5 * n + 4) + 1 + 1)) (CharSumGen.Gv (2 * (5 * n + 4)) (Quint6.psi n (ring 4) w)) (3 + 1 + 1) from rfl, hF, JacobiNorm.mono0_mulg]
    unfold CharSumGen.Gv; rw [gt_tb]
  have hinv := CharSumGen.iv_mul (2 * (5 * n + 4)) hpr (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (3 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) (Q5_ne n)
  have hQi0 : (pw (ring (2 * (5 * n + 4) + 1 + 1)) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (3 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) (2 * (5 * n + 4) + 1)) ≠ (ring (2 * (5 * n + 4) + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' ((2 * (5 * n + 4)) + 1) (by
    calc (ring (2 * (5 * n + 4) + 1 + 1)).one = (ring (2 * (5 * n + 4) + 1 + 1)).mul (pw (ring (2 * (5 * n + 4) + 1 + 1)) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (3 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) (2 * (5 * n + 4) + 1)) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (3 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) := hinv.symm
      _ = (ring (2 * (5 * n + 4) + 1 + 1)).mul (ring (2 * (5 * n + 4) + 1 + 1)).zero (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (3 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) := by rw [e]
      _ = (ring (2 * (5 * n + 4) + 1 + 1)).zero := by lib_ring)
  have h5 : pw (Cyclo5.cyc5 (ring 4)) ((Quint6.psi n (ring 4) w) (pw (ring (2 * (5 * n + 4) + 1 + 1)) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (3 + 1 + 1) (ring (2 * (5 * n + 4) + 1 + 1)).one) (2 * (5 * n + 4) + 1))) (3 + 1 + 1) = (Cyclo5.cyc5 (ring 4)).one := Quint10.psi5 n (ring 4) hpr w hw1 hw5 (pw (ring (2 * (5 * n + 4) + 1 + 1)) (nsm (ring (2 * (5 * n + 4) + 1 + 1)) (3 + 1 + 1) (ring (2 * (5 * n  -- (+48 chars; full line in the Zenodo source)
  have h1 : pw (Cyclo5.cyc5 (ring 4)) ((Quint6.psi n (ring 4) w) (ring (2 * (5 * n + 4) + 1 + 1)).one) (3 + 1 + 1) = (Cyclo5.cyc5 (ring 4)).one := by rw [Quint6.psi_one n (ring 4) hpr w hw1 hw5]; exact CubicChar.pw_one'' (Cyclo5.cyc5 (ring 4)) _
  have hA := e1.symm.trans e2
  rw [h5, h1] at hA
  rw [← Quint11.quint_A' n (ring 4) hpr w hw1 hw5]
  calc CharSumGen.jpr (Cyclo5.cyc5 (ring 4)) (2 * (5 * n + 4)) (Quint6.psi n (ring 4) w) 3 = (Cyclo5.cyc5 (ring 4)).neg ((Cyclo5.cyc5 (ring 4)).mul ((Cyclo5.cyc5 (ring 4)).mul (CharSumGen.jpr (Cyclo5.cyc5 (ring 4)) (2 * (5 * n + 4)) (Quint6.psi n (ring 4) w) 3) (Cyclo5.cyc5 (ring 4)).one) ((Cyclo5.cyc5 (ring 4)).neg (Cyclo5.cyc5 (ring 4)).one)) := by lib_ring
    _ = (Cyclo5.cyc5 (ring 4)).neg ((Cyclo5.cyc5 (ring 4)).mul (Cyclo5.cyc5 (ring 4)).one (Cyclo5.cyc5 (ring 4)).one) := by rw [hA]
    _ = (Cyclo5.cyc5 (ring 4)).neg (Cyclo5.cyc5 (ring 4)).one := by lib_ring

end Quint18

#print axioms Quint18.gauss_frob'
#print axioms Quint18.Q5_ne
#print axioms Quint18.quint_primary

#print axioms: 3 theorems, no axioms.

78.192 Eisenstein’s relation for the primary quintic element

For primes \(q\neq p\) with \(q^4=5M+1\) and \((q-1)\mid M\), over \(K=\mathbb F_q[\sqrt 5][\sqrt D]\) with \(\Phi _5(u)=0\):

\[ \mathrm {ev}_u\,\psi (q^{-4})=\mathrm {ev}_u\bigl (J^2\sigma _2(J)\bigr )^{M}. \]

lang/Quint19.lean

/-
  lang/Quint19 — T49, cycle 767 (ℓ = 5 symbol project, step 19): **removing p from the symbol form**.
  When (q − 1) | M (q⁴ = 5M + 1; e.g. q ≢ 1 (mod 5)) and p ≢ 0 (mod q), p^M = 1 in 𝔽_q (Fermat), and J(u)·J(u⁴) = ev_u(J conj J) = p, so
    quint_sym_A      ev_u ι ψ(q⁻⁴) = (J(u)² J(u²))^M = ev_u(J² σ₂J)^M     in K = 𝔽_q[√5][√D],
  i.e. χ_p(q)⁻⁴ ≡ A^((q⁴−1)/5) (mod q) for the primary element A = J²σ₂J ≡ −1 (mod 5) of Quint18 — Eisenstein's relation for A.
    hom_pow          a ring map commutes with powers
    ev_scalar        ev_u(ph5 (z, 0, 0, 0)) = castZ z
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Quint18

namespace Quint19

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius
open LibRing (castZ castN)

section Gen
variable {R K' : Type} (T0 : CRing R) (K0 : CRing K') (ι : R → K')

theorem hom_pow (hmul : ∀ a b, ι (T0.mul a b) = K0.mul (ι a) (ι b)) (hone : ι T0.one = K0.one) (x : R) : ∀ k, ι (pw T0 x k) = pw K0 (ι x) k
  | 0 => hone
  | k + 1 => by
    show ι (T0.mul (pw T0 x k) x) = K0.mul (pw K0 (ι x) k) (ι x)
    rw [hmul, hom_pow hmul hone x k]

theorem ev_scalar (v : K') (z : Int) : Quint13.ev K0 v (Quint6.ph5 K0 (z, 0, 0, 0)) = castZ K0 z := by
  show K0.add (K0.add (K0.add (castZ K0 z) (K0.mul K0.zero v)) (K0.mul K0.zero (K0.mul v v))) (K0.mul K0.zero (K0.mul (K0.mul v v) v)) = castZ K0 z
  lib_ring
end Gen

variable (n k : Nat)

/-- **χ_p(q)⁻⁴ ≡ (J²σ₂J)^((q⁴−1)/5) (mod q)** when (q − 1) | (q⁴ − 1)/5 and q ≠ p. -/
theorem quint_sym_A (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).  -- (+208 chars; full line in the Zenodo source)
    (hM : (k + 1 + 1) ^ 4 = 5 * M + 1) (kk : Nat) (hkk : k + 1 + 1 = 2 * kk + 1) (t : Nat) (ht : M = (k + 1) * t)
    (hp : (LibRing.castN (ring (k + 1)) (2 * (5 * n + 4) + 1 + 1 + 1)) ≠ (ring (k + 1)).zero) :
    Quint13.ev (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k +   -- (+957 chars; full line in the Zenodo source)
      = pw (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).  -- (+6816 chars; full line in the Zenodo source)
  have hu := Quint15.phi5_root (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (nsm (ring (k + 1)) (kk + 1) (ring (k + 1)).one, (ring (k + 1)).zero) ((ring (k + 1)).zero, (ring (k + 1)).one) (Quint17.e2' k kk hkk) (Quint17.e5' k)
  have hAC : (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)  -- (+6256 chars; full line in the Zenodo source)
    rw [← Quint13.ev_conj (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one)   -- (+1412 chars; full line in the Zenodo source)
      show (Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w)) (Quint5.conj5 (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w))) = (Int.ofNat (2 * (5 * n + 4) + 1 + 1 + 1), 0, 0, 0) from Quint5.quint_norm n hpr w hw1 hw5, ev_scalar]
    exact (Quint14.iota_castN (ring (k + 1)) (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (  -- (+3714 chars; full line in the Zenodo source)
  have hX : pw (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k +   -- (+1048 chars; full line in the Zenodo source)
    (hom_pow (ring (k + 1)) (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one  -- (+1725 chars; full line in the Zenodo source)
  rw [Quint17.quint_sym_all n k hpr w hw1 hw5 hq hQ M hM kk hkk,
    show (Quad.quad (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one) (ring (k + 1)).one)) (Quint15.dd (Quad.quad (ring (k + 1)) ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one (ring (k + 1)).one) (ring (k + 1)).on  -- (+26657 chars; full line in the Zenodo source)
    pw_mul, hAC, hX]
  lib_ring

end Quint19

#print axioms Quint19.hom_pow
#print axioms Quint19.ev_scalar
#print axioms Quint19.quint_sym_A

#print axioms: 3 theorems, no axioms.

78.193 The norm of the primary quintic element

For \(A=J^2\sigma _2(J)\) with \(J=J(\chi ,\chi )\) and complex conjugation on \(\mathbb Z[\zeta _5]\):

\[ A\,\overline A=p^3. \]

lang/Quint20.lean

/-
  lang/Quint20 — T49, cycle 768 (ℓ = 5 symbol project, step 20): **the primary element A = J²σ₂(J) has A·conj(A) = p³**.
    conj_sig2      conj ∘ σ₂ = σ₂ ∘ conj on ℤ[ζ₅] (both are ζ ↦ ζ^(−2) = ζ³)
    sig2_scalar    σ₂ fixes ℤ
    normA          A · conj(A) = (J conj J)² · σ₂(J conj J) = p² · p = p³        (Quint5.quint_norm)
  With Quint18 (A ≡ −1 mod 5): A is a primary element of ℤ[ζ₅] of absolute norm squared p³, the Stickelberger element of χ.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Quint19

namespace Quint20

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

theorem conj_sig2 : ∀ z : Int × Int × Int × Int, Quint5.conj5 (Quint11.sig2 z) = Quint11.sig2 (Quint5.conj5 z)
  | (a0, a1, a2, a3) => by
    apply Prod.ext
    · show ((a0 + (-a2)) + (-(a3 + (-a2)))) = ((a0 + (-a1)) + (-(a3 + (-a1))))
      lib_int
    apply Prod.ext
    · show (-(a3 + (-a2))) = ((a2 + (-a1)) + (-(a3 + (-a1))))
      lib_int
    apply Prod.ext
    · show ((-a2) + (-(a3 + (-a2)))) = ((-a1) + (-(a3 + (-a1))))
      lib_int
    · show ((a1 + (-a2)) + (-(a3 + (-a2)))) = (-(a3 + (-a1)))
      lib_int

theorem sig2_scalar (m : Int) : Quint11.sig2 (m, 0, 0, 0) = (m, 0, 0, 0) := by
  apply Prod.ext
  · show m + -0 = m; lib_int
  apply Prod.ext
  · show (0 : Int) + -0 = 0; lib_int
  apply Prod.ext
  · show (0 : Int) + -0 = 0; lib_int
  · show -(0 : Int) = 0; lib_int

variable (n : Nat)

/-- **A · conj(A) = p³**, A = J² σ₂(J). -/
theorem normA (hpr : ∀ d, Dv d (2 * (5 * n + 4) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (5 * n + 4) + 1 + 1 + 1) (w : Zp (2 * (5 * n + 4) + 1 + 1)) (hw1 : w ≠ (ring (2 * (5 * n + 4) + 1 + 1)).one) (hw5 : (ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul ((ring (2 * (5 * n + 4) + 1 + 1)).mul w w) w) w) w = (ring (2 * (5 * n + 4) + 1 + 1)).one) :  -- (+1008 chars; full line in the Zenodo source)
  have hN : (Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w)) (Quint5.conj5 (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w))) = ((Int.ofNat (2 * (5 * n + 4) + 1 + 1 + 1)), 0, 0, 0) := Quint5.quint_norm n hpr w hw1 hw5
  rw [Quint5.conj5_mul, Quint5.conj5_mul, conj_sig2]
  rw [show (Cyclo5.cyc5 LibInt.intR).mul ((Cyclo5.cyc5 LibInt.intR).mul ((Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w)) (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w))) (Quint11.sig2 (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5   -- (+419 chars; full line in the Zenodo source)
      = (Cyclo5.cyc5 LibInt.intR).mul ((Cyclo5.cyc5 LibInt.intR).mul ((Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w)) (Quint5.conj5 (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Quint4.chi5 n w) (Quint4.chi5 n w)))) ((Cyclo5.cyc5 LibInt.intR).mul (CharSumGen.J2 (Cyclo5.cyc5 LibInt.intR) (2 * (5 * n + 4)) (Q  -- (+429 chars; full line in the Zenodo source)
    ← Quint11.sig2_mul, hN, sig2_scalar]

end Quint20

#print axioms Quint20.conj_sig2
#print axioms Quint20.sig2_scalar
#print axioms Quint20.normA

#print axioms: 3 theorems, no axioms.

78.194 The ring \(K[\zeta _7]\)

The quotient \(K[\zeta ]/(1+\zeta +\dots +\zeta ^6)\) is a commutative ring for every commutative ring \(K\).

\[ \zeta ^6=-(1+\zeta +\zeta ^2+\zeta ^3+\zeta ^4+\zeta ^5),\qquad \zeta ^7=1. \]

lang/Cyclo7.lean

/-
  lang/Cyclo7 — T49, cycle 769 (ℓ = 7 start): the ring K[ζ]/(1 + ζ + … + ζ⁶) over any commutative ring K.
  Elements a₀ + a₁ζ + … + a₅ζ⁵ as 6-tuples; ζ⁶ = −(1 + ζ + … + ζ⁵), ζ⁷ = 1:
    c7mul     the product (raw coefficients r₀..r₁₀ reduced by ζ⁷ = 1 and ζ⁶ = −Σ_{i<6} ζ^i: c_i = r_i + r_(i+7) − r₆)
    cyc7      the commutative ring structure (every law by lib_ring, componentwise; generated by c7gen.py)
  The ℓ = 7 analogue of Cyclo5 (base of the quintic symbol project Quint1–Quint20).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lib.RingTac
import lang.Quad

namespace Cyclo7

variable {R : Type} (K : DetChain.CRing R)

abbrev T6 (R : Type) := R × R × R × R × R × R

def c7mul (x y : T6 R) : T6 R :=
  ((K.add (K.add (K.mul x.1 y.1) (K.add (K.add (K.add (K.mul x.2.2.1 y.2.2.2.2.2) (K.mul x.2.2.2.1 y.2.2.2.2.1)) (K.mul x.2.2.2.2.1 y.2.2.2.1)) (K.mul x.2.2.2.2.2 y.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul x.2.1 y.2.2.2.2.2) (K.mul x.2.2.1 y.2.2.2.2.1)) (K.mul x.2.2.2.1 y.2.2.2.1)) (K.mul x.2.2.2.2.1 y.2.2.1)) (K.mul x.2.2.2.2.2 y.2.1)))), (K.add (K.add (K.add (K.mul x.1 y.2.1) (K.mul x.  -- (+1688 chars; full line in the Zenodo source)

theorem c7_assoc (a b e : T6 R) : c7mul K (c7mul K a b) e = c7mul K a (c7mul K b e) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul (K.add (K.add (K.mul a.1 b.1) (K.add (K.add (K.add (K.mul a.2.2.1 b.2.2.2.2.2) (K.mul a.2.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.2 b.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 b.2.2.2.2.2) (K.mul a.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.2 b.2.1)))) e.1) (K.add (K.add (K.  -- (+7054 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul (K.add (K.add (K.mul a.1 b.1) (K.add (K.add (K.add (K.mul a.2.2.1 b.2.2.2.2.2) (K.mul a.2.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.2 b.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 b.2.2.2.2.2) (K.mul a.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.2 b.2.1)))) e.2.1) (K.mul (  -- (+7058 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.add (K.mul (K.add (K.add (K.mul a.1 b.1) (K.add (K.add (K.add (K.mul a.2.2.1 b.2.2.2.2.2) (K.mul a.2.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.2 b.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 b.2.2.2.2.2) (K.mul a.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.2 b.2.1)))) e.2.2.1)  -- (+7062 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.add (K.add (K.mul (K.add (K.add (K.mul a.1 b.1) (K.add (K.add (K.add (K.mul a.2.2.1 b.2.2.2.2.2) (K.mul a.2.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.2 b.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 b.2.2.2.2.2) (K.mul a.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.2 b.2.1)))) e  -- (+7058 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.add (K.add (K.mul (K.add (K.add (K.mul a.1 b.1) (K.add (K.add (K.add (K.mul a.2.2.1 b.2.2.2.2.2) (K.mul a.2.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.2 b.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 b.2.2.2.2.2) (K.mul a.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.2 b.2.1)))) e  -- (+6978 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (K.add (K.add (K.mul a.1 b.1) (K.add (K.add (K.add (K.mul a.2.2.1 b.2.2.2.2.2) (K.mul a.2.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.2 b.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 b.2.2.2.2.2) (K.mul a.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.2 b.2.  -- (+7786 chars; full line in the Zenodo source)
    lib_ring

theorem c7_comm (a b : T6 R) : c7mul K a b = c7mul K b a := by
  apply Prod.ext
  · show (K.add (K.add (K.mul a.1 b.1) (K.add (K.add (K.add (K.mul a.2.2.1 b.2.2.2.2.2) (K.mul a.2.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.2 b.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 b.2.2.2.2.2) (K.mul a.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.2 b.2.1)))) = (K.add (K.add (K.mul b.1 a.1) (K.add (K.a  -- (+306 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul a.1 b.2.1) (K.mul a.2.1 b.1)) (K.add (K.add (K.mul a.2.2.2.1 b.2.2.2.2.2) (K.mul a.2.2.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.2.2 b.2.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 b.2.2.2.2.2) (K.mul a.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.2 b.2.1)))) = (K.add (K.add (K.add (K.mul b.1 a.2.1) (K.mul  -- (+298 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.add (K.mul a.1 b.2.2.1) (K.mul a.2.1 b.2.1)) (K.mul a.2.2.1 b.1)) (K.add (K.mul a.2.2.2.2.1 b.2.2.2.2.2) (K.mul a.2.2.2.2.2 b.2.2.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 b.2.2.2.2.2) (K.mul a.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.2 b.2.1)))) = (K.add (K.add (K.add (K.add (K.mul b.1 a.2.2.1) (  -- (+290 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.add (K.add (K.mul a.1 b.2.2.2.1) (K.mul a.2.1 b.2.2.1)) (K.mul a.2.2.1 b.2.1)) (K.mul a.2.2.2.1 b.1)) (K.mul a.2.2.2.2.2 b.2.2.2.2.2)) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 b.2.2.2.2.2) (K.mul a.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.2 b.2.1)))) = (K.add (K.add (K.add (K.add (K.add (K.mul b.1 a.2.2.2  -- (+282 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.add (K.add (K.mul a.1 b.2.2.2.2.1) (K.mul a.2.1 b.2.2.2.1)) (K.mul a.2.2.1 b.2.2.1)) (K.mul a.2.2.2.1 b.2.1)) (K.mul a.2.2.2.2.1 b.1)) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 b.2.2.2.2.2) (K.mul a.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.2 b.2.1)))) = (K.add (K.add (K.add (K.add (K.add (K.mul b.1 a.2.2.2  -- (+282 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.1 b.2.2.2.2.2) (K.mul a.2.1 b.2.2.2.2.1)) (K.mul a.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.1 b.2.1)) (K.mul a.2.2.2.2.2 b.1)) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 b.2.2.2.2.2) (K.mul a.2.2.1 b.2.2.2.2.1)) (K.mul a.2.2.2.1 b.2.2.2.1)) (K.mul a.2.2.2.2.1 b.2.2.1)) (K.mul a.2.2.2.2.2 b.2.1)))) = (K.add (K.add  -- (+362 chars; full line in the Zenodo source)
    lib_ring

theorem c7_one (a : T6 R) : c7mul K (K.one, K.zero, K.zero, K.zero, K.zero, K.zero) a = a := by
  apply Prod.ext
  · show (K.add (K.add (K.mul K.one a.1) (K.add (K.add (K.add (K.mul K.zero a.2.2.2.2.2) (K.mul K.zero a.2.2.2.2.1)) (K.mul K.zero a.2.2.2.1)) (K.mul K.zero a.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul K.zero a.2.2.2.2.2) (K.mul K.zero a.2.2.2.2.1)) (K.mul K.zero a.2.2.2.1)) (K.mul K.zero a.2.2.1)) (K.mul K.zero a.2.1)))) = a.1
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul K.one a.2.1) (K.mul K.zero a.1)) (K.add (K.add (K.mul K.zero a.2.2.2.2.2) (K.mul K.zero a.2.2.2.2.1)) (K.mul K.zero a.2.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul K.zero a.2.2.2.2.2) (K.mul K.zero a.2.2.2.2.1)) (K.mul K.zero a.2.2.2.1)) (K.mul K.zero a.2.2.1)) (K.mul K.zero a.2.1)))) = a.2.1
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.add (K.mul K.one a.2.2.1) (K.mul K.zero a.2.1)) (K.mul K.zero a.1)) (K.add (K.mul K.zero a.2.2.2.2.2) (K.mul K.zero a.2.2.2.2.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul K.zero a.2.2.2.2.2) (K.mul K.zero a.2.2.2.2.1)) (K.mul K.zero a.2.2.2.1)) (K.mul K.zero a.2.2.1)) (K.mul K.zero a.2.1)))) = a.2.2.1
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.add (K.add (K.mul K.one a.2.2.2.1) (K.mul K.zero a.2.2.1)) (K.mul K.zero a.2.1)) (K.mul K.zero a.1)) (K.mul K.zero a.2.2.2.2.2)) (K.neg (K.add (K.add (K.add (K.add (K.mul K.zero a.2.2.2.2.2) (K.mul K.zero a.2.2.2.2.1)) (K.mul K.zero a.2.2.2.1)) (K.mul K.zero a.2.2.1)) (K.mul K.zero a.2.1)))) = a.2.2.2.1
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.add (K.add (K.mul K.one a.2.2.2.2.1) (K.mul K.zero a.2.2.2.1)) (K.mul K.zero a.2.2.1)) (K.mul K.zero a.2.1)) (K.mul K.zero a.1)) (K.neg (K.add (K.add (K.add (K.add (K.mul K.zero a.2.2.2.2.2) (K.mul K.zero a.2.2.2.2.1)) (K.mul K.zero a.2.2.2.1)) (K.mul K.zero a.2.2.1)) (K.mul K.zero a.2.1)))) = a.2.2.2.2.1
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.add (K.add (K.mul K.one a.2.2.2.2.2) (K.mul K.zero a.2.2.2.2.1)) (K.mul K.zero a.2.2.2.1)) (K.mul K.zero a.2.2.1)) (K.mul K.zero a.2.1)) (K.mul K.zero a.1)) (K.neg (K.add (K.add (K.add (K.add (K.mul K.zero a.2.2.2.2.2) (K.mul K.zero a.2.2.2.2.1)) (K.mul K.zero a.2.2.2.1)) (K.mul K.zero a.2.2.1)) (K.mul K.zero a.2.1)))) = a.2.2.2.2.2
    lib_ring

theorem c7_distrib (a b e : T6 R) :
    c7mul K a ((K.add b.1 e.1), (K.add b.2.1 e.2.1), (K.add b.2.2.1 e.2.2.1), (K.add b.2.2.2.1 e.2.2.2.1), (K.add b.2.2.2.2.1 e.2.2.2.2.1), (K.add b.2.2.2.2.2 e.2.2.2.2.2))
      = ((K.add (c7mul K a b).1 (c7mul K a e).1), (K.add (c7mul K a b).2.1 (c7mul K a e).2.1), (K.add (c7mul K a b).2.2.1 (c7mul K a e).2.2.1), (K.add (c7mul K a b).2.2.2.1 (c7mul K a e).2.2.2.1), (K.add (c7mul K a b).2.2.2.2.1 (c7mul K a e).2.2.2.2.1), (K.add (c7mul K a b).2.2.2.2.2 (c7mul K a e).2.2.2.2.2)) := by
  apply Prod.ext
  · show (K.add (K.add (K.mul a.1 (K.add b.1 e.1)) (K.add (K.add (K.add (K.mul a.2.2.1 (K.add b.2.2.2.2.2 e.2.2.2.2.2)) (K.mul a.2.2.2.1 (K.add b.2.2.2.2.1 e.2.2.2.2.1))) (K.mul a.2.2.2.2.1 (K.add b.2.2.2.1 e.2.2.2.1))) (K.mul a.2.2.2.2.2 (K.add b.2.2.1 e.2.2.1)))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 (K.add b.2.2.2.2.2 e.2.2.2.2.2)) (K.mul a.2.2.1 (K.add b.2.2.2.2.1 e.2.2.2.2.1))) (K.m  -- (+836 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.mul a.1 (K.add b.2.1 e.2.1)) (K.mul a.2.1 (K.add b.1 e.1))) (K.add (K.add (K.mul a.2.2.2.1 (K.add b.2.2.2.2.2 e.2.2.2.2.2)) (K.mul a.2.2.2.2.1 (K.add b.2.2.2.2.1 e.2.2.2.2.1))) (K.mul a.2.2.2.2.2 (K.add b.2.2.2.1 e.2.2.2.1)))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 (K.add b.2.2.2.2.2 e.2.2.2.2.2)) (K.mul a.2.2.1 (K.add b.2.2.2.2.1 e.2.2.2.2.1))) (K.mul a.2  -- (+822 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.add (K.mul a.1 (K.add b.2.2.1 e.2.2.1)) (K.mul a.2.1 (K.add b.2.1 e.2.1))) (K.mul a.2.2.1 (K.add b.1 e.1))) (K.add (K.mul a.2.2.2.2.1 (K.add b.2.2.2.2.2 e.2.2.2.2.2)) (K.mul a.2.2.2.2.2 (K.add b.2.2.2.2.1 e.2.2.2.2.1)))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 (K.add b.2.2.2.2.2 e.2.2.2.2.2)) (K.mul a.2.2.1 (K.add b.2.2.2.2.1 e.2.2.2.2.1))) (K.mul a.2.2.2.1  -- (+808 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.add (K.add (K.mul a.1 (K.add b.2.2.2.1 e.2.2.2.1)) (K.mul a.2.1 (K.add b.2.2.1 e.2.2.1))) (K.mul a.2.2.1 (K.add b.2.1 e.2.1))) (K.mul a.2.2.2.1 (K.add b.1 e.1))) (K.mul a.2.2.2.2.2 (K.add b.2.2.2.2.2 e.2.2.2.2.2))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 (K.add b.2.2.2.2.2 e.2.2.2.2.2)) (K.mul a.2.2.1 (K.add b.2.2.2.2.1 e.2.2.2.2.1))) (K.mul a.2.2.2.1 (K.ad  -- (+794 chars; full line in the Zenodo source)
    lib_ring
  apply Prod.ext
  · show (K.add (K.add (K.add (K.add (K.add (K.mul a.1 (K.add b.2.2.2.2.1 e.2.2.2.2.1)) (K.mul a.2.1 (K.add b.2.2.2.1 e.2.2.2.1))) (K.mul a.2.2.1 (K.add b.2.2.1 e.2.2.1))) (K.mul a.2.2.2.1 (K.add b.2.1 e.2.1))) (K.mul a.2.2.2.2.1 (K.add b.1 e.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 (K.add b.2.2.2.2.2 e.2.2.2.2.2)) (K.mul a.2.2.1 (K.add b.2.2.2.2.1 e.2.2.2.2.1))) (K.mul a.2.2.2.1 (K.ad  -- (+794 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.1 (K.add b.2.2.2.2.2 e.2.2.2.2.2)) (K.mul a.2.1 (K.add b.2.2.2.2.1 e.2.2.2.2.1))) (K.mul a.2.2.1 (K.add b.2.2.2.1 e.2.2.2.1))) (K.mul a.2.2.2.1 (K.add b.2.2.1 e.2.2.1))) (K.mul a.2.2.2.2.1 (K.add b.2.1 e.2.1))) (K.mul a.2.2.2.2.2 (K.add b.1 e.1))) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2.1 (K.add b.2.2.2.2.2 e.2.2.2.2.2)) (K.mul a.2.  -- (+934 chars; full line in the Zenodo source)
    lib_ring

def cyc7 : DetChain.CRing (T6 R) where
  add := fun x y => ((K.add x.1 y.1), (K.add x.2.1 y.2.1), (K.add x.2.2.1 y.2.2.1), (K.add x.2.2.2.1 y.2.2.2.1), (K.add x.2.2.2.2.1 y.2.2.2.2.1), (K.add x.2.2.2.2.2 y.2.2.2.2.2))
  mul := c7mul K
  neg := fun x => ((K.neg x.1), (K.neg x.2.1), (K.neg x.2.2.1), (K.neg x.2.2.2.1), (K.neg x.2.2.2.2.1), (K.neg x.2.2.2.2.2))
  zero := (K.zero, K.zero, K.zero, K.zero, K.zero, K.zero)
  one := (K.one, K.zero, K.zero, K.zero, K.zero, K.zero)
  add_assoc := fun _ _ _ => Prod.ext (K.add_assoc _ _ _) (Prod.ext (K.add_assoc _ _ _) (Prod.ext (K.add_assoc _ _ _) (Prod.ext (K.add_assoc _ _ _) (Prod.ext (K.add_assoc _ _ _) (K.add_assoc _ _ _)))))
  add_comm := fun _ _ => Prod.ext (K.add_comm _ _) (Prod.ext (K.add_comm _ _) (Prod.ext (K.add_comm _ _) (Prod.ext (K.add_comm _ _) (Prod.ext (K.add_comm _ _) (K.add_comm _ _)))))
  zero_add := fun _ => Prod.ext (K.zero_add _) (Prod.ext (K.zero_add _) (Prod.ext (K.zero_add _) (Prod.ext (K.zero_add _) (Prod.ext (K.zero_add _) (K.zero_add _)))))
  add_neg := fun _ => Prod.ext (K.add_neg _) (Prod.ext (K.add_neg _) (Prod.ext (K.add_neg _) (Prod.ext (K.add_neg _) (Prod.ext (K.add_neg _) (K.add_neg _)))))
  mul_assoc := c7_assoc K
  mul_comm := c7_comm K
  one_mul := c7_one K
  distrib := c7_distrib K

end Cyclo7

#print axioms Cyclo7.c7mul
#print axioms Cyclo7.c7_assoc
#print axioms Cyclo7.c7_comm
#print axioms Cyclo7.c7_one
#print axioms Cyclo7.c7_distrib
#print axioms Cyclo7.cyc7

#print axioms: 6 theorems, no axioms.

78.195 Seventh roots of unity in \(\mathbb F_p\)

For \(p=14n+15\) and \(w\neq 1\) with \(w^7=1\), the seventh roots of unity in \(\mathbb F_p\) are the powers of \(w\).

\[ t^7=1\ \Rightarrow \ t\in \{1,w,w^2,\dots ,w^6\}. \]

lang/Sept1.lean

/-
  lang/Sept1 — T49, cycle 770 (ℓ = 7): **seventh roots of unity in 𝔽_p, p = 14n + 15** (first step of the septic character):
    r_zero7      w ≠ 1, w⁷ = 1 ⇒ 1 + w + … + w⁶ = 0
    fact7        ∏_{j<7} (t − w^j) = t⁷ − 1 modulo 1 + w + … + w⁶      (generated certificate, 24 terms)
    seven_cases  t⁷ = 1 ⇒ t ∈ {1, w, …, w⁶}
  The ℓ = 7 analogue of Quint1. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G4Gen

namespace Sept1

open FTAChain ZMod YMChain FiniteRecChain DetChain

variable (n : Nat)

theorem r_zero7 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+1030 chars; full line in the Zenodo source)
  have e : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).add w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring  -- (+710 chars; full line in the Zenodo source)
    rw [show ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).add w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ri  -- (+1059 chars; full line in the Zenodo source)
  rcases G4Gen.mul_eq_zero (7 * n + 6) hpr _ _ e with e1 | e1
  · exact absurd (by rw [show w = ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) (ring (2 * (7 * n + 6) + 1 + 1)).one) by lib_ring, e1]; lib_ring) hw1
  · exact e1

theorem fact7 (t w : Zp (2 * (7 * n + 6) + 1 + 1)) (hr : ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (  -- (+2426 chars; full line in the Zenodo source)
  have e : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).add t ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) ((ring (2 * (7 * n + 6) + 1 + 1)).add t ((ri  -- (+13424 chars; full line in the Zenodo source)
  rw [e, hr]; lib_ring

theorem seven_cases (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2  -- (+1129 chars; full line in the Zenodo source)
  have h0 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).add t ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) ((ring (2 * (7 * n + 6) + 1 + 1)).add t ((r  -- (+1115 chars; full line in the Zenodo source)
    rw [fact7 n t w (r_zero7 n hpr w hw1 hw7), ht]; lib_ring
  have sub : ∀ a b : Zp (2 * (7 * n + 6) + 1 + 1), ((ring (2 * (7 * n + 6) + 1 + 1)).add a ((ring (2 * (7 * n + 6) + 1 + 1)).neg b)) = (ring (2 * (7 * n + 6) + 1 + 1)).zero → a = b := fun a b e => by
    rw [show a = ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add a ((ring (2 * (7 * n + 6) + 1 + 1)).neg b)) b) by lib_ring, e]; lib_ring
  rcases G4Gen.mul_eq_zero (7 * n + 6) hpr _ _ h0 with e6 | e6
  · rcases G4Gen.mul_eq_zero (7 * n + 6) hpr _ _ e6 with e5 | e5
    · rcases G4Gen.mul_eq_zero (7 * n + 6) hpr _ _ e5 with e4 | e4
      · rcases G4Gen.mul_eq_zero (7 * n + 6) hpr _ _ e4 with e3 | e3
        · rcases G4Gen.mul_eq_zero (7 * n + 6) hpr _ _ e3 with e2 | e2
          · rcases G4Gen.mul_eq_zero (7 * n + 6) hpr _ _ e2 with e1 | e1
            · exact (Or.inl (sub _ _ e1))
            · exact (Or.inr (Or.inl (sub _ _ e1)))
          · exact (Or.inr (Or.inr (Or.inl (sub _ _ e2))))
        · exact (Or.inr (Or.inr (Or.inr (Or.inl (sub _ _ e3)))))
      · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (sub _ _ e4))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (sub _ _ e5)))))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (sub _ _ e6)))))))

end Sept1

#print axioms Sept1.r_zero7
#print axioms Sept1.fact7
#print axioms Sept1.seven_cases

#print axioms: 3 theorems, no axioms.

78.196 The septic character of \(\mathbb F_p\)

For \(p=14n+15\) the character \(\chi (x)=\zeta ^i\) when \(x^{(p-1)/7}=w^i\) takes values in \(\mathbb Z[\zeta _7]\).

\[ \chi (xy)=\chi (x)\chi (y),\qquad \chi (-1)=1,\qquad \sum _{x}\chi (x)=0. \]

lang/Sept2.lean

/-
  lang/Sept2 — T49, cycle 771 (ℓ = 7, step 2): **the septic character of 𝔽_p with values in ℤ[ζ₇]** (p = 14n + 15).
  N = (p − 1)/7 = 2n + 2, w ≠ 1, w⁷ = 1; for x ≠ 0, x^N = w^i (i < 7, Sept1.seven_cases) and χ(x) = ζ^i in Cyclo7.cyc7 ℤ (χ(0) = 0):
    z7, zt_mod7                 ζ⁷ = 1, ζ^(7m + r) = ζ^r
    wfix, wk_ne_one, wpow_ne    w^(7r+1) = w, w^k ≠ 1 (0 < k < 7), the w^i (i < 7) are pairwise distinct
    lg_val, exists_i, val_of    the index of x^N among w⁰ … w⁶, and χ(x) = ζ^i
    chi7_mul, chi7_neg1         χ(xy) = χ(x)χ(y), χ(−1) = 1
    zeta_fix7, iter, chi7_sum   ζS = S ⇒ S = 0 (7a₅ = 0), Σ_x χ(x) = 0
  The ℓ = 7 analogue of Quint4 (generated by s2gen.py). Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Sept1
import lang.Cyclo7
import lang.CubicChar
import lang.CubicRecip

namespace Sept2

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

def zeta : Int × Int × Int × Int × Int × Int := (0, 1, 0, 0, 0, 0)

theorem z7 : pw (Cyclo7.cyc7 LibInt.intR) zeta 7 = (Cyclo7.cyc7 LibInt.intR).one := rfl

theorem zt_mod7 (m r : Nat) : pw (Cyclo7.cyc7 LibInt.intR) zeta (7 * m + r) = pw (Cyclo7.cyc7 LibInt.intR) zeta r := by
  rw [HasseCM.pw_add', CubicRecip.pw_mul_split, z7, CubicChar.pw_one'', (Cyclo7.cyc7 LibInt.intR).one_mul]

variable (n : Nat)

def lg (w t : Zp (2 * (7 * n + 6) + 1 + 1)) : Nat :=
  if t = pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0 then 0 else if t = pw (ring (2 * (7 * n + 6) + 1 + 1)) w 1 then 1 else if t = pw (ring (2 * (7 * n + 6) + 1 + 1)) w 2 then 2 else if t = pw (ring (2 * (7 * n + 6) + 1 + 1)) w 3 then 3 else if t = pw (ring (2 * (7 * n + 6) + 1 + 1)) w 4 then 4 else if t = pw (ring (2 * (7 * n + 6) + 1 + 1)) w 5 then 5 else 6

def chi7 (w x : Zp (2 * (7 * n + 6) + 1 + 1)) : Int × Int × Int × Int × Int × Int :=
  if x = (ring (2 * (7 * n + 6) + 1 + 1)).zero then (Cyclo7.cyc7 LibInt.intR).zero else pw (Cyclo7.cyc7 LibInt.intR) zeta (lg n w (pw (ring (2 * (7 * n + 6) + 1 + 1)) x (2 * n + 2)))

theorem w7 (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) w) w) w) w) = (ring (2 * (7 * n + 6) + 1 + 1)).one) : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 7 = (ring (2 * (7 *  -- (+27 chars; full line in the Zenodo source)
  show (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one w) w) w) w) w) w) w = (ring (2 * (7 * n + 6) + 1 + 1)).one
  rw [(ring (2 * (7 * n + 6) + 1 + 1)).one_mul]; exact hw7

theorem wfix (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) w) w) w) w) = (ring (2 * (7 * n + 6) + 1 + 1)).one) (r : Nat) : pw (ring (2 * (7 * n + 6) + 1 + 1)) w (7 * r   -- (+14 chars; full line in the Zenodo source)
  rw [HasseCM.pw_add', ← TwoPower.pwpow, w7 n w hw7, CubicChar.pw_one'']
  show (ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one w) = w
  rw [(ring (2 * (7 * n + 6) + 1 + 1)).one_mul, (ring (2 * (7 * n + 6) + 1 + 1)).one_mul]

theorem wk_ne_one (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) w) w) w) w) = (ring (2 * (7 * n + 6) + 1 + 1)).one) (k   -- (+137 chars; full line in the Zenodo source)
  have h1 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w (k * m) = (ring (2 * (7 * n + 6) + 1 + 1)).one := by rw [← TwoPower.pwpow, e]; exact CubicChar.pw_one'' (ring (2 * (7 * n + 6) + 1 + 1)) m
  rw [hkm, wfix n w hw7] at h1; exact h1)

theorem w_ne_zero (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) w) w) w) w) = (ring (2 * (7 * n + 6) + 1 + 1)).one) : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := fun e  -- (+6 chars; full line in the Zenodo source)
  have h := w7 n w hw7
  rw [e] at h
  have : pw (ring (2 * (7 * n + 6) + 1 + 1)) (ring (2 * (7 * n + 6) + 1 + 1)).zero 7 = (ring (2 * (7 * n + 6) + 1 + 1)).zero := mul_zero (ring (2 * (7 * n + 6) + 1 + 1)) _
  rw [this] at h
  exact PowerSum.one_ne_zero' (2 * (7 * n + 6) + 1) h.symm

theorem pwnz (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (x : Zp (2 * (7 * n + 6) + 1 + 1)) (hx : x ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero) : ∀ k, pw (ring (2 * (7 * n + 6) + 1 + 1)) x k ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero
  | 0 => fun e => PowerSum.one_ne_zero' (2 * (7 * n + 6) + 1) e
  | k + 1 => fun e => by
    cases G4Gen.mul_eq_zero (7 * n + 6) hpr (pw (ring (2 * (7 * n + 6) + 1 + 1)) x k) x e with
    | inl h => exact pwnz hpr x hx k h
    | inr h => exact hx h

theorem cancel1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (a b : Zp (2 * (7 * n + 6) + 1 + 1)) (ha : a ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero) (h : (ring (2 * (7 * n + 6) + 1 + 1)).mul a b = a) : b = (ring (2 * (7 * n + 6) + 1 + 1)).one := by
  have e : (ring (2 * (7 * n + 6) + 1 + 1)).mul a ((ring (2 * (7 * n + 6) + 1 + 1)).add b ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = (ring (2 * (7 * n + 6) + 1 + 1)).zero := by
    rw [show (ring (2 * (7 * n + 6) + 1 + 1)).mul a ((ring (2 * (7 * n + 6) + 1 + 1)).add b ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = (ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).mul a b) ((ring (2 * (7 * n + 6) + 1 + 1)).neg a) by lib_ring, h]; lib_ring
  cases G4Gen.mul_eq_zero (7 * n + 6) hpr _ _ e with
  | inl h' => exact absurd h' ha
  | inr h' =>
    calc b = (ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).add b ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) (ring (2 * (7 * n + 6) + 1 + 1)).one := by lib_ring
      _ = (ring (2 * (7 * n + 6) + 1 + 1)).one := by rw [h']; lib_ring

theorem wpow_ne (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) w) w) w) w) = (r  -- (+247 chars; full line in the Zenodo source)
  hd (cancel1 n hpr (pw (ring (2 * (7 * n + 6) + 1 + 1)) w i) (pw (ring (2 * (7 * n + 6) + 1 + 1)) w (d + 1)) (pwnz n hpr w (w_ne_zero n w hw7) i) (by rw [← HasseCM.pw_add' (ring (2 * (7 * n + 6) + 1 + 1)) w i (d + 1)]; exact e))

theorem lg_val (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+151 chars; full line in the Zenodo source)
  | 0, _ => by unfold lg; rw [ite_pos' _ _ rfl]
  | 1, _ => by
    have n1 := wk_ne_one n w hw1 hw7 1 1 0 rfl
    unfold lg
    rw [ite_neg' _ _ (wpow_ne n hpr w hw7 0 0 n1 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 1 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0),
      ite_pos' _ _ rfl]
  | 2, _ => by
    have n1 := wk_ne_one n w hw1 hw7 1 1 0 rfl
    have n2 := wk_ne_one n w hw1 hw7 2 4 1 rfl
    unfold lg
    rw [ite_neg' _ _ (wpow_ne n hpr w hw7 0 1 n2 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 2 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0),
      ite_neg' _ _ (wpow_ne n hpr w hw7 1 0 n1 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 2 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 1),
      ite_pos' _ _ rfl]
  | 3, _ => by
    have n1 := wk_ne_one n w hw1 hw7 1 1 0 rfl
    have n2 := wk_ne_one n w hw1 hw7 2 4 1 rfl
    have n3 := wk_ne_one n w hw1 hw7 3 5 2 rfl
    unfold lg
    rw [ite_neg' _ _ (wpow_ne n hpr w hw7 0 2 n3 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 3 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0),
      ite_neg' _ _ (wpow_ne n hpr w hw7 1 1 n2 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 3 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 1),
      ite_neg' _ _ (wpow_ne n hpr w hw7 2 0 n1 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 3 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 2),
      ite_pos' _ _ rfl]
  | 4, _ => by
    have n1 := wk_ne_one n w hw1 hw7 1 1 0 rfl
    have n2 := wk_ne_one n w hw1 hw7 2 4 1 rfl
    have n3 := wk_ne_one n w hw1 hw7 3 5 2 rfl
    have n4 := wk_ne_one n w hw1 hw7 4 2 1 rfl
    unfold lg
    rw [ite_neg' _ _ (wpow_ne n hpr w hw7 0 3 n4 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 4 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0),
      ite_neg' _ _ (wpow_ne n hpr w hw7 1 2 n3 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 4 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 1),
      ite_neg' _ _ (wpow_ne n hpr w hw7 2 1 n2 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 4 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 2),
      ite_neg' _ _ (wpow_ne n hpr w hw7 3 0 n1 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 4 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 3),
      ite_pos' _ _ rfl]
  | 5, _ => by
    have n1 := wk_ne_one n w hw1 hw7 1 1 0 rfl
    have n2 := wk_ne_one n w hw1 hw7 2 4 1 rfl
    have n3 := wk_ne_one n w hw1 hw7 3 5 2 rfl
    have n4 := wk_ne_one n w hw1 hw7 4 2 1 rfl
    have n5 := wk_ne_one n w hw1 hw7 5 3 2 rfl
    unfold lg
    rw [ite_neg' _ _ (wpow_ne n hpr w hw7 0 4 n5 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 5 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0),
      ite_neg' _ _ (wpow_ne n hpr w hw7 1 3 n4 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 5 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 1),
      ite_neg' _ _ (wpow_ne n hpr w hw7 2 2 n3 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 5 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 2),
      ite_neg' _ _ (wpow_ne n hpr w hw7 3 1 n2 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 5 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 3),
      ite_neg' _ _ (wpow_ne n hpr w hw7 4 0 n1 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 5 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 4),
      ite_pos' _ _ rfl]
  | 6, _ => by
    have n1 := wk_ne_one n w hw1 hw7 1 1 0 rfl
    have n2 := wk_ne_one n w hw1 hw7 2 4 1 rfl
    have n3 := wk_ne_one n w hw1 hw7 3 5 2 rfl
    have n4 := wk_ne_one n w hw1 hw7 4 2 1 rfl
    have n5 := wk_ne_one n w hw1 hw7 5 3 2 rfl
    have n6 := wk_ne_one n w hw1 hw7 6 6 5 rfl
    unfold lg
    rw [ite_neg' _ _ (wpow_ne n hpr w hw7 0 5 n6 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 6 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0),
      ite_neg' _ _ (wpow_ne n hpr w hw7 1 4 n5 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 6 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 1),
      ite_neg' _ _ (wpow_ne n hpr w hw7 2 3 n4 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 6 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 2),
      ite_neg' _ _ (wpow_ne n hpr w hw7 3 2 n3 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 6 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 3),
      ite_neg' _ _ (wpow_ne n hpr w hw7 4 1 n2 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 6 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 4),
      ite_neg' _ _ (wpow_ne n hpr w hw7 5 0 n1 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 6 ≠ pw (ring (2 * (7 * n + 6) + 1 + 1)) w 5)]
  | k + 7, hk => absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))

theorem xN7 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (x : Zp (2 * (7 * n + 6) + 1 + 1)) (hx : x ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero) : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6  -- (+430 chars; full line in the Zenodo source)
  have e : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (pw (ring (2 * (7 * n + 6) + 1 + 1)) x (2 * n + 2)) (pw (ring (2 * (7 * n + 6) + 1 + 1)) x (2 * n + 2))) (pw (ring (2 * (7 * n + 6) + 1 + 1)) x (2 * n + 2))) (pw  -- (+306 chars; full line in the Zenodo source)
    show _ = (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one _) _) _) _) _) _) _; rw [(ring (2 * (7 * n + 6) + 1 + 1)).one_mul]
  rw [e, TwoPower.pwpow, show (2 * n + 2) * 7 = 2 * (7 * n + 6) + 1 + 1 by lib_nat]
  exact unit_pow (2 * (7 * n + 6) + 1) hpr x hx

theorem exists_i (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+282 chars; full line in the Zenodo source)
  have p1 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 1 = w := (ring (2 * (7 * n + 6) + 1 + 1)).one_mul w
  have p2 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 2 = ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) := by show (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one w) w = _; rw [(ring (2 * (7 * n + 6) + 1 + 1)).one_mul]
  have p3 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 3 = ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) := by show (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one w) w) w = _; rw [(ring (2 * (7 * n + 6) + 1 + 1)).one_mul]
  have p4 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 4 = ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) w) := by show (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one w) w) w) w = _; rw [(r  -- (+39 chars; full line in the Zenodo source)
  have p5 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 5 = ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) w) w) := by show (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n  -- (+121 chars; full line in the Zenodo source)
  have p6 : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 6 = ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) w) w) w) := by show (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+203 chars; full line in the Zenodo source)
  have hc := Sept1.seven_cases n hpr w hw1 hw7 _ (xN7 n hpr x hx)
  rcases hc with h | h | h | h | h | h | h
  · exact ⟨0, (show 0 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_right (0 + 1) 6)), h⟩
  · exact ⟨1, (show 1 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_right (1 + 1) 5)), by rw [h, p1]⟩
  · exact ⟨2, (show 2 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_right (2 + 1) 4)), by rw [h, p2]⟩
  · exact ⟨3, (show 3 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right (3 + 1) 3)), by rw [h, p3]⟩
  · exact ⟨4, (show 4 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 4) (Nat.le_add_right (4 + 1) 2)), by rw [h, p4]⟩
  · exact ⟨5, (show 5 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 5) (Nat.le_add_right (5 + 1) 1)), by rw [h, p5]⟩
  · exact ⟨6, (show 6 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 6) (Nat.le_add_right (6 + 1) 0)), by rw [h, p6]⟩

theorem val_of (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+290 chars; full line in the Zenodo source)
    chi7 n w x = pw (Cyclo7.cyc7 LibInt.intR) zeta i := by
  unfold chi7
  rw [ite_neg' _ _ hx, h, lg_val n hpr w hw1 hw7 i hi]

theorem reduce (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) w) w) w) w) = (ring (2 * (7 * n + 6) + 1 + 1)).one) : ∀ k, ∃ k', k' < 7 ∧ pw (ring (2 * (7 * n + 6) + 1 + 1  -- (+126 chars; full line in the Zenodo source)
  | 0 => ⟨0, (show 0 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_right (0 + 1) 6)), rfl, rfl⟩
  | k + 1 => by
    match reduce w hw7 k with
    | ⟨k', hk', e1, e2⟩ =>
      match k', hk' with
      | 0, _ => exact ⟨1, (show 1 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_right (1 + 1) 5)), by show (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w = (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w; rw [e1], by show (Cyclo7.cyc7 LibInt.intR).mul _ zeta = (Cyclo7.cyc7 LibInt.intR).mul _ zeta; rw [e2]⟩
      | 1, _ => exact ⟨2, (show 2 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_right (2 + 1) 4)), by show (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w = (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w; rw [e1], by show (Cyclo7.cyc7 LibInt.intR).mul _ zeta = (Cyclo7.cyc7 LibInt.intR).mul _ zeta; rw [e2]⟩
      | 2, _ => exact ⟨3, (show 3 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right (3 + 1) 3)), by show (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w = (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w; rw [e1], by show (Cyclo7.cyc7 LibInt.intR).mul _ zeta = (Cyclo7.cyc7 LibInt.intR).mul _ zeta; rw [e2]⟩
      | 3, _ => exact ⟨4, (show 4 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 4) (Nat.le_add_right (4 + 1) 2)), by show (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w = (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w; rw [e1], by show (Cyclo7.cyc7 LibInt.intR).mul _ zeta = (Cyclo7.cyc7 LibInt.intR).mul _ zeta; rw [e2]⟩
      | 4, _ => exact ⟨5, (show 5 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 5) (Nat.le_add_right (5 + 1) 1)), by show (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w = (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w; rw [e1], by show (Cyclo7.cyc7 LibInt.intR).mul _ zeta = (Cyclo7.cyc7 LibInt.intR).mul _ zeta; rw [e2]⟩
      | 5, _ => exact ⟨6, (show 6 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 6) (Nat.le_add_right (6 + 1) 0)), by show (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w = (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w; rw [e1], by show (Cyclo7.cyc7 LibInt.intR).mul _ zeta = (Cyclo7.cyc7 LibInt.intR).mul _ zeta; rw [e2]⟩
      | 6, _ => exact ⟨0, (show 0 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_right (0 + 1) 6)), by show (ring (2 * (7 * n + 6) + 1 + 1)).mul _ w = _; rw [e1]; exact w7 n w hw7, by show (Cyclo7.cyc7 LibInt.intR).mul _ zeta = _; rw [e2]; exact z7⟩
      | j + 7, hj => exact absurd hj (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 j) h))

theorem chi7_zero (w : Zp (2 * (7 * n + 6) + 1 + 1)) : chi7 n w (ring (2 * (7 * n + 6) + 1 + 1)).zero = (Cyclo7.cyc7 LibInt.intR).zero := by unfold chi7; rw [ite_pos' _ _ rfl]

/-- **χ(xy) = χ(x)χ(y).** -/
theorem chi7_mul (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+240 chars; full line in the Zenodo source)
  by_cases hx : x = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hx, zero_mul', chi7_zero]; exact (zero_mul' (Cyclo7.cyc7 LibInt.intR) _).symm
  by_cases hy : y = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hy, mul_zero, chi7_zero]; exact (mul_zero (Cyclo7.cyc7 LibInt.intR) _).symm
  have hxy : (ring (2 * (7 * n + 6) + 1 + 1)).mul x y ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := fun e => by
    cases G4Gen.mul_eq_zero (7 * n + 6) hpr _ _ e with
    | inl h => exact hx h
    | inr h => exact hy h
  match exists_i n hpr w hw1 hw7 x hx, exists_i n hpr w hw1 hw7 y hy with
  | ⟨i, hi, ei⟩, ⟨j, hj, ej⟩ =>
    match reduce n w hw7 (i + j) with
    | ⟨k, hk, e1, e2⟩ =>
      have hxyN : pw (ring (2 * (7 * n + 6) + 1 + 1)) ((ring (2 * (7 * n + 6) + 1 + 1)).mul x y) (2 * n + 2) = pw (ring (2 * (7 * n + 6) + 1 + 1)) w k := by
        rw [pw_mul, ei, ej, ← HasseCM.pw_add', e1]
      rw [val_of n hpr w hw1 hw7 _ hxy k hk hxyN, val_of n hpr w hw1 hw7 x hx i hi ei, val_of n hpr w hw1 hw7 y hy j hj ej, ← e2, HasseCM.pw_add']

/-- **χ(−1) = 1.** -/
theorem chi7_neg1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *  -- (+211 chars; full line in the Zenodo source)
  have hne : (ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * (7 * n + 6) + 1) (by
    calc (ring (2 * (7 * n + 6) + 1 + 1)).one = (ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) := (neg_neg' (ring (2 * (7 * n + 6) + 1 + 1)) _).symm
      _ = (ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).zero := by rw [e]
      _ = (ring (2 * (7 * n + 6) + 1 + 1)).zero := by lib_ring)
  have h : pw (ring (2 * (7 * n + 6) + 1 + 1)) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) (2 * n + 2) = pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0 := by
    rw [show 2 * n + 2 = 2 * (n + 1) by lib_nat, ← TwoPower.pwpow]
    rw [show pw (ring (2 * (7 * n + 6) + 1 + 1)) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) 2 = (ring (2 * (7 * n + 6) + 1 + 1)).one by show (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one _) _ = _; lib_ring]
    exact CubicChar.pw_one'' (ring (2 * (7 * n + 6) + 1 + 1)) (n + 1)
  exact val_of n hpr w hw1 hw7 _ hne 0 (show 0 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_right (0 + 1) 6)) h

theorem int7 (d : Int) (h : 7 * d = 0) : d = 0 := by
  cases LibInt.int_noZD _ _ h with
  | inl h5 => exact absurd (Int.ofNat.inj (h5 : Int.ofNat 7 = Int.ofNat 0)) (fun e => Nat.noConfusion e)
  | inr h' => exact h'

/-- **ζS = S ⇒ S = 0** in ℤ[ζ₇]. -/
theorem zeta_fix7 : ∀ S : Int × Int × Int × Int × Int × Int, (Cyclo7.cyc7 LibInt.intR).mul zeta S = S → S = (Cyclo7.cyc7 LibInt.intR).zero
  | (a0, a1, a2, a3, a4, a5), h => by

    have h0 : ((Cyclo7.cyc7 LibInt.intR).mul zeta (a0, a1, a2, a3, a4, a5)).1 = a0 := congrArg (fun s => s.1) h
    change (((0 * a0) + ((((0 * a5) + (0 * a4)) + (0 * a3)) + (0 * a2))) + (-(((((1 * a5) + (0 * a4)) + (0 * a3)) + (0 * a2)) + (0 * a1)))) = a0 at h0
    have h1 : ((Cyclo7.cyc7 LibInt.intR).mul zeta (a0, a1, a2, a3, a4, a5)).2.1 = a1 := congrArg (fun s => s.2.1) h
    change ((((0 * a1) + (1 * a0)) + (((0 * a5) + (0 * a4)) + (0 * a3))) + (-(((((1 * a5) + (0 * a4)) + (0 * a3)) + (0 * a2)) + (0 * a1)))) = a1 at h1
    have h2 : ((Cyclo7.cyc7 LibInt.intR).mul zeta (a0, a1, a2, a3, a4, a5)).2.2.1 = a2 := congrArg (fun s => s.2.2.1) h
    change (((((0 * a2) + (1 * a1)) + (0 * a0)) + ((0 * a5) + (0 * a4))) + (-(((((1 * a5) + (0 * a4)) + (0 * a3)) + (0 * a2)) + (0 * a1)))) = a2 at h2
    have h3 : ((Cyclo7.cyc7 LibInt.intR).mul zeta (a0, a1, a2, a3, a4, a5)).2.2.2.1 = a3 := congrArg (fun s => s.2.2.2.1) h
    change ((((((0 * a3) + (1 * a2)) + (0 * a1)) + (0 * a0)) + (0 * a5)) + (-(((((1 * a5) + (0 * a4)) + (0 * a3)) + (0 * a2)) + (0 * a1)))) = a3 at h3
    have h4 : ((Cyclo7.cyc7 LibInt.intR).mul zeta (a0, a1, a2, a3, a4, a5)).2.2.2.2.1 = a4 := congrArg (fun s => s.2.2.2.2.1) h
    change ((((((0 * a4) + (1 * a3)) + (0 * a2)) + (0 * a1)) + (0 * a0)) + (-(((((1 * a5) + (0 * a4)) + (0 * a3)) + (0 * a2)) + (0 * a1)))) = a4 at h4
    have h5 : ((Cyclo7.cyc7 LibInt.intR).mul zeta (a0, a1, a2, a3, a4, a5)).2.2.2.2.2 = a5 := congrArg (fun s => s.2.2.2.2.2) h
    change (((((((0 * a5) + (1 * a4)) + (0 * a3)) + (0 * a2)) + (0 * a1)) + (0 * a0)) + (-(((((1 * a5) + (0 * a4)) + (0 * a3)) + (0 * a2)) + (0 * a1)))) = a5 at h5
    have f0 : a0 + a5 = 0 := by rw [← h0]; lib_int
    have f1 : a0 + -a5 = a1 := by rw [← h1]; lib_int
    have f2 : a1 + -a5 = a2 := by rw [← h2]; lib_int
    have f3 : a2 + -a5 = a3 := by rw [← h3]; lib_int
    have f4 : a3 + -a5 = a4 := by rw [← h4]; lib_int
    have f5 : a4 + -a5 = a5 := (show a4 + -a5 = (((((((0 * a5) + (1 * a4)) + (0 * a3)) + (0 * a2)) + (0 * a1)) + (0 * a0)) + (-(((((1 * a5) + (0 * a4)) + (0 * a3)) + (0 * a2)) + (0 * a1)))) by lib_int).trans h5
    have h7 : 7 * a5 = 0 := by
      rw [show (7 : Int) * a5 = (a5 + -(a4 + -a5)) + (a4 + -(a3 + -a5)) + (a3 + -(a2 + -a5)) + (a2 + -(a1 + -a5)) + (a1 + -(a0 + -a5)) + (a0 + a5) by lib_int,
        f5, f4, f3, f2, f1, f0]; lib_int
    have hd : a5 = 0 := int7 a5 h7
    have ha0 : a0 = 0 := by
      calc a0 = (a0 + a5) + -a5 := by lib_int
        _ = 0 := by rw [f0, hd]; lib_int
    have ha1 : a1 = 0 := by
      calc a1 = a0 + -a5 := f1.symm
        _ = 0 := by rw [ha0, hd]; lib_int
    have ha2 : a2 = 0 := by
      calc a2 = a1 + -a5 := f2.symm
        _ = 0 := by rw [ha1, hd]; lib_int
    have ha3 : a3 = 0 := by
      calc a3 = a2 + -a5 := f3.symm
        _ = 0 := by rw [ha2, hd]; lib_int
    have ha4 : a4 = 0 := by
      calc a4 = a3 + -a5 := f4.symm
        _ = 0 := by rw [ha3, hd]; lib_int
    rw [ha0, ha1, ha2, ha3, ha4, hd]; rfl

theorem iter (u S : Int × Int × Int × Int × Int × Int) (h : (Cyclo7.cyc7 LibInt.intR).mul u S = S) : ∀ k, (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) u k) S = S
  | 0 => (Cyclo7.cyc7 LibInt.intR).one_mul S
  | k + 1 => by
    show (Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) u k) u) S = S
    rw [(Cyclo7.cyc7 LibInt.intR).mul_assoc, h, iter u S h k]

/-- **Σ_x χ(x) = 0.** -/
theorem chi7_sum (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+241 chars; full line in the Zenodo source)
  match PowerSum.exists_nonroot (2 * (7 * n + 6) + 1) hpr (2 * n + 1) (by rw [show 2 * (7 * n + 6) + 1 = (2 * n + 1 + 1) + (12 * n + 11) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := reindex (Cyclo7.cyc7 LibInt.intR) _ (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR)) ((ring (2 * (7 * n + 6) + 1 + 1)).mul x0) ((ring (2 * (7 * n + 6) + 1 + 1)).mul (pw (ring (2 * (7 * n + 6) + 1 + 1)) x0 (2 * (7 * n + 6) + 1)))
      (fun x => by rw [← (ring (2 * (7 * n + 6) + 1 + 1)).mul_assoc, inv_exists (2 * (7 * n + 6) + 1) hpr x0 hx0, (ring (2 * (7 * n + 6) + 1 + 1)).one_mul])
      (fun y => by rw [← (ring (2 * (7 * n + 6) + 1 + 1)).mul_assoc, (ring (2 * (7 * n + 6) + 1 + 1)).mul_comm x0, inv_exists (2 * (7 * n + 6) + 1) hpr x0 hx0, (ring (2 * (7 * n + 6) + 1 + 1)).one_mul]) (chi7 n w)
    have e : sumK (Cyclo7.cyc7 LibInt.intR) (fun x => chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).mul x0 x)) (CMInert.allZ (2 * (7 * n + 6) + 1 + 1) ((2 * (7 * n + 6) + 1 + 1) + 1))
        = (Cyclo7.cyc7 LibInt.intR).mul (chi7 n w x0) (sumK (Cyclo7.cyc7 LibInt.intR) (chi7 n w) (CMInert.allZ (2 * (7 * n + 6) + 1 + 1) ((2 * (7 * n + 6) + 1 + 1) + 1))) := by
      rw [mul_sumK]
      exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun x => chi7_mul n hpr w hw1 hw7 x0 x) _
    rw [e] at hre
    generalize sumK (Cyclo7.cyc7 LibInt.intR) (chi7 n w) (CMInert.allZ (2 * (7 * n + 6) + 1 + 1) ((2 * (7 * n + 6) + 1 + 1) + 1)) = S at hre ⊢
    match exists_i n hpr w hw1 hw7 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      rw [val_of n hpr w hw1 hw7 x0 hx0 i hi ei] at hre
      match i, hi with
      | 0, _ => exact absurd ei hn
      | 1, _ =>
        have h := iter _ S hre 1
        rw [TwoPower.pwpow, show pw (Cyclo7.cyc7 LibInt.intR) zeta (1 * 1) = zeta from (Cyclo7.cyc7 LibInt.intR).one_mul zeta] at h
        exact zeta_fix7 S h
      | 2, _ =>
        have h := iter _ S hre 4
        rw [TwoPower.pwpow, show pw (Cyclo7.cyc7 LibInt.intR) zeta (2 * 4) = zeta from (zt_mod7 1 1).trans ((Cyclo7.cyc7 LibInt.intR).one_mul zeta)] at h
        exact zeta_fix7 S h
      | 3, _ =>
        have h := iter _ S hre 5
        rw [TwoPower.pwpow, show pw (Cyclo7.cyc7 LibInt.intR) zeta (3 * 5) = zeta from (zt_mod7 2 1).trans ((Cyclo7.cyc7 LibInt.intR).one_mul zeta)] at h
        exact zeta_fix7 S h
      | 4, _ =>
        have h := iter _ S hre 2
        rw [TwoPower.pwpow, show pw (Cyclo7.cyc7 LibInt.intR) zeta (4 * 2) = zeta from (zt_mod7 1 1).trans ((Cyclo7.cyc7 LibInt.intR).one_mul zeta)] at h
        exact zeta_fix7 S h
      | 5, _ =>
        have h := iter _ S hre 3
        rw [TwoPower.pwpow, show pw (Cyclo7.cyc7 LibInt.intR) zeta (5 * 3) = zeta from (zt_mod7 2 1).trans ((Cyclo7.cyc7 LibInt.intR).one_mul zeta)] at h
        exact zeta_fix7 S h
      | 6, _ =>
        have h := iter _ S hre 6
        rw [TwoPower.pwpow, show pw (Cyclo7.cyc7 LibInt.intR) zeta (6 * 6) = zeta from (zt_mod7 5 1).trans ((Cyclo7.cyc7 LibInt.intR).one_mul zeta)] at h
        exact zeta_fix7 S h
      | k + 7, hk => exact absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))

end Sept2

#print axioms Sept2.zeta
#print axioms Sept2.z7
#print axioms Sept2.zt_mod7
#print axioms Sept2.w7
#print axioms Sept2.wfix
#print axioms Sept2.wk_ne_one
#print axioms Sept2.w_ne_zero
#print axioms Sept2.pwnz
#print axioms Sept2.cancel1
#print axioms Sept2.wpow_ne
#print axioms Sept2.lg_val
#print axioms Sept2.xN7
#print axioms Sept2.exists_i
#print axioms Sept2.val_of
#print axioms Sept2.reduce
#print axioms Sept2.chi7_zero
#print axioms Sept2.chi7_mul
#print axioms Sept2.chi7_neg1
#print axioms Sept2.int7
#print axioms Sept2.zeta_fix7
#print axioms Sept2.iter
#print axioms Sept2.chi7_sum

#print axioms: 22 theorems, no axioms.

78.197 The septic Jacobi sum has norm \(p\)

For the septic character \(\chi \) of \(\mathbb F_p\), \(J=\sum _t\chi (t)\chi (1-t)\in \mathbb Z[\zeta _7]\) satisfies

\[ J\,\overline {J}=p,\qquad \overline {\zeta }=\zeta ^6. \]

lang/Sept3.lean

/-
  lang/Sept3 — T49, cycle 773 (ℓ = 7, step 3): the septic Jacobi sum has norm p.

  p = 14n + 15, χ = Sept2.chi7 (values 0, ζ^i in ℤ[ζ₇]), ι(x) = x^(p−2), χ̄ = χ∘ι, ρ = χ², ρ̄ = ρ∘ι, conj ζ = ζ⁶ on ℤ[ζ₇].
  J = J(χ, χ) = Σ χ(t)χ(1 − t) (CharSumGen.Jp). In ℤ[ζ₇][C_p] (CharSumGen):
    g_χ ⋆ g_χ = J g_ρ,  g_χ̄ ⋆ g_χ̄ = J̄ g_ρ̄,  g_χ ⋆ g_χ̄ = X,  g_ρ ⋆ g_ρ̄ = X,  X ⋆ X = p X
  so J J̄ X = (g_χ g_χ̄)² = p X and, at ζ⁰, J · conj(J) = p:
    chi_iv          χ(x⁻¹) = conj χ(x)
    rho_sum, kill   Σ ρ = 0 (ζ^i S = S, 0 < i < 7 ⇒ S = 0)
    conv_X, E2gen   the two shapes of the convolutions
    conj7_mul       conj is multiplicative;  Jb_conj: J(χ̄, χ̄) = conj J(χ, χ)
    sept_norm      J · conj(J) = p in ℤ[ζ₇]
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Sept2
import lang.CharSumGen
import lang.JacobiNorm

namespace Sept3

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable (n : Nat)

def conj7 (z : Int × Int × Int × Int × Int × Int) : Int × Int × Int × Int × Int × Int := (z.1 + -z.2.1, -z.2.1, z.2.2.2.2.2 + -z.2.1, z.2.2.2.2.1 + -z.2.1, z.2.2.2.1 + -z.2.1, z.2.2.1 + -z.2.1)
def chib (w x : Zp (2 * (7 * n + 6) + 1 + 1)) : Int × Int × Int × Int × Int × Int := Sept2.chi7 n w (CharSumGen.iv (2 * (7 * n + 6)) x)
def rho (w x : Zp (2 * (7 * n + 6) + 1 + 1)) : Int × Int × Int × Int × Int × Int := (Cyclo7.cyc7 LibInt.intR).mul (Sept2.chi7 n w x) (Sept2.chi7 n w x)
def rhob (w x : Zp (2 * (7 * n + 6) + 1 + 1)) : Int × Int × Int × Int × Int × Int := rho n w (CharSumGen.iv (2 * (7 * n + 6)) x)

theorem chi_one (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+171 chars; full line in the Zenodo source)
  Sept2.val_of n hpr w hw1 hw7 (ring (2 * (7 * n + 6) + 1 + 1)).one (PowerSum.one_ne_zero' _) 0 (Nat.zero_lt_succ 6) (CubicChar.pw_one'' (ring (2 * (7 * n + 6) + 1 + 1)) _)

theorem neg1_ne : (ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * (7 * n + 6) + 1) (by
  calc (ring (2 * (7 * n + 6) + 1 + 1)).one = (ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) := (neg_neg' (ring (2 * (7 * n + 6) + 1 + 1)) _).symm
    _ = (ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).zero := by rw [e]
    _ = (ring (2 * (7 * n + 6) + 1 + 1)).zero := by lib_ring)

/-- χ(x⁻¹) = conj χ(x). -/
theorem chi_iv (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+202 chars; full line in the Zenodo source)
  by_cases hx : x = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hx, CharSumGen.iv_zero, Sept2.chi7_zero]; rfl
  have h1 : (Cyclo7.cyc7 LibInt.intR).mul (Sept2.chi7 n w (CharSumGen.iv (2 * (7 * n + 6)) x)) (Sept2.chi7 n w x) = (Cyclo7.cyc7 LibInt.intR).one := by
    rw [← Sept2.chi7_mul n hpr w hw1 hw7, CharSumGen.iv_mul (2 * (7 * n + 6)) hpr x hx, chi_one n hpr w hw1 hw7]
  have h2 : (Cyclo7.cyc7 LibInt.intR).mul (conj7 (Sept2.chi7 n w x)) (Sept2.chi7 n w x) = (Cyclo7.cyc7 LibInt.intR).one := by
    match Sept2.exists_i n hpr w hw1 hw7 x hx with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 x hx i hi ei]
      match i, hi with
      | 0, _ => rfl
      | 1, _ => rfl
      | 2, _ => rfl
      | 3, _ => rfl
      | 4, _ => rfl
      | 5, _ => rfl
      | 6, _ => rfl
      | k + 7, hk => exact absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))
  calc Sept2.chi7 n w (CharSumGen.iv (2 * (7 * n + 6)) x) = (Cyclo7.cyc7 LibInt.intR).mul (Sept2.chi7 n w (CharSumGen.iv (2 * (7 * n + 6)) x)) ((Cyclo7.cyc7 LibInt.intR).mul (conj7 (Sept2.chi7 n w x)) (Sept2.chi7 n w x)) := by rw [h2]; lib_ring
    _ = (Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (Sept2.chi7 n w (CharSumGen.iv (2 * (7 * n + 6)) x)) (Sept2.chi7 n w x)) (conj7 (Sept2.chi7 n w x)) := by lib_ring
    _ = conj7 (Sept2.chi7 n w x) := by rw [h1]; lib_ring

theorem rho_mul (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+234 chars; full line in the Zenodo source)
  unfold rho; rw [Sept2.chi7_mul n hpr w hw1 hw7]; lib_ring
theorem chib_mul (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+238 chars; full line in the Zenodo source)
  unfold chib; rw [CharSumGen.iv_mulmul, Sept2.chi7_mul n hpr w hw1 hw7]
theorem rhob_mul (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+238 chars; full line in the Zenodo source)
  unfold rhob; rw [CharSumGen.iv_mulmul, rho_mul n hpr w hw1 hw7]

/-- ζ^i S = S with 0 < i < 5 forces S = 0. -/
theorem kill (S : Int × Int × Int × Int × Int × Int) : ∀ i, i < 7 → i ≠ 0 → (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta i) S = S → S = (Cyclo7.cyc7 LibInt.intR).zero
  | 0, _, h0, _ => absurd rfl h0
  | 1, _, _, hre => by
    have h := Sept2.iter _ S hre 1
    rw [TwoPower.pwpow, show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (1 * 1) = Sept2.zeta from (Cyclo7.cyc7 LibInt.intR).one_mul Sept2.zeta] at h
    exact Sept2.zeta_fix7 S h
  | 2, _, _, hre => by
    have h := Sept2.iter _ S hre 4
    rw [TwoPower.pwpow, show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (2 * 4) = Sept2.zeta from (Sept2.zt_mod7 1 1).trans ((Cyclo7.cyc7 LibInt.intR).one_mul Sept2.zeta)] at h
    exact Sept2.zeta_fix7 S h
  | 3, _, _, hre => by
    have h := Sept2.iter _ S hre 5
    rw [TwoPower.pwpow, show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (3 * 5) = Sept2.zeta from (Sept2.zt_mod7 2 1).trans ((Cyclo7.cyc7 LibInt.intR).one_mul Sept2.zeta)] at h
    exact Sept2.zeta_fix7 S h
  | 4, _, _, hre => by
    have h := Sept2.iter _ S hre 2
    rw [TwoPower.pwpow, show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (4 * 2) = Sept2.zeta from (Sept2.zt_mod7 1 1).trans ((Cyclo7.cyc7 LibInt.intR).one_mul Sept2.zeta)] at h
    exact Sept2.zeta_fix7 S h
  | 5, _, _, hre => by
    have h := Sept2.iter _ S hre 3
    rw [TwoPower.pwpow, show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (5 * 3) = Sept2.zeta from (Sept2.zt_mod7 2 1).trans ((Cyclo7.cyc7 LibInt.intR).one_mul Sept2.zeta)] at h
    exact Sept2.zeta_fix7 S h
  | 6, _, _, hre => by
    have h := Sept2.iter _ S hre 6
    rw [TwoPower.pwpow, show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (6 * 6) = Sept2.zeta from (Sept2.zt_mod7 5 1).trans ((Cyclo7.cyc7 LibInt.intR).one_mul Sept2.zeta)] at h
    exact Sept2.zeta_fix7 S h
  | k + 7, hk, _, _ => absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))

theorem rho_sum (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+198 chars; full line in the Zenodo source)
  match PowerSum.exists_nonroot (2 * (7 * n + 6) + 1) hpr (2 * n + 1) (by rw [show 2 * (7 * n + 6) + 1 = (2 * n + 1 + 1) + (12 * n + 11) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := CharSumGen.sum_scale (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (rho n w) x0 hx0
    have e : sumK (Cyclo7.cyc7 LibInt.intR) (fun x => rho n w ((ring (2 * (7 * n + 6) + 1 + 1)).mul x0 x)) (CharSumGen.LL (2 * (7 * n + 6))) = (Cyclo7.cyc7 LibInt.intR).mul (rho n w x0) (sumK (Cyclo7.cyc7 LibInt.intR) (rho n w) (CharSumGen.LL (2 * (7 * n + 6)))) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun x => rho_mul n hpr w hw1 hw7 x0 x) _
    have hre' : (Cyclo7.cyc7 LibInt.intR).mul (rho n w x0) (sumK (Cyclo7.cyc7 LibInt.intR) (rho n w) (CharSumGen.LL (2 * (7 * n + 6)))) = sumK (Cyclo7.cyc7 LibInt.intR) (rho n w) (CharSumGen.LL (2 * (7 * n + 6))) := e.symm.trans hre
    generalize sumK (Cyclo7.cyc7 LibInt.intR) (rho n w) (CharSumGen.LL (2 * (7 * n + 6))) = S at hre' ⊢
    match Sept2.exists_i n hpr w hw1 hw7 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      have hr : rho n w x0 = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (i + i) := by
        unfold rho; rw [Sept2.val_of n hpr w hw1 hw7 x0 hx0 i hi ei, HasseCM.pw_add']
      rw [hr] at hre'
      match i, hi with
      | 0, _ => exact absurd ei hn
      | 1, _ =>
        exact kill S 2 (show 2 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_right (2 + 1) 4)) (fun e => Nat.noConfusion e) hre'
      | 2, _ =>
        exact kill S 4 (show 4 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 4) (Nat.le_add_right (4 + 1) 2)) (fun e => Nat.noConfusion e) hre'
      | 3, _ =>
        exact kill S 6 (show 6 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 6) (Nat.le_add_right (6 + 1) 0)) (fun e => Nat.noConfusion e) hre'
      | 4, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (4 + 4) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 1 from Sept2.zt_mod7 1 1] at hre'
        exact kill S 1 (show 1 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_right (1 + 1) 5)) (fun e => Nat.noConfusion e) hre'
      | 5, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (5 + 5) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 3 from Sept2.zt_mod7 1 3] at hre'
        exact kill S 3 (show 3 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right (3 + 1) 3)) (fun e => Nat.noConfusion e) hre'
      | 6, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (6 + 6) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 5 from Sept2.zt_mod7 1 5] at hre'
        exact kill S 5 (show 5 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 5) (Nat.le_add_right (5 + 1) 1)) (fun e => Nat.noConfusion e) hre'
      | k + 7, hk => exact absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))

theorem chib_sum (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+197 chars; full line in the Zenodo source)
  (reindex (Cyclo7.cyc7 LibInt.intR) _ (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR)) (CharSumGen.iv (2 * (7 * n + 6))) (CharSumGen.iv (2 * (7 * n + 6))) (CharSumGen.iv_iv (2 * (7 * n + 6)) hpr) (CharSumGen.iv_iv (2 * (7 * n + 6)) hpr) (Sept2.chi7 n w)).trans (Sept2.chi7_sum n hpr w hw1 hw7)
theorem rhob_sum (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+197 chars; full line in the Zenodo source)
  (reindex (Cyclo7.cyc7 LibInt.intR) _ (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR)) (CharSumGen.iv (2 * (7 * n + 6))) (CharSumGen.iv (2 * (7 * n + 6))) (CharSumGen.iv_iv (2 * (7 * n + 6)) hpr) (CharSumGen.iv_iv (2 * (7 * n + 6)) hpr) (rho n w)).trans (rho_sum n hpr w hw1 hw7)

theorem chib_one (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+169 chars; full line in the Zenodo source)
  unfold chib; rw [show CharSumGen.iv (2 * (7 * n + 6)) (ring (2 * (7 * n + 6) + 1 + 1)).one = (ring (2 * (7 * n + 6) + 1 + 1)).one from CubicChar.pw_one'' (ring (2 * (7 * n + 6) + 1 + 1)) _, chi_one n hpr w hw1 hw7]
theorem chib_zero (w : Zp (2 * (7 * n + 6) + 1 + 1)) : chib n w (ring (2 * (7 * n + 6) + 1 + 1)).zero = (Cyclo7.cyc7 LibInt.intR).zero := by unfold chib; rw [CharSumGen.iv_zero, Sept2.chi7_zero]
theorem rho_one (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+218 chars; full line in the Zenodo source)
theorem rho_zero (w : Zp (2 * (7 * n + 6) + 1 + 1)) : rho n w (ring (2 * (7 * n + 6) + 1 + 1)).zero = (Cyclo7.cyc7 LibInt.intR).zero := by unfold rho; rw [Sept2.chi7_zero]; lib_ring
theorem rhob_one (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+169 chars; full line in the Zenodo source)
  unfold rhob; rw [show CharSumGen.iv (2 * (7 * n + 6)) (ring (2 * (7 * n + 6) + 1 + 1)).one = (ring (2 * (7 * n + 6) + 1 + 1)).one from CubicChar.pw_one'' (ring (2 * (7 * n + 6) + 1 + 1)) _, rho_one n hpr w hw1 hw7]
theorem rhob_zero (w : Zp (2 * (7 * n + 6) + 1 + 1)) : rhob n w (ring (2 * (7 * n + 6) + 1 + 1)).zero = (Cyclo7.cyc7 LibInt.intR).zero := by unfold rhob; rw [CharSumGen.iv_zero, rho_zero]

theorem chib_neg1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+209 chars; full line in the Zenodo source)
  calc chib n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) = (Cyclo7.cyc7 LibInt.intR).mul (chib n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) (Sept2.chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) := by rw [Sept2.chi7_neg1 n hpr w hw1 hw7]; lib_ring
    _ = Sept2.chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).mul (CharSumGen.iv (2 * (7 * n + 6)) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) := by unfold chib; rw [Sept2.chi7_mul n hpr w hw1 hw7]
    _ = (Cyclo7.cyc7 LibInt.intR).one := by rw [CharSumGen.iv_mul (2 * (7 * n + 6)) hpr _ (neg1_ne n), chi_one n hpr w hw1 hw7]
theorem rhob_neg1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+209 chars; full line in the Zenodo source)
  show (Cyclo7.cyc7 LibInt.intR).mul (chib n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) (chib n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = (Cyclo7.cyc7 LibInt.intR).one
  rw [chib_neg1 n hpr w hw1 hw7]; lib_ring

theorem gt_sq (ψ : Zp (2 * (7 * n + 6) + 1 + 1) → Int × Int × Int × Int × Int × Int) (c : Zp (2 * (7 * n + 6) + 1 + 1)) :
    gt (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (gmul (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (CharSumGen.Gv (2 * (7 * n + 6)) (ψ)) (CharSumGen.Gv (2 * (7 * n + 6)) (ψ))) c = sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (ψ a) (ψ ((ring (2 * (7 * n + 6) + 1 + 1)).add c ((ring (2 * (7 * n + 6) + 1 + 1)).neg a)))) (CharSumGen.LL (2 * (7 * n + 6))) := b  -- (+1 chars; full line in the Zenodo source)
  rw [gt_mul]; exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun a => by unfold CharSumGen.Gv; rw [gt_tb, gt_tb]) _

/-- the X-shape of ψ ⋆ ψ' when ψ'(b) = ψ(b⁻¹). -/
theorem conv_X (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (ψ ψ' : Zp (2 * (7 * n + 6) + 1 + 1) → Int × Int × Int × Int × Int × Int) (hm : ∀ x y, ψ ((ring (2 * (7 * n + 6) + 1 + 1)).mul x y) = (Cyclo7.cyc7 LibInt.intR).mul (ψ x) (ψ y)) (hm' : ∀ x y, ψ' ((ring (2 * (7 * n + 6) + 1 + 1)).mul x y) = (Cyclo7.cyc7 LibInt.intR).mul (ψ' x) (ψ' y))
    (h1 : ψ (ring (2 * (7 * n + 6) + 1 + 1)).one = (Cyclo7.cyc7 LibInt.intR).one) (h0 : ψ (ring (2 * (7 * n + 6) + 1 + 1)).zero = (Cyclo7.cyc7 LibInt.intR).zero) (hs : sumK (Cyclo7.cyc7 LibInt.intR) ψ (CharSumGen.LL (2 * (7 * n + 6))) = (Cyclo7.cyc7 LibInt.intR).zero) (hiv : ∀ b, ψ' b = ψ (CharSumGen.iv (2 * (7 * n + 6)) b)) :
    gmul (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (CharSumGen.Gv (2 * (7 * n + 6)) (ψ)) (CharSumGen.Gv (2 * (7 * n + 6)) (ψ')) = gmul (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (ψ' ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one))) (CharSumGen.Xv (Cyclo7.cy  -- (+37 chars; full line in the Zenodo source)
  ext (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) _ _ (fun c => by
    rw [CharSumGen.conv_D (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ψ ψ' hm' c, JacobiNorm.mono0_mulg, CharSumGen.gt_X]
    rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun b => by rw [hiv b])]
    by_cases hc : c = (ring (2 * (7 * n + 6) + 1 + 1)).zero
    · rw [hc]
      rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun b => by rw [(ring (2 * (7 * n + 6) + 1 + 1)).zero_add]), CharSumGen.D_zero (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr ψ hm h1 h0]
      unfold delta; rw [ite_pos' _ _ rfl]; lib_ring
    · rw [CharSumGen.D_ne (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr ψ hm h1 h0 hs c hc]
      unfold delta; rw [ite_neg' _ _ hc]; lib_ring)

theorem E2gen (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (ψ : Zp (2 * (7 * n + 6) + 1 + 1) → Int × Int × Int × Int × Int × Int) (hm : ∀ x y, ψ ((ring (2 * (7 * n + 6) + 1 + 1)).mul x y) = (Cyclo7.cyc7 LibInt.intR).mul (ψ x) (ψ y)) (r : Zp (2 * (7 * n + 6) + 1 + 1) → Int × Int × Int × Int × Int × Int)
    (hr : ∀ c, (Cyclo7.cyc7 LibInt.intR).mul (ψ c) (ψ c) = r c) (hr0 : r (ring (2 * (7 * n + 6) + 1 + 1)).zero = (Cyclo7.cyc7 LibInt.intR).zero) (hsr : sumK (Cyclo7.cyc7 LibInt.intR) r (CharSumGen.LL (2 * (7 * n + 6))) = (Cyclo7.cyc7 LibInt.intR).zero) :
    gmul (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (CharSumGen.Gv (2 * (7 * n + 6)) (ψ)) (CharSumGen.Gv (2 * (7 * n + 6)) (ψ)) = gmul (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (ψ)))) (CharSumGen.Gv (2 * (7 * n + 6)) (r)) :=
  ext (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) _ _ (fun c => by
    rw [gt_sq, JacobiNorm.mono0_mulg]
    unfold CharSumGen.Gv; rw [gt_tb]
    by_cases hc : c = (ring (2 * (7 * n + 6) + 1 + 1)).zero
    · rw [hc, CharSumGen.S_zero (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ψ hm, hr0, sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ hr, hsr]; lib_ring
    · rw [CharSumGen.S_ne (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr ψ hm c hc, hr]; lib_ring)

theorem E1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n   -- (+542 chars; full line in the Zenodo source)
  conv_X n hpr _ _ (Sept2.chi7_mul n hpr w hw1 hw7) (chib_mul n hpr w hw1 hw7) (chi_one n hpr w hw1 hw7) (Sept2.chi7_zero n w) (Sept2.chi7_sum n hpr w hw1 hw7) (fun _ => rfl)

theorem E4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n   -- (+483 chars; full line in the Zenodo source)
  rw [conv_X n hpr _ _ (rho_mul n hpr w hw1 hw7) (rhob_mul n hpr w hw1 hw7) (rho_one n hpr w hw1 hw7) (rho_zero n w) (rho_sum n hpr w hw1 hw7) (fun _ => rfl), rhob_neg1 n hpr w hw1 hw7]

theorem conj7_mul : ∀ x y : Int × Int × Int × Int × Int × Int, conj7 ((Cyclo7.cyc7 LibInt.intR).mul x y) = (Cyclo7.cyc7 LibInt.intR).mul (conj7 x) (conj7 y)
  | (a0, a1, a2, a3, a4, a5), (b0, b1, b2, b3, b4, b5) => by
    apply Prod.ext
    · show ((((a0 * b0) + ((((a2 * b5) + (a3 * b4)) + (a4 * b3)) + (a5 * b2))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-((((a0 * b1) + (a1 * b0)) + (((a3 * b5) + (a4 * b4)) + (a5 * b3))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = ((((a0 + (-a1)) * (b0 + (-b1))) + (((((a5 + (-a1)) * (b2 + (-b1))) + ((a4 + (-a1)) * (b3 + (-b1)))) +   -- (+222 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show (-((((a0 * b1) + (a1 * b0)) + (((a3 * b5) + (a4 * b4)) + (a5 * b3))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1))))) = (((((a0 + (-a1)) * (-b1)) + ((-a1) * (b0 + (-b1)))) + ((((a4 + (-a1)) * (b2 + (-b1))) + ((a3 + (-a1)) * (b3 + (-b1)))) + ((a2 + (-a1)) * (b4 + (-b1))))) + (-((((((-a1) * (b2 + (-b1))) + ((a5 + (-a1)) * (b3 + (-b1)))) + ((a4 + (-a1)) * (b4 + (-b1  -- (+65 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show ((((((((a0 * b5) + (a1 * b4)) + (a2 * b3)) + (a3 * b2)) + (a4 * b1)) + (a5 * b0)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-((((a0 * b1) + (a1 * b0)) + (((a3 * b5) + (a4 * b4)) + (a5 * b3))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = ((((((a0 + (-a1)) * (b5 + (-b1))) + ((-a1) * (-b1))) + ((a5 + (-a1)) * (b0 + (-b1)))) +   -- (+222 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show (((((((a0 * b4) + (a1 * b3)) + (a2 * b2)) + (a3 * b1)) + (a4 * b0)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-((((a0 * b1) + (a1 * b0)) + (((a3 * b5) + (a4 * b4)) + (a5 * b3))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = (((((((a0 + (-a1)) * (b4 + (-b1))) + ((-a1) * (b5 + (-b1)))) + ((a5 + (-a1)) * (-b1))) + ((a4 + (-a1))  -- (+208 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show (((((((a0 * b3) + (a1 * b2)) + (a2 * b1)) + (a3 * b0)) + (a5 * b5)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-((((a0 * b1) + (a1 * b0)) + (((a3 * b5) + (a4 * b4)) + (a5 * b3))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = (((((((a0 + (-a1)) * (b3 + (-b1))) + ((-a1) * (b4 + (-b1)))) + ((a5 + (-a1)) * (b5 + (-b1)))) + ((a4 +  -- (+208 chars; full line in the Zenodo source)
      lib_int
    · show ((((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + ((a4 * b5) + (a5 * b4))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-((((a0 * b1) + (a1 * b0)) + (((a3 * b5) + (a4 * b4)) + (a5 * b3))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = ((((((((a0 + (-a1)) * (b2 + (-b1))) + ((-a1) * (b3 + (-b1)))) + ((a5 + (-a1)) * (b4 + (-b1)))) + ((a4   -- (+242 chars; full line in the Zenodo source)
      lib_int

theorem conj7_add : ∀ x y : Int × Int × Int × Int × Int × Int, conj7 ((Cyclo7.cyc7 LibInt.intR).add x y) = (Cyclo7.cyc7 LibInt.intR).add (conj7 x) (conj7 y)
  | (a0, a1, a2, a3, a4, a5), (b0, b1, b2, b3, b4, b5) => by
    apply Prod.ext
    · show ((a0 + b0) + (-(a1 + b1))) = ((a0 + (-a1)) + (b0 + (-b1)))
      lib_int
    apply Prod.ext
    · show (-(a1 + b1)) = ((-a1) + (-b1))
      lib_int
    apply Prod.ext
    · show ((a5 + b5) + (-(a1 + b1))) = ((a5 + (-a1)) + (b5 + (-b1)))
      lib_int
    apply Prod.ext
    · show ((a4 + b4) + (-(a1 + b1))) = ((a4 + (-a1)) + (b4 + (-b1)))
      lib_int
    apply Prod.ext
    · show ((a3 + b3) + (-(a1 + b1))) = ((a3 + (-a1)) + (b3 + (-b1)))
      lib_int
    · show ((a2 + b2) + (-(a1 + b1))) = ((a2 + (-a1)) + (b2 + (-b1)))
      lib_int

theorem conj7_sum {A' : Type} (f : A' → Int × Int × Int × Int × Int × Int) : ∀ l : List A', conj7 (sumK (Cyclo7.cyc7 LibInt.intR) f l) = sumK (Cyclo7.cyc7 LibInt.intR) (fun a => conj7 (f a)) l
  | [] => rfl
  | a :: l => by
    show conj7 ((Cyclo7.cyc7 LibInt.intR).add (f a) (sumK (Cyclo7.cyc7 LibInt.intR) f l)) = (Cyclo7.cyc7 LibInt.intR).add (conj7 (f a)) (sumK (Cyclo7.cyc7 LibInt.intR) (fun a => conj7 (f a)) l)
    rw [conj7_add, conj7_sum f l]

theorem Jb_conj (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+250 chars; full line in the Zenodo source)
  unfold CharSumGen.Jp; rw [conj7_sum]
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => by unfold chib; rw [conj7_mul, chi_iv n hpr w hw1 hw7, chi_iv n hpr w hw1 hw7]) _

theorem sum_one7 : ∀ k, sumK (Cyclo7.cyc7 LibInt.intR) (fun _ : Zp (2 * (7 * n + 6) + 1 + 1) => (Cyclo7.cyc7 LibInt.intR).one) (CMInert.allZ (2 * (7 * n + 6) + 1 + 1) k) = (Int.ofNat k, 0, 0, 0, 0, 0)
  | 0 => rfl
  | k + 1 => by
    show sumK (Cyclo7.cyc7 LibInt.intR) (fun _ : Zp (2 * (7 * n + 6) + 1 + 1) => (Cyclo7.cyc7 LibInt.intR).one) (CMInert.allZ (2 * (7 * n + 6) + 1 + 1) k ++ [mk (2 * (7 * n + 6) + 1 + 1) k]) = _
    rw [sumK_append, sum_one7 k]
    rfl

theorem scal_cancel (k : Nat) : ∀ u v : Int × Int × Int × Int × Int × Int, (Cyclo7.cyc7 LibInt.intR).mul u (Int.ofNat (k + 1), 0, 0, 0, 0, 0) = (Cyclo7.cyc7 LibInt.intR).mul v (Int.ofNat (k + 1), 0, 0, 0, 0, 0) → u = v
  | (m0, m1, m2, m3, m4, m5), (r0, r1, r2, r3, r4, r5), h => by
    have h0 : ((Cyclo7.cyc7 LibInt.intR).mul (m0, m1, m2, m3, m4, m5) (Int.ofNat (k + 1), 0, 0, 0, 0, 0)).1 = ((Cyclo7.cyc7 LibInt.intR).mul (r0, r1, r2, r3, r4, r5) (Int.ofNat (k + 1), 0, 0, 0, 0, 0)).1 := congrArg (fun s => s.1) h
    change (((m0 * Int.ofNat (k + 1)) + ((((m2 * 0) + (m3 * 0)) + (m4 * 0)) + (m5 * 0))) + (-(((((m1 * 0) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * 0)))) = (((r0 * Int.ofNat (k + 1)) + ((((r2 * 0) + (r3 * 0)) + (r4 * 0)) + (r5 * 0))) + (-(((((r1 * 0) + (r2 * 0)) + (r3 * 0)) + (r4 * 0)) + (r5 * 0)))) at h0
    have e0 : m0 = r0 := JacobiNorm.int_cancel _ _ k (by rw [show m0 * Int.ofNat (k + 1) = (((m0 * Int.ofNat (k + 1)) + ((((m2 * 0) + (m3 * 0)) + (m4 * 0)) + (m5 * 0))) + (-(((((m1 * 0) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * 0)))) by lib_int, h0]; lib_int)
    have h1 : ((Cyclo7.cyc7 LibInt.intR).mul (m0, m1, m2, m3, m4, m5) (Int.ofNat (k + 1), 0, 0, 0, 0, 0)).2.1 = ((Cyclo7.cyc7 LibInt.intR).mul (r0, r1, r2, r3, r4, r5) (Int.ofNat (k + 1), 0, 0, 0, 0, 0)).2.1 := congrArg (fun s => s.2.1) h
    change ((((m0 * 0) + (m1 * Int.ofNat (k + 1))) + (((m3 * 0) + (m4 * 0)) + (m5 * 0))) + (-(((((m1 * 0) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * 0)))) = ((((r0 * 0) + (r1 * Int.ofNat (k + 1))) + (((r3 * 0) + (r4 * 0)) + (r5 * 0))) + (-(((((r1 * 0) + (r2 * 0)) + (r3 * 0)) + (r4 * 0)) + (r5 * 0)))) at h1
    have e1 : m1 = r1 := JacobiNorm.int_cancel _ _ k (by rw [show m1 * Int.ofNat (k + 1) = ((((m0 * 0) + (m1 * Int.ofNat (k + 1))) + (((m3 * 0) + (m4 * 0)) + (m5 * 0))) + (-(((((m1 * 0) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * 0)))) by lib_int, h1]; lib_int)
    have h2 : ((Cyclo7.cyc7 LibInt.intR).mul (m0, m1, m2, m3, m4, m5) (Int.ofNat (k + 1), 0, 0, 0, 0, 0)).2.2.1 = ((Cyclo7.cyc7 LibInt.intR).mul (r0, r1, r2, r3, r4, r5) (Int.ofNat (k + 1), 0, 0, 0, 0, 0)).2.2.1 := congrArg (fun s => s.2.2.1) h
    change (((((m0 * 0) + (m1 * 0)) + (m2 * Int.ofNat (k + 1))) + ((m4 * 0) + (m5 * 0))) + (-(((((m1 * 0) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * 0)))) = (((((r0 * 0) + (r1 * 0)) + (r2 * Int.ofNat (k + 1))) + ((r4 * 0) + (r5 * 0))) + (-(((((r1 * 0) + (r2 * 0)) + (r3 * 0)) + (r4 * 0)) + (r5 * 0)))) at h2
    have e2 : m2 = r2 := JacobiNorm.int_cancel _ _ k (by rw [show m2 * Int.ofNat (k + 1) = (((((m0 * 0) + (m1 * 0)) + (m2 * Int.ofNat (k + 1))) + ((m4 * 0) + (m5 * 0))) + (-(((((m1 * 0) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * 0)))) by lib_int, h2]; lib_int)
    have h3 : ((Cyclo7.cyc7 LibInt.intR).mul (m0, m1, m2, m3, m4, m5) (Int.ofNat (k + 1), 0, 0, 0, 0, 0)).2.2.2.1 = ((Cyclo7.cyc7 LibInt.intR).mul (r0, r1, r2, r3, r4, r5) (Int.ofNat (k + 1), 0, 0, 0, 0, 0)).2.2.2.1 := congrArg (fun s => s.2.2.2.1) h
    change ((((((m0 * 0) + (m1 * 0)) + (m2 * 0)) + (m3 * Int.ofNat (k + 1))) + (m5 * 0)) + (-(((((m1 * 0) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * 0)))) = ((((((r0 * 0) + (r1 * 0)) + (r2 * 0)) + (r3 * Int.ofNat (k + 1))) + (r5 * 0)) + (-(((((r1 * 0) + (r2 * 0)) + (r3 * 0)) + (r4 * 0)) + (r5 * 0)))) at h3
    have e3 : m3 = r3 := JacobiNorm.int_cancel _ _ k (by rw [show m3 * Int.ofNat (k + 1) = ((((((m0 * 0) + (m1 * 0)) + (m2 * 0)) + (m3 * Int.ofNat (k + 1))) + (m5 * 0)) + (-(((((m1 * 0) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * 0)))) by lib_int, h3]; lib_int)
    have h4 : ((Cyclo7.cyc7 LibInt.intR).mul (m0, m1, m2, m3, m4, m5) (Int.ofNat (k + 1), 0, 0, 0, 0, 0)).2.2.2.2.1 = ((Cyclo7.cyc7 LibInt.intR).mul (r0, r1, r2, r3, r4, r5) (Int.ofNat (k + 1), 0, 0, 0, 0, 0)).2.2.2.2.1 := congrArg (fun s => s.2.2.2.2.1) h
    change ((((((m0 * 0) + (m1 * 0)) + (m2 * 0)) + (m3 * 0)) + (m4 * Int.ofNat (k + 1))) + (-(((((m1 * 0) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * 0)))) = ((((((r0 * 0) + (r1 * 0)) + (r2 * 0)) + (r3 * 0)) + (r4 * Int.ofNat (k + 1))) + (-(((((r1 * 0) + (r2 * 0)) + (r3 * 0)) + (r4 * 0)) + (r5 * 0)))) at h4
    have e4 : m4 = r4 := JacobiNorm.int_cancel _ _ k (by rw [show m4 * Int.ofNat (k + 1) = ((((((m0 * 0) + (m1 * 0)) + (m2 * 0)) + (m3 * 0)) + (m4 * Int.ofNat (k + 1))) + (-(((((m1 * 0) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * 0)))) by lib_int, h4]; lib_int)
    have h5 : ((Cyclo7.cyc7 LibInt.intR).mul (m0, m1, m2, m3, m4, m5) (Int.ofNat (k + 1), 0, 0, 0, 0, 0)).2.2.2.2.2 = ((Cyclo7.cyc7 LibInt.intR).mul (r0, r1, r2, r3, r4, r5) (Int.ofNat (k + 1), 0, 0, 0, 0, 0)).2.2.2.2.2 := congrArg (fun s => s.2.2.2.2.2) h
    change (((((((m0 * 0) + (m1 * 0)) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * Int.ofNat (k + 1))) + (-(((((m1 * 0) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * 0)))) = (((((((r0 * 0) + (r1 * 0)) + (r2 * 0)) + (r3 * 0)) + (r4 * 0)) + (r5 * Int.ofNat (k + 1))) + (-(((((r1 * 0) + (r2 * 0)) + (r3 * 0)) + (r4 * 0)) + (r5 * 0)))) at h5
    have e5 : m5 = r5 := JacobiNorm.int_cancel _ _ k (by rw [show m5 * Int.ofNat (k + 1) = (((((((m0 * 0) + (m1 * 0)) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * Int.ofNat (k + 1))) + (-(((((m1 * 0) + (m2 * 0)) + (m3 * 0)) + (m4 * 0)) + (m5 * 0)))) by lib_int, h5]; lib_int)
    rw [e0, e1, e2, e3, e4, e5]

/-- **N(J) = p**: the septic Jacobi sum J = J(χ, χ) ∈ ℤ[ζ₇] satisfies J · conj(J) = p. -/
theorem sept_norm (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+347 chars; full line in the Zenodo source)
  have h2 := E2gen n hpr _ (Sept2.chi7_mul n hpr w hw1 hw7) (rho n w) (fun c => rfl) (rho_zero n w) (rho_sum n hpr w hw1 hw7)
  have h3 := E2gen n hpr _ (chib_mul n hpr w hw1 hw7) (rhob n w) (fun c => rfl) (rhob_zero n w) (rhob_sum n hpr w hw1 hw7)
  have h1 := E1 n hpr w hw1 hw7
  have h4 := E4 n hpr w hw1 hw7
  have hcm : (Cyclo7.cyc7 LibInt.intR).mul (chib n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) (chib n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = (Cyclo7.cyc7 LibInt.intR).one := by rw [chib_neg1 n hpr w hw1 hw7]; lib_ring
  let G := (gring (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1))
  have key : gmul (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((Sept2.chi7 n w))) (CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((chib n w))))) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7   -- (+294 chars; full line in the Zenodo source)
    have L : G.mul (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) ((Sept2.chi7 n w))) (CharSumGen.Gv (2 * (7 * n + 6)) ((Sept2.chi7 n w)))) (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) ((chib n w))) (CharSumGen.Gv (2 * (7 * n + 6)) ((chib n w))))
        = G.mul (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) ((Sept2.chi7 n w))) (CharSumGen.Gv (2 * (7 * n + 6)) ((chib n w)))) (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) ((Sept2.chi7 n w))) (CharSumGen.Gv (2 * (7 * n + 6)) ((chib n w)))) := by lib_ring
    have h2' : G.mul (CharSumGen.Gv (2 * (7 * n + 6)) ((Sept2.chi7 n w))) (CharSumGen.Gv (2 * (7 * n + 6)) ((Sept2.chi7 n w))) = G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((Sept2.chi7 n w))))) (CharSumGen.Gv (2 * (7 * n + 6)) ((rho n w))) := h2
    have h3' : G.mul (CharSumGen.Gv (2 * (7 * n + 6)) ((chib n w))) (CharSumGen.Gv (2 * (7 * n + 6)) ((chib n w))) = G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((chib n w))))) (CharSumGen.Gv (2 * (7 * n + 6)) ((rhob n w))) := h3
    have h1' : G.mul (CharSumGen.Gv (2 * (7 * n + 6)) ((Sept2.chi7 n w))) (CharSumGen.Gv (2 * (7 * n + 6)) ((chib n w))) = G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (chib n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one))) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))) := h1
    have h4' : G.mul (CharSumGen.Gv (2 * (7 * n + 6)) ((rho n w))) (CharSumGen.Gv (2 * (7 * n + 6)) ((rhob n w))) = G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).one)) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))) := h4
    have hX' : G.mul (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))) = G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((CharSumGen.Pp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))))) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))) := CharSumGen.XX (Cyclo7.cyc7 Li  -- (+28 chars; full line in the Zenodo source)
    have m2 : ∀ s t, G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero s) (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero t) = mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).mul s t) := fun s t => by
      show gmul (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) _ _ = _; rw [mono_mul, show (ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).zero (ring (2 * (7 * n + 6) + 1 + 1)).zero = (ring (2 * (7 * n + 6) + 1 + 1)).zero by lib_ring]
    rw [h2', h3', h1'] at L
    rw [show G.mul (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((Sept2.chi7 n w))))) (CharSumGen.Gv (2 * (7 * n + 6)) ((rho n w)))) (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 *  -- (+77 chars; full line in the Zenodo source)
        = G.mul (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((Sept2.chi7 n w))))) (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((chib n w)))))) (G.mul (CharSumGen.Gv (2 *  -- (+87 chars; full line in the Zenodo source)
      h4', m2, show G.mul (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (chib n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one))) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)))) (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (chib n w ((ring (2  -- (+129 chars; full line in the Zenodo source)
        = G.mul (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (chib n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one))) (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (chib n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)))) (G.mul (  -- (+132 chars; full line in the Zenodo source)
      hX', m2, hcm, show G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((Sept2.chi7 n w))) (CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((chib n w))))) (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6)   -- (+108 chars; full line in the Zenodo source)
        = G.mul (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((Sept2.chi7 n w))) (CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((chib n w))))) (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero   -- (+106 chars; full line in the Zenodo source)
      m2, show G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).one)) (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((CharSumGen.Pp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))))) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)))) = G.mul (G.mul (mono (C  -- (+353 chars; full line in the Zenodo source)
      m2, (Cyclo7.cyc7 LibInt.intR).mul_comm _ (Cyclo7.cyc7 LibInt.intR).one, (Cyclo7.cyc7 LibInt.intR).one_mul, (Cyclo7.cyc7 LibInt.intR).one_mul] at L
    exact L
  have k0 := congrArg (fun u => gt (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) u (ring (2 * (7 * n + 6) + 1 + 1)).zero) key
  have k1 : (Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((Sept2.chi7 n w))) (CharSumGen.Jp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) ((chib n w)))) (gt (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))) (ring (2 * (7 * n + 6) + 1 + 1)).zero) = (Cyclo7.cyc7 LibI  -- (+233 chars; full line in the Zenodo source)
    have := k0
    rw [JacobiNorm.mono0_mulg, JacobiNorm.mono0_mulg] at this
    exact this
  have hP : (CharSumGen.Pp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))) = (Int.ofNat (2 * (7 * n + 6) + 1 + 1 + 1), 0, 0, 0, 0, 0) := sum_one7 n _
  have hX0 : gt (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))) (ring (2 * (7 * n + 6) + 1 + 1)).zero = (Int.ofNat (2 * (7 * n + 6) + 1 + 1), 0, 0, 0, 0, 0) := by
    rw [CharSumGen.gt_X]; unfold delta; rw [ite_pos' _ _ rfl, (Cyclo7.cyc7 LibInt.intR).mul_comm, (Cyclo7.cyc7 LibInt.intR).one_mul, hP]
    apply Prod.ext
    · show Int.ofNat (2 * (7 * n + 6) + 1 + 1) + 1 + -1 = Int.ofNat (2 * (7 * n + 6) + 1 + 1); lib_int
    apply Prod.ext
    · show (0 : Int) + -0 = 0; rfl
    apply Prod.ext
    · show (0 : Int) + -0 = 0; rfl
    apply Prod.ext
    · show (0 : Int) + -0 = 0; rfl
    apply Prod.ext
    · show (0 : Int) + -0 = 0; rfl
    · show (0 : Int) + -0 = 0; rfl
  rw [hX0, hP, Jb_conj n hpr w hw1 hw7] at k1
  exact scal_cancel (2 * (7 * n + 6) + 1) _ _ k1

end Sept3

#print axioms Sept3.chi_one
#print axioms Sept3.neg1_ne
#print axioms Sept3.chi_iv
#print axioms Sept3.rho_mul
#print axioms Sept3.chib_mul
#print axioms Sept3.rhob_mul
#print axioms Sept3.kill
#print axioms Sept3.rho_sum
#print axioms Sept3.chib_sum
#print axioms Sept3.rhob_sum
#print axioms Sept3.chib_one
#print axioms Sept3.chib_zero
#print axioms Sept3.rho_one
#print axioms Sept3.rho_zero
#print axioms Sept3.rhob_one
#print axioms Sept3.rhob_zero
#print axioms Sept3.chib_neg1
#print axioms Sept3.rhob_neg1
#print axioms Sept3.gt_sq
#print axioms Sept3.conv_X
#print axioms Sept3.E2gen
#print axioms Sept3.E1
#print axioms Sept3.E4
#print axioms Sept3.conj7_mul
#print axioms Sept3.conj7_add
#print axioms Sept3.conj7_sum
#print axioms Sept3.Jb_conj
#print axioms Sept3.sum_one7
#print axioms Sept3.scal_cancel
#print axioms Sept3.sept_norm

#print axioms: 30 theorems, no axioms.

78.198 Reduction of septic character values

The coordinatewise reduction \(\mathbb Z[\zeta _7]\to T[\zeta _7]\) is a ring map and the powers \(\chi ^r\) (\(2\le r\le 6\)) sum to zero.

\[ \sum _a\chi (a)^r=0\ (2\le r\le 6),\qquad \chi (x)^6=\chi (x^{-1}). \]

lang/Sept4.lean

/-
  lang/Sept4 — T49, cycle 774 (ℓ = 7, step 4): reduction ℤ[ζ₇] → T[ζ₇] and the inputs of the n-th power residue criterion
  (n = 7) for the septic character (Quint6 pattern, generated by s4gen.py).
    ph7, ph7_add/mul/neg/one/zero/sum/pow, ph7_zeta   the coordinatewise reduction is a ring map with ζ ↦ ζ
    z7T, zt_mod7T                                     ζ⁷ = 1 and ζ^(7m + r) = ζ^r in T[ζ₇] for every T
    chi_pow_sum2 … chi_pow_sum6                       Σ_a χ(a)^r = 0 for 2 ≤ r ≤ 6
    chi_pow6                                          χ(x)⁶ = χ(x⁻¹)
    psi_*                                             ψ = ph7 ∘ χ: multiplicative, ψ(1) = 1, ψ(0) = 0, Σψ = 0,
                                                      Σ ψ^(i+2) = 0 (i < 5), ψ⁶ = ψ ∘ ι
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Sept3
import lang.QuartRecip

namespace Sept4

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)

section Gen
variable {R : Type} (T : CRing R)

def ph7 (z : Int × Int × Int × Int × Int × Int) : R × R × R × R × R × R := (castZ T z.1, castZ T z.2.1, castZ T z.2.2.1, castZ T z.2.2.2.1, castZ T z.2.2.2.2.1, castZ T z.2.2.2.2.2)
def ztT : R × R × R × R × R × R := (T.zero, T.one, T.zero, T.zero, T.zero, T.zero)

theorem ph7_add : ∀ x y : Int × Int × Int × Int × Int × Int, ph7 T ((Cyclo7.cyc7 LibInt.intR).add x y) = (Cyclo7.cyc7 T).add (ph7 T x) (ph7 T y)
  | (a0, a1, a2, a3, a4, a5), (b0, b1, b2, b3, b4, b5) => by
    apply Prod.ext
    · exact IntCast.castZ_add T a0 b0
    apply Prod.ext
    · exact IntCast.castZ_add T a1 b1
    apply Prod.ext
    · exact IntCast.castZ_add T a2 b2
    apply Prod.ext
    · exact IntCast.castZ_add T a3 b3
    apply Prod.ext
    · exact IntCast.castZ_add T a4 b4
    · exact IntCast.castZ_add T a5 b5

theorem ph7_mul : ∀ x y : Int × Int × Int × Int × Int × Int, ph7 T ((Cyclo7.cyc7 LibInt.intR).mul x y) = (Cyclo7.cyc7 T).mul (ph7 T x) (ph7 T y)
  | (a0, a1, a2, a3, a4, a5), (b0, b1, b2, b3, b4, b5) => by
    apply Prod.ext
    · show castZ T (((a0 * b0) + ((((a2 * b5) + (a3 * b4)) + (a4 * b3)) + (a5 * b2))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) = (T.add (T.add (T.mul (castZ T a0) (castZ T b0)) (T.add (T.add (T.add (T.mul (castZ T a2) (castZ T b5)) (T.mul (castZ T a3) (castZ T b4))) (T.mul (castZ T a4) (castZ T b3))) (T.mul (castZ T a5) (castZ T b2)))) (T.neg (T.add (T.add (T.add (T.  -- (+179 chars; full line in the Zenodo source)
      repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    apply Prod.ext
    · show castZ T ((((a0 * b1) + (a1 * b0)) + (((a3 * b5) + (a4 * b4)) + (a5 * b3))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) = (T.add (T.add (T.add (T.mul (castZ T a0) (castZ T b1)) (T.mul (castZ T a1) (castZ T b0))) (T.add (T.add (T.mul (castZ T a3) (castZ T b5)) (T.mul (castZ T a4) (castZ T b4))) (T.mul (castZ T a5) (castZ T b3)))) (T.neg (T.add (T.add (T.add (T.  -- (+179 chars; full line in the Zenodo source)
      repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    apply Prod.ext
    · show castZ T (((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + ((a4 * b5) + (a5 * b4))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) = (T.add (T.add (T.add (T.add (T.mul (castZ T a0) (castZ T b2)) (T.mul (castZ T a1) (castZ T b1))) (T.mul (castZ T a2) (castZ T b0))) (T.add (T.mul (castZ T a4) (castZ T b5)) (T.mul (castZ T a5) (castZ T b4)))) (T.neg (T.add (T.add (T.add (T.  -- (+179 chars; full line in the Zenodo source)
      repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    apply Prod.ext
    · show castZ T ((((((a0 * b3) + (a1 * b2)) + (a2 * b1)) + (a3 * b0)) + (a5 * b5)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) = (T.add (T.add (T.add (T.add (T.add (T.mul (castZ T a0) (castZ T b3)) (T.mul (castZ T a1) (castZ T b2))) (T.mul (castZ T a2) (castZ T b1))) (T.mul (castZ T a3) (castZ T b0))) (T.mul (castZ T a5) (castZ T b5))) (T.neg (T.add (T.add (T.add (T.  -- (+179 chars; full line in the Zenodo source)
      repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    apply Prod.ext
    · show castZ T ((((((a0 * b4) + (a1 * b3)) + (a2 * b2)) + (a3 * b1)) + (a4 * b0)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) = (T.add (T.add (T.add (T.add (T.add (T.mul (castZ T a0) (castZ T b4)) (T.mul (castZ T a1) (castZ T b3))) (T.mul (castZ T a2) (castZ T b2))) (T.mul (castZ T a3) (castZ T b1))) (T.mul (castZ T a4) (castZ T b0))) (T.neg (T.add (T.add (T.add (T.  -- (+179 chars; full line in the Zenodo source)
      repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    · show castZ T (((((((a0 * b5) + (a1 * b4)) + (a2 * b3)) + (a3 * b2)) + (a4 * b1)) + (a5 * b0)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) = (T.add (T.add (T.add (T.add (T.add (T.add (T.mul (castZ T a0) (castZ T b5)) (T.mul (castZ T a1) (castZ T b4))) (T.mul (castZ T a2) (castZ T b3))) (T.mul (castZ T a3) (castZ T b2))) (T.mul (castZ T a4) (castZ T b1))) (T.mul (ca  -- (+235 chars; full line in the Zenodo source)
      repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])

theorem ph7_neg : ∀ x : Int × Int × Int × Int × Int × Int, ph7 T ((Cyclo7.cyc7 LibInt.intR).neg x) = (Cyclo7.cyc7 T).neg (ph7 T x)
  | (a0, a1, a2, a3, a4, a5) => by
    apply Prod.ext
    · exact HasseDavenport.castZ_neg T a0
    apply Prod.ext
    · exact HasseDavenport.castZ_neg T a1
    apply Prod.ext
    · exact HasseDavenport.castZ_neg T a2
    apply Prod.ext
    · exact HasseDavenport.castZ_neg T a3
    apply Prod.ext
    · exact HasseDavenport.castZ_neg T a4
    · exact HasseDavenport.castZ_neg T a5

theorem ph7_one : ph7 T (Cyclo7.cyc7 LibInt.intR).one = (Cyclo7.cyc7 T).one := by
  apply Prod.ext
  · show castN T 1 = T.one
    exact add_zero' T _
  · rfl

theorem ph7_zero : ph7 T (Cyclo7.cyc7 LibInt.intR).zero = (Cyclo7.cyc7 T).zero := rfl

theorem ph7_zeta : ph7 T Sept2.zeta = ztT T := by
  apply Prod.ext
  · rfl
  apply Prod.ext
  · show castN T 1 = T.one
    exact add_zero' T _
  · rfl

theorem ph7_sum {A' : Type} (f : A' → Int × Int × Int × Int × Int × Int) : ∀ l : List A', ph7 T (sumK (Cyclo7.cyc7 LibInt.intR) f l) = sumK (Cyclo7.cyc7 T) (fun a => ph7 T (f a)) l
  | [] => rfl
  | a :: l => by
    show ph7 T ((Cyclo7.cyc7 LibInt.intR).add (f a) (sumK _ f l)) = (Cyclo7.cyc7 T).add (ph7 T (f a)) (sumK (Cyclo7.cyc7 T) (fun a => ph7 T (f a)) l)
    rw [ph7_add, ph7_sum f l]

theorem ph7_pow (z : Int × Int × Int × Int × Int × Int) : ∀ k, pw (Cyclo7.cyc7 T) (ph7 T z) k = ph7 T (pw (Cyclo7.cyc7 LibInt.intR) z k)
  | 0 => (ph7_one T).symm
  | k + 1 => by
    show (Cyclo7.cyc7 T).mul (pw _ (ph7 T z) k) (ph7 T z) = ph7 T ((Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) z k) z)
    rw [ph7_pow z k, ph7_mul]

theorem z7T : pw (Cyclo7.cyc7 T) (ztT T) 7 = (Cyclo7.cyc7 T).one := by
  rw [← ph7_zeta, ph7_pow, Sept2.z7, ph7_one]

theorem zt_mod7T (m r : Nat) : pw (Cyclo7.cyc7 T) (ztT T) (7 * m + r) = pw (Cyclo7.cyc7 T) (ztT T) r := by
  rw [← ph7_zeta, ph7_pow, ph7_pow, Sept2.zt_mod7]
end Gen

variable (n : Nat)

theorem chi_pow_sum2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2  -- (+254 chars; full line in the Zenodo source)
  match PowerSum.exists_nonroot (2 * (7 * n + 6) + 1) hpr (2 * n + 1) (by rw [show 2 * (7 * n + 6) + 1 = (2 * n + 1 + 1) + (12 * n + 11) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := CharSumGen.sum_scale (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 2) x0 hx0
    have e : sumK (Cyclo7.cyc7 LibInt.intR) (fun x => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).mul x0 x)) 2) (CharSumGen.LL (2 * (7 * n + 6))) = (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w x0) 2) (sumK (Cyclo7.cyc7 LibInt.intR) (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 2) (CharSumGen.LL (2 * (7 * n + 6)))) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun x => by rw [Sept2.chi7_mul n hpr w hw1 hw7, pw_mul]) _
    have hre' := e.symm.trans hre
    generalize sumK (Cyclo7.cyc7 LibInt.intR) (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 2) (CharSumGen.LL (2 * (7 * n + 6))) = S at hre' ⊢
    match Sept2.exists_i n hpr w hw1 hw7 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 x0 hx0 i hi ei, TwoPower.pwpow] at hre'
      match i, hi with
      | 0, _ => exact absurd ei hn
      | 1, _ =>
        exact Sept3.kill S 2 (show 2 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_right (2 + 1) 4)) (fun e => Nat.noConfusion e) hre'
      | 2, _ =>
        exact Sept3.kill S 4 (show 4 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 4) (Nat.le_add_right (4 + 1) 2)) (fun e => Nat.noConfusion e) hre'
      | 3, _ =>
        exact Sept3.kill S 6 (show 6 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 6) (Nat.le_add_right (6 + 1) 0)) (fun e => Nat.noConfusion e) hre'
      | 4, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (4 * 2) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 1 from Sept2.zt_mod7 1 1] at hre'
        exact Sept3.kill S 1 (show 1 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_right (1 + 1) 5)) (fun e => Nat.noConfusion e) hre'
      | 5, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (5 * 2) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 3 from Sept2.zt_mod7 1 3] at hre'
        exact Sept3.kill S 3 (show 3 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right (3 + 1) 3)) (fun e => Nat.noConfusion e) hre'
      | 6, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (6 * 2) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 5 from Sept2.zt_mod7 1 5] at hre'
        exact Sept3.kill S 5 (show 5 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 5) (Nat.le_add_right (5 + 1) 1)) (fun e => Nat.noConfusion e) hre'
      | k + 7, hk => exact absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))

theorem chi_pow_sum3 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2  -- (+254 chars; full line in the Zenodo source)
  match PowerSum.exists_nonroot (2 * (7 * n + 6) + 1) hpr (2 * n + 1) (by rw [show 2 * (7 * n + 6) + 1 = (2 * n + 1 + 1) + (12 * n + 11) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := CharSumGen.sum_scale (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 3) x0 hx0
    have e : sumK (Cyclo7.cyc7 LibInt.intR) (fun x => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).mul x0 x)) 3) (CharSumGen.LL (2 * (7 * n + 6))) = (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w x0) 3) (sumK (Cyclo7.cyc7 LibInt.intR) (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 3) (CharSumGen.LL (2 * (7 * n + 6)))) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun x => by rw [Sept2.chi7_mul n hpr w hw1 hw7, pw_mul]) _
    have hre' := e.symm.trans hre
    generalize sumK (Cyclo7.cyc7 LibInt.intR) (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 3) (CharSumGen.LL (2 * (7 * n + 6))) = S at hre' ⊢
    match Sept2.exists_i n hpr w hw1 hw7 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 x0 hx0 i hi ei, TwoPower.pwpow] at hre'
      match i, hi with
      | 0, _ => exact absurd ei hn
      | 1, _ =>
        exact Sept3.kill S 3 (show 3 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right (3 + 1) 3)) (fun e => Nat.noConfusion e) hre'
      | 2, _ =>
        exact Sept3.kill S 6 (show 6 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 6) (Nat.le_add_right (6 + 1) 0)) (fun e => Nat.noConfusion e) hre'
      | 3, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (3 * 3) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 2 from Sept2.zt_mod7 1 2] at hre'
        exact Sept3.kill S 2 (show 2 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_right (2 + 1) 4)) (fun e => Nat.noConfusion e) hre'
      | 4, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (4 * 3) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 5 from Sept2.zt_mod7 1 5] at hre'
        exact Sept3.kill S 5 (show 5 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 5) (Nat.le_add_right (5 + 1) 1)) (fun e => Nat.noConfusion e) hre'
      | 5, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (5 * 3) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 1 from Sept2.zt_mod7 2 1] at hre'
        exact Sept3.kill S 1 (show 1 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_right (1 + 1) 5)) (fun e => Nat.noConfusion e) hre'
      | 6, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (6 * 3) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 4 from Sept2.zt_mod7 2 4] at hre'
        exact Sept3.kill S 4 (show 4 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 4) (Nat.le_add_right (4 + 1) 2)) (fun e => Nat.noConfusion e) hre'
      | k + 7, hk => exact absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))

theorem chi_pow_sum4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2  -- (+254 chars; full line in the Zenodo source)
  match PowerSum.exists_nonroot (2 * (7 * n + 6) + 1) hpr (2 * n + 1) (by rw [show 2 * (7 * n + 6) + 1 = (2 * n + 1 + 1) + (12 * n + 11) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := CharSumGen.sum_scale (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 4) x0 hx0
    have e : sumK (Cyclo7.cyc7 LibInt.intR) (fun x => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).mul x0 x)) 4) (CharSumGen.LL (2 * (7 * n + 6))) = (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w x0) 4) (sumK (Cyclo7.cyc7 LibInt.intR) (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 4) (CharSumGen.LL (2 * (7 * n + 6)))) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun x => by rw [Sept2.chi7_mul n hpr w hw1 hw7, pw_mul]) _
    have hre' := e.symm.trans hre
    generalize sumK (Cyclo7.cyc7 LibInt.intR) (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 4) (CharSumGen.LL (2 * (7 * n + 6))) = S at hre' ⊢
    match Sept2.exists_i n hpr w hw1 hw7 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 x0 hx0 i hi ei, TwoPower.pwpow] at hre'
      match i, hi with
      | 0, _ => exact absurd ei hn
      | 1, _ =>
        exact Sept3.kill S 4 (show 4 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 4) (Nat.le_add_right (4 + 1) 2)) (fun e => Nat.noConfusion e) hre'
      | 2, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (2 * 4) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 1 from Sept2.zt_mod7 1 1] at hre'
        exact Sept3.kill S 1 (show 1 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_right (1 + 1) 5)) (fun e => Nat.noConfusion e) hre'
      | 3, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (3 * 4) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 5 from Sept2.zt_mod7 1 5] at hre'
        exact Sept3.kill S 5 (show 5 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 5) (Nat.le_add_right (5 + 1) 1)) (fun e => Nat.noConfusion e) hre'
      | 4, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (4 * 4) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 2 from Sept2.zt_mod7 2 2] at hre'
        exact Sept3.kill S 2 (show 2 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_right (2 + 1) 4)) (fun e => Nat.noConfusion e) hre'
      | 5, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (5 * 4) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 6 from Sept2.zt_mod7 2 6] at hre'
        exact Sept3.kill S 6 (show 6 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 6) (Nat.le_add_right (6 + 1) 0)) (fun e => Nat.noConfusion e) hre'
      | 6, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (6 * 4) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 3 from Sept2.zt_mod7 3 3] at hre'
        exact Sept3.kill S 3 (show 3 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right (3 + 1) 3)) (fun e => Nat.noConfusion e) hre'
      | k + 7, hk => exact absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))

theorem chi_pow_sum5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2  -- (+254 chars; full line in the Zenodo source)
  match PowerSum.exists_nonroot (2 * (7 * n + 6) + 1) hpr (2 * n + 1) (by rw [show 2 * (7 * n + 6) + 1 = (2 * n + 1 + 1) + (12 * n + 11) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := CharSumGen.sum_scale (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 5) x0 hx0
    have e : sumK (Cyclo7.cyc7 LibInt.intR) (fun x => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).mul x0 x)) 5) (CharSumGen.LL (2 * (7 * n + 6))) = (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w x0) 5) (sumK (Cyclo7.cyc7 LibInt.intR) (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 5) (CharSumGen.LL (2 * (7 * n + 6)))) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun x => by rw [Sept2.chi7_mul n hpr w hw1 hw7, pw_mul]) _
    have hre' := e.symm.trans hre
    generalize sumK (Cyclo7.cyc7 LibInt.intR) (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 5) (CharSumGen.LL (2 * (7 * n + 6))) = S at hre' ⊢
    match Sept2.exists_i n hpr w hw1 hw7 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 x0 hx0 i hi ei, TwoPower.pwpow] at hre'
      match i, hi with
      | 0, _ => exact absurd ei hn
      | 1, _ =>
        exact Sept3.kill S 5 (show 5 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 5) (Nat.le_add_right (5 + 1) 1)) (fun e => Nat.noConfusion e) hre'
      | 2, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (2 * 5) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 3 from Sept2.zt_mod7 1 3] at hre'
        exact Sept3.kill S 3 (show 3 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right (3 + 1) 3)) (fun e => Nat.noConfusion e) hre'
      | 3, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (3 * 5) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 1 from Sept2.zt_mod7 2 1] at hre'
        exact Sept3.kill S 1 (show 1 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_right (1 + 1) 5)) (fun e => Nat.noConfusion e) hre'
      | 4, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (4 * 5) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 6 from Sept2.zt_mod7 2 6] at hre'
        exact Sept3.kill S 6 (show 6 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 6) (Nat.le_add_right (6 + 1) 0)) (fun e => Nat.noConfusion e) hre'
      | 5, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (5 * 5) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 4 from Sept2.zt_mod7 3 4] at hre'
        exact Sept3.kill S 4 (show 4 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 4) (Nat.le_add_right (4 + 1) 2)) (fun e => Nat.noConfusion e) hre'
      | 6, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (6 * 5) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 2 from Sept2.zt_mod7 4 2] at hre'
        exact Sept3.kill S 2 (show 2 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_right (2 + 1) 4)) (fun e => Nat.noConfusion e) hre'
      | k + 7, hk => exact absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))

theorem chi_pow_sum6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2  -- (+254 chars; full line in the Zenodo source)
  match PowerSum.exists_nonroot (2 * (7 * n + 6) + 1) hpr (2 * n + 1) (by rw [show 2 * (7 * n + 6) + 1 = (2 * n + 1 + 1) + (12 * n + 11) by lib_nat]; exact Nat.le_add_right _ _) with
  | ⟨x0, hx0, hn⟩ =>
    have hre := CharSumGen.sum_scale (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 6) x0 hx0
    have e : sumK (Cyclo7.cyc7 LibInt.intR) (fun x => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).mul x0 x)) 6) (CharSumGen.LL (2 * (7 * n + 6))) = (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w x0) 6) (sumK (Cyclo7.cyc7 LibInt.intR) (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 6) (CharSumGen.LL (2 * (7 * n + 6)))) := by
      rw [mul_sumK]; exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun x => by rw [Sept2.chi7_mul n hpr w hw1 hw7, pw_mul]) _
    have hre' := e.symm.trans hre
    generalize sumK (Cyclo7.cyc7 LibInt.intR) (fun a => pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 6) (CharSumGen.LL (2 * (7 * n + 6))) = S at hre' ⊢
    match Sept2.exists_i n hpr w hw1 hw7 x0 hx0 with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 x0 hx0 i hi ei, TwoPower.pwpow] at hre'
      match i, hi with
      | 0, _ => exact absurd ei hn
      | 1, _ =>
        exact Sept3.kill S 6 (show 6 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 6) (Nat.le_add_right (6 + 1) 0)) (fun e => Nat.noConfusion e) hre'
      | 2, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (2 * 6) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 5 from Sept2.zt_mod7 1 5] at hre'
        exact Sept3.kill S 5 (show 5 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 5) (Nat.le_add_right (5 + 1) 1)) (fun e => Nat.noConfusion e) hre'
      | 3, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (3 * 6) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 4 from Sept2.zt_mod7 2 4] at hre'
        exact Sept3.kill S 4 (show 4 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 4) (Nat.le_add_right (4 + 1) 2)) (fun e => Nat.noConfusion e) hre'
      | 4, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (4 * 6) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 3 from Sept2.zt_mod7 3 3] at hre'
        exact Sept3.kill S 3 (show 3 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_right (3 + 1) 3)) (fun e => Nat.noConfusion e) hre'
      | 5, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (5 * 6) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 2 from Sept2.zt_mod7 4 2] at hre'
        exact Sept3.kill S 2 (show 2 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_right (2 + 1) 4)) (fun e => Nat.noConfusion e) hre'
      | 6, _ =>
        rw [show pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta (6 * 6) = pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta 1 from Sept2.zt_mod7 5 1] at hre'
        exact Sept3.kill S 1 (show 1 < 7 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_right (1 + 1) 5)) (fun e => Nat.noConfusion e) hre'
      | k + 7, hk => exact absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))

/-- χ(x)⁶ = χ(x⁻¹). -/
theorem chi_pow6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+229 chars; full line in the Zenodo source)
  rw [Sept3.chi_iv n hpr w hw1 hw7]
  by_cases hx : x = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hx, Sept2.chi7_zero]; rfl
  match Sept2.exists_i n hpr w hw1 hw7 x hx with
  | ⟨i, hi, ei⟩ =>
    rw [Sept2.val_of n hpr w hw1 hw7 x hx i hi ei]
    match i, hi with
    | 0, _ => rfl
    | 1, _ => rfl
    | 2, _ => rfl
    | 3, _ => rfl
    | 4, _ => rfl
    | 5, _ => rfl
    | 6, _ => rfl
    | k + 7, hk => exact absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))

section Psi
variable {R : Type} (T : CRing R)

def psi (w x : Zp (2 * (7 * n + 6) + 1 + 1)) : R × R × R × R × R × R := ph7 T (Sept2.chi7 n w x)

theorem psi_mul (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+230 chars; full line in the Zenodo source)
  unfold psi; rw [Sept2.chi7_mul n hpr w hw1 hw7, ph7_mul]
theorem psi_one (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+159 chars; full line in the Zenodo source)
  unfold psi; rw [Sept3.chi_one n hpr w hw1 hw7, ph7_one]
theorem psi_zero (w : Zp (2 * (7 * n + 6) + 1 + 1)) : psi n T w (ring (2 * (7 * n + 6) + 1 + 1)).zero = (Cyclo7.cyc7 T).zero := by
  unfold psi; rw [Sept2.chi7_zero]; rfl
theorem psi_sum (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+180 chars; full line in the Zenodo source)
  have hs : sumK (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w) (CharSumGen.LL (2 * (7 * n + 6))) = (Cyclo7.cyc7 LibInt.intR).zero := Sept2.chi7_sum n hpr w hw1 hw7
  unfold psi; rw [← ph7_sum, hs]; rfl
theorem psi_nt (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+226 chars; full line in the Zenodo source)
  | 0, _ => by unfold psi; rw [sumK_ext (Cyclo7.cyc7 T) _ _ (fun a => ph7_pow T _ _), ← ph7_sum, chi_pow_sum2 n hpr w hw1 hw7]; rfl
  | 1, _ => by unfold psi; rw [sumK_ext (Cyclo7.cyc7 T) _ _ (fun a => ph7_pow T _ _), ← ph7_sum, chi_pow_sum3 n hpr w hw1 hw7]; rfl
  | 2, _ => by unfold psi; rw [sumK_ext (Cyclo7.cyc7 T) _ _ (fun a => ph7_pow T _ _), ← ph7_sum, chi_pow_sum4 n hpr w hw1 hw7]; rfl
  | 3, _ => by unfold psi; rw [sumK_ext (Cyclo7.cyc7 T) _ _ (fun a => ph7_pow T _ _), ← ph7_sum, chi_pow_sum5 n hpr w hw1 hw7]; rfl
  | 4, _ => by unfold psi; rw [sumK_ext (Cyclo7.cyc7 T) _ _ (fun a => ph7_pow T _ _), ← ph7_sum, chi_pow_sum6 n hpr w hw1 hw7]; rfl
  | k + 5, hk => absurd hk (fun h => Nat.lt_irrefl 5 (Nat.lt_of_le_of_lt (Nat.le_add_left 5 k) h))
theorem psi_inv (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+214 chars; full line in the Zenodo source)
  unfold psi; rw [ph7_pow, chi_pow6 n hpr w hw1 hw7]
end Psi

end Sept4

#print axioms Sept4.ph7_add
#print axioms Sept4.ph7_mul
#print axioms Sept4.ph7_neg
#print axioms Sept4.ph7_one
#print axioms Sept4.ph7_zero
#print axioms Sept4.ph7_zeta
#print axioms Sept4.ph7_sum
#print axioms Sept4.ph7_pow
#print axioms Sept4.z7T
#print axioms Sept4.zt_mod7T
#print axioms Sept4.chi_pow_sum2
#print axioms Sept4.chi_pow_sum3
#print axioms Sept4.chi_pow_sum4
#print axioms Sept4.chi_pow_sum5
#print axioms Sept4.chi_pow_sum6
#print axioms Sept4.chi_pow6
#print axioms Sept4.psi_mul
#print axioms Sept4.psi_one
#print axioms Sept4.psi_zero
#print axioms Sept4.psi_sum
#print axioms Sept4.psi_nt
#print axioms Sept4.psi_inv

#print axioms: 22 theorems, no axioms.

78.199 Septic residue criterion at every prime

For a prime \(q\neq 7,p\) with \(q^6=7M+1\) the reduced septic character satisfies, in \(\mathbb F_q[\zeta _7]\),

\[ \psi (q^{-6})=(A\,p)^{M},\qquad A=\prod _{i\le 5}J(\psi ^i,\psi ). \]

lang/Sept5.lean

/-
  lang/Sept5 — T49, cycle 756 (ℓ = 7, step 5): **the septic residue criterion at every prime q ≠ 7, p**.
  p = 14n + 15, ψ = ph7 ∘ χ with values in 𝔽_q[ζ₇] (Sept4), q prime with q⁶ = 7M + 1 (every q ≠ 7):
    frob_fix6           ψ(a)^(q⁶) = ψ(a)            (ζ^(q⁶) = ζ, Sept4.zt_mod7T)
    sept_recip_all     ψ(q⁻⁶) = (A p)^M  in 𝔽_q[ζ₇],  A = ∏_{i≤5} J(ψ^i, ψ)   (CharSumGen.nth_recip_pow, n = 7, e = 6)
    charq7              q·1 = 0 in 𝔽_q[ζ₇]
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Sept4
import lang.NthRecipPow

namespace Sept5

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob


theorem charq7 (j : Nat) : nsm (Cyclo7.cyc7 (ring (j + 1))) (j + 1 + 1) (Cyclo7.cyc7 (ring (j + 1))).one = (Cyclo7.cyc7 (ring (j + 1))).zero := by
  have h := char_p (j + 1)
  have z : ∀ m, nsm (ring (j + 1)) m (ring (j + 1)).zero = (ring (j + 1)).zero := fun m => by
    induction m with
    | zero => rfl
    | succ m ih => show (ring (j + 1)).add (nsm (ring (j + 1)) m (ring (j + 1)).zero) (ring (j + 1)).zero = _; rw [ih, DetChain.add_zero' (ring (j + 1))]
  have hn : ∀ m, nsm (Cyclo7.cyc7 (ring (j + 1))) m (Cyclo7.cyc7 (ring (j + 1))).one =
      (nsm (ring (j + 1)) m (ring (j + 1)).one, nsm (ring (j + 1)) m (ring (j + 1)).zero, nsm (ring (j + 1)) m (ring (j + 1)).zero,
        nsm (ring (j + 1)) m (ring (j + 1)).zero, nsm (ring (j + 1)) m (ring (j + 1)).zero, nsm (ring (j + 1)) m (ring (j + 1)).zero) := fun m => by
    induction m with
    | zero => rfl
    | succ m ih =>
      show (Cyclo7.cyc7 (ring (j + 1))).add (nsm (Cyclo7.cyc7 (ring (j + 1))) m (Cyclo7.cyc7 (ring (j + 1))).one) (Cyclo7.cyc7 (ring (j + 1))).one = _
      rw [ih]; rfl
  rw [hn, h, z]; rfl

variable (n k : Nat)

/-- ψ(a)^(q⁶) = ψ(a) when q⁶ = 7M + 1. -/
theorem frob_fix6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+294 chars; full line in the Zenodo source)
  rw [hM]
  by_cases ha : a = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [ha, Sept4.psi_zero]
    exact HasseCM.pw_zero_succ (Cyclo7.cyc7 (ring (k + 1))) (7 * M)
  match Sept2.exists_i n hpr w hw1 hw7 a ha with
  | ⟨i, hi, ei⟩ =>
    have hv : (Sept4.psi n (ring (k + 1)) w) a = pw (Cyclo7.cyc7 (ring (k + 1))) (Sept4.ztT (ring (k + 1))) i := by
      unfold Sept4.psi; rw [Sept2.val_of n hpr w hw1 hw7 a ha i hi ei, ← Sept4.ph7_pow, Sept4.ph7_zeta]
    rw [hv, TwoPower.pwpow, show i * (7 * M + 1) = 7 * (i * M) + i by lib_nat, Sept4.zt_mod7T]

theorem psi_iv_neg1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2   -- (+270 chars; full line in the Zenodo source)
  unfold Sept4.psi
  rw [show Sept2.chi7 n w (CharSumGen.iv (2 * (7 * n + 6)) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = Sept3.chib n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) from rfl, Sept3.chib_neg1 n hpr w hw1 hw7, Sept4.ph7_one]

/-- **Septic residue criterion at every prime q with q⁶ = 7M + 1**: ψ(q⁻⁶) = (A p)^M in 𝔽_q[ζ₇], A = ∏_{i≤5} J(ψ^i, ψ). -/
theorem sept_recip_all (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring   -- (+332 chars; full line in the Zenodo source)
    (Sept4.psi n (ring (k + 1)) w) (pw (ring (2 * (7 * n + 6) + 1 + 1)) (nsm (ring (2 * (7 * n + 6) + 1 + 1)) ((k + 1 + 1) ^ 6) (ring (2 * (7 * n + 6) + 1 + 1)).one) (2 * (7 * n + 6) + 1)) = pw (Cyclo7.cyc7 (ring (k + 1))) ((Cyclo7.cyc7 (ring (k + 1))).mul (CharSumGen.jpr (Cyclo7.cyc7 (ring (k + 1))) (2 * (7 * n + 6)) (Sept4.psi n (ring (k + 1)) w) 5) (CharSumGen.Pp (Cyclo7.cyc7 (ring (k + 1))) (2  -- (+25 chars; full line in the Zenodo source)
  have h := CharSumGen.nth_recip_pow (Cyclo7.cyc7 (ring (k + 1))) (2 * (7 * n + 6)) hpr (Sept4.psi n (ring (k + 1)) w) (Sept4.psi_mul n (ring (k + 1)) hpr w hw1 hw7) (Sept4.psi_one n (ring (k + 1)) hpr w hw1 hw7) (Sept4.psi_zero n (ring (k + 1)) w)
    (Sept4.psi_sum n (ring (k + 1)) hpr w hw1 hw7) 5 (Sept4.psi_nt n (ring (k + 1)) hpr w hw1 hw7) (Sept4.psi_inv n (ring (k + 1)) hpr w hw1 hw7) k hq (charq7 k) 6
    (frob_fix6 n k hpr w hw1 hw7 M hM) hQ M hM
  rw [psi_iv_neg1 n k hpr w hw1 hw7] at h
  rw [show (Cyclo7.cyc7 (ring (k + 1))).mul (CharSumGen.jpr (Cyclo7.cyc7 (ring (k + 1))) (2 * (7 * n + 6)) (Sept4.psi n (ring (k + 1)) w) 5) (Cyclo7.cyc7 (ring (k + 1))).one = CharSumGen.jpr (Cyclo7.cyc7 (ring (k + 1))) (2 * (7 * n + 6)) (Sept4.psi n (ring (k + 1)) w) 5 by lib_ring] at h
  exact h

end Sept5

#print axioms Sept5.charq7
#print axioms Sept5.frob_fix6
#print axioms Sept5.psi_iv_neg1
#print axioms Sept5.sept_recip_all

#print axioms: 4 theorems, no axioms.

78.200 Primary normalisation of the septic product

The product of Jacobi sums in the septic residue criterion is congruent to \(-1\) modulo \(7\).

\[ A=\prod _{i\le 5}J(\psi ^i,\psi )\equiv -1\pmod 7. \]

lang/Sept6.lean

/-
  lang/Sept6 — T49, cycle 776 (ℓ = 7, step 6): **the product A = ∏_{i≤5} J(ψ^i, ψ) of the septic residue criterion is ≡ −1 (mod 7)**
  (primary normalisation; ℓ = 5 analogue Quint18). Over E = 𝔽₇[ζ₇] with ψ = ph7 ∘ χ: CharSumGen.gpow_n gives G⁷ = A·X (ζ¹-coefficient −A),
  Quint18.gauss_frob' gives G⁷ = ψ(7⁻¹)⁷ G_(ψ⁷) (ζ¹-coefficient ψ⁷(1) = 1, ψ⁷ = 1 off 0), so A = −1 in 𝔽₇[ζ₇].
    Q7_ne, psi7, psi_neg1, sept_primary
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Sept5
import lang.Quint18
import lang.CubicSeven

namespace Sept6

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing GroupRingFrob Frobenius

variable (n : Nat)

theorem Q7_ne : (nsm (ring (2 * (7 * n + 6) + 1 + 1)) (5 + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).one) ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := fun e => by
  have h := congrArg Subtype.val e
  rw [nsm_one_val, md_small _ (5 + 1 + 1) (by rw [show 2 * (7 * n + 6) + 1 + 1 + 1 = (5 + 1 + 1) + (14 * n + 7) + 1 by lib_nat]; exact Nat.lt_succ_of_le (Nat.le_add_right _ _))] at h
  exact Nat.noConfusion h

section Gen
variable {R : Type} (K : CRing R)

theorem psi7 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+244 chars; full line in the Zenodo source)
  show (Cyclo7.cyc7 K).mul (pw (Cyclo7.cyc7 K) (Sept4.psi n K w x) (5 + 1)) (Sept4.psi n K w x) = _
  rw [Sept4.psi_inv n K hpr w hw1 hw7, ← Sept4.psi_mul n K hpr w hw1 hw7, CharSumGen.iv_mul (2 * (7 * n + 6)) hpr x hx, Sept4.psi_one n K hpr w hw1 hw7]

theorem psi_neg1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+205 chars; full line in the Zenodo source)
  unfold Sept4.psi; rw [Sept2.chi7_neg1 n hpr w hw1 hw7, Sept4.ph7_one]
end Gen

/-- **A = ∏_{i≤5} J(ψ^i, ψ) ≡ −1 (mod 7).** -/
theorem sept_primary (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2  -- (+234 chars; full line in the Zenodo source)
  have hG := CharSumGen.gpow_n (Cyclo7.cyc7 (ring 6)) (2 * (7 * n + 6)) hpr (Sept4.psi n (ring 6) w) (Sept4.psi_mul n (ring 6) hpr w hw1 hw7) (Sept4.psi_one n (ring 6) hpr w hw1 hw7) (Sept4.psi_zero n (ring 6) w)
    (Sept4.psi_sum n (ring 6) hpr w hw1 hw7) 5 (Sept4.psi_nt n (ring 6) hpr w hw1 hw7) (Sept4.psi_inv n (ring 6) hpr w hw1 hw7)
  have hF := Quint18.gauss_frob' (Cyclo7.cyc7 (ring 6)) ((2 * (7 * n + 6)) + 1) hpr 5 CubicSeven.seven_prime (Sept5.charq7 5) (Sept4.psi n (ring 6) w) (Sept4.psi_mul n (ring 6) hpr w hw1 hw7) (Q7_ne n)
  rw [CharSumGen.gauss_Gv, CharSumGen.gauss_Gv] at hF
  have hneg : (Sept4.psi n (ring 6) w) (CharSumGen.iv (2 * (7 * n + 6)) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = (Cyclo7.cyc7 (ring 6)).one := by rw [JacRefl.iv_neg1 (2 * (7 * n + 6)) hpr, psi_neg1 n (ring 6) hpr w hw1 hw7]
  have e1 : gt (Cyclo7.cyc7 (ring 6)) (2 * (7 * n + 6) + 1 + 1) (pw (gring (Cyclo7.cyc7 (ring 6)) (2 * (7 * n + 6) + 1 + 1)) (CharSumGen.Gv (2 * (7 * n + 6)) (Sept4.psi n (ring 6) w)) (5 + 2)) (ring (2 * (7 * n + 6) + 1 + 1)).one
      = (Cyclo7.cyc7 (ring 6)).mul ((Cyclo7.cyc7 (ring 6)).mul (CharSumGen.jpr (Cyclo7.cyc7 (ring 6)) (2 * (7 * n + 6)) (Sept4.psi n (ring 6) w) 5) (Cyclo7.cyc7 (ring 6)).one) ((Cyclo7.cyc7 (ring 6)).neg (Cyclo7.cyc7 (ring 6)).one) := by
    rw [hG, hneg, JacobiNorm.mono0_mulg, CharSumGen.gt_X]
    unfold delta; rw [ite_neg' _ _ (PowerSum.one_ne_zero' _)]; lib_ring
  have e2 : gt (Cyclo7.cyc7 (ring 6)) (2 * (7 * n + 6) + 1 + 1) (pw (gring (Cyclo7.cyc7 (ring 6)) (2 * (7 * n + 6) + 1 + 1)) (CharSumGen.Gv (2 * (7 * n + 6)) (Sept4.psi n (ring 6) w)) (5 + 2)) (ring (2 * (7 * n + 6) + 1 + 1)).one
      = (Cyclo7.cyc7 (ring 6)).mul (pw (Cyclo7.cyc7 (ring 6)) ((Sept4.psi n (ring 6) w) (pw (ring (2 * (7 * n + 6) + 1 + 1)) (nsm (ring (2 * (7 * n + 6) + 1 + 1)) (5 + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).one) (2 * (7 * n + 6) + 1))) (5 + 1 + 1)) (pw (Cyclo7.cyc7 (ring 6)) ((Sept4.psi n (ring 6) w) (ring (2 * (7 * n + 6) + 1 + 1)).one) (5 + 1 + 1)) := by
    rw [show pw (gring (Cyclo7.cyc7 (ring 6)) (2 * (7 * n + 6) + 1 + 1)) (CharSumGen.Gv (2 * (7 * n + 6)) (Sept4.psi n (ring 6) w)) (5 + 2) = pw (gring (Cyclo7.cyc7 (ring 6)) (2 * (7 * n + 6) + 1 + 1)) (CharSumGen.Gv (2 * (7 * n + 6)) (Sept4.psi n (ring 6) w)) (5 + 1 + 1) from rfl, hF, JacobiNorm.mono0_mulg]
    unfold CharSumGen.Gv; rw [gt_tb]
  have hinv := CharSumGen.iv_mul (2 * (7 * n + 6)) hpr (nsm (ring (2 * (7 * n + 6) + 1 + 1)) (5 + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).one) (Q7_ne n)
  have hQi0 : (pw (ring (2 * (7 * n + 6) + 1 + 1)) (nsm (ring (2 * (7 * n + 6) + 1 + 1)) (5 + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).one) (2 * (7 * n + 6) + 1)) ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' ((2 * (7 * n + 6)) + 1) (by
    calc (ring (2 * (7 * n + 6) + 1 + 1)).one = (ring (2 * (7 * n + 6) + 1 + 1)).mul (pw (ring (2 * (7 * n + 6) + 1 + 1)) (nsm (ring (2 * (7 * n + 6) + 1 + 1)) (5 + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).one) (2 * (7 * n + 6) + 1)) (nsm (ring (2 * (7 * n + 6) + 1 + 1)) (5 + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).one) := hinv.symm
      _ = (ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).zero (nsm (ring (2 * (7 * n + 6) + 1 + 1)) (5 + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).one) := by rw [e]
      _ = (ring (2 * (7 * n + 6) + 1 + 1)).zero := by lib_ring)
  have h5 : pw (Cyclo7.cyc7 (ring 6)) ((Sept4.psi n (ring 6) w) (pw (ring (2 * (7 * n + 6) + 1 + 1)) (nsm (ring (2 * (7 * n + 6) + 1 + 1)) (5 + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).one) (2 * (7 * n + 6) + 1))) (5 + 1 + 1) = (Cyclo7.cyc7 (ring 6)).one := psi7 n (ring 6) hpr w hw1 hw7 (pw (ring (2 * (7 * n + 6) + 1 + 1)) (nsm (ring (2 * (7 * n + 6) + 1 + 1)) (5 + 1 + 1) (ring (2 * (7 * n + 6) + 1  -- (+39 chars; full line in the Zenodo source)
  have h1 : pw (Cyclo7.cyc7 (ring 6)) ((Sept4.psi n (ring 6) w) (ring (2 * (7 * n + 6) + 1 + 1)).one) (5 + 1 + 1) = (Cyclo7.cyc7 (ring 6)).one := by rw [Sept4.psi_one n (ring 6) hpr w hw1 hw7]; exact CubicChar.pw_one'' (Cyclo7.cyc7 (ring 6)) _
  have hA := e1.symm.trans e2
  rw [h5, h1] at hA
  calc CharSumGen.jpr (Cyclo7.cyc7 (ring 6)) (2 * (7 * n + 6)) (Sept4.psi n (ring 6) w) 5 = (Cyclo7.cyc7 (ring 6)).neg ((Cyclo7.cyc7 (ring 6)).mul ((Cyclo7.cyc7 (ring 6)).mul (CharSumGen.jpr (Cyclo7.cyc7 (ring 6)) (2 * (7 * n + 6)) (Sept4.psi n (ring 6) w) 5) (Cyclo7.cyc7 (ring 6)).one) ((Cyclo7.cyc7 (ring 6)).neg (Cyclo7.cyc7 (ring 6)).one)) := by lib_ring
    _ = (Cyclo7.cyc7 (ring 6)).neg ((Cyclo7.cyc7 (ring 6)).mul (Cyclo7.cyc7 (ring 6)).one (Cyclo7.cyc7 (ring 6)).one) := by rw [hA]
    _ = (Cyclo7.cyc7 (ring 6)).neg (Cyclo7.cyc7 (ring 6)).one := by lib_ring

end Sept6

#print axioms Sept6.Q7_ne
#print axioms Sept6.psi7
#print axioms Sept6.psi_neg1
#print axioms Sept6.sept_primary

#print axioms: 4 theorems, no axioms.

78.201 The septic product through three Jacobi sums

The product of Jacobi sums in the septic residue criterion reduces to three Jacobi sums.

\[ A=\prod _{i\le 5}J(\psi ^i,\psi )=J(\psi ,\psi )^2\,J(\psi ^2,\psi ^4)^2\,J(\psi ^3,\psi ^3). \]

lang/Sept7.lean

/-
  lang/Sept7 — T49, cycle 777 (ℓ = 7, step 7): **the septic product A in terms of three Jacobi sums**.
  For ψ = ph7 ∘ χ (Sept4, any T) with ψ(−1) = 1 and ψ⁷ = 1 off 0, JacRefl gives J(ψ^a, ψ^b) = J(ψ^a, ψ^c) with c ≡ −(a + b) (mod 7):
    mu_gen          ψ^a(x⁻¹) ψ^b(x⁻¹) = ψ^c(x)   when 6(a + b) = 7t + c
    J_2_1, J_3_1, J_4_1, J_1_5   J(ψ², ψ) = J(ψ², ψ⁴), J(ψ³, ψ) = J(ψ³, ψ³), J(ψ⁴, ψ) = J(ψ⁴, ψ²), J(ψ, ψ⁵) = J(ψ, ψ)
    sept_A          A = ∏_{i≤5} J(ψ^i, ψ) = J(ψ, ψ)² · J(ψ², ψ⁴)² · J(ψ³, ψ³)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Sept6
import lang.Quint10

namespace Sept7

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

variable (n : Nat) {R : Type} (T : CRing R)

theorem psp_mul (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+423 chars; full line in the Zenodo source)
  unfold CharSumGen.psp; rw [Sept4.psi_mul n T hpr w hw1 hw7, pw_mul]
theorem psp_zero (w : Zp (2 * (7 * n + 6) + 1 + 1)) (k : Nat) : (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) (k + 1)) (ring (2 * (7 * n + 6) + 1 + 1)).zero = (Cyclo7.cyc7 T).zero := by
  unfold CharSumGen.psp; rw [Sept4.psi_zero]; exact HasseCM.pw_zero_succ (Cyclo7.cyc7 T) k
theorem psp_one (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+230 chars; full line in the Zenodo source)
  unfold CharSumGen.psp; rw [Sept4.psi_one n T hpr w hw1 hw7]; exact CubicChar.pw_one'' (Cyclo7.cyc7 T) k
theorem psp_neg1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+270 chars; full line in the Zenodo source)
  unfold CharSumGen.psp; rw [Sept6.psi_neg1 n T hpr w hw1 hw7]; exact CubicChar.pw_one'' (Cyclo7.cyc7 T) k

theorem mu_gen (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+182 chars; full line in the Zenodo source)
    (Cyclo7.cyc7 T).mul ((CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) (a + 1)) (CharSumGen.iv (2 * (7 * n + 6)) x)) ((CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) (b + 1)) (CharSumGen.iv (2 * (7 * n + 6)) x)) = (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) (c + 1)) x := by
  unfold CharSumGen.psp
  by_cases hx : x = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hx, CharSumGen.iv_zero, Sept4.psi_zero, HasseCM.pw_zero_succ, HasseCM.pw_zero_succ, HasseCM.pw_zero_succ]; lib_ring
  have h7 : pw (Cyclo7.cyc7 T) ((Sept4.psi n T w) x) 7 = (Cyclo7.cyc7 T).one := Sept6.psi7 n T hpr w hw1 hw7 x hx
  rw [← Sept4.psi_inv n T hpr w hw1 hw7, TwoPower.pwpow, TwoPower.pwpow, ← HasseCM.pw_add',
    show (5 + 1) * (a + 1) + (5 + 1) * (b + 1) = 7 * t + (c + 1) from (show (5 + 1) * (a + 1) + (5 + 1) * (b + 1) = 6 * ((a + 1) + (b + 1)) by lib_nat).trans h,
    HasseCM.pw_add', ← TwoPower.pwpow, h7, CubicChar.pw_one'', (Cyclo7.cyc7 T).one_mul]

theorem J_1_psi (w : Zp (2 * (7 * n + 6) + 1 + 1)) : (CharSumGen.J2 (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1) (Sept4.psi n T w)) = (CharSumGen.J2 (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1)) := by
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 T) _ _ (fun t => by unfold CharSumGen.psp; rw [show pw (Cyclo7.cyc7 T) ((Sept4.psi n T w) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) 1 = (Sept4.psi n T w) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)) from (Cyclo7.cyc7 T).on  -- (+11 chars; full line in the Zenodo source)

theorem J_k_psi (w : Zp (2 * (7 * n + 6) + 1 + 1)) (k : Nat) : (CharSumGen.J2 (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) k) (Sept4.psi n T w)) = (CharSumGen.J2 (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) k) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1)) := b  -- (+1 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 T) _ _ (fun t => by unfold CharSumGen.psp; rw [show pw (Cyclo7.cyc7 T) ((Sept4.psi n T w) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) 1 = (Sept4.psi n T w) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)) from (Cyclo7.cyc7 T).on  -- (+11 chars; full line in the Zenodo source)

theorem J_2_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+474 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 T) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 2) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1) (psp_mul n T hpr w hw1 hw7 2) (psp_mul n T hpr w hw1 hw7 1) (psp_one n T hpr w hw1 hw7 2) (psp_zero n T w 0)
    (psp_zero n T w 1), psp_neg1 n T hpr w hw1 hw7 2, (Cyclo7.cyc7 T).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 T) _ _ (fun t => congrArg ((Cyclo7.cyc7 T).mul ((CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 2) t)) (mu_gen n T hpr w hw1 hw7 1 0 3 2 rfl _)) _

theorem J_3_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+474 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 T) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 3) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1) (psp_mul n T hpr w hw1 hw7 3) (psp_mul n T hpr w hw1 hw7 1) (psp_one n T hpr w hw1 hw7 3) (psp_zero n T w 0)
    (psp_zero n T w 2), psp_neg1 n T hpr w hw1 hw7 3, (Cyclo7.cyc7 T).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 T) _ _ (fun t => congrArg ((Cyclo7.cyc7 T).mul ((CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 3) t)) (mu_gen n T hpr w hw1 hw7 2 0 2 3 rfl _)) _

theorem J_4_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+474 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 T) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 4) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1) (psp_mul n T hpr w hw1 hw7 4) (psp_mul n T hpr w hw1 hw7 1) (psp_one n T hpr w hw1 hw7 4) (psp_zero n T w 0)
    (psp_zero n T w 3), psp_neg1 n T hpr w hw1 hw7 4, (Cyclo7.cyc7 T).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 T) _ _ (fun t => congrArg ((Cyclo7.cyc7 T).mul ((CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 4) t)) (mu_gen n T hpr w hw1 hw7 3 0 1 4 rfl _)) _

theorem J_1_5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+474 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 T) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 5) (psp_mul n T hpr w hw1 hw7 1) (psp_mul n T hpr w hw1 hw7 5) (psp_one n T hpr w hw1 hw7 1) (psp_zero n T w 4)
    (psp_zero n T w 0), psp_neg1 n T hpr w hw1 hw7 1, (Cyclo7.cyc7 T).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 T) _ _ (fun t => congrArg ((Cyclo7.cyc7 T).mul ((CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1) t)) (mu_gen n T hpr w hw1 hw7 0 4 0 5 rfl _)) _

/-- **A = J(ψ,ψ)² J(ψ²,ψ⁴)² J(ψ³,ψ³).** -/
theorem sept_A (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+154 chars; full line in the Zenodo source)
    (Cyclo7.cyc7 T).mul ((Cyclo7.cyc7 T).mul ((Cyclo7.cyc7 T).mul (CharSumGen.J2 (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1)) (CharSumGen.J2 (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1) (CharSumGen.psp (Cyclo  -- (+655 chars; full line in the Zenodo source)
  show (Cyclo7.cyc7 T).mul ((Cyclo7.cyc7 T).mul ((Cyclo7.cyc7 T).mul ((Cyclo7.cyc7 T).mul ((Cyclo7.cyc7 T).mul (Cyclo7.cyc7 T).one (CharSumGen.J2 (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1) (Sept4.psi n T w))) (CharSumGen.J2 (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 2) (Sept4.ps  -- (+433 chars; full line in the Zenodo source)
  rw [J_1_psi n T w, J_k_psi n T w 2, J_k_psi n T w 3, J_k_psi n T w 4, J_k_psi n T w 5, J_2_1 n T hpr w hw1 hw7, J_3_1 n T hpr w hw1 hw7, J_4_1 n T hpr w hw1 hw7,
    Quint10.j2_swap (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 4) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 2), Quint10.j2_swap (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 5) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) 1), J_1_5 n T   -- (+14 chars; full line in the Zenodo source)
  lib_ring

end Sept7
#print axioms Sept7.psp_mul
#print axioms Sept7.psp_zero
#print axioms Sept7.psp_one
#print axioms Sept7.psp_neg1
#print axioms Sept7.mu_gen
#print axioms Sept7.J_1_psi
#print axioms Sept7.J_k_psi
#print axioms Sept7.J_2_1
#print axioms Sept7.J_3_1
#print axioms Sept7.J_4_1
#print axioms Sept7.J_1_5
#print axioms Sept7.sept_A

#print axioms: 12 theorems, no axioms.

78.202 Galois action on the septic Jacobi sums

The automorphisms \(\sigma _2,\sigma _3\) of \(\mathbb Z[\zeta _7]\) express \(A\) through \(J=J(\chi ,\chi )\) and \(J'=J(\chi ,\chi ^2)\).

\[ A=\varphi _7\big (J^2\,\sigma _2(J')^2\,\sigma _3(J)\big ). \]

lang/Sept8.lean

/-
  lang/Sept8 — T49, cycle 778 (ℓ = 7, step 8): **σ₂, σ₃ on ℤ[ζ₇] and A = ph7(J² (σ₂J')² σ₃J)** (Quint11 pattern, generated by s8gen.py).
    sig2, sig3 (_mul, _add, _sum, _pow)   σ_k : ζ ↦ ζ^k (k = 2, 3) are ring maps of ℤ[ζ₇] in the coordinates a₀ … a₅ (ζ⁶ = −Σ_(i<6) ζ^i)
    sig2_chi, sig2_chi2, sig3_chi   σ₂ χ = χ², σ₂ χ² = χ⁴, σ₃ χ = χ³
    sig2_J, sig3_J                  σ₂ J(χ, χ²) = J(χ², χ⁴),  σ₃ J(χ, χ) = J(χ³, χ³)
    J1C, JJ                         J(χ¹, χ¹) = J(χ, χ),  J(ψ^a, ψ^b) = ph7 J(χ^a, χ^b)
    sept_A'                         A = ∏_(i≤5) J(ψ^i, ψ) = ph7(J² · (σ₂J')² · σ₃J),  J = J(χ, χ), J' = J(χ, χ²) ∈ ℤ[ζ₇]
  So the septic residue criteria (Sept5) are statements about the two Jacobi sums J(χ, χ), J(χ, χ²) ∈ ℤ[ζ₇].
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Sept7

namespace Sept8

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CharSumGen

def sig2 (z : Int × Int × Int × Int × Int × Int) : Int × Int × Int × Int × Int × Int := ((z.1 + (-z.2.2.2.1)), (z.2.2.2.2.1 + (-z.2.2.2.1)), (z.2.1 + (-z.2.2.2.1)), (z.2.2.2.2.2 + (-z.2.2.2.1)), (z.2.2.1 + (-z.2.2.2.1)), (-z.2.2.2.1))

theorem sig2_mul : ∀ x y : Int × Int × Int × Int × Int × Int, sig2 ((Cyclo7.cyc7 LibInt.intR).mul x y) = (Cyclo7.cyc7 LibInt.intR).mul (sig2 x) (sig2 y)
  | (a0, a1, a2, a3, a4, a5), (b0, b1, b2, b3, b4, b5) => by
    apply Prod.ext
    · show ((((a0 * b0) + ((((a2 * b5) + (a3 * b4)) + (a4 * b3)) + (a5 * b2))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-((((((a0 * b3) + (a1 * b2)) + (a2 * b1)) + (a3 * b0)) + (a5 * b5)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = ((((a0 + (-a3)) * (b0 + (-b3))) + (((((a1 + (-a3)) * (-b3)) + ((a5 + (-a3)) * (b2 + (-b3)))) + ((a2 +   -- (+208 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show (((((((a0 * b4) + (a1 * b3)) + (a2 * b2)) + (a3 * b1)) + (a4 * b0)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-((((((a0 * b3) + (a1 * b2)) + (a2 * b1)) + (a3 * b0)) + (a5 * b5)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = (((((a0 + (-a3)) * (b4 + (-b3))) + ((a4 + (-a3)) * (b0 + (-b3)))) + ((((a5 + (-a3)) * (-b3)) + ((a2 +   -- (+208 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show (((((a0 * b1) + (a1 * b0)) + (((a3 * b5) + (a4 * b4)) + (a5 * b3))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-((((((a0 * b3) + (a1 * b2)) + (a2 * b1)) + (a3 * b0)) + (a5 * b5)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = ((((((a0 + (-a3)) * (b1 + (-b3))) + ((a4 + (-a3)) * (b4 + (-b3)))) + ((a1 + (-a3)) * (b0 + (-b3)))) +   -- (+208 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show ((((((((a0 * b5) + (a1 * b4)) + (a2 * b3)) + (a3 * b2)) + (a4 * b1)) + (a5 * b0)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-((((((a0 * b3) + (a1 * b2)) + (a2 * b1)) + (a3 * b0)) + (a5 * b5)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = (((((((a0 + (-a3)) * (b5 + (-b3))) + ((a4 + (-a3)) * (b1 + (-b3)))) + ((a1 + (-a3)) * (b  -- (+222 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show ((((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + ((a4 * b5) + (a5 * b4))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-((((((a0 * b3) + (a1 * b2)) + (a2 * b1)) + (a3 * b0)) + (a5 * b5)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = (((((((a0 + (-a3)) * (b2 + (-b3))) + ((a4 + (-a3)) * (b5 + (-b3)))) + ((a1 + (-a3)) * (b1 + (-b3)))) +  -- (+222 chars; full line in the Zenodo source)
      lib_int
    · show (-((((((a0 * b3) + (a1 * b2)) + (a2 * b1)) + (a3 * b0)) + (a5 * b5)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1))))) = ((((((((a0 + (-a3)) * (-b3)) + ((a4 + (-a3)) * (b2 + (-b3)))) + ((a1 + (-a3)) * (b5 + (-b3)))) + ((a5 + (-a3)) * (b1 + (-b3)))) + ((a2 + (-a3)) * (b4 + (-b3)))) + ((-a3) * (b0 + (-b3)))) + (-((((((a4 + (-a3)) * (-b3)) + ((a1 + (-a3)) * (b2 + (-b  -- (+99 chars; full line in the Zenodo source)
      lib_int

theorem sig2_add : ∀ x y : Int × Int × Int × Int × Int × Int, sig2 ((Cyclo7.cyc7 LibInt.intR).add x y) = (Cyclo7.cyc7 LibInt.intR).add (sig2 x) (sig2 y)
  | (a0, a1, a2, a3, a4, a5), (b0, b1, b2, b3, b4, b5) => by
    apply Prod.ext
    · show ((a0 + b0) + (-(a3 + b3))) = ((a0 + (-a3)) + (b0 + (-b3)))
      lib_int
    apply Prod.ext
    · show ((a4 + b4) + (-(a3 + b3))) = ((a4 + (-a3)) + (b4 + (-b3)))
      lib_int
    apply Prod.ext
    · show ((a1 + b1) + (-(a3 + b3))) = ((a1 + (-a3)) + (b1 + (-b3)))
      lib_int
    apply Prod.ext
    · show ((a5 + b5) + (-(a3 + b3))) = ((a5 + (-a3)) + (b5 + (-b3)))
      lib_int
    apply Prod.ext
    · show ((a2 + b2) + (-(a3 + b3))) = ((a2 + (-a3)) + (b2 + (-b3)))
      lib_int
    · show (-(a3 + b3)) = ((-a3) + (-b3))
      lib_int

theorem sig2_sum {A' : Type} (f : A' → Int × Int × Int × Int × Int × Int) : ∀ l : List A', sig2 (sumK (Cyclo7.cyc7 LibInt.intR) f l) = sumK (Cyclo7.cyc7 LibInt.intR) (fun a => sig2 (f a)) l
  | [] => rfl
  | a :: l => by
    show sig2 ((Cyclo7.cyc7 LibInt.intR).add (f a) (sumK (Cyclo7.cyc7 LibInt.intR) f l)) = (Cyclo7.cyc7 LibInt.intR).add (sig2 (f a)) (sumK (Cyclo7.cyc7 LibInt.intR) (fun a => sig2 (f a)) l)
    rw [sig2_add, sig2_sum f l]

theorem sig2_pow (z : Int × Int × Int × Int × Int × Int) : ∀ m, sig2 (pw (Cyclo7.cyc7 LibInt.intR) z m) = pw (Cyclo7.cyc7 LibInt.intR) (sig2 z) m
  | 0 => rfl
  | m + 1 => by
    show sig2 ((Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) z m) z) = (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (sig2 z) m) (sig2 z)
    rw [sig2_mul, sig2_pow z m]

def sig3 (z : Int × Int × Int × Int × Int × Int) : Int × Int × Int × Int × Int × Int := ((z.1 + (-z.2.2.1)), (z.2.2.2.2.2 + (-z.2.2.1)), (z.2.2.2.1 + (-z.2.2.1)), (z.2.1 + (-z.2.2.1)), (-z.2.2.1), (z.2.2.2.2.1 + (-z.2.2.1)))

theorem sig3_mul : ∀ x y : Int × Int × Int × Int × Int × Int, sig3 ((Cyclo7.cyc7 LibInt.intR).mul x y) = (Cyclo7.cyc7 LibInt.intR).mul (sig3 x) (sig3 y)
  | (a0, a1, a2, a3, a4, a5), (b0, b1, b2, b3, b4, b5) => by
    apply Prod.ext
    · show ((((a0 * b0) + ((((a2 * b5) + (a3 * b4)) + (a4 * b3)) + (a5 * b2))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-(((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + ((a4 * b5) + (a5 * b4))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = ((((a0 + (-a2)) * (b0 + (-b2))) + (((((a3 + (-a2)) * (b4 + (-b2))) + ((a1 + (-a2)) * (-b2))) + ((-a2)   -- (+208 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show ((((((((a0 * b5) + (a1 * b4)) + (a2 * b3)) + (a3 * b2)) + (a4 * b1)) + (a5 * b0)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-(((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + ((a4 * b5) + (a5 * b4))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = (((((a0 + (-a2)) * (b5 + (-b2))) + ((a5 + (-a2)) * (b0 + (-b2)))) + ((((a1 + (-a2)) * (b  -- (+222 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show (((((((a0 * b3) + (a1 * b2)) + (a2 * b1)) + (a3 * b0)) + (a5 * b5)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-(((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + ((a4 * b5) + (a5 * b4))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = ((((((a0 + (-a2)) * (b3 + (-b2))) + ((a5 + (-a2)) * (b5 + (-b2)))) + ((a3 + (-a2)) * (b0 + (-b2)))) +   -- (+208 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show (((((a0 * b1) + (a1 * b0)) + (((a3 * b5) + (a4 * b4)) + (a5 * b3))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-(((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + ((a4 * b5) + (a5 * b4))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = (((((((a0 + (-a2)) * (b1 + (-b2))) + ((a5 + (-a2)) * (b3 + (-b2)))) + ((a3 + (-a2)) * (b5 + (-b2)))) +  -- (+222 chars; full line in the Zenodo source)
      lib_int
    apply Prod.ext
    · show (-(((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + ((a4 * b5) + (a5 * b4))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1))))) = (((((((a0 + (-a2)) * (-b2)) + ((a5 + (-a2)) * (b1 + (-b2)))) + ((a3 + (-a2)) * (b3 + (-b2)))) + ((a1 + (-a2)) * (b5 + (-b2)))) + ((-a2) * (b0 + (-b2)))) + (-((((((a5 + (-a2)) * (b4 + (-b2))) + ((a3 + (-a2)) * (-b2))) + ((a1 + (-a2)) * (b1 + (-b2  -- (+65 chars; full line in the Zenodo source)
      lib_int
    · show (((((((a0 * b4) + (a1 * b3)) + (a2 * b2)) + (a3 * b1)) + (a4 * b0)) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))) + (-(((((a0 * b2) + (a1 * b1)) + (a2 * b0)) + ((a4 * b5) + (a5 * b4))) + (-(((((a1 * b5) + (a2 * b4)) + (a3 * b3)) + (a4 * b2)) + (a5 * b1)))))) = ((((((((a0 + (-a2)) * (b4 + (-b2))) + ((a5 + (-a2)) * (-b2))) + ((a3 + (-a2)) * (b1 + (-b2)))) + ((a1   -- (+242 chars; full line in the Zenodo source)
      lib_int

theorem sig3_add : ∀ x y : Int × Int × Int × Int × Int × Int, sig3 ((Cyclo7.cyc7 LibInt.intR).add x y) = (Cyclo7.cyc7 LibInt.intR).add (sig3 x) (sig3 y)
  | (a0, a1, a2, a3, a4, a5), (b0, b1, b2, b3, b4, b5) => by
    apply Prod.ext
    · show ((a0 + b0) + (-(a2 + b2))) = ((a0 + (-a2)) + (b0 + (-b2)))
      lib_int
    apply Prod.ext
    · show ((a5 + b5) + (-(a2 + b2))) = ((a5 + (-a2)) + (b5 + (-b2)))
      lib_int
    apply Prod.ext
    · show ((a3 + b3) + (-(a2 + b2))) = ((a3 + (-a2)) + (b3 + (-b2)))
      lib_int
    apply Prod.ext
    · show ((a1 + b1) + (-(a2 + b2))) = ((a1 + (-a2)) + (b1 + (-b2)))
      lib_int
    apply Prod.ext
    · show (-(a2 + b2)) = ((-a2) + (-b2))
      lib_int
    · show ((a4 + b4) + (-(a2 + b2))) = ((a4 + (-a2)) + (b4 + (-b2)))
      lib_int

theorem sig3_sum {A' : Type} (f : A' → Int × Int × Int × Int × Int × Int) : ∀ l : List A', sig3 (sumK (Cyclo7.cyc7 LibInt.intR) f l) = sumK (Cyclo7.cyc7 LibInt.intR) (fun a => sig3 (f a)) l
  | [] => rfl
  | a :: l => by
    show sig3 ((Cyclo7.cyc7 LibInt.intR).add (f a) (sumK (Cyclo7.cyc7 LibInt.intR) f l)) = (Cyclo7.cyc7 LibInt.intR).add (sig3 (f a)) (sumK (Cyclo7.cyc7 LibInt.intR) (fun a => sig3 (f a)) l)
    rw [sig3_add, sig3_sum f l]

theorem sig3_pow (z : Int × Int × Int × Int × Int × Int) : ∀ m, sig3 (pw (Cyclo7.cyc7 LibInt.intR) z m) = pw (Cyclo7.cyc7 LibInt.intR) (sig3 z) m
  | 0 => rfl
  | m + 1 => by
    show sig3 ((Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) z m) z) = (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (sig3 z) m) (sig3 z)
    rw [sig3_mul, sig3_pow z m]

variable (n : Nat)

theorem sig2_chi (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+236 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  by_cases hx : x = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hx, Sept2.chi7_zero]; rfl
  match Sept2.exists_i n hpr w hw1 hw7 x hx with
  | ⟨i, hi, ei⟩ =>
    rw [Sept2.val_of n hpr w hw1 hw7 x hx i hi ei]
    match i, hi with
    | 0, _ => rfl
    | 1, _ => rfl
    | 2, _ => rfl
    | 3, _ => rfl
    | 4, _ => rfl
    | 5, _ => rfl
    | 6, _ => rfl
    | k + 7, hk => exact absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))

theorem sig2_chi2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+300 chars; full line in the Zenodo source)
  show sig2 (pw (Cyclo7.cyc7 LibInt.intR) ((Sept2.chi7 n w) x) 2) = pw (Cyclo7.cyc7 LibInt.intR) ((Sept2.chi7 n w) x) 4
  rw [sig2_pow, sig2_chi n hpr w hw1 hw7 x]
  show pw (Cyclo7.cyc7 LibInt.intR) (pw (Cyclo7.cyc7 LibInt.intR) ((Sept2.chi7 n w) x) 2) 2 = pw (Cyclo7.cyc7 LibInt.intR) ((Sept2.chi7 n w) x) 4
  exact TwoPower.pwpow _ _ 2 2

theorem sig3_chi (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+236 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  by_cases hx : x = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hx, Sept2.chi7_zero]; rfl
  match Sept2.exists_i n hpr w hw1 hw7 x hx with
  | ⟨i, hi, ei⟩ =>
    rw [Sept2.val_of n hpr w hw1 hw7 x hx i hi ei]
    match i, hi with
    | 0, _ => rfl
    | 1, _ => rfl
    | 2, _ => rfl
    | 3, _ => rfl
    | 4, _ => rfl
    | 5, _ => rfl
    | 6, _ => rfl
    | k + 7, hk => exact absurd hk (fun h => Nat.lt_irrefl 7 (Nat.lt_of_le_of_lt (Nat.le_add_left 7 k) h))

theorem sig2_J (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+473 chars; full line in the Zenodo source)
  unfold CharSumGen.J2; rw [sig2_sum]
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => by rw [sig2_mul, sig2_chi n hpr w hw1 hw7, sig2_chi2 n hpr w hw1 hw7]) _

theorem sig3_J (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+410 chars; full line in the Zenodo source)
  unfold CharSumGen.J2; rw [sig3_sum]
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => by rw [sig3_mul, sig3_chi n hpr w hw1 hw7, sig3_chi n hpr w hw1 hw7]) _

theorem pw1 (z : Int × Int × Int × Int × Int × Int) : pw (Cyclo7.cyc7 LibInt.intR) z 1 = z := (Cyclo7.cyc7 LibInt.intR).one_mul z

theorem J1C (w : Zp (2 * (7 * n + 6) + 1 + 1)) : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)) := by
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => by unfold CharSumGen.psp; rw [pw1, pw1]) _

variable {R : Type} (T : CRing R)

theorem JJ (w : Zp (2 * (7 * n + 6) + 1 + 1)) (a b : Nat) : (CharSumGen.J2 (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) a) (CharSumGen.psp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) (Sept4.psi n T w) b)) = Sept4.ph7 T (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w  -- (+91 chars; full line in the Zenodo source)
  unfold CharSumGen.J2; rw [Sept4.ph7_sum]
  exact sumK_ext (Cyclo7.cyc7 T) _ _ (fun t => by unfold CharSumGen.psp; rw [Sept4.ph7_mul, ← Sept4.ph7_pow, ← Sept4.ph7_pow]; rfl) _

/-- **A = ph7(J² (σ₂J')² σ₃J)**, J = J(χ, χ), J' = J(χ, χ²) ∈ ℤ[ζ₇]. -/
theorem sept_A' (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+918 chars; full line in the Zenodo source)
  rw [Sept7.sept_A n T hpr w hw1 hw7, JJ n T w 1 1, JJ n T w 2 4, JJ n T w 3 3, J1C n w, ← sig2_J n hpr w hw1 hw7, ← sig3_J n hpr w hw1 hw7,
    Sept4.ph7_mul, Sept4.ph7_mul, Sept4.ph7_mul, Sept4.ph7_mul]

end Sept8

#print axioms Sept8.sig2_mul
#print axioms Sept8.sig2_add
#print axioms Sept8.sig2_sum
#print axioms Sept8.sig2_pow
#print axioms Sept8.sig3_mul
#print axioms Sept8.sig3_add
#print axioms Sept8.sig3_sum
#print axioms Sept8.sig3_pow
#print axioms Sept8.sig2_chi
#print axioms Sept8.sig2_chi2
#print axioms Sept8.sig3_chi
#print axioms Sept8.sig2_J
#print axioms Sept8.sig3_J
#print axioms Sept8.pw1
#print axioms Sept8.J1C
#print axioms Sept8.JJ
#print axioms Sept8.sept_A'

#print axioms: 17 theorems, no axioms.

78.203 Septic residue criterion through two Jacobi sums

The septic residue criterion at every prime \(q\neq 7,p\) in terms of \(J\) and \(J'\).

\[ q^6=7M+1\ \Rightarrow \ \psi (q^{-6})=\big (\varphi _7(J^2\sigma _2(J')^2\sigma _3(J))\,p\big )^M\ \text {in}\ \mathbb F_q[\zeta _7]. \]

lang/Sept9.lean

/-
  lang/Sept9 — T49, cycle 779 (ℓ = 7, step 9): **the septic residue criterion through the two Jacobi sums J = J(χ, χ), J' = J(χ, χ²)
  in ℤ[ζ₇]** (Quint12 pattern): for every prime q ≠ 7, p with q⁶ = 7M + 1,
    sept_J_all    ψ(q⁻⁶) = (ph7(J² · (σ₂J')² · σ₃J) · p)^M     in 𝔽_q[ζ₇]
  (Sept5.sept_recip_all with Sept8.sept_A').
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Sept5
import lang.Sept8

namespace Sept9

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

variable (n k : Nat)

/-- **every prime q ≠ 7, p** (q⁶ = 7M + 1): ψ(q⁻⁶) = (ph7(J² (σ₂J')² σ₃J) p)^M in 𝔽_q[ζ₇]. -/
theorem sept_J_all (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *  -- (+328 chars; full line in the Zenodo source)
    (Sept4.psi n (ring (k + 1)) w) (pw (ring (2 * (7 * n + 6) + 1 + 1)) (nsm (ring (2 * (7 * n + 6) + 1 + 1)) ((k + 1 + 1) ^ 6) (ring (2 * (7 * n + 6) + 1 + 1)).one) (2 * (7 * n + 6) + 1)) = pw (Cyclo7.cyc7 (ring (k + 1))) ((Cyclo7.cyc7 (ring (k + 1))).mul (Sept4.ph7 (ring (k + 1)) ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.J2 (Cyclo7.c  -- (+718 chars; full line in the Zenodo source)
  rw [Sept5.sept_recip_all n k hpr w hw1 hw7 hq hQ M hM, Sept8.sept_A' n (ring (k + 1)) hpr w hw1 hw7]

end Sept9

#print axioms Sept9.sept_J_all

#print axioms: 1 theorem, no axioms.

78.204 Septic split case in symbol form

At \(q\equiv 1\pmod 7\) with a root \(u\in \mathbb F_q\) of \(\Phi _7\), the septic character of \(q\) is a product of seventh power symbols of \(J,J'\).

\[ \Phi _7(u)=0\ \Rightarrow \ \mathrm {ev}_u\,\psi (q^{-6})=\big (J(u)^3J'(u^2)^2J(u^3)J(u^6)\big )^M\ \text {in}\ \mathbb F_q. \]

lang/Sept10.lean

/-
  lang/Sept10 — T49, cycle 780 (ℓ = 7, step 10): **the septic residue criterion in symbol form** (Quint13 pattern, generated by s10gen.py).
  For u ∈ T with Φ₇(u) = 1 + u + … + u⁶ = 0 (T any commutative ring), ev_u(a₀ + a₁ζ + … + a₅ζ⁵) = Σ aᵢuⁱ is a ring map T[ζ₇] → T
  (ev_mul: generated certificate (Π − ev) = Q·Φ₇(u)), and ev_u ∘ σ_k = ev_(u^k) for σ₂, σ₃, conj = σ₆ (aux_sig*, ev_sig*).
  On Sept9.sept_J_all with p = J·conj(J) (Sept3.sept_norm), for a prime q = k + 2 ≠ 7, p, q⁶ = 7M + 1, and a root u ∈ 𝔽_q of Φ₇
  (q ≡ 1 mod 7, u ↔ a prime π | q of ℤ[ζ₇]):
      sept_sym      ev_u ψ(q⁻⁶) = (J(u)³ · J'(u²)² · J(u³) · J(u⁶))^M       in 𝔽_q,   J = J(χ, χ), J' = J(χ, χ²).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Sept9

namespace Sept10

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius
open LibRing (castZ castN)

section Gen
variable {R : Type} (T : CRing R) (u : R)

def ev (v : R) (x : R × R × R × R × R × R) : R := (T.add (T.add (T.add (T.add (T.add x.1 (T.mul x.2.1 v)) (T.mul x.2.2.1 (T.mul v v))) (T.mul x.2.2.2.1 (T.mul (T.mul v v) v))) (T.mul x.2.2.2.2.1 (T.mul (T.mul (T.mul v v) v) v))) (T.mul x.2.2.2.2.2 (T.mul (T.mul (T.mul (T.mul v v) v) v) v)))

theorem ev_mul (hu : (T.add (T.add (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u)) (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u)) = T.zero) : ∀ x y : R × R × R × R × R × R, ev T u ((Cyclo7.cyc7 T).mul x y) = T.mul (ev T u x) (ev T u y)
  | (a0, a1, a2, a3, a4, a5), (b0, b1, b2, b3, b4, b5) => by
    show (T.add (T.add (T.add (T.add (T.add (T.add (T.add (T.mul a0 b0) (T.add (T.add (T.add (T.mul a2 b5) (T.mul a3 b4)) (T.mul a4 b3)) (T.mul a5 b2))) (T.neg (T.add (T.add (T.add (T.add (T.mul a1 b5) (T.mul a2 b4)) (T.mul a3 b3)) (T.mul a4 b2)) (T.mul a5 b1)))) (T.mul (T.add (T.add (T.add (T.mul a0 b1) (T.mul a1 b0)) (T.add (T.add (T.mul a3 b5) (T.mul a4 b4)) (T.mul a5 b3))) (T.neg (T.add (T.add  -- (+1564 chars; full line in the Zenodo source)
    calc (T.add (T.add (T.add (T.add (T.add (T.add (T.add (T.mul a0 b0) (T.add (T.add (T.add (T.mul a2 b5) (T.mul a3 b4)) (T.mul a4 b3)) (T.mul a5 b2))) (T.neg (T.add (T.add (T.add (T.add (T.mul a1 b5) (T.mul a2 b4)) (T.mul a3 b3)) (T.mul a4 b2)) (T.mul a5 b1)))) (T.mul (T.add (T.add (T.add (T.mul a0 b1) (T.mul a1 b0)) (T.add (T.add (T.mul a3 b5) (T.mul a4 b4)) (T.mul a5 b3))) (T.neg (T.add (T.add  -- (+2773 chars; full line in the Zenodo source)
      _ = T.mul (T.add (T.add (T.add (T.add (T.add a0 (T.mul a1 u)) (T.mul a2 (T.mul u u))) (T.mul a3 (T.mul (T.mul u u) u))) (T.mul a4 (T.mul (T.mul (T.mul u u) u) u))) (T.mul a5 (T.mul (T.mul (T.mul (T.mul u u) u) u) u))) (T.add (T.add (T.add (T.add (T.add b0 (T.mul b1 u)) (T.mul b2 (T.mul u u))) (T.mul b3 (T.mul (T.mul u u) u))) (T.mul b4 (T.mul (T.mul (T.mul u u) u) u))) (T.mul b5 (T.mul (T.mu  -- (+55 chars; full line in the Zenodo source)

theorem ev_one (v : R) : ev T v (Cyclo7.cyc7 T).one = T.one := by
  show (T.add (T.add (T.add (T.add (T.add T.one (T.mul T.zero v)) (T.mul T.zero (T.mul v v))) (T.mul T.zero (T.mul (T.mul v v) v))) (T.mul T.zero (T.mul (T.mul (T.mul v v) v) v))) (T.mul T.zero (T.mul (T.mul (T.mul (T.mul v v) v) v) v))) = T.one
  lib_ring

theorem ev_pow (hu : (T.add (T.add (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u)) (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u)) = T.zero) (x : R × R × R × R × R × R) : ∀ k, ev T u (pw (Cyclo7.cyc7 T) x k) = pw T (ev T u x) k
  | 0 => ev_one T u
  | k + 1 => by
    show ev T u ((Cyclo7.cyc7 T).mul (pw (Cyclo7.cyc7 T) x k) x) = T.mul (pw T (ev T u x) k) (ev T u x)
    rw [ev_mul T u hu, ev_pow hu x k]

theorem evph_mul (hu : (T.add (T.add (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u)) (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u)) = T.zero) (a b : Int × Int × Int × Int × Int × Int) :
    ev T u (Sept4.ph7 T ((Cyclo7.cyc7 LibInt.intR).mul a b)) = T.mul (ev T u (Sept4.ph7 T a)) (ev T u (Sept4.ph7 T b)) := by
  rw [Sept4.ph7_mul, ev_mul T u hu]

theorem aux_sig2 (hu : (T.add (T.add (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u)) (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u)) = T.zero) (c0 c1 c2 c3 c4 c5 : R) : (T.add (T.add (T.add (T.add (T.add (T.add c0 (T.neg c3)) (T.mul (T.add c4 (T.neg c3)) u)) (T.mul (T.add c1 (T.neg c3)) (T.mul  -- (+551 chars; full line in the Zenodo source)
  calc (T.add (T.add (T.add (T.add (T.add (T.add c0 (T.neg c3)) (T.mul (T.add c4 (T.neg c3)) u)) (T.mul (T.add c1 (T.neg c3)) (T.mul u u))) (T.mul (T.add c5 (T.neg c3)) (T.mul (T.mul u u) u))) (T.mul (T.add c2 (T.neg c3)) (T.mul (T.mul (T.mul u u) u) u))) (T.mul (T.neg c3) (T.mul (T.mul (T.mul (T.mul u u) u) u) u))) = T.add (T.add (T.add (T.add (T.add (T.add c0 (T.mul c1 (T.mul u u))) (T.mul c2 (T  -- (+693 chars; full line in the Zenodo source)
    _ = (T.add (T.add (T.add (T.add (T.add c0 (T.mul c1 (T.mul u u))) (T.mul c2 (T.mul (T.mul u u) (T.mul u u)))) (T.mul c3 (T.mul (T.mul (T.mul u u) (T.mul u u)) (T.mul u u)))) (T.mul c4 (T.mul (T.mul (T.mul (T.mul u u) (T.mul u u)) (T.mul u u)) (T.mul u u)))) (T.mul c5 (T.mul (T.mul (T.mul (T.mul (T.mul u u) (T.mul u u)) (T.mul u u)) (T.mul u u)) (T.mul u u)))) := by rw [hu]; lib_ring

theorem aux_sig3 (hu : (T.add (T.add (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u)) (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u)) = T.zero) (c0 c1 c2 c3 c4 c5 : R) : (T.add (T.add (T.add (T.add (T.add (T.add c0 (T.neg c2)) (T.mul (T.add c5 (T.neg c2)) u)) (T.mul (T.add c3 (T.neg c2)) (T.mul  -- (+701 chars; full line in the Zenodo source)
  calc (T.add (T.add (T.add (T.add (T.add (T.add c0 (T.neg c2)) (T.mul (T.add c5 (T.neg c2)) u)) (T.mul (T.add c3 (T.neg c2)) (T.mul u u))) (T.mul (T.add c1 (T.neg c2)) (T.mul (T.mul u u) u))) (T.mul (T.neg c2) (T.mul (T.mul (T.mul u u) u) u))) (T.mul (T.add c4 (T.neg c2)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u))) = T.add (T.add (T.add (T.add (T.add (T.add c0 (T.mul c1 (T.mul (T.mul u u) u))) (T  -- (+1163 chars; full line in the Zenodo source)
    _ = (T.add (T.add (T.add (T.add (T.add c0 (T.mul c1 (T.mul (T.mul u u) u))) (T.mul c2 (T.mul (T.mul (T.mul u u) u) (T.mul (T.mul u u) u)))) (T.mul c3 (T.mul (T.mul (T.mul (T.mul u u) u) (T.mul (T.mul u u) u)) (T.mul (T.mul u u) u)))) (T.mul c4 (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) (T.mul (T.mul u u) u)) (T.mul (T.mul u u) u)) (T.mul (T.mul u u) u)))) (T.mul c5 (T.mul (T.mul (T.mul (T.mul   -- (+139 chars; full line in the Zenodo source)

theorem aux_sig6 (hu : (T.add (T.add (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u)) (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u)) = T.zero) (c0 c1 c2 c3 c4 c5 : R) : (T.add (T.add (T.add (T.add (T.add (T.add c0 (T.neg c1)) (T.mul (T.neg c1) u)) (T.mul (T.add c5 (T.neg c1)) (T.mul u u))) (T.  -- (+1151 chars; full line in the Zenodo source)
  calc (T.add (T.add (T.add (T.add (T.add (T.add c0 (T.neg c1)) (T.mul (T.neg c1) u)) (T.mul (T.add c5 (T.neg c1)) (T.mul u u))) (T.mul (T.add c4 (T.neg c1)) (T.mul (T.mul u u) u))) (T.mul (T.add c3 (T.neg c1)) (T.mul (T.mul (T.mul u u) u) u))) (T.mul (T.add c2 (T.neg c1)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u))) = T.add (T.add (T.add (T.add (T.add (T.add c0 (T.mul c1 (T.mul (T.mul (T.mul (T.mu  -- (+3533 chars; full line in the Zenodo source)
    _ = (T.add (T.add (T.add (T.add (T.add c0 (T.mul c1 (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u))) (T.mul c2 (T.mul (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u) (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u)))) (T.mul c3 (T.mul (T.mul (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u) (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u)) (T.mul (T.mul (T.mul (T.mul (T.mul u   -- (+589 chars; full line in the Zenodo source)

theorem ev_sig2 (hu : (T.add (T.add (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u)) (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u)) = T.zero) : ∀ z : Int × Int × Int × Int × Int × Int, ev T u (Sept4.ph7 T (Sept8.sig2 z)) = ev T (T.mul u u) (Sept4.ph7 T z)
  | (a0, a1, a2, a3, a4, a5) => by
    show (T.add (T.add (T.add (T.add (T.add (castZ T (a0 + -a3)) (T.mul (castZ T (a4 + -a3)) u)) (T.mul (castZ T (a1 + -a3)) (T.mul u u))) (T.mul (castZ T (a5 + -a3)) (T.mul (T.mul u u) u))) (T.mul (castZ T (a2 + -a3)) (T.mul (T.mul (T.mul u u) u) u))) (T.mul (castZ T (-a3)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u))) = (T.add (T.add (T.add (T.add (T.add (castZ T a0) (T.mul (castZ T a1) (T.mul u u  -- (+339 chars; full line in the Zenodo source)
    repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    exact aux_sig2 T u hu _ _ _ _ _ _

theorem ev_sig3 (hu : (T.add (T.add (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u)) (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u)) = T.zero) : ∀ z : Int × Int × Int × Int × Int × Int, ev T u (Sept4.ph7 T (Sept8.sig3 z)) = ev T (T.mul (T.mul u u) u) (Sept4.ph7 T z)
  | (a0, a1, a2, a3, a4, a5) => by
    show (T.add (T.add (T.add (T.add (T.add (castZ T (a0 + -a2)) (T.mul (castZ T (a5 + -a2)) u)) (T.mul (castZ T (a3 + -a2)) (T.mul u u))) (T.mul (castZ T (a1 + -a2)) (T.mul (T.mul u u) u))) (T.mul (castZ T (-a2)) (T.mul (T.mul (T.mul u u) u) u))) (T.mul (castZ T (a4 + -a2)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u))) = (T.add (T.add (T.add (T.add (T.add (castZ T a0) (T.mul (castZ T a1) (T.mul (T.  -- (+489 chars; full line in the Zenodo source)
    repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    exact aux_sig3 T u hu _ _ _ _ _ _

theorem ev_sig6 (hu : (T.add (T.add (T.add (T.add (T.add (T.add T.one u) (T.mul u u)) (T.mul (T.mul u u) u)) (T.mul (T.mul (T.mul u u) u) u)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u)) (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u)) = T.zero) : ∀ z : Int × Int × Int × Int × Int × Int, ev T u (Sept4.ph7 T (Sept3.conj7 z)) = ev T (T.mul (T.mul (T.mul (T.mul (T.mul u u) u) u) u) u) (Sept4.ph7 T   -- (+2 chars; full line in the Zenodo source)
  | (a0, a1, a2, a3, a4, a5) => by
    show (T.add (T.add (T.add (T.add (T.add (castZ T (a0 + -a1)) (T.mul (castZ T (-a1)) u)) (T.mul (castZ T (a5 + -a1)) (T.mul u u))) (T.mul (castZ T (a4 + -a1)) (T.mul (T.mul u u) u))) (T.mul (castZ T (a3 + -a1)) (T.mul (T.mul (T.mul u u) u) u))) (T.mul (castZ T (a2 + -a1)) (T.mul (T.mul (T.mul (T.mul u u) u) u) u))) = (T.add (T.add (T.add (T.add (T.add (castZ T a0) (T.mul (castZ T a1) (T.mul (T.  -- (+939 chars; full line in the Zenodo source)
    repeat (first | rw [IntCast.castZ_add] | rw [HasseDavenport.castZ_neg] | rw [HasseDavenport.castZ_mul])
    exact aux_sig6 T u hu _ _ _ _ _ _
end Gen

variable (n k : Nat)

theorem Pp_eq (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+409 chars; full line in the Zenodo source)
  have h1 : CharSumGen.Pp (Cyclo7.cyc7 T) (2 * (7 * n + 6)) = Sept4.ph7 T (CharSumGen.Pp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))) := by
    unfold CharSumGen.Pp; rw [Sept4.ph7_sum]
    exact sumK_ext (Cyclo7.cyc7 T) _ _ (fun _ => (Sept4.ph7_one T).symm) _
  have h2 : CharSumGen.Pp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) = (Int.ofNat (2 * (7 * n + 6) + 1 + 1 + 1), 0, 0, 0, 0, 0) := Sept3.sum_one7 n _
  have h3 : (Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)) (Sept3.conj7 (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w))) = (Int.ofNat (2 * (7 * n + 6) + 1 + 1 + 1), 0, 0, 0, 0, 0) := Sept3.sept_norm n hpr w hw1 hw7
  rw [h1, h2, h3]

/-- **Septic residue criterion in symbol form** (q ≡ 1 mod 7, u a root of Φ₇ in 𝔽_q): ev_u ψ(q⁻⁶) = (J(u)³ J'(u²)² J(u³) J(u⁶))^M. -/
theorem sept_sym (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+322 chars; full line in the Zenodo source)
    (u : Zp (k + 1)) (hu : ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add ((ring (k + 1)).add (ring (k + 1)).one u) ((ring (k + 1)).mul u u)) ((ring (k + 1)).mul ((ring (k + 1)).mul u u) u)) ((ring (k + 1)).mul ((ring (k + 1)).mul ((ring (k + 1)).mul u u) u) u)) ((ring (k + 1)).mul ((ring (k + 1)).mul ((ring (k + 1)).mul ((ring (k + 1)).mul u u)  -- (+153 chars; full line in the Zenodo source)
    ev (ring (k + 1)) u (Sept4.psi n (ring (k + 1)) w (pw (ring (2 * (7 * n + 6) + 1 + 1)) (nsm (ring (2 * (7 * n + 6) + 1 + 1)) ((k + 1 + 1) ^ 6) (ring (2 * (7 * n + 6) + 1 + 1)).one) (2 * (7 * n + 6) + 1)))
      = pw (ring (k + 1)) ((ring (k + 1)).mul ((ring (k + 1)).mul ((ring (k + 1)).mul ((ring (k + 1)).mul ((ring (k + 1)).mul (ev (ring (k + 1)) u (Sept4.ph7 (ring (k + 1)) (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)))) (ev (ring (k + 1)) u (Sept4.ph7 (ring (k + 1)) (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.c  -- (+1093 chars; full line in the Zenodo source)
  have h := Sept9.sept_J_all n k hpr w hw1 hw7 hq hQ M hM
  rw [Pp_eq n hpr w hw1 hw7 (ring (k + 1)), ← Sept4.ph7_mul] at h
  have e := congrArg (ev (ring (k + 1)) u) h
  rw [ev_pow (ring (k + 1)) u hu] at e
  repeat rw [evph_mul (ring (k + 1)) u hu] at e
  rw [ev_sig2 (ring (k + 1)) u hu, ev_sig3 (ring (k + 1)) u hu, ev_sig6 (ring (k + 1)) u hu] at e
  have hx : ((ring (k + 1)).mul ((ring (k + 1)).mul ((ring (k + 1)).mul ((ring (k + 1)).mul (ev (ring (k + 1)) u (Sept4.ph7 (ring (k + 1)) (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)))) (ev (ring (k + 1)) u (Sept4.ph7 (ring (k + 1)) (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w))))) ((ring (k + 1)).mul (  -- (+2548 chars; full line in the Zenodo source)
  rw [e, hx]

end Sept10

#print axioms Sept10.ev_mul
#print axioms Sept10.ev_one
#print axioms Sept10.ev_pow
#print axioms Sept10.evph_mul
#print axioms Sept10.aux_sig2
#print axioms Sept10.aux_sig3
#print axioms Sept10.aux_sig6
#print axioms Sept10.ev_sig2
#print axioms Sept10.ev_sig3
#print axioms Sept10.ev_sig6
#print axioms Sept10.Pp_eq
#print axioms Sept10.sept_sym

#print axioms: 12 theorems, no axioms.

78.205 Septic symbol form at every prime

Over any ring \(K\) receiving \(\mathbb F_q\) and containing a root \(u\) of \(\Phi _7\), the septic character of \(q\) in terms of \(J,J'\).

\[ q^6=7M+1,\ \Phi _7(u)=0\ \Rightarrow \ \mathrm {ev}_u\,\iota \,\psi (q^{-6})=\big (J(u)^3J'(u^2)^2J(u^3)J(u^6)\big )^M\ \text {in}\ K. \]

lang/Sept11.lean

/-
  lang/Sept11 — T49, cycle 781 (ℓ = 7, step 11): **the septic residue criterion in symbol form at every prime q ≠ 7, p**
  (Quint14 pattern, generated by s11gen.py), over any commutative ring K receiving 𝔽_q (ring map ι) and containing a root u of Φ₇
  (e.g. 𝔽_(q^f), f the order of q mod 7, the residue field of a prime of ℤ[ζ₇] above q):
    map7, map7_mul, map7_pow     ι extended coordinatewise is a ring map T[ζ₇] → K[ζ₇]
    iota_castN, iota_castZ       ι ∘ castZ_T = castZ_K, so map7 ∘ ph7_T = ph7_K
    sept_sym_all                 q⁶ = 7M + 1:  ev_u ι ψ(q⁻⁶) = (J(u)³ · J'(u²)² · J(u³) · J(u⁶))^M     in K,
  J = J(χ, χ), J' = J(χ, χ²) (Sept9.sept_J_all, p = J conj(J), Sept10: ev_u ∘ σ_k = ev_(u^k)).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Sept10

namespace Sept11

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius
open LibRing (castZ castN)

section Gen
variable {R K' : Type} (T : CRing R) (K : CRing K') (ι : R → K')

def map7 (x : R × R × R × R × R × R) : K' × K' × K' × K' × K' × K' := (ι x.1, ι x.2.1, ι x.2.2.1, ι x.2.2.2.1, ι x.2.2.2.2.1, ι x.2.2.2.2.2)

theorem map7_mul (hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι (T.neg a) = K.neg (ι a)) (_hone : ι T.one = K.one) (_hzero : ι T.zero = K.zero) : ∀ x y : R × R × R × R × R × R, map7 ι ((Cyclo7.cyc7 T).mul x y) = (Cyclo7.cyc7 K).mul (map7 ι x) (map7 ι y)
  | (a0, a1, a2, a3, a4, a5), (b0, b1, b2, b3, b4, b5) => by
    apply Prod.ext
    · show ι (T.add (T.add (T.mul a0 b0) (T.add (T.add (T.add (T.mul a2 b5) (T.mul a3 b4)) (T.mul a4 b3)) (T.mul a5 b2))) (T.neg (T.add (T.add (T.add (T.add (T.mul a1 b5) (T.mul a2 b4)) (T.mul a3 b3)) (T.mul a4 b2)) (T.mul a5 b1)))) = (K.add (K.add (K.mul (ι a0) (ι b0)) (K.add (K.add (K.add (K.mul (ι a2) (ι b5)) (K.mul (ι a3) (ι b4))) (K.mul (ι a4) (ι b3))) (K.mul (ι a5) (ι b2)))) (K.neg (K.add (K  -- (+134 chars; full line in the Zenodo source)
      repeat (first | rw [hadd] | rw [hneg] | rw [hmul])
    apply Prod.ext
    · show ι (T.add (T.add (T.add (T.mul a0 b1) (T.mul a1 b0)) (T.add (T.add (T.mul a3 b5) (T.mul a4 b4)) (T.mul a5 b3))) (T.neg (T.add (T.add (T.add (T.add (T.mul a1 b5) (T.mul a2 b4)) (T.mul a3 b3)) (T.mul a4 b2)) (T.mul a5 b1)))) = (K.add (K.add (K.add (K.mul (ι a0) (ι b1)) (K.mul (ι a1) (ι b0))) (K.add (K.add (K.mul (ι a3) (ι b5)) (K.mul (ι a4) (ι b4))) (K.mul (ι a5) (ι b3)))) (K.neg (K.add (K  -- (+134 chars; full line in the Zenodo source)
      repeat (first | rw [hadd] | rw [hneg] | rw [hmul])
    apply Prod.ext
    · show ι (T.add (T.add (T.add (T.add (T.mul a0 b2) (T.mul a1 b1)) (T.mul a2 b0)) (T.add (T.mul a4 b5) (T.mul a5 b4))) (T.neg (T.add (T.add (T.add (T.add (T.mul a1 b5) (T.mul a2 b4)) (T.mul a3 b3)) (T.mul a4 b2)) (T.mul a5 b1)))) = (K.add (K.add (K.add (K.add (K.mul (ι a0) (ι b2)) (K.mul (ι a1) (ι b1))) (K.mul (ι a2) (ι b0))) (K.add (K.mul (ι a4) (ι b5)) (K.mul (ι a5) (ι b4)))) (K.neg (K.add (K  -- (+134 chars; full line in the Zenodo source)
      repeat (first | rw [hadd] | rw [hneg] | rw [hmul])
    apply Prod.ext
    · show ι (T.add (T.add (T.add (T.add (T.add (T.mul a0 b3) (T.mul a1 b2)) (T.mul a2 b1)) (T.mul a3 b0)) (T.mul a5 b5)) (T.neg (T.add (T.add (T.add (T.add (T.mul a1 b5) (T.mul a2 b4)) (T.mul a3 b3)) (T.mul a4 b2)) (T.mul a5 b1)))) = (K.add (K.add (K.add (K.add (K.add (K.mul (ι a0) (ι b3)) (K.mul (ι a1) (ι b2))) (K.mul (ι a2) (ι b1))) (K.mul (ι a3) (ι b0))) (K.mul (ι a5) (ι b5))) (K.neg (K.add (K  -- (+134 chars; full line in the Zenodo source)
      repeat (first | rw [hadd] | rw [hneg] | rw [hmul])
    apply Prod.ext
    · show ι (T.add (T.add (T.add (T.add (T.add (T.mul a0 b4) (T.mul a1 b3)) (T.mul a2 b2)) (T.mul a3 b1)) (T.mul a4 b0)) (T.neg (T.add (T.add (T.add (T.add (T.mul a1 b5) (T.mul a2 b4)) (T.mul a3 b3)) (T.mul a4 b2)) (T.mul a5 b1)))) = (K.add (K.add (K.add (K.add (K.add (K.mul (ι a0) (ι b4)) (K.mul (ι a1) (ι b3))) (K.mul (ι a2) (ι b2))) (K.mul (ι a3) (ι b1))) (K.mul (ι a4) (ι b0))) (K.neg (K.add (K  -- (+134 chars; full line in the Zenodo source)
      repeat (first | rw [hadd] | rw [hneg] | rw [hmul])
    · show ι (T.add (T.add (T.add (T.add (T.add (T.add (T.mul a0 b5) (T.mul a1 b4)) (T.mul a2 b3)) (T.mul a3 b2)) (T.mul a4 b1)) (T.mul a5 b0)) (T.neg (T.add (T.add (T.add (T.add (T.mul a1 b5) (T.mul a2 b4)) (T.mul a3 b3)) (T.mul a4 b2)) (T.mul a5 b1)))) = (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (ι a0) (ι b5)) (K.mul (ι a1) (ι b4))) (K.mul (ι a2) (ι b3))) (K.mul (ι a3) (ι b2))) (K.mul (ι   -- (+186 chars; full line in the Zenodo source)
      repeat (first | rw [hadd] | rw [hneg] | rw [hmul])

theorem map7_one (_hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (_hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (_hneg : ∀ a, ι (T.neg a) = K.neg (ι a)) (hone : ι T.one = K.one) (hzero : ι T.zero = K.zero) : map7 ι (Cyclo7.cyc7 T).one = (Cyclo7.cyc7 K).one := by
  show (ι T.one, ι T.zero, ι T.zero, ι T.zero, ι T.zero, ι T.zero) = (K.one, K.zero, K.zero, K.zero, K.zero, K.zero)
  rw [hone, hzero]

theorem map7_pow (hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι (T.neg a) = K.neg (ι a)) (hone : ι T.one = K.one) (hzero : ι T.zero = K.zero) (x : R × R × R × R × R × R) : ∀ k, map7 ι (pw (Cyclo7.cyc7 T) x k) = pw (Cyclo7.cyc7 K) (map7 ι x) k
  | 0 => map7_one T K ι hadd hmul hneg hone hzero
  | k + 1 => by
    show map7 ι ((Cyclo7.cyc7 T).mul (pw (Cyclo7.cyc7 T) x k) x) = (Cyclo7.cyc7 K).mul (pw (Cyclo7.cyc7 K) (map7 ι x) k) (map7 ι x)
    rw [map7_mul T K ι hadd hmul hneg hone hzero, map7_pow hadd hmul hneg hone hzero x k]

theorem iota_castN (hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι (T.neg a) = K.neg (ι a)) (hone : ι T.one = K.one) (hzero : ι T.zero = K.zero) : ∀ k, ι (castN T k) = castN K k
  | 0 => hzero
  | k + 1 => by
    show ι (T.add T.one (castN T k)) = K.add K.one (castN K k)
    rw [hadd, hone, iota_castN hadd hmul hneg hone hzero k]

theorem iota_castZ (hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι (T.neg a) = K.neg (ι a)) (hone : ι T.one = K.one) (hzero : ι T.zero = K.zero) : ∀ z : Int, ι (castZ T z) = castZ K z
  | .ofNat k => iota_castN T K ι hadd hmul hneg hone hzero k
  | .negSucc k => by
    show ι (T.neg (castN T (k + 1))) = K.neg (castN K (k + 1))
    rw [hneg, iota_castN T K ι hadd hmul hneg hone hzero]

theorem map7_ph7 (hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι (T.neg a) = K.neg (ι a)) (hone : ι T.one = K.one) (hzero : ι T.zero = K.zero) (z : Int × Int × Int × Int × Int × Int) : map7 ι (Sept4.ph7 T z) = Sept4.ph7 K z := by
  show (ι (castZ T z.1), ι (castZ T z.2.1), ι (castZ T z.2.2.1), ι (castZ T z.2.2.2.1), ι (castZ T z.2.2.2.2.1), ι (castZ T z.2.2.2.2.2)) = (castZ K z.1, castZ K z.2.1, castZ K z.2.2.1, castZ K z.2.2.2.1, castZ K z.2.2.2.2.1, castZ K z.2.2.2.2.2)
  rw [iota_castZ T K ι hadd hmul hneg hone hzero, iota_castZ T K ι hadd hmul hneg hone hzero, iota_castZ T K ι hadd hmul hneg hone hzero, iota_castZ T K ι hadd hmul hneg hone hzero, iota_castZ T K ι hadd hmul hneg hone hzero, iota_castZ T K ι hadd hmul hneg hone hzero]
end Gen

variable (n k : Nat)

/-- **Septic symbol form at every prime q ≠ 7, p**: ev_u ι ψ(q⁻⁶) = (J(u)³ J'(u²)² J(u³) J(u⁶))^M in any K ⊇ 𝔽_q with Φ₇(u) = 0. -/
theorem sept_sym_all (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2  -- (+326 chars; full line in the Zenodo source)
    {K' : Type} (K : CRing K') (ι : Zp (k + 1) → K') (hadd : ∀ a b, ι ((ring (k + 1)).add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι ((ring (k + 1)).mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι ((ring (k + 1)).neg a) = K.neg (ι a)) (hone : ι (ring (k + 1)).one = K.one) (hzero : ι (ring (k + 1)).zero = K.zero) (u : K') (hu : (K.add (K.add (K.add (K.add (K.add (K.add K.one u) (K.mul u u)) (K.mul (K.  -- (+153 chars; full line in the Zenodo source)
    Sept10.ev K u (map7 ι (Sept4.psi n (ring (k + 1)) w (pw (ring (2 * (7 * n + 6) + 1 + 1)) (nsm (ring (2 * (7 * n + 6) + 1 + 1)) ((k + 1 + 1) ^ 6) (ring (2 * (7 * n + 6) + 1 + 1)).one) (2 * (7 * n + 6) + 1)))) = pw K (K.mul (K.mul (K.mul (K.mul (K.mul (Sept10.ev K u (Sept4.ph7 K (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)))) (Sept10.ev K u (Sept4.  -- (+958 chars; full line in the Zenodo source)
  have h := Sept9.sept_J_all n k hpr w hw1 hw7 hq hQ M hM
  rw [Sept10.Pp_eq n hpr w hw1 hw7 (ring (k + 1)), ← Sept4.ph7_mul] at h
  have hx : (K.mul (K.mul (K.mul (K.mul (Sept10.ev K u (Sept4.ph7 K (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)))) (Sept10.ev K u (Sept4.ph7 K (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w))))) (K.mul (Sept10.ev K (K.mul u u) (Sept4.ph7 K (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.  -- (+1892 chars; full line in the Zenodo source)
  rw [h, map7_pow (ring (k + 1)) K ι hadd hmul hneg hone hzero, map7_ph7 (ring (k + 1)) K ι hadd hmul hneg hone hzero, Sept10.ev_pow K u hu]
  repeat rw [Sept10.evph_mul K u hu]
  rw [Sept10.ev_sig2 K u hu, Sept10.ev_sig3 K u hu, Sept10.ev_sig6 K u hu, hx]

end Sept11

#print axioms Sept11.map7_mul
#print axioms Sept11.map7_one
#print axioms Sept11.map7_pow
#print axioms Sept11.iota_castN
#print axioms Sept11.iota_castZ
#print axioms Sept11.map7_ph7
#print axioms Sept11.sept_sym_all

#print axioms: 7 theorems, no axioms.

78.206 The second septic Jacobi sum is fixed by \(\sigma _2\)

The Jacobi sum \(J'=J(\chi ,\chi ^2)\) is fixed by \(\sigma _2\), so it lies in \(\mathbb Q(\sqrt {-7})\), and \(A\) simplifies.

\[ \sigma _2(J')=J',\qquad A=\varphi _7\big (J^2J'^2\sigma _3(J)\big ). \]

lang/Sept12.lean

/-
  lang/Sept12 — T49, cycle 782 (ℓ = 7, step 12): **J' = J(χ, χ²) is fixed by σ₂**, so J' lies in the quadratic subfield ℚ(√−7)
  of ℚ(ζ₇) (the fixed field of ⟨σ₂⟩ = {1, σ₂, σ₄}), and A = ph7(J² · J'² · σ₃J).
    add1, castN_int, castZ_int, ph7_int    castZ on ℤ is the identity, so ph7 over ℤ is the identity (Sept4.ph7 LibInt.intR z = z)
    sig2_JY                                σ₂ J(χ, χ²) = J(χ², χ⁴) = J(χ², χ) = J(χ, χ²)
                                           (Sept8.sig2_J; Sept7.J_2_1 at T = ℤ via Sept8.JJ and ph7_int; Quint10.j2_swap)
    sept_A''                               A = ∏_(i≤5) J(ψ^i, ψ) = ph7(J² · J'² · σ₃J)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Sept8

namespace Sept12

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius
open LibRing (castZ castN)

theorem add1 : ∀ k : Nat, 1 + k = k + 1
  | 0 => rfl
  | k + 1 => congrArg Nat.succ (add1 k)

theorem castN_int : ∀ k : Nat, castN LibInt.intR k = Int.ofNat k
  | 0 => rfl
  | k + 1 => by
    show Int.add 1 (castN LibInt.intR k) = Int.ofNat (k + 1)
    rw [castN_int k]
    show Int.ofNat (1 + k) = Int.ofNat (k + 1)
    rw [add1 k]

theorem castZ_int : ∀ z : Int, castZ LibInt.intR z = z
  | .ofNat k => castN_int k
  | .negSucc k => by
    show Int.neg (castN LibInt.intR (k + 1)) = Int.negSucc k
    rw [castN_int (k + 1)]
    rfl

theorem ph7_int : ∀ z : Int × Int × Int × Int × Int × Int, Sept4.ph7 LibInt.intR z = z
  | (a0, a1, a2, a3, a4, a5) => by
    show (castZ LibInt.intR a0, castZ LibInt.intR a1, castZ LibInt.intR a2, castZ LibInt.intR a3, castZ LibInt.intR a4, castZ LibInt.intR a5) = (a0, a1, a2, a3, a4, a5)
    rw [castZ_int a0, castZ_int a1, castZ_int a2, castZ_int a3, castZ_int a4, castZ_int a5]

variable (n : Nat)

/-- **σ₂ J(χ, χ²) = J(χ, χ²)**. -/
theorem sig2_JY (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+417 chars; full line in the Zenodo source)
  rw [Sept8.sig2_J n hpr w hw1 hw7]
  have e1 : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept4.psi n LibInt.intR w) 2) (CharSumGen.psp   -- (+124 chars; full line in the Zenodo source)
  have e2 : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept4.psi n LibInt.intR w) 1) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept4.psi n LibInt.intR w) 2)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (Char  -- (+124 chars; full line in the Zenodo source)
  rw [e1, ← Sept7.J_2_1 n LibInt.intR hpr w hw1 hw7, Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept4.psi n LibInt.intR w) 2) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept4.psi n LibInt.intR w) 1), e2]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => by unfold CharSumGen.psp; rw [Sept8.pw1]) _

/-- **A = ph7(J² J'² σ₃J)**, J = J(χ, χ), J' = J(χ, χ²) = σ₂J'. -/
theorem sept_A'' (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+936 chars; full line in the Zenodo source)
  rw [Sept8.sept_A' n T hpr w hw1 hw7, sig2_JY n hpr w hw1 hw7]

end Sept12

#print axioms Sept12.add1
#print axioms Sept12.castN_int
#print axioms Sept12.castZ_int
#print axioms Sept12.ph7_int
#print axioms Sept12.sig2_JY
#print axioms Sept12.sept_A''

#print axioms: 6 theorems, no axioms.

78.207 The second septic Jacobi sum has norm \(p\)

The Jacobi sum \(J'=J(\chi ,\chi ^2)\) has norm \(p\), from \(G_\chi G_{\chi ^2}=J'G_{\chi ^3}\).

\[ J'\,\overline {J'}=p. \]

lang/Sept13.lean

/-
  lang/Sept13 — T49, cycle 783 (ℓ = 7, step 13): **the second septic Jacobi sum has norm p**: J' = J(χ, χ²) ∈ ℤ[ζ₇] satisfies
  J' · conj(J') = p (generated by s13gen.py). With κ_k = χ^k and κ̄_k = χ^k ∘ ι (ι(x) = x^(p−2)), in ℤ[ζ₇][C_p]:
    gg12, gb12      G_κ₁ G_κ₂ = J(κ₁, κ₂) G_κ₃,  G_κ̄₁ G_κ̄₂ = J(κ̄₁, κ̄₂) G_κ̄₃       (CharSumGen.jacobi_gauss, Σ χ³ = 0)
    hX1, hX2, hX3   G_κ_k G_κ̄_k = X                                                   (Sept3.conv_X, χ(−1) = 1)
    so J(κ₁,κ₂) J(κ̄₁,κ̄₂) X = X·X = p X; with conj7_pow, J(κ̄₁, κ̄₂) = conj(J'):
    sept_norm2      J' · conj(J') = p
  Together with Sept12 (σ₂J' = J'), J' = (a + b√−7)/2 with a² + 7b² = 4p.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Sept12

namespace Sept13

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable (n : Nat)

def kap (k : Nat) (w x : Zp (2 * (7 * n + 6) + 1 + 1)) : Int × Int × Int × Int × Int × Int := pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w x) k
def kab (k : Nat) (w x : Zp (2 * (7 * n + 6) + 1 + 1)) : Int × Int × Int × Int × Int × Int := pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w ((CharSumGen.iv (2 * (7 * n + 6))) x)) k

theorem kap_mul (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+250 chars; full line in the Zenodo source)
  unfold kap; rw [Sept2.chi7_mul n hpr w hw1 hw7, pw_mul]
theorem kab_mul (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+250 chars; full line in the Zenodo source)
  unfold kab; rw [CharSumGen.iv_mulmul, Sept2.chi7_mul n hpr w hw1 hw7, pw_mul]
theorem kap_zero (w : Zp (2 * (7 * n + 6) + 1 + 1)) (k : Nat) : kap n (k + 1) w (ring (2 * (7 * n + 6) + 1 + 1)).zero = (Cyclo7.cyc7 LibInt.intR).zero := by
  unfold kap; rw [Sept2.chi7_zero]; exact HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) k
theorem kab_zero (w : Zp (2 * (7 * n + 6) + 1 + 1)) (k : Nat) : kab n (k + 1) w (ring (2 * (7 * n + 6) + 1 + 1)).zero = (Cyclo7.cyc7 LibInt.intR).zero := by
  unfold kab; rw [CharSumGen.iv_zero, Sept2.chi7_zero]; exact HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) k
theorem kap_one (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+179 chars; full line in the Zenodo source)
  unfold kap; rw [Sept3.chi_one n hpr w hw1 hw7]; exact CubicChar.pw_one'' (Cyclo7.cyc7 LibInt.intR) k
theorem kab_neg1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+219 chars; full line in the Zenodo source)
  unfold kab; rw [show Sept2.chi7 n w ((CharSumGen.iv (2 * (7 * n + 6))) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = (Cyclo7.cyc7 LibInt.intR).one from Sept3.chib_neg1 n hpr w hw1 hw7]; exact CubicChar.pw_one'' (Cyclo7.cyc7 LibInt.intR) k

theorem pw12 (z : Int × Int × Int × Int × Int × Int) : (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) z 1) (pw (Cyclo7.cyc7 LibInt.intR) z 2) = pw (Cyclo7.cyc7 LibInt.intR) z 3 := by
  show (Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (Cyclo7.cyc7 LibInt.intR).one z) ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (Cyclo7.cyc7 LibInt.intR).one z) z) = (Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (Cyclo7.cyc7 LibInt.intR).one z) z) z
  lib_ring

theorem kap_sum1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+198 chars; full line in the Zenodo source)
  (sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun a => Sept8.pw1 (Sept2.chi7 n w a)) _).trans (Sept2.chi7_sum n hpr w hw1 hw7)
theorem kap_sum2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+233 chars; full line in the Zenodo source)
theorem kap_sum3 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+233 chars; full line in the Zenodo source)
theorem kab_sum3 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+198 chars; full line in the Zenodo source)
  (reindex (Cyclo7.cyc7 LibInt.intR) _ (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR)) (CharSumGen.iv (2 * (7 * n + 6))) (CharSumGen.iv (2 * (7 * n + 6))) (CharSumGen.iv_iv (2 * (7 * n + 6)) hpr) (CharSumGen.iv_iv (2 * (7 * n + 6)) hpr) (kap n 3 w)).trans (kap_sum3 n hpr w hw1 hw7)

theorem gg12 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+523 chars; full line in the Zenodo source)
  ext (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) _ _ (fun c => by
    rw [CharSumGen.jacobi_gauss (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (kap n 1 w) (kap n 2 w) (kap_mul n hpr w hw1 hw7 1) (kap_mul n hpr w hw1 hw7 2) (kap_zero n w 0)
      ((sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun a => pw12 (Sept2.chi7 n w a)) _).trans (kap_sum3 n hpr w hw1 hw7)) c, JacobiNorm.mono0_mulg]
    unfold CharSumGen.Gv; rw [gt_tb]
    unfold kap; rw [pw12])

theorem gb12 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+523 chars; full line in the Zenodo source)
  ext (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) _ _ (fun c => by
    rw [CharSumGen.jacobi_gauss (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (kab n 1 w) (kab n 2 w) (kab_mul n hpr w hw1 hw7 1) (kab_mul n hpr w hw1 hw7 2) (kab_zero n w 0)
      ((sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun a => pw12 (Sept2.chi7 n w ((CharSumGen.iv (2 * (7 * n + 6))) a))) _).trans (kab_sum3 n hpr w hw1 hw7)) c, JacobiNorm.mono0_mulg]
    unfold CharSumGen.Gv; rw [gt_tb]
    unfold kab; rw [pw12])

theorem hX1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n  -- (+485 chars; full line in the Zenodo source)
  rw [Sept3.conv_X n hpr (kap n 1 w) (kab n 1 w) (kap_mul n hpr w hw1 hw7 1) (kab_mul n hpr w hw1 hw7 1) (kap_one n hpr w hw1 hw7 1) (kap_zero n w 0) (kap_sum1 n hpr w hw1 hw7) (fun _ => rfl), kab_neg1 n hpr w hw1 hw7 1]

theorem hX2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n  -- (+485 chars; full line in the Zenodo source)
  rw [Sept3.conv_X n hpr (kap n 2 w) (kab n 2 w) (kap_mul n hpr w hw1 hw7 2) (kab_mul n hpr w hw1 hw7 2) (kap_one n hpr w hw1 hw7 2) (kap_zero n w 1) (kap_sum2 n hpr w hw1 hw7) (fun _ => rfl), kab_neg1 n hpr w hw1 hw7 2]

theorem hX3 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n  -- (+485 chars; full line in the Zenodo source)
  rw [Sept3.conv_X n hpr (kap n 3 w) (kab n 3 w) (kap_mul n hpr w hw1 hw7 3) (kab_mul n hpr w hw1 hw7 3) (kap_one n hpr w hw1 hw7 3) (kap_zero n w 2) (kap_sum3 n hpr w hw1 hw7) (fun _ => rfl), kab_neg1 n hpr w hw1 hw7 3]

theorem conj7_pow (z : Int × Int × Int × Int × Int × Int) : ∀ k, Sept3.conj7 (pw (Cyclo7.cyc7 LibInt.intR) z k) = pw (Cyclo7.cyc7 LibInt.intR) (Sept3.conj7 z) k
  | 0 => rfl
  | k + 1 => by
    show Sept3.conj7 ((Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) z k) z) = (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept3.conj7 z) k) (Sept3.conj7 z)
    rw [Sept3.conj7_mul, conj7_pow z k]

theorem eJa (w : Zp (2 * (7 * n + 6) + 1 + 1)) : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (kap n 1 w) (kap n 2 w)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) := by
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => by unfold kap CharSumGen.psp; rw [Sept8.pw1]) _

theorem eJb (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n  -- (+341 chars; full line in the Zenodo source)
  unfold CharSumGen.J2; rw [Sept3.conj7_sum]
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => by
    unfold kab CharSumGen.psp
    rw [Sept3.conj7_mul, conj7_pow, Sept8.pw1, Sept3.chi_iv n hpr w hw1 hw7, Sept3.chi_iv n hpr w hw1 hw7]) _

/-- **N(J') = p**: J' = J(χ, χ²) ∈ ℤ[ζ₇] satisfies J' · conj(J') = p. -/
theorem sept_norm2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *  -- (+510 chars; full line in the Zenodo source)
  let G := (gring (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1))
  have h12 : (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 1 w)) (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 2 w))) = (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (kap n 1 w) (kap n 2 w))) (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 3 w))) := gg12 n hpr w hw1 hw7
  have hb : (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 1 w)) (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 2 w))) = (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (kab n 1 w) (kab n 2 w))) (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 3 w))) := gb12 n hpr w hw1 hw7
  have x1 : (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 1 w)) (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 1 w))) = (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (Cyclo7.cyc7 LibInt.intR).one) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)))) := hX1 n hpr w hw1 hw7
  have x2 : (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 2 w)) (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 2 w))) = (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (Cyclo7.cyc7 LibInt.intR).one) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)))) := hX2 n hpr w hw1 hw7
  have x3 : (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 3 w)) (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 3 w))) = (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (Cyclo7.cyc7 LibInt.intR).one) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)))) := hX3 n hpr w hw1 hw7
  have xx : (G.mul (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)))) = (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (CharSumGen.Pp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)))) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)))) := CharSumGen.XX (Cyclo7.cyc7 Lib  -- (+27 chars; full line in the Zenodo source)
  have m2 : ∀ s t, (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero s) (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero t)) = (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).mul s t)) := fun s t => by
    show gmul (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) _ _ = _; rw [mono_mul, show (ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).zero (ring (2 * (7 * n + 6) + 1 + 1)).zero = (ring (2 * (7 * n + 6) + 1 + 1)).zero by lib_ring]
  have A1 : (G.mul (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 1 w)) (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 2 w))) (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 1 w)) (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 2 w)))) = (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.J  -- (+254 chars; full line in the Zenodo source)
    rw [h12, hb, show (G.mul (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (kap n 1 w) (kap n 2 w))) (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 3 w))) (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (CharSumGen.J2 (Cyclo7.cyc7 LibIn  -- (+592 chars; full line in the Zenodo source)
      x3, m2, show (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (kap n 1 w) (kap n 2 w)) (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (kab n 1 w) (kab n 2 w)))) (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (  -- (+637 chars; full line in the Zenodo source)
  have A2 : (G.mul (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 1 w)) (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 1 w))) (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 2 w)) (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 2 w)))) = (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (Cyclo7.cyc7   -- (+176 chars; full line in the Zenodo source)
    rw [x1, x2, show (G.mul (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (Cyclo7.cyc7 LibInt.intR).one) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)))) (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (Cyclo7.cyc7 LibInt.intR).one) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2  -- (+431 chars; full line in the Zenodo source)
      m2, xx, show (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).mul (Cyclo7.cyc7 LibInt.intR).one (Cyclo7.cyc7 LibInt.intR).one)) (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero (CharSumGen.Pp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)))) (CharSumGen.Xv (Cyclo  -- (+481 chars; full line in the Zenodo source)
  have key : (G.mul (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (kap n 1 w) (kap n 2 w)) (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (kab n 1 w) (kab n 2 w))) (Cyclo7.cyc7 LibInt.intR).one)) (CharSumGen.Xv (Cyclo  -- (+393 chars; full line in the Zenodo source)
    A1.symm.trans ((show (G.mul (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 1 w)) (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 2 w))) (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 1 w)) (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 2 w)))) = (G.mul (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 1 w)) (CharSumGen.Gv (2 * (7 * n + 6)) (kab n 1 w))) (G.mul (CharSumGen.Gv (2 * (7 * n + 6)) (kap n 2 w)) (Char  -- (+65 chars; full line in the Zenodo source)
  have k0 := congrArg (fun u => gt (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) u (ring (2 * (7 * n + 6) + 1 + 1)).zero) key
  have k1 : ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (kap n 1 w) (kap n 2 w)) (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (kab n 1 w) (kab n 2 w))) (Cyclo7.cyc7 LibInt.intR).one) (gt (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR  -- (+439 chars; full line in the Zenodo source)
    have := k0
    change gt (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (gmul (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (mono (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (ring (2 * (7 * n + 6) + 1 + 1)).zero ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (kap n 1 w) (kap n 2 w)) (CharSumGen.J2 (Cyclo7.cyc7 LibInt  -- (+684 chars; full line in the Zenodo source)
    rw [JacobiNorm.mono0_mulg, JacobiNorm.mono0_mulg] at this
    exact this
  have hP : (CharSumGen.Pp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))) = (Int.ofNat (2 * (7 * n + 6) + 1 + 1 + 1), 0, 0, 0, 0, 0) := Sept3.sum_one7 n _
  have hX0 : gt (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6) + 1 + 1) (CharSumGen.Xv (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6))) (ring (2 * (7 * n + 6) + 1 + 1)).zero = (Int.ofNat (2 * (7 * n + 6) + 1 + 1), 0, 0, 0, 0, 0) := by
    rw [CharSumGen.gt_X]; unfold delta; rw [ite_pos' _ _ rfl, (Cyclo7.cyc7 LibInt.intR).mul_comm, (Cyclo7.cyc7 LibInt.intR).one_mul, hP]
    apply Prod.ext
    · show Int.ofNat (2 * (7 * n + 6) + 1 + 1) + 1 + -1 = Int.ofNat (2 * (7 * n + 6) + 1 + 1); lib_int
    apply Prod.ext
    · show (0 : Int) + -0 = 0; rfl
    apply Prod.ext
    · show (0 : Int) + -0 = 0; rfl
    apply Prod.ext
    · show (0 : Int) + -0 = 0; rfl
    apply Prod.ext
    · show (0 : Int) + -0 = 0; rfl
    · show (0 : Int) + -0 = 0; rfl
  rw [hX0] at k1
  have k2 := Sept3.scal_cancel (2 * (7 * n + 6) + 1) _ _ k1
  calc (Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) (Sept3.conj7 (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2))) = ((Cyclo7.cyc7 LibInt.intR).mul ((  -- (+278 chars; full line in the Zenodo source)
    _ = ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (Cyclo7.cyc7 LibInt.intR).one (Cyclo7.cyc7 LibInt.intR).one) (CharSumGen.Pp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)))) := k2
    _ = (Int.ofNat (2 * (7 * n + 6) + 1 + 1 + 1), 0, 0, 0, 0, 0) := by rw [hP]; lib_ring

end Sept13

#print axioms Sept13.kap_mul
#print axioms Sept13.kab_mul
#print axioms Sept13.kap_zero
#print axioms Sept13.kab_zero
#print axioms Sept13.kap_one
#print axioms Sept13.kab_neg1
#print axioms Sept13.pw12
#print axioms Sept13.kap_sum1
#print axioms Sept13.kap_sum2
#print axioms Sept13.kap_sum3
#print axioms Sept13.kab_sum3
#print axioms Sept13.gg12
#print axioms Sept13.gb12
#print axioms Sept13.hX1
#print axioms Sept13.hX2
#print axioms Sept13.hX3
#print axioms Sept13.conj7_pow
#print axioms Sept13.eJa
#print axioms Sept13.eJb
#print axioms Sept13.sept_norm2

#print axioms: 20 theorems, no axioms.

78.208 \(4p=(2a-b)^2+7b^2\) from the second septic Jacobi sum

The \(\sigma _2\)-fixed Jacobi sum \(J'\) is \(a+b(\zeta +\zeta ^2+\zeta ^4)\) and its norm gives \(p\) as a binary quadratic form.

\[ J(\chi ,\chi ^2)=a+b(\zeta +\zeta ^2+\zeta ^4),\quad a^2-ab+2b^2=p,\quad (2a-b)^2+7b^2=4p. \]

lang/Sept14.lean

/-
  lang/Sept14 — T49, cycle 784 (ℓ = 7, step 14): **J' = J(χ, χ²) = a + b(ζ + ζ² + ζ⁴) with a² − ab + 2b² = p**, i.e.
  4p = (2a − b)² + 7b² (generated by s14gen.py).
    fixed_norm   σ₂z = z in ℤ[ζ₇] forces z = (a, b, b, 0, b, 0) = a + b η, η = ζ + ζ² + ζ⁴; then z · conj z = a² − ab + 2b²
    sept_form    from Sept12.sig2_JY and Sept13.sept_norm2
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.Sept13

namespace Sept14

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

theorem fixed_norm (m : Nat) : ∀ z : Int × Int × Int × Int × Int × Int, Sept8.sig2 z = z → (Cyclo7.cyc7 LibInt.intR).mul z (Sept3.conj7 z) = (Int.ofNat m, 0, 0, 0, 0, 0) →
    z = (z.1, z.2.1, z.2.1, 0, z.2.1, 0) ∧ z.1 * z.1 + -(z.1 * z.2.1) + 2 * (z.2.1 * z.2.1) = Int.ofNat m
  | (a0, a1, a2, a3, a4, a5), hs, hn => by
    have s0 : a0 + -a3 = a0 := congrArg (fun s => s.1) hs
    have s1 : a4 + -a3 = a1 := congrArg (fun s => s.2.1) hs
    have s2 : a1 + -a3 = a2 := congrArg (fun s => s.2.2.1) hs
    have s5 : -a3 = a5 := congrArg (fun s => s.2.2.2.2.2) hs
    have e3 : a3 = 0 := by
      calc a3 = -((a0 + -a3) + -a0) := by lib_int
        _ = -(a0 + -a0) := by rw [s0]
        _ = 0 := by lib_int
    have e5 : a5 = 0 := by rw [← s5, e3]; rfl
    have e4 : a4 = a1 := by
      calc a4 = (a4 + -a3) + a3 := by lib_int
        _ = a1 + 0 := by rw [s1, e3]
        _ = a1 := by lib_int
    have e2 : a2 = a1 := by
      calc a2 = a1 + -a3 := s2.symm
        _ = a1 + -0 := by rw [e3]
        _ = a1 := by lib_int
    rw [e2, e3, e4, e5] at hn
    rw [e2, e3, e4, e5]
    refine ⟨rfl, ?_⟩
    have h0 : (((a0 * (a0 + -a1)) + ((((a1 * (a1 + -a1)) + (0 * (0 + -a1))) + (a1 * (a1 + -a1))) + (0 * (0 + -a1)))) + (-(((((a1 * (a1 + -a1)) + (a1 * (0 + -a1))) + (0 * (a1 + -a1))) + (a1 * (0 + -a1))) + (0 * (-a1))))) = Int.ofNat m := congrArg (fun s => s.1) hn
    show a0 * a0 + -(a0 * a1) + 2 * (a1 * a1) = Int.ofNat m
    rw [show a0 * a0 + -(a0 * a1) + 2 * (a1 * a1) = (((a0 * (a0 + -a1)) + ((((a1 * (a1 + -a1)) + (0 * (0 + -a1))) + (a1 * (a1 + -a1))) + (0 * (0 + -a1)))) + (-(((((a1 * (a1 + -a1)) + (a1 * (0 + -a1))) + (0 * (a1 + -a1))) + (a1 * (0 + -a1))) + (0 * (-a1))))) by lib_int]
    exact h0

variable (n : Nat)

/-- **J(χ, χ²) = a + b(ζ + ζ² + ζ⁴), a² − ab + 2b² = p, (2a − b)² + 7b² = 4p.** -/
theorem sept_form (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+352 chars; full line in the Zenodo source)
    ∧ (2 * a + -b) * (2 * a + -b) + 7 * (b * b) = 4 * Int.ofNat (2 * (7 * n + 6) + 1 + 1 + 1) :=
  match fixed_norm (2 * (7 * n + 6) + 1 + 1 + 1) (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) (Sept12.sig2_JY n hpr w hw1 hw7) (Sept13.sept_norm2 n hpr w hw1 hw7) with
  | ⟨h1, h2⟩ => ⟨_, _, h1, h2, by rw [← h2]; lib_int⟩

end Sept14

#print axioms Sept14.fixed_norm
#print axioms Sept14.sept_form

#print axioms: 2 theorems, no axioms.

78.209 The norm of the septic product

The \(\mathbb Z[\zeta _7]\)-lift of the septic product has norm \(p^5\) under complex conjugation.

\[ A=J^2J'^2\sigma _3(J)\ \Rightarrow \ A\overline A=p^5. \]

lang/Sept15.lean

/-
  lang/Sept15 — T49, cycle 785 (ℓ = 7, step 15): **A · conj(A) = p⁵** for A = J² J'² σ₃J ∈ ℤ[ζ₇] (the ℤ[ζ₇]-lift of the septic
  product, Sept12.sept_A''), J = J(χ, χ), J' = J(χ, χ²) (Quint20 pattern, generated by s15gen.py).
    conj_sig3     conj ∘ σ₃ = σ₃ ∘ conj on ℤ[ζ₇] (coordinates)
    sig3_scalar   σ₃ fixes integers
    sept_AA       A · conj(A) = p⁵      (Sept3.sept_norm, Sept13.sept_norm2, σ₃ multiplicative)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Sept14

namespace Sept15

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

theorem conj_sig3 : ∀ z : Int × Int × Int × Int × Int × Int, Sept3.conj7 (Sept8.sig3 z) = Sept8.sig3 (Sept3.conj7 z)
  | (a0, a1, a2, a3, a4, a5) => by
    apply Prod.ext
    · show ((a0 + (-a2)) + -(a5 + (-a2))) = ((a0 + -a1) + (-(a5 + -a1)))
      lib_int
    apply Prod.ext
    · show (-(a5 + (-a2))) = ((a2 + -a1) + (-(a5 + -a1)))
      lib_int
    apply Prod.ext
    · show ((a4 + (-a2)) + -(a5 + (-a2))) = ((a4 + -a1) + (-(a5 + -a1)))
      lib_int
    apply Prod.ext
    · show ((-a2) + -(a5 + (-a2))) = ((-a1) + (-(a5 + -a1)))
      lib_int
    apply Prod.ext
    · show ((a1 + (-a2)) + -(a5 + (-a2))) = (-(a5 + -a1))
      lib_int
    · show ((a3 + (-a2)) + -(a5 + (-a2))) = ((a3 + -a1) + (-(a5 + -a1)))
      lib_int

theorem sig3_scalar (m : Nat) : Sept8.sig3 (Int.ofNat m, 0, 0, 0, 0, 0) = (Int.ofNat m, 0, 0, 0, 0, 0) := by
  apply Prod.ext
  · show (((Int.ofNat m) + (-0)) : Int) = (Int.ofNat m)
    lib_int
  apply Prod.ext
  · show ((0 + (-0)) : Int) = 0
    lib_int
  apply Prod.ext
  · show ((0 + (-0)) : Int) = 0
    lib_int
  apply Prod.ext
  · show ((0 + (-0)) : Int) = 0
    lib_int
  apply Prod.ext
  · show ((-0) : Int) = 0
    lib_int
  · show ((0 + (-0)) : Int) = 0
    lib_int

variable (n : Nat)

/-- **A · conj(A) = p⁵**, A = J² J'² σ₃J. -/
theorem sept_AA (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+2025 chars; full line in the Zenodo source)
  have N1 : ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)) (Sept3.conj7 (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)))) = (Int.ofNat (2 * (7 * n + 6) + 1 + 1 + 1), 0, 0, 0, 0, 0) := Sept3.sept_norm n hpr w hw1 hw7
  have N2 : ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) (Sept3.conj7 (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)))) = (Int.ofNat (2 * (7 * n + 6  -- (+67 chars; full line in the Zenodo source)
  have N3 : ((Cyclo7.cyc7 LibInt.intR).mul (Sept8.sig3 (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w))) (Sept8.sig3 (Sept3.conj7 (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w))))) = (Int.ofNat (2 * (7 * n + 6) + 1 + 1 + 1), 0, 0, 0, 0, 0) := by rw [← Sept8.sig3_mul, N1, sig3_scalar]
  repeat rw [Sept3.conj7_mul]
  rw [conj_sig3]
  rw [show ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)) (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w))) ((Cyclo7.cyc7 LibInt.intR).mul (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 *  -- (+2777 chars; full line in the Zenodo source)
    N1, N2, N3]

end Sept15

#print axioms Sept15.conj_sig3
#print axioms Sept15.sig3_scalar
#print axioms Sept15.sept_AA

#print axioms: 3 theorems, no axioms.

78.210 Normalisation of the second septic Jacobi sum

Under \(\zeta \mapsto 1\) in \(\mathbb F_7\) the Jacobi sum \(J'\) becomes \(p-2\equiv -1\), which fixes the sign in \(4p=(2a-b)^2+7b^2\).

\[ J(\chi ,\chi ^2)=a+b(\zeta +\zeta ^2+\zeta ^4),\quad a^2-ab+2b^2=p,\quad a+3b\equiv -1\pmod 7. \]

lang/Sept16.lean

/-
  lang/Sept16 — T49, cycle 786 (ℓ = 7, step 16): **J' = J(χ, χ²) ≡ −1 modulo (1 − ζ)**: under ζ ↦ 1 into 𝔽₇ (a root of Φ₇,
  Φ₇(1) = 7 = 0), J' ↦ Σ_(t ≠ 0, 1) 1 = p − 2 = −1, since p = 14n + 15 ≡ 1 (mod 7). With Sept14 (J' = a + b(ζ + ζ² + ζ⁴)):
  a + 3b ≡ −1 (mod 7), the normalisation that fixes the sign of b in 4p = (2a − b)² + 7b² (generated by s16gen.py).
    ev_zero, ev_add, ev_sum     ev_v is additive (any T)
    sone, c7z, cp               Σ_(x ∈ allZ m k) 1 = k, castN 𝔽₇ (7m) = 0, p = 1 in 𝔽₇
    eps_ne, eps_zero            ev_1 ψ(x) = 1 (x ≠ 0), ev_1 ψ(0) = 0 in 𝔽₇
    term, nds                   ev_1(ψ(t)ψ(1 − t)²) = 1 − δ(t, 0) − δ(t, 1);  Σ_t −δ(t, c) = −1
    ev_JY                       ev_1(J') = −1 in 𝔽₇
    sept_primary2               J' = (a, b, b, 0, b, 0), a² − ab + 2b² = p, a + 3b + 1 ≡ 0 (mod 7)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Sept15
import lang.Sept10

namespace Sept16

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius
open LibRing (castZ castN)

section Gen
variable {R : Type} (T : CRing R) (v : R)

theorem ev_zero : Sept10.ev T v (Cyclo7.cyc7 T).zero = T.zero := by
  show (T.add (T.add (T.add (T.add (T.add T.zero (T.mul T.zero v)) (T.mul T.zero (T.mul v v))) (T.mul T.zero (T.mul (T.mul v v) v))) (T.mul T.zero (T.mul (T.mul (T.mul v v) v) v))) (T.mul T.zero (T.mul (T.mul (T.mul (T.mul v v) v) v) v))) = T.zero
  lib_ring

theorem ev_add : ∀ x y : R × R × R × R × R × R, Sept10.ev T v ((Cyclo7.cyc7 T).add x y) = T.add (Sept10.ev T v x) (Sept10.ev T v y)
  | (a0, a1, a2, a3, a4, a5), (b0, b1, b2, b3, b4, b5) => by
    show (T.add (T.add (T.add (T.add (T.add (T.add a0 b0) (T.mul (T.add a1 b1) v)) (T.mul (T.add a2 b2) (T.mul v v))) (T.mul (T.add a3 b3) (T.mul (T.mul v v) v))) (T.mul (T.add a4 b4) (T.mul (T.mul (T.mul v v) v) v))) (T.mul (T.add a5 b5) (T.mul (T.mul (T.mul (T.mul v v) v) v) v))) = T.add (T.add (T.add (T.add (T.add (T.add a0 (T.mul a1 v)) (T.mul a2 (T.mul v v))) (T.mul a3 (T.mul (T.mul v v) v)))  -- (+306 chars; full line in the Zenodo source)
    lib_ring

theorem ev_sum {A' : Type} (f : A' → R × R × R × R × R × R) : ∀ l : List A', Sept10.ev T v (sumK (Cyclo7.cyc7 T) f l) = sumK T (fun a => Sept10.ev T v (f a)) l
  | [] => ev_zero T v
  | a :: l => by
    show Sept10.ev T v ((Cyclo7.cyc7 T).add (f a) (sumK (Cyclo7.cyc7 T) f l)) = T.add (Sept10.ev T v (f a)) (sumK T (fun a => Sept10.ev T v (f a)) l)
    rw [ev_add, ev_sum f l]

theorem sone (m : Nat) : ∀ k, sumK T (fun _ : Zp m => T.one) (CMInert.allZ m k) = castN T k
  | 0 => rfl
  | k + 1 => by
    show sumK T (fun _ : Zp m => T.one) (CMInert.allZ m k ++ [mk m k]) = T.add T.one (castN T k)
    rw [sumK_append, sone m k]
    show T.add (castN T k) (T.add T.one T.zero) = T.add T.one (castN T k)
    lib_ring
end Gen

theorem c7z : ∀ m : Nat, castN (ring 6) (7 * m) = (ring 6).zero
  | 0 => rfl
  | m + 1 => by
    show castN (ring 6) (7 * m + 7) = (ring 6).zero
    rw [IntCast.castN_add, c7z m]
    rfl

theorem hu1 : ((ring 6).add ((ring 6).add ((ring 6).add ((ring 6).add ((ring 6).add ((ring 6).add (ring 6).one (ring 6).one) ((ring 6).mul (ring 6).one (ring 6).one)) ((ring 6).mul ((ring 6).mul (ring 6).one (ring 6).one) (ring 6).one)) ((ring 6).mul ((ring 6).mul ((ring 6).mul (ring 6).one (ring 6).one) (ring 6).one) (ring 6).one)) ((ring 6).mul ((ring 6).mul ((ring 6).mul ((ring 6).mul (ring 6).  -- (+237 chars; full line in the Zenodo source)

variable (n : Nat)

theorem cp : castN (ring 6) (2 * (7 * n + 6) + 1 + 1 + 1) = (ring 6).one := by
  rw [show 2 * (7 * n + 6) + 1 + 1 + 1 = 7 * (2 * n + 2) + 1 by lib_nat, IntCast.castN_add, c7z]
  rfl

theorem eps_zero (w : Zp (2 * (7 * n + 6) + 1 + 1)) : (Sept10.ev (ring 6) (ring 6).one ((Sept4.psi n (ring 6) w) (ring (2 * (7 * n + 6) + 1 + 1)).zero)) = (ring 6).zero := by
  rw [Sept4.psi_zero]; rfl

theorem eps_ne (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+251 chars; full line in the Zenodo source)
  match Sept2.exists_i n hpr w hw1 hw7 x hx with
  | ⟨i, hi, ei⟩ =>
    unfold Sept4.psi
    rw [Sept2.val_of n hpr w hw1 hw7 x hx i hi ei, ← Sept4.ph7_pow, Sept10.ev_pow (ring 6) (ring 6).one hu1]
    rw [show Sept10.ev (ring 6) (ring 6).one (Sept4.ph7 (ring 6) Sept2.zeta) = (ring 6).one from rfl]
    exact CubicChar.pw_one'' (ring 6) i

theorem term (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+116 chars; full line in the Zenodo source)
    Sept10.ev (ring 6) (ring 6).one ((Cyclo7.cyc7 (ring 6)).mul (pw (Cyclo7.cyc7 (ring 6)) ((Sept4.psi n (ring 6) w) t) 1) (pw (Cyclo7.cyc7 (ring 6)) ((Sept4.psi n (ring 6) w) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) 2)) = (ring 6).add (ring 6).one ((ring 6).add ((ring 6).neg (FiniteRecChain.delta (ring 6) t (ring (2 * (7  -- (+119 chars; full line in the Zenodo source)
  have h10 : (ring (2 * (7 * n + 6) + 1 + 1)).one ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * (7 * n + 6) + 1)
  rw [Sept10.ev_mul (ring 6) (ring 6).one hu1, Sept10.ev_pow (ring 6) (ring 6).one hu1, Sept10.ev_pow (ring 6) (ring 6).one hu1]
  unfold FiniteRecChain.delta
  by_cases h0 : t = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [h0, ite_pos' _ _ rfl, ite_neg' _ _ (fun e => h10 e.symm), eps_zero n w, HasseCM.pw_zero_succ (ring 6) 0]
    lib_ring
  by_cases h1 : t = (ring (2 * (7 * n + 6) + 1 + 1)).one
  · rw [h1, ite_pos' _ _ rfl, ite_neg' _ _ h10, show (ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) = (ring (2 * (7 * n + 6) + 1 + 1)).zero by lib_ring, eps_zero n w,
      eps_ne n hpr w hw1 hw7 (ring (2 * (7 * n + 6) + 1 + 1)).one h10, HasseCM.pw_zero_succ (ring 6) 1]
    lib_ring
  have hne : ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)) ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := fun e => h1 (by
    calc t = (ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) := by lib_ring
      _ = (ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).zero) := by rw [e]
      _ = (ring (2 * (7 * n + 6) + 1 + 1)).one := by lib_ring)
  rw [ite_neg' _ _ h0, ite_neg' _ _ h1, eps_ne n hpr w hw1 hw7 t h0, eps_ne n hpr w hw1 hw7 _ hne, CubicChar.pw_one'' (ring 6) 1, CubicChar.pw_one'' (ring 6) 2]
  lib_ring

theorem nds (c : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (ring 6) (fun t => (ring 6).neg (FiniteRecChain.delta (ring 6) t c)) (CharSumGen.LL (2 * (7 * n + 6))) = (ring 6).neg (ring 6).one := by
  rw [sumK_ext (ring 6) _ (fun t => (ring 6).mul ((ring 6).neg (ring 6).one) (FiniteRecChain.delta (ring 6) t c)) (fun t => by lib_ring), ← mul_sumK,
    show sumK (ring 6) (fun t => (FiniteRecChain.delta (ring 6) t c)) (CharSumGen.LL (2 * (7 * n + 6))) = (ring 6).one from CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (ring 6) c]
  lib_ring

/-- **ev_1(J(χ, χ²)) = −1 in 𝔽₇.** -/
theorem ev_JY (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+328 chars; full line in the Zenodo source)
  have e : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) := Sept13.  -- (+7 chars; full line in the Zenodo source)
  rw [← e, ← Sept8.JJ n (ring 6) w 1 2]
  unfold CharSumGen.J2
  rw [ev_sum, sumK_ext (ring 6) _ _ (fun t => by unfold CharSumGen.psp; exact term n hpr w hw1 hw7 t), sumK_add, sumK_add, nds, nds]
  rw [show sumK (ring 6) (fun _ : Zp (2 * (7 * n + 6) + 1 + 1) => (ring 6).one) (CharSumGen.LL (2 * (7 * n + 6))) = castN (ring 6) (2 * (7 * n + 6) + 1 + 1 + 1) from sone (ring 6) (2 * (7 * n + 6) + 1 + 1) (2 * (7 * n + 6) + 1 + 1 + 1), cp]
  lib_ring

/-- **J' = a + b(ζ + ζ² + ζ⁴) with a² − ab + 2b² = p and a + 3b ≡ −1 (mod 7).** -/
theorem sept_primary2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (  -- (+356 chars; full line in the Zenodo source)
    ∧ castZ (ring 6) (a + 3 * b + 1) = (ring 6).zero :=
  match Sept14.sept_form n hpr w hw1 hw7 with
  | ⟨a, b, h1, h2, _⟩ => ⟨a, b, h1, h2, by
    have hev := ev_JY n hpr w hw1 hw7
    rw [h1] at hev
    change ((ring 6).add ((ring 6).add ((ring 6).add ((ring 6).add ((ring 6).add (castZ (ring 6) a) ((ring 6).mul (castZ (ring 6) b) (ring 6).one)) ((ring 6).mul (castZ (ring 6) b) ((ring 6).mul (ring 6).one (ring 6).one))) ((ring 6).mul (castZ (ring 6) 0) ((ring 6).mul ((ring 6).mul (ring 6).one (ring 6).one) (ring 6).one))) ((ring 6).mul (castZ (ring 6) b) ((ring 6).mul ((ring 6).mul ((ring 6).m  -- (+254 chars; full line in the Zenodo source)
    rw [show (castZ (ring 6) 0) = (ring 6).zero from rfl] at hev
    rw [IntCast.castZ_add, IntCast.castZ_add, HasseDavenport.castZ_mul, show (castZ (ring 6) 3) = (ring 6).add (ring 6).one ((ring 6).add (ring 6).one (ring 6).one) from rfl,
      show (castZ (ring 6) 1) = (ring 6).one from rfl]
    calc (ring 6).add ((ring 6).add (castZ (ring 6) a) ((ring 6).mul ((ring 6).add (ring 6).one ((ring 6).add (ring 6).one (ring 6).one)) (castZ (ring 6) b))) (ring 6).one
        = (ring 6).add ((ring 6).add ((ring 6).add ((ring 6).add ((ring 6).add ((ring 6).add (castZ (ring 6) a) ((ring 6).mul (castZ (ring 6) b) (ring 6).one)) ((ring 6).mul (castZ (ring 6) b) ((ring 6).mul (ring 6).one (ring 6).one))) ((ring 6).mul (ring 6).zero ((ring 6).mul ((ring 6).mul (ring 6).one (ring 6).one) (ring 6).one))) ((ring 6).mul (castZ (ring 6) b) ((ring 6).mul ((ring 6).mul ((ri  -- (+249 chars; full line in the Zenodo source)
      _ = (ring 6).zero := by rw [hev]; lib_ring⟩

end Sept16

#print axioms Sept16.ev_zero
#print axioms Sept16.ev_add
#print axioms Sept16.ev_sum
#print axioms Sept16.sone
#print axioms Sept16.c7z
#print axioms Sept16.hu1
#print axioms Sept16.cp
#print axioms Sept16.eps_zero
#print axioms Sept16.eps_ne
#print axioms Sept16.term
#print axioms Sept16.nds
#print axioms Sept16.ev_JY
#print axioms Sept16.sept_primary2

#print axioms: 13 theorems, no axioms.

78.211 Septic symbol form with \(J'\) at \(u\)

Since \(\sigma _2J'=J'\), the symbol form needs \(J'\) only at \(u\).

\[ \mathrm {ev}_u\,\iota \,\psi (q^{-6})=\big (J(u)^3J'(u)^2J(u^3)J(u^6)\big )^M. \]

lang/Sept17.lean

/-
  lang/Sept17 — T49, cycle 787 (ℓ = 7, step 17): **the septic symbol form with J' evaluated at u only**: since σ₂J' = J'
  (Sept12), J'(u²) = ev_u σ₂J' = J'(u), so for every prime q ≠ 7, p with q⁶ = 7M + 1 and any K ⊇ 𝔽_q with Φ₇(u) = 0:
    sept_sym_all'   ev_u ι ψ(q⁻⁶) = (J(u)³ · J'(u)² · J(u³) · J(u⁶))^M
  (Sept11.sept_sym_all, Sept10.ev_sig2, Sept12.sig2_JY; generated by s17gen.py).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Sept11
import lang.Sept12

namespace Sept17

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius
open LibRing (castZ castN)

variable (n k : Nat)

/-- **Septic symbol form**: ev_u ι ψ(q⁻⁶) = (J(u)³ J'(u)² J(u³) J(u⁶))^M. -/
theorem sept_sym_all' (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (  -- (+327 chars; full line in the Zenodo source)
    {K' : Type} (K : CRing K') (ι : Zp (k + 1) → K') (hadd : ∀ a b, ι ((ring (k + 1)).add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι ((ring (k + 1)).mul a b) = K.mul (ι a) (ι b)) (hneg : ∀ a, ι ((ring (k + 1)).neg a) = K.neg (ι a)) (hone : ι (ring (k + 1)).one = K.one) (hzero : ι (ring (k + 1)).zero = K.zero) (u : K') (hu : (K.add (K.add (K.add (K.add (K.add (K.add K.one u) (K.mul u u)) (K.mul (K.  -- (+153 chars; full line in the Zenodo source)
    Sept10.ev K u (Sept11.map7 ι (Sept4.psi n (ring (k + 1)) w (pw (ring (2 * (7 * n + 6) + 1 + 1)) (nsm (ring (2 * (7 * n + 6) + 1 + 1)) ((k + 1 + 1) ^ 6) (ring (2 * (7 * n + 6) + 1 + 1)).one) (2 * (7 * n + 6) + 1)))) = pw K (K.mul (K.mul (K.mul (K.mul (K.mul (Sept10.ev K u (Sept4.ph7 K (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)))) (Sept10.ev K u   -- (+945 chars; full line in the Zenodo source)
  have e : (Sept10.ev K (K.mul u u) (Sept4.ph7 K (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)))) = (Sept10.ev K u (Sept4.ph7 K (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)))) :=   -- (+2 chars; full line in the Zenodo source)
    rw [← Sept10.ev_sig2 K u hu, Sept12.sig2_JY n hpr w hw1 hw7]
  rw [Sept11.sept_sym_all n k hpr w hw1 hw7 hq hQ M hM K ι hadd hmul hneg hone hzero u hu, e]

end Sept17

#print axioms Sept17.sept_sym_all'

#print axioms: 1 theorem, no axioms.

78.212 Septic Jacobi sums modulo \((1-\zeta )^2\)

Under \(\zeta \mapsto 1+\varepsilon \) in \(\mathbb F_7[\varepsilon ]\), \(\varepsilon ^2=0\), both septic Jacobi sums become \(-1\).

\[ \mathrm {ev}_{1+\varepsilon }J(\chi ,\chi )=\mathrm {ev}_{1+\varepsilon }J(\chi ,\chi ^2)=-1\ \text {in}\ \mathbb F_7[\varepsilon ]. \]

lang/Sept18.lean

/-
  lang/Sept18 — T49, cycle 788 (ℓ = 7, step 18): **J ≡ J' ≡ −1 modulo (1 − ζ)²**, as images under ζ ↦ 1 + ε in the dual numbers
  𝔽₇[ε] = Quad.quad 𝔽₇ 0 (ε² = 0; Φ₇(1 + ε) = 7 + 21ε = 0, so ev_(1+ε) is a ring map T[ζ₇] → 𝔽₇[ε] killing (1 − ζ)²;
  generated by s18gen.py). For J(χ^a, χ^b) (a, b ≥ 1) with e(t) = ev_(1+ε) ψ(t):
    dmul, pw_first    in K[ε]: x.1 = y.1 = 1 ⇒ xy = x + y − 1; first coordinates 1 are kept by powers
    efirst            e(t) = (1 + ε)^i has first coordinate 1 (t ≠ 0)
    term              e(t)^a e(1 − t)^b = e(t)^a + e(1 − t)^b − 1 for every t
    sp1, sp2          Σ_t e(t) = Σ_t e(t)² = 0 (Sept4.psi_sum, psi_nt)
    evJab             ev_(1+ε) J(χ^a, χ^b) = 0 + 0 − p = −1 when Σ e^a = Σ e^b = 0
    ev_J, ev_JY       ev_(1+ε) J(χ, χ) = ev_(1+ε) J(χ, χ²) = −1 in 𝔽₇[ε]
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Sept16

namespace Sept18

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius
open LibRing (castZ castN)

section Gen
variable {R : Type} (K : CRing R)

theorem dmul : ∀ x y : R × R, x.1 = K.one → y.1 = K.one → (Quad.quad K K.zero).mul x y = (Quad.quad K K.zero).add ((Quad.quad K K.zero).add x y) ((Quad.quad K K.zero).neg (Quad.quad K K.zero).one)
  | (x1, x2), (y1, y2), hx, hy => by
    have hx' : x1 = K.one := hx
    have hy' : y1 = K.one := hy
    apply Prod.ext
    · show K.add (K.mul x1 y1) (K.mul K.zero (K.mul x2 y2)) = K.add (K.add x1 y1) (K.neg K.one)
      rw [hx', hy']; lib_ring
    · show K.add (K.mul x1 y2) (K.mul x2 y1) = K.add (K.add x2 y2) (K.neg K.zero)
      rw [hx', hy']; lib_ring

theorem pw_first (x : R × R) (hx : x.1 = K.one) : ∀ k, (pw (Quad.quad K K.zero) x k).1 = K.one
  | 0 => rfl
  | k + 1 => by
    show K.add (K.mul (pw (Quad.quad K K.zero) x k).1 x.1) (K.mul K.zero (K.mul (pw (Quad.quad K K.zero) x k).2 x.2)) = K.one
    rw [pw_first x hx k, hx]; lib_ring
end Gen

theorem hu : ((Quad.quad (ring 6) (ring 6).zero).add ((Quad.quad (ring 6) (ring 6).zero).add ((Quad.quad (ring 6) (ring 6).zero).add ((Quad.quad (ring 6) (ring 6).zero).add ((Quad.quad (ring 6) (ring 6).zero).add ((Quad.quad (ring 6) (ring 6).zero).add (Quad.quad (ring 6) (ring 6).zero).one ((ring 6).one, (ring 6).one)) ((Quad.quad (ring 6) (ring 6).zero).mul ((ring 6).one, (ring 6).one) ((ring 6)  -- (+1170 chars; full line in the Zenodo source)

theorem c7z : ∀ m : Nat, castN (Quad.quad (ring 6) (ring 6).zero) (7 * m) = (Quad.quad (ring 6) (ring 6).zero).zero
  | 0 => rfl
  | m + 1 => by
    show castN (Quad.quad (ring 6) (ring 6).zero) (7 * m + 7) = (Quad.quad (ring 6) (ring 6).zero).zero
    rw [IntCast.castN_add, c7z m]
    rfl

variable (n : Nat)

theorem cp : castN (Quad.quad (ring 6) (ring 6).zero) (2 * (7 * n + 6) + 1 + 1 + 1) = (Quad.quad (ring 6) (ring 6).zero).one := by
  rw [show 2 * (7 * n + 6) + 1 + 1 + 1 = 7 * (2 * n + 2) + 1 by lib_nat, IntCast.castN_add, c7z]
  rfl

theorem efirst (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+323 chars; full line in the Zenodo source)
  match Sept2.exists_i n hpr w hw1 hw7 x hx with
  | ⟨i, hi, ei⟩ =>
    unfold Sept4.psi
    rw [Sept2.val_of n hpr w hw1 hw7 x hx i hi ei, ← Sept4.ph7_pow, Sept10.ev_pow (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one) hu]
    rw [show Sept10.ev (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one) (Sept4.ph7 (Quad.quad (ring 6) (ring 6).zero) Sept2.zeta) = ((ring 6).one, (ring 6).one) from rfl]
    exact pw_first (ring 6) ((ring 6).one, (ring 6).one) rfl i

theorem xfirst (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+389 chars; full line in the Zenodo source)
  rw [Sept10.ev_pow (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one) hu]; exact pw_first (ring 6) _ (efirst n hpr w hw1 hw7 x hx) k

theorem term (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+128 chars; full line in the Zenodo source)
    (Sept10.ev (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one) ((Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)).mul (pw (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)) ((Sept4.psi n (Quad.quad (ring 6) (ring 6).zero) w) t) (a + 1)) (pw (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)) ((Sept4.psi n (Quad.quad (ring 6) (ring 6).zero) w) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (  -- (+752 chars; full line in the Zenodo source)
  have h10 : (ring (2 * (7 * n + 6) + 1 + 1)).one ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * (7 * n + 6) + 1)
  rw [Sept10.ev_mul (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one) hu]
  by_cases h0 : t = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [h0, Sept4.psi_zero, HasseCM.pw_zero_succ (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)) a, show (ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).zero) = (ring (2 * (7 * n + 6) + 1 + 1)).one by lib_ring,
      Sept4.psi_one n (Quad.quad (ring 6) (ring 6).zero) hpr w hw1 hw7, CubicChar.pw_one'' (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)) (b + 1), Sept16.ev_zero (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one), Sept10.ev_one (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one)]
    lib_ring
  by_cases h1 : t = (ring (2 * (7 * n + 6) + 1 + 1)).one
  · rw [h1, show (ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) = (ring (2 * (7 * n + 6) + 1 + 1)).zero by lib_ring, Sept4.psi_zero, HasseCM.pw_zero_succ (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)) b,
      Sept4.psi_one n (Quad.quad (ring 6) (ring 6).zero) hpr w hw1 hw7, CubicChar.pw_one'' (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)) (a + 1), Sept16.ev_zero (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one), Sept10.ev_one (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one)]
    lib_ring
  have hne : ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)) ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := fun e => h1 (by
    calc t = (ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) := by lib_ring
      _ = (ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).zero) := by rw [e]
      _ = (ring (2 * (7 * n + 6) + 1 + 1)).one := by lib_ring)
  exact dmul (ring 6) _ _ (xfirst n hpr w hw1 hw7 t h0 (a + 1)) (xfirst n hpr w hw1 hw7 _ hne (b + 1))

theorem sp1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n  -- (+397 chars; full line in the Zenodo source)
  (Sept16.ev_sum (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one) (fun t => pw (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)) ((Sept4.psi n (Quad.quad (ring 6) (ring 6).zero) w) t) 1) (CharSumGen.LL (2 * (7 * n + 6)))).symm.trans (by
    rw [sumK_ext (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)) (fun t => pw (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)) ((Sept4.psi n (Quad.quad (ring 6) (ring 6).zero) w) t) 1) (Sept4.psi n (Quad.quad (ring 6) (ring 6).zero) w) (fun t => (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)).one_mul _), Sept4.psi_sum n (Quad.quad (ring 6) (ring 6).zero) hpr w hw1 hw7]; exact Sept16.ev_zero (Qua  -- (+60 chars; full line in the Zenodo source)

theorem sp2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n  -- (+397 chars; full line in the Zenodo source)
  (Sept16.ev_sum (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one) (fun t => pw (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)) ((Sept4.psi n (Quad.quad (ring 6) (ring 6).zero) w) t) 2) (CharSumGen.LL (2 * (7 * n + 6)))).symm.trans (by
    rw [Sept4.psi_nt n (Quad.quad (ring 6) (ring 6).zero) hpr w hw1 hw7 0 (Nat.zero_lt_succ 4)]; exact Sept16.ev_zero (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one))

theorem evJab (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+748 chars; full line in the Zenodo source)
    (Sept10.ev (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one) (Sept4.ph7 (Quad.quad (ring 6) (ring 6).zero) (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (a + 1)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (b + 1))))) = (Quad.quad (ring 6) (ring 6).zero).neg   -- (+44 chars; full line in the Zenodo source)
  rw [← Sept8.JJ n (Quad.quad (ring 6) (ring 6).zero) w (a + 1) (b + 1)]
  unfold CharSumGen.J2
  rw [Sept16.ev_sum, sumK_ext (Quad.quad (ring 6) (ring 6).zero) _ _ (fun t => by unfold CharSumGen.psp; exact term n hpr w hw1 hw7 a b t), sumK_add, sumK_add]
  have hY : sumK (Quad.quad (ring 6) (ring 6).zero) (fun t => (Sept10.ev (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one) (pw (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)) ((Sept4.psi n (Quad.quad (ring 6) (ring 6).zero) w) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (b + 1)))) (CharSumGen.LL (2 * (7 * n +   -- (+49 chars; full line in the Zenodo source)
    (reindex (Quad.quad (ring 6) (ring 6).zero) _ (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Quad.quad (ring 6) (ring 6).zero)) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t =>  -- (+36 chars; full line in the Zenodo source)
      (fun s => (Sept10.ev (Quad.quad (ring 6) (ring 6).zero) ((ring 6).one, (ring 6).one) (pw (Cyclo7.cyc7 (Quad.quad (ring 6) (ring 6).zero)) ((Sept4.psi n (Quad.quad (ring 6) (ring 6).zero) w) s) (b + 1))))).trans hb
  have hN : sumK (Quad.quad (ring 6) (ring 6).zero) (fun _ : Zp (2 * (7 * n + 6) + 1 + 1) => (Quad.quad (ring 6) (ring 6).zero).neg (Quad.quad (ring 6) (ring 6).zero).one) (CharSumGen.LL (2 * (7 * n + 6))) = (Quad.quad (ring 6) (ring 6).zero).neg (Quad.quad (ring 6) (ring 6).zero).one := by
    rw [sumK_ext (Quad.quad (ring 6) (ring 6).zero) _ (fun _ => (Quad.quad (ring 6) (ring 6).zero).mul ((Quad.quad (ring 6) (ring 6).zero).neg (Quad.quad (ring 6) (ring 6).zero).one) (Quad.quad (ring 6) (ring 6).zero).one) (fun _ => by lib_ring), ← mul_sumK,
      show sumK (Quad.quad (ring 6) (ring 6).zero) (fun _ : Zp (2 * (7 * n + 6) + 1 + 1) => (Quad.quad (ring 6) (ring 6).zero).one) (CharSumGen.LL (2 * (7 * n + 6))) = castN (Quad.quad (ring 6) (ring 6).zero) (2 * (7 * n + 6) + 1 + 1 + 1) from Sept16.sone (Quad.quad (ring 6) (ring 6).zero) (2 * (7 * n + 6) + 1 + 1) (2 * (7 * n + 6) + 1 + 1 + 1), cp]
    lib_ring
  rw [ha, hY, hN]
  lib_ring

/-- **J(χ, χ) ≡ −1 mod (1 − ζ)²**: ev_(1+ε) J = −1 in 𝔽₇[ε]. -/
theorem ev_J (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+384 chars; full line in the Zenodo source)
  rw [← Sept8.J1C n w]; exact evJab n hpr w hw1 hw7 0 0 (sp1 n hpr w hw1 hw7) (sp1 n hpr w hw1 hw7)

/-- **J(χ, χ²) ≡ −1 mod (1 − ζ)²**: ev_(1+ε) J' = −1 in 𝔽₇[ε]. -/
theorem ev_JY (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+448 chars; full line in the Zenodo source)
  have e : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) := Sept13.  -- (+7 chars; full line in the Zenodo source)
  rw [← e]; exact evJab n hpr w hw1 hw7 0 1 (sp1 n hpr w hw1 hw7) (sp2 n hpr w hw1 hw7)

end Sept18

#print axioms Sept18.dmul
#print axioms Sept18.pw_first
#print axioms Sept18.hu
#print axioms Sept18.c7z
#print axioms Sept18.cp
#print axioms Sept18.efirst
#print axioms Sept18.xfirst
#print axioms Sept18.term
#print axioms Sept18.sp1
#print axioms Sept18.sp2
#print axioms Sept18.evJab
#print axioms Sept18.ev_J
#print axioms Sept18.ev_JY

#print axioms: 13 theorems, no axioms.

78.213 Number of seventh roots

The number of solutions of \(x^7=a\) in \(\mathbb F_p\) is the sum of the powers of the septic character at \(a\).

\[ \#\{x\in \mathbb F_p:x^7=a\}=\textstyle \sum _{k<7}\chi (a)^k\ \text {in}\ \mathbb Z[\zeta _7]. \]

lang/Sept19.lean

/-
  lang/Sept19 — T49, cycle 790 (ℓ = 7, step 19; first step of K1, the Fermat septic count): **N(a) = #{x : x⁷ = a} = Σ_(k<7) χ(a)^k**
  in ℤ[ζ₇] for every a ∈ 𝔽_p, p = 14n + 15 (generated by s19gen.py).
    mz, cw_ne, wi7       c·w^i ≠ c·w^(i+k) when w^k ≠ 1;  (w^i)⁷ = 1
    seven_roots          δ(x⁷, c⁷) = Σ_(i<7) δ(x, c w^i)   (c ≠ 0; Sept1.seven_cases)
    seven_root           a^((p−1)/7) = 1, a ≠ 0 ⇒ a = c⁷   (primitive root)
    geo7, zi7, sum_zero  z·Σ_(k<7) z^k = Σ_(k<7) z^k + (z⁷ − 1);  Σ_(k<7) ζ^(ik) = 0 for 0 < i < 7 (Sept3.kill)
    seven_count          Σ_x δ(x⁷, a) = Σ_(k<7) χ(a)^k
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Sept18

namespace Sept19

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

theorem geo7 {R : Type} (K : CRing R) (z : R) : K.mul z (K.add (K.add (K.add (K.add (K.add (K.add (pw K z 0) (pw K z 1)) (pw K z 2)) (pw K z 3)) (pw K z 4)) (pw K z 5)) (pw K z 6)) = K.add (K.add (K.add (K.add (K.add (K.add (K.add (pw K z 0) (pw K z 1)) (pw K z 2)) (pw K z 3)) (pw K z 4)) (pw K z 5)) (pw K z 6)) (K.add (pw K z 7) (K.neg K.one)) := by
  show K.mul z (K.add (K.add (K.add (K.add (K.add (K.add K.one (K.mul K.one z)) (K.mul (K.mul K.one z) z)) (K.mul (K.mul (K.mul K.one z) z) z)) (K.mul (K.mul (K.mul (K.mul K.one z) z) z) z)) (K.mul (K.mul (K.mul (K.mul (K.mul K.one z) z) z) z) z)) (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul K.one z) z) z) z) z) z)) = K.add (K.add (K.add (K.add (K.add (K.add (K.add K.one (K.mul K.one z)) (K.mul (K.mu  -- (+320 chars; full line in the Zenodo source)
  lib_ring

variable (n : Nat)

theorem mz (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (a b : Zp (2 * (7 * n + 6) + 1 + 1)) (ha : a ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero) (h : (ring (2 * (7 * n + 6) + 1 + 1)).mul a b = (ring (2 * (7 * n + 6) + 1 + 1)).zero) : b = (ring (2 * (7 * n + 6) + 1 + 1)).zero := by
  calc b = (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((CharSumGen.iv (2 * (7 * n + 6))) a) a) b := by rw [CharSumGen.iv_mul (2 * (7 * n + 6)) hpr a ha]; lib_ring
    _ = (ring (2 * (7 * n + 6) + 1 + 1)).mul ((CharSumGen.iv (2 * (7 * n + 6))) a) ((ring (2 * (7 * n + 6) + 1 + 1)).mul a b) := by lib_ring
    _ = (ring (2 * (7 * n + 6) + 1 + 1)).zero := by rw [h]; lib_ring

theorem cw_ne (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (_hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+452 chars; full line in the Zenodo source)
  have hnz : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w i) ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := fun e0 => Sept2.pwnz n hpr w (Sept2.w_ne_zero n w hw7) i (mz n hpr c _ hc e0)
  exact hk (Sept2.cancel1 n hpr ((ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w i)) (pw (ring (2 * (7 * n + 6) + 1 + 1)) w k) hnz (by
    rw [show (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w i)) (pw (ring (2 * (7 * n + 6) + 1 + 1)) w k) = (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w (i + k)) by rw [HasseCM.pw_add']; lib_ring]; exact e.symm))

theorem wi7 (_hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (_hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+217 chars; full line in the Zenodo source)
  rw [TwoPower.pwpow, show i * 7 = 7 * i by lib_nat, ← TwoPower.pwpow, Sept2.w7 n w hw7]; exact CubicChar.pw_one'' (ring (2 * (7 * n + 6) + 1 + 1)) i

/-- δ(x⁷, c⁷) = Σ_(i<7) δ(x, c w^i). -/
theorem seven_roots (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2   -- (+1358 chars; full line in the Zenodo source)
  have hk1 := Sept2.wk_ne_one n w hw1 hw7 1 1 0 rfl
  have hk2 := Sept2.wk_ne_one n w hw1 hw7 2 4 1 rfl
  have hk3 := Sept2.wk_ne_one n w hw1 hw7 3 5 2 rfl
  have hk4 := Sept2.wk_ne_one n w hw1 hw7 4 2 1 rfl
  have hk5 := Sept2.wk_ne_one n w hw1 hw7 5 3 2 rfl
  have hk6 := Sept2.wk_ne_one n w hw1 hw7 6 6 5 rfl
  unfold delta
  by_cases hx : (pw (ring (2 * (7 * n + 6) + 1 + 1)) x 7) = (pw (ring (2 * (7 * n + 6) + 1 + 1)) c 7)
  · rw [ite_pos' _ _ hx]
    have hcv : (ring (2 * (7 * n + 6) + 1 + 1)).mul c ((CharSumGen.iv (2 * (7 * n + 6))) c) = (ring (2 * (7 * n + 6) + 1 + 1)).one := by rw [(ring (2 * (7 * n + 6) + 1 + 1)).mul_comm]; exact CharSumGen.iv_mul (2 * (7 * n + 6)) hpr c hc
    have ht : pw (ring (2 * (7 * n + 6) + 1 + 1)) ((ring (2 * (7 * n + 6) + 1 + 1)).mul x ((CharSumGen.iv (2 * (7 * n + 6))) c)) 7 = (ring (2 * (7 * n + 6) + 1 + 1)).one := by rw [pw_mul, hx, ← pw_mul, hcv]; exact CubicChar.pw_one'' (ring (2 * (7 * n + 6) + 1 + 1)) 7
    have ht' : ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul x ((CharSumGen.iv (2 * (7 * n + 6))) c)) ((ring (2 * (7 * n + 6) + 1 + 1)).mul x ((CharSumGen.iv (2 * (7 * n + 6))) c))  -- (+446 chars; full line in the Zenodo source)
      rw [← ht]; show _ = ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).mul x ((CharSumGen.iv (2 * (7 * n + 6  -- (+498 chars; full line in the Zenodo source)
    have back : ∀ s, ((ring (2 * (7 * n + 6) + 1 + 1)).mul x ((CharSumGen.iv (2 * (7 * n + 6))) c)) = s → x = (ring (2 * (7 * n + 6) + 1 + 1)).mul c s := fun s e => by
      rw [← e, show (ring (2 * (7 * n + 6) + 1 + 1)).mul c ((ring (2 * (7 * n + 6) + 1 + 1)).mul x ((CharSumGen.iv (2 * (7 * n + 6))) c)) = (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul c ((CharSumGen.iv (2 * (7 * n + 6))) c)) x by lib_ring, hcv]; lib_ring
    rcases Sept1.seven_cases n hpr w hw1 hw7 _ ht' with h | h | h | h | h | h | h
    · have e := back _ h
      rw [show (ring (2 * (7 * n + 6) + 1 + 1)).one = (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0) by show _ = (ring (2 * (7 * n + 6) + 1 + 1)).one; lib_ring] at e
      rw [ite_pos' _ _ e, ite_neg' _ _ (by rw [e]; exact (cw_ne n hpr w hw1 hw7 c hc 0 1 hk1 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0) ≠ (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 1))), ite_neg' _ _ (by rw [e]; exact (cw_ne n hpr w hw1 hw7 c hc 0 2 hk2 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1  -- (+1051 chars; full line in the Zenodo source)
    · have e := back _ h
      rw [show w = (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 1) by show _ = ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one w); lib_ring] at e
      rw [ite_neg' _ _ (by rw [e]; exact (fun e' => (cw_ne n hpr w hw1 hw7 c hc 0 1 hk1 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0) ≠ (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 1)) e'.symm)), ite_pos' _ _ e, ite_neg' _ _ (by rw [e]; exact (cw_ne n hpr w hw1 hw7 c hc 1 1 hk1 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring   -- (+1071 chars; full line in the Zenodo source)
    · have e := back _ h
      rw [show ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) = (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 2) by show _ = ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one w) w); lib_ring] at e
      rw [ite_neg' _ _ (by rw [e]; exact (fun e' => (cw_ne n hpr w hw1 hw7 c hc 0 2 hk2 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0) ≠ (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 2)) e'.symm)), ite_neg' _ _ (by rw [e]; exact (fun e' => (cw_ne n hpr w hw1 hw7 c hc 1 1 hk1 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 *   -- (+1091 chars; full line in the Zenodo source)
    · have e := back _ h
      rw [show ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) = (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 3) by show _ = ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one w) w) w); lib_ring] at e
      rw [ite_neg' _ _ (by rw [e]; exact (fun e' => (cw_ne n hpr w hw1 hw7 c hc 0 3 hk3 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0) ≠ (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 3)) e'.symm)), ite_neg' _ _ (by rw [e]; exact (fun e' => (cw_ne n hpr w hw1 hw7 c hc 1 2 hk2 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 *   -- (+1111 chars; full line in the Zenodo source)
    · have e := back _ h
      rw [show ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) w) = (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 4) by show _ = ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one w) w) w) w); li  -- (+12 chars; full line in the Zenodo source)
      rw [ite_neg' _ _ (by rw [e]; exact (fun e' => (cw_ne n hpr w hw1 hw7 c hc 0 4 hk4 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0) ≠ (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 4)) e'.symm)), ite_neg' _ _ (by rw [e]; exact (fun e' => (cw_ne n hpr w hw1 hw7 c hc 1 3 hk3 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 *   -- (+1131 chars; full line in the Zenodo source)
    · have e := back _ h
      rw [show ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) w) w) = (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 5) by show _ = ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *  -- (+94 chars; full line in the Zenodo source)
      rw [ite_neg' _ _ (by rw [e]; exact (fun e' => (cw_ne n hpr w hw1 hw7 c hc 0 5 hk5 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0) ≠ (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 5)) e'.symm)), ite_neg' _ _ (by rw [e]; exact (fun e' => (cw_ne n hpr w hw1 hw7 c hc 1 4 hk4 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 *   -- (+1151 chars; full line in the Zenodo source)
    · have e := back _ h
      rw [show ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul w w) w) w) w) w) = (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 6) by show _ = ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (  -- (+176 chars; full line in the Zenodo source)
      rw [ite_neg' _ _ (by rw [e]; exact (fun e' => (cw_ne n hpr w hw1 hw7 c hc 0 6 hk6 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0) ≠ (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 * (7 * n + 6) + 1 + 1)) w 6)) e'.symm)), ite_neg' _ _ (by rw [e]; exact (fun e' => (cw_ne n hpr w hw1 hw7 c hc 1 5 hk5 : (ring (2 * (7 * n + 6) + 1 + 1)).mul c (pw (ring (2 *   -- (+1171 chars; full line in the Zenodo source)
  · rw [ite_neg' _ _ hx, ite_neg' _ _ (fun e => hx (by rw [e, pw_mul, wi7 n hpr w hw1 hw7 0]; lib_ring)), ite_neg' _ _ (fun e => hx (by rw [e, pw_mul, wi7 n hpr w hw1 hw7 1]; lib_ring)), ite_neg' _ _ (fun e => hx (by rw [e, pw_mul, wi7 n hpr w hw1 hw7 2]; lib_ring)), ite_neg' _ _ (fun e => hx (by rw [e, pw_mul, wi7 n hpr w hw1 hw7 3]; lib_ring)), ite_neg' _ _ (fun e => hx (by rw [e, pw_mul, wi7 n   -- (+191 chars; full line in the Zenodo source)
    lib_ring

/-- a ≠ 0, a^((p−1)/7) = 1 ⇒ a is a seventh power. -/
theorem seven_root (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (a : Zp (2 * (7 * n + 6) + 1 + 1)) (ha0 : a ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero) (ha : (pw (ring (2 * (7 * n + 6) + 1 + 1)) a (2 * n + 2)) = (ring (2 * (7 * n + 6) + 1 + 1)).one) : ∃ c, (pw (ring (2 * (7 * n + 6) + 1 + 1)) c 7) = a := by
  match PrimRoot.primitive_root (2 * (7 * n + 6) + 1) hpr with
  | ⟨g, hg⟩ =>
    match DLog.mem_pows (2 * (7 * n + 6) + 1) g a _ (DLog.every_unit (2 * (7 * n + 6) + 1) hpr g hg a ha0) with
    | ⟨k, _, hk⟩ =>
      have h1 : (pw (ring (2 * (7 * n + 6) + 1 + 1)) g (k * (2 * n + 2))) = (ring (2 * (7 * n + 6) + 1 + 1)).one := by rw [← TwoPower.pwpow, ← hk, ha]
      match hg.2 _ h1 with
      | ⟨c, hc⟩ =>
        have h3 : k = 7 * c := by
          apply Nat.eq_of_mul_eq_mul_right (Nat.zero_lt_succ (2 * n + 1))
          rw [hc]; lib_nat
        exact ⟨(pw (ring (2 * (7 * n + 6) + 1 + 1)) g c), by rw [hk, h3, TwoPower.pwpow, show c * 7 = 7 * c by lib_nat]⟩

theorem cs (z : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun x => (delta (Cyclo7.cyc7 LibInt.intR) x z)) (CharSumGen.LL (2 * (7 * n + 6))) = (Cyclo7.cyc7 LibInt.intR).one := CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR) z

theorem zsum {A' : Type} : ∀ l : List A', sumK (Cyclo7.cyc7 LibInt.intR) (fun _ => (Cyclo7.cyc7 LibInt.intR).zero) l = (Cyclo7.cyc7 LibInt.intR).zero
  | [] => rfl
  | a :: l => by
    show (Cyclo7.cyc7 LibInt.intR).add (Cyclo7.cyc7 LibInt.intR).zero (sumK (Cyclo7.cyc7 LibInt.intR) (fun _ => (Cyclo7.cyc7 LibInt.intR).zero) l) = (Cyclo7.cyc7 LibInt.intR).zero
    rw [zsum l]; lib_ring

theorem zi7 (i : Nat) : pw (Cyclo7.cyc7 LibInt.intR) (pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta i) 7 = (Cyclo7.cyc7 LibInt.intR).one := by
  rw [TwoPower.pwpow, show i * 7 = 7 * i + 0 by lib_nat, Sept2.zt_mod7 i 0]; rfl

theorem sum_zero (i : Nat) (hi : i < 7) (hi0 : i ≠ 0) : ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add (pw (Cyclo7.cyc7 LibInt.intR) (pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta i) 0) (pw (Cyclo7.cyc7 LibInt.intR) (pw (Cyclo7.cyc7 LibInt.intR) Sept2.zeta i) 1)) (pw  -- (+422 chars; full line in the Zenodo source)
  Sept3.kill _ i hi hi0 (by rw [geo7, zi7]; lib_ring)

/-- **N(a) = #{x : x⁷ = a} = Σ_(k<7) χ(a)^k.** -/
theorem seven_count (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2   -- (+841 chars; full line in the Zenodo source)
  by_cases ha : a = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [ha, sumK_ext (Cyclo7.cyc7 LibInt.intR) _ (fun x => (delta (Cyclo7.cyc7 LibInt.intR) x (ring (2 * (7 * n + 6) + 1 + 1)).zero)) (fun x => by
      unfold delta
      by_cases hx : x = (ring (2 * (7 * n + 6) + 1 + 1)).zero
      · rw [ite_pos' _ _ hx, ite_pos' _ _ (by rw [hx]; exact HasseCM.pw_zero_succ (ring (2 * (7 * n + 6) + 1 + 1)) 6)]
      · rw [ite_neg' _ _ hx, ite_neg' _ _ (Sept2.pwnz n hpr x hx 7)]), cs n (ring (2 * (7 * n + 6) + 1 + 1)).zero, Sept2.chi7_zero,
      HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 0, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 1, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 2, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 3,
      HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 4, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 5]
    show _ = (Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add (Cyclo7.cyc7 LibInt.intR).one (Cyclo7.cyc7 LibInt.intR).zero) (Cyclo7.cyc7 LibInt.intR).zero) (Cyclo7.cyc7 LibInt.intR).zero) (Cyclo7.cyc7 LibInt.intR).zero) (Cyclo7.cyc7 LibInt.intR).zero) (Cyclo7.cyc7  -- (+18 chars; full line in the Zenodo source)
    lib_ring
  by_cases hN : (pw (ring (2 * (7 * n + 6) + 1 + 1)) a (2 * n + 2)) = (ring (2 * (7 * n + 6) + 1 + 1)).one
  · match seven_root n hpr a ha hN with
    | ⟨c, hc⟩ =>
      have hc0 : c ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := fun e => ha (by rw [← hc, e]; exact HasseCM.pw_zero_succ (ring (2 * (7 * n + 6) + 1 + 1)) 6)
      have h1 : Sept2.chi7 n w a = (Cyclo7.cyc7 LibInt.intR).one := Sept2.val_of n hpr w hw1 hw7 a ha 0 (Nat.zero_lt_succ 6) hN
      rw [h1, CubicChar.pw_one'' (Cyclo7.cyc7 LibInt.intR) 0, CubicChar.pw_one'' (Cyclo7.cyc7 LibInt.intR) 1, CubicChar.pw_one'' (Cyclo7.cyc7 LibInt.intR) 2, CubicChar.pw_one'' (Cyclo7.cyc7 LibInt.intR) 3,
        CubicChar.pw_one'' (Cyclo7.cyc7 LibInt.intR) 4, CubicChar.pw_one'' (Cyclo7.cyc7 LibInt.intR) 5, CubicChar.pw_one'' (Cyclo7.cyc7 LibInt.intR) 6, ← hc,
        sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun x => seven_roots n hpr w hw1 hw7 c hc0 x), sumK_add, sumK_add, sumK_add, sumK_add, sumK_add, sumK_add,
        cs n, cs n, cs n, cs n, cs n, cs n, cs n]
      all_goals lib_ring
  · have hz : ∀ x, (delta (Cyclo7.cyc7 LibInt.intR) (pw (ring (2 * (7 * n + 6) + 1 + 1)) x 7) a) = (Cyclo7.cyc7 LibInt.intR).zero := fun x => by
      unfold delta
      rw [ite_neg' _ _ (fun e => hN (by
        have hx0 : x ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := fun e0 => ha (by rw [← e, e0]; exact HasseCM.pw_zero_succ (ring (2 * (7 * n + 6) + 1 + 1)) 6)
        rw [← e, TwoPower.pwpow, show 7 * (2 * n + 2) = 2 * (7 * n + 6) + 1 + 1 by lib_nat]
        exact unit_pow (2 * (7 * n + 6) + 1) hpr x hx0))]
    rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ hz, zsum]
    match Sept2.exists_i n hpr w hw1 hw7 a ha with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 a ha i hi ei]
      have hi0 : i ≠ 0 := fun e => hN ((ei.trans (congrArg (pw (ring (2 * (7 * n + 6) + 1 + 1)) w) e)).trans (rfl : pw (ring (2 * (7 * n + 6) + 1 + 1)) w 0 = (ring (2 * (7 * n + 6) + 1 + 1)).one))
      exact (sum_zero i hi hi0).symm

end Sept19

#print axioms Sept19.geo7
#print axioms Sept19.mz
#print axioms Sept19.cw_ne
#print axioms Sept19.wi7
#print axioms Sept19.seven_roots
#print axioms Sept19.seven_root
#print axioms Sept19.cs
#print axioms Sept19.zsum
#print axioms Sept19.zi7
#print axioms Sept19.sum_zero
#print axioms Sept19.seven_count

#print axioms: 11 theorems, no axioms.

78.214 Points on the Fermat septic

The number of points of \(x^7+y^7=1\) over \(\mathbb F_p\) is the sum of all septic Jacobi sums.

\[ \#\{(x,y):x^7+y^7=1\}=\textstyle \sum _{k,m<7}J(\chi ^k,\chi ^m)\ \text {in}\ \mathbb Z[\zeta _7]. \]

lang/Sept20.lean

/-
  lang/Sept20 — T49, cycle 791 (ℓ = 7; K1 step 2, the Fermat septic count): **#{x⁷ + y⁷ = 1} = Σ_(k,m<7) J(χ^k, χ^m)** in ℤ[ζ₇],
  with χ^0 ≡ 1 (including at 0) and J(χ^k, χ^m) = Σ_t χ^k(t) χ^m(1 − t) (generated by s20gen.py; FermatCubic pattern).
    step1          Σ_y δ(x⁷ + y⁷, 1) = N(1 − x⁷)                        (Sept19.seven_count)
    step2          Σ_x h(x⁷) = Σ_a N(a) h(a)
    fermat7        Σ_(x,y) δ(x⁷ + y⁷, 1) = Σ_a N(a) N(1 − a) = Σ_(k,m<7) J(χ^k, χ^m)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Sept19

namespace Sept20

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

theorem prod7 {R : Type} (K : CRing R) (a0 a1 a2 a3 a4 a5 a6 b0 b1 b2 b3 b4 b5 b6 : R) : K.mul (K.add (K.add (K.add (K.add (K.add (K.add a0 a1) a2) a3) a4) a5) a6) (K.add (K.add (K.add (K.add (K.add (K.add b0 b1) b2) b3) b4) b5) b6) = (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.a  -- (+910 chars; full line in the Zenodo source)
  lib_ring

variable (n : Nat)

def om1 (a : Zp (2 * (7 * n + 6) + 1 + 1)) : Zp (2 * (7 * n + 6) + 1 + 1) := (ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg a)

theorem j00 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 0) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 0)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j01 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 0) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 1)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j02 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 0) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 2)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j03 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 0) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 3)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j04 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 0) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 4)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j05 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 0) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 5)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j06 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 0) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 6)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j10 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 1) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 0)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j11 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 1) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 1)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j12 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 1) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 2)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j13 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 1) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 3)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j14 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 1) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 4)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j15 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 1) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 5)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j16 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 1) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 6)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j20 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 2) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 0)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j21 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 2) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 1)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j22 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 2) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 2)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j23 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 2) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 3)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j24 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 2) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 4)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j25 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 2) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 5)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j26 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 2) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 6)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j30 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 3) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 0)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j31 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 3) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 1)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j32 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 3) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 2)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j33 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 3) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 3)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j34 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 3) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 4)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j35 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 3) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 5)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j36 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 3) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 6)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j40 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 4) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 0)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j41 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 4) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 1)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j42 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 4) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 2)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j43 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 4) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 3)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j44 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 4) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 4)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j45 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 4) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 5)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j46 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 4) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 6)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j50 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 5) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 0)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j51 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 5) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 1)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j52 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 5) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 2)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j53 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 5) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 3)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j54 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 5) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 4)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j55 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 5) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 5)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j56 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 5) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 6)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j60 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 6) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 0)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j61 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 6) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 1)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j62 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 6) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 2)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j63 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 6) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 3)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j64 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 6) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 4)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j65 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 6) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 5)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)
theorem j66 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 6) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 6)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7   -- (+95 chars; full line in the Zenodo source)

/-- the number of points of x⁷ + y⁷ = 1 over 𝔽_p (in ℤ[ζ₇], first coordinate). -/
def Fc7 : Int × Int × Int × Int × Int × Int := sumK (Cyclo7.cyc7 LibInt.intR) (fun x => sumK (Cyclo7.cyc7 LibInt.intR) (fun y => (delta (Cyclo7.cyc7 LibInt.intR) ((ring (2 * (7 * n + 6) + 1 + 1)).add (pw (ring (2 * (7 * n + 6) + 1 + 1)) x 7) (pw (ring (2 * (7 * n + 6) + 1 + 1)) y 7)) (ring (2 * (7 * n + 6) + 1 + 1)).one)) (CharSumGen.LL (2 * (7 * n + 6)))) (CharSumGen.LL (2 * (7 * n + 6)))

theorem step1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+1287 chars; full line in the Zenodo source)
  rw [← Sept19.seven_count n hpr w hw1 hw7 (om1 n (pw (ring (2 * (7 * n + 6) + 1 + 1)) x 7))]
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun y => by
    unfold delta om1
    by_cases h : (ring (2 * (7 * n + 6) + 1 + 1)).add (pw (ring (2 * (7 * n + 6) + 1 + 1)) x 7) (pw (ring (2 * (7 * n + 6) + 1 + 1)) y 7) = (ring (2 * (7 * n + 6) + 1 + 1)).one
    · rw [ite_pos' _ _ h, ite_pos' _ _ (by rw [← h]; lib_ring)]
    · rw [ite_neg' _ _ h, ite_neg' _ _ (fun e => h (by rw [e]; lib_ring))]) _

theorem step2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+153 chars; full line in the Zenodo source)
    sumK (Cyclo7.cyc7 LibInt.intR) (fun x => h (pw (ring (2 * (7 * n + 6) + 1 + 1)) x 7)) (CharSumGen.LL (2 * (7 * n + 6))) = sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add (pw (Cyclo7.cyc7   -- (+399 chars; full line in the Zenodo source)
  have e1 : sumK (Cyclo7.cyc7 LibInt.intR) (fun x => h (pw (ring (2 * (7 * n + 6) + 1 + 1)) x 7)) (CharSumGen.LL (2 * (7 * n + 6))) = sumK (Cyclo7.cyc7 LibInt.intR) (fun x => sumK (Cyclo7.cyc7 LibInt.intR) (fun a => (Cyclo7.cyc7 LibInt.intR).mul (delta (Cyclo7.cyc7 LibInt.intR) a (pw (ring (2 * (7 * n + 6) + 1 + 1)) x 7)) (h a)) (CharSumGen.LL (2 * (7 * n + 6)))) (CharSumGen.LL (2 * (7 * n + 6)))   -- (+2 chars; full line in the Zenodo source)
    sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun x => (pick (Cyclo7.cyc7 LibInt.intR) _ (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR)) h (pw (ring (2 * (7 * n + 6) + 1 + 1)) x 7)).symm) _
  rw [e1, sumK_swap]
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun a => by
    rw [← sumK_mul, ← Sept19.seven_count n hpr w hw1 hw7 a]
    exact congrArg (fun z => (Cyclo7.cyc7 LibInt.intR).mul z (h a)) (sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun x => delta_symm (Cyclo7.cyc7 LibInt.intR) a (pw (ring (2 * (7 * n + 6) + 1 + 1)) x 7)) _)) _

/-- **#{x⁷ + y⁷ = 1} = Σ_(k,m<7) J(χ^k, χ^m).** -/
theorem fermat7 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+12414 chars; full line in the Zenodo source)
  unfold Fc7
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun x => step1 n hpr w hw1 hw7 x), step2 n hpr w hw1 hw7 (fun a => ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w (om1 n a)) 0) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7  -- (+326 chars; full line in the Zenodo source)
  have hexp : ∀ a, (Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 0) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 1)) (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w a) 2)) (pw (Cy  -- (+10238 chars; full line in the Zenodo source)
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ hexp]
  repeat rw [sumK_add]
  rw [j00 n w, j01 n w, j02 n w, j03 n w, j04 n w, j05 n w, j06 n w, j10 n w, j11 n w, j12 n w, j13 n w, j14 n w, j15 n w, j16 n w, j20 n w, j21 n w, j22 n w, j23 n w, j24 n w, j25 n w, j26 n w, j30 n w, j31 n w, j32 n w, j33 n w, j34 n w, j35 n w, j36 n w, j40 n w, j41 n w, j42 n w, j43 n w, j44 n w, j45 n w, j46 n w, j50 n w, j51 n w, j52 n w, j53 n w, j54 n w, j55 n w, j56 n w, j60 n w, j61 n w  -- (+46 chars; full line in the Zenodo source)

end Sept20

#print axioms Sept20.prod7
#print axioms Sept20.j00
#print axioms Sept20.j01
#print axioms Sept20.j02
#print axioms Sept20.j03
#print axioms Sept20.j04
#print axioms Sept20.j05
#print axioms Sept20.j06
#print axioms Sept20.j10
#print axioms Sept20.j11
#print axioms Sept20.j12
#print axioms Sept20.j13
#print axioms Sept20.j14
#print axioms Sept20.j15
#print axioms Sept20.j16
#print axioms Sept20.j20
#print axioms Sept20.j21
#print axioms Sept20.j22
#print axioms Sept20.j23
#print axioms Sept20.j24
#print axioms Sept20.j25
#print axioms Sept20.j26
#print axioms Sept20.j30
#print axioms Sept20.j31
#print axioms Sept20.j32
#print axioms Sept20.j33
#print axioms Sept20.j34
#print axioms Sept20.j35
#print axioms Sept20.j36
#print axioms Sept20.j40
#print axioms Sept20.j41
#print axioms Sept20.j42
#print axioms Sept20.j43
#print axioms Sept20.j44
#print axioms Sept20.j45
#print axioms Sept20.j46
#print axioms Sept20.j50
#print axioms Sept20.j51
#print axioms Sept20.j52
#print axioms Sept20.j53
#print axioms Sept20.j54
#print axioms Sept20.j55
#print axioms Sept20.j56
#print axioms Sept20.j60
#print axioms Sept20.j61
#print axioms Sept20.j62
#print axioms Sept20.j63
#print axioms Sept20.j64
#print axioms Sept20.j65
#print axioms Sept20.j66
#print axioms Sept20.step1
#print axioms Sept20.step2
#print axioms Sept20.fermat7

#print axioms: 53 theorems, no axioms.

78.215 Trivial-character septic Jacobi sums

The Jacobi sums with a trivial character or with \(\chi ^k\chi ^{7-k}\) trivial are explicit, which reduces the Fermat septic count.

\[ J(1,1)=p,\quad J(1,\chi ^m)=0,\quad J(\chi ^k,\chi ^{7-k})=-1,\quad \#\{x^7+y^7=1\}=p-6+\textstyle \sum '_{k,m}J(\chi ^k,\chi ^m). \]

lang/Sept21.lean

/-
  lang/Sept21 — T49, cycle 792 (ℓ = 7; K1 → K2): **the trivial-character septic Jacobi sums and the Fermat septic count**
  (generated by s21gen.py). With χ^0 ≡ 1:
    inv1 … inv6        χ^(7−k)(y) = χ^k(y⁻¹)              (Sept4.chi_pow6, ζ^(7m+r) = ζ^r)
    Jinv1 … Jinv6      J(χ^k, χ^(7−k)) = −1               (shift t = 1 + b, CharSumGen.D_ne with c = 1, χ(−1) = 1)
    J0_m, Jm_0, J00    J(1, χ^m) = J(χ^m, 1) = 0, J(1, 1) = p
    fermat7'           #{x⁷ + y⁷ = 1} = p − 6 + Σ_(k,m ≥ 1, k+m ≢ 0 mod 7) J(χ^k, χ^m)   (30 terms)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Sept20

namespace Sept21

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

variable (n : Nat)

theorem inv1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+260 chars; full line in the Zenodo source)
  rw [← Sept4.chi_pow6 n hpr w hw1 hw7 y, TwoPower.pwpow]

theorem inv2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+260 chars; full line in the Zenodo source)
  rw [← Sept4.chi_pow6 n hpr w hw1 hw7 y, TwoPower.pwpow]
  by_cases hy : y = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hy, Sept2.chi7_zero, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 4, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 11]
  · match Sept2.exists_i n hpr w hw1 hw7 y hy with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 y hy i hi ei, TwoPower.pwpow, TwoPower.pwpow, show i * (6 * 2) = 7 * (i * 1) + i * 5 by lib_nat, Sept2.zt_mod7]

theorem inv3 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+260 chars; full line in the Zenodo source)
  rw [← Sept4.chi_pow6 n hpr w hw1 hw7 y, TwoPower.pwpow]
  by_cases hy : y = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hy, Sept2.chi7_zero, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 3, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 17]
  · match Sept2.exists_i n hpr w hw1 hw7 y hy with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 y hy i hi ei, TwoPower.pwpow, TwoPower.pwpow, show i * (6 * 3) = 7 * (i * 2) + i * 4 by lib_nat, Sept2.zt_mod7]

theorem inv4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+260 chars; full line in the Zenodo source)
  rw [← Sept4.chi_pow6 n hpr w hw1 hw7 y, TwoPower.pwpow]
  by_cases hy : y = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hy, Sept2.chi7_zero, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 2, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 23]
  · match Sept2.exists_i n hpr w hw1 hw7 y hy with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 y hy i hi ei, TwoPower.pwpow, TwoPower.pwpow, show i * (6 * 4) = 7 * (i * 3) + i * 3 by lib_nat, Sept2.zt_mod7]

theorem inv5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+260 chars; full line in the Zenodo source)
  rw [← Sept4.chi_pow6 n hpr w hw1 hw7 y, TwoPower.pwpow]
  by_cases hy : y = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hy, Sept2.chi7_zero, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 1, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 29]
  · match Sept2.exists_i n hpr w hw1 hw7 y hy with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 y hy i hi ei, TwoPower.pwpow, TwoPower.pwpow, show i * (6 * 5) = 7 * (i * 4) + i * 2 by lib_nat, Sept2.zt_mod7]

theorem inv6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+260 chars; full line in the Zenodo source)
  rw [← Sept4.chi_pow6 n hpr w hw1 hw7 y, TwoPower.pwpow]
  by_cases hy : y = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hy, Sept2.chi7_zero, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 0, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) 35]
  · match Sept2.exists_i n hpr w hw1 hw7 y hy with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 y hy i hi ei, TwoPower.pwpow, TwoPower.pwpow, show i * (6 * 6) = 7 * (i * 5) + i * 1 by lib_nat, Sept2.zt_mod7]

theorem Jinv1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+370 chars; full line in the Zenodo source)
  have h10 : (ring (2 * (7 * n + 6) + 1 + 1)).one ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * (7 * n + 6) + 1)
  unfold CharSumGen.J2
  rw [← CharSumGen.shift1 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)), sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun b => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one b)) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) ((ring (2 * (  -- (+515 chars; full line in the Zenodo source)
    unfold CharSumGen.psp
    rw [show ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one b))) = (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) b by lib_ring, inv1 n hpr w hw1 hw7, CharSumGen.iv_mulmul, Sept2.chi7_mul n hpr  -- (+11 chars; full line in the Zenodo source)
      show Sept2.chi7 n w ((CharSumGen.iv (2 * (7 * n + 6))) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = (Cyclo7.cyc7 LibInt.intR).one from Sept3.chib_neg1 n hpr w hw1 hw7, (Cyclo7.cyc7 LibInt.intR).one_mul])]
  exact CharSumGen.D_ne (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 1 x y) (Sept13.kap_one n hpr w hw1 hw7 1) (Sept13.kap_zero n w 0) (Sept13.kap_sum1 n hpr w hw1 hw7) (ring (2 * (7 * n + 6) + 1 + 1)).one h10

theorem Jinv2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+370 chars; full line in the Zenodo source)
  have h10 : (ring (2 * (7 * n + 6) + 1 + 1)).one ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * (7 * n + 6) + 1)
  unfold CharSumGen.J2
  rw [← CharSumGen.shift1 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)), sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun b => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one b)) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) ((ring (2 * (  -- (+515 chars; full line in the Zenodo source)
    unfold CharSumGen.psp
    rw [show ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one b))) = (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) b by lib_ring, inv2 n hpr w hw1 hw7, CharSumGen.iv_mulmul, Sept2.chi7_mul n hpr  -- (+11 chars; full line in the Zenodo source)
      show Sept2.chi7 n w ((CharSumGen.iv (2 * (7 * n + 6))) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = (Cyclo7.cyc7 LibInt.intR).one from Sept3.chib_neg1 n hpr w hw1 hw7, (Cyclo7.cyc7 LibInt.intR).one_mul])]
  exact CharSumGen.D_ne (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 2 x y) (Sept13.kap_one n hpr w hw1 hw7 2) (Sept13.kap_zero n w 1) (Sept13.kap_sum2 n hpr w hw1 hw7) (ring (2 * (7 * n + 6) + 1 + 1)).one h10

theorem Jinv3 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+370 chars; full line in the Zenodo source)
  have h10 : (ring (2 * (7 * n + 6) + 1 + 1)).one ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * (7 * n + 6) + 1)
  unfold CharSumGen.J2
  rw [← CharSumGen.shift1 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)), sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun b => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one b)) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) ((ring (2 * (  -- (+515 chars; full line in the Zenodo source)
    unfold CharSumGen.psp
    rw [show ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one b))) = (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) b by lib_ring, inv3 n hpr w hw1 hw7, CharSumGen.iv_mulmul, Sept2.chi7_mul n hpr  -- (+11 chars; full line in the Zenodo source)
      show Sept2.chi7 n w ((CharSumGen.iv (2 * (7 * n + 6))) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = (Cyclo7.cyc7 LibInt.intR).one from Sept3.chib_neg1 n hpr w hw1 hw7, (Cyclo7.cyc7 LibInt.intR).one_mul])]
  exact CharSumGen.D_ne (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 3 x y) (Sept13.kap_one n hpr w hw1 hw7 3) (Sept13.kap_zero n w 2) (Sept13.kap_sum3 n hpr w hw1 hw7) (ring (2 * (7 * n + 6) + 1 + 1)).one h10

theorem Jinv4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+370 chars; full line in the Zenodo source)
  have h10 : (ring (2 * (7 * n + 6) + 1 + 1)).one ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * (7 * n + 6) + 1)
  unfold CharSumGen.J2
  rw [← CharSumGen.shift1 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)), sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun b => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one b)) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) ((ring (2 * (  -- (+515 chars; full line in the Zenodo source)
    unfold CharSumGen.psp
    rw [show ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one b))) = (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) b by lib_ring, inv4 n hpr w hw1 hw7, CharSumGen.iv_mulmul, Sept2.chi7_mul n hpr  -- (+11 chars; full line in the Zenodo source)
      show Sept2.chi7 n w ((CharSumGen.iv (2 * (7 * n + 6))) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = (Cyclo7.cyc7 LibInt.intR).one from Sept3.chib_neg1 n hpr w hw1 hw7, (Cyclo7.cyc7 LibInt.intR).one_mul])]
  exact CharSumGen.D_ne (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 4 x y) (Sept13.kap_one n hpr w hw1 hw7 4) (Sept13.kap_zero n w 3) (Sept4.chi_pow_sum4 n hpr w hw1 hw7) (ring (2 * (7 * n + 6) + 1 + 1)).one h10

theorem Jinv5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+370 chars; full line in the Zenodo source)
  have h10 : (ring (2 * (7 * n + 6) + 1 + 1)).one ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * (7 * n + 6) + 1)
  unfold CharSumGen.J2
  rw [← CharSumGen.shift1 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)), sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun b => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one b)) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) ((ring (2 * (  -- (+515 chars; full line in the Zenodo source)
    unfold CharSumGen.psp
    rw [show ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one b))) = (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) b by lib_ring, inv5 n hpr w hw1 hw7, CharSumGen.iv_mulmul, Sept2.chi7_mul n hpr  -- (+11 chars; full line in the Zenodo source)
      show Sept2.chi7 n w ((CharSumGen.iv (2 * (7 * n + 6))) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = (Cyclo7.cyc7 LibInt.intR).one from Sept3.chib_neg1 n hpr w hw1 hw7, (Cyclo7.cyc7 LibInt.intR).one_mul])]
  exact CharSumGen.D_ne (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 5 x y) (Sept13.kap_one n hpr w hw1 hw7 5) (Sept13.kap_zero n w 4) (Sept4.chi_pow_sum5 n hpr w hw1 hw7) (ring (2 * (7 * n + 6) + 1 + 1)).one h10

theorem Jinv6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+370 chars; full line in the Zenodo source)
  have h10 : (ring (2 * (7 * n + 6) + 1 + 1)).one ≠ (ring (2 * (7 * n + 6) + 1 + 1)).zero := PowerSum.one_ne_zero' (2 * (7 * n + 6) + 1)
  unfold CharSumGen.J2
  rw [← CharSumGen.shift1 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)), sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun b => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one b)) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) ((ring (2 * (  -- (+515 chars; full line in the Zenodo source)
    unfold CharSumGen.psp
    rw [show ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one b))) = (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one) b by lib_ring, inv6 n hpr w hw1 hw7, CharSumGen.iv_mulmul, Sept2.chi7_mul n hpr  -- (+11 chars; full line in the Zenodo source)
      show Sept2.chi7 n w ((CharSumGen.iv (2 * (7 * n + 6))) ((ring (2 * (7 * n + 6) + 1 + 1)).neg (ring (2 * (7 * n + 6) + 1 + 1)).one)) = (Cyclo7.cyc7 LibInt.intR).one from Sept3.chib_neg1 n hpr w hw1 hw7, (Cyclo7.cyc7 LibInt.intR).one_mul])]
  exact CharSumGen.D_ne (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 6 x y) (Sept13.kap_one n hpr w hw1 hw7 6) (Sept13.kap_zero n w 5) (Sept4.chi_pow_sum6 n hpr w hw1 hw7) (ring (2 * (7 * n + 6) + 1 + 1)).one h10

theorem J0_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+340 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (fun t => (CharSumGen.psp (Cyclo7.c  -- (+220 chars; full line in the Zenodo source)
  exact (reindex (Cyclo7.cyc7 LibInt.intR) _ (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR)) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => by lib_ring)   -- (+143 chars; full line in the Zenodo source)
theorem J1_0 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+340 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (CharSumGen.psp (Cyclo7.cyc7 LibInt  -- (+141 chars; full line in the Zenodo source)
  exact Sept13.kap_sum1 n hpr w hw1 hw7

theorem J0_2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+340 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (fun t => (CharSumGen.psp (Cyclo7.c  -- (+220 chars; full line in the Zenodo source)
  exact (reindex (Cyclo7.cyc7 LibInt.intR) _ (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR)) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => by lib_ring)   -- (+143 chars; full line in the Zenodo source)
theorem J2_0 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+340 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (CharSumGen.psp (Cyclo7.cyc7 LibInt  -- (+141 chars; full line in the Zenodo source)
  exact Sept13.kap_sum2 n hpr w hw1 hw7

theorem J0_3 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+340 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (fun t => (CharSumGen.psp (Cyclo7.c  -- (+220 chars; full line in the Zenodo source)
  exact (reindex (Cyclo7.cyc7 LibInt.intR) _ (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR)) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => by lib_ring)   -- (+143 chars; full line in the Zenodo source)
theorem J3_0 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+340 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (CharSumGen.psp (Cyclo7.cyc7 LibInt  -- (+141 chars; full line in the Zenodo source)
  exact Sept13.kap_sum3 n hpr w hw1 hw7

theorem J0_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+340 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (fun t => (CharSumGen.psp (Cyclo7.c  -- (+220 chars; full line in the Zenodo source)
  exact (reindex (Cyclo7.cyc7 LibInt.intR) _ (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR)) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => by lib_ring)   -- (+146 chars; full line in the Zenodo source)
theorem J4_0 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+340 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (CharSumGen.psp (Cyclo7.cyc7 LibInt  -- (+141 chars; full line in the Zenodo source)
  exact Sept4.chi_pow_sum4 n hpr w hw1 hw7

theorem J0_5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+340 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (fun t => (CharSumGen.psp (Cyclo7.c  -- (+220 chars; full line in the Zenodo source)
  exact (reindex (Cyclo7.cyc7 LibInt.intR) _ (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR)) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => by lib_ring)   -- (+146 chars; full line in the Zenodo source)
theorem J5_0 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+340 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (CharSumGen.psp (Cyclo7.cyc7 LibInt  -- (+141 chars; full line in the Zenodo source)
  exact Sept4.chi_pow_sum5 n hpr w hw1 hw7

theorem J0_6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+340 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (fun t => (CharSumGen.psp (Cyclo7.c  -- (+220 chars; full line in the Zenodo source)
  exact (reindex (Cyclo7.cyc7 LibInt.intR) _ (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR)) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) (fun t => by lib_ring)   -- (+146 chars; full line in the Zenodo source)
theorem J6_0 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+340 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (CharSumGen.psp (Cyclo7.cyc7 LibInt  -- (+141 chars; full line in the Zenodo source)
  exact Sept4.chi_pow_sum6 n hpr w hw1 hw7

theorem J00 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0)) = (Int.ofNat (2 * (7 * n + 6) + 1 + 1 + 1), 0, 0, 0, 0, 0) := by
  unfold CharSumGen.J2
  rw [sumK_ext (Cyclo7.cyc7 LibInt.intR) (fun t => (Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) t) ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0) ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))) (fun _ => (Cyclo7.cyc7 LibInt.intR)  -- (+53 chars; full line in the Zenodo source)
  exact Sept3.sum_one7 n _

/-- **#{x⁷ + y⁷ = 1} = p − 6 + Σ_(k,m≥1, k+m≢0) J(χ^k, χ^m).** -/
theorem fermat7' (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+7814 chars; full line in the Zenodo source)
  rw [Sept20.fermat7 n hpr w hw1 hw7, J00 n w, J0_1 n hpr w hw1 hw7, J0_2 n hpr w hw1 hw7, J0_3 n hpr w hw1 hw7, J0_4 n hpr w hw1 hw7, J0_5 n hpr w hw1 hw7, J0_6 n hpr w hw1 hw7, J1_0 n hpr w hw1 hw7, J2_0 n hpr w hw1 hw7, J3_0 n hpr w hw1 hw7, J4_0 n hpr w hw1 hw7, J5_0 n hpr w hw1 hw7, J6_0 n hpr w hw1 hw7, Jinv1 n hpr w hw1 hw7, Jinv2 n hpr w hw1 hw7, Jinv3 n hpr w hw1 hw7, Jinv4 n hpr w hw1 hw  -- (+48 chars; full line in the Zenodo source)
  show _ = (Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add (Int.ofNat (2 * (7 * n + 6) + 1 + 1 + 1), 0, 0, 0, 0, 0) ((Cyclo7.cyc7 LibInt.intR).neg ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add (Cyclo7.cyc7 LibInt.intR).one (Cyclo7.cyc7 LibInt.intR).one) (Cyclo7.cyc7 LibInt.intR  -- (+7629 chars; full line in the Zenodo source)
  lib_ring

end Sept21

#print axioms Sept21.inv1
#print axioms Sept21.inv2
#print axioms Sept21.inv3
#print axioms Sept21.inv4
#print axioms Sept21.inv5
#print axioms Sept21.inv6
#print axioms Sept21.Jinv1
#print axioms Sept21.Jinv2
#print axioms Sept21.Jinv3
#print axioms Sept21.Jinv4
#print axioms Sept21.Jinv5
#print axioms Sept21.Jinv6
#print axioms Sept21.J0_1
#print axioms Sept21.J1_0
#print axioms Sept21.J0_2
#print axioms Sept21.J2_0
#print axioms Sept21.J0_3
#print axioms Sept21.J3_0
#print axioms Sept21.J0_4
#print axioms Sept21.J4_0
#print axioms Sept21.J0_5
#print axioms Sept21.J5_0
#print axioms Sept21.J0_6
#print axioms Sept21.J6_0
#print axioms Sept21.J00
#print axioms Sept21.fermat7'

#print axioms: 26 theorems, no axioms.

78.216 Fermat septic count by Jacobi-sum classes

Each septic Jacobi sum depends only on the triple \(\{a,b,-a-b\}\), so the Fermat septic count has two kinds of terms.

\[ \#\{x^7+y^7=1\}=p-6+3\textstyle \sum _{k=1}^6J(\chi ^k,\chi ^k)+6\big (J(\chi ,\chi ^2)+J(\chi ^3,\chi ^6)\big ). \]

lang/Sept22.lean

/-
  lang/Sept22 — T49, cycle 793 (ℓ = 7; K2 step 1): **the Fermat septic count by Jacobi-sum classes** (generated by s22gen.py):
  J(χ^a, χ^b) depends only on the triple {a, b, −a−b} (JacRefl with χ(−1) = 1, and J(f, g) = J(g, f)); the 30 nontrivial pairs fall
  into the classes {k, k, −2k} (18 pairs, representative J(χ^k, χ^k)) and {1, 2, 4}, {3, 5, 6} (6 pairs each, J(χ, χ²), J(χ³, χ⁶)):
    pw_mod, negone, mu_a_b, jr_a_b     χ^(7m+r+1) = χ^(r+1); χ^a(−1) = 1; χ^a(x⁻¹)χ^b(x⁻¹) = χ^c(x) (a + b + c ≡ 0); J(a,b) = J(a,c)
    P_a_b                              each pair reduced to its class representative
    fermat7''                          #{x⁷ + y⁷ = 1} = p − 6 + 3 Σ_(k=1..6) J(χ^k, χ^k) + 6 (J(χ, χ²) + J(χ³, χ⁶))
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Sept21

namespace Sept22

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

variable (n : Nat)

theorem pw_mod (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+258 chars; full line in the Zenodo source)
  by_cases hx : x = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · rw [hx, Sept2.chi7_zero, HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) (7 * m + r), HasseCM.pw_zero_succ (Cyclo7.cyc7 LibInt.intR) r]
  · match Sept2.exists_i n hpr w hw1 hw7 x hx with
    | ⟨i, hi, ei⟩ =>
      rw [Sept2.val_of n hpr w hw1 hw7 x hx i hi ei, TwoPower.pwpow, TwoPower.pwpow, show i * (7 * m + r + 1) = 7 * (i * m) + i * (r + 1) by lib_nat, Sept2.zt_mod7]

theorem negone (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+287 chars; full line in the Zenodo source)
  unfold CharSumGen.psp; rw [Sept2.chi7_neg1 n hpr w hw1 hw7]; exact CubicChar.pw_one'' (Cyclo7.cyc7 LibInt.intR) k

theorem mu1_5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+477 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [← Sept21.inv1 n hpr w hw1 hw7 x, ← Sept21.inv5 n hpr w hw1 hw7 x, ← HasseCM.pw_add', show 6 + 2 = 7 * 1 + 0 + 1 from rfl, pw_mod n hpr w hw1 hw7 1 0]
theorem jr1_5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+530 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 1 x y) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 5 x y)
    (Sept13.kap_one n hpr w hw1 hw7 1) (Sept13.kap_zero n w 4) (Sept13.kap_zero n w 0), negone n hpr w hw1 hw7 1, (Cyclo7.cyc7 LibInt.intR).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => congrArg ((Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) t)) (mu1_5 n hpr w hw1 hw7 _)) _

theorem mu2_3 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+477 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [← Sept21.inv2 n hpr w hw1 hw7 x, ← Sept21.inv3 n hpr w hw1 hw7 x, ← HasseCM.pw_add', show 5 + 4 = 7 * 1 + 1 + 1 from rfl, pw_mod n hpr w hw1 hw7 1 1]
theorem jr2_3 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+530 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 2 x y) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 3 x y)
    (Sept13.kap_one n hpr w hw1 hw7 2) (Sept13.kap_zero n w 2) (Sept13.kap_zero n w 1), negone n hpr w hw1 hw7 2, (Cyclo7.cyc7 LibInt.intR).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => congrArg ((Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) t)) (mu2_3 n hpr w hw1 hw7 _)) _

theorem mu3_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+477 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [← Sept21.inv3 n hpr w hw1 hw7 x, ← Sept21.inv1 n hpr w hw1 hw7 x, ← HasseCM.pw_add', show 4 + 6 = 7 * 1 + 2 + 1 from rfl, pw_mod n hpr w hw1 hw7 1 2]
theorem jr3_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+530 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 3 x y) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 1 x y)
    (Sept13.kap_one n hpr w hw1 hw7 3) (Sept13.kap_zero n w 0) (Sept13.kap_zero n w 2), negone n hpr w hw1 hw7 3, (Cyclo7.cyc7 LibInt.intR).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => congrArg ((Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) t)) (mu3_1 n hpr w hw1 hw7 _)) _

theorem mu4_6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+477 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [← Sept21.inv4 n hpr w hw1 hw7 x, ← Sept21.inv6 n hpr w hw1 hw7 x, ← HasseCM.pw_add']
theorem jr4_6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+530 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 4 x y) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 6 x y)
    (Sept13.kap_one n hpr w hw1 hw7 4) (Sept13.kap_zero n w 5) (Sept13.kap_zero n w 3), negone n hpr w hw1 hw7 4, (Cyclo7.cyc7 LibInt.intR).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => congrArg ((Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) t)) (mu4_6 n hpr w hw1 hw7 _)) _

theorem mu5_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+477 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [← Sept21.inv5 n hpr w hw1 hw7 x, ← Sept21.inv4 n hpr w hw1 hw7 x, ← HasseCM.pw_add']
theorem jr5_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+530 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 5 x y) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 4 x y)
    (Sept13.kap_one n hpr w hw1 hw7 5) (Sept13.kap_zero n w 3) (Sept13.kap_zero n w 4), negone n hpr w hw1 hw7 5, (Cyclo7.cyc7 LibInt.intR).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => congrArg ((Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) t)) (mu5_4 n hpr w hw1 hw7 _)) _

theorem mu6_2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+477 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [← Sept21.inv6 n hpr w hw1 hw7 x, ← Sept21.inv2 n hpr w hw1 hw7 x, ← HasseCM.pw_add']
theorem jr6_2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+530 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 6 x y) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 2 x y)
    (Sept13.kap_one n hpr w hw1 hw7 6) (Sept13.kap_zero n w 1) (Sept13.kap_zero n w 5), negone n hpr w hw1 hw7 6, (Cyclo7.cyc7 LibInt.intR).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => congrArg ((Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) t)) (mu6_2 n hpr w hw1 hw7 _)) _

theorem mu1_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+477 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [← Sept21.inv1 n hpr w hw1 hw7 x, ← Sept21.inv4 n hpr w hw1 hw7 x, ← HasseCM.pw_add', show 6 + 3 = 7 * 1 + 1 + 1 from rfl, pw_mod n hpr w hw1 hw7 1 1]
theorem jr1_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+530 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 1 x y) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 4 x y)
    (Sept13.kap_one n hpr w hw1 hw7 1) (Sept13.kap_zero n w 3) (Sept13.kap_zero n w 0), negone n hpr w hw1 hw7 1, (Cyclo7.cyc7 LibInt.intR).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => congrArg ((Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) t)) (mu1_4 n hpr w hw1 hw7 _)) _

theorem mu2_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+477 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [← Sept21.inv2 n hpr w hw1 hw7 x, ← Sept21.inv4 n hpr w hw1 hw7 x, ← HasseCM.pw_add', show 5 + 3 = 7 * 1 + 0 + 1 from rfl, pw_mod n hpr w hw1 hw7 1 0]
theorem jr2_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+530 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 2 x y) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 4 x y)
    (Sept13.kap_one n hpr w hw1 hw7 2) (Sept13.kap_zero n w 3) (Sept13.kap_zero n w 1), negone n hpr w hw1 hw7 2, (Cyclo7.cyc7 LibInt.intR).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => congrArg ((Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) t)) (mu2_4 n hpr w hw1 hw7 _)) _

theorem mu3_5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+477 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [← Sept21.inv3 n hpr w hw1 hw7 x, ← Sept21.inv5 n hpr w hw1 hw7 x, ← HasseCM.pw_add']
theorem jr3_5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+530 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 3 x y) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 5 x y)
    (Sept13.kap_one n hpr w hw1 hw7 3) (Sept13.kap_zero n w 4) (Sept13.kap_zero n w 2), negone n hpr w hw1 hw7 3, (Cyclo7.cyc7 LibInt.intR).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => congrArg ((Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) t)) (mu3_5 n hpr w hw1 hw7 _)) _

theorem mu5_6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+477 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [← Sept21.inv5 n hpr w hw1 hw7 x, ← Sept21.inv6 n hpr w hw1 hw7 x, ← HasseCM.pw_add']
theorem jr5_6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *  -- (+530 chars; full line in the Zenodo source)
  rw [JacRefl.jac_refl (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) hpr (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 5 x y) (fun x y => Sept13.kap_mul n hpr w hw1 hw7 6 x y)
    (Sept13.kap_one n hpr w hw1 hw7 5) (Sept13.kap_zero n w 5) (Sept13.kap_zero n w 4), negone n hpr w hw1 hw7 5, (Cyclo7.cyc7 LibInt.intR).one_mul]
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => congrArg ((Cyclo7.cyc7 LibInt.intR).mul ((CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) t)) (mu5_6 n hpr w hw1 hw7 _)) _

theorem P1_3 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3), jr3_1 n hpr w hw1 hw7]

theorem P1_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [jr1_4 n hpr w hw1 hw7]

theorem P1_5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [jr1_5 n hpr w hw1 hw7]

theorem P2_1 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.ch  -- (+97 chars; full line in the Zenodo source)
  rw [Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1)]

theorem P2_3 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [jr2_3 n hpr w hw1 hw7]

theorem P2_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [jr2_4 n hpr w hw1 hw7, Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1)]

theorem P2_6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6), jr6_2 n hpr w hw1 hw7]

theorem P3_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [jr3_1 n hpr w hw1 hw7]

theorem P3_2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2), jr2_3 n hpr w hw1 hw7]

theorem P3_5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [jr3_5 n hpr w hw1 hw7]

theorem P4_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1), jr1_4 n hpr w hw1 hw7]

theorem P4_2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2), jr2_4 n hpr w hw1 hw7, Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) (CharSumGe  -- (+70 chars; full line in the Zenodo source)

theorem P4_5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5), jr5_4 n hpr w hw1 hw7]

theorem P4_6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [jr4_6 n hpr w hw1 hw7]

theorem P5_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1), jr1_5 n hpr w hw1 hw7]

theorem P5_3 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3), jr3_5 n hpr w hw1 hw7]

theorem P5_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [jr5_4 n hpr w hw1 hw7]

theorem P5_6 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [jr5_6 n hpr w hw1 hw7, Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3), jr3_5 n hpr w hw1 hw7]

theorem P6_2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [jr6_2 n hpr w hw1 hw7]

theorem P6_3 (w : Zp (2 * (7 * n + 6) + 1 + 1)) : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.ch  -- (+97 chars; full line in the Zenodo source)
  rw [Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3)]

theorem P6_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4), jr4_6 n hpr w hw1 hw7]

theorem P6_5 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+529 chars; full line in the Zenodo source)
  rw [Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5), jr5_6 n hpr w hw1 hw7, Quint10.j2_swap (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) (CharSumGe  -- (+93 chars; full line in the Zenodo source)

/-- **#{x⁷ + y⁷ = 1} = p − 6 + 3 Σ_k J(χ^k, χ^k) + 6 (J(χ, χ²) + J(χ³, χ⁶)).** -/
theorem fermat7'' (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+7815 chars; full line in the Zenodo source)
  rw [Sept21.fermat7' n hpr w hw1 hw7, P1_3 n hpr w hw1 hw7, P1_4 n hpr w hw1 hw7, P1_5 n hpr w hw1 hw7, P2_1 n w, P2_3 n hpr w hw1 hw7, P2_4 n hpr w hw1 hw7, P2_6 n hpr w hw1 hw7, P3_1 n hpr w hw1 hw7, P3_2 n hpr w hw1 hw7, P3_5 n hpr w hw1 hw7, P4_1 n hpr w hw1 hw7, P4_2 n hpr w hw1 hw7, P4_5 n hpr w hw1 hw7, P4_6 n hpr w hw1 hw7, P5_1 n hpr w hw1 hw7, P5_3 n hpr w hw1 hw7, P5_4 n hpr w hw1 hw7,  -- (+98 chars; full line in the Zenodo source)
  lib_ring

end Sept22

#print axioms Sept22.pw_mod
#print axioms Sept22.negone
#print axioms Sept22.mu1_5
#print axioms Sept22.jr1_5
#print axioms Sept22.mu2_3
#print axioms Sept22.jr2_3
#print axioms Sept22.mu3_1
#print axioms Sept22.jr3_1
#print axioms Sept22.mu4_6
#print axioms Sept22.jr4_6
#print axioms Sept22.mu5_4
#print axioms Sept22.jr5_4
#print axioms Sept22.mu6_2
#print axioms Sept22.jr6_2
#print axioms Sept22.mu1_4
#print axioms Sept22.jr1_4
#print axioms Sept22.mu2_4
#print axioms Sept22.jr2_4
#print axioms Sept22.mu3_5
#print axioms Sept22.jr3_5
#print axioms Sept22.mu5_6
#print axioms Sept22.jr5_6
#print axioms Sept22.P1_3
#print axioms Sept22.P1_4
#print axioms Sept22.P1_5
#print axioms Sept22.P2_1
#print axioms Sept22.P2_3
#print axioms Sept22.P2_4
#print axioms Sept22.P2_6
#print axioms Sept22.P3_1
#print axioms Sept22.P3_2
#print axioms Sept22.P3_5
#print axioms Sept22.P4_1
#print axioms Sept22.P4_2
#print axioms Sept22.P4_5
#print axioms Sept22.P4_6
#print axioms Sept22.P5_1
#print axioms Sept22.P5_3
#print axioms Sept22.P5_4
#print axioms Sept22.P5_6
#print axioms Sept22.P6_2
#print axioms Sept22.P6_3
#print axioms Sept22.P6_4
#print axioms Sept22.P6_5
#print axioms Sept22.fermat7''

#print axioms: 45 theorems, no axioms.

78.217 Fermat septic count by Galois conjugates of \(J\)

The automorphisms \(\sigma _k\) of \(\mathbb Z[\zeta _7]\) send \(J(\chi ^a,\chi ^b)\) to \(J(\chi ^{ka},\chi ^{kb})\), so the count is a trace.

\[ \#\{x^7+y^7=1\}=p-6+3\textstyle \sum _{k=1}^6\sigma _kJ(\chi ,\chi )+6\big (J(\chi ,\chi ^2)+\overline {J(\chi ,\chi ^2)}\big ). \]

lang/Sept23.lean

/-
  lang/Sept23 — T49, cycle 794 (ℓ = 7; K2 step 2): **the Fermat septic count through the Galois conjugates of J and J'**
  (generated by s23gen.py): σ₂, σ₃, conj act on the septic characters by χ ↦ χ², χ³, χ⁶, so J(χ^k, χ^k) = σ_k J and J(χ³, χ⁶) = conj J':
    h_*, sJ_*        σ(χ^a) = χ^(ka mod 7), σ J(χ^a, χ^b) = J(χ^(ka), χ^(kb))
    fermat_tr        #{x⁷ + y⁷ = 1} = p − 6 + 3 Σ_(k=1..6) σ_k J + 6 (J' + conj J'),  σ₄ = σ₂², σ₅ = σ₃σ₂², σ₆ = conj
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Sept22

namespace Sept23

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

variable (n : Nat)

theorem h_s2_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+303 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [Sept8.sig2_pow, Sept8.sig2_chi n hpr w hw1 hw7]
  unfold CharSumGen.psp; rw [TwoPower.pwpow]

theorem h_s2_2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+303 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [Sept8.sig2_pow, Sept8.sig2_chi n hpr w hw1 hw7]
  unfold CharSumGen.psp; rw [TwoPower.pwpow]

theorem h_s3_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+303 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [Sept8.sig3_pow, Sept8.sig3_chi n hpr w hw1 hw7]
  unfold CharSumGen.psp; rw [TwoPower.pwpow, show 3 * 4 = 7 * 1 + 4 + 1 from rfl, Sept22.pw_mod n hpr w hw1 hw7 1 4]

theorem h_cj_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+304 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [Sept13.conj7_pow, ← Sept3.chi_iv n hpr w hw1 hw7, ← Sept4.chi_pow6 n hpr w hw1 hw7, TwoPower.pwpow]

theorem h_cj_2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+304 chars; full line in the Zenodo source)
  unfold CharSumGen.psp
  rw [Sept13.conj7_pow, ← Sept3.chi_iv n hpr w hw1 hw7, ← Sept4.chi_pow6 n hpr w hw1 hw7, TwoPower.pwpow, show 6 * 2 = 7 * 1 + 4 + 1 from rfl, Sept22.pw_mod n hpr w hw1 hw7 1 4]

theorem sJ_s2_1_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+545 chars; full line in the Zenodo source)
  unfold CharSumGen.J2; rw [Sept8.sig2_sum]
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => by rw [Sept8.sig2_mul, h_s2_1 n hpr w hw1 hw7, h_s2_1 n hpr w hw1 hw7]) _

theorem sJ_s2_2_2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+545 chars; full line in the Zenodo source)
  unfold CharSumGen.J2; rw [Sept8.sig2_sum]
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => by rw [Sept8.sig2_mul, h_s2_2 n hpr w hw1 hw7, h_s2_2 n hpr w hw1 hw7]) _

theorem sJ_s3_4_4 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+545 chars; full line in the Zenodo source)
  unfold CharSumGen.J2; rw [Sept8.sig3_sum]
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => by rw [Sept8.sig3_mul, h_s3_4 n hpr w hw1 hw7, h_s3_4 n hpr w hw1 hw7]) _

theorem sJ_cj_1_1 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+546 chars; full line in the Zenodo source)
  unfold CharSumGen.J2; rw [Sept3.conj7_sum]
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => by rw [Sept3.conj7_mul, h_cj_1 n hpr w hw1 hw7, h_cj_1 n hpr w hw1 hw7]) _

theorem sJ_cj_1_2 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+546 chars; full line in the Zenodo source)
  unfold CharSumGen.J2; rw [Sept3.conj7_sum]
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => by rw [Sept3.conj7_mul, h_cj_1 n hpr w hw1 hw7, h_cj_2 n hpr w hw1 hw7]) _

/-- **#{x⁷ + y⁷ = 1} = p − 6 + 3 Tr J + 6 (J' + conj J').** -/
theorem fermat_tr (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 *   -- (+5190 chars; full line in the Zenodo source)
  have eY : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) := Sept13  -- (+8 chars; full line in the Zenodo source)
  have e22 : Sept8.sig2 (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) := by rw [← Sept8.J1C n w]; exact sJ_s2_1_1 n hpr w hw1 hw7
  have e44 : Sept8.sig2 (Sept8.sig2 (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4)) := by rw [e22]; exact sJ_s2_2_2 n hpr w hw1 hw7
  have e55 : Sept8.sig3 (Sept8.sig2 (Sept8.sig2 (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5)) := by rw [e44]; exact sJ_s3_4_4 n   -- (+13 chars; full line in the Zenodo source)
  have e66 : Sept3.conj7 (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (Sept2.chi7 n w)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6)) := by rw [← Sept8.J1C n w]; exact sJ_cj_1_1 n hpr w hw1 hw7
  have eY6 : Sept3.conj7 (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w)   -- (+81 chars; full line in the Zenodo source)
  rw [Sept22.fermat7'' n hpr w hw1 hw7, e55, e44, e22, Sept8.sig3_J n hpr w hw1 hw7, e66, eY6, ← Sept8.J1C n w, ← eY]

end Sept23

#print axioms Sept23.h_s2_1
#print axioms Sept23.h_s2_2
#print axioms Sept23.h_s3_4
#print axioms Sept23.h_cj_1
#print axioms Sept23.h_cj_2
#print axioms Sept23.sJ_s2_1_1
#print axioms Sept23.sJ_s2_2_2
#print axioms Sept23.sJ_s3_4_4
#print axioms Sept23.sJ_cj_1_1
#print axioms Sept23.sJ_cj_1_2
#print axioms Sept23.fermat_tr

#print axioms: 11 theorems, no axioms.

78.218 Klein quartic substitution

For \(y\ne 0\) the substitution \(x=y^3/v\) turns the affine Klein quartic into a superelliptic curve.

\[ \#\{x: x^3y+y^3+x=0\}=\#\{v: y^7+v^2+v^3=0\}\quad (y\ne 0). \]

lang/Sept24.lean

/-
  lang/Sept24 — T49, cycle 795 (ℓ = 7; K3 step 1): **the Klein quartic substitution**  (generated by s24gen.py)
  On the affine chart z = 1 the Klein quartic is x³y + y³ + x = 0. For y ≠ 0 the map v ↦ x = y³ ι(v) is an involution of 𝔽_p
  (ι(v) = v^(p−2)), and for v ≠ 0,  v³ (x³y + y³ + x) = y³ (y⁷ + v² + v³). Hence
    klein_sub    y ≠ 0:  Σ_x δ(x³y + y³ + x, 0) = Σ_v δ(y⁷ + v² + v³, 0),
  which turns the Klein count into the superelliptic count #{y⁷ = −v²(1 + v)}.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Sept23

namespace Sept24

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

variable (j : Nat)

/-- The affine Klein quartic x³y + y³ + x. -/
def kl (x y : Zp (j + 1 + 1)) : Zp (j + 1 + 1) := ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul x x) x) y) ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y)) x)

/-- The superelliptic form y⁷ + v² + v³. -/
def ft (y v : Zp (j + 1 + 1)) : Zp (j + 1 + 1) := ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) y) y) y) y) ((ring (j + 1 + 1)).mul v v)) ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul v v) v))

theorem mz (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (a b : Zp (j + 1 + 1)) (ha : a ≠ (ring (j + 1 + 1)).zero) (h : ((ring (j + 1 + 1)).mul a b) = (ring (j + 1 + 1)).zero) : b = (ring (j + 1 + 1)).zero := by
  calc b = ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul (CharSumGen.iv j a) a) b) := by rw [CharSumGen.iv_mul j hpr a ha]; lib_ring
    _ = ((ring (j + 1 + 1)).mul (CharSumGen.iv j a) ((ring (j + 1 + 1)).mul a b)) := by lib_ring
    _ = (ring (j + 1 + 1)).zero := by rw [h]; lib_ring

theorem mul_ne (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (a b : Zp (j + 1 + 1)) (ha : a ≠ (ring (j + 1 + 1)).zero) (hb : b ≠ (ring (j + 1 + 1)).zero) : ((ring (j + 1 + 1)).mul a b) ≠ (ring (j + 1 + 1)).zero :=
  fun h => hb (mz j hpr a b ha h)

/-- v³ · kl(y³u, y) = y³ · (y⁷ + v² + v³) when uv = 1. -/
theorem key (y v u : Zp (j + 1 + 1)) (e : ((ring (j + 1 + 1)).mul u v) = (ring (j + 1 + 1)).one) :
    ((ring (j + 1 + 1)).mul (kl j ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) u) y) ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul v v) v)) = ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) (ft j y v)) := by
  unfold kl ft
  calc _ = ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) y) y) y) y) y) y) y) ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul   -- (+383 chars; full line in the Zenodo source)
    _ = _ := by rw [e]; lib_ring

theorem sig_inv (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (c : Zp (j + 1 + 1)) (hc : c ≠ (ring (j + 1 + 1)).zero) (v : Zp (j + 1 + 1)) : ((ring (j + 1 + 1)).mul c (CharSumGen.iv j ((ring (j + 1 + 1)).mul c (CharSumGen.iv j v)))) = v := by
  rw [CharSumGen.iv_mulmul j, CharSumGen.iv_iv j hpr v]
  calc _ = ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul (CharSumGen.iv j c) c) v) := by lib_ring
    _ = v := by rw [CharSumGen.iv_mul j hpr c hc]; lib_ring

theorem pt {S : Type} (E : CRing S) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (y : Zp (j + 1 + 1)) (hy : y ≠ (ring (j + 1 + 1)).zero) (v : Zp (j + 1 + 1)) :
    delta E (kl j ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) (CharSumGen.iv j v)) y) (ring (j + 1 + 1)).zero = delta E (ft j y v) (ring (j + 1 + 1)).zero := by
  have hy3 : ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) ≠ (ring (j + 1 + 1)).zero := (mul_ne j hpr _ _ (mul_ne j hpr _ _ hy hy) hy)
  by_cases hv : v = (ring (j + 1 + 1)).zero
  · rw [hv, CharSumGen.iv_zero]
    have e0 : (kl j ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) (ring (j + 1 + 1)).zero) y) = ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) := by unfold kl; lib_ring
    have e1 : (ft j y (ring (j + 1 + 1)).zero) = ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) y) y) y) y) := by unfold ft; lib_ring
    have h1 : (kl j ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) (ring (j + 1 + 1)).zero) y) ≠ (ring (j + 1 + 1)).zero := by rw [e0]; exact hy3
    have h2 : (ft j y (ring (j + 1 + 1)).zero) ≠ (ring (j + 1 + 1)).zero := by rw [e1]; exact (mul_ne j hpr _ _ (mul_ne j hpr _ _ (mul_ne j hpr _ _ (mul_ne j hpr _ _ (mul_ne j hpr _ _ (mul_ne j hpr _ _ hy hy) hy) hy) hy) hy) hy)
    unfold delta
    rw [ite_neg' _ _ h1, ite_neg' _ _ h2]
  · have hv3 : ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul v v) v) ≠ (ring (j + 1 + 1)).zero := (mul_ne j hpr _ _ (mul_ne j hpr _ _ hv hv) hv)
    have hk := key j y v (CharSumGen.iv j v) (CharSumGen.iv_mul j hpr v hv)
    by_cases hf : (ft j y v) = (ring (j + 1 + 1)).zero
    · have e2 : ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul v v) v) (kl j ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) (CharSumGen.iv j v)) y)) = (ring (j + 1 + 1)).zero := by
        calc _ = ((ring (j + 1 + 1)).mul (kl j ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) (CharSumGen.iv j v)) y) ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul v v) v)) := (ring (j + 1 + 1)).mul_comm _ _
          _ = ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) (ft j y v)) := hk
          _ = (ring (j + 1 + 1)).zero := by rw [hf]; lib_ring
      have hk0 := mz j hpr _ _ hv3 e2
      unfold delta
      rw [ite_pos' _ _ hk0, ite_pos' _ _ hf]
    · have hk0 : (kl j ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) (CharSumGen.iv j v)) y) ≠ (ring (j + 1 + 1)).zero := fun h => hf (mz j hpr _ _ hy3 (by rw [← hk, h]; lib_ring))
      unfold delta
      rw [ite_neg' _ _ hk0, ite_neg' _ _ hf]

/-- **Klein substitution**: for y ≠ 0, #{x : x³y + y³ + x = 0} = #{v : y⁷ + v² + v³ = 0}. -/
theorem klein_sub {S : Type} (E : CRing S) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (y : Zp (j + 1 + 1)) (hy : y ≠ (ring (j + 1 + 1)).zero) :
    sumK E (fun x => delta E (kl j x y) (ring (j + 1 + 1)).zero) (CharSumGen.LL j) = sumK E (fun v => delta E (ft j y v) (ring (j + 1 + 1)).zero) (CharSumGen.LL j) := by
  have hy3 : ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) ≠ (ring (j + 1 + 1)).zero := (mul_ne j hpr _ _ (mul_ne j hpr _ _ hy hy) hy)
  have r := reindex E (CharSumGen.LL j) (CMInert.complete_allZ (j + 1 + 1) E) (fun v => ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) (CharSumGen.iv j v))) (fun v => ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul y y) y) (CharSumGen.iv j v)))
    (sig_inv j hpr _ hy3) (sig_inv j hpr _ hy3) (fun x => delta E (kl j x y) (ring (j + 1 + 1)).zero)
  exact r.symm.trans (sumK_ext E _ _ (fun v => pt j E hpr y hy v) _)

end Sept24

#print axioms Sept24.mz
#print axioms Sept24.mul_ne
#print axioms Sept24.key
#print axioms Sept24.sig_inv
#print axioms Sept24.pt
#print axioms Sept24.klein_sub

#print axioms: 6 theorems, no axioms.

78.219 Affine Klein count as a superelliptic count

Adding the row \(y=0\) to the substitution of the previous subsection gives the whole affine count.

\[ \#\{x^3y+y^3+x=0\}+1=\#\{y^7+v^2+v^3=0\}. \]

lang/Sept25.lean

/-
  lang/Sept25 — T49, cycle 796 (ℓ = 7; K3 step 2): **the affine Klein count as a superelliptic count**  (generated by s25gen.py)
    row0_A       Σ_x δ(x³·0 + 0 + x, 0) = 1
    row0_B       Σ_v δ(v² + v³, 0) = 2              (δ(v² + v³, 0) = δ(v, 0) + δ(v, −1))
    row          Σ_x δ(kl(x, y), 0) + δ(y, 0) = Σ_v δ(y⁷ + v² + v³, 0)   for every y (Sept24.klein_sub for y ≠ 0)
    klein_aff    #{(x, y) : x³y + y³ + x = 0} + 1 = #{(y, v) : y⁷ + v² + v³ = 0}
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Sept24

namespace Sept25

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

variable (j : Nat)

theorem row0_A {S : Type} (E : CRing S) : (sumK E (fun x => (delta E (Sept24.kl j x (ring (j + 1 + 1)).zero) (ring (j + 1 + 1)).zero)) (CharSumGen.LL j)) = E.one := by
  rw [← CharSumGen.delta_sum1 E j]
  exact sumK_ext E _ _ (fun x => by rw [show (Sept24.kl j x (ring (j + 1 + 1)).zero) = x by unfold Sept24.kl; lib_ring]) _

theorem pt0 {S : Type} (E : CRing S) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (v : Zp (j + 1 + 1)) : (delta E (Sept24.ft j (ring (j + 1 + 1)).zero v) (ring (j + 1 + 1)).zero) = E.add (delta E v (ring (j + 1 + 1)).zero) (delta E v ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) := by
  have hf : (Sept24.ft j (ring (j + 1 + 1)).zero v) = ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul v v) ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one v)) := by unfold Sept24.ft; lib_ring
  have hm1 := JacRefl.neg1_ne j
  unfold delta
  rw [hf]
  by_cases h0 : v = (ring (j + 1 + 1)).zero
  · have h1 : v ≠ ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := fun e => hm1 (by rw [← e, h0])
    have hz : ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul v v) ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one v)) = (ring (j + 1 + 1)).zero := by rw [h0]; lib_ring
    rw [ite_pos' _ _ hz, ite_pos' _ _ h0, ite_neg' _ _ h1]
    lib_ring
  · by_cases h1 : v = ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)
    · have hz : ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul v v) ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one v)) = (ring (j + 1 + 1)).zero := by rw [h1]; lib_ring
      rw [ite_pos' _ _ hz, ite_neg' _ _ h0, ite_pos' _ _ h1]
      lib_ring
    · have hv1 : ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one v) ≠ (ring (j + 1 + 1)).zero := fun e => h1 (by
        calc v = ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one v) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) := by lib_ring
          _ = ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := by rw [e]; lib_ring)
      have hne : ((ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).mul v v) ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one v)) ≠ (ring (j + 1 + 1)).zero := Sept24.mul_ne j hpr _ _ (Sept24.mul_ne j hpr _ _ h0 h0) hv1
      rw [ite_neg' _ _ hne, ite_neg' _ _ h0, ite_neg' _ _ h1]
      lib_ring

theorem row0_B {S : Type} (E : CRing S) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) : (sumK E (fun v => (delta E (Sept24.ft j (ring (j + 1 + 1)).zero v) (ring (j + 1 + 1)).zero)) (CharSumGen.LL j)) = E.add E.one E.one := by
  have c1 : sumK E (fun v => (delta E v ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) (CharSumGen.LL j) = E.one := CMInert.complete_allZ (j + 1 + 1) E ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)
  rw [sumK_ext E _ _ (pt0 j E hpr) _, sumK_add, CharSumGen.delta_sum1 E j, c1]

theorem row {S : Type} (E : CRing S) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (y : Zp (j + 1 + 1)) : E.add (sumK E (fun x => (delta E (Sept24.kl j x y) (ring (j + 1 + 1)).zero)) (CharSumGen.LL j)) (delta E y (ring (j + 1 + 1)).zero) = (sumK E (fun v => (delta E (Sept24.ft j y v) (ring (j + 1 + 1)).zero)) (CharSumGen.LL j)) := by
  by_cases hy : y = (ring (j + 1 + 1)).zero
  · rw [hy, row0_A j E, row0_B j E hpr]
    unfold delta
    rw [ite_pos' _ _ rfl]
  · rw [Sept24.klein_sub j E hpr y hy]
    unfold delta
    rw [ite_neg' _ _ hy]
    lib_ring

/-- **Affine Klein count**: #{x³y + y³ + x = 0} + 1 = #{y⁷ + v² + v³ = 0}. -/
theorem klein_aff {S : Type} (E : CRing S) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) :
    E.add (sumK E (fun y => (sumK E (fun x => (delta E (Sept24.kl j x y) (ring (j + 1 + 1)).zero)) (CharSumGen.LL j))) (CharSumGen.LL j)) E.one = sumK E (fun y => (sumK E (fun v => (delta E (Sept24.ft j y v) (ring (j + 1 + 1)).zero)) (CharSumGen.LL j))) (CharSumGen.LL j) := by
  rw [← CharSumGen.delta_sum1 E j, ← sumK_add]
  exact sumK_ext E _ _ (row j E hpr) _

end Sept25

#print axioms Sept25.row0_A
#print axioms Sept25.pt0
#print axioms Sept25.row0_B
#print axioms Sept25.row
#print axioms Sept25.klein_aff

#print axioms: 5 theorems, no axioms.

78.220 Superelliptic count by Jacobi sums

Counting seventh roots with the septic character and substituting \(v=-t\) gives one Jacobi sum for each power of \(\chi \).

\[ \#\{(y,v): y^7+v^2+v^3=0\}=\textstyle \sum _{k=0}^{6}J(\chi ^{2k},\chi ^k). \]

lang/Sept26.lean

/-
  lang/Sept26 — T49, cycle 797 (ℓ = 7; K3 step 3): **the superelliptic count y⁷ + v² + v³ = 0 by Jacobi sums**  (generated by s26gen.py)
    pw7          y⁷ (Frobenius.pw) = y·y·y·y·y·y·y
    dl           δ(y⁷ + v² + v³, 0) = δ(y⁷, −(v² + v³))
    term         Σ_v χ(−(v² + v³))^k = J(χ^(2k), χ^k)          (v ↦ −t, χ(−1) = 1)
    superell     #{(y, v) : y⁷ + v² + v³ = 0} = Σ_(k<7) J(χ^(2k), χ^k)   (Sept19.seven_count)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Sept25

namespace Sept26

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

variable (n : Nat)

theorem pw7 (y : Zp (2 * (7 * n + 6) + 1 + 1)) : pw (ring (2 * (7 * n + 6) + 1 + 1)) y 7 = ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul y y) y) y) y) y) y) := by
  show ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul (ring (2 * (7 * n + 6) + 1 + 1)).one y) y) y) y) y) y) y) = _
  lib_ring

theorem dl (y v : Zp (2 * (7 * n + 6) + 1 + 1)) : (delta (Cyclo7.cyc7 LibInt.intR) (Sept24.ft (2 * (7 * n + 6)) y v) (ring (2 * (7 * n + 6) + 1 + 1)).zero) = (delta (Cyclo7.cyc7 LibInt.intR) (pw (ring (2 * (7 * n + 6) + 1 + 1)) y 7) ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).mul v v) ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2   -- (+44 chars; full line in the Zenodo source)
  unfold delta
  by_cases h : (Sept24.ft (2 * (7 * n + 6)) y v) = (ring (2 * (7 * n + 6) + 1 + 1)).zero
  · have h2 : pw (ring (2 * (7 * n + 6) + 1 + 1)) y 7 = ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).mul v v) ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul v v) v))) := by
      rw [pw7 n y]
      calc ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul y y) y) y) y) y) y) = ((ring (2 * (7 * n + 6) + 1 + 1)).add (Sept24.ft (2 * (7 * n + 6)) y v) ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 +  -- (+171 chars; full line in the Zenodo source)
        _ = _ := by rw [h]; lib_ring
    rw [ite_pos' _ _ h, ite_pos' _ _ h2]
  · have h2 : pw (ring (2 * (7 * n + 6) + 1 + 1)) y 7 ≠ ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).mul v v) ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul v v) v))) := fun e => h (by
      unfold Sept24.ft
      rw [← pw7 n y, e]
      lib_ring)
    rw [ite_neg' _ _ h, ite_neg' _ _ h2]

theorem pt (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n   -- (+124 chars; full line in the Zenodo source)
    (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).neg t) ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)) ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).neg t) ((ring (2 * (7 * n + 6) + 1 + 1)).ne  -- (+316 chars; full line in the Zenodo source)
  have e1 : ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).neg t) ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)) ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).neg t) ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)) ((ring (2 * (7 * n + 6) + 1 + 1)  -- (+342 chars; full line in the Zenodo source)
  rw [e1, Sept2.chi7_mul n hpr w hw1 hw7, Sept2.chi7_mul n hpr w hw1 hw7, Sept2.chi7_mul n hpr w hw1 hw7, Sept2.chi7_neg1 n hpr w hw1 hw7]
  have e2 : (Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (Cyclo7.cyc7 LibInt.intR).one ((Cyclo7.cyc7 LibInt.intR).mul (Sept2.chi7 n w t) (Sept2.chi7 n w t))) (Sept2.chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))) = (Cyclo7.cyc7 LibInt.intR).mul (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w t) 2) (Sept2.c  -- (+131 chars; full line in the Zenodo source)
    show _ = (Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul ((Cyclo7.cyc7 LibInt.intR).mul (Cyclo7.cyc7 LibInt.intR).one (Sept2.chi7 n w t)) (Sept2.chi7 n w t)) (Sept2.chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).add (ring (2 * (7 * n + 6) + 1 + 1)).one ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)))
    lib_ring
  rw [e2, Frobenius.pw_mul, TwoPower.pwpow]

theorem term (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 *   -- (+91 chars; full line in the Zenodo source)
    sumK (Cyclo7.cyc7 LibInt.intR) (fun v => (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).mul v v) ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul v v) v)))) k)) (CharSumGen.LL (2 * (7 * n + 6))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Cha  -- (+168 chars; full line in the Zenodo source)
  have r := reindex (Cyclo7.cyc7 LibInt.intR) (CharSumGen.LL (2 * (7 * n + 6))) (CMInert.complete_allZ (2 * (7 * n + 6) + 1 + 1) (Cyclo7.cyc7 LibInt.intR)) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)) (fun t => ((ring (2 * (7 * n + 6) + 1 + 1)).neg t))
    (fun t => by show ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)) = t; lib_ring) (fun t => by show ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).neg t)) = t; lib_ring) (fun v => (pw (Cyclo7.cyc7 LibInt.intR) (Sept2.chi7 n w ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).mul   -- (+95 chars; full line in the Zenodo source)
  rw [← r]
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (pt n hpr w hw1 hw7 k) _

/-- **#{y⁷ + v² + v³ = 0} = Σ_(k<7) J(χ^(2k), χ^k).** -/
theorem superell (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (  -- (+85 chars; full line in the Zenodo source)
    sumK (Cyclo7.cyc7 LibInt.intR) (fun y => sumK (Cyclo7.cyc7 LibInt.intR) (fun v => (delta (Cyclo7.cyc7 LibInt.intR) (Sept24.ft (2 * (7 * n + 6)) y v) (ring (2 * (7 * n + 6) + 1 + 1)).zero)) (CharSumGen.LL (2 * (7 * n + 6)))) (CharSumGen.LL (2 * (7 * n + 6))) = ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc7 LibInt.intR).add ((Cyclo7.cyc  -- (+1643 chars; full line in the Zenodo source)
  rw [sumK_swap]
  refine (sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun v => (sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun y => dl n y v) (CharSumGen.LL (2 * (7 * n + 6)))).trans (Sept19.seven_count n hpr w hw1 hw7 ((ring (2 * (7 * n + 6) + 1 + 1)).neg ((ring (2 * (7 * n + 6) + 1 + 1)).add ((ring (2 * (7 * n + 6) + 1 + 1)).mul v v) ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul v v) v)))  -- (+46 chars; full line in the Zenodo source)
  rw [sumK_add, sumK_add, sumK_add, sumK_add, sumK_add, sumK_add]
  rw [term n hpr w hw1 hw7 0, term n hpr w hw1 hw7 1, term n hpr w hw1 hw7 2, term n hpr w hw1 hw7 3, term n hpr w hw1 hw7 4, term n hpr w hw1 hw7 5, term n hpr w hw1 hw7 6]

end Sept26

#print axioms Sept26.pw7
#print axioms Sept26.dl
#print axioms Sept26.pt
#print axioms Sept26.term
#print axioms Sept26.superell

#print axioms: 5 theorems, no axioms.

78.221 Klein quartic point count

Reducing the exponents and grouping the Jacobi sums into Galois classes gives the count of the Klein quartic.

\[ \#\{x^3y+y^3+x=0\}+1=p+3\big (J(\chi ,\chi ^2)+\overline {J(\chi ,\chi ^2)}\big ). \]

lang/Sept27.lean

/-
  lang/Sept27 — T49, cycle 798 (ℓ = 7; K3 done): **the Klein quartic point count**  (generated by s27gen.py)
    red          J(χ^(7m+r+1), χ^b) = J(χ^(r+1), χ^b)
    klein7       #{x³y + y³ + x = 0} + 1 = p + 3 J(χ, χ²) + 3 J(χ³, χ⁶)       (Sept25, Sept26, Sept22 classes)
    klein7c      #{x³y + y³ + x = 0} + 1 = p + 3 (J' + conj J'),  J' = J(χ, χ²)   (Sept23)
  With the two points (1:0:0), (0:1:0) at z = 0 the projective Klein quartic has p + 1 + 3(J' + conj J') points,
  i.e. a_p = −(J' + conj J') for the curve 49a (numerically checked for p ≡ 1 mod 7, p ≤ 281).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Sept26

namespace Sept27

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

variable (n : Nat)

theorem red (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n  -- (+562 chars; full line in the Zenodo source)
  unfold CharSumGen.J2
  exact sumK_ext (Cyclo7.cyc7 LibInt.intR) _ _ (fun t => congrArg (fun z => (Cyclo7.cyc7 LibInt.intR).mul z _) (Sept22.pw_mod n hpr w hw1 hw7 m r t)) _

theorem klein7 (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7   -- (+1980 chars; full line in the Zenodo source)
  rw [Sept25.klein_aff (2 * (7 * n + 6)) (Cyclo7.cyc7 LibInt.intR) hpr, Sept26.superell n hpr w hw1 hw7]
  have e0 : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (2 * 0)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 0)) = (Int.ofNat (2 * (7 * n + 6) + 1 + 1 + 1), 0, 0, 0, 0, 0) := Sept21.J00 n w
  have e1 : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (2 * 1)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (CharSumGen.psp (Cycl  -- (+78 chars; full line in the Zenodo source)
  have e2 : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (2 * 2)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (CharSumGen.psp (Cycl  -- (+90 chars; full line in the Zenodo source)
  have e3 : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (2 * 3)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) (CharSumGen.psp (Cycl  -- (+78 chars; full line in the Zenodo source)
  have e4 : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (2 * 4)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 4)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (CharSumGen.psp (Cycl  -- (+126 chars; full line in the Zenodo source)
  have e5 : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (2 * 5)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 5)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) (CharSumGen.psp (Cycl  -- (+126 chars; full line in the Zenodo source)
  have e6 : (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) (2 * 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 6)) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) (CharSumGen.psp (Cycl  -- (+126 chars; full line in the Zenodo source)
  rw [e0, e1, e2, e3, e4, e5, e6]
  lib_ring

/-- **Klein quartic**: #{x³y + y³ + x = 0} + 1 = p + 3 (J' + conj J'). -/
theorem klein7c (hpr : ∀ d, Dv d (2 * (7 * n + 6) + 1 + 1 + 1) → d = 1 ∨ d = 2 * (7 * n + 6) + 1 + 1 + 1) (w : Zp (2 * (7 * n + 6) + 1 + 1)) (hw1 : w ≠ (ring (2 * (7 * n + 6) + 1 + 1)).one) (hw7 : (ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7 * n + 6) + 1 + 1)).mul ((ring (2 * (7  -- (+2023 chars; full line in the Zenodo source)
  have eY6 : (Sept3.conj7 (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 1) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 2))) = (CharSumGen.J2 (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (CharSumGen.psp (Cyclo7.cyc7 LibInt.intR) (2 * (7 * n + 6)) (Sept2.chi7 n w) 3) (CharSumGen.  -- (+74 chars; full line in the Zenodo source)
    rw [Sept23.sJ_cj_1_2 n hpr w hw1 hw7]; exact Sept22.P6_5 n hpr w hw1 hw7
  rw [eY6]
  exact klein7 n hpr w hw1 hw7

end Sept27

#print axioms Sept27.red
#print axioms Sept27.klein7
#print axioms Sept27.klein7c

#print axioms: 3 theorems, no axioms.

78.222 Hecke eigen-sequences are determined by prime coefficients

Two normalized multiplicative sequences with the same Euler recursion and the same prime values are equal.

\[ a(1)=b(1)=1,\ a(p)=b(p)\ \forall p\ \Rightarrow \ a(n)=b(n)\ \forall n\ge 1. \]

lang/HeckeUniq.lean

/-
  lang/HeckeUniq — T51 (tool for G4 (b2)), cycle 799: **normalized Hecke eigen-sequences are determined by their prime coefficients**.
  Two sequences a, b : ℕ → ℤ with a(1) = b(1) = 1, both multiplicative on p^(k+1)·m (p prime, p ∤ m), both satisfying the
  Euler recursion c(p^(k+2)) = c(p) c(p^(k+1)) − w(p) c(p^k) with the same weight w (w(p) = p at good primes, 0 at bad ones),
  and a(p) = b(p) at every prime p, agree at every n ≥ 1:
    pp_eq      a(p^k) = b(p^k) for all k
    uniq       a(n) = b(n) for all n ≥ 1                     (strong induction by fuel, G4b1.factor)
  So a q-series identity Σ a(n)qⁿ = Σ b(n)qⁿ reduces to the Hecke structure of both sides and the equality of a(p).
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.G4b1

namespace HeckeUniq

open FTAChain Frobenius

theorem pp_eq (a b w : Nat → Int) (P : Nat) (ha1 : a 1 = 1) (hb1 : b 1 = 1) (h1 : P ^ 1 = P) (hab : a P = b P)
    (hrA : ∀ k, a (P ^ (k + 2)) = a P * a (P ^ (k + 1)) - w P * a (P ^ k))
    (hrB : ∀ k, b (P ^ (k + 2)) = b P * b (P ^ (k + 1)) - w P * b (P ^ k)) :
    ∀ k, a (P ^ k) = b (P ^ k) ∧ a (P ^ (k + 1)) = b (P ^ (k + 1))
  | 0 => ⟨by show a 1 = b 1; rw [ha1, hb1], by rw [h1]; exact hab⟩
  | k + 1 => by
    match pp_eq a b w P ha1 hb1 h1 hab hrA hrB k with
    | ⟨e0, e1⟩ => exact ⟨e1, by rw [hrA k, hrB k, e0, e1, hab]⟩

/-- **Uniqueness**: equal prime coefficients ⇒ equal sequences. -/
theorem uniq (a b w : Nat → Int) (ha1 : a 1 = 1) (hb1 : b 1 = 1)
    (hmA : ∀ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      a ((l + 2) ^ (k + 1) * (m + 1)) = a ((l + 2) ^ (k + 1)) * a (m + 1))
    (hmB : ∀ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      b ((l + 2) ^ (k + 1) * (m + 1)) = b ((l + 2) ^ (k + 1)) * b (m + 1))
    (hrA : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) →
      a ((l + 2) ^ (k + 2)) = a (l + 2) * a ((l + 2) ^ (k + 1)) - w (l + 2) * a ((l + 2) ^ k))
    (hrB : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) →
      b ((l + 2) ^ (k + 2)) = b (l + 2) * b ((l + 2) ^ (k + 1)) - w (l + 2) * b ((l + 2) ^ k))
    (hab : ∀ l, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → a (l + 2) = b (l + 2)) :
    ∀ i, a (i + 1) = b (i + 1) := by
  have hpp : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → a ((l + 2) ^ (k + 1)) = b ((l + 2) ^ (k + 1)) := fun l k hp => by
    have h1 : (l + 2) ^ 1 = l + 2 := by show 1 * (l + 2) = l + 2; exact (nmul_comm 1 (l + 2)).trans (nmul_one _)
    exact (pp_eq a b w (l + 2) ha1 hb1 h1 (hab l hp) (hrA l · hp) (hrB l · hp) k).2
  have main : ∀ fuel i, Le i fuel → a (i + 1) = b (i + 1) := by
    intro fuel
    induction fuel with
    | zero =>
      intro i hle
      match hle with
      | ⟨c, hc⟩ => rw [G4b1.az i c hc, ha1, hb1]
    | succ f ih =>
      intro i hle
      cases i with
      | zero => rw [ha1, hb1]
      | succ i' =>
        match G4b1.factor i' with
        | ⟨l, k, m, hp, hn, hnd, ⟨c', hc'⟩⟩ =>
          match hle with
          | ⟨c, hc⟩ =>
            have e2 := ih m ⟨c' + c, Nat.succ.inj (show m + (c' + c) + 1 = f + 1 by rw [← hc, ← hc']; lib_nat)⟩
            show a (i' + 2) = b (i' + 2)
            rw [hn, hmA l k m hp hnd, hmB l k m hp hnd, hpp l k hp, e2]
  exact fun i => main i i ⟨0, rfl⟩

end HeckeUniq

#print axioms HeckeUniq.pp_eq
#print axioms HeckeUniq.uniq

#print axioms: 2 theorems, no axioms.

78.223 \(T_p\)-eigen sequences are multiplicative

The coefficient form of \(T_pa=a(p)a\) gives the Euler recursion and multiplicativity, hence uniqueness from the prime coefficients.

\[ a(pN)=a(p)a(N)\ (p\nmid N),\ a(p^2n)=a(p)a(pn)-w\,a(n)\ \Rightarrow \ a(p^{k+1}N)=a(p^{k+1})a(N). \]

lang/HeckeEigSeq.lean

/-
  lang/HeckeEigSeq — T51 (tool for G4 (b2)), cycle 800: **T_p-eigen sequences are multiplicative and satisfy the Euler recursion**.
  For a coefficient sequence a : ℕ → ℤ with a(1) = 1, the eigen-equation T_p a = a(p)·a of the weight-2 Hecke operator
  T_p = U_p + w V_p reads, coefficientwise,
    (h1)  a(p·N) = a(p) a(N)                         for p ∤ N,
    (h2)  a(p·(p·n)) = a(p) a(p·n) − w a(n)           for every n.
  From these (no primality used in the algebra):
    rec_of      a(p^(k+2)) = a(p) a(p^(k+1)) − w a(p^k)
    mul_of      a(p^(k+1)·N) = a(p^(k+1)) a(N)   whenever a(p·N) = a(p) a(N)
    eigen_uniq  two sequences that are T_p-eigen at every prime p (same weight w) with equal a(p) are equal (HeckeUniq.uniq)
  So the q-series identity of G4 (b2) reduces to: both sides are T_p-eigen sequences, and their a(p) agree.
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.HeckeUniq

namespace HeckeEigSeq

open FTAChain Frobenius

theorem pw1 (P : Nat) : P ^ 1 = P := by show 1 * P = P; exact (nmul_comm 1 P).trans (nmul_one _)

theorem pw2 (P k : Nat) : P ^ (k + 2) = P * (P * P ^ k) := by
  show P ^ k * P * P = P * (P * P ^ k)
  generalize P ^ k = Q
  lib_nat

theorem pw1' (P k : Nat) : P ^ (k + 1) = P * P ^ k := by
  show P ^ k * P = P * P ^ k
  generalize P ^ k = Q
  lib_nat

theorem rec_of (a : Nat → Int) (w : Int) (P : Nat) (h2 : ∀ n, a (P * (P * n)) = a P * a (P * n) - w * a n) (k : Nat) :
    a (P ^ (k + 2)) = a P * a (P ^ (k + 1)) - w * a (P ^ k) := by
  rw [pw2, pw1' P k]
  exact h2 (P ^ k)

theorem mul_of (a : Nat → Int) (w : Int) (P : Nat) (ha1 : a 1 = 1) (h2 : ∀ n, a (P * (P * n)) = a P * a (P * n) - w * a n)
    (N : Nat) (hN : a (P * N) = a P * a N) :
    ∀ k, a (P ^ (k + 1) * N) = a (P ^ (k + 1)) * a N ∧ a (P ^ (k + 2) * N) = a (P ^ (k + 2)) * a N
  | 0 => by
    have e1 : P ^ (0 + 1) * N = P * N := by rw [pw1' P 0]; show P * 1 * N = P * N; rw [nmul_one]
    have e2 : P ^ (0 + 2) * N = P * (P * N) := by rw [pw2 P 0]; show P * (P * 1) * N = P * (P * N); rw [nmul_one]; lib_nat
    have hp2 : a (P ^ (0 + 2)) = a P * a P - w := by
      rw [pw2 P 0, show P * (P * P ^ 0) = P * (P * 1) from rfl, h2 1, nmul_one, ha1]; lib_int
    refine ⟨?_, ?_⟩
    · rw [e1, show P ^ (0 + 1) = P from pw1 P]; exact hN
    · rw [e2, h2 N, hN, hp2]; lib_int
  | k + 1 => by
    match mul_of a w P ha1 h2 N hN k with
    | ⟨e0, e1⟩ =>
      refine ⟨e1, ?_⟩
      have es : P ^ (k + 1 + 2) * N = P * (P * (P ^ (k + 1) * N)) := by
        show P ^ (k + 1) * P * P * N = _
        generalize P ^ (k + 1) = Q
        lib_nat
      have es1 : P * (P ^ (k + 1) * N) = P ^ (k + 2) * N := by
        show _ = P ^ (k + 1) * P * N
        generalize P ^ (k + 1) = Q
        lib_nat
      have hr : a (P ^ (k + 1 + 2)) = a P * a (P ^ (k + 2)) - w * a (P ^ (k + 1)) := rec_of a w P h2 (k + 1)
      rw [es, h2, es1, e1, e0, hr]
      lib_int

/-- **Eigen-uniqueness**: T_p-eigen sequences (all primes p, same weights) with equal prime coefficients are equal. -/
theorem eigen_uniq (a b w : Nat → Int) (ha1 : a 1 = 1) (hb1 : b 1 = 1)
    (h1A : ∀ l m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      a ((l + 2) * (m + 1)) = a (l + 2) * a (m + 1))
    (h1B : ∀ l m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      b ((l + 2) * (m + 1)) = b (l + 2) * b (m + 1))
    (h2A : ∀ l n, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) →
      a ((l + 2) * ((l + 2) * n)) = a (l + 2) * a ((l + 2) * n) - w (l + 2) * a n)
    (h2B : ∀ l n, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) →
      b ((l + 2) * ((l + 2) * n)) = b (l + 2) * b ((l + 2) * n) - w (l + 2) * b n)
    (hab : ∀ l, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → a (l + 2) = b (l + 2)) :
    ∀ i, a (i + 1) = b (i + 1) :=
  HeckeUniq.uniq a b w ha1 hb1
    (fun l k m hp hnd => (mul_of a (w (l + 2)) (l + 2) ha1 (h2A l · hp) (m + 1) (h1A l m hp hnd) k).1)
    (fun l k m hp hnd => (mul_of b (w (l + 2)) (l + 2) hb1 (h2B l · hp) (m + 1) (h1B l m hp hnd) k).1)
    (fun l k hp => rec_of a (w (l + 2)) (l + 2) (h2A l · hp) k)
    (fun l k hp => rec_of b (w (l + 2)) (l + 2) (h2B l · hp) k)
    hab

end HeckeEigSeq

#print axioms HeckeEigSeq.pw1
#print axioms HeckeEigSeq.pw2
#print axioms HeckeEigSeq.pw1'
#print axioms HeckeEigSeq.rec_of
#print axioms HeckeEigSeq.mul_of
#print axioms HeckeEigSeq.eigen_uniq

#print axioms: 6 theorems, no axioms.

78.224 The L-series of 11a modulo 5

The Euler factors of 11a at \(p\neq 11\) have \(a_p\equiv 1+p\pmod 5\), so the L-series coefficients agree with \(\sigma (n)\) modulo 5.

\[ 11\nmid n\ \Rightarrow \ a(n)\equiv \sigma (n)\pmod 5. \]

lang/G4b1.lean

/-
  lang/G4b1 — G4 (b1), the arithmetic half: **an L-series whose Euler factors at p ≠ 11 are 1 − a_p X + p X² with
  a_p ≡ 1 + p (mod 5) is ≡ σ(n) (mod 5) at every n prime to 11**.
  The Euler factor's characteristic polynomial x² − a_p x + p ≡ (x − 1)(x − p) (mod 5), whose power sums are σ(p^k) = 1 + p + … + p^k:
    geo, geo_rec       G_k = 1 + p + … + p^k satisfies G_(k+2) = (1 + p) G_(k+1) − p G_k
    pp_cong            c_0 = 1, c_1 = a_p ≡ 1 + p, c_(k+2) = a_p c_(k+1) − p c_k  ⇒  c_k ≡ G_k (mod 5) for all k
    pull               n ≥ 2 with p | n (p prime) ⇒ n = p^(k+1)·m, p ∤ m, m < n
    factor             n ≥ 2 ⇒ n = p^(k+1)·m with p prime, p ∤ m, m < n
    g4b1               a, s multiplicative (on p^(k+1)·m, p ∤ m), a(1) = s(1) = 1, the Euler recursion and a_p ≡ 1 + p at p ≠ 11,
                       s(p^k) = 1 + … + p^k  ⇒  a(n) ≡ s(n) = σ(n) (mod 5) for every n ≥ 1 with 11 ∤ n
  (The curve input a_p ≡ 1 + p (mod 5) for 11a is G4Count.g4; see G4b1c.) Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.G4Count

namespace G4b1

open FTAChain Frobenius

def geo (p : Int) : Nat → Int
  | 0 => 1
  | k + 1 => geo p k + pw LibInt.intR p (k + 1)

theorem geo_rec (p : Int) (k : Nat) : geo p (k + 2) = (1 + p) * geo p (k + 1) - p * geo p k := by
  show geo p k + pw LibInt.intR p (k + 1) + LibInt.intR.mul (pw LibInt.intR p (k + 1)) p = (1 + p) * (geo p k + pw LibInt.intR p (k + 1)) - p * geo p k
  generalize geo p k = g
  generalize pw LibInt.intR p (k + 1) = q
  show g + q + q * p = (1 + p) * (g + q) - p * g
  lib_int

/-- **c_k ≡ G_k (mod 5)** for the Euler-factor recursion with a_p ≡ 1 + p. -/
theorem pp_cong (A p : Int) (hA : ∃ t, A - (1 + p) = 5 * t) (c : Nat → Int) (h0 : c 0 = 1) (h1 : c 1 = A)
    (hrec : ∀ k, c (k + 2) = A * c (k + 1) - p * c k) : ∀ k, (∃ t, c k - geo p k = 5 * t) ∧ (∃ t, c (k + 1) - geo p (k + 1) = 5 * t)
  | 0 => by
    match hA with
    | ⟨t, ht⟩ =>
      refine ⟨⟨0, by rw [h0]; show (1 : Int) - 1 = 5 * 0; rfl⟩, ⟨t, ?_⟩⟩
      rw [h1, ← ht]; show A - (1 + LibInt.intR.mul LibInt.intR.one p) = A - (1 + p); rw [LibInt.intR.one_mul]
  | k + 1 => by
    match pp_cong A p hA c h0 h1 hrec k, hA with
    | ⟨_, ⟨t1, e1⟩⟩, ⟨t, ht⟩ =>
      match (pp_cong A p hA c h0 h1 hrec k).1 with
      | ⟨t0, e0⟩ =>
        refine ⟨⟨t1, e1⟩, ⟨A * t1 + geo p (k + 1) * t - p * t0, ?_⟩⟩
        rw [hrec, geo_rec]
        have ec1 : c (k + 1) = geo p (k + 1) + 5 * t1 := by rw [← e1]; lib_int
        have ec0 : c k = geo p k + 5 * t0 := by rw [← e0]; lib_int
        have eA : A = 1 + p + 5 * t := by rw [← ht]; lib_int
        rw [ec1, ec0, eA]; lib_int

theorem az : ∀ a b : Nat, a + b = 0 → a = 0
  | _, 0, h => h
  | _, _ + 1, h => Nat.noConfusion h

/-- extraction of the full power of a prime p = l + 2 dividing x + 1. -/
theorem pull (l : Nat) : ∀ fuel x, Le x fuel → Dv (l + 2) (x + 1) →
    ∃ k m, x + 1 = (l + 2) ^ (k + 1) * (m + 1) ∧ (Dv (l + 2) (m + 1) → False) ∧ Lt m x
  | 0, x, hle, ⟨q, hq⟩ => by
    cases q with
    | zero => exact absurd (hq.trans (nzero_mul _)) (fun e => Nat.noConfusion e)
    | succ q' =>
      have hlt : Lt q' x := ⟨q' * l + q' + l, Nat.succ.inj (show q' + (q' * l + q' + l + 1) + 1 = x + 1 by rw [hq]; lib_nat)⟩
      cases dv_dec (l + 1) (q' + 1) with
      | inr nd => exact ⟨0, q', by show x + 1 = 1 * (l + 2) * (q' + 1); rw [hq]; lib_nat, nd, hlt⟩
      | inl _ =>
        match hle, hlt with
        | ⟨c, hc⟩, ⟨c', hc'⟩ =>
          rw [az x c hc] at hc'
          exact Nat.noConfusion hc'
  | f + 1, x, hle, ⟨q, hq⟩ => by
    cases q with
    | zero => exact absurd (hq.trans (nzero_mul _)) (fun e => Nat.noConfusion e)
    | succ q' =>
      have hlt : Lt q' x := ⟨q' * l + q' + l, Nat.succ.inj (show q' + (q' * l + q' + l + 1) + 1 = x + 1 by rw [hq]; lib_nat)⟩
      cases dv_dec (l + 1) (q' + 1) with
      | inr nd => exact ⟨0, q', by show x + 1 = 1 * (l + 2) * (q' + 1); rw [hq]; lib_nat, nd, hlt⟩
      | inl d =>
        match hle, hlt with
        | ⟨c, hc⟩, ⟨c', hc'⟩ =>
          match pull l f q' ⟨c' + c, Nat.succ.inj (show q' + (c' + c) + 1 = f + 1 by rw [← hc, ← hc']; lib_nat)⟩ d with
          | ⟨k, m, hk, hnd, ⟨c'', hc''⟩⟩ =>
            refine ⟨k + 1, m, ?_, hnd, ⟨c'' + c' + 1, by rw [← hc', ← hc'']; lib_nat⟩⟩
            show x + 1 = (l + 2) ^ (k + 1) * (l + 2) * (m + 1)
            rw [hq]
            show (q' + 1) * (l + 2) = (l + 2) ^ (k + 1) * (l + 2) * (m + 1)
            rw [hk]
            generalize (l + 2) ^ (k + 1) = P
            lib_nat

/-- every n = i + 2 is p^(k+1)·(m+1) with p = l + 2 prime, p ∤ m + 1, m < i + 1. -/
theorem factor (i : Nat) : ∃ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) ∧ i + 2 = (l + 2) ^ (k + 1) * (m + 1) ∧
    (Dv (l + 2) (m + 1) → False) ∧ Lt m (i + 1) := by
  match lp_exists i with
  | ⟨l, hl, hmin⟩ =>
    match pull l (i + 1) (i + 1) ⟨0, rfl⟩ hl with
    | ⟨k, m, hk, hnd, hlt⟩ => exact ⟨l, k, m, lp_prime (i + 1 + 1) l hl hmin, hk, hnd, hlt⟩

/-- **G4 (b1)**: a(n) ≡ σ(n) (mod 5) at every n prime to 11. -/
theorem g4b1 (a s : Nat → Int) (ha1 : a 1 = 1) (hs1 : s 1 = 1)
    (hmA : ∀ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      a ((l + 2) ^ (k + 1) * (m + 1)) = a ((l + 2) ^ (k + 1)) * a (m + 1))
    (hmS : ∀ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      s ((l + 2) ^ (k + 1) * (m + 1)) = s ((l + 2) ^ (k + 1)) * s (m + 1))
    (hrec : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (l + 2 = 11 → False) →
      a ((l + 2) ^ (k + 2)) = a (l + 2) * a ((l + 2) ^ (k + 1)) - Int.ofNat (l + 2) * a ((l + 2) ^ k))
    (hap : ∀ l, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (l + 2 = 11 → False) → ∃ t, a (l + 2) - (1 + Int.ofNat (l + 2)) = 5 * t)
    (hsp : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → s ((l + 2) ^ k) = geo (Int.ofNat (l + 2)) k) :
    ∀ i, (Dv 11 (i + 1) → False) → ∃ t, a (i + 1) - s (i + 1) = 5 * t := by
  have hpp : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (l + 2 = 11 → False) →
      ∃ t, a ((l + 2) ^ (k + 1)) - s ((l + 2) ^ (k + 1)) = 5 * t := fun l k hp h11 => by
    have h1 : (l + 2) ^ 1 = l + 2 := by show 1 * (l + 2) = l + 2; exact (nmul_comm 1 (l + 2)).trans (nmul_one _)
    have hc := pp_cong (a (l + 2)) (Int.ofNat (l + 2)) (hap l hp h11) (fun k => a ((l + 2) ^ k)) ha1
      (by show a ((l + 2) ^ 1) = a (l + 2); rw [h1]) (fun k => hrec l k hp h11) k
    rw [hsp l (k + 1) hp]
    exact hc.2
  have main : ∀ fuel i, Le i fuel → (Dv 11 (i + 1) → False) → ∃ t, a (i + 1) - s (i + 1) = 5 * t := by
    intro fuel
    induction fuel with
    | zero =>
      intro i hle _
      match hle with
      | ⟨c, hc⟩ =>
        rw [az i c hc, ha1, hs1]; exact ⟨0, rfl⟩
    | succ f ih =>
      intro i hle h11
      cases i with
      | zero => rw [ha1, hs1]; exact ⟨0, rfl⟩
      | succ i' =>
        match factor i' with
        | ⟨l, k, m, hp, hn, hnd, ⟨c', hc'⟩⟩ =>
          match hle with
          | ⟨c, hc⟩ =>
            have hm11 : Dv 11 (m + 1) → False := fun h => h11 (by
              show Dv 11 (i' + 2); rw [hn]; exact dv_mul_left _ 11 _ h)
            have hp11 : l + 2 = 11 → False := fun e => h11 (by
              show Dv 11 (i' + 2)
              exact ⟨(l + 2) ^ k * (m + 1), by
                rw [hn, ← e]; show (l + 2) ^ k * (l + 2) * (m + 1) = (l + 2) ^ k * (m + 1) * (l + 2)
                generalize (l + 2) ^ k = P; lib_nat⟩)
            match ih m ⟨c' + c, Nat.succ.inj (show m + (c' + c) + 1 = f + 1 by rw [← hc, ← hc']; lib_nat)⟩ hm11, hpp l k hp hp11 with
            | ⟨t2, e2⟩, ⟨t1, e1⟩ =>
              refine ⟨t1 * s (m + 1) + s ((l + 2) ^ (k + 1)) * t2 + 5 * t1 * t2, ?_⟩
              show a (i' + 2) - s (i' + 2) = _
              rw [hn, hmA l k m hp hnd, hmS l k m hp hnd]
              have ea1 : a ((l + 2) ^ (k + 1)) = s ((l + 2) ^ (k + 1)) + 5 * t1 := by rw [← e1]; lib_int
              have ea2 : a (m + 1) = s (m + 1) + 5 * t2 := by rw [← e2]; lib_int
              rw [ea1, ea2]; lib_int
  exact fun i h => main i i ⟨0, rfl⟩ h

end G4b1

#print axioms G4b1.geo_rec
#print axioms G4b1.az
#print axioms G4b1.pp_cong
#print axioms G4b1.pull
#print axioms G4b1.factor
#print axioms G4b1.g4b1

lang/G4b1c.lean

/-
  lang/G4b1c — G4 (b1) for the curve 11a: y² + y = x³ − x². **The L-series coefficients a(n) of 11a satisfy a(n) ≡ σ(n) (mod 5)
  for every n ≥ 1 prime to 11.**
    cnt2_val      #E_aff(𝔽₂) = 4 (direct count), so a(2) = 2 − 4 = −2 ≡ 1 + 2 (mod 5)
    eleven_ne     11 ≠ 0 in 𝔽_p for every odd prime p ≠ 11 (p = 3, 5, 7, 9 by computation, p > 11 since 11 < p)
    hap_11a       a(p) = p − #E_aff(𝔽_p) ≡ 1 + p (mod 5) at every prime p ≠ 11 (G4Count.g4 for odd p)
    g4b1_11a      with the Euler-product structure of the L-series (a multiplicative on p^(k+1)·m, p ∤ m; a(p^(k+2)) = a(p)a(p^(k+1)) − p a(p^k)
                  for p ≠ 11; a(1) = 1) and σ (s multiplicative, s(p^k) = 1 + … + p^k): 5 | a(n) − σ(n) whenever 11 ∤ n   (G4b1.g4b1)
  The identification of a(n) with the q-expansion coefficients of η(z)²η(11z)² (modularity of 11a at every n) is G4 (b2), left open.
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.G4b1
import lang.Quint16

namespace G4b1c

open FTAChain ZMod Frobenius YMChain
open LibRing (castZ castN)

/-- #{(x, y) ∈ 𝔽₂² : y² + y = x³ − x²} (in 𝔽₂: y² + y + x³ + x² = 0). -/
def cnt2 : Nat := (([(0, 0), (0, 1), (1, 0), (1, 1)] : List (Nat × Nat)).filter (fun q => (q.2 * q.2 + q.2 + q.1 * q.1 * q.1 + q.1 * q.1) % 2 == 0)).length

theorem cnt2_val : cnt2 = 4 := rfl

theorem mk_val' (q a : Nat) : (ZMod.mk q a).val = md q a := rfl
theorem zero_val' (q : Nat) : (ring q).zero.val = 0 := rfl

theorem eleven_ne (m : Nat) (h : 2 * m + 1 + 1 + 1 = 11 → False) : G4Fix.eleven m ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  have e0 : castN (ring (2 * m + 1 + 1)) 11 = G4Fix.eleven m := by
    show ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring  -- (+287 chars; full line in the Zenodo source)
  have e1 : castN (ring (2 * m + 1 + 1)) 11 = (ring (2 * m + 1 + 1)).zero := e0.trans e
  rw [IntCast.castN_mk] at e1
  have hv := congrArg Subtype.val e1
  match m with
  | 0 => exact Nat.noConfusion hv
  | 1 => exact Nat.noConfusion hv
  | 2 => exact Nat.noConfusion hv
  | 3 => exact Nat.noConfusion hv
  | 4 => exact h rfl
  | m' + 5 =>
    have : md (2 * (m' + 5) + 1 + 1) 11 = 11 := md_small _ 11 (by
      show 11 < 2 * (m' + 5) + 1 + 1 + 1
      rw [show 2 * (m' + 5) + 1 + 1 + 1 = 11 + (2 * m' + 1) + 1 by lib_nat]
      exact Nat.lt_succ_of_le (Nat.le_add_right 11 _))
    rw [mk_val', this, zero_val'] at hv
    exact Nat.noConfusion hv

/-- **a_p ≡ 1 + p (mod 5) at every prime p ≠ 11** for 11a, with a(p) = p − #E_aff(𝔽_p). -/
theorem hap_11a (a : Nat → Int) (ha2 : a 2 = 2 - Int.ofNat cnt2)
    (hodd : ∀ m, a (2 * m + 1 + 1 + 1) = Int.ofNat (2 * m + 1 + 1 + 1) - sumK LibInt.intR (G4Count.cnt m) (Pairs.pL m)) :
    ∀ l, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (l + 2 = 11 → False) → ∃ t, a (l + 2) - (1 + Int.ofNat (l + 2)) = 5 * t
  | 0, _, _ => ⟨-1, by rw [ha2, cnt2_val]; rfl⟩
  | l' + 1, hp, h11 => by
    match Quint16.par l' with
    | ⟨t, Or.inr ht⟩ =>
      exfalso
      cases hp 2 ⟨t + 2, by rw [ht]; lib_nat⟩ with
      | inl e => exact Nat.noConfusion (Nat.succ.inj e)
      | inr e =>
        rw [ht] at e
        exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj e)).symm
    | ⟨t, Or.inl ht⟩ =>
      subst ht
      have hp' : ∀ d, Dv d (2 * t + 1 + 1 + 1) → d = 1 ∨ d = 2 * t + 1 + 1 + 1 := hp
      match G4Count.g4 t hp' (eleven_ne t h11) with
      | ⟨k, hk⟩ =>
        refine ⟨-k, ?_⟩
        show a (2 * t + 1 + 1 + 1) - (1 + Int.ofNat (2 * t + 1 + 1 + 1)) = 5 * -k
        rw [hodd t]
        have e : sumK LibInt.intR (G4Count.cnt t) (Pairs.pL t) = 5 * k - 1 := by rw [← hk]; lib_int
        rw [e]; lib_int

/-- **G4 (b1)**: for 11a, a(n) ≡ σ(n) (mod 5) at every n ≥ 1 with 11 ∤ n. -/
theorem g4b1_11a (a s : Nat → Int) (ha1 : a 1 = 1) (hs1 : s 1 = 1)
    (hmA : ∀ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      a ((l + 2) ^ (k + 1) * (m + 1)) = a ((l + 2) ^ (k + 1)) * a (m + 1))
    (hmS : ∀ l k m, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (Dv (l + 2) (m + 1) → False) →
      s ((l + 2) ^ (k + 1) * (m + 1)) = s ((l + 2) ^ (k + 1)) * s (m + 1))
    (hrec : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → (l + 2 = 11 → False) →
      a ((l + 2) ^ (k + 2)) = a (l + 2) * a ((l + 2) ^ (k + 1)) - Int.ofNat (l + 2) * a ((l + 2) ^ k))
    (ha2 : a 2 = 2 - Int.ofNat cnt2)
    (hodd : ∀ m, a (2 * m + 1 + 1 + 1) = Int.ofNat (2 * m + 1 + 1 + 1) - sumK LibInt.intR (G4Count.cnt m) (Pairs.pL m))
    (hsp : ∀ l k, (∀ e, Dv e (l + 2) → e = 1 ∨ e = l + 2) → s ((l + 2) ^ k) = G4b1.geo (Int.ofNat (l + 2)) k) :
    ∀ i, (Dv 11 (i + 1) → False) → ∃ t, a (i + 1) - s (i + 1) = 5 * t :=
  G4b1.g4b1 a s ha1 hs1 hmA hmS hrec (hap_11a a ha2 hodd) hsp

end G4b1c

#print axioms G4b1c.cnt2_val
#print axioms G4b1c.mk_val'
#print axioms G4b1c.zero_val'
#print axioms G4b1c.eleven_ne
#print axioms G4b1c.hap_11a
#print axioms G4b1c.g4b1_11a

#print axioms: 12 theorems, no axioms.

78.225 \(\mathbb Z[\omega ]\) is Euclidean

Division with remainder, the gcd as a combination and Euclid’s lemma in \(\mathbb Z[\omega ]\), \(N(a+b\omega )=a^2-ab+b^2\).

\[ \beta \neq 0\ \Rightarrow \ \exists q,\ N(\alpha -q\beta )<N(\beta ). \]

lang/EisEuclid.lean

/-
  lang/EisEuclid — T50, cycle 611: ℤ[ω] is Euclidean: division with remainder, gcd as a combination, and Euclid's lemma.

  ℤ[ω] = Cyclo3.cyc3 ℤ (pairs a + bω, ω² = −1 − ω), N(a + bω) = a² − ab + b², conjugate (a − b) − bω:
    en_rep         N(z) = c ∈ ℕ with c ≤ A² + B² + AB   (A = |a|, B = |b|; sign cases, no natAbs_mul)
    nrm_cast       (nrm z : ℤ) = N(z);   en_mul   N(xy) = N(x)N(y)
    edivr          β ≠ 0:  α = qβ + ρ with nrm ρ < nrm β   (round αβ̄ / N(β) componentwise; 4N(r) ≤ 3N(β)²)
    egcd_exists    d = xα + yβ dividing α and β
    eeuclid        xα + yβ = 1 and α | βγ ⇒ α | γ
  The ℤ[ω] analogue of GaussEuclid: the input for unique splitting by coprime norms and the multiplicativity of the
  Hecke theta series of ℚ(√−3).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Cyclo3
import lang.GaussEuclid

namespace EisEuclid

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + -(z.1 * z.2) + z.2 * z.2
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + -z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Cyclo3.cyc3 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + B * B := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_add_left _ _)
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c ∧
    c ≤ Int.natAbs z.1 * Int.natAbs z.1 + Int.natAbs z.2 * Int.natAbs z.2 + Int.natAbs z.1 * Int.natAbs z.2 := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + Int.ofNat B * Int.ofNat B := congrArg Int.ofNat hc
  have same : en z = Int.ofNat A * Int.ofNat A + Int.ofNat B * Int.ofNat B + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c ∧ c ≤ A * A + B * B + A * B :=
    fun h => ⟨c, by rw [h, ← e]; lib_int, Nat.le_trans (Nat.le_trans (Nat.le_add_right c (A * B)) (Nat.le_of_eq hc)) (Nat.le_add_right _ _)⟩
  have opp : en z = Int.ofNat A * Int.ofNat A + Int.ofNat B * Int.ofNat B + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c ∧ c ≤ A * A + B * B + A * B :=
    fun h => ⟨A * A + B * B + A * B, h, Nat.le_refl _⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc, _⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem nrm_le (z : Int × Int) : nrm z ≤ Int.natAbs z.1 * Int.natAbs z.1 + Int.natAbs z.2 * Int.natAbs z.2 + Int.natAbs z.1 * Int.natAbs z.2 := by
  obtain ⟨c, hc, hle⟩ := en_rep z
  unfold nrm; rw [hc]; exact hle

theorem mul_cj (z : Int × Int) : (Cyclo3.cyc3 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + -z.2) + -(z.2 * -z.2) = z.1 * z.1 + -(z.1 * z.2) + z.2 * z.2
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + -z.2) + -(z.2 * -z.2) = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Cyclo3.cyc3 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2)) + -((x.1 * y.1 + -(x.2 * y.2)) * (x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)))
      + (x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) * (x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) =
    (x.1 * x.1 + -(x.1 * x.2) + x.2 * x.2) * (y.1 * y.1 + -(y.1 * y.2) + y.2 * y.2)
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + -z.2) * (z.1 + -z.2) + -((z.1 + -z.2) * -z.2) + -z.2 * -z.2 = z.1 * z.1 + -(z.1 * z.2) + z.2 * z.2
  lib_int

/-- **Division with remainder in ℤ[ω].** -/
theorem edivr (α β : Int × Int) (M : Nat) (hβ : nrm β = M + 1) :
    ∃ q, nrm ((Cyclo3.cyc3 LibInt.intR).add α ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul q β))) < M + 1 := by
  have hN : en β = Int.ofNat (M + 1) := by rw [← nrm_cast, hβ]
  match GaussEuclid.rnd ((Cyclo3.cyc3 LibInt.intR).mul α (cj β)).1 M, GaussEuclid.rnd ((Cyclo3.cyc3 LibInt.intR).mul α (cj β)).2 M with
  | ⟨q1, r1, h1, b1⟩, ⟨q2, r2, h2, b2⟩ =>
    refine ⟨(q1, q2), ?_⟩
    generalize hρ : (Cyclo3.cyc3 LibInt.intR).add α ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (q1, q2) β)) = ρ
    have hρc : (Cyclo3.cyc3 LibInt.intR).mul ρ (cj β) = (r1, r2) := by
      rw [← hρ]
      have e : (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add α ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (q1, q2) β))) (cj β) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul α (cj β)) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (q1, q2) ((Cyclo3.cyc3 LibInt.intR).mul β (cj β)))) := by lib_ring
      rw [e, mul_cj, hN]
      apply Prod.ext
      · show ((Cyclo3.cyc3 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1) + -(q2 * 0)) = r1
        rw [h1]; lib_int
      · show ((Cyclo3.cyc3 LibInt.intR).mul α (cj β)).2 + -(q1 * 0 + q2 * Int.ofNat (M + 1) + -(q2 * 0)) = r2
        rw [h2]; lib_int
    have hn : en ρ * Int.ofNat (M + 1) = en (r1, r2) := by
      rw [← hN, ← en_cj β, ← en_mul, hρc]
    have hnN : nrm ρ * (M + 1) = nrm (r1, r2) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm ρ) * Int.ofNat (M + 1) = Int.ofNat (nrm (r1, r2))
      rw [nrm_cast, nrm_cast]; exact hn
    have hb := nrm_le (r1, r2)
    generalize Int.natAbs r1 = A at hb b1
    generalize Int.natAbs r2 = B at hb b2
    have hle : (M + 1) * (4 * nrm ρ) ≤ (M + 1) * (3 * (M + 1)) := by
      calc (M + 1) * (4 * nrm ρ) = 4 * nrm (r1, r2) := by rw [← hnN]; lib_nat
        _ ≤ 4 * (A * A + B * B + A * B) := Nat.mul_le_mul_left 4 hb
        _ = (2 * A) * (2 * A) + (2 * B) * (2 * B) + (2 * A) * (2 * B) := by lib_nat
        _ ≤ (M + 1) * (M + 1) + (M + 1) * (M + 1) + (M + 1) * (M + 1) :=
          Nat.add_le_add (Nat.add_le_add (Nat.mul_le_mul b1 b1) (Nat.mul_le_mul b2 b2)) (Nat.mul_le_mul b1 b2)
        _ = (M + 1) * (3 * (M + 1)) := by lib_nat
    have h4 := Nat.le_of_mul_le_mul_left hle (Nat.zero_lt_succ M)
    cases Nat.lt_or_ge (nrm ρ) (M + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      have : 4 * (M + 1) ≤ 3 * (M + 1) := Nat.le_trans (Nat.mul_le_mul_left 4 h) h4
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (show 3 * (M + 1) < 4 * (M + 1) by
        rw [show 4 * (M + 1) = 3 * (M + 1) + (M + 1) by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)) this)

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Cyclo3.cyc3 LibInt.intR).zero := by
  have hen : en z = 0 := by rw [← nrm_cast, h]; rfl
  have h4 : Int.ofNat (Int.natAbs (2 * z.1 + -z.2) * Int.natAbs (2 * z.1 + -z.2) + (Int.natAbs z.2 * Int.natAbs z.2 + (Int.natAbs z.2 * Int.natAbs z.2 + Int.natAbs z.2 * Int.natAbs z.2))) = Int.ofNat 0 := by
    show Int.ofNat (Int.natAbs (2 * z.1 + -z.2) * Int.natAbs (2 * z.1 + -z.2)) + (Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2) + (Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2) + Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2))) = 0
    rw [GaussEuclid.nas, GaussEuclid.nas]
    calc _ = 4 * en z := by unfold en; lib_int
      _ = 0 := by rw [hen]; lib_int
  have h4' := Int.ofNat.inj h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 :=
    Nat.le_antisymm (Nat.le_trans (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_add_left _ _)) (Nat.le_of_eq h4')) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have hA : Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) = Int.ofNat 0 := by
    rw [GaussEuclid.nas]
    calc z.1 * z.1 = en z := by unfold en; rw [b0]; lib_int
      _ = _ := hen
  have hA' := Int.ofNat.inj hA
  have a0 : z.1 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hA' with | inl e => exact e | inr e => exact e)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Cyclo3.cyc3 LibInt.intR).zero) :
    ∃ d x y, d = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x α) ((Cyclo3.cyc3 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Cyclo3.cyc3 LibInt.intR).one, (Cyclo3.cyc3 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Cyclo3.cyc3 LibInt.intR).one, by lib_ring⟩, ⟨(Cyclo3.cyc3 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **gcd as a combination in ℤ[ω].** -/
theorem egcd_exists : ∀ G (α β : Int × Int), nrm β ≤ G →
    ∃ d x y, d = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x α) ((Cyclo3.cyc3 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β
  | 0, α, β, hG => egcd_base α β (nrm_zero β (Nat.le_antisymm hG (Nat.zero_le _)))
  | G + 1, α, β, hG => by
    cases h : nrm β with
    | zero => exact egcd_base α β (nrm_zero β h)
    | succ M =>
        match edivr α β M h with
        | ⟨q, hq⟩ =>
          have hρ : nrm ((Cyclo3.cyc3 LibInt.intR).add α ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul q β))) ≤ G :=
            Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hq (Nat.le_trans (Nat.le_of_eq h.symm) hG))
          match egcd_exists G β _ hρ with
          | ⟨d, x, y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
            refine ⟨d, y, (Cyclo3.cyc3 LibInt.intR).add x ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul y q)), by rw [hd]; lib_ring, ⟨(Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul q c1) c2, ?_⟩, ⟨c1, hc1⟩⟩
            have e : α = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul q β) ((Cyclo3.cyc3 LibInt.intR).add α ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul q β))) := by lib_ring
            rw [e, hc2, hc1]
            lib_ring

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x α) ((Cyclo3.cyc3 LibInt.intR).mul y β) = (Cyclo3.cyc3 LibInt.intR).one) (h : Dvd' α ((Cyclo3.cyc3 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x γ) ((Cyclo3.cyc3 LibInt.intR).mul y c), by
      calc γ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x α) ((Cyclo3.cyc3 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x α) γ) ((Cyclo3.cyc3 LibInt.intR).mul y ((Cyclo3.cyc3 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Cyclo3.cyc3 LibInt.intR).mul α ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x γ) ((Cyclo3.cyc3 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

end EisEuclid

#print axioms EisEuclid.sgn
#print axioms EisEuclid.le_dest
#print axioms EisEuclid.ab_le
#print axioms EisEuclid.en_rep
#print axioms EisEuclid.nrm_cast
#print axioms EisEuclid.nrm_le
#print axioms EisEuclid.mul_cj
#print axioms EisEuclid.en_mul
#print axioms EisEuclid.en_cj
#print axioms EisEuclid.edivr
#print axioms EisEuclid.nrm_zero
#print axioms EisEuclid.egcd_base
#print axioms EisEuclid.egcd_exists
#print axioms EisEuclid.eeuclid

#print axioms: 14 theorems, no axioms.

78.226 Coprime splitting in \(\mathbb Z[\omega ]\)

An element of \(\mathbb Z[\omega ]\) whose norm is a product of coprime factors splits accordingly.

\[ N(\gamma )=mn,\ sm+tn=1\ \Rightarrow \ \gamma =\alpha \beta ,\ N(\alpha )=m,\ N(\beta )=n. \]

lang/EisCoprime.lean

/-
  lang/EisCoprime — T50, cycle 612: splitting by coprime norms in ℤ[ω].

  If N(γ) = mn with sm + tn = 1 (m, n ≥ 1), then γ = αβ with N(α) = m, N(β) = n (N(a + bω) = a² − ab + b²):
  α = gcd(γ, m), e = gcd(γ, n) (EisEuclid.egcd_exists), αe | γ by Euclid's lemma, γ | αe since mn = γγ̄, so γ = αe·v with v a unit;
  then N(α) | m, N(e) | n and N(α)N(e) = mn give N(α) = m. (Port of GaussCoprime.coprime_split.)
    en_emb, emb_mul, en_dvd, emb_add, en_zero_imp, cancel_left, en_nonneg_one, ecoprime_split
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.EisEuclid
import lang.GaussCoprime

namespace EisCoprime

open FTAChain DetChain LibInt EisEuclid

def emb (m : Int) : Int × Int := (m, 0)

theorem en_emb (m : Int) : en (emb m) = m * m := by show m * m + -(m * 0) + 0 * 0 = m * m; lib_int

theorem emb_mul (a b : Int) : (Cyclo3.cyc3 LibInt.intR).mul (emb a) (emb b) = emb (a * b) := by
  apply Prod.ext
  · show a * b + -(0 * 0) = a * b; lib_int
  · show a * 0 + 0 * b + -(0 * 0) = 0; lib_int

theorem en_dvd (d γ : Int × Int) (h : Dvd' d γ) : ∃ k, en γ = en d * k :=
  match h with
  | ⟨c, hc⟩ => ⟨en c, by rw [hc, en_mul]⟩

theorem emb_add (a b : Int) : emb (a + b) = (Cyclo3.cyc3 LibInt.intR).add (emb a) (emb b) := by
  apply Prod.ext
  · rfl
  · show (0 : Int) = 0 + 0; rfl

theorem en_zero_imp (z : Int × Int) (h : en z = 0) : z = (Cyclo3.cyc3 LibInt.intR).zero := by
  apply nrm_zero
  apply Int.ofNat.inj
  rw [nrm_cast]; exact h

theorem cancel_left (γ z : Int × Int) (hγ : en γ ≠ 0) (h : (Cyclo3.cyc3 LibInt.intR).mul γ z = (Cyclo3.cyc3 LibInt.intR).zero) : z = (Cyclo3.cyc3 LibInt.intR).zero := by
  have : en γ * en z = 0 := by rw [← en_mul, h]; rfl
  cases int_noZD _ _ this with
  | inl e => exact absurd e hγ
  | inr e => exact en_zero_imp z e

theorem en_nonneg_one (z : Int × Int) (h : en z = -1) : False := by
  rw [← nrm_cast] at h
  exact Int.noConfusion h

/-- **Coprime splitting**: N(γ) = mn, sm + tn = 1 ⇒ γ = αβ with N(α) = m, N(β) = n. -/
theorem ecoprime_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) :
    ∃ α β, γ = (Cyclo3.cyc3 LibInt.intR).mul α β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match egcd_exists _ γ (emb (Int.ofNat (m + 1))) (Nat.le_refl _), egcd_exists _ γ (emb (Int.ofNat (n + 1))) (Nat.le_refl _) with
  | ⟨d, x, y, hd, ⟨u, hu⟩, ⟨c1, hc1⟩⟩, ⟨e, x', y', he, ⟨u', hu'⟩, ⟨c2, hc2⟩⟩ =>
    have hco : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (emb t) c2) e) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (emb s) c1) d) = (Cyclo3.cyc3 LibInt.intR).one := by
      have h1 : (Cyclo3.cyc3 LibInt.intR).one = emb (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; rfl
      rw [h1, emb_add, ← emb_mul, ← emb_mul, hc1, hc2]; lib_ring
    -- e | u, so γ = d e v
    match eeuclid e d u _ _ hco ⟨u', by rw [← hu, hu']⟩ with
    | ⟨v, hv⟩ =>
    have hγdev : γ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul d e) v := by rw [hu, hv]; lib_ring
    -- γ | d e
    have hmn : (Cyclo3.cyc3 LibInt.intR).mul γ (cj γ) = emb (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by
      rw [mul_cj, hγ]; rfl
    have hW : (Cyclo3.cyc3 LibInt.intR).mul d e = (Cyclo3.cyc3 LibInt.intR).mul γ ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x x') γ) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).  -- (+120 chars; full line in the Zenodo source)
      rw [hd, he]
      have : (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y y') ((Cyclo3.cyc3 LibInt.intR).mul γ (cj γ)) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y y') ((Cyclo3.cyc3 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1)))) := by rw [hmn, emb_mul]
      calc (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x γ) ((Cyclo3.cyc3 LibInt.intR).mul y (emb (Int.ofNat (m + 1))))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul x' γ) ((Cyclo3.cyc3 LibInt.intR).mul y' (emb (Int.ofNat (n + 1)))))
          = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul γ ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x x') γ) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1))))))
              ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y y') ((Cyclo3.cyc3 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1))))) := by lib_ring
        _ = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul γ ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x x') γ) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1))))))
              ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y y') ((Cyclo3.cyc3 LibInt.intR).mul γ (cj γ))) := by rw [this]
        _ = _ := by lib_ring
    generalize (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x x') γ) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1)))) ((Cyclo3.cyc3 LibInt.intR).mul   -- (+55 chars; full line in the Zenodo source)
    have hγ0 : en γ ≠ 0 := by rw [hγ]; exact fun e => Nat.noConfusion (Int.ofNat.inj e)
    have hWv : (Cyclo3.cyc3 LibInt.intR).mul W v = (Cyclo3.cyc3 LibInt.intR).one := by
      have h0 : (Cyclo3.cyc3 LibInt.intR).mul γ ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul W v) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) = (Cyclo3.cyc3 LibInt.intR).zero := by
        rw [show (Cyclo3.cyc3 LibInt.intR).mul γ ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul W v) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul γ W) v) ((Cyclo3.cyc3 LibInt.intR).neg γ) by lib_ring, ← hW, ← hγdev]; lib_ring
      have := cancel_left γ _ hγ0 h0
      calc (Cyclo3.cyc3 LibInt.intR).mul W v = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul W v) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one)) (Cyclo3.cyc3 LibInt.intR).one := by lib_ring
        _ = (Cyclo3.cyc3 LibInt.intR).one := by rw [this]; lib_ring
    have hv1 : en v = 1 := by
      have : en W * en v = 1 := by rw [← en_mul, hWv]; rfl
      cases GaussCoprime.int_unit _ _ (by rw [show en v * en W = en W * en v by lib_int]; exact this) with
      | inl h => exact h
      | inr h => exact absurd h (en_nonneg_one v)
    -- norms
    have hN : en d * en e = Int.ofNat (m + 1) * Int.ofNat (n + 1) := by
      have := congrArg en hγdev
      rw [en_mul, en_mul, hv1, hγ] at this
      rw [show en d * en e = en d * en e * 1 by lib_int, ← this]; rfl
    have hdm : ∃ a, Int.ofNat (m + 1) = en d * a := by
      have k1 : Int.ofNat (m + 1) * Int.ofNat (m + 1) = en d * en c1 := by rw [← en_emb, hc1, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en d * en u := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu, en_mul]
      exact ⟨s * en c1 + t * en u, by
        calc Int.ofNat (m + 1) = Int.ofNat (m + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = s * (Int.ofNat (m + 1) * Int.ofNat (m + 1)) + t * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en d * (s * en c1 + t * en u) := by rw [k1, k2]; lib_int⟩
    have hen' : ∃ b, Int.ofNat (n + 1) = en e * b := by
      have k1 : Int.ofNat (n + 1) * Int.ofNat (n + 1) = en e * en c2 := by rw [← en_emb, hc2, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en e * en u' := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu', en_mul]
      exact ⟨t * en c2 + s * en u', by
        calc Int.ofNat (n + 1) = Int.ofNat (n + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = t * (Int.ofNat (n + 1) * Int.ofNat (n + 1)) + s * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en e * (t * en c2 + s * en u') := by rw [k1, k2]; lib_int⟩
    match hdm, hen' with
    | ⟨a, ha⟩, ⟨b, hb⟩ =>
      have hab : a * b = 1 := by
        have h0 : (en d * en e) * (a * b + -1) = 0 := by
          rw [show (en d * en e) * (a * b + -1) = (en d * a) * (en e * b) + -(en d * en e) by lib_int, ← ha, ← hb, hN]; lib_int
        cases int_noZD _ _ h0 with
        | inl e0 => exact absurd (by rw [hN] at e0; exact e0) (fun e1 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = 0 => Nat.noConfusion (Int.ofNat.inj e1))
        | inr e0 => calc a * b = (a * b + -1) + 1 := by lib_int
            _ = 1 := by rw [e0]; rfl
      have pos : ∀ (k : Nat) (z : Int × Int) (c : Int), Int.ofNat (k + 1) = en z * c → c = 1 ∨ c = -1 → c = 1 := fun k z c h hc => by
        cases hc with
        | inl h1 => exact h1
        | inr h1 =>
          exfalso
          rw [h1, ← nrm_cast] at h
          have : Int.ofNat (k + 1) + Int.ofNat (nrm z) = 0 := by rw [h]; lib_int
          have h2 : k + nrm z + 1 = 0 := by rw [show k + nrm z + 1 = k + 1 + nrm z by lib_nat]; exact Int.ofNat.inj (show Int.ofNat (k + 1 + nrm z) = Int.ofNat 0 from this)
          exact Nat.noConfusion h2
      have a1 := pos m d a ha (GaussCoprime.int_unit a b hab)
      have b1 := pos n e b hb (GaussCoprime.int_unit b a (by rw [show b * a = a * b by lib_int]; exact hab))
      refine ⟨d, (Cyclo3.cyc3 LibInt.intR).mul e v, by rw [hγdev]; lib_ring, by rw [ha, a1]; lib_int, by rw [en_mul, hv1, hb, b1]⟩

end EisCoprime

#print axioms EisCoprime.en_emb
#print axioms EisCoprime.emb_mul
#print axioms EisCoprime.en_dvd
#print axioms EisCoprime.emb_add
#print axioms EisCoprime.en_zero_imp
#print axioms EisCoprime.cancel_left
#print axioms EisCoprime.en_nonneg_one
#print axioms EisCoprime.ecoprime_split

#print axioms: 8 theorems, no axioms.

78.227 Primary normalisation in \(\mathbb Z[\omega ]\)

Elements \(\equiv 1\) mod \(3\) are closed under products, and every \(\alpha \) prime to \(3\) has such an associate.

\[ 3\nmid N(\alpha )\ \Rightarrow \ \exists u,\ N(u)=1,\ u\alpha \equiv 1\pmod 3. \]

lang/EisPrim.lean

/-
  lang/EisPrim — T50, cycle 613: primary normalisation in ℤ[ω].

  Prim3 z :⇔ z ≡ 1 (mod 3), i.e. a ≡ 1, b ≡ 0 (mod 3) for z = a + bω. This class is closed under products, and for 3 ∤ N(α)
  exactly one of the six units u (±1, ±ω, ±ω²) makes uα primary (existence here):
    no3            3x ∉ {1, 2}
    res3           every integer is 3q, 3q + 1 or 3q − 1                       (GaussEuclid.rnd with M = 2)
    prim3_mul      Prim3 x, Prim3 y ⇒ Prim3 (xy)
    prim3_c3       Prim3 x ⇒ N(x) ≡ 1 (mod 3)
    normalize3     N(α) ≢ 0 (mod 3) ⇒ ∃ u, N(u) = 1 and Prim3 (uα)            (the nine residue classes of (a, b) mod 3)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.EisCoprime

namespace EisPrim

open FTAChain DetChain LibInt EisEuclid

def Prim3 (z : Int × Int) : Prop := ∃ e f : Int, z.1 = 3 * e + 1 ∧ z.2 = 3 * f
def C3 (x : Int) : Prop := ∃ k : Int, x = 3 * k + 1 ∨ x = 3 * k + 2

theorem no3_nat (n : Nat) : 3 * n ≠ 1 ∧ 3 * n ≠ 2 := by
  cases n with
  | zero => exact ⟨fun h => Nat.noConfusion h, fun h => Nat.noConfusion h⟩
  | succ n =>
    rw [show 3 * (n + 1) = 3 * n + 2 + 1 by lib_nat]
    exact ⟨fun h => Nat.noConfusion (Nat.succ.inj h), fun h => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj h))⟩

theorem no3 (x : Int) : 3 * x ≠ 1 ∧ 3 * x ≠ 2 := by
  cases x with
  | ofNat n =>
    exact ⟨fun h => (no3_nat n).1 (Int.ofNat.inj h), fun h => (no3_nat n).2 (Int.ofNat.inj h)⟩
  | negSucc n => exact ⟨fun h => Int.noConfusion h, fun h => Int.noConfusion h⟩

theorem c3_ne (x K : Int) (hc : C3 x) (h : x = 3 * K) : False := by
  obtain ⟨k, hk | hk⟩ := hc
  · exact (no3 (K + -k)).1 (by rw [show 3 * (K + -k) = 3 * K + -(3 * k) by lib_int, ← h, hk]; lib_int)
  · exact (no3 (K + -k)).2 (by rw [show 3 * (K + -k) = 3 * K + -(3 * k) by lib_int, ← h, hk]; lib_int)

theorem res3 (a : Int) : ∃ q, a = 3 * q ∨ a = 3 * q + 1 ∨ a = 3 * q + -1 := by
  obtain ⟨q, r, h, hr⟩ := GaussEuclid.rnd a 2
  refine ⟨q, ?_⟩
  have e : a = 3 * q + r := by rw [h]; show q * 3 + r = 3 * q + r; lib_int
  cases r with
  | ofNat t =>
    cases t with
    | zero => exact Or.inl (by rw [e]; show 3 * q + 0 = 3 * q; lib_int)
    | succ t =>
      cases t with
      | zero => exact Or.inr (Or.inl e)
      | succ t =>
        exfalso
        have : 2 * (t + 2) ≤ 3 := hr
        rw [show 2 * (t + 2) = 2 * t + 1 + 3 by lib_nat] at this
        exact Nat.not_succ_le_self 3 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 4) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  | negSucc t =>
    cases t with
    | zero => exact Or.inr (Or.inr e)
    | succ t =>
      exfalso
      have : 2 * (t + 2) ≤ 3 := hr
      rw [show 2 * (t + 2) = 2 * t + 1 + 3 by lib_nat] at this
      exact Nat.not_succ_le_self 3 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 4) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))

theorem prim3_mul (x y : Int × Int) (hx : Prim3 x) (hy : Prim3 y) : Prim3 ((Cyclo3.cyc3 LibInt.intR).mul x y) := by
  obtain ⟨e, f, h1, h2⟩ := hx
  obtain ⟨g, h, h3, h4⟩ := hy
  refine ⟨3 * e * g + e + g + -(3 * f * h), (3 * e + 1) * h + f * (3 * g + 1) + -(3 * f * h), ?_, ?_⟩
  · show x.1 * y.1 + -(x.2 * y.2) = _; rw [h1, h2, h3, h4]; lib_int
  · show x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2) = _; rw [h1, h2, h3, h4]; lib_int


theorem prim3_c3 (x : Int × Int) (hx : Prim3 x) : C3 (en x) := by
  obtain ⟨e, f, h1, h2⟩ := hx
  refine ⟨3 * e * e + 2 * e + -(3 * e * f) + -f + 3 * f * f, Or.inl ?_⟩
  unfold en; rw [h1, h2]; lib_int

/-- the six units. -/
theorem unit_mul1 (u1 u2 a b : Int) : ((Cyclo3.cyc3 LibInt.intR).mul (u1, u2) (a, b)).1 = u1 * a + -(u2 * b) := rfl
theorem unit_mul2 (u1 u2 a b : Int) : ((Cyclo3.cyc3 LibInt.intR).mul (u1, u2) (a, b)).2 = u1 * b + u2 * a + -(u2 * b) := rfl

/-- **Primary normalisation in ℤ[ω].** -/
theorem normalize3 (α : Int × Int) (hα : C3 (en α)) : ∃ u, en u = 1 ∧ Prim3 ((Cyclo3.cyc3 LibInt.intR).mul u α) := by
  obtain ⟨a, b⟩ := α
  obtain ⟨e, he⟩ := res3 a
  obtain ⟨f, hf⟩ := res3 b
  have hn : en (a, b) = a * a + -(a * b) + b * b := rfl
  rw [hn] at hα
  rcases he with ha | ha | ha <;> rcases hf with hb | hb | hb
  · exact (c3_ne _ (3 * (e * e + -(e * f) + f * f)) hα (by rw [ha, hb]; lib_int)).elim
  · -- (0, 1): u = ω² = (−1, −1)
    exact ⟨(-1, -1), rfl, f + -e, -e, by rw [unit_mul1, ha, hb]; lib_int, by rw [unit_mul2, ha, hb]; lib_int⟩
  · -- (0, −1): u = −ω² = (1, 1)
    exact ⟨(1, 1), rfl, e + -f, e, by rw [unit_mul1, ha, hb]; lib_int, by rw [unit_mul2, ha, hb]; lib_int⟩
  · -- (1, 0): u = 1
    exact ⟨(1, 0), rfl, e, f, by rw [unit_mul1, ha, hb]; lib_int, by rw [unit_mul2, ha, hb]; lib_int⟩
  · -- (1, 1): u = −ω = (0, −1)
    exact ⟨(0, -1), rfl, f, f + -e, by rw [unit_mul1, ha, hb]; lib_int, by rw [unit_mul2, ha, hb]; lib_int⟩
  · exact (c3_ne _ (3 * (e * e + -(e * f) + f * f) + 3 * e + -(3 * f) + 1) hα (by rw [ha, hb]; lib_int)).elim
  · -- (−1, 0): u = −1
    exact ⟨(-1, 0), rfl, -e, -f, by rw [unit_mul1, ha, hb]; lib_int, by rw [unit_mul2, ha, hb]; lib_int⟩
  · exact (c3_ne _ (3 * (e * e + -(e * f) + f * f) + -(3 * e) + 3 * f + 1) hα (by rw [ha, hb]; lib_int)).elim
  · -- (−1, −1): u = ω = (0, 1)
    exact ⟨(0, 1), rfl, -f, e + -f, by rw [unit_mul1, ha, hb]; lib_int, by rw [unit_mul2, ha, hb]; lib_int⟩

end EisPrim

#print axioms EisPrim.no3_nat
#print axioms EisPrim.no3
#print axioms EisPrim.c3_ne
#print axioms EisPrim.res3
#print axioms EisPrim.prim3_mul
#print axioms EisPrim.prim3_c3
#print axioms EisPrim.unit_mul1
#print axioms EisPrim.unit_mul2
#print axioms EisPrim.normalize3

#print axioms: 9 theorems, no axioms.

78.228 Unique primary splitting in \(\mathbb Z[\omega ]\)

A unit \(\equiv 1\) mod \(3\) is \(1\); a primary \(\gamma \) of norm \(mn\) with \((m,n)=(mn,3)=1\) is a product of primaries of norms \(m\), \(n\) in exactly one way.

\[ \gamma \equiv 1\ (3),\ N(\gamma )=mn\ \Rightarrow \ \exists !\,(\alpha ,\beta ),\ \gamma =\alpha \beta ,\ \alpha ,\beta \equiv 1\ (3),\ N(\alpha )=m. \]

lang/EisUnique3.lean

/-
  lang/EisUnique3 — T50, cycle 614: uniqueness and existence of the primary splitting in ℤ[ω].

    unit_one3     N(u) = 1, α and uα ≡ 1 (mod 3) ⇒ u = 1      (u ≡ 1 mod 3 via u·N(α) = (uα)ᾱ; then T = Tr u = 2 − 9N(w) for
                                                            u = 1 + 3w, and T² ≤ 4N(u) = 4 forces N(w) = 0)
    prim3_quot    x, xy ≡ 1 (mod 3), 3 ∤ N(y) ⇒ y ≡ 1 (mod 3)
    uniq_split3   αβ = α'β', α, α' ≡ 1 (mod 3), N(α) = N(α') = M, N(β') = N, sM + tN = 1 ⇒ α = α', β = β'
    prim3_split   γ ≡ 1 (mod 3), N(γ) = mn coprime, 3 ∤ m, n ⇒ γ = αβ, α, β ≡ 1 (mod 3), N(α) = m, N(β) = n
  The ℤ[ω] analogue of GaussUnique (with EisCoprime, EisPrim).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.EisPrim

namespace EisUnique3

open FTAChain DetChain LibInt EisEuclid EisPrim

theorem big81 (c : Nat) (T : Nat) (hT : T ≤ 4) (h : T + 36 * (c + 1) = 4 + 81 * ((c + 1) * (c + 1))) : False := by
  have h1 : T + 36 * (c + 1) ≤ 4 + 36 * (c + 1) := Nat.add_le_add_right hT _
  have h2 : 4 + 81 * ((c + 1) * (c + 1)) = (4 + 36 * (c + 1)) + (45 + (81 * (c * c) + 126 * c)) := by lib_nat
  rw [h, h2] at h1
  exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (Nat.lt_add_of_pos_right (Nat.lt_of_lt_of_le (Nat.zero_lt_succ 44) (Nat.le_add_right 45 _))) h1)

/-- a unit ≡ 1 (mod 3) is 1. -/
theorem unit_three (w1 w2 : Int) (hu : en (3 * w1 + 1, 3 * w2) = 1) : w1 = 0 ∧ w2 = 0 := by
  obtain ⟨c, hc, _⟩ := en_rep (w1, w2)
  have hT : (2 * (3 * w1 + 1) + -(3 * w2)) = 2 + -(9 * Int.ofNat c) := by
    rw [← hc]
    have e1 : en (3 * w1 + 1, 3 * w2) = 1 + (6 * w1 + -(3 * w2)) + 9 * en (w1, w2) := by
      show (3 * w1 + 1) * (3 * w1 + 1) + -((3 * w1 + 1) * (3 * w2)) + 3 * w2 * (3 * w2) = 1 + (6 * w1 + -(3 * w2)) + 9 * (w1 * w1 + -(w1 * w2) + w2 * w2)
      lib_int
    rw [hu] at e1
    calc (2 * (3 * w1 + 1) + -(3 * w2)) = 2 + (6 * w1 + -(3 * w2)) := by lib_int
      _ = 2 + ((1 + (6 * w1 + -(3 * w2)) + 9 * en (w1, w2)) + -(1 + 9 * en (w1, w2))) := by lib_int
      _ = 2 + -(9 * en (w1, w2)) := by rw [← e1]; lib_int
  have h4 : Int.ofNat (Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) * Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) + 3 * (Int.natAbs (3 * w2) * Int.natAbs (3 * w2))) = Int.ofNat 4 := by
    show Int.ofNat (Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) * Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2))) + 3 * Int.ofNat (Int.natAbs (3 * w2) * Int.natAbs (3 * w2)) = _
    rw [GaussEuclid.nas, GaussEuclid.nas]
    calc _ = 4 * en (3 * w1 + 1, 3 * w2) := by
          show _ = 4 * ((3 * w1 + 1) * (3 * w1 + 1) + -((3 * w1 + 1) * (3 * w2)) + 3 * w2 * (3 * w2))
          lib_int
      _ = Int.ofNat 4 := by rw [hu]; rfl
  have hle : Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) * Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) ≤ 4 := Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq (Int.ofNat.inj h4))
  have hsq : Int.ofNat (Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) * Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) + 36 * c) = Int.ofNat (4 + 81 * (c * c)) := by
    show Int.ofNat (Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2)) * Int.natAbs (2 * (3 * w1 + 1) + -(3 * w2))) + Int.ofNat 36 * Int.ofNat c = Int.ofNat 4 + Int.ofNat 81 * (Int.ofNat c * Int.ofNat c)
    rw [GaussEuclid.nas, hT]
    show (2 + -(9 * Int.ofNat c)) * (2 + -(9 * Int.ofNat c)) + 36 * Int.ofNat c = 4 + 81 * (Int.ofNat c * Int.ofNat c)
    lib_int
  cases c with
  | succ c => exact (big81 c _ hle (Int.ofNat.inj hsq)).elim
  | zero =>
    have hz := nrm_zero (w1, w2) (by unfold nrm; rw [hc]; rfl)
    exact ⟨congrArg Prod.fst hz, congrArg Prod.snd hz⟩

/-- **N(u) = 1 and α, uα ≡ 1 (mod 3) ⇒ u = 1.** -/
theorem unit_one3 (u α : Int × Int) (hu : en u = 1) (pα : Prim3 α) (pu : Prim3 ((Cyclo3.cyc3 LibInt.intR).mul u α)) :
    u = (Cyclo3.cyc3 LibInt.intR).one := by
  obtain ⟨u1, u2⟩ := u
  obtain ⟨a, b⟩ := α
  obtain ⟨e, f, ha, hb⟩ := pα
  obtain ⟨g, h, hg, hh⟩ := pu
  have hg' : u1 * a + -(u2 * b) = 3 * g + 1 := hg
  have hh' : u1 * b + u2 * a + -(u2 * b) = 3 * h := hh
  have ha' : a = 3 * e + 1 := ha
  have hb' : b = 3 * f := hb
  have k1 : u1 * (a * a + -(a * b) + b * b) = (u1 * a + -(u2 * b)) * (a + -b) + (u1 * b + u2 * a + -(u2 * b)) * b := by lib_int
  have k2 : u2 * (a * a + -(a * b) + b * b) = (u1 * b + u2 * a + -(u2 * b)) * a + -((u1 * a + -(u2 * b)) * b) := by lib_int
  rw [hg', hh'] at k1 k2
  rw [ha', hb'] at k1 k2
  generalize hK : 3 * e * e + 2 * e + -(3 * e * f) + -f + 3 * f * f = K
  have hN : (3 * e + 1) * (3 * e + 1) + -((3 * e + 1) * (3 * f)) + 3 * f * (3 * f) = 3 * K + 1 := by rw [← hK]; lib_int
  rw [hN] at k1 k2
  have hu1 : u1 = 3 * ((3 * g + 1) * (e + -f) + g + 3 * h * f + -(K * u1)) + 1 := by
    calc u1 = u1 * (3 * K + 1) + -(3 * (K * u1)) := by lib_int
      _ = _ := by rw [k1]; lib_int
  have hu2 : u2 = 3 * (h * (3 * e + 1) + -((3 * g + 1) * f) + -(K * u2)) := by
    calc u2 = u2 * (3 * K + 1) + -(3 * (K * u2)) := by lib_int
      _ = _ := by rw [k2]; lib_int
  have hu' : en (3 * ((3 * g + 1) * (e + -f) + g + 3 * h * f + -(K * u1)) + 1, 3 * (h * (3 * e + 1) + -((3 * g + 1) * f) + -(K * u2))) = 1 := by
    rw [← hu1, ← hu2]; exact hu
  obtain ⟨z1, z2⟩ := unit_three _ _ hu'
  apply Prod.ext
  · show u1 = 1; rw [hu1, z1]; rfl
  · show u2 = 0; rw [hu2, z2]; rfl

/-- y ≡ 1 (mod 3) when x and xy are (3 ∤ N(y)). -/
theorem prim3_quot (x y : Int × Int) (hy : C3 (en y)) (hx : Prim3 x) (hxy : Prim3 ((Cyclo3.cyc3 LibInt.intR).mul x y)) : Prim3 y := by
  obtain ⟨u, hu, hpu⟩ := normalize3 y hy
  have h1 : Prim3 ((Cyclo3.cyc3 LibInt.intR).mul u ((Cyclo3.cyc3 LibInt.intR).mul x y)) := by
    rw [show (Cyclo3.cyc3 LibInt.intR).mul u ((Cyclo3.cyc3 LibInt.intR).mul x y) = (Cyclo3.cyc3 LibInt.intR).mul x ((Cyclo3.cyc3 LibInt.intR).mul u y) by lib_ring]
    exact prim3_mul _ _ hx hpu
  have hu1 := unit_one3 u _ hu hxy h1
  rw [hu1, (Cyclo3.cyc3 LibInt.intR).one_mul] at hpu
  exact hpu

/-- **Uniqueness of the primary splitting.** -/
theorem uniq_split3 (α β α' β' : Int × Int) (M N s t : Int) (hα : en α = M) (hα' : en α' = M) (hβ' : en β' = N)
    (hst : s * M + t * N = 1) (hM : M ≠ 0) (pα : Prim3 α) (pα' : Prim3 α') (h : (Cyclo3.cyc3 LibInt.intR).mul α β = (Cyclo3.cyc3 LibInt.intR).mul α' β') :
    α = α' ∧ β = β' := by
  have hco : (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb s) (cj α)) α) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb t) (cj β')) β') = (Cyclo3.cyc3 LibInt.intR).one := by
    rw [show (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb s) (cj α)) α) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb t) (cj β')) β') = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb s) ((Cyclo3.cyc3 LibInt.intR).mul α (cj α))) ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb   -- (+59 chars; full line in the Zenodo source)
      mul_cj, mul_cj, hα, hβ']
    show (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb s) (EisCoprime.emb M)) ((Cyclo3.cyc3 LibInt.intR).mul (EisCoprime.emb t) (EisCoprime.emb N)) = _
    rw [EisCoprime.emb_mul, EisCoprime.emb_mul, ← EisCoprime.emb_add, hst]; rfl
  match eeuclid α β' α' _ _ hco ⟨β, by rw [show (Cyclo3.cyc3 LibInt.intR).mul β' α' = (Cyclo3.cyc3 LibInt.intR).mul α' β' by lib_ring, ← h]⟩ with
  | ⟨w, hw⟩ =>
    have hgw : en w = 1 := by
      have e : M * en w = M * 1 := by
        calc M * en w = en α * en w := by rw [hα]
          _ = en α' := by rw [← en_mul, ← hw]
          _ = M * 1 := by rw [hα']; lib_int
      have e0 : M * (en w + -1) = 0 := by rw [show M * (en w + -1) = M * en w + -(M * 1) by lib_int, e]; lib_int
      cases int_noZD _ _ e0 with
      | inl h0 => exact absurd h0 hM
      | inr h0 => calc en w = (en w + -1) + 1 := by lib_int
          _ = 1 := by rw [h0]; rfl
    have hw1 : w = (Cyclo3.cyc3 LibInt.intR).one :=
      unit_one3 w α hgw pα (by rw [show (Cyclo3.cyc3 LibInt.intR).mul w α = α' by rw [hw]; lib_ring]; exact pα')
    have hαα : α = α' := by rw [hw, hw1]; lib_ring
    refine ⟨hαα, ?_⟩
    have h0 : (Cyclo3.cyc3 LibInt.intR).mul α ((Cyclo3.cyc3 LibInt.intR).add β ((Cyclo3.cyc3 LibInt.intR).neg β')) = (Cyclo3.cyc3 LibInt.intR).zero := by
      rw [show (Cyclo3.cyc3 LibInt.intR).mul α ((Cyclo3.cyc3 LibInt.intR).add β ((Cyclo3.cyc3 LibInt.intR).neg β')) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul α β) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul α β')) by lib_ring, h, ← hαα]; lib_ring
    have := EisCoprime.cancel_left α _ (by rw [hα]; exact hM) h0
    calc β = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add β ((Cyclo3.cyc3 LibInt.intR).neg β')) β' := by lib_ring
      _ = β' := by rw [this]; lib_ring

/-- **Existence of the primary splitting.** -/
theorem prim3_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) (hm : C3 (Int.ofNat (m + 1))) (hn : C3 (Int.ofNat (n + 1)))
    (pγ : Prim3 γ) : ∃ α β, γ = (Cyclo3.cyc3 LibInt.intR).mul α β ∧ Prim3 α ∧ Prim3 β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match EisCoprime.ecoprime_split γ m n s t hγ hst with
  | ⟨α0, β0, h0, hα0, hβ0⟩ =>
    match normalize3 α0 (by rw [hα0]; exact hm) with
    | ⟨u, gu, pu⟩ =>
      have uc : (Cyclo3.cyc3 LibInt.intR).mul u (cj u) = (Cyclo3.cyc3 LibInt.intR).one := by rw [mul_cj, gu]; rfl
      have hβn : en ((Cyclo3.cyc3 LibInt.intR).mul (cj u) β0) = Int.ofNat (n + 1) := by
        rw [en_mul, show en (cj u) = en u from en_cj u, gu, hβ0]; lib_int
      have hsplit : γ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul u α0) ((Cyclo3.cyc3 LibInt.intR).mul (cj u) β0) := by
        rw [h0, show (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul u α0) ((Cyclo3.cyc3 LibInt.intR).mul (cj u) β0) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul u (cj u)) ((Cyclo3.cyc3 LibInt.intR).mul α0 β0) by lib_ring, uc]; lib_ring
      refine ⟨(Cyclo3.cyc3 LibInt.intR).mul u α0, (Cyclo3.cyc3 LibInt.intR).mul (cj u) β0, hsplit, pu,
        prim3_quot _ _ (by rw [hβn]; exact hn) pu (by rw [← hsplit]; exact pγ),
        by rw [en_mul, gu, hα0]; lib_int, hβn⟩

end EisUnique3

#print axioms EisUnique3.big81
#print axioms EisUnique3.unit_three
#print axioms EisUnique3.unit_one3
#print axioms EisUnique3.prim3_quot
#print axioms EisUnique3.uniq_split3
#print axioms EisUnique3.prim3_split

#print axioms: 6 theorems, no axioms.

78.229 Hecke characters of \(\mathbb Q(\sqrt {-3})\): multiplicativity

For every multiplicative \(w\) on \(\mathbb Z[\omega ]\) (e.g. \(w(\alpha )=\alpha ^k\)) the theta coefficients over primary \(\alpha \) are multiplicative.

\[ c_w(n)=\sum _{\alpha \equiv 1\,(3),\ N\alpha =n}w(\alpha ),\qquad c_w(mn)=c_w(m)\,c_w(n)\quad ((m,n)=(mn,3)=1). \]

lang/HeckeMult3.lean

/-
  lang/HeckeMult3 — T50, cycle 615: **the theta series of every multiplicative weight on primary Eisenstein integers is multiplicative**:
  for w : ℤ[ω] → ℤ[ω] with w(αβ) = w(α)w(β) (e.g. w(α) = α^k, the Hecke characters of ℚ(√−3) of ∞-type k, conductor 3),
    c_w(n) = Σ_{α ≡ 1 (mod 3), N(α) = n} w(α)   satisfies   c_w(mn) = c_w(m) c_w(n)  for coprime m, n prime to 3.
  The finite enumeration is box3 n = HeckeMult.box (4n) (|a|, |b| ≤ 4N since 4N = (2a − b)² + 3b² = (2b − a)² + 3a²).
    abs_le3, idv3_iff, pd3_iff, cw3, hecke_mult3, hecke_mult3_pow
  (Port of HeckeMultW with EisUnique3.prim3_split/uniq3_split.) For k = 1 the c_w(p) are the a_p of 27a (y² + y = x³).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.EisUnique3
import lang.HeckeMultW

namespace HeckeMult3

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain EisEuclid EisPrim EisUnique3

abbrev box3 (n : Nat) : List (Int × Int) := HeckeMult.box (4 * n)

theorem abs_one3 (a b : Int) (n : Nat) (h : a * a + -(a * b) + b * b = Int.ofNat n) : Int.natAbs b ≤ 4 * n := by
  have h4 : Int.ofNat (Int.natAbs (2 * a + -b) * Int.natAbs (2 * a + -b) + 3 * (Int.natAbs b * Int.natAbs b)) = Int.ofNat (4 * n) := by
    show Int.ofNat (Int.natAbs (2 * a + -b) * Int.natAbs (2 * a + -b)) + 3 * Int.ofNat (Int.natAbs b * Int.natAbs b) = 4 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; lib_int
  have h4' := Int.ofNat.inj h4
  calc Int.natAbs b ≤ Int.natAbs b * Int.natAbs b := HeckeMult.le_sq _
    _ ≤ 3 * (Int.natAbs b * Int.natAbs b) := Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_succ_of_le (Nat.le_succ 1)))
    _ ≤ _ := Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4')

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 4n. -/
theorem abs_le3 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 4 * n ∧ Int.natAbs z.2 ≤ 4 * n :=
  ⟨abs_one3 z.2 z.1 n (by rw [← h]; unfold en; lib_int), abs_one3 z.1 z.2 n h⟩

def PD3 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim3 z

theorem pd3_iff (n : Nat) (z : Int × Int) :
    (en z = Int.ofNat n ∧ (ZMod.md 2 (Int.natAbs (z.1 + -1)) = 0 ∧ ZMod.md 2 (Int.natAbs z.2) = 0)) ↔ PD3 n z := by
  constructor
  · intro ⟨h1, h2, h3⟩
    obtain ⟨e, he⟩ := (HeckeMult.idv_iff 2 _).mpr h2
    obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 2 _).mpr h3
    exact ⟨h1, e, f, by calc z.1 = (z.1 + -1) + 1 := by lib_int
                              _ = 3 * e + 1 := by rw [he]; rfl, hf⟩
  · intro ⟨h1, e, f, he, hf⟩
    exact ⟨h1, (HeckeMult.idv_iff 2 _).mp ⟨e, by rw [he]; show 3 * e + 1 + -1 = 3 * e; lib_int⟩, (HeckeMult.idv_iff 2 _).mp ⟨f, hf⟩⟩

instance (n : Nat) (z : Int × Int) : Decidable (PD3 n z) := decidable_of_iff _ (pd3_iff n z)

theorem zsum3 {A : Type} : ∀ l : List A, sumK (Cyclo3.cyc3 LibInt.intR) (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) l = (Cyclo3.cyc3 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Cyclo3.cyc3 LibInt.intR).add (Cyclo3.cyc3 LibInt.intR).zero (sumK (Cyclo3.cyc3 LibInt.intR) (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) l) = _; rw [zsum3 l]; exact (Cyclo3.cyc3 LibInt.intR).zero_add _

def I3 (n : Nat) (z : Int × Int) : Int × Int := if PD3 n z then (Cyclo3.cyc3 LibInt.intR).one else (Cyclo3.cyc3 LibInt.intR).zero

theorem I3_pos (n : Nat) (z : Int × Int) (h : PD3 n z) : I3 n z = (Cyclo3.cyc3 LibInt.intR).one := ite_pos' _ _ h
theorem I3_neg (n : Nat) (z : Int × Int) (h : ¬ PD3 n z) : I3 n z = (Cyclo3.cyc3 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw3 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Cyclo3.cyc3 LibInt.intR) (fun z => (Cyclo3.cyc3 LibInt.intR).mul (I3 n z) (w z)) (box3 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult3 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : C3 (Int.ofNat (m + 1))) (hn : C3 (Int.ofNat (n + 1))) :
    cw3 w ((m + 1) * (n + 1)) = (Cyclo3.cyc3 LibInt.intR).mul (cw3 w (m + 1)) (cw3 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD3 (m + 1) α → PD3 (n + 1) β → PD3 ((m + 1) * (n + 1)) ((Cyclo3.cyc3 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim3_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Cyclo3.cyc3 LibInt.intR).mul (cw3 w (m + 1)) (cw3 w (n + 1))
      = sumK (Cyclo3.cyc3 LibInt.intR) (fun α => sumK (Cyclo3.cyc3 LibInt.intR) (fun β => sumK (Cyclo3.cyc3 LibInt.intR) (fun γ => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 (m + 1) α) (I3 (n + 1) β)) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β))) (w γ)) (box3 ((m + 1) * (n + 1)))) (box3 (n + 1))) (box3 (m + 1)) := by
    unfold cw3; rw [sumK_mul]
    refine sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD3 (m + 1) α
    · by_cases hβ : PD3 (n + 1) β
      · rw [I3_pos _ _ hα, I3_pos _ _ hβ]
        have hb := abs_le3 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Cyclo3.cyc3 LibInt.intR) (fun γ => w γ) ((Cyclo3.cyc3 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Cyclo3.cyc3 LibInt.intR) (fun γ => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β))) (w γ)) (box3 ((m + 1) * (n + 1)))
            = sumK (Cyclo3.cyc3 LibInt.intR) (fun γ => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β)) (w γ)) (box3 ((m + 1) * (n + 1))) from sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I3_neg _ _ hβ]
        rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun γ => show (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 (m + 1) α) (Cyclo3.cyc3 LibInt.intR).zero) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β))) (w γ) = (Cyclo3.cyc3 LibInt.intR).zero by lib_ring)]
        rw [zsum3]; lib_ring
    · rw [I3_neg _ _ hα]
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun γ => show (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (I3 (n + 1) β)) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β))) (w γ) = (Cyclo3.cyc3 LibInt.intR).zero by lib_ring)]
      rw [zsum3]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => sumK_swap (Cyclo3.cyc3 LibInt.intR) _ _ _)]
  rw [sumK_swap (Cyclo3.cyc3 LibInt.intR)]
  unfold cw3
  refine sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Cyclo3.cyc3 LibInt.intR) (fun α => sumK (Cyclo3.cyc3 LibInt.intR) (fun β => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 (m + 1) α) (I3 (n + 1) β)) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β))) (w γ)) (box3 (n + 1))) (box3 (m + 1))
      = (Cyclo3.cyc3 LibInt.intR).mul (sumK (Cyclo3.cyc3 LibInt.intR) (fun α => sumK (Cyclo3.cyc3 LibInt.intR) (fun β => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 (m + 1) α) (I3 (n + 1) β)) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β))) (box3 (n + 1))) (box3 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD3 ((m + 1) * (n + 1)) γ
  · rw [I3_pos _ _ hγ]
    match prim3_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 (m + 1) α) (I3 (n + 1) β)) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β)) = (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) α α0) (delta (Cyclo3.cyc3 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD3 (m + 1) α ∧ PD3 (n + 1) β ∧ γ = (Cyclo3.cyc3 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split3 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I3_pos _ _ hα, I3_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) α α0) (delta (Cyclo3.cyc3 LibInt.intR) β β0) = (Cyclo3.cyc3 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD3 (m + 1) α
            · by_cases hβ : PD3 (n + 1) β
              · have hg : ¬ γ = (Cyclo3.cyc3 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I3_neg _ _ hβ]; lib_ring
            · rw [I3_neg _ _ hα]; lib_ring
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le3 α0 _ nα0
      have bb := abs_le3 β0 _ nβ0
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Cyclo3.cyc3 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => show (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) α α0) (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) α α0) (Cyclo3.cyc3 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Cyclo3.cyc3 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I3_neg _ _ hγ]
    have pt : ∀ α β, (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 (m + 1) α) (I3 (n + 1) β)) (delta (Cyclo3.cyc3 LibInt.intR) γ ((Cyclo3.cyc3 LibInt.intR).mul α β)) = (Cyclo3.cyc3 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD3 (m + 1) α
      · by_cases hβ : PD3 (n + 1) β
        · have hg : ¬ γ = (Cyclo3.cyc3 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I3_neg _ _ hβ]; lib_ring
      · rw [I3_neg _ _ hα]; lib_ring
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun α => zsum3 _), zsum3]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult3_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : C3 (Int.ofNat (m + 1))) (hn : C3 (Int.ofNat (n + 1))) :
    cw3 (fun z => Frobenius.pw (Cyclo3.cyc3 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Cyclo3.cyc3 LibInt.intR).mul (cw3 (fun z => Frobenius.pw (Cyclo3.cyc3 LibInt.intR) z k) (m + 1)) (cw3 (fun z => Frobenius.pw (Cyclo3.cyc3 LibInt.intR) z k) (n + 1)) :=
  hecke_mult3 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult3

#print axioms HeckeMult3.abs_one3
#print axioms HeckeMult3.abs_le3
#print axioms HeckeMult3.pd3_iff
#print axioms HeckeMult3.zsum3
#print axioms HeckeMult3.I3_pos
#print axioms HeckeMult3.I3_neg
#print axioms HeckeMult3.hecke_mult3
#print axioms HeckeMult3.hecke_mult3_pow
#print axioms HeckeMult3.cw3

#print axioms: 9 theorems, no axioms.

78.230 Factorisation in \(\mathbb Z[\omega ]\) above a split prime

An element of prime norm is prime, and the primary elements of norm \(p^k\) are the \(\pi ^j\bar \pi ^l\).

\[ N(\alpha )=p^k,\ \alpha \equiv 1\ (3)\ \Rightarrow \ \alpha =\pi ^j\bar \pi ^{\,l},\ j+l=k. \]

lang/EisPrime3.lean

/-
  lang/EisPrime3 — T50, cycle 616: elements of prime norm are prime in ℤ[ω], and the factorisation above a split prime.

  π ∈ ℤ[ω] with N(π) = p prime:
    eprime_div    π | xy ⇒ π | x ∨ π | y                  (gcd(π, x) has norm 1 or p; EisEuclid.eeuclid)
    cj_mul, cj_cj conjugation is a ring involution
    factor3       N(α) = p^k ⇒ α = u π^j π̄^l, N(u) = 1, j + l = k      (p = ππ̄ | αᾱ, induction on k)
    primary_only3 π, α ≡ 1 (mod 3), N(α) = p^k ⇒ α = π^j π̄^l, j + l = k     (EisUnique3.unit_one3)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult3

namespace EisPrime3

open FTAChain DetChain LibInt EisEuclid EisPrim EisUnique3 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Cyclo3.cyc3 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Cyclo3.cyc3 LibInt.intR).mul u (cj u) = (Cyclo3.cyc3 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Cyclo3.cyc3 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists _ π x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Cyclo3.cyc3 LibInt.intR).mul (cj d) X) ((Cyclo3.cyc3 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cj d) X) π) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cj d) Y) x)
          = (Cyclo3.cyc3 LibInt.intR).mul (cj d) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul X π) ((Cyclo3.cyc3 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Cyclo3.cyc3 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Cyclo3.cyc3 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Cyclo3.cyc3 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Cyclo3.cyc3 LibInt.intR).mul d (Cyclo3.cyc3 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Cyclo3.cyc3 LibInt.intR).mul x y) = (Cyclo3.cyc3 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show (x.1 * y.1 + -(x.2 * y.2)) + -(x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) = (x.1 + -x.2) * (y.1 + -y.2) + -(-x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + -(x.2 * y.2)) = (x.1 + -x.2) * -y.2 + -x.2 * (y.1 + -y.2) + -(-x.2 * -y.2)
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show (z.1 + -z.2) + - -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Cyclo3.cyc3 LibInt.intR).mul u ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Cyclo3.cyc3 LibInt.intR).mul α ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).one (Cyclo3.cyc3 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Cyclo3.cyc3 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Cyclo3.cyc3 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Cyclo3.cyc3 LibInt.intR).mul π ((Cyclo3.cyc3 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Cyclo3.cyc3 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Cyclo3.cyc3 LibInt.intR).mul u ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Cyclo3.cyc3 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Cyclo3.cyc3 LibInt.intR).mul u ((Cyclo3.cyc3 LibInt.intR).mul _ ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim3_cj (z : Int × Int) (h : Prim3 z) : Prim3 (cj z) := by
  obtain ⟨e, f, h1, h2⟩ := h
  exact ⟨e + -f, -f, by show z.1 + -z.2 = _; rw [h1, h2]; lib_int, by show -z.2 = _; rw [h2]; lib_int⟩

theorem prim3_one : Prim3 (Cyclo3.cyc3 LibInt.intR).one := ⟨0, 0, rfl, rfl⟩

theorem prim3_pow (z : Int × Int) (h : Prim3 z) : ∀ j, Prim3 (pw (Cyclo3.cyc3 LibInt.intR) z j)
  | 0 => prim3_one
  | j + 1 => prim3_mul _ _ (prim3_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim3 α) :
    ∃ j l, j + l = k ∧ α = (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim3_mul _ _ (prim3_pow π pπ j) (prim3_pow (cj π) (prim3_cj π pπ) l)
  have u1 := unit_one3 u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end EisPrime3

#print axioms EisPrime3.nrm_mul
#print axioms EisPrime3.unit_inv
#print axioms EisPrime3.eprime_div
#print axioms EisPrime3.cj_mul
#print axioms EisPrime3.cj_cj
#print axioms EisPrime3.nrm_cj
#print axioms EisPrime3.nrm_one_eq
#print axioms EisPrime3.factor3
#print axioms EisPrime3.prim3_cj
#print axioms EisPrime3.prim3_one
#print axioms EisPrime3.prim3_pow
#print axioms EisPrime3.primary_only3

#print axioms: 12 theorems, no axioms.

78.231 Hecke characters of \(\mathbb Q(\sqrt {-3})\) at split prime powers

For every multiplicative \(W\) on \(\mathbb Z[\omega ]\) the coefficient at \(p^k\) is a sum over \(\pi ^j\bar \pi ^l\) and obeys the Hecke recursion.

\[ c_W(p^{k+2})=(W(\pi )+W(\bar \pi ))\,c_W(p^{k+1})-W(\pi )W(\bar \pi )\,c_W(p^k). \]

lang/HeckePrime3.lean

/-
  lang/HeckePrime3 — T50, cycle 617: **Hecke characters of ℚ(√−3): coefficients at split prime powers and the Hecke recursion**.
  p = P + 2 prime, π ∈ ℤ[ω] primary (≡ 1 mod 3) of norm p, W multiplicative on ℤ[ω] with W(1) = 1 (e.g. W(α) = α^k):
    no_assoc3      π ∤ π̄                                   (else π̄ = π, b = 0, p = a²)
    no_div3        π ∤ π̄^d
    distinct3      π^j π̄^l = π^j' π̄^l', j + l = j' + l' ⇒ (j, l) = (j', l')
    w3_PD          π^j π̄^l is primary of norm p^(j+l)
    cw3_pk         c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l)            (EisPrime3.primary_only3)
    cw3_pk'        c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l
    cw3_rec        c_W(p^(k+2)) = (W(π) + W(π̄)) c_W(p^(k+1)) − W(π)W(π̄) c_W(p^k)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.EisPrime3
import lang.HeckePrimeW

namespace HeckePrime3

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius EisEuclid EisPrim EisUnique3 EisPrime3 HeckeMult3

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Cyclo3.cyc3 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) A = (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) B) : A = B := by
  have h0 : (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) ((Cyclo3.cyc3 LibInt.intR).add A ((Cyclo3.cyc3 LibInt.intR).neg B)) = (Cyclo3.cyc3 LibInt.intR).zero := by
    rw [show (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) ((Cyclo3.cyc3 LibInt.intR).add A ((Cyclo3.cyc3 LibInt.intR).neg B)) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) A) ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := EisCoprime.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).add A ((Cyclo3.cyc3 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one3 c π (nrm_one_eq c nc) pπ (by rw [(Cyclo3.cyc3 LibInt.intR).mul_comm, ← hc]; exact prim3_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + -(a * b) + b * b; rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) :
    ∀ d, Dvd' π (pw (Cyclo3.cyc3 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (j d l' : Nat)
    (h : (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) (l' + 1 + d)) = (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π (j + 1 + d)) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Cyclo3.cyc3 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Cyclo3.cyc3 LibInt.intR) π j (1 + d)] at h
  have h' : (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l') (pw (Cyclo3.cyc3 LibInt.intR) (cj π) (1 + d)))
      = (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l') (pw (Cyclo3.cyc3 LibInt.intR) π (1 + d))) := by
    calc _ = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) (pw (Cyclo3.cyc3 LibInt.intR) π (1 + d))) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Cyclo3.cyc3 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l) = (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π j') (pw (Cyclo3.cyc3 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) π x.1) (pw (Cyclo3.cyc3 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD3 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim3_mul _ _ (prim3_pow π pπ _) (prim3_pow (cj π) (prim3_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (k : Nat) :
    cw3 W ((P + 1 + 1) ^ k) = sumK (Cyclo3.cyc3 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I3 ((P + 1 + 1) ^ k) z = sumK (Cyclo3.cyc3 LibInt.intR) (fun x => delta (Cyclo3.cyc3 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD3 ((P + 1 + 1) ^ k) z
    · rw [I3_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Cyclo3.cyc3 LibInt.intR) k _ (fun x => delta (Cyclo3.cyc3 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Cyclo3.cyc3 LibInt.intR) k j l hjl]
    · rw [I3_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Cyclo3.cyc3 LibInt.intR) k _ (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Cyclo3.cyc3 LibInt.intR) _).symm
  unfold cw3
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Cyclo3.cyc3 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le3 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Cyclo3.cyc3 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Cyclo3.cyc3 LibInt.intR) z j) = pw (Cyclo3.cyc3 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) z j) z) = (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (k : Nat) :
    cw3 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Cyclo3.cyc3 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (k : Nat) :
    cw3 W ((P + 1 + 1) ^ (k + 2)) = (Cyclo3.cyc3 LibInt.intR).add
      ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (W π) (W (cj π))) (cw3 W ((P + 1 + 1) ^ (k + 1))))
      ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (W π) (W (cj π))) (cw3 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime3

#print axioms HeckePrime3.pow_ne0
#print axioms HeckePrime3.nrm_pw
#print axioms HeckePrime3.npow_add
#print axioms HeckePrime3.en_ne
#print axioms HeckePrime3.cancel_pw3
#print axioms HeckePrime3.no_assoc3
#print axioms HeckePrime3.no_div3
#print axioms HeckePrime3.distinct_lt3
#print axioms HeckePrime3.distinct3
#print axioms HeckePrime3.w3_PD
#print axioms HeckePrime3.cw3_pk
#print axioms HeckePrime3.W_pow3
#print axioms HeckePrime3.cw3_pk'
#print axioms HeckePrime3.cw3_rec

#print axioms: 14 theorems, no axioms.

78.232 27a at every split prime

The Fermat cubic has \(p+1-c(p)\) points, \(c\) the theta series of the Hecke character \(\alpha \mapsto \alpha \) of \(\mathbb Q(\sqrt {-3})\).

\[ \#\{x^3+y^3=z^3\}(\mathbb F_p)=p+1-c(p),\qquad c(n)=\sum _{\alpha \equiv 1\,(3),\ N\alpha =n}\alpha . \]

lang/FermatHecke.lean

/-
  lang/FermatHecke — T47/T50, cycle 618: **27a (the Fermat cubic x³ + y³ = z³) at every prime p ≡ 1 (mod 3): a_p = c(p)**,
  where c(n) = Σ_{α ∈ ℤ[ω], α ≡ 1 (mod 3), N(α) = n} α is the theta series of the Hecke character α ↦ α of ℚ(√−3) (HeckeMult3).

  p = 6n + 7, χ the cubic character, J = J(χ, χ) (JacobiNorm: N(J) = p; JacobiPrimary: J ≡ −1 mod 3). π := −J is primary of norm p,
  so c(p) = π + π̄ (HeckePrime3.cw3_pk' at k = 1) = J̄-trace with sign: c(p) = −(2a − b) for J = a + bω. FermatCubic.fermat_hasse:
      #{x³ + y³ = 1}(𝔽_p) = p − 2 + (2a − b) = p − 2 − c(p),
  and with the three points at infinity, #{x³ + y³ = z³}(ℙ²(𝔽_p)) = p + 1 − c(p).
    neg_prim, tg_one, cp_eq, fermat_hecke
  With HeckeMult3 (c multiplicative) and HeckePrime3 (Hecke recursion at p^k) this gives the Hecke side of 27a at every split prime.
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime3
import lang.FermatCubic
import lang.CubicTwo

namespace FermatHecke

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CubicChar JacobiSum JacobiNorm EisEuclid EisPrim EisPrime3 HeckeMult3
open LibRing (castZ castN)

variable (n : Nat)

theorem tg_one {R : Type} (K : CRing R) (A B : R) : HeckePrime.Tg K A B 1 = K.add A B := by
  rw [HeckePrime.Tg_succ]
  show K.add (K.mul A (K.add (K.mul K.one K.one) K.zero)) (K.mul K.one B) = K.add A B
  lib_ring

/-- −J is primary of norm p. -/
theorem neg_prim (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1))
    (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    Prim3 (-(J n w).1, -(J n w).2) ∧ nrm (-(J n w).1, -(J n w).2) = 6 * n + 5 + 1 + 1 := by
  have hp := JacobiPrimary.jacobi_primary n hpr w hw1 hw3
  have h1 : castZ (ring 2) ((J n w).1 + 1) = (ring 2).zero := by
    rw [IntCast.castZ_add, hp.1, show castZ (ring 2) 1 = (ring 2).one from add_zero' (ring 2) (ring 2).one]; lib_ring
  obtain ⟨u, hu⟩ := CubicTwo.cast_zero_gen 2 _ h1
  obtain ⟨v, hv⟩ := CubicTwo.cast_zero_gen 2 _ hp.2
  refine ⟨⟨-u, -v, ?_, ?_⟩, ?_⟩
  · show -(J n w).1 = 3 * -u + 1
    have e : (J n w).1 + 1 = 3 * u := hu
    calc -(J n w).1 = -((J n w).1 + 1) + 1 := by lib_int
      _ = _ := by rw [e]; lib_int
  · show -(J n w).2 = 3 * -v
    have e : (J n w).2 = 3 * v := hv
    rw [e]; lib_int
  · apply Int.ofNat.inj
    rw [nrm_cast, ← jacobi_norm n hpr w hw1 hw3]
    show -(J n w).1 * -(J n w).1 + -(-(J n w).1 * -(J n w).2) + -(J n w).2 * -(J n w).2 = _
    lib_int

/-- **c(p) = −(2a − b)** for J = a + bω. -/
theorem cp_eq (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1))
    (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    cw3 (fun z => z) (6 * n + 5 + 1 + 1) = (-(2 * (J n w).1 - (J n w).2), 0) := by
  have np := neg_prim n hpr w hw1 hw3
  have h := HeckePrime3.cw3_pk' (6 * n + 5) (fun z => z) (fun _ _ => rfl) rfl hpr _ np.2 np.1 1
  rw [show (6 * n + 5 + 1 + 1) ^ 1 = 6 * n + 5 + 1 + 1 from Nat.one_mul _, tg_one] at h
  rw [h]
  apply Prod.ext
  · show -(J n w).1 + (-(J n w).1 + -(-(J n w).2)) = -(2 * (J n w).1 - (J n w).2); lib_int
  · show -(J n w).2 + -(-(J n w).2) = 0; lib_int

/-- **#{x³ + y³ = 1}(𝔽_p) = p − 2 − c(p)**, i.e. the projective Fermat cubic (27a) has p + 1 − c(p) points. -/
theorem fermat_hecke (hpr : ∀ d, Dv d (6 * n + 5 + 1 + 1) → d = 1 ∨ d = 6 * n + 5 + 1 + 1) (w : Zp (6 * n + 5 + 1))
    (hw1 : w ≠ (ring (6 * n + 5 + 1)).one) (hw3 : pw (ring (6 * n + 5 + 1)) w 3 = (ring (6 * n + 5 + 1)).one) :
    (FermatCubic.Fc n).1 + 3 = Int.ofNat (6 * n + 5 + 1 + 1) + 1 - (cw3 (fun z => z) (6 * n + 5 + 1 + 1)).1 ∧
      (cw3 (fun z => z) (6 * n + 5 + 1 + 1)).2 = 0 := by
  rw [cp_eq n hpr w hw1 hw3]
  refine ⟨?_, rfl⟩
  rw [(FermatCubic.fermat_hasse n hpr w hw1 hw3).1]
  show _ = Int.ofNat (6 * n + 5 + 1 + 1) + 1 - -(2 * (J n w).1 - (J n w).2)
  lib_int

end FermatHecke

#print axioms FermatHecke.tg_one
#print axioms FermatHecke.neg_prim
#print axioms FermatHecke.cp_eq
#print axioms FermatHecke.fermat_hecke

#print axioms: 4 theorems, no axioms.

78.233 27a at the inert primes

No element of \(\mathbb Z[\omega ]\) has norm \(\equiv 2\) mod \(3\), so \(c(p)=0\), and the Fermat cubic has \(p+1\) points.

\[ p\equiv 2\ (3):\quad \#\{x^3+y^3=z^3\}(\mathbb F_p)=p+1=p+1-c(p). \]

lang/FermatInert3.lean

/-
  lang/FermatInert3 — T47/T50, cycle 619: **27a at the primes p ≡ 2 (mod 3)**: a_p = 0 = c(p).

    en_mod3        N(a + bω) = a² − ab + b² is never ≡ 2 (mod 3)                      (residues of a, b mod 3)
    cw3_zero       c_W(n) = 0 for every n ≡ 2 (mod 3) and every weight W               (no α of norm n)
    ones_allZ      Σ_{x ∈ 𝔽_p} 1 = p
    fermat_inert   p = 3t + 2 prime: #{x³ + y³ = 1}(𝔽_p) = p        (x ↦ x³ is a bijection, CMEisen.cube_inv/inv_cube)
    fermat27_inert #{x³ + y³ = z³}(ℙ²(𝔽_p)) = p + 1 = p + 1 − c(p)    (one point at infinity: x = −y)
  With FermatHecke (p ≡ 1 mod 3) this gives a_p(27a) = c(p) at every prime p ≠ 3.
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.FermatHecke
import lang.CMEisen

namespace FermatInert3

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius EisEuclid EisPrim HeckeMult3

/-- N(z) ≢ 2 (mod 3). -/
theorem en_mod3 (z : Int × Int) (k : Int) (h : en z = 3 * k + 2) : False := by
  obtain ⟨a, b⟩ := z
  have hn : a * a + -(a * b) + b * b = 3 * k + 2 := h
  obtain ⟨e, he⟩ := res3 a
  obtain ⟨f, hf⟩ := res3 b
  have r0 : ∀ K : Int, a * a + -(a * b) + b * b = 3 * K → False := fun K hK =>
    (no3 (K + -k)).2 (by
      calc 3 * (K + -k) = 3 * K + -(3 * k + 2) + 2 := by lib_int
        _ = 2 := by rw [← hK, ← hn]; lib_int)
  have r1 : ∀ K : Int, a * a + -(a * b) + b * b = 3 * K + 1 → False := fun K hK =>
    (no3 (K + -k)).1 (by
      calc 3 * (K + -k) = (3 * K + 1) + -(3 * k + 2) + 1 := by lib_int
        _ = 1 := by rw [← hK, ← hn]; lib_int)
  rcases he with ha | ha | ha <;> rcases hf with hb | hb | hb
  · exact r0 (3 * (e * e + -(e * f) + f * f)) (by rw [ha, hb]; lib_int)
  · exact r1 (3 * (e * e + -(e * f) + f * f) + -e + 2 * f) (by rw [ha, hb]; lib_int)
  · exact r1 (3 * (e * e + -(e * f) + f * f) + e + -(2 * f)) (by rw [ha, hb]; lib_int)
  · exact r1 (3 * (e * e + -(e * f) + f * f) + 2 * e + -f) (by rw [ha, hb]; lib_int)
  · exact r1 (3 * (e * e + -(e * f) + f * f) + e + f) (by rw [ha, hb]; lib_int)
  · exact r0 (3 * (e * e + -(e * f) + f * f) + 3 * e + -(3 * f) + 1) (by rw [ha, hb]; lib_int)
  · exact r1 (3 * (e * e + -(e * f) + f * f) + -(2 * e) + f) (by rw [ha, hb]; lib_int)
  · exact r0 (3 * (e * e + -(e * f) + f * f) + -(3 * e) + 3 * f + 1) (by rw [ha, hb]; lib_int)
  · exact r1 (3 * (e * e + -(e * f) + f * f) + -e + -f) (by rw [ha, hb]; lib_int)

/-- **c_W(n) = 0 for n ≡ 2 (mod 3).** -/
theorem cw3_zero (W : Int × Int → Int × Int) (n : Nat) (k : Int) (hn : Int.ofNat n = 3 * k + 2) : cw3 W n = (Cyclo3.cyc3 LibInt.intR).zero := by
  unfold cw3
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) (fun z => by
    rw [I3_neg _ _ (fun h => en_mod3 z k (h.1.trans hn))]; lib_ring)]
  exact zsum3 _

variable (t : Nat)

theorem ones_allZ (m : Nat) : ∀ k, sumK LibInt.intR (fun _ => (1 : Int)) (CMInert.allZ m k) = Int.ofNat k
  | 0 => rfl
  | k + 1 => by
    show sumK LibInt.intR (fun _ => (1 : Int)) (CMInert.allZ m k ++ [mk m k]) = _
    rw [sumK_append, ones_allZ m k]
    show Int.ofNat k + (1 + 0) = Int.ofNat k + 1; lib_int

/-- **#{x³ + y³ = 1}(𝔽_p) = p for p = 3t + 2.** -/
theorem fermat_inert (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (3 * t + 1)).add (pw (ring (3 * t + 1)) x 3) (pw (ring (3 * t + 1)) y 3)) (ring (3 * t + 1)).one)
      (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1))) (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1)) = Int.ofNat (3 * t + 1 + 1) := by
  have he : 3 * (2 * t + 1) = 2 * (3 * t + 1) + 1 := by lib_nat
  have cube : ∀ G : Zp (3 * t + 1) → Int, sumK LibInt.intR (fun x => G (pw (ring (3 * t + 1)) x 3)) (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1)) =
      sumK LibInt.intR G (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1)) := fun G =>
    reindex LibInt.intR _ (CMInert.complete_allZ (3 * t + 1) LibInt.intR) (fun x => pw (ring (3 * t + 1)) x 3) (fun x => pw (ring (3 * t + 1)) x (2 * t + 1))
      (CMEisen.cube_inv (3 * t) hpr _ he) (CMEisen.inv_cube (3 * t) hpr _ he) G
  rw [sumK_ext LibInt.intR _ _ (fun x => cube (fun y => delta LibInt.intR ((ring (3 * t + 1)).add (pw (ring (3 * t + 1)) x 3) y) (ring (3 * t + 1)).one))]
  rw [cube (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (3 * t + 1)).add x y) (ring (3 * t + 1)).one) (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1)))]
  rw [sumK_ext LibInt.intR _ (fun _ => (1 : Int)) (fun x => by
    rw [sumK_ext LibInt.intR _ (fun y => delta LibInt.intR y ((ring (3 * t + 1)).add (ring (3 * t + 1)).one ((ring (3 * t + 1)).neg x))) (fun y => by
      unfold delta
      by_cases hy : (ring (3 * t + 1)).add x y = (ring (3 * t + 1)).one
      · rw [ite_pos' _ _ hy, ite_pos' _ _ (by rw [← hy]; lib_ring)]
      · rw [ite_neg' _ _ hy, ite_neg' _ _ (fun e => hy (by rw [e]; lib_ring))])]
    exact CMInert.complete_allZ (3 * t + 1) LibInt.intR _)]
  exact ones_allZ (3 * t + 1) _

/-- **27a at p ≡ 2 (mod 3)**: #E(𝔽_p) = (affine count) + 1 = p + 1 − c(p), c(p) = 0. -/
theorem fermat27_inert (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) :
    sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => delta LibInt.intR ((ring (3 * t + 1)).add (pw (ring (3 * t + 1)) x 3) (pw (ring (3 * t + 1)) y 3)) (ring (3 * t + 1)).one)
      (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1))) (CMInert.allZ (3 * t + 1) (3 * t + 1 + 1)) + 1 =
      Int.ofNat (3 * t + 1 + 1) + 1 - (cw3 (fun z => z) (3 * t + 1 + 1)).1 := by
  rw [fermat_inert t hpr, cw3_zero _ _ (Int.ofNat t) rfl]
  show _ = Int.ofNat (3 * t + 1 + 1) + 1 - 0; lib_int

end FermatInert3

#print axioms FermatInert3.en_mod3
#print axioms FermatInert3.cw3_zero
#print axioms FermatInert3.ones_allZ
#print axioms FermatInert3.fermat_inert
#print axioms FermatInert3.fermat27_inert

#print axioms: 5 theorems, no axioms.

78.234 Hecke characters of \(\mathbb Q(\sqrt {-3})\) at inert primes

A prime \(p\equiv 2\) mod \(3\) stays prime in \(\mathbb Z[\omega ]\); the only primary element of norm \(p^{2k}\) is \((-p)^k\).

\[ c_W(p^{2k})=W((-p)^k),\qquad c_W(p^{2k+1})=0. \]

lang/EisInert3.lean

/-
  lang/EisInert3 — T50, cycle 620: **Hecke characters of ℚ(√−3) at inert primes**. p = 3t + 2 prime:
    inert_div      p | xy in ℤ[ω] ⇒ p | x ∨ p | y          (gcd(p, x) has norm 1, p or p²; norm p ≡ 2 mod 3 is impossible, en_mod3)
    pow_odd        p^(2k+1) ≡ 2 (mod 3)
    factor_inert   N(α) = p^(2k) ⇒ α = u p^k, N(u) = 1
    prim_inert     α ≡ 1 (mod 3), N(α) = p^(2k) ⇒ α = (−p)^k
    cw3_even       c_W(p^(2k)) = W((−p)^k)
    cw3_odd        c_W(p^(2k+1)) = 0
  (for W(α) = α: c(p²) = −p, so the p-part of L(c, s) is 1/(1 + p^(2−2s)) = 1/(1 − a_p p^(−s) + p^(1−2s)) with a_p = 0.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.FermatInert3

namespace EisInert3

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius EisEuclid EisPrim EisUnique3 EisPrime3 HeckeMult3

variable (t : Nat)

def Pi : Int × Int := (Int.ofNat (3 * t + 1 + 1), 0)

theorem en_Pi : en (Pi t) = Int.ofNat ((3 * t + 1 + 1) * (3 * t + 1 + 1)) := by
  show Int.ofNat (3 * t + 1 + 1) * Int.ofNat (3 * t + 1 + 1) + -(Int.ofNat (3 * t + 1 + 1) * 0) + 0 * 0 = Int.ofNat (3 * t + 1 + 1) * Int.ofNat (3 * t + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi t) = (3 * t + 1 + 1) * (3 * t + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi t) = Pi t := Prod.ext (by show Int.ofNat (3 * t + 1 + 1) + -0 = Int.ofNat (3 * t + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (z : Int × Int) (h : nrm z = 3 * t + 1 + 1) : False :=
  FermatInert3.en_mod3 z (Int.ofNat t) (by rw [← nrm_cast, h]; rfl)

theorem dvd_conv (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) : ∀ d, d ∣ (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) (a b : Nat) (h : a * b = (3 * t + 1 + 1) * (3 * t + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = 3 * t + 1 + 1 := by
  cases euclid_prime (dvd_conv t hpr) (show (3 * t + 1 + 1) ∣ a * b from ⟨3 * t + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = 3 * t + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (3 * t + 1)) (by rw [show (3 * t + 1 + 1) * (s * b) = (3 * t + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (3 * t + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = 3 * t + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (3 * t + 1)) (by
      rw [show (3 * t + 1 + 1) * (a * s) = a * ((3 * t + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) (x y : Int × Int)
    (h : Dvd' (Pi t) ((Cyclo3.cyc3 LibInt.intR).mul x y)) : Dvd' (Pi t) x ∨ Dvd' (Pi t) y := by
  match egcd_exists _ (Pi t) x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (3 * t + 1 + 1) * (3 * t + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq t hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi t) x y ((Cyclo3.cyc3 LibInt.intR).mul (cj d) X) ((Cyclo3.cyc3 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cj d) X) (Pi t)) ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cj d) Y) x)
          = (Cyclo3.cyc3 LibInt.intR).mul (cj d) ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul X (Pi t)) ((Cyclo3.cyc3 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Cyclo3.cyc3 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Cyclo3.cyc3 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Cyclo3.cyc3 LibInt.intR).mul (Pi t) (cj c1) := by
        rw [hc1]
        calc d = (Cyclo3.cyc3 LibInt.intR).mul d (Cyclo3.cyc3 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p t d h1).elim

theorem nat_pow2 (k : Nat) : (3 * t + 1 + 1) ^ (2 * (k + 1)) = (3 * t + 1 + 1) * (3 * t + 1 + 1) * (3 * t + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (3 * t + 1 + 1) ^ (2 * k) * (3 * t + 1 + 1) * (3 * t + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (3 * t + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Cyclo3.cyc3 LibInt.intR).mul u (pw (Cyclo3.cyc3 LibInt.intR) (Pi t) k)
  | 0, α, h => ⟨α, h, by show α = (Cyclo3.cyc3 LibInt.intR).mul α (Cyclo3.cyc3 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi t) ((Cyclo3.cyc3 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((3 * t + 1 + 1) * (3 * t + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((3 * t + 1 + 1) * (3 * t + 1 + 1) * (3 * t + 1 + 1) ^ (2 * k)) = Int.ofNat (3 * t + 1 + 1) * Int.ofNat ((3 * t + 1 + 1) * (3 * t + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (3 * t + 1 + 1) * Int.ofNat (3 * t + 1 + 1) * Int.ofNat ((3 * t + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (3 * t + 1 + 1) * (Int.ofNat (3 * t + 1 + 1) * Int.ofNat ((3 * t + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (3 * t + 1 + 1) * 0 + 0 * Int.ofNat ((3 * t + 1 + 1) * (3 * t + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Cyclo3.cyc3 LibInt.intR).mul (Pi t) β) = (3 * t + 1 + 1) ^ (2 * (k + 1)) → nrm β = (3 * t + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Cyclo3.cyc3 LibInt.intR).mul (Pi t) β := by
      cases inert_div t hpr α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi t, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Cyclo3.cyc3 LibInt.intR).mul u ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) (Pi t) k) (Pi t)); lib_ring

def nPi : Int × Int := (-Int.ofNat (3 * t + 1 + 1), 0)

theorem prim_nPi : Prim3 (nPi t) := ⟨-Int.ofNat t + -1, 0, by show -Int.ofNat (3 * t + 1 + 1) = _; show -(3 * Int.ofNat t + 1 + 1) = _; lib_int, rfl⟩

theorem pw_neg (k : Nat) : pw (Cyclo3.cyc3 LibInt.intR) (Pi t) k =
    (Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) k) (pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (3 * t + 1 + 1) = -1 * -Int.ofNat (3 * t + 1 + 1) + -(-0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (3 * t + 1 + 1) + -(-0 * 0); lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Cyclo3.cyc3 LibInt.intR) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Cyclo3.cyc3 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) (k : Nat) (α : Int × Int)
    (hα : nrm α = (3 * t + 1 + 1) ^ (2 * k)) (pα : Prim3 α) : α = pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k := by
  obtain ⟨u, hu, he⟩ := factor_inert t hpr k α hα
  rw [pw_neg] at he
  generalize hv : (Cyclo3.cyc3 LibInt.intR).mul u (pw (Cyclo3.cyc3 LibInt.intR) ((Cyclo3.cyc3 LibInt.intR).neg (Cyclo3.cyc3 LibInt.intR).one) k) = v
  have he' : α = (Cyclo3.cyc3 LibInt.intR).mul v (pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one3 v _ (nrm_one_eq v hv1) (prim3_pow _ (prim_nPi t) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi t) = (3 * t + 1 + 1) * (3 * t + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (3 * t + 1 + 1) * -Int.ofNat (3 * t + 1 + 1) + -(-Int.ofNat (3 * t + 1 + 1) * 0) + 0 * 0 = Int.ofNat (3 * t + 1 + 1) * Int.ofNat (3 * t + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k) = (3 * t + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime3.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((3 * t + 1 + 1) * (3 * t + 1 + 1)) ^ k * ((3 * t + 1 + 1) * (3 * t + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw3_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) (k : Nat) :
    cw3 W ((3 * t + 1 + 1) ^ (2 * k)) = W (pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k) := by
  have hPD : PD3 ((3 * t + 1 + 1) ^ (2 * k)) (pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim3_pow _ (prim_nPi t) k⟩
  unfold cw3
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun z => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) z (pw (Cyclo3.cyc3 LibInt.intR) (nPi t) k)) (W z)) (fun z => by
    by_cases hz : PD3 ((3 * t + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert t hpr k z (by unfold nrm; rw [hz.1]; rfl) hz.2
      rw [I3_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I3_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le3 _ _ hPD.1
  exact HeckeMult.pick_box (Cyclo3.cyc3 LibInt.intR) W _ _ hb.1 hb.2

theorem pow_odd : ∀ k, ∃ K : Int, Int.ofNat ((3 * t + 1 + 1) ^ (2 * k + 1)) = 3 * K + 2
  | 0 => ⟨Int.ofNat t, by show Int.ofNat (1 * (3 * t + 1 + 1)) = _; rw [Nat.one_mul]; show 3 * Int.ofNat t + 1 + 1 = _; lib_int⟩
  | k + 1 => by
    obtain ⟨K, hK⟩ := pow_odd k
    refine ⟨3 * K * (3 * Int.ofNat t * Int.ofNat t + 4 * Int.ofNat t + 1) + K + 2 * (3 * Int.ofNat t * Int.ofNat t + 4 * Int.ofNat t + 1), ?_⟩
    rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]
    show Int.ofNat ((3 * t + 1 + 1) ^ (2 * k + 1)) * Int.ofNat (3 * t + 1 + 1) * Int.ofNat (3 * t + 1 + 1) = _
    rw [hK]; show (3 * K + 2) * (3 * Int.ofNat t + 1 + 1) * (3 * Int.ofNat t + 1 + 1) = _; lib_int

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw3_odd (W : Int × Int → Int × Int) (k : Nat) : cw3 W ((3 * t + 1 + 1) ^ (2 * k + 1)) = (Cyclo3.cyc3 LibInt.intR).zero := by
  obtain ⟨K, hK⟩ := pow_odd t k
  exact FermatInert3.cw3_zero W _ K hK

end EisInert3

#print axioms EisInert3.en_Pi
#print axioms EisInert3.nrm_Pi
#print axioms EisInert3.cj_Pi
#print axioms EisInert3.not_p
#print axioms EisInert3.dvd_conv
#print axioms EisInert3.split_sq
#print axioms EisInert3.inert_div
#print axioms EisInert3.nat_pow2
#print axioms EisInert3.factor_inert
#print axioms EisInert3.prim_nPi
#print axioms EisInert3.pw_neg
#print axioms EisInert3.nrm_neg_one_pw
#print axioms EisInert3.prim_inert
#print axioms EisInert3.nrm_nPi
#print axioms EisInert3.nrm_nPi_pw
#print axioms EisInert3.cw3_even
#print axioms EisInert3.pow_odd
#print axioms EisInert3.cw3_odd

#print axioms: 18 theorems, no axioms.

78.235 The Hecke character of 36a

Every \(\alpha \) with \((N\alpha ,6)=1\) has exactly one associate \(x+y\sqrt {-3}\) with \(x\equiv 1\) mod \(3\), \(x+y\) odd; this choice is multiplicative.

\[ W_{36}(\alpha \beta )=W_{36}(\alpha )\,W_{36}(\beta ). \]

lang/W36Mul.lean

/-
  lang/W36Mul — T47/T50, cycle 621: **the Hecke character of 36a as a multiplicative weight on ℤ[ω]**.
  S = {x + y√−3 : x ≡ 1 (mod 3), x + y odd} ⊂ ℤ[√−3] ⊂ ℤ[ω]; in the basis 1, ω this is z = (x + y, 2y):
    InS z :⇔ 2 | z.2, 2 | z.1 − 1, 3 | 2z.1 − z.2 − 2.
    unit_list     N(u) = 1 ⇒ u ∈ {±1, ±ω, ±ω²}
    ins_mul       S is closed under products
    ins_unique    γ, wγ ∈ S, N(w) = 1 ⇒ w = 1                     (parity for ±ω, ±ω²; mod 3 for −1)
    ins_exists    N(α) odd and prime to 3 ⇒ uα ∈ S for a unit u     (normalize3, then ω^j by parity)
    ins_norm      z ∈ S ⇒ N(z) odd and prime to 3
    W36           the S-associate (0 if none);  w36_mul   W36(αβ) = W36(α)W36(β) for all α, β
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.EisInert3

namespace W36Mul

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius EisEuclid EisPrim EisUnique3 EisPrime3 HeckeMult3

theorem two_cancel_int (a c : Int) (h : 2 * a = 2 * c) : a = c := by
  have h0 : 2 * (a + -c) = 0 := by rw [show 2 * (a + -c) = 2 * a + -(2 * c) by lib_int, h]; lib_int
  cases int_noZD _ _ h0 with
  | inl e => exact absurd e (fun e => Nat.noConfusion (Int.ofNat.inj e))
  | inr e => calc a = (a + -c) + c := by lib_int
      _ = c := by rw [e]; show (0 : Int) + c = c; lib_int

theorem abs_one (a : Int) (h : Int.natAbs a = 1) : a = 1 ∨ a = -1 := by
  cases EisEuclid.sgn a with
  | inl e => exact Or.inl (by rw [e, h]; rfl)
  | inr e => exact Or.inr (by rw [e, h]; rfl)

theorem nat_sq_le1 (B : Nat) (h : 3 * (B * B) ≤ 4) : B = 0 ∨ B = 1 := by
  match B with
  | 0 => exact Or.inl rfl
  | 1 => exact Or.inr rfl
  | B + 2 =>
    exfalso
    have : 12 ≤ 3 * ((B + 2) * (B + 2)) := by
      rw [show 3 * ((B + 2) * (B + 2)) = 12 + (3 * (B * B) + 12 * B) by lib_nat]; exact Nat.le_add_right _ _
    exact Nat.not_succ_le_zero 7 (Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ (Nat.le_trans this h)))))

theorem nat_sq_one (A : Nat) (h : A * A = 1) : A = 1 := by
  match A with
  | 0 => exact Nat.noConfusion h
  | 1 => rfl
  | A + 2 => exfalso; rw [show (A + 2) * (A + 2) = (A * A + 4 * A + 3) + 1 by lib_nat] at h; exact Nat.noConfusion (Nat.succ.inj h)

/-- **The six units.** -/
theorem unit_list (w : Int × Int) (h : en w = 1) :
    w = (1, 0) ∨ w = (-1, 0) ∨ w = (0, 1) ∨ w = (0, -1) ∨ w = (1, 1) ∨ w = (-1, -1) := by
  obtain ⟨a, b⟩ := w
  have h' : a * a + -(a * b) + b * b = 1 := h
  have h4 : Int.ofNat (Int.natAbs (2 * a + -b) * Int.natAbs (2 * a + -b) + 3 * (Int.natAbs b * Int.natAbs b)) = Int.ofNat 4 := by
    show Int.ofNat (Int.natAbs (2 * a + -b) * Int.natAbs (2 * a + -b)) + 3 * Int.ofNat (Int.natAbs b * Int.natAbs b) = 4
    rw [GaussEuclid.nas, GaussEuclid.nas]
    calc _ = 4 * (a * a + -(a * b) + b * b) := by lib_int
      _ = 4 := by rw [h']; rfl
  have h4' := Int.ofNat.inj h4
  have hB := nat_sq_le1 _ (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4'))
  cases hB with
  | inl b0 =>
    have bz : b = 0 := GaussEuclid.natAbs_zero b b0
    rw [bz] at h'
    have hA : Int.natAbs a * Int.natAbs a = 1 := Int.ofNat.inj (by rw [GaussEuclid.nas]; show a * a = 1; rw [← h']; lib_int)
    rcases abs_one a (nat_sq_one _ hA) with e | e
    · exact Or.inl (by rw [e, bz])
    · exact Or.inr (Or.inl (by rw [e, bz]))
  | inr b1 =>
    rw [b1] at h4'
    have hT : Int.natAbs (2 * a + -b) * Int.natAbs (2 * a + -b) = 1 := by
      have : Int.natAbs (2 * a + -b) * Int.natAbs (2 * a + -b) + 3 = 1 + 3 := h4'
      exact Nat.succ.inj (Nat.succ.inj (Nat.succ.inj this))
    rcases abs_one b b1 with eb | eb <;> rcases abs_one _ (nat_sq_one _ hT) with et | et
    · -- b = 1, 2a − 1 = 1
      exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [eb, two_cancel_int a 1 (by rw [eb] at et; calc 2 * a = (2 * a + -1) + 1 := by lib_int
                                                                                          _ = 2 * 1 := by rw [et]; rfl)])))))
    · -- b = 1, 2a − 1 = −1: a = 0
      exact Or.inr (Or.inr (Or.inl (by rw [eb, two_cancel_int a 0 (by rw [eb] at et; calc 2 * a = (2 * a + -1) + 1 := by lib_int
                                                                              _ = 2 * 0 := by rw [et]; rfl)])))
    · -- b = −1, 2a + 1 = 1: a = 0
      exact Or.inr (Or.inr (Or.inr (Or.inl (by rw [eb, two_cancel_int a 0 (by rw [eb] at et; calc 2 * a = (2 * a + -(-1)) + -1 := by lib_int
                                                                                   _ = 2 * 0 := by rw [et]; rfl)]))))
    · -- b = −1, 2a + 1 = −1: a = −1
      exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [eb, two_cancel_int a (-1) (by rw [eb] at et; calc 2 * a = (2 * a + -(-1)) + -1 := by lib_int
                                                                                            _ = 2 * -1 := by rw [et]; rfl)])))))

def InS (z : Int × Int) : Prop := IDv 2 z.2 ∧ IDv 2 (z.1 - 1) ∧ IDv 3 (2 * z.1 - z.2 - 2)

theorem ins_iff (z : Int × Int) :
    (ZMod.md 1 (Int.natAbs z.2) = 0 ∧ ZMod.md 1 (Int.natAbs (z.1 - 1)) = 0 ∧ ZMod.md 2 (Int.natAbs (2 * z.1 - z.2 - 2)) = 0) ↔ InS z :=
  ⟨fun ⟨h1, h2, h3⟩ => ⟨(HeckeMult.idv_iff 1 _).mpr h1, (HeckeMult.idv_iff 1 _).mpr h2, (HeckeMult.idv_iff 2 _).mpr h3⟩,
   fun ⟨h1, h2, h3⟩ => ⟨(HeckeMult.idv_iff 1 _).mp h1, (HeckeMult.idv_iff 1 _).mp h2, (HeckeMult.idv_iff 2 _).mp h3⟩⟩

instance (z : Int × Int) : Decidable (InS z) := decidable_of_iff _ (ins_iff z)

theorem mul1 (u z : Int × Int) : ((Cyclo3.cyc3 LibInt.intR).mul u z).1 = u.1 * z.1 + -(u.2 * z.2) := rfl
theorem mul2 (u z : Int × Int) : ((Cyclo3.cyc3 LibInt.intR).mul u z).2 = u.1 * z.2 + u.2 * z.1 + -(u.2 * z.2) := rfl

/-- **S is closed under products.** -/
theorem ins_mul (z w : Int × Int) (hz : InS z) (hw : InS w) : InS ((Cyclo3.cyc3 LibInt.intR).mul z w) := by
  obtain ⟨a, b⟩ := z
  obtain ⟨c, d⟩ := w
  obtain ⟨⟨s, hs⟩, ⟨r, hr⟩, ⟨k, hk⟩⟩ := hz
  obtain ⟨⟨s', hs'⟩, ⟨r', hr'⟩, ⟨k', hk'⟩⟩ := hw
  have hs0 : b = 2 * s := hs
  have hs0' : d = 2 * s' := hs'
  have hr0 : a = 2 * r + 1 := by calc a = (a - 1) + 1 := by lib_int
    _ = 2 * r + 1 := by rw [show a - 1 = 2 * r from hr]
  have hr0' : c = 2 * r' + 1 := by calc c = (c - 1) + 1 := by lib_int
    _ = 2 * r' + 1 := by rw [show c - 1 = 2 * r' from hr']
  have hk0 : 2 * a - b - 2 = 3 * k := hk
  have hk0' : 2 * c - d - 2 = 3 * k' := hk'
  refine ⟨⟨(2 * r + 1) * s' + s * (2 * r' + 1) + -(2 * s * s'), ?_⟩, ⟨2 * r * r' + r + r' + -(2 * s * s'), ?_⟩, ⟨k * (c - s') + k' + -(2 * s * s'), ?_⟩⟩
  · rw [mul2]; show a * d + b * c + -(b * d) = _; rw [hr0, hs0, hr0', hs0']; lib_int
  · rw [mul1]; show a * c + -(b * d) - 1 = _; rw [hr0, hs0, hr0', hs0']; lib_int
  · rw [mul1, mul2]; show 2 * (a * c + -(b * d)) - (a * d + b * c + -(b * d)) - 2 = _
    calc 2 * (a * c + -(b * d)) - (a * d + b * c + -(b * d)) - 2
        = (2 * a - b - 2) * (c - s') + (2 * c - d - 2) + -(6 * s * s') + (d - 2 * s') * (1 - a - b) + (b - 2 * s) * -(3 * s') := by lib_int
      _ = 3 * k * (c - s') + 3 * k' + -(6 * s * s') := by rw [hk0, hk0', hs0, hs0']; lib_int
      _ = _ := by lib_int

theorem no3m (x : Int) (r : Int) (h : 3 * x = r) (hr : r = 1 ∨ r = 2 ∨ r = -1 ∨ r = -2 ∨ r = 4 ∨ r = -4 ∨ r = 8) : False := by
  rcases hr with e | e | e | e | e | e | e
  · exact (no3 x).1 (h.trans e)
  · exact (no3 x).2 (h.trans e)
  · exact (no3 (-x)).1 (by rw [show 3 * -x = -(3 * x) by lib_int, h, e]; rfl)
  · exact (no3 (-x)).2 (by rw [show 3 * -x = -(3 * x) by lib_int, h, e]; rfl)
  · exact (no3 (x + -1)).1 (by rw [show 3 * (x + -1) = 3 * x + -3 by lib_int, h, e]; rfl)
  · exact (no3 (-x + -1)).1 (by rw [show 3 * (-x + -1) = -(3 * x) + -3 by lib_int, h, e]; rfl)
  · exact (no3 (x + -2)).2 (by rw [show 3 * (x + -2) = 3 * x + -6 by lib_int, h, e]; rfl)

/-- **Uniqueness of the S-associate.** -/
theorem ins_unique (γ w : Int × Int) (hγ : InS γ) (hw : en w = 1) (h : InS ((Cyclo3.cyc3 LibInt.intR).mul w γ)) : w = (Cyclo3.cyc3 LibInt.intR).one := by
  obtain ⟨a, b⟩ := γ
  obtain ⟨⟨s, hs⟩, ⟨r, hr⟩, ⟨k, hk⟩⟩ := hγ
  have hs0 : b = 2 * s := hs
  have hr0 : a = 2 * r + 1 := by calc a = (a - 1) + 1 := by lib_int
    _ = 2 * r + 1 := by rw [show a - 1 = 2 * r from hr]
  have hk0 : 2 * a - b - 2 = 3 * k := hk
  obtain ⟨⟨s2, hs2⟩, ⟨r2, hr2⟩, ⟨k2, hk2⟩⟩ := h
  rw [mul2] at hs2
  rw [mul1] at hr2
  rw [mul1, mul2] at hk2
  rcases unit_list w hw with e | e | e | e | e | e <;> rw [e] at hs2 hr2 hk2
  · rw [e]; rfl
  · -- −1: mod 3
    exfalso
    have h2 : -1 * (2 * a - b - 2) + -4 = 3 * k2 := by rw [← hk2]; show _ = 2 * (-1 * a + -(0 * b)) - (-1 * b + 0 * a + -(0 * b)) - 2; lib_int
    rw [hk0] at h2
    exact no3m (k2 + k) (-4) (by rw [show 3 * (k2 + k) = 3 * k2 + 3 * k by lib_int, ← h2]; lib_int) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))))
  · -- ω: second coordinate a − b
    exfalso
    exact GaussInt.int_odd_ne_even (r + -s) s2 (by rw [← hs2]; show _ = 0 * b + 1 * a + -(1 * b); rw [hr0, hs0]; lib_int)
  · -- −ω: −a + b
    exfalso
    exact GaussInt.int_odd_ne_even (r + -s) (-s2) (by rw [show 2 * -s2 = -(2 * s2) by lib_int, ← hs2]; show _ = -(0 * b + -1 * a + -(-1 * b)); rw [hr0, hs0]; lib_int)
  · -- −ω²: a
    exfalso
    exact GaussInt.int_odd_ne_even r s2 (by rw [← hs2]; show _ = 1 * b + 1 * a + -(1 * b); rw [hr0]; lib_int)
  · -- ω²: −a
    exfalso
    exact GaussInt.int_odd_ne_even r (-s2) (by rw [show 2 * -s2 = -(2 * s2) by lib_int, ← hs2]; show _ = -(-1 * b + -1 * a + -(-1 * b)); rw [hr0]; lib_int)

theorem ins_norm (z : Int × Int) (hz : InS z) : (∃ k, en z = 2 * k + 1) ∧ C3 (en z) := by
  obtain ⟨a, b⟩ := z
  obtain ⟨⟨s, hs⟩, ⟨r, hr⟩, ⟨k, hk⟩⟩ := hz
  have hs0 : b = 2 * s := hs
  have hr0 : a = 2 * r + 1 := by calc a = (a - 1) + 1 := by lib_int
    _ = 2 * r + 1 := by rw [show a - 1 = 2 * r from hr]
  have hk0 : 2 * a - b - 2 = 3 * k := hk
  have hen : en (a, b) = a * a + -(a * b) + b * b := rfl
  refine ⟨⟨2 * r * r + 2 * r + -(2 * r * s) + -s + 2 * s * s, by rw [hen, hr0, hs0]; lib_int⟩, ?_⟩
  have h4 : 4 * en (a, b) = (3 * k + 2) * (3 * k + 2) + 3 * (b * b) := by
    rw [hen, ← show 2 * a - b - 2 + 2 = 3 * k + 2 by rw [hk0]]; lib_int
  obtain ⟨q, hq | hq | hq⟩ := res3 (en (a, b))
  · exfalso; rw [hq] at h4
    exact no3m (4 * q + -(3 * k * k) + -(4 * k) + -(b * b)) 4 (by rw [show 3 * (4 * q + -(3 * k * k) + -(4 * k) + -(b * b)) = 4 * (3 * q) + -((3 * k + 2) * (3 * k + 2) + 3 * (b * b)) + 4 by lib_int, h4]; lib_int) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl)))))
  · exact ⟨q, Or.inl hq⟩
  · exfalso; rw [hq] at h4
    exact no3m (4 * q + -(3 * k * k) + -(4 * k) + -(b * b)) 8 (by rw [show 3 * (4 * q + -(3 * k * k) + -(4 * k) + -(b * b)) = 4 * (3 * q + -1) + -((3 * k + 2) * (3 * k + 2) + 3 * (b * b)) + 8 by lib_int, h4]; lib_int) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl))))))

/-- **Existence of the S-associate** when N(α) is prime to 6. -/
theorem ins_exists (α : Int × Int) (hodd : ∃ k, en α = 2 * k + 1) (h3 : C3 (en α)) : ∃ u, en u = 1 ∧ InS ((Cyclo3.cyc3 LibInt.intR).mul u α) := by
  obtain ⟨u, hu, hp⟩ := normalize3 α h3
  have hen : en ((Cyclo3.cyc3 LibInt.intR).mul u α) = en α := by rw [en_mul, hu]; lib_int
  generalize hγ : (Cyclo3.cyc3 LibInt.intR).mul u α = γ at hp hen
  obtain ⟨a, b⟩ := γ
  obtain ⟨e, f, he, hf⟩ := hp
  have he0 : a = 3 * e + 1 := he
  have hf0 : b = 3 * f := hf
  obtain ⟨K, hK⟩ := hodd
  have hN : a * a + -(a * b) + b * b = 2 * K + 1 := by rw [← hK, ← hen]; rfl
  rcases GaussInt.int_even_odd a with ⟨ra, ha⟩ | ⟨ra, ha⟩ <;> rcases GaussInt.int_even_odd b with ⟨rb, hb⟩ | ⟨rb, hb⟩
  · exfalso; exact GaussInt.int_odd_ne_even K (2 * ra * ra + -(2 * ra * rb) + 2 * rb * rb) (by rw [← hN, ha, hb]; lib_int)
  · -- a even, b odd: ω²
    refine ⟨(Cyclo3.cyc3 LibInt.intR).mul (-1, -1) u, by rw [en_mul, hu]; rfl, ?_⟩
    rw [show (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (-1, -1) u) α = (Cyclo3.cyc3 LibInt.intR).mul (-1, -1) ((Cyclo3.cyc3 LibInt.intR).mul u α) by lib_ring, hγ]
    refine ⟨⟨-ra, ?_⟩, ⟨rb + -ra, ?_⟩, ⟨2 * f + -e + -1, ?_⟩⟩
    · rw [mul2]; show -1 * b + -1 * a + -(-1 * b) = _; rw [ha]; lib_int
    · rw [mul1]; show -1 * a + -(-1 * b) - 1 = _; rw [ha, hb]; lib_int
    · rw [mul1, mul2]; show 2 * (-1 * a + -(-1 * b)) - (-1 * b + -1 * a + -(-1 * b)) - 2 = _; rw [he0, hf0]; lib_int
  · -- a odd, b even: itself
    refine ⟨u, hu, ?_⟩
    rw [hγ]
    refine ⟨⟨rb, hb⟩, ⟨ra, by show a - 1 = 2 * ra; rw [ha]; lib_int⟩, ⟨2 * e + -f, by show 2 * a - b - 2 = _; rw [he0, hf0]; lib_int⟩⟩
  · -- both odd: ω
    refine ⟨(Cyclo3.cyc3 LibInt.intR).mul (0, 1) u, by rw [en_mul, hu]; rfl, ?_⟩
    rw [show (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (0, 1) u) α = (Cyclo3.cyc3 LibInt.intR).mul (0, 1) ((Cyclo3.cyc3 LibInt.intR).mul u α) by lib_ring, hγ]
    refine ⟨⟨ra + -rb, ?_⟩, ⟨-rb + -1, ?_⟩, ⟨-f + -e + -1, ?_⟩⟩
    · rw [mul2]; show 0 * b + 1 * a + -(1 * b) = _; rw [ha, hb]; lib_int
    · rw [mul1]; show 0 * a + -(1 * b) - 1 = _; rw [hb]; lib_int
    · rw [mul1, mul2]; show 2 * (0 * a + -(1 * b)) - (0 * b + 1 * a + -(1 * b)) - 2 = _; rw [he0, hf0]; lib_int

def W36 (z : Int × Int) : Int × Int :=
  if InS z then z else
  if InS ((Cyclo3.cyc3 LibInt.intR).mul (0, 1) z) then (Cyclo3.cyc3 LibInt.intR).mul (0, 1) z else
  if InS ((Cyclo3.cyc3 LibInt.intR).mul (-1, -1) z) then (Cyclo3.cyc3 LibInt.intR).mul (-1, -1) z else
  if InS ((Cyclo3.cyc3 LibInt.intR).mul (-1, 0) z) then (Cyclo3.cyc3 LibInt.intR).mul (-1, 0) z else
  if InS ((Cyclo3.cyc3 LibInt.intR).mul (0, -1) z) then (Cyclo3.cyc3 LibInt.intR).mul (0, -1) z else
  if InS ((Cyclo3.cyc3 LibInt.intR).mul (1, 1) z) then (Cyclo3.cyc3 LibInt.intR).mul (1, 1) z else (Cyclo3.cyc3 LibInt.intR).zero

theorem same_assoc (z u v : Int × Int) (hu : en u = 1) (hv : en v = 1) (h1 : InS ((Cyclo3.cyc3 LibInt.intR).mul u z)) (h2 : InS ((Cyclo3.cyc3 LibInt.intR).mul v z)) :
    (Cyclo3.cyc3 LibInt.intR).mul v z = (Cyclo3.cyc3 LibInt.intR).mul u z := by
  have hcu : (Cyclo3.cyc3 LibInt.intR).mul (cj u) u = (Cyclo3.cyc3 LibInt.intR).one := by rw [(Cyclo3.cyc3 LibInt.intR).mul_comm, mul_cj, hu]; rfl
  have hw : en ((Cyclo3.cyc3 LibInt.intR).mul v (cj u)) = 1 := by rw [en_mul, en_cj, hu, hv]; rfl
  have e : (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul v (cj u)) ((Cyclo3.cyc3 LibInt.intR).mul u z) = (Cyclo3.cyc3 LibInt.intR).mul v z := by
    rw [show (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul v (cj u)) ((Cyclo3.cyc3 LibInt.intR).mul u z) = (Cyclo3.cyc3 LibInt.intR).mul v ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cj u) u) z) by lib_ring, hcu]; lib_ring
  have w1 := ins_unique _ _ h1 hw (by rw [e]; exact h2)
  rw [← e, w1]; lib_ring

theorem w36_eq (z u : Int × Int) (hu : en u = 1) (h : InS ((Cyclo3.cyc3 LibInt.intR).mul u z)) : W36 z = (Cyclo3.cyc3 LibInt.intR).mul u z := by
  unfold W36
  by_cases c1 : InS z
  · rw [ite_pos' _ _ c1]
    exact (same_assoc z u (Cyclo3.cyc3 LibInt.intR).one hu rfl h (by rw [(Cyclo3.cyc3 LibInt.intR).one_mul]; exact c1)).symm.trans ((Cyclo3.cyc3 LibInt.intR).one_mul z) |>.symm
  rw [ite_neg' _ _ c1]
  by_cases c2 : InS ((Cyclo3.cyc3 LibInt.intR).mul (0, 1) z)
  · rw [ite_pos' _ _ c2]; exact same_assoc z u _ hu rfl h c2
  rw [ite_neg' _ _ c2]
  by_cases c3 : InS ((Cyclo3.cyc3 LibInt.intR).mul (-1, -1) z)
  · rw [ite_pos' _ _ c3]; exact same_assoc z u _ hu rfl h c3
  rw [ite_neg' _ _ c3]
  by_cases c4 : InS ((Cyclo3.cyc3 LibInt.intR).mul (-1, 0) z)
  · rw [ite_pos' _ _ c4]; exact same_assoc z u _ hu rfl h c4
  rw [ite_neg' _ _ c4]
  by_cases c5 : InS ((Cyclo3.cyc3 LibInt.intR).mul (0, -1) z)
  · rw [ite_pos' _ _ c5]; exact same_assoc z u _ hu rfl h c5
  rw [ite_neg' _ _ c5]
  by_cases c6 : InS ((Cyclo3.cyc3 LibInt.intR).mul (1, 1) z)
  · rw [ite_pos' _ _ c6]; exact same_assoc z u _ hu rfl h c6
  exfalso
  rcases unit_list u hu with e | e | e | e | e | e <;> rw [e] at h
  · exact c1 (by rw [← (Cyclo3.cyc3 LibInt.intR).one_mul z]; exact h)
  · exact c4 h
  · exact c2 h
  · exact c5 h
  · exact c6 h
  · exact c3 h

theorem w36_zero (z : Int × Int) (h : ∀ u, en u = 1 → ¬ InS ((Cyclo3.cyc3 LibInt.intR).mul u z)) : W36 z = (Cyclo3.cyc3 LibInt.intR).zero := by
  unfold W36
  rw [ite_neg' _ _ (fun c => h (Cyclo3.cyc3 LibInt.intR).one rfl (by rw [(Cyclo3.cyc3 LibInt.intR).one_mul]; exact c)), ite_neg' _ _ (h _ rfl), ite_neg' _ _ (h _ rfl),
    ite_neg' _ _ (h _ rfl), ite_neg' _ _ (h _ rfl), ite_neg' _ _ (h _ rfl)]

theorem odd_factor (x y K : Int) (h : x * y = 2 * K + 1) : ∃ k, x = 2 * k + 1 := by
  rcases GaussInt.int_even_odd x with ⟨c, hc⟩ | hc
  · exfalso; exact GaussInt.int_odd_ne_even K (c * y) (by rw [← h, hc]; lib_int)
  · exact hc

theorem c3_factor (x y : Int) (h : C3 (x * y)) : C3 x := by
  obtain ⟨q, hq | hq | hq⟩ := res3 x
  · exact (c3_ne _ (q * y) h (by rw [hq]; lib_int)).elim
  · exact ⟨q, Or.inl hq⟩
  · exact ⟨q + -1, Or.inr (by rw [hq]; lib_int)⟩

theorem w36_cases (z : Int × Int) : (∃ u, en u = 1 ∧ InS ((Cyclo3.cyc3 LibInt.intR).mul u z)) ∨ (∀ u, en u = 1 → ¬ InS ((Cyclo3.cyc3 LibInt.intR).mul u z)) := by
  by_cases c1 : InS z
  · exact Or.inl ⟨(Cyclo3.cyc3 LibInt.intR).one, rfl, by rw [(Cyclo3.cyc3 LibInt.intR).one_mul]; exact c1⟩
  by_cases c2 : InS ((Cyclo3.cyc3 LibInt.intR).mul (0, 1) z)
  · exact Or.inl ⟨(0, 1), rfl, c2⟩
  by_cases c3 : InS ((Cyclo3.cyc3 LibInt.intR).mul (-1, -1) z)
  · exact Or.inl ⟨(-1, -1), rfl, c3⟩
  by_cases c4 : InS ((Cyclo3.cyc3 LibInt.intR).mul (-1, 0) z)
  · exact Or.inl ⟨(-1, 0), rfl, c4⟩
  by_cases c5 : InS ((Cyclo3.cyc3 LibInt.intR).mul (0, -1) z)
  · exact Or.inl ⟨(0, -1), rfl, c5⟩
  by_cases c6 : InS ((Cyclo3.cyc3 LibInt.intR).mul (1, 1) z)
  · exact Or.inl ⟨(1, 1), rfl, c6⟩
  refine Or.inr (fun u hu h => ?_)
  rcases unit_list u hu with e | e | e | e | e | e <;> rw [e] at h
  · exact c1 (by rw [← (Cyclo3.cyc3 LibInt.intR).one_mul z]; exact h)
  · exact c4 h
  · exact c2 h
  · exact c5 h
  · exact c6 h
  · exact c3 h

/-- **W36 is multiplicative.** -/
theorem w36_mul (α β : Int × Int) : W36 ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W36 α) (W36 β) := by
  rcases w36_cases α with hα | hα
  · rcases w36_cases β with hβ | hβ
    · obtain ⟨u, hu, iu⟩ := hα
      obtain ⟨v, hv, iv⟩ := hβ
      rw [w36_eq α u hu iu, w36_eq β v hv iv]
      have e : (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul u v) ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul u α) ((Cyclo3.cyc3 LibInt.intR).mul v β) := by lib_ring
      rw [w36_eq _ ((Cyclo3.cyc3 LibInt.intR).mul u v) (by rw [en_mul, hu, hv]; rfl) (by rw [e]; exact ins_mul _ _ iu iv), e]
    · rw [w36_zero β hβ]
      rw [w36_zero _ (fun w hw iw => by
        obtain ⟨⟨K, hK⟩, hc⟩ := ins_norm _ iw
        rw [en_mul, hw, en_mul] at hK hc
        rw [show (1 : Int) * (en α * en β) = en β * en α by lib_int] at hK hc
        obtain ⟨v, hv, iv⟩ := ins_exists β (odd_factor _ _ K hK) (c3_factor _ _ hc)
        exact hβ v hv iv)]
      lib_ring
  · rw [w36_zero α hα]
    rw [w36_zero _ (fun w hw iw => by
      obtain ⟨⟨K, hK⟩, hc⟩ := ins_norm _ iw
      rw [en_mul, hw, en_mul] at hK hc
      rw [show (1 : Int) * (en α * en β) = en α * en β by lib_int] at hK hc
      obtain ⟨u, hu, iu⟩ := ins_exists α (odd_factor _ _ K hK) (c3_factor _ _ hc)
      exact hα u hu iu)]
    lib_ring

end W36Mul

#print axioms W36Mul.two_cancel_int
#print axioms W36Mul.abs_one
#print axioms W36Mul.nat_sq_le1
#print axioms W36Mul.nat_sq_one
#print axioms W36Mul.unit_list
#print axioms W36Mul.ins_iff
#print axioms W36Mul.mul1
#print axioms W36Mul.mul2
#print axioms W36Mul.ins_mul
#print axioms W36Mul.no3m
#print axioms W36Mul.ins_unique
#print axioms W36Mul.ins_norm
#print axioms W36Mul.ins_exists
#print axioms W36Mul.same_assoc
#print axioms W36Mul.w36_eq
#print axioms W36Mul.w36_zero
#print axioms W36Mul.odd_factor
#print axioms W36Mul.c3_factor
#print axioms W36Mul.w36_cases
#print axioms W36Mul.w36_mul

#print axioms: 20 theorems, no axioms.

78.236 The theta series of \(W_{36}\)

For \(n\) prime to \(6\), \(\alpha \mapsto W_{36}(\alpha )\) maps the primary elements of norm \(n\) bijectively onto \(S\).

\[ \sum _{\alpha \equiv 1\,(3),\ N\alpha =n}W_{36}(\alpha )=\sum _{\beta \in S,\ N\beta =n}\beta . \]

lang/Theta36W.lean

/-
  lang/Theta36W — T47/T50, cycle 622: **the theta series of W36 is the sum over S**: for n prime to 6,
    cw3 W36 n = Σ_{α ≡ 1 (3), N(α) = n} W36(α) = Σ_{β ∈ S, N(β) = n} β,
  since α ↦ W36(α) is a bijection from the primary elements of norm n onto S ∩ {N = n} (inverse: the primary associate).
    JS            the indicator of S ∩ {N = n}
    w36_S         PD3 n z ⇒ W36 z ∈ S, N(W36 z) = n, W36 z = v z for a unit v
    key36         Σ_z [PD3 n z] δ(β, W36 z) = JS n β
    cw3_w36       the identity
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.W36Mul

namespace Theta36W

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius EisEuclid EisPrim EisUnique3 EisPrime3 HeckeMult3 W36Mul

def JS (n : Nat) (β : Int × Int) : Int × Int := if InS β ∧ en β = Int.ofNat n then (Cyclo3.cyc3 LibInt.intR).one else (Cyclo3.cyc3 LibInt.intR).zero

theorem w36_S (n : Nat) (hodd : ∃ k, Int.ofNat n = 2 * k + 1) (h3 : C3 (Int.ofNat n)) (z : Int × Int) (hz : PD3 n z) :
    ∃ v, en v = 1 ∧ InS ((Cyclo3.cyc3 LibInt.intR).mul v z) ∧ W36 z = (Cyclo3.cyc3 LibInt.intR).mul v z ∧ en (W36 z) = Int.ofNat n := by
  obtain ⟨k, hk⟩ := hodd
  obtain ⟨v, hv, iv⟩ := ins_exists z ⟨k, by rw [hz.1, hk]⟩ (by rw [hz.1]; exact h3)
  have e := w36_eq z v hv iv
  exact ⟨v, hv, iv, e, by rw [e, en_mul, hv, hz.1]; lib_int⟩

theorem key36 (n : Nat) (hodd : ∃ k, Int.ofNat n = 2 * k + 1) (h3 : C3 (Int.ofNat n)) (β : Int × Int) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun z => (Cyclo3.cyc3 LibInt.intR).mul (I3 n z) (delta (Cyclo3.cyc3 LibInt.intR) β (W36 z))) (box3 n) = JS n β := by
  unfold JS
  by_cases h : InS β ∧ en β = Int.ofNat n
  · rw [ite_pos' _ _ h]
    obtain ⟨_, hc⟩ := ins_norm β h.1
    obtain ⟨u, hu, pu⟩ := normalize3 β hc
    have hcu : (Cyclo3.cyc3 LibInt.intR).mul (cj u) u = (Cyclo3.cyc3 LibInt.intR).one := by rw [(Cyclo3.cyc3 LibInt.intR).mul_comm, mul_cj, hu]; rfl
    generalize hz0 : (Cyclo3.cyc3 LibInt.intR).mul u β = z0 at pu
    have hβ : β = (Cyclo3.cyc3 LibInt.intR).mul (cj u) z0 := by
      rw [← hz0, show (Cyclo3.cyc3 LibInt.intR).mul (cj u) ((Cyclo3.cyc3 LibInt.intR).mul u β) = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cj u) u) β by lib_ring, hcu]; lib_ring
    have hn0 : en z0 = Int.ofNat n := by rw [← hz0, en_mul, hu, h.2]; lib_int
    have pd0 : PD3 n z0 := ⟨hn0, pu⟩
    have w0 : W36 z0 = β := by
      rw [w36_eq z0 (cj u) (by rw [en_cj]; exact hu) (by rw [← hβ]; exact h.1), ← hβ]
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun z => delta (Cyclo3.cyc3 LibInt.intR) z z0) (fun z => by
      by_cases hz : PD3 n z
      · rw [I3_pos _ _ hz]
        by_cases e : z = z0
        · rw [e, w0]; unfold delta; rw [ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · unfold delta
          rw [ite_neg' _ _ e]
          obtain ⟨v, hv, _, ev, _⟩ := w36_S n hodd h3 z hz
          rw [ite_neg' _ _ (fun hb => e (by
            -- β = v z, β = ū z0 ⇒ z = (v̄ ū) z0
            have hcv : (Cyclo3.cyc3 LibInt.intR).mul (cj v) v = (Cyclo3.cyc3 LibInt.intR).one := by rw [(Cyclo3.cyc3 LibInt.intR).mul_comm, mul_cj, hv]; rfl
            have hz' : z = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cj v) (cj u)) z0 := by
              calc z = (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (cj v) v) z := by rw [hcv]; lib_ring
                _ = (Cyclo3.cyc3 LibInt.intR).mul (cj v) ((Cyclo3.cyc3 LibInt.intR).mul v z) := by lib_ring
                _ = (Cyclo3.cyc3 LibInt.intR).mul (cj v) β := by rw [← ev, ← hb]
                _ = _ := by rw [hβ]; lib_ring
            have w1 := unit_one3 ((Cyclo3.cyc3 LibInt.intR).mul (cj v) (cj u)) z0 (by rw [en_mul, en_cj, en_cj, hv, hu]; rfl) pu (by rw [← hz']; exact hz.2)
            rw [hz', w1]; lib_ring))]
          lib_ring
      · rw [I3_neg _ _ hz]; unfold delta
        rw [ite_neg' _ _ (fun (e : z = z0) => hz (by rw [e]; exact pd0))]; lib_ring)]
    have hb := abs_le3 z0 n hn0
    exact HeckeMult.count_box (Cyclo3.cyc3 LibInt.intR) z0 _ hb.1 hb.2
  · rw [ite_neg' _ _ h]
    rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) (fun z => by
      by_cases hz : PD3 n z
      · rw [I3_pos _ _ hz]
        obtain ⟨v, _, iv, ev, nv⟩ := w36_S n hodd h3 z hz
        unfold delta
        rw [ite_neg' _ _ (fun e => h ⟨by rw [e, ev]; exact iv, by rw [e]; exact nv⟩)]; lib_ring
      · rw [I3_neg _ _ hz]; lib_ring)]
    exact zsum3 _

/-- **cw3 W36 n = Σ_{β ∈ S, N(β) = n} β** for n prime to 6. -/
theorem cw3_w36 (n : Nat) (hodd : ∃ k, Int.ofNat n = 2 * k + 1) (h3 : C3 (Int.ofNat n)) :
    cw3 W36 n = sumK (Cyclo3.cyc3 LibInt.intR) (fun β => (Cyclo3.cyc3 LibInt.intR).mul (JS n β) β) (box3 n) := by
  unfold cw3
  have e1 : ∀ z, (Cyclo3.cyc3 LibInt.intR).mul (I3 n z) (W36 z) =
      sumK (Cyclo3.cyc3 LibInt.intR) (fun β => (Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (I3 n z) (delta (Cyclo3.cyc3 LibInt.intR) β (W36 z))) β) (box3 n) := fun z => by
    by_cases hz : PD3 n z
    · obtain ⟨_, _, _, _, nv⟩ := w36_S n hodd h3 z hz
      have hb := abs_le3 _ n nv
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun β => (Cyclo3.cyc3 LibInt.intR).mul (I3 n z) ((Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) β (W36 z)) β)) (fun β => by lib_ring),
        ← mul_sumK, HeckeMult.pick_box (Cyclo3.cyc3 LibInt.intR) (fun β => β) (W36 z) _ hb.1 hb.2]
    · rw [I3_neg _ _ hz]
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) (fun β => by lib_ring), zsum3]; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ e1, sumK_swap]
  refine sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun β => ?_) _
  rw [← key36 n hodd h3 β, sumK_mul]

end Theta36W

#print axioms Theta36W.w36_S
#print axioms Theta36W.key36
#print axioms Theta36W.cw3_w36

#print axioms: 3 theorems, no axioms.

78.237 36a: multiplicativity of the coefficients of \(\eta (6z)^4\)

The coefficients \(c_3(n)=\sum _{x\equiv 1\,(3),\ x^2+3y^2=n}x\) of \(\eta (6z)^4\) are the theta coefficients of \(W_{36}\), hence multiplicative.

\[ c_3(mn)=c_3(m)\,c_3(n)\qquad ((m,n)=(mn,6)=1). \]

lang/Mult36.lean

/-
  lang/Mult36 — T47/T50, cycle 623: **the coefficients of η(6z)⁴ (36a) are multiplicative**:
    c3(mn) = c3(m) c3(n)   for coprime m, n prime to 6,
  where c3 n = Σ_{x ≡ 1 (3), x² + 3y² = n} x = etaC36 n (Theta36, Eta36All).
    sum_pair     a sum in ℤ[ω] is the pair of the coordinate sums
    js_phi       for n odd: [β = (x + y, 2y) ∈ S, N β = n] = [x ≡ 1 (3), x² + 3y² = n]
    sumS         Σ_{β ∈ S, N β = n} β = (c3 n, 0)                     (β ↔ (x, y); Theta36.c3_xy, g3_zero)
    cw3_c3       cw3 W36 n = (c3 n, 0) for n prime to 6                 (Theta36W.cw3_w36)
    c3_mult      c3 multiplicative (HeckeMult3.hecke_mult3 with W36Mul.w36_mul)
    eta36_mult   the η(6z)⁴ coefficients are multiplicative
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Theta36W
import lang.Eta36All

namespace Mult36

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius EisEuclid EisPrim HeckeMult3 W36Mul Theta36W Theta36

theorem sum_pair {A : Type} (f : A → Int × Int) : ∀ l : List A,
    sumK (Cyclo3.cyc3 LibInt.intR) f l = (sumK LibInt.intR (fun a => (f a).1) l, sumK LibInt.intR (fun a => (f a).2) l)
  | [] => rfl
  | a :: l => by
    show (Cyclo3.cyc3 LibInt.intR).add (f a) (sumK (Cyclo3.cyc3 LibInt.intR) f l) = _
    rw [sum_pair f l]; rfl

def phi (p : Int × Int) : Int × Int := (p.1 + p.2, 2 * p.2)

theorem en_phi (x y : Int) : en (phi (x, y)) = x * x + 3 * (y * y) := by
  show (x + y) * (x + y) + -((x + y) * (2 * y)) + 2 * y * (2 * y) = _; lib_int

theorem js_phi (n : Nat) (hodd : ∃ k, Int.ofNat n = 2 * k + 1) (x y : Int) :
    JS n (phi (x, y)) = if P3 n (x, y) then (Cyclo3.cyc3 LibInt.intR).one else (Cyclo3.cyc3 LibInt.intR).zero := by
  unfold JS
  by_cases h : P3 n (x, y)
  · rw [ite_pos' _ _ h]
    obtain ⟨hn0, c, hc⟩ := h
    have hn : x * x + 3 * (y * y) = Int.ofNat n := hn0
    have hc' : x - 1 = 3 * c := hc
    have hxy : ∃ r, x + y - 1 = 2 * r := by
      obtain ⟨K, hK⟩ := hodd
      rcases GaussInt.int_even_odd x with ⟨a, ha⟩ | ⟨a, ha⟩ <;> rcases GaussInt.int_even_odd y with ⟨b, hb⟩ | ⟨b, hb⟩
      · exact (GaussInt.int_odd_ne_even K (2 * a * a + 6 * b * b) (by rw [← hK, ← hn, ha, hb]; lib_int)).elim
      · exact ⟨a + b, by rw [ha, hb]; lib_int⟩
      · exact ⟨a + b, by rw [ha, hb]; lib_int⟩
      · exact (GaussInt.int_odd_ne_even K (2 * a * a + 2 * a + 6 * b * b + 6 * b + 2) (by rw [← hK, ← hn, ha, hb]; lib_int)).elim
    obtain ⟨r, hr⟩ := hxy
    rw [ite_pos' _ _ ⟨⟨⟨y, rfl⟩, ⟨r, hr⟩, ⟨2 * c, by show 2 * (x + y) - 2 * y - 2 = _; rw [show 2 * (x + y) - 2 * y - 2 = 2 * (x - 1) by lib_int, hc']; lib_int⟩⟩, by rw [en_phi, hn]⟩]
  · rw [ite_neg' _ _ h]
    rw [ite_neg' _ _ (fun hs => h ⟨by rw [← en_phi]; exact hs.2, by
      obtain ⟨_, _, ⟨c, hc⟩⟩ := hs.1
      have hc' : 2 * (x + y) - 2 * y - 2 = 3 * c := hc
      exact ⟨2 * c + -(x - 1), by
        calc x - 1 = 4 * (x - 1) + -(3 * (x - 1)) := by lib_int
          _ = 2 * (2 * (x + y) - 2 * y - 2) + -(3 * (x - 1)) := by lib_int
          _ = _ := by rw [hc']; lib_int⟩⟩)]

theorem natsq_le (a : Int) (b : Int) (n : Nat) (h : a * a + 3 * (b * b) = Int.ofNat n) : Int.natAbs a ≤ n ∧ Int.natAbs b ≤ n := by
  have h' : Int.natAbs a * Int.natAbs a + 3 * (Int.natAbs b * Int.natAbs b) = n := Int.ofNat.inj (by
    show Int.ofNat (Int.natAbs a * Int.natAbs a) + 3 * Int.ofNat (Int.natAbs b * Int.natAbs b) = _
    rw [GaussEuclid.nas, GaussEuclid.nas, h])
  constructor
  · exact Nat.le_trans (HeckeMult.le_sq _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h'))
  · exact Nat.le_trans (HeckeMult.le_sq _) (Nat.le_trans (Nat.le_trans (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_succ_of_le (Nat.le_succ 1)))) (Nat.le_add_left _ _)) (Nat.le_of_eq h'))

/-- **Σ_{β ∈ S, N β = n} β = (c3 n, 0)** for odd n. -/
theorem sumS (n : Nat) (hodd : ∃ k, Int.ofNat n = 2 * k + 1) :
    sumK (Cyclo3.cyc3 LibInt.intR) (fun β => (Cyclo3.cyc3 LibInt.intR).mul (JS n β) β) (box3 n) = (c3 n, 0) := by
  -- step 1: insert Σ_p δ(φ p, β)
  have st1 : ∀ β, (Cyclo3.cyc3 LibInt.intR).mul (JS n β) β =
      sumK (Cyclo3.cyc3 LibInt.intR) (fun p => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) β (phi p)) ((Cyclo3.cyc3 LibInt.intR).mul (JS n β) β)) (HeckeMult.box n) := fun β => by
    unfold JS
    by_cases h : InS β ∧ en β = Int.ofNat n
    · rw [ite_pos' _ _ h, ← sumK_mul]
      obtain ⟨⟨s, hs⟩, _, _⟩ := h.1
      have hs' : β.2 = 2 * s := hs
      have hq : (β.1 - s) * (β.1 - s) + 3 * (s * s) = Int.ofNat n := by
        rw [← h.2]; show _ = β.1 * β.1 + -(β.1 * β.2) + β.2 * β.2; rw [hs']; lib_int
      have hb := natsq_le _ _ n hq
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun p => delta (Cyclo3.cyc3 LibInt.intR) p (β.1 - s, s)) (fun p => by
        unfold delta
        by_cases e : p = (β.1 - s, s)
        · rw [ite_pos' _ _ e, ite_pos' _ _ (by rw [e]; exact Prod.ext (show β.1 = β.1 - s + s by lib_int) (show β.2 = 2 * s from hs'))]
        · rw [ite_neg' _ _ e, ite_neg' _ _ (fun (e' : β = phi p) => e (by
            obtain ⟨p1, p2⟩ := p
            have e1 : β.1 = p1 + p2 := congrArg Prod.fst e'
            have e2 : β.2 = 2 * p2 := congrArg Prod.snd e'
            have hp2 : p2 = s := two_cancel_int p2 s (by rw [← e2, hs'])
            exact Prod.ext (show p1 = β.1 - s by rw [e1, hp2]; lib_int) hp2))]),
        HeckeMult.count_box (Cyclo3.cyc3 LibInt.intR) (β.1 - s, s) n hb.1 hb.2]
      lib_ring
    · rw [ite_neg' _ _ h]
      rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) (fun p => by lib_ring), zsum3]; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ st1, sumK_swap]
  -- step 2: Σ_β δ(β, φ p) JS β β = JS(φ p) φ p
  have st2 : ∀ p, sumK (Cyclo3.cyc3 LibInt.intR) (fun β => (Cyclo3.cyc3 LibInt.intR).mul (delta (Cyclo3.cyc3 LibInt.intR) β (phi p)) ((Cyclo3.cyc3 LibInt.intR).mul (JS n β) β)) (box3 n) =
      (Cyclo3.cyc3 LibInt.intR).mul (JS n (phi p)) (phi p) := fun p => by
    by_cases h : InS (phi p) ∧ en (phi p) = Int.ofNat n
    · have hb := abs_le3 _ n h.2
      exact HeckeMult.pick_box (Cyclo3.cyc3 LibInt.intR) (fun β => (Cyclo3.cyc3 LibInt.intR).mul (JS n β) β) (phi p) _ hb.1 hb.2
    · have hz : JS n (phi p) = (Cyclo3.cyc3 LibInt.intR).zero := ite_neg' _ _ h
      rw [hz, sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) (fun β => by
        unfold delta
        by_cases e : β = phi p
        · rw [ite_pos' _ _ e, e, hz]; lib_ring
        · rw [ite_neg' _ _ e]; lib_ring), zsum3]; lib_ring
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ st2]
  unfold HeckeMult.box; rw [HeckeMult.sumK_pairs]
  -- step 3: coordinates
  have st3 : ∀ x y, (Cyclo3.cyc3 LibInt.intR).mul (JS n (phi (x, y))) (phi (x, y)) = (H3 n (x, y), 2 * G3 n (x, y)) := fun x y => by
    rw [js_phi n hodd x y]; unfold H3 G3
    by_cases h : P3 n (x, y)
    · rw [ite_pos' _ _ h, ite_pos' _ _ h, ite_pos' _ _ h]
      apply Prod.ext
      · show 1 * (x + y) + -(0 * (2 * y)) = x + y; lib_int
      · show 1 * (2 * y) + 0 * (x + y) + -(0 * (2 * y)) = 2 * y; lib_int
    · rw [ite_neg' _ _ h, ite_neg' _ _ h, ite_neg' _ _ h]
      apply Prod.ext
      · show 0 * (x + y) + -(0 * (2 * y)) = 0; lib_int
      · show 0 * (2 * y) + 0 * (x + y) + -(0 * (2 * y)) = 2 * 0; lib_int
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun x => sumK_ext (Cyclo3.cyc3 LibInt.intR) _ _ (fun y => st3 x y) _)]
  rw [sum_pair]
  rw [sumK_ext LibInt.intR _ _ (fun x => congrArg Prod.fst (sum_pair (fun y => (H3 n (x, y), 2 * G3 n (x, y))) (HeckeMult.zl n))),
    sumK_ext LibInt.intR (fun x => (sumK (Cyclo3.cyc3 LibInt.intR) (fun y => (H3 n (x, y), 2 * G3 n (x, y))) (HeckeMult.zl n)).2) _
      (fun x => congrArg Prod.snd (sum_pair (fun y => (H3 n (x, y), 2 * G3 n (x, y))) (HeckeMult.zl n)))]
  apply Prod.ext
  · exact (c3_xy n).symm
  · show sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => 2 * G3 n (x, y)) (HeckeMult.zl n)) (HeckeMult.zl n) = 0
    rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.mul 2 (sumK LibInt.intR (fun y => G3 n (x, y)) (HeckeMult.zl n))) (fun x => by rw [mul_sumK]; rfl),
      ← mul_sumK, g3_zero]
    show (2 : Int) * 0 = 0; rfl

theorem cw3_c3 (n : Nat) (hodd : ∃ k, Int.ofNat n = 2 * k + 1) (h3 : C3 (Int.ofNat n)) : cw3 W36 n = (c3 n, 0) := by
  rw [cw3_w36 n hodd h3, sumS n hodd]

theorem odd_mul (a b : Int) (ha : ∃ k, a = 2 * k + 1) (hb : ∃ k, b = 2 * k + 1) : ∃ k, a * b = 2 * k + 1 := by
  obtain ⟨k, hk⟩ := ha; obtain ⟨l, hl⟩ := hb
  exact ⟨2 * k * l + k + l, by rw [hk, hl]; lib_int⟩

theorem c3_mul (a b : Int) (ha : C3 a) (hb : C3 b) : C3 (a * b) := by
  obtain ⟨k, hk | hk⟩ := ha <;> obtain ⟨l, hl | hl⟩ := hb
  · exact ⟨3 * k * l + k + l, Or.inl (by rw [hk, hl]; lib_int)⟩
  · exact ⟨3 * k * l + 2 * k + l, Or.inr (by rw [hk, hl]; lib_int)⟩
  · exact ⟨3 * k * l + k + 2 * l, Or.inr (by rw [hk, hl]; lib_int)⟩
  · exact ⟨3 * k * l + 2 * k + 2 * l + 1, Or.inl (by rw [hk, hl]; lib_int)⟩

/-- **c3(mn) = c3(m) c3(n)** for coprime m, n prime to 6. -/
theorem c3_mult (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (om : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (on : ∃ k, Int.ofNat (n + 1) = 2 * k + 1)
    (hm : C3 (Int.ofNat (m + 1))) (hn : C3 (Int.ofNat (n + 1))) :
    c3 ((m + 1) * (n + 1)) = c3 (m + 1) * c3 (n + 1) := by
  have h := hecke_mult3 W36 w36_mul m n s t hst hm hn
  rw [cw3_c3 _ om hm, cw3_c3 _ on hn, cw3_c3 _ (odd_mul _ _ om on) (c3_mul _ _ hm hn)] at h
  have h1 : c3 ((m + 1) * (n + 1)) = c3 (m + 1) * c3 (n + 1) + -(0 * 0) := congrArg Prod.fst h
  rw [h1]; lib_int

/-- **The coefficients of η(6z)⁴ are multiplicative** (coprime arguments prime to 6). -/
theorem eta36_mult (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (om : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (on : ∃ k, Int.ofNat (n + 1) = 2 * k + 1)
    (hm : C3 (Int.ofNat (m + 1))) (hn : C3 (Int.ofNat (n + 1))) :
    Hecke36.etaC36 ((m + 1) * (n + 1)) = Hecke36.etaC36 (m + 1) * Hecke36.etaC36 (n + 1) := by
  rw [Theta36.eta36_all, Theta36.eta36_all, Theta36.eta36_all]
  exact c3_mult m n s t hst om on hm hn

end Mult36

#print axioms Mult36.sum_pair
#print axioms Mult36.en_phi
#print axioms Mult36.js_phi
#print axioms Mult36.natsq_le
#print axioms Mult36.sumS
#print axioms Mult36.cw3_c3
#print axioms Mult36.odd_mul
#print axioms Mult36.c3_mul
#print axioms Mult36.c3_mult
#print axioms Mult36.eta36_mult

#print axioms: 10 theorems, no axioms.

78.238 36a: the Hecke recursion

At a split prime the coefficients obey the Hecke recursion with \(a_p=\mathrm {Tr}\,W_{36}(\pi )\); at an inert prime \(c_3(p^{2k+1})=0\), \(c_3(p^{2k})=(-p)^k\).

\[ c_3(p^{k+2})=a_p\,c_3(p^{k+1})-p\,c_3(p^k). \]

lang/Hecke36Rec.lean

/-
  lang/Hecke36Rec — T47/T50, cycle 624: **36a: the Hecke recursion at every prime p ≠ 2, 3** for c3 n = Σ_{x≡1 (3), x²+3y²=n} x
  (= the coefficients of η(6z)⁴, Eta36All):
    w36_one       W36(1) = 1
    ins_cj        S is closed under conjugation;  w36_cj   W36(π̄) = conj W36(π)
    c3_rec        p ≡ 1 (mod 3), W36(π) = a + bω:  c3(p^(k+2)) = (2a − b) c3(p^(k+1)) − p c3(p^k)
    c3_inert_odd  p ≡ 2 (mod 3): c3(p^(2k+1)) = 0
    c3_inert_even p ≡ 2 (mod 3): c3(p^(2k)) = ((−p)^k)            (W36(−p) = −p)
  So the p-part of Σ c3(n) n^(−s) is 1/(1 − a_p p^(−s) + p^(1−2s)) at every p ∤ 6 (a_p = 2a − b = 2x, resp. 0).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Mult36

namespace Hecke36Rec

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius EisEuclid EisPrim EisUnique3 EisPrime3 HeckeMult3 W36Mul Theta36W Mult36 Theta36

theorem w36_one : W36 (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).one := by
  have h : InS (Cyclo3.cyc3 LibInt.intR).one := ⟨⟨0, rfl⟩, ⟨0, rfl⟩, ⟨0, rfl⟩⟩
  unfold W36; rw [ite_pos' _ _ h]

theorem ins_cj (z : Int × Int) (h : InS z) : InS (cj z) := by
  obtain ⟨a, b⟩ := z
  obtain ⟨⟨s, hs⟩, ⟨r, hr⟩, ⟨k, hk⟩⟩ := h
  have hs0 : b = 2 * s := hs
  have hr0 : a - 1 = 2 * r := hr
  have hk0 : 2 * a - b - 2 = 3 * k := hk
  refine ⟨⟨-s, ?_⟩, ⟨r + -s, ?_⟩, ⟨k, ?_⟩⟩
  · show -b = 2 * -s; rw [hs0]; lib_int
  · show a + -b - 1 = 2 * (r + -s)
    calc a + -b - 1 = (a - 1) + -b := by lib_int
      _ = _ := by rw [hr0, hs0]; lib_int
  · show 2 * (a + -b) - -b - 2 = 3 * k
    rw [← hk0]; lib_int

theorem w36_cj (π : Int × Int) (u : Int × Int) (hu : en u = 1) (h : InS ((Cyclo3.cyc3 LibInt.intR).mul u π)) :
    W36 (cj π) = cj (W36 π) := by
  rw [w36_eq π u hu h, w36_eq (cj π) (cj u) (by rw [en_cj]; exact hu) (by rw [← cj_mul]; exact ins_cj _ h), cj_mul]

theorem pow_odd (p : Nat) (hp : ∃ k, Int.ofNat p = 2 * k + 1) : ∀ j, ∃ k, Int.ofNat (p ^ j) = 2 * k + 1
  | 0 => ⟨0, rfl⟩
  | j + 1 => by
    obtain ⟨k, hk⟩ := pow_odd p hp j
    obtain ⟨l, hl⟩ := hp
    exact ⟨2 * k * l + k + l, by show Int.ofNat (p ^ j) * Int.ofNat p = _; rw [hk, hl]; lib_int⟩

theorem pow_c3 (p : Nat) (hp : ∃ k, Int.ofNat p = 3 * k + 1) : ∀ j, C3 (Int.ofNat (p ^ j))
  | 0 => ⟨0, Or.inl rfl⟩
  | j + 1 => by
    show C3 (Int.ofNat (p ^ j) * Int.ofNat p)
    obtain ⟨l, hl⟩ := hp
    exact c3_mul _ _ (pow_c3 p ⟨l, hl⟩ j) ⟨l, Or.inl hl⟩

/-- **The 36a Hecke recursion at a split prime.** -/
theorem c3_rec (P : Nat) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hodd : ∃ k, Int.ofNat (P + 1 + 1) = 2 * k + 1) (h1 : ∃ k, Int.ofNat (P + 1 + 1) = 3 * k + 1)
    (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π) (k : Nat) :
    c3 ((P + 1 + 1) ^ (k + 2)) = (2 * (W36 π).1 - (W36 π).2) * c3 ((P + 1 + 1) ^ (k + 1)) - Int.ofNat (P + 1 + 1) * c3 ((P + 1 + 1) ^ k) := by
  have hen : en π = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  obtain ⟨K, hK⟩ := hodd
  obtain ⟨L, hL⟩ := h1
  obtain ⟨u, hu, iu⟩ := ins_exists π ⟨K, by rw [hen, hK]⟩ ⟨L, Or.inl (by rw [hen, hL])⟩
  have hr := HeckePrime3.cw3_rec (P) W36 w36_mul w36_one hp π hπ pπ k
  rw [w36_cj π u hu iu] at hr
  have hW : en (W36 π) = Int.ofNat (P + 1 + 1) := by rw [w36_eq π u hu iu, en_mul, hu, hen]; lib_int
  have c3n : ∀ j, cw3 W36 ((P + 1 + 1) ^ j) = (c3 ((P + 1 + 1) ^ j), 0) := fun j =>
    cw3_c3 _ (pow_odd _ ⟨K, hK⟩ j) (pow_c3 _ ⟨L, hL⟩ j)
  rw [c3n, c3n, c3n] at hr
  have h1' : c3 ((P + 1 + 1) ^ (k + 2)) = ((Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (W36 π) (cj (W36 π))) (c3 ((P + 1 + 1) ^ (k + 1)), 0))
      ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (W36 π) (cj (W36 π))) (c3 ((P + 1 + 1) ^ k), 0)))).1 := congrArg Prod.fst hr
  rw [h1', mul_cj, hW]
  generalize W36 π = w
  show ((w.1 + (w.1 + -w.2)) * c3 ((P + 1 + 1) ^ (k + 1)) + -((w.2 + -w.2) * 0)) + -(Int.ofNat (P + 1 + 1) * c3 ((P + 1 + 1) ^ k) + -(0 * 0)) = _
  lib_int

/-- **36a at an inert prime, odd powers: c3(p^(2k+1)) = 0.** -/
theorem c3_inert_odd (t : Nat) (hodd : ∃ k, Int.ofNat (3 * t + 1 + 1) = 2 * k + 1) (k : Nat) :
    c3 ((3 * t + 1 + 1) ^ (2 * k + 1)) = 0 := by
  obtain ⟨K, hK⟩ := EisInert3.pow_odd t k
  have h := cw3_c3 _ (pow_odd _ hodd _) ⟨K, Or.inr hK⟩
  rw [EisInert3.cw3_odd t W36 k] at h
  exact (congrArg Prod.fst h).symm

theorem w36_nPi (t : Nat) (hodd : ∃ k, Int.ofNat (3 * t + 1 + 1) = 2 * k + 1) : W36 (EisInert3.nPi t) = EisInert3.nPi t := by
  obtain ⟨K, hK⟩ := hodd
  have h : InS (EisInert3.nPi t) := ⟨⟨0, rfl⟩, ⟨-K + -1, by show -Int.ofNat (3 * t + 1 + 1) - 1 = _; rw [hK]; lib_int⟩,
    ⟨-(2 * Int.ofNat t) + -2, by show 2 * -Int.ofNat (3 * t + 1 + 1) - 0 - 2 = _; show 2 * -(3 * Int.ofNat t + 1 + 1) - 0 - 2 = _; lib_int⟩⟩
  unfold W36; rw [ite_pos' _ _ h]

/-- **36a at an inert prime, even powers: c3(p^(2k)) = (−p)^k.** -/
theorem c3_inert_even (t : Nat) (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1)
    (hodd : ∃ k, Int.ofNat (3 * t + 1 + 1) = 2 * k + 1) (k : Nat) :
    c3 ((3 * t + 1 + 1) ^ (2 * k)) = (pw (Cyclo3.cyc3 LibInt.intR) (EisInert3.nPi t) k).1 := by
  obtain ⟨K, hK⟩ := hodd
  have hc : ∀ j, C3 (Int.ofNat ((3 * t + 1 + 1) ^ (2 * j))) := by
    intro j
    induction j with
    | zero => exact ⟨0, Or.inl rfl⟩
    | succ j ih =>
      rw [EisInert3.nat_pow2]
      show C3 (Int.ofNat (3 * t + 1 + 1) * Int.ofNat (3 * t + 1 + 1) * Int.ofNat ((3 * t + 1 + 1) ^ (2 * j)))
      exact c3_mul _ _ ⟨3 * Int.ofNat t * Int.ofNat t + 4 * Int.ofNat t + 1, Or.inl (by
        show (3 * Int.ofNat t + 1 + 1) * (3 * Int.ofNat t + 1 + 1) = _; lib_int)⟩ ih
  have h := cw3_c3 _ (pow_odd _ ⟨K, hK⟩ _) (hc k)
  rw [EisInert3.cw3_even t W36 hpr k, HeckePrime3.W_pow3 W36 w36_mul w36_one, w36_nPi t ⟨K, hK⟩] at h
  exact (congrArg Prod.fst h).symm

end Hecke36Rec

#print axioms Hecke36Rec.w36_one
#print axioms Hecke36Rec.ins_cj
#print axioms Hecke36Rec.w36_cj
#print axioms Hecke36Rec.pow_odd
#print axioms Hecke36Rec.pow_c3
#print axioms Hecke36Rec.c3_rec
#print axioms Hecke36Rec.c3_inert_odd
#print axioms Hecke36Rec.w36_nPi
#print axioms Hecke36Rec.c3_inert_even

#print axioms: 9 theorems, no axioms.

78.239 \(\mathbb Z[\sqrt {-2}]\) is Euclidean

Division with remainder, the gcd as a combination and Euclid’s lemma in \(\mathbb Z[\sqrt {-2}]\), \(N(a+b\sqrt {-2})=a^2+2b^2\).

\[ \beta \neq 0\ \Rightarrow \ \exists q,\ N(\alpha -q\beta )<N(\beta ). \]

lang/Sq2Euclid.lean

/-
  lang/Sq2Euclid — T48/T50, cycle 626: ℤ[√−2] is Euclidean: division with remainder, gcd as a combination, and Euclid's lemma.

  ℤ[√−2] = Quad.quad ℤ (−2) (pairs a + b√−2), N(a + b√−2) = a² + 2b², conjugate a − b√−2:
    nrm_cast       (nrm z : ℤ) = N(z) (nrm = |a|² + 2|b|² in ℕ);   en_mul   N(xy) = N(x)N(y)
    edivr          β ≠ 0:  α = qβ + ρ with nrm ρ < nrm β   (round αβ̄ / N(β) componentwise; 4N(r) ≤ 3N(β)²)
    egcd_exists    d = xα + yβ dividing α and β
    eeuclid        xα + yβ = 1 and α | βγ ⇒ α | γ
  The ℤ[√−2] analogue of GaussEuclid/EisEuclid.
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Quad
import lang.GaussEuclid

namespace Sq2Euclid

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + 2 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs z.1 * Int.natAbs z.1 + 2 * (Int.natAbs z.2 * Int.natAbs z.2)
def cj (z : Int × Int) : Int × Int := (z.1, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Quad.quad LibInt.intR (-2)).mul a c

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  show Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + 2 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2) = _
  rw [GaussEuclid.nas, GaussEuclid.nas]; rfl

theorem mul_cj (z : Int × Int) : (Quad.quad LibInt.intR (-2)).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * z.1 + -2 * (z.2 * -z.2) = z.1 * z.1 + 2 * (z.2 * z.2)
    lib_int
  · show z.1 * -z.2 + z.2 * z.1 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Quad.quad LibInt.intR (-2)).mul x y) = en x * en y := by
  show (x.1 * y.1 + -2 * (x.2 * y.2)) * (x.1 * y.1 + -2 * (x.2 * y.2)) + 2 * ((x.1 * y.2 + x.2 * y.1) * (x.1 * y.2 + x.2 * y.1)) =
    (x.1 * x.1 + 2 * (x.2 * x.2)) * (y.1 * y.1 + 2 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show z.1 * z.1 + 2 * (-z.2 * -z.2) = z.1 * z.1 + 2 * (z.2 * z.2)
  lib_int

/-- **Division with remainder in ℤ[ω].** -/
theorem edivr (α β : Int × Int) (M : Nat) (hβ : nrm β = M + 1) :
    ∃ q, nrm ((Quad.quad LibInt.intR (-2)).add α ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul q β))) < M + 1 := by
  have hN : en β = Int.ofNat (M + 1) := by rw [← nrm_cast, hβ]
  match GaussEuclid.rnd ((Quad.quad LibInt.intR (-2)).mul α (cj β)).1 M, GaussEuclid.rnd ((Quad.quad LibInt.intR (-2)).mul α (cj β)).2 M with
  | ⟨q1, r1, h1, b1⟩, ⟨q2, r2, h2, b2⟩ =>
    refine ⟨(q1, q2), ?_⟩
    generalize hρ : (Quad.quad LibInt.intR (-2)).add α ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul (q1, q2) β)) = ρ
    have hρc : (Quad.quad LibInt.intR (-2)).mul ρ (cj β) = (r1, r2) := by
      rw [← hρ]
      have e : (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).add α ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul (q1, q2) β))) (cj β) = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul α (cj β)) ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul (q1, q2) ((Quad.quad LibInt.intR (-2)).mul β (cj β)))) := by lib_ring
      rw [e, mul_cj, hN]
      apply Prod.ext
      · show ((Quad.quad LibInt.intR (-2)).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1) + -2 * (q2 * 0)) = r1
        rw [h1]; lib_int
      · show ((Quad.quad LibInt.intR (-2)).mul α (cj β)).2 + -(q1 * 0 + q2 * Int.ofNat (M + 1)) = r2
        rw [h2]; lib_int
    have hn : en ρ * Int.ofNat (M + 1) = en (r1, r2) := by
      rw [← hN, ← en_cj β, ← en_mul, hρc]
    have hnN : nrm ρ * (M + 1) = nrm (r1, r2) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm ρ) * Int.ofNat (M + 1) = Int.ofNat (nrm (r1, r2))
      rw [nrm_cast, nrm_cast]; exact hn
    have hb : nrm (r1, r2) = Int.natAbs r1 * Int.natAbs r1 + 2 * (Int.natAbs r2 * Int.natAbs r2) := rfl
    generalize Int.natAbs r1 = A at hb b1
    generalize Int.natAbs r2 = B at hb b2
    have hle : (M + 1) * (4 * nrm ρ) ≤ (M + 1) * (3 * (M + 1)) := by
      calc (M + 1) * (4 * nrm ρ) = 4 * nrm (r1, r2) := by rw [← hnN]; lib_nat
        _ = (2 * A) * (2 * A) + (2 * B) * (2 * B) + (2 * B) * (2 * B) := by rw [hb]; lib_nat
        _ ≤ (M + 1) * (M + 1) + (M + 1) * (M + 1) + (M + 1) * (M + 1) :=
          Nat.add_le_add (Nat.add_le_add (Nat.mul_le_mul b1 b1) (Nat.mul_le_mul b2 b2)) (Nat.mul_le_mul b2 b2)
        _ = (M + 1) * (3 * (M + 1)) := by lib_nat
    have h4 := Nat.le_of_mul_le_mul_left hle (Nat.zero_lt_succ M)
    cases Nat.lt_or_ge (nrm ρ) (M + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      have : 4 * (M + 1) ≤ 3 * (M + 1) := Nat.le_trans (Nat.mul_le_mul_left 4 h) h4
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (show 3 * (M + 1) < 4 * (M + 1) by
        rw [show 4 * (M + 1) = 3 * (M + 1) + (M + 1) by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)) this)

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Quad.quad LibInt.intR (-2)).zero := by
  have h' : Int.natAbs z.1 * Int.natAbs z.1 + 2 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := h
  have h1 : Int.natAbs z.1 * Int.natAbs z.1 = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h')) (Nat.zero_le _)
  have h2 : 2 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h')) (Nat.zero_le _)
  have h2' : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by cases nat_mul_zero _ _ h2 with | inl e => exact Nat.noConfusion e | inr e => exact e
  have a1 : z.1 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ h1 with | inl e => exact e | inr e => exact e)
  have a2 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ h2' with | inl e => exact e | inr e => exact e)
  exact Prod.ext a1 a2

theorem egcd_base (α β : Int × Int) (hb : β = (Quad.quad LibInt.intR (-2)).zero) :
    ∃ d x y, d = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul x α) ((Quad.quad LibInt.intR (-2)).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Quad.quad LibInt.intR (-2)).one, (Quad.quad LibInt.intR (-2)).zero, by rw [hb]; lib_ring, ⟨(Quad.quad LibInt.intR (-2)).one, by lib_ring⟩, ⟨(Quad.quad LibInt.intR (-2)).zero, by rw [hb]; lib_ring⟩⟩

/-- **gcd as a combination in ℤ[ω].** -/
theorem egcd_exists : ∀ G (α β : Int × Int), nrm β ≤ G →
    ∃ d x y, d = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul x α) ((Quad.quad LibInt.intR (-2)).mul y β) ∧ Dvd' d α ∧ Dvd' d β
  | 0, α, β, hG => egcd_base α β (nrm_zero β (Nat.le_antisymm hG (Nat.zero_le _)))
  | G + 1, α, β, hG => by
    cases h : nrm β with
    | zero => exact egcd_base α β (nrm_zero β h)
    | succ M =>
        match edivr α β M h with
        | ⟨q, hq⟩ =>
          have hρ : nrm ((Quad.quad LibInt.intR (-2)).add α ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul q β))) ≤ G :=
            Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hq (Nat.le_trans (Nat.le_of_eq h.symm) hG))
          match egcd_exists G β _ hρ with
          | ⟨d, x, y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
            refine ⟨d, y, (Quad.quad LibInt.intR (-2)).add x ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul y q)), by rw [hd]; lib_ring, ⟨(Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul q c1) c2, ?_⟩, ⟨c1, hc1⟩⟩
            have e : α = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul q β) ((Quad.quad LibInt.intR (-2)).add α ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul q β))) := by lib_ring
            rw [e, hc2, hc1]
            lib_ring

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul x α) ((Quad.quad LibInt.intR (-2)).mul y β) = (Quad.quad LibInt.intR (-2)).one) (h : Dvd' α ((Quad.quad LibInt.intR (-2)).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul x γ) ((Quad.quad LibInt.intR (-2)).mul y c), by
      calc γ = (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul x α) ((Quad.quad LibInt.intR (-2)).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul x α) γ) ((Quad.quad LibInt.intR (-2)).mul y ((Quad.quad LibInt.intR (-2)).mul β γ)) := by lib_ring
        _ = (Quad.quad LibInt.intR (-2)).mul α ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul x γ) ((Quad.quad LibInt.intR (-2)).mul y c)) := by rw [hc]; lib_ring⟩

end Sq2Euclid

#print axioms Sq2Euclid.nrm_cast
#print axioms Sq2Euclid.mul_cj
#print axioms Sq2Euclid.en_mul
#print axioms Sq2Euclid.en_cj
#print axioms Sq2Euclid.edivr
#print axioms Sq2Euclid.nrm_zero
#print axioms Sq2Euclid.egcd_base
#print axioms Sq2Euclid.egcd_exists
#print axioms Sq2Euclid.eeuclid

#print axioms: 9 theorems, no axioms.

78.240 Coprime splitting in \(\mathbb Z[\sqrt {-2}]\)

An element of \(\mathbb Z[\sqrt {-2}]\) whose norm is a product of coprime factors splits accordingly.

\[ N(\gamma )=mn,\ sm+tn=1\ \Rightarrow \ \gamma =\alpha \beta ,\ N(\alpha )=m,\ N(\beta )=n. \]

lang/Sq2Coprime.lean

/-
  lang/Sq2Coprime — T48/T50, cycle 627: splitting by coprime norms in ℤ[√−2].

  If N(γ) = mn with sm + tn = 1 (m, n ≥ 1), then γ = αβ with N(α) = m, N(β) = n (N(a + b√−2) = a² + 2b²):
  α = gcd(γ, m), e = gcd(γ, n) (Sq2Euclid.egcd_exists), αe | γ by Euclid's lemma, γ | αe since mn = γγ̄, so γ = αe·v with v a unit;
  then N(α) | m, N(e) | n and N(α)N(e) = mn give N(α) = m. (Port of GaussCoprime.coprime_split.)
    en_emb, emb_mul, en_dvd, emb_add, en_zero_imp, cancel_left, en_nonneg_one, ecoprime_split
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Sq2Euclid
import lang.GaussCoprime

namespace Sq2Coprime

open FTAChain DetChain LibInt Sq2Euclid

def emb (m : Int) : Int × Int := (m, 0)

theorem en_emb (m : Int) : en (emb m) = m * m := by show m * m + 2 * (0 * 0) = m * m; lib_int

theorem emb_mul (a b : Int) : (Quad.quad LibInt.intR (-2)).mul (emb a) (emb b) = emb (a * b) := by
  apply Prod.ext
  · show a * b + -2 * (0 * 0) = a * b; lib_int
  · show a * 0 + 0 * b = 0; lib_int

theorem en_dvd (d γ : Int × Int) (h : Dvd' d γ) : ∃ k, en γ = en d * k :=
  match h with
  | ⟨c, hc⟩ => ⟨en c, by rw [hc, en_mul]⟩

theorem emb_add (a b : Int) : emb (a + b) = (Quad.quad LibInt.intR (-2)).add (emb a) (emb b) := by
  apply Prod.ext
  · rfl
  · show (0 : Int) = 0 + 0; rfl

theorem en_zero_imp (z : Int × Int) (h : en z = 0) : z = (Quad.quad LibInt.intR (-2)).zero := by
  apply nrm_zero
  apply Int.ofNat.inj
  rw [nrm_cast]; exact h

theorem cancel_left (γ z : Int × Int) (hγ : en γ ≠ 0) (h : (Quad.quad LibInt.intR (-2)).mul γ z = (Quad.quad LibInt.intR (-2)).zero) : z = (Quad.quad LibInt.intR (-2)).zero := by
  have : en γ * en z = 0 := by rw [← en_mul, h]; rfl
  cases int_noZD _ _ this with
  | inl e => exact absurd e hγ
  | inr e => exact en_zero_imp z e

theorem en_nonneg_one (z : Int × Int) (h : en z = -1) : False := by
  rw [← nrm_cast] at h
  exact Int.noConfusion h

/-- **Coprime splitting**: N(γ) = mn, sm + tn = 1 ⇒ γ = αβ with N(α) = m, N(β) = n. -/
theorem ecoprime_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) :
    ∃ α β, γ = (Quad.quad LibInt.intR (-2)).mul α β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match egcd_exists _ γ (emb (Int.ofNat (m + 1))) (Nat.le_refl _), egcd_exists _ γ (emb (Int.ofNat (n + 1))) (Nat.le_refl _) with
  | ⟨d, x, y, hd, ⟨u, hu⟩, ⟨c1, hc1⟩⟩, ⟨e, x', y', he, ⟨u', hu'⟩, ⟨c2, hc2⟩⟩ =>
    have hco : (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (emb t) c2) e) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (emb s) c1) d) = (Quad.quad LibInt.intR (-2)).one := by
      have h1 : (Quad.quad LibInt.intR (-2)).one = emb (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; rfl
      rw [h1, emb_add, ← emb_mul, ← emb_mul, hc1, hc2]; lib_ring
    -- e | u, so γ = d e v
    match eeuclid e d u _ _ hco ⟨u', by rw [← hu, hu']⟩ with
    | ⟨v, hv⟩ =>
    have hγdev : γ = (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul d e) v := by rw [hu, hv]; lib_ring
    -- γ | d e
    have hmn : (Quad.quad LibInt.intR (-2)).mul γ (cj γ) = emb (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by
      rw [mul_cj, hγ]; rfl
    have hW : (Quad.quad LibInt.intR (-2)).mul d e = (Quad.quad LibInt.intR (-2)).mul γ ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul x x') γ) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul x y') (emb (Int.ofNat (n + 1))))) ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).m  -- (+159 chars; full line in the Zenodo source)
      rw [hd, he]
      have : (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul y y') ((Quad.quad LibInt.intR (-2)).mul γ (cj γ)) = (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul y y') ((Quad.quad LibInt.intR (-2)).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1)))) := by rw [hmn, emb_mul]
      calc (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul x γ) ((Quad.quad LibInt.intR (-2)).mul y (emb (Int.ofNat (m + 1))))) ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul x' γ) ((Quad.quad LibInt.intR (-2)).mul y' (emb (Int.ofNat (n + 1)))))
          = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul γ ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul x x') γ) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul x y') (emb (Int.ofNat (n + 1))))) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul y x'  -- (+30 chars; full line in the Zenodo source)
              ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul y y') ((Quad.quad LibInt.intR (-2)).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1))))) := by lib_ring
        _ = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul γ ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul x x') γ) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul x y') (emb (Int.ofNat (n + 1))))) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul y x'  -- (+30 chars; full line in the Zenodo source)
              ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul y y') ((Quad.quad LibInt.intR (-2)).mul γ (cj γ))) := by rw [this]
        _ = _ := by lib_ring
    generalize (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul x x') γ) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul x y') (emb (Int.ofNat (n + 1))))) ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul y x') (emb (Int.ofNat (m + 1)))) ((Qu  -- (+88 chars; full line in the Zenodo source)
    have hγ0 : en γ ≠ 0 := by rw [hγ]; exact fun e => Nat.noConfusion (Int.ofNat.inj e)
    have hWv : (Quad.quad LibInt.intR (-2)).mul W v = (Quad.quad LibInt.intR (-2)).one := by
      have h0 : (Quad.quad LibInt.intR (-2)).mul γ ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul W v) ((Quad.quad LibInt.intR (-2)).neg (Quad.quad LibInt.intR (-2)).one)) = (Quad.quad LibInt.intR (-2)).zero := by
        rw [show (Quad.quad LibInt.intR (-2)).mul γ ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul W v) ((Quad.quad LibInt.intR (-2)).neg (Quad.quad LibInt.intR (-2)).one)) = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul γ W) v) ((Quad.quad LibInt.intR (-2)).neg γ) by lib_ring, ← hW, ← hγdev]; lib_ring
      have := cancel_left γ _ hγ0 h0
      calc (Quad.quad LibInt.intR (-2)).mul W v = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul W v) ((Quad.quad LibInt.intR (-2)).neg (Quad.quad LibInt.intR (-2)).one)) (Quad.quad LibInt.intR (-2)).one := by lib_ring
        _ = (Quad.quad LibInt.intR (-2)).one := by rw [this]; lib_ring
    have hv1 : en v = 1 := by
      have : en W * en v = 1 := by rw [← en_mul, hWv]; rfl
      cases GaussCoprime.int_unit _ _ (by rw [show en v * en W = en W * en v by lib_int]; exact this) with
      | inl h => exact h
      | inr h => exact absurd h (en_nonneg_one v)
    -- norms
    have hN : en d * en e = Int.ofNat (m + 1) * Int.ofNat (n + 1) := by
      have := congrArg en hγdev
      rw [en_mul, en_mul, hv1, hγ] at this
      rw [show en d * en e = en d * en e * 1 by lib_int, ← this]; rfl
    have hdm : ∃ a, Int.ofNat (m + 1) = en d * a := by
      have k1 : Int.ofNat (m + 1) * Int.ofNat (m + 1) = en d * en c1 := by rw [← en_emb, hc1, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en d * en u := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu, en_mul]
      exact ⟨s * en c1 + t * en u, by
        calc Int.ofNat (m + 1) = Int.ofNat (m + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = s * (Int.ofNat (m + 1) * Int.ofNat (m + 1)) + t * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en d * (s * en c1 + t * en u) := by rw [k1, k2]; lib_int⟩
    have hen' : ∃ b, Int.ofNat (n + 1) = en e * b := by
      have k1 : Int.ofNat (n + 1) * Int.ofNat (n + 1) = en e * en c2 := by rw [← en_emb, hc2, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en e * en u' := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu', en_mul]
      exact ⟨t * en c2 + s * en u', by
        calc Int.ofNat (n + 1) = Int.ofNat (n + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = t * (Int.ofNat (n + 1) * Int.ofNat (n + 1)) + s * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en e * (t * en c2 + s * en u') := by rw [k1, k2]; lib_int⟩
    match hdm, hen' with
    | ⟨a, ha⟩, ⟨b, hb⟩ =>
      have hab : a * b = 1 := by
        have h0 : (en d * en e) * (a * b + -1) = 0 := by
          rw [show (en d * en e) * (a * b + -1) = (en d * a) * (en e * b) + -(en d * en e) by lib_int, ← ha, ← hb, hN]; lib_int
        cases int_noZD _ _ h0 with
        | inl e0 => exact absurd (by rw [hN] at e0; exact e0) (fun e1 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = 0 => Nat.noConfusion (Int.ofNat.inj e1))
        | inr e0 => calc a * b = (a * b + -1) + 1 := by lib_int
            _ = 1 := by rw [e0]; rfl
      have pos : ∀ (k : Nat) (z : Int × Int) (c : Int), Int.ofNat (k + 1) = en z * c → c = 1 ∨ c = -1 → c = 1 := fun k z c h hc => by
        cases hc with
        | inl h1 => exact h1
        | inr h1 =>
          exfalso
          rw [h1, ← nrm_cast] at h
          have : Int.ofNat (k + 1) + Int.ofNat (nrm z) = 0 := by rw [h]; lib_int
          have h2 : k + nrm z + 1 = 0 := by rw [show k + nrm z + 1 = k + 1 + nrm z by lib_nat]; exact Int.ofNat.inj (show Int.ofNat (k + 1 + nrm z) = Int.ofNat 0 from this)
          exact Nat.noConfusion h2
      have a1 := pos m d a ha (GaussCoprime.int_unit a b hab)
      have b1 := pos n e b hb (GaussCoprime.int_unit b a (by rw [show b * a = a * b by lib_int]; exact hab))
      refine ⟨d, (Quad.quad LibInt.intR (-2)).mul e v, by rw [hγdev]; lib_ring, by rw [ha, a1]; lib_int, by rw [en_mul, hv1, hb, b1]⟩

end Sq2Coprime

#print axioms Sq2Coprime.en_emb
#print axioms Sq2Coprime.emb_mul
#print axioms Sq2Coprime.en_dvd
#print axioms Sq2Coprime.emb_add
#print axioms Sq2Coprime.en_zero_imp
#print axioms Sq2Coprime.cancel_left
#print axioms Sq2Coprime.en_nonneg_one
#print axioms Sq2Coprime.ecoprime_split

#print axioms: 8 theorems, no axioms.

78.241 Unique primary splitting in \(\mathbb Z[\sqrt {-2}]\)

The primary class \(\langle 1+\sqrt {-2}\rangle \) mod \(4\) is closed under products and meets \(\{\pm \alpha \}\) once for \(N(\alpha )\) odd.

\[ \gamma \ \text {primary},\ N(\gamma )=mn\ \Rightarrow \ \exists !\,(\alpha ,\beta )\ \text {primary},\ \gamma =\alpha \beta ,\ N(\alpha )=m. \]

lang/Sq2Unique.lean

/-
  lang/Sq2Unique — T48/T50, cycle 628: primary elements of ℤ[√−2] and the unique primary splitting.

  Prim2 z :⇔ (b = 2s, a = 4e + 1 − 2s) ∨ (b = 4f + 1, a odd)    for z = a + b√−2
  (the subgroup ⟨1 + √−2⟩ of (ℤ[√−2]/4)^×, of index 2 and not containing −1):
    prim2_mul     closed under products (explicit witnesses)
    normalize2    N(α) odd ⇒ α or −α is primary
    unit_pm       N(u) = 1 ⇒ u = ±1;   unit_one2   α, uα primary, N(u) = 1 ⇒ u = 1
    prim2_quot, uniq_split2, prim2_split    as in GaussUnique / EisUnique3 (with Sq2Coprime)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Sq2Coprime
import lang.GaussUnique
import lang.W36Mul

namespace Sq2Unique

open FTAChain DetChain LibInt Sq2Euclid

def Prim2 (z : Int × Int) : Prop := (∃ s e : Int, z.2 = 2 * s ∧ z.1 = 4 * e + 1 - 2 * s) ∨ (∃ f r : Int, z.2 = 4 * f + 1 ∧ z.1 = 2 * r + 1)

theorem mul1 (x y : Int × Int) : ((Quad.quad LibInt.intR (-2)).mul x y).1 = x.1 * y.1 + -2 * (x.2 * y.2) := rfl
theorem mul2 (x y : Int × Int) : ((Quad.quad LibInt.intR (-2)).mul x y).2 = x.1 * y.2 + x.2 * y.1 := rfl

theorem prim2_AB (a b c d : Int) (s e f r : Int) (hb : b = 2 * s) (ha : a = 4 * e + 1 - 2 * s) (hd : d = 4 * f + 1) (hc : c = 2 * r + 1) :
    Prim2 ((Quad.quad LibInt.intR (-2)).mul (a, b) (c, d)) :=
  Or.inr ⟨e + 4 * e * f + f + -2 * f * s + r * s, 2 * e + 4 * e * r + -8 * f * s + r + -2 * r * s + -3 * s,
    by rw [mul2]; show a * d + b * c = _; rw [ha, hb, hc, hd]; lib_int,
    by rw [mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, hc, hd]; lib_int⟩

/-- **The primary class is closed under products.** -/
theorem prim2_mul (x y : Int × Int) (hx : Prim2 x) (hy : Prim2 y) : Prim2 ((Quad.quad LibInt.intR (-2)).mul x y) := by
  obtain ⟨a, b⟩ := x
  obtain ⟨c, d⟩ := y
  rcases hx with ⟨s, e, hb, ha⟩ | ⟨f, r, hb, ha⟩ <;> rcases hy with ⟨s2, e2, hd, hc⟩ | ⟨f2, r2, hd, hc⟩
  · exact Or.inl ⟨4 * e * s2 + 4 * e2 * s + s + -4 * s * s2 + s2, e + 4 * e * e2 + e2 + -3 * s * s2,
      by rw [mul2]; show a * d + b * c = _; rw [show a = 4 * e + 1 - 2 * s from ha, show b = 2 * s from hb, show c = 4 * e2 + 1 - 2 * s2 from hc, show d = 2 * s2 from hd]; lib_int,
      by rw [mul1]; show a * c + -2 * (b * d) = _; rw [show a = 4 * e + 1 - 2 * s from ha, show b = 2 * s from hb, show c = 4 * e2 + 1 - 2 * s2 from hc, show d = 2 * s2 from hd]; lib_int⟩
  · exact prim2_AB a b c d s e f2 r2 hb ha hd hc
  · rw [(Quad.quad LibInt.intR (-2)).mul_comm]; exact prim2_AB c d a b s2 e2 f r hd hc hb ha
  · exact Or.inl ⟨1 + 2 * f + 4 * f * r2 + 2 * f2 + 4 * f2 * r + r + r2, -f + -8 * f * f2 + 2 * f * r2 + -f2 + 2 * f2 * r + r + r * r2 + r2,
      by rw [mul2]; show a * d + b * c = _; rw [show a = 2 * r + 1 from ha, show b = 4 * f + 1 from hb, show c = 2 * r2 + 1 from hc, show d = 4 * f2 + 1 from hd]; lib_int,
      by rw [mul1]; show a * c + -2 * (b * d) = _; rw [show a = 2 * r + 1 from ha, show b = 4 * f + 1 from hb, show c = 2 * r2 + 1 from hc, show d = 4 * f2 + 1 from hd]; lib_int⟩

theorem neg_mul' (z : Int × Int) : (Quad.quad LibInt.intR (-2)).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -2 * (0 * z.2) = -z.1; lib_int) (by show -1 * z.2 + 0 * z.1 = -z.2; lib_int)

/-- **Normalisation**: N(α) odd ⇒ α or −α primary. -/
theorem normalize2 (α : Int × Int) (k : Int) (h : en α = 2 * k + 1) : ∃ u, en u = 1 ∧ Prim2 ((Quad.quad LibInt.intR (-2)).mul u α) := by
  obtain ⟨a, b⟩ := α
  have h' : a * a + 2 * (b * b) = 2 * k + 1 := h
  rcases GaussInt.int_even_odd a with ⟨w, hw⟩ | ⟨w, hw⟩
  · exact (GaussInt.int_odd_ne_even k (2 * w * w + b * b) (by rw [← h', hw]; lib_int)).elim
  rcases GaussInt.int_even_odd b with ⟨c, hc⟩ | ⟨c, hc⟩
  · rcases GaussUnique.odd4 (w + c) with ⟨E, hE⟩ | ⟨E, hE⟩
    · exact ⟨(Quad.quad LibInt.intR (-2)).one, rfl, by rw [(Quad.quad LibInt.intR (-2)).one_mul]; exact Or.inl ⟨c, E, hc, by
        calc a = (2 * (w + c) + 1) - 2 * c := by rw [hw]; lib_int
          _ = _ := by rw [hE]⟩⟩
    · refine ⟨(-1, 0), rfl, ?_⟩
      rw [neg_mul']
      exact Or.inl ⟨-c, -E + -1, by show -b = _; rw [hc]; lib_int, by
        show -a = _
        calc -a = -(2 * (w + c) + 1) + 2 * c := by rw [hw]; lib_int
          _ = _ := by rw [hE]; lib_int⟩
  · rcases GaussUnique.odd4 c with ⟨F, hF⟩ | ⟨F, hF⟩
    · exact ⟨(Quad.quad LibInt.intR (-2)).one, rfl, by rw [(Quad.quad LibInt.intR (-2)).one_mul]; exact Or.inr ⟨F, w, by rw [hc, hF], hw⟩⟩
    · refine ⟨(-1, 0), rfl, ?_⟩
      rw [neg_mul']
      exact Or.inr ⟨-F + -1, -w + -1, by show -b = _; rw [hc, hF]; lib_int, by show -a = _; rw [hw]; lib_int⟩

theorem unit_pm (u : Int × Int) (h : en u = 1) : u = (Quad.quad LibInt.intR (-2)).one ∨ u = (-1, 0) := by
  obtain ⟨a, b⟩ := u
  have h' : a * a + 2 * (b * b) = 1 := h
  have hn : Int.natAbs a * Int.natAbs a + 2 * (Int.natAbs b * Int.natAbs b) = 1 := Int.ofNat.inj (by
    show Int.ofNat (Int.natAbs a * Int.natAbs a) + 2 * Int.ofNat (Int.natAbs b * Int.natAbs b) = 1
    rw [GaussEuclid.nas, GaussEuclid.nas, h'])
  have hB : Int.natAbs b * Int.natAbs b = 0 := by
    cases hb : Int.natAbs b * Int.natAbs b with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs a * Int.natAbs a + 2 * (t + 1) = (Int.natAbs a * Int.natAbs a + 2 * t + 1) + 1 by lib_nat] at hn; exact Nat.noConfusion (Nat.succ.inj hn)
  have b0 : b = 0 := GaussEuclid.natAbs_zero b (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at hn
  rcases W36Mul.abs_one a (W36Mul.nat_sq_one _ hn) with e | e
  · exact Or.inl (by rw [e, b0]; rfl)
  · exact Or.inr (by rw [e, b0])

/-- **A unit taking a primary element to a primary element is 1.** -/
theorem unit_one2 (u α : Int × Int) (hu : en u = 1) (pα : Prim2 α) (pu : Prim2 ((Quad.quad LibInt.intR (-2)).mul u α)) : u = (Quad.quad LibInt.intR (-2)).one := by
  rcases unit_pm u hu with e | e
  · exact e
  exfalso
  rw [e, neg_mul'] at pu
  obtain ⟨a, b⟩ := α
  rcases pα with ⟨s, e1, hb, ha⟩ | ⟨f, r, hb, ha⟩ <;> rcases pu with ⟨s2, e2, hb2, ha2⟩ | ⟨f2, r2, hb2, ha2⟩
  · have hb' : b = 2 * s := hb
    have hs : s2 = -s := W36Mul.two_cancel_int s2 (-s) (by rw [← show -b = 2 * s2 from hb2, hb']; lib_int)
    have ha' : a = 4 * e1 + 1 - 2 * s := ha
    have ha2' : -a = 4 * e2 + 1 - 2 * s2 := ha2
    rw [hs] at ha2'
    have hsum : 2 * (2 * (e1 + e2) + 1) = 2 * 0 := by
      calc 2 * (2 * (e1 + e2) + 1) = (4 * e1 + 1 - 2 * s) + (4 * e2 + 1 - 2 * -s) := by lib_int
        _ = a + -a := by rw [← ha', ← ha2']
        _ = 2 * 0 := by lib_int
    exact GaussInt.int_odd_ne_even (e1 + e2) 0 (W36Mul.two_cancel_int _ _ hsum)
  · have h1 : b = 2 * s := hb
    have h2 : -b = 4 * f2 + 1 := hb2
    exact GaussInt.int_odd_ne_even (2 * f2) (-s) (by
      calc 2 * (2 * f2) + 1 = -b := by rw [h2]; lib_int
        _ = 2 * -s := by rw [h1]; lib_int)
  · have h1 : b = 4 * f + 1 := hb
    have h2 : -b = 2 * s2 := hb2
    exact GaussInt.int_odd_ne_even (2 * f) (-s2) (by
      calc 2 * (2 * f) + 1 = b := by rw [h1]; lib_int
        _ = -(-b) := by lib_int
        _ = 2 * -s2 := by rw [h2]; lib_int)
  · have h1 : b = 4 * f + 1 := hb
    have h2 : -b = 4 * f2 + 1 := hb2
    have hsum : 2 * (2 * (f + f2) + 1) = 2 * 0 := by
      calc 2 * (2 * (f + f2) + 1) = (4 * f + 1) + (4 * f2 + 1) := by lib_int
        _ = b + -b := by rw [← h1, ← h2]
        _ = 2 * 0 := by lib_int
    exact GaussInt.int_odd_ne_even (f + f2) 0 (W36Mul.two_cancel_int _ _ hsum)

/-- y is primary when x and xy are (N(y) odd). -/
theorem prim2_quot (x y : Int × Int) (hy : ∃ k, en y = 2 * k + 1) (hx : Prim2 x) (hxy : Prim2 ((Quad.quad LibInt.intR (-2)).mul x y)) : Prim2 y := by
  obtain ⟨k, hk⟩ := hy
  obtain ⟨u, hu, hpu⟩ := normalize2 y k hk
  have h1 : Prim2 ((Quad.quad LibInt.intR (-2)).mul u ((Quad.quad LibInt.intR (-2)).mul x y)) := by
    rw [show (Quad.quad LibInt.intR (-2)).mul u ((Quad.quad LibInt.intR (-2)).mul x y) = (Quad.quad LibInt.intR (-2)).mul x ((Quad.quad LibInt.intR (-2)).mul u y) by lib_ring]
    exact prim2_mul _ _ hx hpu
  have hu1 := unit_one2 u _ hu hxy h1
  rw [hu1, (Quad.quad LibInt.intR (-2)).one_mul] at hpu
  exact hpu

/-- **Uniqueness of the primary splitting.** -/
theorem uniq_split2 (α β α' β' : Int × Int) (M N s t : Int) (hα : en α = M) (hα' : en α' = M) (hβ' : en β' = N)
    (hst : s * M + t * N = 1) (hM : M ≠ 0) (pα : Prim2 α) (pα' : Prim2 α') (h : (Quad.quad LibInt.intR (-2)).mul α β = (Quad.quad LibInt.intR (-2)).mul α' β') :
    α = α' ∧ β = β' := by
  have hco : (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb s) (cj α)) α) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb t) (cj β')) β') = (Quad.quad LibInt.intR (-2)).one := by
    rw [show (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb s) (cj α)) α) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb t) (cj β')) β') = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb s) ((Quad.quad LibInt.intR (-2)).mul α (cj α))) ((Quad.quad LibInt.intR  -- (+89 chars; full line in the Zenodo source)
      mul_cj, mul_cj, hα, hβ']
    show (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb s) (Sq2Coprime.emb M)) ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb t) (Sq2Coprime.emb N)) = _
    rw [Sq2Coprime.emb_mul, Sq2Coprime.emb_mul, ← Sq2Coprime.emb_add, hst]; rfl
  match eeuclid α β' α' _ _ hco ⟨β, by rw [show (Quad.quad LibInt.intR (-2)).mul β' α' = (Quad.quad LibInt.intR (-2)).mul α' β' by lib_ring, ← h]⟩ with
  | ⟨w, hw⟩ =>
    have hgw : en w = 1 := by
      have e : M * en w = M * 1 := by
        calc M * en w = en α * en w := by rw [hα]
          _ = en α' := by rw [← en_mul, ← hw]
          _ = M * 1 := by rw [hα']; lib_int
      have e0 : M * (en w + -1) = 0 := by rw [show M * (en w + -1) = M * en w + -(M * 1) by lib_int, e]; lib_int
      cases int_noZD _ _ e0 with
      | inl h0 => exact absurd h0 hM
      | inr h0 => calc en w = (en w + -1) + 1 := by lib_int
          _ = 1 := by rw [h0]; rfl
    have hw1 : w = (Quad.quad LibInt.intR (-2)).one :=
      unit_one2 w α hgw pα (by rw [show (Quad.quad LibInt.intR (-2)).mul w α = α' by rw [hw]; lib_ring]; exact pα')
    have hαα : α = α' := by rw [hw, hw1]; lib_ring
    refine ⟨hαα, ?_⟩
    have h0 : (Quad.quad LibInt.intR (-2)).mul α ((Quad.quad LibInt.intR (-2)).add β ((Quad.quad LibInt.intR (-2)).neg β')) = (Quad.quad LibInt.intR (-2)).zero := by
      rw [show (Quad.quad LibInt.intR (-2)).mul α ((Quad.quad LibInt.intR (-2)).add β ((Quad.quad LibInt.intR (-2)).neg β')) = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul α β) ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul α β')) by lib_ring, h, ← hαα]; lib_ring
    have := Sq2Coprime.cancel_left α _ (by rw [hα]; exact hM) h0
    calc β = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).add β ((Quad.quad LibInt.intR (-2)).neg β')) β' := by lib_ring
      _ = β' := by rw [this]; lib_ring

/-- **Existence of the primary splitting.** -/
theorem prim2_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (hn : ∃ k, Int.ofNat (n + 1) = 2 * k + 1)
    (pγ : Prim2 γ) : ∃ α β, γ = (Quad.quad LibInt.intR (-2)).mul α β ∧ Prim2 α ∧ Prim2 β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  obtain ⟨km, hkm⟩ := hm
  match Sq2Coprime.ecoprime_split γ m n s t hγ hst with
  | ⟨α0, β0, h0, hα0, hβ0⟩ =>
    match normalize2 α0 km (by rw [hα0]; exact hkm) with
    | ⟨u, gu, pu⟩ =>
      have uc : (Quad.quad LibInt.intR (-2)).mul u (cj u) = (Quad.quad LibInt.intR (-2)).one := by rw [mul_cj, gu]; rfl
      have hβn : en ((Quad.quad LibInt.intR (-2)).mul (cj u) β0) = Int.ofNat (n + 1) := by
        rw [en_mul, show en (cj u) = en u from en_cj u, gu, hβ0]; lib_int
      have hsplit : γ = (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul u α0) ((Quad.quad LibInt.intR (-2)).mul (cj u) β0) := by
        rw [h0, show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul u α0) ((Quad.quad LibInt.intR (-2)).mul (cj u) β0) = (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul u (cj u)) ((Quad.quad LibInt.intR (-2)).mul α0 β0) by lib_ring, uc]; lib_ring
      refine ⟨(Quad.quad LibInt.intR (-2)).mul u α0, (Quad.quad LibInt.intR (-2)).mul (cj u) β0, hsplit, pu,
        prim2_quot _ _ (by rw [hβn]; exact hn) pu (by rw [← hsplit]; exact pγ),
        by rw [en_mul, gu, hα0]; lib_int, hβn⟩

end Sq2Unique

#print axioms Sq2Unique.mul1
#print axioms Sq2Unique.mul2
#print axioms Sq2Unique.prim2_AB
#print axioms Sq2Unique.prim2_mul
#print axioms Sq2Unique.neg_mul'
#print axioms Sq2Unique.normalize2
#print axioms Sq2Unique.unit_pm
#print axioms Sq2Unique.unit_one2
#print axioms Sq2Unique.prim2_quot
#print axioms Sq2Unique.uniq_split2
#print axioms Sq2Unique.prim2_split

#print axioms: 11 theorems, no axioms.

78.242 Hecke characters of \(\mathbb Q(\sqrt {-2})\): multiplicativity

For every multiplicative \(w\) on \(\mathbb Z[\sqrt {-2}]\) the theta coefficients over primary \(\alpha \) are multiplicative.

\[ c_w(n)=\sum _{\alpha \ \text {primary},\ N\alpha =n}w(\alpha ),\qquad c_w(mn)=c_w(m)\,c_w(n)\quad ((m,n)=(mn,2)=1). \]

lang/HeckeMult2.lean

/-
  lang/HeckeMult2 — T48/T50, cycle 629: **the theta series of every multiplicative weight on primary elements of ℤ[√−2] is
  multiplicative**: for w : ℤ[√−2] → ℤ[√−2] with w(αβ) = w(α)w(β) (e.g. w(α) = α^k, the Hecke characters of ℚ(√−2) of ∞-type k),
    c_w(n) = Σ_{α primary, N(α) = n} w(α)   satisfies   c_w(mn) = c_w(m) c_w(n)  for coprime odd m, n.
  Enumeration box2 n = HeckeMult.box n (|a|, |b| ≤ N). (Port of HeckeMult3 with Sq2Unique.)
    abs_le2, pd2_iff, cw2, hecke_mult2, hecke_mult2_pow
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Sq2Unique
import lang.HeckeMultW

namespace HeckeMult2

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Sq2Euclid Sq2Unique

abbrev box2 (n : Nat) : List (Int × Int) := HeckeMult.box n

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ n. -/
theorem abs_le2 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ n ∧ Int.natAbs z.2 ≤ n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have hn' : Int.natAbs z.1 * Int.natAbs z.1 + 2 * (Int.natAbs z.2 * Int.natAbs z.2) = n := hn
  constructor
  · exact Nat.le_trans (HeckeMult.le_sq _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq hn'))
  · exact Nat.le_trans (HeckeMult.le_sq _) (Nat.le_trans (Nat.le_trans (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_succ 1))) (Nat.le_add_left _ _)) (Nat.le_of_eq hn'))

def PD2 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim2 z

theorem prim2_iff (z : Int × Int) :
    ((ZMod.md 1 (Int.natAbs z.2) = 0 ∧ ZMod.md 3 (Int.natAbs (z.1 + z.2 - 1)) = 0) ∨ (ZMod.md 3 (Int.natAbs (z.2 - 1)) = 0 ∧ ZMod.md 1 (Int.natAbs (z.1 - 1)) = 0)) ↔ Prim2 z := by
  constructor
  · intro h
    rcases h with ⟨h1, h2⟩ | ⟨h1, h2⟩
    · obtain ⟨s, hs⟩ := (HeckeMult.idv_iff 1 _).mpr h1
      obtain ⟨e, he⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact Or.inl ⟨s, e, hs, by calc z.1 = (z.1 + z.2 - 1) + 1 - z.2 := by lib_int
                                  _ = _ := by rw [he, hs]; rfl⟩
    · obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h1
      obtain ⟨r, hr⟩ := (HeckeMult.idv_iff 1 _).mpr h2
      exact Or.inr ⟨f, r, by calc z.2 = (z.2 - 1) + 1 := by lib_int
                               _ = _ := by rw [hf]; rfl, by calc z.1 = (z.1 - 1) + 1 := by lib_int
                                                              _ = _ := by rw [hr]; rfl⟩
  · intro h
    rcases h with ⟨s, e, hs, he⟩ | ⟨f, r, hf, hr⟩
    · exact Or.inl ⟨(HeckeMult.idv_iff 1 _).mp ⟨s, hs⟩, (HeckeMult.idv_iff 3 _).mp ⟨e, by rw [he, hs]; show 4 * e + 1 - 2 * s + 2 * s - 1 = 4 * e; lib_int⟩⟩
    · exact Or.inr ⟨(HeckeMult.idv_iff 3 _).mp ⟨f, by rw [hf]; show 4 * f + 1 - 1 = 4 * f; lib_int⟩, (HeckeMult.idv_iff 1 _).mp ⟨r, by rw [hr]; show 2 * r + 1 - 1 = 2 * r; lib_int⟩⟩

instance (z : Int × Int) : Decidable (Prim2 z) := decidable_of_iff _ (prim2_iff z)

instance (n : Nat) (z : Int × Int) : Decidable (PD2 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum2 {A : Type} : ∀ l : List A, sumK (Quad.quad LibInt.intR (-2)) (fun _ => (Quad.quad LibInt.intR (-2)).zero) l = (Quad.quad LibInt.intR (-2)).zero
  | [] => rfl
  | _ :: l => by show (Quad.quad LibInt.intR (-2)).add (Quad.quad LibInt.intR (-2)).zero (sumK (Quad.quad LibInt.intR (-2)) (fun _ => (Quad.quad LibInt.intR (-2)).zero) l) = _; rw [zsum2 l]; exact (Quad.quad LibInt.intR (-2)).zero_add _

def I2 (n : Nat) (z : Int × Int) : Int × Int := if PD2 n z then (Quad.quad LibInt.intR (-2)).one else (Quad.quad LibInt.intR (-2)).zero

theorem I2_pos (n : Nat) (z : Int × Int) (h : PD2 n z) : I2 n z = (Quad.quad LibInt.intR (-2)).one := ite_pos' _ _ h
theorem I2_neg (n : Nat) (z : Int × Int) (h : ¬ PD2 n z) : I2 n z = (Quad.quad LibInt.intR (-2)).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw2 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Quad.quad LibInt.intR (-2)) (fun z => (Quad.quad LibInt.intR (-2)).mul (I2 n z) (w z)) (box2 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult2 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (hn : ∃ k, Int.ofNat (n + 1) = 2 * k + 1) :
    cw2 w ((m + 1) * (n + 1)) = (Quad.quad LibInt.intR (-2)).mul (cw2 w (m + 1)) (cw2 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD2 (m + 1) α → PD2 (n + 1) β → PD2 ((m + 1) * (n + 1)) ((Quad.quad LibInt.intR (-2)).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim2_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Quad.quad LibInt.intR (-2)).mul (cw2 w (m + 1)) (cw2 w (n + 1))
      = sumK (Quad.quad LibInt.intR (-2)) (fun α => sumK (Quad.quad LibInt.intR (-2)) (fun β => sumK (Quad.quad LibInt.intR (-2)) (fun γ => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I2 (m + 1) α) (I2 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ)) (box2 ((m + 1) * (n + 1)))) (box2 (n + 1))  -- (+22 chars; full line in the Zenodo source)
    unfold cw2; rw [sumK_mul]
    refine sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun β => ?_) _
    by_cases hα : PD2 (m + 1) α
    · by_cases hβ : PD2 (n + 1) β
      · rw [I2_pos _ _ hα, I2_pos _ _ hβ]
        have hb := abs_le2 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Quad.quad LibInt.intR (-2)) (fun γ => w γ) ((Quad.quad LibInt.intR (-2)).mul α β) _ hb.1 hb.2
        rw [show sumK (Quad.quad LibInt.intR (-2)) (fun γ => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one (Quad.quad LibInt.intR (-2)).one) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ)) (box2 ((m + 1) * (n + 1)))
            = sumK (Quad.quad LibInt.intR (-2)) (fun γ => (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β)) (w γ)) (box2 ((m + 1) * (n + 1))) from sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I2_neg _ _ hβ]
        rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I2 (m + 1) α) (Quad.quad LibInt.intR (-2)).zero) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ) = (Quad.quad LibInt.intR (-2)).zero by lib_ring)]
        rw [zsum2]; lib_ring
    · rw [I2_neg _ _ hα]
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).zero (I2 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ) = (Quad.quad LibInt.intR (-2)).zero by lib_ring)]
      rw [zsum2]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => sumK_swap (Quad.quad LibInt.intR (-2)) _ _ _)]
  rw [sumK_swap (Quad.quad LibInt.intR (-2))]
  unfold cw2
  refine sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => ?_) _
  have e1 : sumK (Quad.quad LibInt.intR (-2)) (fun α => sumK (Quad.quad LibInt.intR (-2)) (fun β => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I2 (m + 1) α) (I2 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ)) (box2 (n + 1))) (box2 (m + 1))
      = (Quad.quad LibInt.intR (-2)).mul (sumK (Quad.quad LibInt.intR (-2)) (fun α => sumK (Quad.quad LibInt.intR (-2)) (fun β => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I2 (m + 1) α) (I2 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (box2 (n + 1))) (box2 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD2 ((m + 1) * (n + 1)) γ
  · rw [I2_pos _ _ hγ]
    match prim2_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I2 (m + 1) α) (I2 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β)) = (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (delta (Quad.quad LibInt.intR (-2)) β β0) := by
        intro α β
        by_cases hαβ : PD2 (m + 1) α ∧ PD2 (n + 1) β ∧ γ = (Quad.quad LibInt.intR (-2)).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split2 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I2_pos _ _ hα, I2_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (delta (Quad.quad LibInt.intR (-2)) β β0) = (Quad.quad LibInt.intR (-2)).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD2 (m + 1) α
            · by_cases hβ : PD2 (n + 1) β
              · have hg : ¬ γ = (Quad.quad LibInt.intR (-2)).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I2_neg _ _ hβ]; lib_ring
            · rw [I2_neg _ _ hα]; lib_ring
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun β => pt α β) _)]
      have ba := abs_le2 α0 _ nα0
      have bb := abs_le2 β0 _ nβ0
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Quad.quad LibInt.intR (-2)) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => show (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (Quad.quad LibInt.intR (-2)).one = (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (Quad.quad LibInt.intR (-2)).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Quad.quad LibInt.intR (-2)) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I2_neg _ _ hγ]
    have pt : ∀ α β, (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I2 (m + 1) α) (I2 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β)) = (Quad.quad LibInt.intR (-2)).zero := by
      intro α β
      by_cases hα : PD2 (m + 1) α
      · by_cases hβ : PD2 (n + 1) β
        · have hg : ¬ γ = (Quad.quad LibInt.intR (-2)).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I2_neg _ _ hβ]; lib_ring
      · rw [I2_neg _ _ hα]; lib_ring
    rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => zsum2 _), zsum2]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult2_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (hn : ∃ k, Int.ofNat (n + 1) = 2 * k + 1) :
    cw2 (fun z => Frobenius.pw (Quad.quad LibInt.intR (-2)) z k) ((m + 1) * (n + 1)) =
      (Quad.quad LibInt.intR (-2)).mul (cw2 (fun z => Frobenius.pw (Quad.quad LibInt.intR (-2)) z k) (m + 1)) (cw2 (fun z => Frobenius.pw (Quad.quad LibInt.intR (-2)) z k) (n + 1)) :=
  hecke_mult2 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult2

#print axioms HeckeMult2.abs_le2
#print axioms HeckeMult2.prim2_iff
#print axioms HeckeMult2.zsum2
#print axioms HeckeMult2.I2_pos
#print axioms HeckeMult2.I2_neg
#print axioms HeckeMult2.hecke_mult2
#print axioms HeckeMult2.hecke_mult2_pow
#print axioms HeckeMult2.cw2

#print axioms: 8 theorems, no axioms.

78.243 The primary class of 256a in \(\mathbb Z[\sqrt {-2}]\)

The conjugation-stable complement of \(\{\pm 1\}\) in \((\mathbb Z[\sqrt {-2}]/4\sqrt {-2})^\times \) is closed under products and picks one of \(\pm \alpha \).

\[ H_8=\{a\equiv 1,3\ (8),\ 4\mid b\}\cup \{a\equiv 5,7\ (8),\ 4\nmid b\}. \]

lang/Sq2Prim8.lean

/-
  lang/Sq2Prim8 — T48/T50, cycle 630: **the primary class of the Hecke character of y² = x³ + 4x² + 2x (256a) in ℤ[√−2]**.
  The class of Sq2Unique (mod 4) is not stable under conjugation, so its theta series is not that of a curve over ℚ; mod 4√−2 the
  conjugation-stable complement of {±1} is
    H8 = {a ≡ 1, 3 (mod 8), b ≡ 0 (mod 4)} ∪ {a ≡ 5, 7 (mod 8), b ≢ 0 (mod 4)}       (α = a + b√−2)
  (numerically: π ∈ H8 of norm p gives 2a = a_p(256a) for all split p < 120, scratchpad gen8.py).
    noM, mod8_odd, mod4      residue helpers
    prim8_mul                H8 closed under products (64 cases, witnesses generated)
    prim8_cj                 H8 closed under conjugation
    normalize8               N(α) odd ⇒ ±α ∈ H8
    unit_one8                N(u) = 1, α, uα ∈ H8 ⇒ u = 1
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Sq2Unique

namespace Sq2Prim8

open FTAChain DetChain LibInt Sq2Euclid Sq2Unique

/-- (m + 1)x = c + 1 with 0 < c + 1 < m + 1 is impossible. -/
theorem noM (m : Nat) (x : Int) (c d : Nat) (h : Int.ofNat (m + 1) * x = Int.ofNat (c + 1)) (hd : c + 1 + d + 1 = m + 1) : False := by
  cases x with
  | ofNat n =>
    have h' : (m + 1) * n = c + 1 := Int.ofNat.inj h
    cases n with
    | zero => exact Nat.noConfusion ((Nat.mul_zero _).symm.trans h')
    | succ n =>
      have hle : c + 1 + (d + 1) ≤ c + 1 := by
        calc c + 1 + (d + 1) = m + 1 := by rw [← hd]; lib_nat
          _ ≤ (m + 1) + (m + 1) * n := Nat.le_add_right _ _
          _ = (m + 1) * (n + 1) := by lib_nat
          _ = c + 1 := h'
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (Nat.lt_add_of_pos_right (Nat.zero_lt_succ d)) hle)
  | negSucc n => exact Int.noConfusion h

theorem mod8_odd (a b k : Int) (h : a * a + 2 * (b * b) = 2 * k + 1) : ∃ e, a = 8 * e + 1 ∨ a = 8 * e + 3 ∨ a = 8 * e + 5 ∨ a = 8 * e + 7 := by
  rcases GaussInt.int_even_odd a with ⟨w, hw⟩ | ⟨w, hw⟩
  · exact (GaussInt.int_odd_ne_even k (2 * w * w + b * b) (by rw [← h, hw]; lib_int)).elim
  rcases GaussInt.int_even_odd w with ⟨u, hu⟩ | ⟨u, hu⟩ <;> rcases GaussInt.int_even_odd u with ⟨v, hv⟩ | ⟨v, hv⟩
  · exact ⟨v, Or.inl (by rw [hw, hu, hv]; lib_int)⟩
  · exact ⟨v, Or.inr (Or.inr (Or.inl (by rw [hw, hu, hv]; lib_int)))⟩
  · exact ⟨v, Or.inr (Or.inl (by rw [hw, hu, hv]; lib_int))⟩
  · exact ⟨v, Or.inr (Or.inr (Or.inr (by rw [hw, hu, hv]; lib_int)))⟩

theorem mod4 (b : Int) : ∃ f, b = 4 * f + 0 ∨ b = 4 * f + 1 ∨ b = 4 * f + 2 ∨ b = 4 * f + 3 := by
  rcases GaussInt.int_even_odd b with ⟨w, hw⟩ | ⟨w, hw⟩ <;> rcases GaussInt.int_even_odd w with ⟨u, hu⟩ | ⟨u, hu⟩
  · exact ⟨u, Or.inl (by rw [hw, hu]; lib_int)⟩
  · exact ⟨u, Or.inr (Or.inr (Or.inl (by rw [hw, hu]; lib_int)))⟩
  · exact ⟨u, Or.inr (Or.inl (by rw [hw, hu]; lib_int))⟩
  · exact ⟨u, Or.inr (Or.inr (Or.inr (by rw [hw, hu]; lib_int)))⟩

def Prim8 (z : Int × Int) : Prop := (∃ e f : Int, z.1 = 8 * e + 1 ∧ z.2 = 4 * f + 0) ∨ (∃ e f : Int, z.1 = 8 * e + 3 ∧ z.2 = 4 * f + 0) ∨ (∃ e f : Int, z.1 = 8 * e + 5 ∧ z.2 = 4 * f + 1) ∨ (∃ e f : Int, z.1 = 8 * e + 5 ∧ z.2 = 4 * f + 2) ∨ (∃ e f : Int, z.1 = 8 * e + 5 ∧ z.2 = 4 * f + 3) ∨ (∃ e f : Int, z.1 = 8 * e + 7 ∧ z.2 = 4 * f + 1) ∨ (∃ e f : Int, z.1 = 8 * e + 7 ∧ z.2 = 4 * f + 2) ∨ (∃ e f   -- (+41 chars; full line in the Zenodo source)

theorem prim8_mul_0 (a b c d e f : Int) (ha : a = 8 * e + 1) (hb : b = 4 * f + 0) (hy : Prim8 (c, d)) : Prim8 ((Quad.quad LibInt.intR (-2)).mul (a, b) (c, d)) := by
  rcases hy with ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩
  · exact (Or.inl ⟨e + (8) * e * e2 + e2 + (-4) * f * f2, (8) * e * f2 + (8) * e2 * f + f + f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 * f2 + 0 from hd]; lib_int⟩)
  · exact (Or.inr (Or.inl ⟨(3) * e + (8) * e * e2 + e2 + (-4) * f * f2, (8) * e * f2 + (8) * e2 * f + (3) * f + f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 3 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 3 from hc, show d = 4 * f2 + 0 from hd]; lib_int⟩))
  · exact (Or.inr (Or.inr (Or.inl ⟨(5) * e + (8) * e * e2 + e2 + (-1) * f + (-4) * f * f2, (2) * e + (8) * e * f2 + (8) * e2 * f + (5) * f + f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 1 from hd]; l  -- (+10 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨(5) * e + (8) * e * e2 + e2 + (-2) * f + (-4) * f * f2, (4) * e + (8) * e * f2 + (8) * e2 * f + (5) * f + f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 2 fro  -- (+19 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(5) * e + (8) * e * e2 + e2 + (-3) * f + (-4) * f * f2, (6) * e + (8) * e * f2 + (8) * e2 * f + (5) * f + f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2  -- (+28 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(7) * e + (8) * e * e2 + e2 + (-1) * f + (-4) * f * f2, (2) * e + (8) * e * f2 + (8) * e2 * f + (7) * f + f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d   -- (+37 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(7) * e + (8) * e * e2 + e2 + (-2) * f + (-4) * f * f2, (4) * e + (8) * e * f2 + (8) * e2 * f + (7) * f + f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 7 from hc,  -- (+46 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨(7) * e + (8) * e * e2 + e2 + (-3) * f + (-4) * f * f2, (6) * e + (8) * e * f2 + (8) * e2 * f + (7) * f + f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 7 from hc,  -- (+46 chars; full line in the Zenodo source)

theorem prim8_mul_1 (a b c d e f : Int) (ha : a = 8 * e + 3) (hb : b = 4 * f + 0) (hy : Prim8 (c, d)) : Prim8 ((Quad.quad LibInt.intR (-2)).mul (a, b) (c, d)) := by
  rcases hy with ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩
  · exact (Or.inr (Or.inl ⟨e + (8) * e * e2 + (3) * e2 + (-4) * f * f2, (8) * e * f2 + (8) * e2 * f + f + (3) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 * f2 + 0 from hd]; lib_int⟩))
  · exact (Or.inl ⟨(1) + (3) * e + (8) * e * e2 + (3) * e2 + (-4) * f * f2, (8) * e * f2 + (8) * e2 * f + (3) * f + (3) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 3 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 3 from hc, show d = 4 * f2 + 0 from hd]; lib_int⟩)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨(1) + (5) * e + (8) * e * e2 + (3) * e2 + (-1) * f + (-4) * f * f2, (2) * e + (8) * e * f2 + (8) * e2 * f + (5) * f + (3) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8  -- (+64 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(1) + (5) * e + (8) * e * e2 + (3) * e2 + (-2) * f + (-4) * f * f2, (1) + (4) * e + (8) * e * f2 + (8) * e2 * f + (5) * f + (3) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show  -- (+70 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(1) + (5) * e + (8) * e * e2 + (3) * e2 + (-3) * f + (-4) * f * f2, (2) + (6) * e + (8) * e * f2 + (8) * e2 * f + (5) * f + (3) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 *  -- (+61 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(2) + (7) * e + (8) * e * e2 + (3) * e2 + (-1) * f + (-4) * f * f2, (2) * e + (8) * e * f2 + (8) * e2 * f + (7) * f + (3) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 7 from h  -- (+46 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨(2) + (7) * e + (8) * e * e2 + (3) * e2 + (-2) * f + (-4) * f * f2, (1) + (4) * e + (8) * e * f2 + (8) * e2 * f + (7) * f + (3) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 7 from hc,  -- (+43 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inl ⟨(2) + (7) * e + (8) * e * e2 + (3) * e2 + (-3) * f + (-4) * f * f2, (2) + (6) * e + (8) * e * f2 + (8) * e2 * f + (7) * f + (3) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d   -- (+34 chars; full line in the Zenodo source)

theorem prim8_mul_2 (a b c d e f : Int) (ha : a = 8 * e + 5) (hb : b = 4 * f + 1) (hy : Prim8 (c, d)) : Prim8 ((Quad.quad LibInt.intR (-2)).mul (a, b) (c, d)) := by
  rcases hy with ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩
  · exact (Or.inr (Or.inr (Or.inl ⟨e + (8) * e * e2 + (5) * e2 + (-4) * f * f2 + (-1) * f2, (8) * e * f2 + (2) * e2 + (8) * e2 * f + f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 * f2 + 0 from hd];  -- (+12 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨(1) + (3) * e + (8) * e * e2 + (5) * e2 + (-4) * f * f2 + (-1) * f2, (8) * e * f2 + (2) * e2 + (8) * e2 * f + (3) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 3 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c =  -- (+66 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(2) + (5) * e + (8) * e * e2 + (5) * e2 + (-1) * f + (-4) * f * f2 + (-1) * f2, (2) + (2) * e + (8) * e * f2 + (2) * e2 + (8) * e2 * f + (5) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b *   -- (+93 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(2) + (5) * e + (8) * e * e2 + (5) * e2 + (-2) * f + (-4) * f * f2 + (-1) * f2, (3) + (4) * e + (8) * e * f2 + (2) * e2 + (8) * e2 * f + (5) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, h  -- (+75 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inl ⟨(2) + (5) * e + (8) * e * e2 + (5) * e2 + (-3) * f + (-4) * f * f2 + (-1) * f2, (5) + (6) * e + (8) * e * f2 + (2) * e2 + (8) * e2 * f + (5) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 5 f  -- (+48 chars; full line in the Zenodo source)
  · exact (Or.inl ⟨(4) + (7) * e + (8) * e * e2 + (5) * e2 + (-1) * f + (-4) * f * f2 + (-1) * f2, (3) + (2) * e + (8) * e * f2 + (2) * e2 + (8) * e2 * f + (7) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 7 from hc,   -- (+39 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(3) + (7) * e + (8) * e * e2 + (5) * e2 + (-2) * f + (-4) * f * f2 + (-1) * f2, (4) + (4) * e + (8) * e * f2 + (2) * e2 + (8) * e2 * f + (7) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; r  -- (+84 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨(3) + (7) * e + (8) * e * e2 + (5) * e2 + (-3) * f + (-4) * f * f2 + (-1) * f2, (5) + (6) * e + (8) * e * f2 + (2) * e2 + (8) * e2 * f + (7) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show   -- (+66 chars; full line in the Zenodo source)

theorem prim8_mul_3 (a b c d e f : Int) (ha : a = 8 * e + 5) (hb : b = 4 * f + 2) (hy : Prim8 (c, d)) : Prim8 ((Quad.quad LibInt.intR (-2)).mul (a, b) (c, d)) := by
  rcases hy with ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩
  · exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨e + (8) * e * e2 + (5) * e2 + (-4) * f * f2 + (-2) * f2, (8) * e * f2 + (4) * e2 + (8) * e2 * f + f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 * f2 + 0 f  -- (+21 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(1) + (3) * e + (8) * e * e2 + (5) * e2 + (-4) * f * f2 + (-2) * f2, (1) + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (3) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 3 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, sh  -- (+72 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(2) + (5) * e + (8) * e * e2 + (5) * e2 + (-1) * f + (-4) * f * f2 + (-2) * f2, (3) + (2) * e + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (5) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, h  -- (+75 chars; full line in the Zenodo source)
  · exact (Or.inl ⟨(2) + (5) * e + (8) * e * e2 + (5) * e2 + (-2) * f + (-4) * f * f2 + (-2) * f2, (5) + (4) * e + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (5) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 5 from hc,   -- (+39 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inl ⟨(1) + (5) * e + (8) * e * e2 + (5) * e2 + (-3) * f + (-4) * f * f2 + (-2) * f2, (6) + (6) * e + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (5) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 *   -- (+57 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨(3) + (7) * e + (8) * e * e2 + (5) * e2 + (-1) * f + (-4) * f * f2 + (-2) * f2, (4) + (2) * e + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (7) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b *   -- (+93 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inl ⟨(3) + (7) * e + (8) * e * e2 + (5) * e2 + (-2) * f + (-4) * f * f2 + (-2) * f2, (6) + (4) * e + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (7) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 7 f  -- (+48 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(2) + (7) * e + (8) * e * e2 + (5) * e2 + (-3) * f + (-4) * f * f2 + (-2) * f2, (7) + (6) * e + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (7) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; r  -- (+84 chars; full line in the Zenodo source)

theorem prim8_mul_4 (a b c d e f : Int) (ha : a = 8 * e + 5) (hb : b = 4 * f + 3) (hy : Prim8 (c, d)) : Prim8 ((Quad.quad LibInt.intR (-2)).mul (a, b) (c, d)) := by
  rcases hy with ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e + (8) * e * e2 + (5) * e2 + (-4) * f * f2 + (-3) * f2, (8) * e * f2 + (6) * e2 + (8) * e2 * f + f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 *   -- (+30 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(1) + (3) * e + (8) * e * e2 + (5) * e2 + (-4) * f * f2 + (-3) * f2, (2) + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (3) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 3 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8  -- (+63 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inl ⟨(2) + (5) * e + (8) * e * e2 + (5) * e2 + (-1) * f + (-4) * f * f2 + (-3) * f2, (5) + (2) * e + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (5) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 5 f  -- (+48 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inl ⟨(1) + (5) * e + (8) * e * e2 + (5) * e2 + (-2) * f + (-4) * f * f2 + (-3) * f2, (6) + (4) * e + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (5) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 *   -- (+57 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(5) * e + (8) * e * e2 + (5) * e2 + (-3) * f + (-4) * f * f2 + (-3) * f2, (7) + (6) * e + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (5) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _;  -- (+87 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨(3) + (7) * e + (8) * e * e2 + (5) * e2 + (-1) * f + (-4) * f * f2 + (-3) * f2, (6) + (2) * e + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (7) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show   -- (+66 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨(2) + (7) * e + (8) * e * e2 + (5) * e2 + (-2) * f + (-4) * f * f2 + (-3) * f2, (7) + (4) * e + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (7) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b *   -- (+93 chars; full line in the Zenodo source)
  · exact (Or.inl ⟨(2) + (7) * e + (8) * e * e2 + (5) * e2 + (-3) * f + (-4) * f * f2 + (-3) * f2, (9) + (6) * e + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (7) * f + (5) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 7 from hc,   -- (+39 chars; full line in the Zenodo source)

theorem prim8_mul_5 (a b c d e f : Int) (ha : a = 8 * e + 7) (hb : b = 4 * f + 1) (hy : Prim8 (c, d)) : Prim8 ((Quad.quad LibInt.intR (-2)).mul (a, b) (c, d)) := by
  rcases hy with ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e + (8) * e * e2 + (7) * e2 + (-4) * f * f2 + (-1) * f2, (8) * e * f2 + (2) * e2 + (8) * e2 * f + f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show   -- (+39 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(2) + (3) * e + (8) * e * e2 + (7) * e2 + (-4) * f * f2 + (-1) * f2, (8) * e * f2 + (2) * e2 + (8) * e2 * f + (3) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 3 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 3 from  -- (+48 chars; full line in the Zenodo source)
  · exact (Or.inl ⟨(4) + (5) * e + (8) * e * e2 + (7) * e2 + (-1) * f + (-4) * f * f2 + (-1) * f2, (3) + (2) * e + (8) * e * f2 + (2) * e2 + (8) * e2 * f + (5) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 5 from hc,   -- (+39 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨(3) + (5) * e + (8) * e * e2 + (7) * e2 + (-2) * f + (-4) * f * f2 + (-1) * f2, (4) + (4) * e + (8) * e * f2 + (2) * e2 + (8) * e2 * f + (5) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b *   -- (+93 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨(3) + (5) * e + (8) * e * e2 + (7) * e2 + (-3) * f + (-4) * f * f2 + (-1) * f2, (6) + (6) * e + (8) * e * f2 + (2) * e2 + (8) * e2 * f + (5) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show   -- (+66 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(5) + (7) * e + (8) * e * e2 + (7) * e2 + (-1) * f + (-4) * f * f2 + (-1) * f2, (3) + (2) * e + (8) * e * f2 + (2) * e2 + (8) * e2 * f + (7) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b *   -- (+93 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inl ⟨(5) + (7) * e + (8) * e * e2 + (7) * e2 + (-2) * f + (-4) * f * f2 + (-1) * f2, (5) + (4) * e + (8) * e * f2 + (2) * e2 + (8) * e2 * f + (7) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 *   -- (+57 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inl ⟨(5) + (7) * e + (8) * e * e2 + (7) * e2 + (-3) * f + (-4) * f * f2 + (-1) * f2, (7) + (6) * e + (8) * e * f2 + (2) * e2 + (8) * e2 * f + (7) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 7 f  -- (+48 chars; full line in the Zenodo source)

theorem prim8_mul_6 (a b c d e f : Int) (ha : a = 8 * e + 7) (hb : b = 4 * f + 2) (hy : Prim8 (c, d)) : Prim8 ((Quad.quad LibInt.intR (-2)).mul (a, b) (c, d)) := by
  rcases hy with ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e + (8) * e * e2 + (7) * e2 + (-4) * f * f2 + (-2) * f2, (8) * e * f2 + (4) * e2 + (8) * e2 * f + f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 1 from h  -- (+48 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨(2) + (3) * e + (8) * e * e2 + (7) * e2 + (-4) * f * f2 + (-2) * f2, (1) + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (3) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 3 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 3 from h  -- (+45 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(3) + (5) * e + (8) * e * e2 + (7) * e2 + (-1) * f + (-4) * f * f2 + (-2) * f2, (4) + (2) * e + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (5) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; r  -- (+84 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inl ⟨(3) + (5) * e + (8) * e * e2 + (7) * e2 + (-2) * f + (-4) * f * f2 + (-2) * f2, (6) + (4) * e + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (5) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 5 f  -- (+48 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨(2) + (5) * e + (8) * e * e2 + (7) * e2 + (-3) * f + (-4) * f * f2 + (-2) * f2, (7) + (6) * e + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (5) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b *   -- (+93 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inl ⟨(5) + (7) * e + (8) * e * e2 + (7) * e2 + (-1) * f + (-4) * f * f2 + (-2) * f2, (5) + (2) * e + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (7) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 *   -- (+57 chars; full line in the Zenodo source)
  · exact (Or.inl ⟨(5) + (7) * e + (8) * e * e2 + (7) * e2 + (-2) * f + (-4) * f * f2 + (-2) * f2, (7) + (4) * e + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (7) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 7 from hc,   -- (+39 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(4) + (7) * e + (8) * e * e2 + (7) * e2 + (-3) * f + (-4) * f * f2 + (-2) * f2, (8) + (6) * e + (8) * e * f2 + (4) * e2 + (8) * e2 * f + (7) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, h  -- (+75 chars; full line in the Zenodo source)

theorem prim8_mul_7 (a b c d e f : Int) (ha : a = 8 * e + 7) (hb : b = 4 * f + 3) (hy : Prim8 (c, d)) : Prim8 ((Quad.quad LibInt.intR (-2)).mul (a, b) (c, d)) := by
  rcases hy with ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨e + (8) * e * e2 + (7) * e2 + (-4) * f * f2 + (-3) * f2, (8) * e * f2 + (6) * e2 + (8) * e2 * f + f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 1 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 1 from h  -- (+48 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inl ⟨(2) + (3) * e + (8) * e * e2 + (7) * e2 + (-4) * f * f2 + (-3) * f2, (2) + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (3) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 3 from hc, show d = 4 * f2 + 0 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 3 from hc, show   -- (+36 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨(3) + (5) * e + (8) * e * e2 + (7) * e2 + (-1) * f + (-4) * f * f2 + (-3) * f2, (5) + (2) * e + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (5) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show   -- (+66 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(2) + (5) * e + (8) * e * e2 + (7) * e2 + (-2) * f + (-4) * f * f2 + (-3) * f2, (7) + (4) * e + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (5) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; r  -- (+84 chars; full line in the Zenodo source)
  · exact (Or.inl ⟨(2) + (5) * e + (8) * e * e2 + (7) * e2 + (-3) * f + (-4) * f * f2 + (-3) * f2, (9) + (6) * e + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (5) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 5 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 5 from hc,   -- (+39 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inl ⟨(5) + (7) * e + (8) * e * e2 + (7) * e2 + (-1) * f + (-4) * f * f2 + (-3) * f2, (7) + (2) * e + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (7) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 1 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, hb, show c = 8 * e2 + 7 f  -- (+48 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(4) + (7) * e + (8) * e * e2 + (7) * e2 + (-2) * f + (-4) * f * f2 + (-3) * f2, (8) + (4) * e + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (7) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 2 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b * c = _; rw [ha, h  -- (+75 chars; full line in the Zenodo source)
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(3) + (7) * e + (8) * e * e2 + (7) * e2 + (-3) * f + (-4) * f * f2 + (-3) * f2, (10) + (6) * e + (8) * e * f2 + (6) * e2 + (8) * e2 * f + (7) * f + (7) * f2, by rw [Sq2Unique.mul1]; show a * c + -2 * (b * d) = _; rw [ha, hb, show c = 8 * e2 + 7 from hc, show d = 4 * f2 + 3 from hd]; lib_int, by rw [Sq2Unique.mul2]; show a * d + b *  -- (+94 chars; full line in the Zenodo source)

theorem prim8_mul (x y : Int × Int) (hx : Prim8 x) (hy : Prim8 y) : Prim8 ((Quad.quad LibInt.intR (-2)).mul x y) := by
  obtain ⟨a, b⟩ := x
  obtain ⟨c, d⟩ := y
  rcases hx with ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩
  · exact prim8_mul_0 a b c d e f ha hb hy
  · exact prim8_mul_1 a b c d e f ha hb hy
  · exact prim8_mul_2 a b c d e f ha hb hy
  · exact prim8_mul_3 a b c d e f ha hb hy
  · exact prim8_mul_4 a b c d e f ha hb hy
  · exact prim8_mul_5 a b c d e f ha hb hy
  · exact prim8_mul_6 a b c d e f ha hb hy
  · exact prim8_mul_7 a b c d e f ha hb hy

theorem prim8_cj (z : Int × Int) (hz : Prim8 z) : Prim8 (Sq2Euclid.cj z) := by
  obtain ⟨a, b⟩ := z
  rcases hz with ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩
  · exact (Or.inl ⟨e, (-1) * f, ha, by show -b = _; rw [show b = 4 * f + 0 from hb]; lib_int⟩)
  · exact (Or.inr (Or.inl ⟨e, (-1) * f, ha, by show -b = _; rw [show b = 4 * f + 0 from hb]; lib_int⟩))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, (-1) + (-1) * f, ha, by show -b = _; rw [show b = 4 * f + 1 from hb]; lib_int⟩)))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, (-1) + (-1) * f, ha, by show -b = _; rw [show b = 4 * f + 2 from hb]; lib_int⟩))))
  · exact (Or.inr (Or.inr (Or.inl ⟨e, (-1) + (-1) * f, ha, by show -b = _; rw [show b = 4 * f + 3 from hb]; lib_int⟩)))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨e, (-1) + (-1) * f, ha, by show -b = _; rw [show b = 4 * f + 1 from hb]; lib_int⟩)))))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, (-1) + (-1) * f, ha, by show -b = _; rw [show b = 4 * f + 2 from hb]; lib_int⟩)))))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, (-1) + (-1) * f, ha, by show -b = _; rw [show b = 4 * f + 3 from hb]; lib_int⟩))))))

theorem normalize8 (α : Int × Int) (k : Int) (h : en α = 2 * k + 1) : ∃ u, en u = 1 ∧ Prim8 ((Quad.quad LibInt.intR (-2)).mul u α) := by
  obtain ⟨a, b⟩ := α
  have h' : a * a + 2 * (b * b) = 2 * k + 1 := h
  obtain ⟨e, ha⟩ := mod8_odd a b k h'
  obtain ⟨f, hb⟩ := mod4 b
  rcases ha with ha | ha | ha | ha <;> rcases hb with hb | hb | hb | hb
  · exact ⟨(Quad.quad LibInt.intR (-2)).one, rfl, by rw [(Quad.quad LibInt.intR (-2)).one_mul]; exact (Or.inl ⟨e, f, ha, hb⟩)⟩
  · refine ⟨(-1, 0), rfl, ?_⟩; rw [Sq2Unique.neg_mul']; exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨(-1) + (-1) * e, (-1) + (-1) * f, by show -a = _; rw [ha]; lib_int, by show -b = _; rw [hb]; lib_int⟩)))))))
  · refine ⟨(-1, 0), rfl, ?_⟩; rw [Sq2Unique.neg_mul']; exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(-1) + (-1) * e, (-1) + (-1) * f, by show -a = _; rw [ha]; lib_int, by show -b = _; rw [hb]; lib_int⟩)))))))
  · refine ⟨(-1, 0), rfl, ?_⟩; rw [Sq2Unique.neg_mul']; exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(-1) + (-1) * e, (-1) + (-1) * f, by show -a = _; rw [ha]; lib_int, by show -b = _; rw [hb]; lib_int⟩))))))
  · exact ⟨(Quad.quad LibInt.intR (-2)).one, rfl, by rw [(Quad.quad LibInt.intR (-2)).one_mul]; exact (Or.inr (Or.inl ⟨e, f, ha, hb⟩))⟩
  · refine ⟨(-1, 0), rfl, ?_⟩; rw [Sq2Unique.neg_mul']; exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(-1) + (-1) * e, (-1) + (-1) * f, by show -a = _; rw [ha]; lib_int, by show -b = _; rw [hb]; lib_int⟩)))))
  · refine ⟨(-1, 0), rfl, ?_⟩; rw [Sq2Unique.neg_mul']; exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨(-1) + (-1) * e, (-1) + (-1) * f, by show -a = _; rw [ha]; lib_int, by show -b = _; rw [hb]; lib_int⟩))))
  · refine ⟨(-1, 0), rfl, ?_⟩; rw [Sq2Unique.neg_mul']; exact (Or.inr (Or.inr (Or.inl ⟨(-1) + (-1) * e, (-1) + (-1) * f, by show -a = _; rw [ha]; lib_int, by show -b = _; rw [hb]; lib_int⟩)))
  · refine ⟨(-1, 0), rfl, ?_⟩; rw [Sq2Unique.neg_mul']; exact (Or.inr (Or.inl ⟨(-1) + (-1) * e, (-1) * f, by show -a = _; rw [ha]; lib_int, by show -b = _; rw [hb]; lib_int⟩))
  · exact ⟨(Quad.quad LibInt.intR (-2)).one, rfl, by rw [(Quad.quad LibInt.intR (-2)).one_mul]; exact (Or.inr (Or.inr (Or.inl ⟨e, f, ha, hb⟩)))⟩
  · exact ⟨(Quad.quad LibInt.intR (-2)).one, rfl, by rw [(Quad.quad LibInt.intR (-2)).one_mul]; exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, f, ha, hb⟩))))⟩
  · exact ⟨(Quad.quad LibInt.intR (-2)).one, rfl, by rw [(Quad.quad LibInt.intR (-2)).one_mul]; exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, f, ha, hb⟩)))))⟩
  · refine ⟨(-1, 0), rfl, ?_⟩; rw [Sq2Unique.neg_mul']; exact (Or.inl ⟨(-1) + (-1) * e, (-1) * f, by show -a = _; rw [ha]; lib_int, by show -b = _; rw [hb]; lib_int⟩)
  · exact ⟨(Quad.quad LibInt.intR (-2)).one, rfl, by rw [(Quad.quad LibInt.intR (-2)).one_mul]; exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, f, ha, hb⟩))))))⟩
  · exact ⟨(Quad.quad LibInt.intR (-2)).one, rfl, by rw [(Quad.quad LibInt.intR (-2)).one_mul]; exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, f, ha, hb⟩)))))))⟩
  · exact ⟨(Quad.quad LibInt.intR (-2)).one, rfl, by rw [(Quad.quad LibInt.intR (-2)).one_mul]; exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨e, f, ha, hb⟩)))))))⟩

theorem unit_one8 (u α : Int × Int) (hu : en u = 1) (pα : Prim8 α) (pu : Prim8 ((Quad.quad LibInt.intR (-2)).mul u α)) : u = (Quad.quad LibInt.intR (-2)).one := by
  rcases Sq2Unique.unit_pm u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, Sq2Unique.neg_mul'] at pu
  obtain ⟨a, b⟩ := α
  rcases pα with ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ | ⟨e, f, ha, hb⟩ <;> rcases pu with ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩ | ⟨e2, f2, hc, hd⟩
  · exact noM 7 (e + e2 + -(-1)) 5 1 (by calc (8 : Int) * (e + e2 + -(-1)) = (8 * e + 1) + (8 * e2 + 1) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 1 from ha, ← show -a = 8 * e2 + 1 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-1)) 3 3 (by calc (8 : Int) * (e + e2 + -(-1)) = (8 * e + 1) + (8 * e2 + 3) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 1 from ha, ← show -a = 8 * e2 + 3 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-1)) 1 5 (by calc (8 : Int) * (e + e2 + -(-1)) = (8 * e + 1) + (8 * e2 + 5) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 1 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-1)) 1 5 (by calc (8 : Int) * (e + e2 + -(-1)) = (8 * e + 1) + (8 * e2 + 5) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 1 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-1)) 1 5 (by calc (8 : Int) * (e + e2 + -(-1)) = (8 * e + 1) + (8 * e2 + 5) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 1 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 3 (f + f2 + -(-1)) 2 0 (by calc (4 : Int) * (f + f2 + -(-1)) = (4 * f + 0) + (4 * f2 + 1) + 3 := by lib_int
        _ = b + -b + 3 := by rw [← show b = 4 * f + 0 from hb, ← show -b = 4 * f2 + 1 from hd]
        _ = 3 := by lib_int) rfl
  · exact noM 3 (f + f2 + -(-1)) 1 1 (by calc (4 : Int) * (f + f2 + -(-1)) = (4 * f + 0) + (4 * f2 + 2) + 2 := by lib_int
        _ = b + -b + 2 := by rw [← show b = 4 * f + 0 from hb, ← show -b = 4 * f2 + 2 from hd]
        _ = 2 := by lib_int) rfl
  · exact noM 3 (f + f2 + -(-1)) 0 2 (by calc (4 : Int) * (f + f2 + -(-1)) = (4 * f + 0) + (4 * f2 + 3) + 1 := by lib_int
        _ = b + -b + 1 := by rw [← show b = 4 * f + 0 from hb, ← show -b = 4 * f2 + 3 from hd]
        _ = 1 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-1)) 3 3 (by calc (8 : Int) * (e + e2 + -(-1)) = (8 * e + 3) + (8 * e2 + 1) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 3 from ha, ← show -a = 8 * e2 + 1 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-1)) 1 5 (by calc (8 : Int) * (e + e2 + -(-1)) = (8 * e + 3) + (8 * e2 + 3) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 3 from ha, ← show -a = 8 * e2 + 3 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 3 (f + f2 + -(-1)) 2 0 (by calc (4 : Int) * (f + f2 + -(-1)) = (4 * f + 0) + (4 * f2 + 1) + 3 := by lib_int
        _ = b + -b + 3 := by rw [← show b = 4 * f + 0 from hb, ← show -b = 4 * f2 + 1 from hd]
        _ = 3 := by lib_int) rfl
  · exact noM 3 (f + f2 + -(-1)) 1 1 (by calc (4 : Int) * (f + f2 + -(-1)) = (4 * f + 0) + (4 * f2 + 2) + 2 := by lib_int
        _ = b + -b + 2 := by rw [← show b = 4 * f + 0 from hb, ← show -b = 4 * f2 + 2 from hd]
        _ = 2 := by lib_int) rfl
  · exact noM 3 (f + f2 + -(-1)) 0 2 (by calc (4 : Int) * (f + f2 + -(-1)) = (4 * f + 0) + (4 * f2 + 3) + 1 := by lib_int
        _ = b + -b + 1 := by rw [← show b = 4 * f + 0 from hb, ← show -b = 4 * f2 + 3 from hd]
        _ = 1 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 3) + (8 * e2 + 7) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 3 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 3) + (8 * e2 + 7) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 3 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 3) + (8 * e2 + 7) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 3 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-1)) 1 5 (by calc (8 : Int) * (e + e2 + -(-1)) = (8 * e + 5) + (8 * e2 + 1) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 1 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 3 (f + f2 + -(-1)) 2 0 (by calc (4 : Int) * (f + f2 + -(-1)) = (4 * f + 1) + (4 * f2 + 0) + 3 := by lib_int
        _ = b + -b + 3 := by rw [← show b = 4 * f + 1 from hb, ← show -b = 4 * f2 + 0 from hd]
        _ = 3 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 5) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 5) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 5) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 7) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 7) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 7) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-1)) 1 5 (by calc (8 : Int) * (e + e2 + -(-1)) = (8 * e + 5) + (8 * e2 + 1) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 1 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 3 (f + f2 + -(-1)) 1 1 (by calc (4 : Int) * (f + f2 + -(-1)) = (4 * f + 2) + (4 * f2 + 0) + 2 := by lib_int
        _ = b + -b + 2 := by rw [← show b = 4 * f + 2 from hb, ← show -b = 4 * f2 + 0 from hd]
        _ = 2 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 5) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 5) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 5) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 7) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 7) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 7) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-1)) 1 5 (by calc (8 : Int) * (e + e2 + -(-1)) = (8 * e + 5) + (8 * e2 + 1) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 1 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 3 (f + f2 + -(-1)) 0 2 (by calc (4 : Int) * (f + f2 + -(-1)) = (4 * f + 3) + (4 * f2 + 0) + 1 := by lib_int
        _ = b + -b + 1 := by rw [← show b = 4 * f + 3 from hb, ← show -b = 4 * f2 + 0 from hd]
        _ = 1 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 5) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 5) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 5) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 7) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 7) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 5) + (8 * e2 + 7) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 5 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 3 (f + f2 + -(-1)) 2 0 (by calc (4 : Int) * (f + f2 + -(-1)) = (4 * f + 1) + (4 * f2 + 0) + 3 := by lib_int
        _ = b + -b + 3 := by rw [← show b = 4 * f + 1 from hb, ← show -b = 4 * f2 + 0 from hd]
        _ = 3 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 3) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 3 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 5) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 5) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 5) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 1 5 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 7) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 1 5 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 7) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 1 5 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 7) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 3 (f + f2 + -(-1)) 1 1 (by calc (4 : Int) * (f + f2 + -(-1)) = (4 * f + 2) + (4 * f2 + 0) + 2 := by lib_int
        _ = b + -b + 2 := by rw [← show b = 4 * f + 2 from hb, ← show -b = 4 * f2 + 0 from hd]
        _ = 2 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 3) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 3 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 5) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 5) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 5) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 1 5 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 7) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 1 5 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 7) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 1 5 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 7) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 3 (f + f2 + -(-1)) 0 2 (by calc (4 : Int) * (f + f2 + -(-1)) = (4 * f + 3) + (4 * f2 + 0) + 1 := by lib_int
        _ = b + -b + 1 := by rw [← show b = 4 * f + 3 from hb, ← show -b = 4 * f2 + 0 from hd]
        _ = 1 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 5 1 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 3) + 6 := by lib_int
        _ = a + -a + 6 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 3 from hc]
        _ = 6 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 5) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 5) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 3 3 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 5) + 4 := by lib_int
        _ = a + -a + 4 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 5 from hc]
        _ = 4 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 1 5 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 7) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 1 5 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 7) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 2 := by lib_int) rfl
  · exact noM 7 (e + e2 + -(-2)) 1 5 (by calc (8 : Int) * (e + e2 + -(-2)) = (8 * e + 7) + (8 * e2 + 7) + 2 := by lib_int
        _ = a + -a + 2 := by rw [← show a = 8 * e + 7 from ha, ← show -a = 8 * e2 + 7 from hc]
        _ = 2 := by lib_int) rfl

end Sq2Prim8

#print axioms Sq2Prim8.noM
#print axioms Sq2Prim8.mod8_odd
#print axioms Sq2Prim8.mod4
#print axioms Sq2Prim8.prim8_mul_0
#print axioms Sq2Prim8.prim8_mul_1
#print axioms Sq2Prim8.prim8_mul_2
#print axioms Sq2Prim8.prim8_mul_3
#print axioms Sq2Prim8.prim8_mul_4
#print axioms Sq2Prim8.prim8_mul_5
#print axioms Sq2Prim8.prim8_mul_6
#print axioms Sq2Prim8.prim8_mul_7
#print axioms Sq2Prim8.prim8_mul
#print axioms Sq2Prim8.prim8_cj
#print axioms Sq2Prim8.normalize8
#print axioms Sq2Prim8.unit_one8

#print axioms: 15 theorems, no axioms.

78.244 Unique splitting into \(H_8\) in \(\mathbb Z[\sqrt {-2}]\)

An element of \(H_8\) of norm \(mn\), \((m,n)=1\), is uniquely a product of \(H_8\)-elements of norms \(m\) and \(n\).

\[ \gamma \in H_8,\ N(\gamma )=mn\ \Rightarrow \ \exists !\,(\alpha ,\beta )\in H_8^2,\ \gamma =\alpha \beta ,\ N(\alpha )=m. \]

lang/Sq2Unique8.lean

/-
  lang/Sq2Unique8 — T48/T50, cycle 631: the unique splitting into elements of the class H8 (Sq2Prim8) in ℤ[√−2]:
    prim8_quot, uniq_split8, prim8_split     (Sq2Unique with H8 in place of the mod-4 class)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Sq2Prim8

namespace Sq2Unique8

open FTAChain DetChain LibInt Sq2Euclid Sq2Prim8

/-- y is primary when x and xy are (N(y) odd). -/
theorem prim8_quot (x y : Int × Int) (hy : ∃ k, en y = 2 * k + 1) (hx : Prim8 x) (hxy : Prim8 ((Quad.quad LibInt.intR (-2)).mul x y)) : Prim8 y := by
  obtain ⟨k, hk⟩ := hy
  obtain ⟨u, hu, hpu⟩ := normalize8 y k hk
  have h1 : Prim8 ((Quad.quad LibInt.intR (-2)).mul u ((Quad.quad LibInt.intR (-2)).mul x y)) := by
    rw [show (Quad.quad LibInt.intR (-2)).mul u ((Quad.quad LibInt.intR (-2)).mul x y) = (Quad.quad LibInt.intR (-2)).mul x ((Quad.quad LibInt.intR (-2)).mul u y) by lib_ring]
    exact prim8_mul _ _ hx hpu
  have hu1 := unit_one8 u _ hu hxy h1
  rw [hu1, (Quad.quad LibInt.intR (-2)).one_mul] at hpu
  exact hpu

/-- **Uniqueness of the primary splitting.** -/
theorem uniq_split8 (α β α' β' : Int × Int) (M N s t : Int) (hα : en α = M) (hα' : en α' = M) (hβ' : en β' = N)
    (hst : s * M + t * N = 1) (hM : M ≠ 0) (pα : Prim8 α) (pα' : Prim8 α') (h : (Quad.quad LibInt.intR (-2)).mul α β = (Quad.quad LibInt.intR (-2)).mul α' β') :
    α = α' ∧ β = β' := by
  have hco : (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb s) (cj α)) α) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb t) (cj β')) β') = (Quad.quad LibInt.intR (-2)).one := by
    rw [show (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb s) (cj α)) α) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb t) (cj β')) β') = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb s) ((Quad.quad LibInt.intR (-2)).mul α (cj α))) ((Quad.quad LibInt.intR  -- (+89 chars; full line in the Zenodo source)
      mul_cj, mul_cj, hα, hβ']
    show (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb s) (Sq2Coprime.emb M)) ((Quad.quad LibInt.intR (-2)).mul (Sq2Coprime.emb t) (Sq2Coprime.emb N)) = _
    rw [Sq2Coprime.emb_mul, Sq2Coprime.emb_mul, ← Sq2Coprime.emb_add, hst]; rfl
  match eeuclid α β' α' _ _ hco ⟨β, by rw [show (Quad.quad LibInt.intR (-2)).mul β' α' = (Quad.quad LibInt.intR (-2)).mul α' β' by lib_ring, ← h]⟩ with
  | ⟨w, hw⟩ =>
    have hgw : en w = 1 := by
      have e : M * en w = M * 1 := by
        calc M * en w = en α * en w := by rw [hα]
          _ = en α' := by rw [← en_mul, ← hw]
          _ = M * 1 := by rw [hα']; lib_int
      have e0 : M * (en w + -1) = 0 := by rw [show M * (en w + -1) = M * en w + -(M * 1) by lib_int, e]; lib_int
      cases int_noZD _ _ e0 with
      | inl h0 => exact absurd h0 hM
      | inr h0 => calc en w = (en w + -1) + 1 := by lib_int
          _ = 1 := by rw [h0]; rfl
    have hw1 : w = (Quad.quad LibInt.intR (-2)).one :=
      unit_one8 w α hgw pα (by rw [show (Quad.quad LibInt.intR (-2)).mul w α = α' by rw [hw]; lib_ring]; exact pα')
    have hαα : α = α' := by rw [hw, hw1]; lib_ring
    refine ⟨hαα, ?_⟩
    have h0 : (Quad.quad LibInt.intR (-2)).mul α ((Quad.quad LibInt.intR (-2)).add β ((Quad.quad LibInt.intR (-2)).neg β')) = (Quad.quad LibInt.intR (-2)).zero := by
      rw [show (Quad.quad LibInt.intR (-2)).mul α ((Quad.quad LibInt.intR (-2)).add β ((Quad.quad LibInt.intR (-2)).neg β')) = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul α β) ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul α β')) by lib_ring, h, ← hαα]; lib_ring
    have := Sq2Coprime.cancel_left α _ (by rw [hα]; exact hM) h0
    calc β = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).add β ((Quad.quad LibInt.intR (-2)).neg β')) β' := by lib_ring
      _ = β' := by rw [this]; lib_ring

/-- **Existence of the primary splitting.** -/
theorem prim8_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (hn : ∃ k, Int.ofNat (n + 1) = 2 * k + 1)
    (pγ : Prim8 γ) : ∃ α β, γ = (Quad.quad LibInt.intR (-2)).mul α β ∧ Prim8 α ∧ Prim8 β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  obtain ⟨km, hkm⟩ := hm
  match Sq2Coprime.ecoprime_split γ m n s t hγ hst with
  | ⟨α0, β0, h0, hα0, hβ0⟩ =>
    match normalize8 α0 km (by rw [hα0]; exact hkm) with
    | ⟨u, gu, pu⟩ =>
      have uc : (Quad.quad LibInt.intR (-2)).mul u (cj u) = (Quad.quad LibInt.intR (-2)).one := by rw [mul_cj, gu]; rfl
      have hβn : en ((Quad.quad LibInt.intR (-2)).mul (cj u) β0) = Int.ofNat (n + 1) := by
        rw [en_mul, show en (cj u) = en u from en_cj u, gu, hβ0]; lib_int
      have hsplit : γ = (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul u α0) ((Quad.quad LibInt.intR (-2)).mul (cj u) β0) := by
        rw [h0, show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul u α0) ((Quad.quad LibInt.intR (-2)).mul (cj u) β0) = (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul u (cj u)) ((Quad.quad LibInt.intR (-2)).mul α0 β0) by lib_ring, uc]; lib_ring
      refine ⟨(Quad.quad LibInt.intR (-2)).mul u α0, (Quad.quad LibInt.intR (-2)).mul (cj u) β0, hsplit, pu,
        prim8_quot _ _ (by rw [hβn]; exact hn) pu (by rw [← hsplit]; exact pγ),
        by rw [en_mul, gu, hα0]; lib_int, hβn⟩

end Sq2Unique8

#print axioms Sq2Unique8.prim8_quot
#print axioms Sq2Unique8.uniq_split8
#print axioms Sq2Unique8.prim8_split

#print axioms: 3 theorems, no axioms.

78.245 256a: multiplicativity of the Hecke theta series

For every multiplicative \(w\) on \(\mathbb Z[\sqrt {-2}]\) the theta coefficients over \(H_8\) are multiplicative.

\[ c_w(n)=\sum _{\alpha \in H_8,\ N\alpha =n}w(\alpha ),\qquad c_w(mn)=c_w(m)\,c_w(n). \]

lang/HeckeMult8.lean

/-
  lang/HeckeMult8 — T48/T50, cycle 632: **the theta series of 256a's Hecke character class**: for every multiplicative w on ℤ[√−2],
    c_w(n) = Σ_{α ∈ H8, N(α) = n} w(α)  is multiplicative on coprime odd m, n  (HeckeMult2 with the class H8 of Sq2Prim8).
  For w = id, c(p) = 2a for p = a² + 2b² (π = a + b√−2 ∈ H8) — the a_p of y² = x³ + 4x² + 2x (checked numerically).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Sq2Unique8
import lang.HeckeMultW

namespace HeckeMult8

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Sq2Euclid Sq2Prim8 Sq2Unique8

abbrev box8 (n : Nat) : List (Int × Int) := HeckeMult.box n

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ n. -/
theorem abs_le8 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ n ∧ Int.natAbs z.2 ≤ n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have hn' : Int.natAbs z.1 * Int.natAbs z.1 + 2 * (Int.natAbs z.2 * Int.natAbs z.2) = n := hn
  constructor
  · exact Nat.le_trans (HeckeMult.le_sq _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq hn'))
  · exact Nat.le_trans (HeckeMult.le_sq _) (Nat.le_trans (Nat.le_trans (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_succ 1))) (Nat.le_add_left _ _)) (Nat.le_of_eq hn'))

def PD8 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim8 z

theorem prim8_iff (z : Int × Int) : ((ZMod.md 7 (Int.natAbs (z.1 - 1)) = 0 ∧ ZMod.md 3 (Int.natAbs (z.2 - 0)) = 0) ∨ (ZMod.md 7 (Int.natAbs (z.1 - 3)) = 0 ∧ ZMod.md 3 (Int.natAbs (z.2 - 0)) = 0) ∨ (ZMod.md 7 (Int.natAbs (z.1 - 5)) = 0 ∧ ZMod.md 3 (Int.natAbs (z.2 - 1)) = 0) ∨ (ZMod.md 7 (Int.natAbs (z.1 - 5)) = 0 ∧ ZMod.md 3 (Int.natAbs (z.2 - 2)) = 0) ∨ (ZMod.md 7 (Int.natAbs (z.1 - 5)) = 0 ∧ ZMo  -- (+291 chars; full line in the Zenodo source)
  constructor
  · intro h
    rcases h with ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inl ⟨e, f, by calc z.1 = (z.1 - 1) + 1 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 0) + 0 := by lib_int
          _ = _ := by rw [hf]; rfl⟩)
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inl ⟨e, f, by calc z.1 = (z.1 - 3) + 3 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 0) + 0 := by lib_int
          _ = _ := by rw [hf]; rfl⟩))
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inr (Or.inl ⟨e, f, by calc z.1 = (z.1 - 5) + 5 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 1) + 1 := by lib_int
          _ = _ := by rw [hf]; rfl⟩)))
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, f, by calc z.1 = (z.1 - 5) + 5 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 2) + 2 := by lib_int
          _ = _ := by rw [hf]; rfl⟩))))
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, f, by calc z.1 = (z.1 - 5) + 5 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 3) + 3 := by lib_int
          _ = _ := by rw [hf]; rfl⟩)))))
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, f, by calc z.1 = (z.1 - 7) + 7 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 1) + 1 := by lib_int
          _ = _ := by rw [hf]; rfl⟩))))))
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨e, f, by calc z.1 = (z.1 - 7) + 7 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 2) + 2 := by lib_int
          _ = _ := by rw [hf]; rfl⟩)))))))
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 7 _).mpr h1
      obtain ⟨f, hf⟩ := (HeckeMult.idv_iff 3 _).mpr h2
      exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨e, f, by calc z.1 = (z.1 - 7) + 7 := by lib_int
          _ = _ := by rw [he]; rfl, by calc z.2 = (z.2 - 3) + 3 := by lib_int
          _ = _ := by rw [hf]; rfl⟩)))))))
  · intro h
    rcases h with ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩
    · exact (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 1 - 1 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 0 - 0 = 4 * f; lib_int⟩⟩)
    · exact (Or.inr (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 3 - 3 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 0 - 0 = 4 * f; lib_int⟩⟩))
    · exact (Or.inr (Or.inr (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 5 - 5 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 1 - 1 = 4 * f; lib_int⟩⟩)))
    · exact (Or.inr (Or.inr (Or.inr (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 5 - 5 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 2 - 2 = 4 * f; lib_int⟩⟩))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 5 - 5 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 3 - 3 = 4 * f; lib_int⟩⟩)))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 7 - 7 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 1 - 1 = 4 * f; lib_int⟩⟩))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 7 - 7 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 2 - 2 = 4 * f; lib_int⟩⟩)))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨(HeckeMult.idv_iff 7 _).mp ⟨e, by rw [h1]; show 8 * e + 7 - 7 = 8 * e; lib_int⟩, (HeckeMult.idv_iff 3 _).mp ⟨f, by rw [h2]; show 4 * f + 3 - 3 = 4 * f; lib_int⟩⟩)))))))

instance (z : Int × Int) : Decidable (Prim8 z) := decidable_of_iff _ (prim8_iff z)

instance (n : Nat) (z : Int × Int) : Decidable (PD8 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum8 {A : Type} : ∀ l : List A, sumK (Quad.quad LibInt.intR (-2)) (fun _ => (Quad.quad LibInt.intR (-2)).zero) l = (Quad.quad LibInt.intR (-2)).zero
  | [] => rfl
  | _ :: l => by show (Quad.quad LibInt.intR (-2)).add (Quad.quad LibInt.intR (-2)).zero (sumK (Quad.quad LibInt.intR (-2)) (fun _ => (Quad.quad LibInt.intR (-2)).zero) l) = _; rw [zsum8 l]; exact (Quad.quad LibInt.intR (-2)).zero_add _

def I8 (n : Nat) (z : Int × Int) : Int × Int := if PD8 n z then (Quad.quad LibInt.intR (-2)).one else (Quad.quad LibInt.intR (-2)).zero

theorem I8_pos (n : Nat) (z : Int × Int) (h : PD8 n z) : I8 n z = (Quad.quad LibInt.intR (-2)).one := ite_pos' _ _ h
theorem I8_neg (n : Nat) (z : Int × Int) (h : ¬ PD8 n z) : I8 n z = (Quad.quad LibInt.intR (-2)).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw8 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Quad.quad LibInt.intR (-2)) (fun z => (Quad.quad LibInt.intR (-2)).mul (I8 n z) (w z)) (box8 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult8 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (hn : ∃ k, Int.ofNat (n + 1) = 2 * k + 1) :
    cw8 w ((m + 1) * (n + 1)) = (Quad.quad LibInt.intR (-2)).mul (cw8 w (m + 1)) (cw8 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD8 (m + 1) α → PD8 (n + 1) β → PD8 ((m + 1) * (n + 1)) ((Quad.quad LibInt.intR (-2)).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim8_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Quad.quad LibInt.intR (-2)).mul (cw8 w (m + 1)) (cw8 w (n + 1))
      = sumK (Quad.quad LibInt.intR (-2)) (fun α => sumK (Quad.quad LibInt.intR (-2)) (fun β => sumK (Quad.quad LibInt.intR (-2)) (fun γ => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I8 (m + 1) α) (I8 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ)) (box8 ((m + 1) * (n + 1)))) (box8 (n + 1))  -- (+22 chars; full line in the Zenodo source)
    unfold cw8; rw [sumK_mul]
    refine sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun β => ?_) _
    by_cases hα : PD8 (m + 1) α
    · by_cases hβ : PD8 (n + 1) β
      · rw [I8_pos _ _ hα, I8_pos _ _ hβ]
        have hb := abs_le8 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Quad.quad LibInt.intR (-2)) (fun γ => w γ) ((Quad.quad LibInt.intR (-2)).mul α β) _ hb.1 hb.2
        rw [show sumK (Quad.quad LibInt.intR (-2)) (fun γ => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one (Quad.quad LibInt.intR (-2)).one) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ)) (box8 ((m + 1) * (n + 1)))
            = sumK (Quad.quad LibInt.intR (-2)) (fun γ => (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β)) (w γ)) (box8 ((m + 1) * (n + 1))) from sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I8_neg _ _ hβ]
        rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I8 (m + 1) α) (Quad.quad LibInt.intR (-2)).zero) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ) = (Quad.quad LibInt.intR (-2)).zero by lib_ring)]
        rw [zsum8]; lib_ring
    · rw [I8_neg _ _ hα]
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).zero (I8 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ) = (Quad.quad LibInt.intR (-2)).zero by lib_ring)]
      rw [zsum8]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => sumK_swap (Quad.quad LibInt.intR (-2)) _ _ _)]
  rw [sumK_swap (Quad.quad LibInt.intR (-2))]
  unfold cw8
  refine sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun γ => ?_) _
  have e1 : sumK (Quad.quad LibInt.intR (-2)) (fun α => sumK (Quad.quad LibInt.intR (-2)) (fun β => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I8 (m + 1) α) (I8 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (w γ)) (box8 (n + 1))) (box8 (m + 1))
      = (Quad.quad LibInt.intR (-2)).mul (sumK (Quad.quad LibInt.intR (-2)) (fun α => sumK (Quad.quad LibInt.intR (-2)) (fun β => (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I8 (m + 1) α) (I8 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β))) (box8 (n + 1))) (box8 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD8 ((m + 1) * (n + 1)) γ
  · rw [I8_pos _ _ hγ]
    match prim8_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I8 (m + 1) α) (I8 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β)) = (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (delta (Quad.quad LibInt.intR (-2)) β β0) := by
        intro α β
        by_cases hαβ : PD8 (m + 1) α ∧ PD8 (n + 1) β ∧ γ = (Quad.quad LibInt.intR (-2)).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split8 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I8_pos _ _ hα, I8_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (delta (Quad.quad LibInt.intR (-2)) β β0) = (Quad.quad LibInt.intR (-2)).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD8 (m + 1) α
            · by_cases hβ : PD8 (n + 1) β
              · have hg : ¬ γ = (Quad.quad LibInt.intR (-2)).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I8_neg _ _ hβ]; lib_ring
            · rw [I8_neg _ _ hα]; lib_ring
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun β => pt α β) _)]
      have ba := abs_le8 α0 _ nα0
      have bb := abs_le8 β0 _ nβ0
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Quad.quad LibInt.intR (-2)) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => show (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (Quad.quad LibInt.intR (-2)).one = (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) α α0) (Quad.quad LibInt.intR (-2)).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Quad.quad LibInt.intR (-2)) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I8_neg _ _ hγ]
    have pt : ∀ α β, (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (I8 (m + 1) α) (I8 (n + 1) β)) (delta (Quad.quad LibInt.intR (-2)) γ ((Quad.quad LibInt.intR (-2)).mul α β)) = (Quad.quad LibInt.intR (-2)).zero := by
      intro α β
      by_cases hα : PD8 (m + 1) α
      · by_cases hβ : PD8 (n + 1) β
        · have hg : ¬ γ = (Quad.quad LibInt.intR (-2)).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I8_neg _ _ hβ]; lib_ring
      · rw [I8_neg _ _ hα]; lib_ring
    rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun α => zsum8 _), zsum8]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult8_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (hn : ∃ k, Int.ofNat (n + 1) = 2 * k + 1) :
    cw8 (fun z => Frobenius.pw (Quad.quad LibInt.intR (-2)) z k) ((m + 1) * (n + 1)) =
      (Quad.quad LibInt.intR (-2)).mul (cw8 (fun z => Frobenius.pw (Quad.quad LibInt.intR (-2)) z k) (m + 1)) (cw8 (fun z => Frobenius.pw (Quad.quad LibInt.intR (-2)) z k) (n + 1)) :=
  hecke_mult8 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult8

#print axioms HeckeMult8.abs_le8
#print axioms HeckeMult8.prim8_iff
#print axioms HeckeMult8.zsum8
#print axioms HeckeMult8.I8_pos
#print axioms HeckeMult8.I8_neg
#print axioms HeckeMult8.hecke_mult8
#print axioms HeckeMult8.hecke_mult8_pow
#print axioms HeckeMult8.cw8

#print axioms: 8 theorems, no axioms.

78.246 Factorisation in \(\mathbb Z[\sqrt {-2}]\) above a split prime

An element of prime norm is prime, and the \(H_8\)-elements of norm \(p^k\) are the \(\pi ^j\bar \pi ^l\).

\[ N(\alpha )=p^k,\ \alpha \in H_8\ \Rightarrow \ \alpha =\pi ^j\bar \pi ^{\,l},\ j+l=k. \]

lang/Sq2Prime8.lean

/-
  lang/Sq2Prime8 — T48/T50, cycle 633: ℤ[√−2] above a split prime p = a² + 2b²:
    eprime_div    N(π) = p prime ⇒ π prime;   cj_mul, cj_cj
    factor3       N(α) = p^k ⇒ α = u π^j π̄^l
    primary_only3 π, α ∈ H8 (Sq2Prim8), N(α) = p^k ⇒ α = π^j π̄^l   (H8 is conjugation-stable, prim8_cj)
  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult8

namespace Sq2Prime8

open FTAChain DetChain LibInt Sq2Euclid Sq2Prim8 Sq2Unique8 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Quad.quad LibInt.intR (-2)).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Quad.quad LibInt.intR (-2)).mul u (cj u) = (Quad.quad LibInt.intR (-2)).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Quad.quad LibInt.intR (-2)).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists _ π x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Quad.quad LibInt.intR (-2)).mul (cj d) X) ((Quad.quad LibInt.intR (-2)).mul (cj d) Y) ?_ h)
      rw [show (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (cj d) X) π) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (cj d) Y) x)
          = (Quad.quad LibInt.intR (-2)).mul (cj d) ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul X π) ((Quad.quad LibInt.intR (-2)).mul Y x)) by lib_ring,
        ← hd, (Quad.quad LibInt.intR (-2)).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Quad.quad LibInt.intR (-2)).mul (cj c1) c2, ?_⟩
      have hd' : d = (Quad.quad LibInt.intR (-2)).mul π (cj c1) := by
        rw [hc1]
        calc d = (Quad.quad LibInt.intR (-2)).mul d (Quad.quad LibInt.intR (-2)).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Quad.quad LibInt.intR (-2)).mul x y) = (Quad.quad LibInt.intR (-2)).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -2 * (x.2 * y.2) = x.1 * y.1 + -2 * (-x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1) = x.1 * -y.2 + -x.2 * y.1
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · rfl
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := Int.ofNat.inj (by rw [nrm_cast, nrm_cast, en_cj])

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Quad.quad LibInt.intR (-2)).mul u ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) (pw (Quad.quad LibInt.intR (-2)) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Quad.quad LibInt.intR (-2)).mul α ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one (Quad.quad LibInt.intR (-2)).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Quad.quad LibInt.intR (-2)).mul α (cj α)) := by
      refine ⟨(Quad.quad LibInt.intR (-2)).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Quad.quad LibInt.intR (-2)).mul π ((Quad.quad LibInt.intR (-2)).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Quad.quad LibInt.intR (-2)).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Quad.quad LibInt.intR (-2)).mul u ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Quad.quad LibInt.intR (-2)).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Quad.quad LibInt.intR (-2)).mul u ((Quad.quad LibInt.intR (-2)).mul _ ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) (cj π) l) (cj π))); lib_ring

theorem prim8_one : Prim8 (Quad.quad LibInt.intR (-2)).one := Or.inl ⟨0, 0, rfl, rfl⟩

theorem prim8_pow (z : Int × Int) (h : Prim8 z) : ∀ j, Prim8 (pw (Quad.quad LibInt.intR (-2)) z j)
  | 0 => prim8_one
  | j + 1 => prim8_mul _ _ (prim8_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim8 α) :
    ∃ j l, j + l = k ∧ α = (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) (pw (Quad.quad LibInt.intR (-2)) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim8_mul _ _ (prim8_pow π pπ j) (prim8_pow (cj π) (prim8_cj π pπ) l)
  have u1 := unit_one8 u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Sq2Prime8

#print axioms Sq2Prime8.nrm_mul
#print axioms Sq2Prime8.unit_inv
#print axioms Sq2Prime8.eprime_div
#print axioms Sq2Prime8.cj_mul
#print axioms Sq2Prime8.cj_cj
#print axioms Sq2Prime8.nrm_cj
#print axioms Sq2Prime8.nrm_one_eq
#print axioms Sq2Prime8.factor3
#print axioms Sq2Prime8.prim8_one
#print axioms Sq2Prime8.prim8_pow
#print axioms Sq2Prime8.primary_only3

#print axioms: 11 theorems, no axioms.

78.247 256a: the Hecke recursion at split primes

For every multiplicative \(W\) the coefficient at \(p^k\) is a sum over \(\pi ^j\bar \pi ^l\) and obeys the Hecke recursion.

\[ c_W(p^{k+2})=(W(\pi )+W(\bar \pi ))\,c_W(p^{k+1})-W(\pi )W(\bar \pi )\,c_W(p^k). \]

lang/HeckePrime8.lean

/-
  lang/HeckePrime8 — T48/T50, cycle 634: **256a's Hecke character at split prime powers**: p = a² + 2b² prime, π ∈ H8 of norm p,
  W multiplicative with W(1) = 1:  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l  and the Hecke recursion
    c_W(p^(k+2)) = (W(π) + W(π̄)) c_W(p^(k+1)) − W(π)W(π̄) c_W(p^k).        (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Sq2Prime8
import lang.HeckePrimeW

namespace HeckePrime8

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Sq2Euclid Sq2Prim8 Sq2Unique8 Sq2Prime8 HeckeMult8

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Quad.quad LibInt.intR (-2)) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; exact Int.ofNat.inj (by rw [nrm_cast, e]; rfl)

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) A = (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) B) : A = B := by
  have h0 : (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) ((Quad.quad LibInt.intR (-2)).add A ((Quad.quad LibInt.intR (-2)).neg B)) = (Quad.quad LibInt.intR (-2)).zero := by
    rw [show (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) ((Quad.quad LibInt.intR (-2)).add A ((Quad.quad LibInt.intR (-2)).neg B)) = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) A) ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) B)) by lib_ring, h]; lib_r  -- (+3 chars; full line in the Zenodo source)
  have := Sq2Coprime.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).add A ((Quad.quad LibInt.intR (-2)).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one8 c π (nrm_one_eq c nc) pπ (by rw [(Quad.quad LibInt.intR (-2)).mul_comm, ← hc]; exact prim8_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + 2 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) :
    ∀ d, Dvd' π (pw (Quad.quad LibInt.intR (-2)) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) (j d l' : Nat)
    (h : (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) (pw (Quad.quad LibInt.intR (-2)) (cj π) (l' + 1 + d)) = (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π (j + 1 + d)) (pw (Quad.quad LibInt.intR (-2)) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Quad.quad LibInt.intR (-2)) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Quad.quad LibInt.intR (-2)) π j (1 + d)] at h
  have h' : (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) (cj π) l') (pw (Quad.quad LibInt.intR (-2)) (cj π) (1 + d)))
      = (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) (cj π) l') (pw (Quad.quad LibInt.intR (-2)) π (1 + d))) := by
    calc _ = (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) (pw (Quad.quad LibInt.intR (-2)) π (1 + d))) (pw (Quad.quad LibInt.intR (-2)) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Quad.quad LibInt.intR (-2)) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j) (pw (Quad.quad LibInt.intR (-2)) (cj π) l) = (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π j') (pw (Quad.quad LibInt.intR (-2)) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) π x.1) (pw (Quad.quad LibInt.intR (-2)) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD8 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim8_mul _ _ (prim8_pow π pπ _) (prim8_pow (cj π) (prim8_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) (k : Nat) :
    cw8 W ((P + 1 + 1) ^ k) = sumK (Quad.quad LibInt.intR (-2)) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I8 ((P + 1 + 1) ^ k) z = sumK (Quad.quad LibInt.intR (-2)) (fun x => delta (Quad.quad LibInt.intR (-2)) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD8 ((P + 1 + 1) ^ k) z
    · rw [I8_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := Int.ofNat.inj (by rw [nrm_cast, hz.1])
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Quad.quad LibInt.intR (-2)) k _ (fun x => delta (Quad.quad LibInt.intR (-2)) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Quad.quad LibInt.intR (-2)) k j l hjl]
    · rw [I8_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Quad.quad LibInt.intR (-2)) k _ (fun _ => (Quad.quad LibInt.intR (-2)).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Quad.quad LibInt.intR (-2)) _).symm
  unfold cw8
  rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Quad.quad LibInt.intR (-2)) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le8 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Quad.quad LibInt.intR (-2)) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (hW1 : W (Quad.quad LibInt.intR (-2)).one = (Quad.quad LibInt.intR (-2)).one) (z : Int × Int) :
    ∀ j, W (pw (Quad.quad LibInt.intR (-2)) z j) = pw (Quad.quad LibInt.intR (-2)) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) z j) z) = (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (hW1 : W (Quad.quad LibInt.intR (-2)).one = (Quad.quad LibInt.intR (-2)).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) (k : Nat) :
    cw8 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Quad.quad LibInt.intR (-2)) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (hW1 : W (Quad.quad LibInt.intR (-2)).one = (Quad.quad LibInt.intR (-2)).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π) (k : Nat) :
    cw8 W ((P + 1 + 1) ^ (k + 2)) = (Quad.quad LibInt.intR (-2)).add
      ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).add (W π) (W (cj π))) (cw8 W ((P + 1 + 1) ^ (k + 1))))
      ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (W π) (W (cj π))) (cw8 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime8

#print axioms HeckePrime8.pow_ne0
#print axioms HeckePrime8.nrm_pw
#print axioms HeckePrime8.npow_add
#print axioms HeckePrime8.en_ne
#print axioms HeckePrime8.cancel_pw3
#print axioms HeckePrime8.no_assoc3
#print axioms HeckePrime8.no_div3
#print axioms HeckePrime8.distinct_lt3
#print axioms HeckePrime8.distinct3
#print axioms HeckePrime8.w3_PD
#print axioms HeckePrime8.cw3_pk
#print axioms HeckePrime8.W_pow3
#print axioms HeckePrime8.cw3_pk'
#print axioms HeckePrime8.cw3_rec

#print axioms: 14 theorems, no axioms.

78.248 256a: the Hecke character at inert primes

A prime \(p\equiv 5,7\) mod \(8\) stays prime in \(\mathbb Z[\sqrt {-2}]\); the only \(H_8\)-element of norm \(p^{2k}\) is \((-p)^k\).

\[ c_W(p^{2k})=W((-p)^k),\qquad c_W(p^{2k+1})=0. \]

lang/Sq2Inert8.lean

/-
  lang/Sq2Inert8 — T48/T50, cycle 635: **256a's Hecke character at inert primes** p ≡ 5, 7 (mod 8):
    en_mod8        N(a + b√−2) = a² + 2b² is never ≡ 5, 7 (mod 8)
    cw8_zero       c_W(n) = 0 for n ≡ 5, 7 (mod 8)
    inert_div      p | xy ⇒ p | x ∨ p | y        (gcd has norm 1, p or p²; norm p is impossible)
    factor_inert, prim_inert     the H8-elements of norm p^(2k) are (−p)^k
    cw8_even       c_W(p^(2k)) = W((−p)^k);   cw8_odd   c_W(p^(2k+1)) = 0
  (Port of EisInert3.) With CM2/Chi2 (a_p(y² = x³ + 4x² + 2x) = 0 at p ≡ 5, 7 mod 8) the inert primes match.
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime8

namespace Sq2Inert8

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Sq2Euclid Sq2Prim8 Sq2Unique8 Sq2Prime8 HeckeMult8

variable (P : Nat)

theorem en_mod8 (z : Int × Int) (k : Int) (h : en z = 8 * k + 5 ∨ en z = 8 * k + 7) : False := by
  obtain ⟨a, b⟩ := z
  have hn : en (a, b) = a * a + 2 * (b * b) := rfl
  obtain ⟨u, hu⟩ := Sq2Prim8.mod4 a
  rcases GaussInt.int_even_odd b with ⟨v, hv⟩ | ⟨v, hv⟩
  · rcases hu with hu | hu | hu | hu
    · rcases h with h | h
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 0 + v * v + v * 0) + -k + -(0)) 4 2 (by calc (8 : Int) * ((2 * u * u + u * 0 + v * v + v * 0) + -k + -(0)) = (a * a + 2 * (b * b)) + -(8 * k + 5) + 5 := by rw [hu, hv]; lib_int
          _ = 5 := by rw [← hn, h]; lib_int) rfl
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 0 + v * v + v * 0) + -k + -(0)) 6 0 (by calc (8 : Int) * ((2 * u * u + u * 0 + v * v + v * 0) + -k + -(0)) = (a * a + 2 * (b * b)) + -(8 * k + 7) + 7 := by rw [hu, hv]; lib_int
          _ = 7 := by rw [← hn, h]; lib_int) rfl
    · rcases h with h | h
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 1 + v * v + v * 0) + -k + -(0)) 3 3 (by calc (8 : Int) * ((2 * u * u + u * 1 + v * v + v * 0) + -k + -(0)) = (a * a + 2 * (b * b)) + -(8 * k + 5) + 4 := by rw [hu, hv]; lib_int
          _ = 4 := by rw [← hn, h]; lib_int) rfl
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 1 + v * v + v * 0) + -k + -(0)) 5 1 (by calc (8 : Int) * ((2 * u * u + u * 1 + v * v + v * 0) + -k + -(0)) = (a * a + 2 * (b * b)) + -(8 * k + 7) + 6 := by rw [hu, hv]; lib_int
          _ = 6 := by rw [← hn, h]; lib_int) rfl
    · rcases h with h | h
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 2 + v * v + v * 0) + -k + -(0)) 0 6 (by calc (8 : Int) * ((2 * u * u + u * 2 + v * v + v * 0) + -k + -(0)) = (a * a + 2 * (b * b)) + -(8 * k + 5) + 1 := by rw [hu, hv]; lib_int
          _ = 1 := by rw [← hn, h]; lib_int) rfl
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 2 + v * v + v * 0) + -k + -(0)) 2 4 (by calc (8 : Int) * ((2 * u * u + u * 2 + v * v + v * 0) + -k + -(0)) = (a * a + 2 * (b * b)) + -(8 * k + 7) + 3 := by rw [hu, hv]; lib_int
          _ = 3 := by rw [← hn, h]; lib_int) rfl
    · rcases h with h | h
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 3 + v * v + v * 0) + -k + -(-1)) 3 3 (by calc (8 : Int) * ((2 * u * u + u * 3 + v * v + v * 0) + -k + -(-1)) = (a * a + 2 * (b * b)) + -(8 * k + 5) + 4 := by rw [hu, hv]; lib_int
          _ = 4 := by rw [← hn, h]; lib_int) rfl
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 3 + v * v + v * 0) + -k + -(-1)) 5 1 (by calc (8 : Int) * ((2 * u * u + u * 3 + v * v + v * 0) + -k + -(-1)) = (a * a + 2 * (b * b)) + -(8 * k + 7) + 6 := by rw [hu, hv]; lib_int
          _ = 6 := by rw [← hn, h]; lib_int) rfl
  · rcases hu with hu | hu | hu | hu
    · rcases h with h | h
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 0 + v * v + v * 1) + -k + -(0)) 2 4 (by calc (8 : Int) * ((2 * u * u + u * 0 + v * v + v * 1) + -k + -(0)) = (a * a + 2 * (b * b)) + -(8 * k + 5) + 3 := by rw [hu, hv]; lib_int
          _ = 3 := by rw [← hn, h]; lib_int) rfl
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 0 + v * v + v * 1) + -k + -(0)) 4 2 (by calc (8 : Int) * ((2 * u * u + u * 0 + v * v + v * 1) + -k + -(0)) = (a * a + 2 * (b * b)) + -(8 * k + 7) + 5 := by rw [hu, hv]; lib_int
          _ = 5 := by rw [← hn, h]; lib_int) rfl
    · rcases h with h | h
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 1 + v * v + v * 1) + -k + -(0)) 1 5 (by calc (8 : Int) * ((2 * u * u + u * 1 + v * v + v * 1) + -k + -(0)) = (a * a + 2 * (b * b)) + -(8 * k + 5) + 2 := by rw [hu, hv]; lib_int
          _ = 2 := by rw [← hn, h]; lib_int) rfl
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 1 + v * v + v * 1) + -k + -(0)) 3 3 (by calc (8 : Int) * ((2 * u * u + u * 1 + v * v + v * 1) + -k + -(0)) = (a * a + 2 * (b * b)) + -(8 * k + 7) + 4 := by rw [hu, hv]; lib_int
          _ = 4 := by rw [← hn, h]; lib_int) rfl
    · rcases h with h | h
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 2 + v * v + v * 1) + -k + -(-1)) 6 0 (by calc (8 : Int) * ((2 * u * u + u * 2 + v * v + v * 1) + -k + -(-1)) = (a * a + 2 * (b * b)) + -(8 * k + 5) + 7 := by rw [hu, hv]; lib_int
          _ = 7 := by rw [← hn, h]; lib_int) rfl
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 2 + v * v + v * 1) + -k + -(0)) 0 6 (by calc (8 : Int) * ((2 * u * u + u * 2 + v * v + v * 1) + -k + -(0)) = (a * a + 2 * (b * b)) + -(8 * k + 7) + 1 := by rw [hu, hv]; lib_int
          _ = 1 := by rw [← hn, h]; lib_int) rfl
    · rcases h with h | h
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 3 + v * v + v * 1) + -k + -(-1)) 1 5 (by calc (8 : Int) * ((2 * u * u + u * 3 + v * v + v * 1) + -k + -(-1)) = (a * a + 2 * (b * b)) + -(8 * k + 5) + 2 := by rw [hu, hv]; lib_int
          _ = 2 := by rw [← hn, h]; lib_int) rfl
      · exact Sq2Prim8.noM 7 ((2 * u * u + u * 3 + v * v + v * 1) + -k + -(-1)) 3 3 (by calc (8 : Int) * ((2 * u * u + u * 3 + v * v + v * 1) + -k + -(-1)) = (a * a + 2 * (b * b)) + -(8 * k + 7) + 4 := by rw [hu, hv]; lib_int
          _ = 4 := by rw [← hn, h]; lib_int) rfl

/-- **c_W(n) = 0 for n ≡ 5, 7 (mod 8).** -/
theorem cw8_zero (W : Int × Int → Int × Int) (n : Nat) (k : Int) (hn : Int.ofNat n = 8 * k + 5 ∨ Int.ofNat n = 8 * k + 7) : cw8 W n = (Quad.quad LibInt.intR (-2)).zero := by
  unfold cw8
  rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ (fun _ => (Quad.quad LibInt.intR (-2)).zero) (fun z => by
    rw [I8_neg _ _ (fun h => en_mod8 z k (by rw [h.1]; exact hn))]; lib_ring)]
  exact zsum8 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + -(Int.ofNat (P + 1 + 1) * 0) + 0 * 0 = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + -0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : ∃ K, Int.ofNat (P + 1 + 1) = 8 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 8 * K + 7) (z : Int × Int) (h : nrm z = P + 1 + 1) : False := by
  obtain ⟨K, hK⟩ := hc
  exact en_mod8 z K (by rw [← nrm_cast, h]; exact hK)

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 8 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 8 * K + 7) (x y : Int × Int)
    (h : Dvd' (Pi P) ((Quad.quad LibInt.intR (-2)).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists _ (Pi P) x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Quad.quad LibInt.intR (-2)).mul (cj d) X) ((Quad.quad LibInt.intR (-2)).mul (cj d) Y) ?_ h)
      rw [show (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (cj d) X) (Pi P)) ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (cj d) Y) x)
          = (Quad.quad LibInt.intR (-2)).mul (cj d) ((Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul X (Pi P)) ((Quad.quad LibInt.intR (-2)).mul Y x)) by lib_ring,
        ← hd, (Quad.quad LibInt.intR (-2)).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Quad.quad LibInt.intR (-2)).mul (cj c1) c2, ?_⟩
      have hd' : d = (Quad.quad LibInt.intR (-2)).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Quad.quad LibInt.intR (-2)).mul d (Quad.quad LibInt.intR (-2)).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 8 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 8 * K + 7) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Quad.quad LibInt.intR (-2)).mul u (pw (Quad.quad LibInt.intR (-2)) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Quad.quad LibInt.intR (-2)).mul α (Quad.quad LibInt.intR (-2)).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Quad.quad LibInt.intR (-2)).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Quad.quad LibInt.intR (-2)).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Quad.quad LibInt.intR (-2)).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Quad.quad LibInt.intR (-2)).mul u ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : ∃ K, Int.ofNat (P + 1 + 1) = 8 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 8 * K + 7) : Prim8 (nPi P) := by
  obtain ⟨K, hK | hK⟩ := hc
  · exact Or.inr (Or.inl ⟨-K + -1, 0, by show -Int.ofNat (P + 1 + 1) = _; rw [hK]; lib_int, rfl⟩)
  · exact Or.inl ⟨-K + -1, 0, by show -Int.ofNat (P + 1 + 1) = _; rw [hK]; lib_int, rfl⟩

theorem pw_neg (k : Nat) : pw (Quad.quad LibInt.intR (-2)) (Pi P) k =
    (Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) ((Quad.quad LibInt.intR (-2)).neg (Quad.quad LibInt.intR (-2)).one) k) (pw (Quad.quad LibInt.intR (-2)) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -2 * (-0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1); lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Quad.quad LibInt.intR (-2)) ((Quad.quad LibInt.intR (-2)).neg (Quad.quad LibInt.intR (-2)).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Quad.quad LibInt.intR (-2)).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 8 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 8 * K + 7) (k : Nat) (α : Int × Int)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim8 α) : α = pw (Quad.quad LibInt.intR (-2)) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Quad.quad LibInt.intR (-2)).mul u (pw (Quad.quad LibInt.intR (-2)) ((Quad.quad LibInt.intR (-2)).neg (Quad.quad LibInt.intR (-2)).one) k) = v
  have he' : α = (Quad.quad LibInt.intR (-2)).mul v (pw (Quad.quad LibInt.intR (-2)) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one8 v _ (nrm_one_eq v hv1) (prim8_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -(-Int.ofNat (P + 1 + 1) * 0) + 0 * 0 = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Quad.quad LibInt.intR (-2)) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime8.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw8_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 8 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 8 * K + 7) (k : Nat) :
    cw8 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Quad.quad LibInt.intR (-2)) (nPi P) k) := by
  have hPD : PD8 ((P + 1 + 1) ^ (2 * k)) (pw (Quad.quad LibInt.intR (-2)) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim8_pow _ (prim_nPi P hc) k⟩
  unfold cw8
  rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ (fun z => (Quad.quad LibInt.intR (-2)).mul (delta (Quad.quad LibInt.intR (-2)) z (pw (Quad.quad LibInt.intR (-2)) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD8 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I8_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I8_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le8 _ _ hPD.1
  exact HeckeMult.pick_box (Quad.quad LibInt.intR (-2)) W _ _ hb.1 hb.2

theorem sq8 (Y L : Int) (hL : Y = 8 * L + 5 ∨ Y = 8 * L + 7) : ∃ M : Int, Y * Y = 8 * M + 1 := by
  rcases hL with hL | hL
  · exact ⟨8 * L * L + 10 * L + 3, by rw [hL]; lib_int⟩
  · exact ⟨8 * L * L + 14 * L + 6, by rw [hL]; lib_int⟩

theorem step8 (X Y K L : Int) (hK : X = 8 * K + 5 ∨ X = 8 * K + 7) (hL : Y = 8 * L + 5 ∨ Y = 8 * L + 7) :
    ∃ W : Int, X * Y * Y = 8 * W + 5 ∨ X * Y * Y = 8 * W + 7 := by
  obtain ⟨M, hM⟩ := sq8 Y L hL
  have e : X * Y * Y = X * (Y * Y) := by lib_int
  rw [e, hM]
  rcases hK with hK | hK
  · exact ⟨8 * K * M + K + 5 * M, Or.inl (by rw [hK]; lib_int)⟩
  · exact ⟨8 * K * M + K + 7 * M, Or.inr (by rw [hK]; lib_int)⟩

theorem pow_odd (hc : ∃ K, Int.ofNat (P + 1 + 1) = 8 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 8 * K + 7) (k : Nat) : ∃ K : Int, Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 8 * K + 5 ∨ Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 8 * K + 7 := by
  obtain ⟨L, hL⟩ := hc
  induction k with
  | zero => exact ⟨L, by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hL⟩
  | succ k ih =>
    obtain ⟨K, hK⟩ := ih
    have e : Int.ofNat ((P + 1 + 1) ^ (2 * (k + 1) + 1)) = Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) := by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; rfl
    rw [e]
    exact step8 _ _ K L hK hL

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw8_odd (W : Int × Int → Int × Int) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 8 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 8 * K + 7) (k : Nat) : cw8 W ((P + 1 + 1) ^ (2 * k + 1)) = (Quad.quad LibInt.intR (-2)).zero := by
  obtain ⟨K, hK⟩ := pow_odd P hc k
  exact cw8_zero W _ K hK

end Sq2Inert8

#print axioms Sq2Inert8.en_mod8
#print axioms Sq2Inert8.cw8_zero
#print axioms Sq2Inert8.en_Pi
#print axioms Sq2Inert8.nrm_Pi
#print axioms Sq2Inert8.cj_Pi
#print axioms Sq2Inert8.not_p
#print axioms Sq2Inert8.dvd_conv
#print axioms Sq2Inert8.split_sq
#print axioms Sq2Inert8.inert_div
#print axioms Sq2Inert8.nat_pow2
#print axioms Sq2Inert8.factor_inert
#print axioms Sq2Inert8.prim_nPi
#print axioms Sq2Inert8.pw_neg
#print axioms Sq2Inert8.nrm_neg_one_pw
#print axioms Sq2Inert8.prim_inert
#print axioms Sq2Inert8.nrm_nPi
#print axioms Sq2Inert8.nrm_nPi_pw
#print axioms Sq2Inert8.cw8_even
#print axioms Sq2Inert8.sq8
#print axioms Sq2Inert8.step8
#print axioms Sq2Inert8.pow_odd
#print axioms Sq2Inert8.cw8_odd

#print axioms: 22 theorems, no axioms.

78.249 256a at the inert primes

At \(p\equiv 5,7\) mod \(8\) both the character sum of the curve and the Hecke coefficient vanish.

\[ \sum _x\chi _p(x^3+4x^2+2x)=-c(p)=0. \]

lang/Curve256Inert.lean

/-
  lang/Curve256Inert — T48, cycle 636: **y² = x³ + 4x² + 2x (256a) at the inert primes p ≡ 5, 7 (mod 8): a_p = c(p) = 0**,
  where c is the theta series of the ℚ(√−2) Hecke character α ↦ α on the class H8 (HeckeMult8).
    curve256_inert   Σ_x χ_p(x³ + 4x² + 2x) = −(c(p)).1 = 0 and (c(p)).2 = 0      (Chi2.cm2_mod8, Sq2Inert8.cw8_zero)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Chi2
import lang.Sq2Inert8

namespace Curve256Inert

open FTAChain ZMod YMChain FiniteRecChain QChar SqCount

theorem curve256_inert (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (k : Nat)
    (hp : 2 * m + 1 + 1 + 1 = 8 * k + 5 ∨ 2 * m + 1 + 1 + 1 = 8 * k + 7) :
    sumK LibInt.intR (fun x => chi m ((K m).mul x ((K m).add ((K m).add ((K m).mul x x) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) x)) ((K m).add (K m).one (K m).one)))) (ConicSum.L m)
      = -(HeckeMult8.cw8 (fun z => z) (2 * m + 1 + 1 + 1)).1 ∧ (HeckeMult8.cw8 (fun z => z) (2 * m + 1 + 1 + 1)).2 = 0 := by
  have hc : Int.ofNat (2 * m + 1 + 1 + 1) = 8 * Int.ofNat k + 5 ∨ Int.ofNat (2 * m + 1 + 1 + 1) = 8 * Int.ofNat k + 7 := by
    rcases hp with h | h
    · exact Or.inl (by rw [h]; rfl)
    · exact Or.inr (by rw [h]; rfl)
  rw [cm2_mod8 m hpr k hp, Sq2Inert8.cw8_zero _ _ _ hc]
  exact ⟨rfl, rfl⟩

end Curve256Inert

#print axioms Curve256Inert.curve256_inert

#print axioms: 1 theorems, no axioms.

78.250 256a: the quotient curve as a twist

The elliptic quotient of \(y^2=x^5-x\) is \(256a\) twisted by \(\sigma ^3h\).

\[ \sum _X\chi \big ((2X^2-\sigma ^2)(2X-2\sigma )\big )=\chi (\sigma ^3h)\sum _z\chi \big (z(z^2+4z+2)\big ). \]

lang/Q256.lean

/-
  lang/Q256 — T48, cycle 721: **the linear step of the genus-2 route for 256a** (OPEN 256a-split, step (i)).
  For σ ≠ 0 and 2h = 1 in 𝔽_p (h = 1/2):
    pt_id      (2X² − σ²)(2X − 2σ) = σ³h · z(z² + 4z + 2)  at X = σh(2 + z)
    quot_sub   Σ_X χ((2X² − σ²)(2X − 2σ)) = χ(σ³h) · Σ_z χ(z(z² + 4z + 2))
  i.e. the elliptic quotient C_σ of y² = x⁵ − x is 256a twisted by σ³h.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CharSumGen
import lang.G4Gen

namespace Q256

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar

variable (m : Nat)

theorem pt_id (σ h z : Zp (2 * m + 1 + 1)) (hh : (ring (2 * m + 1 + 1)).add h h = (ring (2 * m + 1 + 1)).one) :
    ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul σ h) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one z))) ((ring (2 * m + 1 + 1)).m  -- (+1237 chars; full line in the Zenodo source)
  have e0 : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add h h) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [hh]; lib_ring
  have hq : ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul σ h) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one z))) ((ring (2 * m + 1  -- (+6543 chars; full line in the Zenodo source)
  rw [hq, e0]; lib_ring

theorem quot_sub (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (σ h : Zp (2 * m + 1 + 1))
    (hs : σ ≠ (ring (2 * m + 1 + 1)).zero) (hh : (ring (2 * m + 1 + 1)).add h h = (ring (2 * m + 1 + 1)).one) :
    sumK LibInt.intR (fun X => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul X X)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul σ σ))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m +   -- (+214 chars; full line in the Zenodo source)
      chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul σ σ) σ) h) * sumK LibInt.intR (fun z => chi m ((ring (2 * m + 1 + 1)).mul z ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)   -- (+203 chars; full line in the Zenodo source)
  have hh0 : h ≠ (ring (2 * m + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * m + 1) (by rw [← hh, e]; lib_ring)
  have hsh : (ring (2 * m + 1 + 1)).mul σ h ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
    rcases G4Gen.mul_eq_zero m hpr σ h e with e1 | e1
    · exact hs e1
    · exact hh0 e1
  rw [← CharSumGen.sum_scale LibInt.intR (2 * m) hpr _ ((ring (2 * m + 1 + 1)).mul σ h) hsh, ← CharSumGen.shift1 LibInt.intR (2 * m), ← CharSumGen.shift1 LibInt.intR (2 * m)]
  show _ = LibInt.intR.mul _ _
  rw [mul_sumK]
  exact sumK_ext LibInt.intR _ _ (fun z => by
    show chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul σ h) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one z))) ((ring (2 * m   -- (+691 chars; full line in the Zenodo source)
    rw [pt_id m σ h z hh, chi_mul m hpr]; rfl) _

end Q256

#print axioms Q256.pt_id
#print axioms Q256.quot_sub

#print axioms: 2 theorems, no axioms.

78.251 The fibre sum for \(X=x+c/x\)

Each \(X\) has \(1+\chi (X^2-4c)\) preimages under \(x\mapsto x+c/x\).

\[ \sum _{x\ne 0}g(x+c/x)=\sum _X g(X)\big (1+\chi (X^2-4c)\big ). \]

lang/FibSum.lean

/-
  lang/FibSum — T48, cycle 722: **the 2-to-1 fibre sum for X = x + c/x** (OPEN 256a-split, step (ii-a)).
  In 𝔽_p (p = 2m + 3 prime), c ≠ 0, 2h = 1, for every g : 𝔽_p → ℤ:
    key        [(2x − X)² = X² − 4c] = [x ≠ 0] · [X = x + c/x]
    fib_sum    Σ_{x ≠ 0} g(x + c/x) = Σ_X g(X) · (1 + χ(X² − 4c))
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.SqCount
import lang.CharSumGen
import lang.G4Gen

namespace FibSum

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount

variable (m : Nat)

theorem four_cancel (h a : Zp (2 * m + 1 + 1))
    (hh : (K m).add h h = (K m).one) (ha : (K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) a = (K m).zero) :
    a = (K m).zero := by
  have e : a = (K m).mul ((K m).mul h h) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) a) := by
    calc a = (K m).mul ((K m).mul ((K m).add h h) ((K m).add h h)) a := by rw [hh]; lib_ring
      _ = _ := by lib_ring
  rw [e, ha]; lib_ring

/-- [(2x − X)² = X² − 4c] ⇔ x ≠ 0 ∧ X = x + c/x. -/
theorem key (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h c : Zp (2 * m + 1 + 1))
    (hh : (K m).add h h = (K m).one) (hc : c ≠ (K m).zero) (x X : Zp (2 * m + 1 + 1)) :
    delta LibInt.intR ((K m).mul ((K m).add ((K m).add x x) ((K m).neg X)) ((K m).add ((K m).add x x) ((K m).neg X)))
        ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add c c) ((K m).add c c))))
      = if x = (K m).zero then 0 else delta LibInt.intR X ((K m).add x ((K m).mul c (CharSumGen.iv (2 * m) x))) := by
  by_cases hx : x = (K m).zero
  · rw [ite_pos' _ _ hx]
    unfold delta
    refine ite_neg' _ _ (fun e => hc ?_)
    apply four_cancel m h c hh
    have e2 : (K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) c =
        (K m).add ((K m).mul ((K m).add ((K m).add x x) ((K m).neg X)) ((K m).add ((K m).add x x) ((K m).neg X)))
          ((K m).neg ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add c c) ((K m).add c c))))) := by
      rw [hx]; lib_ring
    rw [e2, e]; lib_ring
  · rw [ite_neg' _ _ hx]
    have hw := CharSumGen.iv_mul (2 * m) hpr x hx
    generalize CharSumGen.iv (2 * m) x = w at hw ⊢
    unfold delta
    by_cases hX : X = (K m).add x ((K m).mul c w)
    · rw [ite_pos' _ _ hX, ite_pos' _ _ (by
        rw [hX]
        calc _ = (K m).add ((K m).mul ((K m).add x ((K m).mul c w)) ((K m).add x ((K m).mul c w)))
                ((K m).add ((K m).neg ((K m).add ((K m).add c c) ((K m).add c c)))
                  ((K m).mul ((K m).add ((K m).add c c) ((K m).add c c)) ((K m).add (K m).one ((K m).neg ((K m).mul w x))))) := by lib_ring
          _ = _ := by rw [hw]; lib_ring)]
    · rw [ite_neg' _ _ hX, ite_neg' _ _ (fun e => hX ?_)]
      have e4 : (K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one))
          ((K m).mul x ((K m).add ((K m).add x ((K m).neg X)) ((K m).mul c w))) = (K m).zero := by
        calc _ = (K m).add ((K m).add ((K m).mul ((K m).add ((K m).add x x) ((K m).neg X)) ((K m).add ((K m).add x x) ((K m).neg X)))
                  ((K m).neg ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add c c) ((K m).add c c))))))
                ((K m).mul ((K m).add ((K m).add c c) ((K m).add c c)) ((K m).add ((K m).mul w x) ((K m).neg (K m).one))) := by lib_ring
          _ = (K m).zero := by rw [e, hw]; lib_ring
      have e5 := four_cancel m h _ hh e4
      rcases G4Gen.mul_eq_zero m hpr _ _ e5 with e6 | e6
      · exact absurd e6 hx
      · calc X = (K m).add ((K m).add x ((K m).mul c w)) ((K m).neg ((K m).add ((K m).add x ((K m).neg X)) ((K m).mul c w))) := by lib_ring
          _ = _ := by rw [e6]; lib_ring

/-- **Σ_{x ≠ 0} g(x + c/x) = Σ_X g(X)(1 + χ(X² − 4c)).** -/
theorem fib_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h c : Zp (2 * m + 1 + 1))
    (hh : (K m).add h h = (K m).one) (hc : c ≠ (K m).zero) (g : Zp (2 * m + 1 + 1) → Int) :
    sumK LibInt.intR (fun x => if x = (K m).zero then 0 else g ((K m).add x ((K m).mul c (CharSumGen.iv (2 * m) x)))) (ConicSum.L m)
      = sumK LibInt.intR (fun X => g X * (1 + chi m ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add c c) ((K m).add c c)))))) (ConicSum.L m) := by
  have hcomp : Complete LibInt.intR (ConicSum.L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  -- 1 + χ(D) = Σ_u [u² = D] = Σ_x [(2x − X)² = D]
  have h1 : ∀ X, g X * (1 + chi m ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add c c) ((K m).add c c)))))
      = sumK LibInt.intR (fun x => LibInt.intR.mul (g X) (if x = (K m).zero then 0 else delta LibInt.intR X ((K m).add x ((K m).mul c (CharSumGen.iv (2 * m) x))))) (ConicSum.L m) := fun X => by
    rw [← sq_count m hpr]
    rw [← reindex LibInt.intR _ hcomp (fun x => (K m).add ((K m).add x x) ((K m).neg X)) (fun u => (K m).mul h ((K m).add u X))
      (fun x => by
        calc (K m).mul h ((K m).add ((K m).add ((K m).add x x) ((K m).neg X)) X) = (K m).mul ((K m).add h h) x := by lib_ring
          _ = x := by rw [hh]; lib_ring)
      (fun u => by
        calc (K m).add ((K m).add ((K m).mul h ((K m).add u X)) ((K m).mul h ((K m).add u X))) ((K m).neg X)
            = (K m).add ((K m).mul ((K m).add h h) ((K m).add u X)) ((K m).neg X) := by lib_ring
          _ = u := by rw [hh]; lib_ring)]
    show LibInt.intR.mul (g X) _ = _
    rw [mul_sumK]
    exact sumK_ext LibInt.intR _ _ (fun x => by rw [key m hpr h c hh hc x X]) _
  rw [sumK_ext LibInt.intR _ _ h1, sumK_swap]
  refine sumK_ext LibInt.intR _ _ (fun x => ?_) _
  by_cases hx : x = (K m).zero
  · rw [ite_pos' _ _ hx]
    rw [sumK_ext LibInt.intR _ (fun _ => LibInt.intR.zero) (fun X => by rw [ite_pos' _ _ hx]; show LibInt.intR.mul (g X) LibInt.intR.zero = LibInt.intR.zero; exact DetChain.mul_zero LibInt.intR _)]
    exact (zsum0 _).symm
  · rw [ite_neg' _ _ hx]
    rw [sumK_ext LibInt.intR _ (fun X => LibInt.intR.mul (delta LibInt.intR X ((K m).add x ((K m).mul c (CharSumGen.iv (2 * m) x)))) (g X))
      (fun X => by rw [ite_neg' _ _ hx]; exact LibInt.intR.mul_comm _ _)]
    exact (pick LibInt.intR _ hcomp g _).symm

end FibSum

#print axioms FibSum.four_cancel
#print axioms FibSum.key
#print axioms FibSum.fib_sum

#print axioms: 3 theorems, no axioms.

78.252 The genus-2 curve \(y^2=x^5-x\) splits

In \(\mathbb F_p\) with \(i^2=-1\), \(s^2=-i\), \(\sigma =2si\): the point count of \(y^2=x^5-x\) is the sum of two elliptic ones.

\[ \sum _x\chi (x^5-x)=\sum _X\chi \big ((2X^2-\sigma ^2)(2X-2\sigma )\big )+\sum _X\chi \big ((2X^2-\sigma ^2)(2X+2\sigma )\big ). \]

lang/Genus2.lean

/-
  lang/Genus2 — T48, cycle 723: **the genus-2 curve y² = x⁵ − x splits into two twists of 256a** (OPEN 256a-split, step (ii)).
  In 𝔽_p with i² = −1, s² = −i, 2h = 1, σ = 2si, F_σ(X) = (2X² − σ²)(2X − 2σ):
    d1, c5 … c7  polynomial identities modulo i² + 1, s² + i (generated certificates)
    g2_split    Σ_x χ(x⁵ − x) = Σ_X χ(F_σ(X)) + Σ_X χ(F_{−σ}(X))
  (involution (x, y) ↦ (i/x, sy/x³), X = x + i/x: x⁴F_σ(X) = (2(x − si))²(x⁵ − x); FibSum for the fibres; boundary x = si, X = σ).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.FibSum
import lang.Q256

namespace Genus2

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount

variable (m : Nat)

theorem d1 (x i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    ((K m).mul x ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add ((K m).mul x x) i) ((K m).add ((K m).mul x x) i))) ((K m).neg ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul x x)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).add ((K m).mul x x) i)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m  -- (+363 chars; full line in the Zenodo source)
  have e : ((K m).mul x ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add ((K m).mul x x) i) ((K m).add ((K m).mul x x) i))) ((K m).neg ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul x x)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).add ((K m).mul x x) i)) ((K m).neg ((K m).mul ((K m).add (K m).o  -- (+3329 chars; full line in the Zenodo source)
  rw [e, hi, hs]; lib_ring

theorem c5 (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    ((K m).mul ((K m).mul s i) ((K m).mul ((K m).add s s) i)) = ((K m).add i i) := by
  have e : ((K m).mul ((K m).mul s i) ((K m).mul ((K m).add s s) i)) = ((K m).add ((K m).add ((K m).add i i) ((K m).mul ((K m).add ((K m).mul i i) (K m).one) ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul s s)))) ((K m).mul ((K m).add ((K m).mul s s) i) ((K m).neg ((K m).add (K m).one (K m).one)))) := by lib_ring
  rw [e, hi, hs]; lib_ring

theorem c5b (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    ((K m).mul ((K m).mul s i) ((K m).mul s i)) = i := by
  have e : ((K m).mul ((K m).mul s i) ((K m).mul s i)) = ((K m).add ((K m).add i ((K m).mul ((K m).add ((K m).mul i i) (K m).one) ((K m).mul s s))) ((K m).mul ((K m).add ((K m).mul s s) i) ((K m).neg (K m).one))) := by lib_ring
  rw [e, hi, hs]; lib_ring

theorem c6 (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).mul s i) ((K m).mul s i)) ((K m).mul s i)) ((K m).mul s i)) ((K m).mul s i)) ((K m).neg ((K m).mul s i))) = ((K m).neg ((K m).mul ((K m).add s s) i)) := by
  have e : ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).mul s i) ((K m).mul s i)) ((K m).mul s i)) ((K m).mul s i)) ((K m).mul s i)) ((K m).neg ((K m).mul s i))) = ((K m).add ((K m).add ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).mul ((K m).add ((K m).mul i i) (K m).one) ((K m).add ((K m).add ((K m).neg ((K m).mul ((K m).mul s s) s)) ((K m).neg ((K m).mul ((K m).mul ((K m).mu  -- (+261 chars; full line in the Zenodo source)
  rw [e, hi, hs]; lib_ring

theorem c7 (i s X : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    ((K m).mul ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add i i) ((K m).a  -- (+503 chars; full line in the Zenodo source)
  have e : ((K m).mul ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).add i i) (  -- (+12083 chars; full line in the Zenodo source)
  rw [e, hi, hs]; lib_ring


theorem addI (a b : Int) : LibInt.intR.add a b = a + b := rfl
theorem mulI (a b : Int) : LibInt.intR.mul a b = a * b := rfl
theorem negI (a : Int) : LibInt.intR.neg a = -a := rfl
theorem oneI : LibInt.intR.one = (1 : Int) := rfl
theorem zeroI : LibInt.intR.zero = (0 : Int) := rfl

theorem fin3 (a b c : Int) : LibInt.intR.add (LibInt.intR.add a (LibInt.intR.add b (-c))) c = a + b := by
  rw [addI, addI, addI]; lib_int

theorem sum_delta_L (z : Zp (2 * m + 1 + 1)) : sumK LibInt.intR (fun X => delta LibInt.intR X z) (ConicSum.L m) = 1 :=
  CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR z

theorem chi_sq_mul (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (y b : Zp (2 * m + 1 + 1)) (hy : y ≠ (K m).zero) : chi m ((K m).mul ((K m).mul y y) b) = chi m b := by
  rw [chi_mul m hpr, QChar.chi_sq m hpr y hy]; lib_int

theorem nz (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    i ≠ (K m).zero ∧ s ≠ (K m).zero ∧ ((K m).mul s i) ≠ (K m).zero ∧ ((K m).add (K m).one (K m).one) ≠ (K m).zero := by
  have one_nz := PowerSum.one_ne_zero' (2 * m + 1)
  have hi0 : i ≠ (K m).zero := fun e => one_nz (by rw [← hi, e]; lib_ring)
  have hs0 : s ≠ (K m).zero := fun e => hi0 (by rw [← hs, e]; lib_ring)
  refine ⟨hi0, hs0, fun e => ?_, fun e => one_nz (by rw [← hh, show (K m).add h h = (K m).mul h ((K m).add (K m).one (K m).one) by lib_ring, e]; lib_ring)⟩
  rcases G4Gen.mul_eq_zero m hpr s i e with e1 | e1
  · exact hs0 e1
  · exact hi0 e1

/-- For x ∉ {0, si}: χ(F_σ(x + i/x)) = χ(x⁵ − x). -/
theorem pt (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (K m).zero) (hxs : x ≠ ((K m).mul s i)) : chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add x ((K m).mul i (CharSumGen.iv (2 * m) x))) ((K m).add x ((K m).mul i (CharSumGen.iv (2 * m) x))))) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one  -- (+252 chars; full line in the Zenodo source)
  obtain ⟨_, _, _, h2⟩ := nz m hpr h hh i s hi hs
  have hw := CharSumGen.iv_mul (2 * m) hpr x hx
  generalize CharSumGen.iv (2 * m) x = w at hw ⊢
  have hZ : (K m).mul x ((K m).add x ((K m).mul i w)) = ((K m).add ((K m).mul x x) i) := by
    calc _ = (K m).add ((K m).mul x x) ((K m).mul i ((K m).mul w x)) := by lib_ring
      _ = _ := by rw [hw]; lib_ring
  have e : (K m).mul ((K m).mul ((K m).mul x x) ((K m).mul x x)) (((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add x ((K m).mul i w)) ((K m).add x ((K m).mul i w)))) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).add x ((K m).mul i w))) ((K m).neg ((K m).mul ((K   -- (+62 chars; full line in the Zenodo source)
      = (K m).mul ((K m).mul ((K m).add ((K m).add x ((K m).neg ((K m).mul s i))) ((K m).add x ((K m).neg ((K m).mul s i)))) ((K m).add ((K m).add x ((K m).neg ((K m).mul s i))) ((K m).add x ((K m).neg ((K m).mul s i))))) ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).neg x)) := by
    calc _ = (K m).mul x ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul x ((K m).add x ((K m).mul i w))) ((K m).mul x ((K m).add x ((K m).mul i w))))) ((K m).neg ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul x x))))
          ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul x ((K m).add x ((K m).mul i w)))) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) x))))) := by lib_ring
      _ = _ := by rw [hZ]; exact d1 m x i s hi hs
  have hxx : (K m).mul x x ≠ (K m).zero := fun e0 => by
    rcases G4Gen.mul_eq_zero m hpr x x e0 with e1 | e1 <;> exact hx e1
  have hd : (K m).add ((K m).add x ((K m).neg ((K m).mul s i))) ((K m).add x ((K m).neg ((K m).mul s i))) ≠ (K m).zero := fun e0 => by
    have : (K m).mul ((K m).add (K m).one (K m).one) ((K m).add x ((K m).neg ((K m).mul s i))) = (K m).zero := by rw [← e0]; lib_ring
    rcases G4Gen.mul_eq_zero m hpr _ _ this with e1 | e1
    · exact h2 e1
    · exact hxs (by rw [show x = (K m).add ((K m).add x ((K m).neg ((K m).mul s i))) ((K m).mul s i) by lib_ring, e1]; lib_ring)
  have := congrArg (chi m) e
  rw [chi_sq_mul m hpr _ _ hxx, chi_sq_mul m hpr _ _ hd] at this
  exact this

/-- X(si) = σ, so F_σ vanishes there. -/
theorem at_si (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add ((K m).mul s i) ((K m).mul i (CharSumGen.iv (2 * m) ((K m).mul s i)))) ((K m).add ((K m).mul s i) ((K m).mul i (CharSumGen.iv (2 * m) ((K m).mul s i)))))) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).add ((K   -- (+174 chars; full line in the Zenodo source)
  obtain ⟨_, _, hsi, _⟩ := nz m hpr h hh i s hi hs
  have hw := CharSumGen.iv_mul (2 * m) hpr _ hsi
  generalize CharSumGen.iv (2 * m) ((K m).mul s i) = w at hw ⊢
  have hX : (K m).add ((K m).mul s i) ((K m).mul i w) = ((K m).mul ((K m).add s s) i) := by
    have e0 : (K m).mul ((K m).mul s i) ((K m).add ((K m).add ((K m).mul s i) ((K m).mul i w)) ((K m).neg ((K m).mul ((K m).add s s) i))) = (K m).zero := by
      calc _ = (K m).add ((K m).add ((K m).mul ((K m).mul s i) ((K m).mul s i)) ((K m).mul i ((K m).mul w ((K m).mul s i)))) ((K m).neg ((K m).mul ((K m).mul s i) ((K m).mul ((K m).add s s) i))) := by lib_ring
        _ = _ := by rw [hw, c5b m i s hi hs, c5 m i s hi hs]; lib_ring
    rcases G4Gen.mul_eq_zero m hpr _ _ e0 with e1 | e1
    · exact absurd e1 hsi
    · calc _ = (K m).add ((K m).add ((K m).add ((K m).mul s i) ((K m).mul i w)) ((K m).neg ((K m).mul ((K m).add s s) i))) ((K m).mul ((K m).add s s) i) := by lib_ring
        _ = _ := by rw [e1]; lib_ring
  rw [hX]; lib_ring

/-- **y² = x⁵ − x splits: Σχ(x⁵ − x) = Σχ(F_σ) + Σχ(F_{−σ}).** -/
theorem g2_split (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    sumK LibInt.intR (fun x => chi m ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).neg x))) (ConicSum.L m)
      = sumK LibInt.intR (fun X => chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)))))) (ConicSum.L m) + sumK LibInt.intR (fun X => ch  -- (+373 chars; full line in the Zenodo source)
  obtain ⟨hi0, hs0, hsi, h2⟩ := nz m hpr h hh i s hi hs
  have hcomp : Complete LibInt.intR (ConicSum.L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  have hm1 : chi m ((K m).neg (K m).one) = 1 := by
    rw [show (K m).neg (K m).one = (K m).mul i i by rw [show (K m).mul i i = (K m).add ((K m).add ((K m).mul i i) (K m).one) ((K m).neg (K m).one) by lib_ring, hi]; lib_ring]
    exact QChar.chi_sq m hpr i hi0
  have hsg : ((K m).mul ((K m).add s s) i) ≠ (K m).zero := fun e0 => hsi (by
    have : (K m).mul ((K m).add (K m).one (K m).one) ((K m).mul s i) = (K m).zero := by rw [← e0]; lib_ring
    rcases G4Gen.mul_eq_zero m hpr _ _ this with e1 | e1
    · exact absurd e1 h2
    · exact e1)
  -- step 1: pointwise split
  have p1 : ∀ x, chi m ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).neg x)) = LibInt.intR.add (if x = (K m).zero then 0 else chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add x ((K m).mul i (CharSumGen.iv (2 * m) x))) ((K m).add x ((K m).mul i (CharSumGen.iv (2 * m) x))))) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i)   -- (+230 chars; full line in the Zenodo source)
      (LibInt.intR.mul (delta LibInt.intR x ((K m).mul s i)) (chi m ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).neg x)))) := fun x => by
    by_cases hx : x = (K m).zero
    · rw [ite_pos' _ _ hx, hx, show ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul (K m).zero (K m).zero) (K m).zero) (K m).zero) (K m).zero) ((K m).neg (K m).zero)) = (K m).zero by lib_ring] ; rw [chi_zero, addI, mulI]; lib_int
    · rw [ite_neg' _ _ hx]
      by_cases hxs : x = ((K m).mul s i)
      · rw [hxs, at_si m hpr h hh i s hi hs, chi_zero]
        unfold delta; rw [ite_pos' _ _ rfl, addI, mulI, oneI]; lib_int
      · rw [pt m hpr h hh i s hi hs x hx hxs]
        unfold delta; rw [ite_neg' _ _ hxs, addI, mulI, zeroI]; lib_int
  rw [sumK_ext LibInt.intR _ _ p1, sumK_add, pick LibInt.intR _ hcomp (fun x => chi m ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).neg x))),
    FibSum.fib_sum m hpr h i hh hi0 (fun X => chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))))]
  -- step 2: Σ g(1 + χ(D)) = Σ g + Σ g χ(D)
  have p2 : ∀ X, chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))) * (1 + chi m ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K  -- (+32 chars; full line in the Zenodo source)
      = LibInt.intR.add (chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)))))) (LibInt.intR.add (chi m ((K m).mul ((K m).add ((K m).mul  -- (+316 chars; full line in the Zenodo source)
          (LibInt.intR.neg (LibInt.intR.mul (delta LibInt.intR X ((K m).mul ((K m).add s s) i)) (chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).o  -- (+153 chars; full line in the Zenodo source)
    have c := c7 m i s X hi hs
    by_cases hX : X = ((K m).mul ((K m).add s s) i)
    · rw [hX, show ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).a  -- (+50 chars; full line in the Zenodo source)
      unfold delta; rw [ite_pos' _ _ rfl, addI, addI, negI, mulI, oneI]; lib_int
    · have hXm : (K m).add X ((K m).neg ((K m).mul ((K m).add s s) i)) ≠ (K m).zero := fun e0 => hX (by
        rw [show X = (K m).add ((K m).add X ((K m).neg ((K m).mul ((K m).add s s) i))) ((K m).mul ((K m).add s s) i) by lib_ring, e0]; lib_ring)
      have e1 : chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))) * chi m ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m).ad  -- (+387 chars; full line in the Zenodo source)
        rw [← chi_mul m hpr, c, chi_sq_mul m hpr _ _ hXm]
      unfold delta; rw [ite_neg' _ _ hX, addI, addI, negI, mulI, zeroI]
      rw [show chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))) * (1 + chi m ((K m).add ((K m).mul X X) ((K m).neg ((K m).add ((K m  -- (+30 chars; full line in the Zenodo source)
        = chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i))))) + chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one)  -- (+355 chars; full line in the Zenodo source)
      lib_int
  rw [sumK_ext LibInt.intR _ _ p2, sumK_add, sumK_add]
  -- step 3: the δ-sum and the boundary values
  have p3 : sumK LibInt.intR (fun X => LibInt.intR.neg (LibInt.intR.mul (delta LibInt.intR X ((K m).mul ((K m).add s s) i)) (chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul   -- (+180 chars; full line in the Zenodo source)
      = -(chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one  -- (+54 chars; full line in the Zenodo source)
    rw [sumK_ext LibInt.intR _ (fun X => LibInt.intR.mul (LibInt.intR.neg (chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).  -- (+180 chars; full line in the Zenodo source)
      rw [negI, mulI, mulI, negI]; lib_int), ← mul_sumK, sum_delta_L m, mulI, negI]; lib_int
  have p4 : chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).o  -- (+231 chars; full line in the Zenodo source)
    have hss : (K m).add ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i) ≠ (K m).zero := fun e0 => hsg (by
      have : (K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i) = (K m).zero := by rw [← e0]; lib_ring
      rcases G4Gen.mul_eq_zero m hpr _ _ this with e1 | e1
      · exact absurd e1 h2
      · exact e1)
    rw [show ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) (  -- (+255 chars; full line in the Zenodo source)
      c6 m i s hi hs, show (K m).neg ((K m).mul ((K m).add s s) i) = (K m).mul ((K m).neg (K m).one) ((K m).mul ((K m).add s s) i) by lib_ring, chi_mul m hpr ((K m).neg (K m).one), hm1, chi_sq_mul m hpr _ _ hss]
    lib_int

  rw [p3, p4]
  exact fin3 _ _ _

end Genus2
#print axioms Genus2.d1
#print axioms Genus2.c5
#print axioms Genus2.c5b
#print axioms Genus2.c6
#print axioms Genus2.c7
#print axioms Genus2.addI
#print axioms Genus2.mulI
#print axioms Genus2.negI
#print axioms Genus2.oneI
#print axioms Genus2.zeroI
#print axioms Genus2.fin3
#print axioms Genus2.sum_delta_L
#print axioms Genus2.chi_sq_mul
#print axioms Genus2.nz
#print axioms Genus2.pt
#print axioms Genus2.at_si
#print axioms Genus2.g2_split

#print axioms: 17 theorems, no axioms.

78.253 The genus-2 count is twice the 256a count

The point count of \(y^2=x^5-x\) equals twice that of \(256a\) twisted by \(\sigma ^3h\).

\[ \sum _x\chi (x^5-x)=2\,\chi (\sigma ^3h)\sum _z\chi (z^3+4z^2+2z). \]

lang/Split256.lean

/-
  lang/Split256 — T48, cycle 724: **Σχ(x⁵ − x) = 2χ(σ³h)·Σχ(z³ + 4z² + 2z)** in 𝔽_p with i² = −1, s² = −i, 2h = 1, σ = 2si
  (Genus2.g2_split + Q256.quot_sub for σ and −σ; χ(−1) = χ(i²) = 1): the genus-2 point count is twice the 256a count, twisted.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Genus2

namespace Split256

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount

variable (m : Nat)

theorem split256 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    sumK LibInt.intR (fun x => chi m ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).neg x))) (ConicSum.L m)
      = 2 * (chi m ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul ((K m).add s s) i)) h) * sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (K m).one (K m).one)))) (ConicSum.L m)) := by
  obtain ⟨hi0, hs0, hsi, h2⟩ := Genus2.nz m hpr h hh i s hi hs
  have hsg : ((K m).mul ((K m).add s s) i) ≠ (K m).zero := fun e0 => hsi (by
    have : (K m).mul ((K m).add (K m).one (K m).one) ((K m).mul s i) = (K m).zero := by rw [← e0]; lib_ring
    rcases G4Gen.mul_eq_zero m hpr _ _ this with e1 | e1
    · exact absurd e1 h2
    · exact e1)
  have hnsg : (K m).neg ((K m).mul ((K m).add s s) i) ≠ (K m).zero := fun e0 => hsg (by rw [show ((K m).mul ((K m).add s s) i) = (K m).neg ((K m).neg ((K m).mul ((K m).add s s) i)) by lib_ring, e0]; lib_ring)
  have hm1 : chi m ((K m).neg (K m).one) = 1 := by
    rw [show (K m).neg (K m).one = (K m).mul i i by rw [show (K m).mul i i = (K m).add ((K m).add ((K m).mul i i) (K m).one) ((K m).neg (K m).one) by lib_ring, hi]; lib_ring]
    exact QChar.chi_sq m hpr i hi0
  have hneg : chi m ((K m).mul ((K m).mul ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).add s s) i))) h) = chi m ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul ((K m).add s s) i)) h) := by
    rw [show ((K m).mul ((K m).mul ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))) ((K m).neg ((K m).mul ((K m).add s s) i))) h) = (K m).mul ((K m).neg (K m).one) ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul ((K m).add s s) i)) h) by lib_ring, chi_mul m hpr, hm1]; lib_int
  rw [Genus2.g2_split m hpr h hh i s hi hs]
  have q1 : sumK LibInt.intR (fun X => chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul ((K m).add s s) i)))))) (ConicSum.L m) = chi m ((K m).mul ((K m).m  -- (+338 chars; full line in the Zenodo source)
    Q256.quot_sub m hpr _ h hsg hh
  have q2 : sumK LibInt.intR (fun X => chi m ((K m).mul ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) ((K m).mul X X)) ((K m).neg ((K m).mul ((K m).neg ((K m).mul ((K m).add s s) i)) ((K m).neg ((K m).mul ((K m).add s s) i))))) ((K m).add ((K m).mul ((K m).add (K m).one (K m).one) X) ((K m).neg ((K m).mul ((K m).add (K m).one (K m).one) ((K m).neg ((K m).mul ((K m).add s s) i))))))) (Conic  -- (+410 chars; full line in the Zenodo source)
    Q256.quot_sub m hpr _ h hnsg hh
  rw [q1, q2, hneg]
  lib_int

end Split256

#print axioms Split256.split256

#print axioms: 1 theorems, no axioms.

78.254 The family \(\sum \chi (x^5+cx)\) on fourth-power classes

The family \(T(c)\) is \(\chi \)-equivariant under \(c\mapsto cu^4\).

\[ T(cu^4)=\chi (u)\,T(c),\qquad T(c)=\sum _x\chi (x^5+cx). \]

lang/Jac1.lean

/-
  lang/Jac1 — T48, cycle 726: **the Jacobsthal family T(c) = Σ_x χ(x⁵ + cx) is χ-equivariant on 4th-power classes** (OPEN 256a-split, J1):
    tw_pt    (ux)⁵ + (c u⁴)(ux) = u⁵ (x⁵ + cx)
    tw_cls   u ≠ 0 ⇒ T(c u⁴) = χ(u) T(c)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Genus2

namespace Jac1

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount

variable (m : Nat)

def T (c : Zp (2 * m + 1 + 1)) : Int :=
  sumK LibInt.intR (fun x => chi m ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).mul c x))) (ConicSum.L m)

theorem tw_pt (u c x : Zp (2 * m + 1 + 1)) :
    (K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).mul u x) ((K m).mul u x)) ((K m).mul u x)) ((K m).mul u x)) ((K m).mul u x))
        ((K m).mul ((K m).mul c ((K m).mul ((K m).mul u u) ((K m).mul u u))) ((K m).mul u x))
      = (K m).mul ((K m).mul ((K m).mul u u) ((K m).mul u u)) ((K m).mul u ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).mul c x))) := by
  lib_ring

/-- **T(c u⁴) = χ(u) T(c).** -/
theorem tw_cls (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (u c : Zp (2 * m + 1 + 1)) (hu : u ≠ (K m).zero) :
    T m ((K m).mul c ((K m).mul ((K m).mul u u) ((K m).mul u u))) = chi m u * T m c := by
  have huu : (K m).mul u u ≠ (K m).zero := fun e => by
    rcases G4Gen.mul_eq_zero m hpr u u e with e1 | e1 <;> exact hu e1
  unfold T
  have hsc : ∀ G : Zp (2 * m + 1 + 1) → Int, sumK LibInt.intR (fun x => G ((K m).mul u x)) (ConicSum.L m) = sumK LibInt.intR G (ConicSum.L m) :=
    fun G => CharSumGen.sum_scale LibInt.intR (2 * m) hpr G u hu
  rw [← hsc]
  show _ = LibInt.intR.mul _ _
  rw [mul_sumK]
  exact sumK_ext LibInt.intR _ _ (fun x => by
    show chi m (_) = _
    rw [tw_pt m u c x, Genus2.chi_sq_mul m hpr _ _ huu, chi_mul m hpr]; rfl) _

end Jac1

#print axioms Jac1.T
#print axioms Jac1.tw_pt
#print axioms Jac1.tw_cls

#print axioms: 3 theorems, no axioms.

78.255 The weighted translation moment

Second moment of a weighted translation family of character sums.

\[ \sum _\lambda \Big (\sum _x w(x)\chi (\lambda +h(x))\Big )^2=-\Big (\sum _x w(x)\Big )^2+p\sum _{h(x)=h(y)}w(x)w(y). \]

lang/Jac2.lean

/-
  lang/Jac2 — T48, cycle 727: **the weighted translation moment** (OPEN 256a-split, J2, first half):
    wmoment   Σ_l (Σ_x w(x)χ(l + h(x)))² = −(Σ_x w(x))² + p·Σ_x Σ_y w(x)w(y)[h(x) = h(y)]
  (every weight w : 𝔽_p → ℤ and every h; translation_moment is w = 1). With w = χ, h = x⁴ this is Σ_c T(c)² for T(c) = Σχ(x⁵ + cx).
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.LegendreMoment
import lang.Jac1

namespace Jac2

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum

variable (m : Nat)

theorem wmoment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (w : Zp (2 * m + 1 + 1) → Int) (h : Zp (2 * m + 1 + 1) → Zp (2 * m + 1 + 1)) :
    sumK LibInt.intR (fun l => sumK LibInt.intR (fun x => w x * chi m ((ring (2 * m + 1 + 1)).add l (h x))) (L m) * sumK LibInt.intR (fun x => w x * chi m ((ring (2 * m + 1 + 1)).add l (h x))) (L m)) (L m)
      = -(sumK LibInt.intR w (L m) * sumK LibInt.intR w (L m))
        + JacobMoment.P m * sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => w x * w y * delta LibInt.intR (h x) (h y)) (L m)) (L m) := by
  rw [sumK_ext LibInt.intR _ (fun l => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => w x * w y * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y)))) (L m)) (L m)) (fun l => by
    show LibInt.intR.mul _ _ = _
    rw [sumK_mul]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      show LibInt.intR.mul _ _ = _
      rw [mul_sumK]
      exact sumK_ext LibInt.intR _ _ (fun y => by
        show (w x * chi m _) * (w y * chi m _) = _
        rw [chi_mul m hpr]; lib_int) _) _)]
  rw [← sumK_swap LibInt.intR (fun x l => sumK LibInt.intR (fun y => w x * w y * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y)))) (L m))]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => LibInt.intR.add (LibInt.intR.mul (-(w x)) (w y)) (LibInt.intR.mul (JacobMoment.P m) (w x * w y * delta LibInt.intR (h x) (h y)))) (L m)) (fun x => by
    rw [← sumK_swap LibInt.intR (fun y l => w x * w y * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y))))]
    exact sumK_ext LibInt.intR _ _ (fun y => by
      have e := JacobMoment.inner m hpr (h x) (h y)
      rw [show sumK LibInt.intR (fun l => w x * w y * chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y)))) (L m)
          = LibInt.intR.mul (w x * w y) (sumK LibInt.intR (fun l => chi m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add l (h x)) ((ring (2 * m + 1 + 1)).add l (h y)))) (L m)) by
            rw [mul_sumK]; rfl, e]
      show (w x * w y) * (-1 + JacobMoment.P m * _) = -(w x) * w y + JacobMoment.P m * (w x * w y * _)
      rw [show delta JacobMoment.Z (h x) (h y) = delta LibInt.intR (h x) (h y) from rfl]; lib_int) _)]
  rw [sumK_ext LibInt.intR _ _ (fun x => sumK_add LibInt.intR _ _ _), sumK_add]
  rw [sumK_ext LibInt.intR (fun x => sumK LibInt.intR (fun y => LibInt.intR.mul (-(w x)) (w y)) (L m)) _ (fun x => (mul_sumK LibInt.intR _ _ _).symm)]
  rw [sumK_ext LibInt.intR (fun x => sumK LibInt.intR (fun y => LibInt.intR.mul (JacobMoment.P m) (w x * w y * delta LibInt.intR (h x) (h y))) (L m)) _ (fun x => (mul_sumK LibInt.intR _ _ _).symm)]
  rw [← mul_sumK, ← sumK_mul]
  show LibInt.intR.mul (sumK LibInt.intR (fun x => -(w x)) (L m)) _ + _ = _
  rw [show sumK LibInt.intR (fun x => -(w x)) (L m) = LibInt.intR.mul (-1) (sumK LibInt.intR w (L m)) by
    rw [mul_sumK]; exact sumK_ext LibInt.intR _ _ (fun x => by show _ = -1 * w x; lib_int) _]
  show (-1 * _) * _ + _ * _ = _
  lib_int

end Jac2

#print axioms Jac2.wmoment

#print axioms: 1 theorems, no axioms.

78.256 The second moment of \(\sum \chi (x^5+cx)\)

When \(\sqrt {-1}\) and \(\sqrt {-\sqrt {-1}}\) exist in \(\mathbb F_p\), the family \(T(c)\) has second moment \(4p(p-1)\).

\[ \sum _c T(c)^2=4p(p-1),\qquad T(c)=\sum _x\chi (x^5+cx). \]

lang/Jac3.lean

/-
  lang/Jac3 — T48, cycle 728: **Σ_c T(c)² = 4p(p − 1)** for T(c) = Σχ(x⁵ + cx), when i² = −1 and s² = −i exist in 𝔽_p (J2).
    nz4         x ≠ 0: x, −x, ix, −ix are pairwise distinct (2 ≠ 0, i ≠ ±1)
    four_delta  x ≠ 0: [y⁴ = x⁴] = [y = x] + [y = −x] + [y = ix] + [y = −ix]
    row         x ≠ 0: Σ_y χ(x)χ(y)[x⁴ = y⁴] = 4   (χ(−1) = χ(i²), χ(±i) = χ((si)²), χ(s²))
    jac_moment  Σ_c T(c)² = 4p(p − 1)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Jac2

namespace Jac3

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

theorem fact4 (x y i : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) :
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg x)) ((ring (2 * m + 1 + 1)).add y x  -- (+194 chars; full line in the Zenodo source)
  have e : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg   -- (+224 chars; full line in the Zenodo source)
      ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x x))))) := by lib_ring
  rw [e, show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero from hi]; lib_ring

theorem ne_of_mul (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b c x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hc : c ≠ (ring (2 * m + 1 + 1)).zero)
    (h : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg b) = (ring (2 * m + 1 + 1)).mul c x) : a ≠ b := fun e => by
  rw [e, show (ring (2 * m + 1 + 1)).add b ((ring (2 * m + 1 + 1)).neg b) = (ring (2 * m + 1 + 1)).zero by lib_ring] at h
  rcases G4Gen.mul_eq_zero m hpr c x h.symm with e1 | e1
  · exact hc e1
  · exact hx e1

theorem dl (a b : Zp (2 * m + 1 + 1)) (h : a = b) : delta LibInt.intR a b = 1 := by unfold delta; rw [ite_pos' _ _ h]; rfl
theorem dz (a b : Zp (2 * m + 1 + 1)) (h : a ≠ b) : delta LibInt.intR a b = 0 := by unfold delta; rw [ite_neg' _ _ h]; rfl

theorem four_delta (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (x y : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) = delta LibInt.intR y x + delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg x) + delta LibInt.intR y ((ring (2 * m + 1 + 1)).mul i x) + delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg ((ring  -- (+33 chars; full line in the Zenodo source)
  obtain ⟨hi0, _, _, h2⟩ := Genus2.nz m hpr h hh i s hi hs
  have one_nz := PowerSum.one_ne_zero' (2 * m + 1)
  have h1i : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i) ≠ (ring (2 * m + 1 + 1)).zero := fun e => h2 (by
    have ei : i = (ring (2 * m + 1 + 1)).one := by rw [show i = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i))) (ring (2 * m + 1 + 1)).one by lib_ring, e]; lib_ring
    rw [show (K m).add (K m).one (K m).one = (K m).add ((K m).add ((K m).mul i i) (K m).one) ((K m).add (K m).one ((K m).neg ((K m).mul i i))) by lib_ring, hi, ei]; lib_ring)
  have h1i' : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i ≠ (ring (2 * m + 1 + 1)).zero := fun e => h2 (by
    have ei : i = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one := by rw [show i = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) by lib_ring, e]; lib_ring
    rw [show (K m).add (K m).one (K m).one = (K m).add ((K m).add ((K m).mul i i) (K m).one) ((K m).add (K m).one ((K m).neg ((K m).mul i i))) by lib_ring, hi, ei]; lib_ring)
  have h2i : (ring (2 * m + 1 + 1)).add i i ≠ (ring (2 * m + 1 + 1)).zero := fun e => hi0 (by
    rw [show i = (ring (2 * m + 1 + 1)).mul h ((ring (2 * m + 1 + 1)).add i i) by rw [show (ring (2 * m + 1 + 1)).mul h ((ring (2 * m + 1 + 1)).add i i) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add h h) i by lib_ring, hh]; lib_ring, e]; lib_ring)
  -- the four candidate roots are pairwise distinct
  have d12 : x ≠ (ring (2 * m + 1 + 1)).neg x := ne_of_mul m hpr _ _ _ x hx h2 (by lib_ring)
  have d13 : x ≠ (ring (2 * m + 1 + 1)).mul i x := ne_of_mul m hpr _ _ _ x hx h1i (by lib_ring)
  have d14 : x ≠ (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x) := ne_of_mul m hpr _ _ _ x hx h1i' (by lib_ring)
  have d23 : (ring (2 * m + 1 + 1)).neg x ≠ (ring (2 * m + 1 + 1)).mul i x := ne_of_mul m hpr _ _ ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i)) x hx (fun e => h1i' (by rw [show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i)) by lib_rin  -- (+31 chars; full line in the Zenodo source)
  have d24 : (ring (2 * m + 1 + 1)).neg x ≠ (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x) := ne_of_mul m hpr _ _ ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i))) x hx (fun e => h1i (by rw [show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg i) = (ring (2 * m + 1 + 1)).neg ((ring (2 *  -- (+146 chars; full line in the Zenodo source)
  have d34 : (ring (2 * m + 1 + 1)).mul i x ≠ (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x) := ne_of_mul m hpr _ _ _ x hx h2i (by lib_ring)
  have f4 : ∀ z, ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul z z) ((ring (2 * m + 1 + 1)).mul z z)) = ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) → z = x ∨ z = (ring (2 * m + 1 + 1)).neg x ∨ z = (ring (2 * m + 1 + 1)).mul i x ∨ z = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x) := fun z hz => by
    have e0 := fact4 m x z i hi
    rw [hz, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))) = (ring (2 * m + 1 + 1)).zero by lib_ring] at e0
    rcases G4Gen.mul_eq_zero m hpr _ _ e0.symm with e1 | e1
    · rcases G4Gen.mul_eq_zero m hpr _ _ e1 with e2 | e2
      · exact Or.inl (by rw [show z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg x)) x by lib_ring, e2]; lib_ring)
      · exact Or.inr (Or.inl (by rw [show z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z x) ((ring (2 * m + 1 + 1)).neg x) by lib_ring, e2]; lib_ring))
    · rcases G4Gen.mul_eq_zero m hpr _ _ e1 with e2 | e2
      · exact Or.inr (Or.inr (Or.inl (by rw [show z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x))) ((ring (2 * m + 1 + 1)).mul i x) by lib_ring, e2]; lib_ring)))
      · exact Or.inr (Or.inr (Or.inr (by rw [show z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).mul i x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x)) by lib_ring, e2]; lib_ring)))
  have q4ix : ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x))) = ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) := by
    rw [show ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul i x) ((ring (2 * m + 1 + 1)).mul i x))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +   -- (+328 chars; full line in the Zenodo source)
      show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero from hi]; lib_ring
  by_cases hy : ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) = ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))
  · rw [dl m _ _ hy.symm]
    rcases f4 y hy with e | e | e | e <;> rw [e]
    · rw [dl m _ _ rfl, dz m _ _ d12, dz m _ _ d13, dz m _ _ d14]; rfl
    · rw [dz m _ _ (fun e' => d12 e'.symm), dl m _ _ rfl, dz m _ _ d23, dz m _ _ d24]; rfl
    · rw [dz m _ _ (fun e' => d13 e'.symm), dz m _ _ (fun e' => d23 e'.symm), dl m _ _ rfl, dz m _ _ d34]; rfl
    · rw [dz m _ _ (fun e' => d14 e'.symm), dz m _ _ (fun e' => d24 e'.symm), dz m _ _ (fun e' => d34 e'.symm), dl m _ _ rfl]; rfl
  · rw [dz m _ _ (fun e => hy e.symm)]
    have n1 : y ≠ x := fun e => hy (by rw [e])
    have n2 : y ≠ (ring (2 * m + 1 + 1)).neg x := fun e => hy (by rw [e]; lib_ring)
    have n3 : y ≠ (ring (2 * m + 1 + 1)).mul i x := fun e => hy (by rw [e]; exact q4ix)
    have n4 : y ≠ (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x) := fun e => hy (by rw [e, show ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x)) ((ring (2 * m + 1 +   -- (+290 chars; full line in the Zenodo source)
    rw [dz m _ _ n1, dz m _ _ n2, dz m _ _ n3, dz m _ _ n4]; rfl


theorem row (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    sumK LibInt.intR (fun y => chi m x * chi m y * delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y))) (L m) = 4 := by
  obtain ⟨hi0, hs0, hsi, h2⟩ := Genus2.nz m hpr h hh i s hi hs
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  have pk : ∀ z, sumK LibInt.intR (fun y => LibInt.intR.mul (delta LibInt.intR y z) (chi m x * chi m y)) (L m) = chi m x * chi m z :=
    fun z => pick LibInt.intR _ hcomp (fun y => chi m x * chi m y) z
  rw [sumK_ext LibInt.intR _ (fun y => LibInt.intR.add (LibInt.intR.add (LibInt.intR.add
      (LibInt.intR.mul (delta LibInt.intR y x) (chi m x * chi m y)) (LibInt.intR.mul (delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg x)) (chi m x * chi m y)))
      (LibInt.intR.mul (delta LibInt.intR y ((ring (2 * m + 1 + 1)).mul i x)) (chi m x * chi m y))) (LibInt.intR.mul (delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x))) (chi m x * chi m y))) (fun y => by
    rw [four_delta m hpr h hh i s hi hs x y hx, Genus2.addI, Genus2.addI, Genus2.addI, Genus2.mulI, Genus2.mulI, Genus2.mulI, Genus2.mulI]; lib_int)]
  rw [sumK_add, sumK_add, sumK_add, pk, pk, pk, pk]
  have hxx : chi m x * chi m x = 1 := by rw [← chi_mul m hpr]; exact QChar.chi_sq m hpr x hx
  have hm1 : chi m ((K m).neg (K m).one) = 1 := by
    rw [show (K m).neg (K m).one = (K m).mul i i by rw [show (K m).mul i i = (K m).add ((K m).add ((K m).mul i i) (K m).one) ((K m).neg (K m).one) by lib_ring, hi]; lib_ring]
    exact QChar.chi_sq m hpr i hi0
  have hci : chi m i = 1 := by rw [← Genus2.c5b m i s hi hs]; exact QChar.chi_sq m hpr _ hsi
  have hcni : chi m ((K m).neg i) = 1 := by
    rw [show (K m).neg i = (K m).mul s s by rw [show (K m).mul s s = (K m).add ((K m).add ((K m).mul s s) i) ((K m).neg i) by lib_ring, hs]; lib_ring]
    exact QChar.chi_sq m hpr s hs0
  have v2 : chi m x * chi m ((ring (2 * m + 1 + 1)).neg x) = 1 := by
    rw [show (ring (2 * m + 1 + 1)).neg x = (ring (2 * m + 1 + 1)).mul ((K m).neg (K m).one) x by lib_ring, chi_mul m hpr, hm1]
    show chi m x * (1 * chi m x) = 1; rw [show chi m x * (1 * chi m x) = chi m x * chi m x by lib_int, hxx]
  have v3 : chi m x * chi m ((ring (2 * m + 1 + 1)).mul i x) = 1 := by
    rw [chi_mul m hpr, hci, show chi m x * (1 * chi m x) = chi m x * chi m x by lib_int, hxx]
  have v4 : chi m x * chi m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x)) = 1 := by
    rw [show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i x) = (ring (2 * m + 1 + 1)).mul ((K m).neg i) x by lib_ring, chi_mul m hpr, hcni,
      show chi m x * (1 * chi m x) = chi m x * chi m x by lib_int, hxx]
  rw [Genus2.addI, Genus2.addI, Genus2.addI, hxx, v2, v3, v4]; rfl

/-- **J2: Σ_c T(c)² = 4p(p − 1).** -/
theorem jac_moment (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    sumK LibInt.intR (fun c => Jac1.T m c * Jac1.T m c) (L m) = 4 * JacobMoment.P m * (JacobMoment.P m + -1) := by
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  have hT : ∀ c, Jac1.T m c = sumK LibInt.intR (fun x => chi m x * chi m ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)))) (L m) := fun c =>
    sumK_ext LibInt.intR _ _ (fun x => by
      rw [show ((K m).add ((K m).mul ((K m).mul ((K m).mul ((K m).mul x x) x) x) x) ((K m).mul c x)) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add c ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))) by lib_ring, chi_mul m hpr]) _
  rw [sumK_ext LibInt.intR _ _ (fun c => by rw [hT c])]
  rw [Jac2.wmoment m hpr (chi m) (fun x => ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)))]
  have hs0 : sumK LibInt.intR (chi m) (L m) = 0 := QChar.chi_sum m hpr
  rw [hs0]
  have hrow : ∀ x, sumK LibInt.intR (fun y => chi m x * chi m y * delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y))) (L m) = 4 + -4 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero := fun x => by
    by_cases hx : x = (ring (2 * m + 1 + 1)).zero
    · rw [hx, dl m _ _ rfl, chi_zero]
      rw [sumK_ext LibInt.intR _ (fun _ => LibInt.intR.zero) (fun y => by show 0 * chi m y * _ = 0; lib_int)]
      rw [zsum0]; rfl
    · rw [row m hpr h hh i s hi hs x hx, dz m _ _ hx]; rfl
  rw [sumK_ext LibInt.intR _ _ hrow, PencilMoment.sum_lin m 4 (-4) (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero), hcomp]
  show -(0 * 0) + JacobMoment.P m * (4 * JacobMoment.P m + -4 * 1) = _
  lib_int

end Jac3
#print axioms Jac3.fact4
#print axioms Jac3.ne_of_mul
#print axioms Jac3.dl
#print axioms Jac3.dz
#print axioms Jac3.four_delta
#print axioms Jac3.row
#print axioms Jac3.jac_moment

#print axioms: 7 theorems, no axioms.

78.257 Inversion in the family \(\sum \chi (x^5+cx)\)

The substitution \(x\mapsto \lambda /x\) relates \(T(c)\) and \(T(\lambda ^4/c)\).

\[ T(c)=\chi (\lambda c)\,T(\lambda ^4/c);\quad T(d^2)=0\ \text {if}\ \chi (d)=-1;\quad T(c)=\chi (c)\,T(1/c). \]

lang/Jac4.lean

/-
  lang/Jac4 — T48, cycle 729: **the inversion x ↦ λ/x in the Jacobsthal family** (OPEN 256a-split, J3 and J4):
    inv_pt    (λw)⁵ + c(λw) = λc·w⁶·(x⁵ + c′x),  wx = 1, c′ = λ⁴/c
    inv_sub   c, λ ≠ 0:  T(c) = χ(λc)·T(λ⁴/c)
    zero_cls  c = d², χ(d) = −1  ⇒  T(c) = 0                          (J3: squares that are not 4th powers)
    inv_cls   T(c) = χ(c)·T(1/c), so T(c)² = T(1/c)²                  (J4)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Jac3

namespace Jac4

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

theorem inv_pt (l c ic w x : Zp (2 * m + 1 + 1)) (hw : ((ring (2 * m + 1 + 1)).mul w x) = (ring (2 * m + 1 + 1)).one) (hc : ((ring (2 * m + 1 + 1)).mul c ic) = (ring (2 * m + 1 + 1)).one) :
    ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l w) ((ring (2 * m + 1 + 1)).mul l w)) ((ring (2 * m + 1 + 1)).mul l w)) ((ring (2 * m + 1 + 1)).mul l w)) ((ring (2 * m + 1 + 1)).mul l w)) ((ring (2 * m + 1 + 1)).mul c ((ring (2 * m + 1 + 1)).mul l w))) = (ring (2 * m + 1 + 1  -- (+544 chars; full line in the Zenodo source)
  have e : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l c) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul w w)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x  -- (+169 chars; full line in the Zenodo source)
      = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l c) w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w x) ((ring (2 * m + 1 + 1)).mul w x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w x) ((ring (2 * m + 1 + 1)).mul w x))) ((ring (2 * m + 1 + 1)).mu  -- (+8 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l l) ((ring (2 * m + 1 + 1)).mul l l)) ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) w)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w x) ((ring (2 * m + 1 +   -- (+30 chars; full line in the Zenodo source)
  rw [e, hw, hc]; lib_ring

theorem one_mid (A X : Int) : A * 1 * X = A * X := by lib_int

theorem inv_sub (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c l : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) (hl : l ≠ (ring (2 * m + 1 + 1)).zero) :
    Jac1.T m c = chi m ((ring (2 * m + 1 + 1)).mul l c) * Jac1.T m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l l) ((ring (2 * m + 1 + 1)).mul l l)) (CharSumGen.iv (2 * m) c)) := by
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  have hic := CharSumGen.iv_mul (2 * m) hpr c hc
  have hcc : (ring (2 * m + 1 + 1)).mul c (CharSumGen.iv (2 * m) c) = (ring (2 * m + 1 + 1)).one := by rw [(ring (2 * m + 1 + 1)).mul_comm]; exact hic
  have hil := CharSumGen.iv_mul (2 * m) hpr l hl
  have sig : ∀ x, (ring (2 * m + 1 + 1)).mul l (CharSumGen.iv (2 * m) ((ring (2 * m + 1 + 1)).mul l (CharSumGen.iv (2 * m) x))) = x := fun x => by
    rw [CharSumGen.iv_mulmul, CharSumGen.iv_iv (2 * m) hpr x,
      show (ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) l) x) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) l) l) x by lib_ring, hil]; lib_ring
  unfold Jac1.T
  rw [← reindex LibInt.intR _ hcomp (fun x => (ring (2 * m + 1 + 1)).mul l (CharSumGen.iv (2 * m) x)) (fun x => (ring (2 * m + 1 + 1)).mul l (CharSumGen.iv (2 * m) x)) sig sig]
  show _ = LibInt.intR.mul _ _
  rw [mul_sumK]
  refine sumK_ext LibInt.intR _ _ (fun x => ?_) _
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, CharSumGen.iv_zero, show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul l (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul l (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul l (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul l (ring (2 * m +  -- (+219 chars; full line in the Zenodo source)
      show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1  -- (+177 chars; full line in the Zenodo source)
  · have hw := CharSumGen.iv_mul (2 * m) hpr x hx
    have hw0 : (CharSumGen.iv (2 * m) x) ≠ (ring (2 * m + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * m + 1) (by rw [← hw, e]; lib_ring)
    generalize (CharSumGen.iv (2 * m) x) = w at hw hw0 ⊢
    generalize (CharSumGen.iv (2 * m) c) = ic at hcc ⊢
    rw [inv_pt m l c ic w x hw hcc, chi_mul m hpr,
      show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul w w)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) w) by lib_ring]
    have hw3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) w ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
      rcases G4Gen.mul_eq_zero m hpr _ _ e with e1 | e1
      · rcases G4Gen.mul_eq_zero m hpr _ _ e1 with e2 | e2 <;> exact hw0 e2
      · exact hw0 e1
    rw [chi_mul m hpr ((ring (2 * m + 1 + 1)).mul l c), QChar.chi_sq m hpr _ hw3]
    rw [Genus2.mulI]; exact one_mid _ _

/-- **J3: c = d², χ(d) = −1 ⇒ T(c) = 0.** -/
theorem zero_cls (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (d : Zp (2 * m + 1 + 1)) (hd : chi m d = -1) :
    Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) = 0 := by
  have hd0 : d ≠ (ring (2 * m + 1 + 1)).zero := fun e => by rw [e, chi_zero] at hd; exact Int.noConfusion hd
  have hdd : (ring (2 * m + 1 + 1)).mul d d ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
    rcases G4Gen.mul_eq_zero m hpr _ _ e with e1 | e1 <;> exact hd0 e1
  have e := inv_sub m hpr ((ring (2 * m + 1 + 1)).mul d d) d hdd hd0
  have hiv := CharSumGen.iv_mul (2 * m) hpr _ hdd
  have hc' : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul d d) ((ring (2 * m + 1 + 1)).mul d d)) (CharSumGen.iv (2 * m) ((ring (2 * m + 1 + 1)).mul d d)) = (ring (2 * m + 1 + 1)).mul d d := by
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul d d) ((ring (2 * m + 1 + 1)).mul d d)) (CharSumGen.iv (2 * m) ((ring (2 * m + 1 + 1)).mul d d)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) ((ring (2 * m + 1 + 1)).mul d d)) ((ring (2 * m + 1 + 1)).mul d d)) ((ring (2 * m + 1 + 1)).mul d d) by lib_ring, hiv]; lib_ring
  rw [hc', show (ring (2 * m + 1 + 1)).mul d ((ring (2 * m + 1 + 1)).mul d d) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul d d) d by lib_ring, Genus2.chi_sq_mul m hpr d d hd0, hd] at e
  -- T = −T
  have h2 : Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) + Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) = 0 := by
    rw [show Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) + Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) = Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) + -1 * -1 * Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) by lib_int]
    rw [show -1 * -1 * Jac1.T m ((ring (2 * m + 1 + 1)).mul d d) = -1 * (-1 * Jac1.T m ((ring (2 * m + 1 + 1)).mul d d)) by lib_int, ← e]; lib_int
  exact QChar.int_two_zero _ h2

/-- **J4: T(c) = χ(c)·T(1/c).** -/
theorem inv_cls (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) :
    Jac1.T m c = chi m c * Jac1.T m (CharSumGen.iv (2 * m) c) := by
  have e := inv_sub m hpr c (ring (2 * m + 1 + 1)).one hc (PowerSum.one_ne_zero' (2 * m + 1))
  rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) (CharSumGen.iv (2 * m) c) = (CharSumGen.iv (2 * m) c) by lib_ring,
    show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one c = c by lib_ring] at e
  exact e

end Jac4

#print axioms Jac4.inv_pt
#print axioms Jac4.one_mid
#print axioms Jac4.inv_sub
#print axioms Jac4.zero_cls
#print axioms Jac4.inv_cls

#print axioms: 5 theorems, no axioms.

78.258 \(T(c)^2\) is constant on fourth powers and on non-squares

\(T(c)^2\) depends only on whether \(c\) is a fourth power, a square, or a non-square.

\[ T(e^4)^2=T(1)^2,\qquad \chi (c)=\chi (g)=-1\Rightarrow T(c)^2=T(g)^2. \]

lang/Jac5.lean

/-
  lang/Jac5 — T48, cycle 730: **T(c)² on the classes of the Jacobsthal family** (OPEN 256a-split), g a non-square:
    sqrt_chi     χ(a) = 1 ⇒ a = e²
    cls4, clsN   T(e⁴)² = T(1)²;  χ(c) = −1 ⇒ T(c)² = T(g)²            (J1, J4)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Jac4

namespace Jac5

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

theorem sqrt_chi (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ha : chi m a = 1) : ∃ e, (ring (2 * m + 1 + 1)).mul e e = a := by
  have ha0 : a ≠ (ring (2 * m + 1 + 1)).zero := fun e => by rw [e, chi_zero] at ha; exact Nat.noConfusion (Int.ofNat.inj ha)
  by_cases hp : Frobenius.pw (ring (2 * m + 1 + 1)) a (m + 1) = (ring (2 * m + 1 + 1)).one
  · exact QuadDiagonal.sq_root m hpr a ha0 hp
  · rw [chi_neg_of m a ha0 hp] at ha; exact Int.noConfusion ha

theorem nz_mul (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).mul a b ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  rcases G4Gen.mul_eq_zero m hpr _ _ e with e1 | e1
  · exact ha e1
  · exact hb e1

theorem sq_T (u t : Int) (hu : u * u = 1) : (u * t) * (u * t) = t * t := by
  rw [show (u * t) * (u * t) = (u * u) * (t * t) by lib_int, hu]; lib_int

theorem chi_unit (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (e : Zp (2 * m + 1 + 1)) (he : e ≠ (ring (2 * m + 1 + 1)).zero) : chi m e * chi m e = 1 := by
  rw [← chi_mul m hpr]; exact QChar.chi_sq m hpr e he

/-- T(e⁴)² = T(1)². -/
theorem cls4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (e : Zp (2 * m + 1 + 1)) (he : e ≠ (ring (2 * m + 1 + 1)).zero) : Jac1.T m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e e) ((ring (2 * m + 1 + 1)).mul e e)) * Jac1.T m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e e) ((ring (2 * m + 1 + 1)).mul e e)) = Jac1.T m (ring (2 * m + 1 + 1)).one * J  -- (+40 chars; full line in the Zenodo source)
  have h1 := Jac1.tw_cls m hpr e (ring (2 * m + 1 + 1)).one he
  rw [show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e e) ((ring (2 * m + 1 + 1)).mul e e)) = ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e e) ((ring (2 * m + 1 + 1)).mul e e)) by lib_ring] at h1
  rw [h1]; exact sq_T _ _ (chi_unit m hpr e he)

/-- χ(c) = −1 = χ(g) ⇒ T(c)² = T(g)². -/
theorem clsN (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (c g : Zp (2 * m + 1 + 1)) (hc : chi m c = -1) (hg : chi m g = -1) :
    Jac1.T m c * Jac1.T m c = Jac1.T m g * Jac1.T m g := by
  have hc0 : c ≠ (ring (2 * m + 1 + 1)).zero := fun e => by rw [e, chi_zero] at hc; exact Int.noConfusion hc
  have hg0 : g ≠ (ring (2 * m + 1 + 1)).zero := fun e => by rw [e, chi_zero] at hg; exact Int.noConfusion hg
  have hig := CharSumGen.iv_mul (2 * m) hpr g hg0
  have hig0 : (CharSumGen.iv (2 * m) g) ≠ (ring (2 * m + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * m + 1) (by rw [← hig, e]; lib_ring)
  have hcig : chi m (CharSumGen.iv (2 * m) g) = -1 := by
    have hone : chi m (ring (2 * m + 1 + 1)).one = 1 := by
      have := QChar.chi_sq m hpr (ring (2 * m + 1 + 1)).one (PowerSum.one_ne_zero' (2 * m + 1))
      rwa [show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).one by lib_ring] at this
    have := congrArg (chi m) hig
    rw [chi_mul m hpr, hg, hone] at this
    rw [show chi m (CharSumGen.iv (2 * m) g) = -1 * (chi m (CharSumGen.iv (2 * m) g) * -1) by lib_int, this]; rfl
  obtain ⟨e, he⟩ := sqrt_chi m hpr ((ring (2 * m + 1 + 1)).mul c (CharSumGen.iv (2 * m) g)) (by rw [chi_mul m hpr, hc, hcig]; rfl)
  have he0 : e ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => nz_mul m hpr _ _ hc0 hig0 (by rw [← he, e0]; lib_ring)
  have hceg : c = (ring (2 * m + 1 + 1)).mul g ((ring (2 * m + 1 + 1)).mul e e) := by
    rw [he, show (ring (2 * m + 1 + 1)).mul g ((ring (2 * m + 1 + 1)).mul c (CharSumGen.iv (2 * m) g)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) g) g) c by lib_ring, hig]; lib_ring
  rcases Euler.euler_pm m hpr e he0 with ep | en
  · -- χ(e) = 1: e = f², c = g f⁴
    obtain ⟨f, hf⟩ := sqrt_chi m hpr e (chi_one_of m e he0 ep)
    have hf0 : f ≠ (ring (2 * m + 1 + 1)).zero := fun f0 => he0 (by rw [← hf, f0]; lib_ring)
    have h1 := Jac1.tw_cls m hpr f g hf0
    rw [show (ring (2 * m + 1 + 1)).mul g ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul f f) ((ring (2 * m + 1 + 1)).mul f f)) = c by rw [hceg, ← hf]] at h1
    rw [h1]; exact sq_T _ _ (chi_unit m hpr f hf0)
  · -- χ(e) = −1: c·g = (ge)², χ(ge) = 1, ge = f², c = f⁴/g
    have hce : chi m e = -1 := chi_neg_of m e he0 (by rw [en]; exact ne_pm m)
    obtain ⟨f, hf⟩ := sqrt_chi m hpr ((ring (2 * m + 1 + 1)).mul g e) (by rw [chi_mul m hpr, hg, hce]; rfl)
    have hf0 : f ≠ (ring (2 * m + 1 + 1)).zero := fun f0 => nz_mul m hpr _ _ hg0 he0 (by rw [← hf, f0]; lib_ring)
    have h1 := Jac1.tw_cls m hpr f (CharSumGen.iv (2 * m) g) hf0
    rw [show (ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) g) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul f f) ((ring (2 * m + 1 + 1)).mul f f)) = c by
      rw [hf, hceg, show (ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) g) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul g e) ((ring (2 * m + 1 + 1)).mul g e)) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) g) g) g) ((ring (2 * m + 1 + 1)).mul e e) by lib_ring, hig]; lib_ring] at h1
    rw [h1, sq_T _ _ (chi_unit m hpr f hf0), Jac4.inv_cls m hpr g hg0, sq_T _ _ (chi_unit m hpr g hg0)]

end Jac5

#print axioms Jac5.sqrt_chi
#print axioms Jac5.nz_mul
#print axioms Jac5.sq_T
#print axioms Jac5.chi_unit
#print axioms Jac5.cls4
#print axioms Jac5.clsN

#print axioms: 6 theorems, no axioms.

78.259 The Jacobsthal identity \(T(1)^2+2T(g)^2=16p\)

For a non-square \(g\), the two class values of the family \(\sum \chi (x^5+cx)\) give a representation of \(16p\).

\[ T(1)^2+2\,T(g)^2=16p. \]

lang/Jac6.lean

/-
  lang/Jac6 — T48, cycle 731: **the class formula and T(1)² + 2T(g)² = 16p** (OPEN 256a-split): in 𝔽_p with i² = −1, s² = −i,
  g a non-square, N4(c) = #{y : y⁴ = c}:
    n4_zero, n4_four   N4(c) = 0 unless c is a 4th power, N4(e⁴) = 4
    cls_formula        c ≠ 0:  4T(c)² = T(1)²·N4(c) + 2T(g)²(1 − χ(c))
    T_zero             T(0) = 0
    jac_sum            T(1)² + 2T(g)² = 16p        (summing cls_formula against Jac3.jac_moment)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Jac5

namespace Jac6

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

theorem hm1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (i : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) : chi m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 1 := by
  have hi0 : i ≠ (ring (2 * m + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * m + 1) (by rw [← hi, e]; lib_ring)
  rw [show (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul i i by rw [show (ring (2 * m + 1 + 1)).mul i i = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) by lib_ring, hi]; lib_ring]
  exact QChar.chi_sq m hpr i hi0

/-- N4(c) = 0 when c is not a 4th power (every y⁴ = c would make c a 4th power). -/
theorem n4_zero (c : Zp (2 * m + 1 + 1)) (h : ∀ y, ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) ≠ c) : (sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) c) (L m)) = 0 := by
  rw [sumK_ext LibInt.intR _ (fun _ => LibInt.intR.zero) (fun y => Jac3.dz m _ _ (h y))]; exact zsum0 _

theorem n4_four (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (e : Zp (2 * m + 1 + 1)) (he : e ≠ (ring (2 * m + 1 + 1)).zero) : (sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e e) ((ring (2 * m + 1 + 1)).mul e e))) (L m)) = 4 := by
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  rw [sumK_ext LibInt.intR _ (fun y => LibInt.intR.add (LibInt.intR.add (LibInt.intR.add (delta LibInt.intR y e) (delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg e)))
      (delta LibInt.intR y ((ring (2 * m + 1 + 1)).mul i e))) (delta LibInt.intR y ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i e)))) (fun y => by
    rw [delta_symm, Jac3.four_delta m hpr h hh i s hi hs e y he]; rfl)]
  rw [sumK_add, sumK_add, sumK_add, hcomp, hcomp, hcomp, hcomp]; rfl

theorem cls_formula (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (g : Zp (2 * m + 1 + 1)) (hg : chi m g = -1) (c : Zp (2 * m + 1 + 1)) (hc : c ≠ (ring (2 * m + 1 + 1)).zero) :
    4 * (Jac1.T m c * Jac1.T m c) = Jac1.T m (ring (2 * m + 1 + 1)).one * Jac1.T m (ring (2 * m + 1 + 1)).one * (sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) c) (L m)) + 2 * (Jac1.T m g * Jac1.T m g) * (1 + -chi m c) := by
  have m1 := hm1 m hpr i hi
  rcases Euler.euler_pm m hpr c hc with ep | en
  · have hc1 : chi m c = 1 := chi_one_of m c hc ep
    obtain ⟨d, hd⟩ := Jac5.sqrt_chi m hpr c hc1
    have hd0 : d ≠ (ring (2 * m + 1 + 1)).zero := fun d0 => hc (by rw [← hd, d0]; lib_ring)
    rw [hc1]
    rcases Euler.euler_pm m hpr d hd0 with dp | dn
    · obtain ⟨e, he⟩ := Jac5.sqrt_chi m hpr d (chi_one_of m d hd0 dp)
      have he0 : e ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => hd0 (by rw [← he, e0]; lib_ring)
      have hce : c = ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul e e) ((ring (2 * m + 1 + 1)).mul e e)) := by rw [← hd, ← he]
      rw [hce, Jac5.cls4 m hpr e he0, n4_four m hpr h hh i s hi hs e he0]; lib_int
    · have hdm : chi m d = -1 := chi_neg_of m d hd0 (by rw [dn]; exact ne_pm m)
      rw [← hd, Jac4.zero_cls m hpr d hdm]
      rw [n4_zero m _ (fun y ey => by
        rcases SqCount.sq_eq m hpr _ _ ey with e1 | e1
        · have : chi m d = 1 := by
            rw [← e1]
            have hy : y ≠ (ring (2 * m + 1 + 1)).zero := fun y0 => hd0 (by
              have : (ring (2 * m + 1 + 1)).mul d d = (ring (2 * m + 1 + 1)).zero := by rw [← ey, y0]; lib_ring
              rcases G4Gen.mul_eq_zero m hpr _ _ this with e2 | e2 <;> exact e2)
            exact QChar.chi_sq m hpr y hy
          rw [hdm] at this; exact Int.noConfusion this
        · have : chi m d = 1 := by
            have e2 : d = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul y y) := by
              rw [show d = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg d) by lib_ring, ← e1]; lib_ring
            have hy : y ≠ (ring (2 * m + 1 + 1)).zero := fun y0 => hd0 (by rw [e2, y0]; lib_ring)
            rw [e2, chi_mul m hpr, m1, QChar.chi_sq m hpr y hy]; rfl
          rw [hdm] at this; exact Int.noConfusion this)]
      lib_int
  · have hcm : chi m c = -1 := chi_neg_of m c hc (by rw [en]; exact ne_pm m)
    rw [hcm, Jac5.clsN m hpr c g hcm hg]
    rw [n4_zero m _ (fun y ey => by
      have hy : y ≠ (ring (2 * m + 1 + 1)).zero := fun y0 => hc (by rw [← ey, y0]; lib_ring)
      have hyy : (ring (2 * m + 1 + 1)).mul y y ≠ (ring (2 * m + 1 + 1)).zero := Jac5.nz_mul m hpr _ _ hy hy
      have := QChar.chi_sq m hpr _ hyy
      rw [ey, hcm] at this; exact Int.noConfusion this)]
    lib_int


theorem sum3 (f1 f2 f3 : Zp (2 * m + 1 + 1) → Int) (a b d : Int) :
    sumK LibInt.intR (fun c => a * f1 c + b * f2 c + d * f3 c) (L m)
      = a * sumK LibInt.intR f1 (L m) + b * sumK LibInt.intR f2 (L m) + d * sumK LibInt.intR f3 (L m) := by
  show sumK LibInt.intR (fun c => LibInt.intR.add (LibInt.intR.add (LibInt.intR.mul a (f1 c)) (LibInt.intR.mul b (f2 c))) (LibInt.intR.mul d (f3 c))) (L m) = _
  rw [sumK_add, sumK_add, ← mul_sumK, ← mul_sumK, ← mul_sumK]; rfl

theorem T_zero (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : Jac1.T m (ring (2 * m + 1 + 1)).zero = 0 := by
  unfold Jac1.T
  rw [sumK_ext LibInt.intR _ (chi m) (fun x => by
    by_cases hx : x = (ring (2 * m + 1 + 1)).zero
    · rw [hx, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero) (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (ring (2 * m + 1 + 1)).zero) = (ring (2 *   -- (+29 chars; full line in the Zenodo source)
    · rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) x) x) x) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero x) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) x by lib_ring,
        Genus2.chi_sq_mul m hpr _ _ (Jac5.nz_mul m hpr _ _ hx hx)])]
  exact QChar.chi_sum m hpr

/-- **T(1)² + 2T(g)² = 16p.** -/
theorem jac_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (g : Zp (2 * m + 1 + 1)) (hg : chi m g = -1) :
    Jac1.T m (ring (2 * m + 1 + 1)).one * Jac1.T m (ring (2 * m + 1 + 1)).one + 2 * (Jac1.T m g * Jac1.T m g) = 16 * JacobMoment.P m := by
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  generalize hA : Jac1.T m (ring (2 * m + 1 + 1)).one * Jac1.T m (ring (2 * m + 1 + 1)).one = A
  generalize hB : Jac1.T m g * Jac1.T m g = B
  have pt : ∀ c, 4 * (Jac1.T m c * Jac1.T m c) = A * (sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) c) (L m)) + (2 * B) * (1 + -chi m c) + (-(A + 2 * B)) * delta LibInt.intR c (ring (2 * m + 1 + 1)).zero := fun c => by
    by_cases hc : c = (ring (2 * m + 1 + 1)).zero
    · rw [hc, T_zero m hpr, chi_zero, Jac3.dl m _ _ rfl]
      rw [sumK_ext LibInt.intR _ (fun y => delta LibInt.intR y (ring (2 * m + 1 + 1)).zero) (fun y => by
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [hy, Jac3.dl m _ _ (by lib_ring), Jac3.dl m _ _ rfl]
        · rw [Jac3.dz m _ _ (Jac5.nz_mul m hpr _ _ (Jac5.nz_mul m hpr _ _ hy hy) (Jac5.nz_mul m hpr _ _ hy hy)), Jac3.dz m _ _ hy]), hcomp, Genus2.oneI]
      lib_int
    · rw [Jac6.cls_formula m hpr h hh i s hi hs g hg c hc, hA, hB, Jac3.dz m _ _ hc]; lib_int
  have hsum := congrArg (fun F => F) (sumK_ext LibInt.intR _ _ pt (L m))
  rw [sum3] at hsum
  -- Σ 4T² = 4ΣT²
  have h4 : sumK LibInt.intR (fun c => 4 * (Jac1.T m c * Jac1.T m c)) (L m) = 4 * sumK LibInt.intR (fun c => Jac1.T m c * Jac1.T m c) (L m) := by
    show _ = LibInt.intR.mul _ _
    rw [mul_sumK]; rfl
  rw [h4, Jac3.jac_moment m hpr h hh i s hi hs] at hsum
  have sN : sumK LibInt.intR (fun c => (sumK LibInt.intR (fun y => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) c) (L m))) (L m) = JacobMoment.P m := by
    rw [sumK_swap]
    rw [sumK_ext LibInt.intR _ (fun _ => (1 : Int)) (fun y => by
      rw [sumK_ext LibInt.intR _ (fun c => delta LibInt.intR c ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y))) (fun c => delta_symm _ _ _), hcomp]; rfl)]
    exact QuadDiagonal.T1 m
  have sX : sumK LibInt.intR (fun c => 1 + -chi m c) (L m) = JacobMoment.P m := by
    rw [sumK_ext LibInt.intR _ (fun c => LibInt.intR.add 1 (LibInt.intR.mul (-1) (chi m c))) (fun c => by rw [Genus2.addI, Genus2.mulI]; lib_int),
      sumK_add, ← mul_sumK, QuadDiagonal.T1 m]
    have this : sumK LibInt.intR (chi m) (L m) = 0 := QChar.chi_sum m hpr
    show Int.ofNat (2 * m + 1 + 1 + 1) + -1 * sumK LibInt.intR (chi m) (L m) = _
    rw [this]; show Int.ofNat (2 * m + 1 + 1 + 1) + -1 * 0 = Int.ofNat (2 * m + 1 + 1 + 1); lib_int
  rw [sN, sX, hcomp, Genus2.oneI] at hsum
  -- (A + 2B)(p − 1) = 16 p (p − 1)
  have hP : JacobMoment.P m = Int.ofNat (2 * m + 1 + 1) + 1 := rfl
  rw [hP] at hsum ⊢
  apply JacobiNorm.int_cancel _ _ (2 * m + 1)
  generalize Int.ofNat (2 * m + 1 + 1) = Q at hsum ⊢
  rw [show (A + 2 * B) * Q = A * (Q + 1) + 2 * B * (Q + 1) + -(A + 2 * B) * 1 by lib_int, ← hsum]
  lib_int

end Jac6
#print axioms Jac6.hm1
#print axioms Jac6.n4_zero
#print axioms Jac6.n4_four
#print axioms Jac6.cls_formula
#print axioms Jac6.sum3
#print axioms Jac6.T_zero
#print axioms Jac6.jac_sum

#print axioms: 7 theorems, no axioms.

78.260 Parity and uniqueness for \(p=a^2+2b^2\)

\(2S^2+T^2=8p\) forces \(S=2a\), \(T=4b\), \(a^2+2b^2=p\), and \(a\) is \(\pm \mathrm {Re}\,\pi \).

\[ 2S^2+T^2=8p\Rightarrow S=2a,\ T=4b,\ a^2+2b^2=p;\qquad a^2=(\mathrm {Re}\,\pi )^2. \]

lang/Jac7a.lean

/-
  lang/Jac7a — T48, cycle 732: integer and ℤ[√−2] lemmas for the end of the 256a Jacobsthal route.
    par        2S² + T² = 8p ⇒ S = 2a, T = 4b, a² + 2b² = p
    uniq_re    nrm π = p prime, a² + 2b² = p ⇒ a² = (Re π)²       (Sq2Prime8.factor3 at k = 1, units ±1)
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.Sq2Prime8
import lang.Sq2Unique

namespace Jac7a

open FTAChain DetChain LibInt Frobenius

theorem odd_sq_even (k t : Int) (h : (2 * k + 1) * (2 * k + 1) = 2 * t) : False :=
  GaussInt.int_odd_ne_even (2 * (k * k) + 2 * k) t (by rw [← h]; lib_int)

theorem kill (k : Nat) (W : Int) (h : Int.ofNat (k + 1) * W = 0) : W = 0 :=
  JacobiNorm.int_cancel W 0 k (by
    calc W * Int.ofNat (k + 1) = Int.ofNat (k + 1) * W := by lib_int
      _ = 0 := h
      _ = 0 * Int.ofNat (k + 1) := by lib_int)

theorem par (S T p : Int) (h : 2 * (S * S) + T * T = 8 * p) : ∃ a b : Int, S = 2 * a ∧ T = 4 * b ∧ a * a + 2 * (b * b) = p := by
  rcases GaussInt.int_even_odd T with ⟨t, ht⟩ | ⟨t, ht⟩
  · rcases GaussInt.int_even_odd S with ⟨a, ha⟩ | ⟨k, hk⟩
    · rcases GaussInt.int_even_odd t with ⟨b, hb⟩ | ⟨c, hc⟩
      · refine ⟨a, b, ha, by rw [ht, hb]; lib_int, ?_⟩
        have e : Int.ofNat (7 + 1) * (a * a + 2 * (b * b) + -p) = 0 := by
          rw [show Int.ofNat (7 + 1) * (a * a + 2 * (b * b) + -p) = 2 * (S * S) + T * T + -(8 * p) by rw [ha, ht, hb]; show (8 : Int) * _ = _; lib_int, h]; lib_int
        have e2 := kill 7 _ e
        calc a * a + 2 * (b * b) = (a * a + 2 * (b * b) + -p) + p := by lib_int
          _ = p := by rw [e2]; lib_int
      · exfalso
        -- 8a² + 4(2c+1)² = 8p ⇒ (2c+1)² = 2(p − a²)·... : (2c+1)² = 2(p − a² − ... ) impossible
        have e : Int.ofNat (3 + 1) * ((2 * c + 1) * (2 * c + 1) + -(2 * (p + -(a * a)))) = 0 := by
          rw [show Int.ofNat (3 + 1) * ((2 * c + 1) * (2 * c + 1) + -(2 * (p + -(a * a)))) = 2 * (S * S) + T * T + -(8 * p) by rw [ha, ht, hc]; show (4 : Int) * _ = _; lib_int, h]; lib_int
        have e2 := kill 3 _ e
        exact odd_sq_even c (p + -(a * a)) (by
          calc (2 * c + 1) * (2 * c + 1) = ((2 * c + 1) * (2 * c + 1) + -(2 * (p + -(a * a)))) + 2 * (p + -(a * a)) := by lib_int
            _ = _ := by rw [e2]; lib_int)
    · exfalso
      have e : Int.ofNat (1 + 1) * ((2 * k + 1) * (2 * k + 1) + -(2 * (2 * p + -(t * t)))) = 0 := by
        rw [show Int.ofNat (1 + 1) * ((2 * k + 1) * (2 * k + 1) + -(2 * (2 * p + -(t * t)))) = 2 * (S * S) + T * T + -(8 * p) by rw [hk, ht]; show (2 : Int) * _ = _; lib_int, h]; lib_int
      have e2 := kill 1 _ e
      exact odd_sq_even k (2 * p + -(t * t)) (by
        calc (2 * k + 1) * (2 * k + 1) = ((2 * k + 1) * (2 * k + 1) + -(2 * (2 * p + -(t * t)))) + 2 * (2 * p + -(t * t)) := by lib_int
          _ = _ := by rw [e2]; lib_int)
  · exfalso
    exact odd_sq_even t (4 * p + -(S * S)) (by rw [← ht, show 2 * (4 * p + -(S * S)) = 8 * p + -(2 * (S * S)) by lib_int, ← h]; lib_int)


theorem no_sum (x : Nat) (h : x + 1 + 1 = 1) : False := Nat.noConfusion (Nat.succ.inj h)

theorem pow1 (n : Nat) : n ^ 1 = n := by show 1 * n = n; lib_nat

theorem fst_neg (z : Int × Int) : ((Quad.quad LibInt.intR (-2)).neg z).1 = -z.1 := rfl

/-- **a² + 2b² = p ⇒ a² = (Re π)²** for π of prime norm p in ℤ[√−2]. -/
theorem uniq_re (P : Nat) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : Sq2Euclid.nrm π = P + 1 + 1)
    (a b : Int) (hab : a * a + 2 * (b * b) = Int.ofNat (P + 1 + 1)) : a * a = π.1 * π.1 := by
  have hα : Sq2Euclid.nrm (a, b) = (P + 1 + 1) ^ 1 := by
    rw [pow1]; exact Int.ofNat.inj (by rw [Sq2Euclid.nrm_cast]; exact hab)
  obtain ⟨u, j, l, hu, hjl, he⟩ := Sq2Prime8.factor3 P hp π hπ 1 (a, b) hα
  have hu' : u = (Quad.quad LibInt.intR (-2)).one ∨ u = (Quad.quad LibInt.intR (-2)).neg (Quad.quad LibInt.intR (-2)).one := by
    rcases Sq2Unique.unit_pm u (Sq2Prime8.nrm_one_eq u hu) with e | e
    · exact Or.inl e
    · exact Or.inr (by rw [e]; rfl)
  have ha : a = (a, b).1 := rfl
  match j, l, hjl with
  | 1, 0, _ =>
    have e2 : (a, b) = (Quad.quad LibInt.intR (-2)).mul u π := by rw [he]; show (Quad.quad LibInt.intR (-2)).mul u ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one π) (Quad.quad LibInt.intR (-2)).one) = _; lib_ring
    rcases hu' with eu | eu
    · rw [ha, e2, eu, show (Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one π = π by lib_ring]
    · rw [ha, e2, eu, show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).neg (Quad.quad LibInt.intR (-2)).one) π = (Quad.quad LibInt.intR (-2)).neg π by lib_ring, fst_neg]; lib_int
  | 0, 1, _ =>
    have e2 : (a, b) = (Quad.quad LibInt.intR (-2)).mul u (Sq2Euclid.cj π) := by rw [he]; show (Quad.quad LibInt.intR (-2)).mul u ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one (Sq2Euclid.cj π))) = _; lib_ring
    rcases hu' with eu | eu
    · rw [ha, e2, eu, show (Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).one (Sq2Euclid.cj π) = Sq2Euclid.cj π by lib_ring]; rfl
    · rw [ha, e2, eu, show (Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).neg (Quad.quad LibInt.intR (-2)).one) (Sq2Euclid.cj π) = (Quad.quad LibInt.intR (-2)).neg (Sq2Euclid.cj π) by lib_ring, fst_neg]
      show -π.1 * -π.1 = _; lib_int
  | 0, 0, h => exact absurd h (fun e => Nat.noConfusion e)
  | j + 2, l, h => exact (no_sum (j + l) (by rw [show j + l + 1 + 1 = j + 2 + l by lib_nat]; exact h)).elim
  | 1, l + 1, h => exact (no_sum l (by rw [show l + 1 + 1 = 1 + (l + 1) by lib_nat]; exact h)).elim
  | 0, l + 2, h => exact (no_sum l (by rw [show l + 1 + 1 = 0 + (l + 2) by lib_nat]; exact h)).elim

end Jac7a
#print axioms Jac7a.odd_sq_even
#print axioms Jac7a.kill
#print axioms Jac7a.par
#print axioms Jac7a.no_sum
#print axioms Jac7a.pow1
#print axioms Jac7a.fst_neg
#print axioms Jac7a.uniq_re

#print axioms: 7 theorems, no axioms.

78.261 \(256a\) at \(p\equiv 1\pmod 8\): \(a_p^2=(\pi +\bar \pi )^2\)

For \(p\equiv 1\pmod 8\) the point count of \(256a\) is \(\pm \) the trace of the Hecke character.

\[ \Big (\sum _z\chi (z^3+4z^2+2z)\Big )^2=4\,(\mathrm {Re}\,\pi )^2,\qquad N(\pi )=p. \]

lang/Curve256Split.lean

/-
  lang/Curve256Split — T48, cycle 733: **256a at the primes with √−1 and √(−√−1) in 𝔽_p (p ≡ 1 mod 8): S² = (2 Re π)²**,
  S = Σ_z χ(z³ + 4z² + 2z), π ∈ ℤ[√−2] of norm p — i.e. a_p(256a)² = (π + π̄)², the curve side up to sign.
    t_m1     T(−1) = 2χ(σ³h)·S                 (Split256)
    t_one    T(−1) = χ(s)·T(1)                  (−1 = s⁴, Jac1)
    curve256_split_sq   S² = 4 (Re π)²          (Jac6.jac_sum, Jac7a.par, Jac7a.uniq_re)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Jac6
import lang.Jac7a
import lang.Split256

namespace Curve256Split

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

theorem t_m1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) :
    Jac1.T m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 2 * (chi m ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul ((K m).add s s) i)) h) * (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (  -- (+36 chars; full line in the Zenodo source)
  have e := Split256.split256 m hpr h hh i s hi hs
  unfold Jac1.T
  rw [← e]
  exact sumK_ext LibInt.intR _ _ (fun x => by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) x = (ring (2 * m + 1 + 1)).neg x by lib_ring]) _

theorem t_one (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) (hs0 : s ≠ (ring (2 * m + 1 + 1)).zero) :
    Jac1.T m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = chi m s * Jac1.T m (ring (2 * m + 1 + 1)).one := by
  have e := Jac1.tw_cls m hpr s (ring (2 * m + 1 + 1)).one hs0
  rw [show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul s s) ((ring (2 * m + 1 + 1)).mul s s)) = (ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one by
    rw [show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul s s) ((ring (2 * m + 1 + 1)).mul s s)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul s s) i) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul s s) ((ring (2 * m + 1 + 1)).neg   -- (+163 chars; full line in the Zenodo source)
      show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul s s) i) = (ring (2 * m + 1 + 1)).zero from hs, show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero from hi]; lib_ring] at e
  exact e

/-- **S² = 4 (Re π)²** for 256a at p ≡ 1 (mod 8). -/
theorem curve256_split_sq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero)
    (g : Zp (2 * m + 1 + 1)) (hg : chi m g = -1) (π : Int × Int) (hπ : Sq2Euclid.nrm π = 2 * m + 1 + 1 + 1) :
    (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (K m).one (K m).one)))) (L m)) * (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z))   -- (+65 chars; full line in the Zenodo source)
  obtain ⟨hi0, hs0, hsi, h2⟩ := Genus2.nz m hpr h hh i s hi hs
  have hsg : ((K m).mul ((K m).add s s) i) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => hsi (by
    have : (K m).mul ((K m).add (K m).one (K m).one) ((K m).mul s i) = (K m).zero := by rw [← e0]; lib_ring
    rcases G4Gen.mul_eq_zero m hpr _ _ this with e1 | e1
    · exact absurd e1 h2
    · exact e1)
  have hh0 : h ≠ (ring (2 * m + 1 + 1)).zero := fun e => PowerSum.one_ne_zero' (2 * m + 1) (by rw [← hh, e]; lib_ring)
  have hs3 : ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul ((K m).add s s) i)) h) ≠ (ring (2 * m + 1 + 1)).zero := Jac5.nz_mul m hpr _ _ (Jac5.nz_mul m hpr _ _ (Jac5.nz_mul m hpr _ _ hsg hsg) hsg) hh0
  have hA : Jac1.T m (ring (2 * m + 1 + 1)).one * Jac1.T m (ring (2 * m + 1 + 1)).one = 4 * ((sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (K m).one (K m).one)))) (L m)) * (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((  -- (+137 chars; full line in the Zenodo source)
    have e1 := t_one m hpr i s hi hs hs0
    have e2 := t_m1 m hpr h hh i s hi hs
    rw [e1] at e2
    calc Jac1.T m (ring (2 * m + 1 + 1)).one * Jac1.T m (ring (2 * m + 1 + 1)).one = (chi m s * Jac1.T m (ring (2 * m + 1 + 1)).one) * (chi m s * Jac1.T m (ring (2 * m + 1 + 1)).one) := (Jac5.sq_T _ _ (Jac5.chi_unit m hpr s hs0)).symm
      _ = (2 * (chi m ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul ((K m).add s s) i)) h) * (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (K m).one (K m).one)))) (L m)))) * (2 * (chi m ((K m).mul ((K  -- (+349 chars; full line in the Zenodo source)
      _ = 4 * ((chi m ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul ((K m).add s s) i)) h) * (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (K m).one (K m).one)))) (L m))) * (chi m ((K m).mul ((K m).mu  -- (+343 chars; full line in the Zenodo source)
      _ = 4 * ((sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (K m).one (K m).one)))) (L m)) * (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m)  -- (+106 chars; full line in the Zenodo source)
  have hsum := Jac6.jac_sum m hpr h hh i s hi hs g hg
  rw [hA] at hsum
  generalize (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (K m).one (K m).one)))) (L m)) = S at hsum ⊢
  generalize Jac1.T m g = Tg at hsum
  have w0 : 2 * (S * S) + Tg * Tg + -(8 * JacobMoment.P m) = 0 := Jac7a.kill 1 _ (by
    show (2 : Int) * _ = 0
    rw [show (2 : Int) * (2 * (S * S) + Tg * Tg + -(8 * JacobMoment.P m)) = 4 * (S * S) + 2 * (Tg * Tg) + -(16 * JacobMoment.P m) by lib_int, hsum]
    lib_int)
  have h8 : 2 * (S * S) + Tg * Tg = 8 * JacobMoment.P m := by
    calc 2 * (S * S) + Tg * Tg = (2 * (S * S) + Tg * Tg + -(8 * JacobMoment.P m)) + 8 * JacobMoment.P m := by lib_int
      _ = _ := by rw [w0]; lib_int
  obtain ⟨a, b, ha, _, hab⟩ := Jac7a.par _ _ _ h8
  have hu := Jac7a.uniq_re (2 * m + 1) hpr π hπ a b hab
  rw [ha, show 2 * a * (2 * a) = 4 * (a * a) by lib_int, hu]

end Curve256Split

#print axioms Curve256Split.t_m1
#print axioms Curve256Split.t_one
#print axioms Curve256Split.curve256_split_sq

#print axioms: 3 theorems, no axioms.

78.262 The fourth-power transform \(R(t)=\sum _{x^4=t}\chi (x)\)

The transform \(R\) is inversion-invariant, takes the values \(0\) and \(\pm 4\), and has second moment \(4(p-1)\).

\[ R(y^4)=4\chi (y),\quad R(1/t)=R(t),\quad \sum _t R(t)^2=4(p-1). \]

lang/Sign1.lean

/-
  lang/Sign1 — T48, cycle 735: **the fourth-power transform R(t) = Σ_{x⁴ = t} χ(x)** (sign of 256a, OPEN 256a-split), in 𝔽_p with
  i² = −1, s² = −i:
    sum_ne      a nonzero sum has a nonzero term
    R_pow       R(y⁴) = 4χ(y) for y ≠ 0;   R_zero  R(0) = 0
    H_val       H(t) = χ(t + 1)R(t) ∈ {0, 4, −4}
    R_inv, H_inv   R(1/t) = R(t), H(1/t) = H(t)
    RR_sum      Σ_t R(t)² = 4(p − 1)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Curve256Split

namespace Sign1

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

def R (t : Zp (2 * m + 1 + 1)) : Int := sumK LibInt.intR (fun x => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) t * chi m x) (L m)
def H (t : Zp (2 * m + 1 + 1)) : Int := chi m ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one) * R m t

theorem sum_ne {A : Type} (f : A → Int) : ∀ l : List A, sumK LibInt.intR f l ≠ 0 → ∃ a, f a ≠ 0
  | [], h => absurd rfl h
  | a :: l, h => by
    by_cases ha : f a = 0
    · exact sum_ne f l (fun e => h (by show LibInt.intR.add (f a) (sumK LibInt.intR f l) = 0; rw [ha, e]; rfl))
    · exact ⟨a, ha⟩

theorem chi_pm (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (y : Zp (2 * m + 1 + 1)) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) : chi m y = 1 ∨ chi m y = -1 := by
  rcases Euler.euler_pm m hpr y hy with e | e
  · exact Or.inl (chi_one_of m y hy e)
  · exact Or.inr (chi_neg_of m y hy (by rw [e]; exact ne_pm m))

theorem R_pow (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) (y : Zp (2 * m + 1 + 1)) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) : R m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).m  -- (+61 chars; full line in the Zenodo source)
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  obtain ⟨hi0, hs0, hsi, _⟩ := Genus2.nz m hpr h hh i s hi hs
  have m1 := Jac6.hm1 m hpr i hi
  have hci : chi m i = 1 := by rw [← Genus2.c5b m i s hi hs]; exact QChar.chi_sq m hpr _ hsi
  have hcni : chi m ((K m).neg i) = 1 := by
    rw [show (K m).neg i = (K m).mul s s by rw [show (K m).mul s s = (K m).add ((K m).add ((K m).mul s s) i) ((K m).neg i) by lib_ring, hs]; lib_ring]
    exact QChar.chi_sq m hpr s hs0
  have pk : ∀ z, sumK LibInt.intR (fun x => LibInt.intR.mul (delta LibInt.intR x z) (chi m x)) (L m) = chi m z := fun z => pick LibInt.intR _ hcomp (chi m) z
  unfold R
  rw [sumK_ext LibInt.intR _ (fun x => LibInt.intR.add (LibInt.intR.add (LibInt.intR.add
      (LibInt.intR.mul (delta LibInt.intR x y) (chi m x)) (LibInt.intR.mul (delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg y)) (chi m x)))
      (LibInt.intR.mul (delta LibInt.intR x ((ring (2 * m + 1 + 1)).mul i y)) (chi m x))) (LibInt.intR.mul (delta LibInt.intR x ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i y))) (chi m x))) (fun x => by
    rw [delta_symm, Jac3.four_delta m hpr h hh i s hi hs y x hy, Genus2.addI, Genus2.addI, Genus2.addI, Genus2.mulI, Genus2.mulI, Genus2.mulI, Genus2.mulI]; lib_int)]
  rw [sumK_add, sumK_add, sumK_add, pk, pk, pk, pk]
  rw [show (ring (2 * m + 1 + 1)).neg y = (ring (2 * m + 1 + 1)).mul ((K m).neg (K m).one) y by lib_ring, show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul i y) = (ring (2 * m + 1 + 1)).mul ((K m).neg i) y by lib_ring,
    chi_mul m hpr, chi_mul m hpr, chi_mul m hpr, m1, hci, hcni, Genus2.addI, Genus2.addI, Genus2.addI]
  lib_int

theorem q4_zero (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) : ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ≠ (ring (2 * m + 1 + 1)).zero :=
  Jac5.nz_mul m hpr _ _ (Jac5.nz_mul m hpr _ _ hx hx) (Jac5.nz_mul m hpr _ _ hx hx)

theorem R_zero (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : R m (ring (2 * m + 1 + 1)).zero = 0 := by
  unfold R
  rw [sumK_ext LibInt.intR _ (fun _ => LibInt.intR.zero) (fun x => by
    by_cases hx : x = (ring (2 * m + 1 + 1)).zero
    · rw [hx, chi_zero]; show _ * 0 = 0; lib_int
    · rw [Jac3.dz m _ _ (q4_zero m hpr x hx)]; show 0 * _ = 0; lib_int)]
  exact zsum0 _

/-- R(t) ≠ 0 ⇒ t = x⁴ with x ≠ 0. -/
theorem R_ne (t : Zp (2 * m + 1 + 1)) (hR : R m t ≠ 0) : ∃ x, x ≠ (ring (2 * m + 1 + 1)).zero ∧ ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) = t := by
  obtain ⟨x, hx⟩ := sum_ne _ _ hR
  refine ⟨x, fun e => hx (by rw [e, chi_zero]; show _ * 0 = 0; lib_int), ?_⟩
  by_cases e : ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) = t
  · exact e
  · exact absurd (by rw [Jac3.dz m _ _ e]; show (0 : Int) * _ = 0; lib_int) hx

theorem H_val (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) (t : Zp (2 * m + 1 + 1)) : H m t = 0 ∨ H m t = 4 ∨ H m t = -4 := by
  unfold H
  by_cases hR : R m t = 0
  · rw [hR]; exact Or.inl (by lib_int)
  · obtain ⟨x, hx, ht⟩ := R_ne m t hR
    rw [← ht, R_pow m hpr h hh i s hi hs x hx]
    by_cases h1 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).zero
    · rw [h1, chi_zero]; exact Or.inl (by lib_int)
    · rcases chi_pm m hpr _ h1 with e1 | e1 <;> rcases chi_pm m hpr x hx with e2 | e2 <;> rw [e1, e2]
      · exact Or.inr (Or.inl rfl)
      · exact Or.inr (Or.inr rfl)
      · exact Or.inr (Or.inr rfl)
      · exact Or.inr (Or.inl rfl)


theorem chi_iv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (x : Zp (2 * m + 1 + 1)) : chi m (CharSumGen.iv (2 * m) x) = chi m x := by
  by_cases hx : x = (ring (2 * m + 1 + 1)).zero
  · rw [hx, CharSumGen.iv_zero]
  · have e := congrArg (chi m) (CharSumGen.iv_mul (2 * m) hpr x hx)
    rw [chi_mul m hpr] at e
    have hone : chi m (ring (2 * m + 1 + 1)).one = 1 := by
      have := QChar.chi_sq m hpr (ring (2 * m + 1 + 1)).one (PowerSum.one_ne_zero' (2 * m + 1))
      rwa [show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).one by lib_ring] at this
    rw [hone] at e
    have hivx : (CharSumGen.iv (2 * m) x) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => by
      rw [e0, chi_zero] at e
      have h10 : (1 : Int) = 0 := by rw [← e]; lib_int
      exact Nat.noConfusion (Int.ofNat.inj h10)
    rcases chi_pm m hpr _ hivx with a | a <;> rcases chi_pm m hpr x hx with b | b <;> rw [a, b] at e ⊢
    · exact absurd e (fun e' => Int.noConfusion e')
    · exact absurd e (fun e' => Int.noConfusion e')

theorem q4_iv (x : Zp (2 * m + 1 + 1)) : ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) x) (CharSumGen.iv (2 * m) x)) ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) x) (CharSumGen.iv (2 * m) x))) = (CharSumGen.iv (2 * m) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))) := by
  rw [CharSumGen.iv_mulmul, CharSumGen.iv_mulmul]

theorem iv_inj (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (e : (CharSumGen.iv (2 * m) a) = (CharSumGen.iv (2 * m) b)) : a = b := by
  rw [← CharSumGen.iv_iv (2 * m) hpr a, e, CharSumGen.iv_iv (2 * m) hpr b]

theorem R_inv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (t : Zp (2 * m + 1 + 1)) : R m (CharSumGen.iv (2 * m) t) = R m t := by
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  unfold R
  rw [← reindex LibInt.intR _ hcomp (fun x => (CharSumGen.iv (2 * m) x)) (fun x => (CharSumGen.iv (2 * m) x)) (CharSumGen.iv_iv (2 * m) hpr) (CharSumGen.iv_iv (2 * m) hpr)]
  refine sumK_ext LibInt.intR _ _ (fun x => ?_) _
  show delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) x) (CharSumGen.iv (2 * m) x)) ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) x) (CharSumGen.iv (2 * m) x))) (CharSumGen.iv (2 * m) t) * chi m (CharSumGen.iv (2 * m) x) = _
  rw [q4_iv, chi_iv m hpr]
  by_cases e : ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) = t
  · rw [e, Jac3.dl m _ _ rfl, Jac3.dl m _ _ rfl]
  · rw [Jac3.dz m _ _ e, Jac3.dz m _ _ (fun e' => e (iv_inj m hpr _ _ e'))]

theorem H_inv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (t : Zp (2 * m + 1 + 1)) : H m (CharSumGen.iv (2 * m) t) = H m t := by
  unfold H
  rw [R_inv m hpr]
  by_cases hR : R m t = 0
  · rw [hR]; lib_int
  · obtain ⟨x, hx, ht⟩ := R_ne m t hR
    have ht0 : t ≠ (ring (2 * m + 1 + 1)).zero := fun e => by rw [← ht] at e; exact q4_zero m hpr x hx e
    have hct : chi m t = 1 := by rw [← ht]; exact QChar.chi_sq m hpr _ (Jac5.nz_mul m hpr _ _ hx hx)
    have hw := CharSumGen.iv_mul (2 * m) hpr t ht0
    rw [show (ring (2 * m + 1 + 1)).add (CharSumGen.iv (2 * m) t) (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) t) ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one) by
      rw [show (ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) t) ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) t) t) (CharSumGen.iv (2 * m) t) by lib_ring, hw]; lib_ring,
      chi_mul m hpr, chi_iv m hpr, hct]
    lib_int

theorem RR_sum (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) : sumK LibInt.intR (fun t => R m t * R m t) (L m) = 4 * (JacobMoment.P m + -1) := by
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  -- R t · R t = Σ_x Σ_y χ(x)χ(y)[x⁴ = t][y⁴ = t]
  rw [sumK_ext LibInt.intR _ (fun t => sumK LibInt.intR (fun x => sumK LibInt.intR (fun y => chi m x * chi m y * (delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) t * delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) t)) (L m)) (L m)) (fun t => by
    unfold R
    show LibInt.intR.mul _ _ = _
    rw [sumK_mul]
    exact sumK_ext LibInt.intR _ _ (fun x => by
      show LibInt.intR.mul _ _ = _
      rw [mul_sumK]
      exact sumK_ext LibInt.intR _ _ (fun y => by rw [Genus2.mulI]; lib_int) _) _)]
  rw [sumK_swap]
  rw [sumK_ext LibInt.intR _ (fun x => sumK LibInt.intR (fun y => chi m x * chi m y * delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y))) (L m)) (fun x => by
    rw [sumK_swap]
    exact sumK_ext LibInt.intR _ _ (fun y => by
      rw [show sumK LibInt.intR (fun t => chi m x * chi m y * (delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) t * delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y)) t)) (L m)
          = LibInt.intR.mul (chi m x * chi m y) (sumK LibInt.intR (fun t => LibInt.intR.mul (delta LibInt.intR t ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y))) (delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) t)) (L m)) by
            rw [mul_sumK]; exact sumK_ext LibInt.intR _ _ (fun t => by rw [delta_symm LibInt.intR t, Genus2.mulI, Genus2.mulI]; lib_int) _,
        pick LibInt.intR _ hcomp (fun t => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) t), Genus2.mulI]) _)]
  have hrow : ∀ x, sumK LibInt.intR (fun y => chi m x * chi m y * delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul y y))) (L m) = 4 + -4 * delta LibInt.intR x (ring (2 * m + 1 + 1)).zero := fun x => by
    by_cases hx : x = (ring (2 * m + 1 + 1)).zero
    · rw [hx, Jac3.dl m _ _ rfl, chi_zero]
      rw [sumK_ext LibInt.intR _ (fun _ => LibInt.intR.zero) (fun y => by show 0 * chi m y * _ = 0; lib_int)]
      rw [zsum0]; rfl
    · rw [Jac3.row m hpr h hh i s hi hs x hx, Jac3.dz m _ _ hx]; rfl
  rw [sumK_ext LibInt.intR _ _ hrow, PencilMoment.sum_lin m 4 (-4) (fun x => delta LibInt.intR x (ring (2 * m + 1 + 1)).zero), hcomp]
  show 4 * JacobMoment.P m + -4 * 1 = _; lib_int

end Sign1
#print axioms Sign1.R
#print axioms Sign1.H
#print axioms Sign1.sum_ne
#print axioms Sign1.chi_pm
#print axioms Sign1.R_pow
#print axioms Sign1.q4_zero
#print axioms Sign1.R_zero
#print axioms Sign1.R_ne
#print axioms Sign1.H_val
#print axioms Sign1.chi_iv
#print axioms Sign1.q4_iv
#print axioms Sign1.iv_inj
#print axioms Sign1.R_inv
#print axioms Sign1.H_inv
#print axioms Sign1.RR_sum

#print axioms: 15 theorems, no axioms.

78.263 The congruence \(\sum \chi (x^5+x)\equiv p-5\pmod {16}\)

When \(\sqrt {-1}\) and \(\sqrt {-\sqrt {-1}}\) exist in \(\mathbb F_p\), \(T(1)\) is determined modulo \(16\).

\[ \sum _x\chi (x^5+x)\equiv p-5\pmod {16}. \]

lang/Sign2.lean

/-
  lang/Sign2 — T48, cycle 736: **T(1) ≡ p − 5 (mod 16)** for T(1) = Σχ(x⁵ + x), when i² = −1, s² = −i in 𝔽_p (sign of 256a):
    fix_iv      [1/t = t] = [t = 0] + [t = 1] + [t = −1]
    inv_split   H(1/t) = H(t) ⇒ Σ H = 2·Σ_t H(t)[t < 1/t] + H(0) + H(1) + H(−1)      (LegendreFour.split3, swap_half)
    T1_H        T(1) = Σ_t χ(t + 1)R(t)
    t1_mod      T(1) = p − 5 + 16K
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Sign1
import lang.LegendreFour

namespace Sign2

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

theorem fix_iv (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (t : Zp (2 * m + 1 + 1)) :
    delta LibInt.intR (CharSumGen.iv (2 * m) t) t = delta LibInt.intR t (ring (2 * m + 1 + 1)).zero + delta LibInt.intR t (ring (2 * m + 1 + 1)).one + delta LibInt.intR t ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  have one_nz := PowerSum.one_ne_zero' (2 * m + 1)
  have h2 : (K m).add (K m).one (K m).one ≠ (K m).zero := fun e => one_nz (by
    rw [← hh, show (K m).add h h = (K m).mul h ((K m).add (K m).one (K m).one) by lib_ring, e]; lib_ring)
  have d01 : (ring (2 * m + 1 + 1)).one ≠ ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := fun e => h2 (by rw [show (K m).add (K m).one (K m).one = (K m).add (K m).one ((K m).neg ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) by lib_ring, ← e]; lib_ring)
  have nz1 : ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := fun e => one_nz (by rw [show (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) by lib_ring, e]; lib_ring)
  by_cases ht : t = (ring (2 * m + 1 + 1)).zero
  · rw [ht, CharSumGen.iv_zero, Jac3.dl m _ _ rfl, Jac3.dz m _ _ (fun e => one_nz e.symm), Jac3.dz m _ _ (fun e => nz1 e.symm)]; rfl
  · rw [Jac3.dz m _ _ ht]
    have hw := CharSumGen.iv_mul (2 * m) hpr t ht
    by_cases e : (CharSumGen.iv (2 * m) t) = t
    · rw [Jac3.dl m _ _ e]
      have tt : (ring (2 * m + 1 + 1)).mul t t = (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one := by
        calc (ring (2 * m + 1 + 1)).mul t t = (ring (2 * m + 1 + 1)).mul (CharSumGen.iv (2 * m) t) t := by rw [e]
          _ = (ring (2 * m + 1 + 1)).one := hw
          _ = _ := by lib_ring
      rcases SqCount.sq_eq m hpr _ _ tt with e1 | e1
      · rw [Jac3.dl m _ _ e1, Jac3.dz m _ _ (by rw [e1]; exact d01)]; rfl
      · rw [Jac3.dz m _ _ (by rw [e1]; exact fun e' => d01 e'.symm), Jac3.dl m _ _ e1]; rfl
    · rw [Jac3.dz m _ _ e]
      have n1 : t ≠ (ring (2 * m + 1 + 1)).one := fun e1 => e (by
        have hw1 := CharSumGen.iv_mul (2 * m) hpr (ring (2 * m + 1 + 1)).one one_nz
        rw [e1]; generalize (CharSumGen.iv (2 * m) (ring (2 * m + 1 + 1)).one) = w at hw1 ⊢
        calc w = (ring (2 * m + 1 + 1)).mul w (ring (2 * m + 1 + 1)).one := by lib_ring
          _ = (ring (2 * m + 1 + 1)).one := hw1)
      have n2 : t ≠ ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := fun e1 => e (by
        have hw' := CharSumGen.iv_mul (2 * m) hpr ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) nz1
        rw [e1]; generalize (CharSumGen.iv (2 * m) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = w at hw' ⊢
        calc w = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by lib_ring
          _ = ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by rw [hw']; lib_ring)
      rw [Jac3.dz m _ _ n1, Jac3.dz m _ _ n2]; rfl

theorem inv_split (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (F : Zp (2 * m + 1 + 1) → Int)
    (hF : ∀ t, F (CharSumGen.iv (2 * m) t) = F t) :
    sumK LibInt.intR F (L m) = 2 * sumK LibInt.intR (fun t => F t * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) (L m) + F (ring (2 * m + 1 + 1)).zero + F (ring (2 * m + 1 + 1)).one + F ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  have h3 : sumK LibInt.intR (fun t => F t * delta LibInt.intR (CharSumGen.iv (2 * m) t) t) (L m) = F (ring (2 * m + 1 + 1)).zero + F (ring (2 * m + 1 + 1)).one + F ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) := by
    rw [sumK_ext LibInt.intR _ (fun t => LibInt.intR.add (LibInt.intR.add (LibInt.intR.mul (delta LibInt.intR t (ring (2 * m + 1 + 1)).zero) (F t)) (LibInt.intR.mul (delta LibInt.intR t (ring (2 * m + 1 + 1)).one) (F t)))
        (LibInt.intR.mul (delta LibInt.intR t ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (F t))) (fun t => by
      rw [fix_iv m hpr h hh t, Genus2.addI, Genus2.addI, Genus2.mulI, Genus2.mulI, Genus2.mulI]; lib_int)]
    rw [sumK_add, sumK_add, pick LibInt.intR _ hcomp F, pick LibInt.intR _ hcomp F, pick LibInt.intR _ hcomp F, Genus2.addI, Genus2.addI]
  rw [LegendreFour.split3 m (fun t => (CharSumGen.iv (2 * m) t)) F, LegendreFour.swap_half m (fun t => (CharSumGen.iv (2 * m) t)) (CharSumGen.iv_iv (2 * m) hpr) F hF, h3]
  lib_int


theorem chi_two (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) : chi m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = 1 := by
  obtain ⟨_, _, _, h2⟩ := Genus2.nz m hpr h hh i s hi hs
  have e : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) s) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) s) := by
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) s) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) s) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+195 chars; full line in the Zenodo source)
        ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add i ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one)))) by lib_ring,
      show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul s s) i) = (ring (2 * m + 1 + 1)).zero from hs, show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero from hi]; lib_ring
  have hne : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one i) s ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => h2 (by rw [show (K m).add (K m).one (K m).one = ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) from rfl, e, e0]; lib_ring)
  rw [e]; exact QChar.chi_sq m hpr _ hne

theorem T1_H (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : Jac1.T m (ring (2 * m + 1 + 1)).one = sumK LibInt.intR (fun t => Sign1.H m t) (L m) := by
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  unfold Sign1.H Sign1.R
  rw [sumK_ext LibInt.intR (fun t => chi m ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one) * sumK LibInt.intR (fun x => delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) t * chi m x) (L m))
    (fun t => sumK LibInt.intR (fun x => chi m x * (delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) t * chi m ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one))) (L m)) (fun t => by
      show LibInt.intR.mul _ _ = _
      rw [mul_sumK]; exact sumK_ext LibInt.intR _ _ (fun x => by rw [Genus2.mulI]; lib_int) _)]
  rw [sumK_swap]
  unfold Jac1.T
  refine sumK_ext LibInt.intR _ _ (fun x => ?_) _
  rw [show sumK LibInt.intR (fun t => chi m x * (delta LibInt.intR ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) t * chi m ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one))) (L m)
      = LibInt.intR.mul (chi m x) (sumK LibInt.intR (fun t => LibInt.intR.mul (delta LibInt.intR t ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x))) (chi m ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one))) (L m)) by
        rw [mul_sumK]; exact sumK_ext LibInt.intR _ _ (fun t => by rw [delta_symm LibInt.intR t, Genus2.mulI, Genus2.mulI]) _,
    pick LibInt.intR _ hcomp (fun t => chi m ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one)), Genus2.mulI,
    show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) x) x) x) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one x) = (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).mul x x)) (ring (2 * m + 1 + 1)).one  -- (+14 chars; full line in the Zenodo source)
    chi_mul m hpr]

/-- **T(1) ≡ p − 5 (mod 16).** -/
theorem t1_mod (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) : ∃ Kq : Int, Jac1.T m (ring (2 * m + 1 + 1)).one = JacobMoment.P m + -5 + 16 * Kq := by
  obtain ⟨hi0, hs0, hsi, h2⟩ := Genus2.nz m hpr h hh i s hi hs
  have one_nz := PowerSum.one_ne_zero' (2 * m + 1)
  have hHi : ∀ t, Sign1.H m (CharSumGen.iv (2 * m) t) = Sign1.H m t := Sign1.H_inv m hpr
  have hE : ∀ t, Sign1.H m (CharSumGen.iv (2 * m) t) * Sign1.H m (CharSumGen.iv (2 * m) t) = Sign1.H m t * Sign1.H m t := fun t => by rw [hHi]
  have sH := inv_split m hpr h hh (Sign1.H m) hHi
  have sE := inv_split m hpr h hh (fun t => Sign1.H m t * Sign1.H m t) hE
  -- the three fixed points
  have R1 : Sign1.R m (ring (2 * m + 1 + 1)).one = 4 := by
    have := Sign1.R_pow m hpr h hh i s hi hs (ring (2 * m + 1 + 1)).one one_nz
    rw [show ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one by lib_ring] at this
    rw [this, show chi m (ring (2 * m + 1 + 1)).one = 1 by
      have := QChar.chi_sq m hpr (ring (2 * m + 1 + 1)).one one_nz; rwa [show (ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).one by lib_ring] at this]; rfl
  have H0 : Sign1.H m (ring (2 * m + 1 + 1)).zero = 0 := by unfold Sign1.H; rw [Sign1.R_zero m hpr]; lib_int
  have H1 : Sign1.H m (ring (2 * m + 1 + 1)).one = 4 := by unfold Sign1.H; rw [R1, show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one = ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) from rfl, chi_two m hpr h hh i s hi hs]; rfl
  have Hm1 : Sign1.H m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 0 := by unfold Sign1.H; rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).zero by lib_ring, chi_zero]; lib_int
  rw [H0, H1, Hm1] at sH
  rw [H0, H1, Hm1] at sE
  -- Σ H² = Σ R² − R(−1)² = 4(p − 1) − 16
  have hR4 : Sign1.R m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * Sign1.R m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) = 16 := by
    have e := Sign1.R_pow m hpr h hh i s hi hs s hs0
    rw [show ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul s s) ((ring (2 * m + 1 + 1)).mul s s)) = ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) by
      rw [show ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul s s) ((ring (2 * m + 1 + 1)).mul s s)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul s s) i) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul s s) ((ring (2 * m + 1 + 1)).neg i))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 +  -- (+111 chars; full line in the Zenodo source)
        show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul s s) i) = (ring (2 * m + 1 + 1)).zero from hs, show ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i i) (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).zero from hi]; lib_ring] at e
    rw [e, show 4 * chi m s * (4 * chi m s) = 16 * (chi m s * chi m s) by lib_int, Jac5.chi_unit m hpr s hs0]; rfl
  have hcomp : Complete LibInt.intR (L m) := CMInert.complete_allZ (2 * m + 1 + 1) LibInt.intR
  have sEE : sumK LibInt.intR (fun t => Sign1.H m t * Sign1.H m t) (L m) = 4 * (JacobMoment.P m + -1) + -16 := by
    rw [sumK_ext LibInt.intR _ (fun t => LibInt.intR.add (Sign1.R m t * Sign1.R m t) (LibInt.intR.mul (-(Sign1.R m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) * Sign1.R m ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one))) (delta LibInt.intR t ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)))) (fun t => by
      unfold Sign1.H
      rw [show chi m ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one) * Sign1.R m t * (chi m ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one) * Sign1.R m t) = (chi m ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one) * chi m ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one)) * (Sign1.R m t * Sign1.R m t) by lib_int,
        QuadDiagonal.sqchi m hpr, Genus2.addI, Genus2.mulI]
      by_cases ht : t = ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)
      · rw [ht, Jac3.dl m _ _ rfl, Jac3.dl m _ _ (by lib_ring)]; lib_int
      · rw [Jac3.dz m _ _ ht, Jac3.dz m _ _ (fun e => ht (by rw [show t = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add t (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) by lib_ring, e]; lib_ring))]; lib_int)]
    rw [sumK_add, ← mul_sumK, hcomp, Sign1.RR_sum m hpr h hh i s hi hs, hR4, Genus2.addI, Genus2.mulI, Genus2.oneI]; lib_int
  rw [sEE] at sE
  -- 4H − H² = 32k pointwise
  have hk : ∀ t, 4 * Sign1.H m t + -(Sign1.H m t * Sign1.H m t) = 32 * (if Sign1.H m t = -4 then -1 else 0) := fun t => by
    rcases Sign1.H_val m hpr h hh i s hi hs t with e | e | e
    · rw [e, ite_neg' _ _ (fun e' => Int.noConfusion e')]; rfl
    · rw [e, ite_neg' _ _ (fun e' => Int.noConfusion e')]; rfl
    · rw [e, ite_pos' _ _ rfl]; rfl
  have hA : 4 * sumK LibInt.intR (fun t => Sign1.H m t * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) (L m)
      + -(sumK LibInt.intR (fun t => Sign1.H m t * Sign1.H m t * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) (L m))
      = 32 * sumK LibInt.intR (fun t => (if Sign1.H m t = -4 then -1 else 0) * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) (L m) := by
    have this := Jac6.sum3 m (fun t => Sign1.H m t * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) (fun t => Sign1.H m t * Sign1.H m t * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t))
      (fun t => (if Sign1.H m t = -4 then -1 else 0) * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) 4 (-1) (-32)
    rw [sumK_ext LibInt.intR _ (fun _ => (0 : Int)) (fun t => by
      show 4 * (Sign1.H m t * _) + -1 * (Sign1.H m t * Sign1.H m t * _) + -32 * (_ * _) = 0
      rw [show 4 * (Sign1.H m t * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) + -1 * (Sign1.H m t * Sign1.H m t * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) = (4 * Sign1.H m t + -(Sign1.H m t * Sign1.H m t)) * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t) by lib_int, hk t]
      lib_int)] at this
    have z : sumK LibInt.intR (fun _ : Zp (2 * m + 1 + 1) => (0 : Int)) (L m) = 0 := zsum0 _
    rw [z] at this
    calc _ = (4 * sumK LibInt.intR (fun t => Sign1.H m t * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) (L m)
          + -1 * sumK LibInt.intR (fun t => Sign1.H m t * Sign1.H m t * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) (L m)
          + -32 * sumK LibInt.intR (fun t => (if Sign1.H m t = -4 then -1 else 0) * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) (L m))
          + 32 * sumK LibInt.intR (fun t => (if Sign1.H m t = -4 then -1 else 0) * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) (L m) := by lib_int
      _ = _ := by rw [← this]; lib_int
  rw [T1_H m hpr, sH]
  generalize sumK LibInt.intR (fun t => Sign1.H m t * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) (L m) = A at hA ⊢
  generalize sumK LibInt.intR (fun t => Sign1.H m t * Sign1.H m t * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) (L m) = B at hA sE
  generalize sumK LibInt.intR (fun t => (if Sign1.H m t = -4 then -1 else 0) * LegendreFour.ltI m t (CharSumGen.iv (2 * m) t)) (L m) = C at hA
  refine ⟨C, ?_⟩
  -- 2B = 4(P − 1) − 32, 4A = B + 32C
  have hB : B + -(2 * (JacobMoment.P m + -1) + -16) = 0 := Jac7a.kill 1 _ (by
    show (2 : Int) * _ = 0
    rw [show (2 : Int) * (B + -(2 * (JacobMoment.P m + -1) + -16)) = 2 * B + 0 * 0 + 4 * 4 + 0 * 0 + -(4 * (JacobMoment.P m + -1) + -16) by lib_int, ← sE]; lib_int)
  have hA2 : 2 * A + -((JacobMoment.P m + -1) + -8 + 16 * C) = 0 := Jac7a.kill 1 _ (by
    show (2 : Int) * _ = 0
    calc (2 : Int) * (2 * A + -((JacobMoment.P m + -1) + -8 + 16 * C)) = (4 * A + -B + -(32 * C)) + (B + -(2 * (JacobMoment.P m + -1) + -16)) := by lib_int
      _ = 0 := by rw [hA, hB]; lib_int)
  calc 2 * A + 0 + 4 + 0 = (2 * A + -((JacobMoment.P m + -1) + -8 + 16 * C)) + ((JacobMoment.P m + -1) + -8 + 16 * C) + 4 := by lib_int
    _ = _ := by rw [hA2]; lib_int
end Sign2

#print axioms Sign2.fix_iv
#print axioms Sign2.inv_split
#print axioms Sign2.chi_two
#print axioms Sign2.T1_H
#print axioms Sign2.t1_mod

#print axioms: 5 theorems, no axioms.

78.264 \(256a\) at \(p\equiv 1\pmod 8\): \(a_p=\pi +\bar \pi \)

For \(p\equiv 1\pmod 8\) and \(\pi \in H_8\) of norm \(p\), the point count of \(256a\) is the Hecke trace.

\[ \sum _z\chi (z^3+4z^2+2z)=-2\,\mathrm {Re}\,\pi ,\qquad \pi \in H_8,\ N(\pi )=p. \]

lang/Sign3.lean

/-
  lang/Sign3 — T48, cycle 737: **the sign: S = −2 Re π** for 256a at p ≡ 1 (mod 8) (i² = −1, s² = −i in 𝔽_p), π ∈ H8 of norm p:
    t1_two    T(1) = 2S                       (Curve256Split.t_one, t_m1; χ(σ³h) = χ(si) = χ(s))
    bad       a = 8e + c, b = 4f + d in an H8 class, a² + 2b² = p, 4a = p − 5 + 16K is impossible (residue 2 or 8 mod 16)
    sign256   Σ_z χ(z³ + 4z² + 2z) = −2 Re π          (Sign2.t1_mod, Curve256Split.curve256_split_sq)
  With Curve256Inert (p ≡ 5, 7 mod 8) this is the 256a curve side at every p ≢ 3 (mod 8).
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Sign2
import lang.HeckeEig

namespace Sign3

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

theorem t1_two (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) : Jac1.T m (ring (2 * m + 1 + 1)).one = 2 * (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mu  -- (+145 chars; full line in the Zenodo source)
  obtain ⟨hi0, hs0, hsi, h2⟩ := Genus2.nz m hpr h hh i s hi hs
  have hsg : ((K m).mul ((K m).add s s) i) ≠ (ring (2 * m + 1 + 1)).zero := fun e0 => hsi (by
    have : (K m).mul ((K m).add (K m).one (K m).one) ((K m).mul s i) = (K m).zero := by rw [← e0]; lib_ring
    rcases G4Gen.mul_eq_zero m hpr _ _ this with e1 | e1
    · exact absurd e1 h2
    · exact e1)
  have hci : chi m i = 1 := by rw [← Genus2.c5b m i s hi hs]; exact QChar.chi_sq m hpr _ hsi
  have hc : chi m ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul ((K m).add s s) i)) h) = chi m s := by
    rw [show ((K m).mul ((K m).mul ((K m).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((K m).mul ((K m).add s s) i)) h) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((K m).mul ((K m).add s s) i) ((K m).mul ((K m).add s s) i)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul s i) ((K m).add h h)) by lib_ring, hh,
      Genus2.chi_sq_mul m hpr _ _ hsg, show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul s i) (K m).one = (ring (2 * m + 1 + 1)).mul s i by lib_ring, chi_mul m hpr, hci]; lib_int
  have e1 := Curve256Split.t_one m hpr i s hi hs hs0
  have e2 := Curve256Split.t_m1 m hpr h hh i s hi hs
  rw [e1, hc] at e2
  have hss := Jac5.chi_unit m hpr s hs0
  calc Jac1.T m (ring (2 * m + 1 + 1)).one = (chi m s * chi m s) * Jac1.T m (ring (2 * m + 1 + 1)).one := by rw [hss]; lib_int
    _ = chi m s * (chi m s * Jac1.T m (ring (2 * m + 1 + 1)).one) := by lib_int
    _ = chi m s * (2 * (chi m s * (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (K m).one (K m).one)))) (L m)))) := by rw [e2]
    _ = 2 * ((chi m s * chi m s) * (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (K m).one (K m).one)))) (L m))) := by lib_int
    _ = _ := by rw [hss]; lib_int

theorem bad (a b P Kq e f : Int) (c d r : Nat) (q0 : Int) (h1 : a = 8 * e + Int.ofNat c) (h2 : b = 4 * f + Int.ofNat d)
    (hP : a * a + 2 * (b * b) = P) (hK : 4 * a = P + -5 + 16 * Kq) (hr0 : 0 < r) (hr : r < 16)
    (hcst : Int.ofNat c * Int.ofNat c + 2 * (Int.ofNat d * Int.ofNat d) + -(4 * Int.ofNat c) + -5 = 16 * q0 + Int.ofNat r) : False := by
  apply HeckeEig.nd 16 r (-Kq + -(4 * (e * e) + Int.ofNat c * e + 2 * (f * f) + Int.ofNat d * f + -(2 * e) + q0))
  · show (16 : Int) * _ = _
    have hz : a * a + 2 * (b * b) + -(4 * a) + -5 = -(16 * Kq) := by rw [hP, hK]; lib_int
    rw [h1, h2] at hz
    calc (16 : Int) * (-Kq + -(4 * (e * e) + Int.ofNat c * e + 2 * (f * f) + Int.ofNat d * f + -(2 * e) + q0))
        = -(16 * Kq) + -((8 * e + Int.ofNat c) * (8 * e + Int.ofNat c) + 2 * ((4 * f + Int.ofNat d) * (4 * f + Int.ofNat d)) + -(4 * (8 * e + Int.ofNat c)) + -5)
          + (Int.ofNat c * Int.ofNat c + 2 * (Int.ofNat d * Int.ofNat d) + -(4 * Int.ofNat c) + -5 + -(16 * q0)) := by lib_int
      _ = Int.ofNat r := by rw [hz, hcst]; lib_int
  · exact hr0
  · exact hr

/-- **S = −2 Re π** for 256a at p ≡ 1 (mod 8). -/
theorem sign256 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) (g : Zp (2 * m + 1 + 1)) (hg : chi m g = -1)
    (π : Int × Int) (hπ : Sq2Euclid.nrm π = 2 * m + 1 + 1 + 1) (pπ : Sq2Prim8.Prim8 π) :
    (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (K m).one (K m).one)))) (L m)) = -(2 * π.1) := by
  have hsq := Curve256Split.curve256_split_sq m hpr h hh i s hi hs g hg π hπ
  obtain ⟨Kq, hKq⟩ := Sign2.t1_mod m hpr h hh i s hi hs
  rw [t1_two m hpr h hh i s hi hs] at hKq
  have hP : π.1 * π.1 + 2 * (π.2 * π.2) = JacobMoment.P m := (Sq2Euclid.nrm_cast π).symm.trans (congrArg Int.ofNat hπ)
  generalize (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (K m).one (K m).one)))) (L m)) = S at hsq hKq ⊢
  have hprod : (S + -(2 * π.1)) * (S + 2 * π.1) = 0 := by
    rw [show (S + -(2 * π.1)) * (S + 2 * π.1) = S * S + -(4 * (π.1 * π.1)) by lib_int, hsq]; lib_int
  cases LibInt.int_noZD _ _ hprod with
  | inl e =>
    exfalso
    have hS : S = 2 * π.1 := by
      calc S = (S + -(2 * π.1)) + 2 * π.1 := by lib_int
        _ = 2 * π.1 := by rw [e, Genus2.zeroI]; lib_int
    have hK : 4 * π.1 = JacobMoment.P m + -5 + 16 * Kq := by rw [← hKq, hS]; lib_int
    rcases pπ with ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩ | ⟨e, f, h1, h2⟩
    · exact bad _ _ _ _ _ _ 1 0 8 (-1) h1 h2 hP hK (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl) rfl
    · exact bad _ _ _ _ _ _ 3 0 8 (-1) h1 h2 hP hK (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl) rfl
    · exact bad _ _ _ _ _ _ 5 1 2 (0) h1 h2 hP hK (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl) rfl
    · exact bad _ _ _ _ _ _ 5 2 8 (0) h1 h2 hP hK (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl) rfl
    · exact bad _ _ _ _ _ _ 5 3 2 (1) h1 h2 hP hK (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl) rfl
    · exact bad _ _ _ _ _ _ 7 1 2 (1) h1 h2 hP hK (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl) rfl
    · exact bad _ _ _ _ _ _ 7 2 8 (1) h1 h2 hP hK (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl) rfl
    · exact bad _ _ _ _ _ _ 7 3 2 (2) h1 h2 hP hK (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl) rfl
  | inr e =>
    calc S = (S + 2 * π.1) + -(2 * π.1) := by lib_int
      _ = -(2 * π.1) := by rw [e, Genus2.zeroI]; lib_int

end Sign3

#print axioms Sign3.t1_two
#print axioms Sign3.bad
#print axioms Sign3.sign256

#print axioms: 3 theorems, no axioms.

78.265 \(256a\): \(a_p=c(p)\) at \(p\equiv 1\pmod 8\)

The point count of \(256a\) is the Hecke theta coefficient \(c(p)\) at every \(p\equiv 1\pmod 8\).

\[ \sum _x\chi _p(x^3+4x^2+2x)=-\,c(p)_1,\qquad c(p)_2=0. \]

lang/Curve256Full.lean

/-
  lang/Curve256Full — T48, cycle 742: **256a: Σ_x χ_p(x³ + 4x² + 2x) = −(c(p)).1, (c(p)).2 = 0 at p ≡ 1 (mod 8)**, where c is the
  ℚ(√−2) Hecke theta series α ↦ α on H8 (HeckeMult8.cw8) — the same statement as Curve256Inert (p ≡ 5, 7 mod 8):
    tg_one          T_W(1) = W(π) + W(π̄)
    curve256_split  for π ∈ H8 of norm p (p ≡ 1 mod 8, i² = −1, s² = −i): Σχ = −(c(p)).1 and (c(p)).2 = 0
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Sign3
import lang.HeckePrime8

namespace Curve256Full

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

variable (m : Nat)

theorem tg_one {R : Type} (Kr : CRing R) (A B : R) : HeckePrime.Tg Kr A B 1 = Kr.add A B := by
  rw [HeckePrime.Tg_succ]
  show Kr.add (Kr.mul A (Kr.add (Kr.mul (Frobenius.pw Kr A 0) (Frobenius.pw Kr B 0)) Kr.zero)) (Kr.mul Kr.one B) = _
  show Kr.add (Kr.mul A (Kr.add (Kr.mul Kr.one Kr.one) Kr.zero)) (Kr.mul Kr.one B) = _
  lib_ring

/-- **256a at p ≡ 1 (mod 8): Σχ = −(c(p)).1.** -/
theorem curve256_split (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h : Zp (2 * m + 1 + 1)) (hh : (K m).add h h = (K m).one) (i s : Zp (2 * m + 1 + 1)) (hi : ((K m).add ((K m).mul i i) (K m).one) = (K m).zero) (hs : ((K m).add ((K m).mul s s) i) = (K m).zero) (g : Zp (2 * m + 1 + 1)) (hg : chi m g = -1)
    (π : Int × Int) (hπ : Sq2Euclid.nrm π = 2 * m + 1 + 1 + 1) (pπ : Sq2Prim8.Prim8 π) :
    (sumK LibInt.intR (fun z => chi m ((K m).mul z ((K m).add ((K m).add ((K m).mul z z) ((K m).mul ((K m).add ((K m).add (K m).one (K m).one) ((K m).add (K m).one (K m).one)) z)) ((K m).add (K m).one (K m).one)))) (L m)) = -(HeckeMult8.cw8 (fun z => z) (2 * m + 1 + 1 + 1)).1 ∧ (HeckeMult8.cw8 (fun z => z) (2 * m + 1 + 1 + 1)).2 = 0 := by
  have hc : HeckeMult8.cw8 (fun z => z) (2 * m + 1 + 1 + 1) = (Quad.quad LibInt.intR (-2)).add π (Sq2Euclid.cj π) := by
    have e := HeckePrime8.cw3_pk' (2 * m + 1) (fun z => z) (fun _ _ => rfl) rfl hpr π hπ pπ 1
    rw [Jac7a.pow1, tg_one] at e
    exact e
  rw [hc, Sign3.sign256 m hpr h hh i s hi hs g hg π hπ pπ]
  exact ⟨by show -(2 * π.1) = -(π.1 + π.1); lib_int, by show π.2 + -π.2 = 0; lib_int⟩

end Curve256Full

#print axioms Curve256Full.tg_one
#print axioms Curve256Full.curve256_split

#print axioms: 2 theorems, no axioms.

78.266 \(256a\): \(a_p=c(p)\) at every prime \(p\equiv 1\pmod 8\)

For every prime \(p\equiv 1\pmod 8\) the point count of \(256a\) is the Hecke theta coefficient, with no extra hypotheses.

\[ p=8b+9\ \text {prime}\ \Rightarrow \ \sum _x\chi _p(x^3+4x^2+2x)=-\,c(p)_1,\quad c(p)_2=0. \]

lang/Curve256P1.lean

/-
  lang/Curve256P1 — T48, cycle 743: **256a at every prime p ≡ 1 (mod 8), with no extra hypotheses**: for p = 8b + 9 prime,
    Σ_x χ_p(x³ + 4x² + 2x) = −(c(p)).1 and (c(p)).2 = 0,
  c the ℚ(√−2) Hecke theta series (HeckeMult8.cw8, α ↦ α on H8). The inputs of Curve256Full are constructed:
  i² = −1 (GaussSplit), w² = 2 (TwoSupp), s = (1 − i)/w (s² = −i), h = 1/2, a non-residue (QChar), and π ∈ H8 of norm p
  (TwoNorm2.norm2 for p = u² + 2v², Sq2Prim8.normalize8 for the primary associate).
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Curve256Full
import lang.TwoNorm2Split

namespace Curve256P1

open FTAChain ZMod YMChain FiniteRecChain DetChain QChar SqCount ConicSum

/-- **256a at p = 8b + 9.** -/
theorem curve256_p1 (b : Nat) (hpr : ∀ d, Dv d (2 * (2 * (2 * b + 1) + 1) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * (2 * b + 1) + 1) + 1 + 1 + 1)) :
    (sumK LibInt.intR (fun z => chi (2 * (2 * b + 1) + 1) ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul z ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul z z) ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add  -- (+584 chars; full line in the Zenodo source)
  obtain ⟨i, hi⟩ := GaussSplit.split_one_mod_four (2 * b + 1) hpr
  have e : (b + 1 + (b + 1)) + ((b + 1 + (b + 1)) + 0) + ((b + 1 + (b + 1)) + ((b + 1 + (b + 1)) + 0)) + 1 = (2 * (2 * (2 * b + 1) + 1) + 1 + 1 + 1) := by lib_nat
  obtain ⟨w0, hw0⟩ := TwoSupp.two_8m1 b (by rw [e]; exact ⟨⟨_, rfl⟩, hpr⟩)
  rw [e] at hw0
  have hw := Inf3mod8.rem_sq (2 * (2 * (2 * b + 1) + 1) + 1) w0 hw0
  rw [Inf3mod8.two_eq] at hw
  generalize mk _ w0 = w at hw
  have h2 := QuadDiagonal.two_ne (2 * (2 * b + 1) + 1)
  have hw0' : w ≠ (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).zero := fun e0 => h2 (by rw [← hw, e0]; lib_ring)
  have hiw := CharSumGen.iv_mul (2 * (2 * (2 * b + 1) + 1)) hpr w hw0'
  have hi2 := CharSumGen.iv_mul (2 * (2 * (2 * b + 1) + 1)) hpr ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).one (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).one) h2
  generalize CharSumGen.iv (2 * (2 * (2 * b + 1) + 1)) w = v at hiw
  generalize CharSumGen.iv (2 * (2 * (2 * b + 1) + 1)) ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).one (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).one) = h at hi2
  have hh : (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add h h = (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).one := by rw [← hi2]; lib_ring
  have hi' : (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul i i) (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).one = (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).zero := by rw [hi]; lib_ring
  have hs : (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).one ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).neg i)) v) ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add  -- (+161 chars; full line in the Zenodo source)
    have e1 : (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).one ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).neg i)) v) ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).a  -- (+107 chars; full line in the Zenodo source)
        = (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul v v) ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul i i) (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).one)) ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).  -- (+313 chars; full line in the Zenodo source)
      rw [show (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul v w) ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul v w) = (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul v v) ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul w w) by lib_ring, hw]; lib_ring
    rw [e1, hi', hiw]; lib_ring
  obtain ⟨g, _, hg⟩ := QChar.exists_nonres (2 * (2 * b + 1) + 1) hpr
  obtain ⟨u, vv, huv⟩ := TwoNorm2Split.fermat_1 b hpr
  have hen : Sq2Euclid.en (Int.ofNat u, Int.ofNat vv) = Int.ofNat (2 * (2 * (2 * b + 1) + 1) + 1 + 1 + 1) := by
    show Int.ofNat u * Int.ofNat u + 2 * (Int.ofNat vv * Int.ofNat vv) = _
    exact congrArg Int.ofNat huv
  have hodd : Sq2Euclid.en (Int.ofNat u, Int.ofNat vv) = 2 * Int.ofNat ((2 * (2 * b + 1) + 1) + 1) + 1 := by
    rw [hen]; exact congrArg Int.ofNat (show (2 * (2 * (2 * b + 1) + 1) + 1 + 1 + 1) = 2 * ((2 * (2 * b + 1) + 1) + 1) + 1 by lib_nat)
  obtain ⟨uu, huu, pπ⟩ := Sq2Prim8.normalize8 _ _ hodd
  have hπ : Sq2Euclid.nrm ((Quad.quad LibInt.intR (-2)).mul uu (Int.ofNat u, Int.ofNat vv)) = (2 * (2 * (2 * b + 1) + 1) + 1 + 1 + 1) := Int.ofNat.inj (by
    rw [Sq2Euclid.nrm_cast, Sq2Euclid.en_mul, huu, hen]; lib_int)
  exact Curve256Full.curve256_split (2 * (2 * b + 1) + 1) hpr h hh i ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).mul ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).add (ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).one ((ring (2 * (2 * (2 * b + 1) + 1) + 1 + 1)).neg i)) v) hi' hs g hg _ hπ pπ
end Curve256P1

#print axioms Curve256P1.curve256_p1

#print axioms: 1 theorems, no axioms.

78.267 The ring \(\mathbb Z[(1+\sqrt {-7})/2]\)

Pairs \(a+b\theta \) with \(\theta ^2=\theta -2\) form a commutative ring over any commutative ring.

\[ (a+b\theta )(c+d\theta )=(ac-2bd)+(ad+bc+bd)\theta . \]

lang/Ring7.lean

/-
  lang/Ring7 — T48/T50, cycle 637: the ring K[θ] = K[x]/(x² − x + 2) over any commutative ring K (for K = ℤ: the ring of integers
  ℤ[(1 + √−7)/2] of ℚ(√−7)). a + bθ as pairs (a, b); θ² = θ − 2:  (a + bθ)(c + dθ) = (ac − 2bd) + (ad + bc + bd)θ.
    r7mul, r7_assoc, r7_comm, r7_one, r7_distrib, ring7
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lib.RingTac

namespace Ring7

variable {R : Type} (K : DetChain.CRing R)

def r7mul (x y : R × R) : R × R :=
  (K.add (K.mul x.1 y.1) (K.neg (K.add (K.mul x.2 y.2) (K.mul x.2 y.2))), K.add (K.add (K.mul x.1 y.2) (K.mul x.2 y.1)) (K.mul x.2 y.2))

theorem r7_assoc (a b e : R × R) : r7mul K (r7mul K a b) e = r7mul K a (r7mul K b e) := by
  apply Prod.ext
  · show K.add (K.mul (K.add (K.mul a.1 b.1) (K.neg (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)))) e.1)
        (K.neg (K.add (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.2) (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.2))) =
      K.add (K.mul a.1 (K.add (K.mul b.1 e.1) (K.neg (K.add (K.mul b.2 e.2) (K.mul b.2 e.2)))))
        (K.neg (K.add (K.mul a.2 (K.add (K.add (K.mul b.1 e.2) (K.mul b.2 e.1)) (K.mul b.2 e.2))) (K.mul a.2 (K.add (K.add (K.mul b.1 e.2) (K.mul b.2 e.1)) (K.mul b.2 e.2)))))
    lib_ring
  · show K.add (K.add (K.mul (K.add (K.mul a.1 b.1) (K.neg (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)))) e.2)
        (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.1)) (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.2) =
      K.add (K.add (K.mul a.1 (K.add (K.add (K.mul b.1 e.2) (K.mul b.2 e.1)) (K.mul b.2 e.2)))
        (K.mul a.2 (K.add (K.mul b.1 e.1) (K.neg (K.add (K.mul b.2 e.2) (K.mul b.2 e.2)))))) (K.mul a.2 (K.add (K.add (K.mul b.1 e.2) (K.mul b.2 e.1)) (K.mul b.2 e.2)))
    lib_ring

theorem r7_comm (a b : R × R) : r7mul K a b = r7mul K b a := by
  apply Prod.ext
  · show K.add (K.mul a.1 b.1) (K.neg (K.add (K.mul a.2 b.2) (K.mul a.2 b.2))) = K.add (K.mul b.1 a.1) (K.neg (K.add (K.mul b.2 a.2) (K.mul b.2 a.2)))
    lib_ring
  · show K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2) = K.add (K.add (K.mul b.1 a.2) (K.mul b.2 a.1)) (K.mul b.2 a.2)
    lib_ring

theorem r7_one (a : R × R) : r7mul K (K.one, K.zero) a = a := by
  apply Prod.ext
  · show K.add (K.mul K.one a.1) (K.neg (K.add (K.mul K.zero a.2) (K.mul K.zero a.2))) = a.1
    lib_ring
  · show K.add (K.add (K.mul K.one a.2) (K.mul K.zero a.1)) (K.mul K.zero a.2) = a.2
    lib_ring

theorem r7_distrib (a b e : R × R) :
    r7mul K a (K.add b.1 e.1, K.add b.2 e.2) = (K.add (r7mul K a b).1 (r7mul K a e).1, K.add (r7mul K a b).2 (r7mul K a e).2) := by
  apply Prod.ext
  · show K.add (K.mul a.1 (K.add b.1 e.1)) (K.neg (K.add (K.mul a.2 (K.add b.2 e.2)) (K.mul a.2 (K.add b.2 e.2)))) =
      K.add (K.add (K.mul a.1 b.1) (K.neg (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)))) (K.add (K.mul a.1 e.1) (K.neg (K.add (K.mul a.2 e.2) (K.mul a.2 e.2))))
    lib_ring
  · show K.add (K.add (K.mul a.1 (K.add b.2 e.2)) (K.mul a.2 (K.add b.1 e.1))) (K.mul a.2 (K.add b.2 e.2)) =
      K.add (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) (K.add (K.add (K.mul a.1 e.2) (K.mul a.2 e.1)) (K.mul a.2 e.2))
    lib_ring

/-- **K[θ], θ² = θ − 2, as a commutative ring.** -/
def ring7 : DetChain.CRing (R × R) where
  add := fun x y => (K.add x.1 y.1, K.add x.2 y.2)
  mul := r7mul K
  neg := fun x => (K.neg x.1, K.neg x.2)
  zero := (K.zero, K.zero)
  one := (K.one, K.zero)
  add_assoc := fun a b c => by
    apply Prod.ext
    · exact K.add_assoc a.1 b.1 c.1
    · exact K.add_assoc a.2 b.2 c.2
  add_comm := fun a b => by
    apply Prod.ext
    · exact K.add_comm a.1 b.1
    · exact K.add_comm a.2 b.2
  zero_add := fun a => by
    apply Prod.ext
    · exact K.zero_add a.1
    · exact K.zero_add a.2
  add_neg := fun a => by
    apply Prod.ext
    · exact K.add_neg a.1
    · exact K.add_neg a.2
  mul_assoc := r7_assoc K
  mul_comm := r7_comm K
  one_mul := r7_one K
  distrib := r7_distrib K

end Ring7

#print axioms Ring7.r7_assoc
#print axioms Ring7.r7_comm
#print axioms Ring7.r7_one
#print axioms Ring7.r7_distrib
#print axioms Ring7.ring7

#print axioms: 5 theorems, no axioms.

78.268 \(\mathbb Z[(1+\sqrt {-7})/2]\) is Euclidean

Division with remainder in \(\mathbb Z[\theta ]\), \(N(a+b\theta )=a^2+ab+2b^2\), by rounding \(b\) first and then \(2a+b\).

\[ \beta \neq 0\ \Rightarrow \ \exists q,\ N(\alpha -q\beta )<N(\beta ). \]

lang/Euc7.lean

/-
  lang/Euc7 — T48/T50, cycle 638: ℤ[θ], θ = (1 + √−7)/2, is Euclidean.
  N(a + bθ) = a² + ab + 2b², 4N = (2a + b)² + 7b², conjugate (a + b) − bθ:
    en_rep, nrm_cast, en_mul, en_cj, four_nrm      norm facts (4·nrm = |2a + b|² + 7|b|² in ℕ)
    edivr          β ≠ 0: α = qβ + ρ, nrm ρ < nrm β  — two-step rounding: r₂ by N, then 2r₁ + r₂ by 2N, 16N(r) ≤ 11N²
    egcd_exists, eeuclid
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Ring7
import lang.GaussEuclid
import lang.W36Mul

namespace Euc7

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + z.1 * z.2 + 2 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Ring7.ring7 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + 2 * (B * B) := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_trans (Nat.le_add_left _ (A * A)) (Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_succ 1))) _))
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + 2 * (Int.ofNat B * Int.ofNat B) := congrArg Int.ofNat hc
  have opp : en z = Int.ofNat A * Int.ofNat A + 2 * (Int.ofNat B * Int.ofNat B) + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨c, by rw [h, ← e]; lib_int⟩
  have same : en z = Int.ofNat A * Int.ofNat A + 2 * (Int.ofNat B * Int.ofNat B) + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨A * A + 2 * (B * B) + A * B, h⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem four_nrm (z : Int × Int) : 4 * nrm z = Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 7 * (Int.natAbs z.2 * Int.natAbs z.2) := by
  apply Int.ofNat.inj
  show 4 * Int.ofNat (nrm z) = Int.ofNat (Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2)) + 7 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2)
  rw [nrm_cast, GaussEuclid.nas, GaussEuclid.nas]; unfold en; lib_int

theorem mul_cj (z : Int × Int) : (Ring7.ring7 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + z.2) + -(z.2 * -z.2 + z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 2 * (z.2 * z.2)
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + z.2) + z.2 * -z.2 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Ring7.ring7 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2)) + (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2)) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)
      + 2 * ((x.1 * y.2 + x.2 * y.1 + x.2 * y.2) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)) =
    (x.1 * x.1 + x.1 * x.2 + 2 * (x.2 * x.2)) * (y.1 * y.1 + y.1 * y.2 + 2 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + z.2) * (z.1 + z.2) + (z.1 + z.2) * -z.2 + 2 * (-z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 2 * (z.2 * z.2)
  lib_int

/-- **Division with remainder in ℤ[θ].** -/
theorem edivr (α β : Int × Int) (M : Nat) (hβ : nrm β = M + 1) :
    ∃ q, nrm ((Ring7.ring7 LibInt.intR).add α ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul q β))) < M + 1 := by
  have hN : en β = Int.ofNat (M + 1) := by rw [← nrm_cast, hβ]
  match GaussEuclid.rnd ((Ring7.ring7 LibInt.intR).mul α (cj β)).2 M with
  | ⟨q2, r2, h2, b2⟩ =>
  match GaussEuclid.rnd (2 * ((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + r2) (2 * M + 1) with
  | ⟨q1, t, h1, b1⟩ =>
    refine ⟨(q1, q2), ?_⟩
    generalize hρ : (Ring7.ring7 LibInt.intR).add α ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul (q1, q2) β)) = ρ
    have hρc : (Ring7.ring7 LibInt.intR).mul ρ (cj β) = (((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1)), r2) := by
      rw [← hρ]
      have e : (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).add α ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul (q1, q2) β))) (cj β) = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul α (cj β)) ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul (q1, q2) ((Ring7.ring7 LibInt.intR).mul β (cj β)))) := by lib_ring
      rw [e, mul_cj, hN]
      apply Prod.ext
      · show ((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1) + -(q2 * 0 + q2 * 0)) = ((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1))
        lib_int
      · show ((Ring7.ring7 LibInt.intR).mul α (cj β)).2 + -(q1 * 0 + q2 * Int.ofNat (M + 1) + q2 * 0) = r2
        rw [h2]; lib_int
    generalize hr1 : ((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1)) = r1 at hρc
    have ht : 2 * r1 + r2 = t := by
      rw [← hr1]
      calc 2 * (((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1))) + r2 = (2 * ((Ring7.ring7 LibInt.intR).mul α (cj β)).1 + r2) + -(q1 * Int.ofNat (2 * M + 1 + 1)) := by
            show _ = _ + -(q1 * (2 * Int.ofNat M + 1 + 1)); show 2 * (_ + -(q1 * (Int.ofNat M + 1))) + r2 = _; lib_int
        _ = t := by rw [h1]; lib_int
    have hn : en ρ * Int.ofNat (M + 1) = en (r1, r2) := by
      rw [← hN, ← en_cj β, ← en_mul, hρc]
    have hnN : nrm ρ * (M + 1) = nrm (r1, r2) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm ρ) * Int.ofNat (M + 1) = Int.ofNat (nrm (r1, r2))
      rw [nrm_cast, nrm_cast]; exact hn
    have h4 := four_nrm (r1, r2)
    have hT : Int.natAbs (2 * r1 + r2) ≤ M + 1 := by
      rw [ht]
      apply Nat.le_of_mul_le_mul_left (show 2 * Int.natAbs t ≤ 2 * (M + 1) from Nat.le_trans b1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
    generalize Int.natAbs (2 * r1 + r2) = T at h4 hT
    have h4' : 4 * nrm (r1, r2) = T * T + 7 * (Int.natAbs r2 * Int.natAbs r2) := h4
    generalize Int.natAbs r2 = B at h4' b2
    have hle : (M + 1) * (16 * nrm ρ) ≤ (M + 1) * (11 * (M + 1)) := by
      calc (M + 1) * (16 * nrm ρ) = 4 * (4 * nrm (r1, r2)) := by rw [← hnN]; lib_nat
        _ = 4 * (T * T) + 7 * ((2 * B) * (2 * B)) := by rw [h4']; lib_nat
        _ ≤ 4 * ((M + 1) * (M + 1)) + 7 * ((M + 1) * (M + 1)) := Nat.add_le_add (Nat.mul_le_mul_left 4 (Nat.mul_le_mul hT hT)) (Nat.mul_le_mul_left 7 (Nat.mul_le_mul b2 b2))
        _ = (M + 1) * (11 * (M + 1)) := by lib_nat
    have h16 := Nat.le_of_mul_le_mul_left hle (Nat.zero_lt_succ M)
    cases Nat.lt_or_ge (nrm ρ) (M + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      have : 16 * (M + 1) ≤ 11 * (M + 1) := Nat.le_trans (Nat.mul_le_mul_left 16 h) h16
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (show 11 * (M + 1) < 16 * (M + 1) by
        rw [show 16 * (M + 1) = 11 * (M + 1) + (5 * M + 5) by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)) this)

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Ring7.ring7 LibInt.intR).zero := by
  have h4 := four_nrm z
  rw [h] at h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by
    have : 7 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
    cases nat_mul_zero _ _ this with
    | inl e => exact Nat.noConfusion e
    | inr e => exact e
  have hT : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have t0 : 2 * z.1 + z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hT with | inl e => exact e | inr e => exact e)
  have a0 : z.1 = 0 := W36Mul.two_cancel_int z.1 0 (by rw [show 2 * z.1 = 2 * z.1 + z.2 by rw [b0]; lib_int, t0]; rfl)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Ring7.ring7 LibInt.intR).zero) :
    ∃ d x y, d = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x α) ((Ring7.ring7 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Ring7.ring7 LibInt.intR).one, (Ring7.ring7 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Ring7.ring7 LibInt.intR).one, by lib_ring⟩, ⟨(Ring7.ring7 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **gcd as a combination in ℤ[ω].** -/
theorem egcd_exists : ∀ G (α β : Int × Int), nrm β ≤ G →
    ∃ d x y, d = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x α) ((Ring7.ring7 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β
  | 0, α, β, hG => egcd_base α β (nrm_zero β (Nat.le_antisymm hG (Nat.zero_le _)))
  | G + 1, α, β, hG => by
    cases h : nrm β with
    | zero => exact egcd_base α β (nrm_zero β h)
    | succ M =>
        match edivr α β M h with
        | ⟨q, hq⟩ =>
          have hρ : nrm ((Ring7.ring7 LibInt.intR).add α ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul q β))) ≤ G :=
            Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hq (Nat.le_trans (Nat.le_of_eq h.symm) hG))
          match egcd_exists G β _ hρ with
          | ⟨d, x, y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
            refine ⟨d, y, (Ring7.ring7 LibInt.intR).add x ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul y q)), by rw [hd]; lib_ring, ⟨(Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul q c1) c2, ?_⟩, ⟨c1, hc1⟩⟩
            have e : α = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul q β) ((Ring7.ring7 LibInt.intR).add α ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul q β))) := by lib_ring
            rw [e, hc2, hc1]
            lib_ring

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x α) ((Ring7.ring7 LibInt.intR).mul y β) = (Ring7.ring7 LibInt.intR).one) (h : Dvd' α ((Ring7.ring7 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x γ) ((Ring7.ring7 LibInt.intR).mul y c), by
      calc γ = (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x α) ((Ring7.ring7 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul x α) γ) ((Ring7.ring7 LibInt.intR).mul y ((Ring7.ring7 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Ring7.ring7 LibInt.intR).mul α ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x γ) ((Ring7.ring7 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

end Euc7

#print axioms Euc7.sgn
#print axioms Euc7.le_dest
#print axioms Euc7.ab_le
#print axioms Euc7.en_rep
#print axioms Euc7.nrm_cast
#print axioms Euc7.four_nrm
#print axioms Euc7.mul_cj
#print axioms Euc7.en_mul
#print axioms Euc7.en_cj
#print axioms Euc7.edivr
#print axioms Euc7.nrm_zero
#print axioms Euc7.egcd_base
#print axioms Euc7.egcd_exists
#print axioms Euc7.eeuclid

#print axioms: 14 theorems, no axioms.

78.269 Coprime splitting in \(\mathbb Z[(1+\sqrt {-7})/2]\)

An element whose norm is a product of coprime factors splits accordingly.

\[ N(\gamma )=mn,\ sm+tn=1\ \Rightarrow \ \gamma =\alpha \beta ,\ N(\alpha )=m,\ N(\beta )=n. \]

lang/Cop7.lean

/-
  lang/Cop7 — T48/T50, cycle 639: splitting by coprime norms in ℤ[(1+√−7)/2].

  If N(γ) = mn with sm + tn = 1 (m, n ≥ 1), then γ = αβ with N(α) = m, N(β) = n (N(a + bθ) = a² + ab + 2b²):
  α = gcd(γ, m), e = gcd(γ, n) (Euc7.egcd_exists), αe | γ by Euclid's lemma, γ | αe since mn = γγ̄, so γ = αe·v with v a unit;
  then N(α) | m, N(e) | n and N(α)N(e) = mn give N(α) = m. (Port of GaussCoprime.coprime_split.)
    en_emb, emb_mul, en_dvd, emb_add, en_zero_imp, cancel_left, en_nonneg_one, ecoprime_split
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc7
import lang.GaussCoprime

namespace Cop7

open FTAChain DetChain LibInt Euc7

def emb (m : Int) : Int × Int := (m, 0)

theorem en_emb (m : Int) : en (emb m) = m * m := by show m * m + m * 0 + 2 * (0 * 0) = m * m; lib_int

theorem emb_mul (a b : Int) : (Ring7.ring7 LibInt.intR).mul (emb a) (emb b) = emb (a * b) := by
  apply Prod.ext
  · show a * b + -(0 * 0 + 0 * 0) = a * b; lib_int
  · show a * 0 + 0 * b + 0 * 0 = 0; lib_int

theorem en_dvd (d γ : Int × Int) (h : Dvd' d γ) : ∃ k, en γ = en d * k :=
  match h with
  | ⟨c, hc⟩ => ⟨en c, by rw [hc, en_mul]⟩

theorem emb_add (a b : Int) : emb (a + b) = (Ring7.ring7 LibInt.intR).add (emb a) (emb b) := by
  apply Prod.ext
  · rfl
  · show (0 : Int) = 0 + 0; rfl

theorem en_zero_imp (z : Int × Int) (h : en z = 0) : z = (Ring7.ring7 LibInt.intR).zero := by
  apply nrm_zero
  apply Int.ofNat.inj
  rw [nrm_cast]; exact h

theorem cancel_left (γ z : Int × Int) (hγ : en γ ≠ 0) (h : (Ring7.ring7 LibInt.intR).mul γ z = (Ring7.ring7 LibInt.intR).zero) : z = (Ring7.ring7 LibInt.intR).zero := by
  have : en γ * en z = 0 := by rw [← en_mul, h]; rfl
  cases int_noZD _ _ this with
  | inl e => exact absurd e hγ
  | inr e => exact en_zero_imp z e

theorem en_nonneg_one (z : Int × Int) (h : en z = -1) : False := by
  rw [← nrm_cast] at h
  exact Int.noConfusion h

/-- **Coprime splitting**: N(γ) = mn, sm + tn = 1 ⇒ γ = αβ with N(α) = m, N(β) = n. -/
theorem ecoprime_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) :
    ∃ α β, γ = (Ring7.ring7 LibInt.intR).mul α β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match egcd_exists _ γ (emb (Int.ofNat (m + 1))) (Nat.le_refl _), egcd_exists _ γ (emb (Int.ofNat (n + 1))) (Nat.le_refl _) with
  | ⟨d, x, y, hd, ⟨u, hu⟩, ⟨c1, hc1⟩⟩, ⟨e, x', y', he, ⟨u', hu'⟩, ⟨c2, hc2⟩⟩ =>
    have hco : (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (emb t) c2) e) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (emb s) c1) d) = (Ring7.ring7 LibInt.intR).one := by
      have h1 : (Ring7.ring7 LibInt.intR).one = emb (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; rfl
      rw [h1, emb_add, ← emb_mul, ← emb_mul, hc1, hc2]; lib_ring
    -- e | u, so γ = d e v
    match eeuclid e d u _ _ hco ⟨u', by rw [← hu, hu']⟩ with
    | ⟨v, hv⟩ =>
    have hγdev : γ = (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul d e) v := by rw [hu, hv]; lib_ring
    -- γ | d e
    have hmn : (Ring7.ring7 LibInt.intR).mul γ (cj γ) = emb (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by
      rw [mul_cj, hγ]; rfl
    have hW : (Ring7.ring7 LibInt.intR).mul d e = (Ring7.ring7 LibInt.intR).mul γ ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul x x') γ) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).  -- (+120 chars; full line in the Zenodo source)
      rw [hd, he]
      have : (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul y y') ((Ring7.ring7 LibInt.intR).mul γ (cj γ)) = (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul y y') ((Ring7.ring7 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1)))) := by rw [hmn, emb_mul]
      calc (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x γ) ((Ring7.ring7 LibInt.intR).mul y (emb (Int.ofNat (m + 1))))) ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul x' γ) ((Ring7.ring7 LibInt.intR).mul y' (emb (Int.ofNat (n + 1)))))
          = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul γ ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul x x') γ) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1))))))
              ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul y y') ((Ring7.ring7 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1))))) := by lib_ring
        _ = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul γ ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul x x') γ) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1))))))
              ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul y y') ((Ring7.ring7 LibInt.intR).mul γ (cj γ))) := by rw [this]
        _ = _ := by lib_ring
    generalize (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul x x') γ) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1)))) ((Ring7.ring7 LibInt.intR).mul   -- (+55 chars; full line in the Zenodo source)
    have hγ0 : en γ ≠ 0 := by rw [hγ]; exact fun e => Nat.noConfusion (Int.ofNat.inj e)
    have hWv : (Ring7.ring7 LibInt.intR).mul W v = (Ring7.ring7 LibInt.intR).one := by
      have h0 : (Ring7.ring7 LibInt.intR).mul γ ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul W v) ((Ring7.ring7 LibInt.intR).neg (Ring7.ring7 LibInt.intR).one)) = (Ring7.ring7 LibInt.intR).zero := by
        rw [show (Ring7.ring7 LibInt.intR).mul γ ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul W v) ((Ring7.ring7 LibInt.intR).neg (Ring7.ring7 LibInt.intR).one)) = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul γ W) v) ((Ring7.ring7 LibInt.intR).neg γ) by lib_ring, ← hW, ← hγdev]; lib_ring
      have := cancel_left γ _ hγ0 h0
      calc (Ring7.ring7 LibInt.intR).mul W v = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul W v) ((Ring7.ring7 LibInt.intR).neg (Ring7.ring7 LibInt.intR).one)) (Ring7.ring7 LibInt.intR).one := by lib_ring
        _ = (Ring7.ring7 LibInt.intR).one := by rw [this]; lib_ring
    have hv1 : en v = 1 := by
      have : en W * en v = 1 := by rw [← en_mul, hWv]; rfl
      cases GaussCoprime.int_unit _ _ (by rw [show en v * en W = en W * en v by lib_int]; exact this) with
      | inl h => exact h
      | inr h => exact absurd h (en_nonneg_one v)
    -- norms
    have hN : en d * en e = Int.ofNat (m + 1) * Int.ofNat (n + 1) := by
      have := congrArg en hγdev
      rw [en_mul, en_mul, hv1, hγ] at this
      rw [show en d * en e = en d * en e * 1 by lib_int, ← this]; rfl
    have hdm : ∃ a, Int.ofNat (m + 1) = en d * a := by
      have k1 : Int.ofNat (m + 1) * Int.ofNat (m + 1) = en d * en c1 := by rw [← en_emb, hc1, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en d * en u := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu, en_mul]
      exact ⟨s * en c1 + t * en u, by
        calc Int.ofNat (m + 1) = Int.ofNat (m + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = s * (Int.ofNat (m + 1) * Int.ofNat (m + 1)) + t * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en d * (s * en c1 + t * en u) := by rw [k1, k2]; lib_int⟩
    have hen' : ∃ b, Int.ofNat (n + 1) = en e * b := by
      have k1 : Int.ofNat (n + 1) * Int.ofNat (n + 1) = en e * en c2 := by rw [← en_emb, hc2, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en e * en u' := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu', en_mul]
      exact ⟨t * en c2 + s * en u', by
        calc Int.ofNat (n + 1) = Int.ofNat (n + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = t * (Int.ofNat (n + 1) * Int.ofNat (n + 1)) + s * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en e * (t * en c2 + s * en u') := by rw [k1, k2]; lib_int⟩
    match hdm, hen' with
    | ⟨a, ha⟩, ⟨b, hb⟩ =>
      have hab : a * b = 1 := by
        have h0 : (en d * en e) * (a * b + -1) = 0 := by
          rw [show (en d * en e) * (a * b + -1) = (en d * a) * (en e * b) + -(en d * en e) by lib_int, ← ha, ← hb, hN]; lib_int
        cases int_noZD _ _ h0 with
        | inl e0 => exact absurd (by rw [hN] at e0; exact e0) (fun e1 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = 0 => Nat.noConfusion (Int.ofNat.inj e1))
        | inr e0 => calc a * b = (a * b + -1) + 1 := by lib_int
            _ = 1 := by rw [e0]; rfl
      have pos : ∀ (k : Nat) (z : Int × Int) (c : Int), Int.ofNat (k + 1) = en z * c → c = 1 ∨ c = -1 → c = 1 := fun k z c h hc => by
        cases hc with
        | inl h1 => exact h1
        | inr h1 =>
          exfalso
          rw [h1, ← nrm_cast] at h
          have : Int.ofNat (k + 1) + Int.ofNat (nrm z) = 0 := by rw [h]; lib_int
          have h2 : k + nrm z + 1 = 0 := by rw [show k + nrm z + 1 = k + 1 + nrm z by lib_nat]; exact Int.ofNat.inj (show Int.ofNat (k + 1 + nrm z) = Int.ofNat 0 from this)
          exact Nat.noConfusion h2
      have a1 := pos m d a ha (GaussCoprime.int_unit a b hab)
      have b1 := pos n e b hb (GaussCoprime.int_unit b a (by rw [show b * a = a * b by lib_int]; exact hab))
      refine ⟨d, (Ring7.ring7 LibInt.intR).mul e v, by rw [hγdev]; lib_ring, by rw [ha, a1]; lib_int, by rw [en_mul, hv1, hb, b1]⟩

end Cop7

#print axioms Cop7.en_emb
#print axioms Cop7.emb_mul
#print axioms Cop7.en_dvd
#print axioms Cop7.emb_add
#print axioms Cop7.en_zero_imp
#print axioms Cop7.cancel_left
#print axioms Cop7.en_nonneg_one
#print axioms Cop7.ecoprime_split

#print axioms: 8 theorems, no axioms.

78.270 The primary class of 49a

The elements whose residue mod \(\sqrt {-7}\) is a nonzero square form a conjugation-stable complement of \(\{\pm 1\}\).

\[ H_7=\{a+b\theta :\ a+4b\equiv 1,2,4\pmod 7\}. \]

lang/Prim7.lean

/-
  lang/Prim7 — T48/T50, cycle 640: **the primary class of 49a's Hecke character** in ℤ[θ], θ = (1 + √−7)/2:
  ρ(a + bθ) = a + 4b is the reduction mod √−7 (θ ≡ 4), multiplicative mod 7; H7 = {ρ ≡ 1, 2, 4 (mod 7)} (the squares).
  (Numerically: π ∈ H7 of norm p gives 2a + b = a_p(y² = x³ − 35x − 98) for all split p < 140.)
    prim7_mul, prim7_cj      closed under products and conjugation
    unit_pm7                 N(u) = 1 ⇒ u = ±1
    normalize7               7 ∤ N(α) ⇒ ±α ∈ H7          (N ≡ ρ² mod 7, −1 is not a square mod 7)
    unit_one7                N(u) = 1, α, uα ∈ H7 ⇒ u = 1
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Cop7
import lang.Sq2Prim8

namespace Prim7

open FTAChain DetChain LibInt Euc7

def rho (z : Int × Int) : Int := z.1 + 4 * z.2

def Prim7 (z : Int × Int) : Prop := ∃ e : Int, rho z = 7 * e + 1 ∨ rho z = 7 * e + 2 ∨ rho z = 7 * e + 4

theorem rho_mul (x y : Int × Int) : rho ((Ring7.ring7 LibInt.intR).mul x y) = rho x * rho y + -(14 * (x.2 * y.2)) := by
  show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2) + 4 * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + 4 * x.2) * (y.1 + 4 * y.2) + -(14 * (x.2 * y.2))
  lib_int

theorem prim7_mul (x y : Int × Int) (hx : Prim7 x) (hy : Prim7 y) : Prim7 ((Ring7.ring7 LibInt.intR).mul x y) := by
  obtain ⟨e, hx⟩ := hx
  obtain ⟨f, hy⟩ := hy
  rw [Prim7, rho_mul]
  generalize x.2 * y.2 = w
  rcases hx with hx | hx | hx <;> rcases hy with hy | hy | hy <;> rw [hx, hy]
  · exact ⟨e + (7) * e * f + f + (-2) * w, Or.inl (by lib_int)⟩
  · exact ⟨(2) * e + (7) * e * f + f + (-2) * w, Or.inr (Or.inl (by lib_int))⟩
  · exact ⟨(4) * e + (7) * e * f + f + (-2) * w, Or.inr (Or.inr (by lib_int))⟩
  · exact ⟨e + (7) * e * f + (2) * f + (-2) * w, Or.inr (Or.inl (by lib_int))⟩
  · exact ⟨(2) * e + (7) * e * f + (2) * f + (-2) * w, Or.inr (Or.inr (by lib_int))⟩
  · exact ⟨(1) + (4) * e + (7) * e * f + (2) * f + (-2) * w, Or.inl (by lib_int)⟩
  · exact ⟨e + (7) * e * f + (4) * f + (-2) * w, Or.inr (Or.inr (by lib_int))⟩
  · exact ⟨(1) + (2) * e + (7) * e * f + (4) * f + (-2) * w, Or.inl (by lib_int)⟩
  · exact ⟨(2) + (4) * e + (7) * e * f + (4) * f + (-2) * w, Or.inr (Or.inl (by lib_int))⟩

theorem prim7_cj (z : Int × Int) (hz : Prim7 z) : Prim7 (cj z) := by
  obtain ⟨e, h⟩ := hz
  have hr : rho (cj z) = rho z + -(7 * z.2) := by show z.1 + z.2 + 4 * -z.2 = z.1 + 4 * z.2 + -(7 * z.2); lib_int
  refine ⟨e + -z.2, ?_⟩
  rw [hr]
  rcases h with h | h | h
  · exact Or.inl (by rw [h]; lib_int)
  · exact Or.inr (Or.inl (by rw [h]; lib_int))
  · exact Or.inr (Or.inr (by rw [h]; lib_int))

theorem unit_pm7 (u : Int × Int) (h : en u = 1) : u = (Ring7.ring7 LibInt.intR).one ∨ u = (-1, 0) := by
  have h4 := four_nrm u
  have hn : nrm u = 1 := Int.ofNat.inj (by rw [nrm_cast, h]; rfl)
  rw [hn] at h4
  have hB : Int.natAbs u.2 * Int.natAbs u.2 = 0 := by
    cases hb : Int.natAbs u.2 * Int.natAbs u.2 with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 7 * (t + 1) = (Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 7 * t + 3) + 4 by lib_nat] at h4
                exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h4))))
  have b0 : u.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at h4
  have hT : Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) = 2 * 2 := h4.symm
  have hT2 : Int.natAbs (2 * u.1 + u.2) = 2 := by
    match hx : Int.natAbs (2 * u.1 + u.2) with
    | 0 => rw [hx] at hT; exact Nat.noConfusion hT
    | 1 => rw [hx] at hT; exact Nat.noConfusion (Nat.succ.inj hT)
    | 2 => rfl
    | t + 3 => rw [hx, show (t + 3) * (t + 3) = (t * t + 6 * t + 5) + 4 by lib_nat] at hT; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hT))))
  rw [b0] at hT2
  obtain ⟨a, b⟩ := u
  show (a, b) = (1, 0) ∨ (a, b) = (-1, 0)
  have b0' : b = 0 := b0
  rcases EisEuclid.sgn (2 * a + 0) with s | s <;> rw [hT2] at s
  · exact Or.inl (Prod.ext (W36Mul.two_cancel_int a 1 (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')
  · exact Or.inr (Prod.ext (W36Mul.two_cancel_int a (-1) (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')

theorem neg_mul7 (z : Int × Int) : (Ring7.ring7 LibInt.intR).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -(0 * z.2 + 0 * z.2) = -z.1; lib_int) (by show -1 * z.2 + 0 * z.1 + 0 * z.2 = -z.2; lib_int)

theorem rho_neg (z : Int × Int) : rho (-z.1, -z.2) = -rho z := by
  show -z.1 + 4 * -z.2 = -(z.1 + 4 * z.2); lib_int

/-- 7 ∤ N(α) ⇒ ±α ∈ H7. -/
theorem normalize7 (α : Int × Int) (h7 : ∀ k, en α ≠ 7 * k) : ∃ u, en u = 1 ∧ Prim7 ((Ring7.ring7 LibInt.intR).mul u α) := by
  obtain ⟨q, r, hq, hr⟩ := GaussEuclid.rnd (rho α) 6
  have hq' : rho α = 7 * q + r := by rw [hq]; show q * 7 + r = 7 * q + r; lib_int
  have plus : ∀ e, (rho α = 7 * e + 1 ∨ rho α = 7 * e + 2 ∨ rho α = 7 * e + 4) → ∃ u, en u = 1 ∧ Prim7 ((Ring7.ring7 LibInt.intR).mul u α) :=
    fun e he => ⟨(Ring7.ring7 LibInt.intR).one, rfl, by rw [(Ring7.ring7 LibInt.intR).one_mul]; exact ⟨e, he⟩⟩
  have minus : ∀ e, (-rho α = 7 * e + 1 ∨ -rho α = 7 * e + 2 ∨ -rho α = 7 * e + 4) → ∃ u, en u = 1 ∧ Prim7 ((Ring7.ring7 LibInt.intR).mul u α) :=
    fun e he => ⟨(-1, 0), rfl, by rw [neg_mul7]; exact ⟨e, by rw [rho_neg]; exact he⟩⟩
  cases r with
  | ofNat t =>
    match t, hr with
    | 0, _ =>
      exfalso
      obtain ⟨a, b⟩ := α
      have ha : a = 7 * q + -(4 * b) := by
        have : a + 4 * b = 7 * q + Int.ofNat 0 := hq'
        calc a = (a + 4 * b) + -(4 * b) := by lib_int
          _ = _ := by rw [this]; show 7 * q + 0 + -(4 * b) = _; lib_int
      exact h7 (7 * q * q + -(7 * q * b) + 2 * b * b) (by show a * a + a * b + 2 * (b * b) = _; rw [ha]; lib_int)
    | 1, _ => exact plus q (Or.inl hq')
    | 2, _ => exact plus q (Or.inr (Or.inl hq'))
    | 3, _ => exact minus (-q + -1) (Or.inr (Or.inr (by rw [hq']; show -(7 * q + 3) = _; lib_int)))
    | t + 4, hr' =>
      exfalso
      have : 2 * (t + 4) ≤ 7 := hr'
      rw [show 2 * (t + 4) = 2 * t + 1 + 7 by lib_nat] at this
      exact Nat.not_succ_le_self 7 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 8) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  | negSucc t =>
    match t, hr with
    | 0, _ => exact minus (-q) (Or.inl (by rw [hq']; show -(7 * q + -1) = _; lib_int))
    | 1, _ => exact minus (-q) (Or.inr (Or.inl (by rw [hq']; show -(7 * q + -2) = _; lib_int)))
    | 2, _ => exact plus (q + -1) (Or.inr (Or.inr (by rw [hq']; show 7 * q + -3 = _; lib_int)))
    | t + 3, hr' =>
      exfalso
      have : 2 * (t + 4) ≤ 7 := hr'
      rw [show 2 * (t + 4) = 2 * t + 1 + 7 by lib_nat] at this
      exact Nat.not_succ_le_self 7 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 8) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))

/-- **A unit taking an H7-element to an H7-element is 1.** -/
theorem unit_one7 (u α : Int × Int) (hu : en u = 1) (pα : Prim7 α) (pu : Prim7 ((Ring7.ring7 LibInt.intR).mul u α)) : u = (Ring7.ring7 LibInt.intR).one := by
  rcases unit_pm7 u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, neg_mul7] at pu
  obtain ⟨e, h1⟩ := pα
  obtain ⟨f, h2⟩ := pu
  rw [rho_neg] at h2
  generalize rho α = x at h1 h2
  rcases h1 with h1 | h1 | h1 <;> rcases h2 with h2 | h2 | h2
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 4 1 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 1) + (7 * f + 1) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 3 2 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 1) + (7 * f + 2) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 1 4 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 1) + (7 * f + 4) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 3 2 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 2) + (7 * f + 1) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 2 3 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 2) + (7 * f + 2) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 0 5 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 2) + (7 * f + 4) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 1 4 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 4) + (7 * f + 1) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-1)) 0 5 (by calc (7 : Int) * (e + f + -(-1)) = (7 * e + 4) + (7 * f + 2) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (e + f + -(-2)) 5 0 (by calc (7 : Int) * (e + f + -(-2)) = (7 * e + 4) + (7 * f + 4) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl

end Prim7

#print axioms Prim7.rho_mul
#print axioms Prim7.prim7_mul
#print axioms Prim7.prim7_cj
#print axioms Prim7.unit_pm7
#print axioms Prim7.neg_mul7
#print axioms Prim7.rho_neg
#print axioms Prim7.normalize7
#print axioms Prim7.unit_one7

#print axioms: 8 theorems, no axioms.

78.271 Unique splitting into \(H_7\)

An \(H_7\)-element of norm \(mn\), \((m,n)=(mn,7)=1\), is uniquely a product of \(H_7\)-elements of norms \(m\) and \(n\).

\[ \gamma \in H_7,\ N(\gamma )=mn\ \Rightarrow \ \exists !\,(\alpha ,\beta )\in H_7^2,\ \gamma =\alpha \beta ,\ N(\alpha )=m. \]

lang/Uniq7.lean

/-
  lang/Uniq7 — T48/T50, cycle 641: the unique splitting into H7-elements (Prim7) in ℤ[(1+√−7)/2]:
    prim7_quot, uniq_split7, prim7_split   (norms prime to 7; port of Sq2Unique)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Prim7

namespace Uniq7

open FTAChain DetChain LibInt Euc7 Prim7

/-- y ∈ H7 when x and xy are (7 ∤ N(y)). -/
theorem prim7_quot (x y : Int × Int) (hy : ∀ k, en y ≠ 7 * k) (hx : Prim7 x) (hxy : Prim7 ((Ring7.ring7 LibInt.intR).mul x y)) : Prim7 y := by
  obtain ⟨u, hu, hpu⟩ := normalize7 y hy
  have h1 : Prim7 ((Ring7.ring7 LibInt.intR).mul u ((Ring7.ring7 LibInt.intR).mul x y)) := by
    rw [show (Ring7.ring7 LibInt.intR).mul u ((Ring7.ring7 LibInt.intR).mul x y) = (Ring7.ring7 LibInt.intR).mul x ((Ring7.ring7 LibInt.intR).mul u y) by lib_ring]
    exact prim7_mul _ _ hx hpu
  have hu1 := unit_one7 u _ hu hxy h1
  rw [hu1, (Ring7.ring7 LibInt.intR).one_mul] at hpu
  exact hpu

/-- **Uniqueness of the primary splitting.** -/
theorem uniq_split7 (α β α' β' : Int × Int) (M N s t : Int) (hα : en α = M) (hα' : en α' = M) (hβ' : en β' = N)
    (hst : s * M + t * N = 1) (hM : M ≠ 0) (pα : Prim7 α) (pα' : Prim7 α') (h : (Ring7.ring7 LibInt.intR).mul α β = (Ring7.ring7 LibInt.intR).mul α' β') :
    α = α' ∧ β = β' := by
  have hco : (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (Cop7.emb s) (cj α)) α) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (Cop7.emb t) (cj β')) β') = (Ring7.ring7 LibInt.intR).one := by
    rw [show (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (Cop7.emb s) (cj α)) α) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (Cop7.emb t) (cj β')) β') = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul (Cop7.emb s) ((Ring7.ring7 LibInt.intR).mul α (cj α))) ((Ring7.ring7 LibInt.intR).mul (Cop7.emb t) ((Ring7.ring7 LibInt.  -- (+35 chars; full line in the Zenodo source)
      mul_cj, mul_cj, hα, hβ']
    show (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul (Cop7.emb s) (Cop7.emb M)) ((Ring7.ring7 LibInt.intR).mul (Cop7.emb t) (Cop7.emb N)) = _
    rw [Cop7.emb_mul, Cop7.emb_mul, ← Cop7.emb_add, hst]; rfl
  match eeuclid α β' α' _ _ hco ⟨β, by rw [show (Ring7.ring7 LibInt.intR).mul β' α' = (Ring7.ring7 LibInt.intR).mul α' β' by lib_ring, ← h]⟩ with
  | ⟨w, hw⟩ =>
    have hgw : en w = 1 := by
      have e : M * en w = M * 1 := by
        calc M * en w = en α * en w := by rw [hα]
          _ = en α' := by rw [← en_mul, ← hw]
          _ = M * 1 := by rw [hα']; lib_int
      have e0 : M * (en w + -1) = 0 := by rw [show M * (en w + -1) = M * en w + -(M * 1) by lib_int, e]; lib_int
      cases int_noZD _ _ e0 with
      | inl h0 => exact absurd h0 hM
      | inr h0 => calc en w = (en w + -1) + 1 := by lib_int
          _ = 1 := by rw [h0]; rfl
    have hw1 : w = (Ring7.ring7 LibInt.intR).one :=
      unit_one7 w α hgw pα (by rw [show (Ring7.ring7 LibInt.intR).mul w α = α' by rw [hw]; lib_ring]; exact pα')
    have hαα : α = α' := by rw [hw, hw1]; lib_ring
    refine ⟨hαα, ?_⟩
    have h0 : (Ring7.ring7 LibInt.intR).mul α ((Ring7.ring7 LibInt.intR).add β ((Ring7.ring7 LibInt.intR).neg β')) = (Ring7.ring7 LibInt.intR).zero := by
      rw [show (Ring7.ring7 LibInt.intR).mul α ((Ring7.ring7 LibInt.intR).add β ((Ring7.ring7 LibInt.intR).neg β')) = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul α β) ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul α β')) by lib_ring, h, ← hαα]; lib_ring
    have := Cop7.cancel_left α _ (by rw [hα]; exact hM) h0
    calc β = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).add β ((Ring7.ring7 LibInt.intR).neg β')) β' := by lib_ring
      _ = β' := by rw [this]; lib_ring

/-- **Existence of the primary splitting.** -/
theorem prim7_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) (hm : ∀ k, Int.ofNat (m + 1) ≠ 7 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 7 * k)
    (pγ : Prim7 γ) : ∃ α β, γ = (Ring7.ring7 LibInt.intR).mul α β ∧ Prim7 α ∧ Prim7 β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match Cop7.ecoprime_split γ m n s t hγ hst with
  | ⟨α0, β0, h0, hα0, hβ0⟩ =>
    match normalize7 α0 (by rw [hα0]; exact hm) with
    | ⟨u, gu, pu⟩ =>
      have uc : (Ring7.ring7 LibInt.intR).mul u (cj u) = (Ring7.ring7 LibInt.intR).one := by rw [mul_cj, gu]; rfl
      have hβn : en ((Ring7.ring7 LibInt.intR).mul (cj u) β0) = Int.ofNat (n + 1) := by
        rw [en_mul, show en (cj u) = en u from en_cj u, gu, hβ0]; lib_int
      have hsplit : γ = (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul u α0) ((Ring7.ring7 LibInt.intR).mul (cj u) β0) := by
        rw [h0, show (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul u α0) ((Ring7.ring7 LibInt.intR).mul (cj u) β0) = (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul u (cj u)) ((Ring7.ring7 LibInt.intR).mul α0 β0) by lib_ring, uc]; lib_ring
      refine ⟨(Ring7.ring7 LibInt.intR).mul u α0, (Ring7.ring7 LibInt.intR).mul (cj u) β0, hsplit, pu,
        prim7_quot _ _ (by rw [hβn]; exact hn) pu (by rw [← hsplit]; exact pγ),
        by rw [en_mul, gu, hα0]; lib_int, hβn⟩

end Uniq7

#print axioms Uniq7.prim7_quot
#print axioms Uniq7.uniq_split7
#print axioms Uniq7.prim7_split

#print axioms: 3 theorems, no axioms.

78.272 49a: multiplicativity of the Hecke theta series

For every multiplicative \(w\) on \(\mathbb Z[(1+\sqrt {-7})/2]\) the theta coefficients over \(H_7\) are multiplicative.

\[ c_w(n)=\sum _{\alpha \in H_7,\ N\alpha =n}w(\alpha ),\qquad c_w(mn)=c_w(m)\,c_w(n). \]

lang/HeckeMult7.lean

/-
  lang/HeckeMult7 — T48/T50, cycle 642: **the theta series of 49a's Hecke character class is multiplicative**: for every
  multiplicative w on ℤ[θ], θ = (1 + √−7)/2,  c_w(n) = Σ_{α ∈ H7, N(α) = n} w(α)  satisfies c_w(mn) = c_w(m)c_w(n) for coprime m, n
  prime to 7.  Enumeration box7 n = HeckeMult.box (4n) (4N = (2a + b)² + 7b²).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Uniq7
import lang.HeckeMultW

namespace HeckeMult7

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Euc7 Prim7 Uniq7

abbrev box7 (n : Nat) : List (Int × Int) := HeckeMult.box (8 * n)

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 8n  (7a² + (a + 4b)² = 8N, (2a + b)² + 7b² = 4N). -/
theorem abs_le7 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 8 * n ∧ Int.natAbs z.2 ≤ 8 * n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have h4 := four_nrm z
  rw [hn] at h4
  have h8 : 7 * (Int.natAbs z.1 * Int.natAbs z.1) + Int.natAbs (z.1 + 4 * z.2) * Int.natAbs (z.1 + 4 * z.2) = 8 * n := Int.ofNat.inj (by
    show 7 * Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs (z.1 + 4 * z.2) * Int.natAbs (z.1 + 4 * z.2)) = 8 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; unfold en; lib_int)
  have seven : ∀ X : Nat, X ≤ 7 * (X * X) := fun X => Nat.le_trans (HeckeMult.le_sq X) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 6)))
  constructor
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h8))
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans (Nat.le_of_eq h4.symm) (Nat.mul_le_mul_right n (Nat.le_add_right 4 4))))

def PD7 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim7 z

theorem prim7_iff (z : Int × Int) :
    (ZMod.md 6 (Int.natAbs (rho z - 1)) = 0 ∨ ZMod.md 6 (Int.natAbs (rho z - 2)) = 0 ∨ ZMod.md 6 (Int.natAbs (rho z - 4)) = 0) ↔ Prim7 z := by
  constructor
  · intro h
    rcases h with h | h | h
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 6 _).mpr h
      exact ⟨e, Or.inl (by calc rho z = (rho z - 1) + 1 := by lib_int
                               _ = _ := by rw [he]; rfl)⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 6 _).mpr h
      exact ⟨e, Or.inr (Or.inl (by calc rho z = (rho z - 2) + 2 := by lib_int
                                        _ = _ := by rw [he]; rfl))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 6 _).mpr h
      exact ⟨e, Or.inr (Or.inr (by calc rho z = (rho z - 4) + 4 := by lib_int
                                        _ = _ := by rw [he]; rfl))⟩
  · intro ⟨e, h⟩
    rcases h with h | h | h
    · exact Or.inl ((HeckeMult.idv_iff 6 _).mp ⟨e, by rw [h]; show 7 * e + 1 - 1 = 7 * e; lib_int⟩)
    · exact Or.inr (Or.inl ((HeckeMult.idv_iff 6 _).mp ⟨e, by rw [h]; show 7 * e + 2 - 2 = 7 * e; lib_int⟩))
    · exact Or.inr (Or.inr ((HeckeMult.idv_iff 6 _).mp ⟨e, by rw [h]; show 7 * e + 4 - 4 = 7 * e; lib_int⟩))

instance (z : Int × Int) : Decidable (Prim7 z) := decidable_of_iff _ (prim7_iff z)

instance (n : Nat) (z : Int × Int) : Decidable (PD7 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum7 {A : Type} : ∀ l : List A, sumK (Ring7.ring7 LibInt.intR) (fun _ => (Ring7.ring7 LibInt.intR).zero) l = (Ring7.ring7 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Ring7.ring7 LibInt.intR).add (Ring7.ring7 LibInt.intR).zero (sumK (Ring7.ring7 LibInt.intR) (fun _ => (Ring7.ring7 LibInt.intR).zero) l) = _; rw [zsum7 l]; exact (Ring7.ring7 LibInt.intR).zero_add _

def I7 (n : Nat) (z : Int × Int) : Int × Int := if PD7 n z then (Ring7.ring7 LibInt.intR).one else (Ring7.ring7 LibInt.intR).zero

theorem I7_pos (n : Nat) (z : Int × Int) (h : PD7 n z) : I7 n z = (Ring7.ring7 LibInt.intR).one := ite_pos' _ _ h
theorem I7_neg (n : Nat) (z : Int × Int) (h : ¬ PD7 n z) : I7 n z = (Ring7.ring7 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw7 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Ring7.ring7 LibInt.intR) (fun z => (Ring7.ring7 LibInt.intR).mul (I7 n z) (w z)) (box7 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult7 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 7 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 7 * k) :
    cw7 w ((m + 1) * (n + 1)) = (Ring7.ring7 LibInt.intR).mul (cw7 w (m + 1)) (cw7 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD7 (m + 1) α → PD7 (n + 1) β → PD7 ((m + 1) * (n + 1)) ((Ring7.ring7 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim7_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Ring7.ring7 LibInt.intR).mul (cw7 w (m + 1)) (cw7 w (n + 1))
      = sumK (Ring7.ring7 LibInt.intR) (fun α => sumK (Ring7.ring7 LibInt.intR) (fun β => sumK (Ring7.ring7 LibInt.intR) (fun γ => (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (I7 (m + 1) α) (I7 (n + 1) β)) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β))) (w γ)) (box7 ((m + 1) * (n + 1)))) (box7 (n + 1))) (box7 (m + 1)) := by
    unfold cw7; rw [sumK_mul]
    refine sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD7 (m + 1) α
    · by_cases hβ : PD7 (n + 1) β
      · rw [I7_pos _ _ hα, I7_pos _ _ hβ]
        have hb := abs_le7 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Ring7.ring7 LibInt.intR) (fun γ => w γ) ((Ring7.ring7 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Ring7.ring7 LibInt.intR) (fun γ => (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (Ring7.ring7 LibInt.intR).one (Ring7.ring7 LibInt.intR).one) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β))) (w γ)) (box7 ((m + 1) * (n + 1)))
            = sumK (Ring7.ring7 LibInt.intR) (fun γ => (Ring7.ring7 LibInt.intR).mul (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β)) (w γ)) (box7 ((m + 1) * (n + 1))) from sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I7_neg _ _ hβ]
        rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun γ => show (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (I7 (m + 1) α) (Ring7.ring7 LibInt.intR).zero) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β))) (w γ) = (Ring7.ring7 LibInt.intR).zero by lib_ring)]
        rw [zsum7]; lib_ring
    · rw [I7_neg _ _ hα]
      rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun γ => show (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (Ring7.ring7 LibInt.intR).zero (I7 (n + 1) β)) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β))) (w γ) = (Ring7.ring7 LibInt.intR).zero by lib_ring)]
      rw [zsum7]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => sumK_swap (Ring7.ring7 LibInt.intR) _ _ _)]
  rw [sumK_swap (Ring7.ring7 LibInt.intR)]
  unfold cw7
  refine sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Ring7.ring7 LibInt.intR) (fun α => sumK (Ring7.ring7 LibInt.intR) (fun β => (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (I7 (m + 1) α) (I7 (n + 1) β)) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β))) (w γ)) (box7 (n + 1))) (box7 (m + 1))
      = (Ring7.ring7 LibInt.intR).mul (sumK (Ring7.ring7 LibInt.intR) (fun α => sumK (Ring7.ring7 LibInt.intR) (fun β => (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (I7 (m + 1) α) (I7 (n + 1) β)) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β))) (box7 (n + 1))) (box7 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD7 ((m + 1) * (n + 1)) γ
  · rw [I7_pos _ _ hγ]
    match prim7_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (I7 (m + 1) α) (I7 (n + 1) β)) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β)) = (Ring7.ring7 LibInt.intR).mul (delta (Ring7.ring7 LibInt.intR) α α0) (delta (Ring7.ring7 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD7 (m + 1) α ∧ PD7 (n + 1) β ∧ γ = (Ring7.ring7 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split7 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I7_pos _ _ hα, I7_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Ring7.ring7 LibInt.intR).mul (delta (Ring7.ring7 LibInt.intR) α α0) (delta (Ring7.ring7 LibInt.intR) β β0) = (Ring7.ring7 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD7 (m + 1) α
            · by_cases hβ : PD7 (n + 1) β
              · have hg : ¬ γ = (Ring7.ring7 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I7_neg _ _ hβ]; lib_ring
            · rw [I7_neg _ _ hα]; lib_ring
      rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le7 α0 _ nα0
      have bb := abs_le7 β0 _ nβ0
      rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Ring7.ring7 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => show (Ring7.ring7 LibInt.intR).mul (delta (Ring7.ring7 LibInt.intR) α α0) (Ring7.ring7 LibInt.intR).one = (Ring7.ring7 LibInt.intR).mul (delta (Ring7.ring7 LibInt.intR) α α0) (Ring7.ring7 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Ring7.ring7 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I7_neg _ _ hγ]
    have pt : ∀ α β, (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (I7 (m + 1) α) (I7 (n + 1) β)) (delta (Ring7.ring7 LibInt.intR) γ ((Ring7.ring7 LibInt.intR).mul α β)) = (Ring7.ring7 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD7 (m + 1) α
      · by_cases hβ : PD7 (n + 1) β
        · have hg : ¬ γ = (Ring7.ring7 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I7_neg _ _ hβ]; lib_ring
      · rw [I7_neg _ _ hα]; lib_ring
    rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun α => zsum7 _), zsum7]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult7_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 7 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 7 * k) :
    cw7 (fun z => Frobenius.pw (Ring7.ring7 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Ring7.ring7 LibInt.intR).mul (cw7 (fun z => Frobenius.pw (Ring7.ring7 LibInt.intR) z k) (m + 1)) (cw7 (fun z => Frobenius.pw (Ring7.ring7 LibInt.intR) z k) (n + 1)) :=
  hecke_mult7 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult7

#print axioms HeckeMult7.abs_le7
#print axioms HeckeMult7.prim7_iff
#print axioms HeckeMult7.zsum7
#print axioms HeckeMult7.I7_pos
#print axioms HeckeMult7.I7_neg
#print axioms HeckeMult7.hecke_mult7
#print axioms HeckeMult7.hecke_mult7_pow
#print axioms HeckeMult7.cw7

#print axioms: 8 theorems, no axioms.

78.273 Factorisation in \(\mathbb Z[(1+\sqrt {-7})/2]\) above a split prime

An element of prime norm is prime, and the \(H_7\)-elements of norm \(p^k\) are the \(\pi ^j\bar \pi ^l\).

\[ N(\alpha )=p^k,\ \alpha \in H_7\ \Rightarrow \ \alpha =\pi ^j\bar \pi ^{\,l}. \]

lang/Prime7.lean

/-
  lang/Prime7 — T48/T50, cycle 643: ℤ[(1+√−7)/2] above a split prime: π of prime norm is prime, N(α) = p^k ⇒ α = uπ^jπ̄^l, and the
  H7-elements of norm p^k are the π^j π̄^l (H7 conjugation-stable).  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult7

namespace Prime7

open FTAChain DetChain LibInt Euc7 Prim7 Uniq7 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Ring7.ring7 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Ring7.ring7 LibInt.intR).mul u (cj u) = (Ring7.ring7 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Ring7.ring7 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists _ π x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Ring7.ring7 LibInt.intR).mul (cj d) X) ((Ring7.ring7 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (cj d) X) π) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (cj d) Y) x)
          = (Ring7.ring7 LibInt.intR).mul (cj d) ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul X π) ((Ring7.ring7 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring7.ring7 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Ring7.ring7 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring7.ring7 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Ring7.ring7 LibInt.intR).mul d (Ring7.ring7 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Ring7.ring7 LibInt.intR).mul x y) = (Ring7.ring7 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2) + (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * (y.1 + y.2) + -(-x.2 * -y.2 + -x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * -y.2 + -x.2 * (y.1 + y.2) + -x.2 * -y.2
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show z.1 + z.2 + -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Ring7.ring7 LibInt.intR).mul u ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) (pw (Ring7.ring7 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Ring7.ring7 LibInt.intR).mul α ((Ring7.ring7 LibInt.intR).mul (Ring7.ring7 LibInt.intR).one (Ring7.ring7 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Ring7.ring7 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Ring7.ring7 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Ring7.ring7 LibInt.intR).mul π ((Ring7.ring7 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring7.ring7 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Ring7.ring7 LibInt.intR).mul u ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Ring7.ring7 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Ring7.ring7 LibInt.intR).mul u ((Ring7.ring7 LibInt.intR).mul _ ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim7_one : Prim7 (Ring7.ring7 LibInt.intR).one := ⟨0, Or.inl rfl⟩

theorem prim7_pow (z : Int × Int) (h : Prim7 z) : ∀ j, Prim7 (pw (Ring7.ring7 LibInt.intR) z j)
  | 0 => prim7_one
  | j + 1 => prim7_mul _ _ (prim7_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim7 α) :
    ∃ j l, j + l = k ∧ α = (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) (pw (Ring7.ring7 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim7_mul _ _ (prim7_pow π pπ j) (prim7_pow (cj π) (prim7_cj π pπ) l)
  have u1 := unit_one7 u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Prime7

#print axioms Prime7.nrm_mul
#print axioms Prime7.unit_inv
#print axioms Prime7.eprime_div
#print axioms Prime7.cj_mul
#print axioms Prime7.cj_cj
#print axioms Prime7.nrm_cj
#print axioms Prime7.nrm_one_eq
#print axioms Prime7.factor3
#print axioms Prime7.prim7_one
#print axioms Prime7.prim7_pow
#print axioms Prime7.primary_only3

#print axioms: 11 theorems, no axioms.

78.274 49a: the Hecke recursion at split primes

For every multiplicative \(W\) the coefficient at \(p^k\) is a sum over \(\pi ^j\bar \pi ^l\) and obeys the Hecke recursion.

\[ c_W(p^{k+2})=(W(\pi )+W(\bar \pi ))\,c_W(p^{k+1})-W(\pi )W(\bar \pi )\,c_W(p^k). \]

lang/HeckePrime7.lean

/-
  lang/HeckePrime7 — T48/T50, cycle 644: **49a's Hecke character at split prime powers**: π ∈ H7 of prime norm p, W multiplicative:
  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l and the Hecke recursion.  (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Prime7
import lang.HeckePrimeW

namespace HeckePrime7

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc7 Prim7 Uniq7 Prime7 HeckeMult7

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Ring7.ring7 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) A = (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) B) : A = B := by
  have h0 : (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) ((Ring7.ring7 LibInt.intR).add A ((Ring7.ring7 LibInt.intR).neg B)) = (Ring7.ring7 LibInt.intR).zero := by
    rw [show (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) ((Ring7.ring7 LibInt.intR).add A ((Ring7.ring7 LibInt.intR).neg B)) = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) A) ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := Cop7.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).add A ((Ring7.ring7 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one7 c π (nrm_one_eq c nc) pπ (by rw [(Ring7.ring7 LibInt.intR).mul_comm, ← hc]; exact prim7_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + a * b + 2 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) :
    ∀ d, Dvd' π (pw (Ring7.ring7 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) (j d l' : Nat)
    (h : (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) (pw (Ring7.ring7 LibInt.intR) (cj π) (l' + 1 + d)) = (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π (j + 1 + d)) (pw (Ring7.ring7 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Ring7.ring7 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Ring7.ring7 LibInt.intR) π j (1 + d)] at h
  have h' : (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) (cj π) l') (pw (Ring7.ring7 LibInt.intR) (cj π) (1 + d)))
      = (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) (cj π) l') (pw (Ring7.ring7 LibInt.intR) π (1 + d))) := by
    calc _ = (Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) (pw (Ring7.ring7 LibInt.intR) π (1 + d))) (pw (Ring7.ring7 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Ring7.ring7 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j) (pw (Ring7.ring7 LibInt.intR) (cj π) l) = (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π j') (pw (Ring7.ring7 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) π x.1) (pw (Ring7.ring7 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD7 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim7_mul _ _ (prim7_pow π pπ _) (prim7_pow (cj π) (prim7_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) (k : Nat) :
    cw7 W ((P + 1 + 1) ^ k) = sumK (Ring7.ring7 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I7 ((P + 1 + 1) ^ k) z = sumK (Ring7.ring7 LibInt.intR) (fun x => delta (Ring7.ring7 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD7 ((P + 1 + 1) ^ k) z
    · rw [I7_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Ring7.ring7 LibInt.intR) k _ (fun x => delta (Ring7.ring7 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Ring7.ring7 LibInt.intR) k j l hjl]
    · rw [I7_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Ring7.ring7 LibInt.intR) k _ (fun _ => (Ring7.ring7 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Ring7.ring7 LibInt.intR) _).symm
  unfold cw7
  rw [sumK_ext (Ring7.ring7 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Ring7.ring7 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le7 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Ring7.ring7 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring7.ring7 LibInt.intR).one = (Ring7.ring7 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Ring7.ring7 LibInt.intR) z j) = pw (Ring7.ring7 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) z j) z) = (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring7.ring7 LibInt.intR).one = (Ring7.ring7 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) (k : Nat) :
    cw7 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Ring7.ring7 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring7.ring7 LibInt.intR).one = (Ring7.ring7 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π) (k : Nat) :
    cw7 W ((P + 1 + 1) ^ (k + 2)) = (Ring7.ring7 LibInt.intR).add
      ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).add (W π) (W (cj π))) (cw7 W ((P + 1 + 1) ^ (k + 1))))
      ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (W π) (W (cj π))) (cw7 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime7

#print axioms HeckePrime7.pow_ne0
#print axioms HeckePrime7.nrm_pw
#print axioms HeckePrime7.npow_add
#print axioms HeckePrime7.en_ne
#print axioms HeckePrime7.cancel_pw3
#print axioms HeckePrime7.no_assoc3
#print axioms HeckePrime7.no_div3
#print axioms HeckePrime7.distinct_lt3
#print axioms HeckePrime7.distinct3
#print axioms HeckePrime7.w3_PD
#print axioms HeckePrime7.cw3_pk
#print axioms HeckePrime7.W_pow3
#print axioms HeckePrime7.cw3_pk'
#print axioms HeckePrime7.cw3_rec

#print axioms: 14 theorems, no axioms.

78.275 49a: the Hecke character at inert primes

A prime \(p\equiv 3,5,6\) mod \(7\) stays prime; the only \(H_7\)-element of norm \(p^{2k}\) is \((-p)^k\).

\[ c_W(p^{2k})=W((-p)^k),\qquad c_W(p^{2k+1})=0. \]

lang/Inert7.lean

/-
  lang/Inert7 — T48/T50, cycle 645: **49a's Hecke character at inert primes** p ≡ 3, 5, 6 (mod 7):
    en_mod7        N(a + bθ) ≡ (a + 4b)² (mod 7) (8N = (a + 4b)² + 7a²) is never ≡ 3, 5, 6
    cw7_zero       c_W(n) = 0 for n ≡ 3, 5, 6 (mod 7)
    inert_div, factor_inert, prim_inert   p stays prime; the H7-elements of norm p^(2k) are (−p)^k
    cw7_even, cw7_odd                     c_W(p^(2k)) = W((−p)^k), c_W(p^(2k+1)) = 0
  (Port of Sq2Inert8.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime7

namespace Inert7

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc7 Prim7 Uniq7 Prime7 HeckeMult7

variable (P : Nat)

theorem en_mod7 (z : Int × Int) (k : Int) (h : en z = 7 * k + 3 ∨ en z = 7 * k + 5 ∨ en z = 7 * k + 6) : False := by
  obtain ⟨a, b⟩ := z
  have hn : en (a, b) = a * a + a * b + 2 * (b * b) := rfl
  obtain ⟨q, r, hq, hr⟩ := GaussEuclid.rnd (a + 4 * b) 6
  have hq' : a + 4 * b = 7 * q + r := by rw [hq]; show q * 7 + r = 7 * q + r; lib_int
  have key : en (a, b) = r * r + 7 * (7 * q * q + 2 * q * r + a * a + -en (a, b)) := by
    have e8 : 8 * en (a, b) = (a + 4 * b) * (a + 4 * b) + 7 * (a * a) := by rw [hn]; lib_int
    rw [hq'] at e8
    calc en (a, b) = 8 * en (a, b) + -(7 * en (a, b)) := by lib_int
      _ = (7 * q + r) * (7 * q + r) + 7 * (a * a) + -(7 * en (a, b)) := by rw [e8]
      _ = _ := by lib_int
  generalize en (a, b) = N at key h
  generalize 7 * q * q + 2 * q * r + a * a + -N = X at key
  have cases_r : r = 0 ∨ r = 1 ∨ r = 2 ∨ r = 3 ∨ r = -1 ∨ r = -2 ∨ r = -3 := by
    cases r with
    | ofNat t =>
      match t, hr with
      | 0, _ => exact Or.inl rfl
      | 1, _ => exact Or.inr (Or.inl rfl)
      | 2, _ => exact Or.inr (Or.inr (Or.inl rfl))
      | 3, _ => exact Or.inr (Or.inr (Or.inr (Or.inl rfl)))
      | t + 4, hr' =>
        exfalso
        have : 2 * (t + 4) ≤ 7 := hr'
        rw [show 2 * (t + 4) = 2 * t + 1 + 7 by lib_nat] at this
        exact Nat.not_succ_le_self 7 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 8) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
    | negSucc t =>
      match t, hr with
      | 0, _ => exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))
      | 1, _ => exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl)))))
      | 2, _ => exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl)))))
      | t + 3, hr' =>
        exfalso
        have : 2 * (t + 4) ≤ 7 := hr'
        rw [show 2 * (t + 4) = 2 * t + 1 + 7 by lib_nat] at this
        exact Nat.not_succ_le_self 7 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 8) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  rcases cases_r with e | e | e | e | e | e | e <;> rw [e] at key <;> rcases h with h | h | h
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 3 2 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 3) + -(0 * 0 + 7 * X) + 4 + -(-3) + (0 * 0 + -3) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 1 4 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 5) + -(0 * 0 + 7 * X) + 2 + -(-5) + (0 * 0 + -5) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 0 5 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 6) + -(0 * 0 + 7 * X) + 1 + -(-6) + (0 * 0 + -6) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 4 1 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 3) + -(1 * 1 + 7 * X) + 5 + -(-2) + (1 * 1 + -3) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 2 3 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 5) + -(1 * 1 + 7 * X) + 3 + -(-4) + (1 * 1 + -5) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 1 4 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 6) + -(1 * 1 + 7 * X) + 2 + -(-5) + (1 * 1 + -6) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 0 5 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 3) + -(2 * 2 + 7 * X) + 1 + -(1) + (2 * 2 + -3) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 5 0 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 5) + -(2 * 2 + 7 * X) + 6 + -(-1) + (2 * 2 + -5) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 4 1 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 6) + -(2 * 2 + 7 * X) + 5 + -(-2) + (2 * 2 + -6) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 5 0 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 3) + -(3 * 3 + 7 * X) + 6 + -(6) + (3 * 3 + -3) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 3 2 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 5) + -(3 * 3 + 7 * X) + 4 + -(4) + (3 * 3 + -5) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 2 3 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 6) + -(3 * 3 + 7 * X) + 3 + -(3) + (3 * 3 + -6) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 4 1 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 3) + -(-1 * -1 + 7 * X) + 5 + -(-2) + (-1 * -1 + -3) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 2 3 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 5) + -(-1 * -1 + 7 * X) + 3 + -(-4) + (-1 * -1 + -5) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 1 4 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 6) + -(-1 * -1 + 7 * X) + 2 + -(-5) + (-1 * -1 + -6) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 0 5 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 3) + -(-2 * -2 + 7 * X) + 1 + -(1) + (-2 * -2 + -3) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 5 0 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 5) + -(-2 * -2 + 7 * X) + 6 + -(-1) + (-2 * -2 + -5) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(-1)) 4 1 (by calc (7 : Int) * (k + -X + -(-1)) = (7 * k + 6) + -(-2 * -2 + 7 * X) + 5 + -(-2) + (-2 * -2 + -6) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 5 0 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 3) + -(-3 * -3 + 7 * X) + 6 + -(6) + (-3 * -3 + -3) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 3 2 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 5) + -(-3 * -3 + 7 * X) + 4 + -(4) + (-3 * -3 + -5) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 6 (k + -X + -(0)) 2 3 (by calc (7 : Int) * (k + -X + -(0)) = (7 * k + 6) + -(-3 * -3 + 7 * X) + 3 + -(3) + (-3 * -3 + -6) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl

/-- **c_W(n) = 0 for n ≡ 3, 5, 6 (mod 7).** -/
theorem cw7_zero (W : Int × Int → Int × Int) (n : Nat) (k : Int) (hn : Int.ofNat n = 7 * k + 3 ∨ Int.ofNat n = 7 * k + 5 ∨ Int.ofNat n = 7 * k + 6) : cw7 W n = (Ring7.ring7 LibInt.intR).zero := by
  unfold cw7
  rw [sumK_ext (Ring7.ring7 LibInt.intR) _ (fun _ => (Ring7.ring7 LibInt.intR).zero) (fun z => by
    rw [I7_neg _ _ (fun h => en_mod7 z k (by rw [h.1]; exact hn))]; lib_ring)]
  exact zsum7 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + Int.ofNat (P + 1 + 1) * 0 + 2 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + 0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (z : Int × Int) (h : nrm z = P + 1 + 1) : False := by
  obtain ⟨K, hK⟩ := hc
  exact en_mod7 z K (by rw [← nrm_cast, h]; exact hK)

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (x y : Int × Int)
    (h : Dvd' (Pi P) ((Ring7.ring7 LibInt.intR).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists _ (Pi P) x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Ring7.ring7 LibInt.intR).mul (cj d) X) ((Ring7.ring7 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (cj d) X) (Pi P)) ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (cj d) Y) x)
          = (Ring7.ring7 LibInt.intR).mul (cj d) ((Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul X (Pi P)) ((Ring7.ring7 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring7.ring7 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Ring7.ring7 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring7.ring7 LibInt.intR).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Ring7.ring7 LibInt.intR).mul d (Ring7.ring7 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Ring7.ring7 LibInt.intR).mul u (pw (Ring7.ring7 LibInt.intR) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Ring7.ring7 LibInt.intR).mul α (Ring7.ring7 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Ring7.ring7 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring7.ring7 LibInt.intR).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Ring7.ring7 LibInt.intR).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Ring7.ring7 LibInt.intR).mul u ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) : Prim7 (nPi P) := by
  obtain ⟨K, hK | hK | hK⟩ := hc
  · exact ⟨-K + -1, Or.inr (Or.inr (by show -Int.ofNat (P + 1 + 1) + 4 * 0 = _; rw [hK]; lib_int))⟩
  · exact ⟨-K + -1, Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 4 * 0 = _; rw [hK]; lib_int))⟩
  · exact ⟨-K + -1, Or.inl (by show -Int.ofNat (P + 1 + 1) + 4 * 0 = _; rw [hK]; lib_int)⟩

theorem pw_neg (k : Nat) : pw (Ring7.ring7 LibInt.intR) (Pi P) k =
    (Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) ((Ring7.ring7 LibInt.intR).neg (Ring7.ring7 LibInt.intR).one) k) (pw (Ring7.ring7 LibInt.intR) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -(-0 * 0 + -0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1) + -0 * 0; lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Ring7.ring7 LibInt.intR) ((Ring7.ring7 LibInt.intR).neg (Ring7.ring7 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Ring7.ring7 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (k : Nat) (α : Int × Int)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim7 α) : α = pw (Ring7.ring7 LibInt.intR) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Ring7.ring7 LibInt.intR).mul u (pw (Ring7.ring7 LibInt.intR) ((Ring7.ring7 LibInt.intR).neg (Ring7.ring7 LibInt.intR).one) k) = v
  have he' : α = (Ring7.ring7 LibInt.intR).mul v (pw (Ring7.ring7 LibInt.intR) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one7 v _ (nrm_one_eq v hv1) (prim7_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -Int.ofNat (P + 1 + 1) * 0 + 2 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Ring7.ring7 LibInt.intR) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime7.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw7_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (k : Nat) :
    cw7 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Ring7.ring7 LibInt.intR) (nPi P) k) := by
  have hPD : PD7 ((P + 1 + 1) ^ (2 * k)) (pw (Ring7.ring7 LibInt.intR) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim7_pow _ (prim_nPi P hc) k⟩
  unfold cw7
  rw [sumK_ext (Ring7.ring7 LibInt.intR) _ (fun z => (Ring7.ring7 LibInt.intR).mul (delta (Ring7.ring7 LibInt.intR) z (pw (Ring7.ring7 LibInt.intR) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD7 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I7_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I7_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le7 _ _ hPD.1
  exact HeckeMult.pick_box (Ring7.ring7 LibInt.intR) W _ _ hb.1 hb.2

theorem sq7 (Y L : Int) (hL : Y = 7 * L + 3 ∨ Y = 7 * L + 5 ∨ Y = 7 * L + 6) : ∃ M : Int, Y * Y = 7 * M + 1 ∨ Y * Y = 7 * M + 2 ∨ Y * Y = 7 * M + 4 := by
  rcases hL with hL | hL | hL
  · exact ⟨(1) + (6) * L + (7) * L * L, Or.inr (Or.inl (by rw [hL]; lib_int))⟩
  · exact ⟨(3) + (10) * L + (7) * L * L, Or.inr (Or.inr (by rw [hL]; lib_int))⟩
  · exact ⟨(5) + (12) * L + (7) * L * L, Or.inl (by rw [hL]; lib_int)⟩

theorem step7 (X Y K L : Int) (hK : X = 7 * K + 3 ∨ X = 7 * K + 5 ∨ X = 7 * K + 6) (hL : Y = 7 * L + 3 ∨ Y = 7 * L + 5 ∨ Y = 7 * L + 6) :
    ∃ W : Int, X * Y * Y = 7 * W + 3 ∨ X * Y * Y = 7 * W + 5 ∨ X * Y * Y = 7 * W + 6 := by
  obtain ⟨M, hM⟩ := sq7 Y L hL
  have e : X * Y * Y = X * (Y * Y) := by lib_int
  rw [e]
  rcases hK with hK | hK | hK <;> rcases hM with hM | hM | hM <;> rw [hK, hM]
  · exact ⟨K + (7) * K * M + (3) * M, Or.inl (by lib_int)⟩
  · exact ⟨(2) * K + (7) * K * M + (3) * M, Or.inr (Or.inr (by lib_int))⟩
  · exact ⟨(1) + (4) * K + (7) * K * M + (3) * M, Or.inr (Or.inl (by lib_int))⟩
  · exact ⟨K + (7) * K * M + (5) * M, Or.inr (Or.inl (by lib_int))⟩
  · exact ⟨(1) + (2) * K + (7) * K * M + (5) * M, Or.inl (by lib_int)⟩
  · exact ⟨(2) + (4) * K + (7) * K * M + (5) * M, Or.inr (Or.inr (by lib_int))⟩
  · exact ⟨K + (7) * K * M + (6) * M, Or.inr (Or.inr (by lib_int))⟩
  · exact ⟨(1) + (2) * K + (7) * K * M + (6) * M, Or.inr (Or.inl (by lib_int))⟩
  · exact ⟨(3) + (4) * K + (7) * K * M + (6) * M, Or.inl (by lib_int)⟩

theorem pow_odd (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (k : Nat) : ∃ K : Int, Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 7 * K + 3 ∨ Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 7 * K + 5 ∨ Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 7 * K + 6 := by
  obtain ⟨L, hL⟩ := hc
  induction k with
  | zero => exact ⟨L, by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hL⟩
  | succ k ih =>
    obtain ⟨K, hK⟩ := ih
    have e : Int.ofNat ((P + 1 + 1) ^ (2 * (k + 1) + 1)) = Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) := by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; rfl
    rw [e]
    exact step7 _ _ K L hK hL

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw7_odd (W : Int × Int → Int × Int) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (k : Nat) : cw7 W ((P + 1 + 1) ^ (2 * k + 1)) = (Ring7.ring7 LibInt.intR).zero := by
  obtain ⟨K, hK⟩ := pow_odd P hc k
  exact cw7_zero W _ K hK

end Inert7

#print axioms Inert7.en_mod7
#print axioms Inert7.cw7_zero
#print axioms Inert7.en_Pi
#print axioms Inert7.nrm_Pi
#print axioms Inert7.cj_Pi
#print axioms Inert7.not_p
#print axioms Inert7.dvd_conv
#print axioms Inert7.split_sq
#print axioms Inert7.inert_div
#print axioms Inert7.nat_pow2
#print axioms Inert7.factor_inert
#print axioms Inert7.prim_nPi
#print axioms Inert7.pw_neg
#print axioms Inert7.nrm_neg_one_pw
#print axioms Inert7.prim_inert
#print axioms Inert7.nrm_nPi
#print axioms Inert7.nrm_nPi_pw
#print axioms Inert7.cw7_even
#print axioms Inert7.sq7
#print axioms Inert7.step7
#print axioms Inert7.pow_odd
#print axioms Inert7.cw7_odd

#print axioms: 22 theorems, no axioms.

78.276 The ring \(\mathbb Z[(1+\sqrt {-11})/2]\)

Pairs \(a+b\theta \) with \(\theta ^2=\theta -3\) form a commutative ring over any commutative ring.

\[ (a+b\theta )(c+d\theta )=(ac-3bd)+(ad+bc+bd)\theta . \]

lang/Ring11.lean

/-
  lang/Ring11 — T48/T50, cycle 646: the ring K[θ] = K[x]/(x² − x + 3) over any commutative ring K (for K = ℤ: the ring of integers
  ℤ[(1 + √−11)/2] of ℚ(√−11)).  (a + bθ)(c + dθ) = (ac − 3bd) + (ad + bc + bd)θ.
    r11mul, r11_assoc, r11_comm, r11_one, r11_distrib, ring11
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lib.RingTac

namespace Ring11

variable {R : Type} (K : DetChain.CRing R)

def r11mul (x y : R × R) : R × R :=
  ((K.add (K.mul x.1 y.1) (K.neg (K.add (K.add (K.mul x.2 y.2) (K.mul x.2 y.2)) (K.mul x.2 y.2)))), (K.add (K.add (K.mul x.1 y.2) (K.mul x.2 y.1)) (K.mul x.2 y.2)))

theorem r11_assoc (a b e : R × R) : r11mul K (r11mul K a b) e = r11mul K a (r11mul K b e) := by
  apply Prod.ext
  · show (K.add (K.mul (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)))) e.1) (K.neg (K.add (K.add (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.2) (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.2)) (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.2)))) = (K.add (K.mul a.1 (K.  -- (+346 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)))) e.2) (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.1)) (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.2)) = (K.add (K.add (K.mul a.1 (K.add (K.add (K.mul b.1 e.2) (K.mul b.2 e.1)) (K.mul b.2 e.2))) (K.mul a.2 (K.add (K.mu  -- (+162 chars; full line in the Zenodo source)
    lib_ring

theorem r11_comm (a b : R × R) : r11mul K a b = r11mul K b a := by
  apply Prod.ext
  · show (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)))) = (K.add (K.mul b.1 a.1) (K.neg (K.add (K.add (K.mul b.2 a.2) (K.mul b.2 a.2)) (K.mul b.2 a.2))))
    lib_ring
  · show (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) = (K.add (K.add (K.mul b.1 a.2) (K.mul b.2 a.1)) (K.mul b.2 a.2))
    lib_ring

theorem r11_one (a : R × R) : r11mul K (K.one, K.zero) a = a := by
  apply Prod.ext
  · show (K.add (K.mul K.one a.1) (K.neg (K.add (K.add (K.mul K.zero a.2) (K.mul K.zero a.2)) (K.mul K.zero a.2)))) = a.1
    lib_ring
  · show (K.add (K.add (K.mul K.one a.2) (K.mul K.zero a.1)) (K.mul K.zero a.2)) = a.2
    lib_ring

theorem r11_distrib (a b e : R × R) :
    r11mul K a (K.add b.1 e.1, K.add b.2 e.2) = (K.add (r11mul K a b).1 (r11mul K a e).1, K.add (r11mul K a b).2 (r11mul K a e).2) := by
  apply Prod.ext
  · show (K.add (K.mul a.1 (K.add b.1 e.1)) (K.neg (K.add (K.add (K.mul a.2 (K.add b.2 e.2)) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))))) = (K.add (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)))) (K.add (K.mul a.1 e.1) (K.neg (K.add (K.add (K.mul a.2 e.2) (K.mul a.2 e.2)) (K.mul a.2 e.2)))))
    lib_ring
  · show (K.add (K.add (K.mul a.1 (K.add b.2 e.2)) (K.mul a.2 (K.add b.1 e.1))) (K.mul a.2 (K.add b.2 e.2))) = (K.add (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) (K.add (K.add (K.mul a.1 e.2) (K.mul a.2 e.1)) (K.mul a.2 e.2)))
    lib_ring

/-- **K[θ], θ² = θ − 3, as a commutative ring.** -/
def ring11 : DetChain.CRing (R × R) where
  add := fun x y => (K.add x.1 y.1, K.add x.2 y.2)
  mul := r11mul K
  neg := fun x => (K.neg x.1, K.neg x.2)
  zero := (K.zero, K.zero)
  one := (K.one, K.zero)
  add_assoc := fun a b c => by
    apply Prod.ext
    · exact K.add_assoc a.1 b.1 c.1
    · exact K.add_assoc a.2 b.2 c.2
  add_comm := fun a b => by
    apply Prod.ext
    · exact K.add_comm a.1 b.1
    · exact K.add_comm a.2 b.2
  zero_add := fun a => by
    apply Prod.ext
    · exact K.zero_add a.1
    · exact K.zero_add a.2
  add_neg := fun a => by
    apply Prod.ext
    · exact K.add_neg a.1
    · exact K.add_neg a.2
  mul_assoc := r11_assoc K
  mul_comm := r11_comm K
  one_mul := r11_one K
  distrib := r11_distrib K

end Ring11

#print axioms Ring11.r11_assoc
#print axioms Ring11.r11_comm
#print axioms Ring11.r11_one
#print axioms Ring11.r11_distrib
#print axioms Ring11.ring11

#print axioms: 5 theorems, no axioms.

78.277 \(\mathbb Z[(1+\sqrt {-11})/2]\) is Euclidean

Division with remainder by rounding \(b\) and then \(2a+b\); \(4N=(2a+b)^2+11b^2\).

\[ \beta \neq 0\ \Rightarrow \ \exists q,\ N(\alpha -q\beta )<N(\beta ). \]

lang/Euc11.lean

/-
  lang/Euc11 — T48/T50, cycle 647: ℤ[θ], θ = (1 + √−11)/2, is Euclidean.
  N(a + bθ) = a² + ab + 3b², 4N = (2a + b)² + 11b², conjugate (a + b) − bθ:
    en_rep, nrm_cast, en_mul, en_cj, four_nrm      norm facts (4·nrm = |2a + b|² + 11|b|² in ℕ)
    edivr          β ≠ 0: α = qβ + ρ, nrm ρ < nrm β  — two-step rounding: r₂ by N, then 2r₁ + r₂ by 2N, 16N(r) ≤ 15N²
    egcd_exists, eeuclid
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Ring11
import lang.GaussEuclid
import lang.W36Mul

namespace Euc11

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + z.1 * z.2 + 3 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Ring11.ring11 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + 3 * (B * B) := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_trans (Nat.le_add_left _ (A * A)) (Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_succ_of_le (Nat.le_succ 1)))) _))
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + 3 * (Int.ofNat B * Int.ofNat B) := congrArg Int.ofNat hc
  have opp : en z = Int.ofNat A * Int.ofNat A + 3 * (Int.ofNat B * Int.ofNat B) + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨c, by rw [h, ← e]; lib_int⟩
  have same : en z = Int.ofNat A * Int.ofNat A + 3 * (Int.ofNat B * Int.ofNat B) + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨A * A + 3 * (B * B) + A * B, h⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem four_nrm (z : Int × Int) : 4 * nrm z = Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 11 * (Int.natAbs z.2 * Int.natAbs z.2) := by
  apply Int.ofNat.inj
  show 4 * Int.ofNat (nrm z) = Int.ofNat (Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2)) + 11 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2)
  rw [nrm_cast, GaussEuclid.nas, GaussEuclid.nas]; unfold en; lib_int

theorem mul_cj (z : Int × Int) : (Ring11.ring11 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + z.2) + -(z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 3 * (z.2 * z.2)
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + z.2) + z.2 * -z.2 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Ring11.ring11 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) + (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)
      + 3 * ((x.1 * y.2 + x.2 * y.1 + x.2 * y.2) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)) =
    (x.1 * x.1 + x.1 * x.2 + 3 * (x.2 * x.2)) * (y.1 * y.1 + y.1 * y.2 + 3 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + z.2) * (z.1 + z.2) + (z.1 + z.2) * -z.2 + 3 * (-z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 3 * (z.2 * z.2)
  lib_int

/-- **Division with remainder in ℤ[θ].** -/
theorem edivr (α β : Int × Int) (M : Nat) (hβ : nrm β = M + 1) :
    ∃ q, nrm ((Ring11.ring11 LibInt.intR).add α ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul q β))) < M + 1 := by
  have hN : en β = Int.ofNat (M + 1) := by rw [← nrm_cast, hβ]
  match GaussEuclid.rnd ((Ring11.ring11 LibInt.intR).mul α (cj β)).2 M with
  | ⟨q2, r2, h2, b2⟩ =>
  match GaussEuclid.rnd (2 * ((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + r2) (2 * M + 1) with
  | ⟨q1, t, h1, b1⟩ =>
    refine ⟨(q1, q2), ?_⟩
    generalize hρ : (Ring11.ring11 LibInt.intR).add α ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul (q1, q2) β)) = ρ
    have hρc : (Ring11.ring11 LibInt.intR).mul ρ (cj β) = (((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1)), r2) := by
      rw [← hρ]
      have e : (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).add α ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul (q1, q2) β))) (cj β) = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul α (cj β)) ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul (q1, q2) ((Ring11.ring11 LibInt.intR).mul β (cj β)))) := by lib_ring
      rw [e, mul_cj, hN]
      apply Prod.ext
      · show ((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1) + -(q2 * 0 + q2 * 0 + q2 * 0)) = ((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1))
        lib_int
      · show ((Ring11.ring11 LibInt.intR).mul α (cj β)).2 + -(q1 * 0 + q2 * Int.ofNat (M + 1) + q2 * 0) = r2
        rw [h2]; lib_int
    generalize hr1 : ((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1)) = r1 at hρc
    have ht : 2 * r1 + r2 = t := by
      rw [← hr1]
      calc 2 * (((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + -(q1 * Int.ofNat (M + 1))) + r2 = (2 * ((Ring11.ring11 LibInt.intR).mul α (cj β)).1 + r2) + -(q1 * Int.ofNat (2 * M + 1 + 1)) := by
            show _ = _ + -(q1 * (2 * Int.ofNat M + 1 + 1)); show 2 * (_ + -(q1 * (Int.ofNat M + 1))) + r2 = _; lib_int
        _ = t := by rw [h1]; lib_int
    have hn : en ρ * Int.ofNat (M + 1) = en (r1, r2) := by
      rw [← hN, ← en_cj β, ← en_mul, hρc]
    have hnN : nrm ρ * (M + 1) = nrm (r1, r2) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm ρ) * Int.ofNat (M + 1) = Int.ofNat (nrm (r1, r2))
      rw [nrm_cast, nrm_cast]; exact hn
    have h4 := four_nrm (r1, r2)
    have hT : Int.natAbs (2 * r1 + r2) ≤ M + 1 := by
      rw [ht]
      apply Nat.le_of_mul_le_mul_left (show 2 * Int.natAbs t ≤ 2 * (M + 1) from Nat.le_trans b1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
    generalize Int.natAbs (2 * r1 + r2) = T at h4 hT
    have h4' : 4 * nrm (r1, r2) = T * T + 11 * (Int.natAbs r2 * Int.natAbs r2) := h4
    generalize Int.natAbs r2 = B at h4' b2
    have hle : (M + 1) * (16 * nrm ρ) ≤ (M + 1) * (15 * (M + 1)) := by
      calc (M + 1) * (16 * nrm ρ) = 4 * (4 * nrm (r1, r2)) := by rw [← hnN]; lib_nat
        _ = 4 * (T * T) + 11 * ((2 * B) * (2 * B)) := by rw [h4']; lib_nat
        _ ≤ 4 * ((M + 1) * (M + 1)) + 11 * ((M + 1) * (M + 1)) := Nat.add_le_add (Nat.mul_le_mul_left 4 (Nat.mul_le_mul hT hT)) (Nat.mul_le_mul_left 11 (Nat.mul_le_mul b2 b2))
        _ = (M + 1) * (15 * (M + 1)) := by lib_nat
    have h16 := Nat.le_of_mul_le_mul_left hle (Nat.zero_lt_succ M)
    cases Nat.lt_or_ge (nrm ρ) (M + 1) with
    | inl h => exact h
    | inr h =>
      exfalso
      have : 16 * (M + 1) ≤ 15 * (M + 1) := Nat.le_trans (Nat.mul_le_mul_left 16 h) h16
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (show 15 * (M + 1) < 16 * (M + 1) by
        rw [show 16 * (M + 1) = 15 * (M + 1) + (M + 1) by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)) this)

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Ring11.ring11 LibInt.intR).zero := by
  have h4 := four_nrm z
  rw [h] at h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by
    have : 11 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
    cases nat_mul_zero _ _ this with
    | inl e => exact Nat.noConfusion e
    | inr e => exact e
  have hT : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have t0 : 2 * z.1 + z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hT with | inl e => exact e | inr e => exact e)
  have a0 : z.1 = 0 := W36Mul.two_cancel_int z.1 0 (by rw [show 2 * z.1 = 2 * z.1 + z.2 by rw [b0]; lib_int, t0]; rfl)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Ring11.ring11 LibInt.intR).zero) :
    ∃ d x y, d = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x α) ((Ring11.ring11 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Ring11.ring11 LibInt.intR).one, (Ring11.ring11 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Ring11.ring11 LibInt.intR).one, by lib_ring⟩, ⟨(Ring11.ring11 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **gcd as a combination in ℤ[ω].** -/
theorem egcd_exists : ∀ G (α β : Int × Int), nrm β ≤ G →
    ∃ d x y, d = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x α) ((Ring11.ring11 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β
  | 0, α, β, hG => egcd_base α β (nrm_zero β (Nat.le_antisymm hG (Nat.zero_le _)))
  | G + 1, α, β, hG => by
    cases h : nrm β with
    | zero => exact egcd_base α β (nrm_zero β h)
    | succ M =>
        match edivr α β M h with
        | ⟨q, hq⟩ =>
          have hρ : nrm ((Ring11.ring11 LibInt.intR).add α ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul q β))) ≤ G :=
            Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hq (Nat.le_trans (Nat.le_of_eq h.symm) hG))
          match egcd_exists G β _ hρ with
          | ⟨d, x, y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
            refine ⟨d, y, (Ring11.ring11 LibInt.intR).add x ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul y q)), by rw [hd]; lib_ring, ⟨(Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul q c1) c2, ?_⟩, ⟨c1, hc1⟩⟩
            have e : α = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul q β) ((Ring11.ring11 LibInt.intR).add α ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul q β))) := by lib_ring
            rw [e, hc2, hc1]
            lib_ring

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x α) ((Ring11.ring11 LibInt.intR).mul y β) = (Ring11.ring11 LibInt.intR).one) (h : Dvd' α ((Ring11.ring11 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x γ) ((Ring11.ring11 LibInt.intR).mul y c), by
      calc γ = (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x α) ((Ring11.ring11 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul x α) γ) ((Ring11.ring11 LibInt.intR).mul y ((Ring11.ring11 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Ring11.ring11 LibInt.intR).mul α ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x γ) ((Ring11.ring11 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

end Euc11

#print axioms Euc11.sgn
#print axioms Euc11.le_dest
#print axioms Euc11.ab_le
#print axioms Euc11.en_rep
#print axioms Euc11.nrm_cast
#print axioms Euc11.four_nrm
#print axioms Euc11.mul_cj
#print axioms Euc11.en_mul
#print axioms Euc11.en_cj
#print axioms Euc11.edivr
#print axioms Euc11.nrm_zero
#print axioms Euc11.egcd_base
#print axioms Euc11.egcd_exists
#print axioms Euc11.eeuclid

#print axioms: 14 theorems, no axioms.

78.278 Coprime splitting in \(\mathbb Z[(1+\sqrt {-11})/2]\)

An element whose norm is a product of coprime factors splits accordingly.

\[ N(\gamma )=mn,\ sm+tn=1\ \Rightarrow \ \gamma =\alpha \beta ,\ N(\alpha )=m. \]

lang/Cop11.lean

/-
  lang/Cop11 — T48/T50, cycle 648: splitting by coprime norms in ℤ[(1+√−11)/2].

  If N(γ) = mn with sm + tn = 1 (m, n ≥ 1), then γ = αβ with N(α) = m, N(β) = n (N(a + bθ) = a² + ab + 3b²):
  α = gcd(γ, m), e = gcd(γ, n) (Euc11.egcd_exists), αe | γ by Euclid's lemma, γ | αe since mn = γγ̄, so γ = αe·v with v a unit;
  then N(α) | m, N(e) | n and N(α)N(e) = mn give N(α) = m. (Port of GaussCoprime.coprime_split.)
    en_emb, emb_mul, en_dvd, emb_add, en_zero_imp, cancel_left, en_nonneg_one, ecoprime_split
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc11
import lang.GaussCoprime

namespace Cop11

open FTAChain DetChain LibInt Euc11

def emb (m : Int) : Int × Int := (m, 0)

theorem en_emb (m : Int) : en (emb m) = m * m := by show m * m + m * 0 + 3 * (0 * 0) = m * m; lib_int

theorem emb_mul (a b : Int) : (Ring11.ring11 LibInt.intR).mul (emb a) (emb b) = emb (a * b) := by
  apply Prod.ext
  · show a * b + -(0 * 0 + 0 * 0 + 0 * 0) = a * b; lib_int
  · show a * 0 + 0 * b + 0 * 0 = 0; lib_int

theorem en_dvd (d γ : Int × Int) (h : Dvd' d γ) : ∃ k, en γ = en d * k :=
  match h with
  | ⟨c, hc⟩ => ⟨en c, by rw [hc, en_mul]⟩

theorem emb_add (a b : Int) : emb (a + b) = (Ring11.ring11 LibInt.intR).add (emb a) (emb b) := by
  apply Prod.ext
  · rfl
  · show (0 : Int) = 0 + 0; rfl

theorem en_zero_imp (z : Int × Int) (h : en z = 0) : z = (Ring11.ring11 LibInt.intR).zero := by
  apply nrm_zero
  apply Int.ofNat.inj
  rw [nrm_cast]; exact h

theorem cancel_left (γ z : Int × Int) (hγ : en γ ≠ 0) (h : (Ring11.ring11 LibInt.intR).mul γ z = (Ring11.ring11 LibInt.intR).zero) : z = (Ring11.ring11 LibInt.intR).zero := by
  have : en γ * en z = 0 := by rw [← en_mul, h]; rfl
  cases int_noZD _ _ this with
  | inl e => exact absurd e hγ
  | inr e => exact en_zero_imp z e

theorem en_nonneg_one (z : Int × Int) (h : en z = -1) : False := by
  rw [← nrm_cast] at h
  exact Int.noConfusion h

/-- **Coprime splitting**: N(γ) = mn, sm + tn = 1 ⇒ γ = αβ with N(α) = m, N(β) = n. -/
theorem ecoprime_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) :
    ∃ α β, γ = (Ring11.ring11 LibInt.intR).mul α β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match egcd_exists _ γ (emb (Int.ofNat (m + 1))) (Nat.le_refl _), egcd_exists _ γ (emb (Int.ofNat (n + 1))) (Nat.le_refl _) with
  | ⟨d, x, y, hd, ⟨u, hu⟩, ⟨c1, hc1⟩⟩, ⟨e, x', y', he, ⟨u', hu'⟩, ⟨c2, hc2⟩⟩ =>
    have hco : (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (emb t) c2) e) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (emb s) c1) d) = (Ring11.ring11 LibInt.intR).one := by
      have h1 : (Ring11.ring11 LibInt.intR).one = emb (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; rfl
      rw [h1, emb_add, ← emb_mul, ← emb_mul, hc1, hc2]; lib_ring
    -- e | u, so γ = d e v
    match eeuclid e d u _ _ hco ⟨u', by rw [← hu, hu']⟩ with
    | ⟨v, hv⟩ =>
    have hγdev : γ = (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul d e) v := by rw [hu, hv]; lib_ring
    -- γ | d e
    have hmn : (Ring11.ring11 LibInt.intR).mul γ (cj γ) = emb (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by
      rw [mul_cj, hγ]; rfl
    have hW : (Ring11.ring11 LibInt.intR).mul d e = (Ring11.ring11 LibInt.intR).mul γ ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul x x') γ) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring1  -- (+146 chars; full line in the Zenodo source)
      rw [hd, he]
      have : (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul y y') ((Ring11.ring11 LibInt.intR).mul γ (cj γ)) = (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul y y') ((Ring11.ring11 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1)))) := by rw [hmn, emb_mul]
      calc (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x γ) ((Ring11.ring11 LibInt.intR).mul y (emb (Int.ofNat (m + 1))))) ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul x' γ) ((Ring11.ring11 LibInt.intR).mul y' (emb (Int.ofNat (n + 1)))))
          = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul γ ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul x x') γ) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul y x') (emb (In  -- (+20 chars; full line in the Zenodo source)
              ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul y y') ((Ring11.ring11 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1))))) := by lib_ring
        _ = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul γ ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul x x') γ) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul y x') (emb (In  -- (+20 chars; full line in the Zenodo source)
              ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul y y') ((Ring11.ring11 LibInt.intR).mul γ (cj γ))) := by rw [this]
        _ = _ := by lib_ring
    generalize (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul x x') γ) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1)))) ((Ring11.ring  -- (+77 chars; full line in the Zenodo source)
    have hγ0 : en γ ≠ 0 := by rw [hγ]; exact fun e => Nat.noConfusion (Int.ofNat.inj e)
    have hWv : (Ring11.ring11 LibInt.intR).mul W v = (Ring11.ring11 LibInt.intR).one := by
      have h0 : (Ring11.ring11 LibInt.intR).mul γ ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul W v) ((Ring11.ring11 LibInt.intR).neg (Ring11.ring11 LibInt.intR).one)) = (Ring11.ring11 LibInt.intR).zero := by
        rw [show (Ring11.ring11 LibInt.intR).mul γ ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul W v) ((Ring11.ring11 LibInt.intR).neg (Ring11.ring11 LibInt.intR).one)) = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul γ W) v) ((Ring11.ring11 LibInt.intR).neg γ) by lib_ring, ← hW, ← hγdev]; lib_ring
      have := cancel_left γ _ hγ0 h0
      calc (Ring11.ring11 LibInt.intR).mul W v = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul W v) ((Ring11.ring11 LibInt.intR).neg (Ring11.ring11 LibInt.intR).one)) (Ring11.ring11 LibInt.intR).one := by lib_ring
        _ = (Ring11.ring11 LibInt.intR).one := by rw [this]; lib_ring
    have hv1 : en v = 1 := by
      have : en W * en v = 1 := by rw [← en_mul, hWv]; rfl
      cases GaussCoprime.int_unit _ _ (by rw [show en v * en W = en W * en v by lib_int]; exact this) with
      | inl h => exact h
      | inr h => exact absurd h (en_nonneg_one v)
    -- norms
    have hN : en d * en e = Int.ofNat (m + 1) * Int.ofNat (n + 1) := by
      have := congrArg en hγdev
      rw [en_mul, en_mul, hv1, hγ] at this
      rw [show en d * en e = en d * en e * 1 by lib_int, ← this]; rfl
    have hdm : ∃ a, Int.ofNat (m + 1) = en d * a := by
      have k1 : Int.ofNat (m + 1) * Int.ofNat (m + 1) = en d * en c1 := by rw [← en_emb, hc1, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en d * en u := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu, en_mul]
      exact ⟨s * en c1 + t * en u, by
        calc Int.ofNat (m + 1) = Int.ofNat (m + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = s * (Int.ofNat (m + 1) * Int.ofNat (m + 1)) + t * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en d * (s * en c1 + t * en u) := by rw [k1, k2]; lib_int⟩
    have hen' : ∃ b, Int.ofNat (n + 1) = en e * b := by
      have k1 : Int.ofNat (n + 1) * Int.ofNat (n + 1) = en e * en c2 := by rw [← en_emb, hc2, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en e * en u' := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu', en_mul]
      exact ⟨t * en c2 + s * en u', by
        calc Int.ofNat (n + 1) = Int.ofNat (n + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = t * (Int.ofNat (n + 1) * Int.ofNat (n + 1)) + s * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en e * (t * en c2 + s * en u') := by rw [k1, k2]; lib_int⟩
    match hdm, hen' with
    | ⟨a, ha⟩, ⟨b, hb⟩ =>
      have hab : a * b = 1 := by
        have h0 : (en d * en e) * (a * b + -1) = 0 := by
          rw [show (en d * en e) * (a * b + -1) = (en d * a) * (en e * b) + -(en d * en e) by lib_int, ← ha, ← hb, hN]; lib_int
        cases int_noZD _ _ h0 with
        | inl e0 => exact absurd (by rw [hN] at e0; exact e0) (fun e1 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = 0 => Nat.noConfusion (Int.ofNat.inj e1))
        | inr e0 => calc a * b = (a * b + -1) + 1 := by lib_int
            _ = 1 := by rw [e0]; rfl
      have pos : ∀ (k : Nat) (z : Int × Int) (c : Int), Int.ofNat (k + 1) = en z * c → c = 1 ∨ c = -1 → c = 1 := fun k z c h hc => by
        cases hc with
        | inl h1 => exact h1
        | inr h1 =>
          exfalso
          rw [h1, ← nrm_cast] at h
          have : Int.ofNat (k + 1) + Int.ofNat (nrm z) = 0 := by rw [h]; lib_int
          have h2 : k + nrm z + 1 = 0 := by rw [show k + nrm z + 1 = k + 1 + nrm z by lib_nat]; exact Int.ofNat.inj (show Int.ofNat (k + 1 + nrm z) = Int.ofNat 0 from this)
          exact Nat.noConfusion h2
      have a1 := pos m d a ha (GaussCoprime.int_unit a b hab)
      have b1 := pos n e b hb (GaussCoprime.int_unit b a (by rw [show b * a = a * b by lib_int]; exact hab))
      refine ⟨d, (Ring11.ring11 LibInt.intR).mul e v, by rw [hγdev]; lib_ring, by rw [ha, a1]; lib_int, by rw [en_mul, hv1, hb, b1]⟩

end Cop11

#print axioms Cop11.en_emb
#print axioms Cop11.emb_mul
#print axioms Cop11.en_dvd
#print axioms Cop11.emb_add
#print axioms Cop11.en_zero_imp
#print axioms Cop11.cancel_left
#print axioms Cop11.en_nonneg_one
#print axioms Cop11.ecoprime_split

#print axioms: 8 theorems, no axioms.

78.279 The primary class of 121b

The elements whose residue mod \(\sqrt {-11}\) is a nonzero square form a conjugation-stable complement of \(\{\pm 1\}\).

\[ H_{11}=\{a+b\theta :\ a+6b\equiv 1,3,4,5,9\pmod {11}\}. \]

lang/Prim11.lean

/-
  lang/Prim11 — T48/T50, cycle 649: **the primary class of 121b's Hecke character** in ℤ[θ], θ = (1 + √−11)/2:
  ρ(a + bθ) = a + 6b is the reduction mod √−11 (θ ≡ 6); H11 = {ρ ≡ 1, 3, 4, 5, 9 (mod 11)} (the squares).
  (Numerically π ∈ H11 of norm p gives 2a + b = a_p(121b) for split p < 120.)
    prim11_mul, prim11_cj, unit_pm11, normalize11, unit_one11
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Cop11
import lang.Sq2Prim8
import lang.W36Mul

namespace Prim11

open FTAChain DetChain LibInt Euc11

def rho (z : Int × Int) : Int := z.1 + 6 * z.2

def Prim11 (z : Int × Int) : Prop := ∃ e : Int, rho z = 11 * e + 1 ∨ rho z = 11 * e + 3 ∨ rho z = 11 * e + 4 ∨ rho z = 11 * e + 5 ∨ rho z = 11 * e + 9

theorem rho_mul (x y : Int × Int) : rho ((Ring11.ring11 LibInt.intR).mul x y) = rho x * rho y + -(33 * (x.2 * y.2)) := by
  show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + 6 * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + 6 * x.2) * (y.1 + 6 * y.2) + -(33 * (x.2 * y.2))
  lib_int

theorem rho_neg (z : Int × Int) : rho (-z.1, -z.2) = -rho z := by
  show -z.1 + 6 * -z.2 = -(z.1 + 6 * z.2); lib_int

theorem prim11_mul_0 (u v e f w : Int) (hx : u = 11 * e + 1) (hy : v = 11 * f + 1 ∨ v = 11 * f + 3 ∨ v = 11 * f + 4 ∨ v = 11 * f + 5 ∨ v = 11 * f + 9) :
    ∃ E : Int, u * v + -(33 * w) = 11 * E + 1 ∨ u * v + -(33 * w) = 11 * E + 3 ∨ u * v + -(33 * w) = 11 * E + 4 ∨ u * v + -(33 * w) = 11 * E + 5 ∨ u * v + -(33 * w) = 11 * E + 9 := by
  rcases hy with hy | hy | hy | hy | hy <;> rw [hx, hy]
  · exact ⟨e + (11) * e * f + f + (-3) * w, (Or.inl (by lib_int))⟩
  · exact ⟨(3) * e + (11) * e * f + f + (-3) * w, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(4) * e + (11) * e * f + f + (-3) * w, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(5) * e + (11) * e * f + f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(9) * e + (11) * e * f + f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩

theorem prim11_mul_1 (u v e f w : Int) (hx : u = 11 * e + 3) (hy : v = 11 * f + 1 ∨ v = 11 * f + 3 ∨ v = 11 * f + 4 ∨ v = 11 * f + 5 ∨ v = 11 * f + 9) :
    ∃ E : Int, u * v + -(33 * w) = 11 * E + 1 ∨ u * v + -(33 * w) = 11 * E + 3 ∨ u * v + -(33 * w) = 11 * E + 4 ∨ u * v + -(33 * w) = 11 * E + 5 ∨ u * v + -(33 * w) = 11 * E + 9 := by
  rcases hy with hy | hy | hy | hy | hy <;> rw [hx, hy]
  · exact ⟨e + (11) * e * f + (3) * f + (-3) * w, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(3) * e + (11) * e * f + (3) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(1) + (4) * e + (11) * e * f + (3) * f + (-3) * w, (Or.inl (by lib_int))⟩
  · exact ⟨(1) + (5) * e + (11) * e * f + (3) * f + (-3) * w, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(2) + (9) * e + (11) * e * f + (3) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩

theorem prim11_mul_2 (u v e f w : Int) (hx : u = 11 * e + 4) (hy : v = 11 * f + 1 ∨ v = 11 * f + 3 ∨ v = 11 * f + 4 ∨ v = 11 * f + 5 ∨ v = 11 * f + 9) :
    ∃ E : Int, u * v + -(33 * w) = 11 * E + 1 ∨ u * v + -(33 * w) = 11 * E + 3 ∨ u * v + -(33 * w) = 11 * E + 4 ∨ u * v + -(33 * w) = 11 * E + 5 ∨ u * v + -(33 * w) = 11 * E + 9 := by
  rcases hy with hy | hy | hy | hy | hy <;> rw [hx, hy]
  · exact ⟨e + (11) * e * f + (4) * f + (-3) * w, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(1) + (3) * e + (11) * e * f + (4) * f + (-3) * w, (Or.inl (by lib_int))⟩
  · exact ⟨(1) + (4) * e + (11) * e * f + (4) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(1) + (5) * e + (11) * e * f + (4) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(3) + (9) * e + (11) * e * f + (4) * f + (-3) * w, (Or.inr (Or.inl (by lib_int)))⟩

theorem prim11_mul_3 (u v e f w : Int) (hx : u = 11 * e + 5) (hy : v = 11 * f + 1 ∨ v = 11 * f + 3 ∨ v = 11 * f + 4 ∨ v = 11 * f + 5 ∨ v = 11 * f + 9) :
    ∃ E : Int, u * v + -(33 * w) = 11 * E + 1 ∨ u * v + -(33 * w) = 11 * E + 3 ∨ u * v + -(33 * w) = 11 * E + 4 ∨ u * v + -(33 * w) = 11 * E + 5 ∨ u * v + -(33 * w) = 11 * E + 9 := by
  rcases hy with hy | hy | hy | hy | hy <;> rw [hx, hy]
  · exact ⟨e + (11) * e * f + (5) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(1) + (3) * e + (11) * e * f + (5) * f + (-3) * w, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(1) + (4) * e + (11) * e * f + (5) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(2) + (5) * e + (11) * e * f + (5) * f + (-3) * w, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(4) + (9) * e + (11) * e * f + (5) * f + (-3) * w, (Or.inl (by lib_int))⟩

theorem prim11_mul_4 (u v e f w : Int) (hx : u = 11 * e + 9) (hy : v = 11 * f + 1 ∨ v = 11 * f + 3 ∨ v = 11 * f + 4 ∨ v = 11 * f + 5 ∨ v = 11 * f + 9) :
    ∃ E : Int, u * v + -(33 * w) = 11 * E + 1 ∨ u * v + -(33 * w) = 11 * E + 3 ∨ u * v + -(33 * w) = 11 * E + 4 ∨ u * v + -(33 * w) = 11 * E + 5 ∨ u * v + -(33 * w) = 11 * E + 9 := by
  rcases hy with hy | hy | hy | hy | hy <;> rw [hx, hy]
  · exact ⟨e + (11) * e * f + (9) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(2) + (3) * e + (11) * e * f + (9) * f + (-3) * w, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(3) + (4) * e + (11) * e * f + (9) * f + (-3) * w, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(4) + (5) * e + (11) * e * f + (9) * f + (-3) * w, (Or.inl (by lib_int))⟩
  · exact ⟨(7) + (9) * e + (11) * e * f + (9) * f + (-3) * w, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩

theorem prim11_mul (x y : Int × Int) (hx : Prim11 x) (hy : Prim11 y) : Prim11 ((Ring11.ring11 LibInt.intR).mul x y) := by
  obtain ⟨e, hx⟩ := hx
  obtain ⟨f, hy⟩ := hy
  rw [Prim11, rho_mul]
  generalize x.2 * y.2 = w
  rcases hx with hx | hx | hx | hx | hx
  · exact prim11_mul_0 _ _ e f w hx hy
  · exact prim11_mul_1 _ _ e f w hx hy
  · exact prim11_mul_2 _ _ e f w hx hy
  · exact prim11_mul_3 _ _ e f w hx hy
  · exact prim11_mul_4 _ _ e f w hx hy

theorem prim11_cj (z : Int × Int) (hz : Prim11 z) : Prim11 (cj z) := by
  obtain ⟨e, h⟩ := hz
  have hr : rho (cj z) = rho z + -(11 * z.2) := by show z.1 + z.2 + 6 * -z.2 = z.1 + 6 * z.2 + -(11 * z.2); lib_int
  refine ⟨e + -z.2, ?_⟩
  rw [hr]
  rcases h with h | h | h | h | h
  · exact (Or.inl (by rw [h]; lib_int))
  · exact (Or.inr (Or.inl (by rw [h]; lib_int)))
  · exact (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int)))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (by rw [h]; lib_int)))))

theorem unit_pm11 (u : Int × Int) (h : en u = 1) : u = (Ring11.ring11 LibInt.intR).one ∨ u = (-1, 0) := by
  have h4 := four_nrm u
  have hn : nrm u = 1 := Int.ofNat.inj (by rw [nrm_cast, h]; rfl)
  rw [hn] at h4
  have hB : Int.natAbs u.2 * Int.natAbs u.2 = 0 := by
    cases hb : Int.natAbs u.2 * Int.natAbs u.2 with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 11 * (t + 1) = (Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 11 * t + 7) + 4 by lib_nat] at h4
                exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h4))))
  have b0 : u.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at h4
  have hT : Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) = 2 * 2 := h4.symm
  have hT2 : Int.natAbs (2 * u.1 + u.2) = 2 := by
    match hx : Int.natAbs (2 * u.1 + u.2) with
    | 0 => rw [hx] at hT; exact Nat.noConfusion hT
    | 1 => rw [hx] at hT; exact Nat.noConfusion (Nat.succ.inj hT)
    | 2 => rfl
    | t + 3 => rw [hx, show (t + 3) * (t + 3) = (t * t + 6 * t + 5) + 4 by lib_nat] at hT; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hT))))
  rw [b0] at hT2
  obtain ⟨a, b⟩ := u
  show (a, b) = (1, 0) ∨ (a, b) = (-1, 0)
  have b0' : b = 0 := b0
  rcases EisEuclid.sgn (2 * a + 0) with s | s <;> rw [hT2] at s
  · exact Or.inl (Prod.ext (W36Mul.two_cancel_int a 1 (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')
  · exact Or.inr (Prod.ext (W36Mul.two_cancel_int a (-1) (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')

theorem neg_mul11 (z : Int × Int) : (Ring11.ring11 LibInt.intR).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -(0 * z.2 + 0 * z.2 + 0 * z.2) = -z.1; lib_int) (by show -1 * z.2 + 0 * z.1 + 0 * z.2 = -z.2; lib_int)

/-- 11 ∤ N(α) ⇒ ±α ∈ H11. -/
theorem normalize11 (α : Int × Int) (h11 : ∀ k, en α ≠ 11 * k) : ∃ u, en u = 1 ∧ Prim11 ((Ring11.ring11 LibInt.intR).mul u α) := by
  obtain ⟨q, r, hq, hr⟩ := GaussEuclid.rnd (rho α) 10
  have hq' : rho α = 11 * q + r := by rw [hq]; show q * 11 + r = 11 * q + r; lib_int
  have plus : ∀ e, (rho α = 11 * e + 1 ∨ rho α = 11 * e + 3 ∨ rho α = 11 * e + 4 ∨ rho α = 11 * e + 5 ∨ rho α = 11 * e + 9) → ∃ u, en u = 1 ∧ Prim11 ((Ring11.ring11 LibInt.intR).mul u α) :=
    fun e he => ⟨(Ring11.ring11 LibInt.intR).one, rfl, by rw [(Ring11.ring11 LibInt.intR).one_mul]; exact ⟨e, he⟩⟩
  have minus : ∀ e, (-rho α = 11 * e + 1 ∨ -rho α = 11 * e + 3 ∨ -rho α = 11 * e + 4 ∨ -rho α = 11 * e + 5 ∨ -rho α = 11 * e + 9) → ∃ u, en u = 1 ∧ Prim11 ((Ring11.ring11 LibInt.intR).mul u α) :=
    fun e he => ⟨(-1, 0), rfl, by rw [neg_mul11]; exact ⟨e, by rw [rho_neg]; exact he⟩⟩
  cases r with
  | ofNat t =>
    match t, hr with
    | 0, _ =>
      exfalso
      obtain ⟨a, b⟩ := α
      have ha : a = 11 * q + -(6 * b) := by
        have : a + 6 * b = 11 * q + Int.ofNat 0 := hq'
        calc a = (a + 6 * b) + -(6 * b) := by lib_int
          _ = _ := by rw [this]; show 11 * q + 0 + -(6 * b) = _; lib_int
      exact h11 (11 * q * q + -(11 * q * b) + 3 * b * b) (by show a * a + a * b + 3 * (b * b) = _; rw [ha]; lib_int)
    | 1, _ => exact plus q (Or.inl (hq'))
    | 2, _ => exact minus (-q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (by rw [hq']; show -(11 * q + 2) = _; lib_int)))))
    | 3, _ => exact plus q (Or.inr (Or.inl (hq')))
    | 4, _ => exact plus q (Or.inr (Or.inr (Or.inl (hq'))))
    | 5, _ => exact plus q (Or.inr (Or.inr (Or.inr (Or.inl (hq')))))
    | t + 6, hr' =>
      exfalso
      have : 2 * (t + 6) ≤ 11 := hr'
      rw [show 2 * (t + 6) = 2 * t + 1 + 11 by lib_nat] at this
      exact Nat.not_succ_le_self 11 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 12) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  | negSucc t =>
    match t, hr with
    | 0, _ => exact minus (-q) (Or.inl (by rw [hq']; show -(11 * q + -1) = _; lib_int))
    | 1, _ => exact plus (q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (by rw [hq']; show 11 * q + -2 = _; lib_int)))))
    | 2, _ => exact minus (-q) (Or.inr (Or.inl (by rw [hq']; show -(11 * q + -3) = _; lib_int)))
    | 3, _ => exact minus (-q) (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(11 * q + -4) = _; lib_int))))
    | 4, _ => exact minus (-q) (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(11 * q + -5) = _; lib_int)))))
    | t + 5, hr' =>
      exfalso
      have : 2 * (t + 6) ≤ 11 := hr'
      rw [show 2 * (t + 6) = 2 * t + 1 + 11 by lib_nat] at this
      exact Nat.not_succ_le_self 11 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 12) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))

/-- **A unit taking an H11-element to an H11-element is 1.** -/
theorem unit_one11 (u α : Int × Int) (hu : en u = 1) (pα : Prim11 α) (pu : Prim11 ((Ring11.ring11 LibInt.intR).mul u α)) : u = (Ring11.ring11 LibInt.intR).one := by
  rcases unit_pm11 u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, neg_mul11] at pu
  obtain ⟨e, h1⟩ := pα
  obtain ⟨f, h2⟩ := pu
  rw [rho_neg] at h2
  generalize rho α = x at h1 h2
  rcases h1 with h1 | h1 | h1 | h1 | h1 <;> rcases h2 with h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 8 1 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 1) + (11 * f + 1) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 6 3 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 1) + (11 * f + 3) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 5 4 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 1) + (11 * f + 4) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 4 5 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 1) + (11 * f + 5) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 0 9 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 1) + (11 * f + 9) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 6 3 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 3) + (11 * f + 1) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 4 5 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 3) + (11 * f + 3) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 3 6 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 3) + (11 * f + 4) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 2 7 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 3) + (11 * f + 5) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 9 0 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 3) + (11 * f + 9) + 10 := by lib_int
        _ = x + -x + 10 := by rw [← h1, ← h2]
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 5 4 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 4) + (11 * f + 1) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 3 6 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 4) + (11 * f + 3) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 2 7 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 4) + (11 * f + 4) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 1 8 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 4) + (11 * f + 5) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 8 1 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 4) + (11 * f + 9) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 4 5 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 5) + (11 * f + 1) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 2 7 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 5) + (11 * f + 3) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 1 8 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 5) + (11 * f + 4) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 0 9 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 5) + (11 * f + 5) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 7 2 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 5) + (11 * f + 9) + 8 := by lib_int
        _ = x + -x + 8 := by rw [← h1, ← h2]
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-1)) 0 9 (by calc (11 : Int) * (e + f + -(-1)) = (11 * e + 9) + (11 * f + 1) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 9 0 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 9) + (11 * f + 3) + 10 := by lib_int
        _ = x + -x + 10 := by rw [← h1, ← h2]
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 8 1 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 9) + (11 * f + 4) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 7 2 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 9) + (11 * f + 5) + 8 := by lib_int
        _ = x + -x + 8 := by rw [← h1, ← h2]
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (e + f + -(-2)) 3 6 (by calc (11 : Int) * (e + f + -(-2)) = (11 * e + 9) + (11 * f + 9) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl

end Prim11

#print axioms Prim11.rho_mul
#print axioms Prim11.rho_neg
#print axioms Prim11.prim11_mul_0
#print axioms Prim11.prim11_mul_1
#print axioms Prim11.prim11_mul_2
#print axioms Prim11.prim11_mul_3
#print axioms Prim11.prim11_mul_4
#print axioms Prim11.prim11_mul
#print axioms Prim11.prim11_cj
#print axioms Prim11.unit_pm11
#print axioms Prim11.neg_mul11
#print axioms Prim11.normalize11
#print axioms Prim11.unit_one11

#print axioms: 13 theorems, no axioms.

78.280 Unique splitting into \(H_{11}\)

An \(H_{11}\)-element of norm \(mn\), \((m,n)=(mn,11)=1\), is uniquely a product of \(H_{11}\)-elements of norms \(m\), \(n\).

\[ \gamma =\alpha \beta ,\ \alpha ,\beta \in H_{11},\ N(\alpha )=m\ \text {unique}. \]

lang/Uniq11.lean

/-
  lang/Uniq11 — T48/T50, cycle 650: the unique splitting into H11-elements (Prim11) in ℤ[(1+√−11)/2]:
    prim11_quot, uniq_split11, prim11_split   (norms prime to 11; port of Sq2Unique)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Prim11

namespace Uniq11

open FTAChain DetChain LibInt Euc11 Prim11

/-- y ∈ H11 when x and xy are (11 ∤ N(y)). -/
theorem prim11_quot (x y : Int × Int) (hy : ∀ k, en y ≠ 11 * k) (hx : Prim11 x) (hxy : Prim11 ((Ring11.ring11 LibInt.intR).mul x y)) : Prim11 y := by
  obtain ⟨u, hu, hpu⟩ := normalize11 y hy
  have h1 : Prim11 ((Ring11.ring11 LibInt.intR).mul u ((Ring11.ring11 LibInt.intR).mul x y)) := by
    rw [show (Ring11.ring11 LibInt.intR).mul u ((Ring11.ring11 LibInt.intR).mul x y) = (Ring11.ring11 LibInt.intR).mul x ((Ring11.ring11 LibInt.intR).mul u y) by lib_ring]
    exact prim11_mul _ _ hx hpu
  have hu1 := unit_one11 u _ hu hxy h1
  rw [hu1, (Ring11.ring11 LibInt.intR).one_mul] at hpu
  exact hpu

/-- **Uniqueness of the primary splitting.** -/
theorem uniq_split11 (α β α' β' : Int × Int) (M N s t : Int) (hα : en α = M) (hα' : en α' = M) (hβ' : en β' = N)
    (hst : s * M + t * N = 1) (hM : M ≠ 0) (pα : Prim11 α) (pα' : Prim11 α') (h : (Ring11.ring11 LibInt.intR).mul α β = (Ring11.ring11 LibInt.intR).mul α' β') :
    α = α' ∧ β = β' := by
  have hco : (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (Cop11.emb s) (cj α)) α) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (Cop11.emb t) (cj β')) β') = (Ring11.ring11 LibInt.intR).one := by
    rw [show (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (Cop11.emb s) (cj α)) α) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (Cop11.emb t) (cj β')) β') = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul (Cop11.emb s) ((Ring11.ring11 LibInt.intR).mul α (cj α))) ((Ring11.ring11 LibInt.intR).mul (Cop11.emb t)  -- (+59 chars; full line in the Zenodo source)
      mul_cj, mul_cj, hα, hβ']
    show (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul (Cop11.emb s) (Cop11.emb M)) ((Ring11.ring11 LibInt.intR).mul (Cop11.emb t) (Cop11.emb N)) = _
    rw [Cop11.emb_mul, Cop11.emb_mul, ← Cop11.emb_add, hst]; rfl
  match eeuclid α β' α' _ _ hco ⟨β, by rw [show (Ring11.ring11 LibInt.intR).mul β' α' = (Ring11.ring11 LibInt.intR).mul α' β' by lib_ring, ← h]⟩ with
  | ⟨w, hw⟩ =>
    have hgw : en w = 1 := by
      have e : M * en w = M * 1 := by
        calc M * en w = en α * en w := by rw [hα]
          _ = en α' := by rw [← en_mul, ← hw]
          _ = M * 1 := by rw [hα']; lib_int
      have e0 : M * (en w + -1) = 0 := by rw [show M * (en w + -1) = M * en w + -(M * 1) by lib_int, e]; lib_int
      cases int_noZD _ _ e0 with
      | inl h0 => exact absurd h0 hM
      | inr h0 => calc en w = (en w + -1) + 1 := by lib_int
          _ = 1 := by rw [h0]; rfl
    have hw1 : w = (Ring11.ring11 LibInt.intR).one :=
      unit_one11 w α hgw pα (by rw [show (Ring11.ring11 LibInt.intR).mul w α = α' by rw [hw]; lib_ring]; exact pα')
    have hαα : α = α' := by rw [hw, hw1]; lib_ring
    refine ⟨hαα, ?_⟩
    have h0 : (Ring11.ring11 LibInt.intR).mul α ((Ring11.ring11 LibInt.intR).add β ((Ring11.ring11 LibInt.intR).neg β')) = (Ring11.ring11 LibInt.intR).zero := by
      rw [show (Ring11.ring11 LibInt.intR).mul α ((Ring11.ring11 LibInt.intR).add β ((Ring11.ring11 LibInt.intR).neg β')) = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul α β) ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul α β')) by lib_ring, h, ← hαα]; lib_ring
    have := Cop11.cancel_left α _ (by rw [hα]; exact hM) h0
    calc β = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).add β ((Ring11.ring11 LibInt.intR).neg β')) β' := by lib_ring
      _ = β' := by rw [this]; lib_ring

/-- **Existence of the primary splitting.** -/
theorem prim11_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) (hm : ∀ k, Int.ofNat (m + 1) ≠ 11 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 11 * k)
    (pγ : Prim11 γ) : ∃ α β, γ = (Ring11.ring11 LibInt.intR).mul α β ∧ Prim11 α ∧ Prim11 β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match Cop11.ecoprime_split γ m n s t hγ hst with
  | ⟨α0, β0, h0, hα0, hβ0⟩ =>
    match normalize11 α0 (by rw [hα0]; exact hm) with
    | ⟨u, gu, pu⟩ =>
      have uc : (Ring11.ring11 LibInt.intR).mul u (cj u) = (Ring11.ring11 LibInt.intR).one := by rw [mul_cj, gu]; rfl
      have hβn : en ((Ring11.ring11 LibInt.intR).mul (cj u) β0) = Int.ofNat (n + 1) := by
        rw [en_mul, show en (cj u) = en u from en_cj u, gu, hβ0]; lib_int
      have hsplit : γ = (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul u α0) ((Ring11.ring11 LibInt.intR).mul (cj u) β0) := by
        rw [h0, show (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul u α0) ((Ring11.ring11 LibInt.intR).mul (cj u) β0) = (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul u (cj u)) ((Ring11.ring11 LibInt.intR).mul α0 β0) by lib_ring, uc]; lib_ring
      refine ⟨(Ring11.ring11 LibInt.intR).mul u α0, (Ring11.ring11 LibInt.intR).mul (cj u) β0, hsplit, pu,
        prim11_quot _ _ (by rw [hβn]; exact hn) pu (by rw [← hsplit]; exact pγ),
        by rw [en_mul, gu, hα0]; lib_int, hβn⟩

end Uniq11

#print axioms Uniq11.prim11_quot
#print axioms Uniq11.uniq_split11
#print axioms Uniq11.prim11_split

#print axioms: 3 theorems, no axioms.

78.281 121b: multiplicativity of the Hecke theta series

For every multiplicative \(w\) the theta coefficients over \(H_{11}\) are multiplicative.

\[ c_w(mn)=c_w(m)\,c_w(n). \]

lang/HeckeMult11.lean

/-
  lang/HeckeMult11 — T48/T50, cycle 651: **the theta series of 121b's Hecke character class is multiplicative**: for every
  multiplicative w on ℤ[θ], θ = (1 + √−11)/2,  c_w(n) = Σ_{α ∈ H11, N(α) = n} w(α)  satisfies c_w(mn) = c_w(m)c_w(n) for coprime m, n
  prime to 11.  Enumeration box11 n = HeckeMult.box (4n) (4N = (2a + b)² + 7b²).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Uniq11
import lang.HeckeMultW

namespace HeckeMult11

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Euc11 Prim11 Uniq11

abbrev box11 (n : Nat) : List (Int × Int) := HeckeMult.box (12 * n)

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 12n  (11a² + (a + 6b)² = 12N, (2a + b)² + 11b² = 4N). -/
theorem abs_le11 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 12 * n ∧ Int.natAbs z.2 ≤ 12 * n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have h4 := four_nrm z
  rw [hn] at h4
  have h8 : 11 * (Int.natAbs z.1 * Int.natAbs z.1) + Int.natAbs (z.1 + 6 * z.2) * Int.natAbs (z.1 + 6 * z.2) = 12 * n := Int.ofNat.inj (by
    show 11 * Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs (z.1 + 6 * z.2) * Int.natAbs (z.1 + 6 * z.2)) = 12 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; unfold en; lib_int)
  have seven : ∀ X : Nat, X ≤ 11 * (X * X) := fun X => Nat.le_trans (HeckeMult.le_sq X) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 10)))
  constructor
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h8))
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans (Nat.le_of_eq h4.symm) (Nat.mul_le_mul_right n (Nat.le_add_right 4 8))))

def PD11 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim11 z

theorem prim11_iff (z : Int × Int) :
    (ZMod.md 10 (Int.natAbs (rho z - 1)) = 0 ∨ ZMod.md 10 (Int.natAbs (rho z - 3)) = 0 ∨ ZMod.md 10 (Int.natAbs (rho z - 4)) = 0 ∨ ZMod.md 10 (Int.natAbs (rho z - 5)) = 0 ∨ ZMod.md 10 (Int.natAbs (rho z - 9)) = 0) ↔ Prim11 z := by
  constructor
  · intro h
    rcases h with h | h | h | h | h
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 10 _).mpr h
      exact ⟨e, (Or.inl (by calc rho z = (rho z - 1) + 1 := by lib_int
          _ = _ := by rw [he]; rfl))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 10 _).mpr h
      exact ⟨e, (Or.inr (Or.inl (by calc rho z = (rho z - 3) + 3 := by lib_int
          _ = _ := by rw [he]; rfl)))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 10 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 4) + 4 := by lib_int
          _ = _ := by rw [he]; rfl))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 10 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 5) + 5 := by lib_int
          _ = _ := by rw [he]; rfl)))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 10 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inr (by calc rho z = (rho z - 9) + 9 := by lib_int
          _ = _ := by rw [he]; rfl)))))⟩
  · intro ⟨e, h⟩
    rcases h with h | h | h | h | h
    · exact (Or.inl ((HeckeMult.idv_iff 10 _).mp ⟨e, by rw [h]; show 11 * e + 1 - 1 = 11 * e; lib_int⟩))
    · exact (Or.inr (Or.inl ((HeckeMult.idv_iff 10 _).mp ⟨e, by rw [h]; show 11 * e + 3 - 3 = 11 * e; lib_int⟩)))
    · exact (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 10 _).mp ⟨e, by rw [h]; show 11 * e + 4 - 4 = 11 * e; lib_int⟩))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 10 _).mp ⟨e, by rw [h]; show 11 * e + 5 - 5 = 11 * e; lib_int⟩)))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr ((HeckeMult.idv_iff 10 _).mp ⟨e, by rw [h]; show 11 * e + 9 - 9 = 11 * e; lib_int⟩)))))

instance (z : Int × Int) : Decidable (Prim11 z) := decidable_of_iff _ (prim11_iff z)

instance (n : Nat) (z : Int × Int) : Decidable (PD11 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum11 {A : Type} : ∀ l : List A, sumK (Ring11.ring11 LibInt.intR) (fun _ => (Ring11.ring11 LibInt.intR).zero) l = (Ring11.ring11 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Ring11.ring11 LibInt.intR).add (Ring11.ring11 LibInt.intR).zero (sumK (Ring11.ring11 LibInt.intR) (fun _ => (Ring11.ring11 LibInt.intR).zero) l) = _; rw [zsum11 l]; exact (Ring11.ring11 LibInt.intR).zero_add _

def I11 (n : Nat) (z : Int × Int) : Int × Int := if PD11 n z then (Ring11.ring11 LibInt.intR).one else (Ring11.ring11 LibInt.intR).zero

theorem I11_pos (n : Nat) (z : Int × Int) (h : PD11 n z) : I11 n z = (Ring11.ring11 LibInt.intR).one := ite_pos' _ _ h
theorem I11_neg (n : Nat) (z : Int × Int) (h : ¬ PD11 n z) : I11 n z = (Ring11.ring11 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw11 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Ring11.ring11 LibInt.intR) (fun z => (Ring11.ring11 LibInt.intR).mul (I11 n z) (w z)) (box11 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult11 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 11 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 11 * k) :
    cw11 w ((m + 1) * (n + 1)) = (Ring11.ring11 LibInt.intR).mul (cw11 w (m + 1)) (cw11 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD11 (m + 1) α → PD11 (n + 1) β → PD11 ((m + 1) * (n + 1)) ((Ring11.ring11 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim11_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Ring11.ring11 LibInt.intR).mul (cw11 w (m + 1)) (cw11 w (n + 1))
      = sumK (Ring11.ring11 LibInt.intR) (fun α => sumK (Ring11.ring11 LibInt.intR) (fun β => sumK (Ring11.ring11 LibInt.intR) (fun γ => (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (I11 (m + 1) α) (I11 (n + 1) β)) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β))) (w γ)) (box11 ((m + 1) * (n + 1)))) (box11 (n + 1))) (b  -- (+19 chars; full line in the Zenodo source)
    unfold cw11; rw [sumK_mul]
    refine sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD11 (m + 1) α
    · by_cases hβ : PD11 (n + 1) β
      · rw [I11_pos _ _ hα, I11_pos _ _ hβ]
        have hb := abs_le11 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Ring11.ring11 LibInt.intR) (fun γ => w γ) ((Ring11.ring11 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Ring11.ring11 LibInt.intR) (fun γ => (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (Ring11.ring11 LibInt.intR).one (Ring11.ring11 LibInt.intR).one) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β))) (w γ)) (box11 ((m + 1) * (n + 1)))
            = sumK (Ring11.ring11 LibInt.intR) (fun γ => (Ring11.ring11 LibInt.intR).mul (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β)) (w γ)) (box11 ((m + 1) * (n + 1))) from sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I11_neg _ _ hβ]
        rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun γ => show (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (I11 (m + 1) α) (Ring11.ring11 LibInt.intR).zero) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β))) (w γ) = (Ring11.ring11 LibInt.intR).zero by lib_ring)]
        rw [zsum11]; lib_ring
    · rw [I11_neg _ _ hα]
      rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun γ => show (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (Ring11.ring11 LibInt.intR).zero (I11 (n + 1) β)) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β))) (w γ) = (Ring11.ring11 LibInt.intR).zero by lib_ring)]
      rw [zsum11]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => sumK_swap (Ring11.ring11 LibInt.intR) _ _ _)]
  rw [sumK_swap (Ring11.ring11 LibInt.intR)]
  unfold cw11
  refine sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Ring11.ring11 LibInt.intR) (fun α => sumK (Ring11.ring11 LibInt.intR) (fun β => (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (I11 (m + 1) α) (I11 (n + 1) β)) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β))) (w γ)) (box11 (n + 1))) (box11 (m + 1))
      = (Ring11.ring11 LibInt.intR).mul (sumK (Ring11.ring11 LibInt.intR) (fun α => sumK (Ring11.ring11 LibInt.intR) (fun β => (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (I11 (m + 1) α) (I11 (n + 1) β)) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β))) (box11 (n + 1))) (box11 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD11 ((m + 1) * (n + 1)) γ
  · rw [I11_pos _ _ hγ]
    match prim11_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (I11 (m + 1) α) (I11 (n + 1) β)) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β)) = (Ring11.ring11 LibInt.intR).mul (delta (Ring11.ring11 LibInt.intR) α α0) (delta (Ring11.ring11 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD11 (m + 1) α ∧ PD11 (n + 1) β ∧ γ = (Ring11.ring11 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split11 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I11_pos _ _ hα, I11_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Ring11.ring11 LibInt.intR).mul (delta (Ring11.ring11 LibInt.intR) α α0) (delta (Ring11.ring11 LibInt.intR) β β0) = (Ring11.ring11 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD11 (m + 1) α
            · by_cases hβ : PD11 (n + 1) β
              · have hg : ¬ γ = (Ring11.ring11 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I11_neg _ _ hβ]; lib_ring
            · rw [I11_neg _ _ hα]; lib_ring
      rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le11 α0 _ nα0
      have bb := abs_le11 β0 _ nβ0
      rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Ring11.ring11 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => show (Ring11.ring11 LibInt.intR).mul (delta (Ring11.ring11 LibInt.intR) α α0) (Ring11.ring11 LibInt.intR).one = (Ring11.ring11 LibInt.intR).mul (delta (Ring11.ring11 LibInt.intR) α α0) (Ring11.ring11 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Ring11.ring11 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I11_neg _ _ hγ]
    have pt : ∀ α β, (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (I11 (m + 1) α) (I11 (n + 1) β)) (delta (Ring11.ring11 LibInt.intR) γ ((Ring11.ring11 LibInt.intR).mul α β)) = (Ring11.ring11 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD11 (m + 1) α
      · by_cases hβ : PD11 (n + 1) β
        · have hg : ¬ γ = (Ring11.ring11 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I11_neg _ _ hβ]; lib_ring
      · rw [I11_neg _ _ hα]; lib_ring
    rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun α => zsum11 _), zsum11]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult11_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 11 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 11 * k) :
    cw11 (fun z => Frobenius.pw (Ring11.ring11 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Ring11.ring11 LibInt.intR).mul (cw11 (fun z => Frobenius.pw (Ring11.ring11 LibInt.intR) z k) (m + 1)) (cw11 (fun z => Frobenius.pw (Ring11.ring11 LibInt.intR) z k) (n + 1)) :=
  hecke_mult11 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult11

#print axioms HeckeMult11.abs_le11
#print axioms HeckeMult11.prim11_iff
#print axioms HeckeMult11.zsum11
#print axioms HeckeMult11.I11_pos
#print axioms HeckeMult11.I11_neg
#print axioms HeckeMult11.hecke_mult11
#print axioms HeckeMult11.hecke_mult11_pow
#print axioms HeckeMult11.cw11

#print axioms: 8 theorems, no axioms.

78.282 Factorisation above a split prime (d = 11)

The \(H_{11}\)-elements of norm \(p^k\) are the \(\pi ^j\bar \pi ^l\).

\[ N(\alpha )=p^k,\ \alpha \in H_{11}\ \Rightarrow \ \alpha =\pi ^j\bar \pi ^{\,l}. \]

lang/Prime11.lean

/-
  lang/Prime11 — T48/T50, cycle 652: ℤ[(1+√−11)/2] above a split prime: π of prime norm is prime, N(α) = p^k ⇒ α = uπ^jπ̄^l, and the
  H11-elements of norm p^k are the π^j π̄^l (H11 conjugation-stable).  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult11

namespace Prime11

open FTAChain DetChain LibInt Euc11 Prim11 Uniq11 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Ring11.ring11 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Ring11.ring11 LibInt.intR).mul u (cj u) = (Ring11.ring11 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Ring11.ring11 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists _ π x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Ring11.ring11 LibInt.intR).mul (cj d) X) ((Ring11.ring11 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (cj d) X) π) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (cj d) Y) x)
          = (Ring11.ring11 LibInt.intR).mul (cj d) ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul X π) ((Ring11.ring11 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring11.ring11 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Ring11.ring11 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring11.ring11 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Ring11.ring11 LibInt.intR).mul d (Ring11.ring11 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Ring11.ring11 LibInt.intR).mul x y) = (Ring11.ring11 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * (y.1 + y.2) + -(-x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * -y.2 + -x.2 * (y.1 + y.2) + -x.2 * -y.2
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show z.1 + z.2 + -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Ring11.ring11 LibInt.intR).mul u ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) (pw (Ring11.ring11 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Ring11.ring11 LibInt.intR).mul α ((Ring11.ring11 LibInt.intR).mul (Ring11.ring11 LibInt.intR).one (Ring11.ring11 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Ring11.ring11 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Ring11.ring11 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Ring11.ring11 LibInt.intR).mul π ((Ring11.ring11 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring11.ring11 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Ring11.ring11 LibInt.intR).mul u ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Ring11.ring11 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Ring11.ring11 LibInt.intR).mul u ((Ring11.ring11 LibInt.intR).mul _ ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim11_one : Prim11 (Ring11.ring11 LibInt.intR).one := ⟨0, Or.inl rfl⟩

theorem prim11_pow (z : Int × Int) (h : Prim11 z) : ∀ j, Prim11 (pw (Ring11.ring11 LibInt.intR) z j)
  | 0 => prim11_one
  | j + 1 => prim11_mul _ _ (prim11_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim11 α) :
    ∃ j l, j + l = k ∧ α = (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) (pw (Ring11.ring11 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim11_mul _ _ (prim11_pow π pπ j) (prim11_pow (cj π) (prim11_cj π pπ) l)
  have u1 := unit_one11 u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Prime11

#print axioms Prime11.nrm_mul
#print axioms Prime11.unit_inv
#print axioms Prime11.eprime_div
#print axioms Prime11.cj_mul
#print axioms Prime11.cj_cj
#print axioms Prime11.nrm_cj
#print axioms Prime11.nrm_one_eq
#print axioms Prime11.factor3
#print axioms Prime11.prim11_one
#print axioms Prime11.prim11_pow
#print axioms Prime11.primary_only3

#print axioms: 11 theorems, no axioms.

78.283 121b: the Hecke recursion at split primes

For every multiplicative \(W\) the coefficients at \(p^k\) obey the Hecke recursion.

\[ c_W(p^{k+2})=(W(\pi )+W(\bar \pi ))c_W(p^{k+1})-W(\pi )W(\bar \pi )c_W(p^k). \]

lang/HeckePrime11.lean

/-
  lang/HeckePrime11 — T48/T50, cycle 653: **121b's Hecke character at split prime powers**: π ∈ H11 of prime norm p, W multiplicative:
  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l and the Hecke recursion.  (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Prime11
import lang.HeckePrimeW

namespace HeckePrime11

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc11 Prim11 Uniq11 Prime11 HeckeMult11

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Ring11.ring11 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) A = (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) B) : A = B := by
  have h0 : (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) ((Ring11.ring11 LibInt.intR).add A ((Ring11.ring11 LibInt.intR).neg B)) = (Ring11.ring11 LibInt.intR).zero := by
    rw [show (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) ((Ring11.ring11 LibInt.intR).add A ((Ring11.ring11 LibInt.intR).neg B)) = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) A) ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := Cop11.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).add A ((Ring11.ring11 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one11 c π (nrm_one_eq c nc) pπ (by rw [(Ring11.ring11 LibInt.intR).mul_comm, ← hc]; exact prim11_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + a * b + 3 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) :
    ∀ d, Dvd' π (pw (Ring11.ring11 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) (j d l' : Nat)
    (h : (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) (pw (Ring11.ring11 LibInt.intR) (cj π) (l' + 1 + d)) = (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π (j + 1 + d)) (pw (Ring11.ring11 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Ring11.ring11 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Ring11.ring11 LibInt.intR) π j (1 + d)] at h
  have h' : (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) (cj π) l') (pw (Ring11.ring11 LibInt.intR) (cj π) (1 + d)))
      = (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) (cj π) l') (pw (Ring11.ring11 LibInt.intR) π (1 + d))) := by
    calc _ = (Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) (pw (Ring11.ring11 LibInt.intR) π (1 + d))) (pw (Ring11.ring11 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Ring11.ring11 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j) (pw (Ring11.ring11 LibInt.intR) (cj π) l) = (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π j') (pw (Ring11.ring11 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) π x.1) (pw (Ring11.ring11 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD11 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim11_mul _ _ (prim11_pow π pπ _) (prim11_pow (cj π) (prim11_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) (k : Nat) :
    cw11 W ((P + 1 + 1) ^ k) = sumK (Ring11.ring11 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I11 ((P + 1 + 1) ^ k) z = sumK (Ring11.ring11 LibInt.intR) (fun x => delta (Ring11.ring11 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD11 ((P + 1 + 1) ^ k) z
    · rw [I11_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Ring11.ring11 LibInt.intR) k _ (fun x => delta (Ring11.ring11 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Ring11.ring11 LibInt.intR) k j l hjl]
    · rw [I11_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Ring11.ring11 LibInt.intR) k _ (fun _ => (Ring11.ring11 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Ring11.ring11 LibInt.intR) _).symm
  unfold cw11
  rw [sumK_ext (Ring11.ring11 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Ring11.ring11 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le11 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Ring11.ring11 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring11.ring11 LibInt.intR).one = (Ring11.ring11 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Ring11.ring11 LibInt.intR) z j) = pw (Ring11.ring11 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) z j) z) = (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring11.ring11 LibInt.intR).one = (Ring11.ring11 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) (k : Nat) :
    cw11 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Ring11.ring11 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring11.ring11 LibInt.intR).one = (Ring11.ring11 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π) (k : Nat) :
    cw11 W ((P + 1 + 1) ^ (k + 2)) = (Ring11.ring11 LibInt.intR).add
      ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).add (W π) (W (cj π))) (cw11 W ((P + 1 + 1) ^ (k + 1))))
      ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (W π) (W (cj π))) (cw11 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime11

#print axioms HeckePrime11.pow_ne0
#print axioms HeckePrime11.nrm_pw
#print axioms HeckePrime11.npow_add
#print axioms HeckePrime11.en_ne
#print axioms HeckePrime11.cancel_pw3
#print axioms HeckePrime11.no_assoc3
#print axioms HeckePrime11.no_div3
#print axioms HeckePrime11.distinct_lt3
#print axioms HeckePrime11.distinct3
#print axioms HeckePrime11.w3_PD
#print axioms HeckePrime11.cw3_pk
#print axioms HeckePrime11.W_pow3
#print axioms HeckePrime11.cw3_pk'
#print axioms HeckePrime11.cw3_rec

#print axioms: 14 theorems, no axioms.

78.284 121b: the Hecke character at inert primes

A prime that is a non-square mod \(11\) stays prime; the only \(H_{11}\)-element of norm \(p^{2k}\) is \((-p)^k\).

\[ c_W(p^{2k})=W((-p)^k),\qquad c_W(p^{2k+1})=0. \]

lang/Inert11.lean

/-
  lang/Inert11 — T48/T50, cycle 654: **121b's Hecke character at inert primes** p ≡ 2, 6, 7, 8, 10 (mod 11):
    en_mod11       N(a + bθ) ≡ (a + 6b)² (mod 11) (12N = (a + 6b)² + 11a²) is never a non-square mod 11
    cw11_zero      c_W(n) = 0 for n a non-square mod 11
    inert_div, factor_inert, prim_inert   p stays prime; the H11-elements of norm p^(2k) are (−p)^k
    cw11_even, cw11_odd                   c_W(p^(2k)) = W((−p)^k), c_W(p^(2k+1)) = 0
  (Port of Inert7.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime11

namespace Inert11

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc11 Prim11 Uniq11 Prime11 HeckeMult11

variable (P : Nat)

theorem en_mod11 (z : Int × Int) (k : Int) (h : en z = 11 * k + 2 ∨ en z = 11 * k + 6 ∨ en z = 11 * k + 7 ∨ en z = 11 * k + 8 ∨ en z = 11 * k + 10) : False := by
  obtain ⟨a, b⟩ := z
  have hn : en (a, b) = a * a + a * b + 3 * (b * b) := rfl
  obtain ⟨q, r, hq, hr⟩ := GaussEuclid.rnd (a + 6 * b) 10
  have hq' : a + 6 * b = 11 * q + r := by rw [hq]; show q * 11 + r = 11 * q + r; lib_int
  have key : en (a, b) = r * r + 11 * (11 * q * q + 2 * q * r + a * a + -en (a, b)) := by
    have e12 : 12 * en (a, b) = (a + 6 * b) * (a + 6 * b) + 11 * (a * a) := by rw [hn]; lib_int
    rw [hq'] at e12
    calc en (a, b) = 12 * en (a, b) + -(11 * en (a, b)) := by lib_int
      _ = (11 * q + r) * (11 * q + r) + 11 * (a * a) + -(11 * en (a, b)) := by rw [e12]
      _ = _ := by lib_int
  generalize en (a, b) = N at key h
  generalize 11 * q * q + 2 * q * r + a * a + -N = X at key
  have cases_r : r = 0 ∨ r = 1 ∨ r = 2 ∨ r = 3 ∨ r = 4 ∨ r = 5 ∨ r = -1 ∨ r = -2 ∨ r = -3 ∨ r = -4 ∨ r = -5 := by
    cases r with
    | ofNat t =>
      match t, hr with
      | 0, _ => exact (Or.inl (rfl))
      | 1, _ => exact (Or.inr (Or.inl (rfl)))
      | 2, _ => exact (Or.inr (Or.inr (Or.inl (rfl))))
      | 3, _ => exact (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))
      | 4, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))
      | 5, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))
      | t + 6, hr' =>
        exfalso
        have : 2 * (t + 6) ≤ 11 := hr'
        rw [show 2 * (t + 6) = 2 * t + 1 + 11 by lib_nat] at this
        exact Nat.not_succ_le_self 11 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 12) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
    | negSucc t =>
      match t, hr with
      | 0, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))
      | 1, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))
      | 2, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))))
      | 3, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))))
      | 4, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (rfl)))))))))))
      | t + 5, hr' =>
        exfalso
        have : 2 * (t + 6) ≤ 11 := hr'
        rw [show 2 * (t + 6) = 2 * t + 1 + 11 by lib_nat] at this
        exact Nat.not_succ_le_self 11 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 12) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  rcases cases_r with e | e | e | e | e | e | e | e | e | e | e <;> rw [e] at key <;> rcases h with h | h | h | h | h
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 8 1 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 2) + -(0 * 0 + 11 * X) + 9 + -(-2) + (0 * 0 + -2) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 4 5 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 6) + -(0 * 0 + 11 * X) + 5 + -(-6) + (0 * 0 + -6) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 3 6 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 7) + -(0 * 0 + 11 * X) + 4 + -(-7) + (0 * 0 + -7) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 2 7 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 8) + -(0 * 0 + 11 * X) + 3 + -(-8) + (0 * 0 + -8) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 0 9 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(0 * 0 + 11 * X) + 1 + -(-10) + (0 * 0 + -10) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 9 0 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 2) + -(1 * 1 + 11 * X) + 10 + -(-1) + (1 * 1 + -2) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 5 4 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 6) + -(1 * 1 + 11 * X) + 6 + -(-5) + (1 * 1 + -6) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 4 5 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 7) + -(1 * 1 + 11 * X) + 5 + -(-6) + (1 * 1 + -7) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 3 6 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 8) + -(1 * 1 + 11 * X) + 4 + -(-7) + (1 * 1 + -8) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 1 8 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(1 * 1 + 11 * X) + 2 + -(-9) + (1 * 1 + -10) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 1 8 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 2) + -(2 * 2 + 11 * X) + 2 + -(2) + (2 * 2 + -2) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 8 1 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 6) + -(2 * 2 + 11 * X) + 9 + -(-2) + (2 * 2 + -6) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 7 2 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 7) + -(2 * 2 + 11 * X) + 8 + -(-3) + (2 * 2 + -7) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 6 3 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 8) + -(2 * 2 + 11 * X) + 7 + -(-4) + (2 * 2 + -8) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 4 5 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(2 * 2 + 11 * X) + 5 + -(-6) + (2 * 2 + -10) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 6 3 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 2) + -(3 * 3 + 11 * X) + 7 + -(7) + (3 * 3 + -2) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 2 7 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 6) + -(3 * 3 + 11 * X) + 3 + -(3) + (3 * 3 + -6) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 1 8 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 7) + -(3 * 3 + 11 * X) + 2 + -(2) + (3 * 3 + -7) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 0 9 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 8) + -(3 * 3 + 11 * X) + 1 + -(1) + (3 * 3 + -8) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 9 0 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(3 * 3 + 11 * X) + 10 + -(-1) + (3 * 3 + -10) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 2 7 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 2) + -(4 * 4 + 11 * X) + 3 + -(14) + (4 * 4 + -2) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 9 0 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 6) + -(4 * 4 + 11 * X) + 10 + -(10) + (4 * 4 + -6) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 8 1 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 7) + -(4 * 4 + 11 * X) + 9 + -(9) + (4 * 4 + -7) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 7 2 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 8) + -(4 * 4 + 11 * X) + 8 + -(8) + (4 * 4 + -8) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 5 4 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 10) + -(4 * 4 + 11 * X) + 6 + -(6) + (4 * 4 + -10) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(2)) 0 9 (by calc (11 : Int) * (k + -X + -(2)) = (11 * k + 2) + -(5 * 5 + 11 * X) + 1 + -(23) + (5 * 5 + -2) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 7 2 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 6) + -(5 * 5 + 11 * X) + 8 + -(19) + (5 * 5 + -6) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 6 3 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 7) + -(5 * 5 + 11 * X) + 7 + -(18) + (5 * 5 + -7) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 5 4 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 8) + -(5 * 5 + 11 * X) + 6 + -(17) + (5 * 5 + -8) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 3 6 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 10) + -(5 * 5 + 11 * X) + 4 + -(15) + (5 * 5 + -10) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 9 0 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 2) + -(-1 * -1 + 11 * X) + 10 + -(-1) + (-1 * -1 + -2) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 5 4 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 6) + -(-1 * -1 + 11 * X) + 6 + -(-5) + (-1 * -1 + -6) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 4 5 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 7) + -(-1 * -1 + 11 * X) + 5 + -(-6) + (-1 * -1 + -7) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 3 6 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 8) + -(-1 * -1 + 11 * X) + 4 + -(-7) + (-1 * -1 + -8) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 1 8 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(-1 * -1 + 11 * X) + 2 + -(-9) + (-1 * -1 + -10) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 1 8 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 2) + -(-2 * -2 + 11 * X) + 2 + -(2) + (-2 * -2 + -2) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 8 1 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 6) + -(-2 * -2 + 11 * X) + 9 + -(-2) + (-2 * -2 + -6) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 7 2 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 7) + -(-2 * -2 + 11 * X) + 8 + -(-3) + (-2 * -2 + -7) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 6 3 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 8) + -(-2 * -2 + 11 * X) + 7 + -(-4) + (-2 * -2 + -8) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 4 5 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(-2 * -2 + 11 * X) + 5 + -(-6) + (-2 * -2 + -10) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 6 3 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 2) + -(-3 * -3 + 11 * X) + 7 + -(7) + (-3 * -3 + -2) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 2 7 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 6) + -(-3 * -3 + 11 * X) + 3 + -(3) + (-3 * -3 + -6) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 1 8 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 7) + -(-3 * -3 + 11 * X) + 2 + -(2) + (-3 * -3 + -7) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 0 9 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 8) + -(-3 * -3 + 11 * X) + 1 + -(1) + (-3 * -3 + -8) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(-1)) 9 0 (by calc (11 : Int) * (k + -X + -(-1)) = (11 * k + 10) + -(-3 * -3 + 11 * X) + 10 + -(-1) + (-3 * -3 + -10) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 2 7 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 2) + -(-4 * -4 + 11 * X) + 3 + -(14) + (-4 * -4 + -2) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 9 0 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 6) + -(-4 * -4 + 11 * X) + 10 + -(10) + (-4 * -4 + -6) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 8 1 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 7) + -(-4 * -4 + 11 * X) + 9 + -(9) + (-4 * -4 + -7) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 7 2 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 8) + -(-4 * -4 + 11 * X) + 8 + -(8) + (-4 * -4 + -8) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(0)) 5 4 (by calc (11 : Int) * (k + -X + -(0)) = (11 * k + 10) + -(-4 * -4 + 11 * X) + 6 + -(6) + (-4 * -4 + -10) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(2)) 0 9 (by calc (11 : Int) * (k + -X + -(2)) = (11 * k + 2) + -(-5 * -5 + 11 * X) + 1 + -(23) + (-5 * -5 + -2) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 7 2 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 6) + -(-5 * -5 + 11 * X) + 8 + -(19) + (-5 * -5 + -6) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 6 3 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 7) + -(-5 * -5 + 11 * X) + 7 + -(18) + (-5 * -5 + -7) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 5 4 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 8) + -(-5 * -5 + 11 * X) + 6 + -(17) + (-5 * -5 + -8) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 10 (k + -X + -(1)) 3 6 (by calc (11 : Int) * (k + -X + -(1)) = (11 * k + 10) + -(-5 * -5 + 11 * X) + 4 + -(15) + (-5 * -5 + -10) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl

/-- **c_W(n) = 0 for n a non-square mod 11.** -/
theorem cw11_zero (W : Int × Int → Int × Int) (n : Nat) (k : Int) (hn : Int.ofNat n = 11 * k + 2 ∨ Int.ofNat n = 11 * k + 6 ∨ Int.ofNat n = 11 * k + 7 ∨ Int.ofNat n = 11 * k + 8 ∨ Int.ofNat n = 11 * k + 10) : cw11 W n = (Ring11.ring11 LibInt.intR).zero := by
  unfold cw11
  rw [sumK_ext (Ring11.ring11 LibInt.intR) _ (fun _ => (Ring11.ring11 LibInt.intR).zero) (fun z => by
    rw [I11_neg _ _ (fun h => en_mod11 z k (by rw [h.1]; exact hn))]; lib_ring)]
  exact zsum11 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + Int.ofNat (P + 1 + 1) * 0 + 3 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + 0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (z : Int × Int) (h : nrm z = P + 1 + 1) : False := by
  obtain ⟨K, hK⟩ := hc
  exact en_mod11 z K (by rw [← nrm_cast, h]; exact hK)

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (x y : Int × Int)
    (h : Dvd' (Pi P) ((Ring11.ring11 LibInt.intR).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists _ (Pi P) x (Nat.le_refl _) with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Ring11.ring11 LibInt.intR).mul (cj d) X) ((Ring11.ring11 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (cj d) X) (Pi P)) ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (cj d) Y) x)
          = (Ring11.ring11 LibInt.intR).mul (cj d) ((Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul X (Pi P)) ((Ring11.ring11 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring11.ring11 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Ring11.ring11 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring11.ring11 LibInt.intR).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Ring11.ring11 LibInt.intR).mul d (Ring11.ring11 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Ring11.ring11 LibInt.intR).mul u (pw (Ring11.ring11 LibInt.intR) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Ring11.ring11 LibInt.intR).mul α (Ring11.ring11 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Ring11.ring11 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring11.ring11 LibInt.intR).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Ring11.ring11 LibInt.intR).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Ring11.ring11 LibInt.intR).mul u ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) : Prim11 (nPi P) := by
  obtain ⟨K, hK | hK | hK | hK | hK⟩ := hc
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inr (by show -Int.ofNat (P + 1 + 1) + 6 * 0 = _; rw [hK]; lib_int)))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 6 * 0 = _; rw [hK]; lib_int)))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 6 * 0 = _; rw [hK]; lib_int))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 6 * 0 = _; rw [hK]; lib_int)))⟩
  · exact ⟨-K + -1, (Or.inl (by show -Int.ofNat (P + 1 + 1) + 6 * 0 = _; rw [hK]; lib_int))⟩

theorem pw_neg (k : Nat) : pw (Ring11.ring11 LibInt.intR) (Pi P) k =
    (Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) ((Ring11.ring11 LibInt.intR).neg (Ring11.ring11 LibInt.intR).one) k) (pw (Ring11.ring11 LibInt.intR) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -(-0 * 0 + -0 * 0 + -0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1) + -0 * 0; lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Ring11.ring11 LibInt.intR) ((Ring11.ring11 LibInt.intR).neg (Ring11.ring11 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Ring11.ring11 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (k : Nat) (α : Int × Int)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim11 α) : α = pw (Ring11.ring11 LibInt.intR) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Ring11.ring11 LibInt.intR).mul u (pw (Ring11.ring11 LibInt.intR) ((Ring11.ring11 LibInt.intR).neg (Ring11.ring11 LibInt.intR).one) k) = v
  have he' : α = (Ring11.ring11 LibInt.intR).mul v (pw (Ring11.ring11 LibInt.intR) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one11 v _ (nrm_one_eq v hv1) (prim11_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -Int.ofNat (P + 1 + 1) * 0 + 3 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Ring11.ring11 LibInt.intR) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime11.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw11_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (k : Nat) :
    cw11 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Ring11.ring11 LibInt.intR) (nPi P) k) := by
  have hPD : PD11 ((P + 1 + 1) ^ (2 * k)) (pw (Ring11.ring11 LibInt.intR) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim11_pow _ (prim_nPi P hc) k⟩
  unfold cw11
  rw [sumK_ext (Ring11.ring11 LibInt.intR) _ (fun z => (Ring11.ring11 LibInt.intR).mul (delta (Ring11.ring11 LibInt.intR) z (pw (Ring11.ring11 LibInt.intR) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD11 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I11_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I11_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le11 _ _ hPD.1
  exact HeckeMult.pick_box (Ring11.ring11 LibInt.intR) W _ _ hb.1 hb.2

theorem sq11 (Y L : Int) (hL : Y = 11 * L + 2 ∨ Y = 11 * L + 6 ∨ Y = 11 * L + 7 ∨ Y = 11 * L + 8 ∨ Y = 11 * L + 10) : ∃ M : Int, Y * Y = 11 * M + 1 ∨ Y * Y = 11 * M + 3 ∨ Y * Y = 11 * M + 4 ∨ Y * Y = 11 * M + 5 ∨ Y * Y = 11 * M + 9 := by
  rcases hL with hL | hL | hL | hL | hL
  · exact ⟨(4) * L + (11) * L * L, (Or.inr (Or.inr (Or.inl (by rw [hL]; lib_int))))⟩
  · exact ⟨(3) + (12) * L + (11) * L * L, (Or.inr (Or.inl (by rw [hL]; lib_int)))⟩
  · exact ⟨(4) + (14) * L + (11) * L * L, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hL]; lib_int)))))⟩
  · exact ⟨(5) + (16) * L + (11) * L * L, (Or.inr (Or.inr (Or.inr (Or.inr (by rw [hL]; lib_int)))))⟩
  · exact ⟨(9) + (20) * L + (11) * L * L, (Or.inl (by rw [hL]; lib_int))⟩

theorem step11_0 (X K M Z : Int) (hK : X = 11 * K + 2) (hM : Z = 11 * M + 1 ∨ Z = 11 * M + 3 ∨ Z = 11 * M + 4 ∨ Z = 11 * M + 5 ∨ Z = 11 * M + 9) :
    ∃ W : Int, X * Z = 11 * W + 2 ∨ X * Z = 11 * W + 6 ∨ X * Z = 11 * W + 7 ∨ X * Z = 11 * W + 8 ∨ X * Z = 11 * W + 10 := by
  rcases hM with hM | hM | hM | hM | hM <;> rw [hK, hM]
  · exact ⟨K + (11) * K * M + (2) * M, (Or.inl (by lib_int))⟩
  · exact ⟨(3) * K + (11) * K * M + (2) * M, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(4) * K + (11) * K * M + (2) * M, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(5) * K + (11) * K * M + (2) * M, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(1) + (9) * K + (11) * K * M + (2) * M, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩

theorem step11_1 (X K M Z : Int) (hK : X = 11 * K + 6) (hM : Z = 11 * M + 1 ∨ Z = 11 * M + 3 ∨ Z = 11 * M + 4 ∨ Z = 11 * M + 5 ∨ Z = 11 * M + 9) :
    ∃ W : Int, X * Z = 11 * W + 2 ∨ X * Z = 11 * W + 6 ∨ X * Z = 11 * W + 7 ∨ X * Z = 11 * W + 8 ∨ X * Z = 11 * W + 10 := by
  rcases hM with hM | hM | hM | hM | hM <;> rw [hK, hM]
  · exact ⟨K + (11) * K * M + (6) * M, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(1) + (3) * K + (11) * K * M + (6) * M, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(2) + (4) * K + (11) * K * M + (6) * M, (Or.inl (by lib_int))⟩
  · exact ⟨(2) + (5) * K + (11) * K * M + (6) * M, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(4) + (9) * K + (11) * K * M + (6) * M, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩

theorem step11_2 (X K M Z : Int) (hK : X = 11 * K + 7) (hM : Z = 11 * M + 1 ∨ Z = 11 * M + 3 ∨ Z = 11 * M + 4 ∨ Z = 11 * M + 5 ∨ Z = 11 * M + 9) :
    ∃ W : Int, X * Z = 11 * W + 2 ∨ X * Z = 11 * W + 6 ∨ X * Z = 11 * W + 7 ∨ X * Z = 11 * W + 8 ∨ X * Z = 11 * W + 10 := by
  rcases hM with hM | hM | hM | hM | hM <;> rw [hK, hM]
  · exact ⟨K + (11) * K * M + (7) * M, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(1) + (3) * K + (11) * K * M + (7) * M, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(2) + (4) * K + (11) * K * M + (7) * M, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(3) + (5) * K + (11) * K * M + (7) * M, (Or.inl (by lib_int))⟩
  · exact ⟨(5) + (9) * K + (11) * K * M + (7) * M, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩

theorem step11_3 (X K M Z : Int) (hK : X = 11 * K + 8) (hM : Z = 11 * M + 1 ∨ Z = 11 * M + 3 ∨ Z = 11 * M + 4 ∨ Z = 11 * M + 5 ∨ Z = 11 * M + 9) :
    ∃ W : Int, X * Z = 11 * W + 2 ∨ X * Z = 11 * W + 6 ∨ X * Z = 11 * W + 7 ∨ X * Z = 11 * W + 8 ∨ X * Z = 11 * W + 10 := by
  rcases hM with hM | hM | hM | hM | hM <;> rw [hK, hM]
  · exact ⟨K + (11) * K * M + (8) * M, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(2) + (3) * K + (11) * K * M + (8) * M, (Or.inl (by lib_int))⟩
  · exact ⟨(2) + (4) * K + (11) * K * M + (8) * M, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(3) + (5) * K + (11) * K * M + (8) * M, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(6) + (9) * K + (11) * K * M + (8) * M, (Or.inr (Or.inl (by lib_int)))⟩

theorem step11_4 (X K M Z : Int) (hK : X = 11 * K + 10) (hM : Z = 11 * M + 1 ∨ Z = 11 * M + 3 ∨ Z = 11 * M + 4 ∨ Z = 11 * M + 5 ∨ Z = 11 * M + 9) :
    ∃ W : Int, X * Z = 11 * W + 2 ∨ X * Z = 11 * W + 6 ∨ X * Z = 11 * W + 7 ∨ X * Z = 11 * W + 8 ∨ X * Z = 11 * W + 10 := by
  rcases hM with hM | hM | hM | hM | hM <;> rw [hK, hM]
  · exact ⟨K + (11) * K * M + (10) * M, (Or.inr (Or.inr (Or.inr (Or.inr (by lib_int)))))⟩
  · exact ⟨(2) + (3) * K + (11) * K * M + (10) * M, (Or.inr (Or.inr (Or.inr (Or.inl (by lib_int)))))⟩
  · exact ⟨(3) + (4) * K + (11) * K * M + (10) * M, (Or.inr (Or.inr (Or.inl (by lib_int))))⟩
  · exact ⟨(4) + (5) * K + (11) * K * M + (10) * M, (Or.inr (Or.inl (by lib_int)))⟩
  · exact ⟨(8) + (9) * K + (11) * K * M + (10) * M, (Or.inl (by lib_int))⟩

theorem step11 (X Y K L : Int) (hK : X = 11 * K + 2 ∨ X = 11 * K + 6 ∨ X = 11 * K + 7 ∨ X = 11 * K + 8 ∨ X = 11 * K + 10) (hL : Y = 11 * L + 2 ∨ Y = 11 * L + 6 ∨ Y = 11 * L + 7 ∨ Y = 11 * L + 8 ∨ Y = 11 * L + 10) :
    ∃ W : Int, X * Y * Y = 11 * W + 2 ∨ X * Y * Y = 11 * W + 6 ∨ X * Y * Y = 11 * W + 7 ∨ X * Y * Y = 11 * W + 8 ∨ X * Y * Y = 11 * W + 10 := by
  obtain ⟨M, hM⟩ := sq11 Y L hL
  have e : X * Y * Y = X * (Y * Y) := by lib_int
  rw [e]
  rcases hK with hK | hK | hK | hK | hK
  · exact step11_0 _ K M _ hK hM
  · exact step11_1 _ K M _ hK hM
  · exact step11_2 _ K M _ hK hM
  · exact step11_3 _ K M _ hK hM
  · exact step11_4 _ K M _ hK hM

theorem pow_odd (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (k : Nat) : ∃ K : Int, Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 11 * K + 2 ∨ Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 11 * K + 6 ∨ Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) = 11 * K + 7 ∨ Int.of  -- (+104 chars; full line in the Zenodo source)
  obtain ⟨L, hL⟩ := hc
  induction k with
  | zero => exact ⟨L, by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hL⟩
  | succ k ih =>
    obtain ⟨K, hK⟩ := ih
    have e : Int.ofNat ((P + 1 + 1) ^ (2 * (k + 1) + 1)) = Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) := by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; rfl
    rw [e]
    exact step11 _ _ K L hK hL

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw11_odd (W : Int × Int → Int × Int) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (k : Nat) : cw11 W ((P + 1 + 1) ^ (2 * k + 1)) = (Ring11.ring11 LibInt.intR).zero := by
  obtain ⟨K, hK⟩ := pow_odd P hc k
  exact cw11_zero W _ K hK

end Inert11

#print axioms Inert11.en_mod11
#print axioms Inert11.cw11_zero
#print axioms Inert11.en_Pi
#print axioms Inert11.nrm_Pi
#print axioms Inert11.cj_Pi
#print axioms Inert11.not_p
#print axioms Inert11.dvd_conv
#print axioms Inert11.split_sq
#print axioms Inert11.inert_div
#print axioms Inert11.nat_pow2
#print axioms Inert11.factor_inert
#print axioms Inert11.prim_nPi
#print axioms Inert11.pw_neg
#print axioms Inert11.nrm_neg_one_pw
#print axioms Inert11.prim_inert
#print axioms Inert11.nrm_nPi
#print axioms Inert11.nrm_nPi_pw
#print axioms Inert11.cw11_even
#print axioms Inert11.sq11
#print axioms Inert11.step11_0
#print axioms Inert11.step11_1
#print axioms Inert11.step11_2
#print axioms Inert11.step11_3
#print axioms Inert11.step11_4
#print axioms Inert11.step11
#print axioms Inert11.pow_odd
#print axioms Inert11.cw11_odd

#print axioms: 27 theorems, no axioms.

78.285 The ring \(\mathbb Z[(1+\sqrt {-19})/2]\)

Pairs \(a+b\theta \) with \(\theta ^2=\theta -5\) form a commutative ring.

\[ (a+b\theta )(c+d\theta )=(ac-5bd)+(ad+bc+bd)\theta . \]

lang/Ring19.lean

/-
  lang/Ring19 — T48/T50, cycle 656: the ring K[θ] = K[x]/(x² − x + 5) over any commutative ring K (for K = ℤ: the ring of integers
  ℤ[(1 + √−19)/2] of ℚ(√−19), a PID that is not norm-Euclidean).  (a + bθ)(c + dθ) = (ac − 5bd) + (ad + bc + bd)θ.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lib.RingTac

namespace Ring19

variable {R : Type} (K : DetChain.CRing R)

def r19mul (x y : R × R) : R × R :=
  ((K.add (K.mul x.1 y.1) (K.neg (K.add (K.add (K.add (K.add (K.mul x.2 y.2) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)))), (K.add (K.add (K.mul x.1 y.2) (K.mul x.2 y.1)) (K.mul x.2 y.2)))

theorem r19_assoc (a b e : R × R) : r19mul K (r19mul K a b) e = r19mul K a (r19mul K b e) := by
  apply Prod.ext
  · show (K.add (K.mul (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)))) e.1) (K.neg (K.add (K.add (K.add (K.add (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.2) (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.2)) (K.mul (K.add (K.add (K.mul a.1 b.2) (K.  -- (+778 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)))) e.2) (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.1)) (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.2)) = (K.add (K.add (K.mul a.1 (K.add (K.add (K.mul b.1 e.2) (K.mul b.2  -- (+258 chars; full line in the Zenodo source)
    lib_ring

theorem r19_comm (a b : R × R) : r19mul K a b = r19mul K b a := by
  apply Prod.ext
  · show (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)))) = (K.add (K.mul b.1 a.1) (K.neg (K.add (K.add (K.add (K.add (K.mul b.2 a.2) (K.mul b.2 a.2)) (K.mul b.2 a.2)) (K.mul b.2 a.2)) (K.mul b.2 a.2))))
    lib_ring
  · show (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) = (K.add (K.add (K.mul b.1 a.2) (K.mul b.2 a.1)) (K.mul b.2 a.2))
    lib_ring

theorem r19_one (a : R × R) : r19mul K (K.one, K.zero) a = a := by
  apply Prod.ext
  · show (K.add (K.mul K.one a.1) (K.neg (K.add (K.add (K.add (K.add (K.mul K.zero a.2) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)))) = a.1
    lib_ring
  · show (K.add (K.add (K.mul K.one a.2) (K.mul K.zero a.1)) (K.mul K.zero a.2)) = a.2
    lib_ring

theorem r19_distrib (a b e : R × R) :
    r19mul K a (K.add b.1 e.1, K.add b.2 e.2) = (K.add (r19mul K a b).1 (r19mul K a e).1, K.add (r19mul K a b).2 (r19mul K a e).2) := by
  apply Prod.ext
  · show (K.add (K.mul a.1 (K.add b.1 e.1)) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2 (K.add b.2 e.2)) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))))) = (K.add (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)))) (K.add (K.mul a.1 e.1)  -- (+122 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul a.1 (K.add b.2 e.2)) (K.mul a.2 (K.add b.1 e.1))) (K.mul a.2 (K.add b.2 e.2))) = (K.add (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) (K.add (K.add (K.mul a.1 e.2) (K.mul a.2 e.1)) (K.mul a.2 e.2)))
    lib_ring

/-- **K[θ], θ² = θ − 5, as a commutative ring.** -/
def ring19 : DetChain.CRing (R × R) where
  add := fun x y => (K.add x.1 y.1, K.add x.2 y.2)
  mul := r19mul K
  neg := fun x => (K.neg x.1, K.neg x.2)
  zero := (K.zero, K.zero)
  one := (K.one, K.zero)
  add_assoc := fun a b c => by
    apply Prod.ext
    · exact K.add_assoc a.1 b.1 c.1
    · exact K.add_assoc a.2 b.2 c.2
  add_comm := fun a b => by
    apply Prod.ext
    · exact K.add_comm a.1 b.1
    · exact K.add_comm a.2 b.2
  zero_add := fun a => by
    apply Prod.ext
    · exact K.zero_add a.1
    · exact K.zero_add a.2
  add_neg := fun a => by
    apply Prod.ext
    · exact K.add_neg a.1
    · exact K.add_neg a.2
  mul_assoc := r19_assoc K
  mul_comm := r19_comm K
  one_mul := r19_one K
  distrib := r19_distrib K

end Ring19

#print axioms Ring19.r19_assoc
#print axioms Ring19.r19_comm
#print axioms Ring19.r19_one
#print axioms Ring19.r19_distrib
#print axioms Ring19.ring19

#print axioms: 5 theorems, no axioms.

78.286 A Dedekind–Hasse step for \(d=19\)

Where rounding fails, doubling (or multiplying by \(\theta \), \(\bar \theta \) at the half points) gives a smaller nonzero remainder.

\[ \exists s\in \{1,2,\theta ,\bar \theta \},\ q:\ 0\le N(s\alpha -q\beta )<N(\beta ),\ s\alpha =q\beta \Rightarrow s=1. \]

lang/Euc19.lean

/-
  lang/Euc19 — T48/T50, cycle 657: ℤ[θ], θ = (1 + √−19)/2: norm facts and Euclid's lemma, and the gcd as a combination via a
  **Dedekind–Hasse step** (ℤ[θ] is not norm-Euclidean): for β ≠ 0 there are s ∈ {1, 2, θ, θ̄} and q with
    N(sα − qβ) < N(β),  and sα − qβ = 0 only for s = 1.
  N(a + bθ) = a² + ab + 5b², 4N = (2a + b)² + 19b².
    norm facts (en_rep, nrm_cast, four_nrm, mul_cj, en_mul, en_cj, nrm_zero)
    dh_r        the step on a two-step remainder r (|2r₁ + r₂|, 2|r₂| ≤ N): s = 1 if 4N(r) < 4N², else s = 2 (19(N − 2|r₂|)² < 3N²),
                and at the half points (0, ±N/2), (∓N/2, ±N/2) s = θ̄, θ with remainder (±N/2, 0)
    edh, egcd_exists, eeuclid
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Ring19
import lang.GaussEuclid
import lang.W36Mul

namespace Euc19

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + z.1 * z.2 + 5 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Ring19.ring19 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + 5 * (B * B) := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_trans (Nat.le_add_left _ (A * A)) (Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 4))) _))
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + 5 * (Int.ofNat B * Int.ofNat B) := congrArg Int.ofNat hc
  have opp : en z = Int.ofNat A * Int.ofNat A + 5 * (Int.ofNat B * Int.ofNat B) + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨c, by rw [h, ← e]; lib_int⟩
  have same : en z = Int.ofNat A * Int.ofNat A + 5 * (Int.ofNat B * Int.ofNat B) + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨A * A + 5 * (B * B) + A * B, h⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem four_nrm (z : Int × Int) : 4 * nrm z = Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 19 * (Int.natAbs z.2 * Int.natAbs z.2) := by
  apply Int.ofNat.inj
  show 4 * Int.ofNat (nrm z) = Int.ofNat (Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2)) + 19 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2)
  rw [nrm_cast, GaussEuclid.nas, GaussEuclid.nas]; unfold en; lib_int

theorem mul_cj (z : Int × Int) : (Ring19.ring19 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + z.2) + -(z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 5 * (z.2 * z.2)
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + z.2) + z.2 * -z.2 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Ring19.ring19 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) + (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)
      + 5 * ((x.1 * y.2 + x.2 * y.1 + x.2 * y.2) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)) =
    (x.1 * x.1 + x.1 * x.2 + 5 * (x.2 * x.2)) * (y.1 * y.1 + y.1 * y.2 + 5 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + z.2) * (z.1 + z.2) + (z.1 + z.2) * -z.2 + 5 * (-z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 5 * (z.2 * z.2)
  lib_int

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Ring19.ring19 LibInt.intR).zero := by
  have h4 := four_nrm z
  rw [h] at h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by
    have : 19 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
    cases nat_mul_zero _ _ this with
    | inl e => exact Nat.noConfusion e
    | inr e => exact e
  have hT : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have t0 : 2 * z.1 + z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hT with | inl e => exact e | inr e => exact e)
  have a0 : z.1 = 0 := W36Mul.two_cancel_int z.1 0 (by rw [show 2 * z.1 = 2 * z.1 + z.2 by rw [b0]; lib_int, t0]; rfl)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Ring19.ring19 LibInt.intR).zero) :
    ∃ d x y, d = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x α) ((Ring19.ring19 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Ring19.ring19 LibInt.intR).one, (Ring19.ring19 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Ring19.ring19 LibInt.intR).one, by lib_ring⟩, ⟨(Ring19.ring19 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x α) ((Ring19.ring19 LibInt.intR).mul y β) = (Ring19.ring19 LibInt.intR).one) (h : Dvd' α ((Ring19.ring19 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x γ) ((Ring19.ring19 LibInt.intR).mul y c), by
      calc γ = (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x α) ((Ring19.ring19 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul x α) γ) ((Ring19.ring19 LibInt.intR).mul y ((Ring19.ring19 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Ring19.ring19 LibInt.intR).mul α ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x γ) ((Ring19.ring19 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

set_option maxHeartbeats 0 in
/-- 2B + u = N, 3N² ≤ 19B², N > 0 ⇒ 19u² < 3N². -/
theorem ineq19 (N B u : Nat) (hN : N = 2 * B + u) (h : 3 * (N * N) ≤ 19 * (B * B)) (hpos : 0 < N) : 19 * (u * u) < 3 * (N * N) := by
  cases Nat.lt_or_ge (19 * (u * u)) (3 * (N * N)) with
  | inl h' => exact h'
  | inr hc =>
    exfalso
    subst hN
    have hA : 12 * (B * u) + 3 * (u * u) ≤ 7 * (B * B) := GaussEuclid.le_cancel _ _ (12 * (B * B)) (by
      calc 12 * (B * u) + 3 * (u * u) + 12 * (B * B) = 3 * ((2 * B + u) * (2 * B + u)) := by lib_nat
        _ ≤ 19 * (B * B) := h
        _ = 7 * (B * B) + 12 * (B * B) := by lib_nat)
    have hB : 12 * (B * B) + 12 * (B * u) ≤ 16 * (u * u) := GaussEuclid.le_cancel _ _ (3 * (u * u)) (by
      calc 12 * (B * B) + 12 * (B * u) + 3 * (u * u) = 3 * ((2 * B + u) * (2 * B + u)) := by lib_nat
        _ ≤ 19 * (u * u) := hc
        _ = 16 * (u * u) + 3 * (u * u) := by lib_nat)
    have h3 : 228 * (B * u) + 36 * (B * B) ≤ 112 * (B * B) := by
      calc 228 * (B * u) + 36 * (B * B) = 192 * (B * u) + 3 * (12 * (B * B) + 12 * (B * u)) := by lib_nat
        _ ≤ 192 * (B * u) + 3 * (16 * (u * u)) := Nat.add_le_add_left (Nat.mul_le_mul_left 3 hB) _
        _ = 16 * (12 * (B * u) + 3 * (u * u)) := by lib_nat
        _ ≤ 16 * (7 * (B * B)) := Nat.mul_le_mul_left 16 hA
        _ = 112 * (B * B) := by lib_nat
    have h4 : 228 * (B * u) ≤ 76 * (B * B) := GaussEuclid.le_cancel _ _ (36 * (B * B)) (Nat.le_trans h3 (Nat.le_of_eq (by lib_nat)))
    cases B with
    | zero =>
      have : 3 * ((2 * 0 + u) * (2 * 0 + u)) = 0 := Nat.le_antisymm (Nat.le_trans h (Nat.le_of_eq (by lib_nat))) (Nat.zero_le _)
      have hu : u * u = 0 := by
        rw [show 3 * ((2 * 0 + u) * (2 * 0 + u)) = 3 * (u * u) by lib_nat] at this
        cases nat_mul_zero _ _ this with
        | inl e => exact Nat.noConfusion e
        | inr e => exact e
      have u0 : u = 0 := by cases nat_mul_zero _ _ hu with | inl e => exact e | inr e => exact e
      rw [u0] at hpos; exact Nat.lt_irrefl 0 hpos
    | succ B' =>
      have h5 : 3 * u ≤ B' + 1 := by
        apply Nat.le_of_mul_le_mul_left (show (B' + 1) * (3 * u) ≤ (B' + 1) * (B' + 1) from
          Nat.le_of_mul_le_mul_left (show 76 * ((B' + 1) * (3 * u)) ≤ 76 * ((B' + 1) * (B' + 1)) from Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans h4 (Nat.le_of_eq (by lib_nat)))) (Nat.zero_lt_succ 75)) (Nat.zero_lt_succ B')
      have h6 : 9 * (u * u) ≤ (B' + 1) * (B' + 1) := Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.mul_le_mul h5 h5)
      have h7 : 108 * ((B' + 1) * (B' + 1)) ≤ 16 * ((B' + 1) * (B' + 1)) := by
        calc 108 * ((B' + 1) * (B' + 1)) ≤ 9 * (12 * ((B' + 1) * (B' + 1)) + 12 * ((B' + 1) * u)) := by
              rw [show 9 * (12 * ((B' + 1) * (B' + 1)) + 12 * ((B' + 1) * u)) = 108 * ((B' + 1) * (B' + 1)) + 108 * ((B' + 1) * u) by lib_nat]; exact Nat.le_add_right _ _
          _ ≤ 9 * (16 * (u * u)) := Nat.mul_le_mul_left 9 hB
          _ = 16 * (9 * (u * u)) := by lib_nat
          _ ≤ 16 * ((B' + 1) * (B' + 1)) := Nat.mul_le_mul_left 16 h6
      have : 92 * ((B' + 1) * (B' + 1)) + 16 * ((B' + 1) * (B' + 1)) ≤ 0 + 16 * ((B' + 1) * (B' + 1)) := Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans h7 (Nat.le_of_eq (by lib_nat)))
      have h8 := GaussEuclid.le_cancel _ _ _ this
      rw [show 92 * ((B' + 1) * (B' + 1)) = (92 * (B' * B' + 2 * B') + 91) + 1 by lib_nat] at h8
      exact Nat.not_succ_le_zero _ h8

theorem nrm_lt (z : Int × Int) (N : Nat) (h : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 19 * (Int.natAbs z.2 * Int.natAbs z.2) < 4 * (N * N)) :
    nrm z < N * N := by
  cases Nat.lt_or_ge (nrm z) (N * N) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    rw [← four_nrm] at h
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_left 4 h'))

theorem sq_le (T N : Nat) (h : T ≤ N) : T * T ≤ N * N := Nat.mul_le_mul h h

/-- **The Dedekind–Hasse step on a remainder.** -/
theorem dh_r (N0 : Nat) (r1 r2 : Int) (h2 : 2 * Int.natAbs r2 ≤ N0 + 1) (ht : Int.natAbs (2 * r1 + r2) ≤ N0 + 1) :
    ∃ s e : Int × Int, nrm ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring19.ring19 LibInt.intR).zero → s = (Ring19.ring19 LibInt.intR).one) := by
  have h4 := four_nrm (r1, r2)
  generalize hT : Int.natAbs (2 * r1 + r2) = T at h4 ht
  generalize hB : Int.natAbs r2 = B at h4 h2
  have h4' : 4 * nrm (r1, r2) = T * T + 19 * (B * B) := h4
  cases Nat.lt_or_ge (T * T + 19 * (B * B)) (4 * ((N0 + 1) * (N0 + 1))) with
  | inl hl =>
    refine ⟨(Ring19.ring19 LibInt.intR).one, (Ring19.ring19 LibInt.intR).zero, ?_, fun _ => rfl⟩
    have e : (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).one (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).zero (Int.ofNat (N0 + 1), 0))) = (r1, r2) := by lib_ring
    rw [e]
    exact nrm_lt (r1, r2) (N0 + 1) (by rw [hT, hB]; exact hl)
  | inr hg =>
    have h3 : 3 * ((N0 + 1) * (N0 + 1)) ≤ 19 * (B * B) := GaussEuclid.le_cancel _ _ ((N0 + 1) * (N0 + 1)) (by
      calc 3 * ((N0 + 1) * (N0 + 1)) + (N0 + 1) * (N0 + 1) = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat
        _ ≤ T * T + 19 * (B * B) := hg
        _ ≤ (N0 + 1) * (N0 + 1) + 19 * (B * B) := Nat.add_le_add_right (sq_le T _ ht) _
        _ = 19 * (B * B) + (N0 + 1) * (N0 + 1) := by lib_nat)
    obtain ⟨u, hu⟩ := EisEuclid.le_dest _ _ h2
    have hN : N0 + 1 = 2 * B + u := by rw [← hu]; lib_nat
    have hineq := ineq19 (N0 + 1) B u hN h3 (Nat.zero_lt_succ N0)
    -- the sign of r2
    have hsg : ∃ d2 : Int, (d2 = 1 ∨ d2 = -1) ∧ r2 = d2 * Int.ofNat B := by
      rcases EisEuclid.sgn r2 with e | e
      · exact ⟨1, Or.inl rfl, by rw [e, hB]; lib_int⟩
      · exact ⟨-1, Or.inr rfl, by rw [e, hB]; lib_int⟩
    obtain ⟨d2, hd2, hr2⟩ := hsg
    obtain ⟨d1, t', hrd, hb1⟩ := GaussEuclid.rnd (2 * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1))) (2 * N0 + 1)
    have hr2' : 2 * r2 + -(d2 * Int.ofNat (N0 + 1)) = -(d2 * Int.ofNat u) := by
      rw [hr2, show Int.ofNat (N0 + 1) = 2 * Int.ofNat B + Int.ofNat u from congrArg Int.ofNat hN]; lib_int
    have hrd' : 2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = t' := by
      have e2 : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show Int.ofNat (2 * N0 + 1 + 1) = 2 * (Int.ofNat N0 + 1); show 2 * Int.ofNat N0 + 1 + 1 = _; lib_int
      calc 2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = 2 * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1)) + -(d1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
        _ = t' := by rw [hrd, ← e2]; lib_int
    have hT' : Int.natAbs t' ≤ N0 + 1 := Nat.le_of_mul_le_mul_left (Nat.le_trans hb1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
    have hu' : Int.natAbs (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = u := by
      rw [hr2']; rcases hd2 with e | e <;> rw [e]
      · show Int.natAbs (-(1 * Int.ofNat u)) = u; rw [show -(1 * Int.ofNat u) = -Int.ofNat u by lib_int]; exact Int.natAbs_neg _
      · show Int.natAbs (-(-1 * Int.ofNat u)) = u; rw [show -(-1 * Int.ofNat u) = Int.ofNat u by lib_int]; rfl
    generalize hrr : (2 * r1 + -(d1 * Int.ofNat (N0 + 1)), 2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = r'
    have hexpr : (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (2, 0) (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul (d1, d2) (Int.ofNat (N0 + 1), 0))) = r' := by
      rw [← hrr]
      apply Prod.ext
      · show 2 * r1 + -(0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2) + -(d1 * Int.ofNat (N0 + 1) + -(d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0)) = 2 * r1 + -(d1 * Int.ofNat (N0 + 1)); lib_int
      · show 2 * r2 + 0 * r1 + 0 * r2 + -(d1 * 0 + d2 * Int.ofNat (N0 + 1) + d2 * 0) = 2 * r2 + -(d2 * Int.ofNat (N0 + 1)); lib_int
    by_cases hz : r' = (Ring19.ring19 LibInt.intR).zero
    · -- half points: N = 2B, r1 = d1 B
      have z1 : 2 * r1 + -(d1 * Int.ofNat (N0 + 1)) = 0 := by rw [← hrr] at hz; exact congrArg Prod.fst hz
      have z2 : 2 * r2 + -(d2 * Int.ofNat (N0 + 1)) = 0 := by rw [← hrr] at hz; exact congrArg Prod.snd hz
      have u0 : u = 0 := by rw [← hu', z2]; rfl
      have hNB : Int.ofNat (N0 + 1) = 2 * Int.ofNat B := by rw [show Int.ofNat (N0 + 1) = 2 * Int.ofNat B + Int.ofNat u from congrArg Int.ofNat hN, u0]; show _ + 0 = _; lib_int
      have hr1 : r1 = d1 * Int.ofNat B := W36Mul.two_cancel_int _ _ (by
        calc 2 * r1 = 2 * r1 + -(d1 * Int.ofNat (N0 + 1)) + d1 * Int.ofNat (N0 + 1) := by lib_int
          _ = 2 * (d1 * Int.ofNat B) := by rw [z1, hNB]; lib_int)
      have hBpos : B ≠ 0 := fun e => by rw [e] at hN; rw [u0] at hN; exact Nat.noConfusion hN
      have nB : nrm (d2 * Int.ofNat B, 0) < (N0 + 1) * (N0 + 1) := by
        apply nrm_lt
        have e1 : Int.natAbs (2 * (d2 * Int.ofNat B) + 0) = 2 * B := by
          rcases hd2 with e | e <;> rw [e]
          · show Int.natAbs (2 * (1 * Int.ofNat B) + 0) = 2 * B; rw [show 2 * (1 * Int.ofNat B) + 0 = Int.ofNat (2 * B) by show _ = 2 * Int.ofNat B; lib_int]; rfl
          · show Int.natAbs (2 * (-1 * Int.ofNat B) + 0) = 2 * B; rw [show 2 * (-1 * Int.ofNat B) + 0 = -Int.ofNat (2 * B) by show _ = -(2 * Int.ofNat B); lib_int]; exact Int.natAbs_neg _
        show Int.natAbs (2 * (d2 * Int.ofNat B) + 0) * Int.natAbs (2 * (d2 * Int.ofNat B) + 0) + 19 * (Int.natAbs 0 * Int.natAbs 0) < _
        rw [e1, hN, u0]
        have hBs : ∃ B', B = B' + 1 := by
          cases B with
          | zero => exact absurd rfl hBpos
          | succ b => exact ⟨b, rfl⟩
        obtain ⟨B', hB'⟩ := hBs
        rw [hB']
        show (2 * (B' + 1)) * (2 * (B' + 1)) + 19 * 0 < 4 * ((2 * (B' + 1) + 0) * (2 * (B' + 1) + 0))
        rw [show 4 * ((2 * (B' + 1) + 0) * (2 * (B' + 1) + 0)) = (2 * (B' + 1)) * (2 * (B' + 1)) + 19 * 0 + (12 * (B' * B' + 2 * B') + 11 + 1) by lib_nat]
        exact Nat.lt_add_of_pos_right (Nat.zero_lt_succ _)
      have nz : (d2 * Int.ofNat B, (0 : Int)) ≠ (Ring19.ring19 LibInt.intR).zero := fun e => by
        have : d2 * Int.ofNat B = 0 := congrArg Prod.fst e
        rcases hd2 with e2 | e2 <;> rw [e2] at this
        · exact hBpos (Int.ofNat.inj (show Int.ofNat B = 0 from by calc Int.ofNat B = 1 * Int.ofNat B := by lib_int
            _ = 0 := this))
        · exact hBpos (Int.ofNat.inj (show Int.ofNat B = 0 from by calc Int.ofNat B = -(-1 * Int.ofNat B) := by lib_int
            _ = -0 := by rw [this]
            _ = 0 := rfl))
      have fin : ∀ sv ev : Int × Int, (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul sv (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ev (Int.ofNat (N0 + 1), 0))) = (d2 * Int.ofNat B, 0) →
          ∃ s e : Int × Int, nrm ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
            ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (r1, r2)) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring19.ring19 LibInt.intR).zero → s = (Ring19.ring19 LibInt.intR).one) :=
        fun sv ev he => ⟨sv, ev, by rw [he]; exact nB, fun h0 => absurd (he.symm.trans h0) nz⟩
      rcases GaussInt.int_even_odd d1 with ⟨k, hk⟩ | ⟨k, hk⟩
      · -- s = θ̄ = (1, −1), e = (k + 2 d2, −k)
        apply fin (1, -1) (k + 2 * d2, -k)
        apply Prod.ext
        · show 1 * r1 + -(-1 * r2 + -1 * r2 + -1 * r2 + -1 * r2 + -1 * r2) + -((k + 2 * d2) * Int.ofNat (N0 + 1) + -(-k * 0 + -k * 0 + -k * 0 + -k * 0 + -k * 0)) = d2 * Int.ofNat B
          rw [hr1, hr2, hk, hNB]; lib_int
        · show 1 * r2 + -1 * r1 + -1 * r2 + -((k + 2 * d2) * 0 + -k * Int.ofNat (N0 + 1) + -k * 0) = 0
          rw [hr1, hk, hNB]; lib_int
      · -- s = θ = (0, 1), e = (−3 d2, (d1 + d2)/2)
        rcases hd2 with e2 | e2
        · apply fin (0, 1) (-(3 * d2), k + 1)
          apply Prod.ext
          · show 0 * r1 + -(1 * r2 + 1 * r2 + 1 * r2 + 1 * r2 + 1 * r2) + -(-(3 * d2) * Int.ofNat (N0 + 1) + -((k + 1) * 0 + (k + 1) * 0 + (k + 1) * 0 + (k + 1) * 0 + (k + 1) * 0)) = d2 * Int.ofNat B
            rw [hr2, hNB, e2]; lib_int
          · show 0 * r2 + 1 * r1 + 1 * r2 + -(-(3 * d2) * 0 + (k + 1) * Int.ofNat (N0 + 1) + (k + 1) * 0) = 0
            rw [hr1, hr2, hk, hNB, e2]; lib_int
        · apply fin (0, 1) (-(3 * d2), k)
          apply Prod.ext
          · show 0 * r1 + -(1 * r2 + 1 * r2 + 1 * r2 + 1 * r2 + 1 * r2) + -(-(3 * d2) * Int.ofNat (N0 + 1) + -(k * 0 + k * 0 + k * 0 + k * 0 + k * 0)) = d2 * Int.ofNat B
            rw [hr2, hNB, e2]; lib_int
          · show 0 * r2 + 1 * r1 + 1 * r2 + -(-(3 * d2) * 0 + k * Int.ofNat (N0 + 1) + k * 0) = 0
            rw [hr1, hr2, hk, hNB, e2]; lib_int
    · -- s = 2
      refine ⟨(2, 0), (d1, d2), ?_, fun h0 => absurd (hexpr.symm.trans h0) hz⟩
      rw [hexpr, ← hrr]
      apply nrm_lt
      show Int.natAbs (2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1)))) * Int.natAbs (2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1))))
        + 19 * (Int.natAbs (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) * Int.natAbs (2 * r2 + -(d2 * Int.ofNat (N0 + 1)))) < _
      rw [hrd', hu']
      calc Int.natAbs t' * Int.natAbs t' + 19 * (u * u) < (N0 + 1) * (N0 + 1) + 3 * ((N0 + 1) * (N0 + 1)) := Nat.add_lt_add_of_le_of_lt (sq_le _ _ hT') hineq
        _ = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat

end Euc19

#print axioms Euc19.sgn
#print axioms Euc19.le_dest
#print axioms Euc19.ab_le
#print axioms Euc19.en_rep
#print axioms Euc19.nrm_cast
#print axioms Euc19.four_nrm
#print axioms Euc19.mul_cj
#print axioms Euc19.en_mul
#print axioms Euc19.en_cj
#print axioms Euc19.nrm_zero
#print axioms Euc19.egcd_base
#print axioms Euc19.ineq19
#print axioms Euc19.nrm_lt
#print axioms Euc19.sq_le
#print axioms Euc19.dh_r
#print axioms Euc19.eeuclid

#print axioms: 16 theorems, no axioms.

78.287 gcd in \(\mathbb Z[(1+\sqrt {-19})/2]\)

Every pair has a gcd that is a combination: descend on the norm in the ideal with the Dedekind–Hasse step.

\[ \exists d=x\alpha +y\beta ,\ d\mid \alpha ,\ d\mid \beta . \]

lang/Gcd19.lean

/-
  lang/Gcd19 — T48/T50, cycle 658: **gcd as a combination in ℤ[(1 + √−19)/2]** (a PID that is not norm-Euclidean).
    edh           for β ≠ 0: sα − qβ with N < N(β), and = 0 only for s = 1          (Euc19.dh_r on the two-step remainder)
    gaux          from any nonzero d = xα + yβ: either d | α, d | β, or a nonzero element of smaller norm in the ideal
    egcd_exists   d = xα + yβ dividing α and β
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc19

namespace Euc19

open FTAChain DetChain LibInt

theorem lt_of_mul (a N : Nat) (h : a * (N + 1) < (N + 1) * (N + 1)) : a < N + 1 := by
  cases Nat.lt_or_ge a (N + 1) with
  | inl h' => exact h'
  | inr h' => exact absurd (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_right _ h')) (Nat.lt_irrefl _)

/-- **The Dedekind–Hasse step.** -/
theorem edh (α β : Int × Int) (N0 : Nat) (hβ : nrm β = N0 + 1) :
    ∃ s q : Int × Int, nrm ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q β))) < N0 + 1 ∧
      (((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q β))) = (Ring19.ring19 LibInt.intR).zero → s = (Ring19.ring19 LibInt.intR).one) := by
  have hN : en β = Int.ofNat (N0 + 1) := by rw [← nrm_cast, hβ]
  generalize hS : (Ring19.ring19 LibInt.intR).mul α (cj β) = S
  obtain ⟨q2, r2, h2, b2⟩ := GaussEuclid.rnd S.2 N0
  obtain ⟨q1, t, h1, b1⟩ := GaussEuclid.rnd (2 * S.1 + r2) (2 * N0 + 1)
  have hT : Int.natAbs (2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2) ≤ N0 + 1 := by
    have e : 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = t := by
      have e2 : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
      calc 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = (2 * S.1 + r2) + -(q1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
        _ = t := by rw [h1, ← e2]; lib_int
    rw [e]; exact Nat.le_of_mul_le_mul_left (Nat.le_trans b1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
  obtain ⟨s, e, hlt, hzero⟩ := dh_r N0 (S.1 + -(q1 * Int.ofNat (N0 + 1))) r2 b2 hT
  have hSr : S = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (q1, q2) (Int.ofNat (N0 + 1), 0)) (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) := by
    apply Prod.ext
    · show S.1 = q1 * Int.ofNat (N0 + 1) + -(q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0) + (S.1 + -(q1 * Int.ofNat (N0 + 1))); lib_int
    · show S.2 = q1 * 0 + q2 * Int.ofNat (N0 + 1) + q2 * 0 + r2; rw [h2]; show q2 * Int.ofNat (N0 + 1) + r2 = _; lib_int
  generalize hr : (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) = r at hSr hlt hzero
  refine ⟨s, (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (q1, q2)) e, ?_, ?_⟩
  all_goals
    have key : (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
        = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s r) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) := by
      rw [show (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
          = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s ((Ring19.ring19 LibInt.intR).mul α (cj β))) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (q1, q2)) e) ((Ring19.ring19 LibInt.intR).mul β (cj β)))) by lib_ring,
        hS, mul_cj, hN, hSr]
      lib_ring
  · -- norm bound
    have hn : en ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (q1, q2)) e) β))) * Int.ofNat (N0 + 1)
        = en ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s r) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      rw [← key, en_mul, en_cj, hN]
    apply lt_of_mul
    have : nrm ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s (q1, q2)) e) β))) * (N0 + 1)
        = nrm ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s r) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm _) * Int.ofNat (N0 + 1) = Int.ofNat (nrm _)
      rw [nrm_cast, nrm_cast]; exact hn
    rw [this]; exact hlt
  · intro h0
    apply hzero
    rw [← key, h0]; lib_ring

theorem nrm_succ (d : Int × Int) (hd : d ≠ (Ring19.ring19 LibInt.intR).zero) : ∃ N0, nrm d = N0 + 1 := by
  cases h : nrm d with
  | zero => exact absurd (nrm_zero d h) hd
  | succ N0 => exact ⟨N0, rfl⟩

theorem gaux (α β : Int × Int) : ∀ G (d x y : Int × Int), d = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x α) ((Ring19.ring19 LibInt.intR).mul y β) →
    nrm d ≤ G → d ≠ (Ring19.ring19 LibInt.intR).zero →
    ∃ d' x' y', d' = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x' α) ((Ring19.ring19 LibInt.intR).mul y' β) ∧ Dvd' d' α ∧ Dvd' d' β
  | 0, d, _, _, _, hG, hd => absurd (nrm_zero d (Nat.le_antisymm hG (Nat.zero_le _))) hd
  | G + 1, d, x, y, hdef, hG, hd => by
    obtain ⟨N0, hN0⟩ := nrm_succ d hd
    obtain ⟨s, q, hlt, hz⟩ := edh α d N0 hN0
    have hle : ∀ ρ, nrm ρ < N0 + 1 → nrm ρ ≤ G := fun ρ h => Nat.le_of_lt_succ (Nat.lt_of_lt_of_le h (Nat.le_trans (Nat.le_of_eq hN0.symm) hG))
    by_cases hρ : (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q d)) = (Ring19.ring19 LibInt.intR).zero
    · have s1 := hz hρ
      rw [s1] at hρ
      have hα : Dvd' d α := ⟨q, by
        calc α = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).one α) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q d))) ((Ring19.ring19 LibInt.intR).mul d q) := by lib_ring
          _ = _ := by rw [hρ]; lib_ring⟩
      obtain ⟨s', q', hlt', hz'⟩ := edh β d N0 hN0
      by_cases hρ' : (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul s' β) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q' d)) = (Ring19.ring19 LibInt.intR).zero
      · have s1' := hz' hρ'
        rw [s1'] at hρ'
        exact ⟨d, x, y, hdef, hα, ⟨q', by
          calc β = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).one β) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q' d))) ((Ring19.ring19 LibInt.intR).mul d q') := by lib_ring
            _ = _ := by rw [hρ']; lib_ring⟩⟩
      · exact gaux α β G _ ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q' x)) ((Ring19.ring19 LibInt.intR).add s' ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q' y)))
          (by rw [hdef]; lib_ring) (hle _ hlt') hρ'
    · exact gaux α β G _ ((Ring19.ring19 LibInt.intR).add s ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q x))) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul q y))
        (by rw [hdef]; lib_ring) (hle _ hlt) hρ

/-- **gcd as a combination in ℤ[(1 + √−19)/2].** -/
theorem egcd_exists (α β : Int × Int) :
    ∃ d x y, d = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x α) ((Ring19.ring19 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β := by
  by_cases hb : β = (Ring19.ring19 LibInt.intR).zero
  · exact egcd_base α β hb
  · exact gaux α β (nrm β) β (Ring19.ring19 LibInt.intR).zero (Ring19.ring19 LibInt.intR).one (by lib_ring) (Nat.le_refl _) hb

end Euc19

#print axioms Euc19.lt_of_mul
#print axioms Euc19.edh
#print axioms Euc19.nrm_succ
#print axioms Euc19.gaux
#print axioms Euc19.egcd_exists

#print axioms: 5 theorems, no axioms.

78.288 Coprime splitting (\(d=19\))

An element whose norm is a product of coprime factors splits accordingly.

\[ N(\gamma )=mn\ \Rightarrow \ \gamma =\alpha \beta ,\ N(\alpha )=m. \]

lang/Cop19.lean

/-
  lang/Cop19 — T48/T50, cycle 659: splitting by coprime norms in ℤ[(1+√−19)/2].

  If N(γ) = mn with sm + tn = 1 (m, n ≥ 1), then γ = αβ with N(α) = m, N(β) = n (N(a + bθ) = a² + ab + 3b²):
  α = gcd(γ, m), e = gcd(γ, n) (Euc19.egcd_exists), αe | γ by Euclid's lemma, γ | αe since mn = γγ̄, so γ = αe·v with v a unit;
  then N(α) | m, N(e) | n and N(α)N(e) = mn give N(α) = m. (Port of GaussCoprime.coprime_split.)
    en_emb, emb_mul, en_dvd, emb_add, en_zero_imp, cancel_left, en_nonneg_one, ecoprime_split
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Gcd19
import lang.GaussCoprime

namespace Cop19

open FTAChain DetChain LibInt Euc19

def emb (m : Int) : Int × Int := (m, 0)

theorem en_emb (m : Int) : en (emb m) = m * m := by show m * m + m * 0 + 5 * (0 * 0) = m * m; lib_int

theorem emb_mul (a b : Int) : (Ring19.ring19 LibInt.intR).mul (emb a) (emb b) = emb (a * b) := by
  apply Prod.ext
  · show a * b + -(0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0) = a * b; lib_int
  · show a * 0 + 0 * b + 0 * 0 = 0; lib_int

theorem en_dvd (d γ : Int × Int) (h : Dvd' d γ) : ∃ k, en γ = en d * k :=
  match h with
  | ⟨c, hc⟩ => ⟨en c, by rw [hc, en_mul]⟩

theorem emb_add (a b : Int) : emb (a + b) = (Ring19.ring19 LibInt.intR).add (emb a) (emb b) := by
  apply Prod.ext
  · rfl
  · show (0 : Int) = 0 + 0; rfl

theorem en_zero_imp (z : Int × Int) (h : en z = 0) : z = (Ring19.ring19 LibInt.intR).zero := by
  apply nrm_zero
  apply Int.ofNat.inj
  rw [nrm_cast]; exact h

theorem cancel_left (γ z : Int × Int) (hγ : en γ ≠ 0) (h : (Ring19.ring19 LibInt.intR).mul γ z = (Ring19.ring19 LibInt.intR).zero) : z = (Ring19.ring19 LibInt.intR).zero := by
  have : en γ * en z = 0 := by rw [← en_mul, h]; rfl
  cases int_noZD _ _ this with
  | inl e => exact absurd e hγ
  | inr e => exact en_zero_imp z e

theorem en_nonneg_one (z : Int × Int) (h : en z = -1) : False := by
  rw [← nrm_cast] at h
  exact Int.noConfusion h

/-- **Coprime splitting**: N(γ) = mn, sm + tn = 1 ⇒ γ = αβ with N(α) = m, N(β) = n. -/
theorem ecoprime_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) :
    ∃ α β, γ = (Ring19.ring19 LibInt.intR).mul α β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match egcd_exists γ (emb (Int.ofNat (m + 1))), egcd_exists γ (emb (Int.ofNat (n + 1))) with
  | ⟨d, x, y, hd, ⟨u, hu⟩, ⟨c1, hc1⟩⟩, ⟨e, x', y', he, ⟨u', hu'⟩, ⟨c2, hc2⟩⟩ =>
    have hco : (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (emb t) c2) e) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (emb s) c1) d) = (Ring19.ring19 LibInt.intR).one := by
      have h1 : (Ring19.ring19 LibInt.intR).one = emb (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; rfl
      rw [h1, emb_add, ← emb_mul, ← emb_mul, hc1, hc2]; lib_ring
    -- e | u, so γ = d e v
    match eeuclid e d u _ _ hco ⟨u', by rw [← hu, hu']⟩ with
    | ⟨v, hv⟩ =>
    have hγdev : γ = (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul d e) v := by rw [hu, hv]; lib_ring
    -- γ | d e
    have hmn : (Ring19.ring19 LibInt.intR).mul γ (cj γ) = emb (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by
      rw [mul_cj, hγ]; rfl
    have hW : (Ring19.ring19 LibInt.intR).mul d e = (Ring19.ring19 LibInt.intR).mul γ ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul x x') γ) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring1  -- (+146 chars; full line in the Zenodo source)
      rw [hd, he]
      have : (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul y y') ((Ring19.ring19 LibInt.intR).mul γ (cj γ)) = (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul y y') ((Ring19.ring19 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1)))) := by rw [hmn, emb_mul]
      calc (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x γ) ((Ring19.ring19 LibInt.intR).mul y (emb (Int.ofNat (m + 1))))) ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul x' γ) ((Ring19.ring19 LibInt.intR).mul y' (emb (Int.ofNat (n + 1)))))
          = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul γ ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul x x') γ) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul y x') (emb (In  -- (+20 chars; full line in the Zenodo source)
              ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul y y') ((Ring19.ring19 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1))))) := by lib_ring
        _ = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul γ ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul x x') γ) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul y x') (emb (In  -- (+20 chars; full line in the Zenodo source)
              ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul y y') ((Ring19.ring19 LibInt.intR).mul γ (cj γ))) := by rw [this]
        _ = _ := by lib_ring
    generalize (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul x x') γ) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1)))) ((Ring19.ring  -- (+77 chars; full line in the Zenodo source)
    have hγ0 : en γ ≠ 0 := by rw [hγ]; exact fun e => Nat.noConfusion (Int.ofNat.inj e)
    have hWv : (Ring19.ring19 LibInt.intR).mul W v = (Ring19.ring19 LibInt.intR).one := by
      have h0 : (Ring19.ring19 LibInt.intR).mul γ ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul W v) ((Ring19.ring19 LibInt.intR).neg (Ring19.ring19 LibInt.intR).one)) = (Ring19.ring19 LibInt.intR).zero := by
        rw [show (Ring19.ring19 LibInt.intR).mul γ ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul W v) ((Ring19.ring19 LibInt.intR).neg (Ring19.ring19 LibInt.intR).one)) = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul γ W) v) ((Ring19.ring19 LibInt.intR).neg γ) by lib_ring, ← hW, ← hγdev]; lib_ring
      have := cancel_left γ _ hγ0 h0
      calc (Ring19.ring19 LibInt.intR).mul W v = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul W v) ((Ring19.ring19 LibInt.intR).neg (Ring19.ring19 LibInt.intR).one)) (Ring19.ring19 LibInt.intR).one := by lib_ring
        _ = (Ring19.ring19 LibInt.intR).one := by rw [this]; lib_ring
    have hv1 : en v = 1 := by
      have : en W * en v = 1 := by rw [← en_mul, hWv]; rfl
      cases GaussCoprime.int_unit _ _ (by rw [show en v * en W = en W * en v by lib_int]; exact this) with
      | inl h => exact h
      | inr h => exact absurd h (en_nonneg_one v)
    -- norms
    have hN : en d * en e = Int.ofNat (m + 1) * Int.ofNat (n + 1) := by
      have := congrArg en hγdev
      rw [en_mul, en_mul, hv1, hγ] at this
      rw [show en d * en e = en d * en e * 1 by lib_int, ← this]; rfl
    have hdm : ∃ a, Int.ofNat (m + 1) = en d * a := by
      have k1 : Int.ofNat (m + 1) * Int.ofNat (m + 1) = en d * en c1 := by rw [← en_emb, hc1, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en d * en u := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu, en_mul]
      exact ⟨s * en c1 + t * en u, by
        calc Int.ofNat (m + 1) = Int.ofNat (m + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = s * (Int.ofNat (m + 1) * Int.ofNat (m + 1)) + t * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en d * (s * en c1 + t * en u) := by rw [k1, k2]; lib_int⟩
    have hen' : ∃ b, Int.ofNat (n + 1) = en e * b := by
      have k1 : Int.ofNat (n + 1) * Int.ofNat (n + 1) = en e * en c2 := by rw [← en_emb, hc2, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en e * en u' := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu', en_mul]
      exact ⟨t * en c2 + s * en u', by
        calc Int.ofNat (n + 1) = Int.ofNat (n + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = t * (Int.ofNat (n + 1) * Int.ofNat (n + 1)) + s * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en e * (t * en c2 + s * en u') := by rw [k1, k2]; lib_int⟩
    match hdm, hen' with
    | ⟨a, ha⟩, ⟨b, hb⟩ =>
      have hab : a * b = 1 := by
        have h0 : (en d * en e) * (a * b + -1) = 0 := by
          rw [show (en d * en e) * (a * b + -1) = (en d * a) * (en e * b) + -(en d * en e) by lib_int, ← ha, ← hb, hN]; lib_int
        cases int_noZD _ _ h0 with
        | inl e0 => exact absurd (by rw [hN] at e0; exact e0) (fun e1 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = 0 => Nat.noConfusion (Int.ofNat.inj e1))
        | inr e0 => calc a * b = (a * b + -1) + 1 := by lib_int
            _ = 1 := by rw [e0]; rfl
      have pos : ∀ (k : Nat) (z : Int × Int) (c : Int), Int.ofNat (k + 1) = en z * c → c = 1 ∨ c = -1 → c = 1 := fun k z c h hc => by
        cases hc with
        | inl h1 => exact h1
        | inr h1 =>
          exfalso
          rw [h1, ← nrm_cast] at h
          have : Int.ofNat (k + 1) + Int.ofNat (nrm z) = 0 := by rw [h]; lib_int
          have h2 : k + nrm z + 1 = 0 := by rw [show k + nrm z + 1 = k + 1 + nrm z by lib_nat]; exact Int.ofNat.inj (show Int.ofNat (k + 1 + nrm z) = Int.ofNat 0 from this)
          exact Nat.noConfusion h2
      have a1 := pos m d a ha (GaussCoprime.int_unit a b hab)
      have b1 := pos n e b hb (GaussCoprime.int_unit b a (by rw [show b * a = a * b by lib_int]; exact hab))
      refine ⟨d, (Ring19.ring19 LibInt.intR).mul e v, by rw [hγdev]; lib_ring, by rw [ha, a1]; lib_int, by rw [en_mul, hv1, hb, b1]⟩

end Cop19

#print axioms Cop19.en_emb
#print axioms Cop19.emb_mul
#print axioms Cop19.en_dvd
#print axioms Cop19.emb_add
#print axioms Cop19.en_zero_imp
#print axioms Cop19.cancel_left
#print axioms Cop19.en_nonneg_one
#print axioms Cop19.ecoprime_split

#print axioms: 8 theorems, no axioms.

78.289 The primary class of 361a

Residue mod \(\sqrt {-19}\) a nonzero square; conjugation-stable complement of \(\{\pm 1\}\).

\[ H_{19}=\{a+b\theta :\ (a+10b\bmod 19)\ \text {a nonzero square}\}. \]

lang/Prim19.lean

/-
  lang/Prim19 — T48/T50: **the primary class** in ℤ[θ], θ = (1 + √−19)/2: ρ(a + bθ) = a + 10b is the reduction mod √−19;
  H19 = {ρ a nonzero square mod 19}. Closed under products and conjugation, ±α normalisation (19 ∤ N), unit uniqueness.
  (Generated, scratchpad genprim.py.)
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.Cop19
import lang.Sq2Prim8
import lang.W36Mul

namespace Prim19

open FTAChain DetChain LibInt Euc19

def rho (z : Int × Int) : Int := z.1 + 10 * z.2

def Prim19 (z : Int × Int) : Prop := ∃ e : Int, rho z = 19 * e + 1 ∨ rho z = 19 * e + 4 ∨ rho z = 19 * e + 5 ∨ rho z = 19 * e + 6 ∨ rho z = 19 * e + 7 ∨ rho z = 19 * e + 9 ∨ rho z = 19 * e + 11 ∨ rho z = 19 * e + 16 ∨ rho z = 19 * e + 17

theorem rho_mul (x y : Int × Int) : rho ((Ring19.ring19 LibInt.intR).mul x y) = rho x * rho y + -(95 * (x.2 * y.2)) := by
  show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + 10 * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + 10 * x.2) * (y.1 + 10 * y.2) + -(95 * (x.2 * y.2))
  lib_int

theorem rho_neg (z : Int × Int) : rho (-z.1, -z.2) = -rho z := by
  show -z.1 + 10 * -z.2 = -(z.1 + 10 * z.2); lib_int

theorem mulgenD (u v e f w r s D : Int) (hx : u = D * e + r) (hy : v = D * f + s) :
    u * v + -(5 * D * w) = D * (D * e * f + e * s + r * f + -(5 * w)) + r * s := by
  rw [hx, hy]; lib_int

theorem mulgen (u v e f w r s : Int) (hx : u = 19 * e + r) (hy : v = 19 * f + s) :
    u * v + -(95 * w) = 19 * (19 * e * f + e * s + r * f + -(5 * w)) + r * s :=
  mulgenD u v e f w r s 19 hx hy

set_option maxHeartbeats 0 in
theorem mul_0 (u v e f w : Int) (hx : u = 19 * e + 1) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 1 * f + -(5 * w) + 0, (Or.inl (by rw [mulgen u v e f w 1 1 hx hy]; generalize 19 * e * f + e * 1 + 1 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 4 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inl (by rw [mulgen u v e f w 1 4 hx hy]; generalize 19 * e * f + e * 4 + 1 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 5 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 1 5 hx hy]; generalize 19 * e * f + e * 5 + 1 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 6 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 1 6 hx hy]; generalize 19 * e * f + e * 6 + 1 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 7 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 1 7 hx hy]; generalize 19 * e * f + e * 7 + 1 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 9 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 1 9 hx hy]; generalize 19 * e * f + e * 9 + 1 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 11 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 1 11 hx hy]; generalize 19 * e * f + e * 11 + 1 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 16 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 1 16 hx hy]; generalize 19 * e * f + e * 16 + 1 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 17 + 1 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 1 17 hx hy]; generalize 19 * e * f + e * 17 + 1 * f + -(5 * w) = X; lib_int)))))))))⟩

set_option maxHeartbeats 0 in
theorem mul_1 (u v e f w : Int) (hx : u = 19 * e + 4) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 4 * f + -(5 * w) + 0, (Or.inr (Or.inl (by rw [mulgen u v e f w 4 1 hx hy]; generalize 19 * e * f + e * 1 + 4 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 4 + 4 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 4 4 hx hy]; generalize 19 * e * f + e * 4 + 4 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 5 + 4 * f + -(5 * w) + 1, (Or.inl (by rw [mulgen u v e f w 4 5 hx hy]; generalize 19 * e * f + e * 5 + 4 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 6 + 4 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 4 6 hx hy]; generalize 19 * e * f + e * 6 + 4 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 7 + 4 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 4 7 hx hy]; generalize 19 * e * f + e * 7 + 4 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 9 + 4 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 4 9 hx hy]; generalize 19 * e * f + e * 9 + 4 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 11 + 4 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 4 11 hx hy]; generalize 19 * e * f + e * 11 + 4 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 16 + 4 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 4 16 hx hy]; generalize 19 * e * f + e * 16 + 4 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 17 + 4 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 4 17 hx hy]; generalize 19 * e * f + e * 17 + 4 * f + -(5 * w) = X; lib_int))))))))⟩

set_option maxHeartbeats 0 in
theorem mul_2 (u v e f w : Int) (hx : u = 19 * e + 5) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 5 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 5 1 hx hy]; generalize 19 * e * f + e * 1 + 5 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 4 + 5 * f + -(5 * w) + 1, (Or.inl (by rw [mulgen u v e f w 5 4 hx hy]; generalize 19 * e * f + e * 4 + 5 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 5 + 5 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 5 5 hx hy]; generalize 19 * e * f + e * 5 + 5 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 6 + 5 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 5 6 hx hy]; generalize 19 * e * f + e * 6 + 5 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 7 + 5 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 5 7 hx hy]; generalize 19 * e * f + e * 7 + 5 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 9 + 5 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 5 9 hx hy]; generalize 19 * e * f + e * 9 + 5 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 11 + 5 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 5 11 hx hy]; generalize 19 * e * f + e * 11 + 5 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 16 + 5 * f + -(5 * w) + 4, (Or.inr (Or.inl (by rw [mulgen u v e f w 5 16 hx hy]; generalize 19 * e * f + e * 16 + 5 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 17 + 5 * f + -(5 * w) + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 5 17 hx hy]; generalize 19 * e * f + e * 17 + 5 * f + -(5 * w) = X; lib_int)))))))⟩

set_option maxHeartbeats 0 in
theorem mul_3 (u v e f w : Int) (hx : u = 19 * e + 6) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 6 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 6 1 hx hy]; generalize 19 * e * f + e * 1 + 6 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 4 + 6 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 6 4 hx hy]; generalize 19 * e * f + e * 4 + 6 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 5 + 6 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 6 5 hx hy]; generalize 19 * e * f + e * 5 + 6 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 6 + 6 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 6 6 hx hy]; generalize 19 * e * f + e * 6 + 6 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 7 + 6 * f + -(5 * w) + 2, (Or.inr (Or.inl (by rw [mulgen u v e f w 6 7 hx hy]; generalize 19 * e * f + e * 7 + 6 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 9 + 6 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 6 9 hx hy]; generalize 19 * e * f + e * 9 + 6 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 11 + 6 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 6 11 hx hy]; generalize 19 * e * f + e * 11 + 6 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 16 + 6 * f + -(5 * w) + 5, (Or.inl (by rw [mulgen u v e f w 6 16 hx hy]; generalize 19 * e * f + e * 16 + 6 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 17 + 6 * f + -(5 * w) + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 6 17 hx hy]; generalize 19 * e * f + e * 17 + 6 * f + -(5 * w) = X; lib_int))))))⟩

set_option maxHeartbeats 0 in
theorem mul_4 (u v e f w : Int) (hx : u = 19 * e + 7) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 7 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 7 1 hx hy]; generalize 19 * e * f + e * 1 + 7 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 4 + 7 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 7 4 hx hy]; generalize 19 * e * f + e * 4 + 7 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 5 + 7 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 7 5 hx hy]; generalize 19 * e * f + e * 5 + 7 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 6 + 7 * f + -(5 * w) + 2, (Or.inr (Or.inl (by rw [mulgen u v e f w 7 6 hx hy]; generalize 19 * e * f + e * 6 + 7 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 7 + 7 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 7 7 hx hy]; generalize 19 * e * f + e * 7 + 7 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 9 + 7 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 7 9 hx hy]; generalize 19 * e * f + e * 9 + 7 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 11 + 7 * f + -(5 * w) + 4, (Or.inl (by rw [mulgen u v e f w 7 11 hx hy]; generalize 19 * e * f + e * 11 + 7 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 16 + 7 * f + -(5 * w) + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 7 16 hx hy]; generalize 19 * e * f + e * 16 + 7 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 17 + 7 * f + -(5 * w) + 6, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 7 17 hx hy]; generalize 19 * e * f + e * 17 + 7 * f + -(5 * w) = X; lib_int))))⟩

set_option maxHeartbeats 0 in
theorem mul_5 (u v e f w : Int) (hx : u = 19 * e + 9) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 9 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 9 1 hx hy]; generalize 19 * e * f + e * 1 + 9 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 4 + 9 * f + -(5 * w) + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 9 4 hx hy]; generalize 19 * e * f + e * 4 + 9 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 5 + 9 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 9 5 hx hy]; generalize 19 * e * f + e * 5 + 9 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 6 + 9 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 9 6 hx hy]; generalize 19 * e * f + e * 6 + 9 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 7 + 9 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 9 7 hx hy]; generalize 19 * e * f + e * 7 + 9 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 9 + 9 * f + -(5 * w) + 4, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 9 9 hx hy]; generalize 19 * e * f + e * 9 + 9 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 11 + 9 * f + -(5 * w) + 5, (Or.inr (Or.inl (by rw [mulgen u v e f w 9 11 hx hy]; generalize 19 * e * f + e * 11 + 9 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 16 + 9 * f + -(5 * w) + 7, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 9 16 hx hy]; generalize 19 * e * f + e * 16 + 9 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 17 + 9 * f + -(5 * w) + 8, (Or.inl (by rw [mulgen u v e f w 9 17 hx hy]; generalize 19 * e * f + e * 17 + 9 * f + -(5 * w) = X; lib_int))⟩

set_option maxHeartbeats 0 in
theorem mul_6 (u v e f w : Int) (hx : u = 19 * e + 11) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 11 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 11 1 hx hy]; generalize 19 * e * f + e * 1 + 11 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 4 + 11 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 11 4 hx hy]; generalize 19 * e * f + e * 4 + 11 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 5 + 11 * f + -(5 * w) + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 11 5 hx hy]; generalize 19 * e * f + e * 5 + 11 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 6 + 11 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 11 6 hx hy]; generalize 19 * e * f + e * 6 + 11 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 7 + 11 * f + -(5 * w) + 4, (Or.inl (by rw [mulgen u v e f w 11 7 hx hy]; generalize 19 * e * f + e * 7 + 11 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 9 + 11 * f + -(5 * w) + 5, (Or.inr (Or.inl (by rw [mulgen u v e f w 11 9 hx hy]; generalize 19 * e * f + e * 9 + 11 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 11 + 11 * f + -(5 * w) + 6, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 11 11 hx hy]; generalize 19 * e * f + e * 11 + 11 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 16 + 11 * f + -(5 * w) + 9, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 11 16 hx hy]; generalize 19 * e * f + e * 16 + 11 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 17 + 11 * f + -(5 * w) + 9, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 11 17 hx hy]; generalize 19 * e * f + e * 17 + 11 * f + -(5 * w) = X; lib_int)))))))))⟩

set_option maxHeartbeats 0 in
theorem mul_7 (u v e f w : Int) (hx : u = 19 * e + 16) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 16 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 16 1 hx hy]; generalize 19 * e * f + e * 1 + 16 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 4 + 16 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 16 4 hx hy]; generalize 19 * e * f + e * 4 + 16 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 5 + 16 * f + -(5 * w) + 4, (Or.inr (Or.inl (by rw [mulgen u v e f w 16 5 hx hy]; generalize 19 * e * f + e * 5 + 16 * f + -(5 * w) = X; lib_int)))⟩
  · exact ⟨19 * e * f + e * 6 + 16 * f + -(5 * w) + 5, (Or.inl (by rw [mulgen u v e f w 16 6 hx hy]; generalize 19 * e * f + e * 6 + 16 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 7 + 16 * f + -(5 * w) + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 16 7 hx hy]; generalize 19 * e * f + e * 7 + 16 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 9 + 16 * f + -(5 * w) + 7, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 16 9 hx hy]; generalize 19 * e * f + e * 9 + 16 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 11 + 16 * f + -(5 * w) + 9, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 16 11 hx hy]; generalize 19 * e * f + e * 11 + 16 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 16 + 16 * f + -(5 * w) + 13, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 16 16 hx hy]; generalize 19 * e * f + e * 16 + 16 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 17 + 16 * f + -(5 * w) + 14, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 16 17 hx hy]; generalize 19 * e * f + e * 17 + 16 * f + -(5 * w) = X; lib_int)))))⟩

set_option maxHeartbeats 0 in
theorem mul_8 (u v e f w : Int) (hx : u = 19 * e + 17) (hy : v = 19 * f + 1 ∨ v = 19 * f + 4 ∨ v = 19 * f + 5 ∨ v = 19 * f + 6 ∨ v = 19 * f + 7 ∨ v = 19 * f + 9 ∨ v = 19 * f + 11 ∨ v = 19 * f + 16 ∨ v = 19 * f + 17) :
    ∃ E : Int, u * v + -(95 * w) = 19 * E + 1 ∨ u * v + -(95 * w) = 19 * E + 4 ∨ u * v + -(95 * w) = 19 * E + 5 ∨ u * v + -(95 * w) = 19 * E + 6 ∨ u * v + -(95 * w) = 19 * E + 7 ∨ u * v + -(95 * w) = 19 * E + 9 ∨ u * v + -(95 * w) = 19 * E + 11 ∨ u * v + -(95 * w) = 19 * E + 16 ∨ u * v + -(95 * w) = 19 * E + 17 := by
  rcases hy with hy | hy | hy | hy | hy | hy | hy | hy | hy
  · exact ⟨19 * e * f + e * 1 + 17 * f + -(5 * w) + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [mulgen u v e f w 17 1 hx hy]; generalize 19 * e * f + e * 1 + 17 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 4 + 17 * f + -(5 * w) + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 17 4 hx hy]; generalize 19 * e * f + e * 4 + 17 * f + -(5 * w) = X; lib_int))))))))⟩
  · exact ⟨19 * e * f + e * 5 + 17 * f + -(5 * w) + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 17 5 hx hy]; generalize 19 * e * f + e * 5 + 17 * f + -(5 * w) = X; lib_int)))))))⟩
  · exact ⟨19 * e * f + e * 6 + 17 * f + -(5 * w) + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 17 6 hx hy]; generalize 19 * e * f + e * 6 + 17 * f + -(5 * w) = X; lib_int))))))⟩
  · exact ⟨19 * e * f + e * 7 + 17 * f + -(5 * w) + 6, (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 17 7 hx hy]; generalize 19 * e * f + e * 7 + 17 * f + -(5 * w) = X; lib_int))))⟩
  · exact ⟨19 * e * f + e * 9 + 17 * f + -(5 * w) + 8, (Or.inl (by rw [mulgen u v e f w 17 9 hx hy]; generalize 19 * e * f + e * 9 + 17 * f + -(5 * w) = X; lib_int))⟩
  · exact ⟨19 * e * f + e * 11 + 17 * f + -(5 * w) + 9, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 17 11 hx hy]; generalize 19 * e * f + e * 11 + 17 * f + -(5 * w) = X; lib_int)))))))))⟩
  · exact ⟨19 * e * f + e * 16 + 17 * f + -(5 * w) + 14, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [mulgen u v e f w 17 16 hx hy]; generalize 19 * e * f + e * 16 + 17 * f + -(5 * w) = X; lib_int)))))⟩
  · exact ⟨19 * e * f + e * 17 + 17 * f + -(5 * w) + 15, (Or.inr (Or.inl (by rw [mulgen u v e f w 17 17 hx hy]; generalize 19 * e * f + e * 17 + 17 * f + -(5 * w) = X; lib_int)))⟩

theorem prim_mul (x y : Int × Int) (hx : Prim19 x) (hy : Prim19 y) : Prim19 ((Ring19.ring19 LibInt.intR).mul x y) := by
  obtain ⟨e, hx⟩ := hx
  obtain ⟨f, hy⟩ := hy
  rw [Prim19, rho_mul]
  generalize x.2 * y.2 = w
  rcases hx with hx | hx | hx | hx | hx | hx | hx | hx | hx
  · exact mul_0 _ _ e f w hx hy
  · exact mul_1 _ _ e f w hx hy
  · exact mul_2 _ _ e f w hx hy
  · exact mul_3 _ _ e f w hx hy
  · exact mul_4 _ _ e f w hx hy
  · exact mul_5 _ _ e f w hx hy
  · exact mul_6 _ _ e f w hx hy
  · exact mul_7 _ _ e f w hx hy
  · exact mul_8 _ _ e f w hx hy

theorem prim_cj (z : Int × Int) (hz : Prim19 z) : Prim19 (cj z) := by
  obtain ⟨e, h⟩ := hz
  have hr : rho (cj z) = rho z + -(19 * z.2) := by show z.1 + z.2 + 10 * -z.2 = z.1 + 10 * z.2 + -(19 * z.2); lib_int
  refine ⟨e + -z.2, ?_⟩
  rw [hr]
  rcases h with h | h | h | h | h | h | h | h | h
  · exact (Or.inl (by rw [h]; lib_int))
  · exact (Or.inr (Or.inl (by rw [h]; lib_int)))
  · exact (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int)))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int))))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int)))))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int))))))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [h]; lib_int)))))))))
  · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [h]; lib_int)))))))))

theorem unit_pm (u : Int × Int) (h : en u = 1) : u = (Ring19.ring19 LibInt.intR).one ∨ u = (-1, 0) := by
  have h4 := four_nrm u
  have hn : nrm u = 1 := Int.ofNat.inj (by rw [nrm_cast, h]; rfl)
  rw [hn] at h4
  have hB : Int.natAbs u.2 * Int.natAbs u.2 = 0 := by
    cases hb : Int.natAbs u.2 * Int.natAbs u.2 with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 19 * (t + 1) = (Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 19 * t + 15) + 4 by lib_nat] at h4
                exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h4))))
  have b0 : u.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at h4
  have hT : Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) = 2 * 2 := h4.symm
  have hT2 : Int.natAbs (2 * u.1 + u.2) = 2 := by
    match hx : Int.natAbs (2 * u.1 + u.2) with
    | 0 => rw [hx] at hT; exact Nat.noConfusion hT
    | 1 => rw [hx] at hT; exact Nat.noConfusion (Nat.succ.inj hT)
    | 2 => rfl
    | t + 3 => rw [hx, show (t + 3) * (t + 3) = (t * t + 6 * t + 5) + 4 by lib_nat] at hT; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hT))))
  rw [b0] at hT2
  obtain ⟨a, b⟩ := u
  show (a, b) = (1, 0) ∨ (a, b) = (-1, 0)
  have b0' : b = 0 := b0
  rcases EisEuclid.sgn (2 * a + 0) with s | s <;> rw [hT2] at s
  · exact Or.inl (Prod.ext (W36Mul.two_cancel_int a 1 (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')
  · exact Or.inr (Prod.ext (W36Mul.two_cancel_int a (-1) (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')

theorem neg_mul' (z : Int × Int) : (Ring19.ring19 LibInt.intR).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -(0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2) = -z.1; lib_int) (by show -1 * z.2 + 0 * z.1 + 0 * z.2 = -z.2; lib_int)

set_option maxHeartbeats 0 in
theorem normalize (α : Int × Int) (hd : ∀ k, en α ≠ 19 * k) : ∃ u, en u = 1 ∧ Prim19 ((Ring19.ring19 LibInt.intR).mul u α) := by
  obtain ⟨q, r, hq, hr⟩ := GaussEuclid.rnd (rho α) 18
  have hq' : rho α = 19 * q + r := by rw [hq]; show q * 19 + r = 19 * q + r; lib_int
  have plus : ∀ e, (rho α = 19 * e + 1 ∨ rho α = 19 * e + 4 ∨ rho α = 19 * e + 5 ∨ rho α = 19 * e + 6 ∨ rho α = 19 * e + 7 ∨ rho α = 19 * e + 9 ∨ rho α = 19 * e + 11 ∨ rho α = 19 * e + 16 ∨ rho α = 19 * e + 17) → ∃ u, en u = 1 ∧ Prim19 ((Ring19.ring19 LibInt.intR).mul u α) :=
    fun e he => ⟨(Ring19.ring19 LibInt.intR).one, rfl, by rw [(Ring19.ring19 LibInt.intR).one_mul]; exact ⟨e, he⟩⟩
  have minus : ∀ e, (-rho α = 19 * e + 1 ∨ -rho α = 19 * e + 4 ∨ -rho α = 19 * e + 5 ∨ -rho α = 19 * e + 6 ∨ -rho α = 19 * e + 7 ∨ -rho α = 19 * e + 9 ∨ -rho α = 19 * e + 11 ∨ -rho α = 19 * e + 16 ∨ -rho α = 19 * e + 17) → ∃ u, en u = 1 ∧ Prim19 ((Ring19.ring19 LibInt.intR).mul u α) :=
    fun e he => ⟨(-1, 0), rfl, by rw [neg_mul']; exact ⟨e, by rw [rho_neg]; exact he⟩⟩
  cases r with
  | ofNat t =>
    match t, hr with
    | 0, _ =>
      exfalso
      obtain ⟨a, b⟩ := α
      have ha : a = 19 * q + -(10 * b) := by
        have : a + 10 * b = 19 * q + Int.ofNat 0 := hq'
        calc a = (a + 10 * b) + -(10 * b) := by lib_int
          _ = _ := by rw [this]; show 19 * q + 0 + -(10 * b) = _; lib_int
      exact hd (19 * q * q + -(19 * q * b) + 5 * b * b) (by show a * a + a * b + 5 * (b * b) = _; rw [ha]; lib_int)
    | 1, _ => exact plus q (Or.inl (hq'))
    | 2, _ => exact minus (-q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [hq']; show -(19 * q + 2) = _; lib_int)))))))))
    | 3, _ => exact minus (-q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(19 * q + 3) = _; lib_int)))))))))
    | 4, _ => exact plus q (Or.inr (Or.inl (hq')))
    | 5, _ => exact plus q (Or.inr (Or.inr (Or.inl (hq'))))
    | 6, _ => exact plus q (Or.inr (Or.inr (Or.inr (Or.inl (hq')))))
    | 7, _ => exact plus q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (hq'))))))
    | 8, _ => exact minus (-q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(19 * q + 8) = _; lib_int))))))))
    | 9, _ => exact plus q (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (hq')))))))
    | t + 10, hr' =>
      exfalso
      have : 2 * (t + 10) ≤ 19 := hr'
      rw [show 2 * (t + 10) = 2 * t + 1 + 19 by lib_nat] at this
      exact Nat.not_succ_le_self 19 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 20) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  | negSucc t =>
    match t, hr with
    | 0, _ => exact minus (-q) (Or.inl (by rw [hq']; show -(19 * q + -1) = _; lib_int))
    | 1, _ => exact plus (q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [hq']; show 19 * q + -2 = _; lib_int)))))))))
    | 2, _ => exact plus (q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show 19 * q + -3 = _; lib_int)))))))))
    | 3, _ => exact minus (-q) (Or.inr (Or.inl (by rw [hq']; show -(19 * q + -4) = _; lib_int)))
    | 4, _ => exact minus (-q) (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(19 * q + -5) = _; lib_int))))
    | 5, _ => exact minus (-q) (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(19 * q + -6) = _; lib_int)))))
    | 6, _ => exact minus (-q) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(19 * q + -7) = _; lib_int))))))
    | 7, _ => exact plus (q + -1) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show 19 * q + -8 = _; lib_int))))))))
    | 8, _ => exact minus (-q) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [hq']; show -(19 * q + -9) = _; lib_int)))))))
    | t + 9, hr' =>
      exfalso
      have : 2 * (t + 10) ≤ 19 := hr'
      rw [show 2 * (t + 10) = 2 * t + 1 + 19 by lib_nat] at this
      exact Nat.not_succ_le_self 19 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 20) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))

set_option maxHeartbeats 0 in
theorem one_0 (x e f : Int) (h1 : x = 19 * e + 1) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 16 1 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 1) + 17 := by lib_int
        _ = x + -x + 17 := by rw [← h1, ← h2]
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 13 4 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 4) + 14 := by lib_int
        _ = x + -x + 14 := by rw [← h1, ← h2]
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 12 5 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 5) + 13 := by lib_int
        _ = x + -x + 13 := by rw [← h1, ← h2]
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 11 6 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 6) + 12 := by lib_int
        _ = x + -x + 12 := by rw [← h1, ← h2]
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 10 7 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 7) + 11 := by lib_int
        _ = x + -x + 11 := by rw [← h1, ← h2]
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 8 9 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 9) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 6 11 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 11) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 1 16 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 16) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 0 17 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 1) + (19 * f + 17) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_1 (x e f : Int) (h1 : x = 19 * e + 4) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 13 4 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 1) + 14 := by lib_int
        _ = x + -x + 14 := by rw [← h1, ← h2]
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 10 7 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 4) + 11 := by lib_int
        _ = x + -x + 11 := by rw [← h1, ← h2]
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 9 8 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 5) + 10 := by lib_int
        _ = x + -x + 10 := by rw [← h1, ← h2]
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 8 9 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 6) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 7 10 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 7) + 8 := by lib_int
        _ = x + -x + 8 := by rw [← h1, ← h2]
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 5 12 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 9) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 3 14 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 4) + (19 * f + 11) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 17 0 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 4) + (19 * f + 16) + 18 := by lib_int
        _ = x + -x + 18 := by rw [← h1, ← h2]
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 16 1 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 4) + (19 * f + 17) + 17 := by lib_int
        _ = x + -x + 17 := by rw [← h1, ← h2]
        _ = 17 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_2 (x e f : Int) (h1 : x = 19 * e + 5) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 12 5 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 1) + 13 := by lib_int
        _ = x + -x + 13 := by rw [← h1, ← h2]
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 9 8 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 4) + 10 := by lib_int
        _ = x + -x + 10 := by rw [← h1, ← h2]
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 8 9 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 5) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 7 10 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 6) + 8 := by lib_int
        _ = x + -x + 8 := by rw [← h1, ← h2]
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 6 11 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 7) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 4 13 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 9) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 2 15 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 5) + (19 * f + 11) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 16 1 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 5) + (19 * f + 16) + 17 := by lib_int
        _ = x + -x + 17 := by rw [← h1, ← h2]
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 15 2 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 5) + (19 * f + 17) + 16 := by lib_int
        _ = x + -x + 16 := by rw [← h1, ← h2]
        _ = 16 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_3 (x e f : Int) (h1 : x = 19 * e + 6) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 11 6 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 1) + 12 := by lib_int
        _ = x + -x + 12 := by rw [← h1, ← h2]
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 8 9 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 4) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 7 10 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 5) + 8 := by lib_int
        _ = x + -x + 8 := by rw [← h1, ← h2]
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 6 11 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 6) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 5 12 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 7) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 3 14 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 9) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 1 16 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 6) + (19 * f + 11) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 15 2 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 6) + (19 * f + 16) + 16 := by lib_int
        _ = x + -x + 16 := by rw [← h1, ← h2]
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 14 3 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 6) + (19 * f + 17) + 15 := by lib_int
        _ = x + -x + 15 := by rw [← h1, ← h2]
        _ = 15 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_4 (x e f : Int) (h1 : x = 19 * e + 7) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 10 7 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 1) + 11 := by lib_int
        _ = x + -x + 11 := by rw [← h1, ← h2]
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 7 10 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 4) + 8 := by lib_int
        _ = x + -x + 8 := by rw [← h1, ← h2]
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 6 11 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 5) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 5 12 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 6) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 4 13 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 7) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 2 15 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 9) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 0 17 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 7) + (19 * f + 11) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 14 3 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 7) + (19 * f + 16) + 15 := by lib_int
        _ = x + -x + 15 := by rw [← h1, ← h2]
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 13 4 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 7) + (19 * f + 17) + 14 := by lib_int
        _ = x + -x + 14 := by rw [← h1, ← h2]
        _ = 14 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_5 (x e f : Int) (h1 : x = 19 * e + 9) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 8 9 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 9) + (19 * f + 1) + 9 := by lib_int
        _ = x + -x + 9 := by rw [← h1, ← h2]
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 5 12 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 9) + (19 * f + 4) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 4 13 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 9) + (19 * f + 5) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 3 14 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 9) + (19 * f + 6) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 2 15 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 9) + (19 * f + 7) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 0 17 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 9) + (19 * f + 9) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 17 0 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 9) + (19 * f + 11) + 18 := by lib_int
        _ = x + -x + 18 := by rw [← h1, ← h2]
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 12 5 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 9) + (19 * f + 16) + 13 := by lib_int
        _ = x + -x + 13 := by rw [← h1, ← h2]
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 11 6 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 9) + (19 * f + 17) + 12 := by lib_int
        _ = x + -x + 12 := by rw [← h1, ← h2]
        _ = 12 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_6 (x e f : Int) (h1 : x = 19 * e + 11) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 6 11 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 11) + (19 * f + 1) + 7 := by lib_int
        _ = x + -x + 7 := by rw [← h1, ← h2]
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 3 14 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 11) + (19 * f + 4) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 2 15 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 11) + (19 * f + 5) + 3 := by lib_int
        _ = x + -x + 3 := by rw [← h1, ← h2]
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 1 16 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 11) + (19 * f + 6) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 0 17 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 11) + (19 * f + 7) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 17 0 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 11) + (19 * f + 9) + 18 := by lib_int
        _ = x + -x + 18 := by rw [← h1, ← h2]
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 15 2 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 11) + (19 * f + 11) + 16 := by lib_int
        _ = x + -x + 16 := by rw [← h1, ← h2]
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 10 7 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 11) + (19 * f + 16) + 11 := by lib_int
        _ = x + -x + 11 := by rw [← h1, ← h2]
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 9 8 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 11) + (19 * f + 17) + 10 := by lib_int
        _ = x + -x + 10 := by rw [← h1, ← h2]
        _ = 10 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_7 (x e f : Int) (h1 : x = 19 * e + 16) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 1 16 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 16) + (19 * f + 1) + 2 := by lib_int
        _ = x + -x + 2 := by rw [← h1, ← h2]
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 17 0 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 4) + 18 := by lib_int
        _ = x + -x + 18 := by rw [← h1, ← h2]
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 16 1 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 5) + 17 := by lib_int
        _ = x + -x + 17 := by rw [← h1, ← h2]
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 15 2 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 6) + 16 := by lib_int
        _ = x + -x + 16 := by rw [← h1, ← h2]
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 14 3 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 7) + 15 := by lib_int
        _ = x + -x + 15 := by rw [← h1, ← h2]
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 12 5 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 9) + 13 := by lib_int
        _ = x + -x + 13 := by rw [← h1, ← h2]
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 10 7 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 11) + 11 := by lib_int
        _ = x + -x + 11 := by rw [← h1, ← h2]
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 5 12 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 16) + 6 := by lib_int
        _ = x + -x + 6 := by rw [← h1, ← h2]
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 4 13 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 16) + (19 * f + 17) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem one_8 (x e f : Int) (h1 : x = 19 * e + 17) (h2 : -x = 19 * f + 1 ∨ -x = 19 * f + 4 ∨ -x = 19 * f + 5 ∨ -x = 19 * f + 6 ∨ -x = 19 * f + 7 ∨ -x = 19 * f + 9 ∨ -x = 19 * f + 11 ∨ -x = 19 * f + 16 ∨ -x = 19 * f + 17) : False := by
  rcases h2 with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2
  · exact Sq2Prim8.noM 18 (e + f + -(-1)) 0 17 (by calc (19 : Int) * (e + f + -(-1)) = (19 * e + 17) + (19 * f + 1) + 1 := by lib_int
        _ = x + -x + 1 := by rw [← h1, ← h2]
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 16 1 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 4) + 17 := by lib_int
        _ = x + -x + 17 := by rw [← h1, ← h2]
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 15 2 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 5) + 16 := by lib_int
        _ = x + -x + 16 := by rw [← h1, ← h2]
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 14 3 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 6) + 15 := by lib_int
        _ = x + -x + 15 := by rw [← h1, ← h2]
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 13 4 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 7) + 14 := by lib_int
        _ = x + -x + 14 := by rw [← h1, ← h2]
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 11 6 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 9) + 12 := by lib_int
        _ = x + -x + 12 := by rw [← h1, ← h2]
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 9 8 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 11) + 10 := by lib_int
        _ = x + -x + 10 := by rw [← h1, ← h2]
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 4 13 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 16) + 5 := by lib_int
        _ = x + -x + 5 := by rw [← h1, ← h2]
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (e + f + -(-2)) 3 14 (by calc (19 : Int) * (e + f + -(-2)) = (19 * e + 17) + (19 * f + 17) + 4 := by lib_int
        _ = x + -x + 4 := by rw [← h1, ← h2]
        _ = 4 := by lib_int) rfl

theorem unit_one (u α : Int × Int) (hu : en u = 1) (pα : Prim19 α) (pu : Prim19 ((Ring19.ring19 LibInt.intR).mul u α)) : u = (Ring19.ring19 LibInt.intR).one := by
  rcases unit_pm u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, neg_mul'] at pu
  obtain ⟨e, h1⟩ := pα
  obtain ⟨f, h2⟩ := pu
  rw [rho_neg] at h2
  generalize rho α = x at h1 h2
  rcases h1 with h1 | h1 | h1 | h1 | h1 | h1 | h1 | h1 | h1
  · exact one_0 x e f h1 h2
  · exact one_1 x e f h1 h2
  · exact one_2 x e f h1 h2
  · exact one_3 x e f h1 h2
  · exact one_4 x e f h1 h2
  · exact one_5 x e f h1 h2
  · exact one_6 x e f h1 h2
  · exact one_7 x e f h1 h2
  · exact one_8 x e f h1 h2

end Prim19

#print axioms Prim19.rho_mul
#print axioms Prim19.rho_neg
#print axioms Prim19.mulgenD
#print axioms Prim19.mulgen
#print axioms Prim19.mul_0
#print axioms Prim19.mul_1
#print axioms Prim19.mul_2
#print axioms Prim19.mul_3
#print axioms Prim19.mul_4
#print axioms Prim19.mul_5
#print axioms Prim19.mul_6
#print axioms Prim19.mul_7
#print axioms Prim19.mul_8
#print axioms Prim19.prim_mul
#print axioms Prim19.prim_cj
#print axioms Prim19.unit_pm
#print axioms Prim19.neg_mul'
#print axioms Prim19.normalize
#print axioms Prim19.one_0
#print axioms Prim19.one_1
#print axioms Prim19.one_2
#print axioms Prim19.one_3
#print axioms Prim19.one_4
#print axioms Prim19.one_5
#print axioms Prim19.one_6
#print axioms Prim19.one_7
#print axioms Prim19.one_8
#print axioms Prim19.unit_one

#print axioms: 28 theorems, no axioms.

78.290 Unique splitting into \(H_{19}\)

Unique splitting into \(H_{19}\)-elements by coprime norms prime to \(19\).

\[ \gamma =\alpha \beta \ \text {unique}. \]

lang/Uniq19.lean

/-
  lang/Uniq19 — T48/T50, cycle 661: the unique splitting into H19-elements (Prim19) in ℤ[(1+√−19)/2]:
    prim19_quot, uniq_split19, prim19_split   (norms prime to 19; port of Sq2Unique)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Prim19

namespace Uniq19

open FTAChain DetChain LibInt Euc19 Prim19

/-- y ∈ H19 when x and xy are (19 ∤ N(y)). -/
theorem prim19_quot (x y : Int × Int) (hy : ∀ k, en y ≠ 19 * k) (hx : Prim19 x) (hxy : Prim19 ((Ring19.ring19 LibInt.intR).mul x y)) : Prim19 y := by
  obtain ⟨u, hu, hpu⟩ := normalize y hy
  have h1 : Prim19 ((Ring19.ring19 LibInt.intR).mul u ((Ring19.ring19 LibInt.intR).mul x y)) := by
    rw [show (Ring19.ring19 LibInt.intR).mul u ((Ring19.ring19 LibInt.intR).mul x y) = (Ring19.ring19 LibInt.intR).mul x ((Ring19.ring19 LibInt.intR).mul u y) by lib_ring]
    exact prim_mul _ _ hx hpu
  have hu1 := unit_one u _ hu hxy h1
  rw [hu1, (Ring19.ring19 LibInt.intR).one_mul] at hpu
  exact hpu

/-- **Uniqueness of the primary splitting.** -/
theorem uniq_split19 (α β α' β' : Int × Int) (M N s t : Int) (hα : en α = M) (hα' : en α' = M) (hβ' : en β' = N)
    (hst : s * M + t * N = 1) (hM : M ≠ 0) (pα : Prim19 α) (pα' : Prim19 α') (h : (Ring19.ring19 LibInt.intR).mul α β = (Ring19.ring19 LibInt.intR).mul α' β') :
    α = α' ∧ β = β' := by
  have hco : (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (Cop19.emb s) (cj α)) α) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (Cop19.emb t) (cj β')) β') = (Ring19.ring19 LibInt.intR).one := by
    rw [show (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (Cop19.emb s) (cj α)) α) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (Cop19.emb t) (cj β')) β') = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (Cop19.emb s) ((Ring19.ring19 LibInt.intR).mul α (cj α))) ((Ring19.ring19 LibInt.intR).mul (Cop19.emb t)  -- (+59 chars; full line in the Zenodo source)
      mul_cj, mul_cj, hα, hβ']
    show (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (Cop19.emb s) (Cop19.emb M)) ((Ring19.ring19 LibInt.intR).mul (Cop19.emb t) (Cop19.emb N)) = _
    rw [Cop19.emb_mul, Cop19.emb_mul, ← Cop19.emb_add, hst]; rfl
  match eeuclid α β' α' _ _ hco ⟨β, by rw [show (Ring19.ring19 LibInt.intR).mul β' α' = (Ring19.ring19 LibInt.intR).mul α' β' by lib_ring, ← h]⟩ with
  | ⟨w, hw⟩ =>
    have hgw : en w = 1 := by
      have e : M * en w = M * 1 := by
        calc M * en w = en α * en w := by rw [hα]
          _ = en α' := by rw [← en_mul, ← hw]
          _ = M * 1 := by rw [hα']; lib_int
      have e0 : M * (en w + -1) = 0 := by rw [show M * (en w + -1) = M * en w + -(M * 1) by lib_int, e]; lib_int
      cases int_noZD _ _ e0 with
      | inl h0 => exact absurd h0 hM
      | inr h0 => calc en w = (en w + -1) + 1 := by lib_int
          _ = 1 := by rw [h0]; rfl
    have hw1 : w = (Ring19.ring19 LibInt.intR).one :=
      unit_one w α hgw pα (by rw [show (Ring19.ring19 LibInt.intR).mul w α = α' by rw [hw]; lib_ring]; exact pα')
    have hαα : α = α' := by rw [hw, hw1]; lib_ring
    refine ⟨hαα, ?_⟩
    have h0 : (Ring19.ring19 LibInt.intR).mul α ((Ring19.ring19 LibInt.intR).add β ((Ring19.ring19 LibInt.intR).neg β')) = (Ring19.ring19 LibInt.intR).zero := by
      rw [show (Ring19.ring19 LibInt.intR).mul α ((Ring19.ring19 LibInt.intR).add β ((Ring19.ring19 LibInt.intR).neg β')) = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul α β) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul α β')) by lib_ring, h, ← hαα]; lib_ring
    have := Cop19.cancel_left α _ (by rw [hα]; exact hM) h0
    calc β = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).add β ((Ring19.ring19 LibInt.intR).neg β')) β' := by lib_ring
      _ = β' := by rw [this]; lib_ring

/-- **Existence of the primary splitting.** -/
theorem prim19_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) (hm : ∀ k, Int.ofNat (m + 1) ≠ 19 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 19 * k)
    (pγ : Prim19 γ) : ∃ α β, γ = (Ring19.ring19 LibInt.intR).mul α β ∧ Prim19 α ∧ Prim19 β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match Cop19.ecoprime_split γ m n s t hγ hst with
  | ⟨α0, β0, h0, hα0, hβ0⟩ =>
    match normalize α0 (by rw [hα0]; exact hm) with
    | ⟨u, gu, pu⟩ =>
      have uc : (Ring19.ring19 LibInt.intR).mul u (cj u) = (Ring19.ring19 LibInt.intR).one := by rw [mul_cj, gu]; rfl
      have hβn : en ((Ring19.ring19 LibInt.intR).mul (cj u) β0) = Int.ofNat (n + 1) := by
        rw [en_mul, show en (cj u) = en u from en_cj u, gu, hβ0]; lib_int
      have hsplit : γ = (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul u α0) ((Ring19.ring19 LibInt.intR).mul (cj u) β0) := by
        rw [h0, show (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul u α0) ((Ring19.ring19 LibInt.intR).mul (cj u) β0) = (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul u (cj u)) ((Ring19.ring19 LibInt.intR).mul α0 β0) by lib_ring, uc]; lib_ring
      refine ⟨(Ring19.ring19 LibInt.intR).mul u α0, (Ring19.ring19 LibInt.intR).mul (cj u) β0, hsplit, pu,
        prim19_quot _ _ (by rw [hβn]; exact hn) pu (by rw [← hsplit]; exact pγ),
        by rw [en_mul, gu, hα0]; lib_int, hβn⟩

end Uniq19

#print axioms Uniq19.prim19_quot
#print axioms Uniq19.uniq_split19
#print axioms Uniq19.prim19_split

#print axioms: 3 theorems, no axioms.

78.291 361a: multiplicativity

Theta coefficients over \(H_{19}\) are multiplicative.

\[ c_w(mn)=c_w(m)c_w(n). \]

lang/HeckeMult19.lean

/-
  lang/HeckeMult19 — T48/T50, cycle 662: **the theta series of 361a's Hecke character class is multiplicative**: for every
  multiplicative w on ℤ[θ], θ = (1 + √−19)/2,  c_w(n) = Σ_{α ∈ H19, N(α) = n} w(α)  satisfies c_w(mn) = c_w(m)c_w(n) for coprime m, n
  prime to 19.  Enumeration box19 n = HeckeMult.box (20n).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Uniq19
import lang.HeckeMultW

namespace HeckeMult19

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Euc19 Prim19 Uniq19

abbrev box19 (n : Nat) : List (Int × Int) := HeckeMult.box (20 * n)

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 20n  (19a² + (a + 10b)² = 20N, (2a + b)² + 19b² = 4N). -/
theorem abs_le19 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 20 * n ∧ Int.natAbs z.2 ≤ 20 * n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have h4 := four_nrm z
  rw [hn] at h4
  have h8 : 19 * (Int.natAbs z.1 * Int.natAbs z.1) + Int.natAbs (z.1 + 10 * z.2) * Int.natAbs (z.1 + 10 * z.2) = 20 * n := Int.ofNat.inj (by
    show 19 * Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs (z.1 + 10 * z.2) * Int.natAbs (z.1 + 10 * z.2)) = 20 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; unfold en; lib_int)
  have seven : ∀ X : Nat, X ≤ 19 * (X * X) := fun X => Nat.le_trans (HeckeMult.le_sq X) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 18)))
  constructor
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h8))
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans (Nat.le_of_eq h4.symm) (Nat.mul_le_mul_right n (Nat.le_add_right 4 16))))

def PD19 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim19 z

theorem prim19_iff (z : Int × Int) :
    (ZMod.md 18 (Int.natAbs (rho z - 1)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 4)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 5)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 6)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 7)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 9)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 11)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 16)) = 0 ∨ ZMod.md 18 (Int.natAbs (rho z - 17)) = 0) ↔ Prim19 z := b  -- (+1 chars; full line in the Zenodo source)
  constructor
  · intro h
    rcases h with h | h | h | h | h | h | h | h | h
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inl (by calc rho z = (rho z - 1) + 1 := by lib_int
          _ = _ := by rw [he]; rfl))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inl (by calc rho z = (rho z - 4) + 4 := by lib_int
          _ = _ := by rw [he]; rfl)))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 5) + 5 := by lib_int
          _ = _ := by rw [he]; rfl))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 6) + 6 := by lib_int
          _ = _ := by rw [he]; rfl)))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 7) + 7 := by lib_int
          _ = _ := by rw [he]; rfl))))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 9) + 9 := by lib_int
          _ = _ := by rw [he]; rfl)))))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 11) + 11 := by lib_int
          _ = _ := by rw [he]; rfl))))))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by calc rho z = (rho z - 16) + 16 := by lib_int
          _ = _ := by rw [he]; rfl)))))))))⟩
    · obtain ⟨e, he⟩ := (HeckeMult.idv_iff 18 _).mpr h
      exact ⟨e, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by calc rho z = (rho z - 17) + 17 := by lib_int
          _ = _ := by rw [he]; rfl)))))))))⟩
  · intro ⟨e, h⟩
    rcases h with h | h | h | h | h | h | h | h | h
    · exact (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 1 - 1 = 19 * e; lib_int⟩))
    · exact (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 4 - 4 = 19 * e; lib_int⟩)))
    · exact (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 5 - 5 = 19 * e; lib_int⟩))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 6 - 6 = 19 * e; lib_int⟩)))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 7 - 7 = 19 * e; lib_int⟩))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 9 - 9 = 19 * e; lib_int⟩)))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 11 - 11 = 19 * e; lib_int⟩))))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 16 - 16 = 19 * e; lib_int⟩)))))))))
    · exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ((HeckeMult.idv_iff 18 _).mp ⟨e, by rw [h]; show 19 * e + 17 - 17 = 19 * e; lib_int⟩)))))))))

instance (z : Int × Int) : Decidable (Prim19 z) := decidable_of_iff _ (prim19_iff z)

instance (n : Nat) (z : Int × Int) : Decidable (PD19 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum19 {A : Type} : ∀ l : List A, sumK (Ring19.ring19 LibInt.intR) (fun _ => (Ring19.ring19 LibInt.intR).zero) l = (Ring19.ring19 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Ring19.ring19 LibInt.intR).add (Ring19.ring19 LibInt.intR).zero (sumK (Ring19.ring19 LibInt.intR) (fun _ => (Ring19.ring19 LibInt.intR).zero) l) = _; rw [zsum19 l]; exact (Ring19.ring19 LibInt.intR).zero_add _

def I19 (n : Nat) (z : Int × Int) : Int × Int := if PD19 n z then (Ring19.ring19 LibInt.intR).one else (Ring19.ring19 LibInt.intR).zero

theorem I19_pos (n : Nat) (z : Int × Int) (h : PD19 n z) : I19 n z = (Ring19.ring19 LibInt.intR).one := ite_pos' _ _ h
theorem I19_neg (n : Nat) (z : Int × Int) (h : ¬ PD19 n z) : I19 n z = (Ring19.ring19 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw19 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Ring19.ring19 LibInt.intR) (fun z => (Ring19.ring19 LibInt.intR).mul (I19 n z) (w z)) (box19 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult19 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 19 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 19 * k) :
    cw19 w ((m + 1) * (n + 1)) = (Ring19.ring19 LibInt.intR).mul (cw19 w (m + 1)) (cw19 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD19 (m + 1) α → PD19 (n + 1) β → PD19 ((m + 1) * (n + 1)) ((Ring19.ring19 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Ring19.ring19 LibInt.intR).mul (cw19 w (m + 1)) (cw19 w (n + 1))
      = sumK (Ring19.ring19 LibInt.intR) (fun α => sumK (Ring19.ring19 LibInt.intR) (fun β => sumK (Ring19.ring19 LibInt.intR) (fun γ => (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (I19 (m + 1) α) (I19 (n + 1) β)) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β))) (w γ)) (box19 ((m + 1) * (n + 1)))) (box19 (n + 1))) (b  -- (+19 chars; full line in the Zenodo source)
    unfold cw19; rw [sumK_mul]
    refine sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD19 (m + 1) α
    · by_cases hβ : PD19 (n + 1) β
      · rw [I19_pos _ _ hα, I19_pos _ _ hβ]
        have hb := abs_le19 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Ring19.ring19 LibInt.intR) (fun γ => w γ) ((Ring19.ring19 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Ring19.ring19 LibInt.intR) (fun γ => (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).one (Ring19.ring19 LibInt.intR).one) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β))) (w γ)) (box19 ((m + 1) * (n + 1)))
            = sumK (Ring19.ring19 LibInt.intR) (fun γ => (Ring19.ring19 LibInt.intR).mul (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β)) (w γ)) (box19 ((m + 1) * (n + 1))) from sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I19_neg _ _ hβ]
        rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun γ => show (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (I19 (m + 1) α) (Ring19.ring19 LibInt.intR).zero) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β))) (w γ) = (Ring19.ring19 LibInt.intR).zero by lib_ring)]
        rw [zsum19]; lib_ring
    · rw [I19_neg _ _ hα]
      rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun γ => show (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).zero (I19 (n + 1) β)) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β))) (w γ) = (Ring19.ring19 LibInt.intR).zero by lib_ring)]
      rw [zsum19]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => sumK_swap (Ring19.ring19 LibInt.intR) _ _ _)]
  rw [sumK_swap (Ring19.ring19 LibInt.intR)]
  unfold cw19
  refine sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Ring19.ring19 LibInt.intR) (fun α => sumK (Ring19.ring19 LibInt.intR) (fun β => (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (I19 (m + 1) α) (I19 (n + 1) β)) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β))) (w γ)) (box19 (n + 1))) (box19 (m + 1))
      = (Ring19.ring19 LibInt.intR).mul (sumK (Ring19.ring19 LibInt.intR) (fun α => sumK (Ring19.ring19 LibInt.intR) (fun β => (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (I19 (m + 1) α) (I19 (n + 1) β)) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β))) (box19 (n + 1))) (box19 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD19 ((m + 1) * (n + 1)) γ
  · rw [I19_pos _ _ hγ]
    match prim19_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (I19 (m + 1) α) (I19 (n + 1) β)) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β)) = (Ring19.ring19 LibInt.intR).mul (delta (Ring19.ring19 LibInt.intR) α α0) (delta (Ring19.ring19 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD19 (m + 1) α ∧ PD19 (n + 1) β ∧ γ = (Ring19.ring19 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split19 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I19_pos _ _ hα, I19_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Ring19.ring19 LibInt.intR).mul (delta (Ring19.ring19 LibInt.intR) α α0) (delta (Ring19.ring19 LibInt.intR) β β0) = (Ring19.ring19 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD19 (m + 1) α
            · by_cases hβ : PD19 (n + 1) β
              · have hg : ¬ γ = (Ring19.ring19 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I19_neg _ _ hβ]; lib_ring
            · rw [I19_neg _ _ hα]; lib_ring
      rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le19 α0 _ nα0
      have bb := abs_le19 β0 _ nβ0
      rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Ring19.ring19 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => show (Ring19.ring19 LibInt.intR).mul (delta (Ring19.ring19 LibInt.intR) α α0) (Ring19.ring19 LibInt.intR).one = (Ring19.ring19 LibInt.intR).mul (delta (Ring19.ring19 LibInt.intR) α α0) (Ring19.ring19 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Ring19.ring19 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I19_neg _ _ hγ]
    have pt : ∀ α β, (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (I19 (m + 1) α) (I19 (n + 1) β)) (delta (Ring19.ring19 LibInt.intR) γ ((Ring19.ring19 LibInt.intR).mul α β)) = (Ring19.ring19 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD19 (m + 1) α
      · by_cases hβ : PD19 (n + 1) β
        · have hg : ¬ γ = (Ring19.ring19 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I19_neg _ _ hβ]; lib_ring
      · rw [I19_neg _ _ hα]; lib_ring
    rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun α => zsum19 _), zsum19]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult19_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 19 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 19 * k) :
    cw19 (fun z => Frobenius.pw (Ring19.ring19 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Ring19.ring19 LibInt.intR).mul (cw19 (fun z => Frobenius.pw (Ring19.ring19 LibInt.intR) z k) (m + 1)) (cw19 (fun z => Frobenius.pw (Ring19.ring19 LibInt.intR) z k) (n + 1)) :=
  hecke_mult19 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult19

#print axioms HeckeMult19.abs_le19
#print axioms HeckeMult19.prim19_iff
#print axioms HeckeMult19.zsum19
#print axioms HeckeMult19.I19_pos
#print axioms HeckeMult19.I19_neg
#print axioms HeckeMult19.hecke_mult19
#print axioms HeckeMult19.hecke_mult19_pow
#print axioms HeckeMult19.cw19

#print axioms: 8 theorems, no axioms.

78.292 Factorisation above a split prime (\(d=19\))

The \(H_{19}\)-elements of norm \(p^k\) are the \(\pi ^j\bar \pi ^l\).

\[ \alpha =\pi ^j\bar \pi ^{\,l}. \]

lang/Prime19.lean

/-
  lang/Prime19 — T48/T50, cycle 663: ℤ[(1+√−19)/2] above a split prime: π of prime norm is prime, N(α) = p^k ⇒ α = uπ^jπ̄^l, and the
  H19-elements of norm p^k are the π^j π̄^l (H19 conjugation-stable).  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult19

namespace Prime19

open FTAChain DetChain LibInt Euc19 Prim19 Uniq19 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Ring19.ring19 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Ring19.ring19 LibInt.intR).mul u (cj u) = (Ring19.ring19 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Ring19.ring19 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists π x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Ring19.ring19 LibInt.intR).mul (cj d) X) ((Ring19.ring19 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (cj d) X) π) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (cj d) Y) x)
          = (Ring19.ring19 LibInt.intR).mul (cj d) ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul X π) ((Ring19.ring19 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring19.ring19 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Ring19.ring19 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring19.ring19 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Ring19.ring19 LibInt.intR).mul d (Ring19.ring19 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Ring19.ring19 LibInt.intR).mul x y) = (Ring19.ring19 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * (y.1 + y.2) + -(-x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * -y.2 + -x.2 * (y.1 + y.2) + -x.2 * -y.2
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show z.1 + z.2 + -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Ring19.ring19 LibInt.intR).mul u ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) (pw (Ring19.ring19 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Ring19.ring19 LibInt.intR).mul α ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).one (Ring19.ring19 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Ring19.ring19 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Ring19.ring19 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Ring19.ring19 LibInt.intR).mul π ((Ring19.ring19 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring19.ring19 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Ring19.ring19 LibInt.intR).mul u ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Ring19.ring19 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Ring19.ring19 LibInt.intR).mul u ((Ring19.ring19 LibInt.intR).mul _ ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim19_one : Prim19 (Ring19.ring19 LibInt.intR).one := ⟨0, Or.inl rfl⟩

theorem prim19_pow (z : Int × Int) (h : Prim19 z) : ∀ j, Prim19 (pw (Ring19.ring19 LibInt.intR) z j)
  | 0 => prim19_one
  | j + 1 => prim_mul _ _ (prim19_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim19 α) :
    ∃ j l, j + l = k ∧ α = (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) (pw (Ring19.ring19 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim_mul _ _ (prim19_pow π pπ j) (prim19_pow (cj π) (prim_cj π pπ) l)
  have u1 := unit_one u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Prime19

#print axioms Prime19.nrm_mul
#print axioms Prime19.unit_inv
#print axioms Prime19.eprime_div
#print axioms Prime19.cj_mul
#print axioms Prime19.cj_cj
#print axioms Prime19.nrm_cj
#print axioms Prime19.nrm_one_eq
#print axioms Prime19.factor3
#print axioms Prime19.prim19_one
#print axioms Prime19.prim19_pow
#print axioms Prime19.primary_only3

#print axioms: 11 theorems, no axioms.

78.293 361a: the Hecke recursion

The coefficients at \(p^k\) obey the Hecke recursion.

\[ c_W(p^{k+2})=(W(\pi )+W(\bar \pi ))c_W(p^{k+1})-W(\pi )W(\bar \pi )c_W(p^k). \]

lang/HeckePrime19.lean

/-
  lang/HeckePrime19 — T48/T50, cycle 664: **361a's Hecke character at split prime powers**: π ∈ H19 of prime norm p, W multiplicative:
  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l and the Hecke recursion.  (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Prime19
import lang.HeckePrimeW

namespace HeckePrime19

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc19 Prim19 Uniq19 Prime19 HeckeMult19

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Ring19.ring19 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) A = (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) B) : A = B := by
  have h0 : (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) ((Ring19.ring19 LibInt.intR).add A ((Ring19.ring19 LibInt.intR).neg B)) = (Ring19.ring19 LibInt.intR).zero := by
    rw [show (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) ((Ring19.ring19 LibInt.intR).add A ((Ring19.ring19 LibInt.intR).neg B)) = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) A) ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := Cop19.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).add A ((Ring19.ring19 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one c π (nrm_one_eq c nc) pπ (by rw [(Ring19.ring19 LibInt.intR).mul_comm, ← hc]; exact prim_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + a * b + 5 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) :
    ∀ d, Dvd' π (pw (Ring19.ring19 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) (j d l' : Nat)
    (h : (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) (pw (Ring19.ring19 LibInt.intR) (cj π) (l' + 1 + d)) = (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π (j + 1 + d)) (pw (Ring19.ring19 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Ring19.ring19 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Ring19.ring19 LibInt.intR) π j (1 + d)] at h
  have h' : (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) (cj π) l') (pw (Ring19.ring19 LibInt.intR) (cj π) (1 + d)))
      = (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) (cj π) l') (pw (Ring19.ring19 LibInt.intR) π (1 + d))) := by
    calc _ = (Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) (pw (Ring19.ring19 LibInt.intR) π (1 + d))) (pw (Ring19.ring19 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Ring19.ring19 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j) (pw (Ring19.ring19 LibInt.intR) (cj π) l) = (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π j') (pw (Ring19.ring19 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) π x.1) (pw (Ring19.ring19 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD19 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim_mul _ _ (prim19_pow π pπ _) (prim19_pow (cj π) (prim_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) (k : Nat) :
    cw19 W ((P + 1 + 1) ^ k) = sumK (Ring19.ring19 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I19 ((P + 1 + 1) ^ k) z = sumK (Ring19.ring19 LibInt.intR) (fun x => delta (Ring19.ring19 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD19 ((P + 1 + 1) ^ k) z
    · rw [I19_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Ring19.ring19 LibInt.intR) k _ (fun x => delta (Ring19.ring19 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Ring19.ring19 LibInt.intR) k j l hjl]
    · rw [I19_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Ring19.ring19 LibInt.intR) k _ (fun _ => (Ring19.ring19 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Ring19.ring19 LibInt.intR) _).symm
  unfold cw19
  rw [sumK_ext (Ring19.ring19 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Ring19.ring19 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le19 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Ring19.ring19 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring19.ring19 LibInt.intR).one = (Ring19.ring19 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Ring19.ring19 LibInt.intR) z j) = pw (Ring19.ring19 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) z j) z) = (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring19.ring19 LibInt.intR).one = (Ring19.ring19 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) (k : Nat) :
    cw19 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Ring19.ring19 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring19.ring19 LibInt.intR).one = (Ring19.ring19 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π) (k : Nat) :
    cw19 W ((P + 1 + 1) ^ (k + 2)) = (Ring19.ring19 LibInt.intR).add
      ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add (W π) (W (cj π))) (cw19 W ((P + 1 + 1) ^ (k + 1))))
      ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (W π) (W (cj π))) (cw19 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime19

#print axioms HeckePrime19.pow_ne0
#print axioms HeckePrime19.nrm_pw
#print axioms HeckePrime19.npow_add
#print axioms HeckePrime19.en_ne
#print axioms HeckePrime19.cancel_pw3
#print axioms HeckePrime19.no_assoc3
#print axioms HeckePrime19.no_div3
#print axioms HeckePrime19.distinct_lt3
#print axioms HeckePrime19.distinct3
#print axioms HeckePrime19.w3_PD
#print axioms HeckePrime19.cw3_pk
#print axioms HeckePrime19.W_pow3
#print axioms HeckePrime19.cw3_pk'
#print axioms HeckePrime19.cw3_rec

#print axioms: 14 theorems, no axioms.

78.294 361a: the Hecke character at inert primes

A non-square mod \(19\) stays prime; the only \(H_{19}\)-element of norm \(p^{2k}\) is \((-p)^k\).

\[ c_W(p^{2k})=W((-p)^k),\qquad c_W(p^{2k+1})=0. \]

lang/Inert19.lean

/-
  lang/Inert19 — T48/T50, cycle 665: **the ℚ(√−19) Hecke character (361a) at inert primes** (p a non-square mod 19):
    en_mod19       N ≡ (a + 10b)² (mod 19) (20N = (a + 10b)² + 19a²) is never a non-square mod 19
    cw19_zero, inert_div, factor_inert, prim_inert, cw19_even, cw19_odd    (port of Inert11; residue cases generated)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime19

namespace Inert19

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc19 Prim19 Uniq19 Prime19 HeckeMult19

variable (P : Nat)

theorem prodD (X Z K M c s D : Int) (hX : X = D * K + c) (hZ : Z = D * M + s) :
    X * Z = D * (D * K * M + K * s + c * M) + c * s := by
  rw [hX, hZ]; lib_int

theorem prod19 (X Z K M c s : Int) (hX : X = 19 * K + c) (hZ : Z = 19 * M + s) :
    X * Z = 19 * (19 * K * M + K * s + c * M) + c * s := prodD X Z K M c s 19 hX hZ

set_option maxHeartbeats 0 in
theorem nsq_0 (N k X : Int) (key : N = 0 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 16 1 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 2) + -(0 + 19 * X) + 17 + -(-2) + (0 + -2) := by lib_int
        _ = N + -N + 17 := by rw [← h, ← key]; lib_int
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 15 2 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 3) + -(0 + 19 * X) + 16 + -(-3) + (0 + -3) := by lib_int
        _ = N + -N + 16 := by rw [← h, ← key]; lib_int
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 10 7 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 8) + -(0 + 19 * X) + 11 + -(-8) + (0 + -8) := by lib_int
        _ = N + -N + 11 := by rw [← h, ← key]; lib_int
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 8 9 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 10) + -(0 + 19 * X) + 9 + -(-10) + (0 + -10) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 6 11 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 12) + -(0 + 19 * X) + 7 + -(-12) + (0 + -12) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 5 12 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 13) + -(0 + 19 * X) + 6 + -(-13) + (0 + -13) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 4 13 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 14) + -(0 + 19 * X) + 5 + -(-14) + (0 + -14) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 3 14 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 15) + -(0 + 19 * X) + 4 + -(-15) + (0 + -15) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 0 17 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 18) + -(0 + 19 * X) + 1 + -(-18) + (0 + -18) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_1 (N k X : Int) (key : N = 1 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 17 0 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 2) + -(1 + 19 * X) + 18 + -(-1) + (1 + -2) := by lib_int
        _ = N + -N + 18 := by rw [← h, ← key]; lib_int
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 16 1 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 3) + -(1 + 19 * X) + 17 + -(-2) + (1 + -3) := by lib_int
        _ = N + -N + 17 := by rw [← h, ← key]; lib_int
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 11 6 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 8) + -(1 + 19 * X) + 12 + -(-7) + (1 + -8) := by lib_int
        _ = N + -N + 12 := by rw [← h, ← key]; lib_int
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 9 8 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 10) + -(1 + 19 * X) + 10 + -(-9) + (1 + -10) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 7 10 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 12) + -(1 + 19 * X) + 8 + -(-11) + (1 + -12) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 6 11 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 13) + -(1 + 19 * X) + 7 + -(-12) + (1 + -13) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 5 12 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 14) + -(1 + 19 * X) + 6 + -(-13) + (1 + -14) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 4 13 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 15) + -(1 + 19 * X) + 5 + -(-14) + (1 + -15) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 1 16 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 18) + -(1 + 19 * X) + 2 + -(-17) + (1 + -18) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_2 (N k X : Int) (key : N = 4 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 1 16 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 2) + -(4 + 19 * X) + 2 + -(2) + (4 + -2) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 0 17 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 3) + -(4 + 19 * X) + 1 + -(1) + (4 + -3) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 14 3 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 8) + -(4 + 19 * X) + 15 + -(-4) + (4 + -8) := by lib_int
        _ = N + -N + 15 := by rw [← h, ← key]; lib_int
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 12 5 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 10) + -(4 + 19 * X) + 13 + -(-6) + (4 + -10) := by lib_int
        _ = N + -N + 13 := by rw [← h, ← key]; lib_int
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 10 7 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 12) + -(4 + 19 * X) + 11 + -(-8) + (4 + -12) := by lib_int
        _ = N + -N + 11 := by rw [← h, ← key]; lib_int
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 9 8 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 13) + -(4 + 19 * X) + 10 + -(-9) + (4 + -13) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 8 9 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 14) + -(4 + 19 * X) + 9 + -(-10) + (4 + -14) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 7 10 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 15) + -(4 + 19 * X) + 8 + -(-11) + (4 + -15) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 4 13 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 18) + -(4 + 19 * X) + 5 + -(-14) + (4 + -18) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_3 (N k X : Int) (key : N = 9 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 6 11 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 2) + -(9 + 19 * X) + 7 + -(7) + (9 + -2) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 5 12 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 3) + -(9 + 19 * X) + 6 + -(6) + (9 + -3) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 0 17 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 8) + -(9 + 19 * X) + 1 + -(1) + (9 + -8) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 17 0 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 10) + -(9 + 19 * X) + 18 + -(-1) + (9 + -10) := by lib_int
        _ = N + -N + 18 := by rw [← h, ← key]; lib_int
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 15 2 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 12) + -(9 + 19 * X) + 16 + -(-3) + (9 + -12) := by lib_int
        _ = N + -N + 16 := by rw [← h, ← key]; lib_int
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 14 3 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 13) + -(9 + 19 * X) + 15 + -(-4) + (9 + -13) := by lib_int
        _ = N + -N + 15 := by rw [← h, ← key]; lib_int
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 13 4 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 14) + -(9 + 19 * X) + 14 + -(-5) + (9 + -14) := by lib_int
        _ = N + -N + 14 := by rw [← h, ← key]; lib_int
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 12 5 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 15) + -(9 + 19 * X) + 13 + -(-6) + (9 + -15) := by lib_int
        _ = N + -N + 13 := by rw [← h, ← key]; lib_int
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 9 8 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 18) + -(9 + 19 * X) + 10 + -(-9) + (9 + -18) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_4 (N k X : Int) (key : N = 16 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 13 4 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 2) + -(16 + 19 * X) + 14 + -(14) + (16 + -2) := by lib_int
        _ = N + -N + 14 := by rw [← h, ← key]; lib_int
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 12 5 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 3) + -(16 + 19 * X) + 13 + -(13) + (16 + -3) := by lib_int
        _ = N + -N + 13 := by rw [← h, ← key]; lib_int
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 7 10 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 8) + -(16 + 19 * X) + 8 + -(8) + (16 + -8) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 5 12 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 10) + -(16 + 19 * X) + 6 + -(6) + (16 + -10) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 3 14 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 12) + -(16 + 19 * X) + 4 + -(4) + (16 + -12) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 2 15 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 13) + -(16 + 19 * X) + 3 + -(3) + (16 + -13) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 1 16 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 14) + -(16 + 19 * X) + 2 + -(2) + (16 + -14) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 0 17 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 15) + -(16 + 19 * X) + 1 + -(1) + (16 + -15) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(-1)) 16 1 (by calc (19 : Int) * (k + -X + -(-1)) = (19 * k + 18) + -(16 + 19 * X) + 17 + -(-2) + (16 + -18) := by lib_int
        _ = N + -N + 17 := by rw [← h, ← key]; lib_int
        _ = 17 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_5 (N k X : Int) (key : N = 25 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 3 14 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 2) + -(25 + 19 * X) + 4 + -(23) + (25 + -2) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 2 15 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 3) + -(25 + 19 * X) + 3 + -(22) + (25 + -3) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 16 1 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 8) + -(25 + 19 * X) + 17 + -(17) + (25 + -8) := by lib_int
        _ = N + -N + 17 := by rw [← h, ← key]; lib_int
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 14 3 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 10) + -(25 + 19 * X) + 15 + -(15) + (25 + -10) := by lib_int
        _ = N + -N + 15 := by rw [← h, ← key]; lib_int
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 12 5 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 12) + -(25 + 19 * X) + 13 + -(13) + (25 + -12) := by lib_int
        _ = N + -N + 13 := by rw [← h, ← key]; lib_int
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 11 6 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 13) + -(25 + 19 * X) + 12 + -(12) + (25 + -13) := by lib_int
        _ = N + -N + 12 := by rw [← h, ← key]; lib_int
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 10 7 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 14) + -(25 + 19 * X) + 11 + -(11) + (25 + -14) := by lib_int
        _ = N + -N + 11 := by rw [← h, ← key]; lib_int
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 9 8 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 15) + -(25 + 19 * X) + 10 + -(10) + (25 + -15) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 6 11 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 18) + -(25 + 19 * X) + 7 + -(7) + (25 + -18) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_6 (N k X : Int) (key : N = 36 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 14 3 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 2) + -(36 + 19 * X) + 15 + -(34) + (36 + -2) := by lib_int
        _ = N + -N + 15 := by rw [← h, ← key]; lib_int
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 13 4 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 3) + -(36 + 19 * X) + 14 + -(33) + (36 + -3) := by lib_int
        _ = N + -N + 14 := by rw [← h, ← key]; lib_int
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 8 9 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 8) + -(36 + 19 * X) + 9 + -(28) + (36 + -8) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 6 11 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 10) + -(36 + 19 * X) + 7 + -(26) + (36 + -10) := by lib_int
        _ = N + -N + 7 := by rw [← h, ← key]; lib_int
        _ = 7 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 4 13 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 12) + -(36 + 19 * X) + 5 + -(24) + (36 + -12) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 3 14 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 13) + -(36 + 19 * X) + 4 + -(23) + (36 + -13) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 2 15 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 14) + -(36 + 19 * X) + 3 + -(22) + (36 + -14) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 1 16 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 15) + -(36 + 19 * X) + 2 + -(21) + (36 + -15) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(0)) 17 0 (by calc (19 : Int) * (k + -X + -(0)) = (19 * k + 18) + -(36 + 19 * X) + 18 + -(18) + (36 + -18) := by lib_int
        _ = N + -N + 18 := by rw [← h, ← key]; lib_int
        _ = 18 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_7 (N k X : Int) (key : N = 49 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 8 9 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 2) + -(49 + 19 * X) + 9 + -(47) + (49 + -2) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 7 10 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 3) + -(49 + 19 * X) + 8 + -(46) + (49 + -3) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 2 15 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 8) + -(49 + 19 * X) + 3 + -(41) + (49 + -8) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 0 17 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 10) + -(49 + 19 * X) + 1 + -(39) + (49 + -10) := by lib_int
        _ = N + -N + 1 := by rw [← h, ← key]; lib_int
        _ = 1 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 17 0 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 12) + -(49 + 19 * X) + 18 + -(37) + (49 + -12) := by lib_int
        _ = N + -N + 18 := by rw [← h, ← key]; lib_int
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 16 1 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 13) + -(49 + 19 * X) + 17 + -(36) + (49 + -13) := by lib_int
        _ = N + -N + 17 := by rw [← h, ← key]; lib_int
        _ = 17 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 15 2 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 14) + -(49 + 19 * X) + 16 + -(35) + (49 + -14) := by lib_int
        _ = N + -N + 16 := by rw [← h, ← key]; lib_int
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 14 3 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 15) + -(49 + 19 * X) + 15 + -(34) + (49 + -15) := by lib_int
        _ = N + -N + 15 := by rw [← h, ← key]; lib_int
        _ = 15 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(1)) 11 6 (by calc (19 : Int) * (k + -X + -(1)) = (19 * k + 18) + -(49 + 19 * X) + 12 + -(31) + (49 + -18) := by lib_int
        _ = N + -N + 12 := by rw [← h, ← key]; lib_int
        _ = 12 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_8 (N k X : Int) (key : N = 64 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 4 13 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 2) + -(64 + 19 * X) + 5 + -(62) + (64 + -2) := by lib_int
        _ = N + -N + 5 := by rw [← h, ← key]; lib_int
        _ = 5 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 3 14 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 3) + -(64 + 19 * X) + 4 + -(61) + (64 + -3) := by lib_int
        _ = N + -N + 4 := by rw [← h, ← key]; lib_int
        _ = 4 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 17 0 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 8) + -(64 + 19 * X) + 18 + -(56) + (64 + -8) := by lib_int
        _ = N + -N + 18 := by rw [← h, ← key]; lib_int
        _ = 18 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 15 2 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 10) + -(64 + 19 * X) + 16 + -(54) + (64 + -10) := by lib_int
        _ = N + -N + 16 := by rw [← h, ← key]; lib_int
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 13 4 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 12) + -(64 + 19 * X) + 14 + -(52) + (64 + -12) := by lib_int
        _ = N + -N + 14 := by rw [← h, ← key]; lib_int
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 12 5 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 13) + -(64 + 19 * X) + 13 + -(51) + (64 + -13) := by lib_int
        _ = N + -N + 13 := by rw [← h, ← key]; lib_int
        _ = 13 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 11 6 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 14) + -(64 + 19 * X) + 12 + -(50) + (64 + -14) := by lib_int
        _ = N + -N + 12 := by rw [← h, ← key]; lib_int
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 10 7 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 15) + -(64 + 19 * X) + 11 + -(49) + (64 + -15) := by lib_int
        _ = N + -N + 11 := by rw [← h, ← key]; lib_int
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(2)) 7 10 (by calc (19 : Int) * (k + -X + -(2)) = (19 * k + 18) + -(64 + 19 * X) + 8 + -(46) + (64 + -18) := by lib_int
        _ = N + -N + 8 := by rw [← h, ← key]; lib_int
        _ = 8 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem nsq_9 (N k X : Int) (key : N = 81 + 19 * X) (h : N = 19 * k + 2 ∨ N = 19 * k + 3 ∨ N = 19 * k + 8 ∨ N = 19 * k + 10 ∨ N = 19 * k + 12 ∨ N = 19 * k + 13 ∨ N = 19 * k + 14 ∨ N = 19 * k + 15 ∨ N = 19 * k + 18) : False := by
  rcases h with h | h | h | h | h | h | h | h | h
  · exact Sq2Prim8.noM 18 (k + -X + -(4)) 2 15 (by calc (19 : Int) * (k + -X + -(4)) = (19 * k + 2) + -(81 + 19 * X) + 3 + -(79) + (81 + -2) := by lib_int
        _ = N + -N + 3 := by rw [← h, ← key]; lib_int
        _ = 3 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(4)) 1 16 (by calc (19 : Int) * (k + -X + -(4)) = (19 * k + 3) + -(81 + 19 * X) + 2 + -(78) + (81 + -3) := by lib_int
        _ = N + -N + 2 := by rw [← h, ← key]; lib_int
        _ = 2 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 15 2 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 8) + -(81 + 19 * X) + 16 + -(73) + (81 + -8) := by lib_int
        _ = N + -N + 16 := by rw [← h, ← key]; lib_int
        _ = 16 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 13 4 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 10) + -(81 + 19 * X) + 14 + -(71) + (81 + -10) := by lib_int
        _ = N + -N + 14 := by rw [← h, ← key]; lib_int
        _ = 14 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 11 6 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 12) + -(81 + 19 * X) + 12 + -(69) + (81 + -12) := by lib_int
        _ = N + -N + 12 := by rw [← h, ← key]; lib_int
        _ = 12 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 10 7 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 13) + -(81 + 19 * X) + 11 + -(68) + (81 + -13) := by lib_int
        _ = N + -N + 11 := by rw [← h, ← key]; lib_int
        _ = 11 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 9 8 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 14) + -(81 + 19 * X) + 10 + -(67) + (81 + -14) := by lib_int
        _ = N + -N + 10 := by rw [← h, ← key]; lib_int
        _ = 10 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 8 9 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 15) + -(81 + 19 * X) + 9 + -(66) + (81 + -15) := by lib_int
        _ = N + -N + 9 := by rw [← h, ← key]; lib_int
        _ = 9 := by lib_int) rfl
  · exact Sq2Prim8.noM 18 (k + -X + -(3)) 5 12 (by calc (19 : Int) * (k + -X + -(3)) = (19 * k + 18) + -(81 + 19 * X) + 6 + -(63) + (81 + -18) := by lib_int
        _ = N + -N + 6 := by rw [← h, ← key]; lib_int
        _ = 6 := by lib_int) rfl

set_option maxHeartbeats 0 in
theorem en_mod19 (z : Int × Int) (k : Int) (h : en z = 19 * k + 2 ∨ en z = 19 * k + 3 ∨ en z = 19 * k + 8 ∨ en z = 19 * k + 10 ∨ en z = 19 * k + 12 ∨ en z = 19 * k + 13 ∨ en z = 19 * k + 14 ∨ en z = 19 * k + 15 ∨ en z = 19 * k + 18) : False := by
  obtain ⟨a, b⟩ := z
  have hn : en (a, b) = a * a + a * b + 5 * (b * b) := rfl
  obtain ⟨q, r, hq, hr⟩ := GaussEuclid.rnd (a + 10 * b) 18
  have hq' : a + 10 * b = 19 * q + r := by rw [hq]; show q * 19 + r = 19 * q + r; lib_int
  have key : en (a, b) = r * r + 19 * (19 * q * q + 2 * q * r + a * a + -en (a, b)) := by
    have e20 : 20 * en (a, b) = (a + 10 * b) * (a + 10 * b) + 19 * (a * a) := by rw [hn]; lib_int
    rw [hq'] at e20
    calc en (a, b) = 20 * en (a, b) + -(19 * en (a, b)) := by lib_int
      _ = (19 * q + r) * (19 * q + r) + 19 * (a * a) + -(19 * en (a, b)) := by rw [e20]
      _ = _ := by rw [prod19 (19 * q + r) (19 * q + r) q q r r rfl rfl]; generalize 19 * q * q = Q; lib_int
  generalize en (a, b) = N at key h
  generalize 19 * q * q + 2 * q * r + a * a + -N = X at key
  have cases_r : r = 0 ∨ r = 1 ∨ r = 2 ∨ r = 3 ∨ r = 4 ∨ r = 5 ∨ r = 6 ∨ r = 7 ∨ r = 8 ∨ r = 9 ∨ r = -1 ∨ r = -2 ∨ r = -3 ∨ r = -4 ∨ r = -5 ∨ r = -6 ∨ r = -7 ∨ r = -8 ∨ r = -9 := by
    cases r with
    | ofNat t =>
      match t, hr with
      | 0, _ => exact (Or.inl (rfl))
      | 1, _ => exact (Or.inr (Or.inl (rfl)))
      | 2, _ => exact (Or.inr (Or.inr (Or.inl (rfl))))
      | 3, _ => exact (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))
      | 4, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))
      | 5, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))
      | 6, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))
      | 7, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))
      | 8, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))))
      | 9, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))))
      | t + 10, hr' =>
        exfalso
        have : 2 * (t + 10) ≤ 19 := hr'
        rw [show 2 * (t + 10) = 2 * t + 1 + 19 by lib_nat] at this
        exact Nat.not_succ_le_self 19 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 20) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
    | negSucc t =>
      match t, hr with
      | 0, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))))))
      | 1, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))))))
      | 2, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))))))))
      | 3, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))))))))
      | 4, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))))))))))
      | 5, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))))))))))
      | 6, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl))))))))))))))))))
      | 7, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (rfl)))))))))))))))))))
      | 8, _ => exact (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (rfl)))))))))))))))))))
      | t + 9, hr' =>
        exfalso
        have : 2 * (t + 10) ≤ 19 := hr'
        rw [show 2 * (t + 10) = 2 * t + 1 + 19 by lib_nat] at this
        exact Nat.not_succ_le_self 19 (Nat.le_trans (Nat.add_le_add_right (Nat.zero_le (2 * t)) 20) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) this))
  rcases cases_r with e | e | e | e | e | e | e | e | e | e | e | e | e | e | e | e | e | e | e <;> rw [e] at key
  · exact nsq_0 N k X (by rw [key]; lib_int) h
  · exact nsq_1 N k X (by rw [key]; lib_int) h
  · exact nsq_2 N k X (by rw [key]; lib_int) h
  · exact nsq_3 N k X (by rw [key]; lib_int) h
  · exact nsq_4 N k X (by rw [key]; lib_int) h
  · exact nsq_5 N k X (by rw [key]; lib_int) h
  · exact nsq_6 N k X (by rw [key]; lib_int) h
  · exact nsq_7 N k X (by rw [key]; lib_int) h
  · exact nsq_8 N k X (by rw [key]; lib_int) h
  · exact nsq_9 N k X (by rw [key]; lib_int) h
  · exact nsq_1 N k X (by rw [key]; lib_int) h
  · exact nsq_2 N k X (by rw [key]; lib_int) h
  · exact nsq_3 N k X (by rw [key]; lib_int) h
  · exact nsq_4 N k X (by rw [key]; lib_int) h
  · exact nsq_5 N k X (by rw [key]; lib_int) h
  · exact nsq_6 N k X (by rw [key]; lib_int) h
  · exact nsq_7 N k X (by rw [key]; lib_int) h
  · exact nsq_8 N k X (by rw [key]; lib_int) h
  · exact nsq_9 N k X (by rw [key]; lib_int) h

/-- **c_W(n) = 0 for n a non-square mod 19.** -/
theorem cw19_zero (W : Int × Int → Int × Int) (n : Nat) (k : Int) (hn : Int.ofNat n = 19 * k + 2 ∨ Int.ofNat n = 19 * k + 3 ∨ Int.ofNat n = 19 * k + 8 ∨ Int.ofNat n = 19 * k + 10 ∨ Int.ofNat n = 19 * k + 12 ∨ Int.ofNat n = 19 * k + 13 ∨ Int.ofNat n = 19 * k + 14 ∨ Int.ofNat n = 19 * k + 15 ∨ Int.ofNat n = 19 * k + 18) : cw19 W n = (Ring19.ring19 LibInt.intR).zero := by
  unfold cw19
  rw [sumK_ext (Ring19.ring19 LibInt.intR) _ (fun _ => (Ring19.ring19 LibInt.intR).zero) (fun z => by
    rw [I19_neg _ _ (fun h => en_mod19 z k (by rw [h.1]; exact hn))]; lib_ring)]
  exact zsum19 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + Int.ofNat (P + 1 + 1) * 0 + 5 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + 0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 18) (z : Int × Int) (h : nrm z = P + 1 +   -- (+16 chars; full line in the Zenodo source)
  obtain ⟨K, hK⟩ := hc
  exact en_mod19 z K (by rw [← nrm_cast, h]; exact hK)

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1  -- (+38 chars; full line in the Zenodo source)
    (h : Dvd' (Pi P) ((Ring19.ring19 LibInt.intR).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists (Pi P) x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Ring19.ring19 LibInt.intR).mul (cj d) X) ((Ring19.ring19 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (cj d) X) (Pi P)) ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (cj d) Y) x)
          = (Ring19.ring19 LibInt.intR).mul (cj d) ((Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul X (Pi P)) ((Ring19.ring19 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring19.ring19 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Ring19.ring19 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring19.ring19 LibInt.intR).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Ring19.ring19 LibInt.intR).mul d (Ring19.ring19 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P   -- (+46 chars; full line in the Zenodo source)
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Ring19.ring19 LibInt.intR).mul u (pw (Ring19.ring19 LibInt.intR) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Ring19.ring19 LibInt.intR).mul α (Ring19.ring19 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Ring19.ring19 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring19.ring19 LibInt.intR).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Ring19.ring19 LibInt.intR).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Ring19.ring19 LibInt.intR).mul u ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 18) : Prim19 (nPi P) := by
  obtain ⟨K, hK | hK | hK | hK | hK | hK | hK | hK | hK⟩ := hc
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int)))))))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int)))))))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int))))))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int)))))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int))))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int)))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int))))⟩
  · exact ⟨-K + -1, (Or.inr (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int)))⟩
  · exact ⟨-K + -1, (Or.inl (by show -Int.ofNat (P + 1 + 1) + 10 * 0 = _; rw [hK]; lib_int))⟩

theorem pw_neg (k : Nat) : pw (Ring19.ring19 LibInt.intR) (Pi P) k =
    (Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) ((Ring19.ring19 LibInt.intR).neg (Ring19.ring19 LibInt.intR).one) k) (pw (Ring19.ring19 LibInt.intR) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -(-0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1) + -0 * 0; lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Ring19.ring19 LibInt.intR) ((Ring19.ring19 LibInt.intR).neg (Ring19.ring19 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Ring19.ring19 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P +   -- (+47 chars; full line in the Zenodo source)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim19 α) : α = pw (Ring19.ring19 LibInt.intR) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Ring19.ring19 LibInt.intR).mul u (pw (Ring19.ring19 LibInt.intR) ((Ring19.ring19 LibInt.intR).neg (Ring19.ring19 LibInt.intR).one) k) = v
  have he' : α = (Ring19.ring19 LibInt.intR).mul v (pw (Ring19.ring19 LibInt.intR) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one v _ (nrm_one_eq v hv1) (prim19_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -Int.ofNat (P + 1 + 1) * 0 + 5 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Ring19.ring19 LibInt.intR) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime19.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw19_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19  -- (+60 chars; full line in the Zenodo source)
    cw19 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Ring19.ring19 LibInt.intR) (nPi P) k) := by
  have hPD : PD19 ((P + 1 + 1) ^ (2 * k)) (pw (Ring19.ring19 LibInt.intR) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim19_pow _ (prim_nPi P hc) k⟩
  unfold cw19
  rw [sumK_ext (Ring19.ring19 LibInt.intR) _ (fun z => (Ring19.ring19 LibInt.intR).mul (delta (Ring19.ring19 LibInt.intR) z (pw (Ring19.ring19 LibInt.intR) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD19 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I19_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I19_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le19 _ _ hPD.1
  exact HeckeMult.pick_box (Ring19.ring19 LibInt.intR) W _ _ hb.1 hb.2

set_option maxHeartbeats 0 in
theorem sq19 (Y L : Int) (hL : Y = 19 * L + 2 ∨ Y = 19 * L + 3 ∨ Y = 19 * L + 8 ∨ Y = 19 * L + 10 ∨ Y = 19 * L + 12 ∨ Y = 19 * L + 13 ∨ Y = 19 * L + 14 ∨ Y = 19 * L + 15 ∨ Y = 19 * L + 18) : ∃ M : Int, Y * Y = 19 * M + 1 ∨ Y * Y = 19 * M + 4 ∨ Y * Y = 19 * M + 5 ∨ Y * Y = 19 * M + 6 ∨ Y * Y = 19 * M + 7 ∨ Y * Y = 19 * M + 9 ∨ Y * Y = 19 * M + 11 ∨ Y * Y = 19 * M + 16 ∨ Y * Y = 19 * M + 17 := by
  rcases hL with hL | hL | hL | hL | hL | hL | hL | hL | hL
  · exact ⟨19 * L * L + L * 2 + 2 * L + 0, (Or.inr (Or.inl (by rw [prod19 Y Y L L 2 2 hL hL]; generalize 19 * L * L + L * 2 + 2 * L = X; lib_int)))⟩
  · exact ⟨19 * L * L + L * 3 + 3 * L + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 Y Y L L 3 3 hL hL]; generalize 19 * L * L + L * 3 + 3 * L = X; lib_int)))))))⟩
  · exact ⟨19 * L * L + L * 8 + 8 * L + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 Y Y L L 8 8 hL hL]; generalize 19 * L * L + L * 8 + 8 * L = X; lib_int))))))⟩
  · exact ⟨19 * L * L + L * 10 + 10 * L + 5, (Or.inr (Or.inr (Or.inl (by rw [prod19 Y Y L L 10 10 hL hL]; generalize 19 * L * L + L * 10 + 10 * L = X; lib_int))))⟩
  · exact ⟨19 * L * L + L * 12 + 12 * L + 7, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 Y Y L L 12 12 hL hL]; generalize 19 * L * L + L * 12 + 12 * L = X; lib_int))))))))⟩
  · exact ⟨19 * L * L + L * 13 + 13 * L + 8, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 Y Y L L 13 13 hL hL]; generalize 19 * L * L + L * 13 + 13 * L = X; lib_int)))))))))⟩
  · exact ⟨19 * L * L + L * 14 + 14 * L + 10, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 Y Y L L 14 14 hL hL]; generalize 19 * L * L + L * 14 + 14 * L = X; lib_int)))))⟩
  · exact ⟨19 * L * L + L * 15 + 15 * L + 11, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 Y Y L L 15 15 hL hL]; generalize 19 * L * L + L * 15 + 15 * L = X; lib_int)))))))))⟩
  · exact ⟨19 * L * L + L * 18 + 18 * L + 17, (Or.inl (by rw [prod19 Y Y L L 18 18 hL hL]; generalize 19 * L * L + L * 18 + 18 * L = X; lib_int))⟩

set_option maxHeartbeats 0 in
theorem step19_0 (X K M Z : Int) (hK : X = 19 * K + 2) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 2 * M + 0, (Or.inl (by rw [prod19 X Z K M 2 1 hK hM]; generalize 19 * K * M + K * 1 + 2 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 4 + 2 * M + 0, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 2 4 hK hM]; generalize 19 * K * M + K * 4 + 2 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 5 + 2 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 2 5 hK hM]; generalize 19 * K * M + K * 5 + 2 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 6 + 2 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 2 6 hK hM]; generalize 19 * K * M + K * 6 + 2 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 7 + 2 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 2 7 hK hM]; generalize 19 * K * M + K * 7 + 2 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 9 + 2 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 2 9 hK hM]; generalize 19 * K * M + K * 9 + 2 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 11 + 2 * M + 1, (Or.inr (Or.inl (by rw [prod19 X Z K M 2 11 hK hM]; generalize 19 * K * M + K * 11 + 2 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 16 + 2 * M + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 2 16 hK hM]; generalize 19 * K * M + K * 16 + 2 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 17 + 2 * M + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 2 17 hK hM]; generalize 19 * K * M + K * 17 + 2 * M = Q; lib_int)))))))))⟩

set_option maxHeartbeats 0 in
theorem step19_1 (X K M Z : Int) (hK : X = 19 * K + 3) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 3 * M + 0, (Or.inr (Or.inl (by rw [prod19 X Z K M 3 1 hK hM]; generalize 19 * K * M + K * 1 + 3 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 4 + 3 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 3 4 hK hM]; generalize 19 * K * M + K * 4 + 3 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 5 + 3 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 3 5 hK hM]; generalize 19 * K * M + K * 5 + 3 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 6 + 3 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 3 6 hK hM]; generalize 19 * K * M + K * 6 + 3 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 7 + 3 * M + 1, (Or.inl (by rw [prod19 X Z K M 3 7 hK hM]; generalize 19 * K * M + K * 7 + 3 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 9 + 3 * M + 1, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 3 9 hK hM]; generalize 19 * K * M + K * 9 + 3 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 11 + 3 * M + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 3 11 hK hM]; generalize 19 * K * M + K * 11 + 3 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 16 + 3 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 3 16 hK hM]; generalize 19 * K * M + K * 16 + 3 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 17 + 3 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 3 17 hK hM]; generalize 19 * K * M + K * 17 + 3 * M = Q; lib_int)))))))⟩

set_option maxHeartbeats 0 in
theorem step19_2 (X K M Z : Int) (hK : X = 19 * K + 8) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 8 * M + 0, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 8 1 hK hM]; generalize 19 * K * M + K * 1 + 8 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 4 + 8 * M + 1, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 8 4 hK hM]; generalize 19 * K * M + K * 4 + 8 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 5 + 8 * M + 2, (Or.inl (by rw [prod19 X Z K M 8 5 hK hM]; generalize 19 * K * M + K * 5 + 8 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 6 + 8 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 8 6 hK hM]; generalize 19 * K * M + K * 6 + 8 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 7 + 8 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 8 7 hK hM]; generalize 19 * K * M + K * 7 + 8 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 9 + 8 * M + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 8 9 hK hM]; generalize 19 * K * M + K * 9 + 8 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 11 + 8 * M + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 8 11 hK hM]; generalize 19 * K * M + K * 11 + 8 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 16 + 8 * M + 6, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 8 16 hK hM]; generalize 19 * K * M + K * 16 + 8 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 17 + 8 * M + 7, (Or.inr (Or.inl (by rw [prod19 X Z K M 8 17 hK hM]; generalize 19 * K * M + K * 17 + 8 * M = Q; lib_int)))⟩

set_option maxHeartbeats 0 in
theorem step19_3 (X K M Z : Int) (hK : X = 19 * K + 10) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 10 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 10 1 hK hM]; generalize 19 * K * M + K * 1 + 10 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 4 + 10 * M + 2, (Or.inl (by rw [prod19 X Z K M 10 4 hK hM]; generalize 19 * K * M + K * 4 + 10 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 5 + 10 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 10 5 hK hM]; generalize 19 * K * M + K * 5 + 10 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 6 + 10 * M + 3, (Or.inr (Or.inl (by rw [prod19 X Z K M 10 6 hK hM]; generalize 19 * K * M + K * 6 + 10 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 7 + 10 * M + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 10 7 hK hM]; generalize 19 * K * M + K * 7 + 10 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 9 + 10 * M + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 10 9 hK hM]; generalize 19 * K * M + K * 9 + 10 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 11 + 10 * M + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 10 11 hK hM]; generalize 19 * K * M + K * 11 + 10 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 16 + 10 * M + 8, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 10 16 hK hM]; generalize 19 * K * M + K * 16 + 10 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 17 + 10 * M + 8, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 10 17 hK hM]; generalize 19 * K * M + K * 17 + 10 * M = Q; lib_int)))))))))⟩

set_option maxHeartbeats 0 in
theorem step19_4 (X K M Z : Int) (hK : X = 19 * K + 12) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 12 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 12 1 hK hM]; generalize 19 * K * M + K * 1 + 12 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 4 + 12 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 12 4 hK hM]; generalize 19 * K * M + K * 4 + 12 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 5 + 12 * M + 3, (Or.inr (Or.inl (by rw [prod19 X Z K M 12 5 hK hM]; generalize 19 * K * M + K * 5 + 12 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 6 + 12 * M + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 12 6 hK hM]; generalize 19 * K * M + K * 6 + 12 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 7 + 12 * M + 4, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 12 7 hK hM]; generalize 19 * K * M + K * 7 + 12 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 9 + 12 * M + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 12 9 hK hM]; generalize 19 * K * M + K * 9 + 12 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 11 + 12 * M + 6, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 12 11 hK hM]; generalize 19 * K * M + K * 11 + 12 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 16 + 12 * M + 10, (Or.inl (by rw [prod19 X Z K M 12 16 hK hM]; generalize 19 * K * M + K * 16 + 12 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 17 + 12 * M + 10, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 12 17 hK hM]; generalize 19 * K * M + K * 17 + 12 * M = Q; lib_int))))))))⟩

set_option maxHeartbeats 0 in
theorem step19_5 (X K M Z : Int) (hK : X = 19 * K + 13) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 13 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 13 1 hK hM]; generalize 19 * K * M + K * 1 + 13 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 4 + 13 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 13 4 hK hM]; generalize 19 * K * M + K * 4 + 13 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 5 + 13 * M + 3, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 13 5 hK hM]; generalize 19 * K * M + K * 5 + 13 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 6 + 13 * M + 4, (Or.inl (by rw [prod19 X Z K M 13 6 hK hM]; generalize 19 * K * M + K * 6 + 13 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 7 + 13 * M + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 13 7 hK hM]; generalize 19 * K * M + K * 7 + 13 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 9 + 13 * M + 6, (Or.inr (Or.inl (by rw [prod19 X Z K M 13 9 hK hM]; generalize 19 * K * M + K * 9 + 13 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 11 + 13 * M + 7, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 13 11 hK hM]; generalize 19 * K * M + K * 11 + 13 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 16 + 13 * M + 10, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 13 16 hK hM]; generalize 19 * K * M + K * 16 + 13 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 17 + 13 * M + 11, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 13 17 hK hM]; generalize 19 * K * M + K * 17 + 13 * M = Q; lib_int))))))⟩

set_option maxHeartbeats 0 in
theorem step19_6 (X K M Z : Int) (hK : X = 19 * K + 14) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 14 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 14 1 hK hM]; generalize 19 * K * M + K * 1 + 14 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 4 + 14 * M + 2, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 14 4 hK hM]; generalize 19 * K * M + K * 4 + 14 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 5 + 14 * M + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 14 5 hK hM]; generalize 19 * K * M + K * 5 + 14 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 6 + 14 * M + 4, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 14 6 hK hM]; generalize 19 * K * M + K * 6 + 14 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 7 + 14 * M + 5, (Or.inr (Or.inl (by rw [prod19 X Z K M 14 7 hK hM]; generalize 19 * K * M + K * 7 + 14 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 9 + 14 * M + 6, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 14 9 hK hM]; generalize 19 * K * M + K * 9 + 14 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 11 + 14 * M + 8, (Or.inl (by rw [prod19 X Z K M 14 11 hK hM]; generalize 19 * K * M + K * 11 + 14 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 16 + 14 * M + 11, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 14 16 hK hM]; generalize 19 * K * M + K * 16 + 14 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 17 + 14 * M + 12, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 14 17 hK hM]; generalize 19 * K * M + K * 17 + 14 * M = Q; lib_int)))))⟩

set_option maxHeartbeats 0 in
theorem step19_7 (X K M Z : Int) (hK : X = 19 * K + 15) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 15 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 15 1 hK hM]; generalize 19 * K * M + K * 1 + 15 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 4 + 15 * M + 3, (Or.inr (Or.inl (by rw [prod19 X Z K M 15 4 hK hM]; generalize 19 * K * M + K * 4 + 15 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 5 + 15 * M + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 15 5 hK hM]; generalize 19 * K * M + K * 5 + 15 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 6 + 15 * M + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 15 6 hK hM]; generalize 19 * K * M + K * 6 + 15 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 7 + 15 * M + 5, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 15 7 hK hM]; generalize 19 * K * M + K * 7 + 15 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 9 + 15 * M + 7, (Or.inl (by rw [prod19 X Z K M 15 9 hK hM]; generalize 19 * K * M + K * 9 + 15 * M = Q; lib_int))⟩
  · exact ⟨19 * K * M + K * 11 + 15 * M + 8, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 15 11 hK hM]; generalize 19 * K * M + K * 11 + 15 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 16 + 15 * M + 12, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 15 16 hK hM]; generalize 19 * K * M + K * 16 + 15 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 17 + 15 * M + 13, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 15 17 hK hM]; generalize 19 * K * M + K * 17 + 15 * M = Q; lib_int))))⟩

set_option maxHeartbeats 0 in
theorem step19_8 (X K M Z : Int) (hK : X = 19 * K + 18) (hM : Z = 19 * M + 1 ∨ Z = 19 * M + 4 ∨ Z = 19 * M + 5 ∨ Z = 19 * M + 6 ∨ Z = 19 * M + 7 ∨ Z = 19 * M + 9 ∨ Z = 19 * M + 11 ∨ Z = 19 * M + 16 ∨ Z = 19 * M + 17) :
    ∃ W : Int, X * Z = 19 * W + 2 ∨ X * Z = 19 * W + 3 ∨ X * Z = 19 * W + 8 ∨ X * Z = 19 * W + 10 ∨ X * Z = 19 * W + 12 ∨ X * Z = 19 * W + 13 ∨ X * Z = 19 * W + 14 ∨ X * Z = 19 * W + 15 ∨ X * Z = 19 * W + 18 := by
  rcases hM with hM | hM | hM | hM | hM | hM | hM | hM | hM
  · exact ⟨19 * K * M + K * 1 + 18 * M + 0, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (by rw [prod19 X Z K M 18 1 hK hM]; generalize 19 * K * M + K * 1 + 18 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 4 + 18 * M + 3, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 18 4 hK hM]; generalize 19 * K * M + K * 4 + 18 * M = Q; lib_int)))))))))⟩
  · exact ⟨19 * K * M + K * 5 + 18 * M + 4, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 18 5 hK hM]; generalize 19 * K * M + K * 5 + 18 * M = Q; lib_int))))))))⟩
  · exact ⟨19 * K * M + K * 6 + 18 * M + 5, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 18 6 hK hM]; generalize 19 * K * M + K * 6 + 18 * M = Q; lib_int)))))))⟩
  · exact ⟨19 * K * M + K * 7 + 18 * M + 6, (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 18 7 hK hM]; generalize 19 * K * M + K * 7 + 18 * M = Q; lib_int))))))⟩
  · exact ⟨19 * K * M + K * 9 + 18 * M + 8, (Or.inr (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 18 9 hK hM]; generalize 19 * K * M + K * 9 + 18 * M = Q; lib_int)))))⟩
  · exact ⟨19 * K * M + K * 11 + 18 * M + 10, (Or.inr (Or.inr (Or.inl (by rw [prod19 X Z K M 18 11 hK hM]; generalize 19 * K * M + K * 11 + 18 * M = Q; lib_int))))⟩
  · exact ⟨19 * K * M + K * 16 + 18 * M + 15, (Or.inr (Or.inl (by rw [prod19 X Z K M 18 16 hK hM]; generalize 19 * K * M + K * 16 + 18 * M = Q; lib_int)))⟩
  · exact ⟨19 * K * M + K * 17 + 18 * M + 16, (Or.inl (by rw [prod19 X Z K M 18 17 hK hM]; generalize 19 * K * M + K * 17 + 18 * M = Q; lib_int))⟩

theorem step19 (X Y K L : Int) (hK : X = 19 * K + 2 ∨ X = 19 * K + 3 ∨ X = 19 * K + 8 ∨ X = 19 * K + 10 ∨ X = 19 * K + 12 ∨ X = 19 * K + 13 ∨ X = 19 * K + 14 ∨ X = 19 * K + 15 ∨ X = 19 * K + 18) (hL : Y = 19 * L + 2 ∨ Y = 19 * L + 3 ∨ Y = 19 * L + 8 ∨ Y = 19 * L + 10 ∨ Y = 19 * L + 12 ∨ Y = 19 * L + 13 ∨ Y = 19 * L + 14 ∨ Y = 19 * L + 15 ∨ Y = 19 * L + 18) :
    ∃ W : Int, X * Y * Y = 19 * W + 2 ∨ X * Y * Y = 19 * W + 3 ∨ X * Y * Y = 19 * W + 8 ∨ X * Y * Y = 19 * W + 10 ∨ X * Y * Y = 19 * W + 12 ∨ X * Y * Y = 19 * W + 13 ∨ X * Y * Y = 19 * W + 14 ∨ X * Y * Y = 19 * W + 15 ∨ X * Y * Y = 19 * W + 18 := by
  obtain ⟨M, hM⟩ := sq19 Y L hL
  have e : X * Y * Y = X * (Y * Y) := by lib_int
  rw [e]
  rcases hK with hK | hK | hK | hK | hK | hK | hK | hK | hK
  · exact step19_0 _ K M _ hK hM
  · exact step19_1 _ K M _ hK hM
  · exact step19_2 _ K M _ hK hM
  · exact step19_3 _ K M _ hK hM
  · exact step19_4 _ K M _ hK hM
  · exact step19_5 _ K M _ hK hM
  · exact step19_6 _ K M _ hK hM
  · exact step19_7 _ K M _ hK hM
  · exact step19_8 _ K M _ hK hM

theorem pow_odd (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 18) (k : Nat) : ∃ K : Int, Int.ofNat ((  -- (+474 chars; full line in the Zenodo source)
  obtain ⟨L, hL⟩ := hc
  induction k with
  | zero => exact ⟨L, by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hL⟩
  | succ k ih =>
    obtain ⟨K, hK⟩ := ih
    have e : Int.ofNat ((P + 1 + 1) ^ (2 * (k + 1) + 1)) = Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) := by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; rfl
    rw [e]
    exact step19 _ _ K L hK hL

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw19_odd (W : Int × Int → Int × Int) (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 18) (k : N  -- (+81 chars; full line in the Zenodo source)
  obtain ⟨K, hK⟩ := pow_odd P hc k
  exact cw19_zero W _ K hK

end Inert19

#print axioms Inert19.nsq_0
#print axioms Inert19.nsq_1
#print axioms Inert19.nsq_2
#print axioms Inert19.nsq_3
#print axioms Inert19.nsq_4
#print axioms Inert19.nsq_5
#print axioms Inert19.nsq_6
#print axioms Inert19.nsq_7
#print axioms Inert19.nsq_8
#print axioms Inert19.nsq_9
#print axioms Inert19.en_mod19
#print axioms Inert19.cw19_zero
#print axioms Inert19.en_Pi
#print axioms Inert19.nrm_Pi
#print axioms Inert19.cj_Pi
#print axioms Inert19.not_p
#print axioms Inert19.dvd_conv
#print axioms Inert19.split_sq
#print axioms Inert19.inert_div
#print axioms Inert19.nat_pow2
#print axioms Inert19.factor_inert
#print axioms Inert19.prim_nPi
#print axioms Inert19.pw_neg
#print axioms Inert19.nrm_neg_one_pw
#print axioms Inert19.prim_inert
#print axioms Inert19.nrm_nPi
#print axioms Inert19.nrm_nPi_pw
#print axioms Inert19.cw19_even
#print axioms Inert19.prodD
#print axioms Inert19.prod19
#print axioms Inert19.sq19
#print axioms Inert19.step19_0
#print axioms Inert19.step19_1
#print axioms Inert19.step19_2
#print axioms Inert19.step19_3
#print axioms Inert19.step19_4
#print axioms Inert19.step19_5
#print axioms Inert19.step19_6
#print axioms Inert19.step19_7
#print axioms Inert19.step19_8
#print axioms Inert19.step19
#print axioms Inert19.pow_odd
#print axioms Inert19.cw19_odd

#print axioms: 43 theorems, no axioms.

78.295 The ring \(\mathbb Z[(1+\sqrt {-43})/2]\)

Pairs \(a+b\theta \), \(\theta ^2=\theta -11\).

\[ (a+b\theta )(c+d\theta )=(ac-11bd)+(ad+bc+bd)\theta . \]

lang/Ring43.lean

/-
  lang/Ring43 — T48/T50: the ring K[θ] = K[x]/(x² − x + 11) (for K = ℤ: the integers of ℚ(√−43)).
  (a + bθ)(c + dθ) = (ac − 11bd) + (ad + bc + bd)θ.  (Show-expansions generated.)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lib.RingTac

namespace Ring43

variable {R : Type} (K : DetChain.CRing R)

def rmul (x y : R × R) : R × R :=
  ((K.add (K.mul x.1 y.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul x.2 y.2) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)))), (K.add (K.add (K.mul x.1 y.2) (K.mul x.2 y.1)) (K.mul x.2 y.2)))

theorem r_assoc (a b e : R × R) : rmul K (rmul K a b) e = rmul K a (rmul K b e) := by
  apply Prod.ext
  · show (K.add (K.mul (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)))) e.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul   -- (+2074 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)))) e.2) (K.mul (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) e.1))   -- (+546 chars; full line in the Zenodo source)
    lib_ring

theorem r_comm (a b : R × R) : rmul K a b = rmul K b a := by
  apply Prod.ext
  · show (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)))) = (K.add (K.mul b.1 a.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (  -- (+186 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) = (K.add (K.add (K.mul b.1 a.2) (K.mul b.2 a.1)) (K.mul b.2 a.2))
    lib_ring

theorem r_one (a : R × R) : rmul K (K.one, K.zero) a = a := by
  apply Prod.ext
  · show (K.add (K.mul K.one a.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul K.zero a.2) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)))) = a.1
    lib_ring
  · show (K.add (K.add (K.mul K.one a.2) (K.mul K.zero a.1)) (K.mul K.zero a.2)) = a.2
    lib_ring

theorem r_distrib (a b e : R × R) :
    rmul K a (K.add b.1 e.1, K.add b.2 e.2) = (K.add (rmul K a b).1 (rmul K a e).1, K.add (rmul K a b).2 (rmul K a e).2) := by
  apply Prod.ext
  · show (K.add (K.mul a.1 (K.add b.1 e.1)) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.2 (K.add b.2 e.2)) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b  -- (+626 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul a.1 (K.add b.2 e.2)) (K.mul a.2 (K.add b.1 e.1))) (K.mul a.2 (K.add b.2 e.2))) = (K.add (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) (K.add (K.add (K.mul a.1 e.2) (K.mul a.2 e.1)) (K.mul a.2 e.2)))
    lib_ring

def ring43 : DetChain.CRing (R × R) where
  add := fun x y => (K.add x.1 y.1, K.add x.2 y.2)
  mul := rmul K
  neg := fun x => (K.neg x.1, K.neg x.2)
  zero := (K.zero, K.zero)
  one := (K.one, K.zero)
  add_assoc := fun a b c => by
    apply Prod.ext
    · exact K.add_assoc a.1 b.1 c.1
    · exact K.add_assoc a.2 b.2 c.2
  add_comm := fun a b => by
    apply Prod.ext
    · exact K.add_comm a.1 b.1
    · exact K.add_comm a.2 b.2
  zero_add := fun a => by
    apply Prod.ext
    · exact K.zero_add a.1
    · exact K.zero_add a.2
  add_neg := fun a => by
    apply Prod.ext
    · exact K.add_neg a.1
    · exact K.add_neg a.2
  mul_assoc := r_assoc K
  mul_comm := r_comm K
  one_mul := r_one K
  distrib := r_distrib K

end Ring43

#print axioms Ring43.r_assoc
#print axioms Ring43.r_comm
#print axioms Ring43.r_one
#print axioms Ring43.r_distrib
#print axioms Ring43.ring43

#print axioms: 5 theorems, no axioms.

78.296 Norm facts (\(d=43\))

\(4N=(2a+b)^2+43b^2\); Euclid’s lemma.

\[ N(a+b\theta )=a^2+ab+11b^2. \]

lang/Euc43.lean

/-
  lang/Euc43 — T48/T50: ℤ[θ], θ = (1 + √−43)/2, N(a + bθ) = a² + ab + 11b², 4N = (2a + b)² + 43b²: norm facts and Euclid's lemma.
  (The Dedekind–Hasse step is in DH43.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Ring43
import lang.GaussEuclid
import lang.W36Mul

namespace Euc43

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + z.1 * z.2 + 11 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Ring43.ring43 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + 11 * (B * B) := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_trans (Nat.le_add_left _ (A * A)) (Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 10))) _))
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + 11 * (Int.ofNat B * Int.ofNat B) := congrArg Int.ofNat hc
  have opp : en z = Int.ofNat A * Int.ofNat A + 11 * (Int.ofNat B * Int.ofNat B) + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨c, by rw [h, ← e]; lib_int⟩
  have same : en z = Int.ofNat A * Int.ofNat A + 11 * (Int.ofNat B * Int.ofNat B) + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨A * A + 11 * (B * B) + A * B, h⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem four_nrm (z : Int × Int) : 4 * nrm z = Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 43 * (Int.natAbs z.2 * Int.natAbs z.2) := by
  apply Int.ofNat.inj
  show 4 * Int.ofNat (nrm z) = Int.ofNat (Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2)) + 43 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2)
  rw [nrm_cast, GaussEuclid.nas, GaussEuclid.nas]; unfold en; lib_int

theorem mul_cj (z : Int × Int) : (Ring43.ring43 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + z.2) + -(z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 11 * (z.2 * z.2)
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + z.2) + z.2 * -z.2 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Ring43.ring43 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) + (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.  -- (+89 chars; full line in the Zenodo source)
      + 11 * ((x.1 * y.2 + x.2 * y.1 + x.2 * y.2) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)) =
    (x.1 * x.1 + x.1 * x.2 + 11 * (x.2 * x.2)) * (y.1 * y.1 + y.1 * y.2 + 11 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + z.2) * (z.1 + z.2) + (z.1 + z.2) * -z.2 + 11 * (-z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 11 * (z.2 * z.2)
  lib_int

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Ring43.ring43 LibInt.intR).zero := by
  have h4 := four_nrm z
  rw [h] at h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by
    have : 43 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
    cases nat_mul_zero _ _ this with
    | inl e => exact Nat.noConfusion e
    | inr e => exact e
  have hT : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have t0 : 2 * z.1 + z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hT with | inl e => exact e | inr e => exact e)
  have a0 : z.1 = 0 := W36Mul.two_cancel_int z.1 0 (by rw [show 2 * z.1 = 2 * z.1 + z.2 by rw [b0]; lib_int, t0]; rfl)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Ring43.ring43 LibInt.intR).zero) :
    ∃ d x y, d = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x α) ((Ring43.ring43 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Ring43.ring43 LibInt.intR).one, (Ring43.ring43 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Ring43.ring43 LibInt.intR).one, by lib_ring⟩, ⟨(Ring43.ring43 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x α) ((Ring43.ring43 LibInt.intR).mul y β) = (Ring43.ring43 LibInt.intR).one) (h : Dvd' α ((Ring43.ring43 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x γ) ((Ring43.ring43 LibInt.intR).mul y c), by
      calc γ = (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x α) ((Ring43.ring43 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul x α) γ) ((Ring43.ring43 LibInt.intR).mul y ((Ring43.ring43 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Ring43.ring43 LibInt.intR).mul α ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x γ) ((Ring43.ring43 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

theorem nrm_lt (z : Int × Int) (N : Nat) (h : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 43 * (Int.natAbs z.2 * Int.natAbs z.2) < 4 * (N * N)) :
    nrm z < N * N := by
  cases Nat.lt_or_ge (nrm z) (N * N) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    rw [← four_nrm] at h
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_left 4 h'))

theorem sq_le (T N : Nat) (h : T ≤ N) : T * T ≤ N * N := Nat.mul_le_mul h h


end Euc43

#print axioms Euc43.sgn
#print axioms Euc43.le_dest
#print axioms Euc43.ab_le
#print axioms Euc43.en_rep
#print axioms Euc43.nrm_cast
#print axioms Euc43.four_nrm
#print axioms Euc43.mul_cj
#print axioms Euc43.en_mul
#print axioms Euc43.en_cj
#print axioms Euc43.nrm_zero
#print axioms Euc43.egcd_base
#print axioms Euc43.nrm_lt
#print axioms Euc43.sq_le
#print axioms Euc43.eeuclid

#print axioms: 14 theorems, no axioms.

78.297 A Dedekind–Hasse step for \(d=43\)

Multipliers \(1,2,3\) on the bulk; \(\theta -1,-\theta ,\theta -2\) at the half and third points.

\[ \exists s,q:\ N(s\alpha -q\beta )<N(\beta ),\ s\alpha =q\beta \Rightarrow s=1. \]

lang/DH43.lean

/-
  lang/DH43 — T48/T50: **the Dedekind–Hasse step in ℤ[(1 + √−43)/2]** (not norm-Euclidean): from a two-step remainder r
  (|2r₁ + r₂|, 2|r₂| ≤ N) there are s and e with N(sr − eN) < N², and sr − eN = 0 only for s = 1:
  s = 1 if 4N(r) < 4N²; else (3N² ≤ 43B², B = |r₂|, 2B + u = N) s = 2 if 43u² < 3N², s = 3 otherwise (43(B − u)² < 3N²);
  zero remainders at half and third points are rescued by θ − 1, −θ, θ − 2 (half43, third43; table computed numerically).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc43
import lang.Euc19
import lang.EisPrim

namespace Euc43

open FTAChain DetChain LibInt

theorem sq_le' (T N : Nat) (h : T ≤ N) : T * T ≤ N * N := Nat.mul_le_mul h h

theorem sq_lt (n N : Nat) (h : n < N) : n * n < N * N := by
  cases Nat.lt_or_ge (n * n) (N * N) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    have : (n + 1) * (n + 1) ≤ N * N := Nat.mul_le_mul h h
    have : (2 * n + 1) + n * n ≤ 0 + n * n := Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans this (Nat.le_trans h' (Nat.le_of_eq (by lib_nat))))
    exact Nat.not_succ_le_zero _ (GaussEuclid.le_cancel _ _ _ this)

theorem nrm_pair (n : Nat) : nrm (Int.ofNat n, 0) = n * n := Int.ofNat.inj (by rw [nrm_cast]; show Int.ofNat n * Int.ofNat n + Int.ofNat n * 0 + 11 * (0 * 0) = Int.ofNat n * Int.ofNat n; lib_int)

theorem zero_sq (x : Nat) (k : Nat) (h : (k + 1) * (x * x) ≤ 0) : x = 0 := by
  have h1 : (k + 1) * (x * x) = 0 := Nat.le_antisymm h (Nat.zero_le _)
  have h2 : x * x = 0 := by cases nat_mul_zero _ _ h1 with | inl e => exact Nat.noConfusion e | inr e => exact e
  cases nat_mul_zero _ _ h2 with | inl e => exact e | inr e => exact e

/-- u ≤ B (B = u + v, N = 3u + 2v): 3N² ≤ 43u² ⇒ 43v² < 3N². -/
theorem ineq43c (u v : Nat) (hu : 3 * ((3 * u + 2 * v) * (3 * u + 2 * v)) ≤ 43 * (u * u)) (hpos : 0 < 3 * u + 2 * v) :
    43 * (v * v) < 3 * ((3 * u + 2 * v) * (3 * u + 2 * v)) := by
  cases Nat.lt_or_ge (43 * (v * v)) (3 * ((3 * u + 2 * v) * (3 * u + 2 * v))) with
  | inl h => exact h
  | inr hc =>
    exfalso
    have h1 : 36 * (u * v) + 12 * (v * v) ≤ 16 * (u * u) := GaussEuclid.le_cancel _ _ (27 * (u * u)) (by
      calc 36 * (u * v) + 12 * (v * v) + 27 * (u * u) = 3 * ((3 * u + 2 * v) * (3 * u + 2 * v)) := by lib_nat
        _ ≤ 43 * (u * u) := hu
        _ = 16 * (u * u) + 27 * (u * u) := by lib_nat)
    have h2 : 27 * (u * u) + 36 * (u * v) ≤ 31 * (v * v) := GaussEuclid.le_cancel _ _ (12 * (v * v)) (by
      calc 27 * (u * u) + 36 * (u * v) + 12 * (v * v) = 3 * ((3 * u + 2 * v) * (3 * u + 2 * v)) := by lib_nat
        _ ≤ 43 * (v * v) := hc
        _ = 31 * (v * v) + 12 * (v * v) := by lib_nat)
    have u0 : u = 0 := by
      cases Nat.le_total v u with
      | inl hvu =>
        have a : 31 * (v * v) ≤ 31 * (u * v) := Nat.mul_le_mul_left 31 (Nat.le_trans (Nat.mul_le_mul_right v hvu) (Nat.le_of_eq rfl))
        have b : 27 * (u * u) + 36 * (u * v) ≤ 0 + 31 * (u * v) := Nat.le_trans h2 (Nat.le_trans a (Nat.le_of_eq (by lib_nat)))
        have c : 27 * (u * u) + 5 * (u * v) + 31 * (u * v) ≤ 0 + 31 * (u * v) := Nat.le_trans (Nat.le_of_eq (by lib_nat)) b
        have d := GaussEuclid.le_cancel _ _ _ c
        exact zero_sq u 26 (Nat.le_trans (Nat.le_add_right _ _) d)
      | inr huv =>
        have a : 36 * (u * u) ≤ 36 * (u * v) := Nat.mul_le_mul_left 36 (Nat.mul_le_mul_left u huv)
        have b : 36 * (u * u) ≤ 16 * (u * u) := Nat.le_trans a (Nat.le_trans (Nat.le_add_right _ _) h1)
        have c : 20 * (u * u) + 16 * (u * u) ≤ 0 + 16 * (u * u) := Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans b (Nat.le_of_eq (by lib_nat)))
        exact zero_sq u 19 (GaussEuclid.le_cancel _ _ _ c)
    subst u0
    have v0 : v = 0 := zero_sq v 11 (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans h1 (Nat.le_of_eq (by lib_nat))))
    subst v0; exact Nat.lt_irrefl 0 hpos

/-- B ≤ u (u = B + v, N = 3B + v): 3N² ≤ 43B² ⇒ 43v² < 3N². -/
theorem ineq43d (B v : Nat) (hB : 3 * ((3 * B + v) * (3 * B + v)) ≤ 43 * (B * B)) (hpos : 0 < 3 * B + v) :
    43 * (v * v) < 3 * ((3 * B + v) * (3 * B + v)) := by
  cases Nat.lt_or_ge (43 * (v * v)) (3 * ((3 * B + v) * (3 * B + v))) with
  | inl h => exact h
  | inr hc =>
    exfalso
    have h1 : 18 * (B * v) + 3 * (v * v) ≤ 16 * (B * B) := GaussEuclid.le_cancel _ _ (27 * (B * B)) (by
      calc 18 * (B * v) + 3 * (v * v) + 27 * (B * B) = 3 * ((3 * B + v) * (3 * B + v)) := by lib_nat
        _ ≤ 43 * (B * B) := hB
        _ = 16 * (B * B) + 27 * (B * B) := by lib_nat)
    have h2 : 27 * (B * B) + 18 * (B * v) ≤ 40 * (v * v) := GaussEuclid.le_cancel _ _ (3 * (v * v)) (by
      calc 27 * (B * B) + 18 * (B * v) + 3 * (v * v) = 3 * ((3 * B + v) * (3 * B + v)) := by lib_nat
        _ ≤ 43 * (v * v) := hc
        _ = 40 * (v * v) + 3 * (v * v) := by lib_nat)
    have B0 : B = 0 := by
      cases Nat.le_total v B with
      | inl hvB =>
        have a : 40 * (v * v) ≤ 40 * (B * v) := Nat.mul_le_mul_left 40 (Nat.mul_le_mul_right v hvB)
        have b : 27 * (B * B) + 18 * (B * v) ≤ 0 + 40 * (B * v) := Nat.le_trans h2 (Nat.le_trans a (Nat.le_of_eq (by lib_nat)))
        have c : 27 * (B * B) + 18 * (B * v) ≤ 0 + 40 * (B * B) := Nat.le_trans b (Nat.add_le_add_left (Nat.mul_le_mul_left 40 (Nat.mul_le_mul_left B hvB)) _)
        -- 27B² + 18Bv ≤ 40 Bv  ⇒ 27 B² ≤ 22 Bv ≤ 22 B²
        have d : 27 * (B * B) + 18 * (B * v) ≤ 22 * (B * v) + 18 * (B * v) := Nat.le_trans b (Nat.le_of_eq (by lib_nat))
        have e := GaussEuclid.le_cancel _ _ _ d
        have f : 27 * (B * B) ≤ 22 * (B * B) := Nat.le_trans e (Nat.mul_le_mul_left 22 (Nat.mul_le_mul_left B hvB))
        have g : 5 * (B * B) + 22 * (B * B) ≤ 0 + 22 * (B * B) := Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans f (Nat.le_of_eq (by lib_nat)))
        exact zero_sq B 4 (GaussEuclid.le_cancel _ _ _ g)
      | inr hBv =>
        have a : 18 * (B * B) ≤ 18 * (B * v) := Nat.mul_le_mul_left 18 (Nat.mul_le_mul_left B hBv)
        have b : 18 * (B * B) ≤ 16 * (B * B) := Nat.le_trans a (Nat.le_trans (Nat.le_add_right _ _) h1)
        have c : 2 * (B * B) + 16 * (B * B) ≤ 0 + 16 * (B * B) := Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans b (Nat.le_of_eq (by lib_nat)))
        exact zero_sq B 1 (GaussEuclid.le_cancel _ _ _ c)
    subst B0
    have v0 : v = 0 := zero_sq v 2 (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans h1 (Nat.le_of_eq (by lib_nat))))
    subst v0; exact Nat.lt_irrefl 0 hpos

theorem half43 (B : Nat) (d1 d2 N : Int) (hNB : N = 2 * Int.ofNat B) (hd2 : d2 = 1 ∨ d2 = -1) :
    ∃ s e : Int × Int, (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (d1 * Int.ofNat B, d2 * Int.ofNat B)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (N, 0))) = (Int.ofNat B, 0) := by
  rcases GaussInt.int_even_odd d1 with ⟨k, hk⟩ | ⟨k, hk⟩ <;> rcases hd2 with e2 | e2
  · refine ⟨(-1, 1), (k * -1 + -6, k * 1 + 0), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B)) + -((k * -1 + -6) * N + -((k * 1 + 0) * 0 + (k * 1 + 0) * 0 + (k * 1 + 0) * 0 + (k * 1   -- (+150 chars; full line in the Zenodo source)
      rw [hk, e2, hNB]; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((k * -1 + -6) * 0 + (k * 1 + 0) * N + (k * 1 + 0) * 0) = 0
      rw [hk, e2, hNB]; lib_int
  · refine ⟨(-1, 1), (k * -1 + 5, k * 1 + 0), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B)) + -((k * -1 + 5) * N + -((k * 1 + 0) * 0 + (k * 1 + 0) * 0 + (k * 1 + 0) * 0 + (k * 1 +  -- (+149 chars; full line in the Zenodo source)
      rw [hk, e2, hNB]; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((k * -1 + 5) * 0 + (k * 1 + 0) * N + (k * 1 + 0) * 0) = 0
      rw [hk, e2, hNB]; lib_int
  · refine ⟨(0, -1), (k * 0 + 5, k * -1 + -1), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)) + -((k * 0 + 5) * N + -((k * -1 + -1) * 0 + (k * -1 + -1) * 0 + (k * -1 + -1)  -- (+180 chars; full line in the Zenodo source)
      rw [hk, e2, hNB]; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((k * 0 + 5) * 0 + (k * -1 + -1) * N + (k * -1 + -1) * 0) = 0
      rw [hk, e2, hNB]; lib_int
  · refine ⟨(0, -1), (k * 0 + -6, k * -1 + 0), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)) + -((k * 0 + -6) * N + -((k * -1 + 0) * 0 + (k * -1 + 0) * 0 + (k * -1 + 0) *  -- (+170 chars; full line in the Zenodo source)
      rw [hk, e2, hNB]; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((k * 0 + -6) * 0 + (k * -1 + 0) * N + (k * -1 + 0) * 0) = 0
      rw [hk, e2, hNB]; lib_int

theorem third43 (B : Nat) (d1 d2 N : Int) (hNB : N = 3 * Int.ofNat B) (hd2 : d2 = 1 ∨ d2 = -1) :
    ∃ s e : Int × Int, ∃ c : Nat, (c = 1 ∨ c = 2) ∧ (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (d1 * Int.ofNat B, d2 * Int.ofNat B)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (N, 0))) = (Int.ofNat (c * B), 0) := by
  obtain ⟨q, hq | hq | hq⟩ := EisPrim.res3 d1 <;> rcases hd2 with e2 | e2
  · refine ⟨(-1, 1), (q * -1 + -4, q * 1 + 0), 1, Or.inl rfl, ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B)) + -((q * -1 + -4) * N + -((q * 1 + 0) * 0 + (q * 1 + 0) * 0 + (q * 1 + 0) * 0 + (q * 1   -- (+156 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + -4) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-1, 1), (q * -1 + 3, q * 1 + 0), 2, Or.inr rfl, ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B)) + -((q * -1 + 3) * N + -((q * 1 + 0) * 0 + (q * 1 + 0) * 0 + (q * 1 + 0) * 0 + (q * 1 +  -- (+155 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 2 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + 3) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-2, 1), (q * -2 + -5, q * 1 + 0), 2, Or.inr rfl, ?_⟩
    apply Prod.ext
    · show -2 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B)) + -((q * -2 + -5) * N + -((q * 1 + 0) * 0 + (q * 1 + 0) * 0 + (q * 1 + 0) * 0 + (q * 1   -- (+156 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 2 * Int.ofNat B; lib_int
    · show -2 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -2 + -5) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), (q * 0 + -4, q * -1 + 0), 1, Or.inl rfl, ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)) + -((q * 0 + -4) * N + -((q * -1 + 0) * 0 + (q * -1 + 0) * 0 + (q * -1 + 0) *  -- (+176 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * 0 + -4) * 0 + (q * -1 + 0) * N + (q * -1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), ((q + -1) * 0 + 3, (q + -1) * -1 + -1), 2, Or.inr rfl, ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)) + -(((q + -1) * 0 + 3) * N + -(((q + -1) * -1 + -1) * 0 + ((q + -1) * -1 + -1  -- (+270 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 2 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -(((q + -1) * 0 + 3) * 0 + ((q + -1) * -1 + -1) * N + ((q + -1) * -1 + -1) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-2, 1), ((q + -1) * -2 + 2, (q + -1) * 1 + 1), 1, Or.inl rfl, ?_⟩
    apply Prod.ext
    · show -2 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B)) + -(((q + -1) * -2 + 2) * N + -(((q + -1) * 1 + 1) * 0 + ((q + -1) * 1 + 1) * 0 + ((q +  -- (+239 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -2 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -(((q + -1) * -2 + 2) * 0 + ((q + -1) * 1 + 1) * N + ((q + -1) * 1 + 1) * 0) = 0
      rw [hq, e2, hNB]; lib_int

theorem three_cancel (a c : Int) (h : 3 * a = 3 * c) : a = c := by
  have h0 : 3 * (a + -c) = 0 := by rw [show 3 * (a + -c) = 3 * a + -(3 * c) by lib_int, h]; lib_int
  cases int_noZD _ _ h0 with
  | inl e => exact absurd e (fun e => Nat.noConfusion (Int.ofNat.inj e))
  | inr e => calc a = (a + -c) + c := by lib_int
      _ = c := by rw [e]; show (0 : Int) + c = c; lib_int

/-- **The Dedekind–Hasse step on a remainder (d = 43).** -/
theorem dh43_r (N0 : Nat) (r1 r2 : Int) (h2 : 2 * Int.natAbs r2 ≤ N0 + 1) (ht : Int.natAbs (2 * r1 + r2) ≤ N0 + 1) :
    ∃ s e : Int × Int, nrm ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring43.ring43 LibInt.intR).zero → s = (Ring43.ring43 LibInt.intR).one) := by
  have h4 := four_nrm (r1, r2)
  generalize hT : Int.natAbs (2 * r1 + r2) = T at h4 ht
  generalize hB : Int.natAbs r2 = B at h4 h2
  have h4' : 4 * nrm (r1, r2) = T * T + 43 * (B * B) := h4
  cases Nat.lt_or_ge (T * T + 43 * (B * B)) (4 * ((N0 + 1) * (N0 + 1))) with
  | inl hl =>
    refine ⟨(Ring43.ring43 LibInt.intR).one, (Ring43.ring43 LibInt.intR).zero, ?_, fun _ => rfl⟩
    have e : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).one (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).zero (Int.ofNat (N0 + 1), 0))) = (r1, r2) := by lib_ring
    rw [e]
    exact nrm_lt (r1, r2) (N0 + 1) (by rw [hT, hB]; exact hl)
  | inr hg =>
    have h3 : 3 * ((N0 + 1) * (N0 + 1)) ≤ 43 * (B * B) := GaussEuclid.le_cancel _ _ ((N0 + 1) * (N0 + 1)) (by
      calc 3 * ((N0 + 1) * (N0 + 1)) + (N0 + 1) * (N0 + 1) = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat
        _ ≤ T * T + 43 * (B * B) := hg
        _ ≤ (N0 + 1) * (N0 + 1) + 43 * (B * B) := Nat.add_le_add_right (sq_le' T _ ht) _
        _ = 43 * (B * B) + (N0 + 1) * (N0 + 1) := by lib_nat)
    obtain ⟨u, hu⟩ := EisEuclid.le_dest _ _ h2
    have hN : N0 + 1 = 2 * B + u := by rw [← hu]; lib_nat
    have hNi : Int.ofNat (N0 + 1) = 2 * Int.ofNat B + Int.ofNat u := congrArg Int.ofNat hN
    have hsg : ∃ d2 : Int, (d2 = 1 ∨ d2 = -1) ∧ r2 = d2 * Int.ofNat B := by
      rcases EisEuclid.sgn r2 with e | e
      · exact ⟨1, Or.inl rfl, by rw [e, hB]; lib_int⟩
      · exact ⟨-1, Or.inr rfl, by rw [e, hB]; lib_int⟩
    obtain ⟨d2, hd2, hr2⟩ := hsg
    have hdd : d2 * d2 = 1 := by rcases hd2 with e | e <;> rw [e] <;> rfl
    have e2N : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
    have hBpos : ∀ c : Nat, c * B = 0 → c = 0 ∨ B = 0 := fun c h => nat_mul_zero c B h
    -- finishing a special case: remainder (cB, 0) with cB < N, B ≠ 0
    have fin : ∀ (sv ev : Int × Int) (n : Nat), n < N0 + 1 → n ≠ 0 →
        (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul sv (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ev (Int.ofNat (N0 + 1), 0))) = (Int.ofNat n, 0) →
        ∃ s e : Int × Int, nrm ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
          ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring43.ring43 LibInt.intR).zero → s = (Ring43.ring43 LibInt.intR).one) :=
      fun sv ev n hn hn0 he => ⟨sv, ev, by rw [he, nrm_pair]; exact sq_lt n _ hn, fun h0 => by
        have : Int.ofNat n = 0 := congrArg Prod.fst (he.symm.trans h0)
        exact absurd (Int.ofNat.inj this) hn0⟩
    cases Nat.lt_or_ge (43 * (u * u)) (3 * ((N0 + 1) * (N0 + 1))) with
    | inl hu2 =>
      -- s = 2
      obtain ⟨d1, t', hrd, hb1⟩ := GaussEuclid.rnd (2 * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1))) (2 * N0 + 1)
      have hrd' : 2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = t' := by
        calc 2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = 2 * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1)) + -(d1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
          _ = t' := by rw [hrd, e2N]; lib_int
      have hT' : Int.natAbs t' ≤ N0 + 1 := Nat.le_of_mul_le_mul_left (Nat.le_trans hb1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
      have hu' : Int.natAbs (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = u := by
        rw [hr2, hNi]
        rcases hd2 with e | e <;> rw [e]
        · rw [show 2 * (1 * Int.ofNat B) + -(1 * (2 * Int.ofNat B + Int.ofNat u)) = -Int.ofNat u by lib_int]; exact Int.natAbs_neg _
        · rw [show 2 * (-1 * Int.ofNat B) + -(-1 * (2 * Int.ofNat B + Int.ofNat u)) = Int.ofNat u by lib_int]; rfl
      have hexpr : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (2, 0) (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul (d1, d2) (Int.ofNat (N0 + 1), 0))) = (2 * r1 + -(d1 * Int.ofNat (N0 + 1)), 2 * r2 + -(d2 * Int.ofNat (N0 + 1))) := by
        apply Prod.ext
        · show 2 * r1 + -(0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2) + -(d1 * Int.ofNat (N0 + 1) + -(d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0)) = 2 * r1 + -(d1 * Int.ofNat (N0 + 1)); lib_int
        · show 2 * r2 + 0 * r1 + 0 * r2 + -(d1 * 0 + d2 * Int.ofNat (N0 + 1) + d2 * 0) = 2 * r2 + -(d2 * Int.ofNat (N0 + 1)); lib_int
      by_cases hz : (2 * r1 + -(d1 * Int.ofNat (N0 + 1)), 2 * r2 + -(d2 * Int.ofNat (N0 + 1))) = (Ring43.ring43 LibInt.intR).zero
      · have z1 : 2 * r1 + -(d1 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.fst hz
        have z2 : 2 * r2 + -(d2 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.snd hz
        have u0 : u = 0 := by rw [← hu', z2]; rfl
        have hNB : Int.ofNat (N0 + 1) = 2 * Int.ofNat B := by rw [hNi, u0]; show _ + 0 = _; lib_int
        have hr1 : r1 = d1 * Int.ofNat B := W36Mul.two_cancel_int _ _ (by
          calc 2 * r1 = 2 * r1 + -(d1 * Int.ofNat (N0 + 1)) + d1 * Int.ofNat (N0 + 1) := by lib_int
            _ = 2 * (d1 * Int.ofNat B) := by rw [z1, hNB]; lib_int)
        have hB0 : B ≠ 0 := fun e => by rw [e, u0] at hN; exact Nat.noConfusion hN
        obtain ⟨sv, ev, he⟩ := half43 B d1 d2 (Int.ofNat (N0 + 1)) hNB hd2
        rw [← hr1, ← hr2] at he
        exact fin sv ev B (by rw [hN, u0]; show B < 2 * B + 0; rw [show 2 * B + 0 = B + B by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.pos_of_ne_zero hB0)) hB0 he
      · refine ⟨(2, 0), (d1, d2), ?_, fun h0 => absurd (hexpr.symm.trans h0) hz⟩
        rw [hexpr]
        apply nrm_lt
        show Int.natAbs (2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1)))) * Int.natAbs (2 * (2 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (2 * r2 + -(d2 * Int.ofNat (N0 + 1))))
          + 43 * (Int.natAbs (2 * r2 + -(d2 * Int.ofNat (N0 + 1))) * Int.natAbs (2 * r2 + -(d2 * Int.ofNat (N0 + 1)))) < _
        rw [hrd', hu']
        calc Int.natAbs t' * Int.natAbs t' + 43 * (u * u) < (N0 + 1) * (N0 + 1) + 3 * ((N0 + 1) * (N0 + 1)) := Nat.add_lt_add_of_le_of_lt (sq_le' _ _ hT') hu2
          _ = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat
    | inr hu3 =>
      -- s = 3
      obtain ⟨d1, t', hrd, hb1⟩ := GaussEuclid.rnd (3 * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1))) (2 * N0 + 1)
      have hrd' : 2 * (3 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (3 * r2 + -(d2 * Int.ofNat (N0 + 1))) = t' := by
        calc 2 * (3 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (3 * r2 + -(d2 * Int.ofNat (N0 + 1))) = 3 * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1)) + -(d1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
          _ = t' := by rw [hrd, e2N]; lib_int
      have hT' : Int.natAbs t' ≤ N0 + 1 := Nat.le_of_mul_le_mul_left (Nat.le_trans hb1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
      -- |3 r2 − d2 N| = |B − u|
      have hw : ∃ v : Nat, Int.natAbs (3 * r2 + -(d2 * Int.ofNat (N0 + 1))) = v ∧ 43 * (v * v) < 3 * ((N0 + 1) * (N0 + 1)) := by
        cases Nat.le_total u B with
        | inl huB =>
          obtain ⟨v, hv⟩ := EisEuclid.le_dest _ _ huB
          refine ⟨v, ?_, ?_⟩
          · rw [hr2, hNi, ← show Int.ofNat (v + u) = Int.ofNat B from congrArg Int.ofNat hv]
            show Int.natAbs (3 * (d2 * (Int.ofNat v + Int.ofNat u)) + -(d2 * (2 * (Int.ofNat v + Int.ofNat u) + Int.ofNat u))) = v
            rcases hd2 with e | e <;> rw [e]
            · rw [show 3 * (1 * (Int.ofNat v + Int.ofNat u)) + -(1 * (2 * (Int.ofNat v + Int.ofNat u) + Int.ofNat u)) = Int.ofNat v by lib_int]; rfl
            · rw [show 3 * (-1 * (Int.ofNat v + Int.ofNat u)) + -(-1 * (2 * (Int.ofNat v + Int.ofNat u) + Int.ofNat u)) = -Int.ofNat v by lib_int]; exact Int.natAbs_neg _
          · rw [hN, ← hv, show 2 * (v + u) + u = 3 * u + 2 * v by lib_nat]
            exact ineq43c u v (by rw [← show 2 * (v + u) + u = 3 * u + 2 * v by lib_nat, hv, ← hN]; exact hu3) (by rw [← show 2 * (v + u) + u = 3 * u + 2 * v by lib_nat, hv, ← hN]; exact Nat.zero_lt_succ N0)
        | inr hBu =>
          obtain ⟨v, hv⟩ := EisEuclid.le_dest _ _ hBu
          refine ⟨v, ?_, ?_⟩
          · rw [hr2, hNi, ← show Int.ofNat (v + B) = Int.ofNat u from congrArg Int.ofNat hv]
            show Int.natAbs (3 * (d2 * Int.ofNat B) + -(d2 * (2 * Int.ofNat B + (Int.ofNat v + Int.ofNat B)))) = v
            rcases hd2 with e | e <;> rw [e]
            · rw [show 3 * (1 * Int.ofNat B) + -(1 * (2 * Int.ofNat B + (Int.ofNat v + Int.ofNat B))) = -Int.ofNat v by lib_int]; exact Int.natAbs_neg _
            · rw [show 3 * (-1 * Int.ofNat B) + -(-1 * (2 * Int.ofNat B + (Int.ofNat v + Int.ofNat B))) = Int.ofNat v by lib_int]; rfl
          · rw [hN, ← hv, show 2 * B + (v + B) = 3 * B + v by lib_nat]
            exact ineq43d B v (by rw [← show 2 * B + (v + B) = 3 * B + v by lib_nat, hv, ← hN]; exact h3) (by rw [← show 2 * B + (v + B) = 3 * B + v by lib_nat, hv, ← hN]; exact Nat.zero_lt_succ N0)
      obtain ⟨v, hv1, hv2⟩ := hw
      have hexpr : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (3, 0) (r1, r2)) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul (d1, d2) (Int.ofNat (N0 + 1), 0))) = (3 * r1 + -(d1 * Int.ofNat (N0 + 1)), 3 * r2 + -(d2 * Int.ofNat (N0 + 1))) := by
        apply Prod.ext
        · show 3 * r1 + -(0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2) + -(d1 * Int.ofNat (N0 + 1) + -(d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0)) = 3 * r1 + -(d1 * Int.ofNat (N0 + 1)); lib_int
        · show 3 * r2 + 0 * r1 + 0 * r2 + -(d1 * 0 + d2 * Int.ofNat (N0 + 1) + d2 * 0) = 3 * r2 + -(d2 * Int.ofNat (N0 + 1)); lib_int
      by_cases hz : (3 * r1 + -(d1 * Int.ofNat (N0 + 1)), 3 * r2 + -(d2 * Int.ofNat (N0 + 1))) = (Ring43.ring43 LibInt.intR).zero
      · have z1 : 3 * r1 + -(d1 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.fst hz
        have z2 : 3 * r2 + -(d2 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.snd hz
        -- N = 3B, r1 = d1 B
        have hN3 : Int.ofNat (N0 + 1) = 3 * Int.ofNat B := by
          calc Int.ofNat (N0 + 1) = d2 * d2 * Int.ofNat (N0 + 1) := by rw [hdd]; lib_int
            _ = d2 * (3 * r2) := by rw [show 3 * r2 = d2 * Int.ofNat (N0 + 1) by rw [show 3 * r2 = 3 * r2 + -(d2 * Int.ofNat (N0 + 1)) + d2 * Int.ofNat (N0 + 1) by lib_int, z2]; lib_int]; lib_int
            _ = 3 * Int.ofNat B := by rw [hr2, show d2 * (3 * (d2 * Int.ofNat B)) = 3 * (d2 * d2) * Int.ofNat B by lib_int, hdd]; lib_int
        have hr1 : r1 = d1 * Int.ofNat B := three_cancel _ _ (by
          calc 3 * r1 = 3 * r1 + -(d1 * Int.ofNat (N0 + 1)) + d1 * Int.ofNat (N0 + 1) := by lib_int
            _ = 3 * (d1 * Int.ofNat B) := by rw [z1, hN3]; lib_int)
        have hB0 : B ≠ 0 := fun e => by
          have : Int.ofNat (N0 + 1) = 0 := by rw [hN3, e]; rfl
          exact Nat.noConfusion (Int.ofNat.inj this)
        have hN3n : N0 + 1 = 3 * B := Int.ofNat.inj (hN3.trans (by rfl))
        obtain ⟨sv, ev, c, hc, he⟩ := third43 B d1 d2 (Int.ofNat (N0 + 1)) hN3 hd2
        rw [← hr1, ← hr2] at he
        refine fin sv ev (c * B) ?_ ?_ he
        · rw [hN3n]; rcases hc with e | e <;> rw [e]
          · rw [show 3 * B = 1 * B + (B + B) by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.lt_of_lt_of_le (Nat.pos_of_ne_zero hB0) (Nat.le_add_right _ _))
          · rw [show 3 * B = 2 * B + B by lib_nat]; exact Nat.lt_add_of_pos_right (Nat.pos_of_ne_zero hB0)
        · intro e0; rcases hBpos c e0 with e | e
          · rcases hc with h | h <;> rw [h] at e <;> exact Nat.noConfusion e
          · exact hB0 e
      · refine ⟨(3, 0), (d1, d2), ?_, fun h0 => absurd (hexpr.symm.trans h0) hz⟩
        rw [hexpr]
        apply nrm_lt
        show Int.natAbs (2 * (3 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (3 * r2 + -(d2 * Int.ofNat (N0 + 1)))) * Int.natAbs (2 * (3 * r1 + -(d1 * Int.ofNat (N0 + 1))) + (3 * r2 + -(d2 * Int.ofNat (N0 + 1))))
          + 43 * (Int.natAbs (3 * r2 + -(d2 * Int.ofNat (N0 + 1))) * Int.natAbs (3 * r2 + -(d2 * Int.ofNat (N0 + 1)))) < _
        rw [hrd', hv1]
        calc Int.natAbs t' * Int.natAbs t' + 43 * (v * v) < (N0 + 1) * (N0 + 1) + 3 * ((N0 + 1) * (N0 + 1)) := Nat.add_lt_add_of_le_of_lt (sq_le' _ _ hT') hv2
          _ = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat

end Euc43

#print axioms Euc43.sq_le'
#print axioms Euc43.sq_lt
#print axioms Euc43.nrm_pair
#print axioms Euc43.zero_sq
#print axioms Euc43.ineq43c
#print axioms Euc43.ineq43d
#print axioms Euc43.half43
#print axioms Euc43.third43
#print axioms Euc43.three_cancel
#print axioms Euc43.dh43_r

#print axioms: 10 theorems, no axioms.

78.298 gcd (\(d=43\))

Bezout via the Dedekind–Hasse step.

\[ \exists d=x\alpha +y\beta ,\ d\mid \alpha ,\beta . \]

lang/Gcd43.lean

/-
  lang/Gcd43 — T48/T50, T48: **gcd as a combination in ℤ[(1 + √−43)/2]** (a PID that is not norm-Euclidean).
    edh           for β ≠ 0: sα − qβ with N < N(β), and = 0 only for s = 1          (Euc43.dh_r on the two-step remainder)
    gaux          from any nonzero d = xα + yβ: either d | α, d | β, or a nonzero element of smaller norm in the ideal
    egcd_exists   d = xα + yβ dividing α and β
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.DH43

namespace Euc43

open FTAChain DetChain LibInt

theorem lt_of_mul (a N : Nat) (h : a * (N + 1) < (N + 1) * (N + 1)) : a < N + 1 := by
  cases Nat.lt_or_ge a (N + 1) with
  | inl h' => exact h'
  | inr h' => exact absurd (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_right _ h')) (Nat.lt_irrefl _)

/-- **The Dedekind–Hasse step.** -/
theorem edh (α β : Int × Int) (N0 : Nat) (hβ : nrm β = N0 + 1) :
    ∃ s q : Int × Int, nrm ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q β))) < N0 + 1 ∧
      (((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q β))) = (Ring43.ring43 LibInt.intR).zero → s = (Ring43.ring43 LibInt.intR).one) := by
  have hN : en β = Int.ofNat (N0 + 1) := by rw [← nrm_cast, hβ]
  generalize hS : (Ring43.ring43 LibInt.intR).mul α (cj β) = S
  obtain ⟨q2, r2, h2, b2⟩ := GaussEuclid.rnd S.2 N0
  obtain ⟨q1, t, h1, b1⟩ := GaussEuclid.rnd (2 * S.1 + r2) (2 * N0 + 1)
  have hT : Int.natAbs (2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2) ≤ N0 + 1 := by
    have e : 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = t := by
      have e2 : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
      calc 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = (2 * S.1 + r2) + -(q1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
        _ = t := by rw [h1, ← e2]; lib_int
    rw [e]; exact Nat.le_of_mul_le_mul_left (Nat.le_trans b1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
  obtain ⟨s, e, hlt, hzero⟩ := dh43_r N0 (S.1 + -(q1 * Int.ofNat (N0 + 1))) r2 b2 hT
  have hSr : S = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (q1, q2) (Int.ofNat (N0 + 1), 0)) (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) := by
    apply Prod.ext
    · show S.1 = q1 * Int.ofNat (N0 + 1) + -(q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0) + (S.1 + -(q1 * Int.ofNat (N0 + 1))); lib_int
    · show S.2 = q1 * 0 + q2 * Int.ofNat (N0 + 1) + q2 * 0 + r2; rw [h2]; show q2 * Int.ofNat (N0 + 1) + r2 = _; lib_int
  generalize hr : (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) = r at hSr hlt hzero
  refine ⟨s, (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (q1, q2)) e, ?_, ?_⟩
  all_goals
    have key : (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
        = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s r) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) := by
      rw [show (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
          = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s ((Ring43.ring43 LibInt.intR).mul α (cj β))) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (q1, q2)) e) ((Ring43.ring43 LibInt.intR).mul β (cj β)))) by lib_ring,
        hS, mul_cj, hN, hSr]
      lib_ring
  · -- norm bound
    have hn : en ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (q1, q2)) e) β))) * Int.ofNat (N0 + 1)
        = en ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s r) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      rw [← key, en_mul, en_cj, hN]
    apply lt_of_mul
    have : nrm ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s (q1, q2)) e) β))) * (N0 + 1)
        = nrm ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s r) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm _) * Int.ofNat (N0 + 1) = Int.ofNat (nrm _)
      rw [nrm_cast, nrm_cast]; exact hn
    rw [this]; exact hlt
  · intro h0
    apply hzero
    rw [← key, h0]; lib_ring

theorem nrm_succ (d : Int × Int) (hd : d ≠ (Ring43.ring43 LibInt.intR).zero) : ∃ N0, nrm d = N0 + 1 := by
  cases h : nrm d with
  | zero => exact absurd (nrm_zero d h) hd
  | succ N0 => exact ⟨N0, rfl⟩

theorem gaux (α β : Int × Int) : ∀ G (d x y : Int × Int), d = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x α) ((Ring43.ring43 LibInt.intR).mul y β) →
    nrm d ≤ G → d ≠ (Ring43.ring43 LibInt.intR).zero →
    ∃ d' x' y', d' = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x' α) ((Ring43.ring43 LibInt.intR).mul y' β) ∧ Dvd' d' α ∧ Dvd' d' β
  | 0, d, _, _, _, hG, hd => absurd (nrm_zero d (Nat.le_antisymm hG (Nat.zero_le _))) hd
  | G + 1, d, x, y, hdef, hG, hd => by
    obtain ⟨N0, hN0⟩ := nrm_succ d hd
    obtain ⟨s, q, hlt, hz⟩ := edh α d N0 hN0
    have hle : ∀ ρ, nrm ρ < N0 + 1 → nrm ρ ≤ G := fun ρ h => Nat.le_of_lt_succ (Nat.lt_of_lt_of_le h (Nat.le_trans (Nat.le_of_eq hN0.symm) hG))
    by_cases hρ : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q d)) = (Ring43.ring43 LibInt.intR).zero
    · have s1 := hz hρ
      rw [s1] at hρ
      have hα : Dvd' d α := ⟨q, by
        calc α = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).one α) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q d))) ((Ring43.ring43 LibInt.intR).mul d q) := by lib_ring
          _ = _ := by rw [hρ]; lib_ring⟩
      obtain ⟨s', q', hlt', hz'⟩ := edh β d N0 hN0
      by_cases hρ' : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul s' β) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q' d)) = (Ring43.ring43 LibInt.intR).zero
      · have s1' := hz' hρ'
        rw [s1'] at hρ'
        exact ⟨d, x, y, hdef, hα, ⟨q', by
          calc β = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).one β) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q' d))) ((Ring43.ring43 LibInt.intR).mul d q') := by lib_ring
            _ = _ := by rw [hρ']; lib_ring⟩⟩
      · exact gaux α β G _ ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q' x)) ((Ring43.ring43 LibInt.intR).add s' ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q' y)))
          (by rw [hdef]; lib_ring) (hle _ hlt') hρ'
    · exact gaux α β G _ ((Ring43.ring43 LibInt.intR).add s ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q x))) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul q y))
        (by rw [hdef]; lib_ring) (hle _ hlt) hρ

/-- **gcd as a combination in ℤ[(1 + √−43)/2].** -/
theorem egcd_exists (α β : Int × Int) :
    ∃ d x y, d = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x α) ((Ring43.ring43 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β := by
  by_cases hb : β = (Ring43.ring43 LibInt.intR).zero
  · exact egcd_base α β hb
  · exact gaux α β (nrm β) β (Ring43.ring43 LibInt.intR).zero (Ring43.ring43 LibInt.intR).one (by lib_ring) (Nat.le_refl _) hb

end Euc43

#print axioms Euc43.lt_of_mul
#print axioms Euc43.edh
#print axioms Euc43.nrm_succ
#print axioms Euc43.gaux
#print axioms Euc43.egcd_exists

#print axioms: 5 theorems, no axioms.

78.299 Coprime splitting (\(d=43\))

Splitting by coprime norms.

\[ N(\gamma )=mn\Rightarrow \gamma =\alpha \beta . \]

lang/Cop43.lean

/-
  lang/Cop43 — T48/T50, T48: splitting by coprime norms in ℤ[(1+√−43)/2].

  If N(γ) = mn with sm + tn = 1 (m, n ≥ 1), then γ = αβ with N(α) = m, N(β) = n (N(a + bθ) = a² + ab + 3b²):
  α = gcd(γ, m), e = gcd(γ, n) (Euc43.egcd_exists), αe | γ by Euclid's lemma, γ | αe since mn = γγ̄, so γ = αe·v with v a unit;
  then N(α) | m, N(e) | n and N(α)N(e) = mn give N(α) = m. (Port of GaussCoprime.coprime_split.)
    en_emb, emb_mul, en_dvd, emb_add, en_zero_imp, cancel_left, en_nonneg_one, ecoprime_split
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Gcd43
import lang.GaussCoprime

namespace Cop43

open FTAChain DetChain LibInt Euc43

def emb (m : Int) : Int × Int := (m, 0)

theorem en_emb (m : Int) : en (emb m) = m * m := by show m * m + m * 0 + 11 * (0 * 0) = m * m; lib_int

theorem emb_mul (a b : Int) : (Ring43.ring43 LibInt.intR).mul (emb a) (emb b) = emb (a * b) := by
  apply Prod.ext
  · show a * b + -(0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0) = a * b; lib_int
  · show a * 0 + 0 * b + 0 * 0 = 0; lib_int

theorem en_dvd (d γ : Int × Int) (h : Dvd' d γ) : ∃ k, en γ = en d * k :=
  match h with
  | ⟨c, hc⟩ => ⟨en c, by rw [hc, en_mul]⟩

theorem emb_add (a b : Int) : emb (a + b) = (Ring43.ring43 LibInt.intR).add (emb a) (emb b) := by
  apply Prod.ext
  · rfl
  · show (0 : Int) = 0 + 0; rfl

theorem en_zero_imp (z : Int × Int) (h : en z = 0) : z = (Ring43.ring43 LibInt.intR).zero := by
  apply nrm_zero
  apply Int.ofNat.inj
  rw [nrm_cast]; exact h

theorem cancel_left (γ z : Int × Int) (hγ : en γ ≠ 0) (h : (Ring43.ring43 LibInt.intR).mul γ z = (Ring43.ring43 LibInt.intR).zero) : z = (Ring43.ring43 LibInt.intR).zero := by
  have : en γ * en z = 0 := by rw [← en_mul, h]; rfl
  cases int_noZD _ _ this with
  | inl e => exact absurd e hγ
  | inr e => exact en_zero_imp z e

theorem en_nonneg_one (z : Int × Int) (h : en z = -1) : False := by
  rw [← nrm_cast] at h
  exact Int.noConfusion h

/-- **Coprime splitting**: N(γ) = mn, sm + tn = 1 ⇒ γ = αβ with N(α) = m, N(β) = n. -/
theorem ecoprime_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) :
    ∃ α β, γ = (Ring43.ring43 LibInt.intR).mul α β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match egcd_exists γ (emb (Int.ofNat (m + 1))), egcd_exists γ (emb (Int.ofNat (n + 1))) with
  | ⟨d, x, y, hd, ⟨u, hu⟩, ⟨c1, hc1⟩⟩, ⟨e, x', y', he, ⟨u', hu'⟩, ⟨c2, hc2⟩⟩ =>
    have hco : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (emb t) c2) e) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (emb s) c1) d) = (Ring43.ring43 LibInt.intR).one := by
      have h1 : (Ring43.ring43 LibInt.intR).one = emb (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; rfl
      rw [h1, emb_add, ← emb_mul, ← emb_mul, hc1, hc2]; lib_ring
    -- e | u, so γ = d e v
    match eeuclid e d u _ _ hco ⟨u', by rw [← hu, hu']⟩ with
    | ⟨v, hv⟩ =>
    have hγdev : γ = (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul d e) v := by rw [hu, hv]; lib_ring
    -- γ | d e
    have hmn : (Ring43.ring43 LibInt.intR).mul γ (cj γ) = emb (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by
      rw [mul_cj, hγ]; rfl
    have hW : (Ring43.ring43 LibInt.intR).mul d e = (Ring43.ring43 LibInt.intR).mul γ ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul x x') γ) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring4  -- (+146 chars; full line in the Zenodo source)
      rw [hd, he]
      have : (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul y y') ((Ring43.ring43 LibInt.intR).mul γ (cj γ)) = (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul y y') ((Ring43.ring43 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1)))) := by rw [hmn, emb_mul]
      calc (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x γ) ((Ring43.ring43 LibInt.intR).mul y (emb (Int.ofNat (m + 1))))) ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul x' γ) ((Ring43.ring43 LibInt.intR).mul y' (emb (Int.ofNat (n + 1)))))
          = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul γ ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul x x') γ) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul y x') (emb (In  -- (+20 chars; full line in the Zenodo source)
              ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul y y') ((Ring43.ring43 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1))))) := by lib_ring
        _ = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul γ ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul x x') γ) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul y x') (emb (In  -- (+20 chars; full line in the Zenodo source)
              ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul y y') ((Ring43.ring43 LibInt.intR).mul γ (cj γ))) := by rw [this]
        _ = _ := by lib_ring
    generalize (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul x x') γ) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1)))) ((Ring43.ring  -- (+77 chars; full line in the Zenodo source)
    have hγ0 : en γ ≠ 0 := by rw [hγ]; exact fun e => Nat.noConfusion (Int.ofNat.inj e)
    have hWv : (Ring43.ring43 LibInt.intR).mul W v = (Ring43.ring43 LibInt.intR).one := by
      have h0 : (Ring43.ring43 LibInt.intR).mul γ ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul W v) ((Ring43.ring43 LibInt.intR).neg (Ring43.ring43 LibInt.intR).one)) = (Ring43.ring43 LibInt.intR).zero := by
        rw [show (Ring43.ring43 LibInt.intR).mul γ ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul W v) ((Ring43.ring43 LibInt.intR).neg (Ring43.ring43 LibInt.intR).one)) = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul γ W) v) ((Ring43.ring43 LibInt.intR).neg γ) by lib_ring, ← hW, ← hγdev]; lib_ring
      have := cancel_left γ _ hγ0 h0
      calc (Ring43.ring43 LibInt.intR).mul W v = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul W v) ((Ring43.ring43 LibInt.intR).neg (Ring43.ring43 LibInt.intR).one)) (Ring43.ring43 LibInt.intR).one := by lib_ring
        _ = (Ring43.ring43 LibInt.intR).one := by rw [this]; lib_ring
    have hv1 : en v = 1 := by
      have : en W * en v = 1 := by rw [← en_mul, hWv]; rfl
      cases GaussCoprime.int_unit _ _ (by rw [show en v * en W = en W * en v by lib_int]; exact this) with
      | inl h => exact h
      | inr h => exact absurd h (en_nonneg_one v)
    -- norms
    have hN : en d * en e = Int.ofNat (m + 1) * Int.ofNat (n + 1) := by
      have := congrArg en hγdev
      rw [en_mul, en_mul, hv1, hγ] at this
      rw [show en d * en e = en d * en e * 1 by lib_int, ← this]; rfl
    have hdm : ∃ a, Int.ofNat (m + 1) = en d * a := by
      have k1 : Int.ofNat (m + 1) * Int.ofNat (m + 1) = en d * en c1 := by rw [← en_emb, hc1, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en d * en u := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu, en_mul]
      exact ⟨s * en c1 + t * en u, by
        calc Int.ofNat (m + 1) = Int.ofNat (m + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = s * (Int.ofNat (m + 1) * Int.ofNat (m + 1)) + t * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en d * (s * en c1 + t * en u) := by rw [k1, k2]; lib_int⟩
    have hen' : ∃ b, Int.ofNat (n + 1) = en e * b := by
      have k1 : Int.ofNat (n + 1) * Int.ofNat (n + 1) = en e * en c2 := by rw [← en_emb, hc2, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en e * en u' := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu', en_mul]
      exact ⟨t * en c2 + s * en u', by
        calc Int.ofNat (n + 1) = Int.ofNat (n + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = t * (Int.ofNat (n + 1) * Int.ofNat (n + 1)) + s * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en e * (t * en c2 + s * en u') := by rw [k1, k2]; lib_int⟩
    match hdm, hen' with
    | ⟨a, ha⟩, ⟨b, hb⟩ =>
      have hab : a * b = 1 := by
        have h0 : (en d * en e) * (a * b + -1) = 0 := by
          rw [show (en d * en e) * (a * b + -1) = (en d * a) * (en e * b) + -(en d * en e) by lib_int, ← ha, ← hb, hN]; lib_int
        cases int_noZD _ _ h0 with
        | inl e0 => exact absurd (by rw [hN] at e0; exact e0) (fun e1 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = 0 => Nat.noConfusion (Int.ofNat.inj e1))
        | inr e0 => calc a * b = (a * b + -1) + 1 := by lib_int
            _ = 1 := by rw [e0]; rfl
      have pos : ∀ (k : Nat) (z : Int × Int) (c : Int), Int.ofNat (k + 1) = en z * c → c = 1 ∨ c = -1 → c = 1 := fun k z c h hc => by
        cases hc with
        | inl h1 => exact h1
        | inr h1 =>
          exfalso
          rw [h1, ← nrm_cast] at h
          have : Int.ofNat (k + 1) + Int.ofNat (nrm z) = 0 := by rw [h]; lib_int
          have h2 : k + nrm z + 1 = 0 := by rw [show k + nrm z + 1 = k + 1 + nrm z by lib_nat]; exact Int.ofNat.inj (show Int.ofNat (k + 1 + nrm z) = Int.ofNat 0 from this)
          exact Nat.noConfusion h2
      have a1 := pos m d a ha (GaussCoprime.int_unit a b hab)
      have b1 := pos n e b hb (GaussCoprime.int_unit b a (by rw [show b * a = a * b by lib_int]; exact hab))
      refine ⟨d, (Ring43.ring43 LibInt.intR).mul e v, by rw [hγdev]; lib_ring, by rw [ha, a1]; lib_int, by rw [en_mul, hv1, hb, b1]⟩

end Cop43

#print axioms Cop43.en_emb
#print axioms Cop43.emb_mul
#print axioms Cop43.en_dvd
#print axioms Cop43.emb_add
#print axioms Cop43.en_zero_imp
#print axioms Cop43.cancel_left
#print axioms Cop43.en_nonneg_one
#print axioms Cop43.ecoprime_split

#print axioms: 8 theorems, no axioms.

78.300 The primary class of 1849a (Legendre symbol)

\(H_{43}=\{\chi _{43}(\rho )=1\}\); multiplicativity from \(\chi \), no residue enumeration.

\[ H_{43}=\{a+b\theta :\ \chi _{43}(a+22b)=1\}. \]

lang/Prim43.lean

/-
  lang/Prim43 — T48/T50: **the primary class in ℤ[(1 + √−43)/2] through the Legendre symbol** (no residue enumeration):
  ρ(a + bθ) = a + 22b is the reduction mod √−43 (θ ≡ 22), and H43 = {z : χ₄₃(ρ z) = 1} (QChar.chi with p = 43).
    prime43      43 is prime (ZMod.md certificates)
    chi_m1       χ(−1) = −1 (43 ≡ 3 mod 4);  chi_neg  χ(−x) = −χ(x)
    cr_mul       ρ(xy) ≡ ρ(x)ρ(y) (mod 43)
    prim_mul, prim_cj, unit_pm, normalize, unit_one    the interface used by the Hecke files
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc43
import lang.GaussQR
import lang.CubicTwo
import lang.PencilClosed
import lang.W36Mul

namespace Prim43

open FTAChain DetChain LibInt Euc43 ZMod QChar
open LibRing (castZ castN)

theorem prime43 : ∀ d, Dv d (2 * 20 + 1 + 1 + 1) → d = 1 ∨ d = 2 * 20 + 1 + 1 + 1 := by
  intro d ⟨q, hq⟩
  match d with
  | 0 => exact absurd (hq.trans (Nat.mul_zero q)) (fun h => Nat.noConfusion h)
  | e + 1 =>
    match e with
    | 0 => exact Or.inl rfl
    | 1 => exact absurd (ZMod.dv_md_zero 1 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 2 => exact absurd (ZMod.dv_md_zero 2 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 3 => exact absurd (ZMod.dv_md_zero 3 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 4 => exact absurd (ZMod.dv_md_zero 4 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 5 => exact absurd (ZMod.dv_md_zero 5 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 6 => exact absurd (ZMod.dv_md_zero 6 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 7 => exact absurd (ZMod.dv_md_zero 7 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 8 => exact absurd (ZMod.dv_md_zero 8 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 9 => exact absurd (ZMod.dv_md_zero 9 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 10 => exact absurd (ZMod.dv_md_zero 10 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 11 => exact absurd (ZMod.dv_md_zero 11 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 12 => exact absurd (ZMod.dv_md_zero 12 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 13 => exact absurd (ZMod.dv_md_zero 13 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 14 => exact absurd (ZMod.dv_md_zero 14 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 15 => exact absurd (ZMod.dv_md_zero 15 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 16 => exact absurd (ZMod.dv_md_zero 16 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 17 => exact absurd (ZMod.dv_md_zero 17 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 18 => exact absurd (ZMod.dv_md_zero 18 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 19 => exact absurd (ZMod.dv_md_zero 19 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 20 => exact absurd (ZMod.dv_md_zero 20 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 21 => exact absurd (ZMod.dv_md_zero 21 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 22 => exact absurd (ZMod.dv_md_zero 22 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 23 => exact absurd (ZMod.dv_md_zero 23 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 24 => exact absurd (ZMod.dv_md_zero 24 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 25 => exact absurd (ZMod.dv_md_zero 25 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 26 => exact absurd (ZMod.dv_md_zero 26 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 27 => exact absurd (ZMod.dv_md_zero 27 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 28 => exact absurd (ZMod.dv_md_zero 28 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 29 => exact absurd (ZMod.dv_md_zero 29 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 30 => exact absurd (ZMod.dv_md_zero 30 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 31 => exact absurd (ZMod.dv_md_zero 31 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 32 => exact absurd (ZMod.dv_md_zero 32 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 33 => exact absurd (ZMod.dv_md_zero 33 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 34 => exact absurd (ZMod.dv_md_zero 34 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 35 => exact absurd (ZMod.dv_md_zero 35 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 36 => exact absurd (ZMod.dv_md_zero 36 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 37 => exact absurd (ZMod.dv_md_zero 37 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 38 => exact absurd (ZMod.dv_md_zero 38 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 39 => exact absurd (ZMod.dv_md_zero 39 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 40 => exact absurd (ZMod.dv_md_zero 40 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 41 => exact absurd (ZMod.dv_md_zero 41 43 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 42 => exact Or.inr rfl
    | e' + 43 =>
      exfalso
      cases q with
      | zero => exact Nat.noConfusion (hq.trans (Nat.zero_mul _))
      | succ q' =>
        have : 44 ≤ 43 := by
          calc 44 ≤ 44 + (e' + q' * (e' + 43 + 1)) := Nat.le_add_right _ _
            _ = (q' + 1) * (e' + 43 + 1) := by lib_nat
            _ = 43 := hq.symm
        exact Nat.not_succ_le_self 43 this

theorem pw_neg_one : ∀ k, Frobenius.pw (ZMod.ring (2 * 20 + 1 + 1)) ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one) (2 * k + 1) = (ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one
  | 0 => by show (ZMod.ring (2 * 20 + 1 + 1)).mul (ZMod.ring (2 * 20 + 1 + 1)).one ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one) = _; lib_ring
  | k + 1 => by
    show (ZMod.ring (2 * 20 + 1 + 1)).mul ((ZMod.ring (2 * 20 + 1 + 1)).mul (Frobenius.pw (ZMod.ring (2 * 20 + 1 + 1)) ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one) (2 * k + 1)) ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one)) ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one) = _
    rw [pw_neg_one k]; lib_ring

theorem chi_m1 : chi 20 ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one) = -1 :=
  chi_neg_of 20 _ (PencilClosed.negnz 20 _ (PowerSum.one_ne_zero' (2 * 20 + 1))) (by rw [show 20 + 1 = 2 * 10 + 1 from rfl, pw_neg_one 10]; exact ne_pm 20)

theorem chi_neg (x : Zp (2 * 20 + 1 + 1)) : chi 20 ((ZMod.ring (2 * 20 + 1 + 1)).neg x) = -chi 20 x := by
  rw [show (ZMod.ring (2 * 20 + 1 + 1)).neg x = (ZMod.ring (2 * 20 + 1 + 1)).mul ((ZMod.ring (2 * 20 + 1 + 1)).neg (ZMod.ring (2 * 20 + 1 + 1)).one) x by lib_ring, chi_mul 20 prime43, chi_m1]; lib_int

def rho (z : Int × Int) : Int := z.1 + 22 * z.2

def cr (z : Int × Int) : Zp (2 * 20 + 1 + 1) := castZ (ZMod.ring (2 * 20 + 1 + 1)) (rho z)

theorem rho_mul (x y : Int × Int) : rho ((Ring43.ring43 LibInt.intR).mul x y) = rho x * rho y + -(43 * (11 * (x.2 * y.2))) := by
  show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + 22 * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + 22 * x.2) * (y.1 + 22 * y.2) + -(43 * (11 * (x.2 * y.2)))
  lib_int

theorem cz43 (w : Int) : castZ (ZMod.ring (2 * 20 + 1 + 1)) (43 * w) = (ZMod.ring (2 * 20 + 1 + 1)).zero := by
  rw [HasseDavenport.castZ_mul, show castZ (ZMod.ring (2 * 20 + 1 + 1)) 43 = (ZMod.ring (2 * 20 + 1 + 1)).zero from rfl]; lib_ring

theorem cr_mul (x y : Int × Int) : cr ((Ring43.ring43 LibInt.intR).mul x y) = (ZMod.ring (2 * 20 + 1 + 1)).mul (cr x) (cr y) := by
  unfold cr; rw [rho_mul, IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_neg, cz43]; lib_ring

theorem cr_neg (z : Int × Int) : cr (-z.1, -z.2) = (ZMod.ring (2 * 20 + 1 + 1)).neg (cr z) := by
  unfold cr; rw [show rho (-z.1, -z.2) = -rho z by show -z.1 + 22 * -z.2 = -(z.1 + 22 * z.2); lib_int, HasseDavenport.castZ_neg]

def Prim43 (z : Int × Int) : Prop := chi 20 (cr z) = 1

instance (z : Int × Int) : Decidable (Prim43 z) := inferInstanceAs (Decidable (_ = _))

theorem prim_mul (x y : Int × Int) (hx : Prim43 x) (hy : Prim43 y) : Prim43 ((Ring43.ring43 LibInt.intR).mul x y) := by
  show chi 20 (cr _) = 1
  rw [cr_mul, chi_mul 20 prime43, show chi 20 (cr x) = 1 from hx, show chi 20 (cr y) = 1 from hy]; rfl

theorem prim_cj (z : Int × Int) (hz : Prim43 z) : Prim43 (cj z) := by
  have e : cr (cj z) = cr z := by
    unfold cr
    rw [show rho (cj z) = rho z + 43 * -z.2 by show z.1 + z.2 + 22 * -z.2 = z.1 + 22 * z.2 + 43 * -z.2; lib_int, IntCast.castZ_add, cz43]; lib_ring
  show chi 20 (cr (cj z)) = 1; rw [e]; exact hz

theorem unit_pm (u : Int × Int) (h : en u = 1) : u = (Ring43.ring43 LibInt.intR).one ∨ u = (-1, 0) := by
  have h4 := four_nrm u
  have hn : nrm u = 1 := Int.ofNat.inj (by rw [nrm_cast, h]; rfl)
  rw [hn] at h4
  have hB : Int.natAbs u.2 * Int.natAbs u.2 = 0 := by
    cases hb : Int.natAbs u.2 * Int.natAbs u.2 with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 43 * (t + 1) = (Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 43 * t + 39) + 4 by lib_nat] at h4
                exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h4))))
  have b0 : u.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at h4
  have hT : Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) = 2 * 2 := h4.symm
  have hT2 : Int.natAbs (2 * u.1 + u.2) = 2 := by
    match hx : Int.natAbs (2 * u.1 + u.2) with
    | 0 => rw [hx] at hT; exact Nat.noConfusion hT
    | 1 => rw [hx] at hT; exact Nat.noConfusion (Nat.succ.inj hT)
    | 2 => rfl
    | t + 3 => rw [hx, show (t + 3) * (t + 3) = (t * t + 6 * t + 5) + 4 by lib_nat] at hT; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hT))))
  rw [b0] at hT2
  obtain ⟨a, b⟩ := u
  show (a, b) = (1, 0) ∨ (a, b) = (-1, 0)
  have b0' : b = 0 := b0
  rcases EisEuclid.sgn (2 * a + 0) with s | s <;> rw [hT2] at s
  · exact Or.inl (Prod.ext (W36Mul.two_cancel_int a 1 (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')
  · exact Or.inr (Prod.ext (W36Mul.two_cancel_int a (-1) (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')

theorem neg_mul' (z : Int × Int) : (Ring43.ring43 LibInt.intR).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -(0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2) = -z.1; lib_int) (by show -1 * z.2 + 0 * z.1 + 0 * z.2 = -z.2; lib_int)

theorem cr_ne (α : Int × Int) (hd : ∀ k, en α ≠ 43 * k) : cr α ≠ (ZMod.ring (2 * 20 + 1 + 1)).zero := fun h0 => by
  obtain ⟨q, hq⟩ := CubicTwo.cast_zero_gen (2 * 20 + 1 + 1) (rho α) h0
  obtain ⟨a, b⟩ := α
  have hq' : a + 22 * b = 43 * q := hq
  exact hd (q * (a + -(21 * b)) + 11 * (b * b)) (by
    show a * a + a * b + 11 * (b * b) = _
    calc a * a + a * b + 11 * (b * b) = (a + 22 * b) * (a + -(21 * b)) + 43 * (11 * (b * b)) := by lib_int
      _ = 43 * q * (a + -(21 * b)) + 43 * (11 * (b * b)) := by rw [hq']
      _ = _ := by lib_int)

/-- 43 ∤ N(α) ⇒ ±α ∈ H43. -/
theorem normalize (α : Int × Int) (hd : ∀ k, en α ≠ 43 * k) : ∃ u, en u = 1 ∧ Prim43 ((Ring43.ring43 LibInt.intR).mul u α) := by
  rcases GaussQR.chi_pm 20 _ (cr_ne α hd) with h | h
  · exact ⟨(Ring43.ring43 LibInt.intR).one, rfl, by rw [(Ring43.ring43 LibInt.intR).one_mul]; exact h⟩
  · refine ⟨(-1, 0), rfl, ?_⟩
    show chi 20 (cr _) = 1
    rw [neg_mul', cr_neg, chi_neg, h]; rfl

theorem unit_one (u α : Int × Int) (hu : en u = 1) (pα : Prim43 α) (pu : Prim43 ((Ring43.ring43 LibInt.intR).mul u α)) : u = (Ring43.ring43 LibInt.intR).one := by
  rcases unit_pm u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, neg_mul'] at pu
  have h1 : chi 20 (cr α) = 1 := pα
  have h2 : chi 20 (cr (-α.1, -α.2)) = 1 := pu
  rw [cr_neg, chi_neg, h1] at h2
  exact Int.noConfusion h2

end Prim43

#print axioms Prim43.prime43
#print axioms Prim43.pw_neg_one
#print axioms Prim43.chi_m1
#print axioms Prim43.chi_neg
#print axioms Prim43.rho_mul
#print axioms Prim43.cz43
#print axioms Prim43.cr_mul
#print axioms Prim43.cr_neg
#print axioms Prim43.prim_mul
#print axioms Prim43.prim_cj
#print axioms Prim43.unit_pm
#print axioms Prim43.neg_mul'
#print axioms Prim43.cr_ne
#print axioms Prim43.normalize
#print axioms Prim43.unit_one

#print axioms: 15 theorems, no axioms.

78.301 Unique splitting into \(H_{43}\)

By coprime norms prime to \(43\).

\[ \gamma =\alpha \beta \ \text {unique}. \]

lang/Uniq43.lean

/-
  lang/Uniq43 — T48/T50, T48: the unique splitting into H43-elements (Prim43) in ℤ[(1+√−43)/2]:
    prim43_quot, uniq_split43, prim43_split   (norms prime to 43; port of Sq2Unique)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Prim43
import lang.Cop43

namespace Uniq43

open FTAChain DetChain LibInt Euc43 Prim43

/-- y ∈ H43 when x and xy are (43 ∤ N(y)). -/
theorem prim43_quot (x y : Int × Int) (hy : ∀ k, en y ≠ 43 * k) (hx : Prim43 x) (hxy : Prim43 ((Ring43.ring43 LibInt.intR).mul x y)) : Prim43 y := by
  obtain ⟨u, hu, hpu⟩ := normalize y hy
  have h1 : Prim43 ((Ring43.ring43 LibInt.intR).mul u ((Ring43.ring43 LibInt.intR).mul x y)) := by
    rw [show (Ring43.ring43 LibInt.intR).mul u ((Ring43.ring43 LibInt.intR).mul x y) = (Ring43.ring43 LibInt.intR).mul x ((Ring43.ring43 LibInt.intR).mul u y) by lib_ring]
    exact prim_mul _ _ hx hpu
  have hu1 := unit_one u _ hu hxy h1
  rw [hu1, (Ring43.ring43 LibInt.intR).one_mul] at hpu
  exact hpu

/-- **Uniqueness of the primary splitting.** -/
theorem uniq_split43 (α β α' β' : Int × Int) (M N s t : Int) (hα : en α = M) (hα' : en α' = M) (hβ' : en β' = N)
    (hst : s * M + t * N = 1) (hM : M ≠ 0) (pα : Prim43 α) (pα' : Prim43 α') (h : (Ring43.ring43 LibInt.intR).mul α β = (Ring43.ring43 LibInt.intR).mul α' β') :
    α = α' ∧ β = β' := by
  have hco : (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (Cop43.emb s) (cj α)) α) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (Cop43.emb t) (cj β')) β') = (Ring43.ring43 LibInt.intR).one := by
    rw [show (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (Cop43.emb s) (cj α)) α) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (Cop43.emb t) (cj β')) β') = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (Cop43.emb s) ((Ring43.ring43 LibInt.intR).mul α (cj α))) ((Ring43.ring43 LibInt.intR).mul (Cop43.emb t)  -- (+59 chars; full line in the Zenodo source)
      mul_cj, mul_cj, hα, hβ']
    show (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (Cop43.emb s) (Cop43.emb M)) ((Ring43.ring43 LibInt.intR).mul (Cop43.emb t) (Cop43.emb N)) = _
    rw [Cop43.emb_mul, Cop43.emb_mul, ← Cop43.emb_add, hst]; rfl
  match eeuclid α β' α' _ _ hco ⟨β, by rw [show (Ring43.ring43 LibInt.intR).mul β' α' = (Ring43.ring43 LibInt.intR).mul α' β' by lib_ring, ← h]⟩ with
  | ⟨w, hw⟩ =>
    have hgw : en w = 1 := by
      have e : M * en w = M * 1 := by
        calc M * en w = en α * en w := by rw [hα]
          _ = en α' := by rw [← en_mul, ← hw]
          _ = M * 1 := by rw [hα']; lib_int
      have e0 : M * (en w + -1) = 0 := by rw [show M * (en w + -1) = M * en w + -(M * 1) by lib_int, e]; lib_int
      cases int_noZD _ _ e0 with
      | inl h0 => exact absurd h0 hM
      | inr h0 => calc en w = (en w + -1) + 1 := by lib_int
          _ = 1 := by rw [h0]; rfl
    have hw1 : w = (Ring43.ring43 LibInt.intR).one :=
      unit_one w α hgw pα (by rw [show (Ring43.ring43 LibInt.intR).mul w α = α' by rw [hw]; lib_ring]; exact pα')
    have hαα : α = α' := by rw [hw, hw1]; lib_ring
    refine ⟨hαα, ?_⟩
    have h0 : (Ring43.ring43 LibInt.intR).mul α ((Ring43.ring43 LibInt.intR).add β ((Ring43.ring43 LibInt.intR).neg β')) = (Ring43.ring43 LibInt.intR).zero := by
      rw [show (Ring43.ring43 LibInt.intR).mul α ((Ring43.ring43 LibInt.intR).add β ((Ring43.ring43 LibInt.intR).neg β')) = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul α β) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul α β')) by lib_ring, h, ← hαα]; lib_ring
    have := Cop43.cancel_left α _ (by rw [hα]; exact hM) h0
    calc β = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).add β ((Ring43.ring43 LibInt.intR).neg β')) β' := by lib_ring
      _ = β' := by rw [this]; lib_ring

/-- **Existence of the primary splitting.** -/
theorem prim43_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) (hm : ∀ k, Int.ofNat (m + 1) ≠ 43 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 43 * k)
    (pγ : Prim43 γ) : ∃ α β, γ = (Ring43.ring43 LibInt.intR).mul α β ∧ Prim43 α ∧ Prim43 β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match Cop43.ecoprime_split γ m n s t hγ hst with
  | ⟨α0, β0, h0, hα0, hβ0⟩ =>
    match normalize α0 (by rw [hα0]; exact hm) with
    | ⟨u, gu, pu⟩ =>
      have uc : (Ring43.ring43 LibInt.intR).mul u (cj u) = (Ring43.ring43 LibInt.intR).one := by rw [mul_cj, gu]; rfl
      have hβn : en ((Ring43.ring43 LibInt.intR).mul (cj u) β0) = Int.ofNat (n + 1) := by
        rw [en_mul, show en (cj u) = en u from en_cj u, gu, hβ0]; lib_int
      have hsplit : γ = (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul u α0) ((Ring43.ring43 LibInt.intR).mul (cj u) β0) := by
        rw [h0, show (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul u α0) ((Ring43.ring43 LibInt.intR).mul (cj u) β0) = (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul u (cj u)) ((Ring43.ring43 LibInt.intR).mul α0 β0) by lib_ring, uc]; lib_ring
      refine ⟨(Ring43.ring43 LibInt.intR).mul u α0, (Ring43.ring43 LibInt.intR).mul (cj u) β0, hsplit, pu,
        prim43_quot _ _ (by rw [hβn]; exact hn) pu (by rw [← hsplit]; exact pγ),
        by rw [en_mul, gu, hα0]; lib_int, hβn⟩

end Uniq43

#print axioms Uniq43.prim43_quot
#print axioms Uniq43.uniq_split43
#print axioms Uniq43.prim43_split

#print axioms: 3 theorems, no axioms.

78.302 1849a: multiplicativity

Theta coefficients over \(H_{43}\) are multiplicative.

\[ c_w(mn)=c_w(m)c_w(n). \]

lang/HeckeMult43.lean

/-
  lang/HeckeMult43 — T48/T50, T48: **the theta series of ℚ(√−43)'s Hecke character class is multiplicative**: for every
  multiplicative w on ℤ[θ], θ = (1 + √−43)/2,  c_w(n) = Σ_{α ∈ H43, N(α) = n} w(α)  satisfies c_w(mn) = c_w(m)c_w(n) for coprime m, n
  prime to 43.  Enumeration box43 n = HeckeMult.box (44n).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Uniq43
import lang.HeckeMultW

namespace HeckeMult43

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Euc43 Prim43 Uniq43

abbrev box43 (n : Nat) : List (Int × Int) := HeckeMult.box (44 * n)

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 44n  (43a² + (a + 22b)² = 44N, (2a + b)² + 43b² = 4N). -/
theorem abs_le43 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 44 * n ∧ Int.natAbs z.2 ≤ 44 * n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have h4 := four_nrm z
  rw [hn] at h4
  have h8 : 43 * (Int.natAbs z.1 * Int.natAbs z.1) + Int.natAbs (z.1 + 22 * z.2) * Int.natAbs (z.1 + 22 * z.2) = 44 * n := Int.ofNat.inj (by
    show 43 * Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs (z.1 + 22 * z.2) * Int.natAbs (z.1 + 22 * z.2)) = 44 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; unfold en; lib_int)
  have seven : ∀ X : Nat, X ≤ 43 * (X * X) := fun X => Nat.le_trans (HeckeMult.le_sq X) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 42)))
  constructor
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h8))
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans (Nat.le_of_eq h4.symm) (Nat.mul_le_mul_right n (Nat.le_add_right 4 40))))

def PD43 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim43 z

instance (n : Nat) (z : Int × Int) : Decidable (PD43 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum43 {A : Type} : ∀ l : List A, sumK (Ring43.ring43 LibInt.intR) (fun _ => (Ring43.ring43 LibInt.intR).zero) l = (Ring43.ring43 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Ring43.ring43 LibInt.intR).add (Ring43.ring43 LibInt.intR).zero (sumK (Ring43.ring43 LibInt.intR) (fun _ => (Ring43.ring43 LibInt.intR).zero) l) = _; rw [zsum43 l]; exact (Ring43.ring43 LibInt.intR).zero_add _

def I43 (n : Nat) (z : Int × Int) : Int × Int := if PD43 n z then (Ring43.ring43 LibInt.intR).one else (Ring43.ring43 LibInt.intR).zero

theorem I43_pos (n : Nat) (z : Int × Int) (h : PD43 n z) : I43 n z = (Ring43.ring43 LibInt.intR).one := ite_pos' _ _ h
theorem I43_neg (n : Nat) (z : Int × Int) (h : ¬ PD43 n z) : I43 n z = (Ring43.ring43 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw43 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Ring43.ring43 LibInt.intR) (fun z => (Ring43.ring43 LibInt.intR).mul (I43 n z) (w z)) (box43 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult43 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 43 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 43 * k) :
    cw43 w ((m + 1) * (n + 1)) = (Ring43.ring43 LibInt.intR).mul (cw43 w (m + 1)) (cw43 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD43 (m + 1) α → PD43 (n + 1) β → PD43 ((m + 1) * (n + 1)) ((Ring43.ring43 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Ring43.ring43 LibInt.intR).mul (cw43 w (m + 1)) (cw43 w (n + 1))
      = sumK (Ring43.ring43 LibInt.intR) (fun α => sumK (Ring43.ring43 LibInt.intR) (fun β => sumK (Ring43.ring43 LibInt.intR) (fun γ => (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (I43 (m + 1) α) (I43 (n + 1) β)) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β))) (w γ)) (box43 ((m + 1) * (n + 1)))) (box43 (n + 1))) (b  -- (+19 chars; full line in the Zenodo source)
    unfold cw43; rw [sumK_mul]
    refine sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD43 (m + 1) α
    · by_cases hβ : PD43 (n + 1) β
      · rw [I43_pos _ _ hα, I43_pos _ _ hβ]
        have hb := abs_le43 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Ring43.ring43 LibInt.intR) (fun γ => w γ) ((Ring43.ring43 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Ring43.ring43 LibInt.intR) (fun γ => (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).one (Ring43.ring43 LibInt.intR).one) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β))) (w γ)) (box43 ((m + 1) * (n + 1)))
            = sumK (Ring43.ring43 LibInt.intR) (fun γ => (Ring43.ring43 LibInt.intR).mul (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β)) (w γ)) (box43 ((m + 1) * (n + 1))) from sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I43_neg _ _ hβ]
        rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun γ => show (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (I43 (m + 1) α) (Ring43.ring43 LibInt.intR).zero) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β))) (w γ) = (Ring43.ring43 LibInt.intR).zero by lib_ring)]
        rw [zsum43]; lib_ring
    · rw [I43_neg _ _ hα]
      rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun γ => show (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).zero (I43 (n + 1) β)) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β))) (w γ) = (Ring43.ring43 LibInt.intR).zero by lib_ring)]
      rw [zsum43]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => sumK_swap (Ring43.ring43 LibInt.intR) _ _ _)]
  rw [sumK_swap (Ring43.ring43 LibInt.intR)]
  unfold cw43
  refine sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Ring43.ring43 LibInt.intR) (fun α => sumK (Ring43.ring43 LibInt.intR) (fun β => (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (I43 (m + 1) α) (I43 (n + 1) β)) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β))) (w γ)) (box43 (n + 1))) (box43 (m + 1))
      = (Ring43.ring43 LibInt.intR).mul (sumK (Ring43.ring43 LibInt.intR) (fun α => sumK (Ring43.ring43 LibInt.intR) (fun β => (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (I43 (m + 1) α) (I43 (n + 1) β)) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β))) (box43 (n + 1))) (box43 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD43 ((m + 1) * (n + 1)) γ
  · rw [I43_pos _ _ hγ]
    match prim43_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (I43 (m + 1) α) (I43 (n + 1) β)) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β)) = (Ring43.ring43 LibInt.intR).mul (delta (Ring43.ring43 LibInt.intR) α α0) (delta (Ring43.ring43 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD43 (m + 1) α ∧ PD43 (n + 1) β ∧ γ = (Ring43.ring43 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split43 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I43_pos _ _ hα, I43_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Ring43.ring43 LibInt.intR).mul (delta (Ring43.ring43 LibInt.intR) α α0) (delta (Ring43.ring43 LibInt.intR) β β0) = (Ring43.ring43 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD43 (m + 1) α
            · by_cases hβ : PD43 (n + 1) β
              · have hg : ¬ γ = (Ring43.ring43 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I43_neg _ _ hβ]; lib_ring
            · rw [I43_neg _ _ hα]; lib_ring
      rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le43 α0 _ nα0
      have bb := abs_le43 β0 _ nβ0
      rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Ring43.ring43 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => show (Ring43.ring43 LibInt.intR).mul (delta (Ring43.ring43 LibInt.intR) α α0) (Ring43.ring43 LibInt.intR).one = (Ring43.ring43 LibInt.intR).mul (delta (Ring43.ring43 LibInt.intR) α α0) (Ring43.ring43 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Ring43.ring43 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I43_neg _ _ hγ]
    have pt : ∀ α β, (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (I43 (m + 1) α) (I43 (n + 1) β)) (delta (Ring43.ring43 LibInt.intR) γ ((Ring43.ring43 LibInt.intR).mul α β)) = (Ring43.ring43 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD43 (m + 1) α
      · by_cases hβ : PD43 (n + 1) β
        · have hg : ¬ γ = (Ring43.ring43 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I43_neg _ _ hβ]; lib_ring
      · rw [I43_neg _ _ hα]; lib_ring
    rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun α => zsum43 _), zsum43]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult43_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 43 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 43 * k) :
    cw43 (fun z => Frobenius.pw (Ring43.ring43 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Ring43.ring43 LibInt.intR).mul (cw43 (fun z => Frobenius.pw (Ring43.ring43 LibInt.intR) z k) (m + 1)) (cw43 (fun z => Frobenius.pw (Ring43.ring43 LibInt.intR) z k) (n + 1)) :=
  hecke_mult43 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult43

#print axioms HeckeMult43.abs_le43
#print axioms HeckeMult43.zsum43
#print axioms HeckeMult43.I43_pos
#print axioms HeckeMult43.I43_neg
#print axioms HeckeMult43.hecke_mult43
#print axioms HeckeMult43.hecke_mult43_pow
#print axioms HeckeMult43.cw43

#print axioms: 7 theorems, no axioms.

78.303 Factorisation (\(d=43\))

The \(H_{43}\)-elements of norm \(p^k\) are the \(\pi ^j\bar \pi ^l\).

\[ \alpha =\pi ^j\bar \pi ^{\,l}. \]

lang/Prime43.lean

/-
  lang/Prime43 — T48/T50, T48: ℤ[(1+√−43)/2] above a split prime: π of prime norm is prime, N(α) = p^k ⇒ α = uπ^jπ̄^l, and the
  H43-elements of norm p^k are the π^j π̄^l (H43 conjugation-stable).  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult43

namespace Prime43

open FTAChain DetChain LibInt Euc43 Prim43 Uniq43 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Ring43.ring43 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Ring43.ring43 LibInt.intR).mul u (cj u) = (Ring43.ring43 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Ring43.ring43 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists π x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Ring43.ring43 LibInt.intR).mul (cj d) X) ((Ring43.ring43 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (cj d) X) π) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (cj d) Y) x)
          = (Ring43.ring43 LibInt.intR).mul (cj d) ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul X π) ((Ring43.ring43 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring43.ring43 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Ring43.ring43 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring43.ring43 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Ring43.ring43 LibInt.intR).mul d (Ring43.ring43 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Ring43.ring43 LibInt.intR).mul x y) = (Ring43.ring43 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * (y.1 + y.2) + -(-x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * -y.2 + -x.2 * (y.1 + y.2) + -x.2 * -y.2
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show z.1 + z.2 + -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Ring43.ring43 LibInt.intR).mul u ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) (pw (Ring43.ring43 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Ring43.ring43 LibInt.intR).mul α ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).one (Ring43.ring43 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Ring43.ring43 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Ring43.ring43 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Ring43.ring43 LibInt.intR).mul π ((Ring43.ring43 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring43.ring43 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Ring43.ring43 LibInt.intR).mul u ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Ring43.ring43 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Ring43.ring43 LibInt.intR).mul u ((Ring43.ring43 LibInt.intR).mul _ ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim43_one : Prim43 (Ring43.ring43 LibInt.intR).one := rfl

theorem prim43_pow (z : Int × Int) (h : Prim43 z) : ∀ j, Prim43 (pw (Ring43.ring43 LibInt.intR) z j)
  | 0 => prim43_one
  | j + 1 => prim_mul _ _ (prim43_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim43 α) :
    ∃ j l, j + l = k ∧ α = (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) (pw (Ring43.ring43 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim_mul _ _ (prim43_pow π pπ j) (prim43_pow (cj π) (prim_cj π pπ) l)
  have u1 := unit_one u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Prime43

#print axioms Prime43.nrm_mul
#print axioms Prime43.unit_inv
#print axioms Prime43.eprime_div
#print axioms Prime43.cj_mul
#print axioms Prime43.cj_cj
#print axioms Prime43.nrm_cj
#print axioms Prime43.nrm_one_eq
#print axioms Prime43.factor3
#print axioms Prime43.prim43_one
#print axioms Prime43.prim43_pow
#print axioms Prime43.primary_only3

#print axioms: 11 theorems, no axioms.

78.304 1849a: the Hecke recursion

Hecke recursion at split prime powers.

\[ c_W(p^{k+2})=(W(\pi )+W(\bar \pi ))c_W(p^{k+1})-W(\pi )W(\bar \pi )c_W(p^k). \]

lang/HeckePrime43.lean

/-
  lang/HeckePrime43 — T48/T50, T48: **ℚ(√−43)'s Hecke character at split prime powers**: π ∈ H43 of prime norm p, W multiplicative:
  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l and the Hecke recursion.  (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Prime43
import lang.HeckePrimeW

namespace HeckePrime43

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc43 Prim43 Uniq43 Prime43 HeckeMult43

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Ring43.ring43 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) A = (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) B) : A = B := by
  have h0 : (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) ((Ring43.ring43 LibInt.intR).add A ((Ring43.ring43 LibInt.intR).neg B)) = (Ring43.ring43 LibInt.intR).zero := by
    rw [show (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) ((Ring43.ring43 LibInt.intR).add A ((Ring43.ring43 LibInt.intR).neg B)) = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) A) ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := Cop43.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).add A ((Ring43.ring43 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one c π (nrm_one_eq c nc) pπ (by rw [(Ring43.ring43 LibInt.intR).mul_comm, ← hc]; exact prim_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + a * b + 11 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) :
    ∀ d, Dvd' π (pw (Ring43.ring43 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) (j d l' : Nat)
    (h : (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) (pw (Ring43.ring43 LibInt.intR) (cj π) (l' + 1 + d)) = (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π (j + 1 + d)) (pw (Ring43.ring43 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Ring43.ring43 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Ring43.ring43 LibInt.intR) π j (1 + d)] at h
  have h' : (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) (cj π) l') (pw (Ring43.ring43 LibInt.intR) (cj π) (1 + d)))
      = (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) (cj π) l') (pw (Ring43.ring43 LibInt.intR) π (1 + d))) := by
    calc _ = (Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) (pw (Ring43.ring43 LibInt.intR) π (1 + d))) (pw (Ring43.ring43 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Ring43.ring43 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j) (pw (Ring43.ring43 LibInt.intR) (cj π) l) = (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π j') (pw (Ring43.ring43 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) π x.1) (pw (Ring43.ring43 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD43 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim_mul _ _ (prim43_pow π pπ _) (prim43_pow (cj π) (prim_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) (k : Nat) :
    cw43 W ((P + 1 + 1) ^ k) = sumK (Ring43.ring43 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I43 ((P + 1 + 1) ^ k) z = sumK (Ring43.ring43 LibInt.intR) (fun x => delta (Ring43.ring43 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD43 ((P + 1 + 1) ^ k) z
    · rw [I43_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Ring43.ring43 LibInt.intR) k _ (fun x => delta (Ring43.ring43 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Ring43.ring43 LibInt.intR) k j l hjl]
    · rw [I43_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Ring43.ring43 LibInt.intR) k _ (fun _ => (Ring43.ring43 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Ring43.ring43 LibInt.intR) _).symm
  unfold cw43
  rw [sumK_ext (Ring43.ring43 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Ring43.ring43 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le43 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Ring43.ring43 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring43.ring43 LibInt.intR).one = (Ring43.ring43 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Ring43.ring43 LibInt.intR) z j) = pw (Ring43.ring43 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) z j) z) = (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring43.ring43 LibInt.intR).one = (Ring43.ring43 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) (k : Nat) :
    cw43 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Ring43.ring43 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring43.ring43 LibInt.intR).one = (Ring43.ring43 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π) (k : Nat) :
    cw43 W ((P + 1 + 1) ^ (k + 2)) = (Ring43.ring43 LibInt.intR).add
      ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add (W π) (W (cj π))) (cw43 W ((P + 1 + 1) ^ (k + 1))))
      ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (W π) (W (cj π))) (cw43 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime43

#print axioms HeckePrime43.pow_ne0
#print axioms HeckePrime43.nrm_pw
#print axioms HeckePrime43.npow_add
#print axioms HeckePrime43.en_ne
#print axioms HeckePrime43.cancel_pw3
#print axioms HeckePrime43.no_assoc3
#print axioms HeckePrime43.no_div3
#print axioms HeckePrime43.distinct_lt3
#print axioms HeckePrime43.distinct3
#print axioms HeckePrime43.w3_PD
#print axioms HeckePrime43.cw3_pk
#print axioms HeckePrime43.W_pow3
#print axioms HeckePrime43.cw3_pk'
#print axioms HeckePrime43.cw3_rec

#print axioms: 14 theorems, no axioms.

78.305 1849a: inert primes

A non-residue mod \(43\) stays prime; \((-p)^k\) is the only \(H_{43}\)-element of norm \(p^{2k}\).

\[ c_W(p^{2k})=W((-p)^k),\qquad c_W(p^{2k+1})=0. \]

lang/Inert43.lean

/-
  lang/Inert43 — T48/T50: **the ℚ(√−43) Hecke character at inert primes** (χ₄₃(p) = −1), via the Legendre symbol:
    cast_en, en_mod43   N(z) ≡ ρ(z)² (mod 43) (44N = ρ² + 43a²), so χ₄₃(N(z)) ≠ −1
    cw43_zero, inert_div, factor_inert, prim_inert, cw43_even, pow_odd, cw43_odd   (port of Inert19)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime43

namespace Inert43

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc43 Prim43 Uniq43 Prime43 HeckeMult43

variable (P : Nat)

theorem cast_en (z : Int × Int) : LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (en z) = (ZMod.ring (2 * 20 + 1 + 1)).mul (Prim43.cr z) (Prim43.cr z) := by
  have e44 : 44 * en z = Prim43.rho z * Prim43.rho z + 43 * (z.1 * z.1) := by
    show 44 * (z.1 * z.1 + z.1 * z.2 + 11 * (z.2 * z.2)) = (z.1 + 22 * z.2) * (z.1 + 22 * z.2) + 43 * (z.1 * z.1); lib_int
  have h1 : LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (44 * en z) = LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (en z) := by
    rw [HasseDavenport.castZ_mul, show LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) 44 = (ZMod.ring (2 * 20 + 1 + 1)).one from rfl]; lib_ring
  rw [← h1, e44, IntCast.castZ_add, HasseDavenport.castZ_mul, Prim43.cz43]; unfold Prim43.cr; lib_ring

/-- N(z) is never a non-residue mod 43. -/
theorem en_mod43 (z : Int × Int) (n : Nat) (hn : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat n)) = -1) (h : en z = Int.ofNat n) : False := by
  rw [← h, cast_en] at hn
  by_cases h0 : Prim43.cr z = (ZMod.ring (2 * 20 + 1 + 1)).zero
  · rw [h0, show (ZMod.ring (2 * 20 + 1 + 1)).mul (ZMod.ring (2 * 20 + 1 + 1)).zero (ZMod.ring (2 * 20 + 1 + 1)).zero = (ZMod.ring (2 * 20 + 1 + 1)).zero by lib_ring, QChar.chi_zero] at hn; exact Int.noConfusion hn
  · rw [QChar.chi_sq 20 Prim43.prime43 _ h0] at hn; exact Int.noConfusion hn

/-- **c_W(n) = 0 for n a non-residue mod 43.** -/
theorem cw43_zero (W : Int × Int → Int × Int) (n : Nat) (hn : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat n)) = -1) : cw43 W n = (Ring43.ring43 LibInt.intR).zero := by
  unfold cw43
  rw [sumK_ext (Ring43.ring43 LibInt.intR) _ (fun _ => (Ring43.ring43 LibInt.intR).zero) (fun z => by
    rw [I43_neg _ _ (fun h => en_mod43 z n hn h.1)]; lib_ring)]
  exact zsum43 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + Int.ofNat (P + 1 + 1) * 0 + 11 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + 0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (z : Int × Int) (h : nrm z = P + 1 + 1) : False :=
  en_mod43 z (P + 1 + 1) hc (by rw [← nrm_cast, h])

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (x y : Int × Int)
    (h : Dvd' (Pi P) ((Ring43.ring43 LibInt.intR).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists (Pi P) x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Ring43.ring43 LibInt.intR).mul (cj d) X) ((Ring43.ring43 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (cj d) X) (Pi P)) ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (cj d) Y) x)
          = (Ring43.ring43 LibInt.intR).mul (cj d) ((Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul X (Pi P)) ((Ring43.ring43 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring43.ring43 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Ring43.ring43 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring43.ring43 LibInt.intR).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Ring43.ring43 LibInt.intR).mul d (Ring43.ring43 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Ring43.ring43 LibInt.intR).mul u (pw (Ring43.ring43 LibInt.intR) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Ring43.ring43 LibInt.intR).mul α (Ring43.ring43 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Ring43.ring43 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring43.ring43 LibInt.intR).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Ring43.ring43 LibInt.intR).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Ring43.ring43 LibInt.intR).mul u ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : Prim43 (nPi P) := by
  show QChar.chi 20 (Prim43.cr (nPi P)) = 1
  have e : Prim43.cr (nPi P) = (ZMod.ring (2 * 20 + 1 + 1)).neg (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) := by
    unfold Prim43.cr; rw [show Prim43.rho (nPi P) = -Int.ofNat (P + 1 + 1) by show -Int.ofNat (P + 1 + 1) + 22 * 0 = _; lib_int, HasseDavenport.castZ_neg]
  rw [e, Prim43.chi_neg, hc]; rfl

theorem pw_neg (k : Nat) : pw (Ring43.ring43 LibInt.intR) (Pi P) k =
    (Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) ((Ring43.ring43 LibInt.intR).neg (Ring43.ring43 LibInt.intR).one) k) (pw (Ring43.ring43 LibInt.intR) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -(-0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1) + -0 * 0; lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Ring43.ring43 LibInt.intR) ((Ring43.ring43 LibInt.intR).neg (Ring43.ring43 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Ring43.ring43 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) (α : Int × Int)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim43 α) : α = pw (Ring43.ring43 LibInt.intR) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Ring43.ring43 LibInt.intR).mul u (pw (Ring43.ring43 LibInt.intR) ((Ring43.ring43 LibInt.intR).neg (Ring43.ring43 LibInt.intR).one) k) = v
  have he' : α = (Ring43.ring43 LibInt.intR).mul v (pw (Ring43.ring43 LibInt.intR) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one v _ (nrm_one_eq v hv1) (prim43_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -Int.ofNat (P + 1 + 1) * 0 + 11 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Ring43.ring43 LibInt.intR) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime43.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw43_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) :
    cw43 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Ring43.ring43 LibInt.intR) (nPi P) k) := by
  have hPD : PD43 ((P + 1 + 1) ^ (2 * k)) (pw (Ring43.ring43 LibInt.intR) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim43_pow _ (prim_nPi P hc) k⟩
  unfold cw43
  rw [sumK_ext (Ring43.ring43 LibInt.intR) _ (fun z => (Ring43.ring43 LibInt.intR).mul (delta (Ring43.ring43 LibInt.intR) z (pw (Ring43.ring43 LibInt.intR) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD43 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I43_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I43_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le43 _ _ hPD.1
  exact HeckeMult.pick_box (Ring43.ring43 LibInt.intR) W _ _ hb.1 hb.2

theorem pow_odd (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : ∀ k, QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)))) = -1
  | 0 => by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hc
  | k + 1 => by
    rw [show (P + 1 + 1) ^ (2 * (k + 1) + 1) = (P + 1 + 1) ^ (2 * k + 1) * ((P + 1 + 1) * (P + 1 + 1)) by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; show (P + 1 + 1) ^ (2 * k + 1) * (P + 1 + 1) * (P + 1 + 1) = _; lib_nat]
    show QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * (Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)))) = -1
    rw [HasseDavenport.castZ_mul, HasseDavenport.castZ_mul, QChar.chi_mul 20 Prim43.prime43, QChar.chi_mul 20 Prim43.prime43, pow_odd hc k, hc]; rfl

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw43_odd (W : Int × Int → Int × Int) (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) : cw43 W ((P + 1 + 1) ^ (2 * k + 1)) = (Ring43.ring43 LibInt.intR).zero := by
  exact cw43_zero W _ (pow_odd P hc k)

end Inert43

#print axioms Inert43.cast_en
#print axioms Inert43.en_mod43
#print axioms Inert43.cw43_zero
#print axioms Inert43.en_Pi
#print axioms Inert43.nrm_Pi
#print axioms Inert43.cj_Pi
#print axioms Inert43.not_p
#print axioms Inert43.dvd_conv
#print axioms Inert43.split_sq
#print axioms Inert43.inert_div
#print axioms Inert43.nat_pow2
#print axioms Inert43.factor_inert
#print axioms Inert43.prim_nPi
#print axioms Inert43.pw_neg
#print axioms Inert43.nrm_neg_one_pw
#print axioms Inert43.prim_inert
#print axioms Inert43.nrm_nPi
#print axioms Inert43.nrm_nPi_pw
#print axioms Inert43.cw43_even
#print axioms Inert43.pow_odd
#print axioms Inert43.cw43_odd

#print axioms: 21 theorems, no axioms.

78.306 The ring \(\mathbb Z[(1+\sqrt {-67})/2]\)

Pairs \(a+b\theta \), \(\theta ^2=\theta -17\).

\[ (a+b\theta )(c+d\theta )=(ac-17bd)+(ad+bc+bd)\theta . \]

lang/Ring67.lean

/-
  lang/Ring67 — T48/T50: the ring K[θ] = K[x]/(x² − x + 17) (for K = ℤ: the integers of ℚ(√−67)).
  (a + bθ)(c + dθ) = (ac − 17bd) + (ad + bc + bd)θ.  (Show-expansions generated.)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lib.RingTac

namespace Ring67

variable {R : Type} (K : DetChain.CRing R)

def rmul (x y : R × R) : R × R :=
  ((K.add (K.mul x.1 y.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul x.2 y.2) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (  -- (+100 chars; full line in the Zenodo source)

theorem r_assoc (a b e : R × R) : rmul K (rmul K a b) e = rmul K a (rmul K b e) := by
  apply Prod.ext
  · show (K.add (K.mul (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)  -- (+3370 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a  -- (+834 chars; full line in the Zenodo source)
    lib_ring

theorem r_comm (a b : R × R) : rmul K a b = rmul K b a := by
  apply Prod.ext
  · show (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b  -- (+474 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) = (K.add (K.add (K.mul b.1 a.2) (K.mul b.2 a.1)) (K.mul b.2 a.2))
    lib_ring

theorem r_one (a : R × R) : rmul K (K.one, K.zero) a = a := by
  apply Prod.ext
  · show (K.add (K.mul K.one a.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul K.zero a.2) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K  -- (+99 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul K.one a.2) (K.mul K.zero a.1)) (K.mul K.zero a.2)) = a.2
    lib_ring

theorem r_distrib (a b e : R × R) :
    rmul K a (K.add b.1 e.1, K.add b.2 e.2) = (K.add (rmul K a b).1 (rmul K a e).1, K.add (rmul K a b).2 (rmul K a e).2) := by
  apply Prod.ext
  · show (K.add (K.mul a.1 (K.add b.1 e.1)) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.2 (K.add b.2 e.2)) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (K.add b.2 e.2))) (K.mul  -- (+1130 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul a.1 (K.add b.2 e.2)) (K.mul a.2 (K.add b.1 e.1))) (K.mul a.2 (K.add b.2 e.2))) = (K.add (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) (K.add (K.add (K.mul a.1 e.2) (K.mul a.2 e.1)) (K.mul a.2 e.2)))
    lib_ring

def ring67 : DetChain.CRing (R × R) where
  add := fun x y => (K.add x.1 y.1, K.add x.2 y.2)
  mul := rmul K
  neg := fun x => (K.neg x.1, K.neg x.2)
  zero := (K.zero, K.zero)
  one := (K.one, K.zero)
  add_assoc := fun a b c => by
    apply Prod.ext
    · exact K.add_assoc a.1 b.1 c.1
    · exact K.add_assoc a.2 b.2 c.2
  add_comm := fun a b => by
    apply Prod.ext
    · exact K.add_comm a.1 b.1
    · exact K.add_comm a.2 b.2
  zero_add := fun a => by
    apply Prod.ext
    · exact K.zero_add a.1
    · exact K.zero_add a.2
  add_neg := fun a => by
    apply Prod.ext
    · exact K.add_neg a.1
    · exact K.add_neg a.2
  mul_assoc := r_assoc K
  mul_comm := r_comm K
  one_mul := r_one K
  distrib := r_distrib K

end Ring67

#print axioms Ring67.r_assoc
#print axioms Ring67.r_comm
#print axioms Ring67.r_one
#print axioms Ring67.r_distrib
#print axioms Ring67.ring67

#print axioms: 5 theorems, no axioms.

78.307 Norm facts (\(d=67\))

\(4N=(2a+b)^2+67b^2\); Euclid’s lemma.

\[ N(a+b\theta )=a^2+ab+17b^2. \]

lang/Euc67.lean

/-
  lang/Euc67 — T48/T50: ℤ[θ], θ = (1 + √−67)/2, N(a + bθ) = a² + ab + 17b², 4N = (2a + b)² + 67b²: norm facts and Euclid's lemma.
  (The Dedekind–Hasse step is in DH67.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Ring67
import lang.GaussEuclid
import lang.W36Mul

namespace Euc67

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + z.1 * z.2 + 17 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Ring67.ring67 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + 17 * (B * B) := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_trans (Nat.le_add_left _ (A * A)) (Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 16))) _))
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + 17 * (Int.ofNat B * Int.ofNat B) := congrArg Int.ofNat hc
  have opp : en z = Int.ofNat A * Int.ofNat A + 17 * (Int.ofNat B * Int.ofNat B) + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨c, by rw [h, ← e]; lib_int⟩
  have same : en z = Int.ofNat A * Int.ofNat A + 17 * (Int.ofNat B * Int.ofNat B) + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨A * A + 17 * (B * B) + A * B, h⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem four_nrm (z : Int × Int) : 4 * nrm z = Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 67 * (Int.natAbs z.2 * Int.natAbs z.2) := by
  apply Int.ofNat.inj
  show 4 * Int.ofNat (nrm z) = Int.ofNat (Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2)) + 67 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2)
  rw [nrm_cast, GaussEuclid.nas, GaussEuclid.nas]; unfold en; lib_int

theorem mul_cj (z : Int × Int) : (Ring67.ring67 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + z.2) + -(z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 17 * (z.2 * z.2)
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + z.2) + z.2 * -z.2 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Ring67.ring67 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2)) * (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x  -- (+305 chars; full line in the Zenodo source)
      + 17 * ((x.1 * y.2 + x.2 * y.1 + x.2 * y.2) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)) =
    (x.1 * x.1 + x.1 * x.2 + 17 * (x.2 * x.2)) * (y.1 * y.1 + y.1 * y.2 + 17 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + z.2) * (z.1 + z.2) + (z.1 + z.2) * -z.2 + 17 * (-z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 17 * (z.2 * z.2)
  lib_int

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Ring67.ring67 LibInt.intR).zero := by
  have h4 := four_nrm z
  rw [h] at h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by
    have : 67 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
    cases nat_mul_zero _ _ this with
    | inl e => exact Nat.noConfusion e
    | inr e => exact e
  have hT : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have t0 : 2 * z.1 + z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hT with | inl e => exact e | inr e => exact e)
  have a0 : z.1 = 0 := W36Mul.two_cancel_int z.1 0 (by rw [show 2 * z.1 = 2 * z.1 + z.2 by rw [b0]; lib_int, t0]; rfl)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Ring67.ring67 LibInt.intR).zero) :
    ∃ d x y, d = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x α) ((Ring67.ring67 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Ring67.ring67 LibInt.intR).one, (Ring67.ring67 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Ring67.ring67 LibInt.intR).one, by lib_ring⟩, ⟨(Ring67.ring67 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x α) ((Ring67.ring67 LibInt.intR).mul y β) = (Ring67.ring67 LibInt.intR).one) (h : Dvd' α ((Ring67.ring67 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x γ) ((Ring67.ring67 LibInt.intR).mul y c), by
      calc γ = (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x α) ((Ring67.ring67 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul x α) γ) ((Ring67.ring67 LibInt.intR).mul y ((Ring67.ring67 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Ring67.ring67 LibInt.intR).mul α ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x γ) ((Ring67.ring67 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

theorem nrm_lt (z : Int × Int) (N : Nat) (h : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 67 * (Int.natAbs z.2 * Int.natAbs z.2) < 4 * (N * N)) :
    nrm z < N * N := by
  cases Nat.lt_or_ge (nrm z) (N * N) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    rw [← four_nrm] at h
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_left 4 h'))

theorem sq_le (T N : Nat) (h : T ≤ N) : T * T ≤ N * N := Nat.mul_le_mul h h


end Euc67

#print axioms Euc67.sgn
#print axioms Euc67.le_dest
#print axioms Euc67.ab_le
#print axioms Euc67.en_rep
#print axioms Euc67.nrm_cast
#print axioms Euc67.four_nrm
#print axioms Euc67.mul_cj
#print axioms Euc67.en_mul
#print axioms Euc67.en_cj
#print axioms Euc67.nrm_zero
#print axioms Euc67.egcd_base
#print axioms Euc67.nrm_lt
#print axioms Euc67.sq_le
#print axioms Euc67.eeuclid

#print axioms: 14 theorems, no axioms.

78.308 A Dedekind–Hasse step for \(d=67\)

Multipliers \(1,\dots ,4\) by linear bounds; rescues at \(N/2\), \(N/3\), \(N/4\).

\[ \exists s,q:\ N(s\alpha -q\beta )<N(\beta ),\ s\alpha =q\beta \Rightarrow s=1. \]

lang/DH67.lean

/-
  lang/DH67 — T48/T50: **the Dedekind–Hasse step in ℤ[(1 + √−67)/2]**: from a two-step remainder r there are s ∈ {1, 2, 3, 4}
  (by the size of |r₂| relative to N, via linear bounds 1000B > 211N etc. from 3N² ≤ 67B²) and rescues at the half, third and
  quarter points (resc2, resc3, resc4; table computed numerically) with N(sr − eN) < N², = 0 only for s = 1.
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc67
import lang.Euc19
import lang.EisPrim
import lang.Sq2Prim8
import lang.DH43

namespace Euc67

open FTAChain DetChain LibInt

theorem nrm_pair (n : Nat) : nrm (Int.ofNat n, 0) = n * n := Int.ofNat.inj (by rw [nrm_cast]; show Int.ofNat n * Int.ofNat n + Int.ofNat n * 0 + 17 * (0 * 0) = Int.ofNat n * Int.ofNat n; lib_int)

/-- b·Y² ≤ a·X², a q² < b p², Y > 0 ⇒ qY < pX. -/
theorem lin_lb (a b p q X Y : Nat) (h : b * (Y * Y) ≤ a * (X * X)) (hc : a * (q * q) < b * (p * p)) (hY : 0 < Y) : q * Y < p * X := by
  cases Nat.lt_or_ge (q * Y) (p * X) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    have s1 : (p * X) * (p * X) ≤ (q * Y) * (q * Y) := Nat.mul_le_mul h' h'
    have s2 : (p * p) * (b * (Y * Y)) ≤ (p * p) * (a * (X * X)) := Nat.mul_le_mul_left _ h
    have s3 : a * ((p * X) * (p * X)) ≤ a * ((q * Y) * (q * Y)) := Nat.mul_le_mul_left a s1
    have s4 : (a * (q * q)) * (Y * Y) < (b * (p * p)) * (Y * Y) := Nat.mul_lt_mul_of_pos_right hc (Nat.mul_pos hY hY)
    have : (b * (p * p)) * (Y * Y) < (b * (p * p)) * (Y * Y) :=
      Nat.lt_of_le_of_lt (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s2 (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s3 (Nat.le_of_eq (by lib_nat)))))) s4
    exact Nat.lt_irrefl _ this

/-- pW < qY, a q² < b p² ⇒ a W² < b Y². -/
theorem sq_ub (a b p q W Y : Nat) (h : p * W < q * Y) (hc : a * (q * q) < b * (p * p)) : a * (W * W) < b * (Y * Y) := by
  cases Nat.lt_or_ge (a * (W * W)) (b * (Y * Y)) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    have s1 : (p * W) * (p * W) ≤ (q * Y) * (q * Y) := Nat.mul_le_mul (Nat.le_of_lt h) (Nat.le_of_lt h)
    have hY : 0 < Y := by
      cases Y with
      | zero => exact absurd (Nat.lt_of_lt_of_le h (Nat.le_of_eq (Nat.mul_zero q))) (Nat.not_lt_zero _)
      | succ y => exact Nat.zero_lt_succ y
    have s2 : (p * p) * (b * (Y * Y)) ≤ (p * p) * (a * (W * W)) := Nat.mul_le_mul_left _ h'
    have s3 : a * ((p * W) * (p * W)) ≤ a * ((q * Y) * (q * Y)) := Nat.mul_le_mul_left a s1
    have s4 : (a * (q * q)) * (Y * Y) < (b * (p * p)) * (Y * Y) := Nat.mul_lt_mul_of_pos_right hc (Nat.mul_pos hY hY)
    have : (b * (p * p)) * (Y * Y) < (b * (p * p)) * (Y * Y) :=
      Nat.lt_of_le_of_lt (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s2 (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s3 (Nat.le_of_eq (by lib_nat)))))) s4
    exact Nat.lt_irrefl _ this

theorem resc2 (B : Nat) (d1 d2 N : Int) (hNB : N = 2 * Int.ofNat B) (hd2 : d2 = 1 ∨ d2 = -1) :
    ∃ s e : Int × Int, ∃ c : Nat, c < 2 ∧ 0 < c ∧ (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (d1 * Int.ofNat B, d2 * Int.ofNat B)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (N, 0))) = (Int.ofNat (c * B), 0) := by
  rcases GaussInt.int_even_odd d1 with ⟨q, hq⟩ | ⟨q, hq⟩ <;> rcases hd2 with e2 | e2
  · refine ⟨(-1, 1), (q * -1 + -9, q * 1 + 0), 1, (show 1 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 0)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+414 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + -9) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-1, 1), (q * -1 + 8, q * 1 + 0), 1, (show 1 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 0)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+413 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + 8) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), (q * 0 + 8, q * -1 + -1), 1, (show 1 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 0)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)   -- (+462 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * 0 + 8) * 0 + (q * -1 + -1) * N + (q * -1 + -1) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), (q * 0 + -9, q * -1 + 0), 1, (show 1 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 0)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)   -- (+446 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * 0 + -9) * 0 + (q * -1 + 0) * N + (q * -1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int

theorem resc3 (B : Nat) (d1 d2 N : Int) (hNB : N = 3 * Int.ofNat B) (hd2 : d2 = 1 ∨ d2 = -1) :
    ∃ s e : Int × Int, ∃ c : Nat, c < 3 ∧ 0 < c ∧ (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (d1 * Int.ofNat B, d2 * Int.ofNat B)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (N, 0))) = (Int.ofNat (c * B), 0) := by
  obtain ⟨q, hq | hq | hq⟩ := EisPrim.res3 d1 <;> rcases hd2 with e2 | e2
  · refine ⟨(-1, 1), (q * -1 + -6, q * 1 + 0), 1, (show 1 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 1)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+414 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + -6) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-1, 1), (q * -1 + 5, q * 1 + 0), 2, (show 2 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_left 3 0)), (show 0 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 1)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+413 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 2 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + 5) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-2, 1), (q * -2 + -7, q * 1 + 0), 2, (show 2 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_left 3 0)), (show 0 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 1)), ?_⟩
    apply Prod.ext
    · show -2 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+414 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 2 * Int.ofNat B; lib_int
    · show -2 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -2 + -7) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), (q * 0 + -6, q * -1 + 0), 1, (show 1 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 1)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)   -- (+446 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * 0 + -6) * 0 + (q * -1 + 0) * N + (q * -1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), ((q + -1) * 0 + 5, (q + -1) * -1 + -1), 2, (show 2 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 2) (Nat.le_add_left 3 0)), (show 0 < 2 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 1)), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)   -- (+588 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 2 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -(((q + -1) * 0 + 5) * 0 + ((q + -1) * -1 + -1) * N + ((q + -1) * -1 + -1) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-2, 1), ((q + -1) * -2 + 4, (q + -1) * 1 + 1), 1, (show 1 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 1)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -2 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+539 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -2 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -(((q + -1) * -2 + 4) * 0 + ((q + -1) * 1 + 1) * N + ((q + -1) * 1 + 1) * 0) = 0
      rw [hq, e2, hNB]; lib_int

theorem resc4 (B : Nat) (d1 d2 N : Int) (hNB : N = 4 * Int.ofNat B) (hd2 : d2 = 1 ∨ d2 = -1) :
    ∃ s e : Int × Int, ∃ c : Nat, c < 4 ∧ 0 < c ∧ (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (d1 * Int.ofNat B, d2 * Int.ofNat B)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (N, 0))) = (Int.ofNat (c * B), 0) := by
  obtain ⟨q, hq | hq | hq | hq⟩ := Sq2Prim8.mod4 d1 <;> rcases hd2 with e2 | e2
  · refine ⟨(-1, 1), (q * -1 + -5, q * 1 + 0), 3, (show 3 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_left 4 0)), (show 0 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 2)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+414 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 3 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + -5) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-1, 1), (q * -1 + 4, q * 1 + 0), 1, (show 1 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 2)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+413 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -1 + 4) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-2, 1), (q * -2 + -5, q * 1 + 0), 1, (show 1 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 2)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -2 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+414 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -2 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -2 + -5) * 0 + (q * 1 + 0) * N + (q * 1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), (q * 0 + -5, q * -1 + 0), 3, (show 3 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_left 4 0)), (show 0 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 2)), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)   -- (+446 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 3 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * 0 + -5) * 0 + (q * -1 + 0) * N + (q * -1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-1, -1), (q * -1 + 3, q * -1 + -1), 3, (show 3 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_left 4 0)), (show 0 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 2)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)  -- (+464 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 3 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * -1 + 3) * 0 + (q * -1 + -1) * N + (q * -1 + -1) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-1, -1), (q * -1 + -5, q * -1 + 0), 1, (show 1 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 2)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show -1 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)  -- (+448 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show -1 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * -1 + -5) * 0 + (q * -1 + 0) * N + (q * -1 + 0) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(0, -1), (q * 0 + 4, q * -1 + -1), 1, (show 1 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 1) (Nat.le_add_left 2 2)), (show 0 < 1 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 0)), ?_⟩
    apply Prod.ext
    · show 0 * (d1 * Int.ofNat B) + -(-1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -1 * (d2 * Int.ofNat B)   -- (+462 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 1 * Int.ofNat B; lib_int
    · show 0 * (d2 * Int.ofNat B) + -1 * (d1 * Int.ofNat B) + -1 * (d2 * Int.ofNat B) + -((q * 0 + 4) * 0 + (q * -1 + -1) * N + (q * -1 + -1) * 0) = 0
      rw [hq, e2, hNB]; lib_int
  · refine ⟨(-2, 1), (q * -2 + 2, q * 1 + 1), 3, (show 3 < 4 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 3) (Nat.le_add_left 4 0)), (show 0 < 3 from Nat.lt_of_lt_of_le (Nat.lt_succ_self 0) (Nat.le_add_left 1 2)), ?_⟩
    apply Prod.ext
    · show -2 * (d1 * Int.ofNat B) + -(1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + 1 * (d2 * I  -- (+413 chars; full line in the Zenodo source)
      rw [hq, e2, hNB]; show _ = 3 * Int.ofNat B; lib_int
    · show -2 * (d2 * Int.ofNat B) + 1 * (d1 * Int.ofNat B) + 1 * (d2 * Int.ofNat B) + -((q * -2 + 2) * 0 + (q * 1 + 1) * N + (q * 1 + 1) * 0) = 0
      rw [hq, e2, hNB]; lib_int

theorem kcancel (k : Nat) (a c : Int) (h : Int.ofNat (k + 1) * a = Int.ofNat (k + 1) * c) : a = c := by
  have h0 : Int.ofNat (k + 1) * (a + -c) = 0 := by rw [show Int.ofNat (k + 1) * (a + -c) = Int.ofNat (k + 1) * a + -(Int.ofNat (k + 1) * c) by lib_int, h]; lib_int
  cases int_noZD _ _ h0 with
  | inl e => exact absurd (Int.ofNat.inj e) (fun e => Nat.noConfusion e)
  | inr e => calc a = (a + -c) + c := by lib_int
      _ = c := by rw [e]; show (0 : Int) + c = c; lib_int

/-- One branch: multiplier k + 1 with |(k+1) r₂ − d₂N| = w, 67w² < 3N². -/
theorem branch (N0 : Nat) (r1 r2 d2 : Int) (B k : Nat) (hr2 : r2 = d2 * Int.ofNat B) (hd2 : d2 = 1 ∨ d2 = -1)
    (w : Nat) (hw : Int.natAbs (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = w) (hb : 67 * (w * w) < 3 * ((N0 + 1) * (N0 + 1)))
    (resc : ∀ d1 : Int, Int.ofNat (N0 + 1) = Int.ofNat (k + 1) * Int.ofNat B → ∃ s e : Int × Int, ∃ c : Nat, c < k + 1 ∧ 0 < c ∧
      (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (d1 * Int.ofNat B, d2 * Int.ofNat B)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Int.ofNat (c * B), 0)) :
    ∃ s e : Int × Int, nrm ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (r1, r2)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (r1, r2)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring67.ring67 LibInt.intR).zero → s = (Ring67.ring67 LibInt.intR).one) := by
  have hdd : d2 * d2 = 1 := by rcases hd2 with e | e <;> rw [e] <;> rfl
  have e2N : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
  obtain ⟨d1, t', hrd, hb1⟩ := GaussEuclid.rnd (Int.ofNat (k + 1) * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1))) (2 * N0 + 1)
  have hrd' : 2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = t' := by
    calc 2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = Int.ofNat (k + 1) * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1)) + -(d1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
      _ = t' := by rw [hrd, e2N]; lib_int
  have hT' : Int.natAbs t' ≤ N0 + 1 := Nat.le_of_mul_le_mul_left (Nat.le_trans hb1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
  have hexpr : (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Int.ofNat (k + 1), 0) (r1, r2)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul (d1, d2) (Int.ofNat (N0 + 1), 0))) = (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)), Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) := by
    apply Prod.ext
    · show Int.ofNat (k + 1) * r1 + -(0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2) + -(d1 * Int.ofNat (N0 + 1) + -(d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0 + d2 * 0)) = Int.ofNat (k + 1) * r1   -- (+37 chars; full line in the Zenodo source)
    · show Int.ofNat (k + 1) * r2 + 0 * r1 + 0 * r2 + -(d1 * 0 + d2 * Int.ofNat (N0 + 1) + d2 * 0) = Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)); lib_int
  by_cases hz : (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)), Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = (Ring67.ring67 LibInt.intR).zero
  · have z1 : Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.fst hz
    have z2 : Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.snd hz
    have hNk : Int.ofNat (N0 + 1) = Int.ofNat (k + 1) * Int.ofNat B := by
      calc Int.ofNat (N0 + 1) = d2 * d2 * Int.ofNat (N0 + 1) := by rw [hdd]; lib_int
        _ = d2 * (Int.ofNat (k + 1) * r2) := by rw [show Int.ofNat (k + 1) * r2 = d2 * Int.ofNat (N0 + 1) by rw [show Int.ofNat (k + 1) * r2 = Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)) + d2 * Int.ofNat (N0 + 1) by lib_int, z2]; lib_int]; lib_int
        _ = Int.ofNat (k + 1) * Int.ofNat B := by rw [hr2, show d2 * (Int.ofNat (k + 1) * (d2 * Int.ofNat B)) = Int.ofNat (k + 1) * (d2 * d2) * Int.ofNat B by lib_int, hdd]; lib_int
    have hr1 : r1 = d1 * Int.ofNat B := kcancel k _ _ (by
      calc Int.ofNat (k + 1) * r1 = Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)) + d1 * Int.ofNat (N0 + 1) := by lib_int
        _ = Int.ofNat (k + 1) * (d1 * Int.ofNat B) := by rw [z1, hNk]; lib_int)
    have hNn : N0 + 1 = (k + 1) * B := Int.ofNat.inj hNk
    have hB0 : B ≠ 0 := fun e => by rw [e] at hNn; exact Nat.noConfusion (hNn.trans (Nat.mul_zero _))
    obtain ⟨sv, ev, c, hck, hc0, he⟩ := resc d1 hNk
    rw [← hr1, ← hr2] at he
    refine ⟨sv, ev, ?_, fun h0 => ?_⟩
    · rw [he, nrm_pair, hNn]
      have : c * B < (k + 1) * B := Nat.mul_lt_mul_of_pos_right hck (Nat.pos_of_ne_zero hB0)
      exact Nat.mul_lt_mul_of_le_of_lt (Nat.le_of_lt this) this (Nat.lt_of_le_of_lt (Nat.zero_le _) this) |> fun h => Nat.lt_of_le_of_lt (Nat.mul_le_mul (Nat.le_of_lt this) (Nat.le_refl _)) (Nat.mul_lt_mul_of_pos_left this (Nat.lt_of_le_of_lt (Nat.zero_le _) this))
    · exfalso
      have : Int.ofNat (c * B) = 0 := congrArg Prod.fst (he.symm.trans h0)
      have h1 : c * B = 0 := Int.ofNat.inj this
      cases nat_mul_zero _ _ h1 with
      | inl e => rw [e] at hc0; exact Nat.lt_irrefl 0 hc0
      | inr e => exact hB0 e
  · refine ⟨(Int.ofNat (k + 1), 0), (d1, d2), ?_, fun h0 => absurd (hexpr.symm.trans h0) hz⟩
    rw [hexpr]
    apply nrm_lt
    show Int.natAbs (2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)))) * Int.natAbs (2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))))
      + 67 * (Int.natAbs (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) * Int.natAbs (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)))) < _
    rw [hrd', hw]
    calc Int.natAbs t' * Int.natAbs t' + 67 * (w * w) < (N0 + 1) * (N0 + 1) + 3 * ((N0 + 1) * (N0 + 1)) := Nat.add_lt_add_of_le_of_lt (Nat.mul_le_mul hT' hT') hb
      _ = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat

theorem nlt (a k : Nat) : a < a + k + 1 := Nat.succ_le_succ (Nat.le_add_right a k)

theorem lt_cancel (a b c : Nat) (h : a + c < b + c) : a < b := by
  cases Nat.lt_or_ge a b with
  | inl h' => exact h'
  | inr h' => exact absurd h (Nat.not_lt_of_ge (Nat.add_le_add_right h' c))

theorem absw (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (X : Int) (w : Nat) (hX : X = d2 * Int.ofNat w ∨ X = -(d2 * Int.ofNat w)) : Int.natAbs X = w := by
  rcases hd2 with e | e <;> rcases hX with h | h <;> rw [h, e]
  · show Int.natAbs (1 * Int.ofNat w) = w; rw [show 1 * Int.ofNat w = Int.ofNat w by lib_int]; rfl
  · show Int.natAbs (-(1 * Int.ofNat w)) = w; rw [show -(1 * Int.ofNat w) = -Int.ofNat w by lib_int]; exact Int.natAbs_neg _
  · show Int.natAbs (-1 * Int.ofNat w) = w; rw [show -1 * Int.ofNat w = -Int.ofNat w by lib_int]; exact Int.natAbs_neg _
  · show Int.natAbs (-(-1 * Int.ofNat w)) = w; rw [show -(-1 * Int.ofNat w) = Int.ofNat w by lib_int]; rfl

set_option maxHeartbeats 0 in
/-- **The Dedekind–Hasse step on a remainder (d = 67).** -/
theorem dh67_r (N0 : Nat) (r1 r2 : Int) (h2 : 2 * Int.natAbs r2 ≤ N0 + 1) (ht : Int.natAbs (2 * r1 + r2) ≤ N0 + 1) :
    ∃ s e : Int × Int, nrm ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (r1, r2)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (r1, r2)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring67.ring67 LibInt.intR).zero → s = (Ring67.ring67 LibInt.intR).one) := by
  have h4 := four_nrm (r1, r2)
  generalize hT : Int.natAbs (2 * r1 + r2) = T at h4 ht
  generalize hB : Int.natAbs r2 = B at h4 h2
  have h4' : 4 * nrm (r1, r2) = T * T + 67 * (B * B) := h4
  cases Nat.lt_or_ge (T * T + 67 * (B * B)) (4 * ((N0 + 1) * (N0 + 1))) with
  | inl hl =>
    refine ⟨(Ring67.ring67 LibInt.intR).one, (Ring67.ring67 LibInt.intR).zero, ?_, fun _ => rfl⟩
    have e : (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).one (r1, r2)) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).zero (Int.ofNat (N0 + 1), 0))) = (r1, r2) := by lib_ring
    rw [e]
    exact nrm_lt (r1, r2) (N0 + 1) (by rw [hT, hB]; exact hl)
  | inr hg =>
    have h3 : 3 * ((N0 + 1) * (N0 + 1)) ≤ 67 * (B * B) := GaussEuclid.le_cancel _ _ ((N0 + 1) * (N0 + 1)) (by
      calc 3 * ((N0 + 1) * (N0 + 1)) + (N0 + 1) * (N0 + 1) = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat
        _ ≤ T * T + 67 * (B * B) := hg
        _ ≤ (N0 + 1) * (N0 + 1) + 67 * (B * B) := Nat.add_le_add_right (Nat.mul_le_mul ht ht) _
        _ = 67 * (B * B) + (N0 + 1) * (N0 + 1) := by lib_nat)
    have lb1 : 4 * (N0 + 1) < 19 * B := lin_lb 67 3 19 4 B (N0 + 1) h3 (nlt 1072 10) (Nat.zero_lt_succ N0)
    obtain ⟨u, hu⟩ := EisEuclid.le_dest _ _ h2
    have hN : N0 + 1 = 2 * B + u := by rw [← hu]; lib_nat
    have hNi : Int.ofNat (N0 + 1) = 2 * Int.ofNat B + Int.ofNat u := congrArg Int.ofNat hN
    have hsg : ∃ d2 : Int, (d2 = 1 ∨ d2 = -1) ∧ r2 = d2 * Int.ofNat B := by
      rcases EisEuclid.sgn r2 with e | e
      · exact ⟨1, Or.inl rfl, by rw [e, hB]; lib_int⟩
      · exact ⟨-1, Or.inr rfl, by rw [e, hB]; lib_int⟩
    obtain ⟨d2, hd2, hr2⟩ := hsg
    cases Nat.lt_or_ge (67 * (u * u)) (3 * ((N0 + 1) * (N0 + 1))) with
    | inl hu2 =>
      exact branch N0 r1 r2 d2 B 1 hr2 hd2 u (absw d2 hd2 _ u (Or.inr (by rw [hr2, hNi]; show 2 * (d2 * Int.ofNat B) + -(d2 * (2 * Int.ofNat B + Int.ofNat u)) = _; lib_int))) hu2
        (fun d1 hk => resc2 B d1 d2 _ (by rw [hk]; rfl) hd2)
    | inr hu3 =>
      have lb2 : 4 * (N0 + 1) < 19 * u := lin_lb 67 3 19 4 u (N0 + 1) hu3 (nlt 1072 10) (Nat.zero_lt_succ N0)
      cases Nat.le_total u B with
      | inl huB =>
        obtain ⟨v, hv⟩ := EisEuclid.le_dest _ _ huB
        -- B = v + u, N = 3u + 2v
        have l3 : 3 * (4 * (N0 + 1)) < 3 * (19 * u) := Nat.mul_lt_mul_of_pos_left lb2 (Nat.zero_lt_succ 2)
        have hlin : 38 * v < 7 * (N0 + 1) := lt_cancel _ _ (12 * (N0 + 1)) (by
          calc 38 * v + 12 * (N0 + 1) = 38 * v + 3 * (4 * (N0 + 1)) := by lib_nat
            _ < 38 * v + 3 * (19 * u) := Nat.add_lt_add_left l3 _
            _ = 7 * (N0 + 1) + 12 * (N0 + 1) := by rw [hN, ← hv]; lib_nat)
        have hb : 67 * (v * v) < 3 * ((N0 + 1) * (N0 + 1)) := sq_ub 67 3 38 7 v (N0 + 1) hlin (nlt 3283 1048)
        exact branch N0 r1 r2 d2 B 2 hr2 hd2 v (absw d2 hd2 _ v (Or.inl (by rw [hr2, hNi, ← hv]; show 3 * (d2 * (Int.ofNat v + Int.ofNat u)) + -(d2 * (2 * (Int.ofNat v + Int.ofNat u) + Int.ofNat u)) = _; lib_int))) hb
          (fun d1 hk => resc3 B d1 d2 _ (by rw [hk]; rfl) hd2)
      | inr hBu =>
        obtain ⟨v, hv⟩ := EisEuclid.le_dest _ _ hBu
        -- u = v + B, N = 3B + v
        have hw3 : Int.natAbs (Int.ofNat 3 * r2 + -(d2 * Int.ofNat (N0 + 1))) = v := absw d2 hd2 _ v (Or.inr (by
          rw [hr2, hNi, ← hv]; show 3 * (d2 * Int.ofNat B) + -(d2 * (2 * Int.ofNat B + (Int.ofNat v + Int.ofNat B))) = _; lib_int))
        cases Nat.lt_or_ge (67 * (v * v)) (3 * ((N0 + 1) * (N0 + 1))) with
        | inl hb => exact branch N0 r1 r2 d2 B 2 hr2 hd2 v hw3 hb (fun d1 hk => resc3 B d1 d2 _ (by rw [hk]; rfl) hd2)
        | inr hv3 =>
          have lb3 : 4 * (N0 + 1) < 19 * v := lin_lb 67 3 19 4 v (N0 + 1) hv3 (nlt 1072 10) (Nat.zero_lt_succ N0)
          cases Nat.le_total v B with
          | inl hvB =>
            obtain ⟨x, hx⟩ := EisEuclid.le_dest _ _ hvB
            have hw4 : Int.natAbs (Int.ofNat 4 * r2 + -(d2 * Int.ofNat (N0 + 1))) = x := absw d2 hd2 _ x (Or.inl (by
              rw [hr2, hNi, ← hv, ← hx]; show 4 * (d2 * (Int.ofNat x + Int.ofNat v)) + -(d2 * (2 * (Int.ofNat x + Int.ofNat v) + (Int.ofNat v + (Int.ofNat x + Int.ofNat v)))) = _; lib_int))
            have l4 : 4 * (4 * (N0 + 1)) < 4 * (19 * v) := Nat.mul_lt_mul_of_pos_left lb3 (Nat.zero_lt_succ 3)
            have hlin : 57 * x < 3 * (N0 + 1) := lt_cancel _ _ (16 * (N0 + 1)) (by
              calc 57 * x + 16 * (N0 + 1) = 57 * x + 4 * (4 * (N0 + 1)) := by lib_nat
                _ < 57 * x + 4 * (19 * v) := Nat.add_lt_add_left l4 _
                _ = 3 * (N0 + 1) + 16 * (N0 + 1) := by rw [hN, ← hv, ← hx]; lib_nat)
            exact branch N0 r1 r2 d2 B 3 hr2 hd2 x hw4 (sq_ub 67 3 57 3 x (N0 + 1) hlin (nlt 603 9143))
              (fun d1 hk => resc4 B d1 d2 _ (by rw [hk]; rfl) hd2)
          | inr hBv =>
            obtain ⟨x, hx⟩ := EisEuclid.le_dest _ _ hBv
            have hw4 : Int.natAbs (Int.ofNat 4 * r2 + -(d2 * Int.ofNat (N0 + 1))) = x := absw d2 hd2 _ x (Or.inr (by
              rw [hr2, hNi, ← hv, ← hx]; show 4 * (d2 * Int.ofNat B) + -(d2 * (2 * Int.ofNat B + ((Int.ofNat x + Int.ofNat B) + Int.ofNat B))) = _; lib_int))
            have l4 : 4 * (4 * (N0 + 1)) < 4 * (19 * B) := Nat.mul_lt_mul_of_pos_left lb1 (Nat.zero_lt_succ 3)
            have hlin : 19 * x < 3 * (N0 + 1) := lt_cancel _ _ (16 * (N0 + 1)) (by
              calc 19 * x + 16 * (N0 + 1) = 19 * x + 4 * (4 * (N0 + 1)) := by lib_nat
                _ < 19 * x + 4 * (19 * B) := Nat.add_lt_add_left l4 _
                _ = 3 * (N0 + 1) + 16 * (N0 + 1) := by rw [hN, ← hv, ← hx]; lib_nat)
            exact branch N0 r1 r2 d2 B 3 hr2 hd2 x hw4 (sq_ub 67 3 19 3 x (N0 + 1) hlin (nlt 603 479))
              (fun d1 hk => resc4 B d1 d2 _ (by rw [hk]; rfl) hd2)

end Euc67
#print axioms Euc67.nrm_pair
#print axioms Euc67.lin_lb
#print axioms Euc67.sq_ub
#print axioms Euc67.resc2
#print axioms Euc67.resc3
#print axioms Euc67.resc4
#print axioms Euc67.kcancel
#print axioms Euc67.branch
#print axioms Euc67.nlt
#print axioms Euc67.lt_cancel
#print axioms Euc67.absw
#print axioms Euc67.dh67_r

#print axioms: 12 theorems, no axioms.

78.309 gcd (\(d=67\))

Bezout via the Dedekind–Hasse step.

\[ \exists d=x\alpha +y\beta ,\ d\mid \alpha ,\beta . \]

lang/Gcd67.lean

/-
  lang/Gcd67 — T48/T50, T48: **gcd as a combination in ℤ[(1 + √−67)/2]** (a PID that is not norm-Euclidean).
    edh           for β ≠ 0: sα − qβ with N < N(β), and = 0 only for s = 1          (Euc67.dh_r on the two-step remainder)
    gaux          from any nonzero d = xα + yβ: either d | α, d | β, or a nonzero element of smaller norm in the ideal
    egcd_exists   d = xα + yβ dividing α and β
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.DH67

namespace Euc67

open FTAChain DetChain LibInt

theorem lt_of_mul (a N : Nat) (h : a * (N + 1) < (N + 1) * (N + 1)) : a < N + 1 := by
  cases Nat.lt_or_ge a (N + 1) with
  | inl h' => exact h'
  | inr h' => exact absurd (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_right _ h')) (Nat.lt_irrefl _)

/-- **The Dedekind–Hasse step.** -/
theorem edh (α β : Int × Int) (N0 : Nat) (hβ : nrm β = N0 + 1) :
    ∃ s q : Int × Int, nrm ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q β))) < N0 + 1 ∧
      (((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q β))) = (Ring67.ring67 LibInt.intR).zero → s = (Ring67.ring67 LibInt.intR).one) := by
  have hN : en β = Int.ofNat (N0 + 1) := by rw [← nrm_cast, hβ]
  generalize hS : (Ring67.ring67 LibInt.intR).mul α (cj β) = S
  obtain ⟨q2, r2, h2, b2⟩ := GaussEuclid.rnd S.2 N0
  obtain ⟨q1, t, h1, b1⟩ := GaussEuclid.rnd (2 * S.1 + r2) (2 * N0 + 1)
  have hT : Int.natAbs (2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2) ≤ N0 + 1 := by
    have e : 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = t := by
      have e2 : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
      calc 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = (2 * S.1 + r2) + -(q1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
        _ = t := by rw [h1, ← e2]; lib_int
    rw [e]; exact Nat.le_of_mul_le_mul_left (Nat.le_trans b1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
  obtain ⟨s, e, hlt, hzero⟩ := dh67_r N0 (S.1 + -(q1 * Int.ofNat (N0 + 1))) r2 b2 hT
  have hSr : S = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (q1, q2) (Int.ofNat (N0 + 1), 0)) (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) := by
    apply Prod.ext
    · show S.1 = q1 * Int.ofNat (N0 + 1) + -(q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0) + (S.1 + -(q1 * Int.ofNat (N0 + 1))); lib_int
    · show S.2 = q1 * 0 + q2 * Int.ofNat (N0 + 1) + q2 * 0 + r2; rw [h2]; show q2 * Int.ofNat (N0 + 1) + r2 = _; lib_int
  generalize hr : (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) = r at hSr hlt hzero
  refine ⟨s, (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (q1, q2)) e, ?_, ?_⟩
  all_goals
    have key : (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
        = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s r) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) := by
      rw [show (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
          = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s ((Ring67.ring67 LibInt.intR).mul α (cj β))) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (q1, q2)) e) ((Ring67.ring67 LibInt.intR).mul β (cj β)))) by lib_ring,
        hS, mul_cj, hN, hSr]
      lib_ring
  · -- norm bound
    have hn : en ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (q1, q2)) e) β))) * Int.ofNat (N0 + 1)
        = en ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s r) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      rw [← key, en_mul, en_cj, hN]
    apply lt_of_mul
    have : nrm ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s (q1, q2)) e) β))) * (N0 + 1)
        = nrm ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s r) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm _) * Int.ofNat (N0 + 1) = Int.ofNat (nrm _)
      rw [nrm_cast, nrm_cast]; exact hn
    rw [this]; exact hlt
  · intro h0
    apply hzero
    rw [← key, h0]; lib_ring

theorem nrm_succ (d : Int × Int) (hd : d ≠ (Ring67.ring67 LibInt.intR).zero) : ∃ N0, nrm d = N0 + 1 := by
  cases h : nrm d with
  | zero => exact absurd (nrm_zero d h) hd
  | succ N0 => exact ⟨N0, rfl⟩

theorem gaux (α β : Int × Int) : ∀ G (d x y : Int × Int), d = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x α) ((Ring67.ring67 LibInt.intR).mul y β) →
    nrm d ≤ G → d ≠ (Ring67.ring67 LibInt.intR).zero →
    ∃ d' x' y', d' = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x' α) ((Ring67.ring67 LibInt.intR).mul y' β) ∧ Dvd' d' α ∧ Dvd' d' β
  | 0, d, _, _, _, hG, hd => absurd (nrm_zero d (Nat.le_antisymm hG (Nat.zero_le _))) hd
  | G + 1, d, x, y, hdef, hG, hd => by
    obtain ⟨N0, hN0⟩ := nrm_succ d hd
    obtain ⟨s, q, hlt, hz⟩ := edh α d N0 hN0
    have hle : ∀ ρ, nrm ρ < N0 + 1 → nrm ρ ≤ G := fun ρ h => Nat.le_of_lt_succ (Nat.lt_of_lt_of_le h (Nat.le_trans (Nat.le_of_eq hN0.symm) hG))
    by_cases hρ : (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q d)) = (Ring67.ring67 LibInt.intR).zero
    · have s1 := hz hρ
      rw [s1] at hρ
      have hα : Dvd' d α := ⟨q, by
        calc α = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).one α) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q d))) ((Ring67.ring67 LibInt.intR).mul d q) := by lib_ring
          _ = _ := by rw [hρ]; lib_ring⟩
      obtain ⟨s', q', hlt', hz'⟩ := edh β d N0 hN0
      by_cases hρ' : (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul s' β) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q' d)) = (Ring67.ring67 LibInt.intR).zero
      · have s1' := hz' hρ'
        rw [s1'] at hρ'
        exact ⟨d, x, y, hdef, hα, ⟨q', by
          calc β = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).one β) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q' d))) ((Ring67.ring67 LibInt.intR).mul d q') := by lib_ring
            _ = _ := by rw [hρ']; lib_ring⟩⟩
      · exact gaux α β G _ ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q' x)) ((Ring67.ring67 LibInt.intR).add s' ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q' y)))
          (by rw [hdef]; lib_ring) (hle _ hlt') hρ'
    · exact gaux α β G _ ((Ring67.ring67 LibInt.intR).add s ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q x))) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul q y))
        (by rw [hdef]; lib_ring) (hle _ hlt) hρ

/-- **gcd as a combination in ℤ[(1 + √−67)/2].** -/
theorem egcd_exists (α β : Int × Int) :
    ∃ d x y, d = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x α) ((Ring67.ring67 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β := by
  by_cases hb : β = (Ring67.ring67 LibInt.intR).zero
  · exact egcd_base α β hb
  · exact gaux α β (nrm β) β (Ring67.ring67 LibInt.intR).zero (Ring67.ring67 LibInt.intR).one (by lib_ring) (Nat.le_refl _) hb

end Euc67

#print axioms Euc67.lt_of_mul
#print axioms Euc67.edh
#print axioms Euc67.nrm_succ
#print axioms Euc67.gaux
#print axioms Euc67.egcd_exists

#print axioms: 5 theorems, no axioms.

78.310 Coprime splitting (\(d=67\))

Splitting by coprime norms.

\[ N(\gamma )=mn\Rightarrow \gamma =\alpha \beta . \]

lang/Cop67.lean

/-
  lang/Cop67 — T48/T50, T48: splitting by coprime norms in ℤ[(1+√−67)/2].

  If N(γ) = mn with sm + tn = 1 (m, n ≥ 1), then γ = αβ with N(α) = m, N(β) = n (N(a + bθ) = a² + ab + 17b²):
  α = gcd(γ, m), e = gcd(γ, n) (Euc67.egcd_exists), αe | γ by Euclid's lemma, γ | αe since mn = γγ̄, so γ = αe·v with v a unit;
  then N(α) | m, N(e) | n and N(α)N(e) = mn give N(α) = m. (Port of GaussCoprime.coprime_split.)
    en_emb, emb_mul, en_dvd, emb_add, en_zero_imp, cancel_left, en_nonneg_one, ecoprime_split
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Gcd67
import lang.GaussCoprime

namespace Cop67

open FTAChain DetChain LibInt Euc67

def emb (m : Int) : Int × Int := (m, 0)

theorem en_emb (m : Int) : en (emb m) = m * m := by show m * m + m * 0 + 17 * (0 * 0) = m * m; lib_int

theorem emb_mul (a b : Int) : (Ring67.ring67 LibInt.intR).mul (emb a) (emb b) = emb (a * b) := by
  apply Prod.ext
  · show a * b + -(0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0) = a * b; lib_int
  · show a * 0 + 0 * b + 0 * 0 = 0; lib_int

theorem en_dvd (d γ : Int × Int) (h : Dvd' d γ) : ∃ k, en γ = en d * k :=
  match h with
  | ⟨c, hc⟩ => ⟨en c, by rw [hc, en_mul]⟩

theorem emb_add (a b : Int) : emb (a + b) = (Ring67.ring67 LibInt.intR).add (emb a) (emb b) := by
  apply Prod.ext
  · rfl
  · show (0 : Int) = 0 + 0; rfl

theorem en_zero_imp (z : Int × Int) (h : en z = 0) : z = (Ring67.ring67 LibInt.intR).zero := by
  apply nrm_zero
  apply Int.ofNat.inj
  rw [nrm_cast]; exact h

theorem cancel_left (γ z : Int × Int) (hγ : en γ ≠ 0) (h : (Ring67.ring67 LibInt.intR).mul γ z = (Ring67.ring67 LibInt.intR).zero) : z = (Ring67.ring67 LibInt.intR).zero := by
  have : en γ * en z = 0 := by rw [← en_mul, h]; rfl
  cases int_noZD _ _ this with
  | inl e => exact absurd e hγ
  | inr e => exact en_zero_imp z e

theorem en_nonneg_one (z : Int × Int) (h : en z = -1) : False := by
  rw [← nrm_cast] at h
  exact Int.noConfusion h

/-- **Coprime splitting**: N(γ) = mn, sm + tn = 1 ⇒ γ = αβ with N(α) = m, N(β) = n. -/
theorem ecoprime_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) :
    ∃ α β, γ = (Ring67.ring67 LibInt.intR).mul α β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match egcd_exists γ (emb (Int.ofNat (m + 1))), egcd_exists γ (emb (Int.ofNat (n + 1))) with
  | ⟨d, x, y, hd, ⟨u, hu⟩, ⟨c1, hc1⟩⟩, ⟨e, x', y', he, ⟨u', hu'⟩, ⟨c2, hc2⟩⟩ =>
    have hco : (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (emb t) c2) e) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (emb s) c1) d) = (Ring67.ring67 LibInt.intR).one := by
      have h1 : (Ring67.ring67 LibInt.intR).one = emb (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; rfl
      rw [h1, emb_add, ← emb_mul, ← emb_mul, hc1, hc2]; lib_ring
    -- e | u, so γ = d e v
    match eeuclid e d u _ _ hco ⟨u', by rw [← hu, hu']⟩ with
    | ⟨v, hv⟩ =>
    have hγdev : γ = (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul d e) v := by rw [hu, hv]; lib_ring
    -- γ | d e
    have hmn : (Ring67.ring67 LibInt.intR).mul γ (cj γ) = emb (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by
      rw [mul_cj, hγ]; rfl
    have hW : (Ring67.ring67 LibInt.intR).mul d e = (Ring67.ring67 LibInt.intR).mul γ ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul x x') γ) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring6  -- (+146 chars; full line in the Zenodo source)
      rw [hd, he]
      have : (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul y y') ((Ring67.ring67 LibInt.intR).mul γ (cj γ)) = (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul y y') ((Ring67.ring67 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1)))) := by rw [hmn, emb_mul]
      calc (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x γ) ((Ring67.ring67 LibInt.intR).mul y (emb (Int.ofNat (m + 1))))) ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul x' γ) ((Ring67.ring67 LibInt.intR).mul y' (emb (Int.ofNat (n + 1)))))
          = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul γ ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul x x') γ) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul y x') (emb (In  -- (+20 chars; full line in the Zenodo source)
              ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul y y') ((Ring67.ring67 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1))))) := by lib_ring
        _ = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul γ ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul x x') γ) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul y x') (emb (In  -- (+20 chars; full line in the Zenodo source)
              ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul y y') ((Ring67.ring67 LibInt.intR).mul γ (cj γ))) := by rw [this]
        _ = _ := by lib_ring
    generalize (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul x x') γ) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1)))) ((Ring67.ring  -- (+77 chars; full line in the Zenodo source)
    have hγ0 : en γ ≠ 0 := by rw [hγ]; exact fun e => Nat.noConfusion (Int.ofNat.inj e)
    have hWv : (Ring67.ring67 LibInt.intR).mul W v = (Ring67.ring67 LibInt.intR).one := by
      have h0 : (Ring67.ring67 LibInt.intR).mul γ ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul W v) ((Ring67.ring67 LibInt.intR).neg (Ring67.ring67 LibInt.intR).one)) = (Ring67.ring67 LibInt.intR).zero := by
        rw [show (Ring67.ring67 LibInt.intR).mul γ ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul W v) ((Ring67.ring67 LibInt.intR).neg (Ring67.ring67 LibInt.intR).one)) = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul γ W) v) ((Ring67.ring67 LibInt.intR).neg γ) by lib_ring, ← hW, ← hγdev]; lib_ring
      have := cancel_left γ _ hγ0 h0
      calc (Ring67.ring67 LibInt.intR).mul W v = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul W v) ((Ring67.ring67 LibInt.intR).neg (Ring67.ring67 LibInt.intR).one)) (Ring67.ring67 LibInt.intR).one := by lib_ring
        _ = (Ring67.ring67 LibInt.intR).one := by rw [this]; lib_ring
    have hv1 : en v = 1 := by
      have : en W * en v = 1 := by rw [← en_mul, hWv]; rfl
      cases GaussCoprime.int_unit _ _ (by rw [show en v * en W = en W * en v by lib_int]; exact this) with
      | inl h => exact h
      | inr h => exact absurd h (en_nonneg_one v)
    -- norms
    have hN : en d * en e = Int.ofNat (m + 1) * Int.ofNat (n + 1) := by
      have := congrArg en hγdev
      rw [en_mul, en_mul, hv1, hγ] at this
      rw [show en d * en e = en d * en e * 1 by lib_int, ← this]; rfl
    have hdm : ∃ a, Int.ofNat (m + 1) = en d * a := by
      have k1 : Int.ofNat (m + 1) * Int.ofNat (m + 1) = en d * en c1 := by rw [← en_emb, hc1, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en d * en u := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu, en_mul]
      exact ⟨s * en c1 + t * en u, by
        calc Int.ofNat (m + 1) = Int.ofNat (m + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = s * (Int.ofNat (m + 1) * Int.ofNat (m + 1)) + t * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en d * (s * en c1 + t * en u) := by rw [k1, k2]; lib_int⟩
    have hen' : ∃ b, Int.ofNat (n + 1) = en e * b := by
      have k1 : Int.ofNat (n + 1) * Int.ofNat (n + 1) = en e * en c2 := by rw [← en_emb, hc2, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en e * en u' := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu', en_mul]
      exact ⟨t * en c2 + s * en u', by
        calc Int.ofNat (n + 1) = Int.ofNat (n + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = t * (Int.ofNat (n + 1) * Int.ofNat (n + 1)) + s * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en e * (t * en c2 + s * en u') := by rw [k1, k2]; lib_int⟩
    match hdm, hen' with
    | ⟨a, ha⟩, ⟨b, hb⟩ =>
      have hab : a * b = 1 := by
        have h0 : (en d * en e) * (a * b + -1) = 0 := by
          rw [show (en d * en e) * (a * b + -1) = (en d * a) * (en e * b) + -(en d * en e) by lib_int, ← ha, ← hb, hN]; lib_int
        cases int_noZD _ _ h0 with
        | inl e0 => exact absurd (by rw [hN] at e0; exact e0) (fun e1 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = 0 => Nat.noConfusion (Int.ofNat.inj e1))
        | inr e0 => calc a * b = (a * b + -1) + 1 := by lib_int
            _ = 1 := by rw [e0]; rfl
      have pos : ∀ (k : Nat) (z : Int × Int) (c : Int), Int.ofNat (k + 1) = en z * c → c = 1 ∨ c = -1 → c = 1 := fun k z c h hc => by
        cases hc with
        | inl h1 => exact h1
        | inr h1 =>
          exfalso
          rw [h1, ← nrm_cast] at h
          have : Int.ofNat (k + 1) + Int.ofNat (nrm z) = 0 := by rw [h]; lib_int
          have h2 : k + nrm z + 1 = 0 := by rw [show k + nrm z + 1 = k + 1 + nrm z by lib_nat]; exact Int.ofNat.inj (show Int.ofNat (k + 1 + nrm z) = Int.ofNat 0 from this)
          exact Nat.noConfusion h2
      have a1 := pos m d a ha (GaussCoprime.int_unit a b hab)
      have b1 := pos n e b hb (GaussCoprime.int_unit b a (by rw [show b * a = a * b by lib_int]; exact hab))
      refine ⟨d, (Ring67.ring67 LibInt.intR).mul e v, by rw [hγdev]; lib_ring, by rw [ha, a1]; lib_int, by rw [en_mul, hv1, hb, b1]⟩

end Cop67

#print axioms Cop67.en_emb
#print axioms Cop67.emb_mul
#print axioms Cop67.en_dvd
#print axioms Cop67.emb_add
#print axioms Cop67.en_zero_imp
#print axioms Cop67.cancel_left
#print axioms Cop67.en_nonneg_one
#print axioms Cop67.ecoprime_split

#print axioms: 8 theorems, no axioms.

78.311 The primary class of 4489a (Legendre symbol)

\(H_{67}=\{\chi _{67}(\rho )=1\}\); \(\chi (-1)=-1\).

\[ H_{67}=\{a+b\theta :\ \chi _{67}(a+34b)=1\}. \]

lang/Prim67.lean

/-
  lang/Prim67 — T48/T50: **the primary class in ℤ[(1 + √−67)/2] through the Legendre symbol** (no residue enumeration):
  ρ(a + bθ) = a + 34b is the reduction mod √−67 (θ ≡ 34), and H67 = {z : χ₆₇(ρ z) = 1} (QChar.chi with p = 67).
    prime67      67 is prime (ZMod.md certificates)
    chi_m1       χ(−1) = −1 (67 ≡ 3 mod 4);  chi_neg  χ(−x) = −χ(x)
    cr_mul       ρ(xy) ≡ ρ(x)ρ(y) (mod 67)
    prim_mul, prim_cj, unit_pm, normalize, unit_one    the interface used by the Hecke files
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc67
import lang.GaussQR
import lang.CubicTwo
import lang.PencilClosed
import lang.W36Mul

set_option maxRecDepth 20000

namespace Prim67

open FTAChain DetChain LibInt Euc67 ZMod QChar
open LibRing (castZ castN)

theorem prime67 : ∀ d, Dv d (2 * 32 + 1 + 1 + 1) → d = 1 ∨ d = 2 * 32 + 1 + 1 + 1 := by
  intro d ⟨q, hq⟩
  match d with
  | 0 => exact absurd (hq.trans (Nat.mul_zero q)) (fun h => Nat.noConfusion h)
  | e + 1 =>
    match e with
    | 0 => exact Or.inl rfl
    | 1 => exact absurd (ZMod.dv_md_zero 1 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 2 => exact absurd (ZMod.dv_md_zero 2 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 3 => exact absurd (ZMod.dv_md_zero 3 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 4 => exact absurd (ZMod.dv_md_zero 4 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 5 => exact absurd (ZMod.dv_md_zero 5 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 6 => exact absurd (ZMod.dv_md_zero 6 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 7 => exact absurd (ZMod.dv_md_zero 7 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 8 => exact absurd (ZMod.dv_md_zero 8 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 9 => exact absurd (ZMod.dv_md_zero 9 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 10 => exact absurd (ZMod.dv_md_zero 10 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 11 => exact absurd (ZMod.dv_md_zero 11 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 12 => exact absurd (ZMod.dv_md_zero 12 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 13 => exact absurd (ZMod.dv_md_zero 13 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 14 => exact absurd (ZMod.dv_md_zero 14 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 15 => exact absurd (ZMod.dv_md_zero 15 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 16 => exact absurd (ZMod.dv_md_zero 16 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 17 => exact absurd (ZMod.dv_md_zero 17 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 18 => exact absurd (ZMod.dv_md_zero 18 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 19 => exact absurd (ZMod.dv_md_zero 19 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 20 => exact absurd (ZMod.dv_md_zero 20 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 21 => exact absurd (ZMod.dv_md_zero 21 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 22 => exact absurd (ZMod.dv_md_zero 22 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 23 => exact absurd (ZMod.dv_md_zero 23 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 24 => exact absurd (ZMod.dv_md_zero 24 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 25 => exact absurd (ZMod.dv_md_zero 25 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 26 => exact absurd (ZMod.dv_md_zero 26 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 27 => exact absurd (ZMod.dv_md_zero 27 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 28 => exact absurd (ZMod.dv_md_zero 28 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 29 => exact absurd (ZMod.dv_md_zero 29 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 30 => exact absurd (ZMod.dv_md_zero 30 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 31 => exact absurd (ZMod.dv_md_zero 31 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 32 => exact absurd (ZMod.dv_md_zero 32 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 33 => exact absurd (ZMod.dv_md_zero 33 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 34 => exact absurd (ZMod.dv_md_zero 34 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 35 => exact absurd (ZMod.dv_md_zero 35 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 36 => exact absurd (ZMod.dv_md_zero 36 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 37 => exact absurd (ZMod.dv_md_zero 37 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 38 => exact absurd (ZMod.dv_md_zero 38 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 39 => exact absurd (ZMod.dv_md_zero 39 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 40 => exact absurd (ZMod.dv_md_zero 40 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 41 => exact absurd (ZMod.dv_md_zero 41 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 42 => exact absurd (ZMod.dv_md_zero 42 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 43 => exact absurd (ZMod.dv_md_zero 43 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 44 => exact absurd (ZMod.dv_md_zero 44 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 45 => exact absurd (ZMod.dv_md_zero 45 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 46 => exact absurd (ZMod.dv_md_zero 46 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 47 => exact absurd (ZMod.dv_md_zero 47 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 48 => exact absurd (ZMod.dv_md_zero 48 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 49 => exact absurd (ZMod.dv_md_zero 49 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 50 => exact absurd (ZMod.dv_md_zero 50 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 51 => exact absurd (ZMod.dv_md_zero 51 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 52 => exact absurd (ZMod.dv_md_zero 52 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 53 => exact absurd (ZMod.dv_md_zero 53 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 54 => exact absurd (ZMod.dv_md_zero 54 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 55 => exact absurd (ZMod.dv_md_zero 55 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 56 => exact absurd (ZMod.dv_md_zero 56 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 57 => exact absurd (ZMod.dv_md_zero 57 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 58 => exact absurd (ZMod.dv_md_zero 58 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 59 => exact absurd (ZMod.dv_md_zero 59 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 60 => exact absurd (ZMod.dv_md_zero 60 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 61 => exact absurd (ZMod.dv_md_zero 61 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 62 => exact absurd (ZMod.dv_md_zero 62 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 63 => exact absurd (ZMod.dv_md_zero 63 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 64 => exact absurd (ZMod.dv_md_zero 64 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 65 => exact absurd (ZMod.dv_md_zero 65 67 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 66 => exact Or.inr rfl
    | e' + 67 =>
      exfalso
      cases q with
      | zero => exact Nat.noConfusion (hq.trans (Nat.zero_mul _))
      | succ q' =>
        have : 68 ≤ 67 := by
          calc 68 ≤ 68 + (e' + q' * (e' + 67 + 1)) := Nat.le_add_right _ _
            _ = (q' + 1) * (e' + 67 + 1) := by lib_nat
            _ = 67 := hq.symm
        exact Nat.not_succ_le_self 67 this

theorem pw_neg_one : ∀ k, Frobenius.pw (ZMod.ring (2 * 32 + 1 + 1)) ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one) (2 * k + 1) = (ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one
  | 0 => by show (ZMod.ring (2 * 32 + 1 + 1)).mul (ZMod.ring (2 * 32 + 1 + 1)).one ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one) = _; lib_ring
  | k + 1 => by
    show (ZMod.ring (2 * 32 + 1 + 1)).mul ((ZMod.ring (2 * 32 + 1 + 1)).mul (Frobenius.pw (ZMod.ring (2 * 32 + 1 + 1)) ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one) (2 * k + 1)) ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one)) ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one) = _
    rw [pw_neg_one k]; lib_ring

theorem chi_m1 : chi 32 ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one) = -1 :=
  chi_neg_of 32 _ (PencilClosed.negnz 32 _ (PowerSum.one_ne_zero' (2 * 32 + 1))) (by rw [show 32 + 1 = 2 * 16 + 1 from rfl, pw_neg_one 16]; exact ne_pm 32)

theorem chi_neg (x : Zp (2 * 32 + 1 + 1)) : chi 32 ((ZMod.ring (2 * 32 + 1 + 1)).neg x) = -chi 32 x := by
  rw [show (ZMod.ring (2 * 32 + 1 + 1)).neg x = (ZMod.ring (2 * 32 + 1 + 1)).mul ((ZMod.ring (2 * 32 + 1 + 1)).neg (ZMod.ring (2 * 32 + 1 + 1)).one) x by lib_ring, chi_mul 32 prime67, chi_m1]; lib_int

def rho (z : Int × Int) : Int := z.1 + 34 * z.2

def cr (z : Int × Int) : Zp (2 * 32 + 1 + 1) := castZ (ZMod.ring (2 * 32 + 1 + 1)) (rho z)

theorem rho_mul (x y : Int × Int) : rho ((Ring67.ring67 LibInt.intR).mul x y) = rho x * rho y + -(67 * (17 * (x.2 * y.2))) := by
  show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + 34 * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + 34 * x.2) * (y.1 + 34 * y.2) + -(67 * (17 * (x.2 * y.2)))
  lib_int

theorem cz67 (w : Int) : castZ (ZMod.ring (2 * 32 + 1 + 1)) (67 * w) = (ZMod.ring (2 * 32 + 1 + 1)).zero := by
  rw [HasseDavenport.castZ_mul, show castZ (ZMod.ring (2 * 32 + 1 + 1)) 67 = (ZMod.ring (2 * 32 + 1 + 1)).zero from rfl]; lib_ring

theorem cr_mul (x y : Int × Int) : cr ((Ring67.ring67 LibInt.intR).mul x y) = (ZMod.ring (2 * 32 + 1 + 1)).mul (cr x) (cr y) := by
  unfold cr; rw [rho_mul, IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_neg, cz67]; lib_ring

theorem cr_neg (z : Int × Int) : cr (-z.1, -z.2) = (ZMod.ring (2 * 32 + 1 + 1)).neg (cr z) := by
  unfold cr; rw [show rho (-z.1, -z.2) = -rho z by show -z.1 + 34 * -z.2 = -(z.1 + 34 * z.2); lib_int, HasseDavenport.castZ_neg]

def Prim67 (z : Int × Int) : Prop := chi 32 (cr z) = 1

instance (z : Int × Int) : Decidable (Prim67 z) := inferInstanceAs (Decidable (_ = _))

theorem prim_mul (x y : Int × Int) (hx : Prim67 x) (hy : Prim67 y) : Prim67 ((Ring67.ring67 LibInt.intR).mul x y) := by
  show chi 32 (cr _) = 1
  rw [cr_mul, chi_mul 32 prime67, show chi 32 (cr x) = 1 from hx, show chi 32 (cr y) = 1 from hy]; rfl

theorem prim_cj (z : Int × Int) (hz : Prim67 z) : Prim67 (cj z) := by
  have e : cr (cj z) = cr z := by
    unfold cr
    rw [show rho (cj z) = rho z + 67 * -z.2 by show z.1 + z.2 + 34 * -z.2 = z.1 + 34 * z.2 + 67 * -z.2; lib_int, IntCast.castZ_add, cz67]; lib_ring
  show chi 32 (cr (cj z)) = 1; rw [e]; exact hz

theorem unit_pm (u : Int × Int) (h : en u = 1) : u = (Ring67.ring67 LibInt.intR).one ∨ u = (-1, 0) := by
  have h4 := four_nrm u
  have hn : nrm u = 1 := Int.ofNat.inj (by rw [nrm_cast, h]; rfl)
  rw [hn] at h4
  have hB : Int.natAbs u.2 * Int.natAbs u.2 = 0 := by
    cases hb : Int.natAbs u.2 * Int.natAbs u.2 with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 67 * (t + 1) = (Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 67 * t + 63) + 4 by lib_nat] at h4
                exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h4))))
  have b0 : u.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at h4
  have hT : Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) = 2 * 2 := h4.symm
  have hT2 : Int.natAbs (2 * u.1 + u.2) = 2 := by
    match hx : Int.natAbs (2 * u.1 + u.2) with
    | 0 => rw [hx] at hT; exact Nat.noConfusion hT
    | 1 => rw [hx] at hT; exact Nat.noConfusion (Nat.succ.inj hT)
    | 2 => rfl
    | t + 3 => rw [hx, show (t + 3) * (t + 3) = (t * t + 6 * t + 5) + 4 by lib_nat] at hT; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hT))))
  rw [b0] at hT2
  obtain ⟨a, b⟩ := u
  show (a, b) = (1, 0) ∨ (a, b) = (-1, 0)
  have b0' : b = 0 := b0
  rcases EisEuclid.sgn (2 * a + 0) with s | s <;> rw [hT2] at s
  · exact Or.inl (Prod.ext (W36Mul.two_cancel_int a 1 (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')
  · exact Or.inr (Prod.ext (W36Mul.two_cancel_int a (-1) (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')

theorem neg_mul' (z : Int × Int) : (Ring67.ring67 LibInt.intR).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -(0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2) = -z.1; lib_int) (by show -1 * z.2 + 0 * z.1 + 0 * z.2 = -z.2; lib_int)

/-- Generic factorization: (a + c b)(a − c' b) + D t b² = a² + ab + t b² when c = c' + 1 and D t = c c' + t, with D | a + c b. -/
theorem fac_gen (a b c c' D t q : Int) (h1 : c = c' + 1) (h2 : D * t = c * c' + t) (hq : a + c * b = D * q) :
    a * a + a * b + t * (b * b) = D * (q * (a + -(c' * b)) + t * (b * b)) := by
  calc a * a + a * b + t * (b * b) = (a + c * b) * (a + -(c' * b)) + (c * c' + t) * (b * b) + (c' + 1 + -c) * (a * b) := by lib_int
    _ = D * q * (a + -(c' * b)) + D * t * (b * b) + (c' + 1 + -c) * (a * b) := by rw [hq, h2]
    _ = D * q * (a + -(c' * b)) + D * t * (b * b) := by rw [h1]; lib_int
    _ = _ := by lib_int

theorem cr_ne (α : Int × Int) (hd : ∀ k, en α ≠ 67 * k) : cr α ≠ (ZMod.ring (2 * 32 + 1 + 1)).zero := fun h0 => by
  obtain ⟨q, hq⟩ := CubicTwo.cast_zero_gen (2 * 32 + 1 + 1) (rho α) h0
  obtain ⟨a, b⟩ := α
  exact hd _ (fac_gen a b 34 33 67 17 q rfl rfl hq)

/-- 67 ∤ N(α) ⇒ ±α ∈ H67. -/
theorem normalize (α : Int × Int) (hd : ∀ k, en α ≠ 67 * k) : ∃ u, en u = 1 ∧ Prim67 ((Ring67.ring67 LibInt.intR).mul u α) := by
  rcases GaussQR.chi_pm 32 _ (cr_ne α hd) with h | h
  · exact ⟨(Ring67.ring67 LibInt.intR).one, rfl, by rw [(Ring67.ring67 LibInt.intR).one_mul]; exact h⟩
  · refine ⟨(-1, 0), rfl, ?_⟩
    show chi 32 (cr _) = 1
    rw [neg_mul', cr_neg, chi_neg, h]; rfl

theorem unit_one (u α : Int × Int) (hu : en u = 1) (pα : Prim67 α) (pu : Prim67 ((Ring67.ring67 LibInt.intR).mul u α)) : u = (Ring67.ring67 LibInt.intR).one := by
  rcases unit_pm u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, neg_mul'] at pu
  have h1 : chi 32 (cr α) = 1 := pα
  have h2 : chi 32 (cr (-α.1, -α.2)) = 1 := pu
  rw [cr_neg, chi_neg, h1] at h2
  exact Int.noConfusion h2

end Prim67

#print axioms Prim67.prime67
#print axioms Prim67.pw_neg_one
#print axioms Prim67.chi_m1
#print axioms Prim67.chi_neg
#print axioms Prim67.rho_mul
#print axioms Prim67.cz67
#print axioms Prim67.cr_mul
#print axioms Prim67.fac_gen
#print axioms Prim67.cr_neg
#print axioms Prim67.prim_mul
#print axioms Prim67.prim_cj
#print axioms Prim67.unit_pm
#print axioms Prim67.neg_mul'
#print axioms Prim67.fac_gen
#print axioms Prim67.cr_ne
#print axioms Prim67.normalize
#print axioms Prim67.unit_one

#print axioms: 17 theorems, no axioms.

78.312 Unique splitting into \(H_{67}\)

By coprime norms prime to \(67\).

\[ \gamma =\alpha \beta \ \text {unique}. \]

lang/Uniq67.lean

/-
  lang/Uniq67 — T48/T50, T48: the unique splitting into H67-elements (Prim67) in ℤ[(1+√−67)/2]:
    prim67_quot, uniq_split67, prim67_split   (norms prime to 67; port of Sq2Unique)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Prim67
import lang.Cop67

namespace Uniq67

open FTAChain DetChain LibInt Euc67 Prim67

/-- y ∈ H67 when x and xy are (67 ∤ N(y)). -/
theorem prim67_quot (x y : Int × Int) (hy : ∀ k, en y ≠ 67 * k) (hx : Prim67 x) (hxy : Prim67 ((Ring67.ring67 LibInt.intR).mul x y)) : Prim67 y := by
  obtain ⟨u, hu, hpu⟩ := normalize y hy
  have h1 : Prim67 ((Ring67.ring67 LibInt.intR).mul u ((Ring67.ring67 LibInt.intR).mul x y)) := by
    rw [show (Ring67.ring67 LibInt.intR).mul u ((Ring67.ring67 LibInt.intR).mul x y) = (Ring67.ring67 LibInt.intR).mul x ((Ring67.ring67 LibInt.intR).mul u y) by lib_ring]
    exact prim_mul _ _ hx hpu
  have hu1 := unit_one u _ hu hxy h1
  rw [hu1, (Ring67.ring67 LibInt.intR).one_mul] at hpu
  exact hpu

/-- **Uniqueness of the primary splitting.** -/
theorem uniq_split67 (α β α' β' : Int × Int) (M N s t : Int) (hα : en α = M) (hα' : en α' = M) (hβ' : en β' = N)
    (hst : s * M + t * N = 1) (hM : M ≠ 0) (pα : Prim67 α) (pα' : Prim67 α') (h : (Ring67.ring67 LibInt.intR).mul α β = (Ring67.ring67 LibInt.intR).mul α' β') :
    α = α' ∧ β = β' := by
  have hco : (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (Cop67.emb s) (cj α)) α) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (Cop67.emb t) (cj β')) β') = (Ring67.ring67 LibInt.intR).one := by
    rw [show (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (Cop67.emb s) (cj α)) α) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (Cop67.emb t) (cj β')) β') = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Cop67.emb s) ((Ring67.ring67 LibInt.intR).mul α (cj α))) ((Ring67.ring67 LibInt.intR).mul (Cop67.emb t)  -- (+59 chars; full line in the Zenodo source)
      mul_cj, mul_cj, hα, hβ']
    show (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Cop67.emb s) (Cop67.emb M)) ((Ring67.ring67 LibInt.intR).mul (Cop67.emb t) (Cop67.emb N)) = _
    rw [Cop67.emb_mul, Cop67.emb_mul, ← Cop67.emb_add, hst]; rfl
  match eeuclid α β' α' _ _ hco ⟨β, by rw [show (Ring67.ring67 LibInt.intR).mul β' α' = (Ring67.ring67 LibInt.intR).mul α' β' by lib_ring, ← h]⟩ with
  | ⟨w, hw⟩ =>
    have hgw : en w = 1 := by
      have e : M * en w = M * 1 := by
        calc M * en w = en α * en w := by rw [hα]
          _ = en α' := by rw [← en_mul, ← hw]
          _ = M * 1 := by rw [hα']; lib_int
      have e0 : M * (en w + -1) = 0 := by rw [show M * (en w + -1) = M * en w + -(M * 1) by lib_int, e]; lib_int
      cases int_noZD _ _ e0 with
      | inl h0 => exact absurd h0 hM
      | inr h0 => calc en w = (en w + -1) + 1 := by lib_int
          _ = 1 := by rw [h0]; rfl
    have hw1 : w = (Ring67.ring67 LibInt.intR).one :=
      unit_one w α hgw pα (by rw [show (Ring67.ring67 LibInt.intR).mul w α = α' by rw [hw]; lib_ring]; exact pα')
    have hαα : α = α' := by rw [hw, hw1]; lib_ring
    refine ⟨hαα, ?_⟩
    have h0 : (Ring67.ring67 LibInt.intR).mul α ((Ring67.ring67 LibInt.intR).add β ((Ring67.ring67 LibInt.intR).neg β')) = (Ring67.ring67 LibInt.intR).zero := by
      rw [show (Ring67.ring67 LibInt.intR).mul α ((Ring67.ring67 LibInt.intR).add β ((Ring67.ring67 LibInt.intR).neg β')) = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul α β) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul α β')) by lib_ring, h, ← hαα]; lib_ring
    have := Cop67.cancel_left α _ (by rw [hα]; exact hM) h0
    calc β = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).add β ((Ring67.ring67 LibInt.intR).neg β')) β' := by lib_ring
      _ = β' := by rw [this]; lib_ring

/-- **Existence of the primary splitting.** -/
theorem prim67_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) (hm : ∀ k, Int.ofNat (m + 1) ≠ 67 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 67 * k)
    (pγ : Prim67 γ) : ∃ α β, γ = (Ring67.ring67 LibInt.intR).mul α β ∧ Prim67 α ∧ Prim67 β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match Cop67.ecoprime_split γ m n s t hγ hst with
  | ⟨α0, β0, h0, hα0, hβ0⟩ =>
    match normalize α0 (by rw [hα0]; exact hm) with
    | ⟨u, gu, pu⟩ =>
      have uc : (Ring67.ring67 LibInt.intR).mul u (cj u) = (Ring67.ring67 LibInt.intR).one := by rw [mul_cj, gu]; rfl
      have hβn : en ((Ring67.ring67 LibInt.intR).mul (cj u) β0) = Int.ofNat (n + 1) := by
        rw [en_mul, show en (cj u) = en u from en_cj u, gu, hβ0]; lib_int
      have hsplit : γ = (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul u α0) ((Ring67.ring67 LibInt.intR).mul (cj u) β0) := by
        rw [h0, show (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul u α0) ((Ring67.ring67 LibInt.intR).mul (cj u) β0) = (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul u (cj u)) ((Ring67.ring67 LibInt.intR).mul α0 β0) by lib_ring, uc]; lib_ring
      refine ⟨(Ring67.ring67 LibInt.intR).mul u α0, (Ring67.ring67 LibInt.intR).mul (cj u) β0, hsplit, pu,
        prim67_quot _ _ (by rw [hβn]; exact hn) pu (by rw [← hsplit]; exact pγ),
        by rw [en_mul, gu, hα0]; lib_int, hβn⟩

end Uniq67

#print axioms Uniq67.prim67_quot
#print axioms Uniq67.uniq_split67
#print axioms Uniq67.prim67_split

#print axioms: 3 theorems, no axioms.

78.313 4489a: multiplicativity

Theta coefficients over \(H_{67}\) are multiplicative.

\[ c_w(mn)=c_w(m)c_w(n). \]

lang/HeckeMult67.lean

/-
  lang/HeckeMult67 — T48/T50, T48: **the theta series of ℚ(√−67)'s Hecke character class is multiplicative**: for every
  multiplicative w on ℤ[θ], θ = (1 + √−67)/2,  c_w(n) = Σ_{α ∈ H67, N(α) = n} w(α)  satisfies c_w(mn) = c_w(m)c_w(n) for coprime m, n
  prime to 67.  Enumeration box67 n = HeckeMult.box (68n).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Uniq67
import lang.HeckeMultW

namespace HeckeMult67

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Euc67 Prim67 Uniq67

abbrev box67 (n : Nat) : List (Int × Int) := HeckeMult.box (68 * n)

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 68n  (67a² + (a + 34b)² = 68N, (2a + b)² + 67b² = 4N). -/
theorem abs_le67 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 68 * n ∧ Int.natAbs z.2 ≤ 68 * n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have h4 := four_nrm z
  rw [hn] at h4
  have h8 : 67 * (Int.natAbs z.1 * Int.natAbs z.1) + Int.natAbs (z.1 + 34 * z.2) * Int.natAbs (z.1 + 34 * z.2) = 68 * n := Int.ofNat.inj (by
    show 67 * Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs (z.1 + 34 * z.2) * Int.natAbs (z.1 + 34 * z.2)) = 68 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; unfold en; lib_int)
  have seven : ∀ X : Nat, X ≤ 67 * (X * X) := fun X => Nat.le_trans (HeckeMult.le_sq X) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 66)))
  constructor
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h8))
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans (Nat.le_of_eq h4.symm) (Nat.mul_le_mul_right n (Nat.le_add_right 4 64))))

def PD67 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim67 z

instance (n : Nat) (z : Int × Int) : Decidable (PD67 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum67 {A : Type} : ∀ l : List A, sumK (Ring67.ring67 LibInt.intR) (fun _ => (Ring67.ring67 LibInt.intR).zero) l = (Ring67.ring67 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Ring67.ring67 LibInt.intR).add (Ring67.ring67 LibInt.intR).zero (sumK (Ring67.ring67 LibInt.intR) (fun _ => (Ring67.ring67 LibInt.intR).zero) l) = _; rw [zsum67 l]; exact (Ring67.ring67 LibInt.intR).zero_add _

def I67 (n : Nat) (z : Int × Int) : Int × Int := if PD67 n z then (Ring67.ring67 LibInt.intR).one else (Ring67.ring67 LibInt.intR).zero

theorem I67_pos (n : Nat) (z : Int × Int) (h : PD67 n z) : I67 n z = (Ring67.ring67 LibInt.intR).one := ite_pos' _ _ h
theorem I67_neg (n : Nat) (z : Int × Int) (h : ¬ PD67 n z) : I67 n z = (Ring67.ring67 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw67 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Ring67.ring67 LibInt.intR) (fun z => (Ring67.ring67 LibInt.intR).mul (I67 n z) (w z)) (box67 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult67 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 67 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 67 * k) :
    cw67 w ((m + 1) * (n + 1)) = (Ring67.ring67 LibInt.intR).mul (cw67 w (m + 1)) (cw67 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD67 (m + 1) α → PD67 (n + 1) β → PD67 ((m + 1) * (n + 1)) ((Ring67.ring67 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Ring67.ring67 LibInt.intR).mul (cw67 w (m + 1)) (cw67 w (n + 1))
      = sumK (Ring67.ring67 LibInt.intR) (fun α => sumK (Ring67.ring67 LibInt.intR) (fun β => sumK (Ring67.ring67 LibInt.intR) (fun γ => (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (I67 (m + 1) α) (I67 (n + 1) β)) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β))) (w γ)) (box67 ((m + 1) * (n + 1)))) (box67 (n + 1))) (b  -- (+19 chars; full line in the Zenodo source)
    unfold cw67; rw [sumK_mul]
    refine sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD67 (m + 1) α
    · by_cases hβ : PD67 (n + 1) β
      · rw [I67_pos _ _ hα, I67_pos _ _ hβ]
        have hb := abs_le67 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Ring67.ring67 LibInt.intR) (fun γ => w γ) ((Ring67.ring67 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Ring67.ring67 LibInt.intR) (fun γ => (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).one (Ring67.ring67 LibInt.intR).one) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β))) (w γ)) (box67 ((m + 1) * (n + 1)))
            = sumK (Ring67.ring67 LibInt.intR) (fun γ => (Ring67.ring67 LibInt.intR).mul (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β)) (w γ)) (box67 ((m + 1) * (n + 1))) from sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I67_neg _ _ hβ]
        rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun γ => show (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (I67 (m + 1) α) (Ring67.ring67 LibInt.intR).zero) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β))) (w γ) = (Ring67.ring67 LibInt.intR).zero by lib_ring)]
        rw [zsum67]; lib_ring
    · rw [I67_neg _ _ hα]
      rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun γ => show (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).zero (I67 (n + 1) β)) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β))) (w γ) = (Ring67.ring67 LibInt.intR).zero by lib_ring)]
      rw [zsum67]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => sumK_swap (Ring67.ring67 LibInt.intR) _ _ _)]
  rw [sumK_swap (Ring67.ring67 LibInt.intR)]
  unfold cw67
  refine sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Ring67.ring67 LibInt.intR) (fun α => sumK (Ring67.ring67 LibInt.intR) (fun β => (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (I67 (m + 1) α) (I67 (n + 1) β)) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β))) (w γ)) (box67 (n + 1))) (box67 (m + 1))
      = (Ring67.ring67 LibInt.intR).mul (sumK (Ring67.ring67 LibInt.intR) (fun α => sumK (Ring67.ring67 LibInt.intR) (fun β => (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (I67 (m + 1) α) (I67 (n + 1) β)) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β))) (box67 (n + 1))) (box67 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD67 ((m + 1) * (n + 1)) γ
  · rw [I67_pos _ _ hγ]
    match prim67_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (I67 (m + 1) α) (I67 (n + 1) β)) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β)) = (Ring67.ring67 LibInt.intR).mul (delta (Ring67.ring67 LibInt.intR) α α0) (delta (Ring67.ring67 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD67 (m + 1) α ∧ PD67 (n + 1) β ∧ γ = (Ring67.ring67 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split67 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I67_pos _ _ hα, I67_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Ring67.ring67 LibInt.intR).mul (delta (Ring67.ring67 LibInt.intR) α α0) (delta (Ring67.ring67 LibInt.intR) β β0) = (Ring67.ring67 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD67 (m + 1) α
            · by_cases hβ : PD67 (n + 1) β
              · have hg : ¬ γ = (Ring67.ring67 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I67_neg _ _ hβ]; lib_ring
            · rw [I67_neg _ _ hα]; lib_ring
      rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le67 α0 _ nα0
      have bb := abs_le67 β0 _ nβ0
      rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Ring67.ring67 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => show (Ring67.ring67 LibInt.intR).mul (delta (Ring67.ring67 LibInt.intR) α α0) (Ring67.ring67 LibInt.intR).one = (Ring67.ring67 LibInt.intR).mul (delta (Ring67.ring67 LibInt.intR) α α0) (Ring67.ring67 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Ring67.ring67 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I67_neg _ _ hγ]
    have pt : ∀ α β, (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (I67 (m + 1) α) (I67 (n + 1) β)) (delta (Ring67.ring67 LibInt.intR) γ ((Ring67.ring67 LibInt.intR).mul α β)) = (Ring67.ring67 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD67 (m + 1) α
      · by_cases hβ : PD67 (n + 1) β
        · have hg : ¬ γ = (Ring67.ring67 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I67_neg _ _ hβ]; lib_ring
      · rw [I67_neg _ _ hα]; lib_ring
    rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun α => zsum67 _), zsum67]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult67_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 67 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 67 * k) :
    cw67 (fun z => Frobenius.pw (Ring67.ring67 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Ring67.ring67 LibInt.intR).mul (cw67 (fun z => Frobenius.pw (Ring67.ring67 LibInt.intR) z k) (m + 1)) (cw67 (fun z => Frobenius.pw (Ring67.ring67 LibInt.intR) z k) (n + 1)) :=
  hecke_mult67 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult67

#print axioms HeckeMult67.abs_le67
#print axioms HeckeMult67.zsum67
#print axioms HeckeMult67.I67_pos
#print axioms HeckeMult67.I67_neg
#print axioms HeckeMult67.hecke_mult67
#print axioms HeckeMult67.hecke_mult67_pow
#print axioms HeckeMult67.cw67

#print axioms: 7 theorems, no axioms.

78.314 Factorisation (\(d=67\))

The \(H_{67}\)-elements of norm \(p^k\) are the \(\pi ^j\bar \pi ^l\).

\[ \alpha =\pi ^j\bar \pi ^{\,l}. \]

lang/Prime67.lean

/-
  lang/Prime67 — T48/T50, T48: ℤ[(1+√−67)/2] above a split prime: π of prime norm is prime, N(α) = p^k ⇒ α = uπ^jπ̄^l, and the
  H67-elements of norm p^k are the π^j π̄^l (H67 conjugation-stable).  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult67

namespace Prime67

open FTAChain DetChain LibInt Euc67 Prim67 Uniq67 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Ring67.ring67 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Ring67.ring67 LibInt.intR).mul u (cj u) = (Ring67.ring67 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Ring67.ring67 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists π x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Ring67.ring67 LibInt.intR).mul (cj d) X) ((Ring67.ring67 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (cj d) X) π) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (cj d) Y) x)
          = (Ring67.ring67 LibInt.intR).mul (cj d) ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul X π) ((Ring67.ring67 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring67.ring67 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Ring67.ring67 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring67.ring67 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Ring67.ring67 LibInt.intR).mul d (Ring67.ring67 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Ring67.ring67 LibInt.intR).mul x y) = (Ring67.ring67 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2) + (x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * (y.1 + y.2) + -(-x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 * -y.2 + -x.2 *  -- (+132 chars; full line in the Zenodo source)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * -y.2 + -x.2 * (y.1 + y.2) + -x.2 * -y.2
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show z.1 + z.2 + -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Ring67.ring67 LibInt.intR).mul u ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) (pw (Ring67.ring67 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Ring67.ring67 LibInt.intR).mul α ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).one (Ring67.ring67 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Ring67.ring67 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Ring67.ring67 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Ring67.ring67 LibInt.intR).mul π ((Ring67.ring67 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring67.ring67 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Ring67.ring67 LibInt.intR).mul u ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Ring67.ring67 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Ring67.ring67 LibInt.intR).mul u ((Ring67.ring67 LibInt.intR).mul _ ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim67_one : Prim67 (Ring67.ring67 LibInt.intR).one := rfl

theorem prim67_pow (z : Int × Int) (h : Prim67 z) : ∀ j, Prim67 (pw (Ring67.ring67 LibInt.intR) z j)
  | 0 => prim67_one
  | j + 1 => prim_mul _ _ (prim67_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim67 α) :
    ∃ j l, j + l = k ∧ α = (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) (pw (Ring67.ring67 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim_mul _ _ (prim67_pow π pπ j) (prim67_pow (cj π) (prim_cj π pπ) l)
  have u1 := unit_one u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Prime67

#print axioms Prime67.nrm_mul
#print axioms Prime67.unit_inv
#print axioms Prime67.eprime_div
#print axioms Prime67.cj_mul
#print axioms Prime67.cj_cj
#print axioms Prime67.nrm_cj
#print axioms Prime67.nrm_one_eq
#print axioms Prime67.factor3
#print axioms Prime67.prim67_one
#print axioms Prime67.prim67_pow
#print axioms Prime67.primary_only3

#print axioms: 11 theorems, no axioms.

78.315 4489a: the Hecke recursion

Hecke recursion at split prime powers.

\[ c_W(p^{k+2})=(W(\pi )+W(\bar \pi ))c_W(p^{k+1})-W(\pi )W(\bar \pi )c_W(p^k). \]

lang/HeckePrime67.lean

/-
  lang/HeckePrime67 — T48/T50, T48: **ℚ(√−67)'s Hecke character at split prime powers**: π ∈ H67 of prime norm p, W multiplicative:
  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l and the Hecke recursion.  (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Prime67
import lang.HeckePrimeW

namespace HeckePrime67

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc67 Prim67 Uniq67 Prime67 HeckeMult67

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Ring67.ring67 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) A = (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) B) : A = B := by
  have h0 : (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) ((Ring67.ring67 LibInt.intR).add A ((Ring67.ring67 LibInt.intR).neg B)) = (Ring67.ring67 LibInt.intR).zero := by
    rw [show (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) ((Ring67.ring67 LibInt.intR).add A ((Ring67.ring67 LibInt.intR).neg B)) = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) A) ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) B)) by lib_ring, h]; lib_ring
  have := Cop67.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).add A ((Ring67.ring67 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one c π (nrm_one_eq c nc) pπ (by rw [(Ring67.ring67 LibInt.intR).mul_comm, ← hc]; exact prim_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + a * b + 17 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) :
    ∀ d, Dvd' π (pw (Ring67.ring67 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) (j d l' : Nat)
    (h : (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) (pw (Ring67.ring67 LibInt.intR) (cj π) (l' + 1 + d)) = (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π (j + 1 + d)) (pw (Ring67.ring67 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Ring67.ring67 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Ring67.ring67 LibInt.intR) π j (1 + d)] at h
  have h' : (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) (cj π) l') (pw (Ring67.ring67 LibInt.intR) (cj π) (1 + d)))
      = (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) (cj π) l') (pw (Ring67.ring67 LibInt.intR) π (1 + d))) := by
    calc _ = (Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) (pw (Ring67.ring67 LibInt.intR) π (1 + d))) (pw (Ring67.ring67 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Ring67.ring67 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j) (pw (Ring67.ring67 LibInt.intR) (cj π) l) = (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π j') (pw (Ring67.ring67 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) π x.1) (pw (Ring67.ring67 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD67 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim_mul _ _ (prim67_pow π pπ _) (prim67_pow (cj π) (prim_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) (k : Nat) :
    cw67 W ((P + 1 + 1) ^ k) = sumK (Ring67.ring67 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I67 ((P + 1 + 1) ^ k) z = sumK (Ring67.ring67 LibInt.intR) (fun x => delta (Ring67.ring67 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD67 ((P + 1 + 1) ^ k) z
    · rw [I67_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Ring67.ring67 LibInt.intR) k _ (fun x => delta (Ring67.ring67 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Ring67.ring67 LibInt.intR) k j l hjl]
    · rw [I67_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Ring67.ring67 LibInt.intR) k _ (fun _ => (Ring67.ring67 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Ring67.ring67 LibInt.intR) _).symm
  unfold cw67
  rw [sumK_ext (Ring67.ring67 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Ring67.ring67 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le67 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Ring67.ring67 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring67.ring67 LibInt.intR).one = (Ring67.ring67 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Ring67.ring67 LibInt.intR) z j) = pw (Ring67.ring67 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) z j) z) = (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring67.ring67 LibInt.intR).one = (Ring67.ring67 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) (k : Nat) :
    cw67 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Ring67.ring67 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring67.ring67 LibInt.intR).one = (Ring67.ring67 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π) (k : Nat) :
    cw67 W ((P + 1 + 1) ^ (k + 2)) = (Ring67.ring67 LibInt.intR).add
      ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add (W π) (W (cj π))) (cw67 W ((P + 1 + 1) ^ (k + 1))))
      ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (W π) (W (cj π))) (cw67 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime67

#print axioms HeckePrime67.pow_ne0
#print axioms HeckePrime67.nrm_pw
#print axioms HeckePrime67.npow_add
#print axioms HeckePrime67.en_ne
#print axioms HeckePrime67.cancel_pw3
#print axioms HeckePrime67.no_assoc3
#print axioms HeckePrime67.no_div3
#print axioms HeckePrime67.distinct_lt3
#print axioms HeckePrime67.distinct3
#print axioms HeckePrime67.w3_PD
#print axioms HeckePrime67.cw3_pk
#print axioms HeckePrime67.W_pow3
#print axioms HeckePrime67.cw3_pk'
#print axioms HeckePrime67.cw3_rec

#print axioms: 14 theorems, no axioms.

78.316 4489a: inert primes

A non-residue mod \(67\) stays prime; \((-p)^k\) is the only \(H_{67}\)-element of norm \(p^{2k}\).

\[ c_W(p^{2k})=W((-p)^k),\qquad c_W(p^{2k+1})=0. \]

lang/Inert67.lean

/-
  lang/Inert67 — T48/T50: **the ℚ(√−67) Hecke character at inert primes** (χ₆₇(p) = −1), via the Legendre symbol:
    cast_en, en_mod67   N(z) ≡ ρ(z)² (mod 67) (68N = ρ² + 67a²), so χ₆₇(N(z)) ≠ −1
    cw67_zero, inert_div, factor_inert, prim_inert, cw67_even, pow_odd, cw67_odd   (port of Inert19)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime67

namespace Inert67

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc67 Prim67 Uniq67 Prime67 HeckeMult67

variable (P : Nat)

theorem cast_en (z : Int × Int) : LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (en z) = (ZMod.ring (2 * 32 + 1 + 1)).mul (Prim67.cr z) (Prim67.cr z) := by
  have e44 : 68 * en z = Prim67.rho z * Prim67.rho z + 67 * (z.1 * z.1) := by
    show 68 * (z.1 * z.1 + z.1 * z.2 + 17 * (z.2 * z.2)) = (z.1 + 34 * z.2) * (z.1 + 34 * z.2) + 67 * (z.1 * z.1); lib_int
  have h1 : LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (68 * en z) = LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (en z) := by
    rw [HasseDavenport.castZ_mul, show LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) 68 = (ZMod.ring (2 * 32 + 1 + 1)).one from rfl]; lib_ring
  rw [← h1, e44, IntCast.castZ_add, HasseDavenport.castZ_mul, Prim67.cz67]; unfold Prim67.cr; lib_ring

/-- N(z) is never a non-residue mod 67. -/
theorem en_mod67 (z : Int × Int) (n : Nat) (hn : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat n)) = -1) (h : en z = Int.ofNat n) : False := by
  rw [← h, cast_en] at hn
  by_cases h0 : Prim67.cr z = (ZMod.ring (2 * 32 + 1 + 1)).zero
  · rw [h0, show (ZMod.ring (2 * 32 + 1 + 1)).mul (ZMod.ring (2 * 32 + 1 + 1)).zero (ZMod.ring (2 * 32 + 1 + 1)).zero = (ZMod.ring (2 * 32 + 1 + 1)).zero by lib_ring, QChar.chi_zero] at hn; exact Int.noConfusion hn
  · rw [QChar.chi_sq 32 Prim67.prime67 _ h0] at hn; exact Int.noConfusion hn

/-- **c_W(n) = 0 for n a non-residue mod 67.** -/
theorem cw67_zero (W : Int × Int → Int × Int) (n : Nat) (hn : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat n)) = -1) : cw67 W n = (Ring67.ring67 LibInt.intR).zero := by
  unfold cw67
  rw [sumK_ext (Ring67.ring67 LibInt.intR) _ (fun _ => (Ring67.ring67 LibInt.intR).zero) (fun z => by
    rw [I67_neg _ _ (fun h => en_mod67 z n hn h.1)]; lib_ring)]
  exact zsum67 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + Int.ofNat (P + 1 + 1) * 0 + 17 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + 0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (z : Int × Int) (h : nrm z = P + 1 + 1) : False :=
  en_mod67 z (P + 1 + 1) hc (by rw [← nrm_cast, h])

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (x y : Int × Int)
    (h : Dvd' (Pi P) ((Ring67.ring67 LibInt.intR).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists (Pi P) x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Ring67.ring67 LibInt.intR).mul (cj d) X) ((Ring67.ring67 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (cj d) X) (Pi P)) ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (cj d) Y) x)
          = (Ring67.ring67 LibInt.intR).mul (cj d) ((Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul X (Pi P)) ((Ring67.ring67 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring67.ring67 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Ring67.ring67 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring67.ring67 LibInt.intR).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Ring67.ring67 LibInt.intR).mul d (Ring67.ring67 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Ring67.ring67 LibInt.intR).mul u (pw (Ring67.ring67 LibInt.intR) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Ring67.ring67 LibInt.intR).mul α (Ring67.ring67 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Ring67.ring67 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring67.ring67 LibInt.intR).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Ring67.ring67 LibInt.intR).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Ring67.ring67 LibInt.intR).mul u ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : Prim67 (nPi P) := by
  show QChar.chi 32 (Prim67.cr (nPi P)) = 1
  have e : Prim67.cr (nPi P) = (ZMod.ring (2 * 32 + 1 + 1)).neg (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) := by
    unfold Prim67.cr; rw [show Prim67.rho (nPi P) = -Int.ofNat (P + 1 + 1) by show -Int.ofNat (P + 1 + 1) + 34 * 0 = _; lib_int, HasseDavenport.castZ_neg]
  rw [e, Prim67.chi_neg, hc]; rfl

theorem pw_neg (k : Nat) : pw (Ring67.ring67 LibInt.intR) (Pi P) k =
    (Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) ((Ring67.ring67 LibInt.intR).neg (Ring67.ring67 LibInt.intR).one) k) (pw (Ring67.ring67 LibInt.intR) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -(-0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0); lib_int
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1) + -0 * 0; lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Ring67.ring67 LibInt.intR) ((Ring67.ring67 LibInt.intR).neg (Ring67.ring67 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Ring67.ring67 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) (α : Int × Int)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim67 α) : α = pw (Ring67.ring67 LibInt.intR) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Ring67.ring67 LibInt.intR).mul u (pw (Ring67.ring67 LibInt.intR) ((Ring67.ring67 LibInt.intR).neg (Ring67.ring67 LibInt.intR).one) k) = v
  have he' : α = (Ring67.ring67 LibInt.intR).mul v (pw (Ring67.ring67 LibInt.intR) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one v _ (nrm_one_eq v hv1) (prim67_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -Int.ofNat (P + 1 + 1) * 0 + 17 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Ring67.ring67 LibInt.intR) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime67.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw67_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) :
    cw67 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Ring67.ring67 LibInt.intR) (nPi P) k) := by
  have hPD : PD67 ((P + 1 + 1) ^ (2 * k)) (pw (Ring67.ring67 LibInt.intR) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim67_pow _ (prim_nPi P hc) k⟩
  unfold cw67
  rw [sumK_ext (Ring67.ring67 LibInt.intR) _ (fun z => (Ring67.ring67 LibInt.intR).mul (delta (Ring67.ring67 LibInt.intR) z (pw (Ring67.ring67 LibInt.intR) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD67 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I67_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I67_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le67 _ _ hPD.1
  exact HeckeMult.pick_box (Ring67.ring67 LibInt.intR) W _ _ hb.1 hb.2

theorem pow_odd (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : ∀ k, QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)))) = -1
  | 0 => by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hc
  | k + 1 => by
    rw [show (P + 1 + 1) ^ (2 * (k + 1) + 1) = (P + 1 + 1) ^ (2 * k + 1) * ((P + 1 + 1) * (P + 1 + 1)) by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; show (P + 1 + 1) ^ (2 * k + 1) * (P + 1 + 1) * (P + 1 + 1) = _; lib_nat]
    show QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * (Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)))) = -1
    rw [HasseDavenport.castZ_mul, HasseDavenport.castZ_mul, QChar.chi_mul 32 Prim67.prime67, QChar.chi_mul 32 Prim67.prime67, pow_odd hc k, hc]; rfl

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw67_odd (W : Int × Int → Int × Int) (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) : cw67 W ((P + 1 + 1) ^ (2 * k + 1)) = (Ring67.ring67 LibInt.intR).zero := by
  exact cw67_zero W _ (pow_odd P hc k)

end Inert67

#print axioms Inert67.cast_en
#print axioms Inert67.en_mod67
#print axioms Inert67.cw67_zero
#print axioms Inert67.en_Pi
#print axioms Inert67.nrm_Pi
#print axioms Inert67.cj_Pi
#print axioms Inert67.not_p
#print axioms Inert67.dvd_conv
#print axioms Inert67.split_sq
#print axioms Inert67.inert_div
#print axioms Inert67.nat_pow2
#print axioms Inert67.factor_inert
#print axioms Inert67.prim_nPi
#print axioms Inert67.pw_neg
#print axioms Inert67.nrm_neg_one_pw
#print axioms Inert67.prim_inert
#print axioms Inert67.nrm_nPi
#print axioms Inert67.nrm_nPi_pw
#print axioms Inert67.cw67_even
#print axioms Inert67.pow_odd
#print axioms Inert67.cw67_odd

#print axioms: 21 theorems, no axioms.

78.317 The ring \(\mathbb Z[(1+\sqrt {-163})/2]\)

Pairs \(a+b\theta \), \(\theta ^2=\theta -41\).

\[ (a+b\theta )(c+d\theta )=(ac-41bd)+(ad+bc+bd)\theta . \]

lang/Ring163.lean

/-
  lang/Ring163 — T48/T50: the ring K[θ] = K[x]/(x² − x + 41) (for K = ℤ: the integers of ℚ(√−163)).
  (a + bθ)(c + dθ) = (ac − 41bd) + (ad + bc + bd)θ.  (Show-expansions generated.)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lib.RingTac

namespace Ring163

variable {R : Type} (K : DetChain.CRing R)

def rmul (x y : R × R) : R × R :=
  ((K.add (K.mul x.1 y.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul x.2 y.2) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.mul x.2 y.2)) (K.  -- (+676 chars; full line in the Zenodo source)

theorem r_assoc (a b e : R × R) : rmul K (rmul K a b) e = rmul K a (rmul K b e) := by
  apply Prod.ext
  · show (K.add (K.mul (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2))   -- (+8554 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2  -- (+1986 chars; full line in the Zenodo source)
    lib_ring

theorem r_comm (a b : R × R) : rmul K a b = rmul K b a := by
  apply Prod.ext
  · show (K.add (K.mul a.1 b.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.2 b.2) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2)) (K.mul a.2 b.2  -- (+1626 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) = (K.add (K.add (K.mul b.1 a.2) (K.mul b.2 a.1)) (K.mul b.2 a.2))
    lib_ring

theorem r_one (a : R × R) : rmul K (K.one, K.zero) a = a := by
  apply Prod.ext
  · show (K.add (K.mul K.one a.1) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul K.zero a.2) (K.mul K.zero a.2)) (K.mul K.zero a.2)) (K.mul K.zero a.2))   -- (+747 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul K.one a.2) (K.mul K.zero a.1)) (K.mul K.zero a.2)) = a.2
    lib_ring

theorem r_distrib (a b e : R × R) :
    rmul K a (K.add b.1 e.1, K.add b.2 e.2) = (K.add (rmul K a b).1 (rmul K a e).1, K.add (rmul K a b).2 (rmul K a e).2) := by
  apply Prod.ext
  · show (K.add (K.mul a.1 (K.add b.1 e.1)) (K.neg (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul a.2 (K.add b.2 e.2)) (K.mul a.2 (K.add b.2 e.2))) (K.mul a.2 (  -- (+3146 chars; full line in the Zenodo source)
    lib_ring
  · show (K.add (K.add (K.mul a.1 (K.add b.2 e.2)) (K.mul a.2 (K.add b.1 e.1))) (K.mul a.2 (K.add b.2 e.2))) = (K.add (K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.mul a.2 b.2)) (K.add (K.add (K.mul a.1 e.2) (K.mul a.2 e.1)) (K.mul a.2 e.2)))
    lib_ring

def ring163 : DetChain.CRing (R × R) where
  add := fun x y => (K.add x.1 y.1, K.add x.2 y.2)
  mul := rmul K
  neg := fun x => (K.neg x.1, K.neg x.2)
  zero := (K.zero, K.zero)
  one := (K.one, K.zero)
  add_assoc := fun a b c => by
    apply Prod.ext
    · exact K.add_assoc a.1 b.1 c.1
    · exact K.add_assoc a.2 b.2 c.2
  add_comm := fun a b => by
    apply Prod.ext
    · exact K.add_comm a.1 b.1
    · exact K.add_comm a.2 b.2
  zero_add := fun a => by
    apply Prod.ext
    · exact K.zero_add a.1
    · exact K.zero_add a.2
  add_neg := fun a => by
    apply Prod.ext
    · exact K.add_neg a.1
    · exact K.add_neg a.2
  mul_assoc := r_assoc K
  mul_comm := r_comm K
  one_mul := r_one K
  distrib := r_distrib K

end Ring163

#print axioms Ring163.r_assoc
#print axioms Ring163.r_comm
#print axioms Ring163.r_one
#print axioms Ring163.r_distrib
#print axioms Ring163.ring163

#print axioms: 5 theorems, no axioms.

78.318 Norm facts (\(d=163\))

\(4N=(2a+b)^2+163b^2\); Euclid’s lemma.

\[ N(a+b\theta )=a^2+ab+41b^2. \]

lang/Euc163.lean

/-
  lang/Euc163 — T48/T50: ℤ[θ], θ = (1 + √−163)/2, N(a + bθ) = a² + ab + 41b², 4N = (2a + b)² + 163b²: norm facts and Euclid's lemma.
  (The Dedekind–Hasse step is in DH163.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Ring163
import lang.GaussEuclid
import lang.W36Mul

namespace Euc163

open FTAChain DetChain LibInt


def en (z : Int × Int) : Int := z.1 * z.1 + z.1 * z.2 + 41 * (z.2 * z.2)
def nrm (z : Int × Int) : Nat := Int.natAbs (en z)
def cj (z : Int × Int) : Int × Int := (z.1 + z.2, -z.2)
def Dvd' (a b : Int × Int) : Prop := ∃ c, b = (Ring163.ring163 LibInt.intR).mul a c

theorem sgn (x : Int) : x = Int.ofNat (Int.natAbs x) ∨ x = -Int.ofNat (Int.natAbs x) := by
  cases x with
  | ofNat n => exact Or.inl rfl
  | negSucc n => exact Or.inr rfl

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ c, c + a = b := by
  induction h with
  | refl => exact ⟨0, Nat.zero_add a⟩
  | step _ ih =>
    obtain ⟨c, hc⟩ := ih
    exact ⟨c + 1, by rw [← hc]; lib_nat⟩

theorem ab_le (A B : Nat) : A * B ≤ A * A + 41 * (B * B) := by
  cases Nat.le_total A B with
  | inl h => exact Nat.le_trans (Nat.mul_le_mul_right B h) (Nat.le_trans (Nat.le_add_left _ (A * A)) (Nat.add_le_add_left (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 40))) _))
  | inr h => exact Nat.le_trans (Nat.mul_le_mul_left A h) (Nat.le_add_right _ _)

theorem en_rep (z : Int × Int) : ∃ c : Nat, en z = Int.ofNat c := by
  generalize hA : Int.natAbs z.1 = A
  generalize hB : Int.natAbs z.2 = B
  obtain ⟨c, hc⟩ := le_dest _ _ (ab_le A B)
  have e : Int.ofNat c + Int.ofNat A * Int.ofNat B = Int.ofNat A * Int.ofNat A + 41 * (Int.ofNat B * Int.ofNat B) := congrArg Int.ofNat hc
  have opp : en z = Int.ofNat A * Int.ofNat A + 41 * (Int.ofNat B * Int.ofNat B) + -(Int.ofNat A * Int.ofNat B) → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨c, by rw [h, ← e]; lib_int⟩
  have same : en z = Int.ofNat A * Int.ofNat A + 41 * (Int.ofNat B * Int.ofNat B) + Int.ofNat A * Int.ofNat B → ∃ c : Nat, en z = Int.ofNat c :=
    fun h => ⟨A * A + 41 * (B * B) + A * B, h⟩
  unfold en
  rcases sgn z.1 with h1 | h1 <;> rcases sgn z.2 with h2 | h2 <;> rw [hA] at h1 <;> rw [hB] at h2
  · exact same (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact opp (by unfold en; rw [h1, h2]; lib_int)
  · exact same (by unfold en; rw [h1, h2]; lib_int)

theorem nrm_cast (z : Int × Int) : Int.ofNat (nrm z) = en z := by
  obtain ⟨c, hc⟩ := en_rep z
  unfold nrm; rw [hc]; rfl

theorem four_nrm (z : Int × Int) : 4 * nrm z = Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 163 * (Int.natAbs z.2 * Int.natAbs z.2) := by
  apply Int.ofNat.inj
  show 4 * Int.ofNat (nrm z) = Int.ofNat (Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2)) + 163 * Int.ofNat (Int.natAbs z.2 * Int.natAbs z.2)
  rw [nrm_cast, GaussEuclid.nas, GaussEuclid.nas]; unfold en; lib_int

theorem mul_cj (z : Int × Int) : (Ring163.ring163 LibInt.intR).mul z (cj z) = (en z, 0) := by
  apply Prod.ext
  · show z.1 * (z.1 + z.2) + -(z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 * -z.2 + z.2 *  -- (+205 chars; full line in the Zenodo source)
    lib_int
  · show z.1 * -z.2 + z.2 * (z.1 + z.2) + z.2 * -z.2 = 0
    lib_int

theorem en_mul (x y : Int × Int) : en ((Ring163.ring163 LibInt.intR).mul x y) = en x * en y := by
  show (x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 *   -- (+1169 chars; full line in the Zenodo source)
      + 41 * ((x.1 * y.2 + x.2 * y.1 + x.2 * y.2) * (x.1 * y.2 + x.2 * y.1 + x.2 * y.2)) =
    (x.1 * x.1 + x.1 * x.2 + 41 * (x.2 * x.2)) * (y.1 * y.1 + y.1 * y.2 + 41 * (y.2 * y.2))
  lib_int

theorem en_cj (z : Int × Int) : en (cj z) = en z := by
  show (z.1 + z.2) * (z.1 + z.2) + (z.1 + z.2) * -z.2 + 41 * (-z.2 * -z.2) = z.1 * z.1 + z.1 * z.2 + 41 * (z.2 * z.2)
  lib_int

theorem nrm_zero (z : Int × Int) (h : nrm z = 0) : z = (Ring163.ring163 LibInt.intR).zero := by
  have h4 := four_nrm z
  rw [h] at h4
  have hB : Int.natAbs z.2 * Int.natAbs z.2 = 0 := by
    have : 163 * (Int.natAbs z.2 * Int.natAbs z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
    cases nat_mul_zero _ _ this with
    | inl e => exact Nat.noConfusion e
    | inr e => exact e
  have hT : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) = 0 := Nat.le_antisymm (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h4.symm)) (Nat.zero_le _)
  have b0 : z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  have t0 : 2 * z.1 + z.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hT with | inl e => exact e | inr e => exact e)
  have a0 : z.1 = 0 := W36Mul.two_cancel_int z.1 0 (by rw [show 2 * z.1 = 2 * z.1 + z.2 by rw [b0]; lib_int, t0]; rfl)
  exact Prod.ext a0 b0

theorem egcd_base (α β : Int × Int) (hb : β = (Ring163.ring163 LibInt.intR).zero) :
    ∃ d x y, d = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x α) ((Ring163.ring163 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β :=
  ⟨α, (Ring163.ring163 LibInt.intR).one, (Ring163.ring163 LibInt.intR).zero, by rw [hb]; lib_ring, ⟨(Ring163.ring163 LibInt.intR).one, by lib_ring⟩, ⟨(Ring163.ring163 LibInt.intR).zero, by rw [hb]; lib_ring⟩⟩

/-- **Euclid's lemma in ℤ[ω].** -/
theorem eeuclid (α β γ x y : Int × Int) (h1 : (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x α) ((Ring163.ring163 LibInt.intR).mul y β) = (Ring163.ring163 LibInt.intR).one) (h : Dvd' α ((Ring163.ring163 LibInt.intR).mul β γ)) : Dvd' α γ :=
  match h with
  | ⟨c, hc⟩ => ⟨(Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x γ) ((Ring163.ring163 LibInt.intR).mul y c), by
      calc γ = (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x α) ((Ring163.ring163 LibInt.intR).mul y β)) γ := by rw [h1]; lib_ring
        _ = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul x α) γ) ((Ring163.ring163 LibInt.intR).mul y ((Ring163.ring163 LibInt.intR).mul β γ)) := by lib_ring
        _ = (Ring163.ring163 LibInt.intR).mul α ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x γ) ((Ring163.ring163 LibInt.intR).mul y c)) := by rw [hc]; lib_ring⟩

theorem nrm_lt (z : Int × Int) (N : Nat) (h : Int.natAbs (2 * z.1 + z.2) * Int.natAbs (2 * z.1 + z.2) + 163 * (Int.natAbs z.2 * Int.natAbs z.2) < 4 * (N * N)) :
    nrm z < N * N := by
  cases Nat.lt_or_ge (nrm z) (N * N) with
  | inl h' => exact h'
  | inr h' =>
    exfalso
    rw [← four_nrm] at h
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_left 4 h'))

theorem sq_le (T N : Nat) (h : T ≤ N) : T * T ≤ N * N := Nat.mul_le_mul h h


end Euc163

#print axioms Euc163.sgn
#print axioms Euc163.le_dest
#print axioms Euc163.ab_le
#print axioms Euc163.en_rep
#print axioms Euc163.nrm_cast
#print axioms Euc163.four_nrm
#print axioms Euc163.mul_cj
#print axioms Euc163.en_mul
#print axioms Euc163.en_cj
#print axioms Euc163.nrm_zero
#print axioms Euc163.egcd_base
#print axioms Euc163.nrm_lt
#print axioms Euc163.sq_le
#print axioms Euc163.eeuclid

#print axioms: 14 theorems, no axioms.

78.319 A Dedekind–Hasse step for \(d=163\)

Multipliers \(1,\dots ,7\) by a case tree on \(B/N\); exact points \(m/k\) rescued by a table.

\[ \exists s,q:\ N(s\alpha -q\beta )<N(\beta ),\ s\alpha =q\beta \Rightarrow s=1. \]

lang/DH163.lean

/-
  lang/DH163 — T48/T50: **the Dedekind–Hasse step in ℤ[(1 + √−163)/2]**.  From a two-step remainder r (|r₂| = B, 2B ≤ N) a
  multiplier k ∈ {1, …, 7} with |kB − mN| small is found by a case tree on B/N (generated; each split |kB − mN|² · 163 < 3N² or a
  new linear bound on B/N, leaves closed by `contra`), and the exact points B/N = m/k are rescued by a table (resc_gen: if
  k r = (d₁, ±m) N then N(s r − e N) · k² = N² · N(s (d₁, ±m) − k e)).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat; concrete norms by rfl), no axioms.
-/
import lang.Euc163
import lang.DH67

namespace Euc163

open FTAChain DetChain LibInt

theorem nrm_mulD (x y : Int × Int) : nrm ((Ring163.ring163 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by rw [nrm_cast, en_mul, ← nrm_cast x, ← nrm_cast y]; rfl)

theorem nrm_scal (k : Nat) : nrm (Int.ofNat k, 0) = k * k := Int.ofNat.inj (by
  rw [nrm_cast]; show Int.ofNat k * Int.ofNat k + Int.ofNat k * 0 + 41 * (0 * 0) = Int.ofNat k * Int.ofNat k; lib_int)

/-- Natural residues by induction: n = K q + c with c < K. -/
theorem natRes (K : Nat) (hK : 0 < K) : ∀ n : Nat, ∃ q c : Nat, c < K ∧ n = K * q + c
  | 0 => ⟨0, 0, hK, by lib_nat⟩
  | n + 1 => by
    obtain ⟨q, c, hc, hn⟩ := natRes K hK n
    cases Nat.lt_or_ge (c + 1) K with
    | inl h => exact ⟨q, c + 1, h, by rw [hn]; lib_nat⟩
    | inr h =>
      have e : c + 1 = K := Nat.le_antisymm hc h
      exact ⟨q + 1, 0, hK, by rw [hn, show K * q + c + 1 = K * q + (c + 1) by lib_nat, e]; lib_nat⟩

/-- Residues: x = K q + c with 0 ≤ c < K. -/
theorem resN (K : Nat) (hK : 0 < K) (x : Int) : ∃ q : Int, ∃ c : Nat, c < K ∧ x = Int.ofNat K * q + Int.ofNat c := by
  cases x with
  | ofNat n =>
    obtain ⟨q, c, hc, hn⟩ := natRes K hK n
    exact ⟨Int.ofNat q, c, hc, by rw [hn]; rfl⟩
  | negSucc n =>
    obtain ⟨q, r, hr, hn⟩ := natRes K hK n
    obtain ⟨c, hc⟩ := EisEuclid.le_dest _ _ hr
    have hn' : Int.ofNat n = Int.ofNat K * Int.ofNat q + Int.ofNat r := congrArg Int.ofNat hn
    have hc' : Int.ofNat c + (Int.ofNat r + 1) = Int.ofNat K := congrArg Int.ofNat hc
    refine ⟨-Int.ofNat q + -1, c, Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_add_right c _)) (Nat.le_of_eq hc), ?_⟩
    show -(Int.ofNat n + 1) = _
    rw [hn', ← hc']; lib_int

/-- The exact points: k r = (d₁, e₂) N and N(s (d₁, e₂) − k e) = n ∈ (0, k²) give a remainder of norm < N², nonzero. -/
theorem resc_gen (K N0 : Nat) (r1 r2 d1 e2 : Int) (h1 : Int.ofNat K * r1 = d1 * Int.ofNat (N0 + 1)) (h2 : Int.ofNat K * r2 = e2 * Int.ofNat (N0 + 1))
    (s e : Int × Int) (n : Nat) (hn : nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, e2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) e))) = n)
    (hn0 : 0 < n) (hnk : n < K * K) :
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) ≠ (Ring163.ring163 LibInt.intR).zero := by
  have hKr : (Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) (r1, r2) = (Ring163.ring163 LibInt.intR).mul (Int.ofNat (N0 + 1), 0) (d1, e2) := by
    apply Prod.ext
    · show Int.ofNat K * r1 + -(0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2)   -- (+397 chars; full line in the Zenodo source)
      rw [h1]; lib_int
    · show Int.ofNat K * r2 + 0 * r1 + 0 * r2 = Int.ofNat (N0 + 1) * e2 + 0 * d1 + 0 * e2
      rw [h2]; lib_int
  have hid : (Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) =
      (Ring163.ring163 LibInt.intR).mul (Int.ofNat (N0 + 1), 0) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, e2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) e))) := by
    calc _ = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) (r1, r2))) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (N0 + 1), 0) ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) e))) := by lib_ring
      _ = _ := by rw [hKr]; lib_ring
  have hnn := congrArg nrm hid
  rw [nrm_mulD, nrm_mulD, nrm_scal, nrm_scal, hn] at hnn
  have hM : 0 < (N0 + 1) * (N0 + 1) := Nat.mul_pos (Nat.zero_lt_succ N0) (Nat.zero_lt_succ N0)
  refine ⟨?_, fun h0 => ?_⟩
  · cases Nat.lt_or_ge (nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))))) ((N0 + 1) * (N0 + 1)) with
    | inl h => exact h
    | inr h =>
      exfalso
      have a1 : K * K * ((N0 + 1) * (N0 + 1)) ≤ (N0 + 1) * (N0 + 1) * n := Nat.le_trans (Nat.mul_le_mul_left _ h) (Nat.le_of_eq hnn)
      have a2 : (N0 + 1) * (N0 + 1) * n < (N0 + 1) * (N0 + 1) * (K * K) := Nat.mul_lt_mul_of_pos_left hnk hM
      exact Nat.lt_irrefl _ (Nat.lt_of_le_of_lt a1 (Nat.lt_of_lt_of_le a2 (Nat.le_of_eq (by lib_nat))))
  · rw [h0] at hnn
    have z : (N0 + 1) * (N0 + 1) * n = 0 := hnn.symm.trans (Nat.mul_zero _)
    cases nat_mul_zero _ _ z with
    | inl e => exact Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le hM (Nat.le_of_eq e))
    | inr e => exact Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le hn0 (Nat.le_of_eq e))

theorem shift (K : Nat) (q c e2 : Int) (s e0 : Int × Int) :
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (Int.ofNat K * q + c, e2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q, 0)) e0)))) =
      nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (c, e2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) e0))) := by
  have hp : (Int.ofNat K * q + c, e2) = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Int.ofNat K, 0) (q, 0)) (c, e2) := by
    apply Prod.ext
    · show Int.ofNat K * q + c = Int.ofNat K * q + -(0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0) + c
      lib_int
    · show e2 = Int.ofNat K * 0 + 0 * q + 0 * 0 + e2
      lib_int
  rw [hp]
  exact congrArg nrm (by lib_ring)

/-- One branch: multiplier k + 1 with |(k+1) r₂ − d₂N| = w, 163w² < 3N² (d₂ = ±m; exact points rescued by the table). -/
theorem branchG (N0 : Nat) (r1 r2 d2 : Int) (k : Nat)
    (w : Nat) (hw : Int.natAbs (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = w) (hb : 163 * (w * w) < 3 * ((N0 + 1) * (N0 + 1)))
    (resc : ∀ d1 : Int, ∃ s e : Int × Int, ∃ n : Nat, nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (k + 1), 0) e))) = n ∧ 0 < n ∧ n < (k + 1) * (k + 1)) :
    ∃ s e : Int × Int, nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring163.ring163 LibInt.intR).zero → s = (Ring163.ring163 LibInt.intR).one) := by
  have e2N : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
  obtain ⟨d1, t', hrd, hb1⟩ := GaussEuclid.rnd (Int.ofNat (k + 1) * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1))) (2 * N0 + 1)
  have hrd' : 2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = t' := by
    calc 2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = Int.ofNat (k + 1) * (2 * r1 + r2) + -(d2 * Int.ofNat (N0 + 1)) + -(d1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
      _ = t' := by rw [hrd, e2N]; lib_int
  have hT' : Int.natAbs t' ≤ N0 + 1 := Nat.le_of_mul_le_mul_left (Nat.le_trans hb1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
  have hexpr : (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (k + 1), 0) (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (d1, d2) (Int.ofNat (N0 + 1), 0))) = (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)), Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) := by
    apply Prod.ext
    · show Int.ofNat (k + 1) * r1 + -(0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0 * r2 + 0   -- (+469 chars; full line in the Zenodo source)
    · show Int.ofNat (k + 1) * r2 + 0 * r1 + 0 * r2 + -(d1 * 0 + d2 * Int.ofNat (N0 + 1) + d2 * 0) = Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)); lib_int
  by_cases hz : (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)), Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) = (Ring163.ring163 LibInt.intR).zero
  · have z1 : Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.fst hz
    have z2 : Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)) = 0 := congrArg Prod.snd hz
    have h1 : Int.ofNat (k + 1) * r1 = d1 * Int.ofNat (N0 + 1) := by
      rw [show Int.ofNat (k + 1) * r1 = Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1)) + d1 * Int.ofNat (N0 + 1) by lib_int, z1]; lib_int
    have h2' : Int.ofNat (k + 1) * r2 = d2 * Int.ofNat (N0 + 1) := by
      rw [show Int.ofNat (k + 1) * r2 = Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)) + d2 * Int.ofNat (N0 + 1) by lib_int, z2]; lib_int
    obtain ⟨sv, ev, n, hn, hn0, hnk⟩ := resc d1
    obtain ⟨hlt, hne⟩ := resc_gen (k + 1) N0 r1 r2 d1 d2 h1 h2' sv ev n hn hn0 hnk
    exact ⟨sv, ev, hlt, fun h0 => absurd h0 hne⟩
  · refine ⟨(Int.ofNat (k + 1), 0), (d1, d2), ?_, fun h0 => absurd (hexpr.symm.trans h0) hz⟩
    rw [hexpr]
    apply nrm_lt
    show Int.natAbs (2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)))) * Int.natAbs (2 * (Int.ofNat (k + 1) * r1 + -(d1 * Int.ofNat (N0 + 1))) + (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))))
      + 163 * (Int.natAbs (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1))) * Int.natAbs (Int.ofNat (k + 1) * r2 + -(d2 * Int.ofNat (N0 + 1)))) < _
    rw [hrd', hw]
    calc Int.natAbs t' * Int.natAbs t' + 163 * (w * w) < (N0 + 1) * (N0 + 1) + 3 * ((N0 + 1) * (N0 + 1)) := Nat.add_lt_add_of_le_of_lt (Nat.mul_le_mul hT' hT') hb
      _ = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat


theorem lb_from (k m p q X B N : Nat) (hX : X + m * N = k * B) (h : q * N < p * X) : (p * m + q) * N < (p * k) * B := by
  have e : (p * k) * B = p * X + p * m * N := by rw [show (p * k) * B = p * (k * B) by lib_nat, ← hX]; lib_nat
  rw [e, show (p * m + q) * N = q * N + p * m * N by lib_nat]
  exact Nat.add_lt_add_right h _

theorem ub_from (k m p q X B N C : Nat) (hX : X + k * B = m * N) (h : q * N < p * X) (hC : C + q = p * m) : (p * k) * B < C * N := by
  apply Euc67.lt_cancel _ _ (q * N)
  calc (p * k) * B + q * N < (p * k) * B + p * X := Nat.add_lt_add_left h _
    _ = p * (m * N) := by rw [← hX]; lib_nat
    _ = C * N + q * N := by rw [show p * (m * N) = (p * m) * N by lib_nat, ← hC]; lib_nat

theorem contra (a b c d N B : Nat) (h1 : a * N < b * B) (h2 : c * B ≤ d * N) (hc : 0 < c) (h : b * d ≤ c * a) : False := by
  have s1 : c * (a * N) < c * (b * B) := Nat.mul_lt_mul_of_pos_left h1 hc
  have s2 : b * (c * B) ≤ b * (d * N) := Nat.mul_le_mul_left b h2
  have s3 : (b * d) * N ≤ (c * a) * N := Nat.mul_le_mul_right N h
  have : c * (a * N) < c * (a * N) :=
    Nat.lt_of_lt_of_le s1 (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s2 (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s3 (Nat.le_of_eq (by lib_nat))))))
  exact Nat.lt_irrefl _ this

theorem wpos (k m X B N0 : Nat) (d2 : Int) (hX : X + m * (N0 + 1) = k * B) :
    Int.ofNat k * (d2 * Int.ofNat B) + -(d2 * Int.ofNat m * Int.ofNat (N0 + 1)) = d2 * Int.ofNat X := by
  have h : Int.ofNat X + Int.ofNat m * Int.ofNat (N0 + 1) = Int.ofNat k * Int.ofNat B := congrArg Int.ofNat hX
  calc Int.ofNat k * (d2 * Int.ofNat B) + -(d2 * Int.ofNat m * Int.ofNat (N0 + 1)) = d2 * (Int.ofNat k * Int.ofNat B) + -(d2 * (Int.ofNat m * Int.ofNat (N0 + 1))) := by lib_int
    _ = d2 * Int.ofNat X := by rw [← h]; lib_int

theorem wneg (k m X B N0 : Nat) (d2 : Int) (hX : X + k * B = m * (N0 + 1)) :
    Int.ofNat k * (d2 * Int.ofNat B) + -(d2 * Int.ofNat m * Int.ofNat (N0 + 1)) = -(d2 * Int.ofNat X) := by
  have h : Int.ofNat X + Int.ofNat k * Int.ofNat B = Int.ofNat m * Int.ofNat (N0 + 1) := congrArg Int.ofNat hX
  calc Int.ofNat k * (d2 * Int.ofNat B) + -(d2 * Int.ofNat m * Int.ofNat (N0 + 1)) = d2 * (Int.ofNat k * Int.ofNat B) + -(d2 * (Int.ofNat m * Int.ofNat (N0 + 1))) := by lib_int
    _ = -(d2 * Int.ofNat X) := by rw [← h]; lib_int

theorem rs2_1 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 1)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (1 + 1), 0) e))) = n ∧ 0 < n ∧ n < (1 + 1) * (1 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (1 + 1) (Nat.zero_lt_succ 1) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) ((-21), 0), 1, (shift (1 + 1) q (Int.ofNat 0) (1 * Int.ofNat 1) ((-1), 1) ((-21), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (20, (-1)), 1, (shift (1 + 1) q (Int.ofNat 1) (1 * Int.ofNat 1) (0, (-1)) (20, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 2, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) (20, 0), 1, (shift (1 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 1) ((-1), 1) (20, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-21), 0), 1, (shift (1 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 1) (0, (-1)) ((-21), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 2, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs3_1 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 1)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (2 + 1), 0) e))) = n ∧ 0 < n ∧ n < (2 + 1) * (2 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (2 + 1) (Nat.zero_lt_succ 2) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) ((-14), 0), 1, (shift (2 + 1) q (Int.ofNat 0) (1 * Int.ofNat 1) ((-1), 1) ((-14), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) (13, (-1)), 1, (shift (2 + 1) q (Int.ofNat 1) (1 * Int.ofNat 1) ((-1), (-1)) (13, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (14, (-1)), 1, (shift (2 + 1) q (Int.ofNat 2) (1 * Int.ofNat 1) (0, (-1)) (14, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 3, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) (14, 0), 1, (shift (2 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 1) ((-1), 1) (14, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-14), 0), 1, (shift (2 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 1) (0, (-1)) ((-14), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) ((-14), 0), 1, (shift (2 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 1) ((-1), (-1)) ((-14), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 3, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs5_2 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (4 + 1), 0) e))) = n ∧ 0 < n ∧ n < (4 + 1) * (4 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (4 + 1) (Nat.zero_lt_succ 4) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) ((-16), 0), 4, (shift (4 + 1) q (Int.ofNat 0) (1 * Int.ofNat 2) ((-1), 1) ((-16), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) (16, (-1)), 1, (shift (4 + 1) q (Int.ofNat 1) (1 * Int.ofNat 2) ((-1), (-1)) (16, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) ((-17), 0), 1, (shift (4 + 1) q (Int.ofNat 2) (1 * Int.ofNat 2) ((-2), 1) ((-17), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (16, (-1)), 4, (shift (4 + 1) q (Int.ofNat 3) (1 * Int.ofNat 2) (0, (-1)) (16, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) (15, (-2)), 1, (shift (4 + 1) q (Int.ofNat 4) (1 * Int.ofNat 2) ((-2), (-1)) (15, (-2))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 5, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) (16, 0), 4, (shift (4 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 2) ((-1), 1) (16, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) ((-17), 1), 1, (shift (4 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 2) ((-2), (-1)) ((-17), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-16), 0), 4, (shift (4 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 2) (0, (-1)) ((-16), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) (15, 1), 1, (shift (4 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 2) ((-2), 1) (15, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) ((-17), 0), 1, (shift (4 + 1) q (Int.ofNat 4) (-1 * Int.ofNat 2) ((-1), (-1)) ((-17), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 5, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs7_3 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 3)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (6 + 1), 0) e))) = n ∧ 0 < n ∧ n < (6 + 1) * (6 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (6 + 1) (Nat.zero_lt_succ 6) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) (0, (-1)), 41, (shift (6 + 1) q (Int.ofNat 0) (1 * Int.ofNat 3) ((-2), 0) (0, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) (0, (-1)), 43, (shift (6 + 1) q (Int.ofNat 1) (1 * Int.ofNat 3) ((-2), 0) (0, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) (17, (-1)), 43, (shift (6 + 1) q (Int.ofNat 2) (1 * Int.ofNat 3) ((-1), (-1)) (17, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-1), (-1)), 43, (shift (6 + 1) q (Int.ofNat 3) (1 * Int.ofNat 3) ((-2), 0) ((-1), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-1), (-1)), 41, (shift (6 + 1) q (Int.ofNat 4) (1 * Int.ofNat 3) ((-2), 0) ((-1), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-1), (-1)), 47, (shift (6 + 1) q (Int.ofNat 5) (1 * Int.ofNat 3) ((-2), 0) ((-1), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 6, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-2), (-1)), 47, (shift (6 + 1) q (Int.ofNat 6) (1 * Int.ofNat 3) ((-2), 0) ((-2), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 7, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) (0, 1), 41, (shift (6 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 3) ((-2), 0) (0, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) (0, 1), 47, (shift (6 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 3) ((-2), 0) (0, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-1), 1), 47, (shift (6 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 3) ((-2), 0) ((-1), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-1), 1), 41, (shift (6 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 3) ((-2), 0) ((-1), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-1), 1), 43, (shift (6 + 1) q (Int.ofNat 4) (-1 * Int.ofNat 3) ((-2), 0) ((-1), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) ((-18), 0), 43, (shift (6 + 1) q (Int.ofNat 5) (-1 * Int.ofNat 3) ((-1), (-1)) ((-18), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 6, _ => exact ⟨((-2), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 0) (q, 0)) ((-2), 1), 43, (shift (6 + 1) q (Int.ofNat 6) (-1 * Int.ofNat 3) ((-2), 0) ((-2), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 7, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs4_1 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 1)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (3 + 1), 0) e))) = n ∧ 0 < n ∧ n < (3 + 1) * (3 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (3 + 1) (Nat.zero_lt_succ 3) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) ((-10), 0), 1, (shift (3 + 1) q (Int.ofNat 0) (1 * Int.ofNat 1) ((-1), 1) ((-10), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) ((-11), 0), 1, (shift (3 + 1) q (Int.ofNat 1) (1 * Int.ofNat 1) ((-2), 1) ((-11), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) (10, (-1)), 1, (shift (3 + 1) q (Int.ofNat 2) (1 * Int.ofNat 1) ((-1), (-1)) (10, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (10, (-1)), 1, (shift (3 + 1) q (Int.ofNat 3) (1 * Int.ofNat 1) (0, (-1)) (10, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 4, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) (10, 0), 1, (shift (3 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 1) ((-1), 1) (10, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-10), 0), 1, (shift (3 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 1) (0, (-1)) ((-10), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) ((-11), 0), 1, (shift (3 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 1) ((-1), (-1)) ((-11), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) (9, 1), 1, (shift (3 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 1) ((-2), 1) (9, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 4, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs7_2 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (6 + 1), 0) e))) = n ∧ 0 < n ∧ n < (6 + 1) * (6 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (6 + 1) (Nat.zero_lt_succ 6) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) (0, (-1)), 41, (shift (6 + 1) q (Int.ofNat 0) (1 * Int.ofNat 2) ((-3), 0) (0, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) (0, (-1)), 47, (shift (6 + 1) q (Int.ofNat 1) (1 * Int.ofNat 2) ((-3), 0) (0, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-1), (-1)), 43, (shift (6 + 1) q (Int.ofNat 2) (1 * Int.ofNat 2) ((-3), 0) ((-1), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-1), (-1)), 43, (shift (6 + 1) q (Int.ofNat 3) (1 * Int.ofNat 2) ((-3), 0) ((-1), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-2), (-1)), 47, (shift (6 + 1) q (Int.ofNat 4) (1 * Int.ofNat 2) ((-3), 0) ((-2), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-2), (-1)), 41, (shift (6 + 1) q (Int.ofNat 5) (1 * Int.ofNat 2) ((-3), 0) ((-2), (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 6, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (12, (-1)), 47, (shift (6 + 1) q (Int.ofNat 6) (1 * Int.ofNat 2) (0, (-1)) (12, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 7, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) (0, 1), 41, (shift (6 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 2) ((-3), 0) (0, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-12), 0), 47, (shift (6 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 2) (0, (-1)) ((-12), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-1), 1), 41, (shift (6 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 2) ((-3), 0) ((-1), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-1), 1), 47, (shift (6 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 2) ((-3), 0) ((-1), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-2), 1), 43, (shift (6 + 1) q (Int.ofNat 4) (-1 * Int.ofNat 2) ((-3), 0) ((-2), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-2), 1), 43, (shift (6 + 1) q (Int.ofNat 5) (-1 * Int.ofNat 2) ((-3), 0) ((-2), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 6, _ => exact ⟨((-3), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 0) (q, 0)) ((-3), 1), 47, (shift (6 + 1) q (Int.ofNat 6) (-1 * Int.ofNat 2) ((-3), 0) ((-3), 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 7, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs5_1 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 1)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (4 + 1), 0) e))) = n ∧ 0 < n ∧ n < (4 + 1) * (4 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (4 + 1) (Nat.zero_lt_succ 4) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) ((-8), 0), 1, (shift (4 + 1) q (Int.ofNat 0) (1 * Int.ofNat 1) ((-1), 1) ((-8), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) ((-9), 0), 4, (shift (4 + 1) q (Int.ofNat 1) (1 * Int.ofNat 1) ((-2), 1) ((-9), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) (7, (-1)), 4, (shift (4 + 1) q (Int.ofNat 2) (1 * Int.ofNat 1) ((-2), (-1)) (7, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) (8, (-1)), 4, (shift (4 + 1) q (Int.ofNat 3) (1 * Int.ofNat 1) ((-1), (-1)) (8, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (8, (-1)), 1, (shift (4 + 1) q (Int.ofNat 4) (1 * Int.ofNat 1) (0, (-1)) (8, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 5, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) (8, 0), 1, (shift (4 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 1) ((-1), 1) (8, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-8), 0), 1, (shift (4 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 1) (0, (-1)) ((-8), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) ((-9), 0), 4, (shift (4 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 1) ((-1), (-1)) ((-9), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) ((-9), 0), 4, (shift (4 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 1) ((-2), (-1)) ((-9), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) (7, 1), 4, (shift (4 + 1) q (Int.ofNat 4) (-1 * Int.ofNat 1) ((-2), 1) (7, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 5, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs6_1 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 1)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (5 + 1), 0) e))) = n ∧ 0 < n ∧ n < (5 + 1) * (5 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (5 + 1) (Nat.zero_lt_succ 5) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) ((-7), 0), 1, (shift (5 + 1) q (Int.ofNat 0) (1 * Int.ofNat 1) ((-1), 1) ((-7), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) ((-7), 0), 1, (shift (5 + 1) q (Int.ofNat 1) (1 * Int.ofNat 1) ((-2), 1) ((-7), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-3), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 1) (q, 0)) ((-8), 0), 1, (shift (5 + 1) q (Int.ofNat 2) (1 * Int.ofNat 1) ((-3), 1) ((-8), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) (6, (-1)), 1, (shift (5 + 1) q (Int.ofNat 3) (1 * Int.ofNat 1) ((-2), (-1)) (6, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) (6, (-1)), 1, (shift (5 + 1) q (Int.ofNat 4) (1 * Int.ofNat 1) ((-1), (-1)) (6, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) (7, (-1)), 1, (shift (5 + 1) q (Int.ofNat 5) (1 * Int.ofNat 1) (0, (-1)) (7, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 6, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 1) (q, 0)) (7, 0), 1, (shift (5 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 1) ((-1), 1) (7, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨(0, (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (0, (-1)) (q, 0)) ((-7), 0), 1, (shift (5 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 1) (0, (-1)) ((-7), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), (-1)) (q, 0)) ((-7), 0), 1, (shift (5 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 1) ((-1), (-1)) ((-7), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) ((-8), 0), 1, (shift (5 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 1) ((-2), (-1)) ((-8), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-3), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-3), 1) (q, 0)) (5, 1), 1, (shift (5 + 1) q (Int.ofNat 4) (-1 * Int.ofNat 1) ((-3), 1) (5, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-2), 1), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), 1) (q, 0)) (5, 1), 1, (shift (5 + 1) q (Int.ofNat 5) (-1 * Int.ofNat 1) ((-2), 1) (5, 1)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 6, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

theorem rs7_1 (d2 : Int) (hd2 : d2 = 1 ∨ d2 = -1) (d1 : Int) : ∃ s e : Int × Int, ∃ n : Nat,
    nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (d1, d2 * Int.ofNat 1)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Int.ofNat (6 + 1), 0) e))) = n ∧ 0 < n ∧ n < (6 + 1) * (6 + 1) := by
  obtain ⟨q, c, hc, hd⟩ := resN (6 + 1) (Nat.zero_lt_succ 6) d1
  rw [hd]
  rcases hd2 with e | e <;> rw [e]
  · match c, hc with
    | 0, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 41, (shift (6 + 1) q (Int.ofNat 0) (1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 43, (shift (6 + 1) q (Int.ofNat 1) (1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 47, (shift (6 + 1) q (Int.ofNat 2) (1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) (5, (-1)), 41, (shift (6 + 1) q (Int.ofNat 3) (1 * Int.ofNat 1) ((-2), (-1)) (5, (-1))).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) ((-1), 0), 47, (shift (6 + 1) q (Int.ofNat 4) (1 * Int.ofNat 1) ((-1), 0) ((-1), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) ((-1), 0), 43, (shift (6 + 1) q (Int.ofNat 5) (1 * Int.ofNat 1) ((-1), 0) ((-1), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 6, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) ((-1), 0), 41, (shift (6 + 1) q (Int.ofNat 6) (1 * Int.ofNat 1) ((-1), 0) ((-1), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 7, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))
  · match c, hc with
    | 0, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 41, (shift (6 + 1) q (Int.ofNat 0) (-1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 1, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 41, (shift (6 + 1) q (Int.ofNat 1) (-1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 2, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 43, (shift (6 + 1) q (Int.ofNat 2) (-1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 3, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) (0, 0), 47, (shift (6 + 1) q (Int.ofNat 3) (-1 * Int.ofNat 1) ((-1), 0) (0, 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 4, _ => exact ⟨((-2), (-1)), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-2), (-1)) (q, 0)) ((-7), 0), 41, (shift (6 + 1) q (Int.ofNat 4) (-1 * Int.ofNat 1) ((-2), (-1)) ((-7), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 5, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) ((-1), 0), 47, (shift (6 + 1) q (Int.ofNat 5) (-1 * Int.ofNat 1) ((-1), 0) ((-1), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | 6, _ => exact ⟨((-1), 0), (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((-1), 0) (q, 0)) ((-1), 0), 43, (shift (6 + 1) q (Int.ofNat 6) (-1 * Int.ofNat 1) ((-1), 0) ((-1), 0)).trans rfl, (Nat.le_of_ble_eq_true rfl), (Nat.le_of_ble_eq_true rfl)⟩
    | _ + 7, h => exact absurd h (Nat.not_lt_of_ge (Nat.le_add_left _ _))

set_option maxHeartbeats 0 in
/-- **The Dedekind–Hasse step on a remainder (d = 163).** -/
theorem dh163_r (N0 : Nat) (r1 r2 : Int) (h2 : 2 * Int.natAbs r2 ≤ N0 + 1) (ht : Int.natAbs (2 * r1 + r2) ≤ N0 + 1) :
    ∃ s e : Int × Int, nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) < (N0 + 1) * (N0 + 1) ∧
      ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) = (Ring163.ring163 LibInt.intR).zero → s = (Ring163.ring163 LibInt.intR).one) := by
  have h4 := four_nrm (r1, r2)
  generalize hT : Int.natAbs (2 * r1 + r2) = T at h4 ht
  generalize hB : Int.natAbs r2 = B at h4 h2
  have h4' : 4 * nrm (r1, r2) = T * T + 163 * (B * B) := h4
  cases Nat.lt_or_ge (T * T + 163 * (B * B)) (4 * ((N0 + 1) * (N0 + 1))) with
  | inl hl =>
    refine ⟨(Ring163.ring163 LibInt.intR).one, (Ring163.ring163 LibInt.intR).zero, ?_, fun _ => rfl⟩
    have e : (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).one (r1, r2)) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).zero (Int.ofNat (N0 + 1), 0))) = (r1, r2) := by lib_ring
    rw [e]
    exact nrm_lt (r1, r2) (N0 + 1) (by rw [hT, hB]; exact hl)
  | inr hg =>
    have h3 : 3 * ((N0 + 1) * (N0 + 1)) ≤ 163 * (B * B) := GaussEuclid.le_cancel _ _ ((N0 + 1) * (N0 + 1)) (by
      calc 3 * ((N0 + 1) * (N0 + 1)) + (N0 + 1) * (N0 + 1) = 4 * ((N0 + 1) * (N0 + 1)) := by lib_nat
        _ ≤ T * T + 163 * (B * B) := hg
        _ ≤ (N0 + 1) * (N0 + 1) + 163 * (B * B) := Nat.add_le_add_right (Nat.mul_le_mul ht ht) _
        _ = 163 * (B * B) + (N0 + 1) * (N0 + 1) := by lib_nat)
    have lb0 : 5 * (N0 + 1) < 37 * B := Euc67.lin_lb 163 3 37 5 B (N0 + 1) h3 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)
    have ub0 : 2 * B ≤ 1 * (N0 + 1) := Nat.le_trans h2 (Nat.le_of_eq (by lib_nat))
    have hsg : ∃ d2 : Int, (d2 = 1 ∨ d2 = -1) ∧ r2 = d2 * Int.ofNat B := by
      rcases EisEuclid.sgn r2 with e | e
      · exact ⟨1, Or.inl rfl, by rw [e, hB]; lib_int⟩
      · exact ⟨-1, Or.inr rfl, by rw [e, hB]; lib_int⟩
    obtain ⟨d2, hd2, hr2⟩ := hsg
    cases Nat.le_total (1 * (N0 + 1)) (2 * B) with
    | inl hge1 =>
      obtain ⟨X1, hX1⟩ := EisEuclid.le_dest _ _ hge1
      have hw1 : Int.natAbs (Int.ofNat (1 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = X1 := Euc67.absw d2 hd2 _ X1 (Or.inl (by rw [hr2]; exact wpos (1 + 1) 1 X1 B N0 d2 hX1))
      cases Nat.lt_or_ge (163 * (X1 * X1)) (3 * ((N0 + 1) * (N0 + 1))) with
      | inl hb1 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 1 X1 hw1 hb1 (rs2_1 d2 hd2)
      | inr hc1 =>
        have lo1 : (37 * 1 + 5) * (N0 + 1) < (37 * 2) * B := lb_from 2 1 37 5 X1 B (N0 + 1) hX1 (Euc67.lin_lb 163 3 37 5 X1 (N0 + 1) hc1 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
        exact (contra (37 * 1 + 5) (37 * 2) 2 1 (N0 + 1) B lo1 ub0 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
    | inr hle1 =>
      obtain ⟨Y1, hY1⟩ := EisEuclid.le_dest _ _ hle1
      have hv1 : Int.natAbs (Int.ofNat (1 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = Y1 := Euc67.absw d2 hd2 _ Y1 (Or.inr (by rw [hr2]; exact wneg (1 + 1) 1 Y1 B N0 d2 hY1))
      cases Nat.lt_or_ge (163 * (Y1 * Y1)) (3 * ((N0 + 1) * (N0 + 1))) with
      | inl hf1 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 1 Y1 hv1 hf1 (rs2_1 d2 hd2)
      | inr he1 =>
        have up1 : (37 * 2) * B ≤ 32 * (N0 + 1) := Nat.le_of_lt (ub_from 2 1 37 5 Y1 B (N0 + 1) 32 hY1 (Euc67.lin_lb 163 3 37 5 Y1 (N0 + 1) he1 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
        cases Nat.le_total (1 * (N0 + 1)) (3 * B) with
        | inl hge2 =>
          obtain ⟨X2, hX2⟩ := EisEuclid.le_dest _ _ hge2
          have hw2 : Int.natAbs (Int.ofNat (2 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = X2 := Euc67.absw d2 hd2 _ X2 (Or.inl (by rw [hr2]; exact wpos (2 + 1) 1 X2 B N0 d2 hX2))
          cases Nat.lt_or_ge (163 * (X2 * X2)) (3 * ((N0 + 1) * (N0 + 1))) with
          | inl hb2 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 2 X2 hw2 hb2 (rs3_1 d2 hd2)
          | inr hc2 =>
            have lo2 : (37 * 1 + 5) * (N0 + 1) < (37 * 3) * B := lb_from 3 1 37 5 X2 B (N0 + 1) hX2 (Euc67.lin_lb 163 3 37 5 X2 (N0 + 1) hc2 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
            cases Nat.le_total (2 * (N0 + 1)) (5 * B) with
            | inl hge3 =>
              obtain ⟨X3, hX3⟩ := EisEuclid.le_dest _ _ hge3
              have hw3 : Int.natAbs (Int.ofNat (4 + 1) * r2 + -(d2 * Int.ofNat 2 * Int.ofNat (N0 + 1))) = X3 := Euc67.absw d2 hd2 _ X3 (Or.inl (by rw [hr2]; exact wpos (4 + 1) 2 X3 B N0 d2 hX3))
              cases Nat.lt_or_ge (163 * (X3 * X3)) (3 * ((N0 + 1) * (N0 + 1))) with
              | inl hb3 => exact branchG N0 r1 r2 (d2 * Int.ofNat 2) 4 X3 hw3 hb3 (rs5_2 d2 hd2)
              | inr hc3 =>
                have lo3 : (37 * 2 + 5) * (N0 + 1) < (37 * 5) * B := lb_from 5 2 37 5 X3 B (N0 + 1) hX3 (Euc67.lin_lb 163 3 37 5 X3 (N0 + 1) hc3 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                cases Nat.le_total (3 * (N0 + 1)) (7 * B) with
                | inl hge4 =>
                  obtain ⟨X4, hX4⟩ := EisEuclid.le_dest _ _ hge4
                  have hw4 : Int.natAbs (Int.ofNat (6 + 1) * r2 + -(d2 * Int.ofNat 3 * Int.ofNat (N0 + 1))) = X4 := Euc67.absw d2 hd2 _ X4 (Or.inl (by rw [hr2]; exact wpos (6 + 1) 3 X4 B N0 d2 hX4))
                  cases Nat.lt_or_ge (163 * (X4 * X4)) (3 * ((N0 + 1) * (N0 + 1))) with
                  | inl hb4 => exact branchG N0 r1 r2 (d2 * Int.ofNat 3) 6 X4 hw4 hb4 (rs7_3 d2 hd2)
                  | inr hc4 =>
                    have lo4 : (37 * 3 + 5) * (N0 + 1) < (37 * 7) * B := lb_from 7 3 37 5 X4 B (N0 + 1) hX4 (Euc67.lin_lb 163 3 37 5 X4 (N0 + 1) hc4 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                    exact (contra (37 * 3 + 5) (37 * 7) (37 * 2) 32 (N0 + 1) B lo4 up1 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
                | inr hle4 =>
                  obtain ⟨Y4, hY4⟩ := EisEuclid.le_dest _ _ hle4
                  have hv4 : Int.natAbs (Int.ofNat (6 + 1) * r2 + -(d2 * Int.ofNat 3 * Int.ofNat (N0 + 1))) = Y4 := Euc67.absw d2 hd2 _ Y4 (Or.inr (by rw [hr2]; exact wneg (6 + 1) 3 Y4 B N0 d2 hY4))
                  cases Nat.lt_or_ge (163 * (Y4 * Y4)) (3 * ((N0 + 1) * (N0 + 1))) with
                  | inl hf4 => exact branchG N0 r1 r2 (d2 * Int.ofNat 3) 6 Y4 hv4 hf4 (rs7_3 d2 hd2)
                  | inr he4 =>
                    have up4 : (37 * 7) * B ≤ 106 * (N0 + 1) := Nat.le_of_lt (ub_from 7 3 37 5 Y4 B (N0 + 1) 106 hY4 (Euc67.lin_lb 163 3 37 5 Y4 (N0 + 1) he4 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                    exact (contra (37 * 2 + 5) (37 * 5) (37 * 7) 106 (N0 + 1) B lo3 up4 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
            | inr hle3 =>
              obtain ⟨Y3, hY3⟩ := EisEuclid.le_dest _ _ hle3
              have hv3 : Int.natAbs (Int.ofNat (4 + 1) * r2 + -(d2 * Int.ofNat 2 * Int.ofNat (N0 + 1))) = Y3 := Euc67.absw d2 hd2 _ Y3 (Or.inr (by rw [hr2]; exact wneg (4 + 1) 2 Y3 B N0 d2 hY3))
              cases Nat.lt_or_ge (163 * (Y3 * Y3)) (3 * ((N0 + 1) * (N0 + 1))) with
              | inl hf3 => exact branchG N0 r1 r2 (d2 * Int.ofNat 2) 4 Y3 hv3 hf3 (rs5_2 d2 hd2)
              | inr he3 =>
                have up3 : (37 * 5) * B ≤ 69 * (N0 + 1) := Nat.le_of_lt (ub_from 5 2 37 5 Y3 B (N0 + 1) 69 hY3 (Euc67.lin_lb 163 3 37 5 Y3 (N0 + 1) he3 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                exact (contra (37 * 1 + 5) (37 * 3) (37 * 5) 69 (N0 + 1) B lo2 up3 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
        | inr hle2 =>
          obtain ⟨Y2, hY2⟩ := EisEuclid.le_dest _ _ hle2
          have hv2 : Int.natAbs (Int.ofNat (2 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = Y2 := Euc67.absw d2 hd2 _ Y2 (Or.inr (by rw [hr2]; exact wneg (2 + 1) 1 Y2 B N0 d2 hY2))
          cases Nat.lt_or_ge (163 * (Y2 * Y2)) (3 * ((N0 + 1) * (N0 + 1))) with
          | inl hf2 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 2 Y2 hv2 hf2 (rs3_1 d2 hd2)
          | inr he2 =>
            have up2 : (37 * 3) * B ≤ 32 * (N0 + 1) := Nat.le_of_lt (ub_from 3 1 37 5 Y2 B (N0 + 1) 32 hY2 (Euc67.lin_lb 163 3 37 5 Y2 (N0 + 1) he2 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
            cases Nat.le_total (1 * (N0 + 1)) (4 * B) with
            | inl hge5 =>
              obtain ⟨X5, hX5⟩ := EisEuclid.le_dest _ _ hge5
              have hw5 : Int.natAbs (Int.ofNat (3 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = X5 := Euc67.absw d2 hd2 _ X5 (Or.inl (by rw [hr2]; exact wpos (3 + 1) 1 X5 B N0 d2 hX5))
              cases Nat.lt_or_ge (163 * (X5 * X5)) (3 * ((N0 + 1) * (N0 + 1))) with
              | inl hb5 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 3 X5 hw5 hb5 (rs4_1 d2 hd2)
              | inr hc5 =>
                have lo5 : (37 * 1 + 5) * (N0 + 1) < (37 * 4) * B := lb_from 4 1 37 5 X5 B (N0 + 1) hX5 (Euc67.lin_lb 163 3 37 5 X5 (N0 + 1) hc5 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                cases Nat.le_total (2 * (N0 + 1)) (7 * B) with
                | inl hge6 =>
                  obtain ⟨X6, hX6⟩ := EisEuclid.le_dest _ _ hge6
                  have hw6 : Int.natAbs (Int.ofNat (6 + 1) * r2 + -(d2 * Int.ofNat 2 * Int.ofNat (N0 + 1))) = X6 := Euc67.absw d2 hd2 _ X6 (Or.inl (by rw [hr2]; exact wpos (6 + 1) 2 X6 B N0 d2 hX6))
                  cases Nat.lt_or_ge (163 * (X6 * X6)) (3 * ((N0 + 1) * (N0 + 1))) with
                  | inl hb6 => exact branchG N0 r1 r2 (d2 * Int.ofNat 2) 6 X6 hw6 hb6 (rs7_2 d2 hd2)
                  | inr hc6 =>
                    have lo6 : (37 * 2 + 5) * (N0 + 1) < (37 * 7) * B := lb_from 7 2 37 5 X6 B (N0 + 1) hX6 (Euc67.lin_lb 163 3 37 5 X6 (N0 + 1) hc6 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                    exact (contra (37 * 2 + 5) (37 * 7) (37 * 3) 32 (N0 + 1) B lo6 up2 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
                | inr hle6 =>
                  obtain ⟨Y6, hY6⟩ := EisEuclid.le_dest _ _ hle6
                  have hv6 : Int.natAbs (Int.ofNat (6 + 1) * r2 + -(d2 * Int.ofNat 2 * Int.ofNat (N0 + 1))) = Y6 := Euc67.absw d2 hd2 _ Y6 (Or.inr (by rw [hr2]; exact wneg (6 + 1) 2 Y6 B N0 d2 hY6))
                  cases Nat.lt_or_ge (163 * (Y6 * Y6)) (3 * ((N0 + 1) * (N0 + 1))) with
                  | inl hf6 => exact branchG N0 r1 r2 (d2 * Int.ofNat 2) 6 Y6 hv6 hf6 (rs7_2 d2 hd2)
                  | inr he6 =>
                    have up6 : (37 * 7) * B ≤ 69 * (N0 + 1) := Nat.le_of_lt (ub_from 7 2 37 5 Y6 B (N0 + 1) 69 hY6 (Euc67.lin_lb 163 3 37 5 Y6 (N0 + 1) he6 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                    exact (contra (37 * 1 + 5) (37 * 4) (37 * 7) 69 (N0 + 1) B lo5 up6 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
            | inr hle5 =>
              obtain ⟨Y5, hY5⟩ := EisEuclid.le_dest _ _ hle5
              have hv5 : Int.natAbs (Int.ofNat (3 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = Y5 := Euc67.absw d2 hd2 _ Y5 (Or.inr (by rw [hr2]; exact wneg (3 + 1) 1 Y5 B N0 d2 hY5))
              cases Nat.lt_or_ge (163 * (Y5 * Y5)) (3 * ((N0 + 1) * (N0 + 1))) with
              | inl hf5 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 3 Y5 hv5 hf5 (rs4_1 d2 hd2)
              | inr he5 =>
                have up5 : (37 * 4) * B ≤ 32 * (N0 + 1) := Nat.le_of_lt (ub_from 4 1 37 5 Y5 B (N0 + 1) 32 hY5 (Euc67.lin_lb 163 3 37 5 Y5 (N0 + 1) he5 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                cases Nat.le_total (1 * (N0 + 1)) (5 * B) with
                | inl hge7 =>
                  obtain ⟨X7, hX7⟩ := EisEuclid.le_dest _ _ hge7
                  have hw7 : Int.natAbs (Int.ofNat (4 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = X7 := Euc67.absw d2 hd2 _ X7 (Or.inl (by rw [hr2]; exact wpos (4 + 1) 1 X7 B N0 d2 hX7))
                  cases Nat.lt_or_ge (163 * (X7 * X7)) (3 * ((N0 + 1) * (N0 + 1))) with
                  | inl hb7 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 4 X7 hw7 hb7 (rs5_1 d2 hd2)
                  | inr hc7 =>
                    have lo7 : (37 * 1 + 5) * (N0 + 1) < (37 * 5) * B := lb_from 5 1 37 5 X7 B (N0 + 1) hX7 (Euc67.lin_lb 163 3 37 5 X7 (N0 + 1) hc7 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                    exact (contra (37 * 1 + 5) (37 * 5) (37 * 4) 32 (N0 + 1) B lo7 up5 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
                | inr hle7 =>
                  obtain ⟨Y7, hY7⟩ := EisEuclid.le_dest _ _ hle7
                  have hv7 : Int.natAbs (Int.ofNat (4 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = Y7 := Euc67.absw d2 hd2 _ Y7 (Or.inr (by rw [hr2]; exact wneg (4 + 1) 1 Y7 B N0 d2 hY7))
                  cases Nat.lt_or_ge (163 * (Y7 * Y7)) (3 * ((N0 + 1) * (N0 + 1))) with
                  | inl hf7 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 4 Y7 hv7 hf7 (rs5_1 d2 hd2)
                  | inr he7 =>
                    have up7 : (37 * 5) * B ≤ 32 * (N0 + 1) := Nat.le_of_lt (ub_from 5 1 37 5 Y7 B (N0 + 1) 32 hY7 (Euc67.lin_lb 163 3 37 5 Y7 (N0 + 1) he7 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                    cases Nat.le_total (1 * (N0 + 1)) (6 * B) with
                    | inl hge8 =>
                      obtain ⟨X8, hX8⟩ := EisEuclid.le_dest _ _ hge8
                      have hw8 : Int.natAbs (Int.ofNat (5 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = X8 := Euc67.absw d2 hd2 _ X8 (Or.inl (by rw [hr2]; exact wpos (5 + 1) 1 X8 B N0 d2 hX8))
                      cases Nat.lt_or_ge (163 * (X8 * X8)) (3 * ((N0 + 1) * (N0 + 1))) with
                      | inl hb8 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 5 X8 hw8 hb8 (rs6_1 d2 hd2)
                      | inr hc8 =>
                        have lo8 : (37 * 1 + 5) * (N0 + 1) < (37 * 6) * B := lb_from 6 1 37 5 X8 B (N0 + 1) hX8 (Euc67.lin_lb 163 3 37 5 X8 (N0 + 1) hc8 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                        exact (contra (37 * 1 + 5) (37 * 6) (37 * 5) 32 (N0 + 1) B lo8 up7 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
                    | inr hle8 =>
                      obtain ⟨Y8, hY8⟩ := EisEuclid.le_dest _ _ hle8
                      have hv8 : Int.natAbs (Int.ofNat (5 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = Y8 := Euc67.absw d2 hd2 _ Y8 (Or.inr (by rw [hr2]; exact wneg (5 + 1) 1 Y8 B N0 d2 hY8))
                      cases Nat.lt_or_ge (163 * (Y8 * Y8)) (3 * ((N0 + 1) * (N0 + 1))) with
                      | inl hf8 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 5 Y8 hv8 hf8 (rs6_1 d2 hd2)
                      | inr he8 =>
                        have up8 : (37 * 6) * B ≤ 32 * (N0 + 1) := Nat.le_of_lt (ub_from 6 1 37 5 Y8 B (N0 + 1) 32 hY8 (Euc67.lin_lb 163 3 37 5 Y8 (N0 + 1) he8 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                        cases Nat.le_total (1 * (N0 + 1)) (7 * B) with
                        | inl hge9 =>
                          obtain ⟨X9, hX9⟩ := EisEuclid.le_dest _ _ hge9
                          have hw9 : Int.natAbs (Int.ofNat (6 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = X9 := Euc67.absw d2 hd2 _ X9 (Or.inl (by rw [hr2]; exact wpos (6 + 1) 1 X9 B N0 d2 hX9))
                          cases Nat.lt_or_ge (163 * (X9 * X9)) (3 * ((N0 + 1) * (N0 + 1))) with
                          | inl hb9 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 6 X9 hw9 hb9 (rs7_1 d2 hd2)
                          | inr hc9 =>
                            have lo9 : (37 * 1 + 5) * (N0 + 1) < (37 * 7) * B := lb_from 7 1 37 5 X9 B (N0 + 1) hX9 (Euc67.lin_lb 163 3 37 5 X9 (N0 + 1) hc9 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0))
                            exact (contra (37 * 1 + 5) (37 * 7) (37 * 6) 32 (N0 + 1) B lo9 up8 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim
                        | inr hle9 =>
                          obtain ⟨Y9, hY9⟩ := EisEuclid.le_dest _ _ hle9
                          have hv9 : Int.natAbs (Int.ofNat (6 + 1) * r2 + -(d2 * Int.ofNat 1 * Int.ofNat (N0 + 1))) = Y9 := Euc67.absw d2 hd2 _ Y9 (Or.inr (by rw [hr2]; exact wneg (6 + 1) 1 Y9 B N0 d2 hY9))
                          cases Nat.lt_or_ge (163 * (Y9 * Y9)) (3 * ((N0 + 1) * (N0 + 1))) with
                          | inl hf9 => exact branchG N0 r1 r2 (d2 * Int.ofNat 1) 6 Y9 hv9 hf9 (rs7_1 d2 hd2)
                          | inr he9 =>
                            have up9 : (37 * 7) * B ≤ 32 * (N0 + 1) := Nat.le_of_lt (ub_from 7 1 37 5 Y9 B (N0 + 1) 32 hY9 (Euc67.lin_lb 163 3 37 5 Y9 (N0 + 1) he9 (Nat.le_of_ble_eq_true rfl) (Nat.zero_lt_succ N0)) rfl)
                            exact (contra 5 37 (37 * 7) 32 (N0 + 1) B lb0 up9 (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)).elim

end Euc163

#print axioms Euc163.nrm_mulD
#print axioms Euc163.nrm_scal
#print axioms Euc163.natRes
#print axioms Euc163.resN
#print axioms Euc163.resc_gen
#print axioms Euc163.shift
#print axioms Euc163.branchG
#print axioms Euc163.lb_from
#print axioms Euc163.ub_from
#print axioms Euc163.contra
#print axioms Euc163.wpos
#print axioms Euc163.wneg
#print axioms Euc163.rs2_1
#print axioms Euc163.rs3_1
#print axioms Euc163.rs5_2
#print axioms Euc163.rs7_3
#print axioms Euc163.rs4_1
#print axioms Euc163.rs7_2
#print axioms Euc163.rs5_1
#print axioms Euc163.rs6_1
#print axioms Euc163.rs7_1
#print axioms Euc163.dh163_r

#print axioms: 22 theorems, no axioms.

78.320 gcd (\(d=163\))

Bezout via the Dedekind–Hasse step.

\[ \exists d=x\alpha +y\beta ,\ d\mid \alpha ,\beta . \]

lang/Gcd163.lean

/-
  lang/Gcd163 — T48/T50, T48: **gcd as a combination in ℤ[(1 + √−163)/2]** (a PID that is not norm-Euclidean).
    edh           for β ≠ 0: sα − qβ with N < N(β), and = 0 only for s = 1          (Euc163.dh_r on the two-step remainder)
    gaux          from any nonzero d = xα + yβ: either d | α, d | β, or a nonzero element of smaller norm in the ideal
    egcd_exists   d = xα + yβ dividing α and β
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.DH163

namespace Euc163

open FTAChain DetChain LibInt

theorem lt_of_mul (a N : Nat) (h : a * (N + 1) < (N + 1) * (N + 1)) : a < N + 1 := by
  cases Nat.lt_or_ge a (N + 1) with
  | inl h' => exact h'
  | inr h' => exact absurd (Nat.lt_of_lt_of_le h (Nat.mul_le_mul_right _ h')) (Nat.lt_irrefl _)

/-- **The Dedekind–Hasse step.** -/
theorem edh (α β : Int × Int) (N0 : Nat) (hβ : nrm β = N0 + 1) :
    ∃ s q : Int × Int, nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q β))) < N0 + 1 ∧
      (((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q β))) = (Ring163.ring163 LibInt.intR).zero → s = (Ring163.ring163 LibInt.intR).one) := by
  have hN : en β = Int.ofNat (N0 + 1) := by rw [← nrm_cast, hβ]
  generalize hS : (Ring163.ring163 LibInt.intR).mul α (cj β) = S
  obtain ⟨q2, r2, h2, b2⟩ := GaussEuclid.rnd S.2 N0
  obtain ⟨q1, t, h1, b1⟩ := GaussEuclid.rnd (2 * S.1 + r2) (2 * N0 + 1)
  have hT : Int.natAbs (2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2) ≤ N0 + 1 := by
    have e : 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = t := by
      have e2 : Int.ofNat (2 * N0 + 1 + 1) = 2 * Int.ofNat (N0 + 1) := by show 2 * Int.ofNat N0 + 1 + 1 = 2 * (Int.ofNat N0 + 1); lib_int
      calc 2 * (S.1 + -(q1 * Int.ofNat (N0 + 1))) + r2 = (2 * S.1 + r2) + -(q1 * (2 * Int.ofNat (N0 + 1))) := by lib_int
        _ = t := by rw [h1, ← e2]; lib_int
    rw [e]; exact Nat.le_of_mul_le_mul_left (Nat.le_trans b1 (Nat.le_of_eq (by lib_nat))) (Nat.zero_lt_succ 1)
  obtain ⟨s, e, hlt, hzero⟩ := dh163_r N0 (S.1 + -(q1 * Int.ofNat (N0 + 1))) r2 b2 hT
  have hSr : S = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (q1, q2) (Int.ofNat (N0 + 1), 0)) (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) := by
    apply Prod.ext
    · show S.1 = q1 * Int.ofNat (N0 + 1) + -(q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 * 0 + q2 *  -- (+58 chars; full line in the Zenodo source)
    · show S.2 = q1 * 0 + q2 * Int.ofNat (N0 + 1) + q2 * 0 + r2; rw [h2]; show q2 * Int.ofNat (N0 + 1) + r2 = _; lib_int
  generalize hr : (S.1 + -(q1 * Int.ofNat (N0 + 1)), r2) = r at hSr hlt hzero
  refine ⟨s, (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q1, q2)) e, ?_, ?_⟩
  all_goals
    have key : (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
        = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s r) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0))) := by
      rw [show (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q1, q2)) e) β))) (cj β)
          = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s ((Ring163.ring163 LibInt.intR).mul α (cj β))) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q1, q2)) e) ((Ring163.ring163 LibInt.intR).mul β (cj β)))) by lib_ring,
        hS, mul_cj, hN, hSr]
      lib_ring
  · -- norm bound
    have hn : en ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q1, q2)) e) β))) * Int.ofNat (N0 + 1)
        = en ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s r) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      rw [← key, en_mul, en_cj, hN]
    apply lt_of_mul
    have : nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s (q1, q2)) e) β))) * (N0 + 1)
        = nrm ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s r) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul e (Int.ofNat (N0 + 1), 0)))) := by
      apply Int.ofNat.inj
      show Int.ofNat (nrm _) * Int.ofNat (N0 + 1) = Int.ofNat (nrm _)
      rw [nrm_cast, nrm_cast]; exact hn
    rw [this]; exact hlt
  · intro h0
    apply hzero
    rw [← key, h0]; lib_ring

theorem nrm_succ (d : Int × Int) (hd : d ≠ (Ring163.ring163 LibInt.intR).zero) : ∃ N0, nrm d = N0 + 1 := by
  cases h : nrm d with
  | zero => exact absurd (nrm_zero d h) hd
  | succ N0 => exact ⟨N0, rfl⟩

theorem gaux (α β : Int × Int) : ∀ G (d x y : Int × Int), d = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x α) ((Ring163.ring163 LibInt.intR).mul y β) →
    nrm d ≤ G → d ≠ (Ring163.ring163 LibInt.intR).zero →
    ∃ d' x' y', d' = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x' α) ((Ring163.ring163 LibInt.intR).mul y' β) ∧ Dvd' d' α ∧ Dvd' d' β
  | 0, d, _, _, _, hG, hd => absurd (nrm_zero d (Nat.le_antisymm hG (Nat.zero_le _))) hd
  | G + 1, d, x, y, hdef, hG, hd => by
    obtain ⟨N0, hN0⟩ := nrm_succ d hd
    obtain ⟨s, q, hlt, hz⟩ := edh α d N0 hN0
    have hle : ∀ ρ, nrm ρ < N0 + 1 → nrm ρ ≤ G := fun ρ h => Nat.le_of_lt_succ (Nat.lt_of_lt_of_le h (Nat.le_trans (Nat.le_of_eq hN0.symm) hG))
    by_cases hρ : (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q d)) = (Ring163.ring163 LibInt.intR).zero
    · have s1 := hz hρ
      rw [s1] at hρ
      have hα : Dvd' d α := ⟨q, by
        calc α = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).one α) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q d))) ((Ring163.ring163 LibInt.intR).mul d q) := by lib_ring
          _ = _ := by rw [hρ]; lib_ring⟩
      obtain ⟨s', q', hlt', hz'⟩ := edh β d N0 hN0
      by_cases hρ' : (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul s' β) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q' d)) = (Ring163.ring163 LibInt.intR).zero
      · have s1' := hz' hρ'
        rw [s1'] at hρ'
        exact ⟨d, x, y, hdef, hα, ⟨q', by
          calc β = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).one β) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q' d))) ((Ring163.ring163 LibInt.intR).mul d q') := by lib_ring
            _ = _ := by rw [hρ']; lib_ring⟩⟩
      · exact gaux α β G _ ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q' x)) ((Ring163.ring163 LibInt.intR).add s' ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q' y)))
          (by rw [hdef]; lib_ring) (hle _ hlt') hρ'
    · exact gaux α β G _ ((Ring163.ring163 LibInt.intR).add s ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q x))) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul q y))
        (by rw [hdef]; lib_ring) (hle _ hlt) hρ

/-- **gcd as a combination in ℤ[(1 + √−163)/2].** -/
theorem egcd_exists (α β : Int × Int) :
    ∃ d x y, d = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x α) ((Ring163.ring163 LibInt.intR).mul y β) ∧ Dvd' d α ∧ Dvd' d β := by
  by_cases hb : β = (Ring163.ring163 LibInt.intR).zero
  · exact egcd_base α β hb
  · exact gaux α β (nrm β) β (Ring163.ring163 LibInt.intR).zero (Ring163.ring163 LibInt.intR).one (by lib_ring) (Nat.le_refl _) hb

end Euc163

#print axioms Euc163.lt_of_mul
#print axioms Euc163.edh
#print axioms Euc163.nrm_succ
#print axioms Euc163.gaux
#print axioms Euc163.egcd_exists

#print axioms: 5 theorems, no axioms.

78.321 Coprime splitting (\(d=163\))

Splitting by coprime norms.

\[ N(\gamma )=mn\Rightarrow \gamma =\alpha \beta . \]

lang/Cop163.lean

/-
  lang/Cop163 — T48/T50, T48: splitting by coprime norms in ℤ[(1+√−163)/2].

  If N(γ) = mn with sm + tn = 1 (m, n ≥ 1), then γ = αβ with N(α) = m, N(β) = n (N(a + bθ) = a² + ab + 41b²):
  α = gcd(γ, m), e = gcd(γ, n) (Euc163.egcd_exists), αe | γ by Euclid's lemma, γ | αe since mn = γγ̄, so γ = αe·v with v a unit;
  then N(α) | m, N(e) | n and N(α)N(e) = mn give N(α) = m. (Port of GaussCoprime.coprime_split.)
    en_emb, emb_mul, en_dvd, emb_add, en_zero_imp, cancel_left, en_nonneg_one, ecoprime_split
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Gcd163
import lang.GaussCoprime

namespace Cop163

open FTAChain DetChain LibInt Euc163

def emb (m : Int) : Int × Int := (m, 0)

theorem en_emb (m : Int) : en (emb m) = m * m := by show m * m + m * 0 + 41 * (0 * 0) = m * m; lib_int

theorem emb_mul (a b : Int) : (Ring163.ring163 LibInt.intR).mul (emb a) (emb b) = emb (a * b) := by
  apply Prod.ext
  · show a * b + -(0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0) = a * b; lib_int
  · show a * 0 + 0 * b + 0 * 0 = 0; lib_int

theorem en_dvd (d γ : Int × Int) (h : Dvd' d γ) : ∃ k, en γ = en d * k :=
  match h with
  | ⟨c, hc⟩ => ⟨en c, by rw [hc, en_mul]⟩

theorem emb_add (a b : Int) : emb (a + b) = (Ring163.ring163 LibInt.intR).add (emb a) (emb b) := by
  apply Prod.ext
  · rfl
  · show (0 : Int) = 0 + 0; rfl

theorem en_zero_imp (z : Int × Int) (h : en z = 0) : z = (Ring163.ring163 LibInt.intR).zero := by
  apply nrm_zero
  apply Int.ofNat.inj
  rw [nrm_cast]; exact h

theorem cancel_left (γ z : Int × Int) (hγ : en γ ≠ 0) (h : (Ring163.ring163 LibInt.intR).mul γ z = (Ring163.ring163 LibInt.intR).zero) : z = (Ring163.ring163 LibInt.intR).zero := by
  have : en γ * en z = 0 := by rw [← en_mul, h]; rfl
  cases int_noZD _ _ this with
  | inl e => exact absurd e hγ
  | inr e => exact en_zero_imp z e

theorem en_nonneg_one (z : Int × Int) (h : en z = -1) : False := by
  rw [← nrm_cast] at h
  exact Int.noConfusion h

/-- **Coprime splitting**: N(γ) = mn, sm + tn = 1 ⇒ γ = αβ with N(α) = m, N(β) = n. -/
theorem ecoprime_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) :
    ∃ α β, γ = (Ring163.ring163 LibInt.intR).mul α β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match egcd_exists γ (emb (Int.ofNat (m + 1))), egcd_exists γ (emb (Int.ofNat (n + 1))) with
  | ⟨d, x, y, hd, ⟨u, hu⟩, ⟨c1, hc1⟩⟩, ⟨e, x', y', he, ⟨u', hu'⟩, ⟨c2, hc2⟩⟩ =>
    have hco : (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (emb t) c2) e) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (emb s) c1) d) = (Ring163.ring163 LibInt.intR).one := by
      have h1 : (Ring163.ring163 LibInt.intR).one = emb (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; rfl
      rw [h1, emb_add, ← emb_mul, ← emb_mul, hc1, hc2]; lib_ring
    -- e | u, so γ = d e v
    match eeuclid e d u _ _ hco ⟨u', by rw [← hu, hu']⟩ with
    | ⟨v, hv⟩ =>
    have hγdev : γ = (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul d e) v := by rw [hu, hv]; lib_ring
    -- γ | d e
    have hmn : (Ring163.ring163 LibInt.intR).mul γ (cj γ) = emb (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by
      rw [mul_cj, hγ]; rfl
    have hW : (Ring163.ring163 LibInt.intR).mul d e = (Ring163.ring163 LibInt.intR).mul γ ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul x x') γ) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibI  -- (+172 chars; full line in the Zenodo source)
      rw [hd, he]
      have : (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul y y') ((Ring163.ring163 LibInt.intR).mul γ (cj γ)) = (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul y y') ((Ring163.ring163 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1)))) := by rw [hmn, emb_mul]
      calc (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x γ) ((Ring163.ring163 LibInt.intR).mul y (emb (Int.ofNat (m + 1))))) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul x' γ) ((Ring163.ring163 LibInt.intR).mul y' (emb (Int.ofNat (n + 1)))))
          = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul γ ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul x x') γ) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR  -- (+40 chars; full line in the Zenodo source)
              ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul y y') ((Ring163.ring163 LibInt.intR).mul (emb (Int.ofNat (m + 1))) (emb (Int.ofNat (n + 1))))) := by lib_ring
        _ = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul γ ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul x x') γ) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR  -- (+40 chars; full line in the Zenodo source)
              ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul y y') ((Ring163.ring163 LibInt.intR).mul γ (cj γ))) := by rw [this]
        _ = _ := by lib_ring
    generalize (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul x x') γ) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul x y') (emb (Int.ofNat (n + 1))))) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul y x') (emb (Int.ofNat (m + 1  -- (+99 chars; full line in the Zenodo source)
    have hγ0 : en γ ≠ 0 := by rw [hγ]; exact fun e => Nat.noConfusion (Int.ofNat.inj e)
    have hWv : (Ring163.ring163 LibInt.intR).mul W v = (Ring163.ring163 LibInt.intR).one := by
      have h0 : (Ring163.ring163 LibInt.intR).mul γ ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul W v) ((Ring163.ring163 LibInt.intR).neg (Ring163.ring163 LibInt.intR).one)) = (Ring163.ring163 LibInt.intR).zero := by
        rw [show (Ring163.ring163 LibInt.intR).mul γ ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul W v) ((Ring163.ring163 LibInt.intR).neg (Ring163.ring163 LibInt.intR).one)) = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul γ W) v) ((Ring163.ring163 LibInt.intR).neg γ) by lib_ring, ← hW, ← hγdev]; lib_ring
      have := cancel_left γ _ hγ0 h0
      calc (Ring163.ring163 LibInt.intR).mul W v = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul W v) ((Ring163.ring163 LibInt.intR).neg (Ring163.ring163 LibInt.intR).one)) (Ring163.ring163 LibInt.intR).one := by lib_ring
        _ = (Ring163.ring163 LibInt.intR).one := by rw [this]; lib_ring
    have hv1 : en v = 1 := by
      have : en W * en v = 1 := by rw [← en_mul, hWv]; rfl
      cases GaussCoprime.int_unit _ _ (by rw [show en v * en W = en W * en v by lib_int]; exact this) with
      | inl h => exact h
      | inr h => exact absurd h (en_nonneg_one v)
    -- norms
    have hN : en d * en e = Int.ofNat (m + 1) * Int.ofNat (n + 1) := by
      have := congrArg en hγdev
      rw [en_mul, en_mul, hv1, hγ] at this
      rw [show en d * en e = en d * en e * 1 by lib_int, ← this]; rfl
    have hdm : ∃ a, Int.ofNat (m + 1) = en d * a := by
      have k1 : Int.ofNat (m + 1) * Int.ofNat (m + 1) = en d * en c1 := by rw [← en_emb, hc1, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en d * en u := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu, en_mul]
      exact ⟨s * en c1 + t * en u, by
        calc Int.ofNat (m + 1) = Int.ofNat (m + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = s * (Int.ofNat (m + 1) * Int.ofNat (m + 1)) + t * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en d * (s * en c1 + t * en u) := by rw [k1, k2]; lib_int⟩
    have hen' : ∃ b, Int.ofNat (n + 1) = en e * b := by
      have k1 : Int.ofNat (n + 1) * Int.ofNat (n + 1) = en e * en c2 := by rw [← en_emb, hc2, en_mul]
      have k2 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = en e * en u' := by show Int.ofNat ((m + 1) * (n + 1)) = _; rw [← hγ, hu', en_mul]
      exact ⟨t * en c2 + s * en u', by
        calc Int.ofNat (n + 1) = Int.ofNat (n + 1) * (s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1)) := by rw [hst]; lib_int
          _ = t * (Int.ofNat (n + 1) * Int.ofNat (n + 1)) + s * (Int.ofNat (m + 1) * Int.ofNat (n + 1)) := by lib_int
          _ = en e * (t * en c2 + s * en u') := by rw [k1, k2]; lib_int⟩
    match hdm, hen' with
    | ⟨a, ha⟩, ⟨b, hb⟩ =>
      have hab : a * b = 1 := by
        have h0 : (en d * en e) * (a * b + -1) = 0 := by
          rw [show (en d * en e) * (a * b + -1) = (en d * a) * (en e * b) + -(en d * en e) by lib_int, ← ha, ← hb, hN]; lib_int
        cases int_noZD _ _ h0 with
        | inl e0 => exact absurd (by rw [hN] at e0; exact e0) (fun e1 : Int.ofNat (m + 1) * Int.ofNat (n + 1) = 0 => Nat.noConfusion (Int.ofNat.inj e1))
        | inr e0 => calc a * b = (a * b + -1) + 1 := by lib_int
            _ = 1 := by rw [e0]; rfl
      have pos : ∀ (k : Nat) (z : Int × Int) (c : Int), Int.ofNat (k + 1) = en z * c → c = 1 ∨ c = -1 → c = 1 := fun k z c h hc => by
        cases hc with
        | inl h1 => exact h1
        | inr h1 =>
          exfalso
          rw [h1, ← nrm_cast] at h
          have : Int.ofNat (k + 1) + Int.ofNat (nrm z) = 0 := by rw [h]; lib_int
          have h2 : k + nrm z + 1 = 0 := by rw [show k + nrm z + 1 = k + 1 + nrm z by lib_nat]; exact Int.ofNat.inj (show Int.ofNat (k + 1 + nrm z) = Int.ofNat 0 from this)
          exact Nat.noConfusion h2
      have a1 := pos m d a ha (GaussCoprime.int_unit a b hab)
      have b1 := pos n e b hb (GaussCoprime.int_unit b a (by rw [show b * a = a * b by lib_int]; exact hab))
      refine ⟨d, (Ring163.ring163 LibInt.intR).mul e v, by rw [hγdev]; lib_ring, by rw [ha, a1]; lib_int, by rw [en_mul, hv1, hb, b1]⟩

end Cop163

#print axioms Cop163.en_emb
#print axioms Cop163.emb_mul
#print axioms Cop163.en_dvd
#print axioms Cop163.emb_add
#print axioms Cop163.en_zero_imp
#print axioms Cop163.cancel_left
#print axioms Cop163.en_nonneg_one
#print axioms Cop163.ecoprime_split

#print axioms: 8 theorems, no axioms.

78.322 The primary class of 26569a (Legendre symbol)

\(H_{163}=\{\chi _{163}(\rho )=1\}\); \(\chi (-1)=-1\).

\[ H_{163}=\{a+b\theta :\ \chi _{163}(a+82b)=1\}. \]

lang/Prim163.lean

/-
  lang/Prim163 — T48/T50: **the primary class in ℤ[(1 + √−163)/2] through the Legendre symbol** (no residue enumeration):
  ρ(a + bθ) = a + 82b is the reduction mod √−163 (θ ≡ 82), and H163 = {z : χ₁₆₃(ρ z) = 1} (QChar.chi with p = 163).
    prime163      163 is prime (ZMod.md certificates)
    chi_m1       χ(−1) = −1 (163 ≡ 3 mod 4);  chi_neg  χ(−x) = −χ(x)
    cr_mul       ρ(xy) ≡ ρ(x)ρ(y) (mod 163)
    prim_mul, prim_cj, unit_pm, normalize, unit_one    the interface used by the Hecke files
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Euc163
import lang.GaussQR
import lang.CubicTwo
import lang.PencilClosed
import lang.W36Mul

set_option maxRecDepth 60000

namespace Prim163

open FTAChain DetChain LibInt Euc163 ZMod QChar
open LibRing (castZ castN)

theorem prime163 : ∀ d, Dv d (2 * 80 + 1 + 1 + 1) → d = 1 ∨ d = 2 * 80 + 1 + 1 + 1 := by
  intro d ⟨q, hq⟩
  match d with
  | 0 => exact absurd (hq.trans (Nat.mul_zero q)) (fun h => Nat.noConfusion h)
  | e + 1 =>
    match e with
    | 0 => exact Or.inl rfl
    | 1 => exact absurd (ZMod.dv_md_zero 1 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 2 => exact absurd (ZMod.dv_md_zero 2 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 3 => exact absurd (ZMod.dv_md_zero 3 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 4 => exact absurd (ZMod.dv_md_zero 4 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 5 => exact absurd (ZMod.dv_md_zero 5 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 6 => exact absurd (ZMod.dv_md_zero 6 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 7 => exact absurd (ZMod.dv_md_zero 7 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 8 => exact absurd (ZMod.dv_md_zero 8 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 9 => exact absurd (ZMod.dv_md_zero 9 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 10 => exact absurd (ZMod.dv_md_zero 10 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 11 => exact absurd (ZMod.dv_md_zero 11 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 12 => exact absurd (ZMod.dv_md_zero 12 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 13 => exact absurd (ZMod.dv_md_zero 13 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 14 => exact absurd (ZMod.dv_md_zero 14 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 15 => exact absurd (ZMod.dv_md_zero 15 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 16 => exact absurd (ZMod.dv_md_zero 16 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 17 => exact absurd (ZMod.dv_md_zero 17 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 18 => exact absurd (ZMod.dv_md_zero 18 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 19 => exact absurd (ZMod.dv_md_zero 19 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 20 => exact absurd (ZMod.dv_md_zero 20 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 21 => exact absurd (ZMod.dv_md_zero 21 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 22 => exact absurd (ZMod.dv_md_zero 22 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 23 => exact absurd (ZMod.dv_md_zero 23 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 24 => exact absurd (ZMod.dv_md_zero 24 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 25 => exact absurd (ZMod.dv_md_zero 25 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 26 => exact absurd (ZMod.dv_md_zero 26 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 27 => exact absurd (ZMod.dv_md_zero 27 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 28 => exact absurd (ZMod.dv_md_zero 28 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 29 => exact absurd (ZMod.dv_md_zero 29 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 30 => exact absurd (ZMod.dv_md_zero 30 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 31 => exact absurd (ZMod.dv_md_zero 31 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 32 => exact absurd (ZMod.dv_md_zero 32 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 33 => exact absurd (ZMod.dv_md_zero 33 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 34 => exact absurd (ZMod.dv_md_zero 34 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 35 => exact absurd (ZMod.dv_md_zero 35 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 36 => exact absurd (ZMod.dv_md_zero 36 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 37 => exact absurd (ZMod.dv_md_zero 37 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 38 => exact absurd (ZMod.dv_md_zero 38 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 39 => exact absurd (ZMod.dv_md_zero 39 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 40 => exact absurd (ZMod.dv_md_zero 40 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 41 => exact absurd (ZMod.dv_md_zero 41 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 42 => exact absurd (ZMod.dv_md_zero 42 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 43 => exact absurd (ZMod.dv_md_zero 43 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 44 => exact absurd (ZMod.dv_md_zero 44 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 45 => exact absurd (ZMod.dv_md_zero 45 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 46 => exact absurd (ZMod.dv_md_zero 46 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 47 => exact absurd (ZMod.dv_md_zero 47 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 48 => exact absurd (ZMod.dv_md_zero 48 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 49 => exact absurd (ZMod.dv_md_zero 49 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 50 => exact absurd (ZMod.dv_md_zero 50 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 51 => exact absurd (ZMod.dv_md_zero 51 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 52 => exact absurd (ZMod.dv_md_zero 52 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 53 => exact absurd (ZMod.dv_md_zero 53 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 54 => exact absurd (ZMod.dv_md_zero 54 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 55 => exact absurd (ZMod.dv_md_zero 55 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 56 => exact absurd (ZMod.dv_md_zero 56 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 57 => exact absurd (ZMod.dv_md_zero 57 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 58 => exact absurd (ZMod.dv_md_zero 58 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 59 => exact absurd (ZMod.dv_md_zero 59 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 60 => exact absurd (ZMod.dv_md_zero 60 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 61 => exact absurd (ZMod.dv_md_zero 61 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 62 => exact absurd (ZMod.dv_md_zero 62 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 63 => exact absurd (ZMod.dv_md_zero 63 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 64 => exact absurd (ZMod.dv_md_zero 64 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 65 => exact absurd (ZMod.dv_md_zero 65 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 66 => exact absurd (ZMod.dv_md_zero 66 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 67 => exact absurd (ZMod.dv_md_zero 67 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 68 => exact absurd (ZMod.dv_md_zero 68 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 69 => exact absurd (ZMod.dv_md_zero 69 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 70 => exact absurd (ZMod.dv_md_zero 70 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 71 => exact absurd (ZMod.dv_md_zero 71 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 72 => exact absurd (ZMod.dv_md_zero 72 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 73 => exact absurd (ZMod.dv_md_zero 73 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 74 => exact absurd (ZMod.dv_md_zero 74 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 75 => exact absurd (ZMod.dv_md_zero 75 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 76 => exact absurd (ZMod.dv_md_zero 76 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 77 => exact absurd (ZMod.dv_md_zero 77 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 78 => exact absurd (ZMod.dv_md_zero 78 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 79 => exact absurd (ZMod.dv_md_zero 79 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 80 => exact absurd (ZMod.dv_md_zero 80 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 81 => exact absurd (ZMod.dv_md_zero 81 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 82 => exact absurd (ZMod.dv_md_zero 82 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 83 => exact absurd (ZMod.dv_md_zero 83 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 84 => exact absurd (ZMod.dv_md_zero 84 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 85 => exact absurd (ZMod.dv_md_zero 85 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 86 => exact absurd (ZMod.dv_md_zero 86 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 87 => exact absurd (ZMod.dv_md_zero 87 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 88 => exact absurd (ZMod.dv_md_zero 88 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 89 => exact absurd (ZMod.dv_md_zero 89 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 90 => exact absurd (ZMod.dv_md_zero 90 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 91 => exact absurd (ZMod.dv_md_zero 91 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 92 => exact absurd (ZMod.dv_md_zero 92 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 93 => exact absurd (ZMod.dv_md_zero 93 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 94 => exact absurd (ZMod.dv_md_zero 94 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 95 => exact absurd (ZMod.dv_md_zero 95 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 96 => exact absurd (ZMod.dv_md_zero 96 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 97 => exact absurd (ZMod.dv_md_zero 97 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 98 => exact absurd (ZMod.dv_md_zero 98 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 99 => exact absurd (ZMod.dv_md_zero 99 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 100 => exact absurd (ZMod.dv_md_zero 100 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 101 => exact absurd (ZMod.dv_md_zero 101 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 102 => exact absurd (ZMod.dv_md_zero 102 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 103 => exact absurd (ZMod.dv_md_zero 103 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 104 => exact absurd (ZMod.dv_md_zero 104 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 105 => exact absurd (ZMod.dv_md_zero 105 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 106 => exact absurd (ZMod.dv_md_zero 106 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 107 => exact absurd (ZMod.dv_md_zero 107 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 108 => exact absurd (ZMod.dv_md_zero 108 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 109 => exact absurd (ZMod.dv_md_zero 109 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 110 => exact absurd (ZMod.dv_md_zero 110 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 111 => exact absurd (ZMod.dv_md_zero 111 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 112 => exact absurd (ZMod.dv_md_zero 112 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 113 => exact absurd (ZMod.dv_md_zero 113 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 114 => exact absurd (ZMod.dv_md_zero 114 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 115 => exact absurd (ZMod.dv_md_zero 115 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 116 => exact absurd (ZMod.dv_md_zero 116 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 117 => exact absurd (ZMod.dv_md_zero 117 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 118 => exact absurd (ZMod.dv_md_zero 118 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 119 => exact absurd (ZMod.dv_md_zero 119 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 120 => exact absurd (ZMod.dv_md_zero 120 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 121 => exact absurd (ZMod.dv_md_zero 121 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 122 => exact absurd (ZMod.dv_md_zero 122 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 123 => exact absurd (ZMod.dv_md_zero 123 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 124 => exact absurd (ZMod.dv_md_zero 124 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 125 => exact absurd (ZMod.dv_md_zero 125 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 126 => exact absurd (ZMod.dv_md_zero 126 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 127 => exact absurd (ZMod.dv_md_zero 127 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 128 => exact absurd (ZMod.dv_md_zero 128 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 129 => exact absurd (ZMod.dv_md_zero 129 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 130 => exact absurd (ZMod.dv_md_zero 130 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 131 => exact absurd (ZMod.dv_md_zero 131 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 132 => exact absurd (ZMod.dv_md_zero 132 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 133 => exact absurd (ZMod.dv_md_zero 133 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 134 => exact absurd (ZMod.dv_md_zero 134 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 135 => exact absurd (ZMod.dv_md_zero 135 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 136 => exact absurd (ZMod.dv_md_zero 136 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 137 => exact absurd (ZMod.dv_md_zero 137 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 138 => exact absurd (ZMod.dv_md_zero 138 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 139 => exact absurd (ZMod.dv_md_zero 139 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 140 => exact absurd (ZMod.dv_md_zero 140 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 141 => exact absurd (ZMod.dv_md_zero 141 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 142 => exact absurd (ZMod.dv_md_zero 142 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 143 => exact absurd (ZMod.dv_md_zero 143 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 144 => exact absurd (ZMod.dv_md_zero 144 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 145 => exact absurd (ZMod.dv_md_zero 145 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 146 => exact absurd (ZMod.dv_md_zero 146 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 147 => exact absurd (ZMod.dv_md_zero 147 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 148 => exact absurd (ZMod.dv_md_zero 148 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 149 => exact absurd (ZMod.dv_md_zero 149 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 150 => exact absurd (ZMod.dv_md_zero 150 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 151 => exact absurd (ZMod.dv_md_zero 151 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 152 => exact absurd (ZMod.dv_md_zero 152 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 153 => exact absurd (ZMod.dv_md_zero 153 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 154 => exact absurd (ZMod.dv_md_zero 154 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 155 => exact absurd (ZMod.dv_md_zero 155 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 156 => exact absurd (ZMod.dv_md_zero 156 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 157 => exact absurd (ZMod.dv_md_zero 157 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 158 => exact absurd (ZMod.dv_md_zero 158 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 159 => exact absurd (ZMod.dv_md_zero 159 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 160 => exact absurd (ZMod.dv_md_zero 160 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 161 => exact absurd (ZMod.dv_md_zero 161 163 ⟨q, hq⟩) (fun h => Nat.noConfusion h)
    | 162 => exact Or.inr rfl
    | e' + 163 =>
      exfalso
      cases q with
      | zero => exact Nat.noConfusion (hq.trans (Nat.zero_mul _))
      | succ q' =>
        have : 164 ≤ 163 := by
          calc 164 ≤ 164 + (e' + q' * (e' + 163 + 1)) := Nat.le_add_right _ _
            _ = (q' + 1) * (e' + 163 + 1) := by lib_nat
            _ = 163 := hq.symm
        exact Nat.not_succ_le_self 163 this

theorem pw_neg_one : ∀ k, Frobenius.pw (ZMod.ring (2 * 80 + 1 + 1)) ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one) (2 * k + 1) = (ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one
  | 0 => by show (ZMod.ring (2 * 80 + 1 + 1)).mul (ZMod.ring (2 * 80 + 1 + 1)).one ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one) = _; lib_ring
  | k + 1 => by
    show (ZMod.ring (2 * 80 + 1 + 1)).mul ((ZMod.ring (2 * 80 + 1 + 1)).mul (Frobenius.pw (ZMod.ring (2 * 80 + 1 + 1)) ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one) (2 * k + 1)) ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one)) ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one) = _
    rw [pw_neg_one k]; lib_ring

theorem chi_m1 : chi 80 ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one) = -1 :=
  chi_neg_of 80 _ (PencilClosed.negnz 80 _ (PowerSum.one_ne_zero' (2 * 80 + 1))) (by rw [show 80 + 1 = 2 * 40 + 1 from rfl, pw_neg_one 40]; exact ne_pm 80)

theorem chi_neg (x : Zp (2 * 80 + 1 + 1)) : chi 80 ((ZMod.ring (2 * 80 + 1 + 1)).neg x) = -chi 80 x := by
  rw [show (ZMod.ring (2 * 80 + 1 + 1)).neg x = (ZMod.ring (2 * 80 + 1 + 1)).mul ((ZMod.ring (2 * 80 + 1 + 1)).neg (ZMod.ring (2 * 80 + 1 + 1)).one) x by lib_ring, chi_mul 80 prime163, chi_m1]; lib_int

def rho (z : Int × Int) : Int := z.1 + 82 * z.2

def cr (z : Int × Int) : Zp (2 * 80 + 1 + 1) := castZ (ZMod.ring (2 * 80 + 1 + 1)) (rho z)

theorem rho_mul (x y : Int × Int) : rho ((Ring163.ring163 LibInt.intR).mul x y) = rho x * rho y + -(163 * (41 * (x.2 * y.2))) := by
  show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y  -- (+222 chars; full line in the Zenodo source)
  lib_int

theorem cz163 (w : Int) : castZ (ZMod.ring (2 * 80 + 1 + 1)) (163 * w) = (ZMod.ring (2 * 80 + 1 + 1)).zero := by
  rw [HasseDavenport.castZ_mul, show castZ (ZMod.ring (2 * 80 + 1 + 1)) 163 = (ZMod.ring (2 * 80 + 1 + 1)).zero from rfl]; lib_ring

theorem cr_mul (x y : Int × Int) : cr ((Ring163.ring163 LibInt.intR).mul x y) = (ZMod.ring (2 * 80 + 1 + 1)).mul (cr x) (cr y) := by
  unfold cr; rw [rho_mul, IntCast.castZ_add, HasseDavenport.castZ_mul, HasseDavenport.castZ_neg, cz163]; lib_ring

theorem cr_neg (z : Int × Int) : cr (-z.1, -z.2) = (ZMod.ring (2 * 80 + 1 + 1)).neg (cr z) := by
  unfold cr; rw [show rho (-z.1, -z.2) = -rho z by show -z.1 + 82 * -z.2 = -(z.1 + 82 * z.2); lib_int, HasseDavenport.castZ_neg]

def Prim163 (z : Int × Int) : Prop := chi 80 (cr z) = 1

instance (z : Int × Int) : Decidable (Prim163 z) := inferInstanceAs (Decidable (_ = _))

theorem prim_mul (x y : Int × Int) (hx : Prim163 x) (hy : Prim163 y) : Prim163 ((Ring163.ring163 LibInt.intR).mul x y) := by
  show chi 80 (cr _) = 1
  rw [cr_mul, chi_mul 80 prime163, show chi 80 (cr x) = 1 from hx, show chi 80 (cr y) = 1 from hy]; rfl

theorem prim_cj (z : Int × Int) (hz : Prim163 z) : Prim163 (cj z) := by
  have e : cr (cj z) = cr z := by
    unfold cr
    rw [show rho (cj z) = rho z + 163 * -z.2 by show z.1 + z.2 + 82 * -z.2 = z.1 + 82 * z.2 + 163 * -z.2; lib_int, IntCast.castZ_add, cz163]; lib_ring
  show chi 80 (cr (cj z)) = 1; rw [e]; exact hz

theorem unit_pm (u : Int × Int) (h : en u = 1) : u = (Ring163.ring163 LibInt.intR).one ∨ u = (-1, 0) := by
  have h4 := four_nrm u
  have hn : nrm u = 1 := Int.ofNat.inj (by rw [nrm_cast, h]; rfl)
  rw [hn] at h4
  have hB : Int.natAbs u.2 * Int.natAbs u.2 = 0 := by
    cases hb : Int.natAbs u.2 * Int.natAbs u.2 with
    | zero => rfl
    | succ t => rw [hb, show Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 163 * (t + 1) = (Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) + 163 * t + 159) + 4 by lib_nat] at h4
                exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj h4))))
  have b0 : u.2 = 0 := GaussEuclid.natAbs_zero _ (by cases nat_mul_zero _ _ hB with | inl e => exact e | inr e => exact e)
  rw [hB] at h4
  have hT : Int.natAbs (2 * u.1 + u.2) * Int.natAbs (2 * u.1 + u.2) = 2 * 2 := h4.symm
  have hT2 : Int.natAbs (2 * u.1 + u.2) = 2 := by
    match hx : Int.natAbs (2 * u.1 + u.2) with
    | 0 => rw [hx] at hT; exact Nat.noConfusion hT
    | 1 => rw [hx] at hT; exact Nat.noConfusion (Nat.succ.inj hT)
    | 2 => rfl
    | t + 3 => rw [hx, show (t + 3) * (t + 3) = (t * t + 6 * t + 5) + 4 by lib_nat] at hT; exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hT))))
  rw [b0] at hT2
  obtain ⟨a, b⟩ := u
  show (a, b) = (1, 0) ∨ (a, b) = (-1, 0)
  have b0' : b = 0 := b0
  rcases EisEuclid.sgn (2 * a + 0) with s | s <;> rw [hT2] at s
  · exact Or.inl (Prod.ext (W36Mul.two_cancel_int a 1 (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')
  · exact Or.inr (Prod.ext (W36Mul.two_cancel_int a (-1) (by rw [show 2 * a = 2 * a + 0 by lib_int, s]; rfl)) b0')

theorem neg_mul' (z : Int × Int) : (Ring163.ring163 LibInt.intR).mul (-1, 0) z = (-z.1, -z.2) :=
  Prod.ext (by show -1 * z.1 + -(0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2 + 0 * z.2  -- (+113 chars; full line in the Zenodo source)

/-- Generic factorization: (a + c b)(a − c' b) + D t b² = a² + ab + t b² when c = c' + 1 and D t = c c' + t, with D | a + c b. -/
theorem fac_gen (a b c c' D t q : Int) (h1 : c = c' + 1) (h2 : D * t = c * c' + t) (hq : a + c * b = D * q) :
    a * a + a * b + t * (b * b) = D * (q * (a + -(c' * b)) + t * (b * b)) := by
  calc a * a + a * b + t * (b * b) = (a + c * b) * (a + -(c' * b)) + (c * c' + t) * (b * b) + (c' + 1 + -c) * (a * b) := by lib_int
    _ = D * q * (a + -(c' * b)) + D * t * (b * b) + (c' + 1 + -c) * (a * b) := by rw [hq, h2]
    _ = D * q * (a + -(c' * b)) + D * t * (b * b) := by rw [h1]; lib_int
    _ = _ := by lib_int

theorem cr_ne (α : Int × Int) (hd : ∀ k, en α ≠ 163 * k) : cr α ≠ (ZMod.ring (2 * 80 + 1 + 1)).zero := fun h0 => by
  obtain ⟨q, hq⟩ := CubicTwo.cast_zero_gen (2 * 80 + 1 + 1) (rho α) h0
  obtain ⟨a, b⟩ := α
  exact hd _ (fac_gen a b 82 81 163 41 q rfl rfl hq)

/-- 163 ∤ N(α) ⇒ ±α ∈ H163. -/
theorem normalize (α : Int × Int) (hd : ∀ k, en α ≠ 163 * k) : ∃ u, en u = 1 ∧ Prim163 ((Ring163.ring163 LibInt.intR).mul u α) := by
  rcases GaussQR.chi_pm 80 _ (cr_ne α hd) with h | h
  · exact ⟨(Ring163.ring163 LibInt.intR).one, rfl, by rw [(Ring163.ring163 LibInt.intR).one_mul]; exact h⟩
  · refine ⟨(-1, 0), rfl, ?_⟩
    show chi 80 (cr _) = 1
    rw [neg_mul', cr_neg, chi_neg, h]; rfl

theorem unit_one (u α : Int × Int) (hu : en u = 1) (pα : Prim163 α) (pu : Prim163 ((Ring163.ring163 LibInt.intR).mul u α)) : u = (Ring163.ring163 LibInt.intR).one := by
  rcases unit_pm u hu with e0 | e0
  · exact e0
  exfalso
  rw [e0, neg_mul'] at pu
  have h1 : chi 80 (cr α) = 1 := pα
  have h2 : chi 80 (cr (-α.1, -α.2)) = 1 := pu
  rw [cr_neg, chi_neg, h1] at h2
  exact Int.noConfusion h2

end Prim163

#print axioms Prim163.prime163
#print axioms Prim163.pw_neg_one
#print axioms Prim163.chi_m1
#print axioms Prim163.chi_neg
#print axioms Prim163.rho_mul
#print axioms Prim163.cz163
#print axioms Prim163.cr_mul
#print axioms Prim163.fac_gen
#print axioms Prim163.cr_neg
#print axioms Prim163.prim_mul
#print axioms Prim163.prim_cj
#print axioms Prim163.unit_pm
#print axioms Prim163.neg_mul'
#print axioms Prim163.fac_gen
#print axioms Prim163.cr_ne
#print axioms Prim163.normalize
#print axioms Prim163.unit_one

#print axioms: 17 theorems, no axioms.

78.323 Unique splitting into \(H_{163}\)

By coprime norms prime to \(163\).

\[ \gamma =\alpha \beta \ \text {unique}. \]

lang/Uniq163.lean

/-
  lang/Uniq163 — T48/T50, T48: the unique splitting into H163-elements (Prim163) in ℤ[(1+√−163)/2]:
    prim163_quot, uniq_split163, prim163_split   (norms prime to 163; port of Sq2Unique)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Prim163
import lang.Cop163

namespace Uniq163

open FTAChain DetChain LibInt Euc163 Prim163

/-- y ∈ H163 when x and xy are (163 ∤ N(y)). -/
theorem prim163_quot (x y : Int × Int) (hy : ∀ k, en y ≠ 163 * k) (hx : Prim163 x) (hxy : Prim163 ((Ring163.ring163 LibInt.intR).mul x y)) : Prim163 y := by
  obtain ⟨u, hu, hpu⟩ := normalize y hy
  have h1 : Prim163 ((Ring163.ring163 LibInt.intR).mul u ((Ring163.ring163 LibInt.intR).mul x y)) := by
    rw [show (Ring163.ring163 LibInt.intR).mul u ((Ring163.ring163 LibInt.intR).mul x y) = (Ring163.ring163 LibInt.intR).mul x ((Ring163.ring163 LibInt.intR).mul u y) by lib_ring]
    exact prim_mul _ _ hx hpu
  have hu1 := unit_one u _ hu hxy h1
  rw [hu1, (Ring163.ring163 LibInt.intR).one_mul] at hpu
  exact hpu

/-- **Uniqueness of the primary splitting.** -/
theorem uniq_split163 (α β α' β' : Int × Int) (M N s t : Int) (hα : en α = M) (hα' : en α' = M) (hβ' : en β' = N)
    (hst : s * M + t * N = 1) (hM : M ≠ 0) (pα : Prim163 α) (pα' : Prim163 α') (h : (Ring163.ring163 LibInt.intR).mul α β = (Ring163.ring163 LibInt.intR).mul α' β') :
    α = α' ∧ β = β' := by
  have hco : (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (Cop163.emb s) (cj α)) α) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (Cop163.emb t) (cj β')) β') = (Ring163.ring163 LibInt.intR).one := by
    rw [show (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (Cop163.emb s) (cj α)) α) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (Cop163.emb t) (cj β')) β') = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Cop163.emb s) ((Ring163.ring163 LibInt.intR).mul α (cj α))) ((Ring163.ring163 LibInt.in  -- (+83 chars; full line in the Zenodo source)
      mul_cj, mul_cj, hα, hβ']
    show (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Cop163.emb s) (Cop163.emb M)) ((Ring163.ring163 LibInt.intR).mul (Cop163.emb t) (Cop163.emb N)) = _
    rw [Cop163.emb_mul, Cop163.emb_mul, ← Cop163.emb_add, hst]; rfl
  match eeuclid α β' α' _ _ hco ⟨β, by rw [show (Ring163.ring163 LibInt.intR).mul β' α' = (Ring163.ring163 LibInt.intR).mul α' β' by lib_ring, ← h]⟩ with
  | ⟨w, hw⟩ =>
    have hgw : en w = 1 := by
      have e : M * en w = M * 1 := by
        calc M * en w = en α * en w := by rw [hα]
          _ = en α' := by rw [← en_mul, ← hw]
          _ = M * 1 := by rw [hα']; lib_int
      have e0 : M * (en w + -1) = 0 := by rw [show M * (en w + -1) = M * en w + -(M * 1) by lib_int, e]; lib_int
      cases int_noZD _ _ e0 with
      | inl h0 => exact absurd h0 hM
      | inr h0 => calc en w = (en w + -1) + 1 := by lib_int
          _ = 1 := by rw [h0]; rfl
    have hw1 : w = (Ring163.ring163 LibInt.intR).one :=
      unit_one w α hgw pα (by rw [show (Ring163.ring163 LibInt.intR).mul w α = α' by rw [hw]; lib_ring]; exact pα')
    have hαα : α = α' := by rw [hw, hw1]; lib_ring
    refine ⟨hαα, ?_⟩
    have h0 : (Ring163.ring163 LibInt.intR).mul α ((Ring163.ring163 LibInt.intR).add β ((Ring163.ring163 LibInt.intR).neg β')) = (Ring163.ring163 LibInt.intR).zero := by
      rw [show (Ring163.ring163 LibInt.intR).mul α ((Ring163.ring163 LibInt.intR).add β ((Ring163.ring163 LibInt.intR).neg β')) = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul α β) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul α β')) by lib_ring, h, ← hαα]; lib_ring
    have := Cop163.cancel_left α _ (by rw [hα]; exact hM) h0
    calc β = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).add β ((Ring163.ring163 LibInt.intR).neg β')) β' := by lib_ring
      _ = β' := by rw [this]; lib_ring

/-- **Existence of the primary splitting.** -/
theorem prim163_split (γ : Int × Int) (m n : Nat) (s t : Int) (hγ : en γ = Int.ofNat ((m + 1) * (n + 1)))
    (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1) (hm : ∀ k, Int.ofNat (m + 1) ≠ 163 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 163 * k)
    (pγ : Prim163 γ) : ∃ α β, γ = (Ring163.ring163 LibInt.intR).mul α β ∧ Prim163 α ∧ Prim163 β ∧ en α = Int.ofNat (m + 1) ∧ en β = Int.ofNat (n + 1) := by
  match Cop163.ecoprime_split γ m n s t hγ hst with
  | ⟨α0, β0, h0, hα0, hβ0⟩ =>
    match normalize α0 (by rw [hα0]; exact hm) with
    | ⟨u, gu, pu⟩ =>
      have uc : (Ring163.ring163 LibInt.intR).mul u (cj u) = (Ring163.ring163 LibInt.intR).one := by rw [mul_cj, gu]; rfl
      have hβn : en ((Ring163.ring163 LibInt.intR).mul (cj u) β0) = Int.ofNat (n + 1) := by
        rw [en_mul, show en (cj u) = en u from en_cj u, gu, hβ0]; lib_int
      have hsplit : γ = (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul u α0) ((Ring163.ring163 LibInt.intR).mul (cj u) β0) := by
        rw [h0, show (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul u α0) ((Ring163.ring163 LibInt.intR).mul (cj u) β0) = (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul u (cj u)) ((Ring163.ring163 LibInt.intR).mul α0 β0) by lib_ring, uc]; lib_ring
      refine ⟨(Ring163.ring163 LibInt.intR).mul u α0, (Ring163.ring163 LibInt.intR).mul (cj u) β0, hsplit, pu,
        prim163_quot _ _ (by rw [hβn]; exact hn) pu (by rw [← hsplit]; exact pγ),
        by rw [en_mul, gu, hα0]; lib_int, hβn⟩

end Uniq163

#print axioms Uniq163.prim163_quot
#print axioms Uniq163.uniq_split163
#print axioms Uniq163.prim163_split

#print axioms: 3 theorems, no axioms.

78.324 26569a: multiplicativity

Theta coefficients over \(H_{163}\) are multiplicative.

\[ c_w(mn)=c_w(m)c_w(n). \]

lang/HeckeMult163.lean

/-
  lang/HeckeMult163 — T48/T50, T48: **the theta series of ℚ(√−163)'s Hecke character class is multiplicative**: for every
  multiplicative w on ℤ[θ], θ = (1 + √−163)/2,  c_w(n) = Σ_{α ∈ H163, N(α) = n} w(α)  satisfies c_w(mn) = c_w(m)c_w(n) for coprime m, n
  prime to 163.  Enumeration box163 n = HeckeMult.box (164n).
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Uniq163
import lang.HeckeMultW

namespace HeckeMult163

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Euc163 Prim163 Uniq163

abbrev box163 (n : Nat) : List (Int × Int) := HeckeMult.box (164 * n)

/-- D a² + (a + c b)² = E (a² + ab + t b²) when 2c = E, c² = E t, D + 1 = E. -/
theorem sqgen (a b c D E t : Int) (h1 : 2 * c = E) (h2 : c * c = E * t) (h3 : D + 1 = E) :
    D * (a * a) + (a + c * b) * (a + c * b) = E * (a * a + a * b + t * (b * b)) := by
  calc D * (a * a) + (a + c * b) * (a + c * b) = (D + 1) * (a * a) + (2 * c) * (a * b) + (c * c) * (b * b) := by lib_int
    _ = E * (a * a) + E * (a * b) + (E * t) * (b * b) := by rw [h3, h1, h2]
    _ = _ := by lib_int

/-- N(z) = n ⇒ |z.1|, |z.2| ≤ 164n  (163a² + (a + 82b)² = 164N, (2a + b)² + 163b² = 4N). -/
theorem abs_le163 (z : Int × Int) (n : Nat) (h : en z = Int.ofNat n) : Int.natAbs z.1 ≤ 164 * n ∧ Int.natAbs z.2 ≤ 164 * n := by
  have hn : nrm z = n := Int.ofNat.inj (by rw [nrm_cast]; exact h)
  have h4 := four_nrm z
  rw [hn] at h4
  have h8 : 163 * (Int.natAbs z.1 * Int.natAbs z.1) + Int.natAbs (z.1 + 82 * z.2) * Int.natAbs (z.1 + 82 * z.2) = 164 * n := Int.ofNat.inj (by
    show 163 * Int.ofNat (Int.natAbs z.1 * Int.natAbs z.1) + Int.ofNat (Int.natAbs (z.1 + 82 * z.2) * Int.natAbs (z.1 + 82 * z.2)) = 164 * Int.ofNat n
    rw [GaussEuclid.nas, GaussEuclid.nas, ← h]; unfold en; exact sqgen _ _ 82 163 164 41 rfl rfl rfl)
  have seven : ∀ X : Nat, X ≤ 163 * (X * X) := fun X => Nat.le_trans (HeckeMult.le_sq X) (Nat.le_trans (Nat.le_of_eq (Nat.one_mul _).symm) (Nat.mul_le_mul_right _ (Nat.le_add_left 1 162)))
  constructor
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_of_eq h8))
  · exact Nat.le_trans (seven _) (Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans (Nat.le_of_eq h4.symm) (Nat.mul_le_mul_right n (Nat.le_add_right 4 160))))

def PD163 (n : Nat) (z : Int × Int) : Prop := en z = Int.ofNat n ∧ Prim163 z

instance (n : Nat) (z : Int × Int) : Decidable (PD163 n z) := inferInstanceAs (Decidable (_ ∧ _))

theorem zsum163 {A : Type} : ∀ l : List A, sumK (Ring163.ring163 LibInt.intR) (fun _ => (Ring163.ring163 LibInt.intR).zero) l = (Ring163.ring163 LibInt.intR).zero
  | [] => rfl
  | _ :: l => by show (Ring163.ring163 LibInt.intR).add (Ring163.ring163 LibInt.intR).zero (sumK (Ring163.ring163 LibInt.intR) (fun _ => (Ring163.ring163 LibInt.intR).zero) l) = _; rw [zsum163 l]; exact (Ring163.ring163 LibInt.intR).zero_add _

def I163 (n : Nat) (z : Int × Int) : Int × Int := if PD163 n z then (Ring163.ring163 LibInt.intR).one else (Ring163.ring163 LibInt.intR).zero

theorem I163_pos (n : Nat) (z : Int × Int) (h : PD163 n z) : I163 n z = (Ring163.ring163 LibInt.intR).one := ite_pos' _ _ h
theorem I163_neg (n : Nat) (z : Int × Int) (h : ¬ PD163 n z) : I163 n z = (Ring163.ring163 LibInt.intR).zero := ite_neg' _ _ h

/-- c_w(n) = Σ_{α ≡ 1 (3), N(α) = n} w(α). -/
def cw163 (w : Int × Int → Int × Int) (n : Nat) : Int × Int := sumK (Ring163.ring163 LibInt.intR) (fun z => (Ring163.ring163 LibInt.intR).mul (I163 n z) (w z)) (box163 n)

/-- **c_w(mn) = c_w(m) c_w(n)** for coprime m, n prime to 3 and multiplicative w on ℤ[ω]. -/
theorem hecke_mult163 (w : Int × Int → Int × Int) (hw : ∀ α β, w ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (w α) (w β)) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 163 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 163 * k) :
    cw163 w ((m + 1) * (n + 1)) = (Ring163.ring163 LibInt.intR).mul (cw163 w (m + 1)) (cw163 w (n + 1)) := by
  have hM0 : Int.ofNat (m + 1) ≠ 0 := fun e => Nat.noConfusion (Int.ofNat.inj e)
  -- product of primaries of norms m, n is primary of norm mn
  have hprod : ∀ α β, PD163 (m + 1) α → PD163 (n + 1) β → PD163 ((m + 1) * (n + 1)) ((Ring163.ring163 LibInt.intR).mul α β) := fun α β ⟨h1, p1⟩ ⟨h2, p2⟩ =>
    ⟨by rw [en_mul, h1, h2]; rfl, prim_mul _ _ p1 p2⟩
  -- Step A: expand c_m c_n with a δ over γ
  have stepA : (Ring163.ring163 LibInt.intR).mul (cw163 w (m + 1)) (cw163 w (n + 1))
      = sumK (Ring163.ring163 LibInt.intR) (fun α => sumK (Ring163.ring163 LibInt.intR) (fun β => sumK (Ring163.ring163 LibInt.intR) (fun γ => (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (I163 (m + 1) α) (I163 (n + 1) β)) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β))) (w γ)) (box163 ((m + 1) * (n + 1))))   -- (+40 chars; full line in the Zenodo source)
    unfold cw163; rw [sumK_mul]
    refine sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => ?_) _
    rw [mul_sumK]
    refine sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun β => ?_) _
    by_cases hα : PD163 (m + 1) α
    · by_cases hβ : PD163 (n + 1) β
      · rw [I163_pos _ _ hα, I163_pos _ _ hβ]
        have hb := abs_le163 _ _ (hprod α β hα hβ).1
        have e := HeckeMult.pick_box (Ring163.ring163 LibInt.intR) (fun γ => w γ) ((Ring163.ring163 LibInt.intR).mul α β) _ hb.1 hb.2
        rw [show sumK (Ring163.ring163 LibInt.intR) (fun γ => (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).one (Ring163.ring163 LibInt.intR).one) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β))) (w γ)) (box163 ((m + 1) * (n + 1)))
            = sumK (Ring163.ring163 LibInt.intR) (fun γ => (Ring163.ring163 LibInt.intR).mul (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β)) (w γ)) (box163 ((m + 1) * (n + 1))) from sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun γ => by lib_ring) _, e, hw]
        lib_ring
      · rw [I163_neg _ _ hβ]
        rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun γ => show (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (I163 (m + 1) α) (Ring163.ring163 LibInt.intR).zero) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β))) (w γ) = (Ring163.ring163 LibInt.intR).zero by lib_ring)]
        rw [zsum163]; lib_ring
    · rw [I163_neg _ _ hα]
      rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun γ => show (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).zero (I163 (n + 1) β)) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β))) (w γ) = (Ring163.ring163 LibInt.intR).zero by lib_ring)]
      rw [zsum163]; lib_ring

  rw [stepA]
  -- swap to Σ_γ Σ_α Σ_β
  rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => sumK_swap (Ring163.ring163 LibInt.intR) _ _ _)]
  rw [sumK_swap (Ring163.ring163 LibInt.intR)]
  unfold cw163
  refine sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun γ => ?_) _
  have e1 : sumK (Ring163.ring163 LibInt.intR) (fun α => sumK (Ring163.ring163 LibInt.intR) (fun β => (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (I163 (m + 1) α) (I163 (n + 1) β)) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β))) (w γ)) (box163 (n + 1))) (box163 (m + 1))
      = (Ring163.ring163 LibInt.intR).mul (sumK (Ring163.ring163 LibInt.intR) (fun α => sumK (Ring163.ring163 LibInt.intR) (fun β => (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (I163 (m + 1) α) (I163 (n + 1) β)) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β))) (box163 (n + 1))) (box163 (m + 1))) (w γ) := by
    rw [sumK_mul]; exact sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => by rw [sumK_mul]) _
  rw [e1]
  by_cases hγ : PD163 ((m + 1) * (n + 1)) γ
  · rw [I163_pos _ _ hγ]
    match prim163_split γ m n s t hγ.1 hst hm hn hγ.2 with
    | ⟨α0, β0, h0, pα0, pβ0, nα0, nβ0⟩ =>
      have pt : ∀ α β, (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (I163 (m + 1) α) (I163 (n + 1) β)) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β)) = (Ring163.ring163 LibInt.intR).mul (delta (Ring163.ring163 LibInt.intR) α α0) (delta (Ring163.ring163 LibInt.intR) β β0) := by
        intro α β
        by_cases hαβ : PD163 (m + 1) α ∧ PD163 (n + 1) β ∧ γ = (Ring163.ring163 LibInt.intR).mul α β
        · match hαβ with
          | ⟨hα, hβ, hg⟩ =>
            have u := uniq_split163 α β α0 β0 _ _ s t hα.1 nα0 nβ0 hst hM0 hα.2 pα0 (hg.symm.trans h0)
            rw [I163_pos _ _ hα, I163_pos _ _ hβ, ← u.1, ← u.2]
            unfold delta; rw [ite_pos' _ _ hg, ite_pos' _ _ rfl, ite_pos' _ _ rfl]; lib_ring
        · by_cases he : α = α0 ∧ β = β0
          · exfalso; apply hαβ
            rw [he.1, he.2]; exact ⟨⟨nα0, pα0⟩, ⟨nβ0, pβ0⟩, h0⟩
          · have rhs : (Ring163.ring163 LibInt.intR).mul (delta (Ring163.ring163 LibInt.intR) α α0) (delta (Ring163.ring163 LibInt.intR) β β0) = (Ring163.ring163 LibInt.intR).zero := by
              unfold delta
              by_cases h1 : α = α0
              · rw [ite_neg' _ _ (fun h2 => he ⟨h1, h2⟩), ite_pos' _ _ h1]; lib_ring
              · rw [ite_neg' _ _ h1]; lib_ring
            rw [rhs]
            by_cases hα : PD163 (m + 1) α
            · by_cases hβ : PD163 (n + 1) β
              · have hg : ¬ γ = (Ring163.ring163 LibInt.intR).mul α β := fun hg => hαβ ⟨hα, hβ, hg⟩
                unfold delta; rw [ite_neg' _ _ hg]; lib_ring
              · rw [I163_neg _ _ hβ]; lib_ring
            · rw [I163_neg _ _ hα]; lib_ring
      rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun β => pt α β) _)]
      have ba := abs_le163 α0 _ nα0
      have bb := abs_le163 β0 _ nβ0
      rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => by rw [← mul_sumK, HeckeMult.count_box (Ring163.ring163 LibInt.intR) β0 _ bb.1 bb.2])]
      rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => show (Ring163.ring163 LibInt.intR).mul (delta (Ring163.ring163 LibInt.intR) α α0) (Ring163.ring163 LibInt.intR).one = (Ring163.ring163 LibInt.intR).mul (delta (Ring163.ring163 LibInt.intR) α α0) (Ring163.ring163 LibInt.intR).one from rfl), ← sumK_mul,
        HeckeMult.count_box (Ring163.ring163 LibInt.intR) α0 _ ba.1 ba.2]
      lib_ring
  · rw [I163_neg _ _ hγ]
    have pt : ∀ α β, (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (I163 (m + 1) α) (I163 (n + 1) β)) (delta (Ring163.ring163 LibInt.intR) γ ((Ring163.ring163 LibInt.intR).mul α β)) = (Ring163.ring163 LibInt.intR).zero := by
      intro α β
      by_cases hα : PD163 (m + 1) α
      · by_cases hβ : PD163 (n + 1) β
        · have hg : ¬ γ = (Ring163.ring163 LibInt.intR).mul α β := fun hg => hγ (by rw [hg]; exact hprod α β hα hβ)
          unfold delta; rw [ite_neg' _ _ hg]; lib_ring
        · rw [I163_neg _ _ hβ]; lib_ring
      · rw [I163_neg _ _ hα]; lib_ring
    rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun β => pt α β) _)]
    rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun α => zsum163 _), zsum163]

/-- **The Hecke characters α ↦ α^k of ℚ(√−3)**: c_k(mn) = c_k(m) c_k(n). -/
theorem hecke_mult163_pow (k : Nat) (m n : Nat) (s t : Int) (hst : s * Int.ofNat (m + 1) + t * Int.ofNat (n + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 163 * k) (hn : ∀ k, Int.ofNat (n + 1) ≠ 163 * k) :
    cw163 (fun z => Frobenius.pw (Ring163.ring163 LibInt.intR) z k) ((m + 1) * (n + 1)) =
      (Ring163.ring163 LibInt.intR).mul (cw163 (fun z => Frobenius.pw (Ring163.ring163 LibInt.intR) z k) (m + 1)) (cw163 (fun z => Frobenius.pw (Ring163.ring163 LibInt.intR) z k) (n + 1)) :=
  hecke_mult163 _ (fun α β => Frobenius.pw_mul _ α β k) m n s t hst hm hn

end HeckeMult163

#print axioms HeckeMult163.sqgen
#print axioms HeckeMult163.abs_le163
#print axioms HeckeMult163.zsum163
#print axioms HeckeMult163.I163_pos
#print axioms HeckeMult163.I163_neg
#print axioms HeckeMult163.hecke_mult163
#print axioms HeckeMult163.hecke_mult163_pow
#print axioms HeckeMult163.cw163

#print axioms: 8 theorems, no axioms.

78.325 Factorisation (\(d=163\))

The \(H_{163}\)-elements of norm \(p^k\) are the \(\pi ^j\bar \pi ^l\).

\[ \alpha =\pi ^j\bar \pi ^{\,l}. \]

lang/Prime163.lean

/-
  lang/Prime163 — T48/T50, T48: ℤ[(1+√−163)/2] above a split prime: π of prime norm is prime, N(α) = p^k ⇒ α = uπ^jπ̄^l, and the
  H163-elements of norm p^k are the π^j π̄^l (H163 conjugation-stable).  (Port of EisPrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult163

namespace Prime163

open FTAChain DetChain LibInt Euc163 Prim163 Uniq163 Frobenius

variable (P : Nat)

theorem nrm_mul (x y : Int × Int) : nrm ((Ring163.ring163 LibInt.intR).mul x y) = nrm x * nrm y :=
  Int.ofNat.inj (by show Int.ofNat _ = Int.ofNat (nrm x) * Int.ofNat (nrm y); rw [nrm_cast, nrm_cast, nrm_cast, en_mul])

theorem unit_inv (u : Int × Int) (h : nrm u = 1) : (Ring163.ring163 LibInt.intR).mul u (cj u) = (Ring163.ring163 LibInt.intR).one := by
  rw [mul_cj, ← nrm_cast, h]; rfl

/-- **An element of prime norm is prime.** -/
theorem eprime_div (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (x y : Int × Int)
    (h : Dvd' π ((Ring163.ring163 LibInt.intR).mul x y)) : Dvd' π x ∨ Dvd' π y := by
  match egcd_exists π x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = P + 1 + 1 := by rw [← nrm_mul, ← hc1, hπ]
    cases hp (nrm d) ⟨nrm c1, by rw [← hdc]; lib_nat⟩ with
    | inl h1 =>
      refine Or.inr (eeuclid π x y ((Ring163.ring163 LibInt.intR).mul (cj d) X) ((Ring163.ring163 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (cj d) X) π) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (cj d) Y) x)
          = (Ring163.ring163 LibInt.intR).mul (cj d) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul X π) ((Ring163.ring163 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring163.ring163 LibInt.intR).mul_comm, unit_inv d h1]
    | inr h1 =>
      have nc : nrm c1 = 1 := by
        have e : (P + 1 + 1) * nrm c1 = (P + 1 + 1) * 1 := by rw [← h1, hdc, h1]; lib_nat
        exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
      refine Or.inl ⟨(Ring163.ring163 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring163.ring163 LibInt.intR).mul π (cj c1) := by
        rw [hc1]
        calc d = (Ring163.ring163 LibInt.intR).mul d (Ring163.ring163 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 nc]; lib_ring
      rw [hc2, hd']; lib_ring

theorem cj_mul (x y : Int × Int) : cj ((Ring163.ring163 LibInt.intR).mul x y) = (Ring163.ring163 LibInt.intR).mul (cj x) (cj y) := by
  apply Prod.ext
  · show x.1 * y.1 + -(x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 * y.2 + x.2 *  -- (+756 chars; full line in the Zenodo source)
    lib_int
  · show -(x.1 * y.2 + x.2 * y.1 + x.2 * y.2) = (x.1 + x.2) * -y.2 + -x.2 * (y.1 + y.2) + -x.2 * -y.2
    lib_int

theorem cj_cj (z : Int × Int) : cj (cj z) = z := by
  apply Prod.ext
  · show z.1 + z.2 + -z.2 = z.1; lib_int
  · show - -z.2 = z.2; lib_int

theorem nrm_cj (z : Int × Int) : nrm (cj z) = nrm z := by unfold nrm; rw [en_cj]

theorem nrm_one_eq (z : Int × Int) (h : nrm z = 1) : en z = 1 := by rw [← nrm_cast, h]; rfl

/-- **Factorisation above a split prime.** -/
theorem factor3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ k → ∃ u j l, nrm u = 1 ∧ j + l = k ∧
      α = (Ring163.ring163 LibInt.intR).mul u ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) (pw (Ring163.ring163 LibInt.intR) (cj π) l))
  | 0, α, h => ⟨α, 0, 0, h, rfl, by show α = (Ring163.ring163 LibInt.intR).mul α ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).one (Ring163.ring163 LibInt.intR).one); lib_ring⟩
  | k + 1, α, h => by
    -- π | α ᾱ
    have hdiv : Dvd' π ((Ring163.ring163 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Ring163.ring163 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h]
      rw [show (Ring163.ring163 LibInt.intR).mul π ((Ring163.ring163 LibInt.intR).mul (cj π) (Int.ofNat ((P + 1 + 1) ^ k), 0)) =
        (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul π (cj π)) (Int.ofNat ((P + 1 + 1) ^ k), 0) by lib_ring, mul_cj, ← nrm_cast, hπ]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) ^ k * (P + 1 + 1)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0)
        show Int.ofNat ((P + 1 + 1) ^ k) * Int.ofNat (P + 1 + 1) = _; lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) ^ k) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring163.ring163 LibInt.intR).mul π β) = (P + 1 + 1) ^ (k + 1) → nrm β = (P + 1 + 1) ^ k := fun β hb => by
      rw [nrm_mul, hπ] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) (hb.trans (Nat.mul_comm ((P + 1 + 1) ^ k) (P + 1 + 1)))
    cases eprime_div P hp π hπ α (cj α) hdiv with
    | inl hα =>
      obtain ⟨β, hβ⟩ := hα
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k β (cancel β (by rw [← hβ]; exact h))
      refine ⟨u, j + 1, l, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hβ, he]; show _ = (Ring163.ring163 LibInt.intR).mul u ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) π) _); lib_ring
    | inr hα =>
      obtain ⟨β, hβ⟩ := hα
      have hα' : α = (Ring163.ring163 LibInt.intR).mul (cj π) (cj β) := by rw [← cj_mul, ← hβ, cj_cj]
      have hb : nrm (cj β) = (P + 1 + 1) ^ k := by
        rw [nrm_cj]; apply cancel
        rw [← hβ, nrm_cj]; exact h
      obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 hp π hπ k (cj β) hb
      refine ⟨u, j, l + 1, hu, by rw [← hjl]; lib_nat, ?_⟩
      rw [hα', he]; show _ = (Ring163.ring163 LibInt.intR).mul u ((Ring163.ring163 LibInt.intR).mul _ ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) (cj π) l) (cj π))); lib_ring

theorem prim163_one : Prim163 (Ring163.ring163 LibInt.intR).one := by
  show QChar.chi 80 (Prim163.cr (1, 0)) = 1
  rw [show Prim163.cr (1, 0) = (ZMod.ring (2 * 80 + 1 + 1)).mul (ZMod.ring (2 * 80 + 1 + 1)).one (ZMod.ring (2 * 80 + 1 + 1)).one from rfl]
  exact QChar.chi_sq 80 Prim163.prime163 _ (PowerSum.one_ne_zero' (2 * 80 + 1))

theorem prim163_pow (z : Int × Int) (h : Prim163 z) : ∀ j, Prim163 (pw (Ring163.ring163 LibInt.intR) z j)
  | 0 => prim163_one
  | j + 1 => prim_mul _ _ (prim163_pow z h j) h

/-- **The primary elements of norm p^k are the π^j π̄^l.** -/
theorem primary_only3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π)
    (k : Nat) (α : Int × Int) (hα : nrm α = (P + 1 + 1) ^ k) (pα : Prim163 α) :
    ∃ j l, j + l = k ∧ α = (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) (pw (Ring163.ring163 LibInt.intR) (cj π) l) := by
  obtain ⟨u, j, l, hu, hjl, he⟩ := factor3 P hp π hπ k α hα
  have pw' := prim_mul _ _ (prim163_pow π pπ j) (prim163_pow (cj π) (prim_cj π pπ) l)
  have u1 := unit_one u _ (nrm_one_eq u hu) pw' (by rw [← he]; exact pα)
  exact ⟨j, l, hjl, by rw [he, u1]; lib_ring⟩

end Prime163

#print axioms Prime163.nrm_mul
#print axioms Prime163.unit_inv
#print axioms Prime163.eprime_div
#print axioms Prime163.cj_mul
#print axioms Prime163.cj_cj
#print axioms Prime163.nrm_cj
#print axioms Prime163.nrm_one_eq
#print axioms Prime163.factor3
#print axioms Prime163.prim163_one
#print axioms Prime163.prim163_pow
#print axioms Prime163.primary_only3

#print axioms: 11 theorems, no axioms.

78.326 26569a: the Hecke recursion

Hecke recursion at split prime powers.

\[ c_W(p^{k+2})=(W(\pi )+W(\bar \pi ))c_W(p^{k+1})-W(\pi )W(\bar \pi )c_W(p^k). \]

lang/HeckePrime163.lean

/-
  lang/HeckePrime163 — T48/T50, T48: **ℚ(√−163)'s Hecke character at split prime powers**: π ∈ H163 of prime norm p, W multiplicative:
  c_W(p^k) = Σ_{j+l=k} W(π)^j W(π̄)^l and the Hecke recursion.  (Port of HeckePrime3.)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.Prime163
import lang.HeckePrimeW

namespace HeckePrime163

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc163 Prim163 Uniq163 Prime163 HeckeMult163

variable (P : Nat)

theorem pow_ne0 : ∀ j, (P + 1 + 1) ^ j ≠ 0
  | 0 => fun e => Nat.noConfusion e
  | j + 1 => fun e => by
    cases nat_mul_zero _ _ e with
    | inl h => exact pow_ne0 j h
    | inr h => exact Nat.noConfusion h

theorem nrm_pw (z : Int × Int) : ∀ j, nrm (pw (Ring163.ring163 LibInt.intR) z j) = nrm z ^ j
  | 0 => rfl
  | j + 1 => by
    show nrm ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) z) = nrm z ^ j * nrm z
    rw [nrm_mul, nrm_pw z j]

theorem npow_add (x : Nat) (a : Nat) : ∀ b, x ^ a * x ^ b = x ^ (a + b)
  | 0 => Nat.mul_one _
  | b + 1 => by
    show x ^ a * (x ^ b * x) = x ^ (a + b) * x
    rw [← npow_add x a b]; lib_nat

theorem en_ne (z : Int × Int) (N : Nat) (h : nrm z = N) (hN : N ≠ 0) : en z ≠ 0 := fun e => by
  apply hN; rw [← h]; unfold nrm; rw [e]; rfl

theorem cancel_pw3 (z : Int × Int) (hz : nrm z = P + 1 + 1) (j : Nat) (A B : Int × Int)
    (h : (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) A = (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) B) : A = B := by
  have h0 : (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) ((Ring163.ring163 LibInt.intR).add A ((Ring163.ring163 LibInt.intR).neg B)) = (Ring163.ring163 LibInt.intR).zero := by
    rw [show (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) ((Ring163.ring163 LibInt.intR).add A ((Ring163.ring163 LibInt.intR).neg B)) = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) A) ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) B)) by lib_ring,  -- (+13 chars; full line in the Zenodo source)
  have := Cop163.cancel_left _ _ (en_ne _ _ (by rw [nrm_pw, hz]) (pow_ne0 P j)) h0
  calc A = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).add A ((Ring163.ring163 LibInt.intR).neg B)) B := by lib_ring
    _ = B := by rw [this]; lib_ring

theorem no_assoc3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π)
    (h : Dvd' π (cj π)) : False := by
  obtain ⟨c, hc⟩ := h
  have nc : nrm c = 1 := by
    have e : (P + 1 + 1) * nrm c = (P + 1 + 1) * 1 := by rw [← hπ, ← nrm_mul, ← hc, nrm_cj]; lib_nat
    exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) e
  have c1 := unit_one c π (nrm_one_eq c nc) pπ (by rw [(Ring163.ring163 LibInt.intR).mul_comm, ← hc]; exact prim_cj π pπ)
  rw [c1] at hc
  have hc2 : cj π = π := by rw [hc]; lib_ring
  obtain ⟨a, b⟩ := π
  have hb2 : -b = b := congrArg Prod.snd hc2
  have hb0 : b = 0 := QChar.int_two_zero b (by rw [show b + b = b + -(-b) by lib_int, hb2]; lib_int)
  have hen : en (a, b) = Int.ofNat (P + 1 + 1) := by rw [← nrm_cast, hπ]
  have hsq : Int.natAbs a * Int.natAbs a = P + 1 + 1 := Int.ofNat.inj (by
    rw [GaussEuclid.nas, ← hen]; show a * a = a * a + a * b + 41 * (b * b); rw [hb0]; lib_int)
  cases hp (Int.natAbs a) ⟨Int.natAbs a, hsq.symm⟩ with
  | inl h1' => rw [h1'] at hsq; exact Nat.noConfusion (Nat.succ.inj hsq).symm
  | inr h1' =>
    rw [h1'] at hsq
    have : (P + 1 + 1) * (P + 1 + 1) = (P + 1 + 1) * 1 := hsq.trans (Nat.mul_one _).symm
    have := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ _) this
    exact Nat.noConfusion (Nat.succ.inj this)

theorem no_div3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) :
    ∀ d, Dvd' π (pw (Ring163.ring163 LibInt.intR) (cj π) d) → False
  | 0, ⟨c, hc⟩ => by
    have : (P + 1 + 1) * nrm c = 1 := by rw [← hπ, ← nrm_mul, ← hc]; rfl
    cases h' : nrm c with
    | zero => rw [h'] at this; exact Nat.noConfusion this
    | succ b => rw [h', show (P + 1 + 1) * (b + 1) = (P + 1 + 1) * b + P + 1 + 1 by lib_nat] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | d + 1, h => by
    cases eprime_div P hp π hπ _ _ h with
    | inl h1 => exact no_div3 hp π hπ pπ d h1
    | inr h1 => exact no_assoc3 P hp π hπ pπ h1

theorem distinct_lt3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) (j d l' : Nat)
    (h : (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) (pw (Ring163.ring163 LibInt.intR) (cj π) (l' + 1 + d)) = (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π (j + 1 + d)) (pw (Ring163.ring163 LibInt.intR) (cj π) l')) : False := by
  rw [show l' + 1 + d = l' + (1 + d) by lib_nat, HasseCM.pw_add' (Ring163.ring163 LibInt.intR) (cj π) l' (1 + d),
    show j + 1 + d = j + (1 + d) by lib_nat, HasseCM.pw_add' (Ring163.ring163 LibInt.intR) π j (1 + d)] at h
  have h' : (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) (cj π) l') (pw (Ring163.ring163 LibInt.intR) (cj π) (1 + d)))
      = (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) (cj π) l') (pw (Ring163.ring163 LibInt.intR) π (1 + d))) := by
    calc _ = (Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) (pw (Ring163.ring163 LibInt.intR) π (1 + d))) (pw (Ring163.ring163 LibInt.intR) (cj π) l') := h
      _ = _ := by lib_ring
  have h1 := cancel_pw3 P π hπ j _ _ h'
  have h2 := cancel_pw3 P (cj π) (by rw [nrm_cj, hπ]) l' _ _ h1
  apply no_div3 P hp π hπ pπ (1 + d)
  refine ⟨pw (Ring163.ring163 LibInt.intR) π d, ?_⟩
  rw [h2, show 1 + d = d + 1 by lib_nat]; show (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π d) π = _; lib_ring

/-- **π^j π̄^l are distinct.** -/
theorem distinct3 (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) (j l j' l' : Nat) (hs : j + l = j' + l')
    (he : (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j) (pw (Ring163.ring163 LibInt.intR) (cj π) l) = (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π j') (pw (Ring163.ring163 LibInt.intR) (cj π) l')) : j = j' ∧ l = l' := by
  cases Nat.lt_or_ge j j' with
  | inl hlt =>
    exfalso
    match le_dest hlt with
    | ⟨d, hd⟩ =>
      have hl : l = l' + 1 + d := LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, hd]; lib_nat)
      rw [hd, hl] at he
      exact distinct_lt3 P hp π hπ pπ j d l' he
  | inr hge =>
    cases Nat.lt_or_ge j' j with
    | inl hlt =>
      exfalso
      match le_dest hlt with
      | ⟨d, hd⟩ =>
        have hl : l' = l + 1 + d := LibInt.nadd_right_cancel j' (by rw [show l' + j' = j' + l' by lib_nat, ← hs, hd]; lib_nat)
        rw [hd, hl] at he
        exact distinct_lt3 P hp π hπ pπ j' d l he.symm
    | inr hle =>
      have e : j = j' := Nat.le_antisymm hle hge
      exact ⟨e, LibInt.nadd_right_cancel j (by rw [show l + j = j + l by lib_nat, hs, e]; lib_nat)⟩

def w3 (π : Int × Int) (x : Nat × Nat) : Int × Int := (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) π x.1) (pw (Ring163.ring163 LibInt.intR) (cj π) x.2)

theorem w3_PD (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) (k : Nat) (x : Nat × Nat) (hx : x.1 + x.2 = k) :
    PD163 ((P + 1 + 1) ^ k) (w3 π x) := by
  refine ⟨?_, ?_⟩
  · rw [← nrm_cast]; unfold w3
    rw [nrm_mul, nrm_pw, nrm_pw, nrm_cj, hπ, npow_add, hx]
  · exact prim_mul _ _ (prim163_pow π pπ _) (prim163_pow (cj π) (prim_cj π pπ) _)

/-- **c_W(p^k) = Σ_{j+l=k} W(π^j π̄^l).** -/
theorem cw3_pk (W : Int × Int → Int × Int) (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int)
    (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) (k : Nat) :
    cw163 W ((P + 1 + 1) ^ k) = sumK (Ring163.ring163 LibInt.intR) (fun x => W (w3 π x)) (HeckePrime.Wl k) := by
  have hI : ∀ z, I163 ((P + 1 + 1) ^ k) z = sumK (Ring163.ring163 LibInt.intR) (fun x => delta (Ring163.ring163 LibInt.intR) z (w3 π x)) (HeckePrime.Wl k) := fun z => by
    by_cases hz : PD163 ((P + 1 + 1) ^ k) z
    · rw [I163_pos _ _ hz]
      have hN : nrm z = (P + 1 + 1) ^ k := by unfold nrm; rw [hz.1]; rfl
      match primary_only3 P hp π hπ pπ k z hN hz.2 with
      | ⟨j, l, hjl, hx⟩ =>
        have hz' : z = w3 π (j, l) := hx
        rw [HeckePrime.sumK_ext_all (Ring163.ring163 LibInt.intR) k _ (fun x => delta (Ring163.ring163 LibInt.intR) x (j, l)) (fun x hxk => by
          unfold delta
          by_cases hxe : x = (j, l)
          · rw [ite_pos' _ _ hxe, ite_pos' _ _ (by rw [hz', hxe])]
          · rw [ite_neg' _ _ hxe, ite_neg' _ _ (fun e => hxe (by
              have d := distinct3 P hp π hπ pπ j l x.1 x.2 (hjl.trans hxk.symm) (hz'.symm.trans e)
              exact Prod.ext d.1.symm d.2.symm))]) _ (HeckePrime.allk_Wl k),
          HeckePrime.count_Wl (Ring163.ring163 LibInt.intR) k j l hjl]
    · rw [I163_neg _ _ hz]
      rw [HeckePrime.sumK_ext_all (Ring163.ring163 LibInt.intR) k _ (fun _ => (Ring163.ring163 LibInt.intR).zero) (fun x hxk => by
        unfold delta; rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact w3_PD P π hπ pπ k x hxk))]) _ (HeckePrime.allk_Wl k)]
      exact (HeckePrime.zsumR (Ring163.ring163 LibInt.intR) _).symm
  unfold cw163
  rw [sumK_ext (Ring163.ring163 LibInt.intR) _ _ (fun z => by rw [hI z, sumK_mul])]
  rw [sumK_swap]
  refine HeckePrime.sumK_ext_all (Ring163.ring163 LibInt.intR) k _ _ (fun x hxk => ?_) _ (HeckePrime.allk_Wl k)
  have hb := abs_le163 (w3 π x) _ (w3_PD P π hπ pπ k x hxk).1
  exact HeckeMult.pick_box (Ring163.ring163 LibInt.intR) (fun z => W z) (w3 π x) _ hb.1 hb.2

theorem W_pow3 (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring163.ring163 LibInt.intR).one = (Ring163.ring163 LibInt.intR).one) (z : Int × Int) :
    ∀ j, W (pw (Ring163.ring163 LibInt.intR) z j) = pw (Ring163.ring163 LibInt.intR) (W z) j
  | 0 => hW1
  | j + 1 => by
    show W ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) z j) z) = (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) (W z) j) (W z)
    rw [hW, W_pow3 W hW hW1 z j]

theorem cw3_pk' (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring163.ring163 LibInt.intR).one = (Ring163.ring163 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) (k : Nat) :
    cw163 W ((P + 1 + 1) ^ k) = HeckePrime.Tg (Ring163.ring163 LibInt.intR) (W π) (W (cj π)) k := by
  rw [cw3_pk P W hp π hπ pπ k]
  exact sumK_ext _ _ _ (fun x => by unfold w3; rw [hW, W_pow3 W hW hW1, W_pow3 W hW hW1]) _

/-- **The Hecke recursion for every multiplicative weight on ℤ[ω].** -/
theorem cw3_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring163.ring163 LibInt.intR).one = (Ring163.ring163 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π) (k : Nat) :
    cw163 W ((P + 1 + 1) ^ (k + 2)) = (Ring163.ring163 LibInt.intR).add
      ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add (W π) (W (cj π))) (cw163 W ((P + 1 + 1) ^ (k + 1))))
      ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (W π) (W (cj π))) (cw163 W ((P + 1 + 1) ^ k)))) := by
  rw [cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, cw3_pk' P W hW hW1 hp π hπ pπ, HeckePrime.Tg_rec]

end HeckePrime163

#print axioms HeckePrime163.pow_ne0
#print axioms HeckePrime163.nrm_pw
#print axioms HeckePrime163.npow_add
#print axioms HeckePrime163.en_ne
#print axioms HeckePrime163.cancel_pw3
#print axioms HeckePrime163.no_assoc3
#print axioms HeckePrime163.no_div3
#print axioms HeckePrime163.distinct_lt3
#print axioms HeckePrime163.distinct3
#print axioms HeckePrime163.w3_PD
#print axioms HeckePrime163.cw3_pk
#print axioms HeckePrime163.W_pow3
#print axioms HeckePrime163.cw3_pk'
#print axioms HeckePrime163.cw3_rec

#print axioms: 14 theorems, no axioms.

78.327 26569a: inert primes

A non-residue mod \(163\) stays prime; \((-p)^k\) is the only \(H_{163}\)-element of norm \(p^{2k}\).

\[ c_W(p^{2k})=W((-p)^k),\qquad c_W(p^{2k+1})=0. \]

lang/Inert163.lean

/-
  lang/Inert163 — T48/T50: **the ℚ(√−163) Hecke character at inert primes** (χ₁₆₃(p) = −1), via the Legendre symbol:
    cast_en, en_mod163   N(z) ≡ ρ(z)² (mod 163) (164N = ρ² + 163a²), so χ₁₆₃(N(z)) ≠ −1
    cw163_zero, inert_div, factor_inert, prim_inert, cw163_even, pow_odd, cw163_odd   (port of Inert19)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime163

namespace Inert163

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Euc163 Prim163 Uniq163 Prime163 HeckeMult163

variable (P : Nat)

/-- E (a² + ab + t b²) = (a + c b)² + D a² when 2c = E, c² = E t, D + 1 = E. -/
theorem sqgen2 (a b c D E t : Int) (h1 : 2 * c = E) (h2 : c * c = E * t) (h3 : D + 1 = E) :
    E * (a * a + a * b + t * (b * b)) = (a + c * b) * (a + c * b) + D * (a * a) := by
  calc E * (a * a + a * b + t * (b * b)) = E * (a * a) + E * (a * b) + (E * t) * (b * b) := by lib_int
    _ = (D + 1) * (a * a) + (2 * c) * (a * b) + (c * c) * (b * b) := by rw [h3, h1, h2]
    _ = _ := by lib_int

theorem cast_en (z : Int × Int) : LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (en z) = (ZMod.ring (2 * 80 + 1 + 1)).mul (Prim163.cr z) (Prim163.cr z) := by
  have e44 : 164 * en z = Prim163.rho z * Prim163.rho z + 163 * (z.1 * z.1) :=
    sqgen2 z.1 z.2 82 163 164 41 rfl rfl rfl
  have h1 : LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (164 * en z) = LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (en z) := by
    rw [show (164 : Int) * en z = 163 * en z + en z by lib_int, IntCast.castZ_add, Prim163.cz163]; lib_ring
  rw [← h1, e44, IntCast.castZ_add, HasseDavenport.castZ_mul, Prim163.cz163]; unfold Prim163.cr; lib_ring

/-- N(z) is never a non-residue mod 163. -/
theorem en_mod163 (z : Int × Int) (n : Nat) (hn : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat n)) = -1) (h : en z = Int.ofNat n) : False := by
  rw [← h, cast_en] at hn
  by_cases h0 : Prim163.cr z = (ZMod.ring (2 * 80 + 1 + 1)).zero
  · rw [h0, show (ZMod.ring (2 * 80 + 1 + 1)).mul (ZMod.ring (2 * 80 + 1 + 1)).zero (ZMod.ring (2 * 80 + 1 + 1)).zero = (ZMod.ring (2 * 80 + 1 + 1)).zero by lib_ring, QChar.chi_zero] at hn; exact Int.noConfusion hn
  · rw [QChar.chi_sq 80 Prim163.prime163 _ h0] at hn; exact Int.noConfusion hn

/-- **c_W(n) = 0 for n a non-residue mod 163.** -/
theorem cw163_zero (W : Int × Int → Int × Int) (n : Nat) (hn : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat n)) = -1) : cw163 W n = (Ring163.ring163 LibInt.intR).zero := by
  unfold cw163
  rw [sumK_ext (Ring163.ring163 LibInt.intR) _ (fun _ => (Ring163.ring163 LibInt.intR).zero) (fun z => by
    rw [I163_neg _ _ (fun h => en_mod163 z n hn h.1)]; lib_ring)]
  exact zsum163 _

def Pi : Int × Int := (Int.ofNat (P + 1 + 1), 0)

theorem en_Pi : en (Pi P) = Int.ofNat ((P + 1 + 1) * (P + 1 + 1)) := by
  show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) + Int.ofNat (P + 1 + 1) * 0 + 41 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_Pi : nrm (Pi P) = (P + 1 + 1) * (P + 1 + 1) := Int.ofNat.inj (by rw [nrm_cast, en_Pi])

theorem cj_Pi : cj (Pi P) = Pi P := Prod.ext (by show Int.ofNat (P + 1 + 1) + 0 = Int.ofNat (P + 1 + 1); lib_int) (by show -(0 : Int) = 0; rfl)

theorem not_p (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (z : Int × Int) (h : nrm z = P + 1 + 1) : False :=
  en_mod163 z (P + 1 + 1) hc (by rw [← nrm_cast, h])

theorem dvd_conv (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) : ∀ d, d ∣ (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1 :=
  fun d ⟨c, hc⟩ => hpr d ⟨c, by rw [hc]; lib_nat⟩

/-- the two factor case: a b = p², p ∤ … ⇒ a = 1 or b = 1 (a = p is excluded separately). -/
theorem split_sq (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Nat) (h : a * b = (P + 1 + 1) * (P + 1 + 1)) :
    a = 1 ∨ b = 1 ∨ a = P + 1 + 1 := by
  cases euclid_prime (dvd_conv P hpr) (show (P + 1 + 1) ∣ a * b from ⟨P + 1 + 1, h⟩) with
  | inl ha =>
    obtain ⟨s, hs⟩ := ha
    have e : s * b = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by rw [show (P + 1 + 1) * (s * b) = (P + 1 + 1) * s * b by lib_nat, ← hs, h])
    cases hpr s ⟨b, by rw [← e]; lib_nat⟩ with
    | inl s1 => exact Or.inr (Or.inr (by rw [hs, s1]; lib_nat))
    | inr sp =>
      refine Or.inr (Or.inl ?_)
      rw [sp] at e
      exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (e.trans (Nat.mul_one _).symm)
  | inr hb =>
    obtain ⟨s, hs⟩ := hb
    have e : a * s = P + 1 + 1 := Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ (P + 1)) (by
      rw [show (P + 1 + 1) * (a * s) = a * ((P + 1 + 1) * s) by lib_nat, ← hs, h])
    cases hpr a ⟨s, by rw [← e]; lib_nat⟩ with
    | inl a1 => exact Or.inl a1
    | inr ap => exact Or.inr (Or.inr ap)

/-- **p ≡ 2 (mod 3) stays prime in ℤ[ω].** -/
theorem inert_div (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (x y : Int × Int)
    (h : Dvd' (Pi P) ((Ring163.ring163 LibInt.intR).mul x y)) : Dvd' (Pi P) x ∨ Dvd' (Pi P) y := by
  match egcd_exists (Pi P) x with
  | ⟨d, X, Y, hd, ⟨c1, hc1⟩, ⟨c2, hc2⟩⟩ =>
    have hdc : nrm d * nrm c1 = (P + 1 + 1) * (P + 1 + 1) := by rw [← nrm_mul, ← hc1, nrm_Pi]
    rcases split_sq P hpr _ _ hdc with h1 | h1 | h1
    · refine Or.inr (eeuclid (Pi P) x y ((Ring163.ring163 LibInt.intR).mul (cj d) X) ((Ring163.ring163 LibInt.intR).mul (cj d) Y) ?_ h)
      rw [show (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (cj d) X) (Pi P)) ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (cj d) Y) x)
          = (Ring163.ring163 LibInt.intR).mul (cj d) ((Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul X (Pi P)) ((Ring163.ring163 LibInt.intR).mul Y x)) by lib_ring,
        ← hd, (Ring163.ring163 LibInt.intR).mul_comm, unit_inv d h1]
    · refine Or.inl ⟨(Ring163.ring163 LibInt.intR).mul (cj c1) c2, ?_⟩
      have hd' : d = (Ring163.ring163 LibInt.intR).mul (Pi P) (cj c1) := by
        rw [hc1]
        calc d = (Ring163.ring163 LibInt.intR).mul d (Ring163.ring163 LibInt.intR).one := by lib_ring
          _ = _ := by rw [← unit_inv c1 h1]; lib_ring
      rw [hc2, hd']; lib_ring
    · exact (not_p P hc d h1).elim

theorem nat_pow2 (k : Nat) : (P + 1 + 1) ^ (2 * (k + 1)) = (P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k) := by
  rw [show 2 * (k + 1) = 2 * k + 1 + 1 by lib_nat]
  show (P + 1 + 1) ^ (2 * k) * (P + 1 + 1) * (P + 1 + 1) = _
  lib_nat

/-- **Factorisation above an inert prime.** -/
theorem factor_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : ∀ k (α : Int × Int),
    nrm α = (P + 1 + 1) ^ (2 * k) → ∃ u, nrm u = 1 ∧ α = (Ring163.ring163 LibInt.intR).mul u (pw (Ring163.ring163 LibInt.intR) (Pi P) k)
  | 0, α, h => ⟨α, h, by show α = (Ring163.ring163 LibInt.intR).mul α (Ring163.ring163 LibInt.intR).one; lib_ring⟩
  | k + 1, α, h => by
    have hdiv : Dvd' (Pi P) ((Ring163.ring163 LibInt.intR).mul α (cj α)) := by
      refine ⟨(Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)), 0), ?_⟩
      rw [mul_cj, ← nrm_cast, h, nat_pow2]
      apply Prod.ext
      · show Int.ofNat ((P + 1 + 1) * (P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) = Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0)
        show Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k)) = _
        show _ = Int.ofNat (P + 1 + 1) * (Int.ofNat (P + 1 + 1) * Int.ofNat ((P + 1 + 1) ^ (2 * k))) + -(0 * 0)
        lib_int
      · show (0 : Int) = Int.ofNat (P + 1 + 1) * 0 + 0 * Int.ofNat ((P + 1 + 1) * (P + 1 + 1) ^ (2 * k)) + -(0 * 0); lib_int
    have cancel : ∀ β : Int × Int, nrm ((Ring163.ring163 LibInt.intR).mul (Pi P) β) = (P + 1 + 1) ^ (2 * (k + 1)) → nrm β = (P + 1 + 1) ^ (2 * k) := fun β hb => by
      rw [nrm_mul, nrm_Pi, nat_pow2] at hb
      exact Nat.eq_of_mul_eq_mul_left (Nat.mul_pos (Nat.zero_lt_succ _) (Nat.zero_lt_succ _)) hb
    have hβ : ∃ β, α = (Ring163.ring163 LibInt.intR).mul (Pi P) β := by
      cases inert_div P hpr hc α (cj α) hdiv with
      | inl hα => exact hα
      | inr hα =>
        obtain ⟨β, hβ⟩ := hα
        exact ⟨cj β, by rw [← cj_Pi P, ← cj_mul, ← hβ, cj_cj]⟩
    obtain ⟨β, hβ⟩ := hβ
    obtain ⟨u, hu, he⟩ := factor_inert hpr hc k β (cancel β (by rw [← hβ]; exact h))
    refine ⟨u, hu, ?_⟩
    rw [hβ, he]; show _ = (Ring163.ring163 LibInt.intR).mul u ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) (Pi P) k) (Pi P)); lib_ring

def nPi : Int × Int := (-Int.ofNat (P + 1 + 1), 0)

theorem prim_nPi (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : Prim163 (nPi P) := by
  show QChar.chi 80 (Prim163.cr (nPi P)) = 1
  have e : Prim163.cr (nPi P) = (ZMod.ring (2 * 80 + 1 + 1)).neg (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) := by
    unfold Prim163.cr; rw [show Prim163.rho (nPi P) = -Int.ofNat (P + 1 + 1) by show -Int.ofNat (P + 1 + 1) + 82 * 0 = _; lib_int, HasseDavenport.castZ_neg]
  rw [e, Prim163.chi_neg, hc]; rfl

theorem pw_neg (k : Nat) : pw (Ring163.ring163 LibInt.intR) (Pi P) k =
    (Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) ((Ring163.ring163 LibInt.intR).neg (Ring163.ring163 LibInt.intR).one) k) (pw (Ring163.ring163 LibInt.intR) (nPi P) k) := by
  rw [← Frobenius.pw_mul]
  congr 1
  apply Prod.ext
  · show Int.ofNat (P + 1 + 1) = -1 * -Int.ofNat (P + 1 + 1) + -(-0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0 * 0 + -0  -- (+41 chars; full line in the Zenodo source)
  · show (0 : Int) = -1 * 0 + -0 * -Int.ofNat (P + 1 + 1) + -0 * 0; lib_int

theorem nrm_neg_one_pw : ∀ k, nrm (pw (Ring163.ring163 LibInt.intR) ((Ring163.ring163 LibInt.intR).neg (Ring163.ring163 LibInt.intR).one) k) = 1
  | 0 => rfl
  | k + 1 => by
    show nrm ((Ring163.ring163 LibInt.intR).mul _ _) = 1
    rw [nrm_mul, nrm_neg_one_pw k]; rfl

/-- **The primary elements of norm p^(2k) are (−p)^k.** -/
theorem prim_inert (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) (α : Int × Int)
    (hα : nrm α = (P + 1 + 1) ^ (2 * k)) (pα : Prim163 α) : α = pw (Ring163.ring163 LibInt.intR) (nPi P) k := by
  obtain ⟨u, hu, he⟩ := factor_inert P hpr hc k α hα
  rw [pw_neg] at he
  generalize hv : (Ring163.ring163 LibInt.intR).mul u (pw (Ring163.ring163 LibInt.intR) ((Ring163.ring163 LibInt.intR).neg (Ring163.ring163 LibInt.intR).one) k) = v
  have he' : α = (Ring163.ring163 LibInt.intR).mul v (pw (Ring163.ring163 LibInt.intR) (nPi P) k) := by
    rw [he, ← hv]; lib_ring
  have hv1 : nrm v = 1 := by rw [← hv, nrm_mul, hu, nrm_neg_one_pw]
  have v1 := unit_one v _ (nrm_one_eq v hv1) (prim163_pow _ (prim_nPi P hc) k) (by rw [← he']; exact pα)
  rw [he', v1]; lib_ring

theorem nrm_nPi : nrm (nPi P) = (P + 1 + 1) * (P + 1 + 1) := by
  apply Int.ofNat.inj; rw [nrm_cast]
  show -Int.ofNat (P + 1 + 1) * -Int.ofNat (P + 1 + 1) + -Int.ofNat (P + 1 + 1) * 0 + 41 * (0 * 0) = Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)
  lib_int

theorem nrm_nPi_pw (k : Nat) : nrm (pw (Ring163.ring163 LibInt.intR) (nPi P) k) = (P + 1 + 1) ^ (2 * k) := by
  rw [HeckePrime163.nrm_pw, nrm_nPi]
  induction k with
  | zero => rfl
  | succ k ih =>
    show ((P + 1 + 1) * (P + 1 + 1)) ^ k * ((P + 1 + 1) * (P + 1 + 1)) = _
    rw [ih, nat_pow2]; lib_nat

/-- **c_W(p^(2k)) = W((−p)^k)** at an inert prime. -/
theorem cw163_even (W : Int × Int → Int × Int) (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) :
    cw163 W ((P + 1 + 1) ^ (2 * k)) = W (pw (Ring163.ring163 LibInt.intR) (nPi P) k) := by
  have hPD : PD163 ((P + 1 + 1) ^ (2 * k)) (pw (Ring163.ring163 LibInt.intR) (nPi P) k) :=
    ⟨by rw [← nrm_cast, nrm_nPi_pw], prim163_pow _ (prim_nPi P hc) k⟩
  unfold cw163
  rw [sumK_ext (Ring163.ring163 LibInt.intR) _ (fun z => (Ring163.ring163 LibInt.intR).mul (delta (Ring163.ring163 LibInt.intR) z (pw (Ring163.ring163 LibInt.intR) (nPi P) k)) (W z)) (fun z => by
    by_cases hz : PD163 ((P + 1 + 1) ^ (2 * k)) z
    · have e := prim_inert P hpr hc k z (Int.ofNat.inj (by rw [nrm_cast, hz.1])) hz.2
      rw [I163_pos _ _ hz]; unfold delta; rw [ite_pos' _ _ e]
    · rw [I163_neg _ _ hz]; unfold delta
      rw [ite_neg' _ _ (fun e => hz (by rw [e]; exact hPD))])]
  have hb := abs_le163 _ _ hPD.1
  exact HeckeMult.pick_box (Ring163.ring163 LibInt.intR) W _ _ hb.1 hb.2

theorem pow_odd (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) : ∀ k, QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)))) = -1
  | 0 => by rw [show (P + 1 + 1) ^ (2 * 0 + 1) = P + 1 + 1 from Nat.one_mul _]; exact hc
  | k + 1 => by
    rw [show (P + 1 + 1) ^ (2 * (k + 1) + 1) = (P + 1 + 1) ^ (2 * k + 1) * ((P + 1 + 1) * (P + 1 + 1)) by
      rw [show 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 by lib_nat]; show (P + 1 + 1) ^ (2 * k + 1) * (P + 1 + 1) * (P + 1 + 1) = _; lib_nat]
    show QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat ((P + 1 + 1) ^ (2 * k + 1)) * (Int.ofNat (P + 1 + 1) * Int.ofNat (P + 1 + 1)))) = -1
    rw [HasseDavenport.castZ_mul, HasseDavenport.castZ_mul, QChar.chi_mul 80 Prim163.prime163, QChar.chi_mul 80 Prim163.prime163, pow_odd hc k, hc]; rfl

/-- **c_W(p^(2k+1)) = 0** at an inert prime. -/
theorem cw163_odd (W : Int × Int → Int × Int) (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (k : Nat) : cw163 W ((P + 1 + 1) ^ (2 * k + 1)) = (Ring163.ring163 LibInt.intR).zero := by
  exact cw163_zero W _ (pow_odd P hc k)

end Inert163

#print axioms Inert163.sqgen2
#print axioms Inert163.cast_en
#print axioms Inert163.en_mod163
#print axioms Inert163.cw163_zero
#print axioms Inert163.en_Pi
#print axioms Inert163.nrm_Pi
#print axioms Inert163.cj_Pi
#print axioms Inert163.not_p
#print axioms Inert163.dvd_conv
#print axioms Inert163.split_sq
#print axioms Inert163.inert_div
#print axioms Inert163.nat_pow2
#print axioms Inert163.factor_inert
#print axioms Inert163.prim_nPi
#print axioms Inert163.pw_neg
#print axioms Inert163.nrm_neg_one_pw
#print axioms Inert163.prim_inert
#print axioms Inert163.nrm_nPi
#print axioms Inert163.nrm_nPi_pw
#print axioms Inert163.cw163_even
#print axioms Inert163.pow_odd
#print axioms Inert163.cw163_odd

#print axioms: 22 theorems, no axioms.

78.328 1849a: the ramified prime 43

Norms divisible by \(43\) have \(\rho \equiv 0\), so no primary element lies above \(43\).

\[ c_W(43n)=0. \]

lang/Ram43.lean

/-
  lang/Ram43 — T50: **the ℚ(√−43) Hecke theta series vanishes at the ramified prime**: c_W(43n) = 0.
    no_prim   43 | N(z) ⇒ z ∉ H43  (N ≡ ρ² mod 43, so ρ ≡ 0 and χ(ρ) = 0 ≠ 1)
    cw43_ram   c_W(43n) = 0 for every n and every W
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Inert43

namespace Ram43

open FTAChain DetChain LibInt YMChain FiniteRecChain Euc43 HeckeMult43

/-- 43 | N(z) ⇒ z is not primary. -/
theorem no_prim (z : Int × Int) (n : Nat) (h : en z = 43 * Int.ofNat n) (hp : Prim43.Prim43 z) : False := by
  have hc : (ZMod.ring (2 * 20 + 1 + 1)).mul (Prim43.cr z) (Prim43.cr z) = (ZMod.ring (2 * 20 + 1 + 1)).zero := by
    rw [← Inert43.cast_en, h, Prim43.cz43]
  by_cases h0 : Prim43.cr z = (ZMod.ring (2 * 20 + 1 + 1)).zero
  · have : QChar.chi 20 (Prim43.cr z) = 1 := hp
    rw [h0, QChar.chi_zero] at this; exact Nat.noConfusion (Int.ofNat.inj this)
  · have := QChar.chi_sq 20 Prim43.prime43 _ h0
    rw [hc, QChar.chi_zero] at this; exact Nat.noConfusion (Int.ofNat.inj this)

/-- **c_W(43n) = 0.** -/
theorem cw43_ram (W : Int × Int → Int × Int) (n : Nat) : cw43 W (43 * n) = (Ring43.ring43 LibInt.intR).zero := by
  unfold cw43
  rw [sumK_ext (Ring43.ring43 LibInt.intR) _ (fun _ => (Ring43.ring43 LibInt.intR).zero) (fun z => by
    rw [I43_neg _ _ (fun h => no_prim z n h.1 h.2)]; lib_ring)]
  exact zsum43 _

end Ram43

#print axioms Ram43.no_prim
#print axioms Ram43.cw43_ram

#print axioms: 2 theorems, no axioms.

78.329 4489a: the ramified prime 67

Norms divisible by \(67\) have \(\rho \equiv 0\), so no primary element lies above \(67\).

\[ c_W(67n)=0. \]

lang/Ram67.lean

/-
  lang/Ram67 — T50: **the ℚ(√−67) Hecke theta series vanishes at the ramified prime**: c_W(67n) = 0.
    no_prim   67 | N(z) ⇒ z ∉ H67  (N ≡ ρ² mod 67, so ρ ≡ 0 and χ(ρ) = 0 ≠ 1)
    cw67_ram   c_W(67n) = 0 for every n and every W
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Inert67

namespace Ram67

open FTAChain DetChain LibInt YMChain FiniteRecChain Euc67 HeckeMult67

/-- 67 | N(z) ⇒ z is not primary. -/
theorem no_prim (z : Int × Int) (n : Nat) (h : en z = 67 * Int.ofNat n) (hp : Prim67.Prim67 z) : False := by
  have hc : (ZMod.ring (2 * 32 + 1 + 1)).mul (Prim67.cr z) (Prim67.cr z) = (ZMod.ring (2 * 32 + 1 + 1)).zero := by
    rw [← Inert67.cast_en, h, Prim67.cz67]
  by_cases h0 : Prim67.cr z = (ZMod.ring (2 * 32 + 1 + 1)).zero
  · have : QChar.chi 32 (Prim67.cr z) = 1 := hp
    rw [h0, QChar.chi_zero] at this; exact Nat.noConfusion (Int.ofNat.inj this)
  · have := QChar.chi_sq 32 Prim67.prime67 _ h0
    rw [hc, QChar.chi_zero] at this; exact Nat.noConfusion (Int.ofNat.inj this)

/-- **c_W(67n) = 0.** -/
theorem cw67_ram (W : Int × Int → Int × Int) (n : Nat) : cw67 W (67 * n) = (Ring67.ring67 LibInt.intR).zero := by
  unfold cw67
  rw [sumK_ext (Ring67.ring67 LibInt.intR) _ (fun _ => (Ring67.ring67 LibInt.intR).zero) (fun z => by
    rw [I67_neg _ _ (fun h => no_prim z n h.1 h.2)]; lib_ring)]
  exact zsum67 _

end Ram67

#print axioms Ram67.no_prim
#print axioms Ram67.cw67_ram

#print axioms: 2 theorems, no axioms.

78.330 26569a: the ramified prime 163

Norms divisible by \(163\) have \(\rho \equiv 0\), so no primary element lies above \(163\).

\[ c_W(163n)=0. \]

lang/Ram163.lean

/-
  lang/Ram163 — T50: **the ℚ(√−163) Hecke theta series vanishes at the ramified prime**: c_W(163n) = 0.
    no_prim   163 | N(z) ⇒ z ∉ H163  (N ≡ ρ² mod 163, so ρ ≡ 0 and χ(ρ) = 0 ≠ 1)
    cw163_ram   c_W(163n) = 0 for every n and every W
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Inert163

namespace Ram163

open FTAChain DetChain LibInt YMChain FiniteRecChain Euc163 HeckeMult163

/-- 163 | N(z) ⇒ z is not primary. -/
theorem no_prim (z : Int × Int) (n : Nat) (h : en z = 163 * Int.ofNat n) (hp : Prim163.Prim163 z) : False := by
  have hc : (ZMod.ring (2 * 80 + 1 + 1)).mul (Prim163.cr z) (Prim163.cr z) = (ZMod.ring (2 * 80 + 1 + 1)).zero := by
    rw [← Inert163.cast_en, h, Prim163.cz163]
  by_cases h0 : Prim163.cr z = (ZMod.ring (2 * 80 + 1 + 1)).zero
  · have : QChar.chi 80 (Prim163.cr z) = 1 := hp
    rw [h0, QChar.chi_zero] at this; exact Nat.noConfusion (Int.ofNat.inj this)
  · have := QChar.chi_sq 80 Prim163.prime163 _ h0
    rw [hc, QChar.chi_zero] at this; exact Nat.noConfusion (Int.ofNat.inj this)

/-- **c_W(163n) = 0.** -/
theorem cw163_ram (W : Int × Int → Int × Int) (n : Nat) : cw163 W (163 * n) = (Ring163.ring163 LibInt.intR).zero := by
  unfold cw163
  rw [sumK_ext (Ring163.ring163 LibInt.intR) _ (fun _ => (Ring163.ring163 LibInt.intR).zero) (fun z => by
    rw [I163_neg _ _ (fun h => no_prim z n h.1 h.2)]; lib_ring)]
  exact zsum163 _

end Ram163

#print axioms Ram163.no_prim
#print axioms Ram163.cw163_ram

#print axioms: 2 theorems, no axioms.

78.331 49a: the ramified prime 7

Norms divisible by \(7\) force \(\rho \equiv 0\), which is not a nonzero square mod \(7\).

\[ c_W(7n)=0. \]

lang/Ram7.lean

/-
  lang/Ram7 — T50: **the ℚ(√−7) Hecke theta series vanishes at the ramified prime**: c_W(7n) = 0.
    no_prim   7 | N(z) ⇒ z ∉ H7  (8N = ρ² + 7a², so ρ ≡ 0 mod 7, while ρ of a primary element is a nonzero square mod 7)
    cw7_ram   c_W(7n) = 0 for every n and every W
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.HeckeMult7

namespace Ram7

open FTAChain DetChain LibInt YMChain FiniteRecChain Euc7 Prim7 HeckeMult7

/-- E (a² + ab + t b²) = (a + c b)² + D a² when 2c = E, c² = E t, D + 1 = E. -/
theorem sqg (a b c D E t : Int) (h1 : 2 * c = E) (h2 : c * c = E * t) (h3 : D + 1 = E) :
    E * (a * a + a * b + t * (b * b)) = (a + c * b) * (a + c * b) + D * (a * a) := by
  calc E * (a * a + a * b + t * (b * b)) = E * (a * a) + E * (a * b) + (E * t) * (b * b) := by lib_int
    _ = (D + 1) * (a * a) + (2 * c) * (a * b) + (c * c) * (b * b) := by rw [h3, h1, h2]
    _ = _ := by lib_int

/-- E·(Dn) = (De + c)² + Da² and c² = Dq + r give D·(…) = r. -/
theorem ramgen (D E a e c n q r : Int) (hN : E * (D * n) = (D * e + c) * (D * e + c) + D * (a * a)) (hc : c * c = D * q + r) :
    D * (E * n + -(a * a) + -(D * (e * e)) + -(2 * (e * c)) + -q) = r := by
  calc D * (E * n + -(a * a) + -(D * (e * e)) + -(2 * (e * c)) + -q) = E * (D * n) + -(D * (a * a)) + -((D * e + c) * (D * e + c)) + c * c + -(D * q) := by lib_int
    _ = r := by rw [hN, hc]; lib_int

/-- D·K = r with 0 < r < D is impossible. -/
theorem nd (D r : Nat) (K : Int) (h : Int.ofNat D * K = Int.ofNat r) (h0 : 0 < r) (hr : r < D) : False := by
  cases D with
  | zero => exact Nat.not_lt_zero _ hr
  | succ D' =>
    cases K with
    | ofNat k =>
      have hk : (D' + 1) * k = r := Int.ofNat.inj h
      cases k with
      | zero => exact Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le h0 (Nat.le_of_eq (hk.symm.trans (Nat.mul_zero _))))
      | succ j => exact Nat.lt_irrefl r (Nat.lt_of_lt_of_le hr (Nat.le_trans (Nat.le_add_left (D' + 1) ((D' + 1) * j)) (Nat.le_of_eq hk)))
    | negSucc j =>
      have : Int.ofNat (D' + 1) * Int.negSucc j = Int.negSucc ((D' + 1) * j + D') := by
        show Int.negOfNat ((D' + 1) * (j + 1)) = _
        show Int.negOfNat ((D' + 1) * j + D' + 1) = _
        rfl
      rw [this] at h; exact Int.noConfusion h

/-- 7 | N(z) ⇒ z is not primary. -/
theorem no_prim (z : Int × Int) (n : Nat) (h : en z = 7 * Int.ofNat n) (hp : Prim7 z) : False := by
  have hN : 8 * (7 * Int.ofNat n) = rho z * rho z + 7 * (z.1 * z.1) := by rw [← h]; exact sqg z.1 z.2 4 7 8 2 rfl rfl rfl
  obtain ⟨e, hc | hc | hc⟩ := hp
  · rw [hc] at hN; exact nd 7 1 _ (ramgen 7 8 z.1 e 1 (Int.ofNat n) 0 1 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact nd 7 4 _ (ramgen 7 8 z.1 e 2 (Int.ofNat n) 0 4 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact nd 7 2 _ (ramgen 7 8 z.1 e 4 (Int.ofNat n) 2 2 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)

/-- **c_W(7n) = 0.** -/
theorem cw7_ram (W : Int × Int → Int × Int) (n : Nat) : cw7 W (7 * n) = (Ring7.ring7 LibInt.intR).zero := by
  unfold cw7
  rw [sumK_ext (Ring7.ring7 LibInt.intR) _ (fun _ => (Ring7.ring7 LibInt.intR).zero) (fun z => by
    rw [I7_neg _ _ (fun h => no_prim z n h.1 h.2)]; lib_ring)]
  exact zsum7 _

end Ram7

#print axioms Ram7.sqg
#print axioms Ram7.ramgen
#print axioms Ram7.nd
#print axioms Ram7.no_prim
#print axioms Ram7.cw7_ram

#print axioms: 5 theorems, no axioms.

78.332 121b: the ramified prime 11

Norms divisible by \(11\) force \(\rho \equiv 0\), which is not a nonzero square mod \(11\).

\[ c_W(11n)=0. \]

lang/Ram11.lean

/-
  lang/Ram11 — T50: **the ℚ(√−11) Hecke theta series vanishes at the ramified prime**: c_W(11n) = 0.
    no_prim   11 | N(z) ⇒ z ∉ H11  (12N = ρ² + 11a², so ρ ≡ 0 mod 11, while ρ of a primary element is a nonzero square mod 11)
    cw11_ram   c_W(11n) = 0 for every n and every W
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.HeckeMult11
import lang.Ram7

namespace Ram11

open FTAChain DetChain LibInt YMChain FiniteRecChain Euc11 Prim11 HeckeMult11

/-- 11 | N(z) ⇒ z is not primary. -/
theorem no_prim (z : Int × Int) (n : Nat) (h : en z = 11 * Int.ofNat n) (hp : Prim11 z) : False := by
  have hN : 12 * (11 * Int.ofNat n) = rho z * rho z + 11 * (z.1 * z.1) := by rw [← h]; exact Ram7.sqg z.1 z.2 6 11 12 3 rfl rfl rfl
  obtain ⟨e, hc | hc | hc | hc | hc⟩ := hp
  · rw [hc] at hN; exact Ram7.nd 11 1 _ (Ram7.ramgen 11 12 z.1 e 1 (Int.ofNat n) 0 1 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact Ram7.nd 11 9 _ (Ram7.ramgen 11 12 z.1 e 3 (Int.ofNat n) 0 9 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact Ram7.nd 11 5 _ (Ram7.ramgen 11 12 z.1 e 4 (Int.ofNat n) 1 5 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact Ram7.nd 11 3 _ (Ram7.ramgen 11 12 z.1 e 5 (Int.ofNat n) 2 3 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact Ram7.nd 11 4 _ (Ram7.ramgen 11 12 z.1 e 9 (Int.ofNat n) 7 4 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)

/-- **c_W(11n) = 0.** -/
theorem cw11_ram (W : Int × Int → Int × Int) (n : Nat) : cw11 W (11 * n) = (Ring11.ring11 LibInt.intR).zero := by
  unfold cw11
  rw [sumK_ext (Ring11.ring11 LibInt.intR) _ (fun _ => (Ring11.ring11 LibInt.intR).zero) (fun z => by
    rw [I11_neg _ _ (fun h => no_prim z n h.1 h.2)]; lib_ring)]
  exact zsum11 _

end Ram11

#print axioms Ram11.no_prim
#print axioms Ram11.cw11_ram

#print axioms: 2 theorems, no axioms.

78.333 361a: the ramified prime 19

Norms divisible by \(19\) force \(\rho \equiv 0\), which is not a nonzero square mod \(19\).

\[ c_W(19n)=0. \]

lang/Ram19.lean

/-
  lang/Ram19 — T50: **the ℚ(√−19) Hecke theta series vanishes at the ramified prime**: c_W(19n) = 0.
    no_prim   19 | N(z) ⇒ z ∉ H19  (20N = ρ² + 19a², so ρ ≡ 0 mod 19, while ρ of a primary element is a nonzero square mod 19)
    cw19_ram   c_W(19n) = 0 for every n and every W
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.HeckeMult19
import lang.Ram7

namespace Ram19

open FTAChain DetChain LibInt YMChain FiniteRecChain Euc19 Prim19 HeckeMult19

/-- 19 | N(z) ⇒ z is not primary. -/
theorem no_prim (z : Int × Int) (n : Nat) (h : en z = 19 * Int.ofNat n) (hp : Prim19 z) : False := by
  have hN : 20 * (19 * Int.ofNat n) = rho z * rho z + 19 * (z.1 * z.1) := by rw [← h]; exact Ram7.sqg z.1 z.2 10 19 20 5 rfl rfl rfl
  obtain ⟨e, hc | hc | hc | hc | hc | hc | hc | hc | hc⟩ := hp
  · rw [hc] at hN; exact Ram7.nd 19 1 _ (Ram7.ramgen 19 20 z.1 e 1 (Int.ofNat n) 0 1 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact Ram7.nd 19 16 _ (Ram7.ramgen 19 20 z.1 e 4 (Int.ofNat n) 0 16 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact Ram7.nd 19 6 _ (Ram7.ramgen 19 20 z.1 e 5 (Int.ofNat n) 1 6 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact Ram7.nd 19 17 _ (Ram7.ramgen 19 20 z.1 e 6 (Int.ofNat n) 1 17 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact Ram7.nd 19 11 _ (Ram7.ramgen 19 20 z.1 e 7 (Int.ofNat n) 2 11 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact Ram7.nd 19 5 _ (Ram7.ramgen 19 20 z.1 e 9 (Int.ofNat n) 4 5 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact Ram7.nd 19 7 _ (Ram7.ramgen 19 20 z.1 e 11 (Int.ofNat n) 6 7 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact Ram7.nd 19 9 _ (Ram7.ramgen 19 20 z.1 e 16 (Int.ofNat n) 13 9 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
  · rw [hc] at hN; exact Ram7.nd 19 4 _ (Ram7.ramgen 19 20 z.1 e 17 (Int.ofNat n) 15 4 hN rfl) (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)

/-- **c_W(19n) = 0.** -/
theorem cw19_ram (W : Int × Int → Int × Int) (n : Nat) : cw19 W (19 * n) = (Ring19.ring19 LibInt.intR).zero := by
  unfold cw19
  rw [sumK_ext (Ring19.ring19 LibInt.intR) _ (fun _ => (Ring19.ring19 LibInt.intR).zero) (fun z => by
    rw [I19_neg _ _ (fun h => no_prim z n h.1 h.2)]; lib_ring)]
  exact zsum19 _

end Ram19

#print axioms Ram19.no_prim
#print axioms Ram19.cw19_ram

#print axioms: 2 theorems, no axioms.

78.334 Lifting prime-power recursions

Bezout lifts to powers; a recursion at \(p^j\) lifts to \(p^j m\) for \(c\) multiplicative.

\[ c(p^{j+2}m)=A\,c(p^{j+1}m)-B\,c(p^jm). \]

lang/HeckeEig.lean

/-
  lang/HeckeEig — T50: generic pieces for the Hecke-eigenform statement at every n prime to the level.
    bez_pow     s p + t m = 1 ⇒ s' p^k + t' m = 1
    nd, not_div  p^k is never a multiple of d when u p + v d = 1 (d > 1)
    succ_pow    (q + 1)^k = a + 1
    lift2       a prime-power recursion c(q^(j+2)) = A c(q^(j+1)) − B c(q^j) lifts to c(q^j m) when c(q^j m) = c(q^j) c(m)
  Lean 4 core only, proofs by hand (lib_ring, lib_int, lib_nat), no axioms.
-/
import lang.HeckePrime

namespace HeckeEig

open DetChain LibInt

theorem bez_pow (p : Nat) (m s t : Int) (h : s * Int.ofNat p + t * m = 1) :
    ∀ k : Nat, ∃ s' t' : Int, s' * Int.ofNat (p ^ k) + t' * m = 1
  | 0 => ⟨1, 0, by show (1 : Int) * 1 + 0 * m = 1; lib_int⟩
  | k + 1 => by
    obtain ⟨a, b, hab⟩ := bez_pow p m s t h k
    refine ⟨a * s, a * t * Int.ofNat (p ^ k) + b * s * Int.ofNat p + b * t * m, ?_⟩
    show a * s * (Int.ofNat (p ^ k) * Int.ofNat p) + (a * t * Int.ofNat (p ^ k) + b * s * Int.ofNat p + b * t * m) * m = 1
    calc a * s * (Int.ofNat (p ^ k) * Int.ofNat p) + (a * t * Int.ofNat (p ^ k) + b * s * Int.ofNat p + b * t * m) * m
        = (a * Int.ofNat (p ^ k) + b * m) * (s * Int.ofNat p + t * m) := by lib_int
      _ = 1 := by rw [hab, h]; rfl

/-- D·K = r with 0 < r < D is impossible. -/
theorem nd (D r : Nat) (K : Int) (h : Int.ofNat D * K = Int.ofNat r) (h0 : 0 < r) (hr : r < D) : False := by
  cases D with
  | zero => exact Nat.not_lt_zero _ hr
  | succ D' =>
    cases K with
    | ofNat k =>
      have hk : (D' + 1) * k = r := Int.ofNat.inj h
      cases k with
      | zero => exact Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le h0 (Nat.le_of_eq (hk.symm.trans (Nat.mul_zero _))))
      | succ j => exact Nat.lt_irrefl r (Nat.lt_of_lt_of_le hr (Nat.le_trans (Nat.le_add_left (D' + 1) ((D' + 1) * j)) (Nat.le_of_eq hk)))
    | negSucc j =>
      have : Int.ofNat (D' + 1) * Int.negSucc j = Int.negSucc ((D' + 1) * j + D') := by
        show Int.negOfNat ((D' + 1) * (j + 1)) = _
        show Int.negOfNat ((D' + 1) * j + D' + 1) = _
        rfl
      rw [this] at h; exact Int.noConfusion h

theorem not_div (p d : Nat) (u v : Int) (huv : u * Int.ofNat p + v * Int.ofNat d = 1) (hd : 1 < d) (k : Nat) (K : Int) :
    Int.ofNat (p ^ k) ≠ Int.ofNat d * K := fun e => by
  obtain ⟨a, b, hab⟩ := bez_pow p (Int.ofNat d) u v huv k
  rw [e] at hab
  exact nd d 1 (a * K + b) ((show Int.ofNat d * (a * K + b) = a * (Int.ofNat d * K) + b * Int.ofNat d by lib_int).trans hab) (Nat.zero_lt_succ 0) hd

theorem succ_pow (q : Nat) : ∀ k : Nat, ∃ a : Nat, (q + 1) ^ k = a + 1
  | 0 => ⟨0, rfl⟩
  | k + 1 => by
    obtain ⟨a, ha⟩ := succ_pow q k
    exact ⟨a * (q + 1) + q, by show (q + 1) ^ k * (q + 1) = _; rw [ha]; lib_nat⟩

variable {R : Type} (Rg : CRing R)

/-- **Lifting a prime-power recursion through multiplicativity.** -/
theorem lift2 (c : Nat → R) (q m : Nat) (hmul : ∀ j, c (q ^ j * m) = Rg.mul (c (q ^ j)) (c m)) (A B : R)
    (hrec : ∀ j, c (q ^ (j + 2)) = Rg.add (Rg.mul A (c (q ^ (j + 1)))) (Rg.neg (Rg.mul B (c (q ^ j))))) (j : Nat) :
    c (q ^ (j + 2) * m) = Rg.add (Rg.mul A (c (q ^ (j + 1) * m))) (Rg.neg (Rg.mul B (c (q ^ j * m)))) := by
  rw [hmul, hmul, hmul, hrec]; lib_ring

end HeckeEig

#print axioms HeckeEig.bez_pow
#print axioms HeckeEig.nd
#print axioms HeckeEig.not_div
#print axioms HeckeEig.succ_pow
#print axioms HeckeEig.lift2

#print axioms: 5 theorems, no axioms.

78.335 49a: Hecke relations at every \(p\neq 7\)

Split and inert recursions lifted to all \(p^jm\), \((m,7p)=1\).

\[ c(p^{j+2}m)=a_p\,c(p^{j+1}m)-\psi (p)\,c(p^jm). \]

lang/Eig7.lean

/-
  lang/Eig7 — T50: **49a's Hecke theta series is a Hecke eigenform at every prime p ≠ 7**: for m prime to p and 7,
    eig7_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig7_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (with Ram7: c(7n) = 0).  From HeckeMult7, HeckePrime7, Inert7 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inert7
import lang.HeckeEig

namespace Eig7

open FTAChain DetChain LibInt YMChain Frobenius Euc7 Prim7 HeckeMult7

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 7. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 7 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 7 * k) (j : Nat) :
    cw7 W ((P + 1 + 1) ^ j * (m + 1)) = (Ring7.ring7 LibInt.intR).mul (cw7 W ((P + 1 + 1) ^ j)) (cw7 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult7 W hW a m s' t' h'
    (fun K e => HeckeEig.not_div (P + 1 + 1) 7 u v huv (Nat.le_of_ble_eq_true rfl) j K (by rw [ha]; exact e)) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig7_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring7.ring7 LibInt.intR).one = (Ring7.ring7 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim7 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 7 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 7 * k) (j : Nat) :
    cw7 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring7.ring7 LibInt.intR).add
      ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).add (W π) (W (cj π))) (cw7 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).mul (W π) (W (cj π))) (cw7 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw7 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime7.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6) (j : Nat) :
    cw7 W ((P + 1 + 1) ^ (j + 2)) = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul (Ring7.ring7 LibInt.intR).zero (cw7 W ((P + 1 + 1) ^ (j + 1))))
      ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).neg (W (Inert7.nPi P))) (cw7 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Inert7.cw7_even P W hpr hc (k + 1), Inert7.cw7_even P W hpr hc k]
    show W ((Ring7.ring7 LibInt.intR).mul (pw (Ring7.ring7 LibInt.intR) (Inert7.nPi P) k) (Inert7.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Inert7.cw7_odd P W hc (k + 1), Inert7.cw7_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig7_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring7.ring7 LibInt.intR).mul α β) = (Ring7.ring7 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 7 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 7 * K + 6)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 7 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 7 * k) (j : Nat) :
    cw7 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring7.ring7 LibInt.intR).add ((Ring7.ring7 LibInt.intR).mul (Ring7.ring7 LibInt.intR).zero (cw7 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring7.ring7 LibInt.intR).neg ((Ring7.ring7 LibInt.intR).mul ((Ring7.ring7 LibInt.intR).neg (W (Inert7.nPi P))) (cw7 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw7 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig7

#print axioms Eig7.parity
#print axioms Eig7.mul_pow
#print axioms Eig7.eig7_split
#print axioms Eig7.inert_rec
#print axioms Eig7.eig7_inert

#print axioms: 5 theorems, no axioms.

78.336 121b: Hecke relations at every \(p\neq 11\)

Split and inert recursions lifted to all \(p^jm\), \((m,11p)=1\).

\[ c(p^{j+2}m)=a_p\,c(p^{j+1}m)-\psi (p)\,c(p^jm). \]

lang/Eig11.lean

/-
  lang/Eig11 — T50: **121b's Hecke theta series is a Hecke eigenform at every prime p ≠ 11**: for m prime to p and 11,
    eig11_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig11_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (with Ram11: c(11n) = 0).  From HeckeMult11, HeckePrime11, Inert11 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inert11
import lang.HeckeEig

namespace Eig11

open FTAChain DetChain LibInt YMChain Frobenius Euc11 Prim11 HeckeMult11

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 11. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 11 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 11 * k) (j : Nat) :
    cw11 W ((P + 1 + 1) ^ j * (m + 1)) = (Ring11.ring11 LibInt.intR).mul (cw11 W ((P + 1 + 1) ^ j)) (cw11 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult11 W hW a m s' t' h'
    (fun K e => HeckeEig.not_div (P + 1 + 1) 11 u v huv (Nat.le_of_ble_eq_true rfl) j K (by rw [ha]; exact e)) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig11_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring11.ring11 LibInt.intR).one = (Ring11.ring11 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim11 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 11 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 11 * k) (j : Nat) :
    cw11 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring11.ring11 LibInt.intR).add
      ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).add (W π) (W (cj π))) (cw11 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).mul (W π) (W (cj π))) (cw11 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw11 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime11.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10) (j : Nat) :
    cw11 W ((P + 1 + 1) ^ (j + 2)) = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul (Ring11.ring11 LibInt.intR).zero (cw11 W ((P + 1 + 1) ^ (j + 1))))
      ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).neg (W (Inert11.nPi P))) (cw11 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Inert11.cw11_even P W hpr hc (k + 1), Inert11.cw11_even P W hpr hc k]
    show W ((Ring11.ring11 LibInt.intR).mul (pw (Ring11.ring11 LibInt.intR) (Inert11.nPi P) k) (Inert11.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Inert11.cw11_odd P W hc (k + 1), Inert11.cw11_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig11_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring11.ring11 LibInt.intR).mul α β) = (Ring11.ring11 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 11 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 6 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 7 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 11 * K + 10)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 11 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 11 * k) (j : Nat) :
    cw11 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring11.ring11 LibInt.intR).add ((Ring11.ring11 LibInt.intR).mul (Ring11.ring11 LibInt.intR).zero (cw11 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring11.ring11 LibInt.intR).neg ((Ring11.ring11 LibInt.intR).mul ((Ring11.ring11 LibInt.intR).neg (W (Inert11.nPi P))) (cw11 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw11 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig11

#print axioms Eig11.parity
#print axioms Eig11.mul_pow
#print axioms Eig11.eig11_split
#print axioms Eig11.inert_rec
#print axioms Eig11.eig11_inert

#print axioms: 5 theorems, no axioms.

78.337 361a: Hecke relations at every \(p\neq 19\)

Split and inert recursions lifted to all \(p^jm\), \((m,19p)=1\).

\[ c(p^{j+2}m)=a_p\,c(p^{j+1}m)-\psi (p)\,c(p^jm). \]

lang/Eig19.lean

/-
  lang/Eig19 — T50: **361a's Hecke theta series is a Hecke eigenform at every prime p ≠ 19**: for m prime to p and 19,
    eig19_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig19_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (with Ram19: c(19n) = 0).  From HeckeMult19, HeckePrime19, Inert19 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inert19
import lang.HeckeEig

namespace Eig19

open FTAChain DetChain LibInt YMChain Frobenius Euc19 Prim19 HeckeMult19

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 19. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 19 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 19 * k) (j : Nat) :
    cw19 W ((P + 1 + 1) ^ j * (m + 1)) = (Ring19.ring19 LibInt.intR).mul (cw19 W ((P + 1 + 1) ^ j)) (cw19 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult19 W hW a m s' t' h'
    (fun K e => HeckeEig.not_div (P + 1 + 1) 19 u v huv (Nat.le_of_ble_eq_true rfl) j K (by rw [ha]; exact e)) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig19_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring19.ring19 LibInt.intR).one = (Ring19.ring19 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim19 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 19 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 19 * k) (j : Nat) :
    cw19 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring19.ring19 LibInt.intR).add
      ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).add (W π) (W (cj π))) (cw19 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).mul (W π) (W (cj π))) (cw19 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw19 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime19.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 18) (j : Nat) :
    cw19 W ((P + 1 + 1) ^ (j + 2)) = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).zero (cw19 W ((P + 1 + 1) ^ (j + 1))))
      ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).neg (W (Inert19.nPi P))) (cw19 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Inert19.cw19_even P W hpr hc (k + 1), Inert19.cw19_even P W hpr hc k]
    show W ((Ring19.ring19 LibInt.intR).mul (pw (Ring19.ring19 LibInt.intR) (Inert19.nPi P) k) (Inert19.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Inert19.cw19_odd P W hc (k + 1), Inert19.cw19_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig19_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring19.ring19 LibInt.intR).mul α β) = (Ring19.ring19 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 19 * K + 2 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 3 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 8 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 10 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 12 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 13 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 14 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 15 ∨ Int.ofNat (P + 1 + 1) = 19 * K + 18)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 19 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 19 * k) (j : Nat) :
    cw19 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring19.ring19 LibInt.intR).add ((Ring19.ring19 LibInt.intR).mul (Ring19.ring19 LibInt.intR).zero (cw19 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring19.ring19 LibInt.intR).neg ((Ring19.ring19 LibInt.intR).mul ((Ring19.ring19 LibInt.intR).neg (W (Inert19.nPi P))) (cw19 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw19 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig19

#print axioms Eig19.parity
#print axioms Eig19.mul_pow
#print axioms Eig19.eig19_split
#print axioms Eig19.inert_rec
#print axioms Eig19.eig19_inert

#print axioms: 5 theorems, no axioms.

78.338 1849a: Hecke relations at every \(p\neq 43\)

Split and inert recursions lifted to all \(p^jm\), \((m,43p)=1\).

\[ c(p^{j+2}m)=a_p\,c(p^{j+1}m)-\psi (p)\,c(p^jm). \]

lang/Eig43.lean

/-
  lang/Eig43 — T50: **1849a's Hecke theta series is a Hecke eigenform at every prime p ≠ 43**: for m prime to p and 43,
    eig43_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig43_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (with Ram43: c(43n) = 0).  From HeckeMult43, HeckePrime43, Inert43 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inert43
import lang.HeckeEig

namespace Eig43

open FTAChain DetChain LibInt YMChain Frobenius Euc43 Prim43 HeckeMult43

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 43. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 43 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 43 * k) (j : Nat) :
    cw43 W ((P + 1 + 1) ^ j * (m + 1)) = (Ring43.ring43 LibInt.intR).mul (cw43 W ((P + 1 + 1) ^ j)) (cw43 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult43 W hW a m s' t' h'
    (fun K e => HeckeEig.not_div (P + 1 + 1) 43 u v huv (Nat.le_of_ble_eq_true rfl) j K (by rw [ha]; exact e)) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig43_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring43.ring43 LibInt.intR).one = (Ring43.ring43 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim43 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 43 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 43 * k) (j : Nat) :
    cw43 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring43.ring43 LibInt.intR).add
      ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).add (W π) (W (cj π))) (cw43 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).mul (W π) (W (cj π))) (cw43 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw43 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime43.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (j : Nat) :
    cw43 W ((P + 1 + 1) ^ (j + 2)) = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).zero (cw43 W ((P + 1 + 1) ^ (j + 1))))
      ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).neg (W (Inert43.nPi P))) (cw43 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Inert43.cw43_even P W hpr hc (k + 1), Inert43.cw43_even P W hpr hc k]
    show W ((Ring43.ring43 LibInt.intR).mul (pw (Ring43.ring43 LibInt.intR) (Inert43.nPi P) k) (Inert43.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Inert43.cw43_odd P W hc (k + 1), Inert43.cw43_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig43_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring43.ring43 LibInt.intR).mul α β) = (Ring43.ring43 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : QChar.chi 20 (LibRing.castZ (ZMod.ring (2 * 20 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 43 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 43 * k) (j : Nat) :
    cw43 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring43.ring43 LibInt.intR).add ((Ring43.ring43 LibInt.intR).mul (Ring43.ring43 LibInt.intR).zero (cw43 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring43.ring43 LibInt.intR).neg ((Ring43.ring43 LibInt.intR).mul ((Ring43.ring43 LibInt.intR).neg (W (Inert43.nPi P))) (cw43 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw43 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig43

#print axioms Eig43.parity
#print axioms Eig43.mul_pow
#print axioms Eig43.eig43_split
#print axioms Eig43.inert_rec
#print axioms Eig43.eig43_inert

#print axioms: 5 theorems, no axioms.

78.339 4489a: Hecke relations at every \(p\neq 67\)

Split and inert recursions lifted to all \(p^jm\), \((m,67p)=1\).

\[ c(p^{j+2}m)=a_p\,c(p^{j+1}m)-\psi (p)\,c(p^jm). \]

lang/Eig67.lean

/-
  lang/Eig67 — T50: **4489a's Hecke theta series is a Hecke eigenform at every prime p ≠ 67**: for m prime to p and 67,
    eig67_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig67_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (with Ram67: c(67n) = 0).  From HeckeMult67, HeckePrime67, Inert67 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inert67
import lang.HeckeEig

namespace Eig67

open FTAChain DetChain LibInt YMChain Frobenius Euc67 Prim67 HeckeMult67

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 67. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 67 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 67 * k) (j : Nat) :
    cw67 W ((P + 1 + 1) ^ j * (m + 1)) = (Ring67.ring67 LibInt.intR).mul (cw67 W ((P + 1 + 1) ^ j)) (cw67 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult67 W hW a m s' t' h'
    (fun K e => HeckeEig.not_div (P + 1 + 1) 67 u v huv (Nat.le_of_ble_eq_true rfl) j K (by rw [ha]; exact e)) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig67_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring67.ring67 LibInt.intR).one = (Ring67.ring67 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim67 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 67 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 67 * k) (j : Nat) :
    cw67 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring67.ring67 LibInt.intR).add
      ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).add (W π) (W (cj π))) (cw67 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).mul (W π) (W (cj π))) (cw67 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw67 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime67.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (j : Nat) :
    cw67 W ((P + 1 + 1) ^ (j + 2)) = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).zero (cw67 W ((P + 1 + 1) ^ (j + 1))))
      ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).neg (W (Inert67.nPi P))) (cw67 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Inert67.cw67_even P W hpr hc (k + 1), Inert67.cw67_even P W hpr hc k]
    show W ((Ring67.ring67 LibInt.intR).mul (pw (Ring67.ring67 LibInt.intR) (Inert67.nPi P) k) (Inert67.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Inert67.cw67_odd P W hc (k + 1), Inert67.cw67_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig67_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring67.ring67 LibInt.intR).mul α β) = (Ring67.ring67 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : QChar.chi 32 (LibRing.castZ (ZMod.ring (2 * 32 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 67 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 67 * k) (j : Nat) :
    cw67 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring67.ring67 LibInt.intR).add ((Ring67.ring67 LibInt.intR).mul (Ring67.ring67 LibInt.intR).zero (cw67 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring67.ring67 LibInt.intR).neg ((Ring67.ring67 LibInt.intR).mul ((Ring67.ring67 LibInt.intR).neg (W (Inert67.nPi P))) (cw67 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw67 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig67

#print axioms Eig67.parity
#print axioms Eig67.mul_pow
#print axioms Eig67.eig67_split
#print axioms Eig67.inert_rec
#print axioms Eig67.eig67_inert

#print axioms: 5 theorems, no axioms.

78.340 26569a: Hecke relations at every \(p\neq 163\)

Split and inert recursions lifted to all \(p^jm\), \((m,163p)=1\).

\[ c(p^{j+2}m)=a_p\,c(p^{j+1}m)-\psi (p)\,c(p^jm). \]

lang/Eig163.lean

/-
  lang/Eig163 — T50: **26569a's Hecke theta series is a Hecke eigenform at every prime p ≠ 163**: for m prime to p and 163,
    eig163_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig163_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (with Ram163: c(163n) = 0).  From HeckeMult163, HeckePrime163, Inert163 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Inert163
import lang.HeckeEig

namespace Eig163

open FTAChain DetChain LibInt YMChain Frobenius Euc163 Prim163 HeckeMult163

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 163. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 163 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 163 * k) (j : Nat) :
    cw163 W ((P + 1 + 1) ^ j * (m + 1)) = (Ring163.ring163 LibInt.intR).mul (cw163 W ((P + 1 + 1) ^ j)) (cw163 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult163 W hW a m s' t' h'
    (fun K e => HeckeEig.not_div (P + 1 + 1) 163 u v huv (Nat.le_of_ble_eq_true rfl) j K (by rw [ha]; exact e)) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig163_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Ring163.ring163 LibInt.intR).one = (Ring163.ring163 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim163 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 163 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 163 * k) (j : Nat) :
    cw163 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring163.ring163 LibInt.intR).add
      ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).add (W π) (W (cj π))) (cw163 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).mul (W π) (W (cj π))) (cw163 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw163 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime163.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1) (j : Nat) :
    cw163 W ((P + 1 + 1) ^ (j + 2)) = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).zero (cw163 W ((P + 1 + 1) ^ (j + 1))))
      ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).neg (W (Inert163.nPi P))) (cw163 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Inert163.cw163_even P W hpr hc (k + 1), Inert163.cw163_even P W hpr hc k]
    show W ((Ring163.ring163 LibInt.intR).mul (pw (Ring163.ring163 LibInt.intR) (Inert163.nPi P) k) (Inert163.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Inert163.cw163_odd P W hc (k + 1), Inert163.cw163_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig163_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Ring163.ring163 LibInt.intR).mul α β) = (Ring163.ring163 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : QChar.chi 80 (LibRing.castZ (ZMod.ring (2 * 80 + 1 + 1)) (Int.ofNat (P + 1 + 1))) = -1)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 163 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∀ k, Int.ofNat (m + 1) ≠ 163 * k) (j : Nat) :
    cw163 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Ring163.ring163 LibInt.intR).add ((Ring163.ring163 LibInt.intR).mul (Ring163.ring163 LibInt.intR).zero (cw163 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Ring163.ring163 LibInt.intR).neg ((Ring163.ring163 LibInt.intR).mul ((Ring163.ring163 LibInt.intR).neg (W (Inert163.nPi P))) (cw163 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw163 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig163

#print axioms Eig163.parity
#print axioms Eig163.mul_pow
#print axioms Eig163.eig163_split
#print axioms Eig163.inert_rec
#print axioms Eig163.eig163_inert

#print axioms: 5 theorems, no axioms.

78.341 Hecke relations for \(\mathbb Q(\sqrt {-3})\)

Split and inert recursions lifted to all \(p^jm\).

\[ c(p^{j+2}m)=a_p\,c(p^{j+1}m)-\psi (p)\,c(p^jm). \]

lang/Eig3.lean

/-
  lang/Eig3 — T50: **27a/36a (ℤ[ω])'s Hecke theta series is a Hecke eigenform at every prime p ≠ 3**: for m prime to p and 3,
    eig3_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig3_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (inert p = 3t + 2).  From HeckeMult3, HeckePrime3, EisInert3 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.EisInert3
import lang.HeckePrime3
import lang.HeckeEig

namespace Eig3

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius EisEuclid EisPrim EisUnique3 EisPrime3 HeckeMult3

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- p^j prime to 3 from u p + 3 v = 1. -/
theorem cond_of (p : Nat) (u v : Int) (huv : u * Int.ofNat p + v * 3 = 1) (j a : Nat) (ha : p ^ j = a + 1) : C3 (Int.ofNat (a + 1)) := by
  obtain ⟨q, hq | hq | hq⟩ := EisPrim.res3 (Int.ofNat (a + 1))
  · exact absurd (by rw [ha]; exact hq) (HeckeEig.not_div p 3 u v huv (Nat.le_of_ble_eq_true rfl) j q)
  · exact ⟨q, Or.inl hq⟩
  · exact ⟨q + -1, Or.inr (by rw [hq]; lib_int)⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 3. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 3 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : C3 (Int.ofNat (m + 1))) (j : Nat) :
    cw3 W ((P + 1 + 1) ^ j * (m + 1)) = (Cyclo3.cyc3 LibInt.intR).mul (cw3 W ((P + 1 + 1) ^ j)) (cw3 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult3 W hW a m s' t' h' (cond_of (P + 1 + 1) u v huv j a ha) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig3_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo3.cyc3 LibInt.intR).one = (Cyclo3.cyc3 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim3 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 3 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : C3 (Int.ofNat (m + 1))) (j : Nat) :
    cw3 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Cyclo3.cyc3 LibInt.intR).add
      ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).add (W π) (W (cj π))) (cw3 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).mul (W π) (W (cj π))) (cw3 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw3 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime3.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (t : Nat) (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1) (j : Nat) :
    cw3 W ((3 * t + 1 + 1) ^ (j + 2)) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (cw3 W ((3 * t + 1 + 1) ^ (j + 1))))
      ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).neg (W (EisInert3.nPi t))) (cw3 W ((3 * t + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, EisInert3.cw3_even t W hpr (k + 1), EisInert3.cw3_even t W hpr k]
    show W ((Cyclo3.cyc3 LibInt.intR).mul (pw (Cyclo3.cyc3 LibInt.intR) (EisInert3.nPi t) k) (EisInert3.nPi t)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, EisInert3.cw3_odd t W (k + 1), EisInert3.cw3_odd t W k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig3_inert (t : Nat) (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo3.cyc3 LibInt.intR).mul α β) = (Cyclo3.cyc3 LibInt.intR).mul (W α) (W β))
    (hpr : ∀ d, Dv d (3 * t + 1 + 1) → d = 1 ∨ d = 3 * t + 1 + 1)
    (u v : Int) (huv : u * Int.ofNat (3 * t + 1 + 1) + v * 3 = 1) (m : Nat) (s t' : Int) (hst : s * Int.ofNat (3 * t + 1 + 1) + t' * Int.ofNat (m + 1) = 1)
    (hm : C3 (Int.ofNat (m + 1))) (j : Nat) :
    cw3 W ((3 * t + 1 + 1) ^ (j + 2) * (m + 1)) = (Cyclo3.cyc3 LibInt.intR).add ((Cyclo3.cyc3 LibInt.intR).mul (Cyclo3.cyc3 LibInt.intR).zero (cw3 W ((3 * t + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Cyclo3.cyc3 LibInt.intR).neg ((Cyclo3.cyc3 LibInt.intR).mul ((Cyclo3.cyc3 LibInt.intR).neg (W (EisInert3.nPi t))) (cw3 W ((3 * t + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw3 W) (3 * t + 1 + 1) (m + 1) (mul_pow (3 * t) W hW u v huv m s t' hst hm) _ _ (inert_rec t W hW hpr) j

end Eig3

#print axioms Eig3.parity
#print axioms Eig3.cond_of
#print axioms Eig3.mul_pow
#print axioms Eig3.eig3_split
#print axioms Eig3.inert_rec
#print axioms Eig3.eig3_inert

#print axioms: 5 theorems, no axioms.

78.342 Hecke relations for \(\mathbb Q(\sqrt {-2})\)

Split and inert recursions lifted to all \(p^jm\).

\[ c(p^{j+2}m)=a_p\,c(p^{j+1}m)-\psi (p)\,c(p^jm). \]

lang/Eig8.lean

/-
  lang/Eig8 — T50: **256a (ℚ(√−2))'s Hecke theta series is a Hecke eigenform at every odd prime p**: for m prime to p and 2,
    eig8_split   p = ππ̄ split:  c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eig8_inert   p inert:       c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  (inert p ≡ 5, 7 mod 8).  From HeckeMult8, HeckePrime8, Sq2Inert8 through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Sq2Inert8
import lang.HeckeEig

namespace Eig8

open FTAChain DetChain LibInt LibGcd YMChain FiniteRecChain Frobenius Sq2Euclid Sq2Prim8 Sq2Unique8 Sq2Prime8 HeckeMult8

variable (P : Nat)

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- p^j odd from u p + 2 v = 1. -/
theorem cond_of (p : Nat) (u v : Int) (huv : u * Int.ofNat p + v * 2 = 1) (j a : Nat) (ha : p ^ j = a + 1) : ∃ k, Int.ofNat (a + 1) = 2 * k + 1 := by
  rcases GaussInt.int_even_odd (Int.ofNat (a + 1)) with ⟨q, hq⟩ | ⟨q, hq⟩
  · exact absurd (by rw [ha]; exact hq) (HeckeEig.not_div p 2 u v huv (Nat.le_of_ble_eq_true rfl) j q)
  · exact ⟨q, hq⟩

/-- c(p^j m) = c(p^j) c(m) for m prime to p and 2. -/
theorem mul_pow (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 2 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (j : Nat) :
    cw8 W ((P + 1 + 1) ^ j * (m + 1)) = (Quad.quad LibInt.intR (-2)).mul (cw8 W ((P + 1 + 1) ^ j)) (cw8 W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  exact hecke_mult8 W hW a m s' t' h' (cond_of (P + 1 + 1) u v huv j a ha) hm

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eig8_split (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (hW1 : W (Quad.quad LibInt.intR (-2)).one = (Quad.quad LibInt.intR (-2)).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (π : Int × Int) (hπ : nrm π = P + 1 + 1) (pπ : Prim8 π)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 2 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (j : Nat) :
    cw8 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Quad.quad LibInt.intR (-2)).add
      ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).add (W π) (W (cj π))) (cw8 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).mul (W π) (W (cj π))) (cw8 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw8 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime8.cw3_rec P W hW hW1 hp π hπ pπ) j

/-- Inert p: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 8 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 8 * K + 7) (j : Nat) :
    cw8 W ((P + 1 + 1) ^ (j + 2)) = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).zero (cw8 W ((P + 1 + 1) ^ (j + 1))))
      ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).neg (W (Sq2Inert8.nPi P))) (cw8 W ((P + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, Sq2Inert8.cw8_even P W hpr hc (k + 1), Sq2Inert8.cw8_even P W hpr hc k]
    show W ((Quad.quad LibInt.intR (-2)).mul (pw (Quad.quad LibInt.intR (-2)) (Sq2Inert8.nPi P) k) (Sq2Inert8.nPi P)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, Sq2Inert8.cw8_odd P W hc (k + 1), Sq2Inert8.cw8_odd P W hc k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eig8_inert (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Quad.quad LibInt.intR (-2)).mul α β) = (Quad.quad LibInt.intR (-2)).mul (W α) (W β))
    (hpr : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1)
    (hc : ∃ K, Int.ofNat (P + 1 + 1) = 8 * K + 5 ∨ Int.ofNat (P + 1 + 1) = 8 * K + 7)
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 2 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (j : Nat) :
    cw8 W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Quad.quad LibInt.intR (-2)).add ((Quad.quad LibInt.intR (-2)).mul (Quad.quad LibInt.intR (-2)).zero (cw8 W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Quad.quad LibInt.intR (-2)).neg ((Quad.quad LibInt.intR (-2)).mul ((Quad.quad LibInt.intR (-2)).neg (W (Sq2Inert8.nPi P))) (cw8 W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw8 W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (inert_rec P W hW hpr hc) j

end Eig8

#print axioms Eig8.parity
#print axioms Eig8.cond_of
#print axioms Eig8.mul_pow
#print axioms Eig8.eig8_split
#print axioms Eig8.inert_rec
#print axioms Eig8.eig8_inert

#print axioms: 5 theorems, no axioms.

78.343 Hecke relations for \(\mathbb Q(i)\)

Split and inert recursions lifted to all \(p^jm\), \(m\) odd.

\[ c(p^{j+2}m)=a_p\,c(p^{j+1}m)-\psi (p)\,c(p^jm). \]

lang/EigG.lean

/-
  lang/EigG — T50: **32a (ℤ[i])'s Hecke theta series is a Hecke eigenform at every odd prime**: for odd m prime to p,
    eigG_split   p = a² + b²:   c(p^(j+2) m) = (W(π) + W(π̄)) c(p^(j+1) m) − W(π)W(π̄) c(p^j m)
    eigG_inert   p = 4r + 3:    c(p^(j+2) m) = W(−p) c(p^j m)   (and c(p^(2k+1) m) = 0)
  From HeckeMultW, HeckePrimeW, HeckeInertW through HeckeEig.lift2.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.HeckeInertW
import lang.HeckeEig

namespace EigG

open FTAChain DetChain LibInt GaussEuclid GaussInt GaussFactor GaussUnique HeckeMult YMChain FiniteRecChain Frobenius LibGcd

theorem parity : ∀ j : Nat, (∃ k, j = 2 * k) ∨ (∃ k, j = 2 * k + 1)
  | 0 => Or.inl ⟨0, rfl⟩
  | j + 1 => by
    rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
    · exact Or.inr ⟨k, by rw [hk]⟩
    · exact Or.inl ⟨k + 1, by rw [hk]; lib_nat⟩

/-- p^j odd from u p + 2 v = 1. -/
theorem cond_of (p : Nat) (u v : Int) (huv : u * Int.ofNat p + v * 2 = 1) (j a : Nat) (ha : p ^ j = a + 1) : ∃ k, Int.ofNat (a + 1) = 2 * k + 1 := by
  rcases GaussInt.int_even_odd (Int.ofNat (a + 1)) with ⟨q, hq⟩ | ⟨q, hq⟩
  · exact absurd (by rw [ha]; exact hq) (HeckeEig.not_div p 2 u v huv (Nat.le_of_ble_eq_true rfl) j q)
  · exact ⟨q, hq⟩

/-- c(p^j m) = c(p^j) c(m) for odd m prime to p. -/
theorem mul_pow (P : Nat) (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 2 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (j : Nat) :
    cw W ((P + 1 + 1) ^ j * (m + 1)) = (Cyclo4.cyc4 LibInt.intR).mul (cw W ((P + 1 + 1) ^ j)) (cw W (m + 1)) := by
  obtain ⟨a, ha⟩ := HeckeEig.succ_pow (P + 1) j
  obtain ⟨s', t', h'⟩ := HeckeEig.bez_pow (P + 1 + 1) _ s t hst j
  rw [ha] at h'
  rw [ha]
  obtain ⟨km, hkm⟩ := cond_of (P + 1 + 1) u v huv j a ha
  obtain ⟨kn, hkn⟩ := hm
  exact hecke_mult_w W hW a m s' t' km kn h' hkm hkn

/-- **Split p: the Hecke relation at every p^j m.** -/
theorem eigG_split (P : Nat) (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hW1 : W (Cyclo4.cyc4 LibInt.intR).one = (Cyclo4.cyc4 LibInt.intR).one)
    (hp : ∀ d, Dv d (P + 1 + 1) → d = 1 ∨ d = P + 1 + 1) (a b : Int) (hab : a * a + b * b = Int.ofNat (P + 1 + 1)) (hπ : Prim (a, b))
    (u v : Int) (huv : u * Int.ofNat (P + 1 + 1) + v * 2 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (P + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (j : Nat) :
    cw W ((P + 1 + 1) ^ (j + 2) * (m + 1)) = (Cyclo4.cyc4 LibInt.intR).add
      ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).add (W (a, b)) (W (a, -b))) (cw W ((P + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).mul (W (a, b)) (W (a, -b))) (cw W ((P + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw W) (P + 1 + 1) (m + 1) (mul_pow P W hW u v huv m s t hst hm) _ _ (HeckePrime.cw_rec P W hW hW1 hp a b hab hπ) j

/-- Inert p = 4r + 3: c(p^(j+2)) = W(−p) c(p^j). -/
theorem inert_rec (r : Nat) (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1)) (j : Nat) :
    cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (j + 2)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero (cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (j + 1))))
      ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).neg (W (HeckeInert.mq r))) (cw W ((2 * (2 * r) + 1 + 1 + 1) ^ j)))) := by
  rcases parity j with ⟨k, hk⟩ | ⟨k, hk⟩
  · rw [hk, show 2 * k + 2 = 2 * (k + 1) by lib_nat, HeckeInert.cw_even r W hq (k + 1), HeckeInert.cw_even r W hq k]
    show W ((Cyclo4.cyc4 LibInt.intR).mul (pw (Cyclo4.cyc4 LibInt.intR) (HeckeInert.mq r) k) (HeckeInert.mq r)) = _
    rw [hW]; lib_ring
  · rw [hk, show 2 * k + 1 + 2 = 2 * (k + 1) + 1 by lib_nat, HeckeInert.cw_odd r W hq (k + 1), HeckeInert.cw_odd r W hq k]; lib_ring

/-- **Inert p: the Hecke relation at every p^j m.** -/
theorem eigG_inert (r : Nat) (W : Int × Int → Int × Int) (hW : ∀ α β, W ((Cyclo4.cyc4 LibInt.intR).mul α β) = (Cyclo4.cyc4 LibInt.intR).mul (W α) (W β))
    (hq : ∀ d, Dv d (2 * (2 * r) + 1 + 1 + 1) → d = 1 ∨ d = (2 * (2 * r) + 1 + 1 + 1))
    (u v : Int) (huv : u * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) + v * 2 = 1) (m : Nat) (s t : Int) (hst : s * Int.ofNat (2 * (2 * r) + 1 + 1 + 1) + t * Int.ofNat (m + 1) = 1)
    (hm : ∃ k, Int.ofNat (m + 1) = 2 * k + 1) (j : Nat) :
    cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (j + 2) * (m + 1)) = (Cyclo4.cyc4 LibInt.intR).add ((Cyclo4.cyc4 LibInt.intR).mul (Cyclo4.cyc4 LibInt.intR).zero (cw W ((2 * (2 * r) + 1 + 1 + 1) ^ (j + 1) * (m + 1))))
      ((Cyclo4.cyc4 LibInt.intR).neg ((Cyclo4.cyc4 LibInt.intR).mul ((Cyclo4.cyc4 LibInt.intR).neg (W (HeckeInert.mq r))) (cw W ((2 * (2 * r) + 1 + 1 + 1) ^ j * (m + 1))))) :=
  HeckeEig.lift2 _ (cw W) (2 * (2 * r) + 1 + 1 + 1) (m + 1) (mul_pow (2 * (2 * r) + 1) W hW u v huv m s t hst hm) _ _ (inert_rec r W hW hq) j

end EigG

#print axioms EigG.parity
#print axioms EigG.cond_of
#print axioms EigG.mul_pow
#print axioms EigG.eigG_split
#print axioms EigG.inert_rec
#print axioms EigG.eigG_inert

#print axioms: 6 theorems, no axioms.

78.344 The ramified primes for \(d=1,2,3\)

Primary elements have norm prime to the ramified prime.

\[ c_W(2n)=0\ (d=1,2),\qquad c_W(3n)=0\ (d=3). \]

lang/RamS.lean

/-
  lang/RamS — T50: **the theta series of ℚ(i), ℚ(√−3), ℚ(√−2) vanish at the ramified prime**:
    no_primG, cwG_ram   primary Gaussian integers have odd norm, so c_W(2n) = 0          (32a)
    no_prim3, cw3_ram   primary elements of ℤ[ω] have norm prime to 3, so c_W(3n) = 0   (27a, 36a)
    no_prim8, cw8_ram   primary elements of ℤ[√−2] have odd first coordinate, so c_W(2n) = 0 (256a)
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.HeckeMultW
import lang.HeckeMult3
import lang.HeckeMult8
import lang.HeckeEig

namespace RamS

open FTAChain DetChain LibInt YMChain FiniteRecChain

/-- a = 2g + 1 and a² + k b² = 2n (k ∈ {1, 2} via k b² = 2 h) is impossible. -/
theorem odd_sq (a g h n : Int) (ha : a = 2 * g + 1) (hn : a * a + 2 * h = 2 * n) : False :=
  HeckeEig.nd 2 1 (n + -(2 * (g * g)) + -(2 * g) + -h) (by
    calc (2 : Int) * (n + -(2 * (g * g)) + -(2 * g) + -h) = 2 * n + -(a * a + 2 * h) + 1 := by rw [ha]; lib_int
      _ = 1 := by rw [hn]; lib_int) (Nat.zero_lt_succ 0) (Nat.le_refl 2)

/-- Primary Gaussian integers have odd norm. -/
theorem no_primG (z : Int × Int) (n : Nat) (h : GaussEuclid.gn z = 2 * Int.ofNat n) (hp : GaussUnique.Prim z) : False := by
  obtain ⟨⟨f, hf⟩, ⟨k, hk⟩⟩ := hp
  have hf' : z.2 = 2 * f := hf
  have hk' : z.1 + z.2 - 1 = 4 * k := hk
  have ha : z.1 = 2 * (2 * k + -f) + 1 := by
    calc z.1 = (z.1 + z.2 - 1) + -z.2 + 1 := by show z.1 = (z.1 + z.2 + -1) + -z.2 + 1; lib_int
      _ = 2 * (2 * k + -f) + 1 := by rw [hk', hf']; lib_int
  exact odd_sq z.1 _ (2 * (f * f)) (Int.ofNat n) ha (by
    calc z.1 * z.1 + 2 * (2 * (f * f)) = z.1 * z.1 + (2 * f) * (2 * f) := by lib_int
      _ = 2 * Int.ofNat n := by rw [← hf']; exact h)

/-- **32a: c_W(2n) = 0.** -/
theorem cwG_ram (W : Int × Int → Int × Int) (n : Nat) : HeckeMult.cw W (2 * n) = (Cyclo4.cyc4 LibInt.intR).zero := by
  unfold HeckeMult.cw
  rw [sumK_ext (Cyclo4.cyc4 LibInt.intR) _ (fun _ => (Cyclo4.cyc4 LibInt.intR).zero) (fun z => by
    rw [HeckeMult.I_neg _ _ (fun h => no_primG z n h.1 h.2)]; lib_ring)]
  exact HeckeMult.zsum _

/-- Primary elements of ℤ[ω] have norm prime to 3. -/
theorem no_prim3 (z : Int × Int) (n : Nat) (h : EisEuclid.en z = 3 * Int.ofNat n) (hp : EisPrim.Prim3 z) : False :=
  EisPrim.c3_ne _ _ (EisPrim.prim3_c3 z hp) h

/-- **27a, 36a: c_W(3n) = 0.** -/
theorem cw3_ram (W : Int × Int → Int × Int) (n : Nat) : HeckeMult3.cw3 W (3 * n) = (Cyclo3.cyc3 LibInt.intR).zero := by
  unfold HeckeMult3.cw3
  rw [sumK_ext (Cyclo3.cyc3 LibInt.intR) _ (fun _ => (Cyclo3.cyc3 LibInt.intR).zero) (fun z => by
    rw [HeckeMult3.I3_neg _ _ (fun h => no_prim3 z n h.1 h.2)]; lib_ring)]
  exact HeckeMult3.zsum3 _

/-- Primary elements of ℤ[√−2] have odd norm. -/
theorem no_prim8 (z : Int × Int) (n : Nat) (h : Sq2Euclid.en z = 2 * Int.ofNat n) (hp : Sq2Prim8.Prim8 z) : False := by
  have hodd : ∃ g : Int, z.1 = 2 * g + 1 := by
    rcases hp with ⟨e, f, h1, _⟩ | ⟨e, f, h1, _⟩ | ⟨e, f, h1, _⟩ | ⟨e, f, h1, _⟩ | ⟨e, f, h1, _⟩ | ⟨e, f, h1, _⟩ | ⟨e, f, h1, _⟩ | ⟨e, f, h1, _⟩
    · exact ⟨4 * e, by rw [h1]; lib_int⟩
    · exact ⟨4 * e + 1, by rw [h1]; lib_int⟩
    · exact ⟨4 * e + 2, by rw [h1]; lib_int⟩
    · exact ⟨4 * e + 2, by rw [h1]; lib_int⟩
    · exact ⟨4 * e + 2, by rw [h1]; lib_int⟩
    · exact ⟨4 * e + 3, by rw [h1]; lib_int⟩
    · exact ⟨4 * e + 3, by rw [h1]; lib_int⟩
    · exact ⟨4 * e + 3, by rw [h1]; lib_int⟩
  obtain ⟨g, hg⟩ := hodd
  exact odd_sq z.1 g (z.2 * z.2) (Int.ofNat n) hg h

/-- **256a: c_W(2n) = 0.** -/
theorem cw8_ram (W : Int × Int → Int × Int) (n : Nat) : HeckeMult8.cw8 W (2 * n) = (Quad.quad LibInt.intR (-2)).zero := by
  unfold HeckeMult8.cw8
  rw [sumK_ext (Quad.quad LibInt.intR (-2)) _ (fun _ => (Quad.quad LibInt.intR (-2)).zero) (fun z => by
    rw [HeckeMult8.I8_neg _ _ (fun h => no_prim8 z n h.1 h.2)]; lib_ring)]
  exact HeckeMult8.zsum8 _

end RamS

#print axioms RamS.odd_sq
#print axioms RamS.no_primG
#print axioms RamS.cwG_ram
#print axioms RamS.no_prim3
#print axioms RamS.cw3_ram
#print axioms RamS.no_prim8
#print axioms RamS.cw8_ram

#print axioms: 7 theorems, no axioms.

78.345 Eight divides the point count with torsion \(\mathbb Z/4\times \mathbb Z/2\)

For \(y^2=x(x+a^2)(x+b^2)\) the translations by \(P=(ab,ab(a+b))\) (order four) and by \((-a^2,0)\) generate \(\mathbb Z/4\times \mathbb Z/2\) acting freely on \(E\cup \{O\}\).

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2=x(x+a^2)(x+b^2)\}\equiv 0\pmod 8. \]

lang/Z4Z2g.lean

/-
  lang/Z4Z2g — T43, cycle 427: **8 | #E(𝔽_p) ∪ {O} for y² = x(x + a²)(x + b²)** (torsion Z/4 × Z/2: P = (ab, ab(a + b)) of order 4
  and T_a = (−a², 0)), a, b, a ± b ≠ 0, p odd.

  Orbit42 with t = τ_P (Z4Z2c), s = σ_a (FullTwo.ta at −a², −b²): t⁴ = σ₀² = id (Z4Z2c.tauP2, G5Two.tau2), s² = id (FullTwo.TT2),
  st = ts (Z4Z2f.comm), the curve indicator is invariant (Z4Z2c.onE_tauP, FullTwo.onE_TT). Fixed points: t, t², t³, st, st³ all
  lead to σ₀ x = x (the elements of order 4 square to σ₀), s and st² = σ_b (FullTwo.key) are the other 2-torsion translations;
  none fixes a point of E ∪ {O} (G5Two.onE_fix, FullTwo.onE_TTfix: discriminants (a² − b²)², b⁴, a⁴ ≠ 0).
    sig_eq, g8
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Z4Z2f
import lang.Orbit42

namespace Z4Z2g

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map Pairs G5Two Z4Z2 Z4Z2c Z4Z2d Orbit42
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat) (a b : Zp (2 * m + 1 + 1))

theorem sig_eq (Q : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) : FullTwo.sig m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))) Q = G5Two.tau m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)  -- (+47 chars; full line in the Zenodo source)
  unfold FullTwo.sig
  rw [show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b)) by lib_ring, show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).m  -- (+194 chars; full line in the Zenodo source)

/-- **8 | 1 + #E(𝔽_p)** for y² = x(x + a²)(x + b²). -/
theorem g8 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hg : Good m a b) :
    ∃ k : Int, 1 + sumK LibInt.intR (cnt2 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b)))) (pL m) = 8 * k := by
  have hBn : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b)) ≠ (ring (2 * m + 1 + 1)).zero := nz_mul m hpr _ _ (nz_mul m hpr _ _ hg.1 hg.1) (nz_mul m hpr _ _ hg.2.1 hg.2.1)
  have haa : (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)) ≠ (ring (2 * m + 1 + 1)).zero := fun e => nz_mul m hpr _ _ hg.1 hg.1 (by rw [show (ring (2 * m + 1 + 1)).mul (a) (a) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) by lib_ring, e]; lib_ring)
  have hbb : (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)) ≠ (ring (2 * m + 1 + 1)).zero := fun e => nz_mul m hpr _ _ hg.2.1 hg.2.1 (by rw [show (ring (2 * m + 1 + 1)).mul (b) (b) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))) by lib_ring, e]; lib_ring)
  have hab' : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => nz_mul m hpr _ _ hg.2.2.2.1 hg.2.2.1
    (by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (b))) ((ring (2 * m + 1 + 1)).add (a) (b)) = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))))) by lib_ring, e]; lib_ring)
  have hBa : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))))) ≠ (ring (2 * m + 1 + 1)).zero := nz_mul m hpr _ _ haa hab'
  have hd0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m   -- (+266 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m   -- (+536 chars; full line in the Zenodo source)
    exact nz_mul m hpr _ _ (nz_mul m hpr _ _ hg.2.2.2.1 hg.2.2.1) (nz_mul m hpr _ _ hg.2.2.2.1 hg.2.2.1)
  have hda : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))))) ((ring (2 * m + 1 + 1)).ad  -- (+854 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))))) ((ring (2 * m + 1 + 1)).ad  -- (+1024 chars; full line in the Zenodo source)
  have hdb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).ad  -- (+854 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).ad  -- (+1024 chars; full line in the Zenodo source)
  have ht2 : ∀ Q, tauP m a b (tauP m a b Q) = G5Two.tau m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) Q := fun Q => tauP2 m a b hpr hg.1 hg.2.1 hg.2.2.1 hg.2.2.2.2 Q
  have hs2 : ∀ Q, Z4Z2e.sa m a b (Z4Z2e.sa m a b Q) = Q := fun Q => by
    unfold Z4Z2e.sa FullTwo.ta; exact FullTwo.TT2 m _ _ _ hpr hBa Q
  have hst : ∀ Q, Z4Z2e.sa m a b (tauP m a b Q) = tauP m a b (Z4Z2e.sa m a b Q) := fun Q => Z4Z2f.comm m a b hpr hg Q
  have hfix0 : ∀ Q, G5Two.tau m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) Q = Q → G5Two.onE m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 *  -- (+126 chars; full line in the Zenodo source)
  have hfixa : ∀ Q, Z4Z2e.sa m a b Q = Q → G5Two.onE m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) Q = 0 := fun Q h =>
    FullTwo.onE_TTfix m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg  -- (+532 chars; full line in the Zenodo source)
  have hfixb : ∀ Q, FullTwo.tb m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))) Q = Q → G5Two.onE m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) Q = 0 :=   -- (+10 chars; full line in the Zenodo source)
    FullTwo.onE_TTfix m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg  -- (+532 chars; full line in the Zenodo source)
  have e : sumK LibInt.intR (G5Two.onE m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ptsL m) = 1 + sumK LibInt.intR (cnt2 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2  -- (+107 chars; full line in the Zenodo source)
    show LibInt.intR.add 1 (sumK LibInt.intR (G5Two.onE m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b)))) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  refine ⟨_, orbit42 (ptsL m) (complete_pts m) (code m) (code_inj m) (Z4Z2e.sa m a b) (tauP m a b)
    (fun y => hs2 y) (fun y => by show tauP m a b (tauP m a b (tauP m a b (tauP m a b y))) = y; rw [ht2, ht2, G5Two.tau2 m hpr _ _ hBn])
    (fun y => hst y) (G5Two.onE m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))))
    (fun Q => by unfold Z4Z2e.sa FullTwo.ta; exact FullTwo.onE_TT m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) _ _ _ (fun x => by lib_ring) hpr Q)
    (fun Q => onE_tauP m a b hpr Q) ?_⟩
  intro x hx
  have ht4 : ∀ y, tauP m a b (tauP m a b (tauP m a b (tauP m a b y))) = y := fun y => by rw [ht2, ht2, G5Two.tau2 m hpr _ _ hBn]
  have hfix2 : tauP m a b (tauP m a b x) = x → G5Two.onE m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul (b) (b))) x = 0 := fun h => hfix0 x (by rw [← ht2]; exact h)
  rcases hx with h | h | h | h | h | h | h
  · have h' : tauP m a b x = x := h
    exact hfix2 (by rw [h', h'])
  · exact hfix2 h
  · have h' : tauP m a b (tauP m a b (tauP m a b x)) = x := h
    have h1 : tauP m a b x = x := by
      calc tauP m a b x = tauP m a b (tauP m a b (tauP m a b (tauP m a b x))) := by rw [h']
        _ = x := ht4 x
    exact hfix2 (by rw [h1, h1])
  · exact hfixa x h
  · have h' : Z4Z2e.sa m a b (tauP m a b x) = x := h
    exact hfix2 (by
      calc tauP m a b (tauP m a b x) = tauP m a b (Z4Z2e.sa m a b (Z4Z2e.sa m a b (tauP m a b x))) := by rw [hs2]
        _ = Z4Z2e.sa m a b (tauP m a b (Z4Z2e.sa m a b (tauP m a b x))) := (hst _).symm
        _ = x := by rw [h', h'])
  · have h' : Z4Z2e.sa m a b (tauP m a b (tauP m a b x)) = x := h
    rw [ht2, ← sig_eq] at h'
    unfold Z4Z2e.sa at h'
    rw [← FullTwo.comm m _ _ hpr haa hbb hab', FullTwo.key m _ _ hpr haa hbb hab'] at h'
    exact hfixb x h'
  · have h' : Z4Z2e.sa m a b (tauP m a b (tauP m a b (tauP m a b x))) = x := h
    have hc3 : ∀ y, Z4Z2e.sa m a b (tauP m a b (tauP m a b (tauP m a b y))) = tauP m a b (tauP m a b (tauP m a b (Z4Z2e.sa m a b y))) := fun y => by rw [hst, hst, hst]
    have e6 : tauP m a b (tauP m a b (tauP m a b (tauP m a b (tauP m a b (tauP m a b x))))) = x := by
      calc tauP m a b (tauP m a b (tauP m a b (tauP m a b (tauP m a b (tauP m a b x))))) = tauP m a b (tauP m a b (tauP m a b (Z4Z2e.sa m a b (Z4Z2e.sa m a b (tauP m a b (tauP m a b (tauP m a b x))))))) := by rw [hs2]
        _ = Z4Z2e.sa m a b (tauP m a b (tauP m a b (tauP m a b (Z4Z2e.sa m a b (tauP m a b (tauP m a b (tauP m a b x))))))) := (hc3 _).symm
        _ = x := by rw [h', h']
    exact hfix2 (by
      calc tauP m a b (tauP m a b x) = tauP m a b (tauP m a b (tauP m a b (tauP m a b (tauP m a b (tauP m a b x))))) := (ht4 _).symm
        _ = x := e6)

end Z4Z2g

#print axioms Z4Z2g.sig_eq
#print axioms Z4Z2g.g8

#print axioms: 2 theorems, no axioms.

78.346 Twelve divides the point count with torsion \(\mathbb Z/6\times \mathbb Z/2\)

The curve has the 3-torsion point \((0,0)\) and full 2-torsion; an affine change of variables carries it to \(Y^2=X(X+2w+1)(X+w^3(w+2))\).

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2+2(w^2+w+1)xy+2w^2(w+1)^2y=x^3\}\equiv 0\pmod {12}. \]

lang/Z6Z2.lean

/-
  lang/Z6Z2 — T43, cycle 428: **12 | #E(𝔽_p) ∪ {O} for curves with torsion Z/6 × Z/2.**

  E_w : y² + 2(w² + w + 1)xy + 2w²(w + 1)²y = x³ has the 3-torsion point (0, 0) (lang/G5Tate3) and, after X = x + w²,
  Y = y + (w² + w + 1)x + w²(w + 1)², the form Y² = X(X + 2w + 1)(X + w³(w + 2)) with full 2-torsion (lang/FullTwo):
  (y + αx + β)² = x³ + (αx + β)² = (x + w²)(x + (w + 1)²)(x + w²(w + 1)²). The affine bijection preserves the point count
  (FiniteRecChain.reindex), so 3 and 4 divide 1 + #E_aff, hence 12. Conditions: p odd, w ≠ 0, ±1, −2, −1/2
  (a³ − 27b = 2((w − 1)(w + 2)(2w + 1))², a' − b' = (w − 1)(w + 1)³).
    count_eq, g12
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.FullTwo
import lang.G5Tate3

namespace Z6Z2

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map Pairs G5Two
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat) (w : Zp (2 * m + 1 + 1))

theorem nzm (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (u v : Zp (2 * m + 1 + 1)) (hu : u ≠ (ring (2 * m + 1 + 1)).zero) (hv : v ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).mul u v ≠ (ring (2 * m + 1 + 1)).zero := fun e => by
  rcases mul_eq_zero m hpr _ _ e with e1 | e1
  · exact hu e1
  · exact hv e1

theorem count_eq : sumK LibInt.intR (G5Tate3.cnt3 m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul w w) w) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((rin  -- (+1046 chars; full line in the Zenodo source)
  rw [← FiniteRecChain.reindex LibInt.intR (pL m) (complete_prod m) (fun q => ((ring (2 * m + 1 + 1)).add q.1 ((ring (2 * m + 1 + 1)).mul w w), (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add q.2 ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul w w) w) (ring (2 * m + 1 + 1)).one) q.1)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +  -- (+841 chars; full line in the Zenodo source)
    (fun q => Prod.ext (by show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add q.1 ((ring (2 * m + 1 + 1)).mul w w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul w w)) = q.1; lib_ring) (by show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add q.2 ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m +   -- (+879 chars; full line in the Zenodo source)
    (fun q => Prod.ext (by show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add q.1 ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul w w))) ((ring (2 * m + 1 + 1)).mul w w) = q.1; lib_ring) (by show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add q.2 ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +   -- (+1713 chars; full line in the Zenodo source)
  apply sumK_ext
  intro q
  obtain ⟨x, y⟩ := q
  show (if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul w w) w) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (x) (y)))  -- (+2720 chars; full line in the Zenodo source)
  by_cases h : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul w w) w) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (x) (  -- (+427 chars; full line in the Zenodo source)
  · rw [ite_pos' _ _ h, ite_pos' _ _ (by
      calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul w w) w) (ring (2 * m + 1 + 1)).one) x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add w (ring (2 * m + 1 + 1)).one) ((rin  -- (+2583 chars; full line in the Zenodo source)
        _ = _ := by rw [h]; lib_ring)]
  · rw [ite_neg' _ _ h, ite_neg' _ _ (fun h' => h (by
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul w w) w) (ring (2 * m + 1 + 1)).one) x)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add w (  -- (+1881 chars; full line in the Zenodo source)
        _ = _ := by rw [h']; lib_ring))]

/-- **12 | 1 + #E_aff(𝔽_p)** for y² + 2(w² + w + 1)xy + 2w²(w + 1)²y = x³. -/
theorem g12 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hw : w ≠ (ring (2 * m + 1 + 1)).zero) (hm1 : (ring (2 * m + 1 + 1)).add w ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (hp1 : (ring (2 * m + 1 + 1)).add w (r  -- (+205 chars; full line in the Zenodo source)
    (h21 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add w w) (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) :
    ∃ k : Int, 1 + sumK LibInt.intR (G5Tate3.cnt3 m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul w w) w) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((rin  -- (+225 chars; full line in the Zenodo source)
  have hb : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add w (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add w (ring (2 * m + 1 + 1)).one))) ≠ (ring (2 * m + 1 + 1)).zero := nzm m hpr _ _ h2 (nzm m hpr _ _ (nzm m  -- (+40 chars; full line in the Zenodo source)
  have hd : G5Tate3.d27 m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul w w) w) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((r  -- (+212 chars; full line in the Zenodo source)
    rw [show G5Tate3.d27 m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul w w) w) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((  -- (+1047 chars; full line in the Zenodo source)
    exact nzm m hpr _ _ h2 (nzm m hpr _ _ (nzm m hpr _ _ (nzm m hpr _ _ hm1 hp2) h21) (nzm m hpr _ _ (nzm m hpr _ _ hm1 hp2) h21))
  obtain ⟨k3, hk3⟩ := G5Tate3.g5 m hpr _ _ hb hd
  have ha' : (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add w w) (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := fun e => h21 (by rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add w w) (ring (2 * m + 1 + 1)).one = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add w w) (ring (  -- (+48 chars; full line in the Zenodo source)
  have hb' : (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w ((ring (2 * m + 1 + 1)).mul w w)) ((ring (2 * m + 1 + 1)).add w ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => nzm m hpr _ _ (nzm m hpr _ _ hw (nzm m hpr _ _ hw hw)) hp2 (by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 *   -- (+457 chars; full line in the Zenodo source)
  have hab' : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add w w) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w ((ring (2 * m + 1 + 1)).mul w w)) ((ring (2 * m + 1 + 1)).add w ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2  -- (+127 chars; full line in the Zenodo source)
    (by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add w ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add w (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add w (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add w (ring (2 * m + 1 + 1)).one))) = (ring (2 * m + 1 + 1)).add ((ring (  -- (+391 chars; full line in the Zenodo source)
  obtain ⟨k4, hk4⟩ := FullTwo.g4full m ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add w w) (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul w ((ring (2 * m + 1 + 1)).mul w w)) ((ring (2 * m + 1 + 1)).add w ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)))) hpr ha'  -- (+12 chars; full line in the Zenodo source)
  rw [← count_eq m w] at hk4
  have e34 : 3 * k3 = 4 * k4 := hk3.symm.trans hk4
  exact ⟨k3 - k4, by
    rw [hk3]
    calc 3 * k3 = 12 * k3 - 3 * (3 * k3) := by lib_int
      _ = 12 * k3 - 3 * (4 * k4) := by rw [e34]
      _ = 12 * (k3 - k4) := by lib_int⟩

end Z6Z2

#print axioms Z6Z2.nzm
#print axioms Z6Z2.count_eq
#print axioms Z6Z2.g12

#print axioms: 3 theorems, no axioms.

78.347 Ten divides the point count with torsion \(\mathbb Z/10\)

On the order-5 Tate curve with \(t(u-1)^2=u^2(2u-1)\) the point \(x_0=-u^2\) is 2-torsion; \(5\mid \) and \(2\mid \) combine.

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2+(1-t)xy-ty=x^3-tx^2\}\equiv 0\pmod {10}. \]

lang/Z10.lean

/-
  lang/Z10 — T43, cycle 429: **10 | #E(𝔽_p) ∪ {O} for curves with torsion Z/10.**

  E(t, t): y² + (1 − t)xy − ty = x³ − tx² (5-torsion point (0, 0), lang/T5Done) with t(u − 1)² = u²(2u − 1) has the rational 2-torsion
  point x₀ = −u² (found by a rational-point search: t = u²(2u − 1)/(u − 1)²). (X, Y) = (4(x − x₀), 4(2y + (1 − t)x − t)) carries E to
  Y² = X³ + AX² + BX, A = 12x₀ + b₂, B = 48x₀² + 8b₂x₀ + 8b₄ (b₂ = (1 − t)² − 4t, b₄ = −t(1 − t)): Y² − X(X² + AX + B) = 64·(E) + 16·c(x₀),
  c(x) = 4x³ + b₂x² + 2b₄x + b₆, and c(x₀)(u − 1)⁴ = 0 by the relation. So 5 | 1 + #E (T5Done) and 2 | 1 + #E (G5Two.g2), hence 10.
    c_x0, hk1/scale/key10 (change of variables with x − x₀ as atom), zp0–zp3, trans5, count_eq, g10
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.T5Done
import lang.G5Two
import lang.Z4Z2

namespace Z10

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map Pairs G5Two
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat) (t u : Zp (2 * m + 1 + 1))

set_option maxRecDepth 20000 in
theorem c_x0 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hrel : (ring (2 * m + 1 + 1)).mul (t) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (u) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (u) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (u) (u)) (  -- (+3547 chars; full line in the Zenodo source)
  have h : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (u) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (u) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 *  -- (+3063 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (t) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (u) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (u) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (u)   -- (+1342 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hrel]; lib_ring
  rcases mul_eq_zero m hpr _ _ h with e | e
  · rcases mul_eq_zero m hpr _ _ e with e1 | e1 <;> exact absurd e1 hD
  · exact e

theorem hk1 (t x y w0 : Zp (2 * m + 1 + 1)) : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (t))) (x))  -- (+7417 chars; full line in the Zenodo source)

theorem scale (Y zz c d : Zp (2 * m + 1 + 1)) : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) (Y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((rin  -- (+3116 chars; full line in the Zenodo source)

theorem lin16 (a b : Zp (2 * m + 1 + 1)) : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m   -- (+1585 chars; full line in the Zenodo source)

theorem key10 (x y w0 : Zp (2 * m + 1 + 1)) : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 *   -- (+10344 chars; full line in the Zenodo source)
  rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 +  -- (+3983 chars; full line in the Zenodo source)
  refine (scale m _ _ _ _).trans ?_
  rw [hk1 m t x y w0, show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (t))) (x)) (y))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 *  -- (+1053 chars; full line in the Zenodo source)
  exact lin16 m _ _

theorem zp0 (i2 : Zp (2 * m + 1 + 1)) (ei : (ring (2 * m + 1 + 1)).mul i2 ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one) (q : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (i2) (i2)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 *  -- (+663 chars; full line in the Zenodo source)
  calc _ = (ring (2 * m + 1 + 1)).add (q.1) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i2 ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i2 ((ring (2 * m + 1 + 1)).add ((ring (2  -- (+372 chars; full line in the Zenodo source)
    _ = _ := by rw [ei]; lib_ring

theorem zp1 (i2 : Zp (2 * m + 1 + 1)) (ei : (ring (2 * m + 1 + 1)).mul i2 ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one) (q : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : (ring (2 * m + 1 + 1)).mul (i2) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (i2) (i2)) ((ring  -- (+1895 chars; full line in the Zenodo source)
  calc _ = (ring (2 * m + 1 + 1)).add (q.2) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i2 ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 *   -- (+1206 chars; full line in the Zenodo source)
    _ = _ := by rw [ei]; lib_ring

theorem zp2 (i2 : Zp (2 * m + 1 + 1)) (ei : (ring (2 * m + 1 + 1)).mul i2 ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one) (q : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 +  -- (+663 chars; full line in the Zenodo source)
  calc _ = (ring (2 * m + 1 + 1)).add (q.1) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i2 ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i2 ((ring (2 * m + 1 + 1)).add ((ring (2  -- (+103 chars; full line in the Zenodo source)
    _ = _ := by rw [ei]; lib_ring

theorem zp3 (i2 : Zp (2 * m + 1 + 1)) (ei : (ring (2 * m + 1 + 1)).mul i2 ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one) (q : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 +  -- (+1702 chars; full line in the Zenodo source)
  calc _ = (ring (2 * m + 1 + 1)).add (q.2) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul i2 ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 *   -- (+1330 chars; full line in the Zenodo source)
    _ = _ := by rw [ei]; lib_ring

theorem trans5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (L R E1 E2 c S T : Zp (2 * m + 1 + 1)) (hS : S ≠ (ring (2 * m + 1 + 1)).zero)
    (hk : (ring (2 * m + 1 + 1)).add L ((ring (2 * m + 1 + 1)).neg R) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul S ((ring (2 * m + 1 + 1)).add E1 ((ring (2 * m + 1 + 1)).neg E2))) ((ring (2 * m + 1 + 1)).mul T c)) (hc : c = (ring (2 * m + 1 + 1)).zero) :
    (L = R → E1 = E2) ∧ (E1 = E2 → L = R) := by
  rw [hc] at hk
  refine ⟨fun hL => ?_, fun hE => ?_⟩
  · rw [hL] at hk
    have e0 : (ring (2 * m + 1 + 1)).mul S ((ring (2 * m + 1 + 1)).add E1 ((ring (2 * m + 1 + 1)).neg E2)) = (ring (2 * m + 1 + 1)).zero := by
      calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul S ((ring (2 * m + 1 + 1)).add E1 ((ring (2 * m + 1 + 1)).neg E2))) ((ring (2 * m + 1 + 1)).mul T (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero) := by lib_ring
        _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add R ((ring (2 * m + 1 + 1)).neg R)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero) := by rw [hk]
        _ = _ := by lib_ring
    rcases mul_eq_zero m hpr _ _ e0 with e1 | e1
    · exact absurd e1 hS
    · exact Z4Z2.sub0 m _ _ e1
  · rw [hE] at hk
    exact Z4Z2.sub0 m _ _ (hk.trans (by lib_ring))

theorem count_eq (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (hrel : (ring (2 * m + 1 + 1)).mul (t) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (u) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (u) (  -- (+460 chars; full line in the Zenodo source)
    sumK LibInt.intR (T5LF.cnt5 m t) (pL m) = sumK LibInt.intR (cnt2 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m   -- (+4011 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ h2
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) = i2 at ei
  have hc := c_x0 m t u hpr hrel hD
  have hkey := fun x y => key10 m t x y ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (u) (u))))
  have p0 := zp0 m u i2 ei
  have p1 := zp1 m t u i2 ei
  have p2 := zp2 m u i2 ei
  have p3 := zp3 m t u i2 ei
  rw [← FiniteRecChain.reindex LibInt.intR (pL m) (complete_prod m) (fun q => ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (q.1) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (  -- (+2060 chars; full line in the Zenodo source)
    (fun q => Prod.ext (p0 q) (p1 q))
    (fun q => Prod.ext (p2 q) (p3 q)) (cnt2 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 *  -- (+3974 chars; full line in the Zenodo source)
  apply sumK_ext
  intro q
  obtain ⟨x, y⟩ := q
  show (if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (t))) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (t) y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).mu  -- (+9165 chars; full line in the Zenodo source)
  have hnz : ∀ a b : Zp (2 * m + 1 + 1), a ≠ (ring (2 * m + 1 + 1)).zero → b ≠ (ring (2 * m + 1 + 1)).zero → (ring (2 * m + 1 + 1)).mul a b ≠ (ring (2 * m + 1 + 1)).zero := fun a b ha hb e =>
    (mul_eq_zero m hpr _ _ e).elim ha hb
  have h4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero := fun e => hnz _ _ h2 h2 (by rw [← e]; lib_ring)
  have h64 := hnz _ _ (hnz _ _ h4 h4) h4
  have tr := trans5 m hpr _ _ _ _ _ _ _ h64 (hkey x y) hc
  by_cases h : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (t))) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (t) y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)  -- (+106 chars; full line in the Zenodo source)
  · rw [ite_pos' _ _ h, ite_pos' _ _ (tr.2 h)]
  · rw [ite_neg' _ _ h, ite_neg' _ _ (fun h' => h (tr.1 h'))]

/-- **10 | 1 + #E(𝔽_p)** for E(t, t) with t(u − 1)² = u²(2u − 1) (torsion Z/10). -/
theorem g10 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (hrel : (ring (2 * m + 1 + 1)).mul (t) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (u) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add (u) ((ring  -- (+454 chars; full line in the Zenodo source)
    (ht0 : t ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (t) (t)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2  -- (+3378 chars; full line in the Zenodo source)
    (hAB : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 +  -- (+5575 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (T5LF.cnt5 m t) (pL m) = 10 * k := by
  obtain ⟨k5, hk5⟩ := T5LF.g5t m hpr t ht0 hD
  obtain ⟨k2, hk2⟩ := g2 m hpr _ _ hB h2 hAB
  rw [← count_eq m t u hpr h2 hrel hD1] at hk2
  have e52 : 5 * k5 = 2 * k2 := hk5.symm.trans hk2
  exact ⟨3 * k5 - k2, by
    rw [hk5]
    calc 5 * k5 = 6 * (5 * k5) - 5 * (5 * k5) := by lib_int
      _ = 6 * (5 * k5) - 5 * (2 * k2) := by rw [e52]
      _ = 10 * (3 * k5 - k2) := by lib_int⟩

end Z10
#print axioms Z10.c_x0
#print axioms Z10.hk1
#print axioms Z10.scale
#print axioms Z10.lin16
#print axioms Z10.key10
#print axioms Z10.zp0
#print axioms Z10.zp1
#print axioms Z10.zp2
#print axioms Z10.zp3
#print axioms Z10.trans5
#print axioms Z10.count_eq
#print axioms Z10.g10

#print axioms: 12 theorems, no axioms.

78.348 Twelve divides the point count with torsion \(\mathbb Z/12\)

On the order-4 Tate curve a rational flex \((x_3,y_3)\) with tangent slope \(l\) is moved to the origin; \(4\mid \) and \(3\mid \) combine.

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2+xy-ty=x^3-tx^2\}\equiv 0\pmod {12}. \]

lang/Z12.lean

/-
  lang/Z12 — T43, cycle 431: **12 | #E(𝔽_p) ∪ {O} for curves with torsion Z/12.**

  E_t: y² + xy − ty = x³ − tx² ((0, 0) of order 4, lang/T4Count) with a point R = (x₃, y₃) on it whose tangent y = y₃ + l(x − x₃)
  meets E only at R (a flex, hence R of order 3): r0 (R on E), r1 (no X term), r2 (no X² term). Then X = x − x₃, Y = y − y₃ − lX carries
  E_t to Y² + (1 + 2l)XY + (2y₃ + x₃ − t)Y = X³ (lang/G5Tate3, 3 |); 4 | and 3 | give 12 |.
  Over ℚ: x₃ = ts, s = 4/(k² + 3), t = −(k² − 1)³(k² + 3)/(256k²) (from ψ₃(t, ts) = t³(t(3s⁴ − 4s³) + (s − 1)³) and −s(3s − 4) = □).
    key12, cond_iff, count_eq, g12
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.T4Count
import lang.G5Tate3
import lang.Z4Z2

namespace Z12

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map Pairs
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat) (t x3 y3 l : Zp (2 * m + 1 + 1))

theorem key12 (x y r0 r1 r2 : Zp (2 * m + 1 + 1))
    (e0 : r0 = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y3 y3) ((ring (2 * m + 1 + 1)).mul x3 y3)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul t y3))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x3 ((ring (2 * m + 1 + 1)).mul x3 x3)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +  -- (+1011 chars; full line in the Zenodo source)
    (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul t y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m  -- (+1820 chars; full line in the Zenodo source)
      ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add r0 ((ring (2 * m + 1 + 1)).mul r1 ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg x3)))) ((ring (2 * m + 1 + 1)).mul r2 ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg x3)) ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg x3))))) := by
  rw [e0, e1, e2]; lib_ring

theorem cond_iff (hr0 : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y3 y3) ((ring (2 * m + 1 + 1)).mul x3 y3)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul t y3))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x3 ((ring (2 * m + 1 + 1)).mul x3 x3)) ((ring (2 * m + 1 + 1)).neg ((ring  -- (+1129 chars; full line in the Zenodo source)
    (((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul t y))) = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring   -- (+3775 chars; full line in the Zenodo source)
  have k := key12 m t x3 y3 l x y _ _ _ hr0.symm hr1.symm hr2.symm
  have k' : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul t y))) ((ring (2 * m + 1 + 1)).neg ((rin  -- (+1824 chars; full line in the Zenodo source)
  refine ⟨fun h => Z4Z2.sub0 m _ _ (k'.symm.trans (by rw [h]; lib_ring)), fun h => Z4Z2.sub0 m _ _ (k'.trans (by rw [h]; lib_ring))⟩

theorem count_eq (hr0 : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y3 y3) ((ring (2 * m + 1 + 1)).mul x3 y3)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul t y3))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x3 ((ring (2 * m + 1 + 1)).mul x3 x3)) ((ring (2 * m + 1 + 1)).neg ((ring  -- (+1102 chars; full line in the Zenodo source)
    sumK LibInt.intR (T4LF.cnt4 m t) (pL m) = sumK LibInt.intR (G5Tate3.cnt3 m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add l l)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y3 y3) x3) ((ring (2 * m + 1 + 1)).neg t))) (pL m) := by
  rw [← FiniteRecChain.reindex LibInt.intR (pL m) (complete_prod m)
    (fun q => ((ring (2 * m + 1 + 1)).add q.1 ((ring (2 * m + 1 + 1)).neg x3), (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add q.2 ((ring (2 * m + 1 + 1)).neg y3)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add q.1 ((ring (2 * m + 1 + 1)).neg x3))))))
    (fun q => ((ring (2 * m + 1 + 1)).add q.1 x3, (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add q.2 y3) ((ring (2 * m + 1 + 1)).mul l q.1)))
    (fun q => Prod.ext (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add q.1 ((ring (2 * m + 1 + 1)).neg x3)) x3 = q.1 by lib_ring)
      (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add q.2 ((ring (2 * m + 1 + 1)).neg y3)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add q.1 ((ring (2 * m + 1 + 1)).neg x3))))) y3) ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add q.1 ((ring (2 * m + 1 + 1)).neg x3))) = q.2 by lib_r  -- (+5 chars; full line in the Zenodo source)
    (fun q => Prod.ext (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add q.1 x3) ((ring (2 * m + 1 + 1)).neg x3) = q.1 by lib_ring)
      (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add q.2 y3) ((ring (2 * m + 1 + 1)).mul l q.1)) ((ring (2 * m + 1 + 1)).neg y3)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul l ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add q.1 x3) ((ring (2 * m + 1 + 1)).neg x3)))) = q.2 by lib_ring))
    (G5Tate3.cnt3 m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add l l)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y3 y3) x3) ((ring (2 * m + 1 + 1)).neg t)))]
  apply sumK_ext
  intro q
  obtain ⟨x, y⟩ := q
  show (if ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul t y))) = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x (  -- (+1740 chars; full line in the Zenodo source)
  have tr := cond_iff m t x3 y3 l hr0 hr1 hr2 x y
  by_cases h : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul t y))) = ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul  -- (+130 chars; full line in the Zenodo source)
  · rw [ite_pos' _ _ h, ite_pos' _ _ (tr.1 h)]
  · rw [ite_neg' _ _ h, ite_neg' _ _ (fun h' => h (tr.2 h'))]

/-- **12 | 1 + #E_t(𝔽_p)** when E_t has a rational flex R = (x₃, y₃) (torsion Z/12). -/
theorem g12 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (ht0 : t ≠ (ring (2 * m + 1 + 1)).zero)
    (hD : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (  -- (+150 chars; full line in the Zenodo source)
    (hr0 : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y3 y3) ((ring (2 * m + 1 + 1)).mul x3 y3)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul t y3))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x3 ((ring (2 * m + 1 + 1)).mul x3 x3)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 +  -- (+1087 chars; full line in the Zenodo source)
    (hb : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y3 y3) x3) ((ring (2 * m + 1 + 1)).neg t)) ≠ (ring (2 * m + 1 + 1)).zero) (hd : G5Tate3.d27 m ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).add l l)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y3 y3) x3) ((ring (2 * m + 1 + 1)).neg   -- (+36 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (T4LF.cnt4 m t) (pL m) = 12 * k := by
  obtain ⟨k4, hk4⟩ := T4LF.g4t m hpr t ht0 h2 hD
  obtain ⟨k3, hk3⟩ := G5Tate3.g5 m hpr _ _ hb hd
  rw [← count_eq m t x3 y3 l hr0 hr1 hr2] at hk3
  have e43 : 4 * k4 = 3 * k3 := hk4.symm.trans hk3
  exact ⟨k3 - k4, by
    rw [hk4]
    calc 4 * k4 = 4 * (4 * k4) - 3 * (4 * k4) := by lib_int
      _ = 4 * (3 * k3) - 3 * (4 * k4) := by rw [e43]
      _ = 12 * (k3 - k4) := by lib_int⟩

end Z12

#print axioms Z12.key12
#print axioms Z12.cond_iff
#print axioms Z12.count_eq
#print axioms Z12.g12

#print axioms: 4 theorems, no axioms.

78.349 Eight divides the point count with torsion \(\mathbb Z/8\)

On the order-4 Tate curve with \(t=-w^2(w-1)^2\) the point \(P=(w^2(w-1),w^3(w-1)^2)\) has \(2P=(0,0)\); translation by \(P\) squares to translation by \((0,0)\).

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2+xy+w^2(w-1)^2y=x^3+w^2(w-1)^2x^2\}\equiv 0\pmod 8. \]

lang/Z8Count.lean

/-
  lang/Z8Count — T43, cycle 442: **8 | #E(𝔽_p) ∪ {O} for curves with torsion Z/8.**

  E_t: y² + xy − ty = x³ − tx² with t = −v², v = w(w − 1) (b = v, a = w, relation a² = a + b): P = (wv, wv²) has 2P = T = (0, 0),
  so P has order 8. σ = translation by P (lang/Z8Map) satisfies σσ = τ (lang/Z8All), hence σ⁸ = τ⁴ = id (lang/T4Count.tau4) and
  σ⁴ = τ², whose fixed points on E ∪ {O} do not exist (T4Count.onG_fix4); onG∘σ = onG; lang/Orbit8 gives 8 | 1 + #E.
  Nondegeneracy: p odd, w ≠ 0, v ≠ 0 (so w ≠ 1), 2w − 1 ≠ 0, 16t + 1 ≠ 0; every other condition (ND of lang/Z8Spec, the α, β of
  lang/Z8Gen) is a product of w, v, w − 1, 2w − 1 modulo a² = a + b.
    g8
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Z8All
import lang.Orbit8
import lang.T4Count

namespace Z8Count

open FTAChain ZMod YMChain FiniteRecChain QChar G4Gen G7Tate1 G7LF ConicSum Pairs Z8Map

variable (m : Nat)

/-- **8 | 1 + #E(𝔽_p)** for E_t with t = −w²(w − 1)² (torsion Z/8). -/
theorem g8 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b : Zp (2 * m + 1 + 1)) (hv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).neg (b)) = (ring (2 * m + 1 + 1)).zero) (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (  -- (+21 chars; full line in the Zenodo source)
    (hw : a ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (h21 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m +  -- (+595 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (T4LF.cnt4 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))))) (pL m) = 8 * k := by
  have hw1 : (ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero := fun e => hb (by
    calc b = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).neg (b)))) := by lib_ring
      _ = _ := by rw [e, hv]; lib_ring)
  have nP : (ring (2 * m + 1 + 1)).mul (a) (b) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ hw hb) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)  -- (+28 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have n3P : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => (Z8Comp.nz2 m hpr _ _ hb h21) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((r  -- (+296 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have ntP : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hb hw) hw) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mu  -- (+212 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have nt0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ hb hb) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((rin  -- (+202 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have n30 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ hb hw1) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2   -- (+289 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have nyP : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (b) (b))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)  -- (+271 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (b) (b))))) ((ring (2 * m + 1 +   -- (+732 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have nd0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ hw hb) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((rin  -- (+202 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have ndt : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _  -- (+17 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))))) ((ring (2 * m + 1 + 1)).ne  -- (+300 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have nd3 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))  -- (+79 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((r  -- (+384 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have ndb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).mul (a) (a))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hw hw) hb) h21) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+227 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have nd : Z8Spec.ND m a b := ⟨nP, n3P, ntP, nt0, n30, nyP⟩
  have ss := Z8All.sgsg_all m hpr a b hv nd nd0 ndt nd3 ndb
  have h8 : ∀ x, sg m a b (sg m a b (sg m a b (sg m a b (sg m a b (sg m a b (sg m a b (sg m a b (x)))))))) = x := fun x => by
    rw [ss x, ss (tauG m _ _ x), ss (tauG m _ _ (tauG m _ _ x)), ss (tauG m _ _ (tauG m _ _ (tauG m _ _ x)))]
    exact T4LF.tau4 m hpr _ nt0 x
  have hfix : ∀ x, sg m a b (sg m a b (sg m a b (sg m a b (x)))) = x → onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero x = 0 := fun x h => by
    rw [ss x, ss (tauG m _ _ x)] at h
    exact T4LF.onG_fix4 m hpr _ nt0 h2 hD x h
  have e : sumK LibInt.intR (onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero) (ptsL m) = 1 + sumK LibInt.intR (T4LF.cnt4 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).  -- (+144 chars; full line in the Zenodo source)
    show LibInt.intR.add 1 (sumK LibInt.intR (onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, Orbit8.orbit8 (ptsL m) (complete_pts m) (code m) (code_inj m) (sg m a b) h8
    (onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero) (Z8All.onG_sg m hpr a b hv) hfix⟩

end Z8Count

#print axioms Z8Count.g8

#print axioms: 1 theorem, no axioms.

78.350 Sixteen divides the point count with torsion \(\mathbb Z/2\times \mathbb Z/8\)

On \(y^2+xy-ty=x^3-tx^2\) with \(t=-w^2(w-1)^2\) and \(r^2=1+4w-4w^2\), translations by \(P\) and by a second \(2\)-torsion point generate a free \(\mathbb Z/8\times \mathbb Z/2\) action.

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2+xy-ty=x^3-tx^2\}\equiv 0\pmod {16}. \]

lang/Z82Count.lean

/-
  lang/Z82Count — T43, cycle 473: **16 | #E(𝔽_p) ∪ {O} for curves with torsion Z/2 × Z/8.**

  E_t: y² + xy − ty = x³ − tx², t = −v², v = w(w − 1), with Q₂ = (p, q) a second 2-torsion point (4p² + p − t = 0, 2q + p − t = 0) and
  c the other root (4c + 4p + 1 = 0). σ = +P (lang/Z8Map), ι = +Q₂ (lang/Z82Map): σ⁸ = id (Z8Count), ι² = id, σι = ισ (Z82All),
  onG invariant; the fifteen non-identity σⁱιʲ have no fixed point on E ∪ {O}: (σⁱι)² = σ²ⁱ reduces all but ι and σ⁴ι to σ⁴ = τ²
  (T4LF.onG_fix4), ι by Z82Fix.iofix_gen (Q₂ ↦ O), σ⁴ι by Z82Fix.s4fix_gen and the table at x ∈ {0, 2t, c, p} (lang/Z82Tab);
  lang/Orbit82 gives 16 | 1 + #E. Conditions: p odd, w ≠ 0, 1, 2w ≠ 1, 4w² − 4w − 1 ≠ 0 (then 16t + 1 = −(2w − 1)²(4w² − 4w − 1) ≠ 0).
    g16
    g16w   the same in the family's parameters w, r (r² = 1 + 4w − 4w², r ≠ 0): p = (−1 + (2w − 1)r)/8, c = (−1 − (2w − 1)r)/8, q = (t − p)/2
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Z82All
import lang.Z82Fix
import lang.Orbit82
import lang.Z8Count

namespace Z82Count

open FTAChain ZMod YMChain FiniteRecChain QChar G4Gen G7Tate1 G7LF ConicSum Pairs Z8Map Z82Map Z82Tab
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

set_option maxHeartbeats 0 in
/-- **16 | 1 + #E(𝔽_p)** for E_t with torsion Z/2 × Z/8. -/
theorem g16 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a b p q c : Zp (2 * m + 1 + 1))
    (h2 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hw1 : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1))  -- (+2648 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (T4LF.cnt4 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))))) (pL m) = 16 * k := by
  have hb : b ≠ (ring (2 * m + 1 + 1)).zero := fun e => Z8Comp.nz2 m hpr _ _ ha hw1 (by
    calc (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).neg (b))) b := by lib_ring
      _ = _ := by rw [hv, e]; lib_ring)
  have hq : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) q = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) := by
    apply Z4Z2.sub0
    calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (q)) (p)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2  -- (+105 chars; full line in the Zenodo source)
      _ = _ := by rw [hq2]; lib_ring
  obtain ⟨x1, hx1⟩ : ∃ z : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) z = (ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ri  -- (+266 chars; full line in the Zenodo source)
    ⟨(ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1  -- (+1997 chars; full line in the Zenodo source)
  obtain ⟨y1, hy1⟩ : ∃ z : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) ((ring (2 * m + 1 + 1)).add   -- (+388 chars; full line in the Zenodo source)
    ⟨(ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one  -- (+2363 chars; full line in the Zenodo source)
  obtain ⟨x3, hx3⟩ : ∃ z : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (  -- (+262 chars; full line in the Zenodo source)
    ⟨(ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).  -- (+1985 chars; full line in the Zenodo source)
  obtain ⟨y3, hy3⟩ : ∃ z : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (  -- (+908 chars; full line in the Zenodo source)
    ⟨(ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b))) ((ring (2 * m + 1 + 1)).mul b (b))) ((ring (2 * m + 1  -- (+3923 chars; full line in the Zenodo source)
  obtain ⟨x5, hx5⟩ : ∃ z : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (  -- (+262 chars; full line in the Zenodo source)
    ⟨(ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).  -- (+1985 chars; full line in the Zenodo source)
  obtain ⟨y5, hy5⟩ : ∃ z : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2  -- (+266 chars; full line in the Zenodo source)
    ⟨(ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p ((ring (2 * m + 1 + 1)).mul b (b)))) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 +   -- (+1997 chars; full line in the Zenodo source)
  obtain ⟨x7, hx7⟩ : ∃ z : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) z = (ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ri  -- (+266 chars; full line in the Zenodo source)
    ⟨(ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1  -- (+1997 chars; full line in the Zenodo source)
  obtain ⟨y7, hy7⟩ : ∃ z : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (b)) ((ring (2 * m + 1 + 1)).add ((r  -- (+1082 chars; full line in the Zenodo source)
    ⟨(ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (b)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))   -- (+4445 chars; full line in the Zenodo source)
  obtain ⟨y4, hy4⟩ : ∃ z : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (c)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) :=
    ⟨(ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (c)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b)))) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))), by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).ad  -- (+948 chars; full line in the Zenodo source)
  have hcP2 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).zero) := by
    unfold cB; apply Z4Z2.sub0
    lib_ring
  have hcP3 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1  -- (+357 chars; full line in the Zenodo source)
    unfold cB; apply Z4Z2.sub0
    calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b (b)))) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b (a))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 *   -- (+36 chars; full line in the Zenodo source)
      _ = _ := by rw [hv]; lib_ring
  have hcP4 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) ((ring (2 * m + 1 + 1)).zero) := by
    unfold cB; apply Z4Z2.sub0
    lib_ring
  have hcP5 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1  -- (+119 chars; full line in the Zenodo source)
    unfold cB; apply Z4Z2.sub0
    calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b (b)))) ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b (a))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 *   -- (+36 chars; full line in the Zenodo source)
      _ = _ := by rw [hv]; lib_ring
  have hcP6 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) := by
    unfold cB; apply Z4Z2.sub0
    lib_ring
  have hcP7 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul b (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) ((ring (2 * m +  -- (+179 chars; full line in the Zenodo source)
    unfold cB; apply Z4Z2.sub0
    calc _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b (a))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).neg (b))) := by lib_ring
      _ = _ := by rw [hv]; lib_ring
  have hcP1 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (b) (b))) := onP m a b hv
  have hcQ0 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero (p) (q) := Z82Map.onQ2 m hpr b p q h2 hq2 hp2
  have hcQ2 := Z82Pts.onQ2 m b p hp2
  have hcQ6 := Z82Pts.onQ6 m b p hp2
  have hDp : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero := fun e => hD (Eq.trans (by lib_ring) e)
  have hcQ1 := Z82Pts.onQ1 m hpr a b p x1 y1 hp2 hv hDp (by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) x1 = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +  -- (+663 chars; full line in the Zenodo source)
  have hcQ3 := Z82Pts.onQ3 m hpr a b p x3 y3 hp2 hv hDp (by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) x3 = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +  -- (+663 chars; full line in the Zenodo source)
  have hcQ5 := Z82Pts.onQ5 m hpr a b p x5 y5 hp2 hv hDp (by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) x5 = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +  -- (+663 chars; full line in the Zenodo source)
  have hcQ7 := Z82Pts.onQ7 m hpr a b p x7 y7 hp2 hv hDp (by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a)) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) x7 = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +  -- (+663 chars; full line in the Zenodo source)
  have hy4z : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) y4) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (c)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))))) = (ring (2 * m + 1 + 1)).zero :  -- (+23 chars; full line in the Zenodo source)
  have hy4r : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (y4)) (c)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((rin  -- (+882 chars; full line in the Zenodo source)
  have hp2c : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (c) (c))) (c)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (  -- (+218 chars; full line in the Zenodo source)
    apply Z4Z2.sub0
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m +   -- (+937 chars; full line in the Zenodo source)
      _ = _ := by rw [hp2, hr]; lib_ring
  have hcQ4 : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero (c) (y4) := Z82Map.onQ2 m hpr b c y4 h2 hy4r hp2c
  have hne_sg_Q0 : (p) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_Q0 m hpr a b p ha hw1 hD hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add (p) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a))) = (ring (2 * m + 1 + 1)).add (p) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b))) by lib_ring)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b)))) e) (by lib_ring)))
  have hne_io_P1 : ((ring (2 * m + 1 + 1)).mul (a) (b)) ≠ p := fun e => Z82Nd.nd_hne_io_P1 m hpr a b p ha hw1 hD hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).mul b (a)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg (p)) by lib_ring)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (p))) e) (by lib_ring)))
  have hne_sg_Q1 : (x1) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_Q1 m hpr a b p h2 ha hw1 hD hv hp2 (by
    calc (ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1  -- (+861 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) x1) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +   -- (+184 chars; full line in the Zenodo source)
      _ = _ := by rw [e]; lib_ring)
  have hne_io_Q1 : (x1) ≠ p := fun e => Z82Nd.nd_hne_io_Q1 m hpr a b p ha hw1 hD hr4 hv hp2 (by
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (p) (b)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 *   -- (+923 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) x1) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +   -- (+151 chars; full line in the Zenodo source)
      _ = _ := by rw [e]; lib_ring)
  have hne_sg_P2 : ((ring (2 * m + 1 + 1)).zero) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_P2 m hpr a b p h2 ha hw1 hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b))) by lib_ring)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b)))) e) (by lib_ring)))
  have hne_io_P2 : ((ring (2 * m + 1 + 1)).zero) ≠ p := fun e => Z82Nd.nd_hne_io_P2 m hpr a b p ha hw1 hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).neg (p) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg (p)) by lib_ring)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (p))) e) (by lib_ring)))
  have hne_sg_Q2 : ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul b (b)))) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_Q2 m hpr a b p h2 ha hw1 hD hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring  -- (+461 chars; full line in the Zenodo source)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b)))) e) (by lib_ring)))
  have hne_io_Q2 : ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul b (b)))) ≠ p := fun e => Z82Nd.nd_hne_io_Q2 m hpr a b p ha hw1 hD hr4 hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((rin  -- (+365 chars; full line in the Zenodo source)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (p))) e) (by lib_ring)))
  have hne_sg_P3 : ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a)))) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_P3 m hpr a b p h2 ha hw1 hD hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one  -- (+243 chars; full line in the Zenodo source)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b)))) e) (by lib_ring)))
  have hne_io_P3 : ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a)))) ≠ p := fun e => Z82Nd.nd_hne_io_P3 m hpr a b p ha hw1 hD hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a)))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1   -- (+124 chars; full line in the Zenodo source)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (p))) e) (by lib_ring)))
  have hne_sg_Q3 : (x3) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_Q3 m hpr a b p h2 ha hw1 hD hv hp2 (by
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +   -- (+853 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) x3) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +   -- (+184 chars; full line in the Zenodo source)
      _ = _ := by rw [e]; lib_ring)
  have hne_io_Q3 : (x3) ≠ p := fun e => Z82Nd.nd_hne_io_Q3 m hpr a b p ha hw1 hD hr4 hv hp2 (by
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (p) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+915 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) x3) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +   -- (+151 chars; full line in the Zenodo source)
      _ = _ := by rw [e]; lib_ring)
  have hne_sg_P4 : ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_P4 m hpr a b p h2 ha hw1 hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 +   -- (+122 chars; full line in the Zenodo source)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b)))) e) (by lib_ring)))
  have hne_io_P4 : ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) ≠ p := fun e => Z82Nd.nd_hne_io_P4 m hpr a b p h2 ha hw1 hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) ((ring (2 * m + 1 +   -- (+25 chars; full line in the Zenodo source)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (p))) e) (by lib_ring)))
  have hne_sg_Q4 : (c) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_Q4 m hpr a b p h2 ha hw1 hD hv hp2 (by
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) (p))) ((ring (2 * m + 1  -- (+1240 chars; full line in the Zenodo source)
  have hne_io_Q4 : (c) ≠ p := fun e => Z82Nd.nd_hne_io_Q4 m hpr a b p h2 hD hr4 hv hp2 (by
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).  -- (+1002 chars; full line in the Zenodo source)
  have hne_sg_P5 : ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a)))) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_P5 m hpr a b p h2 ha hw1 hD hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one  -- (+243 chars; full line in the Zenodo source)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b)))) e) (by lib_ring)))
  have hne_io_P5 : ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a)))) ≠ p := fun e => Z82Nd.nd_hne_io_P5 m hpr a b p ha hw1 hD hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a)))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1   -- (+124 chars; full line in the Zenodo source)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (p))) e) (by lib_ring)))
  have hne_sg_Q5 : (x5) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_Q5 m hpr a b p h2 ha hw1 hD hv hp2 (by
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +   -- (+853 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) x5) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +   -- (+184 chars; full line in the Zenodo source)
      _ = _ := by rw [e]; lib_ring)
  have hne_io_Q5 : (x5) ≠ p := fun e => Z82Nd.nd_hne_io_Q5 m hpr a b p ha hw1 hD hr4 hv hp2 (by
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (p) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m +  -- (+915 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) x5) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +   -- (+151 chars; full line in the Zenodo source)
      _ = _ := by rw [e]; lib_ring)
  have hne_sg_P6 : ((ring (2 * m + 1 + 1)).zero) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_P6 m hpr a b p h2 ha hw1 hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b))) by lib_ring)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b)))) e) (by lib_ring)))
  have hne_io_P6 : ((ring (2 * m + 1 + 1)).zero) ≠ p := fun e => Z82Nd.nd_hne_io_P6 m hpr a b p ha hw1 hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).neg (p) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg (p)) by lib_ring)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (p))) e) (by lib_ring)))
  have hne_sg_Q6 : ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul b (b)))) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_Q6 m hpr a b p h2 ha hw1 hD hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring  -- (+461 chars; full line in the Zenodo source)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b)))) e) (by lib_ring)))
  have hne_io_Q6 : ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul b (b)))) ≠ p := fun e => Z82Nd.nd_hne_io_Q6 m hpr a b p ha hw1 hD hr4 hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((rin  -- (+365 chars; full line in the Zenodo source)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (p))) e) (by lib_ring)))
  have hne_io_P7 : ((ring (2 * m + 1 + 1)).mul b (a)) ≠ p := fun e => Z82Nd.nd_hne_io_P7 m hpr a b p ha hw1 hD hv hp2 (Eq.trans (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).mul b (a)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b (a)) ((ring (2 * m + 1 + 1)).neg (p)) by lib_ring)
    (Eq.trans (congrArg (fun z => (ring (2 * m + 1 + 1)).add z ((ring (2 * m + 1 + 1)).neg (p))) e) (by lib_ring)))
  have hne_sg_Q7 : (x7) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => Z82Nd.nd_hne_sg_Q7 m hpr a b p h2 ha hw1 hD hv hp2 (by
    calc (ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1  -- (+861 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) x7) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +   -- (+184 chars; full line in the Zenodo source)
      _ = _ := by rw [e]; lib_ring)
  have hne_io_Q7 : (x7) ≠ p := fun e => Z82Nd.nd_hne_io_Q7 m hpr a b p ha hw1 hD hr4 hv hp2 (by
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (p) (b)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 *   -- (+923 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) x7) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +   -- (+151 chars; full line in the Zenodo source)
      _ = _ := by rw [e]; lib_ring)
  have hyP7 : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b (a)))))) ≠ (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (b) (b)) := fun e => Z8Comp.nz2 m hpr _ _ (Z  -- (+59 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul a a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 *   -- (+754 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have h21 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ≠ (ring (2 * m + 1 + 1)).zero := fun e => hD (Eq.trans (by lib_ring) e)
  have hD16 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one  -- (+428 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (a))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +  -- (+3165 chars; full line in the Zenodo source)
    all_goals lib_ring)
  have hKz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (p))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => Z82Nd.nd_p m hpr a b p ha hw1 hv hp2 (by
    rw [show p = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (p)))) by lib_ring, e]; all_goals lib_ring)
  have hKt : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (p))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => Z82Nd.nd_t_p m h  -- (+29 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) ((ring (2 * m + 1 + 1))  -- (+104 chars; full line in the Zenodo source)
  have hKP : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (p))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => Z82Nd.nd_xP_p m hpr a b p ha hw1 hD hv hp2 (by
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).mul b (a)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (p))) by lib_ring, e]; all_goals lib_ring)
  have hK3 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring  -- (+117 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a)))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (  -- (+224 chars; full line in the Zenodo source)
  have halp : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).mul b (a)) ≠ (ring (2 * m + 1 + 1)).zero := fun e => Z82Nd.nd_xP_p m hpr a b p ha hw1 hD hv hp2 (by
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).mul b (a)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (p)) ((ring (2 * m + 1 + 1)).mul b (a)) by lib_ring, e]; all_goals lib_ring)
  have halt : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul b (b))) ((ring (2 * m + 1 + 1)).mul b (a)) ≠ (ring (2 * m + 1 + 1)).zero := fun e => Z82Nd.nd_xP_2t m hpr a b p h2 ha hw1 hD hv hp2 (by
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul b (b))) ((ring (2 * m + 1 + 1)).mul b (a)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul b (b))) ((  -- (+69 chars; full line in the Zenodo source)
  have nd0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => Z8Comp.nz2 m hpr _ _ ha hb (Eq.trans (by lib_ring) e)
  have hC := Z82Inv.hC_ne m hpr a b p q h2 ha hw1 hD hr4 hv hq2 hp2
  have h144 := Z82Fix.tc4_ne m hpr b p h2 hb hp2
  have h18 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one  -- (+210 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).  -- (+628 chars; full line in the Zenodo source)
  have h2t0 : ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul b (b)))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ h2 hb) hb (by rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 +   -- (+272 chars; full line in the Zenodo source)
  have hpt : (ring (2 * m + 1 + 1)).add p ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => Z82Nd.nd_t_p m hpr a b p h2 ha hw1 hv hp2 (by rw [show (ring (2 * m + 1 + 1)  -- (+429 chars; full line in the Zenodo source)
  have hp0 : p ≠ (ring (2 * m + 1 + 1)).zero := Z82Nd.nd_p m hpr a b p ha hw1 hv hp2
  have hxP4 : ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) ≠ p := fun e => hpt (by rw [show (ring (2 * m + 1 + 1)).add p ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))))) = (ring (2 * m + 1 +  -- (+521 chars; full line in the Zenodo source)
  have h18c : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).on  -- (+167 chars; full line in the Zenodo source)
    rw [show (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one  -- (+1371 chars; full line in the Zenodo source)
    all_goals lib_ring)
  have h144c := Z82Fix.tc4_ne m hpr b c h2 hb hp2c
  have nP : (ring (2 * m + 1 + 1)).mul (a) (b) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ ha hb) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)  -- (+28 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have n3P : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => (Z8Comp.nz2 m hpr _ _ hb h21) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((r  -- (+296 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have ntP : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))) ≠ (ring (2 * m + 1 + 1)).mul (a) (b) := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hb ha) ha) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mu  -- (+212 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have nt0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ hb hb) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((rin  -- (+202 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have n30 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ hb hw1) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2   -- (+289 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have nyP : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (b) (b))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (b)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)  -- (+271 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (b) (b))))) ((ring (2 * m + 1 +   -- (+732 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have nd0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ ha hb) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((rin  -- (+202 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have ndt : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _  -- (+17 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))))) ((ring (2 * m + 1 + 1)).ne  -- (+300 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have nd3 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))))  -- (+79 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).add (a) ((r  -- (+384 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have ndb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).mul (a) (a))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ ha ha) hb) h21) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (b) (b))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) (b)) ((ring (2 * m + 1 + 1)).mul (a) (a)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+227 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hv]; lib_ring)
  have nd : Z8Spec.ND m a b := ⟨nP, n3P, ntP, nt0, n30, nyP⟩
  have ss := Z8All.sgsg_all m hpr a b hv nd nd0 ndt nd3 ndb
  have h8 : ∀ x, sg m a b (sg m a b (sg m a b (sg m a b (sg m a b (sg m a b (sg m a b (sg m a b (x)))))))) = x := fun x => by
    rw [ss x, ss (tauG m _ _ x), ss (tauG m _ _ (tauG m _ _ x)), ss (tauG m _ _ (tauG m _ _ (tauG m _ _ x)))]
    exact T4LF.tau4 m hpr _ nt0 x
  have hfix4 : ∀ x, sg m a b (sg m a b (sg m a b (sg m a b (x)))) = x → onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero x = 0 := fun x h => by
    rw [ss x, ss (tauG m _ _ x)] at h
    exact T4LF.onG_fix4 m hpr _ nt0 h2 hD16 x h
  have comm : ∀ y, io m b p q (sg m a b y) = sg m a b (io m b p q y) := fun y => Z82All.comm_all m hpr a b p q c h2 ha hw1 hD hr4 hv hq2 hp2 hr y
  have inv : ∀ y, io m b p q (io m b p q y) = y := fun y => Z82All.inv_all m hpr a b p q h2 ha hw1 hD hr4 hv hq2 hp2 y
  have hs2 : ∀ y, Orbit42.tp (io m b p q) 2 y = y := fun y => inv y
  have h8' : ∀ y, Orbit42.tp (sg m a b) 8 y = y := fun y => h8 y
  have iofix : ∀ Q, io m b p q Q = Q → onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero Q = 0 := by
    intro Q hfx
    cases Q with
    | none => rw [Z82Spec.ioO] at hfx; cases hfx
    | some P0 =>
      obtain ⟨x, y⟩ := P0
      by_cases hcu : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((r  -- (+550 chars; full line in the Zenodo source)
      · have hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero x y := hcu
        by_cases gp : x = p
        · have h := Z82Fy.fy_xp m hpr b p x y hp2 hc (gp)
          have hyy0 : y = q := by
            apply Z4Z2.sub0
            refine (mul_eq_zero m hpr _ _ ?_).resolve_left h2
            rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg q)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 *  -- (+976 chars; full line in the Zenodo source)
            all_goals lib_ring
          have hxx : p = x := gp.symm
          have hyy : q = y := hyy0.symm
          subst hxx; subst hyy
          rw [Z82Spec.ioQ0 m b p q hcQ0] at hfx; cases hfx
        · exact absurd hfx (fun h => Z82Fix.iofix_gen m hpr b p q x y h2 hq2 hp2 hc gp hC hb h18 h144 h)
      · exact onG_off m _ _ x y hcu
  have s4fix : ∀ Q, io m b p q (sg m a b (sg m a b (sg m a b (sg m a b Q)))) = Q → onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero Q = 0 := by
    intro Q hfx
    cases Q with
    | none => rw [Z82Spec.sgO m a b, Z82Spec.sgP1 m a b hcP1, sg_P2 m hpr a b hcP2 hne_sg_P2, sg_P3 m hpr a b hv hcP3 hne_sg_P3, io_P4 m hpr b p c q y4 ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) (iv m ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)))) h2 hr hq hy4   -- (+940 chars; full line in the Zenodo source)
    | some P0 =>
      obtain ⟨x, y⟩ := P0
      by_cases hcu : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((r  -- (+550 chars; full line in the Zenodo source)
      · have hc : cB m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero x y := hcu
        by_cases gp : x = p
        · have h := Z82Fy.fy_xp m hpr b p x y hp2 hc (gp)
          have hyy0 : y = q := by
            apply Z4Z2.sub0
            refine (mul_eq_zero m hpr _ _ ?_).resolve_left h2
            rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg q)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 *  -- (+976 chars; full line in the Zenodo source)
            all_goals lib_ring
          have hxx : p = x := gp.symm
          have hyy : q = y := hyy0.symm
          subst hxx; subst hyy
          rw [sg_Q0 m hpr a b p q x1 y1 ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) (p))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (a)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m   -- (+14610 chars; full line in the Zenodo source)
          have hxe := (Prod.mk.inj (Option.some.inj hfx)).1
          exact absurd hxe hxP4
        by_cases g0 : x = (ring (2 * m + 1 + 1)).zero
        · have g := g0
          have fy := Z82Fy.fy_x0 m hpr b x y hc (g)
          rcases fy with h | h
          · have hxx : x = (ring (2 * m + 1 + 1)).zero := g.trans (by lib_ring)
            have hyy : y = (ring (2 * m + 1 + 1)).zero := (Z4Z2.sub0 m _ _ h).trans (by lib_ring)
            subst hxx; subst hyy
            rw [sg_P2 m hpr a b hcP2 hne_sg_P2, sg_P3 m hpr a b hv hcP3 hne_sg_P3, sg_P4 m hpr a b hv hcP4 hne_sg_P4, sg_P5 m hpr a b hcP5 hne_sg_P5, io_P6 m hpr b p q h2 hp2 hq hcP6 hne_io_P6] at hfx
            have hxe := (Prod.mk.inj (Option.some.inj hfx)).1
            exact absurd hxe h2t0
          · have hxx : x = (ring (2 * m + 1 + 1)).zero := g.trans (by lib_ring)
            have hyy : y = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b)) := (Z4Z2.sub0 m _ _ h).trans (by lib_ring)
            subst hxx; subst hyy
            rw [sg_P6 m hpr a b hv hcP6 hne_sg_P6, Z82Spec.sgP7 m a b ((ring (2 * m + 1 + 1)).mul b (a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (b))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul b (a)))))) hcP7 (by lib_r  -- (+105 chars; full line in the Zenodo source)
            have hxe := (Prod.mk.inj (Option.some.inj hfx)).1
            exact absurd hxe h2t0
        by_cases g2t : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b))))
        · have g := g2t
          have fy := Z82Fy.fy_x2t m hpr b p x y hp2 hc (g.trans (by lib_ring))
          rcases fy with h | h
          · have hxx : x = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul b (b))) := g.trans (by lib_ring)
            have hyy : y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul b (b)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p ((ring (2 * m + 1 + 1)).mul b (b)))) := (Z4Z2.sub0 m _ _ h  -- (+21 chars; full line in the Zenodo source)
            subst hxx; subst hyy
            rw [sg_Q2 m hpr a b p x3 y3 ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) (p))) ((ring (2 * m + 1   -- (+16110 chars; full line in the Zenodo source)
            have hxe := (Prod.mk.inj (Option.some.inj hfx)).1
            exact absurd hxe (fun e => h2t0 e.symm)
          · have hxx : x = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul b (b))) := g.trans (by lib_ring)
            have hyy : y = (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul p ((ring (2 * m + 1 + 1)).mul b (b)))) := (Z4Z2.sub0 m _ _ h).trans (by lib_ring)
            subst hxx; subst hyy
            rw [sg_Q6 m hpr a b p x7 y7 ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) (p))) ((ring (2 * m + 1 + 1)).add (b) ((ring (2 * m +  -- (+10814 chars; full line in the Zenodo source)
            have hxe := (Prod.mk.inj (Option.some.inj hfx)).1
            exact absurd hxe (fun e => h2t0 e.symm)
        by_cases gc : x = c
        · have h := Z82Fy.fy_xc m hpr b p c x y hp2 hr hc (gc)
          have hyy : y = y4 := by
            apply Z4Z2.sub0
            refine (mul_eq_zero m hpr _ _ ?_).resolve_left h2
            rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg y4)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2   -- (+1001 chars; full line in the Zenodo source)
            all_goals lib_ring
          have hxc' : c = x := gc.symm
          have hyy' : y4 = y := hyy.symm
          subst hxc'; subst hyy'
          rw [sg_Q4 m hpr a b p c y4 x5 y5 ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1))  -- (+13443 chars; full line in the Zenodo source)
          cases hfx
        rw [comm (sg m a b (sg m a b (sg m a b (some (x, y))))), comm (sg m a b (sg m a b (some (x, y)))), comm (sg m a b (some (x, y))), comm (some (x, y))] at hfx
        rw [ss (sg m a b (sg m a b (io m b p q (some (x, y))))), ss (io m b p q (some (x, y)))] at hfx
        exact absurd hfx (fun h => Z82Fix.s4fix_gen m hpr b p q c y4 x y h2 hq2 hp2 hr hy4r hp2c hc gp g0 g2t gc hKz hKt hp0 hpt hb h18c h144c h)
      · exact onG_off m _ _ x y hcu
  have hfix : ∀ x, (Orbit42.act (io m b p q) (sg m a b) 1 0 x = x ∨ Orbit42.act (io m b p q) (sg m a b) 2 0 x = x ∨ Orbit42.act (io m b p q) (sg m a b) 3 0 x = x ∨ Orbit42.act (io m b p q) (sg m a b) 4 0 x = x ∨ Orbit42.act (io m b p q) (sg m a b) 5 0 x = x ∨ Orbit42.act (io m b p q) (sg m a b) 6 0 x = x ∨ Orbit42.act (io m b p q) (sg m a b) 7 0 x = x ∨ Orbit42.act (io m b p q) (sg m a b) 0 1 x =   -- (+608 chars; full line in the Zenodo source)
    rcases h with e | e | e | e | e | e | e | e | e | e | e | e | e | e | e
    · have e2 : Orbit42.act (io m b p q) (sg m a b) 2 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 1 0) e
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      have e3 : Orbit42.act (io m b p q) (sg m a b) 3 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 1 0) e2
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      have e4 : Orbit42.act (io m b p q) (sg m a b) 4 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 1 0) e3
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      exact hfix4 x e4
    · have e2 : Orbit42.act (io m b p q) (sg m a b) 4 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 2 0) e
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      exact hfix4 x e2
    · have e2 : Orbit42.act (io m b p q) (sg m a b) 6 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 3 0) e
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      have e3 : Orbit42.act (io m b p q) (sg m a b) 9 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 3 0) e2
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      have e4 : Orbit42.act (io m b p q) (sg m a b) 12 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 3 0) e3
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      rw [show Orbit42.act (io m b p q) (sg m a b) 12 0 x = Orbit42.act (io m b p q) (sg m a b) 4 0 x from Orbit82.act_red8 (ht := h8') 4 0 x] at e4
      exact hfix4 x e4
    · exact hfix4 x e
    · have e2 : Orbit42.act (io m b p q) (sg m a b) 10 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 5 0) e
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      have e3 : Orbit42.act (io m b p q) (sg m a b) 15 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 5 0) e2
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      have e4 : Orbit42.act (io m b p q) (sg m a b) 20 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 5 0) e3
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      rw [show Orbit42.act (io m b p q) (sg m a b) 20 0 x = Orbit42.act (io m b p q) (sg m a b) 12 0 x from Orbit82.act_red8 (ht := h8') 12 0 x] at e4
      rw [show Orbit42.act (io m b p q) (sg m a b) 12 0 x = Orbit42.act (io m b p q) (sg m a b) 4 0 x from Orbit82.act_red8 (ht := h8') 4 0 x] at e4
      exact hfix4 x e4
    · have e2 : Orbit42.act (io m b p q) (sg m a b) 12 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 6 0) e
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      rw [show Orbit42.act (io m b p q) (sg m a b) 12 0 x = Orbit42.act (io m b p q) (sg m a b) 4 0 x from Orbit82.act_red8 (ht := h8') 4 0 x] at e2
      exact hfix4 x e2
    · have e2 : Orbit42.act (io m b p q) (sg m a b) 14 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 7 0) e
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      have e3 : Orbit42.act (io m b p q) (sg m a b) 21 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 7 0) e2
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      have e4 : Orbit42.act (io m b p q) (sg m a b) 28 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 7 0) e3
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      rw [show Orbit42.act (io m b p q) (sg m a b) 28 0 x = Orbit42.act (io m b p q) (sg m a b) 20 0 x from Orbit82.act_red8 (ht := h8') 20 0 x] at e4
      rw [show Orbit42.act (io m b p q) (sg m a b) 20 0 x = Orbit42.act (io m b p q) (sg m a b) 12 0 x from Orbit82.act_red8 (ht := h8') 12 0 x] at e4
      rw [show Orbit42.act (io m b p q) (sg m a b) 12 0 x = Orbit42.act (io m b p q) (sg m a b) 4 0 x from Orbit82.act_red8 (ht := h8') 4 0 x] at e4
      exact hfix4 x e4
    · exact iofix x e
    · have esq : Orbit42.act (io m b p q) (sg m a b) 2 2 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 1 1) e
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      rw [show Orbit42.act (io m b p q) (sg m a b) 2 2 x = Orbit42.act (io m b p q) (sg m a b) 2 0 x from Orbit42.act_red2 (hs := hs2) 2 0 x] at esq
      have e2 : Orbit42.act (io m b p q) (sg m a b) 4 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 2 0) esq
        rw [Orbit42.act_comp comm, esq] at h1; exact h1
      exact hfix4 x e2
    · have esq : Orbit42.act (io m b p q) (sg m a b) 4 2 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 2 1) e
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      rw [show Orbit42.act (io m b p q) (sg m a b) 4 2 x = Orbit42.act (io m b p q) (sg m a b) 4 0 x from Orbit42.act_red2 (hs := hs2) 4 0 x] at esq
      exact hfix4 x esq
    · have esq : Orbit42.act (io m b p q) (sg m a b) 6 2 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 3 1) e
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      rw [show Orbit42.act (io m b p q) (sg m a b) 6 2 x = Orbit42.act (io m b p q) (sg m a b) 6 0 x from Orbit42.act_red2 (hs := hs2) 6 0 x] at esq
      have e2 : Orbit42.act (io m b p q) (sg m a b) 12 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 6 0) esq
        rw [Orbit42.act_comp comm, esq] at h1; exact h1
      rw [show Orbit42.act (io m b p q) (sg m a b) 12 0 x = Orbit42.act (io m b p q) (sg m a b) 4 0 x from Orbit82.act_red8 (ht := h8') 4 0 x] at e2
      exact hfix4 x e2
    · exact s4fix x e
    · have esq : Orbit42.act (io m b p q) (sg m a b) 10 2 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 5 1) e
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      rw [show Orbit42.act (io m b p q) (sg m a b) 10 2 x = Orbit42.act (io m b p q) (sg m a b) 10 0 x from Orbit42.act_red2 (hs := hs2) 10 0 x] at esq
      rw [show Orbit42.act (io m b p q) (sg m a b) 10 0 x = Orbit42.act (io m b p q) (sg m a b) 2 0 x from Orbit82.act_red8 (ht := h8') 2 0 x] at esq
      have e2 : Orbit42.act (io m b p q) (sg m a b) 4 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 2 0) esq
        rw [Orbit42.act_comp comm, esq] at h1; exact h1
      exact hfix4 x e2
    · have esq : Orbit42.act (io m b p q) (sg m a b) 12 2 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 6 1) e
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      rw [show Orbit42.act (io m b p q) (sg m a b) 12 2 x = Orbit42.act (io m b p q) (sg m a b) 12 0 x from Orbit42.act_red2 (hs := hs2) 12 0 x] at esq
      rw [show Orbit42.act (io m b p q) (sg m a b) 12 0 x = Orbit42.act (io m b p q) (sg m a b) 4 0 x from Orbit82.act_red8 (ht := h8') 4 0 x] at esq
      exact hfix4 x esq
    · have esq : Orbit42.act (io m b p q) (sg m a b) 14 2 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 7 1) e
        rw [Orbit42.act_comp comm, e] at h1; exact h1
      rw [show Orbit42.act (io m b p q) (sg m a b) 14 2 x = Orbit42.act (io m b p q) (sg m a b) 14 0 x from Orbit42.act_red2 (hs := hs2) 14 0 x] at esq
      rw [show Orbit42.act (io m b p q) (sg m a b) 14 0 x = Orbit42.act (io m b p q) (sg m a b) 6 0 x from Orbit82.act_red8 (ht := h8') 6 0 x] at esq
      have e2 : Orbit42.act (io m b p q) (sg m a b) 12 0 x = x := by
        have h1 := congrArg (Orbit42.act (io m b p q) (sg m a b) 6 0) esq
        rw [Orbit42.act_comp comm, esq] at h1; exact h1
      rw [show Orbit42.act (io m b p q) (sg m a b) 12 0 x = Orbit42.act (io m b p q) (sg m a b) 4 0 x from Orbit82.act_red8 (ht := h8') 4 0 x] at e2
      exact hfix4 x e2
  have e : sumK LibInt.intR (onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero) (ptsL m) = 1 + sumK LibInt.intR (T4LF.cnt4 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).  -- (+144 chars; full line in the Zenodo source)
    show LibInt.intR.add 1 (sumK LibInt.intR (onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, Orbit82.orbit82 (ptsL m) (complete_pts m) (code m) (code_inj m) (io m b p q) (sg m a b) hs2 h8' comm
    (onG m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (b)))) (ring (2 * m + 1 + 1)).zero) (Z82Map.onG_io m hpr b p q h2 hq2 hp2) (Z8All.onG_sg m hpr a b hv) hfix⟩


/-- **16 | 1 + #E(𝔽_p)** for the Z/2 × Z/8 family in its parameters: w with r² = 1 + 4w − 4w² (Q₂ = ((−1 + (2w − 1)r)/8, …)). -/
theorem g16w (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a r : Zp (2 * m + 1 + 1))
    (h2 : ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (ha : a ≠ (ring (2 * m + 1 + 1)).zero) (hw1 : (ring (2 * m + 1 + 1)).add a ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ≠ (ring (2 * m + 1 + 1)).zero) (hD : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).o  -- (+161 chars; full line in the Zenodo source)
    (hr2 : (ring (2 * m + 1 + 1)).mul r r = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 +  -- (+241 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (T4LF.cnt4 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a a) ((ring (2 * m + 1 + 1)).neg a))) (((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul  -- (+64 chars; full line in the Zenodo source)
  have h8 : ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one  -- (+154 chars; full line in the Zenodo source)
  have h64 : ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).a  -- (+539 chars; full line in the Zenodo source)
  have hrz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul r r) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1))  -- (+353 chars; full line in the Zenodo source)
  obtain ⟨p, hp8⟩ : ∃ z : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)  -- (+413 chars; full line in the Zenodo source)
    ⟨(ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) a) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) r) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 +  -- (+2867 chars; full line in the Zenodo source)
  obtain ⟨c, hc8⟩ : ∃ z : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)  -- (+442 chars; full line in the Zenodo source)
    ⟨(ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) a) ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).one)) r))) (iv m ((ring (2 * m + 1   -- (+2954 chars; full line in the Zenodo source)
  obtain ⟨q, hq⟩ : ∃ z : Zp (2 * m + 1 + 1), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) z = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m   -- (+208 chars; full line in the Zenodo source)
    ⟨(ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a a) ((ring (2 * m + 1 + 1)).neg a)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 +  -- (+1589 chars; full line in the Zenodo source)
  have hp8z : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).on  -- (+489 chars; full line in the Zenodo source)
  have hc8z : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).on  -- (+518 chars; full line in the Zenodo source)
  have hqz : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) q) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1   -- (+285 chars; full line in the Zenodo source)
  have hp2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (p) (p))) (p)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((  -- (+399 chars; full line in the Zenodo source)
    refine (mul_eq_zero m hpr _ _ ?_).resolve_left h64
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one  -- (+6412 chars; full line in the Zenodo source)
    all_goals lib_ring
  have hp2c : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul (c) (c))) (c)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (  -- (+400 chars; full line in the Zenodo source)
    refine (mul_eq_zero m hpr _ _ ?_).resolve_left h64
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one  -- (+6499 chars; full line in the Zenodo source)
    all_goals lib_ring
  have hrr : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one))) (c)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 *   -- (+205 chars; full line in the Zenodo source)
    refine (mul_eq_zero m hpr _ _ ?_).resolve_left h2
    rw [show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1  -- (+2067 chars; full line in the Zenodo source)
    all_goals lib_ring
  have hq2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) (q)) (p)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).neg ((ring   -- (+1500 chars; full line in the Zenodo source)
  have hr4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) (a))) ((ring (2 * m + 1  -- (+1767 chars; full line in the Zenodo source)
  exact g16 m hpr a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul a a) ((ring (2 * m + 1 + 1)).neg a)) p q c h2 ha hw1 (fun e => hD (Eq.trans (by lib_ring) e)) hr4 (by lib_ring) hq2 hp2 hrr

end Z82Count

#print axioms Z82Count.g16
#print axioms Z82Count.g16w

#print axioms: 2 theorems, no axioms.

78.351 Nine divides the point count with torsion \(\mathbb Z/9\)

On \(y^2+axy+by=x^3\) with \(a=f^3-3fg^2-g^3\), \(b=f^3g^3\), \(f+g=1\), translation by \(P=(f^2g,f^2g^2)\) cubes to translation by \((0,0)\).

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2+axy+by=x^3\}\equiv 0\pmod 9. \]

lang/Z9Count.lean

/-
  lang/Z9Count — T43, cycle 452: **9 | #E(𝔽_p) ∪ {O} for curves with torsion Z/9.**

  E: y² + axy + by = x³ with a = f³ − 3fg² − g³, b = f³g³, f + g = 1 (Kubert's order-9 curve re-centred at 3P): P = (f²g, f²g²) has
  3P = T = (0, 0). σ = +P satisfies σ³ = τ (lang/Z9All: σσ = μ, σμ = τ), hence σ⁹ = τ³ = id (lang/G5Tate3.tau3), σ³x = x ⟹ τx = x ⟹
  onT x = 0 (G5Tate3.onT_fix), onT∘σ = onT, and lang/Orbit9 gives 9 | 1 + #E.
  Nondegeneracy: f ≠ 0, g ≠ 0, h = f² + fg + g² ≠ 0, f³ − 6f² + 3f + 1 ≠ 0; every condition of Z9Map.ND9, Z9Spec.SP9 is ± a product of
  f, g, h modulo f + g = 1, and G5Tate3.d27 = a³ − 27b = −h³(f³ − 6f² + 3f + 1).
    g9
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Z9All
import lang.Orbit9

namespace Z9Count

open FTAChain ZMod YMChain FiniteRecChain QChar G4Gen ConicSum Pairs

variable (m : Nat)

/-- **9 | 1 + #E(𝔽_p)** for y² + axy + by = x³, a = f³ − 3fg² − g³, b = f³g³, f + g = 1 (torsion Z/9). -/
theorem g9 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (w v : Zp (2 * m + 1 + 1)) (hr : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (w) (v)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero)
    (hf : w ≠ (ring (2 * m + 1 + 1)).zero) (hg : v ≠ (ring (2 * m + 1 + 1)).zero) (hh : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (w) (w)) ((ring (2 * m + 1 + 1)).mul (w) (v))) ((ring (2 * m + 1 + 1)).mul (v) (v)) ≠ (ring (2 * m + 1 + 1)).zero) (hq : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).m  -- (+626 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (G5Tate3.cnt3 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1  -- (+342 chars; full line in the Zenodo source)
  have s0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hf hf) hg) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (w) (v)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1))  -- (+24 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have s2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v)))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hf hf) hg) hh) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 *   -- (+505 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have s4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v)))))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp  -- (+29 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v))))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m  -- (+250 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have sP : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (w) (w)) (v))))) ((ring (2 * m + 1 + 1)).neg ((ring (2   -- (+352 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).ad  -- (+923 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have m0 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero)) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hf hf) hf) hg) hg) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)  -- (+90 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have mP : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hf hf) hg) hh) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 *  -- (+534 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have m4 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v)))))) ≠ (ri  -- (+121 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)  -- (+347 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have m2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) (w)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1  -- (+525 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) (w)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v)  -- (+1014 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have dP0 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hf hf) hg) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (w) (v)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1))  -- (+24 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have d20 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hf hf) hf) hg) hg) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)  -- (+90 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have d40 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v)))) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hf hg) hg) hg) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v))))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).z  -- (+145 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have d2P : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v)) ≠ (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v) := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hf hf) hg) hh) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 *   -- (+505 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have d4P : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v)))) ≠ (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v) := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hf hg) hh) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (v)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v))))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m  -- (+250 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have d42 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v)))) ≠ (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v)) := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Com  -- (+34 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)  -- (+347 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have dy8 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (  -- (+885 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) ((ring (2 * m + 1 + 1)).mul (v) (v))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w)  -- (+1839 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have dy7 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) (w)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v)) (v))))) ((ring (2  -- (+1293 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) (w)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v)) (v))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m  -- (+2086 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have db : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v)) ≠ (ring (2 * m + 1 + 1)).zero := fun e => (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ (Z8Comp.nz2 m hpr _ _ hf hf) hf) hg) hg) hg) (by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (v) (v)) (v))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1  -- (+123 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have hd : G5Tate3.d27 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m +  -- (+419 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (G5Tate3.d27 m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 +   -- (+12821 chars; full line in the Zenodo source)
      _ = _ := by rw [e, hr]; lib_ring)
  have nd : Z9Map.ND9 m w v := ⟨s0, s2, s4, sP, m0, mP, m4, m2⟩
  have sp : Z9Spec.SP9 m w v := ⟨dP0, d20, d40, d2P, d4P, d42, dy8, dy7, db⟩
  have s3 := Z9All.sg3 m hpr w v hr nd sp
  have h9 : ∀ x, Z9Map.sg m w v (Z9Map.sg m w v (Z9Map.sg m w v (Z9Map.sg m w v (Z9Map.sg m w v (Z9Map.sg m w v (Z9Map.sg m w v (Z9Map.sg m w v (Z9Map.sg m w v (x))))))))) = x := fun x => by
    rw [s3 x, s3 (G5Tate3.tau m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (w) ((ring (2  -- (+1684 chars; full line in the Zenodo source)
    exact G5Tate3.tau3 m hpr _ _ db x
  have hfix : ∀ x, Z9Map.sg m w v (Z9Map.sg m w v (Z9Map.sg m w v (x))) = x → G5Tate3.onT m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 *  -- (+383 chars; full line in the Zenodo source)
    rw [s3 x] at h
    exact G5Tate3.onT_fix m hpr _ _ db hd x h
  have e : sumK LibInt.intR (G5Tate3.onT m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (w  -- (+1043 chars; full line in the Zenodo source)
    show LibInt.intR.add 1 (sumK LibInt.intR (G5Tate3.onT m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m  -- (+352 chars; full line in the Zenodo source)
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, Orbit9.orbit9 (ptsL m) (complete_pts m) (code m) (code_inj m) (Z9Map.sg m w v) h9
    (G5Tate3.onT m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (w) (w)) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)  -- (+322 chars; full line in the Zenodo source)

end Z9Count

#print axioms Z9Count.g9

#print axioms: 1 theorem, no axioms.

78.352 Four divides the point count of the order-4 Tate family

For \(E_t:y^2+xy-ty=x^3-tx^2\) with \(t\ne 0\), \(16t+1\ne 0\) and \(p\) odd, translation by \(T=(0,0)\) has order four and moves every point.

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2+xy-ty=x^3-tx^2\}\equiv 0\pmod 4. \]

lang/T4Count.lean

/-
  lang/T4Count — T43, cycle 376: G5 for the order-4 Tate family — 4 | #E(𝔽_p) for E(t, 0): y² + xy − ty = x³ − tx²,
  t ≠ 0, p odd, 16t + 1 ≠ 0 (the discriminant is t⁴(16t + 1)).

    chain4        τ⁴P = P for P ∈ E with x ∉ {0, t} (the step identities of lang/T4LF glued by G7LF.glue)
    tau4          τ⁴ = id on {O} ∪ 𝔽_p²; torsion cycle O → (0, 0) → (t, 0) → (0, t) → O
    fix2_4        τ²P = P with x ∉ {0, t} forces x = 2t, y = 8t², 4(16t + 1) = 0
    onG_fix4      H = 0 wherever τ²P = P
    g4t           1 + #{(x, y) ∈ 𝔽_p² on E(t, 0)} = 4k   (Orbit4.orbit4)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.T4LF
import lang.Orbit4

namespace T4LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1 Pairs Orbit5
open G7LF (tauG tauG_some tauG_gen tauG_off glue onG onG_on onG_off onG_tau fst_ne snd_ne yroot tO tT1 sub_ne)
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

theorem chain4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m (a) ((ring (2 * m + 1 + 1)).zero) x y) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero)
    (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ a) : tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) (some (x, y))))) = some (x, y) := by
  have n_L1d : L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L1d m hpr a x y hc h0 hb
  have n_L2d : L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L2d m hpr a x y hc h0 hb
  have n_L3d : L3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L3d m hpr a x y h0
  have n_X1d : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hc h0 hb
  have n_X1n : X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1n m hpr a x y ht0 hb
  have n_X2d : X2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2d m hpr a x y hb
  have n_X2n : X2n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2n m hpr a x y ht0 h0
  have n_X3d : X3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3d m hpr a x y hc h0 hb
  have n_X3n : X3n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3n m hpr a x y ht0 hb
  have ix : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x h0
  have xj0 : (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).mul (iv m x) x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := ix
  have xj1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X1n m a x y)) (X1n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X1d, iv_mul_self m hpr _ n_X1n]; lib_ring
  have nx1 : (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X1n (iv_nz m hpr _ n_X1d)
  have xj2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X2d m a x y)) (X2d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X2n m a x y)) (X2n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X2d, iv_mul_self m hpr _ n_X2n]; lib_ring
  have nx2 : (ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X2n (iv_nz m hpr _ n_X2d)
  have xj3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X3d m a x y)) (X3d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X3n m a x y)) (X3n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X3d, iv_mul_self m hpr _ n_X3n]; lib_ring
  have nx3 : (ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X3n (iv_nz m hpr _ n_X3d)
  have xj4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).mul (iv m x) x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := ix
  have e0 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = y := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
      _ = y := by rw [ix]; lib_ring
  have c0 : cB m (a) ((ring (2 * m + 1 + 1)).zero) x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x) := by rw [e0]; exact hc
  have g0 := glue m hpr (a) ((ring (2 * m + 1 + 1)).zero) (x) ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) c0 h0 xj0 (step0x m hpr a x y hc   -- (+52 chars; full line in the Zenodo source)
  have g1 := glue m hpr (a) ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)  -- (+142 chars; full line in the Zenodo source)
  have g2 := glue m hpr (a) ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y))) ((ring (2 * m + 1 + 1)  -- (+136 chars; full line in the Zenodo source)
  have g3 := glue m hpr (a) ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +   -- (+104 chars; full line in the Zenodo source)
  have key : tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) (some (x, (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x))))) = some ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv   -- (+76 chars; full line in the Zenodo source)
    rw [g0.1, g1.1, g2.1, g3.1]
  rw [e0] at key
  rw [key]
  exact congrArg some (Prod.ext (show (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one) = x by lib_ring) (show (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one)) = y by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
      _ = y := by rw [ix]; lib_ring))

theorem tT2 (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) :
    tauG m (a) ((ring (2 * m + 1 + 1)).zero) (some ((a), (ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).zero))) = some ((ring (2 * m + 1 + 1)).zero, (a)) := by
  rw [tauG_gen m _ _ _ _ (by unfold cB; lib_ring) ht0]
  unfold tx7 ty7
  exact congrArg some (Prod.ext (show (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a (ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul (iv m a) (iv m a))) = (ring (2 * m + 1 + 1)).zero by lib_ring)
    (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a (ring (2 * m + 1 + 1)).zero) (iv m a)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul ((ring (2   -- (+119 chars; full line in the Zenodo source)

theorem tT3 (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) : tauG m (a) ((ring (2 * m + 1 + 1)).zero) (some ((ring (2 * m + 1 + 1)).zero, (a))) = none := by
  rw [tauG_some, ite_pos' _ _ (show (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero (a)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) (a)))   -- (+367 chars; full line in the Zenodo source)

/-- **τ⁴ = id** on {O} ∪ 𝔽_p² for E(t, 0). -/
theorem tau4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) :
    tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) (P)))) = P := by
  cases P with
  | none => rw [tO, tT1, tT2 m a ht0, tT3 m a ht0]
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1))  -- (+135 chars; full line in the Zenodo source)
    · by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [h0] at hc ⊢
        rcases yroot m hpr y (ring (2 * m + 1 + 1)).zero (a) (by
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) y  -- (+365 chars; full line in the Zenodo source)
            _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e | e <;> rw [e]
        · rw [tT1, tT2 m a ht0, tT3 m a ht0, tO]
        · rw [tT3 m a ht0, tO, tT1, tT2 m a ht0]
      · by_cases hb : x = (a)
        · rw [hb] at hc ⊢
          rcases yroot m hpr y ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).zero)) (by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul (a) y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) y))) ((ring (2 * m + 1   -- (+223 chars; full line in the Zenodo source)
              _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e | e <;> rw [e]
          · rw [tT2 m a ht0, tT3 m a ht0, tO, tT1]
          · rw [tT2 m a ht0, tT3 m a ht0, tO, tT1]
        · exact chain4 m hpr a x y hc ht0 h0 hb
    · rw [tauG_off m _ _ x y hc, tauG_off m _ _ x y hc, tauG_off m _ _ x y hc, tauG_off m _ _ x y hc]

theorem half4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m (a) ((ring (2 * m + 1 + 1)).zero) x y) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero)
    (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ a) : tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) (some (x, y))) = some ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y)), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y)))) := by
  have n_L1d : L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L1d m hpr a x y hc h0 hb
  have n_L2d : L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L2d m hpr a x y hc h0 hb
  have n_X1d : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hc h0 hb
  have n_X1n : X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1n m hpr a x y ht0 hb
  have n_X2d : X2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2d m hpr a x y hb
  have n_X2n : X2n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2n m hpr a x y ht0 h0
  have ix : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x h0
  have xj0 : (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).mul (iv m x) x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := ix
  have n2_X1n : X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1n m hpr a x y ht0 hb
  have n2_X1d : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hc h0 hb
  have xj1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X1n m a x y)) (X1n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n2_X1d, iv_mul_self m hpr _ n2_X1n]; lib_ring
  have nx1 : (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n2_X1n (iv_nz m hpr _ n2_X1d)
  have n2_X2n : X2n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2n m hpr a x y ht0 h0
  have n2_X2d : X2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2d m hpr a x y hb
  have xj2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X2d m a x y)) (X2d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X2n m a x y)) (X2n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n2_X2d, iv_mul_self m hpr _ n2_X2n]; lib_ring
  have nx2 : (ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n2_X2n (iv_nz m hpr _ n2_X2d)
  have e0 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = y := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
      _ = y := by rw [ix]; lib_ring
  have c0 : cB m (a) ((ring (2 * m + 1 + 1)).zero) x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x) := by rw [e0]; exact hc
  have g0 := glue m hpr (a) ((ring (2 * m + 1 + 1)).zero) (x) ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) c0 h0 xj0 (step0x m hpr a x y hc   -- (+52 chars; full line in the Zenodo source)
  have g1 := glue m hpr (a) ((ring (2 * m + 1 + 1)).zero) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)  -- (+142 chars; full line in the Zenodo source)
  have key : tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) (some (x, (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x))) = some ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y)), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a  -- (+14 chars; full line in the Zenodo source)
    rw [g0.1, g1.1]
  rw [e0] at key
  exact key

theorem two_ne (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ≠   -- (+33 chars; full line in the Zenodo source)
  have e : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) :  -- (+13 chars; full line in the Zenodo source)
  rw [e]; exact mul_ne m hpr _ _ h2 h2

/-- τ²P = P at a generic point forces 4(16t + 1) = 0. -/
theorem fix2_4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m (a) ((ring (2 * m + 1 + 1)).zero) x y) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ a)
    (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).  -- (+266 chars; full line in the Zenodo source)
    (h : tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) (some (x, y))) = some (x, y)) : False := by
  rw [half4 m hpr a x y hc ht0 h0 hb] at h
  have e' := Option.some.inj h
  have fx : (ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y)) = x := congrArg Prod.fst e'
  have fy : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) = y := congrArg Prod.snd e'
  rw [fx] at fy
  have nW : X2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2d m hpr a x y hb
  have nY : L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L2d m hpr a x y hc h0 hb
  have ew : (ring (2 * m + 1 + 1)).mul (iv m (X2d m a x y)) (X2d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nW
  have ey : (ring (2 * m + 1 + 1)).mul (iv m (L2d m a x y)) (L2d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nY
  unfold X2n X2d at fx ew; unfold L2n L2d at fy ey
  unfold cB at hc
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg a) x) = w at fx ew
  generalize iv m y = j at fy ey
  have e1 : (ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) a))) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg a) x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a x)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a x) w) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg a) x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a x)) := by rw [fx]
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a x) ((ring (2 * m + 1 + 1)).mul w ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg a) x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul a x)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [ew]; lib_ring
  have hx2 : x = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) a := by
    rcases mul_eq_zero m hpr _ _ e1 with e | e
    · exact absurd e h0
    · calc x = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add x ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) a) := by lib_ring
        _ = _ := by rw [e]; lib_ring
  have e2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).neg a) x) j) x)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) := by rw [fy]
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul x x)) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul j y))) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [ey]; lib_ring
  rw [hx2] at e2 hc
  have e3 : (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).  -- (+105 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1  -- (+889 chars; full line in the Zenodo source)
          ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) a) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) a))))) := by lib_r  -- (+3 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hc, e2]; lib_ring
  have hy8 : y = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one))) ((ring (2 * m + 1 + 1)).mul a a) := by
    rcases mul_eq_zero m hpr _ _ e3 with e | e
    · exact absurd e ht0
    · calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1))  -- (+454 chars; full line in the Zenodo source)
        _ = _ := by rw [e]; lib_ring
  rw [hy8] at e2
  have e4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +   -- (+472 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one)  -- (+709 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := e2
  exact mul_ne m hpr _ _ (two_ne m hpr h2) (mul_ne m hpr _ _ ht0 (mul_ne m hpr _ _ ht0 (mul_ne m hpr _ _ ht0 hD))) e4

theorem onG_fix4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero)
    (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).  -- (+266 chars; full line in the Zenodo source)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : tauG m (a) ((ring (2 * m + 1 + 1)).zero) (tauG m (a) ((ring (2 * m + 1 + 1)).zero) P) = P) : onG m (a) ((ring (2 * m + 1 + 1)).zero) P = 0 := by
  cases P with
  | none => exact absurd h (by rw [tO, tT1]; exact fun e => by cases e)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1))  -- (+135 chars; full line in the Zenodo source)
    · exfalso
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [h0] at hc h
        rcases yroot m hpr y (ring (2 * m + 1 + 1)).zero (a) (by
          calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul (ring (2 * m + 1 + 1)).zero y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) y  -- (+365 chars; full line in the Zenodo source)
            _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e | e <;> rw [e] at h
        · rw [tT1, tT2 m a ht0] at h
          exact snd_ne m _ _ ht0 h
        · rw [tT3 m a ht0, tO] at h
          exact snd_ne m _ _ (fun e' => ht0 (show (ring (2 * m + 1 + 1)).zero = a from e').symm) h
      · by_cases hb : x = (a)
        · rw [hb] at hc h
          rcases yroot m hpr y ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).zero)) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).zero)) (by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul (a) y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) y))) ((ring (2 * m + 1   -- (+223 chars; full line in the Zenodo source)
              _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e | e <;> rw [e] at h <;> rw [tT2 m a ht0, tT3 m a ht0] at h <;> cases h
        · exact fix2_4 m hpr a x y hc ht0 h0 hb h2 hD h
    · exact onG_off m _ _ x y hc

def cnt4 (a : Zp (2 * m + 1 + 1)) (q : Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1)) : Int :=
  if (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul q.2 q.2) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).zero))) ((ring (2 * m + 1 + 1)).mul q.1 q.2))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) q.2)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mu  -- (+156 chars; full line in the Zenodo source)

/-- **G5, order 4: 4 | 1 + #E(t, 0)(𝔽_p)**, t ≠ 0, p odd, 16t + 1 ≠ 0. -/
theorem g4t (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (h2 : (ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 +  -- (+411 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (cnt4 m a) (pL m) = 4 * k := by
  have e : sumK LibInt.intR (onG m (a) ((ring (2 * m + 1 + 1)).zero)) (ptsL m) = 1 + sumK LibInt.intR (cnt4 m a) (pL m) := by
    show LibInt.intR.add 1 (sumK LibInt.intR (onG m (a) ((ring (2 * m + 1 + 1)).zero)) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, Orbit4.orbit4 (ptsL m) (complete_pts m) (code m) (code_inj m) (tauG m (a) ((ring (2 * m + 1 + 1)).zero)) (tau4 m hpr a ht0)
    (onG m (a) ((ring (2 * m + 1 + 1)).zero)) (onG_tau m hpr _ _) (onG_fix4 m hpr a ht0 h2 hD)⟩

end T4LF

#print axioms T4LF.chain4
#print axioms T4LF.tT2
#print axioms T4LF.tT3
#print axioms T4LF.tau4
#print axioms T4LF.half4
#print axioms T4LF.two_ne
#print axioms T4LF.fix2_4
#print axioms T4LF.onG_fix4
#print axioms T4LF.g4t

#print axioms: 9 theorems, no axioms.

78.353 Five divides the point count of the order-5 Tate family

For \(E_t:y^2+(1-t)xy-ty=x^3-tx^2\) with \(t\ne 0\), \(t^2-11t-1\ne 0\) (this family contains 11a at \(t=1\)), translation by \(T\) has order five and no fixed points.

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2+(1-t)xy-ty=x^3-tx^2\}\equiv 0\pmod 5. \]

lang/T5Done.lean

/-
  lang/T5Done — T43, cycle 381: G5 for the order-5 Tate family, unconditional — 5 | #E(𝔽_p) for E(t, t), t ≠ 0,
  t² − 11t − 1 ≠ 0.

  The fixed-point lemma by small equations instead of a large Bezout identity (lang/T5Fix ran out of memory on Kamiak because
  lib_ring expands large integer coefficients): at a generic fixed point τP = P also τ³P = P and τ⁴P = P, and the closed forms
  x(P + 3T) = ty/(y − tx), x(P + 4T) = (t² − tx)/y, x(P + T) = t(x − t)/(−t + (1 − t)x + y) give
      y = t − x − x²,   x³ + x² − 2tx + t² = 0,   (1 − t)x² − 3tx + 2t² = 0,
  whose resultant is −t⁵(t² − 11t − 1) = A·(…) + B·(…) with small A, B.
    halfk1 … halfk4, fixg5, onG_fix5', g5t
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.T5Count

namespace T5LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1 Pairs Orbit5
open G7LF (tauG tauG_some tauG_gen tauG_off glue onG onG_on onG_off onG_tau fst_ne snd_ne yroot tO tT1)
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

theorem halfk1 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m (a) (a) x y) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero)
    (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ a) : tauG m (a) (a) (some (x, y)) = some ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)))) := by
  have s0x := step0x m hpr
  have s0l := step0l m hpr
  have s1x := step1x m hpr
  have s1l := step1l m hpr
  have s2x := step2x m hpr
  have s2l := step2l m hpr
  have s3x := step3x m hpr
  have s3l := step3l m hpr
  have s4x := step4x m hpr
  have s4l := step4l m hpr
  have n_L1d : L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L1d m hpr a x y hc h0 hb
  have n_X1d : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hc h0 hb
  have n_X1n : X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1n m hpr a x y ht0 hb
  have ix : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x h0
  have xj0 : (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).mul (iv m x) x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := ix
  have xj1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X1n m a x y)) (X1n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X1d, iv_mul_self m hpr _ n_X1n]; lib_ring
  have nx1 : (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X1n (iv_nz m hpr _ n_X1d)
  have e0 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = y := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
      _ = y := by rw [ix]; lib_ring
  have c0 : cB m (a) (a) x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x) := by rw [e0]; exact hc
  have g0 := glue m hpr (a) (a) (x) ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) c0 h0 xj0 (s0x a x y hc h0 n_X1d) (s0l a x y hc n_L1d h0 n_  -- (+8 chars; full line in the Zenodo source)
  have key : tauG m (a) (a) (some (x, (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x)) = some ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)))) := by
    rw [g0.1]
  rw [e0] at key
  exact key

theorem halfk3 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m (a) (a) x y) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero)
    (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ a) : tauG m (a) (a) (tauG m (a) (a) (tauG m (a) (a) (some (x, y)))) = some ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y)), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y)))) := by
  have s0x := step0x m hpr
  have s0l := step0l m hpr
  have s1x := step1x m hpr
  have s1l := step1l m hpr
  have s2x := step2x m hpr
  have s2l := step2l m hpr
  have s3x := step3x m hpr
  have s3l := step3l m hpr
  have s4x := step4x m hpr
  have s4l := step4l m hpr
  have n_L1d : L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L1d m hpr a x y hc h0 hb
  have n_L2d : L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L2d m hpr a x y hb
  have n_L3d : L3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L3d m hpr a x y hc h0 hb
  have n_X1d : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hc h0 hb
  have n_X1n : X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1n m hpr a x y ht0 hb
  have n_X2d : X2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2d m hpr a x y hc h0 hb
  have n_X2n : X2n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2n m hpr a x y hc ht0 h0 hb
  have n_X3d : X3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3d m hpr a x y hc h0 hb
  have n_X3n : X3n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3n m hpr a x y hc ht0 h0 hb
  have ix : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x h0
  have xj0 : (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).mul (iv m x) x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := ix
  have xj1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X1n m a x y)) (X1n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X1d, iv_mul_self m hpr _ n_X1n]; lib_ring
  have nx1 : (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X1n (iv_nz m hpr _ n_X1d)
  have xj2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X2d m a x y)) (X2d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X2n m a x y)) (X2n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X2d, iv_mul_self m hpr _ n_X2n]; lib_ring
  have nx2 : (ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X2n (iv_nz m hpr _ n_X2d)
  have xj3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X3d m a x y)) (X3d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X3n m a x y)) (X3n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X3d, iv_mul_self m hpr _ n_X3n]; lib_ring
  have nx3 : (ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X3n (iv_nz m hpr _ n_X3d)
  have e0 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = y := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
      _ = y := by rw [ix]; lib_ring
  have c0 : cB m (a) (a) x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x) := by rw [e0]; exact hc
  have g0 := glue m hpr (a) (a) (x) ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) c0 h0 xj0 (s0x a x y hc h0 n_X1d) (s0l a x y hc n_L1d h0 n_  -- (+8 chars; full line in the Zenodo source)
  have g1 := glue m hpr (a) (a) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m   -- (+92 chars; full line in the Zenodo source)
  have g2 := glue m hpr (a) (a) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m   -- (+98 chars; full line in the Zenodo source)
  have key : tauG m (a) (a) (tauG m (a) (a) (tauG m (a) (a) (some (x, (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x)))) = some ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y)), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y)))) := by
    rw [g0.1, g1.1, g2.1]
  rw [e0] at key
  exact key

theorem halfk4 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m (a) (a) x y) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero)
    (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ a) : tauG m (a) (a) (tauG m (a) (a) (tauG m (a) (a) (tauG m (a) (a) (some (x, y))))) = some ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y)), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m (L4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y)))) := by
  have s0x := step0x m hpr
  have s0l := step0l m hpr
  have s1x := step1x m hpr
  have s1l := step1l m hpr
  have s2x := step2x m hpr
  have s2l := step2l m hpr
  have s3x := step3x m hpr
  have s3l := step3l m hpr
  have s4x := step4x m hpr
  have s4l := step4l m hpr
  have n_L1d : L1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L1d m hpr a x y hc h0 hb
  have n_L2d : L2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L2d m hpr a x y hb
  have n_L3d : L3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L3d m hpr a x y hc h0 hb
  have n_L4d : L4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_L4d m hpr a x y h0
  have n_X1d : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hc h0 hb
  have n_X1n : X1n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1n m hpr a x y ht0 hb
  have n_X2d : X2d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2d m hpr a x y hc h0 hb
  have n_X2n : X2n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X2n m hpr a x y hc ht0 h0 hb
  have n_X3d : X3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3d m hpr a x y hc h0 hb
  have n_X3n : X3n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3n m hpr a x y hc ht0 h0 hb
  have n_X4d : X4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X4d m hpr a x y hc h0 hb
  have n_X4n : X4n m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X4n m hpr a x y ht0 hb
  have ix : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x h0
  have xj0 : (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) = (ring (2 * m + 1 + 1)).mul (iv m x) x := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := ix
  have xj1 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X1n m a x y)) (X1n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X1d, iv_mul_self m hpr _ n_X1n]; lib_ring
  have nx1 : (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X1n (iv_nz m hpr _ n_X1d)
  have xj2 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X2d m a x y)) (X2d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X2n m a x y)) (X2n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X2d, iv_mul_self m hpr _ n_X2n]; lib_ring
  have nx2 : (ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X2n (iv_nz m hpr _ n_X2d)
  have xj3 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X3d m a x y)) (X3d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X3n m a x y)) (X3n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X3d, iv_mul_self m hpr _ n_X3n]; lib_ring
  have nx3 : (ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X3n (iv_nz m hpr _ n_X3d)
  have xj4 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y))) = (ring (2 * m + 1 + 1)).one := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m (X4n m a x y))) = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (iv m (X4d m a x y)) (X4d m a x y)) ((ring (2 * m + 1 + 1)).mul (iv m (X4n m a x y)) (X4n m a x y)) := by lib_ring
      _ = (ring (2 * m + 1 + 1)).one := by rw [iv_mul_self m hpr _ n_X4d, iv_mul_self m hpr _ n_X4n]; lib_ring
  have nx4 : (ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y)) ≠ (ring (2 * m + 1 + 1)).zero := mul_ne m hpr _ _ n_X4n (iv_nz m hpr _ n_X4d)
  have e0 : (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = y := by
    calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x = (ring (2 * m + 1 + 1)).mul y ((ring (2 * m + 1 + 1)).mul (iv m x) x) := by lib_ring
      _ = y := by rw [ix]; lib_ring
  have c0 : cB m (a) (a) x ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x) := by rw [e0]; exact hc
  have g0 := glue m hpr (a) (a) (x) ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).one) (iv m (x))) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) c0 h0 xj0 (s0x a x y hc h0 n_X1d) (s0l a x y hc n_L1d h0 n_  -- (+8 chars; full line in the Zenodo source)
  have g1 := glue m hpr (a) (a) ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) ((ring (2 * m + 1 + 1)).mul (L1n m a x y) (iv m (L1d m a x y))) ((ring (2 * m + 1 + 1)).mul (X1d m a x y) (iv m (X1n m a x y))) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m   -- (+92 chars; full line in the Zenodo source)
  have g2 := glue m hpr (a) (a) ((ring (2 * m + 1 + 1)).mul (X2n m a x y) (iv m (X2d m a x y))) ((ring (2 * m + 1 + 1)).mul (L2n m a x y) (iv m (L2d m a x y))) ((ring (2 * m + 1 + 1)).mul (X2d m a x y) (iv m (X2n m a x y))) ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m   -- (+98 chars; full line in the Zenodo source)
  have g3 := glue m hpr (a) (a) ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) ((ring (2 * m + 1 + 1)).mul (L3n m a x y) (iv m (L3d m a x y))) ((ring (2 * m + 1 + 1)).mul (X3d m a x y) (iv m (X3n m a x y))) ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) ((ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m (L4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4d m a x y) (iv m   -- (+98 chars; full line in the Zenodo source)
  have key : tauG m (a) (a) (tauG m (a) (a) (tauG m (a) (a) (tauG m (a) (a) (some (x, (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (iv m (x))) x))))) = some ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y)), (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (L4n m a x y) (iv m (L4d m a x y))) ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y)))) := by
    rw [g0.1, g1.1, g2.1, g3.1]
  rw [e0] at key
  exact key

/-- a fixed point of τ at a generic point of E(t, t) forces t⁵(t² − 11t − 1) = 0. -/
theorem fixg5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a x y : Zp (2 * m + 1 + 1)) (hc : cB m (a) (a) x y) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero)
    (h0 : x ≠ (ring (2 * m + 1 + 1)).zero) (hb : x ≠ a) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).  -- (+324 chars; full line in the Zenodo source)
    (h : tauG m (a) (a) (some (x, y)) = some (x, y)) : False := by
  have hk1 := h
  have hk2 : tauG m (a) (a) (tauG m (a) (a) (some (x, y))) = some (x, y) := by rw [h, h]
  have hk3 : tauG m (a) (a) (tauG m (a) (a) (tauG m (a) (a) (some (x, y)))) = some (x, y) := by rw [h, h, h]
  have hk4 : tauG m (a) (a) (tauG m (a) (a) (tauG m (a) (a) (tauG m (a) (a) (some (x, y))))) = some (x, y) := by rw [h, h, h, h]
  have f1 : (ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y)) = x := by
    have h' := hk1
    rw [halfk1 m hpr a x y hc ht0 h0 hb] at h'
    exact congrArg Prod.fst (Option.some.inj h')
  have nd1 : X1d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X1d m hpr a x y hc h0 hb
  have ew1 : (ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nd1
  have k1 : X1n m a x y = (ring (2 * m + 1 + 1)).mul x (X1d m a x y) := by
    calc X1n m a x y = (ring (2 * m + 1 + 1)).mul (X1n m a x y) ((ring (2 * m + 1 + 1)).mul (iv m (X1d m a x y)) (X1d m a x y)) := by rw [ew1]; lib_ring
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X1n m a x y) (iv m (X1d m a x y))) (X1d m a x y) := by lib_ring
      _ = _ := by rw [f1]
  have f3 : (ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y)) = x := by
    have h' := hk3
    rw [halfk3 m hpr a x y hc ht0 h0 hb] at h'
    exact congrArg Prod.fst (Option.some.inj h')
  have nd3 : X3d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X3d m hpr a x y hc h0 hb
  have ew3 : (ring (2 * m + 1 + 1)).mul (iv m (X3d m a x y)) (X3d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nd3
  have k3 : X3n m a x y = (ring (2 * m + 1 + 1)).mul x (X3d m a x y) := by
    calc X3n m a x y = (ring (2 * m + 1 + 1)).mul (X3n m a x y) ((ring (2 * m + 1 + 1)).mul (iv m (X3d m a x y)) (X3d m a x y)) := by rw [ew3]; lib_ring
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X3n m a x y) (iv m (X3d m a x y))) (X3d m a x y) := by lib_ring
      _ = _ := by rw [f3]
  have f4 : (ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y)) = x := by
    have h' := hk4
    rw [halfk4 m hpr a x y hc ht0 h0 hb] at h'
    exact congrArg Prod.fst (Option.some.inj h')
  have nd4 : X4d m a x y ≠ (ring (2 * m + 1 + 1)).zero := nz_X4d m hpr a x y hc h0 hb
  have ew4 : (ring (2 * m + 1 + 1)).mul (iv m (X4d m a x y)) (X4d m a x y) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ nd4
  have k4 : X4n m a x y = (ring (2 * m + 1 + 1)).mul x (X4d m a x y) := by
    calc X4n m a x y = (ring (2 * m + 1 + 1)).mul (X4n m a x y) ((ring (2 * m + 1 + 1)).mul (iv m (X4d m a x y)) (X4d m a x y)) := by rw [ew4]; lib_ring
      _ = (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (X4n m a x y) (iv m (X4d m a x y))) (X4d m a x y) := by lib_ring
      _ = _ := by rw [f4]
  have z1 : (ring (2 * m + 1 + 1)).add (X1n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X1d m a x y))) = (ring (2 * m + 1 + 1)).zero := by rw [k1]; lib_ring
  have z3 : (ring (2 * m + 1 + 1)).add (X3n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X3d m a x y))) = (ring (2 * m + 1 + 1)).zero := by rw [k3]; lib_ring
  have z4 : (ring (2 * m + 1 + 1)).add (X4n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X4d m a x y))) = (ring (2 * m + 1 + 1)).zero := by rw [k4]; lib_ring
  have hH2 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ri  -- (+254 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (X1n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X1d m a x y))))) ((ring (2 * m + 1 + 1)).add (X4n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X4d m a x y)))) := by
          unfold X1n X1d X4n X4d; lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [z1, z4]; lib_ring
  have zA : (ring (2 * m + 1 + 1)).mul a ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x)))))) = (ring (2 * m + 1 + 1)).zero := by
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (X3n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X3d m a x y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add (X4n m a x y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X4d m a x y))))) := by
          unfold X3n X3d X4n X4d; lib_ring
      _ = (ring (2 * m + 1 + 1)).zero := by rw [z3, z4]; lib_ring
  have hy : y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))) := by
    rcases mul_eq_zero m hpr _ _ zA with e | e
    · exact absurd e ht0
    · calc y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add y ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x)))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +  -- (+38 chars; full line in the Zenodo source)
        _ = _ := by rw [e]; lib_ring
  rw [hy] at z4
  have hH6 : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).one)) ((ring (2 * m + 1 + 1)).mul (a) (x))))) ((ring (2 * m  -- (+59 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add (X4n m a x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (x) (x))))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul x (X4d m a x ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add (a) ((ring (2 * m + 1 + 1)).neg (x))) ((ring (2 * m + 1 + 1)).  -- (+76 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := z4
  have key : (ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 +   -- (+474 chars; full line in the Zenodo source)
    calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m +   -- (+5559 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).zero := by rw [hH6, hH2]; lib_ring
  exact neg_ne m _ (mul_ne m hpr _ _ (mul_ne m hpr _ _ ht0 (mul_ne m hpr _ _ ht0 (mul_ne m hpr _ _ ht0 (mul_ne m hpr _ _ ht0 ht0)))) hD) key

theorem onG_fixg5 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero)
    (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1  -- (+272 chars; full line in the Zenodo source)
    (P : Option (Zp (2 * m + 1 + 1) × Zp (2 * m + 1 + 1))) (h : tauG m (a) (a) P = P) : onG m (a) (a) P = 0 := by
  cases P with
  | none => exact absurd h (fun e => by cases e)
  | some p =>
    obtain ⟨x, y⟩ := p
    by_cases hc : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x ((ring (2 * m + 1 +  -- (+109 chars; full line in the Zenodo source)
    · exfalso
      by_cases h0 : x = (ring (2 * m + 1 + 1)).zero
      · rw [tauG_some, ite_pos' _ _ hc, ite_pos' _ _ h0] at h
        by_cases hy : y = (ring (2 * m + 1 + 1)).zero
        · rw [ite_pos' _ _ hy, h0] at h
          exact fst_ne m _ _ ht0 h
        · rw [ite_neg' _ _ hy] at h
          cases h
      · by_cases hb : x = (a)
        · rw [hb] at hc h
          rcases yroot m hpr y (ring (2 * m + 1 + 1)).zero ((ring (2 * m + 1 + 1)).mul (a) (a)) (by
            calc _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg (a))) ((ring (2 * m + 1 + 1)).mul (a) y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (a) y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m +   -- (+197 chars; full line in the Zenodo source)
              _ = (ring (2 * m + 1 + 1)).zero := by rw [hc]; lib_ring) with e | e <;> rw [e] at h
          · rw [tT3 m a ht0] at h
            exact fst_ne m _ _ (fun e' => ht0 (show (ring (2 * m + 1 + 1)).zero = a from e').symm) h
          · rw [tT2 m hpr a ht0] at h
            exact snd_ne m _ _ (fun e' => mul_ne m hpr _ _ ht0 ht0 (show (ring (2 * m + 1 + 1)).zero = (ring (2 * m + 1 + 1)).mul a a from e').symm) h
        ·           exact fixg5 m hpr a x y hc ht0 h0 hb hD (by rw [h])
    · exact onG_off m _ _ x y hc

theorem g5t (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero)
     (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) (a)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 +   -- (+275 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (cnt5 m a) (pL m) = 5 * k := by
  have e : sumK LibInt.intR (onG m (a) (a)) (ptsL m) = 1 + sumK LibInt.intR (cnt5 m a) (pL m) := by
    show LibInt.intR.add 1 (sumK LibInt.intR (onG m (a) (a)) ((pL m).map some)) = _
    rw [sum_map]; rfl
  rw [← e]
  exact ⟨_, Orbit5.orbit5 (ptsL m) (complete_pts m) (code m) (code_inj m) (tauG m (a) (a)) (tau5 m hpr a ht0)
    (onG m (a) (a)) (onG_tau m hpr _ _) (onG_fixg5 m hpr a ht0 hD)⟩

end T5LF
#print axioms T5LF.halfk1
#print axioms T5LF.halfk3
#print axioms T5LF.halfk4
#print axioms T5LF.fixg5
#print axioms T5LF.onG_fixg5
#print axioms T5LF.g5t

#print axioms: 6 theorems, no axioms.

78.354 Six divides the point count of the order-6 Tate family

For \(E_t:y^2+(1-t)xy-(t+t^2)y=x^3-(t+t^2)x^2\) with \(t\ne 0,-1\) and \(9t+1\ne 0\), translation by \(T\) has order six and no point is fixed by \(\tau ^2\) or \(\tau ^3\).

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2+(1-t)xy-(t+t^2)y=x^3-(t+t^2)x^2\}\equiv 0\pmod 6. \]

lang/T6Final.lean

/-
  lang/T6Final — T43, cycle 389: G5 for the order-6 Tate family — 6 | #E(𝔽_p) for E: y² + (1 − t)xy − (t + t²)y = x³ − (t + t²)x²,
  t ≠ 0, −1, 9t + 1 ≠ 0 (discriminant t⁶(t + 1)³(9t + 1)), unconditional.
  Lean 4 core only, no axioms.
-/
import lang.T6Count
import lang.T6Big

namespace T6LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1 Pairs

variable (m : Nat)

theorem big6 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : Big6 m := ⟨step1x m hpr, step2x m hpr, step4x m hpr⟩

/-- **G5, order 6: 6 | #E(𝔽_p).** -/
theorem g6t (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (htm : (ring (2 * m + 1 + 1)).add a (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).mul ((ring (2 * m +  -- (+331 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (cnt6 m a) (pL m) = 6 * k :=
  g6t_of m hpr (big6 m hpr) a ht0 htm hD

end T6LF

#print axioms T6LF.big6
#print axioms T6LF.g6t

#print axioms: 2 theorems, no axioms.

78.355 Seven divides the point count of the order-7 Tate family

For \(E_t:y^2+(1-c)xy-by=x^3-bx^2\) with \(b=t^3-t^2\), \(c=t^2-t\), \(t\ne 0,1\) and \(t^3-8t^2+5t+1\ne 0\), translation by \(T=(0,0)\) has order seven and no fixed points.

\[ 1+\#\{(x,y)\in \mathbb F_p^2:\ y^2+(1-c)xy-by=x^3-bx^2\}\equiv 0\pmod 7. \]

lang/G7Final.lean

/-
  lang/G7Final — T43, cycle 388: G5 order 7 — 7 | #E_t(𝔽_p) for the Tate normal form with a point of order 7.

    steps7     the fourteen step identities (lang/G7S0, G7R1 … G7R5, G7S6) as Steps7
    g7         for E_t: y² + (1 − c)xy − by = x³ − bx², b = t³ − t², c = t² − t, with t ≠ 0, 1 and
               t³ − 8t² + 5t + 1 ≠ 0 (E_t nonsingular mod p):  1 + #{(x, y) ∈ 𝔽_p² on E_t} = 7k
  Steps 1–5 via the rescaled cores lang/G7C*, G7R* (all on the Mac); the fixed-point lemma is lang/G7Done.
  Lean 4 core only, no axioms.
-/
import lang.G7Done
import lang.G7S0
import lang.G7R1
import lang.G7R2
import lang.G7R3
import lang.G7R4
import lang.G7R5
import lang.G7S6

namespace G7LF

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1 Pairs

variable (m : Nat)

theorem steps7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) : Steps7 m :=
  ⟨step0x m hpr, step0l m hpr, step1x m hpr, step1l m hpr, step2x m hpr, step2l m hpr, step3x m hpr, step3l m hpr, step4x m hpr, step4l m hpr, step5x m hpr, step5l m hpr, step6x m hpr, step6l m hpr⟩

/-- **G5, order 7: 7 | #E_t(𝔽_p).** -/
theorem g7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (a : Zp (2 * m + 1 + 1)) (ht0 : a ≠ (ring (2 * m + 1 + 1)).zero) (ht1 : a ≠ (ring (2 * m + 1 + 1)).one)
    (hD : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (a) ((ring (2 * m + 1 + 1)).mul (a) (a))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one (ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m +  -- (+512 chars; full line in the Zenodo source)
    ∃ k : Int, 1 + sumK LibInt.intR (cnt7 m a) (pL m) = 7 * k :=
  g7t_of m hpr (steps7 m hpr) a ht0 ht1 hD

end G7LF

#print axioms G7LF.steps7
#print axioms G7LF.g7

#print axioms: 2 theorems, no axioms.

78.356 Translation on the Tate normal form

On \(E(b,c):y^2+(1-c)xy-by=x^3-bx^2\) translation by \(T=(0,0)\) preserves the curve and is inverted through negation.

\[ \tau (x,y)=\Big (\frac {by}{x^2},\,-\Big (\frac yx+1-c\Big )\frac {by}{x^2}+b\Big ),\qquad \tau (-\tau (-Q))=Q. \]

lang/G7Tate1.lean

/-
  lang/G7Tate1 — T43, cycle 367: Tate normal form E(b, c): y² + (1 − c)xy − by = x³ − bx², translation by T = (0, 0) (G5 order 7, part 1).

    tx7, ty7   P + T = (by/x², −(y/x + 1 − c)·by/x² + b)        (x ≠ 0)
    on7        P + T stays on E(b, c)
    ny, neg7   −P = (x, −y − (1 − c)x + b) is on E(b, c)
    mu7, inv7  τ(−τ(−Q)) = Q: translation by T has the explicit inverse Q ↦ −(−Q + T)
  For b = t³ − t², c = t² − t the point T has order 7; the later parts use P − kT = −((−P) + kT).
  Certificates from the std-lib Python reduction. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G5Tate3

namespace G7Tate1

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map

variable (m : Nat)

def cB (b c x y : Zp (2 * m + 1 + 1)) : Prop := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul y y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)) ((ring (2 * m + 1 + 1)).mul x y))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul b y)) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1))  -- (+133 chars; full line in the Zenodo source)

def tx7 (b _c x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (iv m x) (iv m x)))
def ty7 (b c x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (iv m x)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c)))) ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul   -- (+26 chars; full line in the Zenodo source)
def ny (b c x y : Zp (2 * m + 1 + 1)) : Zp (2 * m + 1 + 1) := (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg y) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)) x))) b

/-- **P + T stays on E(b, c).** -/
theorem on7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x y : Zp (2 * m + 1 + 1)) (hc : cB m b c x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) :
    cB m b c (tx7 m b c x y) (ty7 m b c x y) := by
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  unfold cB at hc ⊢; unfold tx7 ty7
  generalize iv m x = i at ei ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m   -- (+238 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (i)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c)))) ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((rin  -- (+5394 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul (i) (i)))))  -- (+328 chars; full line in the Zenodo source)

/-- **−P is on E(b, c).** -/
theorem neg7 (b c x y : Zp (2 * m + 1 + 1)) (hc : cB m b c x y) : cB m b c x (ny m b c x y) := by
  unfold cB at hc ⊢; unfold ny
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m   -- (+238 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) (x)))) (b)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring  -- (+1772 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (x)))) := by rw [gcv]; lib_ring

theorem mu7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x w : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hw : w ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) :
    (ring (2 * m + 1 + 1)).mul (tx7 m b c x w) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul (iv m w) (iv m b))) = (ring (2 * m + 1 + 1)).one := by
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m w) w = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr w hw
  have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold tx7
  generalize iv m x = i at ei ⊢
  generalize iv m w = j at ej ⊢
  generalize iv m b = l at el ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have glb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (l) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (w) ((ring (2 * m + 1 + 1)).mul (i) (i)))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).mul (j) (l))) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1   -- (+778 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gjw, glb]; lib_ring

/-- **τ(−τ(−Q)) = Q.** -/
theorem inv7 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x y : Zp (2 * m + 1 + 1)) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hw : ny m b c x y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) :
    tx7 m b c (tx7 m b c x (ny m b c x y)) (ny m b c (tx7 m b c x (ny m b c x y)) (ty7 m b c x (ny m b c x y))) = x ∧
    ty7 m b c (tx7 m b c x (ny m b c x y)) (ny m b c (tx7 m b c x (ny m b c x y)) (ty7 m b c x (ny m b c x y))) = y := by
  have hyw : y = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (ny m b c x y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)) x))) b := by unfold ny; lib_ring
  generalize ny m b c x y = w at hw hyw ⊢
  have hU := iv_eq m hpr _ _ (mu7 m hpr b c x w hx hw hb)
  rw [show tx7 m b c (tx7 m b c x w) (ny m b c (tx7 m b c x w) (ty7 m b c x w)) =
      (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (ny m b c (tx7 m b c x w) (ty7 m b c x w)) ((ring (2 * m + 1 + 1)).mul (iv m (tx7 m b c x w)) (iv m (tx7 m b c x w)))) from rfl,
    show ty7 m b c (tx7 m b c x w) (ny m b c (tx7 m b c x w) (ty7 m b c x w)) =
      (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ny m b c (tx7 m b c x w) (ty7 m b c x w)) (iv m (tx7 m b c x w))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)))
        ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (ny m b c (tx7 m b c x w) (ty7 m b c x w)) ((ring (2 * m + 1 + 1)).mul (iv m (tx7 m b c x w)) (iv m (tx7 m b c x w))))))) b from rfl, hU]
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m w) w = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr w hw
  have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold ny ty7 tx7
  generalize iv m x = i at ei ⊢
  generalize iv m w = j at ej ⊢
  generalize iv m b = l at el ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have glb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (l) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
  constructor
  · calc (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (w) (i)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c)))) ((ring   -- (+2608 chars; full line in the Zenodo source)
    _ = x := by rw [gix, gjw, glb]; lib_ring
  · rw [hyw]
    calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (w) (i)) ((ring (2 * m + 1 +  -- (+10678 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg (w)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) (x)))) (b) := by rw [gix, gjw, glb]; lib_ring

end G7Tate1

#print axioms G7Tate1.on7
#print axioms G7Tate1.neg7
#print axioms G7Tate1.mu7
#print axioms G7Tate1.inv7

#print axioms: 4 theorems, no axioms.

78.357 Twice the translation on the Tate normal form

Applying the translation by \(T\) twice to \(P=(x,y)\) on \(E(b,c)\), \(x,y,b\ne 0\), gives a closed form.

\[ \tau ^2P=\Big (\frac {bx(x^2-y)}{y^2},\ -\frac {b(x^3-y^2)(x^2-y)}{y^3}+b\Big ). \]

lang/G7Tate2.lean

/-
  lang/G7Tate2 — T43, cycle 368: P + 2T on the Tate normal form E(b, c) (G5 order 7, part 2).

    step72   for P = (x, y) ∈ E(b, c), x, y, b ≠ 0:
             τ(τP) = P + 2T = (bx(x² − y)/y², −b(x³ − y²)(x² − y)/y³ + b)
  Certificate from the std-lib Python reduction. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7Tate1

namespace G7Tate2

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

/-- **P + 2T.** -/
theorem step72 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x y : Zp (2 * m + 1 + 1)) (hc : cB m b c x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) :
    tx7 m b c (tx7 m b c x y) (ty7 m b c x y) = (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y)))) ∧ ty7 m b c (tx7 m b c x y) (ty7 m b c x y) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 *   -- (+438 chars; full line in the Zenodo source)
  have hU := iv_eq m hpr _ _ (mu7 m hpr b c x y hx hy hb)
  rw [show tx7 m b c (tx7 m b c x y) (ty7 m b c x y) = (ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (ty7 m b c x y) ((ring (2 * m + 1 + 1)).mul (iv m (tx7 m b c x y)) (iv m (tx7 m b c x y)))) from rfl,
    show ty7 m b c (tx7 m b c x y) (ty7 m b c x y) = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (ty7 m b c x y) (iv m (tx7 m b c x y))) ((ring (2 * m + 1 + 1)).add (ring (2 * m + 1 + 1)).one ((ring (2 * m + 1 + 1)).neg c)))
      ((ring (2 * m + 1 + 1)).mul b ((ring (2 * m + 1 + 1)).mul (ty7 m b c x y) ((ring (2 * m + 1 + 1)).mul (iv m (tx7 m b c x y)) (iv m (tx7 m b c x y))))))) b from rfl, hU]
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  unfold cB at hc
  unfold ty7
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = l at el ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have glb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (l) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m   -- (+238 chars; full line in the Zenodo source)
  constructor
  · calc (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (i)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c)))) ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul  -- (+5761 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).mul (j) (j)))) := by rw [gix, glb, gcv]; lib_ring
  · calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (i)) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c)))) ((ring (2 *  -- (+40454 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) (y)))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1  -- (+134 chars; full line in the Zenodo source)

end G7Tate2

#print axioms G7Tate2.step72

#print axioms: 1 theorem, no axioms.

78.358 Three times the translation on the Tate normal form

The third translate of \(P=(x,y)\) on \(E(b,c)\) by \(T\), for \(x,y,b,x^2-y\ne 0\), with \(M=by+cxy-bx^2\), \(K=-bx^3+bxy+x^2y-(1-c)y^2\).

\[ P+3T=\Big (\frac {yM}{(x^2-y)^2},\ b-\frac {KM}{(x^2-y)^3}\Big ). \]

lang/G7Tate3.lean

/-
  lang/G7Tate3 — T43, cycle 369: P + 3T on the Tate normal form E(b, c) (G5 order 7, part 3).

    mu73     x(P + 2T) · y²/(x(x² − y)b) = 1
    step73   for P = (x, y) ∈ E(b, c) with x, y, b, x² − y ≠ 0, τ(P + 2T) = P + 3T =
               (yM/(x² − y)², b − KM/(x² − y)³),  M = by + cxy − bx²,  K = −bx³ + bxy + x²y − (1 − c)y²
  The y-coordinate goes through y(τQ) = −(Y/X + 1 − c)·x(τQ) + b, so its certificate is small.
  Certificates from the std-lib Python reduction. Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.G7Tate2

namespace G7Tate3

open FTAChain ZMod YMChain FiniteRecChain QChar ConicSum PencilClosed G4Step G4Gen G4Map G7Tate1

variable (m : Nat)

theorem mu73 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x y : Zp (2 * m + 1 + 1)) (hc : cB m b c x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y) ≠ (ring (2 * m + 1 + 1)).zero) : (ring (2 * m + 1 +   -- (+525 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hw
  unfold cB at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = l at el ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) = k at ek ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have glb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (l) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
  have gkw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m   -- (+238 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).mul (j) (j))))) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul (i) ((ring (2 * m + 1 + 1)).mul (k) (l))))  -- (+1995 chars; full line in the Zenodo source)
  _ = (ring (2 * m + 1 + 1)).one := by rw [gix, gjy, glb, gkw]; lib_ring

/-- **P + 3T.** -/
theorem step73 (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (b c x y : Zp (2 * m + 1 + 1)) (hc : cB m b c x y) (hx : x ≠ (ring (2 * m + 1 + 1)).zero) (hy : y ≠ (ring (2 * m + 1 + 1)).zero) (hb : b ≠ (ring (2 * m + 1 + 1)).zero) (hw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y) ≠ (ring (2 * m + 1 + 1)).zero) : tx7 m b c ((ring   -- (+3338 chars; full line in the Zenodo source)
  have hX := iv_eq m hpr _ _ (mu73 m hpr b c x y hc hx hy hb hw)
  have h1 : tx7 m b c ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1  -- (+925 chars; full line in the Zenodo source)
    rw [show tx7 m b c ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 +   -- (+1466 chars; full line in the Zenodo source)
    have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
    have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
    have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
    have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hw
    unfold cB at hc
    generalize iv m x = i at ei ⊢
    generalize iv m y = j at ej ⊢
    generalize iv m b = l at el ⊢
    generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) = k at ek ⊢
    have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
    have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
    have glb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (l) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
    have gkw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring
    have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 *   -- (+240 chars; full line in the Zenodo source)
    calc (ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (y) (y)))) ((ring (2 * m + 1 + 1))  -- (+11009 chars; full line in the Zenodo source)
      _ = (ring (2 * m + 1 + 1)).mul (y) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (b) (y)) ((ring (2 * m + 1 + 1)).mul (c) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (x))))) ((ring (2 * m + 1 + 1)).mul (k) (k))) := by rw [gix, gjy, glb, gcv]  -- (+10 chars; full line in the Zenodo source)
  refine ⟨h1, ?_⟩
  rw [show ty7 m b c ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y))) ((ring (2 * m + 1 + 1)).mul (iv m y) (iv m y))))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)  -- (+2120 chars; full line in the Zenodo source)
  have ei : (ring (2 * m + 1 + 1)).mul (iv m x) x = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr x hx
  have ej : (ring (2 * m + 1 + 1)).mul (iv m y) y = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr y hy
  have el : (ring (2 * m + 1 + 1)).mul (iv m b) b = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr b hb
  have ek : (ring (2 * m + 1 + 1)).mul (iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y))) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) = (ring (2 * m + 1 + 1)).one := iv_mul_self m hpr _ hw
  unfold cB at hc
  generalize iv m x = i at ei ⊢
  generalize iv m y = j at ej ⊢
  generalize iv m b = l at el ⊢
  generalize iv m ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul x x) ((ring (2 * m + 1 + 1)).neg y)) = k at ek ⊢
  have gix : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (i) (x)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ei]; lib_ring
  have gjy : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (j) (y)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ej]; lib_ring
  have glb : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (l) (b)) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [el]; lib_ring
  have gkw : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (k) ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) (x)) ((ring (2 * m + 1 + 1)).neg (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).one)) = (ring (2 * m + 1 + 1)).zero := by rw [ek]; lib_ring
  have gcv : (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (y) (y)) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).one) ((ring (2 * m + 1 + 1)).neg (c))) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) (y)))) ((ring (2 * m + 1 + 1)).neg ((ring (2 * m   -- (+238 chars; full line in the Zenodo source)
  calc (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))) ((ring (2 *   -- (+26523 chars; full line in the Zenodo source)
    _ = (ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).add ((ring (2 * m + 1 + 1)).neg ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) ((ring (2 * m + 1 + 1)).mul (x) (x))))) ((ring (2 * m + 1 + 1)).mul (b) ((ring (2 * m + 1 + 1)).mul (x) (y)))) ((ring (2 * m + 1 + 1)  -- (+649 chars; full line in the Zenodo source)

end G7Tate3

#print axioms G7Tate3.mu73
#print axioms G7Tate3.step73

#print axioms: 2 theorems, no axioms.

78.359 Roots of unity and the power-residue symbol

In a commutative ring without zero divisors, every \(n\)-th root of unity is a power of one of exact order \(n\), and \(a^e=w^j\) (\(j<n\)) is unique and multiplicative.

\[ w\ne 1,\ w^n=1\ \Rightarrow \ \textstyle \sum _{i<n}w^i=0;\qquad t^n=1\ \Rightarrow \ t\in \{w^j: j<n\}. \]

lang/RootSum.lean

/-
  lang/RootSum — T49, cycle 827 (general ℓ, step 1; the ℓ = 11 plan's Und1 made uniform in ℓ): **the sum of the powers of a
  nontrivial n-th root of unity vanishes**, in any commutative ring without zero divisors:
    gsum         Σ_{i<n} wⁱ
    geom         (w − 1)·Σ_{i<n} wⁱ = wⁿ − 1
    root_sum     w ≠ 1, wⁿ = 1 ⇒ Σ_{i<n} wⁱ = 0
  (Quint1.r_zero is the case n = 5 in 𝔽_p.)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.HasseCM
import lib.RingTac

namespace RootSum

open Frobenius

variable {R : Type} (K : DetChain.CRing R)

/-- Σ_{i<n} wⁱ. -/
def gsum (w : R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (gsum w n) (pw K w n)

/-- **(w − 1)·Σ_{i<n} wⁱ = wⁿ − 1.** -/
theorem geom (w : R) : ∀ n : Nat, K.mul (K.add w (K.neg K.one)) (gsum K w n) = K.add (pw K w n) (K.neg K.one)
  | 0 => by
    show K.mul (K.add w (K.neg K.one)) K.zero = K.add K.one (K.neg K.one)
    lib_ring
  | n + 1 => by
    show K.mul (K.add w (K.neg K.one)) (K.add (gsum K w n) (pw K w n)) = K.add (K.mul (pw K w n) w) (K.neg K.one)
    calc K.mul (K.add w (K.neg K.one)) (K.add (gsum K w n) (pw K w n))
        = K.add (K.mul (K.add w (K.neg K.one)) (gsum K w n)) (K.mul (K.add w (K.neg K.one)) (pw K w n)) := by lib_ring
      _ = K.add (K.add (pw K w n) (K.neg K.one)) (K.mul (K.add w (K.neg K.one)) (pw K w n)) := by rw [geom w n]
      _ = K.add (K.mul (pw K w n) w) (K.neg K.one) := by lib_ring

/-- **w ≠ 1 and wⁿ = 1 force Σ_{i<n} wⁱ = 0** (no zero divisors). -/
theorem root_sum (hK : DetChain.NoZD K) (w : R) (n : Nat) (h1 : w ≠ K.one) (hn : pw K w n = K.one) : gsum K w n = K.zero := by
  have e : K.mul (K.add w (K.neg K.one)) (gsum K w n) = K.zero := by
    rw [geom K w n, hn]; lib_ring
  rcases hK _ _ e with e1 | e1
  · exact absurd (by
      calc w = K.add (K.add w (K.neg K.one)) K.one := by lib_ring
        _ = K.add K.zero K.one := by rw [e1]
        _ = K.one := by lib_ring) h1
  · exact e1

/-- a nonzero element has nonzero powers (no zero divisors). -/
theorem pw_ne_zero (hK : DetChain.NoZD K) (w : R) (hw : w ≠ K.zero) (h10 : K.one ≠ K.zero) : ∀ j : Nat, pw K w j ≠ K.zero
  | 0 => h10
  | j + 1 => fun e => by
    rcases hK _ _ (show K.mul (pw K w j) w = K.zero from e) with e1 | e1
    · exact pw_ne_zero hK w hw h10 j e1
    · exact hw e1

/-- **distinct powers**: if w^(d+1) ≠ 1 then wʲ ≠ w^(j + d + 1) (no Nat subtraction: k = j + (d + 1)). -/
theorem pow_ne (hK : DetChain.NoZD K) (w : R) (hw : w ≠ K.zero) (h10 : K.one ≠ K.zero) (j d : Nat)
    (hd : pw K w (d + 1) ≠ K.one) : pw K w j ≠ pw K w (j + (d + 1)) := fun e => by
  have e2 : K.mul (pw K w j) (K.add (pw K w (d + 1)) (K.neg K.one)) = K.zero := by
    calc K.mul (pw K w j) (K.add (pw K w (d + 1)) (K.neg K.one))
        = K.add (K.mul (pw K w j) (pw K w (d + 1))) (K.neg (pw K w j)) := by lib_ring
      _ = K.add (pw K w (j + (d + 1))) (K.neg (pw K w j)) := by rw [HasseCM.pw_add' K w j (d + 1)]
      _ = K.add (pw K w j) (K.neg (pw K w j)) := by rw [← e]
      _ = K.zero := by lib_ring
  rcases hK _ _ e2 with e3 | e3
  · exact pw_ne_zero K hK w hw h10 j e3
  · exact hd (by
      calc pw K w (d + 1) = K.add (K.add (pw K w (d + 1)) (K.neg K.one)) K.one := by lib_ring
        _ = K.add K.zero K.one := by rw [e3]
        _ = K.one := by lib_ring)

/-- membership test in a list, by a given Boolean equality (no core List.any lemmas). -/
def memB (beq : R → R → Bool) (t : R) : List R → Bool
  | [] => false
  | x :: l => beq t x || memB beq t l

/-- memB = true gives a witness. -/
theorem memB_true (beq : R → R → Bool) (hb : ∀ a b, beq a b = true → a = b) (t : R) :
    ∀ l : List R, memB beq t l = true → ∃ x, x ∈ l ∧ t = x
  | [], h => Bool.noConfusion h
  | x :: l, h => by
    cases hx : beq t x with
    | true => exact ⟨x, List.Mem.head l, hb t x hx⟩
    | false =>
      have h' : memB beq t l = true := by
        have e : memB beq t (x :: l) = (beq t x || memB beq t l) := rfl
        rw [e, hx] at h
        exact h
      obtain ⟨y, hy, ht⟩ := memB_true beq hb t l h'
      exact ⟨y, List.Mem.tail x hy, ht⟩

/-- memB = false: t differs from every element. -/
theorem memB_false (beq : R → R → Bool) (hb' : ∀ a b, a = b → beq a b = true) (t : R) :
    ∀ l : List R, memB beq t l = false → ∀ x, x ∈ l → t ≠ x
  | [], _, x, hx => nomatch hx
  | y :: l, h, x, hx => by
    cases hy : beq t y with
    | true =>
      have e : memB beq t (y :: l) = (beq t y || memB beq t l) := rfl
      rw [e, hy] at h
      exact Bool.noConfusion h
    | false =>
      have h' : memB beq t l = false := by
        have e : memB beq t (y :: l) = (beq t y || memB beq t l) := rfl
        rw [e, hy] at h
        exact h
      cases hx with
      | head _ => exact fun ht => Bool.noConfusion ((hb' t y ht).symm.trans hy)
      | tail _ hx' => exact memB_false beq hb' t l h' x hx'

end RootSum

#print axioms RootSum.geom
#print axioms RootSum.root_sum
#print axioms RootSum.pw_ne_zero
#print axioms RootSum.pow_ne
#print axioms RootSum.memB_true
#print axioms RootSum.memB_false

lang/RootAll.lean

/-
  lang/RootAll — T49, cycle 827 (general ℓ, step 2): **every n-th root of unity is a power of one of exact order n**,
  in any commutative ring without zero divisors (Quint1.five_cases is the case n = 5):
    Zl, len_Zl, ev_Zl, ev_xn   the coefficient list of xⁿ − 1 (leading first) and its value
    pows, mem_pows             [w^(k−1), …, w¹, w⁰] and membership
    lt_split                   j < k ⇒ k = j + (d + 1) for some d (structural, on Nat.le)
    dist_pows                  exact order n ⇒ the powers w⁰, …, w^(n−1) are distinct
    pw_one, pw_comm            1ʲ = 1, (wʲ)ⁿ = (wⁿ)ʲ
    all_roots                  wⁿ = 1 of exact order n, tⁿ = 1 ⇒ t is not different from every wʲ (j < n)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.RootSum
import lang.RootBound
import lib.RingTac

namespace RootAll

open Frobenius RootBound

variable {R : Type} (K : DetChain.CRing R)

/-- coefficients of x^(m+1) − 1 after the leading one: m zeros and −1. -/
def Zl : Nat → List R
  | 0 => [K.neg K.one]
  | m + 1 => K.zero :: Zl m

theorem len_Zl : ∀ m : Nat, (Zl K m).length = m + 1
  | 0 => rfl
  | m + 1 => by
    show (Zl K m).length + 1 = m + 1 + 1
    rw [len_Zl m]

theorem ev_Zl (x : R) : ∀ m : Nat, evH K x (Zl K m) = K.neg K.one
  | 0 => by
    show K.add (K.mul (K.neg K.one) K.one) K.zero = K.neg K.one
    lib_ring
  | m + 1 => by
    show K.add (K.mul K.zero (pw K x (Zl K m).length)) (evH K x (Zl K m)) = K.neg K.one
    rw [ev_Zl x m]; lib_ring

theorem ev_xn (x : R) (m : Nat) : evH K x (K.one :: Zl K m) = K.add (pw K x (m + 1)) (K.neg K.one) := by
  show K.add (K.mul K.one (pw K x (Zl K m).length)) (evH K x (Zl K m)) = K.add (pw K x (m + 1)) (K.neg K.one)
  rw [len_Zl K m, ev_Zl K x m]; lib_ring

/-- [w^(k−1), …, w⁰]. -/
def pows (w : R) : Nat → List R
  | 0 => []
  | k + 1 => pw K w k :: pows w k

theorem len_pows (w : R) : ∀ k : Nat, (pows K w k).length = k
  | 0 => rfl
  | k + 1 => by
    show (pows K w k).length + 1 = k + 1
    rw [len_pows w k]

theorem mem_pows (w s : R) : ∀ k : Nat, YMChain.memL s (pows K w k) → ∃ j, j < k ∧ s = pw K w j
  | 0, h => nomatch h
  | k + 1, h => by
    cases h with
    | inl e => exact ⟨k, Nat.lt_succ_self k, e⟩
    | inr h' =>
      obtain ⟨j, hj, e⟩ := mem_pows w s k h'
      exact ⟨j, Nat.lt_succ_of_lt hj, e⟩

/-- j < k gives k = j + (d + 1), by recursion on the proof of j + 1 ≤ k. -/
theorem lt_split (j : Nat) : ∀ k : Nat, Nat.le (j + 1) k → ∃ d, k = j + (d + 1)
  | _, Nat.le.refl => ⟨0, rfl⟩
  | _, Nat.le.step h => by
    obtain ⟨d, e⟩ := lt_split j _ h
    exact ⟨d + 1, by rw [e]; rfl⟩

theorem dist_pows (hK : DetChain.NoZD K) (h10 : K.one ≠ K.zero) (w : R) (hw : w ≠ K.zero) (n : Nat)
    (hord : ∀ d, d + 1 < n → pw K w (d + 1) ≠ K.one) : ∀ k : Nat, k ≤ n → Dist (pows K w k)
  | 0, _ => trivial
  | k + 1, hk => by
    refine ⟨fun s hs => ?_, dist_pows hK h10 w hw n hord k (Nat.le_of_succ_le hk)⟩
    obtain ⟨j, hj, e⟩ := mem_pows K w s k hs
    obtain ⟨d, ek⟩ := lt_split j k hj
    have hd : d + 1 < n := Nat.lt_of_le_of_lt (by rw [ek]; exact Nat.le_add_left (d + 1) j) hk
    rw [e, ek]
    exact RootSum.pow_ne K hK w hw h10 j d (hord d hd)

theorem pw_one : ∀ j : Nat, pw K K.one j = K.one
  | 0 => rfl
  | j + 1 => by
    show K.mul (pw K K.one j) K.one = K.one
    rw [pw_one j]; lib_ring

theorem pw_comm (w : R) (n : Nat) : ∀ j : Nat, pw K (pw K w j) n = pw K (pw K w n) j
  | 0 => by
    show pw K K.one n = K.one
    exact pw_one K n
  | j + 1 => by
    show pw K (K.mul (pw K w j) w) n = K.mul (pw K (pw K w n) j) (pw K w n)
    rw [pw_mul K, pw_comm w n j]

/-- **all roots**: if w has exact order m + 1 and t^(m+1) = 1, then t cannot differ from every wʲ, j ≤ m. -/
theorem all_roots (hK : DetChain.NoZD K) (h10 : K.one ≠ K.zero) (w : R) (hw : w ≠ K.zero) (m : Nat)
    (hord : ∀ d, d + 1 < m + 1 → pw K w (d + 1) ≠ K.one) (hn : pw K w (m + 1) = K.one)
    (t : R) (ht : pw K t (m + 1) = K.one) (hnot : ∀ j, j < m + 1 → t ≠ pw K w j) : False := by
  have hd : Dist (t :: pows K w (m + 1)) :=
    ⟨fun s hs => by
      obtain ⟨j, hj, e⟩ := mem_pows K w s (m + 1) hs
      exact fun es => hnot j hj (es.symm.trans e),
     dist_pows K hK h10 w hw (m + 1) hord (m + 1) (Nat.le_refl _)⟩
  have hroot : ∀ r, YMChain.memL r (t :: pows K w (m + 1)) → evH K r (K.one :: Zl K m) = K.zero := by
    intro r hr
    rw [ev_xn K r m]
    cases hr with
    | inl e => rw [e, ht]; lib_ring
    | inr hr' =>
      obtain ⟨j, _, e⟩ := mem_pows K w r (m + 1) hr'
      rw [e, pw_comm K w (m + 1) j, hn, pw_one K j]; lib_ring
  have hl := root_bound K hK (m + 1) K.one (Zl K m) h10 (len_Zl K m) _ hd hroot
  have hlen : (t :: pows K w (m + 1)).length = m + 1 + 1 := by
    show (pows K w (m + 1)).length + 1 = m + 1 + 1
    exact congrArg (· + 1) (len_pows K w (m + 1))
  rw [hlen] at hl
  exact Nat.not_succ_le_self _ hl

/-- (aᵉ)ⁿ = a^(e·n). -/
theorem pw_pw (a : R) (e : Nat) : ∀ n : Nat, pw K (pw K a e) n = pw K a (e * n)
  | 0 => rfl
  | n + 1 => by
    show K.mul (pw K (pw K a e) n) (pw K a e) = pw K a (e * n + e)
    rw [pw_pw a e n, HasseCM.pw_add' K a (e * n) e]

/-- **the power-residue symbol exists**: if a^(e(m+1)) = 1 (e.g. e(m+1) = p − 1, Fermat), then a^e is some wʲ, j ≤ m. -/
theorem symbol_root (hK : DetChain.NoZD K) (h10 : K.one ≠ K.zero) (w : R) (hw : w ≠ K.zero) (m : Nat)
    (hord : ∀ d, d + 1 < m + 1 → pw K w (d + 1) ≠ K.one) (hn : pw K w (m + 1) = K.one)
    (a : R) (e : Nat) (ha : pw K a (e * (m + 1)) = K.one) (hnot : ∀ j, j < m + 1 → pw K a e ≠ pw K w j) : False :=
  all_roots K hK h10 w hw m hord hn (pw K a e) (by rw [pw_pw K a e (m + 1)]; exact ha) hnot

/-- trichotomy on Nat by structural recursion. -/
theorem tri : ∀ j k : Nat, j = k ∨ Nat.le (j + 1) k ∨ Nat.le (k + 1) j
  | 0, 0 => Or.inl rfl
  | 0, k + 1 => Or.inr (Or.inl (Nat.succ_le_succ (Nat.zero_le k)))
  | j + 1, 0 => Or.inr (Or.inr (Nat.succ_le_succ (Nat.zero_le j)))
  | j + 1, k + 1 =>
    match tri j k with
    | Or.inl e => Or.inl (congrArg (· + 1) e)
    | Or.inr (Or.inl h) => Or.inr (Or.inl (Nat.succ_le_succ h))
    | Or.inr (Or.inr h) => Or.inr (Or.inr (Nat.succ_le_succ h))

/-- one direction of uniqueness: j < k < n gives wʲ ≠ wᵏ. -/
theorem lt_ne (hK : DetChain.NoZD K) (h10 : K.one ≠ K.zero) (w : R) (hw : w ≠ K.zero) (n : Nat)
    (hord : ∀ d, d + 1 < n → pw K w (d + 1) ≠ K.one) (j k : Nat) (hjk : Nat.le (j + 1) k) (hk : k < n) :
    pw K w j ≠ pw K w k := by
  obtain ⟨d, ek⟩ := lt_split j k hjk
  have hd : d + 1 < n := Nat.lt_of_le_of_lt (by rw [ek]; exact Nat.le_add_left (d + 1) j) hk
  rw [ek]
  exact RootSum.pow_ne K hK w hw h10 j d (hord d hd)

/-- **the symbol is unique**: wʲ = wᵏ with j, k < n (exact order n) forces j = k. -/
theorem sym_unique (hK : DetChain.NoZD K) (h10 : K.one ≠ K.zero) (w : R) (hw : w ≠ K.zero) (n : Nat)
    (hord : ∀ d, d + 1 < n → pw K w (d + 1) ≠ K.one) (j k : Nat) (hj : j < n) (hk : k < n)
    (e : pw K w j = pw K w k) : j = k :=
  match tri j k with
  | Or.inl h => h
  | Or.inr (Or.inl h) => absurd e (lt_ne K hK h10 w hw n hord j k h hk)
  | Or.inr (Or.inr h) => absurd e.symm (lt_ne K hK h10 w hw n hord k j h hj)

/-- **the symbol is multiplicative**: aᵉ = wⁱ, bᵉ = wʲ ⇒ (ab)ᵉ = w^(i+j). -/
theorem sym_mul (w a b : R) (e i j : Nat) (ha : pw K a e = pw K w i) (hb : pw K b e = pw K w j) :
    pw K (K.mul a b) e = pw K w (i + j) := by
  rw [pw_mul K, ha, hb, HasseCM.pw_add' K w i j]

end RootAll

#print axioms RootAll.len_Zl
#print axioms RootAll.ev_Zl
#print axioms RootAll.ev_xn
#print axioms RootAll.len_pows
#print axioms RootAll.mem_pows
#print axioms RootAll.lt_split
#print axioms RootAll.dist_pows
#print axioms RootAll.pw_one
#print axioms RootAll.pw_comm
#print axioms RootAll.all_roots
#print axioms RootAll.pw_pw
#print axioms RootAll.symbol_root
#print axioms RootAll.tri
#print axioms RootAll.lt_ne
#print axioms RootAll.sym_unique
#print axioms RootAll.sym_mul

#print axioms: 22 theorems, no axioms.

78.360 Stickelberger divisibility in computational form

Over \(\mathbb F_p\) the reduced Jacobi sum vanishes below the critical degree, and \(p\nmid \binom {k+l}{k}\) for \(k+l<p\).

\[ 1\le a,\ a+b\le p-2\ \Rightarrow \ \textstyle \sum _{x\in \mathbb F_p}x^a(1-x)^b=0;\qquad \binom {k+l}{k}\,k!\,l!=(k+l)!. \]

lang/Stick1.lean

/-
  lang/Stick1 — T53, cycle 828 (Stickelberger divisibility, computational form, step 1):
  over 𝔽_p (p = J + 2 prime), **Σ_x (x(1 − x))^m = 0 whenever 1 ≤ m and 2m ≤ p − 2**:
  expand (1 − x)^m = ev(−x, 1, row m) (Frobenius.ev_row) and sum x^(m+i) term by term (HasseCM.ps_low).
  With m = t(p − 1)/ℓ this is J(χ^t, χ^t) ≡ 0 modulo the prime P_t = (p, ζ − r^t) for 2t < ℓ.
    sumK_negK, U, U_cons, U_nil, U_zero, len_row (with Frobenius.len_nxt), stick_zero
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.HasseCM
import lib.RingTac
import lib.Semi

namespace Stick1

open FTAChain ZMod YMChain Frobenius

theorem sumK_negK {R A : Type} (K : DetChain.CRing R) (f : A → R) : ∀ l : List A,
    sumK K (fun a => K.neg (f a)) l = K.neg (sumK K f l)
  | [] => by show K.zero = K.neg K.zero; lib_ring
  | a :: l => by
    show K.add (K.neg (f a)) (sumK K (fun a => K.neg (f a)) l) = K.neg (K.add (f a) (sumK K f l))
    rw [sumK_negK K f l]; lib_ring

theorem len_row : ∀ n, (row n).length = n + 1
  | 0 => rfl
  | n + 1 => (len_nxt 0 (row n)).trans (congrArg Nat.succ (len_row n))

variable (J : Nat)

/-- Σ_x x^e · ev(−x, 1, cs). -/
def U (e : Nat) (cs : List Nat) : Zp (J + 1) :=
  sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x e)
    (ev (ring (J + 1)) ((ring (J + 1)).neg x) (ring (J + 1)).one cs)) (CMInert.allZ (J + 1) (J + 1 + 1))

theorem U_cons (e c : Nat) (cs : List Nat) :
    U J e (c :: cs) = (ring (J + 1)).add
      ((ring (J + 1)).mul (nsm (ring (J + 1)) c (pw (ring (J + 1)) (ring (J + 1)).one cs.length)) (HasseCM.ps J e))
      ((ring (J + 1)).neg (U J (e + 1) cs)) := by
  have pt : ∀ x : Zp (J + 1), (ring (J + 1)).mul (pw (ring (J + 1)) x e)
      (ev (ring (J + 1)) ((ring (J + 1)).neg x) (ring (J + 1)).one (c :: cs)) =
      (ring (J + 1)).add ((ring (J + 1)).mul (nsm (ring (J + 1)) c (pw (ring (J + 1)) (ring (J + 1)).one cs.length)) (pw (ring (J + 1)) x e))
        ((ring (J + 1)).neg ((ring (J + 1)).mul (pw (ring (J + 1)) x (e + 1))
          (ev (ring (J + 1)) ((ring (J + 1)).neg x) (ring (J + 1)).one cs))) := fun x => by
    show (ring (J + 1)).mul (pw (ring (J + 1)) x e)
        ((ring (J + 1)).add (nsm (ring (J + 1)) c (pw (ring (J + 1)) (ring (J + 1)).one cs.length))
          ((ring (J + 1)).mul ((ring (J + 1)).neg x) (ev (ring (J + 1)) ((ring (J + 1)).neg x) (ring (J + 1)).one cs))) =
      (ring (J + 1)).add ((ring (J + 1)).mul (nsm (ring (J + 1)) c (pw (ring (J + 1)) (ring (J + 1)).one cs.length)) (pw (ring (J + 1)) x e))
        ((ring (J + 1)).neg ((ring (J + 1)).mul ((ring (J + 1)).mul (pw (ring (J + 1)) x e) x)
          (ev (ring (J + 1)) ((ring (J + 1)).neg x) (ring (J + 1)).one cs)))
    lib_ring
  show sumK (ring (J + 1)) _ _ = _
  rw [sumK_ext (ring (J + 1)) _ _ pt, sumK_add, ← mul_sumK, sumK_negK]
  rfl

theorem U_nil (e : Nat) : U J e [] = (ring (J + 1)).zero := by
  show sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x e) (ring (J + 1)).zero) _ = _
  rw [sumK_ext (ring (J + 1)) _ (fun _ => (ring (J + 1)).zero) (fun x => DetChain.mul_zero (ring (J + 1)) _), HasseCM.sumK_zero']

theorem U_zero : ∀ (cs : List Nat) (e : Nat), (∀ i, i < cs.length → HasseCM.ps J (e + i) = (ring (J + 1)).zero) →
    U J e cs = (ring (J + 1)).zero
  | [], e, _ => U_nil J e
  | c :: cs, e, h => by
    have h0 : HasseCM.ps J e = (ring (J + 1)).zero := h 0 (Nat.zero_lt_succ _)
    rw [U_cons, h0, U_zero cs (e + 1) (fun i hi => by
      rw [show e + 1 + i = e + (i + 1) by lib_nat]; exact h (i + 1) (Nat.succ_lt_succ hi))]
    lib_ring

/-- **Σ_x (x(1 − x))^m = 0 for 1 ≤ m, 2m ≤ p − 2.** -/
theorem stick_zero (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (m : Nat) (h1 : 1 ≤ m) (h2 : 2 * m ≤ J) :
    sumK (ring (J + 1)) (fun x => pw (ring (J + 1)) ((ring (J + 1)).mul x ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one)) m)
      (CMInert.allZ (J + 1) (J + 1 + 1)) = (ring (J + 1)).zero := by
  have pt : ∀ x : Zp (J + 1),
      pw (ring (J + 1)) ((ring (J + 1)).mul x ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one)) m =
      (ring (J + 1)).mul (pw (ring (J + 1)) x m) (ev (ring (J + 1)) ((ring (J + 1)).neg x) (ring (J + 1)).one (row m)) := fun x => by
    rw [pw_mul, ev_row]
  rw [sumK_ext (ring (J + 1)) _ _ pt]
  show U J m (row m) = _
  apply U_zero
  intro i hi
  rw [len_row] at hi
  exact HasseCM.ps_low J hpr (m + i) (Nat.le_trans h1 (Nat.le_add_right m i))
    (Nat.le_trans (Nat.add_le_add_left (Nat.le_of_lt_succ hi) m) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) h2))

/-- **the Jacobi sum of the 𝔽_p-valued character ψ(x) = x^m vanishes**: Σ_x ψ(x)ψ(1 − x) = 0 for 1 ≤ m, 2m ≤ p − 2
    (with m = t(p − 1)/ℓ, ψ is χ^t reduced modulo P_t, so P_t divides J(χ^t, χ^t) when 2t < ℓ). -/
theorem jac_zero (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (m : Nat) (h1 : 1 ≤ m) (h2 : 2 * m ≤ J) :
    sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x m)
      (pw (ring (J + 1)) ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one) m))
      (CMInert.allZ (J + 1) (J + 1 + 1)) = (ring (J + 1)).zero := by
  rw [sumK_ext (ring (J + 1)) _ _ (fun x => (pw_mul (ring (J + 1)) x _ m).symm)]
  exact stick_zero J hpr m h1 h2

/-- **the general form**: Σ_x x^a (1 − x)^b = 0 for 1 ≤ a, a + b ≤ p − 2 (P_t | J(χ^a', χ^b') in computational form). -/
theorem jac_gen (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (a b : Nat) (h1 : 1 ≤ a) (h2 : a + b ≤ J) :
    sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x a)
      (pw (ring (J + 1)) ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one) b))
      (CMInert.allZ (J + 1) (J + 1 + 1)) = (ring (J + 1)).zero := by
  have pt : ∀ x : Zp (J + 1), (ring (J + 1)).mul (pw (ring (J + 1)) x a)
      (pw (ring (J + 1)) ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one) b) =
      (ring (J + 1)).mul (pw (ring (J + 1)) x a) (ev (ring (J + 1)) ((ring (J + 1)).neg x) (ring (J + 1)).one (row b)) := fun x => by
    rw [ev_row]
  rw [sumK_ext (ring (J + 1)) _ _ pt]
  show U J a (row b) = _
  apply U_zero
  intro i hi
  rw [len_row] at hi
  exact HasseCM.ps_low J hpr (a + i) (Nat.le_trans h1 (Nat.le_add_right a i))
    (Nat.le_trans (Nat.add_le_add_left (Nat.le_of_lt_succ hi) a) h2)

end Stick1

#print axioms Stick1.sumK_negK
#print axioms Stick1.len_row
#print axioms Stick1.U_cons
#print axioms Stick1.U_nil
#print axioms Stick1.U_zero
#print axioms Stick1.stick_zero
#print axioms Stick1.jac_zero
#print axioms Stick1.jac_gen

lang/ChooseFac.lean

/-
  lang/ChooseFac — T53, cycle 828 (Stickelberger converse, step 1): **binomials and factorials, and p ∤ C(b, i) for b < p**:
    fac          n!
    cf           C(k + l, k) · (k! · l!) = (k + l)!
    fac_ndv      p ∤ m! for m < p                         (FTAChain.euclid_core, dv_le)
    C_ndv        p ∤ C(k + l, k) for k + l < p
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.HasseCM
import lib.Semi

namespace ChooseFac

open FTAChain Frobenius

def fac : Nat → Nat
  | 0 => 1
  | n + 1 => (n + 1) * fac n

theorem cf : ∀ n k l : Nat, k + l = n → C (k + l) k * (fac k * fac l) = fac (k + l)
  | _, 0, l, _ => by
    rw [Nat.zero_add, C_zero_right]
    show 1 * (1 * fac l) = fac l
    lib_nat
  | _, k + 1, 0, _ => by
    rw [Nat.add_zero, C_self]
    show 1 * (fac (k + 1) * 1) = fac (k + 1)
    lib_nat
  | 0, k + 1, l + 1, h => by
    have h' : k + 1 + (l + 1) = (k + l + 1) + 1 := by lib_nat
    rw [h'] at h
    exact Nat.noConfusion h
  | n + 1, k + 1, l + 1, h => by
    have hn : k + l + 1 = n := Nat.succ.inj ((show k + l + 1 + 1 = k + 1 + (l + 1) by lib_nat).trans h)
    have i1 : C (k + l + 1) k * (fac k * ((l + 1) * fac l)) = fac (k + l + 1) := by
      have e := cf n k (l + 1) ((show k + (l + 1) = k + l + 1 by lib_nat).trans hn)
      rw [show k + (l + 1) = k + l + 1 by lib_nat] at e
      exact e
    have i2 : C (k + l + 1) (k + 1) * ((k + 1) * fac k * fac l) = fac (k + l + 1) := by
      have e := cf n (k + 1) l ((show k + 1 + l = k + l + 1 by lib_nat).trans hn)
      rw [show k + 1 + l = k + l + 1 by lib_nat] at e
      exact e
    rw [show k + 1 + (l + 1) = (k + l + 1) + 1 by lib_nat]
    show (C (k + l + 1) k + C (k + l + 1) (k + 1)) * ((k + 1) * fac k * ((l + 1) * fac l)) = (k + l + 1 + 1) * fac (k + l + 1)
    calc (C (k + l + 1) k + C (k + l + 1) (k + 1)) * ((k + 1) * fac k * ((l + 1) * fac l))
        = (k + 1) * (C (k + l + 1) k * (fac k * ((l + 1) * fac l))) +
          (l + 1) * (C (k + l + 1) (k + 1) * ((k + 1) * fac k * fac l)) := by lib_nat
      _ = (k + 1) * fac (k + l + 1) + (l + 1) * fac (k + l + 1) := by rw [i1, i2]
      _ = (k + l + 1 + 1) * fac (k + l + 1) := by lib_nat

theorem not_dv_small (j m : Nat) (hm : m + 1 ≤ j + 1) (h : Dv (j + 1 + 1) (m + 1)) : False := by
  obtain ⟨t, ht⟩ := dv_le (j + 1 + 1) m h
  have h2 : j + 1 + 1 ≤ j + 1 := Nat.le_trans (Nat.le_trans (Nat.le_add_right (j + 1 + 1) t) (Nat.le_of_eq ht)) hm
  exact Nat.not_succ_le_self (j + 1) h2

/-- **p ∤ m! for m < p** (p = j + 2 prime). -/
theorem fac_ndv (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) :
    ∀ m : Nat, m ≤ j + 1 → Dv (j + 1 + 1) (fac m) → False
  | 0, _, h => not_dv_small j 0 (Nat.succ_le_succ (Nat.zero_le j)) h
  | m + 1, hm, h =>
    match euclid_core j m (fac m) hpr h with
    | Or.inl h1 => not_dv_small j m hm h1
    | Or.inr h2 => fac_ndv j hpr m (Nat.le_trans (Nat.le_succ m) hm) h2

/-- **p ∤ C(k + l, k) for k + l < p.** -/
theorem C_ndv (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) (k l : Nat) (hkl : k + l ≤ j + 1)
    (h : Dv (j + 1 + 1) (C (k + l) k)) : False := by
  obtain ⟨q, hq⟩ := h
  apply fac_ndv j hpr (k + l) hkl
  refine ⟨q * (fac k * fac l), ?_⟩
  rw [← cf (k + l) k l rfl, hq]
  lib_nat

end ChooseFac

#print axioms ChooseFac.cf
#print axioms ChooseFac.not_dv_small
#print axioms ChooseFac.fac_ndv
#print axioms ChooseFac.C_ndv

#print axioms: 12 theorems, no axioms.

78.361 The norm of a Jacobi sum of any order

For a character \(\psi \) of \(\mathbb F_p^\times \) of any order with \(\psi ,\psi ^2\) nontrivial, in the group ring \(E[C_p]\).

\[ J(\psi ,\psi )\,J(\bar \psi ,\bar \psi )=p. \]

lang/JacNormN.lean

/-
  lang/JacNormN — T54, cycle 828 (step 1): **the Jacobi sum of a character of any order has norm p**:
  for multiplicative ψ : 𝔽_p → E (ψ(1) = 1, ψ(0) = 0) with ψ and ψ² nontrivial (Σψ = Σψ² = Σψ'² = 0, ψ' = ψ∘ι, ι(x) = x^(p−2)),
    J(ψ, ψ) · J(ψ', ψ') = p      provided p − 1 cancels in E (e.g. E = ℤ[ζ_ℓ]).
  As JacobiNorm (ℓ = 3) but for any E: G² = J G_(ψ²) (jacobi_gauss), G G' = ψ'(−1) X (gauss_norm), X² = pX (XX);
  compute (G G')² both ways and read the ζ⁰ coefficient (gt_X).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GaussNormN
import lang.JacobiGauss
import lang.GaussCritN
import lib.RingTac

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

theorem pw_oneJ {T : Type} (K : CRing T) : ∀ n : Nat, pw K K.one n = K.one
  | 0 => rfl
  | n + 1 => by
    show K.mul (pw K K.one n) K.one = K.one
    rw [pw_oneJ K n]; lib_ring

/-- the scalar m0(s) = s ζ⁰ in E[C_p]. -/
abbrev m0 (s : S) : V S (j + 1 + 1 + 1) := mono E (j + 1 + 1) (ring (j + 1 + 1)).zero s

theorem m0_mul (s t : S) : (gring E (j + 1 + 1)).mul (m0 E j s) (m0 E j t) = m0 E j (E.mul s t) := by
  show gmul E (j + 1 + 1) (mono E (j + 1 + 1) _ s) (mono E (j + 1 + 1) _ t) = _
  rw [mono_mul]
  show mono E (j + 1 + 1) ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero (ring (j + 1 + 1)).zero) (E.mul s t) = _
  rw [show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero (ring (j + 1 + 1)).zero = (ring (j + 1 + 1)).zero by lib_ring]

/-- **J(ψ, ψ) J(ψ', ψ') = p.** -/
theorem jac_norm (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero)
    (hs2 : sumK E (fun a => E.mul (ψ a) (ψ a)) (LL j) = E.zero)
    (hs2' : sumK E (fun a => E.mul (ψ (iv j a)) (ψ (iv j a))) (LL j) = E.zero)
    (hcan : ∀ a b, E.mul a (E.add (Pp E j) (E.neg E.one)) = E.mul b (E.add (Pp E j) (E.neg E.one)) → a = b) :
    E.mul (J2 E j ψ ψ) (J2 E j (fun x => ψ (iv j x)) (fun x => ψ (iv j x))) = Pp E j := by
  let R := gring E (j + 1 + 1)
  let ψ' : Zp (j + 1 + 1) → S := fun x => ψ (iv j x)
  let ψ2 : Zp (j + 1 + 1) → S := fun x => E.mul (ψ x) (ψ x)
  let e : S := ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))
  have hm' : ∀ x y, ψ' ((ring (j + 1 + 1)).mul x y) = E.mul (ψ' x) (ψ' y) := fun x y => by
    show ψ (iv j ((ring (j + 1 + 1)).mul x y)) = _; rw [iv_mulmul, hm]
  have h0' : ψ' (ring (j + 1 + 1)).zero = E.zero := by show ψ (iv j _) = _; rw [iv_zero, h0]
  have hm2 : ∀ x y, ψ2 ((ring (j + 1 + 1)).mul x y) = E.mul (ψ2 x) (ψ2 y) := fun x y => by
    show E.mul (ψ _) (ψ _) = E.mul (E.mul (ψ x) (ψ x)) (E.mul (ψ y) (ψ y)); rw [hm]; lib_ring
  have h12 : ψ2 (ring (j + 1 + 1)).one = E.one := by show E.mul (ψ _) (ψ _) = _; rw [h1]; lib_ring
  have h02 : ψ2 (ring (j + 1 + 1)).zero = E.zero := by show E.mul (ψ _) (ψ _) = _; rw [h0]; lib_ring
  -- e² = 1
  have hee : E.mul e e = E.one := by
    show E.mul (ψ (iv j _)) (ψ (iv j _)) = _
    rw [← hm, ← iv_mulmul, show (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)
      ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).one by lib_ring]
    show ψ (pw (ring (j + 1 + 1)) (ring (j + 1 + 1)).one (j + 1)) = _
    rw [pw_oneJ, h1]
  -- the four group-ring identities
  have hA : R.mul (Gv j ψ) (Gv j ψ) = R.mul (m0 E j (J2 E j ψ ψ)) (Gv j ψ2) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j ψ) (Gv j ψ)) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j _) (Gv j ψ2)) c
    rw [jacobi_gauss E j hpr ψ ψ hm hm h0 hs2 c, gt_mono0]
    unfold Gv; rw [gt_tb])
  have hB : R.mul (Gv j ψ') (Gv j ψ') = R.mul (m0 E j (J2 E j ψ' ψ')) (Gv j (fun x => ψ2 (iv j x))) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j ψ') (Gv j ψ')) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j _) (Gv j _)) c
    rw [jacobi_gauss E j hpr ψ' ψ' hm' hm' h0' hs2' c, gt_mono0]
    unfold Gv; rw [gt_tb])
  have hC : R.mul (Gv j ψ) (Gv j ψ') = R.mul (m0 E j e) (Xv E j) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j ψ) (Gv j (fun x => ψ (iv j x)))) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j e) (Xv E j)) c
    rw [gauss_norm E j hpr ψ hm h1 h0 hs c, gt_mono0])
  have hD : R.mul (Gv j ψ2) (Gv j (fun x => ψ2 (iv j x))) = R.mul (m0 E j (E.mul e e)) (Xv E j) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j ψ2) (Gv j (fun x => ψ2 (iv j x)))) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j _) (Xv E j)) c
    rw [gauss_norm E j hpr ψ2 hm2 h12 h02 hs2 c, gt_mono0])
  have hX : R.mul (Xv E j) (Xv E j) = R.mul (m0 E j (Pp E j)) (Xv E j) := XX E j
  -- (G G')² two ways
  have lhs : R.mul (R.mul (Gv j ψ) (Gv j ψ')) (R.mul (Gv j ψ) (Gv j ψ')) = R.mul (m0 E j (Pp E j)) (Xv E j) := by
    rw [hC]
    calc R.mul (R.mul (m0 E j e) (Xv E j)) (R.mul (m0 E j e) (Xv E j))
        = R.mul (R.mul (m0 E j e) (m0 E j e)) (R.mul (Xv E j) (Xv E j)) := by lib_ring
      _ = R.mul (m0 E j E.one) (R.mul (m0 E j (Pp E j)) (Xv E j)) := by rw [m0_mul, hee, hX]
      _ = R.mul (R.mul (m0 E j E.one) (m0 E j (Pp E j))) (Xv E j) := by lib_ring
      _ = R.mul (m0 E j (Pp E j)) (Xv E j) := by rw [m0_mul, show E.mul E.one (Pp E j) = Pp E j by lib_ring]
  have rhs : R.mul (R.mul (Gv j ψ) (Gv j ψ')) (R.mul (Gv j ψ) (Gv j ψ')) =
      R.mul (m0 E j (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ'))) (Xv E j) := by
    calc R.mul (R.mul (Gv j ψ) (Gv j ψ')) (R.mul (Gv j ψ) (Gv j ψ'))
        = R.mul (R.mul (Gv j ψ) (Gv j ψ)) (R.mul (Gv j ψ') (Gv j ψ')) := by lib_ring
      _ = R.mul (R.mul (m0 E j (J2 E j ψ ψ)) (Gv j ψ2)) (R.mul (m0 E j (J2 E j ψ' ψ')) (Gv j (fun x => ψ2 (iv j x)))) := by rw [hA, hB]
      _ = R.mul (R.mul (m0 E j (J2 E j ψ ψ)) (m0 E j (J2 E j ψ' ψ'))) (R.mul (Gv j ψ2) (Gv j (fun x => ψ2 (iv j x)))) := by lib_ring
      _ = R.mul (m0 E j (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ'))) (R.mul (m0 E j E.one) (Xv E j)) := by rw [m0_mul, hD, hee]
      _ = R.mul (R.mul (m0 E j (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ'))) (m0 E j E.one)) (Xv E j) := by lib_ring
      _ = _ := by rw [m0_mul, show E.mul (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ')) E.one = E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ') by lib_ring]
  -- ζ⁰ coefficient: P·(P − 1) = J J'·(P − 1)
  have h00 : gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j (Pp E j)) (Xv E j)) (ring (j + 1 + 1)).zero =
      gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ'))) (Xv E j)) (ring (j + 1 + 1)).zero :=
    congrArg (fun u => gt E (j + 1 + 1) u (ring (j + 1 + 1)).zero) (lhs.symm.trans rhs)
  rw [gt_mono0, gt_mono0, gt_X] at h00
  have hd : delta E (ring (j + 1 + 1)).zero (ring (j + 1 + 1)).zero = E.one := by
    unfold delta; rw [ite_pos' _ _ rfl]
  rw [hd] at h00
  apply Eq.symm
  apply hcan
  calc E.mul (Pp E j) (E.add (Pp E j) (E.neg E.one))
      = E.mul (Pp E j) (E.add (E.mul (Pp E j) E.one) (E.neg E.one)) := by lib_ring
    _ = E.mul (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ')) (E.add (E.mul (Pp E j) E.one) (E.neg E.one)) := h00
    _ = E.mul (E.mul (J2 E j ψ ψ) (J2 E j ψ' ψ')) (E.add (Pp E j) (E.neg E.one)) := by lib_ring

end CharSumGen

#print axioms CharSumGen.pw_oneJ
#print axioms CharSumGen.m0_mul
#print axioms CharSumGen.jac_norm

#print axioms: 3 theorems, no axioms.

78.362 Order one at a prime above \(p\) and the maps to \(\mathbb Z/p^2\)

From \(JJ'=p\) and \(\iota _2(p)\ne 0\) in \(\mathbb Z/p^2\) the image of \(J\) is nonzero; evaluation of \(K[C_{m+1}]\) at a root of unity, the Teichmüller lift and the reduction \(\mathbb Z/p^2\to \mathbb F_p\) are ring maps.

\[ P^2=0\ \Rightarrow \ (1+Pt)^p=1;\qquad \mathrm {ev}_z(uv)=\mathrm {ev}_z(u)\,\mathrm {ev}_z(v)\ \ (z^{m+1}=1). \]

lang/StickVal.lean

/-
  lang/StickVal — T54, cycle 828 (step 2): **the P_t-order of J is exactly one** (when P_t | J), in computational form.
  R ∋ J, J', with J·J' = p (JacNormN.jac_norm); ι₂ : R → A a ring map (A = ℤ/p², ζ ↦ the Teichmüller root), red : A → 𝔽_p
  the reduction, ι = red ∘ ι₂ (the map to R/P_t):
    val_one   ι(J) = 0 and p ≠ 0 in A  ⇒  red(ι₂ J) = 0 and ι₂ J ≠ 0     (J ∈ P_t, J ∉ P_t²)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.HasseCM

namespace StickVal

variable {R A F : Type} (T : DetChain.CRing R) (Ar : DetChain.CRing A) (K : DetChain.CRing F)

/-- **J ∈ P_t, J ∉ P_t²**: if J J' = p, ι₂ is multiplicative, ι₂(p) ≠ 0 in A, and red(ι₂ J) = ι(J) = 0. -/
theorem val_one (ι₂ : R → A) (red : A → F) (hmul : ∀ a b, ι₂ (T.mul a b) = Ar.mul (ι₂ a) (ι₂ b))
    (hz : ∀ b, Ar.mul Ar.zero b = Ar.zero) (J J' P : R) (hJ : T.mul J J' = P) (hP : ι₂ P ≠ Ar.zero)
    (hdiv : red (ι₂ J) = K.zero) : red (ι₂ J) = K.zero ∧ ι₂ J ≠ Ar.zero :=
  ⟨hdiv, fun h0 => hP (by rw [← hJ, hmul, h0, hz])⟩

/-- **p ≠ 0 in ℤ/p²** (any q + 1 below the modulus is nonzero): ZMod.nsm_one_val, md_small. -/
theorem small_ne_zero (M q : Nat) (hq : q + 1 < M + 1) :
    Frobenius.nsm (ZMod.ring M) (q + 1) (ZMod.ring M).one ≠ (ZMod.ring M).zero := fun h => by
  have h1 := congrArg Subtype.val h
  rw [ZMod.nsm_one_val, ZMod.md_small M _ hq] at h1
  have h2 : q + 1 = 0 := h1
  nomatch h2

end StickVal

#print axioms StickVal.val_one
#print axioms StickVal.small_ne_zero

lang/GEval.lean

/-
  lang/GEval — T54, cycle 828 (step 3): **evaluation of the group ring K[C_(m+1)] at an (m+1)-th root of unity is a ring map**:
    gev z u = Σ_a u(a) z^a       (z^(m+1) = 1)
    pw_md       z^(n mod (m+1)) = z^n
    pw_val_add  z^((a + b).val) = z^a z^b
    gev_add, gev_mul, gev_mono
  With K = ℤ/p², z = the Teichmüller root of order ℓ, and ℤ[C_ℓ] → (ℤ/p²)[C_ℓ] coefficientwise, this is the map ι₂ of StickVal.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GroupRingFrob
import lang.HasseCM
import lib.RingTac
import lib.Semi

namespace GEval

open FTAChain ZMod YMChain Frobenius GroupRing GroupRingFrob

variable {R : Type} (K : DetChain.CRing R) (m : Nat)

def gev (z : R) (u : V R (m + 1)) : R := sumK K (fun a => K.mul (gt K m u a) (pw K z a.val)) (L m)

theorem pw_mulq (z : R) (hz : pw K z (m + 1) = K.one) (r : Nat) : ∀ q : Nat, pw K z (r + q * (m + 1)) = pw K z r
  | 0 => by rw [Nat.zero_mul, Nat.add_zero]
  | q + 1 => by
    rw [show r + (q + 1) * (m + 1) = (r + q * (m + 1)) + (m + 1) by lib_nat, HasseCM.pw_add' K z _ (m + 1), hz, pw_mulq z hz r q]
    lib_ring

theorem pw_md (z : R) (hz : pw K z (m + 1) = K.one) (n : Nat) : pw K z (md m n) = pw K z n := by
  obtain ⟨q, hq⟩ := md_spec m n
  calc pw K z (md m n) = pw K z (md m n + q * (m + 1)) := (pw_mulq K m z hz (md m n) q).symm
    _ = pw K z n := by rw [← hq]

theorem pw_val_add (z : R) (hz : pw K z (m + 1) = K.one) (a b : Zp m) :
    pw K z ((ring m).add a b).val = K.mul (pw K z a.val) (pw K z b.val) := by
  show pw K z (md m (a.val + b.val)) = _
  rw [pw_md K m z hz, HasseCM.pw_add']

/-- **gev is multiplicative.** -/
theorem gev_mul (z : R) (hz : pw K z (m + 1) = K.one) (u v : V R (m + 1)) :
    gev K m z (gmul K m u v) = K.mul (gev K m z u) (gev K m z v) := by
  unfold gev
  have h1 : ∀ c : Zp m, K.mul (gt K m (gmul K m u v) c) (pw K z c.val) =
      sumK K (fun a => K.mul (gt K m u a) (K.mul (gt K m v ((ring m).add c ((ring m).neg a))) (pw K z c.val))) (L m) := fun c => by
    rw [gt_mul, sumK_mul]
    exact sumK_ext K _ _ (fun a => by lib_ring) _
  rw [sumK_ext K _ _ h1, sumK_swap, sumK_mul]
  apply sumK_ext
  intro a
  rw [← mul_sumK]
  have h2 : sumK K (fun c => K.mul (gt K m v ((ring m).add c ((ring m).neg a))) (pw K z c.val)) (L m) =
      K.mul (sumK K (fun b => K.mul (gt K m v b) (pw K z b.val)) (L m)) (pw K z a.val) := by
    rw [← shift K m (fun c => K.mul (gt K m v ((ring m).add c ((ring m).neg a))) (pw K z c.val)) a, sumK_mul]
    apply sumK_ext
    intro b
    show K.mul (gt K m v ((ring m).add ((ring m).add b a) ((ring m).neg a))) (pw K z ((ring m).add b a).val) = _
    rw [show (ring m).add ((ring m).add b a) ((ring m).neg a) = b by lib_ring, pw_val_add K m z hz]
    lib_ring
  rw [h2]
  lib_ring

theorem gev_add (z : R) (u v : V R (m + 1)) : gev K m z (gadd K m u v) = K.add (gev K m z u) (gev K m z v) := by
  unfold gev
  rw [← sumK_add]
  apply sumK_ext
  intro a
  unfold gadd
  rw [gt_tb]
  lib_ring

theorem map_sumK {S A : Type} (Ks : DetChain.CRing S) (f : R → S) (hadd : ∀ a b, f (K.add a b) = Ks.add (f a) (f b))
    (hzero : f K.zero = Ks.zero) (g : A → R) : ∀ l : List A, f (sumK K g l) = sumK Ks (fun a => f (g a)) l
  | [] => hzero
  | a :: l => by
    show f (K.add (g a) (sumK K g l)) = Ks.add (f (g a)) (sumK Ks (fun a => f (g a)) l)
    rw [hadd, map_sumK Ks f hadd hzero g l]

/-- **change of coefficients** along a ring map f : R → S, coefficientwise. -/
def gmap {S : Type} (f : R → S) (u : V R (m + 1)) : V S (m + 1) := tb m (fun a => f (gt K m u a))

theorem gmap_mul {S : Type} (Ks : DetChain.CRing S) (f : R → S) (hadd : ∀ a b, f (K.add a b) = Ks.add (f a) (f b))
    (hmul : ∀ a b, f (K.mul a b) = Ks.mul (f a) (f b)) (hzero : f K.zero = Ks.zero) (u v : V R (m + 1)) :
    gmap K m f (gmul K m u v) = gmul Ks m (gmap K m f u) (gmap K m f v) := ext Ks m _ _ (fun c => by
  unfold gmap
  rw [gt_tb, gt_mul, gt_mul, map_sumK K Ks f hadd hzero]
  apply sumK_ext
  intro a
  rw [hmul, gt_tb, gt_tb])

theorem gmap_add {S : Type} (Ks : DetChain.CRing S) (f : R → S) (hadd : ∀ a b, f (K.add a b) = Ks.add (f a) (f b))
    (u v : V R (m + 1)) : gmap K m f (gadd K m u v) = gadd Ks m (gmap K m f u) (gmap K m f v) := ext Ks m _ _ (fun c => by
  unfold gmap gadd
  repeat rw [gt_tb]
  exact hadd _ _)

end GEval

#print axioms GEval.pw_mulq
#print axioms GEval.pw_md
#print axioms GEval.pw_val_add
#print axioms GEval.gev_mul
#print axioms GEval.map_sumK
#print axioms GEval.gev_add
#print axioms GEval.gmap_mul
#print axioms GEval.gmap_add

lang/Teich.lean

/-
  lang/Teich — T54, cycle 828 (step 4): **the Teichmüller root modulo p²**, by the square-zero binomial:
    sq0_pow     x² = 0 ⇒ (1 + x)^n = 1 + n·x
    teich       P² = 0 (P = p·1, e.g. in ℤ/p²) ⇒ (1 + P t)^p = 1
  So if r^ℓ = 1 + P t (r^ℓ ≡ 1 mod p), z = r^p has z^ℓ = (r^ℓ)^p = 1 in ℤ/p², and z ≡ r mod p (Fermat): z is the root
  that GEval evaluates at (ι₂(ζ) = z, red(z) = r^t's base).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GEval
import lib.RingTac

namespace Teich

open Frobenius

variable {A : Type} (K : DetChain.CRing A)

theorem nsm_mulx (n : Nat) (a b : A) : K.mul (nsm K n a) b = nsm K n (K.mul a b) := by
  induction n with
  | zero => show K.mul K.zero b = K.zero; lib_ring
  | succ n ih =>
    show K.mul (K.add (nsm K n a) a) b = K.add (nsm K n (K.mul a b)) (K.mul a b)
    rw [← ih]; lib_ring

theorem nsm_zero (n : Nat) : nsm K n K.zero = K.zero := by
  induction n with
  | zero => rfl
  | succ n ih => show K.add (nsm K n K.zero) K.zero = K.zero; rw [ih]; lib_ring

theorem nsm_one_mul (n : Nat) (x : A) : nsm K n x = K.mul (nsm K n K.one) x := by
  rw [nsm_mulx]
  show nsm K n x = nsm K n (K.mul K.one x)
  rw [show K.mul K.one x = x by lib_ring]

theorem sq0_pow (x : A) (hx : K.mul x x = K.zero) : ∀ n : Nat, pw K (K.add K.one x) n = K.add K.one (nsm K n x)
  | 0 => by show K.one = K.add K.one K.zero; lib_ring
  | n + 1 => by
    show K.mul (pw K (K.add K.one x) n) (K.add K.one x) = K.add K.one (K.add (nsm K n x) x)
    rw [sq0_pow x hx n]
    calc K.mul (K.add K.one (nsm K n x)) (K.add K.one x)
        = K.add (K.add K.one (K.add (nsm K n x) x)) (K.mul (nsm K n x) x) := by lib_ring
      _ = K.add (K.add K.one (K.add (nsm K n x) x)) (nsm K n (K.mul x x)) := by rw [nsm_mulx K n x x]
      _ = _ := by rw [hx, nsm_zero K n]; lib_ring

/-- **(1 + P t)^p = 1 when P = p·1 and P² = 0.** -/
theorem teich (p : Nat) (t : A) (hP : K.mul (nsm K p K.one) (nsm K p K.one) = K.zero) :
    pw K (K.add K.one (K.mul (nsm K p K.one) t)) p = K.one := by
  have hx : K.mul (K.mul (nsm K p K.one) t) (K.mul (nsm K p K.one) t) = K.zero := by
    calc K.mul (K.mul (nsm K p K.one) t) (K.mul (nsm K p K.one) t)
        = K.mul (K.mul (nsm K p K.one) (nsm K p K.one)) (K.mul t t) := by lib_ring
      _ = K.zero := by rw [hP]; lib_ring
  rw [sq0_pow K _ hx p, nsm_one_mul K p (K.mul (nsm K p K.one) t)]
  calc K.add K.one (K.mul (nsm K p K.one) (K.mul (nsm K p K.one) t))
      = K.add K.one (K.mul (K.mul (nsm K p K.one) (nsm K p K.one)) t) := by lib_ring
    _ = K.one := by rw [hP]; lib_ring

end Teich

#print axioms Teich.nsm_mulx
#print axioms Teich.nsm_zero
#print axioms Teich.nsm_one_mul
#print axioms Teich.sq0_pow
#print axioms Teich.teich

lang/RedP2.lean

/-
  lang/RedP2 — T54, cycle 828 (step 5): **reduction ℤ/(M + 1) → ℤ/p when p | M + 1** (e.g. ℤ/p² → 𝔽_p) is a ring map:
    mdmd                 (a mod (M + 1)) mod p = a mod p
    red, red_add, red_mul, red_zero, red_one
  (with GEval/Teich this gives ι = red ∘ ι₂, the map to R/P_t used by StickVal).
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Teich
import lib.Semi

namespace RedP2

open ZMod

variable (J M k : Nat) (hk : M + 1 = (J + 1 + 1) * k)

include hk in
theorem mdmd (a : Nat) : md (J + 1) (md M a) = md (J + 1) a := by
  obtain ⟨q, hq⟩ := md_spec M a
  calc md (J + 1) (md M a) = md (J + 1) (md M a + (q * k) * (J + 1 + 1)) := (md_add_mul (J + 1) (md M a) (q * k)).symm
    _ = md (J + 1) (md M a + q * (M + 1)) := by rw [hk, show q * k * (J + 1 + 1) = q * ((J + 1 + 1) * k) by lib_nat]
    _ = md (J + 1) a := by rw [← hq]

/-- the reduction map. -/
def red (x : Zp M) : Zp (J + 1) := mk (J + 1) x.val

include hk in
theorem red_add (x y : Zp M) : red J M ((ring M).add x y) = (ring (J + 1)).add (red J M x) (red J M y) := Subtype.ext (by
  show md (J + 1) (md M (x.val + y.val)) = md (J + 1) (md (J + 1) x.val + md (J + 1) y.val)
  rw [mdmd J M k hk, ← md_add2])

include hk in
theorem red_mul (x y : Zp M) : red J M ((ring M).mul x y) = (ring (J + 1)).mul (red J M x) (red J M y) := Subtype.ext (by
  show md (J + 1) (md M (x.val * y.val)) = md (J + 1) (md (J + 1) x.val * md (J + 1) y.val)
  rw [mdmd J M k hk, ← md_mul2])

include hk in
theorem red_zero : red J M (ring M).zero = (ring (J + 1)).zero := Subtype.ext (by
  show md (J + 1) (md M 0) = md (J + 1) 0
  rw [mdmd J M k hk])

include hk in
theorem red_one : red J M (ring M).one = (ring (J + 1)).one := Subtype.ext (by
  show md (J + 1) (md M 1) = md (J + 1) 1
  rw [mdmd J M k hk])

end RedP2

#print axioms RedP2.mdmd
#print axioms RedP2.red_add
#print axioms RedP2.red_mul
#print axioms RedP2.red_zero
#print axioms RedP2.red_one

#print axioms: 20 theorems, no axioms.

78.363 Counting for the weight-one factor of 27a

Each representation of an odd \(n\) by \(x^2+27y^2\) (\(x\equiv 1\) mod \(3\)) or by \(4x^2+2xy+7y^2\) (\(x=3b-5e\), \(y=2e+1\)) comes from exactly one \((j,k)\).

\[ 4n=(2+3j+9k)^2+27(k-j)^2,\qquad n=1+3j(3j+1)+9k(3k+1). \]

lang/Bij27w.lean

/-
  lang/Bij27w — T47, cycle 826 (27a weight-one factor, the reindexing step): the exponent of the double pentagonal sum
  (lang/Eta27wC, 2E(m) = 3j² + j) splits by the parity of k − j into the two forms of discriminant −108:
    rot27      4(1 + 9j² + 3j + 27k² + 9k) = (2 + 3j + 9k)² + 27(k − j)²
    even_rep   k = j + 2e:      1 + 9j² + 3j + 27k² + 9k = x² + 27y²,          x = 1 + 6j + 9e ≡ 1 (mod 3), y = e
    odd_rep    k = j + 2e + 1:  1 + 9j² + 3j + 27k² + 9k = 4x² + 2xy + 7y²,    x = −3 − 3j − 5e, y = 2e + 1
  (cycle 826: every representation is hit exactly once with these normalisations — the counting step is the next file.)
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lib.IntRing
import lang.GaussInt
import lang.ColCert10

namespace Bij27w

/-- the unified identity. -/
theorem rot27 (j k : Int) :
    4 * (1 + 9 * (j * j) + 3 * j + 27 * (k * k) + 9 * k) = (2 + 3 * j + 9 * k) * (2 + 3 * j + 9 * k) + 27 * ((k - j) * (k - j)) := by
  lib_int

/-- **k − j even: the form x² + 27y²**, x = 1 + 6j + 9e ≡ 1 (mod 3). -/
theorem even_rep (j e : Int) :
    1 + 9 * (j * j) + 3 * j + 27 * ((j + 2 * e) * (j + 2 * e)) + 9 * (j + 2 * e) =
      (1 + 6 * j + 9 * e) * (1 + 6 * j + 9 * e) + 27 * (e * e) := by
  lib_int

/-- the even-case x is ≡ 1 (mod 3). -/
theorem even_x_mod3 (j e : Int) : 1 + 6 * j + 9 * e = 3 * (2 * j + 3 * e) + 1 := by lib_int

/-- **k − j odd: the form 4x² + 2xy + 7y²**, x = −3 − 3j − 5e, y = 2e + 1. -/
theorem odd_rep (j e : Int) :
    1 + 9 * (j * j) + 3 * j + 27 * ((j + 2 * e + 1) * (j + 2 * e + 1)) + 9 * (j + 2 * e + 1) =
      4 * ((-3 - 3 * j - 5 * e) * (-3 - 3 * j - 5 * e)) + 2 * ((-3 - 3 * j - 5 * e) * (2 * e + 1)) + 7 * ((2 * e + 1) * (2 * e + 1)) := by
  lib_int

/-- **inverse of the even case**: x = 3a + 1 with a + y even (a + y = 2d) is hit by j = d − 2y, e = y. -/
theorem even_inv (a y d : Int) (h : a + y = 2 * d) : 3 * a + 1 = 1 + 6 * (d - 2 * y) + 9 * y := by
  have ha : a = 2 * d - y := by
    calc a = (a + y) - y := by lib_int
      _ = 2 * d - y := by rw [h]
  rw [ha]
  lib_int

/-- **inverse of the odd case**: y = 2e + 1 and x = 3b − 5e is hit by j = −1 − b. -/
theorem odd_inv (b e : Int) : 3 * b - 5 * e = -3 - 3 * (-1 - b) - 5 * e := by lib_int

/-- **x² + 27y² odd ⇒ x + y odd** (so a + y is even when x = 3a + 1: the even case always has a preimage). -/
theorem odd27 (s : Nat) (x y : Int) (h : x * x + 27 * (y * y) = Int.ofNat (2 * s + 1)) : ∃ r, x + y = 2 * r + 1 := by
  cases GaussInt.int_even_odd (x + y) with
  | inr h' => exact h'
  | inl h' =>
    obtain ⟨e, he⟩ := h'
    exfalso
    apply GaussInt.int_odd_ne_even (Int.ofNat s) (2 * (e * e) - 2 * (e * y) + 14 * (y * y))
    have hx : x = 2 * e - y := by
      calc x = (x + y) - y := by lib_int
        _ = 2 * e - y := by rw [he]
    rw [hx] at h
    calc 2 * Int.ofNat s + 1 = Int.ofNat (2 * s + 1) := rfl
      _ = (2 * e - y) * (2 * e - y) + 27 * (y * y) := h.symm
      _ = _ := by lib_int

/-- **4x² + 2xy + 7y² odd ⇒ y odd** (y = 2e + 1: the odd case's parametrisation). -/
theorem odd7 (s : Nat) (x y : Int) (h : 4 * (x * x) + 2 * (x * y) + 7 * (y * y) = Int.ofNat (2 * s + 1)) : ∃ e, y = 2 * e + 1 := by
  cases GaussInt.int_even_odd y with
  | inr h' => exact h'
  | inl h' =>
    obtain ⟨e, he⟩ := h'
    exfalso
    apply GaussInt.int_odd_ne_even (Int.ofNat s) (2 * (x * x) + 2 * (x * e) + 14 * (e * e))
    rw [he] at h
    calc 2 * Int.ofNat s + 1 = Int.ofNat (2 * s + 1) := rfl
      _ = 4 * (x * x) + 2 * (x * (2 * e)) + 7 * ((2 * e) * (2 * e)) := h.symm
      _ = _ := by lib_int

/-- **3c ≠ 1** for integers (by the constructors of Int; no order lemmas). -/
theorem three_ne_one (c : Int) (h : 3 * c = 1) : False := by
  cases c with
  | ofNat m =>
    cases m with
    | zero =>
      have h0 : (0 : Nat) = 1 := Int.ofNat.inj (show Int.ofNat 0 = Int.ofNat 1 from h)
      nomatch h0
    | succ m =>
      have h' : 3 * (m + 1) = 1 := Int.ofNat.inj (show Int.ofNat (3 * (m + 1)) = Int.ofNat 1 from h)
      have h2 : 3 * m + 2 = 0 := Nat.succ.inj (show 3 * m + 2 + 1 = 0 + 1 from h')
      nomatch h2
  | negSucc m => exact Int.noConfusion (show Int.negSucc (3 * m + 2) = Int.ofNat 1 from h)

/-- the form splits as a square plus 3 times a form: 4x² + 2xy + 7y² = (x + y)² + 3(x² + 2y²). -/
theorem split3 (x y : Int) : 4 * (x * x) + 2 * (x * y) + 7 * (y * y) = (x + y) * (x + y) + 3 * (x * x + 2 * (y * y)) := by
  lib_int

/-- **the ± choice in the odd case**: if y = 2e + 1 and 4x² + 2xy + 7y² = 3t + 1, then x = 3b − 5e for some b, or
    −x = 3b' − 5e' with −y = 2e' + 1 (e' = −e − 1) — exactly the pairs hit by odd_inv. -/
theorem odd_sel (x e t : Int) (h : 4 * (x * x) + 2 * (x * (2 * e + 1)) + 7 * ((2 * e + 1) * (2 * e + 1)) = 3 * t + 1) :
    (∃ b, x = 3 * b - 5 * e) ∨ (∃ b, -x = 3 * b - 5 * (-e - 1)) := by
  obtain ⟨q, r, s, hrs, hq⟩ := ColCert10.ediv 2 (x + 5 * e)
  cases r with
  | zero =>
    refine Or.inl ⟨q, ?_⟩
    calc x = (x + 5 * e) - 5 * e := by lib_int
      _ = ((0 : Nat) : Int) + ((2 + 1 : Nat) : Int) * q - 5 * e := by rw [hq]
      _ = 3 * q - 5 * e := by show (0 : Int) + 3 * q - 5 * e = _; lib_int
  | succ r =>
    cases r with
    | zero =>
      refine Or.inr ⟨-2 - q, ?_⟩
      calc -x = -((x + 5 * e) - 5 * e) := by lib_int
        _ = -(((0 + 1 : Nat) : Int) + ((2 + 1 : Nat) : Int) * q - 5 * e) := by rw [hq]
        _ = 3 * (-2 - q) - 5 * (-e - 1) := by show -((1 : Int) + 3 * q - 5 * e) = _; lib_int
    | succ r =>
      exfalso
      have hr : r = 0 := by
        cases r with
        | zero => rfl
        | succ r' =>
          have h2 : s + (r' + 1) = 0 :=
            Nat.succ.inj (Nat.succ.inj ((show (s + (r' + 1)) + 1 + 1 = r' + 1 + 1 + 1 + s by lib_nat).trans hrs))
          nomatch h2
      rw [hr] at hq
      have hxy : x + (2 * e + 1) = 3 * (1 + q - e) := by
        calc x + (2 * e + 1) = (x + 5 * e) - 3 * e + 1 := by lib_int
          _ = ((0 + 1 + 1 : Nat) : Int) + ((2 + 1 : Nat) : Int) * q - 3 * e + 1 := by rw [hq]
          _ = 3 * (1 + q - e) := by show (2 : Int) + 3 * q - 3 * e + 1 = _; lib_int
      have hs := split3 x (2 * e + 1)
      rw [hxy, h] at hs
      apply three_ne_one ((3 * ((1 + q - e) * (1 + q - e)) + (x * x + 2 * ((2 * e + 1) * (2 * e + 1)))) - t)
      calc 3 * ((3 * ((1 + q - e) * (1 + q - e)) + (x * x + 2 * ((2 * e + 1) * (2 * e + 1)))) - t)
          = (3 * (1 + q - e) * (3 * (1 + q - e)) + 3 * (x * x + 2 * ((2 * e + 1) * (2 * e + 1)))) - (3 * t + 1) + 1 := by lib_int
        _ = 1 := by rw [← hs]; lib_int

/-- **12c = 0 ⇒ c = 0** for integers (constructors, no order lemmas). -/
theorem twelve_cancel (c : Int) (h : 12 * c = 0) : c = 0 := by
  cases c with
  | ofNat m =>
    cases m with
    | zero => rfl
    | succ m =>
      have h' : 12 * (m + 1) = 0 := Int.ofNat.inj (show Int.ofNat (12 * (m + 1)) = Int.ofNat 0 from h)
      have h2 : 12 * m + 11 + 1 = 0 := h'
      nomatch h2
  | negSucc m => exact Int.noConfusion (show Int.negSucc (12 * m + 11) = Int.ofNat 0 from h)

/-- **existence (even case)**: if a + y = 2d, then (j, k) = (d − 2y, d) satisfies 2 + 3j + 9k = 2(3a + 1) and k − j = 2y. -/
theorem ex_even (a y d : Int) (h : a + y = 2 * d) :
    2 + 3 * (d - 2 * y) + 9 * d = 2 * (3 * a + 1) ∧ d - (d - 2 * y) = 2 * y := by
  have ha : a = 2 * d - y := by
    calc a = (a + y) - y := by lib_int
      _ = 2 * d - y := by rw [h]
  rw [ha]
  exact ⟨by lib_int, by lib_int⟩

/-- **uniqueness**: the linear system 2 + 3j + 9k = X, k − j = Y has at most one solution. -/
theorem uniq (j k j' k' : Int) (h1 : 3 * j + 9 * k = 3 * j' + 9 * k') (h2 : k - j = k' - j') : j = j' ∧ k = k' := by
  have hk : k - k' = 0 := by
    apply twelve_cancel
    calc 12 * (k - k') = (3 * j + 9 * k) - (3 * j' + 9 * k') + 3 * ((k - j) - (k' - j')) := by lib_int
      _ = 0 := by rw [h1, h2]; lib_int
  have hj : j - j' = 0 := by
    calc j - j' = (k - k') - ((k - j) - (k' - j')) := by lib_int
      _ = 0 := by rw [hk, h2]; lib_int
  exact ⟨by calc j = (j - j') + j' := by lib_int
              _ = j' := by rw [hj]; lib_int,
         by calc k = (k - k') + k' := by lib_int
              _ = k' := by rw [hk]; lib_int⟩

/-- **existence (odd case)**: for y = 2e + 1, x = 3b − 5e, (j, k) = (−1 − b, −b + 2e) satisfies k − j = y and
    4x + y = −(2 + 3j + 9k) (uniqueness: uniq). -/
theorem ex_odd (b e : Int) :
    (-b + 2 * e) - (-1 - b) = 2 * e + 1 ∧ 4 * (3 * b - 5 * e) + (2 * e + 1) = -(2 + 3 * (-1 - b) + 9 * (-b + 2 * e)) :=
  ⟨by lib_int, by lib_int⟩

end Bij27w

#print axioms Bij27w.rot27
#print axioms Bij27w.even_rep
#print axioms Bij27w.even_x_mod3
#print axioms Bij27w.odd_rep
#print axioms Bij27w.even_inv
#print axioms Bij27w.odd_inv
#print axioms Bij27w.odd27
#print axioms Bij27w.odd7
#print axioms Bij27w.three_ne_one
#print axioms Bij27w.split3
#print axioms Bij27w.odd_sel
#print axioms Bij27w.twelve_cancel
#print axioms Bij27w.ex_even
#print axioms Bij27w.uniq
#print axioms Bij27w.ex_odd

lang/Bij27wB.lean

/-
  lang/Bij27wB — T47, cycle 826 (box bound for the 27a reindexing): **j(3j + 1) is a natural number ≥ |j|**, hence
  1 + 3j(3j + 1) + 9k(3k + 1) = n forces |j|, |k| ≤ n (no triangle inequality, no Int order lemmas):
    pent_nat     j(3j + 1) = A with A : ℕ and |j| ≤ A          (cases on the Int constructors)
    box_bound    1 + 3j(3j + 1) + 9k(3k + 1) = n  ⇒  |j| ≤ n ∧ |k| ≤ n
  Lean 4 core only, proofs by hand (lib_int, lib_nat), no axioms.
-/
import lang.HeckeMult

namespace Bij27wB

/-- **j(3j + 1) as a natural number dominating |j|.** -/
theorem pent_nat (j : Int) : ∃ A : Nat, j * (3 * j + 1) = Int.ofNat A ∧ Int.natAbs j ≤ A := by
  cases j with
  | ofNat m =>
    refine ⟨m * (3 * m + 1), rfl, ?_⟩
    show m ≤ m * (3 * m + 1)
    exact Nat.le_trans (Nat.le_of_eq (Nat.mul_one m).symm) (Nat.mul_le_mul_left m (Nat.succ_le_succ (Nat.zero_le (3 * m))))
  | negSucc m =>
    refine ⟨m * (3 * m + 2) + (3 * m + 2), ?_, ?_⟩
    · show (-(Int.ofNat m + 1)) * (3 * (-(Int.ofNat m + 1)) + 1) = Int.ofNat m * (3 * Int.ofNat m + 2) + (3 * Int.ofNat m + 2)
      lib_int
    · show m + 1 ≤ m * (3 * m + 2) + (3 * m + 2)
      exact Nat.le_trans (Nat.le_trans (Nat.le_add_right (m + 1) (2 * m + 1)) (Nat.le_of_eq (by lib_nat)))
        (Nat.le_add_left (3 * m + 2) (m * (3 * m + 2)))

/-- **the box bound**: 1 + 3j(3j + 1) + 9k(3k + 1) = n ⇒ |j| ≤ n and |k| ≤ n. -/
theorem box_bound (j k : Int) (n : Nat) (h : 1 + 3 * (j * (3 * j + 1)) + 9 * (k * (3 * k + 1)) = Int.ofNat n) :
    Int.natAbs j ≤ n ∧ Int.natAbs k ≤ n := by
  obtain ⟨A, hA, hjA⟩ := pent_nat j
  obtain ⟨B, hB, hkB⟩ := pent_nat k
  have hn : 1 + 3 * A + 9 * B = n := Int.ofNat.inj
    ((show Int.ofNat (1 + 3 * A + 9 * B) = 1 + 3 * Int.ofNat A + 9 * Int.ofNat B from rfl).trans (by rw [← hA, ← hB]; exact h))
  refine ⟨Nat.le_trans hjA ?_, Nat.le_trans hkB ?_⟩
  · exact Nat.le_trans (Nat.le_trans (Nat.le_add_left A (2 * A)) (Nat.le_of_eq (by lib_nat)))
      (Nat.le_trans (Nat.le_add_right (3 * A) (9 * B + 1)) (Nat.le_of_eq ((show 3 * A + (9 * B + 1) = 1 + 3 * A + 9 * B by lib_nat).trans hn)))
  · exact Nat.le_trans (Nat.le_trans (Nat.le_add_left B (8 * B)) (Nat.le_of_eq (by lib_nat)))
      (Nat.le_trans (Nat.le_add_left (9 * B) (1 + 3 * A)) (Nat.le_of_eq hn))

end Bij27wB

#print axioms Bij27wB.pent_nat
#print axioms Bij27wB.box_bound

lang/All27wE.lean

/-
  lang/All27wE — T47, cycle 826 (27a weight-one factor, counting, even case): **each representation n = x² + 27y²
  (n odd, x ≡ 1 mod 3) is hit by exactly one (j, k) in the box**: Σ_(j ∈ zl N) Σ_(k ∈ zl N) [2 + 3j + 9k = 2x ∧ k − j = 2y] = 1
  for N ≥ n (Bij27w: odd27, ex_even, uniq, even_rep; Bij27wB.box_bound; HeckeMult.count_zl) — the Bij36.count_R step.
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.Bij27w
import lang.Bij27wB
import lib.IntRing

namespace All27w

open DetChain YMChain FiniteRecChain HeckeMult

abbrev Z := LibInt.intR

/-- the even-case relation between (j, k) and (x, y). -/
def RE (x y j k : Int) : Prop := 2 + 3 * j + 9 * k = 2 * x ∧ k - j = 2 * y

instance (x y j k : Int) : Decidable (RE x y j k) := by unfold RE; exact inferInstance

def rE (x y j k : Int) : Int := if RE x y j k then Z.one else Z.zero

/-- two solutions of the even relation coincide. -/
theorem RE_uniq (x y j k j0 k0 : Int) (h : RE x y j k) (h0 : RE x y j0 k0) : j = j0 ∧ k = k0 := by
  apply Bij27w.uniq j k j0 k0
  · calc 3 * j + 9 * k = (2 + 3 * j + 9 * k) - 2 := by lib_int
      _ = 2 * x - 2 := by rw [h.1]
      _ = (2 + 3 * j0 + 9 * k0) - 2 := by rw [h0.1]
      _ = 3 * j0 + 9 * k0 := by lib_int
  · exact h.2.trans h0.2.symm

/-- the indicator is δ(j, j0)·δ(k, k0) once (j0, k0) is a solution. -/
theorem rE_delta (x y j0 k0 : Int) (h0 : RE x y j0 k0) (j k : Int) : rE x y j k = Z.mul (delta Z j j0) (delta Z k k0) := by
  unfold rE
  by_cases hj : j = j0
  · by_cases hk : k = k0
    · rw [ite_pos' _ _ (show RE x y j k by rw [hj, hk]; exact h0)]
      unfold delta; rw [ite_pos' _ _ hj, ite_pos' _ _ hk]; rfl
    · rw [ite_neg' _ _ (fun hh => hk (RE_uniq x y j k j0 k0 hh h0).2)]
      unfold delta; rw [ite_neg' _ _ hk]; show (0 : Int) = _ * 0; lib_int
  · rw [ite_neg' _ _ (fun hh => hj (RE_uniq x y j k j0 k0 hh h0).1)]
    unfold delta; rw [ite_neg' _ _ hj]; show (0 : Int) = 0 * _; lib_int

/-- **exactly one (j, k) in the box for each even representation.** -/
theorem count_even (s N : Nat) (a y : Int) (h : (3 * a + 1) * (3 * a + 1) + 27 * (y * y) = Int.ofNat (2 * s + 1))
    (hN : 2 * s + 1 ≤ N) :
    sumK Z (fun j => sumK Z (fun k => rE (3 * a + 1) y j k) (zl N)) (zl N) = Z.one := by
  obtain ⟨r, hr⟩ := Bij27w.odd27 s (3 * a + 1) y h
  have hd : a + y = 2 * (r - a) := by
    calc a + y = (3 * a + 1 + y) - 1 - 2 * a := by lib_int
      _ = (2 * r + 1) - 1 - 2 * a := by rw [hr]
      _ = 2 * (r - a) := by lib_int
  have h0 : RE (3 * a + 1) y (r - a - 2 * y) (r - a) := Bij27w.ex_even a y (r - a) hd
  have hx : 1 + 6 * (r - a - 2 * y) + 9 * y = 3 * a + 1 := by
    calc 1 + 6 * (r - a - 2 * y) + 9 * y = 1 + 3 * (2 * (r - a)) - 3 * y := by lib_int
      _ = 1 + 3 * (a + y) - 3 * y := by rw [← hd]
      _ = 3 * a + 1 := by lib_int
  have hexp : 1 + 3 * ((r - a - 2 * y) * (3 * (r - a - 2 * y) + 1)) + 9 * ((r - a) * (3 * (r - a) + 1)) = Int.ofNat (2 * s + 1) := by
    calc 1 + 3 * ((r - a - 2 * y) * (3 * (r - a - 2 * y) + 1)) + 9 * ((r - a) * (3 * (r - a) + 1))
        = 1 + 9 * ((r - a - 2 * y) * (r - a - 2 * y)) + 3 * (r - a - 2 * y)
            + 27 * (((r - a - 2 * y) + 2 * y) * ((r - a - 2 * y) + 2 * y)) + 9 * ((r - a - 2 * y) + 2 * y) := by lib_int
      _ = (1 + 6 * (r - a - 2 * y) + 9 * y) * (1 + 6 * (r - a - 2 * y) + 9 * y) + 27 * (y * y) := Bij27w.even_rep (r - a - 2 * y) y
      _ = (3 * a + 1) * (3 * a + 1) + 27 * (y * y) := by rw [hx]
      _ = Int.ofNat (2 * s + 1) := h
  obtain ⟨bj, bk⟩ := Bij27wB.box_bound (r - a - 2 * y) (r - a) (2 * s + 1) hexp
  rw [sumK_ext Z _ (fun j => Z.mul (delta Z j (r - a - 2 * y)) (sumK Z (fun k => delta Z k (r - a)) (zl N))) (fun j => by
    rw [mul_sumK]; exact sumK_ext Z _ _ (fun k => rE_delta _ _ _ _ h0 j k) _)]
  rw [sumK_ext Z _ (fun j => Z.mul (delta Z j (r - a - 2 * y)) Z.one) (fun j => by rw [count_zl Z (r - a) N (Nat.le_trans bk hN)]),
    ← sumK_mul, count_zl Z (r - a - 2 * y) N (Nat.le_trans bj hN)]
  rfl

end All27w

#print axioms All27w.RE_uniq
#print axioms All27w.rE_delta
#print axioms All27w.count_even

lang/All27wO.lean

/-
  lang/All27wO — T47, cycle 826 (27a weight-one factor, counting, odd case): **each representation n = 4x² + 2xy + 7y²
  with y = 2e + 1, x = 3b − 5e is hit by exactly one (j, k) in the box**:
  Σ_(j ∈ zl N) Σ_(k ∈ zl N) [4x + y = −(2 + 3j + 9k) ∧ k − j = y] = 1 for N ≥ n
  (Bij27w: ex_odd, uniq, odd_rep; Bij27wB.box_bound; HeckeMult.count_zl). Which of (x, y), (−x, −y) has this shape: Bij27w.odd_sel.
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.All27wE
import lib.IntRing

namespace All27w

open DetChain YMChain FiniteRecChain HeckeMult

/-- the odd-case relation. -/
def RO (x y j k : Int) : Prop := 4 * x + y = -(2 + 3 * j + 9 * k) ∧ k - j = y

instance (x y j k : Int) : Decidable (RO x y j k) := by unfold RO; exact inferInstance

def rO (x y j k : Int) : Int := if RO x y j k then Z.one else Z.zero

theorem RO_uniq (x y j k j0 k0 : Int) (h : RO x y j k) (h0 : RO x y j0 k0) : j = j0 ∧ k = k0 := by
  apply Bij27w.uniq j k j0 k0
  · calc 3 * j + 9 * k = -(-(2 + 3 * j + 9 * k)) - 2 := by lib_int
      _ = -(4 * x + y) - 2 := by rw [h.1]
      _ = -(-(2 + 3 * j0 + 9 * k0)) - 2 := by rw [h0.1]
      _ = 3 * j0 + 9 * k0 := by lib_int
  · exact h.2.trans h0.2.symm

theorem rO_delta (x y j0 k0 : Int) (h0 : RO x y j0 k0) (j k : Int) : rO x y j k = Z.mul (delta Z j j0) (delta Z k k0) := by
  unfold rO
  by_cases hj : j = j0
  · by_cases hk : k = k0
    · rw [ite_pos' _ _ (show RO x y j k by rw [hj, hk]; exact h0)]
      unfold delta; rw [ite_pos' _ _ hj, ite_pos' _ _ hk]; rfl
    · rw [ite_neg' _ _ (fun hh => hk (RO_uniq x y j k j0 k0 hh h0).2)]
      unfold delta; rw [ite_neg' _ _ hk]; show (0 : Int) = _ * 0; lib_int
  · rw [ite_neg' _ _ (fun hh => hj (RO_uniq x y j k j0 k0 hh h0).1)]
    unfold delta; rw [ite_neg' _ _ hj]; show (0 : Int) = 0 * _; lib_int

/-- **exactly one (j, k) in the box for each odd representation of the selected shape.** -/
theorem count_odd (s N : Nat) (b e : Int)
    (h : 4 * ((3 * b - 5 * e) * (3 * b - 5 * e)) + 2 * ((3 * b - 5 * e) * (2 * e + 1)) + 7 * ((2 * e + 1) * (2 * e + 1)) = Int.ofNat (2 * s + 1))
    (hN : 2 * s + 1 ≤ N) :
    sumK Z (fun j => sumK Z (fun k => rO (3 * b - 5 * e) (2 * e + 1) j k) (zl N)) (zl N) = Z.one := by
  have h0 : RO (3 * b - 5 * e) (2 * e + 1) (-1 - b) (-b + 2 * e) := And.intro (Bij27w.ex_odd b e).2 (Bij27w.ex_odd b e).1
  have hexp : 1 + 3 * ((-1 - b) * (3 * (-1 - b) + 1)) + 9 * ((-b + 2 * e) * (3 * (-b + 2 * e) + 1)) = Int.ofNat (2 * s + 1) := by
    calc 1 + 3 * ((-1 - b) * (3 * (-1 - b) + 1)) + 9 * ((-b + 2 * e) * (3 * (-b + 2 * e) + 1))
        = 1 + 9 * ((-1 - b) * (-1 - b)) + 3 * (-1 - b) + 27 * ((-1 - b + 2 * e + 1) * (-1 - b + 2 * e + 1)) + 9 * (-1 - b + 2 * e + 1) := by lib_int
      _ = 4 * ((-3 - 3 * (-1 - b) - 5 * e) * (-3 - 3 * (-1 - b) - 5 * e)) + 2 * ((-3 - 3 * (-1 - b) - 5 * e) * (2 * e + 1))
            + 7 * ((2 * e + 1) * (2 * e + 1)) := Bij27w.odd_rep (-1 - b) e
      _ = 4 * ((3 * b - 5 * e) * (3 * b - 5 * e)) + 2 * ((3 * b - 5 * e) * (2 * e + 1)) + 7 * ((2 * e + 1) * (2 * e + 1)) := by
          rw [show -3 - 3 * (-1 - b) - 5 * e = 3 * b - 5 * e by lib_int]
      _ = Int.ofNat (2 * s + 1) := h
  obtain ⟨bj, bk⟩ := Bij27wB.box_bound (-1 - b) (-b + 2 * e) (2 * s + 1) hexp
  rw [sumK_ext Z _ (fun j => Z.mul (delta Z j (-1 - b)) (sumK Z (fun k => delta Z k (-b + 2 * e)) (zl N))) (fun j => by
    rw [mul_sumK]; exact sumK_ext Z _ _ (fun k => rO_delta _ _ _ _ h0 j k) _)]
  rw [sumK_ext Z _ (fun j => Z.mul (delta Z j (-1 - b)) Z.one) (fun j => by rw [count_zl Z (-b + 2 * e) N (Nat.le_trans bk hN)]),
    ← sumK_mul, count_zl Z (-1 - b) N (Nat.le_trans bj hN)]
  rfl

end All27w

#print axioms All27w.RO_uniq
#print axioms All27w.rO_delta
#print axioms All27w.count_odd

lang/Inner27w.lean

/-
  lang/Inner27w — T47, cycle 826 (27a weight-one factor, the inner direction): **for each (j, k), the (x, y) related to it is
  unique**, and when the exponent condition holds it exists:
    two_cancel     2c = 0 ⇒ c = 0 (Int constructors)
    RE_xy_uniq     RE x y j k ∧ RE x' y' j k ⇒ x = x' ∧ y = y'          (even relation of All27wE)
    RO_xy_uniq     RO x y j k ∧ RO x' y' j k ⇒ x = x' ∧ y = y'          (odd relation of All27wO)
    RE_of_even     k = j + 2e ⇒ RE (1 + 6j + 9e) e j k
    RO_of_odd      k = j + 2e + 1 ⇒ RO (−3 − 3j − 5e) (2e + 1) j k
  (with Bij27w.even_rep / odd_rep the exponent of (j, k) is then the value of the form at (x, y)).
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.All27wO
import lib.IntRing

namespace All27w

/-- **2c = 0 ⇒ c = 0.** -/
theorem two_cancel (c : Int) (h : 2 * c = 0) : c = 0 := by
  cases c with
  | ofNat m =>
    cases m with
    | zero => rfl
    | succ m =>
      have h' : 2 * (m + 1) = 0 := Int.ofNat.inj (show Int.ofNat (2 * (m + 1)) = Int.ofNat 0 from h)
      have h2 : 2 * m + 1 + 1 = 0 := h'
      nomatch h2
  | negSucc m => exact Int.noConfusion (show Int.negSucc (2 * m + 1) = Int.ofNat 0 from h)

theorem RE_xy_uniq (x y x' y' j k : Int) (h : RE x y j k) (h' : RE x' y' j k) : x = x' ∧ y = y' := by
  have hx : x - x' = 0 := two_cancel _ (by
    calc 2 * (x - x') = 2 * x - 2 * x' := by lib_int
      _ = (2 + 3 * j + 9 * k) - (2 + 3 * j + 9 * k) := by rw [← h.1, ← h'.1]
      _ = 0 := by lib_int)
  have hy : y - y' = 0 := two_cancel _ (by
    calc 2 * (y - y') = 2 * y - 2 * y' := by lib_int
      _ = (k - j) - (k - j) := by rw [← h.2, ← h'.2]
      _ = 0 := by lib_int)
  exact ⟨by calc x = (x - x') + x' := by lib_int
              _ = x' := by rw [hx]; lib_int,
         by calc y = (y - y') + y' := by lib_int
              _ = y' := by rw [hy]; lib_int⟩

theorem RO_xy_uniq (x y x' y' j k : Int) (h : RO x y j k) (h' : RO x' y' j k) : x = x' ∧ y = y' := by
  have hy : y = y' := h.2.symm.trans h'.2
  have hx4 : 4 * (x - x') = 0 := by
    calc 4 * (x - x') = (4 * x + y) - (4 * x' + y') + (y' - y) := by lib_int
      _ = (-(2 + 3 * j + 9 * k)) - (-(2 + 3 * j + 9 * k)) + (y' - y) := by rw [h.1, h'.1]
      _ = y' - y := by lib_int
      _ = 0 := by rw [hy]; lib_int
  have hx : x - x' = 0 := two_cancel _ (two_cancel _ (by
    calc 2 * (2 * (x - x')) = 4 * (x - x') := by lib_int
      _ = 0 := hx4))
  exact ⟨by calc x = (x - x') + x' := by lib_int
              _ = x' := by rw [hx]; lib_int, hy⟩

theorem RE_of_even (j e : Int) : RE (1 + 6 * j + 9 * e) e j (j + 2 * e) :=
  And.intro (by lib_int) (by lib_int)

theorem RO_of_odd (j e : Int) : RO (-3 - 3 * j - 5 * e) (2 * e + 1) j (j + 2 * e + 1) :=
  And.intro (by lib_int) (by lib_int)

/-- the odd-side congruence x + y ≡ 1 (mod 3), y = 2e + 1, gives Bij27w's form x = 3b − 5e. -/
theorem odd_cond (x e t : Int) (h : x + (2 * e + 1) = 3 * t + 1) : x = 3 * (t + e) - 5 * e := by
  calc x = (x + (2 * e + 1)) - 2 * e - 1 := by lib_int
    _ = (3 * t + 1) - 2 * e - 1 := by rw [h]
    _ = 3 * (t + e) - 5 * e := by lib_int

/-- the (x, y) of an even (j, k) has x ≡ 1 (mod 3). -/
theorem even_x (j e : Int) : 1 + 6 * j + 9 * e = 3 * (2 * j + 3 * e) + 1 := by lib_int

/-- the (x, y) of an odd (j, k) has x + y ≡ 1 (mod 3). -/
theorem odd_xy (j e : Int) : (-3 - 3 * j - 5 * e) + (2 * e + 1) = 3 * (-1 - j - e) + 1 := by lib_int

end All27w

#print axioms All27w.two_cancel
#print axioms All27w.RE_xy_uniq
#print axioms All27w.RO_xy_uniq
#print axioms All27w.RE_of_even
#print axioms All27w.RO_of_odd
#print axioms All27w.odd_cond
#print axioms All27w.even_x
#print axioms All27w.odd_xy

#print axioms: 31 theorems, no axioms.

78.364 The weight-one factor of 27a at every odd exponent

The coefficients of \(\eta (6z)\eta (18z)\) at odd exponents are differences of representation numbers of the two forms of discriminant \(-108\).

\[ [q^{n}]\,\eta (6z)\eta (18z)=\#\{x^2+27y^2=n,\ x\equiv 1\ (3)\}-\#\{4x^2+2xy+7y^2=n,\ y\ \mathrm {odd},\ x+y\equiv 1\ (3)\}\quad (n\ \mathrm {odd}). \]

lang/Coef27w.lean

/-
  lang/Coef27w — T47, cycle 827 (27a weight-one factor): **the coefficient of q^(n+1) in η(6z)η(18z), re-indexed over j, k ∈ ℤ**:
    [q^(n+1)] = Σ_{j ∈ zl(2M)} (−1)^|j| [shift 6G(j)] Σ_{k ∈ zl(2M)} (−1)^|k| [shift 18G(k)] 1,   G = Gj g36,
  i.e. Eta27wC.eta27w_coef with QBinom.gzg applied to the outer and to the inner pentagonal sum (as All36.eta36_sum).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Eta27wC
import lang.GzGen
import lang.Ap36
import lib.RingTac
import lib.IntRing

namespace Trunc

open GLnLocal

variable {R : Type} (K : DetChain.CRing R) (M : Nat)

/-- the inner pentagonal sum at index i, re-indexed. -/
def in27 (i : Nat) : R :=
  QBinom.Bg K (fun e => shn K (18 * e) (nth K M (TR K M).one) i) Hecke36.g36 (2 * M)

theorem in27_eq (i : Nat) :
    QBinom.es K (fun m' => shn K (18 * (2 * QBinom.Ta (2 * M) m' + QBinom.Tb (2 * M) m')) (nth K M (TR K M).one) i)
      (K.neg K.one) (2 * (2 * M) + 1) = in27 K M i := by
  show QBinom.es K (fun m' => (fun e => shn K (18 * e) (nth K M (TR K M).one) i) (Hecke36.g36 (2 * M) m'))
      (K.neg K.one) (2 * (2 * M) + 1) = _
  rw [QBinom.gzg K (fun e => shn K (18 * e) (nth K M (TR K M).one) i) Hecke36.g36 Hecke36.g36_shift (2 * M),
    QBinom.pm_even K M]
  show K.mul K.one (in27 K M i) = in27 K M i
  lib_ring

/-- **[q^(n+1)] η(6z)η(18z) as a double sum over j, k ∈ zl(2M).** -/
theorem coef27w (n : Nat) (hn : n + 1 < M) :
    nth K M ((TR K M).mul (qe K M) ((TR K M).mul (QBinom.pp (TR K M) (Frobenius.pw (TR K M) (qe K M) 6) M)
      (QBinom.pp (TR K M) (Frobenius.pw (TR K M) (qe K M) 18) M))) (n + 1) =
    QBinom.Bg K (fun e => shn K (6 * e) (in27 K M) n) Hecke36.g36 (2 * M) := by
  rw [eta27w_coef K M n hn]
  calc QBinom.es K (fun m => shn K (6 * (2 * QBinom.Ta (2 * M) m + QBinom.Tb (2 * M) m))
        (fun i => QBinom.es K (fun m' => shn K (18 * (2 * QBinom.Ta (2 * M) m' + QBinom.Tb (2 * M) m')) (nth K M (TR K M).one) i)
          (K.neg K.one) (2 * (2 * M) + 1)) n) (K.neg K.one) (2 * (2 * M) + 1)
      = QBinom.es K (fun m => (fun e => shn K (6 * e) (in27 K M) n) (Hecke36.g36 (2 * M) m)) (K.neg K.one) (2 * (2 * M) + 1) := by
        apply QBinom.es_congr
        intro m
        apply shn_congr K _ _ _ n
        intro i _
        exact in27_eq K M i
    _ = K.mul (Frobenius.pw K (K.neg K.one) (2 * M)) (QBinom.Bg K (fun e => shn K (6 * e) (in27 K M) n) Hecke36.g36 (2 * M)) :=
        QBinom.gzg K (fun e => shn K (6 * e) (in27 K M) n) Hecke36.g36 Hecke36.g36_shift (2 * M)
    _ = _ := by rw [QBinom.pm_even K M]; lib_ring

/-- **the exponent in pentagonal coordinates**: 1 + 6G(j) + 18G(k) = 1 + 9j² + 3j + 27k² + 9k (Hecke36.e2G). -/
theorem exp27 (j k : Int) :
    1 + Int.ofNat (6 * QBinom.Gj Hecke36.g36 j + 18 * QBinom.Gj Hecke36.g36 k) =
      1 + 9 * (j * j) + 3 * j + 27 * (k * k) + 9 * k := by
  have e : Int.ofNat (6 * QBinom.Gj Hecke36.g36 j + 18 * QBinom.Gj Hecke36.g36 k) =
      3 * (2 * Int.ofNat (QBinom.Gj Hecke36.g36 j)) + 9 * (2 * Int.ofNat (QBinom.Gj Hecke36.g36 k)) := by
    show 6 * Int.ofNat (QBinom.Gj Hecke36.g36 j) + 18 * Int.ofNat (QBinom.Gj Hecke36.g36 k) = _
    lib_int
  rw [e, Hecke36.e2G j, Hecke36.e2G k]
  lib_int

end Trunc

#print axioms Trunc.in27_eq
#print axioms Trunc.coef27w
#print axioms Trunc.exp27

lang/Dbl27w.lean

/-
  lang/Dbl27w — T47, cycle 827 (27a weight-one factor): **[q^(n+1)] η(6z)η(18z) over ℤ as an explicit double sum**
    [q^(n+1)] = Σ_{j ∈ zl(2M)} (−1)^|j| Σ_{k ∈ zl(2M)} (−1)^|k| δ(18G(k) + 6G(j), n),   G = Gj g36,  n + 1 < M,
  from Trunc.coef27w (Coef27w) by unfolding the shifts (Hecke32.shn_add/shn_smul/shn_shn, Coef36.shn_delta, TruncQ.nth_pw).
    shn_sumK   a shift commutes with a finite sum
    dbl27      the double sum
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Coef27w
import lang.Coef36
import lib.RingTac

namespace Hecke36

open DetChain YMChain FiniteRecChain Hecke32

theorem shn_sumK (E k : Nat) (f : Int → Nat → Int) : ∀ L : List Int,
    Trunc.shn Z E (fun i => sumK Z (fun j => f j i) L) k = sumK Z (fun j => Trunc.shn Z E (f j) k) L
  | [] => shn_zero E k
  | a :: L => by
    show Trunc.shn Z E (fun i => Z.add (f a i) (sumK Z (fun j => f j i) L)) k = Z.add (Trunc.shn Z E (f a) k) (sumK Z (fun j => Trunc.shn Z E (f j) k) L)
    rw [shn_add, shn_sumK E k f L]

/-- the coefficient as an explicit signed double count. -/
def dbl (M n : Nat) : Int :=
  sumK Z (fun j => Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs j))
    (sumK Z (fun k => Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs k))
      (delta Z (18 * QBinom.Gj g36 k + 6 * QBinom.Gj g36 j) n)) (HeckeMult.zl (2 * M)))) (HeckeMult.zl (2 * M))

/-- **[q^(n+1)] η(6z)η(18z) = dbl M n** (n + 1 < M). -/
theorem dbl27 (M n : Nat) (hn : n + 1 < M) :
    Trunc.nth Z M ((Trunc.TR Z M).mul (Trunc.qe Z M) ((Trunc.TR Z M).mul (QBinom.pp (Trunc.TR Z M) (Frobenius.pw (Trunc.TR Z M) (Trunc.qe Z M) 6) M)
      (QBinom.pp (Trunc.TR Z M) (Frobenius.pw (Trunc.TR Z M) (Trunc.qe Z M) 18) M))) (n + 1) = dbl M n := by
  rw [Trunc.coef27w Z M n hn]
  apply sumK_ext
  intro j
  show Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs j))
      (Trunc.shn Z (6 * QBinom.Gj g36 j) (fun i => sumK Z (fun k => Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs k))
        (Trunc.shn Z (18 * QBinom.Gj g36 k) (Trunc.nth Z M (Trunc.TR Z M).one) i)) (HeckeMult.zl (2 * M))) n) = _
  have hnM : n < M := Nat.lt_of_succ_lt hn
  rw [Trunc.shn_congr Z (6 * QBinom.Gj g36 j) _
      (fun i => sumK Z (fun k => Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs k))
        (Trunc.shn Z (18 * QBinom.Gj g36 k) (GLnLocal.delta Z) i)) (HeckeMult.zl (2 * M))) n (fun i hi => by
      apply sumK_ext
      intro k
      rw [Trunc.shn_congr Z (18 * QBinom.Gj g36 k) (Trunc.nth Z M (Trunc.TR Z M).one) (GLnLocal.delta Z) i (fun i' hi' =>
        Trunc.nth_pw Z M 0 i' (Nat.lt_of_le_of_lt hi' (Nat.lt_of_le_of_lt hi hnM)))]),
    shn_sumK]
  apply congrArg
  apply sumK_ext
  intro k
  rw [shn_smul, shn_shn, shn_delta]

end Hecke36

#print axioms Hecke36.shn_sumK
#print axioms Hecke36.dbl27

lang/Sign27w.lean

/-
  lang/Sign27w — T47, cycle 827 (27a weight-one factor, the sign): **(−1)^(|j| + |k|) is +1 when k − j is even and −1 when odd**:
    sg            sg j = (−1)^|j|
    sg_succ       sg (j + 1) = −sg j        (cases on the Int constructors)
    sg_pred       sg (j − 1) = −sg j
    sg_addN, sg_subN, sg_even   sg (j + 2y) = sg j
    sg_sq         sg j · sg j = 1
    sign_even     k − j = 2y ⇒ sg j · sg k = 1
    sign_odd      k − j = 2e + 1 ⇒ sg j · sg k = −1
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.Inner27w
import lib.RingTac
import lib.IntRing

namespace All27w

open Frobenius

def sg (j : Int) : Int := pw Z (Z.neg Z.one) (Int.natAbs j)

theorem sg_succ (j : Int) : sg (j + 1) = Z.neg (sg j) := by
  cases j with
  | ofNat m =>
    show pw Z (Z.neg Z.one) (m + 1) = Z.neg (pw Z (Z.neg Z.one) m)
    show Z.mul (pw Z (Z.neg Z.one) m) (Z.neg Z.one) = Z.neg (pw Z (Z.neg Z.one) m)
    lib_ring
  | negSucc m =>
    cases m with
    | zero =>
      show pw Z (Z.neg Z.one) 0 = Z.neg (pw Z (Z.neg Z.one) 1)
      show Z.one = Z.neg (Z.mul Z.one (Z.neg Z.one))
      lib_ring
    | succ m =>
      show pw Z (Z.neg Z.one) (m + 1) = Z.neg (pw Z (Z.neg Z.one) (m + 1 + 1))
      show pw Z (Z.neg Z.one) (m + 1) = Z.neg (Z.mul (pw Z (Z.neg Z.one) (m + 1)) (Z.neg Z.one))
      lib_ring

theorem sg_pred (j : Int) : sg (j - 1) = Z.neg (sg j) := by
  have h := sg_succ (j - 1)
  rw [show j - 1 + 1 = j by lib_int] at h
  calc sg (j - 1) = Z.neg (Z.neg (sg (j - 1))) := by lib_ring
    _ = Z.neg (sg j) := by rw [h]

theorem sg_addN (j : Int) : ∀ n : Nat, sg (j + Int.ofNat (2 * n)) = sg j
  | 0 => by rw [show j + Int.ofNat (2 * 0) = j by show j + 0 = j; lib_int]
  | n + 1 => by
    rw [show j + Int.ofNat (2 * (n + 1)) = j + Int.ofNat (2 * n) + 1 + 1 by
          show j + (2 * Int.ofNat n + 2) = j + 2 * Int.ofNat n + 1 + 1; lib_int,
      sg_succ, sg_succ, sg_addN j n]
    lib_ring

theorem sg_subN (j : Int) : ∀ n : Nat, sg (j - Int.ofNat (2 * n)) = sg j
  | 0 => by rw [show j - Int.ofNat (2 * 0) = j by show j - 0 = j; lib_int]
  | n + 1 => by
    rw [show j - Int.ofNat (2 * (n + 1)) = j - Int.ofNat (2 * n) - 1 - 1 by
          show j - (2 * Int.ofNat n + 2) = j - 2 * Int.ofNat n - 1 - 1; lib_int,
      sg_pred, sg_pred, sg_subN j n]
    lib_ring

theorem sg_even (j y : Int) : sg (j + 2 * y) = sg j := by
  cases y with
  | ofNat n =>
    show sg (j + 2 * Int.ofNat n) = sg j
    rw [show j + 2 * Int.ofNat n = j + Int.ofNat (2 * n) from rfl]
    exact sg_addN j n
  | negSucc n =>
    have e1 : Int.negSucc n = -(Int.ofNat (n + 1)) := rfl
    have e2 : Int.ofNat (2 * (n + 1)) = 2 * Int.ofNat (n + 1) := rfl
    rw [show j + 2 * Int.negSucc n = j - Int.ofNat (2 * (n + 1)) by rw [e1, e2]; lib_int]
    exact sg_subN j (n + 1)

theorem sg_sq (j : Int) : Z.mul (sg j) (sg j) = Z.one := by
  show Z.mul (pw Z (Z.neg Z.one) (Int.natAbs j)) (pw Z (Z.neg Z.one) (Int.natAbs j)) = Z.one
  generalize Int.natAbs j = n
  induction n with
  | zero => show Z.mul Z.one Z.one = Z.one; lib_ring
  | succ n ih =>
    show Z.mul (Z.mul (pw Z (Z.neg Z.one) n) (Z.neg Z.one)) (Z.mul (pw Z (Z.neg Z.one) n) (Z.neg Z.one)) = Z.one
    calc Z.mul (Z.mul (pw Z (Z.neg Z.one) n) (Z.neg Z.one)) (Z.mul (pw Z (Z.neg Z.one) n) (Z.neg Z.one))
        = Z.mul (pw Z (Z.neg Z.one) n) (pw Z (Z.neg Z.one) n) := by lib_ring
      _ = Z.one := ih

theorem sign_even (j k y : Int) (h : k - j = 2 * y) : Z.mul (sg j) (sg k) = Z.one := by
  rw [show k = j + 2 * y by
        calc k = (k - j) + j := by lib_int
          _ = 2 * y + j := by rw [h]
          _ = j + 2 * y := by lib_int,
    sg_even]
  exact sg_sq j

theorem sign_odd (j k e : Int) (h : k - j = 2 * e + 1) : Z.mul (sg j) (sg k) = Z.neg Z.one := by
  rw [show k = j + 2 * e + 1 by
        calc k = (k - j) + j := by lib_int
          _ = 2 * e + 1 + j := by rw [h]
          _ = j + 2 * e + 1 := by lib_int,
    sg_succ, sg_even]
  calc Z.mul (sg j) (Z.neg (sg j)) = Z.neg (Z.mul (sg j) (sg j)) := by lib_ring
    _ = Z.neg Z.one := by rw [sg_sq]

end All27w

#print axioms All27w.sg_succ
#print axioms All27w.sg_pred
#print axioms All27w.sg_addN
#print axioms All27w.sg_subN
#print axioms All27w.sg_even
#print axioms All27w.sg_sq
#print axioms All27w.sign_even
#print axioms All27w.sign_odd

lang/AbsTri.lean

/-
  lang/AbsTri — T47, cycle 827 (27a box bounds): **the triangle inequality for Int.natAbs, by hand** (core's lemmas may pull
  propext; this one is by constructor cases):
    sub_abs     |m ⊖ k| ≤ m + k            (Int.subNatNat)
    abs_add     |a + b| ≤ |a| + |b|
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.Bij27wB
import lib.Semi
import lib.IntRing

namespace AbsTri

theorem sub_abs (m k : Nat) : Int.natAbs (Int.subNatNat m k) ≤ m + k := by
  unfold Int.subNatNat
  cases h : k - m with
  | zero =>
    show m - k ≤ m + k
    exact Nat.le_trans (Nat.sub_le m k) (Nat.le_add_right m k)
  | succ t =>
    show t + 1 ≤ m + k
    rw [← h]
    exact Nat.le_trans (Nat.sub_le k m) (Nat.le_add_left k m)

theorem abs_add (a b : Int) : Int.natAbs (a + b) ≤ Int.natAbs a + Int.natAbs b := by
  cases a with
  | ofNat m =>
    cases b with
    | ofNat n => exact Nat.le_refl _
    | negSucc n =>
      show Int.natAbs (Int.subNatNat m (n + 1)) ≤ m + (n + 1)
      exact sub_abs m (n + 1)
  | negSucc m =>
    cases b with
    | ofNat n =>
      show Int.natAbs (Int.subNatNat n (m + 1)) ≤ (m + 1) + n
      exact Nat.le_trans (sub_abs n (m + 1)) (Nat.le_of_eq (Nat.add_comm n (m + 1)))
    | negSucc n =>
      show m + n + 1 + 1 ≤ (m + 1) + (n + 1)
      exact Nat.le_of_eq (by lib_nat)

theorem abs_negOfNat : ∀ t : Nat, Int.natAbs (Int.negOfNat t) = t
  | 0 => rfl
  | _ + 1 => rfl

theorem abs_neg (a : Int) : Int.natAbs (-a) = Int.natAbs a := by
  cases a with
  | ofNat m => exact abs_negOfNat m
  | negSucc m => rfl

/-- |c·a| = c·|a| for a natural constant c. -/
theorem abs_c (c : Nat) (a : Int) : Int.natAbs (Int.ofNat c * a) = c * Int.natAbs a := by
  cases a with
  | ofNat m => rfl
  | negSucc m => exact abs_negOfNat (c * (m + 1))

theorem le_abs2 (q : Int) : Int.natAbs q ≤ Int.natAbs (2 * q) := by
  have e : Int.natAbs (2 * q) = 2 * Int.natAbs q := abs_c 2 q
  rw [e]
  exact Nat.le_trans (Nat.le_add_left (Int.natAbs q) (Int.natAbs q)) (Nat.le_of_eq (by lib_nat))

/-- |k − j| ≤ |k| + |j|. -/
theorem abs_sub (k j : Int) : Int.natAbs (k + -j) ≤ Int.natAbs k + Int.natAbs j := by
  have h := abs_add k (-j)
  rw [abs_neg] at h
  exact h

/-- **box for the even case**: |j|, |k| ≤ n and k = j + 2q ⇒ |2j + 3q| ≤ 8n and |q| ≤ 8n. -/
theorem box_even (j k q : Int) (n : Nat) (hj : Int.natAbs j ≤ n) (hk : Int.natAbs k ≤ n) (hq : k = j + 2 * q) :
    Int.natAbs (2 * j + 3 * q) ≤ 8 * n ∧ Int.natAbs q ≤ 8 * n := by
  have h2q : Int.natAbs q ≤ 2 * n := by
    have e : 2 * q = k + -j := by rw [hq]; lib_int
    have h3 : Int.natAbs (2 * q) ≤ 2 * n := by
      rw [e]
      exact Nat.le_trans (abs_sub k j) (Nat.le_trans (Nat.add_le_add hk hj) (Nat.le_of_eq (by lib_nat)))
    exact Nat.le_trans (le_abs2 q) h3
  have ha : Int.natAbs (2 * j + 3 * q) ≤ 8 * n := by
    refine Nat.le_trans (abs_add (2 * j) (3 * q)) ?_
    have e2 : Int.natAbs (2 * j) = 2 * Int.natAbs j := abs_c 2 j
    have e3 : Int.natAbs (3 * q) = 3 * Int.natAbs q := abs_c 3 q
    rw [e2, e3]
    exact Nat.le_trans (Nat.add_le_add (Nat.mul_le_mul_left 2 hj) (Nat.mul_le_mul_left 3 h2q)) (Nat.le_of_eq (by lib_nat))
  exact ⟨ha, Nat.le_trans h2q (Nat.le_trans (Nat.le_add_right (2 * n) (6 * n)) (Nat.le_of_eq (by lib_nat)))⟩

/-- **box for the odd case**: |j|, |k| ≤ n and k = j + 2q + 1 ⇒ |−1 − j| ≤ 8n + 1 and |q| ≤ 8n + 1. -/
theorem box_odd (j k q : Int) (n : Nat) (hj : Int.natAbs j ≤ n) (hk : Int.natAbs k ≤ n) (hq : k = j + 2 * q + 1) :
    Int.natAbs (-1 - j) ≤ 8 * n + 1 ∧ Int.natAbs q ≤ 8 * n + 1 := by
  have hb : Int.natAbs (-1 - j) ≤ n + 1 := by
    rw [show -1 - j = -(j + 1) by lib_int, abs_neg]
    exact Nat.le_trans (abs_add j 1) (Nat.add_le_add_right hj 1)
  have hq2 : Int.natAbs q ≤ 2 * n + 1 := by
    have e : 2 * q = (k + -j) + -1 := by rw [hq]; lib_int
    refine Nat.le_trans (le_abs2 q) ?_
    rw [e]
    refine Nat.le_trans (abs_add (k + -j) (-1)) ?_
    exact Nat.le_trans (Nat.add_le_add (abs_sub k j) (Nat.le_refl 1))
      (Nat.le_trans (Nat.add_le_add_right (Nat.add_le_add hk hj) 1) (Nat.le_of_eq (by lib_nat)))
  exact ⟨Nat.le_trans hb (Nat.le_trans (Nat.add_le_add_right (Nat.le_add_right n (7 * n)) 1) (Nat.le_of_eq (by lib_nat))),
    Nat.le_trans hq2 (Nat.le_trans (Nat.add_le_add_right (Nat.le_add_right (2 * n) (6 * n)) 1) (Nat.le_of_eq (by lib_nat)))⟩

end AbsTri

#print axioms AbsTri.sub_abs
#print axioms AbsTri.abs_add
#print axioms AbsTri.abs_negOfNat
#print axioms AbsTri.abs_neg
#print axioms AbsTri.abs_c
#print axioms AbsTri.le_abs2
#print axioms AbsTri.abs_sub
#print axioms AbsTri.box_even
#print axioms AbsTri.box_odd

lang/All27wI.lean

/-
  lang/All27wI — T47, cycle 827 (27a weight-one factor, the inner step): **for each (j, k) the signed term of the double sum
  equals the (a, y)-count minus the (b, e)-count it hits**:
    HE s a y = [(3a + 1)² + 27y² = 2s + 1],   HO s b e = [4x² + 2xy + 7y² = 2s + 1], x = 3b − 5e, y = 2e + 1
    SE, SO    Σ_(a, y ∈ zl N) rE(3a + 1, y, j, k)·HE,   Σ_(b, e ∈ zl N) rO(3b − 5e, 2e + 1, j, k)·HO
    inner     SE − SO = sg j · (sg k · δ(18G(k) + 6G(j), 2s))     (N ≥ 8(2s + 1) + 1)
  Lean 4 core only, proofs by hand (lib_int, lib_ring), no axioms.
-/
import lang.Inner27w
import lang.Sign27w
import lang.Dbl27w
import lang.AbsTri
import lib.RingTac
import lib.IntRing

namespace All27w

open DetChain YMChain FiniteRecChain HeckeMult

def HE (s : Nat) (a y : Int) : Int :=
  if (3 * a + 1) * (3 * a + 1) + 27 * (y * y) = Int.ofNat (2 * s + 1) then Z.one else Z.zero

def HO (s : Nat) (b e : Int) : Int :=
  if 4 * ((3 * b - 5 * e) * (3 * b - 5 * e)) + 2 * ((3 * b - 5 * e) * (2 * e + 1)) + 7 * ((2 * e + 1) * (2 * e + 1)) = Int.ofNat (2 * s + 1)
  then Z.one else Z.zero

def SE (s N : Nat) (j k : Int) : Int :=
  sumK Z (fun a => sumK Z (fun y => Z.mul (rE (3 * a + 1) y j k) (HE s a y)) (zl N)) (zl N)

def SO (s N : Nat) (j k : Int) : Int :=
  sumK Z (fun b => sumK Z (fun e => Z.mul (rO (3 * b - 5 * e) (2 * e + 1) j k) (HO s b e)) (zl N)) (zl N)

theorem two_ne_one (c : Int) (h : 2 * c = 1) : False := by
  cases c with
  | ofNat m =>
    have h' : 2 * m = 1 := Int.ofNat.inj (show Int.ofNat (2 * m) = Int.ofNat 1 from h)
    cases m with
    | zero => exact Nat.noConfusion h'
    | succ m =>
      have h2 : 2 * m + 1 + 1 = 0 + 1 := h'
      exact Nat.noConfusion (Nat.succ.inj h2)
  | negSucc m => exact Int.noConfusion (show Int.negSucc (2 * m + 1) = Int.ofNat 1 from h)

theorem three_zero (c : Int) (h : 3 * c = 0) : c = 0 := by
  cases c with
  | ofNat m =>
    cases m with
    | zero => rfl
    | succ m =>
      have h' : 3 * (m + 1) = 0 := Int.ofNat.inj (show Int.ofNat (3 * (m + 1)) = Int.ofNat 0 from h)
      have h2 : 3 * m + 2 + 1 = 0 := h'
      exact Nat.noConfusion h2
  | negSucc m => exact Int.noConfusion (show Int.negSucc (3 * m + 2) = Int.ofNat 0 from h)

theorem cancel3 (a b c : Int) (h : 3 * a + c = 3 * b + c) : a = b := by
  have h0 : a - b = 0 := three_zero _ (by
    calc 3 * (a - b) = (3 * a + c) - (3 * b + c) := by lib_int
      _ = (3 * b + c) - (3 * b + c) := by rw [h]
      _ = 0 := by lib_int)
  calc a = (a - b) + b := by lib_int
    _ = b := by rw [h0]; lib_int

/-- a double sum of δ(a, a0)·δ(y, y0)·F picks F(a0, y0) when (a0, y0) is in the box. -/
theorem pt_sum (N : Nat) (F : Int → Int → Int) (a0 y0 : Int) (ha : Int.natAbs a0 ≤ N) (hy : Int.natAbs y0 ≤ N) :
    sumK Z (fun a => sumK Z (fun y => Z.mul (Z.mul (delta Z a a0) (delta Z y y0)) (F a y)) (zl N)) (zl N) = F a0 y0 := by
  have hin : ∀ a, sumK Z (fun y => Z.mul (Z.mul (delta Z a a0) (delta Z y y0)) (F a y)) (zl N) = Z.mul (delta Z a a0) (F a y0) := fun a => by
    rw [sumK_ext Z _ (fun y => Z.mul (delta Z y y0) (Z.mul (delta Z a a0) (F a y))) (fun y => by lib_ring),
      delta_sum, count_zl Z y0 N hy]
    lib_ring
  rw [sumK_ext Z _ _ hin, delta_sum, count_zl Z a0 N ha]
  lib_ring

theorem zero_sum (N : Nat) (F : Int → Int → Int) (h : ∀ a y, F a y = Z.zero) :
    sumK Z (fun a => sumK Z (fun y => F a y) (zl N)) (zl N) = Z.zero := by
  rw [sumK_ext Z _ (fun _ => Z.zero) (fun a => by rw [sumK_ext Z _ (fun _ => Z.zero) (fun y => h a y)]; exact HeckePrime.zsumR Z _)]
  exact HeckePrime.zsumR Z _

/-- the exponent condition, Int side to Nat side. -/
theorem cond_nat (s m : Nat) (h : 1 + Int.ofNat m = Int.ofNat (2 * s + 1)) : m = 2 * s := by
  have h1 : 1 + m = 2 * s + 1 := Int.ofNat.inj (show Int.ofNat (1 + m) = Int.ofNat (2 * s + 1) from h)
  exact Nat.succ.inj ((Nat.add_comm m 1).trans h1)

theorem exp_of (j k : Int) (s : Nat) (hC : 18 * QBinom.Gj Hecke36.g36 k + 6 * QBinom.Gj Hecke36.g36 j = 2 * s) :
    1 + 9 * (j * j) + 3 * j + 27 * (k * k) + 9 * k = Int.ofNat (2 * s + 1) := by
  rw [← Trunc.exp27 j k, Nat.add_comm (6 * QBinom.Gj Hecke36.g36 j), hC]
  show Int.ofNat (1 + 2 * s) = Int.ofNat (2 * s + 1)
  rw [Nat.add_comm]

theorem exp_to (j k : Int) (s : Nat) (h : 1 + 9 * (j * j) + 3 * j + 27 * (k * k) + 9 * k = Int.ofNat (2 * s + 1)) :
    18 * QBinom.Gj Hecke36.g36 k + 6 * QBinom.Gj Hecke36.g36 j = 2 * s := by
  rw [← Trunc.exp27 j k] at h
  exact (Nat.add_comm _ _).trans (cond_nat s _ h)

theorem bound_of (j k : Int) (s : Nat) (h : 1 + 9 * (j * j) + 3 * j + 27 * (k * k) + 9 * k = Int.ofNat (2 * s + 1)) :
    Int.natAbs j ≤ 2 * s + 1 ∧ Int.natAbs k ≤ 2 * s + 1 :=
  Bij27wB.box_bound j k (2 * s + 1) (by rw [← h]; lib_int)

/-- **the inner identity, even case** (k = j + 2q). -/
theorem inner_even (s N : Nat) (hN : 8 * (2 * s + 1) + 1 ≤ N) (j q : Int) :
    Z.add (SE s N j (j + 2 * q)) (Z.neg (SO s N j (j + 2 * q))) =
      Z.mul (sg j) (Z.mul (sg (j + 2 * q)) (delta Z (18 * QBinom.Gj Hecke36.g36 (j + 2 * q) + 6 * QBinom.Gj Hecke36.g36 j) (2 * s))) := by
  have hO : SO s N j (j + 2 * q) = Z.zero := zero_sum N _ (fun b e => by
    unfold rO
    by_cases hr : RO (3 * b - 5 * e) (2 * e + 1) j (j + 2 * q)
    · exact absurd hr (fun hh => two_ne_one (q - e) (by
        calc 2 * (q - e) = (j + 2 * q - j) - 2 * e := by lib_int
          _ = (2 * e + 1) - 2 * e := by rw [hh.2]
          _ = 1 := by lib_int))
    · rw [ite_neg' _ _ hr]; lib_ring)
  rw [hO, show sg (j + 2 * q) = sg j from sg_even j q]
  have hsg : Z.mul (sg j) (sg j) = Z.one := sg_sq j
  have h0 : RE (3 * (2 * j + 3 * q) + 1) q j (j + 2 * q) := by
    rw [show 3 * (2 * j + 3 * q) + 1 = 1 + 6 * j + 9 * q by lib_int]
    exact RE_of_even j q
  have hpt : ∀ a y, rE (3 * a + 1) y j (j + 2 * q) = Z.mul (delta Z a (2 * j + 3 * q)) (delta Z y q) := fun a y => by
    unfold rE
    by_cases hr : RE (3 * a + 1) y j (j + 2 * q)
    · obtain ⟨hx, hy⟩ := RE_xy_uniq _ _ _ _ _ _ hr h0
      have ha : a = 2 * j + 3 * q := cancel3 a _ 1 hx
      rw [ite_pos' _ _ hr]; unfold delta; rw [ite_pos' _ _ ha, ite_pos' _ _ hy]; rfl
    · rw [ite_neg' _ _ hr]
      by_cases ha : a = 2 * j + 3 * q
      · by_cases hy : y = q
        · exact absurd (show RE (3 * a + 1) y j (j + 2 * q) by rw [ha, hy]; exact h0) hr
        · unfold delta; rw [ite_neg' _ _ hy]; show (0 : Int) = _ * 0; lib_int
      · unfold delta; rw [ite_neg' _ _ ha]; show (0 : Int) = 0 * _; lib_int
  have hval : (3 * (2 * j + 3 * q) + 1) * (3 * (2 * j + 3 * q) + 1) + 27 * (q * q) =
      1 + 9 * (j * j) + 3 * j + 27 * ((j + 2 * q) * (j + 2 * q)) + 9 * (j + 2 * q) := by
    rw [show 3 * (2 * j + 3 * q) + 1 = 1 + 6 * j + 9 * q by lib_int, Bij27w.even_rep j q]
  unfold SE
  have hpt' : ∀ a y, Z.mul (rE (3 * a + 1) y j (j + 2 * q)) (HE s a y) =
      Z.mul (Z.mul (delta Z a (2 * j + 3 * q)) (delta Z y q)) (HE s a y) := fun a y => by rw [hpt a y]
  rw [sumK_ext Z _ _ (fun a => sumK_ext Z _ _ (fun y => hpt' a y) _)]
  by_cases hC : 18 * QBinom.Gj Hecke36.g36 (j + 2 * q) + 6 * QBinom.Gj Hecke36.g36 j = 2 * s
  · have he := exp_of j (j + 2 * q) s hC
    obtain ⟨bj, bk⟩ := bound_of j (j + 2 * q) s he
    obtain ⟨ba, hb2⟩ := AbsTri.box_even j (j + 2 * q) q (2 * s + 1) bj bk rfl
    rw [pt_sum N (HE s) _ _ (Nat.le_trans ba (Nat.le_trans (Nat.le_succ _) hN)) (Nat.le_trans hb2 (Nat.le_trans (Nat.le_succ _) hN))]
    unfold HE; rw [ite_pos' _ _ (hval.trans he)]
    unfold delta; rw [ite_pos' _ _ hC]
    calc Z.add Z.one (Z.neg Z.zero) = Z.mul (Z.mul (sg j) (sg j)) Z.one := by rw [hsg]; lib_ring
      _ = _ := by lib_ring
  · have hz : ∀ a y, Z.mul (Z.mul (delta Z a (2 * j + 3 * q)) (delta Z y q)) (HE s a y) = Z.zero := fun a y => by
      by_cases ha : a = 2 * j + 3 * q
      · by_cases hy : y = q
        · rw [ha, hy]; unfold HE
          rw [ite_neg' _ _ (fun hh => hC (exp_to j (j + 2 * q) s (hval.symm.trans hh)))]; lib_ring
        · unfold delta; rw [ite_neg' _ _ hy]; lib_ring
      · unfold delta; rw [ite_neg' _ _ ha]; lib_ring
    rw [zero_sum N _ hz]
    unfold delta; rw [ite_neg' _ _ hC]; lib_ring

/-- **the inner identity, odd case** (k = j + 2q + 1). -/
theorem inner_odd (s N : Nat) (hN : 8 * (2 * s + 1) + 1 ≤ N) (j q : Int) :
    Z.add (SE s N j (j + 2 * q + 1)) (Z.neg (SO s N j (j + 2 * q + 1))) =
      Z.mul (sg j) (Z.mul (sg (j + 2 * q + 1)) (delta Z (18 * QBinom.Gj Hecke36.g36 (j + 2 * q + 1) + 6 * QBinom.Gj Hecke36.g36 j) (2 * s))) := by
  have hE : SE s N j (j + 2 * q + 1) = Z.zero := zero_sum N _ (fun a y => by
    unfold rE
    by_cases hr : RE (3 * a + 1) y j (j + 2 * q + 1)
    · exact absurd hr (fun hh => two_ne_one (y - q) (by
        calc 2 * (y - q) = 2 * y - 2 * q := by lib_int
          _ = (j + 2 * q + 1 - j) - 2 * q := by rw [hh.2]
          _ = 1 := by lib_int))
    · rw [ite_neg' _ _ hr]; lib_ring)
  rw [hE, show sg (j + 2 * q + 1) = Z.neg (sg j) by rw [sg_succ, sg_even]]
  have hsg : Z.mul (sg j) (sg j) = Z.one := sg_sq j
  have h0 : RO (3 * (-1 - j) - 5 * q) (2 * q + 1) j (j + 2 * q + 1) := by
    rw [show 3 * (-1 - j) - 5 * q = -3 - 3 * j - 5 * q by lib_int]
    exact RO_of_odd j q
  have hpt : ∀ b e, rO (3 * b - 5 * e) (2 * e + 1) j (j + 2 * q + 1) = Z.mul (delta Z b (-1 - j)) (delta Z e q) := fun b e => by
    unfold rO
    by_cases hr : RO (3 * b - 5 * e) (2 * e + 1) j (j + 2 * q + 1)
    · obtain ⟨hx, hy⟩ := RO_xy_uniq _ _ _ _ _ _ hr h0
      have he0 : e - q = 0 := two_cancel _ (by
        calc 2 * (e - q) = (2 * e + 1) - (2 * q + 1) := by lib_int
          _ = (2 * q + 1) - (2 * q + 1) := by rw [hy]
          _ = 0 := by lib_int)
      have he : e = q := by
        calc e = (e - q) + q := by lib_int
          _ = q := by rw [he0]; lib_int
      have hb : b = -1 - j := cancel3 b _ (-(5 * q)) (by
        calc 3 * b + -(5 * q) = 3 * b - 5 * e := by rw [he]; lib_int
          _ = 3 * (-1 - j) - 5 * q := hx
          _ = 3 * (-1 - j) + -(5 * q) := by lib_int)
      rw [ite_pos' _ _ hr]; unfold delta; rw [ite_pos' _ _ hb, ite_pos' _ _ he]; rfl
    · rw [ite_neg' _ _ hr]
      by_cases hb : b = -1 - j
      · by_cases he : e = q
        · exact absurd (show RO (3 * b - 5 * e) (2 * e + 1) j (j + 2 * q + 1) by rw [hb, he]; exact h0) hr
        · unfold delta; rw [ite_neg' _ _ he]; show (0 : Int) = _ * 0; lib_int
      · unfold delta; rw [ite_neg' _ _ hb]; show (0 : Int) = 0 * _; lib_int
  have hval : 4 * ((3 * (-1 - j) - 5 * q) * (3 * (-1 - j) - 5 * q)) + 2 * ((3 * (-1 - j) - 5 * q) * (2 * q + 1)) +
      7 * ((2 * q + 1) * (2 * q + 1)) =
      1 + 9 * (j * j) + 3 * j + 27 * ((j + 2 * q + 1) * (j + 2 * q + 1)) + 9 * (j + 2 * q + 1) := by
    rw [show 3 * (-1 - j) - 5 * q = -3 - 3 * j - 5 * q by lib_int, Bij27w.odd_rep j q]
  have hpt' : ∀ b e, Z.mul (rO (3 * b - 5 * e) (2 * e + 1) j (j + 2 * q + 1)) (HO s b e) =
      Z.mul (Z.mul (delta Z b (-1 - j)) (delta Z e q)) (HO s b e) := fun b e => by rw [hpt b e]
  unfold SO
  rw [sumK_ext Z _ _ (fun b => sumK_ext Z _ _ (fun e => hpt' b e) _)]
  by_cases hC : 18 * QBinom.Gj Hecke36.g36 (j + 2 * q + 1) + 6 * QBinom.Gj Hecke36.g36 j = 2 * s
  · have hx := exp_of j (j + 2 * q + 1) s hC
    obtain ⟨bj, bk⟩ := bound_of j (j + 2 * q + 1) s hx
    obtain ⟨hb1, hb2⟩ := AbsTri.box_odd j (j + 2 * q + 1) q (2 * s + 1) bj bk rfl
    rw [pt_sum N (HO s) _ _ (Nat.le_trans hb1 hN) (Nat.le_trans hb2 hN)]
    unfold HO; rw [ite_pos' _ _ (hval.trans hx)]
    unfold delta; rw [ite_pos' _ _ hC]
    calc Z.add Z.zero (Z.neg Z.one) = Z.mul (Z.mul (sg j) (sg j)) (Z.neg Z.one) := by rw [hsg]; lib_ring
      _ = _ := by lib_ring
  · have hz : ∀ b e, Z.mul (Z.mul (delta Z b (-1 - j)) (delta Z e q)) (HO s b e) = Z.zero := fun b e => by
      by_cases hb : b = -1 - j
      · by_cases he : e = q
        · rw [hb, he]; unfold HO
          rw [ite_neg' _ _ (fun hh => hC (exp_to j (j + 2 * q + 1) s (hval.symm.trans hh)))]; lib_ring
        · unfold delta; rw [ite_neg' _ _ he]; lib_ring
      · unfold delta; rw [ite_neg' _ _ hb]; lib_ring
    rw [zero_sum N _ hz]
    unfold delta; rw [ite_neg' _ _ hC]; lib_ring

end All27w

#print axioms All27w.two_ne_one
#print axioms All27w.three_zero
#print axioms All27w.cancel3
#print axioms All27w.pt_sum
#print axioms All27w.zero_sum
#print axioms All27w.cond_nat
#print axioms All27w.exp_of
#print axioms All27w.exp_to
#print axioms All27w.bound_of
#print axioms All27w.inner_even
#print axioms All27w.inner_odd

lang/All27wF.lean

/-
  lang/All27wF — T47, cycle 827: **27a, the weight-one factor at every odd exponent**:
    [q^(2s+1)] η(6z)η(18z) = #{(a, y) : (3a + 1)² + 27y² = 2s + 1} − #{(b, e) : 4x² + 2xy + 7y² = 2s + 1, x = 3b − 5e, y = 2e + 1}
  (counts over the box zl(8(2s + 1) + 1)²; i.e. x² + 27y² with x ≡ 1 (3), minus 4x² + 2xy + 7y² with y odd, x + y ≡ 1 (3)).
    inner_jk     parity split of k − j (ColCert10.ediv) into All27wI.inner_even / inner_odd
    colE, colO   for each (a, y) resp. (b, e) the (j, k)-count is 1 or the indicator is 0 (count_even, count_odd)
    swap4, sumK_neg
    eta27w_odd   the theorem
  Lean 4 core only, proofs by hand (lib_int, lib_ring, lib_nat), no axioms.
-/
import lang.All27wI
import lib.RingTac
import lib.Semi
import lib.IntRing

namespace All27w

open DetChain YMChain FiniteRecChain HeckeMult

theorem inner_jk (s N : Nat) (hN : 8 * (2 * s + 1) + 1 ≤ N) (j k : Int) :
    Z.add (SE s N j k) (Z.neg (SO s N j k)) =
      Z.mul (sg j) (Z.mul (sg k) (delta Z (18 * QBinom.Gj Hecke36.g36 k + 6 * QBinom.Gj Hecke36.g36 j) (2 * s))) := by
  obtain ⟨q, r, t, hrt, hq⟩ := ColCert10.ediv 1 (k - j)
  cases r with
  | zero =>
    have hk : k = j + 2 * q := by
      calc k = (k - j) + j := by lib_int
        _ = ((0 : Nat) : Int) + ((1 + 1 : Nat) : Int) * q + j := by rw [hq]
        _ = j + 2 * q := by show (0 : Int) + 2 * q + j = _; lib_int
    rw [hk]; exact inner_even s N hN j q
  | succ r =>
    cases r with
    | zero =>
      have hk : k = j + 2 * q + 1 := by
        calc k = (k - j) + j := by lib_int
          _ = ((0 + 1 : Nat) : Int) + ((1 + 1 : Nat) : Int) * q + j := by rw [hq]
          _ = j + 2 * q + 1 := by show (1 : Int) + 2 * q + j = _; lib_int
      rw [hk]; exact inner_odd s N hN j q
    | succ r' =>
      exfalso
      have h2 : r' + t + 1 = 0 := Nat.succ.inj ((show r' + t + 1 + 1 = r' + 1 + 1 + t by lib_nat).trans hrt)
      nomatch h2

theorem colE (s M : Nat) (hM : 2 * s + 1 ≤ 2 * M) (a y : Int) :
    sumK Z (fun j => sumK Z (fun k => Z.mul (rE (3 * a + 1) y j k) (HE s a y)) (zl (2 * M))) (zl (2 * M)) = HE s a y := by
  by_cases h : (3 * a + 1) * (3 * a + 1) + 27 * (y * y) = Int.ofNat (2 * s + 1)
  · rw [sumK_ext Z _ _ (fun j => (sumK_mul Z (HE s a y) (fun k => rE (3 * a + 1) y j k) (zl (2 * M))).symm),
      ← sumK_mul, count_even s (2 * M) a y h hM]
    lib_ring
  · unfold HE; rw [ite_neg' _ _ h]
    exact zero_sum (2 * M) _ (fun j k => by lib_ring)

theorem colO (s M : Nat) (hM : 2 * s + 1 ≤ 2 * M) (b e : Int) :
    sumK Z (fun j => sumK Z (fun k => Z.mul (rO (3 * b - 5 * e) (2 * e + 1) j k) (HO s b e)) (zl (2 * M))) (zl (2 * M)) = HO s b e := by
  by_cases h : 4 * ((3 * b - 5 * e) * (3 * b - 5 * e)) + 2 * ((3 * b - 5 * e) * (2 * e + 1)) + 7 * ((2 * e + 1) * (2 * e + 1)) = Int.ofNat (2 * s + 1)
  · rw [sumK_ext Z _ _ (fun j => (sumK_mul Z (HO s b e) (fun k => rO (3 * b - 5 * e) (2 * e + 1) j k) (zl (2 * M))).symm),
      ← sumK_mul, count_odd s (2 * M) b e h hM]
    lib_ring
  · unfold HO; rw [ite_neg' _ _ h]
    exact zero_sum (2 * M) _ (fun j k => by lib_ring)

theorem swap4 (G : Int → Int → Int → Int → Int) (L1 L2 L3 L4 : List Int) :
    sumK Z (fun j => sumK Z (fun k => sumK Z (fun a => sumK Z (fun y => G j k a y) L4) L3) L2) L1 =
      sumK Z (fun a => sumK Z (fun y => sumK Z (fun j => sumK Z (fun k => G j k a y) L2) L1) L4) L3 := by
  have e1 : ∀ j, sumK Z (fun k => sumK Z (fun a => sumK Z (fun y => G j k a y) L4) L3) L2 =
      sumK Z (fun a => sumK Z (fun y => sumK Z (fun k => G j k a y) L2) L4) L3 := fun j => by
    rw [sumK_swap Z (fun k a => sumK Z (fun y => G j k a y) L4) L3 L2]
    exact sumK_ext Z _ _ (fun a => sumK_swap Z (fun k y => G j k a y) L4 L2) L3
  rw [sumK_ext Z _ _ e1, sumK_swap Z (fun j a => sumK Z (fun y => sumK Z (fun k => G j k a y) L2) L4) L3 L1]
  exact sumK_ext Z _ _ (fun a => sumK_swap Z (fun j y => sumK Z (fun k => G j k a y) L2) L4 L1) L3

theorem sumK_neg {A : Type} (f : A → Int) : ∀ l : List A, sumK Z (fun a => Z.neg (f a)) l = Z.neg (sumK Z f l)
  | [] => by show Z.zero = Z.neg Z.zero; lib_ring
  | a :: l => by
    show Z.add (Z.neg (f a)) (sumK Z (fun a => Z.neg (f a)) l) = Z.neg (Z.add (f a) (sumK Z f l))
    rw [sumK_neg f l]; lib_ring

/-- the counts. -/
def cE (s N : Nat) : Int := sumK Z (fun a => sumK Z (fun y => HE s a y) (zl N)) (zl N)
def cO (s N : Nat) : Int := sumK Z (fun b => sumK Z (fun e => HO s b e) (zl N)) (zl N)

/-- **27a: [q^(2s+1)] η(6z)η(18z) = cE − cO.** -/
theorem eta27w_odd (s : Nat) :
    Trunc.nth Z (2 * s + 2) ((Trunc.TR Z (2 * s + 2)).mul (Trunc.qe Z (2 * s + 2)) ((Trunc.TR Z (2 * s + 2)).mul
      (QBinom.pp (Trunc.TR Z (2 * s + 2)) (Frobenius.pw (Trunc.TR Z (2 * s + 2)) (Trunc.qe Z (2 * s + 2)) 6) (2 * s + 2))
      (QBinom.pp (Trunc.TR Z (2 * s + 2)) (Frobenius.pw (Trunc.TR Z (2 * s + 2)) (Trunc.qe Z (2 * s + 2)) 18) (2 * s + 2)))) (2 * s + 1) =
    Z.add (cE s (8 * (2 * s + 1) + 1)) (Z.neg (cO s (8 * (2 * s + 1) + 1))) := by
  have hN : 8 * (2 * s + 1) + 1 ≤ 8 * (2 * s + 1) + 1 := Nat.le_refl _
  have hM : 2 * s + 1 ≤ 2 * (2 * s + 2) := Nat.le_trans (Nat.le_succ _) (Nat.le_trans (Nat.le_add_left _ (2 * s + 2)) (Nat.le_of_eq (by lib_nat)))
  rw [Hecke36.dbl27 (2 * s + 2) (2 * s) (Nat.lt_succ_self _)]
  unfold Hecke36.dbl
  have hj : ∀ j, Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs j))
      (sumK Z (fun k => Z.mul (Frobenius.pw Z (Z.neg Z.one) (Int.natAbs k))
        (delta Z (18 * QBinom.Gj Hecke36.g36 k + 6 * QBinom.Gj Hecke36.g36 j) (2 * s))) (zl (2 * (2 * s + 2)))) =
      sumK Z (fun k => Z.add (SE s (8 * (2 * s + 1) + 1) j k) (Z.neg (SO s (8 * (2 * s + 1) + 1) j k))) (zl (2 * (2 * s + 2))) := fun j => by
    rw [mul_sumK]
    exact sumK_ext Z _ _ (fun k => (inner_jk s (8 * (2 * s + 1) + 1) hN j k).symm) (zl (2 * (2 * s + 2)))
  rw [sumK_ext Z _ _ hj]
  rw [sumK_ext Z _ _ (fun j => sumK_add Z (fun k => SE s (8 * (2 * s + 1) + 1) j k) (fun k => Z.neg (SO s (8 * (2 * s + 1) + 1) j k)) (zl (2 * (2 * s + 2)))), sumK_add,
    sumK_ext Z _ _ (fun j => sumK_neg (fun k => SO s (8 * (2 * s + 1) + 1) j k) (zl (2 * (2 * s + 2)))), sumK_neg]
  have hE : sumK Z (fun j => sumK Z (fun k => SE s (8 * (2 * s + 1) + 1) j k) (zl (2 * (2 * s + 2)))) (zl (2 * (2 * s + 2))) = cE s (8 * (2 * s + 1) + 1) := by
    unfold SE
    rw [swap4 (fun j k a y => Z.mul (rE (3 * a + 1) y j k) (HE s a y)) (zl (2 * (2 * s + 2))) (zl (2 * (2 * s + 2))) (zl (8 * (2 * s + 1) + 1)) (zl (8 * (2 * s + 1) + 1))]
    exact sumK_ext Z _ _ (fun a => sumK_ext Z _ _ (fun y => colE s (2 * s + 2) hM a y) (zl (8 * (2 * s + 1) + 1))) (zl (8 * (2 * s + 1) + 1))
  have hO : sumK Z (fun j => sumK Z (fun k => SO s (8 * (2 * s + 1) + 1) j k) (zl (2 * (2 * s + 2)))) (zl (2 * (2 * s + 2))) = cO s (8 * (2 * s + 1) + 1) := by
    unfold SO
    rw [swap4 (fun j k b e => Z.mul (rO (3 * b - 5 * e) (2 * e + 1) j k) (HO s b e)) (zl (2 * (2 * s + 2))) (zl (2 * (2 * s + 2))) (zl (8 * (2 * s + 1) + 1)) (zl (8 * (2 * s + 1) + 1))]
    exact sumK_ext Z _ _ (fun b => sumK_ext Z _ _ (fun e => colO s (2 * s + 2) hM b e) (zl (8 * (2 * s + 1) + 1))) (zl (8 * (2 * s + 1) + 1))
  rw [hE, hO]

end All27w

#print axioms All27w.inner_jk
#print axioms All27w.colE
#print axioms All27w.colO
#print axioms All27w.swap4
#print axioms All27w.sumK_neg
#print axioms All27w.eta27w_odd

#print axioms: 39 theorems, no axioms.

78.365 The converse of the Stickelberger divisibility

Past the critical degree the reduced Jacobi sum is a nonzero binomial coefficient in \(\mathbb F_p\), and exponents reduce modulo \(p-1\).

\[ 1\le a,\ a+k=p-1,\ l\le p-2\ \Rightarrow \ \textstyle \sum _{x\in \mathbb F_p}x^a(1-x)^{k+l}=(-1)^{k+1}\binom {k+l}{k}\ne 0. \]

lang/StickConv.lean

/-
  lang/StickConv — T53, cycle 828 (Stickelberger converse, step 2): **the exact value of Σ_x x^a (1 − x)^b past p − 1**:
  for 1 ≤ a, a + k = p − 1, l ≤ p − 2,
    Σ_x x^a (1 − x)^(k+l) = (−1)^k · (−C(k + l, k))   in 𝔽_p,
  so with ChooseFac.C_ndv (k + l < p) it is nonzero: P_t ∤ J(χ^a', χ^b') when ⟨a't/ℓ⟩ + ⟨b't/ℓ⟩ ≥ 1.
    pw_one1, listC_app (Frobenius.len_listC reused), U_app, U_top, conv
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.Stick1
import lang.ChooseFac
import lib.RingTac
import lib.Semi

namespace StickConv

open FTAChain ZMod YMChain Frobenius Stick1

theorem pw_one1 {R : Type} (K : DetChain.CRing R) : ∀ n : Nat, pw K K.one n = K.one
  | 0 => rfl
  | n + 1 => by
    show K.mul (pw K K.one n) K.one = K.one
    rw [pw_one1 K n]; lib_ring

theorem listC_app (n r : Nat) : ∀ (k m : Nat), listC n k (m + r) = listC n k m ++ listC n (k + m) r
  | k, 0 => by rw [Nat.zero_add, Nat.add_zero]; try rfl
  | k, m + 1 => by
    rw [show m + 1 + r = (m + r) + 1 by lib_nat]
    show C n k :: listC n (k + 1) (m + r) = C n k :: (listC n (k + 1) m ++ listC n (k + (m + 1)) r)
    rw [listC_app n r (k + 1) m, show k + 1 + m = k + (m + 1) by lib_nat]

variable (J : Nat)

theorem U_app (rest : List Nat) : ∀ (pre : List Nat) (e : Nat), (∀ i, i < pre.length → HasseCM.ps J (e + i) = (ring (J + 1)).zero) →
    U J e (pre ++ rest) = (ring (J + 1)).mul (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) pre.length) (U J (e + pre.length) rest)
  | [], e, _ => by
    show U J e rest = (ring (J + 1)).mul (ring (J + 1)).one (U J (e + 0) rest)
    show U J e rest = (ring (J + 1)).mul (ring (J + 1)).one (U J e rest)
    lib_ring
  | c :: pre, e, h => by
    have h0 : HasseCM.ps J e = (ring (J + 1)).zero := h 0 (Nat.zero_lt_succ _)
    show U J e (c :: (pre ++ rest)) = _
    rw [U_cons, h0, U_app rest pre (e + 1) (fun i hi => by
      rw [show e + 1 + i = e + (i + 1) by lib_nat]; exact h (i + 1) (Nat.succ_lt_succ hi)),
      show e + 1 + pre.length = e + (c :: pre).length by show e + 1 + pre.length = e + (pre.length + 1); lib_nat]
    show _ = (ring (J + 1)).mul ((ring (J + 1)).mul (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) pre.length)
      ((ring (J + 1)).neg (ring (J + 1)).one)) _
    lib_ring

theorem U_top (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (c : Nat) (rest : List Nat) (hl : rest.length ≤ J) :
    U J (J + 1) (c :: rest) = (ring (J + 1)).neg (nsm (ring (J + 1)) c (ring (J + 1)).one) := by
  rw [U_cons, HasseCM.ps_top J hpr, pw_one1, U_zero J rest (J + 1 + 1) (fun i hi => by
    rw [show J + 1 + 1 + i = J + 1 + (i + 1) by lib_nat]
    exact HasseCM.ps_high J hpr (i + 1) (Nat.succ_le_succ (Nat.zero_le i)) (Nat.le_trans hi hl))]
  lib_ring

/-- **Σ_x x^a (1 − x)^(k+l) = (−1)^k · (−C(k + l, k))** for 1 ≤ a, a + k = p − 1, l ≤ p − 2. -/
theorem conv (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (a k l : Nat) (ha : 1 ≤ a) (hak : a + k = J + 1) (hl : l ≤ J) :
    sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x a)
      (pw (ring (J + 1)) ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one) (k + l)))
      (CMInert.allZ (J + 1) (J + 1 + 1)) =
    (ring (J + 1)).mul (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) k)
      ((ring (J + 1)).neg (nsm (ring (J + 1)) (C (k + l) k) (ring (J + 1)).one)) := by
  have pt : ∀ x : Zp (J + 1), (ring (J + 1)).mul (pw (ring (J + 1)) x a)
      (pw (ring (J + 1)) ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one) (k + l)) =
      (ring (J + 1)).mul (pw (ring (J + 1)) x a) (ev (ring (J + 1)) ((ring (J + 1)).neg x) (ring (J + 1)).one (row (k + l))) := fun x => by
    rw [ev_row]
  rw [sumK_ext (ring (J + 1)) _ _ pt]
  show U J a (row (k + l)) = _
  have hrow : row (k + l) = listC (k + l) 0 k ++ (C (k + l) k :: listC (k + l) (k + 1) l) := by
    rw [row_eq, show k + l + 1 = k + (l + 1) by lib_nat, listC_app (k + l) (l + 1) 0 k, Nat.zero_add]
    try rfl
  rw [hrow, U_app J _ (listC (k + l) 0 k) a (fun i hi => by
      rw [len_listC] at hi
      exact HasseCM.ps_low J hpr (a + i) (Nat.le_trans ha (Nat.le_add_right a i))
        (Nat.le_of_succ_le_succ (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans (Nat.add_le_add_left hi a) (Nat.le_of_eq hak))))),
    len_listC, hak, U_top J hpr (C (k + l) k) (listC (k + l) (k + 1) l) (by rw [len_listC]; exact hl)]

theorem negone_sq {R : Type} (K : DetChain.CRing R) : ∀ k : Nat,
    K.mul (pw K (K.neg K.one) k) (pw K (K.neg K.one) k) = K.one
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | k + 1 => by
    show K.mul (K.mul (pw K (K.neg K.one) k) (K.neg K.one)) (K.mul (pw K (K.neg K.one) k) (K.neg K.one)) = K.one
    calc K.mul (K.mul (pw K (K.neg K.one) k) (K.neg K.one)) (K.mul (pw K (K.neg K.one) k) (K.neg K.one))
        = K.mul (pw K (K.neg K.one) k) (pw K (K.neg K.one) k) := by lib_ring
      _ = K.one := negone_sq K k

/-- C(k + l, k)·1 ≠ 0 in 𝔽_p for k + l < p. -/
theorem nsm_ne (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (k l : Nat) (hkl : k + l ≤ J + 1)
    (h : nsm (ring (J + 1)) (C (k + l) k) (ring (J + 1)).one = (ring (J + 1)).zero) : False :=
  ChooseFac.C_ndv J hpr k l hkl (ZMod.md_zero_dv (J + 1) _
    ((ZMod.nsm_one_val (J + 1) (C (k + l) k)).symm.trans (congrArg Subtype.val h)))

/-- **the converse**: for 1 ≤ a, a + k = p − 1, l ≤ p − 2, Σ_x x^a (1 − x)^(k+l) ≠ 0 in 𝔽_p (P_t ∤ J). -/
theorem conv_ne (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (a k l : Nat) (ha : 1 ≤ a) (hak : a + k = J + 1)
    (hl : l ≤ J) (hkl : k + l ≤ J + 1)
    (h0 : sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x a)
      (pw (ring (J + 1)) ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one) (k + l)))
      (CMInert.allZ (J + 1) (J + 1 + 1)) = (ring (J + 1)).zero) : False := by
  rw [conv J hpr a k l ha hak hl] at h0
  apply nsm_ne J hpr k l hkl
  calc nsm (ring (J + 1)) (C (k + l) k) (ring (J + 1)).one
      = (ring (J + 1)).neg ((ring (J + 1)).mul ((ring (J + 1)).mul (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) k)
          (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) k))
          ((ring (J + 1)).neg (nsm (ring (J + 1)) (C (k + l) k) (ring (J + 1)).one))) := by
        rw [negone_sq]; lib_ring
    _ = (ring (J + 1)).neg ((ring (J + 1)).mul (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) k)
          ((ring (J + 1)).mul (pw (ring (J + 1)) ((ring (J + 1)).neg (ring (J + 1)).one) k)
            ((ring (J + 1)).neg (nsm (ring (J + 1)) (C (k + l) k) (ring (J + 1)).one)))) := by lib_ring
    _ = (ring (J + 1)).zero := by rw [h0]; lib_ring

end StickConv

#print axioms StickConv.pw_one1
#print axioms StickConv.listC_app
#print axioms StickConv.U_app
#print axioms StickConv.U_top
#print axioms StickConv.conv
#print axioms StickConv.negone_sq
#print axioms StickConv.nsm_ne
#print axioms StickConv.conv_ne

lang/StickRed.lean

/-
  lang/StickRed — T53, cycle 828 (step 3): **exponents of the 𝔽_p-valued characters reduce mod p − 1** (ZMod.fermat, x^p = x,
  for every x including 0), so the Jacobi sum of ψ^A, ψ^B (ψ(x) = x^m) is the Stick1/StickConv sum with reduced exponents:
    red1     x^(A+1 + (p−1)) = x^(A+1)
    redq     x^(A+1 + (p−1)q) = x^(A+1)
    jac_red  Σ_x x^(A+1 + (p−1)q) (1 − x)^(B+1 + (p−1)r) = Σ_x x^(A+1) (1 − x)^(B+1)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.StickConv
import lib.RingTac
import lib.Semi

namespace StickRed

open FTAChain ZMod YMChain Frobenius

variable (J : Nat)

theorem red1 (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (A : Nat) (x : Zp (J + 1)) :
    pw (ring (J + 1)) x (A + 1 + (J + 1)) = pw (ring (J + 1)) x (A + 1) := by
  rw [show A + 1 + (J + 1) = A + (J + 1 + 1) by lib_nat, HasseCM.pw_add' (ring (J + 1)) x A (J + 1 + 1), ZMod.fermat J hpr x]
  rfl

theorem redq (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (A : Nat) (x : Zp (J + 1)) :
    ∀ q : Nat, pw (ring (J + 1)) x (A + 1 + (J + 1) * q) = pw (ring (J + 1)) x (A + 1)
  | 0 => by rw [show A + 1 + (J + 1) * 0 = A + 1 by lib_nat]
  | q + 1 => by
    rw [show A + 1 + (J + 1) * (q + 1) = (A + (J + 1) * q) + 1 + (J + 1) by lib_nat, red1 J hpr (A + (J + 1) * q) x,
      show A + (J + 1) * q + 1 = A + 1 + (J + 1) * q by lib_nat]
    exact redq hpr A x q

theorem jac_red (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (A B q r : Nat) :
    sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x (A + 1 + (J + 1) * q))
      (pw (ring (J + 1)) ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one) (B + 1 + (J + 1) * r)))
      (CMInert.allZ (J + 1) (J + 1 + 1)) =
    sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x (A + 1))
      (pw (ring (J + 1)) ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one) (B + 1)))
      (CMInert.allZ (J + 1) (J + 1 + 1)) :=
  sumK_ext (ring (J + 1)) _ _ (fun x => by rw [redq J hpr A x q, redq J hpr B _ r]) _

end StickRed

#print axioms StickRed.red1
#print axioms StickRed.redq
#print axioms StickRed.jac_red

#print axioms: 11 theorems, no axioms.

78.366 Stickelberger’s divisibility for Jacobi sums of order \(\ell \)

For \(p-1=\ell n\) and a ring map \(\iota \) to \(\mathbb F_p\) sending \(\zeta \) to \(w^t\) (the prime \(P_t\)), the Jacobi sum lies in \(P_t\) exactly when \(2t<\ell \).

\[ \iota \big (J(\varphi ,\varphi )\big )=\textstyle \sum _x x^{tn}(1-x)^{tn}=0\iff 2t<\ell ;\qquad \sum _x x^{\alpha n}(1-x)^{\beta n}=0\iff \alpha +\beta <\ell . \]

lang/StickL.lean

/-
  lang/StickL — T53, cycle 828 (step 4): **Stickelberger's divisibility for J(χ^t, χ^t) in ℓ-form**.
  p − 1 = ℓn, ψ_t(x) = x^(tn) (χ^t reduced modulo the prime P_t of ℤ[ζ_ℓ] above p):
    stick_lt   2t + 1 ≤ ℓ, 1 ≤ t, 1 ≤ n  ⇒  Σ_x ψ_t(x) ψ_t(1 − x) = 0            (P_t | J(χ^t, χ^t))
    stick_gt   ℓ = 2s + r, t = s + r (2t > ℓ), 1 ≤ s, r, n  ⇒  Σ_x ψ_t(x) ψ_t(1 − x) ≠ 0   (P_t ∤ J(χ^t, χ^t))
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.StickRed
import lib.Semi

namespace StickL

open FTAChain ZMod YMChain Frobenius

variable (J : Nat)

theorem stick_lt (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (l n t : Nat) (hp : J + 1 = l * n)
    (ht : 2 * t + 1 ≤ l) (h1 : 1 ≤ t) (hn : 1 ≤ n) :
    sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x (t * n))
      (pw (ring (J + 1)) ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one) (t * n)))
      (CMInert.allZ (J + 1) (J + 1 + 1)) = (ring (J + 1)).zero := by
  have hb : (2 * t + 1) * n ≤ J + 1 := by rw [hp]; exact Nat.mul_le_mul_right n ht
  have h2 : t * n + t * n + 1 ≤ J + 1 :=
    Nat.le_trans (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.add_le_add_left hn (t * n + t * n))) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) hb)
  exact Stick1.jac_gen J hpr (t * n) (t * n) (Nat.le_trans h1 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one t).symm) (Nat.mul_le_mul_left t hn)))
    (Nat.le_of_succ_le_succ h2)

theorem stick_gt (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (s r n : Nat) (hp : J + 1 = (2 * s + r) * n)
    (hs : 1 ≤ s) (_hr : 1 ≤ r) (hn : 1 ≤ n)
    (h0 : sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x ((s + r) * n))
      (pw (ring (J + 1)) ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one) ((s + r) * n)))
      (CMInert.allZ (J + 1) (J + 1 + 1)) = (ring (J + 1)).zero) : False := by
  have hsplit : (s + r) * n = s * n + r * n := by lib_nat
  rw [hsplit] at h0
  have hak : s * n + r * n + s * n = J + 1 := by rw [hp]; lib_nat
  have ha : 1 ≤ s * n + r * n :=
    Nat.le_trans hs (Nat.le_trans (Nat.le_trans (Nat.le_of_eq (Nat.mul_one s).symm) (Nat.mul_le_mul_left s hn)) (Nat.le_add_right _ _))
  -- r·n < (2s + r)·n = p − 1, since s·n ≥ 1
  have hsn : 1 ≤ s * n := Nat.le_trans hs (Nat.le_trans (Nat.le_of_eq (Nat.mul_one s).symm) (Nat.mul_le_mul_left s hn))
  have h5 : r * n + s * n + s * n = J + 1 := by rw [hp]; lib_nat
  have hl : r * n ≤ J := Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left hsn (r * n))
    (Nat.le_trans (Nat.le_add_right (r * n + s * n) (s * n)) (Nat.le_of_eq h5)))
  have hkl : s * n + r * n ≤ J + 1 := Nat.le_trans (Nat.le_add_right _ (s * n)) (Nat.le_of_eq hak)
  exact StickConv.conv_ne J hpr (s * n + r * n) (s * n) (r * n) ha hak hl hkl h0

/-- **general pair, vanishing side**: α + β + 1 ≤ ℓ ⇒ Σ_x x^(αn)(1 − x)^(βn) = 0 (P | J(χ^α, χ^β) when ⟨α/ℓ⟩ + ⟨β/ℓ⟩ < 1). -/
theorem stick_ab_lt (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (l n α β : Nat) (hp : J + 1 = l * n)
    (hab : α + β + 1 ≤ l) (h1 : 1 ≤ α) (hn : 1 ≤ n) :
    sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x (α * n))
      (pw (ring (J + 1)) ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one) (β * n)))
      (CMInert.allZ (J + 1) (J + 1 + 1)) = (ring (J + 1)).zero := by
  have hb : (α + β + 1) * n ≤ J + 1 := by rw [hp]; exact Nat.mul_le_mul_right n hab
  have h2 : α * n + β * n + 1 ≤ J + 1 :=
    Nat.le_trans (Nat.add_le_add_left hn (α * n + β * n)) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) hb)
  exact Stick1.jac_gen J hpr (α * n) (β * n) (Nat.le_trans h1 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one α).symm) (Nat.mul_le_mul_left α hn)))
    (Nat.le_of_succ_le_succ h2)

/-- **general pair, non-vanishing side**: ℓ = α + κ, β = κ + μ ≤ ℓ − 1 (so ℓ ≤ α + β < 2ℓ) ⇒ Σ_x x^(αn)(1 − x)^(βn) ≠ 0. -/
theorem stick_ab_ge (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (α κ μ n : Nat) (hp : J + 1 = (α + κ) * n)
    (h1 : 1 ≤ α) (hβ : κ + μ + 1 ≤ α + κ) (hn : 1 ≤ n)
    (h0 : sumK (ring (J + 1)) (fun x => (ring (J + 1)).mul (pw (ring (J + 1)) x (α * n))
      (pw (ring (J + 1)) ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one) ((κ + μ) * n)))
      (CMInert.allZ (J + 1) (J + 1 + 1)) = (ring (J + 1)).zero) : False := by
  rw [show (κ + μ) * n = κ * n + μ * n by lib_nat] at h0
  have hak : α * n + κ * n = J + 1 := by rw [hp]; lib_nat
  have ha : 1 ≤ α * n := Nat.le_trans h1 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one α).symm) (Nat.mul_le_mul_left α hn))
  have hb : (κ + μ + 1) * n ≤ J + 1 := by rw [hp]; exact Nat.mul_le_mul_right n hβ
  have hb' : κ * n + μ * n + 1 ≤ J + 1 :=
    Nat.le_trans (Nat.add_le_add_left hn (κ * n + μ * n)) (Nat.le_trans (Nat.le_of_eq (by lib_nat)) hb)
  have hkl : κ * n + μ * n ≤ J + 1 := Nat.le_trans (Nat.le_succ _) hb'
  have hl : μ * n ≤ J := Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_right (Nat.le_add_left (μ * n) (κ * n)) 1) hb')
  exact StickConv.conv_ne J hpr (α * n) (κ * n) (μ * n) ha hak hl hkl h0

end StickL

#print axioms StickL.stick_lt
#print axioms StickL.stick_gt
#print axioms StickL.stick_ab_lt
#print axioms StickL.stick_ab_ge

lang/StickHom.lean

/-
  lang/StickHom — T53, cycle 828 (step 5): **reduction of a Jacobi sum through a ring map** ι : R → F
  (R ∋ ζ, e.g. ℤ[ζ_ℓ]; F = 𝔽_p; ι(ζ) = w^t is the prime P_t):
    map_sumK     ι(Σ f) = Σ ι∘f
    map_pw       ι(aⁿ) = ι(a)ⁿ
    pw_swap      (w^t)^j = (w^j)^t
    red_root     ι(ζ) = w^t ⇒ ι(ζ^j) = (w^j)^t          (the character value ζ^(ind) goes to (x^n)^t = x^(tn))
    red_jac      ι∘φ = (·)^m on F ⇒ ι(Σ_x φ(x)φ(1 − x)) = Σ_x x^m (1 − x)^m      (then Stick1/StickL decide P_t | J)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.StickL
import lib.RingTac

namespace StickHom

open YMChain Frobenius

variable {R F : Type} (T : DetChain.CRing R) (K : DetChain.CRing F) (ι : R → F)

theorem map_sumK {A : Type} (hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (hzero : ι T.zero = K.zero) (f : A → R) :
    ∀ l : List A, ι (sumK T f l) = sumK K (fun a => ι (f a)) l
  | [] => hzero
  | a :: l => by
    show ι (T.add (f a) (sumK T f l)) = K.add (ι (f a)) (sumK K (fun a => ι (f a)) l)
    rw [hadd, map_sumK hadd hzero f l]

theorem map_pw (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hone : ι T.one = K.one) (a : R) :
    ∀ n : Nat, ι (pw T a n) = pw K (ι a) n
  | 0 => hone
  | n + 1 => by
    show ι (T.mul (pw T a n) a) = K.mul (pw K (ι a) n) (ι a)
    rw [hmul, map_pw hmul hone a n]

theorem pw_swap (w : F) (t : Nat) : ∀ j : Nat, pw K (pw K w t) j = pw K (pw K w j) t
  | 0 => by
    show K.one = pw K K.one t
    exact (StickConv.pw_one1 K t).symm
  | j + 1 => by
    show K.mul (pw K (pw K w t) j) (pw K w t) = pw K (K.mul (pw K w j) w) t
    rw [pw_mul K, pw_swap w t j]

/-- **the root goes to the t-th power**: ι(ζ) = w^t ⇒ ι(ζ^j) = (w^j)^t. -/
theorem red_root (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hone : ι T.one = K.one) (ζ : R) (w : F) (t : Nat)
    (hζ : ι ζ = pw K w t) (j : Nat) : ι (pw T ζ j) = pw K (pw K w j) t := by
  rw [map_pw T K ι hmul hone ζ j, hζ, pw_swap K w t j]

/-- **the Jacobi sum reduces to the 𝔽_p sum**: if ι(φ(x)) = x^m for every x, then
    ι(Σ_x φ(x) φ(1 − x)) = Σ_x x^m (1 − x)^m. -/
theorem red_jac (hadd : ∀ a b, ι (T.add a b) = K.add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b))
    (hzero : ι T.zero = K.zero) (φ : F → R) (m : Nat) (hφ : ∀ x, ι (φ x) = pw K x m) (L : List F) :
    ι (sumK T (fun x => T.mul (φ x) (φ (K.add (K.neg x) K.one))) L) =
      sumK K (fun x => K.mul (pw K x m) (pw K (K.add (K.neg x) K.one) m)) L := by
  rw [map_sumK T K ι hadd hzero]
  exact sumK_ext K _ _ (fun x => by rw [hmul, hφ, hφ]) L

theorem pw_pw' (a : F) (e : Nat) : ∀ n : Nat, pw K (pw K a e) n = pw K a (e * n)
  | 0 => rfl
  | n + 1 => by
    show K.mul (pw K (pw K a e) n) (pw K a e) = pw K a (e * n + e)
    rw [pw_pw' a e n, HasseCM.pw_add' K a (e * n) e]

/-- **the character reduces to a power**: φ(x) = ζ^(j(x)) with x^n = w^(j(x)) (x ≠ 0), φ(0) = 0, ι(ζ) = w^t
    ⇒ ι(φ(x)) = x^(nt) for every x. -/
theorem char_red [DecidableEq F] (hmul : ∀ a b, ι (T.mul a b) = K.mul (ι a) (ι b)) (hone : ι T.one = K.one)
    (hzero : ι T.zero = K.zero) (ζ : R) (w : F) (t n : Nat) (hζ : ι ζ = pw K w t)
    (φ : F → R) (j : F → Nat) (hφ : ∀ x, x ≠ K.zero → φ x = pw T ζ (j x))
    (hj : ∀ x, x ≠ K.zero → pw K x n = pw K w (j x)) (h0 : φ K.zero = T.zero) (k : Nat) (hnt : n * t = k + 1) :
    ∀ x, ι (φ x) = pw K x (n * t) := fun x => by
  by_cases hx : x = K.zero
  · rw [hx, h0, hzero, hnt, HasseCM.pw_zero_succ]
  · rw [hφ x hx, red_root T K ι hmul hone ζ w t hζ (j x), ← hj x hx, pw_pw' K x n t]

/-- **Stickelberger (divisibility side)**: p − 1 = ℓn, 2t + 1 ≤ ℓ, ι : R → 𝔽_p a ring map with ι∘φ = (·)^(nt)
    (e.g. char_red) ⇒ ι(Σ_x φ(x)φ(1 − x)) = 0, i.e. P_t | J(φ, φ). -/
theorem jac_div {R : Type} (T : DetChain.CRing R) (J : Nat) (ι : R → ZMod.Zp (J + 1))
    (hadd : ∀ a b, ι (T.add a b) = (ZMod.ring (J + 1)).add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = (ZMod.ring (J + 1)).mul (ι a) (ι b))
    (hzero : ι T.zero = (ZMod.ring (J + 1)).zero)
    (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (l n t : Nat) (hp : J + 1 = l * n)
    (ht : 2 * t + 1 ≤ l) (h1 : 1 ≤ t) (hn : 1 ≤ n)
    (φ : ZMod.Zp (J + 1) → R) (hφ : ∀ x, ι (φ x) = pw (ZMod.ring (J + 1)) x (t * n)) :
    ι (sumK T (fun x => T.mul (φ x) (φ ((ZMod.ring (J + 1)).add ((ZMod.ring (J + 1)).neg x) (ZMod.ring (J + 1)).one)))
      (CMInert.allZ (J + 1) (J + 1 + 1))) = (ZMod.ring (J + 1)).zero := by
  rw [red_jac T (ZMod.ring (J + 1)) ι hadd hmul hzero φ (t * n) hφ]
  exact StickL.stick_lt J hpr l n t hp ht h1 hn

/-- **Stickelberger (non-divisibility side)**: ℓ = 2s + r, t = s + r (2t > ℓ) ⇒ ι(Σ_x φ(x)φ(1 − x)) ≠ 0, i.e. P_t ∤ J(φ, φ). -/
theorem jac_ndiv {R : Type} (T : DetChain.CRing R) (J : Nat) (ι : R → ZMod.Zp (J + 1))
    (hadd : ∀ a b, ι (T.add a b) = (ZMod.ring (J + 1)).add (ι a) (ι b)) (hmul : ∀ a b, ι (T.mul a b) = (ZMod.ring (J + 1)).mul (ι a) (ι b))
    (hzero : ι T.zero = (ZMod.ring (J + 1)).zero)
    (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (s r n : Nat) (hp : J + 1 = (2 * s + r) * n)
    (hs : 1 ≤ s) (hr : 1 ≤ r) (hn : 1 ≤ n)
    (φ : ZMod.Zp (J + 1) → R) (hφ : ∀ x, ι (φ x) = pw (ZMod.ring (J + 1)) x ((s + r) * n))
    (h0 : ι (sumK T (fun x => T.mul (φ x) (φ ((ZMod.ring (J + 1)).add ((ZMod.ring (J + 1)).neg x) (ZMod.ring (J + 1)).one)))
      (CMInert.allZ (J + 1) (J + 1 + 1))) = (ZMod.ring (J + 1)).zero) : False := by
  rw [red_jac T (ZMod.ring (J + 1)) ι hadd hmul hzero φ ((s + r) * n) hφ] at h0
  exact StickL.stick_gt J hpr s r n hp hs hr hn h0

end StickHom

#print axioms StickHom.map_sumK
#print axioms StickHom.map_pw
#print axioms StickHom.pw_swap
#print axioms StickHom.red_root
#print axioms StickHom.red_jac
#print axioms StickHom.pw_pw'
#print axioms StickHom.char_red
#print axioms StickHom.jac_div
#print axioms StickHom.jac_ndiv

#print axioms: 13 theorems, no axioms.

78.367 The Jacobi sum has order exactly one at \(P_t\)

With \(JJ'=p\), a ring map \(\iota _2\) to \(\mathbb Z/p^2\) at the Teichmüller root \(z=R^p\) (\(z^\ell =1\)), and the reduction to \(\mathbb F_p\).

\[ 2t<\ell \ \Rightarrow \ \mathrm {red}\,\iota _2(J)=0\ \text { and }\ \iota _2(J)\ne 0. \]

lang/StickExact.lean

/-
  lang/StickExact — T54, cycle 828 (assembly): **for 2t < ℓ the Jacobi sum lies in P_t and not in P_t²**, in computational form.
  R ∋ J(φ, φ) = Σ_x φ(x)φ(1 − x), J·J' = P_R; ι₂ : R → ℤ/p² (M + 1 = p², p = J + 2 prime) a ring map with ι₂(P_R) = p·1, and
  red ∘ ι₂ ∘ φ = (·)^(tn) on 𝔽_p (p − 1 = ℓn). Then
    red(ι₂ J(φ, φ)) = 0  and  ι₂ J(φ, φ) ≠ 0.
  (StickHom.jac_div through ι = red ∘ ι₂ (RedP2), StickVal.val_one and small_ne_zero.)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.StickHom
import lang.StickVal
import lang.RedP2
import lib.RingTac
import lib.Semi

namespace StickExact

open ZMod Frobenius

theorem exact_order {R : Type} (T : DetChain.CRing R) (J M : Nat) (hM : M + 1 = (J + 1 + 1) * (J + 1 + 1))
    (ι₂ : R → Zp M) (hadd : ∀ a b, ι₂ (T.add a b) = (ring M).add (ι₂ a) (ι₂ b))
    (hmul : ∀ a b, ι₂ (T.mul a b) = (ring M).mul (ι₂ a) (ι₂ b)) (hzero : ι₂ T.zero = (ring M).zero)
    (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (l n t : Nat) (hp : J + 1 = l * n)
    (ht : 2 * t + 1 ≤ l) (h1 : 1 ≤ t) (hn : 1 ≤ n)
    (φ : Zp (J + 1) → R) (hφ : ∀ x, RedP2.red J M (ι₂ (φ x)) = pw (ring (J + 1)) x (t * n))
    (J' P : R) (hJJ : T.mul (YMChain.sumK T (fun x => T.mul (φ x) (φ ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one)))
      (CMInert.allZ (J + 1) (J + 1 + 1))) J' = P)
    (hP : ι₂ P = nsm (ring M) (J + 1 + 1) (ring M).one) :
    RedP2.red J M (ι₂ (YMChain.sumK T (fun x => T.mul (φ x) (φ ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one)))
        (CMInert.allZ (J + 1) (J + 1 + 1)))) = (ring (J + 1)).zero ∧
      ι₂ (YMChain.sumK T (fun x => T.mul (φ x) (φ ((ring (J + 1)).add ((ring (J + 1)).neg x) (ring (J + 1)).one)))
        (CMInert.allZ (J + 1) (J + 1 + 1))) ≠ (ring M).zero := by
  have hk : M + 1 = (J + 1 + 1) * (J + 1 + 1) := hM
  have hdiv := StickHom.jac_div T J (fun r => RedP2.red J M (ι₂ r))
    (fun a b => by show RedP2.red J M (ι₂ (T.add a b)) = _; rw [hadd, RedP2.red_add J M (J + 1 + 1) hk])
    (fun a b => by show RedP2.red J M (ι₂ (T.mul a b)) = _; rw [hmul, RedP2.red_mul J M (J + 1 + 1) hk])
    (by show RedP2.red J M (ι₂ T.zero) = _; rw [hzero, RedP2.red_zero J M (J + 1 + 1) hk])
    hpr l n t hp ht h1 hn φ hφ
  have hlt : J + 1 + 1 < M + 1 := by
    rw [hM]
    exact Nat.le_trans (Nat.add_le_add_left (Nat.le_add_left 1 (J * (J + 1 + 1) + J + 1)) (J + 1 + 1)) (Nat.le_of_eq (by lib_nat))
  have hPne : ι₂ P ≠ (ring M).zero := by
    rw [hP]; exact StickVal.small_ne_zero M (J + 1) hlt
  exact StickVal.val_one T (ring M) (ring (J + 1)) ι₂ (RedP2.red J M) hmul (fun b => by lib_ring) _ J' P hJJ hPne hdiv

end StickExact

#print axioms StickExact.exact_order

lang/TeichRoot.lean

/-
  lang/TeichRoot — T54, cycle 828 (step 6): **the Teichmüller root exists**, abstractly: A a ring with P = p·1, P² = 0,
  red : A → F multiplicative with kernel P·A, F with y^p = y (Fermat). If red(R)^ℓ = 1 then z = R^p has
    z^ℓ = 1 in A    and    red(z) = red(R).
  For A = ℤ/p², F = 𝔽_p (RedP2) this is the root ι₂(ζ) of StickVal.
    map_pw, teich_root
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Teich
import lang.RedP2
import lang.RootAll
import lib.RingTac

namespace TeichRoot

open Frobenius

variable {A F : Type} (K : DetChain.CRing A) (Kf : DetChain.CRing F) (red : A → F)

theorem map_pw (hmul : ∀ a b, red (K.mul a b) = Kf.mul (red a) (red b)) (hone : red K.one = Kf.one) (a : A) :
    ∀ n : Nat, red (pw K a n) = pw Kf (red a) n
  | 0 => hone
  | n + 1 => by
    show red (K.mul (pw K a n) a) = Kf.mul (pw Kf (red a) n) (red a)
    rw [hmul, map_pw hmul hone a n]

/-- **z = R^p is an ℓ-th root of unity in A lifting red(R).** -/
theorem teich_root (hmul : ∀ a b, red (K.mul a b) = Kf.mul (red a) (red b)) (hone : red K.one = Kf.one)
    (hadd : ∀ a b, red (K.add a b) = Kf.add (red a) (red b)) (hneg : ∀ a, red (K.neg a) = Kf.neg (red a))
    (p l : Nat) (hP : K.mul (nsm K p K.one) (nsm K p K.one) = K.zero)
    (hker : ∀ x, red x = Kf.zero → ∃ t, x = K.mul (nsm K p K.one) t)
    (hfer : ∀ y : F, pw Kf y p = y) (R : A) (hR : pw Kf (red R) l = Kf.one) :
    pw K (pw K R p) l = K.one ∧ red (pw K R p) = red R := by
  have hk : red (K.add (pw K R l) (K.neg K.one)) = Kf.zero := by
    rw [hadd, hneg, map_pw K Kf red hmul hone, hR, hone]; lib_ring
  obtain ⟨t, ht⟩ := hker _ hk
  have hRl : pw K R l = K.add K.one (K.mul (nsm K p K.one) t) := by
    calc pw K R l = K.add (K.add (pw K R l) (K.neg K.one)) K.one := by lib_ring
      _ = K.add (K.mul (nsm K p K.one) t) K.one := by rw [ht]
      _ = _ := by lib_ring
  refine ⟨?_, ?_⟩
  · rw [RootAll.pw_comm K R l p, hRl]
    exact Teich.teich K p t hP
  · rw [map_pw K Kf red hmul hone, hfer]

end TeichRoot

#print axioms TeichRoot.map_pw
#print axioms TeichRoot.teich_root

lang/RedKer.lean

/-
  lang/RedKer — T54, cycle 828 (step 7): **ℤ/p² facts for the Teichmüller root** (M + 1 = p·p, p = J + 2):
    P_sq     P·P = 0 for P = p·1
    red_ker  red(x) = 0 in 𝔽_p ⇒ x = P·t for some t
  With RedP2 (red is a ring map), ZMod.fermat (y^p = y in 𝔽_p) and TeichRoot.teich_root this gives z ∈ ℤ/p² with
  z^ℓ = 1 and red(z) = r for every r ∈ 𝔽_p with r^ℓ = 1.
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.RedP2
import lib.Semi

namespace RedP2

open ZMod Frobenius

variable (J M : Nat)

theorem P_sq (hM : M + 1 = (J + 1 + 1) * (J + 1 + 1)) :
    (ring M).mul (nsm (ring M) (J + 1 + 1) (ring M).one) (nsm (ring M) (J + 1 + 1) (ring M).one) = (ring M).zero :=
  Subtype.ext (by
    show md M ((nsm (ring M) (J + 1 + 1) (ring M).one).val * (nsm (ring M) (J + 1 + 1) (ring M).one).val) = 0
    rw [nsm_one_val, ← md_mul2, ← hM, md_self])

theorem red_ker (x : Zp M) (h : red J M x = (ring (J + 1)).zero) :
    ∃ t, x = (ring M).mul (nsm (ring M) (J + 1 + 1) (ring M).one) t := by
  have hv : md (J + 1) x.val = 0 := congrArg Subtype.val h
  obtain ⟨q, hq⟩ := md_zero_dv (J + 1) _ hv
  refine ⟨mk M q, Subtype.ext ?_⟩
  calc x.val = md M x.val := (md_small M _ x.property).symm
    _ = md M (q * (J + 1 + 1)) := by rw [hq]
    _ = md M ((J + 1 + 1) * q) := by rw [Nat.mul_comm]
    _ = md M (md M (J + 1 + 1) * md M q) := md_mul2 M _ _
    _ = ((ring M).mul (nsm (ring M) (J + 1 + 1) (ring M).one) (mk M q)).val := by
      show _ = md M ((nsm (ring M) (J + 1 + 1) (ring M).one).val * md M q)
      rw [nsm_one_val]

end RedP2

#print axioms RedP2.P_sq
#print axioms RedP2.red_ker

#print axioms: 5 theorems, no axioms.

78.368 The Jacobi sum of the Teichmüller character modulo \(p^2\)

With \(\psi _e(x)=(\tilde x^{p})^{e}\) in \(\mathbb Z/p^2\) (multiplicative, reducing to \(x^e\)), the Jacobi sum has \(p\)-adic order exactly one.

\[ 1\le e,\ 2e\le p-2\ \Rightarrow \ J(\psi _e,\psi _e)\equiv 0\ (\mathrm {mod}\ p),\qquad J(\psi _e,\psi _e)\not \equiv 0\ (\mathrm {mod}\ p^2). \]

lang/TeichChar.lean

/-
  lang/TeichChar — T54, cycle 829 (concrete ι₂, part 1): **units of ℤ/p² and the p-th power killing p-multiples**
  (M + 1 = p², p = J + 2 prime):
    pow_sq0     x² = 0 ⇒ (a + x)^(n+1) = a^(n+1) + (n + 1)·(aⁿ x)
    pow_p_kill  P² = 0 (P = p·1) ⇒ (a + P u)^p = a^p
    one_plus    red(a) ≠ 0 ⇒ a^(p−1) = 1 + P t
    unit_p2     red(a) ≠ 0 ⇒ a · a^(J(J+3)+1) = 1   (a^(p(p−1)) = 1)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.RedKer
import lang.TeichRoot
import lib.RingTac
import lib.Semi

namespace TeichChar

open ZMod Frobenius

section gen
variable {A : Type} (K : DetChain.CRing A)

theorem pow_sq0 (a x : A) (hx : K.mul x x = K.zero) :
    ∀ n : Nat, pw K (K.add a x) (n + 1) = K.add (pw K a (n + 1)) (K.mul (nsm K (n + 1) K.one) (K.mul (pw K a n) x))
  | 0 => by
    show K.mul K.one (K.add a x) = K.add (K.mul K.one a) (K.mul (K.add K.zero K.one) (K.mul K.one x))
    lib_ring
  | n + 1 => by
    show K.mul (pw K (K.add a x) (n + 1)) (K.add a x) = K.add (K.mul (K.mul (pw K a n) a) a)
      (K.mul (K.add (nsm K (n + 1) K.one) K.one) (K.mul (K.mul (pw K a n) a) x))
    rw [pow_sq0 a x hx n]
    show K.mul (K.add (K.mul (pw K a n) a) (K.mul (nsm K (n + 1) K.one) (K.mul (pw K a n) x))) (K.add a x) = _
    calc K.mul (K.add (K.mul (pw K a n) a) (K.mul (nsm K (n + 1) K.one) (K.mul (pw K a n) x))) (K.add a x)
        = K.add (K.add (K.mul (K.mul (pw K a n) a) a) (K.mul (K.add (nsm K (n + 1) K.one) K.one) (K.mul (K.mul (pw K a n) a) x)))
            (K.mul (K.mul (nsm K (n + 1) K.one) (pw K a n)) (K.mul x x)) := by lib_ring
      _ = _ := by rw [hx]; lib_ring

theorem pow_p_kill (p : Nat) (hP : K.mul (nsm K (p + 1) K.one) (nsm K (p + 1) K.one) = K.zero) (a u : A) :
    pw K (K.add a (K.mul (nsm K (p + 1) K.one) u)) (p + 1) = pw K a (p + 1) := by
  have hx : K.mul (K.mul (nsm K (p + 1) K.one) u) (K.mul (nsm K (p + 1) K.one) u) = K.zero := by
    calc _ = K.mul (K.mul (nsm K (p + 1) K.one) (nsm K (p + 1) K.one)) (K.mul u u) := by lib_ring
      _ = K.zero := by rw [hP]; lib_ring
  rw [pow_sq0 K a _ hx p]
  calc K.add (pw K a (p + 1)) (K.mul (nsm K (p + 1) K.one) (K.mul (pw K a p) (K.mul (nsm K (p + 1) K.one) u)))
      = K.add (pw K a (p + 1)) (K.mul (K.mul (nsm K (p + 1) K.one) (nsm K (p + 1) K.one)) (K.mul (pw K a p) u)) := by lib_ring
    _ = _ := by rw [hP]; lib_ring
end gen

variable (J M : Nat) (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
  (hM : M + 1 = (J + 1 + 1) * (J + 1 + 1))

include hpr hM in
theorem one_plus (a : Zp M) (ha : RedP2.red J M a ≠ (ring (J + 1)).zero) :
    ∃ t, pw (ring M) a (J + 1) = (ring M).add (ring M).one ((ring M).mul (nsm (ring M) (J + 1 + 1) (ring M).one) t) := by
  have hr : RedP2.red J M ((ring M).add (pw (ring M) a (J + 1)) ((ring M).neg (ring M).one)) = (ring (J + 1)).zero := by
    have hneg : ∀ y, RedP2.red J M ((ring M).neg y) = (ring (J + 1)).neg (RedP2.red J M y) := fun y => by
      have h0 := RedP2.red_add J M (J + 1 + 1) hM y ((ring M).neg y)
      rw [show (ring M).add y ((ring M).neg y) = (ring M).zero by lib_ring, RedP2.red_zero J M (J + 1 + 1) hM] at h0
      calc RedP2.red J M ((ring M).neg y)
          = (ring (J + 1)).add (ring (J + 1)).zero ((ring (J + 1)).neg (RedP2.red J M y)) := by
            rw [h0]; lib_ring
        _ = _ := by lib_ring
    rw [RedP2.red_add J M (J + 1 + 1) hM, hneg, TeichRoot.map_pw (ring M) (ring (J + 1)) (RedP2.red J M)
      (RedP2.red_mul J M (J + 1 + 1) hM) (RedP2.red_one J M (J + 1 + 1) hM), unit_pow J hpr _ ha,
      RedP2.red_one J M (J + 1 + 1) hM]
    lib_ring
  obtain ⟨t, ht⟩ := RedP2.red_ker J M _ hr
  exact ⟨t, by
    calc pw (ring M) a (J + 1) = (ring M).add ((ring M).add (pw (ring M) a (J + 1)) ((ring M).neg (ring M).one)) (ring M).one := by lib_ring
      _ = _ := by rw [ht]; lib_ring⟩

include hpr hM in
/-- **units of ℤ/p²**: red(a) ≠ 0 ⇒ a · a^(J(J+3)+1) = 1. -/
theorem unit_p2 (a : Zp M) (ha : RedP2.red J M a ≠ (ring (J + 1)).zero) :
    (ring M).mul a (pw (ring M) a (J * (J + 3) + 1)) = (ring M).one := by
  obtain ⟨t, ht⟩ := one_plus J M hpr hM a ha
  have e : (ring M).mul a (pw (ring M) a (J * (J + 3) + 1)) = pw (ring M) (pw (ring M) a (J + 1)) (J + 1 + 1) := by
    rw [RootAll.pw_pw, show (ring M).mul a (pw (ring M) a (J * (J + 3) + 1)) = pw (ring M) a (J * (J + 3) + 1 + 1) by
      show _ = (ring M).mul (pw (ring M) a (J * (J + 3) + 1)) a; lib_ring, show J * (J + 3) + 1 + 1 = (J + 1) * (J + 1 + 1) by lib_nat]
  rw [e, ht]
  exact Teich.teich (ring M) (J + 1 + 1) t (RedP2.P_sq J M hM)

end TeichChar

#print axioms TeichChar.pow_sq0
#print axioms TeichChar.pow_p_kill
#print axioms TeichChar.one_plus
#print axioms TeichChar.unit_p2

lang/TeichChar2.lean

/-
  lang/TeichChar2 — T54, cycle 829 (concrete ι₂, part 2): **the Teichmüller character modulo p²** (M + 1 = p², p = J + 2):
    lift x = x.val in ℤ/p²;   red(lift x) = x;   lift(xy) = lift x · lift y + P u
    ψ_e(x) = ((lift x)^p)^e   is multiplicative, ψ_e(1) = 1, ψ_e(0) = 0 (e ≥ 1), and red(ψ_e x) = x^e (Fermat).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.TeichChar
import lib.RingTac

namespace TeichChar

open ZMod Frobenius

variable (J M : Nat) (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
  (hM : M + 1 = (J + 1 + 1) * (J + 1 + 1))

def lift (x : Zp (J + 1)) : Zp M := mk M x.val

include hM in
theorem red_lift (x : Zp (J + 1)) : RedP2.red J M (lift J M x) = x := by
  show mk (J + 1) (md M x.val) = x
  have e : mk (J + 1) (md M x.val) = mk (J + 1) x.val := Subtype.ext (RedP2.mdmd J M (J + 1 + 1) hM x.val)
  rw [e, mk_val]

include hM in
theorem red_neg (y : Zp M) : RedP2.red J M ((ring M).neg y) = (ring (J + 1)).neg (RedP2.red J M y) := by
  have h0 := RedP2.red_add J M (J + 1 + 1) hM y ((ring M).neg y)
  rw [show (ring M).add y ((ring M).neg y) = (ring M).zero by lib_ring, RedP2.red_zero J M (J + 1 + 1) hM] at h0
  calc RedP2.red J M ((ring M).neg y)
      = (ring (J + 1)).add (ring (J + 1)).zero ((ring (J + 1)).neg (RedP2.red J M y)) := by rw [h0]; lib_ring
    _ = _ := by lib_ring

include hM in
theorem lift_mul (x y : Zp (J + 1)) :
    ∃ u, lift J M ((ring (J + 1)).mul x y) = (ring M).add ((ring M).mul (lift J M x) (lift J M y))
      ((ring M).mul (nsm (ring M) (J + 1 + 1) (ring M).one) u) := by
  have hr : RedP2.red J M ((ring M).add (lift J M ((ring (J + 1)).mul x y)) ((ring M).neg ((ring M).mul (lift J M x) (lift J M y)))) =
      (ring (J + 1)).zero := by
    rw [RedP2.red_add J M (J + 1 + 1) hM, red_neg J M hM, RedP2.red_mul J M (J + 1 + 1) hM, red_lift J M hM, red_lift J M hM,
      red_lift J M hM]
    lib_ring
  obtain ⟨u, hu⟩ := RedP2.red_ker J M _ hr
  exact ⟨u, by
    calc lift J M ((ring (J + 1)).mul x y)
        = (ring M).add ((ring M).mul (lift J M x) (lift J M y))
            ((ring M).add (lift J M ((ring (J + 1)).mul x y)) ((ring M).neg ((ring M).mul (lift J M x) (lift J M y)))) := by lib_ring
      _ = _ := by rw [hu]⟩

/-- the Teichmüller character ψ_e(x) = ((lift x)^p)^e. -/
def psi (e : Nat) (x : Zp (J + 1)) : Zp M := pw (ring M) (pw (ring M) (lift J M x) (J + 1 + 1)) e

include hM in
theorem psi_mul (e : Nat) (x y : Zp (J + 1)) :
    psi J M e ((ring (J + 1)).mul x y) = (ring M).mul (psi J M e x) (psi J M e y) := by
  obtain ⟨u, hu⟩ := lift_mul J M hM x y
  unfold psi
  rw [hu, pow_p_kill (ring M) (J + 1) (RedP2.P_sq J M hM), pw_mul (ring M), pw_mul (ring M)]

include hpr hM in
theorem red_psi (e : Nat) (x : Zp (J + 1)) : RedP2.red J M (psi J M e x) = pw (ring (J + 1)) x e := by
  unfold psi
  rw [TeichRoot.map_pw (ring M) (ring (J + 1)) (RedP2.red J M) (RedP2.red_mul J M (J + 1 + 1) hM) (RedP2.red_one J M (J + 1 + 1) hM),
    TeichRoot.map_pw (ring M) (ring (J + 1)) (RedP2.red J M) (RedP2.red_mul J M (J + 1 + 1) hM) (RedP2.red_one J M (J + 1 + 1) hM),
    red_lift J M hM, fermat J hpr x]

theorem lift_one : lift J M (ring (J + 1)).one = (ring M).one := Subtype.ext (by
  show md M (md (J + 1) 1) = md M 1
  rw [md_small (J + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ J))])

theorem psi_one (e : Nat) : psi J M e (ring (J + 1)).one = (ring M).one := by
  unfold psi
  rw [lift_one, RootAll.pw_one, RootAll.pw_one]

theorem psi_zero (e : Nat) : psi J M (e + 1) (ring (J + 1)).zero = (ring M).zero := by
  unfold psi
  have h0 : lift J M (ring (J + 1)).zero = (ring M).zero := rfl
  rw [h0, HasseCM.pw_zero_succ (ring M) (J + 1), HasseCM.pw_zero_succ (ring M) e]

include hM in
/-- ψ_e(x^k) = ψ_e(x)^k. -/
theorem psi_pw (e : Nat) (x : Zp (J + 1)) : ∀ k : Nat, psi J M e (pw (ring (J + 1)) x k) = pw (ring M) (psi J M e x) k
  | 0 => psi_one J M e
  | k + 1 => by
    show psi J M e ((ring (J + 1)).mul (pw (ring (J + 1)) x k) x) = (ring M).mul (pw (ring M) (psi J M e x) k) (psi J M e x)
    rw [psi_mul J M hM, psi_pw e x k]

end TeichChar

#print axioms TeichChar.red_lift
#print axioms TeichChar.red_neg
#print axioms TeichChar.lift_mul
#print axioms TeichChar.psi_mul
#print axioms TeichChar.red_psi
#print axioms TeichChar.lift_one
#print axioms TeichChar.psi_one
#print axioms TeichChar.psi_zero
#print axioms TeichChar.psi_pw

lang/TeichChar3.lean

/-
  lang/TeichChar3 — T54, cycle 829 (concrete ι₂, part 3 — the assembly): **the Jacobi sum of the Teichmüller character modulo p²
  is divisible by p exactly once** (p = j + 3 prime, M + 1 = p², ψ = ψ_e of TeichChar2, 1 ≤ e, 2e ≤ p − 2, and g ∈ 𝔽_p^× with
  g^e, g^(2e), g^(2e(p−2)) ≠ 1):
    J(ψ, ψ) = Σ_t ψ(t) ψ(1 − t) ∈ ℤ/p²:   red J = 0   and   J ≠ 0.
  Pieces: sum_one_allZ, red_P (p ≡ 0 mod p), unit_of (red a ≠ 0 ⇒ a cancels), psi_sum (Σψ_f = 0 by sum_scale), psi_sq, psi_iv,
  then CharSumGen.jac_norm (J J' = p), Stick1.jac_gen (red J = 0), StickVal.val_one.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.TeichChar2
import lang.JacNormN
import lang.StickVal
import lang.Stick1
import lang.StickHom
import lib.RingTac
import lib.Semi

namespace TeichChar

open ZMod Frobenius YMChain

theorem sum_one_allZ {R : Type} (K : DetChain.CRing R) (m : Nat) :
    ∀ k, sumK K (fun _ : Zp m => K.one) (CMInert.allZ m k) = nsm K k K.one
  | 0 => rfl
  | k + 1 => by
    show sumK K (fun _ : Zp m => K.one) (CMInert.allZ m k ++ [mk m k]) = K.add (nsm K k K.one) K.one
    rw [sumK_append, sum_one_allZ K m k]
    show K.add (nsm K k K.one) (K.add K.one K.zero) = _
    lib_ring

variable (j M : Nat) (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hM : M + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1))

include hM in
theorem red_P : RedP2.red (j + 1) M (CharSumGen.Pp (ring M) j) = (ring (j + 1 + 1)).zero := by
  show RedP2.red (j + 1) M (sumK (ring M) (fun _ => (ring M).one) (CMInert.allZ (j + 1 + 1) (j + 1 + 1 + 1))) = _
  rw [sum_one_allZ (ring M) (j + 1 + 1) (j + 1 + 1 + 1)]
  exact Subtype.ext (by
    show md (j + 1 + 1) ((nsm (ring M) (j + 1 + 1 + 1) (ring M).one).val) = 0
    rw [nsm_one_val, RedP2.mdmd (j + 1) M (j + 1 + 1 + 1) hM, md_self])

include hpr hM in
/-- an element whose reduction is nonzero cancels. -/
theorem unit_of (u : Zp M) (hu : RedP2.red (j + 1) M u ≠ (ring (j + 1 + 1)).zero) (a b : Zp M)
    (h : (ring M).mul a u = (ring M).mul b u) : a = b := by
  have hi := unit_p2 (j + 1) M hpr hM u hu
  calc a = (ring M).mul ((ring M).mul a u) (pw (ring M) u ((j + 1) * (j + 1 + 3) + 1)) := by rw [(ring M).mul_assoc, hi]; lib_ring
    _ = (ring M).mul ((ring M).mul b u) (pw (ring M) u ((j + 1) * (j + 1 + 3) + 1)) := by rw [h]
    _ = b := by rw [(ring M).mul_assoc, hi]; lib_ring

include hM in
theorem red_sub_one (a : Zp M) (h : RedP2.red (j + 1) M a ≠ (ring (j + 1 + 1)).one) :
    RedP2.red (j + 1) M ((ring M).add a ((ring M).neg (ring M).one)) ≠ (ring (j + 1 + 1)).zero := fun h0 => h (by
  rw [RedP2.red_add (j + 1) M (j + 1 + 1 + 1) hM, red_neg (j + 1) M hM, RedP2.red_one (j + 1) M (j + 1 + 1 + 1) hM] at h0
  calc RedP2.red (j + 1) M a = (ring (j + 1 + 1)).add ((ring (j + 1 + 1)).add (RedP2.red (j + 1) M a) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) (ring (j + 1 + 1)).one := by lib_ring
    _ = (ring (j + 1 + 1)).one := by rw [h0]; lib_ring)

include hpr hM in
/-- **Σ ψ_f = 0** when some g has g^f ≠ 1. -/
theorem psi_sum (f : Nat) (g : Zp (j + 1 + 1)) (hg : g ≠ (ring (j + 1 + 1)).zero) (hgf : pw (ring (j + 1 + 1)) g f ≠ (ring (j + 1 + 1)).one) :
    sumK (ring M) (psi (j + 1) M f) (CharSumGen.LL j) = (ring M).zero := by
  have hsc := CharSumGen.sum_scale (ring M) j hpr (psi (j + 1) M f) g hg
  have e1 : sumK (ring M) (fun x => psi (j + 1) M f ((ring (j + 1 + 1)).mul g x)) (CharSumGen.LL j) =
      (ring M).mul (psi (j + 1) M f g) (sumK (ring M) (psi (j + 1) M f) (CharSumGen.LL j)) := by
    rw [mul_sumK]
    exact sumK_ext (ring M) _ _ (fun x => psi_mul (j + 1) M hM f g x) _
  rw [e1] at hsc
  have hu : RedP2.red (j + 1) M ((ring M).add (psi (j + 1) M f g) ((ring M).neg (ring M).one)) ≠ (ring (j + 1 + 1)).zero :=
    red_sub_one j M hM _ (by rw [red_psi (j + 1) M hpr hM]; exact hgf)
  apply unit_of j M hpr hM _ hu
  calc (ring M).mul (sumK (ring M) (psi (j + 1) M f) (CharSumGen.LL j)) ((ring M).add (psi (j + 1) M f g) ((ring M).neg (ring M).one))
      = (ring M).add ((ring M).mul (psi (j + 1) M f g) (sumK (ring M) (psi (j + 1) M f) (CharSumGen.LL j)))
          ((ring M).neg (sumK (ring M) (psi (j + 1) M f) (CharSumGen.LL j))) := by lib_ring
    _ = (ring M).zero := by rw [hsc]; lib_ring
    _ = _ := by lib_ring

theorem psi_sq (e : Nat) (x : Zp (j + 1 + 1)) :
    (ring M).mul (psi (j + 1) M e x) (psi (j + 1) M e x) = psi (j + 1) M (e + e) x := by
  unfold psi
  rw [HasseCM.pw_add' (ring M) (pw (ring M) (lift (j + 1) M x) (j + 1 + 1 + 1)) e e]

include hM in
theorem psi_powk (e k : Nat) (x : Zp (j + 1 + 1)) :
    psi (j + 1) M e (pw (ring (j + 1 + 1)) x k) = psi (j + 1) M (e * k) x := by
  rw [psi_pw (j + 1) M hM]
  unfold psi
  rw [RootAll.pw_pw (ring M) (pw (ring M) (lift (j + 1) M x) (j + 1 + 1 + 1)) e k]

include hpr hM in
/-- **the Jacobi sum of the Teichmüller character ψ_(e+1) modulo p² is divisible by p exactly once**. -/
theorem jac_teich (e : Nat) (he2 : e + 1 + (e + 1) ≤ j + 1) (g : Zp (j + 1 + 1)) (hg : g ≠ (ring (j + 1 + 1)).zero)
    (hg1 : pw (ring (j + 1 + 1)) g (e + 1) ≠ (ring (j + 1 + 1)).one)
    (hg2 : pw (ring (j + 1 + 1)) g (e + 1 + (e + 1)) ≠ (ring (j + 1 + 1)).one)
    (hg3 : pw (ring (j + 1 + 1)) g ((e + 1 + (e + 1)) * (j + 1)) ≠ (ring (j + 1 + 1)).one) :
    RedP2.red (j + 1) M (CharSumGen.J2 (ring M) j (psi (j + 1) M (e + 1)) (psi (j + 1) M (e + 1))) = (ring (j + 1 + 1)).zero ∧
      CharSumGen.J2 (ring M) j (psi (j + 1) M (e + 1)) (psi (j + 1) M (e + 1)) ≠ (ring M).zero := by
  have hs := psi_sum j M hpr hM (e + 1) g hg hg1
  have hs2 : sumK (ring M) (fun a => (ring M).mul (psi (j + 1) M (e + 1) a) (psi (j + 1) M (e + 1) a)) (CharSumGen.LL j) = (ring M).zero := by
    rw [sumK_ext (ring M) _ _ (fun a => psi_sq j M (e + 1) a)]
    exact psi_sum j M hpr hM _ g hg hg2
  have hs2' : sumK (ring M) (fun a => (ring M).mul (psi (j + 1) M (e + 1) (CharSumGen.iv j a)) (psi (j + 1) M (e + 1) (CharSumGen.iv j a)))
      (CharSumGen.LL j) = (ring M).zero := by
    rw [sumK_ext (ring M) _ (fun a => psi (j + 1) M ((e + 1 + (e + 1)) * (j + 1)) a) (fun a => by
      rw [psi_sq j M (e + 1)]
      exact psi_powk j M hM (e + 1 + (e + 1)) (j + 1) a)]
    exact psi_sum j M hpr hM _ g hg hg3
  have hredP : RedP2.red (j + 1) M (CharSumGen.Pp (ring M) j) ≠ (ring (j + 1 + 1)).one := by
    rw [red_P j M hM]; exact fun h => PowerSum.one_ne_zero' (j + 1) h.symm
  have hcan : ∀ a b, (ring M).mul a ((ring M).add (CharSumGen.Pp (ring M) j) ((ring M).neg (ring M).one)) =
      (ring M).mul b ((ring M).add (CharSumGen.Pp (ring M) j) ((ring M).neg (ring M).one)) → a = b :=
    unit_of j M hpr hM _ (red_sub_one j M hM _ hredP)
  have hJJ := CharSumGen.jac_norm (ring M) j hpr (psi (j + 1) M (e + 1)) (psi_mul (j + 1) M hM (e + 1)) (psi_one (j + 1) M (e + 1))
    (psi_zero (j + 1) M e) hs hs2 hs2' hcan
  -- red J = 0
  have hdiv : RedP2.red (j + 1) M (CharSumGen.J2 (ring M) j (psi (j + 1) M (e + 1)) (psi (j + 1) M (e + 1))) = (ring (j + 1 + 1)).zero := by
    unfold CharSumGen.J2
    rw [StickHom.map_sumK (ring M) (ring (j + 1 + 1)) (RedP2.red (j + 1) M) (RedP2.red_add (j + 1) M (j + 1 + 1 + 1) hM)
      (RedP2.red_zero (j + 1) M (j + 1 + 1 + 1) hM)]
    rw [sumK_ext (ring (j + 1 + 1)) _ (fun x => (ring (j + 1 + 1)).mul (pw (ring (j + 1 + 1)) x (e + 1))
      (pw (ring (j + 1 + 1)) ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).neg x) (ring (j + 1 + 1)).one) (e + 1))) (fun x => by
        rw [RedP2.red_mul (j + 1) M (j + 1 + 1 + 1) hM, red_psi (j + 1) M hpr hM, red_psi (j + 1) M hpr hM,
          (ring (j + 1 + 1)).add_comm (ring (j + 1 + 1)).one])]
    exact Stick1.jac_gen (j + 1) hpr (e + 1) (e + 1) (Nat.succ_le_succ (Nat.zero_le e)) he2
  have hlt : j + 1 + 1 + 1 < M + 1 := by
    rw [hM]
    exact Nat.le_trans (Nat.add_le_add_left (Nat.le_add_left 1 (j * (j + 5) + 5)) (j + 1 + 1 + 1))
      (Nat.le_of_eq (by lib_nat))
  have hP : CharSumGen.Pp (ring M) j ≠ (ring M).zero := by
    show sumK (ring M) (fun _ => (ring M).one) (CMInert.allZ (j + 1 + 1) (j + 1 + 1 + 1)) ≠ _
    rw [sum_one_allZ (ring M) (j + 1 + 1) (j + 1 + 1 + 1)]
    exact StickVal.small_ne_zero M (j + 1 + 1) hlt
  exact StickVal.val_one (ring M) (ring M) (ring (j + 1 + 1)) (fun x => x) (RedP2.red (j + 1) M) (fun a b => rfl)
    (fun b => by lib_ring) _ _ _ hJJ hP hdiv

end TeichChar

#print axioms TeichChar.sum_one_allZ
#print axioms TeichChar.red_P
#print axioms TeichChar.unit_of
#print axioms TeichChar.red_sub_one
#print axioms TeichChar.psi_sum
#print axioms TeichChar.psi_sq
#print axioms TeichChar.psi_powk
#print axioms TeichChar.jac_teich

#print axioms: 21 theorems, no axioms.

78.369 The Gauss sum power is primary for every odd \(\ell \)

For a character \(\chi \) of odd order \(\ell \mid p-1\), \(A=\prod _{i\le \ell -2}J(\chi ^i,\chi )\,\chi (-1)\) satisfies \(A\,p\equiv -1\) modulo \((1-\zeta )^2\).

\[ \chi =(1+\pi )^{\mathrm {dlog}},\ \pi ^2=0,\ \ell \cdot 1=0\ \Rightarrow \ A\cdot p=-1. \]

lang/DLogF.lean

/-
  lang/DLogF — T49 (general ℓ), cycle 829: **the discrete logarithm as a function** (DLog proves only existence):
    dlogF      search g^k = a for k = k0, …, k0 + n − 1
    dlogF_spec a hit in the window ⇒ g^(dlogF) = a and dlogF < k0 + n
    dlog       dlog a = dlogF (p − 1) 0;   a ≠ 0 ⇒ g^(dlog a) = a, dlog a < p − 1     (DLog.every_unit)
    dlog_pw    k < p − 1 ⇒ dlog (g^k) = k                                          (DLog.pw_inj, RootAll.tri)
  So χ(a) = ζ^(dlog a) is an honest function on 𝔽_p^×.
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.DLog
import lang.RootAll
import lib.Semi

namespace DLogF

open FTAChain ZMod YMChain FiniteRecChain Frobenius

variable (J : Nat)

def dlogF (g a : Zp (J + 1)) : Nat → Nat → Nat
  | 0, _ => 0
  | n + 1, k => if pw (ring (J + 1)) g k = a then k else dlogF g a n (k + 1)

theorem succ_le_of_ne (k i : Nat) (h : Nat.le k i) (hne : i ≠ k) : Nat.le (k + 1) i := by
  cases h with
  | refl => exact absurd rfl hne
  | step h' => exact Nat.succ_le_succ h'

theorem dlogF_spec (g a : Zp (J + 1)) : ∀ n k, (∃ i, k ≤ i ∧ i < k + n ∧ pw (ring (J + 1)) g i = a) →
    pw (ring (J + 1)) g (dlogF J g a n k) = a ∧ dlogF J g a n k < k + n
  | 0, k, ⟨i, h1, h2, _⟩ => absurd (Nat.lt_of_le_of_lt h1 h2) (Nat.lt_irrefl k)
  | n + 1, k, ⟨i, h1, h2, h3⟩ => by
    by_cases h : pw (ring (J + 1)) g k = a
    · have e : dlogF J g a (n + 1) k = k := by
        show (if pw (ring (J + 1)) g k = a then k else dlogF J g a n (k + 1)) = k
        rw [ite_pos' _ _ h]
      rw [e]
      exact ⟨h, Nat.lt_of_lt_of_le (Nat.lt_succ_self k) (Nat.succ_le_succ (Nat.le_add_right k n))⟩
    · have e : dlogF J g a (n + 1) k = dlogF J g a n (k + 1) := by
        show (if pw (ring (J + 1)) g k = a then k else dlogF J g a n (k + 1)) = _
        rw [ite_neg' _ _ h]
      rw [e]
      have hi : k + 1 ≤ i := succ_le_of_ne k i h1 (fun hik => h (hik ▸ h3))
      have hi2 : i < k + 1 + n := by
        rw [show k + 1 + n = k + (n + 1) by lib_nat]; exact h2
      obtain ⟨r1, r2⟩ := dlogF_spec g a n (k + 1) ⟨i, hi, hi2, h3⟩
      exact ⟨r1, by rw [show k + (n + 1) = k + 1 + n by lib_nat]; exact r2⟩

/-- the discrete logarithm base g. -/
def dlog (g a : Zp (J + 1)) : Nat := dlogF J g a (J + 1) 0

theorem dlog_spec (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (g : Zp (J + 1)) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
    (a : Zp (J + 1)) (ha : a ≠ (ring (J + 1)).zero) :
    pw (ring (J + 1)) g (dlog J g a) = a ∧ dlog J g a < J + 1 := by
  obtain ⟨k, hk, e⟩ := DLog.mem_pows J g a (J + 1) (DLog.every_unit J hpr g hg a ha)
  have r := dlogF_spec J g a (J + 1) 0 ⟨k, Nat.zero_le k, by rw [Nat.zero_add]; exact hk, e.symm⟩
  rw [Nat.zero_add] at r
  exact r

theorem dlog_pw (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (g : Zp (J + 1)) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
    (hg0 : g ≠ (ring (J + 1)).zero) (k : Nat) (hk : k < J + 1) : dlog J g (pw (ring (J + 1)) g k) = k := by
  have hne : pw (ring (J + 1)) g k ≠ (ring (J + 1)).zero := DLog.pw_ne_zero J hpr g hg0 k
  obtain ⟨e, hd⟩ := dlog_spec J hpr g hg _ hne
  match RootAll.tri (dlog J g (pw (ring (J + 1)) g k)) k with
  | Or.inl h => exact h
  | Or.inr (Or.inl h) => exact absurd e (DLog.pw_inj J hpr g hg _ k h hk)
  | Or.inr (Or.inr h) => exact absurd e.symm (DLog.pw_inj J hpr g hg k _ h hd)

end DLogF

#print axioms DLogF.succ_le_of_ne
#print axioms DLogF.dlogF_spec
#print axioms DLogF.dlog_spec
#print axioms DLogF.dlog_pw

lang/PrimJ.lean

/-
  lang/PrimJ — T49 (general ℓ), cycle 829: **the Jacobi sum is primary**, in computational form.
  In any ring Q (think Q = ℤ[ζ_ℓ]/(1 − ζ)², u = image of a character χ of 𝔽_p), if
    u(0) = 0, u(1) = 1, (u(t) − 1)(u(s) − 1) = 0 for t, s ≠ 0, Σ_t u(t) = 0, (p − 1)·1 = 0 in Q,
  then  Σ_t u(t) u(1 − t) = −1,   i.e.  J(χ, χ) ≡ −1 (mod (1 − ζ)²).
  Proof: u_t u_(1−t) = u_t + u_(1−t) − 1 + (u_t − 1)(u_(1−t) − 1); the last term is 0 for every t (t = 0, 1 by u(0) = 0, u(1) = 1),
  Σ u_(1−t) = Σ u_t = 0 (GroupRing.flip), Σ 1 = p.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.TeichChar3
import lib.RingTac

namespace PrimJ

open ZMod Frobenius YMChain GroupRing

variable {Q : Type} (K : DetChain.CRing Q) (m : Nat)

theorem prim (u : Zp m → Q) (h0 : u (ring m).zero = K.zero) (h1 : u (ring m).one = K.one)
    (hsq : ∀ t s, t ≠ (ring m).zero → s ≠ (ring m).zero → K.mul (K.add (u t) (K.neg K.one)) (K.add (u s) (K.neg K.one)) = K.zero)
    (hs : sumK K u (L m) = K.zero) (hp1 : nsm K m K.one = K.zero) :
    sumK K (fun t => K.mul (u t) (u ((ring m).add (ring m).one ((ring m).neg t)))) (L m) = K.neg K.one := by
  have pt : ∀ t, K.mul (u t) (u ((ring m).add (ring m).one ((ring m).neg t))) =
      K.add (K.add (u t) (u ((ring m).add (ring m).one ((ring m).neg t)))) (K.neg K.one) := fun t => by
    have hz : K.mul (K.add (u t) (K.neg K.one)) (K.add (u ((ring m).add (ring m).one ((ring m).neg t))) (K.neg K.one)) = K.zero := by
      by_cases ht : t = (ring m).zero
      · rw [ht, h0, show (ring m).add (ring m).one ((ring m).neg (ring m).zero) = (ring m).one by lib_ring, h1]
        lib_ring
      · by_cases ht1 : t = (ring m).one
        · rw [ht1, h1]; lib_ring
        · apply hsq t _ ht
          intro h
          apply ht1
          calc t = (ring m).add (ring m).zero t := by lib_ring
            _ = (ring m).add ((ring m).add (ring m).one ((ring m).neg t)) t := by rw [h]
            _ = (ring m).one := by lib_ring
    calc K.mul (u t) (u ((ring m).add (ring m).one ((ring m).neg t)))
        = K.add (K.add (K.add (u t) (u ((ring m).add (ring m).one ((ring m).neg t)))) (K.neg K.one))
            (K.mul (K.add (u t) (K.neg K.one)) (K.add (u ((ring m).add (ring m).one ((ring m).neg t))) (K.neg K.one))) := by lib_ring
      _ = _ := by rw [hz]; lib_ring
  rw [sumK_ext K _ _ pt, sumK_add, sumK_add, hs, flip K m u (ring m).one, hs, Stick1.sumK_negK K (fun _ => K.one)]
  show K.add (K.add K.zero K.zero) (K.neg (sumK K (fun _ : Zp m => K.one) (CMInert.allZ m (m + 1)))) = K.neg K.one
  rw [TeichChar.sum_one_allZ K m (m + 1)]
  show K.add (K.add K.zero K.zero) (K.neg (K.add (nsm K m K.one) K.one)) = K.neg K.one
  rw [hp1]
  lib_ring

end PrimJ

#print axioms PrimJ.prim

lang/PrimJC.lean

/-
  lang/PrimJC — T49 (general ℓ), cycle 829: **the concrete character for primarity**. In a ring Q with π² = 0 and ℓ·1 = 0
  (think Q = ℤ[ζ_ℓ]/(1 − ζ)², π = ζ − 1), p − 1 = ℓ·2h, g a primitive root mod p:
    u(a) = (1 + π)^(dlog a)  (u(0) = 0)    satisfies the hypotheses of PrimJ.prim, hence  Σ_t u(t) u(1 − t) = −1,
  i.e. the Jacobi sum J(χ, χ) of χ(a) = ζ^(dlog a) is primary: J ≡ −1 (mod (1 − ζ)²).
    tri, double_inj, tri_val, u_pow, sum_pows, sum_u, prim_conc
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PrimJ
import lang.DLogF
import lib.RingTac
import lib.Semi
import FiniteRecChain

namespace PrimJC

open ZMod Frobenius YMChain
open FiniteRecChain (ite_pos' ite_neg')

def tri : Nat → Nat
  | 0 => 0
  | k + 1 => tri k + k

theorem double_inj : ∀ a b : Nat, a + a = b + b → a = b
  | 0, 0, _ => rfl
  | 0, b + 1, h => by
    have h2 : b + b + 1 + 1 = 0 := (show b + b + 1 + 1 = b + 1 + (b + 1) by lib_nat).trans h.symm
    nomatch h2
  | a + 1, 0, h => by
    have h2 : a + a + 1 + 1 = 0 := (show a + a + 1 + 1 = a + 1 + (a + 1) by lib_nat).trans h
    nomatch h2
  | a + 1, b + 1, h => by
    have h' : a + a = b + b := Nat.succ.inj (Nat.succ.inj ((show a + a + 1 + 1 = a + 1 + (a + 1) by lib_nat).trans
      (h.trans (show b + 1 + (b + 1) = b + b + 1 + 1 by lib_nat))))
    rw [double_inj a b h']

theorem two_tri : ∀ k : Nat, tri (k + 1) + tri (k + 1) = (k + 1) * k
  | 0 => rfl
  | k + 1 => by
    show tri (k + 1) + (k + 1) + (tri (k + 1) + (k + 1)) = (k + 1 + 1) * (k + 1)
    calc tri (k + 1) + (k + 1) + (tri (k + 1) + (k + 1)) = (tri (k + 1) + tri (k + 1)) + (k + 1) + (k + 1) := by lib_nat
      _ = (k + 1) * k + (k + 1) + (k + 1) := by rw [two_tri k]
      _ = _ := by lib_nat

/-- tri(N) = ℓ·h·(N − 1) when N = ℓ·2h. -/
theorem tri_val (l h M' : Nat) (hN : M' + 1 = l * (2 * h)) : tri (M' + 1) = l * (h * M') :=
  double_inj _ _ (by rw [two_tri, hN]; lib_nat)

variable {Q : Type} (K : DetChain.CRing Q) (π : Q) (hπ : K.mul π π = K.zero)

include hπ in
theorem u_pow (k : Nat) : pw K (K.add K.one π) k = K.add K.one (K.mul (nsm K k K.one) π) := by
  rw [Teich.sq0_pow K π hπ k, Teich.nsm_one_mul K k π]

variable (J : Nat) (g : Zp (J + 1))

/-- u(a) = (1 + π)^(dlog a), u(0) = 0. -/
def u (a : Zp (J + 1)) : Q := if a = (ring (J + 1)).zero then K.zero else pw K (K.add K.one π) (DLogF.dlog J g a)

theorem g_ne_zero (hg : OrdMul.Ord (ring (J + 1)) g (J + 1)) : g ≠ (ring (J + 1)).zero := fun h => by
  have := hg.1; rw [h, HasseCM.pw_zero_succ] at this; exact PowerSum.one_ne_zero' J this.symm

include hπ in
theorem sum_pows (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1)) :
    ∀ k, k ≤ J + 1 → sumK K (u K π J g) (DLog.pows J g k) = K.add (nsm K k K.one) (K.mul (nsm K (tri k) K.one) π)
  | 0, _ => by show K.zero = K.add K.zero (K.mul K.zero π); lib_ring
  | k + 1, hk => by
    have hne := DLog.pw_ne_zero J hpr g (g_ne_zero J g hg) k
    have hu : u K π J g (pw (ring (J + 1)) g k) = K.add K.one (K.mul (nsm K k K.one) π) := by
      unfold u
      rw [ite_neg' _ _ hne, DLogF.dlog_pw J hpr g hg (g_ne_zero J g hg) k hk, u_pow K π hπ]
    show K.add (u K π J g (pw (ring (J + 1)) g k)) (sumK K (u K π J g) (DLog.pows J g k)) =
      K.add (K.add (nsm K k K.one) K.one) (K.mul (nsm K (tri k + k) K.one) π)
    rw [hu, sum_pows hpr hg k (Nat.le_of_succ_le hk), nsm_add]
    lib_ring

include hπ in
/-- **Σ_a u(a) = 0** when ℓ·1 = 0 and p − 1 = ℓ·2h. -/
theorem sum_u (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
    (l h : Nat) (hl : nsm K l K.one = K.zero) (hN : J + 1 = l * (2 * h)) :
    sumK K (u K π J g) (GroupRing.L (J + 1)) = K.zero := by
  show sumK K (u K π J g) (CMInert.allZ (J + 1) (J + 1 + 1)) = K.zero
  rw [DLog.sum_split J K hpr g hg, sum_pows K π hπ J g hpr hg (J + 1) (Nat.le_refl _), tri_val l h J hN]
  have h0 : u K π J g (ring (J + 1)).zero = K.zero := by unfold u; rw [ite_pos' _ _ rfl]
  rw [h0, hN, nsm_mul_nat K l K.one (2 * h), nsm_mul_nat K l K.one (h * J), hl, Teich.nsm_zero, Teich.nsm_zero]
  lib_ring

include hπ in
/-- **J(χ, χ) ≡ −1 (mod (1 − ζ)²)** for χ = ζ^(dlog ·). -/
theorem prim_conc (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
    (l h : Nat) (hl : nsm K l K.one = K.zero) (hN : J + 1 = l * (2 * h)) :
    sumK K (fun t => K.mul (u K π J g t) (u K π J g ((ring (J + 1)).add (ring (J + 1)).one ((ring (J + 1)).neg t))))
      (GroupRing.L (J + 1)) = K.neg K.one := by
  apply PrimJ.prim K (J + 1) (u K π J g)
  · unfold u; rw [ite_pos' _ _ rfl]
  · have e : DLogF.dlog J g (ring (J + 1)).one = 0 := DLogF.dlog_pw J hpr g hg (g_ne_zero J g hg) 0 (Nat.zero_lt_succ J)
    unfold u; rw [ite_neg' _ _ (PowerSum.one_ne_zero' J), e]; try rfl
  · intro t s ht hs
    unfold u
    rw [ite_neg' _ _ ht, ite_neg' _ _ hs, u_pow K π hπ, u_pow K π hπ]
    calc K.mul (K.add (K.add K.one (K.mul (nsm K (DLogF.dlog J g t) K.one) π)) (K.neg K.one))
          (K.add (K.add K.one (K.mul (nsm K (DLogF.dlog J g s) K.one) π)) (K.neg K.one))
        = K.mul (K.mul (nsm K (DLogF.dlog J g t) K.one) (nsm K (DLogF.dlog J g s) K.one)) (K.mul π π) := by lib_ring
      _ = K.zero := by rw [hπ]; lib_ring
  · exact sum_u K π hπ J g hpr hg l h hl hN
  · rw [hN, nsm_mul_nat K l K.one (2 * h), hl, Teich.nsm_zero]

end PrimJC

#print axioms PrimJC.double_inj
#print axioms PrimJC.two_tri
#print axioms PrimJC.tri_val
#print axioms PrimJC.u_pow
#print axioms PrimJC.g_ne_zero
#print axioms PrimJC.sum_pows
#print axioms PrimJC.sum_u
#print axioms PrimJC.prim_conc

lang/PrimA.lean

/-
  lang/PrimA — T49 (general ℓ), cycle 830: **the ℓ-th power of the Gauss sum is primary**, G(χ)^ℓ ≡ −1 (mod (1 − ζ)²),
  in computational form. NthRecip writes G_χ^ℓ = A·(pζ⁰ − N) with A = (∏_{i ≤ k} J(χ^i, χ))·χ(ι(−1)), k = ℓ − 2,
  and the n-th power residue criterion reads χ(q⁻¹) = (A p)^m. Here, in a ring Q with π² = 0 and ℓ·1 = 0
  (think Q = ℤ[ζ_ℓ]/(1 − ζ)², π = ζ − 1), p − 1 = ℓ·2h, g a primitive root, u = (1 + π)^(dlog ·) (PrimJC.u):
    prim2      Σ_t u(t) w(1 − t) = −1 for two characters u, w of the PrimJ shape
    v_val, v_sum, v_sum_all, v_zero, v_one, v_lin   the powers u^c of u
    jac_c      J(u^(c+1), u) = −1          (every J(χ^i, χ) is primary)
    jpr_val    ∏_{i ≤ k} J(u^i, u) = (−1)^k
    iv_neg     ι(−1) = −1 in 𝔽_p
    u_neg      u(−1) = 1                   (g^(ℓh) = −1, dlog(−1) = ℓh, ℓ·1 = 0)
    pp_one     p·1 = 1 in Q
    gauss_prim A·p = −1 for k odd (k = ℓ − 2), i.e. G(χ)^ℓ ≡ −1 (mod (1 − ζ)²) — the general form of Quint18.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PrimJC
import lang.GaussCritN
import lang.Euler
import lib.RingTac
import lib.Semi
import FiniteRecChain

namespace PrimA

open ZMod Frobenius YMChain GroupRing
open FiniteRecChain (ite_pos' ite_neg')

variable {Q : Type} (K : DetChain.CRing Q)

/-- **Σ_t u(t) w(1 − t) = −1** for two maps u, w with u(0) = w(0) = 0, u(1) = w(1) = 1, (u_t − 1)(w_s − 1) = 0, Σu = Σw = 0. -/
theorem prim2 (m : Nat) (u w : Zp m → Q) (hu0 : u (ring m).zero = K.zero) (hu1 : u (ring m).one = K.one)
    (_hw0 : w (ring m).zero = K.zero) (hw1 : w (ring m).one = K.one)
    (hsq : ∀ t s, t ≠ (ring m).zero → s ≠ (ring m).zero → K.mul (K.add (u t) (K.neg K.one)) (K.add (w s) (K.neg K.one)) = K.zero)
    (hsu : sumK K u (L m) = K.zero) (hsw : sumK K w (L m) = K.zero) (hp1 : nsm K m K.one = K.zero) :
    sumK K (fun t => K.mul (u t) (w ((ring m).add (ring m).one ((ring m).neg t)))) (L m) = K.neg K.one := by
  have pt : ∀ t, K.mul (u t) (w ((ring m).add (ring m).one ((ring m).neg t))) =
      K.add (K.add (u t) (w ((ring m).add (ring m).one ((ring m).neg t)))) (K.neg K.one) := fun t => by
    have hz : K.mul (K.add (u t) (K.neg K.one)) (K.add (w ((ring m).add (ring m).one ((ring m).neg t))) (K.neg K.one)) = K.zero := by
      by_cases ht : t = (ring m).zero
      · rw [ht, hu0, show (ring m).add (ring m).one ((ring m).neg (ring m).zero) = (ring m).one by lib_ring, hw1]
        lib_ring
      · by_cases ht1 : t = (ring m).one
        · rw [ht1, hu1]; lib_ring
        · apply hsq t _ ht
          intro h
          apply ht1
          calc t = (ring m).add (ring m).zero t := by lib_ring
            _ = (ring m).add ((ring m).add (ring m).one ((ring m).neg t)) t := by rw [h]
            _ = (ring m).one := by lib_ring
    calc K.mul (u t) (w ((ring m).add (ring m).one ((ring m).neg t)))
        = K.add (K.add (K.add (u t) (w ((ring m).add (ring m).one ((ring m).neg t)))) (K.neg K.one))
            (K.mul (K.add (u t) (K.neg K.one)) (K.add (w ((ring m).add (ring m).one ((ring m).neg t))) (K.neg K.one))) := by lib_ring
      _ = _ := by rw [hz]; lib_ring
  rw [sumK_ext K _ _ pt, sumK_add, sumK_add, hsu, flip K m w (ring m).one, hsw, Stick1.sumK_negK K (fun _ => K.one)]
  show K.add (K.add K.zero K.zero) (K.neg (sumK K (fun _ : Zp m => K.one) (CMInert.allZ m (m + 1)))) = K.neg K.one
  rw [TeichChar.sum_one_allZ K m (m + 1)]
  show K.add (K.add K.zero K.zero) (K.neg (K.add (nsm K m K.one) K.one)) = K.neg K.one
  rw [hp1]
  lib_ring

theorem pw_one : ∀ c : Nat, pw K K.one c = K.one
  | 0 => rfl
  | c + 1 => by show K.mul (pw K K.one c) K.one = K.one; rw [pw_one c]; lib_ring

variable (π : Q) (hπ : K.mul π π = K.zero) (j : Nat) (g : Zp (j + 1 + 1))

/-- v_c = u^c = (1 + π)^(c·dlog ·). -/
def v (c : Nat) (a : Zp (j + 1 + 1)) : Q := pw K (PrimJC.u K π (j + 1) g a) c

include hπ in
theorem sq0_lin (c : Nat) (a : Q) :
    pw K (K.add K.one (K.mul a π)) c = K.add K.one (K.mul (K.mul (nsm K c K.one) a) π) := by
  have hx : K.mul (K.mul a π) (K.mul a π) = K.zero := by
    calc K.mul (K.mul a π) (K.mul a π) = K.mul (K.mul a a) (K.mul π π) := by lib_ring
      _ = K.zero := by rw [hπ]; lib_ring
  rw [Teich.sq0_pow K (K.mul a π) hx c, Teich.nsm_one_mul K c (K.mul a π)]
  lib_ring

include hπ in
/-- u(t) = 1 + dlog(t)·π and u^c(t) = 1 + c·dlog(t)·π for t ≠ 0. -/
theorem v_lin (c : Nat) (t : Zp (j + 1 + 1)) (ht : t ≠ (ring (j + 1 + 1)).zero) :
    v K π j g c t = K.add K.one (K.mul (K.mul (nsm K c K.one) (nsm K (DLogF.dlog (j + 1) g t) K.one)) π) := by
  unfold v PrimJC.u
  rw [ite_neg' _ _ ht, PrimJC.u_pow K π hπ, sq0_lin K π hπ]

include hπ in
theorem u_lin (t : Zp (j + 1 + 1)) (ht : t ≠ (ring (j + 1 + 1)).zero) :
    PrimJC.u K π (j + 1) g t = K.add K.one (K.mul (nsm K (DLogF.dlog (j + 1) g t) K.one) π) := by
  unfold PrimJC.u
  rw [ite_neg' _ _ ht, PrimJC.u_pow K π hπ]

theorem u_zero : PrimJC.u K π (j + 1) g (ring (j + 1 + 1)).zero = K.zero := by
  unfold PrimJC.u; rw [ite_pos' _ _ rfl]

theorem v_zero (c : Nat) : v K π j g (c + 1) (ring (j + 1 + 1)).zero = K.zero := by
  unfold v; rw [u_zero K π j g]; exact HasseCM.pw_zero_succ K c

theorem u_one (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) :
    PrimJC.u K π (j + 1) g (ring (j + 1 + 1)).one = K.one := by
  have e : DLogF.dlog (j + 1) g (ring (j + 1 + 1)).one = 0 :=
    DLogF.dlog_pw (j + 1) hpr g hg (PrimJC.g_ne_zero (j + 1) g hg) 0 (Nat.zero_lt_succ (j + 1))
  unfold PrimJC.u; rw [ite_neg' _ _ (PowerSum.one_ne_zero' (j + 1)), e]; try rfl

theorem v_one (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (c : Nat) :
    v K π j g c (ring (j + 1 + 1)).one = K.one := by
  unfold v; rw [u_one K π j g hpr hg]; exact pw_one K c

include hπ in
theorem v_val (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (c k : Nat) (hk : k < j + 1 + 1) :
    v K π j g c (pw (ring (j + 1 + 1)) g k) = K.add K.one (K.mul (K.mul (nsm K c K.one) (nsm K k K.one)) π) := by
  rw [v_lin K π hπ j g c _ (DLog.pw_ne_zero (j + 1) hpr g (PrimJC.g_ne_zero (j + 1) g hg) k),
    DLogF.dlog_pw (j + 1) hpr g hg (PrimJC.g_ne_zero (j + 1) g hg) k hk]

include hπ in
theorem v_sum (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (c : Nat) :
    ∀ k, k ≤ j + 1 + 1 → sumK K (v K π j g c) (DLog.pows (j + 1) g k) =
      K.add (nsm K k K.one) (K.mul (K.mul (nsm K c K.one) (nsm K (PrimJC.tri k) K.one)) π)
  | 0, _ => by show K.zero = K.add K.zero (K.mul (K.mul (nsm K c K.one) K.zero) π); lib_ring
  | k + 1, hk => by
    show K.add (v K π j g c (pw (ring (j + 1 + 1)) g k)) (sumK K (v K π j g c) (DLog.pows (j + 1) g k)) =
      K.add (K.add (nsm K k K.one) K.one) (K.mul (K.mul (nsm K c K.one) (nsm K (PrimJC.tri k + k) K.one)) π)
    rw [v_val K π hπ j g hpr hg c k hk, v_sum hpr hg c k (Nat.le_of_succ_le hk), nsm_add]
    lib_ring

include hπ in
/-- **Σ_a u^(c+1)(a) = 0** when ℓ·1 = 0 and p − 1 = ℓ·2h. -/
theorem v_sum_all (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (l h : Nat) (hl : nsm K l K.one = K.zero)
    (hN : j + 1 + 1 = l * (2 * h)) (c : Nat) :
    sumK K (v K π j g (c + 1)) (L (j + 1 + 1)) = K.zero := by
  show sumK K (v K π j g (c + 1)) (CMInert.allZ (j + 1 + 1) (j + 1 + 1 + 1)) = K.zero
  rw [DLog.sum_split (j + 1) K hpr g hg, v_sum K π hπ j g hpr hg (c + 1) (j + 1 + 1) (Nat.le_refl _),
    PrimJC.tri_val l h (j + 1) hN, v_zero K π j g c, hN, nsm_mul_nat K l K.one (2 * h), nsm_mul_nat K l K.one (h * (j + 1)), hl,
    Teich.nsm_zero, Teich.nsm_zero]
  lib_ring

theorem hp1 (l h : Nat) (hl : nsm K l K.one = K.zero) (hN : j + 1 + 1 = l * (2 * h)) :
    nsm K (j + 1 + 1) K.one = K.zero := by
  rw [hN, nsm_mul_nat K l K.one (2 * h), hl, Teich.nsm_zero]

include hπ in
/-- **J(χ^(c+1), χ) ≡ −1 (mod (1 − ζ)²)**. -/
theorem jac_c (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (l h : Nat) (hl : nsm K l K.one = K.zero)
    (hN : j + 1 + 1 = l * (2 * h)) (c : Nat) :
    CharSumGen.J2 K j (CharSumGen.psp K j (PrimJC.u K π (j + 1) g) (c + 1)) (PrimJC.u K π (j + 1) g) = K.neg K.one := by
  show sumK K (fun t => K.mul (v K π j g (c + 1) t)
      (PrimJC.u K π (j + 1) g ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))) (L (j + 1 + 1)) = K.neg K.one
  apply prim2 K (j + 1 + 1) (v K π j g (c + 1)) (PrimJC.u K π (j + 1) g)
  · exact v_zero K π j g c
  · exact v_one K π j g hpr hg (c + 1)
  · exact u_zero K π j g
  · exact u_one K π j g hpr hg
  · intro t s ht hs
    rw [v_lin K π hπ j g (c + 1) t ht, u_lin K π hπ j g s hs]
    calc K.mul (K.add (K.add K.one (K.mul (K.mul (nsm K (c + 1) K.one) (nsm K (DLogF.dlog (j + 1) g t) K.one)) π)) (K.neg K.one))
          (K.add (K.add K.one (K.mul (nsm K (DLogF.dlog (j + 1) g s) K.one) π)) (K.neg K.one))
        = K.mul (K.mul (K.mul (nsm K (c + 1) K.one) (nsm K (DLogF.dlog (j + 1) g t) K.one)) (nsm K (DLogF.dlog (j + 1) g s) K.one))
            (K.mul π π) := by lib_ring
      _ = K.zero := by rw [hπ]; lib_ring
  · exact v_sum_all K π hπ j g hpr hg l h hl hN c
  · exact PrimJC.sum_u K π hπ (j + 1) g hpr hg l h hl hN
  · exact hp1 K j l h hl hN

include hπ in
/-- **∏_{i ≤ k} J(χ^i, χ) ≡ (−1)^k (mod (1 − ζ)²)**. -/
theorem jpr_val (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (l h : Nat) (hl : nsm K l K.one = K.zero)
    (hN : j + 1 + 1 = l * (2 * h)) :
    ∀ k, CharSumGen.jpr K j (PrimJC.u K π (j + 1) g) k = pw K (K.neg K.one) k
  | 0 => rfl
  | k + 1 => by
    show K.mul (CharSumGen.jpr K j (PrimJC.u K π (j + 1) g) k)
        (CharSumGen.J2 K j (CharSumGen.psp K j (PrimJC.u K π (j + 1) g) (k + 1)) (PrimJC.u K π (j + 1) g)) =
      K.mul (pw K (K.neg K.one) k) (K.neg K.one)
    rw [jpr_val hpr hg l h hl hN k, jac_c K π hπ j g hpr hg l h hl hN k]

theorem neg_one_ne : (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero := fun h => by
  apply PowerSum.one_ne_zero' (j + 1)
  calc (ring (j + 1 + 1)).one = (ring (j + 1 + 1)).neg ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) := by lib_ring
    _ = (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).zero := by rw [h]
    _ = (ring (j + 1 + 1)).zero := by lib_ring

/-- **ι(−1) = −1** in 𝔽_p. -/
theorem iv_neg (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) :
    CharSumGen.iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one := by
  have e := CharSumGen.iv_mul j hpr _ (neg_one_ne j)
  calc CharSumGen.iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)
      = (ring (j + 1 + 1)).neg ((ring (j + 1 + 1)).mul (CharSumGen.iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))
          ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) := by lib_ring
    _ = (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one := by rw [e]

theorem lt_double : ∀ M : Nat, 0 < M → M < M + M
  | 0, h0 => absurd h0 (Nat.lt_irrefl 0)
  | M + 1, _ => Nat.lt_of_succ_le (Nat.add_le_add_left (Nat.succ_le_succ (Nat.zero_le M)) (M + 1))

theorem not_dvd_small (N M : Nat) (h0 : 0 < M) (hM : M < N) (hd : N ∣ M) : False := by
  obtain ⟨c, hc⟩ := hd
  cases c with
  | zero => rw [hc] at h0; exact Nat.lt_irrefl 0 h0
  | succ c =>
    have : N ≤ M := by
      rw [hc]
      exact Nat.le_trans (Nat.le_add_left N (N * c)) (Nat.le_of_eq (show N * c + N = N * (c + 1) from rfl))
    exact Nat.lt_irrefl M (Nat.lt_of_lt_of_le hM this)

include hπ in
/-- **χ(−1) ≡ 1**: g^(ℓh) = −1, so dlog(−1) = ℓh and (1 + π)^(ℓh) = 1 when ℓ·1 = 0. -/
theorem u_neg (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (l h : Nat) (hl : nsm K l K.one = K.zero)
    (hN : j + 1 + 1 = l * (2 * h)) :
    PrimJC.u K π (j + 1) g ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = K.one := by
  have hM : j + 1 + 1 = l * h + l * h := hN.trans (by lib_nat)
  have hpos : 0 < l * h := by
    cases e : l * h with
    | zero => rw [e] at hM; exact Nat.noConfusion hM
    | succ M => exact Nat.zero_lt_succ M
  have hlt : l * h < j + 1 + 1 := by rw [hM]; exact lt_double (l * h) hpos
  have hsq : (ring (j + 1 + 1)).mul (pw (ring (j + 1 + 1)) g (l * h)) (pw (ring (j + 1 + 1)) g (l * h)) = (ring (j + 1 + 1)).one := by
    rw [← HasseCM.pw_add', ← hM]; exact hg.1
  have hy : pw (ring (j + 1 + 1)) g (l * h) = (ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one := by
    cases Euler.sq_one (ring (j + 1 + 1)) (ZMod.nozd (j + 1) hpr) _ hsq with
    | inl h1 => exact absurd (hg.2 (l * h) h1) (fun hd => not_dvd_small (j + 1 + 1) (l * h) hpos hlt hd)
    | inr h1 => exact h1
  rw [u_lin K π hπ j g _ (neg_one_ne j), ← hy, DLogF.dlog_pw (j + 1) hpr g hg (PrimJC.g_ne_zero (j + 1) g hg) (l * h) hlt,
    nsm_mul_nat K l K.one h, hl, Teich.nsm_zero]
  lib_ring

/-- p·1 = 1 in Q. -/
theorem pp_one (l h : Nat) (hl : nsm K l K.one = K.zero) (hN : j + 1 + 1 = l * (2 * h)) : CharSumGen.Pp K j = K.one := by
  show sumK K (fun _ : Zp (j + 1 + 1) => K.one) (CMInert.allZ (j + 1 + 1) (j + 1 + 1 + 1)) = K.one
  rw [TeichChar.sum_one_allZ K (j + 1 + 1) (j + 1 + 1 + 1)]
  show K.add (nsm K (j + 1 + 1) K.one) K.one = K.one
  rw [hp1 K j l h hl hN]
  lib_ring

theorem pw_neg_odd : ∀ r : Nat, pw K (K.neg K.one) (2 * r + 1) = K.neg K.one
  | 0 => by show K.mul K.one (K.neg K.one) = K.neg K.one; lib_ring
  | r + 1 => by
    show K.mul (K.mul (pw K (K.neg K.one) (2 * r + 1)) (K.neg K.one)) (K.neg K.one) = K.neg K.one
    rw [pw_neg_odd r]; lib_ring

include hπ in
/-- **A·p ≡ −1 (mod (1 − ζ)²)**, A = (∏_{i ≤ k} J(χ^i, χ))·χ(ι(−1)), k odd (k = ℓ − 2): the coefficient of
    G(χ)^ℓ = A·(pζ⁰ − N) (CharSumGen.gpow_n) and the base of the criterion χ(q⁻¹) = (A p)^m (NthRecip) is primary. -/
theorem gauss_prim (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (l h : Nat) (hl : nsm K l K.one = K.zero)
    (hN : j + 1 + 1 = l * (2 * h)) (r : Nat) :
    K.mul (K.mul (CharSumGen.jpr K j (PrimJC.u K π (j + 1) g) (2 * r + 1))
        (PrimJC.u K π (j + 1) g (CharSumGen.iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))
      (CharSumGen.Pp K j) = K.neg K.one := by
  rw [jpr_val K π hπ j g hpr hg l h hl hN, pw_neg_odd K r, iv_neg j hpr, u_neg K π hπ j g hpr hg l h hl hN,
    pp_one K j l h hl hN]
  lib_ring

end PrimA

#print axioms PrimA.prim2
#print axioms PrimA.pw_one
#print axioms PrimA.sq0_lin
#print axioms PrimA.v_lin
#print axioms PrimA.u_lin
#print axioms PrimA.u_zero
#print axioms PrimA.v_zero
#print axioms PrimA.u_one
#print axioms PrimA.v_one
#print axioms PrimA.v_val
#print axioms PrimA.v_sum
#print axioms PrimA.v_sum_all
#print axioms PrimA.hp1
#print axioms PrimA.jac_c
#print axioms PrimA.jpr_val
#print axioms PrimA.neg_one_ne
#print axioms PrimA.iv_neg
#print axioms PrimA.lt_double
#print axioms PrimA.not_dvd_small
#print axioms PrimA.u_neg
#print axioms PrimA.pp_one
#print axioms PrimA.pw_neg_odd
#print axioms PrimA.gauss_prim

#print axioms: 36 theorems, no axioms.

78.370 Eisenstein’s relation without \(p\) for every \(n\)

In characteristic \(q\) with \(p\not \equiv 0\), \((q-1)\mid m\) removes \(p^m\) from the \(n\)-th power residue criterion.

\[ q^e=nm+1,\ (q-1)\mid m\ \Rightarrow \ \psi (q^{-e})=A^m. \]

lang/NthEis.lean

/-
  lang/NthEis — T49 (general ℓ, Eisenstein's relation without p): the general form of Quint19.
  In a ring E of characteristic q with a ring map ι : 𝔽_q → E, p ≢ 0 (mod q) gives p^(q−1) = 1 (Fermat), so when
  (q − 1) | m the factor p^m in the n-th power residue criterion (NthRecipPow) disappears:
    ψ(q^(−e)) = A^m,   A = ∏_(i ≤ k) J(ψ^i, ψ)·ψ(ι(−1)),   q^e = n m + 1,
  i.e. χ_p(q)^(−e) ≡ A^((q^e − 1)/n) (mod q) for the element A that is primary by lang/PrimA.
    fermat_unit   x ≠ 0 in 𝔽_q ⇒ x^(q−1) = 1
    iota_nsm      a ring map sends n·1 to n·1
    pp_nsm        P = Σ_(x ∈ 𝔽_p) 1 = p·1
    pp_pow        P^((q−1)t) = 1 in E
    nth_eis       ψ(q^(−e)) = A^m when m = (q − 1)t
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.NthRecipPow
import lang.TeichChar3
import lib.RingTac

namespace NthEis

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

/-- **Fermat with cancellation**: x ≠ 0 in 𝔽_q (q = k' + 2 prime) ⇒ x^(q−1) = 1. -/
theorem fermat_unit (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (x : Zp (k' + 1))
    (hx : x ≠ (ring (k' + 1)).zero) : pw (ring (k' + 1)) x (k' + 1) = (ring (k' + 1)).one := by
  have hf : (ring (k' + 1)).mul (pw (ring (k' + 1)) x (k' + 1)) x = x := ZMod.fermat k' hq x
  have hz : (ring (k' + 1)).mul ((ring (k' + 1)).add (pw (ring (k' + 1)) x (k' + 1)) ((ring (k' + 1)).neg (ring (k' + 1)).one)) x
      = (ring (k' + 1)).zero := by
    calc (ring (k' + 1)).mul ((ring (k' + 1)).add (pw (ring (k' + 1)) x (k' + 1)) ((ring (k' + 1)).neg (ring (k' + 1)).one)) x
        = (ring (k' + 1)).add ((ring (k' + 1)).mul (pw (ring (k' + 1)) x (k' + 1)) x) ((ring (k' + 1)).neg x) := by lib_ring
      _ = (ring (k' + 1)).add x ((ring (k' + 1)).neg x) := by rw [hf]
      _ = (ring (k' + 1)).zero := by lib_ring
  cases ZMod.nozd k' hq _ _ hz with
  | inl h =>
    calc pw (ring (k' + 1)) x (k' + 1)
        = (ring (k' + 1)).add ((ring (k' + 1)).add (pw (ring (k' + 1)) x (k' + 1)) ((ring (k' + 1)).neg (ring (k' + 1)).one))
            (ring (k' + 1)).one := by lib_ring
      _ = (ring (k' + 1)).add (ring (k' + 1)).zero (ring (k' + 1)).one := by rw [h]
      _ = (ring (k' + 1)).one := by lib_ring
  | inr h => exact absurd h hx

section Hom
variable {R S : Type} (T : CRing R) (E : CRing S) (ι : R → S)

/-- a ring map sends n·1 to n·1. -/
theorem iota_nsm (hadd : ∀ a b, ι (T.add a b) = E.add (ι a) (ι b)) (hone : ι T.one = E.one) (hzero : ι T.zero = E.zero) :
    ∀ n, ι (nsm T n T.one) = nsm E n E.one
  | 0 => hzero
  | n + 1 => by
    show ι (T.add (nsm T n T.one) T.one) = E.add (nsm E n E.one) E.one
    rw [hadd, hone, iota_nsm hadd hone hzero n]

/-- a ring map commutes with powers. -/
theorem iota_pw (hmul : ∀ a b, ι (T.mul a b) = E.mul (ι a) (ι b)) (hone : ι T.one = E.one) (x : R) :
    ∀ k, ι (pw T x k) = pw E (ι x) k
  | 0 => hone
  | k + 1 => by
    show ι (T.mul (pw T x k) x) = E.mul (pw E (ι x) k) (ι x)
    rw [hmul, iota_pw hmul hone x k]
end Hom

theorem pw_one {S : Type} (E : CRing S) : ∀ t, pw E E.one t = E.one
  | 0 => rfl
  | t + 1 => by
    show E.mul (pw E E.one t) E.one = E.one
    rw [pw_one E t]
    lib_ring

/-- P = Σ_(x ∈ 𝔽_p) 1 = p·1. -/
theorem pp_nsm {S : Type} (E : CRing S) (j : Nat) : Pp E j = nsm E (j + 1 + 1 + 1) E.one :=
  TeichChar.sum_one_allZ E (j + 1 + 1) (j + 1 + 1 + 1)

/-- **P^((q−1)t) = 1** in E when p ≢ 0 (mod q). -/
theorem pp_pow {S : Type} (E : CRing S) (j k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1)
    (ι : Zp (k' + 1) → S) (hadd : ∀ a b, ι ((ring (k' + 1)).add a b) = E.add (ι a) (ι b))
    (hmul : ∀ a b, ι ((ring (k' + 1)).mul a b) = E.mul (ι a) (ι b)) (hone : ι (ring (k' + 1)).one = E.one)
    (hzero : ι (ring (k' + 1)).zero = E.zero)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (t : Nat) :
    pw E (Pp E j) ((k' + 1) * t) = E.one := by
  rw [pp_nsm E j, ← iota_nsm (ring (k' + 1)) E ι hadd hone hzero (j + 1 + 1 + 1), ← TwoPower.pwpow,
    ← iota_pw (ring (k' + 1)) E ι hmul hone _ (k' + 1), fermat_unit k' hq _ hp, hone]
  exact pw_one E t

/-- **Eisenstein's relation for every n**: if q^e = n m + 1 with (q − 1) | m and p ≢ 0 (mod q), then
    ψ(q^(−e)) = A^m, A = ∏_(i ≤ k) J(ψ^i, ψ)·ψ(ι(−1)) (n = k + 2). -/
theorem nth_eis {S : Type} (E : CRing S) (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (ψ : Zp (j + 1 + 1) → S)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (k : Nat)
    (hnt : ∀ i, i < k → sumK E (fun a => pw E (ψ a) (i + 2)) (LL j) = E.zero) (hinv : ∀ x, pw E (ψ x) (k + 1) = ψ (iv j x))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hE : nsm E (k' + 1 + 1) E.one = E.zero) (e : Nat)
    (hfix : ∀ a, pw E (ψ a) ((k' + 1 + 1) ^ e) = ψ a)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (m : Nat) (hqn : (k' + 1 + 1) ^ e = (k + 2) * m + 1)
    (ι : Zp (k' + 1) → S) (hadd : ∀ a b, ι ((ring (k' + 1)).add a b) = E.add (ι a) (ι b))
    (hmul : ∀ a b, ι ((ring (k' + 1)).mul a b) = E.mul (ι a) (ι b)) (hone : ι (ring (k' + 1)).one = E.one)
    (hzero : ι (ring (k' + 1)).zero = E.zero)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (t : Nat) (ht : m = (k' + 1) * t) :
    ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) =
      pw E (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) m := by
  rw [CharSumGen.nth_recip_pow E j hpr ψ hm h1 h0 hs k hnt hinv k' hq hE e hfix hQ m hqn, Frobenius.pw_mul, ht,
    pp_pow E j k' hq ι hadd hmul hone hzero hp t]
  lib_ring

end NthEis

#print axioms NthEis.fermat_unit
#print axioms NthEis.iota_nsm
#print axioms NthEis.iota_pw
#print axioms NthEis.pw_one
#print axioms NthEis.pp_nsm
#print axioms NthEis.pp_pow
#print axioms NthEis.nth_eis

#print axioms: 7 theorems, no axioms.

78.371 Eisenstein’s relation for a primary element of \(\mathbb Z[C_\ell ]\)

One element \(A\in \mathbb Z[C_\ell ]\) is primary and satisfies Eisenstein’s relation over \(\mathbb F_q[C_\ell ]/(N)\) for every prime \(\ell \) and \(q\ne \ell ,p\).

\[ A\,p\equiv -1\ \bmod (\ell ,(\sigma -1)^2),\qquad \psi _u(q^{-e})=\varphi _u(A)^M\quad (q^e=\ell M+1,\ (q-1)\mid M). \]

lang/JprHom.lean

/-
  lang/JprHom — T49 (general ℓ): **Jacobi sums and their products commute with ring maps**.
  For a ring map φ : E → F and ψ : 𝔽_p → E,
    sumK_congr   pointwise equal summands give equal sums (no function extensionality)
    j2_map       φ J(χ, μ) = J(φ∘χ, φ∘μ)
    jpr_map      φ (∏_(i ≤ k) J(ψ^i, ψ)) = ∏_(i ≤ k) J((φ∘ψ)^i, φ∘ψ)
    a_map        φ (∏ J(ψ^i, ψ)·ψ(ι(−1))) = ∏ J((φ∘ψ)^i, φ∘ψ)·(φ∘ψ)(ι(−1))
  so the element A of the residue criterion (NthRecipPow, NthEis) and of the primary normalisation (PrimA) are images of
  one element of any ring through which the character factors.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.GaussPowN

namespace JprHom

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

variable {S F : Type} (E : CRing S) (K : CRing F) (φ : S → F) (j : Nat)

theorem sumK_congr {A : Type} (f g : A → F) (h : ∀ a, f a = g a) : ∀ l : List A, sumK K f l = sumK K g l
  | [] => rfl
  | a :: l => by
    show K.add (f a) (sumK K f l) = K.add (g a) (sumK K g l)
    rw [h a, sumK_congr f g h l]

theorem map_sum {A : Type} (hadd : ∀ a b, φ (E.add a b) = K.add (φ a) (φ b)) (hzero : φ E.zero = K.zero) (f : A → S) :
    ∀ l : List A, φ (sumK E f l) = sumK K (fun a => φ (f a)) l
  | [] => hzero
  | a :: l => by
    show φ (E.add (f a) (sumK E f l)) = K.add (φ (f a)) (sumK K (fun a => φ (f a)) l)
    rw [hadd, map_sum hadd hzero f l]

theorem map_pw (hmul : ∀ a b, φ (E.mul a b) = K.mul (φ a) (φ b)) (hone : φ E.one = K.one) (a : S) :
    ∀ k, φ (pw E a k) = pw K (φ a) k
  | 0 => hone
  | k + 1 => by
    show φ (E.mul (pw E a k) a) = K.mul (pw K (φ a) k) (φ a)
    rw [hmul, map_pw hmul hone a k]

/-- **φ J(χ, μ) = J(φ∘χ, φ∘μ)**. -/
theorem j2_map (hadd : ∀ a b, φ (E.add a b) = K.add (φ a) (φ b)) (hzero : φ E.zero = K.zero)
    (hmul : ∀ a b, φ (E.mul a b) = K.mul (φ a) (φ b)) (χ μ : Zp (j + 1 + 1) → S) :
    φ (J2 E j χ μ) = J2 K j (fun x => φ (χ x)) (fun x => φ (μ x)) := by
  show φ (sumK E (fun t => E.mul (χ t) (μ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))) (LL j)) =
    sumK K (fun t => K.mul (φ (χ t)) (φ (μ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))))) (LL j)
  rw [map_sum E K φ hadd hzero]
  exact sumK_congr K _ _ (fun t => hmul _ _) (LL j)

/-- **φ (∏_(i ≤ k) J(ψ^i, ψ)) = ∏_(i ≤ k) J((φ∘ψ)^i, φ∘ψ)**. -/
theorem jpr_map (hadd : ∀ a b, φ (E.add a b) = K.add (φ a) (φ b)) (hzero : φ E.zero = K.zero)
    (hmul : ∀ a b, φ (E.mul a b) = K.mul (φ a) (φ b)) (hone : φ E.one = K.one) (ψ : Zp (j + 1 + 1) → S) :
    ∀ k, φ (jpr E j ψ k) = jpr K j (fun x => φ (ψ x)) k
  | 0 => hone
  | k + 1 => by
    show φ (E.mul (jpr E j ψ k) (J2 E j (psp E j ψ (k + 1)) ψ)) =
      K.mul (jpr K j (fun x => φ (ψ x)) k) (J2 K j (psp K j (fun x => φ (ψ x)) (k + 1)) (fun x => φ (ψ x)))
    rw [hmul, jpr_map hadd hzero hmul hone ψ k, j2_map E K φ j hadd hzero hmul]
    show K.mul (jpr K j (fun x => φ (ψ x)) k)
        (sumK K (fun t => K.mul (φ (pw E (ψ t) (k + 1))) (φ (ψ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))))) (LL j)) =
      K.mul (jpr K j (fun x => φ (ψ x)) k)
        (sumK K (fun t => K.mul (pw K (φ (ψ t)) (k + 1)) (φ (ψ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))))) (LL j))
    exact congrArg (K.mul (jpr K j (fun x => φ (ψ x)) k)) (sumK_congr K _ _ (fun t =>
      congrArg (fun z => K.mul z (φ (ψ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))))
        (map_pw E K φ hmul hone (ψ t) (k + 1))) (LL j))

/-- **the residue-criterion element maps to the residue-criterion element**. -/
theorem a_map (hadd : ∀ a b, φ (E.add a b) = K.add (φ a) (φ b)) (hzero : φ E.zero = K.zero)
    (hmul : ∀ a b, φ (E.mul a b) = K.mul (φ a) (φ b)) (hone : φ E.one = K.one) (ψ : Zp (j + 1 + 1) → S) (k : Nat) :
    φ (E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) =
      K.mul (jpr K j (fun x => φ (ψ x)) k) (φ (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) := by
  rw [hmul, jpr_map E K φ j hadd hzero hmul hone ψ k]

end JprHom

#print axioms JprHom.sumK_congr
#print axioms JprHom.map_sum
#print axioms JprHom.map_pw
#print axioms JprHom.j2_map
#print axioms JprHom.jpr_map
#print axioms JprHom.a_map

lang/EisBridge.lean

/-
  lang/EisBridge — T49 (general ℓ): **Eisenstein's relation for a primary element**, every odd ℓ.
  Let χ : 𝔽_p → R be a character with values in any ring R (e.g. ℤ[C_ℓ] or ℤ[ζ_ℓ]) and A = ∏_(i ≤ k) J(χ^i, χ)·χ(ι(−1)).
  For ring maps φ₁ : R → Q (ζ ↦ 1 + π, π² = 0, ℓ·1 = 0) and φ₂ : R → E (characteristic q) through which χ becomes the
  characters of PrimA and NthEis:
    jpr_congr     pointwise equal characters have equal Jacobi products (no function extensionality)
    a_primary     φ₁(A)·p = −1, i.e. A·p ≡ −1 (mod (1 − ζ)²)                          (PrimA, k = ℓ − 2)
    a_eis         ψ(q^(−e)) = φ₂(A)^m when q^e = ℓm + 1, (q − 1) | m, p ≢ 0 (mod q)  (NthEis)
  so one element A of R is primary and satisfies χ_p(q)^(−e) ≡ A^((q^e − 1)/ℓ) (mod q).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.JprHom
import lang.NthEis
import lang.PrimA

namespace EisBridge

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

theorem j2_congr {S : Type} (E : CRing S) (j : Nat) (χ χ' μ μ' : Zp (j + 1 + 1) → S) (hχ : ∀ x, χ x = χ' x)
    (hμ : ∀ x, μ x = μ' x) : J2 E j χ μ = J2 E j χ' μ' :=
  JprHom.sumK_congr E _ _ (fun t => by
    show E.mul (χ t) (μ ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t))) =
      E.mul (χ' t) (μ' ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg t)))
    rw [hχ, hμ]) (LL j)

/-- **pointwise equal characters have equal Jacobi products**. -/
theorem jpr_congr {S : Type} (E : CRing S) (j : Nat) (ψ₁ ψ₂ : Zp (j + 1 + 1) → S) (h : ∀ x, ψ₁ x = ψ₂ x) :
    ∀ k, jpr E j ψ₁ k = jpr E j ψ₂ k
  | 0 => rfl
  | k + 1 => by
    show E.mul (jpr E j ψ₁ k) (J2 E j (psp E j ψ₁ (k + 1)) ψ₁) = E.mul (jpr E j ψ₂ k) (J2 E j (psp E j ψ₂ (k + 1)) ψ₂)
    rw [jpr_congr E j ψ₁ ψ₂ h k,
      j2_congr E j (psp E j ψ₁ (k + 1)) (psp E j ψ₂ (k + 1)) ψ₁ ψ₂
        (fun x => by show pw E (ψ₁ x) (k + 1) = pw E (ψ₂ x) (k + 1); rw [h x]) h]

/-- the element A = ∏_(i ≤ k) J(χ^i, χ)·χ(ι(−1)) of R. -/
def AA {R : Type} (T : CRing R) (j : Nat) (χ : Zp (j + 1 + 1) → R) (k : Nat) : R :=
  T.mul (jpr T j χ k) (χ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))

/-- the image of A under a ring map is A of the image character. -/
theorem aa_map {R S : Type} (T : CRing R) (E : CRing S) (φ : R → S) (hadd : ∀ a b, φ (T.add a b) = E.add (φ a) (φ b))
    (hzero : φ T.zero = E.zero) (hmul : ∀ a b, φ (T.mul a b) = E.mul (φ a) (φ b)) (hone : φ T.one = E.one) (j : Nat)
    (χ : Zp (j + 1 + 1) → R) (ψ : Zp (j + 1 + 1) → S) (hχ : ∀ x, φ (χ x) = ψ x) (k : Nat) :
    φ (AA T j χ k) = AA E j ψ k := by
  show φ (T.mul (jpr T j χ k) (χ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) =
    E.mul (jpr E j ψ k) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))
  rw [JprHom.a_map T E φ j hadd hzero hmul hone χ k, jpr_congr E j _ ψ hχ k, hχ]

/-- **A is primary**: φ₁(A)·p = −1 in Q, i.e. A·p ≡ −1 (mod (1 − ζ)²), ℓ = 2r + 3. -/
theorem a_primary {R Q : Type} (T : CRing R) (K : CRing Q) (φ : R → Q) (hadd : ∀ a b, φ (T.add a b) = K.add (φ a) (φ b))
    (hzero : φ T.zero = K.zero) (hmul : ∀ a b, φ (T.mul a b) = K.mul (φ a) (φ b)) (hone : φ T.one = K.one)
    (π : Q) (hπ : K.mul π π = K.zero) (j : Nat) (g : Zp (j + 1 + 1)) (χ : Zp (j + 1 + 1) → R)
    (hχ : ∀ x, φ (χ x) = PrimJC.u K π (j + 1) g x)
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (l h : Nat) (hl : nsm K l K.one = K.zero)
    (hN : j + 1 + 1 = l * (2 * h)) (r : Nat) :
    K.mul (φ (AA T j χ (2 * r + 1))) (Pp K j) = K.neg K.one := by
  rw [aa_map T K φ hadd hzero hmul hone j χ _ hχ (2 * r + 1)]
  exact PrimA.gauss_prim K π hπ j g hpr hg l h hl hN r

/-- **Eisenstein's relation for A**: ψ(q^(−e)) = φ₂(A)^m. -/
theorem a_eis {R S : Type} (T : CRing R) (E : CRing S) (φ : R → S) (hadd : ∀ a b, φ (T.add a b) = E.add (φ a) (φ b))
    (hzero : φ T.zero = E.zero) (hmul : ∀ a b, φ (T.mul a b) = E.mul (φ a) (φ b)) (hone : φ T.one = E.one)
    (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (χ : Zp (j + 1 + 1) → R)
    (ψ : Zp (j + 1 + 1) → S) (hχ : ∀ x, φ (χ x) = ψ x)
    (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y)) (h1 : ψ (ring (j + 1 + 1)).one = E.one)
    (h0 : ψ (ring (j + 1 + 1)).zero = E.zero) (hs : sumK E ψ (LL j) = E.zero) (k : Nat)
    (hnt : ∀ i, i < k → sumK E (fun a => pw E (ψ a) (i + 2)) (LL j) = E.zero) (hinv : ∀ x, pw E (ψ x) (k + 1) = ψ (iv j x))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hE : nsm E (k' + 1 + 1) E.one = E.zero) (e : Nat)
    (hfix : ∀ a, pw E (ψ a) ((k' + 1 + 1) ^ e) = ψ a)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (m : Nat) (hqn : (k' + 1 + 1) ^ e = (k + 2) * m + 1)
    (ι : Zp (k' + 1) → S) (hιa : ∀ a b, ι ((ring (k' + 1)).add a b) = E.add (ι a) (ι b))
    (hιm : ∀ a b, ι ((ring (k' + 1)).mul a b) = E.mul (ι a) (ι b)) (hι1 : ι (ring (k' + 1)).one = E.one)
    (hι0 : ι (ring (k' + 1)).zero = E.zero)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (t : Nat) (ht : m = (k' + 1) * t) :
    ψ (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) =
      pw E (φ (AA T j χ k)) m := by
  rw [aa_map T E φ hadd hzero hmul hone j χ ψ hχ k]
  exact NthEis.nth_eis E j hpr ψ hm h1 h0 hs k hnt hinv k' hq hE e hfix hQ m hqn ι hιa hιm hι1 hι0 hp t ht

end EisBridge

#print axioms EisBridge.j2_congr
#print axioms EisBridge.jpr_congr
#print axioms EisBridge.aa_map
#print axioms EisBridge.a_primary
#print axioms EisBridge.a_eis

lang/CycPrim.lean

/-
  lang/CycPrim — T49 (general odd ℓ): **the primary element A in the integral group ring ℤ[C_ℓ]**.
  ℓ = m + 1 (odd). The character χ(x) = [dlog x mod ℓ] ∈ ℤ[C_ℓ] (χ(0) = 0) and the ring map
    φ₁ : ℤ[C_ℓ] → Q,   u ↦ Σ_a u(a) (1 + π)^a        (Q any ring with π² = 0, ℓ·1 = 0)
  satisfy φ₁∘χ = (1 + π)^dlog (the character of PrimA), so A = ∏_(i ≤ ℓ−2) J(χ^i, χ)·χ(−1) ∈ ℤ[C_ℓ] has φ₁(A)·p = −1:
  **A·p ≡ −1 modulo (ℓ, (σ − 1)²)**, the primary normalisation of the Gauss sum power for every odd ℓ.
    z_pow        (1 + π)^ℓ = 1
    phi_add, phi_mul, phi_zero, phi_one     φ₁ is a ring map
    phi_delta    u with integer coordinates δ_(b,a) maps to (1 + π)^a
    phi_chi      φ₁(χ x) = (1 + π)^(dlog x) (x ≠ 0), 0 (x = 0)
    cyc_primary  φ₁(A)·p = −1
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.EisBridge
import lang.GEval
import lang.IntCast
import lang.HasseDavenport
import lib.RingTac

namespace CycPrim

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob GEval
open FiniteRecChain (ite_pos' ite_neg')
open LibRing (castZ castN)

variable {Q : Type} (K : CRing Q) (π : Q) (hπ : K.mul π π = K.zero) (m : Nat)

/-- the coefficient map ℤ → Q followed by evaluation at ζ = 1 + π. -/
def phi (u : V Int (m + 1)) : Q := gev K m (K.add K.one π) (gmap LibInt.intR m (castZ K) u)

include hπ in
theorem z_pow (hl : nsm K (m + 1) K.one = K.zero) : pw K (K.add K.one π) (m + 1) = K.one := by
  rw [PrimJC.u_pow K π hπ (m + 1), hl]
  lib_ring

theorem phi_add (u v : V Int (m + 1)) :
    phi K π m ((gring LibInt.intR m).add u v) = K.add (phi K π m u) (phi K π m v) := by
  show gev K m (K.add K.one π) (gmap LibInt.intR m (castZ K) (gadd LibInt.intR m u v)) = _
  rw [gmap_add LibInt.intR m K (castZ K) (fun a b => IntCast.castZ_add K a b), gev_add]
  rfl

include hπ in
theorem phi_mul (hl : nsm K (m + 1) K.one = K.zero) (u v : V Int (m + 1)) :
    phi K π m ((gring LibInt.intR m).mul u v) = K.mul (phi K π m u) (phi K π m v) := by
  show gev K m (K.add K.one π) (gmap LibInt.intR m (castZ K) (gmul LibInt.intR m u v)) = _
  rw [gmap_mul LibInt.intR m K (castZ K) (fun a b => IntCast.castZ_add K a b) (fun a b => HasseDavenport.castZ_mul K a b) rfl,
    gev_mul K m _ (z_pow K π hπ m hl)]
  rfl

theorem phi_zero : phi K π m (gring LibInt.intR m).zero = K.zero := by
  have h := phi_add K π m (gring LibInt.intR m).zero (gring LibInt.intR m).zero
  rw [(gring LibInt.intR m).zero_add] at h
  calc phi K π m (gring LibInt.intR m).zero
      = K.add (K.add (phi K π m (gring LibInt.intR m).zero) (phi K π m (gring LibInt.intR m).zero))
          (K.neg (phi K π m (gring LibInt.intR m).zero)) := by lib_ring
    _ = K.add (phi K π m (gring LibInt.intR m).zero) (K.neg (phi K π m (gring LibInt.intR m).zero)) := by rw [← h]
    _ = K.zero := by lib_ring

/-- an integer vector with coordinates δ_(b,a) maps to (1 + π)^a. -/
theorem phi_delta (u : V Int (m + 1)) (a : Zp m) (hu : ∀ b, castZ K (gt LibInt.intR m u b) = delta K b a) :
    phi K π m u = pw K (K.add K.one π) a.val := by
  show sumK K (fun b => K.mul (gt K m (gmap LibInt.intR m (castZ K) u) b) (pw K (K.add K.one π) b.val)) (L m) = _
  rw [sumK_ext K _ (fun b => K.mul (delta K b a) (pw K (K.add K.one π) b.val)) (fun b => by
    unfold gmap; rw [gt_tb, hu]) (L m)]
  exact pick K _ (CMInert.complete_allZ m K) (fun b => pw K (K.add K.one π) b.val) a

theorem cast_delta (b a : Zp m) : castZ K (delta LibInt.intR b a) = delta K b a := by
  unfold delta
  by_cases h : b = a
  · rw [ite_pos' _ _ h, ite_pos' _ _ h]
    show castZ K 1 = K.one
    show K.add K.one K.zero = K.one
    lib_ring
  · rw [ite_neg' _ _ h, ite_neg' _ _ h]
    rfl

theorem phi_one : phi K π m (gring LibInt.intR m).one = K.one := by
  rw [phi_delta K π m _ (ring m).zero (fun b => by
    show castZ K (gt LibInt.intR m (tb m (fun x => delta LibInt.intR x (ring m).zero)) b) = _
    rw [gt_tb, cast_delta])]
  rfl

variable (j : Nat) (g : Zp (j + 1 + 1))

/-- **the character with values in ℤ[C_ℓ]**: χ(x) = [dlog x mod ℓ], χ(0) = 0. -/
def chi (x : Zp (j + 1 + 1)) : V Int (m + 1) :=
  if x = (ring (j + 1 + 1)).zero then (gring LibInt.intR m).zero else mono LibInt.intR m (mk m (DLogF.dlog (j + 1) g x)) 1

include hπ in
theorem phi_chi (hl : nsm K (m + 1) K.one = K.zero) (x : Zp (j + 1 + 1)) :
    phi K π m (chi m j g x) = PrimJC.u K π (j + 1) g x := by
  unfold chi PrimJC.u
  by_cases hx : x = (ring (j + 1 + 1)).zero
  · rw [ite_pos' _ _ hx, ite_pos' _ _ hx]
    exact phi_zero K π m
  · rw [ite_neg' _ _ hx, ite_neg' _ _ hx,
      phi_delta K π m _ (mk m (DLogF.dlog (j + 1) g x)) (fun b => by
        rw [gt_mono]
        show castZ K (Int.mul 1 (delta LibInt.intR b (mk m (DLogF.dlog (j + 1) g x)))) = _
        rw [show Int.mul 1 (delta LibInt.intR b (mk m (DLogF.dlog (j + 1) g x))) = 1 * delta LibInt.intR b (mk m (DLogF.dlog (j + 1) g x)) from rfl,
          HasseDavenport.castZ_mul, cast_delta]
        show K.mul (K.add K.one K.zero) (delta K b (mk m (DLogF.dlog (j + 1) g x))) = _
        lib_ring)]
    exact pw_md K m _ (z_pow K π hπ m hl) _

include hπ in
/-- **A ∈ ℤ[C_ℓ] is primary**: φ₁(A)·p = −1 for every odd k = 2r + 1 (in particular k = ℓ − 2), p − 1 = ℓ·2h. -/
theorem cyc_primary (r : Nat) (hl : nsm K (m + 1) K.one = K.zero)
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (h : Nat) (hN : j + 1 + 1 = (m + 1) * (2 * h)) :
    K.mul (phi K π m (EisBridge.AA (gring LibInt.intR m) j (chi m j g) (2 * r + 1))) (CharSumGen.Pp K j) = K.neg K.one :=
  EisBridge.a_primary (gring LibInt.intR m) K (phi K π m) (phi_add K π m) (phi_zero K π m) (phi_mul K π hπ m hl)
    (phi_one K π m) π hπ j g (chi m j g) (phi_chi K π hπ m j g hl) hpr hg (m + 1) h hl hN r

end CycPrim

#print axioms CycPrim.z_pow
#print axioms CycPrim.phi_add
#print axioms CycPrim.phi_mul
#print axioms CycPrim.phi_zero
#print axioms CycPrim.phi_delta
#print axioms CycPrim.cast_delta
#print axioms CycPrim.phi_one
#print axioms CycPrim.phi_chi
#print axioms CycPrim.cyc_primary

lang/CycChar.lean

/-
  lang/CycChar — T49 (general ℓ, the E-side character): for w in any ring E and a generator g of 𝔽_p^×,
    ψ_w(x) = w^(dlog x) (x ≠ 0),  ψ_w(0) = 0.
    zero_add', succ_add', split   Nat facts by structural recursion
    g_cancel     g^(b + c) = g^b ⇒ g^c = 1 (no zero divisors in 𝔽_p)
    w_of_g       w^(p−1) = 1 and g^a = g^b ⇒ w^a = w^b
    psi_mul, psi_one, psi_zero    ψ_w is multiplicative
    psi_pow      ψ_w(x)^(c+1) = ψ_(w^(c+1))(x)
    gs, gs_add, gs_block          Σ_(i<n) w^i, additivity, Σ_(i<ℓt) w^i = 0 when w^ℓ = 1, Σ_(i<ℓ) w^i = 0
    sum_pows     Σ_(k<n) ψ_w(g^k) = Σ_(k<n) w^k
    sum_psi      Σ_x ψ_w(x) = 0 when p − 1 = ℓt
  These are the hypotheses of the n-th power residue criterion (NthRecipPow) for ψ_u, u an ℓ-th root of unity.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.PrimJC
import lang.NthEis
import lang.StickHom
import lib.RingTac

namespace CycChar

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius
open FiniteRecChain (ite_pos' ite_neg')

theorem zero_add' : ∀ n : Nat, 0 + n = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (zero_add' n)

theorem succ_add' (b : Nat) : ∀ c : Nat, (b + 1) + c = (b + c) + 1
  | 0 => rfl
  | c + 1 => congrArg Nat.succ (succ_add' b c)

theorem split : ∀ a b : Nat, (∃ c, a = b + c) ∨ (∃ c, b = a + c)
  | 0, b => Or.inr ⟨b, (zero_add' b).symm⟩
  | a + 1, 0 => Or.inl ⟨a + 1, (zero_add' (a + 1)).symm⟩
  | a + 1, b + 1 =>
    match split a b with
    | Or.inl ⟨c, h⟩ => Or.inl ⟨c, (congrArg Nat.succ h).trans (succ_add' b c).symm⟩
    | Or.inr ⟨c, h⟩ => Or.inr ⟨c, (congrArg Nat.succ h).trans (succ_add' a c).symm⟩

variable (J : Nat) (g : Zp (J + 1))

/-- **cancellation**: g^(b + c) = g^b ⇒ g^c = 1 in 𝔽_p. -/
theorem g_cancel (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg0 : g ≠ (ring (J + 1)).zero) (b c : Nat)
    (h : pw (ring (J + 1)) g (b + c) = pw (ring (J + 1)) g b) : pw (ring (J + 1)) g c = (ring (J + 1)).one := by
  rw [HasseCM.pw_add'] at h
  have hz : (ring (J + 1)).mul (pw (ring (J + 1)) g b) ((ring (J + 1)).add (pw (ring (J + 1)) g c) ((ring (J + 1)).neg (ring (J + 1)).one))
      = (ring (J + 1)).zero := by
    calc (ring (J + 1)).mul (pw (ring (J + 1)) g b) ((ring (J + 1)).add (pw (ring (J + 1)) g c) ((ring (J + 1)).neg (ring (J + 1)).one))
        = (ring (J + 1)).add ((ring (J + 1)).mul (pw (ring (J + 1)) g b) (pw (ring (J + 1)) g c)) ((ring (J + 1)).neg (pw (ring (J + 1)) g b)) := by lib_ring
      _ = (ring (J + 1)).add (pw (ring (J + 1)) g b) ((ring (J + 1)).neg (pw (ring (J + 1)) g b)) := by rw [h]
      _ = (ring (J + 1)).zero := by lib_ring
  cases ZMod.nozd J hpr _ _ hz with
  | inl h0 => exact absurd h0 (DLog.pw_ne_zero J hpr g hg0 b)
  | inr h1 =>
    calc pw (ring (J + 1)) g c
        = (ring (J + 1)).add ((ring (J + 1)).add (pw (ring (J + 1)) g c) ((ring (J + 1)).neg (ring (J + 1)).one)) (ring (J + 1)).one := by lib_ring
      _ = (ring (J + 1)).add (ring (J + 1)).zero (ring (J + 1)).one := by rw [h1]
      _ = (ring (J + 1)).one := by lib_ring

variable {S : Type} (E : CRing S)

theorem w_shift (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
    (w : S) (hw : pw E w (J + 1) = E.one) (b c : Nat)
    (h : pw (ring (J + 1)) g (b + c) = pw (ring (J + 1)) g b) : pw E w (b + c) = pw E w b := by
  obtain ⟨s, hs⟩ := hg.2 c (g_cancel J g hpr (PrimJC.g_ne_zero J g hg) b c h)
  rw [HasseCM.pw_add', hs, ← TwoPower.pwpow, hw, NthEis.pw_one E s]
  lib_ring

/-- **w^(p−1) = 1 and g^a = g^b ⇒ w^a = w^b**. -/
theorem w_of_g (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
    (w : S) (hw : pw E w (J + 1) = E.one) (a b : Nat) (h : pw (ring (J + 1)) g a = pw (ring (J + 1)) g b) :
    pw E w a = pw E w b := by
  cases split a b with
  | inl hc =>
    obtain ⟨c, e⟩ := hc
    rw [e] at h ⊢
    exact w_shift J g E hpr hg w hw b c h
  | inr hc =>
    obtain ⟨c, e⟩ := hc
    rw [e] at h ⊢
    exact (w_shift J g E hpr hg w hw a c h.symm).symm

/-- **the character ψ_w(x) = w^(dlog x)**. -/
def psi (w : S) (x : Zp (J + 1)) : S := if x = (ring (J + 1)).zero then E.zero else pw E w (DLogF.dlog J g x)

theorem psi_zero (w : S) : psi J g E w (ring (J + 1)).zero = E.zero := by
  unfold psi; rw [ite_pos' _ _ rfl]

theorem psi_one (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1)) (w : S) :
    psi J g E w (ring (J + 1)).one = E.one := by
  have e : DLogF.dlog J g (ring (J + 1)).one = 0 := DLogF.dlog_pw J hpr g hg (PrimJC.g_ne_zero J g hg) 0 (Nat.zero_lt_succ J)
  unfold psi; rw [ite_neg' _ _ (PowerSum.one_ne_zero' J), e]; rfl

theorem psi_mul (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
    (w : S) (hw : pw E w (J + 1) = E.one) (x y : Zp (J + 1)) :
    psi J g E w ((ring (J + 1)).mul x y) = E.mul (psi J g E w x) (psi J g E w y) := by
  by_cases hx : x = (ring (J + 1)).zero
  · have e : (ring (J + 1)).mul x y = (ring (J + 1)).zero := by rw [hx]; lib_ring
    rw [e, hx, psi_zero]; lib_ring
  · by_cases hy : y = (ring (J + 1)).zero
    · have e : (ring (J + 1)).mul x y = (ring (J + 1)).zero := by rw [hy]; lib_ring
      rw [e, hy, psi_zero]; lib_ring
    · have hxy : (ring (J + 1)).mul x y ≠ (ring (J + 1)).zero := fun h0 => by
        cases ZMod.nozd J hpr _ _ h0 with
        | inl h => exact hx h
        | inr h => exact hy h
      unfold psi
      rw [ite_neg' _ _ hxy, ite_neg' _ _ hx, ite_neg' _ _ hy, ← HasseCM.pw_add']
      apply w_of_g J g E hpr hg w hw
      rw [HasseCM.pw_add', (DLogF.dlog_spec J hpr g hg _ hxy).1, (DLogF.dlog_spec J hpr g hg _ hx).1,
        (DLogF.dlog_spec J hpr g hg _ hy).1]

theorem psi_pow (w : S) (x : Zp (J + 1)) (c : Nat) : pw E (psi J g E w x) (c + 1) = psi J g E (pw E w (c + 1)) x := by
  unfold psi
  by_cases hx : x = (ring (J + 1)).zero
  · rw [ite_pos' _ _ hx, ite_pos' _ _ hx]
    exact GroupRingFrob.pw_zero_succ' E c
  · rw [ite_neg' _ _ hx, ite_neg' _ _ hx]
    exact StickHom.pw_swap E w (DLogF.dlog J g x) (c + 1)

/-- Σ_(i<n) w^i (newest term first, like sumK over pows). -/
def gs (w : S) : Nat → S
  | 0 => E.zero
  | n + 1 => E.add (pw E w n) (gs w n)

theorem gs_add (w : S) (a : Nat) : ∀ b, gs E w (a + b) = E.add (gs E w a) (E.mul (pw E w a) (gs E w b))
  | 0 => by show gs E w a = E.add (gs E w a) (E.mul (pw E w a) E.zero); lib_ring
  | b + 1 => by
    show E.add (pw E w (a + b)) (gs E w (a + b)) = E.add (gs E w a) (E.mul (pw E w a) (E.add (pw E w b) (gs E w b)))
    rw [gs_add w a b, HasseCM.pw_add']
    lib_ring

/-- **Σ_(i<ℓt) w^i = 0** when w^ℓ = 1 and Σ_(i<ℓ) w^i = 0. -/
theorem gs_block (w : S) (L : Nat) (hz : gs E w L = E.zero) : ∀ t, gs E w (L * t) = E.zero
  | 0 => rfl
  | t + 1 => by
    show gs E w (L * t + L) = E.zero
    rw [gs_add, gs_block w L hz t, hz]
    lib_ring

theorem sum_pows (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1)) (w : S) :
    ∀ k, k ≤ J + 1 → sumK E (psi J g E w) (DLog.pows J g k) = gs E w k
  | 0, _ => rfl
  | k + 1, hk => by
    have hne := DLog.pw_ne_zero J hpr g (PrimJC.g_ne_zero J g hg) k
    have hu : psi J g E w (pw (ring (J + 1)) g k) = pw E w k := by
      unfold psi
      rw [ite_neg' _ _ hne, DLogF.dlog_pw J hpr g hg (PrimJC.g_ne_zero J g hg) k hk]
    show E.add (psi J g E w (pw (ring (J + 1)) g k)) (sumK E (psi J g E w) (DLog.pows J g k)) = E.add (pw E w k) (gs E w k)
    rw [hu, sum_pows hpr hg w k (Nat.le_of_succ_le hk)]

/-- **Σ_x ψ_w(x) = 0** when p − 1 = ℓt and Σ_(i<ℓ) w^i = 0. -/
theorem sum_psi (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1)) (w : S)
    (L t : Nat) (hN : J + 1 = L * t) (hz : gs E w L = E.zero) :
    sumK E (psi J g E w) (CMInert.allZ (J + 1) (J + 1 + 1)) = E.zero := by
  rw [DLog.sum_split J E hpr g hg, sum_pows J g E hpr hg w (J + 1) (Nat.le_refl _), psi_zero, hN, gs_block E w L hz t]
  lib_ring

end CycChar

#print axioms CycChar.zero_add'
#print axioms CycChar.succ_add'
#print axioms CycChar.split
#print axioms CycChar.g_cancel
#print axioms CycChar.w_shift
#print axioms CycChar.w_of_g
#print axioms CycChar.psi_zero
#print axioms CycChar.psi_one
#print axioms CycChar.psi_mul
#print axioms CycChar.psi_pow
#print axioms CycChar.gs_add
#print axioms CycChar.gs_block
#print axioms CycChar.sum_pows
#print axioms CycChar.sum_psi

lang/CycEis.lean

/-
  lang/CycEis — T49 (general odd ℓ = k + 2): **Eisenstein's relation for the primary element A ∈ ℤ[C_ℓ]**.
  E any ring of characteristic q, u ∈ E with u^ℓ = 1 and Σ_(i<ℓ) (u^c)^i = 0 for 1 ≤ c ≤ ℓ − 1 (u a primitive ℓ-th
  root of unity in a field). With ψ_u = u^dlog (CycChar), φ_u : ℤ[C_ℓ] → E, v ↦ Σ_a v(a) u^a, and χ, A as in CycPrim:
    psi_pw       ψ(x^n) = ψ(x)^n
    psi_ell      ψ(x)^ℓ = 1 (x ≠ 0)
    psi_inv      ψ(x)^(ℓ−1) = ψ(x^(p−2))           (p − 1 = ℓ(t + 1))
    psi_fix      ψ(x)^(ℓM+1) = ψ(x)
    phz_add, phz_mul, phz_zero, phz_one, phz_delta, phz_chi    φ_u is a ring map and φ_u∘χ = ψ_u
    cyc_eis      ψ_u(q^(−e)) = φ_u(A)^M  when q^e = ℓM + 1, (q − 1) | M, p ≢ 0 (mod q)
    gs_mul, gs_zero, cyc_eis_field   Σ_(i<ℓ) w^i = 0 for w ≠ 1, w^ℓ = 1 without zero divisors; u primitive suffices
  Together with CycPrim (A·p ≡ −1 modulo (ℓ, (σ − 1)²)) this is Eisenstein's relation χ_p(q)^(−e) ≡ A^((q^e−1)/ℓ) (mod q)
  for one primary element A of ℤ[C_ℓ], every odd ℓ.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycChar
import lang.CycPrim

namespace CycEis

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob GEval CharSumGen
open FiniteRecChain (ite_pos' ite_neg')
open LibRing (castZ castN)

variable {S : Type} (E : CRing S) (j : Nat) (g : Zp (j + 1 + 1))

theorem psi_pw (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (w : S) (hw : pw E w (j + 1 + 1) = E.one) (x : Zp (j + 1 + 1)) :
    ∀ n, CycChar.psi (j + 1) g E w (pw (ring (j + 1 + 1)) x n) = pw E (CycChar.psi (j + 1) g E w x) n
  | 0 => CycChar.psi_one (j + 1) g E hpr hg w
  | n + 1 => by
    show CycChar.psi (j + 1) g E w ((ring (j + 1 + 1)).mul (pw (ring (j + 1 + 1)) x n) x) =
      E.mul (pw E (CycChar.psi (j + 1) g E w x) n) (CycChar.psi (j + 1) g E w x)
    rw [CycChar.psi_mul (j + 1) g E hpr hg w hw, psi_pw hpr hg w hw x n]

theorem psi_ell (k : Nat) (u : S) (hu : pw E u (k + 2) = E.one) (x : Zp (j + 1 + 1)) (hx : x ≠ (ring (j + 1 + 1)).zero) :
    pw E (CycChar.psi (j + 1) g E u x) (k + 2) = E.one := by
  unfold CycChar.psi
  rw [ite_neg' _ _ hx, StickHom.pw_swap E u (DLogF.dlog (j + 1) g x) (k + 2), hu]
  exact NthEis.pw_one E _

theorem pw_ell_mul (a : S) (k t : Nat) (h : pw E a (k + 2) = E.one) (r : Nat) : pw E a ((k + 2) * t + r) = pw E a r := by
  rw [HasseCM.pw_add', ← TwoPower.pwpow, h, NthEis.pw_one E t]
  lib_ring

/-- **ψ(x)^(ℓ−1) = ψ(x^(p−2))** = ψ(ι x), p − 1 = ℓ(t + 1). -/
theorem psi_inv (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (k t : Nat) (hN : j + 1 + 1 = (k + 2) * (t + 1)) (u : S) (hu : pw E u (k + 2) = E.one) (x : Zp (j + 1 + 1)) :
    pw E (CycChar.psi (j + 1) g E u x) (k + 1) = CycChar.psi (j + 1) g E u (iv j x) := by
  have hw : pw E u (j + 1 + 1) = E.one := by rw [hN, ← TwoPower.pwpow, hu]; exact NthEis.pw_one E _
  have hj : j + 1 = (k + 2) * t + (k + 1) := Nat.succ.inj hN
  show _ = CycChar.psi (j + 1) g E u (pw (ring (j + 1 + 1)) x (j + 1))
  rw [psi_pw E j g hpr hg u hw x (j + 1), show pw E (CycChar.psi (j + 1) g E u x) (j + 1) =
    pw E (CycChar.psi (j + 1) g E u x) ((k + 2) * t + (k + 1)) from congrArg (pw E (CycChar.psi (j + 1) g E u x)) hj]
  by_cases hx : x = (ring (j + 1 + 1)).zero
  · rw [hx, CycChar.psi_zero, GroupRingFrob.pw_zero_succ' E k, HasseCM.pw_add', GroupRingFrob.pw_zero_succ' E k]
    lib_ring
  · exact (pw_ell_mul E _ k t (psi_ell E j g k u hu x hx) (k + 1)).symm

/-- **ψ(x)^(ℓM+1) = ψ(x)**. -/
theorem psi_fix (k : Nat) (u : S) (hu : pw E u (k + 2) = E.one) (M : Nat) (x : Zp (j + 1 + 1)) :
    pw E (CycChar.psi (j + 1) g E u x) ((k + 2) * M + 1) = CycChar.psi (j + 1) g E u x := by
  by_cases hx : x = (ring (j + 1 + 1)).zero
  · rw [hx, CycChar.psi_zero]
    exact GroupRingFrob.pw_zero_succ' E _
  · rw [pw_ell_mul E _ k M (psi_ell E j g k u hu x hx) 1]
    show E.mul E.one _ = _
    lib_ring

/-! ## evaluation ℤ[C_ℓ] → E at u -/

/-- φ_u(v) = Σ_a v(a) u^a. -/
def phz (n : Nat) (z : S) (v : V Int (n + 1)) : S := gev E n z (gmap LibInt.intR n (castZ E) v)

theorem phz_add (n : Nat) (z : S) (v w : V Int (n + 1)) :
    phz E n z ((gring LibInt.intR n).add v w) = E.add (phz E n z v) (phz E n z w) := by
  show gev E n z (gmap LibInt.intR n (castZ E) (gadd LibInt.intR n v w)) = _
  rw [gmap_add LibInt.intR n E (castZ E) (fun a b => IntCast.castZ_add E a b), gev_add]
  rfl

theorem phz_mul (n : Nat) (z : S) (hz : pw E z (n + 1) = E.one) (v w : V Int (n + 1)) :
    phz E n z ((gring LibInt.intR n).mul v w) = E.mul (phz E n z v) (phz E n z w) := by
  show gev E n z (gmap LibInt.intR n (castZ E) (gmul LibInt.intR n v w)) = _
  rw [gmap_mul LibInt.intR n E (castZ E) (fun a b => IntCast.castZ_add E a b) (fun a b => HasseDavenport.castZ_mul E a b) rfl,
    gev_mul E n z hz]
  rfl

theorem phz_zero (n : Nat) (z : S) : phz E n z (gring LibInt.intR n).zero = E.zero := by
  have h := phz_add E n z (gring LibInt.intR n).zero (gring LibInt.intR n).zero
  rw [(gring LibInt.intR n).zero_add] at h
  calc phz E n z (gring LibInt.intR n).zero
      = E.add (E.add (phz E n z (gring LibInt.intR n).zero) (phz E n z (gring LibInt.intR n).zero))
          (E.neg (phz E n z (gring LibInt.intR n).zero)) := by lib_ring
    _ = E.add (phz E n z (gring LibInt.intR n).zero) (E.neg (phz E n z (gring LibInt.intR n).zero)) := by rw [← h]
    _ = E.zero := by lib_ring

theorem phz_delta (n : Nat) (z : S) (v : V Int (n + 1)) (a : Zp n) (hv : ∀ b, castZ E (gt LibInt.intR n v b) = delta E b a) :
    phz E n z v = pw E z a.val := by
  show sumK E (fun b => E.mul (gt E n (gmap LibInt.intR n (castZ E) v) b) (pw E z b.val)) (L n) = _
  rw [sumK_ext E _ (fun b => E.mul (delta E b a) (pw E z b.val)) (fun b => by
    unfold gmap; rw [gt_tb, hv]) (L n)]
  exact pick E _ (CMInert.complete_allZ n E) (fun b => pw E z b.val) a

theorem phz_one (n : Nat) (z : S) : phz E n z (gring LibInt.intR n).one = E.one := by
  rw [phz_delta E n z _ (ring n).zero (fun b => by
    show castZ E (gt LibInt.intR n (tb n (fun x => delta LibInt.intR x (ring n).zero)) b) = _
    rw [gt_tb, CycPrim.cast_delta])]
  rfl

theorem phz_chi (_hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (_hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (n : Nat) (z : S) (hz : pw E z (n + 1) = E.one) (x : Zp (j + 1 + 1)) :
    phz E n z (CycPrim.chi n j g x) = CycChar.psi (j + 1) g E z x := by
  unfold CycPrim.chi CycChar.psi
  by_cases hx : x = (ring (j + 1 + 1)).zero
  · rw [ite_pos' _ _ hx, ite_pos' _ _ hx]
    exact phz_zero E n z
  · rw [ite_neg' _ _ hx, ite_neg' _ _ hx,
      phz_delta E n z _ (mk n (DLogF.dlog (j + 1) g x)) (fun b => by
        rw [gt_mono]
        show castZ E (Int.mul 1 (delta LibInt.intR b (mk n (DLogF.dlog (j + 1) g x)))) = _
        rw [show Int.mul 1 (delta LibInt.intR b (mk n (DLogF.dlog (j + 1) g x))) = 1 * delta LibInt.intR b (mk n (DLogF.dlog (j + 1) g x)) from rfl,
          HasseDavenport.castZ_mul, CycPrim.cast_delta]
        show E.mul (E.add E.one E.zero) (delta E b (mk n (DLogF.dlog (j + 1) g x))) = _
        lib_ring)]
    exact pw_md E n _ hz _

/-- **Eisenstein's relation for A ∈ ℤ[C_ℓ]**, ℓ = k + 2: ψ_u(q^(−e)) = φ_u(A)^M. -/
theorem cyc_eis (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (k t : Nat) (hN : j + 1 + 1 = (k + 2) * (t + 1)) (u : S) (hu : pw E u (k + 2) = E.one)
    (hsum : ∀ c, c < k + 1 → CycChar.gs E (pw E u (c + 1)) (k + 2) = E.zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hE : nsm E (k' + 1 + 1) E.one = E.zero) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (k + 2) * M + 1)
    (ι : Zp (k' + 1) → S) (hιa : ∀ a b, ι ((ring (k' + 1)).add a b) = E.add (ι a) (ι b))
    (hιm : ∀ a b, ι ((ring (k' + 1)).mul a b) = E.mul (ι a) (ι b)) (hι1 : ι (ring (k' + 1)).one = E.one)
    (hι0 : ι (ring (k' + 1)).zero = E.zero)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s) :
    CycChar.psi (j + 1) g E u (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) =
      pw E (phz E (k + 1) u (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k)) M := by
  have hw : pw E u (j + 1 + 1) = E.one := by rw [hN, ← TwoPower.pwpow, hu]; exact NthEis.pw_one E _
  have hpsum : ∀ c, c < k + 1 → sumK E (CycChar.psi (j + 1) g E (pw E u (c + 1))) (LL j) = E.zero := fun c hc =>
    CycChar.sum_psi (j + 1) g E hpr hg _ (k + 2) (t + 1) hN (hsum c hc)
  exact EisBridge.a_eis (gring LibInt.intR (k + 1)) E (phz E (k + 1) u) (phz_add E (k + 1) u) (phz_zero E (k + 1) u)
    (phz_mul E (k + 1) u hu) (phz_one E (k + 1) u) j hpr (CycPrim.chi (k + 1) j g) (CycChar.psi (j + 1) g E u)
    (phz_chi E j g hpr hg (k + 1) u hu)
    (CycChar.psi_mul (j + 1) g E hpr hg u hw) (CycChar.psi_one (j + 1) g E hpr hg u) (CycChar.psi_zero (j + 1) g E u)
    (by
      have h0 := hpsum 0 (Nat.zero_lt_succ k)
      rw [show pw E u (0 + 1) = u by show E.mul E.one u = u; lib_ring] at h0
      exact h0)
    k
    (fun i hi => (sumK_ext E _ _ (fun a => CycChar.psi_pow (j + 1) g E u a (i + 1)) (LL j)).trans
      (hpsum (i + 1) (Nat.succ_lt_succ hi)))
    (psi_inv E j g hpr hg k t hN u hu)
    k' hq hE e (fun a => by rw [hqn]; exact psi_fix E j g k u hu M a) hQ M hqn ι hιa hιm hι1 hι0 hp s hs

/-! ## the geometric sums in a ring without zero divisors -/

theorem gs_mul (w : S) : ∀ n, E.mul (E.add w (E.neg E.one)) (CycChar.gs E w n) = E.add (pw E w n) (E.neg E.one)
  | 0 => by show E.mul (E.add w (E.neg E.one)) E.zero = E.add E.one (E.neg E.one); lib_ring
  | n + 1 => by
    show E.mul (E.add w (E.neg E.one)) (E.add (pw E w n) (CycChar.gs E w n)) = E.add (E.mul (pw E w n) w) (E.neg E.one)
    have ih := gs_mul w n
    calc E.mul (E.add w (E.neg E.one)) (E.add (pw E w n) (CycChar.gs E w n))
        = E.add (E.mul (E.add w (E.neg E.one)) (pw E w n)) (E.mul (E.add w (E.neg E.one)) (CycChar.gs E w n)) := by lib_ring
      _ = E.add (E.mul (E.add w (E.neg E.one)) (pw E w n)) (E.add (pw E w n) (E.neg E.one)) := by rw [ih]
      _ = E.add (E.mul (pw E w n) w) (E.neg E.one) := by lib_ring

/-- **Σ_(i<n) w^i = 0** when w^n = 1, w ≠ 1 and E has no zero divisors. -/
theorem gs_zero (hK : NoZD E) (w : S) (hw1 : w ≠ E.one) (n : Nat) (hwn : pw E w n = E.one) : CycChar.gs E w n = E.zero := by
  have h := gs_mul E w n
  rw [hwn] at h
  have h0 : E.mul (E.add w (E.neg E.one)) (CycChar.gs E w n) = E.zero := h.trans (by lib_ring)
  cases hK _ _ h0 with
  | inl h1 =>
    exact absurd (calc w = E.add (E.add w (E.neg E.one)) E.one := by lib_ring
      _ = E.add E.zero E.one := by rw [h1]
      _ = E.one := by lib_ring) hw1
  | inr h2 => exact h2

/-- **Eisenstein's relation for A ∈ ℤ[C_ℓ] in a ring without zero divisors**: u a primitive ℓ-th root of unity
    (u^ℓ = 1, u^c ≠ 1 for 1 ≤ c ≤ ℓ − 1). -/
theorem cyc_eis_field (hK : NoZD E) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (k t : Nat) (hN : j + 1 + 1 = (k + 2) * (t + 1)) (u : S) (hu : pw E u (k + 2) = E.one)
    (hprim : ∀ c, c < k + 1 → pw E u (c + 1) ≠ E.one)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hE : nsm E (k' + 1 + 1) E.one = E.zero) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (k + 2) * M + 1)
    (ι : Zp (k' + 1) → S) (hιa : ∀ a b, ι ((ring (k' + 1)).add a b) = E.add (ι a) (ι b))
    (hιm : ∀ a b, ι ((ring (k' + 1)).mul a b) = E.mul (ι a) (ι b)) (hι1 : ι (ring (k' + 1)).one = E.one)
    (hι0 : ι (ring (k' + 1)).zero = E.zero)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s) :
    CycChar.psi (j + 1) g E u (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) =
      pw E (phz E (k + 1) u (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k)) M :=
  cyc_eis E j g hpr hg k t hN u hu
    (fun c hc => gs_zero E hK _ (hprim c hc) (k + 2)
      (by rw [StickHom.pw_swap E u (c + 1) (k + 2), hu]; exact NthEis.pw_one E _))
    k' hq hE e hQ M hqn ι hιa hιm hι1 hι0 hp s hs

end CycEis

#print axioms CycEis.psi_pw
#print axioms CycEis.psi_ell
#print axioms CycEis.pw_ell_mul
#print axioms CycEis.psi_inv
#print axioms CycEis.psi_fix
#print axioms CycEis.phz_add
#print axioms CycEis.phz_mul
#print axioms CycEis.phz_zero
#print axioms CycEis.phz_delta
#print axioms CycEis.phz_one
#print axioms CycEis.phz_chi
#print axioms CycEis.cyc_eis
#print axioms CycEis.gs_mul
#print axioms CycEis.gs_zero
#print axioms CycEis.cyc_eis_field

lang/CycRing.lean

/-
  lang/CycRing — T49 (general ℓ = m + 1): **the cyclotomic quotient R[C_ℓ]/(N), N = Σ_a σ^a**, as a ring of normal forms.
  For any commutative ring R (e.g. 𝔽_q) this is R[x]/(Φ_ℓ) when ℓ is prime; no field extension is needed.
    NN, tot, top, red      N, Σ_a v(a), the top index ℓ − 1, the normal form red u = u − u(top)·N
    gt_red, red_top        coordinates of red u; red u has top coordinate 0
    red_shift              vectors differing by a constant have the same normal form
    gt_mul_red             (red u)·v = u·v − u(top)·(Σv)·N coordinatewise (N·v = (Σv)·N)
    red_mul_left, red_mul_right, red_add, red_fix
    cyc                    the ring: {u // u(top) = 0}, u ⊕ v = u + v, u ⊗ v = red(u v), 1 = red(δ₀)
    qmap, qmap_add, qmap_mul, qmap_one, qmap_zero    u ↦ red u is a ring map R[C_ℓ] → cyc
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GroupRing
import lib.RingTac

namespace CycRing

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

variable {R : Type} (K : CRing R) (m : Nat)

/-- N = Σ_a σ^a. -/
def NN : V R (m + 1) := tb m (fun _ => K.one)
/-- Σ_a v(a). -/
def tot (v : V R (m + 1)) : R := sumK K (gt K m v) (L m)
/-- the top index ℓ − 1. -/
def top : Zp m := mk m m
/-- **normal form**: red u = u − u(top)·N. -/
def red (u : V R (m + 1)) : V R (m + 1) := tb m (fun a => K.add (gt K m u a) (K.neg (gt K m u (top m))))

theorem gt_red (u : V R (m + 1)) (a : Zp m) : gt K m (red K m u) a = K.add (gt K m u a) (K.neg (gt K m u (top m))) :=
  gt_tb K m _ a

theorem red_top (u : V R (m + 1)) : gt K m (red K m u) (top m) = K.zero := by
  rw [gt_red]; lib_ring

/-- vectors differing by a constant have the same normal form. -/
theorem red_shift (w w' : V R (m + 1)) (d : R) (h : ∀ a, gt K m w a = K.add (gt K m w' a) d) : red K m w = red K m w' :=
  ext K m _ _ (fun a => by rw [gt_red, gt_red, h a, h (top m)]; lib_ring)

theorem red_fix (u : V R (m + 1)) (hu : gt K m u (top m) = K.zero) : red K m u = u :=
  ext K m _ _ (fun a => by rw [gt_red, hu]; lib_ring)

theorem red_red (u : V R (m + 1)) : red K m (red K m u) = red K m u :=
  red_fix K m _ (red_top K m u)

/-- **(red u)·v = u·v − u(top)·(Σ v)** coordinatewise. -/
theorem gt_mul_red (u v : V R (m + 1)) (c : Zp m) :
    gt K m (gmul K m (red K m u) v) c = K.add (gt K m (gmul K m u v) c) (K.mul (K.neg (gt K m u (top m))) (tot K m v)) := by
  rw [gt_mul, gt_mul]
  have e1 : sumK K (fun a => K.mul (gt K m (red K m u) a) (gt K m v ((ring m).add c ((ring m).neg a)))) (L m) =
      sumK K (fun a => K.add (K.mul (gt K m u a) (gt K m v ((ring m).add c ((ring m).neg a))))
        (K.mul (K.neg (gt K m u (top m))) (gt K m v ((ring m).add c ((ring m).neg a))))) (L m) :=
    sumK_ext K _ _ (fun a => by rw [gt_red]; lib_ring) (L m)
  have e2 : sumK K (fun a => K.mul (K.neg (gt K m u (top m))) (gt K m v ((ring m).add c ((ring m).neg a)))) (L m) =
      K.mul (K.neg (gt K m u (top m))) (tot K m v) :=
    (mul_sumK K (K.neg (gt K m u (top m))) (fun a => gt K m v ((ring m).add c ((ring m).neg a))) (L m)).symm.trans
      (congrArg (K.mul (K.neg (gt K m u (top m)))) (flip K m (gt K m v) c))
  rw [e1, sumK_add, e2]

theorem red_mul_left (u v : V R (m + 1)) : red K m (gmul K m (red K m u) v) = red K m (gmul K m u v) :=
  red_shift K m _ _ _ (gt_mul_red K m u v)

theorem red_mul_right (u v : V R (m + 1)) : red K m (gmul K m u (red K m v)) = red K m (gmul K m u v) := by
  rw [gm_comm K m u (red K m v), red_mul_left, gm_comm]

theorem red_add (u v : V R (m + 1)) : red K m (gadd K m u v) = gadd K m (red K m u) (red K m v) :=
  ext K m _ _ (fun a => by
    unfold gadd
    rw [gt_red, gt_tb, gt_tb, gt_tb, gt_red, gt_red]
    lib_ring)

theorem top_add (u v : V R (m + 1)) (hu : gt K m u (top m) = K.zero) (hv : gt K m v (top m) = K.zero) :
    gt K m (gadd K m u v) (top m) = K.zero := by
  unfold gadd; rw [gt_tb, hu, hv]; lib_ring

theorem top_neg (u : V R (m + 1)) (hu : gt K m u (top m) = K.zero) : gt K m (gneg K m u) (top m) = K.zero := by
  unfold gneg; rw [gt_tb, hu]; lib_ring

theorem top_zero : gt K m (gzero K m) (top m) = K.zero := by
  unfold gzero; rw [gt_tb]

/-- the carrier: group-ring elements in normal form. -/
abbrev C : Type := {u : V R (m + 1) // gt K m u (top m) = K.zero}

def cadd (x y : C K m) : C K m := ⟨gadd K m x.val y.val, top_add K m _ _ x.property y.property⟩
def cneg (x : C K m) : C K m := ⟨gneg K m x.val, top_neg K m _ x.property⟩
def czero : C K m := ⟨gzero K m, top_zero K m⟩
def cone : C K m := ⟨red K m (gone K m), red_top K m _⟩
def cmul (x y : C K m) : C K m := ⟨red K m (gmul K m x.val y.val), red_top K m _⟩

theorem cext (x y : C K m) (h : x.val = y.val) : x = y := Subtype.ext h

/-- **the ring R[C_ℓ]/(N)**. -/
def cyc : CRing (C K m) where
  add := cadd K m
  mul := cmul K m
  neg := cneg K m
  zero := czero K m
  one := cone K m
  add_assoc := fun a b c => cext K m _ _ ((gring K m).add_assoc a.val b.val c.val)
  add_comm := fun a b => cext K m _ _ ((gring K m).add_comm a.val b.val)
  zero_add := fun a => cext K m _ _ ((gring K m).zero_add a.val)
  add_neg := fun a => cext K m _ _ ((gring K m).add_neg a.val)
  mul_assoc := fun a b c => cext K m _ _ (by
    show red K m (gmul K m (red K m (gmul K m a.val b.val)) c.val) = red K m (gmul K m a.val (red K m (gmul K m b.val c.val)))
    rw [red_mul_left, red_mul_right, gm_assoc])
  mul_comm := fun a b => cext K m _ _ (by
    show red K m (gmul K m a.val b.val) = red K m (gmul K m b.val a.val)
    rw [gm_comm])
  one_mul := fun a => cext K m _ _ (by
    show red K m (gmul K m (red K m (gone K m)) a.val) = a.val
    rw [red_mul_left, gm_one, red_fix K m _ a.property])
  distrib := fun a b c => cext K m _ _ (by
    show red K m (gmul K m a.val (gadd K m b.val c.val)) = gadd K m (red K m (gmul K m a.val b.val)) (red K m (gmul K m a.val c.val))
    rw [gm_dist, red_add])

/-- the quotient map R[C_ℓ] → R[C_ℓ]/(N). -/
def qmap (u : V R (m + 1)) : C K m := ⟨red K m u, red_top K m u⟩

theorem qmap_add (u v : V R (m + 1)) : qmap K m (gadd K m u v) = (cyc K m).add (qmap K m u) (qmap K m v) :=
  cext K m _ _ (red_add K m u v)

theorem qmap_mul (u v : V R (m + 1)) : qmap K m (gmul K m u v) = (cyc K m).mul (qmap K m u) (qmap K m v) :=
  cext K m _ _ (by
    show red K m (gmul K m u v) = red K m (gmul K m (red K m u) (red K m v))
    rw [red_mul_left, red_mul_right])

theorem qmap_one : qmap K m (gone K m) = (cyc K m).one := rfl

theorem qmap_zero : qmap K m (gzero K m) = (cyc K m).zero :=
  cext K m _ _ (red_fix K m _ (top_zero K m))

/-- **N maps to 0**. -/
theorem qmap_NN : qmap K m (NN K m) = (cyc K m).zero :=
  cext K m _ _ (ext K m _ _ (fun a => by
    show gt K m (red K m (NN K m)) a = gt K m (gzero K m) a
    unfold gzero NN
    rw [gt_red, gt_tb, gt_tb, gt_tb]
    lib_ring))

end CycRing

#print axioms CycRing.gt_red
#print axioms CycRing.red_top
#print axioms CycRing.red_shift
#print axioms CycRing.red_fix
#print axioms CycRing.red_red
#print axioms CycRing.gt_mul_red
#print axioms CycRing.red_mul_left
#print axioms CycRing.red_mul_right
#print axioms CycRing.red_add
#print axioms CycRing.top_add
#print axioms CycRing.top_neg
#print axioms CycRing.top_zero
#print axioms CycRing.cext
#print axioms CycRing.cyc
#print axioms CycRing.qmap_add
#print axioms CycRing.qmap_mul
#print axioms CycRing.qmap_one
#print axioms CycRing.qmap_zero
#print axioms CycRing.qmap_NN

lang/CycUnit.lean

/-
  lang/CycUnit — T49 (general prime ℓ = m + 1): **the ℓ-th root of unity u = [σ] in R[C_ℓ]/(N)**.
    qmap_pw      the quotient map commutes with powers
    u_pow        u^k = [σ^k]
    u_ell        u^ℓ = 1
    sum_allZ     Σ_(x ∈ {0..n−1}) F(x) = Σ_(i<n) F(i)  (the list allZ)
    sum_scaled   Σ_x δ(b, x·c) = 1 for c ≠ 0 in 𝔽_ℓ (x ↦ xc is a permutation)
    gs_u         Σ_(i<ℓ) (u^c)^i = 0 for ℓ ∤ c (the geometric sum is the class of N)
    cyc_char     nsm q 1 = 0 in R ⇒ nsm q 1 = 0 in R[C_ℓ]/(N)
    iota, iota_add, iota_mul, iota_one, iota_zero      R → R[C_ℓ]/(N), a ↦ [a·σ⁰] is a ring map
  No field extension is used: R[C_ℓ]/(N) = R[x]/(Φ_ℓ) always contains a root u of Φ_ℓ.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycRing
import lang.CycChar
import lang.GroupRingFrob
import lang.Teich
import lang.TwoPower
import lang.NthEis
import lib.RingTac

namespace CycUnit

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CycRing

variable {R : Type} (K : CRing R) (m : Nat)

theorem qmap_pw (x : V R (m + 1)) : ∀ k, pw (cyc K m) (qmap K m x) k = qmap K m (pw (gring K m) x k)
  | 0 => (qmap_one K m).symm
  | k + 1 => by
    show (cyc K m).mul (pw (cyc K m) (qmap K m x) k) (qmap K m x) = qmap K m (gmul K m (pw (gring K m) x k) x)
    rw [qmap_pw x k, qmap_mul]

/-- σ = the generator of C_ℓ. -/
def sig : V R (m + 1) := mono K m (ring m).one K.one
/-- u = [σ]. -/
def u : C K m := qmap K m (sig K m)

theorem u_pow (k : Nat) : pw (cyc K m) (u K m) k = qmap K m (mono K m (nsm (ring m) k (ring m).one) K.one) := by
  unfold u sig
  rw [qmap_pw, mono_pow, NthEis.pw_one K k]

theorem gone_mono : gone K m = mono K m (ring m).zero K.one :=
  (mono_pow K m (ring m).one K.one 0)

/-- **u^ℓ = 1**. -/
theorem u_ell : pw (cyc K m) (u K m) (m + 1) = (cyc K m).one := by
  rw [u_pow, ZMod.char_p m, ← gone_mono]
  rfl

/-- 0, 1, …, n − 1. -/
def rng : Nat → List Nat
  | 0 => []
  | n + 1 => rng n ++ [n]

/-- the list allZ enumerates 0, …, n − 1. -/
theorem sum_allZ {S : Type} (E : CRing S) (F : Zp m → S) : ∀ n,
    sumK E F (CMInert.allZ m n) = sumK E (fun i => F (mk m i)) (rng n)
  | 0 => rfl
  | n + 1 => by
    show sumK E F (CMInert.allZ m n ++ [mk m n]) = sumK E (fun i => F (mk m i)) (rng n ++ [n])
    rw [sumK_append, sumK_append, sum_allZ E F n]
    rfl

/-- **x ↦ x·c permutes 𝔽_ℓ**: Σ_x δ(b, x·c) = 1 for c ≠ 0. -/
theorem sum_scaled (n : Nat) (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) (c : Zp (n + 1))
    (hc : c ≠ (ring (n + 1)).zero) (b : Zp (n + 1)) :
    sumK K (fun x => delta K b ((ring (n + 1)).mul x c)) (L (n + 1)) = K.one := by
  have hi := ZMod.inv_exists n hpr c hc
  have e := reindex K (L (n + 1)) (CMInert.complete_allZ (n + 1) K)
    (fun x => (ring (n + 1)).mul x c) (fun y => (ring (n + 1)).mul y (pw (ring (n + 1)) c n))
    (fun x => by
      show (ring (n + 1)).mul ((ring (n + 1)).mul x c) (pw (ring (n + 1)) c n) = x
      calc (ring (n + 1)).mul ((ring (n + 1)).mul x c) (pw (ring (n + 1)) c n)
          = (ring (n + 1)).mul x ((ring (n + 1)).mul (pw (ring (n + 1)) c n) c) := by lib_ring
        _ = (ring (n + 1)).mul x (ring (n + 1)).one := by rw [hi]
        _ = x := by lib_ring)
    (fun y => by
      show (ring (n + 1)).mul ((ring (n + 1)).mul y (pw (ring (n + 1)) c n)) c = y
      calc (ring (n + 1)).mul ((ring (n + 1)).mul y (pw (ring (n + 1)) c n)) c
          = (ring (n + 1)).mul y ((ring (n + 1)).mul (pw (ring (n + 1)) c n) c) := by lib_ring
        _ = (ring (n + 1)).mul y (ring (n + 1)).one := by rw [hi]
        _ = y := by lib_ring)
    (fun y => delta K b y)
  exact e.trans ((sumK_ext K _ _ (fun y => delta_symm K b y) (L (n + 1))).trans (CMInert.complete_allZ (n + 1) K b))

theorem nsm_mk (k : Nat) : nsm (ring m) k (ring m).one = mk m k := Subtype.ext (nsm_one_val m k)

/-- the geometric sum in the group ring: Σ_(i<n) σ^(ci). -/
def gG (c : Nat) : Nat → V R (m + 1)
  | 0 => gzero K m
  | n + 1 => gadd K m (mono K m (nsm (ring m) (c * n) (ring m).one) K.one) (gG c n)

theorem gs_qmap (c : Nat) : ∀ n, CycChar.gs (cyc K m) (pw (cyc K m) (u K m) c) n = qmap K m (gG K m c n)
  | 0 => (qmap_zero K m).symm
  | n + 1 => by
    show (cyc K m).add (pw (cyc K m) (pw (cyc K m) (u K m) c) n) (CycChar.gs (cyc K m) (pw (cyc K m) (u K m) c) n) =
      qmap K m (gadd K m (mono K m (nsm (ring m) (c * n) (ring m).one) K.one) (gG K m c n))
    rw [gs_qmap c n, qmap_add, TwoPower.pwpow, u_pow]

theorem gt_gG (c : Nat) (b : Zp m) : ∀ n, gt K m (gG K m c n) b =
    sumK K (fun i => delta K b ((ring m).mul (mk m i) (nsm (ring m) c (ring m).one))) (rng n)
  | 0 => by
    show gt K m (gzero K m) b = K.zero
    unfold gzero; rw [gt_tb]
  | n + 1 => by
    show gt K m (gadd K m (mono K m (nsm (ring m) (c * n) (ring m).one) K.one) (gG K m c n)) b =
      sumK K (fun i => delta K b ((ring m).mul (mk m i) (nsm (ring m) c (ring m).one))) (rng n ++ [n])
    unfold gadd
    rw [gt_tb, gt_mono, gt_gG c b n, sumK_append]
    have e : nsm (ring m) (c * n) (ring m).one = (ring m).mul (mk m n) (nsm (ring m) c (ring m).one) := by
      rw [nsm_mul_nat, Teich.nsm_one_mul (ring m) n (nsm (ring m) c (ring m).one), nsm_mk m n]
    rw [e]
    show K.add (K.mul K.one (delta K b ((ring m).mul (mk m n) (nsm (ring m) c (ring m).one))))
        (sumK K (fun i => delta K b ((ring m).mul (mk m i) (nsm (ring m) c (ring m).one))) (rng n)) =
      K.add (sumK K (fun i => delta K b ((ring m).mul (mk m i) (nsm (ring m) c (ring m).one))) (rng n))
        (K.add (delta K b ((ring m).mul (mk m n) (nsm (ring m) c (ring m).one))) K.zero)
    lib_ring

/-- **Σ_(i<ℓ) (u^c)^i = 0** for ℓ ∤ c, ℓ = n + 2 prime. -/
theorem gs_u (n : Nat) (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) (c : Nat)
    (hc : nsm (ring (n + 1)) c (ring (n + 1)).one ≠ (ring (n + 1)).zero) :
    CycChar.gs (cyc K (n + 1)) (pw (cyc K (n + 1)) (u K (n + 1)) c) (n + 1 + 1) = (cyc K (n + 1)).zero := by
  rw [gs_qmap]
  have hN : gG K (n + 1) c (n + 1 + 1) = NN K (n + 1) := ext K (n + 1) _ _ (fun b => by
    rw [gt_gG]
    unfold NN
    rw [gt_tb]
    exact (sum_allZ (n + 1) K (fun x => delta K b ((ring (n + 1)).mul x (nsm (ring (n + 1)) c (ring (n + 1)).one))) (n + 1 + 1)).symm.trans
      (sum_scaled K n hpr _ hc b))
  rw [hN, qmap_NN]

/-- characteristic q passes to R[C_ℓ]/(N). -/
theorem cyc_char (q : Nat) (hq : nsm K q K.one = K.zero) : nsm (cyc K m) q (cyc K m).one = (cyc K m).zero := by
  have h : ∀ k, nsm (cyc K m) k (cyc K m).one = qmap K m (nsm (gring K m) k (gring K m).one) := fun k => by
    induction k with
    | zero => exact (qmap_zero K m).symm
    | succ k ih =>
      show (cyc K m).add (nsm (cyc K m) k (cyc K m).one) (cyc K m).one = qmap K m (gadd K m (nsm (gring K m) k (gring K m).one) (gone K m))
      rw [ih, qmap_add]
      rfl
  rw [h, char_G K m q hq]
  exact qmap_zero K m

/-- the scalars: a ↦ [a σ⁰]. -/
def iota (a : R) : C K m := qmap K m (mono K m (ring m).zero a)

theorem iota_add (a b : R) : iota K m (K.add a b) = (cyc K m).add (iota K m a) (iota K m b) := by
  unfold iota
  rw [← qmap_add]
  exact congrArg (qmap K m) (ext K m _ _ (fun c => by unfold gadd; rw [gt_tb, gt_mono, gt_mono, gt_mono]; lib_ring))

theorem iota_mul (a b : R) : iota K m (K.mul a b) = (cyc K m).mul (iota K m a) (iota K m b) := by
  unfold iota
  rw [← qmap_mul, mono_mul, show (ring m).add (ring m).zero (ring m).zero = (ring m).zero by lib_ring]

theorem iota_one : iota K m K.one = (cyc K m).one := by
  unfold iota
  rw [← gone_mono]
  rfl

theorem iota_zero : iota K m K.zero = (cyc K m).zero := by
  unfold iota
  rw [← qmap_zero K m]
  exact congrArg (qmap K m) (ext K m _ _ (fun c => by unfold gzero; rw [gt_tb, gt_mono]; lib_ring))

end CycUnit

#print axioms CycUnit.qmap_pw
#print axioms CycUnit.u_pow
#print axioms CycUnit.gone_mono
#print axioms CycUnit.u_ell
#print axioms CycUnit.sum_allZ
#print axioms CycUnit.sum_scaled
#print axioms CycUnit.nsm_mk
#print axioms CycUnit.gs_qmap
#print axioms CycUnit.gt_gG
#print axioms CycUnit.gs_u
#print axioms CycUnit.cyc_char
#print axioms CycUnit.iota_add
#print axioms CycUnit.iota_mul
#print axioms CycUnit.iota_one
#print axioms CycUnit.iota_zero

lang/CycFq.lean

/-
  lang/CycFq — T49 (every prime ℓ = k + 2, every prime q ≠ ℓ, p): **Eisenstein's relation for the primary element
  A ∈ ℤ[C_ℓ] over E = 𝔽_q[C_ℓ]/(N) = 𝔽_q[x]/(Φ_ℓ)**, with no field extension and no hypothesis on u:
    small_ne     c + 1 < ℓ ⇒ (c + 1)·1 ≠ 0 in 𝔽_ℓ
    cyc_eis_fq   ψ_u(q^(−e)) = φ_u(A)^M in 𝔽_q[C_ℓ]/(N), u = [σ], when q^e = ℓM + 1, (q − 1) | M, p ≢ 0 (mod q)
  where A = ∏_(i ≤ ℓ−2) J(χ^i, χ)·χ(−1) ∈ ℤ[C_ℓ] is primary by CycPrim (A·p ≡ −1 modulo (ℓ, (σ − 1)²)).
  This is χ_p(q)^(−e) ≡ A^((q^e−1)/ℓ) (mod q) — Eisenstein's relation for every odd prime ℓ (ℓ = 5: Quint19).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.CycEis
import lang.CycUnit

namespace CycFq

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

/-- (c + 1)·1 ≠ 0 in 𝔽_ℓ for c + 1 < ℓ. -/
theorem small_ne (k c : Nat) (hc : c + 1 < k + 1 + 1) :
    nsm (ring (k + 1)) (c + 1) (ring (k + 1)).one ≠ (ring (k + 1)).zero := fun h => by
  have e : c + 1 = 0 :=
    (md_small (k + 1) (c + 1) hc).symm.trans ((nsm_one_val (k + 1) (c + 1)).symm.trans (congrArg Subtype.val h))
  exact Nat.noConfusion e

/-- **Eisenstein's relation over 𝔽_q[C_ℓ]/(N)**. -/
theorem cyc_eis_fq (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (k t : Nat) (hℓ : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1) (hN : j + 1 + 1 = (k + 2) * (t + 1))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (k + 2) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s) :
    CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1))
        (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) =
      pw (CycRing.cyc (ring (k' + 1)) (k + 1))
        (CycEis.phz (CycRing.cyc (ring (k' + 1)) (k + 1)) (k + 1) (CycUnit.u (ring (k' + 1)) (k + 1))
          (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k)) M :=
  CycEis.cyc_eis (CycRing.cyc (ring (k' + 1)) (k + 1)) j g hpr hg k t hN (CycUnit.u (ring (k' + 1)) (k + 1))
    (CycUnit.u_ell (ring (k' + 1)) (k + 1))
    (fun c hc => CycUnit.gs_u (ring (k' + 1)) k hℓ (c + 1) (small_ne k c (Nat.succ_lt_succ hc)))
    k' hq (CycUnit.cyc_char (ring (k' + 1)) (k + 1) (k' + 1 + 1) (ZMod.char_p (k' + 1))) e hQ M hqn
    (CycUnit.iota (ring (k' + 1)) (k + 1)) (CycUnit.iota_add (ring (k' + 1)) (k + 1)) (CycUnit.iota_mul (ring (k' + 1)) (k + 1))
    (CycUnit.iota_one (ring (k' + 1)) (k + 1)) (CycUnit.iota_zero (ring (k' + 1)) (k + 1)) hp s hs

end CycFq

#print axioms CycFq.small_ne
#print axioms CycFq.cyc_eis_fq

lang/CycSym.lean

/-
  lang/CycSym — T49 (every prime ℓ = k + 2, every prime q ≠ ℓ, p, any f with q^f ≡ 1 mod ℓ): **the n-th power residue
  criterion for A ∈ ℤ[C_ℓ] over E = 𝔽_q[C_ℓ]/(N)**, without removing p:
    cyc_recip_fq   ψ_u(q^(−f)) = (φ_u(A)·p)^M,   q^f = ℓM + 1.
  Composing with any ring map ρ : E → F (F the residue field of a prime 𝔮 | q, |F| = q^f) this is the classical first
  step of Eisenstein reciprocity, χ_p(q)^(−f) = (A/𝔮)_ℓ (p/𝔮)_ℓ, for the primary element A of CycPrim.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.CycFq

namespace CycSym

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

theorem cyc_recip_fq (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (k t : Nat) (hℓ : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1) (hN : j + 1 + 1 = (k + 2) * (t + 1))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (f : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ f) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ f = (k + 2) * M + 1) :
    CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1))
        (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ f) (ring (j + 1 + 1)).one) (j + 1)) =
      pw (CycRing.cyc (ring (k' + 1)) (k + 1))
        ((CycRing.cyc (ring (k' + 1)) (k + 1)).mul
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (k + 1)) (k + 1) (CycUnit.u (ring (k' + 1)) (k + 1))
            (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k))
          (Pp (CycRing.cyc (ring (k' + 1)) (k + 1)) j)) M := by
  have hu := CycUnit.u_ell (ring (k' + 1)) (k + 1)
  have hw : pw (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1)) (j + 1 + 1) =
      (CycRing.cyc (ring (k' + 1)) (k + 1)).one := by
    rw [hN, ← TwoPower.pwpow]
    rw [show pw (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1)) (k + 2) =
      (CycRing.cyc (ring (k' + 1)) (k + 1)).one from hu]
    exact NthEis.pw_one _ _
  have hsum : ∀ c, c < k + 1 → CycChar.gs (CycRing.cyc (ring (k' + 1)) (k + 1))
      (pw (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1)) (c + 1)) (k + 2) =
      (CycRing.cyc (ring (k' + 1)) (k + 1)).zero :=
    fun c hc => CycUnit.gs_u (ring (k' + 1)) k hℓ (c + 1) (CycFq.small_ne k c (Nat.succ_lt_succ hc))
  have hpsum : ∀ c, c < k + 1 → sumK (CycRing.cyc (ring (k' + 1)) (k + 1))
      (CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1))
        (pw (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1)) (c + 1))) (LL j) =
      (CycRing.cyc (ring (k' + 1)) (k + 1)).zero := fun c hc =>
    CycChar.sum_psi (j + 1) g _ hpr hg _ (k + 2) (t + 1) hN (hsum c hc)
  rw [EisBridge.aa_map (gring LibInt.intR (k + 1)) (CycRing.cyc (ring (k' + 1)) (k + 1))
    (CycEis.phz (CycRing.cyc (ring (k' + 1)) (k + 1)) (k + 1) (CycUnit.u (ring (k' + 1)) (k + 1)))
    (CycEis.phz_add _ (k + 1) _) (CycEis.phz_zero _ (k + 1) _) (CycEis.phz_mul _ (k + 1) _ hu) (CycEis.phz_one _ (k + 1) _) j
    (CycPrim.chi (k + 1) j g) _ (CycEis.phz_chi _ j g hpr hg (k + 1) _ hu) k]
  exact CharSumGen.nth_recip_pow (CycRing.cyc (ring (k' + 1)) (k + 1)) j hpr _
    (CycChar.psi_mul (j + 1) g _ hpr hg _ hw) (CycChar.psi_one (j + 1) g _ hpr hg _) (CycChar.psi_zero (j + 1) g _ _)
    (by
      have h0 := hpsum 0 (Nat.zero_lt_succ k)
      rw [show pw (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1)) (0 + 1) = CycUnit.u (ring (k' + 1)) (k + 1) by
        show (CycRing.cyc (ring (k' + 1)) (k + 1)).mul (CycRing.cyc (ring (k' + 1)) (k + 1)).one _ = _; lib_ring] at h0
      exact h0)
    k
    (fun i hi => (sumK_ext _ _ _ (fun a => CycChar.psi_pow (j + 1) g _ _ a (i + 1)) (LL j)).trans
      (hpsum (i + 1) (Nat.succ_lt_succ hi)))
    (CycEis.psi_inv _ j g hpr hg k t hN _ hu)
    k' hq (CycUnit.cyc_char (ring (k' + 1)) (k + 1) (k' + 1 + 1) (ZMod.char_p (k' + 1))) f
    (fun a => by rw [hqn]; exact CycEis.psi_fix _ j g k _ hu M a) hQ M hqn

end CycSym

#print axioms CycSym.cyc_recip_fq

lang/CycCoef.lean

/-
  lang/CycCoef — T49 (general ℓ = m + 1): **coefficient reduction ℤ[ζ_ℓ] → K[ζ_ℓ]** (ℤ[C_ℓ]/(N) → K[C_ℓ]/(N)) for any ring K:
    cmap               v ↦ (castZ v(a))_a on normal forms
    gmap_red           reduction commutes with the normal form
    cmap_add, cmap_mul, cmap_one, cmap_zero      a ring map
    cmap_u             u ↦ u
    cmap_pw            powers are preserved
  So identities proved over ℤ[ζ_ℓ] (e.g. J(ψ, ψ)·J(ψ̄, ψ̄) = p, CycNorm) hold over 𝔽_q[ζ_ℓ] (CycFq, CycSym).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycEval

namespace CycCoef

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob GEval
open LibRing (castZ castN)

variable {R : Type} (K : CRing R) (m : Nat)

theorem gmap_red (w : V Int (m + 1)) :
    gmap LibInt.intR m (castZ K) (CycRing.red LibInt.intR m w) = CycRing.red K m (gmap LibInt.intR m (castZ K) w) :=
  ext K m _ _ (fun a => by
    unfold gmap
    rw [gt_tb, CycRing.gt_red, CycRing.gt_red, gt_tb, gt_tb, CycEval.cast_add, CycEval.cast_neg])

theorem gmap_top (v : CycRing.C LibInt.intR m) : gt K m (gmap LibInt.intR m (castZ K) v.val) (CycRing.top m) = K.zero := by
  unfold gmap
  rw [gt_tb, v.property]
  rfl

/-- the coefficient map on normal forms. -/
def cmap (v : CycRing.C LibInt.intR m) : CycRing.C K m := ⟨gmap LibInt.intR m (castZ K) v.val, gmap_top K m v⟩

theorem cmap_add (a b : CycRing.C LibInt.intR m) :
    cmap K m ((CycRing.cyc LibInt.intR m).add a b) = (CycRing.cyc K m).add (cmap K m a) (cmap K m b) :=
  CycRing.cext K m _ _ (gmap_add LibInt.intR m K (castZ K) (fun x y => IntCast.castZ_add K x y) a.val b.val)

theorem cmap_mul (a b : CycRing.C LibInt.intR m) :
    cmap K m ((CycRing.cyc LibInt.intR m).mul a b) = (CycRing.cyc K m).mul (cmap K m a) (cmap K m b) :=
  CycRing.cext K m _ _ (by
    show gmap LibInt.intR m (castZ K) (CycRing.red LibInt.intR m (gmul LibInt.intR m a.val b.val)) =
      CycRing.red K m (gmul K m (gmap LibInt.intR m (castZ K) a.val) (gmap LibInt.intR m (castZ K) b.val))
    rw [gmap_red, gmap_mul LibInt.intR m K (castZ K) (fun x y => IntCast.castZ_add K x y)
      (fun x y => HasseDavenport.castZ_mul K x y) rfl])

theorem gmap_gone : gmap LibInt.intR m (castZ K) (gone LibInt.intR m) = gone K m :=
  ext K m _ _ (fun a => by
    unfold gmap gone
    rw [gt_tb, gt_tb, gt_tb, CycPrim.cast_delta])

theorem cmap_one : cmap K m (CycRing.cyc LibInt.intR m).one = (CycRing.cyc K m).one :=
  CycRing.cext K m _ _ (by
    show gmap LibInt.intR m (castZ K) (CycRing.red LibInt.intR m (gone LibInt.intR m)) = CycRing.red K m (gone K m)
    rw [gmap_red, gmap_gone])

theorem cmap_zero : cmap K m (CycRing.cyc LibInt.intR m).zero = (CycRing.cyc K m).zero :=
  CycRing.cext K m _ _ (ext K m _ _ (fun a => by
    show gt K m (gmap LibInt.intR m (castZ K) (gzero LibInt.intR m)) a = gt K m (gzero K m) a
    unfold gmap gzero
    rw [gt_tb, gt_tb, gt_tb]
    rfl))

theorem gmap_sig : gmap LibInt.intR m (castZ K) (CycUnit.sig LibInt.intR m) = CycUnit.sig K m :=
  ext K m _ _ (fun a => by
    unfold gmap CycUnit.sig
    rw [gt_tb, gt_mono, gt_mono]
    show castZ K (Int.mul 1 (delta LibInt.intR a (ring m).one)) = _
    rw [show Int.mul 1 (delta LibInt.intR a (ring m).one) = 1 * delta LibInt.intR a (ring m).one from rfl,
      HasseDavenport.castZ_mul, CycPrim.cast_delta]
    show K.mul (K.add K.one K.zero) _ = _
    lib_ring)

/-- **u ↦ u**. -/
theorem cmap_u : cmap K m (CycUnit.u LibInt.intR m) = CycUnit.u K m :=
  CycRing.cext K m _ _ (by
    show gmap LibInt.intR m (castZ K) (CycRing.red LibInt.intR m (CycUnit.sig LibInt.intR m)) = CycRing.red K m (CycUnit.sig K m)
    rw [gmap_red, gmap_sig])

theorem cmap_pw (a : CycRing.C LibInt.intR m) :
    ∀ k, cmap K m (pw (CycRing.cyc LibInt.intR m) a k) = pw (CycRing.cyc K m) (cmap K m a) k
  | 0 => cmap_one K m
  | k + 1 => by
    show cmap K m ((CycRing.cyc LibInt.intR m).mul (pw (CycRing.cyc LibInt.intR m) a k) a) =
      (CycRing.cyc K m).mul (pw (CycRing.cyc K m) (cmap K m a) k) (cmap K m a)
    rw [cmap_mul, cmap_pw a k]

end CycCoef

#print axioms CycCoef.gmap_red
#print axioms CycCoef.gmap_top
#print axioms CycCoef.cmap_add
#print axioms CycCoef.cmap_mul
#print axioms CycCoef.gmap_gone
#print axioms CycCoef.cmap_one
#print axioms CycCoef.cmap_zero
#print axioms CycCoef.gmap_sig
#print axioms CycCoef.cmap_u
#print axioms CycCoef.cmap_pw

lang/CycSym2.lean

/-
  lang/CycSym2 — T49 (every prime ℓ = k + 2 ≥ 3, q ≠ ℓ, p): **the residue criterion with p = J(ψ, ψ)·J(ψ̄, ψ̄)**:
  over E = 𝔽_q[ζ_ℓ] = 𝔽_q[C_ℓ]/(N),
    cmap_psi, cmap_nsm, cmap_J2    coefficient reduction ℤ[ζ_ℓ] → 𝔽_q[ζ_ℓ] sends ψ, n·1 and Jacobi sums to the same objects
    pp_J                           p = J(ψ, ψ)·J(ψ̄, ψ̄) in 𝔽_q[ζ_ℓ]   (CycNorm over ℤ, then reduce)
    cyc_recip_J                    ψ_u(q^(−f)) = (φ_u(A)·J(ψ, ψ)·J(ψ̄, ψ̄))^M,   q^f = ℓM + 1
  Composed with a ring map ρ to the residue field of 𝔮 | q this reads χ_p(q)^(−f) = (A/𝔮)(J/𝔮)(J̄/𝔮).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycSym
import lang.CycNorm
import lang.CycCoef

namespace CycSym2

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen
open FiniteRecChain (ite_pos' ite_neg')

variable {R : Type} (K : CRing R) (k j : Nat) (g : Zp (j + 1 + 1))

theorem cmap_psi (x : Zp (j + 1 + 1)) :
    CycCoef.cmap K (k + 1) (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) x) =
      CycChar.psi (j + 1) g (CycRing.cyc K (k + 1)) (CycUnit.u K (k + 1)) x := by
  unfold CycChar.psi
  by_cases hx : x = (ring (j + 1 + 1)).zero
  · rw [ite_pos' _ _ hx, ite_pos' _ _ hx]
    exact CycCoef.cmap_zero K (k + 1)
  · rw [ite_neg' _ _ hx, ite_neg' _ _ hx, CycCoef.cmap_pw, CycCoef.cmap_u]

theorem cmap_nsm : ∀ n, CycCoef.cmap K (k + 1) (nsm (CycRing.cyc LibInt.intR (k + 1)) n (CycRing.cyc LibInt.intR (k + 1)).one) =
    nsm (CycRing.cyc K (k + 1)) n (CycRing.cyc K (k + 1)).one
  | 0 => CycCoef.cmap_zero K (k + 1)
  | n + 1 => by
    show CycCoef.cmap K (k + 1) ((CycRing.cyc LibInt.intR (k + 1)).add (nsm (CycRing.cyc LibInt.intR (k + 1)) n (CycRing.cyc LibInt.intR (k + 1)).one)
      (CycRing.cyc LibInt.intR (k + 1)).one) = (CycRing.cyc K (k + 1)).add (nsm (CycRing.cyc K (k + 1)) n (CycRing.cyc K (k + 1)).one) (CycRing.cyc K (k + 1)).one
    rw [CycCoef.cmap_add, cmap_nsm n, CycCoef.cmap_one]

theorem cmap_J2 (χ μ : Zp (j + 1 + 1) → CycRing.C LibInt.intR (k + 1)) (χ' μ' : Zp (j + 1 + 1) → CycRing.C K (k + 1))
    (hχ : ∀ x, CycCoef.cmap K (k + 1) (χ x) = χ' x) (hμ : ∀ x, CycCoef.cmap K (k + 1) (μ x) = μ' x) :
    CycCoef.cmap K (k + 1) (J2 (CycRing.cyc LibInt.intR (k + 1)) j χ μ) = J2 (CycRing.cyc K (k + 1)) j χ' μ' := by
  rw [JprHom.j2_map (CycRing.cyc LibInt.intR (k + 1)) (CycRing.cyc K (k + 1)) (CycCoef.cmap K (k + 1)) j
    (CycCoef.cmap_add K (k + 1)) (CycCoef.cmap_zero K (k + 1)) (CycCoef.cmap_mul K (k + 1))]
  exact EisBridge.j2_congr _ j _ _ _ _ hχ hμ

/-- **p = J(ψ, ψ)·J(ψ̄, ψ̄) in K[ζ_ℓ]**. -/
theorem pp_J (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (t : Nat) (hk : 1 ≤ k) (hℓ : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1) (hN : j + 1 + 1 = (k + 1 + 1) * (t + 1)) :
    Pp (CycRing.cyc K (k + 1)) j =
      (CycRing.cyc K (k + 1)).mul
        (J2 (CycRing.cyc K (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc K (k + 1)) (CycUnit.u K (k + 1)))
          (CycChar.psi (j + 1) g (CycRing.cyc K (k + 1)) (CycUnit.u K (k + 1))))
        (J2 (CycRing.cyc K (k + 1)) j (fun x => CycChar.psi (j + 1) g (CycRing.cyc K (k + 1)) (CycUnit.u K (k + 1)) (iv j x))
          (fun x => CycChar.psi (j + 1) g (CycRing.cyc K (k + 1)) (CycUnit.u K (k + 1)) (iv j x))) := by
  have h := congrArg (CycCoef.cmap K (k + 1)) (CycNorm.cyc_norm j g hpr hg k t hk hℓ hN)
  rw [CycCoef.cmap_mul, cmap_J2 K k j _ _ _ _ (cmap_psi K k j g) (cmap_psi K k j g),
    cmap_J2 K k j _ _ _ _ (fun x => cmap_psi K k j g (iv j x)) (fun x => cmap_psi K k j g (iv j x)),
    NthEis.pp_nsm, cmap_nsm] at h
  rw [NthEis.pp_nsm]
  exact h.symm

/-- **χ_p(q)^(−f) = (A·J(ψ, ψ)·J(ψ̄, ψ̄))^M over 𝔽_q[ζ_ℓ]**. -/
theorem cyc_recip_J (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (t : Nat) (hk : 1 ≤ k) (hℓ : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1) (hN : j + 1 + 1 = (k + 1 + 1) * (t + 1))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (f : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ f) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ f = (k + 2) * M + 1) :
    CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1))
        (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ f) (ring (j + 1 + 1)).one) (j + 1)) =
      pw (CycRing.cyc (ring (k' + 1)) (k + 1))
        ((CycRing.cyc (ring (k' + 1)) (k + 1)).mul
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (k + 1)) (k + 1) (CycUnit.u (ring (k' + 1)) (k + 1))
            (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k))
          ((CycRing.cyc (ring (k' + 1)) (k + 1)).mul
            (J2 (CycRing.cyc (ring (k' + 1)) (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1)))
              (CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1))))
            (J2 (CycRing.cyc (ring (k' + 1)) (k + 1)) j
              (fun x => CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1)) (iv j x))
              (fun x => CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1)) (iv j x))))) M := by
  rw [← pp_J (ring (k' + 1)) k j g hpr hg t hk hℓ hN]
  exact CycSym.cyc_recip_fq j g hpr hg k t hℓ hN k' hq f hQ M hqn

end CycSym2

#print axioms CycSym2.cmap_psi
#print axioms CycSym2.cmap_nsm
#print axioms CycSym2.cmap_J2
#print axioms CycSym2.pp_J
#print axioms CycSym2.cyc_recip_J

lang/CycRho.lean

/-
  lang/CycRho — T49 (every prime ℓ = k + 2 ≥ 3, q ≠ ℓ, p): **the residue-field form of CycSym2**. For any ring map
  ρ : 𝔽_q[ζ_ℓ] → F (e.g. onto the residue field of a prime 𝔮 | q, |F| = q^f = ℓM + 1) put sym(x) = ρ(x)^M, the ℓ-th power
  residue symbol (x/𝔮)_ℓ. Then
    sym_mul        sym(xy) = sym(x) sym(y)
    cyc_recip_rho  ρ(χ_p(q))^(−f) = sym(A) · sym(J(ψ, ψ)) · sym(J(ψ̄, ψ̄))
  i.e. χ_p(q)^(−f) = (A/𝔮)(J/𝔮)(J̄/𝔮) for the primary A ∈ ℤ[C_ℓ] (CycPrim).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.CycSym2

namespace CycRho

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

section Gen
variable {R F : Type} (E : CRing R) (K : CRing F) (ρ : R → F)
  (hmul : ∀ a b, ρ (E.mul a b) = K.mul (ρ a) (ρ b))

/-- the symbol ρ(x)^M. -/
def sym (M : Nat) (x : R) : F := pw K (ρ x) M

include hmul in
theorem sym_mul (M : Nat) (x y : R) : sym K ρ M (E.mul x y) = K.mul (sym K ρ M x) (sym K ρ M y) := by
  unfold sym
  rw [hmul, Frobenius.pw_mul]

end Gen

/-- **χ_p(q)^(−f) = (A/𝔮)(J/𝔮)(J̄/𝔮)**. -/
theorem cyc_recip_rho {F : Type} (K : CRing F) (k j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (t : Nat) (hk : 1 ≤ k) (hℓ : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1) (hN : j + 1 + 1 = (k + 1 + 1) * (t + 1))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (f : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ f) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ f = (k + 2) * M + 1)
    (ρ' : CycRing.C (ring (k' + 1)) (k + 1) → F)
    (hmul' : ∀ a b, ρ' ((CycRing.cyc (ring (k' + 1)) (k + 1)).mul a b) = K.mul (ρ' a) (ρ' b))
    (hone' : ρ' (CycRing.cyc (ring (k' + 1)) (k + 1)).one = K.one) :
    ρ' (CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1))
        (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ f) (ring (j + 1 + 1)).one) (j + 1))) =
      K.mul (sym K ρ' M
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (k + 1)) (k + 1) (CycUnit.u (ring (k' + 1)) (k + 1))
            (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k)))
        (K.mul (sym K ρ' M
            (J2 (CycRing.cyc (ring (k' + 1)) (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1)))
              (CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1)))))
          (sym K ρ' M
            (J2 (CycRing.cyc (ring (k' + 1)) (k + 1)) j
              (fun x => CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1)) (iv j x))
              (fun x => CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (k + 1)) (CycUnit.u (ring (k' + 1)) (k + 1)) (iv j x))))) := by
  rw [CycSym2.cyc_recip_J k j g hpr hg t hk hℓ hN k' hq f hQ M hqn,
    JprHom.map_pw (CycRing.cyc (ring (k' + 1)) (k + 1)) K ρ' hmul' hone', ← sym_mul _ K ρ' hmul' M, ← sym_mul _ K ρ' hmul' M]
  rfl

end CycRho

#print axioms CycRho.sym_mul
#print axioms CycRho.cyc_recip_rho

#print axioms: 103 theorems, no axioms.

78.372 Stickelberger’s factorisation data for \(J(\chi ,\chi )\in \mathbb Z[\zeta _\ell ]\)

Galois action, \(P_t\)-divisibility, the norm \(J\bar J=p\) and the exact \(P_t\)-order of the Jacobi sum in \(\mathbb Z[\zeta _\ell ]=\mathbb Z[C_\ell ]/(N)\).

\[ \sigma _c J(\chi ^a,\chi ^b)=J(\chi ^{ac},\chi ^{bc}),\quad 2t<\ell \Rightarrow J\in P_t\setminus P_t^2,\quad 2t>\ell \Rightarrow J\notin P_t,\quad J(\psi ,\psi )J(\bar \psi ,\bar \psi )=p. \]

lang/CycGal.lean

/-
  lang/CycGal — T49 (general ℓ, Galois action on the group ring): for c, d ∈ 𝔽_ℓ with c·d = 1,
    σ_c(u)(b) = u(b·d),   so σ_c(σ^a) = σ^(ac)   (on ℤ[ζ_ℓ]: ζ ↦ ζ^c).
    scale_sum    Σ_x F(x·d) = Σ_x F(x)          (x ↦ xd is a permutation)
    gt_gsig, gsig_mono, gsig_add, gsig_mul, gsig_one, gsig_zero      σ_c is a ring map R[C_ℓ] → R[C_ℓ]
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GroupRingFrob
import lib.RingTac

namespace CycGal

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {R : Type} (K : CRing R) (m : Nat) (c d : Zp m) (hcd : (ring m).mul c d = (ring m).one)

include hcd in
theorem scale_sum (F : Zp m → R) : sumK K (fun x => F ((ring m).mul x d)) (L m) = sumK K F (L m) :=
  reindex K _ (CMInert.complete_allZ m K) (fun x => (ring m).mul x d) (fun y => (ring m).mul y c)
    (fun x => by
      show (ring m).mul ((ring m).mul x d) c = x
      calc (ring m).mul ((ring m).mul x d) c = (ring m).mul x ((ring m).mul c d) := by lib_ring
        _ = (ring m).mul x (ring m).one := by rw [hcd]
        _ = x := by lib_ring)
    (fun y => by
      show (ring m).mul ((ring m).mul y c) d = y
      calc (ring m).mul ((ring m).mul y c) d = (ring m).mul y ((ring m).mul c d) := by lib_ring
        _ = (ring m).mul y (ring m).one := by rw [hcd]
        _ = y := by lib_ring) F

/-- σ_c(u)(b) = u(b·d). -/
def gsig (u : V R (m + 1)) : V R (m + 1) := tb m (fun b => gt K m u ((ring m).mul b d))

theorem gt_gsig (u : V R (m + 1)) (b : Zp m) : gt K m (gsig K m d u) b = gt K m u ((ring m).mul b d) := gt_tb K m _ b

include hcd in
/-- b·d = a ⇔ b = a·c, as δ-values. -/
theorem delta_scale (a b : Zp m) : delta K ((ring m).mul b d) a = delta K b ((ring m).mul a c) := by
  unfold delta
  by_cases h : b = (ring m).mul a c
  · have h' : (ring m).mul b d = a := by
      rw [h]
      calc (ring m).mul ((ring m).mul a c) d = (ring m).mul a ((ring m).mul c d) := by lib_ring
        _ = (ring m).mul a (ring m).one := by rw [hcd]
        _ = a := by lib_ring
    rw [ite_pos' _ _ h, ite_pos' _ _ h']
  · have h' : ¬ (ring m).mul b d = a := fun e => h (by
      rw [← e]
      calc b = (ring m).mul b (ring m).one := by lib_ring
        _ = (ring m).mul b ((ring m).mul c d) := by rw [hcd]
        _ = (ring m).mul ((ring m).mul b d) c := by lib_ring)
    rw [ite_neg' _ _ h, ite_neg' _ _ h']

include hcd in
/-- **σ_c(σ^a) = σ^(ac)**. -/
theorem gsig_mono (a : Zp m) (s : R) : gsig K m d (mono K m a s) = mono K m ((ring m).mul a c) s :=
  ext K m _ _ (fun b => by rw [gt_gsig, gt_mono, gt_mono, delta_scale K m c d hcd])

theorem gsig_add (u v : V R (m + 1)) : gsig K m d (gadd K m u v) = gadd K m (gsig K m d u) (gsig K m d v) :=
  ext K m _ _ (fun b => by
    unfold gadd
    rw [gt_gsig, gt_tb, gt_tb, gt_gsig, gt_gsig])

include hcd in
theorem gsig_mul (u v : V R (m + 1)) : gsig K m d (gmul K m u v) = gmul K m (gsig K m d u) (gsig K m d v) :=
  ext K m _ _ (fun b => by
    rw [gt_gsig, gt_mul, gt_mul]
    have e : sumK K (fun a => K.mul (gt K m (gsig K m d u) a) (gt K m (gsig K m d v) ((ring m).add b ((ring m).neg a)))) (L m) =
        sumK K (fun a => K.mul (gt K m u ((ring m).mul a d)) (gt K m v ((ring m).add ((ring m).mul b d) ((ring m).neg ((ring m).mul a d))))) (L m) :=
      sumK_ext K _ _ (fun a => by
        rw [gt_gsig, gt_gsig]
        show K.mul (gt K m u ((ring m).mul a d)) (gt K m v ((ring m).mul ((ring m).add b ((ring m).neg a)) d)) = _
        rw [show (ring m).mul ((ring m).add b ((ring m).neg a)) d = (ring m).add ((ring m).mul b d) ((ring m).neg ((ring m).mul a d)) by lib_ring]) (L m)
    rw [e]
    exact (scale_sum K m c d hcd (fun x => K.mul (gt K m u x) (gt K m v ((ring m).add ((ring m).mul b d) ((ring m).neg x))))).symm)

theorem gsig_zero : gsig K m d (gzero K m) = gzero K m :=
  ext K m _ _ (fun b => by unfold gzero; rw [gt_gsig, gt_tb, gt_tb])

include hcd in
theorem gsig_one : gsig K m d (gone K m) = gone K m := by
  have e := gsig_mono K m c d hcd (ring m).zero K.one
  rw [show (ring m).mul (ring m).zero c = (ring m).zero by lib_ring] at e
  rw [show gone K m = mono K m (ring m).zero K.one from mono_pow K m (ring m).one K.one 0]
  exact e

end CycGal

#print axioms CycGal.scale_sum
#print axioms CycGal.gt_gsig
#print axioms CycGal.delta_scale
#print axioms CycGal.gsig_mono
#print axioms CycGal.gsig_add
#print axioms CycGal.gsig_mul
#print axioms CycGal.gsig_zero
#print axioms CycGal.gsig_one

lang/CycGalJ.lean

/-
  lang/CycGalJ — T49 (general ℓ): **the Galois action on the character and its Jacobi sums in ℤ[C_ℓ]**.
  With χ(x) = [dlog x] (CycPrim) and σ_c (CycGal, c = c'·1, c·d = 1):
    mono_mul_nsm   [a]·(c'·1) = c'·a in 𝔽_ℓ
    gsig_chi       σ_c(χ(x)) = χ(x)^c'                    (c' ≥ 1)
    gsig_j2        σ_c J(χ^a, χ^b) = J(χ^(ac'), χ^(bc'))
    gsig_jpr       σ_c ∏_(i ≤ k) J(χ^i, χ) = ∏_(i ≤ k) J((χ^c')^i, χ^c')
  so the conjugates of A are the elements A built from the characters χ^c' — the Galois structure used for the
  Stickelberger factorisation of A (ℓ = 5: Quint11).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycGal
import lang.CycPrim
import lang.Teich

namespace CycGalJ

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

theorem mono_mul_nsm (a : Zp m) (c' : Nat) : (ring m).mul a (nsm (ring m) c' (ring m).one) = nsm (ring m) c' a := by
  rw [Teich.nsm_one_mul (ring m) c' a]
  lib_ring

variable (j : Nat) (g : Zp (j + 1 + 1)) (c d : Zp m) (hcd : (ring m).mul c d = (ring m).one)

include hcd in
/-- **σ_c(χ(x)) = χ(x)^c'** for c = c'·1, c' ≥ 1. -/
theorem gsig_chi (c' : Nat) (hc : c = nsm (ring m) (c' + 1) (ring m).one) (x : Zp (j + 1 + 1)) :
    CycGal.gsig LibInt.intR m d (CycPrim.chi m j g x) = pw (gring LibInt.intR m) (CycPrim.chi m j g x) (c' + 1) := by
  unfold CycPrim.chi
  by_cases hx : x = (ring (j + 1 + 1)).zero
  · rw [ite_pos' _ _ hx]
    exact (CycGal.gsig_zero LibInt.intR m d).trans (GroupRingFrob.pw_zero_succ' (gring LibInt.intR m) c').symm
  · rw [ite_neg' _ _ hx, CycGal.gsig_mono LibInt.intR m c d hcd, mono_pow, hc, mono_mul_nsm]
    exact congrArg (mono LibInt.intR m _) (NthEis.pw_one LibInt.intR (c' + 1)).symm

include hcd in
/-- **σ_c J(χ^a, χ^b) = J(χ^(ac'), χ^(bc'))**. -/
theorem gsig_j2 (c' : Nat) (hc : c = nsm (ring m) (c' + 1) (ring m).one) (a b : Nat) :
    CycGal.gsig LibInt.intR m d (J2 (gring LibInt.intR m) j (psp (gring LibInt.intR m) j (CycPrim.chi m j g) a)
        (psp (gring LibInt.intR m) j (CycPrim.chi m j g) b)) =
      J2 (gring LibInt.intR m) j (psp (gring LibInt.intR m) j (CycPrim.chi m j g) ((c' + 1) * a))
        (psp (gring LibInt.intR m) j (CycPrim.chi m j g) ((c' + 1) * b)) := by
  rw [JprHom.j2_map (gring LibInt.intR m) (gring LibInt.intR m) (CycGal.gsig LibInt.intR m d) j
    (CycGal.gsig_add LibInt.intR m d) (CycGal.gsig_zero LibInt.intR m d) (CycGal.gsig_mul LibInt.intR m c d hcd)]
  apply EisBridge.j2_congr
  · intro x
    show CycGal.gsig LibInt.intR m d (pw (gring LibInt.intR m) (CycPrim.chi m j g x) a) =
      pw (gring LibInt.intR m) (CycPrim.chi m j g x) ((c' + 1) * a)
    rw [JprHom.map_pw (gring LibInt.intR m) (gring LibInt.intR m) _ (CycGal.gsig_mul LibInt.intR m c d hcd)
      (CycGal.gsig_one LibInt.intR m c d hcd), gsig_chi m j g c d hcd c' hc x, TwoPower.pwpow]
  · intro x
    show CycGal.gsig LibInt.intR m d (pw (gring LibInt.intR m) (CycPrim.chi m j g x) b) =
      pw (gring LibInt.intR m) (CycPrim.chi m j g x) ((c' + 1) * b)
    rw [JprHom.map_pw (gring LibInt.intR m) (gring LibInt.intR m) _ (CycGal.gsig_mul LibInt.intR m c d hcd)
      (CycGal.gsig_one LibInt.intR m c d hcd), gsig_chi m j g c d hcd c' hc x, TwoPower.pwpow]

include hcd in
/-- **σ_c of the Jacobi product is the Jacobi product of χ^c'**. -/
theorem gsig_jpr (c' : Nat) (hc : c = nsm (ring m) (c' + 1) (ring m).one) (k : Nat) :
    CycGal.gsig LibInt.intR m d (jpr (gring LibInt.intR m) j (CycPrim.chi m j g) k) =
      jpr (gring LibInt.intR m) j (psp (gring LibInt.intR m) j (CycPrim.chi m j g) (c' + 1)) k := by
  rw [JprHom.jpr_map (gring LibInt.intR m) (gring LibInt.intR m) (CycGal.gsig LibInt.intR m d) j
    (CycGal.gsig_add LibInt.intR m d) (CycGal.gsig_zero LibInt.intR m d) (CycGal.gsig_mul LibInt.intR m c d hcd)
    (CycGal.gsig_one LibInt.intR m c d hcd)]
  exact EisBridge.jpr_congr (gring LibInt.intR m) j _ _ (gsig_chi m j g c d hcd c' hc) k

end CycGalJ

#print axioms CycGalJ.mono_mul_nsm
#print axioms CycGalJ.gsig_chi
#print axioms CycGalJ.gsig_j2
#print axioms CycGalJ.gsig_jpr

lang/CycGalEv.lean

/-
  lang/CycGalEv — T49 (general ℓ = m + 1): **evaluation commutes with the Galois action**:
  for c·d = 1 in 𝔽_ℓ and z^ℓ = 1,   φ_z(σ_c v) = φ_(z^c)(v),
  so the symbol of σ_c(A) at the prime given by z is the symbol of A at the prime given by z^c.
    pw_mul_val   z^((a·c).val) = (z^c)^a
    phz_gsig     φ_z(σ_c v) = φ_(z^c)(v)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CycGal
import lang.CycEis

namespace CycGalEv

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob GEval
open LibRing (castZ castN)

variable {S : Type} (E : CRing S) (m : Nat) (z : S) (hz : pw E z (m + 1) = E.one)

include hz in
theorem pw_mul_val (a c : Zp m) : pw E z ((ring m).mul a c).val = pw E (pw E z c.val) a.val := by
  show pw E z (md m (a.val * c.val)) = _
  rw [pw_md E m z hz, TwoPower.pwpow, show c.val * a.val = a.val * c.val by lib_nat]

include hz in
/-- **φ_z(σ_c v) = φ_(z^c)(v)**. -/
theorem phz_gsig (c d : Zp m) (hcd : (ring m).mul c d = (ring m).one) (v : V Int (m + 1)) :
    CycEis.phz E m z (CycGal.gsig LibInt.intR m d v) = CycEis.phz E m (pw E z c.val) v := by
  show sumK E (fun b => E.mul (gt E m (gmap LibInt.intR m (castZ E) (CycGal.gsig LibInt.intR m d v)) b) (pw E z b.val)) (L m) =
    sumK E (fun a => E.mul (gt E m (gmap LibInt.intR m (castZ E) v) a) (pw E (pw E z c.val) a.val)) (L m)
  have hdc : (ring m).mul d c = (ring m).one := by
    calc (ring m).mul d c = (ring m).mul c d := by lib_ring
      _ = (ring m).one := hcd
  rw [← CycGal.scale_sum E m d c hdc (fun b => E.mul (gt E m (gmap LibInt.intR m (castZ E) (CycGal.gsig LibInt.intR m d v)) b) (pw E z b.val))]
  exact sumK_ext E _ _ (fun a => by
    show E.mul (gt E m (gmap LibInt.intR m (castZ E) (CycGal.gsig LibInt.intR m d v)) ((ring m).mul a c)) (pw E z ((ring m).mul a c).val) =
      E.mul (gt E m (gmap LibInt.intR m (castZ E) v) a) (pw E (pw E z c.val) a.val)
    unfold gmap
    rw [gt_tb, gt_tb, CycGal.gt_gsig, pw_mul_val E m z hz a c,
      show (ring m).mul ((ring m).mul a c) d = a by
        calc (ring m).mul ((ring m).mul a c) d = (ring m).mul a ((ring m).mul c d) := by lib_ring
          _ = (ring m).mul a (ring m).one := by rw [hcd]
          _ = a := by lib_ring]) (L m)

end CycGalEv

#print axioms CycGalEv.pw_mul_val
#print axioms CycGalEv.phz_gsig

lang/CycGalA.lean

/-
  lang/CycGalA — T49 (general ℓ = m + 1): **the Galois conjugates of the primary element A ∈ ℤ[C_ℓ]**.
  For c = (c' + 1)·1, c·d = 1 in 𝔽_ℓ:
    gsig_AA      σ_c(A(χ)) = A(χ^(c'+1))                          (A built from the character χ^c)
    phz_AA       φ_z(A(χ^(c'+1))) = φ_(z^c)(A(χ))                  (z^ℓ = 1)
  so the residue symbols of the conjugate elements A(χ^c) at the prime given by z are those of A at the prime given by z^c.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.CycGalJ
import lang.CycGalEv

namespace CycGalA

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

variable (m j : Nat) (g : Zp (j + 1 + 1)) (c d : Zp m) (hcd : (ring m).mul c d = (ring m).one)

include hcd in
/-- **σ_c(A(χ)) = A(χ^(c'+1))**. -/
theorem gsig_AA (c' : Nat) (hc : c = nsm (ring m) (c' + 1) (ring m).one) (k : Nat) :
    CycGal.gsig LibInt.intR m d (EisBridge.AA (gring LibInt.intR m) j (CycPrim.chi m j g) k) =
      EisBridge.AA (gring LibInt.intR m) j (psp (gring LibInt.intR m) j (CycPrim.chi m j g) (c' + 1)) k := by
  show CycGal.gsig LibInt.intR m d (gmul LibInt.intR m (jpr (gring LibInt.intR m) j (CycPrim.chi m j g) k)
      (CycPrim.chi m j g (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) =
    gmul LibInt.intR m (jpr (gring LibInt.intR m) j (psp (gring LibInt.intR m) j (CycPrim.chi m j g) (c' + 1)) k)
      (pw (gring LibInt.intR m) (CycPrim.chi m j g (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) (c' + 1))
  rw [CycGal.gsig_mul LibInt.intR m c d hcd, CycGalJ.gsig_jpr m j g c d hcd c' hc k, CycGalJ.gsig_chi m j g c d hcd c' hc]

include hcd in
/-- **φ_z(A(χ^c)) = φ_(z^c)(A(χ))**. -/
theorem phz_AA {S : Type} (E : CRing S) (z : S) (hz : pw E z (m + 1) = E.one) (c' : Nat)
    (hc : c = nsm (ring m) (c' + 1) (ring m).one) (k : Nat) :
    CycEis.phz E m z (EisBridge.AA (gring LibInt.intR m) j (psp (gring LibInt.intR m) j (CycPrim.chi m j g) (c' + 1)) k) =
      CycEis.phz E m (pw E z c.val) (EisBridge.AA (gring LibInt.intR m) j (CycPrim.chi m j g) k) := by
  rw [← gsig_AA m j g c d hcd c' hc k]
  exact CycGalEv.phz_gsig E m z hz c d hcd _

end CycGalA

#print axioms CycGalA.gsig_AA
#print axioms CycGalA.phz_AA

lang/CycStick.lean

/-
  lang/CycStick — T49/T53 (general ℓ): **Stickelberger's divisibility for the Jacobi sum J(χ, χ) ∈ ℤ[C_ℓ]**.
  p − 1 = ℓn, w = g^n (order ℓ in 𝔽_p), ι_t = φ_(w^t) : ℤ[C_ℓ] → 𝔽_p (σ ↦ w^t, the prime P_t above p):
    psi_red      ψ_(w^t)(x) = x^(tn)       (the character reduced at P_t)
    jac_red      ι_t(J(χ, χ)) = Σ_x x^(tn) (1 − x)^(tn)
    cyc_stick_lt 2t + 1 ≤ ℓ ⇒ ι_t(J(χ, χ)) = 0       (P_t | J)
    cyc_stick_gt 2t > ℓ ⇒ ι_t(J(χ, χ)) ≠ 0           (P_t ∤ J)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycEis
import lang.StickL

namespace CycStick

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen
open FiniteRecChain (ite_pos' ite_neg')

variable (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))

include hpr hg in
/-- **ψ_((g^n)^t)(x) = x^(tn)**. -/
theorem psi_red (n t : Nat) (hnt : 1 ≤ t * n) (x : Zp (j + 1 + 1)) :
    CycChar.psi (j + 1) g (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) t) x =
      pw (ring (j + 1 + 1)) x (t * n) := by
  unfold CycChar.psi
  by_cases hx : x = (ring (j + 1 + 1)).zero
  · rw [ite_pos' _ _ hx, hx]
    cases e : t * n with
    | zero => rw [e] at hnt; exact absurd hnt (fun h => nomatch h)
    | succ r => exact (GroupRingFrob.pw_zero_succ' (ring (j + 1 + 1)) r).symm
  · rw [ite_neg' _ _ hx]
    have hs := (DLogF.dlog_spec (j + 1) hpr g hg x hx).1
    calc pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) t) (DLogF.dlog (j + 1) g x)
        = pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (DLogF.dlog (j + 1) g x)) (n * t) := by
          rw [TwoPower.pwpow (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) t (DLogF.dlog (j + 1) g x),
            TwoPower.pwpow (ring (j + 1 + 1)) g n (t * DLogF.dlog (j + 1) g x),
            TwoPower.pwpow (ring (j + 1 + 1)) g (DLogF.dlog (j + 1) g x) (n * t),
            show n * (t * DLogF.dlog (j + 1) g x) = DLogF.dlog (j + 1) g x * (n * t) by lib_nat]
      _ = pw (ring (j + 1 + 1)) x (t * n) := by rw [hs, show n * t = t * n by lib_nat]

include hpr hg in
/-- **ι_t(J(χ, χ)) = Σ_x x^(tn) (1 − x)^(tn)**. -/
theorem jac_red (k n t : Nat) (hz : pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) t) (k + 1 + 1) = (ring (j + 1 + 1)).one)
    (hnt : 1 ≤ t * n) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) t)
        (J2 (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (CycPrim.chi (k + 1) j g)) =
      sumK (ring (j + 1 + 1)) (fun x => (ring (j + 1 + 1)).mul (pw (ring (j + 1 + 1)) x (t * n))
        (pw (ring (j + 1 + 1)) ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).neg x) (ring (j + 1 + 1)).one) (t * n)))
        (CMInert.allZ (j + 1 + 1) (j + 1 + 1 + 1)) := by
  rw [JprHom.j2_map (gring LibInt.intR (k + 1)) (ring (j + 1 + 1)) _ j
    (CycEis.phz_add (ring (j + 1 + 1)) (k + 1) _) (CycEis.phz_zero (ring (j + 1 + 1)) (k + 1) _)
    (CycEis.phz_mul (ring (j + 1 + 1)) (k + 1) _ hz)]
  exact sumK_ext (ring (j + 1 + 1)) _ _ (fun x => by
    show (ring (j + 1 + 1)).mul (CycEis.phz (ring (j + 1 + 1)) (k + 1) _ (CycPrim.chi (k + 1) j g x))
        (CycEis.phz (ring (j + 1 + 1)) (k + 1) _ (CycPrim.chi (k + 1) j g ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg x)))) = _
    rw [CycEis.phz_chi (ring (j + 1 + 1)) j g hpr hg (k + 1) _ hz, CycEis.phz_chi (ring (j + 1 + 1)) j g hpr hg (k + 1) _ hz,
      psi_red j g hpr hg n t hnt, psi_red j g hpr hg n t hnt,
      show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg x) =
        (ring (j + 1 + 1)).add ((ring (j + 1 + 1)).neg x) (ring (j + 1 + 1)).one by lib_ring]) _

include hg in
/-- (g^n)^t has order dividing ℓ when p − 1 = ℓn. -/
theorem z_ell (k n t : Nat) (hN : j + 1 + 1 = (k + 2) * n) :
    pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) t) (k + 1 + 1) = (ring (j + 1 + 1)).one := by
  rw [StickHom.pw_swap (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) t (k + 1 + 1), TwoPower.pwpow (ring (j + 1 + 1)) g n (k + 1 + 1),
    show n * (k + 1 + 1) = j + 1 + 1 by rw [hN]; lib_nat, hg.1]
  exact NthEis.pw_one (ring (j + 1 + 1)) t

include hpr hg in
/-- **Stickelberger, divisibility**: 2t + 1 ≤ ℓ ⇒ P_t | J(χ, χ). -/
theorem cyc_stick_lt (k n t : Nat) (hN : j + 1 + 1 = (k + 2) * n) (ht : 2 * t + 1 ≤ k + 2) (h1 : 1 ≤ t) (hn : 1 ≤ n) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) t)
        (J2 (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (CycPrim.chi (k + 1) j g)) = (ring (j + 1 + 1)).zero := by
  rw [jac_red j g hpr hg k n t (z_ell j g hg k n t hN)
    (Nat.le_trans h1 (Nat.le_trans (Nat.le_of_eq (Nat.mul_one t).symm) (Nat.mul_le_mul_left t hn)))]
  exact StickL.stick_lt (j + 1) hpr (k + 2) n t hN ht h1 hn

include hpr hg in
/-- **Stickelberger, non-divisibility**: ℓ = 2s + r, t = s + r (2t > ℓ) ⇒ P_t ∤ J(χ, χ). -/
theorem cyc_stick_gt (k n s r : Nat) (hN : j + 1 + 1 = (k + 2) * n) (hℓ : k + 2 = 2 * s + r) (hs : 1 ≤ s) (hr : 1 ≤ r)
    (hn : 1 ≤ n) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) (s + r))
        (J2 (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (CycPrim.chi (k + 1) j g)) ≠ (ring (j + 1 + 1)).zero := by
  intro h0
  have hsr : 1 ≤ (s + r) * n :=
    Nat.le_trans hr (Nat.le_trans (Nat.le_add_left r s)
      (Nat.le_trans (Nat.le_of_eq (Nat.mul_one (s + r)).symm) (Nat.mul_le_mul_left (s + r) hn)))
  rw [jac_red j g hpr hg k n (s + r) (z_ell j g hg k n (s + r) hN) hsr] at h0
  exact StickL.stick_gt (j + 1) hpr s r n (hN.trans (congrArg (· * n) hℓ)) hs hr hn h0

end CycStick

#print axioms CycStick.psi_red
#print axioms CycStick.jac_red
#print axioms CycStick.z_ell
#print axioms CycStick.cyc_stick_lt
#print axioms CycStick.cyc_stick_gt

lang/CycStickAB.lean

/-
  lang/CycStickAB — T49/T53 (general ℓ = k + 2): **Stickelberger for every factor J(χ^a, χ^b) of A ∈ ℤ[C_ℓ]** at the prime
  P_1 (σ ↦ g^n, p − 1 = ℓn); the other primes P_t are its Galois conjugates (CycGalEv, CycGalA):
    psp_red      ι(χ(x)^a) = x^(an)
    jac_red_ab   ι(J(χ^a, χ^b)) = Σ_x x^(an) (1 − x)^(bn)
    cyc_ab_lt    a + b + 1 ≤ ℓ, 1 ≤ a ⇒ ι(J(χ^a, χ^b)) = 0           (P_1 | J(χ^a, χ^b))
    cyc_ab_ge    ℓ = a + κ, b = κ + μ ≤ ℓ − 1 ⇒ ι(J(χ^a, χ^b)) ≠ 0     (P_1 ∤ J(χ^a, χ^b) when a + b ≥ ℓ)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CycStick

namespace CycStickAB

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

variable (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))

include hpr hg in
theorem psp_red (k n a : Nat) (hN : j + 1 + 1 = (k + 2) * n) (hn : 1 ≤ n) (x : Zp (j + 1 + 1)) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
        (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) a x) = pw (ring (j + 1 + 1)) x (a * n) := by
  have hz := CycStick.z_ell j g hg k n 1 hN
  show CycEis.phz (ring (j + 1 + 1)) (k + 1) _ (pw (gring LibInt.intR (k + 1)) (CycPrim.chi (k + 1) j g x) a) = _
  rw [JprHom.map_pw (gring LibInt.intR (k + 1)) (ring (j + 1 + 1)) _ (CycEis.phz_mul (ring (j + 1 + 1)) (k + 1) _ hz)
      (CycEis.phz_one (ring (j + 1 + 1)) (k + 1) _),
    CycEis.phz_chi (ring (j + 1 + 1)) j g hpr hg (k + 1) _ hz x,
    CycStick.psi_red j g hpr hg n 1 (Nat.le_trans hn (Nat.le_of_eq (by lib_nat))) x, TwoPower.pwpow,
    show 1 * n * a = a * n by lib_nat]

include hpr hg in
theorem jac_red_ab (k n a b : Nat) (hN : j + 1 + 1 = (k + 2) * n) (hn : 1 ≤ n) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
        (J2 (gring LibInt.intR (k + 1)) j (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) a)
          (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) b)) =
      sumK (ring (j + 1 + 1)) (fun x => (ring (j + 1 + 1)).mul (pw (ring (j + 1 + 1)) x (a * n))
        (pw (ring (j + 1 + 1)) ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).neg x) (ring (j + 1 + 1)).one) (b * n)))
        (CMInert.allZ (j + 1 + 1) (j + 1 + 1 + 1)) := by
  have hz := CycStick.z_ell j g hg k n 1 hN
  rw [JprHom.j2_map (gring LibInt.intR (k + 1)) (ring (j + 1 + 1)) _ j
    (CycEis.phz_add (ring (j + 1 + 1)) (k + 1) _) (CycEis.phz_zero (ring (j + 1 + 1)) (k + 1) _)
    (CycEis.phz_mul (ring (j + 1 + 1)) (k + 1) _ hz)]
  exact sumK_ext (ring (j + 1 + 1)) _ _ (fun x => by
    show (ring (j + 1 + 1)).mul
        (CycEis.phz (ring (j + 1 + 1)) (k + 1) _ (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) a x))
        (CycEis.phz (ring (j + 1 + 1)) (k + 1) _ (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) b
          ((ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg x)))) = _
    rw [psp_red j g hpr hg k n a hN hn, psp_red j g hpr hg k n b hN hn,
      show (ring (j + 1 + 1)).add (ring (j + 1 + 1)).one ((ring (j + 1 + 1)).neg x) =
        (ring (j + 1 + 1)).add ((ring (j + 1 + 1)).neg x) (ring (j + 1 + 1)).one by lib_ring]) _

include hpr hg in
/-- **P_1 | J(χ^a, χ^b)** when a + b < ℓ. -/
theorem cyc_ab_lt (k n a b : Nat) (hN : j + 1 + 1 = (k + 2) * n) (hab : a + b + 1 ≤ k + 2) (h1 : 1 ≤ a) (hn : 1 ≤ n) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
        (J2 (gring LibInt.intR (k + 1)) j (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) a)
          (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) b)) = (ring (j + 1 + 1)).zero := by
  rw [jac_red_ab j g hpr hg k n a b hN hn]
  exact StickL.stick_ab_lt (j + 1) hpr (k + 2) n a b hN hab h1 hn

include hpr hg in
/-- **P_1 ∤ J(χ^a, χ^(κ+μ))** when ℓ = a + κ and κ + μ < ℓ. -/
theorem cyc_ab_ge (k n a κ μ : Nat) (hN : j + 1 + 1 = (k + 2) * n) (hℓ : k + 2 = a + κ) (h1 : 1 ≤ a) (hβ : κ + μ + 1 ≤ a + κ)
    (hn : 1 ≤ n) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
        (J2 (gring LibInt.intR (k + 1)) j (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) a)
          (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (κ + μ))) ≠ (ring (j + 1 + 1)).zero := by
  intro h0
  rw [jac_red_ab j g hpr hg k n a (κ + μ) hN hn] at h0
  exact StickL.stick_ab_ge (j + 1) hpr a κ μ n (hN.trans (congrArg (· * n) hℓ)) h1 hβ hn h0

end CycStickAB

#print axioms CycStickAB.psp_red
#print axioms CycStickAB.jac_red_ab
#print axioms CycStickAB.cyc_ab_lt
#print axioms CycStickAB.cyc_ab_ge

lang/CycAP1.lean

/-
  lang/CycAP1 — T49/T53 (general ℓ = m + 1): **the primary element A = ∏_(i ≤ k+1) J(χ^i, χ)·χ(−1) ∈ ℤ[C_ℓ] lies in P_1**
  (k + 3 ≤ ℓ, e.g. k + 1 = ℓ − 2): its last factor J(χ^(k+1), χ) does (CycStickAB, (k + 1) + 1 < ℓ), and ι is multiplicative.
    j2_chi1      J(χ^a, χ) = J(χ^a, χ^1)
    cyc_A_P1     ι(A) = 0
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycStickAB

namespace CycAP1

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

theorem j2_chi1 (k j : Nat) (g : Zp (j + 1 + 1)) (a : Nat) :
    J2 (gring LibInt.intR (k + 1)) j (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) a) (CycPrim.chi (k + 1) j g) =
      J2 (gring LibInt.intR (k + 1)) j (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) a)
        (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) 1) :=
  EisBridge.j2_congr _ j _ _ _ _ (fun _ => rfl) (fun x => by
    show CycPrim.chi (k + 1) j g x = (gring LibInt.intR (k + 1)).mul (gring LibInt.intR (k + 1)).one (CycPrim.chi (k + 1) j g x)
    exact ((gring LibInt.intR (k + 1)).one_mul _).symm)

/-- **A ∈ P_1**. -/
theorem cyc_A_P1 (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (k n i : Nat) (hN : j + 1 + 1 = (k + 2) * n) (hi : i + 1 + 1 + 1 ≤ k + 2)
    (hn : 1 ≤ n) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
        (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (i + 1)) = (ring (j + 1 + 1)).zero := by
  have hz := CycStick.z_ell j g hg k n 1 hN
  have hmul := CycEis.phz_mul (ring (j + 1 + 1)) (k + 1) _ hz
  show CycEis.phz (ring (j + 1 + 1)) (k + 1) _ ((gring LibInt.intR (k + 1)).mul
      ((gring LibInt.intR (k + 1)).mul (jpr (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) i)
        (J2 (gring LibInt.intR (k + 1)) j (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (i + 1)) (CycPrim.chi (k + 1) j g)))
      (CycPrim.chi (k + 1) j g (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) = _
  rw [hmul, hmul, j2_chi1 k j g (i + 1), CycStickAB.cyc_ab_lt j g hpr hg k n (i + 1) 1 hN hi (Nat.le_add_left 1 i) hn]
  lib_ring

end CycAP1

#print axioms CycAP1.j2_chi1
#print axioms CycAP1.cyc_A_P1

lang/CycPsp.lean

/-
  lang/CycPsp — T49 (general ℓ = m + 1): **exponents of the character χ ∈ ℤ[C_ℓ] reduce modulo ℓ**:
    nsm_ell     (ℓq)·d = 0 in 𝔽_ℓ
    psp_ell     χ(x)^(a + 1 + ℓq) = χ(x)^(a + 1)       (x = 0 included, exponent ≥ 1)
  Used to rewrite the conjugates A(χ^c) = ∏ J(χ^(ci), χ^c) with reduced exponents ci mod ℓ.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CycPrim
import lang.Teich
import lib.Semi

namespace CycPsp

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

theorem nsm_ell (q : Nat) (d : Zp m) : nsm (ring m) ((m + 1) * q) d = (ring m).zero := by
  rw [nsm_mul_nat, Teich.nsm_one_mul (ring m) (m + 1) d, ZMod.char_p m,
    show (ring m).mul (ring m).zero d = (ring m).zero by lib_ring, Teich.nsm_zero]

/-- **χ(x)^(a + 1 + ℓq) = χ(x)^(a + 1)**. -/
theorem psp_ell (j : Nat) (g : Zp (j + 1 + 1)) (a q : Nat) (x : Zp (j + 1 + 1)) :
    psp (gring LibInt.intR m) j (CycPrim.chi m j g) (a + 1 + (m + 1) * q) x =
      psp (gring LibInt.intR m) j (CycPrim.chi m j g) (a + 1) x := by
  show pw (gring LibInt.intR m) (CycPrim.chi m j g x) (a + 1 + (m + 1) * q) = pw (gring LibInt.intR m) (CycPrim.chi m j g x) (a + 1)
  unfold CycPrim.chi
  by_cases hx : x = (ring (j + 1 + 1)).zero
  · rw [ite_pos' _ _ hx, show a + 1 + (m + 1) * q = (a + (m + 1) * q) + 1 by lib_nat,
      GroupRingFrob.pw_zero_succ' (gring LibInt.intR m) (a + (m + 1) * q), GroupRingFrob.pw_zero_succ' (gring LibInt.intR m) a]
  · rw [ite_neg' _ _ hx, mono_pow, mono_pow,
      Frobenius.nsm_add (ring m) (a + 1) ((m + 1) * q) (mk m (DLogF.dlog (j + 1) g x)), nsm_ell m q,
      show (ring m).add (nsm (ring m) (a + 1) (mk m (DLogF.dlog (j + 1) g x))) (ring m).zero =
        nsm (ring m) (a + 1) (mk m (DLogF.dlog (j + 1) g x)) by lib_ring]
    exact congrArg (mono LibInt.intR m (nsm (ring m) (a + 1) (mk m (DLogF.dlog (j + 1) g x))))
      ((NthEis.pw_one LibInt.intR (a + 1 + (m + 1) * q)).trans (NthEis.pw_one LibInt.intR (a + 1)).symm)

end CycPsp

#print axioms CycPsp.nsm_ell
#print axioms CycPsp.psp_ell

lang/JprZero.lean

/-
  lang/JprZero — T49 (general): **a Jacobi product with a vanishing factor vanishes**:
    jpr_zero    J(ψ^(i+1), ψ) = 0 and i + 1 ≤ k ⇒ ∏_(i' ≤ k) J(ψ^(i'), ψ) = 0
  (applied after a ring map, e.g. to show that the primary element A lies in a prime P_t).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.GaussPowN
import lib.RingTac

namespace JprZero

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius CharSumGen

variable {S : Type} (E : CRing S) (j : Nat) (ψ : Zp (j + 1 + 1) → S)

theorem jpr_zero (i : Nat) (hz : J2 E j (psp E j ψ (i + 1)) ψ = E.zero) : ∀ k, i + 1 ≤ k → jpr E j ψ k = E.zero
  | 0, h => absurd h (fun h' => nomatch h')
  | k + 1, h => by
    show E.mul (jpr E j ψ k) (J2 E j (psp E j ψ (k + 1)) ψ) = E.zero
    by_cases e : i = k
    · rw [← e, hz]; lib_ring
    · rw [jpr_zero i hz k (Nat.lt_of_le_of_ne (Nat.le_of_succ_le_succ h) e)]; lib_ring

end JprZero

#print axioms JprZero.jpr_zero

lang/CycAPt.lean

/-
  lang/CycAPt — T49/T53 (general ℓ = k + 2): **the primary element A ∈ ℤ[C_ℓ] lies in the conjugate primes P_c**.
  ι_z = φ_z with z = (g^n)^1 (P_1), c = (c' + 1)·1 with c·d = 1, and an index i with (c' + 1)(i + 1) = 1 + ℓq, i + 1 ≤ k:
    psp_psp      (χ^a)^b = χ^(ab)
    fac_zero     ι_z J((χ^c)^(i+1), χ^c) = 0      (the factor J(χ, χ^c), 1 + c < ℓ, CycStickAB)
    cyc_A_Pc     ι_(z^c)(A(χ)) = 0                (CycGalA: ι_z(A(χ^c)) = ι_(z^c)(A(χ)); JprZero)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CycGalA
import lang.CycStickAB
import lang.CycPsp
import lang.JprZero

namespace CycAPt

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

theorem psp_psp (m j : Nat) (g : Zp (j + 1 + 1)) (a b : Nat) (x : Zp (j + 1 + 1)) :
    psp (gring LibInt.intR m) j (psp (gring LibInt.intR m) j (CycPrim.chi m j g) a) b x =
      psp (gring LibInt.intR m) j (CycPrim.chi m j g) (a * b) x :=
  TwoPower.pwpow (gring LibInt.intR m) (CycPrim.chi m j g x) a b

variable (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))

include hpr hg in
/-- **the factor J(χ, χ^c) of A(χ^c) lies in P_1**. -/
theorem fac_zero (k n c' i q : Nat) (hN : j + 1 + 1 = (k + 2) * n) (hn : 1 ≤ n) (hc : c' + 1 + 1 + 1 ≤ k + 2)
    (hi : (c' + 1) * (i + 1) = 0 + 1 + (k + 1 + 1) * q) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
        (J2 (gring LibInt.intR (k + 1)) j
          (psp (gring LibInt.intR (k + 1)) j (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (c' + 1)) (i + 1))
          (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (c' + 1))) = (ring (j + 1 + 1)).zero := by
  have e : J2 (gring LibInt.intR (k + 1)) j
      (psp (gring LibInt.intR (k + 1)) j (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (c' + 1)) (i + 1))
      (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (c' + 1)) =
    J2 (gring LibInt.intR (k + 1)) j (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) 1)
      (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (c' + 1)) :=
    EisBridge.j2_congr _ j _ _ _ _ (fun x => by
      rw [psp_psp, hi, CycPsp.psp_ell (k + 1) j g 0 q x]) (fun _ => rfl)
  rw [e]
  exact CycStickAB.cyc_ab_lt j g hpr hg k n 1 (c' + 1) hN (by
    show 1 + (c' + 1) + 1 ≤ k + 2
    exact Nat.le_trans (Nat.le_of_eq (by lib_nat)) hc) (Nat.le_refl 1) hn

include hpr hg in
/-- **A(χ) ∈ P_c**: ι_(z^c)(A(χ)) = 0. -/
theorem cyc_A_Pc (k n c' i q : Nat) (hN : j + 1 + 1 = (k + 2) * n) (hn : 1 ≤ n) (hc : c' + 1 + 1 + 1 ≤ k + 2)
    (hi : (c' + 1) * (i + 1) = 0 + 1 + (k + 1 + 1) * q) (hik : i + 1 ≤ k)
    (c d : Zp (k + 1)) (hcd : (ring (k + 1)).mul c d = (ring (k + 1)).one) (hcc : c = nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1) c.val)
        (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k) = (ring (j + 1 + 1)).zero := by
  have hz := CycStick.z_ell j g hg k n 1 hN
  rw [← CycGalA.phz_AA (k + 1) j g c d hcd (ring (j + 1 + 1)) _ hz c' hcc k]
  have hmul := CycEis.phz_mul (ring (j + 1 + 1)) (k + 1) _ hz
  have hone := CycEis.phz_one (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
  have hadd := CycEis.phz_add (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
  have hzero := CycEis.phz_zero (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
  rw [EisBridge.aa_map (gring LibInt.intR (k + 1)) (ring (j + 1 + 1)) _ hadd hzero hmul hone j
    (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (c' + 1)) _ (fun _ => rfl) k]
  show (ring (j + 1 + 1)).mul (jpr (ring (j + 1 + 1)) j _ k) _ = _
  rw [JprZero.jpr_zero (ring (j + 1 + 1)) j _ i _ k hik]
  · show (ring (j + 1 + 1)).mul (ring (j + 1 + 1)).zero _ = _
    lib_ring
  · rw [← fac_zero j g hpr hg k n c' i q hN hn hc hi, JprHom.j2_map (gring LibInt.intR (k + 1)) (ring (j + 1 + 1)) _ j hadd hzero hmul]
    exact EisBridge.j2_congr _ j _ _ _ _
      (fun x => (JprHom.map_pw (gring LibInt.intR (k + 1)) (ring (j + 1 + 1)) _ hmul hone _ (i + 1)).symm) (fun _ => rfl)

end CycAPt

#print axioms CycAPt.psp_psp
#print axioms CycAPt.fac_zero
#print axioms CycAPt.cyc_A_Pc

lang/CycInv.lean

/-
  lang/CycInv — T49 (prime ℓ = k + 2): **inverses of 1 ≤ c ≤ ℓ − 2 modulo ℓ stay in 1 … ℓ − 2**:
    inv_index    ∃ i q, c·(i + 1) = 1 + ℓq and i + 1 ≤ ℓ − 2
  (the index hypothesis of CycAPt.cyc_A_Pc; the inverse ℓ − 1 would force c ≡ −1).
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CycFq
import lang.PowerSum
import lib.Semi

namespace CycInv

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

theorem inv_index (k c' : Nat) (hℓ : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1) (hc : c' + 1 + 1 + 1 ≤ k + 2) :
    ∃ i q, (c' + 1) * (i + 1) = 0 + 1 + (k + 1 + 1) * q ∧ i + 1 ≤ k := by
  have hlt : c' + 1 < k + 1 + 1 := Nat.le_trans (Nat.le_succ (c' + 1 + 1)) hc
  have hc0 := CycFq.small_ne k c' hlt
  have hi := ZMod.inv_exists k hℓ (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one) hc0
  have hcv : (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one).val = c' + 1 :=
    (nsm_one_val (k + 1) (c' + 1)).trans (md_small (k + 1) (c' + 1) hlt)
  generalize pw (ring (k + 1)) (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one) k = d at hi
  have hd0 : d.val ≠ 0 := fun h => by
    have e : d = (ring (k + 1)).zero := Subtype.ext h
    rw [e] at hi
    exact PowerSum.one_ne_zero' k (hi.symm.trans (by lib_ring))
  have h1 : md (k + 1) (d.val * (c' + 1)) = 1 := by
    have e := congrArg Subtype.val hi
    rw [show ((ring (k + 1)).mul d (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one)).val =
      md (k + 1) (d.val * (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one).val) from rfl, hcv] at e
    exact e.trans (md_small (k + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ k)))
  cases hv : d.val with
  | zero => exact absurd hv hd0
  | succ i =>
    rw [hv] at h1
    obtain ⟨q, hq⟩ := md_spec (k + 1) ((i + 1) * (c' + 1))
    rw [h1] at hq
    refine ⟨i, q, ?_, ?_⟩
    · rw [show (c' + 1) * (i + 1) = (i + 1) * (c' + 1) by lib_nat, hq]
      lib_nat
    · have hp : i + 1 < k + 1 + 1 := by rw [← hv]; exact d.property
      have hle : i ≤ k := Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ hp)
      refine Nat.lt_of_le_of_ne hle (fun hik => ?_)
      have hd1 : (ring (k + 1)).add d (ring (k + 1)).one = (ring (k + 1)).zero := Subtype.ext (by
        show md (k + 1) (d.val + md (k + 1) 1) = md (k + 1) 0
        rw [hv, hik, md_small (k + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ k)), md_self]
        rfl)
      apply CycFq.small_ne k (c' + 1) (Nat.le_trans (Nat.le_of_eq rfl) hc)
      calc nsm (ring (k + 1)) (c' + 1 + 1) (ring (k + 1)).one
          = (ring (k + 1)).add (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one)
              ((ring (k + 1)).mul d (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one)) := by rw [hi]; rfl
        _ = (ring (k + 1)).mul (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one) ((ring (k + 1)).add d (ring (k + 1)).one) := by lib_ring
        _ = (ring (k + 1)).zero := by rw [hd1]; lib_ring

end CycInv

#print axioms CycInv.inv_index

lang/CycAPall.lean

/-
  lang/CycAPall — T49/T53 (prime ℓ = k + 2): **the primary element A ∈ ℤ[C_ℓ] lies in P_c for every 1 ≤ c ≤ ℓ − 2**
  (CycAPt with the inverse index of CycInv; c = (c' + 1)·1, z = (g^n)^1):
    cyc_A_Pc_all   ι_(z^c)(A) = 0
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycAPt
import lang.CycInv

namespace CycAPall

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

theorem cyc_A_Pc_all (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (k n c' : Nat) (hN : j + 1 + 1 = (k + 2) * n) (hn : 1 ≤ n)
    (hℓ : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1) (hc : c' + 1 + 1 + 1 ≤ k + 2) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1)
        (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
          (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one).val)
        (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k) = (ring (j + 1 + 1)).zero := by
  obtain ⟨i, q, hi, hik⟩ := CycInv.inv_index k c' hℓ hc
  have hc0 := CycFq.small_ne k c' (Nat.le_trans (Nat.le_succ (c' + 1 + 1)) hc)
  have hinv := ZMod.inv_exists k hℓ (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one) hc0
  exact CycAPt.cyc_A_Pc j g hpr hg k n c' i q hN hn hc hi hik (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one)
    (pw (ring (k + 1)) (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one) k)
    ((show (ring (k + 1)).mul (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one) (pw (ring (k + 1)) (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one) k) =
      (ring (k + 1)).mul (pw (ring (k + 1)) (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one) k) (nsm (ring (k + 1)) (c' + 1) (ring (k + 1)).one) by lib_ring).trans hinv)
    rfl

end CycAPall

#print axioms CycAPall.cyc_A_Pc_all

lang/CycAPneg.lean

/-
  lang/CycAPneg — T49/T53 (prime ℓ = k + 2): **the primary element A ∉ P_(ℓ−1)**, the complement of CycAPall
  (G(χ)^ℓ = p·A, so A has valuation 0 at the prime where G^ℓ has valuation 1).
  At P_1 (z = (g^n)^1) every factor J(χ^(−(i+1)), χ^(−1)) of A(χ^(−1)) = A(χ^(k+1)) has exponents (k + 1 − i) + (k + 1) ≥ ℓ,
  so it does not vanish (CycStickAB.cyc_ab_ge); 𝔽_p has no zero divisors, so neither does the product.
    jpr_ne       nonvanishing factors ⇒ nonvanishing Jacobi product (no zero divisors)
    fac_ne       ι J((χ^(k+1))^(i+1), χ^(k+1)) ≠ 0       (k = i + 1 + r)
    cyc_A_Pneg   ι_(z^(ℓ−1))(A(χ)) ≠ 0
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CycAPt

namespace CycAPneg

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen
open FiniteRecChain (ite_pos' ite_neg')

theorem le_exists : ∀ {a b : Nat}, a ≤ b → ∃ r, b = a + r
  | _, _, Nat.le.refl => ⟨0, rfl⟩
  | _, _, Nat.le.step h => match le_exists h with
    | ⟨r, hr⟩ => ⟨r + 1, congrArg Nat.succ hr⟩

/-- **a Jacobi product with nonvanishing factors is nonzero** in a ring without zero divisors. -/
theorem jpr_ne {S : Type} (E : CRing S) (hE : NoZD E) (h1 : E.one ≠ E.zero) (j : Nat) (ψ : Zp (j + 1 + 1) → S) :
    ∀ k, (∀ i, i + 1 ≤ k → J2 E j (psp E j ψ (i + 1)) ψ ≠ E.zero) → jpr E j ψ k ≠ E.zero
  | 0, _ => h1
  | k + 1, h => fun h0 => by
    cases hE _ _ h0 with
    | inl e => exact jpr_ne E hE h1 j ψ k (fun i hi => h i (Nat.le_trans hi (Nat.le_succ k))) e
    | inr e => exact h k (Nat.le_refl _) e

variable (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))

include hpr hg in
/-- **the factors of A(χ^(−1)) do not lie in P_1**. -/
theorem fac_ne (k n i r : Nat) (hN : j + 1 + 1 = (k + 2) * n) (hn : 1 ≤ n) (hk : k = i + 1 + r) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
        (J2 (gring LibInt.intR (k + 1)) j
          (psp (gring LibInt.intR (k + 1)) j (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (k + 1)) (i + 1))
          (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (k + 1))) ≠ (ring (j + 1 + 1)).zero := by
  have e : J2 (gring LibInt.intR (k + 1)) j
      (psp (gring LibInt.intR (k + 1)) j (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (k + 1)) (i + 1))
      (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (k + 1)) =
    J2 (gring LibInt.intR (k + 1)) j (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (r + 1 + 1))
      (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) ((i + 1) + (r + 1))) :=
    EisBridge.j2_congr _ j _ _ _ _
      (fun x => by
        rw [CycAPt.psp_psp, show (k + 1) * (i + 1) = (r + 1) + 1 + (k + 1 + 1) * i by rw [hk]; lib_nat,
          CycPsp.psp_ell (k + 1) j g (r + 1) i x])
      (fun x => by rw [show (i + 1) + (r + 1) = k + 1 by rw [hk]; lib_nat])
  rw [e]
  exact CycStickAB.cyc_ab_ge j g hpr hg k n (r + 1 + 1) (i + 1) (r + 1) hN (by rw [hk]; lib_nat)
    (Nat.le_add_left 1 (r + 1)) (Nat.le_of_eq (by lib_nat)) hn

include hpr hg in
/-- **A ∉ P_(ℓ−1)**: ι_(z^(ℓ−1))(A(χ)) ≠ 0. -/
theorem cyc_A_Pneg (k n : Nat) (hN : j + 1 + 1 = (k + 2) * n) (hn : 1 ≤ n)
    (c d : Zp (k + 1)) (hcd : (ring (k + 1)).mul c d = (ring (k + 1)).one) (hcc : c = nsm (ring (k + 1)) (k + 1) (ring (k + 1)).one) :
    CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1) c.val)
        (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k) ≠ (ring (j + 1 + 1)).zero := by
  have hz := CycStick.z_ell j g hg k n 1 hN
  rw [← CycGalA.phz_AA (k + 1) j g c d hcd (ring (j + 1 + 1)) _ hz k hcc k]
  have hmul := CycEis.phz_mul (ring (j + 1 + 1)) (k + 1) _ hz
  have hone := CycEis.phz_one (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
  have hadd := CycEis.phz_add (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
  have hzero := CycEis.phz_zero (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
  have hE := ZMod.nozd (j + 1) hpr
  have h1 := PowerSum.one_ne_zero' (j + 1)
  rw [EisBridge.aa_map (gring LibInt.intR (k + 1)) (ring (j + 1 + 1)) _ hadd hzero hmul hone j
    (psp (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) (k + 1)) _ (fun _ => rfl) k]
  intro h0
  cases hE _ _ h0 with
  | inl hj =>
    refine jpr_ne (ring (j + 1 + 1)) hE h1 j _ k (fun i hi => ?_) hj
    obtain ⟨r, hr⟩ := le_exists hi
    intro hf
    apply fac_ne j g hpr hg k n i r hN hn hr
    rw [JprHom.j2_map (gring LibInt.intR (k + 1)) (ring (j + 1 + 1)) _ j hadd hzero hmul]
    refine (EisBridge.j2_congr _ j _ _ _ _
      (fun x => JprHom.map_pw (gring LibInt.intR (k + 1)) (ring (j + 1 + 1)) _ hmul hone _ (i + 1)) (fun _ => rfl)).trans hf
  | inr hc =>
    have hy : iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) ≠ (ring (j + 1 + 1)).zero := fun h => by
      have := CharSumGen.iv_mul j hpr _ (PrimA.neg_one_ne j)
      rw [h] at this
      exact h1 (this.symm.trans (by lib_ring))
    have hpsi : CycChar.psi (j + 1) g (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
        (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)) ≠ (ring (j + 1 + 1)).zero := by
      rw [CycStick.psi_red j g hpr hg n 1 (Nat.le_trans hn (Nat.le_of_eq (by lib_nat)))]
      exact DLog.pw_ne_zero (j + 1) hpr _ hy _
    have hc' : CycEis.phz (ring (j + 1 + 1)) (k + 1) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g n) 1)
        (pw (gring LibInt.intR (k + 1)) (CycPrim.chi (k + 1) j g (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) (k + 1)) =
        (ring (j + 1 + 1)).zero := hc
    rw [JprHom.map_pw (gring LibInt.intR (k + 1)) (ring (j + 1 + 1)) _ hmul hone,
      CycEis.phz_chi (ring (j + 1 + 1)) j g hpr hg (k + 1) _ hz] at hc'
    exact DLog.pw_ne_zero (j + 1) hpr _ hpsi (k + 1) hc'

end CycAPneg

#print axioms CycAPneg.jpr_ne
#print axioms CycAPneg.fac_ne
#print axioms CycAPneg.cyc_A_Pneg

lang/CycEval.lean

/-
  lang/CycEval — T49/T54 (general ℓ = n + 1): **evaluation of ℤ[ζ_ℓ] = ℤ[C_ℓ]/(N) at z**, for z in any ring E with
  z^ℓ = 1 and Σ_(a<ℓ) z^a = 0 (e.g. a Teichmüller root in ℤ/p²):
    rng_gs, sum_L      Σ_(a ∈ C_ℓ) z^a = Σ_(i<ℓ) z^i
    phz_red            φ_z(red w) = φ_z(w)  (φ_z kills N)
    ev, ev_add, ev_mul, ev_one, ev_zero, ev_q      ev = φ_z on normal forms is a ring map, ev([v]) = φ_z(v)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycUnit
import lang.CycEis

namespace CycEval

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)

variable {S : Type} (E : CRing S) (n : Nat) (z : S)

theorem rng_gs : ∀ k, sumK E (fun i => pw E z i) (CycUnit.rng k) = CycChar.gs E z k
  | 0 => rfl
  | k + 1 => by
    show sumK E (fun i => pw E z i) (CycUnit.rng k ++ [k]) = E.add (pw E z k) (CycChar.gs E z k)
    rw [sumK_append, rng_gs k]
    show E.add (CycChar.gs E z k) (E.add (pw E z k) E.zero) = _
    lib_ring

theorem sum_L (hz : pw E z (n + 1) = E.one) : sumK E (fun a : Zp n => pw E z a.val) (L n) = CycChar.gs E z (n + 1) :=
  ((CycUnit.sum_allZ n E (fun a => pw E z a.val) (n + 1)).trans
    (sumK_ext E _ _ (fun i => GEval.pw_md E n z hz i) (CycUnit.rng (n + 1)))).trans (rng_gs E z (n + 1))

theorem cast_add (x y : Int) : castZ E (LibInt.intR.add x y) = E.add (castZ E x) (castZ E y) := IntCast.castZ_add E x y
theorem cast_neg (x : Int) : castZ E (LibInt.intR.neg x) = E.neg (castZ E x) := HasseDavenport.castZ_neg E x

/-- **φ_z kills N**: φ_z(red w) = φ_z(w). -/
theorem phz_red (hz : pw E z (n + 1) = E.one) (hs : CycChar.gs E z (n + 1) = E.zero) (w : V Int (n + 1)) :
    CycEis.phz E n z (CycRing.red LibInt.intR n w) = CycEis.phz E n z w := by
  show sumK E (fun a => E.mul (gt E n (GEval.gmap LibInt.intR n (castZ E) (CycRing.red LibInt.intR n w)) a) (pw E z a.val)) (L n) =
    sumK E (fun a => E.mul (gt E n (GEval.gmap LibInt.intR n (castZ E) w) a) (pw E z a.val)) (L n)
  have e : ∀ a : Zp n, E.mul (gt E n (GEval.gmap LibInt.intR n (castZ E) (CycRing.red LibInt.intR n w)) a) (pw E z a.val) =
      E.add (E.mul (gt E n (GEval.gmap LibInt.intR n (castZ E) w) a) (pw E z a.val))
        (E.mul (E.neg (castZ E (gt LibInt.intR n w (CycRing.top n)))) (pw E z a.val)) := fun a => by
    unfold GEval.gmap
    rw [gt_tb, gt_tb, CycRing.gt_red, cast_add, cast_neg]
    lib_ring
  have e2 : sumK E (fun a : Zp n => E.mul (E.neg (castZ E (gt LibInt.intR n w (CycRing.top n)))) (pw E z a.val)) (L n) = E.zero :=
    (mul_sumK E (E.neg (castZ E (gt LibInt.intR n w (CycRing.top n)))) (fun a : Zp n => pw E z a.val) (L n)).symm.trans
      (by rw [sum_L E n z hz, hs]; lib_ring)
  rw [sumK_ext E _ _ e (L n), sumK_add, e2]
  lib_ring

/-- evaluation on normal forms. -/
def ev (v : CycRing.C LibInt.intR n) : S := CycEis.phz E n z v.val

theorem ev_q (hz : pw E z (n + 1) = E.one) (hs : CycChar.gs E z (n + 1) = E.zero) (v : V Int (n + 1)) :
    ev E n z (CycRing.qmap LibInt.intR n v) = CycEis.phz E n z v :=
  phz_red E n z hz hs v

theorem ev_add (a b : CycRing.C LibInt.intR n) :
    ev E n z ((CycRing.cyc LibInt.intR n).add a b) = E.add (ev E n z a) (ev E n z b) :=
  CycEis.phz_add E n z a.val b.val

theorem ev_mul (hz : pw E z (n + 1) = E.one) (hs : CycChar.gs E z (n + 1) = E.zero) (a b : CycRing.C LibInt.intR n) :
    ev E n z ((CycRing.cyc LibInt.intR n).mul a b) = E.mul (ev E n z a) (ev E n z b) :=
  (phz_red E n z hz hs (gmul LibInt.intR n a.val b.val)).trans (CycEis.phz_mul E n z hz a.val b.val)

theorem ev_one (hz : pw E z (n + 1) = E.one) (hs : CycChar.gs E z (n + 1) = E.zero) :
    ev E n z (CycRing.cyc LibInt.intR n).one = E.one :=
  (phz_red E n z hz hs (gone LibInt.intR n)).trans (CycEis.phz_one E n z)

theorem ev_zero : ev E n z (CycRing.cyc LibInt.intR n).zero = E.zero := CycEis.phz_zero E n z

end CycEval

#print axioms CycEval.rng_gs
#print axioms CycEval.sum_L
#print axioms CycEval.cast_add
#print axioms CycEval.cast_neg
#print axioms CycEval.phz_red
#print axioms CycEval.ev_q
#print axioms CycEval.ev_add
#print axioms CycEval.ev_mul
#print axioms CycEval.ev_one
#print axioms CycEval.ev_zero

lang/CycNorm.lean

/-
  lang/CycNorm — T49/T54 (every prime ℓ = k + 2 ≥ 3): **J(ψ, ψ)·J(ψ̄, ψ̄) = p in ℤ[ζ_ℓ] = ℤ[C_ℓ]/(N)**.
  ψ = u^dlog with u = [σ] (CycUnit, coefficients ℤ). In ℤ[C_ℓ] itself this fails (Σψ = ((p−1)/ℓ)·N ≠ 0); in the
  quotient N = 0 and all hypotheses of CharSumGen.jac_norm hold:
    nsm_int      n·1 = n in ℤ
    val_nsm      coordinates of n·a in R[C_ℓ]/(N) are n·a(x)
    cancel       (p − 1)·a = (p − 1)·b ⇒ a = b in ℤ[C_ℓ]/(N)
    cyc_norm     J(ψ, ψ)·J(ψ∘ι, ψ∘ι) = p
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycUnit
import lang.CycFq
import lang.JacNormN
import lib.IntRing

namespace CycNorm

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

theorem nsm_int : ∀ c : Nat, nsm LibInt.intR c LibInt.intR.one = Int.ofNat c
  | 0 => rfl
  | c + 1 => by
    show Int.add (nsm LibInt.intR c LibInt.intR.one) 1 = Int.ofNat (c + 1)
    rw [nsm_int c]
    rfl

theorem val_nsm {R : Type} (K : CRing R) (m : Nat) (a : CycRing.C K m) (x : Zp m) :
    ∀ c : Nat, gt K m (nsm (CycRing.cyc K m) c a).val x = nsm K c (gt K m a.val x)
  | 0 => by
    show gt K m (gzero K m) x = K.zero
    unfold gzero; rw [gt_tb]
  | c + 1 => by
    show gt K m (gadd K m (nsm (CycRing.cyc K m) c a).val a.val) x = K.add (nsm K c (gt K m a.val x)) (gt K m a.val x)
    unfold gadd
    rw [gt_tb, val_nsm K m a x c]

/-- **cancellation of c ≥ 1 in ℤ[C_ℓ]/(N)**. -/
theorem cancel (m c : Nat) (a b : CycRing.C LibInt.intR m)
    (h : nsm (CycRing.cyc LibInt.intR m) (c + 1) a = nsm (CycRing.cyc LibInt.intR m) (c + 1) b) : a = b :=
  CycRing.cext LibInt.intR m _ _ (ext LibInt.intR m _ _ (fun x => by
    have hx : nsm LibInt.intR (c + 1) (gt LibInt.intR m a.val x) = nsm LibInt.intR (c + 1) (gt LibInt.intR m b.val x) := by
      rw [← val_nsm, ← val_nsm, h]
    rw [Teich.nsm_one_mul LibInt.intR (c + 1) (gt LibInt.intR m a.val x),
      Teich.nsm_one_mul LibInt.intR (c + 1) (gt LibInt.intR m b.val x), nsm_int] at hx
    have h0 : LibInt.intR.mul (Int.ofNat (c + 1)) (LibInt.intR.add (gt LibInt.intR m a.val x) (LibInt.intR.neg (gt LibInt.intR m b.val x)))
        = LibInt.intR.zero := by
      calc LibInt.intR.mul (Int.ofNat (c + 1)) (LibInt.intR.add (gt LibInt.intR m a.val x) (LibInt.intR.neg (gt LibInt.intR m b.val x)))
          = LibInt.intR.add (LibInt.intR.mul (Int.ofNat (c + 1)) (gt LibInt.intR m a.val x))
              (LibInt.intR.neg (LibInt.intR.mul (Int.ofNat (c + 1)) (gt LibInt.intR m b.val x))) := by lib_ring
        _ = LibInt.intR.add (LibInt.intR.mul (Int.ofNat (c + 1)) (gt LibInt.intR m b.val x))
              (LibInt.intR.neg (LibInt.intR.mul (Int.ofNat (c + 1)) (gt LibInt.intR m b.val x))) := by rw [hx]
        _ = LibInt.intR.zero := by lib_ring
    cases LibInt.int_noZD _ _ h0 with
    | inl e => exact Nat.noConfusion (Int.ofNat.inj e)
    | inr e =>
      calc gt LibInt.intR m a.val x
          = LibInt.intR.add (LibInt.intR.add (gt LibInt.intR m a.val x) (LibInt.intR.neg (gt LibInt.intR m b.val x)))
              (gt LibInt.intR m b.val x) := by lib_ring
        _ = LibInt.intR.add LibInt.intR.zero (gt LibInt.intR m b.val x) := by rw [e]
        _ = gt LibInt.intR m b.val x := by lib_ring))

/-- **J(ψ, ψ)·J(ψ̄, ψ̄) = p in ℤ[ζ_ℓ]**, ψ = u^dlog, ℓ = k + 2 ≥ 3 prime, p − 1 = ℓ(t + 1). -/
theorem cyc_norm (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (k t : Nat) (hk : 1 ≤ k) (hℓ : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1) (hN : j + 1 + 1 = (k + 1 + 1) * (t + 1)) :
    (CycRing.cyc LibInt.intR (k + 1)).mul
        (J2 (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)))
          (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))))
        (J2 (CycRing.cyc LibInt.intR (k + 1)) j
          (fun x => CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (iv j x))
          (fun x => CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (iv j x))) =
      Pp (CycRing.cyc LibInt.intR (k + 1)) j := by
  have hw : pw (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (j + 1 + 1) = (CycRing.cyc LibInt.intR (k + 1)).one := by
    rw [hN, ← TwoPower.pwpow, CycUnit.u_ell]
    exact NthEis.pw_one _ _
  have hz1 := CycUnit.gs_u LibInt.intR k hℓ 1 (CycFq.small_ne k 0 (Nat.succ_lt_succ (Nat.zero_lt_succ k)))
  rw [show pw (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) 1 = CycUnit.u LibInt.intR (k + 1) by
    show (CycRing.cyc LibInt.intR (k + 1)).mul (CycRing.cyc LibInt.intR (k + 1)).one (CycUnit.u LibInt.intR (k + 1)) = _; lib_ring] at hz1
  have hz2 := CycUnit.gs_u LibInt.intR k hℓ (1 + 1) (CycFq.small_ne k 1 (Nat.succ_lt_succ (Nat.succ_lt_succ hk)))
  have hs2 : sumK (CycRing.cyc LibInt.intR (k + 1))
      (fun a => (CycRing.cyc LibInt.intR (k + 1)).mul
        (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) a)
        (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) a)) (LL j) =
      (CycRing.cyc LibInt.intR (k + 1)).zero :=
    (sumK_ext _ _ _ (fun a => (show (CycRing.cyc LibInt.intR (k + 1)).mul
        (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) a)
        (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) a) =
        pw (CycRing.cyc LibInt.intR (k + 1)) (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) a) (1 + 1) by
          show _ = (CycRing.cyc LibInt.intR (k + 1)).mul ((CycRing.cyc LibInt.intR (k + 1)).mul (CycRing.cyc LibInt.intR (k + 1)).one _) _
          lib_ring).trans
        (CycChar.psi_pow (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) a 1)) (LL j)).trans
      (CycChar.sum_psi (j + 1) g _ hpr hg _ (k + 1 + 1) (t + 1) hN hz2)
  refine CharSumGen.jac_norm (CycRing.cyc LibInt.intR (k + 1)) j hpr _
    (CycChar.psi_mul (j + 1) g _ hpr hg _ hw) (CycChar.psi_one (j + 1) g _ hpr hg _) (CycChar.psi_zero (j + 1) g _ _)
    (CycChar.sum_psi (j + 1) g _ hpr hg _ (k + 1 + 1) (t + 1) hN hz1) hs2
    ((reindex (CycRing.cyc LibInt.intR (k + 1)) (LL j) (CMInert.complete_allZ (j + 1 + 1) _) (iv j) (iv j)
      (CharSumGen.iv_iv j hpr) (CharSumGen.iv_iv j hpr)
      (fun a => (CycRing.cyc LibInt.intR (k + 1)).mul
        (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) a)
        (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) a))).trans hs2) ?_
  intro a b hab
  apply cancel (k + 1) (j + 1) a b
  have e : ∀ y, (CycRing.cyc LibInt.intR (k + 1)).mul y
      ((CycRing.cyc LibInt.intR (k + 1)).add (Pp (CycRing.cyc LibInt.intR (k + 1)) j) ((CycRing.cyc LibInt.intR (k + 1)).neg (CycRing.cyc LibInt.intR (k + 1)).one)) =
      nsm (CycRing.cyc LibInt.intR (k + 1)) (j + 1 + 1) y := fun y => by
    rw [NthEis.pp_nsm, Teich.nsm_one_mul (CycRing.cyc LibInt.intR (k + 1)) (j + 1 + 1) y]
    show (CycRing.cyc LibInt.intR (k + 1)).mul y ((CycRing.cyc LibInt.intR (k + 1)).add
      ((CycRing.cyc LibInt.intR (k + 1)).add (nsm (CycRing.cyc LibInt.intR (k + 1)) (j + 1 + 1) (CycRing.cyc LibInt.intR (k + 1)).one)
        (CycRing.cyc LibInt.intR (k + 1)).one) ((CycRing.cyc LibInt.intR (k + 1)).neg (CycRing.cyc LibInt.intR (k + 1)).one)) = _
    lib_ring
  rw [← e a, ← e b]
  exact hab

end CycNorm

#print axioms CycNorm.nsm_int
#print axioms CycNorm.val_nsm
#print axioms CycNorm.cancel
#print axioms CycNorm.cyc_norm

lang/CycExact.lean

/-
  lang/CycExact — T54 for the Jacobi sum of ℤ[ζ_ℓ] (every prime ℓ = k + 2, 2t < ℓ): **J(ψ, ψ) lies in P_t exactly once**.
  ψ = u^dlog in ℤ[ζ_ℓ] = ℤ[C_ℓ]/(N) (CycUnit), p − 1 = ℓn, e + 1 = tn, z = ψ_(e+1)(g) = ((lift g)^p)^(e+1) ∈ ℤ/p²
  (TeichChar2), ι₂ = evaluation at z (CycEval):
    small_ne_one   0 < r < p − 1 ⇒ g^r ≠ 1
    z_ell          z^ℓ = 1
    gs_z           Σ_(a<ℓ) z^a = 0     (z − 1 is a unit: red z = g^(e+1) ≠ 1)
    psi_q          ψ(x) = [χ(x)]
    ev_psi         ι₂(ψ(x)) = ψ_(e+1)(x)   (the Teichmüller character)
    cyc_exact      red(ι₂ J(ψ, ψ)) = 0 and ι₂ J(ψ, ψ) ≠ 0 in ℤ/p²
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CycEval
import lang.TeichChar3
import lang.PrimA

namespace CycExact

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen
open FiniteRecChain (ite_pos' ite_neg')

variable (j M : Nat) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hM : M + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1)) (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))

include hg in
theorem small_ne_one (r : Nat) (h0 : 0 < r) (hr : r < j + 1 + 1) : pw (ring (j + 1 + 1)) g r ≠ (ring (j + 1 + 1)).one :=
  fun h => PrimA.not_dvd_small (j + 1 + 1) r h0 hr (hg.2 r h)

include hM hg in
/-- **z^ℓ = 1**. -/
theorem z_ell (k n e t : Nat) (hN : j + 1 + 1 = (k + 1 + 1) * n) (he : e + 1 = t * n) :
    pw (ring M) (TeichChar.psi (j + 1) M (e + 1) g) (k + 1 + 1) = (ring M).one := by
  have hL1 : pw (ring M) (pw (ring M) (TeichChar.lift (j + 1) M g) (j + 1 + 1 + 1)) 1 =
      pw (ring M) (TeichChar.lift (j + 1) M g) (j + 1 + 1 + 1) := by
    show (ring M).mul (ring M).one _ = _; lib_ring
  have hP : pw (ring M) (pw (ring M) (TeichChar.lift (j + 1) M g) (j + 1 + 1 + 1)) (j + 1 + 1) = (ring M).one :=
    calc pw (ring M) (pw (ring M) (TeichChar.lift (j + 1) M g) (j + 1 + 1 + 1)) (j + 1 + 1)
        = pw (ring M) (TeichChar.psi (j + 1) M 1 g) (j + 1 + 1) := by
          show _ = pw (ring M) (pw (ring M) (pw (ring M) (TeichChar.lift (j + 1) M g) (j + 1 + 1 + 1)) 1) (j + 1 + 1)
          rw [hL1]
      _ = TeichChar.psi (j + 1) M 1 (pw (ring (j + 1 + 1)) g (j + 1 + 1)) := (TeichChar.psi_pw (j + 1) M hM 1 g (j + 1 + 1)).symm
      _ = (ring M).one := by rw [hg.1]; exact TeichChar.psi_one (j + 1) M 1
  show pw (ring M) (pw (ring M) (pw (ring M) (TeichChar.lift (j + 1) M g) (j + 1 + 1 + 1)) (e + 1)) (k + 1 + 1) = _
  rw [TwoPower.pwpow (ring M) _ (e + 1) (k + 1 + 1), show (e + 1) * (k + 1 + 1) = (j + 1 + 1) * t by rw [he, hN]; lib_nat,
    ← TwoPower.pwpow (ring M) _ (j + 1 + 1) t, hP]
  exact NthEis.pw_one (ring M) t

include hpr hM hg in
/-- **Σ_(a<ℓ) z^a = 0** in ℤ/p². -/
theorem gs_z (k n e t : Nat) (hN : j + 1 + 1 = (k + 1 + 1) * n) (he : e + 1 = t * n) (hlt : e + 1 < j + 1 + 1) :
    CycChar.gs (ring M) (TeichChar.psi (j + 1) M (e + 1) g) (k + 1 + 1) = (ring M).zero := by
  have hm := CycEis.gs_mul (ring M) (TeichChar.psi (j + 1) M (e + 1) g) (k + 1 + 1)
  rw [z_ell j M hM g hg k n e t hN he] at hm
  have hu : RedP2.red (j + 1) M ((ring M).add (TeichChar.psi (j + 1) M (e + 1) g) ((ring M).neg (ring M).one)) ≠ (ring (j + 1 + 1)).zero := by
    rw [RedP2.red_add (j + 1) M (j + 1 + 1 + 1) hM, TeichChar.red_neg (j + 1) M hM, RedP2.red_one (j + 1) M (j + 1 + 1 + 1) hM,
      TeichChar.red_psi (j + 1) M hpr hM (e + 1) g]
    intro h0
    apply small_ne_one j g hg (e + 1) (Nat.zero_lt_succ e) hlt
    calc pw (ring (j + 1 + 1)) g (e + 1)
        = (ring (j + 1 + 1)).add ((ring (j + 1 + 1)).add (pw (ring (j + 1 + 1)) g (e + 1)) ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))
            (ring (j + 1 + 1)).one := by lib_ring
      _ = (ring (j + 1 + 1)).add (ring (j + 1 + 1)).zero (ring (j + 1 + 1)).one := by rw [h0]
      _ = (ring (j + 1 + 1)).one := by lib_ring
  apply TeichChar.unit_of j M hpr hM _ hu
  calc (ring M).mul (CycChar.gs (ring M) (TeichChar.psi (j + 1) M (e + 1) g) (k + 1 + 1))
        ((ring M).add (TeichChar.psi (j + 1) M (e + 1) g) ((ring M).neg (ring M).one))
      = (ring M).mul ((ring M).add (TeichChar.psi (j + 1) M (e + 1) g) ((ring M).neg (ring M).one))
          (CycChar.gs (ring M) (TeichChar.psi (j + 1) M (e + 1) g) (k + 1 + 1)) := by lib_ring
    _ = (ring M).add (ring M).one ((ring M).neg (ring M).one) := hm
    _ = (ring M).mul (ring M).zero ((ring M).add (TeichChar.psi (j + 1) M (e + 1) g) ((ring M).neg (ring M).one)) := by lib_ring

/-- **ψ(x) = [χ(x)]** in ℤ[ζ_ℓ]. -/
theorem psi_q (k : Nat) (x : Zp (j + 1 + 1)) :
    CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) x =
      CycRing.qmap LibInt.intR (k + 1) (CycPrim.chi (k + 1) j g x) := by
  unfold CycChar.psi CycPrim.chi
  by_cases hx : x = (ring (j + 1 + 1)).zero
  · rw [ite_pos' _ _ hx, ite_pos' _ _ hx]
    exact (CycRing.qmap_zero LibInt.intR (k + 1)).symm
  · rw [ite_neg' _ _ hx, ite_neg' _ _ hx, CycUnit.u_pow, CycUnit.nsm_mk]
    rfl

include hpr hM hg in
/-- **ι₂(ψ(x)) = ψ_(e+1)(x)**, the Teichmüller character. -/
theorem ev_psi (k n e t : Nat) (hN : j + 1 + 1 = (k + 1 + 1) * n) (he : e + 1 = t * n) (hlt : e + 1 < j + 1 + 1)
    (x : Zp (j + 1 + 1)) :
    CycEval.ev (ring M) (k + 1) (TeichChar.psi (j + 1) M (e + 1) g)
        (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) x) =
      TeichChar.psi (j + 1) M (e + 1) x := by
  rw [psi_q j g k x, CycEval.ev_q (ring M) (k + 1) _ (z_ell j M hM g hg k n e t hN he) (gs_z j M hpr hM g hg k n e t hN he hlt),
    CycEis.phz_chi (ring M) j g hpr hg (k + 1) _ (z_ell j M hM g hg k n e t hN he) x]
  unfold CycChar.psi
  by_cases hx : x = (ring (j + 1 + 1)).zero
  · rw [ite_pos' _ _ hx, hx]
    exact (TeichChar.psi_zero (j + 1) M e).symm
  · rw [ite_neg' _ _ hx, ← TeichChar.psi_pw (j + 1) M hM (e + 1) g, (DLogF.dlog_spec (j + 1) hpr g hg x hx).1]

include hpr hM hg in
/-- **J(ψ, ψ) ∈ ℤ[ζ_ℓ] lies in P_t and not in P_t²** (2t + 1 ≤ ℓ, e + 1 = tn). -/
theorem cyc_exact (k n e t : Nat) (hN : j + 1 + 1 = (k + 1 + 1) * n) (he : e + 1 = t * n) (ht : 2 * t + 1 ≤ k + 1 + 1)
    (hn : 1 ≤ n) :
    RedP2.red (j + 1) M (CycEval.ev (ring M) (k + 1) (TeichChar.psi (j + 1) M (e + 1) g)
        (J2 (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)))
          (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))))) = (ring (j + 1 + 1)).zero ∧
      CycEval.ev (ring M) (k + 1) (TeichChar.psi (j + 1) M (e + 1) g)
        (J2 (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)))
          (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)))) ≠ (ring M).zero := by
  have hb : (2 * t + 1) * n ≤ (k + 1 + 1) * n := Nat.mul_le_mul_right n ht
  have hb2 : e + 1 + (e + 1) + n ≤ j + 1 + 1 := by
    rw [hN, show e + 1 + (e + 1) + n = (2 * t + 1) * n by rw [he]; lib_nat]
    exact hb
  have he2 : e + 1 + (e + 1) ≤ j + 1 :=
    Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left hn (e + 1 + (e + 1))) hb2)
  have hlt1 : e + 1 < j + 1 + 1 := Nat.lt_succ_of_le (Nat.le_trans (Nat.le_add_right (e + 1) (e + 1)) he2)
  have hlt2 : e + 1 + (e + 1) < j + 1 + 1 := Nat.lt_succ_of_le he2
  have hz := z_ell j M hM g hg k n e t hN he
  have hs := gs_z j M hpr hM g hg k n e t hN he hlt1
  have hJ : CycEval.ev (ring M) (k + 1) (TeichChar.psi (j + 1) M (e + 1) g)
      (J2 (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)))
        (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)))) =
      J2 (ring M) j (TeichChar.psi (j + 1) M (e + 1)) (TeichChar.psi (j + 1) M (e + 1)) := by
    rw [JprHom.j2_map (CycRing.cyc LibInt.intR (k + 1)) (ring M) _ j (CycEval.ev_add (ring M) (k + 1) _)
      (CycEval.ev_zero (ring M) (k + 1) _) (CycEval.ev_mul (ring M) (k + 1) _ hz hs)]
    exact EisBridge.j2_congr (ring M) j _ _ _ _ (ev_psi j M hpr hM g hg k n e t hN he hlt1) (ev_psi j M hpr hM g hg k n e t hN he hlt1)
  have hg2 : pw (ring (j + 1 + 1)) g (e + 1 + (e + 1)) ≠ (ring (j + 1 + 1)).one :=
    small_ne_one j g hg _ (Nat.zero_lt_succ _) hlt2
  have hg3 : pw (ring (j + 1 + 1)) g ((e + 1 + (e + 1)) * (j + 1)) ≠ (ring (j + 1 + 1)).one := fun h3 => hg2 (
    calc pw (ring (j + 1 + 1)) g (e + 1 + (e + 1))
        = (ring (j + 1 + 1)).mul (pw (ring (j + 1 + 1)) g ((e + 1 + (e + 1)) * (j + 1))) (pw (ring (j + 1 + 1)) g (e + 1 + (e + 1))) := by
          rw [h3]; lib_ring
      _ = pw (ring (j + 1 + 1)) g ((e + 1 + (e + 1)) * (j + 1) + (e + 1 + (e + 1))) := (HasseCM.pw_add' _ _ _ _).symm
      _ = pw (ring (j + 1 + 1)) g ((j + 1 + 1) * (e + 1 + (e + 1))) := by
          rw [show (e + 1 + (e + 1)) * (j + 1) + (e + 1 + (e + 1)) = (j + 1 + 1) * (e + 1 + (e + 1)) by lib_nat]
      _ = pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (j + 1 + 1)) (e + 1 + (e + 1)) := (TwoPower.pwpow _ _ _ _).symm
      _ = (ring (j + 1 + 1)).one := by rw [hg.1]; exact NthEis.pw_one _ _)
  rw [hJ]
  exact TeichChar.jac_teich j M hpr hM e he2 g (PrimJC.g_ne_zero (j + 1) g hg)
    (small_ne_one j g hg (e + 1) (Nat.zero_lt_succ e) hlt1) hg2 hg3

end CycExact

#print axioms CycExact.small_ne_one
#print axioms CycExact.z_ell
#print axioms CycExact.gs_z
#print axioms CycExact.psi_q
#print axioms CycExact.ev_psi
#print axioms CycExact.cyc_exact

lang/JacNormPair.lean

/-
  lang/JacNormPair — T49/T54: **J(χ, μ)·J(χ̄, μ̄) = p for two characters** (χ, μ, χμ nontrivial), generalising JacNormN (χ = μ).
  G(χ)G(μ) = J(χ, μ) G(χμ) (jacobi_gauss), G(χ)G(χ̄) = χ̄(−1) X (gauss_norm), X² = pX; compute (G_χ G_χ̄)(G_μ G_μ̄) both ways,
  read the ζ⁰ coefficient and cancel p − 1 and the unit χ̄(−1)μ̄(−1).
    e_sq        χ(ι(−1))² = 1
    jac_norm2   J(χ, μ) J(χ∘ι, μ∘ι) = p
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.JacNormN

namespace CharSumGen

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {S : Type} (E : CRing S) (j : Nat)

theorem e_sq (ψ : Zp (j + 1 + 1) → S) (hm : ∀ x y, ψ ((ring (j + 1 + 1)).mul x y) = E.mul (ψ x) (ψ y))
    (h1 : ψ (ring (j + 1 + 1)).one = E.one) :
    E.mul (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) (ψ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) = E.one := by
  rw [← hm, ← iv_mulmul, show (ring (j + 1 + 1)).mul ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)
    ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) = (ring (j + 1 + 1)).one by lib_ring]
  show ψ (pw (ring (j + 1 + 1)) (ring (j + 1 + 1)).one (j + 1)) = _
  rw [pw_oneJ, h1]

/-- **J(χ, μ) J(χ∘ι, μ∘ι) = p.** -/
theorem jac_norm2 (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1) (χ μ : Zp (j + 1 + 1) → S)
    (hmχ : ∀ x y, χ ((ring (j + 1 + 1)).mul x y) = E.mul (χ x) (χ y)) (h1χ : χ (ring (j + 1 + 1)).one = E.one)
    (h0χ : χ (ring (j + 1 + 1)).zero = E.zero) (hsχ : sumK E χ (LL j) = E.zero)
    (hmμ : ∀ x y, μ ((ring (j + 1 + 1)).mul x y) = E.mul (μ x) (μ y)) (h1μ : μ (ring (j + 1 + 1)).one = E.one)
    (h0μ : μ (ring (j + 1 + 1)).zero = E.zero) (hsμ : sumK E μ (LL j) = E.zero)
    (hsν : sumK E (fun a => E.mul (χ a) (μ a)) (LL j) = E.zero)
    (hsν' : sumK E (fun a => E.mul (χ (iv j a)) (μ (iv j a))) (LL j) = E.zero)
    (hcan : ∀ a b, E.mul a (E.add (Pp E j) (E.neg E.one)) = E.mul b (E.add (Pp E j) (E.neg E.one)) → a = b) :
    E.mul (J2 E j χ μ) (J2 E j (fun x => χ (iv j x)) (fun x => μ (iv j x))) = Pp E j := by
  let R := gring E (j + 1 + 1)
  let χ' : Zp (j + 1 + 1) → S := fun x => χ (iv j x)
  let μ' : Zp (j + 1 + 1) → S := fun x => μ (iv j x)
  let ν : Zp (j + 1 + 1) → S := fun x => E.mul (χ x) (μ x)
  let eχ : S := χ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))
  let eμ : S := μ (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))
  have hmχ' : ∀ x y, χ' ((ring (j + 1 + 1)).mul x y) = E.mul (χ' x) (χ' y) := fun x y => by
    show χ (iv j ((ring (j + 1 + 1)).mul x y)) = _; rw [iv_mulmul, hmχ]
  have hmμ' : ∀ x y, μ' ((ring (j + 1 + 1)).mul x y) = E.mul (μ' x) (μ' y) := fun x y => by
    show μ (iv j ((ring (j + 1 + 1)).mul x y)) = _; rw [iv_mulmul, hmμ]
  have h0χ' : χ' (ring (j + 1 + 1)).zero = E.zero := by show χ (iv j _) = _; rw [iv_zero, h0χ]
  have hmν : ∀ x y, ν ((ring (j + 1 + 1)).mul x y) = E.mul (ν x) (ν y) := fun x y => by
    show E.mul (χ _) (μ _) = E.mul (E.mul (χ x) (μ x)) (E.mul (χ y) (μ y)); rw [hmχ, hmμ]; lib_ring
  have h1ν : ν (ring (j + 1 + 1)).one = E.one := by show E.mul (χ _) (μ _) = _; rw [h1χ, h1μ]; lib_ring
  have h0ν : ν (ring (j + 1 + 1)).zero = E.zero := by show E.mul (χ _) (μ _) = _; rw [h0χ]; lib_ring
  have heχ : E.mul eχ eχ = E.one := e_sq E j χ hmχ h1χ
  have heμ : E.mul eμ eμ = E.one := e_sq E j μ hmμ h1μ
  have hA : R.mul (Gv j χ) (Gv j μ) = R.mul (m0 E j (J2 E j χ μ)) (Gv j ν) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j χ) (Gv j μ)) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j _) (Gv j ν)) c
    rw [jacobi_gauss E j hpr χ μ hmχ hmμ h0χ hsν c, gt_mono0]
    unfold Gv; rw [gt_tb])
  have hB : R.mul (Gv j χ') (Gv j μ') = R.mul (m0 E j (J2 E j χ' μ')) (Gv j (fun x => ν (iv j x))) :=
    ext E (j + 1 + 1) _ _ (fun c => by
      show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j χ') (Gv j μ')) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j _) (Gv j _)) c
      rw [jacobi_gauss E j hpr χ' μ' hmχ' hmμ' h0χ' hsν' c, gt_mono0]
      unfold Gv; rw [gt_tb])
  have hCχ : R.mul (Gv j χ) (Gv j χ') = R.mul (m0 E j eχ) (Xv E j) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j χ) (Gv j (fun x => χ (iv j x)))) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j eχ) (Xv E j)) c
    rw [gauss_norm E j hpr χ hmχ h1χ h0χ hsχ c, gt_mono0])
  have hCμ : R.mul (Gv j μ) (Gv j μ') = R.mul (m0 E j eμ) (Xv E j) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j μ) (Gv j (fun x => μ (iv j x)))) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j eμ) (Xv E j)) c
    rw [gauss_norm E j hpr μ hmμ h1μ h0μ hsμ c, gt_mono0])
  have hD : R.mul (Gv j ν) (Gv j (fun x => ν (iv j x))) = R.mul (m0 E j (E.mul eχ eμ)) (Xv E j) := ext E (j + 1 + 1) _ _ (fun c => by
    show gt E (j + 1 + 1) (gmul E (j + 1 + 1) (Gv j ν) (Gv j (fun x => ν (iv j x)))) c = gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j _) (Xv E j)) c
    rw [gauss_norm E j hpr ν hmν h1ν h0ν hsν c, gt_mono0])
  have hX : R.mul (Xv E j) (Xv E j) = R.mul (m0 E j (Pp E j)) (Xv E j) := XX E j
  have lhs : R.mul (R.mul (Gv j χ) (Gv j χ')) (R.mul (Gv j μ) (Gv j μ')) =
      R.mul (m0 E j (E.mul (E.mul eχ eμ) (Pp E j))) (Xv E j) := by
    rw [hCχ, hCμ]
    calc R.mul (R.mul (m0 E j eχ) (Xv E j)) (R.mul (m0 E j eμ) (Xv E j))
        = R.mul (R.mul (m0 E j eχ) (m0 E j eμ)) (R.mul (Xv E j) (Xv E j)) := by lib_ring
      _ = R.mul (m0 E j (E.mul eχ eμ)) (R.mul (m0 E j (Pp E j)) (Xv E j)) := by rw [m0_mul, hX]
      _ = R.mul (R.mul (m0 E j (E.mul eχ eμ)) (m0 E j (Pp E j))) (Xv E j) := by lib_ring
      _ = _ := by rw [m0_mul]
  have rhs : R.mul (R.mul (Gv j χ) (Gv j χ')) (R.mul (Gv j μ) (Gv j μ')) =
      R.mul (m0 E j (E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul eχ eμ))) (Xv E j) := by
    calc R.mul (R.mul (Gv j χ) (Gv j χ')) (R.mul (Gv j μ) (Gv j μ'))
        = R.mul (R.mul (Gv j χ) (Gv j μ)) (R.mul (Gv j χ') (Gv j μ')) := by lib_ring
      _ = R.mul (R.mul (m0 E j (J2 E j χ μ)) (Gv j ν)) (R.mul (m0 E j (J2 E j χ' μ')) (Gv j (fun x => ν (iv j x)))) := by rw [hA, hB]
      _ = R.mul (R.mul (m0 E j (J2 E j χ μ)) (m0 E j (J2 E j χ' μ'))) (R.mul (Gv j ν) (Gv j (fun x => ν (iv j x)))) := by lib_ring
      _ = R.mul (m0 E j (E.mul (J2 E j χ μ) (J2 E j χ' μ'))) (R.mul (m0 E j (E.mul eχ eμ)) (Xv E j)) := by rw [m0_mul, hD]
      _ = R.mul (R.mul (m0 E j (E.mul (J2 E j χ μ) (J2 E j χ' μ'))) (m0 E j (E.mul eχ eμ))) (Xv E j) := by lib_ring
      _ = _ := by rw [m0_mul]
  have h00 : gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j (E.mul (E.mul eχ eμ) (Pp E j))) (Xv E j)) (ring (j + 1 + 1)).zero =
      gt E (j + 1 + 1) (gmul E (j + 1 + 1) (m0 E j (E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul eχ eμ))) (Xv E j)) (ring (j + 1 + 1)).zero :=
    congrArg (fun u => gt E (j + 1 + 1) u (ring (j + 1 + 1)).zero) (lhs.symm.trans rhs)
  rw [gt_mono0, gt_mono0, gt_X] at h00
  have hd : delta E (ring (j + 1 + 1)).zero (ring (j + 1 + 1)).zero = E.one := by
    unfold delta; rw [ite_pos' _ _ rfl]
  rw [hd] at h00
  have hc : E.mul (E.mul eχ eμ) (Pp E j) = E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul eχ eμ) := by
    apply hcan
    calc E.mul (E.mul (E.mul eχ eμ) (Pp E j)) (E.add (Pp E j) (E.neg E.one))
        = E.mul (E.mul (E.mul eχ eμ) (Pp E j)) (E.add (E.mul (Pp E j) E.one) (E.neg E.one)) := by lib_ring
      _ = E.mul (E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul eχ eμ)) (E.add (E.mul (Pp E j) E.one) (E.neg E.one)) := h00
      _ = _ := by lib_ring
  calc E.mul (J2 E j χ μ) (J2 E j χ' μ')
      = E.mul (E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul eχ eμ)) (E.mul eχ eμ) := by
        rw [show E.mul (E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul eχ eμ)) (E.mul eχ eμ) =
          E.mul (E.mul (J2 E j χ μ) (J2 E j χ' μ')) (E.mul (E.mul eχ eχ) (E.mul eμ eμ)) by lib_ring, heχ, heμ]
        lib_ring
    _ = E.mul (E.mul (E.mul eχ eμ) (Pp E j)) (E.mul eχ eμ) := by rw [hc]
    _ = E.mul (Pp E j) (E.mul (E.mul eχ eχ) (E.mul eμ eμ)) := by lib_ring
    _ = Pp E j := by rw [heχ, heμ]; lib_ring

end CharSumGen

#print axioms CharSumGen.e_sq
#print axioms CharSumGen.jac_norm2

lang/TeichPair.lean

/-
  lang/TeichPair — T54 for pairs: **J(ψ_(a+1), ψ_(b+1)) of Teichmüller characters modulo p² is divisible by p exactly once**
  when (a + 1) + (b + 1) ≤ p − 2 (the generalisation of TeichChar3.jac_teich from a = b, via CharSumGen.jac_norm2):
    psi_add      ψ_a ψ_b = ψ_(a+b)
    jac_teich2   red J(ψ_(a+1), ψ_(b+1)) = 0 and J(ψ_(a+1), ψ_(b+1)) ≠ 0 in ℤ/p²
  With CycStickAB this gives the exact P-order (0 or 1) of every factor J(χ^a, χ^b) of the primary element A.
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.TeichChar3
import lang.JacNormPair

namespace TeichChar

open ZMod YMChain FiniteRecChain Frobenius

variable (j M : Nat) (hpr : ∀ d, FTAChain.Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hM : M + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1))

theorem psi_add (a b : Nat) (x : Zp (j + 1 + 1)) :
    (ring M).mul (psi (j + 1) M a x) (psi (j + 1) M b x) = psi (j + 1) M (a + b) x := by
  unfold psi
  rw [HasseCM.pw_add' (ring M) (pw (ring M) (lift (j + 1) M x) (j + 1 + 1 + 1)) a b]

include hpr hM in
/-- **J(ψ_(a+1), ψ_(b+1)) is divisible by p exactly once in ℤ/p²**. -/
theorem jac_teich2 (a b : Nat) (he2 : a + 1 + (b + 1) ≤ j + 1) (g : Zp (j + 1 + 1)) (hg : g ≠ (ring (j + 1 + 1)).zero)
    (hga : pw (ring (j + 1 + 1)) g (a + 1) ≠ (ring (j + 1 + 1)).one)
    (hgb : pw (ring (j + 1 + 1)) g (b + 1) ≠ (ring (j + 1 + 1)).one)
    (hgab : pw (ring (j + 1 + 1)) g (a + 1 + (b + 1)) ≠ (ring (j + 1 + 1)).one)
    (hg3 : pw (ring (j + 1 + 1)) g ((a + 1 + (b + 1)) * (j + 1)) ≠ (ring (j + 1 + 1)).one) :
    RedP2.red (j + 1) M (CharSumGen.J2 (ring M) j (psi (j + 1) M (a + 1)) (psi (j + 1) M (b + 1))) = (ring (j + 1 + 1)).zero ∧
      CharSumGen.J2 (ring M) j (psi (j + 1) M (a + 1)) (psi (j + 1) M (b + 1)) ≠ (ring M).zero := by
  have hsa := psi_sum j M hpr hM (a + 1) g hg hga
  have hsb := psi_sum j M hpr hM (b + 1) g hg hgb
  have hsν : sumK (ring M) (fun x => (ring M).mul (psi (j + 1) M (a + 1) x) (psi (j + 1) M (b + 1) x)) (CharSumGen.LL j) = (ring M).zero :=
    (sumK_ext (ring M) _ _ (fun x => psi_add j M (a + 1) (b + 1) x) (CharSumGen.LL j)).trans (psi_sum j M hpr hM _ g hg hgab)
  have hsν' : sumK (ring M) (fun x => (ring M).mul (psi (j + 1) M (a + 1) (CharSumGen.iv j x)) (psi (j + 1) M (b + 1) (CharSumGen.iv j x)))
      (CharSumGen.LL j) = (ring M).zero :=
    (sumK_ext (ring M) _ (fun x => psi (j + 1) M ((a + 1 + (b + 1)) * (j + 1)) x) (fun x => by
      rw [psi_add j M (a + 1) (b + 1)]
      exact psi_powk j M hM (a + 1 + (b + 1)) (j + 1) x) (CharSumGen.LL j)).trans (psi_sum j M hpr hM _ g hg hg3)
  have hredP : RedP2.red (j + 1) M (CharSumGen.Pp (ring M) j) ≠ (ring (j + 1 + 1)).one := by
    rw [red_P j M hM]; exact fun h => PowerSum.one_ne_zero' (j + 1) h.symm
  have hcan : ∀ x y, (ring M).mul x ((ring M).add (CharSumGen.Pp (ring M) j) ((ring M).neg (ring M).one)) =
      (ring M).mul y ((ring M).add (CharSumGen.Pp (ring M) j) ((ring M).neg (ring M).one)) → x = y :=
    unit_of j M hpr hM _ (red_sub_one j M hM _ hredP)
  have hJJ := CharSumGen.jac_norm2 (ring M) j hpr (psi (j + 1) M (a + 1)) (psi (j + 1) M (b + 1))
    (psi_mul (j + 1) M hM (a + 1)) (psi_one (j + 1) M (a + 1)) (psi_zero (j + 1) M a) hsa
    (psi_mul (j + 1) M hM (b + 1)) (psi_one (j + 1) M (b + 1)) (psi_zero (j + 1) M b) hsb hsν hsν' hcan
  have hdiv : RedP2.red (j + 1) M (CharSumGen.J2 (ring M) j (psi (j + 1) M (a + 1)) (psi (j + 1) M (b + 1))) = (ring (j + 1 + 1)).zero := by
    unfold CharSumGen.J2
    rw [StickHom.map_sumK (ring M) (ring (j + 1 + 1)) (RedP2.red (j + 1) M) (RedP2.red_add (j + 1) M (j + 1 + 1 + 1) hM)
      (RedP2.red_zero (j + 1) M (j + 1 + 1 + 1) hM)]
    rw [sumK_ext (ring (j + 1 + 1)) _ (fun x => (ring (j + 1 + 1)).mul (pw (ring (j + 1 + 1)) x (a + 1))
      (pw (ring (j + 1 + 1)) ((ring (j + 1 + 1)).add ((ring (j + 1 + 1)).neg x) (ring (j + 1 + 1)).one) (b + 1))) (fun x => by
        rw [RedP2.red_mul (j + 1) M (j + 1 + 1 + 1) hM, red_psi (j + 1) M hpr hM, red_psi (j + 1) M hpr hM,
          (ring (j + 1 + 1)).add_comm (ring (j + 1 + 1)).one])]
    exact Stick1.jac_gen (j + 1) hpr (a + 1) (b + 1) (Nat.succ_le_succ (Nat.zero_le a)) he2
  have hlt : j + 1 + 1 + 1 < M + 1 := by
    rw [hM]
    exact Nat.le_trans (Nat.add_le_add_left (Nat.le_add_left 1 (j * (j + 5) + 5)) (j + 1 + 1 + 1))
      (Nat.le_of_eq (by lib_nat))
  have hP : CharSumGen.Pp (ring M) j ≠ (ring M).zero := by
    show sumK (ring M) (fun _ => (ring M).one) (CMInert.allZ (j + 1 + 1) (j + 1 + 1 + 1)) ≠ _
    rw [sum_one_allZ (ring M) (j + 1 + 1) (j + 1 + 1 + 1)]
    exact StickVal.small_ne_zero M (j + 1 + 1) hlt
  exact StickVal.val_one (ring M) (ring M) (ring (j + 1 + 1)) (fun x => x) (RedP2.red (j + 1) M) (fun a b => rfl)
    (fun b => by lib_ring) _ _ _ hJJ hP hdiv

end TeichChar

#print axioms TeichChar.psi_add
#print axioms TeichChar.jac_teich2

lang/CycExactAB.lean

/-
  lang/CycExactAB — T54 for every factor of A (prime ℓ = k + 2): **J(ψ^a, ψ^b) ∈ ℤ[ζ_ℓ] lies in P_1 exactly once when a + b < ℓ**.
  ι₂ = evaluation at z = ψ_(e+1)(g), e + 1 = n (P_1, CycExact with t = 1); ι₂(ψ(x)^a) = ψ_(n a)(x) (Teichmüller),
  then TeichPair.jac_teich2 with n a = ea + 1, n b = eb + 1.
    ev_pow       ι₂(ψ(x)^a) = ψ_((e+1)a)(x)
    cyc_exact_ab red(ι₂ J(ψ^a, ψ^b)) = 0 and ι₂ J(ψ^a, ψ^b) ≠ 0 in ℤ/p²
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CycExact
import lang.TeichPair

namespace CycExactAB

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

variable (j M : Nat) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hM : M + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1)) (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))

include hpr hM hg in
theorem ev_pow (k n e : Nat) (hN : j + 1 + 1 = (k + 1 + 1) * n) (he : e + 1 = 1 * n) (hlt : e + 1 < j + 1 + 1) (a : Nat)
    (x : Zp (j + 1 + 1)) :
    CycEval.ev (ring M) (k + 1) (TeichChar.psi (j + 1) M (e + 1) g)
        (psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) a x) =
      TeichChar.psi (j + 1) M ((e + 1) * a) x := by
  have hz := CycExact.z_ell j M hM g hg k n e 1 hN he
  have hs := CycExact.gs_z j M hpr hM g hg k n e 1 hN he hlt
  show CycEval.ev (ring M) (k + 1) _ (pw (CycRing.cyc LibInt.intR (k + 1)) _ a) = _
  rw [JprHom.map_pw (CycRing.cyc LibInt.intR (k + 1)) (ring M) _ (CycEval.ev_mul (ring M) (k + 1) _ hz hs)
      (CycEval.ev_one (ring M) (k + 1) _ hz hs),
    CycExact.ev_psi j M hpr hM g hg k n e 1 hN he hlt x]
  unfold TeichChar.psi
  exact RootAll.pw_pw (ring M) _ (e + 1) a

include hpr hM hg in
/-- **J(ψ^a, ψ^b) lies in P_1 exactly once** (a + b < ℓ, n a = ea + 1, n b = eb + 1). -/
theorem cyc_exact_ab (k n e a b ea eb : Nat) (hN : j + 1 + 1 = (k + 1 + 1) * n) (he : e + 1 = 1 * n) (hn : 1 ≤ n)
    (hab : a + b + 1 ≤ k + 1 + 1) (hea : (e + 1) * a = ea + 1) (heb : (e + 1) * b = eb + 1) :
    RedP2.red (j + 1) M (CycEval.ev (ring M) (k + 1) (TeichChar.psi (j + 1) M (e + 1) g)
        (J2 (CycRing.cyc LibInt.intR (k + 1)) j
          (psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) a)
          (psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) b))) =
        (ring (j + 1 + 1)).zero ∧
      CycEval.ev (ring M) (k + 1) (TeichChar.psi (j + 1) M (e + 1) g)
        (J2 (CycRing.cyc LibInt.intR (k + 1)) j
          (psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) a)
          (psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) b)) ≠
        (ring M).zero := by
  -- sizes: ea + 1 + (eb + 1) = n(a + b) ≤ n(ℓ − 1) = p − 1 − n ≤ p − 2
  have hb : (a + b + 1) * n ≤ (k + 1 + 1) * n := Nat.mul_le_mul_right n hab
  have hb2 : ea + 1 + (eb + 1) + n ≤ j + 1 + 1 := by
    rw [hN, ← hea, ← heb, he, show 1 * n * a + 1 * n * b + n = (a + b + 1) * n by lib_nat]
    exact hb
  have he2 : ea + 1 + (eb + 1) ≤ j + 1 :=
    Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left hn (ea + 1 + (eb + 1))) hb2)
  have hlt2 : ea + 1 + (eb + 1) < j + 1 + 1 := Nat.lt_succ_of_le he2
  have hlta : ea + 1 < j + 1 + 1 := Nat.lt_succ_of_le (Nat.le_trans (Nat.le_add_right (ea + 1) (eb + 1)) he2)
  have hltb : eb + 1 < j + 1 + 1 := Nat.lt_succ_of_le (Nat.le_trans (Nat.le_add_left (eb + 1) (ea + 1)) he2)
  have hlte : e + 1 < j + 1 + 1 := by
    rw [he, hN]
    exact Nat.lt_of_lt_of_le (Nat.lt_succ_self (1 * n)) (Nat.le_trans (Nat.add_le_add_left hn (1 * n))
      (Nat.le_trans (Nat.le_add_right (1 * n + n) (k * n)) (Nat.le_of_eq (by lib_nat))))
  have hz := CycExact.z_ell j M hM g hg k n e 1 hN he
  have hs := CycExact.gs_z j M hpr hM g hg k n e 1 hN he hlte
  have hJ : CycEval.ev (ring M) (k + 1) (TeichChar.psi (j + 1) M (e + 1) g)
      (J2 (CycRing.cyc LibInt.intR (k + 1)) j
        (psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) a)
        (psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) b)) =
      J2 (ring M) j (TeichChar.psi (j + 1) M (ea + 1)) (TeichChar.psi (j + 1) M (eb + 1)) := by
    rw [JprHom.j2_map (CycRing.cyc LibInt.intR (k + 1)) (ring M) _ j (CycEval.ev_add (ring M) (k + 1) _)
      (CycEval.ev_zero (ring M) (k + 1) _) (CycEval.ev_mul (ring M) (k + 1) _ hz hs)]
    exact EisBridge.j2_congr (ring M) j _ _ _ _
      (fun x => (ev_pow j M hpr hM g hg k n e hN he hlte a x).trans (by rw [hea]))
      (fun x => (ev_pow j M hpr hM g hg k n e hN he hlte b x).trans (by rw [heb]))
  have hg2 : pw (ring (j + 1 + 1)) g (ea + 1 + (eb + 1)) ≠ (ring (j + 1 + 1)).one :=
    CycExact.small_ne_one j g hg _ (Nat.zero_lt_succ _) hlt2
  have hg3 : pw (ring (j + 1 + 1)) g ((ea + 1 + (eb + 1)) * (j + 1)) ≠ (ring (j + 1 + 1)).one := fun h3 => hg2 (
    calc pw (ring (j + 1 + 1)) g (ea + 1 + (eb + 1))
        = (ring (j + 1 + 1)).mul (pw (ring (j + 1 + 1)) g ((ea + 1 + (eb + 1)) * (j + 1))) (pw (ring (j + 1 + 1)) g (ea + 1 + (eb + 1))) := by
          rw [h3]; lib_ring
      _ = pw (ring (j + 1 + 1)) g ((ea + 1 + (eb + 1)) * (j + 1) + (ea + 1 + (eb + 1))) := (HasseCM.pw_add' _ _ _ _).symm
      _ = pw (ring (j + 1 + 1)) g ((j + 1 + 1) * (ea + 1 + (eb + 1))) := by
          rw [show (ea + 1 + (eb + 1)) * (j + 1) + (ea + 1 + (eb + 1)) = (j + 1 + 1) * (ea + 1 + (eb + 1)) by lib_nat]
      _ = pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (j + 1 + 1)) (ea + 1 + (eb + 1)) := (TwoPower.pwpow _ _ _ _).symm
      _ = (ring (j + 1 + 1)).one := by rw [hg.1]; exact NthEis.pw_one _ _)
  rw [hJ]
  exact TeichChar.jac_teich2 j M hpr hM ea eb he2 g (PrimJC.g_ne_zero (j + 1) g hg)
    (CycExact.small_ne_one j g hg _ (Nat.zero_lt_succ _) hlta) (CycExact.small_ne_one j g hg _ (Nat.zero_lt_succ _) hltb) hg2 hg3

end CycExactAB

#print axioms CycExactAB.ev_pow
#print axioms CycExactAB.cyc_exact_ab

#print axioms: 64 theorems, no axioms.

78.373 The exact valuation \(v_{P_1}(A)=\ell -2\) of the primary element

Evaluation \(\iota _N:\mathbb Z[C_\ell ]\to \mathbb Z/p^N\) at the Teichmüller root of the prime \(P_1\): each factor of \(A\) has valuation one, and valuations add.

\[ \iota _N\bigl (J(\chi ^{a},\chi )\bigr )=p\,u_a,\qquad \iota _N(A)=p^{\ell -2}\,U,\qquad u_a,U\not \equiv 0 \pmod p. \]

lang/BinomP.lean

/-
  lang/BinomP — T49/T54 (step toward Teichmüller lifts modulo p^N): **the binomial expansion to second order** and its
  p-adic consequence, in any commutative ring K with P = (k + 1)·1:
    binom2       (a + y)^(n+1) = a^(n+1) + (n + 1)·a^n·y + y²·Q        (some Q)
    pow_lift     (a + P^(m+1)·b)^(k+1) = a^(k+1) + P^(m+2)·c           (some c)
  so raising to the p-th power improves congruences modulo powers of p by one: the basis of ψ(x) = lift(x)^(p^(N−1)) mod p^N.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.Teich
import lib.RingTac

namespace BinomP

open FTAChain ZMod YMChain Frobenius

variable {A : Type} (K : DetChain.CRing A)

/-- **(a + y)^(n+1) = a^(n+1) + (n + 1) a^n y + y² Q**. -/
theorem binom2 (a y : A) : ∀ n : Nat, ∃ Q : A,
    pw K (K.add a y) (n + 1) = K.add (K.add (pw K a (n + 1)) (K.mul (nsm K (n + 1) K.one) (K.mul (pw K a n) y))) (K.mul (K.mul y y) Q)
  | 0 => ⟨K.zero, by
      show K.mul K.one (K.add a y) = K.add (K.add (K.mul K.one a) (K.mul (K.add K.zero K.one) (K.mul K.one y))) (K.mul (K.mul y y) K.zero)
      lib_ring⟩
  | n + 1 => by
    obtain ⟨Q, hQ⟩ := binom2 a y n
    refine ⟨K.add (K.mul (nsm K (n + 1) K.one) (pw K a n)) (K.mul Q (K.add a y)), ?_⟩
    show K.mul (pw K (K.add a y) (n + 1)) (K.add a y) =
      K.add (K.add (K.mul (pw K a (n + 1)) a) (K.mul (K.add (nsm K (n + 1) K.one) K.one) (K.mul (pw K a (n + 1)) y)))
        (K.mul (K.mul y y) (K.add (K.mul (nsm K (n + 1) K.one) (pw K a n)) (K.mul Q (K.add a y))))
    rw [hQ]
    show K.mul (K.add (K.add (K.mul (pw K a n) a) (K.mul (nsm K (n + 1) K.one) (K.mul (pw K a n) y))) (K.mul (K.mul y y) Q)) (K.add a y) =
      K.add (K.add (K.mul (K.mul (pw K a n) a) a) (K.mul (K.add (nsm K (n + 1) K.one) K.one) (K.mul (K.mul (pw K a n) a) y)))
        (K.mul (K.mul y y) (K.add (K.mul (nsm K (n + 1) K.one) (pw K a n)) (K.mul Q (K.add a y))))
    lib_ring

/-- **(a + P^(m+1) b)^p = a^p + P^(m+2) c**, P = p·1. -/
theorem pow_lift (k m : Nat) (a b : A) : ∃ c : A,
    pw K (K.add a (K.mul (pw K (nsm K (k + 1) K.one) (m + 1)) b)) (k + 1) =
      K.add (pw K a (k + 1)) (K.mul (pw K (nsm K (k + 1) K.one) (m + 1 + 1)) c) := by
  obtain ⟨Q, hQ⟩ := binom2 K a (K.mul (pw K (nsm K (k + 1) K.one) (m + 1)) b) k
  refine ⟨K.add (K.mul (pw K a k) b) (K.mul (K.mul (pw K (nsm K (k + 1) K.one) m) (K.mul b b)) Q), ?_⟩
  rw [hQ]
  show K.add (K.add (pw K a (k + 1)) (K.mul (nsm K (k + 1) K.one) (K.mul (pw K a k) (K.mul (K.mul (pw K (nsm K (k + 1) K.one) m) (nsm K (k + 1) K.one)) b))))
      (K.mul (K.mul (K.mul (K.mul (pw K (nsm K (k + 1) K.one) m) (nsm K (k + 1) K.one)) b) (K.mul (K.mul (pw K (nsm K (k + 1) K.one) m) (nsm K (k + 1) K.one)) b)) Q) =
    K.add (pw K a (k + 1)) (K.mul (K.mul (K.mul (pw K (nsm K (k + 1) K.one) m) (nsm K (k + 1) K.one)) (nsm K (k + 1) K.one))
      (K.add (K.mul (pw K a k) b) (K.mul (K.mul (pw K (nsm K (k + 1) K.one) m) (K.mul b b)) Q)))
  lib_ring

end BinomP

#print axioms BinomP.binom2
#print axioms BinomP.pow_lift

lang/BinomPj.lean

/-
  lang/BinomPj — T49/T54 (Teichmüller lifts modulo p^N, step 2): **raising to p^j gains j powers of p**:
    pow_lift_iter   (a + P^(m+1) b)^(p^j) = a^(p^j) + P^(m+1+j) c      (P = p·1, some c)
  With m = 0 and P^N = 0 (ℤ/p^N, j = N − 1) the lift x ↦ lift(x)^(p^(N−1)) is multiplicative modulo p^N.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.BinomP
import lang.TwoPower
import lib.Semi

namespace BinomP

open FTAChain ZMod YMChain Frobenius

variable {A : Type} (K : DetChain.CRing A)

/-- **(a + P^(m+1) b)^(p^j) = a^(p^j) + P^(m+1+j) c**. -/
theorem pow_lift_iter (k m : Nat) (a b : A) : ∀ j : Nat, ∃ c : A,
    pw K (K.add a (K.mul (pw K (nsm K (k + 1) K.one) (m + 1)) b)) ((k + 1) ^ j) =
      K.add (pw K a ((k + 1) ^ j)) (K.mul (pw K (nsm K (k + 1) K.one) (m + 1 + j)) c)
  | 0 => ⟨b, by
      show pw K (K.add a (K.mul (pw K (nsm K (k + 1) K.one) (m + 1)) b)) 1 =
        K.add (pw K a 1) (K.mul (pw K (nsm K (k + 1) K.one) (m + 1)) b)
      show K.mul K.one (K.add a (K.mul (pw K (nsm K (k + 1) K.one) (m + 1)) b)) =
        K.add (K.mul K.one a) (K.mul (pw K (nsm K (k + 1) K.one) (m + 1)) b)
      lib_ring⟩
  | j + 1 => by
    obtain ⟨c, hc⟩ := pow_lift_iter k m a b j
    obtain ⟨c', hc'⟩ := pow_lift K k (m + j) (pw K a ((k + 1) ^ j)) c
    refine ⟨c', ?_⟩
    rw [show (k + 1) ^ (j + 1) = (k + 1) ^ j * (k + 1) from rfl, ← TwoPower.pwpow, hc, ← TwoPower.pwpow,
      show m + 1 + j = m + j + 1 by lib_nat, hc', show m + j + 1 + 1 = m + 1 + (j + 1) by lib_nat]

end BinomP

#print axioms BinomP.pow_lift_iter

lang/TeichN.lean

/-
  lang/TeichN — T49/T54 (step 3): **the Teichmüller character modulo p^N**. ℤ/(M + 1) with M + 1 = p·k (TeichChar2 had k = p),
  lift x = x mod (M + 1), and P = p·1 with P^(N'+1) = 0 (M + 1 | p^(N'+1)):
    red_liftK, red_negK, lift_mulK     the reduction facts of TeichChar2 for any k
    psiN e x = (lift x)^(p^N' · e)
    psiN_mul      ψ(xy) = ψ(x)ψ(y)          (BinomPj: (ab + P u)^(p^N') = (ab)^(p^N') + P^(N'+1) c)
    red_psiN      red ψ_e(x) = x^e          (Fermat)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.TeichChar2
import lang.BinomPj

namespace TeichN

open ZMod YMChain FiniteRecChain Frobenius

variable (J M k : Nat) (hk : M + 1 = (J + 1 + 1) * k)

include hk in
theorem red_liftK (x : Zp (J + 1)) : RedP2.red J M (TeichChar.lift J M x) = x := by
  show mk (J + 1) (md M x.val) = x
  have e : mk (J + 1) (md M x.val) = mk (J + 1) x.val := Subtype.ext (RedP2.mdmd J M k hk x.val)
  rw [e, mk_val]

include hk in
theorem red_negK (y : Zp M) : RedP2.red J M ((ring M).neg y) = (ring (J + 1)).neg (RedP2.red J M y) := by
  have h0 := RedP2.red_add J M k hk y ((ring M).neg y)
  rw [show (ring M).add y ((ring M).neg y) = (ring M).zero by lib_ring, RedP2.red_zero J M k hk] at h0
  calc RedP2.red J M ((ring M).neg y)
      = (ring (J + 1)).add ((ring (J + 1)).add (RedP2.red J M y) (RedP2.red J M ((ring M).neg y))) ((ring (J + 1)).neg (RedP2.red J M y)) := by lib_ring
    _ = (ring (J + 1)).add (ring (J + 1)).zero ((ring (J + 1)).neg (RedP2.red J M y)) := by rw [h0]
    _ = _ := by lib_ring

include hk in
theorem lift_mulK (x y : Zp (J + 1)) :
    ∃ u, TeichChar.lift J M ((ring (J + 1)).mul x y) = (ring M).add ((ring M).mul (TeichChar.lift J M x) (TeichChar.lift J M y))
      ((ring M).mul (nsm (ring M) (J + 1 + 1) (ring M).one) u) := by
  have hr : RedP2.red J M ((ring M).add (TeichChar.lift J M ((ring (J + 1)).mul x y))
      ((ring M).neg ((ring M).mul (TeichChar.lift J M x) (TeichChar.lift J M y)))) = (ring (J + 1)).zero := by
    rw [RedP2.red_add J M k hk, red_negK J M k hk, RedP2.red_mul J M k hk, red_liftK J M k hk, red_liftK J M k hk,
      red_liftK J M k hk]
    lib_ring
  obtain ⟨u, hu⟩ := RedP2.red_ker J M _ hr
  exact ⟨u, by
    calc TeichChar.lift J M ((ring (J + 1)).mul x y)
        = (ring M).add ((ring M).mul (TeichChar.lift J M x) (TeichChar.lift J M y))
            ((ring M).add (TeichChar.lift J M ((ring (J + 1)).mul x y))
              ((ring M).neg ((ring M).mul (TeichChar.lift J M x) (TeichChar.lift J M y)))) := by lib_ring
      _ = _ := by rw [hu]⟩

/-- ψ_e(x) = (lift x)^(p^N' e). -/
def psiN (N' e : Nat) (x : Zp (J + 1)) : Zp M := pw (ring M) (pw (ring M) (TeichChar.lift J M x) ((J + 1 + 1) ^ N')) e

include hk in
/-- **ψ is multiplicative modulo p^N** when P^(N'+1) = 0. -/
theorem psiN_mul (N' : Nat) (hN : pw (ring M) (nsm (ring M) (J + 1 + 1) (ring M).one) (N' + 1) = (ring M).zero) (e : Nat)
    (x y : Zp (J + 1)) : psiN J M N' e ((ring (J + 1)).mul x y) = (ring M).mul (psiN J M N' e x) (psiN J M N' e y) := by
  obtain ⟨u, hu⟩ := lift_mulK J M k hk x y
  obtain ⟨c, hc⟩ := BinomP.pow_lift_iter (ring M) (J + 1) 0
    ((ring M).mul (TeichChar.lift J M x) (TeichChar.lift J M y)) u N'
  unfold psiN
  rw [hu, show (ring M).mul (nsm (ring M) (J + 1 + 1) (ring M).one) u =
      (ring M).mul (pw (ring M) (nsm (ring M) (J + 1 + 1) (ring M).one) (0 + 1)) u by
        show _ = (ring M).mul ((ring M).mul (ring M).one (nsm (ring M) (J + 1 + 1) (ring M).one)) u; lib_ring,
    hc, show 0 + 1 + N' = N' + 1 by lib_nat, hN,
    show (ring M).add (pw (ring M) ((ring M).mul (TeichChar.lift J M x) (TeichChar.lift J M y)) ((J + 1 + 1) ^ N'))
        ((ring M).mul (ring M).zero c) = pw (ring M) ((ring M).mul (TeichChar.lift J M x) (TeichChar.lift J M y)) ((J + 1 + 1) ^ N') by lib_ring,
    Frobenius.pw_mul, Frobenius.pw_mul]

include hk in
/-- **red ψ_e(x) = x^e**. -/
theorem red_psiN (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (N' e : Nat) (x : Zp (J + 1)) :
    RedP2.red J M (psiN J M N' e x) = pw (ring (J + 1)) x e := by
  have hf : ∀ n : Nat, pw (ring (J + 1)) x ((J + 1 + 1) ^ n) = x := fun n => by
    induction n with
    | zero => show (ring (J + 1)).mul (ring (J + 1)).one x = x; lib_ring
    | succ n ih =>
      rw [show (J + 1 + 1) ^ (n + 1) = (J + 1 + 1) ^ n * (J + 1 + 1) from rfl, ← TwoPower.pwpow, ih]
      exact ZMod.fermat J hpr x
  unfold psiN
  rw [TeichRoot.map_pw (ring M) (ring (J + 1)) (RedP2.red J M) (RedP2.red_mul J M k hk) (RedP2.red_one J M k hk),
    TeichRoot.map_pw (ring M) (ring (J + 1)) (RedP2.red J M) (RedP2.red_mul J M k hk) (RedP2.red_one J M k hk),
    red_liftK J M k hk, hf N']

end TeichN

#print axioms TeichN.red_liftK
#print axioms TeichN.red_negK
#print axioms TeichN.lift_mulK
#print axioms TeichN.psiN_mul
#print axioms TeichN.red_psiN

lang/ValP.lean

/-
  lang/ValP — T49/T54 (step 4): **p-adic valuation bookkeeping in ℤ/(M + 1)**, M + 1 = p·k, P = p·1, red : ℤ/(M+1) → 𝔽_p:
    val_one      red x = 0 and x ∉ P²·(ℤ/(M+1)) ⇒ x = P·u with red u ≠ 0          (valuation exactly one)
    val_mul      x = P^a u, y = P^b v, red u, red v ≠ 0 ⇒ x y = P^(a+b) w, red w ≠ 0    (valuations add)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.RedKer
import lang.PowerSum
import lang.HasseCM
import lib.RingTac

namespace ValP

open ZMod YMChain FiniteRecChain Frobenius

variable (J M k : Nat) (hk : M + 1 = (J + 1 + 1) * k)

/-- **valuation exactly one**. -/
theorem val_one (x : Zp M) (h0 : RedP2.red J M x = (ring (J + 1)).zero)
    (h2 : ∀ s, x ≠ (ring M).mul (pw (ring M) (nsm (ring M) (J + 1 + 1) (ring M).one) 2) s) :
    ∃ u, x = (ring M).mul (nsm (ring M) (J + 1 + 1) (ring M).one) u ∧ RedP2.red J M u ≠ (ring (J + 1)).zero := by
  obtain ⟨u, hu⟩ := RedP2.red_ker J M x h0
  refine ⟨u, hu, fun hu0 => ?_⟩
  obtain ⟨s, hs⟩ := RedP2.red_ker J M u hu0
  apply h2 s
  rw [hu, hs]
  show _ = (ring M).mul ((ring M).mul ((ring M).mul (ring M).one (nsm (ring M) (J + 1 + 1) (ring M).one)) (nsm (ring M) (J + 1 + 1) (ring M).one)) s
  lib_ring

include hk in
/-- **valuations add** (𝔽_p has no zero divisors). -/
theorem val_mul (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (a b : Nat) (u v : Zp M)
    (hu : RedP2.red J M u ≠ (ring (J + 1)).zero) (hv : RedP2.red J M v ≠ (ring (J + 1)).zero) :
    (ring M).mul ((ring M).mul (pw (ring M) (nsm (ring M) (J + 1 + 1) (ring M).one) a) u)
        ((ring M).mul (pw (ring M) (nsm (ring M) (J + 1 + 1) (ring M).one) b) v) =
      (ring M).mul (pw (ring M) (nsm (ring M) (J + 1 + 1) (ring M).one) (a + b)) ((ring M).mul u v) ∧
    RedP2.red J M ((ring M).mul u v) ≠ (ring (J + 1)).zero := by
  refine ⟨?_, fun h => ?_⟩
  · rw [HasseCM.pw_add']; lib_ring
  · rw [RedP2.red_mul J M k hk] at h
    cases ZMod.nozd J hpr _ _ h with
    | inl e => exact hu e
    | inr e => exact hv e

end ValP

#print axioms ValP.val_one
#print axioms ValP.val_mul

lang/RedPN.lean

/-
  lang/RedPN — T49/T54 (step 5): **reduction ℤ/p^N → ℤ/p² is compatible with the Teichmüller characters**.
  ℤ/(M + 1) with M + 1 = p²·k₂ (ℤ/p² = Zp (J₂ + 1), J₂ + 2 = p²), r = RedP2.red J₂ M:
    r_lift        r(lift_N x) = lift_2 x
    r_pw          r(a^n) = r(a)^n
    teich_stable  lift_2(x)^(p^j·p) = lift_2(x)^p      (the mod-p² Teichmüller lift is stable under p-th powers)
    r_psiN        r(ψ_N(x)) = ψ_2(x)                      (TeichN.psiN with N' + 1, TeichChar.psi)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.TeichN

namespace RedPN

open ZMod YMChain FiniteRecChain Frobenius

variable (J J₂ M k₂ : Nat) (hk₂ : M + 1 = (J₂ + 1 + 1) * k₂) (hM₂ : J₂ + 1 + 1 = (J + 1 + 1) * (J + 1 + 1))

include hk₂ in
theorem r_lift (x : Zp (J + 1)) : RedP2.red J₂ M (TeichChar.lift J M x) = TeichChar.lift J (J₂ + 1) x :=
  Subtype.ext (RedP2.mdmd J₂ M k₂ hk₂ x.val)

include hk₂ in
theorem r_pw (a : Zp M) (n : Nat) : RedP2.red J₂ M (pw (ring M) a n) = pw (ring (J₂ + 1)) (RedP2.red J₂ M a) n :=
  TeichRoot.map_pw (ring M) (ring (J₂ + 1)) (RedP2.red J₂ M) (RedP2.red_mul J₂ M k₂ hk₂) (RedP2.red_one J₂ M k₂ hk₂) a n

include hM₂ in
/-- **lift(x)^(p^(j+1)) = lift(x)^p in ℤ/p²**. -/
theorem teich_stable (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (x : Zp (J + 1)) :
    ∀ j, pw (ring (J₂ + 1)) (TeichChar.lift J (J₂ + 1) x) ((J + 1 + 1) ^ j * (J + 1 + 1)) =
      pw (ring (J₂ + 1)) (TeichChar.lift J (J₂ + 1) x) (J + 1 + 1)
  | 0 => by
    show pw (ring (J₂ + 1)) _ (1 * (J + 1 + 1)) = _
    rw [show 1 * (J + 1 + 1) = J + 1 + 1 by lib_nat]
  | j + 1 => by
    have h1 : ∀ y : Zp (J₂ + 1), pw (ring (J₂ + 1)) y 1 = y := fun y => by
      show (ring (J₂ + 1)).mul (ring (J₂ + 1)).one y = y; lib_ring
    have hf := TeichChar.psi_pw J (J₂ + 1) hM₂ 1 x (J + 1 + 1)
    rw [ZMod.fermat J hpr x] at hf
    unfold TeichChar.psi at hf
    rw [h1] at hf
    rw [show (J + 1 + 1) ^ (j + 1) * (J + 1 + 1) = ((J + 1 + 1) ^ j * (J + 1 + 1)) * (J + 1 + 1) from rfl, ← TwoPower.pwpow,
      teich_stable hpr x j]
    exact hf.symm

include hk₂ hM₂ in
/-- **r(ψ_N(x)) = ψ_2(x)**. -/
theorem r_psiN (hpr : ∀ d, FTAChain.Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1) (N' e : Nat) (x : Zp (J + 1)) :
    RedP2.red J₂ M (TeichN.psiN J M (N' + 1) e x) = TeichChar.psi J (J₂ + 1) e x := by
  show RedP2.red J₂ M (pw (ring M) (pw (ring M) (TeichChar.lift J M x) ((J + 1 + 1) ^ N' * (J + 1 + 1))) e) =
    pw (ring (J₂ + 1)) (pw (ring (J₂ + 1)) (TeichChar.lift J (J₂ + 1) x) (J + 1 + 1)) e
  rw [r_pw J₂ M k₂ hk₂, r_pw J₂ M k₂ hk₂, r_lift J J₂ M k₂ hk₂, teich_stable J J₂ hM₂ hpr x N']

end RedPN

#print axioms RedPN.r_lift
#print axioms RedPN.r_pw
#print axioms RedPN.teich_stable
#print axioms RedPN.r_psiN

lang/ValA1.lean

/-
  lang/ValA1 — T49/T54 (step 6): **evaluation ℤ[C_ℓ] → ℤ/p^N at z = ψ_N(g) (the prime P_1)**.
  ψ_N = TeichN.psiN (P^(N'+1) = 0 in ℤ/(M + 1)), p − 1 = ℓn, e + 1 = n, z = ψ_(N,e+1)(g):
    psiN_one, psiN_pw, psiN_exp     ψ(1) = 1, ψ(x^m) = ψ(x)^m, ψ_e(x)^a = ψ_(ea)(x)
    z_ellN                          z^ℓ = 1
    phz_chiN                        φ_z(χ(x)) = ψ_(N,e+1)(x)
    jac_imageN                      φ_z(J(χ^a, χ^b)) = J(ψ_(N,(e+1)a), ψ_(N,(e+1)b))
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.TeichN
import lang.CycEis
import lang.RootAll

namespace ValA1

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen
open FiniteRecChain (ite_pos' ite_neg')

variable (j M k N' : Nat) (hk : M + 1 = (j + 1 + 1 + 1) * k)
  (hN0 : pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) (N' + 1) = (ring M).zero)

theorem psiN_one (e : Nat) : TeichN.psiN (j + 1) M N' e (ring (j + 1 + 1)).one = (ring M).one := by
  unfold TeichN.psiN
  rw [TeichChar.lift_one, RootAll.pw_one, RootAll.pw_one]

include hk hN0 in
theorem psiN_pw (e : Nat) (x : Zp (j + 1 + 1)) :
    ∀ m, TeichN.psiN (j + 1) M N' e (pw (ring (j + 1 + 1)) x m) = pw (ring M) (TeichN.psiN (j + 1) M N' e x) m
  | 0 => psiN_one j M N' e
  | m + 1 => by
    show TeichN.psiN (j + 1) M N' e ((ring (j + 1 + 1)).mul (pw (ring (j + 1 + 1)) x m) x) =
      (ring M).mul (pw (ring M) (TeichN.psiN (j + 1) M N' e x) m) (TeichN.psiN (j + 1) M N' e x)
    rw [TeichN.psiN_mul (j + 1) M k hk N' hN0, psiN_pw e x m]

theorem zero_pow_p : ∀ N'' : Nat, pw (ring M) (ring M).zero ((j + 1 + 1 + 1) ^ N'') = (ring M).zero
  | 0 => by show (ring M).mul (ring M).one (ring M).zero = (ring M).zero; lib_ring
  | N'' + 1 => by
    rw [show (j + 1 + 1 + 1) ^ (N'' + 1) = (j + 1 + 1 + 1) ^ N'' * (j + 1 + 1 + 1) from rfl, ← TwoPower.pwpow, zero_pow_p N'']
    exact GroupRingFrob.pw_zero_succ' (ring M) (j + 1 + 1)

theorem psiN_exp (e a : Nat) (x : Zp (j + 1 + 1)) :
    pw (ring M) (TeichN.psiN (j + 1) M N' e x) a = TeichN.psiN (j + 1) M N' (e * a) x := by
  unfold TeichN.psiN
  exact RootAll.pw_pw (ring M) _ e a

variable (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))

include hk hN0 hg in
/-- **z^ℓ = 1**. -/
theorem z_ellN (kk n e : Nat) (hN : j + 1 + 1 = (kk + 1 + 1) * n) (he : e + 1 = n) :
    pw (ring M) (TeichN.psiN (j + 1) M N' (e + 1) g) (kk + 1 + 1) = (ring M).one := by
  rw [psiN_exp, show (e + 1) * (kk + 1 + 1) = 1 * (j + 1 + 1) by rw [he, hN]; lib_nat, ← psiN_exp,
    ← psiN_pw j M k N' hk hN0 1 g (j + 1 + 1), hg.1, psiN_one]

include hk hN0 hpr hg in
/-- **φ_z(χ(x)) = ψ_(N,e+1)(x)**. -/
theorem phz_chiN (kk e : Nat) (hz : pw (ring M) (TeichN.psiN (j + 1) M N' (e + 1) g) (kk + 1 + 1) = (ring M).one)
    (x : Zp (j + 1 + 1)) :
    CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M N' (e + 1) g) (CycPrim.chi (kk + 1) j g x) =
      TeichN.psiN (j + 1) M N' (e + 1) x := by
  rw [CycEis.phz_chi (ring M) j g hpr hg (kk + 1) _ hz x]
  unfold CycChar.psi
  by_cases hx : x = (ring (j + 1 + 1)).zero
  · rw [ite_pos' _ _ hx, hx]
    unfold TeichN.psiN
    show (ring M).zero = pw (ring M) (pw (ring M) (TeichChar.lift (j + 1) M (ring (j + 1 + 1)).zero) ((j + 1 + 1 + 1) ^ N')) (e + 1)
    rw [show TeichChar.lift (j + 1) M (ring (j + 1 + 1)).zero = (ring M).zero from rfl, zero_pow_p j M N',
      GroupRingFrob.pw_zero_succ']
  · rw [ite_neg' _ _ hx, ← psiN_pw j M k N' hk hN0 (e + 1) g, (DLogF.dlog_spec (j + 1) hpr g hg x hx).1]

include hk hN0 hpr hg in
/-- **φ_z(J(χ^a, χ^b)) = J(ψ_(N,(e+1)a), ψ_(N,(e+1)b))**. -/
theorem jac_imageN (kk e a b : Nat) (hz : pw (ring M) (TeichN.psiN (j + 1) M N' (e + 1) g) (kk + 1 + 1) = (ring M).one) :
    CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M N' (e + 1) g)
        (J2 (gring LibInt.intR (kk + 1)) j (psp (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) a)
          (psp (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) b)) =
      J2 (ring M) j (TeichN.psiN (j + 1) M N' ((e + 1) * a)) (TeichN.psiN (j + 1) M N' ((e + 1) * b)) := by
  have hmul := CycEis.phz_mul (ring M) (kk + 1) _ hz
  have hone := CycEis.phz_one (ring M) (kk + 1) (TeichN.psiN (j + 1) M N' (e + 1) g)
  rw [JprHom.j2_map (gring LibInt.intR (kk + 1)) (ring M) _ j (CycEis.phz_add (ring M) (kk + 1) _)
    (CycEis.phz_zero (ring M) (kk + 1) _) hmul]
  exact EisBridge.j2_congr (ring M) j _ _ _ _
    (fun x => (JprHom.map_pw (gring LibInt.intR (kk + 1)) (ring M) _ hmul hone _ a).trans
      ((congrArg (fun w => pw (ring M) w a) (phz_chiN j M k N' hk hN0 g hpr hg kk e hz x)).trans (psiN_exp j M N' (e + 1) a x)))
    (fun x => (JprHom.map_pw (gring LibInt.intR (kk + 1)) (ring M) _ hmul hone _ b).trans
      ((congrArg (fun w => pw (ring M) w b) (phz_chiN j M k N' hk hN0 g hpr hg kk e hz x)).trans (psiN_exp j M N' (e + 1) b x)))

end ValA1

#print axioms ValA1.psiN_one
#print axioms ValA1.psiN_pw
#print axioms ValA1.zero_pow_p
#print axioms ValA1.psiN_exp
#print axioms ValA1.z_ellN
#print axioms ValA1.phz_chiN
#print axioms ValA1.jac_imageN

lang/ValA2.lean

/-
  lang/ValA2 — T49/T54 (step 7): **the primary element A ∈ ℤ[C_ℓ] has P_1-valuation exactly ℓ − 2**:
  under ι_N = φ_z : ℤ[C_ℓ] → ℤ/p^N (z = ψ_(N,n)(g), P_1), ι_N(A) = P^(ℓ−2)·U with U a unit modulo p.
    red_comp     reduction to 𝔽_p factors through ℤ/p²
    r_nsm        reduction ℤ/p^N → ℤ/p² sends n·1 to n·1
    fac_val      ι_N J(χ^(a+1), χ) = P·u, red u ≠ 0         (a + 1 ≤ ℓ − 2; TeichPair through RedPN, then ValP.val_one)
  (the product over the ℓ − 2 factors, ι_N(A) = P^(ℓ−2)·U, follows with ValP.val_mul in the next file)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.ValA1
import lang.RedPN
import lang.ValP
import lang.TeichPair
import lang.CycAP1
import lang.CycExact

namespace ValA2

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

variable (j M k J₂ k₂ N'' : Nat) (hk : M + 1 = (j + 1 + 1 + 1) * k) (hk₂ : M + 1 = (J₂ + 1 + 1) * k₂)
  (hM₂ : J₂ + 1 + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1))
  (hN0 : pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) (N'' + 1 + 1) = (ring M).zero)

include hM₂ in
theorem red_comp (x : Zp M) : RedP2.red (j + 1) M x = RedP2.red (j + 1) (J₂ + 1) (RedP2.red J₂ M x) :=
  Subtype.ext (RedP2.mdmd (j + 1) (J₂ + 1) (j + 1 + 1 + 1) hM₂ x.val).symm

include hk₂ in
theorem r_nsm : ∀ n, RedP2.red J₂ M (nsm (ring M) n (ring M).one) = nsm (ring (J₂ + 1)) n (ring (J₂ + 1)).one
  | 0 => RedP2.red_zero J₂ M k₂ hk₂
  | n + 1 => by
    show RedP2.red J₂ M ((ring M).add (nsm (ring M) n (ring M).one) (ring M).one) = (ring (J₂ + 1)).add _ _
    rw [RedP2.red_add J₂ M k₂ hk₂, r_nsm n, RedP2.red_one J₂ M k₂ hk₂]

variable (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))

include hk hk₂ hM₂ hN0 hpr hg in
/-- **each factor J(χ^(a+1), χ) of A maps to P·(unit)**. -/
theorem fac_val (kk n e a : Nat) (hN : j + 1 + 1 = (kk + 1 + 1) * n) (he : e + 1 = n) (hn : 1 ≤ n) (ha : a + 1 ≤ kk) :
    ∃ u, CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
        (J2 (gring LibInt.intR (kk + 1)) j (psp (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) (a + 1))
          (CycPrim.chi (kk + 1) j g)) = (ring M).mul (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) u ∧
      RedP2.red (j + 1) M u ≠ (ring (j + 1 + 1)).zero := by
  have hz := ValA1.z_ellN j M k (N'' + 1) hk hN0 g hg kk n e hN he
  have hJ : CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
      (J2 (gring LibInt.intR (kk + 1)) j (psp (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) (a + 1))
        (CycPrim.chi (kk + 1) j g)) =
      J2 (ring M) j (TeichN.psiN (j + 1) M (N'' + 1) (e * (a + 1) + a + 1)) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1)) := by
    rw [CycAP1.j2_chi1 kk j g (a + 1), ValA1.jac_imageN j M k (N'' + 1) hk hN0 g hpr hg kk e (a + 1) 1 hz,
      show (e + 1) * (a + 1) = e * (a + 1) + a + 1 by lib_nat, show (e + 1) * 1 = e + 1 by lib_nat]
  have hr : RedP2.red J₂ M (J2 (ring M) j (TeichN.psiN (j + 1) M (N'' + 1) (e * (a + 1) + a + 1))
      (TeichN.psiN (j + 1) M (N'' + 1) (e + 1))) =
      J2 (ring (J₂ + 1)) j (TeichChar.psi (j + 1) (J₂ + 1) (e * (a + 1) + a + 1)) (TeichChar.psi (j + 1) (J₂ + 1) (e + 1)) := by
    rw [JprHom.j2_map (ring M) (ring (J₂ + 1)) (RedP2.red J₂ M) j (RedP2.red_add J₂ M k₂ hk₂) (RedP2.red_zero J₂ M k₂ hk₂)
      (RedP2.red_mul J₂ M k₂ hk₂)]
    exact EisBridge.j2_congr _ j _ _ _ _ (fun x => RedPN.r_psiN (j + 1) J₂ M k₂ hk₂ hM₂ hpr N'' _ x)
      (fun x => RedPN.r_psiN (j + 1) J₂ M k₂ hk₂ hM₂ hpr N'' _ x)
  -- sizes: (e+1)(a+1) + (e+1) = n(a + 2) ≤ n(ℓ − 1) = p − 1 − n ≤ p − 2
  have hb : (a + 1 + 1 + 1) * n ≤ (kk + 1 + 1) * n := Nat.mul_le_mul_right n (Nat.succ_le_succ (Nat.succ_le_succ ha))
  have hb2 : e * (a + 1) + a + 1 + (e + 1) + n ≤ j + 1 + 1 := by
    rw [hN, show e * (a + 1) + a + 1 + (e + 1) + n = (e + 1) * (a + 1 + 1) + n by lib_nat, he,
      show n * (a + 1 + 1) + n = (a + 1 + 1 + 1) * n by lib_nat]
    exact hb
  have he2 : e * (a + 1) + a + 1 + (e + 1) ≤ j + 1 :=
    Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left hn _) hb2)
  have hlt2 : e * (a + 1) + a + 1 + (e + 1) < j + 1 + 1 := Nat.lt_succ_of_le he2
  have hlta : e * (a + 1) + a + 1 < j + 1 + 1 := Nat.lt_succ_of_le (Nat.le_trans (Nat.le_add_right _ (e + 1)) he2)
  have hltb : e + 1 < j + 1 + 1 := Nat.lt_succ_of_le (Nat.le_trans (Nat.le_add_left (e + 1) _) he2)
  have hg2 : pw (ring (j + 1 + 1)) g (e * (a + 1) + a + 1 + (e + 1)) ≠ (ring (j + 1 + 1)).one :=
    CycExact.small_ne_one j g hg _ (Nat.zero_lt_succ _) hlt2
  have hg3 : pw (ring (j + 1 + 1)) g ((e * (a + 1) + a + 1 + (e + 1)) * (j + 1)) ≠ (ring (j + 1 + 1)).one := fun h3 => hg2 (
    calc pw (ring (j + 1 + 1)) g (e * (a + 1) + a + 1 + (e + 1))
        = (ring (j + 1 + 1)).mul (pw (ring (j + 1 + 1)) g ((e * (a + 1) + a + 1 + (e + 1)) * (j + 1)))
            (pw (ring (j + 1 + 1)) g (e * (a + 1) + a + 1 + (e + 1))) := by rw [h3]; lib_ring
      _ = pw (ring (j + 1 + 1)) g ((e * (a + 1) + a + 1 + (e + 1)) * (j + 1) + (e * (a + 1) + a + 1 + (e + 1))) :=
          (HasseCM.pw_add' _ _ _ _).symm
      _ = pw (ring (j + 1 + 1)) g ((j + 1 + 1) * (e * (a + 1) + a + 1 + (e + 1))) := by
          rw [show (e * (a + 1) + a + 1 + (e + 1)) * (j + 1) + (e * (a + 1) + a + 1 + (e + 1)) =
            (j + 1 + 1) * (e * (a + 1) + a + 1 + (e + 1)) by lib_nat]
      _ = pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (j + 1 + 1)) (e * (a + 1) + a + 1 + (e + 1)) := (TwoPower.pwpow _ _ _ _).symm
      _ = (ring (j + 1 + 1)).one := by rw [hg.1]; exact NthEis.pw_one _ _)
  obtain ⟨hred2, hne2⟩ := TeichChar.jac_teich2 j (J₂ + 1) hpr hM₂ (e * (a + 1) + a) e he2 g (PrimJC.g_ne_zero (j + 1) g hg)
    (CycExact.small_ne_one j g hg _ (Nat.zero_lt_succ _) hlta) (CycExact.small_ne_one j g hg _ (Nat.zero_lt_succ _) hltb) hg2 hg3
  rw [hJ]
  apply ValP.val_one (j + 1) M
  · rw [red_comp j M J₂ hM₂, hr]; exact hred2
  · intro s h
    apply hne2
    rw [← hr, h, RedP2.red_mul J₂ M k₂ hk₂, TeichRoot.map_pw (ring M) (ring (J₂ + 1)) (RedP2.red J₂ M)
      (RedP2.red_mul J₂ M k₂ hk₂) (RedP2.red_one J₂ M k₂ hk₂), r_nsm M J₂ k₂ hk₂]
    show (ring (J₂ + 1)).mul ((ring (J₂ + 1)).mul ((ring (J₂ + 1)).mul (ring (J₂ + 1)).one
      (nsm (ring (J₂ + 1)) (j + 1 + 1 + 1) (ring (J₂ + 1)).one)) (nsm (ring (J₂ + 1)) (j + 1 + 1 + 1) (ring (J₂ + 1)).one)) _ = _
    rw [show (ring (J₂ + 1)).mul (ring (J₂ + 1)).one (nsm (ring (J₂ + 1)) (j + 1 + 1 + 1) (ring (J₂ + 1)).one) =
      nsm (ring (J₂ + 1)) (j + 1 + 1 + 1) (ring (J₂ + 1)).one by lib_ring, RedP2.P_sq (j + 1) (J₂ + 1) hM₂]
    lib_ring

end ValA2

#print axioms ValA2.red_comp
#print axioms ValA2.r_nsm
#print axioms ValA2.fac_val

lang/ValA3.lean

/-
  lang/ValA3 — T49/T54 (step 8): **the primary element A ∈ ℤ[C_ℓ] has P_1-valuation exactly ℓ − 2**:
  under ι_N = φ_z : ℤ[C_ℓ] → ℤ/p^N (z = ψ_(N,n)(g), the prime P_1), with ℓ = kk + 2,
    jpr_val      ι_N ∏_(i ≤ m) J(χ^i, χ) = P^m·U, red U ≠ 0     (m ≤ ℓ − 2; ValA2.fac_val, ValP.val_mul)
    A_val        ι_N(A) = P^(ℓ−2)·U, red U ≠ 0                    (χ(−1) maps to a unit)
  With N > ℓ − 2 this is v_(P_1)(A) = ℓ − 2, i.e. v_(P_1)(G(χ)^ℓ) = ℓ − 1 — the Stickelberger exponent at P_1.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ValA2

namespace ValA3

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

variable (j M k J₂ k₂ N'' : Nat) (hk : M + 1 = (j + 1 + 1 + 1) * k) (hk₂ : M + 1 = (J₂ + 1 + 1) * k₂)
  (hM₂ : J₂ + 1 + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1))
  (hN0 : pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) (N'' + 1 + 1) = (ring M).zero)
  (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))

include hk hk₂ hM₂ hN0 hpr hg in
/-- **ι_N ∏_(i ≤ m) J(χ^i, χ) = P^m·U**. -/
theorem jpr_val (kk n e : Nat) (hN : j + 1 + 1 = (kk + 1 + 1) * n) (he : e + 1 = n) (hn : 1 ≤ n) :
    ∀ m, m ≤ kk → ∃ U, CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
        (jpr (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) m) =
        (ring M).mul (pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) m) U ∧
      RedP2.red (j + 1) M U ≠ (ring (j + 1 + 1)).zero
  | 0, _ => ⟨(ring M).one, by
      show CycEis.phz (ring M) (kk + 1) _ (gring LibInt.intR (kk + 1)).one = (ring M).mul (ring M).one (ring M).one
      rw [CycEis.phz_one]; lib_ring,
      by rw [RedP2.red_one (j + 1) M k hk]; exact PowerSum.one_ne_zero' (j + 1)⟩
  | m + 1, h => by
    have hz := ValA1.z_ellN j M k (N'' + 1) hk hN0 g hg kk n e hN he
    obtain ⟨U, hU, hU0⟩ := jpr_val kk n e hN he hn m (Nat.le_trans (Nat.le_succ m) h)
    obtain ⟨u, hu, hu0⟩ := ValA2.fac_val j M k J₂ k₂ N'' hk hk₂ hM₂ hN0 g hpr hg kk n e m hN he hn h
    obtain ⟨hprod, hred⟩ := ValP.val_mul (j + 1) M k hk hpr m 1 U u hU0 hu0
    refine ⟨(ring M).mul U u, ?_, hred⟩
    show CycEis.phz (ring M) (kk + 1) _ ((gring LibInt.intR (kk + 1)).mul (jpr (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) m)
      (J2 (gring LibInt.intR (kk + 1)) j (psp (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) (m + 1)) (CycPrim.chi (kk + 1) j g))) = _
    rw [CycEis.phz_mul (ring M) (kk + 1) _ hz, hU, hu, ← hprod]
    show _ = (ring M).mul ((ring M).mul (pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) m) U)
      ((ring M).mul ((ring M).mul (ring M).one (nsm (ring M) (j + 1 + 1 + 1) (ring M).one)) u)
    lib_ring

include hk hk₂ hM₂ hN0 hpr hg in
/-- **ι_N(A) = P^(ℓ−2)·U with U a unit modulo p**: v_(P_1)(A) = ℓ − 2. -/
theorem A_val (kk n e : Nat) (hN : j + 1 + 1 = (kk + 1 + 1) * n) (he : e + 1 = n) (hn : 1 ≤ n) :
    ∃ U, CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
        (EisBridge.AA (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) kk) =
        (ring M).mul (pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) kk) U ∧
      RedP2.red (j + 1) M U ≠ (ring (j + 1 + 1)).zero := by
  have hz := ValA1.z_ellN j M k (N'' + 1) hk hN0 g hg kk n e hN he
  obtain ⟨U, hU, hU0⟩ := jpr_val j M k J₂ k₂ N'' hk hk₂ hM₂ hN0 g hpr hg kk n e hN he hn kk (Nat.le_refl kk)
  have hy : iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) ≠ (ring (j + 1 + 1)).zero := fun h => by
    have := CharSumGen.iv_mul j hpr _ (PrimA.neg_one_ne j)
    rw [h] at this
    exact PowerSum.one_ne_zero' (j + 1) (this.symm.trans (by lib_ring))
  have hw : RedP2.red (j + 1) M (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))
      ≠ (ring (j + 1 + 1)).zero := by
    rw [TeichN.red_psiN (j + 1) M k hk hpr]
    exact DLog.pw_ne_zero (j + 1) hpr _ hy _
  refine ⟨(ring M).mul U (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))), ?_, ?_⟩
  · show CycEis.phz (ring M) (kk + 1) _ ((gring LibInt.intR (kk + 1)).mul (jpr (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) kk)
      (CycPrim.chi (kk + 1) j g (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) = _
    rw [CycEis.phz_mul (ring M) (kk + 1) _ hz, hU, ValA1.phz_chiN j M k (N'' + 1) hk hN0 g hpr hg kk e hz]
    lib_ring
  · intro h0
    rw [RedP2.red_mul (j + 1) M k hk] at h0
    cases ZMod.nozd (j + 1) hpr _ _ h0 with
    | inl h => exact hU0 h
    | inr h => exact hw h

end ValA3

#print axioms ValA3.jpr_val
#print axioms ValA3.A_val

#print axioms: 26 theorems, no axioms.

78.374 The valuations \(v_{P_c}(A)=\ell -1-c\) at every prime above \(p\)

Evaluation at \(\zeta \mapsto z^c\) modulo \(p^N\); the count of non-wrapping steps of \(a\mapsto ca \bmod \ell \) is \(\ell -1-c\), and \(A\bar A=p^{\ell -2}\).

\[ \iota _{N,c}(A)=p^{\ell -1-c}\,U_c,\quad U_c\not \equiv 0 \pmod p\quad (1\le c\le \ell -1),\qquad A\,\bar A=p^{\ell -2}. \]

lang/ValC1.lean

/-
  lang/ValC1 — T49/T54 (step 9): **Teichmüller Jacobi sums modulo p^N, every exponent pair**, towards v_(P_c)(A) for all c.
  ψ_e = TeichN.psiN (j + 1) M (N'' + 1) e on ℤ/p^N, p = j + 3:
    pw_per        x^(e + 1 + (p − 1)) = x^(e + 1) in 𝔽_p
    psiN_per      ψ_(e + 1 + (p − 1)) = ψ_(e + 1)                        (exponents of ψ only matter modulo p − 1)
    teich_val1    J(ψ_(a+1), ψ_(b+1)) = P·u, red u ≠ 0  when a + 1 + b + 1 ≤ p − 2 and g^(a+1), g^(b+1), g^(a+b+2) ≠ 1
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ValA2

namespace ValC1

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

variable (j M k J₂ k₂ N'' : Nat) (hk : M + 1 = (j + 1 + 1 + 1) * k) (hk₂ : M + 1 = (J₂ + 1 + 1) * k₂)
  (hM₂ : J₂ + 1 + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1))
  (hN0 : pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) (N'' + 1 + 1) = (ring M).zero)
  (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)

include hpr in
theorem pw_per (x : Zp (j + 1 + 1)) (e : Nat) :
    pw (ring (j + 1 + 1)) x (e + 1 + (j + 1 + 1)) = pw (ring (j + 1 + 1)) x (e + 1) := by
  rw [show e + 1 + (j + 1 + 1) = e + (j + 1 + 1 + 1) by lib_nat, HasseCM.pw_add', ZMod.fermat (j + 1) hpr x]
  rfl

include hk hN0 hpr in
/-- **exponents of ψ matter only modulo p − 1**. -/
theorem psiN_per (e : Nat) (x : Zp (j + 1 + 1)) :
    TeichN.psiN (j + 1) M (N'' + 1) (e + 1 + (j + 1 + 1)) x = TeichN.psiN (j + 1) M (N'' + 1) (e + 1) x := by
  rw [show e + 1 + (j + 1 + 1) = 1 * (e + 1 + (j + 1 + 1)) by lib_nat, ← ValA1.psiN_exp,
    ← ValA1.psiN_pw j M k (N'' + 1) hk hN0 1 x, pw_per j hpr x e, ValA1.psiN_pw j M k (N'' + 1) hk hN0 1 x,
    ValA1.psiN_exp, show 1 * (e + 1) = e + 1 by lib_nat]

include hk₂ hM₂ hpr in
/-- **J(ψ_(a+1), ψ_(b+1)) has P-valuation exactly one modulo p^N** (a + b + 2 ≤ p − 2). -/
theorem teich_val1 (a b : Nat) (he2 : a + 1 + (b + 1) ≤ j + 1) (g : Zp (j + 1 + 1)) (hg : g ≠ (ring (j + 1 + 1)).zero)
    (hga : pw (ring (j + 1 + 1)) g (a + 1) ≠ (ring (j + 1 + 1)).one)
    (hgb : pw (ring (j + 1 + 1)) g (b + 1) ≠ (ring (j + 1 + 1)).one)
    (hgab : pw (ring (j + 1 + 1)) g (a + 1 + (b + 1)) ≠ (ring (j + 1 + 1)).one)
    (hg3 : pw (ring (j + 1 + 1)) g ((a + 1 + (b + 1)) * (j + 1)) ≠ (ring (j + 1 + 1)).one) :
    ∃ u, J2 (ring M) j (TeichN.psiN (j + 1) M (N'' + 1) (a + 1)) (TeichN.psiN (j + 1) M (N'' + 1) (b + 1)) =
        (ring M).mul (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) u ∧
      RedP2.red (j + 1) M u ≠ (ring (j + 1 + 1)).zero := by
  have hr : RedP2.red J₂ M (J2 (ring M) j (TeichN.psiN (j + 1) M (N'' + 1) (a + 1)) (TeichN.psiN (j + 1) M (N'' + 1) (b + 1))) =
      J2 (ring (J₂ + 1)) j (TeichChar.psi (j + 1) (J₂ + 1) (a + 1)) (TeichChar.psi (j + 1) (J₂ + 1) (b + 1)) := by
    rw [JprHom.j2_map (ring M) (ring (J₂ + 1)) (RedP2.red J₂ M) j (RedP2.red_add J₂ M k₂ hk₂) (RedP2.red_zero J₂ M k₂ hk₂)
      (RedP2.red_mul J₂ M k₂ hk₂)]
    exact EisBridge.j2_congr _ j _ _ _ _ (fun x => RedPN.r_psiN (j + 1) J₂ M k₂ hk₂ hM₂ hpr N'' _ x)
      (fun x => RedPN.r_psiN (j + 1) J₂ M k₂ hk₂ hM₂ hpr N'' _ x)
  obtain ⟨hred2, hne2⟩ := TeichChar.jac_teich2 j (J₂ + 1) hpr hM₂ a b he2 g hg hga hgb hgab hg3
  apply ValP.val_one (j + 1) M
  · rw [ValA2.red_comp j M J₂ hM₂, hr]; exact hred2
  · intro s h
    apply hne2
    rw [← hr, h, RedP2.red_mul J₂ M k₂ hk₂, TeichRoot.map_pw (ring M) (ring (J₂ + 1)) (RedP2.red J₂ M)
      (RedP2.red_mul J₂ M k₂ hk₂) (RedP2.red_one J₂ M k₂ hk₂), ValA2.r_nsm M J₂ k₂ hk₂]
    show (ring (J₂ + 1)).mul ((ring (J₂ + 1)).mul ((ring (J₂ + 1)).mul (ring (J₂ + 1)).one
      (nsm (ring (J₂ + 1)) (j + 1 + 1 + 1) (ring (J₂ + 1)).one)) (nsm (ring (J₂ + 1)) (j + 1 + 1 + 1) (ring (J₂ + 1)).one)) _ = _
    rw [show (ring (J₂ + 1)).mul (ring (J₂ + 1)).one (nsm (ring (J₂ + 1)) (j + 1 + 1 + 1) (ring (J₂ + 1)).one) =
      nsm (ring (J₂ + 1)) (j + 1 + 1 + 1) (ring (J₂ + 1)).one by lib_ring, RedP2.P_sq (j + 1) (J₂ + 1) hM₂]
    lib_ring

end ValC1

#print axioms ValC1.pw_per
#print axioms ValC1.psiN_per
#print axioms ValC1.teich_val1

lang/ValC3.lean

/-
  lang/ValC3 — T49/T54 (step 11): **each factor of A at the prime P_c**, c = c' + 1 < ℓ = kk + 2, p − 1 = ℓ(n' + 1).
  ι_(N,c) = φ_z at z = ψ_(n c)(g) (z^ℓ = 1); for a factor J(χ^(a+1), χ) write c(a + 1) = (r' + 1) + qℓ (0 < r' + 1 < ℓ):
    add_lc        n + m = n + k ⇒ m = k                         (by hand; the core lemma is avoided)
    le_ex         a ≤ b ⇒ ∃ d, a + d = b
    psiN_perq     ψ_(e + 1 + q(p − 1)) = ψ_(e + 1)
    hz_c          z^ℓ = 1
    img_c         ι_(N,c) J(χ^(a+1), χ) = J(ψ_(n(r'+1)), ψ_(nc))
    fac_small     r' + 1 + c < ℓ  ⇒ ι_(N,c) J(χ^(a+1), χ) = P·u, red u ≠ 0
    fac_big       r' + 1 + c ≥ ℓ  ⇒ red ι_(N,c) J(χ^(a+1), χ) ≠ 0
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.ValC1
import lang.ValC2

namespace ValC3

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

theorem add_lc : ∀ n m k : Nat, n + m = n + k → m = k
  | 0, m, k, h => calc m = 0 + m := by lib_nat
      _ = 0 + k := h
      _ = k := by lib_nat
  | n + 1, m, k, h => add_lc n m k (Nat.succ.inj (calc n + m + 1 = n + 1 + m := by lib_nat
      _ = n + 1 + k := h
      _ = n + k + 1 := by lib_nat))

theorem le_ex {a b : Nat} (h : a ≤ b) : ∃ d, a + d = b := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih =>
    obtain ⟨d, hd⟩ := ih
    exact ⟨d + 1, congrArg Nat.succ hd⟩

variable (j M k J₂ k₂ N'' : Nat) (hk : M + 1 = (j + 1 + 1 + 1) * k) (hk₂ : M + 1 = (J₂ + 1 + 1) * k₂)
  (hM₂ : J₂ + 1 + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1))
  (hN0 : pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) (N'' + 1 + 1) = (ring M).zero)
  (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)

include hk hN0 hpr in
theorem psiN_perq (e : Nat) (x : Zp (j + 1 + 1)) : ∀ q,
    TeichN.psiN (j + 1) M (N'' + 1) (e + 1 + q * (j + 1 + 1)) x = TeichN.psiN (j + 1) M (N'' + 1) (e + 1) x
  | 0 => by rw [show e + 1 + 0 * (j + 1 + 1) = e + 1 by lib_nat]
  | q + 1 => by
    rw [show e + 1 + (q + 1) * (j + 1 + 1) = (e + q * (j + 1 + 1)) + 1 + (j + 1 + 1) by lib_nat,
      ValC1.psiN_per j M k N'' hk hN0 hpr (e + q * (j + 1 + 1)) x,
      show e + q * (j + 1 + 1) + 1 = e + 1 + q * (j + 1 + 1) by lib_nat]
    exact psiN_perq e x q

variable (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))

include hk hN0 hg in
/-- **z = ψ_(nc)(g) is an ℓ-th root of unity**. -/
theorem hz_c (kk n' c' e : Nat) (hN : j + 1 + 1 = (kk + 1 + 1) * (n' + 1)) (he : e + 1 = (n' + 1) * (c' + 1)) :
    pw (ring M) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g) (kk + 1 + 1) = (ring M).one := by
  rw [ValA1.psiN_exp, show (e + 1) * (kk + 1 + 1) = 1 * ((j + 1 + 1) * (c' + 1)) by rw [he, hN]; lib_nat,
    ← ValA1.psiN_exp, ← TwoPower.pwpow, ← ValA1.psiN_pw j M k (N'' + 1) hk hN0 1 g (j + 1 + 1), hg.1, ValA1.psiN_one]
  exact NthEis.pw_one _ _

include hk hN0 hpr hg in
/-- **ι_(N,c) J(χ^(a+1), χ) = J(ψ_(n(r'+1)), ψ_(nc))**. -/
theorem img_c (kk n' c' e a r' q : Nat) (hN : j + 1 + 1 = (kk + 1 + 1) * (n' + 1)) (he : e + 1 = (n' + 1) * (c' + 1))
    (hr : (c' + 1) * (a + 1) = r' + 1 + q * (kk + 1 + 1)) :
    CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
        (J2 (gring LibInt.intR (kk + 1)) j (psp (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) (a + 1))
          (CycPrim.chi (kk + 1) j g)) =
      J2 (ring M) j (TeichN.psiN (j + 1) M (N'' + 1) (n' * (r' + 1) + r' + 1))
        (TeichN.psiN (j + 1) M (N'' + 1) (n' * (c' + 1) + c' + 1)) := by
  have hz := hz_c j M k N'' hk hN0 g hg kk n' c' e hN he
  rw [CycAP1.j2_chi1 kk j g (a + 1), ValA1.jac_imageN j M k (N'' + 1) hk hN0 g hpr hg kk e (a + 1) 1 hz]
  have h1 : (e + 1) * (a + 1) = n' * (r' + 1) + r' + 1 + q * (j + 1 + 1) := by
    rw [he, hN, show (n' + 1) * (c' + 1) * (a + 1) = (n' + 1) * ((c' + 1) * (a + 1)) by lib_nat, hr]
    lib_nat
  have h2 : (e + 1) * 1 = n' * (c' + 1) + c' + 1 := by rw [he]; lib_nat
  rw [h1, h2]
  exact EisBridge.j2_congr _ j _ _ _ _ (fun x => psiN_perq j M k N'' hk hN0 hpr (n' * (r' + 1) + r') x q) (fun _ => rfl)

include hk hk₂ hM₂ hN0 hpr hg in
/-- **a factor with r' + 1 + c < ℓ has valuation one at P_c**. -/
theorem fac_small (kk n' c' e a r' q : Nat) (hN : j + 1 + 1 = (kk + 1 + 1) * (n' + 1)) (he : e + 1 = (n' + 1) * (c' + 1))
    (hr : (c' + 1) * (a + 1) = r' + 1 + q * (kk + 1 + 1)) (hs : r' + 1 + (c' + 1) + 1 ≤ kk + 1 + 1) :
    ∃ u, CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
        (J2 (gring LibInt.intR (kk + 1)) j (psp (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) (a + 1))
          (CycPrim.chi (kk + 1) j g)) = (ring M).mul (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) u ∧
      RedP2.red (j + 1) M u ≠ (ring (j + 1 + 1)).zero := by
  rw [img_c j M k N'' hk hN0 hpr g hg kk n' c' e a r' q hN he hr]
  -- sizes: (n'+1)(r'+1+c'+1) + (n'+1) ≤ (n'+1)(kk+2) = p − 1
  have hb : (r' + 1 + (c' + 1) + 1) * (n' + 1) ≤ (kk + 1 + 1) * (n' + 1) := Nat.mul_le_mul_right (n' + 1) hs
  have hb2 : n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1) + 1 ≤ j + 1 + 1 := by
    rw [hN]
    exact Nat.le_trans (by
      rw [show (r' + 1 + (c' + 1) + 1) * (n' + 1) = n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1) + 1 + n' by lib_nat]
      exact Nat.le_add_right _ n') hb
  have he2 : n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1) ≤ j + 1 := Nat.le_of_succ_le_succ hb2
  have hlt2 : n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1) < j + 1 + 1 := Nat.lt_succ_of_le he2
  have hlta : n' * (r' + 1) + r' + 1 < j + 1 + 1 := Nat.lt_succ_of_le (Nat.le_trans (Nat.le_add_right _ _) he2)
  have hltb : n' * (c' + 1) + c' + 1 < j + 1 + 1 := Nat.lt_succ_of_le (Nat.le_trans (Nat.le_add_left _ _) he2)
  have hg2 := CycExact.small_ne_one j g hg _ (Nat.zero_lt_succ _) hlt2
  have hg3 : pw (ring (j + 1 + 1)) g ((n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1)) * (j + 1)) ≠ (ring (j + 1 + 1)).one :=
    fun h3 => hg2 (
    calc pw (ring (j + 1 + 1)) g (n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1))
        = (ring (j + 1 + 1)).mul (pw (ring (j + 1 + 1)) g ((n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1)) * (j + 1)))
            (pw (ring (j + 1 + 1)) g (n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1))) := by rw [h3]; lib_ring
      _ = pw (ring (j + 1 + 1)) g ((n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1)) * (j + 1) +
            (n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1))) := (HasseCM.pw_add' _ _ _ _).symm
      _ = pw (ring (j + 1 + 1)) g ((j + 1 + 1) * (n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1))) := by
          rw [show (n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1)) * (j + 1) +
            (n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1)) =
            (j + 1 + 1) * (n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1)) by lib_nat]
      _ = pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (j + 1 + 1)) (n' * (r' + 1) + r' + 1 + (n' * (c' + 1) + c' + 1)) :=
          (TwoPower.pwpow _ _ _ _).symm
      _ = (ring (j + 1 + 1)).one := by rw [hg.1]; exact NthEis.pw_one _ _)
  exact ValC1.teich_val1 j M J₂ k₂ N'' hk₂ hM₂ hpr (n' * (r' + 1) + r') (n' * (c' + 1) + c') he2 g
    (PrimJC.g_ne_zero (j + 1) g hg) (CycExact.small_ne_one j g hg _ (Nat.zero_lt_succ _) hlta)
    (CycExact.small_ne_one j g hg _ (Nat.zero_lt_succ _) hltb) hg2 hg3

include hk hN0 hpr hg in
/-- **a factor with r' + 1 + c ≥ ℓ is a unit at P_c**. -/
theorem fac_big (kk n' c' e a r' q : Nat) (hN : j + 1 + 1 = (kk + 1 + 1) * (n' + 1)) (he : e + 1 = (n' + 1) * (c' + 1))
    (hr : (c' + 1) * (a + 1) = r' + 1 + q * (kk + 1 + 1)) (hrl : r' + 1 + 1 ≤ kk + 1 + 1) (hcl : c' + 1 + 1 ≤ kk + 1 + 1)
    (hbg : kk + 1 + 1 ≤ r' + 1 + (c' + 1)) :
    RedP2.red (j + 1) M (CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
        (J2 (gring LibInt.intR (kk + 1)) j (psp (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) (a + 1))
          (CycPrim.chi (kk + 1) j g))) ≠ (ring (j + 1 + 1)).zero := by
  rw [img_c j M k N'' hk hN0 hpr g hg kk n' c' e a r' q hN he hr]
  obtain ⟨κ, hκ⟩ := le_ex (Nat.le_of_succ_le hrl)
  obtain ⟨μ, hμ⟩ := le_ex (show r' + 1 + κ ≤ r' + 1 + (c' + 1) by rw [hκ]; exact hbg)
  rw [show n' * (r' + 1) + r' + 1 = (r' + 1) * (n' + 1) by lib_nat, show n' * (c' + 1) + c' + 1 = (κ + μ) * (n' + 1) by
    rw [show κ + μ = c' + 1 from add_lc (r' + 1) (κ + μ) (c' + 1) (by rw [← hμ]; lib_nat)]; lib_nat]
  exact ValC2.teich_unit j M k (N'' + 1) hk hpr (r' + 1) κ μ (n' + 1) (hN.trans (by rw [hκ]))
    (Nat.succ_le_succ (Nat.zero_le r')) (by rw [show κ + μ = c' + 1 from add_lc (r' + 1) (κ + μ) (c' + 1) (by rw [← hμ]; lib_nat), hκ]; exact hcl)
    (Nat.succ_le_succ (Nat.zero_le n'))

end ValC3

#print axioms ValC3.add_lc
#print axioms ValC3.le_ex
#print axioms ValC3.psiN_perq
#print axioms ValC3.hz_c
#print axioms ValC3.img_c
#print axioms ValC3.fac_small
#print axioms ValC3.fac_big

lang/ValC4.lean

/-
  lang/ValC4 — T49/T54 (step 12): **v_(P_c)(A) for every prime P_c**, c = c' + 1 < ℓ = kk + 2, p − 1 = ℓ(n' + 1):
  ℓ prime, ⟨x⟩ = rm ℓ x (remainder by recursion), cnt m = #{a < m : ⟨c(a + 1)⟩ + c < ℓ}:
    rm_spec      x = rm x + qm x · ℓ, rm x < ℓ
    dv_small     ℓ ∤ m for 0 < m < ℓ
    jprc_val     ι_(N,c) ∏_(i ≤ m) J(χ^i, χ) = P^(cnt m)·U, red U ≠ 0
    A_valc       ι_(N,c)(A) = P^(cnt (ℓ − 2))·U, red U ≠ 0             (v_(P_c)(A) = cnt (ℓ − 2))
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ValC3
import lang.ValA3

namespace ValC4

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen
open FiniteRecChain (ite_pos' ite_neg')

/-- remainder and quotient by L, by recursion (no subtraction): x = rm x + qm x · L, rm x < L. -/
def rm (L : Nat) : Nat → Nat
  | 0 => 0
  | x + 1 => if rm L x + 1 + 1 ≤ L then rm L x + 1 else 0

def qm (L : Nat) : Nat → Nat
  | 0 => 0
  | x + 1 => if rm L x + 1 + 1 ≤ L then qm L x else qm L x + 1

theorem rm_spec (L : Nat) (hL : 1 ≤ L) : ∀ x, x = rm L x + qm L x * L ∧ rm L x + 1 ≤ L
  | 0 => ⟨by show 0 = 0 + 0 * L; lib_nat, hL⟩
  | x + 1 => by
    obtain ⟨h1, h2⟩ := rm_spec L hL x
    by_cases hs : rm L x + 1 + 1 ≤ L
    · show x + 1 = (if rm L x + 1 + 1 ≤ L then rm L x + 1 else 0) + (if rm L x + 1 + 1 ≤ L then qm L x else qm L x + 1) * L ∧
        (if rm L x + 1 + 1 ≤ L then rm L x + 1 else 0) + 1 ≤ L
      rw [ite_pos' _ _ hs, ite_pos' _ _ hs]
      exact ⟨(congrArg (· + 1) h1).trans (by lib_nat), hs⟩
    · have hge : L ≤ rm L x + 1 := match Nat.lt_or_ge (rm L x + 1) L with
        | Or.inl h => absurd h hs
        | Or.inr h => h
      have heq : rm L x + 1 = L := Nat.le_antisymm h2 hge
      show x + 1 = (if rm L x + 1 + 1 ≤ L then rm L x + 1 else 0) + (if rm L x + 1 + 1 ≤ L then qm L x else qm L x + 1) * L ∧
        (if rm L x + 1 + 1 ≤ L then rm L x + 1 else 0) + 1 ≤ L
      rw [ite_neg' _ _ hs, ite_neg' _ _ hs]
      refine ⟨?_, hL⟩
      calc x + 1 = rm L x + qm L x * L + 1 := congrArg (· + 1) h1
        _ = (rm L x + 1) + qm L x * L := by lib_nat
        _ = L + qm L x * L := by rw [heq]
        _ = 0 + (qm L x + 1) * L := by lib_nat

theorem dv_small (L m : Nat) (h0 : 1 ≤ m) (hm : m + 1 ≤ L) (hd : Dv L m) : False := by
  obtain ⟨q, hq⟩ := hd
  cases q with
  | zero =>
    have h : m = 0 := hq.trans (by lib_nat)
    rw [h] at h0
    exact Nat.not_succ_le_zero 0 h0
  | succ q =>
    have h : L ≤ m := by
      rw [hq, show (q + 1) * L = q * L + L by lib_nat]
      exact Nat.le_add_left L (q * L)
    exact Nat.lt_irrefl m (Nat.le_trans hm h)

/-- cnt m = #{a < m : ⟨c(a + 1)⟩ + c < L}. -/
def cnt (L c' : Nat) : Nat → Nat
  | 0 => 0
  | m + 1 => cnt L c' m + (if rm L ((c' + 1) * (m + 1)) + (c' + 1) + 1 ≤ L then 1 else 0)

variable (j M k J₂ k₂ N'' : Nat) (hk : M + 1 = (j + 1 + 1 + 1) * k) (hk₂ : M + 1 = (J₂ + 1 + 1) * k₂)
  (hM₂ : J₂ + 1 + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1))
  (hN0 : pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) (N'' + 1 + 1) = (ring M).zero)
  (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))

include hk hk₂ hM₂ hN0 hpr hg in
/-- **ι_(N,c) ∏_(i ≤ m) J(χ^i, χ) = P^(cnt m)·U**. -/
theorem jprc_val (kk n' c' e : Nat) (hN : j + 1 + 1 = (kk + 1 + 1) * (n' + 1)) (he : e + 1 = (n' + 1) * (c' + 1))
    (hcl : c' + 1 + 1 ≤ kk + 1 + 1) (hl : ∀ d, Dv d (kk + 1 + 1) → d = 1 ∨ d = kk + 1 + 1) :
    ∀ m, m ≤ kk → ∃ U, CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
        (jpr (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) m) =
        (ring M).mul (pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) (cnt (kk + 1 + 1) c' m)) U ∧
      RedP2.red (j + 1) M U ≠ (ring (j + 1 + 1)).zero
  | 0, _ => ⟨(ring M).one, by
      show CycEis.phz (ring M) (kk + 1) _ (gring LibInt.intR (kk + 1)).one = (ring M).mul (ring M).one (ring M).one
      rw [CycEis.phz_one]; lib_ring,
      by rw [RedP2.red_one (j + 1) M k hk]; exact PowerSum.one_ne_zero' (j + 1)⟩
  | m + 1, hm => by
    have hz := ValC3.hz_c j M k N'' hk hN0 g hg kk n' c' e hN he
    obtain ⟨U, hU, hU0⟩ := jprc_val kk n' c' e hN he hcl hl m (Nat.le_trans (Nat.le_succ m) hm)
    obtain ⟨hX, hXl⟩ := rm_spec (kk + 1 + 1) (Nat.succ_le_succ (Nat.zero_le _)) ((c' + 1) * (m + 1))
    obtain ⟨r', hr'⟩ : ∃ r', rm (kk + 1 + 1) ((c' + 1) * (m + 1)) = r' + 1 := by
      cases h : rm (kk + 1 + 1) ((c' + 1) * (m + 1)) with
      | zero =>
        have hd : Dv (kk + 1 + 1) ((c' + 1) * (m + 1)) := ⟨qm (kk + 1 + 1) ((c' + 1) * (m + 1)), by
          rw [h] at hX; exact hX.trans (by lib_nat)⟩
        cases euclid_core kk c' (m + 1) hl hd with
        | inl h1 => exact (dv_small (kk + 1 + 1) (c' + 1) (Nat.succ_le_succ (Nat.zero_le c')) hcl h1).elim
        | inr h1 => exact (dv_small (kk + 1 + 1) (m + 1) (Nat.succ_le_succ (Nat.zero_le m))
            (Nat.le_trans (Nat.succ_le_succ hm) (Nat.le_succ _)) h1).elim
      | succ r' => exact ⟨r', rfl⟩
    have hr : (c' + 1) * (m + 1) = r' + 1 + qm (kk + 1 + 1) ((c' + 1) * (m + 1)) * (kk + 1 + 1) := by
      rw [← hr']; exact hX
    have hrl : r' + 1 + 1 ≤ kk + 1 + 1 := by rw [← hr']; exact hXl
    have hstep : CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
        (jpr (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) (m + 1)) =
        (ring M).mul (CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
          (jpr (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) m))
          (CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
            (J2 (gring LibInt.intR (kk + 1)) j (psp (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) (m + 1))
              (CycPrim.chi (kk + 1) j g))) :=
      CycEis.phz_mul (ring M) (kk + 1) _ hz _ _
    by_cases hs : rm (kk + 1 + 1) ((c' + 1) * (m + 1)) + (c' + 1) + 1 ≤ kk + 1 + 1
    · have hs' : r' + 1 + (c' + 1) + 1 ≤ kk + 1 + 1 := by rw [← hr']; exact hs
      obtain ⟨u, hu, hu0⟩ := ValC3.fac_small j M k J₂ k₂ N'' hk hk₂ hM₂ hN0 hpr g hg kk n' c' e m r' _ hN he hr hs'
      obtain ⟨hprod, hred⟩ := ValP.val_mul (j + 1) M k hk hpr (cnt (kk + 1 + 1) c' m) 1 U u hU0 hu0
      refine ⟨(ring M).mul U u, ?_, hred⟩
      rw [show cnt (kk + 1 + 1) c' (m + 1) = cnt (kk + 1 + 1) c' m + 1 from
        congrArg (cnt (kk + 1 + 1) c' m + ·) (ite_pos' _ _ hs), hstep, hU, hu, ← hprod]
      show _ = (ring M).mul ((ring M).mul (pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) _) U)
        ((ring M).mul ((ring M).mul (ring M).one (nsm (ring M) (j + 1 + 1 + 1) (ring M).one)) u)
      lib_ring
    · have hbg : kk + 1 + 1 ≤ r' + 1 + (c' + 1) := match Nat.lt_or_ge (r' + 1 + (c' + 1)) (kk + 1 + 1) with
        | Or.inl h => absurd (show rm (kk + 1 + 1) ((c' + 1) * (m + 1)) + (c' + 1) + 1 ≤ kk + 1 + 1 by rw [hr']; exact h) hs
        | Or.inr h => h
      have hw := ValC3.fac_big j M k N'' hk hN0 hpr g hg kk n' c' e m r' _ hN he hr hrl hcl hbg
      obtain ⟨hprod, hred⟩ := ValP.val_mul (j + 1) M k hk hpr (cnt (kk + 1 + 1) c' m) 0 U _ hU0 hw
      refine ⟨_, ?_, hred⟩
      rw [show cnt (kk + 1 + 1) c' (m + 1) = cnt (kk + 1 + 1) c' m + 0 from
        congrArg (cnt (kk + 1 + 1) c' m + ·) (ite_neg' _ _ hs), hstep, hU, ← hprod]
      show _ = (ring M).mul ((ring M).mul (pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) _) U)
        ((ring M).mul (ring M).one _)
      lib_ring

include hk hk₂ hM₂ hN0 hpr hg in
/-- **ι_(N,c)(A) = P^(cnt (ℓ − 2))·U with U a unit modulo p**: v_(P_c)(A) = #{a ≤ ℓ − 2 : ⟨ca⟩ + c < ℓ}. -/
theorem A_valc (kk n' c' e : Nat) (hN : j + 1 + 1 = (kk + 1 + 1) * (n' + 1)) (he : e + 1 = (n' + 1) * (c' + 1))
    (hcl : c' + 1 + 1 ≤ kk + 1 + 1) (hl : ∀ d, Dv d (kk + 1 + 1) → d = 1 ∨ d = kk + 1 + 1) :
    ∃ U, CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
        (EisBridge.AA (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) kk) =
        (ring M).mul (pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) (cnt (kk + 1 + 1) c' kk)) U ∧
      RedP2.red (j + 1) M U ≠ (ring (j + 1 + 1)).zero := by
  have hz := ValC3.hz_c j M k N'' hk hN0 g hg kk n' c' e hN he
  obtain ⟨U, hU, hU0⟩ := jprc_val j M k J₂ k₂ N'' hk hk₂ hM₂ hN0 hpr g hg kk n' c' e hN he hcl hl kk (Nat.le_refl kk)
  have hy : iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) ≠ (ring (j + 1 + 1)).zero := fun h => by
    have := CharSumGen.iv_mul j hpr _ (PrimA.neg_one_ne j)
    rw [h] at this
    exact PowerSum.one_ne_zero' (j + 1) (this.symm.trans (by lib_ring))
  have hw : RedP2.red (j + 1) M (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))
      ≠ (ring (j + 1 + 1)).zero := by
    rw [TeichN.red_psiN (j + 1) M k hk hpr]
    exact DLog.pw_ne_zero (j + 1) hpr _ hy _
  refine ⟨(ring M).mul U (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))), ?_, ?_⟩
  · show CycEis.phz (ring M) (kk + 1) _ ((gring LibInt.intR (kk + 1)).mul (jpr (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) kk)
      (CycPrim.chi (kk + 1) j g (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) = _
    rw [CycEis.phz_mul (ring M) (kk + 1) _ hz, hU, ValA1.phz_chiN j M k (N'' + 1) hk hN0 g hpr hg kk e hz]
    lib_ring
  · intro h0
    rw [RedP2.red_mul (j + 1) M k hk] at h0
    cases ZMod.nozd (j + 1) hpr _ _ h0 with
    | inl h => exact hU0 h
    | inr h => exact hw h

end ValC4

#print axioms ValC4.rm_spec
#print axioms ValC4.dv_small
#print axioms ValC4.jprc_val
#print axioms ValC4.A_valc

lang/ValC5.lean

/-
  lang/ValC5 — T49/T54 (step 13): **v_(P_c)(A) = ℓ − 1 − c** (Stickelberger's exponents for the primary element).
  The quotient qm counts the wraps of a ↦ ⟨ca⟩, and cnt counts the steps without a wrap:
    rq_uniq       x = r + qL, r < L ⇒ rm x = r, qm x = q
    cnt_qm        cnt m + qm(c(m + 1)) = m
    cnt_closed    cnt (ℓ − 2) + (c − 1) = ℓ − 2
    A_val_all     ι_(N,c)(A) = P^v·U, red U ≠ 0, v + (c − 1) = ℓ − 2       (ℓ prime, 1 ≤ c ≤ ℓ − 1)
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.ValC4
import lang.Fp2Count

namespace ValC5

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen
open FiniteRecChain (ite_pos' ite_neg')
open ValC4 (rm qm cnt rm_spec)

theorem rq_uniq (L x r q : Nat) (hL : 1 ≤ L) (hx : x = r + q * L) (hr : r < L) : rm L x = r ∧ qm L x = q := by
  obtain ⟨h1, h2⟩ := rm_spec L hL x
  obtain ⟨hq, hrr⟩ := Fp2Count.divmod_unique L (qm L x) (rm L x) q r h2 hr (calc
    qm L x * L + rm L x = rm L x + qm L x * L := by lib_nat
    _ = x := h1.symm
    _ = r + q * L := hx
    _ = q * L + r := by lib_nat)
  exact ⟨hrr, hq⟩

variable (L c' : Nat) (hc : c' + 1 + 1 ≤ L)

include hc in
/-- **non-wraps plus wraps**: cnt m + qm(c(m + 1)) = m. -/
theorem cnt_qm : ∀ m, cnt L c' m + qm L ((c' + 1) * (m + 1)) = m
  | 0 => by
    have hL : 1 ≤ L := Nat.le_trans (Nat.succ_le_succ (Nat.zero_le _)) hc
    rw [(rq_uniq L ((c' + 1) * (0 + 1)) (c' + 1) 0 hL (by lib_nat) hc).2]
    rfl
  | m + 1 => by
    have hL : 1 ≤ L := Nat.le_trans (Nat.succ_le_succ (Nat.zero_le _)) hc
    have ih := cnt_qm m
    obtain ⟨hX, hXl⟩ := rm_spec L hL ((c' + 1) * (m + 1))
    have hY : (c' + 1) * (m + 1 + 1) =
        rm L ((c' + 1) * (m + 1)) + (c' + 1) + qm L ((c' + 1) * (m + 1)) * L :=
      calc (c' + 1) * (m + 1 + 1) = (c' + 1) * (m + 1) + (c' + 1) := by lib_nat
        _ = rm L ((c' + 1) * (m + 1)) + qm L ((c' + 1) * (m + 1)) * L + (c' + 1) := congrArg (· + (c' + 1)) hX
        _ = _ := by lib_nat
    show cnt L c' m + (if rm L ((c' + 1) * (m + 1)) + (c' + 1) + 1 ≤ L then 1 else 0) + qm L ((c' + 1) * (m + 1 + 1)) = m + 1
    by_cases hs : rm L ((c' + 1) * (m + 1)) + (c' + 1) + 1 ≤ L
    · rw [ite_pos' _ _ hs, (rq_uniq L _ _ _ hL hY hs).2]
      exact (show cnt L c' m + 1 + qm L ((c' + 1) * (m + 1)) = cnt L c' m + qm L ((c' + 1) * (m + 1)) + 1 by lib_nat).trans
        (congrArg (· + 1) ih)
    · have hge : L ≤ rm L ((c' + 1) * (m + 1)) + (c' + 1) := match Nat.lt_or_ge (rm L ((c' + 1) * (m + 1)) + (c' + 1)) L with
        | Or.inl h => absurd h hs
        | Or.inr h => h
      obtain ⟨s, hsd⟩ := ValC3.le_ex hge
      have hsl : s < L := match Nat.lt_or_ge s L with
        | Or.inl h => h
        | Or.inr h => (Nat.lt_irrefl (L + L) (Nat.le_trans (Nat.le_succ _)
            (calc L + L + 1 + 1 ≤ L + s + 1 + 1 := Nat.succ_le_succ (Nat.succ_le_succ (Nat.add_le_add_left h L))
              _ = rm L ((c' + 1) * (m + 1)) + 1 + (c' + 1 + 1) := by rw [hsd]; lib_nat
              _ ≤ L + L := Nat.add_le_add hXl hc))).elim
      have hY2 : (c' + 1) * (m + 1 + 1) = s + (qm L ((c' + 1) * (m + 1)) + 1) * L := by
        rw [hY, ← hsd]; lib_nat
      rw [ite_neg' _ _ hs, (rq_uniq L _ _ _ hL hY2 hsl).2]
      exact (show cnt L c' m + 0 + (qm L ((c' + 1) * (m + 1)) + 1) = cnt L c' m + qm L ((c' + 1) * (m + 1)) + 1 by lib_nat).trans
        (congrArg (· + 1) ih)

include hc in
/-- **cnt (ℓ − 2) + (c − 1) = ℓ − 2**, i.e. v_(P_c)(A) = ℓ − 1 − c. -/
theorem cnt_closed (kk : Nat) (hL : L = kk + 1 + 1) : cnt L c' kk + c' = kk := by
  have hq := cnt_qm L c' hc kk
  have hc2 : c' ≤ kk := Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ (show c' + 1 + 1 ≤ kk + 1 + 1 by rw [← hL]; exact hc))
  obtain ⟨d, hd⟩ := ValC3.le_ex hc2
  have hL1 : 1 ≤ L := Nat.le_trans (Nat.succ_le_succ (Nat.zero_le _)) hc
  have hx : (c' + 1) * (kk + 1) = (d + 1) + c' * L := by
    rw [hL, ← hd]; lib_nat
  have hdl : d + 1 < L := by
    rw [hL]
    exact Nat.succ_lt_succ (Nat.lt_succ_of_le (show d ≤ kk by rw [← hd]; exact Nat.le_add_left d c'))
  rw [(rq_uniq L _ _ _ hL1 hx hdl).2] at hq
  exact hq

variable (j M k J₂ k₂ N'' : Nat) (hk : M + 1 = (j + 1 + 1 + 1) * k) (hk₂ : M + 1 = (J₂ + 1 + 1) * k₂)
  (hM₂ : J₂ + 1 + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1))
  (hN0 : pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) (N'' + 1 + 1) = (ring M).zero)
  (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))

include hk hk₂ hM₂ hN0 hpr hg in
/-- **v_(P_c)(A) = ℓ − 1 − c for every prime P_c** (ℓ = kk + 2 prime, c = c' + 1 ≤ ℓ − 1). -/
theorem A_val_all (kk n' c' e : Nat) (hN : j + 1 + 1 = (kk + 1 + 1) * (n' + 1)) (he : e + 1 = (n' + 1) * (c' + 1))
    (hcl : c' + 1 + 1 ≤ kk + 1 + 1) (hl : ∀ d, Dv d (kk + 1 + 1) → d = 1 ∨ d = kk + 1 + 1) :
    ∃ v U, v + c' = kk ∧ CycEis.phz (ring M) (kk + 1) (TeichN.psiN (j + 1) M (N'' + 1) (e + 1) g)
        (EisBridge.AA (gring LibInt.intR (kk + 1)) j (CycPrim.chi (kk + 1) j g) kk) =
        (ring M).mul (pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) v) U ∧
      RedP2.red (j + 1) M U ≠ (ring (j + 1 + 1)).zero := by
  obtain ⟨U, hU, hU0⟩ := ValC4.A_valc j M k J₂ k₂ N'' hk hk₂ hM₂ hN0 hpr g hg kk n' c' e hN he hcl hl
  exact ⟨_, U, cnt_closed (kk + 1 + 1) c' hcl kk rfl, hU, hU0⟩

end ValC5

#print axioms ValC5.rq_uniq
#print axioms ValC5.cnt_qm
#print axioms ValC5.cnt_closed
#print axioms ValC5.A_val_all

lang/CycAbar.lean

/-
  lang/CycAbar — T49/T54 (every prime ℓ = k + 2 ≥ 3): **A·Ā = p^(ℓ−2) in ℤ[ζ_ℓ] = ℤ[C_ℓ]/(N)**, so the primary element A
  has no prime factors outside p; with the valuations v_(P_c)(A) = ℓ − 1 − c (ValC5) this is its full factorisation.
  ψ = u^dlog (CycChar.psi, u = [σ]), ψ̄ = ψ∘ι:
    pair_norm    J(ψ^(a+1), ψ)·J(ψ̄^(a+1), ψ̄) = p               (a + 2 < ℓ; JacNormPair.jac_norm2 in the quotient)
    jpr_norm     ∏_(i ≤ m) J(ψ^i, ψ) · ∏_(i ≤ m) J(ψ̄^i, ψ̄) = p^m
    A_norm       A(ψ)·A(ψ̄) = p^(ℓ−2)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycNorm
import lang.JacNormPair
import lang.EisBridge
import lang.PrimA

namespace CycAbar

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

variable (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
  (k t : Nat) (hℓ : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1) (hN : j + 1 + 1 = (k + 1 + 1) * (t + 1))

theorem rearr {R : Type} (T : CRing R) (X a Y b : R) : T.mul (T.mul X a) (T.mul Y b) = T.mul (T.mul X Y) (T.mul a b) := by
  lib_ring

include hN in
theorem hu_ell : pw (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (j + 1 + 1) = (CycRing.cyc LibInt.intR (k + 1)).one := by
  rw [hN, ← TwoPower.pwpow, CycUnit.u_ell]
  exact NthEis.pw_one _ _

include hN in
theorem hw_u (c : Nat) : pw (CycRing.cyc LibInt.intR (k + 1)) (pw (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) c) (j + 1 + 1) =
    (CycRing.cyc LibInt.intR (k + 1)).one := by
  rw [TwoPower.pwpow, show c * (j + 1 + 1) = (k + 1 + 1) * ((t + 1) * c) by rw [hN]; lib_nat, ← TwoPower.pwpow, CycUnit.u_ell]
  exact NthEis.pw_one _ _

include hpr hg hℓ hN in
/-- **J(ψ^(a+1), ψ)·J(ψ̄^(a+1), ψ̄) = p**. -/
theorem pair_norm (a : Nat) (ha : a + 1 + 1 < k + 1 + 1) :
    (CycRing.cyc LibInt.intR (k + 1)).mul
        (J2 (CycRing.cyc LibInt.intR (k + 1)) j
          (psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) (a + 1))
          (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))))
        (J2 (CycRing.cyc LibInt.intR (k + 1)) j
          (fun x => psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) (a + 1) (iv j x))
          (fun x => CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (iv j x))) =
      Pp (CycRing.cyc LibInt.intR (k + 1)) j := by
  have hχ : ∀ x, psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) (a + 1) x =
      CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (pw (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (a + 1)) x :=
    fun x => CycChar.psi_pow (j + 1) g _ _ x a
  have hν : ∀ x, (CycRing.cyc LibInt.intR (k + 1)).mul
      (psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) (a + 1) x)
      (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) x) =
      CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (pw (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (a + 1 + 1)) x :=
    fun x => CycChar.psi_pow (j + 1) g _ _ x (a + 1)
  have hu1 : CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) =
      CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (pw (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (0 + 1)) := by
    rw [show pw (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (0 + 1) = CycUnit.u LibInt.intR (k + 1) by
      show (CycRing.cyc LibInt.intR (k + 1)).mul (CycRing.cyc LibInt.intR (k + 1)).one (CycUnit.u LibInt.intR (k + 1)) = _; lib_ring]
  have hs : ∀ c, c + 1 < k + 1 + 1 → sumK (CycRing.cyc LibInt.intR (k + 1))
      (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (pw (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (c + 1))) (LL j) =
      (CycRing.cyc LibInt.intR (k + 1)).zero := fun c hc =>
    CycChar.sum_psi (j + 1) g _ hpr hg _ (k + 1 + 1) (t + 1) hN (CycUnit.gs_u LibInt.intR k hℓ (c + 1) (CycFq.small_ne k c hc))
  have ha1 : a + 1 < k + 1 + 1 := Nat.lt_trans (Nat.lt_succ_self _) ha
  have hsν : sumK (CycRing.cyc LibInt.intR (k + 1)) (fun x => (CycRing.cyc LibInt.intR (k + 1)).mul
      (psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) (a + 1) x)
      (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) x)) (LL j) =
      (CycRing.cyc LibInt.intR (k + 1)).zero :=
    (sumK_ext _ _ _ hν (LL j)).trans (hs (a + 1) ha)
  refine CharSumGen.jac_norm2 (CycRing.cyc LibInt.intR (k + 1)) j hpr _ _
    (fun x y => by rw [hχ, hχ, hχ]; exact CycChar.psi_mul (j + 1) g _ hpr hg _ (hw_u j k t hN (a + 1)) x y)
    (by rw [hχ]; exact CycChar.psi_one (j + 1) g _ hpr hg _)
    (by rw [hχ]; exact CycChar.psi_zero (j + 1) g _ _)
    ((sumK_ext _ _ _ hχ (LL j)).trans (hs a ha1))
    (CycChar.psi_mul (j + 1) g _ hpr hg _ (hu_ell j k t hN))
    (CycChar.psi_one (j + 1) g _ hpr hg _) (CycChar.psi_zero (j + 1) g _ _)
    (by rw [hu1]; exact hs 0 (Nat.succ_lt_succ (Nat.zero_lt_succ k)))
    hsν
    ((reindex (CycRing.cyc LibInt.intR (k + 1)) (LL j) (CMInert.complete_allZ (j + 1 + 1) _) (iv j) (iv j)
      (CharSumGen.iv_iv j hpr) (CharSumGen.iv_iv j hpr) (fun x => (CycRing.cyc LibInt.intR (k + 1)).mul
        (psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) (a + 1) x)
        (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) x))).trans hsν) ?_
  intro x y hxy
  apply CycNorm.cancel (k + 1) (j + 1) x y
  have e : ∀ z, (CycRing.cyc LibInt.intR (k + 1)).mul z
      ((CycRing.cyc LibInt.intR (k + 1)).add (Pp (CycRing.cyc LibInt.intR (k + 1)) j) ((CycRing.cyc LibInt.intR (k + 1)).neg (CycRing.cyc LibInt.intR (k + 1)).one)) =
      nsm (CycRing.cyc LibInt.intR (k + 1)) (j + 1 + 1) z := fun z => by
    rw [NthEis.pp_nsm, Teich.nsm_one_mul (CycRing.cyc LibInt.intR (k + 1)) (j + 1 + 1) z]
    show (CycRing.cyc LibInt.intR (k + 1)).mul z ((CycRing.cyc LibInt.intR (k + 1)).add
      ((CycRing.cyc LibInt.intR (k + 1)).add (nsm (CycRing.cyc LibInt.intR (k + 1)) (j + 1 + 1) (CycRing.cyc LibInt.intR (k + 1)).one)
        (CycRing.cyc LibInt.intR (k + 1)).one) ((CycRing.cyc LibInt.intR (k + 1)).neg (CycRing.cyc LibInt.intR (k + 1)).one)) = _
    lib_ring
  rw [← e x, ← e y]
  exact hxy

include hpr hg hℓ hN in
/-- **∏_(i ≤ m) J(ψ^i, ψ) · ∏_(i ≤ m) J(ψ̄^i, ψ̄) = p^m** (m ≤ ℓ − 2). -/
theorem jpr_norm : ∀ m, m ≤ k →
    (CycRing.cyc LibInt.intR (k + 1)).mul
        (jpr (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) m)
        (jpr (CycRing.cyc LibInt.intR (k + 1)) j
          (fun x => CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (iv j x)) m) =
      pw (CycRing.cyc LibInt.intR (k + 1)) (Pp (CycRing.cyc LibInt.intR (k + 1)) j) m
  | 0, _ => by
    show (CycRing.cyc LibInt.intR (k + 1)).mul (CycRing.cyc LibInt.intR (k + 1)).one (CycRing.cyc LibInt.intR (k + 1)).one =
      (CycRing.cyc LibInt.intR (k + 1)).one
    lib_ring
  | m + 1, h => by
    have ih := jpr_norm m (Nat.le_trans (Nat.le_succ m) h)
    have hp := pair_norm j g hpr hg k t hℓ hN m (Nat.lt_succ_of_le (Nat.succ_le_succ h))
    show (CycRing.cyc LibInt.intR (k + 1)).mul
        ((CycRing.cyc LibInt.intR (k + 1)).mul
          (jpr (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) m)
          (J2 (CycRing.cyc LibInt.intR (k + 1)) j
            (psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) (m + 1))
            (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)))))
        ((CycRing.cyc LibInt.intR (k + 1)).mul
          (jpr (CycRing.cyc LibInt.intR (k + 1)) j
            (fun x => CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (iv j x)) m)
          (J2 (CycRing.cyc LibInt.intR (k + 1)) j
            (fun x => psp (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) (m + 1) (iv j x))
            (fun x => CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (iv j x)))) =
      (CycRing.cyc LibInt.intR (k + 1)).mul (pw (CycRing.cyc LibInt.intR (k + 1)) (Pp (CycRing.cyc LibInt.intR (k + 1)) j) m)
        (Pp (CycRing.cyc LibInt.intR (k + 1)) j)
    rw [← ih, ← hp]
    lib_ring

include hpr hg hℓ hN in
/-- **A(ψ)·A(ψ̄) = p^(ℓ−2) in ℤ[ζ_ℓ]**. -/
theorem A_norm :
    (CycRing.cyc LibInt.intR (k + 1)).mul
        (EisBridge.AA (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) k)
        (EisBridge.AA (CycRing.cyc LibInt.intR (k + 1)) j
          (fun x => CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (iv j x)) k) =
      pw (CycRing.cyc LibInt.intR (k + 1)) (Pp (CycRing.cyc LibInt.intR (k + 1)) j) k := by
  have hy : iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one) ≠ (ring (j + 1 + 1)).zero := fun h => by
    have := CharSumGen.iv_mul j hpr _ (PrimA.neg_one_ne j)
    rw [h] at this
    exact PowerSum.one_ne_zero' (j + 1) (this.symm.trans (by lib_ring))
  have h1 : (ring (j + 1 + 1)).mul (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))
      (iv j (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))) = (ring (j + 1 + 1)).one := by
    rw [(ring (j + 1 + 1)).mul_comm]
    exact CharSumGen.iv_mul j hpr _ hy
  have hc : (CycRing.cyc LibInt.intR (k + 1)).mul
      (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))
      (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (iv j (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one)))) =
      (CycRing.cyc LibInt.intR (k + 1)).one := by
    rw [← CycChar.psi_mul (j + 1) g _ hpr hg _ (hu_ell j k t hN), h1]
    exact CycChar.psi_one (j + 1) g _ hpr hg _
  have hj := jpr_norm j g hpr hg k t hℓ hN k (Nat.le_refl k)
  show (CycRing.cyc LibInt.intR (k + 1)).mul
      ((CycRing.cyc LibInt.intR (k + 1)).mul
        (jpr (CycRing.cyc LibInt.intR (k + 1)) j (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) k)
        (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))
      ((CycRing.cyc LibInt.intR (k + 1)).mul
        (jpr (CycRing.cyc LibInt.intR (k + 1)) j
          (fun x => CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (iv j x)) k)
        (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))
          (iv j (iv j ((ring (j + 1 + 1)).neg (ring (j + 1 + 1)).one))))) = _
  rw [rearr, hc, ← hj]
  lib_ring

end CycAbar

#print axioms CycAbar.rearr
#print axioms CycAbar.hu_ell
#print axioms CycAbar.hw_u
#print axioms CycAbar.pair_norm
#print axioms CycAbar.jpr_norm
#print axioms CycAbar.A_norm

#print axioms: 24 theorems, no axioms.

78.375 Eisenstein reciprocity for \(A\) in symbol form

With \((x/A)_\ell :=\prod _c (x/P_c)_\ell ^{v_{P_c}(A)}\), the Stickelberger-weighted product of the \(P_c\) symbols is the \(P_1\) symbol, which is the character of Eisenstein’s relation.

\[ \sum _{c=1}^{\ell -1} k_c(\ell -1-c)\equiv k_1 \pmod \ell ,\qquad \varphi _u(A)^{M}=\prod _{c}\bigl (u^{k_c}\bigr )^{\ell -1-c}. \]

lang/SymProd.lean

/-
  lang/SymProd — T49 (general ℓ, symbol form): **the Stickelberger-weighted product of the P_c symbols
  is the P_1 symbol**.
  Over ℤ/ℓ (ℓ = m + 3 prime): if the symbol of q at P_c is ζ^(k_c), then the base change ζ ↦ z^c gives
  c·k_c = j (j the exponent at P_1). With v_(P_c)(A) = ℓ − 1 − c (lang/ValC5) the exponent of the
  product Π_c (q/P_c)^(v_c) is Σ_c k_c (ℓ − 1 − c) ≡ Σ_c k_c (−1 − c), and
    k_form      x ≠ 0 ⇒ k_x = j·x^(ℓ−2);  k_0 = 0                   (Fermat: x^(ℓ−2)·x = 1)
    sum_k       Σ_x k_x = 0                                         (power sum of exponent ℓ − 2)
    sum_xk      Σ_x x·k_x = −j                                      (top power sum)
    sym_exp     Σ_x k_x·(−1 − x) = j
    weight      v + c + 1 = ℓ ⇒ v ≡ −1 − c (mod ℓ)
  Test: work/data/t49/symprod_test.py (ℓ = 3, 5, 7, 11, 13; 2376 cases, 0 mismatches).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.PowerSum
import lib.RingTac

namespace SymProd

open FTAChain ZMod YMChain FiniteRecChain

variable (m : Nat) (hpr : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1)

theorem sumK_negZ {A : Type} (K : DetChain.CRing (Zp (m + 1 + 1))) (f : A → Zp (m + 1 + 1)) :
    ∀ l : List A, sumK K (fun a => K.neg (f a)) l = K.neg (sumK K f l)
  | [] => by show K.zero = K.neg K.zero; lib_ring
  | a :: l => by
    show K.add (K.neg (f a)) (sumK K (fun a => K.neg (f a)) l) = K.neg (K.add (f a) (sumK K f l))
    rw [sumK_negZ K f l]; lib_ring

theorem pw_zero_succ (n : Nat) :
    Frobenius.pw (ring (m + 1 + 1)) (ring (m + 1 + 1)).zero (n + 1) = (ring (m + 1 + 1)).zero := by
  show (ring (m + 1 + 1)).mul (Frobenius.pw (ring (m + 1 + 1)) (ring (m + 1 + 1)).zero n) (ring (m + 1 + 1)).zero
    = (ring (m + 1 + 1)).zero
  lib_ring

include hpr in
/-- **k_x = j·x^(ℓ−2)** for every x, when x·k_x = j (x ≠ 0) and k_0 = 0. -/
theorem k_form (j : Zp (m + 1 + 1)) (k : Zp (m + 1 + 1) → Zp (m + 1 + 1))
    (hk : ∀ x, x ≠ (ring (m + 1 + 1)).zero → (ring (m + 1 + 1)).mul x (k x) = j)
    (hk0 : k (ring (m + 1 + 1)).zero = (ring (m + 1 + 1)).zero) (x : Zp (m + 1 + 1)) :
    k x = (ring (m + 1 + 1)).mul j (Frobenius.pw (ring (m + 1 + 1)) x (m + 1)) := by
  match decEq x (ring (m + 1 + 1)).zero with
  | isTrue h =>
    rw [h, hk0, pw_zero_succ m m]
    lib_ring
  | isFalse h =>
    have hinv := inv_exists (m + 1) hpr x h
    calc k x = (ring (m + 1 + 1)).mul (k x) (ring (m + 1 + 1)).one := by lib_ring
      _ = (ring (m + 1 + 1)).mul (k x)
          ((ring (m + 1 + 1)).mul (Frobenius.pw (ring (m + 1 + 1)) x (m + 1)) x) := by rw [hinv]
      _ = (ring (m + 1 + 1)).mul ((ring (m + 1 + 1)).mul x (k x)) (Frobenius.pw (ring (m + 1 + 1)) x (m + 1)) := by
          lib_ring
      _ = (ring (m + 1 + 1)).mul j (Frobenius.pw (ring (m + 1 + 1)) x (m + 1)) := by rw [hk x h]

include hpr in
/-- **Σ_x k_x = 0**. -/
theorem sum_k (j : Zp (m + 1 + 1)) (k : Zp (m + 1 + 1) → Zp (m + 1 + 1))
    (hk : ∀ x, x ≠ (ring (m + 1 + 1)).zero → (ring (m + 1 + 1)).mul x (k x) = j)
    (hk0 : k (ring (m + 1 + 1)).zero = (ring (m + 1 + 1)).zero) :
    sumK (ring (m + 1 + 1)) k (CMInert.allZ (m + 1 + 1) (m + 1 + 1 + 1)) = (ring (m + 1 + 1)).zero := by
  rw [sumK_ext (ring (m + 1 + 1)) k _ (k_form m hpr j k hk hk0), ← mul_sumK,
    PowerSum.power_sum_zero (m + 1) hpr m (Nat.le_refl _)]
  lib_ring

include hpr in
/-- **Σ_x x·k_x = −j**. -/
theorem sum_xk (j : Zp (m + 1 + 1)) (k : Zp (m + 1 + 1) → Zp (m + 1 + 1))
    (hk : ∀ x, x ≠ (ring (m + 1 + 1)).zero → (ring (m + 1 + 1)).mul x (k x) = j)
    (hk0 : k (ring (m + 1 + 1)).zero = (ring (m + 1 + 1)).zero) :
    sumK (ring (m + 1 + 1)) (fun x => (ring (m + 1 + 1)).mul x (k x)) (CMInert.allZ (m + 1 + 1) (m + 1 + 1 + 1))
      = (ring (m + 1 + 1)).neg j := by
  have hx : ∀ x, (ring (m + 1 + 1)).mul x (k x)
      = (ring (m + 1 + 1)).mul j (Frobenius.pw (ring (m + 1 + 1)) x (m + 1 + 1)) := fun x => by
    rw [k_form m hpr j k hk hk0 x]
    show (ring (m + 1 + 1)).mul x ((ring (m + 1 + 1)).mul j (Frobenius.pw (ring (m + 1 + 1)) x (m + 1)))
      = (ring (m + 1 + 1)).mul j ((ring (m + 1 + 1)).mul (Frobenius.pw (ring (m + 1 + 1)) x (m + 1)) x)
    lib_ring
  rw [sumK_ext (ring (m + 1 + 1)) _ _ hx, ← mul_sumK, PowerSum.power_sum_top (m + 1) hpr]
  lib_ring

include hpr in
/-- **The weighted symbol exponent**: Σ_x k_x·(−1 − x) = j. With x = c, −1 − c ≡ ℓ − 1 − c = v_(P_c)(A),
  this says Π_c (q/P_c)^(v_(P_c)(A)) = (q/P_1). -/
theorem sym_exp (j : Zp (m + 1 + 1)) (k : Zp (m + 1 + 1) → Zp (m + 1 + 1))
    (hk : ∀ x, x ≠ (ring (m + 1 + 1)).zero → (ring (m + 1 + 1)).mul x (k x) = j)
    (hk0 : k (ring (m + 1 + 1)).zero = (ring (m + 1 + 1)).zero) :
    sumK (ring (m + 1 + 1))
      (fun x => (ring (m + 1 + 1)).mul (k x)
        ((ring (m + 1 + 1)).add ((ring (m + 1 + 1)).neg (ring (m + 1 + 1)).one) ((ring (m + 1 + 1)).neg x)))
      (CMInert.allZ (m + 1 + 1) (m + 1 + 1 + 1)) = j := by
  have hs : ∀ x, (ring (m + 1 + 1)).mul (k x)
      ((ring (m + 1 + 1)).add ((ring (m + 1 + 1)).neg (ring (m + 1 + 1)).one) ((ring (m + 1 + 1)).neg x))
      = (ring (m + 1 + 1)).add ((ring (m + 1 + 1)).neg (k x))
          ((ring (m + 1 + 1)).neg ((ring (m + 1 + 1)).mul x (k x))) := fun x => by lib_ring
  rw [sumK_ext (ring (m + 1 + 1)) _ _ hs, sumK_add, sumK_negZ m, sumK_negZ m,
    sum_k m hpr j k hk hk0, sum_xk m hpr j k hk hk0]
  lib_ring

/-- **The Stickelberger weight mod ℓ**: v + c + 1 = ℓ ⇒ v ≡ −1 − c. -/
theorem weight (v c : Nat) (h : v + c + 1 = m + 1 + 1 + 1) :
    mk (m + 1 + 1) v = (ring (m + 1 + 1)).add ((ring (m + 1 + 1)).neg (ring (m + 1 + 1)).one)
      ((ring (m + 1 + 1)).neg (mk (m + 1 + 1) c)) := by
  have hadd : ∀ a b, (ring (m + 1 + 1)).add (mk (m + 1 + 1) a) (mk (m + 1 + 1) b) = mk (m + 1 + 1) (a + b) :=
    fun a b => Subtype.ext (by
      show md (m + 1 + 1) (md (m + 1 + 1) a + md (m + 1 + 1) b) = md (m + 1 + 1) (a + b)
      rw [← md_add2])
  have h0 : (ring (m + 1 + 1)).add (mk (m + 1 + 1) v)
      ((ring (m + 1 + 1)).add (mk (m + 1 + 1) c) (ring (m + 1 + 1)).one) = (ring (m + 1 + 1)).zero := by
    show (ring (m + 1 + 1)).add (mk (m + 1 + 1) v) ((ring (m + 1 + 1)).add (mk (m + 1 + 1) c) (mk (m + 1 + 1) 1))
      = mk (m + 1 + 1) 0
    rw [hadd c 1, hadd v (c + 1), ← Nat.add_assoc, h]
    exact Subtype.ext (by show md (m + 1 + 1) (m + 1 + 1 + 1) = md (m + 1 + 1) 0; rw [md_self]; rfl)
  calc mk (m + 1 + 1) v
      = (ring (m + 1 + 1)).add ((ring (m + 1 + 1)).add (mk (m + 1 + 1) v)
          ((ring (m + 1 + 1)).add (mk (m + 1 + 1) c) (ring (m + 1 + 1)).one))
          ((ring (m + 1 + 1)).add ((ring (m + 1 + 1)).neg (ring (m + 1 + 1)).one)
            ((ring (m + 1 + 1)).neg (mk (m + 1 + 1) c))) := by lib_ring
    _ = _ := by rw [h0]; lib_ring

end SymProd

#print axioms SymProd.sumK_negZ
#print axioms SymProd.pw_zero_succ
#print axioms SymProd.k_form
#print axioms SymProd.sum_k
#print axioms SymProd.sum_xk
#print axioms SymProd.sym_exp
#print axioms SymProd.weight

lang/SymProdW.lean

/-
  lang/SymProdW — T49 (general ℓ, symbol form): **Π_c (q/P_c)^(v_c) = (q/P_1)** with natural-number exponents.
  ℓ = m + 3 prime. Exponents k_c ∈ ℕ (c = 1, …, ℓ − 1) with c·k_c ≡ j (mod ℓ) and Stickelberger weights
  v_c + c + 1 = ℓ (v_c = v_(P_c)(A), lang/ValC5). T n = Σ_(c=1..n) k_c v_c.
    sum_allZ     Σ_(x ∈ allZ (n+1)) K_x (−1 − x) = mk (T n)       (K_x = mk k_(x.val), K_0 = 0)
    exp_nat      T (ℓ − 1) ≡ j (mod ℓ)                            (lang/SymProd.sym_exp)
    sym_root     w^ℓ = 1 in any commutative ring ⇒ w^(T (ℓ − 1)) = w^j,
                 i.e. Π_c (w^(k_c))^(v_c) = w^j
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.SymProd
import lib.Semi

namespace SymProdW

open FTAChain ZMod YMChain FiniteRecChain

variable (m : Nat)

/-- T n = Σ_(c = 1..n) k_c·v_c. -/
def T (k v : Nat → Nat) : Nat → Nat
  | 0 => 0
  | n + 1 => T k v n + k (n + 1) * v (n + 1)

def K (k : Nat → Nat) (x : Zp (m + 1 + 1)) : Zp (m + 1 + 1) :=
  if x.val = 0 then (ring (m + 1 + 1)).zero else mk (m + 1 + 1) (k x.val)

theorem mk_add (a b : Nat) : (ring (m + 1 + 1)).add (mk (m + 1 + 1) a) (mk (m + 1 + 1) b) = mk (m + 1 + 1) (a + b) :=
  Subtype.ext (by
    show md (m + 1 + 1) (md (m + 1 + 1) a + md (m + 1 + 1) b) = md (m + 1 + 1) (a + b)
    rw [← md_add2])

theorem mk_mul (a b : Nat) : (ring (m + 1 + 1)).mul (mk (m + 1 + 1) a) (mk (m + 1 + 1) b) = mk (m + 1 + 1) (a * b) :=
  Subtype.ext (by
    show md (m + 1 + 1) (md (m + 1 + 1) a * md (m + 1 + 1) b) = md (m + 1 + 1) (a * b)
    rw [← md_mul2])

theorem K_zero (k : Nat → Nat) : K m k (ring (m + 1 + 1)).zero = (ring (m + 1 + 1)).zero := by
  show (if (mk (m + 1 + 1) 0).val = 0 then (ring (m + 1 + 1)).zero else mk (m + 1 + 1) (k (mk (m + 1 + 1) 0).val))
    = (ring (m + 1 + 1)).zero
  exact ZMod.ite_t _ _ (rfl : (mk (m + 1 + 1) 0).val = 0)

theorem K_mk (k : Nat → Nat) (c : Nat) (h1 : 1 ≤ c) (h2 : c < m + 1 + 1 + 1) :
    K m k (mk (m + 1 + 1) c) = mk (m + 1 + 1) (k c) := by
  have hv : (mk (m + 1 + 1) c).val = c := md_small (m + 1 + 1) c h2
  show (if (mk (m + 1 + 1) c).val = 0 then (ring (m + 1 + 1)).zero else mk (m + 1 + 1) (k (mk (m + 1 + 1) c).val))
    = mk (m + 1 + 1) (k c)
  rw [hv]
  exact ZMod.ite_f _ _ (fun e => by rw [e] at h1; exact Nat.not_succ_le_zero 0 h1)

/-- the summand of SymProd.sym_exp. -/
def F (k : Nat → Nat) (x : Zp (m + 1 + 1)) : Zp (m + 1 + 1) :=
  (ring (m + 1 + 1)).mul (K m k x)
    ((ring (m + 1 + 1)).add ((ring (m + 1 + 1)).neg (ring (m + 1 + 1)).one) ((ring (m + 1 + 1)).neg x))

theorem sum_allZ (k v : Nat → Nat) (hv : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → v c + c + 1 = m + 1 + 1 + 1) :
    ∀ n, n < m + 1 + 1 + 1 →
      sumK (ring (m + 1 + 1)) (F m k) (CMInert.allZ (m + 1 + 1) (n + 1)) = mk (m + 1 + 1) (T k v n)
  | 0, _ => by
    show (ring (m + 1 + 1)).add (F m k (mk (m + 1 + 1) 0)) (ring (m + 1 + 1)).zero = mk (m + 1 + 1) 0
    show (ring (m + 1 + 1)).add ((ring (m + 1 + 1)).mul (K m k (ring (m + 1 + 1)).zero)
      ((ring (m + 1 + 1)).add ((ring (m + 1 + 1)).neg (ring (m + 1 + 1)).one)
        ((ring (m + 1 + 1)).neg (ring (m + 1 + 1)).zero))) (ring (m + 1 + 1)).zero = (ring (m + 1 + 1)).zero
    rw [K_zero]
    lib_ring
  | n + 1, h => by
    have h' : n < m + 1 + 1 + 1 := Nat.lt_trans (Nat.lt_succ_self n) h
    have ih := sum_allZ k v hv n h'
    show sumK (ring (m + 1 + 1)) (F m k) (CMInert.allZ (m + 1 + 1) (n + 1) ++ [mk (m + 1 + 1) (n + 1)])
      = mk (m + 1 + 1) (T k v n + k (n + 1) * v (n + 1))
    rw [CMInert.sumK_app', ih]
    show (ring (m + 1 + 1)).add (mk (m + 1 + 1) (T k v n))
      ((ring (m + 1 + 1)).add (F m k (mk (m + 1 + 1) (n + 1))) (ring (m + 1 + 1)).zero)
      = mk (m + 1 + 1) (T k v n + k (n + 1) * v (n + 1))
    have hF : F m k (mk (m + 1 + 1) (n + 1)) = mk (m + 1 + 1) (k (n + 1) * v (n + 1)) := by
      show (ring (m + 1 + 1)).mul (K m k (mk (m + 1 + 1) (n + 1)))
        ((ring (m + 1 + 1)).add ((ring (m + 1 + 1)).neg (ring (m + 1 + 1)).one)
          ((ring (m + 1 + 1)).neg (mk (m + 1 + 1) (n + 1)))) = mk (m + 1 + 1) (k (n + 1) * v (n + 1))
      rw [K_mk m k (n + 1) (Nat.succ_le_succ (Nat.zero_le n)) h,
        ← SymProd.weight m (v (n + 1)) (n + 1) (hv (n + 1) (Nat.succ_le_succ (Nat.zero_le n)) h), mk_mul]
    rw [hF, ← mk_add m]
    lib_ring

/-- **T (ℓ − 1) ≡ j (mod ℓ)**: the Stickelberger-weighted exponent sum of the P_c symbols. -/
theorem exp_nat (hpr : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1) (k v : Nat → Nat) (j : Nat)
    (hk : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → md (m + 1 + 1) (c * k c) = md (m + 1 + 1) j)
    (hv : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → v c + c + 1 = m + 1 + 1 + 1) :
    md (m + 1 + 1) (T k v (m + 1 + 1)) = md (m + 1 + 1) j := by
  have hK : ∀ x, x ≠ (ring (m + 1 + 1)).zero → (ring (m + 1 + 1)).mul x (K m k x) = mk (m + 1 + 1) j := by
    intro x hx
    have hx1 : 1 ≤ x.val := by
      cases hxv : x.val with
      | zero => exact absurd (Subtype.ext (by rw [hxv]; rfl)) hx
      | succ n => exact Nat.succ_le_succ (Nat.zero_le n)
    have hmk : mk (m + 1 + 1) x.val = x := mk_val (m + 1 + 1) x
    calc (ring (m + 1 + 1)).mul x (K m k x)
        = (ring (m + 1 + 1)).mul (mk (m + 1 + 1) x.val) (K m k (mk (m + 1 + 1) x.val)) := by rw [hmk]
      _ = mk (m + 1 + 1) (x.val * k x.val) := by rw [K_mk m k x.val hx1 x.property, mk_mul]
      _ = mk (m + 1 + 1) j := Subtype.ext (hk x.val hx1 x.property)
  have hs := SymProd.sym_exp m hpr (mk (m + 1 + 1) j) (K m k) hK (K_zero m k)
  have ha := sum_allZ m k v hv (m + 1 + 1) (Nat.lt_succ_self _)
  have he : mk (m + 1 + 1) (T k v (m + 1 + 1)) = mk (m + 1 + 1) j := by
    rw [← ha]; exact hs
  exact congrArg Subtype.val he

theorem pw_add {R : Type} (Rg : DetChain.CRing R) (x : R) (a : Nat) :
    ∀ b, Frobenius.pw Rg x (a + b) = Rg.mul (Frobenius.pw Rg x a) (Frobenius.pw Rg x b)
  | 0 => by show Frobenius.pw Rg x a = Rg.mul (Frobenius.pw Rg x a) Rg.one; rw [Rg.mul_comm, Rg.one_mul]
  | b + 1 => by
    show Rg.mul (Frobenius.pw Rg x (a + b)) x = Rg.mul (Frobenius.pw Rg x a) (Rg.mul (Frobenius.pw Rg x b) x)
    rw [pw_add Rg x a b, Rg.mul_assoc]

theorem pw_mulE {R : Type} (Rg : DetChain.CRing R) (x : R) (L : Nat) (hL : Frobenius.pw Rg x L = Rg.one) :
    ∀ q, Frobenius.pw Rg x (q * L) = Rg.one
  | 0 => by
    have e : 0 * L = 0 := by lib_nat
    rw [e]; rfl
  | q + 1 => by
    have e : (q + 1) * L = q * L + L := by lib_nat
    rw [e, pw_add, pw_mulE Rg x L hL q, hL, Rg.one_mul]

/-- w^ℓ = 1 ⇒ w^a = w^(a mod ℓ). -/
theorem pw_md {R : Type} (Rg : DetChain.CRing R) (x : R) (hL : Frobenius.pw Rg x (m + 1 + 1 + 1) = Rg.one) (a : Nat) :
    Frobenius.pw Rg x a = Frobenius.pw Rg x (md (m + 1 + 1) a) := by
  obtain ⟨q, hq⟩ := md_spec (m + 1 + 1) a
  calc Frobenius.pw Rg x a = Frobenius.pw Rg x (md (m + 1 + 1) a + q * (m + 1 + 1 + 1)) := by rw [← hq]
    _ = Frobenius.pw Rg x (md (m + 1 + 1) a) := by rw [pw_add, pw_mulE Rg x _ hL q, Rg.mul_comm, Rg.one_mul]

/-- **Π_c (q/P_c)^(v_(P_c)(A)) = (q/P_1)**: for any w with w^ℓ = 1, w^(Σ_c k_c v_c) = w^j. -/
theorem sym_root (hpr : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1) (k v : Nat → Nat) (j : Nat)
    (hk : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → md (m + 1 + 1) (c * k c) = md (m + 1 + 1) j)
    (hv : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → v c + c + 1 = m + 1 + 1 + 1)
    {R : Type} (Rg : DetChain.CRing R) (w : R) (hw : Frobenius.pw Rg w (m + 1 + 1 + 1) = Rg.one) :
    Frobenius.pw Rg w (T k v (m + 1 + 1)) = Frobenius.pw Rg w j := by
  rw [pw_md m Rg w hw, exp_nat m hpr k v j hk hv, ← pw_md m Rg w hw]

end SymProdW

#print axioms SymProdW.mk_add
#print axioms SymProdW.mk_mul
#print axioms SymProdW.K_zero
#print axioms SymProdW.K_mk
#print axioms SymProdW.sum_allZ
#print axioms SymProdW.exp_nat
#print axioms SymProdW.pw_add
#print axioms SymProdW.pw_mulE
#print axioms SymProdW.pw_md
#print axioms SymProdW.sym_root

lang/SymProdZ.lean

/-
  lang/SymProdZ — T49 (general ℓ, symbol form): **the P_c symbols read at the Teichmüller root**.
  ℓ = m + 3 prime, z of exact order ℓ in a commutative ring (z = g^((p−1)/ℓ) in 𝔽_p), s = q^((p−1)/ℓ) = z^j.
  The symbol of q at P_c (where ζ ↦ z^c) is ζ^(k_c) with (z^c)^(k_c) = s. Then
    exp_eq      z^a = z^b ⇒ a ≡ b (mod ℓ)                         (order ℓ, no division)
    pw_pw       (z^a)^b = z^(a·b)
    sym_prod_z  for every w with w^ℓ = 1:  w^(Σ_c k_c v_c) = w^j,   v_c + c + 1 = ℓ
  i.e. Π_c (q/P_c)^(v_(P_c)(A)) = (q/P_1) (lang/SymProdW, lang/ValC5 for v_c = v_(P_c)(A)).
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.SymProdW
import lang.OrdMul

namespace SymProdZ

open FTAChain ZMod YMChain FiniteRecChain

variable (m : Nat)

theorem le_ex {a b : Nat} (h : a ≤ b) : ∃ d, a + d = b := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih =>
    obtain ⟨d, hd⟩ := ih
    exact ⟨d + 1, congrArg Nat.succ hd⟩

theorem pw_pw {R : Type} (Rg : DetChain.CRing R) (x : R) (a : Nat) :
    ∀ b, Frobenius.pw Rg (Frobenius.pw Rg x a) b = Frobenius.pw Rg x (a * b)
  | 0 => rfl
  | b + 1 => by
    show Rg.mul (Frobenius.pw Rg (Frobenius.pw Rg x a) b) (Frobenius.pw Rg x a) = Frobenius.pw Rg x (a * b + a)
    rw [pw_pw Rg x a b, SymProdW.pw_add]

/-- equal powers below the order are equal exponents. -/
theorem small_eq {R : Type} (Rg : DetChain.CRing R) (z : R) (hz : OrdMul.Ord Rg z (m + 1 + 1 + 1))
    (r1 r2 : Nat) (h2 : r2 < m + 1 + 1 + 1) (hle : r1 ≤ r2)
    (he : Frobenius.pw Rg z r1 = Frobenius.pw Rg z r2) : r1 = r2 := by
  obtain ⟨d, hd⟩ := le_ex hle
  have hdle : d ≤ r2 := by rw [← hd]; exact Nat.le_add_left d r1
  have hdl : d < m + 1 + 1 + 1 := Nat.lt_of_le_of_lt hdle h2
  have hL : Frobenius.pw Rg z (r1 * (m + 1 + 1 + 1)) = Rg.one := SymProdW.pw_mulE Rg z _ hz.1 r1
  have hsplit : r1 * (m + 1 + 1 + 1) = r1 * (m + 1 + 1) + r1 := by lib_nat
  rw [hsplit, SymProdW.pw_add] at hL
  have hx : Frobenius.pw Rg z r1 = Rg.mul (Frobenius.pw Rg z r1) (Frobenius.pw Rg z d) := by
    rw [← SymProdW.pw_add, hd]; exact he
  have h1 : Frobenius.pw Rg z d = Rg.one :=
    calc Frobenius.pw Rg z d
        = Rg.mul (Rg.mul (Frobenius.pw Rg z (r1 * (m + 1 + 1))) (Frobenius.pw Rg z r1)) (Frobenius.pw Rg z d) := by
          rw [hL, Rg.one_mul]
      _ = Rg.mul (Frobenius.pw Rg z (r1 * (m + 1 + 1)))
            (Rg.mul (Frobenius.pw Rg z r1) (Frobenius.pw Rg z d)) := Rg.mul_assoc _ _ _
      _ = Rg.mul (Frobenius.pw Rg z (r1 * (m + 1 + 1))) (Frobenius.pw Rg z r1) := by rw [← hx]
      _ = Rg.one := hL
  obtain ⟨t, ht⟩ := hz.2 d h1
  cases t with
  | zero =>
    have : d = 0 := by rw [ht]; lib_nat
    calc r1 = r1 + 0 := rfl
      _ = r1 + d := by rw [this]
      _ = r2 := hd
  | succ t =>
    have hge : m + 1 + 1 + 1 ≤ d := by
      rw [ht]
      have e : (m + 1 + 1 + 1) * (t + 1) = (m + 1 + 1 + 1) * t + (m + 1 + 1 + 1) := by lib_nat
      rw [e]; exact Nat.le_add_left _ _
    exact absurd (Nat.lt_of_lt_of_le hdl hge) (Nat.lt_irrefl _)

/-- **z^a = z^b ⇒ a ≡ b (mod ℓ)** for z of exact order ℓ. -/
theorem exp_eq {R : Type} (Rg : DetChain.CRing R) (z : R) (hz : OrdMul.Ord Rg z (m + 1 + 1 + 1)) (a b : Nat)
    (he : Frobenius.pw Rg z a = Frobenius.pw Rg z b) : md (m + 1 + 1) a = md (m + 1 + 1) b := by
  have h' : Frobenius.pw Rg z (md (m + 1 + 1) a) = Frobenius.pw Rg z (md (m + 1 + 1) b) := by
    rw [← SymProdW.pw_md m Rg z hz.1 a, ← SymProdW.pw_md m Rg z hz.1 b]; exact he
  cases Nat.lt_or_ge (md (m + 1 + 1) a) (md (m + 1 + 1) b) with
  | inl hlt => exact small_eq m Rg z hz _ _ (md_lt (m + 1 + 1) b) (Nat.le_of_lt hlt) h'
  | inr hge => exact (small_eq m Rg z hz _ _ (md_lt (m + 1 + 1) a) hge h'.symm).symm

/-- **Π_c (q/P_c)^(v_(P_c)(A)) = (q/P_1)**: if (z^c)^(k_c) = s = z^j for 1 ≤ c ≤ ℓ − 1 and v_c + c + 1 = ℓ,
  then w^(Σ_c k_c v_c) = w^j for every w with w^ℓ = 1. -/
theorem sym_prod_z (hpr : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1)
    {R : Type} (Rg : DetChain.CRing R) (z s : R) (hz : OrdMul.Ord Rg z (m + 1 + 1 + 1)) (j : Nat)
    (hj : Frobenius.pw Rg z j = s) (k v : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → Frobenius.pw Rg (Frobenius.pw Rg z c) (k c) = s)
    (hv : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → v c + c + 1 = m + 1 + 1 + 1)
    {E : Type} (Eg : DetChain.CRing E) (w : E) (hw : Frobenius.pw Eg w (m + 1 + 1 + 1) = Eg.one) :
    Frobenius.pw Eg w (SymProdW.T k v (m + 1 + 1)) = Frobenius.pw Eg w j :=
  SymProdW.sym_root m hpr k v j
    (fun c h1 h2 => exp_eq m Rg z hz _ _ (by rw [← pw_pw, hk c h1 h2, hj])) hv Eg w hw

end SymProdZ

#print axioms SymProdZ.le_ex
#print axioms SymProdZ.pw_pw
#print axioms SymProdZ.small_eq
#print axioms SymProdZ.exp_eq
#print axioms SymProdZ.sym_prod_z

lang/SymPsi.lean

/-
  lang/SymPsi — T49 (general ℓ, symbol form): **ψ_u(q) is the Stickelberger-weighted product of the P_c symbols**.
  p = J + 2 prime, g of order p − 1 = ℓ(t + 1), ℓ = m + 3 prime, z = g^(t+1) (order ℓ), u^ℓ = 1 in any ring E.
    z_ord       z has exact order ℓ
    psi_sym     x^((p−1)/ℓ) = z^j ⇒ ψ_u(x) = u^j                      (the P_1 symbol read through ψ_u)
    psi_stick   (z^c)^(k_c) = x^((p−1)/ℓ) for 1 ≤ c ≤ ℓ − 1 and v_c + c + 1 = ℓ
                ⇒ ψ_u(x) = u^(Σ_c k_c v_c) = Π_c (u^(k_c))^(v_c)
  With v_c = v_(P_c)(A) (lang/ValC5) the right side is (x/A)_ℓ := Π_c (x/P_c)^(v_(P_c)(A)), and the left side
  is the character of lang/CycFq.cyc_eis_fq (ψ_u(q^(−e)) = φ_u(A)^M): Eisenstein's reciprocity for A.
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.SymProdZ
import lang.CycChar

namespace SymPsi

open FTAChain ZMod YMChain FiniteRecChain
open FiniteRecChain (ite_neg')

variable (J m t : Nat) (g : Zp (J + 1)) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
  (hN : J + 1 = (m + 1 + 1 + 1) * (t + 1))

include hg hN in
/-- **z = g^((p−1)/ℓ) has exact order ℓ**. -/
theorem z_ord : OrdMul.Ord (ring (J + 1)) (Frobenius.pw (ring (J + 1)) g (t + 1)) (m + 1 + 1 + 1) := by
  refine ⟨?_, fun r hr => ?_⟩
  · have e : (t + 1) * (m + 1 + 1 + 1) = J + 1 := by rw [hN]; lib_nat
    rw [SymProdZ.pw_pw, e]; exact hg.1
  · rw [SymProdZ.pw_pw] at hr
    obtain ⟨c, hc⟩ := hg.2 _ hr
    have e : (t + 1) * r = (t + 1) * ((m + 1 + 1 + 1) * c) := by rw [hc, hN]; lib_nat
    exact ⟨c, Nat.eq_of_mul_eq_mul_left (Nat.succ_pos t) e⟩

include hg hN in
/-- **the P_1 symbol through ψ_u**: x^((p−1)/ℓ) = z^j ⇒ ψ_u(x) = u^j. -/
theorem psi_sym (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
    {S : Type} (E : DetChain.CRing S) (u : S) (hu : Frobenius.pw E u (m + 1 + 1 + 1) = E.one)
    (x : Zp (J + 1)) (hx : x ≠ (ring (J + 1)).zero) (j : Nat)
    (hj : Frobenius.pw (ring (J + 1)) x (t + 1) = Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) g (t + 1)) j) :
    CycChar.psi J g E u x = Frobenius.pw E u j := by
  have hd := (DLogF.dlog_spec J hpr g hg x hx).1
  have h1 : Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) g (t + 1)) (DLogF.dlog J g x)
      = Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) g (t + 1)) j := by
    have e : (t + 1) * DLogF.dlog J g x = DLogF.dlog J g x * (t + 1) := by lib_nat
    rw [SymProdZ.pw_pw, e, ← SymProdZ.pw_pw, hd]
    exact hj
  have h2 := SymProdZ.exp_eq m (ring (J + 1)) _ (z_ord J m t g hg hN) _ _ h1
  show (if x = (ring (J + 1)).zero then E.zero else Frobenius.pw E u (DLogF.dlog J g x)) = Frobenius.pw E u j
  rw [ite_neg' _ _ hx, SymProdW.pw_md m E u hu (DLogF.dlog J g x), h2, ← SymProdW.pw_md m E u hu j]

include hg hN in
/-- **ψ_u(x) = Π_c (u^(k_c))^(v_c)**: the character of Eisenstein's relation is the Stickelberger-weighted
  product of the P_c symbols (z^c)^(k_c) = x^((p−1)/ℓ). -/
theorem psi_stick (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
    (hℓ : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1)
    {S : Type} (E : DetChain.CRing S) (u : S) (hu : Frobenius.pw E u (m + 1 + 1 + 1) = E.one)
    (x : Zp (J + 1)) (hx : x ≠ (ring (J + 1)).zero) (k v : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 →
      Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) g (t + 1)) c) (k c)
        = Frobenius.pw (ring (J + 1)) x (t + 1))
    (hv : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → v c + c + 1 = m + 1 + 1 + 1) :
    CycChar.psi J g E u x = Frobenius.pw E u (SymProdW.T k v (m + 1 + 1)) := by
  have hk1 := hk 1 (Nat.le_refl 1) (Nat.succ_lt_succ (Nat.succ_pos _))
  have hj : Frobenius.pw (ring (J + 1)) x (t + 1)
      = Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) g (t + 1)) (1 * k 1) := by
    rw [← SymProdZ.pw_pw]; exact hk1.symm
  rw [psi_sym J m t g hg hN hpr E u hu x hx (1 * k 1) hj]
  exact (SymProdZ.sym_prod_z m hℓ (ring (J + 1)) _ _ (z_ord J m t g hg hN) (1 * k 1) hj.symm k v hk hv E u hu).symm

end SymPsi

#print axioms SymPsi.z_ord
#print axioms SymPsi.psi_sym
#print axioms SymPsi.psi_stick

lang/EisRecA.lean

/-
  lang/EisRecA — T49 (every odd prime ℓ, symbol form): **Eisenstein's reciprocity for the primary element A**.
  A = Π_(i ≤ ℓ−2) J(χ^i, χ)·χ(−1) ∈ ℤ[C_ℓ] (primary, lang/CycPrim), E = 𝔽_q[C_ℓ]/(N), u = [σ].
  x = q^(−e) ∈ 𝔽_p. If the symbol of x at P_c is u^(k_c), i.e. (z^c)^(k_c) = x^((p−1)/ℓ) (z = g^((p−1)/ℓ)),
  and v_c + c + 1 = ℓ (v_c = v_(P_c)(A), lang/ValC5.A_val_all), then
    eis_recip_A   φ_u(A)^M = u^(Σ_c k_c v_c) = Π_c (u^(k_c))^(v_c)        (q^e = ℓM + 1, (q − 1) | M)
  i.e. (A/𝔮)_ℓ = (x/A)_ℓ with (x/A)_ℓ := Π_c (x/P_c)^(v_(P_c)(A)): lang/CycFq.cyc_eis_fq + lang/SymPsi.psi_stick.
  Lean 4 core only, no axioms.
-/
import lang.SymPsi
import lang.CycFq

namespace EisRecA

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

/-- **Eisenstein's reciprocity for A (symbol form)**: φ_u(A)^M = u^(Σ_c k_c v_(P_c)(A)). -/
theorem eis_recip_A (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (m t : Nat) (hℓ : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1)
    (hN : j + 1 + 1 = (m + 1 + 1 + 1) * (t + 1))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (m + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k v : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (hv : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → v c + c + 1 = m + 1 + 1 + 1) :
    pw (CycRing.cyc (ring (k' + 1)) (m + 1 + 1))
        (CycEis.phz (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (m + 1 + 1) (CycUnit.u (ring (k' + 1)) (m + 1 + 1))
          (EisBridge.AA (gring LibInt.intR (m + 1 + 1)) j (CycPrim.chi (m + 1 + 1) j g) (m + 1))) M
      = pw (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1)) (SymProdW.T k v (m + 1 + 1)) := by
  rw [← CycFq.cyc_eis_fq j g hpr hg (m + 1) t hℓ hN k' hq e hQ M hqn hp s hs]
  exact SymPsi.psi_stick (j + 1) m t g hg hN hpr hℓ (CycRing.cyc (ring (k' + 1)) (m + 1 + 1))
    (CycUnit.u (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u_ell (ring (k' + 1)) (m + 1 + 1)) _
    (DLog.pw_ne_zero (j + 1) hpr _ hQ (j + 1)) k v hk hv

end EisRecA

#print axioms EisRecA.eis_recip_A

lang/EisRecV.lean

/-
  lang/EisRecV — T49 (every odd prime ℓ = m + 3, symbol form): **Eisenstein's reciprocity for A with the
  Stickelberger valuations in one statement**.
    subN, subN_add     truncated subtraction by structural recursion; (b + d) − b = d
    vS_spec            v_c := ℓ − 1 − c satisfies v_c + c + 1 = ℓ (1 ≤ c ≤ ℓ − 1)
    eis_recip_val      (1) ι_(N,c)(A) = P^(v_c)·U, U a unit mod p, for every 1 ≤ c ≤ ℓ − 1   (lang/ValC5)
                       (2) φ_u(A)^M = u^(Σ_c k_c v_c) = Π_c (u^(k_c))^(v_c)              (lang/EisRecA)
  i.e. (A) = Π_c P_c^(v_c) at every P_c above p, and (A/𝔮)_ℓ = (q^(−e)/A)_ℓ with the same exponents v_c.
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.EisRecA
import lang.ValC5

namespace EisRecV

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

def subN : Nat → Nat → Nat
  | a, 0 => a
  | 0, _ + 1 => 0
  | a + 1, b + 1 => subN a b

theorem subN_zero : ∀ a, subN a 0 = a
  | 0 => rfl
  | _ + 1 => rfl

theorem subN_add : ∀ b d, subN (b + d) b = d
  | 0, d => by
    have e : 0 + d = d := by lib_nat
    rw [e, subN_zero]
  | b + 1, d => by
    have e : b + 1 + d = (b + d) + 1 := by lib_nat
    rw [e]
    exact subN_add b d

theorem add_rc (a b : Nat) : ∀ c, a + c = b + c → a = b
  | 0, h => h
  | c + 1, h => add_rc a b c (Nat.succ.inj h)

/-- v_c := ℓ − 1 − c (ℓ = kk + 2). -/
def vS (kk c : Nat) : Nat := subN (kk + 1) c

/-- the Stickelberger exponent below ℓ − 1: c' + d = kk ⇒ v_(c'+1) = d. -/
theorem vS_eq (kk c' d : Nat) (h : c' + d = kk) : vS kk (c' + 1) = d := by
  show subN kk c' = d
  rw [← h]; exact subN_add c' d

theorem vS_spec (kk : Nat) : ∀ c, 1 ≤ c → c < kk + 1 + 1 → vS kk c + c + 1 = kk + 1 + 1
  | 0, h1, _ => nomatch h1
  | c' + 1, _, h2 => by
    obtain ⟨d, hd⟩ := SymProdZ.le_ex (Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ h2))
    rw [vS_eq kk c' d hd, ← hd]
    lib_nat

/-- **Eisenstein's reciprocity for A with its valuations**: (1) v_(P_c)(A) = ℓ − 1 − c for every c, and
  (2) φ_u(A)^Me = u^(Σ_c k_c (ℓ − 1 − c)). -/
theorem eis_recip_val (j Mp kp J₂ k₂ N'' : Nat) (hk : Mp + 1 = (j + 1 + 1 + 1) * kp) (hk₂ : Mp + 1 = (J₂ + 1 + 1) * k₂)
    (hM₂ : J₂ + 1 + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1))
    (hN0 : pw (ring Mp) (nsm (ring Mp) (j + 1 + 1 + 1) (ring Mp).one) (N'' + 1 + 1) = (ring Mp).zero)
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (m t : Nat) (hℓ : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1)
    (hN : j + 1 + 1 = (m + 1 + 1 + 1) * (t + 1))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (Me : Nat) (hqn : (k' + 1 + 1) ^ e = (m + 1 + 1 + 1) * Me + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : Me = (k' + 1) * s)
    (k : Nat → Nat)
    (hkc : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1)) :
    (∀ c' f, f + 1 = (t + 1) * (c' + 1) → c' + 1 + 1 ≤ m + 1 + 1 + 1 →
      ∃ U, CycEis.phz (ring Mp) (m + 1 + 1) (TeichN.psiN (j + 1) Mp (N'' + 1) (f + 1) g)
          (EisBridge.AA (gring LibInt.intR (m + 1 + 1)) j (CycPrim.chi (m + 1 + 1) j g) (m + 1)) =
          (ring Mp).mul (pw (ring Mp) (nsm (ring Mp) (j + 1 + 1 + 1) (ring Mp).one) (vS (m + 1) (c' + 1))) U ∧
        RedP2.red (j + 1) Mp U ≠ (ring (j + 1 + 1)).zero) ∧
    pw (CycRing.cyc (ring (k' + 1)) (m + 1 + 1))
        (CycEis.phz (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (m + 1 + 1) (CycUnit.u (ring (k' + 1)) (m + 1 + 1))
          (EisBridge.AA (gring LibInt.intR (m + 1 + 1)) j (CycPrim.chi (m + 1 + 1) j g) (m + 1))) Me
      = pw (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1))
          (SymProdW.T k (vS (m + 1)) (m + 1 + 1)) := by
  refine ⟨fun c' f hf hcl => ?_, EisRecA.eis_recip_A j g hpr hg m t hℓ hN k' hq e hQ Me hqn hp s hs k (vS (m + 1)) hkc
    (vS_spec (m + 1))⟩
  obtain ⟨v, U, hv, hU, hU0⟩ := ValC5.A_val_all j Mp kp J₂ k₂ N'' hk hk₂ hM₂ hN0 hpr g hg (m + 1) t c' f hN hf hcl hℓ
  obtain ⟨d, hd⟩ := SymProdZ.le_ex (Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ hcl))
  have hvd : v = d := add_rc v d c' (by rw [hv, ← hd]; lib_nat)
  refine ⟨U, ?_, hU0⟩
  rw [vS_eq (m + 1) c' d hd, ← hvd]
  exact hU

end EisRecV

#print axioms EisRecV.subN_zero
#print axioms EisRecV.subN_add
#print axioms EisRecV.add_rc
#print axioms EisRecV.vS_eq
#print axioms EisRecV.vS_spec
#print axioms EisRecV.eis_recip_val

lang/EisRecJ.lean

/-
  lang/EisRecJ — T49 (every odd prime ℓ = m + 3, symbol form, no condition on M): **Eisenstein's reciprocity
  for A with the p-factor kept**. Over E = 𝔽_q[C_ℓ]/(N), u = [σ], q^f = ℓM + 1 (no (q − 1) | M):
    eis_recip_J   (φ_u(A)·J(ψ, ψ)·J(ψ̄, ψ̄))^M = u^(Σ_c k_c v_c)
  where (z^c)^(k_c) = (q^(−f))^((p−1)/ℓ) and v_c + c + 1 = ℓ: (A/𝔮)(J/𝔮)(J̄/𝔮) = (q^(−f)/A)_ℓ
  (lang/CycSym2.cyc_recip_J + lang/SymPsi.psi_stick). Test: work/data/t49/eisreca_test.py.
  Lean 4 core only, no axioms.
-/
import lang.SymPsi
import lang.CycSym2

namespace EisRecJ

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

/-- **Eisenstein's reciprocity for A, p kept as J J̄**. -/
theorem eis_recip_J (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (m t : Nat) (hℓ : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1)
    (hN : j + 1 + 1 = (m + 1 + 1 + 1) * (t + 1))
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (f : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ f) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ f = (m + 1 + 1 + 1) * M + 1)
    (k v : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ f) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (hv : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → v c + c + 1 = m + 1 + 1 + 1) :
    pw (CycRing.cyc (ring (k' + 1)) (m + 1 + 1))
        ((CycRing.cyc (ring (k' + 1)) (m + 1 + 1)).mul
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (m + 1 + 1) (CycUnit.u (ring (k' + 1)) (m + 1 + 1))
            (EisBridge.AA (gring LibInt.intR (m + 1 + 1)) j (CycPrim.chi (m + 1 + 1) j g) (m + 1)))
          ((CycRing.cyc (ring (k' + 1)) (m + 1 + 1)).mul
            (J2 (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) j
              (CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1)))
              (CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1))))
            (J2 (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) j
              (fun x => CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1)) (iv j x))
              (fun x => CycChar.psi (j + 1) g (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1)) (iv j x))))) M
      = pw (CycRing.cyc (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u (ring (k' + 1)) (m + 1 + 1)) (SymProdW.T k v (m + 1 + 1)) := by
  rw [← CycSym2.cyc_recip_J (m + 1) j g hpr hg t (Nat.succ_le_succ (Nat.zero_le m)) hℓ hN k' hq f hQ M hqn]
  exact SymPsi.psi_stick (j + 1) m t g hg hN hpr hℓ (CycRing.cyc (ring (k' + 1)) (m + 1 + 1))
    (CycUnit.u (ring (k' + 1)) (m + 1 + 1)) (CycUnit.u_ell (ring (k' + 1)) (m + 1 + 1)) _
    (DLog.pw_ne_zero (j + 1) hpr _ hQ (j + 1)) k v hk hv

end EisRecJ

#print axioms EisRecJ.eis_recip_J

lang/SymMul.lean

/-
  lang/SymMul — T49 (general ℓ, symbol form): **the symbol (x/A)_ℓ is multiplicative in x** (composite
  moduli). ℓ = m + 3, p − 1 = ℓ(t + 1), u^ℓ = 1, v_c + c + 1 = ℓ.
    T_add      Σ_c (k_c + k'_c) v_c = Σ_c k_c v_c + Σ_c k'_c v_c
    u_p1       u^(p−1) = 1
    sym_mul    ψ_u(xy) = u^(Σ_c k_c v_c)·u^(Σ_c k'_c v_c) = u^(Σ_c (k_c + k'_c) v_c)
               when (z^c)^(k_c) = x^((p−1)/ℓ) and (z^c)^(k'_c) = y^((p−1)/ℓ)
  so (xy/A)_ℓ = (x/A)_ℓ (y/A)_ℓ, and Eisenstein's reciprocity for A (lang/EisRecA) extends to products of primes.
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.SymPsi

namespace SymMul

open FTAChain ZMod YMChain FiniteRecChain

theorem T_add (k k' v : Nat → Nat) : ∀ n,
    SymProdW.T (fun c => k c + k' c) v n = SymProdW.T k v n + SymProdW.T k' v n
  | 0 => rfl
  | n + 1 => by
    show SymProdW.T (fun c => k c + k' c) v n + (k (n + 1) + k' (n + 1)) * v (n + 1)
      = SymProdW.T k v n + k (n + 1) * v (n + 1) + (SymProdW.T k' v n + k' (n + 1) * v (n + 1))
    rw [T_add k k' v n]
    generalize SymProdW.T k v n = a
    generalize SymProdW.T k' v n = b
    generalize k (n + 1) = x
    generalize k' (n + 1) = y
    generalize v (n + 1) = w
    lib_nat

theorem u_p1 (J m t : Nat) (hN : J + 1 = (m + 1 + 1 + 1) * (t + 1))
    {S : Type} (E : DetChain.CRing S) (u : S) (hu : Frobenius.pw E u (m + 1 + 1 + 1) = E.one) :
    Frobenius.pw E u (J + 1) = E.one := by
  have e : J + 1 = (t + 1) * (m + 1 + 1 + 1) := by rw [hN]; lib_nat
  rw [e]; exact SymProdW.pw_mulE E u _ hu (t + 1)

/-- **(xy/A)_ℓ = (x/A)_ℓ·(y/A)_ℓ** through ψ_u. -/
theorem sym_mul (J m t : Nat) (g : Zp (J + 1)) (hg : OrdMul.Ord (ring (J + 1)) g (J + 1))
    (hN : J + 1 = (m + 1 + 1 + 1) * (t + 1))
    (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
    (hℓ : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1)
    {S : Type} (E : DetChain.CRing S) (u : S) (hu : Frobenius.pw E u (m + 1 + 1 + 1) = E.one)
    (x y : Zp (J + 1)) (hx : x ≠ (ring (J + 1)).zero) (hy : y ≠ (ring (J + 1)).zero) (k k' v : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 →
      Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) g (t + 1)) c) (k c)
        = Frobenius.pw (ring (J + 1)) x (t + 1))
    (hk' : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 →
      Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) (Frobenius.pw (ring (J + 1)) g (t + 1)) c) (k' c)
        = Frobenius.pw (ring (J + 1)) y (t + 1))
    (hv : ∀ c, 1 ≤ c → c < m + 1 + 1 + 1 → v c + c + 1 = m + 1 + 1 + 1) :
    CycChar.psi J g E u ((ring (J + 1)).mul x y)
      = Frobenius.pw E u (SymProdW.T (fun c => k c + k' c) v (m + 1 + 1)) := by
  rw [CycChar.psi_mul J g E hpr hg u (u_p1 J m t hN E u hu) x y,
    SymPsi.psi_stick J m t g hg hN hpr hℓ E u hu x hx k v hk hv,
    SymPsi.psi_stick J m t g hg hN hpr hℓ E u hu y hy k' v hk' hv, T_add, SymProdW.pw_add]

end SymMul

#print axioms SymMul.T_add
#print axioms SymMul.u_p1
#print axioms SymMul.sym_mul

#print axioms: 36 theorems, no axioms.

78.376 Units of absolute value one in coefficient form

Integer vectors with \(\sum a_i^2=1+t\) and \((\sum a_i)^2=1+\ell t\) are a constant vector plus a signed unit vector (the coefficient form of \(u\bar u=1\Rightarrow u=\pm \zeta ^k\)).

\[ \sum _i(\ell a_i-s)^2=\ell ^2-\ell ,\qquad a=u\,(1,\dots ,1)\pm e_k. \]

lang/UnitCirc1.lean

/-
  lang/UnitCirc1 — T49 (O3') (iii), step 1: **the centred square sum of a unit of absolute value one**.
  For integers a_0, …, a_(ℓ−1) (the coefficients of u ∈ ℤ[C_ℓ]) with Σ a_i² = 1 + t and (Σ a_i)² = 1 + ℓt
  (the identity and augmentation coefficients of u·ū = 1 + t·N), the centred values b_i = ℓ a_i − Σ a satisfy
    sb_eq     Σ (L a_i − s)² = L² Σ a_i² − 2Ls Σ a_i + n s²         (n = number of terms)
    centred   Σ b_i² = ℓ² − ℓ
  Next (UnitCirc2): b_i ≡ −s, s² ≡ 1 (mod ℓ) ⇒ one b_i = ±(ℓ − 1), the others ∓1 ⇒ u ≡ ±σ^k (mod N).
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lib.IntRing

namespace UnitCirc1

def sq : List Int → Int
  | [] => 0
  | x :: xs => x * x + sq xs

def sm : List Int → Int
  | [] => 0
  | x :: xs => x + sm xs

def ln : List Int → Int
  | [] => 0
  | _ :: xs => 1 + ln xs

def sb (L s : Int) : List Int → Int
  | [] => 0
  | x :: xs => (L * x - s) * (L * x - s) + sb L s xs

theorem sb_eq (L s : Int) : ∀ l, sb L s l = L * L * sq l - 2 * L * s * sm l + ln l * s * s
  | [] => by show (0 : Int) = L * L * 0 - 2 * L * s * 0 + 0 * s * s; lib_int
  | x :: xs => by
    show (L * x - s) * (L * x - s) + sb L s xs
      = L * L * (x * x + sq xs) - 2 * L * s * (x + sm xs) + (1 + ln xs) * s * s
    rw [sb_eq L s xs]
    lib_int

/-- **Σ (ℓ a_i − Σ a)² = ℓ² − ℓ** when Σ a_i² = 1 + t and (Σ a_i)² = 1 + ℓt. -/
theorem centred (L t : Int) (l : List Int) (hn : ln l = L) (hq : sq l = 1 + t) (hs : sm l * sm l = 1 + L * t) :
    sb L (sm l) l = L * L - L := by
  rw [sb_eq, hn]
  calc L * L * sq l - 2 * L * sm l * sm l + L * sm l * sm l
      = L * L * sq l - L * (sm l * sm l) := by lib_int
    _ = L * L * (1 + t) - L * (1 + L * t) := by rw [hq, hs]
    _ = L * L - L := by lib_int

end UnitCirc1

#print axioms UnitCirc1.sb_eq
#print axioms UnitCirc1.centred

lang/UnitCirc2.lean

/-
  lang/UnitCirc2 — T49 (O3') (iii), step 2: **integers whose squares sum to 1 are a signed unit vector**.
    nsq, sq_cast     Σ |c_i|² in ℕ, and its cast is Σ c_i²
    abs0, abs1       |c| = 0 ⇒ c = 0;  |c| = 1 ⇒ c = ±1
    nat_sq0, nat_sq1 m² + R = 0 ⇒ m = 0, R = 0;  m² + R = 1 ⇒ (m = 1, R = 0) or (m = 0, R = 1)
    zeros, one_hot   Σ c_i² = 0 ⇒ all c_i = 0;  Σ c_i² = 1 ⇒ exactly one c_i = ±1, the rest 0
  With UnitCirc1 (Σ (ℓa_i − s)² = ℓ² − ℓ, b_i = ℓc_i + ε) this gives u ≡ ±σ^k (mod N) for u·ū = 1.
  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.UnitCirc1
import lib.Semi

namespace UnitCirc2

open UnitCirc1

def nsq : List Int → Nat
  | [] => 0
  | x :: xs => Int.natAbs x * Int.natAbs x + nsq xs

theorem sq_cast : ∀ l, Int.ofNat (nsq l) = sq l
  | [] => rfl
  | x :: xs => by
    show Int.ofNat (Int.natAbs x * Int.natAbs x + nsq xs) = x * x + sq xs
    rw [← sq_cast xs, ← (Int.natAbs_mul_self : Int.ofNat (Int.natAbs x * Int.natAbs x) = x * x)] <;> rfl

theorem abs0 : ∀ c : Int, Int.natAbs c = 0 → c = 0
  | Int.ofNat n, h => by
    have : n = 0 := h
    rw [this] <;> rfl
  | Int.negSucc n, h => Nat.noConfusion h

theorem abs1 : ∀ c : Int, Int.natAbs c = 1 → c = 1 ∨ c = -1
  | Int.ofNat n, h => by
    have : n = 1 := h
    rw [this]; exact Or.inl rfl
  | Int.negSucc n, h => by
    have : n + 1 = 1 := h
    have hn : n = 0 := Nat.succ.inj this
    rw [hn]; exact Or.inr rfl

theorem nat_sq0 (m R : Nat) (h : m * m + R = 0) : m = 0 ∧ R = 0 := by
  cases m with
  | zero =>
    have e : 0 * 0 + R = R := by lib_nat
    exact ⟨rfl, e.symm.trans h⟩
  | succ m =>
    have e : (m + 1) * (m + 1) + R = (m * m + 2 * m + R) + 1 := by lib_nat
    exact Nat.noConfusion (e.symm.trans h)

theorem nat_sq1 (m R : Nat) (h : m * m + R = 1) : (m = 1 ∧ R = 0) ∨ (m = 0 ∧ R = 1) := by
  cases m with
  | zero =>
    have e : 0 * 0 + R = R := by lib_nat
    exact Or.inr ⟨rfl, e.symm.trans h⟩
  | succ m =>
    cases m with
    | zero =>
      have e : (0 + 1) * (0 + 1) + R = R + 1 := by lib_nat
      exact Or.inl ⟨rfl, Nat.succ.inj ((e.symm.trans h))⟩
    | succ m =>
      have e : (m + 1 + 1) * (m + 1 + 1) + R = (m * m + 4 * m + R + 3) + 1 := by lib_nat
      have h2 : m * m + 4 * m + R + 3 = 0 := Nat.succ.inj (e.symm.trans h)
      exact Nat.noConfusion h2

def Zeros : List Int → Prop
  | [] => True
  | x :: xs => x = 0 ∧ Zeros xs

def OneHot : List Int → Prop
  | [] => False
  | x :: xs => ((x = 1 ∨ x = -1) ∧ Zeros xs) ∨ (x = 0 ∧ OneHot xs)

theorem zeros_n : ∀ l, nsq l = 0 → Zeros l
  | [], _ => trivial
  | x :: xs, h => by
    obtain ⟨h1, h2⟩ := nat_sq0 _ _ h
    exact ⟨abs0 x h1, zeros_n xs h2⟩

theorem one_hot_n : ∀ l, nsq l = 1 → OneHot l
  | [], h => Nat.noConfusion h
  | x :: xs, h => by
    cases nat_sq1 _ _ h with
    | inl h' => exact Or.inl ⟨abs1 x h'.1, zeros_n xs h'.2⟩
    | inr h' => exact Or.inr ⟨abs0 x h'.1, one_hot_n xs h'.2⟩

/-- **Σ c_i² = 1 ⇒ exactly one c_i = ±1 and all others 0**. -/
theorem one_hot (l : List Int) (h : sq l = 1) : OneHot l :=
  one_hot_n l (Int.ofNat.inj ((sq_cast l).trans h))

end UnitCirc2

#print axioms UnitCirc2.sq_cast
#print axioms UnitCirc2.abs0
#print axioms UnitCirc2.abs1
#print axioms UnitCirc2.nat_sq0
#print axioms UnitCirc2.nat_sq1
#print axioms UnitCirc2.zeros_n
#print axioms UnitCirc2.one_hot_n
#print axioms UnitCirc2.one_hot

lang/UnitCirc3.lean

/-
  lang/UnitCirc3 — T49 (O3') (iii), step 3: **an integer vector with Σ a_i² = 1 + t, (Σ a_i)² = 1 + ℓt (ℓ terms,
  ℓ prime) is constant plus a signed unit vector** — the coefficient form of "u·ū = 1 ⇒ u = ±ζ^k" in ℤ[C_ℓ]/(N).
    mul_cancel    L·x = L·y, L = ℓ ≠ 0 ⇒ x = y                   (LibInt.int_noZD)
    sq_shift      Σ (a_i − u)² = Σ a_i² − 2u Σ a_i + n u²
    sm_shift      Σ (a_i − u) = Σ a_i − n u
    s_pm          s² = 1 + ℓt ⇒ s = 1 + ℓu or s = −1 + ℓu     (FermatUnique.int_euclid)
    unit_vec      ⇒ ∃ u ε, ε = ±1, Σ (a_i − u) = ε and (a_i − u) is a signed unit vector (UnitCirc2.OneHot)
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.UnitCirc2
import lang.FermatUnique

namespace UnitCirc3

open UnitCirc1 UnitCirc2

theorem mul_cancel (p : Nat) (x y : Int) (h : Int.ofNat (p + 1 + 1) * x = Int.ofNat (p + 1 + 1) * y) : x = y := by
  have h0 : LibInt.intR.mul (Int.ofNat (p + 1 + 1)) (x - y) = LibInt.intR.zero := by
    show Int.ofNat (p + 1 + 1) * (x - y) = 0
    calc Int.ofNat (p + 1 + 1) * (x - y) = Int.ofNat (p + 1 + 1) * x - Int.ofNat (p + 1 + 1) * y := by lib_int
      _ = Int.ofNat (p + 1 + 1) * y - Int.ofNat (p + 1 + 1) * y := by rw [h]
      _ = 0 := by lib_int
  cases LibInt.int_noZD _ _ h0 with
  | inl hL => exact Nat.noConfusion (Int.ofNat.inj hL)
  | inr hd =>
    have hd' : x - y = 0 := hd
    calc x = (x - y) + y := by lib_int
      _ = 0 + y := by rw [hd']
      _ = y := by lib_int

theorem sq_shift (u : Int) : ∀ l, sq (l.map (fun a => a - u)) = sq l - 2 * u * sm l + ln l * u * u
  | [] => by show (0 : Int) = 0 - 2 * u * 0 + 0 * u * u; lib_int
  | x :: xs => by
    show (x - u) * (x - u) + sq (xs.map (fun a => a - u)) = (x * x + sq xs) - 2 * u * (x + sm xs) + (1 + ln xs) * u * u
    rw [sq_shift u xs]; lib_int

theorem sm_shift (u : Int) : ∀ l, sm (l.map (fun a => a - u)) = sm l - ln l * u
  | [] => by show (0 : Int) = 0 - 0 * u; lib_int
  | x :: xs => by
    show (x - u) + sm (xs.map (fun a => a - u)) = (x + sm xs) - (1 + ln xs) * u
    rw [sm_shift u xs]; lib_int

/-- **s² = 1 + ℓt ⇒ s ≡ ±1 (mod ℓ)**. -/
theorem s_pm (p : Nat) (hp : ∀ d, FTAChain.Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (s t : Int)
    (h : s * s = 1 + Int.ofNat (p + 1 + 1) * t) :
    (∃ u, s = 1 + Int.ofNat (p + 1 + 1) * u) ∨ (∃ u, s = -1 + Int.ofNat (p + 1 + 1) * u) := by
  have h2 : (s - 1) * (s + 1) = Int.ofNat (p + 1 + 1) * t := by
    calc (s - 1) * (s + 1) = s * s - 1 := by lib_int
      _ = (1 + Int.ofNat (p + 1 + 1) * t) - 1 := by rw [h]
      _ = Int.ofNat (p + 1 + 1) * t := by lib_int
  cases FermatUnique.int_euclid p hp (s - 1) (s + 1) t h2 with
  | inl h' =>
    obtain ⟨u, hu⟩ := h'
    exact Or.inl ⟨u, by calc s = (s - 1) + 1 := by lib_int
      _ = Int.ofNat (p + 1 + 1) * u + 1 := by rw [hu]
      _ = 1 + Int.ofNat (p + 1 + 1) * u := by lib_int⟩
  | inr h' =>
    obtain ⟨u, hu⟩ := h'
    exact Or.inr ⟨u, by calc s = (s + 1) - 1 := by lib_int
      _ = Int.ofNat (p + 1 + 1) * u - 1 := by rw [hu]
      _ = -1 + Int.ofNat (p + 1 + 1) * u := by lib_int⟩

/-- the shifted vector for s = ε + ℓu: Σ c = ε and Σ c² = 1. -/
theorem shift_sums (p : Nat) (l : List Int) (t u ε : Int) (hε : ε * ε = 1)
    (hn : ln l = Int.ofNat (p + 1 + 1)) (hq : sq l = 1 + t)
    (hs2 : sm l * sm l = 1 + Int.ofNat (p + 1 + 1) * t) (hs : sm l = ε + Int.ofNat (p + 1 + 1) * u) :
    sm (l.map (fun a => a - u)) = ε ∧ sq (l.map (fun a => a - u)) = 1 := by
  refine ⟨by rw [sm_shift, hn, hs]; lib_int, ?_⟩
  have ht : t = 2 * ε * u + Int.ofNat (p + 1 + 1) * u * u := by
    apply mul_cancel p
    have e1 : Int.ofNat (p + 1 + 1) * t = sm l * sm l - 1 := by rw [hs2]; lib_int
    rw [e1, hs]
    calc (ε + Int.ofNat (p + 1 + 1) * u) * (ε + Int.ofNat (p + 1 + 1) * u) - 1
        = ε * ε - 1 + Int.ofNat (p + 1 + 1) * (2 * ε * u + Int.ofNat (p + 1 + 1) * u * u) := by lib_int
      _ = 1 - 1 + Int.ofNat (p + 1 + 1) * (2 * ε * u + Int.ofNat (p + 1 + 1) * u * u) := by rw [hε]
      _ = Int.ofNat (p + 1 + 1) * (2 * ε * u + Int.ofNat (p + 1 + 1) * u * u) := by lib_int
  rw [sq_shift, hq, hn, hs, ht]
  calc 1 + (2 * ε * u + Int.ofNat (p + 1 + 1) * u * u) - 2 * u * (ε + Int.ofNat (p + 1 + 1) * u)
        + Int.ofNat (p + 1 + 1) * u * u = 1 := by lib_int

/-- **u·ū = 1 in coefficient form ⇒ constant + signed unit vector**: Σ a_i² = 1 + t, (Σ a_i)² = 1 + ℓt, ℓ terms ⇒
  for some u and ε = ±1, the vector (a_i − u) has sum ε and is ±(unit vector). -/
theorem unit_vec (p : Nat) (hp : ∀ d, FTAChain.Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (l : List Int) (t : Int)
    (hn : ln l = Int.ofNat (p + 1 + 1)) (hq : sq l = 1 + t) (hs2 : sm l * sm l = 1 + Int.ofNat (p + 1 + 1) * t) :
    ∃ u ε, (ε = 1 ∨ ε = -1) ∧ sm (l.map (fun a => a - u)) = ε ∧ OneHot (l.map (fun a => a - u)) := by
  cases s_pm p hp (sm l) t hs2 with
  | inl h =>
    obtain ⟨u, hu⟩ := h
    obtain ⟨h1, h2⟩ := shift_sums p l t u 1 rfl hn hq hs2 hu
    exact ⟨u, 1, Or.inl rfl, h1, one_hot _ h2⟩
  | inr h =>
    obtain ⟨u, hu⟩ := h
    obtain ⟨h1, h2⟩ := shift_sums p l t u (-1) rfl hn hq hs2 hu
    exact ⟨u, -1, Or.inr rfl, h1, one_hot _ h2⟩

end UnitCirc3

#print axioms UnitCirc3.mul_cancel
#print axioms UnitCirc3.sq_shift
#print axioms UnitCirc3.sm_shift
#print axioms UnitCirc3.s_pm
#print axioms UnitCirc3.shift_sums
#print axioms UnitCirc3.unit_vec

#print axioms: 16 theorems, no axioms.

78.377 An element of norm \(p\) lies in exactly one prime \(P_c\)

Evaluating the norm at the Teichmüller root multiplies the evaluations at all \(P_c\); in \(\mathbb Z/p^2\) a product equal to \(p\) has exactly one factor divisible by \(p\).

\[ N(\alpha )=p\ \Longrightarrow \ \#\{c:\ \varphi _{z^c}(\alpha )\equiv 0 \pmod p\}=1. \]

lang/ExactOne.lean

/-
  lang/ExactOne — T49 (general primary α, step (O1')): **a product equal to P with P² = 0 has exactly one factor
  divisible by P**. Abstract form over any commutative ring R with P·P = 0, P ≠ 0 and P not a unit, for a list of
  factors each flagged (d x = true ⇒ x ∈ P·R, d x = false ⇒ x a unit):
    pr, cnt          product and number of flagged factors
    pr_form          pr xs = P^(cnt xs)·w
    pr_unit          cnt xs = 0 ⇒ pr xs is a unit
    exact_one        pr xs = P ⇒ cnt xs = 1
  Use: R = ℤ/p², P = p, xs = (ι_c(α))_c with Π_c ι_c(α) = ι(N(α)) = p — exactly one P_c divides α, once
  (test: work/data/t49/normp_test.py).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.SymProdW

namespace ExactOne

open FTAChain YMChain FiniteRecChain

variable {R : Type} (K : DetChain.CRing R)

def pr : List R → R
  | [] => K.one
  | x :: xs => K.mul x (pr xs)

def cnt (d : R → Bool) : List R → Nat
  | [] => 0
  | x :: xs => (if d x = true then 1 else 0) + cnt d xs

variable (P : R) (d : R → Bool) (hP : ∀ x, d x = true → ∃ y, x = K.mul P y)
  (hU : ∀ x, d x = false → ∃ w, K.mul x w = K.one)

include hP in
theorem pr_form : ∀ xs : List R, ∃ w, pr K xs = K.mul (Frobenius.pw K P (cnt d xs)) w
  | [] => ⟨K.one, by show K.one = K.mul (Frobenius.pw K P 0) K.one; show K.one = K.mul K.one K.one; lib_ring⟩
  | x :: xs => by
    obtain ⟨w, hw⟩ := pr_form xs
    show ∃ w', K.mul x (pr K xs) = K.mul (Frobenius.pw K P ((if d x = true then 1 else 0) + cnt d xs)) w'
    rw [hw]
    cases hx : d x with
    | true =>
      obtain ⟨y, hy⟩ := hP x hx
      refine ⟨K.mul y w, ?_⟩
      have e : 1 + cnt d xs = cnt d xs + 1 := by lib_nat
      rw [ite_pos' _ _ rfl, e, SymProdW.pw_add, hy]
      show K.mul (K.mul P y) (K.mul (Frobenius.pw K P (cnt d xs)) w)
        = K.mul (K.mul (Frobenius.pw K P (cnt d xs)) (K.mul K.one P)) (K.mul y w)
      lib_ring
    | false =>
      refine ⟨K.mul x w, ?_⟩
      have e : 0 + cnt d xs = cnt d xs := by lib_nat
      rw [ite_neg' _ _ (fun h => Bool.noConfusion h), e]
      lib_ring

include hU in
theorem pr_unit : ∀ xs : List R, cnt d xs = 0 → ∃ w, K.mul (pr K xs) w = K.one
  | [], _ => ⟨K.one, by show K.mul K.one K.one = K.one; lib_ring⟩
  | x :: xs, h => by
    cases hx : d x with
    | true =>
      have h' : (if d x = true then 1 else 0) + cnt d xs = 0 := h
      rw [hx, ite_pos' _ _ rfl] at h'
      have e : cnt d xs + 1 = 0 := (show cnt d xs + 1 = 1 + cnt d xs by lib_nat).trans h'
      exact Nat.noConfusion e
    | false =>
      have h' : (if d x = true then 1 else 0) + cnt d xs = 0 := h
      have e : 0 + cnt d xs = cnt d xs := by lib_nat
      rw [hx, ite_neg' _ _ (fun h => Bool.noConfusion h), e] at h'
      obtain ⟨w, hw⟩ := pr_unit xs h'
      obtain ⟨u, hu⟩ := hU x hx
      refine ⟨K.mul w u, ?_⟩
      show K.mul (K.mul x (pr K xs)) (K.mul w u) = K.one
      calc K.mul (K.mul x (pr K xs)) (K.mul w u) = K.mul (K.mul (pr K xs) w) (K.mul x u) := by lib_ring
        _ = K.one := by rw [hw, hu]; lib_ring

include hP hU in
/-- **exactly one factor is divisible by P** when the product is P, P² = 0, P ≠ 0, P not a unit. -/
theorem exact_one (hPP : K.mul P P = K.zero) (hP0 : P ≠ K.zero) (hPu : ∀ w, K.mul P w ≠ K.one)
    (xs : List R) (hx : pr K xs = P) : cnt d xs = 1 := by
  obtain ⟨w, hw⟩ := pr_form K P d hP xs
  match hc : cnt d xs with
  | 0 =>
    obtain ⟨u, hu⟩ := pr_unit K d hU xs hc
    rw [hx] at hu
    exact absurd hu (hPu u)
  | 1 => rfl
  | c + 2 =>
    rw [hc] at hw
    have h0 : Frobenius.pw K P (c + 2) = K.zero := by
      show K.mul (K.mul (Frobenius.pw K P c) P) P = K.zero
      rw [K.mul_assoc, hPP]; lib_ring
    rw [h0, hx] at hw
    exact (hP0 (hw.trans (show K.mul K.zero w = K.zero by lib_ring))).elim

end ExactOne

#print axioms ExactOne.pr_form
#print axioms ExactOne.pr_unit
#print axioms ExactOne.exact_one

lang/ExactP2.lean

/-
  lang/ExactP2 — T49 (general primary α, step (O1')): **in ℤ/p², a product of factors equal to p has exactly one
  factor divisible by p** (lang/ExactOne over R = ℤ/p², P = p·1). M + 1 = p², p = J + 2 prime.
    red_P        red(p·1) = 0 in 𝔽_p
    unit_p2      red(x) ≠ 0 ⇒ x is a unit in ℤ/p²    (x·y = 1 + P t with y a lift of red(x)^(−1); then (1 + Pt)(1 − Pt) = 1)
    P_ne         p·1 ≠ 0 in ℤ/p²
    P_nunit      p·w ≠ 1 in ℤ/p²
    exact_one_p2 Π xs = p ⇒ #{x ∈ xs : red(x) = 0} = 1
  With xs = (ι_c(α))_c and Π_c ι_c(α) = ι(N(α)) = p: exactly one P_c divides α (test: work/data/t49/normp_test.py).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ExactOne
import lang.RedKer
import lang.TeichChar2
import lang.StickVal

namespace ExactP2

open FTAChain ZMod YMChain FiniteRecChain Frobenius

variable (J M : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
  (hM : M + 1 = (J + 1 + 1) * (J + 1 + 1))

/-- P = p·1 in ℤ/p². -/
def P : Zp M := nsm (ring M) (J + 1 + 1) (ring M).one

include hM in
theorem red_P : RedP2.red J M (P J M) = (ring (J + 1)).zero := Subtype.ext (by
  show md (J + 1) (nsm (ring M) (J + 1 + 1) (ring M).one).val = md (J + 1) 0
  rw [nsm_one_val, RedP2.mdmd J M (J + 1 + 1) hM, md_self]
  rfl)

include hpr hM in
/-- **red(x) ≠ 0 ⇒ x is a unit in ℤ/p²**. -/
theorem unit_p2 (x : Zp M) (hx : RedP2.red J M x ≠ (ring (J + 1)).zero) :
    ∃ w, (ring M).mul x w = (ring M).one := by
  have hinv := inv_exists J hpr (RedP2.red J M x) hx
  let y := TeichChar.lift J M (pw (ring (J + 1)) (RedP2.red J M x) J)
  have hy : RedP2.red J M y = pw (ring (J + 1)) (RedP2.red J M x) J := TeichChar.red_lift J M hM _
  have h0 : RedP2.red J M ((ring M).add ((ring M).mul x y) ((ring M).neg (ring M).one)) = (ring (J + 1)).zero := by
    rw [RedP2.red_add J M (J + 1 + 1) hM, RedP2.red_mul J M (J + 1 + 1) hM, TeichChar.red_neg J M hM,
      RedP2.red_one J M (J + 1 + 1) hM, hy]
    calc (ring (J + 1)).add ((ring (J + 1)).mul (RedP2.red J M x) (pw (ring (J + 1)) (RedP2.red J M x) J))
          ((ring (J + 1)).neg (ring (J + 1)).one)
        = (ring (J + 1)).add ((ring (J + 1)).mul (pw (ring (J + 1)) (RedP2.red J M x) J) (RedP2.red J M x))
          ((ring (J + 1)).neg (ring (J + 1)).one) := by lib_ring
      _ = (ring (J + 1)).zero := by rw [hinv]; lib_ring
  obtain ⟨t, ht⟩ := RedP2.red_ker J M _ h0
  have hPP := RedP2.P_sq J M hM
  refine ⟨(ring M).mul y ((ring M).add (ring M).one ((ring M).neg ((ring M).mul (P J M) t))), ?_⟩
  calc (ring M).mul x ((ring M).mul y ((ring M).add (ring M).one ((ring M).neg ((ring M).mul (P J M) t))))
      = (ring M).mul ((ring M).add ((ring M).add ((ring M).mul x y) ((ring M).neg (ring M).one)) (ring M).one)
          ((ring M).add (ring M).one ((ring M).neg ((ring M).mul (P J M) t))) := by lib_ring
    _ = (ring M).mul ((ring M).add ((ring M).mul (P J M) t) (ring M).one)
          ((ring M).add (ring M).one ((ring M).neg ((ring M).mul (P J M) t))) :=
          congrArg (fun z => (ring M).mul ((ring M).add z (ring M).one)
            ((ring M).add (ring M).one ((ring M).neg ((ring M).mul (P J M) t)))) ht
    _ = (ring M).add (ring M).one ((ring M).neg ((ring M).mul ((ring M).mul (P J M) (P J M)) ((ring M).mul t t))) := by
          lib_ring
    _ = (ring M).one := by
          show (ring M).add (ring M).one ((ring M).neg ((ring M).mul
            ((ring M).mul (nsm (ring M) (J + 1 + 1) (ring M).one) (nsm (ring M) (J + 1 + 1) (ring M).one))
            ((ring M).mul t t))) = (ring M).one
          rw [hPP]; lib_ring

include hM in
theorem P_ne : P J M ≠ (ring M).zero :=
  StickVal.small_ne_zero M (J + 1) (by
    have e : (J + 1 + 1) * (J + 1 + 1) = (J + 1 + 1 + 1) + (J * J + 3 * J + 1) := by lib_nat
    have h3 : J + 1 + 1 + 1 ≤ (J + 1 + 1) * (J + 1 + 1) := by rw [e]; exact Nat.le_add_right _ _
    rw [hM]; exact h3)

include hM in
theorem P_nunit (w : Zp M) : (ring M).mul (P J M) w ≠ (ring M).one := fun h => by
  have h1 := congrArg (RedP2.red J M) h
  rw [RedP2.red_mul J M (J + 1 + 1) hM, red_P J M hM, RedP2.red_one J M (J + 1 + 1) hM] at h1
  exact PowerSum.one_ne_zero' J (h1.symm.trans
    (show (ring (J + 1)).mul (ring (J + 1)).zero (RedP2.red J M w) = (ring (J + 1)).zero by lib_ring))

/-- the flag: red(x) = 0. -/
def d (x : Zp M) : Bool := decide (RedP2.red J M x = (ring (J + 1)).zero)

include hpr hM in
/-- **exactly one factor is divisible by p** when the product in ℤ/p² is p. -/
theorem exact_one_p2 (xs : List (Zp M)) (hx : ExactOne.pr (ring M) xs = P J M) : ExactOne.cnt (d J M) xs = 1 :=
  ExactOne.exact_one (ring M) (P J M) (d J M)
    (fun x h => RedP2.red_ker J M x (of_decide_eq_true h))
    (fun x h => unit_p2 J M hpr hM x (of_decide_eq_false h))
    (RedP2.P_sq J M hM) (P_ne J M hM) (P_nunit J M hM) xs hx

end ExactP2

#print axioms ExactP2.red_P
#print axioms ExactP2.unit_p2
#print axioms ExactP2.P_ne
#print axioms ExactP2.P_nunit
#print axioms ExactP2.exact_one_p2

lang/NormEval.lean

/-
  lang/NormEval — T49 (general primary α, step (O1')): **the evaluations of α at the primes above p multiply to
  the evaluation of its norm**, and a norm-p element is divisible by exactly one P_c, once.
  ℓ = m + 1, V = ℤ[C_ℓ], σ_d = CycGal.gsig d, ps = list of pairs (c, d) with c·d = 1 in 𝔽_ℓ.
    nrm ps v        Π_((c,d) ∈ ps) σ_d(v)                     (the norm when ps runs over all of 𝔽_ℓ^×)
    evs ps v        the list (φ_(z^c)(v))_((c,d) ∈ ps)
    eval_norm       φ_z(nrm ps v) = Π evs ps v                 (CycGalEv.phz_gsig, CycEis.phz_mul)
    phz_nsm         φ_z(n·1) = n·1
    norm_p_one      in ℤ/p² at a root z with z^ℓ = 1: nrm ps α = p·1 ⇒ exactly one φ_(z^c)(α) is ≡ 0 (mod p)
                    (lang/ExactP2; test work/data/t49/normp_test.py)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycGalEv
import lang.ExactP2

namespace NormEval

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

def AllInv (m : Nat) : List (Zp m × Zp m) → Prop
  | [] => True
  | p :: ps => (ring m).mul p.1 p.2 = (ring m).one ∧ AllInv m ps

def nrm (m : Nat) (v : V Int (m + 1)) : List (Zp m × Zp m) → V Int (m + 1)
  | [] => (gring LibInt.intR m).one
  | p :: ps => (gring LibInt.intR m).mul (CycGal.gsig LibInt.intR m p.2 v) (nrm m v ps)

def evs {S : Type} (E : CRing S) (m : Nat) (z : S) (v : V Int (m + 1)) (ps : List (Zp m × Zp m)) : List S :=
  ps.map (fun p => CycEis.phz E m (pw E z p.1.val) v)

/-- **φ_z(Π σ_d v) = Π φ_(z^c)(v)**. -/
theorem eval_norm {S : Type} (E : CRing S) (m : Nat) (z : S) (hz : pw E z (m + 1) = E.one) (v : V Int (m + 1)) :
    ∀ ps, AllInv m ps → CycEis.phz E m z (nrm m v ps) = ExactOne.pr E (evs E m z v ps)
  | [], _ => CycEis.phz_one E m z
  | p :: ps, ⟨h1, h2⟩ => by
    show CycEis.phz E m z ((gring LibInt.intR m).mul (CycGal.gsig LibInt.intR m p.2 v) (nrm m v ps))
      = E.mul (CycEis.phz E m (pw E z p.1.val) v) (ExactOne.pr E (evs E m z v ps))
    rw [CycEis.phz_mul E m z hz, CycGalEv.phz_gsig E m z hz p.1 p.2 h1 v, eval_norm E m z hz v ps h2]

theorem phz_nsm {S : Type} (E : CRing S) (m : Nat) (z : S) :
    ∀ n, CycEis.phz E m z (nsm (gring LibInt.intR m) n (gring LibInt.intR m).one) = nsm E n E.one
  | 0 => CycEis.phz_zero E m z
  | n + 1 => by
    show CycEis.phz E m z ((gring LibInt.intR m).add (nsm (gring LibInt.intR m) n (gring LibInt.intR m).one)
        (gring LibInt.intR m).one) = E.add (nsm E n E.one) E.one
    rw [CycEis.phz_add, phz_nsm E m z n, CycEis.phz_one]

/-- **a norm-p element is divisible by exactly one P_c**: in ℤ/p² (M + 1 = p², p = J + 2), z^ℓ = 1,
  nrm ps α = p·1 ⇒ exactly one evaluation φ_(z^c)(α), (c, d) ∈ ps, reduces to 0 mod p. -/
theorem norm_p_one (J M : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
    (hM : M + 1 = (J + 1 + 1) * (J + 1 + 1)) (m : Nat) (z : Zp M) (hz : pw (ring M) z (m + 1) = (ring M).one)
    (α : V Int (m + 1)) (ps : List (Zp m × Zp m)) (hps : AllInv m ps)
    (hN : nrm m α ps = nsm (gring LibInt.intR m) (J + 1 + 1) (gring LibInt.intR m).one) :
    ExactOne.cnt (ExactP2.d J M) (evs (ring M) m z α ps) = 1 := by
  apply ExactP2.exact_one_p2 J M hpr hM
  rw [← eval_norm (ring M) m z hz α ps hps, hN, phz_nsm]
  rfl

end NormEval

#print axioms NormEval.eval_norm
#print axioms NormEval.phz_nsm
#print axioms NormEval.norm_p_one

#print axioms: 11 theorems, no axioms.

78.378 Tools for the element-level Stickelberger relation

Over any commutative ring: polynomials vanishing at points with unit differences, units of \(\mathbb Z/p^k\), constant-coefficient criterion, and the paired product \(B\,\sigma _{-1}(B)\).

\[ B\,\sigma _{-1}(B)=\Bigl (\prod _d\sigma _d\alpha \,\sigma _{-d}\alpha \Bigr )^{k},\qquad u\bar u\equiv 1 \pmod N\Rightarrow u\equiv \pm \sigma ^k. \]

lang/UnitRoots.lean

/-
  lang/UnitRoots — T49 (O3') (i'), tool: **a polynomial with n + 1 coefficients vanishing at n + 1 points whose
  pairwise differences are units is zero** — over any commutative ring (no field, no NoZD), e.g. ℤ/p^k at the
  Teichmüller roots z^c (differences z^a − z^b are units mod p). RootBound's synthetic division:
    Zeros, UDist, AllRoot   all coefficients 0; pairwise unit differences (later minus earlier); all r are roots
    zeros_div               divQ r acc t all 0 and divR r acc t = 0 ⇒ acc = 0 and t all 0
    roots_q                 f(r) = 0, s − r a unit, f(s) = 0 ⇒ (f / (x − r))(s) = 0
    unit_roots              |f| = n + 1, |rs| = n + 1, UDist rs, AllRoot f rs ⇒ Zeros f
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.RootBound
import lib.RingTac

namespace UnitRoots

open Frobenius RootBound YMChain

variable {R : Type} (K : DetChain.CRing R)

def Zeros : List R → Prop
  | [] => True
  | a :: t => a = K.zero ∧ Zeros t

def UDist : List R → Prop
  | [] => True
  | r :: rs => (∀ s, memL s rs → ∃ w, K.mul (K.add s (K.neg r)) w = K.one) ∧ UDist rs

def AllRoot (f : List R) : List R → Prop
  | [] => True
  | r :: rs => evH K r f = K.zero ∧ AllRoot f rs

theorem zeros_div (r : R) : ∀ (t : List R) (acc : R), Zeros K (divQ K r acc t) → divR K r acc t = K.zero →
    acc = K.zero ∧ Zeros K t
  | [], acc, _, h => ⟨h, trivial⟩
  | b :: t, acc, ⟨h0, hq⟩, h => by
    obtain ⟨h1, h2⟩ := zeros_div r t (K.add b (K.mul r acc)) hq h
    refine ⟨h0, ?_, h2⟩
    calc b = K.add (K.add b (K.mul r acc)) (K.neg (K.mul r acc)) := by lib_ring
      _ = K.add K.zero (K.neg (K.mul r K.zero)) := by rw [h1, h0]
      _ = K.zero := by lib_ring

theorem roots_q (r acc : R) (t : List R) (hr : evH K r (acc :: t) = K.zero) :
    ∀ rs : List R, (∀ s, memL s rs → ∃ w, K.mul (K.add s (K.neg r)) w = K.one) → AllRoot K (acc :: t) rs →
      AllRoot K (divQ K r acc t) rs
  | [], _, _ => trivial
  | s :: rs, hu, ⟨hs, hrs⟩ => by
    refine ⟨?_, roots_q r acc t hr rs (fun s' h' => hu s' (Or.inr h')) hrs⟩
    obtain ⟨w, hw⟩ := hu s (Or.inl rfl)
    have hd := div_identity K s r t acc
    rw [rem_root K r acc t, hr, hs] at hd
    calc evH K s (divQ K r acc t)
        = K.mul (K.mul (K.add s (K.neg r)) w) (evH K s (divQ K r acc t)) := by rw [hw]; lib_ring
      _ = K.mul w (K.add (K.mul (K.add s (K.neg r)) (evH K s (divQ K r acc t))) K.zero) := by lib_ring
      _ = K.mul w K.zero := by rw [← hd]
      _ = K.zero := by lib_ring

/-- **n + 1 roots with unit differences kill a polynomial with n + 1 coefficients**. -/
theorem unit_roots : ∀ (n : Nat) (acc : R) (t : List R), t.length = n → ∀ rs : List R, rs.length = n + 1 →
    UDist K rs → AllRoot K (acc :: t) rs → acc = K.zero ∧ Zeros K t
  | _, _, _, _, [], hl, _, _ => Nat.noConfusion hl
  | 0, acc, t, ht, r :: _, _, _, ha => by
    have hdr : divR K r acc t = K.zero := by rw [rem_root]; exact ha.1
    cases t with
    | nil => exact ⟨hdr, trivial⟩
    | cons b t' => exact Nat.noConfusion ht
  | n + 1, acc, t, ht, r :: rs, hl, hd, ha => by
    cases t with
    | nil => exact Nat.noConfusion ht
    | cons b t' =>
      have hdr : divR K r acc (b :: t') = K.zero := by rw [rem_root]; exact ha.1
      have hq := roots_q K r acc (b :: t') ha.1 rs hd.1 ha.2
      have ht'' : (divQ K r (K.add b (K.mul r acc)) t').length = n := by
        rw [len_divQ]; exact Nat.succ.inj ht
      obtain ⟨h1, h2⟩ := unit_roots n acc (divQ K r (K.add b (K.mul r acc)) t') ht'' rs (Nat.succ.inj hl) hd.2 hq
      exact zeros_div K r (b :: t') acc ⟨h1, h2⟩ hdr

end UnitRoots

#print axioms UnitRoots.zeros_div
#print axioms UnitRoots.roots_q
#print axioms UnitRoots.unit_roots

lang/GsUnit.lean

/-
  lang/GsUnit — T49 (O3') (i') (a): **Σ_(i<n) w^i = 0 when w^n = 1 and w − 1 is a unit** — in any commutative ring
  (no zero-divisor hypothesis), e.g. ℤ/p^k at w = z^c with red(z^c) ≠ 1 (CycEis.gs_mul: (w − 1)·Σ w^i = w^n − 1).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycEis

namespace GsUnit

open DetChain Frobenius

theorem gs_unit {S : Type} (E : CRing S) (w u : S) (hu : E.mul (E.add w (E.neg E.one)) u = E.one) (n : Nat)
    (hwn : pw E w n = E.one) : CycChar.gs E w n = E.zero := by
  have h := CycEis.gs_mul E w n
  rw [hwn] at h
  calc CycChar.gs E w n = E.mul (E.mul (E.add w (E.neg E.one)) u) (CycChar.gs E w n) := by rw [hu]; lib_ring
    _ = E.mul u (E.mul (E.add w (E.neg E.one)) (CycChar.gs E w n)) := by lib_ring
    _ = E.mul u (E.add E.one (E.neg E.one)) := by rw [h]
    _ = E.zero := by lib_ring

end GsUnit

#print axioms GsUnit.gs_unit

lang/CoefList.lean

/-
  lang/CoefList — T49 (O3') (i') (b), bridge: **Σ_b f(b) z^b as a RootBound polynomial value**.
  For f : 𝔽_ℓ → S (the coefficients of x ∈ ℤ[C_ℓ] in a ring S), cl f n = [f(n−1), …, f(0)] (leading first):
    len_cl      |cl f n| = n
    ev_cl       evH z (cl f n) = Σ_(b ∈ allZ n) f(b)·z^b         (n ≤ ℓ)
  so φ_z(x) = evH z (cl x ℓ) and UnitRoots applies to the evaluations at the points z^c.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.UnitRoots
import lang.CMInert

namespace CoefList

open FTAChain ZMod YMChain FiniteRecChain Frobenius RootBound

variable {S : Type} (E : DetChain.CRing S) (m : Nat)

def cl (f : Zp m → S) : Nat → List S
  | 0 => []
  | k + 1 => f (mk m k) :: cl f k

theorem len_cl (f : Zp m → S) : ∀ n, (cl m f n).length = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (len_cl f n)

theorem ev_cl (f : Zp m → S) (z : S) : ∀ n, n ≤ m + 1 →
    evH E z (cl m f n) = sumK E (fun b => E.mul (f b) (pw E z b.val)) (CMInert.allZ m n)
  | 0, _ => rfl
  | n + 1, h => by
    have ih := ev_cl f z n (Nat.le_trans (Nat.le_succ n) h)
    have hv : (mk m n).val = n := md_small m n h
    show E.add (E.mul (f (mk m n)) (pw E z (cl m f n).length)) (evH E z (cl m f n))
      = sumK E (fun b => E.mul (f b) (pw E z b.val)) (CMInert.allZ m n ++ [mk m n])
    rw [CMInert.sumK_app', ih, len_cl]
    show E.add (E.mul (f (mk m n)) (pw E z n)) (sumK E (fun b => E.mul (f b) (pw E z b.val)) (CMInert.allZ m n))
      = E.add (sumK E (fun b => E.mul (f b) (pw E z b.val)) (CMInert.allZ m n))
          (E.add (E.mul (f (mk m n)) (pw E z (mk m n).val)) E.zero)
    rw [hv]
    generalize sumK E (fun b => E.mul (f b) (pw E z b.val)) (CMInert.allZ m n) = X
    lib_ring

end CoefList

#print axioms CoefList.len_cl
#print axioms CoefList.ev_cl

lang/RootsConst.lean

/-
  lang/RootsConst — T49 (O3') (i') (b): **x ∈ S[C_ℓ] vanishing at ℓ − 1 points with unit differences has constant
  coefficients**. ℓ = m + 1, f : 𝔽_ℓ → S the coefficients, rs = the points (e.g. z^c, c = 1..ℓ − 1, in ℤ/p^k):
  each r ∈ rs has r^ℓ = 1 and r − 1 a unit, Σ_b f(b) r^b = 0, and rs has pairwise unit differences. Then
    g := f − f(ℓ − 1):  g(ℓ − 1) = 0 and Σ_b g(b) r^b = 0 (GsUnit: Σ_b r^b = 0)
    zeros_cl            Zeros (cl g k) ⇒ g(j) = 0 for j < k
    roots_const         f(j) = f(ℓ − 1) for every j < ℓ − 1        (UnitRoots on cl g (ℓ − 1), CoefList.ev_cl)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CoefList
import lang.GsUnit

namespace RootsConst

open FTAChain ZMod YMChain FiniteRecChain Frobenius RootBound UnitRoots CoefList

variable {S : Type} (E : DetChain.CRing S) (m : Nat)

/-- the hypotheses on each point r. -/
def PtOK (f : Zp m → S) : List S → Prop
  | [] => True
  | r :: rs => (pw E r (m + 1) = E.one ∧ (∃ w, E.mul (E.add r (E.neg E.one)) w = E.one) ∧
      sumK E (fun b => E.mul (f b) (pw E r b.val)) (CMInert.allZ m (m + 1)) = E.zero) ∧ PtOK f rs

theorem zeros_cl (g : Zp m → S) : ∀ k, Zeros E (cl m g k) → ∀ j, j < k → g (mk m j) = E.zero
  | 0, _, j, hj => absurd hj (Nat.not_lt_zero j)
  | k + 1, ⟨h0, h1⟩, j, hj => by
    cases Nat.lt_or_ge j k with
    | inl hlt => exact zeros_cl g k h1 j hlt
    | inr hge =>
      have hjk : j = k := Nat.le_antisymm (Nat.le_of_lt_succ hj) hge
      rw [hjk]; exact h0

theorem sum_sub (f : Zp m → S) (c : S) (r : S) : ∀ l : List (Zp m),
    sumK E (fun b => E.mul (E.add (f b) (E.neg c)) (pw E r b.val)) l
      = E.add (sumK E (fun b => E.mul (f b) (pw E r b.val)) l) (E.neg (E.mul c (sumK E (fun b => pw E r b.val) l)))
  | [] => by show E.zero = E.add E.zero (E.neg (E.mul c E.zero)); lib_ring
  | b :: l => by
    show E.add (E.mul (E.add (f b) (E.neg c)) (pw E r b.val)) (sumK E (fun b => E.mul (E.add (f b) (E.neg c)) (pw E r b.val)) l)
      = E.add (E.add (E.mul (f b) (pw E r b.val)) (sumK E (fun b => E.mul (f b) (pw E r b.val)) l))
          (E.neg (E.mul c (E.add (pw E r b.val) (sumK E (fun b => pw E r b.val) l))))
    rw [sum_sub f c r l]
    generalize sumK E (fun b => E.mul (f b) (pw E r b.val)) l = X
    generalize sumK E (fun b => pw E r b.val) l = Y
    lib_ring

/-- Σ_(b ∈ allZ n) r^b = Σ_(i<n) r^i. -/
theorem sum_pows (r : S) : ∀ n, n ≤ m + 1 → sumK E (fun b => pw E r b.val) (CMInert.allZ m n) = CycChar.gs E r n
  | 0, _ => rfl
  | n + 1, h => by
    show sumK E (fun b => pw E r b.val) (CMInert.allZ m n ++ [mk m n]) = E.add (pw E r n) (CycChar.gs E r n)
    rw [CMInert.sumK_app', sum_pows r n (Nat.le_trans (Nat.le_succ n) h)]
    show E.add (CycChar.gs E r n) (E.add (pw E r (mk m n).val) E.zero) = _
    have hv : (mk m n).val = n := md_small m n h
    rw [hv]
    lib_ring

theorem roots_g (f : Zp m → S) : ∀ rs : List S, PtOK E m f rs →
    AllRoot E (cl m (fun b => E.add (f b) (E.neg (f (mk m m)))) (m + 1)) rs
  | [], _ => trivial
  | r :: rs, ⟨⟨hr, ⟨w, hw⟩, hs⟩, hrs⟩ => by
    refine ⟨?_, roots_g f rs hrs⟩
    rw [ev_cl E m _ r (m + 1) (Nat.le_refl _)]
    show sumK E (fun b => E.mul (E.add (f b) (E.neg (f (mk m m)))) (pw E r b.val)) (CMInert.allZ m (m + 1)) = E.zero
    rw [sum_sub, hs, sum_pows E m r (m + 1) (Nat.le_refl _),
      GsUnit.gs_unit E r w hw (m + 1) hr]
    lib_ring

/-- **constant coefficients**: f(j) = f(ℓ − 1) for j < ℓ − 1. -/
theorem roots_const (f : Zp m → S) (m' : Nat) (hm : m = m' + 1) (rs : List S) (hl : rs.length = m)
    (hu : UDist E rs) (hp : PtOK E m f rs) : ∀ j, j < m → f (mk m j) = f (mk m m) := by
  intro j hj
  have hg := roots_g E m f rs hp
  -- cl g (m + 1) = g(m) :: cl g m, and g(m) = 0, so evH r (cl g m) = 0 at every r
  have hg0 : E.add (f (mk m m)) (E.neg (f (mk m m))) = E.zero := by lib_ring
  have hroot : AllRoot E (cl m (fun b => E.add (f b) (E.neg (f (mk m m)))) m) rs := by
    have key : ∀ rs' : List S, AllRoot E (cl m (fun b => E.add (f b) (E.neg (f (mk m m)))) (m + 1)) rs' →
        AllRoot E (cl m (fun b => E.add (f b) (E.neg (f (mk m m)))) m) rs' := by
      intro rs'
      induction rs' with
      | nil => intro _; trivial
      | cons r rs' ih =>
        intro ⟨h1, h2⟩
        refine ⟨?_, ih h2⟩
        have e : evH E r (cl m (fun b => E.add (f b) (E.neg (f (mk m m)))) (m + 1))
            = E.add (E.mul (E.add (f (mk m m)) (E.neg (f (mk m m))))
                (pw E r (cl m (fun b => E.add (f b) (E.neg (f (mk m m)))) m).length))
              (evH E r (cl m (fun b => E.add (f b) (E.neg (f (mk m m)))) m)) := rfl
        rw [e, hg0] at h1
        exact (show evH E r (cl m (fun b => E.add (f b) (E.neg (f (mk m m)))) m)
            = E.add (E.mul E.zero (pw E r (cl m (fun b => E.add (f b) (E.neg (f (mk m m)))) m).length))
              (evH E r (cl m (fun b => E.add (f b) (E.neg (f (mk m m)))) m)) by lib_ring).trans h1
    exact key rs hg
  subst hm
  have hz := unit_roots E m' (E.add (f (mk (m' + 1) m')) (E.neg (f (mk (m' + 1) (m' + 1)))))
    (cl (m' + 1) (fun b => E.add (f b) (E.neg (f (mk (m' + 1) (m' + 1))))) m') (len_cl (m' + 1) _ m') rs hl hu hroot
  have hzero := zeros_cl E (m' + 1) (fun b => E.add (f b) (E.neg (f (mk (m' + 1) (m' + 1))))) (m' + 1) hz j hj
  calc f (mk (m' + 1) j)
      = E.add (E.add (f (mk (m' + 1) j)) (E.neg (f (mk (m' + 1) (m' + 1))))) (f (mk (m' + 1) (m' + 1))) := by lib_ring
    _ = E.add E.zero (f (mk (m' + 1) (m' + 1))) := by rw [hzero]
    _ = f (mk (m' + 1) (m' + 1)) := by lib_ring

end RootsConst

#print axioms RootsConst.zeros_cl
#print axioms RootsConst.sum_sub
#print axioms RootsConst.sum_pows
#print axioms RootsConst.roots_g
#print axioms RootsConst.roots_const

lang/UnitPk.lean

/-
  lang/UnitPk — T49 (O3') (i') (c): **units of ℤ/p^k**: red(x) ≠ 0 in 𝔽_p ⇒ x is a unit in ℤ/M whenever
  M + 1 = p·k' and (p·1)^n = 0 in ℤ/M (e.g. M + 1 = p^n).
    one_add_unit   P^n = 0 ⇒ (1 + P t)·Σ_(i<n) (−P t)^i = 1     (CycEis.gs_mul with w = −Pt)
    unit_pk        red(x) ≠ 0 ⇒ x·w = 1 for some w            (lift of red(x)^(−1), then one_add_unit)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ExactP2
import lang.GsUnit

namespace UnitPk

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

theorem one_add_unit {S : Type} (E : CRing S) (P t : S) (n : Nat) (hP : pw E P n = E.zero) :
    E.mul (E.add E.one (E.mul P t)) (CycChar.gs E (E.neg (E.mul P t)) n) = E.one := by
  have h := CycEis.gs_mul E (E.neg (E.mul P t)) n
  have hn : pw E (E.neg (E.mul P t)) n = E.zero := by
    rw [show E.neg (E.mul P t) = E.mul P (E.neg t) by lib_ring, pw_mul, hP]
    lib_ring
  rw [hn] at h
  calc E.mul (E.add E.one (E.mul P t)) (CycChar.gs E (E.neg (E.mul P t)) n)
      = E.neg (E.mul (E.add (E.neg (E.mul P t)) (E.neg E.one)) (CycChar.gs E (E.neg (E.mul P t)) n)) := by lib_ring
    _ = E.neg (E.add E.zero (E.neg E.one)) := by rw [h]
    _ = E.one := by lib_ring

/-- **red(x) ≠ 0 ⇒ x is a unit in ℤ/M** (M + 1 = p·k', (p·1)^n = 0). -/
theorem unit_pk (J M k' n : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
    (hk : M + 1 = (J + 1 + 1) * k') (hPn : pw (ring M) (ExactP2.P J M) n = (ring M).zero)
    (x : Zp M) (hx : RedP2.red J M x ≠ (ring (J + 1)).zero) : ∃ w, (ring M).mul x w = (ring M).one := by
  have hinv := inv_exists J hpr (RedP2.red J M x) hx
  let y : Zp M := mk M (pw (ring (J + 1)) (RedP2.red J M x) J).val
  have hy : RedP2.red J M y = pw (ring (J + 1)) (RedP2.red J M x) J := Subtype.ext (by
    show md (J + 1) (md M (pw (ring (J + 1)) (RedP2.red J M x) J).val) = (pw (ring (J + 1)) (RedP2.red J M x) J).val
    rw [RedP2.mdmd J M k' hk]
    exact md_small (J + 1) _ (pw (ring (J + 1)) (RedP2.red J M x) J).property)
  have hneg : ∀ v : Zp M, RedP2.red J M ((ring M).neg v) = (ring (J + 1)).neg (RedP2.red J M v) := fun v => by
    have h0 := RedP2.red_add J M k' hk v ((ring M).neg v)
    rw [show (ring M).add v ((ring M).neg v) = (ring M).zero by lib_ring, RedP2.red_zero J M k' hk] at h0
    calc RedP2.red J M ((ring M).neg v)
        = (ring (J + 1)).add (ring (J + 1)).zero ((ring (J + 1)).neg (RedP2.red J M v)) := by rw [h0]; lib_ring
      _ = _ := by lib_ring
  have h0 : RedP2.red J M ((ring M).add ((ring M).mul x y) ((ring M).neg (ring M).one)) = (ring (J + 1)).zero := by
    rw [RedP2.red_add J M k' hk, RedP2.red_mul J M k' hk, hneg, RedP2.red_one J M k' hk, hy]
    calc (ring (J + 1)).add ((ring (J + 1)).mul (RedP2.red J M x) (pw (ring (J + 1)) (RedP2.red J M x) J))
          ((ring (J + 1)).neg (ring (J + 1)).one)
        = (ring (J + 1)).add ((ring (J + 1)).mul (pw (ring (J + 1)) (RedP2.red J M x) J) (RedP2.red J M x))
          ((ring (J + 1)).neg (ring (J + 1)).one) := by lib_ring
      _ = (ring (J + 1)).zero := by rw [hinv]; lib_ring
  obtain ⟨t, ht⟩ := RedP2.red_ker J M _ h0
  have hu := one_add_unit (ring M) (ExactP2.P J M) t n hPn
  refine ⟨(ring M).mul y (CycChar.gs (ring M) ((ring M).neg ((ring M).mul (ExactP2.P J M) t)) n), ?_⟩
  have hxy : (ring M).mul x y = (ring M).add (ring M).one ((ring M).mul (ExactP2.P J M) t) := by
    calc (ring M).mul x y = (ring M).add ((ring M).add ((ring M).mul x y) ((ring M).neg (ring M).one)) (ring M).one := by
          lib_ring
      _ = (ring M).add ((ring M).mul (ExactP2.P J M) t) (ring M).one :=
          congrArg (fun z => (ring M).add z (ring M).one) ht
      _ = _ := by lib_ring
  calc (ring M).mul x ((ring M).mul y (CycChar.gs (ring M) ((ring M).neg ((ring M).mul (ExactP2.P J M) t)) n))
      = (ring M).mul ((ring M).mul x y) (CycChar.gs (ring M) ((ring M).neg ((ring M).mul (ExactP2.P J M) t)) n) := by
        lib_ring
    _ = (ring M).one := by rw [hxy]; exact hu

end UnitPk

#print axioms UnitPk.one_add_unit
#print axioms UnitPk.unit_pk

lang/TeichUnits.lean

/-
  lang/TeichUnits — T49 (O3') (i'): **differences of distinct powers of a Teichmüller root are units in ℤ/p^k**.
  M + 1 = p·k', (p·1)^n = 0 in ℤ/M, z ∈ ℤ/M with red(z) of exact order ℓ = m + 3 in 𝔽_p. For 0 ≤ a < b < ℓ:
    red_ne     red(z^a) ≠ red(z^b)                  (SymProdZ.small_eq)
    diff_unit  z^a − z^b is a unit                   (UnitPk.unit_pk)
  This gives UnitRoots.UDist for the points z^c (PtsDist) and the unit z^c − 1 (a = 0) for GsUnit.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.UnitPk
import lang.SymProdZ
import lang.RedPN

namespace TeichUnits

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius

variable (J M k' n m : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
  (hk : M + 1 = (J + 1 + 1) * k') (hPn : pw (ring M) (ExactP2.P J M) n = (ring M).zero)
  (z : Zp M) (hz : OrdMul.Ord (ring (J + 1)) (RedP2.red J M z) (m + 1 + 1 + 1))

include hk hz in
theorem red_ne (a b : Nat) (hab : a < b) (hb : b < m + 1 + 1 + 1) :
    RedP2.red J M ((ring M).add (pw (ring M) z a) ((ring M).neg (pw (ring M) z b))) ≠ (ring (J + 1)).zero := by
  intro h
  have hneg : ∀ v : Zp M, RedP2.red J M ((ring M).neg v) = (ring (J + 1)).neg (RedP2.red J M v) := fun v => by
    have h0 := RedP2.red_add J M k' hk v ((ring M).neg v)
    rw [show (ring M).add v ((ring M).neg v) = (ring M).zero by lib_ring, RedP2.red_zero J M k' hk] at h0
    calc RedP2.red J M ((ring M).neg v)
        = (ring (J + 1)).add (ring (J + 1)).zero ((ring (J + 1)).neg (RedP2.red J M v)) := by rw [h0]; lib_ring
      _ = _ := by lib_ring
  rw [RedP2.red_add J M k' hk, hneg, RedPN.r_pw J M k' hk, RedPN.r_pw J M k' hk] at h
  have he : pw (ring (J + 1)) (RedP2.red J M z) a = pw (ring (J + 1)) (RedP2.red J M z) b := by
    calc pw (ring (J + 1)) (RedP2.red J M z) a
        = (ring (J + 1)).add ((ring (J + 1)).add (pw (ring (J + 1)) (RedP2.red J M z) a)
            ((ring (J + 1)).neg (pw (ring (J + 1)) (RedP2.red J M z) b))) (pw (ring (J + 1)) (RedP2.red J M z) b) := by
          lib_ring
      _ = (ring (J + 1)).add (ring (J + 1)).zero (pw (ring (J + 1)) (RedP2.red J M z) b) := by rw [h]
      _ = _ := by lib_ring
  have := SymProdZ.small_eq m (ring (J + 1)) _ hz a b hb (Nat.le_of_lt hab) he
  rw [this] at hab
  exact Nat.lt_irrefl b hab

include hpr hk hPn hz in
/-- **z^a − z^b is a unit in ℤ/p^k** (0 ≤ a < b < ℓ). -/
theorem diff_unit (a b : Nat) (hab : a < b) (hb : b < m + 1 + 1 + 1) :
    ∃ w, (ring M).mul ((ring M).add (pw (ring M) z a) ((ring M).neg (pw (ring M) z b))) w = (ring M).one :=
  UnitPk.unit_pk J M k' n hpr hk hPn _ (red_ne J M k' m hk z hz a b hab hb)

end TeichUnits

#print axioms TeichUnits.red_ne
#print axioms TeichUnits.diff_unit

lang/PtsDist.lean

/-
  lang/PtsDist — T49 (O3') (i'): **the points z^n, …, z^1 have pairwise unit differences** once every difference
  z^b − z^a (a < b ≤ n) is a unit (in ℤ/p^k: red(z) of order ℓ and UnitPk).
    pts        [z^n, …, z^1]
    mem_pts    s ∈ pts n ⇒ s = z^a with 1 ≤ a ≤ n
    udist_pts  UDist (pts n)       (UnitRoots.UDist: later minus earlier is a unit)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.UnitRoots

namespace PtsDist

open Frobenius YMChain UnitRoots

variable {S : Type} (E : DetChain.CRing S) (z : S)

def pts : Nat → List S
  | 0 => []
  | n + 1 => pw E z (n + 1) :: pts n

theorem len_pts : ∀ n, (pts E z n).length = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (len_pts n)

theorem mem_pts : ∀ n s, memL s (pts E z n) → ∃ a, 1 ≤ a ∧ a ≤ n ∧ s = pw E z a
  | 0, _, h => h.elim
  | n + 1, s, h => by
    cases h with
    | inl h => exact ⟨n + 1, Nat.succ_le_succ (Nat.zero_le n), Nat.le_refl _, h⟩
    | inr h =>
      obtain ⟨a, h1, h2, h3⟩ := mem_pts n s h
      exact ⟨a, h1, Nat.le_trans h2 (Nat.le_succ n), h3⟩

theorem udist_pts (hU : ∀ a b, 1 ≤ a → a < b → ∃ w, E.mul (E.add (pw E z a) (E.neg (pw E z b))) w = E.one) :
    ∀ n, UDist E (pts E z n)
  | 0 => trivial
  | n + 1 => by
    refine ⟨fun s hs => ?_, udist_pts hU n⟩
    obtain ⟨a, h1, h2, h3⟩ := mem_pts E z n s hs
    rw [h3]
    exact hU a (n + 1) h1 (Nat.lt_succ_of_le h2)

end PtsDist

#print axioms PtsDist.len_pts
#print axioms PtsDist.mem_pts
#print axioms PtsDist.udist_pts

lang/AbsPair.lean

/-
  lang/AbsPair — T49 (O3') (ii'), abstract step: **B·J(B) = Π (x_i y_i)^k for paired exponents**.
  In a commutative ring with a multiplicative J (J 1 = 1), entries (x_i, y_i, a_i, b_i) with J x_i = y_i, J y_i = x_i and
  a_i + b_i = k: for B = Π x_i^(a_i) y_i^(b_i),
    J_pw       J(x^n) = J(x)^n
    pair_one   x^a y^b · y^a x^b = (x y)^(a+b)
    pair_prod  B·J(B) = Π (x_i y_i)^k
  Use: x = σ_d(α), y = σ_(−d)(α) over d = 1, …, (ℓ−1)/2, J = σ_(−1), a = v_d, b = v_(ℓ−d), a + b = ℓ − 2:
  B·B̄ = N(α)^(ℓ−2) with no reindexing of products.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.SymProdW

namespace AbsPair

open Frobenius

variable {R : Type} (K : DetChain.CRing R) (J : R → R) (hJm : ∀ a b, J (K.mul a b) = K.mul (J a) (J b))
  (hJ1 : J K.one = K.one)

include hJm hJ1 in
theorem J_pw (x : R) : ∀ n, J (pw K x n) = pw K (J x) n
  | 0 => hJ1
  | n + 1 => by
    show J (K.mul (pw K x n) x) = K.mul (pw K (J x) n) (J x)
    rw [hJm, J_pw x n]

theorem pair_one (x y : R) (a b : Nat) :
    K.mul (K.mul (pw K x a) (pw K y b)) (K.mul (pw K y a) (pw K x b)) = pw K (K.mul x y) (a + b) := by
  rw [pw_mul, SymProdW.pw_add, SymProdW.pw_add]
  generalize pw K x a = xa
  generalize pw K x b = xb
  generalize pw K y a = ya
  generalize pw K y b = yb
  lib_ring

def Bp : List (R × R × Nat × Nat) → R
  | [] => K.one
  | e :: es => K.mul (K.mul (pw K e.1 e.2.2.1) (pw K e.2.1 e.2.2.2)) (Bp es)

def Np (k : Nat) : List (R × R × Nat × Nat) → R
  | [] => K.one
  | e :: es => K.mul (pw K (K.mul e.1 e.2.1) k) (Np k es)

def Good (k : Nat) : List (R × R × Nat × Nat) → Prop
  | [] => True
  | e :: es => (J e.1 = e.2.1 ∧ J e.2.1 = e.1 ∧ e.2.2.1 + e.2.2.2 = k) ∧ Good k es

include hJm hJ1 in
/-- **B·J(B) = Π (x_i y_i)^k**. -/
theorem pair_prod (k : Nat) : ∀ l, Good J k l → K.mul (Bp K l) (J (Bp K l)) = Np K k l
  | [], _ => by
    show K.mul K.one (J K.one) = K.one
    rw [hJ1]; lib_ring
  | e :: es, ⟨⟨h1, h2, h3⟩, hg⟩ => by
    show K.mul (K.mul (K.mul (pw K e.1 e.2.2.1) (pw K e.2.1 e.2.2.2)) (Bp K es))
        (J (K.mul (K.mul (pw K e.1 e.2.2.1) (pw K e.2.1 e.2.2.2)) (Bp K es)))
      = K.mul (pw K (K.mul e.1 e.2.1) k) (Np K k es)
    rw [hJm, hJm, J_pw K J hJm hJ1, J_pw K J hJm hJ1, h1, h2, ← pair_prod k es hg, ← h3, ← pair_one]
    generalize pw K e.1 e.2.2.1 = A
    generalize pw K e.2.1 e.2.2.2 = Bb
    generalize pw K e.2.1 e.2.2.1 = C
    generalize pw K e.1 e.2.2.2 = D
    generalize Bp K es = E
    generalize J E = F
    lib_ring

end AbsPair

#print axioms AbsPair.J_pw
#print axioms AbsPair.pair_one
#print axioms AbsPair.pair_prod

lang/StickBB.lean

/-
  lang/StickBB — T49 (O3') (ii'): **B·B̄ = Π_d (σ_d α · σ_(−d) α)^k** in ℤ[C_ℓ] for paired Stickelberger exponents.
  J = σ_(−1) (CycGal.gsig with −1) is a ring map; σ_(−1)σ_d = σ_(−d), σ_(−1)σ_(−d) = σ_d. Entries (d, a_d, b_d) with
  a_d + b_d = k give B = Π_d (σ_d α)^(a_d) (σ_(−d) α)^(b_d), and AbsPair.pair_prod yields
    gsig_comp   σ_e(σ_d u) = σ_(e d)(u)
    bb          B·σ_(−1)(B) = Π_d (σ_d α · σ_(−d) α)^k
  With a_d = v_d, b_d = v_(ℓ−d), k = ℓ − 2 and d over 1..(ℓ−1)/2 the right side is N(α)^(ℓ−2).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.AbsPair
import lang.CycGal

namespace StickBB

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing

variable (m : Nat)

def e : Zp m := (ring m).neg (ring m).one

theorem e_sq : (ring m).mul (e m) (e m) = (ring m).one := by
  show (ring m).mul ((ring m).neg (ring m).one) ((ring m).neg (ring m).one) = (ring m).one
  lib_ring

theorem gsig_comp (d f : Zp m) (u : V Int (m + 1)) :
    CycGal.gsig LibInt.intR m f (CycGal.gsig LibInt.intR m d u) = CycGal.gsig LibInt.intR m ((ring m).mul f d) u :=
  ext LibInt.intR m _ _ (fun b => by
    rw [CycGal.gt_gsig, CycGal.gt_gsig, CycGal.gt_gsig]
    rw [show (ring m).mul ((ring m).mul b f) d = (ring m).mul b ((ring m).mul f d) by lib_ring])

def J (u : V Int (m + 1)) : V Int (m + 1) := CycGal.gsig LibInt.intR m (e m) u

def ents (α : V Int (m + 1)) : List (Zp m × Nat × Nat) → List (V Int (m + 1) × V Int (m + 1) × Nat × Nat)
  | [] => []
  | q :: qs => (CycGal.gsig LibInt.intR m q.1 α, CycGal.gsig LibInt.intR m ((ring m).mul (e m) q.1) α, q.2.1, q.2.2)
      :: ents α qs

def Sums (k : Nat) : List (Zp m × Nat × Nat) → Prop
  | [] => True
  | q :: qs => q.2.1 + q.2.2 = k ∧ Sums k qs

theorem good (α : V Int (m + 1)) (k : Nat) : ∀ qs, Sums m k qs → AbsPair.Good (J m) k (ents m α qs)
  | [], _ => trivial
  | q :: qs, ⟨hk, hs⟩ => by
    refine ⟨⟨?_, ?_, hk⟩, good α k qs hs⟩
    · exact gsig_comp m q.1 (e m) α
    · show CycGal.gsig LibInt.intR m (e m) (CycGal.gsig LibInt.intR m ((ring m).mul (e m) q.1) α)
        = CycGal.gsig LibInt.intR m q.1 α
      rw [gsig_comp, show (ring m).mul (e m) ((ring m).mul (e m) q.1) = q.1 by
        show (ring m).mul ((ring m).neg (ring m).one) ((ring m).mul ((ring m).neg (ring m).one) q.1) = q.1
        lib_ring]

/-- **B·σ_(−1)(B) = Π_d (σ_d α · σ_(−d) α)^k**. -/
theorem bb (α : V Int (m + 1)) (k : Nat) (qs : List (Zp m × Nat × Nat)) (hs : Sums m k qs) :
    (gring LibInt.intR m).mul (AbsPair.Bp (gring LibInt.intR m) (ents m α qs))
        (J m (AbsPair.Bp (gring LibInt.intR m) (ents m α qs)))
      = AbsPair.Np (gring LibInt.intR m) k (ents m α qs) :=
  AbsPair.pair_prod (gring LibInt.intR m) (J m)
    (fun a b => CycGal.gsig_mul LibInt.intR m (e m) (e m) (e_sq m) a b)
    (CycGal.gsig_one LibInt.intR m (e m) (e m) (e_sq m)) k (ents m α qs) (good m α k qs hs)

end StickBB

#print axioms StickBB.e_sq
#print axioms StickBB.gsig_comp
#print axioms StickBB.good
#print axioms StickBB.bb

lang/NpPow.lean

/-
  lang/NpPow — T49 (O3') (ii'): **Π_i (x_i y_i)^k = (Π_i x_i y_i)^k**, so B·σ_(−1)(B) (lang/StickBB) is the k-th power
  of the product of the pairs σ_d α · σ_(−d) α, i.e. N(α)^k when the pairs run over all of 𝔽_ℓ^× / ±1.
    Pp       Π_i x_i y_i
    np_pow   AbsPair.Np k l = (Pp l)^k
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.AbsPair

namespace NpPow

open Frobenius

variable {R : Type} (K : DetChain.CRing R)

def Pp : List (R × R × Nat × Nat) → R
  | [] => K.one
  | e :: es => K.mul (K.mul e.1 e.2.1) (Pp es)

theorem np_pow (k : Nat) : ∀ l, AbsPair.Np K k l = pw K (Pp K l) k
  | [] => by
    show K.one = pw K K.one k
    induction k with
    | zero => rfl
    | succ k ih =>
      show K.one = K.mul (pw K K.one k) K.one
      rw [← ih]; lib_ring
  | e :: es => by
    show K.mul (pw K (K.mul e.1 e.2.1) k) (AbsPair.Np K k es) = pw K (K.mul (K.mul e.1 e.2.1) (Pp K es)) k
    rw [np_pow k es, pw_mul K (K.mul e.1 e.2.1) (Pp K es) k, pw_mul K e.1 e.2.1 k]

end NpPow

#print axioms NpPow.np_pow

lang/PairNorm.lean

/-
  lang/PairNorm — T49 (O3') (ii'): **the product of the pairs σ_a α · σ_b α is the norm product** of NormEval.
  For a list of entries (σ_(a_i) α, σ_(b_i) α, ·, ·) and inverse data (c_i, a_i), (c'_i, b_i):
    flat     [(c_1, a_1), (c'_1, b_1), (c_2, a_2), …]
    pp_nrm   NpPow.Pp (entries) = NormEval.nrm α (flat)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.NpPow
import lang.NormEval

namespace PairNorm

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing

variable (m : Nat) (α : V Int (m + 1))

/-- entries from (a, b, c, c', i, j): (σ_a α, σ_b α, i, j). -/
def ents : List ((Zp m × Zp m) × (Zp m × Zp m) × Nat × Nat) → List (V Int (m + 1) × V Int (m + 1) × Nat × Nat)
  | [] => []
  | q :: qs => (CycGal.gsig LibInt.intR m q.1.2 α, CycGal.gsig LibInt.intR m q.2.1.2 α, q.2.2.1, q.2.2.2) :: ents qs

def flat : List ((Zp m × Zp m) × (Zp m × Zp m) × Nat × Nat) → List (Zp m × Zp m)
  | [] => []
  | q :: qs => q.1 :: q.2.1 :: flat qs

theorem pp_nrm : ∀ qs, NpPow.Pp (gring LibInt.intR m) (ents m α qs) = NormEval.nrm m α (flat m qs)
  | [] => rfl
  | q :: qs => by
    show (gring LibInt.intR m).mul
        ((gring LibInt.intR m).mul (CycGal.gsig LibInt.intR m q.1.2 α) (CycGal.gsig LibInt.intR m q.2.1.2 α))
        (NpPow.Pp (gring LibInt.intR m) (ents m α qs))
      = (gring LibInt.intR m).mul (CycGal.gsig LibInt.intR m q.1.2 α)
        ((gring LibInt.intR m).mul (CycGal.gsig LibInt.intR m q.2.1.2 α) (NormEval.nrm m α (flat m qs)))
    rw [pp_nrm qs]
    generalize CycGal.gsig LibInt.intR m q.1.2 α = X
    generalize CycGal.gsig LibInt.intR m q.2.1.2 α = Y
    generalize NormEval.nrm m α (flat m qs) = Z
    lib_ring

end PairNorm

#print axioms PairNorm.pp_nrm

lang/BBNorm.lean

/-
  lang/BBNorm — T49 (O3') (ii'): **B·σ_(−1)(B) = N^k** with N = NormEval.nrm α over the pairs (d, −d).
    conv      (d, a, b) ↦ ((iv d, d), (iv(−d), −d), a, b)   (iv: any choice of first components, e.g. inverses)
    ents_eq   StickBB.ents α qs = PairNorm.ents α (conv qs)
    bb_norm   B·σ_(−1)(B) = (NormEval.nrm α (flat (conv qs)))^k
  With k = ℓ − 2, the pairs over d = 1..(ℓ−1)/2 and N(α) = p: B B̄ = p^(ℓ−2).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.StickBB
import lang.PairNorm

namespace BBNorm

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing

variable (m : Nat) (α : V Int (m + 1)) (iv : Zp m → Zp m)

def conv : List (Zp m × Nat × Nat) → List ((Zp m × Zp m) × (Zp m × Zp m) × Nat × Nat)
  | [] => []
  | q :: qs => ((iv q.1, q.1), (iv ((ring m).mul (StickBB.e m) q.1), (ring m).mul (StickBB.e m) q.1), q.2.1, q.2.2)
      :: conv qs

theorem ents_eq : ∀ qs, StickBB.ents m α qs = PairNorm.ents m α (conv m iv qs)
  | [] => rfl
  | q :: qs => by
    exact congrArg (List.cons _) (ents_eq qs)

/-- **B·σ_(−1)(B) = N^k**. -/
theorem bb_norm (k : Nat) (qs : List (Zp m × Nat × Nat)) (hs : StickBB.Sums m k qs) :
    (gring LibInt.intR m).mul (AbsPair.Bp (gring LibInt.intR m) (StickBB.ents m α qs))
        (StickBB.J m (AbsPair.Bp (gring LibInt.intR m) (StickBB.ents m α qs)))
      = Frobenius.pw (gring LibInt.intR m) (NormEval.nrm m α (PairNorm.flat m (conv m iv qs))) k := by
  rw [StickBB.bb m α k qs hs, NpPow.np_pow, ents_eq m α iv qs, PairNorm.pp_nrm]

end BBNorm

#print axioms BBNorm.ents_eq
#print axioms BBNorm.bb_norm

lang/UnitCirc4.lean

/-
  lang/UnitCirc4 — T49 (O3') (iii), step 4: **u·ū = 1 + t·N in ℤ[C_ℓ] ⇒ u = c·N ± σ^k** (ℓ = p + 2 prime).
  ū = σ_(−1)(u) (CycGal.gsig with d = −1). Reading two coefficients of u·ū:
    coef0      (u ū)(0) = Σ_a u(a)²
    aug        Σ_b (u ū)(b) = (Σ_a u(a))·(Σ_b ū(b)) = (Σ u)²
    list forms sq, sm, ln of the coefficient list (UnitCirc1)
    unit_circ  (u ū)(b) = [b = 0] + t for all b ⇒ ∃ c ε k-form: the list (u(a) − c)_a has sum ε = ±1 and is a signed
               unit vector (UnitCirc2.OneHot) — i.e. u = c·N + ε σ^k: every u with u ū ≡ 1 (mod N) is ±ζ^k in ℤ[ζ_ℓ].
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.UnitCirc3
import lang.CycGal

namespace UnitCirc4

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing UnitCirc1 UnitCirc2
open FiniteRecChain (ite_pos')

variable (m : Nat)

/-- −1 in 𝔽_ℓ. -/
def e : Zp m := (ring m).neg (ring m).one

theorem e_sq : (ring m).mul (e m) (e m) = (ring m).one := by
  show (ring m).mul ((ring m).neg (ring m).one) ((ring m).neg (ring m).one) = (ring m).one
  lib_ring

def ubar (u : V Int (m + 1)) : V Int (m + 1) := CycGal.gsig LibInt.intR m (e m) u

theorem sq_map (f : Zp m → Int) : ∀ l : List (Zp m), sumK LibInt.intR (fun a => f a * f a) l = sq (l.map f)
  | [] => rfl
  | a :: l => by
    show f a * f a + sumK LibInt.intR (fun a => f a * f a) l = f a * f a + sq (l.map f)
    rw [sq_map f l]

theorem sm_map (f : Zp m → Int) : ∀ l : List (Zp m), sumK LibInt.intR f l = sm (l.map f)
  | [] => rfl
  | a :: l => by
    show f a + sumK LibInt.intR f l = f a + sm (l.map f)
    rw [sm_map f l]

theorem ln_app (f : Zp m → Int) (x : Zp m) : ∀ l : List (Zp m), ln ((l ++ [x]).map f) = ln (l.map f) + 1
  | [] => by show (1 : Int) + 0 = 0 + 1; lib_int
  | a :: l => by
    show 1 + ln ((l ++ [x]).map f) = 1 + ln (l.map f) + 1
    rw [ln_app f x l]; lib_int

theorem ln_allZ (f : Zp m → Int) : ∀ n, ln ((CMInert.allZ m n).map f) = Int.ofNat n
  | 0 => rfl
  | n + 1 => by
    show ln ((CMInert.allZ m n ++ [mk m n]).map f) = Int.ofNat (n + 1)
    rw [ln_app, ln_allZ f n] <;> rfl

theorem ln_L (f : Zp m → Int) : ln ((L m).map f) = Int.ofNat (m + 1) := ln_allZ m f (m + 1)

theorem sum_const (t : Int) : ∀ l : List (Zp m), sumK LibInt.intR (fun _ => t) l = ln (l.map (fun _ => t)) * t
  | [] => by show (0 : Int) = 0 * t; lib_int
  | a :: l => by
    show t + sumK LibInt.intR (fun _ => t) l = (1 + ln (l.map (fun _ => t))) * t
    rw [sum_const t l]; lib_int

/-- (u ū)(0) = Σ_a u(a)². -/
theorem coef0 (u : V Int (m + 1)) :
    gt LibInt.intR m (gmul LibInt.intR m u (ubar m u)) (ring m).zero
      = sumK LibInt.intR (fun a => gt LibInt.intR m u a * gt LibInt.intR m u a) (L m) := by
  rw [gt_mul]
  apply sumK_ext
  intro a
  show gt LibInt.intR m u a * gt LibInt.intR m (CycGal.gsig LibInt.intR m (e m) u) ((ring m).add (ring m).zero ((ring m).neg a))
    = gt LibInt.intR m u a * gt LibInt.intR m u a
  rw [CycGal.gt_gsig]
  have h : (ring m).mul ((ring m).add (ring m).zero ((ring m).neg a)) (e m) = a := by
    show (ring m).mul ((ring m).add (ring m).zero ((ring m).neg a)) ((ring m).neg (ring m).one) = a
    lib_ring
  rw [h]

/-- Σ_b (u v)(b) = (Σ u)(Σ v). -/
theorem aug_mul (u v : V Int (m + 1)) :
    sumK LibInt.intR (gt LibInt.intR m (gmul LibInt.intR m u v)) (L m)
      = sumK LibInt.intR (gt LibInt.intR m u) (L m) * sumK LibInt.intR (gt LibInt.intR m v) (L m) := by
  rw [sumK_ext LibInt.intR _ _ (gt_mul LibInt.intR m u v), sumK_swap]
  show _ = LibInt.intR.mul (sumK LibInt.intR (gt LibInt.intR m u) (L m)) (sumK LibInt.intR (gt LibInt.intR m v) (L m))
  rw [sumK_mul]
  apply sumK_ext
  intro a
  exact (shift LibInt.intR m (fun b => LibInt.intR.mul (gt LibInt.intR m u a) (gt LibInt.intR m v b)) ((ring m).neg a)).trans
    (mul_sumK LibInt.intR _ _ _).symm

/-- Σ ū = Σ u. -/
theorem aug_bar (u : V Int (m + 1)) :
    sumK LibInt.intR (gt LibInt.intR m (ubar m u)) (L m) = sumK LibInt.intR (gt LibInt.intR m u) (L m) := by
  show sumK LibInt.intR (gt LibInt.intR m (CycGal.gsig LibInt.intR m (e m) u)) (L m) = _
  rw [sumK_ext LibInt.intR _ _ (CycGal.gt_gsig LibInt.intR m (e m) u)]
  exact CycGal.scale_sum LibInt.intR m (e m) (e m) (e_sq m) (gt LibInt.intR m u)

theorem sum_delta : sumK LibInt.intR (fun b => delta LibInt.intR b (ring m).zero) (L m) = 1 := by
  show sumK LibInt.intR (fun b => delta LibInt.intR b (ring m).zero) (CMInert.allZ m (m + 1)) = 1
  rw [CMInert.count_allZ m LibInt.intR (ring m).zero (m + 1) (Nat.le_refl _)]
  exact ite_pos' _ _ (md_lt m 0)

/-- **u ū = 1 + tN ⇒ u = c·N ± σ^k** (ℓ = p + 2 prime). -/
theorem unit_circ (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (u : V Int (p + 1 + 1)) (t : Int)
    (hu : ∀ b, gt LibInt.intR (p + 1) (gmul LibInt.intR (p + 1) u (ubar (p + 1) u)) b
      = LibInt.intR.add (delta LibInt.intR b (ring (p + 1)).zero) t) :
    ∃ c ε, (ε = 1 ∨ ε = -1) ∧ sm (((L (p + 1)).map (gt LibInt.intR (p + 1) u)).map (fun a => a - c)) = ε ∧
      OneHot (((L (p + 1)).map (gt LibInt.intR (p + 1) u)).map (fun a => a - c)) := by
  have hn : ln ((L (p + 1)).map (gt LibInt.intR (p + 1) u)) = Int.ofNat (p + 1 + 1) := ln_L (p + 1) _
  have hq : sq ((L (p + 1)).map (gt LibInt.intR (p + 1) u)) = 1 + t := by
    rw [← sq_map, ← coef0, hu]
    show (if (ring (p + 1)).zero = (ring (p + 1)).zero then (1 : Int) else 0) + t = 1 + t
    rw [ite_pos' _ _ rfl]
  have hs : sm ((L (p + 1)).map (gt LibInt.intR (p + 1) u)) * sm ((L (p + 1)).map (gt LibInt.intR (p + 1) u))
      = 1 + Int.ofNat (p + 1 + 1) * t := by
    rw [← sm_map]
    have h1 := aug_mul (p + 1) u (ubar (p + 1) u)
    rw [aug_bar, sumK_ext LibInt.intR _ _ hu, sumK_add, sum_delta, sum_const, ln_L] at h1
    exact h1.symm
  exact UnitCirc3.unit_vec p hp _ t hn hq hs

end UnitCirc4

#print axioms UnitCirc4.e_sq
#print axioms UnitCirc4.sq_map
#print axioms UnitCirc4.sm_map
#print axioms UnitCirc4.ln_app
#print axioms UnitCirc4.ln_allZ
#print axioms UnitCirc4.ln_L
#print axioms UnitCirc4.sum_const
#print axioms UnitCirc4.coef0
#print axioms UnitCirc4.aug_mul
#print axioms UnitCirc4.aug_bar
#print axioms UnitCirc4.sum_delta
#print axioms UnitCirc4.unit_circ

#print axioms: 41 theorems, no axioms.

78.379 Congruences modulo the norm element and valuations

Coefficient forms of congruences modulo \(N=\sum \sigma ^i\), division by \(p^k\), finite choice without the choice axiom, and valuation arithmetic.

\[ W=c\,u+sN,\ W\bar W\equiv c^2 \pmod N\Rightarrow u\bar u\equiv 1;\qquad \prod x_i^{n_i}=P^{\sum _{P\mid x_i}n_i}\,U. \]

lang/ModN.lean

/-
  lang/ModN — T49 (O3') (iii): **congruence modulo N = Σ σ^i in ℤ[C_ℓ]** in coefficient form:
  u ≡ v (mod N) :⟺ ∃ t, u(b) = v(b) + t for every b.
    modn_refl, modn_symm, modn_trans
    modn_mul    u ≡ u' ⇒ u·v ≡ u'·v                 (the shift Σ_a v(b − a) = Σ v is independent of b)
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.GroupRing
import lib.IntRing
import lib.RingTac

namespace ModN

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing

variable (m : Nat)

def ModN (u v : V Int (m + 1)) : Prop := ∃ t : Int, ∀ b, gt LibInt.intR m u b = gt LibInt.intR m v b + t

theorem modn_refl (u : V Int (m + 1)) : ModN m u u := ⟨0, fun b => by lib_int⟩

theorem modn_symm (u v : V Int (m + 1)) (h : ModN m u v) : ModN m v u := by
  obtain ⟨t, ht⟩ := h
  exact ⟨-t, fun b => by rw [ht b]; lib_int⟩

theorem modn_trans (u v w : V Int (m + 1)) (h1 : ModN m u v) (h2 : ModN m v w) : ModN m u w := by
  obtain ⟨t, ht⟩ := h1
  obtain ⟨s, hs⟩ := h2
  exact ⟨s + t, fun b => by rw [ht b, hs b]; lib_int⟩

theorem sum_split (f g : Zp m → Int) (t : Int) : ∀ l : List (Zp m),
    sumK LibInt.intR (fun a => (f a + t) * g a) l
      = sumK LibInt.intR (fun a => f a * g a) l + t * sumK LibInt.intR g l
  | [] => by show (0 : Int) = 0 + t * 0; lib_int
  | a :: l => by
    show (f a + t) * g a + sumK LibInt.intR (fun a => (f a + t) * g a) l
      = (f a * g a + sumK LibInt.intR (fun a => f a * g a) l) + t * (g a + sumK LibInt.intR g l)
    rw [sum_split f g t l]
    generalize sumK LibInt.intR (fun a => f a * g a) l = X
    generalize sumK LibInt.intR g l = Y
    lib_int

/-- **u ≡ u' (mod N) ⇒ u v ≡ u' v (mod N)**. -/
theorem modn_mul (u u' v : V Int (m + 1)) (h : ModN m u u') :
    ModN m (gmul LibInt.intR m u v) (gmul LibInt.intR m u' v) := by
  obtain ⟨t, ht⟩ := h
  refine ⟨t * sumK LibInt.intR (gt LibInt.intR m v) (L m), fun b => ?_⟩
  rw [gt_mul, gt_mul]
  have e1 : sumK LibInt.intR (fun a => LibInt.intR.mul (gt LibInt.intR m u a)
        (gt LibInt.intR m v ((ring m).add b ((ring m).neg a)))) (L m)
      = sumK LibInt.intR (fun a => (gt LibInt.intR m u' a + t) * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))) (L m) :=
    sumK_ext LibInt.intR _ _ (fun a => by
      show gt LibInt.intR m u a * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))
        = (gt LibInt.intR m u' a + t) * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))
      rw [ht a]) (L m)
  rw [e1, sum_split]
  have e2 : sumK LibInt.intR (fun a => gt LibInt.intR m v ((ring m).add b ((ring m).neg a))) (L m)
      = sumK LibInt.intR (gt LibInt.intR m v) (L m) := by
    have hinv : ∀ a, (ring m).add b ((ring m).neg ((ring m).add b ((ring m).neg a))) = a := fun a => by lib_ring
    exact reindex LibInt.intR (L m) (CMInert.complete_allZ m LibInt.intR)
      (fun a => (ring m).add b ((ring m).neg a)) (fun a => (ring m).add b ((ring m).neg a)) hinv hinv
      (gt LibInt.intR m v)
  rw [e2]
  show sumK LibInt.intR (fun a => gt LibInt.intR m u' a * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))) (L m)
      + t * sumK LibInt.intR (gt LibInt.intR m v) (L m)
    = sumK LibInt.intR (fun a => LibInt.intR.mul (gt LibInt.intR m u' a) (gt LibInt.intR m v ((ring m).add b ((ring m).neg a)))) (L m)
      + t * sumK LibInt.intR (gt LibInt.intR m v) (L m)
  rfl

end ModN

#print axioms ModN.modn_refl
#print axioms ModN.modn_symm
#print axioms ModN.modn_trans
#print axioms ModN.sum_split
#print axioms ModN.modn_mul

lang/IntCancel.lean

/-
  lang/IntCancel — T49 (O3') (iii): **cancellation of a nonzero integer** (LibInt.int_noZD), and the
  coefficient form "c·w ≡ const ⇒ w constant" used to divide u ū ≡ 1 (mod N) by p^(2(ℓ−2)).
    int_cancel   c ≠ 0, c x = c y ⇒ x = y
    const_div    c ≠ 0, c·w(b) = s for every b ⇒ w(b) = w(b0) for every b
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lib.IntRing

namespace IntCancel

theorem int_cancel (c x y : Int) (hc : c ≠ 0) (h : c * x = c * y) : x = y := by
  have h0 : LibInt.intR.mul c (x - y) = LibInt.intR.zero := by
    show c * (x - y) = 0
    calc c * (x - y) = c * x - c * y := by lib_int
      _ = c * y - c * y := by rw [h]
      _ = 0 := by lib_int
  cases LibInt.int_noZD _ _ h0 with
  | inl hz => exact absurd hz hc
  | inr hd =>
    have hd' : x - y = 0 := hd
    calc x = (x - y) + y := by lib_int
      _ = 0 + y := by rw [hd']
      _ = y := by lib_int

theorem const_div {A : Type} (w : A → Int) (c s : Int) (hc : c ≠ 0) (h : ∀ b, c * w b = s) (b b0 : A) :
    w b = w b0 :=
  int_cancel c _ _ hc ((h b).trans (h b0).symm)

end IntCancel

#print axioms IntCancel.int_cancel
#print axioms IntCancel.const_div

lang/ModDiv.lean

/-
  lang/ModDiv — T49 (O3') (iii): **dividing a congruence mod N by a nonzero integer**, coefficient form.
  X(b) = c·Y(b) + s₁ and X(b) = c·D(b) + s₂ for every b, c ≠ 0 ⇒ Y(b) = D(b) + t for every b (t = Y(b₀) − D(b₀)).
  Used with c = p^(2(ℓ−2)), X = W·W̄ (W = A B̄ = p^(ℓ−2)u + sN), Y = u ū, D = δ₀: u ū ≡ 1 (mod N).
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.IntCancel

namespace ModDiv

theorem mod_div {A : Type} (X Y D : A → Int) (c s₁ s₂ : Int) (hc : c ≠ 0)
    (h1 : ∀ b, X b = c * Y b + s₁) (h2 : ∀ b, X b = c * D b + s₂) (b₀ : A) :
    ∀ b, Y b = D b + (Y b₀ - D b₀) := by
  have hw : ∀ b, c * (Y b - D b) = s₂ - s₁ := fun b => by
    calc c * (Y b - D b) = (c * Y b + s₁) - (c * D b + s₂) + (s₂ - s₁) := by lib_int
      _ = X b - X b + (s₂ - s₁) := by rw [← h1 b, ← h2 b]
      _ = s₂ - s₁ := by lib_int
  intro b
  have := IntCancel.const_div (fun b => Y b - D b) c (s₂ - s₁) hc hw b b₀
  calc Y b = D b + (Y b - D b) := by lib_int
    _ = D b + (Y b₀ - D b₀) := by rw [show Y b - D b = Y b₀ - D b₀ from this]

end ModDiv

#print axioms ModDiv.mod_div

lang/ModSq.lean

/-
  lang/ModSq — T49 (O3') (iii): **(c u + sN)·(c ū + s'N) ≡ c² u ū (mod N)** in coefficient form.
    sc            c·u (coefficientwise)
    gt_sc_mul     (c u)·(c v) has coefficients c²·(u v)(b)
    modn_sc       W(b) = c u(b) + s ⇒ W ≡ c u (mod N)
    modn_gsig     W ≡ W' ⇒ σ_d W ≡ σ_d W'
    sq_form       W(b) = c u(b) + s ⇒ ∃ s₁, (W·σ_(−1)W)(b) = c²·(u·σ_(−1)u)(b) + s₁
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.ModN
import lang.CycGal
import lang.StickBB

namespace ModSq

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing ModN

variable (m : Nat)

def sc (c : Int) (u : V Int (m + 1)) : V Int (m + 1) := tb m (fun b => c * gt LibInt.intR m u b)

theorem sum_sc (c d : Int) (f : Zp m → Int) : ∀ l : List (Zp m),
    sumK LibInt.intR (fun a => (c * d) * f a) l = (c * d) * sumK LibInt.intR f l
  | [] => by show (0 : Int) = c * d * 0; lib_int
  | a :: l => by
    show c * d * f a + sumK LibInt.intR (fun a => (c * d) * f a) l = c * d * (f a + sumK LibInt.intR f l)
    rw [sum_sc c d f l]; lib_int

theorem gt_sc_mul (c : Int) (u v : V Int (m + 1)) (b : Zp m) :
    gt LibInt.intR m (gmul LibInt.intR m (sc m c u) (sc m c v)) b = c * c * gt LibInt.intR m (gmul LibInt.intR m u v) b := by
  rw [gt_mul, gt_mul]
  exact (sumK_ext LibInt.intR _
    (fun a => (c * c) * (gt LibInt.intR m u a * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))))
    (fun a => by
      show gt LibInt.intR m (sc m c u) a * gt LibInt.intR m (sc m c v) ((ring m).add b ((ring m).neg a)) = _
      unfold sc
      rw [gt_tb, gt_tb]
      lib_int) (L m)).trans
    (sum_sc m c c (fun a => gt LibInt.intR m u a * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))) (L m))

theorem modn_sc (c s : Int) (W u : V Int (m + 1)) (h : ∀ b, gt LibInt.intR m W b = c * gt LibInt.intR m u b + s) :
    ModN m W (sc m c u) := ⟨s, fun b => by unfold sc; rw [gt_tb]; exact h b⟩

theorem modn_gsig (d : Zp m) (W W' : V Int (m + 1)) (h : ModN m W W') :
    ModN m (CycGal.gsig LibInt.intR m d W) (CycGal.gsig LibInt.intR m d W') := by
  obtain ⟨t, ht⟩ := h
  exact ⟨t, fun b => by rw [CycGal.gt_gsig, CycGal.gt_gsig]; exact ht _⟩

theorem gsig_sc (d : Zp m) (c : Int) (u : V Int (m + 1)) :
    CycGal.gsig LibInt.intR m d (sc m c u) = sc m c (CycGal.gsig LibInt.intR m d u) :=
  ext LibInt.intR m _ _ (fun b => by unfold sc; rw [CycGal.gt_gsig, gt_tb, gt_tb, CycGal.gt_gsig])

/-- **(c u + sN)·σ_(−1)(c u + sN) ≡ c² u ū (mod N)**. -/
theorem sq_form (c s : Int) (W u : V Int (m + 1)) (h : ∀ b, gt LibInt.intR m W b = c * gt LibInt.intR m u b + s) :
    ∃ s₁, ∀ b, gt LibInt.intR m (gmul LibInt.intR m W (StickBB.J m W)) b
      = c * c * gt LibInt.intR m (gmul LibInt.intR m u (StickBB.J m u)) b + s₁ := by
  have h1 : ModN m W (sc m c u) := modn_sc m c s W u h
  have h2 : ModN m (StickBB.J m W) (sc m c (StickBB.J m u)) := by
    have := modn_gsig m (StickBB.e m) W (sc m c u) h1
    show ModN m (CycGal.gsig LibInt.intR m (StickBB.e m) W) (sc m c (CycGal.gsig LibInt.intR m (StickBB.e m) u))
    rw [← gsig_sc]; exact this
  have h3 := modn_mul m W (sc m c u) (StickBB.J m W) h1
  have h4 := modn_mul m (StickBB.J m W) (sc m c (StickBB.J m u)) (sc m c u) h2
  rw [gm_comm LibInt.intR m (StickBB.J m W) (sc m c u), gm_comm LibInt.intR m (sc m c (StickBB.J m u)) (sc m c u)] at h4
  obtain ⟨t, ht⟩ := modn_trans m _ _ _ h3 h4
  exact ⟨t, fun b => by rw [ht b, gt_sc_mul]⟩

end ModSq

#print axioms ModSq.sum_sc
#print axioms ModSq.gt_sc_mul
#print axioms ModSq.modn_sc
#print axioms ModSq.modn_gsig
#print axioms ModSq.gsig_sc
#print axioms ModSq.sq_form

lang/UStep.lean

/-
  lang/UStep — T49 (O3') (iii), assembly: **W = c·u + sN and W·W̄ ≡ c² (mod N) ⇒ u = c'N ± σ^k** (ℓ = p + 2 prime).
    uu_one     ⇒ u ū = δ₀ + t·N                     (ModSq.sq_form, ModDiv.mod_div, c ≠ 0)
    u_root     ⇒ the coefficients of u are a constant plus a signed unit vector (UnitCirc4.unit_circ)
  With W = A·σ_(−1)(B), c = p^(ℓ−2) (A Ā = B B̄ = p^(ℓ−2)) this is A = ±ζ^k·B up to the N-part.
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.ModSq
import lang.ModDiv
import lang.UnitCirc4

namespace UStep

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing

theorem uu_one (m : Nat) (c s : Int) (hc : c ≠ 0) (W u : V Int (m + 1))
    (hW : ∀ b, gt LibInt.intR m W b = c * gt LibInt.intR m u b + s)
    (hX : ∃ s₂, ∀ b, gt LibInt.intR m (gmul LibInt.intR m W (StickBB.J m W)) b
      = c * c * delta LibInt.intR b (ring m).zero + s₂) :
    ∃ t, ∀ b, gt LibInt.intR m (gmul LibInt.intR m u (StickBB.J m u)) b
      = LibInt.intR.add (delta LibInt.intR b (ring m).zero) t := by
  obtain ⟨s₁, h1⟩ := ModSq.sq_form m c s W u hW
  obtain ⟨s₂, h2⟩ := hX
  have hcc : c * c ≠ 0 := fun h => by
    cases LibInt.int_noZD c c h with
    | inl h' => exact hc h'
    | inr h' => exact hc h'
  exact ⟨_, ModDiv.mod_div (fun b => gt LibInt.intR m (gmul LibInt.intR m W (StickBB.J m W)) b)
    (fun b => gt LibInt.intR m (gmul LibInt.intR m u (StickBB.J m u)) b)
    (fun b => delta LibInt.intR b (ring m).zero) (c * c) s₁ s₂ hcc h1 h2 (ring m).zero⟩

/-- **u = c'N ± σ^k**. -/
theorem u_root (p : Nat) (hp : ∀ d, Dv d (p + 1 + 1) → d = 1 ∨ d = p + 1 + 1) (c s : Int) (hc : c ≠ 0)
    (W u : V Int (p + 1 + 1))
    (hW : ∀ b, gt LibInt.intR (p + 1) W b = c * gt LibInt.intR (p + 1) u b + s)
    (hX : ∃ s₂, ∀ b, gt LibInt.intR (p + 1) (gmul LibInt.intR (p + 1) W (StickBB.J (p + 1) W)) b
      = c * c * delta LibInt.intR b (ring (p + 1)).zero + s₂) :
    ∃ c' ε, (ε = 1 ∨ ε = -1) ∧
      UnitCirc1.sm (((L (p + 1)).map (gt LibInt.intR (p + 1) u)).map (fun a => a - c')) = ε ∧
      UnitCirc2.OneHot (((L (p + 1)).map (gt LibInt.intR (p + 1) u)).map (fun a => a - c')) := by
  obtain ⟨t, ht⟩ := uu_one (p + 1) c s hc W u hW hX
  exact UnitCirc4.unit_circ p hp u t ht

end UStep

#print axioms UStep.uu_one
#print axioms UStep.u_root

lang/CastConst.lean

/-
  lang/CastConst — T49 (O3') (i'): **equal images in ℤ/(M+1) ⇒ integers differ by a multiple of M + 1**.
    cast_eq    castZ x = castZ y in ℤ/(M+1) ⇒ x = y + (M + 1)·q      (IntCast.castZ_add, HasseDavenport.castZ_zero_dvd)
  Used to turn RootsConst (coefficients of W constant mod p^k) into W(b) = W(b₀) + p^k·u(b).
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.HasseDavenport
import lib.IntRing
import lib.RingTac

namespace CastConst

open ZMod LibRing

theorem cast_eq (M : Nat) (x y : Int) (h : castZ (ring M) x = castZ (ring M) y) :
    ∃ q : Int, x = y + Int.ofNat (M + 1) * q := by
  have ha := IntCast.castZ_add (ring M) (x - y) y
  rw [show x - y + y = x by lib_int, h] at ha
  have h0 : castZ (ring M) (x - y) = (ring M).zero := by
    calc castZ (ring M) (x - y)
        = (ring M).add ((ring M).add (castZ (ring M) (x - y)) (castZ (ring M) y)) ((ring M).neg (castZ (ring M) y)) := by
          lib_ring
      _ = (ring M).add (castZ (ring M) y) ((ring M).neg (castZ (ring M) y)) := by rw [← ha]
      _ = (ring M).zero := by lib_ring
  obtain ⟨q, hq⟩ := HasseDavenport.castZ_zero_dvd M (x - y) h0
  exact ⟨q, by calc x = (x - y) + y := by lib_int
    _ = Int.ofNat (M + 1) * q + y := by rw [hq]
    _ = y + Int.ofNat (M + 1) * q := by lib_int⟩

end CastConst

#print axioms CastConst.cast_eq

lang/FinChoice.lean

/-
  lang/FinChoice — T49 (O3') (2): **finite choice without the choice axiom**: per-index existence below n gives
  one function (induction on n, the new value patched in with `if i = n`).
    fin_choice   (∀ i < n, ∃ q, P i q) ⇒ ∃ g, ∀ i < n, P i (g i)
  Used to assemble u(b) with W(b) = W(b₀) + p^k·u(b) from CastConst.cast_eq at every b.
  Lean 4 core only, proofs by hand, no axioms.
-/

namespace FinChoice

theorem ite_t {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : c) : @ite α c d a b = a := by
  cases d with
  | isTrue _ => rfl
  | isFalse hc => exact absurd h hc

theorem ite_f {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : ¬ c) : @ite α c d a b = b := by
  cases d with
  | isTrue hc => exact absurd hc h
  | isFalse _ => rfl

theorem fin_choice {A : Type} (P : Nat → A → Prop) (a0 : A) :
    ∀ n, (∀ i, i < n → ∃ q, P i q) → ∃ g : Nat → A, ∀ i, i < n → P i (g i)
  | 0, _ => ⟨fun _ => a0, fun i hi => absurd hi (Nat.not_lt_zero i)⟩
  | n + 1, h => by
    obtain ⟨g, hg⟩ := fin_choice P a0 n (fun i hi => h i (Nat.lt_trans hi (Nat.lt_succ_self n)))
    obtain ⟨q, hq⟩ := h n (Nat.lt_succ_self n)
    refine ⟨fun i => if i = n then q else g i, fun i hi => ?_⟩
    cases Nat.lt_or_ge i n with
    | inl hlt =>
      have hne : i ≠ n := fun e => Nat.lt_irrefl n (e ▸ hlt)
      show P i (if i = n then q else g i)
      rw [ite_f _ _ hne]; exact hg i hlt
    | inr hge =>
      have heq : i = n := Nat.le_antisymm (Nat.le_of_lt_succ hi) hge
      show P i (if i = n then q else g i)
      rw [ite_t _ _ heq, heq]; exact hq

end FinChoice

#print axioms FinChoice.ite_t
#print axioms FinChoice.ite_f
#print axioms FinChoice.fin_choice

lang/WForm.lean

/-
  lang/WForm — T49 (O3') (2): **coefficients constant mod (M+1) ⇒ W = (M+1)·u + s·N** in ℤ[C_ℓ].
    w_form   castZ W(j) = castZ W(ℓ−1) in ℤ/(M+1) for all j ⇒ ∃ u, W(b) = (M+1)·u(b) + W(ℓ−1) for every b
             (CastConst.cast_eq at each j, FinChoice.fin_choice to assemble u, no choice axiom)
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.CastConst
import lang.FinChoice
import lang.GroupRing

namespace WForm

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing LibRing

theorem w_form (m M : Nat) (W : V Int (m + 1))
    (h : ∀ j, j < m + 1 → castZ (ring M) (gt LibInt.intR m W (mk m j)) = castZ (ring M) (gt LibInt.intR m W (mk m m))) :
    ∃ u : V Int (m + 1), ∀ b, gt LibInt.intR m W b = Int.ofNat (M + 1) * gt LibInt.intR m u b + gt LibInt.intR m W (mk m m) := by
  obtain ⟨g, hg⟩ := FinChoice.fin_choice
    (fun j q => gt LibInt.intR m W (mk m j) = gt LibInt.intR m W (mk m m) + Int.ofNat (M + 1) * q) 0 (m + 1)
    (fun j hj => CastConst.cast_eq M _ _ (h j hj))
  refine ⟨tb m (fun b => g b.val), fun b => ?_⟩
  rw [gt_tb]
  have hb := hg b.val b.property
  rw [mk_val m b] at hb
  rw [hb]
  lib_int

end WForm

#print axioms WForm.w_form

lang/ValArith.lean

/-
  lang/ValArith — T49 (O3') (v1): **valuation arithmetic** in any commutative ring with a nilpotent P.
    val_mul    (P^a U)(P^b U') = P^(a+b) (U U')
    val_pow    (P^a U)^n = P^(a n) U^n
    val_zero   P^N = 0, N ≤ a ⇒ P^a U = 0
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.SymProdZ

namespace ValArith

open Frobenius

variable {R : Type} (K : DetChain.CRing R)

theorem val_mul (P U U' : R) (a b : Nat) :
    K.mul (K.mul (pw K P a) U) (K.mul (pw K P b) U') = K.mul (pw K P (a + b)) (K.mul U U') := by
  rw [SymProdW.pw_add]
  generalize pw K P a = A
  generalize pw K P b = B
  lib_ring

theorem val_pow (P U : R) (a : Nat) : ∀ n, pw K (K.mul (pw K P a) U) n = K.mul (pw K P (a * n)) (pw K U n)
  | 0 => by show K.one = K.mul K.one K.one; lib_ring
  | n + 1 => by
    show K.mul (pw K (K.mul (pw K P a) U) n) (K.mul (pw K P a) U) = K.mul (pw K P (a * n + a)) (K.mul (pw K U n) U)
    rw [val_pow P U a n, val_mul]

theorem val_zero (P U : R) (N a : Nat) (hN : pw K P N = K.zero) (ha : N ≤ a) : K.mul (pw K P a) U = K.zero := by
  obtain ⟨d, hd⟩ := SymProdZ.le_ex ha
  rw [← hd, SymProdW.pw_add, hN]
  lib_ring

end ValArith

#print axioms ValArith.val_mul
#print axioms ValArith.val_pow
#print axioms ValArith.val_zero

lang/ProdVal.lean

/-
  lang/ProdVal — T49 (O3') (v3), abstract: **the valuation of a product of powers is the sum over the factors
  divisible by P**. Entries (x, n, f): f = true ⇒ x = P·U with U a unit, f = false ⇒ x a unit. Then
    pv        Π x^n = P^(Σ_(f = true) n) · U for a unit U
  With exactly one flagged factor (lang/NormEval, ExactOne) the valuation of φ_(z^c)(σ_(−1)B) is that factor's exponent.
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ValArith
import lang.FinChoice

namespace ProdVal

open Frobenius

variable {R : Type} (K : DetChain.CRing R) (P : R)

def prd : List (R × Nat × Bool) → R
  | [] => K.one
  | e :: es => K.mul (pw K e.1 e.2.1) (prd es)

def fexp : List (R × Nat × Bool) → Nat
  | [] => 0
  | e :: es => (if e.2.2 = true then e.2.1 else 0) + fexp es

def Ok : List (R × Nat × Bool) → Prop
  | [] => True
  | e :: es => (e.2.2 = true → ∃ U w, e.1 = K.mul P U ∧ K.mul U w = K.one) ∧
      (e.2.2 = false → ∃ w, K.mul e.1 w = K.one) ∧ Ok es

theorem unit_pw (x w : R) (h : K.mul x w = K.one) : ∀ n, K.mul (pw K x n) (pw K w n) = K.one
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | n + 1 => by
    show K.mul (K.mul (pw K x n) x) (K.mul (pw K w n) w) = K.one
    calc K.mul (K.mul (pw K x n) x) (K.mul (pw K w n) w) = K.mul (K.mul (pw K x n) (pw K w n)) (K.mul x w) := by
          lib_ring
      _ = K.one := by rw [unit_pw x w h n, h]; lib_ring

/-- **Π x^n = P^(flagged exponent sum) · unit**. -/
theorem pv : ∀ es, Ok K P es → ∃ U w, prd K es = K.mul (pw K P (fexp es)) U ∧ K.mul U w = K.one
  | [], _ => ⟨K.one, K.one, by show K.one = K.mul K.one K.one; lib_ring, by lib_ring⟩
  | e :: es, ⟨h1, h2, h3⟩ => by
    obtain ⟨U, w, hU, hw⟩ := pv es h3
    cases hf : e.2.2 with
    | true =>
      obtain ⟨V, v, hV, hv⟩ := h1 hf
      refine ⟨K.mul (pw K V e.2.1) U, K.mul (pw K v e.2.1) w, ?_, ?_⟩
      · show K.mul (pw K e.1 e.2.1) (prd K es) = K.mul (pw K P ((if e.2.2 = true then e.2.1 else 0) + fexp es)) _
        rw [hf, FinChoice.ite_t _ _ rfl, hU, hV, pw_mul, SymProdW.pw_add]
        generalize pw K P e.2.1 = A
        generalize pw K P (fexp es) = B
        lib_ring
      · calc K.mul (K.mul (pw K V e.2.1) U) (K.mul (pw K v e.2.1) w)
            = K.mul (K.mul (pw K V e.2.1) (pw K v e.2.1)) (K.mul U w) := by lib_ring
          _ = K.one := by rw [unit_pw K V v hv, hw]; lib_ring
    | false =>
      obtain ⟨v, hv⟩ := h2 hf
      refine ⟨K.mul (pw K e.1 e.2.1) U, K.mul (pw K v e.2.1) w, ?_, ?_⟩
      · show K.mul (pw K e.1 e.2.1) (prd K es) = K.mul (pw K P ((if e.2.2 = true then e.2.1 else 0) + fexp es)) _
        rw [hf, FinChoice.ite_f _ _ (fun h => Bool.noConfusion h), hU, show 0 + fexp es = fexp es by lib_nat]
        lib_ring
      · calc K.mul (K.mul (pw K e.1 e.2.1) U) (K.mul (pw K v e.2.1) w)
            = K.mul (K.mul (pw K e.1 e.2.1) (pw K v e.2.1)) (K.mul U w) := by lib_ring
          _ = K.one := by rw [unit_pw K e.1 v hv, hw]; lib_ring

end ProdVal

#print axioms ProdVal.unit_pw
#print axioms ProdVal.pv

#print axioms: 26 theorems, no axioms.

78.380 Conditional element-level Stickelberger relation

If \(W=A\,\sigma _{-1}(B)\) vanishes at every Teichmüller point modulo \(p^k\) and \(W\bar W\equiv p^{2k}\), then \(W=p^k(c'N\pm \sigma ^j)+sN\); both hypotheses reduce to valuations and norms.

\[ \varphi _{z^c}(W)=0\ \forall c,\ W\bar W\equiv p^{2k}\ \Longrightarrow \ W\equiv \pm p^k\sigma ^j \pmod N. \]

lang/PhzPw.lean

/-
  lang/PhzPw — T49 (O3') (v3): **evaluation of powers and of the product B**: φ_z(v^n) = φ_z(v)^n, and
  φ_z(AbsPair.Bp entries) = Π φ_z(x)^a φ_z(y)^b (z^ℓ = 1).
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.AbsPair
import lang.CycEis

namespace PhzPw

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

variable {S : Type} (E : CRing S) (m : Nat) (z : S) (hz : pw E z (m + 1) = E.one)

include hz in
theorem phz_pw (v : V Int (m + 1)) : ∀ n, CycEis.phz E m z (pw (gring LibInt.intR m) v n) = pw E (CycEis.phz E m z v) n
  | 0 => CycEis.phz_one E m z
  | n + 1 => by
    show CycEis.phz E m z ((gring LibInt.intR m).mul (pw (gring LibInt.intR m) v n) v)
      = E.mul (pw E (CycEis.phz E m z v) n) (CycEis.phz E m z v)
    rw [CycEis.phz_mul E m z hz, phz_pw v n]

def evBp : List (V Int (m + 1) × V Int (m + 1) × Nat × Nat) → S
  | [] => E.one
  | e :: es => E.mul (E.mul (pw E (CycEis.phz E m z e.1) e.2.2.1) (pw E (CycEis.phz E m z e.2.1) e.2.2.2)) (evBp es)

include hz in
theorem phz_Bp : ∀ es, CycEis.phz E m z (AbsPair.Bp (gring LibInt.intR m) es) = evBp E m z es
  | [] => CycEis.phz_one E m z
  | e :: es => by
    show CycEis.phz E m z ((gring LibInt.intR m).mul ((gring LibInt.intR m).mul (pw (gring LibInt.intR m) e.1 e.2.2.1)
        (pw (gring LibInt.intR m) e.2.1 e.2.2.2)) (AbsPair.Bp (gring LibInt.intR m) es))
      = E.mul (E.mul (pw E (CycEis.phz E m z e.1) e.2.2.1) (pw E (CycEis.phz E m z e.2.1) e.2.2.2)) (evBp E m z es)
    rw [CycEis.phz_mul E m z hz, CycEis.phz_mul E m z hz, phz_pw E m z hz, phz_pw E m z hz, phz_Bp es]

end PhzPw

#print axioms PhzPw.phz_pw
#print axioms PhzPw.phz_Bp

lang/PhzSum.lean

/-
  lang/PhzSum — T49 (O3') assembly: **φ_z(v) = Σ_b castZ(v(b))·z^b** over CMInert.allZ (the RootsConst form).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.CycEis

namespace PhzSum

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing LibRing

theorem phz_sum {S : Type} (E : CRing S) (m : Nat) (z : S) (v : V Int (m + 1)) :
    CycEis.phz E m z v
      = sumK E (fun b => E.mul (castZ E (gt LibInt.intR m v b)) (Frobenius.pw E z b.val)) (CMInert.allZ m (m + 1)) := by
  show sumK E (fun b => E.mul (gt E m (GEval.gmap LibInt.intR m (castZ E) v) b) (Frobenius.pw E z b.val)) (L m) = _
  exact sumK_ext E _ _ (fun b => by
    show E.mul (gt E m (tb m (fun a => castZ E (gt LibInt.intR m v a))) b) (Frobenius.pw E z b.val) = _
    rw [gt_tb]) (L m)

end PhzSum

#print axioms PhzSum.phz_sum

lang/GsigPow.lean

/-
  lang/GsigPow — T49 (O3') (v2): **φ_(z^e)(σ_d α) = φ_(z^(e·c))(α)** for c·d = 1 in 𝔽_ℓ, z^ℓ = 1
  (CycGalEv.phz_gsig at the root z^e, SymProdZ.pw_pw).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.CycGalEv
import lang.SymProdZ

namespace GsigPow

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

theorem phz_gsig_pow {S : Type} (E : CRing S) (m : Nat) (z : S) (hz : pw E z (m + 1) = E.one) (e : Nat)
    (c d : Zp m) (hcd : (ring m).mul c d = (ring m).one) (v : V Int (m + 1)) :
    CycEis.phz E m (pw E z e) (CycGal.gsig LibInt.intR m d v) = CycEis.phz E m (pw E z (e * c.val)) v := by
  have hze : pw E (pw E z e) (m + 1) = E.one := by
    rw [SymProdZ.pw_pw]
    exact SymProdW.pw_mulE E z (m + 1) hz e
  rw [CycGalEv.phz_gsig E m (pw E z e) hze c d hcd v, SymProdZ.pw_pw]

end GsigPow

#print axioms GsigPow.phz_gsig_pow

lang/RootsW.lean

/-
  lang/RootsW — T49 (O3') assembly (2): **W ∈ ℤ[C_ℓ] with φ_(z^c)(W) = 0 in ℤ/p^k for c = 1..ℓ−1 is
  (M+1)·u + s·N** (ℓ = m + 3, z the Teichmüller root, M + 1 = p·k', (p·1)^n = 0 in ℤ/M).
    udist_b     UDist [z^(ℓ−1), …, z^1]                     (TeichUnits.diff_unit, bounded PtsDist)
    ptok        each z^a: (z^a)^ℓ = 1, z^a − 1 a unit, Σ_b castZ(W(b)) (z^a)^b = φ_(z^a)(W) = 0
    roots_w     ⇒ ∃ u, W(b) = (M + 1)·u(b) + W(ℓ − 1)     (RootsConst, WForm)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.TeichUnits
import lang.PtsDist
import lang.RootsConst
import lang.WForm
import lang.PhzSum

namespace RootsW

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing UnitRoots

variable (J M k' n m : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
  (hk : M + 1 = (J + 1 + 1) * k') (hPn : pw (ring M) (ExactP2.P J M) n = (ring M).zero)
  (z : Zp M) (hz : OrdMul.Ord (ring (J + 1)) (RedP2.red J M z) (m + 1 + 1 + 1))
  (hzl : pw (ring M) z (m + 1 + 1 + 1) = (ring M).one)

include hpr hk hPn hz in
theorem udist_b : ∀ t, t ≤ m + 1 + 1 → UDist (ring M) (PtsDist.pts (ring M) z t)
  | 0, _ => trivial
  | t + 1, ht => by
    refine ⟨fun s hs => ?_, udist_b t (Nat.le_trans (Nat.le_succ t) ht)⟩
    obtain ⟨a, h1, h2, h3⟩ := PtsDist.mem_pts (ring M) z t s hs
    rw [h3]
    exact TeichUnits.diff_unit J M k' _ m hpr hk hPn z hz a (t + 1) (Nat.lt_succ_of_le h2) (Nat.lt_succ_of_le ht)

include hpr hk hPn hz hzl in
theorem ptok (W : V Int (m + 1 + 1 + 1))
    (hW : ∀ c, 1 ≤ c → c ≤ m + 1 + 1 → CycEis.phz (ring M) (m + 1 + 1) (pw (ring M) z c) W = (ring M).zero) :
    ∀ t, t ≤ m + 1 + 1 →
      RootsConst.PtOK (ring M) (m + 1 + 1) (fun b => LibRing.castZ (ring M) (gt LibInt.intR (m + 1 + 1) W b))
        (PtsDist.pts (ring M) z t)
  | 0, _ => trivial
  | t + 1, ht => by
    refine ⟨⟨?_, ?_, ?_⟩, ptok W hW t (Nat.le_trans (Nat.le_succ t) ht)⟩
    · rw [SymProdZ.pw_pw]
      exact SymProdW.pw_mulE (ring M) z _ hzl (t + 1)
    · obtain ⟨w, hw⟩ := TeichUnits.diff_unit J M k' _ m hpr hk hPn z hz 0 (t + 1) (Nat.succ_pos t) (Nat.lt_succ_of_le ht)
      refine ⟨(ring M).neg w, ?_⟩
      calc (ring M).mul ((ring M).add (pw (ring M) z (t + 1)) ((ring M).neg (ring M).one)) ((ring M).neg w)
          = (ring M).mul ((ring M).add (pw (ring M) z 0) ((ring M).neg (pw (ring M) z (t + 1)))) w := by
            show _ = (ring M).mul ((ring M).add (ring M).one ((ring M).neg (pw (ring M) z (t + 1)))) w
            lib_ring
        _ = (ring M).one := hw
    · exact (PhzSum.phz_sum (ring M) (m + 1 + 1) (pw (ring M) z (t + 1)) W).symm.trans
        (hW (t + 1) (Nat.succ_le_succ (Nat.zero_le t)) ht)

include hpr hk hPn hz hzl in
/-- **W = (M + 1)·u + W(ℓ − 1)·N** when φ_(z^c)(W) = 0 for all c. -/
theorem roots_w (W : V Int (m + 1 + 1 + 1))
    (hW : ∀ c, 1 ≤ c → c ≤ m + 1 + 1 → CycEis.phz (ring M) (m + 1 + 1) (pw (ring M) z c) W = (ring M).zero) :
    ∃ u : V Int (m + 1 + 1 + 1), ∀ b, gt LibInt.intR (m + 1 + 1) W b
      = Int.ofNat (M + 1) * gt LibInt.intR (m + 1 + 1) u b + gt LibInt.intR (m + 1 + 1) W (mk (m + 1 + 1) (m + 1 + 1)) := by
  apply WForm.w_form
  intro j hj
  cases Nat.lt_or_ge j (m + 1 + 1) with
  | inl hlt =>
    exact RootsConst.roots_const (ring M) (m + 1 + 1) (fun b => LibRing.castZ (ring M) (gt LibInt.intR (m + 1 + 1) W b)) (m + 1) rfl
      (PtsDist.pts (ring M) z (m + 1 + 1)) (PtsDist.len_pts (ring M) z _)
      (udist_b J M k' _ m hpr hk hPn z hz _ (Nat.le_refl _)) (ptok J M k' _ m hpr hk hPn z hz hzl W hW _ (Nat.le_refl _)) j hlt
  | inr hge =>
    have : j = m + 1 + 1 := Nat.le_antisymm (Nat.le_of_lt_succ hj) hge
    rw [this]

end RootsW

#print axioms RootsW.udist_b
#print axioms RootsW.ptok
#print axioms RootsW.roots_w

lang/StickCond.lean

/-
  lang/StickCond — T49 (O3') assembly, conditional form: **if W ∈ ℤ[C_ℓ] vanishes at every z^c in ℤ/p^k and
  W·W̄ ≡ p^(2k) (mod N), then W = p^k·u + sN with u = c'N ± σ^j** (RootsW + UStep).
  With W = A·σ_(−1)(B) the two hypotheses are (v3)–(v4) (valuations) and CycAbar·BBNorm (norms); the conclusion is
  the element-level Stickelberger relation A ≡ ±σ^j B.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.RootsW
import lang.UStep

namespace StickCond

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

theorem stick_cond (J M k' n m : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
    (hk : M + 1 = (J + 1 + 1) * k') (hPn : pw (ring M) (ExactP2.P J M) n = (ring M).zero)
    (z : Zp M) (hz : OrdMul.Ord (ring (J + 1)) (RedP2.red J M z) (m + 1 + 1 + 1))
    (hzl : pw (ring M) z (m + 1 + 1 + 1) = (ring M).one)
    (hℓ : ∀ d, Dv d (m + 1 + 1 + 1) → d = 1 ∨ d = m + 1 + 1 + 1)
    (W : V Int (m + 1 + 1 + 1))
    (hW : ∀ c, 1 ≤ c → c ≤ m + 1 + 1 → CycEis.phz (ring M) (m + 1 + 1) (pw (ring M) z c) W = (ring M).zero)
    (hX : ∃ s₂, ∀ b, gt LibInt.intR (m + 1 + 1) (gmul LibInt.intR (m + 1 + 1) W (StickBB.J (m + 1 + 1) W)) b
      = Int.ofNat (M + 1) * Int.ofNat (M + 1) * delta LibInt.intR b (ring (m + 1 + 1)).zero + s₂) :
    ∃ u : V Int (m + 1 + 1 + 1), (∀ b, gt LibInt.intR (m + 1 + 1) W b
        = Int.ofNat (M + 1) * gt LibInt.intR (m + 1 + 1) u b + gt LibInt.intR (m + 1 + 1) W (mk (m + 1 + 1) (m + 1 + 1))) ∧
      ∃ c' ε, (ε = 1 ∨ ε = -1) ∧
        UnitCirc1.sm (((L (m + 1 + 1)).map (gt LibInt.intR (m + 1 + 1) u)).map (fun a => a - c')) = ε ∧
        UnitCirc2.OneHot (((L (m + 1 + 1)).map (gt LibInt.intR (m + 1 + 1) u)).map (fun a => a - c')) := by
  obtain ⟨u, hu⟩ := RootsW.roots_w J M k' _ m hpr hk hPn z hz hzl W hW
  refine ⟨u, hu, ?_⟩
  exact UStep.u_root (m + 1) hℓ (Int.ofNat (M + 1)) _ (fun h => Nat.noConfusion (Int.ofNat.inj h)) W u hu hX

end StickCond

#print axioms StickCond.stick_cond

lang/WWbar.lean

/-
  lang/WWbar — T49 (O3') assembly (norm side): **W = A·σ_(−1)B with A Ā ≡ a·1, B B̄ ≡ b·1 (mod N) ⇒ W W̄ ≡ ab·1**.
    J_inv       σ_(−1)σ_(−1) = id
    gt_one      (1)(b) = [b = 0]
    sc_mul_l    (c u)·v = c (u v) coefficientwise
    ww          coefficient form: W W̄ (b) = ab·[b = 0] + s
  With a = b = p^(ℓ−2) (CycAbar, BBNorm and N(α) = p) this is hypothesis hX of StickCond.
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.ModSq

namespace WWbar

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing ModN

variable (m : Nat)

theorem J_inv (v : V Int (m + 1)) : StickBB.J m (StickBB.J m v) = v := by
  show CycGal.gsig LibInt.intR m (StickBB.e m) (CycGal.gsig LibInt.intR m (StickBB.e m) v) = v
  rw [StickBB.gsig_comp, StickBB.e_sq]
  exact ext LibInt.intR m _ _ (fun b => by
    rw [CycGal.gt_gsig, show (ring m).mul b (ring m).one = b by lib_ring])

theorem gt_one (b : Zp m) : gt LibInt.intR m (gone LibInt.intR m) b = delta LibInt.intR b (ring m).zero := gt_tb LibInt.intR m _ b

theorem sum_scal (c : Int) (f : Zp m → Int) : ∀ l : List (Zp m),
    sumK LibInt.intR (fun a => c * f a) l = c * sumK LibInt.intR f l
  | [] => by show (0 : Int) = c * 0; lib_int
  | a :: l => by
    show c * f a + sumK LibInt.intR (fun a => c * f a) l = c * (f a + sumK LibInt.intR f l)
    rw [sum_scal c f l]; lib_int

theorem sc_mul_l (c : Int) (u v : V Int (m + 1)) (b : Zp m) :
    gt LibInt.intR m (gmul LibInt.intR m (ModSq.sc m c u) v) b = c * gt LibInt.intR m (gmul LibInt.intR m u v) b := by
  rw [gt_mul, gt_mul]
  exact (sumK_ext LibInt.intR _
    (fun a => c * (gt LibInt.intR m u a * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))))
    (fun a => by
      show gt LibInt.intR m (ModSq.sc m c u) a * gt LibInt.intR m v ((ring m).add b ((ring m).neg a)) = _
      unfold ModSq.sc
      rw [gt_tb]
      lib_int) (L m)).trans
    (sum_scal m c (fun a => gt LibInt.intR m u a * gt LibInt.intR m v ((ring m).add b ((ring m).neg a))) (L m))

/-- **W W̄ ≡ ab (mod N)**. -/
theorem ww (A B : V Int (m + 1)) (a b sA sB : Int)
    (hA : ∀ x, gt LibInt.intR m (gmul LibInt.intR m A (StickBB.J m A)) x = a * delta LibInt.intR x (ring m).zero + sA)
    (hB : ∀ x, gt LibInt.intR m (gmul LibInt.intR m B (StickBB.J m B)) x = b * delta LibInt.intR x (ring m).zero + sB) :
    ∃ s, ∀ x, gt LibInt.intR m (gmul LibInt.intR m (gmul LibInt.intR m A (StickBB.J m B))
        (StickBB.J m (gmul LibInt.intR m A (StickBB.J m B)))) x
      = a * b * delta LibInt.intR x (ring m).zero + s := by
  have hJ : StickBB.J m (gmul LibInt.intR m A (StickBB.J m B))
      = gmul LibInt.intR m (StickBB.J m A) B := by
    show CycGal.gsig LibInt.intR m (StickBB.e m) (gmul LibInt.intR m A (StickBB.J m B)) = _
    rw [CycGal.gsig_mul LibInt.intR m (StickBB.e m) (StickBB.e m) (StickBB.e_sq m)]
    show gmul LibInt.intR m (StickBB.J m A) (StickBB.J m (StickBB.J m B)) = _
    rw [J_inv]
  have hre : gmul LibInt.intR m (gmul LibInt.intR m A (StickBB.J m B)) (gmul LibInt.intR m (StickBB.J m A) B)
      = gmul LibInt.intR m (gmul LibInt.intR m A (StickBB.J m A)) (gmul LibInt.intR m B (StickBB.J m B)) := by
    show (gring LibInt.intR m).mul ((gring LibInt.intR m).mul A (StickBB.J m B)) ((gring LibInt.intR m).mul (StickBB.J m A) B)
      = (gring LibInt.intR m).mul ((gring LibInt.intR m).mul A (StickBB.J m A)) ((gring LibInt.intR m).mul B (StickBB.J m B))
    generalize StickBB.J m A = JA
    generalize StickBB.J m B = JB
    lib_ring
  rw [hJ, hre]
  have h1 : ModN m (gmul LibInt.intR m A (StickBB.J m A)) (ModSq.sc m a (gone LibInt.intR m)) :=
    ModSq.modn_sc m a sA _ _ (fun x => by rw [hA x, gt_one])
  have h2 : ModN m (gmul LibInt.intR m B (StickBB.J m B)) (ModSq.sc m b (gone LibInt.intR m)) :=
    ModSq.modn_sc m b sB _ _ (fun x => by rw [hB x, gt_one])
  have h3 := modn_mul m _ _ (gmul LibInt.intR m B (StickBB.J m B)) h1
  have h4 := modn_mul m _ _ (ModSq.sc m a (gone LibInt.intR m)) h2
  rw [gm_comm LibInt.intR m (gmul LibInt.intR m B (StickBB.J m B)),
    gm_comm LibInt.intR m (ModSq.sc m b (gone LibInt.intR m))] at h4
  obtain ⟨t, ht⟩ := modn_trans m _ _ _ h3 h4
  refine ⟨t, fun x => ?_⟩
  rw [ht x, sc_mul_l, gm_one]
  unfold ModSq.sc
  rw [gt_tb, gt_one]
  lib_int

end WWbar

#print axioms WWbar.J_inv
#print axioms WWbar.gt_one
#print axioms WWbar.sum_scal
#print axioms WWbar.sc_mul_l
#print axioms WWbar.ww

lang/EvalZero.lean

/-
  lang/EvalZero — T49 (O3') (v4): **φ_z(A·σ_(−1)B) = 0 in ℤ/p^k when the valuations add up to k**:
  φ_z(A) = P^a U, φ_z(σ_(−1)B) = P^b U', a + b = k, P^k = 0 ⇒ φ_z(A·σ_(−1)B) = 0
  (CycEis.phz_mul, ValArith). With a = ℓ − 1 − c (ValC5) and b = c − 1 (bbar_val_test) this is hypothesis hW of StickCond.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.ValArith
import lang.StickBB
import lang.CycEis

namespace EvalZero

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

theorem eval_zero {S : Type} (E : CRing S) (m : Nat) (z : S) (hz : pw E z (m + 1) = E.one)
    (A B : V Int (m + 1)) (P U U' : S) (a b k : Nat)
    (hA : CycEis.phz E m z A = E.mul (pw E P a) U) (hB : CycEis.phz E m z (StickBB.J m B) = E.mul (pw E P b) U')
    (hk : a + b = k) (hP : pw E P k = E.zero) :
    CycEis.phz E m z (gmul LibInt.intR m A (StickBB.J m B)) = E.zero := by
  show CycEis.phz E m z ((gring LibInt.intR m).mul A (StickBB.J m B)) = E.zero
  rw [CycEis.phz_mul E m z hz, hA, hB, ValArith.val_mul, hk]
  exact ValArith.val_zero E P _ k k hP (Nat.le_refl k)

end EvalZero

#print axioms EvalZero.eval_zero

#print axioms: 14 theorems, no axioms.

78.381 The hypotheses of the conditional relation, concretely

The norm \(A\bar A\equiv p^{\ell -2}\) in the group ring via the quotient map, \(B\bar B=N(\alpha )^{\ell -2}\), exactly one conjugate of a norm-\(p\) element divisible by \(p\) modulo \(p^n\), and the valuation of \(\sigma _{-1}B\) at every \(P_e\).

\[ A\bar A\equiv p^{\ell -2}\pmod N,\qquad \varphi _{z^e}(\sigma _{-1}B)=p^{F_e}U_e,\ F_e\ge e-1. \]

lang/NsmCoef.lean

/-
  lang/NsmCoef — T49 (O3') norm side: **coefficients of n·1 and (n·1)^k in ℤ[C_ℓ]**.
    gt_nsm    (n·1)(b) = n·[b = 0]
    gt_npw    ((n·1)^k)(b) = n^k·[b = 0]      (written with LibRing.pw on Int)
  So B B̄ = N(α)^(ℓ−2) with N(α) = p·1 has coefficients p^(ℓ−2)·[b = 0] — the form WWbar.ww needs.
  Lean 4 core only, proofs by hand (lib_int), no axioms.
-/
import lang.WWbar

namespace NsmCoef

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing

variable (m : Nat)

theorem gt_nsm : ∀ n (b : Zp m), gt LibInt.intR m (Frobenius.nsm (gring LibInt.intR m) n (gring LibInt.intR m).one) b
    = Int.ofNat n * delta LibInt.intR b (ring m).zero
  | 0, b => by
    show gt LibInt.intR m (gzero LibInt.intR m) b = _
    unfold gzero; rw [gt_tb]
    show (0 : Int) = 0 * delta LibInt.intR b (ring m).zero
    lib_int
  | n + 1, b => by
    show gt LibInt.intR m (gadd LibInt.intR m (Frobenius.nsm (gring LibInt.intR m) n (gring LibInt.intR m).one)
      (gone LibInt.intR m)) b = _
    unfold gadd; rw [gt_tb, gt_nsm n b, WWbar.gt_one]
    show Int.ofNat n * delta LibInt.intR b (ring m).zero + delta LibInt.intR b (ring m).zero
      = Int.ofNat (n + 1) * delta LibInt.intR b (ring m).zero
    rw [show Int.ofNat (n + 1) = Int.ofNat n + 1 from rfl]
    lib_int

theorem gt_npw (c : Int)
    (x : V Int (m + 1)) (hx : ∀ b, gt LibInt.intR m x b = c * delta LibInt.intR b (ring m).zero) :
    ∀ k (b : Zp m), gt LibInt.intR m (Frobenius.pw (gring LibInt.intR m) x k) b
      = LibRing.pw LibInt.intR c k * delta LibInt.intR b (ring m).zero
  | 0, b => by
    show gt LibInt.intR m (gone LibInt.intR m) b = LibRing.pw LibInt.intR c 0 * delta LibInt.intR b (ring m).zero
    rw [WWbar.gt_one]
    show delta LibInt.intR b (ring m).zero = 1 * delta LibInt.intR b (ring m).zero
    lib_int
  | k + 1, b => by
    show gt LibInt.intR m (gmul LibInt.intR m (Frobenius.pw (gring LibInt.intR m) x k) x) b = _
    have hx' : x = ModSq.sc m c (gone LibInt.intR m) :=
      ext LibInt.intR m _ _ (fun y => by unfold ModSq.sc; rw [gt_tb, hx y, WWbar.gt_one])
    rw [hx', GroupRing.gm_comm, WWbar.sc_mul_l, gm_one, ← hx', gt_npw c x hx k b]
    show c * (LibRing.pw LibInt.intR c k * delta LibInt.intR b (ring m).zero)
      = (LibRing.pw LibInt.intR c k * c) * delta LibInt.intR b (ring m).zero
    generalize LibRing.pw LibInt.intR c k = P
    generalize delta LibInt.intR b (ring m).zero = D
    lib_int

end NsmCoef

#print axioms NsmCoef.gt_nsm
#print axioms NsmCoef.gt_npw

lang/NormHyp.lean

/-
  lang/NormHyp — T49 (O3') norm side, B: **N(α) = p·1 ⇒ (B·B̄)(b) = p^k·[b = 0]** for B the paired Stickelberger
  product (BBNorm.bb_norm, NsmCoef). This is hypothesis hB of WWbar.ww (with sB = 0).
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.BBNorm
import lang.NsmCoef

namespace NormHyp

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing

theorem norm_hyp (m : Nat) (α : V Int (m + 1)) (iv : Zp m → Zp m) (k p : Nat) (qs : List (Zp m × Nat × Nat))
    (hs : StickBB.Sums m k qs)
    (hN : NormEval.nrm m α (PairNorm.flat m (BBNorm.conv m iv qs))
      = Frobenius.nsm (gring LibInt.intR m) p (gring LibInt.intR m).one) (b : Zp m) :
    gt LibInt.intR m (gmul LibInt.intR m (AbsPair.Bp (gring LibInt.intR m) (StickBB.ents m α qs))
        (StickBB.J m (AbsPair.Bp (gring LibInt.intR m) (StickBB.ents m α qs)))) b
      = LibRing.pw LibInt.intR (Int.ofNat p) k * delta LibInt.intR b (ring m).zero := by
  have h := BBNorm.bb_norm m α iv k qs hs
  rw [hN] at h
  show gt LibInt.intR m ((gring LibInt.intR m).mul _ _) b = _
  rw [h]
  exact NsmCoef.gt_npw m (Int.ofNat p) _ (fun y => NsmCoef.gt_nsm m p y) k b

end NormHyp

#print axioms NormHyp.norm_hyp

lang/AbarGR.lean

/-
  lang/AbarGR — T49 (O3'): **A·σ_(−1)(A) ≡ p^(ℓ−2) (mod N) in the group ring ℤ[C_ℓ]** (the hypothesis hA of WWbar.ww).
  The quotient map q : ℤ[C_ℓ] → ℤ[C_ℓ]/(N) (CycRing.qmap) sends χ to ψ_u and σ_(−1)A(χ) = A(χ^(ℓ−1)) to A(ψ̄);
  CycAbar.A_norm gives q(A·σ_(−1)A) = q(p^(ℓ−2)), and equal images differ by a constant vector:
    hc_neg    −1 = (ℓ − 1)·1 in 𝔽_ℓ
    q_nsm     q(n·1) = n·1
    q_chi     q(χ(x)) = ψ_u(x)
    q_A       q(A(χ)) = A(ψ_u)
    q_JA      q(σ_(−1)A(χ)) = A(ψ_u ∘ ι)
    red_eq    q(X) = q(Y) ⇒ X(b) = Y(b) + (X(top) − Y(top))
    abar_hA   (A·σ_(−1)A)(b) = p^(ℓ−2)·δ_b + s
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.CycAbar
import lang.CycGalA
import lang.CycEis
import lang.NsmCoef
import lib.IntRing
import lib.RingTac

namespace AbarGR

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob CharSumGen

variable (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
  (k t : Nat) (hℓ : ∀ d, Dv d (k + 1 + 1) → d = 1 ∨ d = k + 1 + 1) (hN : j + 1 + 1 = (k + 1 + 1) * (t + 1))

/-- −1 = (ℓ − 1)·1 in 𝔽_ℓ. -/
theorem hc_neg : StickBB.e (k + 1) = Frobenius.nsm (ring (k + 1)) (k + 1) (ring (k + 1)).one := by
  have h : (ring (k + 1)).add (Frobenius.nsm (ring (k + 1)) (k + 1) (ring (k + 1)).one) (ring (k + 1)).one
      = (ring (k + 1)).zero := ZMod.char_p (k + 1)
  show (ring (k + 1)).neg (ring (k + 1)).one = _
  generalize Frobenius.nsm (ring (k + 1)) (k + 1) (ring (k + 1)).one = X at h ⊢
  calc (ring (k + 1)).neg (ring (k + 1)).one
      = (ring (k + 1)).add X ((ring (k + 1)).neg ((ring (k + 1)).add X (ring (k + 1)).one)) := by lib_ring
    _ = (ring (k + 1)).add X ((ring (k + 1)).neg (ring (k + 1)).zero) := by rw [h]
    _ = X := by lib_ring

/-- q(n·1) = n·1. -/
theorem q_nsm : ∀ n, CycRing.qmap LibInt.intR (k + 1) (Frobenius.nsm (gring LibInt.intR (k + 1)) n (gring LibInt.intR (k + 1)).one)
    = Frobenius.nsm (CycRing.cyc LibInt.intR (k + 1)) n (CycRing.cyc LibInt.intR (k + 1)).one
  | 0 => CycRing.qmap_zero LibInt.intR (k + 1)
  | n + 1 => by
    show CycRing.qmap LibInt.intR (k + 1) (gadd LibInt.intR (k + 1)
        (Frobenius.nsm (gring LibInt.intR (k + 1)) n (gring LibInt.intR (k + 1)).one) (gone LibInt.intR (k + 1)))
      = (CycRing.cyc LibInt.intR (k + 1)).add
        (Frobenius.nsm (CycRing.cyc LibInt.intR (k + 1)) n (CycRing.cyc LibInt.intR (k + 1)).one) (CycRing.cyc LibInt.intR (k + 1)).one
    rw [CycRing.qmap_add, q_nsm n, CycRing.qmap_one]

/-- q(χ(x)) = ψ_u(x). -/
theorem q_chi (x : Zp (j + 1 + 1)) :
    CycRing.qmap LibInt.intR (k + 1) (CycPrim.chi (k + 1) j g x)
      = CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) x := by
  unfold CycPrim.chi CycChar.psi
  by_cases hx : x = (ring (j + 1 + 1)).zero
  · rw [ite_pos' _ _ hx, ite_pos' _ _ hx]
    exact CycRing.qmap_zero LibInt.intR (k + 1)
  · rw [ite_neg' _ _ hx, ite_neg' _ _ hx]
    exact (congrArg (fun a => CycRing.qmap LibInt.intR (k + 1) (mono LibInt.intR (k + 1) a 1))
      (CycUnit.nsm_mk (k + 1) (DLogF.dlog (j + 1) g x)).symm).trans
      (CycUnit.u_pow LibInt.intR (k + 1) (DLogF.dlog (j + 1) g x)).symm

/-- q(A(χ)) = A(ψ_u). -/
theorem q_A :
    CycRing.qmap LibInt.intR (k + 1) (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k)
      = EisBridge.AA (CycRing.cyc LibInt.intR (k + 1)) j
          (CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1))) k :=
  EisBridge.aa_map (gring LibInt.intR (k + 1)) (CycRing.cyc LibInt.intR (k + 1)) (CycRing.qmap LibInt.intR (k + 1))
    (fun a b => CycRing.qmap_add LibInt.intR (k + 1) a b) (CycRing.qmap_zero LibInt.intR (k + 1))
    (fun a b => CycRing.qmap_mul LibInt.intR (k + 1) a b) (CycRing.qmap_one LibInt.intR (k + 1)) j
    (CycPrim.chi (k + 1) j g) _ (q_chi j g k) k

include hpr hg hN in
/-- q(σ_(−1)A(χ)) = A(ψ_u ∘ ι). -/
theorem q_JA :
    CycRing.qmap LibInt.intR (k + 1)
        (StickBB.J (k + 1) (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k))
      = EisBridge.AA (CycRing.cyc LibInt.intR (k + 1)) j
          (fun x => CycChar.psi (j + 1) g (CycRing.cyc LibInt.intR (k + 1)) (CycUnit.u LibInt.intR (k + 1)) (iv j x)) k := by
  show CycRing.qmap LibInt.intR (k + 1) (CycGal.gsig LibInt.intR (k + 1) (StickBB.e (k + 1))
      (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k)) = _
  rw [CycGalA.gsig_AA (k + 1) j g (StickBB.e (k + 1)) (StickBB.e (k + 1)) (StickBB.e_sq (k + 1)) k (hc_neg k) k]
  exact EisBridge.aa_map (gring LibInt.intR (k + 1)) (CycRing.cyc LibInt.intR (k + 1)) (CycRing.qmap LibInt.intR (k + 1))
    (fun a b => CycRing.qmap_add LibInt.intR (k + 1) a b) (CycRing.qmap_zero LibInt.intR (k + 1))
    (fun a b => CycRing.qmap_mul LibInt.intR (k + 1) a b) (CycRing.qmap_one LibInt.intR (k + 1)) j _ _
    (fun x => by
      show CycRing.qmap LibInt.intR (k + 1) (pw (gring LibInt.intR (k + 1)) (CycPrim.chi (k + 1) j g x) (k + 1)) = _
      rw [← CycUnit.qmap_pw, q_chi]
      exact CycEis.psi_inv (CycRing.cyc LibInt.intR (k + 1)) j g hpr hg k t hN (CycUnit.u LibInt.intR (k + 1))
        (CycUnit.u_ell LibInt.intR (k + 1)) x) k

/-- equal images under q differ by a constant vector. -/
theorem red_eq (X Y : V Int (k + 1 + 1))
    (h : CycRing.qmap LibInt.intR (k + 1) X = CycRing.qmap LibInt.intR (k + 1) Y) (b : Zp (k + 1)) :
    gt LibInt.intR (k + 1) X b = gt LibInt.intR (k + 1) Y b
      + (gt LibInt.intR (k + 1) X (CycRing.top (k + 1)) + -gt LibInt.intR (k + 1) Y (CycRing.top (k + 1))) := by
  have h1 : gt LibInt.intR (k + 1) (CycRing.red LibInt.intR (k + 1) X) b
      = gt LibInt.intR (k + 1) (CycRing.red LibInt.intR (k + 1) Y) b := by
    rw [show CycRing.red LibInt.intR (k + 1) X = CycRing.red LibInt.intR (k + 1) Y from congrArg Subtype.val h]
  rw [CycRing.gt_red, CycRing.gt_red] at h1
  have h2 : gt LibInt.intR (k + 1) X b + -gt LibInt.intR (k + 1) X (CycRing.top (k + 1))
      = gt LibInt.intR (k + 1) Y b + -gt LibInt.intR (k + 1) Y (CycRing.top (k + 1)) := h1
  generalize gt LibInt.intR (k + 1) X b = a at h2 ⊢
  generalize gt LibInt.intR (k + 1) Y b = c at h2 ⊢
  generalize gt LibInt.intR (k + 1) X (CycRing.top (k + 1)) = d at h2 ⊢
  generalize gt LibInt.intR (k + 1) Y (CycRing.top (k + 1)) = e at h2 ⊢
  calc a = (a + -d) + d := by lib_int
    _ = (c + -e) + d := by rw [h2]
    _ = c + (d + -e) := by lib_int

include hpr hg hℓ hN in
/-- **A·σ_(−1)A ≡ p^(ℓ−2) (mod N)** in ℤ[C_ℓ]: the hypothesis hA of WWbar.ww for the primary element A. -/
theorem abar_hA : ∃ sA : Int, ∀ b, gt LibInt.intR (k + 1)
      (gmul LibInt.intR (k + 1) (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k)
        (StickBB.J (k + 1) (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k))) b
      = LibRing.pw LibInt.intR (Int.ofNat (j + 1 + 1 + 1)) k * delta LibInt.intR b (ring (k + 1)).zero + sA := by
  have hq : CycRing.qmap LibInt.intR (k + 1)
      (gmul LibInt.intR (k + 1) (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k)
        (StickBB.J (k + 1) (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k)))
      = CycRing.qmap LibInt.intR (k + 1) (pw (gring LibInt.intR (k + 1))
          (Frobenius.nsm (gring LibInt.intR (k + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (k + 1)).one) k) := by
    rw [CycRing.qmap_mul, q_A, q_JA j g hpr hg k t hN, CycAbar.A_norm j g hpr hg k t hℓ hN, NthEis.pp_nsm,
      ← q_nsm k (j + 1 + 1 + 1), CycUnit.qmap_pw]
  have hY := fun b => NsmCoef.gt_npw (k + 1) (Int.ofNat (j + 1 + 1 + 1)) _
    (fun y => NsmCoef.gt_nsm (k + 1) (j + 1 + 1 + 1) y) k b
  generalize Frobenius.pw (gring LibInt.intR (k + 1))
    (Frobenius.nsm (gring LibInt.intR (k + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (k + 1)).one) k = Y at hq hY
  generalize gmul LibInt.intR (k + 1) (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k)
    (StickBB.J (k + 1) (EisBridge.AA (gring LibInt.intR (k + 1)) j (CycPrim.chi (k + 1) j g) k)) = X at hq ⊢
  exact ⟨gt LibInt.intR (k + 1) X (CycRing.top (k + 1)) + -gt LibInt.intR (k + 1) Y (CycRing.top (k + 1)),
    fun b => by rw [red_eq k X Y hq b, hY b]⟩

end AbarGR

#print axioms AbarGR.hc_neg
#print axioms AbarGR.q_nsm
#print axioms AbarGR.q_chi
#print axioms AbarGR.q_A
#print axioms AbarGR.q_JA
#print axioms AbarGR.red_eq
#print axioms AbarGR.abar_hA

lang/ExactOneK.lean

/-
  lang/ExactOneK — T49 (O3') (v3): **a product equal to P has exactly one factor divisible by P** when every
  1 − P·t is a unit (instead of P² = 0 in lang/ExactOne), so the count also holds in ℤ/p^n, n ≥ 2:
    exact_one_k    pr xs = P, P ≠ 0, P not a unit, (1 − P t) a unit for all t ⇒ cnt xs = 1
    others_unit    pr (x :: xs) = P with x flagged and cnt = 1 ⇒ pr xs is a unit and x = P·U, U a unit
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ExactOne

namespace ExactOneK

open FTAChain YMChain FiniteRecChain ExactOne

variable {R : Type} (K : DetChain.CRing R) (P : R) (d : R → Bool) (hP : ∀ x, d x = true → ∃ y, x = K.mul P y)
  (hU : ∀ x, d x = false → ∃ w, K.mul x w = K.one)

include hP hU in
/-- **exactly one factor is divisible by P** when the product is P and every 1 − P t is a unit. -/
theorem exact_one_k (hT : ∀ t, ∃ v, K.mul (K.add K.one (K.neg (K.mul P t))) v = K.one) (hP0 : P ≠ K.zero)
    (hPu : ∀ w, K.mul P w ≠ K.one) (xs : List R) (hx : pr K xs = P) : cnt d xs = 1 := by
  obtain ⟨w, hw⟩ := pr_form K P d hP xs
  match hc : cnt d xs with
  | 0 =>
    obtain ⟨u, hu⟩ := pr_unit K d hU xs hc
    rw [hx] at hu
    exact absurd hu (hPu u)
  | 1 => rfl
  | c + 2 =>
    rw [hc, hx] at hw
    have hpw : Frobenius.pw K P (c + 2) = K.mul (K.mul (Frobenius.pw K P c) P) P := rfl
    rw [hpw] at hw
    obtain ⟨v, hv⟩ := hT (K.mul (Frobenius.pw K P c) w)
    refine (hP0 ?_).elim
    generalize Frobenius.pw K P c = Q at hw hv
    have h1 : K.mul P (K.add K.one (K.neg (K.mul P (K.mul Q w)))) = K.zero := by
      calc K.mul P (K.add K.one (K.neg (K.mul P (K.mul Q w))))
          = K.add P (K.neg (K.mul (K.mul (K.mul Q P) P) w)) := by lib_ring
        _ = K.add P (K.neg P) := by rw [← hw]
        _ = K.zero := by lib_ring
    calc P = K.mul P K.one := by lib_ring
      _ = K.mul P (K.mul (K.add K.one (K.neg (K.mul P (K.mul Q w)))) v) := by rw [hv]
      _ = K.mul (K.mul P (K.add K.one (K.neg (K.mul P (K.mul Q w))))) v := by lib_ring
      _ = K.mul K.zero v := by rw [h1]
      _ = K.zero := by lib_ring

include hU in
/-- **the flagged factor is P times a unit** when it is the only flagged one. -/
theorem others_unit (x : R) (xs : List R) (hx : pr K (x :: xs) = P) (hd : d x = true)
    (hc : cnt d (x :: xs) = 1) : ∃ U w, x = K.mul P U ∧ K.mul U w = K.one := by
  have h0 : cnt d xs = 0 := by
    have h' : (if d x = true then 1 else 0) + cnt d xs = 1 := hc
    rw [hd, ite_pos' _ _ rfl] at h'
    exact Nat.succ.inj ((show cnt d xs + 1 = 1 + cnt d xs by lib_nat).trans h')
  obtain ⟨w, hw⟩ := pr_unit K d hU xs h0
  have hx' : K.mul x (pr K xs) = P := hx
  refine ⟨w, pr K xs, ?_, ?_⟩
  · calc x = K.mul (K.mul x (pr K xs)) w := by rw [show K.mul (K.mul x (pr K xs)) w = K.mul x (K.mul (pr K xs) w) by lib_ring, hw]; lib_ring
      _ = K.mul P w := by rw [hx']
  · rw [show K.mul w (pr K xs) = K.mul (pr K xs) w by lib_ring]
    exact hw

end ExactOneK

#print axioms ExactOneK.exact_one_k
#print axioms ExactOneK.others_unit

lang/FlagPos.lean

/-
  lang/FlagPos — T49 (O3') (v3): **two different indices cannot both be flagged when exactly one factor is**.
  For a list of indices Fs and factors Y f (f ∈ Fs), with ExactOne.cnt counting the flagged factors:
    one_flag    f ∈ Fs, Y f flagged ⇒ 1 ≤ cnt (Fs.map Y)
    two_flag    f ≠ g in Fs, both flagged ⇒ 2 ≤ cnt (Fs.map Y)
    unflag      cnt (Fs.map Y) = 1, Y f flagged, g ≠ f in Fs ⇒ Y g not flagged
  Use: Y c = φ_z(σ_c α); α ∈ P_1 flags c = 1, so every other conjugate is a unit.
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.ExactOne

namespace FlagPos

open FTAChain YMChain FiniteRecChain ExactOne

variable {R A : Type} (d : R → Bool) (Y : A → R)

theorem one_flag (f : A) (hf : d (Y f) = true) : ∀ Fs : List A, f ∈ Fs → 1 ≤ cnt d (Fs.map Y)
  | [], h => nomatch h
  | _ :: t, h => by
    cases h with
    | head =>
      show 1 ≤ (if d (Y f) = true then 1 else 0) + cnt d (t.map Y)
      rw [hf, ite_pos' _ _ rfl]
      exact Nat.le_add_right 1 _
    | tail _ h' => exact Nat.le_trans (one_flag f hf t h') (Nat.le_add_left _ _)

theorem two_flag (f g : A) (hfg : f ≠ g) (hf : d (Y f) = true) (hg : d (Y g) = true) :
    ∀ Fs : List A, f ∈ Fs → g ∈ Fs → 2 ≤ cnt d (Fs.map Y)
  | [], h, _ => nomatch h
  | _ :: t, hf', hg' => by
    cases hf' with
    | head =>
      cases hg' with
      | head => exact absurd rfl hfg
      | tail _ hgt =>
        show 2 ≤ (if d (Y f) = true then 1 else 0) + cnt d (t.map Y)
        rw [hf, ite_pos' _ _ rfl]
        exact Nat.add_le_add_left (one_flag d Y g hg t hgt) 1
    | tail _ hft =>
      cases hg' with
      | head =>
        show 2 ≤ (if d (Y g) = true then 1 else 0) + cnt d (t.map Y)
        rw [hg, ite_pos' _ _ rfl]
        exact Nat.add_le_add_left (one_flag d Y f hf t hft) 1
      | tail _ hgt => exact Nat.le_trans (two_flag f g hfg hf hg t hft hgt) (Nat.le_add_left _ _)

/-- **only one flagged index**: the others are unflagged. -/
theorem unflag (Fs : List A) (hc : cnt d (Fs.map Y) = 1) (f g : A) (hfm : f ∈ Fs) (hgm : g ∈ Fs) (hfg : f ≠ g)
    (hf : d (Y f) = true) : d (Y g) = false := by
  cases hgd : d (Y g) with
  | false => rfl
  | true =>
    have h2 := two_flag d Y f g hfg hf hgd Fs hfm hgm
    rw [hc] at h2
    exact absurd h2 (Nat.not_succ_le_self 1)

end FlagPos

#print axioms FlagPos.one_flag
#print axioms FlagPos.two_flag
#print axioms FlagPos.unflag

lang/NormPk.lean

/-
  lang/NormPk — T49 (O3') (v3): **exactly one factor divisible by p in ℤ/p^n** (M + 1 = p·k', (p·1)^n = 0, p < M + 1).
    red_P_k        red(p·1) = 0 in 𝔽_p
    red_neg_k      red(−v) = −red(v)
    P_nunit_k      p·1 is not a unit in ℤ/M
    one_sub_unit   1 − p·t is a unit in ℤ/M                      (UnitPk.unit_pk)
    exact_one_pk   Π xs = p·1 ⇒ exactly one x ∈ xs has red(x) = 0  (ExactOneK)
    flagged_unit   for Π (x :: xs) = p·1 with red(x) = 0: x = p·U, U a unit
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ExactOneK
import lang.UnitPk

namespace NormPk

open FTAChain ZMod YMChain FiniteRecChain Frobenius

variable (J M k' n : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
  (hk : M + 1 = (J + 1 + 1) * k') (hPn : pw (ring M) (ExactP2.P J M) n = (ring M).zero)

include hk in
theorem red_P_k : RedP2.red J M (ExactP2.P J M) = (ring (J + 1)).zero := Subtype.ext (by
  show md (J + 1) (nsm (ring M) (J + 1 + 1) (ring M).one).val = md (J + 1) 0
  rw [nsm_one_val, RedP2.mdmd J M k' hk (J + 1 + 1), md_self]
  rfl)

include hk in
theorem red_neg_k (v : Zp M) : RedP2.red J M ((ring M).neg v) = (ring (J + 1)).neg (RedP2.red J M v) := by
  have h0 := RedP2.red_add J M k' hk v ((ring M).neg v)
  rw [show (ring M).add v ((ring M).neg v) = (ring M).zero by lib_ring, RedP2.red_zero J M k' hk] at h0
  calc RedP2.red J M ((ring M).neg v)
      = (ring (J + 1)).add (ring (J + 1)).zero ((ring (J + 1)).neg (RedP2.red J M v)) := by rw [h0]; lib_ring
    _ = _ := by lib_ring

include hk in
theorem P_nunit_k (w : Zp M) : (ring M).mul (ExactP2.P J M) w ≠ (ring M).one := fun h => by
  have h1 := congrArg (RedP2.red J M) h
  rw [RedP2.red_mul J M k' hk, red_P_k J M k' hk, RedP2.red_one J M k' hk] at h1
  exact PowerSum.one_ne_zero' J (h1.symm.trans
    (show (ring (J + 1)).mul (ring (J + 1)).zero (RedP2.red J M w) = (ring (J + 1)).zero by lib_ring))

include hpr hk hPn in
theorem one_sub_unit (t : Zp M) :
    ∃ v, (ring M).mul ((ring M).add (ring M).one ((ring M).neg ((ring M).mul (ExactP2.P J M) t))) v = (ring M).one := by
  apply UnitPk.unit_pk J M k' n hpr hk hPn
  rw [RedP2.red_add J M k' hk, red_neg_k J M k' hk, RedP2.red_mul J M k' hk, red_P_k J M k' hk, RedP2.red_one J M k' hk]
  intro h
  exact PowerSum.one_ne_zero' J ((show (ring (J + 1)).one = (ring (J + 1)).add (ring (J + 1)).one
    ((ring (J + 1)).neg ((ring (J + 1)).mul (ring (J + 1)).zero (RedP2.red J M t))) by lib_ring).trans h)

include hpr hk hPn in
/-- **exactly one factor is divisible by p** when the product in ℤ/p^n is p. -/
theorem exact_one_pk (hlt : J + 1 + 1 < M + 1) (xs : List (Zp M)) (hx : ExactOne.pr (ring M) xs = ExactP2.P J M) :
    ExactOne.cnt (ExactP2.d J M) xs = 1 :=
  ExactOneK.exact_one_k (ring M) (ExactP2.P J M) (ExactP2.d J M)
    (fun x h => RedP2.red_ker J M x (of_decide_eq_true h))
    (fun x h => UnitPk.unit_pk J M k' n hpr hk hPn x (of_decide_eq_false h))
    (one_sub_unit J M k' n hpr hk hPn) (StickVal.small_ne_zero M (J + 1) hlt) (P_nunit_k J M k' hk) xs hx

include hpr hk hPn in
/-- **the factor divisible by p is p times a unit** when the product is p. -/
theorem flagged_unit (hlt : J + 1 + 1 < M + 1) (x : Zp M) (xs : List (Zp M))
    (hx : ExactOne.pr (ring M) (x :: xs) = ExactP2.P J M) (h0 : RedP2.red J M x = (ring (J + 1)).zero) :
    ∃ U w, x = (ring M).mul (ExactP2.P J M) U ∧ (ring M).mul U w = (ring M).one :=
  ExactOneK.others_unit (ring M) (ExactP2.P J M) (ExactP2.d J M)
    (fun x h => UnitPk.unit_pk J M k' n hpr hk hPn x (of_decide_eq_false h)) x xs hx (decide_eq_true h0)
    (exact_one_pk J M k' n hpr hk hPn hlt (x :: xs) hx)

end NormPk

#print axioms NormPk.red_P_k
#print axioms NormPk.red_neg_k
#print axioms NormPk.P_nunit_k
#print axioms NormPk.one_sub_unit
#print axioms NormPk.exact_one_pk
#print axioms NormPk.flagged_unit

lang/ConjUnit.lean

/-
  lang/ConjUnit — T49 (O3') (v3): **the conjugates of a norm-p element α ∈ P_1 at z in ℤ/p^n**.
  Y f = φ_z(σ_f α); ps a list of pairs whose second entries Fs run over the conjugations of the norm,
  N(α) = Π_(p ∈ ps) σ_(p.2) α = p·1, and α ∈ P_1 (red Y 1 = 0). Then
    phz_nrm     φ_z(Π σ_f α) = Π Y f
    norm_pr     Π_(f ∈ Fs) Y f = p·1
    conj_unit   Y f0 flagged ⇒ Y g is a unit for every g ∈ Fs, g ≠ f0   (NormPk.exact_one_pk + FlagPos.unflag + UnitPk)
    conj_P      Y 1 = p·U with U a unit (1 the first entry of Fs)  (NormPk.flagged_unit)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.NormPk
import lang.FlagPos
import lang.NormEval

namespace ConjUnit

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

variable (J M k' n : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
  (hk : M + 1 = (J + 1 + 1) * k') (hPn : pw (ring M) (ExactP2.P J M) n = (ring M).zero) (hlt : J + 1 + 1 < M + 1)
  (m : Nat) (z : Zp M) (hz : pw (ring M) z (m + 1) = (ring M).one) (α : V Int (m + 1))

/-- the conjugate σ_f α evaluated at z. -/
def Y (f : Zp m) : Zp M := CycEis.phz (ring M) m z (CycGal.gsig LibInt.intR m f α)

/-- the conjugation indices of a norm list. -/
def Fs (ps : List (Zp m × Zp m)) : List (Zp m) := ps.map (fun p => p.2)

include hz in
theorem phz_nrm : ∀ ps : List (Zp m × Zp m),
    CycEis.phz (ring M) m z (NormEval.nrm m α ps) = ExactOne.pr (ring M) ((Fs m ps).map (Y M m z α))
  | [] => CycEis.phz_one (ring M) m z
  | p :: ps => by
    show CycEis.phz (ring M) m z ((gring LibInt.intR m).mul (CycGal.gsig LibInt.intR m p.2 α) (NormEval.nrm m α ps))
      = (ring M).mul (Y M m z α p.2) (ExactOne.pr (ring M) ((Fs m ps).map (Y M m z α)))
    rw [CycEis.phz_mul (ring M) m z hz, phz_nrm ps]
    rfl

include hz in
theorem norm_pr (ps : List (Zp m × Zp m))
    (hN : NormEval.nrm m α ps = nsm (gring LibInt.intR m) (J + 1 + 1) (gring LibInt.intR m).one) :
    ExactOne.pr (ring M) ((Fs m ps).map (Y M m z α)) = ExactP2.P J M := by
  rw [← phz_nrm M m z hz α ps, hN, NormEval.phz_nsm]
  rfl

include hpr hk hPn hlt hz in
/-- **every other conjugate is a unit**: f0 flagged (red Y f0 = 0) ⇒ Y g is a unit for g ≠ f0 in Fs. -/
theorem conj_unit (ps : List (Zp m × Zp m))
    (hN : NormEval.nrm m α ps = nsm (gring LibInt.intR m) (J + 1 + 1) (gring LibInt.intR m).one)
    (f0 : Zp m) (h0 : RedP2.red J M (Y M m z α f0) = (ring (J + 1)).zero) (h1m : f0 ∈ Fs m ps)
    (g : Zp m) (hgm : g ∈ Fs m ps) (hg1 : f0 ≠ g) : ∃ w, (ring M).mul (Y M m z α g) w = (ring M).one :=
  UnitPk.unit_pk J M k' n hpr hk hPn (Y M m z α g) (of_decide_eq_false
    (FlagPos.unflag (ExactP2.d J M) (Y M m z α) (Fs m ps)
      (NormPk.exact_one_pk J M k' n hpr hk hPn hlt _ (norm_pr J M m z hz α ps hN))
      f0 g h1m hgm hg1 (decide_eq_true h0)))

include hpr hk hPn hlt hz in
/-- **the conjugate at 1 is p times a unit**. -/
theorem conj_P (p0 : Zp m × Zp m) (ps : List (Zp m × Zp m)) (h1 : p0.2 = (ring m).one)
    (hN : NormEval.nrm m α (p0 :: ps) = nsm (gring LibInt.intR m) (J + 1 + 1) (gring LibInt.intR m).one)
    (h0 : RedP2.red J M (Y M m z α (ring m).one) = (ring (J + 1)).zero) :
    ∃ U w, Y M m z α (ring m).one = (ring M).mul (ExactP2.P J M) U ∧ (ring M).mul U w = (ring M).one := by
  have hx := norm_pr J M m z hz α (p0 :: ps) hN
  have hx' : ExactOne.pr (ring M) (Y M m z α (ring m).one :: (Fs m ps).map (Y M m z α)) = ExactP2.P J M := by
    rw [← h1]; exact hx
  exact NormPk.flagged_unit J M k' n hpr hk hPn hlt _ _ hx' h0

end ConjUnit

#print axioms ConjUnit.phz_nrm
#print axioms ConjUnit.norm_pr
#print axioms ConjUnit.conj_unit
#print axioms ConjUnit.conj_P

lang/BbarVal.lean

/-
  lang/BbarVal — T49 (O3') (v3): **φ_(z^e)(σ_(−1)B) = P^F·U**, U a unit, F the sum of the exponents of the factors
  conjugate to α by e. B = Π_q (σ_(q₁) α)^(a_q) (σ_(−q₁) α)^(b_q) (StickBB.ents), N(α) = p·1, α ∈ P_1, z^ℓ = 1 in ℤ/p^n:
    hz_e        (z^e)^ℓ = 1
    y_bridge    φ_(z^e)(σ_e α) = φ_z(α)                       (CycGalEv.phz_gsig, pw_mul_val)
    J_Bp        σ_(−1)B = Π_q (σ_(−q₁) α)^(a_q) (σ_(q₁) α)^(b_q)
    ev_prd      φ_(z^e)(σ_(−1)B) = Π of the entries ees (factor, exponent, flag "index = e")
    idx_mem     every index of ees lies in the conjugation list of the norm
    ok_ees      flagged entries are p·unit (ConjUnit.conj_P + y_bridge), the others units (ConjUnit.conj_unit at z^e)
    bbar_val    φ_(z^e)(σ_(−1)B) = P^(fexp ees)·U, U a unit  (ProdVal.pv)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.ConjUnit
import lang.ProdVal
import lang.PhzPw
import lang.BBNorm
import lang.CycGalEv

namespace BbarVal

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

variable (J M k' n : Nat) (hpr : ∀ d, Dv d (J + 1 + 1) → d = 1 ∨ d = J + 1 + 1)
  (hk : M + 1 = (J + 1 + 1) * k') (hPn : pw (ring M) (ExactP2.P J M) n = (ring M).zero) (hlt : J + 1 + 1 < M + 1)
  (m : Nat) (z : Zp M) (hz : pw (ring M) z (m + 1) = (ring M).one) (α : V Int (m + 1))
  (e0 c0 : Zp m) (hce : (ring m).mul c0 e0 = (ring m).one)

include hz in
theorem hz_e : pw (ring M) (pw (ring M) z e0.val) (m + 1) = (ring M).one := by
  rw [SymProdZ.pw_pw]
  exact SymProdW.pw_mulE (ring M) z (m + 1) hz e0.val

include hz hce in
/-- **φ_(z^e)(σ_e α) = φ_z(σ_1 α)**. -/
theorem y_bridge : ConjUnit.Y M m (pw (ring M) z e0.val) α e0 = ConjUnit.Y M m z α (ring m).one := by
  show CycEis.phz (ring M) m (pw (ring M) z e0.val) (CycGal.gsig LibInt.intR m e0 α)
    = CycEis.phz (ring M) m z (CycGal.gsig LibInt.intR m (ring m).one α)
  rw [CycGalEv.phz_gsig (ring M) m _ (hz_e M m z hz e0) c0 e0 hce α, ← CycGalEv.pw_mul_val (ring M) m z hz c0 e0, hce,
    CycGalEv.phz_gsig (ring M) m z hz (ring m).one (ring m).one (by lib_ring) α]

/-- the entries of σ_(−1)B. -/
def jents : List (Zp m × Nat × Nat) → List (V Int (m + 1) × V Int (m + 1) × Nat × Nat)
  | [] => []
  | q :: qs => (CycGal.gsig LibInt.intR m ((ring m).mul (StickBB.e m) q.1) α, CycGal.gsig LibInt.intR m q.1 α, q.2.1, q.2.2)
      :: jents qs

theorem J_mul (a b : V Int (m + 1)) :
    StickBB.J m ((gring LibInt.intR m).mul a b) = (gring LibInt.intR m).mul (StickBB.J m a) (StickBB.J m b) :=
  CycGal.gsig_mul LibInt.intR m (StickBB.e m) (StickBB.e m) (StickBB.e_sq m) a b

theorem J_one : StickBB.J m (gring LibInt.intR m).one = (gring LibInt.intR m).one :=
  CycGal.gsig_one LibInt.intR m (StickBB.e m) (StickBB.e m) (StickBB.e_sq m)

/-- **σ_(−1)B = Π (σ_(−q₁) α)^(a_q) (σ_(q₁) α)^(b_q)**. -/
theorem J_Bp : ∀ qs : List (Zp m × Nat × Nat),
    StickBB.J m (AbsPair.Bp (gring LibInt.intR m) (StickBB.ents m α qs)) = AbsPair.Bp (gring LibInt.intR m) (jents m α qs)
  | [] => J_one m
  | q :: qs => by
    show StickBB.J m ((gring LibInt.intR m).mul ((gring LibInt.intR m).mul
        (pw (gring LibInt.intR m) (CycGal.gsig LibInt.intR m q.1 α) q.2.1)
        (pw (gring LibInt.intR m) (CycGal.gsig LibInt.intR m ((ring m).mul (StickBB.e m) q.1) α) q.2.2))
        (AbsPair.Bp (gring LibInt.intR m) (StickBB.ents m α qs)))
      = (gring LibInt.intR m).mul ((gring LibInt.intR m).mul
        (pw (gring LibInt.intR m) (CycGal.gsig LibInt.intR m ((ring m).mul (StickBB.e m) q.1) α) q.2.1)
        (pw (gring LibInt.intR m) (CycGal.gsig LibInt.intR m q.1 α) q.2.2))
        (AbsPair.Bp (gring LibInt.intR m) (jents m α qs))
    rw [J_mul, J_mul, AbsPair.J_pw (gring LibInt.intR m) (StickBB.J m) (J_mul m) (J_one m),
      AbsPair.J_pw (gring LibInt.intR m) (StickBB.J m) (J_mul m) (J_one m), J_Bp qs]
    have h1 : StickBB.J m (CycGal.gsig LibInt.intR m q.1 α) = CycGal.gsig LibInt.intR m ((ring m).mul (StickBB.e m) q.1) α :=
      StickBB.gsig_comp m q.1 (StickBB.e m) α
    have h2 : StickBB.J m (CycGal.gsig LibInt.intR m ((ring m).mul (StickBB.e m) q.1) α) = CycGal.gsig LibInt.intR m q.1 α := by
      show CycGal.gsig LibInt.intR m (StickBB.e m) (CycGal.gsig LibInt.intR m ((ring m).mul (StickBB.e m) q.1) α) = _
      rw [StickBB.gsig_comp, show (ring m).mul (StickBB.e m) ((ring m).mul (StickBB.e m) q.1) = q.1 by
        show (ring m).mul ((ring m).neg (ring m).one) ((ring m).mul ((ring m).neg (ring m).one) q.1) = q.1
        lib_ring]
    rw [h1, h2]

/-- the factor list of φ_(z')(σ_(−1)B) for ProdVal: (value, exponent, flag "index = e0"). -/
def ees (z' : Zp M) : List (Zp m × Nat × Nat) → List (Zp M × Nat × Bool)
  | [] => []
  | q :: qs => (ConjUnit.Y M m z' α ((ring m).mul (StickBB.e m) q.1), q.2.1, decide ((ring m).mul (StickBB.e m) q.1 = e0))
      :: (ConjUnit.Y M m z' α q.1, q.2.2, decide (q.1 = e0)) :: ees z' qs

theorem ev_prd (z' : Zp M) : ∀ qs : List (Zp m × Nat × Nat),
    PhzPw.evBp (ring M) m z' (jents m α qs) = ProdVal.prd (ring M) (ees M m α e0 z' qs)
  | [] => rfl
  | q :: qs => by
    show (ring M).mul ((ring M).mul
        (pw (ring M) (CycEis.phz (ring M) m z' (CycGal.gsig LibInt.intR m ((ring m).mul (StickBB.e m) q.1) α)) q.2.1)
        (pw (ring M) (CycEis.phz (ring M) m z' (CycGal.gsig LibInt.intR m q.1 α)) q.2.2))
        (PhzPw.evBp (ring M) m z' (jents m α qs))
      = (ring M).mul (pw (ring M) (CycEis.phz (ring M) m z' (CycGal.gsig LibInt.intR m ((ring m).mul (StickBB.e m) q.1) α)) q.2.1)
        ((ring M).mul (pw (ring M) (CycEis.phz (ring M) m z' (CycGal.gsig LibInt.intR m q.1 α)) q.2.2)
          (ProdVal.prd (ring M) (ees M m α e0 z' qs)))
    rw [ev_prd z' qs]
    exact (ring M).mul_assoc _ _ _

/-- the conjugation list of the norm attached to qs. -/
def nlist (iv : Zp m → Zp m) (qs : List (Zp m × Nat × Nat)) : List (Zp m × Zp m) := PairNorm.flat m (BBNorm.conv m iv qs)

theorem idx_mem (iv : Zp m → Zp m) : ∀ qs : List (Zp m × Nat × Nat), ∀ q, q ∈ qs →
    q.1 ∈ ConjUnit.Fs m (nlist m iv qs) ∧ (ring m).mul (StickBB.e m) q.1 ∈ ConjUnit.Fs m (nlist m iv qs)
  | [], _, h => nomatch h
  | r :: qs, q, h => by
    cases h with
    | head => exact ⟨List.Mem.head _, List.Mem.tail _ (List.Mem.head _)⟩
    | tail _ h' =>
      obtain ⟨h1, h2⟩ := idx_mem iv qs q h'
      exact ⟨List.Mem.tail _ (List.Mem.tail _ h1), List.Mem.tail _ (List.Mem.tail _ h2)⟩

include hpr hk hPn hlt hz hce in
/-- **flags of the factors**: index e0 ⇒ p·unit, other indices ⇒ unit. -/
theorem ok_ees (iv : Zp m → Zp m) (q0 : Zp m × Nat × Nat) (qs : List (Zp m × Nat × Nat)) (h1 : q0.1 = (ring m).one)
    (hN : NormEval.nrm m α (nlist m iv (q0 :: qs)) = nsm (gring LibInt.intR m) (J + 1 + 1) (gring LibInt.intR m).one)
    (h0 : RedP2.red J M (ConjUnit.Y M m z α (ring m).one) = (ring (J + 1)).zero)
    (he : e0 ∈ ConjUnit.Fs m (nlist m iv (q0 :: qs))) :
    ∀ qt : List (Zp m × Nat × Nat), (∀ q, q ∈ qt → q ∈ q0 :: qs) →
      ProdVal.Ok (ring M) (ExactP2.P J M) (ees M m α e0 (pw (ring M) z e0.val) qt) := by
  have hz' := hz_e M m z hz e0
  have h0' : RedP2.red J M (ConjUnit.Y M m (pw (ring M) z e0.val) α e0) = (ring (J + 1)).zero := by
    rw [y_bridge M m z hz α e0 c0 hce]; exact h0
  obtain ⟨U, w, hU, hw⟩ := ConjUnit.conj_P J M k' n hpr hk hPn hlt m z hz α
    (iv q0.1, q0.1) ((iv ((ring m).mul (StickBB.e m) q0.1), (ring m).mul (StickBB.e m) q0.1) :: PairNorm.flat m (BBNorm.conv m iv qs))
    h1 hN h0
  have entry : ∀ f, f ∈ ConjUnit.Fs m (nlist m iv (q0 :: qs)) →
      ((decide (f = e0) = true → ∃ U w, ConjUnit.Y M m (pw (ring M) z e0.val) α f = (ring M).mul (ExactP2.P J M) U ∧
          (ring M).mul U w = (ring M).one) ∧
        (decide (f = e0) = false → ∃ w, (ring M).mul (ConjUnit.Y M m (pw (ring M) z e0.val) α f) w = (ring M).one)) :=
    fun f hf => ⟨fun hd => by
        have hfe : f = e0 := of_decide_eq_true hd
        rw [hfe, y_bridge M m z hz α e0 c0 hce]
        exact ⟨U, w, hU, hw⟩,
      fun hd => ConjUnit.conj_unit J M k' n hpr hk hPn hlt m _ hz' α _ hN e0 h0' he f hf
        (fun h => (of_decide_eq_false hd) h.symm)⟩
  intro qt
  induction qt with
  | nil => intro _; exact True.intro
  | cons q qt ih =>
    intro hsub
    obtain ⟨m1, m2⟩ := idx_mem m iv (q0 :: qs) q (hsub q (List.Mem.head _))
    exact And.intro (entry _ m2).1 (And.intro (entry _ m2).2 (And.intro (entry _ m1).1 (And.intro (entry _ m1).2
      (ih (fun r hr => hsub r (List.Mem.tail _ hr))))))

include hpr hk hPn hlt hz hce in
/-- **v_(P_e)(σ_(−1)B) = fexp**: φ_(z^e)(σ_(−1)B) = P^F·U, U a unit. -/
theorem bbar_val (iv : Zp m → Zp m) (q0 : Zp m × Nat × Nat) (qs : List (Zp m × Nat × Nat)) (h1 : q0.1 = (ring m).one)
    (hN : NormEval.nrm m α (nlist m iv (q0 :: qs)) = nsm (gring LibInt.intR m) (J + 1 + 1) (gring LibInt.intR m).one)
    (h0 : RedP2.red J M (ConjUnit.Y M m z α (ring m).one) = (ring (J + 1)).zero)
    (he : e0 ∈ ConjUnit.Fs m (nlist m iv (q0 :: qs))) :
    ∃ U w, CycEis.phz (ring M) m (pw (ring M) z e0.val)
        (StickBB.J m (AbsPair.Bp (gring LibInt.intR m) (StickBB.ents m α (q0 :: qs))))
      = (ring M).mul (pw (ring M) (ExactP2.P J M) (ProdVal.fexp (ees M m α e0 (pw (ring M) z e0.val) (q0 :: qs)))) U ∧
      (ring M).mul U w = (ring M).one := by
  rw [J_Bp, PhzPw.phz_Bp (ring M) m _ (hz_e M m z hz e0), ev_prd]
  exact ProdVal.pv (ring M) (ExactP2.P J M) _
    (ok_ees J M k' n hpr hk hPn hlt m z hz α e0 c0 hce iv q0 qs h1 hN h0 he (q0 :: qs) (fun _ h => h))

end BbarVal

#print axioms BbarVal.hz_e
#print axioms BbarVal.y_bridge
#print axioms BbarVal.J_mul
#print axioms BbarVal.J_one
#print axioms BbarVal.J_Bp
#print axioms BbarVal.ev_prd
#print axioms BbarVal.idx_mem
#print axioms BbarVal.ok_ees
#print axioms BbarVal.bbar_val

lang/StickQs.lean

/-
  lang/StickQs — T49 (O3') (v3), concrete exponents: **qs = [(d, ℓ − 1 − d, d − 1) : d = 1..h]**, ℓ = kk + 2 = 2h + 1,
  and the valuation of σ_(−1)B at every P_e is at least e − 1:
    qsL         the list, built with two counters (no subtraction)
    mem_qsL     (d, j_d, d − 1) ∈ qsL with j_d + (d − 1) constant
    sums_qsL    every entry has a_d + b_d = ℓ − 2                       (StickBB.Sums)
    le_fexp     a flagged entry's exponent is at most fexp
    mem_ees     both factors of an entry occur in BbarVal.ees
    neg_mk      (−1)·d = e in 𝔽_ℓ when d + e = ℓ
    val_lower   e ∈ Fs(norm list) and fexp(ees) = (e − 1) + r     (d = e if e ≤ h, d = ℓ − e otherwise)
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.BbarVal
import lib.Semi

namespace StickQs

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

variable (kk : Nat)

/-- entries (d, j, d − 1) for d = i + 1, …, i + c, with j decreasing. -/
def qsL : Nat → Nat → Nat → List (Zp (kk + 1) × Nat × Nat)
  | _, _, 0 => []
  | i, j, c + 1 => (mk (kk + 1) (i + 1), j, i) :: qsL (i + 1) (Nat.pred j) c

theorem mem_qsL : ∀ c i j t, c ≤ j → t < c →
    ∃ jj, (mk (kk + 1) (i + 1 + t), jj, i + t) ∈ qsL kk i j c ∧ jj + (i + t) = j + i
  | 0, _, _, _, _, ht => absurd ht (Nat.not_lt_zero _)
  | c + 1, i, j, 0, _, _ => ⟨j, List.Mem.head _, rfl⟩
  | c + 1, i, 0, t + 1, hc, _ => absurd hc (Nat.not_succ_le_zero c)
  | c + 1, i, j' + 1, t + 1, hc, ht => by
    obtain ⟨jj, hm, he⟩ := mem_qsL c (i + 1) j' t (Nat.le_of_succ_le_succ hc) (Nat.lt_of_succ_lt_succ ht)
    refine ⟨jj, ?_, ?_⟩
    · have e1 : i + 1 + (t + 1) = i + 1 + 1 + t := by lib_nat
      have e2 : i + (t + 1) = i + 1 + t := by lib_nat
      rw [e1, e2]
      exact List.Mem.tail _ hm
    · have e2 : i + (t + 1) = i + 1 + t := by lib_nat
      rw [e2, he]
      lib_nat

theorem sums_qsL : ∀ c i j, c ≤ j → i + j = kk → StickBB.Sums (kk + 1) kk (qsL kk i j c)
  | 0, _, _, _, _ => True.intro
  | c + 1, _, 0, hc, _ => absurd hc (Nat.not_succ_le_zero c)
  | c + 1, i, j' + 1, hc, hs =>
    And.intro ((Nat.add_comm (j' + 1) i).trans hs)
      (sums_qsL c (i + 1) j' (Nat.le_of_succ_le_succ hc) ((show i + 1 + j' = i + (j' + 1) by lib_nat).trans hs))

theorem le_fexp {R : Type} (x : R) (n : Nat) : ∀ es : List (R × Nat × Bool), (x, n, true) ∈ es → n ≤ ProdVal.fexp es
  | [], h => nomatch h
  | _ :: es, h => by
    cases h with
    | head =>
      show n ≤ (if true = true then n else 0) + ProdVal.fexp es
      rw [ite_pos' _ _ rfl]
      exact Nat.le_add_right n _
    | tail _ h' => exact Nat.le_trans (le_fexp x n es h') (Nat.le_add_left _ _)

theorem mem_ees (M : Nat) (α : V Int (kk + 1 + 1)) (e0 : Zp (kk + 1)) (z' : Zp M) :
    ∀ qs : List (Zp (kk + 1) × Nat × Nat), ∀ q, q ∈ qs →
      (ConjUnit.Y M (kk + 1) z' α ((ring (kk + 1)).mul (StickBB.e (kk + 1)) q.1), q.2.1,
          decide ((ring (kk + 1)).mul (StickBB.e (kk + 1)) q.1 = e0)) ∈ BbarVal.ees M (kk + 1) α e0 z' qs ∧
        (ConjUnit.Y M (kk + 1) z' α q.1, q.2.2, decide (q.1 = e0)) ∈ BbarVal.ees M (kk + 1) α e0 z' qs
  | [], _, h => nomatch h
  | _ :: qs, q, h => by
    cases h with
    | head => exact ⟨List.Mem.head _, List.Mem.tail _ (List.Mem.head _)⟩
    | tail _ h' =>
      obtain ⟨h1, h2⟩ := mem_ees M α e0 z' qs q h'
      exact ⟨List.Mem.tail _ (List.Mem.tail _ h1), List.Mem.tail _ (List.Mem.tail _ h2)⟩

/-- **(−1)·d = e in 𝔽_ℓ** when d + e = ℓ (d = d' + 1). -/
theorem neg_mk (d' e : Nat) (hd : d' + 1 < kk + 1 + 1) (hed : e + d' = kk + 1) :
    (ring (kk + 1)).mul (StickBB.e (kk + 1)) (mk (kk + 1) (d' + 1)) = mk (kk + 1) e := Subtype.ext (by
  show md (kk + 1) (md (kk + 1) ((kk + 1) * md (kk + 1) 1) * md (kk + 1) (d' + 1)) = md (kk + 1) e
  have h1 : 1 < kk + 1 + 1 := Nat.succ_lt_succ (Nat.zero_lt_succ kk)
  rw [md_small (kk + 1) 1 h1, Nat.mul_one, md_small (kk + 1) (kk + 1) (Nat.lt_succ_self _),
    md_small (kk + 1) (d' + 1) hd]
  have e1 : (kk + 1) * (d' + 1) = e + d' * (kk + 1 + 1) := by rw [← hed]; lib_nat
  rw [e1, md_add_mul])

theorem lt_of_nle : ∀ a b : Nat, ¬ a ≤ b → b < a
  | 0, b, h => absurd (Nat.zero_le b) h
  | _ + 1, 0, _ => Nat.zero_lt_succ _
  | a + 1, b + 1, h => Nat.succ_lt_succ (lt_of_nle a b (fun h' => h (Nat.succ_le_succ h')))

theorem le_of_add_le_right (a c : Nat) : ∀ b, a + b ≤ c + b → a ≤ c
  | 0, h => h
  | b + 1, h => le_of_add_le_right a c b (Nat.le_of_succ_le_succ h)

theorem add_cancel_right (a c : Nat) : ∀ b, a + b = c + b → a = c
  | 0, h => h
  | b + 1, h => add_cancel_right a c b (Nat.succ.inj h)

/-- **v_(P_e)(σ_(−1)B) ≥ e − 1** for qs = qsL 0 kk h, and e lies in the conjugation list of the norm. -/
theorem val_lower (h : Nat) (hh : kk + 1 = h + h) (h1 : 1 ≤ h) (M : Nat) (α : V Int (kk + 1 + 1)) (z' : Zp M)
    (iv : Zp (kk + 1) → Zp (kk + 1)) (e' : Nat) (he1 : e' + 1 ≤ kk + 1) :
    mk (kk + 1) (e' + 1) ∈ ConjUnit.Fs (kk + 1) (BbarVal.nlist (kk + 1) iv (qsL kk 0 kk h)) ∧
      ∃ r, ProdVal.fexp (BbarVal.ees M (kk + 1) α (mk (kk + 1) (e' + 1)) z' (qsL kk 0 kk h)) = e' + r := by
  have hhk : h ≤ kk := Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left h1 h) (Nat.le_of_eq hh.symm))
  by_cases hA : e' + 1 ≤ h
  · obtain ⟨jj, hm, _⟩ := mem_qsL kk h 0 kk e' hhk hA
    have e1 : 0 + 1 + e' = e' + 1 := by lib_nat
    have e2 : 0 + e' = e' := by lib_nat
    rw [e1, e2] at hm
    refine ⟨(BbarVal.idx_mem (kk + 1) iv _ _ hm).1, ?_⟩
    have hmem := (mem_ees kk M α (mk (kk + 1) (e' + 1)) z' _ _ hm).2
    rw [show decide ((mk (kk + 1) (e' + 1), jj, e').1 = mk (kk + 1) (e' + 1)) = true from decide_eq_true rfl] at hmem
    obtain ⟨r, hr⟩ := SymProdZ.le_ex (le_fexp _ _ _ hmem)
    exact ⟨r, hr.symm⟩
  · have hB : h ≤ e' := Nat.le_of_succ_le_succ (lt_of_nle _ _ hA)
    obtain ⟨t, het⟩ := SymProdZ.le_ex (Nat.le_of_succ_le_succ he1)
    have hs : t + 1 + e' = h + h := ((show t + 1 + e' = e' + t + 1 by lib_nat).trans (congrArg (· + 1) het)).trans hh
    have ht : t < h := le_of_add_le_right _ _ _ (Nat.le_trans (Nat.le_of_eq hs) (Nat.add_le_add_left hB h))
    obtain ⟨jj, hm, hj⟩ := mem_qsL kk h 0 kk t hhk ht
    have e1 : 0 + 1 + t = t + 1 := by lib_nat
    have e2 : 0 + t = t := by lib_nat
    rw [e1, e2] at hm
    rw [e2] at hj
    have hjj : jj = e' := add_cancel_right _ _ _ (hj.trans ((show kk + 0 = kk by lib_nat).trans het.symm))
    have hneg : (ring (kk + 1)).mul (StickBB.e (kk + 1)) (mk (kk + 1) (t + 1)) = mk (kk + 1) (e' + 1) :=
      neg_mk kk t (e' + 1) (Nat.succ_lt_succ (Nat.lt_succ_of_lt (Nat.lt_of_lt_of_le ht hhk)))
        ((show e' + 1 + t = e' + t + 1 by lib_nat).trans (congrArg (· + 1) het))
    have hfs := (BbarVal.idx_mem (kk + 1) iv _ _ hm).2
    rw [show ((mk (kk + 1) (t + 1), jj, t) : Zp (kk + 1) × Nat × Nat).1 = mk (kk + 1) (t + 1) from rfl, hneg] at hfs
    refine ⟨hfs, ?_⟩
    have hmem := (mem_ees kk M α (mk (kk + 1) (e' + 1)) z' _ _ hm).1
    rw [show ((mk (kk + 1) (t + 1), jj, t) : Zp (kk + 1) × Nat × Nat).1 = mk (kk + 1) (t + 1) from rfl, hneg,
      show ((mk (kk + 1) (t + 1), jj, t) : Zp (kk + 1) × Nat × Nat).2.1 = jj from rfl, hjj,
      show decide (mk (kk + 1) (e' + 1) = mk (kk + 1) (e' + 1)) = true from decide_eq_true rfl] at hmem
    obtain ⟨r, hr⟩ := SymProdZ.le_ex (le_fexp _ _ _ hmem)
    exact ⟨r, hr.symm⟩

end StickQs

#print axioms StickQs.mem_qsL
#print axioms StickQs.sums_qsL
#print axioms StickQs.le_fexp
#print axioms StickQs.mem_ees
#print axioms StickQs.neg_mk
#print axioms StickQs.lt_of_nle
#print axioms StickQs.le_of_add_le_right
#print axioms StickQs.add_cancel_right
#print axioms StickQs.val_lower

lang/OneHotIdx.lean

/-
  lang/OneHotIdx — T49 (O3'): **a one-hot coefficient list has one ±1 entry at some index j, zeros elsewhere**.
  For the list (L m).map f (L m = allZ m (m + 1), built by appending):
    map_app, map_map      list map facts (by hand)
    zeros_app, onehot_app  Zeros / OneHot of l ++ [x]
    lt_succ_cases         i < k + 1 ⇒ i < k ∨ i = k
    onehot_idx            OneHot ((allZ k).map f) ⇒ f(j) = ±1 for one j < k, f(i) = 0 for the other i < k
    onehot_L              OneHot ((L m).map f) ⇒ ∃ j, f j = ±1 ∧ ∀ b ≠ j, f b = 0
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.UnitCirc2
import lang.GroupRing

namespace OneHotIdx

open FTAChain ZMod YMChain FiniteRecChain DetChain GroupRing UnitCirc2

theorem map_app {A B : Type} (f : A → B) (x : A) : ∀ l : List A, (l ++ [x]).map f = l.map f ++ [f x]
  | [] => rfl
  | y :: l => congrArg (List.cons (f y)) (map_app f x l)

theorem map_map {A B C : Type} (g : A → B) (h : B → C) : ∀ l : List A, (l.map g).map h = l.map (fun a => h (g a))
  | [] => rfl
  | y :: l => congrArg (List.cons (h (g y))) (map_map g h l)

theorem zeros_app (x : Int) : ∀ l : List Int, Zeros (l ++ [x]) → Zeros l ∧ x = 0
  | [], h => And.intro True.intro (And.left h)
  | _ :: l, h => And.intro (And.intro (And.left h) (zeros_app x l (And.right h)).1) (zeros_app x l (And.right h)).2

theorem onehot_app (x : Int) : ∀ l : List Int, OneHot (l ++ [x]) →
    (OneHot l ∧ x = 0) ∨ (Zeros l ∧ (x = 1 ∨ x = -1))
  | [], h => by
    cases h with
    | inl h1 => exact Or.inr ⟨True.intro, h1.1⟩
    | inr h2 => exact (h2.2 : False).elim
  | y :: l, h => by
    cases h with
    | inl h1 =>
      obtain ⟨hz, hx⟩ := zeros_app x l h1.2
      exact Or.inl ⟨Or.inl ⟨h1.1, hz⟩, hx⟩
    | inr h2 =>
      cases onehot_app x l h2.2 with
      | inl h3 => exact Or.inl ⟨Or.inr ⟨h2.1, h3.1⟩, h3.2⟩
      | inr h4 => exact Or.inr ⟨And.intro h2.1 h4.1, h4.2⟩

theorem lt_succ_cases : ∀ i k : Nat, i < k + 1 → i < k ∨ i = k
  | 0, 0, _ => Or.inr rfl
  | 0, _ + 1, _ => Or.inl (Nat.zero_lt_succ _)
  | _ + 1, 0, h => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _)
  | i + 1, k + 1, h => by
    cases lt_succ_cases i k (Nat.le_of_succ_le_succ h) with
    | inl h1 => exact Or.inl (Nat.succ_lt_succ h1)
    | inr h2 => exact Or.inr (congrArg Nat.succ h2)

variable (m : Nat)

theorem zeros_get (f : Zp m → Int) : ∀ k, Zeros ((CMInert.allZ m k).map f) → ∀ i, i < k → f (mk m i) = 0
  | 0, _, _, hi => absurd hi (Nat.not_lt_zero _)
  | k + 1, h, i, hi => by
    have h' : Zeros ((CMInert.allZ m k).map f ++ [f (mk m k)]) := by rw [← map_app f (mk m k)]; exact h
    obtain ⟨hz, hx⟩ := zeros_app _ _ h'
    cases lt_succ_cases i k hi with
    | inl h1 => exact zeros_get f k hz i h1
    | inr h2 => rw [h2]; exact hx

theorem onehot_idx (f : Zp m → Int) : ∀ k, OneHot ((CMInert.allZ m k).map f) →
    ∃ j, j < k ∧ (f (mk m j) = 1 ∨ f (mk m j) = -1) ∧ ∀ i, i < k → i ≠ j → f (mk m i) = 0
  | 0, h => (h : False).elim
  | k + 1, h => by
    have h' : OneHot ((CMInert.allZ m k).map f ++ [f (mk m k)]) := by rw [← map_app f (mk m k)]; exact h
    cases onehot_app _ _ h' with
    | inl h1 =>
      obtain ⟨j, hj, hpm, hz⟩ := onehot_idx f k h1.1
      refine ⟨j, Nat.lt_succ_of_lt hj, hpm, fun i hi hij => ?_⟩
      cases lt_succ_cases i k hi with
      | inl h3 => exact hz i h3 hij
      | inr h4 => rw [h4]; exact h1.2
    | inr h2 =>
      refine ⟨k, Nat.lt_succ_self k, h2.2, fun i hi hik => ?_⟩
      cases lt_succ_cases i k hi with
      | inl h3 => exact zeros_get m f k h2.1 i h3
      | inr h4 => exact absurd h4 hik

/-- **one-hot over all of 𝔽_ℓ**: f j = ±1 at one j, f b = 0 for b ≠ j. -/
theorem onehot_L (f : Zp m → Int) (h : OneHot ((L m).map f)) :
    ∃ j : Zp m, (f j = 1 ∨ f j = -1) ∧ ∀ b, b ≠ j → f b = 0 := by
  obtain ⟨j, hj, hpm, hz⟩ := onehot_idx m f (m + 1) h
  refine ⟨mk m j, hpm, fun b hb => ?_⟩
  rw [← mk_val m b]
  exact hz b.val b.property (fun e => hb ((mk_val m b).symm.trans (congrArg (mk m) e)))

end OneHotIdx

#print axioms OneHotIdx.map_app
#print axioms OneHotIdx.map_map
#print axioms OneHotIdx.zeros_app
#print axioms OneHotIdx.onehot_app
#print axioms OneHotIdx.lt_succ_cases
#print axioms OneHotIdx.zeros_get
#print axioms OneHotIdx.onehot_idx
#print axioms OneHotIdx.onehot_L

#print axioms: 51 theorems, no axioms.

78.382 The element-level Stickelberger relation

For \(\ell \ge 5\), \(p\equiv 1\pmod \ell \) and \(\alpha \) of norm \(p\) in \(P_1\): \(A\,\sigma _{-1}(B)\) vanishes at every Teichmüller point modulo \(p^{\ell -2}\) and has norm \(p^{2(\ell -2)}\), so \(A=\pm \zeta ^jB\).

\[ A\equiv \pm \sigma ^j\prod _{d\le (\ell -1)/2}(\sigma _d\alpha )^{\ell -1-d}(\sigma _{-d}\alpha )^{d-1}\pmod N. \]

lang/StickElem.lean

/-
  lang/StickElem — T49 (O3') assembly: **element-level Stickelberger** in the setting of ValC5.
  ℓ = ms + 3 prime, p = j + 3 prime with ℓ | p − 1, ℤ/(M + 1) with M + 1 = p^(ℓ−2) (hypotheses hk, hk₂, hM₂, hN0,
  hPk, hMk), z = ψ_(n'+1)(g) of order ℓ, α ∈ ℤ[C_ℓ] with N(α) = p and α ∈ P_1, A the primary element of ValC5,
  B = Π_(d ≤ (ℓ−1)/2) (σ_d α)^(ℓ−1−d) (σ_(−d) α)^(d−1) (StickQs.qsL). Then W = A·σ_(−1)B satisfies
    hW_all   φ_(z^c)(W) = 0 for c = 1..ℓ−1       (ValC5 + BbarVal + StickQs + EvalZero)
    hX_W     W·W̄ ≡ p^(2(ℓ−2)) (mod N)              (AbarGR + NormHyp + WWbar)
    stick_elem   W = p^(ℓ−2)·u + sN with u ≡ ±σ^j (mod N)  (StickCond)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.StickCond
import lang.StickQs
import lang.AbarGR
import lang.NormHyp
import lang.EvalZero
import lang.ValC5

namespace StickElem

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

variable (j M k J₂ k₂ N'' : Nat) (hk : M + 1 = (j + 1 + 1 + 1) * k) (hk₂ : M + 1 = (J₂ + 1 + 1) * k₂)
  (hM₂ : J₂ + 1 + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1))
  (hN0 : pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) (N'' + 1 + 1) = (ring M).zero)
  (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
  (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
  (ms t h' : Nat) (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1)
  (hN : j + 1 + 1 = (ms + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 = (h' + 1) + (h' + 1))
  (hPk : pw (ring M) (ExactP2.P (j + 1) M) (ms + 1) = (ring M).zero) (hlt : j + 1 + 1 + 1 < M + 1)
  (hzl : pw (ring M) (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g) (ms + 1 + 1 + 1) = (ring M).one)
  (α : V Int (ms + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1) → Zp (ms + 1 + 1))
  (hNα : NormEval.nrm (ms + 1 + 1) α (BbarVal.nlist (ms + 1 + 1) iv (StickQs.qsL (ms + 1) 0 (ms + 1) (h' + 1)))
    = nsm (gring LibInt.intR (ms + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1)).one)
  (h0 : RedP2.red (j + 1) M (ConjUnit.Y M (ms + 1 + 1) (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g) α (ring (ms + 1 + 1)).one)
    = (ring (j + 1 + 1)).zero)

/-- the exponent e with e + 1 = (t + 1)(c' + 1). -/
def n'e (c' t : Nat) : Nat := t * c' + t + c'

/-- the primary element A. -/
def A : V Int (ms + 1 + 1 + 1) := EisBridge.AA (gring LibInt.intR (ms + 1 + 1)) j (CycPrim.chi (ms + 1 + 1) j g) (ms + 1)
/-- the Stickelberger product B. -/
def B : V Int (ms + 1 + 1 + 1) :=
  AbsPair.Bp (gring LibInt.intR (ms + 1 + 1)) (StickBB.ents (ms + 1 + 1) α (StickQs.qsL (ms + 1) 0 (ms + 1) (h' + 1)))
/-- W = A·σ_(−1)B. -/
def W : V Int (ms + 1 + 1 + 1) := gmul LibInt.intR (ms + 1 + 1) (A j g ms) (StickBB.J (ms + 1 + 1) (B ms h' α))

include hk hk₂ hM₂ hN0 hpr hg hℓ hN hh hPk hlt hzl hNα h0 in
/-- **φ_(z^c)(A·σ_(−1)B) = 0 for every c = 1..ℓ−1**. -/
theorem hW_all : ∀ c, 1 ≤ c → c ≤ ms + 1 + 1 →
    CycEis.phz (ring M) (ms + 1 + 1) (pw (ring M) (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g) c) (W j g ms h' α) = (ring M).zero
  | 0, hc1, _ => absurd hc1 (Nat.not_succ_le_zero 0)
  | c' + 1, _, hc2 => by
    have hcl : c' + 1 < ms + 1 + 1 + 1 := Nat.succ_le_succ hc2
    -- the point e0 = c and its inverse
    have he0 : (mk (ms + 1 + 1) (c' + 1)).val = c' + 1 := md_small (ms + 1 + 1) (c' + 1) hcl
    have hx0 : mk (ms + 1 + 1) (c' + 1) ≠ (ring (ms + 1 + 1)).zero := fun hx => by
      have h1 := congrArg Subtype.val hx
      rw [he0] at h1
      exact Nat.noConfusion h1
    have hce := inv_exists (ms + 1) hℓ (mk (ms + 1 + 1) (c' + 1)) hx0
    -- B̄ side
    obtain ⟨hFs, r, hr⟩ := StickQs.val_lower (ms + 1) (h' + 1) hh (Nat.succ_le_succ (Nat.zero_le h')) M α
      (pw (ring M) (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g) (mk (ms + 1 + 1) (c' + 1)).val) iv c' hc2
    have hz : pw (ring M) (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g) (ms + 1 + 1 + 1) = (ring M).one := hzl
    obtain ⟨U', w', hB', _⟩ := BbarVal.bbar_val (j + 1) M k (ms + 1) hpr hk hPk hlt (ms + 1 + 1)
      (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g) hz α (mk (ms + 1 + 1) (c' + 1)) _ hce iv
      (mk (ms + 1 + 1) (0 + 1), ms + 1, 0) (StickQs.qsL (ms + 1) (0 + 1) (Nat.pred (ms + 1)) h') rfl hNα h0 hFs
    have hr' : ProdVal.fexp (BbarVal.ees M (ms + 1 + 1) α (mk (ms + 1 + 1) (c' + 1))
        (pw (ring M) (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g) (mk (ms + 1 + 1) (c' + 1)).val)
        ((mk (ms + 1 + 1) (0 + 1), ms + 1, 0) :: StickQs.qsL (ms + 1) (0 + 1) (Nat.pred (ms + 1)) h')) = c' + r := hr
    rw [hr', he0] at hB'
    -- A side
    have hcl' : c' + 1 + 1 ≤ ms + 1 + 1 + 1 := Nat.succ_le_succ hc2
    have he : n'e c' t + 1 = (t + 1) * (c' + 1) := by unfold n'e; lib_nat
    obtain ⟨v, U, hv, hA', _⟩ := ValC5.A_val_all j M k J₂ k₂ N'' hk hk₂ hM₂ hN0 hpr g hg (ms + 1) t c' (n'e c' t) hN he hcl' hℓ
    have hzc : pw (ring M) (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g) (c' + 1)
        = TeichN.psiN (j + 1) M (N'' + 1) (n'e c' t + 1) g := by
      unfold TeichN.psiN
      generalize pw (ring M) (TeichChar.lift (j + 1) M g) ((j + 1 + 1 + 1) ^ (N'' + 1)) = T
      rw [he]
      exact SymProdZ.pw_pw (ring M) T (t + 1) (c' + 1)
    rw [← hzc] at hA'
    -- vanishing
    have hP : pw (ring M) (ExactP2.P (j + 1) M) (v + (c' + r)) = (ring M).zero := by
      rw [show v + (c' + r) = (v + c') + r by lib_nat, hv, SymProdW.pw_add, hPk]
      lib_ring
    have hzc1 : pw (ring M) (pw (ring M) (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g) (c' + 1)) (ms + 1 + 1 + 1) = (ring M).one := by
      rw [SymProdZ.pw_pw]
      exact SymProdW.pw_mulE (ring M) _ (ms + 1 + 1 + 1) hz (c' + 1)
    exact EvalZero.eval_zero (ring M) (ms + 1 + 1) _ hzc1 (A j g ms) (B ms h' α) (ExactP2.P (j + 1) M) U U' v (c' + r)
      (v + (c' + r)) hA' hB' rfl hP

include hpr hg hℓ hN hh hNα in
/-- **W·W̄ ≡ p^(2(ℓ−2)) (mod N)**. -/
theorem hX_W (hMk : LibRing.pw LibInt.intR (Int.ofNat (j + 1 + 1 + 1)) (ms + 1) = Int.ofNat (M + 1)) :
    ∃ s₂, ∀ b, gt LibInt.intR (ms + 1 + 1)
      (gmul LibInt.intR (ms + 1 + 1) (W j g ms h' α) (StickBB.J (ms + 1 + 1) (W j g ms h' α))) b
      = Int.ofNat (M + 1) * Int.ofNat (M + 1) * delta LibInt.intR b (ring (ms + 1 + 1)).zero + s₂ := by
  obtain ⟨sA, hA⟩ := AbarGR.abar_hA j g hpr hg (ms + 1) t hℓ hN
  have hcj : h' + 1 ≤ ms + 1 := Nat.le_of_succ_le_succ
    (Nat.le_trans (Nat.add_le_add_left (Nat.succ_le_succ (Nat.zero_le h')) (h' + 1)) (Nat.le_of_eq hh.symm))
  have hB := fun b => NormHyp.norm_hyp (ms + 1 + 1) α iv (ms + 1) (j + 1 + 1 + 1) _
    (StickQs.sums_qsL (ms + 1) (h' + 1) 0 (ms + 1) hcj (by lib_nat)) hNα b
  obtain ⟨s, hs⟩ := WWbar.ww (ms + 1 + 1) (A j g ms) (B ms h' α)
    (LibRing.pw LibInt.intR (Int.ofNat (j + 1 + 1 + 1)) (ms + 1)) (LibRing.pw LibInt.intR (Int.ofNat (j + 1 + 1 + 1)) (ms + 1))
    sA 0 hA (fun x => (hB x).trans (Int.add_zero _).symm)
  exact ⟨s, fun b => (hs b).trans (by rw [hMk])⟩

include hk hk₂ hM₂ hN0 hpr hg hℓ hN hh hPk hlt hzl hNα h0 in
/-- **element-level Stickelberger**: A·σ_(−1)B = p^(ℓ−2)·u + sN with u ≡ ±σ^j (mod N). -/
theorem stick_elem (hMk : LibRing.pw LibInt.intR (Int.ofNat (j + 1 + 1 + 1)) (ms + 1) = Int.ofNat (M + 1))
    (hzo : OrdMul.Ord (ring (j + 1 + 1)) (RedP2.red (j + 1) M (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g)) (ms + 1 + 1 + 1)) :
    ∃ u : V Int (ms + 1 + 1 + 1), (∀ b, gt LibInt.intR (ms + 1 + 1) (W j g ms h' α) b
        = Int.ofNat (M + 1) * gt LibInt.intR (ms + 1 + 1) u b
          + gt LibInt.intR (ms + 1 + 1) (W j g ms h' α) (mk (ms + 1 + 1) (ms + 1 + 1))) ∧
      ∃ c' ε, (ε = 1 ∨ ε = -1) ∧
        UnitCirc1.sm (((L (ms + 1 + 1)).map (gt LibInt.intR (ms + 1 + 1) u)).map (fun a => a - c')) = ε ∧
        UnitCirc2.OneHot (((L (ms + 1 + 1)).map (gt LibInt.intR (ms + 1 + 1) u)).map (fun a => a - c')) :=
  StickCond.stick_cond (j + 1) M k (ms + 1) ms hpr hk hPk _ hzo hzl hℓ (W j g ms h' α)
    (hW_all j M k J₂ k₂ N'' hk hk₂ hM₂ hN0 hpr g hg ms t h' hℓ hN hh hPk hlt hzl α iv hNα h0)
    (hX_W j M hpr g hg ms t h' hℓ hN hh α iv hNα hMk)

end StickElem

#print axioms StickElem.hW_all
#print axioms StickElem.hX_W
#print axioms StickElem.stick_elem

lang/StickAB.lean

/-
  lang/StickAB — T49 (O3'): **A ≡ ±σ^j·B (mod N)**, the element-level Stickelberger relation.
  From W = A·σ_(−1)B = P·u + c₀N (StickElem), B·σ_(−1)B ≡ P (NormHyp), P ≠ 0 and u ≡ ε σ^j (mod N):
    P·A ≡ (B σ_(−1)B)·A = W·B ≡ P·u·B, cancel P (IntCancel.const_div) ⇒ A ≡ u·B ≡ ε σ^j B.
    u_delta     a one-hot shifted coefficient list gives u(b) = ε δ_(b,j) + c'     (OneHotIdx.onehot_L)
    ab_rel      the cancellation argument above
    stick_ab    with StickElem: A ≡ ε σ^j B (mod N), ε = ±1, for every α of norm p in P_1
  Lean 4 core only, proofs by hand (lib_ring, lib_int), no axioms.
-/
import lang.StickElem
import lang.OneHotIdx
import lang.IntCancel
import lib.IntRing

namespace StickAB

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob ModN
open FiniteRecChain (ite_pos' ite_neg')

variable (m : Nat)

/-- **u ≡ ε σ^j (mod N)** from the one-hot form of StickCond. -/
theorem u_delta (u : V Int (m + 1)) (c' : Int)
    (h : UnitCirc2.OneHot (((L m).map (gt LibInt.intR m u)).map (fun a => a - c'))) :
    ∃ j ε, (ε = 1 ∨ ε = -1) ∧ ∀ b, gt LibInt.intR m u b = ε * delta LibInt.intR b j + c' := by
  rw [OneHotIdx.map_map] at h
  obtain ⟨j, hpm, hz⟩ := OneHotIdx.onehot_L m (fun b => gt LibInt.intR m u b - c') h
  refine ⟨j, gt LibInt.intR m u j - c', hpm, fun b => ?_⟩
  unfold delta
  by_cases hb : b = j
  · rw [ite_pos' _ _ hb, hb]
    show gt LibInt.intR m u j = (gt LibInt.intR m u j - c') * 1 + c'
    lib_int
  · rw [ite_neg' _ _ hb]
    have h0 : gt LibInt.intR m u b - c' = 0 := hz b hb
    show gt LibInt.intR m u b = (gt LibInt.intR m u j - c') * 0 + c'
    calc gt LibInt.intR m u b = (gt LibInt.intR m u b - c') + c' := by lib_int
      _ = 0 + c' := by rw [h0]
      _ = (gt LibInt.intR m u j - c') * 0 + c' := by lib_int

/-- **the cancellation**: A ≡ ε σ^j B (mod N). -/
theorem ab_rel (A B u : V Int (m + 1)) (P c0 sB : Int) (hP : P ≠ 0)
    (hW : ∀ b, gt LibInt.intR m (gmul LibInt.intR m A (StickBB.J m B)) b = P * gt LibInt.intR m u b + c0)
    (hBB : ∀ b, gt LibInt.intR m (gmul LibInt.intR m B (StickBB.J m B)) b = P * delta LibInt.intR b (ring m).zero + sB)
    (j : Zp m) (ε c' : Int) (hu : ∀ b, gt LibInt.intR m u b = ε * delta LibInt.intR b j + c') :
    ModN m A (gmul LibInt.intR m (mono LibInt.intR m j ε) B) := by
  have h1 : ModN m (gmul LibInt.intR m A (StickBB.J m B)) (ModSq.sc m P u) := ModSq.modn_sc m P c0 _ _ hW
  have h2 := modn_mul m _ _ B h1
  have h3 : ModN m (gmul LibInt.intR m B (StickBB.J m B)) (ModSq.sc m P (gone LibInt.intR m)) :=
    ModSq.modn_sc m P sB _ _ (fun b => by rw [hBB b, WWbar.gt_one])
  have h4 := modn_mul m _ _ A h3
  have hre : gmul LibInt.intR m (gmul LibInt.intR m A (StickBB.J m B)) B
      = gmul LibInt.intR m (gmul LibInt.intR m B (StickBB.J m B)) A := by
    show (gring LibInt.intR m).mul ((gring LibInt.intR m).mul A (StickBB.J m B)) B
      = (gring LibInt.intR m).mul ((gring LibInt.intR m).mul B (StickBB.J m B)) A
    generalize StickBB.J m B = JB
    lib_ring
  have h2' : ModN m (gmul LibInt.intR m (gmul LibInt.intR m B (StickBB.J m B)) A)
      (gmul LibInt.intR m (ModSq.sc m P u) B) := by rw [← hre]; exact h2
  obtain ⟨t, ht⟩ := modn_trans m _ _ _ (modn_symm m _ _ h4) h2'
  have hone : gmul LibInt.intR m (gone LibInt.intR m) A = A := (gring LibInt.intR m).one_mul A
  have hb : ∀ b, P * (gt LibInt.intR m A b - gt LibInt.intR m (gmul LibInt.intR m u B) b) = t := fun b => by
    have e := ht b
    rw [WWbar.sc_mul_l, WWbar.sc_mul_l, hone] at e
    generalize gt LibInt.intR m A b = x at e ⊢
    generalize gt LibInt.intR m (gmul LibInt.intR m u B) b = y at e ⊢
    calc P * (x - y) = P * x - P * y := by lib_int
      _ = (P * y + t) - P * y := by rw [e]
      _ = t := by lib_int
  have h5 : ModN m A (gmul LibInt.intR m u B) :=
    ⟨gt LibInt.intR m A (ring m).zero - gt LibInt.intR m (gmul LibInt.intR m u B) (ring m).zero, fun b => by
      have e : gt LibInt.intR m A b - gt LibInt.intR m (gmul LibInt.intR m u B) b
          = gt LibInt.intR m A (ring m).zero - gt LibInt.intR m (gmul LibInt.intR m u B) (ring m).zero :=
        IntCancel.const_div (fun b => gt LibInt.intR m A b - gt LibInt.intR m (gmul LibInt.intR m u B) b)
          P t hP hb b (ring m).zero
      generalize gt LibInt.intR m A (ring m).zero - gt LibInt.intR m (gmul LibInt.intR m u B) (ring m).zero = c at e ⊢
      generalize gt LibInt.intR m A b = x at e ⊢
      generalize gt LibInt.intR m (gmul LibInt.intR m u B) b = y at e ⊢
      calc x = y + (x - y) := by lib_int
        _ = y + c := by rw [e]⟩
  have h6 : ModN m u (mono LibInt.intR m j ε) := ⟨c', fun b =>
    (hu b).trans (congrArg (fun z => z + c') (gt_mono LibInt.intR m j ε b).symm)⟩
  exact modn_trans m _ _ _ h5 (modn_mul m _ _ B h6)

end StickAB

#print axioms StickAB.u_delta
#print axioms StickAB.ab_rel

lang/StickABFull.lean

/-
  lang/StickABFull — T49 (O3'): **element-level Stickelberger, A ≡ ±σ^j·B (mod N)**, in the setting of StickElem:
  ℓ = ms + 3, p = j + 3 primes with ℓ | p − 1, M + 1 = p^(ℓ−2), α ∈ ℤ[C_ℓ] of norm p lying in P_1,
  A the primary element (ValC5), B = Π_(d ≤ (ℓ−1)/2) (σ_d α)^(ℓ−1−d) (σ_(−d) α)^(d−1).
    stick_ab_full   ∃ j ε, ε = ±1 ∧ A ≡ ε σ^j B (mod N)      (StickElem.stick_elem + StickAB.u_delta + StickAB.ab_rel)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.StickAB

namespace StickABFull

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

theorem stick_ab_full (j M k J₂ k₂ N'' : Nat) (hk : M + 1 = (j + 1 + 1 + 1) * k) (hk₂ : M + 1 = (J₂ + 1 + 1) * k₂)
    (hM₂ : J₂ + 1 + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1))
    (hN0 : pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) (N'' + 1 + 1) = (ring M).zero)
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (ms t h' : Nat) (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 = (h' + 1) + (h' + 1))
    (hPk : pw (ring M) (ExactP2.P (j + 1) M) (ms + 1) = (ring M).zero) (hlt : j + 1 + 1 + 1 < M + 1)
    (hzl : pw (ring M) (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g) (ms + 1 + 1 + 1) = (ring M).one)
    (α : V Int (ms + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1) → Zp (ms + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1) α (BbarVal.nlist (ms + 1 + 1) iv (StickQs.qsL (ms + 1) 0 (ms + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) M (ConjUnit.Y M (ms + 1 + 1) (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g) α (ring (ms + 1 + 1)).one)
      = (ring (j + 1 + 1)).zero)
    (hMk : LibRing.pw LibInt.intR (Int.ofNat (j + 1 + 1 + 1)) (ms + 1) = Int.ofNat (M + 1))
    (hzo : OrdMul.Ord (ring (j + 1 + 1)) (RedP2.red (j + 1) M (TeichN.psiN (j + 1) M (N'' + 1) (t + 1) g)) (ms + 1 + 1 + 1)) :
    ∃ jj ε, (ε = 1 ∨ ε = -1) ∧ ModN.ModN (ms + 1 + 1) (StickElem.A j g ms)
      (gmul LibInt.intR (ms + 1 + 1) (mono LibInt.intR (ms + 1 + 1) jj ε) (StickElem.B ms h' α)) := by
  obtain ⟨u, hu, c', _, _, _, hoh⟩ := StickElem.stick_elem j M k J₂ k₂ N'' hk hk₂ hM₂ hN0 hpr g hg ms t h' hℓ hN hh hPk hlt
    hzl α iv hNα h0 hMk hzo
  obtain ⟨jj, ε, hε, hud⟩ := StickAB.u_delta (ms + 1 + 1) u c' hoh
  have hcj : h' + 1 ≤ ms + 1 := Nat.le_of_succ_le_succ
    (Nat.le_trans (Nat.add_le_add_left (Nat.succ_le_succ (Nat.zero_le h')) (h' + 1)) (Nat.le_of_eq hh.symm))
  have hBB : ∀ b, gt LibInt.intR (ms + 1 + 1)
      (gmul LibInt.intR (ms + 1 + 1) (StickElem.B ms h' α) (StickBB.J (ms + 1 + 1) (StickElem.B ms h' α))) b
      = Int.ofNat (M + 1) * delta LibInt.intR b (ring (ms + 1 + 1)).zero + 0 := fun b =>
    (NormHyp.norm_hyp (ms + 1 + 1) α iv (ms + 1) (j + 1 + 1 + 1) _
      (StickQs.sums_qsL (ms + 1) (h' + 1) 0 (ms + 1) hcj (by lib_nat)) hNα b).trans
      (by rw [hMk]; exact (Int.add_zero _).symm)
  exact ⟨jj, ε, hε, StickAB.ab_rel (ms + 1 + 1) (StickElem.A j g ms) (StickElem.B ms h' α) u (Int.ofNat (M + 1)) _ 0
    (fun h => Nat.noConfusion (Int.ofNat.inj h)) hu hBB jj ε c' hud⟩

end StickABFull

#print axioms StickABFull.stick_ab_full

lang/StickInst.lean

/-
  lang/StickInst — T49 (O3'): **element-level Stickelberger with the ring hypotheses discharged**.
  ℓ = ms + 4 ≥ 5 and p = j + 3 primes, p − 1 = ℓ(t + 1), M + 1 = p^(ℓ−2), z = ψ_(t+1)(g) the Teichmüller point:
    pw_mk        (a mod M+1)^n = a^n mod M+1
    P_vanish     p^(ℓ−2) = 0 in ℤ/(M + 1)
    cast_pw      (a : ℤ)^n = (a^n : ℕ)
    lt_M         p < M + 1
    mul_lt_left  r < L ⇒ (t + 1) r < (t + 1) L
    z_ord        red z = g^(t+1) has order ℓ in 𝔽_p^×
    stick_inst   N(α) = p, α ∈ P_1 ⇒ A ≡ ±σ^j B (mod N)     (StickABFull with all ring hypotheses proved)
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.StickABFull
import lang.ValA1
import lang.CycExact

namespace StickInst

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

theorem pw_mk (M a : Nat) : ∀ n, pw (ring M) (mk M a) n = mk M (a ^ n)
  | 0 => rfl
  | n + 1 => Subtype.ext (by
    show md M ((pw (ring M) (mk M a) n).val * (mk M a).val) = md M (a ^ n * a)
    rw [pw_mk M a n]
    exact (md_mul2 M _ _).symm)

theorem P_vanish (j M e : Nat) (hM : M + 1 = (j + 1 + 1 + 1) ^ e) :
    pw (ring M) (nsm (ring M) (j + 1 + 1 + 1) (ring M).one) e = (ring M).zero := by
  rw [CycUnit.nsm_mk M (j + 1 + 1 + 1), pw_mk, ← hM]
  exact Subtype.ext ((md_self M).trans (md_small M 0 (Nat.zero_lt_succ M)).symm)

theorem cast_pw (a : Nat) : ∀ n, LibRing.pw LibInt.intR (Int.ofNat a) n = Int.ofNat (a ^ n)
  | 0 => rfl
  | n + 1 => by
    show LibInt.intR.mul (LibRing.pw LibInt.intR (Int.ofNat a) n) (Int.ofNat a) = Int.ofNat (a ^ n * a)
    rw [cast_pw a n]
    rfl

theorem pow_pos1 (p : Nat) : ∀ n, 1 ≤ (p + 1) ^ n
  | 0 => Nat.le_refl 1
  | n + 1 => by
    show 1 ≤ (p + 1) ^ n * (p + 1)
    have e : (p + 1) ^ n * (p + 1) = (p + 1) ^ n + (p + 1) ^ n * p := by
      generalize (p + 1) ^ n = Q; lib_nat
    rw [e]
    exact Nat.le_trans (pow_pos1 p n) (Nat.le_add_right _ _)

theorem lt_M (j M ms : Nat) (hM : M + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1)) : j + 1 + 1 + 1 < M + 1 := by
  rw [hM]
  show j + 1 + 1 + 1 < (j + 1 + 1 + 1) ^ ms * (j + 1 + 1 + 1) * (j + 1 + 1 + 1)
  obtain ⟨Q, hQ⟩ := SymProdZ.le_ex (pow_pos1 (j + 1 + 1) ms)
  rw [← hQ]
  have e : (1 + Q) * (j + 1 + 1 + 1) * (j + 1 + 1 + 1)
      = (j + 1 + 1 + 1) + ((j * j + 5 * j + 5 + Q * (j + 1 + 1 + 1) * (j + 1 + 1 + 1)) + 1) := by lib_nat
  rw [e]
  exact Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.add_le_add_left (Nat.succ_le_succ (Nat.zero_le _)) _)

theorem mul_lt_left (r L : Nat) (h : r < L) : ∀ t, (t + 1) * r < (t + 1) * L
  | 0 => by
    rw [show (0 + 1) * r = r by lib_nat, show (0 + 1) * L = L by lib_nat]
    exact h
  | t + 1 => by
    rw [show (t + 1 + 1) * r = (t + 1) * r + r by lib_nat, show (t + 1 + 1) * L = (t + 1) * L + L by lib_nat]
    exact Nat.add_lt_add (mul_lt_left r L h t) h

theorem z_ord (j ms t : Nat) (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) :
    OrdMul.Ord (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) (ms + 1 + 1 + 1 + 1) := by
  apply OrdMul.ord_of_min (ring (j + 1 + 1)) _ (ms + 1 + 1 + 1)
  · rw [SymProdZ.pw_pw, show (t + 1) * (ms + 1 + 1 + 1 + 1) = j + 1 + 1 by rw [hN]; lib_nat]
    exact hg.1
  · intro r hr0 hrl
    rw [SymProdZ.pw_pw]
    apply CycExact.small_ne_one j g hg ((t + 1) * r)
    · rw [show (t + 1) * r = r + t * r by lib_nat]
      exact Nat.lt_of_lt_of_le hr0 (Nat.le_add_right r (t * r))
    · have h1 := mul_lt_left r (ms + 1 + 1 + 1 + 1) hrl t
      rw [show (t + 1) * (ms + 1 + 1 + 1 + 1) = j + 1 + 1 by rw [hN]; lib_nat] at h1
      exact h1

/-- **element-level Stickelberger**: for ℓ ≥ 5, p ≡ 1 (mod ℓ), α ∈ ℤ[C_ℓ] of norm p lying in P_1,
A ≡ ε σ^j B (mod N) with ε = ±1. -/
theorem stick_inst (j M ms t h' : Nat) (hM : M + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) M (ConjUnit.Y M (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) M (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero) :
    ∃ jj ε, (ε = 1 ∨ ε = -1) ∧ ModN.ModN (ms + 1 + 1 + 1) (StickElem.A j g (ms + 1))
      (gmul LibInt.intR (ms + 1 + 1 + 1) (mono LibInt.intR (ms + 1 + 1 + 1) jj ε) (StickElem.B (ms + 1) h' α)) := by
  have hk : M + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1) ^ (ms + 1) :=
    hM.trans (Nat.mul_comm ((j + 1 + 1 + 1) ^ (ms + 1)) (j + 1 + 1 + 1))
  have hk₂ : M + 1 = (j * j + 6 * j + 7 + 1 + 1) * (j + 1 + 1 + 1) ^ ms := by
    rw [hM]
    show (j + 1 + 1 + 1) ^ ms * (j + 1 + 1 + 1) * (j + 1 + 1 + 1) = _
    generalize (j + 1 + 1 + 1) ^ ms = Q
    lib_nat
  have hM₂ : j * j + 6 * j + 7 + 1 + 1 = (j + 1 + 1 + 1) * (j + 1 + 1 + 1) := by lib_nat
  have hP0 := P_vanish j M (ms + 1 + 1) hM
  have hMk : LibRing.pw LibInt.intR (Int.ofNat (j + 1 + 1 + 1)) (ms + 1 + 1) = Int.ofNat (M + 1) :=
    (cast_pw (j + 1 + 1 + 1) (ms + 1 + 1)).trans (congrArg Int.ofNat hM.symm)
  have hzl := ValA1.z_ellN j M _ (ms + 1) hk hP0 g hg (ms + 1 + 1) (t + 1) t hN rfl
  have hzo : OrdMul.Ord (ring (j + 1 + 1)) (RedP2.red (j + 1) M (TeichN.psiN (j + 1) M (ms + 1) (t + 1) g))
      (ms + 1 + 1 + 1 + 1) := by
    rw [TeichN.red_psiN (j + 1) M _ hk hpr (ms + 1) (t + 1) g]
    exact z_ord j ms t g hg hN
  exact StickABFull.stick_ab_full j M _ (j * j + 6 * j + 7) _ ms hk hk₂ hM₂ hP0 hpr g hg (ms + 1) t h' hℓ hN hh hP0
    (lt_M j M ms hM) hzl α iv hNα h0 hMk hzo

end StickInst

#print axioms StickInst.pw_mk
#print axioms StickInst.P_vanish
#print axioms StickInst.cast_pw
#print axioms StickInst.pow_pos1
#print axioms StickInst.lt_M
#print axioms StickInst.mul_lt_left
#print axioms StickInst.z_ord
#print axioms StickInst.stick_inst

#print axioms: 14 theorems, no axioms.

78.383 Eisenstein reciprocity transported to a prime element of norm \(p\)

Evaluation at \(u\) kills \(N\), the Galois maps \(\tau _c\) and Frobenius act on \(E=\mathbb F_q[C_\ell ]/(N)\), and the global symbol \(G(W)=\prod _i\tau (g^i)(W)^{g^i}\) turns \(A=\pm \zeta ^jB\) into a relation for \(\alpha \) (exponent \(\sum _d d^{-1}(\ell -2d)\equiv 1\)).

\[ (u^{jM})^{\ell -1}\,G\bigl (\varphi _u(\alpha )^M\bigr )=\bigl (u^{\sum _c k_c(\ell -1-c)}\bigr )^{\ell -1}. \]

lang/PhzModN.lean

/-
  lang/PhzModN — T49 (from A to α, step 1): **evaluation at u kills N**, so the element-level
  Stickelberger congruence A ≡ ε σ^j B (mod N) becomes an equation in E = R[C_ℓ]/(N):
    sumK_cmul    Σ c·f = c·Σ f
    sumK_addc    Σ (f + c)·g = Σ f g + c·Σ g
    phz_modn     Σ_b z^b = 0 and u ≡ v (mod N) ⇒ φ_z(u) = φ_z(v)
    phz_mono     φ_z(s·σ^a) = s·z^a
    rng_gs       Σ_(i<n) w^i over rng n is CycChar.gs w n
    sum_u        Σ_b u^b = 0 in R[C_ℓ]/(N) (ℓ prime)
    phz_u_modn   u ≡ v (mod N) ⇒ φ_u(u) = φ_u(v) in R[C_ℓ]/(N)
    phz_A_B      A ≡ ε σ^j B (mod N) ⇒ φ_u(A) = ε·u^j·φ_u(B)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.ModN
import lang.CycEis
import lang.CycUnit
import lang.PhzSum
import lib.RingTac
import lib.Semi

namespace PhzModN

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)

variable {S : Type} (E : CRing S)

theorem sumK_cmul {A : Type} (c : S) (f : A → S) : ∀ l : List A,
    sumK E (fun a => E.mul c (f a)) l = E.mul c (sumK E f l)
  | [] => by show E.zero = E.mul c E.zero; lib_ring
  | a :: l => by
    show E.add (E.mul c (f a)) (sumK E (fun a => E.mul c (f a)) l) = E.mul c (E.add (f a) (sumK E f l))
    rw [sumK_cmul c f l]
    lib_ring

theorem sumK_addc {A : Type} (c : S) (f g : A → S) : ∀ l : List A,
    sumK E (fun a => E.mul (E.add (f a) c) (g a)) l
      = E.add (sumK E (fun a => E.mul (f a) (g a)) l) (E.mul c (sumK E g l))
  | [] => by show E.zero = E.add E.zero (E.mul c E.zero); lib_ring
  | a :: l => by
    show E.add (E.mul (E.add (f a) c) (g a)) (sumK E (fun a => E.mul (E.add (f a) c) (g a)) l)
      = E.add (E.add (E.mul (f a) (g a)) (sumK E (fun a => E.mul (f a) (g a)) l)) (E.mul c (E.add (g a) (sumK E g l)))
    rw [sumK_addc c f g l]
    generalize sumK E (fun a => E.mul (f a) (g a)) l = X
    generalize sumK E g l = Y
    lib_ring

theorem phz_eq (m : Nat) (z : S) (v : V Int (m + 1)) :
    CycEis.phz E m z v = sumK E (fun b => E.mul (castZ E (gt LibInt.intR m v b)) (pw E z b.val)) (L m) :=
  PhzSum.phz_sum E m z v

/-- **φ_z respects congruence mod N when Σ_b z^b = 0**. -/
theorem phz_modn (m : Nat) (z : S) (hs : sumK E (fun b : Zp m => pw E z b.val) (L m) = E.zero)
    (u v : V Int (m + 1)) (h : ModN.ModN m u v) : CycEis.phz E m z u = CycEis.phz E m z v := by
  obtain ⟨t, ht⟩ := h
  have e1 : sumK E (fun b => E.mul (castZ E (gt LibInt.intR m u b)) (pw E z b.val)) (L m)
      = sumK E (fun b => E.mul (E.add (castZ E (gt LibInt.intR m v b)) (castZ E t)) (pw E z b.val)) (L m) :=
    sumK_ext E _ _ (fun b => by rw [ht b, IntCast.castZ_add]) (L m)
  have e2 := sumK_addc E (castZ E t) (fun b => castZ E (gt LibInt.intR m v b)) (fun b : Zp m => pw E z b.val) (L m)
  rw [phz_eq, phz_eq, e1]
  refine e2.trans ?_
  show E.add (sumK E (fun b => E.mul (castZ E (gt LibInt.intR m v b)) (pw E z b.val)) (L m))
      (E.mul (castZ E t) (sumK E (fun b : Zp m => pw E z b.val) (L m))) = _
  rw [hs]
  lib_ring

/-- **φ_z(s·σ^a) = s·z^a**. -/
theorem phz_mono (m : Nat) (z : S) (a : Zp m) (s : Int) :
    CycEis.phz E m z (mono LibInt.intR m a s) = E.mul (castZ E s) (pw E z a.val) := by
  have e1 : sumK E (fun b => E.mul (castZ E (gt LibInt.intR m (mono LibInt.intR m a s) b)) (pw E z b.val)) (L m)
      = sumK E (fun b => E.mul (castZ E s) (E.mul (delta E b a) (pw E z b.val))) (L m) :=
    sumK_ext E _ _ (fun b => by
      rw [gt_mono]
      show E.mul (castZ E (s * delta LibInt.intR b a)) (pw E z b.val) = _
      rw [HasseDavenport.castZ_mul, CycPrim.cast_delta]
      lib_ring) (L m)
  rw [phz_eq, e1]
  refine (sumK_cmul E (castZ E s) (fun b => E.mul (delta E b a) (pw E z b.val)) (L m)).trans ?_
  exact congrArg (E.mul (castZ E s)) (pick E _ (CMInert.complete_allZ m E) (fun b => pw E z b.val) a)

theorem rng_gs (w : S) : ∀ n, sumK E (fun i => pw E w i) (CycUnit.rng n) = CycChar.gs E w n
  | 0 => rfl
  | n + 1 => by
    show sumK E (fun i => pw E w i) (CycUnit.rng n ++ [n]) = E.add (pw E w n) (CycChar.gs E w n)
    rw [sumK_append, rng_gs w n]
    show E.add (CycChar.gs E w n) (E.add (pw E w n) E.zero) = _
    lib_ring

variable {R : Type} (K : CRing R)

/-- **Σ_b u^b = 0 in R[C_ℓ]/(N)**, ℓ = n + 2 prime. -/
theorem sum_u (n : Nat) (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) :
    sumK (CycRing.cyc K (n + 1)) (fun b : Zp (n + 1) => pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) b.val) (L (n + 1))
      = (CycRing.cyc K (n + 1)).zero := by
  show sumK (CycRing.cyc K (n + 1)) (fun b : Zp (n + 1) => pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) b.val)
    (CMInert.allZ (n + 1) (n + 1 + 1)) = _
  rw [CycUnit.sum_allZ (n + 1) (CycRing.cyc K (n + 1)) _ (n + 1 + 1)]
  have e1 : sumK (CycRing.cyc K (n + 1)) (fun i => pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) (md (n + 1) i))
        (CycUnit.rng (n + 1 + 1))
      = sumK (CycRing.cyc K (n + 1)) (fun i => pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) i) (CycUnit.rng (n + 1 + 1)) :=
    sumK_ext (CycRing.cyc K (n + 1)) _ _ (fun i =>
      GEval.pw_md (CycRing.cyc K (n + 1)) (n + 1) _ (CycUnit.u_ell K (n + 1)) i) (CycUnit.rng (n + 1 + 1))
  refine Eq.trans e1 ?_
  rw [rng_gs]
  have h1 : CycUnit.u K (n + 1) = pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) 1 := by
    show CycUnit.u K (n + 1) = (CycRing.cyc K (n + 1)).mul (CycRing.cyc K (n + 1)).one (CycUnit.u K (n + 1))
    exact ((CycRing.cyc K (n + 1)).one_mul _).symm
  rw [h1]
  apply CycUnit.gs_u K n hpr 1
  intro h0
  rw [CycUnit.nsm_mk (n + 1) 1] at h0
  have hv := congrArg Subtype.val h0
  have e1 : (mk (n + 1) 1).val = 1 := md_small (n + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ n))
  have e0 : ((ring (n + 1)).zero).val = 0 := md_small (n + 1) 0 (Nat.zero_lt_succ (n + 1))
  rw [e1, e0] at hv
  exact Nat.noConfusion hv

/-- **u ≡ v (mod N) ⇒ φ_u(u) = φ_u(v)** in R[C_ℓ]/(N). -/
theorem phz_u_modn (n : Nat) (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) (x y : V Int (n + 1 + 1))
    (h : ModN.ModN (n + 1) x y) :
    CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) x
      = CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) y :=
  phz_modn (CycRing.cyc K (n + 1)) (n + 1) _ (sum_u K n hpr) x y h

/-- **A ≡ ε σ^j B (mod N) ⇒ φ_u(A) = ε·u^j·φ_u(B)** in R[C_ℓ]/(N). -/
theorem phz_A_B (n : Nat) (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) (x y : V Int (n + 1 + 1))
    (jj : Zp (n + 1)) (ε : Int) (h : ModN.ModN (n + 1) x (gmul LibInt.intR (n + 1) (mono LibInt.intR (n + 1) jj ε) y)) :
    CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) x
      = (CycRing.cyc K (n + 1)).mul
          ((CycRing.cyc K (n + 1)).mul (castZ (CycRing.cyc K (n + 1)) ε) (pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) jj.val))
          (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) y) := by
  rw [phz_u_modn K n hpr _ _ h]
  show CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1))
      ((gring LibInt.intR (n + 1)).mul (mono LibInt.intR (n + 1) jj ε) y) = _
  rw [CycEis.phz_mul (CycRing.cyc K (n + 1)) (n + 1) _ (CycUnit.u_ell K (n + 1)), phz_mono]

end PhzModN

#print axioms PhzModN.sumK_cmul
#print axioms PhzModN.sumK_addc
#print axioms PhzModN.phz_eq
#print axioms PhzModN.phz_modn
#print axioms PhzModN.phz_mono
#print axioms PhzModN.rng_gs
#print axioms PhzModN.sum_u
#print axioms PhzModN.phz_u_modn
#print axioms PhzModN.phz_A_B

lang/CycTau.lean

/-
  lang/CycTau — T49 (from A to α, step 2): **the Galois automorphism τ_c of E = R[C_ℓ]/(N)**, u ↦ u^c, and
  ring maps commute with evaluation:
    hom_zero, hom_neg, hom_castN, hom_castZ, hom_pw, hom_sumK    a map preserving +, ·, 1 is a ring map
    hom_phz      f(φ_z(v)) = φ_(f z)(v)
    tau          τ(x) := [σ_c(x)]  (CycGal.gsig with c·d = 1)
    tau_qmap     τ([v]) = [σ_c v]
    tau_add, tau_mul, tau_one        τ is a ring map E → E
    tau_u        τ(u) = u^c
    tau_phz      τ(φ_u(v)) = φ_(u^c)(v)
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycGal
import lang.CycUnit
import lang.PhzSum
import lib.RingTac
import lib.Semi

namespace CycTau

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)

section Hom

variable {S T : Type} (E : CRing S) (F : CRing T) (f : S → T)
  (hadd : ∀ x y, f (E.add x y) = F.add (f x) (f y)) (hmul : ∀ x y, f (E.mul x y) = F.mul (f x) (f y))
  (hone : f E.one = F.one)

include hadd in
theorem hom_zero : f E.zero = F.zero := by
  have h := hadd E.zero E.zero
  rw [show E.add E.zero E.zero = E.zero by lib_ring] at h
  calc f E.zero = F.add (F.add (f E.zero) (f E.zero)) (F.neg (f E.zero)) := by lib_ring
    _ = F.add (f E.zero) (F.neg (f E.zero)) := by rw [← h]
    _ = F.zero := by lib_ring

include hadd in
theorem hom_neg (x : S) : f (E.neg x) = F.neg (f x) := by
  have h := hadd (E.neg x) x
  rw [show E.add (E.neg x) x = E.zero by lib_ring, hom_zero E F f hadd] at h
  calc f (E.neg x) = F.add (F.add (f (E.neg x)) (f x)) (F.neg (f x)) := by lib_ring
    _ = F.add F.zero (F.neg (f x)) := by rw [← h]
    _ = F.neg (f x) := by lib_ring

include hadd hone in
theorem hom_castN : ∀ n, f (castN E n) = castN F n
  | 0 => hom_zero E F f hadd
  | n + 1 => by
    show f (E.add E.one (castN E n)) = F.add F.one (castN F n)
    rw [hadd, hone, hom_castN n]

include hadd hone in
theorem hom_castZ : ∀ k, f (castZ E k) = castZ F k
  | .ofNat n => hom_castN E F f hadd hone n
  | .negSucc n => by
    show f (E.neg (castN E (n + 1))) = F.neg (castN F (n + 1))
    rw [hom_neg E F f hadd, hom_castN E F f hadd hone]

include hmul hone in
theorem hom_pw (a : S) : ∀ n, f (pw E a n) = pw F (f a) n
  | 0 => hone
  | n + 1 => by
    show f (E.mul (pw E a n) a) = F.mul (pw F (f a) n) (f a)
    rw [hmul, hom_pw a n]

include hadd in
theorem hom_sumK {A : Type} (g : A → S) : ∀ l : List A, f (sumK E g l) = sumK F (fun a => f (g a)) l
  | [] => hom_zero E F f hadd
  | a :: l => by
    show f (E.add (g a) (sumK E g l)) = F.add (f (g a)) (sumK F (fun a => f (g a)) l)
    rw [hadd, hom_sumK g l]

include hadd hmul hone in
/-- **f(φ_z(v)) = φ_(f z)(v)** for a ring map f. -/
theorem hom_phz (m : Nat) (z : S) (v : V Int (m + 1)) : f (CycEis.phz E m z v) = CycEis.phz F m (f z) v := by
  rw [PhzSum.phz_sum, PhzSum.phz_sum, hom_sumK E F f hadd]
  exact sumK_ext F _ _ (fun b => by
    show f (E.mul (castZ E (gt LibInt.intR m v b)) (pw E z b.val)) = F.mul (castZ F (gt LibInt.intR m v b)) (pw F (f z) b.val)
    rw [hmul, hom_castZ E F f hadd hone, hom_pw E F f hmul hone]) _

end Hom

variable {R : Type} (K : CRing R) (m : Nat) (c d : Zp m) (hcd : (ring m).mul c d = (ring m).one)

/-- τ_c on R[C_ℓ]/(N). -/
def tau (x : CycRing.C K m) : CycRing.C K m := CycRing.qmap K m (CycGal.gsig K m d x.val)

theorem tau_qmap (v : V R (m + 1)) : tau K m d (CycRing.qmap K m v) = CycRing.qmap K m (CycGal.gsig K m d v) :=
  CycRing.cext K m _ _ (CycRing.red_shift K m _ _ (K.neg (gt K m v (CycRing.top m))) (fun a => by
    rw [CycGal.gt_gsig]
    show gt K m (CycRing.red K m v) ((ring m).mul a d) = _
    rw [CycRing.gt_red, CycGal.gt_gsig]))

theorem tau_add (x y : CycRing.C K m) :
    tau K m d ((CycRing.cyc K m).add x y) = (CycRing.cyc K m).add (tau K m d x) (tau K m d y) := by
  show CycRing.qmap K m (CycGal.gsig K m d (gadd K m x.val y.val)) = _
  rw [CycGal.gsig_add, CycRing.qmap_add]
  rfl

include hcd in
theorem tau_mul (x y : CycRing.C K m) :
    tau K m d ((CycRing.cyc K m).mul x y) = (CycRing.cyc K m).mul (tau K m d x) (tau K m d y) := by
  show tau K m d (CycRing.qmap K m (gmul K m x.val y.val)) = _
  rw [tau_qmap, CycGal.gsig_mul K m c d hcd, CycRing.qmap_mul]
  rfl

include hcd in
theorem tau_one : tau K m d (CycRing.cyc K m).one = (CycRing.cyc K m).one := by
  show tau K m d (CycRing.qmap K m (gone K m)) = _
  rw [tau_qmap, CycGal.gsig_one K m c d hcd]
  rfl

include hcd in
/-- **τ_c(u) = u^c**. -/
theorem tau_u : tau K m d (CycUnit.u K m) = pw (CycRing.cyc K m) (CycUnit.u K m) c.val := by
  show tau K m d (CycRing.qmap K m (mono K m (ring m).one K.one)) = _
  rw [tau_qmap, CycGal.gsig_mono K m c d hcd, CycUnit.u_pow, CycUnit.nsm_mk]
  have e : (ring m).mul (ring m).one c = mk m c.val := by
    calc (ring m).mul (ring m).one c = c := by lib_ring
      _ = mk m c.val := Subtype.ext (md_small m c.val c.property).symm
  rw [e]

include hcd in
/-- **τ_c(φ_u(v)) = φ_(u^c)(v)**. -/
theorem tau_phz (v : V Int (m + 1)) :
    tau K m d (CycEis.phz (CycRing.cyc K m) m (CycUnit.u K m) v)
      = CycEis.phz (CycRing.cyc K m) m (pw (CycRing.cyc K m) (CycUnit.u K m) c.val) v := by
  rw [hom_phz (CycRing.cyc K m) (CycRing.cyc K m) (tau K m d) (tau_add K m d) (tau_mul K m c d hcd)
    (tau_one K m c d hcd), tau_u K m c d hcd]

end CycTau

#print axioms CycTau.hom_zero
#print axioms CycTau.hom_neg
#print axioms CycTau.hom_castN
#print axioms CycTau.hom_castZ
#print axioms CycTau.hom_pw
#print axioms CycTau.hom_sumK
#print axioms CycTau.hom_phz
#print axioms CycTau.tau_qmap
#print axioms CycTau.tau_add
#print axioms CycTau.tau_mul
#print axioms CycTau.tau_one
#print axioms CycTau.tau_u
#print axioms CycTau.tau_phz

lang/CycFrob.lean

/-
  lang/CycFrob — T49 (from A to α, step 3): **Frobenius on E = 𝔽_q[C_ℓ]/(N) is the Galois automorphism τ_q**:
    nsm_mul       n·a = a·(n·1) in 𝔽_ℓ
    gsig_sumK     σ_c(Σ v_i) = Σ σ_c(v_i)
    gsig_comp     σ(d) ∘ σ(d') = σ(d'·d)        (coefficient form, any coefficient ring)
    gsig_id       σ(1) = id
    frob_G        v^q = σ(d) v in 𝔽_q[C_ℓ]       (c·d = 1, c = q·1 in 𝔽_ℓ; Fermat x^q = x in 𝔽_q)
    frob_E        x^q = τ(d) x in E
    tau_comp, tau_id        τ(d) ∘ τ(d') = τ(d'·d), τ(1) = id
    frob_pow      x^(q^n) = τ(d^n) x
    frob_fix      q^f ≡ 1 (mod ℓ) ⇒ x^(q^f) = x for every x ∈ E
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycTau
import lib.RingTac
import lib.Semi

namespace CycFrob

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

theorem nsm_mul (m : Nat) (a : Zp m) : ∀ n, nsm (ring m) n a = (ring m).mul a (nsm (ring m) n (ring m).one)
  | 0 => by show (ring m).zero = (ring m).mul a (ring m).zero; lib_ring
  | n + 1 => by
    show (ring m).add (nsm (ring m) n a) a = (ring m).mul a ((ring m).add (nsm (ring m) n (ring m).one) (ring m).one)
    rw [nsm_mul m a n]
    lib_ring

variable {R : Type} (K : CRing R) (m : Nat)

theorem gsig_sumK (d : Zp m) {A : Type} (f : A → V R (m + 1)) : ∀ l : List A,
    CycGal.gsig K m d (sumK (gring K m) f l) = sumK (gring K m) (fun a => CycGal.gsig K m d (f a)) l
  | [] => CycGal.gsig_zero K m d
  | a :: l => by
    show CycGal.gsig K m d (gadd K m (f a) (sumK (gring K m) f l))
      = gadd K m (CycGal.gsig K m d (f a)) (sumK (gring K m) (fun a => CycGal.gsig K m d (f a)) l)
    rw [CycGal.gsig_add, gsig_sumK d f l]

theorem gsig_comp (d d' : Zp m) (u : V R (m + 1)) :
    CycGal.gsig K m d (CycGal.gsig K m d' u) = CycGal.gsig K m ((ring m).mul d' d) u :=
  ext K m _ _ (fun b => by
    rw [CycGal.gt_gsig, CycGal.gt_gsig, CycGal.gt_gsig]
    rw [show (ring m).mul ((ring m).mul b d) d' = (ring m).mul b ((ring m).mul d' d) by lib_ring])

theorem gsig_id (u : V R (m + 1)) : CycGal.gsig K m (ring m).one u = u :=
  ext K m _ _ (fun b => by
    rw [CycGal.gt_gsig, show (ring m).mul b (ring m).one = b by lib_ring])

theorem tau_comp (d d' : Zp m) (x : CycRing.C K m) :
    CycTau.tau K m d (CycTau.tau K m d' x) = CycTau.tau K m ((ring m).mul d' d) x := by
  show CycTau.tau K m d (CycRing.qmap K m (CycGal.gsig K m d' x.val)) = _
  rw [CycTau.tau_qmap, gsig_comp]
  rfl

theorem tau_id (x : CycRing.C K m) : CycTau.tau K m (ring m).one x = x := by
  apply CycRing.cext
  show CycRing.red K m (CycGal.gsig K m (ring m).one x.val) = x.val
  rw [gsig_id]
  exact CycRing.red_fix K m _ x.property

/-- **Frobenius in 𝔽_q[C_ℓ]: v^q = σ(d) v** (c·d = 1, c = q·1). -/
theorem frob_G (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (c d : Zp m)
    (hcd : (ring m).mul c d = (ring m).one) (hc : c = nsm (ring m) (k' + 1 + 1) (ring m).one)
    (v : V (Zp (k' + 1)) (m + 1)) :
    pw (gring (ring (k' + 1)) m) v (k' + 1 + 1) = CycGal.gsig (ring (k' + 1)) m d v := by
  have hv := decomp (ring (k' + 1)) m v
  have hL : pw (gring (ring (k' + 1)) m) v (k' + 1 + 1)
      = sumK (gring (ring (k' + 1)) m) (fun a => mono (ring (k' + 1)) m ((ring m).mul a c) (gt (ring (k' + 1)) m v a)) (L m) := by
    rw [hv]
    rw [frob_sum (ring (k' + 1)) m k' hq (ZMod.char_p (k' + 1))]
    rw [← hv]
    exact sumK_ext (gring (ring (k' + 1)) m) _ _ (fun a => by
      rw [mono_pow, ZMod.fermat k' hq, nsm_mul m a (k' + 1 + 1), ← hc]) (L m)
  have hR : CycGal.gsig (ring (k' + 1)) m d v
      = sumK (gring (ring (k' + 1)) m) (fun a => mono (ring (k' + 1)) m ((ring m).mul a c) (gt (ring (k' + 1)) m v a)) (L m) := by
    rw [hv, gsig_sumK]
    rw [← hv]
    exact sumK_ext (gring (ring (k' + 1)) m) _ _ (fun a => CycGal.gsig_mono (ring (k' + 1)) m c d hcd a _) (L m)
  rw [hL, hR]

/-- **Frobenius in E: x^q = τ(d) x**. -/
theorem frob_E (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (c d : Zp m)
    (hcd : (ring m).mul c d = (ring m).one) (hc : c = nsm (ring m) (k' + 1 + 1) (ring m).one)
    (x : CycRing.C (ring (k' + 1)) m) :
    pw (CycRing.cyc (ring (k' + 1)) m) x (k' + 1 + 1) = CycTau.tau (ring (k' + 1)) m d x := by
  have hx : x = CycRing.qmap (ring (k' + 1)) m x.val :=
    CycRing.cext _ m _ _ (CycRing.red_fix (ring (k' + 1)) m _ x.property).symm
  rw [hx, CycUnit.qmap_pw, frob_G m k' hq c d hcd hc, CycTau.tau_qmap]

/-- **x^(q^n) = τ(d^n) x**. -/
theorem frob_pow (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (c d : Zp m)
    (hcd : (ring m).mul c d = (ring m).one) (hc : c = nsm (ring m) (k' + 1 + 1) (ring m).one)
    (x : CycRing.C (ring (k' + 1)) m) : ∀ n,
    pw (CycRing.cyc (ring (k' + 1)) m) x ((k' + 1 + 1) ^ n) = CycTau.tau (ring (k' + 1)) m (pw (ring m) d n) x
  | 0 => by
    show pw (CycRing.cyc (ring (k' + 1)) m) x 1 = CycTau.tau (ring (k' + 1)) m (ring m).one x
    rw [tau_id]
    show (CycRing.cyc (ring (k' + 1)) m).mul (CycRing.cyc (ring (k' + 1)) m).one x = x
    exact (CycRing.cyc (ring (k' + 1)) m).one_mul x
  | n + 1 => by
    show pw (CycRing.cyc (ring (k' + 1)) m) x ((k' + 1 + 1) ^ n * (k' + 1 + 1))
      = CycTau.tau (ring (k' + 1)) m ((ring m).mul (pw (ring m) d n) d) x
    rw [← TwoPower.pwpow, frob_pow k' hq c d hcd hc x n, frob_E m k' hq c d hcd hc, tau_comp]

theorem one_pw {S : Type} (E : CRing S) : ∀ t, pw E E.one t = E.one
  | 0 => rfl
  | t + 1 => by
    show E.mul (pw E E.one t) E.one = E.one
    rw [one_pw E t]
    lib_ring

/-- **x^(q^f) = x** when q^f ≡ 1 (mod ℓ). -/
theorem frob_fix (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (c d : Zp m)
    (hcd : (ring m).mul c d = (ring m).one) (hc : c = nsm (ring m) (k' + 1 + 1) (ring m).one) (f : Nat)
    (hf : pw (ring m) c f = (ring m).one) (x : CycRing.C (ring (k' + 1)) m) :
    pw (CycRing.cyc (ring (k' + 1)) m) x ((k' + 1 + 1) ^ f) = x := by
  have hdf : pw (ring m) d f = (ring m).one := by
    calc pw (ring m) d f = (ring m).mul (pw (ring m) c f) (pw (ring m) d f) := by rw [hf]; lib_ring
      _ = pw (ring m) ((ring m).mul c d) f := (Frobenius.pw_mul (ring m) c d f).symm
      _ = (ring m).one := by rw [hcd]; exact one_pw (ring m) f
  rw [frob_pow m k' hq c d hcd hc x f, hdf, tau_id]

end CycFrob

#print axioms CycFrob.nsm_mul
#print axioms CycFrob.gsig_sumK
#print axioms CycFrob.gsig_comp
#print axioms CycFrob.gsig_id
#print axioms CycFrob.tau_comp
#print axioms CycFrob.tau_id
#print axioms CycFrob.frob_G
#print axioms CycFrob.frob_E
#print axioms CycFrob.frob_pow
#print axioms CycFrob.one_pw
#print axioms CycFrob.frob_fix

lang/SymG.lean

/-
  lang/SymG — T49 (from A to α, step 4): **the global symbol operator on E = R[C_ℓ]/(N)**
    G(W) := Π_(i<ℓ−1) τ(g^i)(W)^(g^i)          (g ∈ 𝔽_ℓ with g^(ℓ−1) = 1; τ(d): u ↦ u^(d^(−1)))
  For g a generator this is Π_(k ∈ 𝔽_ℓ^×) τ_k(W)^(k^(−1)): for W = (α/·)-type elements it multiplies the symbols at
  all primes above q with equal weights. Properties:
    prodL, prodL_head, prodL_ext, rot1, shiftP      finite products Π_(i<n) F(i), cyclic shift for N-periodic F
    prodL_mul, prodL_pw, prodL_const, pw_add
    cd_one       (g^i)^(ℓ−2)·g^i = 1
    tau_comp     τ(d)∘τ(d') = τ(d'·d)
    tau_pw       τ(d)(W^n) = τ(d)(W)^n
    G_mul        G(W W') = G(W) G(W')
    G_u          G(u^T) = (u^T)^(ℓ−1)
    G_tau        W^ℓ = 1 ⇒ G(τ(g^s) W) = G(W)^((g^s)^(−1))
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CycTau
import lib.RingTac
import lib.Semi

namespace SymG

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

section Prod

variable {S : Type} (E : CRing S)

theorem one_pw : ∀ t, pw E E.one t = E.one
  | 0 => rfl
  | t + 1 => by
    show E.mul (pw E E.one t) E.one = E.one
    rw [one_pw t]
    lib_ring

theorem pw3 (x : S) (a b c : Nat) : pw E (pw E (pw E x a) b) c = pw E (pw E x (a * c)) b := by
  rw [TwoPower.pwpow E (pw E x a) b c, TwoPower.pwpow E x a (b * c), TwoPower.pwpow E x (a * c) b]
  rw [show a * (b * c) = a * c * b by lib_nat]

/-- Π_(i<n) F(i). -/
def prodL (F : Nat → S) : Nat → S
  | 0 => E.one
  | n + 1 => E.mul (prodL F n) (F n)

theorem prodL_head (F : Nat → S) : ∀ n, prodL E F (n + 1) = E.mul (F 0) (prodL E (fun i => F (i + 1)) n)
  | 0 => by
    show E.mul E.one (F 0) = E.mul (F 0) E.one
    lib_ring
  | n + 1 => by
    show E.mul (prodL E F (n + 1)) (F (n + 1)) = E.mul (F 0) (E.mul (prodL E (fun i => F (i + 1)) n) (F (n + 1)))
    rw [prodL_head F n]
    lib_ring

theorem prodL_ext (F H : Nat → S) (h : ∀ i, F i = H i) : ∀ n, prodL E F n = prodL E H n
  | 0 => rfl
  | n + 1 => by
    show E.mul (prodL E F n) (F n) = E.mul (prodL E H n) (H n)
    rw [prodL_ext F H h n, h n]

theorem rot1 (F : Nat → S) : ∀ N, F N = F 0 → prodL E (fun i => F (i + 1)) N = prodL E F N
  | 0, _ => rfl
  | n + 1, hp => by
    rw [prodL_head E F n]
    show E.mul (prodL E (fun i => F (i + 1)) n) (F (n + 1)) = _
    rw [hp]
    lib_ring

/-- **cyclic shift**: F N-periodic ⇒ Π_(i<N) F(i + s) = Π_(i<N) F(i). -/
theorem shiftP (F : Nat → S) (N : Nat) (hper : ∀ i, F (i + N) = F i) : ∀ s, prodL E (fun i => F (i + s)) N = prodL E F N
  | 0 => rfl
  | s + 1 => by
    have h := rot1 E (fun i => F (i + s)) N (by
      show F (N + s) = F (0 + s)
      rw [show N + s = s + N by lib_nat, hper s, show 0 + s = s by lib_nat])
    have e : prodL E (fun i => F (i + (s + 1))) N = prodL E (fun i => (fun j => F (j + s)) (i + 1)) N :=
      prodL_ext E _ _ (fun i => by
        show F (i + (s + 1)) = F (i + 1 + s)
        rw [show i + (s + 1) = i + 1 + s by lib_nat]) N
    exact e.trans (h.trans (shiftP F N hper s))

theorem prodL_mul (F H : Nat → S) : ∀ n, prodL E (fun i => E.mul (F i) (H i)) n = E.mul (prodL E F n) (prodL E H n)
  | 0 => by
    show E.one = E.mul E.one E.one
    lib_ring
  | n + 1 => by
    show E.mul (prodL E (fun i => E.mul (F i) (H i)) n) (E.mul (F n) (H n))
      = E.mul (E.mul (prodL E F n) (F n)) (E.mul (prodL E H n) (H n))
    rw [prodL_mul F H n]
    lib_ring

theorem prodL_pw (F : Nat → S) (e : Nat) : ∀ n, prodL E (fun i => pw E (F i) e) n = pw E (prodL E F n) e
  | 0 => (one_pw E e).symm
  | n + 1 => by
    show E.mul (prodL E (fun i => pw E (F i) e) n) (pw E (F n) e) = pw E (E.mul (prodL E F n) (F n)) e
    rw [prodL_pw F e n, Frobenius.pw_mul]

theorem prodL_const (a : S) : ∀ n, prodL E (fun _ => a) n = pw E a n
  | 0 => rfl
  | n + 1 => by
    show E.mul (prodL E (fun _ => a) n) a = E.mul (pw E a n) a
    rw [prodL_const a n]

theorem pw_add (x : S) (a : Nat) : ∀ b, pw E x (a + b) = E.mul (pw E x a) (pw E x b)
  | 0 => by
    show pw E x a = E.mul (pw E x a) E.one
    lib_ring
  | b + 1 => by
    show E.mul (pw E x (a + b)) x = E.mul (pw E x a) (E.mul (pw E x b) x)
    rw [pw_add x a b]
    lib_ring

end Prod

section G

variable {R : Type} (K : CRing R) (n : Nat) (g : Zp (n + 1))
  (hg : pw (ring (n + 1)) g (n + 1) = (ring (n + 1)).one)

def dI (i : Nat) : Zp (n + 1) := pw (ring (n + 1)) g i
def cI (i : Nat) : Zp (n + 1) := pw (ring (n + 1)) (dI n g i) n

include hg in
theorem cd_one (i : Nat) : (ring (n + 1)).mul (cI n g i) (dI n g i) = (ring (n + 1)).one := by
  show pw (ring (n + 1)) (pw (ring (n + 1)) g i) (n + 1) = (ring (n + 1)).one
  rw [TwoPower.pwpow, show i * (n + 1) = (n + 1) * i by lib_nat, ← TwoPower.pwpow, hg]
  exact one_pw (ring (n + 1)) i

theorem dI_add (i s : Nat) : (ring (n + 1)).mul (dI n g s) (dI n g i) = dI n g (i + s) := by
  show (ring (n + 1)).mul (pw (ring (n + 1)) g s) (pw (ring (n + 1)) g i) = pw (ring (n + 1)) g (i + s)
  rw [pw_add (ring (n + 1)) g i s]
  lib_ring

include hg in
theorem dI_per (j : Nat) : dI n g (j + (n + 1)) = dI n g j := by
  show pw (ring (n + 1)) g (j + (n + 1)) = pw (ring (n + 1)) g j
  rw [pw_add (ring (n + 1)) g j (n + 1), hg]
  lib_ring

theorem tau_comp (d d' : Zp (n + 1)) (x : CycRing.C K (n + 1)) :
    CycTau.tau K (n + 1) d (CycTau.tau K (n + 1) d' x) = CycTau.tau K (n + 1) ((ring (n + 1)).mul d' d) x := by
  show CycTau.tau K (n + 1) d (CycRing.qmap K (n + 1) (CycGal.gsig K (n + 1) d' x.val)) = _
  rw [CycTau.tau_qmap]
  have e : CycGal.gsig K (n + 1) d (CycGal.gsig K (n + 1) d' x.val) = CycGal.gsig K (n + 1) ((ring (n + 1)).mul d' d) x.val :=
    ext K (n + 1) _ _ (fun b => by
      rw [CycGal.gt_gsig, CycGal.gt_gsig, CycGal.gt_gsig]
      rw [show (ring (n + 1)).mul ((ring (n + 1)).mul b d) d' = (ring (n + 1)).mul b ((ring (n + 1)).mul d' d) by lib_ring])
  rw [e]
  rfl

theorem tau_pw (c d : Zp (n + 1)) (hcd : (ring (n + 1)).mul c d = (ring (n + 1)).one) (W : CycRing.C K (n + 1)) (e : Nat) :
    CycTau.tau K (n + 1) d (pw (CycRing.cyc K (n + 1)) W e) = pw (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) d W) e :=
  CycTau.hom_pw (CycRing.cyc K (n + 1)) (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) d)
    (CycTau.tau_mul K (n + 1) c d hcd) (CycTau.tau_one K (n + 1) c d hcd) W e

/-- **the global symbol operator** G(W) = Π_(i<ℓ−1) τ(g^i)(W)^(g^i). -/
def G (W : CycRing.C K (n + 1)) : CycRing.C K (n + 1) :=
  prodL (CycRing.cyc K (n + 1)) (fun i => pw (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) (dI n g i) W) (dI n g i).val) (n + 1)

include hg in
/-- **G is multiplicative**. -/
theorem G_mul (W W' : CycRing.C K (n + 1)) :
    G K n g ((CycRing.cyc K (n + 1)).mul W W') = (CycRing.cyc K (n + 1)).mul (G K n g W) (G K n g W') := by
  unfold G
  rw [← prodL_mul]
  exact prodL_ext _ _ _ (fun i => by
    show pw (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) (dI n g i) ((CycRing.cyc K (n + 1)).mul W W')) (dI n g i).val = _
    rw [CycTau.tau_mul K (n + 1) (cI n g i) (dI n g i) (cd_one n g hg i), Frobenius.pw_mul]) _

include hg in
/-- **G(u^T) = (u^T)^(ℓ−1)**. -/
theorem G_u (T : Nat) :
    G K n g (pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) T)
      = pw (CycRing.cyc K (n + 1)) (pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) T) (n + 1) := by
  unfold G
  rw [← prodL_const (CycRing.cyc K (n + 1)) (pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) T) (n + 1)]
  exact prodL_ext _ _ _ (fun i => by
    have hcd := cd_one n g hg i
    rw [tau_pw K n (cI n g i) (dI n g i) hcd, CycTau.tau_u K (n + 1) (cI n g i) (dI n g i) hcd]
    rw [pw3]
    rw [← GEval.pw_md (CycRing.cyc K (n + 1)) (n + 1) _ (CycUnit.u_ell K (n + 1)) ((cI n g i).val * (dI n g i).val)]
    have e1 : md (n + 1) ((cI n g i).val * (dI n g i).val) = 1 := by
      have h := congrArg Subtype.val hcd
      exact h.trans (md_small (n + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ n)))
    rw [e1]
    show pw (CycRing.cyc K (n + 1)) ((CycRing.cyc K (n + 1)).mul (CycRing.cyc K (n + 1)).one (CycUnit.u K (n + 1))) T = _
    rw [(CycRing.cyc K (n + 1)).one_mul]) _

include hg in
/-- **G(τ(g^s) W) = G(W)^((g^s)^(−1))** when W^ℓ = 1. -/
theorem G_tau (s : Nat) (W : CycRing.C K (n + 1)) (hW : pw (CycRing.cyc K (n + 1)) W (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) :
    G K n g (CycTau.tau K (n + 1) (dI n g s) W) = pw (CycRing.cyc K (n + 1)) (G K n g W) (cI n g s).val := by
  have hX : ∀ j, pw (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) (dI n g j) W) (n + 1 + 1) = (CycRing.cyc K (n + 1)).one :=
    fun j => by
      rw [← tau_pw K n (cI n g j) (dI n g j) (cd_one n g hg j), hW]
      exact CycTau.tau_one K (n + 1) (cI n g j) (dI n g j) (cd_one n g hg j)
  let Kf : Nat → CycRing.C K (n + 1) := fun j =>
    pw (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) (dI n g j) W) ((dI n g j).val * (cI n g s).val)
  have hfac : ∀ i, pw (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) (dI n g i) (CycTau.tau K (n + 1) (dI n g s) W)) (dI n g i).val
      = Kf (i + s) := fun i => by
    show _ = pw (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) (dI n g (i + s)) W) ((dI n g (i + s)).val * (cI n g s).val)
    rw [tau_comp, dI_add n g i s]
    rw [← GEval.pw_md (CycRing.cyc K (n + 1)) (n + 1) _ (hX (i + s)) ((dI n g (i + s)).val * (cI n g s).val)]
    have e : (ring (n + 1)).mul (dI n g (i + s)) (cI n g s) = dI n g i := by
      rw [← dI_add n g i s]
      have h1 := cd_one n g hg s
      calc (ring (n + 1)).mul ((ring (n + 1)).mul (dI n g s) (dI n g i)) (cI n g s)
          = (ring (n + 1)).mul ((ring (n + 1)).mul (cI n g s) (dI n g s)) (dI n g i) := by lib_ring
        _ = (ring (n + 1)).mul (ring (n + 1)).one (dI n g i) := by rw [h1]
        _ = dI n g i := by lib_ring
    have e2 : md (n + 1) ((dI n g (i + s)).val * (cI n g s).val) = (dI n g i).val := congrArg Subtype.val e
    rw [e2]
  have hper : ∀ j, Kf (j + (n + 1)) = Kf j := fun j => by
    show pw (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) (dI n g (j + (n + 1))) W) ((dI n g (j + (n + 1))).val * (cI n g s).val)
      = pw (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) (dI n g j) W) ((dI n g j).val * (cI n g s).val)
    rw [dI_per n g hg j]
  unfold G
  rw [prodL_ext _ _ (fun i => Kf (i + s)) hfac (n + 1), shiftP _ Kf (n + 1) hper s, ← prodL_pw]
  exact prodL_ext _ _ _ (fun j => (TwoPower.pwpow (CycRing.cyc K (n + 1)) _ _ _).symm) _

end G

end SymG

#print axioms SymG.one_pw
#print axioms SymG.pw3
#print axioms SymG.prodL_head
#print axioms SymG.prodL_ext
#print axioms SymG.rot1
#print axioms SymG.shiftP
#print axioms SymG.prodL_mul
#print axioms SymG.prodL_pw
#print axioms SymG.prodL_const
#print axioms SymG.pw_add
#print axioms SymG.cd_one
#print axioms SymG.dI_add
#print axioms SymG.dI_per
#print axioms SymG.tau_comp
#print axioms SymG.tau_pw
#print axioms SymG.G_mul
#print axioms SymG.G_u
#print axioms SymG.G_tau

lang/SymY.lean

/-
  lang/SymY — T49 (from A to α, step 5): **the symbol value Y = φ_u(α)^M is an ℓ-th root of unity in E**
  (E = 𝔽_q[C_ℓ]/(N), q^f = ℓM + 1), once φ_u(α) is a unit:
    unit_pow      x v = 1, x^(LM+1) = x ⇒ (x^M)^L = 1
    iota_nsm      ι(n·1) = n·1
    nsm_inv       p·1 ≠ 0 in 𝔽_q ⇒ p·1 is a unit in E
    unit_of_mul   x·R = p·1 (p·1 ≠ 0 in 𝔽_q) ⇒ x is a unit in E
    Y_ell         x a unit in E ⇒ (x^M)^ℓ = 1                (Frobenius x^(q^f) = x, lang/CycFrob)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.CycFrob
import lang.NthEis
import lib.RingTac
import lib.Semi

namespace SymY

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

theorem unit_pow {S : Type} (E : CRing S) (x v : S) (hv : E.mul x v = E.one) (L M : Nat)
    (h : pw E x (L * M + 1) = x) : pw E (pw E x M) L = E.one := by
  have h1 : E.mul (pw E x (L * M)) x = x := h
  have h2 : pw E x (L * M) = E.one :=
    calc pw E x (L * M) = E.mul (pw E x (L * M)) (E.mul x v) := by rw [hv]; lib_ring
      _ = E.mul (E.mul (pw E x (L * M)) x) v := by lib_ring
      _ = E.mul x v := by rw [h1]
      _ = E.one := hv
  rw [TwoPower.pwpow, show M * L = L * M by lib_nat, h2]

variable {R : Type} (K : CRing R) (m : Nat)

theorem iota_nsm : ∀ n, CycUnit.iota K m (nsm K n K.one) = nsm (CycRing.cyc K m) n (CycRing.cyc K m).one
  | 0 => CycUnit.iota_zero K m
  | n + 1 => by
    show CycUnit.iota K m (K.add (nsm K n K.one) K.one) = (CycRing.cyc K m).add (nsm (CycRing.cyc K m) n (CycRing.cyc K m).one) (CycRing.cyc K m).one
    rw [CycUnit.iota_add, iota_nsm n, CycUnit.iota_one]

/-- **p·1 is a unit in E = 𝔽_q[C_ℓ]/(N)** when p ≢ 0 (mod q). -/
theorem nsm_inv (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (p : Nat)
    (hp : nsm (ring (k' + 1)) p (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) :
    (CycRing.cyc (ring (k' + 1)) m).mul (nsm (CycRing.cyc (ring (k' + 1)) m) p (CycRing.cyc (ring (k' + 1)) m).one)
      (CycUnit.iota (ring (k' + 1)) m (pw (ring (k' + 1)) (nsm (ring (k' + 1)) p (ring (k' + 1)).one) k'))
      = (CycRing.cyc (ring (k' + 1)) m).one := by
  rw [← iota_nsm, ← CycUnit.iota_mul]
  have hf := NthEis.fermat_unit k' hq _ hp
  have h2 : (ring (k' + 1)).mul (nsm (ring (k' + 1)) p (ring (k' + 1)).one)
      (pw (ring (k' + 1)) (nsm (ring (k' + 1)) p (ring (k' + 1)).one) k')
      = pw (ring (k' + 1)) (nsm (ring (k' + 1)) p (ring (k' + 1)).one) (k' + 1) := by
    show _ = (ring (k' + 1)).mul (pw (ring (k' + 1)) (nsm (ring (k' + 1)) p (ring (k' + 1)).one) k')
      (nsm (ring (k' + 1)) p (ring (k' + 1)).one)
    lib_ring
  have e : (ring (k' + 1)).mul (nsm (ring (k' + 1)) p (ring (k' + 1)).one)
      (pw (ring (k' + 1)) (nsm (ring (k' + 1)) p (ring (k' + 1)).one) k') = (ring (k' + 1)).one := h2.trans hf
  rw [e, CycUnit.iota_one]

/-- **x·R = p·1 ⇒ x is a unit**. -/
theorem unit_of_mul (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (p : Nat)
    (hp : nsm (ring (k' + 1)) p (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (x Rr : CycRing.C (ring (k' + 1)) m)
    (h : (CycRing.cyc (ring (k' + 1)) m).mul x Rr = nsm (CycRing.cyc (ring (k' + 1)) m) p (CycRing.cyc (ring (k' + 1)) m).one) :
    ∃ v, (CycRing.cyc (ring (k' + 1)) m).mul x v = (CycRing.cyc (ring (k' + 1)) m).one := by
  refine ⟨(CycRing.cyc (ring (k' + 1)) m).mul Rr
    (CycUnit.iota (ring (k' + 1)) m (pw (ring (k' + 1)) (nsm (ring (k' + 1)) p (ring (k' + 1)).one) k')), ?_⟩
  rw [← nsm_inv m k' hq p hp, ← h]
  lib_ring

/-- **Y = x^M satisfies Y^ℓ = 1** for a unit x of E (q^f = ℓM + 1). -/
theorem Y_ell (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (c d : Zp m)
    (hcd : (ring m).mul c d = (ring m).one) (hc : c = nsm (ring m) (k' + 1 + 1) (ring m).one) (f : Nat)
    (hf : pw (ring m) c f = (ring m).one) (M : Nat) (hqM : (k' + 1 + 1) ^ f = (m + 1) * M + 1)
    (x : CycRing.C (ring (k' + 1)) m) (hx : ∃ v, (CycRing.cyc (ring (k' + 1)) m).mul x v = (CycRing.cyc (ring (k' + 1)) m).one) :
    pw (CycRing.cyc (ring (k' + 1)) m) (pw (CycRing.cyc (ring (k' + 1)) m) x M) (m + 1) = (CycRing.cyc (ring (k' + 1)) m).one := by
  obtain ⟨v, hv⟩ := hx
  have h := CycFrob.frob_fix m k' hq c d hcd hc f hf x
  rw [hqM] at h
  exact unit_pow _ x v hv (m + 1) M h

end SymY

#print axioms SymY.unit_pow
#print axioms SymY.iota_nsm
#print axioms SymY.nsm_inv
#print axioms SymY.unit_of_mul
#print axioms SymY.Y_ell

lang/SymB1.lean

/-
  lang/SymB1 — T49 (from A to α, step 6): **the global symbol of the Stickelberger product B**.
  E = R[C_ℓ]/(N), ℓ = n + 2 prime, g a generator of 𝔽_ℓ^×, G the operator of lang/SymG, Y = φ_u(α)^M with Y^ℓ = 1.
  For B = Π_q σ(d_q)(α)^(a_q) σ(−d_q)(α)^(b_q) (StickBB.ents, all d_q ≠ 0):
    G_one, G_pw          G(1) = 1, G(W^e) = G(W)^e
    G_tau_ne             d ≠ 0, W^ℓ = 1 ⇒ G(τ(d) W) = G(W)^(d^(ℓ−2))
    phz_gsig_tau         d ≠ 0 ⇒ φ_u(σ(d) v) = τ(d)(φ_u(v))
    ed_ne                d ≠ 0 ⇒ −d ≠ 0
    expo                 Σ_q (d_q^(ℓ−2)·a_q + (−d_q)^(ℓ−2)·b_q)
    G_B                  G(φ_u(B)^M) = G(Y)^(expo)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.SymG
import lang.PhzPw
import lang.StickBB
import lang.CycGalEv
import lang.DLogF
import lang.NthEis
import lib.RingTac
import lib.Semi

namespace SymB1

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {R : Type} (K : CRing R) (n : Nat) (g : Zp (n + 1)) (hg : OrdMul.Ord (ring (n + 1)) g (n + 1))
  (hℓ : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1)

include hg in
theorem G_one : SymG.G K n g (CycRing.cyc K (n + 1)).one = (CycRing.cyc K (n + 1)).one := by
  have e : ∀ i, pw (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) (SymG.dI n g i) (CycRing.cyc K (n + 1)).one) (SymG.dI n g i).val
      = (CycRing.cyc K (n + 1)).one := fun i => by
    rw [CycTau.tau_one K (n + 1) (SymG.cI n g i) (SymG.dI n g i) (SymG.cd_one n g hg.1 i)]
    exact SymG.one_pw _ _
  unfold SymG.G
  rw [SymG.prodL_ext _ _ _ e (n + 1), SymG.prodL_const]
  exact SymG.one_pw _ _

include hg in
theorem G_pw (W : CycRing.C K (n + 1)) : ∀ e, SymG.G K n g (pw (CycRing.cyc K (n + 1)) W e) = pw (CycRing.cyc K (n + 1)) (SymG.G K n g W) e
  | 0 => G_one K n g hg
  | e + 1 => by
    show SymG.G K n g ((CycRing.cyc K (n + 1)).mul (pw (CycRing.cyc K (n + 1)) W e) W)
      = (CycRing.cyc K (n + 1)).mul (pw (CycRing.cyc K (n + 1)) (SymG.G K n g W) e) (SymG.G K n g W)
    rw [SymG.G_mul K n g hg.1, G_pw W e]

include hg hℓ in
/-- **G(τ(d) W) = G(W)^(d^(ℓ−2))** for every d ≠ 0 (W^ℓ = 1). -/
theorem G_tau_ne (d : Zp (n + 1)) (hd : d ≠ (ring (n + 1)).zero) (W : CycRing.C K (n + 1))
    (hW : pw (CycRing.cyc K (n + 1)) W (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) :
    SymG.G K n g (CycTau.tau K (n + 1) d W) = pw (CycRing.cyc K (n + 1)) (SymG.G K n g W) (pw (ring (n + 1)) d n).val := by
  obtain ⟨hs, _⟩ := DLogF.dlog_spec n hℓ g hg d hd
  have e : d = SymG.dI n g (DLogF.dlog n g d) := hs.symm
  rw [e]
  exact SymG.G_tau K n g hg.1 (DLogF.dlog n g d) W hW

include hℓ in
theorem inv_ne (d : Zp (n + 1)) (hd : d ≠ (ring (n + 1)).zero) :
    (ring (n + 1)).mul (pw (ring (n + 1)) d n) d = (ring (n + 1)).one :=
  NthEis.fermat_unit n hℓ d hd

include hℓ in
/-- **φ_u(σ(d) v) = τ(d)(φ_u(v))** for d ≠ 0. -/
theorem phz_gsig_tau (d : Zp (n + 1)) (hd : d ≠ (ring (n + 1)).zero) (v : V Int (n + 1 + 1)) :
    CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) (CycGal.gsig LibInt.intR (n + 1) d v)
      = CycTau.tau K (n + 1) d (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) v) := by
  rw [CycGalEv.phz_gsig (CycRing.cyc K (n + 1)) (n + 1) _ (CycUnit.u_ell K (n + 1)) _ d (inv_ne n hℓ d hd) v,
    CycTau.tau_phz K (n + 1) _ d (inv_ne n hℓ d hd) v]

theorem ed_ne (d : Zp (n + 1)) (hd : d ≠ (ring (n + 1)).zero) :
    (ring (n + 1)).mul (StickBB.e (n + 1)) d ≠ (ring (n + 1)).zero := fun h => hd (by
  calc d = (ring (n + 1)).mul ((ring (n + 1)).mul (StickBB.e (n + 1)) (StickBB.e (n + 1))) d := by
        rw [StickBB.e_sq]; lib_ring
    _ = (ring (n + 1)).mul (StickBB.e (n + 1)) ((ring (n + 1)).mul (StickBB.e (n + 1)) d) := by lib_ring
    _ = (ring (n + 1)).mul (StickBB.e (n + 1)) (ring (n + 1)).zero := by rw [h]
    _ = (ring (n + 1)).zero := by lib_ring)

/-- the exponent Σ_q (d_q^(ℓ−2)·a_q + (−d_q)^(ℓ−2)·b_q). -/
def expo : List (Zp (n + 1) × Nat × Nat) → Nat
  | [] => 0
  | q :: qs => (pw (ring (n + 1)) q.1 n).val * q.2.1
      + (pw (ring (n + 1)) ((ring (n + 1)).mul (StickBB.e (n + 1)) q.1) n).val * q.2.2 + expo qs

def NZ : List (Zp (n + 1) × Nat × Nat) → Prop
  | [] => True
  | q :: qs => q.1 ≠ (ring (n + 1)).zero ∧ NZ qs

theorem pw_pw_comm {S : Type} (E : CRing S) (x : S) (a b : Nat) : pw E (pw E x a) b = pw E (pw E x b) a := by
  rw [TwoPower.pwpow, TwoPower.pwpow, show a * b = b * a by lib_nat]

include hg hℓ in
/-- **G(φ_u(B)^M) = G(Y)^(expo)**, Y = φ_u(α)^M, Y^ℓ = 1. -/
theorem G_evB (α : V Int (n + 1 + 1)) (M : Nat)
    (hY : pw (CycRing.cyc K (n + 1)) (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) α) M) (n + 1 + 1)
      = (CycRing.cyc K (n + 1)).one) :
    ∀ qs, NZ n qs →
    SymG.G K n g (pw (CycRing.cyc K (n + 1)) (PhzPw.evBp (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) (StickBB.ents (n + 1) α qs)) M)
      = pw (CycRing.cyc K (n + 1))
          (SymG.G K n g (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) α) M)) (expo n qs)
  | [], _ => by
    show SymG.G K n g (pw (CycRing.cyc K (n + 1)) (CycRing.cyc K (n + 1)).one M) = (CycRing.cyc K (n + 1)).one
    rw [SymG.one_pw]
    exact G_one K n g hg
  | q :: qs, ⟨hq, hqs⟩ => by
    have ih := G_evB α M hY qs hqs
    show SymG.G K n g (pw (CycRing.cyc K (n + 1)) ((CycRing.cyc K (n + 1)).mul ((CycRing.cyc K (n + 1)).mul
        (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) (CycGal.gsig LibInt.intR (n + 1) q.1 α)) q.2.1)
        (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1))
          (CycGal.gsig LibInt.intR (n + 1) ((ring (n + 1)).mul (StickBB.e (n + 1)) q.1) α)) q.2.2))
        (PhzPw.evBp (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) (StickBB.ents (n + 1) α qs))) M)
      = pw (CycRing.cyc K (n + 1)) _ ((pw (ring (n + 1)) q.1 n).val * q.2.1
          + (pw (ring (n + 1)) ((ring (n + 1)).mul (StickBB.e (n + 1)) q.1) n).val * q.2.2 + expo n qs)
    rw [phz_gsig_tau K n hℓ q.1 hq, phz_gsig_tau K n hℓ _ (ed_ne n q.1 hq)]
    rw [Frobenius.pw_mul, Frobenius.pw_mul, SymG.G_mul K n g hg.1, SymG.G_mul K n g hg.1, ih]
    rw [pw_pw_comm _ (CycTau.tau K (n + 1) q.1 _) q.2.1 M, pw_pw_comm _ (CycTau.tau K (n + 1) _ _) q.2.2 M]
    rw [← SymG.tau_pw K n _ q.1 (inv_ne n hℓ q.1 hq), ← SymG.tau_pw K n _ _ (inv_ne n hℓ _ (ed_ne n q.1 hq))]
    rw [G_pw K n g hg, G_pw K n g hg]
    rw [G_tau_ne K n g hg hℓ q.1 hq _ hY, G_tau_ne K n g hg hℓ _ (ed_ne n q.1 hq) _ hY]
    rw [TwoPower.pwpow, TwoPower.pwpow, ← SymG.pw_add, ← SymG.pw_add]

end SymB1

#print axioms SymB1.G_one
#print axioms SymB1.G_pw
#print axioms SymB1.G_tau_ne
#print axioms SymB1.inv_ne
#print axioms SymB1.phz_gsig_tau
#print axioms SymB1.ed_ne
#print axioms SymB1.pw_pw_comm
#print axioms SymB1.G_evB

lang/SymB2.lean

/-
  lang/SymB2 — T49 (from A to α, step 7): **the exponent of the Stickelberger product is 1 mod ℓ**.
  For B = Π_(d=1..h) σ(d)(α)^(ℓ−1−d) σ(−d)(α)^(d−1) (StickQs.qsL, ℓ = n + 2 = 2h + 1):
    Σ_d (d^(−1)(ℓ−1−d) + (−d)^(−1)(d−1)) = Σ_d d^(−1)(ℓ − 2d) ≡ −2h ≡ 1 (mod ℓ).
    nu_add, nu_mul     (a + b)·1 = a·1 + b·1, (ab)·1 = (a·1)(b·1) in 𝔽_ℓ
    term               one entry (d = b + 1, a + b = ℓ − 2) contributes −2
    nz_qsL             every d in qsL is nonzero
    expo_qsL           (expo qsL)·1 = c·(−2) for c entries
    expo_one           2h = ℓ − 1 ⇒ (expo qsL)·1 = 1
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.SymB1
import lang.StickQs
import lib.RingTac
import lib.Semi

namespace SymB2

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing

variable (n : Nat)

theorem nu_add (a : Nat) : ∀ b, nsm (ring (n + 1)) (a + b) (ring (n + 1)).one
    = (ring (n + 1)).add (nsm (ring (n + 1)) a (ring (n + 1)).one) (nsm (ring (n + 1)) b (ring (n + 1)).one)
  | 0 => by
    show nsm (ring (n + 1)) a (ring (n + 1)).one = (ring (n + 1)).add (nsm (ring (n + 1)) a (ring (n + 1)).one) (ring (n + 1)).zero
    lib_ring
  | b + 1 => by
    show (ring (n + 1)).add (nsm (ring (n + 1)) (a + b) (ring (n + 1)).one) (ring (n + 1)).one
      = (ring (n + 1)).add (nsm (ring (n + 1)) a (ring (n + 1)).one)
          ((ring (n + 1)).add (nsm (ring (n + 1)) b (ring (n + 1)).one) (ring (n + 1)).one)
    rw [nu_add a b]
    lib_ring

theorem nu_mul (a : Nat) : ∀ b, nsm (ring (n + 1)) (a * b) (ring (n + 1)).one
    = (ring (n + 1)).mul (nsm (ring (n + 1)) a (ring (n + 1)).one) (nsm (ring (n + 1)) b (ring (n + 1)).one)
  | 0 => by
    show (ring (n + 1)).zero = (ring (n + 1)).mul (nsm (ring (n + 1)) a (ring (n + 1)).one) (ring (n + 1)).zero
    lib_ring
  | b + 1 => by
    show nsm (ring (n + 1)) (a * b + a) (ring (n + 1)).one
      = (ring (n + 1)).mul (nsm (ring (n + 1)) a (ring (n + 1)).one)
          ((ring (n + 1)).add (nsm (ring (n + 1)) b (ring (n + 1)).one) (ring (n + 1)).one)
    rw [nu_add n (a * b) a, nu_mul a b]
    lib_ring

/-- −2 in 𝔽_ℓ. -/
def m2 : Zp (n + 1) := (ring (n + 1)).neg ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).one)

/-- n·1 = −2 in 𝔽_(n+2). -/
theorem nu_n : nsm (ring (n + 1)) n (ring (n + 1)).one = m2 n := by
  have h := ZMod.char_p (n + 1)
  have e : nsm (ring (n + 1)) (n + 1 + 1) (ring (n + 1)).one
      = (ring (n + 1)).add (nsm (ring (n + 1)) n (ring (n + 1)).one)
          ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).one) := by
    show (ring (n + 1)).add ((ring (n + 1)).add (nsm (ring (n + 1)) n (ring (n + 1)).one) (ring (n + 1)).one) (ring (n + 1)).one = _
    lib_ring
  rw [e] at h
  show _ = (ring (n + 1)).neg ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).one)
  calc nsm (ring (n + 1)) n (ring (n + 1)).one
      = (ring (n + 1)).add ((ring (n + 1)).add (nsm (ring (n + 1)) n (ring (n + 1)).one)
          ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).one))
          ((ring (n + 1)).neg ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).one)) := by lib_ring
    _ = (ring (n + 1)).add (ring (n + 1)).zero
          ((ring (n + 1)).neg ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).one)) := by rw [h]
    _ = _ := by lib_ring

variable (hℓ : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1)

include hℓ in
/-- **one entry contributes −2**: d = b + 1 ≠ 0, a + b = ℓ − 2. -/
theorem term (a b : Nat) (hab : a + b = n) (d : Zp (n + 1)) (hd : d = mk (n + 1) (b + 1)) (hd0 : d ≠ (ring (n + 1)).zero) :
    nsm (ring (n + 1)) ((pw (ring (n + 1)) d n).val * a
        + (pw (ring (n + 1)) ((ring (n + 1)).mul (StickBB.e (n + 1)) d) n).val * b) (ring (n + 1)).one = m2 n := by
  have h1 := SymB1.inv_ne n hℓ d hd0
  have h2 := SymB1.inv_ne n hℓ _ (SymB1.ed_ne n d hd0)
  rw [nu_add, nu_mul, nu_mul, ZMod.every_elem, ZMod.every_elem]
  generalize pw (ring (n + 1)) d n = c1 at h1 ⊢
  generalize pw (ring (n + 1)) ((ring (n + 1)).mul (StickBB.e (n + 1)) d) n = c2 at h2 ⊢
  have hee := StickBB.e_sq (n + 1)
  have hc2 : c2 = (ring (n + 1)).mul c1 (StickBB.e (n + 1)) := by
    calc c2 = (ring (n + 1)).mul c2 ((ring (n + 1)).mul c1 d) := by rw [h1]; lib_ring
      _ = (ring (n + 1)).mul ((ring (n + 1)).mul c2 ((ring (n + 1)).mul (StickBB.e (n + 1)) d)) ((ring (n + 1)).mul c1 (StickBB.e (n + 1))) := by
          rw [show (ring (n + 1)).mul ((ring (n + 1)).mul c2 ((ring (n + 1)).mul (StickBB.e (n + 1)) d)) ((ring (n + 1)).mul c1 (StickBB.e (n + 1)))
              = (ring (n + 1)).mul ((ring (n + 1)).mul c2 ((ring (n + 1)).mul c1 d)) ((ring (n + 1)).mul (StickBB.e (n + 1)) (StickBB.e (n + 1))) by lib_ring, hee]
          lib_ring
      _ = (ring (n + 1)).mul c1 (StickBB.e (n + 1)) := by rw [h2]; lib_ring
  have ha : nsm (ring (n + 1)) a (ring (n + 1)).one
      = (ring (n + 1)).add (m2 n) ((ring (n + 1)).neg (nsm (ring (n + 1)) b (ring (n + 1)).one)) := by
    have h3 := nu_add n a b
    rw [hab, nu_n] at h3
    rw [h3]
    lib_ring
  have hdb : d = (ring (n + 1)).add (nsm (ring (n + 1)) b (ring (n + 1)).one) (ring (n + 1)).one := by
    rw [hd, ← CycUnit.nsm_mk]
    rfl
  rw [hc2, ha]
  rw [hdb] at h1
  unfold m2 at ⊢
  show _ = (ring (n + 1)).neg ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).one)
  unfold StickBB.e
  generalize nsm (ring (n + 1)) b (ring (n + 1)).one = B at h1 ⊢
  calc (ring (n + 1)).add ((ring (n + 1)).mul c1 ((ring (n + 1)).add ((ring (n + 1)).neg ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).one)) ((ring (n + 1)).neg B)))
        ((ring (n + 1)).mul ((ring (n + 1)).mul c1 ((ring (n + 1)).neg (ring (n + 1)).one)) B)
      = (ring (n + 1)).neg ((ring (n + 1)).add ((ring (n + 1)).mul c1 ((ring (n + 1)).add B (ring (n + 1)).one))
          ((ring (n + 1)).mul c1 ((ring (n + 1)).add B (ring (n + 1)).one))) := by lib_ring
    _ = (ring (n + 1)).neg ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).one) := by rw [h1]

theorem nz_qsL : ∀ c i j, c ≤ j → i + j = n → SymB1.NZ n (StickQs.qsL n i j c)
  | 0, _, _, _, _ => True.intro
  | c + 1, _, 0, hc, _ => absurd hc (Nat.not_succ_le_zero c)
  | c + 1, i, j' + 1, hc, hs => by
    refine ⟨fun h0 => ?_, nz_qsL c (i + 1) j' (Nat.le_of_succ_le_succ hc) ((show i + 1 + j' = i + (j' + 1) by lib_nat).trans hs)⟩
    have hi : i ≤ n := hs ▸ Nat.le_add_right i (j' + 1)
    have hv := congrArg Subtype.val h0
    have e1 : (mk (n + 1) (i + 1)).val = i + 1 := md_small (n + 1) (i + 1) (Nat.succ_le_succ (Nat.succ_le_succ hi))
    have e0 : ((ring (n + 1)).zero).val = 0 := md_small (n + 1) 0 (Nat.zero_lt_succ (n + 1))
    rw [e1, e0] at hv
    exact Nat.noConfusion hv

include hℓ in
/-- **(expo qsL)·1 = c·(−2)**. -/
theorem expo_qsL : ∀ c i j, c ≤ j → i + j = n →
    nsm (ring (n + 1)) (SymB1.expo n (StickQs.qsL n i j c)) (ring (n + 1)).one = nsm (ring (n + 1)) c (m2 n)
  | 0, _, _, _, _ => rfl
  | c + 1, _, 0, hc, _ => absurd hc (Nat.not_succ_le_zero c)
  | c + 1, i, j' + 1, hc, hs => by
    have hnz := nz_qsL n (c + 1) i (j' + 1) hc hs
    have ih := expo_qsL c (i + 1) j' (Nat.le_of_succ_le_succ hc) ((show i + 1 + j' = i + (j' + 1) by lib_nat).trans hs)
    show nsm (ring (n + 1)) ((pw (ring (n + 1)) (mk (n + 1) (i + 1)) n).val * (j' + 1)
        + (pw (ring (n + 1)) ((ring (n + 1)).mul (StickBB.e (n + 1)) (mk (n + 1) (i + 1))) n).val * i
        + SymB1.expo n (StickQs.qsL n (i + 1) j' c)) (ring (n + 1)).one
      = (ring (n + 1)).add (nsm (ring (n + 1)) c (m2 n)) (m2 n)
    rw [nu_add, ih, term n hℓ (j' + 1) i ((show j' + 1 + i = i + (j' + 1) by lib_nat).trans hs) _ rfl hnz.1]
    lib_ring

theorem nsm_scal (x : Zp (n + 1)) : ∀ k, nsm (ring (n + 1)) k x = (ring (n + 1)).mul (nsm (ring (n + 1)) k (ring (n + 1)).one) x
  | 0 => by
    show (ring (n + 1)).zero = (ring (n + 1)).mul (ring (n + 1)).zero x
    lib_ring
  | k + 1 => by
    show (ring (n + 1)).add (nsm (ring (n + 1)) k x) x
      = (ring (n + 1)).mul ((ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) (ring (n + 1)).one) x
    rw [nsm_scal x k]
    lib_ring

theorem h_le (h : Nat) (hh : n + 1 = h + h) : h ≤ n := by
  cases h with
  | zero => exact Nat.noConfusion hh
  | succ h0 =>
    have e : n = h0 + 1 + h0 := Nat.succ.inj (hh.trans (show h0 + 1 + (h0 + 1) = h0 + 1 + h0 + 1 by lib_nat))
    rw [e]
    exact Nat.le_add_right (h0 + 1) h0

include hℓ in
/-- **the Stickelberger exponent is 1 mod ℓ**: (expo qsL)·1 = 1 when ℓ − 1 = 2h. -/
theorem expo_one (h : Nat) (hh : n + 1 = h + h) :
    nsm (ring (n + 1)) (SymB1.expo n (StickQs.qsL n 0 n h)) (ring (n + 1)).one = (ring (n + 1)).one := by
  rw [expo_qsL n hℓ h 0 n (h_le n h hh) (Nat.zero_add n), nsm_scal]
  have e1 : nsm (ring (n + 1)) (h + h) (ring (n + 1)).one
      = (ring (n + 1)).add (nsm (ring (n + 1)) h (ring (n + 1)).one) (nsm (ring (n + 1)) h (ring (n + 1)).one) := nu_add n h h
  rw [← hh] at e1
  have e2 : nsm (ring (n + 1)) (n + 1) (ring (n + 1)).one = (ring (n + 1)).add (m2 n) (ring (n + 1)).one := by
    rw [← nu_n n]
    rfl
  rw [e2] at e1
  unfold m2 at e1 ⊢
  generalize nsm (ring (n + 1)) h (ring (n + 1)).one = H at e1 ⊢
  calc (ring (n + 1)).mul H ((ring (n + 1)).neg ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).one))
      = (ring (n + 1)).neg ((ring (n + 1)).add H H) := by lib_ring
    _ = (ring (n + 1)).neg ((ring (n + 1)).add ((ring (n + 1)).neg ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).one))
          (ring (n + 1)).one) := by rw [← e1]
    _ = (ring (n + 1)).one := by lib_ring

end SymB2

#print axioms SymB2.nu_add
#print axioms SymB2.nu_mul
#print axioms SymB2.nu_n
#print axioms SymB2.term
#print axioms SymB2.nz_qsL
#print axioms SymB2.expo_qsL
#print axioms SymB2.nsm_scal
#print axioms SymB2.h_le
#print axioms SymB2.expo_one

lang/EisAlpha.lean

/-
  lang/EisAlpha — T49 (general ℓ ≥ 5): **Eisenstein's reciprocity transported from A to a prime element α of
  norm p**, in global-symbol form.
  ℓ = ms + 4 and p = j + 3 primes, p ≡ 1 (mod ℓ), α ∈ ℤ[C_ℓ] with N(α) = p and α ∈ P_1, q = k' + 2 a prime with
  q^e = ℓM + 1, (q − 1) | M, M even; E = 𝔽_q[C_ℓ]/(N), u = [σ], g_ℓ a generator of 𝔽_ℓ^×,
  G(W) = Π_i τ(g_ℓ^i)(W)^(g_ℓ^i) (lang/SymG), Y = φ_u(α)^M. Then, with j₀ from A ≡ ±σ^(j₀) B (lang/StickInst),
    (u^(j₀ M))^(ℓ−1) · G(Y) = (u^T)^(ℓ−1),     T = Σ_c k_c (ℓ − 1 − c)          (eis_alpha)
  where u^(k_c) is the symbol of q^(−e) at P_c (EisRecA). For g_ℓ a generator, G(Y) is u^(f·s) with ζ^s = (α/q)_ℓ
  (product over all primes above q; checked numerically in work/data/t49/globsym_test.py), so for primary α
  (j₀ ≡ 0) this is (α/q)_ℓ expressed through the q-symbols at the primes P_c — Eisenstein's reciprocity for α.
    nsm_pow       (a^e)·1 = (a·1)^e in 𝔽_ℓ
    q_order       q^e = ℓM + 1 ⇒ (q·1)^e = 1 in 𝔽_ℓ
    G_eps         ε = ±1, M even ⇒ G((ε·1)^M) = 1
    G_Bsym        G(φ_u(B)^M) = G(Y)
    eis_alpha     the reciprocity relation above
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.SymB2
import lang.SymY
import lang.PhzModN
import lang.StickInst
import lang.EisRecV
import lib.RingTac
import lib.Semi
import lib.IntRing

namespace EisAlpha

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)

theorem nsm_pow (n a : Nat) : ∀ e, nsm (ring (n + 1)) (a ^ e) (ring (n + 1)).one = pw (ring (n + 1)) (nsm (ring (n + 1)) a (ring (n + 1)).one) e
  | 0 => by
    show nsm (ring (n + 1)) 1 (ring (n + 1)).one = (ring (n + 1)).one
    show (ring (n + 1)).add (ring (n + 1)).zero (ring (n + 1)).one = _
    lib_ring
  | e + 1 => by
    show nsm (ring (n + 1)) (a ^ e * a) (ring (n + 1)).one = (ring (n + 1)).mul (pw (ring (n + 1)) (nsm (ring (n + 1)) a (ring (n + 1)).one) e) (nsm (ring (n + 1)) a (ring (n + 1)).one)
    rw [SymB2.nu_mul, nsm_pow n a e]

/-- **(q·1)^e = 1 in 𝔽_ℓ** when q^e = ℓM + 1. -/
theorem q_order (n q e M : Nat) (h : q ^ e = (n + 1 + 1) * M + 1) :
    pw (ring (n + 1)) (nsm (ring (n + 1)) q (ring (n + 1)).one) e = (ring (n + 1)).one := by
  rw [← nsm_pow, h]
  show (ring (n + 1)).add (nsm (ring (n + 1)) ((n + 1 + 1) * M) (ring (n + 1)).one) (ring (n + 1)).one = _
  rw [SymB2.nu_mul, ZMod.char_p (n + 1)]
  lib_ring

/-- **φ_u(α) is a unit in E** when N(α) = p (p ≢ 0 mod q): the first factor of the norm list is σ(1)α = α. -/
theorem unit_alpha (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (n p : Nat)
    (hp : nsm (ring (k' + 1)) p (ring (k' + 1)).one ≠ (ring (k' + 1)).zero)
    (α : V Int (n + 1 + 1)) (iv : Zp (n + 1) → Zp (n + 1)) (h : Nat)
    (hN : NormEval.nrm (n + 1) α (BbarVal.nlist (n + 1) iv (StickQs.qsL n 0 n (h + 1)))
      = nsm (gring LibInt.intR (n + 1)) p (gring LibInt.intR (n + 1)).one) :
    ∃ v, (CycRing.cyc (ring (k' + 1)) (n + 1)).mul
        (CycEis.phz (CycRing.cyc (ring (k' + 1)) (n + 1)) (n + 1) (CycUnit.u (ring (k' + 1)) (n + 1)) α) v
        = (CycRing.cyc (ring (k' + 1)) (n + 1)).one := by
  obtain ⟨rest, hr⟩ : ∃ rest, NormEval.nrm (n + 1) α (BbarVal.nlist (n + 1) iv (StickQs.qsL n 0 n (h + 1)))
      = (gring LibInt.intR (n + 1)).mul (CycGal.gsig LibInt.intR (n + 1) (ring (n + 1)).one α) rest := ⟨_, rfl⟩
  rw [hr, CycFrob.gsig_id] at hN
  have h2 := congrArg (CycEis.phz (CycRing.cyc (ring (k' + 1)) (n + 1)) (n + 1) (CycUnit.u (ring (k' + 1)) (n + 1))) hN
  rw [CycEis.phz_mul _ (n + 1) _ (CycUnit.u_ell (ring (k' + 1)) (n + 1)), NormEval.phz_nsm] at h2
  exact SymY.unit_of_mul (n + 1) k' hq p hp _ _ h2

variable {R : Type} (K : CRing R) (n : Nat) (gl : Zp (n + 1)) (hgl : OrdMul.Ord (ring (n + 1)) gl (n + 1))

include hgl in
/-- **G((ε·1)^M) = 1** for ε = ±1 and M even. -/
theorem G_eps (ε : Int) (hε : ε = 1 ∨ ε = -1) (M' : Nat) :
    SymG.G K n gl (pw (CycRing.cyc K (n + 1)) (castZ (CycRing.cyc K (n + 1)) ε) (M' + M')) = (CycRing.cyc K (n + 1)).one := by
  have hsq : (CycRing.cyc K (n + 1)).mul (castZ (CycRing.cyc K (n + 1)) ε) (castZ (CycRing.cyc K (n + 1)) ε)
      = (CycRing.cyc K (n + 1)).one := by
    rw [← HasseDavenport.castZ_mul]
    have e1 : ε * ε = 1 := by
      cases hε with
      | inl h => rw [h]; rfl
      | inr h => rw [h]; rfl
    rw [e1]
    show (CycRing.cyc K (n + 1)).add (CycRing.cyc K (n + 1)).one (CycRing.cyc K (n + 1)).zero = _
    lib_ring
  rw [SymG.pw_add, ← Frobenius.pw_mul, hsq, SymG.one_pw]
  exact SymB1.G_one K n gl hgl

variable (hℓ : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1)

include hgl hℓ in
/-- **G(φ_u(B)^M) = G(Y)** for the Stickelberger product B (ℓ − 1 = 2h), Y = φ_u(α)^M, Y^ℓ = 1. -/
theorem G_Bsym (h : Nat) (hh : n + 1 = h + h) (α : V Int (n + 1 + 1)) (M : Nat)
    (hY : pw (CycRing.cyc K (n + 1)) (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) α) M) (n + 1 + 1)
      = (CycRing.cyc K (n + 1)).one) :
    SymG.G K n gl (pw (CycRing.cyc K (n + 1))
        (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1))
          (AbsPair.Bp (gring LibInt.intR (n + 1)) (StickBB.ents (n + 1) α (StickQs.qsL n 0 n h)))) M)
      = SymG.G K n gl (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) α) M) := by
  rw [PhzPw.phz_Bp (CycRing.cyc K (n + 1)) (n + 1) _ (CycUnit.u_ell K (n + 1))]
  rw [SymB1.G_evB K n gl hgl hℓ α M hY _ (SymB2.nz_qsL n h 0 n (SymB2.h_le n h hh) (Nat.zero_add n))]
  have hG : pw (CycRing.cyc K (n + 1)) (SymG.G K n gl (pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) α) M))
      (n + 1 + 1) = (CycRing.cyc K (n + 1)).one := by
    rw [← SymB1.G_pw K n gl hgl, hY]
    exact SymB1.G_one K n gl hgl
  rw [← GEval.pw_md (CycRing.cyc K (n + 1)) (n + 1) _ hG]
  have e1 : md (n + 1) (SymB1.expo n (StickQs.qsL n 0 n h)) = 1 :=
    (ZMod.nsm_one_val (n + 1) _).symm.trans ((congrArg Subtype.val (SymB2.expo_one n hℓ h hh)).trans
      (md_small (n + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ n))))
  rw [e1]
  show (CycRing.cyc K (n + 1)).mul (CycRing.cyc K (n + 1)).one _ = _
  exact (CycRing.cyc K (n + 1)).one_mul _

end EisAlpha

namespace EisAlpha

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)

/-- **Eisenstein's reciprocity for α (global-symbol form)**: A ≡ ±σ^(j₀)B (mod N) and φ_u(A)^M = u^T give
(u^(j₀M))^(ℓ−1)·G(φ_u(α)^M) = (u^T)^(ℓ−1) in E = 𝔽_q[C_ℓ]/(N). -/
theorem eis_alpha (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (M' : Nat) (hM' : M = M' + M') :
    ∃ jj : Zp (ms + 1 + 1 + 1),
      (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).mul
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (jj.val * M)) (ms + 1 + 1 + 1))
          (SymG.G (ring (k' + 1)) (ms + 1 + 1) gl
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
              (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
        = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
              (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1))) (ms + 1 + 1 + 1) := by
  obtain ⟨jj, ε, hε, hmod⟩ := StickInst.stick_inst j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0
  refine ⟨jj, ?_⟩
  have hU := unit_alpha k' hq (ms + 1 + 1) (j + 1 + 1 + 1) hp α iv h' hNα
  have hA := EisRecA.eis_recip_A j g hpr hg (ms + 1) t hℓ hN k' hq e hQ M hqn hp s hs k (EisRecV.vS (ms + 1 + 1)) hk
    (EisRecV.vS_spec (ms + 1 + 1))
  have hA' : pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
        (StickElem.A j g (ms + 1))) M
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
          (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1)) := hA
  have hAB := PhzModN.phz_A_B (ring (k' + 1)) (ms + 1 + 1) hℓ _ _ jj ε hmod
  -- Y^ℓ = 1
  have hcd : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
      = (ring (ms + 1 + 1 + 1)).one := by
    have hf := NthEis.fermat_unit (ms + 1 + 1) hℓ _ hc0
    have h2 : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
        (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
        = pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1 + 1) := by
      show _ = (ring (ms + 1 + 1 + 1)).mul (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
        (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      lib_ring
    exact h2.trans hf
  have hY := SymY.Y_ell (ms + 1 + 1 + 1) k' hq _ _ hcd rfl e (q_order (ms + 1 + 1) (k' + 1 + 1) e M hqn) M hqn _ hU
  -- the three factors
  have hC1 : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (castZ (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) ε) M) = (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one := by
    rw [hM']
    exact G_eps (ring (k' + 1)) (ms + 1 + 1) gl hgl ε hε M'
  have hU1 : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) jj.val) M)
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (jj.val * M)) (ms + 1 + 1 + 1) := by
    rw [TwoPower.pwpow]
    exact SymG.G_u (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 (jj.val * M)
  have hB1 := G_Bsym (ring (k' + 1)) (ms + 1 + 1) gl hgl hℓ (h' + 1) hh α M hY
  have hGA : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
        (StickElem.A j g (ms + 1))) M)
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
            (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1))) (ms + 1 + 1 + 1) := by
    rw [hA']
    exact SymG.G_u (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 _
  rw [hAB, Frobenius.pw_mul, Frobenius.pw_mul, SymG.G_mul (ring (k' + 1)) (ms + 1 + 1) gl hgl.1,
    SymG.G_mul (ring (k' + 1)) (ms + 1 + 1) gl hgl.1, hC1, hU1] at hGA
  have hB2 : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
        (StickElem.B (ms + 1) h' α)) M)
      = SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M) := hB1
  rw [hB2] at hGA
  rw [← hGA]
  lib_ring

end EisAlpha

#print axioms EisAlpha.nsm_pow
#print axioms EisAlpha.q_order
#print axioms EisAlpha.unit_alpha
#print axioms EisAlpha.G_eps
#print axioms EisAlpha.G_Bsym
#print axioms EisAlpha.eis_alpha

#print axioms: 79 theorems, no axioms.

78.384 Eisenstein reciprocity for a primary prime element of norm \(p\)

In \(\mathbb F_\ell [\pi ]/(\pi ^2)\) the primary element \(A\) maps to \(-1\) and \(B\) to a constant, so the root of unity in \(A=\pm \zeta ^jB\) is \(\pm 1\); the Stickelberger-weighted \(q\)-symbols collapse to the symbol at \(P_1=(\alpha )\).

\[ G\bigl (\varphi _u(\alpha )^M\bigr )=\bigl (u^{k_1}\bigr )^{\ell -1},\qquad u^{k_1}=\Bigl (\frac {q^{-e}}{\alpha }\Bigr )_{\ell }. \]

lang/DualL.lean

/-
  lang/DualL — T49 (primary elements, step 1): **dual numbers R[π]/(π²) over a commutative ring R**, and the
  ring maps ψ_c : (a, b) ↦ (a, c·b) (on R[ζ]/(1 − ζ)² this is σ_c, since (1 + π)^c = 1 + cπ).
    D, dext, dring       the ring structure on R × R, (a, b)(a', b') = (aa', ab' + ba')
    pi, pi_sq            π = (0, 1), π² = 0
    psi, psi_add, psi_mul, psi_one, psi_pi      ψ_c is a ring map with ψ_c(1 + π) = 1 + cπ
    snd_mul_zero         b = b' = 0 ⇒ the π-part of the product is 0
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import DetChain
import lib.RingTac

namespace DualL

open DetChain

variable {R : Type} (K : CRing R)

theorem dext {a b c d : R} (h1 : a = c) (h2 : b = d) : (a, b) = (c, d) := by rw [h1, h2]

def dadd (x y : R × R) : R × R := (K.add x.1 y.1, K.add x.2 y.2)
def dmul (x y : R × R) : R × R := (K.mul x.1 y.1, K.add (K.mul x.1 y.2) (K.mul x.2 y.1))
def dneg (x : R × R) : R × R := (K.neg x.1, K.neg x.2)

/-- **R[π]/(π²)**. -/
def dring : CRing (R × R) where
  add := dadd K
  mul := dmul K
  neg := dneg K
  zero := (K.zero, K.zero)
  one := (K.one, K.zero)
  add_assoc := fun a b c => by
    show (K.add (K.add a.1 b.1) c.1, K.add (K.add a.2 b.2) c.2) = (K.add a.1 (K.add b.1 c.1), K.add a.2 (K.add b.2 c.2))
    exact dext (by lib_ring) (by lib_ring)
  add_comm := fun a b => by
    show (K.add a.1 b.1, K.add a.2 b.2) = (K.add b.1 a.1, K.add b.2 a.2)
    exact dext (by lib_ring) (by lib_ring)
  zero_add := fun a => by
    show (K.add K.zero a.1, K.add K.zero a.2) = a
    exact dext (by lib_ring) (by lib_ring)
  add_neg := fun a => by
    show (K.add a.1 (K.neg a.1), K.add a.2 (K.neg a.2)) = (K.zero, K.zero)
    exact dext (by lib_ring) (by lib_ring)
  mul_assoc := fun a b c => by
    show (K.mul (K.mul a.1 b.1) c.1, K.add (K.mul (K.mul a.1 b.1) c.2) (K.mul (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) c.1))
      = (K.mul a.1 (K.mul b.1 c.1), K.add (K.mul a.1 (K.add (K.mul b.1 c.2) (K.mul b.2 c.1))) (K.mul a.2 (K.mul b.1 c.1)))
    exact dext (by lib_ring) (by lib_ring)
  mul_comm := fun a b => by
    show (K.mul a.1 b.1, K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) = (K.mul b.1 a.1, K.add (K.mul b.1 a.2) (K.mul b.2 a.1))
    exact dext (by lib_ring) (by lib_ring)
  one_mul := fun a => by
    show (K.mul K.one a.1, K.add (K.mul K.one a.2) (K.mul K.zero a.1)) = a
    exact dext (by lib_ring) (by lib_ring)
  distrib := fun a b c => by
    show (K.mul a.1 (K.add b.1 c.1), K.add (K.mul a.1 (K.add b.2 c.2)) (K.mul a.2 (K.add b.1 c.1)))
      = (K.add (K.mul a.1 b.1) (K.mul a.1 c.1),
          K.add (K.add (K.mul a.1 b.2) (K.mul a.2 b.1)) (K.add (K.mul a.1 c.2) (K.mul a.2 c.1)))
    exact dext (by lib_ring) (by lib_ring)

def pi : R × R := (K.zero, K.one)

theorem pi_sq : (dring K).mul (pi K) (pi K) = (dring K).zero := by
  show (K.mul K.zero K.zero, K.add (K.mul K.zero K.one) (K.mul K.one K.zero)) = (K.zero, K.zero)
  exact dext (by lib_ring) (by lib_ring)

/-- ψ_c(a, b) = (a, c·b). -/
def psi (c : R) (x : R × R) : R × R := (x.1, K.mul c x.2)

theorem psi_add (c : R) (x y : R × R) : psi K c ((dring K).add x y) = (dring K).add (psi K c x) (psi K c y) := by
  show (K.add x.1 y.1, K.mul c (K.add x.2 y.2)) = (K.add x.1 y.1, K.add (K.mul c x.2) (K.mul c y.2))
  exact dext rfl (by lib_ring)

theorem psi_mul (c : R) (x y : R × R) : psi K c ((dring K).mul x y) = (dring K).mul (psi K c x) (psi K c y) := by
  show (K.mul x.1 y.1, K.mul c (K.add (K.mul x.1 y.2) (K.mul x.2 y.1)))
    = (K.mul x.1 y.1, K.add (K.mul x.1 (K.mul c y.2)) (K.mul (K.mul c x.2) y.1))
  exact dext rfl (by lib_ring)

theorem psi_one (c : R) : psi K c (dring K).one = (dring K).one := by
  show (K.one, K.mul c K.zero) = (K.one, K.zero)
  exact dext rfl (by lib_ring)

theorem psi_pi (c : R) : psi K c ((dring K).add (dring K).one (pi K)) = (dring K).add (dring K).one ((dring K).mul (c, K.zero) (pi K)) := by
  show (K.add K.one K.zero, K.mul c (K.add K.zero K.one))
    = (K.add K.one (K.mul c K.zero), K.add K.zero (K.add (K.mul c K.one) (K.mul K.zero K.zero)))
  exact dext (by lib_ring) (by lib_ring)

theorem snd_mul_zero (x y : R × R) (hx : x.2 = K.zero) (hy : y.2 = K.zero) : ((dring K).mul x y).2 = K.zero := by
  show K.add (K.mul x.1 y.2) (K.mul x.2 y.1) = K.zero
  rw [hx, hy]
  lib_ring

end DualL

#print axioms DualL.dext
#print axioms DualL.dring
#print axioms DualL.pi_sq
#print axioms DualL.psi_add
#print axioms DualL.psi_mul
#print axioms DualL.psi_one
#print axioms DualL.psi_pi
#print axioms DualL.snd_mul_zero

lang/PrimAlpha1.lean

/-
  lang/PrimAlpha1 — T49 (primary elements, step 2): **evaluation at ζ = 1 + π in D = 𝔽_ℓ[π]/(π²)**.
  ℓ = n + 2 prime, F = 𝔽_ℓ, D = F[π]/(π²), z = 1 + π, φ₁ = φ_z : ℤ[C_ℓ] → D.
    nsm_D        k·1 = (k·1, 0) in D
    hl_D         ℓ·1 = 0 in D
    z_ell        z^ℓ = 1
    z_pw         z^k = (1, k·1)
    sum_rng      Σ_(i<k) z^i = (k·1, tri(k)·1)
    sum_z        Σ_b z^b = 0 (ℓ odd)
    psi_z        z^c = ψ_c(z)
    phz_psi      φ₁(σ(d) v) = ψ_c(φ₁(v))      (c·d = 1)
    Prim         v is primary :⟺ the π-part of φ₁(v) is 0  (Σ_i i·v(i) ≡ 0 mod ℓ)
    prim_gsig    v primary ⇒ σ(d) v primary with the same constant part
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.DualL
import lang.CycTau
import lang.CycGalEv
import lang.PrimJC
import lib.RingTac
import lib.Semi

namespace PrimAlpha1

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable (n : Nat)

/-- D = 𝔽_ℓ[π]/(π²). -/
abbrev D := DualL.dring (ring (n + 1))
/-- z = 1 + π. -/
def z : Zp (n + 1) × Zp (n + 1) := (D n).add (D n).one (DualL.pi (ring (n + 1)))

theorem nsm_D : ∀ k, nsm (D n) k (D n).one = (nsm (ring (n + 1)) k (ring (n + 1)).one, (ring (n + 1)).zero)
  | 0 => rfl
  | k + 1 => by
    rw [show nsm (D n) (k + 1) (D n).one = (D n).add (nsm (D n) k (D n).one) (D n).one from rfl, nsm_D k]
    show ((ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) (ring (n + 1)).one,
        (ring (n + 1)).add (ring (n + 1)).zero (ring (n + 1)).zero)
      = ((ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) (ring (n + 1)).one, (ring (n + 1)).zero)
    exact DualL.dext rfl (by lib_ring)

theorem hl_D : nsm (D n) (n + 1 + 1) (D n).one = (D n).zero := by
  rw [nsm_D, ZMod.char_p (n + 1)]
  rfl

theorem z_ell : pw (D n) (z n) (n + 1 + 1) = (D n).one :=
  CycPrim.z_pow (D n) (DualL.pi (ring (n + 1))) (DualL.pi_sq (ring (n + 1))) (n + 1) (hl_D n)

theorem z_pw : ∀ k, pw (D n) (z n) k = ((ring (n + 1)).one, nsm (ring (n + 1)) k (ring (n + 1)).one)
  | 0 => rfl
  | k + 1 => by
    show (D n).mul (pw (D n) (z n) k) (z n) = _
    rw [z_pw k]
    show ((ring (n + 1)).mul (ring (n + 1)).one ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).zero),
        (ring (n + 1)).add ((ring (n + 1)).mul (ring (n + 1)).one ((ring (n + 1)).add (ring (n + 1)).zero (ring (n + 1)).one))
          ((ring (n + 1)).mul (nsm (ring (n + 1)) k (ring (n + 1)).one) ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).zero)))
      = ((ring (n + 1)).one, (ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) (ring (n + 1)).one)
    exact DualL.dext (by lib_ring) (by lib_ring)

theorem nu_add (a : Nat) : ∀ b, nsm (ring (n + 1)) (a + b) (ring (n + 1)).one
    = (ring (n + 1)).add (nsm (ring (n + 1)) a (ring (n + 1)).one) (nsm (ring (n + 1)) b (ring (n + 1)).one)
  | 0 => by
    show nsm (ring (n + 1)) a (ring (n + 1)).one = (ring (n + 1)).add (nsm (ring (n + 1)) a (ring (n + 1)).one) (ring (n + 1)).zero
    lib_ring
  | b + 1 => by
    show (ring (n + 1)).add (nsm (ring (n + 1)) (a + b) (ring (n + 1)).one) (ring (n + 1)).one
      = (ring (n + 1)).add (nsm (ring (n + 1)) a (ring (n + 1)).one)
          ((ring (n + 1)).add (nsm (ring (n + 1)) b (ring (n + 1)).one) (ring (n + 1)).one)
    rw [nu_add a b]
    lib_ring

theorem nu_mul (a : Nat) : ∀ b, nsm (ring (n + 1)) (a * b) (ring (n + 1)).one
    = (ring (n + 1)).mul (nsm (ring (n + 1)) a (ring (n + 1)).one) (nsm (ring (n + 1)) b (ring (n + 1)).one)
  | 0 => by
    show (ring (n + 1)).zero = (ring (n + 1)).mul (nsm (ring (n + 1)) a (ring (n + 1)).one) (ring (n + 1)).zero
    lib_ring
  | b + 1 => by
    show nsm (ring (n + 1)) (a * b + a) (ring (n + 1)).one
      = (ring (n + 1)).mul (nsm (ring (n + 1)) a (ring (n + 1)).one)
          ((ring (n + 1)).add (nsm (ring (n + 1)) b (ring (n + 1)).one) (ring (n + 1)).one)
    rw [nu_add n (a * b) a, nu_mul a b]
    lib_ring

theorem sum_rng : ∀ k, sumK (D n) (fun i => pw (D n) (z n) i) (CycUnit.rng k)
    = (nsm (ring (n + 1)) k (ring (n + 1)).one, nsm (ring (n + 1)) (PrimJC.tri k) (ring (n + 1)).one)
  | 0 => rfl
  | k + 1 => by
    show sumK (D n) (fun i => pw (D n) (z n) i) (CycUnit.rng k ++ [k]) = _
    rw [sumK_append, sum_rng k]
    show (D n).add _ ((D n).add (pw (D n) (z n) k) (D n).zero) = _
    rw [z_pw]
    show ((ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).zero),
        (ring (n + 1)).add (nsm (ring (n + 1)) (PrimJC.tri k) (ring (n + 1)).one)
          ((ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) (ring (n + 1)).zero))
      = ((ring (n + 1)).add (nsm (ring (n + 1)) k (ring (n + 1)).one) (ring (n + 1)).one,
          nsm (ring (n + 1)) (PrimJC.tri k + k) (ring (n + 1)).one)
    rw [nu_add n (PrimJC.tri k) k]
    exact DualL.dext (by lib_ring) (by lib_ring)

/-- **Σ_b z^b = 0** in D (ℓ = 2h + 1). -/
theorem sum_z (h : Nat) (hh : n + 1 = h + h) :
    sumK (D n) (fun b : Zp (n + 1) => pw (D n) (z n) b.val) (L (n + 1)) = (D n).zero := by
  show sumK (D n) (fun b : Zp (n + 1) => pw (D n) (z n) b.val) (CMInert.allZ (n + 1) (n + 1 + 1)) = _
  rw [CycUnit.sum_allZ (n + 1) (D n) _ (n + 1 + 1)]
  have e1 : sumK (D n) (fun i => pw (D n) (z n) (md (n + 1) i)) (CycUnit.rng (n + 1 + 1))
      = sumK (D n) (fun i => pw (D n) (z n) i) (CycUnit.rng (n + 1 + 1)) :=
    sumK_ext (D n) _ _ (fun i => GEval.pw_md (D n) (n + 1) _ (z_ell n) i) _
  refine Eq.trans e1 ?_
  rw [sum_rng]
  have ht : PrimJC.tri (n + 1 + 1) = (n + 1 + 1) * h :=
    PrimJC.double_inj _ _ ((PrimJC.two_tri (n + 1)).trans (by rw [hh]; lib_nat))
  rw [ht, nu_mul, ZMod.char_p (n + 1)]
  exact DualL.dext rfl (by lib_ring)

/-- **z^c = ψ_c(z)**. -/
theorem psi_z (c : Zp (n + 1)) : pw (D n) (z n) c.val = DualL.psi (ring (n + 1)) c (z n) := by
  rw [z_pw, ZMod.every_elem]
  show _ = ((ring (n + 1)).add (ring (n + 1)).one (ring (n + 1)).zero,
      (ring (n + 1)).mul c ((ring (n + 1)).add (ring (n + 1)).zero (ring (n + 1)).one))
  exact DualL.dext (by lib_ring) (by lib_ring)

/-- **φ₁(σ(d) v) = ψ_c(φ₁(v))** for c·d = 1. -/
theorem phz_psi (c d : Zp (n + 1)) (hcd : (ring (n + 1)).mul c d = (ring (n + 1)).one) (v : V Int (n + 1 + 1)) :
    CycEis.phz (D n) (n + 1) (z n) (CycGal.gsig LibInt.intR (n + 1) d v)
      = DualL.psi (ring (n + 1)) c (CycEis.phz (D n) (n + 1) (z n) v) := by
  rw [CycGalEv.phz_gsig (D n) (n + 1) (z n) (z_ell n) c d hcd v, psi_z]
  exact (CycTau.hom_phz (D n) (D n) (DualL.psi (ring (n + 1)) c) (DualL.psi_add (ring (n + 1)) c)
    (DualL.psi_mul (ring (n + 1)) c) (DualL.psi_one (ring (n + 1)) c) (n + 1) (z n) v).symm

/-- v is primary: the π-part of φ₁(v) vanishes. -/
def Prim (v : V Int (n + 1 + 1)) : Prop := (CycEis.phz (D n) (n + 1) (z n) v).2 = (ring (n + 1)).zero

theorem prim_gsig (c d : Zp (n + 1)) (hcd : (ring (n + 1)).mul c d = (ring (n + 1)).one) (v : V Int (n + 1 + 1))
    (hv : Prim n v) :
    Prim n (CycGal.gsig LibInt.intR (n + 1) d v) ∧
      (CycEis.phz (D n) (n + 1) (z n) (CycGal.gsig LibInt.intR (n + 1) d v)).1 = (CycEis.phz (D n) (n + 1) (z n) v).1 := by
  unfold Prim at hv ⊢
  rw [phz_psi n c d hcd v]
  refine ⟨?_, rfl⟩
  show (ring (n + 1)).mul c (CycEis.phz (D n) (n + 1) (z n) v).2 = _
  rw [hv]
  lib_ring

end PrimAlpha1

#print axioms PrimAlpha1.nsm_D
#print axioms PrimAlpha1.hl_D
#print axioms PrimAlpha1.z_ell
#print axioms PrimAlpha1.z_pw
#print axioms PrimAlpha1.nu_add
#print axioms PrimAlpha1.nu_mul
#print axioms PrimAlpha1.sum_rng
#print axioms PrimAlpha1.sum_z
#print axioms PrimAlpha1.psi_z
#print axioms PrimAlpha1.phz_psi
#print axioms PrimAlpha1.prim_gsig

lang/PrimAlpha2.lean

/-
  lang/PrimAlpha2 — T49 (primary elements, step 3): **for primary α the root of unity in A ≡ ±σ^(j₀)B is ±1**.
  In D = 𝔽_ℓ[π]/(π²) with z = 1 + π: φ₁(A) = −1 (A primary, p ≡ 1 mod ℓ), φ₁(B) has π-part 0 when α is primary,
  and φ₁ kills N; comparing π-parts of φ₁(A) = ε·(1 + j₀π)·φ₁(B) gives j₀ = 0.
    castN_D, castZ_snd     integers map to D with π-part 0
    snd_pw                 π-part 0 is stable under powers
    B_snd                  α primary, all d ≠ 0 ⇒ φ₁(B) has π-part 0
    pp_one                 p ≡ 1 (mod ℓ) ⇒ p·1 = 1 in D
    A_val                  φ₁(A) = −1
    j0_zero                A ≡ ε σ^(j₀) B (mod N) ⇒ j₀ = 0
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.PrimAlpha1
import lang.PhzModN
import lang.SymB1
import lib.RingTac
import lib.Semi

namespace PrimAlpha2

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)

variable (n : Nat)

theorem castN_D : ∀ k, castN (PrimAlpha1.D n) k = (castN (ring (n + 1)) k, (ring (n + 1)).zero)
  | 0 => rfl
  | k + 1 => by
    show (PrimAlpha1.D n).add (PrimAlpha1.D n).one (castN (PrimAlpha1.D n) k) = _
    rw [castN_D k]
    show ((ring (n + 1)).add (ring (n + 1)).one (castN (ring (n + 1)) k), (ring (n + 1)).add (ring (n + 1)).zero (ring (n + 1)).zero)
      = ((ring (n + 1)).add (ring (n + 1)).one (castN (ring (n + 1)) k), (ring (n + 1)).zero)
    exact DualL.dext rfl (by lib_ring)

theorem castZ_snd : ∀ ε : Int, (castZ (PrimAlpha1.D n) ε).2 = (ring (n + 1)).zero
  | .ofNat k => by
    show (castN (PrimAlpha1.D n) k).2 = _
    rw [castN_D]
  | .negSucc k => by
    show ((PrimAlpha1.D n).neg (castN (PrimAlpha1.D n) (k + 1))).2 = _
    rw [castN_D]
    show (ring (n + 1)).neg (ring (n + 1)).zero = (ring (n + 1)).zero
    lib_ring

theorem snd_pw (x : Zp (n + 1) × Zp (n + 1)) (hx : x.2 = (ring (n + 1)).zero) :
    ∀ e, (pw (PrimAlpha1.D n) x e).2 = (ring (n + 1)).zero
  | 0 => rfl
  | e + 1 => DualL.snd_mul_zero (ring (n + 1)) _ _ (snd_pw x hx e) hx

variable (hℓ : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1)

include hℓ in
/-- **φ₁(B) has π-part 0** for primary α. -/
theorem B_snd (α : V Int (n + 1 + 1)) (hα : PrimAlpha1.Prim n α) : ∀ qs, SymB1.NZ n qs →
    (PhzPw.evBp (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) (StickBB.ents (n + 1) α qs)).2 = (ring (n + 1)).zero
  | [], _ => rfl
  | q :: qs, ⟨hq, hqs⟩ => by
    have h1 := (PrimAlpha1.prim_gsig n _ q.1 (SymB1.inv_ne n hℓ q.1 hq) α hα).1
    have h2 := (PrimAlpha1.prim_gsig n _ _ (SymB1.inv_ne n hℓ _ (SymB1.ed_ne n q.1 hq)) α hα).1
    exact DualL.snd_mul_zero (ring (n + 1)) _ _
      (DualL.snd_mul_zero (ring (n + 1)) _ _ (snd_pw n _ h1 q.2.1) (snd_pw n _ h2 q.2.2)) (B_snd α hα qs hqs)

/-- **p·1 = 1 in D** when p = ℓ·(2h) + 1. -/
theorem pp_one (j h : Nat) (hN : j + 1 + 1 = (n + 1 + 1) * (2 * h)) :
    CharSumGen.Pp (PrimAlpha1.D n) j = (PrimAlpha1.D n).one := by
  rw [NthEis.pp_nsm, PrimAlpha1.nsm_D]
  show (nsm (ring (n + 1)) (j + 1 + 1 + 1) (ring (n + 1)).one, (ring (n + 1)).zero) = ((ring (n + 1)).one, (ring (n + 1)).zero)
  rw [show j + 1 + 1 + 1 = (n + 1 + 1) * (2 * h) + 1 by rw [← hN]]
  show ((ring (n + 1)).add (nsm (ring (n + 1)) ((n + 1 + 1) * (2 * h)) (ring (n + 1)).one) (ring (n + 1)).one, (ring (n + 1)).zero) = _
  rw [PrimAlpha1.nu_mul, ZMod.char_p (n + 1)]
  exact DualL.dext (by lib_ring) rfl

/-- **φ₁(A) = −1** (A of odd degree k = 2r + 1, p − 1 = ℓ·2h). -/
theorem A_val (j : Nat) (g : Zp (j + 1 + 1)) (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1)) (h : Nat) (hN : j + 1 + 1 = (n + 1 + 1) * (2 * h))
    (k r : Nat) (hk : k = 2 * r + 1) :
    CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n)
        (EisBridge.AA (gring LibInt.intR (n + 1)) j (CycPrim.chi (n + 1) j g) k) = (PrimAlpha1.D n).neg (PrimAlpha1.D n).one := by
  subst hk
  have e := CycPrim.cyc_primary (PrimAlpha1.D n) (DualL.pi (ring (n + 1))) (DualL.pi_sq (ring (n + 1))) (n + 1) j g r
    (PrimAlpha1.hl_D n) hpr hg h hN
  rw [pp_one n j h hN] at e
  rw [← e]
  show _ = (PrimAlpha1.D n).mul _ (PrimAlpha1.D n).one
  exact (((PrimAlpha1.D n).mul_comm _ _).trans ((PrimAlpha1.D n).one_mul _)).symm

/-- **j₀ = 0 for primary α**: φ₁(A) = −1, A ≡ ε σ^(j₀) B (mod N), φ₁(B) with π-part 0 ⇒ j₀ = 0. -/
theorem j0_zero (h : Nat) (hh : n + 1 = h + h) (A B : V Int (n + 1 + 1))
    (hA : CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) A = (PrimAlpha1.D n).neg (PrimAlpha1.D n).one)
    (hB : (CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) B).2 = (ring (n + 1)).zero)
    (jj : Zp (n + 1)) (ε : Int)
    (hmod : ModN.ModN (n + 1) A (gmul LibInt.intR (n + 1) (mono LibInt.intR (n + 1) jj ε) B)) :
    jj = (ring (n + 1)).zero := by
  have e := PhzModN.phz_modn (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) (PrimAlpha1.sum_z n h hh) _ _ hmod
  rw [hA] at e
  have e2 : CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) (gmul LibInt.intR (n + 1) (mono LibInt.intR (n + 1) jj ε) B)
      = (PrimAlpha1.D n).mul ((PrimAlpha1.D n).mul (castZ (PrimAlpha1.D n) ε) ((ring (n + 1)).one, jj))
          (CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) B) := by
    show CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) ((gring LibInt.intR (n + 1)).mul (mono LibInt.intR (n + 1) jj ε) B) = _
    rw [CycEis.phz_mul (PrimAlpha1.D n) (n + 1) _ (PrimAlpha1.z_ell n), PhzModN.phz_mono, PrimAlpha1.z_pw, ZMod.every_elem]
  rw [e2] at e
  have hX := castZ_snd n ε
  generalize castZ (PrimAlpha1.D n) ε = X at e hX
  generalize CycEis.phz (PrimAlpha1.D n) (n + 1) (PrimAlpha1.z n) B = Y at e hB
  have hf := congrArg Prod.fst e
  have hs := congrArg Prod.snd e
  change (ring (n + 1)).neg (ring (n + 1)).one
      = (ring (n + 1)).mul ((ring (n + 1)).mul X.1 (ring (n + 1)).one) Y.1 at hf
  change (ring (n + 1)).neg (ring (n + 1)).zero
      = (ring (n + 1)).add ((ring (n + 1)).mul ((ring (n + 1)).mul X.1 (ring (n + 1)).one) Y.2)
          ((ring (n + 1)).mul ((ring (n + 1)).add ((ring (n + 1)).mul X.1 jj) ((ring (n + 1)).mul X.2 (ring (n + 1)).one)) Y.1) at hs
  rw [hX, hB] at hs
  calc jj = (ring (n + 1)).neg ((ring (n + 1)).mul jj ((ring (n + 1)).neg (ring (n + 1)).one)) := by lib_ring
    _ = (ring (n + 1)).neg ((ring (n + 1)).mul jj ((ring (n + 1)).mul ((ring (n + 1)).mul X.1 (ring (n + 1)).one) Y.1)) := by
        rw [← hf]
    _ = (ring (n + 1)).neg ((ring (n + 1)).add ((ring (n + 1)).mul ((ring (n + 1)).mul X.1 (ring (n + 1)).one) (ring (n + 1)).zero)
          ((ring (n + 1)).mul ((ring (n + 1)).add ((ring (n + 1)).mul X.1 jj) ((ring (n + 1)).mul (ring (n + 1)).zero (ring (n + 1)).one)) Y.1)) := by
        lib_ring
    _ = (ring (n + 1)).neg ((ring (n + 1)).neg (ring (n + 1)).zero) := by rw [← hs]
    _ = (ring (n + 1)).zero := by lib_ring

end PrimAlpha2

#print axioms PrimAlpha2.castN_D
#print axioms PrimAlpha2.castZ_snd
#print axioms PrimAlpha2.snd_pw
#print axioms PrimAlpha2.B_snd
#print axioms PrimAlpha2.pp_one
#print axioms PrimAlpha2.A_val
#print axioms PrimAlpha2.j0_zero

lang/EisAlphaP.lean

/-
  lang/EisAlphaP — T49 (general ℓ ≥ 5): **Eisenstein's reciprocity for a primary prime element α of norm p**.
  For α primary (α ≡ rational integer mod (1 − ζ)², i.e. Σ_i i·α(i) ≡ 0 mod ℓ), the root of unity in A ≡ ±σ^(j₀)B is ±1
  (lang/PrimAlpha2), so the relation of lang/EisAlpha becomes
    G(φ_u(α)^M) = (u^T)^(ℓ−1),   T = Σ_c k_c (ℓ − 1 − c)        in E = 𝔽_q[C_ℓ]/(N),
  where u^(k_c) is the symbol of q^(−e) at P_c and G is the global-symbol operator (lang/SymG).
    eis_alpha_of     the relation of EisAlpha for a given congruence A ≡ ε σ^(j₀) B (mod N)
    eis_alpha_prim   the primary case: j₀ = 0
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.EisAlpha
import lang.PrimAlpha2
import lib.RingTac
import lib.Semi

namespace EisAlphaP

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castZ castN)
open EisAlpha

/-- **the reciprocity relation from a given congruence A ≡ ε σ^(j₀) B (mod N)**. -/
theorem eis_alpha_of (j ms t h' : Nat)
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (M' : Nat) (hM' : M = M' + M') (jj : Zp (ms + 1 + 1 + 1)) (ε : Int) (hε : ε = 1 ∨ ε = -1)
    (hmod : ModN.ModN (ms + 1 + 1 + 1) (StickElem.A j g (ms + 1))
      (gmul LibInt.intR (ms + 1 + 1 + 1) (mono LibInt.intR (ms + 1 + 1 + 1) jj ε) (StickElem.B (ms + 1) h' α))) :
      (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).mul
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (jj.val * M)) (ms + 1 + 1 + 1))
          (SymG.G (ring (k' + 1)) (ms + 1 + 1) gl
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
              (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
        = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
              (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1))) (ms + 1 + 1 + 1) := by
  have hU := unit_alpha k' hq (ms + 1 + 1) (j + 1 + 1 + 1) hp α iv h' hNα
  have hA := EisRecA.eis_recip_A j g hpr hg (ms + 1) t hℓ hN k' hq e hQ M hqn hp s hs k (EisRecV.vS (ms + 1 + 1)) hk
    (EisRecV.vS_spec (ms + 1 + 1))
  have hA' : pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
        (StickElem.A j g (ms + 1))) M
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
          (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1)) := hA
  have hAB := PhzModN.phz_A_B (ring (k' + 1)) (ms + 1 + 1) hℓ _ _ jj ε hmod
  -- Y^ℓ = 1
  have hcd : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
      = (ring (ms + 1 + 1 + 1)).one := by
    have hf := NthEis.fermat_unit (ms + 1 + 1) hℓ _ hc0
    have h2 : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
        (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
        = pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1 + 1) := by
      show _ = (ring (ms + 1 + 1 + 1)).mul (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
        (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      lib_ring
    exact h2.trans hf
  have hY := SymY.Y_ell (ms + 1 + 1 + 1) k' hq _ _ hcd rfl e (q_order (ms + 1 + 1) (k' + 1 + 1) e M hqn) M hqn _ hU
  -- the three factors
  have hC1 : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (castZ (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) ε) M) = (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one := by
    rw [hM']
    exact G_eps (ring (k' + 1)) (ms + 1 + 1) gl hgl ε hε M'
  have hU1 : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) jj.val) M)
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (jj.val * M)) (ms + 1 + 1 + 1) := by
    rw [TwoPower.pwpow]
    exact SymG.G_u (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 (jj.val * M)
  have hB1 := G_Bsym (ring (k' + 1)) (ms + 1 + 1) gl hgl hℓ (h' + 1) hh α M hY
  have hGA : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
        (StickElem.A j g (ms + 1))) M)
      = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
            (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1))) (ms + 1 + 1 + 1) := by
    rw [hA']
    exact SymG.G_u (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 _
  rw [hAB, Frobenius.pw_mul, Frobenius.pw_mul, SymG.G_mul (ring (k' + 1)) (ms + 1 + 1) gl hgl.1,
    SymG.G_mul (ring (k' + 1)) (ms + 1 + 1) gl hgl.1, hC1, hU1] at hGA
  have hB2 : SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
        (StickElem.B (ms + 1) h' α)) M)
      = SymG.G (ring (k' + 1)) (ms + 1 + 1) gl (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M) := hB1
  rw [hB2] at hGA
  rw [← hGA]
  lib_ring

/-- **Eisenstein's reciprocity for a primary prime element α of norm p (global-symbol form)**:
G(φ_u(α)^M) = (u^T)^(ℓ−1) in E = 𝔽_q[C_ℓ]/(N), T = Σ_c k_c(ℓ−1−c). -/
theorem eis_alpha_prim (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (M' : Nat) (hM' : M = M' + M') (hprim : PrimAlpha1.Prim (ms + 1 + 1) α) (h2 : Nat) (ht : t + 1 = 2 * h2) :
          (SymG.G (ring (k' + 1)) (ms + 1 + 1) gl
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
              (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
        = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
              (SymProdW.T k (EisRecV.vS (ms + 1 + 1)) (ms + 1 + 1 + 1))) (ms + 1 + 1 + 1) := by
  obtain ⟨jj, ε, hε, hmod⟩ := StickInst.stick_inst j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0
  have hrel := eis_alpha_of j ms t h' hpr g hg hℓ hN hh α iv hNα k' hq e hQ M hqn hp s hs k hk gl hgl hc0 M' hM' jj ε hε hmod
  have hN2 : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (2 * h2) := by rw [hN, ht]
  have hk2 : ms + 1 + 1 = 2 * h' + 1 :=
    Nat.succ.inj (hh.trans (show h' + 1 + (h' + 1) = 2 * h' + 1 + 1 by lib_nat))
  have hA := PrimAlpha2.A_val (ms + 1 + 1) j g hpr hg h2 hN2 (ms + 1 + 1) h' hk2
  have hB : (CycEis.phz (PrimAlpha1.D (ms + 1 + 1)) (ms + 1 + 1 + 1) (PrimAlpha1.z (ms + 1 + 1))
      (AbsPair.Bp (gring LibInt.intR (ms + 1 + 1 + 1)) (StickBB.ents (ms + 1 + 1 + 1) α (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1))))).2
      = (ring (ms + 1 + 1 + 1)).zero := by
    rw [PhzPw.phz_Bp (PrimAlpha1.D (ms + 1 + 1)) (ms + 1 + 1 + 1) _ (PrimAlpha1.z_ell (ms + 1 + 1))]
    exact PrimAlpha2.B_snd (ms + 1 + 1) hℓ α hprim _
      (SymB2.nz_qsL (ms + 1 + 1) (h' + 1) 0 (ms + 1 + 1) (SymB2.h_le (ms + 1 + 1) (h' + 1) hh) (Nat.zero_add _))
  have hj := PrimAlpha2.j0_zero (ms + 1 + 1) (h' + 1) hh _ _ hA hB jj ε hmod
  have hv : jj.val = 0 := by
    rw [hj]
    exact md_small (ms + 1 + 1 + 1) 0 (Nat.zero_lt_succ _)
  rw [hv, show 0 * M = 0 by lib_nat] at hrel
  rw [← hrel]
  show _ = (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).mul
    (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one (ms + 1 + 1 + 1)) _
  rw [SymG.one_pw]
  exact ((CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one_mul _).symm

end EisAlphaP

#print axioms EisAlphaP.eis_alpha_of
#print axioms EisAlphaP.eis_alpha_prim

lang/EisAlphaK1.lean

/-
  lang/EisAlphaK1 — T49 (general ℓ ≥ 5): **Eisenstein's reciprocity for a primary prime element α of norm p,
  with the P_1 symbol on the right**: α ∈ P_1, so the Stickelberger-weighted product of the q-symbols at the P_c
  collapses to the q-symbol at P_1 (lang/SymProdZ):
    G(φ_u(α)^M) = (u^(k_1))^(ℓ−1)       in E = 𝔽_q[C_ℓ]/(N),
  where u^(k_1) is (q^(−e)/P_1)_ℓ = (q^(−e)/α)_ℓ (z^(k_1) = (q^(−e))^((p−1)/ℓ)) and G(φ_u(α)^M) is the product over
  all primes above q of (α/𝔮)_ℓ (lang/SymG; work/data/t49/globsym_test.py).
    eis_alpha_k1
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EisAlphaP

namespace EisAlphaK1

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

/-- **(α/q) = (q^(−e)/α)** in global-symbol form, α primary of norm p in P_1. -/
theorem eis_alpha_k1 (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (M' : Nat) (hM' : M = M' + M') (hprim : PrimAlpha1.Prim (ms + 1 + 1) α) (h2 : Nat) (ht : t + 1 = 2 * h2) :
          (SymG.G (ring (k' + 1)) (ms + 1 + 1) gl
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
              (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
        = pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (1 * k 1)) (ms + 1 + 1 + 1) := by
  rw [EisAlphaP.eis_alpha_prim j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq e hQ M hqn hp s hs k hk gl hgl hc0 M' hM' hprim h2 ht]
  have hj : pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) (1 * k 1)
      = pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1) :=
    (SymProdZ.pw_pw (ring (j + 1 + 1)) _ 1 (k 1)).symm.trans (hk 1 (Nat.le_refl 1) (Nat.succ_lt_succ (Nat.succ_pos _)))
  rw [SymProdZ.sym_prod_z (ms + 1) hℓ (ring (j + 1 + 1)) _ _ (SymPsi.z_ord (j + 1) (ms + 1) t g hg hN) (1 * k 1) hj k
    (EisRecV.vS (ms + 1 + 1)) hk (EisRecV.vS_spec (ms + 1 + 1)) (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
    (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) (CycUnit.u_ell (ring (k' + 1)) (ms + 1 + 1 + 1))]

end EisAlphaK1

#print axioms EisAlphaK1.eis_alpha_k1

#print axioms: 29 theorems, no axioms.

78.385 Eisenstein reciprocity read in a residue field

For any ring map \(\rho \) out of \(E\), \(\rho \circ \tau (g^i)\) runs over the conjugate primes; the global symbol multiplies the symbols there with equal weight, so their product is \((q^{-e}/\alpha )^{\ell -1}\).

\[ \rho (u)^{\sum _{i<\ell -1}s_i}=\rho (u)^{k_1(\ell -1)},\qquad \rho \bigl (\tau (g^i)\varphi _u(\alpha )^M\bigr )=\rho \bigl (\tau (g^i)u\bigr )^{s_i}. \]

lang/SymRho.lean

/-
  lang/SymRho — T49 (general ℓ): **the global symbol read in a residue field**. For any ring map ρ : E → F
  (E = R[C_ℓ]/(N); for F the residue field of a prime 𝔮 above q, ρ∘τ(g^i) runs over the conjugate primes),
  if the symbol of Y at ρ∘τ(g^i) is s_i, i.e. ρ(τ(g^i) Y) = ρ(τ(g^i) u)^(s_i), then
    ρ(G(Y)) = ρ(u)^(Σ_(i<ℓ−1) s_i):
  G multiplies the symbols at all the conjugate primes with equal weight.
    hom_prodL     f(Π F(i)) = Π f(F(i))
    sumN, prodL_pw_sum    Π_i x^(s_i) = x^(Σ s_i)
    rho_tau_u     ρ(τ(d) u)^(s·d) = ρ(u)^s        (c·d = 1, ρ(u)^ℓ = 1)
    rho_G         ρ(G(Y)) = ρ(u)^(Σ_i s_i)
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.SymG
import lib.RingTac
import lib.Semi

namespace SymRho

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

section Hom

variable {S T : Type} (E : CRing S) (F : CRing T) (f : S → T)
  (hmul : ∀ x y, f (E.mul x y) = F.mul (f x) (f y)) (hone : f E.one = F.one)

include hmul hone in
theorem hom_prodL (H : Nat → S) : ∀ n, f (SymG.prodL E H n) = SymG.prodL F (fun i => f (H i)) n
  | 0 => hone
  | n + 1 => by
    show f (E.mul (SymG.prodL E H n) (H n)) = F.mul (SymG.prodL F (fun i => f (H i)) n) (f (H n))
    rw [hmul, hom_prodL H n]

end Hom

/-- Σ_(i<n) s(i). -/
def sumN (s : Nat → Nat) : Nat → Nat
  | 0 => 0
  | n + 1 => sumN s n + s n

theorem prodL_pw_sum {T : Type} (F : CRing T) (x : T) (s : Nat → Nat) :
    ∀ n, SymG.prodL F (fun i => pw F x (s i)) n = pw F x (sumN s n)
  | 0 => rfl
  | n + 1 => by
    show F.mul (SymG.prodL F (fun i => pw F x (s i)) n) (pw F x (s n)) = pw F x (sumN s n + s n)
    rw [prodL_pw_sum F x s n, SymG.pw_add]

variable {R : Type} (K : CRing R) (n : Nat) (g : Zp (n + 1)) (hg : pw (ring (n + 1)) g (n + 1) = (ring (n + 1)).one)
  {T : Type} (F : CRing T) (ρ : CycRing.C K (n + 1) → T)
  (hmul : ∀ x y, ρ ((CycRing.cyc K (n + 1)).mul x y) = F.mul (ρ x) (ρ y)) (hone : ρ (CycRing.cyc K (n + 1)).one = F.one)

include hmul hone in
/-- **ρ(τ(d) u)^(s·d) = ρ(u)^s** for c·d = 1 in 𝔽_ℓ. -/
theorem rho_tau_u (c d : Zp (n + 1)) (hcd : (ring (n + 1)).mul c d = (ring (n + 1)).one) (s : Nat) :
    pw F (pw F (ρ (CycTau.tau K (n + 1) d (CycUnit.u K (n + 1)))) s) d.val = pw F (ρ (CycUnit.u K (n + 1))) s := by
  rw [CycTau.tau_u K (n + 1) c d hcd, CycTau.hom_pw (CycRing.cyc K (n + 1)) F ρ hmul hone]
  rw [SymG.pw3]
  have hu : pw F (ρ (CycUnit.u K (n + 1))) (n + 1 + 1) = F.one := by
    rw [← CycTau.hom_pw (CycRing.cyc K (n + 1)) F ρ hmul hone, CycUnit.u_ell K (n + 1)]
    exact hone
  rw [← GEval.pw_md F (n + 1) _ hu (c.val * d.val)]
  have e1 : md (n + 1) (c.val * d.val) = 1 :=
    (congrArg Subtype.val hcd).trans (md_small (n + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ n)))
  rw [e1]
  show pw F (F.mul F.one (ρ (CycUnit.u K (n + 1)))) s = _
  rw [F.one_mul]

include hg hmul hone in
/-- **ρ(G(Y)) = ρ(u)^(Σ_i s_i)** when the symbol of Y at ρ∘τ(g^i) is s_i. -/
theorem rho_G (Y : CycRing.C K (n + 1)) (s : Nat → Nat)
    (hs : ∀ i, ρ (CycTau.tau K (n + 1) (SymG.dI n g i) Y)
      = pw F (ρ (CycTau.tau K (n + 1) (SymG.dI n g i) (CycUnit.u K (n + 1)))) (s i)) :
    ρ (SymG.G K n g Y) = pw F (ρ (CycUnit.u K (n + 1))) (sumN s (n + 1)) := by
  unfold SymG.G
  rw [hom_prodL (CycRing.cyc K (n + 1)) F ρ hmul hone, ← prodL_pw_sum]
  exact SymG.prodL_ext F _ _ (fun i => by
    show ρ (pw (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) (SymG.dI n g i) Y) (SymG.dI n g i).val) = _
    rw [CycTau.hom_pw (CycRing.cyc K (n + 1)) F ρ hmul hone, hs i]
    exact rho_tau_u K n F ρ hmul hone (SymG.cI n g i) (SymG.dI n g i) (SymG.cd_one n g hg i) (s i)) _

end SymRho

#print axioms SymRho.hom_prodL
#print axioms SymRho.prodL_pw_sum
#print axioms SymRho.rho_tau_u
#print axioms SymRho.rho_G

lang/EisRho.lean

/-
  lang/EisRho — T49 (general ℓ ≥ 5): **Eisenstein's reciprocity read in a residue field**. For a primary prime
  element α of norm p and any ring map ρ : 𝔽_q[C_ℓ]/(N) → F, if s_i is the symbol of φ_u(α)^M at the conjugate
  ρ∘τ(g^i) (i.e. ρ(τ(g^i)(φ_u(α)^M)) = ρ(τ(g^i) u)^(s_i)), then
    ρ(u)^(Σ_(i<ℓ−1) s_i) = ρ(u)^(k_1 (ℓ−1)),      u^(k_1) = (q^(−e)/α)_ℓ:
  the product of the ℓ-th power symbols of α at all the primes above q (each counted f times) is (q^(−e)/α)^(ℓ−1).
    eis_rho
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EisAlphaFin
import lang.SymRho

namespace EisRho

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

/-- **the symbols of α at the conjugate primes multiply to (q^(−e)/α)^(ℓ−1)**. -/
theorem eis_rho (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hq2 : k' + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α)
    {T : Type} (F : CRing T) (ρ : CycRing.C (ring (k' + 1)) (ms + 1 + 1 + 1) → T)
    (hmul : ∀ x y, ρ ((CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).mul x y) = F.mul (ρ x) (ρ y))
    (hone : ρ (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one = F.one) (sy : Nat → Nat)
    (hsy : ∀ i, ρ (CycTau.tau (ring (k' + 1)) (ms + 1 + 1 + 1) (SymG.dI (ms + 1 + 1) gl i)
        (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
      = pw F (ρ (CycTau.tau (ring (k' + 1)) (ms + 1 + 1 + 1) (SymG.dI (ms + 1 + 1) gl i) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)))) (sy i)) :
    pw F (ρ (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))) (SymRho.sumN sy (ms + 1 + 1 + 1))
      = pw F (ρ (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))) ((1 * k 1) * (ms + 1 + 1 + 1)) := by
  have h1 := SymRho.rho_G (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 F ρ hmul hone _ sy hsy
  rw [EisAlphaFin.eis_alpha_fin j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq hq2 e hQ M hqn hp s hs k hk gl hgl hc0 hprim] at h1
  rw [CycTau.hom_pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) F ρ hmul hone,
    CycTau.hom_pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) F ρ hmul hone, TwoPower.pwpow] at h1
  exact h1.symm

end EisRho

#print axioms EisRho.eis_rho

#print axioms: 5 theorems, no axioms.

78.386 Eisenstein reciprocity at the primes of degree one above \(q\)

In a ring without zero divisors every \(\ell \)-th root of unity is a power of a nontrivial one, found by a decidable search (no choice); evaluation at \(z\in \mathbb F_q\) of order \(\ell \) is a ring map \(\rho _z\) on \(\mathbb F_q[C_\ell ]/(N)\), and the computed symbols of \(\alpha \) at the primes \(\rho _z\circ \tau (g^i)\) multiply to \((q^{-e}/\alpha )^{\ell -1}\).

\[ z^{\sum _{i<\ell -1}s_i}=z^{k_1(\ell -1)},\qquad \rho _z\bigl (\tau (g^i)\varphi _u(\alpha )^M\bigr )=\rho _z\bigl (\tau (g^i)u\bigr )^{s_i}. \]

lang/RootPick.lean

/-
  lang/RootPick — T49 (symbols at a prime, step 1): **picking the exponent of a root of unity**, without choice.
  In a commutative ring without zero divisors and with decidable equality:
    pick, fp       fp t w k: the first j < k with t = w^j found by a decidable search (k if none)
    le_cases       j ≤ k ⇒ j = k or j + 1 ≤ k (structural, on Nat.le)
    fp_or          t = w^(fp t w k) or t differs from every w^j, j < k
    prime_ne       ℓ prime, w^ℓ = 1, w ≠ 1 ⇒ w^(d+1) ≠ 1 for d + 1 < ℓ        (inverse of d + 1 mod ℓ)
    root_pick      w^ℓ = 1, w ≠ 1, t^ℓ = 1 ⇒ t = w^(fp t w ℓ)                   (RootAll.all_roots)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.RootAll
import lang.OrdMul
import lang.GEval

namespace RootPick

open FTAChain ZMod Frobenius

variable {R : Type} (K : DetChain.CRing R) (dec : DecidableEq R)

/-- choose j if t = w^j was decided true, else k. -/
def pick (t w : R) (j k : Nat) : Decidable (t = pw K w j) → Nat
  | isTrue _ => j
  | isFalse _ => k

/-- the exponent found by searching j < k. -/
def fp (t w : R) : Nat → Nat
  | 0 => 0
  | k + 1 => pick K t w (fp t w k) k (dec t (pw K w (fp t w k)))

theorem le_cases : ∀ (j k : Nat), Nat.le j k → j = k ∨ Nat.le (j + 1) k
  | _, _, Nat.le.refl => Or.inl rfl
  | _, _, Nat.le.step h => Or.inr (Nat.succ_le_succ h)

/-- **the search either finds an exponent or rules out every j < k**. -/
theorem fp_or (t w : R) : ∀ k : Nat, t = pw K w (fp K dec t w k) ∨ ∀ j, j < k → t ≠ pw K w j
  | 0 => Or.inr (fun j h => absurd h (Nat.not_succ_le_zero j))
  | k + 1 => by
    show t = pw K w (pick K t w (fp K dec t w k) k (dec t (pw K w (fp K dec t w k)))) ∨ _
    match dec t (pw K w (fp K dec t w k)) with
    | isTrue h => exact Or.inl h
    | isFalse h =>
      cases fp_or t w k with
      | inl h' => exact absurd h' h
      | inr hall =>
        match dec t (pw K w k) with
        | isTrue h2 => exact Or.inl h2
        | isFalse h2 =>
          refine Or.inr (fun j hj => ?_)
          cases le_cases j k (Nat.le_of_succ_le_succ hj) with
          | inl e => rw [e]; exact h2
          | inr hlt => exact hall j hlt

/-- **a nontrivial ℓ-th root of unity has exact order ℓ** (ℓ prime). -/
theorem prime_ne (n : Nat) (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) (w : R)
    (hn : pw K w (n + 1 + 1) = K.one) (hw1 : w ≠ K.one) : ∀ d, d + 1 < n + 1 + 1 → pw K w (d + 1) ≠ K.one := by
  intro d hd h
  let x : Zp (n + 1) := ⟨d + 1, hd⟩
  have hx : x ≠ (ring (n + 1)).zero := fun e => by
    have e0 : d + 1 = 0 := congrArg Subtype.val e
    exact Nat.noConfusion e0
  have hinv := inv_exists n hpr x hx
  have hv : md (n + 1) ((pw (ring (n + 1)) x n).val * (d + 1)) = 1 :=
    (congrArg Subtype.val hinv).trans (md_small (n + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ n)))
  apply hw1
  calc w = pw K w 1 := (K.one_mul w).symm
    _ = pw K w (md (n + 1) ((pw (ring (n + 1)) x n).val * (d + 1))) := by rw [hv]
    _ = pw K w ((pw (ring (n + 1)) x n).val * (d + 1)) := GEval.pw_md K (n + 1) w hn _
    _ = pw K w ((d + 1) * (pw (ring (n + 1)) x n).val) := by rw [nmul_comm]
    _ = pw K (pw K w (d + 1)) (pw (ring (n + 1)) x n).val := (TwoPower.pwpow K w _ _).symm
    _ = K.one := by rw [h, TwoPower.pwone]

/-- **every ℓ-th root of unity is the found power of a nontrivial one**. -/
theorem root_pick (hK : DetChain.NoZD K) (h10 : K.one ≠ K.zero) (n : Nat)
    (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) (w : R)
    (hn : pw K w (n + 1 + 1) = K.one) (hw1 : w ≠ K.one) (t : R) (ht : pw K t (n + 1 + 1) = K.one) :
    t = pw K w (fp K dec t w (n + 1 + 1)) := by
  have hw : w ≠ K.zero := fun e => by
    rw [e] at hn
    apply h10
    rw [← hn]
    show K.mul (pw K K.zero (n + 1)) K.zero = K.zero
    exact DetChain.mul_zero K _
  cases fp_or K dec t w (n + 1 + 1) with
  | inl h => exact h
  | inr hall => exact absurd hall (fun hnot =>
      RootAll.all_roots K hK h10 w hw (n + 1) (prime_ne K n hpr w hn hw1) hn t ht hnot)

end RootPick

#print axioms RootPick.le_cases
#print axioms RootPick.fp_or
#print axioms RootPick.prime_ne
#print axioms RootPick.root_pick

lang/EisRhoF.lean

/-
  lang/EisRhoF — T49 (symbols at a prime, step 2): **Eisenstein's reciprocity at the primes above q, with the
  symbols computed**. For a ring map ρ : E → F into a ring without zero divisors with decidable equality and
  ρ(u) ≠ 1 (F the residue field of a prime 𝔮 above q), every ℓ-th root of unity ρ(τ(g^i) Y) is a power
  ρ(τ(g^i) u)^(s_i), with s_i found by a decidable search (no choice), and for a primary prime α of norm p
    ρ(u)^(Σ_(i<ℓ−1) s_i) = ρ(u)^(k_1 (ℓ−1)),      u^(k_1) = (q^(−e)/α)_ℓ.
    tau_pw_one   Y^ℓ = 1 ⇒ ρ(τ(d) Y)^ℓ = 1
    tau_u_ne     ρ(u) ≠ 1 ⇒ ρ(τ(d) u) ≠ 1
    syF, sy_spec the symbol s_i of Y at ρ∘τ(g^i), and ρ(τ(g^i) Y) = ρ(τ(g^i) u)^(s_i)
    rho_G_field  ρ(G(Y)) = ρ(u)^(Σ_i s_i)
    eis_rho_field
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.EisRho
import lang.RootPick
import lib.RingTac

namespace EisRhoF

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

section Field

variable {R : Type} (K : CRing R) (n : Nat) (g : Zp (n + 1)) (hg : pw (ring (n + 1)) g (n + 1) = (ring (n + 1)).one)
  {T : Type} (F : CRing T) (ρ : CycRing.C K (n + 1) → T)
  (hmul : ∀ x y, ρ ((CycRing.cyc K (n + 1)).mul x y) = F.mul (ρ x) (ρ y)) (hone : ρ (CycRing.cyc K (n + 1)).one = F.one)
  (hK : NoZD F) (h10 : F.one ≠ F.zero) (dec : DecidableEq T)
  (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) (hu1 : ρ (CycUnit.u K (n + 1)) ≠ F.one)

include hmul hone in
/-- **Y^ℓ = 1 ⇒ ρ(τ(d) Y)^ℓ = 1**. -/
theorem tau_pw_one (c d : Zp (n + 1)) (hcd : (ring (n + 1)).mul c d = (ring (n + 1)).one) (Y : CycRing.C K (n + 1))
    (hY : pw (CycRing.cyc K (n + 1)) Y (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) :
    pw F (ρ (CycTau.tau K (n + 1) d Y)) (n + 1 + 1) = F.one := by
  rw [← CycTau.hom_pw (CycRing.cyc K (n + 1)) F ρ hmul hone,
    ← CycTau.hom_pw (CycRing.cyc K (n + 1)) (CycRing.cyc K (n + 1)) (CycTau.tau K (n + 1) d)
      (CycTau.tau_mul K (n + 1) c d hcd) (CycTau.tau_one K (n + 1) c d hcd),
    hY, CycTau.tau_one K (n + 1) c d hcd, hone]

include hmul hone hu1 in
/-- **ρ(u) ≠ 1 ⇒ ρ(τ(d) u) ≠ 1**. -/
theorem tau_u_ne (c d : Zp (n + 1)) (hcd : (ring (n + 1)).mul c d = (ring (n + 1)).one) :
    ρ (CycTau.tau K (n + 1) d (CycUnit.u K (n + 1))) ≠ F.one := fun e => by
  have h := SymRho.rho_tau_u K n F ρ hmul hone c d hcd 1
  rw [e, TwoPower.pwone, TwoPower.pwone] at h
  exact hu1 ((F.one_mul _).symm.trans h.symm)

/-- the symbol of Y at ρ∘τ(g^i). -/
def syF (Y : CycRing.C K (n + 1)) (i : Nat) : Nat :=
  RootPick.fp F dec (ρ (CycTau.tau K (n + 1) (SymG.dI n g i) Y))
    (ρ (CycTau.tau K (n + 1) (SymG.dI n g i) (CycUnit.u K (n + 1)))) (n + 1 + 1)

include hg hmul hone hK h10 hpr hu1 in
/-- **ρ(τ(g^i) Y) = ρ(τ(g^i) u)^(s_i)** for Y^ℓ = 1. -/
theorem sy_spec (Y : CycRing.C K (n + 1))
    (hY : pw (CycRing.cyc K (n + 1)) Y (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) (i : Nat) :
    ρ (CycTau.tau K (n + 1) (SymG.dI n g i) Y)
      = pw F (ρ (CycTau.tau K (n + 1) (SymG.dI n g i) (CycUnit.u K (n + 1)))) (syF K n g F ρ dec Y i) :=
  RootPick.root_pick F dec hK h10 n hpr _
    (tau_pw_one K n F ρ hmul hone _ _ (SymG.cd_one n g hg i) _ (CycUnit.u_ell K (n + 1)))
    (tau_u_ne K n F ρ hmul hone hu1 _ _ (SymG.cd_one n g hg i))
    _ (tau_pw_one K n F ρ hmul hone _ _ (SymG.cd_one n g hg i) Y hY)

include hg hmul hone hK h10 hpr hu1 in
/-- **ρ(G(Y)) = ρ(u)^(Σ_i s_i)** with the computed symbols. -/
theorem rho_G_field (Y : CycRing.C K (n + 1))
    (hY : pw (CycRing.cyc K (n + 1)) Y (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) :
    ρ (SymG.G K n g Y) = pw F (ρ (CycUnit.u K (n + 1))) (SymRho.sumN (syF K n g F ρ dec Y) (n + 1)) :=
  SymRho.rho_G K n g hg F ρ hmul hone Y _ (sy_spec K n g hg F ρ hmul hone hK h10 dec hpr hu1 Y hY)

end Field

/-- **Eisenstein's reciprocity at the primes above q**: the computed symbols s_i of a primary prime α of norm p
    satisfy ρ(u)^(Σ s_i) = ρ(u)^(k_1(ℓ−1)). -/
theorem eis_rho_field (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hq2 : k' + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α)
    {T : Type} (F : CRing T) (ρ : CycRing.C (ring (k' + 1)) (ms + 1 + 1 + 1) → T)
    (hmul : ∀ x y, ρ ((CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).mul x y) = F.mul (ρ x) (ρ y))
    (hone : ρ (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one = F.one)
    (hK : NoZD F) (h10 : F.one ≠ F.zero) (dec : DecidableEq T)
    (hu1 : ρ (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) ≠ F.one) :
    pw F (ρ (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)))
        (SymRho.sumN (syF (ring (k' + 1)) (ms + 1 + 1) gl F ρ dec
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
          (ms + 1 + 1 + 1))
      = pw F (ρ (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))) ((1 * k 1) * (ms + 1 + 1 + 1)) := by
  have hU := EisAlpha.unit_alpha k' hq (ms + 1 + 1) (j + 1 + 1 + 1) hp α iv h' hNα
  have hcd : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
      = (ring (ms + 1 + 1 + 1)).one := by
    have hf := NthEis.fermat_unit (ms + 1 + 1) hℓ _ hc0
    have h2 : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
        (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
        = pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1 + 1) := by
      show _ = (ring (ms + 1 + 1 + 1)).mul (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
        (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      lib_ring
    exact h2.trans hf
  have hY := SymY.Y_ell (ms + 1 + 1 + 1) k' hq _ _ hcd rfl e (EisAlpha.q_order (ms + 1 + 1) (k' + 1 + 1) e M hqn) M hqn _ hU
  exact EisRho.eis_rho j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq hq2 e hQ M hqn hp s hs k hk gl hgl hc0 hprim
    F ρ hmul hone _ (sy_spec (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 F ρ hmul hone hK h10 dec hℓ hu1 _ hY)

end EisRhoF

#print axioms EisRhoF.tau_pw_one
#print axioms EisRhoF.tau_u_ne
#print axioms EisRhoF.sy_spec
#print axioms EisRhoF.rho_G_field
#print axioms EisRhoF.eis_rho_field

lang/RhoU1.lean

/-
  lang/RhoU1 — T49 (symbols at a prime, step 3): **ρ(u) ≠ 1 when ℓ is a unit in the target**. For a ring map
  ρ : R[C_ℓ]/(N) → F (ℓ prime) with ℓ·1 ≠ 0 in F (the residue characteristic is not ℓ), ρ(u) ≠ 1, since
  Σ_b u^b = 0 in R[C_ℓ]/(N) and ρ(u) = 1 would give ℓ·1 = 0.
    sum_one_rng   Σ_(i<k) 1 = k·1
    rho_u_ne      castN F ℓ ≠ 0 ⇒ ρ(u) ≠ 1
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.PhzModN
import lang.CycTau
import lib.RingTac

namespace RhoU1

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob
open LibRing (castN)

/-- **Σ_(i<k) 1 = k·1**. -/
theorem sum_one_rng {T : Type} (F : CRing T) : ∀ k, sumK F (fun _ : Nat => F.one) (CycUnit.rng k) = castN F k
  | 0 => rfl
  | k + 1 => by
    show sumK F (fun _ : Nat => F.one) (CycUnit.rng k ++ [k]) = F.add F.one (castN F k)
    rw [sumK_append, sum_one_rng F k]
    show F.add (castN F k) (F.add F.one F.zero) = F.add F.one (castN F k)
    lib_ring

/-- **ρ(u) ≠ 1** for a ring map ρ out of R[C_ℓ]/(N) into a ring where ℓ·1 ≠ 0. -/
theorem rho_u_ne {R : Type} (K : CRing R) (n : Nat) (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1)
    {T : Type} (F : CRing T) (ρ : CycRing.C K (n + 1) → T)
    (hadd : ∀ x y, ρ ((CycRing.cyc K (n + 1)).add x y) = F.add (ρ x) (ρ y))
    (hmul : ∀ x y, ρ ((CycRing.cyc K (n + 1)).mul x y) = F.mul (ρ x) (ρ y)) (hone : ρ (CycRing.cyc K (n + 1)).one = F.one)
    (hℓF : castN F (n + 1 + 1) ≠ F.zero) : ρ (CycUnit.u K (n + 1)) ≠ F.one := fun e => by
  have h := congrArg ρ (PhzModN.sum_u K n hpr)
  rw [CycTau.hom_sumK (CycRing.cyc K (n + 1)) F ρ hadd, CycTau.hom_zero (CycRing.cyc K (n + 1)) F ρ hadd] at h
  have h1 : sumK F (fun b : Zp (n + 1) => ρ (pw (CycRing.cyc K (n + 1)) (CycUnit.u K (n + 1)) b.val)) (L (n + 1))
      = sumK F (fun _ : Zp (n + 1) => F.one) (L (n + 1)) :=
    sumK_ext F _ _ (fun b => by
      rw [CycTau.hom_pw (CycRing.cyc K (n + 1)) F ρ hmul hone, e, TwoPower.pwone]) _
  rw [h1] at h
  have h2 : sumK F (fun _ : Zp (n + 1) => F.one) (L (n + 1)) = castN F (n + 1 + 1) := by
    show sumK F (fun _ : Zp (n + 1) => F.one) (CMInert.allZ (n + 1) (n + 1 + 1)) = _
    rw [CycUnit.sum_allZ (n + 1) F _ (n + 1 + 1)]
    exact sum_one_rng F (n + 1 + 1)
  exact hℓF (h2.symm.trans h)

end RhoU1

#print axioms RhoU1.sum_one_rng
#print axioms RhoU1.rho_u_ne

lang/CycEvalK.lean

/-
  lang/CycEvalK — T49 (symbols at a prime, step 4): **evaluation of K[C_ℓ]/(N) at z ∈ K is a ring map**, for any
  coefficient ring K and z with z^ℓ = 1, Σ_(a<ℓ) z^a = 0 (e.g. K = 𝔽_q with ℓ | q − 1 and z of order ℓ: the
  residue map at a prime of degree one above q):
    gev_red          gev_z(red w) = gev_z(w)   (evaluation kills N)
    evK, evK_add, evK_mul, evK_one     evK(x) := gev_z(x) is a ring map
    evK_u            evK(u) = z
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycEval
import lib.RingTac

namespace CycEvalK

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {R : Type} (K : CRing R) (n : Nat) (z : R)

/-- **evaluation kills N**: gev_z(red w) = gev_z(w). -/
theorem gev_red (hz : pw K z (n + 1) = K.one) (hs : CycChar.gs K z (n + 1) = K.zero) (w : V R (n + 1)) :
    GEval.gev K n z (CycRing.red K n w) = GEval.gev K n z w := by
  show sumK K (fun a => K.mul (gt K n (CycRing.red K n w) a) (pw K z a.val)) (L n) =
    sumK K (fun a => K.mul (gt K n w a) (pw K z a.val)) (L n)
  have e : ∀ a : Zp n, K.mul (gt K n (CycRing.red K n w) a) (pw K z a.val) =
      K.add (K.mul (gt K n w a) (pw K z a.val)) (K.mul (K.neg (gt K n w (CycRing.top n))) (pw K z a.val)) := fun a => by
    rw [CycRing.gt_red]
    lib_ring
  have e2 : sumK K (fun a : Zp n => K.mul (K.neg (gt K n w (CycRing.top n))) (pw K z a.val)) (L n) = K.zero :=
    (mul_sumK K (K.neg (gt K n w (CycRing.top n))) (fun a : Zp n => pw K z a.val) (L n)).symm.trans
      (by rw [CycEval.sum_L K n z hz, hs]; lib_ring)
  rw [sumK_ext K _ _ e (L n), sumK_add, e2]
  lib_ring

/-- evaluation on normal forms. -/
def evK (x : CycRing.C K n) : R := GEval.gev K n z x.val

theorem evK_add (a b : CycRing.C K n) :
    evK K n z ((CycRing.cyc K n).add a b) = K.add (evK K n z a) (evK K n z b) :=
  GEval.gev_add K n z a.val b.val

theorem evK_mul (hz : pw K z (n + 1) = K.one) (hs : CycChar.gs K z (n + 1) = K.zero) (a b : CycRing.C K n) :
    evK K n z ((CycRing.cyc K n).mul a b) = K.mul (evK K n z a) (evK K n z b) :=
  (gev_red K n z hz hs (gmul K n a.val b.val)).trans (GEval.gev_mul K n z hz a.val b.val)

theorem gev_mono (a : Zp n) (s : R) : GEval.gev K n z (mono K n a s) = K.mul s (pw K z a.val) := by
  show sumK K (fun b => K.mul (gt K n (mono K n a s) b) (pw K z b.val)) (L n) = _
  rw [sumK_ext K _ (fun b => K.mul (delta K b a) (K.mul s (pw K z b.val))) (fun b => by
    rw [gt_mono]; lib_ring) (L n)]
  exact pick K _ (CMInert.complete_allZ n K) (fun b => K.mul s (pw K z b.val)) a

theorem evK_one (hz : pw K z (n + 1) = K.one) (hs : CycChar.gs K z (n + 1) = K.zero) :
    evK K n z (CycRing.cyc K n).one = K.one := by
  show GEval.gev K n z (CycRing.red K n (gone K n)) = K.one
  rw [gev_red K n z hz hs, CycUnit.gone_mono, gev_mono]
  show K.mul K.one K.one = K.one
  lib_ring

theorem evK_u (hz : pw K z (n + 1) = K.one) (hs : CycChar.gs K z (n + 1) = K.zero) :
    evK K n z (CycUnit.u K n) = z := by
  show GEval.gev K n z (CycRing.red K n (mono K n (ring n).one K.one)) = z
  rw [gev_red K n z hz hs, gev_mono]
  show K.mul K.one (pw K z (md n 1)) = z
  rw [GEval.pw_md K n z hz 1]
  show K.mul K.one (K.mul K.one z) = z
  lib_ring

end CycEvalK

#print axioms CycEvalK.gev_red
#print axioms CycEvalK.evK_add
#print axioms CycEvalK.evK_mul
#print axioms CycEvalK.gev_mono
#print axioms CycEvalK.evK_one
#print axioms CycEvalK.evK_u

lang/EisFq.lean

/-
  lang/EisFq — T49 (symbols at a prime, step 5): **Eisenstein's reciprocity at a prime of degree one above q**.
  For z ∈ 𝔽_q of order ℓ (so ℓ | q − 1), evaluation at z is a ring map ρ_z : 𝔽_q[C_ℓ]/(N) → 𝔽_q (CycEvalK), and
  ρ_z∘τ(g^i) runs over the primes above q. With the symbols s_i of φ_u(α)^M at these primes computed by a decidable
  search (EisRhoF, no choice), for a primary prime α of norm p
    z^(Σ_(i<ℓ−1) s_i) = z^(k_1 (ℓ−1)),      u^(k_1) = (q^(−e)/α)_ℓ.
    one_ne, decZ, gs_z, eis_fq
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EisRhoF
import lang.CycEvalK

namespace EisFq

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

theorem one_ne (k : Nat) : (ring (k + 1)).one ≠ (ring (k + 1)).zero := fun e => by
  have e0 : md (k + 1) 1 = md (k + 1) 0 := congrArg Subtype.val e
  rw [md_small (k + 1) 1 (Nat.succ_lt_succ (Nat.zero_lt_succ k))] at e0
  exact Nat.noConfusion e0

/-- decidable equality on 𝔽_p. -/
def decZ (k : Nat) : DecidableEq (Zp k) := fun a b =>
  match Nat.decEq a.val b.val with
  | isTrue h => isTrue (Subtype.ext h)
  | isFalse h => isFalse (fun e => h (congrArg Subtype.val e))

/-- **Σ_(a<ℓ) z^a = 0** for z ≠ 1 of order dividing ℓ in 𝔽_q. -/
theorem gs_z (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (n : Nat) (z : Zp (k' + 1))
    (hz : pw (ring (k' + 1)) z (n + 1) = (ring (k' + 1)).one) (hz1 : z ≠ (ring (k' + 1)).one) : CycChar.gs (ring (k' + 1)) z (n + 1) = (ring (k' + 1)).zero :=
  CycEis.gs_zero (ring (k' + 1)) (nozd k' hq) z hz1 (n + 1) hz

/-- **Eisenstein's reciprocity at the primes of degree one above q**. -/
theorem eis_fq (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hq2 : k' + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs' : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α)
    (z : Zp (k' + 1)) (hz : pw (ring (k' + 1)) z (ms + 1 + 1 + 1 + 1) = (ring (k' + 1)).one) (hz1 : z ≠ (ring (k' + 1)).one) :
    pw (ring (k' + 1)) z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z) (decZ (k' + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
          (ms + 1 + 1 + 1))
      = pw (ring (k' + 1)) z ((1 * k 1) * (ms + 1 + 1 + 1)) := by
  have hs := gs_z k' hq (ms + 1 + 1 + 1) z hz hz1
  have hu := CycEvalK.evK_u (ring (k' + 1)) (ms + 1 + 1 + 1) z hz hs
  have h := EisRhoF.eis_rho_field j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq hq2 e hQ M hqn hp s hs' k hk gl hgl hc0
    hprim (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z) (CycEvalK.evK_mul (ring (k' + 1)) (ms + 1 + 1 + 1) z hz hs)
    (CycEvalK.evK_one (ring (k' + 1)) (ms + 1 + 1 + 1) z hz hs) (nozd k' hq) (one_ne k') (decZ (k' + 1)) (by rw [hu]; exact hz1)
  rw [hu] at h
  exact h

end EisFq

#print axioms EisFq.one_ne
#print axioms EisFq.gs_z
#print axioms EisFq.eis_fq

#print axioms: 20 theorems, no axioms.

78.387 Eisenstein reciprocity at degree-one primes for products

The computed symbols add under products, and \(\varphi _u(\alpha )^M\) is an \(\ell \)-th root of unity whenever \(N(\alpha )\) is prime to \(q\), so the reciprocity for each primary prime factor gives it for the product.

\[ z^{\sum _i s_i(\prod _j\alpha _j)}=z^{\sum _j c_j}\quad \text {when}\quad z^{\sum _i s_i(\alpha _j)}=z^{c_j}\ \text {for all } j. \]

lang/EisFqMul.lean

/-
  lang/EisFqMul — T49 (general ℓ): **the computed symbols add under products**, so Eisenstein's reciprocity at the
  primes of degree one above q (lang/EisFq) passes from primary primes of norm p to their products.
    rho_sum_mul   ρ(u)^(Σ s(Y Y')) = ρ(u)^(Σ s(Y) + Σ s(Y'))           (Y^ℓ = Y'^ℓ = 1)
    rho_sum_one   ρ(u)^(Σ s(1)) = 1
    prodY         Π of a list in K[C_ℓ]/(N)
    prodY_ell     Y_i^ℓ = 1 for all i ⇒ (Π Y_i)^ℓ = 1
    rho_sum_list  ρ(u)^(Σ s(Y_i)) = ρ(u)^(c_i) for all i ⇒ ρ(u)^(Σ s(Π Y_i)) = ρ(u)^(Σ c_i)
    phz_prodV     φ_u(Π α_i)^M = Π φ_u(α_i)^M
    eis_fq_mul    z^(Σ s(φ_u(α_i)^M)) = z^(c_i) for all i ⇒ z^(Σ s(φ_u(Π α_i)^M)) = z^(Σ c_i)
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EisFq
import lang.EisAlphaMul

namespace EisFqMul

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

section Gen

variable {R : Type} (K : CRing R) (n : Nat) (g : Zp (n + 1)) (hg : pw (ring (n + 1)) g (n + 1) = (ring (n + 1)).one)
  {T : Type} (F : CRing T) (ρ : CycRing.C K (n + 1) → T)
  (hmul : ∀ x y, ρ ((CycRing.cyc K (n + 1)).mul x y) = F.mul (ρ x) (ρ y)) (hone : ρ (CycRing.cyc K (n + 1)).one = F.one)
  (hK : NoZD F) (h10 : F.one ≠ F.zero) (dec : DecidableEq T)
  (hpr : ∀ d, Dv d (n + 1 + 1) → d = 1 ∨ d = n + 1 + 1) (hu1 : ρ (CycUnit.u K (n + 1)) ≠ F.one)

include hg hmul hone hK h10 hpr hu1 in
/-- **ρ(u)^(Σ s(Y Y')) = ρ(u)^(Σ s(Y) + Σ s(Y'))**. -/
theorem rho_sum_mul (Y Y' : CycRing.C K (n + 1))
    (hY : pw (CycRing.cyc K (n + 1)) Y (n + 1 + 1) = (CycRing.cyc K (n + 1)).one)
    (hY' : pw (CycRing.cyc K (n + 1)) Y' (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) :
    pw F (ρ (CycUnit.u K (n + 1))) (SymRho.sumN (EisRhoF.syF K n g F ρ dec ((CycRing.cyc K (n + 1)).mul Y Y')) (n + 1))
      = pw F (ρ (CycUnit.u K (n + 1)))
          (SymRho.sumN (EisRhoF.syF K n g F ρ dec Y) (n + 1) + SymRho.sumN (EisRhoF.syF K n g F ρ dec Y') (n + 1)) := by
  have hYY : pw (CycRing.cyc K (n + 1)) ((CycRing.cyc K (n + 1)).mul Y Y') (n + 1 + 1) = (CycRing.cyc K (n + 1)).one := by
    rw [Frobenius.pw_mul, hY, hY']
    exact (CycRing.cyc K (n + 1)).one_mul _
  rw [← EisRhoF.rho_G_field K n g hg F ρ hmul hone hK h10 dec hpr hu1 _ hYY, SymG.G_mul K n g hg, hmul,
    EisRhoF.rho_G_field K n g hg F ρ hmul hone hK h10 dec hpr hu1 _ hY,
    EisRhoF.rho_G_field K n g hg F ρ hmul hone hK h10 dec hpr hu1 _ hY']
  exact (SymG.pw_add F _ _ _).symm

include hg hmul hone hK h10 hpr hu1 in
/-- **ρ(u)^(Σ s(1)) = 1**. -/
theorem rho_sum_one :
    pw F (ρ (CycUnit.u K (n + 1))) (SymRho.sumN (EisRhoF.syF K n g F ρ dec (CycRing.cyc K (n + 1)).one) (n + 1)) = F.one := by
  rw [← EisRhoF.rho_G_field K n g hg F ρ hmul hone hK h10 dec hpr hu1 _ (TwoPower.pwone _ _)]
  have h := SymG.G_u K n g hg 0
  have h1 : SymG.G K n g (CycRing.cyc K (n + 1)).one = pw (CycRing.cyc K (n + 1)) (CycRing.cyc K (n + 1)).one (n + 1) := h
  rw [h1, TwoPower.pwone, hone]

/-- Π of a list in K[C_ℓ]/(N). -/
def prodY : List (CycRing.C K (n + 1)) → CycRing.C K (n + 1)
  | [] => (CycRing.cyc K (n + 1)).one
  | a :: as => (CycRing.cyc K (n + 1)).mul a (prodY as)

/-- **Y_i^ℓ = 1 for all i ⇒ (Π Y_i)^ℓ = 1**. -/
theorem prodY_ell {ι : Type} (f : ι → CycRing.C K (n + 1)) : ∀ xs : List ι,
    (∀ x, x ∈ xs → pw (CycRing.cyc K (n + 1)) (f x) (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) →
    pw (CycRing.cyc K (n + 1)) (prodY K n (xs.map f)) (n + 1 + 1) = (CycRing.cyc K (n + 1)).one
  | [], _ => TwoPower.pwone _ _
  | a :: as, h => by
    show pw (CycRing.cyc K (n + 1)) ((CycRing.cyc K (n + 1)).mul (f a) (prodY K n (as.map f))) (n + 1 + 1) = _
    rw [Frobenius.pw_mul, h a (List.Mem.head as), prodY_ell f as (fun x hx => h x (List.Mem.tail a hx))]
    exact (CycRing.cyc K (n + 1)).one_mul _

include hg hmul hone hK h10 hpr hu1 in
/-- **ρ(u)^(Σ s(Y_i)) = ρ(u)^(c_i) for all i ⇒ ρ(u)^(Σ s(Π Y_i)) = ρ(u)^(Σ c_i)**. -/
theorem rho_sum_list {ι : Type} (f : ι → CycRing.C K (n + 1)) (c : ι → Nat) : ∀ xs : List ι,
    (∀ x, x ∈ xs → pw (CycRing.cyc K (n + 1)) (f x) (n + 1 + 1) = (CycRing.cyc K (n + 1)).one) →
    (∀ x, x ∈ xs → pw F (ρ (CycUnit.u K (n + 1))) (SymRho.sumN (EisRhoF.syF K n g F ρ dec (f x)) (n + 1))
      = pw F (ρ (CycUnit.u K (n + 1))) (c x)) →
    pw F (ρ (CycUnit.u K (n + 1))) (SymRho.sumN (EisRhoF.syF K n g F ρ dec (prodY K n (xs.map f))) (n + 1))
      = pw F (ρ (CycUnit.u K (n + 1))) ((xs.map c).foldr (· + ·) 0)
  | [], _, _ => rho_sum_one K n g hg F ρ hmul hone hK h10 dec hpr hu1
  | a :: as, h1, h2 => by
    show pw F _ (SymRho.sumN (EisRhoF.syF K n g F ρ dec ((CycRing.cyc K (n + 1)).mul (f a) (prodY K n (as.map f)))) (n + 1))
      = pw F _ (c a + (as.map c).foldr (· + ·) 0)
    rw [rho_sum_mul K n g hg F ρ hmul hone hK h10 dec hpr hu1 _ _ (h1 a (List.Mem.head as)) (prodY_ell K n f as (fun x hx => h1 x (List.Mem.tail a hx))),
      SymG.pw_add F, SymG.pw_add F, h2 a (List.Mem.head as),
      rho_sum_list f c as (fun x hx => h1 x (List.Mem.tail a hx)) (fun x hx => h2 x (List.Mem.tail a hx))]

/-- **φ_u(Π α_i)^M = Π φ_u(α_i)^M**. -/
theorem phz_prodV (M : Nat) : ∀ αs : List (V Int (n + 1 + 1)),
    pw (CycRing.cyc K (n + 1))
        (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) (EisAlphaMul.prodV n αs)) M
      = prodY K n (αs.map (fun a => pw (CycRing.cyc K (n + 1))
          (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) a) M))
  | [] => by
    show pw (CycRing.cyc K (n + 1))
        (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1)) (gring LibInt.intR (n + 1)).one) M
      = (CycRing.cyc K (n + 1)).one
    rw [CycEis.phz_one]
    exact TwoPower.pwone _ _
  | a :: as => by
    show pw (CycRing.cyc K (n + 1)) (CycEis.phz (CycRing.cyc K (n + 1)) (n + 1) (CycUnit.u K (n + 1))
        ((gring LibInt.intR (n + 1)).mul a (EisAlphaMul.prodV n as))) M
      = (CycRing.cyc K (n + 1)).mul _ (prodY K n (as.map _))
    rw [CycEis.phz_mul (CycRing.cyc K (n + 1)) (n + 1) _ (CycUnit.u_ell K (n + 1)), Frobenius.pw_mul, phz_prodV M as]

end Gen

/-- **Eisenstein's reciprocity at the primes of degree one above q, for products**: if each factor α_i satisfies
    z^(Σ s(φ_u(α_i)^M)) = z^(c_i) (lang/EisFq gives this with c_i = k_1(α_i)(ℓ−1) for primary primes of norm p),
    then z^(Σ s(φ_u(Π α_i)^M)) = z^(Σ c_i). -/
theorem eis_fq_mul (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (ms : Nat)
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (z : Zp (k' + 1)) (hz : pw (ring (k' + 1)) z (ms + 1 + 1 + 1 + 1) = (ring (k' + 1)).one) (hz1 : z ≠ (ring (k' + 1)).one)
    (M : Nat) (c : V Int (ms + 1 + 1 + 1 + 1) → Nat) (αs : List (V Int (ms + 1 + 1 + 1 + 1)))
    (hY : ∀ a, a ∈ αs → pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
        (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) a) M)
        (ms + 1 + 1 + 1 + 1) = (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one)
    (hrec : ∀ a, a ∈ αs → pw (ring (k' + 1)) z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z)
          (EisFq.decZ (k' + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) a) M))
          (ms + 1 + 1 + 1))
        = pw (ring (k' + 1)) z (c a)) :
    pw (ring (k' + 1)) z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z)
          (EisFq.decZ (k' + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
              (EisAlphaMul.prodV (ms + 1 + 1) αs)) M))
          (ms + 1 + 1 + 1))
      = pw (ring (k' + 1)) z ((αs.map c).foldr (· + ·) 0) := by
  have hs := EisFq.gs_z k' hq (ms + 1 + 1 + 1) z hz hz1
  have hu := CycEvalK.evK_u (ring (k' + 1)) (ms + 1 + 1 + 1) z hz hs
  have hu1 : CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) ≠ (ring (k' + 1)).one := by
    rw [hu]
    exact hz1
  rw [phz_prodV (ring (k' + 1)) (ms + 1 + 1) M αs]
  have h := rho_sum_list (ring (k' + 1)) (ms + 1 + 1) gl hgl.1 (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z)
    (CycEvalK.evK_mul (ring (k' + 1)) (ms + 1 + 1 + 1) z hz hs) (CycEvalK.evK_one (ring (k' + 1)) (ms + 1 + 1 + 1) z hz hs)
    (nozd k' hq) (EisFq.one_ne k') (EisFq.decZ (k' + 1)) hℓ hu1
    (fun a => pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
      (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) a) M)
    c αs hY (fun a ha => by rw [hu]; exact hrec a ha)
  rw [hu] at h
  exact h

end EisFqMul

#print axioms EisFqMul.rho_sum_mul
#print axioms EisFqMul.rho_sum_one
#print axioms EisFqMul.prodY_ell
#print axioms EisFqMul.rho_sum_list
#print axioms EisFqMul.phz_prodV
#print axioms EisFqMul.eis_fq_mul

lang/EisFqY.lean

/-
  lang/EisFqY — T49 (general ℓ): **φ_u(α)^M is an ℓ-th root of unity** in 𝔽_q[C_ℓ]/(N) whenever the norm of α is
  prime to q and q^e = ℓM + 1, so the hypothesis hY of lang/EisFqMul holds for every factor; with it the
  reciprocity for products of primary primes of norm p_i needs only the per-prime statements of lang/EisFq.
    q_inv        q · q^(ℓ−2) = 1 in 𝔽_ℓ
    y_ell_alpha  (φ_u(α)^M)^ℓ = 1
    eis_fq_prod  z^(Σ s(φ_u(Π α_i)^M)) = z^(Σ c_i) from the per-prime reciprocity alone
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.EisFqMul
import lib.RingTac
import lib.Semi

namespace EisFqY

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

/-- **q · q^(ℓ−2) = 1 in 𝔽_ℓ** (q ≢ 0 mod ℓ). -/
theorem q_inv (ms k' : Nat) (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero) :
    (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
      = (ring (ms + 1 + 1 + 1)).one := by
  have hf := NthEis.fermat_unit (ms + 1 + 1) hℓ _ hc0
  have h2 : (ring (ms + 1 + 1 + 1)).mul (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
      (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
      = pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1 + 1) := by
    show _ = (ring (ms + 1 + 1 + 1)).mul (pw (ring (ms + 1 + 1 + 1)) (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one) (ms + 1 + 1))
      (nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one)
    lib_ring
  exact h2.trans hf

/-- **(φ_u(α)^M)^ℓ = 1** for N(α) = P prime to q and q^e = ℓM + 1. -/
theorem y_ell_alpha (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (ms : Nat)
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (P : Nat) (hp : nsm (ring (k' + 1)) P (ring (k' + 1)).one ≠ (ring (k' + 1)).zero)
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1)) (h' : Nat)
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) P (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (e M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1) :
    pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
        (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
          (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M)
        (ms + 1 + 1 + 1 + 1) = (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)).one :=
  SymY.Y_ell (ms + 1 + 1 + 1) k' hq _ _ (q_inv ms k' hℓ hc0) rfl e (EisAlpha.q_order (ms + 1 + 1) (k' + 1 + 1) e M hqn) M hqn _
    (EisAlpha.unit_alpha k' hq (ms + 1 + 1) P hp α iv h' hNα)

/-- **Eisenstein's reciprocity at the primes of degree one above q, for products of elements of norm prime to q**:
    the per-prime statements z^(Σ s(φ_u(α_i)^M)) = z^(c_i) give z^(Σ s(φ_u(Π α_i)^M)) = z^(Σ c_i). -/
theorem eis_fq_prod (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (ms : Nat)
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (z : Zp (k' + 1)) (hz : pw (ring (k' + 1)) z (ms + 1 + 1 + 1 + 1) = (ring (k' + 1)).one) (hz1 : z ≠ (ring (k' + 1)).one)
    (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1)) (h' : Nat)
    (e M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (P c : V Int (ms + 1 + 1 + 1 + 1) → Nat) (αs : List (V Int (ms + 1 + 1 + 1 + 1)))
    (hp : ∀ a, a ∈ αs → nsm (ring (k' + 1)) (P a) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero)
    (hN : ∀ a, a ∈ αs → NormEval.nrm (ms + 1 + 1 + 1) a
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (P a) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (hrec : ∀ a, a ∈ αs → pw (ring (k' + 1)) z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z)
          (EisFq.decZ (k' + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) a) M))
          (ms + 1 + 1 + 1))
        = pw (ring (k' + 1)) z (c a)) :
    pw (ring (k' + 1)) z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl (ring (k' + 1)) (CycEvalK.evK (ring (k' + 1)) (ms + 1 + 1 + 1) z)
          (EisFq.decZ (k' + 1))
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1))
              (EisAlphaMul.prodV (ms + 1 + 1) αs)) M))
          (ms + 1 + 1 + 1))
      = pw (ring (k' + 1)) z ((αs.map c).foldr (· + ·) 0) :=
  EisFqMul.eis_fq_mul k' hq ms hℓ gl hgl z hz hz1 M c αs
    (fun a ha => y_ell_alpha k' hq ms hℓ hc0 (P a) (hp a ha) a iv h' (hN a ha) e M hqn) hrec

end EisFqY

#print axioms EisFqY.q_inv
#print axioms EisFqY.y_ell_alpha
#print axioms EisFqY.eis_fq_prod

#print axioms: 9 theorems, no axioms.

78.388 Eisenstein reciprocity at the primes of any degree above \(q\)

Base change of \(K[C_\ell ]/(N)\) along a ring map \(K\to F\), the embedding \(\mathbb F_q\to F\) into a ring of characteristic \(q\), and evaluation at \(z\in F\) of order \(\ell \) give the residue map \(\rho _z\) at a prime of any residue degree; the computed symbols of a primary prime \(\alpha \) of norm \(p\) again multiply to \((q^{-e}/\alpha )^{\ell -1}\).

\[ z^{\sum _{i<\ell -1}s_i}=z^{k_1(\ell -1)},\qquad z\in F\supseteq \mathbb F_q,\ z^\ell =1\ne z. \]

lang/CycBase.lean

/-
  lang/CycBase — T49 (residue fields of degree f > 1): **base change of K[C_ℓ]/(N) along a ring map ι : K → F**,
  and the evaluation K[C_ℓ]/(N) → F at z ∈ F with z^ℓ = 1, Σ_(a<ℓ) z^a = 0: a ring map sending u to z. With
  K = 𝔽_q and F a field of order q^f containing an element of order ℓ, this is the residue map at a prime of
  degree f above q, so lang/EisRhoF applies to every prime above q, not only those of degree one.
    hom_zero, hom_neg, hom_sumK, hom_delta     a ring map preserves 0, −, Σ, δ
    mapV, gt_mapV, mapC, gt_mapC               coefficientwise image
    mapC_add, mapC_mul, mapC_one, mapC_u       mapC is a ring map with u ↦ u
    evI, evI_mul, evI_one, evI_u               evI = evK_z ∘ mapC
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.CycEvalK
import lib.RingTac

namespace CycBase

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

variable {R T : Type} (K : CRing R) (F : CRing T) (ι : R → T)
  (hadd : ∀ x y, ι (K.add x y) = F.add (ι x) (ι y)) (hmul : ∀ x y, ι (K.mul x y) = F.mul (ι x) (ι y))
  (hone : ι K.one = F.one)

include hadd in
theorem hom_zero : ι K.zero = F.zero := by
  have h : ι K.zero = F.add (ι K.zero) (ι K.zero) := by
    rw [← hadd, K.zero_add]
  calc ι K.zero = F.add (F.add (ι K.zero) (ι K.zero)) (F.neg (ι K.zero)) := by lib_ring
    _ = F.add (ι K.zero) (F.neg (ι K.zero)) := by rw [← h]
    _ = F.zero := F.add_neg _

include hadd in
theorem hom_neg (x : R) : ι (K.neg x) = F.neg (ι x) := by
  have h : F.add (ι x) (ι (K.neg x)) = F.zero := by
    rw [← hadd, K.add_neg, hom_zero K F ι hadd]
  calc ι (K.neg x) = F.add (F.add (ι x) (ι (K.neg x))) (F.neg (ι x)) := by lib_ring
    _ = F.add F.zero (F.neg (ι x)) := by rw [h]
    _ = F.neg (ι x) := F.zero_add _

include hadd in
theorem hom_sumK {A : Type} (f : A → R) : ∀ l : List A, ι (sumK K f l) = sumK F (fun a => ι (f a)) l
  | [] => hom_zero K F ι hadd
  | a :: as => by
    show ι (K.add (f a) (sumK K f as)) = F.add (ι (f a)) (sumK F (fun a => ι (f a)) as)
    rw [hadd, hom_sumK f as]

include hadd hone in
theorem hom_delta {G : Type} [DecidableEq G] (y z : G) : ι (delta K y z) = delta F y z := by
  unfold FiniteRecChain.delta
  by_cases h : y = z
  · rw [ite_pos' _ _ h, ite_pos' _ _ h]
    exact hone
  · rw [ite_neg' _ _ h, ite_neg' _ _ h]
    exact hom_zero K F ι hadd

variable (n : Nat)

/-- the coefficientwise image of an element of K[C_ℓ]. -/
def mapV (u : V R (n + 1)) : V T (n + 1) := tb n (fun a => ι (gt K n u a))

theorem gt_mapV (u : V R (n + 1)) (a : Zp n) : gt F n (mapV K ι n u) a = ι (gt K n u a) := gt_tb F n _ a

/-- **the base change K[C_ℓ]/(N) → F[C_ℓ]/(N)**. -/
def mapC (x : CycRing.C K n) : CycRing.C F n := CycRing.qmap F n (mapV K ι n x.val)

include hadd in
theorem gt_mapC (x : CycRing.C K n) (a : Zp n) : gt F n (mapC K F ι n x).val a = ι (gt K n x.val a) := by
  show gt F n (CycRing.red F n (mapV K ι n x.val)) a = _
  rw [CycRing.gt_red, gt_mapV, gt_mapV, x.property, hom_zero K F ι hadd]
  lib_ring

include hadd in
theorem mapC_add (x y : CycRing.C K n) :
    mapC K F ι n ((CycRing.cyc K n).add x y) = (CycRing.cyc F n).add (mapC K F ι n x) (mapC K F ι n y) :=
  CycRing.cext F n _ _ (GroupRing.ext F n _ _ (fun a => by
    rw [gt_mapC K F ι hadd n]
    have e1 : gt K n ((CycRing.cyc K n).add x y).val a = K.add (gt K n x.val a) (gt K n y.val a) :=
      gt_tb K n (fun c => K.add (gt K n x.val c) (gt K n y.val c)) a
    have e2 : gt F n ((CycRing.cyc F n).add (mapC K F ι n x) (mapC K F ι n y)).val a
        = F.add (gt F n (mapC K F ι n x).val a) (gt F n (mapC K F ι n y).val a) :=
      gt_tb F n (fun c => F.add (gt F n (mapC K F ι n x).val c) (gt F n (mapC K F ι n y).val c)) a
    rw [e1, e2, hadd, gt_mapC K F ι hadd n, gt_mapC K F ι hadd n]))

include hadd hmul in
theorem hom_gmul (x y : CycRing.C K n) (c : Zp n) :
    ι (gt K n (gmul K n x.val y.val) c) = gt F n (gmul F n (mapC K F ι n x).val (mapC K F ι n y).val) c := by
  rw [GroupRing.gt_mul, GroupRing.gt_mul, hom_sumK K F ι hadd]
  exact sumK_ext F _ _ (fun b => by rw [hmul, gt_mapC K F ι hadd n, gt_mapC K F ι hadd n]) (L n)

include hadd hmul in
theorem mapC_mul (x y : CycRing.C K n) :
    mapC K F ι n ((CycRing.cyc K n).mul x y) = (CycRing.cyc F n).mul (mapC K F ι n x) (mapC K F ι n y) :=
  CycRing.cext F n _ _ (GroupRing.ext F n _ _ (fun a => by
    rw [gt_mapC K F ι hadd n]
    show ι (gt K n (CycRing.red K n (gmul K n x.val y.val)) a)
      = gt F n (CycRing.red F n (gmul F n (mapC K F ι n x).val (mapC K F ι n y).val)) a
    rw [CycRing.gt_red, CycRing.gt_red, hadd, hom_neg K F ι hadd, hom_gmul K F ι hadd hmul n,
      hom_gmul K F ι hadd hmul n]))

include hadd hone in
theorem mapC_one : mapC K F ι n (CycRing.cyc K n).one = (CycRing.cyc F n).one :=
  CycRing.cext F n _ _ (GroupRing.ext F n _ _ (fun a => by
    rw [gt_mapC K F ι hadd n]
    show ι (gt K n (CycRing.red K n (gone K n)) a) = gt F n (CycRing.red F n (gone F n)) a
    rw [CycRing.gt_red, CycRing.gt_red, hadd, hom_neg K F ι hadd]
    have e1 : ∀ c, gt K n (gone K n) c = delta K c (ring n).zero := fun c => gt_tb K n _ c
    have e2 : ∀ c, gt F n (gone F n) c = delta F c (ring n).zero := fun c => gt_tb F n _ c
    rw [e1, e1, e2, e2, hom_delta K F ι hadd hone, hom_delta K F ι hadd hone]))

include hadd hmul hone in
theorem mapC_u : mapC K F ι n (CycUnit.u K n) = CycUnit.u F n :=
  CycRing.cext F n _ _ (GroupRing.ext F n _ _ (fun a => by
    rw [gt_mapC K F ι hadd n]
    show ι (gt K n (CycRing.red K n (mono K n (ring n).one K.one)) a)
      = gt F n (CycRing.red F n (mono F n (ring n).one F.one)) a
    rw [CycRing.gt_red, CycRing.gt_red, hadd, hom_neg K F ι hadd, gt_mono, gt_mono, gt_mono, gt_mono, hmul, hmul, hone,
      hom_delta K F ι hadd hone, hom_delta K F ι hadd hone]))

variable (z : T)

/-- **evaluation at z ∈ F after base change**. -/
def evI (x : CycRing.C K n) : T := CycEvalK.evK F n z (mapC K F ι n x)

include hadd hmul in
theorem evI_mul (hz : pw F z (n + 1) = F.one) (hs : CycChar.gs F z (n + 1) = F.zero) (x y : CycRing.C K n) :
    evI K F ι n z ((CycRing.cyc K n).mul x y) = F.mul (evI K F ι n z x) (evI K F ι n z y) := by
  unfold evI
  rw [mapC_mul K F ι hadd hmul n]
  exact CycEvalK.evK_mul F n z hz hs _ _

include hadd hone in
theorem evI_one (hz : pw F z (n + 1) = F.one) (hs : CycChar.gs F z (n + 1) = F.zero) :
    evI K F ι n z (CycRing.cyc K n).one = F.one := by
  unfold evI
  rw [mapC_one K F ι hadd hone n]
  exact CycEvalK.evK_one F n z hz hs

include hadd hmul hone in
theorem evI_u (hz : pw F z (n + 1) = F.one) (hs : CycChar.gs F z (n + 1) = F.zero) :
    evI K F ι n z (CycUnit.u K n) = z := by
  unfold evI
  rw [mapC_u K F ι hadd hmul hone n]
  exact CycEvalK.evK_u F n z hz hs

end CycBase

#print axioms CycBase.hom_zero
#print axioms CycBase.hom_neg
#print axioms CycBase.hom_sumK
#print axioms CycBase.hom_delta
#print axioms CycBase.gt_mapV
#print axioms CycBase.gt_mapC
#print axioms CycBase.mapC_add
#print axioms CycBase.hom_gmul
#print axioms CycBase.mapC_mul
#print axioms CycBase.mapC_one
#print axioms CycBase.mapC_u
#print axioms CycBase.evI_mul
#print axioms CycBase.evI_one
#print axioms CycBase.evI_u

lang/FpEmb.lean

/-
  lang/FpEmb — T49 (residue fields of degree f > 1): **the embedding 𝔽_p → F into any ring of characteristic p**,
  a ↦ a·1. With lang/CycBase it carries 𝔽_p[C_ℓ]/(N) into F[C_ℓ]/(N), so a field F ⊇ 𝔽_p containing an element of
  order ℓ gives the residue map at a prime above p of any degree.
    emb_md     (md n)·1 = n·1          (p·1 = 0)
    emb_add, emb_mul, emb_one          a ↦ a·1 is a ring map 𝔽_p → F
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.Frobenius
import lang.ZMod

namespace FpEmb

open FTAChain ZMod DetChain Frobenius

variable {T : Type} (F : CRing T) (m : Nat) (hp : nsm F (m + 1) F.one = F.zero)

/-- a ↦ a·1. -/
def emb (a : Zp m) : T := nsm F a.val F.one

include hp in
theorem emb_md (n : Nat) : nsm F (md m n) F.one = nsm F n F.one := by
  obtain ⟨q, hq⟩ := md_spec m n
  calc nsm F (md m n) F.one = F.add (nsm F (md m n) F.one) F.zero := by
        rw [F.add_comm]
        exact (F.zero_add _).symm
    _ = F.add (nsm F (md m n) F.one) (nsm F (q * (m + 1)) F.one) := by rw [nsm_char F (m + 1) q hp F.one]
    _ = nsm F (md m n + q * (m + 1)) F.one := (nsm_add F _ _ _).symm
    _ = nsm F n F.one := by rw [← hq]

include hp in
theorem emb_add (a b : Zp m) : emb F m ((ring m).add a b) = F.add (emb F m a) (emb F m b) := by
  show nsm F (md m (a.val + b.val)) F.one = _
  rw [emb_md F m hp]
  exact nsm_add F a.val b.val F.one

include hp in
theorem emb_mul (a b : Zp m) : emb F m ((ring m).mul a b) = F.mul (emb F m a) (emb F m b) := by
  show nsm F (md m (a.val * b.val)) F.one = _
  rw [emb_md F m hp, nsm_mul_nat F a.val F.one b.val, nsm_eq_mul F b.val]
  exact F.mul_comm _ _

include hp in
theorem emb_one : emb F m (ring m).one = F.one := by
  show nsm F (md m 1) F.one = F.one
  rw [emb_md F m hp]
  exact nsm_one F F.one

end FpEmb

#print axioms FpEmb.emb_md
#print axioms FpEmb.emb_add
#print axioms FpEmb.emb_mul
#print axioms FpEmb.emb_one

lang/EisFqF.lean

/-
  lang/EisFqF — T49 (residue fields of degree f > 1): **Eisenstein's reciprocity at every prime above q**.
  For any ring F ⊇ 𝔽_q (characteristic q, no zero divisors) and z ∈ F of order ℓ, the base change
  𝔽_q[C_ℓ]/(N) → F[C_ℓ]/(N) followed by evaluation at z (lang/CycBase, lang/FpEmb) is a ring map ρ_z with ρ_z(u) = z;
  ρ_z∘τ(g^i) runs over the primes above q of residue degree f = ord_ℓ(q) (F = 𝔽_(q^f)), and for a primary prime α
  of norm p the computed symbols satisfy z^(Σ_(i<ℓ−1) s_i) = z^(k_1(ℓ−1)), u^(k_1) = (q^(−e)/α)_ℓ.
    eis_fqF
  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.EisFq
import lang.CycBase
import lang.FpEmb

namespace EisFqF

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob

/-- **Eisenstein's reciprocity at the primes of any degree above q**: F ⊇ 𝔽_q without zero divisors, z ∈ F of order ℓ. -/
theorem eis_fqF (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hq2 : k' + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs' : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α)
    {T : Type} (F : CRing T) (hF : NoZD F) (h10 : F.one ≠ F.zero) (dec : DecidableEq T)
    (hpF : nsm F (k' + 1 + 1) F.one = F.zero)
    (z : T) (hz : pw F z (ms + 1 + 1 + 1 + 1) = F.one) (hz1 : z ≠ F.one) :
    pw F z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl F (CycBase.evI (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) (ms + 1 + 1 + 1) z) dec
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
          (ms + 1 + 1 + 1))
      = pw F z ((1 * k 1) * (ms + 1 + 1 + 1)) := by
  have hs : CycChar.gs F z (ms + 1 + 1 + 1 + 1) = F.zero := CycEis.gs_zero F hF z hz1 (ms + 1 + 1 + 1 + 1) hz
  have hadd := FpEmb.emb_add F (k' + 1) hpF
  have hmul := FpEmb.emb_mul F (k' + 1) hpF
  have hone := FpEmb.emb_one F (k' + 1) hpF
  have hu := CycBase.evI_u (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) hadd hmul hone (ms + 1 + 1 + 1) z hz hs
  have h := EisRhoF.eis_rho_field j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq hq2 e hQ M hqn hp s hs' k hk gl hgl hc0
    hprim F (CycBase.evI (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) (ms + 1 + 1 + 1) z)
    (CycBase.evI_mul (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) hadd hmul (ms + 1 + 1 + 1) z hz hs)
    (CycBase.evI_one (ring (k' + 1)) F (FpEmb.emb F (k' + 1)) hadd hone (ms + 1 + 1 + 1) z hz hs) hF h10 dec (by rw [hu]; exact hz1)
  rw [hu] at h
  exact h

end EisFqF

#print axioms EisFqF.eis_fqF

#print axioms: 19 theorems, no axioms.

78.389 Residue fields of order \(q^{2^k}\)

A finite field of the interface of order \(q^{N+1}\) has characteristic \(q\); the quadratic tower has order \(q^{2^k}\), and when \(\ell \mid q^{N+1}-1\) it contains an element of order \(\ell \), so the reciprocity at primes of any degree applies in it.

\[ |F|=q^{N+1}\Rightarrow q\cdot 1_F=0,\qquad |F_k|=q^{2^k},\qquad \ell \mid |F^\times |\Rightarrow z^{\sum _i s_i}=z^{k_1(\ell -1)}. \]

lang/FFChar.lean

/-
  lang/FFChar — T49 (residue fields of degree f > 1): **the characteristic of a finite field of the interface**.
  Translation x ↦ x + 1 permutes the element list, so |F|·1 = 0; if |F| = q^(N+1) then (q·1)^(N+1) = 0 and, without
  zero divisors, q·1 = 0. This is the hypothesis p·1 = 0 of lang/FpEmb for the fields 𝔽_(q^(2^k)) of lang/FieldTower.
    len_count     Σ_(x∈l) 1 = |l| in ℤ
    sum_one       Σ_(x∈l) 1 = |l|·1 in any ring
    nsm_pow_any   (a^e)·1 = (a·1)^e
    pw_zero_eq    x^(N+1) = 0 ⇒ x = 0          (no zero divisors)
    char_card     |F|·1 = 0
    char_q        |F| = q^(N+1) ⇒ q·1 = 0
  Lean 4 core only, proofs by hand (lib_ring), no axioms.
-/
import lang.FinPowerSum
import lib.RingTac

namespace FFChar

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius FinField

theorem len_count {A : Type} : ∀ l : List A, sumK LibInt.intR (fun _ => (1 : Int)) l = Int.ofNat l.length
  | [] => rfl
  | _ :: as => by
    show LibInt.intR.add 1 (sumK LibInt.intR (fun _ => (1 : Int)) as) = Int.ofNat (as.length + 1)
    rw [len_count as]
    exact LibInt.intR.add_comm _ _

theorem sum_one {R : Type} (K : CRing R) {A : Type} : ∀ l : List A, sumK K (fun _ => K.one) l = nsm K l.length K.one
  | [] => rfl
  | _ :: as => by
    show K.add K.one (sumK K (fun _ => K.one) as) = K.add (nsm K as.length K.one) K.one
    rw [sum_one K as]
    exact K.add_comm _ _

theorem nsm_pow_any {R : Type} (K : CRing R) (a : Nat) : ∀ e, nsm K (a ^ e) K.one = pw K (nsm K a K.one) e
  | 0 => by
    show nsm K 1 K.one = K.one
    exact nsm_one K K.one
  | e + 1 => by
    show nsm K (a ^ e * a) K.one = K.mul (pw K (nsm K a K.one) e) (nsm K a K.one)
    rw [nsm_mul_nat K (a ^ e) K.one a, nsm_eq_mul K a (nsm K (a ^ e) K.one), nsm_pow_any K a e]
    exact K.mul_comm _ _

theorem pw_zero_eq {R : Type} (K : CRing R) (hK : NoZD K) (x : R) : ∀ N, pw K x (N + 1) = K.zero → x = K.zero
  | 0, h => by
    have e : pw K x 1 = K.mul K.one x := rfl
    rw [e, K.one_mul] at h
    exact h
  | N + 1, h => by
    cases hK (pw K x (N + 1)) x h with
    | inl h1 => exact pw_zero_eq K hK x N h1
    | inr h2 => exact h2

variable {R : Type} [DecidableEq R] (F : FF R)

/-- **|F|·1 = 0**. -/
theorem char_card : nsm F.K (2 * F.h + 1) F.K.one = F.K.zero := by
  have hlen : F.els.length = 2 * F.h + 1 := Int.ofNat.inj ((len_count F.els).symm.trans F.card)
  have hr := reindex F.K F.els (FinPowerSum.compK F) (fun x => F.K.add x F.K.one) (fun y => F.K.add y (F.K.neg F.K.one))
    (fun x => by
      show F.K.add (F.K.add x F.K.one) (F.K.neg F.K.one) = x
      lib_ring) (fun y => by
      show F.K.add (F.K.add y (F.K.neg F.K.one)) F.K.one = y
      lib_ring) (fun x => x)
  have hs : sumK F.K (fun x => F.K.add x F.K.one) F.els = F.K.add (sumK F.K (fun x => x) F.els) (sumK F.K (fun _ => F.K.one) F.els) :=
    sumK_add F.K (fun x => x) (fun _ => F.K.one) F.els
  rw [sum_one F.K F.els, hlen] at hs
  have h2 : F.K.add (sumK F.K (fun x => x) F.els) (nsm F.K (2 * F.h + 1) F.K.one) = sumK F.K (fun x => x) F.els :=
    hs.symm.trans hr
  generalize sumK F.K (fun x => x) F.els = S at h2
  generalize nsm F.K (2 * F.h + 1) F.K.one = c at h2 ⊢
  calc c = F.K.add (F.K.add S c) (F.K.neg S) := by lib_ring
    _ = F.K.add S (F.K.neg S) := by rw [h2]
    _ = F.K.zero := F.K.add_neg _

/-- **|F| = q^(N+1) ⇒ q·1 = 0**. -/
theorem char_q (q N : Nat) (hq : 2 * F.h + 1 = q ^ (N + 1)) : nsm F.K q F.K.one = F.K.zero := by
  have h := char_card F
  rw [hq, nsm_pow_any F.K q (N + 1)] at h
  exact pw_zero_eq F.K F.nozd _ N h

end FFChar

#print axioms FFChar.len_count
#print axioms FFChar.sum_one
#print axioms FFChar.nsm_pow_any
#print axioms FFChar.pw_zero_eq
#print axioms FFChar.char_card
#print axioms FFChar.char_q

lang/TowerCard.lean

/-
  lang/TowerCard — T49 (residue fields of degree f > 1): **the field of level k of lang/FieldTower has order
  q^(2^k)**, q = 2m + 3. With lang/FFChar (q·1 = 0) and FinCyclic.split_pow_ff (ℓ | q^(2^k) − 1 gives an element of
  order ℓ) it is a residue field for every prime above q whose degree divides 2^k.
    pow_add', pow_sq     q^(a+b) = q^a·q^b, (q^a)^2 = q^(2a)
    tower_card           2h_k + 1 = q^(2^k)
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.FieldTower

namespace TowerCard

open FTAChain ZMod YMChain FinField FieldTower

theorem pow_add' (q a : Nat) : ∀ b, q ^ (a + b) = q ^ a * q ^ b
  | 0 => by
    show q ^ a = q ^ a * 1
    lib_nat
  | b + 1 => by
    show q ^ (a + b) * q = q ^ a * (q ^ b * q)
    rw [pow_add' q a b]
    lib_nat

theorem pow_sq (q a : Nat) : q ^ a * q ^ a = q ^ (2 * a) := by
  rw [show 2 * a = a + a by lib_nat, pow_add']

theorem step_card (m k : Nat) (L : Level m k) (h : 2 * L.F.h + 1 = (2 * m + 1 + 1 + 1) ^ (2 ^ k)) :
    2 * (match L with
      | ⟨F, j, hj⟩ =>
        match @step _ (instDec m k) F j hj with
        | ⟨G, hG⟩ => (⟨G, 2 * j * j + 6 * j + 3, hG⟩ : Level m (k + 1))).F.h + 1
      = (2 * m + 1 + 1 + 1) ^ (2 ^ (k + 1)) := by
  match L, h with
  | ⟨F, j, hj⟩, h =>
    show 2 * (match @step _ (instDec m k) F j hj with
        | ⟨G, hG⟩ => (⟨G, 2 * j * j + 6 * j + 3, hG⟩ : Level m (k + 1))).F.h + 1 = _
    generalize @step _ (instDec m k) F j hj = st
    match st with
    | ⟨G, hG⟩ =>
      show 2 * G.h + 1 = _
      have hF : 2 * F.h + 1 = 2 * (j + 1) + 1 := by rw [hj]
      rw [hG, order_sq, ← hF, h, pow_sq]
      show (2 * m + 1 + 1 + 1) ^ (2 * 2 ^ k) = (2 * m + 1 + 1 + 1) ^ (2 ^ k * 2)
      rw [show 2 * 2 ^ k = 2 ^ k * 2 by lib_nat]

/-- **|F_k| = q^(2^k)**. -/
theorem tower_card (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    ∀ k, 2 * (tower m hpr k).F.h + 1 = (2 * m + 1 + 1 + 1) ^ (2 ^ k)
  | 0 => by
    show 2 * (m + 1) + 1 = (2 * m + 1 + 1 + 1) ^ 1
    show 2 * (m + 1) + 1 = 1 * (2 * m + 1 + 1 + 1)
    lib_nat
  | k + 1 => step_card m k (tower m hpr k) (tower_card m hpr k)

end TowerCard

#print axioms TowerCard.pow_add'
#print axioms TowerCard.pow_sq
#print axioms TowerCard.step_card
#print axioms TowerCard.tower_card

lang/EisFqFF.lean

/-
  lang/EisFqFF — T49 (residue fields of degree f > 1): **Eisenstein's reciprocity in every finite field of the
  interface of order q^(N+1) with ℓ | q^(N+1) − 1**: such a field has characteristic q (lang/FFChar), an element z of
  order ℓ (FinCyclic.split_pow_ff), and the residue map ρ_z of lang/EisFqF. With the tower 𝔽_(q^(2^k)) of
  lang/FieldTower (order lang/TowerCard) this covers every prime above q whose residue degree divides 2^k.
    eis_ff
  Lean 4 core only, proofs by hand (lib_ring, lib_nat), no axioms.
-/
import lang.EisFqF
import lang.FFChar
import lib.RingTac

namespace EisFqFF

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob FinField

/-- **Eisenstein's reciprocity in a finite field of order q^(N+1) with ℓ | q^(N+1) − 1**: an element z of order ℓ
    exists, and the computed symbols at the primes ρ_z∘τ(g^i) satisfy z^(Σ s_i) = z^(k_1(ℓ−1)). -/
theorem eis_ff (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hN : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (k' : Nat) (hq : ∀ d, Dv d (k' + 1 + 1) → d = 1 ∨ d = k' + 1 + 1) (hq2 : k' + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (k' + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (k' + 1)) (j + 1 + 1 + 1) (ring (k' + 1)).one ≠ (ring (k' + 1)).zero) (s : Nat) (hs' : M = (k' + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((k' + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (k' + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α)
    {R : Type} [dec : DecidableEq R] (F : FF R) (N : Nat) (hcard : 2 * F.h + 1 = (k' + 1 + 1) ^ (N + 1))
    (hdv : Dv (ms + 1 + 1 + 1 + 1) (2 * F.h)) :
    ∃ z, pw F.K z (ms + 1 + 1 + 1 + 1) = F.K.one ∧ z ≠ F.K.one ∧ pw F.K z
        (SymRho.sumN (EisRhoF.syF (ring (k' + 1)) (ms + 1 + 1) gl F.K (CycBase.evI (ring (k' + 1)) F.K (FpEmb.emb F.K (k' + 1)) (ms + 1 + 1 + 1) z) dec
          (pw (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (k' + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (k' + 1)) (ms + 1 + 1 + 1)) α) M))
          (ms + 1 + 1 + 1))
      = pw F.K z ((1 * k 1) * (ms + 1 + 1 + 1)) := by
  have h10 : F.K.one ≠ F.K.zero := fun e => F.ne_pm (by
    rw [e]
    show F.K.neg F.K.zero = F.K.zero
    lib_ring)
  have hpF := FFChar.char_q F (k' + 1 + 1) N hcard
  have hd : Dv ((ms + 1 + 1 + 1 + 1) ^ (0 + 1)) (2 * F.h) := by
    rw [show (ms + 1 + 1 + 1 + 1) ^ (0 + 1) = ms + 1 + 1 + 1 + 1 by
      show 1 * (ms + 1 + 1 + 1 + 1) = ms + 1 + 1 + 1 + 1
      lib_nat]
    exact hdv
  obtain ⟨y, hy1, hy0⟩ := FinCyclic.split_pow_ff F (ms + 1 + 1) 0 hd
  have hz : pw F.K y (ms + 1 + 1 + 1 + 1) = F.K.one := by
    rw [show ms + 1 + 1 + 1 + 1 = (ms + 1 + 1 + 1 + 1) ^ (0 + 1) by
      show ms + 1 + 1 + 1 + 1 = 1 * (ms + 1 + 1 + 1 + 1)
      lib_nat]
    exact hy1
  have hz1 : y ≠ F.K.one := fun e => hy0 (by
    show F.K.mul F.K.one y = F.K.one
    rw [e]
    exact F.K.one_mul _)
  exact ⟨y, hz, hz1, EisFqF.eis_fqF j Mp ms t h' hM hpr g hg hℓ hN hh α iv hNα h0 k' hq hq2 e hQ M hqn hp s hs' k hk gl hgl hc0
    hprim F.K F.nozd h10 dec hpF y hz hz1⟩

end EisFqFF

#print axioms EisFqFF.eis_ff

#print axioms: 11 theorems, no axioms.

78.390 Eisenstein reciprocity at primes of degree \(2^a3^b\)

Every cubic step of the mixed tower cubes the order, so the field of level \((L,b)\) has order exactly \(q^{2^{L+1}3^b}\); if the residue degree \(f\) divides \(2^{L+1}3^b\) then \(\ell \mid q^{2^{L+1}3^b}-1\) and the reciprocity holds in that field.

\[ |F_{L,b}|=q^{2^{L+1}3^b},\qquad f\mid 2^{L+1}3^b\ \Longrightarrow \ z^{\sum _i s_i}=z^{k_1(\ell -1)}. \]

lang/MixedCard.lean

/-
  lang/MixedCard — T49 (residue fields of degree f > 1): **exact orders of the mixed tower**. The quadratic tower
  TowerPow.tower2 has order q^(2^L), and every cubic step of lang/MixedTower cubes the order, so the mixed tower
  carries a finite field of order exactly q^(2^(L+1)·3^b) (q = 2m + 3 prime, q not 0 mod 3).
    tower2_card      2h_L + 1 = q^(2^L)
    step3c           a cubic step keeps Good3' and gives 2h' + 1 = (2h + 1)^3
    pow_cube         q^(AB)·(q^(AB)·q^(AB)) = q^(A(3B))
    mixed_card       ∃ G on C m L b, Good3' q G and 2h + 1 = q^(2^(L+1)·3^b)
  Lean 4 core only, proofs by hand (lib_nat, lib_int), no axioms.
-/
import lang.MixedTower
import lang.TowerCard

namespace MixedCard

open FTAChain ZMod YMChain FiniteRecChain Frobenius GaussInt GaussFactor FieldTower TowerPow3 MixedTower
open LibRing (castZ castN)

/-- **|tower2_L| = q^(2^L)**. -/
theorem tower2_card (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) :
    ∀ L, 2 * (TowerPow.tower2 m hpr L).F.h + 1 = (2 * m + 1 + 1 + 1) ^ (2 ^ L)
  | 0 => by
    rw [(TowerPow.tower2 m hpr 0).hj]
    show 2 * (m + 1) + 1 = 1 * (2 * m + 1 + 1 + 1)
    lib_nat
  | L + 1 => by
    rw [(TowerPow.tower2 m hpr (L + 1)).hj]
    show 2 * ((2 * (TowerPow.tower2 m hpr L).j * (TowerPow.tower2 m hpr L).j + 6 * (TowerPow.tower2 m hpr L).j + 3) + 1) + 1
      = (2 * m + 1 + 1 + 1) ^ (2 ^ (L + 1))
    rw [order_sq, ← (TowerPow.tower2 m hpr L).hj, tower2_card m hpr L, TowerCard.pow_sq]
    show (2 * m + 1 + 1 + 1) ^ (2 * 2 ^ L) = (2 * m + 1 + 1 + 1) ^ (2 ^ L * 2)
    rw [show 2 * 2 ^ L = 2 ^ L * 2 by lib_nat]

/-- A cubic step with its order. -/
theorem step3c {R : Type} [DecidableEq R] (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (F : FinField.FF R) (hG : Good3' (j + 1 + 1) F) :
    ∃ G : FinField.FF (R × R × R), Good3' (j + 1 + 1) G ∧
      2 * G.h + 1 = (2 * F.h + 1) * ((2 * F.h + 1) * (2 * F.h + 1)) := by
  match hG with
  | ⟨⟨hc, hz, ⟨N, hN⟩⟩, ⟨m, hm⟩⟩ =>
    have hq : FrobIter.npow (j + 1 + 1) (N + 1) = 2 * F.h + 1 :=
      Int.ofNat.inj ((FrobIter.npow_int _ _).trans hN.symm)
    match GenCubicExt.cubic_ext F j hpr hc (N + 1) hq m hm with
    | ⟨G, hs, hgc, hgz⟩ =>
      refine ⟨G, ⟨⟨hgc, fun z h => hz z (hgz z h), ⟨N + 1 + (N + 1) + N, ?_⟩⟩, ⟨9 * m * m * m + 9 * m * m + 3 * m, ?_⟩⟩, hs⟩
      · rw [show N + 1 + (N + 1) + N + 1 = (N + 1) * 3 by lib_nat, CubicTower.ipow_mul, ← hN, hs]
        show Int.ofNat (2 * F.h + 1) * (Int.ofNat (2 * F.h + 1) * Int.ofNat (2 * F.h + 1)) = 1 * Int.ofNat (2 * F.h + 1) * Int.ofNat (2 * F.h + 1) * Int.ofNat (2 * F.h + 1)
        lib_int
      · have e : 2 * G.h + 1 = (3 * m + 1) * ((3 * m + 1) * (3 * m + 1)) := by rw [hs, hm]
        apply FermatUnique.add_cancel 1
        rw [Nat.add_comm 1 (2 * G.h), e]; lib_nat

theorem pow_cube (q A B : Nat) : q ^ (A * B) * (q ^ (A * B) * q ^ (A * B)) = q ^ (A * (B * 3)) := by
  rw [show A * (B * 3) = A * B + (A * B + A * B) by lib_nat, TowerCard.pow_add', TowerCard.pow_add']

/-- **The mixed tower has order q^(2^(L+1)·3^b)**. -/
theorem mixed_card (m : Nat) (hpr : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = (2 * m + 1 + 1 + 1))
    (h3 : (∃ t, m = 3 * t + 1) ∨ (∃ t, m = 3 * t + 2)) (L : Nat) :
    ∀ b, ∃ G : @FinField.FF (C m L b) (instDecC m L b), @Good3' _ (instDecC m L b) (2 * m + 1 + 1 + 1) G ∧
      2 * G.h + 1 = (2 * m + 1 + 1 + 1) ^ (2 ^ (L + 1) * 3 ^ b)
  | 0 => by
    refine ⟨(TowerPow.tower2 m hpr (L + 1)).F, ⟨good3_tower m hpr (L + 1), level_mod3 m hpr h3 L⟩, ?_⟩
    rw [tower2_card m hpr (L + 1)]
    show (2 * m + 1 + 1 + 1) ^ (2 ^ (L + 1)) = (2 * m + 1 + 1 + 1) ^ (2 ^ (L + 1) * 1)
    rw [show 2 ^ (L + 1) * 1 = 2 ^ (L + 1) by lib_nat]
  | b + 1 => by
    match mixed_card m hpr h3 L b with
    | ⟨G, hG, hc⟩ =>
      match @step3c _ (instDecC m L b) (2 * m + 1) hpr G hG with
      | ⟨G', hG', hc'⟩ =>
        refine ⟨G', hG', ?_⟩
        rw [hc', hc]
        exact pow_cube (2 * m + 1 + 1 + 1) (2 ^ (L + 1)) (3 ^ b)

end MixedCard

#print axioms MixedCard.tower2_card
#print axioms MixedCard.step3c
#print axioms MixedCard.pow_cube
#print axioms MixedCard.mixed_card

lang/EisFqMixed.lean

/-
  lang/EisFqMixed — T49 (residue fields of degree f > 1): **Eisenstein's reciprocity at every prime above q whose
  residue degree f has only the prime factors 2 and 3** (q = 2m + 3, q not 0 mod 3): the mixed tower field of order
  q^(2^(L+1)·3^b) (lang/MixedCard) with f | 2^(L+1)·3^b satisfies ℓ | |F^×|, so lang/EisFqFF applies.
    pow_one_mul      q^e = ℓM + 1 ⇒ q^(ec) = ℓM' + 1
    pow_succ_ex      (x+1)^n = N + 1
    mixed_pos        2^(L+1)·3^b = N + 1
    eis_mixed
  Lean 4 core only, proofs by hand (lib_nat), no axioms.
-/
import lang.EisFqFF
import lang.MixedCard

namespace EisFqMixed

open FTAChain ZMod YMChain FiniteRecChain DetChain Frobenius GroupRing GroupRingFrob FinField MixedTower

theorem pow_one_mul (q l e M : Nat) (h : q ^ e = l * M + 1) : ∀ c, ∃ M', q ^ (e * c) = l * M' + 1
  | 0 => ⟨0, by
    show 1 = l * 0 + 1
    lib_nat⟩
  | c + 1 => by
    match pow_one_mul q l e M h c with
    | ⟨M', hM'⟩ =>
      exact ⟨M' * l * M + M' + M, by
        show q ^ (e * c + e) = _
        rw [TowerCard.pow_add', hM', h]
        lib_nat⟩

theorem pow_succ_ex (x : Nat) : ∀ n, ∃ N, (x + 1) ^ n = N + 1
  | 0 => ⟨0, rfl⟩
  | n + 1 => by
    match pow_succ_ex x n with
    | ⟨N, h⟩ =>
      exact ⟨N * x + N + x, by
        show (x + 1) ^ n * (x + 1) = _
        rw [h]
        lib_nat⟩

theorem mixed_pos (L b : Nat) : ∃ N, 2 ^ (L + 1) * 3 ^ b = N + 1 := by
  match pow_succ_ex 1 (L + 1), pow_succ_ex 2 b with
  | ⟨A, hA⟩, ⟨B, hB⟩ =>
    exact ⟨A * B + A + B, by
      show (1 + 1) ^ (L + 1) * (2 + 1) ^ b = _
      rw [hA, hB]
      lib_nat⟩

/-- **Eisenstein's reciprocity at primes of residue degree f | 2^(L+1)·3^b**: in the mixed tower field of order
    q^(2^(L+1)·3^b) an element z of order ℓ exists and the computed symbols satisfy z^(Σ s_i) = z^(k_1(ℓ−1)). -/
theorem eis_mixed (j Mp ms t h' : Nat) (hM : Mp + 1 = (j + 1 + 1 + 1) ^ (ms + 1 + 1))
    (hpr : ∀ d, Dv d (j + 1 + 1 + 1) → d = 1 ∨ d = j + 1 + 1 + 1)
    (g : Zp (j + 1 + 1)) (hg : OrdMul.Ord (ring (j + 1 + 1)) g (j + 1 + 1))
    (hℓ : ∀ d, Dv d (ms + 1 + 1 + 1 + 1) → d = 1 ∨ d = ms + 1 + 1 + 1 + 1)
    (hNj : j + 1 + 1 = (ms + 1 + 1 + 1 + 1) * (t + 1)) (hh : ms + 1 + 1 + 1 = (h' + 1) + (h' + 1))
    (α : V Int (ms + 1 + 1 + 1 + 1)) (iv : Zp (ms + 1 + 1 + 1) → Zp (ms + 1 + 1 + 1))
    (hNα : NormEval.nrm (ms + 1 + 1 + 1) α
        (BbarVal.nlist (ms + 1 + 1 + 1) iv (StickQs.qsL (ms + 1 + 1) 0 (ms + 1 + 1) (h' + 1)))
      = nsm (gring LibInt.intR (ms + 1 + 1 + 1)) (j + 1 + 1 + 1) (gring LibInt.intR (ms + 1 + 1 + 1)).one)
    (h0 : RedP2.red (j + 1) Mp (ConjUnit.Y Mp (ms + 1 + 1 + 1) (TeichN.psiN (j + 1) Mp (ms + 1) (t + 1) g) α
        (ring (ms + 1 + 1 + 1)).one) = (ring (j + 1 + 1)).zero)
    (m : Nat) (hq : ∀ d, Dv d (2 * m + 1 + 1 + 1) → d = 1 ∨ d = 2 * m + 1 + 1 + 1) (hq2 : 2 * m + 1 + 1 + 1 ≠ 2) (e : Nat)
    (hQ : nsm (ring (j + 1 + 1)) ((2 * m + 1 + 1 + 1) ^ e) (ring (j + 1 + 1)).one ≠ (ring (j + 1 + 1)).zero)
    (M : Nat) (hqn : (2 * m + 1 + 1 + 1) ^ e = (ms + 1 + 1 + 1 + 1) * M + 1)
    (hp : nsm (ring (2 * m + 1 + 1)) (j + 1 + 1 + 1) (ring (2 * m + 1 + 1)).one ≠ (ring (2 * m + 1 + 1)).zero) (s : Nat) (hs' : M = (2 * m + 1 + 1) * s)
    (k : Nat → Nat)
    (hk : ∀ c, 1 ≤ c → c < ms + 1 + 1 + 1 + 1 →
      pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) (pw (ring (j + 1 + 1)) g (t + 1)) c) (k c)
        = pw (ring (j + 1 + 1))
            (pw (ring (j + 1 + 1)) (nsm (ring (j + 1 + 1)) ((2 * m + 1 + 1 + 1) ^ e) (ring (j + 1 + 1)).one) (j + 1)) (t + 1))
    (gl : Zp (ms + 1 + 1 + 1)) (hgl : OrdMul.Ord (ring (ms + 1 + 1 + 1)) gl (ms + 1 + 1 + 1))
    (hc0 : nsm (ring (ms + 1 + 1 + 1)) (2 * m + 1 + 1 + 1) (ring (ms + 1 + 1 + 1)).one ≠ (ring (ms + 1 + 1 + 1)).zero)
    (hprim : PrimAlpha1.Prim (ms + 1 + 1) α)
    (h3 : (∃ t, m = 3 * t + 1) ∨ (∃ t, m = 3 * t + 2)) (L b c : Nat) (hf : 2 ^ (L + 1) * 3 ^ b = e * c) :
    ∃ G : @FF (C m L b) (instDecC m L b),
      ∃ z, pw G.K z (ms + 1 + 1 + 1 + 1) = G.K.one ∧ z ≠ G.K.one ∧ pw G.K z
        (SymRho.sumN (EisRhoF.syF (ring (2 * m + 1 + 1)) (ms + 1 + 1) gl G.K (CycBase.evI (ring (2 * m + 1 + 1)) G.K (FpEmb.emb G.K (2 * m + 1 + 1)) (ms + 1 + 1 + 1) z) (instDecC m L b)
          (pw (CycRing.cyc (ring (2 * m + 1 + 1)) (ms + 1 + 1 + 1))
            (CycEis.phz (CycRing.cyc (ring (2 * m + 1 + 1)) (ms + 1 + 1 + 1)) (ms + 1 + 1 + 1) (CycUnit.u (ring (2 * m + 1 + 1)) (ms + 1 + 1 + 1)) α) M))
          (ms + 1 + 1 + 1))
      = pw G.K z ((1 * k 1) * (ms + 1 + 1 + 1))  := by
  match MixedCard.mixed_card m hq h3 L b, mixed_pos L b with
  | ⟨G, _, hc⟩, ⟨N, hNp⟩ =>
    have hcard : 2 * G.h + 1 = (2 * m + 1 + 1 + 1) ^ (N + 1) := by rw [hc, hNp]
    match pow_one_mul (2 * m + 1 + 1 + 1) (ms + 1 + 1 + 1 + 1) e M hqn c with
    | ⟨M', hM'⟩ =>
      have e1 : 2 * G.h + 1 = (ms + 1 + 1 + 1 + 1) * M' + 1 := by rw [hc, hf, hM']
      have hdv : Dv (ms + 1 + 1 + 1 + 1) (2 * G.h) := ⟨M', by
        apply FermatUnique.add_cancel 1
        rw [show 1 + 2 * G.h = 2 * G.h + 1 by lib_nat, e1]
        lib_nat⟩
      exact ⟨G, @EisFqFF.eis_ff j Mp ms t h' hM hpr g hg hℓ hNj hh α iv hNα h0 (2 * m + 1) hq hq2 e hQ M hqn hp s hs' k hk
        gl hgl hc0 hprim _ (instDecC m L b) G N hcard hdv⟩

end EisFqMixed

#print axioms EisFqMixed.pow_one_mul
#print axioms EisFqMixed.pow_succ_ex
#print axioms EisFqMixed.mixed_pos
#print axioms EisFqMixed.eis_mixed

#print axioms: 8 theorems, no axioms.

78.391 Machine check

FiniteRecChain.lean (\(11\) theorems), KoszulRangeChain.lean (\(4\)) and the 193 files of work/lang (\(1317\)) are checked by lake env lean, and every #print axioms reports that it does not depend on any axioms (AXIOMS.txt). The Python scripts use the standard library only.

Listing 144: FiniteRecChain.lean: reindexing, induction, Frobenius reciprocity

FiniteRecChain.lean

/-
  FiniteRecChain — T31 R6: a two-sided finite model of reciprocity and functoriality.

  Both sides are finite: a finite group `G` (listed once each, `Complete`), a subgroup `H`
  (a Boolean membership test), and functions with values in any commutative ring.

  "Automorphic side": class functions on G (and on H).  "Galois side": the same functions read
  as characters.  Functoriality between H and G is induction / restriction.

    delta_sum         Σ_y [y = z] F(y) = (Σ_y [y = z]) · F(z)
    reindex           a bijection σ of G (inverse τ) leaves Σ_x F(x) unchanged
    ind_class         the induced function Ind f is a class function on G
    frobenius         ⟨Ind f, χ⟩_G = |G| · ⟨f, Res χ⟩_H  for every class function χ on G
                      (Frobenius reciprocity: transfer H → G is adjoint to restriction)

  Lean 4 core only, proofs by hand, no axioms.
-/
import YMChain

namespace FiniteRecChain

open YMChain

variable {R : Type} (Rg : DetChain.CRing R) {G : Type} [DecidableEq G]

theorem ite_pos' {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : c) : @ite α c d a b = a := by
  cases d with
  | isTrue _ => rfl
  | isFalse hn => exact absurd h hn

theorem ite_neg' {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : ¬ c) : @ite α c d a b = b := by
  cases d with
  | isTrue hc => exact absurd hc h
  | isFalse _ => rfl

/-- `[y = z]` as a ring element. -/
def delta (y z : G) : R := if y = z then Rg.one else Rg.zero

/-- Every element of `G` occurs exactly once in `els` (ring form of "count = 1"). -/
def Complete (els : List G) : Prop := ∀ z : G, sumK Rg (fun y => delta Rg y z) els = Rg.one

theorem delta_symm (y z : G) : delta Rg y z = delta Rg (R := R) z y := by
  unfold delta
  by_cases h : y = z
  · rw [ite_pos' _ _ h, ite_pos' _ _ h.symm]
  · rw [ite_neg' _ _ h, ite_neg' _ _ (fun h' => h h'.symm)]

theorem delta_mul (F : G → R) (y z : G) : Rg.mul (delta Rg y z) (F y) = Rg.mul (delta Rg y z) (F z) := by
  unfold delta
  by_cases h : y = z
  · rw [ite_pos' _ _ h, h]
  · rw [ite_neg' _ _ h, DetChain.zero_mul', DetChain.zero_mul']

theorem delta_sum (F : G → R) (z : G) : ∀ l : List G,
    sumK Rg (fun y => Rg.mul (delta Rg y z) (F y)) l = Rg.mul (sumK Rg (fun y => delta Rg y z) l) (F z)
  | [] => (DetChain.zero_mul' Rg (F z)).symm
  | y :: l => by
    show Rg.add (Rg.mul (delta Rg y z) (F y)) (sumK Rg (fun y => Rg.mul (delta Rg y z) (F y)) l)
      = Rg.mul (Rg.add (delta Rg y z) (sumK Rg (fun y => delta Rg y z) l)) (F z)
    rw [delta_sum F z l, delta_mul Rg F y z, Rg.mul_comm (Rg.add _ _), Rg.distrib,
      Rg.mul_comm (F z), Rg.mul_comm (F z)]

theorem pick (els : List G) (hc : Complete Rg els) (F : G → R) (z : G) :
    sumK Rg (fun y => Rg.mul (delta Rg y z) (F y)) els = F z := by
  rw [delta_sum, hc z, Rg.one_mul]

/-- **Reindexing.** A bijection `σ` (inverse `τ`) of a completely listed finite set
    leaves sums unchanged. -/
theorem reindex (els : List G) (hc : Complete Rg els) (σ τ : G → G)
    (hστ : ∀ x, τ (σ x) = x) (hτσ : ∀ y, σ (τ y) = y) (F : G → R) :
    sumK Rg (fun x => F (σ x)) els = sumK Rg F els := by
  have e1 : sumK Rg (fun x => F (σ x)) els
      = sumK Rg (fun x => sumK Rg (fun y => Rg.mul (delta Rg y (σ x)) (F y)) els) els :=
    sumK_ext Rg _ _ (fun x => (pick Rg els hc F (σ x)).symm) els
  rw [e1, sumK_swap]
  refine sumK_ext Rg _ _ (fun y => ?_) els
  -- Σ_x [y = σ x] F y = F y
  have e2 : sumK Rg (fun x => Rg.mul (delta Rg y (σ x)) (F y)) els
      = sumK Rg (fun x => Rg.mul (delta Rg x (τ y)) (F y)) els :=
    sumK_ext Rg _ _ (fun x => by
      show Rg.mul (delta Rg y (σ x)) (F y) = Rg.mul (delta Rg x (τ y)) (F y)
      have : delta Rg (R := R) y (σ x) = delta Rg x (τ y) := by
        unfold delta
        by_cases h : y = σ x
        · have h' : x = τ y := by rw [h, hστ]
          rw [ite_pos' _ _ h, ite_pos' _ _ h']
        · have h' : ¬ x = τ y := fun h' => h (by rw [h', hτσ])
          rw [ite_neg' _ _ h, ite_neg' _ _ h']
      rw [this]) els
  rw [e2, ← sumK_mul, hc (τ y), Rg.one_mul]

/-- `f` extended by zero outside `H`. -/
def ext0 (inH : G → Bool) (f : G → R) (x : G) : R := if inH x = true then f x else Rg.zero

section Frob
variable (K : Grp G) (els : List G) (inH : G → Bool)

/-- Class function on `G`, ring-valued. -/
def ClassR (χ : G → R) : Prop := ∀ g y, χ (K.mul (K.mul g y) (K.inv g)) = χ y

/-- Induction: `(Ind f)(x) = Σ_g f°(g x g⁻¹)` (that is `|H|` times the usual normalisation). -/
def ind (f : G → R) (x : G) : R := sumK Rg (fun g => ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) els

/-- Ring-valued pairing `⟨a, b⟩ = Σ_x a(x) b(x⁻¹)`. -/
def pair (a b : G → R) : R := sumK Rg (fun x => Rg.mul (a x) (b (K.inv x))) els

omit [DecidableEq G] in
theorem conj_conj (g h x : G) :
    K.mul (K.mul g (K.mul (K.mul h x) (K.inv h))) (K.inv g)
      = K.mul (K.mul (K.mul g h) x) (K.inv (K.mul g h)) := by
  rw [inv_mul_rev, ← K.assoc, ← K.assoc, ← K.assoc, K.assoc g h x]

/-- **The induced function is a class function.** -/
theorem ind_class (hc : Complete Rg els) (f : G → R) (h x : G) :
    ind Rg K els inH f (K.mul (K.mul h x) (K.inv h)) = ind Rg K els inH f x := by
  show sumK Rg (fun g => ext0 Rg inH f (K.mul (K.mul g (K.mul (K.mul h x) (K.inv h))) (K.inv g))) els
    = sumK Rg (fun g => ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) els
  have e : sumK Rg (fun g => ext0 Rg inH f (K.mul (K.mul g (K.mul (K.mul h x) (K.inv h))) (K.inv g))) els
      = sumK Rg (fun g => (fun k => ext0 Rg inH f (K.mul (K.mul k x) (K.inv k))) (K.mul g h)) els :=
    sumK_ext Rg _ _ (fun g => by show ext0 Rg inH f _ = ext0 Rg inH f _; rw [conj_conj]) els
  rw [e]
  exact reindex Rg els hc (fun g => K.mul g h) (fun k => K.mul k (K.inv h))
    (fun g => by show K.mul (K.mul g h) (K.inv h) = g; rw [K.assoc, K.mul_inv, K.mul_one])
    (fun k => by show K.mul (K.mul k (K.inv h)) h = k; rw [K.assoc, K.inv_mul, K.mul_one])
    (fun k => ext0 Rg inH f (K.mul (K.mul k x) (K.inv k)))

omit [DecidableEq G] in
theorem conj_inv_conj (g x : G) :
    K.inv (K.mul (K.mul g x) (K.inv g)) = K.mul (K.mul g (K.inv x)) (K.inv g) := by
  rw [inv_mul_rev, inv_mul_rev, inv_inv, K.assoc]

/-- **Frobenius reciprocity** (transfer from `H` to `G` is adjoint to restriction):
    `⟨Ind f, χ⟩_G = |G| · Σ_{y ∈ H} f(y) χ(y⁻¹)` for every class function `χ` on `G`,
    with `|G| = Σ_g 1`. -/
theorem frobenius (hc : Complete Rg els) (f χ : G → R) (hχ : ClassR K χ) :
    pair Rg K els (ind Rg K els inH f) χ
      = Rg.mul (sumK Rg (fun _ => Rg.one) els)
          (sumK Rg (fun y => Rg.mul (ext0 Rg inH f y) (χ (K.inv y))) els) := by
  unfold pair ind
  have e1 : sumK Rg (fun x => Rg.mul (sumK Rg (fun g => ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) els) (χ (K.inv x))) els
      = sumK Rg (fun x => sumK Rg (fun g => Rg.mul (ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) (χ (K.inv x))) els) els :=
    sumK_ext Rg _ _ (fun x => sumK_mul Rg _ _ els) els
  rw [e1, sumK_swap]
  -- for each g: reindex x ↦ g x g⁻¹
  have e2 : ∀ g, sumK Rg (fun x => Rg.mul (ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) (χ (K.inv x))) els
      = sumK Rg (fun y => Rg.mul (ext0 Rg inH f y) (χ (K.inv y))) els := by
    intro g
    have e3 : sumK Rg (fun x => Rg.mul (ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) (χ (K.inv x))) els
        = sumK Rg (fun x => (fun y => Rg.mul (ext0 Rg inH f y) (χ (K.inv y))) (K.mul (K.mul g x) (K.inv g))) els :=
      sumK_ext Rg _ _ (fun x => by
        show Rg.mul (ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) (χ (K.inv x))
          = Rg.mul (ext0 Rg inH f (K.mul (K.mul g x) (K.inv g))) (χ (K.inv (K.mul (K.mul g x) (K.inv g))))
        rw [conj_inv_conj, hχ]) els
    rw [e3]
    exact reindex Rg els hc (fun x => K.mul (K.mul g x) (K.inv g)) (fun y => K.mul (K.mul (K.inv g) y) g)
      (fun x => by
        show K.mul (K.mul (K.inv g) (K.mul (K.mul g x) (K.inv g))) g = x
        rw [← K.assoc, ← K.assoc, K.inv_mul, K.one_mul, K.assoc, K.inv_mul, K.mul_one])
      (fun y => by
        show K.mul (K.mul g (K.mul (K.mul (K.inv g) y) g)) (K.inv g) = y
        rw [← K.assoc, ← K.assoc, K.mul_inv, K.one_mul, K.assoc, K.mul_inv, K.mul_one])
      (fun y => Rg.mul (ext0 Rg inH f y) (χ (K.inv y)))
  rw [sumK_ext Rg _ _ e2 els, sumK_mul Rg]
  exact sumK_ext Rg _ _ (fun _ => (Rg.one_mul _).symm) els

end Frob

end FiniteRecChain

#print axioms FiniteRecChain.ite_pos'
#print axioms FiniteRecChain.ite_neg'
#print axioms FiniteRecChain.delta_symm
#print axioms FiniteRecChain.delta_mul
#print axioms FiniteRecChain.delta_sum
#print axioms FiniteRecChain.pick
#print axioms FiniteRecChain.reindex
#print axioms FiniteRecChain.conj_conj
#print axioms FiniteRecChain.ind_class
#print axioms FiniteRecChain.conj_inv_conj
#print axioms FiniteRecChain.frobenius

Listing 145: KoszulRangeChain.lean: range and Koszul regularity, \(\ell _0\le 2\)

KoszulRangeChain.lean

/-
  KoszulRangeChain — T35 R1: "cohomology vanishes outside the range" ↔ "Koszul form with a
  regular sequence", for l0 = 1 and l0 = 2.

  The patched complex of Calegari–Geraghty is, in the good case, the Koszul complex of
  f_1, …, f_{l0} over the patched ring; its cohomology is concentrated in the top degree of
  the range [q0, q0 + l0] exactly when the lower Koszul homology vanishes.

    l0 = 1:  range1_iff         H_1(x) = 0  ↔  x regular
    l0 = 2:  range2_of_regular  (x, y) regular  ⇒  H_2 = 0 and H_1 = 0       (DetChain.koszul2_*)
             regular_of_range2  x regular and H_1 = 0  ⇒  (x, y) regular
             range2_iff         for x regular:  H_1 = 0  ↔  (x, y) regular
  The hypothesis "x regular" cannot be dropped in a general ring: over ℤ/6 with (x, y) = (2, 3)
  the Koszul complex is exact (the ideal is the unit ideal) while 2·3 = 0.
  In a Noetherian local ring with x, y in the maximal ideal it can be dropped (Matsumura,
  Commutative Ring Theory, Thm 16.5); that step is cited, not proved here.

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain

namespace KoszulRangeChain

open DetChain

variable {R : Type} (K : CRing R)

/-- `H_1` of the length-one Koszul complex `0 → R --x--> R → 0` vanishes. -/
def H1zero1 (x : R) : Prop := ∀ r, K.mul r x = K.zero → r = K.zero

/-- `H_2` of the length-two Koszul complex vanishes. -/
def H2zero (x y : R) : Prop := ∀ r, d2 K x y r = (K.zero, K.zero) → r = K.zero

/-- `H_1` of the length-two Koszul complex vanishes. -/
def H1zero (x y : R) : Prop := ∀ a b, d1 K x y (a, b) = K.zero → ∃ r, (a, b) = d2 K x y r

theorem range1_iff (x : R) : H1zero1 K x ↔ Regular1 K x := Iff.rfl

theorem range2_of_regular (x y : R) (h : Regular2 K x y) : H2zero K x y ∧ H1zero K x y :=
  ⟨fun r hr => koszul2_H2 K x y h r hr, fun a b hab => koszul2_H1 K x y h a b hab⟩

theorem regular_of_range2 (x y : R) (hx : Regular1 K x) (h1 : H1zero K x y) : Regular2 K x y := by
  refine ⟨hx, fun b c hbc => ?_⟩
  -- (−c) x + b y = 0
  have hd : d1 K x y (K.neg c, b) = K.zero := by
    show K.add (K.mul (K.neg c) x) (K.mul b y) = K.zero
    rw [neg_mul_left, hbc, add_neg']
  obtain ⟨r, hr⟩ := h1 (K.neg c) b hd
  exact ⟨r, congrArg Prod.snd hr⟩

theorem range2_iff (x y : R) (hx : Regular1 K x) : H1zero K x y ↔ Regular2 K x y :=
  ⟨regular_of_range2 K x y hx, fun h => (range2_of_regular K x y h).2⟩

end KoszulRangeChain

#print axioms KoszulRangeChain.range1_iff
#print axioms KoszulRangeChain.range2_of_regular
#print axioms KoszulRangeChain.regular_of_range2
#print axioms KoszulRangeChain.range2_iff

Listing 146: lang/Recip1.lean: reciprocity as a split idempotent

Recip1.lean

/-
  lang/Recip1 — T43, cycle 1 (branch R8): reciprocity as a split idempotent, and GL1 over ℚ.

  The pattern of the chain (FTAChain.split_idem, read_inj): placing then reading is the identity,
  reading then placing is idempotent. Read as an adjunction between a Galois side `A` and an
  automorphic side `B`:
    unit_iso      read ∘ place = id                         (the unit is an isomorphism)
    e_idem        e = place ∘ read is idempotent
    image_iff     b is in the image of place  ↔  e b = b     (the counit is an isomorphism on Fix e)
    place_inj     place is injective
  So A ≅ Fix(e): reciprocity is the statement that the automorphic objects are the fixed points.

  GL1 over ℚ, modulus N. Galois side: functions χ on residues (Dirichlet-type, periodic mod N).
  Automorphic side: eigen-systems λ : ℕ → R indexed by primes. place χ = (p ↦ χ (p % N)) is
  "Frobenius at p is p mod N". read λ = (a ↦ λ (w a)) with a prime witness w a ≡ a (mod N).
    gl1_unit      read (place χ) = χ on residues with a witness
    gl1_fix       an eigen-system is fixed by e iff it is p ↦ χ (p % N) for some χ (pointwise)
  Witnesses for N = 5 and N = 8 are checked by evaluation (`rfl` on concrete numbers).

  Lean 4 core only, proofs by hand, no axioms.
-/

namespace Recip1

universe u

section Split
variable {A B : Type u} (place : A → B) (read : B → A)

def e (b : B) : B := place (read b)

theorem unit_iso (h : ∀ a, read (place a) = a) (a : A) : read (place a) = a := h a

theorem e_idem (h : ∀ a, read (place a) = a) (b : B) : e place read (e place read b) = e place read b :=
  congrArg place (h (read b))

theorem image_iff (h : ∀ a, read (place a) = a) (b : B) : (∃ a, place a = b) ↔ e place read b = b :=
  ⟨fun ⟨a, ha⟩ => by
      show place (read b) = b
      rw [← ha, h a],
   fun hb => ⟨read b, hb⟩⟩

theorem place_inj (h : ∀ a, read (place a) = a) {a a' : A} (hp : place a = place a') : a = a' :=
  Eq.trans (h a).symm (Eq.trans (congrArg read hp) (h a'))

end Split

/-! ## GL1 over ℚ -/

section GL1
variable {R : Type u} (N : Nat) (w : Nat → Nat)

/-- Galois side: functions on ℕ that only see the residue mod N. -/
def Periodic (χ : Nat → R) : Prop := ∀ n, χ n = χ (n % N)

/-- Frobenius at p acts through p mod N. -/
def placeF (χ : Nat → R) : Nat → R := fun p => χ (p % N)

/-- Read an eigen-system at the prime witnesses. -/
def readF (lam : Nat → R) : Nat → R := fun a => lam (w (a % N))

theorem mod_mod' (a : Nat) (hw : ∀ b, b < N → w b % N = b) (hN : 0 < N) :
    w (a % N) % N = a % N := hw (a % N) (Nat.mod_lt a hN)

theorem gl1_unit (χ : Nat → R) (hχ : Periodic N χ) (hw : ∀ b, b < N → w b % N = b) (hN : 0 < N)
    (a : Nat) : readF N w (placeF N χ) a = χ a := by
  show χ (w (a % N) % N) = χ a
  rw [mod_mod' N w a hw hN, ← hχ a]

/-- **GL1 reciprocity (finite form).** An eigen-system `λ` is fixed by `e = place ∘ read` iff it is
    `p ↦ χ (p % N)` for a periodic `χ`. -/
theorem gl1_fix (lam : Nat → R) (hw : ∀ b, b < N → w b % N = b) (hN : 0 < N)
    (hmod : ∀ a, a < N → a % N = a) :
    (∃ χ : Nat → R, Periodic N χ ∧ ∀ p, placeF N χ p = lam p) ↔ ∀ p, placeF N (readF N w lam) p = lam p := by
  constructor
  · intro ⟨χ, hχ, hl⟩ p
    rw [← hl p]
    show lam (w ((p % N) % N)) = χ (p % N)
    rw [← hl (w ((p % N) % N))]
    show χ (w ((p % N) % N) % N) = χ (p % N)
    have hpp : (p % N) % N = p % N := hmod (p % N) (Nat.mod_lt p hN)
    rw [hpp, hw (p % N) (Nat.mod_lt p hN)]
  · intro hf
    refine ⟨readF N w lam, fun n => ?_, hf⟩
    show lam (w (n % N)) = lam (w ((n % N) % N))
    rw [hmod (n % N) (Nat.mod_lt n hN)]

end GL1

/-! ## Prime witnesses, checked by evaluation -/

/-- Trial-division primality as a Boolean. -/
def noDivBelow (n : Nat) : Nat → Bool
  | 0 => true
  | 1 => true
  | k + 2 => (n % (k + 2) != 0) && noDivBelow n (k + 1)

def isPrimeB (n : Nat) : Bool := (1 < n) && noDivBelow n (n - 1)

/-- Witnesses mod 5: residues 1, 2, 3, 4 -> primes 11, 2, 3, 19 (residue 0: 5). -/
def w5 : Nat → Nat
  | 0 => 5 | 1 => 11 | 2 => 2 | 3 => 3 | _ => 19

theorem w5_prime : (isPrimeB (w5 1), isPrimeB (w5 2), isPrimeB (w5 3), isPrimeB (w5 4))
    = (true, true, true, true) := rfl

theorem w5_res : (w5 0 % 5, w5 1 % 5, w5 2 % 5, w5 3 % 5, w5 4 % 5) = (0, 1, 2, 3, 4) := rfl

theorem w5_hw : ∀ b, b < 5 → w5 b % 5 = b
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, hk => absurd hk (Nat.not_lt_of_le (Nat.le_add_left 5 k))

/-- Witnesses mod 8: odd residues 1, 3, 5, 7 -> primes 17, 3, 5, 7; even residues by 2 and placeholders. -/
def w8 : Nat → Nat
  | 0 => 8 | 1 => 17 | 2 => 2 | 3 => 3 | 4 => 4 | 5 => 5 | 6 => 6 | _ => 7

theorem w8_prime : (isPrimeB (w8 1), isPrimeB (w8 3), isPrimeB (w8 5), isPrimeB (w8 7))
    = (true, true, true, true) := rfl

theorem w8_hw : ∀ b, b < 8 → w8 b % 8 = b
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | k + 8, hk => absurd hk (Nat.not_lt_of_le (Nat.le_add_left 8 k))

theorem mod5_small : ∀ a, a < 5 → a % 5 = a
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | k + 5, hk => absurd hk (Nat.not_lt_of_le (Nat.le_add_left 5 k))

theorem mod8_small : ∀ a, a < 8 → a % 8 = a
  | 0, _ => rfl
  | 1, _ => rfl
  | 2, _ => rfl
  | 3, _ => rfl
  | 4, _ => rfl
  | 5, _ => rfl
  | 6, _ => rfl
  | 7, _ => rfl
  | k + 8, hk => absurd hk (Nat.not_lt_of_le (Nat.le_add_left 8 k))

/-- GL1 reciprocity mod 5 and mod 8, with the witnesses above. -/
theorem gl1_mod5 {R : Type u} (lam : Nat → R) :
    (∃ χ : Nat → R, Periodic 5 χ ∧ ∀ p, placeF 5 χ p = lam p) ↔ ∀ p, placeF 5 (readF 5 w5 lam) p = lam p :=
  gl1_fix 5 w5 lam w5_hw (Nat.zero_lt_succ 4) mod5_small

theorem gl1_mod8 {R : Type u} (lam : Nat → R) :
    (∃ χ : Nat → R, Periodic 8 χ ∧ ∀ p, placeF 8 χ p = lam p) ↔ ∀ p, placeF 8 (readF 8 w8 lam) p = lam p :=
  gl1_fix 8 w8 lam w8_hw (Nat.zero_lt_succ 7) mod8_small

end Recip1

#print axioms Recip1.unit_iso
#print axioms Recip1.e_idem
#print axioms Recip1.image_iff
#print axioms Recip1.place_inj
#print axioms Recip1.mod_mod'
#print axioms Recip1.gl1_unit
#print axioms Recip1.gl1_fix
#print axioms Recip1.w5_prime
#print axioms Recip1.w5_res
#print axioms Recip1.w5_hw
#print axioms Recip1.w8_prime
#print axioms Recip1.w8_hw
#print axioms Recip1.mod5_small
#print axioms Recip1.mod8_small
#print axioms Recip1.gl1_mod5
#print axioms Recip1.gl1_mod8

Listing 147: lang/Frobenius.lean: binomial theorem, Frobenius in characteristic \(p\)

Frobenius.lean

/-
  lang/Frobenius — T43, cycle 2: GL1 over a general number field, the general pieces.

  (a) The reciprocity shell, for any group G and any set of primes P with a Frobenius map
      frob : P → G and a Chebotarev section sec : G → P (frob (sec g) = g):
        shell          λ factors through frob  ↔  λ = λ ∘ sec ∘ frob
        shell_unique   the factor is λ ∘ sec
      Over a number field F with abelian L/F, G = Gal(L/F) and frob p = Artin symbol; the arithmetic
      inputs are the Frobenius map itself and the section (Chebotarev). For non-abelian G the same
      shell applies with frob landing in conjugacy classes.
  (b) Frobenius is additive in characteristic p, in every commutative ring (every residue field
      O_F/p of every number field):
        ev_nxt         ev (nxt p r) = (a + b) · ev r + p • b^|r|   (Pascal rows, no subtraction)
        ev_row         (a + b)^n = ev (row n)                       (binomial theorem)
        C_absorb       (k + 1) C(n+1, k+1) = (n + 1) C(n, k)
        dv_choose      p | C(p, k+1) for k + 1 < p, p prime          (Euclid: FTAChain.euclid_core)
        row_eq         row n = [C(n,0), …, C(n,n)]
        nsm_char       p • 1 = 0  ⇒  (q p) • y = 0
        frob_add       p prime, p • 1 = 0  ⇒  (a + b)^p = a^p + b^p
        pw_mul         (a b)^n = a^n b^n
      So x ↦ x^p is a ring endomorphism of every commutative ring of characteristic p.

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain
import FTAChain

namespace Frobenius

/-! ## (a) The shell -/

section Shell
variable {P G R : Type} (frob : P → G) (sec : G → P) (hs : ∀ g, frob (sec g) = g)

theorem shell (lam : P → R) (hs : ∀ g, frob (sec g) = g) :
    (∃ χ : G → R, ∀ p, lam p = χ (frob p)) ↔ ∀ p, lam p = lam (sec (frob p)) :=
  ⟨fun ⟨χ, hχ⟩ p => by rw [hχ p, hχ (sec (frob p)), hs (frob p)],
   fun h => ⟨fun g => lam (sec g), h⟩⟩

theorem shell_unique (lam : P → R) (χ : G → R) (hs : ∀ g, frob (sec g) = g)
    (hχ : ∀ p, lam p = χ (frob p)) (g : G) : χ g = lam (sec g) := by
  rw [hχ (sec g), hs g]

end Shell

/-! ## (b) Pascal rows and the binomial theorem in a commutative ring -/

section Binom
variable {R : Type} (K : DetChain.CRing R)

/-- `n • x`. -/
def nsm : Nat → R → R
  | 0, _ => K.zero
  | n + 1, x => K.add (nsm n x) x

def pw (x : R) : Nat → R
  | 0 => K.one
  | k + 1 => K.mul (pw x k) x

/-- Homogeneous evaluation of a coefficient list `c_0, …, c_d`: `Σ c_k a^k b^(d-k)`. -/
def ev (a b : R) : List Nat → R
  | [] => K.zero
  | c :: cs => K.add (nsm K c (pw K b cs.length)) (K.mul a (ev a b cs))

/-- Next Pascal row: `n_k = r_k + r_(k-1)`. -/
def nxt : Nat → List Nat → List Nat
  | p, [] => [p]
  | p, c :: cs => (c + p) :: nxt c cs

def row : Nat → List Nat
  | 0 => [1]
  | n + 1 => nxt 0 (row n)

theorem nsm_add (m n : Nat) (x : R) : nsm K (m + n) x = K.add (nsm K m x) (nsm K n x) := by
  induction n with
  | zero => exact (DetChain.add_zero' K _).symm
  | succ n ih =>
    show K.add (nsm K (m + n) x) x = K.add (nsm K m x) (K.add (nsm K n x) x)
    rw [ih, K.add_assoc]

theorem mul_nsm (a : R) (n : Nat) (x : R) : K.mul a (nsm K n x) = nsm K n (K.mul a x) := by
  induction n with
  | zero => exact DetChain.mul_zero K a
  | succ n ih =>
    show K.mul a (K.add (nsm K n x) x) = K.add (nsm K n (K.mul a x)) (K.mul a x)
    rw [K.distrib, ih]

theorem len_nxt : ∀ (p : Nat) (r : List Nat), (nxt p r).length = r.length + 1
  | _, [] => rfl
  | _, c :: cs => congrArg Nat.succ (len_nxt c cs)

theorem add4 (u v w z : R) : K.add (K.add u v) (K.add w z) = K.add (K.add u w) (K.add v z) :=
  DetChain.add_mid4 K u v w z

theorem nsm_eq_mul (n : Nat) (x : R) : nsm K n x = K.mul (nsm K n K.one) x := by
  induction n with
  | zero => exact (DetChain.zero_mul' K x).symm
  | succ n ih =>
    show K.add (nsm K n x) x = K.mul (K.add (nsm K n K.one) K.one) x
    rw [ih, DetChain.right_distrib, K.one_mul]

theorem perm4 (X Y Z U : R) : K.add (K.add X Y) (K.add Z U) = K.add (K.add (K.add U X) Z) Y := by
  rw [K.add_comm Z U, add4 K, K.add_comm X U, K.add_assoc (K.add U X) Z Y, K.add_comm Z Y,
    ← K.add_assoc (K.add U X) Y Z, K.add_assoc U X Y, K.add_comm (K.add U (K.add X Y)) Z,
    K.add_comm Z, K.add_assoc U (K.add X Y) Z, K.add_comm (K.add X Y) Z, ← K.add_assoc U Z]

theorem swap3' (a s E : R) : K.mul a (K.mul s E) = K.mul s (K.mul a E) := by
  rw [← K.mul_assoc, K.mul_comm a s, K.mul_assoc]

theorem ring_step (a b C P E B : R) :
    K.add (K.add (K.mul C (K.mul B b)) (K.mul P (K.mul B b))) (K.mul a (K.add (K.mul (K.add a b) E) (K.mul C B)))
      = K.add (K.mul (K.add a b) (K.add (K.mul C B) (K.mul a E))) (K.mul P (K.mul B b)) := by
  have l1 : K.mul a (K.add (K.mul (K.add a b) E) (K.mul C B))
      = K.add (K.mul (K.add a b) (K.mul a E)) (K.mul a (K.mul C B)) := by
    rw [K.distrib, swap3' K a (K.add a b) E]
  have r1 : K.mul (K.add a b) (K.add (K.mul C B) (K.mul a E))
      = K.add (K.add (K.mul a (K.mul C B)) (K.mul C (K.mul B b))) (K.mul (K.add a b) (K.mul a E)) := by
    rw [K.distrib, DetChain.right_distrib, K.mul_comm b (K.mul C B), K.mul_assoc C B b]
  rw [l1, r1]
  exact perm4 K _ _ _ _

/-- **Pascal step**: `ev (nxt p r) = (a + b) · ev r + p • b^|r|`. -/
theorem ev_nxt (a b : R) : ∀ (p : Nat) (r : List Nat),
    ev K a b (nxt p r) = K.add (K.mul (K.add a b) (ev K a b r)) (nsm K p (pw K b r.length))
  | p, [] => by
    show K.add (nsm K p (pw K b 0)) (K.mul a K.zero) = K.add (K.mul (K.add a b) K.zero) (nsm K p (pw K b 0))
    rw [DetChain.mul_zero, DetChain.mul_zero, DetChain.add_zero', K.zero_add]
  | p, c :: cs => by
    show K.add (nsm K (c + p) (pw K b (nxt c cs).length)) (K.mul a (ev K a b (nxt c cs)))
      = K.add (K.mul (K.add a b) (K.add (nsm K c (pw K b cs.length)) (K.mul a (ev K a b cs))))
          (nsm K p (pw K b (cs.length + 1)))
    rw [ev_nxt a b c cs, len_nxt, nsm_add]
    have hB' : pw K b (cs.length + 1) = K.mul (pw K b cs.length) b := rfl
    rw [hB', nsm_eq_mul K c (K.mul (pw K b cs.length) b), nsm_eq_mul K p (K.mul (pw K b cs.length) b),
      nsm_eq_mul K c (pw K b cs.length)]
    exact ring_step K a b _ _ _ _

theorem ev_row (a b : R) : ∀ n, ev K a b (row n) = pw K (K.add a b) n
  | 0 => by
    show K.add (nsm K 1 (pw K b 0)) (K.mul a K.zero) = K.one
    rw [DetChain.mul_zero, DetChain.add_zero']
    show K.add K.zero K.one = K.one
    exact K.zero_add _
  | n + 1 => by
    show ev K a b (nxt 0 (row n)) = K.mul (pw K (K.add a b) n) (K.add a b)
    rw [ev_nxt, ev_row a b n]
    show K.add (K.mul (K.add a b) (pw K (K.add a b) n)) K.zero = _
    rw [DetChain.add_zero', K.mul_comm]

end Binom

/-! ## Binomial coefficients, and p | C(p, k) -/

section Choose
open FTAChain

def C : Nat → Nat → Nat
  | 0, 0 => 1
  | 0, _ + 1 => 0
  | _ + 1, 0 => 1
  | n + 1, k + 1 => C n k + C n (k + 1)

theorem C_zero_right : ∀ n, C n 0 = 1
  | 0 => rfl
  | _ + 1 => rfl

theorem C_above : ∀ n k, C n (k + n + 1) = 0
  | 0, _ => rfl
  | n + 1, k => by
    show C n (k + n + 1) + C n (k + n + 1 + 1) = 0
    have e : k + n + 1 + 1 = (k + 1) + n + 1 := by rw [nadd_right_comm k n 1]
    rw [C_above n k, e, C_above n (k + 1)]

theorem C_above' (n : Nat) : C n (n + 1) = 0 := by
  have h := C_above n 0
  rw [nzero_add] at h
  exact h

theorem C_self : ∀ n, C n n = 1
  | 0 => rfl
  | n + 1 => by
    show C n n + C n (n + 1) = 1
    rw [C_self n, C_above' n]

theorem C_one : ∀ n, C (n + 1) 1 = n + 1
  | 0 => rfl
  | n + 1 => by
    show C (n + 1) 0 + C (n + 1) 1 = n + 1 + 1
    rw [C_zero_right, C_one n, nadd_comm]

/-- `(k + 1) · C(n+1, k+1) = (n + 1) · C(n, k)`. -/
theorem C_absorb : ∀ n k, (k + 1) * C (n + 1) (k + 1) = (n + 1) * C n k
  | 0, 0 => rfl
  | 0, k + 1 => by
    show (k + 1 + 1) * (C 0 (k + 1) + C 0 (k + 1 + 1)) = 1 * C 0 (k + 1)
    rfl
  | n + 1, 0 => by
    show 1 * (C (n + 1) 0 + C (n + 1) 1) = (n + 1 + 1) * C (n + 1) 0
    rw [C_zero_right, C_one, nmul_one (n + 1 + 1), nmul_comm 1, nmul_one, nadd_comm]
  | n + 1, j + 1 => by
    show (j + 1 + 1) * (C (n + 1) (j + 1) + C (n + 1) (j + 1 + 1)) = (n + 1 + 1) * C (n + 1) (j + 1)
    rw [nleft_distrib, C_absorb n (j + 1)]
    have e1 : (j + 1 + 1) * C (n + 1) (j + 1) = (n + 1) * C n j + C (n + 1) (j + 1) := by
      rw [nsucc_mul, C_absorb n j]
    rw [e1]
    show (n + 1) * C n j + C (n + 1) (j + 1) + (n + 1) * C n (j + 1) = (n + 1 + 1) * (C n j + C n (j + 1))
    rw [nsucc_mul (n + 1), nleft_distrib (n + 1), nadd_right_comm]
    rfl

theorem le_succ_self_absurd : ∀ n, Le (n + 1) n → False
  | 0, h => le_zero_absurd 0 h
  | n + 1, h => le_succ_self_absurd n (le_pred (n + 1) n h)

/-- **p divides C(p, k+1) for k + 1 < p** (p = j + 2 prime). -/
theorem dv_choose (j k : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hk : Le (k + 1 + 1) (j + 1 + 1)) : Dv (j + 1 + 1) (C (j + 1 + 1) (k + 1)) := by
  have h1 : Dv (j + 1 + 1) ((k + 1) * C (j + 1 + 1) (k + 1)) :=
    ⟨C (j + 1) k, by rw [C_absorb (j + 1) k, nmul_comm]⟩
  cases euclid_core j k (C (j + 1 + 1) (k + 1)) hpr h1 with
  | inl h => exact absurd (le_trans' _ _ _ hk (dv_le (j + 1 + 1) k h)) (le_succ_self_absurd (k + 1))
  | inr h => exact h

/-- `[C n k, C n (k+1), …]`, `m` entries. -/
def listC (n : Nat) : Nat → Nat → List Nat
  | _, 0 => []
  | k, m + 1 => C n k :: listC n (k + 1) m

theorem nxt_listC (n : Nat) : ∀ (m k : Nat), C n (k + 1 + m) = 0 →
    nxt (C n k) (listC n (k + 1) m) = listC (n + 1) (k + 1) (m + 1)
  | 0, k, h => by
    show [C n k] = [C n k + C n (k + 1)]
    rw [show C n (k + 1) = 0 from h]
    exact rfl
  | m + 1, k, h => by
    show (C n (k + 1) + C n k) :: nxt (C n (k + 1)) (listC n (k + 1 + 1) m)
      = (C n k + C n (k + 1)) :: listC (n + 1) (k + 1 + 1) (m + 1)
    have h' : C n (k + 1 + 1 + m) = 0 := by
      rw [nadd_assoc (k + 1) 1 m, nadd_comm 1 m]; exact h
    rw [nxt_listC n m (k + 1) h', nadd_comm (C n (k + 1))]

theorem row_eq : ∀ n, row n = listC n 0 (n + 1)
  | 0 => rfl
  | n + 1 => by
    show nxt 0 (row n) = C (n + 1) 0 :: listC (n + 1) 1 (n + 1)
    rw [row_eq n]
    show (C n 0 + 0) :: nxt (C n 0) (listC n 1 n) = C (n + 1) 0 :: listC (n + 1) 1 (n + 1)
    have h : C n (0 + 1 + n) = 0 := by rw [nadd_comm (0 + 1) n]; exact C_above' n
    rw [nxt_listC n n 0 h, C_zero_right n, C_zero_right (n + 1)]

end Choose

/-! ## Frobenius in characteristic p -/

section Frob
open FTAChain
variable {R : Type} (K : DetChain.CRing R)

theorem len_listC (n : Nat) : ∀ k m, (listC n k m).length = m
  | _, 0 => rfl
  | k, m + 1 => congrArg Nat.succ (len_listC n (k + 1) m)

theorem nsm_one (x : R) : nsm K 1 x = x := K.zero_add x

theorem nsm_mul_nat (q : Nat) (x : R) : ∀ n, nsm K (q * n) x = nsm K n (nsm K q x)
  | 0 => rfl
  | n + 1 => by
    show nsm K (q * n + q) x = K.add (nsm K n (nsm K q x)) (nsm K q x)
    rw [nsm_add, nsm_mul_nat q x n]

/-- In characteristic `p` (p • 1 = 0) every multiple of `p` kills every element. -/
theorem nsm_char (p q : Nat) (hp : nsm K p K.one = K.zero) (y : R) : nsm K (q * p) y = K.zero := by
  rw [nsm_mul_nat, nsm_eq_mul, hp, DetChain.zero_mul']

theorem ev_tail (a b : R) (n : Nat) : ∀ m k, (∀ i y, Le (i + 1) m → nsm K (C n (k + i)) y = K.zero) →
    ev K a b (listC n k (m + 1)) = K.mul (pw K a m) (nsm K (C n (k + m)) K.one)
  | 0, k, _ => by
    show K.add (nsm K (C n k) K.one) (K.mul a K.zero) = K.mul K.one (nsm K (C n k) K.one)
    rw [DetChain.mul_zero, DetChain.add_zero', K.one_mul]
  | m + 1, k, h => by
    show K.add (nsm K (C n k) (pw K b (listC n (k + 1) (m + 1)).length)) (K.mul a (ev K a b (listC n (k + 1) (m + 1))))
      = K.mul (K.mul (pw K a m) a) (nsm K (C n (k + (m + 1))) K.one)
    have h0 : nsm K (C n k) (pw K b (listC n (k + 1) (m + 1)).length) = K.zero :=
      h 0 _ ⟨m, nadd_comm 1 m⟩
    have h' : ∀ i y, Le (i + 1) m → nsm K (C n (k + 1 + i)) y = K.zero := fun i y hi => by
      rw [nadd_assoc k 1 i, nadd_comm 1 i]
      exact h (i + 1) y (le_succ (i + 1) m hi)
    rw [h0, K.zero_add, ev_tail a b n m (k + 1) h', ← K.mul_assoc, K.mul_comm a (pw K a m),
      nadd_assoc k 1 m, nadd_comm 1 m]

/-- **Frobenius is additive in characteristic p** (p = j + 2 prime, p • 1 = 0 in the ring):
    `(a + b)^p = a^p + b^p`. This holds in every residue ring `O_F/p` of every number field. -/
theorem frob_add (j : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
    (hp : nsm K (j + 1 + 1) K.one = K.zero) (a b : R) :
    pw K (K.add a b) (j + 1 + 1) = K.add (pw K a (j + 1 + 1)) (pw K b (j + 1 + 1)) := by
  rw [← ev_row K a b, row_eq]
  show K.add (nsm K (C (j + 1 + 1) 0) (pw K b (listC (j + 1 + 1) 1 (j + 1 + 1)).length))
      (K.mul a (ev K a b (listC (j + 1 + 1) 1 (j + 1 + 1)))) = _
  have hv : ∀ i y, Le (i + 1) (j + 1) → nsm K (C (j + 1 + 1) (1 + i)) y = K.zero := fun i y hi => by
    rw [nadd_comm 1 i]
    obtain ⟨q, hq⟩ := dv_choose j i hpr (le_succ (i + 1) (j + 1) hi)
    rw [hq]
    exact nsm_char K (j + 1 + 1) q hp y
  rw [ev_tail K a b (j + 1 + 1) (j + 1) 1 hv, len_listC, C_zero_right, nsm_one]
  have hc : C (j + 1 + 1) (1 + (j + 1)) = 1 := by rw [nadd_comm 1 (j + 1)]; exact C_self (j + 1 + 1)
  rw [hc, nsm_one, ← K.mul_assoc, K.mul_comm a (pw K a (j + 1)), K.add_comm]
  show K.add (K.mul (K.mul (pw K a (j + 1)) a) K.one) (pw K b (j + 1 + 1)) = _
  rw [K.mul_comm _ K.one, K.one_mul]
  rfl

theorem pw_mul (a b : R) : ∀ n, pw K (K.mul a b) n = K.mul (pw K a n) (pw K b n)
  | 0 => (K.one_mul K.one).symm
  | n + 1 => by
    show K.mul (pw K (K.mul a b) n) (K.mul a b) = K.mul (K.mul (pw K a n) a) (K.mul (pw K b n) b)
    rw [pw_mul a b n, K.mul_assoc, ← K.mul_assoc (pw K b n) a b, K.mul_comm (pw K b n) a,
      K.mul_assoc a (pw K b n) b, ← K.mul_assoc (pw K a n) a]

end Frob

end Frobenius

#print axioms Frobenius.shell
#print axioms Frobenius.shell_unique
#print axioms Frobenius.nsm_add
#print axioms Frobenius.mul_nsm
#print axioms Frobenius.len_nxt
#print axioms Frobenius.add4
#print axioms Frobenius.nsm_eq_mul
#print axioms Frobenius.perm4
#print axioms Frobenius.swap3'
#print axioms Frobenius.ring_step
#print axioms Frobenius.ev_nxt
#print axioms Frobenius.ev_row
#print axioms Frobenius.C_zero_right
#print axioms Frobenius.C_above
#print axioms Frobenius.C_above'
#print axioms Frobenius.C_self
#print axioms Frobenius.C_one
#print axioms Frobenius.C_absorb
#print axioms Frobenius.le_succ_self_absurd
#print axioms Frobenius.dv_choose
#print axioms Frobenius.nxt_listC
#print axioms Frobenius.row_eq
#print axioms Frobenius.len_listC
#print axioms Frobenius.nsm_one
#print axioms Frobenius.nsm_mul_nat
#print axioms Frobenius.nsm_char
#print axioms Frobenius.ev_tail
#print axioms Frobenius.frob_add
#print axioms Frobenius.pw_mul

Listing 148: lang/FiniteField.lean: \(x^q=x\)

FiniteField.lean

/-
  lang/FiniteField — T43, cycle 3 (GL1 over a general number field): x^q = x in every finite field.

  For a residue field O_F/p with q = Np elements, Frob_p = (x ↦ x^q) fixes every element; with
  Frobenius.frob_add this makes Frob_p a field automorphism of O_L/P over O_F/p.

    prodL_perm      products over two lists with the same multiplicities are equal
    cnt_map         multiplicities of a list mapped by a bijection
    unit_pow        for a unit x of a finite field whose nonzero elements are listed once each
                    (n of them): x^n = 1  (Lagrange, by permuting the product of all units)
    fermat_field    x^(n+1) = x for every x, i.e. x^q = x with q = n + 1

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain
import lang.Frobenius

namespace FiniteField

open Frobenius

variable {R : Type} [DecidableEq R] (K : DetChain.CRing R)

def prodL : List R → R
  | [] => K.one
  | a :: l => K.mul a (prodL l)

def cnt (z : R) : List R → Nat
  | [] => 0
  | a :: l => (if a = z then 1 else 0) + cnt z l

/-- Remove the first occurrence. -/
def erase1 (z : R) : List R → List R
  | [] => []
  | a :: l => if a = z then l else a :: erase1 z l

theorem ite_t {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : c) : @ite α c d a b = a := by
  cases d with
  | isTrue _ => rfl
  | isFalse hn => exact absurd h hn

theorem ite_f {α : Type} {c : Prop} [d : Decidable c] (a b : α) (h : ¬ c) : @ite α c d a b = b := by
  cases d with
  | isTrue hc => exact absurd hc h
  | isFalse _ => rfl

/-- If `z` occurs in `l`, then `prod l = z · prod (erase1 z l)`. -/
theorem prod_erase (z : R) : ∀ l : List R, 0 < cnt z l → prodL K l = K.mul z (prodL K (erase1 z l))
  | [], h => absurd h (Nat.lt_irrefl 0)
  | a :: l, h => by
    by_cases ha : a = z
    · show K.mul a (prodL K l) = K.mul z (prodL K (erase1 z (a :: l)))
      show _ = K.mul z (prodL K (if a = z then l else a :: erase1 z l))
      rw [ite_t _ _ ha, ha]
    · have h' : 0 < cnt z l := by
        have e : cnt z (a :: l) = 0 + cnt z l := by
          show (if a = z then 1 else 0) + cnt z l = 0 + cnt z l
          rw [ite_f _ _ ha]
        rw [e, Nat.zero_add] at h
        exact h
      show K.mul a (prodL K l) = K.mul z (prodL K (if a = z then l else a :: erase1 z l))
      rw [ite_f _ _ ha]
      show K.mul a (prodL K l) = K.mul z (K.mul a (prodL K (erase1 z l)))
      rw [prod_erase z l h', ← K.mul_assoc, K.mul_comm a z, K.mul_assoc]

theorem cnt_erase_same (z : R) : ∀ l : List R, 0 < cnt z l → cnt z (erase1 z l) + 1 = cnt z l
  | [], h => absurd h (Nat.lt_irrefl 0)
  | a :: l, h => by
    by_cases ha : a = z
    · show cnt z (if a = z then l else a :: erase1 z l) + 1 = (if a = z then 1 else 0) + cnt z l
      rw [ite_t _ _ ha, ite_t (1 : Nat) 0 ha, Nat.add_comm]
    · have h' : 0 < cnt z l := by
        have e : cnt z (a :: l) = 0 + cnt z l := by
          show (if a = z then 1 else 0) + cnt z l = 0 + cnt z l
          rw [ite_f _ _ ha]
        rw [e, Nat.zero_add] at h
        exact h
      show cnt z (if a = z then l else a :: erase1 z l) + 1 = (if a = z then 1 else 0) + cnt z l
      rw [ite_f _ _ ha, ite_f (1 : Nat) 0 ha]
      show (if a = z then 1 else 0) + cnt z (erase1 z l) + 1 = 0 + cnt z l
      rw [ite_f (1 : Nat) 0 ha, Nat.zero_add, Nat.zero_add, cnt_erase_same z l h']

theorem cnt_erase_other (z w : R) (hzw : w ≠ z) : ∀ l : List R, cnt w (erase1 z l) = cnt w l
  | [] => rfl
  | a :: l => by
    by_cases ha : a = z
    · show cnt w (if a = z then l else a :: erase1 z l) = (if a = w then 1 else 0) + cnt w l
      rw [ite_t _ _ ha]
      have : ¬ a = w := fun h => hzw (h ▸ ha)
      rw [ite_f (1 : Nat) 0 this, Nat.zero_add]
    · show cnt w (if a = z then l else a :: erase1 z l) = (if a = w then 1 else 0) + cnt w l
      rw [ite_f _ _ ha]
      show (if a = w then 1 else 0) + cnt w (erase1 z l) = _
      rw [cnt_erase_other z w hzw l]

/-- **Products are invariant under permutation** (equal multiplicities). -/
theorem prodL_perm : ∀ (l l' : List R), (∀ z, cnt z l = cnt z l') → prodL K l = prodL K l'
  | [], l', h => by
    cases l' with
    | nil => rfl
    | cons b l' =>
      have := h b
      have e : cnt b (b :: l') = 1 + cnt b l' := by
        show (if b = b then 1 else 0) + cnt b l' = 1 + cnt b l'
        rw [ite_t (1 : Nat) 0 rfl]
      rw [e] at this
      exact absurd this (fun h0 => Nat.noConfusion (h0.trans (Nat.add_comm 1 _)))
  | a :: l, l', h => by
    have hpos : 0 < cnt a l' := by
      rw [← h a]
      show 0 < (if a = a then 1 else 0) + cnt a l
      rw [ite_t (1 : Nat) 0 rfl, Nat.add_comm]
      exact Nat.succ_pos _
    rw [prod_erase K a l' hpos]
    show K.mul a (prodL K l) = _
    have hrest : ∀ z, cnt z l = cnt z (erase1 a l') := fun z => by
      by_cases hz : z = a
      · rw [hz]
        have e1 := cnt_erase_same a l' hpos
        have e2 : cnt a (a :: l) = 1 + cnt a l := by
          show (if a = a then 1 else 0) + cnt a l = 1 + cnt a l
          rw [ite_t (1 : Nat) 0 rfl]
        rw [← h a, e2, Nat.add_comm] at e1
        have e3 : cnt a (erase1 a l') + 1 = cnt a l + 1 := by
          rw [Nat.add_comm (cnt a (erase1 a l')) 1, Nat.add_comm (cnt a l) 1]; exact e1
        exact (Nat.succ.inj e3).symm
      · rw [cnt_erase_other a z hz l']
        have e2 : cnt z (a :: l) = 0 + cnt z l := by
          show (if a = z then 1 else 0) + cnt z l = 0 + cnt z l
          rw [ite_f (1 : Nat) 0 (fun e => hz e.symm)]
        rw [← h z, e2, Nat.zero_add]
    rw [prodL_perm l (erase1 a l') hrest]

/-- Multiplicities of a mapped list, for a bijection `f` with inverse `g`. -/
theorem cnt_map (f g : R → R) (hgf : ∀ x, g (f x) = x) (hfg : ∀ y, f (g y) = y) (z : R) :
    ∀ l : List R, cnt z (l.map f) = cnt (g z) l
  | [] => rfl
  | a :: l => by
    show (if f a = z then 1 else 0) + cnt z (l.map f) = (if a = g z then 1 else 0) + cnt (g z) l
    rw [cnt_map f g hgf hfg z l]
    by_cases ha : a = g z
    · rw [ite_t (1 : Nat) 0 ha, ite_t (1 : Nat) 0 (by rw [ha, hfg])]
    · rw [ite_f (1 : Nat) 0 ha, ite_f (1 : Nat) 0 (fun e => ha (by rw [← e, hgf]))]

omit [DecidableEq R] in
theorem prod_map_mul (x : R) : ∀ l : List R, prodL K (l.map (K.mul x)) = K.mul (pw K x l.length) (prodL K l)
  | [] => (K.one_mul K.one).symm
  | a :: l => by
    show K.mul (K.mul x a) (prodL K (l.map (K.mul x))) = K.mul (K.mul (pw K x l.length) x) (K.mul a (prodL K l))
    rw [prod_map_mul x l, K.mul_assoc, ← K.mul_assoc a, K.mul_comm a (pw K x l.length), K.mul_assoc,
      ← K.mul_assoc x, K.mul_comm x (pw K x l.length), K.mul_assoc]

/-- **Lagrange for the units of a finite field.** `us` lists every nonzero element exactly once;
    `inv` is a two-sided inverse on nonzero elements; the ring has no zero divisors. -/
theorem unit_pow (hK : DetChain.NoZD K) (inv : R → R) (hinv : ∀ x, x ≠ K.zero → K.mul (inv x) x = K.one)
    (us : List R) (hus : ∀ z, cnt z us = if z = K.zero then 0 else 1)
    (hnz : ∀ z, 0 < cnt z us → z ≠ K.zero) (hone : K.one ≠ K.zero)
    (x : R) (hx : x ≠ K.zero) : pw K x us.length = K.one := by
  have hxinv : ∀ y, K.mul x (K.mul (inv x) y) = y := fun y => by
    rw [← K.mul_assoc, K.mul_comm x (inv x), hinv x hx, K.one_mul]
  have hinvx : ∀ y, K.mul (inv x) (K.mul x y) = y := fun y => by
    rw [← K.mul_assoc, hinv x hx, K.one_mul]
  -- x·u ranges over the units as u does
  have hperm : ∀ z, cnt z (us.map (K.mul x)) = cnt z us := fun z => by
    rw [cnt_map (K.mul x) (K.mul (inv x)) hinvx hxinv z us, hus, hus]
    by_cases hz : z = K.zero
    · rw [ite_t (0 : Nat) 1 hz]
      have : K.mul (inv x) z = K.zero := by rw [hz]; exact DetChain.mul_zero K _
      rw [ite_t (0 : Nat) 1 this]
    · rw [ite_f (0 : Nat) 1 hz]
      have : ¬ K.mul (inv x) z = K.zero := fun h0 => by
        apply hz
        have := congrArg (K.mul x) h0
        rw [hxinv, DetChain.mul_zero] at this
        exact this
      rw [ite_f (0 : Nat) 1 this]
  have e := prodL_perm K _ _ hperm
  rw [prod_map_mul] at e
  -- P = prod us ≠ 0
  have hP : ∀ l : List R, (∀ z, 0 < cnt z l → z ≠ K.zero) → prodL K l ≠ K.zero := by
    intro l
    induction l with
    | nil => intro _; exact hone
    | cons a l ih =>
      intro hl h0
      have ha : a ≠ K.zero := hl a (by
        show 0 < (if a = a then 1 else 0) + cnt a l
        rw [ite_t (1 : Nat) 0 rfl, Nat.add_comm]; exact Nat.succ_pos _)
      have hl' : ∀ z, 0 < cnt z l → z ≠ K.zero := fun z hz => hl z (by
        show 0 < (if a = z then 1 else 0) + cnt z l
        exact Nat.lt_of_lt_of_le hz (Nat.le_add_left _ _))
      cases hK a (prodL K l) h0 with
      | inl h => exact ha h
      | inr h => exact ih hl' h
  -- x^n P = P, so (x^n - 1) P = 0
  have e2 : K.mul (K.add (pw K x us.length) (K.neg K.one)) (prodL K us) = K.zero := by
    rw [DetChain.right_distrib, e, DetChain.neg_mul_left, K.one_mul, K.add_neg]
  cases hK _ _ e2 with
  | inl h =>
    have this : K.add (K.add (pw K x us.length) (K.neg K.one)) K.one = K.add K.zero K.one :=
      congrArg (fun t => K.add t K.one) h
    show pw K x us.length = K.one
    rw [K.add_assoc, DetChain.add_neg' K K.one, DetChain.add_zero', K.zero_add] at this
    exact this
  | inr h => exact absurd h (hP us hnz)

/-- **x^q = x in a finite field** (q = n + 1 elements, `us` the n nonzero ones): the Frobenius
    x ↦ x^q of the residue field O_F/p (q = Np) is the identity. -/
theorem fermat_field (hK : DetChain.NoZD K) (inv : R → R) (hinv : ∀ x, x ≠ K.zero → K.mul (inv x) x = K.one)
    (us : List R) (hus : ∀ z, cnt z us = if z = K.zero then 0 else 1)
    (hnz : ∀ z, 0 < cnt z us → z ≠ K.zero) (hone : K.one ≠ K.zero) (x : R) :
    pw K x (us.length + 1) = x := by
  by_cases hx : x = K.zero
  · rw [hx]; exact DetChain.mul_zero K _
  · show K.mul (pw K x us.length) x = x
    rw [unit_pow K hK inv hinv us hus hnz hone x hx, K.one_mul]

end FiniteField

#print axioms FiniteField.ite_t
#print axioms FiniteField.ite_f
#print axioms FiniteField.prod_erase
#print axioms FiniteField.cnt_erase_same
#print axioms FiniteField.cnt_erase_other
#print axioms FiniteField.prodL_perm
#print axioms FiniteField.cnt_map
#print axioms FiniteField.prod_map_mul
#print axioms FiniteField.unit_pow
#print axioms FiniteField.fermat_field

Listing 149: lang/GL2Local.lean

GL2Local.lean

/-
  lang/GL2Local — T43, cycle 4 (GL2 over ℚ): the place/read split at one prime.

  Automorphic side at p: sequences a(p^n) obeying the Hecke recursion
      a(p^(n+2)) = t · a(p^(n+1)) − d · a(p^n),   a(1) = 1,  a(p) = t,     (d = p^(k−1) χ(p))
  Galois side at p: the characteristic data (t, d) = (tr, det) of ρ(Frob_p), with eigenvalues α, β.
    place (t, d)          = the sequence h t d  (h 0 = 1, h 1 = t, Hecke recursion)
    read  a               = (a(p), a(p)^2 − a(p^2))
    read_place            read (place (t, d)) = (t, d)                       (unit iso)
    fix_iff               a = place (read a)  ↔  a obeys the Hecke recursion  (counit iso on Fix)
    h_symm                with t = α + β, d = α β:  h t d n = Σ_{i ≤ n} α^i β^(n−i) = tr Sym^n ρ(Frob_p)
  So at each unramified p "Hecke eigenvalues a(p^n)" and "traces of Sym^n of Frobenius" are the same
  data, in every commutative ring. Checked exactly on Δ (τ(p^n), p ≤ 7) before the proof.

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain
import lang.Frobenius

namespace GL2Local

open Frobenius

variable {R : Type} (K : DetChain.CRing R)

def sub (x y : R) : R := K.add x (K.neg y)

/-- `place (t, d)`: the Hecke-recursive sequence. -/
def h (t d : R) : Nat → R
  | 0 => K.one
  | 1 => t
  | n + 2 => sub K (K.mul t (h t d (n + 1))) (K.mul d (h t d n))

/-- `read a = (a 1, a 1 ^ 2 − a 2)`. -/
def readTD (a : Nat → R) : R × R := (a 1, sub K (K.mul (a 1) (a 1)) (a 2))

def Hecke (a : Nat → R) : Prop :=
  a 0 = K.one ∧ ∀ n, a (n + 2) = sub K (K.mul (a 1) (a (n + 1))) (K.mul (sub K (K.mul (a 1) (a 1)) (a 2)) (a n))

theorem sub_sub_self (x y : R) : sub K x (sub K x y) = y := by
  show K.add x (K.neg (K.add x (K.neg y))) = y
  have e : K.neg (K.add x (K.neg y)) = K.add (K.neg x) y := by
    apply (DetChain.neg_unique K _ _ _).symm
    rw [K.add_assoc, ← K.add_assoc y x (K.neg y), K.add_comm y x, K.add_assoc x y, K.add_neg y,
      DetChain.add_zero', DetChain.add_neg']
  rw [e, ← K.add_assoc, K.add_neg, K.zero_add]

theorem read_place (t d : R) : readTD K (h K t d) = (t, d) := by
  show (t, sub K (K.mul t t) (h K t d 2)) = (t, d)
  have h2 : h K t d 2 = sub K (K.mul t t) (K.mul d K.one) := rfl
  rw [h2, K.mul_comm d K.one, K.one_mul, sub_sub_self]

theorem place_hecke (t d : R) : Hecke K (h K t d) := by
  refine ⟨rfl, fun n => ?_⟩
  have e := congrArg Prod.snd (read_place K t d)
  show h K t d (n + 2) = sub K (K.mul t (h K t d (n + 1))) (K.mul (sub K (K.mul t t) (h K t d 2)) (h K t d n))
  have e' : sub K (K.mul t t) (h K t d 2) = d := e
  rw [e']
  rfl

theorem hecke_eq_place (a : Nat → R) (ha : Hecke K a) :
    ∀ n, a n = h K (readTD K a).1 (readTD K a).2 n
  | 0 => ha.1
  | 1 => rfl
  | n + 2 => by
    rw [ha.2 n, hecke_eq_place a ha (n + 1), hecke_eq_place a ha n]
    rfl

/-- **Counit iso on the fixed points**: `a` is `place (read a)` iff it obeys the Hecke recursion. -/
theorem fix_iff (a : Nat → R) : (∀ n, a n = h K (readTD K a).1 (readTD K a).2 n) ↔ Hecke K a := by
  constructor
  · intro hf
    have hp := place_hecke K (readTD K a).1 (readTD K a).2
    refine ⟨(hf 0).trans hp.1, fun n => ?_⟩
    rw [hf (n + 2), hf (n + 1), hf n, hp.2 n]
    have r := read_place K (readTD K a).1 (readTD K a).2
    have r1 : h K (readTD K a).1 (readTD K a).2 1 = (readTD K a).1 := rfl
    have r2 : sub K (K.mul (h K (readTD K a).1 (readTD K a).2 1) (h K (readTD K a).1 (readTD K a).2 1))
        (h K (readTD K a).1 (readTD K a).2 2) = (readTD K a).2 := congrArg Prod.snd r
    rw [r1] at r2 |-
    rw [r2]
    show sub K (K.mul (a 1) (h K (readTD K a).1 (readTD K a).2 (n + 1))) (K.mul (readTD K a).2 (h K (readTD K a).1 (readTD K a).2 n))
      = sub K (K.mul (a 1) (h K (readTD K a).1 (readTD K a).2 (n + 1))) (K.mul (sub K (K.mul (a 1) (a 1)) (a 2)) (h K (readTD K a).1 (readTD K a).2 n))
    rfl
  · exact hecke_eq_place K a

/-- `Σ_{i ≤ n} α^i β^(n−i)`, by `s (n+1) = α^(n+1) + β · s n`. -/
def symTr (α β : R) : Nat → R
  | 0 => K.one
  | n + 1 => K.add (pw K α (n + 1)) (K.mul β (symTr α β n))

/-- **Hecke eigenvalues are symmetric-power traces of Frobenius.** -/
theorem h_symm (α β : R) : ∀ n, h K (K.add α β) (K.mul α β) n = symTr K α β n
  | 0 => rfl
  | 1 => by
    show K.add α β = K.add (K.mul K.one α) (K.mul β K.one)
    rw [K.one_mul, K.mul_comm β K.one, K.one_mul]
  | n + 2 => by
    show sub K (K.mul (K.add α β) (h K (K.add α β) (K.mul α β) (n + 1)))
        (K.mul (K.mul α β) (h K (K.add α β) (K.mul α β) n))
      = K.add (pw K α (n + 2)) (K.mul β (symTr K α β (n + 1)))
    rw [h_symm α β (n + 1), h_symm α β n]
    -- α s(n+1) = α^(n+2) + αβ s(n)
    have e1 : K.mul α (symTr K α β (n + 1)) = K.add (pw K α (n + 2)) (K.mul (K.mul α β) (symTr K α β n)) := by
      show K.mul α (K.add (pw K α (n + 1)) (K.mul β (symTr K α β n))) = _
      rw [K.distrib, K.mul_comm α (pw K α (n + 1)), ← K.mul_assoc]
      rfl
    show K.add (K.mul (K.add α β) (symTr K α β (n + 1))) (K.neg (K.mul (K.mul α β) (symTr K α β n))) = _
    rw [DetChain.right_distrib, e1, K.add_assoc, K.add_assoc, K.add_comm (K.mul β (symTr K α β (n + 1))),
      ← K.add_assoc (K.mul (K.mul α β) (symTr K α β n)), K.add_neg, K.zero_add]

end GL2Local

#print axioms GL2Local.sub_sub_self
#print axioms GL2Local.read_place
#print axioms GL2Local.place_hecke
#print axioms GL2Local.hecke_eq_place
#print axioms GL2Local.fix_iff
#print axioms GL2Local.h_symm

Listing 150: lang/Dihedral.lean

Dihedral.lean

/-
  lang/Dihedral — T43, cycle 5 (GL2 over ℚ, CM / dihedral case from GL1 over K).

  G = Gal(L/ℚ), H = Gal(L/K) of index 2 (K imaginary quadratic), σ ∈ G \ H. For a character ψ of H
  (a Hecke character of K, through GL1 over K), the induced representation Ind_H^G ψ has trace
      split p  (Frob_p ∈ H):   ψ(Frob) + ψ(σ Frob σ⁻¹)      = ψ(p) + ψ(p̄)
      inert p  (Frob_p ∉ H):   0
  which is the theta series a(p) of ψ. In the normalisation of FiniteRecChain (Ind = |H| × trace):
    ind_split    x ∈ H  ⇒  ind ψ x = |H| ψ(x) + |H| ψ(σ x σ⁻¹)
    ind_inert    x ∉ H  ⇒  ind ψ x = 0
  Hypotheses: G = H ∪ σH as lists (hsplit), H normal and closed under conjugation, ψ a class
  function on H. Checked first on y² = x³ − x (CM by ℚ(i)): a_p = π + π̄ (π ≡ 1 mod 2+2i) for
  p ≡ 1 (4) and a_p = 0 for p ≡ 3 (4), all p < 400, exactly.

  Lean 4 core only, proofs by hand, no axioms.
-/
import FiniteRecChain

namespace Dihedral

open YMChain FiniteRecChain

variable {R : Type} (Rg : DetChain.CRing R) {G : Type} [DecidableEq G] (K : Grp G)

def conjG (a y : G) : G := K.mul (K.mul a y) (K.inv a)

omit [DecidableEq G] in
theorem sum_const {A : Type} (c : R) (f : A → R) : ∀ l : List A, (∀ a, memL a l → f a = c) →
    sumK Rg f l = Rg.mul (sumK Rg (fun _ => Rg.one) l) c
  | [], _ => (DetChain.zero_mul' Rg c).symm
  | a :: l, h => by
    show Rg.add (f a) (sumK Rg f l) = Rg.mul (Rg.add Rg.one (sumK Rg (fun _ => Rg.one) l)) c
    rw [h a (Or.inl rfl), sum_const c f l (fun b hb => h b (Or.inr hb)), DetChain.right_distrib, Rg.one_mul]

variable (inH : G → Bool)

omit [DecidableEq G] in
/-- **Split primes.** -/
theorem ind_split (els Hl : List G) (σ : G) (ψ : G → R)
    (hsplit : ∀ F : G → R, sumK Rg F els = Rg.add (sumK Rg F Hl) (sumK Rg (fun h => F (K.mul σ h)) Hl))
    (hHl : ∀ h, memL h Hl → inH h = true)
    (hcl : ∀ h y, inH h = true → inH y = true → inH (conjG K h y) = true)
    (hnorm : ∀ y, inH y = true → inH (conjG K σ y) = true)
    (hcls : ∀ h y, inH h = true → inH y = true → ψ (conjG K h y) = ψ y)
    (x : G) (hx : inH x = true) :
    ind Rg K els inH ψ x
      = Rg.add (Rg.mul (sumK Rg (fun _ => Rg.one) Hl) (ψ x))
               (Rg.mul (sumK Rg (fun _ => Rg.one) Hl) (ψ (conjG K σ x))) := by
  unfold ind
  rw [hsplit]
  have e1 : sumK Rg (fun g => ext0 Rg inH ψ (K.mul (K.mul g x) (K.inv g))) Hl
      = Rg.mul (sumK Rg (fun _ => Rg.one) Hl) (ψ x) :=
    sum_const Rg (ψ x) _ Hl (fun h hh => by
      show (if inH (conjG K h x) = true then ψ (conjG K h x) else Rg.zero) = ψ x
      rw [ite_pos' _ _ (hcl h x (hHl h hh) hx)]
      exact hcls h x (hHl h hh) hx)
  have e2 : sumK Rg (fun h => ext0 Rg inH ψ (K.mul (K.mul (K.mul σ h) x) (K.inv (K.mul σ h)))) Hl
      = Rg.mul (sumK Rg (fun _ => Rg.one) Hl) (ψ (conjG K σ x)) :=
    sum_const Rg (ψ (conjG K σ x)) _ Hl (fun h hh => by
      -- (σh) x (σh)⁻¹ = (σ h σ⁻¹) (σ x σ⁻¹) (σ h σ⁻¹)⁻¹
      have hc : K.mul (K.mul (K.mul σ h) x) (K.inv (K.mul σ h)) = conjG K (conjG K σ h) (conjG K σ x) := by
        have cc := conj_conj K (conjG K σ h) σ x
        have e : K.mul (conjG K σ h) σ = K.mul σ h := by
          show K.mul (K.mul (K.mul σ h) (K.inv σ)) σ = K.mul σ h
          rw [K.assoc, K.inv_mul, K.mul_one]
        rw [e] at cc
        exact cc.symm
      show ext0 Rg inH ψ (K.mul (K.mul (K.mul σ h) x) (K.inv (K.mul σ h))) = ψ (conjG K σ x)
      rw [hc]
      have hh' := hnorm h (hHl h hh)
      have hx' := hnorm x hx
      show (if inH (conjG K (conjG K σ h) (conjG K σ x)) = true then ψ (conjG K (conjG K σ h) (conjG K σ x))
        else Rg.zero) = ψ (conjG K σ x)
      rw [ite_pos' _ _ (hcl _ _ hh' hx')]
      exact hcls _ _ hh' hx')
  rw [e1, e2]

omit [DecidableEq G] in
/-- **Inert primes.** -/
theorem ind_inert (els : List G) (ψ : G → R)
    (hout : ∀ g y, inH y = false → inH (conjG K g y) = false) (x : G) (hx : inH x = false) :
    ind Rg K els inH ψ x = Rg.zero := by
  unfold ind
  have : ∀ l : List G, sumK Rg (fun g => ext0 Rg inH ψ (K.mul (K.mul g x) (K.inv g))) l = Rg.zero := by
    intro l
    induction l with
    | nil => rfl
    | cons g l ih =>
      have hf : ¬ inH (conjG K g x) = true := by rw [hout g x hx]; exact Bool.noConfusion
      have hz : ext0 Rg inH ψ (conjG K g x) = Rg.zero := by
        show (if inH (conjG K g x) = true then ψ (conjG K g x) else Rg.zero) = Rg.zero
        rw [ite_neg' _ _ hf]
      show Rg.add (ext0 Rg inH ψ (conjG K g x)) _ = Rg.zero
      rw [hz, ih, Rg.zero_add]
  exact this els

end Dihedral

#print axioms Dihedral.sum_const
#print axioms Dihedral.ind_split
#print axioms Dihedral.ind_inert

Listing 151: lang/GLnLocal.lean

GLnLocal.lean

/-
  lang/GLnLocal — T43, cycle 6 (GL_n at an unramified place, every n).

  Satake / Frobenius data α = (α_1, …, α_n) (a list in any commutative ring).
    cf α     coefficients of Π (1 − α_i X)        (cf (a :: α) (k+1) = cf α (k+1) − a · cf α k)
    hk α     complete homogeneous h_k(α)          (hk (a :: α) (k+1) = hk α (k+1) + a · hk (a :: α) k)
             = tr Sym^k of diag(α) = the Hecke eigenvalue of T_(p^k) for GL_n
    conv     Cauchy product of sequences, by conv c h (k+1) = c 0 · h (k+1) + conv (c ∘ succ) h k
  Proved:
    conv_cf_hk     conv (cf α) (hk α) = δ_0, i.e.  Σ_k h_k X^k = 1 / Π (1 − α_i X)  (the local L-factor)
    conv_unique    if c 0 = 1, the sequence h with conv c h = δ_0 is unique
  So at an unramified p the Hecke eigen-system (a(p^k))_k and the Satake polynomial Π (1 − α_i X)
  determine each other (place = solve conv c h = δ, read = the polynomial), for every n.

  Lean 4 core only, proofs by hand, no axioms.
-/
import DetChain

namespace GLnLocal

variable {R : Type} (K : DetChain.CRing R)

def cf : List R → Nat → R
  | [], 0 => K.one
  | [], _ + 1 => K.zero
  | _ :: α, 0 => cf α 0
  | a :: α, k + 1 => K.add (cf α (k + 1)) (K.neg (K.mul a (cf α k)))

/-- One more variable: `hkc a H 0 = H 0`, `hkc a H (k+1) = H (k+1) + a · hkc a H k`. -/
def hkc (a : R) (H : Nat → R) : Nat → R
  | 0 => H 0
  | k + 1 => K.add (H (k + 1)) (K.mul a (hkc a H k))

def hk : List R → Nat → R
  | [] => fun k => match k with
    | 0 => K.one
    | _ + 1 => K.zero
  | a :: α => hkc K a (hk α)

/-- Cauchy product. -/
def conv : (Nat → R) → (Nat → R) → Nat → R
  | c, h, 0 => K.mul (c 0) (h 0)
  | c, h, k + 1 => K.add (K.mul (c 0) (h (k + 1))) (conv (fun i => c (i + 1)) h k)

/-- Shift right: `(sR h) 0 = 0`, `(sR h) (k+1) = h k`. -/
def sR (h : Nat → R) : Nat → R
  | 0 => K.zero
  | k + 1 => h k

def delta : Nat → R
  | 0 => K.one
  | _ + 1 => K.zero

theorem conv_ext (c h g : Nat → R) (hg : ∀ i, h i = g i) : ∀ k, conv K c h k = conv K c g k := by
  intro k
  induction k generalizing c with
  | zero => show K.mul (c 0) (h 0) = K.mul (c 0) (g 0); rw [hg 0]
  | succ k ih =>
    show K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k)
      = K.add (K.mul (c 0) (g (k + 1))) (conv K (fun i => c (i + 1)) g k)
    rw [hg (k + 1), ih]

theorem conv_ext_left (c d h : Nat → R) (hcd : ∀ i, c i = d i) : ∀ k, conv K c h k = conv K d h k := by
  intro k
  induction k generalizing c d with
  | zero => show K.mul (c 0) (h 0) = K.mul (d 0) (h 0); rw [hcd 0]
  | succ k ih =>
    show K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k)
      = K.add (K.mul (d 0) (h (k + 1))) (conv K (fun i => d (i + 1)) h k)
    rw [hcd 0, ih (fun i => c (i + 1)) (fun i => d (i + 1)) (fun i => hcd (i + 1))]

theorem conv_add (c h g : Nat → R) : ∀ k, conv K c (fun i => K.add (h i) (g i)) k = K.add (conv K c h k) (conv K c g k) := by
  intro k
  induction k generalizing c with
  | zero => exact K.distrib _ _ _
  | succ k ih =>
    show K.add (K.mul (c 0) (K.add (h (k + 1)) (g (k + 1)))) (conv K (fun i => c (i + 1)) (fun i => K.add (h i) (g i)) k)
      = K.add (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
          (K.add (K.mul (c 0) (g (k + 1))) (conv K (fun i => c (i + 1)) g k))
    rw [ih, K.distrib, DetChain.add_mid4]

theorem conv_add_left (c d h : Nat → R) : ∀ k, conv K (fun i => K.add (c i) (d i)) h k = K.add (conv K c h k) (conv K d h k) := by
  intro k
  induction k generalizing c d with
  | zero => exact DetChain.right_distrib K _ _ _
  | succ k ih =>
    show K.add (K.mul (K.add (c 0) (d 0)) (h (k + 1))) (conv K (fun i => K.add (c (i + 1)) (d (i + 1))) h k)
      = K.add (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
          (K.add (K.mul (d 0) (h (k + 1))) (conv K (fun i => d (i + 1)) h k))
    rw [ih (fun i => c (i + 1)) (fun i => d (i + 1)), DetChain.right_distrib, DetChain.add_mid4]

theorem conv_smul_left (c h : Nat → R) (a : R) : ∀ k, conv K (fun i => K.mul a (c i)) h k = K.mul a (conv K c h k) := by
  intro k
  induction k generalizing c with
  | zero => show K.mul (K.mul a (c 0)) (h 0) = K.mul a (K.mul (c 0) (h 0)); exact K.mul_assoc _ _ _
  | succ k ih =>
    show K.add (K.mul (K.mul a (c 0)) (h (k + 1))) (conv K (fun i => K.mul a (c (i + 1))) h k)
      = K.mul a (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
    rw [ih (fun i => c (i + 1)), K.distrib, K.mul_assoc]

theorem conv_smul (c h : Nat → R) (a : R) : ∀ k, conv K c (fun i => K.mul a (h i)) k = K.mul a (conv K c h k) := by
  intro k
  induction k generalizing c with
  | zero =>
    show K.mul (c 0) (K.mul a (h 0)) = K.mul a (K.mul (c 0) (h 0))
    rw [← K.mul_assoc, K.mul_comm (c 0) a, K.mul_assoc]
  | succ k ih =>
    show K.add (K.mul (c 0) (K.mul a (h (k + 1)))) (conv K (fun i => c (i + 1)) (fun i => K.mul a (h i)) k)
      = K.mul a (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
    rw [ih, K.distrib, ← K.mul_assoc, K.mul_comm (c 0) a, K.mul_assoc]

theorem conv_zero_left (c h : Nat → R) (hc : ∀ i, c i = K.zero) : ∀ k, conv K c h k = K.zero := by
  intro k
  induction k generalizing c with
  | zero => show K.mul (c 0) (h 0) = K.zero; rw [hc 0, DetChain.zero_mul']
  | succ k ih =>
    show K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k) = K.zero
    rw [hc 0, DetChain.zero_mul', ih (fun i => c (i + 1)) (fun i => hc (i + 1)), K.zero_add]

theorem conv_sR_right (c h : Nat → R) : ∀ k, conv K c (sR K h) (k + 1) = conv K c h k := by
  intro k
  induction k generalizing c with
  | zero =>
    show K.add (K.mul (c 0) (h 0)) (K.mul (c 1) K.zero) = K.mul (c 0) (h 0)
    rw [DetChain.mul_zero, DetChain.add_zero']
  | succ k ih =>
    show K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) (sR K h) (k + 1))
      = K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k)
    rw [ih]

theorem conv_sR_right0 (c h : Nat → R) : conv K c (sR K h) 0 = K.zero := DetChain.mul_zero K _

/-- `conv c (sR h) = sR (conv c h)` pointwise. -/
theorem conv_sR (c h : Nat → R) : ∀ k, conv K c (sR K h) k = sR K (conv K c h) k
  | 0 => conv_sR_right0 K c h
  | k + 1 => conv_sR_right K c h k

theorem cf_cons (a : R) (α : List R) (k : Nat) :
    cf K (a :: α) k = K.add (cf K α k) (K.neg (K.mul a (sR K (cf K α) k))) := by
  cases k with
  | zero =>
    show cf K α 0 = K.add (cf K α 0) (K.neg (K.mul a K.zero))
    rw [DetChain.mul_zero]
    have : K.neg K.zero = K.zero := (DetChain.neg_unique K _ _ (K.zero_add K.zero)).symm
    rw [this, DetChain.add_zero']
  | succ k => rfl

theorem hk_cons (a : R) (α : List R) (k : Nat) :
    hk K (a :: α) k = K.add (hk K α k) (K.mul a (sR K (hk K (a :: α)) k)) := by
  cases k with
  | zero =>
    show hk K α 0 = K.add (hk K α 0) (K.mul a K.zero)
    rw [DetChain.mul_zero, DetChain.add_zero']
  | succ k => rfl

theorem neg_as_mul (x : R) : K.neg x = K.mul (K.neg K.one) x := by
  rw [DetChain.neg_mul_left, K.one_mul]

/-- **The local L-factor**: `Σ h_k(α) X^k · Π (1 − α_i X) = 1`. -/
theorem conv_cf_hk : ∀ (α : List R) (k : Nat), conv K (cf K α) (hk K α) k = delta K k
  | [], 0 => K.one_mul K.one
  | [], k + 1 => by
    show K.add (K.mul K.one K.zero) (conv K (fun i => cf K [] (i + 1)) (hk K []) k) = K.zero
    rw [DetChain.mul_zero, K.zero_add]
    exact conv_zero_left K _ _ (fun i => rfl) k
  | a :: α, k => by
    -- D = conv (cf α) (hk (a::α)) satisfies D = δ + a sR D
    have hD : ∀ j, conv K (cf K α) (hk K (a :: α)) j
        = K.add (delta K j) (K.mul a (sR K (conv K (cf K α) (hk K (a :: α))) j)) := fun j => by
      rw [conv_ext K _ _ _ (hk_cons K a α) j, conv_add, conv_smul, conv_cf_hk α j, conv_sR]
    -- conv (cf (a::α)) H' = D − a sR D
    rw [conv_ext_left K _ _ _ (cf_cons K a α) k, conv_add_left,
      conv_ext_left K (fun i => K.neg (K.mul a (sR K (cf K α) i))) (fun i => K.mul (K.neg a) (sR K (cf K α) i)) _
        (fun i => by rw [neg_as_mul K, ← K.mul_assoc, ← neg_as_mul K]) k,
      conv_smul_left]
    -- conv (sR c) h = sR (conv c h)
    have hsl : ∀ j, conv K (sR K (cf K α)) (hk K (a :: α)) j = sR K (conv K (cf K α) (hk K (a :: α))) j := by
      intro j
      cases j with
      | zero => show K.mul K.zero _ = K.zero; exact DetChain.zero_mul' K _
      | succ j =>
        show K.add (K.mul K.zero _) (conv K (fun i => sR K (cf K α) (i + 1)) (hk K (a :: α)) j) = _
        rw [DetChain.zero_mul', K.zero_add]
        exact conv_ext_left K _ _ _ (fun i => rfl) j
    rw [hsl k, hD k, K.add_assoc, DetChain.neg_mul_left K a, K.add_neg, DetChain.add_zero']

/-- `conv d h m` depends only on `h 0, …, h m`. -/
theorem conv_dep (d h g : Nat → R) : ∀ m, (∀ j, j ≤ m → h j = g j) → conv K d h m = conv K d g m := by
  intro m
  induction m generalizing d with
  | zero => intro hj; show K.mul (d 0) (h 0) = K.mul (d 0) (g 0); rw [hj 0 (Nat.le_refl 0)]
  | succ m ih =>
    intro hj
    show K.add (K.mul (d 0) (h (m + 1))) (conv K (fun i => d (i + 1)) h m)
      = K.add (K.mul (d 0) (g (m + 1))) (conv K (fun i => d (i + 1)) g m)
    rw [hj (m + 1) (Nat.le_refl _), ih (fun i => d (i + 1)) (fun j hjm => hj j (Nat.le_succ_of_le hjm))]

/-- **Uniqueness**: with `c 0 = 1`, `conv c h = δ` determines `h`. -/
theorem conv_unique (c h g : Nat → R) (hc : c 0 = K.one)
    (hh : ∀ k, conv K c h k = delta K k) (hg : ∀ k, conv K c g k = delta K k) : ∀ k, ∀ j, j ≤ k → h j = g j := by
  intro k
  induction k with
  | zero =>
    intro j hj
    have hj0 : j = 0 := Nat.le_zero.mp hj
    rw [hj0]
    have e := (hh 0).trans (hg 0).symm
    show h 0 = g 0
    have e' : K.mul (c 0) (h 0) = K.mul (c 0) (g 0) := e
    rw [hc, K.one_mul, K.one_mul] at e'
    exact e'
  | succ k ih =>
    intro j hj
    cases Nat.lt_or_eq_of_le hj with
    | inl hlt => exact ih j (Nat.le_of_lt_succ hlt)
    | inr heq =>
      rw [heq]
      have e := (hh (k + 1)).trans (hg (k + 1)).symm
      have e' : K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k)
          = K.add (K.mul (c 0) (g (k + 1))) (conv K (fun i => c (i + 1)) g k) := e
      rw [conv_dep K _ h g k ih, hc, K.one_mul, K.one_mul] at e'
      have := congrArg (fun t => K.add t (K.neg (conv K (fun i => c (i + 1)) g k))) e'
      show h (k + 1) = g (k + 1)
      have t1 : K.add (K.add (h (k + 1)) (conv K (fun i => c (i + 1)) g k)) (K.neg (conv K (fun i => c (i + 1)) g k))
          = K.add (K.add (g (k + 1)) (conv K (fun i => c (i + 1)) g k)) (K.neg (conv K (fun i => c (i + 1)) g k)) := this
      rw [K.add_assoc, K.add_neg, DetChain.add_zero', K.add_assoc, K.add_neg, DetChain.add_zero'] at t1
      exact t1

end GLnLocal

#print axioms GLnLocal.conv_ext
#print axioms GLnLocal.conv_ext_left
#print axioms GLnLocal.conv_add
#print axioms GLnLocal.conv_add_left
#print axioms GLnLocal.conv_smul_left
#print axioms GLnLocal.conv_smul
#print axioms GLnLocal.conv_zero_left
#print axioms GLnLocal.conv_sR_right
#print axioms GLnLocal.conv_sR_right0
#print axioms GLnLocal.conv_sR
#print axioms GLnLocal.cf_cons
#print axioms GLnLocal.hk_cons
#print axioms GLnLocal.neg_as_mul
#print axioms GLnLocal.conv_cf_hk
#print axioms GLnLocal.conv_dep
#print axioms GLnLocal.conv_unique

Listing 152: lang/Sym2Local.lean

Sym2Local.lean

/-
  lang/Sym2Local — T43, cycle 7 (functoriality, local, unramified): Sym² : GL2 → GL3.

  The L-homomorphism Sym² sends Satake data (α, β) of GL2 to (α², αβ, β²) of GL3. Its Satake polynomial
      Π (1 − λ X) over λ ∈ {α², αβ, β²}  =  1 − (t² − d) X + d (t² − d) X² − d³ X³,   t = α + β, d = αβ,
  depends only on (t, d) = (a(p), p^(k−1)χ(p)) — the GL2 eigen-system at p (GL2Local). With
  GLnLocal.conv_unique the GL3 eigen-system of the lift at p is therefore determined by that of π:
    cf3_k            cf [a, b, c] = 1, −(a + b + c), ab + ac + bc, −abc, 0, 0, …
    sym2_poly        cf [α², αβ, β²] = 1, −(t² − d), d(t² − d), −d³, 0, …
    sym2_lift        the lifted Hecke eigen-system h_k(α², αβ, β²) is the unique solution of
                     conv (c_lift(t, d)) h = δ  — local Sym² functoriality at unramified p
  Checked first on 200 random integer pairs (α, β).

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.GLnLocal

namespace Sym2Local

open GLnLocal

variable {R : Type} (K : DetChain.CRing R)

def neg' (x : R) : R := K.neg x

theorem neg_zero' : K.neg K.zero = K.zero := (DetChain.neg_unique K _ _ (K.zero_add K.zero)).symm

theorem neg_add_distrib (x y : R) : K.neg (K.add x y) = K.add (K.neg x) (K.neg y) := by
  apply (DetChain.neg_unique K _ _ _).symm
  rw [DetChain.add_mid4, DetChain.add_neg', DetChain.add_neg', K.zero_add]

theorem neg_mul_neg (x y : R) : K.mul (K.neg x) (K.neg y) = K.mul x y := by
  rw [DetChain.neg_mul_left, K.mul_comm x, DetChain.neg_mul_left, DetChain.neg_neg', K.mul_comm]

theorem mul_neg' (x y : R) : K.mul x (K.neg y) = K.neg (K.mul x y) := by
  rw [K.mul_comm, DetChain.neg_mul_left, K.mul_comm]

/-- `cf [a, b, c]` in closed form. -/
theorem cf3_0 (a b c : R) : cf K [a, b, c] 0 = K.one := rfl

theorem cf3_1 (a b c : R) : cf K [a, b, c] 1 = K.neg (K.add (K.add a b) c) := by
  show K.add (K.add (K.add K.zero (K.neg (K.mul c K.one))) (K.neg (K.mul b K.one))) (K.neg (K.mul a K.one)) = _
  rw [K.mul_comm c, K.mul_comm b, K.mul_comm a, K.one_mul, K.one_mul, K.one_mul, K.zero_add,
    neg_add_distrib, neg_add_distrib, K.add_comm (K.neg c) (K.neg b), K.add_assoc, K.add_comm]
  rw [K.add_comm (K.neg c) (K.neg a)]
  rw [K.add_assoc, K.add_comm (K.neg c) (K.neg b), ← K.add_assoc]

theorem cf3_2 (a b c : R) : cf K [a, b, c] 2 = K.add (K.add (K.mul a b) (K.mul a c)) (K.mul b c) := by
  show K.add (K.add (K.add K.zero (K.neg (K.mul c K.zero)))
      (K.neg (K.mul b (K.add K.zero (K.neg (K.mul c K.one))))))
      (K.neg (K.mul a (K.add (K.add K.zero (K.neg (K.mul c K.one))) (K.neg (K.mul b K.one))))) = _
  rw [DetChain.mul_zero, neg_zero', K.zero_add, K.zero_add, K.mul_comm c K.one, K.mul_comm b K.one,
    K.one_mul, K.one_mul, K.zero_add, mul_neg', DetChain.neg_neg', K.distrib, mul_neg', mul_neg',
    neg_add_distrib, DetChain.neg_neg', DetChain.neg_neg']
  rw [K.add_comm (K.mul a c) (K.mul a b), K.add_comm (K.mul b c), K.add_assoc]

theorem cf3_3 (a b c : R) : cf K [a, b, c] 3 = K.neg (K.mul (K.mul a b) c) := by
  show K.add (K.add (K.add K.zero (K.neg (K.mul c K.zero)))
      (K.neg (K.mul b (K.add K.zero (K.neg (K.mul c K.zero))))))
      (K.neg (K.mul a (K.add (K.add K.zero (K.neg (K.mul c K.zero)))
        (K.neg (K.mul b (K.add K.zero (K.neg (K.mul c K.one)))))))) = _
  rw [DetChain.mul_zero, neg_zero', K.zero_add, DetChain.mul_zero, neg_zero', K.zero_add, K.zero_add,
    K.mul_comm c K.one, K.one_mul, K.zero_add, mul_neg', DetChain.neg_neg', K.zero_add, K.mul_assoc]
  rw [mul_neg' K b c, mul_neg' K a]

theorem cf3_high (a b c : R) (k : Nat) : cf K [a, b, c] (k + 4) = K.zero := by
  show K.add (K.add (K.add K.zero (K.neg (K.mul c K.zero)))
      (K.neg (K.mul b (K.add K.zero (K.neg (K.mul c K.zero))))))
      (K.neg (K.mul a (K.add (K.add K.zero (K.neg (K.mul c K.zero)))
        (K.neg (K.mul b (K.add K.zero (K.neg (K.mul c K.zero)))))))) = K.zero
  rw [DetChain.mul_zero, neg_zero', K.zero_add, DetChain.mul_zero, neg_zero', K.zero_add,
    DetChain.mul_zero, neg_zero', K.zero_add]

/-- `t·t − d = α² + αβ + β²` for `t = α + β`, `d = αβ`. -/
theorem tt_sub (α β : R) :
    K.add (K.mul (K.add α β) (K.add α β)) (K.neg (K.mul α β))
      = K.add (K.add (K.mul α α) (K.mul α β)) (K.mul β β) := by
  rw [DetChain.right_distrib, K.distrib, K.distrib, K.mul_comm β α, K.add_assoc,
    K.add_assoc (K.mul α β) (K.mul β β) (K.neg (K.mul α β)), K.add_comm (K.mul β β) (K.neg (K.mul α β)),
    ← K.add_assoc (K.mul α β) (K.neg (K.mul α β)), K.add_neg, K.zero_add]

/-- The Sym² Satake polynomial in terms of `(t, d)`. -/
def cLift (t d : R) : Nat → R
  | 0 => K.one
  | 1 => K.neg (K.add (K.mul t t) (K.neg d))
  | 2 => K.mul d (K.add (K.mul t t) (K.neg d))
  | 3 => K.neg (K.mul (K.mul d d) d)
  | _ + 4 => K.zero

/-- **The lift's Satake polynomial depends only on (t, d).** -/
theorem sym2_poly (α β : R) : ∀ k,
    cf K [K.mul α α, K.mul α β, K.mul β β] k = cLift K (K.add α β) (K.mul α β) k
  | 0 => rfl
  | 1 => by
    rw [cf3_1]
    show _ = K.neg (K.add (K.mul (K.add α β) (K.add α β)) (K.neg (K.mul α β)))
    rw [tt_sub]
  | 2 => by
    rw [cf3_2]
    show _ = K.mul (K.mul α β) (K.add (K.mul (K.add α β) (K.add α β)) (K.neg (K.mul α β)))
    rw [tt_sub, K.distrib, K.distrib]
    -- α²·αβ + α²·β² + αβ·β²  =  αβ·α² + αβ·αβ + αβ·β²
    have e1 : K.mul (K.mul α α) (K.mul α β) = K.mul (K.mul α β) (K.mul α α) := K.mul_comm _ _
    have e2 : K.mul (K.mul α α) (K.mul β β) = K.mul (K.mul α β) (K.mul α β) := by
      rw [K.mul_assoc, ← K.mul_assoc α β β, K.mul_comm (K.mul α β) β, ← K.mul_assoc, ← K.mul_assoc,
        K.mul_assoc (K.mul α β)]
    rw [e1, e2]
  | 3 => by
    rw [cf3_3]
    show _ = K.neg (K.mul (K.mul (K.mul α β) (K.mul α β)) (K.mul α β))
    have e : K.mul (K.mul (K.mul α α) (K.mul α β)) (K.mul β β)
        = K.mul (K.mul (K.mul α β) (K.mul α β)) (K.mul α β) := by
      rw [K.mul_assoc (K.mul α α) (K.mul α β), K.mul_comm (K.mul α β) (K.mul β β), ← K.mul_assoc (K.mul α α)]
      have e2 : K.mul (K.mul α α) (K.mul β β) = K.mul (K.mul α β) (K.mul α β) := by
        rw [K.mul_assoc, ← K.mul_assoc α β β, K.mul_comm (K.mul α β) β, ← K.mul_assoc, ← K.mul_assoc,
          K.mul_assoc (K.mul α β)]
      rw [e2]
    rw [e]
  | k + 4 => cf3_high K _ _ _ k

/-- **Local Sym² functoriality.** The lifted GL3 eigen-system at `p`, `h_k(α², αβ, β²)`, is the unique
    solution of `conv (cLift t d) h = δ`; so it is a function of the GL2 data `(t, d)` at `p`. -/
theorem sym2_lift (α β : R) (g : Nat → R)
    (hg : ∀ k, conv K (cLift K (K.add α β) (K.mul α β)) g k = delta K k) :
    ∀ k, hk K [K.mul α α, K.mul α β, K.mul β β] k = g k := fun k =>
  conv_unique K (cLift K (K.add α β) (K.mul α β)) _ g rfl
    (fun j => (conv_ext_left K _ _ _ (fun i => (sym2_poly K α β i).symm) j).trans
      (conv_cf_hk K [K.mul α α, K.mul α β, K.mul β β] j)) hg k k (Nat.le_refl k)

end Sym2Local

#print axioms Sym2Local.neg_zero'
#print axioms Sym2Local.neg_add_distrib
#print axioms Sym2Local.neg_mul_neg
#print axioms Sym2Local.mul_neg'
#print axioms Sym2Local.cf3_0
#print axioms Sym2Local.cf3_1
#print axioms Sym2Local.cf3_2
#print axioms Sym2Local.cf3_3
#print axioms Sym2Local.cf3_high
#print axioms Sym2Local.tt_sub
#print axioms Sym2Local.sym2_poly
#print axioms Sym2Local.sym2_lift

Listing 153: lang/SymNLocal.lean

SymNLocal.lean

/-
  lang/SymNLocal — T43, cycle 8 (functoriality, local, unramified): Sym^n : GL2 → GL_(n+1), every n.

  Satake data of Sym^n: the multiset {α^(n−i) β^i}, listed recursively as
      symL 0 = [1],  symL 1 = [α, β],  symL (n+2) = [α^(n+2), β^(n+2)] ++ (αβ) · symL n.
  General facts about Satake polynomials (any commutative ring):
    cf_append    cf (l₁ ++ l₂) = cf l₁ * cf l₂        (Cauchy product)
    cf_scale     cf (d · l) k = d^k · cf l k
    psum_eq      power sums α^n + β^n satisfy s(n+2) = t s(n+1) − d s(n), t = α + β, d = αβ
  Hence (symn_poly) the Sym^n Satake polynomial equals Q_n(t, d), a polynomial in (t, d) alone:
      Q_(n+2) = (1 − s_(n+2) X + d^(n+2) X²) · Q_n(d X),
  and (symn_lift) the lifted GL_(n+1) eigen-system at p is the unique solution of conv (Q_n) h = δ:
  determined by the GL2 data (a(p), p^(k−1)χ(p)). Checked first for n ≤ 6 on random integers.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.GLnLocal
import lang.Frobenius
import lang.Sym2Local

namespace SymNLocal

open GLnLocal

variable {R : Type} (K : DetChain.CRing R)

abbrev pw := Frobenius.pw K

theorem cf_nil (k : Nat) : cf K [] k = delta K k := by cases k <;> rfl

theorem conv_delta_left (h : Nat → R) : ∀ k, conv K (delta K) h k = h k
  | 0 => K.one_mul _
  | k + 1 => by
    show K.add (K.mul K.one (h (k + 1))) (conv K (fun i => delta K (i + 1)) h k) = h (k + 1)
    rw [K.one_mul, show conv K (fun i => delta K (i + 1)) h k = K.zero from conv_zero_left K _ h (fun _ => rfl) k,
      DetChain.add_zero']

theorem conv_sR_left (c h : Nat → R) : ∀ k, conv K (sR K c) h k = sR K (conv K c h) k
  | 0 => DetChain.zero_mul' K _
  | k + 1 => by
    show K.add (K.mul K.zero (h (k + 1))) (conv K (fun i => sR K c (i + 1)) h k) = conv K c h k
    rw [DetChain.zero_mul', K.zero_add]
    exact conv_ext_left K _ _ _ (fun i => rfl) k

theorem sR_ext (f g : Nat → R) (hfg : ∀ i, f i = g i) : ∀ k, sR K f k = sR K g k
  | 0 => rfl
  | k + 1 => hfg k

theorem mid4' (a b c d : R) : K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := by
  rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]

theorem neg_mul_eq (a x : R) : K.neg (K.mul a x) = K.mul (K.neg a) x := (DetChain.neg_mul_left K a x).symm

/-- **The Satake polynomial of a union is the product.** -/
theorem cf_append : ∀ (l₁ l₂ : List R) (k : Nat), cf K (l₁ ++ l₂) k = conv K (cf K l₁) (cf K l₂) k
  | [], l₂, k => by
    rw [conv_ext_left K _ _ _ (cf_nil K) k, conv_delta_left]
    rfl
  | a :: l₁, l₂, k => by
    show cf K (a :: (l₁ ++ l₂)) k = _
    rw [cf_cons, conv_ext_left K _ _ _ (cf_cons K a l₁) k, conv_add_left,
      conv_ext_left K (fun i => K.neg (K.mul a (sR K (cf K l₁) i))) (fun i => K.mul (K.neg a) (sR K (cf K l₁) i)) _
        (fun i => neg_mul_eq K _ _) k, conv_smul_left, conv_sR_left, cf_append l₁ l₂ k,
      sR_ext K _ _ (fun i => cf_append l₁ l₂ i) k, neg_mul_eq]

/-- **Scaling the Satake data by d multiplies the k-th coefficient by d^k.** -/
theorem cf_scale (d : R) : ∀ (l : List R) (k : Nat), cf K (l.map (K.mul d)) k = K.mul (pw K d k) (cf K l k)
  | [], k => by
    cases k with
    | zero => exact (K.one_mul K.one).symm
    | succ k => exact (DetChain.mul_zero K _).symm
  | a :: l, k => by
    cases k with
    | zero => show cf K (l.map (K.mul d)) 0 = K.mul K.one (cf K l 0); rw [cf_scale d l 0]; rfl
    | succ k =>
      show K.add (cf K (l.map (K.mul d)) (k + 1)) (K.neg (K.mul (K.mul d a) (cf K (l.map (K.mul d)) k)))
        = K.mul (K.mul (pw K d k) d) (K.add (cf K l (k + 1)) (K.neg (K.mul a (cf K l k))))
      rw [cf_scale d l (k + 1), cf_scale d l k, K.distrib]
      show K.add (K.mul (K.mul (pw K d k) d) (cf K l (k + 1))) (K.neg (K.mul (K.mul d a) (K.mul (pw K d k) (cf K l k))))
        = K.add (K.mul (K.mul (pw K d k) d) (cf K l (k + 1))) (K.mul (K.mul (pw K d k) d) (K.neg (K.mul a (cf K l k))))
      congr 1
      rw [K.mul_comm (K.mul (pw K d k) d) (K.neg _), DetChain.neg_mul_left]
      congr 1
      rw [K.mul_comm d a, mid4' K a d, mid4' K a (cf K l k), K.mul_comm d (cf K l k)]

theorem neg_zero'' : K.neg K.zero = K.zero := (DetChain.neg_unique K _ _ (K.zero_add K.zero)).symm

/-- Two parameters: `cf [a, b] = 1, −(a + b), ab, 0, …`. -/
def B2 (s p : R) : Nat → R
  | 0 => K.one
  | 1 => K.neg s
  | 2 => p
  | _ + 3 => K.zero

theorem cf2 (a b : R) : ∀ k, cf K [a, b] k = B2 K (K.add a b) (K.mul a b) k
  | 0 => rfl
  | 1 => by
    show K.add (K.add K.zero (K.neg (K.mul b K.one))) (K.neg (K.mul a K.one)) = K.neg (K.add a b)
    rw [K.zero_add, K.mul_comm b K.one, K.mul_comm a K.one, K.one_mul, K.one_mul,
      Sym2Local.neg_add_distrib K a b, K.add_comm]
  | 2 => by
    show K.add (K.add K.zero (K.neg (K.mul b K.zero))) (K.neg (K.mul a (K.add K.zero (K.neg (K.mul b K.one)))))
      = K.mul a b
    rw [DetChain.mul_zero, neg_zero'', K.zero_add, K.zero_add, K.mul_comm b K.one, K.one_mul, K.zero_add,
      Sym2Local.mul_neg' K a b, DetChain.neg_neg']
  | k + 3 => by
    show K.add (K.add K.zero (K.neg (K.mul b K.zero))) (K.neg (K.mul a (K.add K.zero (K.neg (K.mul b K.zero)))))
      = K.zero
    rw [DetChain.mul_zero, neg_zero'', K.zero_add, DetChain.mul_zero, neg_zero'', K.zero_add]

/-- Power sums from `(t, d)`: `s 0 = 2`, `s 1 = t`, `s (n+2) = t s(n+1) − d s(n)`. -/
def psum (t d : R) : Nat → R
  | 0 => K.add K.one K.one
  | 1 => t
  | n + 2 => K.add (K.mul t (psum t d (n + 1))) (K.neg (K.mul d (psum t d n)))

theorem cancel4 (P U V Q : R) : K.add (K.add (K.add P U) (K.add V Q)) (K.neg (K.add V U)) = K.add P Q := by
  rw [K.add_comm V Q, DetChain.add_mid4, K.add_comm U V, K.add_assoc, K.add_neg, DetChain.add_zero']

theorem psum_eq (α β : R) : ∀ n, psum K (K.add α β) (K.mul α β) n = K.add (pw K α n) (pw K β n)
  | 0 => rfl
  | 1 => by show K.add α β = K.add (K.mul K.one α) (K.mul K.one β); rw [K.one_mul, K.one_mul]
  | n + 2 => by
    show K.add (K.mul (K.add α β) (psum K (K.add α β) (K.mul α β) (n + 1)))
        (K.neg (K.mul (K.mul α β) (psum K (K.add α β) (K.mul α β) n)))
      = K.add (K.mul (K.mul (pw K α n) α) α) (K.mul (K.mul (pw K β n) β) β)
    rw [psum_eq α β (n + 1), psum_eq α β n]
    show K.add (K.mul (K.add α β) (K.add (K.mul (pw K α n) α) (K.mul (pw K β n) β)))
        (K.neg (K.mul (K.mul α β) (K.add (pw K α n) (pw K β n)))) = _
    rw [DetChain.right_distrib, K.distrib, K.distrib, K.distrib]
    have u : K.mul α (K.mul (pw K β n) β) = K.mul (K.mul α β) (pw K β n) := by
      rw [K.mul_comm (pw K β n) β, ← K.mul_assoc]
    have v : K.mul β (K.mul (pw K α n) α) = K.mul (K.mul α β) (pw K α n) := by
      rw [K.mul_comm (pw K α n) α, ← K.mul_assoc, K.mul_comm β α]
    rw [u, v, K.mul_comm α (K.mul (pw K α n) α), K.mul_comm β (K.mul (pw K β n) β)]
    exact cancel4 K _ _ _ _

def symL (α β : R) : Nat → List R
  | 0 => [K.one]
  | 1 => [α, β]
  | n + 2 => [pw K α (n + 2), pw K β (n + 2)] ++ (symL α β n).map (K.mul (K.mul α β))

/-- The Sym^n Satake polynomial as a function of `(t, d)` only. -/
def Q (t d : R) : Nat → Nat → R
  | 0 => cf K [K.one]
  | 1 => B2 K t d
  | n + 2 => conv K (B2 K (psum K t d (n + 2)) (pw K d (n + 2))) (fun j => K.mul (pw K d j) (Q t d n j))

/-- **The Sym^n Satake polynomial depends only on (t, d).** -/
theorem symn_poly (α β : R) : ∀ n k, cf K (symL K α β n) k = Q K (K.add α β) (K.mul α β) n k
  | 0, _ => rfl
  | 1, k => cf2 K α β k
  | n + 2, k => by
    show cf K ([pw K α (n + 2), pw K β (n + 2)] ++ (symL K α β n).map (K.mul (K.mul α β))) k
      = conv K (B2 K (psum K (K.add α β) (K.mul α β) (n + 2)) (pw K (K.mul α β) (n + 2)))
          (fun j => K.mul (pw K (K.mul α β) j) (Q K (K.add α β) (K.mul α β) n j)) k
    rw [cf_append]
    have hB : ∀ j, cf K [pw K α (n + 2), pw K β (n + 2)] j
        = B2 K (psum K (K.add α β) (K.mul α β) (n + 2)) (pw K (K.mul α β) (n + 2)) j := fun j => by
      rw [cf2, psum_eq, show pw K (K.mul α β) (n + 2) = K.mul (pw K α (n + 2)) (pw K β (n + 2)) from
        Frobenius.pw_mul K α β (n + 2)]
    rw [conv_ext_left K _ _ _ hB k]
    exact conv_ext K _ _ _ (fun j => by rw [cf_scale, symn_poly α β n j]) k

theorem cf_zero : ∀ l : List R, cf K l 0 = K.one
  | [] => rfl
  | _ :: l => cf_zero l

/-- **Local Sym^n functoriality.** The lifted GL_(n+1) eigen-system at `p` is the unique solution of
    `conv (Q_n(t, d)) h = δ`, hence a function of the GL2 data `(t, d)`. -/
theorem symn_lift (α β : R) (n : Nat) (g : Nat → R)
    (hg : ∀ k, conv K (Q K (K.add α β) (K.mul α β) n) g k = delta K k) :
    ∀ k, hk K (symL K α β n) k = g k := fun k =>
  conv_unique K (Q K (K.add α β) (K.mul α β) n) _ g
    ((symn_poly K α β n 0).symm.trans (cf_zero K _))
    (fun j => (conv_ext_left K _ _ _ (fun i => (symn_poly K α β n i).symm) j).trans
      (conv_cf_hk K (symL K α β n) j)) hg k k (Nat.le_refl k)

end SymNLocal

#print axioms SymNLocal.cf_nil
#print axioms SymNLocal.conv_delta_left
#print axioms SymNLocal.conv_sR_left
#print axioms SymNLocal.sR_ext
#print axioms SymNLocal.mid4'
#print axioms SymNLocal.neg_mul_eq
#print axioms SymNLocal.cf_append
#print axioms SymNLocal.cf_scale
#print axioms SymNLocal.neg_zero''
#print axioms SymNLocal.cf2
#print axioms SymNLocal.cancel4
#print axioms SymNLocal.psum_eq
#print axioms SymNLocal.symn_poly
#print axioms SymNLocal.cf_zero
#print axioms SymNLocal.symn_lift

Listing 154: lang/TwistLocal.lean

TwistLocal.lean

/-
  lang/TwistLocal — T43, cycle 9 (functoriality, local unramified): twisting GL_n by a character of GL_1.

  The L-homomorphism GL_n × GL_1 → GL_n, (g, c) ↦ c g, multiplies the Satake data by c = χ(p).
    conv_scale     conv (d^• c) (d^• h) k = d^k · conv c h k
    twist_lift     h_k(c α) = c^k · h_k(α): the twisted eigen-system at p is a_twist(p^k) = χ(p)^k a(p^k)
  (from SymNLocal.cf_scale and GLnLocal.conv_unique). Checked numerically before the proof.

  Lean 4 core only, proofs by hand, no axioms.
-/
import lang.SymNLocal

namespace TwistLocal

open GLnLocal SymNLocal

variable {R : Type} (K : DetChain.CRing R)

def scl (d : R) (c : Nat → R) (k : Nat) : R := K.mul (SymNLocal.pw K d k) (c k)

theorem conv_scale (d : R) : ∀ (c h : Nat → R) (k : Nat),
    conv K (scl K d c) (scl K d h) k = K.mul (SymNLocal.pw K d k) (conv K c h k)
  | c, h, 0 => by
    show K.mul (K.mul K.one (c 0)) (K.mul K.one (h 0)) = K.mul K.one (K.mul (c 0) (h 0))
    rw [K.one_mul, K.one_mul, K.one_mul]
  | c, h, k + 1 => by
    show K.add (K.mul (K.mul K.one (c 0)) (scl K d h (k + 1))) (conv K (fun i => scl K d c (i + 1)) (scl K d h) k)
      = K.mul (SymNLocal.pw K d (k + 1)) (K.add (K.mul (c 0) (h (k + 1))) (conv K (fun i => c (i + 1)) h k))
    -- scl d c (i+1) = d · scl d (c ∘ succ) i
    have e : ∀ i, scl K d c (i + 1) = K.mul d (scl K d (fun j => c (j + 1)) i) := fun i => by
      show K.mul (K.mul (SymNLocal.pw K d i) d) (c (i + 1)) = K.mul d (K.mul (SymNLocal.pw K d i) (c (i + 1)))
      rw [K.mul_comm (SymNLocal.pw K d i) d, K.mul_assoc]
    rw [conv_ext_left K _ _ _ e k, conv_smul_left, conv_scale d (fun j => c (j + 1)) h k, K.one_mul, K.distrib]
    show K.add (K.mul (c 0) (K.mul (SymNLocal.pw K d (k + 1)) (h (k + 1))))
        (K.mul d (K.mul (SymNLocal.pw K d k) (conv K (fun i => c (i + 1)) h k)))
      = K.add (K.mul (K.mul (SymNLocal.pw K d k) d) (K.mul (c 0) (h (k + 1))))
          (K.mul (K.mul (SymNLocal.pw K d k) d) (conv K (fun i => c (i + 1)) h k))
    rw [← K.mul_assoc d, K.mul_comm d (SymNLocal.pw K d k)]
    congr 1
    show K.mul (c 0) (K.mul (K.mul (SymNLocal.pw K d k) d) (h (k + 1))) = K.mul (K.mul (SymNLocal.pw K d k) d) (K.mul (c 0) (h (k + 1)))
    rw [← K.mul_assoc, K.mul_comm (c 0), K.mul_assoc]

/-- **Twisting.** `h_k(c α) = c^k h_k(α)`. -/
theorem twist_lift (c : R) (α : List R) (k : Nat) :
    hk K (α.map (K.mul c)) k = K.mul (SymNLocal.pw K c k) (hk K α k) := by
  have hc : ∀ j, conv K (cf K (α.map (K.mul c))) (scl K c (hk K α)) j = delta K j := fun j => by
    rw [conv_ext_left K _ (scl K c (cf K α)) _ (fun i => cf_scale K c α i) j, conv_scale, conv_cf_hk]
    cases j with
    | zero => exact K.one_mul K.one
    | succ j => exact DetChain.mul_zero K _
  exact conv_unique K (cf K (α.map (K.mul c))) _ _ (cf_zero K _) (conv_cf_hk K _) hc k k (Nat.le_refl k)

end TwistLocal

#print axioms TwistLocal.conv_scale
#print axioms TwistLocal.twist_lift

Listing 155: numeric_r1.py: Bianchi groups mod \(\ell \)

numeric_r1.py

#!/usr/bin/env python3
"""T35 R1 numerics: Bianchi groups (l0 = 1), cohomology mod small primes ell.

For Gamma_0(pi) in PSL_2(O_d), d = 1, 3, prime levels pi of growing norm, and ell = 5, 7, 11, 13:
  * dim Hom(Gamma_0, F_ell) and its cuspidal part (vanishing on the two cusp groups), against
    the rational rank b1: the excess is the ell-torsion of H_1, i.e. ell-torsion classes;
  * the Hecke operator T_lambda (lambda the smallest prime not dividing pi * ell) on the cuspidal
    part mod ell, its characteristic polynomial, and the split into generalised eigenspaces:
    the Eisenstein value is N(lambda) + 1; every other eigenvalue (or irreducible factor) is a
    non-Eisenstein maximal ideal m;
  * for each non-Eisenstein m: H^0_m = 0 (H^0 = F_ell is Eisenstein, T acts by N(lambda) + 1),
    H^3 = 0 (cohomological dimension 2), so the localised cohomology lies in degrees [1, 2] =
    [q0, q0 + l0]; dim H^1_m mod ell against the rational multiplicity tells whether m carries
    torsion (a lift that is not a rational eigenform).
Python standard library only, exact arithmetic mod ell. Writes work/NUMERIC_R1.txt.
"""
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import numeric_bianchi as nb  # noqa: E402

ELLS = (5, 7, 11, 13)


def matmul_mod(A, B, P):
    n = len(A)
    return [[sum(A[i][k] * B[k][j] for k in range(n)) % P for j in range(n)] for i in range(n)]


def nullity_mod(M, P):
    n = len(M)
    rows = [{j: M[i][j] % P for j in range(n) if M[i][j] % P} for i in range(n)]
    return len(nb.nullspace_mod(rows, n, P))


def gen_eigen_dim(T, a, P):
    n = len(T)
    A = [[(T[i][j] - (a if i == j else 0)) % P for j in range(n)] for i in range(n)]
    M = A
    for _ in range(n - 1):
        M = matmul_mod(M, A, P)
    return nullity_mod(M, P)


def main():
    out = []
    ok = True

    def check(name, cond):
        nonlocal ok
        ok = ok and cond
        out.append(f"  [{'PASS' if cond else 'FAIL'}] {name}")

    summary = []
    out.append("Bianchi groups, l0 = 1, range [q0, q0 + l0] = [1, 2]. Cohomology mod ell of Gamma_0(pi).")
    out.append(" d  N(pi)  ell  b1(Q)  dim Hom(F_ell)  cusp(F_ell)  T_lam  eigenvalues mod ell (dim)          non-Eis dims  Eis value")
    for d, maxnorm in ((1, 101), (3, 103)):
        R, gens, rels, _ = nb.presentation(d)
        for pi in nb.primes_of(R, maxnorm):
            n = R.norm(pi)
            Cq = nb.CohomologyMod(R, gens, rels, pi, 1000003)
            for ell in ELLS:
                if n % ell == 0:
                    continue
                C = nb.CohomologyMod(R, gens, rels, pi, ell)
                lam = next(l for l in nb.primes_of(R, 60)
                           if not (R.divides(l, pi) or R.divides(pi, l)) and R.norm(l) % ell != 0)
                nl = R.norm(lam)
                eis = (nl + 1) % ell
                if not C.cusp:
                    out.append(f" {d}  {n:<6} {ell:<4} {Cq.b1:<6} {C.b1:<15} 0            -")
                    continue
                T = [[x % ell for x in row] for row in C.hecke(lam)]
                m = len(T)
                parts = []
                tot = 0
                noneis = 0
                for a in range(ell):
                    g = gen_eigen_dim(T, a, ell)
                    if g:
                        parts.append(f"{a}({g})")
                        tot += g
                        if a != eis:
                            noneis += g
                irr = m - tot                  # eigenvalues outside F_ell: non-Eisenstein
                if irr:
                    parts.append(f"irr({irr})")
                    noneis += irr
                out.append(f" {d}  {n:<6} {ell:<4} {Cq.b1:<6} {C.b1:<15} {m:<12} N={nl:<4} {' '.join(parts):<34} {noneis:<13} {eis}")
                summary.append((d, n, ell, Cq.b1, m, noneis, " ".join(parts)))
                check(f"d={d}, N={n}, ell={ell}: cuspidal dimension mod ell >= rational b1_cusp", m >= len(Cq.cusp))
    ex = [r for r in summary if r[4] > r[3]]
    out.append("")
    out.append("Torsion classes (dim mod ell above the rational rank), with their maximal ideals:")
    for d_, n_, ell_, b1_, dm_, noneis_, parts_ in ex:
        out.append(f"  d={d_}, N={n_}, ell={ell_}: rational {b1_}, mod ell {dm_}; eigenvalues {parts_}; non-Eisenstein part {noneis_}")
    out.append(f"  {len(ex)} (level, ell) pairs with torsion; {sum(1 for r in ex if r[5] > r[3])} with torsion at a non-Eisenstein m")
    out.append("For every non-Eisenstein m in the table: H^0_m = 0 and H^3 = 0, so the localised cohomology")
    out.append("is concentrated in [q0, q0 + l0] = [1, 2]; the Koszul form with l0 = 1 is KoszulRangeChain.range1_iff.")
    out.append(f"ALL {'PASS' if ok else 'FAIL'}")
    text = "\n".join(out) + "\n"
    with open(os.path.join(HERE, "NUMERIC_R1.txt"), "w", encoding="utf-8") as fh:
        fh.write(text)
    print(text, end="")
    return 0 if ok else 1


if __name__ == "__main__":
    raise SystemExit(main())

Listing 156: numeric_r1_gl3.py: \(\Gamma _0(p)\subset \mathrm {SL}_3(\mathbb Z)\)

numeric_r1_gl3.py

#!/usr/bin/env python3
"""T35 R1 numerics for GL3/Q (q0 = 2, l0 = 1, range [2, 3]): degree 1 below the range.

SL3(Z) has the Steinberg presentation (Milnor, Introduction to Algebraic K-theory, Sec. 10;
K_2(Z) = Z/2 generated by w^4): generators x_ij (i != j), relations
   [x_ij, x_jk] = x_ik  (i, j, k distinct),  [x_ij, x_kl] = 1  (j != k, i != l),
   (x_12 x_21^-1 x_12)^4 = 1.
Every relator is checked on the matrices. Gamma_0(p) = { g : bottom row = (0, 0, *) mod p } has the
points of P^2(F_p) as cosets (index p^2 + p + 1). Reidemeister-Schreier gives the relation matrix of
Gamma_0(p)^ab; its nullspace mod ell is H^1(Gamma_0(p), F_ell) = Hom(Gamma_0(p), F_ell).
Degree 0 is Eisenstein (F_ell, T acts by the degree), degrees > 3 vanish (vcd SL3(Z) = 3, ell >= 5
prime to the torsion), so H^1 = 0 means: outside [2, 3] the cohomology vanishes for every m.
Python standard library only. Writes work/NUMERIC_R1_GL3.txt.
"""
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import numeric_bianchi as nb  # noqa: E402  (nullspace_mod, smith_diagonal)


def E(i, j, s=1):
    M = [[1 if a == b else 0 for b in range(3)] for a in range(3)]
    M[i][j] = s
    return M


def mm(A, B):
    return [[sum(A[i][k] * B[k][j] for k in range(3)) for j in range(3)] for i in range(3)]


GENS = [(i, j) for i in range(3) for j in range(3) if i != j]
NAME = {g: k for k, g in enumerate(GENS)}


def relators():
    R = []
    for i in range(3):
        for j in range(3):
            for k in range(3):
                if len({i, j, k}) == 3:
                    # x_ij x_jk x_ij^-1 x_jk^-1 x_ik^-1
                    R.append([(NAME[(i, j)], 1), (NAME[(j, k)], 1), (NAME[(i, j)], -1), (NAME[(j, k)], -1), (NAME[(i, k)], -1)])
    for a in GENS:
        for b in GENS:
            if a < b and a[1] != b[0] and a[0] != b[1]:
                R.append([(NAME[a], 1), (NAME[b], 1), (NAME[a], -1), (NAME[b], -1)])
    w = [(NAME[(0, 1)], 1), (NAME[(1, 0)], -1), (NAME[(0, 1)], 1)]
    R.append(w * 4)
    return R


def word_matrix(w):
    M = [[1 if a == b else 0 for b in range(3)] for a in range(3)]
    for g, e in w:
        i, j = GENS[g]
        M = mm(M, E(i, j, e))
    return M


def points(p):
    pts = []
    for a in range(p):
        for b in range(p):
            pts.append((a, b, 1))
    for a in range(p):
        pts.append((a, 1, 0))
    pts.append((1, 0, 0))
    return pts


def normalise(v, p):
    v = [x % p for x in v]
    for k in (2, 1, 0):
        if v[k]:
            inv = pow(v[k], p - 2, p)
            return tuple(x * inv % p for x in v)
    raise ValueError


def act(v, g, e, p):
    """row vector v times x_g^e."""
    i, j = GENS[g]
    w = list(v)
    w[j] = (w[j] + e * v[i]) % p
    return normalise(w, p)


def h1_dims(p, ells):
    pts = points(p)
    idx = {v: k for k, v in enumerate(pts)}
    base = (0, 0, 1)
    n = len(pts)
    # Schreier tree by BFS from the base point
    parent = {base: None}
    order = [base]
    q = [base]
    tree = set()
    while q:
        new = []
        for v in q:
            for g in range(len(GENS)):
                for e in (1, -1):
                    u = act(v, g, e, p)
                    if u not in parent:
                        parent[u] = (v, g, e)
                        tree.add((v, g) if e == 1 else (u, g))
                        new.append(u)
        q = new
    assert len(parent) == n
    cols = {}
    for v in pts:
        for g in range(len(GENS)):
            if (v, g) not in tree:
                cols[(v, g)] = len(cols)
    rows = []
    for r in relators():
        for v in pts:
            row = {}
            c = v
            for g, e in r:
                if e == 1:
                    key = (c, g)
                    if key in cols:
                        row[cols[key]] = row.get(cols[key], 0) + 1
                    c = act(c, g, 1, p)
                else:
                    c2 = act(c, g, -1, p)
                    key = (c2, g)
                    if key in cols:
                        row[cols[key]] = row.get(cols[key], 0) - 1
                    c = c2
            assert c == v
            row = {k: x for k, x in row.items() if x}
            if row:
                rows.append(row)
    # coset representatives as matrices, from the Schreier tree
    rep = {base: [[1 if a == b else 0 for b in range(3)] for a in range(3)]}
    for v in order_bfs(parent, base):
        if v == base:
            continue
        u, g, e = parent[v]
        i, j = GENS[g]
        rep[v] = mm(rep[u], E(i, j, e))
    res = {}
    torus = {}
    for ell in ells:
        red = [{k: x % ell for k, x in r.items() if x % ell} for r in rows]
        res[ell] = len(nb.nullspace_mod(red, len(cols), ell))
        if (p - 1) % ell == 0:
            # chi(g) = dlog(g_33 mod p) mod ell on every Schreier generator
            prim = next(a for a in range(2, p) if all(pow(a, (p - 1) // q, p) != 1 for q in range(2, p) if (p - 1) % q == 0 and all(q % d for d in range(2, q))))
            dlog = {pow(prim, k, p): k for k in range(p - 1)}
            vec = [0] * len(cols)
            for (v, g), c in cols.items():
                i, j = GENS[g]
                M = mm(mm(rep[v], E(i, j)), inv3(rep[act(v, g, 1, p)]))
                assert M[2][0] % p == 0 and M[2][1] % p == 0
                vec[c] = dlog[M[2][2] % p] % ell
            hom = all(sum(x * vec[k] for k, x in r.items()) % ell == 0 for r in red)
            torus[ell] = (hom and any(vec))
    res[0] = len(nb.nullspace_mod([{k: x % 1000003 for k, x in r.items()} for r in rows], len(cols), 1000003))
    return n, len(cols), res, torus


def order_bfs(parent, base):
    """vertices in an order where each parent precedes its children"""
    seen, out = {base}, [base]
    k = 0
    children = {}
    for v, pr in parent.items():
        if pr is not None:
            children.setdefault(pr[0], []).append(v)
    while k < len(out):
        for c in children.get(out[k], []):
            if c not in seen:
                seen.add(c)
                out.append(c)
        k += 1
    return out


def inv3(M):
    """inverse of an integer matrix of determinant 1 (adjugate)."""
    a = M
    C = [[(a[(j + 1) % 3][(i + 1) % 3] * a[(j + 2) % 3][(i + 2) % 3] - a[(j + 1) % 3][(i + 2) % 3] * a[(j + 2) % 3][(i + 1) % 3]) for j in range(3)] for i in range(3)]
    return C


def main():
    out = []
    ok = True

    def check(name, cond):
        nonlocal ok
        ok = ok and cond
        out.append(f"  [{'PASS' if cond else 'FAIL'}] {name}")

    I3 = [[1 if a == b else 0 for b in range(3)] for a in range(3)]
    check("every Steinberg relator is the identity on the matrices", all(word_matrix(r) == I3 for r in relators()))
    ells = (5, 7, 11, 13)
    out.append("GL3/Q, Gamma_0(p) in SL3(Z): H^1 (degree 1 < q0 = 2).")
    out.append("  p    index  Schreier gens   dim H^1(Q)   " + "  ".join(f"dim H^1(F_{l})" for l in ells))
    allzero = True
    for p in (2, 3, 5, 7, 11, 13, 17):
        n, ng, res, torus = h1_dims(p, ells)
        out.append(f"  {p:<4} {n:<6} {ng:<15} {res[0]:<12} " + "  ".join(f"{res[l]:<13}" for l in ells))
        for l, t in torus.items():
            out.append(f"       ell = {l} divides p - 1: the torus character g -> g_33 mod p -> Z/{l} is a class: {t}")
        explained = all(res[l] == (1 if (p - 1) % l == 0 and torus.get(l) else 0) for l in ells if p % l)
        allzero = allzero and res[0] == 0 and explained
    check("H^1(Gamma_0(p), Q) = 0; H^1(Gamma_0(p), F_ell) is 0, or spanned by the Eisenstein torus character when ell | p - 1", allzero)
    out.append("So for these levels and ell, at every non-Eisenstein m the cohomology outside [q0, q0 + l0] = [2, 3]")
    out.append("vanishes: degree 0 is Eisenstein, degree 1 is zero or the Eisenstein torus class, degrees > 3 are zero.")
    out.append(f"ALL {'PASS' if ok else 'FAIL'}")
    text = "\n".join(out) + "\n"
    with open(os.path.join(HERE, "NUMERIC_R1_GL3.txt"), "w", encoding="utf-8") as fh:
        fh.write(text)
    print(text, end="")
    return 0 if ok else 1


if __name__ == "__main__":
    raise SystemExit(main())

Listing 157: numeric_r2.py: monomial test for \(2I\) and \(A_5\)

numeric_r2.py

#!/usr/bin/env python3
"""T35 R2 numerics: descent through a non-solvable extension, the obstruction in finite form.

Automorphy is known after restriction to solvable (in particular elementary / nilpotent)
subextensions; descent combines the pieces with Brauer's theorem
    chi = sum_i n_i Ind_{H_i}^G psi_i,   n_i in Z,  H_i elementary, psi_i linear.
If all n_i >= 0 were possible for an irreducible chi, then chi = Ind psi for one H, psi (chi is
monomial), and the automorphic side would follow by automorphic induction from H alone.
So the smallest missing lemma is: descent for the NON-monomial irreducibles. This script finds them
for G = A5 (projective icosahedral) and G = 2I = SL2(F5) (linear icosahedral, 2-dimensional), and
writes an explicit integral Brauer combination with negative coefficients for each.

Python standard library only, exact arithmetic (characters in Q(sqrt5) as pairs). Writes
work/NUMERIC_R2.txt.
"""
import itertools
import os
import sys
from fractions import Fraction

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)


# ------------------------------------------------------------ groups
def a5():
    els = [p for p in itertools.permutations(range(5)) if sign(p) == 1]
    mul = lambda p, q: tuple(p[q[i]] for i in range(5))
    return els, mul, tuple(range(5))


def sign(p):
    s, seen = 1, set()
    for i in range(len(p)):
        if i in seen:
            continue
        j, L = i, 0
        while j not in seen:
            seen.add(j)
            j = p[j]
            L += 1
        s *= (-1) ** (L - 1)
    return s


def two_i():
    import numeric_ym2i as ym
    G = ym.Group()
    els = list(range(G.n))
    mul = lambda a, b: G.mul[a][b]
    return els, mul, G.one


class Grp:
    def __init__(self, els, mul, one):
        els = [one] + [g for g in els if g != one]      # class 0 is the identity class
        self.els, self.mul, self.one = els, mul, one
        self.idx = {g: i for i, g in enumerate(els)}
        self.inv = {g: next(h for h in els if mul(g, h) == one) for g in els}
        self.classes = []
        seen = set()
        for g in els:
            if g in seen:
                continue
            c = {mul(mul(h, g), self.inv[h]) for h in els}
            seen |= c
            self.classes.append(sorted(c, key=lambda x: self.idx[x]))
        self.cls_of = {g: k for k, c in enumerate(self.classes) for g in c}

    def order(self, g):
        k, h = 1, g
        while h != self.one:
            h = self.mul(h, g)
            k += 1
        return k

    def generated(self, gens):
        S = {self.one}
        frontier = [self.one]
        while frontier:
            new = []
            for x in frontier:
                for g in gens:
                    y = self.mul(x, g)
                    if y not in S:
                        S.add(y)
                        new.append(y)
            frontier = new
        return frozenset(S)

    def subgroups(self):
        subs = set()
        for g in self.els:
            subs.add(self.generated([g]))
        for g, h in itertools.combinations(self.els, 2):
            subs.add(self.generated([g, h]))
        return subs


# ------------------------------------------------------------ characters as complex numbers (exact enough via rounding)
import cmath  # noqa: E402


def class_fn_from(G, f):
    return [f(c[0]) for c in G.classes]


def inner(G, a, b):
    """<a, b> over class representatives (exact up to float round-off)."""
    s = 0
    for k, c in enumerate(G.classes):
        s += len(c) * a[k] * b[k].conjugate()
    return s / len(G.els)


def linear_chars_cyclic(G, H):
    """Linear characters of a cyclic H = <g>."""
    g = max(H, key=lambda x: G.order(x))
    n = G.order(g)
    powers = {}
    h = G.one
    for k in range(n):
        powers[h] = k
        h = G.mul(h, g)
    return [(lambda x, j=j: cmath.exp(2j * cmath.pi * j * powers[x] / n)) for j in range(n)]


def induced(G, H, psi):
    Hs = set(H)
    vals = []
    for c in G.classes:
        x = c[0]
        s = 0
        for g in G.els:
            y = G.mul(G.mul(G.inv[g], x), g)
            if y in Hs:
                s += psi(y)
        vals.append(s / len(H))
    return vals


def irreducibles(G, pool):
    """Irreducible characters from a spanning pool: project away the known irreducibles; a projected
    character, or a difference of two, with norm 1 is +- an irreducible."""
    irr = []
    for _ in range(20):
        proj = []
        for ch in pool:
            v = list(ch)
            for e in irr:
                m = inner(G, v, e)
                v = [x - m * y for x, y in zip(v, e)]
            if inner(G, v, v).real > 0.5:
                proj.append(v)
        cands = proj + [[x - y for x, y in zip(u, w)] for u, w in itertools.combinations(proj, 2)]
        found = False
        for v in cands:
            if abs(inner(G, v, v).real - 1) < 1e-8:
                if v[0].real < 0:
                    v = [-x for x in v]
                if v[0].real > 0.5 and all(abs(inner(G, v, e)) < 1e-8 for e in irr):
                    irr.append(v)
                    found = True
                    break
        if len(irr) == len(G.classes) or not found:
            break
        pool = pool + [[x * y for x, y in zip(irr[-1], e)] for e in irr]
    return irr


PHI = (1 + 5 ** 0.5) / 2


def galois(t):
    """phi -> 1 - phi on the trace values 2 Re q of 2I."""
    for a, b in ((PHI, 1 - PHI), (-PHI, PHI - 1), (PHI - 1, -PHI), (1 - PHI, PHI)):
        if abs(t - a) < 1e-9:
            return b
    return t


def analyse(name, G, out, seeds=(), keep=None):
    subs = G.subgroups()
    proper = [H for H in subs if 1 < len(H) < len(G.els)]
    cyc = [H for H in subs if len(H) > 1 and any(G.order(x) == len(H) for x in H)]
    out.append(f"{name}: order {len(G.els)}, {len(G.classes)} classes, {len(subs)} subgroups, "
               f"orders of proper subgroups {sorted({len(H) for H in proper})}")
    pool = []
    for H in cyc:
        for psi in linear_chars_cyclic(G, H):
            pool.append(induced(G, H, psi))
    pool = [[1 + 0j] * len(G.classes)] + list(seeds) + pool
    irr = irreducibles(G, pool)
    if keep is not None:
        irr = [v for v in irr if keep(v)]
    irr.sort(key=lambda v: v[0].real)
    out.append(f"  irreducible degrees: {[round(v[0].real) for v in irr]}")
    # monomial test: chi = Ind_H psi needs [G:H] = chi(1) and psi linear on H; test all H of that index
    # (linear characters of H factor through H^ab; enumerate via cyclic generation of H/[H,H] is heavy,
    #  so test Ind of every linear character of every cyclic or abelian H of the right index, and for
    #  non-abelian H of the right index use the criterion <Res chi, psi> for psi = trivial-on-[H,H])
    results = []
    for chi in irr:
        d = round(chi[0].real)
        idx_ok = [H for H in subs if len(H) * d == len(G.els) // (1 if keep is None else 2)
                  and (keep is None or G.minus in H)]
        if keep is not None:
            idx_ok = [H for H in subs if G.minus in H and len(H) * d == len(G.els)]
        mono = False
        for H in idx_ok:
            # linear characters of H: characters of H/[H,H]; find via induced test with all class functions
            # constant on cosets of the commutator subgroup -> enumerate homomorphisms H -> C* through cyclic pieces
            comm = G.generated([G.mul(G.mul(a, b), G.mul(G.inv[a], G.inv[b])) for a in H for b in H])
            cos = {}
            for h in H:
                key = frozenset(G.mul(h, c) for c in comm)
                cos.setdefault(key, []).append(h)
            q = len(cos)
            # H/[H,H] is abelian of order q; characters: try exponent-q roots on generators by brute force
            reps = [v[0] for v in cos.values()]
            keyof = {h: frozenset(G.mul(h, c) for c in comm) for h in H}
            # brute force: assign q-th roots of unity to each coset rep consistently (q is small here)
            roots = [cmath.exp(2j * cmath.pi * k / q) for k in range(q)]
            cand = []
            if q <= 12:
                gens = []
                span = {keyof[G.one]}
                for r in reps:
                    if keyof[r] not in span:
                        gens.append(r)
                        span = {keyof[x] for x in G.generated(gens) & H} if True else span
                        span = {keyof[x] for x in G.generated(gens)}
                for vals in itertools.product(roots, repeat=len(gens)):
                    # define on the generated group by words: BFS
                    f = {G.one: 1 + 0j}
                    frontier = [G.one]
                    ok = True
                    while frontier and ok:
                        new = []
                        for x in frontier:
                            for g, v in zip(gens, vals):
                                y = G.mul(x, g)
                                val = f[x] * v
                                if y in f:
                                    if abs(f[y] - val) > 1e-8:
                                        ok = False
                                        break
                                else:
                                    f[y] = val
                                    new.append(y)
                            if not ok:
                                break
                        frontier = new
                    if ok:
                        # extend to H through cosets of comm
                        psi = {}
                        for h in H:
                            rep = next((x for x in f if keyof.get(x) == keyof[h]), None)
                            if rep is None:
                                ok = False
                                break
                            psi[h] = f[rep]
                        if ok:
                            cand.append(psi)
            for psi in cand:
                ind = induced(G, H, lambda x, psi=psi: psi[x])
                if all(abs(a - b) < 1e-8 for a, b in zip(ind, chi)):
                    mono = True
                    break
            if mono:
                break
        results.append((d, mono, len(idx_ok)))
        verdict = "yes" if mono else ("no (no subgroup of that index)" if not idx_ok else "not found by the search")
        out.append(f"  degree {d}: subgroups of index {d}: {len(idx_ok)}; monomial: {verdict}")
    return irr, results


def main():
    out = []
    ok = True

    def check(name, cond):
        nonlocal ok
        ok = ok and cond
        out.append(f"  [{'PASS' if cond else 'FAIL'}] {name}")

    els, mul, one = two_i()
    B = Grp(els, mul, one)
    import numeric_ym2i as ym
    Y = ym.Group()
    B.minus = next(g for g in B.els if g != B.one and Y.re[g] < -0.999)
    chi2 = [2 * Y.re[c[0]] + 0j for c in B.classes]
    chi2c = [galois(2 * Y.re[c[0]]) + 0j for c in B.classes]
    irrB, resB = analyse("2I = SL2(F5) (linear icosahedral image)", B, out, seeds=[chi2, chi2c])
    check("2I has irreducible degrees 1, 2, 2, 3, 3, 4, 4, 5, 6", sorted(d for d, _, _ in resB) == [1, 2, 2, 3, 3, 4, 4, 5, 6])
    nonmonoB = sorted(d for d, m, n in resB if not m and n == 0 and d > 1)
    out.append(f"  degrees with no subgroup of that index (hence not monomial): {nonmonoB}")
    check("the two 2-dimensional (icosahedral Galois) representations of 2I are not monomial",
          nonmonoB.count(2) == 2)
    mi = B.cls_of[B.minus]
    oi = B.cls_of[B.one]
    a5 = [(d, m) for (d, m, _), v in zip(resB, irrB) if abs(v[mi] - v[oi]) < 1e-8]
    out.append(f"A5 = 2I/(+-1): irreducibles of 2I trivial on -1 have degrees {sorted(d for d, _ in a5)}")
    check("A5 has irreducible degrees 1, 3, 3, 4, 5", sorted(d for d, _ in a5) == [1, 3, 3, 4, 5])
    subA = sorted({len(H) // 2 for H in B.subgroups() if B.minus in H})
    out.append(f"  orders of subgroups of A5 (images of subgroups of 2I containing -1): {subA}")
    check("A5 has no subgroup of index 3 or 4, so its degree 3, 3, 4 irreducibles are not monomial",
          20 not in subA and 15 not in subA)
    out.append("Consequence: for an icosahedral rho (image 2I in GL2), any Brauer expression of rho through")
    out.append("solvable subgroups has negative coefficients; descent must cancel virtual automorphic")
    out.append("representations. This is the finite form of R2-gap.")
    out.append(f"ALL {'PASS' if ok else 'FAIL'}")
    text = "\n".join(out) + "\n"
    with open(os.path.join(HERE, "NUMERIC_R2.txt"), "w", encoding="utf-8") as fh:
        fh.write(text)
    print(text, end="")
    return 0 if ok else 1


if __name__ == "__main__":
    raise SystemExit(main())

Listing 158: numeric_r3.py: elliptic part of the trace formula

numeric_r3.py

#!/usr/bin/env python3
"""T35 R3 numerics: the elliptic part of the GL2 trace formula, exactly, and its size for GL3.

GL2 (level 1, Eichler-Selberg):
  tr T_n | S_k = -1/2 sum_{t^2 <= 4n} P_k(t, n) H(4n - t^2) - 1/2 sum_{dd' = n} min(d, d')^(k-1)
                 (+ sigma_1(n) if k = 2),
  P_k(t, n) = (rho^(k-1) - rhobar^(k-1)) / (rho - rhobar), rho + rhobar = t, rho rhobar = n,
  H = Hurwitz class number (forms weighted 1/2, 1/3 at i, rho; H(0) = -1/12).
  1. H(N) by reduced forms; check H(3) = 1/3, H(4) = 1/2, H(23) = 3.
  2. The step Altug's Poisson summation reproduces at k = 2 (no cusp forms): the elliptic sum
     cancels the trivial representation exactly,
        sum_{t^2 <= 4n} H(4n - t^2) = 2 sigma_1(n) - sum_{dd'=n} min(d, d')   (Kronecker-Hurwitz).
  3. Eichler-Selberg at k = 12 gives tau(n) (Ramanujan's Delta), n <= 60.
  4. At k = 12 the weight-k test function sees only the discrete series: the elliptic sum equals
     -tau(n) up to the hyperbolic term (ratio 1, nothing to cancel). The trivial representation
     enters only through a test function that is not a pseudo-coefficient; at k = 2 its size is
     2 sigma_1(n) ~ 2n and step 2 cancels it exactly.
GL3 (the corresponding sum): elliptic terms are indexed by x^3 - a x^2 + b x - n with complex roots,
  weighted by orbital integrals ~ class numbers of the cubic orders Z[x]/(f). Measured here:
  the number of such (a, b) with |roots| = n^(1/3) (tempered box), which is the number of terms the
  GL3 Poisson summation has to cancel, against n.
Python standard library only, exact fractions. Writes work/NUMERIC_R3.txt.
"""
import math
import os
from fractions import Fraction

HERE = os.path.dirname(os.path.abspath(__file__))


def hurwitz(N):
    """H(N): classes of positive definite forms of discriminant -N, weighted; H(0) = -1/12."""
    if N == 0:
        return Fraction(-1, 12)
    if N % 4 in (1, 2):
        return Fraction(0)
    h = Fraction(0)
    a = 1
    while 3 * a * a <= N:
        for b in range(-a + 1, a + 1):
            if (b * b + N) % (4 * a):
                continue
            c = (b * b + N) // (4 * a)
            if c < a:
                continue
            if b < 0 and a == c:
                continue
            w = Fraction(1)
            if a == b == c:
                w = Fraction(1, 3)
            elif a == c and b == 0:
                w = Fraction(1, 2)
            h += w
        a += 1
    return h


def sigma1(n):
    return sum(d for d in range(1, n + 1) if n % d == 0)


def min_sum(n, k):
    return sum(min(d, n // d) ** (k - 1) for d in range(1, n + 1) if n % d == 0)


def P(k, t, n):
    # rho^(k-1)-rhobar^(k-1) over rho-rhobar: recurrence u_j = t u_{j-1} - n u_{j-2}, u_0 = 0, u_1 = 1
    u0, u1 = 0, 1
    for _ in range(k - 2):
        u0, u1 = u1, t * u1 - n * u0
    return u1


def eichler_selberg(k, n):
    ell = Fraction(0)
    t = 0
    while t * t <= 4 * n:
        term = P(k, t, n) * hurwitz(4 * n - t * t)
        ell += term if t == 0 else 2 * term
        t += 1
    tr = -ell / 2 - Fraction(min_sum(n, k), 2)
    if k == 2:
        tr += sigma1(n)
    return tr, ell


def tau_list(N):
    """Coefficients of Delta = q prod (1 - q^m)^24 up to q^N."""
    c = [0] * (N + 1)
    c[0] = 1
    for m in range(1, N + 1):
        for _ in range(24):
            for i in range(N, m - 1, -1):
                c[i] -= c[i - m]
    return [0] + c[:N]


def main():
    out = []
    ok = True

    def check(name, cond):
        nonlocal ok
        ok = ok and cond
        out.append(f"  [{'PASS' if cond else 'FAIL'}] {name}")

    out.append("1. Hurwitz class numbers.")
    check("H(3) = 1/3, H(4) = 1/2, H(23) = 3, H(15) = 2", (hurwitz(3), hurwitz(4), hurwitz(23), hurwitz(15)) ==
          (Fraction(1, 3), Fraction(1, 2), Fraction(3), Fraction(2)))
    out.append("2. k = 2: the elliptic sum cancels the trivial representation (Kronecker-Hurwitz).")
    good = True
    for n in range(1, 201):
        s = sum((1 if t == 0 else 2) * hurwitz(4 * n - t * t) for t in range(0, int(math.isqrt(4 * n)) + 1))
        good = good and s == 2 * sigma1(n) - min_sum(n, 2)
    check("sum_t H(4n - t^2) = 2 sigma_1(n) - sum min(d, d') for n = 1..200 (exact)", good)
    out.append("3. k = 12: Eichler-Selberg gives tau(n).")
    tau = tau_list(60)
    good = all(eichler_selberg(12, n)[0] == tau[n] for n in range(1, 61))
    check("tr T_n | S_12 = tau(n) for n = 1..60 (exact)", good)
    out.append("4. k = 12: elliptic sum E(n) against tau(n) (discrete-series test function: no trivial term).")
    out.append("   n      E(n)/2                      tau(n)          |E/2| / |tau|   |E/2| / n^(11/2)")
    for n in (2, 3, 5, 7, 11, 13, 23, 37, 53):
        tr, ell = eichler_selberg(12, n)
        r = abs(float(ell) / 2) / abs(tau[n]) if tau[n] else float("inf")
        out.append(f"   {n:<6} {float(ell) / 2:>24.6g}   {tau[n]:>14}   {r:12.4g}   {abs(float(ell) / 2) / n ** 5.5:10.4g}")
    out.append("")
    out.append("GL3: elliptic characteristic polynomials x^3 - a x^2 + b x - n with one real and two complex")
    out.append("roots, all of absolute value n^(1/3) up to a factor 2 (tempered box): number of terms.")
    out.append("   n      terms     terms / n^(2/3)   terms / n")
    for n in (2, 3, 5, 7, 11, 13, 23, 37, 53, 101, 211):
        cnt = 0
        R = n ** (1 / 3)
        A = int(6 * R) + 1
        B = int(12 * R * R) + 1
        for a in range(-A, A + 1):
            for b in range(-B, B + 1):
                # discriminant of x^3 - a x^2 + b x - n < 0  <=> one real, two complex roots
                disc = a * a * b * b - 4 * b ** 3 - 4 * a ** 3 * n + 18 * a * b * n - 27 * n * n
                if disc >= 0:
                    continue
                # roots bounded: |a| <= 3 * 2R, |b| <= 3 * (2R)^2 (box), already enforced by ranges
                if abs(a) <= 6 * R and abs(b) <= 12 * R * R:
                    cnt += 1
        out.append(f"   {n:<6} {cnt:<9} {cnt / n ** (2 / 3):<16.4g} {cnt / n:.4g}")
    out.append("   The number of GL3 elliptic terms grows like n^(1/3) * n^(2/3) = n (box area), each weighted by")
    out.append("   a class number of a cubic order; the GL2 analogue grows like n^(1/2) terms. The GL3 Poisson")
    out.append("   cancellation of the trivial representation (size n^(dim/2)) is the missing step (R3-gap).")
    out.append(f"ALL {'PASS' if ok else 'FAIL'}")
    text = "\n".join(out) + "\n"
    with open(os.path.join(HERE, "NUMERIC_R3.txt"), "w", encoding="utf-8") as fh:
        fh.write(text)
    print(text, end="")
    return 0 if ok else 1


if __name__ == "__main__":
    raise SystemExit(main())

Listing 159: numeric_r4.py: functional equations of \(\mathrm {Sym}^n\Delta \)

numeric_r4.py

#!/usr/bin/env python3
"""T35 R4 numerics: functional equations of L(s, Sym^n Delta), n = 1..5, measured.

Delta = q prod (1 - q^m)^24, level 1, weight 12, alpha_p + beta_p = tau(p)/p^(11/2), alpha_p beta_p = 1.
L(s, Sym^n Delta) = prod_p prod_{j=0..n} (1 - alpha_p^(n-j) beta_p^j p^(-s))^(-1)   (analytic normalisation)
Gamma factor (conductor 1): prod over the Hodge pairs  Gamma_C(s + (n - 2j) 11/2), j < n/2, and
Gamma_R(s + e) for the middle term when n is even (e = 0 if n/2 even, 1 if odd).
Test (Dokchitser): theta(t) = sum_n a_n phi(n t), phi = inverse Mellin transform of the gamma factor;
the functional equation Lambda(s) = eps Lambda(1 - s) is equivalent to theta(1/t) = eps t theta(t).
The script measures eps(t) = theta(1/t) / (t theta(t)) at several t: it is constant (= +-1) exactly when
the functional equation holds. Newton-Thorne prove that every Sym^n Delta is automorphic, so these
are checks of theorems; the same test is what the converse theorem needs for twists.
Python standard library only (complex gamma by Lanczos). Writes work/NUMERIC_R4.txt.
"""
import cmath
import math
import os

HERE = os.path.dirname(os.path.abspath(__file__))

LG = [0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313,
      -176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6,
      1.5056327351493116e-7]


def lgamma_c(z):
    if z.real < 0.5:
        return cmath.log(math.pi / cmath.sin(math.pi * z)) - lgamma_c(1 - z)
    z -= 1
    x = LG[0]
    for i in range(1, 9):
        x += LG[i] / (z + i)
    t = z + 7.5
    return 0.5 * math.log(2 * math.pi) + (z + 0.5) * cmath.log(t) - t + cmath.log(x)


def log_gamma_factor(s, shifts_C, shifts_R):
    v = 0
    for a in shifts_C:   # Gamma_C(s) = 2 (2 pi)^(-s) Gamma(s)
        z = s + a
        v += math.log(2) - z * math.log(2 * math.pi) + lgamma_c(z)
    for a in shifts_R:   # Gamma_R(s) = pi^(-s/2) Gamma(s/2)
        z = s + a
        v += -z / 2 * math.log(math.pi) + lgamma_c(z / 2)
    return v


_GRID = {}


def phi(x, shifts_C, shifts_R, c=2.0, T=80.0, M=4000):
    """(1/2 pi i) int_{c - iT}^{c + iT} gamma(s) x^(-s) ds by the trapezoid rule (gamma on the line cached)."""
    key = (tuple(shifts_C), tuple(shifts_R), c, T, M)
    if key not in _GRID:
        h = 2 * T / M
        pts = []
        for k in range(M + 1):
            s = complex(c, -T + k * h)
            w = 0.5 if k in (0, M) else 1.0
            pts.append((s, w * cmath.exp(log_gamma_factor(s, shifts_C, shifts_R))))
        _GRID[key] = (h, pts)
    h, pts = _GRID[key]
    lx = math.log(x)
    tot = 0.0
    for s, g in pts:
        tot += (g * cmath.exp(-s * lx)).real
    return tot * h / (2 * math.pi)


def tau_list(N):
    # prod (1 - q^n)^3 = sum_k (-1)^k (2k+1) q^(k(k+1)/2)
    e3 = [0] * (N + 1)
    k = 0
    while k * (k + 1) // 2 <= N:
        e3[k * (k + 1) // 2] += (-1) ** k * (2 * k + 1)
        k += 1

    def mul(a, b):
        r = [0] * (N + 1)
        for i, x in enumerate(a):
            if x:
                for j in range(0, N + 1 - i):
                    if b[j]:
                        r[i + j] += x * b[j]
        return r
    e6 = mul(e3, e3)
    e12 = mul(e6, e6)
    e24 = mul(e12, e12)
    return [0] + e24[:N]          # tau(n) = coefficient of q^n in q * prod^24


def primes(n):
    s = bytearray([1]) * (n + 1)
    s[0] = s[1] = 0
    for i in range(2, int(n ** 0.5) + 1):
        if s[i]:
            s[i * i::i] = bytearray(len(s[i * i::i]))
    return [i for i in range(n + 1) if s[i]]


def sym_coeffs(n, N, tau):
    """Dirichlet coefficients a(m), m <= N, of L(s, Sym^n Delta) (analytic normalisation)."""
    a = [0.0] * (N + 1)
    a[1] = 1.0
    for p in primes(N):
        t = tau[p] / p ** 5.5
        # alpha = e^{i th}, beta = e^{-i th}; roots alpha^(n-j) beta^j = e^{i (n-2j) th}
        th = math.acos(max(-1.0, min(1.0, t / 2)))
        roots = [cmath.exp(1j * (n - 2 * j) * th) for j in range(n + 1)]
        # local coefficients of prod (1 - r X)^(-1): complete homogeneous symmetric polynomials
        maxe = int(math.log(N) / math.log(p))
        h = [1.0 + 0j] + [0j] * maxe
        for r in roots:
            for e in range(1, maxe + 1):
                h[e] += r * h[e - 1]
        pk = [p ** e for e in range(maxe + 1)]
        new = a[:]
        for m in range(1, N + 1):
            if a[m] and m % p:
                for e in range(1, maxe + 1):
                    if m * pk[e] > N:
                        break
                    new[m * pk[e]] = a[m] * h[e].real
        a = new
    return a


def ap_11a(p):
    """a_p of 11a: y^2 + y = x^3 - x^2; (2y+1)^2 = 4(x^3 - x^2) + 1, Legendre symbols for odd p."""
    if p == 2:
        cnt = 1 + sum(1 for x in range(2) for y in range(2) if (y * y + y - x ** 3 + x * x) % 2 == 0)
        return p + 1 - cnt
    cnt = 1
    for x in range(p):
        d = (4 * (x ** 3 - x * x) + 1) % p
        cnt += 1 if d == 0 else (2 if pow(d, (p - 1) // 2, p) == 1 else 0)
    return p + 1 - cnt


def sym_coeffs_11a(n, N):
    a = [0.0] * (N + 1)
    a[1] = 1.0
    for p in primes(N):
        maxe = int(math.log(N) / math.log(p) + 1e-9)
        if p == 11:
            h = [11 ** (-n * e / 2) for e in range(maxe + 1)]     # Steinberg at 11, a_11 = +1
        else:
            t = ap_11a(p) / p ** 0.5
            th = math.acos(max(-1.0, min(1.0, t / 2)))
            roots = [cmath.exp(1j * (n - 2 * j) * th) for j in range(n + 1)]
            hc = [1.0 + 0j] + [0j] * maxe
            for r in roots:
                for e in range(1, maxe + 1):
                    hc[e] += r * hc[e - 1]
            h = [x.real for x in hc]
        new = a[:]
        for m in range(1, N + 1):
            if a[m] and m % p:
                pe = p
                for e in range(1, maxe + 1):
                    if m * pe > N:
                        break
                    new[m * pe] = a[m] * h[e]
                    pe *= p
        a = new
    return a


def main():
    out = []
    ok = True

    def check(name, cond):
        nonlocal ok
        ok = ok and cond
        out.append(f"  [{'PASS' if cond else 'FAIL'}] {name}")

    NMAX = 1200
    tau = tau_list(NMAX)
    check("tau(2) = -24, tau(3) = 252, tau(11) = 534612", (tau[2], tau[3], tau[11]) == (-24, 252, 534612))
    out.append("eps(t) = theta(1/t) / (t theta(t)) for L(s, Sym^n Delta); constant +-1 <=> functional equation.")
    out.append("  n   degree  terms   eps(1.15)        eps(1.3)         eps(1.5)         spread")
    for n in range(1, 6):
        shC = [(n - 2 * j) * 5.5 for j in range(n // 2 + (1 if n % 2 else 0)) if n - 2 * j > 0]
        shR = [] if n % 2 else [0.0 if (n // 2) % 2 == 0 else 1.0]
        deg = 2 * len(shC) + len(shR)
        N = {1: 60, 2: 120, 3: 300, 4: 700, 5: NMAX}[n]
        a = sym_coeffs(n, N, tau)
        cache = {}

        def theta(t):
            s = 0.0
            for m in range(1, N + 1):
                if a[m] == 0:
                    continue
                x = m * t
                if x not in cache:
                    cache[x] = phi(x, shC, shR)
                s += a[m] * cache[x]
            return s
        eps = []
        for t in (1.15, 1.3, 1.5):
            eps.append(theta(1 / t) / (t * theta(t)))
        spread = max(eps) - min(eps)
        out.append(f"  {n}   {deg:<6}  {N:<6}  {eps[0]:+.10f}   {eps[1]:+.10f}   {eps[2]:+.10f}   {spread:.2e}")
        check(f"Sym^{n} Delta: eps(t) constant and = +-1 to 1e-6", spread < 1e-6 and abs(abs(eps[0]) - 1) < 1e-6)
    out.append("eps(t) for L(s, Sym^n 11a), conductor 11^n (Steinberg at 11), theta(t) = sum a_m phi(m t / sqrt(11^n)):")
    check("11a: a_2 = -2, a_3 = -1, a_5 = 1, a_7 = -2", [ap_11a(q) for q in (2, 3, 5, 7)] == [-2, -1, 1, -2])
    for n in (1, 2, 3):
        shC = [(n - 2 * j) * 0.5 for j in range(n // 2 + (1 if n % 2 else 0)) if n - 2 * j > 0]
        shR = [] if n % 2 else [0.0 if (n // 2) % 2 == 0 else 1.0]
        N = {1: 200, 2: 900, 3: 4000}[n]
        a11 = sym_coeffs_11a(n, N)
        sq = 11 ** (n / 2)
        cache = {}

        def theta11(t):
            s_ = 0.0
            for m in range(1, N + 1):
                if a11[m]:
                    x = m * t / sq
                    if x not in cache:
                        cache[x] = phi(x, shC, shR)
                    s_ += a11[m] * cache[x]
            return s_
        eps = [theta11(1 / t) / (t * theta11(t)) for t in (1.15, 1.3, 1.5)]
        spread = max(eps) - min(eps)
        out.append(f"  Sym^{n} 11a: terms {N}, eps = " + ", ".join(f"{e:+.8f}" for e in eps) + f", spread {spread:.1e}")
        check(f"Sym^{n} 11a: eps(t) constant and = +-1 to 1e-5", spread < 1e-5 and abs(abs(eps[0]) - 1) < 1e-5)
    a = sym_coeffs(3, 300, tau)
    a[2] *= 1.01
    th = lambda t: sum(a[k] * phi(k * t, [16.5, 5.5], []) for k in range(1, 301) if a[k])
    ctl = [th(1 / t) / (t * th(t)) for t in (1.15, 1.3, 1.5)]
    out.append("  control: Sym^3 with a(2) changed by 1%: eps = " + ", ".join(f"{e:+.4f}" for e in ctl))
    check("the test detects a 1% error in one coefficient (spread > 1e-3)", max(ctl) - min(ctl) > 1e-3)
    out.append(f"ALL {'PASS' if ok else 'FAIL'}")
    text = "\n".join(out) + "\n"
    with open(os.path.join(HERE, "NUMERIC_R4.txt"), "w", encoding="utf-8") as fh:
        fh.write(text)
    print(text, end="")
    return 0 if ok else 1


if __name__ == "__main__":
    raise SystemExit(main())

Listing 160: numeric_r7.py: \(A_5\) Artin sums

numeric_r7.py

#!/usr/bin/env python3
"""T35 R7 numerics: Artin L-functions with image A5, test for a pole at s = 1.

f(x) = x^5 + 20 x + 16 has Galois group A5. For unramified p the Frobenius class is read off the
degrees of the irreducible factors of f mod p (distinct-degree factorisation over F_p):
  1^5 -> identity, 2 2 1 -> involution, 3 1 1 -> order 3, 5 -> order 5.
Characters of A5 that depend only on the cycle type:
  chi4 = (fixed points) - 1           : 4, 0, 1, -1   (identity, (2,2), (3), (5))
  chi5                                : 5, 1, -1, 0
  chi3 + chi3'                        : 6, -2, 0, 1
log L(s, chi) = sum_p chi(Frob_p) p^(-s) + (holomorphic at s = 1); a pole (zero) of order k at s = 1
makes S(X) = sum_{p <= X} chi(Frob_p)/p grow like -k log log X (+k for a zero). Holomorphy and
non-vanishing at s = 1 show up as bounded S(X). Also: the Frobenius frequencies against Chebotarev
(1/60, 15/60, 20/60, 24/60).
Python standard library only. Writes work/NUMERIC_R7.txt.
"""
import math
import os

HERE = os.path.dirname(os.path.abspath(__file__))
F = [16, 20, 0, 0, 0, 1]          # coefficients, low degree first: x^5 + 20x + 16
DISC_PRIMES = {2, 5}              # disc(f) = 2^16 5^6


def pmod(a, m, p):
    """a mod m over F_p (lists low degree first, m monic)."""
    a = [x % p for x in a]
    dm = len(m) - 1
    while len(a) - 1 >= dm and any(a):
        while a and a[-1] == 0:
            a.pop()
        if len(a) - 1 < dm:
            break
        c = a[-1]
        sh = len(a) - 1 - dm
        for i in range(len(m)):
            a[sh + i] = (a[sh + i] - c * m[i]) % p
        while a and a[-1] == 0:
            a.pop()
    return a or [0]


def pmul(a, b, p):
    r = [0] * (len(a) + len(b) - 1)
    for i, x in enumerate(a):
        if x:
            for j, y in enumerate(b):
                r[i + j] = (r[i + j] + x * y) % p
    return r


def ppow_x(e, m, p):
    """x^e mod m."""
    result, base = [1], [0, 1]
    while e:
        if e & 1:
            result = pmod(pmul(result, base, p), m, p)
        base = pmod(pmul(base, base, p), m, p)
        e >>= 1
    return result


def psub(a, b, p):
    n = max(len(a), len(b))
    r = [((a[i] if i < len(a) else 0) - (b[i] if i < len(b) else 0)) % p for i in range(n)]
    while len(r) > 1 and r[-1] == 0:
        r.pop()
    return r


def pgcd(a, b, p):
    while any(b):
        a, b = b, pmod(a, monic(b, p), p) if any(b) else b
        a = monic(a, p) if any(a) else a
    return monic(a, p)


def monic(a, p):
    while len(a) > 1 and a[-1] == 0:
        a = a[:-1]
    inv = pow(a[-1], p - 2, p)
    return [x * inv % p for x in a]


def pdiv(a, b, p):
    a = [x % p for x in a]
    q = [0] * max(1, len(a) - len(b) + 1)
    b = monic(b, p)
    while len(a) >= len(b) and any(a):
        c = a[-1]
        sh = len(a) - len(b)
        q[sh] = c
        for i in range(len(b)):
            a[sh + i] = (a[sh + i] - c * b[i]) % p
        a.pop()
    return q


def cycle_type(p):
    f = monic(F, p)
    degs = []
    h = [0, 1]
    k = 0
    g = f
    while len(g) > 1:
        k += 1
        h = ppow_x(p ** k, g, p) if False else ppow_frob(h, g, p)
        d = pgcd(g, psub(h, [0, 1], p), p)
        nd = len(d) - 1
        if nd:
            degs += [k] * (nd // k)
            g = pdiv(g, d, p)
            h = pmod(h, g, p) if len(g) > 1 else h
        if 2 * k > len(g) - 1 and len(g) > 1:
            degs.append(len(g) - 1)
            break
    return tuple(sorted(degs, reverse=True))


def ppow_frob(h, m, p):
    """h(x)^p mod m."""
    result, base, e = [1], pmod(h, m, p), p
    while e:
        if e & 1:
            result = pmod(pmul(result, base, p), m, p)
        base = pmod(pmul(base, base, p), m, p)
        e >>= 1
    return result


def primes(n):
    s = bytearray([1]) * (n + 1)
    s[0] = s[1] = 0
    for i in range(2, int(n ** 0.5) + 1):
        if s[i]:
            s[i * i::i] = bytearray(len(s[i * i::i]))
    return [i for i in range(n + 1) if s[i]]


CHI = {"chi4": {(1, 1, 1, 1, 1): 4, (2, 2, 1): 0, (3, 1, 1): 1, (5,): -1},
       "chi5": {(1, 1, 1, 1, 1): 5, (2, 2, 1): 1, (3, 1, 1): -1, (5,): 0},
       "chi3+chi3'": {(1, 1, 1, 1, 1): 6, (2, 2, 1): -2, (3, 1, 1): 0, (5,): 1}}


def main():
    out = []
    ok = True

    def check(name, cond):
        nonlocal ok
        ok = ok and cond
        out.append(f"  [{'PASS' if cond else 'FAIL'}] {name}")

    X = 300000
    ps = [p for p in primes(X) if p not in DISC_PRIMES]
    counts = {}
    S = {k: 0.0 for k in CHI}
    marks = [1000, 10000, 100000, 300000]
    rows = []
    for p in ps:
        ct = cycle_type(p)
        counts[ct] = counts.get(ct, 0) + 1
        for k, tab in CHI.items():
            S[k] += tab[ct] / p
        if marks and p > marks[0]:
            rows.append((marks.pop(0), dict(S)))
    rows.append((X, dict(S)))
    n = sum(counts.values())
    out.append(f"f = x^5 + 20x + 16, unramified primes up to {X}: {n}")
    out.append("Frobenius cycle types against Chebotarev for A5:")
    expect = {(1, 1, 1, 1, 1): 1 / 60, (2, 2, 1): 15 / 60, (3, 1, 1): 20 / 60, (5,): 24 / 60}
    good = set(counts) <= set(expect)
    check("only the cycle types of A5 occur (1^5, 2^2 1, 3 1^2, 5)", good)
    for ct, e in expect.items():
        out.append(f"  {str(ct):<16} {counts.get(ct, 0) / n:.4f}  (Chebotarev {e:.4f})")
    dev = max(abs(counts.get(ct, 0) / n - e) for ct, e in expect.items())
    check("frequencies within 0.01 of Chebotarev", dev < 0.01)
    out.append("S(X) = sum_{p <= X} chi(Frob_p)/p   (a pole of order k gives -k log log X)")
    out.append("  X        log log X   " + "   ".join(f"{k:>11}" for k in CHI))
    for x, s in rows:
        out.append(f"  {x:<8} {math.log(math.log(x)):9.3f}   " + "   ".join(f"{s[k]:11.4f}" for k in CHI))
    last = rows[-1][1]
    first = rows[1][1]
    drift = {k: last[k] - first[k] for k in CHI}
    ll = math.log(math.log(X)) - math.log(math.log(10000))
    out.append("  change of S from X = 10^4 to 3*10^5 against the change of log log X (" + f"{ll:.3f}): "
               + ", ".join(f"{k}: {v:+.4f}" for k, v in drift.items()))
    check("no sum drifts by a pole's amount (|change| < 0.5 * change of log log X)",
          all(abs(v) < 0.5 * ll for v in drift.values()))
    out.append("Known: chi4 (zeta_K / zeta, Aramata-Brauer); chi3, chi3' and the 2-dimensional lift are holomorphic")
    out.append("when the 2-dimensional lift is odd (Khare-Wintenberger); f has one real root, so complex conjugation")
    out.append("is an involution (2,2) and the lift is odd.")
    out.append(f"ALL {'PASS' if ok else 'FAIL'}")
    text = "\n".join(out) + "\n"
    with open(os.path.join(HERE, "NUMERIC_R7.txt"), "w", encoding="utf-8") as fh:
        fh.write(text)
    print(text, end="")
    return 0 if ok else 1


if __name__ == "__main__":
    raise SystemExit(main())

Listing 161: NUMERIC_R1.txt

NUMERIC_R1.txt

Bianchi groups, l0 = 1, range [q0, q0 + l0] = [1, 2]. Cohomology mod ell of Gamma_0(pi).
 d  N(pi)  ell  b1(Q)  dim Hom(F_ell)  cusp(F_ell)  T_lam  eigenvalues mod ell (dim)          non-Eis dims  Eis value
 1  2      5    0      0               0            -
 1  2      7    0      0               0            -
 1  2      11   0      0               0            -
 1  2      13   0      0               0            -
 1  5      7    0      0               0            -
 1  5      11   0      0               0            -
 1  5      13   0      0               0            -
 1  5      7    0      0               0            -
 1  5      11   0      0               0            -
 1  5      13   0      0               0            -
 1  9      5    0      0               0            -
 1  9      7    0      0               0            -
 1  9      11   0      0               0            -
 1  9      13   0      0               0            -
 1  13     5    0      0               0            -
 1  13     7    0      0               0            -
 1  13     11   0      0               0            -
 1  13     5    0      0               0            -
 1  13     7    0      0               0            -
 1  13     11   0      0               0            -
 1  17     5    0      0               0            -
 1  17     7    0      0               0            -
 1  17     11   0      0               0            -
 1  17     13   0      0               0            -
 1  17     5    0      0               0            -
 1  17     7    0      0               0            -
 1  17     11   0      0               0            -
 1  17     13   0      0               0            -
 1  29     5    0      0               0            -
 1  29     7    0      1               1            N=2    3(1)                               0             3
  [PASS] d=1, N=29, ell=7: cuspidal dimension mod ell >= rational b1_cusp
 1  29     11   0      0               0            -
 1  29     13   0      0               0            -
 1  29     5    0      0               0            -
 1  29     7    0      1               1            N=2    3(1)                               0             3
  [PASS] d=1, N=29, ell=7: cuspidal dimension mod ell >= rational b1_cusp
 1  29     11   0      0               0            -
 1  29     13   0      0               0            -
 1  37     5    0      0               0            -
 1  37     7    0      0               0            -
 1  37     11   0      0               0            -
 1  37     13   0      0               0            -
 1  37     5    0      0               0            -
 1  37     7    0      0               0            -
 1  37     11   0      0               0            -
 1  37     13   0      0               0            -
 1  41     5    0      1               1            N=2    3(1)                               0             3
  [PASS] d=1, N=41, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 1  41     7    0      0               0            -
 1  41     11   0      0               0            -
 1  41     13   0      0               0            -
 1  41     5    0      1               1            N=2    3(1)                               0             3
  [PASS] d=1, N=41, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 1  41     7    0      0               0            -
 1  41     11   0      0               0            -
 1  41     13   0      0               0            -
 1  49     5    0      0               0            -
 1  49     11   0      0               0            -
 1  49     13   0      0               0            -
 1  53     5    0      0               0            -
 1  53     7    0      0               0            -
 1  53     11   0      0               0            -
 1  53     13   0      1               1            N=2    3(1)                               0             3
  [PASS] d=1, N=53, ell=13: cuspidal dimension mod ell >= rational b1_cusp
 1  53     5    0      0               0            -
 1  53     7    0      0               0            -
 1  53     11   0      0               0            -
 1  53     13   0      1               1            N=2    3(1)                               0             3
  [PASS] d=1, N=53, ell=13: cuspidal dimension mod ell >= rational b1_cusp
 1  61     5    0      1               1            N=2    3(1)                               0             3
  [PASS] d=1, N=61, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 1  61     7    0      0               0            -
 1  61     11   0      0               0            -
 1  61     13   0      0               0            -
 1  61     5    0      1               1            N=2    3(1)                               0             3
  [PASS] d=1, N=61, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 1  61     7    0      0               0            -
 1  61     11   0      0               0            -
 1  61     13   0      0               0            -
 1  73     5    0      0               0            -
 1  73     7    0      0               0            -
 1  73     11   0      0               0            -
 1  73     13   0      0               0            -
 1  73     5    0      0               0            -
 1  73     7    0      0               0            -
 1  73     11   0      0               0            -
 1  73     13   0      0               0            -
 1  89     5    0      0               0            -
 1  89     7    0      0               0            -
 1  89     11   0      2               2            N=2    3(1) 6(1)                          1             3
  [PASS] d=1, N=89, ell=11: cuspidal dimension mod ell >= rational b1_cusp
 1  89     13   0      0               0            -
 1  89     5    0      0               0            -
 1  89     7    0      0               0            -
 1  89     11   0      2               2            N=2    3(1) 5(1)                          1             3
  [PASS] d=1, N=89, ell=11: cuspidal dimension mod ell >= rational b1_cusp
 1  89     13   0      0               0            -
 1  97     5    0      1               1            N=2    2(1)                               1             3
  [PASS] d=1, N=97, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 1  97     7    0      0               0            -
 1  97     11   0      0               0            -
 1  97     13   0      0               0            -
 1  97     5    0      1               1            N=2    2(1)                               1             3
  [PASS] d=1, N=97, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 1  97     7    0      0               0            -
 1  97     11   0      0               0            -
 1  97     13   0      0               0            -
 1  101    5    0      1               1            N=2    3(1)                               0             3
  [PASS] d=1, N=101, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 1  101    7    0      0               0            -
 1  101    11   0      0               0            -
 1  101    13   0      0               0            -
 1  101    5    0      1               1            N=2    3(1)                               0             3
  [PASS] d=1, N=101, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 1  101    7    0      0               0            -
 1  101    11   0      0               0            -
 1  101    13   0      0               0            -
 3  3      5    0      0               0            -
 3  3      7    0      0               0            -
 3  3      11   0      0               0            -
 3  3      13   0      0               0            -
 3  4      5    0      0               0            -
 3  4      7    0      0               0            -
 3  4      11   0      0               0            -
 3  4      13   0      0               0            -
 3  7      5    0      0               0            -
 3  7      11   0      0               0            -
 3  7      13   0      0               0            -
 3  7      5    0      0               0            -
 3  7      11   0      0               0            -
 3  7      13   0      0               0            -
 3  13     5    0      0               0            -
 3  13     7    0      0               0            -
 3  13     11   0      0               0            -
 3  13     5    0      0               0            -
 3  13     7    0      0               0            -
 3  13     11   0      0               0            -
 3  19     5    0      0               0            -
 3  19     7    0      0               0            -
 3  19     11   0      0               0            -
 3  19     13   0      0               0            -
 3  19     5    0      0               0            -
 3  19     7    0      0               0            -
 3  19     11   0      0               0            -
 3  19     13   0      0               0            -
 3  25     7    0      0               0            -
 3  25     11   0      0               0            -
 3  25     13   0      0               0            -
 3  31     5    0      1               1            N=3    4(1)                               0             4
  [PASS] d=3, N=31, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 3  31     7    0      0               0            -
 3  31     11   0      0               0            -
 3  31     13   0      0               0            -
 3  31     5    0      1               1            N=3    4(1)                               0             4
  [PASS] d=3, N=31, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 3  31     7    0      0               0            -
 3  31     11   0      0               0            -
 3  31     13   0      0               0            -
 3  37     5    0      0               0            -
 3  37     7    0      0               0            -
 3  37     11   0      0               0            -
 3  37     13   0      0               0            -
 3  37     5    0      0               0            -
 3  37     7    0      0               0            -
 3  37     11   0      0               0            -
 3  37     13   0      0               0            -
 3  43     5    0      0               0            -
 3  43     7    0      1               1            N=3    4(1)                               0             4
  [PASS] d=3, N=43, ell=7: cuspidal dimension mod ell >= rational b1_cusp
 3  43     11   0      0               0            -
 3  43     13   0      0               0            -
 3  43     5    0      0               0            -
 3  43     7    0      1               1            N=3    4(1)                               0             4
  [PASS] d=3, N=43, ell=7: cuspidal dimension mod ell >= rational b1_cusp
 3  43     11   0      0               0            -
 3  43     13   0      0               0            -
 3  61     5    0      1               1            N=3    4(1)                               0             4
  [PASS] d=3, N=61, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 3  61     7    0      0               0            -
 3  61     11   0      0               0            -
 3  61     13   0      0               0            -
 3  61     5    0      1               1            N=3    4(1)                               0             4
  [PASS] d=3, N=61, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 3  61     7    0      0               0            -
 3  61     11   0      0               0            -
 3  61     13   0      0               0            -
 3  67     5    0      1               1            N=3    4(1)                               0             4
  [PASS] d=3, N=67, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 3  67     7    0      0               0            -
 3  67     11   0      1               1            N=3    4(1)                               0             4
  [PASS] d=3, N=67, ell=11: cuspidal dimension mod ell >= rational b1_cusp
 3  67     13   0      0               0            -
 3  67     5    0      1               1            N=3    1(1)                               1             4
  [PASS] d=3, N=67, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 3  67     7    0      0               0            -
 3  67     11   0      1               1            N=3    4(1)                               0             4
  [PASS] d=3, N=67, ell=11: cuspidal dimension mod ell >= rational b1_cusp
 3  67     13   0      0               0            -
 3  73     5    1      1               1            N=3    3(1)                               1             4
  [PASS] d=3, N=73, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 3  73     7    1      1               1            N=3    5(1)                               1             4
  [PASS] d=3, N=73, ell=7: cuspidal dimension mod ell >= rational b1_cusp
 3  73     11   1      1               1            N=3    9(1)                               1             4
  [PASS] d=3, N=73, ell=11: cuspidal dimension mod ell >= rational b1_cusp
 3  73     13   1      1               1            N=3    11(1)                              1             4
  [PASS] d=3, N=73, ell=13: cuspidal dimension mod ell >= rational b1_cusp
 3  73     5    1      1               1            N=3    3(1)                               1             4
  [PASS] d=3, N=73, ell=5: cuspidal dimension mod ell >= rational b1_cusp
 3  73     7    1      1               1            N=3    5(1)                               1             4
  [PASS] d=3, N=73, ell=7: cuspidal dimension mod ell >= rational b1_cusp
 3  73     11   1      1               1            N=3    9(1)                               1             4
  [PASS] d=3, N=73, ell=11: cuspidal dimension mod ell >= rational b1_cusp
 3  73     13   1      1               1            N=3    11(1)                              1             4
  [PASS] d=3, N=73, ell=13: cuspidal dimension mod ell >= rational b1_cusp
 3  79     5    0      0               0            -
 3  79     7    0      0               0            -
 3  79     11   0      0               0            -
 3  79     13   0      1               1            N=3    4(1)                               0             4
  [PASS] d=3, N=79, ell=13: cuspidal dimension mod ell >= rational b1_cusp
 3  79     5    0      0               0            -
 3  79     7    0      0               0            -
 3  79     11   0      0               0            -
 3  79     13   0      1               1            N=3    4(1)                               0             4
  [PASS] d=3, N=79, ell=13: cuspidal dimension mod ell >= rational b1_cusp
 3  97     5    0      0               0            -
 3  97     7    0      0               0            -
 3  97     11   0      0               0            -
 3  97     13   0      0               0            -
 3  97     5    0      0               0            -
 3  97     7    0      0               0            -
 3  97     11   0      0               0            -
 3  97     13   0      0               0            -
 3  103    5    0      0               0            -
 3  103    7    0      0               0            -
 3  103    11   0      0               0            -
 3  103    13   0      0               0            -
 3  103    5    0      0               0            -
 3  103    7    0      0               0            -
 3  103    11   0      0               0            -
 3  103    13   0      0               0            -

Torsion classes (dim mod ell above the rational rank), with their maximal ideals:
  d=1, N=29, ell=7: rational 0, mod ell 1; eigenvalues 3(1); non-Eisenstein part 0
  d=1, N=29, ell=7: rational 0, mod ell 1; eigenvalues 3(1); non-Eisenstein part 0
  d=1, N=41, ell=5: rational 0, mod ell 1; eigenvalues 3(1); non-Eisenstein part 0
  d=1, N=41, ell=5: rational 0, mod ell 1; eigenvalues 3(1); non-Eisenstein part 0
  d=1, N=53, ell=13: rational 0, mod ell 1; eigenvalues 3(1); non-Eisenstein part 0
  d=1, N=53, ell=13: rational 0, mod ell 1; eigenvalues 3(1); non-Eisenstein part 0
  d=1, N=61, ell=5: rational 0, mod ell 1; eigenvalues 3(1); non-Eisenstein part 0
  d=1, N=61, ell=5: rational 0, mod ell 1; eigenvalues 3(1); non-Eisenstein part 0
  d=1, N=89, ell=11: rational 0, mod ell 2; eigenvalues 3(1) 6(1); non-Eisenstein part 1
  d=1, N=89, ell=11: rational 0, mod ell 2; eigenvalues 3(1) 5(1); non-Eisenstein part 1
  d=1, N=97, ell=5: rational 0, mod ell 1; eigenvalues 2(1); non-Eisenstein part 1
  d=1, N=97, ell=5: rational 0, mod ell 1; eigenvalues 2(1); non-Eisenstein part 1
  d=1, N=101, ell=5: rational 0, mod ell 1; eigenvalues 3(1); non-Eisenstein part 0
  d=1, N=101, ell=5: rational 0, mod ell 1; eigenvalues 3(1); non-Eisenstein part 0
  d=3, N=31, ell=5: rational 0, mod ell 1; eigenvalues 4(1); non-Eisenstein part 0
  d=3, N=31, ell=5: rational 0, mod ell 1; eigenvalues 4(1); non-Eisenstein part 0
  d=3, N=43, ell=7: rational 0, mod ell 1; eigenvalues 4(1); non-Eisenstein part 0
  d=3, N=43, ell=7: rational 0, mod ell 1; eigenvalues 4(1); non-Eisenstein part 0
  d=3, N=61, ell=5: rational 0, mod ell 1; eigenvalues 4(1); non-Eisenstein part 0
  d=3, N=61, ell=5: rational 0, mod ell 1; eigenvalues 4(1); non-Eisenstein part 0
  d=3, N=67, ell=5: rational 0, mod ell 1; eigenvalues 4(1); non-Eisenstein part 0
  d=3, N=67, ell=11: rational 0, mod ell 1; eigenvalues 4(1); non-Eisenstein part 0
  d=3, N=67, ell=5: rational 0, mod ell 1; eigenvalues 1(1); non-Eisenstein part 1
  d=3, N=67, ell=11: rational 0, mod ell 1; eigenvalues 4(1); non-Eisenstein part 0
  d=3, N=79, ell=13: rational 0, mod ell 1; eigenvalues 4(1); non-Eisenstein part 0
  d=3, N=79, ell=13: rational 0, mod ell 1; eigenvalues 4(1); non-Eisenstein part 0
  26 (level, ell) pairs with torsion; 5 with torsion at a non-Eisenstein m
For every non-Eisenstein m in the table: H^0_m = 0 and H^3 = 0, so the localised cohomology
is concentrated in [q0, q0 + l0] = [1, 2]; the Koszul form with l0 = 1 is KoszulRangeChain.range1_iff.
ALL PASS

Listing 162: NUMERIC_R1_GL3.txt

NUMERIC_R1_GL3.txt

  [PASS] every Steinberg relator is the identity on the matrices
GL3/Q, Gamma_0(p) in SL3(Z): H^1 (degree 1 < q0 = 2).
  p    index  Schreier gens   dim H^1(Q)   dim H^1(F_5)  dim H^1(F_7)  dim H^1(F_11)  dim H^1(F_13)
  2    7      36              0            0              0              0              0            
  3    13     66              0            0              0              0              0            
  5    31     156             0            0              0              0              0            
  7    57     286             0            0              0              0              0            
  11   133    666             0            1              0              0              0            
       ell = 5 divides p - 1: the torus character g -> g_33 mod p -> Z/5 is a class: True
  13   183    916             0            0              0              0              0            
  17   307    1536            0            0              0              0              0            
  [PASS] H^1(Gamma_0(p), Q) = 0; H^1(Gamma_0(p), F_ell) is 0, or spanned by the Eisenstein torus character when ell | p - 1
So for these levels and ell, at every non-Eisenstein m the cohomology outside [q0, q0 + l0] = [2, 3]
vanishes: degree 0 is Eisenstein, degree 1 is zero or the Eisenstein torus class, degrees > 3 are zero.
ALL PASS

Listing 163: NUMERIC_R2.txt

NUMERIC_R2.txt

2I = SL2(F5) (linear icosahedral image): order 120, 9 classes, 76 subgroups, orders of proper subgroups [2, 3, 4, 5, 6, 8, 10, 12, 20, 24]
  irreducible degrees: [1, 2, 2, 3, 3, 4, 4, 5, 6]
  degree 1: subgroups of index 1: 1; monomial: yes
  degree 2: subgroups of index 2: 0; monomial: no (no subgroup of that index)
  degree 2: subgroups of index 2: 0; monomial: no (no subgroup of that index)
  degree 3: subgroups of index 3: 0; monomial: no (no subgroup of that index)
  degree 3: subgroups of index 3: 0; monomial: no (no subgroup of that index)
  degree 4: subgroups of index 4: 0; monomial: no (no subgroup of that index)
  degree 4: subgroups of index 4: 0; monomial: no (no subgroup of that index)
  degree 5: subgroups of index 5: 5; monomial: yes
  degree 6: subgroups of index 6: 6; monomial: yes
  [PASS] 2I has irreducible degrees 1, 2, 2, 3, 3, 4, 4, 5, 6
  degrees with no subgroup of that index (hence not monomial): [2, 2, 3, 3, 4, 4]
  [PASS] the two 2-dimensional (icosahedral Galois) representations of 2I are not monomial
A5 = 2I/(+-1): irreducibles of 2I trivial on -1 have degrees [1, 3, 3, 4, 5]
  [PASS] A5 has irreducible degrees 1, 3, 3, 4, 5
  orders of subgroups of A5 (images of subgroups of 2I containing -1): [1, 2, 3, 4, 5, 6, 10, 12, 60]
  [PASS] A5 has no subgroup of index 3 or 4, so its degree 3, 3, 4 irreducibles are not monomial
Consequence: for an icosahedral rho (image 2I in GL2), any Brauer expression of rho through
solvable subgroups has negative coefficients; descent must cancel virtual automorphic
representations. This is the finite form of R2-gap.
ALL PASS

Listing 164: NUMERIC_R3.txt

NUMERIC_R3.txt

1. Hurwitz class numbers.
  [PASS] H(3) = 1/3, H(4) = 1/2, H(23) = 3, H(15) = 2
2. k = 2: the elliptic sum cancels the trivial representation (Kronecker-Hurwitz).
  [PASS] sum_t H(4n - t^2) = 2 sigma_1(n) - sum min(d, d') for n = 1..200 (exact)
3. k = 12: Eichler-Selberg gives tau(n).
  [PASS] tr T_n | S_12 = tau(n) for n = 1..60 (exact)
4. k = 12: elliptic sum E(n) against tau(n) (discrete-series test function: no trivial term).
   n      E(n)/2                      tau(n)          |E/2| / |tau|   |E/2| / n^(11/2)
   2                            23              -24         0.9583       0.5082
   3                          -253              252          1.004       0.6011
   5                         -4831             4830              1       0.6914
   7                         16743           -16744         0.9999       0.3765
   11                      -534613           534612              1        1.001
   13                       577737          -577738              1       0.4316
   23                 -1.86433e+07         18643272              1        0.604
   37                  1.82213e+08       -182213314              1        0.432
   53                  1.59606e+09      -1596055698              1       0.5242

GL3: elliptic characteristic polynomials x^3 - a x^2 + b x - n with one real and two complex
roots, all of absolute value n^(1/3) up to a factor 2 (tempered box): number of terms.
   n      terms     terms / n^(2/3)   terms / n
   2      297       187.1            148.5
   3      425       204.3            141.7
   5      752       257.2            150.4
   7      1021      279              145.9
   11     1635      330.6            148.6
   13     1949      352.5            149.9
   23     3460      427.8            150.4
   37     5372      483.8            145.2
   53     7809      553.4            147.3
   101    14743     679.8            146
   211    30975     874              146.8
   The number of GL3 elliptic terms grows like n^(1/3) * n^(2/3) = n (box area), each weighted by
   a class number of a cubic order; the GL2 analogue grows like n^(1/2) terms. The GL3 Poisson
   cancellation of the trivial representation (size n^(dim/2)) is the missing step (R3-gap).
ALL PASS

Listing 165: NUMERIC_R4.txt

NUMERIC_R4.txt

  [PASS] tau(2) = -24, tau(3) = 252, tau(11) = 534612
eps(t) = theta(1/t) / (t theta(t)) for L(s, Sym^n Delta); constant +-1 <=> functional equation.
  n   degree  terms   eps(1.15)        eps(1.3)         eps(1.5)         spread
  1   2       60      +1.0000000000   +1.0000000000   +1.0000000000   4.44e-16
  [PASS] Sym^1 Delta: eps(t) constant and = +-1 to 1e-6
  2   3       120     +1.0000000000   +1.0000000000   +1.0000000000   4.44e-15
  [PASS] Sym^2 Delta: eps(t) constant and = +-1 to 1e-6
  3   4       300     -1.0000000000   -1.0000000000   -1.0000000000   2.44e-15
  [PASS] Sym^3 Delta: eps(t) constant and = +-1 to 1e-6
  4   5       700     +1.0000000000   +1.0000000000   +1.0000000000   3.11e-15
  [PASS] Sym^4 Delta: eps(t) constant and = +-1 to 1e-6
  5   6       1200    -1.0000000000   -1.0000000000   -1.0000000000   1.73e-13
  [PASS] Sym^5 Delta: eps(t) constant and = +-1 to 1e-6
eps(t) for L(s, Sym^n 11a), conductor 11^n (Steinberg at 11), theta(t) = sum a_m phi(m t / sqrt(11^n)):
  [PASS] 11a: a_2 = -2, a_3 = -1, a_5 = 1, a_7 = -2
  Sym^1 11a: terms 200, eps = +1.00000000, +1.00000000, +1.00000000, spread 7.2e-15
  [PASS] Sym^1 11a: eps(t) constant and = +-1 to 1e-5
  Sym^2 11a: terms 900, eps = +1.00000000, +1.00000000, +1.00000000, spread 2.0e-15
  [PASS] Sym^2 11a: eps(t) constant and = +-1 to 1e-5
  Sym^3 11a: terms 4000, eps = +1.00000000, +1.00000000, +1.00000000, spread 5.2e-15
  [PASS] Sym^3 11a: eps(t) constant and = +-1 to 1e-5
  control: Sym^3 with a(2) changed by 1%: eps = -0.9692, -0.9840, -0.9903
  [PASS] the test detects a 1% error in one coefficient (spread > 1e-3)
ALL PASS

Listing 166: NUMERIC_R7.txt

NUMERIC_R7.txt

f = x^5 + 20x + 16, unramified primes up to 300000: 25995
Frobenius cycle types against Chebotarev for A5:
  [PASS] only the cycle types of A5 occur (1^5, 2^2 1, 3 1^2, 5)
  (1, 1, 1, 1, 1)  0.0164  (Chebotarev 0.0167)
  (2, 2, 1)        0.2503  (Chebotarev 0.2500)
  (3, 1, 1)        0.3329  (Chebotarev 0.3333)
  (5,)             0.4004  (Chebotarev 0.4000)
  [PASS] frequencies within 0.01 of Chebotarev
S(X) = sum_{p <= X} chi(Frob_p)/p   (a pole of order k gives -k log log X)
  X        log log X          chi4          chi5    chi3+chi3'
  1000         1.933       -0.2313       -0.3327        0.3857
  10000        2.220       -0.2255       -0.3462        0.3841
  100000       2.443       -0.2314       -0.3437        0.3839
  300000       2.535       -0.2314       -0.3438        0.3841
  change of S from X = 10^4 to 3*10^5 against the change of log log X (0.314): chi4: -0.0059, chi5: +0.0025, chi3+chi3': +0.0000
  [PASS] no sum drifts by a pole's amount (|change| < 0.5 * change of log log X)
Known: chi4 (zeta_K / zeta, Aramata-Brauer); chi3, chi3' and the 2-dimensional lift are holomorphic
when the 2-dimensional lift is odd (Khare-Wintenberger); f has one real root, so complex conjugation
is an involution (2,2) and the lift is odd.
ALL PASS