(**************************************************************)
(*   Copyright Dominique Larchey-Wendling [*]                 *)
(*                                                            *)
(*                             [*] Affiliation LORIA -- CNRS  *)
(**************************************************************)
(*      This file is distributed under the terms of the       *)
(*         CeCILL v2 FREE SOFTWARE LICENSE AGREEMENT          *)
(**************************************************************)

(** ** Solutions of Pell's equation *)

Require Import Arith Omega Eqdep_dec ZArith.

Require Import utils_tac gcd prime binomial sums matrix Zp.

Set Implicit Arguments.

Section Zp_alpha_2.

  Variable (ak am k l m : nat)
           (H1 : m = (l*k)%nat)
           (H2 : (ak*ak <> 0)%nat).

  Notation "〚 u 〛" := (nat2Zp H2 u).
  Infix "⊗" := (Zp_mult H2) (at level 40, left associativity).

  Hypothesis (H3 : (m <> 0)%nat)
             (H4 : exists q, Zp_invertible H2 q /\〚am〛 = q⊗〚l〛⊗〚ak〛). 

  Let Hak : ak <> 0.  Proof. intro; subst; destruct H2; auto. Qed.
  Let Hl : l <> 0.    Proof. contradict H3; subst; ring. Qed.
  Let Hk : k <> 0.    Proof. contradict H3; subst; ring. Qed.

  Lemma Zp_alpha_congruence_2 : divides (ak*ak) am <-> divides (k*ak) m.
  Proof.
(* Goal: iff (divides (Init.Nat.mul ak ak) am) (divides (Init.Nat.mul k ak) m) *)
    rewrite mult_comm in H1.
(* Goal: iff (divides (Init.Nat.mul ak ak) am) (divides (Init.Nat.mul k ak) m) *)
    destruct H4 as (q & Hq1 & Hq2).
(* Goal: iff (divides (Init.Nat.mul ak ak) am) (divides (Init.Nat.mul k ak) m) *)
    split; intros H.
    + rewrite (proj1 (divides_nat2Zp _ _)) in Hq2; auto.
(* Goal: divides (Init.Nat.mul k ak) m *)
      symmetry in Hq2.
(* Goal: divides (Init.Nat.mul k ak) m *)
      rewrite <- Zp_mult_assoc in Hq2.
(* Goal: divides (Init.Nat.mul k ak) m *)
      apply Zp_invertible_eq_zero in Hq2; auto.
(* Goal: divides (Init.Nat.mul k ak) m *)
      rewrite <- nat2Zp_mult, <- divides_nat2Zp in Hq2.
(* Goal: divides (Init.Nat.mul k ak) m *)
      destruct Hq2 as (d & Hd); exists d; subst m.
(* Goal: @eq nat (Nat.mul k l) (Init.Nat.mul d (Init.Nat.mul k ak)) *)
      rewrite <- Nat.mul_cancel_r with (1 := Hak).
(* Goal: @eq nat (Nat.mul (Nat.mul k l) ak) (Nat.mul (Init.Nat.mul d (Init.Nat.mul k ak)) ak) *)
      rewrite <- mult_assoc, Hd; ring.
    + subst m.
(* Goal: divides (Init.Nat.mul ak ak) am *)
      apply divides_mult_inv in H; auto.
(* Goal: divides (Init.Nat.mul ak ak) am *)
      destruct H as (g & Hg); subst l.
(* Goal: divides (Init.Nat.mul ak ak) am *)
      rewrite divides_nat2Zp with (Hp := H2).
(* Goal: @eq (Z_Zp (Init.Nat.mul ak ak)) (@nat2Zp (Init.Nat.mul ak ak) H2 am) (@Zp_zero (Init.Nat.mul ak ak) H2) *)
      rewrite Hq2, nat2Zp_mult. 
(* Goal: @eq (Z_Zp (Init.Nat.mul ak ak)) (@Zp_mult (Init.Nat.mul ak ak) H2 (@Zp_mult (Init.Nat.mul ak ak) H2 q (@Zp_mult (Init.Nat.mul ak ak) H2 (@nat2Zp (Init.Nat.mul ak ak) H2 g) (@nat2Zp (Init.Nat.mul ak ak) H2 ak))) (@nat2Zp (Init.Nat.mul ak ak) H2 ak)) (@Zp_zero (Init.Nat.mul ak ak) H2) *)
      do 2 rewrite <- Zp_mult_assoc.
(* Goal: @eq (Z_Zp (Init.Nat.mul ak ak)) (@Zp_mult (Init.Nat.mul ak ak) H2 q (@Zp_mult (Init.Nat.mul ak ak) H2 (@nat2Zp (Init.Nat.mul ak ak) H2 g) (@Zp_mult (Init.Nat.mul ak ak) H2 (@nat2Zp (Init.Nat.mul ak ak) H2 ak) (@nat2Zp (Init.Nat.mul ak ak) H2 ak)))) (@Zp_zero (Init.Nat.mul ak ak) H2) *)
      rewrite <- nat2Zp_mult, nat2Zp_p.
(* Goal: @eq (Z_Zp (Init.Nat.mul ak ak)) (@Zp_mult (Init.Nat.mul ak ak) H2 q (@Zp_mult (Init.Nat.mul ak ak) H2 (@nat2Zp (Init.Nat.mul ak ak) H2 g) (@Zp_zero (Init.Nat.mul ak ak) H2))) (@Zp_zero (Init.Nat.mul ak ak) H2) *)
      do 2 rewrite (Zp_mult_comm _ _ (Zp_zero H2)), Zp_mult_zero.
(* Goal: @eq nat (rem (alpha_nat b1 n) q) (rem (alpha_nat b2 n) q) *)
      trivial.
  Qed.

End Zp_alpha_2.

Local Infix "│" := divides (at level 70, no associativity).

Section Pell.

  Variable (b_nat : nat) (Hb_nat : 2 <= b_nat).

  Fixpoint alpha_nat n := 
    match n with
      | 0   => 0
      | S p => match p with
        | 0   => 1
        | S r => b_nat * alpha_nat p - alpha_nat r
      end
    end.

  Fact alpha_nat_fix_0 : alpha_nat 0 = 0.    Proof. auto. Qed.
  Fact alpha_nat_fix_1 : alpha_nat 1 = 1.    Proof. auto. Qed.

  Fact alpha_nat_fix_2 n : alpha_nat (S (S n)) = b_nat *alpha_nat (S n) - alpha_nat n.
  Proof. auto. Qed.

  Fact alpha_nat_2 n : b_nat = 2 -> alpha_nat n = n.
  Proof.
(* Goal: forall _ : not (@eq nat (Init.Nat.sub b (S (S O))) O), @eq nat (rem (alpha_nat b n) (Init.Nat.sub b (S (S O)))) (rem n (Init.Nat.sub b (S (S O)))) *)
    intros Hb.
(* Goal: @eq (Z_Zp q) (@Z2Zp q Hq (alpha_Z b1 n)) (@Z2Zp q Hq (alpha_Z b2 n)) *)
    induction on n as IHn with measure n.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    destruct n as [ | [ | n ] ]; auto.
(* Goal: @eq nat (alpha_nat (S (S n))) (S (S n)) *)
    rewrite alpha_nat_fix_2, Hb, IHn, IHn; omega.
  Qed.

  Fact alpha_nat_inc n : alpha_nat n < alpha_nat (S n).
  Proof.
(* Goal: lt (alpha_nat n) (alpha_nat (S n)) *)
    induction n as [ | n IHn ]; try (simpl; omega).
(* Goal: @eq Z (Z.of_nat (alpha_nat (S (S n)))) (Z.sub (Z.mul b (alpha_Z (S (S n)))) (alpha_Z (S n))) *)
    rewrite alpha_nat_fix_2.
    replace b_nat with (2+(b_nat-2)) by omega.
(* Goal: lt (Init.Nat.add (Init.Nat.mul (S (S O)) (alpha_nat b m)) (Init.Nat.mul (Init.Nat.sub b (S (S O))) (alpha_nat b (S m)))) (Init.Nat.mul (Init.Nat.add (S (S O)) (Init.Nat.sub b (S (S O)))) (alpha_nat b (S m))) *)
    rewrite Nat.mul_add_distr_r.
(* Goal: lt (alpha_nat (S n)) (Init.Nat.sub (Nat.add (Nat.mul (S (S O)) (alpha_nat (S n))) (Nat.mul (Init.Nat.sub b_nat (S (S O))) (alpha_nat (S n)))) (alpha_nat n)) *)
    generalize ((b_nat -2)*alpha_nat (S n)); intro; omega.
  Qed.

  Corollary alpha_nat_mono i j : i <= j -> alpha_nat i <= alpha_nat j.
  Proof.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    induction 1 as [ | j H1 H2 ]; auto.
(* Goal: le (alpha_nat i) (alpha_nat (S j)) *)
    apply le_trans with (1 := H2), lt_le_weak, alpha_nat_inc.
  Qed.

  Corollary alpha_nat_smono i j : i < j -> alpha_nat i < alpha_nat j.
  Proof.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    intros; apply lt_le_trans with (1 := alpha_nat_inc _), alpha_nat_mono; auto.
  Qed.

  Fact alpha_nat_ge_n n : n <= alpha_nat n.
  Proof.
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@MU22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) n B)) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
    induction n as [ | n IHn ].
    + simpl; auto.
    + apply le_n_S in IHn.
(* Goal: le (S n) (alpha_nat (S n)) *)
      apply le_trans with (1 := IHn), alpha_nat_inc.
  Qed.

  Fact alpha_nat_gt_0 : forall n, n <> 0 -> alpha_nat n <> 0.
  Proof.
(* Goal: forall (n : nat) (_ : not (@eq nat n O)), not (@eq nat (alpha_nat n) O) *)
    intros [ | n ] H; try omega.
(* Goal: not (@eq nat (alpha_nat (S n)) O) *)
    generalize (alpha_nat_ge_n (S n)); omega.
  Qed.

  Fact alpha_nat_le n : alpha_nat n <= b_nat * alpha_nat (S n).
  Proof.
(* Goal: le (alpha_nat n) (Init.Nat.mul b_nat (alpha_nat (S n))) *)
    apply le_trans with (1*alpha_nat (S n)).
    + apply lt_le_weak, lt_le_trans with (1 := alpha_nat_inc _); omega.
    + apply mult_le_compat; omega.
  Qed.

  Notation power := (mscal mult 1).

  Fact alpha_nat_power n : 2 < b_nat -> power n (b_nat-1) <= alpha_nat (S n) <= power n b_nat.
  Proof.
(* Goal: forall _ : not (@eq nat (Init.Nat.sub b (S (S O))) O), @eq nat (rem (alpha_nat b n) (Init.Nat.sub b (S (S O)))) (rem n (Init.Nat.sub b (S (S O)))) *)
    intros Hb.
(* Goal: @eq (Z_Zp q) (@Z2Zp q Hq (alpha_Z b1 n)) (@Z2Zp q Hq (alpha_Z b2 n)) *)
    induction on n as IHn with measure n.
(* Goal: and (le (@mscal nat Init.Nat.mul (S O) n (Init.Nat.sub b_nat (S O))) (alpha_nat (S n))) (le (alpha_nat (S n)) (@mscal nat Init.Nat.mul (S O) n b_nat)) *)
    destruct n as [ | [ | n ] ].
    + do 2 rewrite power_0; simpl; omega.
    + do 2 rewrite power_1; simpl; omega.
    + rewrite alpha_nat_fix_2.
(* Goal: and (le (@mscal nat Init.Nat.mul (S O) (S (S n)) (Init.Nat.sub b_nat (S O))) (Init.Nat.sub (Init.Nat.mul b_nat (alpha_nat (S (S n)))) (alpha_nat (S n)))) (le (Init.Nat.sub (Init.Nat.mul b_nat (alpha_nat (S (S n)))) (alpha_nat (S n))) (@mscal nat Init.Nat.mul (S O) (S (S n)) b_nat)) *)
      destruct (IHn n) as (H1 & H2); try omega.
(* Goal: and (le (@mscal nat Init.Nat.mul (S O) (S (S n)) (Init.Nat.sub b_nat (S O))) (Init.Nat.sub (Init.Nat.mul b_nat (alpha_nat (S (S n)))) (alpha_nat (S n)))) (le (Init.Nat.sub (Init.Nat.mul b_nat (alpha_nat (S (S n)))) (alpha_nat (S n))) (@mscal nat Init.Nat.mul (S O) (S (S n)) b_nat)) *)
      destruct (IHn (S n)) as (H3 & H4); try omega.
(* Goal: iff (and (lt (S (S (S O))) b) (@eq nat a (alpha_nat b c))) (@ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y))))))))) *)
      split.
      * replace b_nat with (b_nat-1+1) at 2 by omega.
(* Goal: lt (Init.Nat.add (Init.Nat.mul (S (S O)) (alpha_nat b m)) (Init.Nat.mul (Init.Nat.sub b (S (S O))) (alpha_nat b (S m)))) (Init.Nat.mul (Init.Nat.add (S (S O)) (Init.Nat.sub b (S (S O)))) (alpha_nat b (S m))) *)
        rewrite Nat.mul_add_distr_r.
        rewrite <- (Nat.add_sub_assoc (_*_)).
        - apply le_trans with (2 := le_plus_l _ _).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
          rewrite power_S; apply mult_le_compat; auto.
        - rewrite Nat.mul_1_l; apply alpha_nat_mono; omega.
      * rewrite power_S.
(* Goal: le (Init.Nat.sub (Init.Nat.mul b_nat (alpha_nat (S (S n)))) (alpha_nat (S n))) (Init.Nat.mul b_nat (@mscal nat Init.Nat.mul (S O) (S n) b_nat)) *)
        apply le_trans with (1 := Nat.le_sub_l _ _).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        apply mult_le_compat_l; auto.
  Qed.

  Open Scope Z.

  Let b := Z.of_nat b_nat.
  Let Hb : 2 <= b.
  Proof. apply inj_le with (n:= 2%nat); omega. Qed. 

  Definition alpha_Z n := 
    match n with 
      | 0%nat => -1
      | S n   => Z.of_nat (alpha_nat n)
    end.

  Notation α := alpha_Z.

  Hint Resolve alpha_nat_le.

  Fact alpha_Z_S n : α (S n) = Z.of_nat (alpha_nat n). Proof. auto. Qed.

  Fact alpha_fix_0 : α (0%nat) = -1.     Proof. auto. Qed.
  Fact alpha_fix_1 : α 1%nat = 0.        Proof. auto. Qed.
  Fact alpha_fix_2 : α 2%nat = 1.        Proof. auto. Qed.
  Fact alpha_fix_3 n : α (S (S n)) = b*α (S n) - α n.
  Proof.
(* Goal: @eq Z (alpha_Z (S (S n))) (Z.sub (Z.mul b (alpha_Z (S n))) (alpha_Z n)) *)
    destruct n as [ | n ].
(* Goal: @eq Z (alpha_Z (S (S O))) (Z.sub (Z.mul b (alpha_Z (S O))) (alpha_Z O)) *)
(* Goal: @eq Z (alpha_Z (S (S (S n)))) (Z.sub (Z.mul b (alpha_Z (S (S n)))) (alpha_Z (S n))) *)
    1: simpl; ring.
(* Goal: @eq Z (alpha_Z (S (S (S n)))) (Z.sub (Z.mul b (alpha_Z (S (S n)))) (alpha_Z (S n))) *)
    unfold α at 1.
(* Goal: @eq Z (Z.of_nat (alpha_nat (S (S n)))) (Z.sub (Z.mul b (alpha_Z (S (S n)))) (alpha_Z (S n))) *)
    rewrite alpha_nat_fix_2.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite Nat2Z.inj_sub; auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite Nat2Z.inj_mul; auto.
  Qed.
 
  Fact alpha_inc n : α n < α (S n).
  Proof. 
(* Goal: Z.lt (alpha_Z n) (alpha_Z (S n)) *)
    destruct n; simpl; try omega.
(* Goal: Z.lt (Z.of_nat (alpha_nat n)) (Z.of_nat match n with | O => S O | S r => Init.Nat.sub (Init.Nat.mul b_nat (alpha_nat n)) (alpha_nat r) end) *)
    apply inj_lt, alpha_nat_inc. 
  Qed.

  Fact alpha_ge_0 n : 0 <= α (S n).
  Proof.
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@MU22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) n B)) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
    induction n as [ | n IHn ].
    + rewrite alpha_fix_1; omega.
    + apply Zlt_le_weak, Z.le_lt_trans with (2 := alpha_inc _); trivial.
  Qed.

  Opaque α.

  Create HintDb alpha_db.

  Hint Rewrite alpha_fix_0 alpha_fix_1 alpha_fix_2 alpha_fix_3 : alpha_db.

  Ltac alpha := autorewrite with alpha_db.

  Fact alpha_2 : b = 2 -> forall n, α n = Z.of_nat n - 1.
  Proof.
(* Goal: forall (_ : @eq Z b (Zpos (xO xH))) (n : nat), @eq Z (alpha_Z n) (Z.sub (Z.of_nat n) (Zpos xH)) *)
    intros H n.
(* Goal: @eq (Z_Zp q) (@Z2Zp q Hq (alpha_Z b1 n)) (@Z2Zp q Hq (alpha_Z b2 n)) *)
    induction on n as IHn with measure n.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    destruct n as [ | [ | n ] ]; alpha; auto.
(* Goal: @eq Z (Z.sub (Z.mul b (alpha_Z (S n))) (alpha_Z n)) (Z.sub (Z.of_nat (S (S n))) (Zpos xH)) *)
    do 2 (rewrite IHn; try omega).
(* Goal: @eq Z (Z.sub (Z.mul b (Z.sub (Z.of_nat (S n)) (Zpos xH))) (Z.sub (Z.of_nat n) (Zpos xH))) (Z.sub (Z.of_nat (S (S n))) (Zpos xH)) *)
    repeat rewrite Nat2Z.inj_succ.
(* Goal: @eq Z (Z.sub (Z.mul b (Z.sub (Z.succ (Z.of_nat n)) (Zpos xH))) (Z.sub (Z.of_nat n) (Zpos xH))) (Z.sub (Z.succ (Z.succ (Z.of_nat n))) (Zpos xH)) *)
    rewrite H; omega.
  Qed.

  Notation MZ := (M22 Z).

  Notation MZ_opp := (MI22 Z.opp).
  Notation MZ_plus := (PL22 Zplus).
  Notation MZ_mult := (MU22 Zplus Zmult).
  Notation MZ_zero := (ZE_22 0).
  Notation MZ_one := (ID_22 0 1).
  Notation MZ_scal := (M22scal Zmult).
  Notation MZ_expo := (mscal MZ_mult MZ_one).
  Notation MZ_det := (Det22 Zplus Zmult Z.opp).

  Local Fact MZ_plus_monoid : monoid_theory MZ_plus MZ_zero.
  Proof. apply M22plus_monoid with (1 := Zring). Qed.
 
  Local Fact MZ_mult_monoid : monoid_theory MZ_mult MZ_one.
  Proof. apply M22mult_monoid with (1 := Zring). Qed.

  Hint Resolve MZ_plus_monoid MZ_mult_monoid.
 
  (* ⊕  ⊗  ∸    ⊞ ⊠ ⊟ *)

  Notation "⊟" := MZ_opp.
  Infix "⊞" := MZ_plus (at level 50, left associativity).
  Infix "⊠" := MZ_mult (at level 40, left associativity).

  Definition B  : MZ := (b,-1,1,0).
  Definition iB : MZ := (0,1,-1,b).

  Definition A n := (α (2+n),-α(1+n),α(1+n),-α n).
  Definition iA n := (-α n,α(1+n),-α(1+n),α (2+n)).

  (* b -1   *   0  1 
     1  0      -1  b  *)

  Notation mI := (-1,0,0,-1).

  (* B has an inverse *)

  Fact B_iB : B ⊠ iB = MZ_one.
  Proof. apply M22_equal; ring. Qed.

  Fact iB_i : iB ⊠ B = MZ_one.
  Proof. apply M22_equal; ring. Qed.

  Fact A_is_sum k : A k = MZ_scal (-α k) MZ_one ⊞ MZ_scal (α (S k)) B.
  Proof.
(* Goal: @eq (prod (prod (prod Z Z) Z) Z) (A k) (@PL22 Z Z.add (@M22scal Z Z.mul (Z.opp (alpha_Z k)) (@ID_22 Z Z0 (Zpos xH))) (@M22scal Z Z.mul (alpha_Z (S k)) B)) *)
    simpl; apply M22_equal; simpl; alpha; ring.
  Qed.

  Lemma MZ_expo_A n : MZ_expo n B = A n.
  Proof.
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@MU22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) n B)) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
    induction n as [ | n IHn ].
    + rewrite mscal_0.
(* Goal: @eq (M22 Z) (@ID_22 Z Z0 (Zpos xH)) (A O) *)
      unfold B, A; simpl.
(* Goal: @eq (M22 Z) (@ID_22 Z Z0 (Zpos xH)) (@pair (prod (prod Z Z) Z) Z (@pair (prod Z Z) Z (@pair Z Z (alpha_Z (S (S O))) (Z.opp (alpha_Z (S O)))) (alpha_Z (S O))) (Z.opp (alpha_Z O))) *)
      rewrite alpha_fix_2, alpha_fix_1, alpha_fix_0.
(* Goal: @eq (M22 Z) (@ID_22 Z Z0 (Zpos xH)) (@pair (prod (prod Z Z) Z) Z (@pair (prod Z Z) Z (@pair Z Z (Zpos xH) (Z.opp Z0)) Z0) (Z.opp (Zneg xH))) *)
      apply M22_equal; ring.
    + rewrite mscal_S, IHn.
(* Goal: @eq (M22 Z) (@MU22 Z Z.add Z.mul B (A n)) (A (S n)) *)
      apply M22_equal; simpl plus; try ring.
      - rewrite alpha_fix_3 with (n := S _); ring.
      - alpha; ring.
  Qed.

  Hint Resolve MZ_expo_A.

  Fact A_plus u v : A (u+v)%nat = A u ⊠ A v.
  Proof.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite <- MZ_expo_A, mscal_plus; auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    f_equal; auto.
  Qed.

  Fact A_mult u v : A (u*v)%nat = MZ_expo u (A v).
  Proof.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite <- MZ_expo_A, mscal_mult; auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    f_equal; auto.
  Qed.

  Fact A_plus_mult m n k l : 
          (m = n + l * k)%nat 
       -> A m = A n ⊠ MZ_expo l (A k).
  Proof.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    intro; subst; rewrite A_plus, A_mult; auto.
  Qed.

  Fact MZ_det_B : MZ_det B = 1.
  Proof. simpl; ring. Qed.

  Lemma MZ_det_A n : MZ_det (A n) = 1.
  Proof. 
    rewrite <- MZ_expo_A.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite Det22_expo with (Rminus := Z.sub); auto. 
(* Goal: @eq Z (@mscal Z Z.mul (Zpos xH) n (@Det22 Z Z.add Z.mul Z.opp B)) (Zpos xH) *)
    rewrite MZ_det_B.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite mscal_of_unit; auto.
  Qed.

  Definition Pell x y := x*x -b*x*y+y*y=1.

  Theorem alpha_Pell n : Pell (α (S n)) (α n).
  Proof.
(* Goal: not (Pell x x) *)
    unfold Pell.
(* Goal: @eq Z (Z.add (Z.sub (Z.mul (alpha_Z (S n)) (alpha_Z (S n))) (Z.mul (Z.mul b (alpha_Z (S n))) (alpha_Z n))) (Z.mul (alpha_Z n) (alpha_Z n))) (Zpos xH) *)
    generalize (MZ_det_A n).
(* Goal: forall _ : @eq Z (@Det22 Z Z.add Z.mul Z.opp (A n)) (Zpos xH), @eq Z (Z.add (Z.sub (Z.mul (alpha_Z (S n)) (alpha_Z (S n))) (Z.mul (Z.mul b (alpha_Z (S n))) (alpha_Z n))) (Z.mul (alpha_Z n) (alpha_Z n))) (Zpos xH) *)
    unfold A; simpl; intros H.
    rewrite <- H.
(* Goal: @eq Z (Z.add (Z.sub (Z.mul (alpha_Z (S n)) (alpha_Z (S n))) (Z.mul (Z.mul b (alpha_Z (S n))) (alpha_Z n))) (Z.mul (alpha_Z n) (alpha_Z n))) (Z.add (Z.mul (alpha_Z (S (S n))) (Z.opp (alpha_Z n))) (Z.opp (Z.mul (Z.opp (alpha_Z (S n))) (alpha_Z (S n))))) *)
    rewrite alpha_fix_3; ring.
  Qed.

  Fact A_iA n : A n ⊠ iA n = MZ_one.
  Proof.
(* Goal: @ex Z (fun u : Z => @ex Z (fun v : Z => @eq Z (Z.add (Z.mul u (alpha_Z (S n))) (Z.mul v (alpha_Z n))) (Zpos xH))) *)
    generalize (alpha_Pell n); unfold Pell; intros H. 
    apply M22_equal; try ring; simpl; rewrite alpha_fix_3, <- H; ring.
  Qed.

  Fact iA_A n : iA n ⊠ A n = MZ_one.
  Proof.
(* Goal: @ex Z (fun u : Z => @ex Z (fun v : Z => @eq Z (Z.add (Z.mul u (alpha_Z (S n))) (Z.mul v (alpha_Z n))) (Zpos xH))) *)
    generalize (alpha_Pell n); unfold Pell; intros H. 
    apply M22_equal; try ring; simpl; rewrite alpha_fix_3, <- H; ring.
  Qed.

  Fact A_minus u v : (v <= u)%nat -> A (u-v)%nat = A u ⊠ iA v.
  Proof.
(* Goal: not (@eq nat (Init.Nat.mul (Init.Nat.mul (S (S O)) l) q) O) *)
    intros H.
    rewrite <- (MZ_expo_A u).
    replace u with (u-v+v)%nat at 2 by omega.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite mscal_plus; auto.
(* Goal: @eq (prod (prod (prod Z Z) Z) Z) (A (Init.Nat.sub u v)) (@MU22 Z Z.add Z.mul (@MU22 Z Z.add Z.mul (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) (Init.Nat.sub u v) B) (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) v B)) (iA v)) *)
    do 2 rewrite MZ_expo_A.
    rewrite <- M22mult_assoc with (1 := Zring).
(* Goal: @eq (prod (prod (prod Z Z) Z) Z) (A (Init.Nat.sub u v)) (@MU22 Z Z.add Z.mul (A (Init.Nat.sub u v)) (@MU22 Z Z.add Z.mul (A v) (iA v))) *)
    rewrite A_iA.
(* Goal: @eq (prod (prod (prod Z Z) Z) Z) (A (Init.Nat.sub u v)) (@MU22 Z Z.add Z.mul (A (Init.Nat.sub u v)) (@ID_22 Z Z0 (Zpos xH))) *)
    rewrite M22mult_one_r with (1 := Zring).
(* Goal: @eq nat (rem (alpha_nat b1 n) q) (rem (alpha_nat b2 n) q) *)
    trivial.
  Qed.

  Section alpha_nat_coprime. 

    Let A_eq_3_12 n : exists u v, u*α (S n) + v*α n = 1.
    Proof.
(* Goal: @ex Z (fun u : Z => @ex Z (fun v : Z => @eq Z (Z.add (Z.mul u (alpha_Z (S n))) (Z.mul v (alpha_Z n))) (Zpos xH))) *)
      generalize (alpha_Pell n); unfold Pell; intros H. 
      exists (α (S n)-b*α n), (α n).
      rewrite <- H; ring.
    Qed.

    Lemma alpha_nat_coprime n : is_gcd (alpha_nat (S n)) (alpha_nat n) 1.
    Proof. apply Z_coprime, (A_eq_3_12 (S n)). Qed.

    Corollary alpha_nat_odd n : (rem (alpha_nat (S n)) 2 = 1 \/ rem (alpha_nat n) 2 = 1)%nat.
    Proof.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      destruct rem_2_is_0_or_1 with (x := alpha_nat (S n)) as [ H1 | ]; auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      destruct rem_2_is_0_or_1 with (x := alpha_nat n) as [ H2 | ]; auto.
(* Goal: or (@eq nat (rem (alpha_nat (S n)) (S (S O))) (S O)) (@eq nat (rem (alpha_nat n) (S (S O))) (S O)) *)
      exfalso; generalize (alpha_nat_coprime n); intros (_ & _ & H3).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      destruct (H3 2%nat) as (? & ?); try omega; apply divides_rem_eq; auto.
    Qed.  

  End alpha_nat_coprime.

  Theorem find_odd_alpha u : exists n, (u <= alpha_nat (S n) /\ rem (alpha_nat (S n)) 2 = 1)%nat.
  Proof.
    destruct (alpha_nat_odd (S u)) as [ H | H ]; [ exists (S u) | exists u ]; split; auto;
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      apply le_trans with (1 := alpha_nat_ge_n _), alpha_nat_mono; omega.
  Qed.

  Theorem find_odd_alpha' u : exists n, (u <= alpha_nat n /\ rem (alpha_nat n) 2 = 1)%nat.
  Proof.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    destruct (find_odd_alpha u) as (n & ?); exists (S n); auto.
  Qed.

  (* ⊕  ⊗  ∸    ⊞ ⊠ ⊟ *)
 
  Notation expoZ := (mscal Zmult 1).

  Fact expoZ_power n x : expoZ n (Z.of_nat x) = Z.of_nat (power n x).
  Proof. 
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    symmetry; apply mscal_morph; auto.
(* Goal: forall x y : nat, @eq Z (Z.of_nat (Init.Nat.mul x y)) (Z.mul (Z.of_nat x) (Z.of_nat y)) *)
    intros; apply Nat2Z.inj_mul.
  Qed.

  Fact mscal_Zplus n : mscal Zplus 0 n 1 = Z.of_nat n.
  Proof.
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@MU22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) n B)) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
    induction n as [ | n IHn ].
    + rewrite mscal_0; auto.
    + rewrite mscal_S, IHn.
(* Goal: @eq Z (Z.add (Zpos xH) (Z.of_nat n)) (Z.of_nat (S n)) *)
      rewrite Nat2Z.inj_succ; ring.
  Qed.

  Notation "∑" := (msum MZ_plus MZ_zero).

  Theorem MA_expo_A_binomial m k l :
          (m = l * k)%nat 
       -> A m = ∑ (S l) (fun i => MZ_scal ( expoZ (l-i) (-1)
                                          * Z.of_nat (binomial l i) 
                                          * expoZ i (α (S k)) 
                                          * expoZ (l-i) (α k) ) 
                                     (MZ_expo i B)).
  Proof.
(* Goal: divides (Init.Nat.mul ak ak) am *)
    intro; subst m.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite <- MZ_expo_A, mscal_mult, MZ_expo_A, A_is_sum; auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite binomial_Newton with (zero := MZ_zero); auto.
    2: apply M22plus_comm with (1 := Zring).
    2: apply M22plus_cancel with (1 := Zring).
    2: apply M22_mult_distr_l with (1 := Zring).
    2: apply M22_mult_distr_r with (1 := Zring).
(* Goal: @eq (M22 Z) (@ID_22 Z Z0 (Zpos xH)) (@pair (prod (prod Z Z) Z) Z (@pair (prod Z Z) Z (@pair Z Z (Zpos xH) (Z.opp Z0)) Z0) (Z.opp (Zneg xH))) *)
    2: apply M22_equal; ring.
    apply msum_ext; intros i Hi.
    repeat rewrite expo22_scal with (1 := Zring).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite  mscal_of_unit; auto.
    rewrite <- M22scal_MU22_l with (1 := Zring).
    rewrite <- M22scal_MU22_r with (1 := Zring).
    rewrite M22mult_one_l with (1 := Zring).
    rewrite M22scal_mult with (1 := Zring).
    rewrite mscal_M22scal with (1 := Zring).
    rewrite M22scal_mult with (1 := Zring).
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.mul (Z.of_nat q) (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
    f_equal.
    rewrite mscal_Zplus.
    replace (- α k) with ((-1)*α k) by ring.
    rewrite mscal_sum; auto; ring.
  Qed.

  Section A2m.

    Variable (l m v : nat) (Hv : Z.of_nat v = α (2+m) - α m).

    Fact alpha_SSm_m_neq_0 : v <> 0%nat.
    Proof. 
(* Goal: not (@eq nat v O) *)
      intros H; subst; simpl in Hv.
(* Goal: False *)
      generalize (alpha_inc m) (alpha_inc (S m)); omega.
    Qed.

    Notation Hv' := alpha_SSm_m_neq_0.

    Let Z2Zp_morph := Z2Zp_morphishm Hv'.

    Notation f := (Z2Zp Hv').
    Notation "〚 x 〛" :=  (f x).
    Notation "〘 x 〙" := (morph22 f x).
    Notation "⊟" := (MI22 (Zp_opp Hv')).
    Infix "⊠" := (MU22 (Zp_plus Hv') (Zp_mult Hv')) (at level 40, left associativity).

    Let Am_iAm_mod :〘A m〙= ⊟〘iA m〙.
    Proof.
(* Goal: @eq (M22 (Z_Zp v)) (@morph22 Z (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0) (A m)) (@MI22 (Z_Zp v) (@Zp_opp v alpha_SSm_m_neq_0) (@morph22 Z (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0) (iA m))) *)
      apply M22_equal.
      + rewrite Z2Zp_opp, Zp_opp_inv.
(* Goal: @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (Z.opp (alpha_Z m))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.opp (alpha_Z (Init.Nat.add (S (S O)) m)))) *)
        apply Z2Zp_inj.
(* Goal: @ex Z (fun i : Z => @eq Z (Z.sub (Z.opp (alpha_Z m)) (Z.opp (alpha_Z (Init.Nat.add (S (S O)) m)))) (Z.mul i (Z.of_nat v))) *)
        exists 1; rewrite Hv; ring.
      + rewrite Z2Zp_opp; auto.
      + rewrite Z2Zp_opp, Zp_opp_inv; auto.
      + rewrite <- Z2Zp_opp.
(* Goal: @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (Z.opp (alpha_Z m))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.opp (alpha_Z (Init.Nat.add (S (S O)) m)))) *)
        apply Z2Zp_inj.
(* Goal: @ex Z (fun i : Z => @eq Z (Z.sub (Z.opp (alpha_Z m)) (Z.opp (alpha_Z (Init.Nat.add (S (S O)) m)))) (Z.mul i (Z.of_nat v))) *)
        exists 1; rewrite Hv; ring.
    Qed.
    
    Fact A2m_mod : 〘A (2*m)〙= ⊟〘MZ_one〙.
    Proof.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      rewrite <- MZ_expo_A, mscal_mult, MZ_expo_A; auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      rewrite mscal_S, mscal_1; auto.
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@MU22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@MU22 Z Z.add Z.mul (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) n B) B)) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) (S n) (Z.of_nat q))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
      rewrite MU22_morph with (1 := Z2Zp_morph).
(* Goal: @eq (M22 (Z_Zp v)) (@MU22 (Z_Zp v) (@Zp_plus v alpha_SSm_m_neq_0) (@Zp_mult v alpha_SSm_m_neq_0) (@morph22 Z (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0) (A m)) (@morph22 Z (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0) (A m))) (@MI22 (Z_Zp v) (@Zp_opp v alpha_SSm_m_neq_0) (@morph22 Z (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0) (@ID_22 Z Z0 (Zpos xH)))) *)
      rewrite Am_iAm_mod at 1.
      do 2 rewrite <- MI22_morph with (1 := Z2Zp_morph).
      rewrite <- MU22_morph with (1 := Z2Zp_morph).
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.mul (Z.of_nat q) (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
      f_equal.
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.mul (Z.of_nat q) (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
      rewrite M22_opp_mult_l with (1 := Zring); f_equal.
(* Goal: @eq (M22 Z) (@MU22 Z Z.add Z.mul (iA m) (A m)) (@ID_22 Z Z0 (Zpos xH)) *)
      apply iA_A.
    Qed.

    Fact A2lm_mod : 〘A (2*l*m)〙= 〘MZ_scal (mscal Zmult 1 l (-1)) MZ_one〙.
    Proof.
      replace (2*l*m)%nat with (l*(2*m))%nat by ring.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      rewrite <- MZ_expo_A, mscal_mult, MZ_expo_A; auto.
(* Goal: @eq (M22 (Z_Zp v)) (@morph22 Z (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0) (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) l (A (Init.Nat.mul (S (S O)) m)))) (@morph22 Z (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0) (@M22scal Z Z.mul (@mscal Z Z.mul (Zpos xH) l (Zneg xH)) (@ID_22 Z Z0 (Zpos xH)))) *)
      rewrite expo22_morph with (1 := Z2Zp_morph).
(* Goal: @eq (M22 (Z_Zp v)) (@mscal (M22 (Z_Zp v)) (@MU22 (Z_Zp v) (@Zp_plus v alpha_SSm_m_neq_0) (@Zp_mult v alpha_SSm_m_neq_0)) (@ID_22 (Z_Zp v) (@Zp_zero v alpha_SSm_m_neq_0) (@Zp_one v alpha_SSm_m_neq_0)) l (@morph22 Z (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0) (A (Init.Nat.mul (S (S O)) m)))) (@morph22 Z (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0) (@M22scal Z Z.mul (@mscal Z Z.mul (Zpos xH) l (Zneg xH)) (@ID_22 Z Z0 (Zpos xH)))) *)
      rewrite A2m_mod.
      rewrite <- MI22_morph with (1 := Z2Zp_morph).
      rewrite <- expo22_morph with (1 := Z2Zp_morph).
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.mul (Z.of_nat q) (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
      f_equal.
      rewrite <- M22scal_MI22 with (1 := Zring).
      change (-(1)) with (-1).
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.mul (Z.of_nat q) (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
      rewrite expo22_scal with (1 := Zring); f_equal.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      rewrite mscal_of_unit; auto.
    Qed.

    Let expoZ_opp1 i : expoZ i (-1) = 1 \/ expoZ i (-1) = -1.
    Proof.
(* Goal: or (@eq Z (@mscal Z Z.mul (Zpos xH) i (Zneg xH)) (Zpos xH)) (@eq Z (@mscal Z Z.mul (Zpos xH) i (Zneg xH)) (Zneg xH)) *)
      induction i as [ | i IHi ].
      + rewrite mscal_0; auto.
      + rewrite mscal_S; omega.
    Qed. 

    Variable (j : nat) (Hl : (l <> 0)%nat) (Hj : (j <= m)%nat).

    Fact alpha_2lm_plus_j :〚α (S (2*l*m+j))〛=〚expoZ l (-1)*α (S j)〛.
    Proof.
      generalize (A_plus (2*l*m) j); intros H.
(* Goal: @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (alpha_Z (S (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j)))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.mul (@mscal Z Z.mul (Zpos xH) (S l) (Zneg xH)) (alpha_Z (S j)))) *)
      apply f_equal with (f := morph22 f) in H.
(* Goal: @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (alpha_Z (S (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j)))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.mul (@mscal Z Z.mul (Zpos xH) (S l) (Zneg xH)) (alpha_Z (S j)))) *)
      rewrite MU22_morph with (1 := Z2Zp_morph) in H.
(* Goal: @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (alpha_Z (S (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j)))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.mul (@mscal Z Z.mul (Zpos xH) (S l) (Zneg xH)) (alpha_Z (S j)))) *)
      rewrite A2lm_mod in H.
      rewrite <- MU22_morph with (1 := Z2Zp_morph) in H.
(* Goal: @eq (Z_Zp ak2) (@Z2Zp ak2 Hak2 (alpha_Z b (S m))) (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (Zneg xH))) (@nat2Zp ak2 Hak2 l)) (@Z2Zp ak2 Hak2 (alpha_Z b (S k)))) (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (alpha_Z b k)))) *)
      apply M22_proj12 in H.
(* Goal: @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (alpha_Z (S (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j)))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.mul (@mscal Z Z.mul (Zpos xH) (S l) (Zneg xH)) (alpha_Z (S j)))) *)
      rewrite Z.mul_0_r, Z.mul_0_l, Z.mul_1_r, Z.add_0_l in H.
(* Goal: @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (alpha_Z (S (Init.Nat.add (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j)))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.mul (@mscal Z Z.mul (Zpos xH) l (Zneg xH)) (alpha_Z (S j)))) *)
      apply H.
    Qed.

    Let Hj' : (j <= 2*l*m)%nat.
    Proof.
      apply le_trans with (1*m)%nat; try omega.
(* Goal: le (Nat.mul u (S O)) (Init.Nat.mul u k) *)
      apply mult_le_compat; omega.
    Qed.

    Fact alpha_2lm_minus_j :〚α (S (2*l*m-j))〛=〚expoZ (S l) (-1)*α (S j)〛.
    Proof.
(* Goal: not (@eq nat (Init.Nat.mul (Init.Nat.mul (S (S O)) l) q) O) *)
      generalize (A_minus Hj'); intros H.
(* Goal: @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (alpha_Z (S (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j)))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.mul (@mscal Z Z.mul (Zpos xH) (S l) (Zneg xH)) (alpha_Z (S j)))) *)
      apply f_equal with (f := morph22 f) in H.
(* Goal: @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (alpha_Z (S (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j)))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.mul (@mscal Z Z.mul (Zpos xH) (S l) (Zneg xH)) (alpha_Z (S j)))) *)
      rewrite MU22_morph with (1 := Z2Zp_morph) in H.
(* Goal: @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (alpha_Z (S (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j)))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.mul (@mscal Z Z.mul (Zpos xH) (S l) (Zneg xH)) (alpha_Z (S j)))) *)
      rewrite A2lm_mod in H.
      rewrite <- MU22_morph with (1 := Z2Zp_morph) in H.
(* Goal: @eq (Z_Zp ak2) (@Z2Zp ak2 Hak2 (alpha_Z b (S m))) (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (Zneg xH))) (@nat2Zp ak2 Hak2 l)) (@Z2Zp ak2 Hak2 (alpha_Z b (S k)))) (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (alpha_Z b k)))) *)
      apply M22_proj12 in H.
(* Goal: @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (alpha_Z (S (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j)))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.mul (@mscal Z Z.mul (Zpos xH) (S l) (Zneg xH)) (alpha_Z (S j)))) *)
      rewrite Z.mul_0_r, Z.mul_0_l, Z.mul_1_r, Z.add_0_l in H.
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.mul (Z.of_nat q) (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
      unfold plus in H; rewrite H; f_equal.
(* Goal: @eq Z (Z.mul (@mscal Z Z.mul (Zpos xH) l (Zneg xH)) (Z.opp (alpha_Z (S j)))) (Z.mul (@mscal Z Z.mul (Zpos xH) (S l) (Zneg xH)) (alpha_Z (S j))) *)
      rewrite mscal_S; ring.
    Qed.
  
    Theorem alpha_nat_2lm_plus_j : nat2Zp Hv' (alpha_nat (2*l*m+j)) = nat2Zp Hv' (alpha_nat j)
                                \/ nat2Zp Hv' (alpha_nat (2*l*m+j)) = Zp_opp Hv' (nat2Zp Hv' (alpha_nat j)).
    Proof.
(* Goal: or (@eq (Z_Zp v) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat (Init.Nat.add (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j))) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat j))) (@eq (Z_Zp v) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat (Init.Nat.add (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j))) (@Zp_opp v alpha_SSm_m_neq_0 (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat j)))) *)
      generalize (alpha_2lm_plus_j).
(* Goal: forall _ : @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (alpha_Z (S (Init.Nat.add (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j)))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.mul (@mscal Z Z.mul (Zpos xH) l (Zneg xH)) (alpha_Z (S j)))), or (@eq (Z_Zp v) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat (Init.Nat.add (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j))) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat j))) (@eq (Z_Zp v) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat (Init.Nat.add (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j))) (@Zp_opp v alpha_SSm_m_neq_0 (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat j)))) *)
      destruct (expoZ_opp1 l) as [ E | E ]; rewrite E; intros H; [ left | right ].
      + repeat rewrite alpha_Z_S in H.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        rewrite Z2Zp_of_nat, Z.mul_1_l, Z2Zp_of_nat in H; auto.
      + repeat rewrite alpha_Z_S in H.
        change (-1) with (-(1)) in H.
(* Goal: @eq (Z_Zp v) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j))) (@Zp_opp v alpha_SSm_m_neq_0 (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat j))) *)
        rewrite Z.mul_opp_l, Z.mul_1_l, Z2Zp_opp in H.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        repeat rewrite Z2Zp_of_nat in H; auto.
    Qed.

    Theorem alpha_nat_2lm_minus_j : nat2Zp Hv' (alpha_nat (2*l*m-j)) = nat2Zp Hv' (alpha_nat j)
                                 \/ nat2Zp Hv' (alpha_nat (2*l*m-j)) = Zp_opp Hv' (nat2Zp Hv' (alpha_nat j)).
    Proof.
(* Goal: or (@eq (Z_Zp v) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j))) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat j))) (@eq (Z_Zp v) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j))) (@Zp_opp v alpha_SSm_m_neq_0 (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat j)))) *)
      generalize (alpha_2lm_minus_j).
(* Goal: forall _ : @eq (Z_Zp v) (@Z2Zp v alpha_SSm_m_neq_0 (alpha_Z (S (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j)))) (@Z2Zp v alpha_SSm_m_neq_0 (Z.mul (@mscal Z Z.mul (Zpos xH) (S l) (Zneg xH)) (alpha_Z (S j)))), or (@eq (Z_Zp v) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j))) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat j))) (@eq (Z_Zp v) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j))) (@Zp_opp v alpha_SSm_m_neq_0 (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat j)))) *)
      destruct (expoZ_opp1 (S l)) as [ E | E ]; rewrite E; intros H; [ left | right ].
      + repeat rewrite alpha_Z_S in H.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        rewrite Z2Zp_of_nat, Z.mul_1_l, Z2Zp_of_nat in H; auto.
      + repeat rewrite alpha_Z_S in H.
        change (-1) with (-(1)) in H.
(* Goal: @eq (Z_Zp v) (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) m) j))) (@Zp_opp v alpha_SSm_m_neq_0 (@nat2Zp v alpha_SSm_m_neq_0 (alpha_nat j))) *)
        rewrite Z.mul_opp_l, Z.mul_1_l, Z2Zp_opp in H.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        repeat rewrite Z2Zp_of_nat in H; auto.
    Qed.

  End A2m.

  Section expo_congruence.

    Variable (q : nat). 
   
    Notation m := (b_nat*q-q*q-1)%nat.

    Hypothesis Hm : m <> 0%nat.

    Let Hq : (1+q*q < b_nat*q)%nat.
    Proof. omega. Qed.

    Let VP : MZ := (Z.of_nat q,0,1,0).

    Notation Zm_ring := (Zp_is_ring Hm).

    Local Add Ring m_ring : Zm_ring.

    Notation qz := (Z.of_nat q).

    Let Z2Zp_morph := Z2Zp_morphishm Hm.

    (* ⊕  ⊗  ∸    ⊞ ⊠ ⊟ *)

    Infix "⊕" := (Zp_plus Hm) (at level 50, left associativity).
    Infix "⊗" := (Zp_mult Hm) (at level 40, left associativity).
    Notation "∸" := (Zp_opp Hm).
    Notation f := (Z2Zp Hm).
    Notation "〚 x 〛" :=  (f x).
 
    Let qz_eq :〚b〛⊗〚qz〛 ⊕ ∸ (〚qz〛⊗〚qz〛 ⊕〚1〛) = Zp_zero Hm.
    Proof.
      do 2 rewrite <- Z2Zp_mult.
      rewrite <- Z2Zp_plus, <- Z2Zp_opp, <- Z2Zp_plus.
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.add (Z.mul b (Z.of_nat q)) (Z.opp (Z.add (Z.mul (Z.of_nat q) (Z.of_nat q)) (Zpos xH))))) (@Zp_zero (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) *)
      unfold b.
      do 2 rewrite <- Nat2Z.inj_mul.
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.add (Z.of_nat (Init.Nat.mul b_nat q)) (Z.opp (Z.add (Z.of_nat (Init.Nat.mul q q)) (Zpos xH))))) (@Zp_zero (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) *)
      change 1 with (Z.of_nat 1%nat).
      rewrite Z.add_opp_r, <- Nat2Z.inj_add, <- Nat2Z.inj_sub; try omega.
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.add (Init.Nat.mul q q) (S O))))) (@Zp_zero (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) *)
      rewrite Nat.sub_add_distr; fold m.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      rewrite Z2Zp_of_nat, nat2Zp_p; auto.
    Qed.

    Notation "〘 x 〙" := (morph22 f x).
    Infix "⊠" := (MU22 (Zp_plus Hm) (Zp_mult Hm)) (at level 40, left associativity).
    Notation scal := (M22scal (Zp_mult Hm)).

    Let BVP : 〘 B 〙 ⊠ 〘 VP 〙= scal〚qz〛〘 VP 〙.
    Proof.
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@MU22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) B) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
      apply M22_equal; try rewrite Z2Zp_zero; try ring.
      change (-1) with (-(1)).
      rewrite Z2Zp_opp, Zp_opp_mult, <- (Z2Zp_mult _ 1).
      rewrite Z.mul_1_l, <- (Zp_plus_zero Hm), <- qz_eq.
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm b) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Zp_opp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Zpos xH)))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm b) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Zp_opp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Zpos xH))))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)))) *)
      ring.
    Qed.    

    Let AnVP n :〘 A n 〙 ⊠ 〘 VP 〙= scal〚expoZ n qz〛〘 VP 〙.
    Proof.
      rewrite <- MZ_expo_A.
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@MU22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) n B)) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
      induction n as [ | n IHn ].
      + do 2 rewrite mscal_0.
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm b) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Zp_opp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Zpos xH)))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm b) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Zp_opp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Zpos xH))))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)))) *)
        apply M22_equal; try rewrite Z2Zp_zero; try rewrite Z2Zp_one; ring.
      + rewrite mscal_plus1; auto.
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@MU22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@MU22 Z Z.add Z.mul (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) n B) B)) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) (S n) (Z.of_nat q))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
        rewrite MU22_morph with (1 := Z2Zp_morph).
        rewrite <- M22mult_assoc with (1 := Zm_ring).
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@MU22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) n B)) (@MU22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) B) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP))) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) (S n) (Z.of_nat q))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
        rewrite BVP.
        rewrite <- M22scal_MU22_r with (1 := Zm_ring).
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@MU22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) n B)) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP))) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) (S n) (Z.of_nat q))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
        rewrite IHn, mscal_S, M22scal_mult with (1 := Zm_ring).
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.mul (Z.of_nat q) (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
        f_equal.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        rewrite Z2Zp_mult; auto.
    Qed.

    Theorem expo_congruence_Z n : nat2Zp Hm q ⊗〚α (S n)〛=〚α n〛⊕ nat2Zp Hm (power n q).
    Proof.
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm q) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (alpha_Z (S n)))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (alpha_Z n)) (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal nat Init.Nat.mul (S O) n q))) *)
      destruct Z2Zp_morph as [ G1 G2 G3 G4 G5 ].
(* Goal: not (@eq nat (Init.Nat.mul (Init.Nat.mul (S (S O)) l) q) O) *)
      generalize (AnVP n); intros H.
      apply  M22_proj12 in H.
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm q) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (alpha_Z (S n)))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (alpha_Z n)) (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal nat Init.Nat.mul (S O) n q))) *)
      rewrite Z2Zp_one in H. 
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm q) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (alpha_Z (S n)))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (alpha_Z n)) (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal nat Init.Nat.mul (S O) n q))) *)
      do 2 rewrite Zp_mult_one_r in H.
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm q) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (alpha_Z (S n)))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (alpha_Z n)) (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal nat Init.Nat.mul (S O) n q))) *)
      rewrite expoZ_power in H. 
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm q) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (alpha_Z (S n)))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (alpha_Z n)) (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal nat Init.Nat.mul (S O) n q))) *)
      rewrite (Z2Zp_of_nat _ (power _ _)) in H.
      rewrite <- H, Z2Zp_of_nat. 
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm b) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Zp_opp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Zpos xH)))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm b) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Zp_opp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Zpos xH))))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)))) *)
      simpl plus; rewrite Z2Zp_opp; ring.
    Qed.

    Theorem expo_congruence n : (0 < n)%nat -> nat2Zp Hm (q * alpha_nat n) = nat2Zp Hm (alpha_nat (n-1) + power n q).
    Proof.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      destruct n as [ | n ]; try omega.
      replace (S n-1)%nat with n by omega.
(* Goal: forall _ : lt O (S n), @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Init.Nat.mul q (alpha_nat (S n)))) (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Init.Nat.add (alpha_nat n) (@mscal nat Init.Nat.mul (S O) (S n) q))) *)
      rewrite nat2Zp_mult.
(* Goal: not (@eq nat (Init.Nat.mul (Init.Nat.mul (S (S O)) l) q) O) *)
      generalize (expo_congruence_Z (S n)); intros H.
(* Goal: forall _ : lt O (S n), @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm q) (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (alpha_nat (S n)))) (@nat2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Init.Nat.add (alpha_nat n) (@mscal nat Init.Nat.mul (S O) (S n) q))) *)
      do 2 rewrite alpha_Z_S in H.
(* Goal: @eq (Z_Zp ak2) (@nat2Zp ak2 Hak2 (alpha_nat b m)) (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (Zneg xH))) (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (alpha_Z b k)))) (@nat2Zp ak2 Hak2 l)) (@nat2Zp ak2 Hak2 (alpha_nat b k))) *)
      do 2 rewrite Z2Zp_of_nat in H.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      rewrite H, nat2Zp_plus; auto.
    Qed.

  End expo_congruence.

  Fact Pell_sym x y : Pell x y <-> Pell y x.
  Proof.
    unfold Pell; split; intros H; rewrite <- H; ring.
  Qed.

  Theorem Pell_zero_left y : Pell 0 y <-> y = 1 \/ y = -1.
  Proof.
(* Goal: iff (and (lt (S (S (S O))) b) (@eq nat a (alpha_nat b c))) (@ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y))))))))) *)
    unfold Pell; split.
    + intros H.
      assert ((y-1)*(y+1) = 0) as H1.
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
      { ring_simplify in H.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
        ring_simplify; omega. }
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      apply  Zmult_integral in H1; omega.
    + intros [ | ]; subst; ring.
  Qed.

  Theorem Pell_zero_right x : Pell x 0 <-> x = 1 \/ x = -1.
  Proof.
(* Goal: iff (Pell x Z0) (or (@eq Z x (Zpos xH)) (@eq Z x (Zneg xH))) *)
    rewrite Pell_sym; apply Pell_zero_left.
  Qed.

  Theorem Pell_not_diag x : ~ Pell x x.
  Proof.   
(* Goal: not (Pell x x) *)
    unfold Pell.
(* Goal: not (@eq nat (Init.Nat.mul (Init.Nat.mul (S (S O)) l) q) O) *)
    intros H.
    assert (x*((2-b)*x) = 1) as H1.
    { rewrite <- H; ring. }
(* Goal: False *)
    generalize H1; intros H2.
(* Goal: False *)
    apply Z.eq_mul_1 in H1.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    destruct H1; subst; omega.
  Qed.

  Theorem Pell_opposite_not x y : y < 0 -> 0 < x -> ~ Pell x y.
  Proof.
(* Goal: forall (_ : lt (Init.Nat.mul (S (S O)) a) v) (_ : lt (Init.Nat.mul (S (S O)) b) v) (_ : @eq nat (rem a v) (rem b v)), @eq nat a b *)
    intros H1 H2 H3.
(* Goal: False *)
    red in H3.
    assert (0 <= - (b *x*y)) as H4.
    { replace (-(b*x*y)) with (b*x*-y) by ring.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      apply Z.mul_nonneg_nonneg; try omega.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      apply Z.mul_nonneg_nonneg; omega. }
    assert (0 < x*x) as H5.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    { apply Z.mul_pos_pos; auto. }
    assert (0 < y*y) as H6.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    { apply Z.mul_neg_neg; auto. }
(* Goal: False *)
    revert H3 H4 H5 H6.
    generalize (x*x) (y*y) (b*x*y); intros; omega.
  Qed.

  Theorem Pell_alpha x y : 0 <= y < x -> Pell x y -> { n | x = α (S (S n)) /\ y = α (S n) }.
  Proof.
(* Goal: forall (_ : and (Z.le Z0 y) (Z.lt y x)) (_ : Pell x y), @sig nat (fun n : nat => and (@eq Z x (alpha_Z (S (S n)))) (@eq Z y (alpha_Z (S n)))) *)
    induction on x y as IH with measure (Z.to_nat y); intros Hxy HP.
(* Goal: @sig nat (fun n : nat => and (@eq Z x (alpha_Z (S (S n)))) (@eq Z y (alpha_Z (S n)))) *)
    destruct (Z.eq_dec y 0) as [ Hy | Hy ].
    + exists 0%nat.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      rewrite alpha_fix_1, alpha_fix_2; split; auto.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      subst y; rewrite Pell_zero_right in HP; omega.
    + red in HP.
      assert (x*x = 1 + (b*y)*x - y*y) as H1.
      { rewrite <- HP; ring. }
      assert (0 < y*y) as H2.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      { apply Z.mul_pos_pos; omega. }
      assert (x <= (b*y)) as H3.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      { apply Zmult_le_reg_r with x; [ | rewrite H1 ]; omega. }
      assert (-(y*x) <= - (y*y)) as H4.
      { rewrite <- Z.opp_le_mono.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
        apply Zmult_le_compat; omega. }
      assert (x > b*y-y) as H5.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      { apply Zmult_gt_reg_r with x; try omega.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
        rewrite H1; rewrite Z.mul_sub_distr_r; omega. }
      destruct (IH y (b*y-x)) as (m & G1 & G2); try omega.
      - apply Z2Nat.inj_lt; omega.
      - red; rewrite <- HP; ring.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      - exists (S m); split; auto.
        rewrite alpha_fix_3, <- G2, <- G1; ring.
  Qed.

End Pell.

Theorem alpha_nat_Pell b n : 
    2 <= b -> alpha_nat b (S n)*alpha_nat b (S n) +  alpha_nat b n * alpha_nat b n  
            = 1 + b*(alpha_nat b (S n) * alpha_nat b n).
Proof.
(* Goal: forall _ : not (@eq nat (Init.Nat.sub b (S (S O))) O), @eq nat (rem (alpha_nat b n) (Init.Nat.sub b (S (S O)))) (rem n (Init.Nat.sub b (S (S O)))) *)
  intros Hb.
(* Goal: @eq nat (Init.Nat.add (Init.Nat.mul (alpha_nat b (S n)) (alpha_nat b (S n))) (Init.Nat.mul (alpha_nat b n) (alpha_nat b n))) (Init.Nat.add (S O) (Init.Nat.mul b (Init.Nat.mul (alpha_nat b (S n)) (alpha_nat b n)))) *)
  generalize (alpha_Pell Hb (S n)); intros H; red in H.
(* Goal: @eq nat (Init.Nat.add (Init.Nat.mul (alpha_nat b (S n)) (alpha_nat b (S n))) (Init.Nat.mul (alpha_nat b n) (alpha_nat b n))) (Init.Nat.add (S O) (Init.Nat.mul b (Init.Nat.mul (alpha_nat b (S n)) (alpha_nat b n)))) *)
  unfold alpha_Z in H.
(* Goal: @eq nat (Init.Nat.add (Init.Nat.mul (alpha_nat b (S n)) (alpha_nat b (S n))) (Init.Nat.mul (alpha_nat b n) (alpha_nat b n))) (Init.Nat.add (S O) (Init.Nat.mul b (Init.Nat.mul (alpha_nat b (S n)) (alpha_nat b n)))) *)
  apply Nat2Z.inj.
(* Goal: @eq Z (Z.of_nat (Init.Nat.add (Init.Nat.mul (alpha_nat b (S n)) (alpha_nat b (S n))) (Init.Nat.mul (alpha_nat b n) (alpha_nat b n)))) (Z.of_nat (Init.Nat.add (S O) (Init.Nat.mul b (Init.Nat.mul (alpha_nat b (S n)) (alpha_nat b n))))) *)
  repeat rewrite Nat2Z.inj_add.
(* Goal: @eq Z (Z.add (Z.of_nat (Init.Nat.mul (alpha_nat b (S n)) (alpha_nat b (S n)))) (Z.of_nat (Init.Nat.mul (alpha_nat b n) (alpha_nat b n)))) (Z.add (Z.of_nat (S O)) (Z.of_nat (Init.Nat.mul b (Init.Nat.mul (alpha_nat b (S n)) (alpha_nat b n))))) *)
  repeat rewrite Nat2Z.inj_mul.
(* Goal: @eq Z (Z.add (Z.mul (Z.of_nat (alpha_nat b (S n))) (Z.of_nat (alpha_nat b (S n)))) (Z.mul (Z.of_nat (alpha_nat b n)) (Z.of_nat (alpha_nat b n)))) (Z.add (Z.of_nat (S O)) (Z.mul (Z.of_nat b) (Z.mul (Z.of_nat (alpha_nat b (S n))) (Z.of_nat (alpha_nat b n))))) *)
  change (Z.of_nat 1) with (1%Z).
  rewrite <- H; ring.
Qed.

Theorem alpha_nat_Pell' b n : 
    2 <= b -> alpha_nat b n*alpha_nat b n +  alpha_nat b (S n) * alpha_nat b (S n)  
            = 1 + b*(alpha_nat b n * alpha_nat b (S n)).
Proof.
(* Goal: forall _ : le (S (S O)) b, @eq nat (Init.Nat.add (Init.Nat.mul (alpha_nat b n) (alpha_nat b n)) (Init.Nat.mul (alpha_nat b (S n)) (alpha_nat b (S n)))) (Init.Nat.add (S O) (Init.Nat.mul b (Init.Nat.mul (alpha_nat b n) (alpha_nat b (S n))))) *)
  rewrite plus_comm, (mult_comm (alpha_nat b n) (alpha_nat b (S n))).
(* Goal: forall _ : le (S (S O)) b, @eq nat (Nat.add (Init.Nat.mul (alpha_nat b (S n)) (alpha_nat b (S n))) (Init.Nat.mul (alpha_nat b n) (alpha_nat b n))) (Init.Nat.add (S O) (Init.Nat.mul b (Nat.mul (alpha_nat b (S n)) (alpha_nat b n)))) *)
  apply alpha_nat_Pell.
Qed.

Theorem Pell_alpha_nat b x y : 2 <= b -> y <= x -> x*x+y*y = 1+b*(x*y) -> { n | x = alpha_nat b (S n) /\ y = alpha_nat b n }.
Proof.
(* Goal: forall (_ : le (S (S O)) b) (_ : le y x) (_ : @eq nat (Init.Nat.add (Init.Nat.mul x x) (Init.Nat.mul y y)) (Init.Nat.add (S O) (Init.Nat.mul b (Init.Nat.mul x y)))), @sig nat (fun n : nat => and (@eq nat x (alpha_nat b (S n))) (@eq nat y (alpha_nat b n))) *)
  intros Hb H2 H1.
(* Goal: @sig nat (fun n : nat => and (@eq nat x (alpha_nat b (S n))) (@eq nat y (alpha_nat b n))) *)
  apply f_equal with (f := Z.of_nat) in H1.
(* Goal: @sig nat (fun n : nat => and (@eq nat x (alpha_nat b (S n))) (@eq nat y (alpha_nat b n))) *)
  do 2 rewrite Nat2Z.inj_add in H1.
(* Goal: @sig nat (fun n : nat => and (@eq nat x (alpha_nat b (S n))) (@eq nat y (alpha_nat b n))) *)
  do 4 rewrite Nat2Z.inj_mul in H1.
(* Goal: @sig nat (fun n : nat => and (@eq nat x (alpha_nat b (S n))) (@eq nat y (alpha_nat b n))) *)
  simpl Z.of_nat in H1.
(* Goal: @sig nat (fun n : nat => and (@eq nat x (alpha_nat b (S n))) (@eq nat y (alpha_nat b n))) *)
  apply Z.sub_move_r in H1.
(* Goal: @sig nat (fun n : nat => and (@eq nat x (alpha_nat b (S n))) (@eq nat y (alpha_nat b n))) *)
  destruct (le_lt_dec x y) as [ H3 | H3 ].
  + revert H1; replace y with x by omega; clear y H2 H3; intros H1.
(* Goal: @sig nat (fun n : nat => and (@eq nat x (alpha_nat b (S n))) (@eq nat x (alpha_nat b n))) *)
    exfalso.
(* Goal: False *)
    apply (@Pell_not_diag _ Hb (Z.of_nat x)); red.
    rewrite <- H1; ring.
  + destruct (@Pell_alpha _ Hb (Z.of_nat x) (Z.of_nat y)) as (n & P1 & P2).
(* Goal: iff (and (lt (S (S (S O))) b) (@eq nat a (alpha_nat b c))) (@ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y))))))))) *)
    * split.
      - apply Zle_0_nat. 
(* Goal: @eq nat (rem (alpha_nat b1 n) q) (rem (alpha_nat b2 n) q) *)
      - apply inj_lt; trivial.
    * red; rewrite <- H1; ring.
    * unfold alpha_Z in P1, P2.
(* Goal: @sig nat (fun n : nat => and (@eq nat x (alpha_nat b (S n))) (@eq nat y (alpha_nat b n))) *)
      apply Nat2Z.inj in P1.
(* Goal: @sig nat (fun n : nat => and (@eq nat x (alpha_nat b (S n))) (@eq nat y (alpha_nat b n))) *)
      apply Nat2Z.inj in P2.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      exists n; auto.
Qed.

Corollary Pell_alpha_nat' b x y : 2 <= b -> x*x+y*y = 1+b*(x*y) -> { n | x = alpha_nat b n }.
Proof.
(* Goal: forall (_ : le (S (S O)) b) (_ : @eq nat (Init.Nat.add (Init.Nat.mul x x) (Init.Nat.mul y y)) (Init.Nat.add (S O) (Init.Nat.mul b (Init.Nat.mul x y)))), @sig nat (fun n : nat => @eq nat x (alpha_nat b n)) *)
  intros H1 H2.
(* Goal: @sig nat (fun n : nat => @eq nat x (alpha_nat b n)) *)
  destruct (le_lt_dec y x) as [ H3 | H3 ].
  + destruct Pell_alpha_nat with (3 := H2) as (n & ? & ?); auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    exists (S n); auto.
  + rewrite plus_comm, (mult_comm x y) in H2.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    destruct Pell_alpha_nat with (3 := H2) as (n & ? & ?); auto; try omega.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    exists n; auto.
Qed.

Theorem alpha_nat_2lm b n m l j v : 
          2 <= b 
       -> v = alpha_nat b (S (S m)) - alpha_nat b m
       -> arem n (S m) l j 
       -> rem (alpha_nat b n) v = rem (alpha_nat b j) v
       \/ rem (alpha_nat b n + alpha_nat b j) v = 0.
Proof.
(* Goal: forall (_ : le (S (S O)) b) (_ : @eq nat v (Init.Nat.sub (alpha_nat b (S (S m))) (alpha_nat b m))) (_ : arem n (S m) l j), or (@eq nat (rem (alpha_nat b n) v) (rem (alpha_nat b j) v)) (@eq nat (rem (Init.Nat.add (alpha_nat b n) (alpha_nat b j)) v) O) *)
  intros Hb Hv.
  assert (Hv' : Z.of_nat v = (alpha_Z b (S(2 + m)) - alpha_Z b (S m))%Z).
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
  { rewrite Hv.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite Nat2Z.inj_sub; auto.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    apply lt_le_weak, alpha_nat_smono; omega. }
(* Goal: forall _ : arem n (S m) l j, or (@eq nat (rem (alpha_nat b n) v) (rem (alpha_nat b j) v)) (@eq nat (rem (Init.Nat.add (alpha_nat b n) (alpha_nat b j)) v) O) *)
  intros (Hk & [ Hl | (Hl1 & Hl2) ] ).
  + destruct alpha_nat_2lm_plus_j with (Hb_nat := Hb) (Hv := Hv') (l := l) (j := j) as [ H | H ].
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    - rewrite nat2Zp_inj in H; subst; auto.
    - right; rewrite <- rem_of_0 with v.
      rewrite <- nat2Zp_inj with (Hp := alpha_SSm_m_neq_0 Hb (S m) Hv').
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      rewrite nat2Zp_plus, Hl, H, Zp_plus_comm, Zp_minus, nat2Zp_zero; auto.
  + destruct alpha_nat_2lm_minus_j with (Hb_nat := Hb) (Hv := Hv') (l := l) (j := j) as [ H | H ]; auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    - rewrite nat2Zp_inj in H; subst; auto.
    - right; rewrite <- rem_of_0 with v.
      rewrite <- nat2Zp_inj with (Hp := alpha_SSm_m_neq_0 Hb (S m) Hv').
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      rewrite nat2Zp_plus, Hl2, H, Zp_plus_comm, Zp_minus, nat2Zp_zero; auto.
Qed.

Section divisibility_1.

  Variable (b : nat) (Hb : 2 <= b) (k : nat) (Hk : k <> 0).

  Let Hak : alpha_nat b k <> 0.
  Proof. apply alpha_nat_gt_0; auto. Qed.

  Section equation.

    Variable (m n l : nat) (Hm : m = n+l*k).

    Infix "⊗" := (Zp_mult Hak) (at level 40, left associativity).
    Notation expo := (mscal (Zp_mult Hak) (Zp_one Hak)).

    Hint Resolve Zle_0_nat.

    Section in_Z.

      Notation "〚 x 〛" := (Z2Zp Hak x).  (* congruence class modulo alpha_nat b k *)

      Let Z2ZP_morph := Z2Zp_morphishm Hak.

      Open Scope Z_scope.

      Fact A_k_morph22 : morph22 (Z2Zp Hak) (A b k) = (〚alpha_Z b (2+k)〛,Zp_zero Hak,Zp_zero Hak,〚-alpha_Z b k〛).
      Proof.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
        destruct k as [ | k' ]; try omega.
        unfold A; apply M22_equal; auto;
(* Goal: forall _ : @eq (Z_Zp (alpha_nat b k)) (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (S m))) (@Zp_mult (alpha_nat b k) Hak (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (S n))) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (S (S k)))))), @eq (Z_Zp (alpha_nat b k)) (@nat2Zp (alpha_nat b k) Hak (alpha_nat b m)) (@Zp_mult (alpha_nat b k) Hak (@nat2Zp (alpha_nat b k) Hak (alpha_nat b n)) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@nat2Zp (alpha_nat b k) Hak (alpha_nat b (S k))))) *)
        simpl plus; unfold alpha_Z.
        + rewrite Z2Zp_opp, Z2Zp_pos, Nat2Z.id, nat2Zp_p, Zp_opp_zero; auto.
        + rewrite Z2Zp_pos, Nat2Z.id, nat2Zp_p; auto.
      Qed.

      Lemma alpha_Z_mnlk_eq : 〚 alpha_Z b (1+m) 〛 = 〚 alpha_Z b (1+n) 〛⊗ expo l〚 alpha_Z b (2+k) 〛.
      Proof.
(* Goal: not (@eq nat (Init.Nat.mul (Init.Nat.mul (S (S O)) l) q) O) *)
        generalize (A_plus_mult Hb _ _ _ Hm); intros H. 
(* Goal: @eq (Z_Zp (alpha_nat b k)) (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) m))) (@Zp_mult (alpha_nat b k) Hak (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) n))) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S (S O)) k))))) *)
        apply f_equal with (f := morph22 (Z2Zp Hak)) in H.
(* Goal: @eq (Z_Zp (alpha_nat b k)) (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) m))) (@Zp_mult (alpha_nat b k) Hak (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) n))) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S (S O)) k))))) *)
        rewrite MU22_morph with (1 := Z2ZP_morph) in H.
(* Goal: @eq (Z_Zp (alpha_nat b k)) (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) m))) (@Zp_mult (alpha_nat b k) Hak (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) n))) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S (S O)) k))))) *)
        rewrite expo22_morph with (1 := Z2ZP_morph) in H.
(* Goal: @eq (Z_Zp (alpha_nat b k)) (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) m))) (@Zp_mult (alpha_nat b k) Hak (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) n))) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S (S O)) k))))) *)
        rewrite A_k_morph22 in H.
(* Goal: @eq (Z_Zp (alpha_nat b k)) (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) m))) (@Zp_mult (alpha_nat b k) Hak (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) n))) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S (S O)) k))))) *)
        rewrite Diag22_expo with (1 := Zp_is_ring Hak) in H.
(* Goal: @eq (Z_Zp (alpha_nat b k)) (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) m))) (@Zp_mult (alpha_nat b k) Hak (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) n))) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S (S O)) k))))) *)
        unfold A, morph22 in H.
(* Goal: @eq (Z_Zp (alpha_nat b k)) (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) m))) (@Zp_mult (alpha_nat b k) Hak (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) n))) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S (S O)) k))))) *)
        rewrite MU22_Diag22 with (1 := Zp_is_ring Hak) in H.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        inversion H; auto.
      Qed.

    End in_Z.

    Section in_nat.

      Notation "〚 x 〛" := (nat2Zp Hak x).  (* congruence class modulo alpha_nat b k *)

      Theorem alpha_nat_mnlk_eq : 〚 alpha_nat b m 〛 = 〚 alpha_nat b n 〛⊗ expo l〚 alpha_nat b (1+k) 〛.
      Proof.
(* Goal: @eq (Z_Zp (alpha_nat b k)) (@nat2Zp (alpha_nat b k) Hak (alpha_nat b m)) (@Zp_mult (alpha_nat b k) Hak (@nat2Zp (alpha_nat b k) Hak (alpha_nat b n)) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@nat2Zp (alpha_nat b k) Hak (alpha_nat b (Init.Nat.add (S O) k))))) *)
        generalize alpha_Z_mnlk_eq.
(* Goal: forall _ : @eq (Z_Zp (alpha_nat b k)) (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) m))) (@Zp_mult (alpha_nat b k) Hak (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S O) n))) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (Init.Nat.add (S (S O)) k))))), @eq (Z_Zp (alpha_nat b k)) (@nat2Zp (alpha_nat b k) Hak (alpha_nat b m)) (@Zp_mult (alpha_nat b k) Hak (@nat2Zp (alpha_nat b k) Hak (alpha_nat b n)) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@nat2Zp (alpha_nat b k) Hak (alpha_nat b (Init.Nat.add (S O) k))))) *)
        simpl plus.
(* Goal: forall _ : @eq (Z_Zp (alpha_nat b k)) (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (S m))) (@Zp_mult (alpha_nat b k) Hak (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (S n))) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@Z2Zp (alpha_nat b k) Hak (alpha_Z b (S (S k)))))), @eq (Z_Zp (alpha_nat b k)) (@nat2Zp (alpha_nat b k) Hak (alpha_nat b m)) (@Zp_mult (alpha_nat b k) Hak (@nat2Zp (alpha_nat b k) Hak (alpha_nat b n)) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@nat2Zp (alpha_nat b k) Hak (alpha_nat b (S k))))) *)
        unfold alpha_Z.
(* Goal: forall _ : @eq (Z_Zp (alpha_nat b k)) (@Z2Zp (alpha_nat b k) Hak (Z.of_nat (alpha_nat b m))) (@Zp_mult (alpha_nat b k) Hak (@Z2Zp (alpha_nat b k) Hak (Z.of_nat (alpha_nat b n))) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@Z2Zp (alpha_nat b k) Hak (Z.of_nat (alpha_nat b (S k)))))), @eq (Z_Zp (alpha_nat b k)) (@nat2Zp (alpha_nat b k) Hak (alpha_nat b m)) (@Zp_mult (alpha_nat b k) Hak (@nat2Zp (alpha_nat b k) Hak (alpha_nat b n)) (@mscal (Z_Zp (alpha_nat b k)) (@Zp_mult (alpha_nat b k) Hak) (@Zp_one (alpha_nat b k) Hak) l (@nat2Zp (alpha_nat b k) Hak (alpha_nat b (S k))))) *)
        do 3 (rewrite Z2Zp_pos; auto).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        do 3 rewrite Nat2Z.id; auto.
      Qed.

    End in_nat.

  End equation.

  (* This is property 3.23 *)

  Theorem alpha_nat_divides_k_ge_1 m : alpha_nat b k │ alpha_nat b m <-> k │ m.
  Proof.
(* Goal: iff (and (lt (S (S (S O))) b) (@eq nat a (alpha_nat b c))) (@ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y))))))))) *)
    split.
    + intros H1.
      destruct (euclid m Hk) as (l & [ | n ] & E1 & E2).
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      { exists l; omega. }
      rewrite plus_comm in E1.
      generalize (alpha_nat_mnlk_eq _ _ E1); intros H2.
      rewrite <- nat2Zp_expo, <- nat2Zp_mult in H2.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      apply nat2Zp_divides in H2; auto.
      apply is_rel_prime_div_r in H2.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      2: apply is_rel_prime_expo, is_gcd_sym, alpha_nat_coprime; auto.
      apply divides_le in H2.
      * apply alpha_nat_smono with (1 := Hb) in E2; omega.
      * apply alpha_nat_gt_0; omega.
    + intros (l & Hl).
      generalize (alpha_nat_mnlk_eq 0 _ Hl); intros H2.
      rewrite <- nat2Zp_expo, <- nat2Zp_mult in H2.
      simpl in H2.
      rewrite nat2Zp_inj in H2.
      rewrite rem_of_0 in H2.
      generalize (div_rem_spec1 (alpha_nat b m) (alpha_nat b k)).
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      exists (div (alpha_nat b m) (alpha_nat b k)); omega.
  Qed.

End divisibility_1.

Theorem alpha_nat_divisibility_1 b k m : 2 <= b -> alpha_nat b k │ alpha_nat b m <-> k │ m.
Proof.
(* Goal: forall _ : not (@eq nat (Init.Nat.sub b (S (S O))) O), @eq nat (rem (alpha_nat b n) (Init.Nat.sub b (S (S O)))) (rem n (Init.Nat.sub b (S (S O)))) *)
  intros Hb.
(* Goal: iff (divides (alpha_nat b k) (alpha_nat b m)) (divides k m) *)
  destruct (eq_nat_dec k 0) as [ | Hk ]; subst.
  + simpl; split; intros H; apply divides_0_inv in H.
    - destruct (eq_nat_dec m 0) as [ | Hm ]; subst.
      * apply divides_0.
      * generalize (alpha_nat_gt_0 Hb Hm); omega.
    - subst; apply divides_0.
  + apply alpha_nat_divides_k_ge_1; auto.
Qed.

Check alpha_nat_divisibility_1.
Print Assumptions alpha_nat_divisibility_1.

Section divisibility_2.

  Variable (b : nat) (Hb : 2 <= b) (k : nat) (Hk : k <> 0).

  Let Hak : alpha_nat b k <> 0.
  Proof. apply alpha_nat_gt_0; auto. Qed.

  Let ak2 := alpha_nat b k * alpha_nat b k.

  Let Hak2 : ak2 <> 0.
  Proof.
(* Goal: not (@eq nat (Init.Nat.mul (Init.Nat.mul (S (S O)) l) q) O) *)
    unfold ak2; intros H.
    apply mult_is_O in H; destruct H as [ H | H ];
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
     revert H; apply alpha_nat_gt_0; auto. 
  Qed.

  Section equation.

    Variable (m l : nat) (Hm : m = l*k) (Hl : l <> 0).

    (* ⊕  ⊗  ∸    ⊞ ⊠ ⊟ *)

    Infix "⊕" := (Zp_plus Hak2) (at level 50, left associativity).
    Infix "⊗" := (Zp_mult Hak2) (at level 40, left associativity).
    Notation expoZp := (mscal (Zp_mult Hak2) (Zp_one Hak2)).

    Hint Resolve Zle_0_nat.

    Section in_Zp.

      Notation "〚 x 〛" := (Z2Zp Hak2 x).  (* congruence class modulo alpha_nat b k *)

      Let Z2Zp_morph := Z2Zp_morphishm Hak2.

      Open Scope Z_scope.

      Let Zmult_monoid : monoid_theory Zmult 1.
      Proof. exists; intros; ring. Qed.

      Notation MZp := (M22 ak2).
      Infix "⊞" := (PL22 (Zp_plus Hak2)) (at level 50, left associativity).
      Infix "⊠" := (MU22 (Zp_plus Hak2) (Zp_mult Hak2)) (at level 40, left associativity).
      Notation MZp_Z := (ZE_22 (Zp_zero Hak2)).
      Notation MZp_I := (ID_22 (Zp_zero Hak2) (Zp_one Hak2)).
      Notation MZp_expo := (mscal (fun u v => u⊠v) MZp_I).
      Notation MZp_scal := (M22scal (Zp_mult Hak2)).

      Fact A_m_morph22 : morph22 (Z2Zp Hak2) (A b m) 
                       = MZp_scal (expoZp l〚-1〛⊗ expoZp l〚alpha_Z b k〛) MZp_I
                       ⊞ MZp_scal (expoZp (l-1)〚-1〛⊗ nat2Zp Hak2 l ⊗ 〚alpha_Z b (S k)〛⊗ expoZp (l-1)〚alpha_Z b k〛) (morph22 (Z2Zp Hak2) (B b)).
      Proof.
(* Goal: not (@eq nat (Init.Nat.mul (Init.Nat.mul (S (S O)) l) q) O) *)
        generalize (MA_expo_A_binomial Hb _ _ Hm); intros H.
        apply f_equal with (f := morph22 (Z2Zp Hak2)) in H.

        rewrite msum_morph with (m2 := fun u v => u⊞v) (u2 := MZp_Z) in H.
        2: simpl; apply M22_equal; apply Z2Zp_zero.
        2: intros (((?&?)&?)&?) (((?&?)&?)&?); apply M22_equal; apply Z2Zp_plus.

(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
        rewrite msum_first_two in H; try omega.
        2: apply M22plus_monoid with (1 := Zp_is_ring Hak2).
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
        2: { intros i Hi.
             rewrite M22scal_morph with (1 := Z2Zp_morph).
             repeat rewrite Z2Zp_mult.
             replace i with (2+(i-2))%nat at 3 by omega.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
             rewrite mscal_plus, Z2Zp_mult; auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
             rewrite mscal_S, mscal_1; auto.
             unfold alpha_Z at 1 2.
             rewrite <- Nat2Z.inj_mul. 
             fold ak2.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
             rewrite (@Z2Zp_pos _ Hak2 (Z.of_nat ak2)); auto.
             rewrite Nat2Z.id, nat2Zp_p. 
             repeat rewrite Zp_mult_zero.
             rewrite (Zp_mult_comm _ _ (Zp_zero _)).
             repeat rewrite Zp_mult_zero.
             apply M22scal_zero with (1 := Zp_is_ring Hak2). }

        rewrite H.
        repeat rewrite M22scal_morph with (1 := Z2Zp_morph).
        repeat rewrite Z2Zp_mult.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
        rewrite binomial_n1; try omega.
        rewrite binomial_n0.
        rewrite Nat.sub_0_r.
(* Goal: @eq (M22 Z) (@mscal (M22 Z) (@MU22 Z Z.add Z.mul) (@ID_22 Z Z0 (Zpos xH)) O B) (A O) *)
        repeat rewrite mscal_0.
        repeat rewrite Z2Zp_one.
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.mul (Z.of_nat q) (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
        f_equal.
        + f_equal; auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
          2: apply M22_equal; try rewrite Z2Zp_one; auto; rewrite Z2Zp_zero; auto.
          rewrite <- mscal_morph with (m1 := Zmult) (u1 := 1).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
          2: rewrite Z2Zp_one; auto.
          2: apply Z2Zp_mult.
          rewrite <- mscal_morph with (m1 := Zmult) (u1 := 1).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
          2: rewrite Z2Zp_one; auto.
          2: apply Z2Zp_mult.
          repeat rewrite <- Zp_mult_assoc.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
          repeat rewrite Zp_mult_one; auto.
        + repeat (rewrite mscal_1; auto).
          2: apply  M22mult_monoid with (1 := Zring).
          rewrite <- mscal_morph with (m1 := Zmult) (u1 := 1).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
          2: rewrite Z2Zp_one; auto.
          2: apply Z2Zp_mult.
          rewrite <- mscal_morph with (m1 := Zmult) (u1 := 1).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
          2: rewrite Z2Zp_one; auto.
          2: apply Z2Zp_mult.
(* Goal: @eq (M22 (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)))) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) (@M22scal (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.mul (Z.of_nat q) (@mscal Z Z.mul (Zpos xH) n (Z.of_nat q)))) (@morph22 Z (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm) VP)) *)
          do 4 f_equal.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
          rewrite Z2Zp_pos, Nat2Z.id; auto.
      Qed.

      Lemma alpha_Z_ml_eq : 〚 alpha_Z b (S m) 〛 
                          = expoZp (l-1) 〚-1〛
                          ⊗ nat2Zp Hak2 l
                          ⊗〚 alpha_Z b (S k) 〛
                          ⊗ expoZp (l-1)〚 alpha_Z b k 〛.
      Proof.
(* Goal: not (@eq nat (Init.Nat.mul (Init.Nat.mul (S (S O)) l) q) O) *)
        generalize A_m_morph22; intros H.
(* Goal: @eq (Z_Zp ak2) (@Z2Zp ak2 Hak2 (alpha_Z b (S m))) (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (Zneg xH))) (@nat2Zp ak2 Hak2 l)) (@Z2Zp ak2 Hak2 (alpha_Z b (S k)))) (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (alpha_Z b k)))) *)
        unfold morph22, A, B in H.
(* Goal: @eq (Z_Zp ak2) (@Z2Zp ak2 Hak2 (alpha_Z b (S m))) (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (Zneg xH))) (@nat2Zp ak2 Hak2 l)) (@Z2Zp ak2 Hak2 (alpha_Z b (S k)))) (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (alpha_Z b k)))) *)
        unfold M22scal, PL22, MZp_I in H.
(* Goal: @eq (Z_Zp ak2) (@Z2Zp ak2 Hak2 (alpha_Z b (S m))) (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (Zneg xH))) (@nat2Zp ak2 Hak2 l)) (@Z2Zp ak2 Hak2 (alpha_Z b (S k)))) (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (alpha_Z b k)))) *)
        apply M22_proj12 in H.
(* Goal: @eq (Z_Zp ak2) (@Z2Zp ak2 Hak2 (alpha_Z b (S m))) (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (Zneg xH))) (@nat2Zp ak2 Hak2 l)) (@Z2Zp ak2 Hak2 (alpha_Z b (S k)))) (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (alpha_Z b k)))) *)
        unfold plus in H; rewrite H; clear H.
(* Goal: @eq (Z_Zp ak2) (@Zp_plus ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) l (@Z2Zp ak2 Hak2 (Zneg xH))) (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) l (@Z2Zp ak2 Hak2 (alpha_Z b k)))) (@Zp_zero ak2 Hak2)) (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (Zneg xH))) (@nat2Zp ak2 Hak2 l)) (@Z2Zp ak2 Hak2 (alpha_Z b (S k)))) (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (alpha_Z b k)))) (@Z2Zp ak2 Hak2 (Zpos xH)))) (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (Zneg xH))) (@nat2Zp ak2 Hak2 l)) (@Z2Zp ak2 Hak2 (alpha_Z b (S k)))) (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (alpha_Z b k)))) *)
        rewrite Zp_mult_comm, Zp_mult_zero, Zp_plus_zero.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        rewrite Z2Zp_one, Zp_mult_comm, Zp_mult_one; auto.
      Qed.

    End in_Zp.

    Local Add Ring myring2 : (Zp_is_ring Hak2).

    Corollary alpha_square_nat : exists q, Zp_invertible Hak2 q /\ nat2Zp Hak2 (alpha_nat b m) = q ⊗ nat2Zp Hak2 l  ⊗ nat2Zp Hak2 (alpha_nat b k).
    Proof.
(* Goal: iff (and (lt (S (S (S O))) b) (@eq nat a (alpha_nat b c))) (@ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y))))))))) *)
      exists (expoZp (l-1) (Z2Zp Hak2 (-1)) ⊗ expoZp (l-1) (Z2Zp Hak2 (alpha_Z b k))); split.
      + apply Zp_mult_invertible; apply Zp_expo_invertible.
        * apply Zp_opp_invertible. 
          rewrite <- Z2Zp_opp.
          replace (- (-1))%Z with 1%Z by omega.
(* Goal: @Zp_invertible ak2 Hak2 (@Z2Zp ak2 Hak2 (Zpos xH)) *)
          rewrite Z2Zp_one.
(* Goal: @Zp_invertible ak2 Hak2 (@Zp_one ak2 Hak2) *)
          apply Zp_one_invertible.
        * destruct k; try omega; simpl.
(* Goal: @Zp_invertible ak2 Hak2 (@Z2Zp ak2 Hak2 (Z.of_nat (alpha_nat b n))) *)
          rewrite Z2Zp_of_nat.
(* Goal: @Zp_invertible ak2 Hak2 (@nat2Zp ak2 Hak2 (alpha_nat b n)) *)
          apply nat2Zp_invertible.
          unfold ak2.
          apply is_gcd_sym, is_rel_prime_mult; 
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
            apply is_gcd_sym, alpha_nat_coprime; auto.
      + generalize alpha_Z_ml_eq; intros H.
(* Goal: @eq (Z_Zp ak2) (@nat2Zp ak2 Hak2 (alpha_nat b m)) (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (Zneg xH))) (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (alpha_Z b k)))) (@nat2Zp ak2 Hak2 l)) (@nat2Zp ak2 Hak2 (alpha_nat b k))) *)
        unfold alpha_Z in H at 1 2.
(* Goal: @eq (Z_Zp ak2) (@nat2Zp ak2 Hak2 (alpha_nat b m)) (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@Zp_mult ak2 Hak2 (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (Zneg xH))) (@mscal (Z_Zp ak2) (@Zp_mult ak2 Hak2) (@Zp_one ak2 Hak2) (Init.Nat.sub l (S O)) (@Z2Zp ak2 Hak2 (alpha_Z b k)))) (@nat2Zp ak2 Hak2 l)) (@nat2Zp ak2 Hak2 (alpha_nat b k))) *)
        do 2 rewrite Z2Zp_of_nat in H.
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm b) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Zp_opp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Zpos xH)))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm b) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Zp_opp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Zpos xH))))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)))) *)
        rewrite H; ring.
    Qed.
  
  End equation.

  Theorem alpha_nat_divides_2_pos m : alpha_nat b k * alpha_nat b k │ alpha_nat b m <-> k*alpha_nat b k │ m.
  Proof.
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    destruct m as [ | m ].
    + simpl; split; intros; apply divides_0.
    + destruct (divides_dec (S m) k) as [ (l & Hl) | C ].
      * apply Zp_alpha_congruence_2 with (1 := Hl) (H2 := Hak2); try omega.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        apply alpha_square_nat; auto.
(* Goal: not (@eq nat k O) *)
        intro; subst; discriminate.
      * split; intros H; exfalso; apply C.
        - rewrite <- alpha_nat_divisibility_1 with (1 := Hb).
(* Goal: divides (alpha_nat b k) (alpha_nat b (S m)) *)
          apply divides_trans with (2 := H), divides_mult, divides_refl.
        - apply divides_trans with (2 := H).
(* Goal: divides k (Init.Nat.mul k (alpha_nat b k)) *)
          rewrite mult_comm.
(* Goal: divides u (Init.Nat.mul u u) *)
          apply divides_mult, divides_refl.
  Qed.

End divisibility_2.

(** Second divisibility property *)

Theorem alpha_nat_divisibility_2 b k m : 
    2 <= b -> alpha_nat b k * alpha_nat b k │ alpha_nat b m <-> k*alpha_nat b k │ m.
Proof.
(* Goal: forall _ : not (@eq nat (Init.Nat.sub b (S (S O))) O), @eq nat (rem (alpha_nat b n) (Init.Nat.sub b (S (S O)))) (rem n (Init.Nat.sub b (S (S O)))) *)
  intros Hb.
(* Goal: iff (divides (Init.Nat.mul (alpha_nat b k) (alpha_nat b k)) (alpha_nat b m)) (divides (Init.Nat.mul k (alpha_nat b k)) m) *)
  destruct k as [ | k ].
  * simpl.
(* Goal: iff (divides O (alpha_nat b m)) (divides O m) *)
    destruct m as [ | m ]; try (simpl; tauto).
(* Goal: iff (divides O (alpha_nat b (S m))) (divides O (S m)) *)
    split; intros H; apply divides_0_inv in H; try discriminate.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    contradict H; apply alpha_nat_gt_0; omega.
  * apply alpha_nat_divides_2_pos; omega.
Qed.

Check alpha_nat_divisibility_2.
Print Assumptions alpha_nat_divisibility_2.

Section congruence_1.

  Variable (b1 b2 : nat) (Hb1 : 2 <= b1) (Hb2 : 2 <= b2)
           (q : nat) (Hq : q <> 0) (Hb : nat2Zp Hq b1 = nat2Zp Hq b2).

  Hint Resolve Zle_0_nat.

  Theorem alpha_Z_congr n : Z2Zp Hq (alpha_Z b1 n) = Z2Zp Hq (alpha_Z b2 n).
  Proof.
(* Goal: @eq (Z_Zp q) (@Z2Zp q Hq (alpha_Z b1 n)) (@Z2Zp q Hq (alpha_Z b2 n)) *)
    induction on n as IHn with measure n.
(* Goal: @eq (Z_Zp q) (@Z2Zp q Hq (alpha_Z b1 n)) (@Z2Zp q Hq (alpha_Z b2 n)) *)
    destruct n as [ | [ | n ] ]; try reflexivity.
(* Goal: @eq (Z_Zp q) (@Z2Zp q Hq (alpha_Z b1 (S (S n)))) (@Z2Zp q Hq (alpha_Z b2 (S (S n)))) *)
    do 2 (rewrite alpha_fix_3; auto).
(* Goal: @eq (Z_Zp q) (@Z2Zp q Hq (Z.sub (Z.mul (Z.of_nat b1) (alpha_Z b1 (S n))) (alpha_Z b1 n))) (@Z2Zp q Hq (Z.sub (Z.mul (Z.of_nat b2) (alpha_Z b2 (S n))) (alpha_Z b2 n))) *)
    do 2 rewrite Z2Zp_minus, Z2Zp_mult.
(* Goal: @eq (Z_Zp q) (@Zp_plus q Hq (@Zp_mult q Hq (@Z2Zp q Hq (Z.of_nat b1)) (@Z2Zp q Hq (alpha_Z b1 (S n)))) (@Zp_opp q Hq (@Z2Zp q Hq (alpha_Z b1 n)))) (@Zp_plus q Hq (@Zp_mult q Hq (@Z2Zp q Hq (Z.of_nat b2)) (@Z2Zp q Hq (alpha_Z b2 (S n)))) (@Zp_opp q Hq (@Z2Zp q Hq (alpha_Z b2 n)))) *)
    do 2 f_equal; try (apply IHn; omega).
(* Goal: @eq (Z_Zp q) (@Z2Zp q Hq (Z.of_nat b1)) (@Z2Zp q Hq (Z.of_nat b2)) *)
    do 2 (rewrite Z2Zp_pos; auto).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    do 2 rewrite Nat2Z.id; auto.
  Qed.

End congruence_1.

Theorem alpha_nat_congruence_0 b1 b2 q n : 
           2 <= b1 
        -> 2 <= b2
        -> rem b1 q = rem b2 q 
        -> rem (alpha_nat b1 n) q = rem (alpha_nat b2 n) q.
Proof.
(* Goal: forall (_ : lt (Init.Nat.mul (S (S O)) a) v) (_ : lt (Init.Nat.mul (S (S O)) b) v) (_ : @eq nat (rem a v) (rem b v)), @eq nat a b *)
  intros H1 H2 H3.
(* Goal: @eq nat (rem (alpha_nat b1 n) q) (rem (alpha_nat b2 n) q) *)
  destruct (eq_nat_dec q 0) as [ H4 | H4 ].
  + subst; do 2 rewrite rem_0 in H3; subst; auto.
  + rewrite <- nat2Zp_inj with (Hp := H4) in H3.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    apply alpha_Z_congr with (n := S n) in H3; auto.
(* Goal: @eq nat (rem (alpha_nat b1 n) q) (rem (alpha_nat b2 n) q) *)
    simpl in H3.
(* Goal: @eq nat (rem (alpha_nat b1 n) q) (rem (alpha_nat b2 n) q) *)
    do 2 rewrite Z2Zp_of_nat in H3.
(* Goal: @eq nat (rem (alpha_nat b1 n) q) (rem (alpha_nat b2 n) q) *)
    rewrite nat2Zp_inj in H3.
(* Goal: @eq nat (rem (alpha_nat b1 n) q) (rem (alpha_nat b2 n) q) *)
    trivial.
Qed.

Corollary alpha_nat_congruence_1 b n : b-2 <> 0 -> rem (alpha_nat b n) (b-2) = rem n (b-2).
Proof.
(* Goal: forall _ : not (@eq nat (Init.Nat.sub b (S (S O))) O), @eq nat (rem (alpha_nat b n) (Init.Nat.sub b (S (S O)))) (rem n (Init.Nat.sub b (S (S O)))) *)
  intros Hb.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
  rewrite <- alpha_nat_2 with (b_nat := 2) (n := n) at 2; auto.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
  apply alpha_nat_congruence_0; try omega.
  replace b with ((b-2)+2) at 1 by omega.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
  apply rem_erase with 1; omega.
Qed.

Check alpha_nat_congruence_0.
Check alpha_nat_congruence_1.

Section congruence_2.

  (** This section might notbe needed anymore *)

  Variable (b : nat) (Hb : b - 2 <> 0).

  Notation "〚 x 〛" := (Z2Zp Hb x).  (* congruence class modulo (b-2) *)

  Hint Resolve Zle_0_nat.

  Open Scope Z_scope.

  Theorem alpha_Z_b_2 n : 〚 alpha_Z b n 〛 = Zp_plus Hb 〚 Z.of_nat n 〛〚 -1 〛.
  Proof.
    rewrite <- Z2Zp_plus.
    replace (Z.of_nat n + -1) with (Z.of_nat n -1) by omega.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite <- (@alpha_2 2); auto.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    apply alpha_Z_congr; try omega.
    replace b with (2+(b-2))%nat at 3 by omega.
(* Goal: @eq (Z_Zp (Init.Nat.sub b (S (S O)))) (@nat2Zp (Init.Nat.sub b (S (S O))) Hb (Init.Nat.add (S (S O)) (Init.Nat.sub b (S (S O))))) (@nat2Zp (Init.Nat.sub b (S (S O))) Hb (S (S O))) *)
    rewrite nat2Zp_plus.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    rewrite nat2Zp_p, Zp_plus_comm, Zp_plus_zero; auto.
  Qed.

End congruence_2.

Lemma rem_eq_eq a b v : 2*a < v -> 2*b < v -> rem a v = rem b v -> a = b.
Proof.
(* Goal: forall (_ : lt (Init.Nat.mul (S (S O)) a) v) (_ : lt (Init.Nat.mul (S (S O)) b) v) (_ : @eq nat (rem a v) (rem b v)), @eq nat a b *)
  intros H1 H2 H3.
(* Goal: @eq nat a b *)
  do 2 (rewrite rem_lt in H3; try omega).
Qed.

Lemma rem_eq_diff_eq a b v : 2*a < v -> 2*b < v -> (rem a v = rem b v) \/ (rem (a+b) v = 0) -> a = b.
Proof.
(* Goal: forall (_ : lt (Init.Nat.mul (S (S O)) a) v) (_ : lt (Init.Nat.mul (S (S O)) b) v) (_ : or (@eq nat (rem a v) (rem b v)) (@eq nat (rem (Init.Nat.add a b) v) O)), @eq nat a b *)
  intros H1 H2 [ H3 | H3 ].
  + do 2 (rewrite rem_lt in H3; try omega).
  + rewrite rem_lt in H3; omega.
Qed.

Section diophantine_sufficiency.

  Variables (a b c : nat) (u t r s v w x y : nat).

  Definition alpha_conditions :=
                    3 < b
                 /\ u*u+t*t = 1+b*(u*t)
                 /\ s*s+r*r = 1+b*(s*r)
                 /\ r < s
                 /\ u*u │ s
                 /\ v+2*r = b*s
                 /\ rem w v = rem b v
                 /\ rem w u = rem 2 u
                 /\ 2 < w
                 /\ x*x+y*y = 1+w*(x*y)
                 /\ 2*a < u
                 /\ 2*a < v
                 /\ rem a v = rem x v
                 /\ 2*c < u
                 /\ rem c u = rem x u.

  Theorem alpha_sufficiency : alpha_conditions -> 3 < b /\ a = alpha_nat b c.
  Proof.
    intros (H41 & H42 & H43 & H44 & H45 & H46 & H47 & H48 & H49 & H50 &
            H51 & H52 & H53 & H54 & H55).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    split; auto.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    destruct Pell_alpha_nat' with (2 := H42) as (k & Hu); try omega.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    destruct Pell_alpha_nat with (3 := H43) as (m & Hs & Hr); try omega.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    destruct Pell_alpha_nat' with (2 := H50) as (n & Hx); try omega.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    destruct (@division_by_even n (S m)) as (l & j & Hlj); try omega.
    assert (divides (k*u) (S m)) as H60'.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    { subst u s; apply alpha_nat_divisibility_2; try omega; auto. }
(* Goal: @eq nat a (alpha_nat b c) *)
    assert (divides u (S m)) as H60.
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    { apply divides_trans with (2 := H60'), divides_mult, divides_refl. }
    assert (v = alpha_nat b (S (S m)) - alpha_nat b m) as H61.
    { rewrite alpha_nat_fix_2, <- Hs, <- Hr; omega. }
(* Goal: @eq nat a (alpha_nat b c) *)
    assert (rem x v = rem (alpha_nat b n) v) as H62_1.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    { rewrite Hx; apply alpha_nat_congruence_0; omega. }
(* Goal: @eq nat a (alpha_nat b c) *)
    assert (rem x u = rem n u) as H65.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    { rewrite Hx, alpha_nat_congruence_0 with (3 := H48); try omega.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      f_equal; apply alpha_nat_2; auto. }
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    assert (2 <= b) as Hb by omega.
(* Goal: @eq nat a (alpha_nat b c) *)
    generalize (alpha_nat_2lm Hb H61 Hlj); intros H62_2.
    assert (2*alpha_nat b j < v) as H63.
    { apply le_lt_trans with ((b-2)*alpha_nat b (S m)).
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      { apply mult_le_compat; try omega.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        red in Hlj; apply alpha_nat_mono; tauto. }
      apply plus_lt_reg_l with (2*r).
(* Goal: lt (Init.Nat.add (Init.Nat.mul (S (S O)) r) (Init.Nat.mul (Init.Nat.sub b (S (S O))) (alpha_nat b (S m)))) (Init.Nat.add (Init.Nat.mul (S (S O)) r) v) *)
      rewrite (plus_comm _ v), H46, Hr, Hs.
      replace b with (2+(b-2)) at 4 by omega.
(* Goal: lt (Init.Nat.add (Init.Nat.mul (S (S O)) (alpha_nat b m)) (Init.Nat.mul (Init.Nat.sub b (S (S O))) (alpha_nat b (S m)))) (Init.Nat.mul (Init.Nat.add (S (S O)) (Init.Nat.sub b (S (S O)))) (alpha_nat b (S m))) *)
      rewrite Nat.mul_add_distr_r.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      apply plus_lt_le_compat; auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      apply mult_lt_compat_l; auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      apply alpha_nat_inc; auto. }
    rewrite plus_comm, <- rem_plus_rem, <- H62_1, <- H53, rem_plus_rem, plus_comm in H62_2.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    apply rem_eq_diff_eq in H62_2; auto.
    assert (2*j < u) as H66.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    { apply le_lt_trans with (2 := H51), mult_le_compat; auto.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      subst; apply alpha_nat_ge_n; auto. }
    rewrite <- H55 in H65.
(* Goal: @eq nat a (alpha_nat b c) *)
    assert (c = j) as H67.
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    { destruct H60 as (q & Hq).
(* Goal: @eq nat c j *)
      destruct Hlj as (G1 & [ G2 | (G2 & G3) ]).
      + apply rem_eq_diff_eq with u; auto.
(* Goal: or (@eq nat (rem c u) (rem j u)) (@eq nat (rem (Init.Nat.add c j) u) O) *)
        left.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        rewrite H65, rem_erase with (n := 2*l*q) (r := j); auto.
(* Goal: @eq (Z_Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm b) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Zp_opp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Zpos xH)))) (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm b) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Zp_opp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_plus (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q))) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Zpos xH))))) (@Zp_mult (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)) (@Z2Zp (Init.Nat.sub (Init.Nat.sub (Init.Nat.mul b_nat q) (Init.Nat.mul q q)) (S O)) Hm (Z.of_nat q)))) *)
        rewrite G2, Hq; ring.
      + apply rem_eq_diff_eq with u; auto.
(* Goal: or (@eq nat (rem c u) (rem j u)) (@eq nat (rem (Init.Nat.add c j) u) O) *)
        right.
        rewrite plus_comm, <- rem_plus_rem, H65, rem_plus_rem, G3.
        apply rem_prop with (2*l*q); try omega.
(* Goal: @eq nat (Init.Nat.add j (Init.Nat.sub (Init.Nat.mul (Init.Nat.mul (S (S O)) l) (S m)) j)) (Init.Nat.add (Init.Nat.mul (Init.Nat.mul (Init.Nat.mul (S (S O)) l) q) u) O) *)
        rewrite Hq, mult_assoc.
        assert (2*l*q <> 0) as G4.
(* Goal: not (@eq nat (Init.Nat.mul (Init.Nat.mul (S (S O)) l) q) O) *)
        { intros H.
(* Goal: False *)
          apply mult_is_O in H; destruct H as [ H | H ]; try (subst; discriminate).
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
          apply mult_is_O in H; destruct H; omega. }
        assert (1*u <= 2*l*q*u).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        { apply mult_le_compat; auto.
          revert G4; generalize (2*l*q); intros; omega. }
(* Goal: @eq nat (Init.Nat.add j (Init.Nat.sub (Nat.mul (Nat.mul (Init.Nat.mul (S (S O)) l) q) u) j)) (Init.Nat.add (Init.Nat.mul (Init.Nat.mul (Init.Nat.mul (S (S O)) l) q) u) O) *)
        rewrite Nat.mul_1_l in H.
        revert H; generalize (2*l*q*u); intros; omega. }
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    subst; auto.
  Qed.

End diophantine_sufficiency.

Section diophantine_necessity.

  Variables (a b c : nat).

  Theorem alpha_necessity : 3 < b /\ a = alpha_nat b c -> exists u t r s v w x y, alpha_conditions a b c u t r s v w x y.
  Proof.
(* Goal: forall _ : and (lt (S (S (S O))) b) (@eq nat a (alpha_nat b c)), @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    intros (H1 & H2).
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    assert (2 <= b) as Hb by omega.
    destruct find_odd_alpha' with (b_nat := b) (u := 1+2*a+2*c) as (k & Hk1 & Hk2); try omega.
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    remember (alpha_nat b k) as u.
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    remember (alpha_nat b (S k)) as t.
    remember (u*k) as m.
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    destruct m as [ | m ].
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    { symmetry in Heqm.
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
      apply mult_is_O in Heqm.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      destruct Heqm; subst; try omega.
       rewrite alpha_nat_fix_0, rem_of_0 in Hk2; discriminate. }
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    remember (alpha_nat b m) as r.
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    remember (alpha_nat b (S m)) as s.
    assert (s*s+r*r = 1+b*(s*r)) as Hrs.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
    { rewrite Heqr, Heqs; apply alpha_nat_Pell; auto. }
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    assert (k <> 0) as Hk.
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    { intro; subst; discriminate. }
    assert (divides (u*u) s) as Hus.
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    { rewrite Hequ, Heqs.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      apply alpha_nat_divisibility_2; auto.
      rewrite <- Hequ, Heqm, mult_comm.
      apply divides_refl. }
    assert (2*a < b*s - 2*r) as H3.
    { apply lt_le_trans with (2*u).
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      { apply mult_lt_compat_l; omega. }
      apply le_trans with (2*(S m)).
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
      { apply mult_le_compat_l; rewrite Heqm.
        rewrite <- (Nat.mul_1_r u) at 1.
(* Goal: le (Nat.mul u (S O)) (Init.Nat.mul u k) *)
        apply mult_le_compat; omega. }
      apply le_trans with (2*alpha_nat b (S m)).
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
      { apply mult_le_compat_l.
(* Goal: le (S m) (alpha_nat b (S m)) *)
        apply le_trans with (1 := alpha_nat_ge_n Hb _).
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
        apply alpha_nat_mono; omega. }
      apply le_trans with (4*s - 2*r).
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
      { rewrite Heqs, Heqr.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
        generalize (alpha_nat_inc Hb m); intros; omega. }
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      apply Nat.sub_le_mono_r, mult_le_compat_r; omega. }
    remember (b*s-2*r) as v.
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    assert (is_gcd u v 1) as Huv.
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    { split; [ | split ]; try apply divides_1.
(* Goal: forall (k : nat) (_ : divides k u) (_ : divides k v), divides k (S O) *)
      intros d Hd1 Hd2.
(* Goal: divides d (S O) *)
      assert (divides d s) as F1.
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
      { apply divides_trans with (2 := Hus).
(* Goal: divides d (Init.Nat.mul u u) *)
        apply divides_trans with (1 := Hd1).
        apply divides_mult, divides_refl. }
      assert (divides d (2*r)) as F2.
(* Goal: @ex nat (fun w : nat => and (@eq nat (rem w u) (rem (S (S O)) u)) (and (@eq nat (rem w v) (rem b v)) (lt (S (S O)) w))) *)
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
      { apply divides_plus_inv with (1 := Hd2).
        replace (v+2*r) with (b*s) by omega.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        apply divides_mult; auto. }
(* Goal: divides d (S O) *)
      apply is_rel_prime_div in F2.
      + apply divides_plus_inv with (b*(s*r)).
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        * do 2 apply divides_mult; auto.
        * rewrite plus_comm, <- Hrs.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
          apply divides_plus; apply divides_mult; auto.
      + apply is_gcd_sym, no_common_prime_is_coprime; try discriminate.
(* Goal: forall (p : nat) (_ : prime p) (_ : divides p (S (S O))) (_ : divides p d), False *)
        intros z G0 G1 G2.
(* Goal: False *)
        apply divides_2_inv in G1; destruct G1; subst z.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
        destruct G0; omega.
(* Goal: False *)
        replace (rem u 2) with 0 in Hk2; try discriminate.
(* Goal: @eq nat O (rem u (S (S O))) *)
        symmetry; apply divides_rem_eq; try discriminate.
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
        apply divides_trans with (1 := G2); auto. }
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    assert (exists w, rem w u = rem 2 u /\ rem w v = rem b v /\ 2 < w) as Hw.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
    { apply CRT; auto; try omega. }
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    destruct Hw as (w & Hw1 & Hw2 & Hw3).
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    remember (alpha_nat w c) as x.
(* Goal: @ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y)))))))) *)
    remember (alpha_nat w (S c)) as y.
    exists u, t, r, s, v, w, x, y; repeat (split; auto); 
(* Goal: @eq Z (alpha_Z (S (S O))) (Zpos xH) *)
      try (subst; (apply alpha_nat_Pell || apply alpha_nat_Pell' || apply alpha_nat_inc)); try omega; auto.
    + rewrite Heqx, alpha_nat_congruence_0 with (3 := Hw2), H2; try omega.
    + rewrite Heqx; symmetry.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      rewrite alpha_nat_congruence_0 with (3 := Hw1); try omega.
(* Goal: lt (Init.Nat.add (S O) (Init.Nat.mul q q)) (Init.Nat.mul b_nat q) *)
      f_equal; apply alpha_nat_2; omega.
  Qed.

End diophantine_necessity.

Theorem alpha_diophantine a b c : 3 < b /\ a = alpha_nat b c 
                              <-> exists u t r s v w x y, alpha_conditions a b c u t r s v w x y.
Proof.
(* Goal: iff (and (lt (S (S (S O))) b) (@eq nat a (alpha_nat b c))) (@ex nat (fun u : nat => @ex nat (fun t : nat => @ex nat (fun r : nat => @ex nat (fun s : nat => @ex nat (fun v : nat => @ex nat (fun w : nat => @ex nat (fun x : nat => @ex nat (fun y : nat => alpha_conditions a b c u t r s v w x y))))))))) *)
  split.
  + apply alpha_necessity.
  + intros (u & t & r & s & v & w & x & y & H); revert H.
(* Goal: forall _ : alpha_conditions a b c u t r s v w x y, and (lt (S (S (S O))) b) (@eq nat a (alpha_nat b c)) *)
    apply alpha_sufficiency.
Qed.

