Library QedkConcrete.reserveprecompileproperties

Concrete reserve-precompile effects

ABI validation, getters and failed setters leave state unchanged. A successful setter writes configuration for the calling execution context; it cannot alter balances or transaction history. This proof follows the actual precompile dispatcher, not an assumed list of reserve updates.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles.Reserve_balance_introspection.
Require Import QedkConcrete.reservewrites QedkConcrete.hostframe.
Require Import QedkConcrete.worldaccounts QedkConcrete.supply.
Require Import QedkConcrete.hostaccountformat QedkConcrete.vmhostframe.
Require Import QedkConcrete.bytewords.
Require Export QedkConcrete.reserveprecompileeffects.
Set Default Goal Selector "!".

Module R := Reserve_balance_introspection.Make.
Section Precompile.
Context {F : R.FArgs}.
Context {UZ : Basics.Unreachable Z.t}.
Context {Uarr : Basics.Unreachable (OCamlIarray.t int)}.
Context {UN : Basics.Unreachable Support.UintN.t}.
Context {U64 : Basics.Unreachable Numeric.U64.t}.
Context {Urepr : Basics.Unreachable U.Repr.t}.
Context {U256 : Basics.Unreachable U.t}.
Context {Uaddr : Basics.Unreachable Addr.t}.
Context {Ubytes : Basics.Unreachable Byte_string.Bytes.t}.
Context {Uunit : Basics.Unreachable unit}.
Context {Ustr : Basics.Unreachable string}.
Context {Uint : Basics.Unreachable int}.
Context {Ubyte : Basics.Unreachable ascii}.
Context {Upair : Basics.Unreachable (int × int)}.
Context {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.

Context {Useq : Basics.Unreachable (OCamlSeq.t (Addr.Map.key × A.t))}.
Context {Uint64 : Basics.Unreachable MonadVM.Int64.t}.
Context {Ugas : Basics.Unreachable Gas.t}.

Local Abbreviation precompile := (@R.precompile F).
Local Abbreviation introspection := (@R.introspection F).
Local Abbreviation setter := (@Reserve_balance.set_reserve_balance).
Local Abbreviation blockNumber := (fun s ⇒
  Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block s))).

Lemma precompile_preserves_context msg : preservesContext (precompile msg).
Proof.
  intro s. change (transactionContext (snd (precompile msg s)) = transactionContext s).
  destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
  - rewrite Hsame. reflexivity.
  - rewrite Hafter.
    destruct (@setter_preserves_history _ _ _ _ _ Hwrite) as [Hhistory Hcounters].
    unfold transactionContext. cbn [S.t.with_world_state S.t.initial_world_state
      S.t.current_block S.t.tx_origin S.t.tx_gas_limit S.t.tx_gas_price S.t.world_state].
    rewrite Hhistory, Hcounters. reflexivity.
Qed.

Lemma precompile_preserves_supply msg s :
  balanceInvariant (S.t.world_state s) →
  balanceInvariant (S.t.world_state (snd (precompile msg s))) ∧
  totalBalance (S.t.world_state (snd (precompile msg s))) = totalBalance (S.t.world_state s).
Proof.
  intro Hinvariant.
  destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
  - rewrite Hsame. split; [exact Hinvariant|reflexivity].
  - rewrite Hafter. cbn [S.t.with_world_state S.t.world_state].
    exact (@setter_preserves_supply _ _ _ _ _ Hinvariant Hwrite).
Qed.

Lemma precompile_preserves_balances msg s query :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  U.to_z (A.t.balance (State.op_pointcaretlparenrparen (S.t.world_state (snd (precompile msg s)))
    (@W.account None query))) =
  U.to_z (A.t.balance (State.op_pointcaretlparenrparen (S.t.world_state s)
    (@W.account None query))).
Proof.
  intro Hwf.
  destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
  - rewrite Hsame. reflexivity.
  - rewrite Hafter. cbn [S.t.with_world_state S.t.world_state].
    exact (@setter_preserves_balances _ _ _ _ _ query Hwf Hwrite).
Qed.
Lemma precompile_preserves_format msg : preservesCallback hostFormat (precompile msg).
Proof.
  intros s Hformat.
  change (hostFormat (snd (precompile msg s))).
  destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
  - rewrite Hsame. exact Hformat.
  - rewrite Hafter. destruct Hformat as [Hworld Htransient]. split; [|exact Htransient].
    eapply setter_preserves_format; [exact Hworld|exact Hwrite].
Qed.
Even malformed inputs return a bounded error string. The three-word getter is the largest response; no configuration or input-length premise is needed.
Lemma introspection_output_bounded msg s :
  (OCamlString.length (Evmc.Result.t.output_data (fst (introspection msg s))) ≤ 32)%Z.
Proof.
  unfold R.introspection.
  repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  all: cbn -[U.to_repr_bytes Reserve_balance.dipped_into_reserve].
  all: try solve [lia].
  match goal with |- context [Reserve_balance.dipped_into_reserve ?revision ?state] ⇒
    destruct (Reserve_balance.dipped_into_reserve revision state)
  end.
  cbn -[U.to_repr_bytes]. rewrite word_bytes_encoding_length. lia.
Qed.

Lemma precompile_output_bounded msg s :
  (OCamlString.length (Evmc.Result.t.output_data (fst (precompile msg s))) ≤ 96)%Z.
Proof.
  unfold R.precompile. cbn beta zeta.
  repeat match goal with
    |- context [match ?methods with None ⇒ _ | Some _ ⇒ _ end] ⇒
      destruct methods as [[gas length]|]
  end.
  all: try (pose proof (introspection_output_bounded msg s); lia).
  all: repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  all: cbn -[Reserve_balance.set_reserve_balance U.to_repr_bytes
    U.of_bytes_be_exn Byte_string.Bytes.sub U.of_uint_exn
    Reserve_config.read Reserve_balance.stored_config].
  all: try (repeat rewrite byte_append_length; repeat rewrite word_bytes_encoding_length; lia).
  match goal with |- context [match ?writeResult with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct writeResult as [world|] end.
  all: cbn -[U.to_repr_bytes U.of_uint_exn]; try rewrite word_bytes_encoding_length; lia.
Qed.
End Precompile.

Print Assumptions precompile_preserves_context.
Print Assumptions precompile_preserves_supply.
Print Assumptions precompile_preserves_balances.
Print Assumptions precompile_preserves_format.
Print Assumptions precompile_output_bounded.