Library QedkConcrete.modexpoutputs

MODEXP return-data bound

Revision Nine caps each encoded parameter at 1024 bytes. The result is smaller than the modulus, so its zero-padded encoding fits that cap. The proof does not assume the discarded OCaml assertion about padding.
From Stdlib Require Import ZArith Lia Bool.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles.Modexp QedkConcrete.precompileoutputs
  QedkConcrete.nativeencoding QedkConcrete.bytewords QedkConcrete.nativeindices.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Local Open Scope bool_scope.
Module MX := Precompiles.Modexp.Make.

Definition nativeEnsures {A : Set} (P : A → Prop) (action : PC.t A) : Prop :=
  ∀ state, match fst (action state) with inl value ⇒ P value | inr _ ⇒ True end.

Lemma native_ensures_bind {A B : Set} (P : A → Prop) (Q : B → Prop)
    (action : PC.t A) (next : A → PC.t B) :
  nativeEnsures P action → (∀ value, P value → nativeEnsures Q (next value)) →
  nativeEnsures Q (PC.op_letdollar action next).
Proof.
  intros Haction Hnext state. specialize (Haction state).
  change (match fst (let '(result, middle) := action state in
    match result with inl value ⇒ next value middle | inr err ⇒ (inr err, middle) end) with
    | inl value ⇒ Q value | inr _ ⇒ True end).
  destruct (action state) as [[value|err] middle]; [apply Hnext; exact Haction|exact I].
Qed.

Lemma native_ensures_return {A : Set} (P : A → Prop) value :
  P value → nativeEnsures P (PC._return value).
Proof. intros Hvalue state. exact Hvalue. Qed.

Lemma native_ensures_map {A B : Set} (P : A → Prop) (Q : B → Prop)
    (f : A → B) (action : PC.t A) :
  nativeEnsures P action → (∀ value, P value → Q (f value)) →
  nativeEnsures Q (PC.op_ltdollargt f action).
Proof.
  intros Haction Hf state. specialize (Haction state).
  change (match fst (let '(result, middle) := action state in
    match result with inl value ⇒ (inl (f value), middle) | inr err ⇒ (inr err, middle) end) with
    | inl value ⇒ Q value | inr _ ⇒ True end).
  destruct (action state) as [[value|err] middle]; [apply Hf; exact Haction|exact I].
Qed.

Lemma native_ensures_any {A B : Set} (Q : B → Prop)
    (action : PC.t A) (next : A → PC.t B) :
  (∀ value, nativeEnsures Q (next value)) →
  nativeEnsures Q (PC.op_letdollar action next).
Proof.
  intro Hnext. apply (native_ensures_bind (fun _ ⇒ True));
    [intros state; destruct (fst (action state)); exact I|intros value _; apply Hnext].
Qed.

Lemma native_bytes_length {US : Basics.Unreachable string} {UC : Basics.Unreachable ascii} count :
  0 ≤ count → nativeEnsures (fun bytes ⇒ OCamlString.length bytes = count)
    (@PC.bytes_value count).
Proof.
  intro Hcount. unfold PC.bytes_value.
  apply native_ensures_any; intro offset. apply native_ensures_any; intro bytes.
  apply native_ensures_return, padded_length; exact Hcount.
Qed.

Lemma uint_to_int_small {UI : Basics.Unreachable int} value :
  Z.of_N value < 2^62 → @Support.UintN.to_int value = Z.of_N value.
Proof.
  intro Hvalue. change (@OCamlZ.checked_signed Support.UintN.primitive_int 63 (Z.of_N value) = Z.of_N value).
  unfold OCamlZ.checked_signed, OCamlZ.fits_signed, OCamlZ.signed_min, OCamlZ.signed_max.
  assert (Htest : ((- (2 ^ Z.pred 63) <=? Z.of_N value) &&
    (Z.of_N value <=? Z.pred (2 ^ Z.pred 63))) = true).
  { apply andb_true_iff. split; apply Z.leb_le; lia. }
  rewrite Htest. reflexivity.
Qed.

Lemma modexp_parameter_length_bounded `{_fargs : MX.FArgs}
    `{_rocq_assumption_0 :
      RocqOfOCaml.Basics.Unreachable MonadVM.Support.UintN.t}
    `{_rocq_assumption_1 :
      RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.Repr.t}
    `{_rocq_assumption_2 : RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.t}
    `{_rocq_assumption_3 : RocqOfOCaml.Basics.Unreachable int}
    `{_rocq_assumption_4 : RocqOfOCaml.Basics.Unreachable ascii}
    `{_rocq_assumption_5 :
      RocqOfOCaml.Basics.Unreachable
        (MonadVM.Evmc.Message.t × int →
        MonadVM.Precompiles.Utils.Precompile.Inner.t
          (sum MonadVM.Support.UintN.t MonadVM.Evmc.Result.StatusCode.t ×
            (MonadVM.Evmc.Message.t × int)))} :
  MX.Revision.(Precompiles.Modexp.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
  nativeEnsures (fun n ⇒ (n ≤ 1024)%N)
    (@MX.parameter_length _fargs).
Proof.
  intro Hrev. unfold MX.parameter_length. rewrite Hrev.
  apply native_ensures_any; intro n.
  change (nativeEnsures (fun n ⇒ (n ≤ 1024)%N)
    (PC.op_letdollar (PC.when_ (N.ltb 1024 n) PC.precompile_failure)
      (fun _ ⇒ PC._return n))).
  destruct (N.ltb_spec 1024 n) as [Hlarge|Hsmall].
  - intros state. exact I.
  - apply native_ensures_any; intros []. apply native_ensures_return. lia.
Qed.

Lemma native_padding_length {US : string} count byte :
  0 ≤ count → OCamlString.length (@Byte_string.Bytes.make US count byte) = count.
Proof.
  intro Hcount.
  assert (Hnat : ∀ n, Strings.String.length (OCamlString.make_nat n byte) = n).
  { induction n; cbn [OCamlString.make_nat Strings.String.length]; congruence. }
  unfold Byte_string.Bytes.make, MonadVM.String.make.
  rewrite OCamlString.make_matches_structural.
  rewrite (proj2 (Z.ltb_ge count 0) Hcount).
  unfold OCamlString.length. rewrite Hnat, Z2Nat.id by exact Hcount. reflexivity.
Qed.

Lemma native_uint_bytes_bound {US : Basics.Unreachable string}
    {UC : Basics.Unreachable ascii} {UN : Basics.Unreachable Support.UintN.t} count :
  0 ≤ count → nativeEnsures (fun n ⇒ Z.of_N n < 2^(8×count))
    (PC.op_ltdollargt (@Support.UintN.of_bytes_be) (@PC.bytes_value count)).
Proof.
  intro Hcount. eapply native_ensures_map; [apply native_bytes_length; exact Hcount|].
  intros bytes Hbytes. rewrite <- Hbytes. apply uint_decoding_bound.
Qed.

Lemma modexp_result_bound {UZ : Basics.Unreachable Z} {UN : Basics.Unreachable Support.UintN.t}
    base exponent modulus count :
  0 ≤ count → Z.of_N modulus < 2^(8×count) →
  Z.of_N (if (N.eqb modulus 0 || N.eqb modulus 1)%bool then 0%N
    else if N.eqb exponent 0 then 1%N else @Support.UintN.exp_mod modulus base exponent)
    < 2^(8×count).
Proof.
  intros Hcount Hmodulus. destruct (orb _ _) eqn:Htrivial.
  - apply Z.pow_pos_nonneg; lia.
  - apply orb_false_iff in Htrivial as [Hzero Hone].
    apply N.eqb_neq in Hone. destruct (N.eqb exponent 0).
    + change (1 < 2^(8×count)). apply N.eqb_neq in Hzero. lia.
    + unfold Support.UintN.exp_mod. rewrite Hzero.
      apply N.eqb_neq in Hzero.
      pose proof (@powm_bound Support.UintN.primitive_int (Z.of_N base) (Z.of_N exponent) (Z.of_N modulus)
        ltac:(lia) ltac:(lia)) as Hpow.
      change (Z.of_N (Z.to_N (@OCamlZ.powm Support.UintN.primitive_int (Z.of_N base) (Z.of_N exponent)
        (Z.of_N modulus))) < 2^(8×count)).
      rewrite Z2N.id by lia. lia.
Qed.

Lemma modexp_output_bounded `{_fargs : MX.FArgs}
    `{_rocq_assumption_0 : RocqOfOCaml.Basics.Unreachable Z.t}
    `{_rocq_assumption_1 : RocqOfOCaml.Basics.Unreachable MonadVM.Uint64.t}
    `{_rocq_assumption_2 :
      RocqOfOCaml.Basics.Unreachable MonadVM.Support.UintN.t}
    `{_rocq_assumption_3 :
      RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.Repr.t}
    `{_rocq_assumption_4 : RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.t}
    `{_rocq_assumption_5 : RocqOfOCaml.Basics.Unreachable MonadVM.Gas.t}
    `{_rocq_assumption_6 :
      RocqOfOCaml.Basics.Unreachable MonadVM.Chain.Ethereum.Address.t}
    `{_rocq_assumption_7 :
      RocqOfOCaml.Basics.Unreachable MonadVM.Byte_string.Bytes.t}
    `{_rocq_assumption_8 : RocqOfOCaml.Basics.Unreachable unit}
    `{_rocq_assumption_9 : RocqOfOCaml.Basics.Unreachable string}
    `{_rocq_assumption_10 : RocqOfOCaml.Basics.Unreachable int}
    `{_rocq_assumption_11 : RocqOfOCaml.Basics.Unreachable ascii}
    `{_rocq_assumption_12 : RocqOfOCaml.Basics.Unreachable (int × int)}
    `{_rocq_assumption_13 :
      RocqOfOCaml.Basics.Unreachable
        (MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
        MonadVM.Support.UintN.Impl.t)}
    `{_rocq_assumption_14 :
      RocqOfOCaml.Basics.Unreachable
        (MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
        MonadVM.Support.UintN.t)}
    `{_rocq_assumption_15 :
      RocqOfOCaml.Basics.Unreachable
        (MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
        MonadVM.Support.UintN.Impl.t → MonadVM.Support.UintN.Impl.t →
        MonadVM.Support.UintN.Impl.t)}
    `{_rocq_assumption_16 :
      RocqOfOCaml.Basics.Unreachable
        (MonadVM.Evmc.Message.t × int →
        MonadVM.Precompiles.Utils.Precompile.Inner.t
          (sum MonadVM.Support.UintN.t MonadVM.Evmc.Result.StatusCode.t ×
            (MonadVM.Evmc.Message.t × int)))} (msg : MonadVM.Evmc.Message.t)
 :
  MX.Revision.(Precompiles.Modexp.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
  OCamlString.length (Evmc.Result.t.output_data
    (@MX.precompile _fargs msg)) ≤ 1024.
Proof.
  intro Hrev. unfold MX.precompile. apply native_run_output; [lia|].
  eapply native_ensures_bind; [apply modexp_parameter_length_bounded; exact Hrev|].
  intros baseLength Hbase.
  eapply native_ensures_bind; [apply modexp_parameter_length_bounded; exact Hrev|].
  intros exponentLength Hexponent.
  eapply native_ensures_bind; [apply modexp_parameter_length_bounded; exact Hrev|].
  intros modulusLength Hmodulus.
  cbn beta in Hbase, Hexponent, Hmodulus.
  apply native_ensures_any; intros []. cbn beta iota zeta.
  rewrite !uint_to_int_small by lia.
  lazymatch goal with |- nativeEnsures _ (if ?test then _ else _) ⇒ destruct test end.
  - apply native_ensures_return. change (0 ≤ 1024). lia.
  - apply native_ensures_any; intro base.
    apply native_ensures_any; intro exponent.
    eapply native_ensures_bind; [apply native_uint_bytes_bound; lia|].
    intros modulus Hbytes. cbn beta iota zeta.
    apply native_ensures_return.
    lazymatch goal with
    | |- OCamlString.length (String.append _ ?encoded) ≤ _ ⇒
      assert (Hencoded : OCamlString.length encoded ≤ Z.of_N modulusLength)
        by (apply uint_encoding_length_bound; [lia|apply modexp_result_bound; [lia|exact Hbytes]])
    end.
    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.

Print Assumptions modexp_output_bounded.