Library QedkConcrete.complexresiduefield

The quadratic residue field used by BLS

For a prime p congruent to 3 modulo 4, a*a+b*b vanishes only when both coordinates vanish. Thus conjugation divided by the norm supplies the inverse, exactly as in the generated complex arithmetic.
From Stdlib Require Import ZArith Lia Field Zmod.Zmod.
Require Import QedkConcrete.complexsqrtmath.
Set Default Goal Selector "!".

Module QuadraticResidue.
Section Field.
Variable p : Z.
Local Open Scope Zmod_scope.
Definition t := (Zmod p × Zmod p)%type.
Definition zero : t := (0,0).
Definition one : t := (1,0).
Definition add (x y : t) : t := (fst x + fst y, snd x + snd y).
Definition opp (x : t) : t := (-fst x, -snd x).
Definition sub (x y : t) : t := add x (opp y).
Definition mul (x y : t) : t :=
  (fst x × fst y - snd x × snd y, fst x × snd y + snd x × fst y).
Definition norm (x : t) : Zmod p := fst x × fst x + snd x × snd x.
Definition inv (x : t) : t :=
  (fst x × Zmod.inv (norm x), -snd x × Zmod.inv (norm x)).
Definition divide (x y : t) : t := mul x (inv y).

Add Ring base_ring : (Zmod.ring_theory p).

Lemma quadratic_ring : ring_theory zero one add mul sub opp eq.
Proof.
  constructor; intros; repeat match goal with x : t |- _ ⇒ destruct x end;
    cbv beta iota zeta delta [zero one add mul sub opp fst snd]; f_equal; ring.
Qed.

Hypothesis Hprime : Z.prime p.
Hypothesis Hfour : (p mod 4 = 3)%Z.

Lemma norm_zero x : norm x = 0 → x = zero.
Proof.
  destruct x as [a b]. intro Hnorm.
  assert (Hb : b = 0).
  { assert (Heq : ((Zmod.to_Z a × Zmod.to_Z a + Zmod.to_Z b × Zmod.to_Z b) mod p = 0)%Z).
    { apply Zmod.to_Z_inj_iff in Hnorm.
      rewrite Zmod.to_Z_0 in Hnorm.
      rewrite <- Zmod.to_Z_of_Z.
      rewrite Zmod.of_Z_add, !Zmod.of_Z_mul, !Zmod.of_Z_to_Z.
      exact Hnorm. }
    pose proof (sum_squares_zero_imaginary p (Zmod.to_Z a) (Zmod.to_Z b)
      Hprime Hfour Heq) as Hzero.
    apply Zmod.to_Z_inj.
    rewrite Zmod.to_Z_0.
    rewrite Z.mod_small in Hzero by
      (pose proof (Zmod.to_Z_range b); pose proof (Z.prime_ge_2 _ Hprime); lia).
    exact Hzero. }
  subst b. cbv beta iota zeta delta [norm fst snd] in Hnorm.
  rewrite Zmod.mul_0_l, Zmod.add_0_r in Hnorm.
  apply (Zmod.mul_0_iff_prime Hprime) in Hnorm.
  destruct Hnorm; subst a; reflexivity.
Qed.

Lemma norm_nonzero x : x ≠ zero → norm x ≠ 0.
Proof. intros Hx Hz. apply Hx. apply norm_zero; exact Hz. Qed.

Lemma inverse_left x : x ≠ zero → mul (inv x) x = one.
Proof.
  intro Hx. pose proof (norm_nonzero x Hx) as Hnorm.
  destruct x as [a b]. unfold norm in Hnorm. cbn [fst snd] in Hnorm.
  cbv beta iota zeta delta [mul inv norm one fst snd]. f_equal.
  - replace (a × Zmod.inv (a×a+b×b) × a - -b × Zmod.inv (a×a+b×b) × b)
      with ((a×a+b×b) × Zmod.inv (a×a+b×b)) by ring.
    apply Zmod.mul_inv_same_r_prime; assumption.
  - ring.
Qed.

Lemma quadratic_field : field_theory zero one add mul sub opp divide inv eq.
Proof.
  constructor.
  - exact quadratic_ring.
  - intro H. apply (f_equal fst) in H.
    change ((1 : Zmod p) = 0) in H.
    apply Zmod.to_Z_inj_iff in H.
    rewrite Zmod.to_Z_1_pos, Zmod.to_Z_0 in H;
      [discriminate|pose proof (Z.prime_ge_2 _ Hprime); lia].
  - reflexivity.
  - exact inverse_left.
Qed.
End Field.
End QuadraticResidue.