Skip to content

Commit d9986b8

Browse files
committed
update call_gas_extra() in stp
This updates the `call_gas_extra()` function within the `stp` module with a more efficient implementation.
1 parent 05ae52e commit d9986b8

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

constants/evm.zkasm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const EVM_INST_SHL = 0x1b
88
const EVM_INST_SHR = 0x1c
99
const EVM_INST_SAR = 0x1d
1010
const EVM_INST_CREATE = 0xF0
11+
const EVM_INST_CALL = 0xF1
12+
const EVM_INST_CALLCODE = 0xF2
1113
const EVM_INST_CREATE2 = 0xF5
1214

1315
;; =============================================================================

stp/stp.zkasm

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,41 +74,30 @@ failed_pre:
7474
fn call_gas_extra(inst=0xf1 u8, value u256, warm u1, exists u1) -> (gas_extra=2600 u64, stipend u64)
7575
;; PRE: inst in {0xf1,0xf2,0xf4,0xf5]
7676
{
77-
var gas_transfer u16
78-
var gas_new_account u16
7977
var gas_access u16
78+
var notWarm, notExists u1
79+
;; inversions
80+
notWarm = 1 - warm
81+
notExists = 1 - exists
82+
;; calculate gas access cost
83+
gas_access = (warm * G_WARMACCESS) + (notWarm * G_COLDACCOUNTACCESS)
8084
;;
81-
if inst == 0xf1 goto call_only
82-
if inst == 0xf2 goto call_other
83-
if inst == 0xf4 goto call_no_transfer
84-
if inst == 0xfa goto call_no_transfer
85-
fail
86-
call_only:
8785
if value == 0 goto call_no_transfer
88-
if exists == 1 goto call_other
86+
if inst == EVM_INST_CALL goto call
87+
if inst == EVM_INST_CALLCODE goto callcode
88+
goto call_no_transfer
89+
call:
8990
stipend = G_CALLSTIPEND
90-
gas_transfer = G_CALLVALUE
91-
gas_new_account = G_NEWACCOUNT
92-
goto call
93-
call_other:
94-
if value == 0 goto call_no_transfer
91+
gas_extra = gas_access + G_CALLVALUE + (notExists * G_NEWACCOUNT)
92+
return
93+
callcode:
9594
stipend = G_CALLSTIPEND
96-
gas_transfer = G_CALLVALUE
97-
gas_new_account = 0
98-
goto call
95+
gas_extra = gas_access + G_CALLVALUE
96+
return
9997
call_no_transfer:
100-
stipend = 0
101-
gas_transfer = 0
102-
gas_new_account = 0
103-
call:
104-
;; check for warm access
105-
if warm == 0 goto call_cold
106-
gas_access = G_WARMACCESS
107-
goto call_cont
108-
call_cold:
109-
gas_access = G_COLDACCOUNTACCESS
110-
call_cont:
111-
gas_extra = gas_access + gas_transfer + gas_new_account
98+
;; staticcall / delegatecall
99+
stipend=0
100+
gas_extra = gas_access
112101
return
113102
}
114103

0 commit comments

Comments
 (0)