Library QedkConcrete.nativepointbounds

Point parsers and serializers

Parsing validates field coordinates before curve operations. The encoding lemmas expose the coordinate bounds required by zero padding; they do not assume an arbitrary point has a fixed-size encoding.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles.Utils QedkConcrete.modexpoutputs QedkConcrete.precompileoutputs
  QedkConcrete.nativeencoding QedkConcrete.bytewords QedkConcrete.nativeindices.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module EP := Precompiles.Utils.Ec_precompile_utils.
Module ES := Precompiles.Utils.Ec_precompile_utils_M_signature.

Lemma native_option_ensures {A : Set} (P : A → Prop) value :
  (∀ result, value = Some result → P result) →
  nativeEnsures P (PC.Option.or_fail value).
Proof.
  intro Hresult. destruct value as [value|].
  - apply native_ensures_return, Hresult. reflexivity.
  - intro state. exact I.
Qed.

Section Points.
Context {Bytes Point1 Fp2 Point2 : Set} {F : @EP.FArgs Bytes Point1 Fp2 Point2}
  {UN : Basics.Unreachable Support.UintN.t} {UU : Basics.Unreachable unit}
  {US : Basics.Unreachable string} {UC : Basics.Unreachable ascii}.

Lemma ec_field_parse_bound (P : Z → Prop) :
  (∀ input output, EP.M.(ES.F_p_of_uint_opt) input = Some output → P output) →
  nativeEnsures P (@EP.f_p _ _ _ _ F).
Proof.
  intro Hparse. unfold EP.f_p. apply native_ensures_any; intro value.
  apply native_option_ensures, Hparse.
Qed.

Lemma ec_point_parse_bound (P : Z → Prop) (Q : Point1 → Prop) :
  (∀ input output, EP.M.(ES.F_p_of_uint_opt) input = Some output → P output) →
  (∀ x y point, P x → P y →
    EP.M.(ES.C_1).(Ec.Curve.SIG.of_coords) x y = Some point → Q point) →
  nativeEnsures Q (@EP.point_c1 _ _ _ _ F).
Proof.
  intros Hparse Hpoint. unfold EP.point_c1.
  eapply native_ensures_bind; [apply ec_field_parse_bound; exact Hparse|].
  intros x Hx. eapply native_ensures_bind; [apply ec_field_parse_bound; exact Hparse|].
  intros y Hy. apply native_option_ensures. intros point Hcoords.
  exact (Hpoint x y point Hx Hy Hcoords).
Qed.

Lemma ec_coordinate_encoding_length x :
  let width := EP.M.(ES.C1_coord_repr).(PC.BYTES.byte_width) in
  0 ≤ width < 2^62 → 0 ≤ x < 2^(8×width) →
  OCamlString.length (@EP.c1_coord_to_repr _ _ _ _ F x) = width.
Proof.
  cbn beta zeta. intros Hwidth Hx.
  assert (Huint : @Numeric.Integer.as_unsigned_exn x = Z.to_N x).
  { change (@Support.UintN.of_z_exn x = Z.to_N x).
    unfold Support.UintN.of_z_exn, Support.UintN.of_z_opt, Support.UintN.in_range.
    rewrite (proj2 (Z.leb_le 0 x) (proj1 Hx)). reflexivity. }
  unfold EP.c1_coord_to_repr. rewrite Huint. cbn beta iota zeta.
  assert (Hencoded : OCamlString.length (@Support.UintN.to_bytes_be (Z.to_N x)) ≤
    EP.M.(ES.C1_coord_repr).(PC.BYTES.byte_width)).
  { apply uint_encoding_length_bound; [lia|]. rewrite Z2N.id by lia. lia. }
  rewrite byte_append_length. unfold Byte_string.Bytes.length, MonadVM.String.length.
  unfold OCamlInt.sub. rewrite native_repr_small by
    (unfold OCamlString.length in *; lia).
  rewrite native_padding_length by lia. lia.
Qed.

Lemma ec_point_encoding_length point :
  let width := EP.M.(ES.C1_coord_repr).(PC.BYTES.byte_width) in
  let coords := EP.M.(ES.C_1).(Ec.Curve.SIG.coords) point in
  0 ≤ width < 2^62 →
  0 ≤ fst coords < 2^(8×width) → 0 ≤ snd coords < 2^(8×width) →
  OCamlString.length (@EP.delta_1_inv _ _ _ _ F point) = 2×width.
Proof.
  cbn beta zeta. intros Hwidth Hx Hy. unfold EP.delta_1_inv.
  destruct (EP.M.(ES.C_1).(Ec.Curve.SIG.coords) point) as [x y].
  cbn beta iota zeta in Hx, Hy |- ×.
  change (OCamlString.length (String.append
    (@EP.c1_coord_to_repr _ _ _ _ F x)
    (@EP.c1_coord_to_repr _ _ _ _ F y)) =
    2×EP.M.(ES.C1_coord_repr).(PC.BYTES.byte_width)).
  rewrite byte_append_length, !ec_coordinate_encoding_length by assumption. lia.
Qed.
End Points.