Library QedkConcrete.bytecodec

Word serialization is lossless

Reserve records use the VM's actual big-endian encoding for both keys and values. Length bounds alone do not justify reading back a stored threshold: this file proves the byte contents, without a serialization axiom.
From Stdlib Require Import String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml QedkConcrete.wordrange QedkConcrete.bytewords.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Lemma init_nat_get n start f index : (index < n)%nat →
  Strings.String.get index (OCamlString.init_nat n start f) =
    Some (f (start + Z.of_nat index)).
Proof.
  revert start index. induction n as [|n IH]; intros start [|index] Hindex; try lia.
  - cbn [OCamlString.init_nat Strings.String.get]. f_equal. f_equal. lia.
  - cbn [OCamlString.init_nat Strings.String.get]. rewrite IH by lia.
    rewrite Nat2Z.inj_succ. f_equal. f_equal. lia.
Qed.

Lemma init_get {US : Basics.Unreachable string} {UC : Basics.Unreachable ascii}
    count f index : 0 ≤ index < count →
  @OCamlString.get UC (@OCamlString.init US count f) index = f index.
Proof.
  intro Hindex. rewrite OCamlString.init_matches_structural. unfold OCamlString.get.
  assert (Hcount : Z.ltb count 0 = false) by (apply Z.ltb_ge; lia).
  assert (Hnonneg : Z.ltb index 0 = false) by (apply Z.ltb_ge; lia).
  rewrite Hcount, Hnonneg, init_nat_get by lia.
  rewrite Z2Nat.id by lia. reflexivity.
Qed.

Lemma get_ext {UC : Basics.Unreachable ascii} left right :
  OCamlString.length left = OCamlString.length right →
  (∀ index, 0 ≤ index < OCamlString.length left →
    @OCamlString.get UC left index = @OCamlString.get UC right index) → left = right.
Proof.
  revert right. induction left as [|byte rest IH]; intros [|other tail] Hlen Hget;
    try (unfold OCamlString.length in Hlen; cbn [Strings.String.length] in Hlen; lia).
  - reflexivity.
  - assert (Hbyte : byte = other).
    { specialize (Hget 0). apply Hget. unfold OCamlString.length; cbn [Strings.String.length]. lia. }
    subst other. f_equal. apply IH.
    + unfold OCamlString.length in ×. cbn [Strings.String.length] in Hlen.
      rewrite !Nat2Z.inj_succ in Hlen. lia.
    + intros index Hindex. specialize (Hget (index + 1)).
      assert (Hrange : 0 ≤ index + 1 < OCamlString.length (String byte rest)).
      { unfold OCamlString.length in ×. cbn [Strings.String.length]. rewrite Nat2Z.inj_succ. lia. }
      specialize (Hget Hrange). unfold OCamlString.get in ×.
      assert (Htest : Z.ltb index 0 = false) by (apply Z.ltb_ge; lia).
      assert (Htest' : Z.ltb (index+1) 0 = false) by (apply Z.ltb_ge; lia).
      rewrite Htest', Z2Nat.inj_add in Hget by lia.
      replace (Z.to_nat index + Z.to_nat 1)%nat with (S (Z.to_nat index)) in Hget by (cbn; lia).
      cbn [Strings.String.get] in Hget. rewrite Htest. exact Hget.
Qed.

Lemma mapi_from_get f bytes start index : (index < Strings.String.length bytes)%nat →
  Strings.String.get index (OCamlString.mapi_from f start bytes) =
    option_map (fun byte ⇒ f (start + Z.of_nat index) byte) (Strings.String.get index bytes).
Proof.
  revert start index. induction bytes as [|byte rest IH]; intros start [|index] Hindex;
    cbn [Strings.String.length] in Hindex; try lia.
  - cbn [OCamlString.mapi_from Strings.String.get option_map]. f_equal. f_equal. lia.
  - cbn [OCamlString.mapi_from Strings.String.get]. rewrite IH by lia.
    rewrite Nat2Z.inj_succ. replace (start + 1 + Z.of_nat index) with
      (start + Z.succ (Z.of_nat index)) by lia. reflexivity.
Qed.

Lemma get_none_bound bytes index : Strings.String.get index bytes = None →
  (Strings.String.length bytes ≤ index)%nat.
Proof.
  revert index. induction bytes as [|byte rest IH]; intros [|index] Hget;
    cbn [Strings.String.length Strings.String.get] in *; try discriminate; try lia.
  specialize (IH index Hget). lia.
Qed.

Lemma mapi_get {UC : Basics.Unreachable ascii} f bytes index :
  0 ≤ index < OCamlString.length bytes →
  @OCamlString.get UC (OCamlString.mapi f bytes) index =
    f index (@OCamlString.get UC bytes index).
Proof.
  intro Hindex. unfold OCamlString.mapi, OCamlString.get.
  assert (Htest : Z.ltb index 0 = false) by (apply Z.ltb_ge; lia).
  rewrite Htest, mapi_from_get by (unfold OCamlString.length in *; lia).
  rewrite Z2Nat.id by lia. cbn [Z.add].
  destruct (Strings.String.get (Z.to_nat index) bytes) eqn:Hget; [reflexivity|].
  apply get_none_bound in Hget. unfold OCamlString.length in Hindex. lia.
Qed.

Lemma get_fallback_irrelevant {left right : Basics.Unreachable ascii} bytes index :
  0 ≤ index < OCamlString.length bytes →
  @OCamlString.get left bytes index = @OCamlString.get right bytes index.
Proof.
  intro Hindex. unfold OCamlString.get.
  assert (Htest : Z.ltb index 0 = false) by (apply Z.ltb_ge; lia).
  rewrite Htest. destruct (Strings.String.get (Z.to_nat index) bytes) eqn:Hget;
    [reflexivity|].
  apply get_none_bound in Hget. unfold OCamlString.length in Hindex. lia.
Qed.

Lemma reverse_get {UC : Basics.Unreachable ascii} bytes index :
  OCamlString.length bytes < 2^62 →
  0 ≤ index < OCamlString.length bytes →
  @OCamlString.get UC (@Byte_string.Bytes.reverse bytes) index =
    @OCamlString.get UC bytes (OCamlString.length bytes - index - 1).
Proof.
  intros Hlength Hindex. unfold Byte_string.Bytes.reverse.
  rewrite mapi_get by exact Hindex.
  change (OCamlString.length bytes < 2^62) in Hlength.
  unfold OCamlInt.sub.
  change (Byte_string.Bytes.length bytes) with (OCamlString.length bytes).
  rewrite (OCamlInt.repr_small (OCamlString.length bytes - index)),
    (OCamlInt.repr_small (OCamlString.length bytes - index - 1)) by
    (unfold OCamlInt.min_int, OCamlInt.max_int, OCamlInt.sign_bit,
      OCamlInt.size, OCamlInt.Width.bits; lia).
  apply get_fallback_irrelevant. lia.
Qed.

Lemma reverse_init {UC : Basics.Unreachable ascii} {US : Basics.Unreachable string}
    count f : 0 ≤ count < 2^62 →
  @Byte_string.Bytes.reverse (@OCamlString.init US count f) =
    @OCamlString.init US count (fun index ⇒ f (count - index - 1)).
Proof.
  intro Hcount. apply (@get_ext UC).
  - rewrite reverse_length, !init_length by lia. reflexivity.
  - intros index Hindex. rewrite reverse_length, init_length in Hindex by lia.
    rewrite reverse_get, init_length, !init_get by (try rewrite init_length; lia).
    reflexivity.
Qed.

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. change (Z.succ (OCamlString.length
    (OCamlZ.to_bits_bytes count (value / 256))) = Z.succ (Z.of_nat count)).
  rewrite IHcount. reflexivity.
Qed.

Lemma to_bits_bytes_roundtrip count value :
  0 ≤ value < 2^(8 × Z.of_nat count) →
  OCamlZ.of_bits (OCamlZ.to_bits_bytes count value) = value.
Proof.
  revert value. induction count as [|count IH]; intros value Hvalue.
  - cbn in Hvalue. assert (value=0) by lia. subst value. reflexivity.
  - rewrite Nat2Z.inj_succ in Hvalue.
    replace (8 × Z.succ (Z.of_nat count)) with (8 × Z.of_nat count + 8) in Hvalue by lia.
    rewrite Z.pow_add_r in Hvalue by lia.
    pose proof (Z.mod_pos_bound value 256 ltac:(lia)) as Hmod.
    cbn [OCamlZ.to_bits_bytes OCamlZ.of_bits].
    rewrite Ascii.N_ascii_embedding by lia.
    rewrite Z2N.id by lia. rewrite IH.
    + pose proof (Z.div_mod value 256 ltac:(lia)). lia.
    + split; [apply Z.div_pos; lia|]. apply Z.div_lt_upper_bound; nia.
Qed.

Lemma to_bits_roundtrip value : 0 ≤ value →
  OCamlZ.of_bits (OCamlZ.to_bits value) = value.
Proof.
  intro Hvalue. destruct (Z.eq_dec value 0) as [->|Hnonzero]; [reflexivity|].
  unfold OCamlZ.to_bits, OCamlZ.limb_count, OCamlZ.numbits.
  rewrite !Z.abs_eq by lia.
  assert (Htest : Z.eqb value 0 = false) by (apply Z.eqb_neq; exact Hnonzero).
  rewrite Htest. apply to_bits_bytes_roundtrip.
  pose proof (Z.log2_nonneg value) as Hlog.
  pose proof (Z.div_mod (Z.succ (Z.log2 value) + 63) 64 ltac:(lia)) as Hdiv.
  pose proof (Z.mod_pos_bound (Z.succ (Z.log2 value) + 63) 64 ltac:(lia)) as Hmod.
  rewrite Nat2Z.inj_mul, Z2Nat.id by (apply Z.div_pos; lia).
  split; [lia|].
  apply (proj2 (Z.log2_lt_pow2 value _ ltac:(lia))). lia.
Qed.

Lemma to_bits_word_length value : wordRange value →
  OCamlString.length (OCamlZ.to_bits value) ≤ 32.
Proof.
  unfold wordRange. change (0 ≤ value < 2^256 → OCamlString.length (OCamlZ.to_bits value) ≤ 32).
  intro Hvalue. destruct (Z.eq_dec value 0) as [->|Hnonzero]; [vm_compute; discriminate|].
  unfold OCamlZ.to_bits, OCamlZ.limb_count, OCamlZ.numbits.
  rewrite !Z.abs_eq by lia.
  assert (Htest : Z.eqb value 0 = false) by (apply Z.eqb_neq; exact Hnonzero).
  rewrite Htest, to_bits_bytes_length, Nat2Z.inj_mul, Z2Nat.id
    by (apply Z.div_pos; pose proof (Z.log2_nonneg value); lia).
  pose proof (proj1 (Z.log2_lt_pow2 value 256 ltac:(lia)) (proj2 Hvalue)).
  assert ((Z.succ (Z.log2 value) + 63) / 64 < 5)
    by (apply Z.div_lt_upper_bound; lia).
  change (8 × ((Z.succ (Z.log2 value) + 63) / 64) ≤ 32). lia.
Qed.

Lemma append_get {UC : Basics.Unreachable ascii} left right index : 0 ≤ index →
  @OCamlString.get UC (Strings.String.append left right) index =
    if index <? OCamlString.length left then @OCamlString.get UC left index
    else @OCamlString.get UC right (index - OCamlString.length left).
Proof.
  intro Hindex. destruct (Z.ltb_spec index (OCamlString.length left)) as [Hleft|Hright].
  - unfold OCamlString.get. assert (Htest : Z.ltb index 0 = false) by (apply Z.ltb_ge; lia).
    rewrite Htest, <- Strings.String.append_correct1 by (unfold OCamlString.length in *; lia).
    reflexivity.
  - unfold OCamlString.get.
    assert (Htest : Z.ltb index 0 = false) by (apply Z.ltb_ge; lia).
    assert (Htest' : Z.ltb (index - OCamlString.length left) 0 = false) by (apply Z.ltb_ge; lia).
    rewrite Htest, Htest'.
    replace (Z.to_nat index) with
      (Z.to_nat (index - OCamlString.length left) + Strings.String.length left)%nat
      by (unfold OCamlString.length in *; lia).
    rewrite <- Strings.String.append_correct2. reflexivity.
Qed.

Lemma bits_append left right :
  OCamlZ.of_bits (Strings.String.append left right) =
    OCamlZ.of_bits left + 2^(8 × OCamlString.length left) × OCamlZ.of_bits right.
Proof.
  induction left as [|byte rest IH].
  - change (OCamlZ.of_bits right = 0 + 1 × OCamlZ.of_bits right). ring.
  - change (Z.of_N (Ascii.N_of_ascii byte) + 256 × OCamlZ.of_bits (Strings.String.append rest right) =
      Z.of_N (Ascii.N_of_ascii byte) + 256 × OCamlZ.of_bits rest +
      2^(8 × Z.of_nat (S (Strings.String.length rest))) × OCamlZ.of_bits right).
    rewrite IH, Nat2Z.inj_succ.
    replace (8 × Z.succ (Z.of_nat (Strings.String.length rest))) with
      (8 × OCamlString.length rest + 8) by (unfold OCamlString.length; lia).
    rewrite Z.pow_add_r by (unfold OCamlString.length; lia). ring.
Qed.

Lemma bits_zero_padding {US : Basics.Unreachable string} count : 0 ≤ count →
  OCamlZ.of_bits (@OCamlString.init US count (fun _ ⇒ "000"%char)) = 0.
Proof.
  intro Hcount.
  assert (Hzero : ∀ n start, OCamlZ.of_bits (OCamlString.init_nat n start (fun _ ⇒ "000"%char)) = 0).
  { induction n; intro start; [reflexivity|].
    change (0 + 256 × OCamlZ.of_bits (OCamlString.init_nat n (start+1) (fun _ ⇒ "000"%char)) = 0).
    rewrite IHn. reflexivity. }
  rewrite OCamlString.init_matches_structural. assert (Htest : Z.ltb count 0 = false) by (apply Z.ltb_ge; lia).
  rewrite Htest. apply Hzero.
Qed.

Lemma right_padding {US : Basics.Unreachable string} {UC : Basics.Unreachable ascii} count bytes :
  OCamlString.length bytes ≤ count →
  @OCamlString.init US count (fun i ⇒
    if Z.geb i (OCamlString.length bytes) then "000"%char else @OCamlString.get UC bytes i) =
  Strings.String.append bytes (@OCamlString.init US (count - OCamlString.length bytes) (fun _ ⇒ "000"%char)).
Proof.
  intro Hcount. assert (Hlen : 0 ≤ OCamlString.length bytes) by (unfold OCamlString.length; lia).
  apply (@get_ext UC).
  - rewrite byte_append_length, !init_length by lia. lia.
  - intros index Hindex. rewrite init_length in Hindex by lia.
    rewrite init_get, append_get by lia.
    destruct (Z.ltb_spec index (OCamlString.length bytes)) as [Hinside|Houtside].
    + assert (Htest : Z.geb index (OCamlString.length bytes) = false) by (rewrite Z.geb_leb; apply Z.leb_gt; lia).
      rewrite Htest. reflexivity.
    + assert (Htest : Z.geb index (OCamlString.length bytes) = true) by (rewrite Z.geb_leb; apply Z.leb_le; lia).
      rewrite Htest, init_get by lia. reflexivity.
Qed.

Lemma bits_right_padding {US : Basics.Unreachable string} {UC : Basics.Unreachable ascii} count bytes :
  OCamlString.length bytes ≤ count →
  OCamlZ.of_bits (@OCamlString.init US count (fun i ⇒
    if Z.geb i (OCamlString.length bytes) then "000"%char else @OCamlString.get UC bytes i)) =
    OCamlZ.of_bits bytes.
Proof.
  intro Hcount. rewrite right_padding by exact Hcount.
  rewrite bits_append, bits_zero_padding by lia. ring.
Qed.

Lemma reverse_init_mirror {UC : Basics.Unreachable ascii} {US : Basics.Unreachable string}
    count f : 0 ≤ count < 2^62 →
  @Byte_string.Bytes.reverse (@OCamlString.init US count (fun i ⇒ f (count - i - 1))) =
    @OCamlString.init US count f.
Proof.
  intro Hcount. rewrite reverse_init by exact Hcount. apply (@get_ext UC).
  - rewrite !init_length by lia. reflexivity.
  - intros index Hindex. rewrite init_length in Hindex by lia.
    rewrite !init_get by lia. f_equal. lia.
Qed.

Lemma word_encoding_roundtrip value : wordRange value →
  @U.of_repr (@U.to_repr value) = value.
Proof.
  intro Hvalue.
  pose (UR := Opaque.legacy_witness EmptyString).
  pose (charFallback := Opaque.legacy_witness
    (@Numeric.TwosComplement.Unsigned._rocq_fallback_to_repr_0 Numeric.Bits256_fargs)).
  change (@U.of_z_exn (OCamlZ.of_bits (@Byte_string.Bytes.reverse
    (@OCamlString.init UR 32 (fun i ⇒
      if Z.geb (32 - i - 1) (OCamlString.length (OCamlZ.to_bits value))
      then "000"%char else @OCamlString.get charFallback (OCamlZ.to_bits value) (32 - i - 1))))) = value).
  rewrite (@reverse_init_mirror charFallback UR 32 (fun i ⇒
    if Z.geb i (OCamlString.length (OCamlZ.to_bits value)) then "000"%char
    else @OCamlString.get charFallback (OCamlZ.to_bits value) i)) by lia.
  rewrite bits_right_padding by (apply to_bits_word_length; exact Hvalue).
  rewrite to_bits_roundtrip by (exact (proj1 Hvalue)).
  apply word_of_z. exact Hvalue.
Qed.

Lemma word_encoding_injective left right :
  wordRange left → wordRange right →
  @U.to_repr left = @U.to_repr right → left = right.
Proof.
  intros Hleft Hright Hencoded.
  pose proof (f_equal (@U.of_repr) Hencoded) as Hdecoded.
  rewrite !word_encoding_roundtrip in Hdecoded by assumption. exact Hdecoded.
Qed.

Print Assumptions word_encoding_roundtrip.
Print Assumptions word_encoding_injective.