Library QedkConcrete.blsmapbounds

BLS output bounds from the approved cofactor identities

blsG1CofactorOrder and blsG2CofactorOrder are explicit mathematical premises, approved on 2026-10-24, not global axioms or VM contracts. Everything surrounding the subgroup check is derived from the generated code, including the partial coordinate constructor and its independently supplied fallback values.
Informal argument: cofactor clearing removes the component outside the prime-order subgroup. The generated h_eff constants are exactly those of RFC 9380 sections 8.8.1 and 8.8.2; section 7 specifies multiplication by these scalars as cofactor clearing. Thus Q = h_eff × P is in the subgroup of order r, and r × Q is infinity. The subsequent membership test therefore succeeds, including when Q itself is infinity. This argument assumes the identity for the concrete generated point operations; it does not assume anything about their serializers or EVM effects.
TODO: derive these two identities in Rocq instead of taking them as premises. No change to OCaml failure behavior is needed or made. Reference: https://www.rfc-editor.org/rfc/rfc9380.htmlsection-7
From Stdlib Require Import ZArith Bool.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Ec.Bls12_381
  QedkConcrete.nativefieldbounds QedkConcrete.nativecomplexbounds
  QedkConcrete.curvevalidity QedkConcrete.blscurvevalidity
  QedkConcrete.blssquareroots QedkConcrete.blsswumap QedkConcrete.blsswu2map
  QedkConcrete.blsisogeny3 QedkConcrete.blsisogeny11.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Bls := Ec.Bls12_381.
Module Poly := Algebra.Polynomial_ring.
Module SWU := Ec.Curve.SWU_method.

Section Maps.
Local Definition prime := @Bls.F_p.MonadVM_Ec_Algebra_Prime_field_include_fargs .
Local Definition curve1 := @Bls.C_1_fargs .
Local Definition auxiliary1 := @Bls.F_p_SWU_curve.MonadVM_Ec_Curve_Make_include_fargs .
Local Notation bounded1 := (@primeFieldBound prime).
Local Notation pointValid1 := (@curvePointValid _ curve1 bounded1).
Local Notation pointBound1 := (@curvePointBound _ curve1 bounded1).

Definition blsG1CofactorOrder : Prop :=
  ∀ point, pointValid1 point →
    @Curve.is_zero _ curve1
      (@Curve.op_star _ curve1 (@Bls.curve_order)
        (@Curve.op_star _ curve1 (@Bls.F_p_SWU_curve.h_eff) point)) = true.

Section G1.
Context {UP : @Curve.t _ curve1}.
Local Notation poly := (@Bls.F_p_SWU_curve.F_px_fargs).
Local Notation eval := (@Poly.eval _ poly).
Local Notation xnum := (@Bls.F_p_SWU_curve.x_num).
Local Notation xden := (@Bls.F_p_SWU_curve.x_den).
Local Notation ynum := (@Bls.F_p_SWU_curve.y_num).
Local Notation yden := (@Bls.F_p_SWU_curve.y_den).
Local Notation divide := (@PF.op_div prime).
Local Notation mul := (@PF.op_star prime).
Local Notation subgroup := (@Curve.Subgroup.Impl.in_subgroup _ curve1
  (@Bls.G_1_fargs UP)).

Lemma bls_isogeny_g1_bound pt : blsG1CofactorOrder →
  (let '(x,y) := @Curve.coords _ auxiliary1 pt in
    @Curve.in_curve _ auxiliary1 x y = true) →
  pointBound1 (@Bls.F_p_SWU_curve.isogeny UP pt).
Proof.
  intros Horder.
  unfold Bls.F_p_SWU_curve.isogeny.
  change ((let '(x,y) := @Curve.coords _ auxiliary1 pt in
    @Curve.in_curve _ auxiliary1 x y = true) →
    pointBound1 (let '(x,y) := @Curve.coords _ auxiliary1 pt in
      if @PF.op_eq prime (eval xden x) 0 then Curve.Infinity else
        @MonadVM.Option.get _ UP
          (subgroup (@Curve.op_star _ curve1 (@Bls.F_p_SWU_curve.h_eff)
            (@MonadVM.Option.get _ UP (@Curve.of_coords _ curve1
              (divide (eval xnum x) (eval xden x))
              (mul (divide (eval ynum x) (eval yden x)) y))))))).
  destruct (@Curve.coords _ auxiliary1 pt) as [x y]. intro Hcurve.
  destruct (@PF.op_eq prime (eval xden x) 0) eqn:Hkernel; [exact I|].
  destruct (@bls_iso11_coordinates_valid x y Hcurve Hkernel)
    as [Hx [Hy Htarget]].
  destruct (curve_construct_valid bounded1 _ _ Hx Hy Htarget)
    as [point [Hparse Hvalid]].
  change (@Curve.of_coords _ curve1 (divide (eval xnum x) (eval xden x))
    (mul (divide (eval ynum x) (eval yden x)) y) = Some point) in Hparse.
  rewrite Hparse. cbn [MonadVM.Option.get OCamlOption.get].
  assert (Hsubgroup : subgroup
    (@Curve.op_star _ curve1 (@Bls.F_p_SWU_curve.h_eff) point) =
    Some (@Curve.op_star _ curve1 (@Bls.F_p_SWU_curve.h_eff) point)).
  { unfold Curve.Subgroup.Impl.in_subgroup.
    change ((if @Curve.is_zero _ curve1
      (@Curve.op_star _ curve1 (@Bls.F_p_SWU_curve.h_eff) point) ||
      @Curve.is_zero _ curve1
      (@Curve.op_star _ curve1 (@Bls.curve_order)
        (@Curve.op_star _ curve1 (@Bls.F_p_SWU_curve.h_eff) point))
      then Some (@Curve.op_star _ curve1 (@Bls.F_p_SWU_curve.h_eff) point)
      else None) = Some (@Curve.op_star _ curve1 (@Bls.F_p_SWU_curve.h_eff) point)).
    rewrite (Horder point Hvalid), Bool.orb_true_r. reflexivity. }
  rewrite Hsubgroup. cbn [MonadVM.Option.get OCamlOption.get].
  exact (proj1 (@bls_g1_mul_valid (Opaque.legacy_witness 0%N) _ point Hvalid)).
Qed.

Lemma bls_map_g1_bound u : blsG1CofactorOrder →
  pointBound1 (@Bls.map_fp_to_g1 UP u).
Proof.
  intro Horder. unfold Bls.map_fp_to_g1.
  apply bls_isogeny_g1_bound; [exact Horder|].
  pose proof (@bls_swu_map_coordinates Bls._rocq_fallback_map_fp_to_g1_1 Bls._rocq_fallback_map_fp_to_g1_0 u) as Hcoords.
  cbv beta iota zeta delta [Bls.F_p_SWU SWU.functor
    SWU.SWU_method_result.map_to_curve_simple_swu
    blsswumap.curve blsswumap.swu] in Hcoords |- ×.
  change (let '(x,y) := @Curve.coords _ auxiliary1
    (@SWU.map_to_curve_simple_swu _ _ (@Bls.F_p_SWU_fargs)
      Bls._rocq_fallback_map_fp_to_g1_1 Bls._rocq_fallback_map_fp_to_g1_0 u) in
    bounded1 x ∧ bounded1 y ∧ @Curve.in_curve _ auxiliary1 x y = true) in Hcoords.
  destruct (@Curve.coords _ auxiliary1
    (@SWU.map_to_curve_simple_swu _ _ (@Bls.F_p_SWU_fargs)
      Bls._rocq_fallback_map_fp_to_g1_1 Bls._rocq_fallback_map_fp_to_g1_0 u)) as [x y] in Hcoords |- ×.
  exact (proj2 (proj2 Hcoords)).
Qed.
End G1.

Local Definition complex := @Bls.F_p2.MonadVM_Ec_Algebra_Complex_extension_include_fargs .
Local Definition curve2 := @Bls.C_2_fargs .
Local Definition auxiliary2 := @Bls.F_p2_SWU_curve.MonadVM_Ec_Curve_Make_include_fargs .
Local Notation elt := (@Complex.t _ complex).
Local Notation bounded2 := (@complexBound _ complex bounded1).
Local Notation pointValid2 := (@curvePointValid _ curve2 bounded2).
Local Notation pointBound2 := (@curvePointBound _ curve2 bounded2).

Definition blsG2CofactorOrder : Prop :=
  ∀ point, pointValid2 point →
    @Curve.is_zero _ curve2
      (@Curve.op_star _ curve2 (@Bls.curve_order)
        (@Curve.op_star _ curve2 (@Bls.F_p2_SWU_curve.h_eff) point)) = true.

Section G2.
Context {UP : @Curve.t _ curve2}.
Local Notation poly := (@Bls.F_p2_SWU_curve.F_p2x_fargs).
Local Notation eval := (@Poly.eval _ poly).
Local Notation xnum := (@Bls.F_p2_SWU_curve.x_num).
Local Notation xden := (@Bls.F_p2_SWU_curve.x_den).
Local Notation ynum := (@Bls.F_p2_SWU_curve.y_num).
Local Notation yden := (@Bls.F_p2_SWU_curve.y_den).
Local Notation divide := (@Complex.op_div _ complex).
Local Notation mul := (@Complex.op_star _ complex).
Local Notation subgroup := (@Curve.Subgroup.Impl.in_subgroup _ curve2
  (@Bls.G_2_fargs UP)).

Lemma bls_isogeny_g2_bound pt : blsG2CofactorOrder →
  (let '(x,y) := @Curve.coords _ auxiliary2 pt in
    @Curve.in_curve _ auxiliary2 x y = true) →
  pointBound2 (@Bls.F_p2_SWU_curve.isogeny UP pt).
Proof.
  intros Horder.
  unfold Bls.F_p2_SWU_curve.isogeny.
  change ((let '(x,y) := @Curve.coords _ auxiliary2 pt in
    @Curve.in_curve _ auxiliary2 x y = true) →
    pointBound2 (let '(x,y) := @Curve.coords _ auxiliary2 pt in
      if @Complex.op_eq _ complex (eval xden x) (@Complex.zero _ complex)
      then Curve.Infinity else
        @MonadVM.Option.get _ UP
          (subgroup (@Curve.op_star _ curve2 (@Bls.F_p2_SWU_curve.h_eff)
            (@MonadVM.Option.get _ UP (@Curve.of_coords _ curve2
              (divide (eval xnum x) (eval xden x))
              (mul (divide (eval ynum x) (eval yden x)) y))))))).
  destruct (@Curve.coords _ auxiliary2 pt) as [x y]. intro Hcurve.
  destruct (@Complex.op_eq _ complex (eval xden x) (@Complex.zero _ complex))
    eqn:Hkernel; [exact I|].
  destruct (@bls_iso3_coordinates_valid x y Hcurve Hkernel)
    as [Hx [Hy Htarget]].
  destruct (curve_construct_valid bounded2 _ _ Hx Hy Htarget)
    as [point [Hparse Hvalid]].
  change (@Curve.of_coords _ curve2 (divide (eval xnum x) (eval xden x))
    (mul (divide (eval ynum x) (eval yden x)) y) = Some point) in Hparse.
  rewrite Hparse. cbn [MonadVM.Option.get OCamlOption.get].
  assert (Hsubgroup : subgroup
    (@Curve.op_star _ curve2 (@Bls.F_p2_SWU_curve.h_eff) point) =
    Some (@Curve.op_star _ curve2 (@Bls.F_p2_SWU_curve.h_eff) point)).
  { unfold Curve.Subgroup.Impl.in_subgroup.
    change ((if @Curve.is_zero _ curve2
      (@Curve.op_star _ curve2 (@Bls.F_p2_SWU_curve.h_eff) point) ||
      @Curve.is_zero _ curve2
      (@Curve.op_star _ curve2 (@Bls.curve_order)
        (@Curve.op_star _ curve2 (@Bls.F_p2_SWU_curve.h_eff) point))
      then Some (@Curve.op_star _ curve2 (@Bls.F_p2_SWU_curve.h_eff) point)
      else None) = Some (@Curve.op_star _ curve2 (@Bls.F_p2_SWU_curve.h_eff) point)).
    rewrite (Horder point Hvalid), Bool.orb_true_r. reflexivity. }
  rewrite Hsubgroup. cbn [MonadVM.Option.get OCamlOption.get].
  exact (proj1 (@bls_g2_mul_valid (Opaque.legacy_witness 0%N) _ point Hvalid)).
Qed.

Lemma bls_map_g2_bound u : blsG2CofactorOrder →
  pointBound2 (@Bls.map_fp2_to_g2 UP u).
Proof.
  intro Horder. unfold Bls.map_fp2_to_g2.
  apply bls_isogeny_g2_bound; [exact Horder|].
  pose proof (@bls_swu2_map_coordinates Bls._rocq_fallback_map_fp2_to_g2_1 Bls._rocq_fallback_map_fp2_to_g2_0 u) as Hcoords.
  cbv beta iota zeta delta [Bls.F_p2_SWU SWU.functor
    SWU.SWU_method_result.map_to_curve_simple_swu
    blsswu2map.curve blsswu2map.swu] in Hcoords |- ×.
  change (let '(x,y) := @Curve.coords _ auxiliary2
    (@SWU.map_to_curve_simple_swu _ _ (@Bls.F_p2_SWU_fargs)
      Bls._rocq_fallback_map_fp2_to_g2_1 Bls._rocq_fallback_map_fp2_to_g2_0 u) in
    bounded2 x ∧ bounded2 y ∧ @Curve.in_curve _ auxiliary2 x y = true) in Hcoords.
  destruct (@Curve.coords _ auxiliary2
    (@SWU.map_to_curve_simple_swu _ _ (@Bls.F_p2_SWU_fargs)
      Bls._rocq_fallback_map_fp2_to_g2_1 Bls._rocq_fallback_map_fp2_to_g2_0 u)) as [x y] in Hcoords |- ×.
  exact (proj2 (proj2 Hcoords)).
Qed.
End G2.
End Maps.