Library QedkConcrete.complexsqrtcorrectmath

Soundness of the extension-field square-root formula

The real coordinate c satisfies 2*c*c = a+r, where r*r=a*a+b*b. The imaginary coordinate is b/(2*c). These equations suffice to show that the returned pair squares to a+i*b; no curve assumption is used.
From Stdlib Require Import ZArith Lia Zmod.Zmod.
Require Import RocqOfOCaml.RocqOfOCaml.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

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

Lemma complex_half_root_equations a b r c i :
  (r × r) mod modulus = (a × a + b × b) mod modulus →
  (2 × c × c) mod modulus = (a + r) mod modulus →
  (2 × c × i) mod modulus = 1 mod modulus →
  (c × c - (b × i) × (b × i)) mod modulus = a mod modulus ∧
  (2 × c × (b × i)) mod modulus = b mod modulus.
Proof.
  replace (2 × c × c) with (c×c + c×c) by ring.
  replace (2 × c × i) with (c×i + c×i) by ring.
  replace (2 × c × (b × i)) with ((c×i + c×i)*b) by ring.
  intros Hr Hc Hi.
  apply (proj2 (Zmod.of_Z_inj _ _)) in Hr.
  apply (proj2 (Zmod.of_Z_inj _ _)) in Hc.
  apply (proj2 (Zmod.of_Z_inj _ _)) in Hi.
  repeat first [rewrite Zmod.of_Z_add in Hr | rewrite Zmod.of_Z_mul in Hr |
    rewrite Zmod.of_Z_add in Hc | rewrite Zmod.of_Z_mul in Hc |
    rewrite Zmod.of_Z_add in Hi | rewrite Zmod.of_Z_mul in Hi | rewrite Zmod.of_Z_1 in Hi].
  set (A := Zmod.of_Z modulus a) in ×.
  set (B := Zmod.of_Z modulus b) in ×.
  set (R := Zmod.of_Z modulus r) in ×.
  set (C := Zmod.of_Z modulus c) in ×.
  set (I := Zmod.of_Z modulus i) in ×.
  split; apply (proj1 (Zmod.of_Z_inj _ _)).
  - repeat rewrite ?Zmod.of_Z_sub, ?Zmod.of_Z_mul.
    apply Zmod.sub_eq_0_iff.
    change (C×C - (B×I)*(B×I) - A = 0)%Zmod.
    replace (C×C - (B×I)*(B×I) - A)%Zmod with
      (((C×C+C×C-A)*(C×C+C×C-A) - (A×A+B×B))*I×I +
       (C×C-A)*(1-(C×I+C×I)*(C×I+C×I)))%Zmod by ring.
    rewrite Hc, Hi.
    replace (A+R-A)%Zmod with R by ring.
    rewrite Hr. ring.
  - repeat rewrite ?Zmod.of_Z_add, ?Zmod.of_Z_mul.
    change ((C×I+C×I)*B = B)%Zmod. rewrite Hi. ring.
Qed.
Lemma complex_half_root_denominator a b r c :
  Z.prime modulus → b mod modulus ≠ 0 →
  (r × r) mod modulus = (a × a + b × b) mod modulus →
  (2 × c × c) mod modulus = (a + r) mod modulus →
  (2 × c) mod modulus ≠ 0.
Proof.
  intros Hprime Hb Hr Hc Hzero.
  assert (Hzero' : Zmod.of_Z modulus (2×c) = Zmod.zero).
  { apply Zmod.to_Z_inj. rewrite Zmod.to_Z_of_Z, Zmod.to_Z_0. exact Hzero. }
  assert (Hhalfzero : Zmod.of_Z modulus (a+r) = Zmod.zero).
  { apply (proj2 (Zmod.of_Z_inj _ _)) in Hc.
    rewrite <- Hc, Zmod.of_Z_mul, Hzero', Zmod.mul_0_l. reflexivity. }
  rewrite Zmod.of_Z_add in Hhalfzero. apply Zmod.add_eq_0 in Hhalfzero.
  apply (proj2 (Zmod.of_Z_inj _ _)) in Hr.
  repeat first [rewrite Zmod.of_Z_add in Hr | rewrite Zmod.of_Z_mul in Hr].
  assert (Hbsquare : (Zmod.of_Z modulus b × Zmod.of_Z modulus b = 0)%Zmod).
  { replace (Zmod.of_Z modulus b × Zmod.of_Z modulus b)%Zmod with
      ((Zmod.of_Z modulus a × Zmod.of_Z modulus a +
        Zmod.of_Z modulus b × Zmod.of_Z modulus b) -
        Zmod.of_Z modulus a × Zmod.of_Z modulus a)%Zmod by ring.
    rewrite <- Hr, Hhalfzero. ring. }
  apply (Zmod.mul_0_iff_prime Hprime) in Hbsquare.
  pose proof (Zmod.of_Z_nz b Hb). intuition.
Qed.
Lemma complex_half_root_formula a b r c :
  Z.prime modulus → b mod modulus ≠ 0 →
  (r × r) mod modulus = (a × a + b × b) mod modulus →
  (2 × c × c) mod modulus = (a + r) mod modulus →
  let d := (b × OCamlZ.invert ((2×c) mod modulus) modulus) mod modulus in
  (c×c - d×d) mod modulus = a mod modulus ∧
  (2×c×d) mod modulus = b mod modulus.
Proof.
  intros Hprime Hb Hr Hc. cbv zeta.
  pose proof (Z.prime_ge_2 _ Hprime) as Hmodulus.
  pose proof (complex_half_root_denominator a b r c Hprime Hb Hr Hc) as Hnonzero.
  pose proof (Z.mod_pos_bound (2×c) modulus ltac:(lia)) as Hrange.
  pose proof (OCamlZ.invert_prime_modular_inverse ((2×c) mod modulus) modulus
    (proj1 (Znumtheory.prime_alt _) Hprime) ltac:(lia)) as Hinverse.
  assert (Hi : (2×c×OCamlZ.invert ((2×c) mod modulus) modulus) mod modulus =
    1 mod modulus).
  { rewrite <- Z.mul_mod_idemp_l, Hinverse, Z.mod_small by lia. reflexivity. }
  pose proof (complex_half_root_equations a b r c _ Hr Hc Hi) as [Hreal Himag].
  split; [|rewrite Z.mul_mod_idemp_r by lia; exact Himag].
  rewrite Zminus_mod, Z.mul_mod_idemp_l, Z.mul_mod_idemp_r by lia.
  rewrite <- Zminus_mod by lia. exact Hreal.
Qed.
End Modular.