package tezos-protocol-008-PtEdo2Zk

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file michelson_v1_gas.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2019-2020 Nomadic Labs <contact@nomadic-labs.com>           *)
(* Copyright (c) 2020 Metastate AG <hello@metastate.dev>                     *)
(*                                                                           *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Alpha_context
open Gas

module Cost_of = struct
  module Z_syntax = struct
    (* This is a good enough approximation. Z.numbits 0 = 0 *)
    let log2 x = Z.of_int (1 + Z.numbits x)

    let ( + ) = Z.add

    let ( * ) = Z.mul

    let ( lsr ) = Z.shift_right
  end

  let z_bytes (z : Z.t) =
    let bits = Z.numbits z in
    (7 + bits) / 8

  let int_bytes (z : 'a Script_int.num) = z_bytes (Script_int.to_zint z)

  let timestamp_bytes (t : Script_timestamp.t) =
    let z = Script_timestamp.to_zint t in
    z_bytes z

  (* Upper-bound on the time to compare the given value.
     For now, returns size in bytes, but this could get more complicated... *)
  let rec size_of_comparable :
      type a. a Script_typed_ir.comparable_ty -> a -> Z.t =
   fun wit v ->
    match (wit, v) with
    | (Unit_key _, _) ->
        Z.of_int 1
    | (Never_key _, _) ->
        .
    | (Int_key _, _) ->
        Z.of_int (int_bytes v)
    | (Nat_key _, _) ->
        Z.of_int (int_bytes v)
    | (Signature_key _, _) ->
        Z.of_int Signature.size
    | (String_key _, _) ->
        Z.of_int (String.length v)
    | (Bytes_key _, _) ->
        Z.of_int (Bytes.length v)
    | (Bool_key _, _) ->
        Z.of_int 8
    | (Key_hash_key _, _) ->
        Z.of_int Signature.Public_key_hash.size
    | (Key_key _, k) ->
        Z.of_int (Signature.Public_key.size k)
    | (Timestamp_key _, _) ->
        Z.of_int (timestamp_bytes v)
    | (Address_key _, _) ->
        Z.of_int Signature.Public_key_hash.size
    | (Mutez_key _, _) ->
        Z.of_int 8
    | (Chain_id_key _, _) ->
        Z.of_int Chain_id.size
    | (Pair_key ((l, _), (r, _), _), (lval, rval)) ->
        Z.add (size_of_comparable l lval) (size_of_comparable r rval)
    | (Union_key ((t, _), _, _), L x) ->
        Z.add (Z.of_int 1) (size_of_comparable t x)
    | (Union_key (_, (t, _), _), R x) ->
        Z.add (Z.of_int 1) (size_of_comparable t x)
    | (Option_key _, None) ->
        Z.of_int 1
    | (Option_key (t, _), Some x) ->
        Z.add (Z.of_int 1) (size_of_comparable t x)

  let manager_operation = step_cost @@ Z.of_int 1_000

  (* FIXME: hardcoded constant, available in next environment version.
     Set to a reasonable upper bound. *)
  let public_key_size = 64

  module Generated_costs_007 = struct
    (* Automatically generated costs functions. *)

    (* model N_Abs_int *)
    (* Approximating 0.068306 x term *)
    let cost_N_Abs_int size = Z.of_int @@ (80 + (size lsr 4))

    (* model N_Add_bls12_381_fr *)

    let cost_N_Add_bls12_381_fr = Z.of_int 230

    (* model N_Add_bls12_381_g1 *)

    let cost_N_Add_bls12_381_g1 = Z.of_int 9_300

    (* model N_Add_bls12_381_g2 *)

    let cost_N_Add_bls12_381_g2 = Z.of_int 13_000

    (* model N_Add_intint *)
    (* Approximating 0.082158 x term *)
    let cost_N_Add_intint size1 size2 =
      let v0 = Compare.Int.max size1 size2 in
      Z.of_int (80 + ((v0 lsr 4) + (v0 lsr 6)))

    (* model N_Add_tez *)
    let cost_N_Add_tez = Z.of_int 100

    (* model N_And *)
    let cost_N_And = Z.of_int 100

    (* model N_And_nat *)
    (* Approximating 0.079325 x term *)
    let cost_N_And_nat size1 size2 =
      let v0 = Compare.Int.min size1 size2 in
      Z.of_int (80 + ((v0 lsr 4) + (v0 lsr 6)))

    (* model N_Blake2b *)
    (* Approximating 1.366428 x term *)
    let cost_N_Blake2b size =
      let open Z_syntax in
      let size = Z.of_int size in
      Z.of_int 500 + (size + (size lsr 2))

    (* model N_Car *)
    let cost_N_Car = Z.of_int 80

    (* model N_Cdr *)
    let cost_N_Cdr = Z.of_int 80

    (* model N_Check_signature_ed25519 *)
    (* Approximating 1.372685 x term *)
    let cost_N_Check_signature_ed25519 size =
      let open Z_syntax in
      let size = Z.of_int size in
      Z.of_int 270_000 + (size + (size lsr 2))

    (* model N_Check_signature_p256 *)
    (* Approximating 1.385771 x term *)
    let cost_N_Check_signature_p256 size =
      let open Z_syntax in
      let size = Z.of_int size in
      Z.of_int 600_000 + (size + (size lsr 2) + (size lsr 3))

    (* model N_Check_signature_secp256k1 *)
    (* Approximating 1.372411 x term *)
    let cost_N_Check_signature_secp256k1 size =
      let open Z_syntax in
      let size = Z.of_int size in
      Z.of_int 60_000 + (size + (size lsr 2))

    (* model N_Comb *)
    (* Approximating 3.275337 x term *)
    let cost_N_Comb size = Z.of_int (80 + ((3 * size) + (size lsr 2)))

    (* model N_Comb_get *)
    (* Approximating 0.553178 x term *)
    let cost_N_Comb_get size = Z.of_int (80 + ((size lsr 1) + (size lsr 4)))

    (* model N_Comb_set *)
    (* Approximating 1.282976 x term *)
    let cost_N_Comb_set size = Z.of_int (80 + (size + (size lsr 2)))

    (* model N_Compare_address *)
    let cost_N_Compare_address size1 size2 =
      Z.of_int (80 + (2 * Compare.Int.min size1 size2))

    (* model N_Compare_bool *)
    let cost_N_Compare_bool size1 size2 =
      Z.of_int (80 + (128 * Compare.Int.min size1 size2))

    (* model N_Compare_int *)
    (* Approximating 0.073657 x term *)
    let cost_N_Compare_int size1 size2 =
      let v0 = Compare.Int.min size1 size2 in
      Z.of_int (150 + ((v0 lsr 4) + (v0 lsr 7)))

    (* model N_Compare_key_hash *)
    let cost_N_Compare_key_hash size1 size2 =
      Z.of_int (80 + (2 * Compare.Int.min size1 size2))

    (* model N_Compare_mutez *)
    let cost_N_Compare_mutez size1 size2 =
      Z.of_int (13 * Compare.Int.min size1 size2)

    (* model N_Compare_string *)
    (* Approximating 0.039389 x term *)
    let cost_N_Compare_string size1 size2 =
      let v0 = Compare.Int.min size1 size2 in
      Z.of_int (120 + ((v0 lsr 5) + (v0 lsr 7)))

    (* model N_Compare_timestamp *)
    (* Approximating 0.072483 x term *)
    let cost_N_Compare_timestamp size1 size2 =
      let v0 = Compare.Int.min size1 size2 in
      Z.of_int (140 + ((v0 lsr 4) + (v0 lsr 7)))

    (* model N_Concat_string_pair *)
    (* Approximating 0.068808 x term *)
    let cost_N_Concat_string_pair size1 size2 =
      let open Z_syntax in
      let v0 = Z.of_int size1 + Z.of_int size2 in
      Z.of_int 80 + (v0 lsr 4)

    (* model N_Cons_list *)
    let cost_N_Cons_list = Z.of_int 80

    (* model N_Cons_none *)
    let cost_N_Cons_none = Z.of_int 80

    (* model N_Cons_pair *)
    let cost_N_Cons_pair = Z.of_int 80

    (* model N_Cons_some *)
    let cost_N_Cons_some = Z.of_int 80

    (* model N_Const *)
    let cost_N_Const = Z.of_int 80

    (* model N_Dig *)
    let cost_N_Dig size = Z.of_int (100 + (4 * size))

    (* model N_Dip *)
    let cost_N_Dip = Z.of_int 100

    (* model N_DipN *)
    let cost_N_DipN size = Z.of_int (100 + (4 * size))

    (* model N_Drop *)
    let cost_N_Drop = Z.of_int 80

    (* model N_DropN *)
    let cost_N_DropN size = Z.of_int (100 + (4 * size))

    (* model N_Dug *)
    let cost_N_Dug size = Z.of_int (100 + (4 * size))

    (* model N_Dup *)
    let cost_N_Dup = Z.of_int 80

    (* model N_DupN *)
    (* Approximating 1.299969 x term *)
    let cost_N_DupN size = Z.of_int (60 + size + (size lsr 2))

    (* model N_Ediv_natnat *)
    (* Approximating 0.001599 x term *)
    let cost_N_Ediv_natnat size1 size2 =
      let q = size1 - size2 in
      if Compare.Int.(q < 0) then Z.of_int 300
      else
        let open Z_syntax in
        let v0 = Z.of_int q * Z.of_int size2 in
        Z.of_int 300 + (v0 lsr 10) + (v0 lsr 11) + (v0 lsr 13)

    (* model N_Ediv_tez *)
    let cost_N_Ediv_tez = Z.of_int 200

    (* model N_Ediv_teznat *)
    (* Extracted by hand from the empirical data *)
    let cost_N_Ediv_teznat = Z.of_int 300

    (* model N_Empty_map *)
    let cost_N_Empty_map = Z.of_int 240

    (* model N_Empty_set *)
    let cost_N_Empty_set = Z.of_int 240

    (* model N_Eq *)
    let cost_N_Eq = Z.of_int 80

    (* model N_If *)
    let cost_N_If = Z.of_int 60

    (* model N_If_cons *)
    let cost_N_If_cons = Z.of_int 110

    (* model N_If_left *)
    let cost_N_If_left = Z.of_int 90

    (* model N_If_none *)
    let cost_N_If_none = Z.of_int 80

    (* model N_Int_nat *)
    let cost_N_Int_nat = Z.of_int 80

    (* model N_Is_nat *)
    let cost_N_Is_nat = Z.of_int 80

    (* model N_Keccak *)
    let cost_N_Keccak size =
      let open Z_syntax in
      Z.of_int 1_400 + (Z.of_int 30 * Z.of_int size)

    (* model N_Left *)
    let cost_N_Left = Z.of_int 80

    (* model N_List_iter *)
    let cost_N_List_iter size =
      let open Z_syntax in
      Z.of_int 500 + (Z.of_int 7 * Z.of_int size)

    (* model N_List_map *)
    let cost_N_List_map size =
      let open Z_syntax in
      Z.of_int 500 + (Z.of_int 12 * Z.of_int size)

    (* model N_List_size *)
    let cost_N_List_size = Z.of_int 80

    (* model N_Loop *)
    let cost_N_Loop = Z.of_int 70

    (* model N_Loop_left *)
    let cost_N_Loop_left = Z.of_int 80

    (* model N_Lsl_nat *)
    (* Approximating 0.129443 x term *)
    let cost_N_Lsl_nat size = Z.of_int (150 + (size lsr 3))

    (* model N_Lsr_nat *)
    (* Approximating 0.129435 x term *)
    let cost_N_Lsr_nat size = Z.of_int (150 + (size lsr 3))

    (* model N_Map_get *)
    (* Approximating 0.057548 x term *)
    let cost_N_Map_get size1 size2 =
      let open Z_syntax in
      let v0 = size1 * log2 (Z.of_int size2) in
      Z.of_int 80 + (v0 lsr 5) + (v0 lsr 6) + (v0 lsr 7)

    (* model N_Map_iter *)
    let cost_N_Map_iter size =
      let open Z_syntax in
      Z.of_int 80 + (Z.of_int 40 * Z.of_int size)

    (* model N_Map_map *)
    let cost_N_Map_map size =
      let open Z_syntax in
      Z.of_int 80 + (Z.of_int 761 * Z.of_int size)

    (* model N_Map_mem *)
    (* Approximating 0.058563 x term *)
    let cost_N_Map_mem size1 size2 =
      let open Z_syntax in
      let v0 = size1 * log2 (Z.of_int size2) in
      Z.of_int 80 + (v0 lsr 5) + (v0 lsr 6) + (v0 lsr 7)

    (* model N_Map_size *)
    let cost_N_Map_size = Z.of_int 90

    (* model N_Map_update *)
    (* Approximating 0.119968 x term *)
    let cost_N_Map_update size1 size2 =
      let open Z_syntax in
      let v0 = size1 * log2 (Z.of_int size2) in
      Z.of_int 80 + (v0 lsr 4) + (v0 lsr 5) + (v0 lsr 6) + (v0 lsr 7)

    (* model N_Mul_bls12_381_fr *)

    let cost_N_Mul_bls12_381_fr = Z.of_int 260

    (* model N_Mul_bls12_381_g1 *)

    let cost_N_Mul_bls12_381_g1 = Z.of_int 265_000

    (* model N_Mul_bls12_381_g2 *)

    let cost_N_Mul_bls12_381_g2 = Z.of_int 850_000

    (* Converting fr from/to Z.t *)
    let cost_bls12_381_fr_of_z = Z.of_int 130

    let cost_bls12_381_fr_to_z = Z.of_int 30

    let cost_N_Mul_bls12_381_fr_z =
      Z.add cost_bls12_381_fr_of_z cost_N_Mul_bls12_381_fr

    let cost_N_Int_bls12_381_fr = cost_bls12_381_fr_to_z

    (* model N_Mul_intint *)
    let cost_N_Mul_intint size1 size2 =
      let open Z_syntax in
      let a = Z.of_int size1 + Z.of_int size2 in
      Z.of_int 80 + (a * log2 a)

    (* model N_Mul_teznat *)
    let cost_N_Mul_teznat size =
      let open Z_syntax in
      Z.of_int 200 + (Z.of_int 133 * Z.of_int size)

    (* model N_Neg_bls12_381_fr *)

    let cost_N_Neg_bls12_381_fr = Z.of_int 180

    (* model N_Neg_bls12_381_g1 *)

    let cost_N_Neg_bls12_381_g1 = Z.of_int 410

    (* model N_Neg_bls12_381_g2 *)

    let cost_N_Neg_bls12_381_g2 = Z.of_int 715

    (* model N_Neg_int *)
    (* Approximating 0.068419 x term *)
    let cost_N_Neg_int size = Z.of_int (80 + (size lsr 4))

    (* model N_Neq *)
    let cost_N_Neq = Z.of_int 80

    (* model N_Nil *)
    let cost_N_Nil = Z.of_int 80

    (* model N_Nop *)
    let cost_N_Nop = Z.of_int 70

    (* model N_Not *)
    let cost_N_Not = Z.of_int 90

    (* model N_Not_int *)
    (* Approximating 0.076564 x term *)
    let cost_N_Not_int size = Z.of_int (55 + ((size lsr 4) + (size lsr 7)))

    (* model N_Or *)
    let cost_N_Or = Z.of_int 90

    (* model N_Or_nat *)
    (* Approximating 0.078718 x term *)
    let cost_N_Or_nat size1 size2 =
      let v0 = Compare.Int.max size1 size2 in
      Z.of_int (80 + ((v0 lsr 4) + (v0 lsr 6)))

    (* model N_Pairing_check_bls12_381 *)

    let cost_N_Pairing_check_bls12_381 size =
      Z.add (Z.of_int 1_550_000) (Z.mul (Z.of_int 510_000) (Z.of_int size))

    (* model N_Right *)
    let cost_N_Right = Z.of_int 80

    (* model N_Seq *)
    let cost_N_Seq = Z.of_int 60

    (* model N_Set_iter *)
    let cost_N_Set_iter size =
      let open Z_syntax in
      Z.of_int 80 + (Z.of_int 36 * Z.of_int size)

    (* model N_Set_mem *)
    (* Approximating 0.059410 x term *)
    let cost_N_Set_mem size1 size2 =
      let open Z_syntax in
      let v0 = size1 * log2 (Z.of_int size2) in
      Z.of_int 80 + (v0 lsr 5) + (v0 lsr 6) + (v0 lsr 7) + (v0 lsr 8)

    (* model N_Set_size *)
    let cost_N_Set_size = Z.of_int 80

    (* model N_Set_update *)
    (* Approximating 0.126260 x term *)
    let cost_N_Set_update size1 size2 =
      let open Z_syntax in
      let v0 = size1 * log2 (Z.of_int size2) in
      Z.of_int 80 + (v0 lsr 3)

    (* model N_Sha256 *)
    let cost_N_Sha256 size =
      let open Z_syntax in
      Z.of_int 500 + (Z.of_int 5 * Z.of_int size)

    (* model N_Sha3 *)
    let cost_N_Sha3 size =
      let open Z_syntax in
      Z.of_int 1_400 + (Z.of_int 32 * Z.of_int size)

    (* model N_Sha512 *)
    let cost_N_Sha512 size =
      let open Z_syntax in
      Z.of_int 500 + (Z.of_int 3 * Z.of_int size)

    (* model N_Slice_string *)
    (* Approximating 0.067048 x term *)
    let cost_N_Slice_string size = Z.of_int (80 + (size lsr 4))

    (* model N_String_size *)
    let cost_N_String_size = Z.of_int 80

    (* model N_Sub_int *)
    (* Approximating 0.082399 x term *)
    let cost_N_Sub_int size1 size2 =
      let v0 = Compare.Int.max size1 size2 in
      Z.of_int (80 + ((v0 lsr 4) + (v0 lsr 6)))

    (* model N_Sub_tez *)
    let cost_N_Sub_tez = Z.of_int 80

    (* model N_Swap *)
    let cost_N_Swap = Z.of_int 70

    (* model N_Total_voting_power *)
    let cost_N_Total_voting_power = Z.of_int 400

    (* model N_Uncomb *)
    (* Approximating 3.666332 x term *)
    let cost_N_Uncomb size =
      Z.of_int (80 + ((3 * size) + (size lsr 1) + (size lsr 3)))

    (* model N_Unpair *)
    let cost_N_Unpair = Z.of_int 80

    (* model N_Voting_power *)
    let cost_N_Voting_power = Z.of_int 400

    (* model N_Xor *)
    let cost_N_Xor = Z.of_int 100

    (* model N_Xor_nat *)
    (* Approximating 0.078258 x term *)
    let cost_N_Xor_nat size1 size2 =
      let v0 = Compare.Int.max size1 size2 in
      Z.of_int (80 + ((v0 lsr 4) + (v0 lsr 6)))

    (* model DECODING_BLS_FR *)

    let cost_DECODING_BLS_FR = Z.of_int 50

    (* model DECODING_BLS_G1 *)

    let cost_DECODING_BLS_G1 = Z.of_int 230_000

    (* model DECODING_BLS_G2 *)

    let cost_DECODING_BLS_G2 = Z.of_int 740_000

    (* model B58CHECK_DECODING_CHAIN_ID *)
    let cost_B58CHECK_DECODING_CHAIN_ID = Z.of_int 1_500

    (* model B58CHECK_DECODING_PUBLIC_KEY_HASH_ed25519 *)
    let cost_B58CHECK_DECODING_PUBLIC_KEY_HASH_ed25519 = Z.of_int 3_300

    (* model B58CHECK_DECODING_PUBLIC_KEY_HASH_p256 *)
    let cost_B58CHECK_DECODING_PUBLIC_KEY_HASH_p256 = Z.of_int 3_300

    (* model B58CHECK_DECODING_PUBLIC_KEY_HASH_secp256k1 *)
    let cost_B58CHECK_DECODING_PUBLIC_KEY_HASH_secp256k1 = Z.of_int 3_300

    (* model B58CHECK_DECODING_PUBLIC_KEY_ed25519 *)
    let cost_B58CHECK_DECODING_PUBLIC_KEY_ed25519 = Z.of_int 4_300

    (* model B58CHECK_DECODING_PUBLIC_KEY_p256 *)
    let cost_B58CHECK_DECODING_PUBLIC_KEY_p256 = Z.of_int 29_000

    (* model B58CHECK_DECODING_PUBLIC_KEY_secp256k1 *)
    let cost_B58CHECK_DECODING_PUBLIC_KEY_secp256k1 = Z.of_int 9_400

    (* model B58CHECK_DECODING_SIGNATURE_ed25519 *)
    let cost_B58CHECK_DECODING_SIGNATURE_ed25519 = Z.of_int 6_600

    (* model B58CHECK_DECODING_SIGNATURE_p256 *)
    let cost_B58CHECK_DECODING_SIGNATURE_p256 = Z.of_int 6_600

    (* model B58CHECK_DECODING_SIGNATURE_secp256k1 *)
    let cost_B58CHECK_DECODING_SIGNATURE_secp256k1 = Z.of_int 6_600

    (* model ENCODING_BLS_FR *)

    let cost_ENCODING_BLS_FR = Z.of_int 30

    (* model ENCODING_BLS_G1 *)

    let cost_ENCODING_BLS_G1 = Z.of_int 30

    (* model ENCODING_BLS_G2 *)

    let cost_ENCODING_BLS_G2 = Z.of_int 30

    (* model B58CHECK_ENCODING_CHAIN_ID *)
    let cost_B58CHECK_ENCODING_CHAIN_ID = Z.of_int 1_600

    (* model B58CHECK_ENCODING_PUBLIC_KEY_HASH_ed25519 *)
    let cost_B58CHECK_ENCODING_PUBLIC_KEY_HASH_ed25519 = Z.of_int 3_300

    (* model B58CHECK_ENCODING_PUBLIC_KEY_HASH_p256 *)
    let cost_B58CHECK_ENCODING_PUBLIC_KEY_HASH_p256 = Z.of_int 3_750

    (* model B58CHECK_ENCODING_PUBLIC_KEY_HASH_secp256k1 *)
    let cost_B58CHECK_ENCODING_PUBLIC_KEY_HASH_secp256k1 = Z.of_int 3_300

    (* model B58CHECK_ENCODING_PUBLIC_KEY_ed25519 *)
    let cost_B58CHECK_ENCODING_PUBLIC_KEY_ed25519 = Z.of_int 4_500

    (* model B58CHECK_ENCODING_PUBLIC_KEY_p256 *)
    let cost_B58CHECK_ENCODING_PUBLIC_KEY_p256 = Z.of_int 5_300

    (* model B58CHECK_ENCODING_PUBLIC_KEY_secp256k1 *)
    let cost_B58CHECK_ENCODING_PUBLIC_KEY_secp256k1 = Z.of_int 5_000

    (* model B58CHECK_ENCODING_SIGNATURE_ed25519 *)
    let cost_B58CHECK_ENCODING_SIGNATURE_ed25519 = Z.of_int 8_700

    (* model B58CHECK_ENCODING_SIGNATURE_p256 *)
    let cost_B58CHECK_ENCODING_SIGNATURE_p256 = Z.of_int 8_700

    (* model B58CHECK_ENCODING_SIGNATURE_secp256k1 *)
    let cost_B58CHECK_ENCODING_SIGNATURE_secp256k1 = Z.of_int 8_700

    (* model DECODING_CHAIN_ID *)
    let cost_DECODING_CHAIN_ID = Z.of_int 50

    (* model DECODING_PUBLIC_KEY_HASH_ed25519 *)
    let cost_DECODING_PUBLIC_KEY_HASH_ed25519 = Z.of_int 50

    (* model DECODING_PUBLIC_KEY_HASH_p256 *)
    let cost_DECODING_PUBLIC_KEY_HASH_p256 = Z.of_int 60

    (* model DECODING_PUBLIC_KEY_HASH_secp256k1 *)
    let cost_DECODING_PUBLIC_KEY_HASH_secp256k1 = Z.of_int 60

    (* model DECODING_PUBLIC_KEY_ed25519 *)
    let cost_DECODING_PUBLIC_KEY_ed25519 = Z.of_int 60

    (* model DECODING_PUBLIC_KEY_p256 *)
    let cost_DECODING_PUBLIC_KEY_p256 = Z.of_int 25_000

    (* model DECODING_PUBLIC_KEY_secp256k1 *)
    let cost_DECODING_PUBLIC_KEY_secp256k1 = Z.of_int 5_300

    (* model DECODING_SIGNATURE_ed25519 *)
    let cost_DECODING_SIGNATURE_ed25519 = Z.of_int 30

    (* model DECODING_SIGNATURE_p256 *)
    let cost_DECODING_SIGNATURE_p256 = Z.of_int 30

    (* model DECODING_SIGNATURE_secp256k1 *)
    let cost_DECODING_SIGNATURE_secp256k1 = Z.of_int 30

    (* model ENCODING_CHAIN_ID *)
    let cost_ENCODING_CHAIN_ID = Z.of_int 50

    (* model ENCODING_PUBLIC_KEY_HASH_ed25519 *)
    let cost_ENCODING_PUBLIC_KEY_HASH_ed25519 = Z.of_int 70

    (* model ENCODING_PUBLIC_KEY_HASH_p256 *)
    let cost_ENCODING_PUBLIC_KEY_HASH_p256 = Z.of_int 80

    (* model ENCODING_PUBLIC_KEY_HASH_secp256k1 *)
    let cost_ENCODING_PUBLIC_KEY_HASH_secp256k1 = Z.of_int 70

    (* model ENCODING_PUBLIC_KEY_ed25519 *)
    let cost_ENCODING_PUBLIC_KEY_ed25519 = Z.of_int 80

    (* model ENCODING_PUBLIC_KEY_p256 *)
    let cost_ENCODING_PUBLIC_KEY_p256 = Z.of_int 450

    (* model ENCODING_PUBLIC_KEY_secp256k1 *)
    let cost_ENCODING_PUBLIC_KEY_secp256k1 = Z.of_int 490

    (* model ENCODING_SIGNATURE_ed25519 *)
    let cost_ENCODING_SIGNATURE_ed25519 = Z.of_int 40

    (* model ENCODING_SIGNATURE_p256 *)
    let cost_ENCODING_SIGNATURE_p256 = Z.of_int 40

    (* model ENCODING_SIGNATURE_secp256k1 *)
    let cost_ENCODING_SIGNATURE_secp256k1 = Z.of_int 40

    (* model TIMESTAMP_READABLE_DECODING *)
    let cost_TIMESTAMP_READABLE_DECODING = Z.of_int 130

    (* model TIMESTAMP_READABLE_ENCODING *)
    let cost_TIMESTAMP_READABLE_ENCODING = Z.of_int 900

    (* model CHECK_PRINTABLE *)
    let cost_CHECK_PRINTABLE size =
      let open Z_syntax in
      Z.of_int 14 + (Z.of_int 10 * Z.of_int size)

    (* model MERGE_TYPES
       This is the estimated cost of one iteration of merge_types, extracted
       and copied manually from the parameter fit for the MERGE_TYPES benchmark
       (the model is parametric on the size of the type, which we don't have
       access to in O(1)). *)
    let cost_MERGE_TYPES = Z.of_int 130

    (* 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 = Z.of_int 375

    (* 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 = Z.of_int 200

    (* 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 = Z.of_int 240

    (* 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 = Z.of_int 140

    (* 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 = Z.of_int 170

    (* 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 = Z.of_int 185

    (* TODO: benchmark *)
    let cost_COMPARABLE_TY_OF_TY = Z.of_int 120
  end

  module Interpreter = struct
    open Generated_costs_007

    let drop = atomic_step_cost cost_N_Drop

    let dup = atomic_step_cost cost_N_Dup

    let swap = atomic_step_cost cost_N_Swap

    let push = atomic_step_cost cost_N_Const

    let cons_some = atomic_step_cost cost_N_Cons_some

    let cons_none = atomic_step_cost cost_N_Cons_none

    let if_none = atomic_step_cost cost_N_If_none

    let cons_pair = atomic_step_cost cost_N_Cons_pair

    let unpair = atomic_step_cost cost_N_Unpair

    let car = atomic_step_cost cost_N_Car

    let cdr = atomic_step_cost cost_N_Cdr

    let cons_left = atomic_step_cost cost_N_Left

    let cons_right = atomic_step_cost cost_N_Right

    let if_left = atomic_step_cost cost_N_If_left

    let cons_list = atomic_step_cost cost_N_Cons_list

    let nil = atomic_step_cost cost_N_Nil

    let if_cons = atomic_step_cost cost_N_If_cons

    let list_map : 'a Script_typed_ir.boxed_list -> Gas.cost =
     fun {length; _} -> atomic_step_cost (cost_N_List_map length)

    let list_size = atomic_step_cost cost_N_List_size

    let list_iter : 'a Script_typed_ir.boxed_list -> Gas.cost =
     fun {length; _} -> atomic_step_cost (cost_N_List_iter length)

    let empty_set = atomic_step_cost cost_N_Empty_set

    let set_iter (type a) ((module Box) : a Script_typed_ir.set) =
      atomic_step_cost (cost_N_Set_iter Box.size)

    let set_mem (type a) (elt : a) ((module Box) : a Script_typed_ir.set) =
      let elt_size = size_of_comparable Box.elt_ty elt in
      atomic_step_cost (cost_N_Set_mem elt_size Box.size)

    let set_update (type a) (elt : a) ((module Box) : a Script_typed_ir.set) =
      let elt_size = size_of_comparable Box.elt_ty elt in
      atomic_step_cost (cost_N_Set_update elt_size Box.size)

    let set_size = atomic_step_cost cost_N_Set_size

    let empty_map = atomic_step_cost cost_N_Empty_map

    let map_map (type k v) ((module Box) : (k, v) Script_typed_ir.map) =
      atomic_step_cost (cost_N_Map_map (snd Box.boxed))

    let map_iter (type k v) ((module Box) : (k, v) Script_typed_ir.map) =
      atomic_step_cost (cost_N_Map_iter (snd Box.boxed))

    let map_mem (type k v) (elt : k)
        ((module Box) : (k, v) Script_typed_ir.map) =
      let elt_size = size_of_comparable Box.key_ty elt in
      atomic_step_cost (cost_N_Map_mem elt_size (snd Box.boxed))

    let map_get (type k v) (elt : k)
        ((module Box) : (k, v) Script_typed_ir.map) =
      let elt_size = size_of_comparable Box.key_ty elt in
      atomic_step_cost (cost_N_Map_get elt_size (snd Box.boxed))

    let map_update (type k v) (elt : k)
        ((module Box) : (k, v) Script_typed_ir.map) =
      let elt_size = size_of_comparable Box.key_ty elt in
      atomic_step_cost (cost_N_Map_update elt_size (snd Box.boxed))

    let map_get_and_update (type k v) (elt : k)
        (m : (k, v) Script_typed_ir.map) =
      map_get elt m +@ map_update elt m

    let map_size = atomic_step_cost cost_N_Map_size

    let add_seconds_timestamp :
        'a Script_int.num -> Script_timestamp.t -> Gas.cost =
     fun seconds timestamp ->
      let seconds_bytes = int_bytes seconds in
      let timestamp_bytes = z_bytes (Script_timestamp.to_zint timestamp) in
      atomic_step_cost (cost_N_Add_intint seconds_bytes timestamp_bytes)

    let sub_seconds_timestamp :
        'a Script_int.num -> Script_timestamp.t -> Gas.cost =
     fun seconds timestamp ->
      let seconds_bytes = int_bytes seconds in
      let timestamp_bytes = z_bytes (Script_timestamp.to_zint timestamp) in
      atomic_step_cost (cost_N_Sub_int seconds_bytes timestamp_bytes)

    let diff_timestamps t1 t2 =
      let t1_bytes = z_bytes (Script_timestamp.to_zint t1) in
      let t2_bytes = z_bytes (Script_timestamp.to_zint t2) in
      atomic_step_cost (cost_N_Sub_int t1_bytes t2_bytes)

    let concat_string_pair s1 s2 =
      atomic_step_cost
        (cost_N_Concat_string_pair (String.length s1) (String.length s2))

    let slice_string s =
      atomic_step_cost (cost_N_Slice_string (String.length s))

    let string_size = atomic_step_cost cost_N_String_size

    let concat_bytes_pair b1 b2 =
      atomic_step_cost
        (cost_N_Concat_string_pair (Bytes.length b1) (Bytes.length b2))

    let slice_bytes b = atomic_step_cost (cost_N_Slice_string (Bytes.length b))

    let bytes_size = atomic_step_cost cost_N_String_size

    let add_tez = atomic_step_cost cost_N_Add_tez

    let sub_tez = atomic_step_cost cost_N_Sub_tez

    let mul_teznat n = atomic_step_cost (cost_N_Mul_teznat (int_bytes n))

    let bool_or = atomic_step_cost cost_N_Or

    let bool_and = atomic_step_cost cost_N_And

    let bool_xor = atomic_step_cost cost_N_Xor

    let bool_not = atomic_step_cost cost_N_Not

    let is_nat = atomic_step_cost cost_N_Is_nat

    let abs_int i = atomic_step_cost (cost_N_Abs_int (int_bytes i))

    let int_nat = atomic_step_cost cost_N_Int_nat

    let neg_int i = atomic_step_cost (cost_N_Neg_int (int_bytes i))

    let neg_nat n = atomic_step_cost (cost_N_Neg_int (int_bytes n))

    let add_bigint i1 i2 =
      atomic_step_cost (cost_N_Add_intint (int_bytes i1) (int_bytes i2))

    let sub_bigint i1 i2 =
      atomic_step_cost (cost_N_Sub_int (int_bytes i1) (int_bytes i2))

    let mul_bigint i1 i2 =
      atomic_step_cost (cost_N_Mul_intint (int_bytes i1) (int_bytes i2))

    let ediv_teznat _tez _n = atomic_step_cost cost_N_Ediv_teznat

    let ediv_tez = atomic_step_cost cost_N_Ediv_tez

    let ediv_bigint i1 i2 =
      atomic_step_cost (cost_N_Ediv_natnat (int_bytes i1) (int_bytes i2))

    let eq = atomic_step_cost cost_N_Eq

    let lsl_nat shifted = atomic_step_cost (cost_N_Lsl_nat (int_bytes shifted))

    let lsr_nat shifted = atomic_step_cost (cost_N_Lsr_nat (int_bytes shifted))

    let or_nat n1 n2 =
      atomic_step_cost (cost_N_Or_nat (int_bytes n1) (int_bytes n2))

    let and_nat n1 n2 =
      atomic_step_cost (cost_N_And_nat (int_bytes n1) (int_bytes n2))

    let xor_nat n1 n2 =
      atomic_step_cost (cost_N_Xor_nat (int_bytes n1) (int_bytes n2))

    let not_int i = atomic_step_cost (cost_N_Not_int (int_bytes i))

    let not_nat = not_int

    let seq = atomic_step_cost cost_N_Seq

    let if_ = atomic_step_cost cost_N_If

    let loop = atomic_step_cost cost_N_Loop

    let loop_left = atomic_step_cost cost_N_Loop_left

    let dip = atomic_step_cost cost_N_Dip

    let check_signature (pkey : Signature.public_key) b =
      let cost =
        match pkey with
        | Ed25519 _ ->
            cost_N_Check_signature_ed25519 (Bytes.length b)
        | Secp256k1 _ ->
            cost_N_Check_signature_secp256k1 (Bytes.length b)
        | P256 _ ->
            cost_N_Check_signature_p256 (Bytes.length b)
      in
      atomic_step_cost cost

    let blake2b b = atomic_step_cost (cost_N_Blake2b (Bytes.length b))

    let sha256 b = atomic_step_cost (cost_N_Sha256 (Bytes.length b))

    let sha512 b = atomic_step_cost (cost_N_Sha512 (Bytes.length b))

    let dign n = atomic_step_cost (cost_N_Dig n)

    let dugn n = atomic_step_cost (cost_N_Dug n)

    let dipn n = atomic_step_cost (cost_N_DipN n)

    let dropn n = atomic_step_cost (cost_N_DropN n)

    let voting_power = atomic_step_cost cost_N_Voting_power

    let total_voting_power = atomic_step_cost cost_N_Total_voting_power

    let keccak b = atomic_step_cost (cost_N_Keccak (Bytes.length b))

    let sha3 b = atomic_step_cost (cost_N_Sha3 (Bytes.length b))

    let add_bls12_381_g1 = atomic_step_cost cost_N_Add_bls12_381_g1

    let add_bls12_381_g2 = atomic_step_cost cost_N_Add_bls12_381_g2

    let add_bls12_381_fr = atomic_step_cost cost_N_Add_bls12_381_fr

    let mul_bls12_381_g1 = atomic_step_cost cost_N_Mul_bls12_381_g1

    let mul_bls12_381_g2 = atomic_step_cost cost_N_Mul_bls12_381_g2

    let mul_bls12_381_fr = atomic_step_cost cost_N_Mul_bls12_381_fr

    let mul_bls12_381_fr_z = atomic_step_cost cost_N_Mul_bls12_381_fr_z

    let int_bls12_381_fr = atomic_step_cost cost_N_Int_bls12_381_fr

    let neg_bls12_381_g1 = atomic_step_cost cost_N_Neg_bls12_381_g1

    let neg_bls12_381_g2 = atomic_step_cost cost_N_Neg_bls12_381_g2

    let neg_bls12_381_fr = atomic_step_cost cost_N_Neg_bls12_381_fr

    let neq = atomic_step_cost cost_N_Neq

    let nop = atomic_step_cost cost_N_Nop

    let pairing_check_bls12_381 (l : 'a Script_typed_ir.boxed_list) =
      atomic_step_cost (cost_N_Pairing_check_bls12_381 l.length)

    let comb n = atomic_step_cost (cost_N_Comb n)

    let uncomb n = atomic_step_cost (cost_N_Uncomb n)

    let comb_get n = atomic_step_cost (cost_N_Comb_get n)

    let comb_set n = atomic_step_cost (cost_N_Comb_set n)

    let dupn n = atomic_step_cost (cost_N_DupN n)

    let sapling_verify_update ~inputs ~outputs =
      let open Z_syntax in
      atomic_step_cost
        ( Z.of_int 85_000
        + (Z.of_int inputs * Z.of_int 4)
        + (Z.of_int outputs * Z.of_int 30) )

    (* --------------------------------------------------------------------- *)
    (* Semi-hand-crafted models *)
    let compare_unit = atomic_step_cost (Z.of_int 10)

    let compare_union_tag = atomic_step_cost (Z.of_int 10)

    let compare_option_tag = atomic_step_cost (Z.of_int 10)

    let compare_bool = atomic_step_cost (cost_N_Compare_bool 1 1)

    let compare_signature = atomic_step_cost (Z.of_int 92)

    let compare_string s1 s2 =
      atomic_step_cost
        (cost_N_Compare_string (String.length s1) (String.length s2))

    let compare_bytes b1 b2 =
      atomic_step_cost
        (cost_N_Compare_string (Bytes.length b1) (Bytes.length b2))

    let compare_mutez = atomic_step_cost (cost_N_Compare_mutez 8 8)

    let compare_int i1 i2 =
      atomic_step_cost (cost_N_Compare_int (int_bytes i1) (int_bytes i2))

    let compare_nat n1 n2 =
      atomic_step_cost (cost_N_Compare_int (int_bytes n1) (int_bytes n2))

    let compare_key_hash =
      let sz = Signature.Public_key_hash.size in
      atomic_step_cost (cost_N_Compare_key_hash sz sz)

    let compare_key = atomic_step_cost (Z.of_int 92)

    let compare_timestamp t1 t2 =
      atomic_step_cost
        (cost_N_Compare_timestamp
           (z_bytes (Script_timestamp.to_zint t1))
           (z_bytes (Script_timestamp.to_zint t2)))

    let compare_address =
      let sz = Signature.Public_key_hash.size + Chain_id.size in
      atomic_step_cost (cost_N_Compare_address sz sz)

    let compare_chain_id = atomic_step_cost (Z.of_int 30)

    let rec compare : type a. a Script_typed_ir.comparable_ty -> a -> a -> cost
        =
     fun ty x y ->
      match ty with
      | Unit_key _ ->
          compare_unit
      | Never_key _ -> (
        match x with _ -> . )
      | Bool_key _ ->
          compare_bool
      | String_key _ ->
          compare_string x y
      | Signature_key _ ->
          compare_signature
      | Bytes_key _ ->
          compare_bytes x y
      | Mutez_key _ ->
          compare_mutez
      | Int_key _ ->
          compare_int x y
      | Nat_key _ ->
          compare_nat x y
      | Key_hash_key _ ->
          compare_key_hash
      | Key_key _ ->
          compare_key
      | Timestamp_key _ ->
          compare_timestamp x y
      | Address_key _ ->
          compare_address
      | Chain_id_key _ ->
          compare_chain_id
      | Pair_key ((tl, _), (tr, _), _) ->
          (* Reasonable over-approximation of the cost of lexicographic comparison. *)
          let (xl, xr) = x in
          let (yl, yr) = y in
          compare tl xl yl +@ compare tr xr yr
      | Union_key ((tl, _), (tr, _), _) -> (
          compare_union_tag
          +@
          match (x, y) with
          | (L x, L y) ->
              compare tl x y
          | (L _, R _) ->
              free
          | (R _, L _) ->
              free
          | (R x, R y) ->
              compare tr x y )
      | Option_key (t, _) -> (
          compare_option_tag
          +@
          match (x, y) with
          | (None, None) ->
              free
          | (None, Some _) ->
              free
          | (Some _, None) ->
              free
          | (Some x, Some y) ->
              compare t x y )

    (* --------------------------------------------------------------------- *)
    (* Hand-crafted models *)

    (* The cost functions below where not benchmarked, a cost model was derived
       from looking at similar instructions. *)

    let sapling_empty_state = empty_map

    (* Cost for Concat_string is paid in two steps: when entering the interpreter,
       the user pays for the cost of computing the information necessary to compute
       the actual gas (so it's meta-gas): indeed, one needs to run through the
       list of strings to compute the total allocated cost.
       [concat_string_precheck] corresponds to the meta-gas cost of this computation.
     *)
    let concat_string_precheck (l : 'a Script_typed_ir.boxed_list) =
      (* we set the precheck to be slightly more expensive than cost_N_List_iter *)
      atomic_step_cost (Z.mul (Z.of_int l.length) (Z.of_int 10))

    (* This is the cost of allocating a string and blitting existing ones into it. *)
    let concat_string total_bytes =
      atomic_step_cost
        Z.(add (of_int 100) (fst (ediv_rem total_bytes (of_int 10))))

    (* Same story as Concat_string. *)
    let concat_bytes total_bytes =
      atomic_step_cost
        Z.(add (of_int 100) (fst (ediv_rem total_bytes (of_int 10))))

    (* Cost of additional call to logger + overhead of setting up call to [interp]. *)
    let exec = atomic_step_cost (Z.of_int 100)

    (* Heavy computation happens in the [unparse_data], [unparse_ty]
       functions which are carbonated. We must account for allocating
       the Micheline lambda wrapper. *)
    let apply = atomic_step_cost (Z.of_int 1000)

    (* Pushing a pointer on the stack. *)
    let lambda = push

    (* Pusing an address on the stack. *)
    let address = push

    (* Most computation happens in [parse_contract_from_script], which is carbonated.
       Account for pushing on the stack. *)
    let contract = push

    (* Most computation happens in [collect_lazy_storage], [extract_lazy_storage_diff]
       and [unparse_data] which are carbonated. The instruction-specific overhead
       is mostly that of updating the internal nonce, which we approximate by the
       cost of a push. *)
    let transfer_tokens = Gas.(push +@ push)

    (* Wrapping a value and pushing it on the stack. *)
    let implicit_account = push

    (* As for [transfer_token], most computation happens elsewhere.
       We still account for the overhead of updating the internal_nonce. *)
    let create_contract = Gas.(push +@ push)

    (* Increments the internal_nonce counter. *)
    let set_delegate = Gas.(push +@ push)

    (* Cost of access taken care of in Contract_storage.get_balance_carbonated *)
    let balance = Gas.free

    (* Accessing the raw_context, Small arithmetic & pushing on the stack. *)
    let level = atomic_step_cost (Z.mul (Z.of_int 2) cost_N_Const)

    (* Same as [cost_level] *)
    let now = level

    (* Public keys are hashed with Blake2b *)
    let hash_key _pk = atomic_step_cost (cost_N_Blake2b public_key_size)

    (* Pushes on the stack an element from the [step_constants] record. *)
    let source = push

    (* Same as cost_source *)
    let sender = source

    (* Same as cost_source *)
    let self = source

    (* Same as cost_source *)
    let self_address = source

    (* Same as cost_source *)
    let amount = source

    (* Same as cost_source *)
    let chain_id = source

    (* TODO benchmark *)
    (* FIXME: imported from 006, needs proper benchmarks *)
    let unpack_failed bytes =
      (* We cannot instrument failed deserialization,
         so we take worst case fees: a set of size 1 bytes values. *)
      let len = Z.of_int (Bytes.length bytes) in
      (len *@ alloc_mbytes_cost 1)
      +@ len
         *@ ( Z.of_int (Z.numbits len)
            *@ (alloc_cost (Z.of_int 3) +@ step_cost Z.one) )

    let ticket = atomic_step_cost (Z.of_int 80)

    let read_ticket = atomic_step_cost (Z.of_int 80)

    let split_ticket ticket_amount amount_a amount_b =
      ticket
      +@ add_bigint amount_a amount_b
      +@ compare_nat ticket_amount ticket_amount

    let join_tickets :
        'a Script_typed_ir.comparable_ty ->
        'a Script_typed_ir.ticket ->
        'a Script_typed_ir.ticket ->
        Gas.cost =
     fun ty ticket_a ticket_b ->
      ticket +@ compare_address
      +@ add_bigint ticket_a.amount ticket_b.amount
      +@ compare ty ticket_a.contents ticket_b.contents
  end

  module Typechecking = struct
    open Generated_costs_007

    let public_key_optimized =
      atomic_step_cost
      @@ Compare.Z.(
           max
             cost_DECODING_PUBLIC_KEY_ed25519
             (max
                cost_DECODING_PUBLIC_KEY_secp256k1
                cost_DECODING_PUBLIC_KEY_p256))

    let public_key_readable =
      atomic_step_cost
      @@ Compare.Z.(
           max
             cost_B58CHECK_DECODING_PUBLIC_KEY_ed25519
             (max
                cost_B58CHECK_DECODING_PUBLIC_KEY_secp256k1
                cost_B58CHECK_DECODING_PUBLIC_KEY_p256))

    let key_hash_optimized =
      atomic_step_cost
      @@ Compare.Z.(
           max
             cost_DECODING_PUBLIC_KEY_HASH_ed25519
             (max
                cost_DECODING_PUBLIC_KEY_HASH_secp256k1
                cost_DECODING_PUBLIC_KEY_HASH_p256))

    let key_hash_readable =
      atomic_step_cost
      @@ Compare.Z.(
           max
             cost_B58CHECK_DECODING_PUBLIC_KEY_HASH_ed25519
             (max
                cost_B58CHECK_DECODING_PUBLIC_KEY_HASH_secp256k1
                cost_B58CHECK_DECODING_PUBLIC_KEY_HASH_p256))

    let signature_optimized =
      atomic_step_cost
      @@ Compare.Z.(
           max
             cost_DECODING_SIGNATURE_ed25519
             (max
                cost_DECODING_SIGNATURE_secp256k1
                cost_DECODING_SIGNATURE_p256))

    let signature_readable =
      atomic_step_cost
      @@ Compare.Z.(
           max
             cost_B58CHECK_DECODING_SIGNATURE_ed25519
             (max
                cost_B58CHECK_DECODING_SIGNATURE_secp256k1
                cost_B58CHECK_DECODING_SIGNATURE_p256))

    let chain_id_optimized = atomic_step_cost cost_DECODING_CHAIN_ID

    let chain_id_readable = atomic_step_cost cost_B58CHECK_DECODING_CHAIN_ID

    (* Reasonable approximation *)
    let address_optimized = key_hash_optimized

    (* Reasonable approximation *)
    let contract_optimized = key_hash_optimized

    (* Reasonable approximation *)
    let contract_readable = key_hash_readable

    let bls12_381_g1 = atomic_step_cost cost_DECODING_BLS_G1

    let bls12_381_g2 = atomic_step_cost cost_DECODING_BLS_G2

    let bls12_381_fr = atomic_step_cost cost_DECODING_BLS_FR

    let check_printable s =
      atomic_step_cost (cost_CHECK_PRINTABLE (String.length s))

    let merge_cycle = atomic_step_cost cost_MERGE_TYPES

    let parse_type_cycle = atomic_step_cost cost_PARSE_TYPE

    let parse_instr_cycle = atomic_step_cost cost_TYPECHECKING_CODE

    let parse_data_cycle = atomic_step_cost cost_TYPECHECKING_DATA

    let comparable_ty_of_ty_cycle = atomic_step_cost cost_COMPARABLE_TY_OF_TY

    (* Cost of a cycle of checking that a type is dupable *)
    (* TODO: bench *)
    let check_dupable_cycle = atomic_step_cost cost_TYPECHECKING_DATA

    let bool = free

    let unit = free

    let timestamp_readable = atomic_step_cost cost_TIMESTAMP_READABLE_DECODING

    (* Reasonable estimate. *)
    let contract = Gas.(Z.of_int 2 *@ public_key_readable)

    (* Assuming unflattened storage: /contracts/hash1/.../hash6/key/balance,
       balance stored on 64 bits *)
    let contract_exists =
      Gas.cost_of_repr
      @@ Storage_costs.read_access ~path_length:9 ~read_bytes:8

    (* Constructing proof arguments consists in a decreasing loop in the result
       monad, allocating at each step. We charge a reasonable overapproximation. *)
    let proof_argument n = atomic_step_cost (Z.mul (Z.of_int n) (Z.of_int 50))
  end

  module Unparsing = struct
    open Generated_costs_007

    let public_key_optimized =
      atomic_step_cost
      @@ Compare.Z.(
           max
             cost_ENCODING_PUBLIC_KEY_ed25519
             (max
                cost_ENCODING_PUBLIC_KEY_secp256k1
                cost_ENCODING_PUBLIC_KEY_p256))

    let public_key_readable =
      atomic_step_cost
      @@ Compare.Z.(
           max
             cost_B58CHECK_ENCODING_PUBLIC_KEY_ed25519
             (max
                cost_B58CHECK_ENCODING_PUBLIC_KEY_secp256k1
                cost_B58CHECK_ENCODING_PUBLIC_KEY_p256))

    let key_hash_optimized =
      atomic_step_cost
      @@ Compare.Z.(
           max
             cost_ENCODING_PUBLIC_KEY_HASH_ed25519
             (max
                cost_ENCODING_PUBLIC_KEY_HASH_secp256k1
                cost_ENCODING_PUBLIC_KEY_HASH_p256))

    let key_hash_readable =
      atomic_step_cost
      @@ Compare.Z.(
           max
             cost_B58CHECK_ENCODING_PUBLIC_KEY_HASH_ed25519
             (max
                cost_B58CHECK_ENCODING_PUBLIC_KEY_HASH_secp256k1
                cost_B58CHECK_ENCODING_PUBLIC_KEY_HASH_p256))

    let signature_optimized =
      atomic_step_cost
      @@ Compare.Z.(
           max
             cost_ENCODING_SIGNATURE_ed25519
             (max
                cost_ENCODING_SIGNATURE_secp256k1
                cost_ENCODING_SIGNATURE_p256))

    let signature_readable =
      atomic_step_cost
      @@ Compare.Z.(
           max
             cost_B58CHECK_ENCODING_SIGNATURE_ed25519
             (max
                cost_B58CHECK_ENCODING_SIGNATURE_secp256k1
                cost_B58CHECK_ENCODING_SIGNATURE_p256))

    let chain_id_optimized = atomic_step_cost cost_ENCODING_CHAIN_ID

    let chain_id_readable = atomic_step_cost cost_B58CHECK_ENCODING_CHAIN_ID

    let timestamp_readable = atomic_step_cost cost_TIMESTAMP_READABLE_ENCODING

    (* Reasonable approximation *)
    let address_optimized = key_hash_optimized

    (* Reasonable approximation *)
    let contract_optimized = key_hash_optimized

    (* Reasonable approximation *)
    let contract_readable = key_hash_readable

    let bls12_381_g1 = atomic_step_cost cost_ENCODING_BLS_G1

    let bls12_381_g2 = atomic_step_cost cost_ENCODING_BLS_G2

    let bls12_381_fr = atomic_step_cost cost_ENCODING_BLS_FR

    let unparse_type_cycle = atomic_step_cost cost_UNPARSE_TYPE

    let unparse_instr_cycle = atomic_step_cost cost_UNPARSING_CODE

    let unparse_data_cycle = atomic_step_cost cost_UNPARSING_DATA

    let unit = Gas.free

    (* Reasonable estimate. *)
    let contract = Gas.(Z.of_int 2 *@ public_key_readable)

    (* Reuse 006 costs. *)
    let operation bytes = Script.bytes_node_cost bytes

    let sapling_transaction _t =
      (* TODO should it be scaled? *)
      (*       let size = Data_encoding.Binary.length Sapling.transaction_encoding t in *)
      (*       string_cost size *)
      Gas.free

    let sapling_diff _d =
      (* TODO should it be scaled? *)
      (*       let size = Data_encoding.Binary.length Sapling.diff_encoding d in *)
      (*       string_cost size *)
      Gas.free
  end
end
OCaml

Innovation. Community. Security.