Library SetcodeConcrete.codeframe
Ordinary Host callbacks preserve code
From Stdlib Require Import String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Host.
Require Import QedkConcrete.accountcode QedkConcrete.accountnonces
QedkConcrete.hostnonce QedkConcrete.hostframe QedkConcrete.vmhostframe
QedkConcrete.worldaccounts QedkConcrete.recursiveassembly SetcodeConcrete.nonceprecompile.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Definition codeInvariant query code (s : S.t) :=
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) ∧
A.t.code (State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None query)) = code.
Lemma code_write query code s keep addr value :
codeInvariant query code s →
(addr = query → A.t.code value = code) →
codeInvariant query code (S.t.with_world_state
(State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some keep) addr) value) s).
Proof.
intros [Hmap Hcode] Hvalue. split.
- apply account_write_wellformed. exact Hmap.
- cbn beta iota zeta delta [S.t.with_world_state S.t.world_state].
rewrite code_after_account_write by exact Hmap.
destruct (String.eqb addr query) eqn:Heq.
+ apply Hvalue, String.eqb_eq, Heq.
+ exact Hcode.
Qed.
Lemma code_world_frame query code before after :
codeInvariant query code before → S.t.world_state after = S.t.world_state before →
codeInvariant query code after.
Proof. unfold codeInvariant. intros Hbefore Hworld. rewrite Hworld. exact Hbefore. Qed.
Section Host.
Context {F : H.FArgs}.
Lemma transfer_preserves_code_invariant query code sender recipient amount :
preservesCallback (codeInvariant query code) (H.transfer_ether sender recipient amount).
Proof.
intros s Hbefore. unfold H.transfer_ether.
cbn -[Numeric.U256.op_gteq Numeric.U256.op_plus Numeric.U256.op_minus W.account].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [|exact Hbefore].
cbn beta iota zeta delta [Monad.Identity.Impl._return fst snd].
match goal with |- codeInvariant _ _ (S.t.with_world_state _ ?middle) ⇒
set (debited := middle) in × end.
assert (Hdebit : codeInvariant query code debited).
{ unfold debited. apply code_write with (s := s) (keep := false) (addr := sender); [exact Hbefore|].
intro Heq. subst sender. exact (proj2 Hbefore). }
apply code_write with (s := debited) (keep := false) (addr := recipient); [exact Hdebit|]. intro Heq. subst recipient. exact (proj2 Hdebit).
Qed.
Lemma storage_preserves_code query code addr key value :
preservesCallback (codeInvariant query code) (H.set_storage addr key value).
Proof.
intros s Hbefore. apply code_write with (s := s) (keep := true) (addr := addr); [exact Hbefore|].
intro Heq. subst addr. exact (proj2 Hbefore).
Qed.
Lemma selfdestruct_preserves_code query code addr beneficiary :
preservesCallback (codeInvariant query code) (H.selfdestruct addr beneficiary).
Proof.
intros s Hbefore. unfold H.selfdestruct.
cbn -[H.transfer_ether Addr._Set W.account].
match goal with |- context [H.transfer_ether ?from ?to ?value ?initial] ⇒
pose proof (transfer_preserves_code_invariant query code from to value initial Hbefore) as Htransfer;
destruct (H.transfer_ether from to value initial) as [ok middle]
end.
cbn beta iota zeta delta [snd] in Htransfer.
cbn -[Addr._Set W.account].
destruct (Addr._Set.(OCamlSet.S.mem) addr (S.t.accounts_created_in_current_transaction middle)).
- apply (code_write query code
(S.t.with_self_destruct (Addr._Set.(OCamlSet.S.add) addr (S.t.self_destruct middle)) middle)
false addr (A.t.with_balance Numeric.U256.zero
(State.op_pointcaretlparenrparen (S.t.world_state middle) (W.account None addr))));
[exact Htransfer|].
intro Heq. subst addr. exact (proj2 Htransfer).
- exact Htransfer.
Qed.
End Host.
Section Assembly.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Theorem concrete_primitives_preserve_code vm query code :
PrimitiveHostFrame (HostF vm) (codeInvariant query code).
Proof.
constructor; intros.
all: cbn -[H.account_exists H.get_balance H.access_account H.get_storage
H.set_storage H.access_storage H.get_transient_storage H.set_transient_storage
H.get_code_size H.get_code_hash H.copy_code H.get_block_hash H.emit_log
H.selfdestruct codeInvariant].
all: try lazymatch goal with
| |- preservesCallback _ (H.set_storage _ _ _) ⇒ apply storage_preserves_code
| |- preservesCallback _ (H.selfdestruct _ _) ⇒ apply selfdestruct_preserves_code
end.
all: intros s Hinitial; eapply code_world_frame; [exact Hinitial|].
all: first [reflexivity | rewrite copy_code_read_only; reflexivity |
unfold H.access_account, H.access_storage;
cbn -[W.account];
repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
reflexivity].
Qed.
End Assembly.
Local Abbreviation readCode := (fun world addr ⇒
A.t.code (State.op_pointcaretlparenrparen world (W.account None addr))).
Lemma activation_preserves_code world query :
stringmaps.WellFormed (W.t.accounts world) →
readCode (Reserve_balance.activate_precompile world) query = readCode world query.
Proof.
intro Hmap.
unfold Reserve_balance.activate_precompile.
set (old := State.op_pointcaretlparenrparen world (W.account None Reserve_balance.precompile_address)).
etransitivity; [exact (code_after_account_write false world Reserve_balance.precompile_address
(A.t.with_nonce (Numeric.U64.max (A.t.nonce old) Numeric.U64.one) old) query Hmap)|].
destruct (String.eqb Reserve_balance.precompile_address query) eqn:Heq; [|reflexivity].
apply String.eqb_eq in Heq. subst query. reflexivity.
Qed.
Lemma reserve_setting_preserves_code world block addr value after query :
stringmaps.WellFormed (W.t.accounts world) →
Reserve_balance.set_reserve_balance world block addr value = Some after →
stringmaps.WellFormed (W.t.accounts after) ∧ readCode after query = readCode world query.
Proof.
intros Hmap Hset. unfold Reserve_balance.set_reserve_balance in Hset.
match type of Hset with context [match ?result with None ⇒ _ | Some _ ⇒ _ end] ⇒
destruct result as [record|] end; [|discriminate].
injection Hset as <-.
assert (Hactive : stringmaps.WellFormed (W.t.accounts (Reserve_balance.activate_precompile world)))
by (apply account_write_wellformed; exact Hmap).
split; [apply (@account_write_wellformed false (Reserve_balance.activate_precompile world) Reserve_balance.precompile_address); exact Hactive|].
etransitivity; [|exact (activation_preserves_code world query Hmap)].
cbn beta iota zeta delta [State.op_pointcaretlparenrparenltminus Lens.Infix.op_pipeminusminus A.storage A.TLens.storage Lens.t.set Lens.t.get].
etransitivity; [exact (code_after_account_write false (Reserve_balance.activate_precompile world) Reserve_balance.precompile_address _ query Hactive)|].
destruct (String.eqb Reserve_balance.precompile_address query) eqn:Heq; [|reflexivity].
apply String.eqb_eq in Heq. subst query. reflexivity.
Qed.
Lemma reserve_precompile_preserves_code {F : Reserve_balance_introspection.Make.FArgs} query code msg :
preservesCallback (codeInvariant query code) (Reserve_balance_introspection.Make.precompile msg).
Proof.
intros s [Hmap Hcode].
change (codeInvariant query code (snd (@Reserve_balance_introspection.Make.precompile F msg s))).
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. split; assumption.
- rewrite Hafter. unfold codeInvariant.
destruct (reserve_setting_preserves_code _ _ _ _ _ query Hmap Hwrite) as [Hmap' Hcode'].
split; [exact Hmap'|]. etransitivity; [exact Hcode'|exact Hcode].
Qed.
Lemma precompile_preserves_code {F : Precompiles.Make.FArgs}
cold warm access write reserve query code msg addr action :
Precompiles.Make.Revision.(Precompiles.Make_Revision_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
Precompiles.Make.try_precompile msg addr = Some action →
preservesCallback (codeInvariant query code) action.
Proof.
intros Hrevision Hselected. unfold Precompiles.Make.try_precompile in Hselected.
match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
destruct (@Addr.Map.find_opt T addr entries) as [pure|] eqn:Hethereum in Hselected end.
- destruct (Evmc.Message.t.delegated msg) in Hselected; inversion Hselected; subst action;
intros s Hinitial; exact Hinitial.
- unfold Precompiles.Make.monad_precompiles in Hselected. rewrite Hrevision in Hselected.
match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
destruct (@Addr.Map.find_opt T addr entries) as [native|] eqn:Hnative in Hselected end.
2: discriminate.
apply nonceprecompile.find_singleton_value in Hnative. subst native.
match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end.
+ inversion Hselected; subst action. intros s Hinitial. exact Hinitial.
+ inversion Hselected; subst action. apply reserve_precompile_preserves_code.
Qed.
Print Assumptions concrete_primitives_preserve_code.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Host.
Require Import QedkConcrete.accountcode QedkConcrete.accountnonces
QedkConcrete.hostnonce QedkConcrete.hostframe QedkConcrete.vmhostframe
QedkConcrete.worldaccounts QedkConcrete.recursiveassembly SetcodeConcrete.nonceprecompile.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Definition codeInvariant query code (s : S.t) :=
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) ∧
A.t.code (State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None query)) = code.
Lemma code_write query code s keep addr value :
codeInvariant query code s →
(addr = query → A.t.code value = code) →
codeInvariant query code (S.t.with_world_state
(State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some keep) addr) value) s).
Proof.
intros [Hmap Hcode] Hvalue. split.
- apply account_write_wellformed. exact Hmap.
- cbn beta iota zeta delta [S.t.with_world_state S.t.world_state].
rewrite code_after_account_write by exact Hmap.
destruct (String.eqb addr query) eqn:Heq.
+ apply Hvalue, String.eqb_eq, Heq.
+ exact Hcode.
Qed.
Lemma code_world_frame query code before after :
codeInvariant query code before → S.t.world_state after = S.t.world_state before →
codeInvariant query code after.
Proof. unfold codeInvariant. intros Hbefore Hworld. rewrite Hworld. exact Hbefore. Qed.
Section Host.
Context {F : H.FArgs}.
Lemma transfer_preserves_code_invariant query code sender recipient amount :
preservesCallback (codeInvariant query code) (H.transfer_ether sender recipient amount).
Proof.
intros s Hbefore. unfold H.transfer_ether.
cbn -[Numeric.U256.op_gteq Numeric.U256.op_plus Numeric.U256.op_minus W.account].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [|exact Hbefore].
cbn beta iota zeta delta [Monad.Identity.Impl._return fst snd].
match goal with |- codeInvariant _ _ (S.t.with_world_state _ ?middle) ⇒
set (debited := middle) in × end.
assert (Hdebit : codeInvariant query code debited).
{ unfold debited. apply code_write with (s := s) (keep := false) (addr := sender); [exact Hbefore|].
intro Heq. subst sender. exact (proj2 Hbefore). }
apply code_write with (s := debited) (keep := false) (addr := recipient); [exact Hdebit|]. intro Heq. subst recipient. exact (proj2 Hdebit).
Qed.
Lemma storage_preserves_code query code addr key value :
preservesCallback (codeInvariant query code) (H.set_storage addr key value).
Proof.
intros s Hbefore. apply code_write with (s := s) (keep := true) (addr := addr); [exact Hbefore|].
intro Heq. subst addr. exact (proj2 Hbefore).
Qed.
Lemma selfdestruct_preserves_code query code addr beneficiary :
preservesCallback (codeInvariant query code) (H.selfdestruct addr beneficiary).
Proof.
intros s Hbefore. unfold H.selfdestruct.
cbn -[H.transfer_ether Addr._Set W.account].
match goal with |- context [H.transfer_ether ?from ?to ?value ?initial] ⇒
pose proof (transfer_preserves_code_invariant query code from to value initial Hbefore) as Htransfer;
destruct (H.transfer_ether from to value initial) as [ok middle]
end.
cbn beta iota zeta delta [snd] in Htransfer.
cbn -[Addr._Set W.account].
destruct (Addr._Set.(OCamlSet.S.mem) addr (S.t.accounts_created_in_current_transaction middle)).
- apply (code_write query code
(S.t.with_self_destruct (Addr._Set.(OCamlSet.S.add) addr (S.t.self_destruct middle)) middle)
false addr (A.t.with_balance Numeric.U256.zero
(State.op_pointcaretlparenrparen (S.t.world_state middle) (W.account None addr))));
[exact Htransfer|].
intro Heq. subst addr. exact (proj2 Htransfer).
- exact Htransfer.
Qed.
End Host.
Section Assembly.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Theorem concrete_primitives_preserve_code vm query code :
PrimitiveHostFrame (HostF vm) (codeInvariant query code).
Proof.
constructor; intros.
all: cbn -[H.account_exists H.get_balance H.access_account H.get_storage
H.set_storage H.access_storage H.get_transient_storage H.set_transient_storage
H.get_code_size H.get_code_hash H.copy_code H.get_block_hash H.emit_log
H.selfdestruct codeInvariant].
all: try lazymatch goal with
| |- preservesCallback _ (H.set_storage _ _ _) ⇒ apply storage_preserves_code
| |- preservesCallback _ (H.selfdestruct _ _) ⇒ apply selfdestruct_preserves_code
end.
all: intros s Hinitial; eapply code_world_frame; [exact Hinitial|].
all: first [reflexivity | rewrite copy_code_read_only; reflexivity |
unfold H.access_account, H.access_storage;
cbn -[W.account];
repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
reflexivity].
Qed.
End Assembly.
Local Abbreviation readCode := (fun world addr ⇒
A.t.code (State.op_pointcaretlparenrparen world (W.account None addr))).
Lemma activation_preserves_code world query :
stringmaps.WellFormed (W.t.accounts world) →
readCode (Reserve_balance.activate_precompile world) query = readCode world query.
Proof.
intro Hmap.
unfold Reserve_balance.activate_precompile.
set (old := State.op_pointcaretlparenrparen world (W.account None Reserve_balance.precompile_address)).
etransitivity; [exact (code_after_account_write false world Reserve_balance.precompile_address
(A.t.with_nonce (Numeric.U64.max (A.t.nonce old) Numeric.U64.one) old) query Hmap)|].
destruct (String.eqb Reserve_balance.precompile_address query) eqn:Heq; [|reflexivity].
apply String.eqb_eq in Heq. subst query. reflexivity.
Qed.
Lemma reserve_setting_preserves_code world block addr value after query :
stringmaps.WellFormed (W.t.accounts world) →
Reserve_balance.set_reserve_balance world block addr value = Some after →
stringmaps.WellFormed (W.t.accounts after) ∧ readCode after query = readCode world query.
Proof.
intros Hmap Hset. unfold Reserve_balance.set_reserve_balance in Hset.
match type of Hset with context [match ?result with None ⇒ _ | Some _ ⇒ _ end] ⇒
destruct result as [record|] end; [|discriminate].
injection Hset as <-.
assert (Hactive : stringmaps.WellFormed (W.t.accounts (Reserve_balance.activate_precompile world)))
by (apply account_write_wellformed; exact Hmap).
split; [apply (@account_write_wellformed false (Reserve_balance.activate_precompile world) Reserve_balance.precompile_address); exact Hactive|].
etransitivity; [|exact (activation_preserves_code world query Hmap)].
cbn beta iota zeta delta [State.op_pointcaretlparenrparenltminus Lens.Infix.op_pipeminusminus A.storage A.TLens.storage Lens.t.set Lens.t.get].
etransitivity; [exact (code_after_account_write false (Reserve_balance.activate_precompile world) Reserve_balance.precompile_address _ query Hactive)|].
destruct (String.eqb Reserve_balance.precompile_address query) eqn:Heq; [|reflexivity].
apply String.eqb_eq in Heq. subst query. reflexivity.
Qed.
Lemma reserve_precompile_preserves_code {F : Reserve_balance_introspection.Make.FArgs} query code msg :
preservesCallback (codeInvariant query code) (Reserve_balance_introspection.Make.precompile msg).
Proof.
intros s [Hmap Hcode].
change (codeInvariant query code (snd (@Reserve_balance_introspection.Make.precompile F msg s))).
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. split; assumption.
- rewrite Hafter. unfold codeInvariant.
destruct (reserve_setting_preserves_code _ _ _ _ _ query Hmap Hwrite) as [Hmap' Hcode'].
split; [exact Hmap'|]. etransitivity; [exact Hcode'|exact Hcode].
Qed.
Lemma precompile_preserves_code {F : Precompiles.Make.FArgs}
cold warm access write reserve query code msg addr action :
Precompiles.Make.Revision.(Precompiles.Make_Revision_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
Precompiles.Make.try_precompile msg addr = Some action →
preservesCallback (codeInvariant query code) action.
Proof.
intros Hrevision Hselected. unfold Precompiles.Make.try_precompile in Hselected.
match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
destruct (@Addr.Map.find_opt T addr entries) as [pure|] eqn:Hethereum in Hselected end.
- destruct (Evmc.Message.t.delegated msg) in Hselected; inversion Hselected; subst action;
intros s Hinitial; exact Hinitial.
- unfold Precompiles.Make.monad_precompiles in Hselected. rewrite Hrevision in Hselected.
match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
destruct (@Addr.Map.find_opt T addr entries) as [native|] eqn:Hnative in Hselected end.
2: discriminate.
apply nonceprecompile.find_singleton_value in Hnative. subst native.
match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end.
+ inversion Hselected; subst action. intros s Hinitial. exact Hinitial.
+ inversion Hselected; subst action. apply reserve_precompile_preserves_code.
Qed.
Print Assumptions concrete_primitives_preserve_code.