Library QedkConcrete.bytewords

Concrete byte decoding produces bounded words

The bounds depend on byte-string length, not byte contents. This covers untrusted calldata and PUSH immediates, including zero padding beyond the input. Checked conversions are proved not to reach their fallback.
From Stdlib Require Import String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Numeric QedkConcrete.wordrange.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Lemma bits_range bytes :
  0 ≤ OCamlZ.of_bits bytes < 2^(8 × OCamlString.length bytes).
Proof.
  induction bytes as [|byte rest IH].
  - change (0 ≤ 0 < 1). lia.
  - change (0 ≤ Z.of_N (Ascii.N_of_ascii byte) + 256 × OCamlZ.of_bits rest <
      2^(8 × Z.of_nat (S (Strings.String.length rest)))).
    rewrite Nat2Z.inj_succ.
    replace (8 × Z.succ (Z.of_nat (Strings.String.length rest))) with
      (8 × Z.of_nat (Strings.String.length rest) + 8) by lia.
    rewrite Z.pow_add_r by lia.
    pose proof (Ascii.N_ascii_bounded byte).
    change (0 ≤ OCamlZ.of_bits rest < 2^(8 × Z.of_nat (Strings.String.length rest))) in IH.
    nia.
Qed.

Lemma mapi_length f bytes :
  OCamlString.length (OCamlString.mapi f bytes) = OCamlString.length bytes.
Proof.
  unfold OCamlString.mapi, OCamlString.length. generalize 0 as index.
  induction bytes; intros index; cbn [OCamlString.mapi_from Strings.String.length]; [reflexivity|].
  rewrite !Nat2Z.inj_succ, IHbytes. reflexivity.
Qed.

Lemma reverse_length bytes :
  OCamlString.length (@Byte_string.Bytes.reverse bytes) = OCamlString.length bytes.
Proof. apply mapi_length. Qed.

Lemma uint_reverse_length bytes :
  OCamlString.length (Support.UintN.reverse bytes) = OCamlString.length bytes.
Proof.
  assert (Happend : ∀ suffix,
    Strings.String.length (Support.UintN.reverse_append bytes suffix) =
      (Strings.String.length bytes + Strings.String.length suffix)%nat).
  { induction bytes; intros suffix; cbn [Support.UintN.reverse_append Strings.String.length]; [reflexivity|].
    rewrite IHbytes. cbn [Strings.String.length]. lia. }
  unfold Support.UintN.reverse, OCamlString.length. rewrite Happend. cbn [Strings.String.length]. f_equal. lia.
Qed.

Lemma bits_word bytes : OCamlString.length bytes ≤ 32 → wordRange (OCamlZ.of_bits bytes).
Proof.
  intro Hlength. pose proof (bits_range bytes) as [Hlo Hhi].
  unfold wordRange. change (0 ≤ OCamlZ.of_bits bytes < 2^256).
  split; [exact Hlo|]. eapply Z.lt_le_trans; [exact Hhi|].
  apply Z.pow_le_mono_r; unfold OCamlString.length in *; lia.
Qed.

Lemma word_repr bytes :
  OCamlString.length bytes ≤ 32 → wordRange (@U.of_repr bytes).
Proof.
  intro Hlength.
  change (wordRange (@U.of_z_exn (OCamlZ.of_bits (@Byte_string.Bytes.reverse bytes)))).
  apply word_of_z_range, bits_word. rewrite reverse_length. exact Hlength.
Qed.

Lemma word_bytes bytes :
  OCamlString.length bytes ≤ 32 → wordRange (@U.of_bytes_be_exn bytes).
Proof.
  intro Hlength.
  change (wordRange (@U.of_z_exn
    (Z.of_N (Z.to_N (OCamlZ.of_bits (Support.UintN.reverse bytes)))))).
  rewrite Z2N.id by (exact (proj1 (bits_range _))).
  apply word_of_z_range, bits_word. rewrite uint_reverse_length. exact Hlength.
Qed.

Lemma init_length {US : Basics.Unreachable string} count f : 0 ≤ count →
  OCamlString.length (@OCamlString.init US count f) = count.
Proof.
  intro Hcount.
  assert (Hnat : ∀ n index, Strings.String.length (OCamlString.init_nat n index f) = n).
  { induction n; intro index; cbn [OCamlString.init_nat Strings.String.length]; [reflexivity|].
    rewrite IHn. reflexivity. }
  rewrite OCamlString.init_matches_structural. assert (Htest : Z.ltb count 0 = false) by (apply Z.ltb_ge; exact Hcount).
  rewrite Htest. unfold OCamlString.length. rewrite Hnat, Z2Nat.id by exact Hcount. reflexivity.
Qed.

Lemma padded_length
    bytes offset count : 0 ≤ count →
  OCamlString.length (@Byte_string.Bytes.sub_with_zero_padding bytes offset count) = count.
Proof. apply init_length. Qed.

Lemma padded32_length bytes offset :
  OCamlString.length
    (@Byte_string.Fixed.Fixed_result.sub_with_zero_padding _ _ _ Byte_string.B32 bytes offset) = 32.
Proof. apply init_length. lia. Qed.

Lemma padded_word bytes offset :
  wordRange (@U.of_repr (@Byte_string.Fixed.Fixed_result.sub_with_zero_padding _ _ _ Byte_string.B32 bytes offset)).
Proof. apply word_repr. rewrite padded32_length. lia. Qed.

Lemma b32_checked_identity bytes :
  OCamlString.length bytes = 32 →
  @Byte_string.Fixed.Fixed_result.of_bytes_exn _ _ _ Byte_string.B32 bytes = bytes.
Proof.
  intro Hlength.
  cbn -[OCamlString.length].
  unfold Byte_string.Fixed.of_bytes_exn, Byte_string.Fixed.of_bytes.
  cbn -[OCamlString.length].
  unfold Byte_string.Fixed.Impl.of_bytes.
  rewrite Hlength. reflexivity.
Qed.

Lemma b32_zeros_length :
  OCamlString.length (@Byte_string.Fixed.Fixed_result.zeros _ _ _ Byte_string.B32) = 32.
Proof. apply init_length. lia. Qed.

Lemma b32_sub_length bytes offset :
  0 ≤ offset → offset + 32 ≤ OCamlString.length bytes →
  OCamlString.length (@Byte_string.Fixed.Fixed_result.sub _ _ _ Byte_string.B32 bytes offset) = 32.
Proof.
  intros Hoffset Hbound.
  pose (substringFallback := Opaque.legacy_witness
    (@Byte_string.Fixed._rocq_fallback_sub_0 Byte_string.B32_fargs bytes)).
  change (OCamlString.length
    (@Byte_string.Fixed.Fixed_result.of_bytes_exn _ _ _ Byte_string.B32
      (@OCamlString.sub substringFallback bytes offset 32)) = 32).
  rewrite b32_checked_identity by (apply StringBounds.sub_length_in_bounds; lia).
  apply StringBounds.sub_length_in_bounds; lia.
Qed.

Lemma word_encoding_length value :
  OCamlString.length (@U.to_repr value) = 32.
Proof. apply init_length. lia. Qed.

Lemma word_bytes_encoding_length value :
  OCamlString.length (@U.to_repr_bytes value) = 32.
Proof. apply init_length. lia. Qed.

Lemma byte_append_length left right :
  OCamlString.length (Strings.String.append left right) =
    OCamlString.length left + OCamlString.length right.
Proof.
  unfold OCamlString.length. induction left; cbn [Strings.String.append Strings.String.length]; [lia|].
  rewrite !Nat2Z.inj_succ, IHleft. lia.
Qed.

Lemma concat32_length chunks :
  List.Forall (fun bytes ⇒ OCamlString.length bytes = 32) chunks →
  OCamlString.length (OCamlString.concat EmptyString chunks) = 32 × Z.of_nat (Lists.List.length chunks).
Proof.
  intro Hchunks. induction Hchunks as [|chunk rest Hchunk Hrest IH].
  - reflexivity.
  - destruct rest as [|next rest].
    + cbn [OCamlString.concat Lists.List.length]. rewrite Hchunk. reflexivity.
    + change (OCamlString.length (Strings.String.append chunk
        (OCamlString.concat EmptyString (next :: rest))) =
        32 × Z.of_nat (S (Lists.List.length (next :: rest)))).
      rewrite byte_append_length, Hchunk, IH, Nat2Z.inj_succ. lia.
Qed.

Print Assumptions word_repr.
Print Assumptions word_bytes.
Print Assumptions padded_word.