Library QedkConcrete.blspointoutputs

BLS12_381 G1 output bounds from concrete coordinate invariants

From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles.Bls12_381 MonadVM.Ec.Bls12_381
  QedkConcrete.nativefieldbounds QedkConcrete.nativepointbounds
  QedkConcrete.precompileoutputs QedkConcrete.modexpoutputs QedkConcrete.nativelistbounds
  QedkConcrete.nativecomplexbounds QedkConcrete.bytewords QedkConcrete.nativeindices.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Bls := Ec.Bls12_381.
Module BlsPC := Precompiles.Bls12_381.

Section BlsOutputs.
Context {defaultC2 : Bls.C_2.(Ec.Curve.Make.Make_result.t)}
  {defaultC1 : Bls.C_1.(Ec.Curve.Make.Make_result.t)}.
Local Definition prime := @Bls.F_p.MonadVM_Ec_Algebra_Prime_field_include_fargs .
Local Definition curve := @Bls.C_1_fargs .
Local Definition decoder := @BlsPC.opened_module_143_fargs defaultC2 defaultC1.
Local Definition coordBound := @primeFieldBound prime.
Local Definition pointBound := @curvePointBound _ curve coordBound.

Lemma bls_modulus_bound : 0 < @PF.modulus prime < 2^512.
Proof. vm_compute. split; reflexivity. Qed.

Lemma bls_field_closed : fieldClosed (@Curve.F _ curve) coordBound.
Proof.
  constructor.
  - apply prime_zero_bound. exact (proj1 bls_modulus_bound).
  - intro n. apply prime_int_bound. exact (proj1 bls_modulus_bound).
  - intros x y Hx Hy. apply prime_add_bound; assumption.
  - intros x y Hx Hy. apply prime_sub_bound; assumption.
  - intros x y _ _. apply prime_mul_bound. exact (proj1 bls_modulus_bound).
  - intros x y _ _. apply prime_div_bound. exact (proj1 bls_modulus_bound).
Qed.

Lemma bls_parameter_bound : coordBound (@Curve.P _ curve).(Ec.Curve.Make_P_signature.a_value).
Proof. apply prime_zero_bound. exact (proj1 bls_modulus_bound). Qed.

Lemma bls_point_parser : nativeEnsures pointBound
  (@EP.point_c1 _ _ _ _ decoder).
Proof.
  apply (ec_point_parse_bound coordBound).
  - intros input output Hout. eapply prime_parse_bound;
      [exact (proj1 bls_modulus_bound)|exact Hout].
  - intros x y point Hx Hy Hpoint. eapply curve_parse_bound;
      [exact Hx|exact Hy|exact Hpoint].
Qed.

Lemma bls_point_encoding point : pointBound point →
  OCamlString.length (@EP.delta_1_inv _ _ _ _ decoder point) = 128.
Proof.
  intro Hpoint.
  pose proof (@curve_coords_bound _ curve coordBound point
    (field_zero_closed _ _ bls_field_closed) Hpoint) as [Hx Hy].
  change (OCamlString.length (@EP.delta_1_inv _ _ _ _ decoder point) = 2 × 64).
  apply (@ec_point_encoding_length _ _ _ _ decoder (Opaque.legacy_witness String.EmptyString) (Opaque.legacy_witness (Ascii.ascii_of_nat 0)) point).
  - change (0 ≤ 64 < 2^62). lia.
  - change (0 ≤ fst (@Curve.coords _ curve point) < 2^(8×64)).
    change (0 ≤ fst (@Curve.coords _ curve point) < @PF.modulus prime) in Hx.
    pose proof bls_modulus_bound. lia.
  - change (0 ≤ snd (@Curve.coords _ curve point) < 2^(8×64)).
    change (0 ≤ snd (@Curve.coords _ curve point) < @PF.modulus prime) in Hy.
    pose proof bls_modulus_bound. lia.
Qed.

Lemma bls_subgroup_parser : nativeEnsures pointBound
  (@EP.point_g1 _ _ _ _ decoder).
Proof.
  unfold EP.point_g1. eapply native_ensures_bind; [apply bls_point_parser|].
  intros point Hpoint. apply native_option_ensures. intros result Hresult.
  change (@Curve.Subgroup.Impl.in_subgroup _ curve
    (@Bls.G_1_fargs defaultC1) point = Some result) in Hresult.
  unfold Curve.Subgroup.Impl.in_subgroup in Hresult.
  destruct (orb _ _) in Hresult; [inversion Hresult; subst; exact Hpoint|discriminate].
Qed.

Local Definition complex := @Bls.F_p2.MonadVM_Ec_Algebra_Complex_extension_include_fargs
    .
Local Definition curve2 := @Bls.C_2_fargs .
Local Definition complexCoordBound := @complexBound _ complex coordBound.
Local Definition point2Bound := @curvePointBound _ curve2 complexCoordBound.

Lemma bls_complex_closed : fieldClosed (@Curve.F _ curve2) complexCoordBound.
Proof.
  constructor.
  - apply complex_zero_bound, bls_field_closed.
  - intro n. apply complex_int_bound, bls_field_closed.
  - intros x y Hx Hy. apply complex_add_bound; [apply bls_field_closed|exact Hx|exact Hy].
  - intros x y Hx Hy. apply complex_sub_bound; [apply bls_field_closed|exact Hx|exact Hy].
  - intros x y Hx Hy. apply complex_mul_bound; [apply bls_field_closed|exact Hx|exact Hy].
  - intros x y Hx Hy. apply complex_div_bound; [apply bls_field_closed| |exact Hx|exact Hy].
    intros value _. apply prime_inv_bound. exact (proj1 bls_modulus_bound).
Qed.

Lemma bls_parameter2_bound : complexCoordBound (@Curve.P _ curve2).(Ec.Curve.Make_P_signature.a_value).
Proof. apply complex_zero_bound, bls_field_closed. Qed.

Context {UE : Basics.Unreachable (@Curve.t _ curve2 → string)}
  {UP : PC.t (@Complex.t _ complex)}.

Lemma bls_complex_parser : nativeEnsures complexCoordBound
  (@EP.f_p2 _ _ _ _ decoder UP).
Proof.
  change (nativeEnsures complexCoordBound
    (PC.op_letdollar (@EP.f_p _ _ _ _ decoder)
      (fun re ⇒ PC.op_letdollar (@EP.f_p _ _ _ _ decoder)
        (fun im ⇒ PC._return (@Complex.t.Build _ complex re im))))).
  eapply native_ensures_bind.
  - apply (ec_field_parse_bound coordBound). intros input output Hout.
    eapply prime_parse_bound; [exact (proj1 bls_modulus_bound)|exact Hout].
  - intros re Hre. eapply native_ensures_bind.
    + apply (ec_field_parse_bound coordBound). intros input output Hout.
      eapply prime_parse_bound; [exact (proj1 bls_modulus_bound)|exact Hout].
    + intros im Him. apply native_ensures_return. split; assumption.
Qed.

Lemma bls_point2_parser : nativeEnsures point2Bound
  (@EP.point_c2 _ _ _ _ decoder UP).
Proof.
  unfold EP.point_c2.
  eapply native_ensures_bind; [apply bls_complex_parser|]. intros x Hx.
  eapply native_ensures_bind; [apply bls_complex_parser|]. intros y Hy.
  apply native_option_ensures. intros point Hpoint.
  eapply curve_parse_bound; [exact Hx|exact Hy|exact Hpoint].
Qed.

Lemma bls_point2_encoding point : point2Bound point →
  OCamlString.length (@EP.delta_2_inv _ _ _ _ decoder point) = 256.
Proof.
  intro Hpoint.
  pose proof (@curve_coords_bound _ curve2 complexCoordBound point
    (field_zero_closed _ _ bls_complex_closed) Hpoint) as [Hx Hy].
  unfold EP.delta_2_inv.
  destruct (@Curve.coords _ curve2 point) as [x y] eqn:Hcoords.
  cbn beta iota zeta in Hx, Hy.
  set (encode := @EP.c1_coord_to_repr _ _ _ _ decoder
    ).
  change (OCamlString.length
    (let '(x,y) := @Curve.coords _ curve2 point in
      String.append (encode x.(Complex.t.re))
        (String.append (encode x.(Complex.t.im))
          (String.append (encode y.(Complex.t.re)) (encode y.(Complex.t.im))))) = 256).
  rewrite Hcoords. cbn beta iota zeta.
  assert (Hencode : ∀ value, coordBound value → OCamlString.length (encode value) = 64).
  { intros value Hvalue. unfold encode.
    apply (@ec_coordinate_encoding_length _ _ _ _ decoder (Opaque.legacy_witness String.EmptyString) (Opaque.legacy_witness (Ascii.ascii_of_nat 0)) value); [change (0 ≤ 64 < 2^62); lia|].
    change (0 ≤ value < 2^(8×64)).
    change (0 ≤ value < @PF.modulus prime) in Hvalue.
    pose proof bls_modulus_bound. lia. }
  destruct Hx as [Hxr Hxi], Hy as [Hyr Hyi].
  rewrite !byte_append_length, !Hencode by assumption. reflexivity.
Qed.
Lemma bls_subgroup2_parser : nativeEnsures point2Bound
  (@EP.point_g2 _ _ _ _ decoder UP).
Proof.
  unfold EP.point_g2. eapply native_ensures_bind; [apply bls_point2_parser|].
  intros point Hpoint. apply native_option_ensures. intros result Hresult.
  change (@Curve.Subgroup.Impl.in_subgroup _ curve2
    (@Bls.G_2_fargs defaultC2) point = Some result) in Hresult.
  unfold Curve.Subgroup.Impl.in_subgroup in Hresult.
  destruct (orb _ _) in Hresult; [inversion Hresult; subst; exact Hpoint|discriminate].
Qed.

End BlsOutputs.

Lemma bls_g1_add_output_bounded msg :
  OCamlString.length (Evmc.Result.t.output_data (@BlsPC.g1_add msg)) ≤ 128.
Proof.
  unfold BlsPC.g1_add. apply native_run_output; [lia|].
  do 2 (apply native_output_bind; intros []).
  eapply native_ensures_bind; [apply (@bls_point_parser BlsPC._rocq_fallback_g1_add_0 BlsPC._rocq_fallback_g1_add_1)|]. intros left Hleft.
  eapply native_ensures_bind; [apply (@bls_point_parser BlsPC._rocq_fallback_g1_add_0 BlsPC._rocq_fallback_g1_add_1)|]. intros right Hright.
  apply native_ensures_return. rewrite (@bls_point_encoding BlsPC._rocq_fallback_g1_add_0 BlsPC._rocq_fallback_g1_add_1); [lia|].
  apply curve_add_bound; [apply bls_field_closed|apply bls_parameter_bound|exact Hleft|exact Hright].
Qed.

Lemma bls_g1_msm_output_bounded {UR : Basics.Unreachable Numeric.U256.Repr.t}
    {UW : Basics.Unreachable Numeric.U256.t} msg :
  OCamlString.length (Evmc.Message.t.input_data msg) < 2^62 →
  OCamlString.length (Evmc.Result.t.output_data (@BlsPC.g1_msm msg)) ≤ 128.
Proof.
  intro Hinput. unfold BlsPC.g1_msm. apply native_run_output; [lia|].
  do 3 (apply native_output_bind; intros []; cbn beta iota zeta).
  eapply native_ensures_bind.
  - eapply native_list_values.
    + apply native_nonnegative_quotient;
        [unfold Byte_string.Bytes.length, MonadVM.String.length, OCamlString.length in *; lia|lia].
    + eapply native_pair_left, (@bls_subgroup_parser BlsPC._rocq_fallback_g1_add_0 BlsPC._rocq_fallback_g1_add_1).
  - intros points Hpoints. apply native_ensures_return.
    rewrite (@bls_point_encoding BlsPC._rocq_fallback_g1_add_0 BlsPC._rocq_fallback_g1_add_1); [lia|].
    eapply native_fold_preserves; [|exact Hpoints|exact I].
    intros accumulator [point scalar] Hacc Hpoint.
    apply curve_add_bound; [apply bls_field_closed|apply bls_parameter_bound|exact Hacc|].
    apply curve_mul_bound; [apply bls_field_closed|apply bls_parameter_bound|exact Hpoint].
Qed.

Lemma bls_g2_add_output_bounded
  {UP : ltac:(let T := type of @BlsPC.g2_add in
    lazymatch T with ?A → _ ⇒ exact A end)} msg :
  OCamlString.length (Evmc.Result.t.output_data (@BlsPC.g2_add UP msg)) ≤ 256.
Proof.
  unfold BlsPC.g2_add. apply native_run_output; [lia|].
  do 2 (apply native_output_bind; intros []).
  eapply native_ensures_bind; [apply (@bls_point2_parser BlsPC._rocq_fallback_g1_add_0 BlsPC._rocq_fallback_g1_add_1 UP)|]. intros left Hleft.
  eapply native_ensures_bind; [apply (@bls_point2_parser BlsPC._rocq_fallback_g1_add_0 BlsPC._rocq_fallback_g1_add_1 UP)|]. intros right Hright.
  apply native_ensures_return. rewrite (@bls_point2_encoding BlsPC._rocq_fallback_g1_add_0 BlsPC._rocq_fallback_g1_add_1); [lia|].
  apply curve_add_bound; [apply bls_complex_closed|apply bls_parameter2_bound|exact Hleft|exact Hright].
Qed.

Lemma bls_g2_msm_output_bounded
  {UP : ltac:(let T := type of @BlsPC.g2_msm in
    lazymatch T with ?A → _ ⇒ exact A end)} {UR : Basics.Unreachable Numeric.U256.Repr.t}
    {UW : Basics.Unreachable Numeric.U256.t} msg :
  OCamlString.length (Evmc.Message.t.input_data msg) < 2^62 →
  OCamlString.length (Evmc.Result.t.output_data (@BlsPC.g2_msm UP msg)) ≤ 256.
Proof.
  intro Hinput. unfold BlsPC.g2_msm. apply native_run_output; [lia|].
  do 3 (apply native_output_bind; intros []; cbn beta iota zeta).
  eapply native_ensures_bind.
  - eapply native_list_values.
    + apply native_nonnegative_quotient;
        [unfold Byte_string.Bytes.length, MonadVM.String.length, OCamlString.length in *; lia|lia].
    + eapply native_pair_left, (@bls_subgroup2_parser BlsPC._rocq_fallback_g1_add_0 BlsPC._rocq_fallback_g1_add_1 UP).
  - intros points Hpoints. apply native_ensures_return.
    rewrite (@bls_point2_encoding BlsPC._rocq_fallback_g1_add_0 BlsPC._rocq_fallback_g1_add_1); [lia|].
    eapply native_fold_preserves; [|exact Hpoints|exact I].
    intros accumulator [point scalar] Hacc Hpoint.
    apply curve_add_bound; [apply bls_complex_closed|apply bls_parameter2_bound|exact Hacc|].
    apply curve_mul_bound; [apply bls_complex_closed|apply bls_parameter2_bound|exact Hpoint].
Qed.

Print Assumptions bls_g1_add_output_bounded.
Print Assumptions bls_g1_msm_output_bounded.
Print Assumptions bls_g2_add_output_bounded.
Print Assumptions bls_g2_msm_output_bounded.