Library MonadVM.Precompiles.Modexp

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.Evmc.
Require MonadVM.Gas.
Require MonadVM.Numeric.
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.
Module Make_Revision_signature.
  Record signature : Type := {
    revision : MonadVM.Chain.Monad.Revision.active;
  }.
End Make_Revision_signature.
Definition Make_Revision_signature := Make_Revision_signature.signature.

Module Make.
  Class FArgs := {
    Revision : Make_Revision_signature;
  }.

  Definition _rocq_fallback_calculate_multiplication_complexity_0 `{_fargs : FArgs} : ltac:(let typ := constr:(ltac:(first [unshelve refine ((MonadVM.Support.UintN.Impl.t
  → MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.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 calculate_multiplication_complexity `{_fargs : FArgs}
    : MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
    MonadVM.Support.UintN.t :=
    match Revision.(Make_Revision_signature.revision) with
    | MonadVM.Support.Variants.Revision.Eight ⇒
      fun (base_length : MonadVM.Support.UintN.Impl.t) ⇒
        fun (modulus_length : MonadVM.Support.UintN.Impl.t) ⇒
          let max_length := MonadVM.Support.UintN.max base_length modulus_length
            in
          let words :=
            MonadVM.Support.UintN.ceil_div max_length
              (MonadVM.Support.UintN.op_tildedollar 8) in
          MonadVM.Support.UintN.op_starstar words 2
    |
      (MonadVM.Support.Variants.Revision.Nine |
      MonadVM.Support.Variants.Revision.Eleven |
      MonadVM.Support.Variants.Revision.Setcode _) ⇒
      fun (base_length : MonadVM.Support.UintN.Impl.t) ⇒
        fun (modulus_length : MonadVM.Support.UintN.Impl.t) ⇒
          let max_length := MonadVM.Support.UintN.max base_length modulus_length
            in
          let words :=
            MonadVM.Support.UintN.ceil_div max_length
              (MonadVM.Support.UintN.op_tildedollar 8) in
          if
            MonadVM.Support.UintN.op_gt max_length
              (MonadVM.Support.UintN.op_tildedollar 32)
          then
            MonadVM.Support.UintN.op_star
              (MonadVM.Support.UintN.op_tildedollar 2)
              (MonadVM.Support.UintN.op_starstar words 2)
          else
            MonadVM.Support.UintN.op_tildedollar 16
    | _ ⇒ _rocq_fallback_calculate_multiplication_complexity_0
    end.

  Definition _rocq_fallback_calculate_iteration_count_0 `{_fargs : FArgs} : ltac:(let typ := constr:(ltac:(first [unshelve refine ((MonadVM.Support.UintN.Impl.t
  → MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.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 calculate_iteration_count `{_fargs : FArgs}
    : MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
    MonadVM.Support.UintN.Impl.t :=
    let bit_length_minus_one (n_value : MonadVM.Support.UintN.t)
      : MonadVM.Support.UintN.t :=
      MonadVM.Support.UintN.of_int
        (let b_value := MonadVM.Support.UintN.significant_bits n_value in
        if Z.eqb b_value 0 then
          0
        else
          RocqOfOCaml.OCamlInt.sub b_value 1) in
    match Revision.(Make_Revision_signature.revision) with
    | MonadVM.Support.Variants.Revision.Eight ⇒
      fun (exponent_length : MonadVM.Support.UintN.Impl.t) ⇒
        fun (exponent_upper_256_bits : MonadVM.Support.UintN.Impl.t) ⇒
          let k_value :=
            match
              (tt,
                (let '_ := tt in
                andb
                  (MonadVM.Support.UintN.op_lteq exponent_length
                    (MonadVM.Support.UintN.op_tildedollar 32))
                  (MonadVM.Support.UintN.op_eq exponent_upper_256_bits
                    MonadVM.Support.UintN.zero)),
                (let '_ := tt in
                MonadVM.Support.UintN.op_lteq exponent_length
                  (MonadVM.Support.UintN.op_tildedollar 32))) with
            | (_, true, _) ⇒ MonadVM.Support.UintN.zero
            | (_, _, true) ⇒ bit_length_minus_one exponent_upper_256_bits
            | (_, _, _) ⇒
              MonadVM.Support.UintN.op_plus
                (MonadVM.Support.UintN.op_star
                  (MonadVM.Support.UintN.op_tildedollar 8)
                  (MonadVM.Support.UintN.op_minus exponent_length
                    (MonadVM.Support.UintN.op_tildedollar 32)))
                (bit_length_minus_one exponent_upper_256_bits)
            end in
          MonadVM.Support.UintN.max k_value MonadVM.Support.UintN.one
    |
      (MonadVM.Support.Variants.Revision.Nine |
      MonadVM.Support.Variants.Revision.Eleven |
      MonadVM.Support.Variants.Revision.Setcode _) ⇒
      fun (exponent_length : MonadVM.Support.UintN.Impl.t) ⇒
        fun (exponent_upper_256_bits : MonadVM.Support.UintN.Impl.t) ⇒
          let k_value :=
            match
              (tt,
                (let '_ := tt in
                andb
                  (MonadVM.Support.UintN.op_lteq exponent_length
                    (MonadVM.Support.UintN.op_tildedollar 32))
                  (MonadVM.Support.UintN.op_eq exponent_upper_256_bits
                    MonadVM.Support.UintN.zero)),
                (let '_ := tt in
                MonadVM.Support.UintN.op_lteq exponent_length
                  (MonadVM.Support.UintN.op_tildedollar 32))) with
            | (_, true, _) ⇒ MonadVM.Support.UintN.zero
            | (_, _, true) ⇒ bit_length_minus_one exponent_upper_256_bits
            | (_, _, _) ⇒
              MonadVM.Support.UintN.op_plus
                (MonadVM.Support.UintN.op_star
                  (MonadVM.Support.UintN.op_tildedollar 16)
                  (MonadVM.Support.UintN.op_minus exponent_length
                    (MonadVM.Support.UintN.op_tildedollar 32)))
                (bit_length_minus_one exponent_upper_256_bits)
            end in
          MonadVM.Support.UintN.max k_value MonadVM.Support.UintN.one
    | _ ⇒ _rocq_fallback_calculate_iteration_count_0
    end.

  Definition gas_quad_divisor `{_fargs : FArgs} : MonadVM.Gas.t :=
    MonadVM.Gas.op_tildedollar 3.

  Definition _rocq_fallback_calculate_gas_cost_0 `{_fargs : FArgs} : ltac:(let typ := constr:(ltac:(first [unshelve refine ((MonadVM.Support.UintN.Impl.t
  → MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
  MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.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 calculate_gas_cost `{_fargs : FArgs}
    : MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
    MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
    MonadVM.Support.UintN.Impl.t :=
    match Revision.(Make_Revision_signature.revision) with
    | MonadVM.Support.Variants.Revision.Eight ⇒
      fun (base_length : MonadVM.Support.UintN.Impl.t) ⇒
        fun (modulus_length : MonadVM.Support.UintN.Impl.t) ⇒
          fun (exponent_length : MonadVM.Support.UintN.Impl.t) ⇒
            fun (exponent_upper_256_bits : MonadVM.Support.UintN.Impl.t) ⇒
              let multiplication_complexity :=
                calculate_multiplication_complexity base_length modulus_length
                in
              let iteration_count :=
                calculate_iteration_count exponent_length
                  exponent_upper_256_bits in
              MonadVM.Support.UintN.max
                (MonadVM.Support.UintN.op_tildedollar 200)
                (MonadVM.Support.UintN.op_div
                  (MonadVM.Support.UintN.op_star multiplication_complexity
                    iteration_count) gas_quad_divisor)
    |
      (MonadVM.Support.Variants.Revision.Nine |
      MonadVM.Support.Variants.Revision.Eleven |
      MonadVM.Support.Variants.Revision.Setcode _) ⇒
      fun (base_length : MonadVM.Support.UintN.Impl.t) ⇒
        fun (modulus_length : MonadVM.Support.UintN.Impl.t) ⇒
          fun (exponent_length : MonadVM.Support.UintN.Impl.t) ⇒
            fun (exponent_upper_256_bits : MonadVM.Support.UintN.Impl.t) ⇒
              let multiplication_complexity :=
                calculate_multiplication_complexity base_length modulus_length
                in
              let iteration_count :=
                calculate_iteration_count exponent_length
                  exponent_upper_256_bits in
              MonadVM.Support.UintN.max
                (MonadVM.Support.UintN.op_tildedollar 500)
                (MonadVM.Support.UintN.op_star multiplication_complexity
                  iteration_count)
    | _ ⇒ _rocq_fallback_calculate_gas_cost_0
    end.

  Definition _rocq_fallback_read_at_most_256_bits_0 `{_fargs : FArgs} : ltac:(let typ := constr:(ltac:(first [unshelve refine ((string)%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 read_at_most_256_bits `{_fargs : FArgs}
    (data : MonadVM.Byte_string.Bytes.t) (start : MonadVM.Support.UintN.t)
    (length : MonadVM.Support.UintN.t) : MonadVM.Numeric.U256.t :=
    let length :=
      match MonadVM.Support.UintN.to_int_opt length with
      | None ⇒ 32
      | Some i_value ⇒ Z.min i_value 32
      end in
    match
      ((MonadVM.Support.UintN.to_int_opt start),
        match MonadVM.Support.UintN.to_int_opt start with
        | Some i_value ⇒ Z.ltb i_value (MonadVM.Byte_string.Bytes.length data)
        | _ ⇒ false
        end) with
    | (Some i_value, true) ⇒
      let in_range_bytes :=
        Z.min length
          (RocqOfOCaml.OCamlInt.sub (MonadVM.Byte_string.Bytes.length data)
            i_value) in
      let prefix :=
        (MonadVM.Byte_string.Bytes.sub (_rocq_fallback_read_at_most_256_bits_0))
          data i_value in_range_bytes in
      let suffix :=
        (MonadVM.Byte_string.Bytes.make
          (_rocq_fallback_read_at_most_256_bits_0))
          (RocqOfOCaml.OCamlInt.sub length in_range_bytes) "000" % char in
      MonadVM.Numeric.U256.of_bytes_be_exn (String.append prefix suffix)
    | (_, _) ⇒ MonadVM.Numeric.U256.zero
    end.

  Definition _rocq_fallback_parameter_length_0 `{_fargs : FArgs} : ltac:(let typ := constr:(ltac:(first [unshelve refine ((MonadVM.Evmc.Message.t
    × int →
  MonadVM.Precompiles.Utils.Precompile.Inner.t
    (sum MonadVM.Support.UintN.t MonadVM.Evmc.Result.StatusCode.t ×
      (MonadVM.Evmc.Message.t × int)))%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 parameter_length `{_fargs : FArgs}
    : MonadVM.Evmc.Message.t × int →
    MonadVM.Precompiles.Utils.Precompile.Inner.t
      (sum MonadVM.Support.UintN.t MonadVM.Evmc.Result.StatusCode.t ×
        (MonadVM.Evmc.Message.t × int)) :=
    match Revision.(Make_Revision_signature.revision) with
    | MonadVM.Support.Variants.Revision.Eight ⇒
      MonadVM.Precompiles.Utils.Precompile.op_ltdollargt
        MonadVM.Numeric.U256.to_uint MonadVM.Precompiles.Utils.Precompile.u256
    |
      (MonadVM.Support.Variants.Revision.Nine |
      MonadVM.Support.Variants.Revision.Eleven |
      MonadVM.Support.Variants.Revision.Setcode _) ⇒
      MonadVM.Precompiles.Utils.Precompile.op_letdollar
        (MonadVM.Precompiles.Utils.Precompile.op_ltdollargt
          MonadVM.Numeric.U256.to_uint MonadVM.Precompiles.Utils.Precompile.u256)
        (fun len ⇒
          MonadVM.Precompiles.Utils.Precompile.op_letdollar
            (MonadVM.Precompiles.Utils.Precompile.when_
              (MonadVM.Support.UintN.op_gt len
                (MonadVM.Support.UintN.op_tildedollar 1024))
              MonadVM.Precompiles.Utils.Precompile.precompile_failure)
            (fun function_parameter ⇒
              let '_ := function_parameter in
              MonadVM.Precompiles.Utils.Precompile._return len))
    | _ ⇒ _rocq_fallback_parameter_length_0
    end.

  Definition address `{_fargs : FArgs} : MonadVM.Chain.Ethereum.Address.t :=
    MonadVM.Chain.Ethereum.Address.of_hex_string None "0x05".

  Definition _rocq_fallback_precompile_0 `{_fargs : FArgs} : ltac:(let typ := constr:(ltac:(first [unshelve refine ((unit)%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 precompile `{_fargs : FArgs} (msg : MonadVM.Evmc.Message.t)
    : MonadVM.Evmc.Result.t :=
    MonadVM.Precompiles.Utils.Precompile.run msg
      (MonadVM.Precompiles.Utils.Precompile.op_letdollar parameter_length
        (fun base_length ⇒
          MonadVM.Precompiles.Utils.Precompile.op_letdollar parameter_length
            (fun exponent_length ⇒
              MonadVM.Precompiles.Utils.Precompile.op_letdollar parameter_length
                (fun modulus_length ⇒
                  let base_start :=
                    RocqOfOCaml.OCamlInt.mul 3 MonadVM.Numeric.U256.byte_width
                    in
                  MonadVM.Precompiles.Utils.Precompile.op_letdollar
                    (let gas_cost :=
                      let exponent_upper_256_bits :=
                        let exponent_start :=
                          MonadVM.Support.UintN.op_plus
                            (MonadVM.Support.UintN.op_tildedollar base_start)
                            base_length in
                        MonadVM.Numeric.U256.to_uint
                          (read_at_most_256_bits
                            msg.(MonadVM.Evmc.Message.t.input_data)
                            exponent_start exponent_length) in
                      calculate_gas_cost base_length modulus_length
                        exponent_length exponent_upper_256_bits in
                    MonadVM.Precompiles.Utils.Precompile.spend_gas gas_cost)
                    (fun function_parameter ⇒
                      let '_ := function_parameter in
                      let base_length :=
                        MonadVM.Support.UintN.to_int base_length in
                      let modulus_length :=
                        MonadVM.Support.UintN.to_int modulus_length in
                      if Z.eqb modulus_length 0 then
                        MonadVM.Precompiles.Utils.Precompile._return
                          MonadVM.Byte_string.Bytes.empty
                      else
                        let exponent_length :=
                          MonadVM.Support.UintN.to_int exponent_length in
                        MonadVM.Precompiles.Utils.Precompile.op_letdollar
                          (MonadVM.Precompiles.Utils.Precompile.op_ltdollargt
                            MonadVM.Support.UintN.of_bytes_be
                            (MonadVM.Precompiles.Utils.Precompile.bytes_value
                              base_length))
                          (fun base ⇒
                            MonadVM.Precompiles.Utils.Precompile.op_letdollar
                              (MonadVM.Precompiles.Utils.Precompile.op_ltdollargt
                                MonadVM.Support.UintN.of_bytes_be
                                (MonadVM.Precompiles.Utils.Precompile.bytes_value
                                  exponent_length))
                              (fun exponent ⇒
                                MonadVM.Precompiles.Utils.Precompile.op_letdollar
                                  (MonadVM.Precompiles.Utils.Precompile.op_ltdollargt
                                    MonadVM.Support.UintN.of_bytes_be
                                    (MonadVM.Precompiles.Utils.Precompile.bytes_value
                                      modulus_length))
                                  (fun modulus ⇒
                                    let result_value :=
                                      if
                                        orb
                                          (MonadVM.Support.UintN.op_eq
                                            modulus
                                            MonadVM.Support.UintN.zero)
                                          (MonadVM.Support.UintN.op_eq
                                            modulus
                                            MonadVM.Support.UintN.one)
                                      then
                                        MonadVM.Support.UintN.zero
                                      else
                                        if
                                          MonadVM.Support.UintN.op_eq exponent
                                            MonadVM.Support.UintN.zero
                                        then
                                          MonadVM.Support.UintN.one
                                        else
                                          MonadVM.Support.UintN.exp_mod modulus
                                            base exponent in
                                    let result_bytes :=
                                      MonadVM.Support.UintN.to_bytes_be
                                        result_value in
                                    let padding_length :=
                                      RocqOfOCaml.OCamlInt.sub modulus_length
                                        (MonadVM.Byte_string.Bytes.length
                                          result_bytes) in
                                    let '_ :=
                                      if Z.geb padding_length 0 then tt else
                                        _rocq_fallback_precompile_0 in
                                    MonadVM.Precompiles.Utils.Precompile._return
                                      (String.append
                                        ((MonadVM.Byte_string.Bytes.make
                                          (_rocq_fallback_read_at_most_256_bits_0))
                                          padding_length "000" % char)
                                        result_bytes))))))))).

  Module Make_result.
    Record signature `{_fargs : FArgs} : Type := {
      calculate_multiplication_complexity :
        MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
        MonadVM.Support.UintN.t;
      calculate_iteration_count :
        MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
        MonadVM.Support.UintN.Impl.t;
      gas_quad_divisor : MonadVM.Gas.t;
      calculate_gas_cost :
        MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
        MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
        MonadVM.Support.UintN.Impl.t;
      read_at_most_256_bits :
        MonadVM.Byte_string.Bytes.t → MonadVM.Support.UintN.t →
        MonadVM.Support.UintN.t → MonadVM.Numeric.U256.t;
      parameter_length :
        MonadVM.Evmc.Message.t × int →
        MonadVM.Precompiles.Utils.Precompile.Inner.t
          (sum MonadVM.Support.UintN.t MonadVM.Evmc.Result.StatusCode.t ×
            (MonadVM.Evmc.Message.t × int));
      address : MonadVM.Chain.Ethereum.Address.t;
      precompile : MonadVM.Evmc.Message.t → MonadVM.Evmc.Result.t;
    }.
  End Make_result.
  Definition Make_result `{_fargs : FArgs} := @Make_result.signature _.
  Arguments Make_result {_}.

  Definition functor `{_fargs : FArgs} :@Make_result _fargs :=
    ((@Make_result.Build_signature _fargs calculate_multiplication_complexity
      calculate_iteration_count gas_quad_divisor calculate_gas_cost
      read_at_most_256_bits parameter_length address precompile) :
      @Make_result _fargs).
End Make.
Definition Make (Revision : Make_Revision_signature) :=
  @Make.functor (Make.Build_FArgs Revision).