Library QedkConcrete.wordrange

Ranges of concrete EVM words

U256 and I256 are represented by integers in the generated code. Their arithmetic operations truncate, whereas checked conversions require a range proof to exclude the unreachable fallback. These lemmas establish those facts from the implementations, without axioms about arithmetic.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Numeric.
Require Import MonadExecutionSpecs.Refinement.VmPcConversion.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Module U := Numeric.U256.
Module I := Numeric.I256.

Definition wordRange (value : U.t) : Prop := 0 ≤ U.to_z value < 2^256.
Definition signedWordRange (value : I.t) : Prop := -(2^255) ≤ I.to_z value < 2^255.

Lemma low_bits_range value :
  0 ≤ Z.land (Z.shiftl value 0) (Z.ones 256) < 2^256.
Proof.
  rewrite Z.shiftl_0_r, Z.land_ones by lia.
  apply Z.mod_pos_bound. lia.
Qed.

Section Unsigned.
Context {fallback : Basics.Unreachable U.t}.

Lemma word_after_op value : wordRange (@U.of_z_after_op value).
Proof. apply low_bits_range. Qed.

Lemma word_of_z value : wordRange value → @U.of_z_exn value = value.
Proof.
  intros [Hlo Hhi].
  let expression := eval hnf in (U.of_z_exn value) in
  lazymatch expression with
  | match _ with Some _ ⇒ _ | None ⇒ ?default end ⇒
      change ((match (if negb (Z.geb value 0 && Z.leb value (2^256-1))
        then None else Some value) with Some z ⇒ z | None ⇒ default end) = value)
  end.
  assert (Hlo' : Z.geb value 0 = true) by (apply Z.geb_le; exact Hlo).
  assert (Hhi' : Z.leb value (2^256-1) = true).
  { apply Z.leb_le. change (value ≤ Z.pred (2^256)). apply Z.lt_le_pred. exact Hhi. }
  rewrite Hlo', Hhi'. reflexivity.
Qed.

Lemma word_of_z_range value : wordRange value → wordRange (@U.of_z_exn value).
Proof. intro Hrange. rewrite word_of_z by exact Hrange. exact Hrange. Qed.

Lemma word_zero : wordRange (@U.zero).
Proof. change (0 ≤ 0 < 2^256). lia. Qed.

Lemma word_one : wordRange (@U.one).
Proof. change (0 ≤ 1 < 2^256). lia. Qed.

Lemma word_bool value : wordRange (@U.of_bool value).
Proof. destruct value; [exact word_one|exact word_zero]. Qed.

Lemma word_int value : 0 ≤ value < 2^256 → wordRange (@U.of_int value).
Proof. apply word_of_z_range. Qed.

Lemma word_lift1 f value : wordRange (@U.lift_1 f value).
Proof. apply word_after_op. Qed.

Lemma word_lift2 f left right : wordRange (@U.lift_2 f left right).
Proof. apply word_after_op. Qed.

Lemma word_add left right : wordRange (@U.op_plus left right).
Proof. apply word_lift2. Qed.

Lemma word_sub left right : wordRange (@U.op_minus left right).
Proof. apply word_lift2. Qed.

Lemma word_mul left right : wordRange (@U.op_star left right).
Proof. apply word_lift2. Qed.

Lemma word_div left right : wordRange (@U.op_div left right).
Proof. apply word_lift2. Qed.

Lemma word_rem left right : wordRange (@U.rem left right).
Proof. apply word_lift2. Qed.

Lemma word_and left right : wordRange (@U.logand left right).
Proof. apply word_lift2. Qed.

Lemma word_or left right : wordRange (@U.logor left right).
Proof. apply word_lift2. Qed.

Lemma word_xor left right : wordRange (@U.logxor left right).
Proof. apply word_lift2. Qed.

Lemma word_not value : wordRange (@U.lognot value).
Proof. apply word_lift1. Qed.

Lemma word_addmod left right modulus : wordRange (@U.addmod left right modulus).
Proof. apply word_after_op. Qed.

Lemma word_mulmod left right modulus : wordRange (@U.mulmod left right modulus).
Proof. apply word_after_op. Qed.

Lemma word_shift_left value shift : wordRange (@U.shift_left value shift).
Proof. apply word_after_op. Qed.

Lemma word_shift_right value shift : wordRange (@U.shift_right value shift).
Proof. apply word_after_op. Qed.
End Unsigned.

Lemma signed_extract_range {fallback : Basics.Unreachable Z} value width :
  0 < width →
  -(2^(width-1)) ≤ @OCamlZ.signed_extract fallback value 0 width < 2^(width-1).
Proof.
  intro Hwidth.
  assert (Htest : Z.leb width 0 = false) by (apply Z.leb_gt; exact Hwidth).
  unfold OCamlZ.signed_extract, OCamlZ.extract. rewrite Htest.
  cbn [Z.ltb orb]. rewrite Z.shiftr_0_r, Z.shiftl_1_l by lia.
  replace (Z.pred (2^width)) with (Z.ones width) by (rewrite Z.ones_equiv; lia).
  rewrite Z.land_ones by lia.
  set (x := value mod 2^width).
  assert (Hrange : 0 ≤ x < 2^width) by (apply Z.mod_pos_bound; apply Z.pow_pos_nonneg; lia).
  assert (Hhalf : 0 < 2^(width-1)) by (apply Z.pow_pos_nonneg; lia).
  assert (Hpow : 2^width = 2 × 2^(width-1)).
  { replace width with ((width-1)+1) at 1 by lia. rewrite Z.pow_add_r by lia. ring. }
  assert (Hdiv : 0 ≤ x / 2^(width-1) < 2).
  { split.
    - apply Z.div_pos; lia.
    - apply Z.div_lt_upper_bound; lia. }
  pose proof (Z.div_mod x (2^(width-1)) ltac:(lia)) as Hdecomp.
  pose proof (Z.mod_pos_bound x (2^(width-1)) Hhalf) as Hrem.
  replace (Z.pred width) with (width-1) by lia.
  change (-(2^(width-1)) ≤
    (if Z.testbit x (width-1) then x-2^width else x) < 2^(width-1)).
  destruct (Z.testbit x (width-1)) eqn:Hbit.
  - apply Z.testbit_true in Hbit; [|lia]. rewrite Z.mod_small in Hbit by exact Hdiv.
    rewrite Hpow. nia.
  - apply Z.testbit_false in Hbit; [|lia]. rewrite Z.mod_small in Hbit by exact Hdiv.
    nia.
Qed.

Section Signed.
Context {fallback : Basics.Unreachable I.t}.

Lemma signed_after_op value : signedWordRange (@I.of_z_after_op value).
Proof.
  unfold signedWordRange, I.of_z_after_op. cbn.
  exact (@signed_extract_range _ value 256 ltac:(lia)).
Qed.

Lemma signed_of_z value : signedWordRange value → @I.of_z_exn value = value.
Proof.
  intros [Hlo Hhi]. change (-(2^255) ≤ value) in Hlo. change (value < 2^255) in Hhi.
  let expression := eval hnf in (I.of_z_exn value) in
  lazymatch expression with
  | match _ with Some _ ⇒ _ | None ⇒ ?default end ⇒
      change ((match (if negb (Z.geb value (-(2^255)) && Z.leb value (2^255-1))
        then None else Some value) with Some z ⇒ z | None ⇒ default end) = value)
  end.
  assert (Hlo' : Z.geb value (-(2^255)) = true) by (apply Z.geb_le; lia).
  assert (Hhi' : Z.leb value (2^255-1) = true) by (apply Z.leb_le; lia).
  rewrite Hlo', Hhi'. reflexivity.
Qed.

Lemma signed_zero : signedWordRange (@I.zero).
Proof. change (-(2^255) ≤ 0 < 2^255). lia. Qed.

Lemma signed_minus_one : signedWordRange (@I.of_int (-1)).
Proof. change (-(2^255) ≤ -1 < 2^255). lia. Qed.

Lemma signed_div left right : signedWordRange (@I.op_div left right).
Proof. apply signed_after_op. Qed.

Lemma signed_rem left right : signedWordRange (@I.rem left right).
Proof. apply signed_after_op. Qed.

Lemma signed_shift_right value shift : signedWordRange (@I.shift_right value shift).
Proof. apply signed_after_op. Qed.
End Signed.

Lemma word_as_unsigned {fallback : U.t} value :
  signedWordRange value → wordRange (@I.as_unsigned fallback value).
Proof.
  intros [Hlo Hhi]. change (-(2^255) ≤ value) in Hlo. change (value < 2^255) in Hhi.
  change (wordRange (U.of_z_exn
    (if Z.geb value 0 then value else (2^256-1)+value+1))).
  apply word_of_z_range.
  change (0 ≤ (if Z.geb value 0 then value else (2^256-1)+value+1) < 2^256).
  destruct (Z.geb value 0) eqn:Hpositive.
  - apply Z.geb_le in Hpositive. lia.
  - rewrite Z.geb_leb in Hpositive. apply Z.leb_gt in Hpositive. lia.
Qed.

Lemma word_as_signed {SF : I.t} value :
  wordRange value → signedWordRange (@U.as_signed SF value).
Proof.
  intros [Hlo Hhi]. change (0 ≤ value) in Hlo. change (value < 2^256) in Hhi.
  change (signedWordRange (I.of_z_exn
    (if Z.leb value (2^255-1) then value else value-(2^256-1)-1))).
  assert (Hrange : signedWordRange
    (if Z.leb value (2^255-1) then value else value-(2^256-1)-1)).
  { change (-(2^255) ≤
      (if Z.leb value (2^255-1) then value else value-(2^256-1)-1) < 2^255).
    destruct (Z.leb value (2^255-1)) eqn:Hsmall.
    - apply Z.leb_le in Hsmall. lia.
    - apply Z.leb_gt in Hsmall. lia. }
  rewrite signed_of_z by exact Hrange. exact Hrange.
Qed.

Lemma word_byte {fallback : Basics.Unreachable U.t} value :
  wordRange (@U.of_byte value).
Proof.
  change (wordRange (@U.of_z_exn (Z.of_N (Ascii.N_of_ascii value)))).
  apply word_of_z_range. pose proof (Ascii.N_ascii_bounded value).
  change (0 ≤ Z.of_N (Ascii.N_of_ascii value) < 2^256). lia.
Qed.

Lemma word_exp {UZ : Basics.Unreachable Z.t} {Uunit : Basics.Unreachable unit}
    {Upair : Basics.Unreachable (int × int)} {UF : Basics.Unreachable U.t} base exponent :
  wordRange (@U.exp base exponent).
Proof. apply word_after_op. Qed.

Lemma signed_sign_extend byteIndex value :
  0 ≤ byteIndex < 32 → signedWordRange (@U.sign_extend byteIndex value).
Proof.
  intro Hindex.
  let expression := eval cbn in (U.sign_extend byteIndex value) in
  lazymatch expression with
  | context [@OCamlZ.signed_extract ?witness _ _ _] ⇒ pose (UZ := witness)
  end.
  change (signedWordRange (@I.of_z_exn
    (@OCamlZ.signed_extract UZ value 0
      (OCamlInt.add 1 (OCamlInt.add 7 (OCamlInt.mul 8 byteIndex)))))).
  assert (Hmachine : OCamlInt.min_int ≤ 0 ∧ 256 ≤ OCamlInt.max_int)
    by (vm_compute; split; discriminate).
  assert (Hmul : OCamlInt.mul 8 byteIndex = 8 × byteIndex).
  { unfold OCamlInt.mul. apply OCamlInt.repr_small. lia. }
  rewrite Hmul.
  rewrite (OCamlInt.add_no_overflow 7 (8 × byteIndex)) by lia.
  rewrite (OCamlInt.add_no_overflow 1 (7 + 8 × byteIndex)) by lia.
  pose proof (@signed_extract_range UZ value (1+(7+8×byteIndex)) ltac:(lia)) as Hrange.
  assert (Hpower : 2^((1+(7+8×byteIndex))-1) ≤ 2^255).
  { apply Z.pow_le_mono_r; lia. }
  assert (Hsigned : signedWordRange (@OCamlZ.signed_extract UZ value 0 (1+(7+8×byteIndex)))).
  { unfold signedWordRange. change (-(2^255) ≤
      @OCamlZ.signed_extract UZ value 0 (1+(7+8×byteIndex)) < 2^255). lia. }
  rewrite signed_of_z by exact Hsigned. exact Hsigned.
Qed.

Lemma word_significant_bits value : wordRange value → 0 ≤ U.significant_bits value ≤ 256.
Proof.
  intros [Hlo Hhi]. change (0 ≤ value) in Hlo. change (value < 2^256) in Hhi.
  change (0 ≤ (if Z.eqb value 0 then 0 else Z.succ (Z.log2 (Z.abs value))) ≤ 256).
  rewrite Z.abs_eq by exact Hlo. destruct (Z.eqb value 0) eqn:Hzero; [lia|].
  apply Z.eqb_neq in Hzero.
  pose proof (Z.log2_nonneg value).
  assert (Z.log2 value < 256) by (apply Z.log2_lt_pow2; lia).
  lia.
Qed.

Print Assumptions word_as_signed.
Print Assumptions signed_sign_extend.

Lemma word_to_int_opt value converted :
  U.to_int_opt value = Some converted → converted = U.to_z value.
Proof.
  rewrite (@generated_u256_to_int_opt_eq UintN.primitive_int).
  unfold pc_to_int_opt, OCamlZ.to_int, OCamlZ.checked_signed.
  unfold OCamlZ.fits_int.
  destruct (OCamlZ.fits_signed 63 value); intro Hconvert; inversion Hconvert. reflexivity.
Qed.