Skip to content

Commit

Permalink
feat(EVM): Optimize extcodecopy (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov authored Oct 30, 2024
1 parent 19a6872 commit 7a4a895
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
54 changes: 40 additions & 14 deletions system-contracts/contracts/EvmEmulator.yul
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ object "EvmEmulator" {
max_uint := 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
}

function MAX_UINT64() -> max {
max := sub(shl(64, 1), 1)
}

// Each evm gas is 5 zkEVM one
function GAS_DIVISOR() -> gas_div { gas_div := 5 }

Expand Down Expand Up @@ -1662,14 +1666,23 @@ object "EvmEmulator" {
}

evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

$llvm_AlwaysInline_llvm$_memsetToZero(dstOffset, len)

// Gets the code from the addr
if and(iszero(iszero(getRawCodeHash(addr))), gt(len, 0)) {
pop(fetchDeployedCode(addr, add(dstOffset, MEM_OFFSET()), srcOffset, len))
}

if gt(srcOffset, MAX_UINT64()) {
srcOffset := MAX_UINT64()
}

if gt(len, 0) {
let realCodeLen
if getRawCodeHash(addr) {
// Gets the code from the addr
realCodeLen := fetchDeployedCode(addr, add(dstOffset, MEM_OFFSET()), srcOffset, len)
}

if lt(realCodeLen, len) {
$llvm_AlwaysInline_llvm$_memsetToZero(add(dstOffset, realCodeLen), sub(len, realCodeLen))
}
}

ip := add(ip, 1)
}
case 0x3D { // OP_RETURNDATASIZE
Expand Down Expand Up @@ -3178,6 +3191,10 @@ object "EvmEmulator" {
max_uint := 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
}

function MAX_UINT64() -> max {
max := sub(shl(64, 1), 1)
}

// Each evm gas is 5 zkEVM one
function GAS_DIVISOR() -> gas_div { gas_div := 5 }

Expand Down Expand Up @@ -4665,14 +4682,23 @@ object "EvmEmulator" {
}

evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

$llvm_AlwaysInline_llvm$_memsetToZero(dstOffset, len)

// Gets the code from the addr
if and(iszero(iszero(getRawCodeHash(addr))), gt(len, 0)) {
pop(fetchDeployedCode(addr, add(dstOffset, MEM_OFFSET()), srcOffset, len))
}

if gt(srcOffset, MAX_UINT64()) {
srcOffset := MAX_UINT64()
}

if gt(len, 0) {
let realCodeLen
if getRawCodeHash(addr) {
// Gets the code from the addr
realCodeLen := fetchDeployedCode(addr, add(dstOffset, MEM_OFFSET()), srcOffset, len)
}

if lt(realCodeLen, len) {
$llvm_AlwaysInline_llvm$_memsetToZero(add(dstOffset, realCodeLen), sub(len, realCodeLen))
}
}

ip := add(ip, 1)
}
case 0x3D { // OP_RETURNDATASIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ function MAX_UINT() -> max_uint {
max_uint := 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
}

function MAX_UINT64() -> max {
max := sub(shl(64, 1), 1)
}

// Each evm gas is 5 zkEVM one
function GAS_DIVISOR() -> gas_div { gas_div := 5 }

Expand Down
23 changes: 16 additions & 7 deletions system-contracts/evm-emulator/EvmEmulatorLoop.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,23 @@ for { } true { } {
}

evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

$llvm_AlwaysInline_llvm$_memsetToZero(dstOffset, len)

// Gets the code from the addr
if and(iszero(iszero(getRawCodeHash(addr))), gt(len, 0)) {
pop(fetchDeployedCode(addr, add(dstOffset, MEM_OFFSET()), srcOffset, len))
}

if gt(srcOffset, MAX_UINT64()) {
srcOffset := MAX_UINT64()
}

if gt(len, 0) {
let realCodeLen
if getRawCodeHash(addr) {
// Gets the code from the addr
realCodeLen := fetchDeployedCode(addr, add(dstOffset, MEM_OFFSET()), srcOffset, len)
}

if lt(realCodeLen, len) {
$llvm_AlwaysInline_llvm$_memsetToZero(add(dstOffset, realCodeLen), sub(len, realCodeLen))
}
}

ip := add(ip, 1)
}
case 0x3D { // OP_RETURNDATASIZE
Expand Down

0 comments on commit 7a4a895

Please sign in to comment.