Library QedkConcrete.complexsqrtmath

Why the extension-field square-root alternatives are exhaustive

If r squared is a squared plus b squared, the product of (a+r)/2 and (a-r)/2 is minus (b/2) squared. For p=3 mod 4 and b nonzero, that product is a nonsquare, so precisely one of the two candidates is square.
From Stdlib Require Import ZArith Lia Zmod.Zmod.
Require Import RocqOfOCaml.RocqOfOCaml QedkConcrete.eulercompleteness
  QedkConcrete.legendrearithmetic.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section Modular.
Variable modulus : Z.
Add Ring residue_ring : (Zmod.ring_theory modulus).

Lemma half_product_mod a b r u v :
  (r × r) mod modulus = (a × a + b × b) mod modulus →
  (2 × u) mod modulus = (a + r) mod modulus →
  (2 × v) mod modulus = (a - r) mod modulus →
  (4 × u × v) mod modulus = (- (b × b)) mod modulus.
Proof.
  intros Hr Hu Hv.
  apply (proj2 (Zmod.of_Z_inj _ _)) in Hr.
  apply (proj2 (Zmod.of_Z_inj _ _)) in Hu.
  apply (proj2 (Zmod.of_Z_inj _ _)) in Hv.
  apply (proj1 (Zmod.of_Z_inj _ _)).
  replace (4 × u × v) with ((2 × u) × (2 × v)) by ring.
  rewrite Zmod.of_Z_mul, Hu, Hv.
  rewrite !Zmod.of_Z_mul, !Zmod.of_Z_add, !Zmod.of_Z_sub, !Zmod.of_Z_opp,
    !Zmod.of_Z_mul in ×.
  replace ((Zmod.of_Z modulus a + Zmod.of_Z modulus r) ×
    (Zmod.of_Z modulus a - Zmod.of_Z modulus r))%Zmod
    with ((Zmod.of_Z modulus a × Zmod.of_Z modulus a) -
      (Zmod.of_Z modulus r × Zmod.of_Z modulus r))%Zmod by ring.
  rewrite Hr. ring.
Qed.

Lemma sum_squares_zero_imaginary a b :
  Z.prime modulus → modulus mod 4 = 3 →
  (a × a + b × b) mod modulus = 0 → b mod modulus = 0.
Proof.
  intros Hprime Hfour Hnorm.
  pose proof (Z.prime_ge_2 _ Hprime) as Hmodulus.
  destruct (Z.eqb_spec (b mod modulus) 0) as [Hb|Hb]; [exact Hb|].
  exfalso.
  assert (Hsquare : (a × a) mod modulus = (- (b × b)) mod modulus).
  { apply (proj1 (Zmod.of_Z_inj _ _)).
    assert (Hnorm' : Zmod.of_Z modulus (a × a + b × b) = Zmod.zero).
    { apply Zmod.to_Z_inj. rewrite Zmod.to_Z_of_Z, Zmod.to_Z_0. exact Hnorm. }
    rewrite Zmod.of_Z_add in Hnorm'. rewrite Zmod.of_Z_opp.
    transitivity ((Zmod.of_Z modulus (a × a) + Zmod.of_Z modulus (b × b)) -
      Zmod.of_Z modulus (b × b))%Zmod; [ring|].
    rewrite Hnorm'. ring. }
  pose proof (euler_power_congruent _ _ modulus ltac:(lia) Hsquare) as He.
  rewrite (euler_negative_square b modulus Hprime Hfour Hb) in He.
  destruct (Z.eqb_spec (a mod modulus) 0) as [Ha|Ha].
  - assert (Hzero : (a × a) mod modulus = 0).
    { rewrite Z.mul_mod, Ha, Z.mul_0_l, Z.mod_0_l by lia. reflexivity. }
    rewrite <- (euler_power_reduce (a × a) modulus ltac:(lia)), Hzero in He.
    assert (Hlarge : 2 < modulus).
    { destruct (Z.eqb_spec modulus 2) as [->|]; [discriminate|lia]. }
    rewrite euler_power_zero in He by lia. lia.
  - rewrite (euler_nonzero_square a modulus Hprime Hfour Ha) in He.
    assert (modulus = 2) by lia. subst modulus. discriminate.
Qed.

Lemma half_candidates_opposite a b r u v :
  Z.prime modulus → modulus mod 4 = 3 → b mod modulus ≠ 0 →
  (r × r) mod modulus = (a × a + b × b) mod modulus →
  (2 × u) mod modulus = (a + r) mod modulus →
  (2 × v) mod modulus = (a - r) mod modulus →
  OCamlZ.legendre u modulus = -1 → OCamlZ.legendre v modulus = 1.
Proof.
  intros Hprime Hfour Hb Hr Hu Hv Hnegative.
  pose proof (Z.prime_ge_2 _ Hprime) as Hmodulus.
  assert (Hlarge : 2 < modulus).
  { destruct (Z.eqb_spec modulus 2) as [->|]; [discriminate|lia]. }
  apply (euler_opposite_factors u v modulus Hprime Hfour); [|exact Hnegative].
  pose proof (euler_power_congruent _ _ modulus ltac:(lia)
    (half_product_mod a b r u v Hr Hu Hv)) as He.
  rewrite (euler_negative_square b modulus Hprime Hfour Hb) in He.
  replace (4 × u × v) with ((2 × 2) × (u × v)) in He by ring.
  rewrite euler_power_product, (euler_nonzero_square 2 modulus Hprime Hfour),
    Z.mul_1_l in He by (try (rewrite Z.mod_small by lia); lia).
  rewrite Z.mod_small in He; [exact He|].
  rewrite euler_power_spec by lia. apply Z.mod_pos_bound; lia.
Qed.
Lemma complex_product_norm a b c d :
  let re := (a × c - b × d) mod modulus in
  let im := (a × d + b × c) mod modulus in
  (re × re + im × im) mod modulus = ((a × a + b × b) × (c × c + d × d)) mod modulus.
Proof.
  cbv zeta. apply (proj1 (Zmod.of_Z_inj _ _)).
  repeat rewrite ?Zmod.of_Z_mul, ?Zmod.of_Z_add, ?Zmod.of_Z_mod, ?Zmod.of_Z_sub.
  ring.
Qed.
End Modular.