https://gitlab.com/nomadic-labs/mi-cho-coq
Raw File
Tip revision: ccf5f784c964b9bab9d8aaeec0dd9eb35234401e authored by Colin González on 07 September 2020, 08:49:03 UTC
[dexter] proof for ep_setBaker_correct
Tip revision: ccf5f78
dexter_verification.v
(* Open Source License *)
(* Copyright (c) 2019 Nomadic Labs. <contact@nomadic-labs.com> *)

(* Permission is hereby granted, free of charge, to any person obtaining a *)
(* copy of this software and associated documentation files (the "Software"), *)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)
(* and/or sell copies of the Software, and to permit persons to whom the *)
(* Software is furnished to do so, subject to the following conditions: *)

(* The above copyright notice and this permission notice shall be included *)
(* in all copies or substantial portions of the Software. *)

(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
(* DEALINGS IN THE SOFTWARE. *)


Require Import Michocoq.macros.
Import syntax.
Import comparable.
Require Import NArith.
Require Import ZArith.
Require Import semantics.
Require Import util.
Import error.
Require List.

Require Import Coq.micromega.Lia.

Require dexter_definition.
Require dexter_spec.

Module Dexter_verification (C : ContractContext).

  Module Def := dexter_definition.
  Module Spec := dexter_spec.Dexter_spec C.

  Import Spec.semantics.

  Module Tactics.
    Ltac destruct_sto sto accounts self_is_updating_token_pool freeze_baker lq_total manager token_address token_pool xtz_pool :=
      destruct sto as
        (accounts, ((self_is_updating_token_pool, (freeze_baker, lq_total)), ((manager, token_address), (token_pool, xtz_pool)))).
    Ltac auto_fuel :=
      repeat match goal with
             | [ |- context G[eval_precond] ]
               => more_fuel; simpl
             end.
  End Tactics.

  Import Tactics.

  Section entrypoints.
    Variable (env : @proto_env dexter_definition.self_type).

    Lemma ep_addLiquidity_correct
          (p : data dexter_definition.parameter_ep_addLiquidity_ty)
          (sto : data dexter_definition.storage_ty)
          (ret_ops : Datatypes.list (data operation))
          (ret_sto : data dexter_definition.storage_ty)
          (fuel : Datatypes.nat) :
      (* TODO: fuel may have to be adapted to suit the entrypoint *)
      400 <= fuel ->

      eval_precond fuel env Def.ep_addLiquidity
                   (fun x => x = (ret_ops, ret_sto, tt))
                   (p, (sto, tt)) <->
      Spec.ep_addLiquidity env p sto ret_ops ret_sto.
    Proof.
      (* TODO: add proof *)
    Admitted.


    Lemma ep_approve_correct
          (p : data dexter_definition.parameter_ep_approve_ty)
          (sto : data dexter_definition.storage_ty)
          (ret_ops : Datatypes.list (data operation))
          (ret_sto : data dexter_definition.storage_ty)
          (fuel : Datatypes.nat) :
      (* TODO: fuel may have to be adapted to suit the entrypoint *)
      400 <= fuel ->
      eval_precond fuel env Def.ep_approve
                   (fun x => x = (ret_ops, ret_sto, tt))
                   (p, (sto, tt)) <->
      Spec.ep_approve env p sto ret_ops ret_sto.
    Proof.
      (* TODO: add proof *)
    Admitted.



    Lemma ep_default_correct
          (p : data dexter_definition.parameter_ep_default_ty)
          (sto : data dexter_definition.storage_ty)
          (ret_ops : Datatypes.list (data operation))
          (ret_sto : data dexter_definition.storage_ty)
          (fuel : Datatypes.nat) :
      400 <= fuel ->
      eval_precond fuel env Def.ep_default
                   (fun x => x = (ret_ops, ret_sto, tt))
                   (p, (sto, tt)) <->
      Spec.ep_default env p sto ret_ops ret_sto.
    Proof.
      intro Hfuel.
      auto_fuel.
      destruct_sto sto
                   accounts self_is_updating_token_pool freeze_baker lq_total manager token_address token_pool xtz_pool.
      rewrite precond_exists.
      unfold precond_ex.
      apply and_both_0.
      unfold Spec.Storage.sto_selfIsUpdatingTokenPool.
      - (* Check self_updating_token_pool is false *)
        rewrite bool_not_false.
        apply not_iff_compat.
        split; [apply Bool.Is_true_eq_left | apply Bool.Is_true_eq_true].
      - (* Check xtz_pool is set to xtz_pool + amount *)
        unfold Spec.ep_default, Spec.mutez_add, add.
        unfold Spec.Storage.sto_xtzPool.
        unfold Spec.Storage.sto_set_xtzPool.
        simpl.
        intuition.
        + destruct H.
          destruct H.
          exists x.
          split.
          ++ rewrite <- H.
             rewrite Zplus_comm.
             reflexivity.
          ++ injection H0.
             intros.
             subst.
             intuition reflexivity.
        + destruct H.
          destruct H.
          exists x.
          split.
          ++ rewrite <- H.
             rewrite Zplus_comm.
             reflexivity.
          ++ destruct H0.
             subst.
             reflexivity.
    Qed.

    Lemma ep_removeLiquidity_correct
          (p : data dexter_definition.parameter_ep_removeLiquidity_ty)
          (sto : data dexter_definition.storage_ty)
          (ret_ops : Datatypes.list (data operation))
          (ret_sto : data dexter_definition.storage_ty)
          (fuel : Datatypes.nat) :
      (* TODO: fuel may have to be adapted to suit the entrypoint *)
      400 <= fuel ->
      eval_precond fuel env Def.ep_removeLiquidity
                   (fun x => x = (ret_ops, ret_sto, tt))
                   (p, (sto, tt)) <->
      Spec.ep_removeLiquidity env p sto ret_ops ret_sto.
    Proof.
      (* TODO: add proof *)
    Admitted.

    Lemma eqb_eq a c1 c2 :
      BinInt.Z.eqb (comparison_to_int (compare a c1 c2)) Z0 = true <->
      c1 = c2.
    Proof.
      rewrite BinInt.Z.eqb_eq.
      rewrite comparison_to_int_Eq.
      apply comparable.compare_eq_iff.
    Qed.

    Lemma eqb_neq a c1 c2 :
      BinInt.Z.eqb (comparison_to_int (compare a c1 c2)) Z0 = false <->
      c1 <> c2.
    Proof.
      split.
      - intros Hf He.
        rewrite <- eqb_eq in He.
        congruence.
      - intro Hneq.
        rewrite <- eqb_eq in Hneq.
        destruct ((comparison_to_int (compare a c1 c2) =? 0)%Z); congruence.
    Qed.

    Lemma negb_is_true:
      forall b : Datatypes.bool, negb b = true <-> ~ is_true b.
    Proof.
      intros. destruct b.
      - intuition. elimtype False. apply H. reflexivity.
      - simpl. split; intuition.
    Qed.

    Lemma ep_setBaker_correct
          (p : data dexter_definition.parameter_ep_setBaker_ty)
          (sto : data dexter_definition.storage_ty)
          (ret_ops : Datatypes.list (data operation))
          (ret_sto : data dexter_definition.storage_ty)
          (fuel : Datatypes.nat) :
      (* TODO: fuel may have to be adapted to suit the entrypoint *)
      400 <= fuel ->
      eval_precond fuel env Def.ep_setBaker
                   (fun x => x = (ret_ops, ret_sto, tt))
                   (p, (sto, tt)) <->
      Spec.ep_setBaker env p sto ret_ops ret_sto.
    Proof.
      intros Hfuel.
      auto_fuel.
      destruct_sto sto
          accounts self_is_updating_token_pool freeze_baker lq_total manager token_address token_pool xtz_pool.
      (* destruct p as (_baker, p_freeze_baker). *)
      rewrite (eqb_eq address).
      rewrite (eqb_eq mutez).
      rewrite negb_is_true.
      destruct p as (param_baker, param_freeze_baker).
      apply and_both_0.
      - rewrite bool_not_false.
        apply not_iff_compat.
        split; [apply Bool.Is_true_eq_left | apply Bool.Is_true_eq_true].
      - apply and_both_2.
        intros.
        simpl.
        unfold Spec.env_sender.
        intuition.
        + injection H3.
          intros.
          rewrite <- H4.
          reflexivity.
        + congruence.
        +rewrite H2.
         rewrite H4.
         reflexivity.
Qed.

    Lemma ep_updateTokenPool_correct
          (p : data dexter_definition.parameter_ep_updateTokenPool_ty)
          (sto : data dexter_definition.storage_ty)
          (ret_ops : Datatypes.list (data operation))
          (ret_sto : data dexter_definition.storage_ty)
          (fuel : Datatypes.nat) :
      (* TODO: fuel may have to be adapted to suit the entrypoint *)
      400 <= fuel ->
      eval_precond fuel env Def.ep_updateTokenPool
                   (fun x => x = (ret_ops, ret_sto, tt))
                   (p, (sto, tt)) <->
      Spec.ep_updateTokenPool env p sto ret_ops ret_sto.
    Proof.
      intros Hfuel.
      auto_fuel.
      destruct_sto sto
          accounts self_is_updating_token_pool freeze_baker lq_total manager token_address token_pool xtz_pool.
      (* TODO: finish proof *)
      admit.
    Admitted.

    Lemma ep_updateTokenPoolInternal_correct
          (p : data dexter_definition.parameter_ep_updateTokenPoolInternal_ty)
          (sto : data dexter_definition.storage_ty)
          (ret_ops : Datatypes.list (data operation))
          (ret_sto : data dexter_definition.storage_ty)
          (fuel : Datatypes.nat) :
      (* TODO: fuel may have to be adapted to suit the entrypoint *)
      400 <= fuel ->
      eval_precond fuel env Def.ep_updateTokenPoolInternal
                   (fun x => x = (ret_ops, ret_sto, tt))
                   (p, (sto, tt)) <->
      Spec.ep_updateTokenPoolInternal env p sto ret_ops ret_sto.
    Proof.
      intros Hfuel.
      auto_fuel.
      destruct_sto sto
          accounts self_is_updating_token_pool freeze_baker lq_total manager token_address token_pool xtz_pool.
      admit. (* spec must be updated *)
      (*
       * rewrite if_false_is_and.
       * simpl.
       * rewrite (eqb_eq address).
       * unfold Spec.ep_updateTokenPoolInternal.
       * apply and_both_0.
       * - reflexivity.
       * - simpl.
       *   intuition congruence.
       *)
    Admitted.

    Lemma ep_tokenToXtz_correct
          (p : data dexter_definition.parameter_ep_tokenToXtz_ty)
          (sto : data dexter_definition.storage_ty)
          (ret_ops : Datatypes.list (data operation))
          (ret_sto : data dexter_definition.storage_ty)
          (fuel : Datatypes.nat) :
      (* TODO: fuel may have to be adapted to suit the entrypoint *)
      400 <= fuel ->
      eval_precond fuel env Def.ep_tokenToXtz
                   (fun x => x = (ret_ops, ret_sto, tt))
                   (p, (sto, tt)) <->
      Spec.ep_tokenToXtz env p sto ret_ops ret_sto.
    Proof.
      (* TODO: add proof *)
    Admitted.

    Lemma ep_xtzToToken_correct
          (p : data dexter_definition.parameter_ep_xtzToToken_ty)
          (sto : data dexter_definition.storage_ty)
          (ret_ops : Datatypes.list (data operation))
          (ret_sto : data dexter_definition.storage_ty)
          (fuel : Datatypes.nat) :
      (* TODO: fuel may have to be adapted to suit the entrypoint *)
      400 <= fuel ->
      eval_precond fuel env Def.ep_xtzToToken
                   (fun x => x = (ret_ops, ret_sto, tt))
                   (p, (sto, tt)) <->
      Spec.ep_xtzToToken env p sto ret_ops ret_sto.
    Proof.
      (* TODO: add proof *)
    Admitted.

    Lemma ep_tokenToToken_correct
          (p : data dexter_definition.parameter_ep_tokenToToken_ty)
          (sto : data dexter_definition.storage_ty)
          (ret_ops : Datatypes.list (data operation))
          (ret_sto : data dexter_definition.storage_ty)
          (fuel : Datatypes.nat) :
      (* TODO: fuel may have to be adapted to suit the entrypoint *)
      400 <= fuel ->
      eval_precond fuel env Def.ep_tokenToToken
                   (fun x => x = (ret_ops, ret_sto, tt))
                   (p, (sto, tt)) <->
      Spec.ep_tokenToToken env p sto ret_ops ret_sto.
    Proof.
      (* TODO: add proof *)
    Admitted.

    Lemma ep_setManager_correct
          (p : data dexter_definition.parameter_ep_setManager_ty)
          (sto : data dexter_definition.storage_ty)
          (ret_ops : Datatypes.list (data operation))
          (ret_sto : data dexter_definition.storage_ty)
          (fuel : Datatypes.nat) :
      400 <= fuel ->
      eval_precond fuel env Def.ep_setManager
                   (fun x => x = (ret_ops, ret_sto, tt))
                   (p, (sto, tt)) <->
      Spec.ep_setManager env p sto ret_ops ret_sto.
    Proof.
      intros Hfuel.
      auto_fuel.
      destruct_sto sto accounts self_is_updating_token_pool freeze_baker lq_total manager token_address token_pool xtz_pool.
      apply and_both_0.
      - unfold Spec.Storage.sto_selfIsUpdatingTokenPool.
        Set Printing Coercions.
        unfold is_true, Is_true.
        rewrite bool_not_false.
        apply not_iff_compat.
        split; [apply Bool.Is_true_eq_left | apply Bool.Is_true_eq_true ].
      - apply and_both_0.
        rewrite (eqb_eq mutez).
        intuition.
        + rewrite (eqb_eq address).
          simpl; intuition congruence.
Qed.

  End entrypoints.

End Dexter_verification.
back to top