Library QedkConcrete.worldaccounts
Concrete account updates
From Stdlib Require Import ZArith Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.State QedkConcrete.stringmaps QedkConcrete.reservecheck.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module W := MonadVM.State.WorldState.
Module A := MonadVM.Chain.Ethereum.Account.
Module Addr := MonadVM.Chain.Ethereum.Address.
Module U := MonadVM.Numeric.U256.
Section Accounts.
Context {U64 : Basics.Unreachable Numeric.U64.t}.
Context {U256 : Basics.Unreachable U.t}.
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation writeAccount := (fun keep world addr value ⇒
State.op_pointcaretlparenrparenltminus world (@W.account (Some keep) addr) value).
Lemma account_write_wellformed keep world addr value :
WellFormed (W.t.accounts world) →
WellFormed (W.t.accounts (writeAccount keep world addr value)).
Proof.
intro Hwf. destruct keep; apply update_wellformed; exact Hwf.
Qed.
Lemma account_read_after_write keep world addr value query :
WellFormed (W.t.accounts world) →
readAccount (writeAccount keep world addr value) query =
if String.eqb addr query then
if keep then value else
if @A.is_empty value then @A.empty else value
else readAccount world query.
Proof.
intro Hwf. destruct keep.
- match goal with |- _ = ?rhs ⇒
change ((match OCamlMap.Make.find_opt stringOrder query
(OCamlMap.Make.update stringOrder addr (fun _ ⇒ Some value) (W.t.accounts world)) with
| Some a ⇒ a | None ⇒ @A.empty end) = rhs) end.
rewrite find_update by exact Hwf. destruct (String.eqb addr query); reflexivity.
- match goal with |- _ = ?rhs ⇒
change ((match OCamlMap.Make.find_opt stringOrder query
(OCamlMap.Make.update stringOrder addr
(fun _ ⇒ if @A.is_empty value then None else Some value)
(W.t.accounts world)) with
| Some a ⇒ a | None ⇒ @A.empty end) = rhs) end.
rewrite find_update by exact Hwf. destruct (String.eqb addr query); [|reflexivity].
destruct (@A.is_empty value); reflexivity.
Qed.
Lemma account_empty_balance account :
@A.is_empty account = true → U.to_z (A.t.balance account) = 0%Z.
Proof.
unfold A.is_empty. intro Hempty.
apply Bool.andb_true_iff in Hempty as [Hbalance _].
change (Z.eqb (match Z.compare (U.to_z (A.t.balance account)) 0 with
| Eq ⇒ 0 | Lt ⇒ -1 | Gt ⇒ 1 end) 0%Z = true) in Hbalance.
destruct (Z.compare_spec (U.to_z (A.t.balance account)) 0);
cbn in Hbalance; [assumption|discriminate|discriminate].
Qed.
Lemma balance_after_account_write keep world addr value query :
WellFormed (W.t.accounts world) →
U.to_z (A.t.balance (readAccount (writeAccount keep world addr value) query)) =
if String.eqb addr query then U.to_z (A.t.balance value)
else U.to_z (A.t.balance (readAccount world query)).
Proof.
intro Hwf. cbn beta. rewrite (account_read_after_write keep world addr value query Hwf).
destruct (String.eqb addr query); [|reflexivity].
destruct keep; [reflexivity|].
destruct (@A.is_empty value) eqn:Hempty; [|reflexivity].
change (0%Z = U.to_z (A.t.balance value)).
symmetry. apply account_empty_balance. exact Hempty.
Qed.
Lemma u256_add x y :
(0 ≤ U.to_z x + U.to_z y < 2^256)%Z →
U.to_z (@U.op_plus x y) = (U.to_z x + U.to_z y)%Z.
Proof.
intro Hrange.
change (Z.land (Z.shiftl (x+y) 0) (Z.ones 256) = (x+y)%Z).
rewrite Z.shiftl_0_r, Z.land_ones by lia.
apply Z.mod_small. exact Hrange.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.State QedkConcrete.stringmaps QedkConcrete.reservecheck.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module W := MonadVM.State.WorldState.
Module A := MonadVM.Chain.Ethereum.Account.
Module Addr := MonadVM.Chain.Ethereum.Address.
Module U := MonadVM.Numeric.U256.
Section Accounts.
Context {U64 : Basics.Unreachable Numeric.U64.t}.
Context {U256 : Basics.Unreachable U.t}.
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation writeAccount := (fun keep world addr value ⇒
State.op_pointcaretlparenrparenltminus world (@W.account (Some keep) addr) value).
Lemma account_write_wellformed keep world addr value :
WellFormed (W.t.accounts world) →
WellFormed (W.t.accounts (writeAccount keep world addr value)).
Proof.
intro Hwf. destruct keep; apply update_wellformed; exact Hwf.
Qed.
Lemma account_read_after_write keep world addr value query :
WellFormed (W.t.accounts world) →
readAccount (writeAccount keep world addr value) query =
if String.eqb addr query then
if keep then value else
if @A.is_empty value then @A.empty else value
else readAccount world query.
Proof.
intro Hwf. destruct keep.
- match goal with |- _ = ?rhs ⇒
change ((match OCamlMap.Make.find_opt stringOrder query
(OCamlMap.Make.update stringOrder addr (fun _ ⇒ Some value) (W.t.accounts world)) with
| Some a ⇒ a | None ⇒ @A.empty end) = rhs) end.
rewrite find_update by exact Hwf. destruct (String.eqb addr query); reflexivity.
- match goal with |- _ = ?rhs ⇒
change ((match OCamlMap.Make.find_opt stringOrder query
(OCamlMap.Make.update stringOrder addr
(fun _ ⇒ if @A.is_empty value then None else Some value)
(W.t.accounts world)) with
| Some a ⇒ a | None ⇒ @A.empty end) = rhs) end.
rewrite find_update by exact Hwf. destruct (String.eqb addr query); [|reflexivity].
destruct (@A.is_empty value); reflexivity.
Qed.
Lemma account_empty_balance account :
@A.is_empty account = true → U.to_z (A.t.balance account) = 0%Z.
Proof.
unfold A.is_empty. intro Hempty.
apply Bool.andb_true_iff in Hempty as [Hbalance _].
change (Z.eqb (match Z.compare (U.to_z (A.t.balance account)) 0 with
| Eq ⇒ 0 | Lt ⇒ -1 | Gt ⇒ 1 end) 0%Z = true) in Hbalance.
destruct (Z.compare_spec (U.to_z (A.t.balance account)) 0);
cbn in Hbalance; [assumption|discriminate|discriminate].
Qed.
Lemma balance_after_account_write keep world addr value query :
WellFormed (W.t.accounts world) →
U.to_z (A.t.balance (readAccount (writeAccount keep world addr value) query)) =
if String.eqb addr query then U.to_z (A.t.balance value)
else U.to_z (A.t.balance (readAccount world query)).
Proof.
intro Hwf. cbn beta. rewrite (account_read_after_write keep world addr value query Hwf).
destruct (String.eqb addr query); [|reflexivity].
destruct keep; [reflexivity|].
destruct (@A.is_empty value) eqn:Hempty; [|reflexivity].
change (0%Z = U.to_z (A.t.balance value)).
symmetry. apply account_empty_balance. exact Hempty.
Qed.
Lemma u256_add x y :
(0 ≤ U.to_z x + U.to_z y < 2^256)%Z →
U.to_z (@U.op_plus x y) = (U.to_z x + U.to_z y)%Z.
Proof.
intro Hrange.
change (Z.land (Z.shiftl (x+y) 0) (Z.ones 256) = (x+y)%Z).
rewrite Z.shiftl_0_r, Z.land_ones by lia.
apply Z.mod_small. exact Hrange.
Qed.
This is the balance update used for beneficiary credits. The no-overflow
condition is explicit: U256 addition is not monotone on unrestricted inputs.
Lemma credit_balance world beneficiary amount query :
WellFormed (W.t.accounts world) →
(0 ≤ U.to_z (A.t.balance (readAccount world beneficiary)) + U.to_z amount < 2^256)%Z →
let credited := State.op_pointcaretdollarlparenrparenltminus world
(Lens.Infix.op_pipeminusminus (@W.account None beneficiary) A.balance)
(fun balance ⇒ @U.op_plus balance amount) in
U.to_z (A.t.balance (readAccount credited query)) =
(U.to_z (A.t.balance (readAccount world query)) +
if String.eqb beneficiary query then U.to_z amount else 0)%Z.
Proof.
intros Hwf Hrange. cbn zeta.
match goal with |- _ = ?rhs ⇒
change (U.to_z (A.t.balance (readAccount
(writeAccount false world beneficiary (A.t.with_balance
(@U.op_plus (A.t.balance (readAccount world beneficiary)) amount)
(readAccount world beneficiary))) query)) = rhs) end.
cbn beta. rewrite balance_after_account_write by exact Hwf.
destruct (String.eqb beneficiary query) eqn:Heq.
- apply String.eqb_eq in Heq. subst query. cbn beta iota zeta delta [A.t.with_balance A.t.balance].
apply u256_add. exact Hrange.
- lia.
Qed.
Lemma credit_nondecreasing world beneficiary amount query :
WellFormed (W.t.accounts world) →
(0 ≤ U.to_z amount)%Z →
(0 ≤ U.to_z (A.t.balance (readAccount world beneficiary)) + U.to_z amount < 2^256)%Z →
let credited := State.op_pointcaretdollarlparenrparenltminus world
(Lens.Infix.op_pipeminusminus (@W.account None beneficiary) A.balance)
(fun balance ⇒ @U.op_plus balance amount) in
(U.to_z (A.t.balance (readAccount world query)) ≤
U.to_z (A.t.balance (readAccount credited query)))%Z.
Proof.
intros Hwf Hnonnegative Hrange. cbn zeta beta.
rewrite credit_balance by assumption.
destruct (String.eqb beneficiary query); lia.
Qed.
WellFormed (W.t.accounts world) →
(0 ≤ U.to_z (A.t.balance (readAccount world beneficiary)) + U.to_z amount < 2^256)%Z →
let credited := State.op_pointcaretdollarlparenrparenltminus world
(Lens.Infix.op_pipeminusminus (@W.account None beneficiary) A.balance)
(fun balance ⇒ @U.op_plus balance amount) in
U.to_z (A.t.balance (readAccount credited query)) =
(U.to_z (A.t.balance (readAccount world query)) +
if String.eqb beneficiary query then U.to_z amount else 0)%Z.
Proof.
intros Hwf Hrange. cbn zeta.
match goal with |- _ = ?rhs ⇒
change (U.to_z (A.t.balance (readAccount
(writeAccount false world beneficiary (A.t.with_balance
(@U.op_plus (A.t.balance (readAccount world beneficiary)) amount)
(readAccount world beneficiary))) query)) = rhs) end.
cbn beta. rewrite balance_after_account_write by exact Hwf.
destruct (String.eqb beneficiary query) eqn:Heq.
- apply String.eqb_eq in Heq. subst query. cbn beta iota zeta delta [A.t.with_balance A.t.balance].
apply u256_add. exact Hrange.
- lia.
Qed.
Lemma credit_nondecreasing world beneficiary amount query :
WellFormed (W.t.accounts world) →
(0 ≤ U.to_z amount)%Z →
(0 ≤ U.to_z (A.t.balance (readAccount world beneficiary)) + U.to_z amount < 2^256)%Z →
let credited := State.op_pointcaretdollarlparenrparenltminus world
(Lens.Infix.op_pipeminusminus (@W.account None beneficiary) A.balance)
(fun balance ⇒ @U.op_plus balance amount) in
(U.to_z (A.t.balance (readAccount world query)) ≤
U.to_z (A.t.balance (readAccount credited query)))%Z.
Proof.
intros Hwf Hnonnegative Hrange. cbn zeta beta.
rewrite credit_balance by assumption.
destruct (String.eqb beneficiary query); lia.
Qed.
Individual U256 bounds do not suffice for credit monotonicity. This
boundary case is not a claim about a state reachable on the live network;
excluding it requires a supply/reachability invariant.
Lemma credit_wraps_at_u256_limit world beneficiary :
WellFormed (W.t.accounts world) →
U.to_z (A.t.balance (readAccount world beneficiary)) = (2^256 - 1)%Z →
let credited := State.op_pointcaretdollarlparenrparenltminus world
(Lens.Infix.op_pipeminusminus (@W.account None beneficiary) A.balance)
(fun balance ⇒ @U.op_plus balance (@U.one)) in
U.to_z (A.t.balance (readAccount credited beneficiary)) = 0%Z.
Proof.
intros Hwf Hbalance. cbn zeta.
change (U.to_z (A.t.balance (readAccount
(writeAccount false world beneficiary (A.t.with_balance
(@U.op_plus (A.t.balance (readAccount world beneficiary)) (@U.one))
(readAccount world beneficiary))) beneficiary)) = 0%Z).
cbn beta. rewrite balance_after_account_write by exact Hwf.
rewrite String.eqb_refl.
change (Z.land (Z.shiftl
(U.to_z (A.t.balance (readAccount world beneficiary)) + 1) 0) (Z.ones 256) = 0)%Z.
cbn beta. rewrite Hbalance. vm_compute. reflexivity.
Qed.
WellFormed (W.t.accounts world) →
U.to_z (A.t.balance (readAccount world beneficiary)) = (2^256 - 1)%Z →
let credited := State.op_pointcaretdollarlparenrparenltminus world
(Lens.Infix.op_pipeminusminus (@W.account None beneficiary) A.balance)
(fun balance ⇒ @U.op_plus balance (@U.one)) in
U.to_z (A.t.balance (readAccount credited beneficiary)) = 0%Z.
Proof.
intros Hwf Hbalance. cbn zeta.
change (U.to_z (A.t.balance (readAccount
(writeAccount false world beneficiary (A.t.with_balance
(@U.op_plus (A.t.balance (readAccount world beneficiary)) (@U.one))
(readAccount world beneficiary))) beneficiary)) = 0%Z).
cbn beta. rewrite balance_after_account_write by exact Hwf.
rewrite String.eqb_refl.
change (Z.land (Z.shiftl
(U.to_z (A.t.balance (readAccount world beneficiary)) + 1) 0) (Z.ones 256) = 0)%Z.
cbn beta. rewrite Hbalance. vm_compute. reflexivity.
Qed.
The upfront fee debit also increments the sender nonce. Even if this
changes whether EIP-161 keeps the account, the balance equation is exact.
Lemma upfront_fee_balance_with_nonce (nextNonce : A.t → Numeric.U64.t) world sender fee query :
WellFormed (W.t.accounts world) →
(0 ≤ U.to_z (A.t.balance (readAccount world sender)) - U.to_z fee < 2^256)%Z →
let charged := State.op_pointcaretdollarlparenrparenltminus world
(@W.account None sender)
(fun account ⇒ A.t.with_balance (@U.op_minus (A.t.balance account) fee)
(A.t.with_nonce (nextNonce account) account)) in
U.to_z (A.t.balance (readAccount charged query)) =
if String.eqb sender query then
(U.to_z (A.t.balance (readAccount world query)) - U.to_z fee)%Z
else U.to_z (A.t.balance (readAccount world query)).
Proof.
intros Hwf Hrange. cbn zeta.
match goal with |- _ = ?rhs ⇒
change (U.to_z (A.t.balance (readAccount
(writeAccount false world sender (A.t.with_balance
(@U.op_minus (A.t.balance (readAccount world sender)) fee)
(A.t.with_nonce (nextNonce (readAccount world sender))
(readAccount world sender)))) query)) = rhs) end.
cbn beta. rewrite balance_after_account_write by exact Hwf.
destruct (String.eqb sender query) eqn:Heq; [|reflexivity].
apply String.eqb_eq in Heq. subst query.
cbn beta iota zeta delta [A.t.with_balance A.t.with_nonce A.t.balance].
apply reservecheck.u256_sub. exact Hrange.
Qed.
Lemma upfront_fee_balance world sender fee query :
WellFormed (W.t.accounts world) →
(0 ≤ U.to_z (A.t.balance (readAccount world sender)) - U.to_z fee < 2^256)%Z →
let charged := State.op_pointcaretdollarlparenrparenltminus world
(@W.account None sender)
(fun account ⇒ A.t.with_balance (@U.op_minus (A.t.balance account) fee)
(A.t.with_nonce (@Numeric.U64.op_plus (A.t.nonce account)
(@Numeric.U64.one)) account)) in
U.to_z (A.t.balance (readAccount charged query)) =
if String.eqb sender query then
(U.to_z (A.t.balance (readAccount world query)) - U.to_z fee)%Z
else U.to_z (A.t.balance (readAccount world query)).
Proof. apply (upfront_fee_balance_with_nonce (fun account ⇒
Numeric.U64.op_plus (A.t.nonce account) Numeric.U64.one)). Qed.
End Accounts.
Print Assumptions credit_nondecreasing.
Print Assumptions upfront_fee_balance.
Print Assumptions credit_wraps_at_u256_limit.
WellFormed (W.t.accounts world) →
(0 ≤ U.to_z (A.t.balance (readAccount world sender)) - U.to_z fee < 2^256)%Z →
let charged := State.op_pointcaretdollarlparenrparenltminus world
(@W.account None sender)
(fun account ⇒ A.t.with_balance (@U.op_minus (A.t.balance account) fee)
(A.t.with_nonce (nextNonce account) account)) in
U.to_z (A.t.balance (readAccount charged query)) =
if String.eqb sender query then
(U.to_z (A.t.balance (readAccount world query)) - U.to_z fee)%Z
else U.to_z (A.t.balance (readAccount world query)).
Proof.
intros Hwf Hrange. cbn zeta.
match goal with |- _ = ?rhs ⇒
change (U.to_z (A.t.balance (readAccount
(writeAccount false world sender (A.t.with_balance
(@U.op_minus (A.t.balance (readAccount world sender)) fee)
(A.t.with_nonce (nextNonce (readAccount world sender))
(readAccount world sender)))) query)) = rhs) end.
cbn beta. rewrite balance_after_account_write by exact Hwf.
destruct (String.eqb sender query) eqn:Heq; [|reflexivity].
apply String.eqb_eq in Heq. subst query.
cbn beta iota zeta delta [A.t.with_balance A.t.with_nonce A.t.balance].
apply reservecheck.u256_sub. exact Hrange.
Qed.
Lemma upfront_fee_balance world sender fee query :
WellFormed (W.t.accounts world) →
(0 ≤ U.to_z (A.t.balance (readAccount world sender)) - U.to_z fee < 2^256)%Z →
let charged := State.op_pointcaretdollarlparenrparenltminus world
(@W.account None sender)
(fun account ⇒ A.t.with_balance (@U.op_minus (A.t.balance account) fee)
(A.t.with_nonce (@Numeric.U64.op_plus (A.t.nonce account)
(@Numeric.U64.one)) account)) in
U.to_z (A.t.balance (readAccount charged query)) =
if String.eqb sender query then
(U.to_z (A.t.balance (readAccount world query)) - U.to_z fee)%Z
else U.to_z (A.t.balance (readAccount world query)).
Proof. apply (upfront_fee_balance_with_nonce (fun account ⇒
Numeric.U64.op_plus (A.t.nonce account) Numeric.U64.one)). Qed.
End Accounts.
Print Assumptions credit_nondecreasing.
Print Assumptions upfront_fee_balance.
Print Assumptions credit_wraps_at_u256_limit.