Library SetcodeConcrete.nonceeffects
Concrete nonce effects used by the SETCODE increment bound
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution MonadVM.Host
QedkConcrete.worldaccounts SetcodeConcrete.noncebudget.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module E := Execution.Make.
Module H := Host.Make.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Lemma empty_account_nonce account :
A.is_empty account = true → A.t.nonce account = 0.
Proof.
unfold A.is_empty. intro Hempty.
apply Bool.andb_true_iff in Hempty as [_ Hempty].
apply Bool.andb_true_iff in Hempty as [Hnonce _].
rewrite rules.u64_eq in Hnonce. apply Z.eqb_eq in Hnonce. exact Hnonce.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution MonadVM.Host
QedkConcrete.worldaccounts SetcodeConcrete.noncebudget.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module E := Execution.Make.
Module H := Host.Make.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Lemma empty_account_nonce account :
A.is_empty account = true → A.t.nonce account = 0.
Proof.
unfold A.is_empty. intro Hempty.
apply Bool.andb_true_iff in Hempty as [_ Hempty].
apply Bool.andb_true_iff in Hempty as [Hnonce _].
rewrite rules.u64_eq in Hnonce. apply Z.eqb_eq in Hnonce. exact Hnonce.
Qed.
Account normalization deletes only a zero-nonce account.
Lemma nonce_after_write keep world addr value query :
stringmaps.WellFormed (W.t.accounts world) →
nonceAt (State.op_pointcaretlparenrparenltminus world
(W.account (Some keep) addr) value) query =
if String.eqb addr query then A.t.nonce value else nonceAt world query.
Proof.
intro Hwf. unfold nonceAt. rewrite account_read_after_write by exact Hwf.
destruct (String.eqb addr query); [|reflexivity].
destruct keep; [reflexivity|].
destruct (A.is_empty value) eqn:Hempty; [|reflexivity].
symmetry. exact (empty_account_nonce value Hempty).
Qed.
Lemma increment_upper nonce :
0 ≤ nonce → 0 ≤ Numeric.U64.op_plus nonce Numeric.U64.one ≤ nonce + 1.
Proof.
intro Hnonce.
change (0 ≤ Z.land (Z.shiftl (nonce+1) 0) (Z.ones 64) ≤ nonce+1).
rewrite Z.shiftl_0_r, Z.land_ones by lia.
split.
- apply Z.mod_pos_bound. lia.
- apply Z.mod_le; lia.
Qed.
stringmaps.WellFormed (W.t.accounts world) →
nonceAt (State.op_pointcaretlparenrparenltminus world
(W.account (Some keep) addr) value) query =
if String.eqb addr query then A.t.nonce value else nonceAt world query.
Proof.
intro Hwf. unfold nonceAt. rewrite account_read_after_write by exact Hwf.
destruct (String.eqb addr query); [|reflexivity].
destruct keep; [reflexivity|].
destruct (A.is_empty value) eqn:Hempty; [|reflexivity].
symmetry. exact (empty_account_nonce value Hempty).
Qed.
Lemma increment_upper nonce :
0 ≤ nonce → 0 ≤ Numeric.U64.op_plus nonce Numeric.U64.one ≤ nonce + 1.
Proof.
intro Hnonce.
change (0 ≤ Z.land (Z.shiftl (nonce+1) 0) (Z.ones 64) ≤ nonce+1).
rewrite Z.shiftl_0_r, Z.land_ones by lia.
split.
- apply Z.mod_pos_bound. lia.
- apply Z.mod_le; lia.
Qed.
A code lens write preserves even nonce zero, despite empty-account
normalization. world is the execution state before the write.
Lemma code_write_preserves_nonce world addr code query :
stringmaps.WellFormed (W.t.accounts world) →
nonceAt (State.op_pointcaretlparenrparenltminus world (W.account (Some false) addr)
(A.t.with_code code (State.op_pointcaretlparenrparen world (W.account None addr)))) query =
nonceAt world query.
Proof.
intro Hwf. rewrite nonce_after_write by exact Hwf.
destruct (String.eqb addr query) eqn:Heq; [|reflexivity].
apply String.eqb_eq in Heq. subst query. reflexivity.
Qed.
Theorem setcodefrom_preserves_nonce {F : H.FArgs} current source static gas s query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
nonceAt (S.t.world_state (snd (H.setcodefrom current source static gas s))) query =
nonceAt (S.t.world_state s) query.
Proof.
intro Hwf. unfold H.setcodefrom.
destruct H.ChainParams.(Chain.Monad.PARAMS.revision); try reflexivity.
match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
destruct prices as [[[[cold warm] currentCost] writeCost] reserveCost] end.
cbn beta iota zeta.
repeat match goal with |- context [if ?condition then _ else _] ⇒
destruct condition eqn:?; cbn beta iota zeta end.
all: try reflexivity.
all: match goal with
| |- context [State.op_pointcaretlparenrparenltminus ?state _ ?code] ⇒
exact (code_write_preserves_nonce (S.t.world_state state) current code query Hwf)
end.
Qed.
Theorem create_increment_nonce_bound {F : H.FArgs} addr s query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
0 ≤ nonceAt (S.t.world_state s) query →
let after := snd (H.try_increment_nonce addr s) in
stringmaps.WellFormed (W.t.accounts (S.t.world_state after)) ∧
0 ≤ nonceAt (S.t.world_state after) query ≤ nonceAt (S.t.world_state s) query + 1.
Proof.
intros Hwf Hnonce. unfold H.try_increment_nonce, H.M.
cbn -[W.account Numeric.U64.op_eq Numeric.U64.op_plus].
match goal with |- context [if ?condition then _ else _] ⇒ destruct condition end.
all: cbn beta iota zeta delta [Monad.Identity.Impl._return fst snd].
- split; [exact Hwf|lia].
- split.
+ apply account_write_wellformed. exact Hwf.
+ change (0 ≤ nonceAt (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some false) addr)
(A.t.with_nonce (Numeric.U64.op_plus (nonceAt (S.t.world_state s) addr) Numeric.U64.one)
(State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None addr)))) query ≤
nonceAt (S.t.world_state s) query + 1).
rewrite nonce_after_write by exact Hwf.
destruct (String.eqb addr query) eqn:Heq.
× apply String.eqb_eq in Heq. subst query. apply increment_upper. exact Hnonce.
× lia.
Qed.
Section Authorizations.
Context {F : E.FArgs}.
Theorem authorization_nonce_bound s auth query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
0 ≤ nonceAt (S.t.world_state s) query →
let after := snd (E.process_authorization s auth) in
stringmaps.WellFormed (W.t.accounts (S.t.world_state after)) ∧
0 ≤ nonceAt (S.t.world_state after) query ≤ nonceAt (S.t.world_state s) query + 1.
Proof.
intros Hwf Hnonce. unfold E.process_authorization.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: cbn beta iota zeta delta [fst snd].
- split; [exact Hwf|lia].
- destruct (T.Authorization.authority auth) as [authority|].
2: cbn beta iota zeta delta [fst snd]; split; [exact Hwf|lia].
cbn beta iota zeta delta [S.t.with_accessed_addresses S.t.world_state].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
2: change (stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) ∧
0 ≤ nonceAt (S.t.world_state s) query ≤ nonceAt (S.t.world_state s) query + 1);
split; [exact Hwf|lia].
match goal with |- context [State.op_pointcaretlparenrparenltminus _
(S.account None authority) ?value] ⇒ set (updated := value) end.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: change (stringmaps.WellFormed (W.t.accounts
(State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some false) authority) updated)) ∧
0 ≤ nonceAt (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some false) authority) updated) query ≤
nonceAt (S.t.world_state s) query + 1).
all: split; [apply account_write_wellformed; exact Hwf|].
all: rewrite nonce_after_write by exact Hwf.
all: destruct (String.eqb authority query) eqn:Heq; [|lia].
all: apply String.eqb_eq in Heq; subst query; apply increment_upper; exact Hnonce.
Qed.
Lemma authorization_selfdestruct s auth :
S.t.self_destruct (snd (E.process_authorization s auth)) = S.t.self_destruct s.
Proof.
unfold E.process_authorization.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [reflexivity|].
destruct (T.Authorization.authority auth) as [authority|]; [|reflexivity].
cbn beta iota zeta delta [S.t.with_accessed_addresses S.t.world_state].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [|reflexivity].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; reflexivity.
Qed.
stringmaps.WellFormed (W.t.accounts world) →
nonceAt (State.op_pointcaretlparenrparenltminus world (W.account (Some false) addr)
(A.t.with_code code (State.op_pointcaretlparenrparen world (W.account None addr)))) query =
nonceAt world query.
Proof.
intro Hwf. rewrite nonce_after_write by exact Hwf.
destruct (String.eqb addr query) eqn:Heq; [|reflexivity].
apply String.eqb_eq in Heq. subst query. reflexivity.
Qed.
Theorem setcodefrom_preserves_nonce {F : H.FArgs} current source static gas s query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
nonceAt (S.t.world_state (snd (H.setcodefrom current source static gas s))) query =
nonceAt (S.t.world_state s) query.
Proof.
intro Hwf. unfold H.setcodefrom.
destruct H.ChainParams.(Chain.Monad.PARAMS.revision); try reflexivity.
match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
destruct prices as [[[[cold warm] currentCost] writeCost] reserveCost] end.
cbn beta iota zeta.
repeat match goal with |- context [if ?condition then _ else _] ⇒
destruct condition eqn:?; cbn beta iota zeta end.
all: try reflexivity.
all: match goal with
| |- context [State.op_pointcaretlparenrparenltminus ?state _ ?code] ⇒
exact (code_write_preserves_nonce (S.t.world_state state) current code query Hwf)
end.
Qed.
Theorem create_increment_nonce_bound {F : H.FArgs} addr s query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
0 ≤ nonceAt (S.t.world_state s) query →
let after := snd (H.try_increment_nonce addr s) in
stringmaps.WellFormed (W.t.accounts (S.t.world_state after)) ∧
0 ≤ nonceAt (S.t.world_state after) query ≤ nonceAt (S.t.world_state s) query + 1.
Proof.
intros Hwf Hnonce. unfold H.try_increment_nonce, H.M.
cbn -[W.account Numeric.U64.op_eq Numeric.U64.op_plus].
match goal with |- context [if ?condition then _ else _] ⇒ destruct condition end.
all: cbn beta iota zeta delta [Monad.Identity.Impl._return fst snd].
- split; [exact Hwf|lia].
- split.
+ apply account_write_wellformed. exact Hwf.
+ change (0 ≤ nonceAt (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some false) addr)
(A.t.with_nonce (Numeric.U64.op_plus (nonceAt (S.t.world_state s) addr) Numeric.U64.one)
(State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None addr)))) query ≤
nonceAt (S.t.world_state s) query + 1).
rewrite nonce_after_write by exact Hwf.
destruct (String.eqb addr query) eqn:Heq.
× apply String.eqb_eq in Heq. subst query. apply increment_upper. exact Hnonce.
× lia.
Qed.
Section Authorizations.
Context {F : E.FArgs}.
Theorem authorization_nonce_bound s auth query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
0 ≤ nonceAt (S.t.world_state s) query →
let after := snd (E.process_authorization s auth) in
stringmaps.WellFormed (W.t.accounts (S.t.world_state after)) ∧
0 ≤ nonceAt (S.t.world_state after) query ≤ nonceAt (S.t.world_state s) query + 1.
Proof.
intros Hwf Hnonce. unfold E.process_authorization.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: cbn beta iota zeta delta [fst snd].
- split; [exact Hwf|lia].
- destruct (T.Authorization.authority auth) as [authority|].
2: cbn beta iota zeta delta [fst snd]; split; [exact Hwf|lia].
cbn beta iota zeta delta [S.t.with_accessed_addresses S.t.world_state].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
2: change (stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) ∧
0 ≤ nonceAt (S.t.world_state s) query ≤ nonceAt (S.t.world_state s) query + 1);
split; [exact Hwf|lia].
match goal with |- context [State.op_pointcaretlparenrparenltminus _
(S.account None authority) ?value] ⇒ set (updated := value) end.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: change (stringmaps.WellFormed (W.t.accounts
(State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some false) authority) updated)) ∧
0 ≤ nonceAt (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some false) authority) updated) query ≤
nonceAt (S.t.world_state s) query + 1).
all: split; [apply account_write_wellformed; exact Hwf|].
all: rewrite nonce_after_write by exact Hwf.
all: destruct (String.eqb authority query) eqn:Heq; [|lia].
all: apply String.eqb_eq in Heq; subst query; apply increment_upper; exact Hnonce.
Qed.
Lemma authorization_selfdestruct s auth :
S.t.self_destruct (snd (E.process_authorization s auth)) = S.t.self_destruct s.
Proof.
unfold E.process_authorization.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [reflexivity|].
destruct (T.Authorization.authority auth) as [authority|]; [|reflexivity].
cbn beta iota zeta delta [S.t.with_accessed_addresses S.t.world_state].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [|reflexivity].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; reflexivity.
Qed.
This is the authorization fold used by process_transaction, including
accumulation of the applied-authority list. The bound counts *entries*,
not that list: skipped entries are safe to overcount.
Definition processAuthorizations (s : S.t) (authorities : list Chain.Ethereum.Address.t)
(auths : list T.Authorization.t) :=
Result.List.fold_left
(fun previous ⇒
let '(applied, before) := previous in
fun auth ⇒ match E.process_authorization before auth with
| (None, after) ⇒ (applied, after)
| (Some a, after) ⇒ (a :: applied, after)
end) (authorities, s) auths.
Theorem authorizations_nonce_bound auths s authorities query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
0 ≤ nonceAt (S.t.world_state s) query →
let after := snd (processAuthorizations s authorities auths) in
stringmaps.WellFormed (W.t.accounts (S.t.world_state after)) ∧
0 ≤ nonceAt (S.t.world_state after) query ≤
nonceAt (S.t.world_state s) query + Z.of_nat (Stdlib.Lists.List.length auths).
Proof.
revert s authorities. induction auths as [|auth rest IH]; intros s authorities Hwf Hnonce.
- change (stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) ∧
0 ≤ nonceAt (S.t.world_state s) query ≤ nonceAt (S.t.world_state s) query + 0).
split; [exact Hwf|lia].
- pose proof (authorization_nonce_bound s auth query Hwf Hnonce) as Hstep.
unfold processAuthorizations.
change (@Result.List.fold_left) with (@OCamlList.fold_left).
cbn [OCamlList.fold_left].
destruct (E.process_authorization s auth) as [applied after] eqn:Hrun.
cbn beta iota zeta delta [fst snd] in Hstep |- ×.
destruct Hstep as [Hmap Hbound]. destruct applied as [authority|].
1: specialize (IH after (authority :: authorities) Hmap (proj1 Hbound)).
2: specialize (IH after authorities Hmap (proj1 Hbound)).
all: destruct IH as [Hmap' Hbound']; split; [exact Hmap'|].
all: unfold processAuthorizations in Hbound'.
all: change (@Result.List.fold_left) with (@OCamlList.fold_left) in Hbound'.
all: cbn [Datatypes.length]; rewrite Nat2Z.inj_succ; lia.
Qed.
Lemma authorizations_selfdestruct auths s authorities :
S.t.self_destruct (snd (processAuthorizations s authorities auths)) = S.t.self_destruct s.
Proof.
revert s authorities. induction auths as [|auth rest IH]; intros s authorities; [reflexivity|].
pose proof (authorization_selfdestruct s auth) as Hstep.
unfold processAuthorizations.
change (@Result.List.fold_left) with (@OCamlList.fold_left).
cbn [OCamlList.fold_left].
destruct (E.process_authorization s auth) as [applied after].
cbn beta iota zeta delta [fst snd] in Hstep |- ×.
destruct applied as [authority|].
- change (S.t.self_destruct (snd (processAuthorizations after (authority::authorities) rest)) = S.t.self_destruct s).
rewrite IH. exact Hstep.
- change (S.t.self_destruct (snd (processAuthorizations after authorities rest)) = S.t.self_destruct s).
rewrite IH. exact Hstep.
Qed.
Lemma bump_counters_accounts s addresses :
W.t.accounts (S.t.world_state (E.bump_emptying_transaction_counters addresses s)) =
W.t.accounts (S.t.world_state s).
Proof.
unfold E.bump_emptying_transaction_counters.
cbn beta iota zeta delta [S.t.with_world_state S.t.world_state].
generalize (S.t.world_state s). induction addresses as [|a rest IH]; intro world.
- reflexivity.
- cbn [OCamlList.fold_left]. rewrite IH. reflexivity.
Qed.
End Authorizations.
Print Assumptions setcodefrom_preserves_nonce.
Print Assumptions create_increment_nonce_bound.
Print Assumptions authorizations_nonce_bound.
(auths : list T.Authorization.t) :=
Result.List.fold_left
(fun previous ⇒
let '(applied, before) := previous in
fun auth ⇒ match E.process_authorization before auth with
| (None, after) ⇒ (applied, after)
| (Some a, after) ⇒ (a :: applied, after)
end) (authorities, s) auths.
Theorem authorizations_nonce_bound auths s authorities query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
0 ≤ nonceAt (S.t.world_state s) query →
let after := snd (processAuthorizations s authorities auths) in
stringmaps.WellFormed (W.t.accounts (S.t.world_state after)) ∧
0 ≤ nonceAt (S.t.world_state after) query ≤
nonceAt (S.t.world_state s) query + Z.of_nat (Stdlib.Lists.List.length auths).
Proof.
revert s authorities. induction auths as [|auth rest IH]; intros s authorities Hwf Hnonce.
- change (stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) ∧
0 ≤ nonceAt (S.t.world_state s) query ≤ nonceAt (S.t.world_state s) query + 0).
split; [exact Hwf|lia].
- pose proof (authorization_nonce_bound s auth query Hwf Hnonce) as Hstep.
unfold processAuthorizations.
change (@Result.List.fold_left) with (@OCamlList.fold_left).
cbn [OCamlList.fold_left].
destruct (E.process_authorization s auth) as [applied after] eqn:Hrun.
cbn beta iota zeta delta [fst snd] in Hstep |- ×.
destruct Hstep as [Hmap Hbound]. destruct applied as [authority|].
1: specialize (IH after (authority :: authorities) Hmap (proj1 Hbound)).
2: specialize (IH after authorities Hmap (proj1 Hbound)).
all: destruct IH as [Hmap' Hbound']; split; [exact Hmap'|].
all: unfold processAuthorizations in Hbound'.
all: change (@Result.List.fold_left) with (@OCamlList.fold_left) in Hbound'.
all: cbn [Datatypes.length]; rewrite Nat2Z.inj_succ; lia.
Qed.
Lemma authorizations_selfdestruct auths s authorities :
S.t.self_destruct (snd (processAuthorizations s authorities auths)) = S.t.self_destruct s.
Proof.
revert s authorities. induction auths as [|auth rest IH]; intros s authorities; [reflexivity|].
pose proof (authorization_selfdestruct s auth) as Hstep.
unfold processAuthorizations.
change (@Result.List.fold_left) with (@OCamlList.fold_left).
cbn [OCamlList.fold_left].
destruct (E.process_authorization s auth) as [applied after].
cbn beta iota zeta delta [fst snd] in Hstep |- ×.
destruct applied as [authority|].
- change (S.t.self_destruct (snd (processAuthorizations after (authority::authorities) rest)) = S.t.self_destruct s).
rewrite IH. exact Hstep.
- change (S.t.self_destruct (snd (processAuthorizations after authorities rest)) = S.t.self_destruct s).
rewrite IH. exact Hstep.
Qed.
Lemma bump_counters_accounts s addresses :
W.t.accounts (S.t.world_state (E.bump_emptying_transaction_counters addresses s)) =
W.t.accounts (S.t.world_state s).
Proof.
unfold E.bump_emptying_transaction_counters.
cbn beta iota zeta delta [S.t.with_world_state S.t.world_state].
generalize (S.t.world_state s). induction addresses as [|a rest IH]; intro world.
- reflexivity.
- cbn [OCamlList.fold_left]. rewrite IH. reflexivity.
Qed.
End Authorizations.
Print Assumptions setcodefrom_preserves_nonce.
Print Assumptions create_increment_nonce_bound.
Print Assumptions authorizations_nonce_bound.