Library QedkConcrete.transactiondata

Transaction input length follows from validation

Every calldata byte costs at least four intrinsic gas. A successful validation therefore bounds the VM input length; it need not be supplied as a separate VM contract. This also applies to top-level initcode.
From Stdlib Require Import String ZArith NArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.validation QedkConcrete.nativeindices.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Counts := Byte_string.Bytes.zero_and_nonzero_counts.

This bound is weaker than the supported OCaml platform's string-size limit. Without a native length bound the counters can wrap.
Lemma calldata_token_bounds tx :
  OCamlString.length (T.data tx) < 2^60 →
  0 ≤ Gas.tokens_in_calldata tx ∧
  OCamlString.length (T.data tx) ≤ Gas.tokens_in_calldata tx.
Proof.
  intro HlengthLimit.
  unfold Gas.tokens_in_calldata, Byte_string.Bytes.count_zero_and_nonzero_bytes.
  set (step := fun (counts : Byte_string.Bytes.zero_and_nonzero_counts) (byte : ascii) ⇒
    if Ascii.eqb byte "000"%char
    then Counts.with_zero_bytes (OCamlInt.add (Counts.zero_bytes counts) 1) counts
    else Counts.with_nonzero_bytes (OCamlInt.add (Counts.nonzero_bytes counts) 1) counts).
  assert (Hfold : ∀ bytes counts, 0 ≤ Counts.zero_bytes counts → 0 ≤ Counts.nonzero_bytes counts →
    Counts.zero_bytes counts + Counts.nonzero_bytes counts + OCamlString.length bytes < 2^62 →
    let result := OCamlString.fold_left_structural step counts bytes in
    0 ≤ Counts.zero_bytes result ∧ 0 ≤ Counts.nonzero_bytes result ∧
    Counts.zero_bytes result + Counts.nonzero_bytes result =
      Counts.zero_bytes counts + Counts.nonzero_bytes counts + OCamlString.length bytes).
  { induction bytes as [|byte bytes IH]; intros [zeros nonzeros] Hz Hnz Hlimit.
    - unfold OCamlString.length. cbn [OCamlString.fold_left_structural Counts.zero_bytes Counts.nonzero_bytes Strings.String.length] in ×.
      lia.
    - cbn [Counts.zero_bytes Counts.nonzero_bytes] in Hz, Hnz, Hlimit.
      unfold OCamlString.length in Hlimit. cbn [Strings.String.length] in Hlimit.
      rewrite Nat2Z.inj_succ in Hlimit.
      cbn [OCamlString.fold_left_structural]. unfold step in ×.
      destruct (Ascii.eqb byte "000"%char).
      + cbn [Counts.with_zero_bytes Counts.zero_bytes Counts.nonzero_bytes].
        unfold OCamlInt.add. rewrite (native_repr_small (zeros + 1)) by lia.
        specialize (IH (Counts.Build (zeros + 1) nonzeros) ltac:(cbn; lia) Hnz
          ltac:(cbn; unfold OCamlString.length; lia)).
        unfold OCamlString.length in ×. cbn [Counts.zero_bytes Counts.nonzero_bytes Strings.String.length] in ×.
        cbv [Counts.with_zero_bytes Counts.with_nonzero_bytes Counts.zero_bytes Counts.nonzero_bytes OCamlInt.add] in ×.
        rewrite Nat2Z.inj_succ. lia.
      + cbn [Counts.with_nonzero_bytes Counts.zero_bytes Counts.nonzero_bytes].
        unfold OCamlInt.add. rewrite (native_repr_small (nonzeros + 1)) by lia.
        specialize (IH (Counts.Build zeros (nonzeros + 1)) Hz ltac:(cbn; lia)
          ltac:(cbn; unfold OCamlString.length; lia)).
        unfold OCamlString.length in ×. cbn [Counts.zero_bytes Counts.nonzero_bytes Strings.String.length] in ×.
        cbv [Counts.with_zero_bytes Counts.with_nonzero_bytes Counts.zero_bytes Counts.nonzero_bytes OCamlInt.add] in ×.
        rewrite Nat2Z.inj_succ. lia. }
  pose proof (Hfold (T.data tx) (Counts.Build 0 0) ltac:(cbn; lia) ltac:(cbn; lia)
    ltac:(cbn; lia)) as Hcounts.
  cbn zeta in Hcounts.
  change (let counts := OCamlString.fold_left step (Counts.Build 0 0) (T.data tx) in
    0 ≤ OCamlInt.add (Counts.zero_bytes counts) (OCamlInt.mul 4 (Counts.nonzero_bytes counts)) ∧
    OCamlString.length (T.data tx) ≤ OCamlInt.add (Counts.zero_bytes counts) (OCamlInt.mul 4 (Counts.nonzero_bytes counts))).
  rewrite OCamlString.fold_left_matches_structural.
  cbn zeta. cbn [Counts.zero_bytes Counts.nonzero_bytes] in Hcounts.
  set (counts := OCamlString.fold_left_structural step (Counts.Build 0 0) (T.data tx)) in ×.
  destruct counts as [zeros nonzeros]. cbn [Counts.zero_bytes Counts.nonzero_bytes] in Hcounts |- ×.
  unfold OCamlInt.add, OCamlInt.mul.
  rewrite (native_repr_small (4 × nonzeros)) by lia.
  rewrite (native_repr_small (zeros + 4 × nonzeros)) by lia. lia.
Qed.

Lemma intrinsic_data_length tx :
  OCamlString.length (T.data tx) < 2^60 →
  (Z.to_N (OCamlString.length (T.data tx)) × 4 + 21000 ≤ @Gas.tx_intrinsic_gas tx)%N.
Proof.
  intro HlengthLimit. pose proof (calldata_token_bounds tx HlengthLimit) as [Htokens Hlength].
  assert (Hconvert : @Gas.op_tildedollar (Gas.tokens_in_calldata tx) = Z.to_N (Gas.tokens_in_calldata tx)).
  { change (@Support.UintN.of_z_exn (Gas.tokens_in_calldata tx) = Z.to_N (Gas.tokens_in_calldata tx)).
    unfold Support.UintN.of_z_exn, Support.UintN.of_z_opt, Support.UintN.in_range.
    assert (Hguard : Z.leb 0 (Gas.tokens_in_calldata tx) = true) by (apply Z.leb_le; exact Htokens).
    rewrite Hguard. reflexivity. }
  assert (HlengthN : (Z.to_N (OCamlString.length (T.data tx)) ≤ Z.to_N (Gas.tokens_in_calldata tx))%N)
    by (apply Z2N.inj_le; [unfold OCamlString.length; lia|exact Htokens|exact Hlength]).
  unfold Gas.tx_intrinsic_gas. rewrite Hconvert.
  unfold Gas.op_plus, Gas.op_star, Support.UintN.op_plus, Support.UintN.op_star.
  change (@Gas.tx_calldata_token_gas) with 4%N.
  change (@Gas.tx_base_gas) with 21000%N. lia.
Qed.

Section Validation.
Context {F : E.FArgs}.

Lemma validated_data_length block tx validation :
  OCamlString.length (T.data tx) < 2^60 →
  @E.validate_transaction F block tx = inl validation →
  0 ≤ OCamlString.length (T.data tx) ≤ 7494750.
Proof.
  intros HlengthLimit Hvalidated. pose proof (validated_gas block tx validation Hvalidated) as [Hintrinsic Hlimit].
  assert (Hcost : E.transaction_validation.intrinsic_gas validation =
    @Gas.tx_intrinsic_gas tx).
  { unfold E.validate_transaction in Hvalidated. cbn zeta in Hvalidated.
    do 13 (apply validation.bind_success in Hvalidated;
      let step := fresh "step" in let Hstep := fresh "Hstep" in
      destruct Hvalidated as [step [Hstep Hvalidated]];
      cbn beta iota zeta in Hvalidated).
    injection Hvalidated as <-. reflexivity. }
  rewrite Hcost in Hintrinsic.
  pose proof (@intrinsic_data_length tx HlengthLimit) as Hdata.
  apply N2Z.inj_le in Hdata. apply N2Z.inj_le in Hintrinsic. apply N2Z.inj_le in Hlimit.
  rewrite N2Z.inj_add, N2Z.inj_mul, Z2N.id in Hdata by (unfold OCamlString.length; lia).
  change (OCamlString.length (T.data tx) × 4 + 21000 ≤
    Z.of_N (@Gas.tx_intrinsic_gas tx)) in Hdata.
  split; [unfold OCamlString.length; lia|lia].
Qed.

Lemma validated_message_data_length block tx validation world :
  OCamlString.length (T.data tx) < 2^60 →
  @E.validate_transaction F block tx = inl validation →
  let message := @E.prepare_message F (E.transaction_validation.sender validation)
    (@Gas.op_minus (T.gas_limit tx) (E.transaction_validation.intrinsic_gas validation)) tx world in
  0 ≤ OCamlString.length (Evmc.Message.t.input_data message) ≤ 7494750.
Proof.
  intros HlengthLimit Hvalidated. pose proof (validated_data_length block tx validation HlengthLimit Hvalidated) as Hlength.
  cbn zeta. unfold E.prepare_message, T.call_or_create_value.
  destruct (T.to_ tx); exact Hlength.
Qed.
End Validation.

Print Assumptions validated_message_data_length.