Library QedkConcrete.blsswumap

The BLS base-field SWU coordinates pass the curve check

From Stdlib Require Import ZArith Lia Field Zmod.Zmod.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Ec.Bls12_381 QedkConcrete.blssquareroots
  QedkConcrete.nativefieldbounds QedkConcrete.primefieldresidues
  QedkConcrete.primefieldarith QedkConcrete.blsprimality QedkConcrete.swuprime.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Bls := Ec.Bls12_381.

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 : 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 curve := @Bls.F_p_SWU_curve.MonadVM_Ec_Curve_Make_include_fargs .
Local Definition swu := @Bls.F_p_SWU_fargs .
Local Notation p := (@PF.modulus field).
Local Notation repr := (Zmod.of_Z p).
Local Notation mul := ((@SWU.F _ _ swu).(Algebra.FIELD.op_star)).
Local Notation add := ((@SWU.F _ _ swu).(Algebra.FIELD.op_plus)).
Local Notation sub := ((@SWU.F _ _ swu).(Algebra.FIELD.op_minus)).
Local Notation divide := ((@SWU.F _ _ swu).(Algebra.FIELD.op_div)).
Local Notation equal := ((@SWU.F _ _ swu).(Algebra.FIELD.op_eq)).
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 := (@SWU.F _ _ swu).(Algebra.FIELD.zero).
Local Definition one := (@SWU.F _ _ swu).(Algebra.FIELD.one).

Lemma bls_swu_parameters :
  0 < a < p ∧ 0 ≤ b < p ∧ z = 11 ∧ zero = 0 ∧ one = 1.
Proof. vm_compute. repeat split; congruence. Qed.

Lemma bls_swu_exceptional_square :
  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 UF
    (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 := 1439257076369429616335874216237123739464560681952602056194427427817441006483822902442243154947945956288630039559824).
  assert (Hq : divide numerator denominator = q).
  { apply (@prime_div_certificate field).
    - exact (@bls_generated_modulus_prime).
    - vm_compute. split; congruence.
    - vm_compute. congruence.
    - vm_compute. split; congruence.
    - vm_compute. reflexivity. }
  change (fst (match @Bls.F_p.sqrt_opt (divide numerator denominator) with
    | Some root ⇒ (true, root)
    | None ⇒ (false, @MonadVM.Option.get Z UF
        (@Bls.F_p.sqrt_opt ((11 × divide numerator denominator) mod p)))
    end) = true).
  rewrite Hq.
  destruct (@primesqrt.prime_sqrt_positive
    (@Bls.F_p.MonadVM_Ec_Algebra_Prime_field_include_fargs)
    (@Bls.F_p.sqrt_opt) q
    ltac:(vm_compute; reflexivity) (@bls_sqrt_available) ltac:(rewrite OCamlZ.legendre_euler; vm_compute; reflexivity)) as [r Hr].
  rewrite Hr. reflexivity.
Qed.

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

Local Definition coordinates := @swuPrimeCoordinates field a b z
  (@SWU.sqrt_ratio _ _ swu UF)
  ((@SWU.P _ _ swu).(Ec.Curve.SWU_method_P_signature.sgn0)).

Lemma bls_swu_coordinates_valid u :
  let '(x,y) := coordinates u in
  @primeFieldBound field x ∧ @primeFieldBound field y ∧
  (repr y × repr y)%Zmod = (repr x × (repr x × repr x + repr a) + repr b)%Zmod.
Proof.
  destruct bls_swu_parameters as [Ha [Hb [Hz [Hzero Hone]]]].
  assert (Hp : 11 < p) by (vm_compute; reflexivity).
  apply swu_prime_coordinates_valid.
  - exact (@bls_generated_modulus_prime).
  - unfold primeFieldBound. lia.
  - lia.
  - exact Hb.
  - unfold primeFieldBound. lia.
  - lia.
  - intros top bottom. exact (@bls_swu_sqrt_ratio_bound UF top bottom).
  - intros top bottom. rewrite Hz.
    exact (@bls_swu_sqrt_ratio_correct UF top bottom).
  - exact bls_swu_exceptional_square.
Qed.

The partial coordinate constructor cannot take its exception branch: either candidate satisfies the curve equation, and sign correction preserves it. No bound on the fallback value is assumed.
Theorem bls_swu_map_valid_nonzero {UP : @Curve.t _ curve} u :
  blsSwuPointValid (@SWU.map_to_curve_simple_swu _ _ swu UF UP u) ∧
  @SWU.map_to_curve_simple_swu _ _ swu UF UP u ≠ Curve.Infinity.
Proof.
  rewrite (@swu_generated_apply field UF _ swu UP) by reflexivity.
  change ((fun point ⇒ blsSwuPointValid point ∧ point ≠ Curve.Infinity) (@swuPrimeApply field a b z
    (@SWU.sqrt_ratio _ _ swu UF)
    ((@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_swu_parameters as [Ha [Hb [Hz [Hzero Hone]]]].
  assert (Hp11 : 11 < p) by (vm_compute; reflexivity).
  apply swu_prime_apply_valid.
  - exact (@bls_generated_modulus_prime).
  - unfold primeFieldBound. lia.
  - lia.
  - exact Hb.
  - unfold primeFieldBound. lia.
  - lia.
  - intros top bottom. exact (@bls_swu_sqrt_ratio_bound UF top bottom).
  - intros top bottom. rewrite Hz.
    exact (@bls_swu_sqrt_ratio_correct UF top bottom).
  - exact bls_swu_exceptional_square.
  - intros x y Hx Hy Heq.
    assert (Hp : 0 < p) by lia.
    assert (HonCurve : @Curve.in_curve _ curve x y = true).
    { change (@PF.op_eq field (@PF.op_star field y y)
        (@PF.op_plus field
          (@PF.op_star field x (@PF.op_plus field (@PF.op_star field x x) a)) b) = true).
      apply prime_eq_true. apply (@prime_residue_injective field).
      - apply prime_mul_bound; exact Hp.
      - apply prime_add_bound; [apply prime_mul_bound; exact Hp|exact Hb].
      - repeat first [rewrite prime_add_residue by
          (repeat first [assumption | apply prime_mul_bound; exact Hp | apply prime_add_bound]) |
          rewrite prime_mul_residue by (try assumption; try lia; vm_compute; reflexivity)].
        exact Heq. }
    unfold Curve.of_coords. rewrite HonCurve.
    destruct (andb _ _) eqn:Hboth.
    + exfalso. apply Bool.andb_true_iff in Hboth. destruct Hboth as [Hx0 Hy0].
      change (@PF.op_eq field x 0 = true) in Hx0.
      change (@PF.op_eq field y 0 = true) in Hy0.
      apply prime_eq_true in Hx0, Hy0. subst x y.
      vm_compute in HonCurve. discriminate.
    + cbn [MonadVM.Option.get OCamlOption.get blsSwuPointValid curvePointBound].
      split; [split; [split; assumption|exact HonCurve]|discriminate].
Qed.

Theorem bls_swu_map_valid {UP : @Curve.t _ curve} u :
  blsSwuPointValid (@SWU.map_to_curve_simple_swu _ _ swu UF UP u).
Proof.
  destruct (@bls_swu_map_valid_nonzero UP u) as [Hvalid _]. exact Hvalid.
Qed.

The isogeny unconditionally reads coordinates, so its input must be a finite point. The auxiliary curve's nonzero constant term rules out the coordinate constructor's special (0,0) infinity case.
Lemma bls_swu_map_coordinates {UP : @Curve.t _ curve} u :
  let '(x,y) := @Curve.coords _ curve (@SWU.map_to_curve_simple_swu _ _ swu UF UP u) in
  @primeFieldBound field x ∧ @primeFieldBound field y ∧
    @Curve.in_curve _ curve x y = true.
Proof.
  destruct (@bls_swu_map_valid_nonzero UP u) as [Hvalid Hnonzero].
  destruct (@SWU.map_to_curve_simple_swu _ _ swu UF UP u) as [x y|];
    [|contradiction].
  destruct Hvalid as [[Hx Hy] HonCurve].
  change (@primeFieldBound field x ∧ @primeFieldBound field y ∧
    @Curve.in_curve _ curve x y = true).
  split; [exact Hx|]. split; assumption.
Qed.

Corollary bls_swu_map_bound {UP : @Curve.t _ curve} u :
  @curvePointBound _ curve (@primeFieldBound field)
    (@SWU.map_to_curve_simple_swu _ _ swu UF UP u).
Proof.
  destruct (@bls_swu_map_valid UP u) as [Hbound _]. exact Hbound.
Qed.
End Map.