https://gitlab.com/tezos/tezos
Raw File
Tip revision: 278ba0a46487eba8679975d2ec3c2fff4948e11b authored by Andrea Cerone on 31 January 2023, 19:57:12 UTC
Dac/Node: mode Dac_member module to separate file
Tip revision: 278ba0a
michelson_v1_gas_costs.ml
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2019-2022 Nomadic Labs <contact@nomadic-labs.com>           *)
(* Copyright (c) 2020 Metastate AG <hello@metastate.dev>                     *)
(* Copyright (c) 2022 DaiLambda, Inc. <contact@dailambda.jp>                 *)
(*                                                                           *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

include Michelson_v1_gas_costs_generated
module S = Saturation_repr

(* This file contains functions saved from
   the original michelson_v1_gas_costs.ml.
   These functions couldn't be generated by the snoop tool. *)

(* N_IMul_nattez / N_IMul_teznat
   These functions are benchmarked with the affine model.
   However, empirically, the coefficient is ignorably small. *)

(* model N_IMul_nattez *)
let cost_N_IMul_nattez = S.safe_int 50

(* model N_IMul_teznat *)
let cost_N_IMul_teznat = S.safe_int 50

(* N_IEdiv_teznat
   This function is modeled with `division_cost` model,
   which takes two arguments, divisor and dividend. *)

(* model N_IEdiv_teznat *)
let cost_N_IEdiv_teznat = S.safe_int 70

(* N_ISapling_verify_update
   This function depends on another cost function cost_N_IBlake2b.
   Such code can't be generated by the current Snoop. *)

(* model N_ISapling_verify_update *)
let cost_N_ISapling_verify_update size1 size2 bound_data =
  let open S.Syntax in
  let v1 = S.safe_int size1 in
  let v2 = S.safe_int size2 in
  cost_N_IBlake2b bound_data + S.safe_int 310_000
  + (S.safe_int 5_575_000 * v1)
  + (S.safe_int 5_075_000 * v2)

(* N_IApply
   The current generated model receives int as a flag,
   but it should receive bool. *)

(* model N_IApply *)
let cost_N_IApply rec_flag = if rec_flag then S.safe_int 220 else S.safe_int 140

(* N_KIter / N_KMap_enter_body
   The empty_branch_model are used as the models.
   However, the defined cost functions receive nothing. *)

(* model N_KIter *)
let cost_N_KIter = S.safe_int 10

(* model N_KMap_enter_body *)
let cost_N_KMap_enter_body = S.safe_int 80

(* N_KList_enter_body
   The generated model receives the length of `xs` as the first argument
   and branches on whether it is 0 or not.
   However, calculating the length makes the performance worse.
   The model should be changed to receive `xs_is_nil` as the first argument. *)

(* model N_KList_enter_body *)
(* Approximating 1.797068 x term *)
let cost_N_KList_enter_body xs size_ys =
  match xs with
  | [] ->
      let open S.Syntax in
      let v0 = S.safe_int size_ys in
      S.safe_int 30 + (v0 + (v0 lsr 1) + (v0 lsr 2) + (v0 lsr 4))
  | _ :: _ -> S.safe_int 30

(* TY_EQ / PARSE_TYPE / UNPARSE_TYPE / TYPECHECKING_CODE / UNPARSING_CODE
   TYPECHECKING_DATA / UNPARSING_DATA / FIND_ENTRYPOINT
   Following functions are commented to indicate that they were manually modified. *)

(* model TY_EQ
   This is the estimated cost of one iteration of ty_eq, extracted
   and copied manually from the parameter fit for the TY_EQ benchmark
   (the model is parametric on the size of the type, which we don't have
   access to in O(1)). *)
let cost_TY_EQ = S.safe_int 60

(* model PARSE_TYPE
   This is the cost of one iteration of parse_ty, extracted by hand from the
   parameter fit for the PARSE_TYPE benchmark. *)
let cost_PARSE_TYPE = S.safe_int 60

(* model UNPARSE_TYPE
   This is the cost of one iteration of unparse_ty, extracted by hand from the
   parameter fit for the UNPARSE_TYPE benchmark. *)
let cost_UNPARSE_TYPE type_size = S.mul (S.safe_int 20) type_size

(* model TYPECHECKING_CODE
   This is the cost of one iteration of parse_instr, extracted by hand from the
   parameter fit for the TYPECHECKING_CODE benchmark. *)
let cost_TYPECHECKING_CODE = S.safe_int 220

(* model UNPARSING_CODE
   This is the cost of one iteration of unparse_instr, extracted by hand from the
   parameter fit for the UNPARSING_CODE benchmark. *)
let cost_UNPARSING_CODE = S.safe_int 115

(* model TYPECHECKING_DATA
   This is the cost of one iteration of parse_data, extracted by hand from the
   parameter fit for the TYPECHECKING_DATA benchmark. *)
let cost_TYPECHECKING_DATA = S.safe_int 100

(* model UNPARSING_DATA
   This is the cost of one iteration of unparse_data, extracted by hand from the
   parameter fit for the UNPARSING_DATA benchmark. *)
let cost_UNPARSING_DATA = S.safe_int 65

(* TODO: https://gitlab.com/tezos/tezos/-/issues/2264
   Benchmark.
   Currently approximated by 2 comparisons of the longest entrypoint. *)
let cost_FIND_ENTRYPOINT = cost_N_ICompare 31 31

(* SAPLING_TRANSACTION_ENCODING / SAPLING_DIFF_ENCODING
   These functions are missing model definitions. *)

(* model SAPLING_TRANSACTION_ENCODING *)
let cost_SAPLING_TRANSACTION_ENCODING ~inputs ~outputs ~bound_data =
  S.safe_int (1500 + (inputs * 160) + (outputs * 320) + (bound_data lsr 3))

(* model SAPLING_DIFF_ENCODING *)
let cost_SAPLING_DIFF_ENCODING ~nfs ~cms = S.safe_int ((nfs * 22) + (cms * 215))
back to top