Library SetcodeConcrete.regularhost
From Stdlib Require Import List String ZArith NArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.persistentcode QedkConcrete.hostframe
QedkConcrete.hostcode QedkConcrete.vmhostframe QedkConcrete.recursiveassembly.
Require Import SetcodeConcrete.regularaccount.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Local Opaque regularAccount.
Lemma regular_bind {U64 : Basics.Unreachable Numeric.U64.t}
{UF : Basics.Unreachable Numeric.U256.t} {A B : Set} query
(action : S.t → A × S.t) (next : A → S.t → B × S.t) :
preservesCallback (regularAccount query) action →
(∀ value, preservesCallback (regularAccount query) (next value)) →
preservesCallback (regularAccount query) (S.M.(Monad.State.State_result.op_letdollar) action next).
Proof.
intros Haction Hnext s Hinitial.
change (regularAccount query (snd (let '(value, middle) := action s in next value middle))).
specialize (Haction s Hinitial). destruct (action s) as [value middle].
apply Hnext. exact Haction.
Qed.
Section Host.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation protected := regularAccount.
Context (cold warm access write reserve : N).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Local Abbreviation nativeDispatch := (@Precompiles.Make.try_precompile
(@H.Precompiles_fargs (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm))).
Local Abbreviation processCall := (@H.process_call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).
Local Abbreviation callImpl := (@H.call_impl (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).
Local Abbreviation hostCall := (@H.call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).
Lemma process_call_preserves_regular from_tx query msg :
(∀ addr action, nativeDispatch msg addr = Some action →
preservesCallback (protected query) action) →
(∀ body s, protected query s →
protected query (snd (vm.(Evmc.Vm.SIG.execute) msg body s))) →
preservesCallback (protected query) (processCall from_tx msg).
Proof.
intros Hnative Hvm s Hinitial.
assert (Htransfer : protected query (snd
((if @H.should_transfer (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg
then @H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
(Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
else fun st ⇒ (true, st)) s))).
{ destruct (H.should_transfer msg); [apply transfer_regular|]; exact Hinitial. }
unfold H.process_call, H.M.
cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile].
match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hmiddle : protected query (snd run)) by exact Htransfer;
destruct run as [ok middle] eqn:Hrun end.
cbn beta iota zeta delta [snd] in Hmiddle.
destruct ok; [|exact Hmiddle].
destruct from_tx as [tx|].
all: cbn -[W.account Precompiles.Make.try_precompile Delegation.get_delegated_address].
all: try match goal with |- context [@Delegation.get_delegated_address ?bytes] ⇒
destruct (@Delegation.get_delegated_address bytes) as [delegated|] eqn:Hdelegation end.
all: cbn -[W.account Precompiles.Make.try_precompile].
all: match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct selection as [action|] eqn:Hselected end.
all: try (eapply Hnative; eassumption).
all: apply Hvm; exact Hmiddle.
Qed.
Lemma call_impl_preserves_regular from_tx query msg :
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
(∀ nested body, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
preservesCallback (protected query) (vm.(Evmc.Vm.SIG.execute) nested body)) →
(0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesCallback (protected query) (callImpl from_tx msg).
Proof.
intros Hrevision Hvm Hgas s Hinitial.
pose proof (process_call_preserves_regular from_tx query msg
(fun addr action Hselected ⇒ @precompile_regular
(@H.Precompiles_fargs (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm))
cold warm access write reserve query msg addr action Hrevision Hselected)
(fun body ⇒ Hvm msg body Hgas)) as Hcall.
pose proof (process_create_preserves_regular (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
query msg Hvm Hgas) as Hcreate.
unfold H.call_impl.
destruct (Evmc.Message.t.kind msg);
cbn -[H.process_call H.process_create
Chain.Ethereum.Address.of_contract_creation Reserve_balance.dipped_into_reserve].
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hcore : protected query (snd run)) by
(lazymatch run with ?action ?initial ⇒
assert (Hentry : protected query initial) by
(eapply (@regular_unchanged query s _); [exact Hinitial|reflexivity|reflexivity|reflexivity]);
first [exact (Hcall initial Hentry)|exact (Hcreate initial Hentry)]
end);
destruct run as [result intermediate] eqn:Hrun
end.
all: cbn beta iota zeta delta [snd] in Hcore.
all: destruct result as [status gasleft refund output address].
all: destruct from_tx as [tx|]; destruct status.
all: cbn beta iota zeta delta [snd fst Monad.Identity.Impl.op_gtgteq Monad.Make_Monad.when_ Monad.Identity.Impl._return Evmc.Result.StatusCode.is_success Evmc.Result.t.status_code negb].
all: try exact Hcore.
all: try (eapply (@regular_unchanged query s _); [exact Hinitial|reflexivity|reflexivity|reflexivity]).
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
assert (Hcheck : snd check = intermediate) by reflexivity;
destruct check as [dipped checked]; cbn [snd] in Hcheck; subst checked
end.
all: destruct dipped; cbn beta iota zeta delta [snd fst Monad.Identity.Impl.op_gtgteq Monad.Make_Monad.when_ Monad.Identity.Impl._return Evmc.Result.StatusCode.is_success Evmc.Result.t.status_code Evmc.Result.t.with_gas_refund Evmc.Result.t.with_status_code negb]; [eapply (@regular_unchanged query s _); [exact Hinitial|reflexivity|reflexivity|reflexivity]|exact Hcore].
Qed.
Lemma call_preserves_regular query msg :
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
(∀ nested body, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
preservesCallback (protected query) (vm.(Evmc.Vm.SIG.execute) nested body)) →
(0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesCallback (protected query) (hostCall msg).
Proof.
intros Hrevision Hvm Hgas. unfold H.call, H.M.
apply regular_bind.
- match goal with |- preservesCallback _ (if ?test then _ else _) ⇒ destruct test end.
+ apply increment_regular.
+ intros s Hinitial. exact Hinitial.
- intros ok. destruct ok.
+ apply call_impl_preserves_regular; assumption.
+ intros s Hinitial. exact Hinitial.
Qed.
End Host.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.persistentcode QedkConcrete.hostframe
QedkConcrete.hostcode QedkConcrete.vmhostframe QedkConcrete.recursiveassembly.
Require Import SetcodeConcrete.regularaccount.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Local Opaque regularAccount.
Lemma regular_bind {U64 : Basics.Unreachable Numeric.U64.t}
{UF : Basics.Unreachable Numeric.U256.t} {A B : Set} query
(action : S.t → A × S.t) (next : A → S.t → B × S.t) :
preservesCallback (regularAccount query) action →
(∀ value, preservesCallback (regularAccount query) (next value)) →
preservesCallback (regularAccount query) (S.M.(Monad.State.State_result.op_letdollar) action next).
Proof.
intros Haction Hnext s Hinitial.
change (regularAccount query (snd (let '(value, middle) := action s in next value middle))).
specialize (Haction s Hinitial). destruct (action s) as [value middle].
apply Hnext. exact Haction.
Qed.
Section Host.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation protected := regularAccount.
Context (cold warm access write reserve : N).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Local Abbreviation nativeDispatch := (@Precompiles.Make.try_precompile
(@H.Precompiles_fargs (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm))).
Local Abbreviation processCall := (@H.process_call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).
Local Abbreviation callImpl := (@H.call_impl (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).
Local Abbreviation hostCall := (@H.call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).
Lemma process_call_preserves_regular from_tx query msg :
(∀ addr action, nativeDispatch msg addr = Some action →
preservesCallback (protected query) action) →
(∀ body s, protected query s →
protected query (snd (vm.(Evmc.Vm.SIG.execute) msg body s))) →
preservesCallback (protected query) (processCall from_tx msg).
Proof.
intros Hnative Hvm s Hinitial.
assert (Htransfer : protected query (snd
((if @H.should_transfer (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg
then @H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
(Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
else fun st ⇒ (true, st)) s))).
{ destruct (H.should_transfer msg); [apply transfer_regular|]; exact Hinitial. }
unfold H.process_call, H.M.
cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile].
match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hmiddle : protected query (snd run)) by exact Htransfer;
destruct run as [ok middle] eqn:Hrun end.
cbn beta iota zeta delta [snd] in Hmiddle.
destruct ok; [|exact Hmiddle].
destruct from_tx as [tx|].
all: cbn -[W.account Precompiles.Make.try_precompile Delegation.get_delegated_address].
all: try match goal with |- context [@Delegation.get_delegated_address ?bytes] ⇒
destruct (@Delegation.get_delegated_address bytes) as [delegated|] eqn:Hdelegation end.
all: cbn -[W.account Precompiles.Make.try_precompile].
all: match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct selection as [action|] eqn:Hselected end.
all: try (eapply Hnative; eassumption).
all: apply Hvm; exact Hmiddle.
Qed.
Lemma call_impl_preserves_regular from_tx query msg :
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
(∀ nested body, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
preservesCallback (protected query) (vm.(Evmc.Vm.SIG.execute) nested body)) →
(0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesCallback (protected query) (callImpl from_tx msg).
Proof.
intros Hrevision Hvm Hgas s Hinitial.
pose proof (process_call_preserves_regular from_tx query msg
(fun addr action Hselected ⇒ @precompile_regular
(@H.Precompiles_fargs (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm))
cold warm access write reserve query msg addr action Hrevision Hselected)
(fun body ⇒ Hvm msg body Hgas)) as Hcall.
pose proof (process_create_preserves_regular (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
query msg Hvm Hgas) as Hcreate.
unfold H.call_impl.
destruct (Evmc.Message.t.kind msg);
cbn -[H.process_call H.process_create
Chain.Ethereum.Address.of_contract_creation Reserve_balance.dipped_into_reserve].
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hcore : protected query (snd run)) by
(lazymatch run with ?action ?initial ⇒
assert (Hentry : protected query initial) by
(eapply (@regular_unchanged query s _); [exact Hinitial|reflexivity|reflexivity|reflexivity]);
first [exact (Hcall initial Hentry)|exact (Hcreate initial Hentry)]
end);
destruct run as [result intermediate] eqn:Hrun
end.
all: cbn beta iota zeta delta [snd] in Hcore.
all: destruct result as [status gasleft refund output address].
all: destruct from_tx as [tx|]; destruct status.
all: cbn beta iota zeta delta [snd fst Monad.Identity.Impl.op_gtgteq Monad.Make_Monad.when_ Monad.Identity.Impl._return Evmc.Result.StatusCode.is_success Evmc.Result.t.status_code negb].
all: try exact Hcore.
all: try (eapply (@regular_unchanged query s _); [exact Hinitial|reflexivity|reflexivity|reflexivity]).
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
assert (Hcheck : snd check = intermediate) by reflexivity;
destruct check as [dipped checked]; cbn [snd] in Hcheck; subst checked
end.
all: destruct dipped; cbn beta iota zeta delta [snd fst Monad.Identity.Impl.op_gtgteq Monad.Make_Monad.when_ Monad.Identity.Impl._return Evmc.Result.StatusCode.is_success Evmc.Result.t.status_code Evmc.Result.t.with_gas_refund Evmc.Result.t.with_status_code negb]; [eapply (@regular_unchanged query s _); [exact Hinitial|reflexivity|reflexivity|reflexivity]|exact Hcore].
Qed.
Lemma call_preserves_regular query msg :
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
(∀ nested body, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
preservesCallback (protected query) (vm.(Evmc.Vm.SIG.execute) nested body)) →
(0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesCallback (protected query) (hostCall msg).
Proof.
intros Hrevision Hvm Hgas. unfold H.call, H.M.
apply regular_bind.
- match goal with |- preservesCallback _ (if ?test then _ else _) ⇒ destruct test end.
+ apply increment_regular.
+ intros s Hinitial. exact Hinitial.
- intros ok. destruct ok.
+ apply call_impl_preserves_regular; assumption.
+ intros s Hinitial. exact Hinitial.
Qed.
End Host.