Library QedkConcrete.vmbytewords
Words loaded from byte strings
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.wordrange QedkConcrete.bytewords
QedkConcrete.memorywords QedkConcrete.vmmachinewords.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Instructions.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (invariant : Host_t → Prop).
Context {UF : Basics.Unreachable U.t} {UG : Basics.Unreachable Gas.t}
{US : Basics.Unreachable string} {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}.
Lemma calldataload_preserves_words :
machineSafe invariant (fun _ ⇒ True) (@V.Executor.calldataload Host_t F E).
Proof.
unfold V.Executor.calldataload. eapply safe_bind; [apply safe_pop|]. intros index Hindex.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind.
- apply safe_push. destruct (@U.to_int_opt index).
+ apply padded_word.
+ apply word_zero.
- intros [] _. apply safe_increase_pc.
Qed.
Lemma push_preserves_words {UN : Basics.Unreachable Support.UintN.t}
{UB : Basics.Unreachable Byte_string.Bytes.t} {Uunit : Basics.Unreachable unit} count :
0 ≤ count ≤ 32 → machineSafe invariant (fun _ ⇒ True)
(@V.Executor.push_ Host_t F E count).
Proof.
intro Hcount. unfold V.Executor.push_. cbn beta iota zeta.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind.
- apply safe_fmap, (safe_read invariant wordRange).
intros state [_ [Hpc _]]. exact Hpc.
- intros here _.
eapply safe_bind.
+ apply safe_push. apply word_bytes.
rewrite padded_length by lia. lia.
+ intros [] _. apply safe_update_pc. intros pc Hpc. apply word_add.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.wordrange QedkConcrete.bytewords
QedkConcrete.memorywords QedkConcrete.vmmachinewords.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Instructions.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (invariant : Host_t → Prop).
Context {UF : Basics.Unreachable U.t} {UG : Basics.Unreachable Gas.t}
{US : Basics.Unreachable string} {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}.
Lemma calldataload_preserves_words :
machineSafe invariant (fun _ ⇒ True) (@V.Executor.calldataload Host_t F E).
Proof.
unfold V.Executor.calldataload. eapply safe_bind; [apply safe_pop|]. intros index Hindex.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind.
- apply safe_push. destruct (@U.to_int_opt index).
+ apply padded_word.
+ apply word_zero.
- intros [] _. apply safe_increase_pc.
Qed.
Lemma push_preserves_words {UN : Basics.Unreachable Support.UintN.t}
{UB : Basics.Unreachable Byte_string.Bytes.t} {Uunit : Basics.Unreachable unit} count :
0 ≤ count ≤ 32 → machineSafe invariant (fun _ ⇒ True)
(@V.Executor.push_ Host_t F E count).
Proof.
intro Hcount. unfold V.Executor.push_. cbn beta iota zeta.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind.
- apply safe_fmap, (safe_read invariant wordRange).
intros state [_ [Hpc _]]. exact Hpc.
- intros here _.
eapply safe_bind.
+ apply safe_push. apply word_bytes.
rewrite padded_length by lia. lia.
+ intros [] _. apply safe_update_pc. intros pc Hpc. apply word_add.
Qed.
Extending memory changes its accounting fields, not its stored chunks.
Both the successful extension and the out-of-memory exit preserve the
machine invariant. This does not require the integer-comparison contract.
Lemma extend_memory_preserves_words {UN : Basics.Unreachable Support.UintN.t}
{Ucost : Basics.Unreachable (Support.UintN.t → Support.UintN.t)}
{UE : Basics.Unreachable (U.t → U.t → @Mem.t Host_t F → option (@Mem.t Host_t F))}
start count :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
machineSafe invariant (fun _ ⇒ True)
(@V.Executor.extend_memory_to Host_t F E start count).
Proof.
intro Hrevision. unfold V.Executor.extend_memory_to.
destruct (U.op_eq count (@U.zero)); [apply safe_return; exact I|].
eapply safe_bind; [apply safe_memory_read|]. intros current Hcurrent.
eapply safe_bind.
- apply (safe_option invariant memoryWordChunks). intros extended Hextended.
eapply extend_word_chunks; [exact Hrevision|exact Hcurrent|exact Hextended].
- intros extended Hextended.
eapply safe_bind; [apply safe_memory_write; exact Hextended|]. intros [] _.
eapply safe_bind; [apply safe_fmap, safe_memory_read|]. intros newSize _.
destruct (Support.UintN.op_gteq _ _); apply safe_return; exact I.
Qed.
End Instructions.
Print Assumptions calldataload_preserves_words.
Print Assumptions push_preserves_words.
Print Assumptions extend_memory_preserves_words.
{Ucost : Basics.Unreachable (Support.UintN.t → Support.UintN.t)}
{UE : Basics.Unreachable (U.t → U.t → @Mem.t Host_t F → option (@Mem.t Host_t F))}
start count :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
machineSafe invariant (fun _ ⇒ True)
(@V.Executor.extend_memory_to Host_t F E start count).
Proof.
intro Hrevision. unfold V.Executor.extend_memory_to.
destruct (U.op_eq count (@U.zero)); [apply safe_return; exact I|].
eapply safe_bind; [apply safe_memory_read|]. intros current Hcurrent.
eapply safe_bind.
- apply (safe_option invariant memoryWordChunks). intros extended Hextended.
eapply extend_word_chunks; [exact Hrevision|exact Hcurrent|exact Hextended].
- intros extended Hextended.
eapply safe_bind; [apply safe_memory_write; exact Hextended|]. intros [] _.
eapply safe_bind; [apply safe_fmap, safe_memory_read|]. intros newSize _.
destruct (Support.UintN.op_gteq _ _); apply safe_return; exact I.
Qed.
End Instructions.
Print Assumptions calldataload_preserves_words.
Print Assumptions push_preserves_words.
Print Assumptions extend_memory_preserves_words.