Library MonadVM.Evmc

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.Int64.
Require MonadVM.Numeric.
Require MonadVM.Uint64.

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 Result.
  Module StatusCode.
EVM execution status code: Success or Revert in case of normal termination, or a descriptive error code in case of failure. Equivalent to [evmc_status_code].
    Inductive t : Set :=
    | Success : t
    | Failure : t
    | Revert : t
    | Out_of_gas : t
    | Invalid_instruction : t
    | Undefined_instruction : t
    | Stack_overflow : t
    | Stack_underflow : t
    | Bad_jump_destination : t
    | Invalid_memory_access : t
    | Call_depth_exceeded : t
    | Static_mode_violation : t
    | Precompile_failure : t
    | Contract_validation_failure : t
    | Argument_out_of_range : t
    | Wasm_unreachable_instruction : t
    | Wasm_trap : t
    | Insufficient_balance : t
    | Monad_reserve_balance_violation : t
    | Internal_error : t
    | Rejected : t
    | Out_of_memory : t
    | Create_from_delegated_eoa : t
    | Setcode_forbidden : t
    | Migrated_sender : t.

EVM execution status code: Success or Revert in case of normal termination, or a descriptive error code in case of failure. Equivalent to [evmc_status_code]. EVM execution status code: Success or Revert in case of normal termination, or a descriptive error code in case of failure. Equivalent to [evmc_status_code].
A high-level version of the Category Labs fork of the EVMC interface.
The EVMC interface is a de-facto Ethereum standard that allows EVM implementations to be written against an abstract interface (the EVMC host) instead of tightly integrated with an entire execution client.
The Monad reference implementation follows the boundaries between VM and host delineated by the EVMC interface but diverges in two important respects. + High-level OCaml types are used rather than C primitive types. + APIs are based around explicit state passing, to allow for a purely functional implementation.
    Definition equal (lhs : t) (rhs : t) : Ppx_deriving_runtime.bool :=
      match (lhs, rhs) with
      |
        (MonadVM.Evmc.Result.StatusCode.Success,
          MonadVM.Evmc.Result.StatusCode.Success) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Failure,
          MonadVM.Evmc.Result.StatusCode.Failure) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Revert,
          MonadVM.Evmc.Result.StatusCode.Revert) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Out_of_gas,
          MonadVM.Evmc.Result.StatusCode.Out_of_gas) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Invalid_instruction,
          MonadVM.Evmc.Result.StatusCode.Invalid_instruction) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Undefined_instruction,
          MonadVM.Evmc.Result.StatusCode.Undefined_instruction) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Stack_overflow,
          MonadVM.Evmc.Result.StatusCode.Stack_overflow) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Stack_underflow,
          MonadVM.Evmc.Result.StatusCode.Stack_underflow) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Bad_jump_destination,
          MonadVM.Evmc.Result.StatusCode.Bad_jump_destination) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Invalid_memory_access,
          MonadVM.Evmc.Result.StatusCode.Invalid_memory_access) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Call_depth_exceeded,
          MonadVM.Evmc.Result.StatusCode.Call_depth_exceeded) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Static_mode_violation,
          MonadVM.Evmc.Result.StatusCode.Static_mode_violation) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Precompile_failure,
          MonadVM.Evmc.Result.StatusCode.Precompile_failure) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Contract_validation_failure,
          MonadVM.Evmc.Result.StatusCode.Contract_validation_failure) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Argument_out_of_range,
          MonadVM.Evmc.Result.StatusCode.Argument_out_of_range) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Wasm_unreachable_instruction,
          MonadVM.Evmc.Result.StatusCode.Wasm_unreachable_instruction) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Wasm_trap,
          MonadVM.Evmc.Result.StatusCode.Wasm_trap) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Insufficient_balance,
          MonadVM.Evmc.Result.StatusCode.Insufficient_balance) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Monad_reserve_balance_violation,
          MonadVM.Evmc.Result.StatusCode.Monad_reserve_balance_violation) ⇒
        true
      |
        (MonadVM.Evmc.Result.StatusCode.Internal_error,
          MonadVM.Evmc.Result.StatusCode.Internal_error) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Rejected,
          MonadVM.Evmc.Result.StatusCode.Rejected) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Out_of_memory,
          MonadVM.Evmc.Result.StatusCode.Out_of_memory) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Create_from_delegated_eoa,
          MonadVM.Evmc.Result.StatusCode.Create_from_delegated_eoa) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Setcode_forbidden,
          MonadVM.Evmc.Result.StatusCode.Setcode_forbidden) ⇒ true
      |
        (MonadVM.Evmc.Result.StatusCode.Migrated_sender,
          MonadVM.Evmc.Result.StatusCode.Migrated_sender) ⇒ true
      | __ ⇒ false
      end.

A high-level version of the Category Labs fork of the EVMC interface.
The EVMC interface is a de-facto Ethereum standard that allows EVM implementations to be written against an abstract interface (the EVMC host) instead of tightly integrated with an entire execution client.
The Monad reference implementation follows the boundaries between VM and host delineated by the EVMC interface but diverges in two important respects. + High-level OCaml types are used rather than C primitive types. + APIs are based around explicit state passing, to allow for a purely functional implementation.


    Definition is_success (function_parameter : t) : bool :=
      match function_parameter with
      | MonadVM.Evmc.Result.StatusCode.Success ⇒ true
      | _ ⇒ false
      end.

    Definition to_string (function_parameter : t) : string :=
      match function_parameter with
      | MonadVM.Evmc.Result.StatusCode.Success ⇒ "Success"
      | MonadVM.Evmc.Result.StatusCode.Failure ⇒ "Failure"
      | MonadVM.Evmc.Result.StatusCode.Revert ⇒ "Revert"
      | MonadVM.Evmc.Result.StatusCode.Out_of_gas ⇒ "Out of gas"
      | MonadVM.Evmc.Result.StatusCode.Invalid_instruction ⇒
        "Invalid instruction"
      | MonadVM.Evmc.Result.StatusCode.Undefined_instruction ⇒
        "Undefined instruction"
      | MonadVM.Evmc.Result.StatusCode.Stack_overflow ⇒ "Stack overflow"
      | MonadVM.Evmc.Result.StatusCode.Stack_underflow ⇒ "Stack underflow"
      | MonadVM.Evmc.Result.StatusCode.Bad_jump_destination ⇒
        "Bad jump destination"
      | MonadVM.Evmc.Result.StatusCode.Invalid_memory_access ⇒
        "Invalid memory access"
      | MonadVM.Evmc.Result.StatusCode.Call_depth_exceeded ⇒
        "Call depth exceeded"
      | MonadVM.Evmc.Result.StatusCode.Static_mode_violation ⇒
        "Static memory violation"
      | MonadVM.Evmc.Result.StatusCode.Precompile_failure ⇒
        "Precompile failure"
      | MonadVM.Evmc.Result.StatusCode.Contract_validation_failure ⇒
        "Contract validation failure"
      | MonadVM.Evmc.Result.StatusCode.Argument_out_of_range ⇒
        "Argument out of range"
      | MonadVM.Evmc.Result.StatusCode.Wasm_unreachable_instruction ⇒
        "Wasm unreachable instruction"
      | MonadVM.Evmc.Result.StatusCode.Wasm_trap ⇒ "Wasm trap"
      | MonadVM.Evmc.Result.StatusCode.Insufficient_balance ⇒
        "Insufficient balance"
      | MonadVM.Evmc.Result.StatusCode.Monad_reserve_balance_violation ⇒
        "Monad reserve balance violation"
      | MonadVM.Evmc.Result.StatusCode.Internal_error ⇒ "Internal error"
      | MonadVM.Evmc.Result.StatusCode.Rejected ⇒ "Rejected"
      | MonadVM.Evmc.Result.StatusCode.Out_of_memory ⇒ "Out of memory"
      | MonadVM.Evmc.Result.StatusCode.Create_from_delegated_eoa ⇒
        "Create from delegated eoa"
      | MonadVM.Evmc.Result.StatusCode.Setcode_forbidden ⇒
        "SETCODEFROM forbidden"
      | MonadVM.Evmc.Result.StatusCode.Migrated_sender ⇒
        "Sender migrated to regular code"
      end.

    Definition op_eq : t → t → Ppx_deriving_runtime.bool := equal.

    Definition op_ltgt (x_value : t) (y_value : t) : bool :=
      negb (equal x_value y_value).
  End StatusCode.

Equivalent to [evmc_result].
  Module t.
    Record record : Set := Build {
      status_code : StatusCode.t;
      gas_left : MonadVM.Int64.t;
      gas_refund : MonadVM.Int64.t;
      output_data : MonadVM.Byte_string.Bytes.t;
      create_address : MonadVM.Chain.Ethereum.Address.t;
    }.
    Definition with_status_code status_code (r : record) :=
      Build status_code r.(gas_left) r.(gas_refund) r.(output_data)
        r.(create_address).
    Definition with_gas_left gas_left (r : record) :=
      Build r.(status_code) gas_left r.(gas_refund) r.(output_data)
        r.(create_address).
    Definition with_gas_refund gas_refund (r : record) :=
      Build r.(status_code) r.(gas_left) gas_refund r.(output_data)
        r.(create_address).
    Definition with_output_data output_data (r : record) :=
      Build r.(status_code) r.(gas_left) r.(gas_refund) output_data
        r.(create_address).
    Definition with_create_address create_address (r : record) :=
      Build r.(status_code) r.(gas_left) r.(gas_refund) r.(output_data)
        create_address.
  End t.
  Definition t := t.record.

failure error_code constructs a result representing termination due to execution failure (but not deliberate termination due to a REVERT instruction).
  Definition _rocq_fallback_failure_0 :
    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 failure (error_code : StatusCode.t) : t :=
    let '_ :=
      if
        match error_code with
        |
          (MonadVM.Evmc.Result.StatusCode.Success |
          MonadVM.Evmc.Result.StatusCode.Revert) ⇒ false
        | _ ⇒ true
        end then tt else _rocq_fallback_failure_0 in
    {| MonadVM.Evmc.Result.t.status_code := error_code;
      MonadVM.Evmc.Result.t.gas_left := 0;
      MonadVM.Evmc.Result.t.gas_refund := 0;
      MonadVM.Evmc.Result.t.output_data := MonadVM.Byte_string.Bytes.empty;
      MonadVM.Evmc.Result.t.create_address :=
        MonadVM.Chain.Ethereum.Address.zero; |}.
End Result.

Module Message.
Equivalent to [evmc_call_kind]. Note that the EVMC_EOFCREATE call kind is not provided.
  Module CallKind.
    Inductive t : Set :=
    | Call : t
    | DelegateCall : t
    | CallCode : t
    | Create : t
    | Create2 : t.

A high-level version of the Category Labs fork of the EVMC interface.
The EVMC interface is a de-facto Ethereum standard that allows EVM implementations to be written against an abstract interface (the EVMC host) instead of tightly integrated with an entire execution client.
The Monad reference implementation follows the boundaries between VM and host delineated by the EVMC interface but diverges in two important respects. + High-level OCaml types are used rather than C primitive types. + APIs are based around explicit state passing, to allow for a purely functional implementation.
    Definition equal (lhs : t) (rhs : t) : Ppx_deriving_runtime.bool :=
      match (lhs, rhs) with
      | (MonadVM.Evmc.Message.CallKind.Call, MonadVM.Evmc.Message.CallKind.Call)
        ⇒ true
      |
        (MonadVM.Evmc.Message.CallKind.DelegateCall,
          MonadVM.Evmc.Message.CallKind.DelegateCall) ⇒ true
      |
        (MonadVM.Evmc.Message.CallKind.CallCode,
          MonadVM.Evmc.Message.CallKind.CallCode) ⇒ true
      |
        (MonadVM.Evmc.Message.CallKind.Create,
          MonadVM.Evmc.Message.CallKind.Create) ⇒ true
      |
        (MonadVM.Evmc.Message.CallKind.Create2,
          MonadVM.Evmc.Message.CallKind.Create2) ⇒ true
      | __ ⇒ false
      end.

A high-level version of the Category Labs fork of the EVMC interface.
The EVMC interface is a de-facto Ethereum standard that allows EVM implementations to be written against an abstract interface (the EVMC host) instead of tightly integrated with an entire execution client.
The Monad reference implementation follows the boundaries between VM and host delineated by the EVMC interface but diverges in two important respects. + High-level OCaml types are used rather than C primitive types. + APIs are based around explicit state passing, to allow for a purely functional implementation.


    Definition is_create (function_parameter : t) : bool :=
      match function_parameter with
      |
        (MonadVM.Evmc.Message.CallKind.Create |
        MonadVM.Evmc.Message.CallKind.Create2) ⇒ true
      |
        (MonadVM.Evmc.Message.CallKind.Call |
        MonadVM.Evmc.Message.CallKind.DelegateCall |
        MonadVM.Evmc.Message.CallKind.CallCode) ⇒ false
      end.

    Definition op_eq : t → t → Ppx_deriving_runtime.bool := equal.

    Definition op_ltgt (x_value : t) (y_value : t) : bool :=
      negb (equal x_value y_value).
  End CallKind.

An EVMC message represents a call to the EVM, corresponding to a contract creation or message call in Sections 7 and 8 respectively of the Ethereum Yellow Paper. Equivalent to [evmc_message]. The remaining memory capacity for this call, see MIP-3.
  Module t.
    Record record : Set := Build {
      kind : CallKind.t;
      static : bool;
      delegated : bool;
      depth : RocqOfOCaml.OCamlInt32.t;
      gas : MonadVM.Uint64.t;
      recipient : MonadVM.Chain.Ethereum.Address.t;
      sender : MonadVM.Chain.Ethereum.Address.t;
      input_data : MonadVM.Byte_string.Bytes.t;
      value : MonadVM.Numeric.U256.t;
      create2_salt : string;
      code_address : MonadVM.Chain.Ethereum.Address.t;
      code : MonadVM.Byte_string.Bytes.t;
      memory_capacity : RocqOfOCaml.OCamlInt32.t;
    }.
    Definition with_kind kind (r : record) :=
      Build kind r.(static) r.(delegated) r.(depth) r.(gas) r.(recipient)
        r.(sender) r.(input_data) r.(value) r.(create2_salt) r.(code_address)
        r.(code) r.(memory_capacity).
    Definition with_static static (r : record) :=
      Build r.(kind) static r.(delegated) r.(depth) r.(gas) r.(recipient)
        r.(sender) r.(input_data) r.(value) r.(create2_salt) r.(code_address)
        r.(code) r.(memory_capacity).
    Definition with_delegated delegated (r : record) :=
      Build r.(kind) r.(static) delegated r.(depth) r.(gas) r.(recipient)
        r.(sender) r.(input_data) r.(value) r.(create2_salt) r.(code_address)
        r.(code) r.(memory_capacity).
    Definition with_depth depth (r : record) :=
      Build r.(kind) r.(static) r.(delegated) depth r.(gas) r.(recipient)
        r.(sender) r.(input_data) r.(value) r.(create2_salt) r.(code_address)
        r.(code) r.(memory_capacity).
    Definition with_gas gas (r : record) :=
      Build r.(kind) r.(static) r.(delegated) r.(depth) gas r.(recipient)
        r.(sender) r.(input_data) r.(value) r.(create2_salt) r.(code_address)
        r.(code) r.(memory_capacity).
    Definition with_recipient recipient (r : record) :=
      Build r.(kind) r.(static) r.(delegated) r.(depth) r.(gas) recipient
        r.(sender) r.(input_data) r.(value) r.(create2_salt) r.(code_address)
        r.(code) r.(memory_capacity).
    Definition with_sender sender (r : record) :=
      Build r.(kind) r.(static) r.(delegated) r.(depth) r.(gas) r.(recipient)
        sender r.(input_data) r.(value) r.(create2_salt) r.(code_address)
        r.(code) r.(memory_capacity).
    Definition with_input_data input_data (r : record) :=
      Build r.(kind) r.(static) r.(delegated) r.(depth) r.(gas) r.(recipient)
        r.(sender) input_data r.(value) r.(create2_salt) r.(code_address)
        r.(code) r.(memory_capacity).
    Definition with_value value (r : record) :=
      Build r.(kind) r.(static) r.(delegated) r.(depth) r.(gas) r.(recipient)
        r.(sender) r.(input_data) value r.(create2_salt) r.(code_address)
        r.(code) r.(memory_capacity).
    Definition with_create2_salt create2_salt (r : record) :=
      Build r.(kind) r.(static) r.(delegated) r.(depth) r.(gas) r.(recipient)
        r.(sender) r.(input_data) r.(value) create2_salt r.(code_address)
        r.(code) r.(memory_capacity).
    Definition with_code_address code_address (r : record) :=
      Build r.(kind) r.(static) r.(delegated) r.(depth) r.(gas) r.(recipient)
        r.(sender) r.(input_data) r.(value) r.(create2_salt) code_address
        r.(code) r.(memory_capacity).
    Definition with_code code (r : record) :=
      Build r.(kind) r.(static) r.(delegated) r.(depth) r.(gas) r.(recipient)
        r.(sender) r.(input_data) r.(value) r.(create2_salt) r.(code_address)
        code r.(memory_capacity).
    Definition with_memory_capacity memory_capacity (r : record) :=
      Build r.(kind) r.(static) r.(delegated) r.(depth) r.(gas) r.(recipient)
        r.(sender) r.(input_data) r.(value) r.(create2_salt) r.(code_address)
        r.(code) memory_capacity.
  End t.
  Definition t := t.record.
End Message.

Module TxInitcode.
Equivalent to [evmc_tx_initcode].
  Module t.
    Record record : Set := Build {
      hash : string;
      code : MonadVM.Byte_string.Bytes.t;
    }.
    Definition with_hash hash (r : record) :=
      Build hash r.(code).
    Definition with_code code (r : record) :=
      Build r.(hash) code.
  End t.
  Definition t := t.record.
End TxInitcode.

Module TxContext.
Equivalent to [evmc_tx_context].
  Module t.
    Record record : Set := Build {
      tx_gas_price : MonadVM.Numeric.U256.t;
      tx_origin : MonadVM.Chain.Ethereum.Address.t;
      block_coinbase : MonadVM.Chain.Ethereum.Address.t;
      block_number : MonadVM.Uint64.t;
      block_timestamp : MonadVM.Uint64.t;
      block_gas_limit : MonadVM.Uint64.t;
      block_prev_randao : MonadVM.Numeric.U256.t;
      chain_id : MonadVM.Numeric.U256.t;
      block_base_fee : MonadVM.Numeric.U256.t;
      blob_base_fee : MonadVM.Numeric.U256.t;
      blob_hashes : list string;
      initcodes : list TxInitcode.t;
    }.
    Definition with_tx_gas_price tx_gas_price (r : record) :=
      Build tx_gas_price r.(tx_origin) r.(block_coinbase) r.(block_number)
        r.(block_timestamp) r.(block_gas_limit) r.(block_prev_randao)
        r.(chain_id) r.(block_base_fee) r.(blob_base_fee) r.(blob_hashes)
        r.(initcodes).
    Definition with_tx_origin tx_origin (r : record) :=
      Build r.(tx_gas_price) tx_origin r.(block_coinbase) r.(block_number)
        r.(block_timestamp) r.(block_gas_limit) r.(block_prev_randao)
        r.(chain_id) r.(block_base_fee) r.(blob_base_fee) r.(blob_hashes)
        r.(initcodes).
    Definition with_block_coinbase block_coinbase (r : record) :=
      Build r.(tx_gas_price) r.(tx_origin) block_coinbase r.(block_number)
        r.(block_timestamp) r.(block_gas_limit) r.(block_prev_randao)
        r.(chain_id) r.(block_base_fee) r.(blob_base_fee) r.(blob_hashes)
        r.(initcodes).
    Definition with_block_number block_number (r : record) :=
      Build r.(tx_gas_price) r.(tx_origin) r.(block_coinbase) block_number
        r.(block_timestamp) r.(block_gas_limit) r.(block_prev_randao)
        r.(chain_id) r.(block_base_fee) r.(blob_base_fee) r.(blob_hashes)
        r.(initcodes).
    Definition with_block_timestamp block_timestamp (r : record) :=
      Build r.(tx_gas_price) r.(tx_origin) r.(block_coinbase) r.(block_number)
        block_timestamp r.(block_gas_limit) r.(block_prev_randao) r.(chain_id)
        r.(block_base_fee) r.(blob_base_fee) r.(blob_hashes) r.(initcodes).
    Definition with_block_gas_limit block_gas_limit (r : record) :=
      Build r.(tx_gas_price) r.(tx_origin) r.(block_coinbase) r.(block_number)
        r.(block_timestamp) block_gas_limit r.(block_prev_randao) r.(chain_id)
        r.(block_base_fee) r.(blob_base_fee) r.(blob_hashes) r.(initcodes).
    Definition with_block_prev_randao block_prev_randao (r : record) :=
      Build r.(tx_gas_price) r.(tx_origin) r.(block_coinbase) r.(block_number)
        r.(block_timestamp) r.(block_gas_limit) block_prev_randao r.(chain_id)
        r.(block_base_fee) r.(blob_base_fee) r.(blob_hashes) r.(initcodes).
    Definition with_chain_id chain_id (r : record) :=
      Build r.(tx_gas_price) r.(tx_origin) r.(block_coinbase) r.(block_number)
        r.(block_timestamp) r.(block_gas_limit) r.(block_prev_randao) chain_id
        r.(block_base_fee) r.(blob_base_fee) r.(blob_hashes) r.(initcodes).
    Definition with_block_base_fee block_base_fee (r : record) :=
      Build r.(tx_gas_price) r.(tx_origin) r.(block_coinbase) r.(block_number)
        r.(block_timestamp) r.(block_gas_limit) r.(block_prev_randao)
        r.(chain_id) block_base_fee r.(blob_base_fee) r.(blob_hashes)
        r.(initcodes).
    Definition with_blob_base_fee blob_base_fee (r : record) :=
      Build r.(tx_gas_price) r.(tx_origin) r.(block_coinbase) r.(block_number)
        r.(block_timestamp) r.(block_gas_limit) r.(block_prev_randao)
        r.(chain_id) r.(block_base_fee) blob_base_fee r.(blob_hashes)
        r.(initcodes).
    Definition with_blob_hashes blob_hashes (r : record) :=
      Build r.(tx_gas_price) r.(tx_origin) r.(block_coinbase) r.(block_number)
        r.(block_timestamp) r.(block_gas_limit) r.(block_prev_randao)
        r.(chain_id) r.(block_base_fee) r.(blob_base_fee) blob_hashes
        r.(initcodes).
    Definition with_initcodes initcodes (r : record) :=
      Build r.(tx_gas_price) r.(tx_origin) r.(block_coinbase) r.(block_number)
        r.(block_timestamp) r.(block_gas_limit) r.(block_prev_randao)
        r.(chain_id) r.(block_base_fee) r.(blob_base_fee) r.(blob_hashes)
        initcodes.
  End t.
  Definition t := t.record.
End TxContext.

Module StorageStatus.
The status of a storage modification, used to compute SSTORE gas cost and refund calculation. See EIP-2200 and the definition of C_SSTORE in the Yellow Paper. Equivalent to [evmc_storage_status].
  Inductive t : Set :=
  | Added : t
  | Deleted : t
  | Modified : t
  | DeletedAdded : t
  | ModifiedDeleted : t
  | DeletedRestored : t
  | AddedDeleted : t
  | ModifiedRestored : t
  | Assigned : t.
End StorageStatus.

Module HOST.
  Record signature {t : Set} : Type := {
    
Types supporting a purely functional version of the EVMC host API. This mirrors the structure of [evmc_host_interface], replacing the mutable evmc_host_context× parameter with an explicit threaded state of abstract type t.
    t := t;
    
account_exists addr returns true if an account exists at addr, false otherwise. Equivalent to evmc_account_exists_fn.
    account_exists : MonadVM.Chain.Ethereum.Address.t → t → bool × t;
    
get_storage addr key returns the value at the storage key key for the account at addr. If the account does not exist or the storage key has never been written to, the value is taken to be zero. Equivalent to evmc_get_storage_fn.
    get_storage : MonadVM.Chain.Ethereum.Address.t → string → t → string × t;
    
set_storage addr key value sets the value at the storage key key for the account at addr to the new value value, returning a StorageStatus.t corresponding to the effect of this update on the storage item. Equivalent to evmc_set_storage_fn.
    set_storage :
      MonadVM.Chain.Ethereum.Address.t → string → string → t →
      StorageStatus.t × t;
    
get_balance addr returns the balance of the account at addr. If the account does not exist, the balance is taken to be zero. Equivalent to evmc_get_balance_fn.
    get_balance :
      MonadVM.Chain.Ethereum.Address.t → t → MonadVM.Numeric.U256.t × t;
    
get_code_size addr returns the size in bytes of the Ethereum bytecode stored in the account at addr. If the account does not exist, get_code_size behaves as if the code was empty. Equivalent to evmc_get_code_size_fn.
    get_code_size :
      MonadVM.Chain.Ethereum.Address.t → t → MonadVM.Uint64.t × t;
    
get_code_hash addr returns the hash of the Ethereum bytecode stored in the account at addr, or None if the account does not exist. Equivalent to evmc_get_code_hash_fn.
    get_code_hash : MonadVM.Chain.Ethereum.Address.t → t → option string × t;
    
copy_code addr ¬offset ¬size returns at most size bytes of the code for the account at addr starting at offset offset. Equivalent to evmc_copy_code_fn.
    copy_code :
      MonadVM.Chain.Ethereum.Address.t → int → int → t →
      MonadVM.Byte_string.Bytes.t × t;
    
MIP-19's state operation, including source access and explicit gas charges. Error is an exceptional halt. Ok reports source validity and remaining gas.
    setcodefrom :
      MonadVM.Chain.Ethereum.Address.t → MonadVM.Chain.Ethereum.Address.t →
      bool → MonadVM.Support.UintN.t → t →
      sum (bool × MonadVM.Support.UintN.t) Result.StatusCode.t × t;
    
selfdestruct ¬address ¬beneficiary indicates that a SELFDESTRUCT instruction has been called. The host must transfer the full balance of address to beneficiary and, if address was created in the current transaction, add it to the self-destruct list as per EIP-6780. selfdestruct ¬address ¬beneficiary returns true if and only if the account has been successfully added to the self-destruct list and it had not been self-destructed yet during the current transaction. Equivalent to evmc_selfdestruct_fn.
    selfdestruct :
      MonadVM.Chain.Ethereum.Address.t → MonadVM.Chain.Ethereum.Address.t →
      t → bool × t;
    
call msg performs a contract creation or message call according to YP Section 7 or 8 respectively. Equivalent to evmc_call_fn.
    call : Message.t → t → Result.t × t;
    
get_tx_context returns the context for the current transaction. Equivalent to evmc_get_tx_context_fn.
    get_tx_context : t → TxContext.t × t;
    
get_block_hash num returns the block hash of the block num, or None if no such block is available. Equivalent to evmc_get_block_hash_fn.
    get_block_hash : MonadVM.Uint64.t → t → option string × t;
    
emit_log addr ¬data ¬topics creates a log with the specified data and topics and appends it to the logs emitted during the current transaction. Equivalent to evmc_emit_log_fn.
    emit_log :
      MonadVM.Chain.Ethereum.Address.t → MonadVM.Byte_string.Bytes.t →
      list string → t → unit × t;
    
access_account addr adds the account addr to the list of accessed accounts in the current transaction, and returns `Warm if the account had previously been accessed or `Cold otherwise. Equivalent to evmc_access_account_fn.
    access_account :
      MonadVM.Chain.Ethereum.Address.t → t →
      MonadVM.Support.Variants.AccessStatus.t × t;
    
access_storage addr key adds the pair (addr, key) to the list of accessed storage slots in the current transaction, and returns `Warm if the pair had previously been accessed or `Cold otherwise. Equivalent to evmc_access_storage_fn.
    access_storage :
      MonadVM.Chain.Ethereum.Address.t → string → t →
      MonadVM.Support.Variants.AccessStatus.t × t;
    
get_transient_storage addr key returns the value at the transient storage key key for the account at addr. If the account does not exist or the transient storage key has not been written to during this transaction, the value is taken to be zero. Equivalent to evmc_get_transient_storage_fn.
    get_transient_storage :
      MonadVM.Chain.Ethereum.Address.t → string → t → string × t;
    
set_transient_storage addr key value sets the value at the transient storage key key for the account at addr to the new value value. Equivalent to evmc_set_transient_storage_fn.
    set_transient_storage :
      MonadVM.Chain.Ethereum.Address.t → string → string → t → unit × t;
  }.
End HOST.
Definition HOST := @HOST.signature.
Arguments HOST {_}.

The type of EVMC VMs over a host H.t, broadly based on [evmc_vm], minus the ancillary introspection operations.
Module Vm_H_signature.
  Record signature {t : Set} : Type := {
    t := t;
  }.
End Vm_H_signature.
Definition Vm_H_signature := @Vm_H_signature.signature.
Arguments Vm_H_signature {_}.

Module Vm.
  Class FArgs {H_t : Set} := {
    H : Vm_H_signature (t := H_t);
  }.
  Arguments Build_FArgs {_}.

  Module SIG.
    Record signature `{_fargs : FArgs} : Type := {
      
execute msg code executes the code code on the input msg. Equivalent to evmc_execute_fn.
      execute :
        Message.t → MonadVM.Byte_string.Bytes.t → H.(Vm_H_signature.t) →
        Result.t × H.(Vm_H_signature.t);
    }.
  End SIG.
  Definition SIG `{_fargs : FArgs} := @SIG.signature _ _.
  Arguments SIG {_ _}.

  Module Vm_result.
    Inductive signature `{_fargs : FArgs} : Type :=
    | Build_signature : signature.
  End Vm_result.
  Definition Vm_result `{_fargs : FArgs} := @Vm_result.signature _ _.
  Arguments Vm_result {_ _}.

  Definition functor `{_fargs : FArgs} :@Vm_result H_t _fargs :=
    ((@Vm_result.Build_signature H_t _fargs) : @Vm_result H_t _fargs).
End Vm.
Definition Vm {H_t : Set} (H : Vm_H_signature (t := H_t)) :=
  @Vm.functor H_t (Vm.Build_FArgs H).

Helper module to instantiate a host and VM over the same underlying host type.
Module Instantiate.
  Class FArgs {T_t : Set} := {
    T : Vm_H_signature (t := T_t);
    HostF :
      ∀ (Vm : Vm.SIG (_fargs := Vm.Build_FArgs T)),
      HOST (t := T.(Vm_H_signature.t));
    VmF :
      ∀ (H : HOST (t := T.(Vm_H_signature.t))),
      Vm.SIG (_fargs := Vm.Build_FArgs T);
  }.
  Arguments Build_FArgs {_}.


  Module V := Vm.
  Definition V := @Vm.
  Arguments V : default implicits.



Vm_n invokes Host_(n-1), whose recursive VM call invokes Vm_(n-2). Fuel 2 × 1024 + 1 therefore covers every call depth admitted by the source VM's max_stack_depth check.
  Definition recursive_module_fuel : nat := 2049%nat.

This VM is reached only after exhausting the modeled call-depth budget. It implements the source VM's depth-failure behavior without requiring an arbitrary inhabitant of the mutually recursive module types.
  Definition recursive_module_base_vm `{_fargs : FArgs} :
      V.SIG (_fargs := V.Build_FArgs (@T T_t _fargs)) :=
    {|
      V.SIG.execute :=
        fun (_message : Message.t) (_code : string)
            (state : (@T T_t _fargs).(Vm_H_signature.t)) ⇒
          ({| Result.t.status_code := Result.StatusCode.Call_depth_exceeded;
              Result.t.gas_left := (0 : Int64.t);
              Result.t.gas_refund := (0 : Int64.t);
              Result.t.output_data := Byte_string.Bytes.empty;
              Result.t.create_address :=
                RocqOfOCaml.OCamlString.make_nat 20%nat ("000"%char) |},
           state)
    |}.

  Definition recursive_module_seed `{_fargs : FArgs} :
      HOST (t := T.(Vm_H_signature.t)) ×
      V.SIG (_fargs := V.Build_FArgs T) :=
    (HostF recursive_module_base_vm, recursive_module_base_vm).

  Definition _recursive_modules_Host_Vm `{_fargs : FArgs} :
    HOST (t := T.(Vm_H_signature.t)) × V.SIG (_fargs := V.Build_FArgs T) :=
    ltac:(first [unshelve refine (recursive_module_unroll recursive_module_fuel recursive_module_seed
      (fun (_recursive_modules :
        HOST (t := T.(Vm_H_signature.t)) × V.SIG (_fargs := V.Build_FArgs T)) ⇒
        let Host := fst _recursive_modules in
        let Vm := snd _recursive_modules in
        ((HostF Vm), (VmF Host)))
    )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
    recursive_module_seed | eassumption | typeclasses eauto]).

  Definition Host `{_fargs : FArgs} : HOST (t := T.(Vm_H_signature.t)) :=
    ltac:(first [unshelve refine (fst _recursive_modules_Host_Vm
    )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
    recursive_module_seed | eassumption | typeclasses eauto]).

  Definition Vm `{_fargs : FArgs} : V.SIG (_fargs := V.Build_FArgs T) :=
    ltac:(first [unshelve refine (snd _recursive_modules_Host_Vm
    )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
    recursive_module_seed | eassumption | typeclasses eauto]).

  Module Instantiate_signature.
    Record signature `{_fargs : FArgs} : Type := {
      Host : HOST (t := T.(Vm_H_signature.t));
      Vm : Vm.SIG (_fargs := Vm.Build_FArgs T);
    }.
  End Instantiate_signature.
  Definition Instantiate_signature `{_fargs : FArgs} :=
    @Instantiate_signature.signature _ _.
  Arguments Instantiate_signature {_ _}.

  Definition functor `{_fargs : FArgs} :Instantiate_signature :=
    {|
      Instantiate_signature.Host := Host;
      Instantiate_signature.Vm := Vm
    |}.
End Instantiate.
Definition Instantiate {T_t : Set}
  (T : Vm_H_signature (t := T_t))
  (HostF :
    ∀ (Vm : Vm.SIG (_fargs := Vm.Build_FArgs T)),
    HOST (t := T.(Vm_H_signature.t)))
  (VmF :
    ∀ (H : HOST (t := T.(Vm_H_signature.t))),
    Vm.SIG (_fargs := Vm.Build_FArgs T)) :=
  @Instantiate.functor T_t
    (Instantiate.Build_FArgs T HostF VmF).