Library QedkConcrete.primesqrt

Soundness of the generated prime-field square root

A successful root is reduced and squares to the input. This proves a property of the generated arithmetic, not an axiom about native calls. The final lemma proves that SWU's alternative branch finds a root when the first test fails, provided its fixed parameter is a nonsquare.
From Stdlib Require Import ZArith Lia Bool.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Ec.Algebra QedkConcrete.legendrearithmetic.
Require Import QedkConcrete.eulercompleteness QedkConcrete.nativefieldbounds.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module PF := Ec.Algebra.Prime_field.

Section PrimeRoot.
Context {F : PF.FArgs} {UZ : Basics.Unreachable Z}
  {UI : Basics.Unreachable Numeric.Integer.t} {UF : Basics.Unreachable PF.t}.

Lemma prime_sqrt_available : PF.modulus mod 4 = 3 →
  ∃ sqrt, @PF.sqrt_opt F = Some sqrt.
Proof.
  intro Hfour. unfold PF.sqrt_opt.
  change (Numeric.Integer.op_eq
    (Numeric.Integer.modulo PF.modulus (Numeric.Integer.op_tildedollar 4))
    (Numeric.Integer.op_tildedollar 3))
    with (Z.eqb (OCamlZ.compare (PF.modulus mod 4) 3) 0).
  rewrite Hfour. eexists. reflexivity.
Qed.

Lemma prime_sqrt_equation sqrt value :
  0 < PF.modulus →
  @PF.sqrt_opt F = Some sqrt →
  sqrt value =
    if Z.eqb (OCamlZ.compare value (0 mod PF.modulus)) 0 then
      Some (0 mod PF.modulus)
    else if Z.eqb (OCamlZ.legendre value PF.modulus) 1 then
      Some ((@OCamlZ.powm (Opaque.legacy_witness (@Numeric.Make._rocq_fallback_of_string_0 Numeric.IntegerBase_fargs)) value (Z.quot (PF.modulus + 1) 4) PF.modulus) mod PF.modulus)
    else None.
Proof.
  intros Hmodulus Hsqrt. unfold PF.sqrt_opt in Hsqrt.
  destruct (Numeric.Integer.op_eq _ _); [|discriminate].
  injection Hsqrt as Hsqrt. subst sqrt. cbn beta.
  unfold PF.zero. rewrite !prime_reduce_mod by exact Hmodulus. reflexivity.
Qed.

Lemma prime_sqrt_positive sqrt value :
  0 < PF.modulus →
  @PF.sqrt_opt F = Some sqrt → OCamlZ.legendre value PF.modulus = 1 →
  ∃ root, sqrt value = Some root.
Proof.
  intros Hmodulus Hsqrt Hpositive. rewrite (prime_sqrt_equation sqrt value ltac:(lia) Hsqrt), Hpositive.
  destruct (Z.eqb (OCamlZ.compare value (0 mod PF.modulus)) 0); eexists; reflexivity.
Qed.

Lemma prime_sqrt_zero sqrt :
  0 < PF.modulus → @PF.sqrt_opt F = Some sqrt → sqrt 0 = Some 0.
Proof.
  intros Hmodulus Hsqrt. rewrite (prime_sqrt_equation sqrt 0 ltac:(lia) Hsqrt).
  rewrite Z.mod_0_l by lia. reflexivity.
Qed.

Lemma prime_sqrt_correct sqrt value root :
  1 < PF.modulus → PF.modulus mod 4 = 3 →
  @PF.sqrt_opt F = Some sqrt → sqrt value = Some root →
  0 ≤ root < PF.modulus ∧ (root × root) mod PF.modulus = value mod PF.modulus.
Proof.
  intros Hmodulus Hfour Hsqrt Hroot.
  rewrite (prime_sqrt_equation sqrt value ltac:(lia) Hsqrt) in Hroot.
  rewrite Z.mod_0_l in Hroot by lia.
  rewrite Z.quot_div_nonneg in Hroot by lia.
  unfold OCamlZ.compare in Hroot.
  destruct (Z.compare_spec value 0) as [Hzero|Hnegative|Hpositive].
  { subst value. cbn in Hroot. injection Hroot as Hroot. subst root.
    rewrite Z.mod_0_l by lia. split; [lia|reflexivity]. }
  all: cbn in Hroot.
  all: destruct (Z.eqb (OCamlZ.legendre value PF.modulus) 1) eqn:Hlegendre; [|discriminate].
  all: apply Z.eqb_eq in Hlegendre.
  all: injection Hroot as Hroot; subst root.
  all: split; [apply Z.mod_pos_bound; lia|].
  all: rewrite <- Z.mul_mod by lia.
  all: apply euler_root_correct; assumption.
Qed.
Lemma prime_sqrt_none_negative sqrt value :
  1 < PF.modulus → 0 ≤ value < PF.modulus →
  @PF.sqrt_opt F = Some sqrt → sqrt value = None →
  OCamlZ.legendre value PF.modulus = -1.
Proof.
  intros Hmodulus Hvalue Hsqrt Hnone.
  destruct (legendre_euler_range value PF.modulus) as [Hnegative|[Hzero|Hpositive]];
    [exact Hnegative| |].
  - rewrite OCamlZ.legendre_euler in Hzero.
    destruct (Z.eqb (value mod PF.modulus) 0) eqn:HzeroMod.
    + apply Z.eqb_eq in HzeroMod. rewrite Z.mod_small in HzeroMod by exact Hvalue.
      subst value. rewrite (prime_sqrt_zero sqrt ltac:(lia) Hsqrt) in Hnone.
      discriminate.
    + destruct (Z.eqb (OCamlZ.euler_power value PF.modulus) 1); discriminate.
  - destruct (prime_sqrt_positive sqrt value ltac:(lia) Hsqrt Hpositive) as [root Hroot].
    congruence.
Qed.

A rejected first root cannot lead to Option.get None in SWU: its nonsquare parameter flips Euler's sign, so the alternative root exists.
Theorem prime_sqrt_swu_alternative sqrt value z :
  Z.prime PF.modulus → PF.modulus mod 4 = 3 →
  OCamlZ.euler_power z PF.modulus = PF.modulus - 1 →
  0 ≤ value < PF.modulus → @PF.sqrt_opt F = Some sqrt →
  sqrt value = None →
  ∃ root, sqrt ((z × value) mod PF.modulus) = Some root ∧
    0 ≤ root < PF.modulus ∧
    (root × root) mod PF.modulus = (z × value) mod PF.modulus.
Proof.
  intros Hprime Hfour Hz Hvalue Hsqrt Hnone.
  pose proof (Z.prime_ge_2 _ Hprime) as Hmodulus.
  pose proof (prime_sqrt_none_negative sqrt value ltac:(lia) Hvalue Hsqrt Hnone) as Hnegative.
  pose proof (euler_swu_alternative z value PF.modulus Hprime Hfour Hz Hnegative) as Hpositive.
  destruct (prime_sqrt_positive sqrt _ ltac:(lia) Hsqrt Hpositive) as [root Hroot].
  ∃ root. split; [exact Hroot|].
  pose proof (prime_sqrt_correct sqrt _ root ltac:(lia) Hfour Hsqrt Hroot) as Hcorrect.
  rewrite Z.mod_mod in Hcorrect by lia. exact Hcorrect.
Qed.
End PrimeRoot.