Library QedkConcrete.precompilesframe
Context frame of the concrete precompile dispatcher
From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostsupply QedkConcrete.supply.
Require Import MonadVM.Precompiles QedkConcrete.hostcomposition QedkConcrete.reserveprecompileproperties.
Require Import QedkConcrete.hostframe QedkConcrete.stringmaps.
Require Import QedkConcrete.hostaccountformat QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
Module P := Precompiles.Make.
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}.
Local Abbreviation lookup := (@P.try_precompile F).
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostsupply QedkConcrete.supply.
Require Import MonadVM.Precompiles QedkConcrete.hostcomposition QedkConcrete.reserveprecompileproperties.
Require Import QedkConcrete.hostframe QedkConcrete.stringmaps.
Require Import QedkConcrete.hostaccountformat QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
Module P := Precompiles.Make.
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}.
Local Abbreviation lookup := (@P.try_precompile F).
The stateful native table contains only the reserve precompile. This
equation is checked from each supported revision's implementation.
Definition reservePrecompileTable : Prop :=
let revision := {| Reserve_balance_introspection.Make_Revision_signature.revision :=
(@P.Revision F).(Precompiles.Make_Revision_signature.revision) |} in
@P.monad_precompiles F = Chain.Ethereum.Address.Map.of_list
((@Reserve_balance_introspection.Make.address
(Reserve_balance_introspection.Make.Build_FArgs revision),
@Reserve_balance_introspection.Make.precompile
(Reserve_balance_introspection.Make.Build_FArgs revision)) :: nil).
Lemma eleven_reserve_precompile_table :
(@P.Revision F).(Precompiles.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
reservePrecompileTable.
Proof. intro Hrevision. unfold reservePrecompileTable, P.monad_precompiles.
rewrite Hrevision. reflexivity. Qed.
Lemma try_precompile_preserves_context_table msg addr action :
reservePrecompileTable →
lookup msg addr = Some action → preservesContext action.
Proof.
intros Hrevision 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;
apply context_return.
- unfold reservePrecompileTable in Hrevision. 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. apply context_return.
+ inversion Hselected; subst action. apply precompile_preserves_context.
Qed.
Lemma try_precompile_preserves_context msg addr action :
(@P.Revision F).(Precompiles.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
lookup msg addr = Some action → preservesContext action.
Proof. intros Hrevision Hselected. eapply try_precompile_preserves_context_table;
[apply eleven_reserve_precompile_table; exact Hrevision | exact Hselected]. Qed.
Lemma try_precompile_preserves_supply_table msg addr action :
reservePrecompileTable →
lookup msg addr = Some action → preservesSupply action.
Proof.
intros Hrevision 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;
apply supply_return.
- unfold reservePrecompileTable in Hrevision. 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. apply supply_return.
+ inversion Hselected; subst action. intros s Hinv.
match goal with |- balanceInvariant ?world ∧ _ ⇒
assert (Hsafe : balanceInvariant world ∧
totalBalance world = totalBalance (S.t.world_state s)) by
(apply precompile_preserves_supply; exact Hinv) end.
destruct Hsafe as [Hafter Htotal].
split; [exact Hafter|apply Z.eq_le_incl; exact Htotal].
Qed.
Lemma try_precompile_preserves_supply msg addr action :
(@P.Revision F).(Precompiles.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
lookup msg addr = Some action → preservesSupply action.
Proof. intros Hrevision Hselected. eapply try_precompile_preserves_supply_table;
[apply eleven_reserve_precompile_table; exact Hrevision | exact Hselected]. Qed.
Lemma try_precompile_preserves_format_table msg addr action :
reservePrecompileTable →
lookup msg addr = Some action → preservesCallback hostFormat action.
Proof.
intros Hrevision 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;
apply format_return.
- unfold reservePrecompileTable in Hrevision. 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. apply format_return.
+ inversion Hselected; subst action. exact (@precompile_preserves_format _
(Opaque.legacy_witness tt) (Opaque.legacy_witness (Ascii.ascii_of_nat 0))
(Opaque.legacy_witness (0%Z,0%Z)) (Opaque.legacy_witness String.EmptyString)
(Opaque.legacy_witness 0%Z) msg).
Qed.
Lemma try_precompile_preserves_format msg addr action :
(@P.Revision F).(Precompiles.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
lookup msg addr = Some action → preservesCallback hostFormat action.
Proof. intros Hrevision Hselected. eapply try_precompile_preserves_format_table;
[apply eleven_reserve_precompile_table; exact Hrevision | exact Hselected]. Qed.
End Dispatch.
let revision := {| Reserve_balance_introspection.Make_Revision_signature.revision :=
(@P.Revision F).(Precompiles.Make_Revision_signature.revision) |} in
@P.monad_precompiles F = Chain.Ethereum.Address.Map.of_list
((@Reserve_balance_introspection.Make.address
(Reserve_balance_introspection.Make.Build_FArgs revision),
@Reserve_balance_introspection.Make.precompile
(Reserve_balance_introspection.Make.Build_FArgs revision)) :: nil).
Lemma eleven_reserve_precompile_table :
(@P.Revision F).(Precompiles.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
reservePrecompileTable.
Proof. intro Hrevision. unfold reservePrecompileTable, P.monad_precompiles.
rewrite Hrevision. reflexivity. Qed.
Lemma try_precompile_preserves_context_table msg addr action :
reservePrecompileTable →
lookup msg addr = Some action → preservesContext action.
Proof.
intros Hrevision 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;
apply context_return.
- unfold reservePrecompileTable in Hrevision. 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. apply context_return.
+ inversion Hselected; subst action. apply precompile_preserves_context.
Qed.
Lemma try_precompile_preserves_context msg addr action :
(@P.Revision F).(Precompiles.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
lookup msg addr = Some action → preservesContext action.
Proof. intros Hrevision Hselected. eapply try_precompile_preserves_context_table;
[apply eleven_reserve_precompile_table; exact Hrevision | exact Hselected]. Qed.
Lemma try_precompile_preserves_supply_table msg addr action :
reservePrecompileTable →
lookup msg addr = Some action → preservesSupply action.
Proof.
intros Hrevision 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;
apply supply_return.
- unfold reservePrecompileTable in Hrevision. 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. apply supply_return.
+ inversion Hselected; subst action. intros s Hinv.
match goal with |- balanceInvariant ?world ∧ _ ⇒
assert (Hsafe : balanceInvariant world ∧
totalBalance world = totalBalance (S.t.world_state s)) by
(apply precompile_preserves_supply; exact Hinv) end.
destruct Hsafe as [Hafter Htotal].
split; [exact Hafter|apply Z.eq_le_incl; exact Htotal].
Qed.
Lemma try_precompile_preserves_supply msg addr action :
(@P.Revision F).(Precompiles.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
lookup msg addr = Some action → preservesSupply action.
Proof. intros Hrevision Hselected. eapply try_precompile_preserves_supply_table;
[apply eleven_reserve_precompile_table; exact Hrevision | exact Hselected]. Qed.
Lemma try_precompile_preserves_format_table msg addr action :
reservePrecompileTable →
lookup msg addr = Some action → preservesCallback hostFormat action.
Proof.
intros Hrevision 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;
apply format_return.
- unfold reservePrecompileTable in Hrevision. 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. apply format_return.
+ inversion Hselected; subst action. exact (@precompile_preserves_format _
(Opaque.legacy_witness tt) (Opaque.legacy_witness (Ascii.ascii_of_nat 0))
(Opaque.legacy_witness (0%Z,0%Z)) (Opaque.legacy_witness String.EmptyString)
(Opaque.legacy_witness 0%Z) msg).
Qed.
Lemma try_precompile_preserves_format msg addr action :
(@P.Revision F).(Precompiles.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
lookup msg addr = Some action → preservesCallback hostFormat action.
Proof. intros Hrevision Hselected. eapply try_precompile_preserves_format_table;
[apply eleven_reserve_precompile_table; exact Hrevision | exact Hselected]. Qed.
End Dispatch.