Library QedkConcrete.memorycopybounds
Sizes of the byte buffers written by copy instructions
From Stdlib Require Import String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import RocqOfOCaml.Unreachable.StringBounds.
Require Import MonadVM.Vm QedkConcrete.memorysizes QedkConcrete.wordrange
QedkConcrete.bytewords QedkConcrete.nativeindices.
Require Import MonadExecutionSpecs.Refinement.VmPcConversion.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Lemma small_word_to_int_opt size :
0 ≤ U.to_z size < 2^32 → U.to_int_opt size = Some (U.to_z size).
Proof.
intro Hsize. rewrite (@generated_u256_to_int_opt_eq UintN.primitive_int).
unfold pc_to_int_opt, OCamlZ.fits_int, OCamlZ.fits_signed,
OCamlZ.signed_min, OCamlZ.signed_max.
change (0 ≤ size < 2^32) in Hsize.
assert (Hlo : Z.leb (-(2^62)) size = true) by (apply Z.leb_le; lia).
assert (Hhi : Z.leb size (Z.pred (2^62)) = true) by (apply Z.leb_le; lia).
change ((if andb (-(2^62) <=? size) (size <=? Z.pred (2^62))
then Some (@OCamlZ.to_int UintN.primitive_int size) else None) = Some size).
rewrite Hlo, Hhi. cbn [andb].
unfold OCamlZ.to_int, OCamlZ.checked_signed, OCamlZ.fits_signed,
OCamlZ.signed_min, OCamlZ.signed_max.
change (Some (if andb (-(2^62) <=? size) (size <=? Z.pred (2^62))
then size else @Basics.unreachable Z UintN.primitive_int) = Some size).
rewrite Hlo, Hhi. reflexivity.
Qed.
Lemma copy_padding_length fallback count byte :
0 ≤ count → OCamlString.length (Byte_string.Bytes.make fallback count byte) = count.
Proof.
intro Hcount. unfold Byte_string.Bytes.make, MonadVM.String.make.
rewrite OCamlString.make_matches_structural.
rewrite (proj2 (Z.ltb_ge _ _) Hcount).
assert (Hnat : ∀ n, Strings.String.length (OCamlString.make_nat n byte) = n).
{ induction n; cbn [OCamlString.make_nat Strings.String.length]; congruence. }
unfold OCamlString.length. rewrite Hnat, Z2Nat.id by lia. reflexivity.
Qed.
Lemma copy_input_length fallback data start size :
0 ≤ U.to_z size < 2^32 →
OCamlString.length
(match U.to_int_opt start, U.to_int_opt size with
| Some offset, Some count ⇒ Byte_string.Bytes.sub_with_zero_padding data offset count
| None, Some count ⇒ Byte_string.Bytes.make fallback count "000"%char
| _, None ⇒ fallback
end) = U.to_z size.
Proof.
intro Hsize. rewrite (small_word_to_int_opt size Hsize).
destruct (U.to_int_opt start).
- apply padded_length. lia.
- apply copy_padding_length. lia.
Qed.
Lemma truncated_output_length fallback bytes size :
0 ≤ U.to_z size < 2^32 →
OCamlString.length
(match U.to_int_opt size with
| None ⇒ bytes
| Some count ⇒ Byte_string.Bytes.sub fallback bytes 0
(Z.min count (Byte_string.Bytes.length bytes))
end) < 2^32.
Proof.
intro Hsize. rewrite (small_word_to_int_opt size Hsize).
change (OCamlString.length (@OCamlString.sub (Opaque.legacy_witness fallback)
bytes 0 (Z.min (U.to_z size) (OCamlString.length bytes))) < 2^32).
rewrite StringBounds.sub_length_in_bounds.
- pose proof (Z.le_min_l (U.to_z size) (OCamlString.length bytes)). lia.
- lia.
- apply Z.min_glb; [lia|unfold OCamlString.length; lia].
- pose proof (Z.le_min_r (U.to_z size) (OCamlString.length bytes)). lia.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import RocqOfOCaml.Unreachable.StringBounds.
Require Import MonadVM.Vm QedkConcrete.memorysizes QedkConcrete.wordrange
QedkConcrete.bytewords QedkConcrete.nativeindices.
Require Import MonadExecutionSpecs.Refinement.VmPcConversion.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Lemma small_word_to_int_opt size :
0 ≤ U.to_z size < 2^32 → U.to_int_opt size = Some (U.to_z size).
Proof.
intro Hsize. rewrite (@generated_u256_to_int_opt_eq UintN.primitive_int).
unfold pc_to_int_opt, OCamlZ.fits_int, OCamlZ.fits_signed,
OCamlZ.signed_min, OCamlZ.signed_max.
change (0 ≤ size < 2^32) in Hsize.
assert (Hlo : Z.leb (-(2^62)) size = true) by (apply Z.leb_le; lia).
assert (Hhi : Z.leb size (Z.pred (2^62)) = true) by (apply Z.leb_le; lia).
change ((if andb (-(2^62) <=? size) (size <=? Z.pred (2^62))
then Some (@OCamlZ.to_int UintN.primitive_int size) else None) = Some size).
rewrite Hlo, Hhi. cbn [andb].
unfold OCamlZ.to_int, OCamlZ.checked_signed, OCamlZ.fits_signed,
OCamlZ.signed_min, OCamlZ.signed_max.
change (Some (if andb (-(2^62) <=? size) (size <=? Z.pred (2^62))
then size else @Basics.unreachable Z UintN.primitive_int) = Some size).
rewrite Hlo, Hhi. reflexivity.
Qed.
Lemma copy_padding_length fallback count byte :
0 ≤ count → OCamlString.length (Byte_string.Bytes.make fallback count byte) = count.
Proof.
intro Hcount. unfold Byte_string.Bytes.make, MonadVM.String.make.
rewrite OCamlString.make_matches_structural.
rewrite (proj2 (Z.ltb_ge _ _) Hcount).
assert (Hnat : ∀ n, Strings.String.length (OCamlString.make_nat n byte) = n).
{ induction n; cbn [OCamlString.make_nat Strings.String.length]; congruence. }
unfold OCamlString.length. rewrite Hnat, Z2Nat.id by lia. reflexivity.
Qed.
Lemma copy_input_length fallback data start size :
0 ≤ U.to_z size < 2^32 →
OCamlString.length
(match U.to_int_opt start, U.to_int_opt size with
| Some offset, Some count ⇒ Byte_string.Bytes.sub_with_zero_padding data offset count
| None, Some count ⇒ Byte_string.Bytes.make fallback count "000"%char
| _, None ⇒ fallback
end) = U.to_z size.
Proof.
intro Hsize. rewrite (small_word_to_int_opt size Hsize).
destruct (U.to_int_opt start).
- apply padded_length. lia.
- apply copy_padding_length. lia.
Qed.
Lemma truncated_output_length fallback bytes size :
0 ≤ U.to_z size < 2^32 →
OCamlString.length
(match U.to_int_opt size with
| None ⇒ bytes
| Some count ⇒ Byte_string.Bytes.sub fallback bytes 0
(Z.min count (Byte_string.Bytes.length bytes))
end) < 2^32.
Proof.
intro Hsize. rewrite (small_word_to_int_opt size Hsize).
change (OCamlString.length (@OCamlString.sub (Opaque.legacy_witness fallback)
bytes 0 (Z.min (U.to_z size) (OCamlString.length bytes))) < 2^32).
rewrite StringBounds.sub_length_in_bounds.
- pose proof (Z.le_min_l (U.to_z size) (OCamlString.length bytes)). lia.
- lia.
- apply Z.min_glb; [lia|unfold OCamlString.length; lia].
- pose proof (Z.le_min_r (U.to_z size) (OCamlString.length bytes)). lia.
Qed.