Library QedkConcrete.nativeindices

Native integer bounds for VM memory indexing

OCaml int is signed 63-bit on the supported 64-bit platforms. The unsigned 32-bit EVMC memory capacity is small enough for the word-count and padding calculations. These are arithmetic lemmas, not extra VM contracts: callers must derive the size bound from successful extension.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import RocqOfOCaml.Opaque.
Local Open Scope Z_scope.
Set Default Goal Selector "!".

Lemma native_repr_small value : -(2^62) ≤ value < 2^62 →
  OCamlInt.repr value = value.
Proof.
  intro H. apply OCamlInt.repr_small.
  change (-(2^62) ≤ value ≤ 2^62-1). lia.
Qed.

Lemma native_repr_le_nonnegative value : 0 ≤ value →
  OCamlInt.repr value ≤ value.
Proof.
  intro Hvalue.
  unfold OCamlInt.repr, OCamlZ.signed_repr, OCamlInt.size, OCamlInt.Width.bits.
  change ((let v := value mod 2^63 in if 2^62 <=? v then v-2^63 else v) ≤ value).
  pose proof (Z.mod_le value (2^63) Hvalue ltac:(lia)) as Hmod.
  cbn zeta. destruct (2^62 <=? value mod 2^63); lia.
Qed.

Lemma native_nonnegative_quotient {fallback : Basics.Unreachable int} value divisor :
  0 ≤ value < 2^62 → 0 < divisor →
  0 ≤ @OCamlInt.div fallback value divisor.
Proof.
  intros Hvalue Hdivisor. unfold OCamlInt.div.
  rewrite (proj2 (Z.eqb_neq divisor 0) ltac:(lia)).
  rewrite Z.quot_div_nonneg by lia.
  pose proof (Z.div_pos value divisor ltac:(lia) ltac:(lia)).
  assert (value / divisor < 2^62) by (apply Z.div_lt_upper_bound; nia).
  rewrite native_repr_small by lia. lia.
Qed.

Lemma memory_word_count {fallback : Basics.Unreachable int} offset size :
  0 ≤ offset < 32 → 0 ≤ size < 2^32 →
  @OCamlInt.div fallback (OCamlInt.add (OCamlInt.add offset size) 31) 32 =
    (offset + size + 31) / 32.
Proof.
  intros Hoffset Hsize. unfold OCamlInt.add, OCamlInt.div.
  rewrite (native_repr_small (offset + size)) by lia.
  rewrite (native_repr_small (offset + size + 31)) by lia.
  cbn [Z.eqb]. rewrite Z.quot_div_nonneg by lia.
  apply native_repr_small.
  pose proof (Z.div_pos (offset + size + 31) 32 ltac:(lia) ltac:(lia)).
  assert ((offset + size + 31) / 32 < 2^32).
  { apply Z.div_lt_upper_bound; lia. }
  lia.
Qed.

Lemma memory_padding_bounds offset size :
  0 ≤ offset < 32 → 0 ≤ size < 2^32 →
  let count := (offset + size + 31) / 32 in
  0 ≤ count ∧ 32 × count < 2^32 + 64 ∧
  0 ≤ 32 × count - offset - size < 32.
Proof.
  intros Hoffset Hsize. cbn zeta.
  pose proof (Z.div_pos (offset+size+31) 32 ltac:(lia) ltac:(lia)).
  pose proof (Z.div_mod (offset+size+31) 32 ltac:(lia)).
  pose proof (Z.mod_pos_bound (offset+size+31) 32 ltac:(lia)). lia.
Qed.

Lemma memory_suffix_length offset size :
  0 ≤ offset < 32 → 0 ≤ size < 2^32 →
  let count := (offset + size + 31) / 32 in
  OCamlInt.sub (OCamlInt.sub (OCamlInt.mul count 32) offset) size =
    count × 32 - offset - size.
Proof.
  intros Hoffset Hsize. cbn zeta.
  pose proof (memory_padding_bounds offset size Hoffset Hsize) as Hbounds.
  cbn zeta in Hbounds. unfold OCamlInt.sub, OCamlInt.mul.
  rewrite (native_repr_small (((offset + size + 31) / 32) × 32)) by lia.
  rewrite (native_repr_small (((offset + size + 31) / 32) × 32 - offset)) by lia.
  rewrite native_repr_small by lia. reflexivity.
Qed.

The former unbounded calculation is not valid for arbitrary lengths. This input is not an allocatable OCaml string or a valid EVMC memory size; it demonstrates why the helper needs a bound.
Example memory_word_count_wraps_at_native_limit :
  @OCamlInt.div (Opaque.legacy_witness 0)
    (OCamlInt.add (OCamlInt.add 0 (2^62-1)) 31) 32 < 0.
Proof. vm_compute. reflexivity. Qed.

Library counters use the same wrapping operation as translated OCaml expressions. Starting at the boundary tests this with a one-element list, without constructing a nonrepresentable list.
Example list_length_counter_wraps :
  OCamlList.length_aux (2^62-1) (tt :: nil) = -(2^62).
Proof. vm_compute. reflexivity. Qed.

Example list_index_counter_wraps :
  OCamlList.mapi_shadow1 (2^62-1) (fun index (_ : unit) ⇒ index)
    (tt :: tt :: nil) = (2^62-1) :: -(2^62) :: nil.
Proof. vm_compute. reflexivity. Qed.