Library QedkConcrete.nativeencoding

Bounds for variable-width native encodings

Unlike the EVM word serializer, Zarith's byte encoder is not fixed-width. The native precompiles must bound the integer before zero-padding it.
From Stdlib Require Import String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Support.UintN QedkConcrete.bytewords.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Lemma to_bits_bytes_length count value :
  OCamlString.length (OCamlZ.to_bits_bytes count value) = Z.of_nat count.
Proof.
  revert value. induction count; intro value; [reflexivity|].
  change (Z.of_nat (S (Strings.String.length (OCamlZ.to_bits_bytes count (value / 256)))) =
    Z.of_nat (S count)).
  rewrite !Nat2Z.inj_succ. unfold OCamlString.length in IHcount. rewrite IHcount. reflexivity.
Qed.

Raw Zarith output includes up to seven padding bytes. The former unpadded bound is false: to_bits 1 has length eight, not one.
Lemma to_bits_length_bound value count :
  0 ≤ count → 0 ≤ value < 2^(8×count) →
  OCamlString.length (OCamlZ.to_bits value) ≤ count + 7.
Proof.
  intros Hcount Hvalue. unfold OCamlZ.to_bits.
  rewrite to_bits_bytes_length, Nat2Z.inj_mul.
  change (8 × Z.of_nat (OCamlZ.limb_count value) ≤ count + 7).
  unfold OCamlZ.limb_count, OCamlZ.numbits.
  destruct (Z.eqb_spec value 0) as [->|Hnonzero]; [cbn; lia|].
  rewrite Z.abs_eq by lia.
  pose proof (Z.log2_nonneg value).
  pose proof (proj1 (Z.log2_lt_pow2 value (8×count) ltac:(lia)) (proj2 Hvalue)).
  rewrite Z2Nat.id by (apply Z.div_pos; lia).
  pose proof (Z.div_mod (Z.succ (Z.log2 value) + 63) 64 ltac:(lia)).
  pose proof (Z.mod_pos_bound (Z.succ (Z.log2 value) + 63) 64 ltac:(lia)).
  lia.
Qed.

Example to_bits_one_has_limb_padding : OCamlString.length (OCamlZ.to_bits 1) = 8.
Proof. vm_compute. reflexivity. Qed.

The bound is for the significant-byte encoding. Raw Zarith output is padded to 64-bit limbs; e.g. encoding 1 there takes eight bytes.
Lemma minimal_bits_length_bound value count :
  0 ≤ count → 0 ≤ value < 2^(8×count) →
  OCamlString.length (OCamlZ.to_bits_minimal value) ≤ count.
Proof.
  intros Hcount Hvalue. unfold OCamlZ.to_bits_minimal.
  rewrite Z.abs_eq by lia. rewrite to_bits_bytes_length.
  unfold OCamlZ.numbits. destruct (Z.eqb_spec value 0) as [->|Hnonzero].
  - cbn. lia.
  - rewrite Z.abs_eq by lia.
    pose proof (Z.log2_nonneg value).
    pose proof (proj1 (Z.log2_lt_pow2 value (8×count) ltac:(lia)) (proj2 Hvalue)).
    rewrite Z2Nat.id by (apply Z.div_pos; lia).
    assert ((Z.succ (Z.log2 value) + 7) / 8 < count + 1)
      by (apply Z.div_lt_upper_bound; lia). lia.
Qed.

Lemma uint_encoding_length_bound {US : Basics.Unreachable string}
    {UC : Basics.Unreachable ascii} value count :
  0 ≤ count → Z.of_N value < 2^(8×count) →
  OCamlString.length (@Support.UintN.to_bytes_be value) ≤ count.
Proof.
  intros Hcount Hvalue. change
    (OCamlString.length (Support.UintN.reverse (OCamlZ.to_bits_minimal (Z.of_N value))) ≤ count).
  rewrite uint_reverse_length. apply minimal_bits_length_bound; lia.
Qed.

Lemma uint_decoding_bound {UC : Basics.Unreachable ascii}
    {UN : Basics.Unreachable Support.UintN.t} bytes :
  Z.of_N (@Support.UintN.of_bytes_be bytes) < 2^(8×OCamlString.length bytes).
Proof.
  change (Z.of_N (Z.to_N (OCamlZ.of_bits (Support.UintN.reverse bytes))) <
    2^(8×OCamlString.length bytes)).
  rewrite Z2N.id by (exact (proj1 (bits_range _))).
  pose proof (bits_range (Support.UintN.reverse bytes)). rewrite uint_reverse_length in H. lia.
Qed.

Lemma powm_bound {UZ : Basics.Unreachable Z} base exponent modulus :
  0 ≤ exponent → 0 < modulus →
  0 ≤ @OCamlZ.powm UZ base exponent modulus < modulus.
Proof.
  intros Hexponent Hmodulus. unfold OCamlZ.powm.
  rewrite Z.abs_eq by lia.
  assert (Hnonzero : Z.eqb modulus 0 = false) by (apply Z.eqb_neq; lia).
  rewrite Hnonzero. destruct exponent as [|p|p]; [|destruct p|lia];
    apply Z.mod_pos_bound; exact Hmodulus.
Qed.