Library MonadVM.Precompiles.Point_evaluation

File generated by rocq-of-ocaml
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import RocqOfOCaml.Settings.

Require Import MonadVM.Support.UintN.
Require MonadVM.Byte_string.
Require MonadVM.Chain.Ethereum.
Require MonadVM.Chain.Monad.
Require MonadVM.Crypto.
Require MonadVM.Ec.Algebra.
Require MonadVM.Ec.Bls12_381.
Require MonadVM.Ec.Curve.
Require MonadVM.Ec.Pairing.
Require MonadVM.Evmc.
Require MonadVM.Gas.
Require MonadVM.Numeric.
Require MonadVM.Option.
Require MonadVM.Precompiles.Utils.
Require MonadVM.Traits.

Require Import MonadVM.Support.Variants.
Require MonadVM.Support.CryptoBlake2.
Require Import RocqOfOCaml.Opaque.
Local Ltac _rocq_witness_type typ :=
  let typ := eval hnf in typ in
  lazymatch typ with
  | list ?a ⇒ let a := _rocq_witness_type a in constr:(list a)
  | option ?a ⇒ let a := _rocq_witness_type a in constr:(option a)
  | prod ?a ?b ⇒ let a := _rocq_witness_type a in
      let b := _rocq_witness_type b in constr:(prod a b)
  | sum ?a ?b ⇒ let a := _rocq_witness_type a in
      let b := _rocq_witness_type b in constr:(sum a b)
  | ?a → ?b ⇒ let a := _rocq_witness_type a in
      let b := _rocq_witness_type b in constr:(a → b)
  | _ ⇒ typ
  end.
Definition versioned_hash_version_kzg : ascii := "001" % char.

Definition kzg_commitment_to_versioned_hash (kzg : string) : string :=
  let h_value :=
    MonadVM.Crypto.sha_256
      (MonadVM.Byte_string.B48.(MonadVM.Byte_string.Fixed.Fixed_result.to_bytes)
        kzg) in
  MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.init)
    (fun (function_parameter : int) ⇒
      match function_parameter with
      | 0 ⇒ versioned_hash_version_kzg
      | i_value ⇒
        MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.op_pointdollarlparenrparen)
          h_value i_value
      end).

Definition field_elements_per_blob : MonadVM.Support.UintN.t :=
  MonadVM.Support.UintN.op_tildedollar 4096.

Definition bls_modulus : MonadVM.Numeric.Integer.t :=
  MonadVM.Support.UintN.as_signed MonadVM.Ec.Bls12_381.q_value.

Definition precompile_result : MonadVM.Byte_string.Bytes.t :=
  String.append
    (MonadVM.Numeric.U256.to_repr_bytes
      (MonadVM.Numeric.U256.of_uint_exn field_elements_per_blob))
    (MonadVM.Numeric.U256.to_repr_bytes
      (MonadVM.Numeric.U256.of_integer_exn bls_modulus)).

Module flags.
  Record record : Set := Build {
    compressed : bool;
    infinity : bool;
    negative : bool;
  }.
  Definition with_compressed compressed (r : record) :=
    Build compressed r.(infinity) r.(negative).
  Definition with_infinity infinity (r : record) :=
    Build r.(compressed) infinity r.(negative).
  Definition with_negative negative (r : record) :=
    Build r.(compressed) r.(infinity) negative.
End flags.
Definition flags := flags.record.

Definition flag_compressed : int := 128.

Definition flag_infinity : int := 64.

Definition flag_negative : int := 32.

Definition read_flags (c_value : ascii) : flags :=
  let b_value := RocqOfOCaml.OCamlChar.code c_value in
  {|
    MonadVM.Precompiles.Point_evaluation.flags.compressed :=
      RocqOfOCaml.OCamlInt.not_equal
        (RocqOfOCaml.OCamlInt.logand b_value flag_compressed) 0;
    MonadVM.Precompiles.Point_evaluation.flags.infinity :=
      RocqOfOCaml.OCamlInt.not_equal
        (RocqOfOCaml.OCamlInt.logand b_value flag_infinity) 0;
    MonadVM.Precompiles.Point_evaluation.flags.negative :=
      RocqOfOCaml.OCamlInt.not_equal
        (RocqOfOCaml.OCamlInt.logand b_value flag_negative) 0; |}.

Definition _rocq_fallback_infinity_G1_0 :
ltac:(let typ := constr:(ltac:(first [unshelve refine ((ascii)%type
)]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

Definition infinity_G1 : string :=
  MonadVM.Byte_string.B48.(MonadVM.Byte_string.Fixed.Fixed_result.init)
    (fun (function_parameter : int) ⇒
      match function_parameter with
      | 0 ⇒
        (RocqOfOCaml.OCamlChar.chr
          (H := RocqOfOCaml.Opaque.legacy_witness _rocq_fallback_infinity_G1_0))
          (RocqOfOCaml.OCamlInt.logor flag_compressed flag_infinity)
      | _ ⇒ "000" % char
      end).

Definition _rocq_fallback_decompress_G1_0 :
ltac:(let typ := constr:(ltac:(first [unshelve refine ((MonadVM.Ec.Bls12_381.C_1.(MonadVM.Ec.Curve.Make.Make_result.t))%type
)]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

Definition decompress_G1 (bs : string)
  : option
    (MonadVM.Ec.Bls12_381.G_1 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G1_0)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.t) :=
  let '{|
    MonadVM.Precompiles.Point_evaluation.flags.compressed := compressed;
      MonadVM.Precompiles.Point_evaluation.flags.infinity := infinity;
      MonadVM.Precompiles.Point_evaluation.flags.negative := negative
      |} :=
    read_flags
      (MonadVM.Byte_string.B48.(MonadVM.Byte_string.Fixed.Fixed_result.op_pointdollarlparenrparen)
        bs 0) in
  if negb compressed then
    None
  else
    if infinity then
      MonadVM.Option.op_letdollar
        (MonadVM.Option.ensure
          (MonadVM.Byte_string.B48.(MonadVM.Byte_string.Fixed.Fixed_result.op_eq)
            bs infinity_G1))
        (fun function_parameter ⇒
          let '_ := function_parameter in
          MonadVM.Option._return
            (MonadVM.Ec.Bls12_381.G_1 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G1_0)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.zero))
    else
      MonadVM.Option.op_letdollar
        (MonadVM.Ec.Bls12_381.F_p.of_uint_opt
          (MonadVM.Support.UintN.modulo
            (MonadVM.Support.UintN.of_bytes_be
              (MonadVM.Byte_string.B48.(MonadVM.Byte_string.Fixed.Fixed_result.to_bytes)
                bs))
            (MonadVM.Support.UintN.op_starstar
              (MonadVM.Support.UintN.op_tildedollar 2) 381)))
        (fun x_value ⇒
          MonadVM.Option.op_letdollar
            (MonadVM.Option.op_letdollar
              (MonadVM.Ec.Bls12_381.F_p.sqrt_opt
                (MonadVM.Ec.Bls12_381.F_p.op_plus
                  (MonadVM.Ec.Bls12_381.F_p.op_star
                    (MonadVM.Ec.Bls12_381.F_p.op_star x_value x_value) x_value)
                  (MonadVM.Ec.Bls12_381.F_p.op_tildedollar 4)))
              (fun y_abs ⇒
                let y_is_large :=
                  MonadVM.Numeric.Integer.op_gteq
                    (MonadVM.Numeric.Integer.op_star y_abs
                      (MonadVM.Numeric.Integer.op_tildedollar 2))
                    (MonadVM.Support.UintN.as_signed
                      MonadVM.Ec.Bls12_381.p_value) in
                MonadVM.Option._return
                  (if negb (Bool.eqb y_is_large negative) then
                    MonadVM.Ec.Bls12_381.F_p.op_minus
                      MonadVM.Ec.Bls12_381.F_p.zero y_abs
                  else
                    y_abs)))
            (fun y_value ⇒
              (MonadVM.Ec.Bls12_381.G_1 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G1_0)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.of_coords)
                x_value y_value)).

Definition infinity_G2 : string :=
  MonadVM.Byte_string.B96.(MonadVM.Byte_string.Fixed.Fixed_result.init)
    (fun (function_parameter : int) ⇒
      match function_parameter with
      | 0 ⇒
        (RocqOfOCaml.OCamlChar.chr
          (H := RocqOfOCaml.Opaque.legacy_witness _rocq_fallback_infinity_G1_0))
          (RocqOfOCaml.OCamlInt.logor flag_compressed flag_infinity)
      | _ ⇒ "000" % char
      end).

Definition _rocq_fallback_decompress_G2_0 :
ltac:(let typ := constr:(ltac:(first [unshelve refine ((MonadVM.Byte_string.Bytes.t)%type
)]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

Definition _rocq_fallback_decompress_G2_1 :
ltac:(let typ := constr:(ltac:(first [unshelve refine ((MonadVM.Ec.Bls12_381.C_2.(MonadVM.Ec.Curve.Make.Make_result.t))%type
)]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
_rocq_fallback_decompress_G2_0 | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

Definition decompress_G2 (bs : string)
  : option
    (MonadVM.Ec.Bls12_381.G_2 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G2_1)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.t) :=
  let '{|
    MonadVM.Precompiles.Point_evaluation.flags.compressed := compressed;
      MonadVM.Precompiles.Point_evaluation.flags.infinity := infinity;
      MonadVM.Precompiles.Point_evaluation.flags.negative := negative
      |} :=
    read_flags
      (MonadVM.Byte_string.B96.(MonadVM.Byte_string.Fixed.Fixed_result.op_pointdollarlparenrparen)
        bs 0) in
  if negb compressed then
    None
  else
    if infinity then
      MonadVM.Option.op_letdollar
        (MonadVM.Option.ensure
          (MonadVM.Byte_string.B96.(MonadVM.Byte_string.Fixed.Fixed_result.op_eq)
            bs infinity_G2))
        (fun function_parameter ⇒
          let '_ := function_parameter in
          MonadVM.Option._return
            (MonadVM.Ec.Bls12_381.G_2 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G2_1)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.zero))
    else
      let bs :=
        MonadVM.Byte_string.B96.(MonadVM.Byte_string.Fixed.Fixed_result.to_bytes)
          bs in
      MonadVM.Option.op_letdollar
        (MonadVM.Ec.Bls12_381.F_p.of_uint_opt
          (MonadVM.Support.UintN.modulo
            (MonadVM.Support.UintN.of_bytes_be
              ((MonadVM.Byte_string.Bytes.sub (_rocq_fallback_decompress_G2_0))
                bs 0 48))
            (MonadVM.Support.UintN.op_starstar
              (MonadVM.Support.UintN.op_tildedollar 2) 381)))
        (fun x_c1 ⇒
          MonadVM.Option.op_letdollar
            (MonadVM.Ec.Bls12_381.F_p.of_uint_opt
              (MonadVM.Support.UintN.of_bytes_be
                ((MonadVM.Byte_string.Bytes.sub
                  (_rocq_fallback_decompress_G2_0)) bs 48 48)))
            (fun x_c0 ⇒
              let x_value :=
                {| MonadVM.Ec.Algebra.Complex_extension.t.re := x_c0;
                  MonadVM.Ec.Algebra.Complex_extension.t.im := x_c1; |} in
              MonadVM.Option.op_letdollar
                (MonadVM.Option.op_letdollar
                  (MonadVM.Ec.Bls12_381.F_p2.sqrt_opt
                    (MonadVM.Ec.Bls12_381.F_p2.op_plus
                      (MonadVM.Ec.Bls12_381.F_p2.op_star
                        (MonadVM.Ec.Bls12_381.F_p2.op_star x_value x_value)
                        x_value)
                      (MonadVM.Ec.Bls12_381.F_p2.op_plus
                        (MonadVM.Ec.Bls12_381.F_p2.op_tildedollar 4)
                        (MonadVM.Ec.Bls12_381.F_p2.op_star
                          MonadVM.Ec.Bls12_381.F_p2.i_value
                          (MonadVM.Ec.Bls12_381.F_p2.op_tildedollar 4)))))
                  (fun y_abs ⇒
                    let '{|
                      MonadVM.Ec.Algebra.Complex_extension.t.re := y_re;
                        MonadVM.Ec.Algebra.Complex_extension.t.im := y_im
                        |} := y_abs in
                    let dominant :=
                      if
                        MonadVM.Ec.Bls12_381.F_p.op_ltgt y_im
                          MonadVM.Ec.Bls12_381.F_p.zero
                      then
                        y_im
                      else
                        y_re in
                    let sign_y :=
                      MonadVM.Numeric.Integer.op_gteq
                        (MonadVM.Numeric.Integer.op_star dominant
                          (MonadVM.Numeric.Integer.op_tildedollar 2))
                        (MonadVM.Support.UintN.as_signed
                          MonadVM.Ec.Bls12_381.p_value) in
                    MonadVM.Option._return
                      (if negb (Bool.eqb sign_y negative) then
                        MonadVM.Ec.Bls12_381.F_p2.op_minus
                          MonadVM.Ec.Bls12_381.F_p2.zero y_abs
                      else
                        y_abs)))
                (fun y_value ⇒
                  (MonadVM.Ec.Bls12_381.G_2 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G2_1)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.of_coords)
                    x_value y_value))).

Definition bytes_to_bls_field (bs : string)
  : option MonadVM.Numeric.Integer.t :=
  let x_value :=
    MonadVM.Numeric.U256.to_integer (MonadVM.Numeric.U256.of_repr bs) in
  if
    andb (MonadVM.Numeric.Integer.op_gteq x_value MonadVM.Numeric.Integer.zero)
      (MonadVM.Numeric.Integer.op_lt x_value
        (MonadVM.Support.UintN.as_signed MonadVM.Ec.Bls12_381.curve_order))
  then
    Some x_value
  else
    None.

Definition bytes_to_kzg_commitment (commitment : string)
  : option
    (MonadVM.Ec.Bls12_381.G_1 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G1_0)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.t) :=
  decompress_G1 commitment.

Definition bytes_to_kzg_proof (proof : string)
  : option
    (MonadVM.Ec.Bls12_381.G_1 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G1_0)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.t) :=
  decompress_G1 proof.

Definition _rocq_fallback_kzg_setup_G2_monomial_1_0 :
ltac:(let typ := constr:(ltac:(first [unshelve refine ((MonadVM.Ec.Bls12_381.G_2.(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.t))%type
)]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

Definition kzg_setup_G2_monomial_1
  : (MonadVM.Ec.Bls12_381.G_2 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G2_1)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.t) :=
  (MonadVM.Option.get (_rocq_fallback_kzg_setup_G2_monomial_1_0))
    (decompress_G2
      (MonadVM.Byte_string.B96.(MonadVM.Byte_string.Fixed.Fixed_result.of_hex_string)
        None
        "0xb5bfd7dd8cdeb128843bc287230af38926187075cbfbefa81009a2ce615ac53d2914e5870cb452d2afaaab24f3499f72185cbfee53492714734429b7b38608e23926c911cceceac9a36851477ba4c60b087041de621000edc98edada20c1def2")).

Definition verify_kzg_proof
  (commitment : string) (z_value : string) (y_value : string) (proof : string)
  : option unit :=
  MonadVM.Option.op_letdollar (bytes_to_kzg_commitment commitment)
    (fun commitment ⇒
      MonadVM.Option.op_letdollar (bytes_to_bls_field z_value)
        (fun z_value ⇒
          MonadVM.Option.op_letdollar (bytes_to_bls_field y_value)
            (fun y_value ⇒
              MonadVM.Option.op_letdollar (bytes_to_kzg_proof proof)
                (fun proof ⇒
                  let x_minus_z :=
                    (MonadVM.Ec.Bls12_381.G_2 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G2_1)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.op_plus)
                      kzg_setup_G2_monomial_1
                      ((MonadVM.Ec.Bls12_381.G_2 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G2_1)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.op_star)
                        (MonadVM.Numeric.Integer.as_unsigned_exn
                          (MonadVM.Numeric.Integer.modulo
                            (MonadVM.Numeric.Integer.op_minus bls_modulus
                              z_value) bls_modulus))
                        ((MonadVM.Ec.Bls12_381.G_2 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G2_1)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.generator)))
                    in
                  let p_minus_y :=
                    (MonadVM.Ec.Bls12_381.G_1 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G1_0)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.op_plus)
                      commitment
                      ((MonadVM.Ec.Bls12_381.G_1 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G1_0)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.op_star)
                        (MonadVM.Numeric.Integer.as_unsigned_exn
                          (MonadVM.Numeric.Integer.modulo
                            (MonadVM.Numeric.Integer.op_minus bls_modulus
                              y_value) bls_modulus))
                        ((MonadVM.Ec.Bls12_381.G_1 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G1_0)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.generator)))
                    in
                  MonadVM.Option.ensure
                    (MonadVM.Ec.Bls12_381.pairing_check
                      (Datatypes.cons
                        (p_minus_y,
                          ((MonadVM.Ec.Bls12_381.G_2 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G2_1)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.neg)
                            ((MonadVM.Ec.Bls12_381.G_2 (_rocq_module_assumption_0 := _rocq_fallback_decompress_G2_1)).(MonadVM.Ec.Curve.Make.Subgroup.Subgroup_result.generator))))
                        (Datatypes.cons (proof, x_minus_z) nil))))))).

Definition address : MonadVM.Chain.Ethereum.Address.t :=
  MonadVM.Chain.Ethereum.Address.of_hex_string None "0x0a".

Definition precompile (msg : MonadVM.Evmc.Message.t) : MonadVM.Evmc.Result.t :=
  MonadVM.Precompiles.Utils.Precompile.run msg
    (MonadVM.Precompiles.Utils.Precompile.op_letdollar
      (MonadVM.Precompiles.Utils.Precompile.ensure
        (Z.eqb
          (MonadVM.Byte_string.Bytes.length
            msg.(MonadVM.Evmc.Message.t.input_data)) 192))
      (fun function_parameter ⇒
        let '_ := function_parameter in
        MonadVM.Precompiles.Utils.Precompile.op_letdollar
          (MonadVM.Precompiles.Utils.Precompile.spend_gas
            (MonadVM.Gas.of_int 200000))
          (fun function_parameter ⇒
            let '_ := function_parameter in
            MonadVM.Precompiles.Utils.Precompile.op_letdollar
              MonadVM.Precompiles.Utils.Precompile.b32
              (fun versioned_hash ⇒
                MonadVM.Precompiles.Utils.Precompile.op_letdollar
                  MonadVM.Precompiles.Utils.Precompile.b32
                  (fun z_value ⇒
                    MonadVM.Precompiles.Utils.Precompile.op_letdollar
                      MonadVM.Precompiles.Utils.Precompile.b32
                      (fun y_value ⇒
                        MonadVM.Precompiles.Utils.Precompile.op_letdollar
                          MonadVM.Precompiles.Utils.Precompile.b48
                          (fun commitment ⇒
                            MonadVM.Precompiles.Utils.Precompile.op_letdollar
                              MonadVM.Precompiles.Utils.Precompile.b48
                              (fun proof ⇒
                                MonadVM.Precompiles.Utils.Precompile.op_letdollar
                                  (MonadVM.Precompiles.Utils.Precompile.ensure
                                    (MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.op_eq)
                                      versioned_hash
                                      (kzg_commitment_to_versioned_hash
                                        commitment)))
                                  (fun function_parameter ⇒
                                    let '_ := function_parameter in
                                    MonadVM.Precompiles.Utils.Precompile.op_letdollar
                                      (MonadVM.Precompiles.Utils.Precompile.Option.or_fail
                                        (verify_kzg_proof commitment z_value
                                          y_value proof))
                                      (fun function_parameter ⇒
                                        let '_ := function_parameter in
                                        MonadVM.Precompiles.Utils.Precompile._return
                                          precompile_result)))))))))).