-
Notifications
You must be signed in to change notification settings - Fork 115
/
test_op_arith.nim
467 lines (409 loc) · 12.7 KB
/
test_op_arith.nim
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
# Nimbus
# Copyright (c) 2019-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# https://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
# https://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
import macro_assembler, unittest2
proc opArithMain*() =
suite "Arithmetic Opcodes":
assembler:
title: "Add_1"
code:
Push1 "0x02"
Push1 "0x02"
Add
stack: "0x04"
assembler:
title: "Add_2"
code:
Push2 "0x1002"
Push1 "0x02"
Add
stack: "0x1004"
assembler:
title: "Add_3"
code:
Push2 "0x1002"
Push6 "0x123456789009"
Add
stack: "0x12345678A00B"
assembler:
title: "Add_4"
code:
Push2 "0x1234"
Add
success: false
stack: "0x1234"
assembler:
title: "Addmod_1"
code:
Push1 "0x02"
Push1 "0x02"
Push1 "0x03"
Addmod
stack: "0x01"
assembler:
title: "Addmod_2"
code:
Push2 "0x1000"
Push1 "0x02"
Push2 "0x1002"
Addmod
Push1 "0x00"
stack:
"0x04"
"0x00"
assembler:
title: "Addmod_3"
code:
Push2 "0x1002"
Push6 "0x123456789009"
Push1 "0x02"
Addmod
stack: "0x093B"
assembler:
title: "Addmod_4"
code:
Push2 "0x1234"
Addmod
stack: "0x1234"
success: false
assembler:
title: "MUL_1"
code:
Push1 "0x03"
Push1 "0x02"
Mul
stack: "0x06"
assembler:
title: "MUL_2"
code:
Push3 "0x222222"
Push1 "0x03"
Mul
stack: "0x666666"
assembler:
title: "MUL_3"
code:
Push3 "0x222222"
Push3 "0x333333"
Mul
stack: "0x6D3A05F92C6"
assembler:
title: "MUL_4"
code:
Push1 "0x01"
Mul
stack: "0x01"
success: false
assembler: # Mulmod OP
title: "MULMOD_2"
code:
Push3 "0x222222"
Push1 "0x03"
Push1 "0x04"
Mulmod
stack: "0x000000000000000000000000000000000000000000000000000000000000000C"
assembler: # Mulmod OP
title: "MULMOD_3"
code:
Push3 "0x222222"
Push3 "0x333333"
Push3 "0x444444"
Mulmod
stack: "0x0000000000000000000000000000000000000000000000000000000000000000"
assembler: # Mulmod OP mal
title: "MULMOD_4"
code:
Push1 "0x01"
Mulmod
success: false
stack: "0x01"
assembler: # Div OP
title: "DIV_1"
code:
Push1 "0x02"
Push1 "0x04"
Div
stack: "0x0000000000000000000000000000000000000000000000000000000000000002"
assembler: # Div OP
title: "DIV_2"
code:
Push1 "0x33"
Push1 "0x99"
Div
stack: "0x0000000000000000000000000000000000000000000000000000000000000003"
assembler: # Div OP
title: "DIV_3"
code:
Push1 "0x22"
Push1 "0x99"
Div
stack: "0x0000000000000000000000000000000000000000000000000000000000000004"
assembler: # Div OP
title: "DIV_4"
code:
Push1 "0x15"
Push1 "0x99"
Div
stack: "0x0000000000000000000000000000000000000000000000000000000000000007"
assembler: # Div OP
title: "DIV_5"
code:
Push1 "0x04"
Push1 "0x07"
Div
stack: "0x0000000000000000000000000000000000000000000000000000000000000001"
assembler: # Div OP
title: "DIV_6"
code:
Push1 "0x07"
Div
success: false
stack: "0x07"
assembler: # Sdiv OP
title: "SDIV_1"
code:
Push2 "0x03E8"
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC18"
Sdiv
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
assembler: # Sdiv OP
title: "SDIV_2"
code:
Push1 "0xFF"
Push1 "0xFF"
Sdiv
stack: "0x0000000000000000000000000000000000000000000000000000000000000001"
assembler: # Sdiv OP, Sdiv by zero should be zero
title: "SDIV_3"
code:
Push1 "0x00"
Push1 "0xFF"
Sdiv
stack: "0x0000000000000000000000000000000000000000000000000000000000000000"
assembler: # Sdiv OP mal
title: "SDIV_4"
code:
Push1 "0xFF"
Sdiv
success: false
stack: "0xFF"
assembler: # -2^255 Sdiv -1 = -2^255 (special case in yellow paper)
title: "SDIV_5"
code:
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" # -1
Push32 "0x8000000000000000000000000000000000000000000000000000000000000000" # -2^255 == low(int256)
Sdiv
stack: "0x8000000000000000000000000000000000000000000000000000000000000000" # -2^255 == low(int256)
assembler: # -2^255 Sdiv 1 = -2^255
title: "SDIV_6"
code:
Push32 "0x0000000000000000000000000000000000000000000000000000000000000001" # 1
Push32 "0x8000000000000000000000000000000000000000000000000000000000000000" # -2^255 == low(int256)
Sdiv
stack: "0x8000000000000000000000000000000000000000000000000000000000000000" # -2^255 == low(int256)
assembler: # -2 Sdiv 2 = -1
title: "SDIV_7"
code:
Push32 "0x0000000000000000000000000000000000000000000000000000000000000002" # 2
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" # -2
Sdiv
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" # -1
assembler: # 4 Sdiv -2 = -2
title: "SDIV_8"
code:
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" # -2
Push32 "0x0000000000000000000000000000000000000000000000000000000000000004" # 4
Sdiv
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" # -2
assembler: # -4 Sdiv 2 = -2
title: "SDIV_9"
code:
Push32 "0x0000000000000000000000000000000000000000000000000000000000000002" # -4
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC" # 2
Sdiv
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" # -2
assembler: # -1 Sdiv 2 = 0
title: "SDIV_10"
code:
Push32 "0x0000000000000000000000000000000000000000000000000000000000000002" # 2
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" # -1
Sdiv
stack: "0x00" # 0
assembler: # low(int256) Sdiv low(int256) = 1
title: "SDIV_11"
code:
Push32 "0x8000000000000000000000000000000000000000000000000000000000000000" # low(int256)
Push32 "0x8000000000000000000000000000000000000000000000000000000000000000" # low(int256)
Sdiv
stack: "0x01" # 1
assembler: # low(int256) Sdiv 2
title: "SDIV_12"
code:
Push32 "0x0000000000000000000000000000000000000000000000000000000000000002" # 2
Push32 "0x8000000000000000000000000000000000000000000000000000000000000000" # low(int256)
Sdiv
stack: "0xC000000000000000000000000000000000000000000000000000000000000000" # negative half low(int256)
assembler: # low(int256) Sdiv -2
title: "SDIV_13"
code:
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" # -2
Push32 "0x8000000000000000000000000000000000000000000000000000000000000000" # low(int256)
Sdiv
stack: "0x4000000000000000000000000000000000000000000000000000000000000000" # positive version of SDIV_12
assembler:
title: "SDIV_14"
code:
Push1 "0x01" # 1
Push1 "0x7F" # 127
Shl # 1 shl 127 (move the bit to the center or 128th position)
Push1 "0x01" # 1
Push1 "0xFF" # 255
Shl # 1 shl 255 (create low(int256))
Sdiv
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000" # half of the hi bits are set
fork: Constantinople
assembler: # Sub OP
title: "SUB_1"
code:
Push1 "0x04"
Push1 "0x06"
Sub
stack: "0x0000000000000000000000000000000000000000000000000000000000000002"
assembler: # Sub OP
title: "SUB_2"
code:
Push2 "0x4444"
Push2 "0x6666"
Sub
stack: "0x0000000000000000000000000000000000000000000000000000000000002222"
assembler: # Sub OP
title: "SUB_3"
code:
Push2 "0x4444"
Push4 "0x99996666"
Sub
stack: "0x0000000000000000000000000000000000000000000000000000000099992222"
assembler: # Sub OP mal
title: "SUB_4"
code:
Push4 "0x99996666"
Sub
success: false
stack: "0x99996666"
assembler: # Exp OP
title: "EXP_1"
code:
Push1 "0x03"
Push1 "0x02"
Exp
stack: "0x0000000000000000000000000000000000000000000000000000000000000008"
#assertEquals(4, gas);
assembler: # Exp OP
title: "EXP_2"
code:
Push1 "0x00"
Push3 "0x123456"
Exp
stack: "0x0000000000000000000000000000000000000000000000000000000000000001"
#assertEquals(3, gas);
assembler: # Exp OP
title: "EXP_3"
code:
Push2 "0x1122"
Push1 "0x01"
Exp
stack: "0x0000000000000000000000000000000000000000000000000000000000000001"
#assertEquals(5, gas);
assembler: # Exp OP mal
title: "EXP_4"
code:
Push3 "0x123456"
Exp
success: false
stack: "0x123456"
assembler: # Mod OP
title: "MOD_1"
code:
Push1 "0x03"
Push1 "0x04"
Mod
stack: "0x0000000000000000000000000000000000000000000000000000000000000001"
assembler: # Mod OP
title: "MOD_2"
code:
Push2 "0x012C"
Push2 "0x01F4"
Mod
stack: "0x00000000000000000000000000000000000000000000000000000000000000C8"
assembler: # Mod OP
title: "MOD_3"
code:
Push1 "0x04"
Push1 "0x02"
Mod
stack: "0x0000000000000000000000000000000000000000000000000000000000000002"
assembler: # Mod OP mal
title: "MOD_4"
code:
Push1 "0x04"
Mod
success: false
stack: "0x04"
assembler: # Smod OP
title: "SMOD_1"
code:
Push1 "0x03"
Push1 "0x04"
Smod
stack: "0x0000000000000000000000000000000000000000000000000000000000000001"
assembler: # Smod OP
title: "SMOD_2"
code:
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE2" # -30
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF56" # -170
Smod
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEC"
assembler: # Smod OP
title: "SMOD_3"
code:
Push32 "0x000000000000000000000000000000000000000000000000000000000000001E" # 30
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF56" # -170
Smod
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEC"
assembler: # Smod OP mal
title: "SMOD_4"
code:
Push32 "0x000000000000000000000000000000000000000000000000000000000000001E" # 30
Smod
success: false
stack: "0x000000000000000000000000000000000000000000000000000000000000001E"
# real case, EVM bug, integer over flow
assembler: # SignExtend OP
title: "SIGNEXTEND_1"
code:
Push32 "0x000000000000000000000000000000003f9b347132d29b62d161117bca8c7307"
Push1 "0x0F"
SignExtend
stack: "0x000000000000000000000000000000003f9b347132d29b62d161117bca8c7307"
assembler:
title: "Byte with overflow pos 1"
code:
Push32 "0x77676767676760000000000000001002e000000000000040000000e000000000"
Push32 "0x0000000000000000000000000000000000000000000000010000000000000000"
Byte
stack: "0x0000000000000000000000000000000000000000000000000000000000000000"
assembler:
title: "Byte with overflow pos 2"
code:
Push32 "0x001f000000000000000000000000000000200000000100000000000000000000"
Push32 "0x0000000000000000000000000000000080000000000000000000000000000001"
Byte
stack: "0x0000000000000000000000000000000000000000000000000000000000000000"
when isMainModule:
opArithMain()