Theorem: lucas_primality
File: Mathlib/NumberTheory/LucasPrimality.lean

tactic: have h0 : p ≠ 0 := by
  rintro ⟨⟩
  exact hd 2 Nat.prime_two (dvd_zero _) (pow_zero _)
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
⊢ Nat.Prime p

tactic: have h1 : p ≠ 1 := by
  rintro ⟨⟩
  exact hd 2 Nat.prime_two (dvd_zero _) (pow_zero _)
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
⊢ Nat.Prime p

tactic: have hp1 : 1 < p := lt_of_le_of_ne h0.bot_lt h1.symm
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
h1 : p ≠ 1
⊢ Nat.Prime p

tactic: have order_of_a : orderOf a = p - 1 := by
  apply orderOf_eq_of_pow_and_pow_div_prime _ ha hd
  exact tsub_pos_of_lt hp1
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
h1 : p ≠ 1
hp1 : 1 < p
⊢ Nat.Prime p

tactic: haveI : NeZero p := ⟨h0⟩
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
h1 : p ≠ 1
hp1 : 1 < p
order_of_a : orderOf a = p - 1
⊢ Nat.Prime p

tactic: rw [Nat.prime_iff_card_units]
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
h1 : p ≠ 1
hp1 : 1 < p
order_of_a : orderOf a = p - 1
this : NeZero p
⊢ Nat.Prime p

tactic: refine' le_antisymm (Nat.card_units_zmod_lt_sub_one hp1) _
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
h1 : p ≠ 1
hp1 : 1 < p
order_of_a : orderOf a = p - 1
this : NeZero p
⊢ Fintype.card (ZMod p)ˣ = p - 1

tactic: have hp' : p - 2 + 1 = p - 1 := tsub_add_eq_add_tsub hp1
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
h1 : p ≠ 1
hp1 : 1 < p
order_of_a : orderOf a = p - 1
this : NeZero p
⊢ p - 1 ≤ Fintype.card (ZMod p)ˣ

tactic: let a' : (ZMod p)ˣ := Units.mkOfMulEqOne a (a ^ (p - 2)) (by rw [← pow_succ, hp', ha])
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
h1 : p ≠ 1
hp1 : 1 < p
order_of_a : orderOf a = p - 1
this : NeZero p
hp' : p - 2 + 1 = p - 1
⊢ p - 1 ≤ Fintype.card (ZMod p)ˣ

tactic: calc
  p - 1 = orderOf a := order_of_a.symm
  _ = orderOf a' := (orderOf_injective (Units.coeHom (ZMod p)) Units.ext a')
  _ ≤ Fintype.card (ZMod p)ˣ := orderOf_le_card_univ
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
h1 : p ≠ 1
hp1 : 1 < p
order_of_a : orderOf a = p - 1
this : NeZero p
hp' : p - 2 + 1 = p - 1
a' : (ZMod p)ˣ := Units.mkOfMulEqOne a (a ^ (p - 2)) (_ : a * a ^ (p - 2) = 1)
⊢ p - 1 ≤ Fintype.card (ZMod p)ˣ

tactic: rintro ⟨⟩
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
⊢ p ≠ 0

tactic: exact hd 2 Nat.prime_two (dvd_zero _) (pow_zero _)
state_before: case refl
a : ZMod 0
ha : a ^ (0 - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ 0 - 1 → a ^ ((0 - 1) / q) ≠ 1
⊢ False

tactic: rintro ⟨⟩
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
⊢ p ≠ 1

tactic: exact hd 2 Nat.prime_two (dvd_zero _) (pow_zero _)
state_before: case refl
a : ZMod 1
ha : a ^ (1 - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ 1 - 1 → a ^ ((1 - 1) / q) ≠ 1
h0 : 1 ≠ 0
⊢ False

tactic: apply orderOf_eq_of_pow_and_pow_div_prime _ ha hd
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
h1 : p ≠ 1
hp1 : 1 < p
⊢ orderOf a = p - 1

tactic: exact tsub_pos_of_lt hp1
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
h1 : p ≠ 1
hp1 : 1 < p
⊢ 0 < p - 1

tactic: rw [← pow_succ, hp', ha]
state_before: p : ℕ
a : ZMod p
ha : a ^ (p - 1) = 1
hd : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → a ^ ((p - 1) / q) ≠ 1
h0 : p ≠ 0
h1 : p ≠ 1
hp1 : 1 < p
order_of_a : orderOf a = p - 1
this : NeZero p
hp' : p - 2 + 1 = p - 1
⊢ a * a ^ (p - 2) = 1