Library QedkConcrete.blscomplexcorrect

Soundness of BLS extension-field square roots

The concrete routine's zero-imaginary case uses a real or purely imaginary root. Otherwise it uses one of the two half-sum roots and divides by twice that root. The norm equation proves the denominator is nonzero; the generated inverse therefore satisfies its equation.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Ec.Bls12_381 QedkConcrete.blssquareroots
  QedkConcrete.blscomplexsqrt QedkConcrete.complexsqrtcorrectmath
  QedkConcrete.nativecomplexbounds QedkConcrete.nativefieldbounds
  QedkConcrete.primefieldarith QedkConcrete.blsprimality.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Bls := Ec.Bls12_381.

Section Soundness.
Context {UZ : Basics.Unreachable Z} {UU : Basics.Unreachable Support.UintN.t}
  {UI : Basics.Unreachable Numeric.Integer.t}
  {UB : Basics.Unreachable Numeric.IntegerBase.(Numeric.Make.Make_result.t)}
  {UF : Basics.Unreachable Z} {UO : Basics.Unreachable (option Z)}
  {US : Basics.Unreachable (Z → option Z)}.
Local Definition field := @Bls.F_p.MonadVM_Ec_Algebra_Prime_field_include_fargs .
Local Definition complex := @Bls.F_p2.MonadVM_Ec_Algebra_Complex_extension_include_fargs .
Local Definition sqrt := @Bls.F_p.sqrt_opt .
Local Definition modulus := @PF.modulus field.

Lemma bls_half_root_equation value c :
  sqrt (@PF.op_div field value 2) = Some c →
  (2×c×c) mod modulus = value mod modulus.
Proof.
  intro Hroot.
  pose proof (@bls_sqrt_correct
    (@PF.op_div field value 2) c Hroot) as [_ Hsquare].
  change ((c×c) mod modulus = (@PF.op_div field value 2) mod modulus) in Hsquare.
  replace (2×c×c) with (2*(c×c)) by ring.
  rewrite Z.mul_mod, Hsquare, <- Z.mul_mod by
    (pose proof (@bls_generated_modulus_prime) as H;
     apply Z.prime_ge_2 in H; change (2 ≤ modulus) in H; lia).
  apply (@prime_half_mod field).
  - exact (@bls_generated_modulus_prime).
  - vm_compute. reflexivity.
Qed.

Lemma bls_complex_square_coordinates a b c d :
  0 ≤ a < modulus → 0 ≤ b < modulus →
  (c×c-d×d) mod modulus = a mod modulus →
  (2×c×d) mod modulus = b mod modulus →
  @Complex.op_star _ complex (@Complex.t.Build _ complex c d)
    (@Complex.t.Build _ complex c d) = @Complex.t.Build _ complex a b.
Proof.
  intros Ha Hb Hreal Himag.
  assert (Hpositive : 0 < modulus).
  { pose proof (@bls_generated_modulus_prime) as H.
    apply Z.prime_ge_2 in H. change (2 ≤ modulus) in H. lia. }
  change (@Complex.t.Build _ complex
    (@PF.op_minus field (@PF.op_star field c c) (@PF.op_star field d d))
    (@PF.op_plus field (@PF.op_star field c d) (@PF.op_star field d c)) =
    @Complex.t.Build _ complex a b).
  rewrite (@prime_sub_mod field), (@prime_add_mod field);
    try (apply prime_mul_bound; exact Hpositive).
  change (@Complex.t.Build _ complex
    (((c×c) mod modulus - (d×d) mod modulus) mod modulus)
    (((c×d) mod modulus + (d×c) mod modulus) mod modulus) =
    @Complex.t.Build _ complex a b).
  rewrite <- Zminus_mod, <- Z.add_mod by lia.
  replace (c×d+d×c) with (2×c×d) by ring.
  rewrite Hreal, Himag, !Z.mod_small by assumption. reflexivity.
Qed.

Lemma bls_complex_half_root a b r c :
  0 ≤ a < modulus → 0 ≤ b < 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 root := @Complex.t.Build _ complex c
    (@PF.op_div field b (@PF.op_star field 2 c)) in
  @Complex.op_star _ complex root root = @Complex.t.Build _ complex a b.
Proof.
  intros Ha Hb Hnonzero Hr Hc. cbv zeta.
  pose proof (complex_half_root_formula modulus a b r c
    (@bls_generated_modulus_prime) Hnonzero Hr Hc) as [Hreal Himag].
  apply bls_complex_square_coordinates; assumption.
Qed.
Theorem bls_complex_sqrt_correct value root :
  @complexBound _ complex (@primeFieldBound field) value →
  @Bls.F_p2.sqrt_opt value = Some root →
  @Complex.op_star _ complex root root = value.
Proof.
  destruct value as [a b]. change Z in a, b. intros [Ha Hb] Hroot.
  change (0 ≤ a < modulus) in Ha. change (0 ≤ b < modulus) in Hb.
  assert (Hpositive : 0 < modulus).
  { pose proof (@bls_generated_modulus_prime) as H.
    apply Z.prime_ge_2 in H. change (2 ≤ modulus) in H. lia. }
  change ((if @PF.op_eq field b 0 then
     match sqrt a with
     | Some c ⇒ Some (@Complex.t.Build _ complex c 0)
     | None ⇒ MonadVM.Option.bind (sqrt (@PF.op_minus field 0 a))
         (fun c ⇒ Some (@Complex.t.Build _ complex 0 c))
     end
   else MonadVM.Option.bind
     (sqrt (@PF.op_plus field (@PF.op_star field a a) (@PF.op_star field b b)))
     (fun r ⇒ MonadVM.Option.bind
       (match sqrt (@PF.op_div field (@PF.op_plus field a r) 2) with
        | Some c ⇒ Some c
        | None ⇒ sqrt (@PF.op_div field (@PF.op_minus field a r) 2)
        end)
       (fun c ⇒ Some (@Complex.t.Build _ complex c
         (@PF.op_div field b (@PF.op_star field 2 c)))))) = Some root) in Hroot.
  destruct (@PF.op_eq field b 0) eqn:Hzero.
  - apply prime_eq_true in Hzero. subst b.
    destruct (sqrt a) as [c|] eqn:Hc.
    + inversion Hroot; subst root.
      pose proof (@bls_sqrt_correct a c Hc) as [_ Hsquare].
      apply bls_complex_square_coordinates; try assumption.
      × rewrite Z.mul_0_l, Z.sub_0_r. exact Hsquare.
      × rewrite Z.mul_0_r. reflexivity.
    + destruct (sqrt (@PF.op_minus field 0 a)) as [c|] eqn:Hcneg;
        cbn [MonadVM.Option.bind OCamlOption.bind] in Hroot; [|discriminate].
      inversion Hroot; subst root.
      pose proof (@bls_sqrt_correct
        (@PF.op_minus field 0 a) c Hcneg) as [_ Hsquare].
      rewrite (@prime_sub_mod field 0 a ltac:(change (0 ≤ 0 < modulus); lia) Ha) in Hsquare.
      change ((c×c) mod modulus = ((0-a) mod modulus) mod modulus) in Hsquare.
      rewrite Z.mod_mod in Hsquare by lia.
      apply bls_complex_square_coordinates; try assumption.
      × rewrite Z.mul_0_l, Zminus_mod, Hsquare, <- Zminus_mod by lia.
        replace (0-(0-a)) with a by ring. reflexivity.
      × rewrite Z.mul_0_r. reflexivity.
  - assert (Hnonzero : b mod modulus ≠ 0).
    { rewrite Z.mod_small by exact Hb. intro H. subst b.
      assert (@PF.op_eq field 0 0 = true) by (apply prime_eq_true; reflexivity).
      congruence. }
    assert (Hnormeq : @PF.op_plus field (@PF.op_star field a a)
      (@PF.op_star field b b) = (a×a+b×b) mod modulus).
    { rewrite (@prime_add_mod field); [|apply prime_mul_bound|apply prime_mul_bound];
        try exact Hpositive.
      change (((a×a) mod modulus + (b×b) mod modulus) mod modulus =
        (a×a+b×b) mod modulus). symmetry. apply Z.add_mod; lia. }
    rewrite Hnormeq in Hroot.
    destruct (sqrt ((a×a+b×b) mod modulus)) as [r|] eqn:Hr;
      cbn [MonadVM.Option.bind OCamlOption.bind] in Hroot; [|discriminate].
    change Z in r.
    pose proof (@bls_sqrt_correct
      ((a×a+b×b) mod modulus) r Hr) as [Hrb Hsquare].
    change (0 ≤ r < modulus) in Hrb.
    change ((r×r) mod modulus = ((a×a+b×b) mod modulus) mod modulus) in Hsquare.
    rewrite Z.mod_mod in Hsquare by lia.
    destruct (sqrt (@PF.op_div field (@PF.op_plus field a r) 2)) as [c|] eqn:Hc.
    + cbn [MonadVM.Option.bind OCamlOption.bind] in Hroot. inversion Hroot; subst root.
      apply (bls_complex_half_root a b r c Ha Hb Hnonzero Hsquare).
      rewrite (bls_half_root_equation _ _ Hc), (@prime_add_mod field a r Ha Hrb).
      change (((a+r) mod modulus) mod modulus = (a+r) mod modulus).
      apply Z.mod_mod; lia.
    + destruct (sqrt (@PF.op_div field (@PF.op_minus field a r) 2))
        as [c|] eqn:Hcminus;
        cbn [MonadVM.Option.bind OCamlOption.bind] in Hroot; [|discriminate].
      inversion Hroot; subst root.
      apply (bls_complex_half_root a b (-r) c Ha Hb Hnonzero).
      × replace ((-r)*(-r)) with (r×r) by ring. exact Hsquare.
      × rewrite (bls_half_root_equation _ _ Hcminus), (@prime_sub_mod field a r Ha Hrb).
        change (((a-r) mod modulus) mod modulus = (a+ -r) mod modulus).
        rewrite Z.mod_mod by lia. f_equal; ring.
Qed.
SWU receives a root of its quotient when the first test succeeds, and a root of the parameter times that quotient otherwise. In particular, the second branch's partial extraction is justified by arithmetic.
Theorem bls_swu2_sqrt_ratio_correct
    {UX : @Complex.t _ complex} u v :
  let swu := @Bls.F_p2_SWU_fargs in
  let q := @Bls.F_p2_SWU_curve.Underlying.op_div u v in
  let z := (@SWU.P _ _ swu).(Ec.Curve.SWU_method_P_signature.z_value) in
  let '(square, root) := @SWU.sqrt_ratio _ _ swu UX u v in
  @Complex.op_star _ complex root root =
    if square then q else @Complex.op_star _ complex z q.
Proof.
  cbv zeta.
  set (q := @Bls.F_p2_SWU_curve.Underlying.op_div u v).
  set (z := (@SWU.P _ _ (@Bls.F_p2_SWU_fargs))
    .(Ec.Curve.SWU_method_P_signature.z_value)).
  change (let '(square, root) :=
    match @Bls.F_p2.sqrt_opt q with
    | Some root ⇒ (true, root)
    | None ⇒ (false, @MonadVM.Option.get _ UX
        (@Bls.F_p2.sqrt_opt (@Complex.op_star _ complex z q)))
    end in
    @Complex.op_star _ complex root root =
      if square then q else @Complex.op_star _ complex z q).
  assert (Hq : @complexBound _ complex (@primeFieldBound field) q).
  { apply (@bls_complex_mul_bound). }
  destruct (@Bls.F_p2.sqrt_opt q) as [root|] eqn:Hroot.
  - exact (bls_complex_sqrt_correct q root Hq Hroot).
  - destruct (@bls_complex_sqrt_alternative z q Hq
      (@bls_swu2_parameter_nonsquare) Hroot)
      as [root [Hr Hbound]].
    change (@Bls.F_p2.sqrt_opt
      (@Complex.op_star _ complex z q) = Some root) in Hr.
    rewrite Hr. apply bls_complex_sqrt_correct;
      [apply (@bls_complex_mul_bound)|exact Hr].
Qed.
End Soundness.