Library QedkConcrete.blsswu2map

The BLS extension-field SWU map passes the coordinate constructor

The exceptional denominator selects a square quotient; all other cases follow the two SWU field identities. Thus the generated partial constructor never needs an arbitrary fallback value.
From Stdlib Require Import ZArith Lia Field Zmod.Zmod.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Ec.Bls12_381
  QedkConcrete.nativefieldbounds QedkConcrete.nativecomplexbounds
  QedkConcrete.primefieldarith QedkConcrete.blscomplexsqrt
  QedkConcrete.blscomplexcorrect QedkConcrete.blscomplexresidues
  QedkConcrete.complexresiduefield QedkConcrete.swucomplex.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Bls := Ec.Bls12_381.
Module QR := QuadraticResidue.
Module SWU := Ec.Curve.SWU_method.

Section Map.
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 Notation field := (@blscomplexresidues.field).
Local Notation complex := (@blscomplexresidues.complex).
Local Definition curve := @Bls.F_p2_SWU_curve.MonadVM_Ec_Curve_Make_include_fargs .
Local Definition swu := @Bls.F_p2_SWU_fargs .
Local Notation p := (@PF.modulus field).
Local Notation residue := (@blsComplexResidue).
Local Notation bounded := (@complexBound _ complex (@primeFieldBound field)).
Local Notation mul := (@Complex.op_star _ complex).
Local Notation add := (@Complex.op_plus _ complex).
Local Notation sub := (@Complex.op_minus _ complex).
Local Notation divide := (@Complex.op_div _ complex).
Local Definition a := @SWU.a_value _ _ swu.
Local Definition b := @SWU.b_value _ _ swu.
Local Definition z := (@SWU.P _ _ swu).(Ec.Curve.SWU_method_P_signature.z_value).
Local Definition zero := @Complex.zero _ complex.
Local Definition one := @Complex.op_tildedollar _ complex 1.
Add Field extension_field : (QR.quadratic_field p
  (@blsprimality.bls_generated_modulus_prime)
  (@legendretests.bls_modulus_three_mod_four)).

Lemma bls_swu2_parameters : bounded a ∧ bounded b ∧ bounded z ∧
  residue a ≠ QR.zero p ∧ residue z ≠ QR.zero p.
Proof. vm_compute. repeat split; congruence. Qed.

Lemma bls_swu2_exceptional_square
    {UX : @Complex.t _ complex} :
  let n := mul b (add zero one) in
  let d := mul a z in
  let d2 := mul d d in
  let d3 := mul d2 d in
  fst (@SWU.sqrt_ratio _ _ swu UX
    (add (mul (add (mul n n) (mul a d2)) n) (mul b d3)) d3) = true.
Proof.
  cbv zeta.
  match goal with |- fst (@SWU.sqrt_ratio _ _ _ _ ?top ?bottom) = true ⇒
    set (numerator := top); set (denominator := bottom) end.
  set (q := @Complex.t.Build _ complex
    233763546583149232693324345809390259381444540386448082992812237259012452124745508654976075092796954350637537951911
    1377551988989230297911463847415000649873185633555154359978844657329926629487036903409127189985377363925671809326608).
  assert (Hden : bounded denominator) by (apply (@bls_complex_mul_bound)).
  assert (Hdennz : residue denominator ≠ QR.zero p) by (vm_compute; congruence).
  assert (Hq : divide numerator denominator = q).
  { apply (@bls_complex_residue_injective).
    - apply (@bls_complex_mul_bound).
    - vm_compute. repeat split; congruence.
    - rewrite (@bls_complex_residue_div) by assumption.
      assert (Hproduct : QR.mul p (residue q) (residue denominator) = residue numerator)
        by (vm_compute; reflexivity).
      rewrite <- Hproduct. field; exact Hdennz. }
  change (fst (match @Bls.F_p2.sqrt_opt (divide numerator denominator) with
    | Some root ⇒ (true, root)
    | None ⇒ (false, @MonadVM.Option.get _ UX
        (@Bls.F_p2.sqrt_opt (mul z (divide numerator denominator))))
    end) = true).
  rewrite Hq.
  destruct (@bls_complex_sqrt_if_norm_square q.(Complex.t.re) q.(Complex.t.im)
    ltac:(vm_compute; split; congruence) ltac:(vm_compute; split; congruence)
    ltac:(rewrite OCamlZ.legendre_euler; vm_compute; discriminate)) as [r Hr].
  change (@Bls.F_p2.sqrt_opt q = Some r) in Hr.
  rewrite Hr. reflexivity.
Qed.

Definition blsSwu2PointValid (point : @Curve.t _ curve) : Prop :=
  @curvePointBound _ curve bounded point ∧
  match point with
  | Curve.Infinity ⇒ True
  | Curve.Point x y ⇒ @Curve.in_curve _ curve x y = true
  end.

Theorem bls_swu2_map_valid_nonzero
    {UX : @Complex.t _ complex}
    {UP : @Curve.t _ curve} u :
  blsSwu2PointValid (@SWU.map_to_curve_simple_swu _ _ swu UX UP u) ∧
  @SWU.map_to_curve_simple_swu _ _ swu UX UP u ≠ Curve.Infinity.
Proof.
  rewrite (@swu_complex_generated_apply _ swu) by reflexivity.
  change ((fun point ⇒ blsSwu2PointValid point ∧ point ≠ Curve.Infinity) (@swuComplexApply a b z
    (@SWU.sqrt_ratio _ _ swu UX)
    ((@SWU.P _ _ swu).(Ec.Curve.SWU_method_P_signature.sgn0))
    (@Curve.t _ curve) (fun x y ⇒ @MonadVM.Option.get _ UP (@Curve.of_coords _ curve x y)) u)).
  destruct bls_swu2_parameters as [Ha [Hb [Hz [Hanz Hznz]]]].
  apply swu_complex_apply_valid.
  - exact Hanz.
  - exact Hznz.
  - intros top bottom. exact (@bls_swu2_sqrt_ratio_bound UX top bottom).
  - intros top bottom. exact (@bls_swu2_sqrt_ratio_correct UX top bottom).
  - exact (@bls_swu2_exceptional_square UX).
  - intros x y Hx Hy Heq.
    assert (Hcurve : @Curve.in_curve _ curve x y = true).
    { change (@Complex.op_eq _ complex (mul y y) (add (mul x (add (mul x x) a)) b) = true).
      assert (Hcoord : mul y y = add (mul x (add (mul x x) a)) b).
      { apply (@bls_complex_residue_injective).
        - apply (@bls_complex_mul_bound).
        - apply complex_add_bound; [exact (@bls_complex_base_closed)|
            apply (@bls_complex_mul_bound)|exact Hb].
        - repeat first [rewrite (@bls_complex_residue_mul) |
            rewrite (@bls_complex_residue_add) by
              (repeat first [assumption | apply (@bls_complex_mul_bound) |
                apply complex_add_bound; [exact (@bls_complex_base_closed)| |]])].
          exact Heq. }
      rewrite Hcoord. destruct (add (mul x (add (mul x x) a)) b) as [re im].
      change ((@PF.op_eq field re re && @PF.op_eq field im im)%bool = true).
      apply Bool.andb_true_iff. split; apply prime_eq_true; reflexivity. }
    unfold Curve.of_coords. rewrite Hcurve.
    destruct (andb _ _) eqn:Hboth.
    + exfalso. apply Bool.andb_true_iff in Hboth. destruct Hboth as [Hzx Hzy].
      change (@Complex.op_eq _ complex x (@Complex.zero _ complex) = true) in Hzx.
      change (@Complex.op_eq _ complex y (@Complex.zero _ complex) = true) in Hzy.
      apply (@swucomplex.equal_true) in Hzx, Hzy.
      subst x y. vm_compute in Hcurve. discriminate.
    + cbn [MonadVM.Option.get OCamlOption.get blsSwu2PointValid curvePointBound].
      split; [split; [split; assumption|exact Hcurve]|discriminate].
Qed.

Theorem bls_swu2_map_valid
    {UX : @Complex.t _ complex}
    {UP : @Curve.t _ curve} u :
  blsSwu2PointValid (@SWU.map_to_curve_simple_swu _ _ swu UX UP u).
Proof.
  destruct (@bls_swu2_map_valid_nonzero UX UP u) as [Hvalid _]. exact Hvalid.
Qed.

The isogeny reads coordinates without an infinity branch. SWU never returns infinity, because its auxiliary curve does not contain (0,0).
Lemma bls_swu2_map_coordinates
    {UX : @Complex.t _ complex}
    {UP : @Curve.t _ curve} u :
  let '(x,y) := @Curve.coords _ curve (@SWU.map_to_curve_simple_swu _ _ swu UX UP u) in
  bounded x ∧ bounded y ∧ @Curve.in_curve _ curve x y = true.
Proof.
  destruct (@bls_swu2_map_valid_nonzero UX UP u) as [Hvalid Hnonzero].
  destruct (@SWU.map_to_curve_simple_swu _ _ swu UX UP u) as [x y|];
    [|contradiction].
  destruct Hvalid as [[Hx Hy] HonCurve].
  change (bounded x ∧ bounded y ∧ @Curve.in_curve _ curve x y = true).
  split; [exact Hx|]. split; assumption.
Qed.

Corollary bls_swu2_map_bound
    {UX : @Complex.t _ complex}
    {UP : @Curve.t _ curve} u :
  @curvePointBound _ curve bounded (@SWU.map_to_curve_simple_swu _ _ swu UX UP u).
Proof.
  destruct (@bls_swu2_map_valid UX UP u) as [Hbound _]. exact Hbound.
Qed.
End Map.