From bfe45e3424859bc30ed4326a9dbee9f6b4801494 Mon Sep 17 00:00:00 2001 From: Sota Sato Date: Mon, 5 Sep 2022 21:36:17 +0900 Subject: [PATCH 1/4] proto: separate _costs and _costs_generated from michelson_v1_gas.ml --- src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 2 + src/proto_alpha/lib_protocol/dune | 8 + .../lib_protocol/michelson_v1_gas.ml | 949 +----------------- .../lib_protocol/michelson_v1_gas_costs.ml | 29 + .../michelson_v1_gas_costs_generated.ml | 948 +++++++++++++++++ .../lib_protocol/saturation_repr.ml | 11 + .../lib_protocol/saturation_repr.mli | 11 + 7 files changed, 1018 insertions(+), 940 deletions(-) create mode 100644 src/proto_alpha/lib_protocol/michelson_v1_gas_costs.ml create mode 100644 src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index a6fbc9a7f5b1..29acacd89e7b 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -200,6 +200,8 @@ "Gas_input_size", "Script_typed_ir_size", "Script_typed_ir_size_costs", + "Michelson_v1_gas_costs_generated", + "Michelson_v1_gas_costs", "Michelson_v1_gas", "Script_list", "Script_tc_context", diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index fb09c19b20f2..ccbbfeb2cc9e 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -214,6 +214,8 @@ Gas_input_size Script_typed_ir_size Script_typed_ir_size_costs + Michelson_v1_gas_costs_generated + Michelson_v1_gas_costs Michelson_v1_gas Script_list Script_tc_context @@ -481,6 +483,8 @@ gas_input_size.ml gas_input_size.mli script_typed_ir_size.ml script_typed_ir_size.mli script_typed_ir_size_costs.ml script_typed_ir_size_costs.mli + michelson_v1_gas_costs_generated.ml + michelson_v1_gas_costs.ml michelson_v1_gas.ml michelson_v1_gas.mli script_list.ml script_list.mli script_tc_context.ml script_tc_context.mli @@ -728,6 +732,8 @@ gas_input_size.ml gas_input_size.mli script_typed_ir_size.ml script_typed_ir_size.mli script_typed_ir_size_costs.ml script_typed_ir_size_costs.mli + michelson_v1_gas_costs_generated.ml + michelson_v1_gas_costs.ml michelson_v1_gas.ml michelson_v1_gas.mli script_list.ml script_list.mli script_tc_context.ml script_tc_context.mli @@ -980,6 +986,8 @@ gas_input_size.ml gas_input_size.mli script_typed_ir_size.ml script_typed_ir_size.mli script_typed_ir_size_costs.ml script_typed_ir_size_costs.mli + michelson_v1_gas_costs_generated.ml + michelson_v1_gas_costs.ml michelson_v1_gas.ml michelson_v1_gas.mli script_list.ml script_list.mli script_tc_context.ml script_tc_context.mli diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas.ml b/src/proto_alpha/lib_protocol/michelson_v1_gas.ml index 4f6bc65f1a23..6c88513c5783 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_gas.ml +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas.ml @@ -31,17 +31,6 @@ module S = Saturation_repr module Size = Gas_input_size module Cost_of = struct - module S_syntax = struct - (* This is a good enough approximation. S.numbits 0 = 0 *) - let log2 x = S.safe_int (1 + S.numbits x) - - let ( + ) = S.add - - let ( * ) = S.mul - - let ( lsr ) = S.shift_right - end - let z_bytes (z : Z.t) = let bits = Z.numbits z in (7 + bits) / 8 @@ -52,928 +41,8 @@ module Cost_of = struct let manager_operation = step_cost @@ S.safe_int manager_operation_int - module Generated_costs = struct - (* Automatically generated costs functions. *) - - (* model N_IAbs_int *) - (* Allocates [size] bytes. *) - let cost_N_IAbs_int size = S.safe_int (20 + (size lsr 1)) - - (* model N_IAdd_bls12_381_fr *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_IAdd_bls12_381_fr = S.safe_int 30 - - (* model N_IAdd_bls12_381_g1 *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_IAdd_bls12_381_g1 = S.safe_int 900 - - (* model N_IAdd_bls12_381_g2 *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_IAdd_bls12_381_g2 = S.safe_int 2_470 - - (* Allocates [max size1 size2] *) - let cost_linear_op_int size1 size2 = - let open S_syntax in - let v0 = S.safe_int (Compare.Int.max size1 size2) in - S.safe_int 35 + (v0 lsr 1) - - (* model N_IAdd_int *) - (* Approximating 0.078154 x term *) - let cost_N_IAdd_int = cost_linear_op_int - - (* model N_IAdd_nat *) - (* Approximating 0.077807 x term *) - let cost_N_IAdd_nat = cost_linear_op_int - - (* model N_IAdd_seconds_to_timestamp *) - (* Approximating 0.078056 x term *) - let cost_N_IAdd_seconds_to_timestamp = cost_linear_op_int - - (* model N_IAdd_tez *) - let cost_N_IAdd_tez = S.safe_int 20 - - (* model N_IAdd_timestamp_to_seconds *) - (* Approximating 0.077771 x term *) - let cost_N_IAdd_timestamp_to_seconds = cost_linear_op_int - - (* model N_IAddress *) - let cost_N_IAddress = S.safe_int 10 - - (* model N_IAmount *) - let cost_N_IAmount = S.safe_int 10 - - (* model N_IAnd *) - let cost_N_IAnd = S.safe_int 10 - - (* model N_IAnd_int_nat *) - (* Allocates [min size1 size2] *) - let cost_N_IAnd_int_nat size1 size2 = - let open S_syntax in - let v0 = S.safe_int (Compare.Int.min size1 size2) in - S.safe_int 35 + (v0 lsr 1) - - (* model N_IAnd_nat *) - (* Allocates [min size1 size2] *) - let cost_N_IAnd_nat size1 size2 = - let open S_syntax in - let v0 = S.safe_int (Compare.Int.min size1 size2) in - S.safe_int 35 + (v0 lsr 1) - - (* model N_IApply *) - let cost_N_IApply rec_flag = - if rec_flag then S.safe_int 220 else S.safe_int 140 - - (* model N_IBalance *) - let cost_N_IBalance = S.safe_int 10 - - (* model N_IBlake2b *) - (* Approximating 1.120804 x term *) - let cost_N_IBlake2b size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 430 + v0 + (v0 lsr 3) - - (* model N_IBytes_size *) - let cost_N_IBytes_size = S.safe_int 10 - - (* model N_ICar *) - let cost_N_ICar = S.safe_int 10 - - (* model N_ICdr *) - let cost_N_ICdr = S.safe_int 10 - - (* model N_IChainId *) - let cost_N_IChainId = S.safe_int 15 - - (* model N_ICheck_signature_ed25519 *) - (* Approximating 1.123507 x term *) - let cost_N_ICheck_signature_ed25519 size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 65_800 + (v0 + (v0 lsr 3)) - - (* model N_ICheck_signature_p256 *) - (* Approximating 1.111539 x term *) - let cost_N_ICheck_signature_p256 size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 990_000 + (v0 + (v0 lsr 3)) - - (* model N_ICheck_signature_secp256k1 *) - (* Approximating 1.125404 x term *) - let cost_N_ICheck_signature_secp256k1 size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 51_600 + (v0 + (v0 lsr 3)) - - (* model N_IComb *) - (* Approximating 3.531001 x term *) - (* Note: size >= 2, so the cost is never 0 *) - let cost_N_IComb size = - let open S_syntax in - let v0 = S.safe_int size in - (S.safe_int 3 * v0) + (v0 lsr 1) + (v0 lsr 5) - - (* model N_IComb_get *) - (* Approximating 0.573180 x term *) - let cost_N_IComb_get size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 20 + (v0 lsr 1) + (v0 lsr 4) - - (* model N_IComb_set *) - (* Approximating 1.287531 x term *) - let cost_N_IComb_set size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 20 + (v0 + (v0 lsr 2) + (v0 lsr 5)) - - (* Model N_ICompare *) - (* Approximating 0.024413 x term *) - let cost_N_ICompare size1 size2 = - let open S_syntax in - let v0 = S.safe_int (Compare.Int.min size1 size2) in - S.safe_int 35 + ((v0 lsr 6) + (v0 lsr 7)) - - (* model N_IConcat_bytes_pair *) - (* Allocates [size1 + size2] *) - let cost_N_IConcat_bytes_pair size1 size2 = - let open S_syntax in - let v0 = S.safe_int size1 + S.safe_int size2 in - S.safe_int 45 + (v0 lsr 1) - - (* model N_IConcat_string_pair *) - (* Allocates [size1 + size2] *) - let cost_N_IConcat_string_pair size1 size2 = - let open S_syntax in - let v0 = S.safe_int size1 + S.safe_int size2 in - S.safe_int 45 + (v0 lsr 1) - - (* model N_ICons_list *) - let cost_N_ICons_list = S.safe_int 10 - - (* model N_ICons_none *) - let cost_N_ICons_none = S.safe_int 10 - - (* model N_ICons_pair *) - let cost_N_ICons_pair = S.safe_int 10 - - (* model N_ICons_some *) - let cost_N_ICons_some = S.safe_int 10 - - (* model N_IConst *) - let cost_N_IConst = S.safe_int 10 - - (* model N_IContract *) - let cost_N_IContract = S.safe_int 30 - - (* model N_ICreate_contract *) - let cost_N_ICreate_contract = S.safe_int 60 - - (* model N_IDiff_timestamps *) - (* Approximating 0.077922 x term *) - let cost_N_IDiff_timestamps = cost_linear_op_int - - (* model N_IDig *) - (* Approximating 6.750442 x term *) - let cost_N_IDig size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 30 + ((S.safe_int 6 * v0) + (v0 lsr 1) + (v0 lsr 2)) - - (* model N_IDip *) - let cost_N_IDip = S.safe_int 10 - - (* model N_IDipN *) - (* Approximating 4.05787663635 x term *) - let cost_N_IDipN size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 15 + (S.safe_int 4 * v0) - - (* model N_IView *) - let cost_N_IView = S.safe_int 1460 - - (* model N_IDrop *) - let cost_N_IDrop = S.safe_int 10 - - (* model N_IDropN *) - (* Approximating 2.713108 x term *) - let cost_N_IDropN size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 30 + (S.safe_int 2 * v0) + (v0 lsr 1) + (v0 lsr 3) - - (* model N_IDug *) - (* Approximating 6.718396 x term *) - let cost_N_IDug size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 35 + ((S.safe_int 6 * v0) + (v0 lsr 1) + (v0 lsr 2)) - - (* model N_IDup *) - let cost_N_IDup = S.safe_int 10 - - (* model N_IDupN *) - (* Approximating 1.222263 x term *) - let cost_N_IDupN size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 20 + v0 + (v0 lsr 2) - - let cost_div_int size1 size2 = - (* Allocates at most [size1] bytes *) - let open S_syntax in - let v1 = S.safe_int size1 in - let q = size1 - size2 in - if Compare.Int.(q < 0) then S.safe_int 105 + (v1 lsr 1) - else - let v0 = S.safe_int q * S.safe_int size2 in - S.safe_int 105 + (v0 lsr 10) + (v0 lsr 11) + (v0 lsr 13) + (v1 lsr 1) - - (* model N_IEdiv_int *) - (* Approximating 0.001591 x term *) - let cost_N_IEdiv_int = cost_div_int - - (* model N_IEdiv_nat *) - (* Approximating 0.001605 x term *) - let cost_N_IEdiv_nat = cost_div_int - - (* model N_IEdiv_tez *) - let cost_N_IEdiv_tez = S.safe_int 80 - - (* model N_IEdiv_teznat *) - let cost_N_IEdiv_teznat = S.safe_int 70 - - (* model N_IEmpty_big_map *) - let cost_N_IEmpty_big_map = S.safe_int 300 - - (* model N_IEmpty_map *) - let cost_N_IEmpty_map = S.safe_int 300 - - (* model N_IEmpty_set *) - let cost_N_IEmpty_set = S.safe_int 300 - - (* model N_IEq *) - let cost_N_IEq = S.safe_int 10 - - (* model N_IExec *) - let cost_N_IExec = S.safe_int 10 - - (* model N_IFailwith *) - (* let cost_N_IFailwith = S.safe_int 105 *) - - (* model N_IGe *) - let cost_N_IGe = S.safe_int 10 - - (* model N_IGt *) - let cost_N_IGt = S.safe_int 10 - - (* model N_IHalt *) - let cost_N_IHalt = S.safe_int 15 - - (* model N_IHash_key *) - let cost_N_IHash_key = S.safe_int 605 - - (* model N_IIf *) - let cost_N_IIf = S.safe_int 10 - - (* model N_IIf_cons *) - let cost_N_IIf_cons = S.safe_int 10 - - (* model N_IIf_left *) - let cost_N_IIf_left = S.safe_int 10 - - (* model N_IIf_none *) - let cost_N_IIf_none = S.safe_int 10 - - (* model N_IOpt_map *) - let cost_opt_map = S.safe_int 10 - - (* model N_IImplicit_account *) - let cost_N_IImplicit_account = S.safe_int 10 - - (* model N_IInt_bls12_381_z_fr *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_IInt_bls12_381_z_fr = S.safe_int 115 - - (* model N_IInt_nat *) - let cost_N_IInt_nat = S.safe_int 10 - - (* model N_IIs_nat *) - let cost_N_IIs_nat = S.safe_int 10 - - (* model N_IKeccak *) - (* Approximating 8.276352 x term *) - let cost_N_IKeccak size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 1350 + ((S.safe_int 8 * v0) + (v0 lsr 2)) - - (* model N_ILambda *) - let cost_N_ILambda = S.safe_int 10 - - (* model N_ILe *) - let cost_N_ILe = S.safe_int 10 - - (* model N_ILeft *) - let cost_N_ILeft = S.safe_int 10 - - (* model N_ILevel *) - let cost_N_ILevel = S.safe_int 10 - - (* model N_IList_iter *) - let cost_N_IList_iter _ = S.safe_int 20 - - (* model N_IList_map *) - let cost_N_IList_map _ = S.safe_int 20 - - (* model N_IList_size *) - let cost_N_IList_size = S.safe_int 10 - - (* model N_ILoop *) - let cost_N_ILoop = S.safe_int 10 - - (* model N_ILoop_left *) - let cost_N_ILoop_left = S.safe_int 10 - - (* model N_ILsl_nat *) - (* Allocates at most [size + 256] bytes *) - let cost_N_ILsl_nat size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 128 + (v0 lsr 1) - - (* model N_ILsr_nat *) - (* Allocates at most [size] bytes*) - let cost_N_ILsr_nat size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 45 + (v0 lsr 1) - - (* model N_ILt *) - let cost_N_ILt = S.safe_int 10 - - (* model N_IMap_get *) - (* Approximating 0.048359 x term *) - let cost_N_IMap_get size1 size2 = - let open S_syntax in - let v0 = size1 * log2 size2 in - S.safe_int 45 + (v0 lsr 5) + (v0 lsr 6) - - (* model N_IMap_get_and_update *) - (* Approximating 0.145661 x term *) - let cost_N_IMap_get_and_update size1 size2 = - let open S_syntax in - let v0 = size1 * log2 size2 in - S.safe_int 75 + (v0 lsr 3) + (v0 lsr 6) - - (* model N_IMap_iter *) - (* Approximating 7.621331 x term *) - let cost_N_IMap_iter size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 50 + (S.safe_int 7 * v0) + (v0 lsr 1) + (v0 lsr 3) - - (* model N_IMap_map *) - (* Approximating 8.38965386732 x term *) - let cost_N_IMap_map size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 40 + ((S.safe_int 8 * v0) + (v0 lsr 1)) - - (* model N_IMap_mem *) - (* Approximating 0.048446 x term *) - let cost_N_IMap_mem size1 size2 = - let open S_syntax in - let v0 = size1 * log2 size2 in - S.safe_int 45 + (v0 lsr 5) + (v0 lsr 6) - - (* model N_IMap_size *) - let cost_N_IMap_size = S.safe_int 10 - - (* model N_IMap_update *) - (* Approximating 0.097072 x term *) - let cost_N_IMap_update size1 size2 = - let open S_syntax in - let v0 = size1 * log2 size2 in - S.safe_int 55 + (v0 lsr 4) + (v0 lsr 5) - - (* model N_IMul_bls12_381_fr *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_IMul_bls12_381_fr = S.safe_int 45 - - (* model N_IMul_bls12_381_fr_z *) - (* Approximating 1.059386 x term *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_IMul_bls12_381_fr_z size1 = - let open S_syntax in - let v0 = S.safe_int size1 in - S.safe_int 265 + v0 + (v0 lsr 4) - - (* model N_IMul_bls12_381_g1 *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_IMul_bls12_381_g1 = S.safe_int 103_000 - - (* model N_IMul_bls12_381_g2 *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_IMul_bls12_381_g2 = S.safe_int 220_000 - - (* model N_IMul_bls12_381_z_fr *) - (* Approximating 1.068674 x term *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_IMul_bls12_381_z_fr size1 = - let open S_syntax in - let v0 = S.safe_int size1 in - S.safe_int 265 + v0 + (v0 lsr 4) - - let cost_mul size1 size2 = - let open S_syntax in - let a = S.add (S.safe_int size1) (S.safe_int size2) in - let v0 = a * log2 a in - S.safe_int 55 + (v0 lsr 1) + (v0 lsr 2) + (v0 lsr 4) - - (* model N_IMul_int *) - (* Approximating 0.857931 x term *) - let cost_N_IMul_int = cost_mul - - (* model N_IMul_nat *) - (* Approximating 0.861823 x term *) - let cost_N_IMul_nat = cost_mul - - (* 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 - - (* model N_INeg_bls12_381_fr *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_INeg_bls12_381_fr = S.safe_int 25 - - (* model N_INeg_bls12_381_g1 *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_INeg_bls12_381_g1 = S.safe_int 50 - - (* model N_INeg_bls12_381_g2 *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_INeg_bls12_381_g2 = S.safe_int 70 - - (* model N_INeg *) - (* Allocates [size] bytes *) - let cost_N_INeg size = - let open S_syntax in - S.safe_int 25 + (S.safe_int size lsr 1) - - (* model N_INeq *) - let cost_N_INeq = S.safe_int 10 - - (* model N_INil *) - let cost_N_INil = S.safe_int 10 - - (* model N_INot *) - let cost_N_INot = S.safe_int 10 - - (* model N_INot_int *) - (* Allocates [size] bytes *) - let cost_N_INot_int size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 25 + (v0 lsr 1) - - (* model N_INow *) - let cost_N_INow = S.safe_int 10 - - (* model N_IMin_block_time *) - let cost_N_IMin_block_time = S.safe_int 20 - - (* model N_IOpen_chest *) - (* 612000 + chest * 19 + time * 19050 *) - let cost_N_IOpen_chest ~chest ~time = - let open S_syntax in - let v0 = S.safe_int chest in - let v1 = S.safe_int time in - S.safe_int 612_000 + (S.safe_int 19 * v0) + (S.safe_int 19050 * v1) - - (* model N_IOr *) - let cost_N_IOr = S.safe_int 10 - - (* model N_IOr_nat *) - (* Approximating 0.075758 x term *) - let cost_N_IOr_nat = cost_linear_op_int - - (* model N_IPairing_check_bls12_381 *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_N_IPairing_check_bls12_381 size = - S.add (S.safe_int 450_000) (S.mul (S.safe_int 342_500) (S.safe_int size)) - - (* model N_IRead_ticket *) - let cost_N_IRead_ticket = S.safe_int 10 - - (* model N_IRight *) - let cost_N_IRight = S.safe_int 10 - - (* model N_ISapling_empty_state *) - let cost_N_ISapling_empty_state = S.safe_int 300 - - (* 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) - - (* model N_ISelf_address *) - let cost_N_ISelf_address = S.safe_int 10 - - (* model N_ISelf *) - let cost_N_ISelf = S.safe_int 10 - - (* model N_ISender *) - let cost_N_ISender = S.safe_int 10 - - (* model N_ISet_delegate *) - let cost_N_ISet_delegate = S.safe_int 60 - - (* model N_ISet_iter *) - (* Approximating 7.633555 x term *) - let cost_N_ISet_iter size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 50 + (S.safe_int 7 * v0) + (v0 lsr 1) + (v0 lsr 3) - - (* model N_ISet_size *) - let cost_N_ISet_size = S.safe_int 10 - - (* model N_ISha256 *) - (* Approximating 4.763264 x term *) - let cost_N_ISha256 size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 600 + ((S.safe_int 4 * v0) + (v0 lsr 1) + (v0 lsr 2)) - - (* model N_ISha3 *) - (* Approximating 8.362339 x term *) - let cost_N_ISha3 = cost_N_IKeccak - - (* model N_ISha512 *) - (* Approximating 3.074641 x term *) - let cost_N_ISha512 size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 680 + (S.safe_int 3 * v0) - - (* model N_ISlice_bytes *) - (* Allocates [size] bytes *) - let cost_N_ISlice_bytes size = - let open S_syntax in - S.safe_int 25 + (S.safe_int size lsr 1) - - (* model N_ISlice_string *) - (* Allocates [size] bytes *) - let cost_N_ISlice_string size = - let open S_syntax in - S.safe_int 25 + (S.safe_int size lsr 1) - - (* model N_ISource *) - let cost_N_ISource = S.safe_int 10 - - (* model N_ISplit_ticket *) - (* Allocates [max size1 size2] *) - let cost_N_ISplit_ticket size1 size2 = - let open S_syntax in - let v1 = S.safe_int (Compare.Int.max size1 size2) in - S.safe_int 40 + (v1 lsr 1) - - (* model N_IString_size *) - let cost_N_IString_size = S.safe_int 15 - - (* model N_ISub_int *) - (* Approximating 0.077849 x term *) - let cost_N_ISub_int = cost_linear_op_int - - (* model N_ISub_tez *) - let cost_N_ISub_tez = S.safe_int 15 - - (* model N_ISub_tez_legacy *) - let cost_N_ISub_tez_legacy = S.safe_int 20 - - (* model N_ISub_timestamp_seconds *) - (* Approximating 0.077794 x term *) - let cost_N_ISub_timestamp_seconds = cost_linear_op_int - - (* model N_ISwap *) - let cost_N_ISwap = S.safe_int 10 - - (* model N_ITicket *) - let cost_N_ITicket = S.safe_int 10 - - (* model N_ITotal_voting_power *) - let cost_N_ITotal_voting_power = S.safe_int 450 - - (* model N_ITransfer_tokens *) - let cost_N_ITransfer_tokens = S.safe_int 60 - - (* model N_IUncomb *) - (* Approximating 3.944710 x term *) - let cost_N_IUncomb size = - let open S_syntax in - let v0 = S.safe_int size in - S.safe_int 25 + (S.safe_int 4 * v0) - - (* model N_IUnpair *) - let cost_N_IUnpair = S.safe_int 10 - - (* model N_IVoting_power *) - let cost_N_IVoting_power = S.safe_int 640 - - (* model N_IXor *) - let cost_N_IXor = S.safe_int 15 - - (* model N_IXor_nat *) - (* Approximating 0.075601 x term *) - let cost_N_IXor_nat = cost_linear_op_int - - (* model N_KCons *) - let cost_N_KCons = S.safe_int 10 - - (* model N_KIter *) - let cost_N_KIter = S.safe_int 10 - - (* model N_KList_enter_body *) - (* Approximating 1.672196 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 25 + (v0 + (v0 lsr 1) + (v0 lsr 3)) - | _ :: _ -> S.safe_int 25 - - (* model N_KList_exit_body *) - let cost_N_KList_exit_body = S.safe_int 10 - - (* model N_KLoop_in *) - let cost_N_KLoop_in = S.safe_int 10 - - (* model N_KLoop_in_left *) - let cost_N_KLoop_in_left = S.safe_int 10 - - (* model N_KMap_enter_body *) - let cost_N_KMap_enter_body = S.safe_int 80 - - (* model N_KNil *) - let cost_N_KNil = S.safe_int 15 - - (* model N_KReturn *) - let cost_N_KReturn = S.safe_int 10 - - (* model N_KView_exit *) - let cost_N_KView_exit = S.safe_int 20 - - (* model N_KMap_head *) - let cost_N_KMap_head = S.safe_int 20 - - (* model N_KUndip *) - let cost_N_KUndip = S.safe_int 10 - - (* model DECODING_BLS_FR *) - (* when benchmarking, compile bls12-381 without ADX, see - https://gitlab.com/dannywillems/ocaml-bls12-381/-/blob/71d0b4d467fbfaa6452d702fcc408d7a70916a80/README.md#install - *) - let cost_DECODING_BLS_FR = S.safe_int 120 - - (* model DECODING_BLS_G1 *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_DECODING_BLS_G1 = S.safe_int 54_600 - - (* model DECODING_BLS_G2 *) - (* when benchmarking, compile bls12-381 without ADX *) - let cost_DECODING_BLS_G2 = S.safe_int 69_000 - - (* model B58CHECK_DECODING_CHAIN_ID *) - let cost_B58CHECK_DECODING_CHAIN_ID = S.safe_int 1_600 - - (* model B58CHECK_DECODING_PUBLIC_KEY_HASH_ed25519 *) - let cost_B58CHECK_DECODING_PUBLIC_KEY_HASH_ed25519 = S.safe_int 3_300 - - (* model B58CHECK_DECODING_PUBLIC_KEY_HASH_p256 *) - let cost_B58CHECK_DECODING_PUBLIC_KEY_HASH_p256 = S.safe_int 3_300 - - (* model B58CHECK_DECODING_PUBLIC_KEY_HASH_secp256k1 *) - let cost_B58CHECK_DECODING_PUBLIC_KEY_HASH_secp256k1 = S.safe_int 3_300 - - (* model B58CHECK_DECODING_PUBLIC_KEY_ed25519 *) - let cost_B58CHECK_DECODING_PUBLIC_KEY_ed25519 = S.safe_int 4_200 - - (* model B58CHECK_DECODING_PUBLIC_KEY_p256 *) - let cost_B58CHECK_DECODING_PUBLIC_KEY_p256 = S.safe_int 325_000 - - (* model B58CHECK_DECODING_PUBLIC_KEY_secp256k1 *) - let cost_B58CHECK_DECODING_PUBLIC_KEY_secp256k1 = S.safe_int 9_000 - - (* model B58CHECK_DECODING_SIGNATURE_ed25519 *) - let cost_B58CHECK_DECODING_SIGNATURE_ed25519 = S.safe_int 6_400 - - (* model B58CHECK_DECODING_SIGNATURE_p256 *) - let cost_B58CHECK_DECODING_SIGNATURE_p256 = S.safe_int 6_400 - - (* model B58CHECK_DECODING_SIGNATURE_secp256k1 *) - let cost_B58CHECK_DECODING_SIGNATURE_secp256k1 = S.safe_int 6_400 - - (* model ENCODING_BLS_FR *) - let cost_ENCODING_BLS_FR = S.safe_int 80 - - (* model ENCODING_BLS_G1 *) - let cost_ENCODING_BLS_G1 = S.safe_int 3200 - - (* model ENCODING_BLS_G2 *) - let cost_ENCODING_BLS_G2 = S.safe_int 3900 - - (* model B58CHECK_ENCODING_CHAIN_ID *) - let cost_B58CHECK_ENCODING_CHAIN_ID = S.safe_int 1_800 - - (* model B58CHECK_ENCODING_PUBLIC_KEY_HASH_ed25519 *) - let cost_B58CHECK_ENCODING_PUBLIC_KEY_HASH_ed25519 = S.safe_int 3_200 - - (* model B58CHECK_ENCODING_PUBLIC_KEY_HASH_p256 *) - let cost_B58CHECK_ENCODING_PUBLIC_KEY_HASH_p256 = S.safe_int 3_200 - - (* model B58CHECK_ENCODING_PUBLIC_KEY_HASH_secp256k1 *) - let cost_B58CHECK_ENCODING_PUBLIC_KEY_HASH_secp256k1 = S.safe_int 3_200 - - (* model B58CHECK_ENCODING_PUBLIC_KEY_ed25519 *) - let cost_B58CHECK_ENCODING_PUBLIC_KEY_ed25519 = S.safe_int 4_500 - - (* model B58CHECK_ENCODING_PUBLIC_KEY_p256 *) - let cost_B58CHECK_ENCODING_PUBLIC_KEY_p256 = S.safe_int 4_550 - - (* model B58CHECK_ENCODING_PUBLIC_KEY_secp256k1 *) - let cost_B58CHECK_ENCODING_PUBLIC_KEY_secp256k1 = S.safe_int 4_950 - - (* model B58CHECK_ENCODING_SIGNATURE_ed25519 *) - let cost_B58CHECK_ENCODING_SIGNATURE_ed25519 = S.safe_int 8_300 - - (* model B58CHECK_ENCODING_SIGNATURE_p256 *) - let cost_B58CHECK_ENCODING_SIGNATURE_p256 = S.safe_int 8_300 - - (* model B58CHECK_ENCODING_SIGNATURE_secp256k1 *) - let cost_B58CHECK_ENCODING_SIGNATURE_secp256k1 = S.safe_int 8_300 - - (* model DECODING_CHAIN_ID *) - let cost_DECODING_CHAIN_ID = S.safe_int 50 - - (* model DECODING_PUBLIC_KEY_HASH_ed25519 *) - let cost_DECODING_PUBLIC_KEY_HASH_ed25519 = S.safe_int 50 - - (* model DECODING_PUBLIC_KEY_HASH_p256 *) - let cost_DECODING_PUBLIC_KEY_HASH_p256 = S.safe_int 50 - - (* model DECODING_PUBLIC_KEY_HASH_secp256k1 *) - let cost_DECODING_PUBLIC_KEY_HASH_secp256k1 = S.safe_int 50 - - (* model DECODING_PUBLIC_KEY_ed25519 *) - let cost_DECODING_PUBLIC_KEY_ed25519 = S.safe_int 60 - - (* model DECODING_PUBLIC_KEY_p256 *) - let cost_DECODING_PUBLIC_KEY_p256 = S.safe_int 320_000 - - (* model DECODING_PUBLIC_KEY_secp256k1 *) - let cost_DECODING_PUBLIC_KEY_secp256k1 = S.safe_int 4_900 - - (* model DECODING_SIGNATURE_ed25519 *) - let cost_DECODING_SIGNATURE_ed25519 = S.safe_int 35 - - (* model DECODING_SIGNATURE_p256 *) - let cost_DECODING_SIGNATURE_p256 = S.safe_int 35 - - (* model DECODING_SIGNATURE_secp256k1 *) - let cost_DECODING_SIGNATURE_secp256k1 = S.safe_int 35 - - (* model DECODING_Chest_key *) - let cost_DECODING_Chest_key = S.safe_int 5900 - - (* model DECODING_Chest *) - (* Approximating 0.039349 x term *) - let cost_DECODING_Chest ~bytes = - let open S_syntax in - let v0 = S.safe_int bytes in - S.safe_int 7400 + (v0 lsr 5) + (v0 lsr 7) - - (* model ENCODING_CHAIN_ID *) - let cost_ENCODING_CHAIN_ID = S.safe_int 50 - - (* model ENCODING_PUBLIC_KEY_HASH_ed25519 *) - let cost_ENCODING_PUBLIC_KEY_HASH_ed25519 = S.safe_int 70 - - (* model ENCODING_PUBLIC_KEY_HASH_p256 *) - let cost_ENCODING_PUBLIC_KEY_HASH_p256 = S.safe_int 70 - - (* model ENCODING_PUBLIC_KEY_HASH_secp256k1 *) - let cost_ENCODING_PUBLIC_KEY_HASH_secp256k1 = S.safe_int 70 - - (* model ENCODING_PUBLIC_KEY_ed25519 *) - let cost_ENCODING_PUBLIC_KEY_ed25519 = S.safe_int 80 - - (* model ENCODING_PUBLIC_KEY_p256 *) - let cost_ENCODING_PUBLIC_KEY_p256 = S.safe_int 90 - - (* model ENCODING_PUBLIC_KEY_secp256k1 *) - let cost_ENCODING_PUBLIC_KEY_secp256k1 = S.safe_int 455 - - (* model ENCODING_SIGNATURE_ed25519 *) - let cost_ENCODING_SIGNATURE_ed25519 = S.safe_int 45 - - (* model ENCODING_SIGNATURE_p256 *) - let cost_ENCODING_SIGNATURE_p256 = S.safe_int 45 - - (* model ENCODING_SIGNATURE_secp256k1 *) - let cost_ENCODING_SIGNATURE_secp256k1 = S.safe_int 45 - - (* model ENCODING_Chest_key *) - let cost_ENCODING_Chest_key = S.safe_int 10_000 - - (* model ENCODING_Chest *) - (* Approximating 0.120086 x term *) - let cost_ENCODING_Chest ~plaintext_size = - let open S_syntax in - let v0 = S.safe_int plaintext_size in - S.safe_int 12_200 + (v0 lsr 3) - - (* model TIMESTAMP_READABLE_DECODING *) - (* Approximating 0.045400 x term *) - let cost_TIMESTAMP_READABLE_DECODING ~bytes = - let open S_syntax in - let b = S.safe_int bytes in - let v0 = S.mul (S.sqrt b) b in - S.safe_int 105 + ((v0 lsr 5) + (v0 lsr 6)) - - (* model TIMESTAMP_READABLE_ENCODING *) - let cost_TIMESTAMP_READABLE_ENCODING = S.safe_int 820 - - (* model CHECK_PRINTABLE *) - let cost_CHECK_PRINTABLE size = - let open S_syntax in - S.safe_int 14 + (S.safe_int 10 * S.safe_int size) - - (* 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 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 - - (* 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 - - (* 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 - - (* 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)) - - (* model IEmit *) - let cost_N_IEmit = S.safe_int 30 - end - module Interpreter = struct - open Generated_costs + open Michelson_v1_gas_costs let drop = atomic_step_cost cost_N_IDrop @@ -1450,7 +519,7 @@ module Cost_of = struct compare ty x y Gas.free Return let set_mem (type a) (elt : a) (set : a Script_typed_ir.set) = - let open S_syntax in + let open S.Syntax in let (module Box) = Script_set.get set in let per_elt_cost = Box.OPS.elt_size elt |> Size.to_int |> S.safe_int in let size = S.safe_int Box.size in @@ -1458,7 +527,7 @@ module Cost_of = struct Gas.(intercept +@ (log2 size *@ per_elt_cost)) let set_update (type a) (elt : a) (set : a Script_typed_ir.set) = - let open S_syntax in + let open S.Syntax in let (module Box) = Script_set.get set in let per_elt_cost = Box.OPS.elt_size elt |> Size.to_int |> S.safe_int in let size = S.safe_int Box.size in @@ -1468,7 +537,7 @@ module Cost_of = struct Gas.(intercept +@ (S.safe_int 2 * log2 size *@ per_elt_cost)) let map_mem (type k v) (elt : k) (map : (k, v) Script_typed_ir.map) = - let open S_syntax in + let open S.Syntax in let (module Box) = Script_map.get_module map in let per_elt_cost = Box.OPS.key_size elt |> Size.to_int |> S.safe_int in let size = S.safe_int Box.size in @@ -1478,7 +547,7 @@ module Cost_of = struct let map_get = map_mem let map_update (type k v) (elt : k) (map : (k, v) Script_typed_ir.map) = - let open S_syntax in + let open S.Syntax in let (module Box) = Script_map.get_module map in let per_elt_cost = Box.OPS.key_size elt |> Size.to_int |> S.safe_int in let size = S.safe_int Box.size in @@ -1489,7 +558,7 @@ module Cost_of = struct let map_get_and_update (type k v) (elt : k) (map : (k, v) Script_typed_ir.map) = - let open S_syntax in + let open S.Syntax in let (module Box) = Script_map.get_module map in let per_elt_cost = Box.OPS.key_size elt |> Size.to_int |> S.safe_int in let size = S.safe_int Box.size in @@ -1580,7 +649,7 @@ module Cost_of = struct (* Cost of Unpack pays two integer comparisons, and a Bytes slice *) let unpack bytes = let blen = Bytes.length bytes in - let open S_syntax in + let open S.Syntax in atomic_step_cost (S.safe_int 260 + (S.safe_int blen lsr 1)) (* TODO benchmark *) @@ -1597,7 +666,7 @@ module Cost_of = struct end module Typechecking = struct - open Generated_costs + open Michelson_v1_gas_costs let public_key_optimized = atomic_step_cost @@ -1716,7 +785,7 @@ module Cost_of = struct end module Unparsing = struct - open Generated_costs + open Michelson_v1_gas_costs let public_key_optimized = atomic_step_cost diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas_costs.ml b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs.ml new file mode 100644 index 000000000000..bd9b3c2dc46d --- /dev/null +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs.ml @@ -0,0 +1,29 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. *) +(* Copyright (c) 2019-2022 Nomadic Labs *) +(* Copyright (c) 2020 Metastate AG *) +(* Copyright (c) 2022 DaiLambda, Inc. *) +(* *) +(* 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 diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml new file mode 100644 index 000000000000..c11352c266d6 --- /dev/null +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml @@ -0,0 +1,948 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. *) +(* Copyright (c) 2019-2022 Nomadic Labs *) +(* Copyright (c) 2020 Metastate AG *) +(* Copyright (c) 2022 DaiLambda, Inc. *) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + +module S = Saturation_repr +module S_syntax = S.Syntax + +(* Automatically generated costs functions. *) + +(* model N_IAbs_int *) +(* Allocates [size] bytes. *) +let cost_N_IAbs_int size = S.safe_int (20 + (size lsr 1)) + +(* model N_IAdd_bls12_381_fr *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_IAdd_bls12_381_fr = S.safe_int 30 + +(* model N_IAdd_bls12_381_g1 *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_IAdd_bls12_381_g1 = S.safe_int 900 + +(* model N_IAdd_bls12_381_g2 *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_IAdd_bls12_381_g2 = S.safe_int 2_470 + +(* Allocates [max size1 size2] *) +let cost_linear_op_int size1 size2 = + let open S_syntax in + let v0 = S.safe_int (Compare.Int.max size1 size2) in + S.safe_int 35 + (v0 lsr 1) + +(* model N_IAdd_int *) +(* Approximating 0.078154 x term *) +let cost_N_IAdd_int = cost_linear_op_int + +(* model N_IAdd_nat *) +(* Approximating 0.077807 x term *) +let cost_N_IAdd_nat = cost_linear_op_int + +(* model N_IAdd_seconds_to_timestamp *) +(* Approximating 0.078056 x term *) +let cost_N_IAdd_seconds_to_timestamp = cost_linear_op_int + +(* model N_IAdd_tez *) +let cost_N_IAdd_tez = S.safe_int 20 + +(* model N_IAdd_timestamp_to_seconds *) +(* Approximating 0.077771 x term *) +let cost_N_IAdd_timestamp_to_seconds = cost_linear_op_int + +(* model N_IAddress *) +let cost_N_IAddress = S.safe_int 10 + +(* model N_IAmount *) +let cost_N_IAmount = S.safe_int 10 + +(* model N_IAnd *) +let cost_N_IAnd = S.safe_int 10 + +(* model N_IAnd_int_nat *) +(* Allocates [min size1 size2] *) +let cost_N_IAnd_int_nat size1 size2 = + let open S_syntax in + let v0 = S.safe_int (Compare.Int.min size1 size2) in + S.safe_int 35 + (v0 lsr 1) + +(* model N_IAnd_nat *) +(* Allocates [min size1 size2] *) +let cost_N_IAnd_nat size1 size2 = + let open S_syntax in + let v0 = S.safe_int (Compare.Int.min size1 size2) in + S.safe_int 35 + (v0 lsr 1) + +(* model N_IApply *) +let cost_N_IApply rec_flag = + if rec_flag then S.safe_int 220 else S.safe_int 140 + +(* model N_IBalance *) +let cost_N_IBalance = S.safe_int 10 + +(* model N_IBlake2b *) +(* Approximating 1.120804 x term *) +let cost_N_IBlake2b size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 430 + v0 + (v0 lsr 3) + +(* model N_IBytes_size *) +let cost_N_IBytes_size = S.safe_int 10 + +(* model N_ICar *) +let cost_N_ICar = S.safe_int 10 + +(* model N_ICdr *) +let cost_N_ICdr = S.safe_int 10 + +(* model N_IChainId *) +let cost_N_IChainId = S.safe_int 15 + +(* model N_ICheck_signature_ed25519 *) +(* Approximating 1.123507 x term *) +let cost_N_ICheck_signature_ed25519 size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 65_800 + (v0 + (v0 lsr 3)) + +(* model N_ICheck_signature_p256 *) +(* Approximating 1.111539 x term *) +let cost_N_ICheck_signature_p256 size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 990_000 + (v0 + (v0 lsr 3)) + +(* model N_ICheck_signature_secp256k1 *) +(* Approximating 1.125404 x term *) +let cost_N_ICheck_signature_secp256k1 size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 51_600 + (v0 + (v0 lsr 3)) + +(* model N_IComb *) +(* Approximating 3.531001 x term *) +(* Note: size >= 2, so the cost is never 0 *) +let cost_N_IComb size = + let open S_syntax in + let v0 = S.safe_int size in + (S.safe_int 3 * v0) + (v0 lsr 1) + (v0 lsr 5) + +(* model N_IComb_get *) +(* Approximating 0.573180 x term *) +let cost_N_IComb_get size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 20 + (v0 lsr 1) + (v0 lsr 4) + +(* model N_IComb_set *) +(* Approximating 1.287531 x term *) +let cost_N_IComb_set size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 20 + (v0 + (v0 lsr 2) + (v0 lsr 5)) + +(* Model N_ICompare *) +(* Approximating 0.024413 x term *) +let cost_N_ICompare size1 size2 = + let open S_syntax in + let v0 = S.safe_int (Compare.Int.min size1 size2) in + S.safe_int 35 + ((v0 lsr 6) + (v0 lsr 7)) + +(* model N_IConcat_bytes_pair *) +(* Allocates [size1 + size2] *) +let cost_N_IConcat_bytes_pair size1 size2 = + let open S_syntax in + let v0 = S.safe_int size1 + S.safe_int size2 in + S.safe_int 45 + (v0 lsr 1) + +(* model N_IConcat_string_pair *) +(* Allocates [size1 + size2] *) +let cost_N_IConcat_string_pair size1 size2 = + let open S_syntax in + let v0 = S.safe_int size1 + S.safe_int size2 in + S.safe_int 45 + (v0 lsr 1) + +(* model N_ICons_list *) +let cost_N_ICons_list = S.safe_int 10 + +(* model N_ICons_none *) +let cost_N_ICons_none = S.safe_int 10 + +(* model N_ICons_pair *) +let cost_N_ICons_pair = S.safe_int 10 + +(* model N_ICons_some *) +let cost_N_ICons_some = S.safe_int 10 + +(* model N_IConst *) +let cost_N_IConst = S.safe_int 10 + +(* model N_IContract *) +let cost_N_IContract = S.safe_int 30 + +(* model N_ICreate_contract *) +let cost_N_ICreate_contract = S.safe_int 60 + +(* model N_IDiff_timestamps *) +(* Approximating 0.077922 x term *) +let cost_N_IDiff_timestamps = cost_linear_op_int + +(* model N_IDig *) +(* Approximating 6.750442 x term *) +let cost_N_IDig size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 30 + ((S.safe_int 6 * v0) + (v0 lsr 1) + (v0 lsr 2)) + +(* model N_IDip *) +let cost_N_IDip = S.safe_int 10 + +(* model N_IDipN *) +(* Approximating 4.05787663635 x term *) +let cost_N_IDipN size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 15 + (S.safe_int 4 * v0) + +(* model N_IView *) +let cost_N_IView = S.safe_int 1460 + +(* model N_IDrop *) +let cost_N_IDrop = S.safe_int 10 + +(* model N_IDropN *) +(* Approximating 2.713108 x term *) +let cost_N_IDropN size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 30 + (S.safe_int 2 * v0) + (v0 lsr 1) + (v0 lsr 3) + +(* model N_IDug *) +(* Approximating 6.718396 x term *) +let cost_N_IDug size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 35 + ((S.safe_int 6 * v0) + (v0 lsr 1) + (v0 lsr 2)) + +(* model N_IDup *) +let cost_N_IDup = S.safe_int 10 + +(* model N_IDupN *) +(* Approximating 1.222263 x term *) +let cost_N_IDupN size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 20 + v0 + (v0 lsr 2) + +let cost_div_int size1 size2 = + (* Allocates at most [size1] bytes *) + let open S_syntax in + let v1 = S.safe_int size1 in + let q = size1 - size2 in + if Compare.Int.(q < 0) then S.safe_int 105 + (v1 lsr 1) + else + let v0 = S.safe_int q * S.safe_int size2 in + S.safe_int 105 + (v0 lsr 10) + (v0 lsr 11) + (v0 lsr 13) + (v1 lsr 1) + +(* model N_IEdiv_int *) +(* Approximating 0.001591 x term *) +let cost_N_IEdiv_int = cost_div_int + +(* model N_IEdiv_nat *) +(* Approximating 0.001605 x term *) +let cost_N_IEdiv_nat = cost_div_int + +(* model N_IEdiv_tez *) +let cost_N_IEdiv_tez = S.safe_int 80 + +(* model N_IEdiv_teznat *) +let cost_N_IEdiv_teznat = S.safe_int 70 + +(* model N_IEmpty_big_map *) +let cost_N_IEmpty_big_map = S.safe_int 300 + +(* model N_IEmpty_map *) +let cost_N_IEmpty_map = S.safe_int 300 + +(* model N_IEmpty_set *) +let cost_N_IEmpty_set = S.safe_int 300 + +(* model N_IEq *) +let cost_N_IEq = S.safe_int 10 + +(* model N_IExec *) +let cost_N_IExec = S.safe_int 10 + +(* model N_IFailwith *) +(* let cost_N_IFailwith = S.safe_int 105 *) + +(* model N_IGe *) +let cost_N_IGe = S.safe_int 10 + +(* model N_IGt *) +let cost_N_IGt = S.safe_int 10 + +(* model N_IHalt *) +let cost_N_IHalt = S.safe_int 15 + +(* model N_IHash_key *) +let cost_N_IHash_key = S.safe_int 605 + +(* model N_IIf *) +let cost_N_IIf = S.safe_int 10 + +(* model N_IIf_cons *) +let cost_N_IIf_cons = S.safe_int 10 + +(* model N_IIf_left *) +let cost_N_IIf_left = S.safe_int 10 + +(* model N_IIf_none *) +let cost_N_IIf_none = S.safe_int 10 + +(* model N_IOpt_map *) +let cost_opt_map = S.safe_int 10 + +(* model N_IImplicit_account *) +let cost_N_IImplicit_account = S.safe_int 10 + +(* model N_IInt_bls12_381_z_fr *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_IInt_bls12_381_z_fr = S.safe_int 115 + +(* model N_IInt_nat *) +let cost_N_IInt_nat = S.safe_int 10 + +(* model N_IIs_nat *) +let cost_N_IIs_nat = S.safe_int 10 + +(* model N_IKeccak *) +(* Approximating 8.276352 x term *) +let cost_N_IKeccak size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 1350 + ((S.safe_int 8 * v0) + (v0 lsr 2)) + +(* model N_ILambda *) +let cost_N_ILambda = S.safe_int 10 + +(* model N_ILe *) +let cost_N_ILe = S.safe_int 10 + +(* model N_ILeft *) +let cost_N_ILeft = S.safe_int 10 + +(* model N_ILevel *) +let cost_N_ILevel = S.safe_int 10 + +(* model N_IList_iter *) +let cost_N_IList_iter _ = S.safe_int 20 + +(* model N_IList_map *) +let cost_N_IList_map _ = S.safe_int 20 + +(* model N_IList_size *) +let cost_N_IList_size = S.safe_int 10 + +(* model N_ILoop *) +let cost_N_ILoop = S.safe_int 10 + +(* model N_ILoop_left *) +let cost_N_ILoop_left = S.safe_int 10 + +(* model N_ILsl_nat *) +(* Allocates at most [size + 256] bytes *) +let cost_N_ILsl_nat size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 128 + (v0 lsr 1) + +(* model N_ILsr_nat *) +(* Allocates at most [size] bytes*) +let cost_N_ILsr_nat size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 45 + (v0 lsr 1) + +(* model N_ILt *) +let cost_N_ILt = S.safe_int 10 + +(* model N_IMap_get *) +(* Approximating 0.048359 x term *) +let cost_N_IMap_get size1 size2 = + let open S_syntax in + let v0 = size1 * log2 size2 in + S.safe_int 45 + (v0 lsr 5) + (v0 lsr 6) + +(* model N_IMap_get_and_update *) +(* Approximating 0.145661 x term *) +let cost_N_IMap_get_and_update size1 size2 = + let open S_syntax in + let v0 = size1 * log2 size2 in + S.safe_int 75 + (v0 lsr 3) + (v0 lsr 6) + +(* model N_IMap_iter *) +(* Approximating 7.621331 x term *) +let cost_N_IMap_iter size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 50 + (S.safe_int 7 * v0) + (v0 lsr 1) + (v0 lsr 3) + +(* model N_IMap_map *) +(* Approximating 8.38965386732 x term *) +let cost_N_IMap_map size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 40 + ((S.safe_int 8 * v0) + (v0 lsr 1)) + +(* model N_IMap_mem *) +(* Approximating 0.048446 x term *) +let cost_N_IMap_mem size1 size2 = + let open S_syntax in + let v0 = size1 * log2 size2 in + S.safe_int 45 + (v0 lsr 5) + (v0 lsr 6) + +(* model N_IMap_size *) +let cost_N_IMap_size = S.safe_int 10 + +(* model N_IMap_update *) +(* Approximating 0.097072 x term *) +let cost_N_IMap_update size1 size2 = + let open S_syntax in + let v0 = size1 * log2 size2 in + S.safe_int 55 + (v0 lsr 4) + (v0 lsr 5) + +(* model N_IMul_bls12_381_fr *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_IMul_bls12_381_fr = S.safe_int 45 + +(* model N_IMul_bls12_381_fr_z *) +(* Approximating 1.059386 x term *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_IMul_bls12_381_fr_z size1 = + let open S_syntax in + let v0 = S.safe_int size1 in + S.safe_int 265 + v0 + (v0 lsr 4) + +(* model N_IMul_bls12_381_g1 *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_IMul_bls12_381_g1 = S.safe_int 103_000 + +(* model N_IMul_bls12_381_g2 *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_IMul_bls12_381_g2 = S.safe_int 220_000 + +(* model N_IMul_bls12_381_z_fr *) +(* Approximating 1.068674 x term *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_IMul_bls12_381_z_fr size1 = + let open S_syntax in + let v0 = S.safe_int size1 in + S.safe_int 265 + v0 + (v0 lsr 4) + +let cost_mul size1 size2 = + let open S_syntax in + let a = S.add (S.safe_int size1) (S.safe_int size2) in + let v0 = a * log2 a in + S.safe_int 55 + (v0 lsr 1) + (v0 lsr 2) + (v0 lsr 4) + +(* model N_IMul_int *) +(* Approximating 0.857931 x term *) +let cost_N_IMul_int = cost_mul + +(* model N_IMul_nat *) +(* Approximating 0.861823 x term *) +let cost_N_IMul_nat = cost_mul + +(* 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 + +(* model N_INeg_bls12_381_fr *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_INeg_bls12_381_fr = S.safe_int 25 + +(* model N_INeg_bls12_381_g1 *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_INeg_bls12_381_g1 = S.safe_int 50 + +(* model N_INeg_bls12_381_g2 *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_INeg_bls12_381_g2 = S.safe_int 70 + +(* model N_INeg *) +(* Allocates [size] bytes *) +let cost_N_INeg size = + let open S_syntax in + S.safe_int 25 + (S.safe_int size lsr 1) + +(* model N_INeq *) +let cost_N_INeq = S.safe_int 10 + +(* model N_INil *) +let cost_N_INil = S.safe_int 10 + +(* model N_INot *) +let cost_N_INot = S.safe_int 10 + +(* model N_INot_int *) +(* Allocates [size] bytes *) +let cost_N_INot_int size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 25 + (v0 lsr 1) + +(* model N_INow *) +let cost_N_INow = S.safe_int 10 + +(* model N_IMin_block_time *) +let cost_N_IMin_block_time = S.safe_int 20 + +(* model N_IOpen_chest *) +(* 612000 + chest * 19 + time * 19050 *) +let cost_N_IOpen_chest ~chest ~time = + let open S_syntax in + let v0 = S.safe_int chest in + let v1 = S.safe_int time in + S.safe_int 612_000 + (S.safe_int 19 * v0) + (S.safe_int 19050 * v1) + +(* model N_IOr *) +let cost_N_IOr = S.safe_int 10 + +(* model N_IOr_nat *) +(* Approximating 0.075758 x term *) +let cost_N_IOr_nat = cost_linear_op_int + +(* model N_IPairing_check_bls12_381 *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_N_IPairing_check_bls12_381 size = + S.add (S.safe_int 450_000) (S.mul (S.safe_int 342_500) (S.safe_int size)) + +(* model N_IRead_ticket *) +let cost_N_IRead_ticket = S.safe_int 10 + +(* model N_IRight *) +let cost_N_IRight = S.safe_int 10 + +(* model N_ISapling_empty_state *) +let cost_N_ISapling_empty_state = S.safe_int 300 + +(* 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) + +(* model N_ISelf_address *) +let cost_N_ISelf_address = S.safe_int 10 + +(* model N_ISelf *) +let cost_N_ISelf = S.safe_int 10 + +(* model N_ISender *) +let cost_N_ISender = S.safe_int 10 + +(* model N_ISet_delegate *) +let cost_N_ISet_delegate = S.safe_int 60 + +(* model N_ISet_iter *) +(* Approximating 7.633555 x term *) +let cost_N_ISet_iter size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 50 + (S.safe_int 7 * v0) + (v0 lsr 1) + (v0 lsr 3) + +(* model N_ISet_size *) +let cost_N_ISet_size = S.safe_int 10 + +(* model N_ISha256 *) +(* Approximating 4.763264 x term *) +let cost_N_ISha256 size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 600 + ((S.safe_int 4 * v0) + (v0 lsr 1) + (v0 lsr 2)) + +(* model N_ISha3 *) +(* Approximating 8.362339 x term *) +let cost_N_ISha3 = cost_N_IKeccak + +(* model N_ISha512 *) +(* Approximating 3.074641 x term *) +let cost_N_ISha512 size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 680 + (S.safe_int 3 * v0) + +(* model N_ISlice_bytes *) +(* Allocates [size] bytes *) +let cost_N_ISlice_bytes size = + let open S_syntax in + S.safe_int 25 + (S.safe_int size lsr 1) + +(* model N_ISlice_string *) +(* Allocates [size] bytes *) +let cost_N_ISlice_string size = + let open S_syntax in + S.safe_int 25 + (S.safe_int size lsr 1) + +(* model N_ISource *) +let cost_N_ISource = S.safe_int 10 + +(* model N_ISplit_ticket *) +(* Allocates [max size1 size2] *) +let cost_N_ISplit_ticket size1 size2 = + let open S_syntax in + let v1 = S.safe_int (Compare.Int.max size1 size2) in + S.safe_int 40 + (v1 lsr 1) + +(* model N_IString_size *) +let cost_N_IString_size = S.safe_int 15 + +(* model N_ISub_int *) +(* Approximating 0.077849 x term *) +let cost_N_ISub_int = cost_linear_op_int + +(* model N_ISub_tez *) +let cost_N_ISub_tez = S.safe_int 15 + +(* model N_ISub_tez_legacy *) +let cost_N_ISub_tez_legacy = S.safe_int 20 + +(* model N_ISub_timestamp_seconds *) +(* Approximating 0.077794 x term *) +let cost_N_ISub_timestamp_seconds = cost_linear_op_int + +(* model N_ISwap *) +let cost_N_ISwap = S.safe_int 10 + +(* model N_ITicket *) +let cost_N_ITicket = S.safe_int 10 + +(* model N_ITotal_voting_power *) +let cost_N_ITotal_voting_power = S.safe_int 450 + +(* model N_ITransfer_tokens *) +let cost_N_ITransfer_tokens = S.safe_int 60 + +(* model N_IUncomb *) +(* Approximating 3.944710 x term *) +let cost_N_IUncomb size = + let open S_syntax in + let v0 = S.safe_int size in + S.safe_int 25 + (S.safe_int 4 * v0) + +(* model N_IUnpair *) +let cost_N_IUnpair = S.safe_int 10 + +(* model N_IVoting_power *) +let cost_N_IVoting_power = S.safe_int 640 + +(* model N_IXor *) +let cost_N_IXor = S.safe_int 15 + +(* model N_IXor_nat *) +(* Approximating 0.075601 x term *) +let cost_N_IXor_nat = cost_linear_op_int + +(* model N_KCons *) +let cost_N_KCons = S.safe_int 10 + +(* model N_KIter *) +let cost_N_KIter = S.safe_int 10 + +(* model N_KList_enter_body *) +(* Approximating 1.672196 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 25 + (v0 + (v0 lsr 1) + (v0 lsr 3)) + | _ :: _ -> S.safe_int 25 + +(* model N_KList_exit_body *) +let cost_N_KList_exit_body = S.safe_int 10 + +(* model N_KLoop_in *) +let cost_N_KLoop_in = S.safe_int 10 + +(* model N_KLoop_in_left *) +let cost_N_KLoop_in_left = S.safe_int 10 + +(* model N_KMap_enter_body *) +let cost_N_KMap_enter_body = S.safe_int 80 + +(* model N_KNil *) +let cost_N_KNil = S.safe_int 15 + +(* model N_KReturn *) +let cost_N_KReturn = S.safe_int 10 + +(* model N_KView_exit *) +let cost_N_KView_exit = S.safe_int 20 + +(* model N_KMap_head *) +let cost_N_KMap_head = S.safe_int 20 + +(* model N_KUndip *) +let cost_N_KUndip = S.safe_int 10 + +(* model DECODING_BLS_FR *) +(* when benchmarking, compile bls12-381 without ADX, see + https://gitlab.com/dannywillems/ocaml-bls12-381/-/blob/71d0b4d467fbfaa6452d702fcc408d7a70916a80/README.md#install +*) +let cost_DECODING_BLS_FR = S.safe_int 120 + +(* model DECODING_BLS_G1 *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_DECODING_BLS_G1 = S.safe_int 54_600 + +(* model DECODING_BLS_G2 *) +(* when benchmarking, compile bls12-381 without ADX *) +let cost_DECODING_BLS_G2 = S.safe_int 69_000 + +(* model B58CHECK_DECODING_CHAIN_ID *) +let cost_B58CHECK_DECODING_CHAIN_ID = S.safe_int 1_600 + +(* model B58CHECK_DECODING_PUBLIC_KEY_HASH_ed25519 *) +let cost_B58CHECK_DECODING_PUBLIC_KEY_HASH_ed25519 = S.safe_int 3_300 + +(* model B58CHECK_DECODING_PUBLIC_KEY_HASH_p256 *) +let cost_B58CHECK_DECODING_PUBLIC_KEY_HASH_p256 = S.safe_int 3_300 + +(* model B58CHECK_DECODING_PUBLIC_KEY_HASH_secp256k1 *) +let cost_B58CHECK_DECODING_PUBLIC_KEY_HASH_secp256k1 = S.safe_int 3_300 + +(* model B58CHECK_DECODING_PUBLIC_KEY_ed25519 *) +let cost_B58CHECK_DECODING_PUBLIC_KEY_ed25519 = S.safe_int 4_200 + +(* model B58CHECK_DECODING_PUBLIC_KEY_p256 *) +let cost_B58CHECK_DECODING_PUBLIC_KEY_p256 = S.safe_int 325_000 + +(* model B58CHECK_DECODING_PUBLIC_KEY_secp256k1 *) +let cost_B58CHECK_DECODING_PUBLIC_KEY_secp256k1 = S.safe_int 9_000 + +(* model B58CHECK_DECODING_SIGNATURE_ed25519 *) +let cost_B58CHECK_DECODING_SIGNATURE_ed25519 = S.safe_int 6_400 + +(* model B58CHECK_DECODING_SIGNATURE_p256 *) +let cost_B58CHECK_DECODING_SIGNATURE_p256 = S.safe_int 6_400 + +(* model B58CHECK_DECODING_SIGNATURE_secp256k1 *) +let cost_B58CHECK_DECODING_SIGNATURE_secp256k1 = S.safe_int 6_400 + +(* model ENCODING_BLS_FR *) +let cost_ENCODING_BLS_FR = S.safe_int 80 + +(* model ENCODING_BLS_G1 *) +let cost_ENCODING_BLS_G1 = S.safe_int 3200 + +(* model ENCODING_BLS_G2 *) +let cost_ENCODING_BLS_G2 = S.safe_int 3900 + +(* model B58CHECK_ENCODING_CHAIN_ID *) +let cost_B58CHECK_ENCODING_CHAIN_ID = S.safe_int 1_800 + +(* model B58CHECK_ENCODING_PUBLIC_KEY_HASH_ed25519 *) +let cost_B58CHECK_ENCODING_PUBLIC_KEY_HASH_ed25519 = S.safe_int 3_200 + +(* model B58CHECK_ENCODING_PUBLIC_KEY_HASH_p256 *) +let cost_B58CHECK_ENCODING_PUBLIC_KEY_HASH_p256 = S.safe_int 3_200 + +(* model B58CHECK_ENCODING_PUBLIC_KEY_HASH_secp256k1 *) +let cost_B58CHECK_ENCODING_PUBLIC_KEY_HASH_secp256k1 = S.safe_int 3_200 + +(* model B58CHECK_ENCODING_PUBLIC_KEY_ed25519 *) +let cost_B58CHECK_ENCODING_PUBLIC_KEY_ed25519 = S.safe_int 4_500 + +(* model B58CHECK_ENCODING_PUBLIC_KEY_p256 *) +let cost_B58CHECK_ENCODING_PUBLIC_KEY_p256 = S.safe_int 4_550 + +(* model B58CHECK_ENCODING_PUBLIC_KEY_secp256k1 *) +let cost_B58CHECK_ENCODING_PUBLIC_KEY_secp256k1 = S.safe_int 4_950 + +(* model B58CHECK_ENCODING_SIGNATURE_ed25519 *) +let cost_B58CHECK_ENCODING_SIGNATURE_ed25519 = S.safe_int 8_300 + +(* model B58CHECK_ENCODING_SIGNATURE_p256 *) +let cost_B58CHECK_ENCODING_SIGNATURE_p256 = S.safe_int 8_300 + +(* model B58CHECK_ENCODING_SIGNATURE_secp256k1 *) +let cost_B58CHECK_ENCODING_SIGNATURE_secp256k1 = S.safe_int 8_300 + +(* model DECODING_CHAIN_ID *) +let cost_DECODING_CHAIN_ID = S.safe_int 50 + +(* model DECODING_PUBLIC_KEY_HASH_ed25519 *) +let cost_DECODING_PUBLIC_KEY_HASH_ed25519 = S.safe_int 50 + +(* model DECODING_PUBLIC_KEY_HASH_p256 *) +let cost_DECODING_PUBLIC_KEY_HASH_p256 = S.safe_int 50 + +(* model DECODING_PUBLIC_KEY_HASH_secp256k1 *) +let cost_DECODING_PUBLIC_KEY_HASH_secp256k1 = S.safe_int 50 + +(* model DECODING_PUBLIC_KEY_ed25519 *) +let cost_DECODING_PUBLIC_KEY_ed25519 = S.safe_int 60 + +(* model DECODING_PUBLIC_KEY_p256 *) +let cost_DECODING_PUBLIC_KEY_p256 = S.safe_int 320_000 + +(* model DECODING_PUBLIC_KEY_secp256k1 *) +let cost_DECODING_PUBLIC_KEY_secp256k1 = S.safe_int 4_900 + +(* model DECODING_SIGNATURE_ed25519 *) +let cost_DECODING_SIGNATURE_ed25519 = S.safe_int 35 + +(* model DECODING_SIGNATURE_p256 *) +let cost_DECODING_SIGNATURE_p256 = S.safe_int 35 + +(* model DECODING_SIGNATURE_secp256k1 *) +let cost_DECODING_SIGNATURE_secp256k1 = S.safe_int 35 + +(* model DECODING_Chest_key *) +let cost_DECODING_Chest_key = S.safe_int 5900 + +(* model DECODING_Chest *) +(* Approximating 0.039349 x term *) +let cost_DECODING_Chest ~bytes = + let open S_syntax in + let v0 = S.safe_int bytes in + S.safe_int 7400 + (v0 lsr 5) + (v0 lsr 7) + +(* model ENCODING_CHAIN_ID *) +let cost_ENCODING_CHAIN_ID = S.safe_int 50 + +(* model ENCODING_PUBLIC_KEY_HASH_ed25519 *) +let cost_ENCODING_PUBLIC_KEY_HASH_ed25519 = S.safe_int 70 + +(* model ENCODING_PUBLIC_KEY_HASH_p256 *) +let cost_ENCODING_PUBLIC_KEY_HASH_p256 = S.safe_int 70 + +(* model ENCODING_PUBLIC_KEY_HASH_secp256k1 *) +let cost_ENCODING_PUBLIC_KEY_HASH_secp256k1 = S.safe_int 70 + +(* model ENCODING_PUBLIC_KEY_ed25519 *) +let cost_ENCODING_PUBLIC_KEY_ed25519 = S.safe_int 80 + +(* model ENCODING_PUBLIC_KEY_p256 *) +let cost_ENCODING_PUBLIC_KEY_p256 = S.safe_int 90 + +(* model ENCODING_PUBLIC_KEY_secp256k1 *) +let cost_ENCODING_PUBLIC_KEY_secp256k1 = S.safe_int 455 + +(* model ENCODING_SIGNATURE_ed25519 *) +let cost_ENCODING_SIGNATURE_ed25519 = S.safe_int 45 + +(* model ENCODING_SIGNATURE_p256 *) +let cost_ENCODING_SIGNATURE_p256 = S.safe_int 45 + +(* model ENCODING_SIGNATURE_secp256k1 *) +let cost_ENCODING_SIGNATURE_secp256k1 = S.safe_int 45 + +(* model ENCODING_Chest_key *) +let cost_ENCODING_Chest_key = S.safe_int 10_000 + +(* model ENCODING_Chest *) +(* Approximating 0.120086 x term *) +let cost_ENCODING_Chest ~plaintext_size = + let open S_syntax in + let v0 = S.safe_int plaintext_size in + S.safe_int 12_200 + (v0 lsr 3) + +(* model TIMESTAMP_READABLE_DECODING *) +(* Approximating 0.045400 x term *) +let cost_TIMESTAMP_READABLE_DECODING ~bytes = + let open S_syntax in + let b = S.safe_int bytes in + let v0 = S.mul (S.sqrt b) b in + S.safe_int 105 + ((v0 lsr 5) + (v0 lsr 6)) + +(* model TIMESTAMP_READABLE_ENCODING *) +let cost_TIMESTAMP_READABLE_ENCODING = S.safe_int 820 + +(* model CHECK_PRINTABLE *) +let cost_CHECK_PRINTABLE size = + let open S_syntax in + S.safe_int 14 + (S.safe_int 10 * S.safe_int size) + +(* 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 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 + +(* 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 + +(* 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 + +(* 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)) + +(* model IEmit *) +let cost_N_IEmit = S.safe_int 30 diff --git a/src/proto_alpha/lib_protocol/saturation_repr.ml b/src/proto_alpha/lib_protocol/saturation_repr.ml index db134564a4f1..5c17c2f32d48 100644 --- a/src/proto_alpha/lib_protocol/saturation_repr.ml +++ b/src/proto_alpha/lib_protocol/saturation_repr.ml @@ -175,3 +175,14 @@ let z_encoding = Data_encoding.(check_size 9 (conv to_z t_to_z_exn z)) let n_encoding = Data_encoding.(check_size 9 (conv to_z t_to_z_exn n)) let pp fmt x = Format.pp_print_int fmt x + +module Syntax = struct + (* This is a good enough approximation. S.log2 0 = 1 *) + let log2 x = safe_int (1 + numbits x) + + let ( + ) = add + + let ( * ) = mul + + let ( lsr ) = shift_right +end diff --git a/src/proto_alpha/lib_protocol/saturation_repr.mli b/src/proto_alpha/lib_protocol/saturation_repr.mli index 4831b070e15b..ff52c0eeb537 100644 --- a/src/proto_alpha/lib_protocol/saturation_repr.mli +++ b/src/proto_alpha/lib_protocol/saturation_repr.mli @@ -208,3 +208,14 @@ val n_encoding : _ t Data_encoding.t (** A pretty-printer for native integers. *) val pp : Format.formatter -> _ t -> unit + +(** Syntax for simple representations. *) +module Syntax : sig + val log2 : _ t -> may_saturate t + + val ( + ) : _ t -> _ t -> may_saturate t + + val ( * ) : _ t -> _ t -> may_saturate t + + val ( lsr ) : 'a t -> int -> 'a t +end -- GitLab From 2efe894222cbcb5e22217d91ba186ef8f0e24817 Mon Sep 17 00:00:00 2001 From: Sota Sato Date: Wed, 28 Sep 2022 16:05:33 +0900 Subject: [PATCH 2/4] proto: expose some operators in saturation_repr.Syntax --- .../lib_protocol/michelson_v1_gas_costs_generated.ml | 2 +- src/proto_alpha/lib_protocol/saturation_repr.ml | 10 ++++++++++ src/proto_alpha/lib_protocol/saturation_repr.mli | 10 ++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml index c11352c266d6..635dbb131f9c 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml @@ -259,9 +259,9 @@ let cost_N_IDupN size = let cost_div_int size1 size2 = (* Allocates at most [size1] bytes *) + let q = size1 - size2 in let open S_syntax in let v1 = S.safe_int size1 in - let q = size1 - size2 in if Compare.Int.(q < 0) then S.safe_int 105 + (v1 lsr 1) else let v0 = S.safe_int q * S.safe_int size2 in diff --git a/src/proto_alpha/lib_protocol/saturation_repr.ml b/src/proto_alpha/lib_protocol/saturation_repr.ml index 5c17c2f32d48..8310cd25d111 100644 --- a/src/proto_alpha/lib_protocol/saturation_repr.ml +++ b/src/proto_alpha/lib_protocol/saturation_repr.ml @@ -180,9 +180,19 @@ module Syntax = struct (* This is a good enough approximation. S.log2 0 = 1 *) let log2 x = safe_int (1 + numbits x) + let sqrt = sqrt + let ( + ) = add + let ( - ) = sub + let ( * ) = mul + let ( < ) = ( < ) + + let ( = ) = ( = ) + let ( lsr ) = shift_right + + let ( lsl ) = shift_left end diff --git a/src/proto_alpha/lib_protocol/saturation_repr.mli b/src/proto_alpha/lib_protocol/saturation_repr.mli index ff52c0eeb537..4aeceae43d1f 100644 --- a/src/proto_alpha/lib_protocol/saturation_repr.mli +++ b/src/proto_alpha/lib_protocol/saturation_repr.mli @@ -213,9 +213,19 @@ val pp : Format.formatter -> _ t -> unit module Syntax : sig val log2 : _ t -> may_saturate t + val sqrt : _ t -> may_saturate t + val ( + ) : _ t -> _ t -> may_saturate t + val ( - ) : _ t -> _ t -> may_saturate t + val ( * ) : _ t -> _ t -> may_saturate t + val ( < ) : _ t -> _ t -> bool + + val ( = ) : _ t -> _ t -> bool + val ( lsr ) : 'a t -> int -> 'a t + + val ( lsl ) : 'a t -> int -> 'a t end -- GitLab From 321371521f4ceccf67c95e45a09cabb1b882107c Mon Sep 17 00:00:00 2001 From: Sota Sato Date: Wed, 28 Sep 2022 16:10:11 +0900 Subject: [PATCH 3/4] proto: arrange arguments of gas cost functions for auto-generation --- .../lib_protocol/michelson_v1_gas.ml | 26 +++++++++---------- .../lib_protocol/michelson_v1_gas.mli | 2 ++ .../michelson_v1_gas_costs_generated.ml | 22 +++++++++++----- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas.ml b/src/proto_alpha/lib_protocol/michelson_v1_gas.ml index 6c88513c5783..d31ddd7e1e55 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_gas.ml +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas.ml @@ -56,7 +56,7 @@ module Cost_of = struct let if_none = atomic_step_cost cost_N_IIf_none - let opt_map = atomic_step_cost cost_opt_map + let opt_map = atomic_step_cost cost_N_IOpt_map let cons_pair = atomic_step_cost cost_N_ICons_pair @@ -79,12 +79,12 @@ module Cost_of = struct let if_cons = atomic_step_cost cost_N_IIf_cons let list_map : 'a Script_typed_ir.boxed_list -> Gas.cost = - fun {length; _} -> atomic_step_cost (cost_N_IList_map length) + fun _ -> atomic_step_cost cost_N_IList_map let list_size = atomic_step_cost cost_N_IList_size let list_iter : 'a Script_typed_ir.boxed_list -> Gas.cost = - fun {length; _} -> atomic_step_cost (cost_N_IList_iter length) + fun _ -> atomic_step_cost cost_N_IList_iter let empty_set = atomic_step_cost cost_N_IEmpty_set @@ -106,20 +106,19 @@ module Cost_of = struct let map_size = atomic_step_cost cost_N_IMap_size - let big_map_elt_size = S.safe_int Script_expr_hash.size + let big_map_elt_size = Script_expr_hash.size let big_map_mem ({size; _} : _ Script_typed_ir.big_map_overlay) = - atomic_step_cost (cost_N_IMap_mem big_map_elt_size (S.safe_int size)) + atomic_step_cost (cost_N_IMap_mem big_map_elt_size size) let big_map_get ({size; _} : _ Script_typed_ir.big_map_overlay) = - atomic_step_cost (cost_N_IMap_get big_map_elt_size (S.safe_int size)) + atomic_step_cost (cost_N_IMap_get big_map_elt_size size) let big_map_update ({size; _} : _ Script_typed_ir.big_map_overlay) = - atomic_step_cost (cost_N_IMap_update big_map_elt_size (S.safe_int size)) + atomic_step_cost (cost_N_IMap_update big_map_elt_size size) let big_map_get_and_update ({size; _} : _ Script_typed_ir.big_map_overlay) = - atomic_step_cost - (cost_N_IMap_get_and_update big_map_elt_size (S.safe_int size)) + atomic_step_cost (cost_N_IMap_get_and_update big_map_elt_size size) let add_seconds_timestamp : 'a Script_int.num -> Script_timestamp.t -> Gas.cost = @@ -397,7 +396,7 @@ module Cost_of = struct Script_typed_ir.Script_timelock.get_plaintext_size chest in let log_time = Z.log2 Z.(add one time) in - atomic_step_cost (cost_N_IOpen_chest ~chest:plaintext ~time:log_time) + atomic_step_cost (cost_N_IOpen_chest log_time plaintext) (* --------------------------------------------------------------------- *) (* Semi-hand-crafted models *) @@ -763,8 +762,7 @@ module Cost_of = struct let unit = free let timestamp_readable s = - atomic_step_cost - (cost_TIMESTAMP_READABLE_DECODING ~bytes:(String.length s)) + atomic_step_cost (cost_TIMESTAMP_READABLE_DECODING (String.length s)) (** TODO: https://gitlab.com/tezos/tezos/-/issues/2340 Refine the gas model *) @@ -781,7 +779,7 @@ module Cost_of = struct let chest_key = atomic_step_cost cost_DECODING_Chest_key - let chest ~bytes = atomic_step_cost (cost_DECODING_Chest ~bytes) + let chest ~bytes = atomic_step_cost (cost_DECODING_Chest bytes) end module Unparsing = struct @@ -900,7 +898,7 @@ module Cost_of = struct let chest_key = atomic_step_cost cost_ENCODING_Chest_key let chest ~plaintext_size = - atomic_step_cost (cost_ENCODING_Chest ~plaintext_size) + atomic_step_cost (cost_ENCODING_Chest plaintext_size) end end diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas.mli b/src/proto_alpha/lib_protocol/michelson_v1_gas.mli index 59b4cd7df69c..d15851a04ad6 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_gas.mli +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas.mli @@ -69,10 +69,12 @@ module Cost_of : sig val if_cons : Gas.cost + (* The argument of this function is ignored when calculating gas cost. *) val list_map : 'a Script_typed_ir.boxed_list -> Gas.cost val list_size : Gas.cost + (* The argument of this function is ignored when calculating gas cost. *) val list_iter : 'a Script_typed_ir.boxed_list -> Gas.cost val empty_set : Gas.cost diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml index 635dbb131f9c..7486ee8d864f 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml @@ -324,7 +324,7 @@ let cost_N_IIf_left = S.safe_int 10 let cost_N_IIf_none = S.safe_int 10 (* model N_IOpt_map *) -let cost_opt_map = S.safe_int 10 +let cost_N_IOpt_map = S.safe_int 10 (* model N_IImplicit_account *) let cost_N_IImplicit_account = S.safe_int 10 @@ -359,10 +359,10 @@ let cost_N_ILeft = S.safe_int 10 let cost_N_ILevel = S.safe_int 10 (* model N_IList_iter *) -let cost_N_IList_iter _ = S.safe_int 20 +let cost_N_IList_iter = S.safe_int 20 (* model N_IList_map *) -let cost_N_IList_map _ = S.safe_int 20 +let cost_N_IList_map = S.safe_int 20 (* model N_IList_size *) let cost_N_IList_size = S.safe_int 10 @@ -394,6 +394,8 @@ let cost_N_ILt = S.safe_int 10 (* Approximating 0.048359 x term *) let cost_N_IMap_get size1 size2 = let open S_syntax in + let size1 = S.safe_int size1 in + let size2 = S.safe_int size2 in let v0 = size1 * log2 size2 in S.safe_int 45 + (v0 lsr 5) + (v0 lsr 6) @@ -401,6 +403,8 @@ let cost_N_IMap_get size1 size2 = (* Approximating 0.145661 x term *) let cost_N_IMap_get_and_update size1 size2 = let open S_syntax in + let size1 = S.safe_int size1 in + let size2 = S.safe_int size2 in let v0 = size1 * log2 size2 in S.safe_int 75 + (v0 lsr 3) + (v0 lsr 6) @@ -422,6 +426,8 @@ let cost_N_IMap_map size = (* Approximating 0.048446 x term *) let cost_N_IMap_mem size1 size2 = let open S_syntax in + let size1 = S.safe_int size1 in + let size2 = S.safe_int size2 in let v0 = size1 * log2 size2 in S.safe_int 45 + (v0 lsr 5) + (v0 lsr 6) @@ -432,6 +438,8 @@ let cost_N_IMap_size = S.safe_int 10 (* Approximating 0.097072 x term *) let cost_N_IMap_update size1 size2 = let open S_syntax in + let size1 = S.safe_int size1 in + let size2 = S.safe_int size2 in let v0 = size1 * log2 size2 in S.safe_int 55 + (v0 lsr 4) + (v0 lsr 5) @@ -525,7 +533,7 @@ let cost_N_IMin_block_time = S.safe_int 20 (* model N_IOpen_chest *) (* 612000 + chest * 19 + time * 19050 *) -let cost_N_IOpen_chest ~chest ~time = +let cost_N_IOpen_chest time chest = let open S_syntax in let v0 = S.safe_int chest in let v1 = S.safe_int time in @@ -833,7 +841,7 @@ let cost_DECODING_Chest_key = S.safe_int 5900 (* model DECODING_Chest *) (* Approximating 0.039349 x term *) -let cost_DECODING_Chest ~bytes = +let cost_DECODING_Chest bytes = let open S_syntax in let v0 = S.safe_int bytes in S.safe_int 7400 + (v0 lsr 5) + (v0 lsr 7) @@ -873,14 +881,14 @@ let cost_ENCODING_Chest_key = S.safe_int 10_000 (* model ENCODING_Chest *) (* Approximating 0.120086 x term *) -let cost_ENCODING_Chest ~plaintext_size = +let cost_ENCODING_Chest plaintext_size = let open S_syntax in let v0 = S.safe_int plaintext_size in S.safe_int 12_200 + (v0 lsr 3) (* model TIMESTAMP_READABLE_DECODING *) (* Approximating 0.045400 x term *) -let cost_TIMESTAMP_READABLE_DECODING ~bytes = +let cost_TIMESTAMP_READABLE_DECODING bytes = let open S_syntax in let b = S.safe_int bytes in let v0 = S.mul (S.sqrt b) b in -- GitLab From 7ca67442407c87375a6c8414fed83f7ad3e81179 Mon Sep 17 00:00:00 2001 From: Sota Sato Date: Wed, 28 Sep 2022 17:05:17 +0900 Subject: [PATCH 4/4] proto: move some gas cost functions that cannot be auto-generated --- .../lib_protocol/michelson_v1_gas_costs.ml | 124 ++++++++++++++++++ .../michelson_v1_gas_costs_generated.ml | 91 +------------ 2 files changed, 126 insertions(+), 89 deletions(-) diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas_costs.ml b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs.ml index bd9b3c2dc46d..8f741bdefb2b 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_gas_costs.ml +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs.ml @@ -27,3 +27,127 @@ (*****************************************************************************) 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.672196 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 25 + (v0 + (v0 lsr 1) + (v0 lsr 3)) + | _ :: _ -> S.safe_int 25 + +(* 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)) diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml index 7486ee8d864f..f05cb2c5dc38 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas_costs_generated.ml @@ -29,7 +29,8 @@ module S = Saturation_repr module S_syntax = S.Syntax -(* Automatically generated costs functions. *) +(* This file is planned to be automatically generated. *) +(* If you want to update the following functions, update the gas model instead. *) (* model N_IAbs_int *) (* Allocates [size] bytes. *) @@ -95,10 +96,6 @@ let cost_N_IAnd_nat size1 size2 = let v0 = S.safe_int (Compare.Int.min size1 size2) in S.safe_int 35 + (v0 lsr 1) -(* model N_IApply *) -let cost_N_IApply rec_flag = - if rec_flag then S.safe_int 220 else S.safe_int 140 - (* model N_IBalance *) let cost_N_IBalance = S.safe_int 10 @@ -278,9 +275,6 @@ let cost_N_IEdiv_nat = cost_div_int (* model N_IEdiv_tez *) let cost_N_IEdiv_tez = S.safe_int 80 -(* model N_IEdiv_teznat *) -let cost_N_IEdiv_teznat = S.safe_int 70 - (* model N_IEmpty_big_map *) let cost_N_IEmpty_big_map = S.safe_int 300 @@ -485,12 +479,6 @@ let cost_N_IMul_int = cost_mul (* Approximating 0.861823 x term *) let cost_N_IMul_nat = cost_mul -(* 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 - (* model N_INeg_bls12_381_fr *) (* when benchmarking, compile bls12-381 without ADX *) let cost_N_INeg_bls12_381_fr = S.safe_int 25 @@ -560,15 +548,6 @@ let cost_N_IRight = S.safe_int 10 (* model N_ISapling_empty_state *) let cost_N_ISapling_empty_state = S.safe_int 300 -(* 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) - (* model N_ISelf_address *) let cost_N_ISelf_address = S.safe_int 10 @@ -683,19 +662,6 @@ let cost_N_IXor_nat = cost_linear_op_int (* model N_KCons *) let cost_N_KCons = S.safe_int 10 -(* model N_KIter *) -let cost_N_KIter = S.safe_int 10 - -(* model N_KList_enter_body *) -(* Approximating 1.672196 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 25 + (v0 + (v0 lsr 1) + (v0 lsr 3)) - | _ :: _ -> S.safe_int 25 - (* model N_KList_exit_body *) let cost_N_KList_exit_body = S.safe_int 10 @@ -705,9 +671,6 @@ let cost_N_KLoop_in = S.safe_int 10 (* model N_KLoop_in_left *) let cost_N_KLoop_in_left = S.safe_int 10 -(* model N_KMap_enter_body *) -let cost_N_KMap_enter_body = S.safe_int 80 - (* model N_KNil *) let cost_N_KNil = S.safe_int 15 @@ -902,55 +865,5 @@ let cost_CHECK_PRINTABLE size = let open S_syntax in S.safe_int 14 + (S.safe_int 10 * S.safe_int size) -(* 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 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 - -(* 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 - -(* 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 - -(* 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)) - (* model IEmit *) let cost_N_IEmit = S.safe_int 30 -- GitLab