Library SetcodeConcrete.vmgasopcodes

Ordinary instructions do not increase gas, on success or failure

This deliberately excludes recursive CALL/CREATE and SETCODEFROM: their returned gas requires separate accounting. No assumptions on callback account-state effects are needed for this machine-gas property.
From Stdlib Require Import NArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import QedkConcrete.vminvariant QedkConcrete.vmopcodeframe QedkConcrete.vmhostframe
  SetcodeConcrete.vmgasframe.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Set Default Goal Selector "!".
Section VM.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Local Abbreviation executeOpcode := (@dispatch Host_t F E).

Lemma arithmetic_gas_bound opcode :
  gasBound (executeOpcode Opcode.Clz) →
  arithmeticOpcode opcode = true → gasBound (executeOpcode opcode).
Proof.
  intros Hclz Harithmetic. destruct opcode; cbn [arithmeticOpcode] in Harithmetic;
    try discriminate; try exact Hclz.
  all: cbn [dispatch V.Executor.execute_opcode].
  all: unfold V.Executor.add, V.Executor.mul, V.Executor.sub, V.Executor.udiv, V.Executor.sdiv,
    V.Executor.umod, V.Executor.smod, V.Executor.addmod, V.Executor.mulmod, V.Executor.exp,
    V.Executor.signextend, V.Executor.lt, V.Executor.gt, V.Executor.slt, V.Executor.sgt,
    V.Executor.eq, V.Executor.is_zero, V.Executor.bitwise_and, V.Executor.bitwise_or,
    V.Executor.bitwise_xor, V.Executor.bitwise_not, V.Executor.byte, V.Executor.shl,
    V.Executor.shr, V.Executor.sar, V.Executor.clz, V.Executor.logical_shift_opcode_impl.
  all: solve_gas_bound.
Qed.

Lemma machine_gas_bound opcode :
  gasBound (executeOpcode Opcode.Clz) →
  (∀ extra, extension_opcode extra → gasBound (executeOpcode extra)) →
  V.Params.(Vm.Make_Params_signature.debug_tstore) = false →
  machineOnlyOpcode opcode = true → gasBound (executeOpcode opcode).
Proof.
  intros Hclz Hextension Hdebug Hlocal.
  destruct (arithmeticOpcode opcode) eqn:Harithmetic.
  { apply arithmetic_gas_bound; assumption. }
  destruct opcode; cbn [arithmeticOpcode] in Harithmetic;
    cbn [machineOnlyOpcode] in Hlocal; try discriminate;
    try solve [apply Hextension; exact I].
  all: cbn [dispatch V.Executor.execute_opcode].
  all: unfold V.Executor.stop, V.Executor.keccak,
    V.Executor.address, V.Executor.origin, V.Executor.caller, V.Executor.callvalue,
    V.Executor.calldataload, V.Executor.calldatasize, V.Executor.codesize,
    V.Executor.calldatacopy, V.Executor.codecopy, V.Executor.gasprice,
    V.Executor.returndatasize, V.Executor.returndatacopy,
    V.Executor.coinbase, V.Executor.timestamp, V.Executor.number, V.Executor.prevrandao,
    V.Executor.gaslimit, V.Executor.chainid, V.Executor.basefee,
    V.Executor.blobhash, V.Executor.blobbasefee, V.Executor.pop_, V.Executor.push_,
    V.Executor.dup, V.Executor.swap, V.Executor.mload, V.Executor.mstore,
    V.Executor.mstore8, V.Executor.jump, V.Executor.jumpi, V.Executor.pc_,
    V.Executor.msize, V.Executor.gas_, V.Executor.jumpdest, V.Executor.mcopy,
    V.Executor.return_, V.Executor.revert, V.Executor.invalid, V.Executor.undefined.
  all: unfold V.Executor.copy_input_data_opcode_impl,
    V.Executor.fetch_environment_variable_opcode_impl,
    V.Executor.fetch_environment_variable_opcode_impl',
    V.Executor.debug_tstore_stack, V.Executor.extend_memory_to,
    V.Executor.check_jump_destination, V.Executor.self.
  all: try rewrite Hdebug.
  all: cbn beta iota zeta; cbn [negb].
  all: solve_gas_bound.
Qed.

Lemma primitive_gas_bound opcode :
  V.Params.(Vm.Make_Params_signature.debug_tstore) = false →
  primitiveHostOpcode opcode = true → gasBound (executeOpcode opcode).
Proof.
  intros Hdebug Hprimitive.
  destruct opcode; cbn [primitiveHostOpcode] in Hprimitive; try discriminate.
  all: cbn [dispatch V.Executor.execute_opcode].
  all: unfold V.Executor.balance, V.Executor.extcodesize, V.Executor.extcodecopy,
    V.Executor.extcodehash, V.Executor.blockhash, V.Executor.selfbalance,
    V.Executor.sload, V.Executor.sstore, V.Executor.tload, V.Executor.tstore,
    V.Executor.log, V.Executor.selfdestruct.
  all: unfold V.Executor.debug_tstore_stack, V.Executor.extend_memory_to,
    V.Executor.check_write_permissions, V.Executor.self.
  all: try rewrite Hdebug.
  all: unfold V.M.HostAPI.get_balance, V.M.HostAPI.access_account,
    V.M.HostAPI.get_code_size, V.M.HostAPI.copy_code, V.M.HostAPI.get_code_hash,
    V.M.HostAPI.get_block_hash, V.M.HostAPI.access_storage, V.M.HostAPI.get_storage,
    V.M.HostAPI.set_storage, V.M.HostAPI.get_transient_storage,
    V.M.HostAPI.set_transient_storage, V.M.HostAPI.account_exists,
    V.M.HostAPI.emit_log, V.M.HostAPI.selfdestruct, V.M.HostAPI.Base.
  all: cbn beta iota zeta; cbn [negb].
  all: solve_gas_bound.
Qed.
End VM.

Print Assumptions primitive_gas_bound.