Library SetcodeConcrete.guardedaccounthost
Recursive calls preserve the guarded delegated account
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.protectedstorage QedkConcrete.protectedstoragehost
QedkConcrete.vmcallframe QedkConcrete.hostcode QedkConcrete.hostframe
QedkConcrete.vmhostframe QedkConcrete.assemblychecked QedkConcrete.contexthostinstance.
Require Import SetcodeConcrete.guardedaccountcreate SetcodeConcrete.guardedaccountframe
SetcodeConcrete.creationtracking.
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.
Section Host.
Context {F : Host.Instantiate.FArgs}.
Context (query : Addr.t) (delegated : string) (storage : list (string × string))
(block : N) (sender : Addr.t).
Local Abbreviation native := Reserve_balance.precompile_address.
Local Abbreviation protected := (guardedAccount query delegated storage block sender).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs
(@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Context (Htracking : ∀ nested body, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ initial, preservesCallback (creationsRetained initial)
(vm.(Evmc.Vm.SIG.execute) nested body)).
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_guarded_account from_tx msg :
(∀ addr action, nativeDispatch vm msg addr = Some action →
preservesCallback (protected) action) →
(Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native) →
(∀ code s, protected s →
(Evmc.Message.t.recipient msg = native → code = Byte_string.Bytes.empty) →
protected (snd (vm.(Evmc.Vm.SIG.execute) msg code s))) →
preservesCallback (protected) (processCall from_tx msg).
Proof.
intros Hnative HcodeAddress Hvm s Hinitial.
assert (Htransfer : protected (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_guarded_account|]; 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 (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 [target|] 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|].
all: intro Hrecipient;
pose proof (protected_storage_code _ _ _ (proj1 Hmiddle)) as Hempty;
first [rewrite (HcodeAddress Hrecipient); exact Hempty|idtac].
exfalso. match type of Hdelegation with
| @Delegation.get_delegated_address ?bytes = Some _ ⇒
assert (Hbytes : bytes = Byte_string.Bytes.empty) by
(rewrite (HcodeAddress Hrecipient); exact Hempty);
pose proof (eq_trans
(eq_sym (f_equal (@Delegation.get_delegated_address) Hbytes))
Hdelegation) as Hfalse;
discriminate Hfalse
end.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.protectedstorage QedkConcrete.protectedstoragehost
QedkConcrete.vmcallframe QedkConcrete.hostcode QedkConcrete.hostframe
QedkConcrete.vmhostframe QedkConcrete.assemblychecked QedkConcrete.contexthostinstance.
Require Import SetcodeConcrete.guardedaccountcreate SetcodeConcrete.guardedaccountframe
SetcodeConcrete.creationtracking.
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.
Section Host.
Context {F : Host.Instantiate.FArgs}.
Context (query : Addr.t) (delegated : string) (storage : list (string × string))
(block : N) (sender : Addr.t).
Local Abbreviation native := Reserve_balance.precompile_address.
Local Abbreviation protected := (guardedAccount query delegated storage block sender).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs
(@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Context (Htracking : ∀ nested body, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ initial, preservesCallback (creationsRetained initial)
(vm.(Evmc.Vm.SIG.execute) nested body)).
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_guarded_account from_tx msg :
(∀ addr action, nativeDispatch vm msg addr = Some action →
preservesCallback (protected) action) →
(Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native) →
(∀ code s, protected s →
(Evmc.Message.t.recipient msg = native → code = Byte_string.Bytes.empty) →
protected (snd (vm.(Evmc.Vm.SIG.execute) msg code s))) →
preservesCallback (protected) (processCall from_tx msg).
Proof.
intros Hnative HcodeAddress Hvm s Hinitial.
assert (Htransfer : protected (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_guarded_account|]; 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 (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 [target|] 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|].
all: intro Hrecipient;
pose proof (protected_storage_code _ _ _ (proj1 Hmiddle)) as Hempty;
first [rewrite (HcodeAddress Hrecipient); exact Hempty|idtac].
exfalso. match type of Hdelegation with
| @Delegation.get_delegated_address ?bytes = Some _ ⇒
assert (Hbytes : bytes = Byte_string.Bytes.empty) by
(rewrite (HcodeAddress Hrecipient); exact Hempty);
pose proof (eq_trans
(eq_sym (f_equal (@Delegation.get_delegated_address) Hbytes))
Hdelegation) as Hfalse;
discriminate Hfalse
end.
Qed.
On failure the Host restores the call-entry state. On success it keeps
the state protected by the recursive VM/native-call induction steps.
The top-level reserve check is read-only and can only cause rollback.
Lemma call_impl_guarded_account from_tx msg :
(∀ nested addr action, nativeDispatch vm nested addr = Some action →
preservesCallback (protected) action) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ s, protected s →
(Evmc.Message.t.recipient nested = native → code = Byte_string.Bytes.empty) →
protected (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native)) →
preservesCallback (protected) (callImpl from_tx msg).
Proof using F Htracking block delegated query sender storage vm.
intros Hnative Hvm Hgas Htarget s Hinitial.
pose proof (process_create_guarded_account
(F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
query delegated storage block sender msg Htracking
(fun nested code Hbound initial Hprotected Hneq ⇒
Hvm nested code Hbound initial Hprotected (fun Heq ⇒ False_rect _ (Hneq Heq))) Hgas) as Hcreate.
assert (Hcall :
(Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native) →
preservesCallback (protected) (processCall from_tx msg)).
{ intro Hcode. exact (process_call_guarded_account from_tx msg
(Hnative msg) Hcode (fun code initial ⇒ Hvm msg code Hgas initial)). }
unfold H.call_impl.
destruct (Evmc.Message.t.kind msg) eqn:Hkind;
cbn -[H.process_call H.process_create Addr.of_contract_creation Reserve_balance.dipped_into_reserve].
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hcore : protected (snd run)) by
(lazymatch run with ?f ?initial ⇒
assert (Hentry : protected initial) by
(eapply (@guarded_account_same query delegated storage block sender s);
[reflexivity|reflexivity|reflexivity|reflexivity|reflexivity|exact Hinitial]);
first [exact (Hcreate initial Hentry)|
destruct Htarget as [Hbad|[Hbad|Htarget]]; try discriminate;
exact (Hcall Htarget initial Hentry)] end);
destruct run as [result intermediate] eqn:Hrun
end.
all: destruct result as [status gasleft refund output address].
all: destruct from_tx as [tx|]; destruct status;
cbn -[Reserve_balance.dipped_into_reserve];
unfold Monad.Make_Monad.when_; cbn -[Reserve_balance.dipped_into_reserve];
try exact Hcore; try exact Hinitial.
all: try (eapply (@guarded_account_same query delegated storage block sender s);
[reflexivity|reflexivity|reflexivity|reflexivity|reflexivity|exact Hinitial]).
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; first [exact Hinitial|exact Hcore|
eapply (@guarded_account_same query delegated storage block sender s);
[reflexivity|reflexivity|reflexivity|reflexivity|reflexivity|exact Hinitial]].
Qed.
Lemma call_guarded_account self msg :
(∀ nested addr action, nativeDispatch vm nested addr = Some action →
preservesCallback (protected) action) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ s, protected s →
(Evmc.Message.t.recipient nested = native → code = Byte_string.Bytes.empty) →
protected (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
self ≠ native → childCallFrom self msg →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native)) →
preservesCallback (protected) (hostCall msg).
Proof using F Htracking block delegated query sender storage vm.
intros Hnative Hvm Hgas Hdifferent Horigin Htarget s Hinitial.
pose proof (call_impl_guarded_account None msg
Hnative Hvm Hgas Htarget) as Himpl.
unfold H.call.
destruct (Evmc.Message.t.kind msg) eqn:Hkind.
all: cbn -[H.call_impl H.try_increment_nonce].
all: try (apply Himpl; exact Hinitial).
all: assert (Hsender : Evmc.Message.t.sender msg ≠ native) by
(unfold childCallFrom in Horigin; rewrite Hkind in Horigin; rewrite Horigin; exact Hdifferent).
all: assert (Hincrement : protected (snd (@H.try_increment_nonce
(H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) (Evmc.Message.t.sender msg) s))).
all: try (unfold H.try_increment_nonce, H.M;
cbn -[W.account Numeric.U64.op_eq Numeric.U64.op_plus];
match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
[exact Hinitial|eapply guarded_account_write with (s := s) (keep := false)
(addr := Evmc.Message.t.sender msg); [exact Hinitial|exact Hsender|]];
intro Heq; rewrite Heq; exact (proj2 (proj2 (proj2 Hinitial)))).
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
destruct run as [ok middle] end.
all: cbn [snd] in Hincrement; destruct ok;
cbn -[H.call_impl]; [apply Himpl; exact Hincrement|exact Hincrement].
Qed.
End Host.
Print Assumptions call_impl_guarded_account.
Print Assumptions call_guarded_account.
(∀ nested addr action, nativeDispatch vm nested addr = Some action →
preservesCallback (protected) action) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ s, protected s →
(Evmc.Message.t.recipient nested = native → code = Byte_string.Bytes.empty) →
protected (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native)) →
preservesCallback (protected) (callImpl from_tx msg).
Proof using F Htracking block delegated query sender storage vm.
intros Hnative Hvm Hgas Htarget s Hinitial.
pose proof (process_create_guarded_account
(F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
query delegated storage block sender msg Htracking
(fun nested code Hbound initial Hprotected Hneq ⇒
Hvm nested code Hbound initial Hprotected (fun Heq ⇒ False_rect _ (Hneq Heq))) Hgas) as Hcreate.
assert (Hcall :
(Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native) →
preservesCallback (protected) (processCall from_tx msg)).
{ intro Hcode. exact (process_call_guarded_account from_tx msg
(Hnative msg) Hcode (fun code initial ⇒ Hvm msg code Hgas initial)). }
unfold H.call_impl.
destruct (Evmc.Message.t.kind msg) eqn:Hkind;
cbn -[H.process_call H.process_create Addr.of_contract_creation Reserve_balance.dipped_into_reserve].
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hcore : protected (snd run)) by
(lazymatch run with ?f ?initial ⇒
assert (Hentry : protected initial) by
(eapply (@guarded_account_same query delegated storage block sender s);
[reflexivity|reflexivity|reflexivity|reflexivity|reflexivity|exact Hinitial]);
first [exact (Hcreate initial Hentry)|
destruct Htarget as [Hbad|[Hbad|Htarget]]; try discriminate;
exact (Hcall Htarget initial Hentry)] end);
destruct run as [result intermediate] eqn:Hrun
end.
all: destruct result as [status gasleft refund output address].
all: destruct from_tx as [tx|]; destruct status;
cbn -[Reserve_balance.dipped_into_reserve];
unfold Monad.Make_Monad.when_; cbn -[Reserve_balance.dipped_into_reserve];
try exact Hcore; try exact Hinitial.
all: try (eapply (@guarded_account_same query delegated storage block sender s);
[reflexivity|reflexivity|reflexivity|reflexivity|reflexivity|exact Hinitial]).
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; first [exact Hinitial|exact Hcore|
eapply (@guarded_account_same query delegated storage block sender s);
[reflexivity|reflexivity|reflexivity|reflexivity|reflexivity|exact Hinitial]].
Qed.
Lemma call_guarded_account self msg :
(∀ nested addr action, nativeDispatch vm nested addr = Some action →
preservesCallback (protected) action) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ s, protected s →
(Evmc.Message.t.recipient nested = native → code = Byte_string.Bytes.empty) →
protected (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
self ≠ native → childCallFrom self msg →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native)) →
preservesCallback (protected) (hostCall msg).
Proof using F Htracking block delegated query sender storage vm.
intros Hnative Hvm Hgas Hdifferent Horigin Htarget s Hinitial.
pose proof (call_impl_guarded_account None msg
Hnative Hvm Hgas Htarget) as Himpl.
unfold H.call.
destruct (Evmc.Message.t.kind msg) eqn:Hkind.
all: cbn -[H.call_impl H.try_increment_nonce].
all: try (apply Himpl; exact Hinitial).
all: assert (Hsender : Evmc.Message.t.sender msg ≠ native) by
(unfold childCallFrom in Horigin; rewrite Hkind in Horigin; rewrite Horigin; exact Hdifferent).
all: assert (Hincrement : protected (snd (@H.try_increment_nonce
(H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) (Evmc.Message.t.sender msg) s))).
all: try (unfold H.try_increment_nonce, H.M;
cbn -[W.account Numeric.U64.op_eq Numeric.U64.op_plus];
match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
[exact Hinitial|eapply guarded_account_write with (s := s) (keep := false)
(addr := Evmc.Message.t.sender msg); [exact Hinitial|exact Hsender|]];
intro Heq; rewrite Heq; exact (proj2 (proj2 (proj2 Hinitial)))).
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
destruct run as [ok middle] end.
all: cbn [snd] in Hincrement; destruct ok;
cbn -[H.call_impl]; [apply Himpl; exact Hincrement|exact Hincrement].
Qed.
End Host.
Print Assumptions call_impl_guarded_account.
Print Assumptions call_guarded_account.