-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevm.lua
More file actions
2022 lines (1763 loc) · 66.5 KB
/
Copy pathevm.lua
File metadata and controls
2022 lines (1763 loc) · 66.5 KB
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
#!lua name=EVM
local function print(v)
-- NOOP, as redis can't print
end
-- Define a local environment for the EVM implementation
local EVM = {}
-- Initialize some necessary components for the EVM
function EVM.init(addr)
-- Initialize state attributes
local evmState = {
address = addr,
stack = {},
memory = {},
storage = {},
transient_storage = {}, -- Transient storage for TLOAD/TSTORE
pc = 1, -- Program Counter
logs = {},
return_data = {},
}
return evmState
end
-- Define a simple pairs function
local function tpairs(t)
local i = 0
local n = #t
return function()
i = i + 1
if i <= n then return i, t[i] end
end
end
local function hexStringToTable(hexString)
local tbl = {}
if hexString then
hexString = hexString:gsub("^0x", "")
hexString = hexString:gsub(" ", "")
else
return ""
end
-- Iterate over the string in steps of 2 characters
for i = 1, #hexString, 2 do
local byteString = hexString:sub(i, i+1)
local byte = tonumber(byteString, 16)
if byte then
table.insert(tbl, byte)
else
error("Invalid hex string")
end
end
return tbl
end
-- Normalize an address (number or "0x..."/bare hex string) to lowercase 40-hex, no prefix.
local function normalize_address(address)
local addr_str = type(address) == "number" and string.format("%040X", address) or tostring(address)
addr_str = addr_str:gsub("^0x", ""):lower()
return addr_str
end
-- Look up contract code for an address, tolerating the several key conventions
-- used across the test suite: raw "0x..", raw bare hex, "CODE:0x..", "CODE:..".
local function lookup_code(address)
local bare = normalize_address(address)
local candidates = {
"0x" .. bare,
bare,
"CODE:0x" .. bare,
"CODE:" .. bare,
}
for _, key in ipairs(candidates) do
local code = redis.call("GET", key)
if code and code ~= "" then
return code
end
end
return nil
end
-- Resolve the active calldata for a state as a byte table.
-- Nested calls set state.calldata directly (passed via memory args);
-- the top-level eth_call falls back to the Redis "CALLDATA" key.
local function get_calldata_bytes(state)
if state and state.calldata then
return state.calldata
end
return hexStringToTable(redis.call("GET", "CALLDATA") or "")
end
-- Helper function to convert stack values to numbers
local function toNumber(val)
if type(val) == "number" then
return val
elseif type(val) == "string" and val:match("^0x[0-9A-Fa-f]+$") then
local hex_part = val:sub(3)
if #hex_part <= 14 then
return tonumber(val, 16) or 0
else
return tonumber("0x" .. hex_part:sub(-14), 16) or 0
end
else
return tonumber(val) or 0
end
end
-- Redis key generation and validation helpers for environmental context
-- Generate Redis key for account balance
local function balance_key(address)
-- Ensure address is properly formatted
local addr_str = type(address) == "number" and string.format("0x%040X", address) or tostring(address)
-- Remove 0x prefix if present and ensure lowercase
addr_str = addr_str:gsub("^0x", ""):lower()
return "BALANCE:" .. addr_str
end
-- Generate Redis key for contract code storage
local function code_key(address)
-- Ensure address is properly formatted
local addr_str = type(address) == "number" and string.format("0x%040X", address) or tostring(address)
-- Remove 0x prefix if present and ensure lowercase
addr_str = addr_str:gsub("^0x", ""):lower()
return "CODE:" .. addr_str
end
-- Generate Redis key for contract code hash storage
local function codehash_key(address)
-- Ensure address is properly formatted
local addr_str = type(address) == "number" and string.format("0x%040X", address) or tostring(address)
-- Remove 0x prefix if present and ensure lowercase
addr_str = addr_str:gsub("^0x", ""):lower()
return "CODEHASH:" .. addr_str
end
-- Validate Redis key format for environmental context
local function validate_env_key(key, expected_prefix)
if not key or type(key) ~= "string" then
return false
end
if not key:match("^" .. expected_prefix .. ":") then
return false
end
-- Extract address part and validate hex format
local address_part = key:sub(#expected_prefix + 2) -- +2 for ":"
if not address_part:match("^[0-9a-f]+$") then
return false
end
return true
end
-- Helper to safely get environmental data from Redis with defaults
local function get_env_data(key, default_value)
local value = redis.call("GET", key)
if value == nil or value == false then
return default_value
end
return value
end
-- display hex data in string representation
local function h(n)
if n == nil then
return "0x00"
elseif type(n) == "boolean" then
return n and "true" or "false"
elseif type(n) == "string" then
if n:match("^0x[0-9A-Fa-f]+$") then
local hex_part = n:sub(3):upper()
if #hex_part > 2 then
local simplified = hex_part:gsub("^0+", "")
if simplified == "" then
return "0x00"
elseif #simplified == 1 then
return "0x0" .. simplified
else
return "0x" .. simplified
end
else
return "0x" .. hex_part
end
else
return n
end
elseif type(n) == "number" then
if n < 256 then
return string.format("0x%02X", n)
else
return string.format("0x%X", n)
end
else
return tostring(n)
end
end
-- 64-bit arithmetic using 32-bit chunks for better precision
local function split64(n)
local high = math.floor(n / 4294967296) % 4294967296
local low = n % 4294967296
return high, low
end
local function join64(high, low)
return (high * 4294967296 + low) % (2^53)
end
local function band64(a, b)
local ah, al = split64(a)
local bh, bl = split64(b)
local function band32(x, y)
local result = 0
local bit = 1
for i = 1, 32 do
if (math.floor(x / bit) % 2 == 1) and (math.floor(y / bit) % 2 == 1) then
result = result + bit
end
bit = bit * 2
if bit > x and bit > y then break end
end
return result
end
return join64(band32(ah, bh), band32(al, bl))
end
local function bxor64(a, b)
local ah, al = split64(a)
local bh, bl = split64(b)
local function bxor32(x, y)
local result = 0
local bit = 1
for i = 1, 32 do
if (math.floor(x / bit) % 2) ~= (math.floor(y / bit) % 2) then
result = result + bit
end
bit = bit * 2
if bit > x and bit > y then break end
end
return result
end
return join64(bxor32(ah, bh), bxor32(al, bl))
end
local function bnot64(a)
return bxor64(a, 0x1FFFFFFFFFFFFF)
end
local function lrotate64(value, amount)
if amount == 0 then return value end
amount = amount % 64
local high, low = split64(value)
if amount == 32 then
return join64(low, high)
elseif amount < 32 then
local new_high = ((high * (2^amount)) % 4294967296) + math.floor(low / (2^(32-amount)))
local new_low = ((low * (2^amount)) % 4294967296) + math.floor(high / (2^(32-amount)))
return join64(new_high % 4294967296, new_low % 4294967296)
else
amount = amount - 32
local new_high = ((low * (2^amount)) % 4294967296) + math.floor(high / (2^(32-amount)))
local new_low = ((high * (2^amount)) % 4294967296) + math.floor(low / (2^(32-amount)))
return join64(new_high % 4294967296, new_low % 4294967296)
end
end
-- Keccak implamentation ported from: https://github.com/paulmillr/noble-hashes/
-- Keccak256 constants
local SHA3_PI = {2,6,12,18,24,3,9,10,16,22,1,7,13,19,20,4,5,11,17,23,8,14,15,21,2}
local SHA3_ROTL = {1,3,6,10,15,21,28,36,45,55,2,14,27,41,56,8,25,43,62,18,39,61,20,44}
local SHA3_IOTA_H = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
local SHA3_IOTA_L = {1,130,32898,32906,2147516416,2147483649,32777,138,136,2147516425,2147483658,2147516555,139,32905,32771,32770,128,32778,2147483658,2147516545,32896,2147483649,2147516424,2147516555}
local function rotl32(n, b)
n = n % 0x100000000
local left_shift = (n * (2^b)) % 0x100000000
local right_shift = math.floor(n / (2^(32-b)))
return (left_shift + right_shift) % 0x100000000
end
local function rotlH(h, l, s)
if s > 32 then
return bxor64(rotl32(l, s - 32), rotl32(h, s - 32))
else
return bxor64(rotl32(h, s), math.floor(l / (2^(32-s))))
end
end
local function rotlL(h, l, s)
if s > 32 then
return bxor64(rotl32(h, s - 32), rotl32(l, s - 32))
else
return bxor64(rotl32(l, s), math.floor(h / (2^(32-s))))
end
end
local function keccakP(s)
local B = {}
for i = 1, 10 do B[i] = 0 end
for round = 0, 23 do
-- Theta
for x = 0, 4 do
B[x * 2 + 1] = bxor64(bxor64(bxor64(bxor64(s[x * 2 + 1], s[x * 2 + 11]), s[x * 2 + 21]), s[x * 2 + 31]), s[x * 2 + 41])
B[x * 2 + 2] = bxor64(bxor64(bxor64(bxor64(s[x * 2 + 2], s[x * 2 + 12]), s[x * 2 + 22]), s[x * 2 + 32]), s[x * 2 + 42])
end
for x = 0, 4 do
local idx1 = ((x + 4) % 5) * 2
local idx0 = ((x + 1) % 5) * 2
local B0 = B[idx0 + 1]
local B1 = B[idx0 + 2]
local Th = bxor64(rotlH(B0, B1, 1), B[idx1 + 1])
local Tl = bxor64(rotlL(B0, B1, 1), B[idx1 + 2])
for y = 0, 4 do
s[x * 2 + y * 10 + 1] = bxor64(s[x * 2 + y * 10 + 1], Th)
s[x * 2 + y * 10 + 2] = bxor64(s[x * 2 + y * 10 + 2], Tl)
end
end
-- Rho and Pi
local curH = s[3]
local curL = s[4]
for t = 0, 23 do
local shift = SHA3_ROTL[t + 1]
local Th = rotlH(curH, curL, shift)
local Tl = rotlL(curH, curL, shift)
local PI = SHA3_PI[t + 1]
curH = s[PI + 1]
curL = s[PI + 2]
s[PI + 1] = Th
s[PI + 2] = Tl
end
-- Chi
for y = 0, 4 do
for x = 0, 4 do
B[x * 2 + 1] = s[y * 10 + x * 2 + 1]
B[x * 2 + 2] = s[y * 10 + x * 2 + 2]
end
for x = 0, 4 do
local not_b1 = bnot64(B[((x + 1) % 5) * 2 + 1])
local not_b2 = bnot64(B[((x + 1) % 5) * 2 + 2])
s[y * 10 + x * 2 + 1] = bxor64(s[y * 10 + x * 2 + 1], band64(not_b1, B[((x + 2) % 5) * 2 + 1]))
s[y * 10 + x * 2 + 2] = bxor64(s[y * 10 + x * 2 + 2], band64(not_b2, B[((x + 2) % 5) * 2 + 2]))
end
end
-- Iota
s[1] = bxor64(s[1], SHA3_IOTA_H[round + 1])
s[2] = bxor64(s[2], SHA3_IOTA_L[round + 1])
end
end
local function keccak256(data)
local state32 = {}
for i = 1, 50 do state32[i] = 0 end
local state = {}
for i = 1, 200 do state[i] = 0 end
local blockLen = 136
local pos = 0
-- Absorb
local bytes = type(data) == "string" and {data:byte(1, #data)} or data
local len = #bytes
local dataPos = 0
while dataPos < len do
local take = math.min(blockLen - pos, len - dataPos)
for i = 0, take - 1 do
state[pos + i + 1] = bxor64(state[pos + i + 1], bytes[dataPos + i + 1])
end
pos = pos + take
dataPos = dataPos + take
if pos == blockLen then
-- Convert to 32-bit words
for i = 0, 49 do
local byte_idx = i * 4
state32[i + 1] = state[byte_idx + 1] +
(state[byte_idx + 2] * 256) +
(state[byte_idx + 3] * 65536) +
(state[byte_idx + 4] * 16777216)
end
keccakP(state32)
-- Convert back to bytes
for i = 0, 49 do
local word = state32[i + 1]
local byte_idx = i * 4
state[byte_idx + 1] = word % 256
state[byte_idx + 2] = math.floor(word / 256) % 256
state[byte_idx + 3] = math.floor(word / 65536) % 256
state[byte_idx + 4] = math.floor(word / 16777216) % 256
end
pos = 0
end
end
-- Padding
state[pos + 1] = bxor64(state[pos + 1], 0x01)
state[blockLen] = bxor64(state[blockLen], 0x80)
-- Final permutation
for i = 0, 49 do
local byte_idx = i * 4
state32[i + 1] = state[byte_idx + 1] +
(state[byte_idx + 2] * 256) +
(state[byte_idx + 3] * 65536) +
(state[byte_idx + 4] * 16777216)
end
keccakP(state32)
for i = 0, 49 do
local word = state32[i + 1]
local byte_idx = i * 4
state[byte_idx + 1] = word % 256
state[byte_idx + 2] = math.floor(word / 256) % 256
state[byte_idx + 3] = math.floor(word / 65536) % 256
state[byte_idx + 4] = math.floor(word / 16777216) % 256
end
-- Extract 32 bytes
local result = "0x"
for i = 1, 32 do
result = result .. string.format("%02X", state[i])
end
return result
end
-- Utility functions for enhanced arithmetic operations
-- Safe stack pop function with underflow protection
local function safe_pop(stack, count)
count = count or 1
if #stack < count then
error("Stack underflow: attempted to pop " .. count .. " items from stack of size " .. #stack)
end
local values = {}
for i = 1, count do
table.insert(values, table.remove(stack))
end
if count == 1 then
return values[1]
else
return table.unpack(values)
end
end
-- Helper for signed arithmetic operations
local function signed_arithmetic(value)
-- Convert unsigned 256-bit value to signed
-- If the high bit is set, it's negative in two's complement
if value >= 2^255 then
return value - 2^256
else
return value
end
end
-- Maximum EVM call stack depth (EIP-150).
local MAX_CALL_DEPTH = 1024
-- Upper bound on a single memory-region length (in bytes). Real EVM bounds this
-- via quadratic memory-expansion gas; without gas metering a stack-supplied length
-- can be astronomically large and hang single-threaded Redis. Real fixtures touch
-- at most a few dozen bytes; the giant-length cases are gas tests (expecting
-- out-of-gas, which we cannot replicate without gas metering). 4 KiB keeps the
-- worst-case per-opcode work bounded (pure-Lua keccak is ~0.14ms/byte) while
-- leaving every realistic operation unaffected.
local MAX_MEM_BYTES = 4 * 1024
-- Clamp a stack-derived length to the memory ceiling.
local function clamp_length(length)
if length > MAX_MEM_BYTES then
return MAX_MEM_BYTES
end
return length
end
-- Read `length` bytes of memory starting at `offset` into a fresh byte table.
local function read_memory_bytes(state, offset, length)
local bytes = {}
for i = 0, length - 1 do
bytes[i + 1] = state.memory[offset + i] or 0
end
return bytes
end
-- Write a byte table into memory starting at `dest_offset`, bounded by `length`.
local function write_memory_bytes(state, dest_offset, data, length)
for i = 0, length - 1 do
state.memory[dest_offset + i] = (data and data[i + 1]) or 0
end
end
-- Shared implementation for the CALL family (CALL/CALLCODE/DELEGATECALL/STATICCALL).
-- `kind` selects the storage/value semantics. Returns nothing; pushes success flag.
local function do_call(state, kind)
local gas = toNumber(table.remove(state.stack))
local to_address = table.remove(state.stack)
-- CALL and CALLCODE carry a value argument; DELEGATECALL/STATICCALL do not.
local value = 0
if kind == "CALL" or kind == "CALLCODE" then
value = toNumber(table.remove(state.stack))
end
local args_offset = toNumber(table.remove(state.stack))
local args_length = toNumber(table.remove(state.stack))
local ret_offset = toNumber(table.remove(state.stack))
local ret_length = toNumber(table.remove(state.stack))
-- Depth limit: a call beyond MAX_CALL_DEPTH fails (pushes 0) without executing.
local depth = state.call_depth or 0
if depth >= MAX_CALL_DEPTH then
state.return_data = {}
table.insert(state.stack, 0)
state.pc = state.pc + 1
return
end
local calldata = read_memory_bytes(state, args_offset, args_length)
local code = lookup_code(to_address)
-- Calling an account with no code (EOA / precompile-less) succeeds with empty return.
if not code then
state.return_data = {}
write_memory_bytes(state, ret_offset, {}, 0)
table.insert(state.stack, 1)
state.pc = state.pc + 1
return
end
-- DELEGATECALL/CALLCODE run the target code in the *caller's* storage context;
-- CALL/STATICCALL switch the storage context to the target address.
local exec_address = state.address
if kind == "CALL" or kind == "STATICCALL" then
exec_address = "0x" .. normalize_address(to_address)
end
local sub = EVM.init(exec_address)
sub.calldata = calldata
sub.call_depth = depth + 1
sub.static = state.static or (kind == "STATICCALL")
sub.budget = state.budget -- share the call-tree gas budget
local ok = pcall(function()
EVM.execute(sub, hexStringToTable(code))
end)
-- Surface the callee's return data to the caller and copy into return memory.
state.return_data = sub.return_data or {}
local ret_data_len = math.min(ret_length, #state.return_data)
write_memory_bytes(state, ret_offset, state.return_data, ret_data_len)
-- Success unless the callee reverted, hit an invalid opcode, or errored.
if ok and not sub.reverted and not sub.invalid_opcode then
table.insert(state.stack, 1)
else
table.insert(state.stack, 0)
end
state.pc = state.pc + 1
end
-- Helper for modulo operations with proper zero handling
local function mod_arithmetic(a, b)
if b == 0 then
return 0
end
local result = a % b
-- Ensure result is always positive for unsigned operations
if result < 0 then
result = result + math.abs(b)
end
return result
end
-- Define opcodes
EVM.opcodes = {
-- STOP
[0x00] = function(state)
state.running = false
end,
-- ADD
[0x01] = function(state)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
table.insert(state.stack, a + b)
state.pc = state.pc + 1
end,
-- MUL
[0x02] = function(state)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
table.insert(state.stack, a * b)
state.pc = state.pc + 1
end,
-- SUB
[0x03] = function(state)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
table.insert(state.stack, a - b)
state.pc = state.pc + 1
end,
-- DIV
[0x04] = function(state)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
local result = 0
if b == 0 then
result = 0
else
result = math.floor(a / b)
end
table.insert(state.stack, result)
state.pc = state.pc + 1
end,
-- SDIV
[0x05] = function(state)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
local result
if b == 0 then
result = 0
else
result = a / b
if result < 0 then
result = math.ceil(result)
else
result = math.floor(result)
end
end
table.insert(state.stack, result)
state.pc = state.pc + 1
end,
-- MOD
[0x06] = function(state)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
local result = 0
if b == 0 then
result = 0
else
result = a % b
end
table.insert(state.stack, result)
state.pc = state.pc + 1
end,
-- SMOD (0x07) - Signed modulo operation
[0x07] = function(state)
local a = signed_arithmetic(toNumber(table.remove(state.stack)))
local b = signed_arithmetic(toNumber(table.remove(state.stack)))
local result = 0
if b == 0 then
result = 0
else
result = a % b
-- Handle sign properly for signed modulo
if (a < 0) ~= (b < 0) and result ~= 0 then
result = result + b
end
end
table.insert(state.stack, result)
state.pc = state.pc + 1
end,
-- ADDMOD (0x08) - Addition modulo operation
[0x08] = function(state)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
local N = toNumber(table.remove(state.stack))
local result = 0
if N == 0 then
result = 0
else
result = (a + b) % N
end
table.insert(state.stack, result)
state.pc = state.pc + 1
end,
-- MULMOD (0x09) - Multiplication modulo operation
[0x09] = function(state)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
local N = toNumber(table.remove(state.stack))
local result = 0
if N == 0 then
result = 0
else
result = (a * b) % N
end
table.insert(state.stack, result)
state.pc = state.pc + 1
end,
-- LT
[0x10] = function(state, bytecode)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
if a > b then
table.insert(state.stack, 1)
else
table.insert(state.stack, 0)
end
state.pc = state.pc + 1
end,
-- GT
[0x11] = function(state, bytecode)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
if a < b then
table.insert(state.stack, 1)
else
table.insert(state.stack, 0)
end
state.pc = state.pc + 1
end,
-- SLT
[0x12] = function(state, bytecode)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
if a > b then
table.insert(state.stack, 1)
else
table.insert(state.stack, 0)
end
state.pc = state.pc + 1
end,
-- SGT
[0x13] = function(state, bytecode)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
if a < b then
table.insert(state.stack, 1)
else
table.insert(state.stack, 0)
end
state.pc = state.pc + 1
end,
-- EQ
[0x14] = function(state, bytecode)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
if a == b then
table.insert(state.stack, 1)
else
table.insert(state.stack, 0)
end
state.pc = state.pc + 1
end,
-- ISZERO
[0x15] = function(state, bytecode)
local a = toNumber(table.remove(state.stack))
if a == 0 then
table.insert(state.stack, 1)
else
table.insert(state.stack, 0)
end
state.pc = state.pc + 1
end,
-- AND
[0x16] = function(state)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
table.insert(state.stack, band64(a, b))
state.pc = state.pc + 1
end,
-- OR
[0x17] = function(state)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
local result = 0
local bit = 1
while a > 0 or b > 0 do
if (a % 2 == 1) or (b % 2 == 1) then
result = result + bit
end
a = math.floor(a / 2)
b = math.floor(b / 2)
bit = bit * 2
end
table.insert(state.stack, result)
state.pc = state.pc + 1
end,
-- XOR (0x18) - Bitwise exclusive OR
[0x18] = function(state)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
table.insert(state.stack, bxor64(a, b))
state.pc = state.pc + 1
end,
-- NOT
[0x19] = function(state)
local a = toNumber(table.remove(state.stack))
table.insert(state.stack, bnot64(a))
state.pc = state.pc + 1
end,
-- BYTE (0x1A) - Extract byte from 32-byte word
[0x1A] = function(state)
local i = toNumber(table.remove(state.stack))
local val = toNumber(table.remove(state.stack))
if i >= 32 then
table.insert(state.stack, 0)
else
-- Extract byte at position i (0 is most significant byte)
-- Convert to 32-byte representation and extract byte
local bytes = {}
local temp_val = val
-- Fill bytes array (little endian)
for j = 0, 31 do
bytes[31 - j] = temp_val % 256
temp_val = math.floor(temp_val / 256)
end
table.insert(state.stack, bytes[i] or 0)
end
state.pc = state.pc + 1
end,
-- EXP
[0x0A] = function(state)
local a = toNumber(table.remove(state.stack))
local b = toNumber(table.remove(state.stack))
local result = 1
for i = 1, b do
result = result * a
end
table.insert(state.stack, result)
state.pc = state.pc + 1
end,
-- SIGNEXTEND (0x0B) - Sign extension operation
[0x0B] = function(state)
local i = toNumber(table.remove(state.stack))
local x = toNumber(table.remove(state.stack))
if i >= 32 then
-- If i >= 32, no sign extension needed
table.insert(state.stack, x)
else
-- Sign extend from byte i
local sign_bit_pos = (i * 8) + 7
local sign_bit = math.floor(x / (2^sign_bit_pos)) % 2
if sign_bit == 1 then
-- Negative number - set all higher bits to 1
local mask = 0
for bit = sign_bit_pos + 1, 255 do
mask = mask + (2^bit)
end
x = x + mask
else
-- Positive number - clear all higher bits
local mask = (2^(sign_bit_pos + 1)) - 1
x = x % (mask + 1)
end
end
table.insert(state.stack, x)
state.pc = state.pc + 1
end,
-- KECCAK256 (SHA3)
[0x20] = function(state)
local offset = toNumber(table.remove(state.stack))
local length = clamp_length(toNumber(table.remove(state.stack)))
local data = {}
for i = 1, length do
data[i] = state.memory[offset + i - 1] or 0
end
local hash = keccak256(data)
table.insert(state.stack, hash)
state.pc = state.pc + 1
end,
-- ADDRESS
[0x30] = function(state)
table.insert(state.stack, state.address)
state.pc = state.pc + 1
end,
-- BALANCE (0x31) - Get account balance
[0x31] = function(state)
local address = table.remove(state.stack)
local balance_key = balance_key(address)
local balance = get_env_data(balance_key, "0")
table.insert(state.stack, toNumber(balance))
state.pc = state.pc + 1
end,
-- ORIGIN (0x32) - Get transaction origin address
[0x32] = function(state)
local origin = get_env_data("ORIGIN", "0x0000000000000000000000000000000000000000")
-- Keep as string to preserve full address
table.insert(state.stack, origin)
state.pc = state.pc + 1
end,
-- CALLER
[0x33] = function(state)
local caller = redis.call("GET", "CALLER") or "0x0000000000000000000000000000000000000000"
table.insert(state.stack, caller)
state.pc = state.pc + 1
end,
-- GASPRICE
[0x3A] = function(state)
local gasprice = redis.call("GET", "GASPRICE") or 1000000000
table.insert(state.stack, toNumber(gasprice))
state.pc = state.pc + 1
end,
-- CALLVALUE
[0x34] = function(state)
local value = redis.call("GET", "CALLVALUE") or 0
table.insert(state.stack, toNumber(value))
state.pc = state.pc + 1
end,
-- CALLDATALOAD
[0x35] = function(state)
local offset = toNumber(table.remove(state.stack))
local data_bytes = get_calldata_bytes(state)
local value = 0
for i = 0, 31 do
local byte_val = data_bytes[offset + i + 1] or 0
value = value + (byte_val * (256 ^ (31 - i)))
end
table.insert(state.stack, value)
state.pc = state.pc + 1
end,
-- CALLDATASIZE
[0x36] = function(state)
local data_bytes = get_calldata_bytes(state)
table.insert(state.stack, #data_bytes)
state.pc = state.pc + 1
end,
-- CALLDATACOPY (0x37) - Copy calldata to memory
[0x37] = function(state)
local dest_offset = toNumber(table.remove(state.stack))
local data_offset = toNumber(table.remove(state.stack))
local length = clamp_length(toNumber(table.remove(state.stack)))
-- Get active calldata (nested call args or Redis CALLDATA)
local data_bytes = get_calldata_bytes(state)