Library QedkConcrete.vmarithmeticwords

Word and Host invariants for concrete arithmetic instructions

Even wrapping arithmetic produces an in-range word. Checked signed conversions are justified by the operands popped from the stack, not by assuming that the generated integer aliases enforce EVM word widths.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.wordrange QedkConcrete.vmmachinewords
  QedkConcrete.vmopcodeframe QedkConcrete.vmrun QedkConcrete.nativeindices.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Ltac solve_word_range :=
  first [assumption |
  lazymatch goal with
  | |- wordRange (@U.op_plus _ _) ⇒ apply word_add
  | |- wordRange (@U.op_minus _ _) ⇒ apply word_sub
  | |- wordRange (@U.op_star _ _) ⇒ apply word_mul
  | |- wordRange (@U.op_div _ _) ⇒ apply word_div
  | |- wordRange (@U.rem _ _) ⇒ apply word_rem
  | |- wordRange (@U.modulo _ _) ⇒ apply word_lift2
  | |- wordRange (@U.addmod _ _ _) ⇒ apply word_addmod
  | |- wordRange (@U.mulmod _ _ _) ⇒ apply word_mulmod
  | |- wordRange (@U.exp _ _) ⇒ apply word_exp
  | |- wordRange (@U.logand _ _) ⇒ apply word_and
  | |- wordRange (@U.logor _ _) ⇒ apply word_or
  | |- wordRange (@U.logxor _ _) ⇒ apply word_xor
  | |- wordRange (@U.lognot _) ⇒ apply word_not
  | |- wordRange (U.zero) ⇒ apply word_zero
  | |- wordRange (U.one) ⇒ apply word_one
  | |- wordRange (@U.of_bool _) ⇒ apply word_bool
  | |- wordRange (@U.of_byte _) ⇒ apply word_byte
  | |- wordRange (@U.shift_left _ _) ⇒ apply word_shift_left
  | |- wordRange (@U.shift_right _ _) ⇒ apply word_shift_right
  | |- wordRange (@I.as_unsigned _ _) ⇒ apply word_as_unsigned; solve_word_range
  | |- signedWordRange (I.zero) ⇒ apply signed_zero
  | |- signedWordRange (I.op_tildedollar (-1)) ⇒ apply signed_minus_one
  | |- signedWordRange (@I.op_div _ _) ⇒ apply signed_div
  | |- signedWordRange (@I.rem _ _) ⇒ apply signed_rem
  | |- signedWordRange (@I.shift_right _ _) ⇒ apply signed_shift_right
  | |- signedWordRange (@U.sign_extend _ _) ⇒
    apply signed_sign_extend;
    repeat match goal with H : @U.to_int_opt _ = Some _ |- _ ⇒ apply word_to_int_opt in H end;
    repeat match goal with H : wordRange _ |- _ ⇒ unfold wordRange in H end;
    repeat match goal with H : Z.ltb _ _ = true |- _ ⇒ apply Z.ltb_lt in H end;
    cbn [fst snd] in *;
    lia
  | |- wordRange (@U.of_int (OCamlInt.sub _ (U.significant_bits ?v))) ⇒
    apply word_int; change (0 ≤ OCamlInt.repr (256 - U.significant_bits v) < 2^256);
    let Hbits := fresh "Hbits" in
    assert (Hbits : 0 ≤ U.significant_bits v ≤ 256) by (apply word_significant_bits; assumption);
    rewrite native_repr_small by lia; lia
  | |- ?P (if ?b then _ else _) ⇒ destruct b eqn:?; solve_word_range
  | |- ?P (match ?v with _ ⇒ _ end) ⇒ destruct v eqn:?; cbn beta iota zeta; solve_word_range
  | |- ?g ⇒ fail "Unhandled word range:" g
  end].

Ltac unpack_operand :=
  repeat match goal with
  | value : _ × _ |- _ ⇒ destruct value
  | H : ∃ _, _ |- _ ⇒ destruct H
  | H : _ ∧ _ |- _ ⇒ destruct H
  | H : ?value = _ |- _ ⇒ is_var value; subst value
  end.

Ltac solve_machine_words :=
  lazymatch goal with
  | |- machineSafe _ _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
    eapply safe_bind; [solve_machine_words|];
    let value := fresh "value" in let Hvalue := fresh "Hvalue" in
    intros value Hvalue; cbn beta iota zeta in Hvalue; unpack_operand;
    cbn beta iota zeta; solve_machine_words
  | |- machineSafe _ _ (@V.M.op_ltdollargt _ _ _ _ _ _) ⇒
    apply safe_fmap; solve_machine_words
  | |- machineSafe _ _ (@V.pop _ _) ⇒ apply safe_pop
  | |- machineSafe _ _ (@V.pop2 _ _) ⇒ apply safe_pop2
  | |- machineSafe _ _ (@V.pop3 _ _) ⇒ apply safe_pop3
  | |- machineSafe _ _ (@V.spend _ _ _) ⇒ apply safe_spend
  | |- machineSafe _ _ (@V.push _ _ _) ⇒ apply safe_push; solve_word_range
  | |- machineSafe _ _ (@V.increase_pc_and_continue _ _) ⇒ apply safe_increase_pc
  | |- machineSafe _ _ (@V.M._return _ _ _ _) ⇒ apply safe_return; exact I
  | |- machineSafe _ _ (if ?b then _ else _) ⇒ destruct b; solve_machine_words
  | |- ?g ⇒ fail "Unhandled machine word goal:" g
  end.

Section Opcodes.
  Context {Host_t : Set} {F : @MonadVM.Vm.Make.FArgs Host_t}.
  Context {E : @MonadVM.Vm.Make.Executor.FArgs Host_t F}.
  Context `{U0 : RocqOfOCaml.Basics.Unreachable Z.t}.
  Context `{U1 :
    RocqOfOCaml.Basics.Unreachable (RocqOfOCaml.OCamlSeq.t int)}.
  Context `{U2 : RocqOfOCaml.Basics.Unreachable RocqOfOCaml.OCamlInt32.t}.
  Context `{U3 : RocqOfOCaml.Basics.Unreachable MonadVM.Uint64.t}.
  Context `{U4 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Support.UintN.t}.
  Context `{U5 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.Repr.t}.
  Context `{U7 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.Integer.t}.
  Context `{U8 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.I256.t}.
  Context `{U9 : RocqOfOCaml.Basics.Unreachable MonadVM.Gas.t}.
  Context `{U10 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Delegation.Address.t}.
  Context `{U11 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Chain.Ethereum.Address.t}.
  Context `{U12 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Byte_string.Bytes.t}.
  Context `{U13 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.M.Option.t Host_t F MonadVM.Numeric.U256.t)}.
  Context `{U15 : RocqOfOCaml.Basics.Unreachable unit}.
  Context `{U16 : RocqOfOCaml.Basics.Unreachable string}.
  Context `{U17 :
    RocqOfOCaml.Basics.Unreachable (option MonadVM.Numeric.U256.t)}.
  Context `{U18 : RocqOfOCaml.Basics.Unreachable (option string)}.
  Context `{U19 :
    RocqOfOCaml.Basics.Unreachable
      (list MonadVM.Byte_string.Bytes.t)}.
  Context `{U20 : RocqOfOCaml.Basics.Unreachable (list int)}.
  Context `{U21 : RocqOfOCaml.Basics.Unreachable int}.
  Context `{U22 : RocqOfOCaml.Basics.Unreachable ascii}.
  Context `{U23 : RocqOfOCaml.Basics.Unreachable (int × int)}.
  Context `{U24 :
    RocqOfOCaml.Basics.Unreachable
      (Z.t → MonadVM.Numeric.U256.t)}.
  Context `{U25 :
    RocqOfOCaml.Basics.Unreachable
      (MonadVM.Support.UintN.t → MonadVM.Support.UintN.t)}.
  Context `{U26 :
    RocqOfOCaml.Basics.Unreachable
      (unit → RocqOfOCaml.OCamlSeq.node int)}.
  Context `{U27 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.opcode_impl Host_t F)}.
  Context `{U28 :
    RocqOfOCaml.Basics.Unreachable
      MonadVM.Numeric.IntegerBase.(MonadVM.Numeric.Make.Make_result.t)}.
  Context `{U31 :
    RocqOfOCaml.Basics.Unreachable
      MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.t)}.
  Context `{U32 :
    RocqOfOCaml.Basics.Unreachable
      (MonadVM.Numeric.U256.t → MonadVM.Numeric.U256.t →
        @MonadVM.Vm.Make.Memory.t Host_t F →
        @MonadVM.Vm.Make.M.Option.t Host_t F
          (@MonadVM.Vm.Make.Memory.t Host_t F))}.
  Context `{U33 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.Memory.t Host_t F →
        MonadVM.Support.UintN.t)}.

  Let State := @MonadVM.Vm.Make.MachineState.t Host_t F.

Local Abbreviation executeOpcode := (@dispatch Host_t F E).

Lemma arithmetic_preserves_words_core (invariant : Host_t → Prop) opcode :
  machineSafe invariant (fun _ ⇒ True) (executeOpcode Opcode.Clz) →
  arithmeticOpcode opcode = true → machineSafe invariant (fun _ ⇒ True) (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_machine_words.
Qed.

Lemma arithmetic_preserves_words (invariant : Host_t → Prop) opcode :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  arithmeticOpcode opcode = true → machineSafe invariant (fun _ ⇒ True) (executeOpcode opcode).
Proof. intro Hrevision. apply arithmetic_preserves_words_core.
  cbn [dispatch V.Executor.execute_opcode]. unfold V.Executor.clz.
  rewrite Hrevision. solve_machine_words. Qed.

End Opcodes.

Print Assumptions arithmetic_preserves_words.