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
open Alpha_context
open Gas
module Cost_of = struct
let cycle = step_cost 1
let nop = free
let stack_op = step_cost 1
let bool_binop _ _ = step_cost 1
let bool_unop _ = step_cost 1
let pair = alloc_cost 2
let pair_access = step_cost 1
let cons = alloc_cost 2
let variant_no_data = alloc_cost 1
let branch = step_cost 2
let string length =
alloc_bytes_cost length
let bytes length =
alloc_mbytes_cost length
let zint z =
alloc_bits_cost (Z.numbits z)
let concat cost length ss =
let rec cum acc = function
| [] -> acc
| s :: ss -> cum (cost (length s) +@ acc) ss in
cum free ss
let concat_string ss = concat string String.length ss
let concat_bytes ss = concat bytes MBytes.length ss
let slice_string length = string length
let slice_bytes = alloc_cost 0
let loop_cycle = step_cost 2
let list_size = step_cost 1
let log2 =
let rec help acc = function
| 0 -> acc
| n -> help (acc + 1) (n / 2)
in help 1
let module_cost = alloc_cost 10
let map_access : type key value. (key, value) Script_typed_ir.map -> int
= fun (module Box) ->
log2 (snd Box.boxed)
let map_to_list : type key value. (key, value) Script_typed_ir.map -> cost
= fun (module Box) ->
let size = snd Box.boxed in
3 *@ alloc_cost size
let map_mem _key map = step_cost (map_access map)
let map_get = map_mem
let map_update _ _ map =
map_access map *@ alloc_cost 3
let map_size = step_cost 2
let big_map_mem _key _map = step_cost 50
let big_map_get _key _map = step_cost 50
let big_map_update _key _value _map = step_cost 10
let set_access : type elt. elt -> elt Script_typed_ir.set -> int
= fun _key (module Box) ->
log2 @@ Box.size
let set_mem key set = step_cost (set_access key set)
let set_update key _presence set =
set_access key set *@ alloc_cost 3
let wrap = alloc_cost 1
let mul n1 n2 =
let steps =
(Z.numbits (Script_int.to_zint n1))
* (Z.numbits (Script_int.to_zint n2)) in
let bits =
(Z.numbits (Script_int.to_zint n1))
+ (Z.numbits (Script_int.to_zint n2)) in
step_cost steps +@ alloc_bits_cost bits
let div n1 n2 =
mul n1 n2 +@ alloc_cost 2
let add_sub_z n1 n2 =
let bits =
Compare.Int.max (Z.numbits n1) (Z.numbits n2) in
step_cost bits +@ alloc_cost bits
let add n1 n2 =
add_sub_z (Script_int.to_zint n1) (Script_int.to_zint n2)
let sub = add
let abs n =
alloc_bits_cost (Z.numbits @@ Script_int.to_zint n)
let neg = abs
let int _ = step_cost 1
let add_timestamp t n =
add_sub_z (Script_timestamp.to_zint t) (Script_int.to_zint n)
let sub_timestamp t n =
add_sub_z (Script_timestamp.to_zint t) (Script_int.to_zint n)
let diff_timestamps t1 t2 =
add_sub_z (Script_timestamp.to_zint t1) (Script_timestamp.to_zint t2)
let empty_set = module_cost
let set_size = step_cost 2
let set_to_list : type item. item Script_typed_ir.set -> cost
= fun (module Box) ->
alloc_cost @@ Pervasives.(Box.size * 2)
let empty_map = module_cost
let int64_op = step_cost 1 +@ alloc_cost 1
let z_to_int64 = step_cost 2 +@ alloc_cost 1
let int64_to_z = step_cost 2 +@ alloc_cost 1
let bitwise_binop n1 n2 =
let bits = Compare.Int.max (Z.numbits (Script_int.to_zint n1)) (Z.numbits (Script_int.to_zint n2)) in
step_cost bits +@ alloc_bits_cost bits
let logor = bitwise_binop
let logand = bitwise_binop
let logxor = bitwise_binop
let lognot n =
let bits = Z.numbits @@ Script_int.to_zint n in
step_cost bits +@ alloc_cost bits
let unopt ~default = function
| None -> default
| Some x -> x
let max_int = 1073741823
let shift_left x y =
alloc_bits_cost
(Z.numbits (Script_int.to_zint x) +
(unopt (Script_int.to_int y) ~default:max_int))
let shift_right x y =
alloc_bits_cost
(Compare.Int.max 1
(Z.numbits (Script_int.to_zint x) -
unopt (Script_int.to_int y) ~default:max_int))
let exec = step_cost 1
let push = step_cost 1
let compare_res = step_cost 1
let unpack_failed bytes =
let len = MBytes.length bytes in
(len *@ alloc_mbytes_cost 1) +@
(len *@ (log2 len *@ (alloc_cost 3 +@ step_cost 1)))
let address = step_cost 1
let contract = Gas.read_bytes_cost Z.zero +@ step_cost 100
let transfer = step_cost 10
let create_account = step_cost 10
let create_contract = step_cost 10
let implicit_account = step_cost 10
let set_delegate = step_cost 10 +@ write_bytes_cost (Z.of_int 32)
let balance = step_cost 1 +@ read_bytes_cost (Z.of_int 8)
let now = step_cost 5
let check_signature = step_cost 1000
let hash_key = step_cost 3 +@ bytes 20
let hash data len = 10 *@ step_cost (MBytes.length data) +@ bytes len
let steps_to_quota = step_cost 1
let source = step_cost 1
let self = step_cost 1
let amount = step_cost 1
let compare_bool _ _ = step_cost 1
let compare_string s1 s2 =
step_cost ((7 + Compare.Int.max (String.length s1) (String.length s2)) / 8) +@ step_cost 1
let compare_bytes s1 s2 =
step_cost ((7 + Compare.Int.max (MBytes.length s1) (MBytes.length s2)) / 8) +@ step_cost 1
let compare_tez _ _ = step_cost 1
let compare_zint n1 n2 = step_cost ((7 + Compare.Int.max (Z.numbits n1) (Z.numbits n2)) / 8) +@ step_cost 1
let compare_int n1 n2 = compare_zint (Script_int.to_zint n1) (Script_int.to_zint n2)
let compare_nat = compare_int
let compare_key_hash _ _ = alloc_bytes_cost 36
let compare_timestamp t1 t2 = compare_zint (Script_timestamp.to_zint t1) (Script_timestamp.to_zint t2)
let compare_address _ _ = step_cost 20
module Typechecking = struct
let cycle = step_cost 1
let bool = free
let unit = free
let string = string
let bytes = bytes
let z = zint
let int_of_string str =
alloc_cost @@ (Pervasives.(/) (String.length str) 5)
let tez = step_cost 1 +@ alloc_cost 1
let string_timestamp = step_cost 3 +@ alloc_cost 3
let key = step_cost 3 +@ alloc_cost 3
let key_hash = step_cost 1 +@ alloc_cost 1
let signature = step_cost 1 +@ alloc_cost 1
let contract = step_cost 5
let get_script = step_cost 20 +@ alloc_cost 5
let contract_exists = step_cost 15 +@ alloc_cost 5
let pair = alloc_cost 2
let union = alloc_cost 1
let lambda = alloc_cost 5 +@ step_cost 3
let some = alloc_cost 1
let none = alloc_cost 0
let list_element = alloc_cost 2 +@ step_cost 1
let set_element size = log2 size *@ (alloc_cost 3 +@ step_cost 2)
let map_element size = log2 size *@ (alloc_cost 4 +@ step_cost 2)
let primitive_type = alloc_cost 1
let one_arg_type = alloc_cost 2
let two_arg_type = alloc_cost 3
let operation b = bytes b
let type_ nb_args = alloc_cost (nb_args + 1)
let instr
: type b a. (b, a) Script_typed_ir.instr -> cost
= fun i ->
let open Script_typed_ir in
alloc_cost 1 +@
match i with
| Drop -> alloc_cost 0
| Dup -> alloc_cost 1
| Swap -> alloc_cost 0
| Const _ -> alloc_cost 1
| Cons_pair -> alloc_cost 2
| Car -> alloc_cost 1
| Cdr -> alloc_cost 1
| Cons_some -> alloc_cost 2
| Cons_none _ -> alloc_cost 3
| If_none _ -> alloc_cost 2
| Left -> alloc_cost 3
| Right -> alloc_cost 3
| If_left _ -> alloc_cost 2
| Cons_list -> alloc_cost 1
| Nil -> alloc_cost 1
| If_cons _ -> alloc_cost 2
| List_map _ -> alloc_cost 5
| List_iter _ -> alloc_cost 4
| List_size -> alloc_cost 1
| Empty_set _ -> alloc_cost 1
| Set_iter _ -> alloc_cost 4
| Set_mem -> alloc_cost 1
| Set_update -> alloc_cost 1
| Set_size -> alloc_cost 1
| Empty_map _ -> alloc_cost 2
| Map_map _ -> alloc_cost 5
| Map_iter _ -> alloc_cost 4
| Map_mem -> alloc_cost 1
| Map_get -> alloc_cost 1
| Map_update -> alloc_cost 1
| Map_size -> alloc_cost 1
| Big_map_mem -> alloc_cost 1
| Big_map_get -> alloc_cost 1
| Big_map_update -> alloc_cost 1
| Concat_string -> alloc_cost 1
| Concat_string_pair -> alloc_cost 1
| Concat_bytes -> alloc_cost 1
| Concat_bytes_pair -> alloc_cost 1
| Slice_string -> alloc_cost 1
| Slice_bytes -> alloc_cost 1
| String_size -> alloc_cost 1
| Bytes_size -> alloc_cost 1
| Add_seconds_to_timestamp -> alloc_cost 1
| Add_timestamp_to_seconds -> alloc_cost 1
| Sub_timestamp_seconds -> alloc_cost 1
| Diff_timestamps -> alloc_cost 1
| Add_tez -> alloc_cost 1
| Sub_tez -> alloc_cost 1
| Mul_teznat -> alloc_cost 1
| Mul_nattez -> alloc_cost 1
| Ediv_teznat -> alloc_cost 1
| Ediv_tez -> alloc_cost 1
| Or -> alloc_cost 1
| And -> alloc_cost 1
| Xor -> alloc_cost 1
| Not -> alloc_cost 1
| Is_nat -> alloc_cost 1
| Neg_nat -> alloc_cost 1
| Neg_int -> alloc_cost 1
| Abs_int -> alloc_cost 1
| Int_nat -> alloc_cost 1
| Add_intint -> alloc_cost 1
| Add_intnat -> alloc_cost 1
| Add_natint -> alloc_cost 1
| Add_natnat -> alloc_cost 1
| Sub_int -> alloc_cost 1
| Mul_intint -> alloc_cost 1
| Mul_intnat -> alloc_cost 1
| Mul_natint -> alloc_cost 1
| Mul_natnat -> alloc_cost 1
| Ediv_intint -> alloc_cost 1
| Ediv_intnat -> alloc_cost 1
| Ediv_natint -> alloc_cost 1
| Ediv_natnat -> alloc_cost 1
| Lsl_nat -> alloc_cost 1
| Lsr_nat -> alloc_cost 1
| Or_nat -> alloc_cost 1
| And_nat -> alloc_cost 1
| And_int_nat -> alloc_cost 1
| Xor_nat -> alloc_cost 1
| Not_nat -> alloc_cost 1
| Not_int -> alloc_cost 1
| Seq _ -> alloc_cost 8
| If _ -> alloc_cost 8
| Loop _ -> alloc_cost 4
| Loop_left _ -> alloc_cost 5
| Dip _ -> alloc_cost 4
| Exec -> alloc_cost 1
| Lambda _ -> alloc_cost 2
| Failwith _ -> alloc_cost 1
| Nop -> alloc_cost 0
| Compare _ -> alloc_cost 1
| Eq -> alloc_cost 1
| Neq -> alloc_cost 1
| Lt -> alloc_cost 1
| Gt -> alloc_cost 1
| Le -> alloc_cost 1
| Ge -> alloc_cost 1
| Address -> alloc_cost 1
| Contract _ -> alloc_cost 2
| Transfer_tokens -> alloc_cost 1
| Create_account -> alloc_cost 2
| Implicit_account -> alloc_cost 1
| Create_contract _ -> alloc_cost 8
| Set_delegate -> alloc_cost 1
| Now -> alloc_cost 1
| Balance -> alloc_cost 1
| Check_signature -> alloc_cost 1
| Hash_key -> alloc_cost 1
| Pack _ -> alloc_cost 2
| Unpack _ -> alloc_cost 2
| Blake2b -> alloc_cost 1
| Sha256 -> alloc_cost 1
| Sha512 -> alloc_cost 1
| Steps_to_quota -> alloc_cost 1
| Source -> alloc_cost 1
| Sender -> alloc_cost 1
| Self _ -> alloc_cost 2
| Amount -> alloc_cost 1
end
module Unparse = struct
let prim_cost l annot = Script.prim_node_cost_nonrec_of_length l annot
let seq_cost = Script.seq_node_cost_nonrec_of_length
let string_cost length = Script.string_node_cost_of_length length
let cycle = step_cost 1
let bool = prim_cost 0 []
let unit = prim_cost 0 []
let string s = Script.string_node_cost s
let bytes s = Script.bytes_node_cost s
let z i = Script.int_node_cost i
let int i = Script.int_node_cost (Script_int.to_zint i)
let tez = Script.int_node_cost_of_numbits 60
let timestamp x = Script_timestamp.to_zint x |> Script_int.of_zint |> int
let operation bytes = Script.bytes_node_cost bytes
let key = string_cost 54
let key_hash = string_cost 36
let signature = string_cost 128
let contract = string_cost 36
let pair = prim_cost 2 []
let union = prim_cost 1 []
let some = prim_cost 1 []
let none = prim_cost 0 []
let list_element = alloc_cost 2
let set_element = alloc_cost 2
let map_element = alloc_cost 2
let one_arg_type = prim_cost 1
let two_arg_type = prim_cost 2
let set_to_list = set_to_list
let map_to_list = map_to_list
end
end