Library QedkConcrete.vmchildsizes

Sizes passed across recursive VM calls

Child memory is the parent's unused capacity, not a fresh independent allocation. Its uint32 encoding is exact under the machine invariant. Input bytes come from a memory read after the corresponding extension succeeds. These bounds are derived at the caller, not assumed of the VM.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vmsizes QedkConcrete.memorysizes
  QedkConcrete.vmreturnsizes QedkConcrete.vmmachinewords QedkConcrete.memorywords
  QedkConcrete.wordrange QedkConcrete.memorycomposition.
Require Import QedkConcrete.memorymodel.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Lemma uint32_roundtrip {UN : Basics.Unreachable Support.UintN.t}
    {UI : Basics.Unreachable OCamlInt32.t} value :
  Z.of_N value < 2^32 →
  @Support.UintN.of_uint32 (@Support.UintN.to_uint32 value) = value.
Proof.
  intro Hvalue. unfold Support.UintN.to_uint32, Support.UintN.of_uint32,
    Support.UintN.to_z, OCamlZ.to_int32_unsigned.
  rewrite (proj2 (Z.leb_le _ _) (N2Z.is_nonneg value)),
    (proj2 (Z.ltb_lt _ _) Hvalue). cbn [andb].
  unfold OCamlZ.signed_repr. rewrite Z.mod_small by lia.
  cbn zeta. destruct (Z.leb (2^Z.pred 32) (Z.of_N value)) eqn:Hsign.
  - apply Z.leb_le in Hsign. unfold OCamlZ.of_int32_unsigned.
    rewrite (proj2 (Z.ltb_lt (Z.of_N value - 2^32) 0) ltac:(lia)).
    replace (Z.of_N value - 2^32 + 2^32) with (Z.of_N value) by lia.
    unfold Support.UintN.of_z_exn. rewrite Support.UintN.of_z_opt_nonnegative by lia.
    apply N2Z.id.
  - unfold OCamlZ.of_int32_unsigned.
    rewrite (proj2 (Z.ltb_ge _ _) (N2Z.is_nonneg value)).
    unfold Support.UintN.of_z_exn. rewrite Support.UintN.of_z_opt_nonnegative by lia.
    apply N2Z.id.
Qed.

Section Child.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context {UN : Basics.Unreachable Support.UintN.t} {UF : Basics.Unreachable U.t}
  {UG : Basics.Unreachable Gas.t} {UI32 : Basics.Unreachable OCamlInt32.t}
  {UA : Basics.Unreachable (@Mem.t Host_t F → 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))}.

Lemma encoded_child_capacity_bounded_bounded memory :
  BoundedMemory F →
  memoryFits memory →
  let encoded := @Support.UintN.to_uint32 (@Mem.available_memory_size Host_t F memory) in
  let decoded := @Support.UintN.of_uint32 encoded in
  decoded = (Mem.t.memory_capacity memory - Mem.t.active_bytes memory)%N ∧
  (decoded ≤ Mem.t.memory_capacity memory)%N ∧ Z.of_N decoded < 2^32.
Proof.
  intros Hrevision [Hactive Hcapacity].
  pose proof (@available_nine_memory_bounded_bounded Host_t F UA memory Hrevision Hactive) as Hbound.
  cbn zeta. rewrite uint32_roundtrip by lia.
  split; [|split; [exact Hbound|lia]].
  rewrite (bounded_available F Hrevision).
  unfold Support.UintN.op_minus. rewrite (proj2 (N.leb_le _ _) Hactive). reflexivity.
Qed.

Lemma encoded_child_capacity_bounded memory :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  memoryFits memory →
  let encoded := @Support.UintN.to_uint32 (@Mem.available_memory_size Host_t F memory) in
  let decoded := @Support.UintN.of_uint32 encoded in
  decoded = (Mem.t.memory_capacity memory - Mem.t.active_bytes memory)%N ∧
  (decoded ≤ Mem.t.memory_capacity memory)%N ∧ Z.of_N decoded < 2^32.
Proof. intros; eapply encoded_child_capacity_bounded_bounded with (memory := memory); eauto using eleven_bounded_memory. Qed.

Lemma extended_input_size_bounded_bounded start size state gas after :
  BoundedMemory F →
  0 ≤ U.to_z start → 0 ≤ U.to_z size → machineByteSizes state →
  @V.Executor.extend_memory_to Host_t F E start size state = (inl gas, after) →
  0 ≤ U.to_z size < 2^32.
Proof.
  intros Hrevision Hstart Hsize [[Hactive Hcapacity] Houtput] Hresult.
  pose proof (@extend_memory_nine_shape_bounded Host_t F E UF start size state Hrevision) as Hshape.
  rewrite Hresult in Hshape. cbn [fst snd] in Hshape.
  destruct Hshape as [_ [_ [_ Hextended]]].
  pose proof (extend_nine_read_size start size
    (MS.t.memory state) (MS.t.memory after) Hstart Hsize Hextended) as [Hbound _].
  destruct Hbound; lia.
Qed.

Lemma extended_input_size_bounded start size state gas after :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  0 ≤ U.to_z start → 0 ≤ U.to_z size → machineByteSizes state →
  @V.Executor.extend_memory_to Host_t F E start size state = (inl gas, after) →
  0 ≤ U.to_z size < 2^32.
Proof. intros; eapply extended_input_size_bounded_bounded with (start := start) (size := size) (state := state) (gas := gas) (after := after); eauto using eleven_bounded_memory. Qed.

Context {US : Basics.Unreachable string} {UU : Basics.Unreachable unit}
  {UL : Basics.Unreachable (list string)} {UI : Basics.Unreachable int}
  {UZ : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.

The extent bound remains useful after further memory operations: the size of a read depends on the requested length, not on the stored bytes.
Lemma read_small_input_bounded start size memory :
  0 ≤ U.to_z size < 2^32 → memoryWordChunks memory →
  OCamlString.length (@V.Memory.Memory_signature.read_block_at Host_t F
    (@Mem.t Host_t F) (@V.Memory Host_t F) start size memory) < 2^32 ∨
  badMemoryInitComparison.
Proof.
  intros Hsize Hwords.
  cbv beta iota zeta delta [V.Memory V.Memory.module V.Memory.Memory_signature.read_block_at].
  lazymatch goal with
  | |- context [@Mem.read_block_at ?H ?F ?start ?size ?mem] ⇒
    destruct (read_block_length_or_bad_comparison start size mem ltac:(lia) Hwords) as [Hlength|Hfailure]
  end.
  - left. rewrite Hlength. exact (proj2 Hsize).
  - right. exact Hfailure.
Qed.

Lemma extended_input_data_bounded_bounded start size state gas after :
  BoundedMemory F →
  0 ≤ U.to_z start → 0 ≤ U.to_z size →
  machineByteSizes state → memoryWordChunks (MS.t.memory state) →
  @V.Executor.extend_memory_to Host_t F E start size state = (inl gas, after) →
  OCamlString.length (@V.Memory.Memory_signature.read_block_at Host_t F
    (@Mem.t Host_t F) (@V.Memory Host_t F)
    start size (MS.t.memory after)) < 2^32 ∨ badMemoryInitComparison.
Proof.
  intros Hrevision Hstart Hsize [[Hactive Hcapacity] Houtput] Hwords Hresult.
  destruct (@extended_memory_read_bounded_bounded Host_t F E UF UG UI start size state gas after Hrevision Hstart Hsize ltac:(lia) Hwords Hresult)
    as [[Hlength Hbound]|Hfailure].
  - left. rewrite Hlength. lia.
  - right. exact Hfailure.
Qed.

Lemma extended_input_data_bounded start size state gas after :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  0 ≤ U.to_z start → 0 ≤ U.to_z size →
  machineByteSizes state → memoryWordChunks (MS.t.memory state) →
  @V.Executor.extend_memory_to Host_t F E start size state = (inl gas, after) →
  OCamlString.length (@V.Memory.Memory_signature.read_block_at Host_t F
    (@Mem.t Host_t F) (@V.Memory Host_t F)
    start size (MS.t.memory after)) < 2^32 ∨ badMemoryInitComparison.
Proof. intros; eapply extended_input_data_bounded_bounded with (start := start) (size := size) (state := state) (gas := gas) (after := after); eauto using eleven_bounded_memory. Qed.
End Child.

Print Assumptions uint32_roundtrip.
Print Assumptions encoded_child_capacity_bounded.
Print Assumptions extended_input_size_bounded.
Print Assumptions extended_input_data_bounded.