Library QedkConcrete.reserveprecompile
Concrete reserve-precompile effects
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles.Reserve_balance_introspection.
Require Import QedkConcrete.reservewrites QedkConcrete.hostframe.
Require Import QedkConcrete.worldaccounts QedkConcrete.supply.
Require Import QedkConcrete.hostaccountformat QedkConcrete.vmhostframe.
Require Import QedkConcrete.bytewords.
Set Default Goal Selector "!".
Module R := Reserve_balance_introspection.Make.
Section Precompile.
Context {F : R.FArgs}.
Context {UZ : Basics.Unreachable Z.t}.
Context {Uarr : Basics.Unreachable (OCamlIarray.t int)}.
Context {UN : Basics.Unreachable Support.UintN.t}.
Context {U64 : Basics.Unreachable Numeric.U64.t}.
Context {Urepr : Basics.Unreachable U.Repr.t}.
Context {U256 : Basics.Unreachable U.t}.
Context {Uaddr : Basics.Unreachable Addr.t}.
Context {Ubytes : Basics.Unreachable Byte_string.Bytes.t}.
Context {Uunit : Basics.Unreachable unit}.
Context {Ustr : Basics.Unreachable string}.
Context {Uint : Basics.Unreachable int}.
Context {Ubyte : Basics.Unreachable ascii}.
Context {Upair : Basics.Unreachable (int × int)}.
Context {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Context {Useq : Basics.Unreachable (OCamlSeq.t (Addr.Map.key × A.t))}.
Context {Uint64 : Basics.Unreachable MonadVM.Int64.t}.
Context {Ugas : Basics.Unreachable Gas.t}.
Local Abbreviation precompile := (@R.precompile F).
Local Abbreviation introspection := (@R.introspection F).
Local Abbreviation setter := (@Reserve_balance.set_reserve_balance).
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.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles.Reserve_balance_introspection.
Require Import QedkConcrete.reservewrites QedkConcrete.hostframe.
Require Import QedkConcrete.worldaccounts QedkConcrete.supply.
Require Import QedkConcrete.hostaccountformat QedkConcrete.vmhostframe.
Require Import QedkConcrete.bytewords.
Set Default Goal Selector "!".
Module R := Reserve_balance_introspection.Make.
Section Precompile.
Context {F : R.FArgs}.
Context {UZ : Basics.Unreachable Z.t}.
Context {Uarr : Basics.Unreachable (OCamlIarray.t int)}.
Context {UN : Basics.Unreachable Support.UintN.t}.
Context {U64 : Basics.Unreachable Numeric.U64.t}.
Context {Urepr : Basics.Unreachable U.Repr.t}.
Context {U256 : Basics.Unreachable U.t}.
Context {Uaddr : Basics.Unreachable Addr.t}.
Context {Ubytes : Basics.Unreachable Byte_string.Bytes.t}.
Context {Uunit : Basics.Unreachable unit}.
Context {Ustr : Basics.Unreachable string}.
Context {Uint : Basics.Unreachable int}.
Context {Ubyte : Basics.Unreachable ascii}.
Context {Upair : Basics.Unreachable (int × int)}.
Context {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Context {Useq : Basics.Unreachable (OCamlSeq.t (Addr.Map.key × A.t))}.
Context {Uint64 : Basics.Unreachable MonadVM.Int64.t}.
Context {Ugas : Basics.Unreachable Gas.t}.
Local Abbreviation precompile := (@R.precompile F).
Local Abbreviation introspection := (@R.introspection F).
Local Abbreviation setter := (@Reserve_balance.set_reserve_balance).
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.
Older revisions retain exactly the introspection entrypoint: even existing
configuration storage cannot make a setter reachable through this native.
Lemma precompile_before_qedk msg :
(@R.Revision F).(Reserve_balance_introspection.Make_Revision_signature.revision) ≠
Support.Variants.Revision.Eleven →
precompile msg = introspection msg.
Proof.
intro Hrevision. unfold R.precompile.
destruct ((@R.Revision F).(Reserve_balance_introspection.Make_Revision_signature.revision));
try reflexivity; exfalso; apply Hrevision; 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 precompile_preserves_creation_set msg s :
S.t.accounts_created_in_current_transaction (snd (precompile msg s)) =
S.t.accounts_created_in_current_transaction s.
Proof.
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]];
[rewrite Hsame|rewrite Hafter]; reflexivity.
Qed.
Lemma precompile_preserves_context msg : preservesContext (precompile msg).
Proof.
intro s. change (transactionContext (snd (precompile msg s)) = transactionContext s).
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. reflexivity.
- rewrite Hafter.
destruct (@setter_preserves_history _ _ _ _ _ Hwrite) as [Hhistory Hcounters].
unfold transactionContext. cbn [S.t.with_world_state S.t.initial_world_state
S.t.current_block S.t.tx_origin S.t.tx_gas_limit S.t.tx_gas_price S.t.world_state].
rewrite Hhistory, Hcounters. reflexivity.
Qed.
Lemma precompile_preserves_supply msg s :
balanceInvariant (S.t.world_state s) →
balanceInvariant (S.t.world_state (snd (precompile msg s))) ∧
totalBalance (S.t.world_state (snd (precompile msg s))) = totalBalance (S.t.world_state s).
Proof.
intro Hinvariant.
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. split; [exact Hinvariant|reflexivity].
- rewrite Hafter. cbn [S.t.with_world_state S.t.world_state].
exact (@setter_preserves_supply _ _ _ _ _ Hinvariant Hwrite).
Qed.
Lemma precompile_preserves_balances msg s query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
U.to_z (A.t.balance (State.op_pointcaretlparenrparen (S.t.world_state (snd (precompile msg s)))
(@W.account None query))) =
U.to_z (A.t.balance (State.op_pointcaretlparenrparen (S.t.world_state s)
(@W.account None query))).
Proof.
intro Hwf.
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. reflexivity.
- rewrite Hafter. cbn [S.t.with_world_state S.t.world_state].
exact (@setter_preserves_balances _ _ _ _ _ query Hwf Hwrite).
Qed.
Lemma precompile_preserves_format msg : preservesCallback hostFormat (precompile msg).
Proof.
intros s Hformat.
change (hostFormat (snd (precompile msg s))).
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. exact Hformat.
- rewrite Hafter. destruct Hformat as [Hworld Htransient]. split; [|exact Htransient].
eapply setter_preserves_format; [exact Hworld|exact Hwrite].
Qed.
(@R.Revision F).(Reserve_balance_introspection.Make_Revision_signature.revision) ≠
Support.Variants.Revision.Eleven →
precompile msg = introspection msg.
Proof.
intro Hrevision. unfold R.precompile.
destruct ((@R.Revision F).(Reserve_balance_introspection.Make_Revision_signature.revision));
try reflexivity; exfalso; apply Hrevision; 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 precompile_preserves_creation_set msg s :
S.t.accounts_created_in_current_transaction (snd (precompile msg s)) =
S.t.accounts_created_in_current_transaction s.
Proof.
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]];
[rewrite Hsame|rewrite Hafter]; reflexivity.
Qed.
Lemma precompile_preserves_context msg : preservesContext (precompile msg).
Proof.
intro s. change (transactionContext (snd (precompile msg s)) = transactionContext s).
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. reflexivity.
- rewrite Hafter.
destruct (@setter_preserves_history _ _ _ _ _ Hwrite) as [Hhistory Hcounters].
unfold transactionContext. cbn [S.t.with_world_state S.t.initial_world_state
S.t.current_block S.t.tx_origin S.t.tx_gas_limit S.t.tx_gas_price S.t.world_state].
rewrite Hhistory, Hcounters. reflexivity.
Qed.
Lemma precompile_preserves_supply msg s :
balanceInvariant (S.t.world_state s) →
balanceInvariant (S.t.world_state (snd (precompile msg s))) ∧
totalBalance (S.t.world_state (snd (precompile msg s))) = totalBalance (S.t.world_state s).
Proof.
intro Hinvariant.
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. split; [exact Hinvariant|reflexivity].
- rewrite Hafter. cbn [S.t.with_world_state S.t.world_state].
exact (@setter_preserves_supply _ _ _ _ _ Hinvariant Hwrite).
Qed.
Lemma precompile_preserves_balances msg s query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
U.to_z (A.t.balance (State.op_pointcaretlparenrparen (S.t.world_state (snd (precompile msg s)))
(@W.account None query))) =
U.to_z (A.t.balance (State.op_pointcaretlparenrparen (S.t.world_state s)
(@W.account None query))).
Proof.
intro Hwf.
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. reflexivity.
- rewrite Hafter. cbn [S.t.with_world_state S.t.world_state].
exact (@setter_preserves_balances _ _ _ _ _ query Hwf Hwrite).
Qed.
Lemma precompile_preserves_format msg : preservesCallback hostFormat (precompile msg).
Proof.
intros s Hformat.
change (hostFormat (snd (precompile msg s))).
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. exact Hformat.
- rewrite Hafter. destruct Hformat as [Hworld Htransient]. split; [|exact Htransient].
eapply setter_preserves_format; [exact Hworld|exact Hwrite].
Qed.
Even malformed inputs return a bounded error string. The three-word getter
is the largest response; no configuration or input-length premise is needed.
Lemma introspection_output_bounded msg s :
(OCamlString.length (Evmc.Result.t.output_data (fst (introspection msg s))) ≤ 32)%Z.
Proof.
unfold R.introspection.
repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: cbn -[U.to_repr_bytes Reserve_balance.dipped_into_reserve].
all: try solve [lia].
match goal with |- context [Reserve_balance.dipped_into_reserve ?revision ?state] ⇒
destruct (Reserve_balance.dipped_into_reserve revision state)
end.
cbn -[U.to_repr_bytes]. rewrite word_bytes_encoding_length. lia.
Qed.
Lemma precompile_output_bounded msg s :
(OCamlString.length (Evmc.Result.t.output_data (fst (precompile msg s))) ≤ 96)%Z.
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 (pose proof (introspection_output_bounded msg s); lia).
all: repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: cbn -[Reserve_balance.set_reserve_balance U.to_repr_bytes
U.of_bytes_be_exn Byte_string.Bytes.sub U.of_uint_exn
Reserve_config.read Reserve_balance.stored_config].
all: try (repeat rewrite byte_append_length; repeat rewrite word_bytes_encoding_length; lia).
match goal with |- context [match ?writeResult with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct writeResult as [world|] end.
all: cbn -[U.to_repr_bytes U.of_uint_exn]; try rewrite word_bytes_encoding_length; lia.
Qed.
End Precompile.
Print Assumptions precompile_preserves_context.
Print Assumptions precompile_preserves_supply.
Print Assumptions precompile_preserves_balances.
Print Assumptions precompile_preserves_format.
Print Assumptions precompile_output_bounded.
(OCamlString.length (Evmc.Result.t.output_data (fst (introspection msg s))) ≤ 32)%Z.
Proof.
unfold R.introspection.
repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: cbn -[U.to_repr_bytes Reserve_balance.dipped_into_reserve].
all: try solve [lia].
match goal with |- context [Reserve_balance.dipped_into_reserve ?revision ?state] ⇒
destruct (Reserve_balance.dipped_into_reserve revision state)
end.
cbn -[U.to_repr_bytes]. rewrite word_bytes_encoding_length. lia.
Qed.
Lemma precompile_output_bounded msg s :
(OCamlString.length (Evmc.Result.t.output_data (fst (precompile msg s))) ≤ 96)%Z.
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 (pose proof (introspection_output_bounded msg s); lia).
all: repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: cbn -[Reserve_balance.set_reserve_balance U.to_repr_bytes
U.of_bytes_be_exn Byte_string.Bytes.sub U.of_uint_exn
Reserve_config.read Reserve_balance.stored_config].
all: try (repeat rewrite byte_append_length; repeat rewrite word_bytes_encoding_length; lia).
match goal with |- context [match ?writeResult with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct writeResult as [world|] end.
all: cbn -[U.to_repr_bytes U.of_uint_exn]; try rewrite word_bytes_encoding_length; lia.
Qed.
End Precompile.
Print Assumptions precompile_preserves_context.
Print Assumptions precompile_preserves_supply.
Print Assumptions precompile_preserves_balances.
Print Assumptions precompile_preserves_format.
Print Assumptions precompile_output_bounded.