Library QedkConcrete.memorywords
Word-sized chunks in concrete VM memory
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import RocqOfOCaml.Unreachable.StringBounds.
Require Import QedkConcrete.nativeindices.
Require Import MonadVM.Vm QedkConcrete.wordrange QedkConcrete.bytewords QedkConcrete.memorybulk.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Lemma map_find_value_property {Key Value : Set} (order : OCamlMap.OrderedType (t := Key))
(P : Value → Prop) entries key value :
Forall (fun pair ⇒ P (snd pair)) entries →
OCamlMap.Make.find_opt order key entries = Some value → P value.
Proof.
intro Hentries. induction Hentries as [|[k v] rest Hv Hrest IH]; intro Hfind; [discriminate|].
cbn [OCamlMap.Make.find_opt] in Hfind.
destruct (OCamlMap.Make.key_before order key k); [discriminate|].
destruct (OCamlMap.Make.key_equal order key k).
- inversion Hfind; subst. exact Hv.
- apply IH. exact Hfind.
Qed.
Lemma map_add_value_property {Key Value : Set} (order : OCamlMap.OrderedType (t := Key))
(P : Value → Prop) entries key value :
P value → Forall (fun pair ⇒ P (snd pair)) entries →
Forall (fun pair ⇒ P (snd pair)) (OCamlMap.Make.add order key value entries).
Proof.
intros Hvalue Hentries. induction Hentries as [|[k v] rest Hv Hrest IH].
- constructor; [exact Hvalue|constructor].
- cbn [OCamlMap.Make.add].
destruct (OCamlMap.Make.key_before order key k).
+ constructor; [exact Hvalue|constructor; assumption].
+ destruct (OCamlMap.Make.key_equal order key k); constructor; assumption.
Qed.
Lemma list_init_property {A : Set} {UL : Basics.Unreachable (list A)}
(P : A → Prop) count f :
0 ≤ count < 2^62 → Z.ltb count 0 = false →
(∀ index, 0 ≤ index < count → P (f index)) →
Forall P (@OCamlList.init A UL count f).
Proof.
intros Hcount Htest Hf.
assert (Haux : ∀ n start, 0 ≤ start → start + Z.of_nat n < 2^62 →
(∀ index, start ≤ index < start + Z.of_nat n → P (f index)) →
Forall P (OCamlList.init_nat_aux n start f)).
{ induction n; intros start Hstart Hlimit Hindices; cbn [OCamlList.init_nat_aux].
- constructor.
- constructor.
+ apply Hindices. rewrite Nat2Z.inj_succ. lia.
+ unfold OCamlInt.add. rewrite native_repr_small by
(rewrite Nat2Z.inj_succ in Hlimit; lia).
apply IHn; try (rewrite Nat2Z.inj_succ in Hlimit; lia).
intros index Hindex. apply Hindices. rewrite Nat2Z.inj_succ. lia. }
unfold OCamlList.init.
change (Z.ltb count 0 = false) in Htest.
rewrite Htest. apply Haux; [lia|rewrite Z2Nat.id by lia; lia|].
intros index Hindex. apply Hf. rewrite Z2Nat.id in Hindex by lia. lia.
Qed.
Lemma list_init_length {A : Set} {UL : Basics.Unreachable (list A)} count f :
0 ≤ count → Z.ltb count 0 = false →
Z.of_nat (Lists.List.length (@OCamlList.init A UL count f)) = count.
Proof.
intros Hcount Htest.
assert (Haux : ∀ n start, Lists.List.length (OCamlList.init_nat_aux n start f) = n).
{ induction n; intro start; cbn [OCamlList.init_nat_aux Lists.List.length]; [reflexivity|].
rewrite IHn. reflexivity. }
unfold OCamlList.init.
change (Z.ltb count 0 = false) in Htest.
rewrite Htest, Haux, Z2Nat.id by exact Hcount. reflexivity.
Qed.
Module Mem := Vm.Make.Memory.
Section Memory.
Context {Host_t : Set} {F : @Vm.Make.FArgs Host_t}.
Context {UN : Basics.Unreachable Support.UintN.t} {UF : Basics.Unreachable U.t}
{Uunit : Basics.Unreachable unit} {UI : Basics.Unreachable int}
{US : Basics.Unreachable string} {UL : Basics.Unreachable (list string)}
{UZeros : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Local Definition memoryStringFallback := Opaque.legacy_witness
(@Mem._rocq_fallback_read_block_at_0 Host_t F).
Local Definition memoryReadFallback := Opaque.legacy_witness
(@Mem._rocq_fallback_read_block_at_1 Host_t F).
Local Definition memoryWriteFallback := Opaque.legacy_witness
(@Mem._rocq_fallback_write_block_at_1 Host_t F).
Definition memoryWordChunks (memory : @Mem.t Host_t F) : Prop :=
Forall (fun pair ⇒ OCamlString.length (snd pair) = 32) (Mem.t.contents memory).
Lemma empty_word_chunks capacity : memoryWordChunks (@Mem.empty Host_t F capacity).
Proof. constructor. Qed.
Lemma read_aligned_length address memory : memoryWordChunks memory →
OCamlString.length (@Mem.read_aligned Host_t F address memory) = 32.
Proof.
intro Hmemory. unfold Mem.read_aligned. cbn beta iota zeta.
destruct (Map.S.find_opt _ address (Mem.t.contents memory)) as [bytes|] eqn:Hfind.
- change (OCamlString.length bytes = 32).
eapply (map_find_value_property _ (fun bytes ⇒ OCamlString.length bytes = 32));
[exact Hmemory|exact Hfind].
- apply b32_zeros_length.
Qed.
Lemma write_aligned_word_chunks address bytes memory :
OCamlString.length bytes = 32 → memoryWordChunks memory →
memoryWordChunks (@Mem.write_aligned Host_t F address bytes memory).
Proof.
intros Hbytes Hmemory. unfold memoryWordChunks, Mem.write_aligned.
cbn beta iota zeta delta [Mem.t.with_contents Mem.t.contents].
apply (map_add_value_property
{| OCamlMap.OrderedType.compare := OCamlInt.compare |}
(fun bytes ⇒ OCamlString.length bytes = 32)); assumption.
Qed.
Lemma align_offset_range address : 0 ≤ snd (@Mem.align Host_t F address) < 32.
Proof.
change (0 ≤ OCamlInt.repr (Z.land address (Z.ones 5)) < 2^5).
rewrite Z.land_ones by lia.
pose proof (Z.mod_pos_bound address (2^5) ltac:(lia)) as Hmod.
rewrite OCamlInt.repr_small; [exact Hmod|].
change (-4611686018427387904 ≤ address mod 2^5 ≤ 4611686018427387903).
lia.
Qed.
Lemma chunks_cover offset count : 0 ≤ offset → 0 ≤ count →
let chunks := (offset + count + 31) ÷ 32 in
0 ≤ chunks ∧ offset + count ≤ 32 × chunks ∧ 32 × chunks < offset + count + 32.
Proof.
intros Hoffset Hcount. cbn zeta. rewrite Z.quot_div_nonneg by lia.
pose proof (Z.div_mod (offset + count + 31) 32 ltac:(lia)).
pose proof (Z.mod_pos_bound (offset + count + 31) 32 ltac:(lia)).
pose proof (Z.div_pos (offset + count + 31) 32 ltac:(lia) ltac:(lia)). lia.
Qed.
Lemma read_block32_length start memory : memoryWordChunks memory →
Z.ltb ((snd (@Mem.align Host_t F (@U.to_int start)) + 32 + 31) ÷ 32) 0 = false →
OCamlString.length (@Mem.read_block_at Host_t F start (@U.of_int 32) memory) = 32.
Proof.
intros Hmemory Hinit.
set (position := @U.to_int start).
set (offset := snd (@Mem.align Host_t F position)).
set (base := fst (@Mem.align Host_t F position)).
set (count := (offset + 32 + 31) ÷ 32).
fold position offset count in Hinit.
pose proof (align_offset_range position) as Hoffset. fold offset in Hoffset.
assert (HcountNative : ∀ fallback,
@OCamlInt.div fallback (OCamlInt.add (OCamlInt.add offset 32) 31) 32 = count).
{ intro fallback. unfold OCamlInt.add, OCamlInt.div.
rewrite (OCamlInt.repr_small (offset + 32)),
(OCamlInt.repr_small (offset + 32 + 31)) by
(unfold OCamlInt.min_int, OCamlInt.max_int, OCamlInt.sign_bit, OCamlInt.size, OCamlInt.Width.bits; lia).
cbn [Z.eqb]. rewrite OCamlInt.repr_small; [reflexivity|].
pose proof (chunks_cover offset 32 ltac:(lia) ltac:(lia)) as [Hcount Hbounds].
change (-4611686018427387904 ≤ (offset + 32 + 31) ÷ 32 ≤ 4611686018427387903).
lia. }
change (OCamlString.length (@OCamlString.sub memoryStringFallback
(OCamlString.concat EmptyString (@OCamlList.init string memoryReadFallback
(@OCamlInt.div (Opaque.legacy_witness (@Mem._rocq_fallback_read_block_at_2 Host_t F))
(OCamlInt.add (OCamlInt.add offset 32) 31) 32)
(fun index ⇒ @Mem.read_aligned Host_t F
(OCamlInt.add base (OCamlInt.mul index 32)) memory)))
offset 32) = 32).
rewrite HcountNative.
pose proof (chunks_cover offset 32 ltac:(lia) ltac:(lia)) as [Hcount [Hcover Hpadding]].
fold count in Hcount, Hcover, Hpadding.
apply StringBounds.sub_length_in_bounds; [lia|lia|].
rewrite concat32_length.
- rewrite list_init_length by assumption. exact Hcover.
- apply list_init_property; [lia|exact Hinit|]. intros index Hindex.
apply read_aligned_length. exact Hmemory.
Qed.
Lemma read_word_range {UR : Basics.Unreachable U.Repr.t} {UC : Basics.Unreachable ascii}
start memory : memoryWordChunks memory →
Z.ltb ((snd (@Mem.align Host_t F (@U.to_int start)) + 32 + 31) ÷ 32) 0 = false →
wordRange (@Mem.read_word_at Host_t F start memory).
Proof.
intros Hmemory Hinit. unfold Mem.read_word_at.
destruct (Mem.is_aligned (@U.to_int start)); apply word_repr.
- rewrite read_aligned_length by exact Hmemory. lia.
- change (OCamlString.length
(@Byte_string.Fixed.Fixed_result.of_bytes_exn _ _ _ Byte_string.B32
(@Mem.read_block_at Host_t F start (@U.of_int 32) memory)) ≤ 32).
rewrite b32_checked_identity by (apply read_block32_length; assumption).
rewrite read_block32_length by assumption. lia.
Qed.
Lemma write_block_word_chunks {UB : Basics.Unreachable Byte_string.Bytes.t}
{ULI : Basics.Unreachable (list int)} start bytes memory :
memoryWordChunks memory →
OCamlString.length bytes < 2^32 →
Z.ltb ((snd (@Mem.align Host_t F (@U.to_int start)) +
OCamlString.length bytes + 31) ÷ 32) 0 = false →
memoryWordChunks (@Mem.write_block_at Host_t F start bytes memory).
Proof.
intros Hmemory HsizeLimit Hinit.
set (position := @U.to_int start).
set (offset := snd (@Mem.align Host_t F position)).
set (base := fst (@Mem.align Host_t F position)).
set (size := OCamlString.length bytes).
set (count := (offset + size + 31) ÷ 32).
set (suffixSize := count × 32 - offset - size).
fold position offset size count in Hinit.
pose proof (align_offset_range position) as Hoffset. fold offset in Hoffset.
assert (Hsize : 0 ≤ size) by (unfold size, OCamlString.length; lia).
pose proof (chunks_cover offset size ltac:(lia) Hsize) as [Hcount [Hcover Hpadding]].
fold count in Hcount, Hcover, Hpadding.
assert (Hsuffix : 0 ≤ suffixSize < 32) by (unfold suffixSize; lia).
assert (HcountNative : ∀ fallback,
@OCamlInt.div fallback (OCamlInt.add (OCamlInt.add offset size) 31) 32 = count).
{ intro fallback. rewrite memory_word_count by (unfold size in *; lia).
unfold count. rewrite Z.quot_div_nonneg by lia. reflexivity. }
assert (HsuffixNative : OCamlInt.sub (OCamlInt.sub (OCamlInt.mul count 32) offset) size
= suffixSize).
{ unfold suffixSize, count. rewrite Z.quot_div_nonneg by lia.
apply memory_suffix_length; unfold size in *; lia. }
set (prefix := if Z.eqb offset 0
then EmptyString
else @OCamlString.sub memoryStringFallback (@Mem.read_aligned Host_t F base memory) 0 offset).
set (suffix := if Z.eqb suffixSize 0
then EmptyString
else @OCamlString.sub memoryStringFallback
(@Mem.read_aligned Host_t F
(OCamlInt.add base (OCamlInt.mul (OCamlInt.sub count 1) 32)) memory)
(32-suffixSize) suffixSize).
assert (HprefixLength : OCamlString.length prefix = offset).
{ unfold prefix.
destruct (Z.eqb_spec offset 0) as [Hzero|Hnonzero].
- rewrite Hzero. reflexivity.
- apply StringBounds.sub_length_in_bounds; [lia|lia|].
rewrite read_aligned_length by exact Hmemory. lia. }
assert (HsuffixLength : OCamlString.length suffix = suffixSize).
{ unfold suffix.
destruct (Z.eqb_spec suffixSize 0) as [Hzero|Hnonzero].
- rewrite Hzero. reflexivity.
- apply StringBounds.sub_length_in_bounds; [lia|lia|].
rewrite read_aligned_length by exact Hmemory. lia. }
set (buffer := Strings.String.append prefix (Strings.String.append bytes suffix)).
assert (HbufferLength : OCamlString.length buffer = 32×count).
{ unfold buffer. rewrite !byte_append_length, HprefixLength, HsuffixLength.
fold size. unfold suffixSize. lia. }
unfold Mem.write_block_at.
destruct (Byte_string.Bytes.op_eq bytes Byte_string.Bytes.empty); [exact Hmemory|].
cbn beta iota zeta. cbn [Mem.align]. fold position base offset size.
change (OCamlInt.logand position (@Mem.alignment_mask Host_t F)) with offset.
change (OCamlInt.logand position (OCamlInt.lognot (@Mem.alignment_mask Host_t F))) with base.
change (Byte_string.Bytes.length bytes) with size.
rewrite HcountNative, HsuffixNative.
replace (OCamlInt.sub 32 suffixSize) with (32-suffixSize)
by (symmetry; apply native_repr_small; lia).
rewrite memory_bulk_as_fold.
change (memoryWordChunks (@OCamlList.fold_left (@Mem.t Host_t F) int
(fun mem index ⇒ @Mem.write_aligned Host_t F (OCamlInt.add base (OCamlInt.mul index 32))
(@Byte_string.Fixed.Fixed_result.sub _ _ _ Byte_string.B32 buffer (OCamlInt.mul index 32)) mem)
memory (@OCamlList.init int memoryWriteFallback count (fun index ⇒ index)))).
assert (Hindices : Forall (fun index ⇒ 0 ≤ index < count)
(@OCamlList.init int memoryWriteFallback count (fun index ⇒ index))).
{ apply list_init_property; [unfold count, size in *; lia|exact Hinit|].
intros index Hindex. exact Hindex. }
assert (Hfold : ∀ indices, Forall (fun index ⇒ 0 ≤ index < count) indices →
∀ mem, memoryWordChunks mem →
memoryWordChunks (@OCamlList.fold_left (@Mem.t Host_t F) int
(fun mem index ⇒ @Mem.write_aligned Host_t F (OCamlInt.add base (OCamlInt.mul index 32))
(@Byte_string.Fixed.Fixed_result.sub _ _ _ Byte_string.B32 buffer (OCamlInt.mul index 32)) mem)
mem indices)).
{ intros indices Hbounds. induction Hbounds as [|index rest Hindex Hrest IH]; intros mem Hmem.
- exact Hmem.
- cbn [OCamlList.fold_left]. apply IH, write_aligned_word_chunks; [|exact Hmem].
unfold OCamlInt.mul. rewrite native_repr_small by (unfold size in *; lia).
apply b32_sub_length; [lia|]. rewrite HbufferLength. lia. }
apply Hfold; assumption.
Qed.
Lemma write_word_chunks {UR : Basics.Unreachable U.Repr.t}
{UB : Basics.Unreachable Byte_string.Bytes.t} {ULI : Basics.Unreachable (list int)}
{UC : Basics.Unreachable ascii} {UP : Basics.Unreachable (int × int)} start value memory :
memoryWordChunks memory →
Z.ltb ((snd (@Mem.align Host_t F (@U.to_int start)) + 32 + 31) ÷ 32) 0 = false →
memoryWordChunks (@Mem.write_word_at Host_t F start value memory).
Proof.
intros Hmemory Hinit. unfold Mem.write_word_at.
destruct (Mem.is_aligned (@U.to_int start)).
- apply write_aligned_word_chunks; [apply word_encoding_length|exact Hmemory].
- apply write_block_word_chunks; [exact Hmemory| |].
+ rewrite word_bytes_encoding_length. lia.
+ rewrite word_bytes_encoding_length. exact Hinit.
Qed.
Lemma write_byte_chunks {UB : Basics.Unreachable Byte_string.Bytes.t}
{ULI : Basics.Unreachable (list int)} start byte memory :
memoryWordChunks memory →
Z.ltb ((snd (@Mem.align Host_t F (@U.to_int start)) + 1 + 31) ÷ 32) 0 = false →
memoryWordChunks (@Mem.write_byte_at Host_t F start byte memory).
Proof.
intros Hmemory Hinit. unfold Mem.write_byte_at.
apply write_block_word_chunks; [exact Hmemory| |exact Hinit].
change (1 < 2^32). lia.
Qed.
Lemma extend_word_chunks
{UE : Basics.Unreachable (U.t → U.t → @Mem.t Host_t F → option (@Mem.t Host_t F))}
start count memory result :
(@Vm.Make.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
memoryWordChunks memory →
@Mem.extend_to Host_t F start count memory = Some result → memoryWordChunks result.
Proof.
intros Hrevision Hmemory. unfold Mem.extend_to. rewrite Hrevision.
unfold Mem.extend_to_monad_nine.
repeat match goal with |- context[if ?test then _ else _] ⇒ destruct test end;
intro Hresult; inversion Hresult; subst; exact Hmemory.
Qed.
End Memory.
Print Assumptions read_word_range.
Print Assumptions write_block_word_chunks.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import RocqOfOCaml.Unreachable.StringBounds.
Require Import QedkConcrete.nativeindices.
Require Import MonadVM.Vm QedkConcrete.wordrange QedkConcrete.bytewords QedkConcrete.memorybulk.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Lemma map_find_value_property {Key Value : Set} (order : OCamlMap.OrderedType (t := Key))
(P : Value → Prop) entries key value :
Forall (fun pair ⇒ P (snd pair)) entries →
OCamlMap.Make.find_opt order key entries = Some value → P value.
Proof.
intro Hentries. induction Hentries as [|[k v] rest Hv Hrest IH]; intro Hfind; [discriminate|].
cbn [OCamlMap.Make.find_opt] in Hfind.
destruct (OCamlMap.Make.key_before order key k); [discriminate|].
destruct (OCamlMap.Make.key_equal order key k).
- inversion Hfind; subst. exact Hv.
- apply IH. exact Hfind.
Qed.
Lemma map_add_value_property {Key Value : Set} (order : OCamlMap.OrderedType (t := Key))
(P : Value → Prop) entries key value :
P value → Forall (fun pair ⇒ P (snd pair)) entries →
Forall (fun pair ⇒ P (snd pair)) (OCamlMap.Make.add order key value entries).
Proof.
intros Hvalue Hentries. induction Hentries as [|[k v] rest Hv Hrest IH].
- constructor; [exact Hvalue|constructor].
- cbn [OCamlMap.Make.add].
destruct (OCamlMap.Make.key_before order key k).
+ constructor; [exact Hvalue|constructor; assumption].
+ destruct (OCamlMap.Make.key_equal order key k); constructor; assumption.
Qed.
Lemma list_init_property {A : Set} {UL : Basics.Unreachable (list A)}
(P : A → Prop) count f :
0 ≤ count < 2^62 → Z.ltb count 0 = false →
(∀ index, 0 ≤ index < count → P (f index)) →
Forall P (@OCamlList.init A UL count f).
Proof.
intros Hcount Htest Hf.
assert (Haux : ∀ n start, 0 ≤ start → start + Z.of_nat n < 2^62 →
(∀ index, start ≤ index < start + Z.of_nat n → P (f index)) →
Forall P (OCamlList.init_nat_aux n start f)).
{ induction n; intros start Hstart Hlimit Hindices; cbn [OCamlList.init_nat_aux].
- constructor.
- constructor.
+ apply Hindices. rewrite Nat2Z.inj_succ. lia.
+ unfold OCamlInt.add. rewrite native_repr_small by
(rewrite Nat2Z.inj_succ in Hlimit; lia).
apply IHn; try (rewrite Nat2Z.inj_succ in Hlimit; lia).
intros index Hindex. apply Hindices. rewrite Nat2Z.inj_succ. lia. }
unfold OCamlList.init.
change (Z.ltb count 0 = false) in Htest.
rewrite Htest. apply Haux; [lia|rewrite Z2Nat.id by lia; lia|].
intros index Hindex. apply Hf. rewrite Z2Nat.id in Hindex by lia. lia.
Qed.
Lemma list_init_length {A : Set} {UL : Basics.Unreachable (list A)} count f :
0 ≤ count → Z.ltb count 0 = false →
Z.of_nat (Lists.List.length (@OCamlList.init A UL count f)) = count.
Proof.
intros Hcount Htest.
assert (Haux : ∀ n start, Lists.List.length (OCamlList.init_nat_aux n start f) = n).
{ induction n; intro start; cbn [OCamlList.init_nat_aux Lists.List.length]; [reflexivity|].
rewrite IHn. reflexivity. }
unfold OCamlList.init.
change (Z.ltb count 0 = false) in Htest.
rewrite Htest, Haux, Z2Nat.id by exact Hcount. reflexivity.
Qed.
Module Mem := Vm.Make.Memory.
Section Memory.
Context {Host_t : Set} {F : @Vm.Make.FArgs Host_t}.
Context {UN : Basics.Unreachable Support.UintN.t} {UF : Basics.Unreachable U.t}
{Uunit : Basics.Unreachable unit} {UI : Basics.Unreachable int}
{US : Basics.Unreachable string} {UL : Basics.Unreachable (list string)}
{UZeros : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Local Definition memoryStringFallback := Opaque.legacy_witness
(@Mem._rocq_fallback_read_block_at_0 Host_t F).
Local Definition memoryReadFallback := Opaque.legacy_witness
(@Mem._rocq_fallback_read_block_at_1 Host_t F).
Local Definition memoryWriteFallback := Opaque.legacy_witness
(@Mem._rocq_fallback_write_block_at_1 Host_t F).
Definition memoryWordChunks (memory : @Mem.t Host_t F) : Prop :=
Forall (fun pair ⇒ OCamlString.length (snd pair) = 32) (Mem.t.contents memory).
Lemma empty_word_chunks capacity : memoryWordChunks (@Mem.empty Host_t F capacity).
Proof. constructor. Qed.
Lemma read_aligned_length address memory : memoryWordChunks memory →
OCamlString.length (@Mem.read_aligned Host_t F address memory) = 32.
Proof.
intro Hmemory. unfold Mem.read_aligned. cbn beta iota zeta.
destruct (Map.S.find_opt _ address (Mem.t.contents memory)) as [bytes|] eqn:Hfind.
- change (OCamlString.length bytes = 32).
eapply (map_find_value_property _ (fun bytes ⇒ OCamlString.length bytes = 32));
[exact Hmemory|exact Hfind].
- apply b32_zeros_length.
Qed.
Lemma write_aligned_word_chunks address bytes memory :
OCamlString.length bytes = 32 → memoryWordChunks memory →
memoryWordChunks (@Mem.write_aligned Host_t F address bytes memory).
Proof.
intros Hbytes Hmemory. unfold memoryWordChunks, Mem.write_aligned.
cbn beta iota zeta delta [Mem.t.with_contents Mem.t.contents].
apply (map_add_value_property
{| OCamlMap.OrderedType.compare := OCamlInt.compare |}
(fun bytes ⇒ OCamlString.length bytes = 32)); assumption.
Qed.
Lemma align_offset_range address : 0 ≤ snd (@Mem.align Host_t F address) < 32.
Proof.
change (0 ≤ OCamlInt.repr (Z.land address (Z.ones 5)) < 2^5).
rewrite Z.land_ones by lia.
pose proof (Z.mod_pos_bound address (2^5) ltac:(lia)) as Hmod.
rewrite OCamlInt.repr_small; [exact Hmod|].
change (-4611686018427387904 ≤ address mod 2^5 ≤ 4611686018427387903).
lia.
Qed.
Lemma chunks_cover offset count : 0 ≤ offset → 0 ≤ count →
let chunks := (offset + count + 31) ÷ 32 in
0 ≤ chunks ∧ offset + count ≤ 32 × chunks ∧ 32 × chunks < offset + count + 32.
Proof.
intros Hoffset Hcount. cbn zeta. rewrite Z.quot_div_nonneg by lia.
pose proof (Z.div_mod (offset + count + 31) 32 ltac:(lia)).
pose proof (Z.mod_pos_bound (offset + count + 31) 32 ltac:(lia)).
pose proof (Z.div_pos (offset + count + 31) 32 ltac:(lia) ltac:(lia)). lia.
Qed.
Lemma read_block32_length start memory : memoryWordChunks memory →
Z.ltb ((snd (@Mem.align Host_t F (@U.to_int start)) + 32 + 31) ÷ 32) 0 = false →
OCamlString.length (@Mem.read_block_at Host_t F start (@U.of_int 32) memory) = 32.
Proof.
intros Hmemory Hinit.
set (position := @U.to_int start).
set (offset := snd (@Mem.align Host_t F position)).
set (base := fst (@Mem.align Host_t F position)).
set (count := (offset + 32 + 31) ÷ 32).
fold position offset count in Hinit.
pose proof (align_offset_range position) as Hoffset. fold offset in Hoffset.
assert (HcountNative : ∀ fallback,
@OCamlInt.div fallback (OCamlInt.add (OCamlInt.add offset 32) 31) 32 = count).
{ intro fallback. unfold OCamlInt.add, OCamlInt.div.
rewrite (OCamlInt.repr_small (offset + 32)),
(OCamlInt.repr_small (offset + 32 + 31)) by
(unfold OCamlInt.min_int, OCamlInt.max_int, OCamlInt.sign_bit, OCamlInt.size, OCamlInt.Width.bits; lia).
cbn [Z.eqb]. rewrite OCamlInt.repr_small; [reflexivity|].
pose proof (chunks_cover offset 32 ltac:(lia) ltac:(lia)) as [Hcount Hbounds].
change (-4611686018427387904 ≤ (offset + 32 + 31) ÷ 32 ≤ 4611686018427387903).
lia. }
change (OCamlString.length (@OCamlString.sub memoryStringFallback
(OCamlString.concat EmptyString (@OCamlList.init string memoryReadFallback
(@OCamlInt.div (Opaque.legacy_witness (@Mem._rocq_fallback_read_block_at_2 Host_t F))
(OCamlInt.add (OCamlInt.add offset 32) 31) 32)
(fun index ⇒ @Mem.read_aligned Host_t F
(OCamlInt.add base (OCamlInt.mul index 32)) memory)))
offset 32) = 32).
rewrite HcountNative.
pose proof (chunks_cover offset 32 ltac:(lia) ltac:(lia)) as [Hcount [Hcover Hpadding]].
fold count in Hcount, Hcover, Hpadding.
apply StringBounds.sub_length_in_bounds; [lia|lia|].
rewrite concat32_length.
- rewrite list_init_length by assumption. exact Hcover.
- apply list_init_property; [lia|exact Hinit|]. intros index Hindex.
apply read_aligned_length. exact Hmemory.
Qed.
Lemma read_word_range {UR : Basics.Unreachable U.Repr.t} {UC : Basics.Unreachable ascii}
start memory : memoryWordChunks memory →
Z.ltb ((snd (@Mem.align Host_t F (@U.to_int start)) + 32 + 31) ÷ 32) 0 = false →
wordRange (@Mem.read_word_at Host_t F start memory).
Proof.
intros Hmemory Hinit. unfold Mem.read_word_at.
destruct (Mem.is_aligned (@U.to_int start)); apply word_repr.
- rewrite read_aligned_length by exact Hmemory. lia.
- change (OCamlString.length
(@Byte_string.Fixed.Fixed_result.of_bytes_exn _ _ _ Byte_string.B32
(@Mem.read_block_at Host_t F start (@U.of_int 32) memory)) ≤ 32).
rewrite b32_checked_identity by (apply read_block32_length; assumption).
rewrite read_block32_length by assumption. lia.
Qed.
Lemma write_block_word_chunks {UB : Basics.Unreachable Byte_string.Bytes.t}
{ULI : Basics.Unreachable (list int)} start bytes memory :
memoryWordChunks memory →
OCamlString.length bytes < 2^32 →
Z.ltb ((snd (@Mem.align Host_t F (@U.to_int start)) +
OCamlString.length bytes + 31) ÷ 32) 0 = false →
memoryWordChunks (@Mem.write_block_at Host_t F start bytes memory).
Proof.
intros Hmemory HsizeLimit Hinit.
set (position := @U.to_int start).
set (offset := snd (@Mem.align Host_t F position)).
set (base := fst (@Mem.align Host_t F position)).
set (size := OCamlString.length bytes).
set (count := (offset + size + 31) ÷ 32).
set (suffixSize := count × 32 - offset - size).
fold position offset size count in Hinit.
pose proof (align_offset_range position) as Hoffset. fold offset in Hoffset.
assert (Hsize : 0 ≤ size) by (unfold size, OCamlString.length; lia).
pose proof (chunks_cover offset size ltac:(lia) Hsize) as [Hcount [Hcover Hpadding]].
fold count in Hcount, Hcover, Hpadding.
assert (Hsuffix : 0 ≤ suffixSize < 32) by (unfold suffixSize; lia).
assert (HcountNative : ∀ fallback,
@OCamlInt.div fallback (OCamlInt.add (OCamlInt.add offset size) 31) 32 = count).
{ intro fallback. rewrite memory_word_count by (unfold size in *; lia).
unfold count. rewrite Z.quot_div_nonneg by lia. reflexivity. }
assert (HsuffixNative : OCamlInt.sub (OCamlInt.sub (OCamlInt.mul count 32) offset) size
= suffixSize).
{ unfold suffixSize, count. rewrite Z.quot_div_nonneg by lia.
apply memory_suffix_length; unfold size in *; lia. }
set (prefix := if Z.eqb offset 0
then EmptyString
else @OCamlString.sub memoryStringFallback (@Mem.read_aligned Host_t F base memory) 0 offset).
set (suffix := if Z.eqb suffixSize 0
then EmptyString
else @OCamlString.sub memoryStringFallback
(@Mem.read_aligned Host_t F
(OCamlInt.add base (OCamlInt.mul (OCamlInt.sub count 1) 32)) memory)
(32-suffixSize) suffixSize).
assert (HprefixLength : OCamlString.length prefix = offset).
{ unfold prefix.
destruct (Z.eqb_spec offset 0) as [Hzero|Hnonzero].
- rewrite Hzero. reflexivity.
- apply StringBounds.sub_length_in_bounds; [lia|lia|].
rewrite read_aligned_length by exact Hmemory. lia. }
assert (HsuffixLength : OCamlString.length suffix = suffixSize).
{ unfold suffix.
destruct (Z.eqb_spec suffixSize 0) as [Hzero|Hnonzero].
- rewrite Hzero. reflexivity.
- apply StringBounds.sub_length_in_bounds; [lia|lia|].
rewrite read_aligned_length by exact Hmemory. lia. }
set (buffer := Strings.String.append prefix (Strings.String.append bytes suffix)).
assert (HbufferLength : OCamlString.length buffer = 32×count).
{ unfold buffer. rewrite !byte_append_length, HprefixLength, HsuffixLength.
fold size. unfold suffixSize. lia. }
unfold Mem.write_block_at.
destruct (Byte_string.Bytes.op_eq bytes Byte_string.Bytes.empty); [exact Hmemory|].
cbn beta iota zeta. cbn [Mem.align]. fold position base offset size.
change (OCamlInt.logand position (@Mem.alignment_mask Host_t F)) with offset.
change (OCamlInt.logand position (OCamlInt.lognot (@Mem.alignment_mask Host_t F))) with base.
change (Byte_string.Bytes.length bytes) with size.
rewrite HcountNative, HsuffixNative.
replace (OCamlInt.sub 32 suffixSize) with (32-suffixSize)
by (symmetry; apply native_repr_small; lia).
rewrite memory_bulk_as_fold.
change (memoryWordChunks (@OCamlList.fold_left (@Mem.t Host_t F) int
(fun mem index ⇒ @Mem.write_aligned Host_t F (OCamlInt.add base (OCamlInt.mul index 32))
(@Byte_string.Fixed.Fixed_result.sub _ _ _ Byte_string.B32 buffer (OCamlInt.mul index 32)) mem)
memory (@OCamlList.init int memoryWriteFallback count (fun index ⇒ index)))).
assert (Hindices : Forall (fun index ⇒ 0 ≤ index < count)
(@OCamlList.init int memoryWriteFallback count (fun index ⇒ index))).
{ apply list_init_property; [unfold count, size in *; lia|exact Hinit|].
intros index Hindex. exact Hindex. }
assert (Hfold : ∀ indices, Forall (fun index ⇒ 0 ≤ index < count) indices →
∀ mem, memoryWordChunks mem →
memoryWordChunks (@OCamlList.fold_left (@Mem.t Host_t F) int
(fun mem index ⇒ @Mem.write_aligned Host_t F (OCamlInt.add base (OCamlInt.mul index 32))
(@Byte_string.Fixed.Fixed_result.sub _ _ _ Byte_string.B32 buffer (OCamlInt.mul index 32)) mem)
mem indices)).
{ intros indices Hbounds. induction Hbounds as [|index rest Hindex Hrest IH]; intros mem Hmem.
- exact Hmem.
- cbn [OCamlList.fold_left]. apply IH, write_aligned_word_chunks; [|exact Hmem].
unfold OCamlInt.mul. rewrite native_repr_small by (unfold size in *; lia).
apply b32_sub_length; [lia|]. rewrite HbufferLength. lia. }
apply Hfold; assumption.
Qed.
Lemma write_word_chunks {UR : Basics.Unreachable U.Repr.t}
{UB : Basics.Unreachable Byte_string.Bytes.t} {ULI : Basics.Unreachable (list int)}
{UC : Basics.Unreachable ascii} {UP : Basics.Unreachable (int × int)} start value memory :
memoryWordChunks memory →
Z.ltb ((snd (@Mem.align Host_t F (@U.to_int start)) + 32 + 31) ÷ 32) 0 = false →
memoryWordChunks (@Mem.write_word_at Host_t F start value memory).
Proof.
intros Hmemory Hinit. unfold Mem.write_word_at.
destruct (Mem.is_aligned (@U.to_int start)).
- apply write_aligned_word_chunks; [apply word_encoding_length|exact Hmemory].
- apply write_block_word_chunks; [exact Hmemory| |].
+ rewrite word_bytes_encoding_length. lia.
+ rewrite word_bytes_encoding_length. exact Hinit.
Qed.
Lemma write_byte_chunks {UB : Basics.Unreachable Byte_string.Bytes.t}
{ULI : Basics.Unreachable (list int)} start byte memory :
memoryWordChunks memory →
Z.ltb ((snd (@Mem.align Host_t F (@U.to_int start)) + 1 + 31) ÷ 32) 0 = false →
memoryWordChunks (@Mem.write_byte_at Host_t F start byte memory).
Proof.
intros Hmemory Hinit. unfold Mem.write_byte_at.
apply write_block_word_chunks; [exact Hmemory| |exact Hinit].
change (1 < 2^32). lia.
Qed.
Lemma extend_word_chunks
{UE : Basics.Unreachable (U.t → U.t → @Mem.t Host_t F → option (@Mem.t Host_t F))}
start count memory result :
(@Vm.Make.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
memoryWordChunks memory →
@Mem.extend_to Host_t F start count memory = Some result → memoryWordChunks result.
Proof.
intros Hrevision Hmemory. unfold Mem.extend_to. rewrite Hrevision.
unfold Mem.extend_to_monad_nine.
repeat match goal with |- context[if ?test then _ else _] ⇒ destruct test end;
intro Hresult; inversion Hresult; subst; exact Hmemory.
Qed.
End Memory.
Print Assumptions read_word_range.
Print Assumptions write_block_word_chunks.