Library SetcodeConcrete.nonceprecompile
Native precompiles preserve protected senders' nonces
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Precompiles.
Require Import QedkConcrete.accountnonces QedkConcrete.hostnonce QedkConcrete.hostframe
QedkConcrete.worldaccounts QedkConcrete.stringmaps QedkConcrete.vmhostframe
SetcodeConcrete.noncebudget SetcodeConcrete.nonceeffects
SetcodeConcrete.noncecreate SetcodeConcrete.checkednonce.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module R := Reserve_balance_introspection.Make.
Module P := Precompiles.Make.
Section ReserveSetter.
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (W.account None addr)).
Local Abbreviation activate := Reserve_balance.activate_precompile.
Local Abbreviation setter := Reserve_balance.set_reserve_balance.
Lemma account_write_other_nonce keep world addr value query :
stringmaps.WellFormed (W.t.accounts world) → addr ≠ query →
A.t.nonce (readAccount (State.op_pointcaretlparenrparenltminus world
(@W.account (Some keep) addr) value) query) = A.t.nonce (readAccount world query).
Proof.
intros Hwf Hdifferent. cbv beta.
rewrite accountnonces.nonce_after_account_write by exact Hwf.
destruct (String.eqb addr query) eqn:Heq; [|reflexivity].
apply String.eqb_eq in Heq. contradiction.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Precompiles.
Require Import QedkConcrete.accountnonces QedkConcrete.hostnonce QedkConcrete.hostframe
QedkConcrete.worldaccounts QedkConcrete.stringmaps QedkConcrete.vmhostframe
SetcodeConcrete.noncebudget SetcodeConcrete.nonceeffects
SetcodeConcrete.noncecreate SetcodeConcrete.checkednonce.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module R := Reserve_balance_introspection.Make.
Module P := Precompiles.Make.
Section ReserveSetter.
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (W.account None addr)).
Local Abbreviation activate := Reserve_balance.activate_precompile.
Local Abbreviation setter := Reserve_balance.set_reserve_balance.
Lemma account_write_other_nonce keep world addr value query :
stringmaps.WellFormed (W.t.accounts world) → addr ≠ query →
A.t.nonce (readAccount (State.op_pointcaretlparenrparenltminus world
(@W.account (Some keep) addr) value) query) = A.t.nonce (readAccount world query).
Proof.
intros Hwf Hdifferent. cbv beta.
rewrite accountnonces.nonce_after_account_write by exact Hwf.
destruct (String.eqb addr query) eqn:Heq; [|reflexivity].
apply String.eqb_eq in Heq. contradiction.
Qed.
Activation touches the reserve precompile's nonce; all other accounts'
nonces survive setting or replacing their configured threshold.
Lemma setter_other_nonce world block addr value after query :
query ≠ @Reserve_balance.precompile_address →
stringmaps.WellFormed (W.t.accounts world) → setter world block addr value = Some after →
stringmaps.WellFormed (W.t.accounts after) ∧
A.t.nonce (readAccount after query) = A.t.nonce (readAccount world query).
Proof.
intros Hdifferent Hwf Hset. unfold Reserve_balance.set_reserve_balance in Hset.
match type of Hset with context [match ?configResult with None ⇒ _ | Some _ ⇒ _ end] ⇒
destruct configResult as [record|] end; [|discriminate].
injection Hset as <-.
assert (Hactive : stringmaps.WellFormed (W.t.accounts (activate world))).
{ apply account_write_wellformed. exact Hwf. }
split.
- apply (@account_write_wellformed false (activate world)
(@Reserve_balance.precompile_address)). exact Hactive.
- transitivity (A.t.nonce (readAccount (activate world) query)).
+ eapply (account_write_other_nonce false (activate world)
(@Reserve_balance.precompile_address));
[exact Hactive|congruence].
+ eapply account_write_other_nonce; [exact Hwf|congruence].
Qed.
Context {F : R.FArgs}.
Local Abbreviation precompile := (@R.precompile F).
Local Abbreviation introspection := (@R.introspection F).
Local Abbreviation blockNumber := (fun s ⇒
Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block s))).
Lemma introspection_read_only msg s : snd (introspection msg s) = s.
Proof.
unfold R.introspection.
repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: reflexivity.
Qed.
Lemma precompile_world_effect msg s :
snd (precompile msg s) = s ∨
∃ value world,
setter (S.t.world_state s) (blockNumber s) (Evmc.Message.t.sender msg) value = Some world ∧
snd (precompile msg s) = S.t.with_world_state world s.
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 (left; apply introspection_read_only).
all: repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: try (left; reflexivity).
cbn -[Reserve_balance.set_reserve_balance Numeric.U256.of_bytes_be_exn
Byte_string.Bytes.sub Numeric.U256.to_repr_bytes Numeric.U256.of_uint_exn
Gas.to_uint64 Gas.op_minus Gas.of_uint64 Gas.of_int Evmc.Result.failure].
match goal with |- context [match ?writeResult with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct writeResult as [world|] eqn:Hwrite end.
- right. eexists _, world. split; [exact Hwrite|reflexivity].
- left. reflexivity.
Qed.
Lemma reserve_precompile_preserves_other_nonce query nonce msg :
query ≠ @Reserve_balance.precompile_address →
preservesCallback (@nonceInvariant query nonce) (precompile msg).
Proof.
intros Hdifferent s [Hwf Hnonce].
change (@nonceInvariant query nonce (snd (precompile msg s))).
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. split; assumption.
- rewrite Hafter. unfold nonceInvariant.
destruct (setter_other_nonce _ _ _ _ _ query Hdifferent Hwf Hwrite) as [Hmap Hvalue].
split; [exact Hmap|].
etransitivity; [exact Hvalue|exact Hnonce].
Qed.
End ReserveSetter.
Local Lemma find_singleton_value {A : Set} key (value : A) query result :
Chain.Ethereum.Address.Map.find_opt query
(Chain.Ethereum.Address.Map.of_list ((key, value) :: nil)) = Some result → result = value.
Proof.
change (OCamlMap.Make.find_opt stringOrder query ((key, value) :: nil) = Some result → result = value).
cbn [OCamlMap.Make.find_opt].
destruct (OCamlMap.Make.key_before stringOrder query key);
[discriminate|].
destruct (OCamlMap.Make.key_equal stringOrder query key); [|discriminate].
intro Heq. inversion Heq. reflexivity.
Qed.
Section Dispatch.
Context {F : P.FArgs}.
Lemma precompile_preserves_nonce cold warm access write reserve query nonce msg addr action :
P.Revision.(Precompiles.Make_Revision_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
query ≠ Reserve_balance.precompile_address →
P.try_precompile msg addr = Some action →
preservesCallback (nonceInvariant query nonce) action.
Proof.
intros Hrevision Hdifferent Hselected. unfold P.try_precompile in Hselected.
match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
destruct (@Chain.Ethereum.Address.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 P.monad_precompiles in Hselected. rewrite Hrevision in Hselected.
match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
destruct (@Chain.Ethereum.Address.Map.find_opt T addr entries) as [native|] eqn:Hnative in Hselected end.
2: discriminate.
apply 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_other_nonce. exact Hdifferent.
Qed.
End Dispatch.
Print Assumptions precompile_preserves_nonce.
query ≠ @Reserve_balance.precompile_address →
stringmaps.WellFormed (W.t.accounts world) → setter world block addr value = Some after →
stringmaps.WellFormed (W.t.accounts after) ∧
A.t.nonce (readAccount after query) = A.t.nonce (readAccount world query).
Proof.
intros Hdifferent Hwf Hset. unfold Reserve_balance.set_reserve_balance in Hset.
match type of Hset with context [match ?configResult with None ⇒ _ | Some _ ⇒ _ end] ⇒
destruct configResult as [record|] end; [|discriminate].
injection Hset as <-.
assert (Hactive : stringmaps.WellFormed (W.t.accounts (activate world))).
{ apply account_write_wellformed. exact Hwf. }
split.
- apply (@account_write_wellformed false (activate world)
(@Reserve_balance.precompile_address)). exact Hactive.
- transitivity (A.t.nonce (readAccount (activate world) query)).
+ eapply (account_write_other_nonce false (activate world)
(@Reserve_balance.precompile_address));
[exact Hactive|congruence].
+ eapply account_write_other_nonce; [exact Hwf|congruence].
Qed.
Context {F : R.FArgs}.
Local Abbreviation precompile := (@R.precompile F).
Local Abbreviation introspection := (@R.introspection F).
Local Abbreviation blockNumber := (fun s ⇒
Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block s))).
Lemma introspection_read_only msg s : snd (introspection msg s) = s.
Proof.
unfold R.introspection.
repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: reflexivity.
Qed.
Lemma precompile_world_effect msg s :
snd (precompile msg s) = s ∨
∃ value world,
setter (S.t.world_state s) (blockNumber s) (Evmc.Message.t.sender msg) value = Some world ∧
snd (precompile msg s) = S.t.with_world_state world s.
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 (left; apply introspection_read_only).
all: repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: try (left; reflexivity).
cbn -[Reserve_balance.set_reserve_balance Numeric.U256.of_bytes_be_exn
Byte_string.Bytes.sub Numeric.U256.to_repr_bytes Numeric.U256.of_uint_exn
Gas.to_uint64 Gas.op_minus Gas.of_uint64 Gas.of_int Evmc.Result.failure].
match goal with |- context [match ?writeResult with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct writeResult as [world|] eqn:Hwrite end.
- right. eexists _, world. split; [exact Hwrite|reflexivity].
- left. reflexivity.
Qed.
Lemma reserve_precompile_preserves_other_nonce query nonce msg :
query ≠ @Reserve_balance.precompile_address →
preservesCallback (@nonceInvariant query nonce) (precompile msg).
Proof.
intros Hdifferent s [Hwf Hnonce].
change (@nonceInvariant query nonce (snd (precompile msg s))).
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. split; assumption.
- rewrite Hafter. unfold nonceInvariant.
destruct (setter_other_nonce _ _ _ _ _ query Hdifferent Hwf Hwrite) as [Hmap Hvalue].
split; [exact Hmap|].
etransitivity; [exact Hvalue|exact Hnonce].
Qed.
End ReserveSetter.
Local Lemma find_singleton_value {A : Set} key (value : A) query result :
Chain.Ethereum.Address.Map.find_opt query
(Chain.Ethereum.Address.Map.of_list ((key, value) :: nil)) = Some result → result = value.
Proof.
change (OCamlMap.Make.find_opt stringOrder query ((key, value) :: nil) = Some result → result = value).
cbn [OCamlMap.Make.find_opt].
destruct (OCamlMap.Make.key_before stringOrder query key);
[discriminate|].
destruct (OCamlMap.Make.key_equal stringOrder query key); [|discriminate].
intro Heq. inversion Heq. reflexivity.
Qed.
Section Dispatch.
Context {F : P.FArgs}.
Lemma precompile_preserves_nonce cold warm access write reserve query nonce msg addr action :
P.Revision.(Precompiles.Make_Revision_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
query ≠ Reserve_balance.precompile_address →
P.try_precompile msg addr = Some action →
preservesCallback (nonceInvariant query nonce) action.
Proof.
intros Hrevision Hdifferent Hselected. unfold P.try_precompile in Hselected.
match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
destruct (@Chain.Ethereum.Address.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 P.monad_precompiles in Hselected. rewrite Hrevision in Hselected.
match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
destruct (@Chain.Ethereum.Address.Map.find_opt T addr entries) as [native|] eqn:Hnative in Hselected end.
2: discriminate.
apply 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_other_nonce. exact Hdifferent.
Qed.
End Dispatch.
Print Assumptions precompile_preserves_nonce.