Library SetcodeConcrete.checkednonce
Nonce bounds at the checked Host interface
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostnonce.
Require Import SetcodeConcrete.noncecreate SetcodeConcrete.noncebudget SetcodeConcrete.nonceeffects.
Require Import MonadExecutionSpecs.Termination.VmHostGasBound.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Definition checkedRefund gas result :=
if invalid_host_gas (Evmc.Result.t.gas_left result) gas
then 0 else Evmc.Result.t.gas_left result.
Definition checkedNonceResult query gas allowance before result after :=
nonceState query after ∧
nonceAt (S.t.world_state after) query + checkedRefund gas result ≤
nonceAt (S.t.world_state before) query + gas + allowance.
Lemma valid_returned_gas returned gas :
0 ≤ gas → invalid_host_gas returned gas = false →
0 ≤ returned ≤ gas.
Proof.
intros Hgas Hcheck. pose proof (checked_host_gas_bound returned gas Hcheck) as Hbound.
unfold OCamlZ.of_int64_unsigned in Hbound.
destruct (Z.ltb gas 0) eqn:Hnegative;
[apply Z.ltb_lt in Hnegative; lia|exact Hbound].
Qed.
Lemma checked_refund_bound gas result :
0 ≤ gas → 0 ≤ checkedRefund gas result ≤ gas.
Proof.
intro Hgas. unfold checkedRefund.
destruct (invalid_host_gas (Evmc.Result.t.gas_left result) gas) eqn:Hcheck;
[lia|exact (valid_returned_gas _ _ Hgas Hcheck)].
Qed.
Lemma nonce_result_checked query gas allowance before result after :
nonceResult query gas allowance before result after →
checkedNonceResult query gas allowance before result after.
Proof.
intros [Hstate [Hgas Hbound]]. split; [exact Hstate|].
unfold checkedRefund. destruct (invalid_host_gas (Evmc.Result.t.gas_left result) gas); lia.
Qed.
Lemma unchanged_checked_nonce query gas allowance before result after :
0 ≤ gas → 0 ≤ allowance → nonceState query after →
nonceAt (S.t.world_state after) query = nonceAt (S.t.world_state before) query →
checkedNonceResult query gas allowance before result after.
Proof.
intros Hgas Hallow Hstate Heq. split; [exact Hstate|].
rewrite Heq. pose proof (checked_refund_bound gas result Hgas). lia.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostnonce.
Require Import SetcodeConcrete.noncecreate SetcodeConcrete.noncebudget SetcodeConcrete.nonceeffects.
Require Import MonadExecutionSpecs.Termination.VmHostGasBound.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Definition checkedRefund gas result :=
if invalid_host_gas (Evmc.Result.t.gas_left result) gas
then 0 else Evmc.Result.t.gas_left result.
Definition checkedNonceResult query gas allowance before result after :=
nonceState query after ∧
nonceAt (S.t.world_state after) query + checkedRefund gas result ≤
nonceAt (S.t.world_state before) query + gas + allowance.
Lemma valid_returned_gas returned gas :
0 ≤ gas → invalid_host_gas returned gas = false →
0 ≤ returned ≤ gas.
Proof.
intros Hgas Hcheck. pose proof (checked_host_gas_bound returned gas Hcheck) as Hbound.
unfold OCamlZ.of_int64_unsigned in Hbound.
destruct (Z.ltb gas 0) eqn:Hnegative;
[apply Z.ltb_lt in Hnegative; lia|exact Hbound].
Qed.
Lemma checked_refund_bound gas result :
0 ≤ gas → 0 ≤ checkedRefund gas result ≤ gas.
Proof.
intro Hgas. unfold checkedRefund.
destruct (invalid_host_gas (Evmc.Result.t.gas_left result) gas) eqn:Hcheck;
[lia|exact (valid_returned_gas _ _ Hgas Hcheck)].
Qed.
Lemma nonce_result_checked query gas allowance before result after :
nonceResult query gas allowance before result after →
checkedNonceResult query gas allowance before result after.
Proof.
intros [Hstate [Hgas Hbound]]. split; [exact Hstate|].
unfold checkedRefund. destruct (invalid_host_gas (Evmc.Result.t.gas_left result) gas); lia.
Qed.
Lemma unchanged_checked_nonce query gas allowance before result after :
0 ≤ gas → 0 ≤ allowance → nonceState query after →
nonceAt (S.t.world_state after) query = nonceAt (S.t.world_state before) query →
checkedNonceResult query gas allowance before result after.
Proof.
intros Hgas Hallow Hstate Heq. split; [exact Hstate|].
rewrite Heq. pose proof (checked_refund_bound gas result Hgas). lia.
Qed.