From 7b4569ae2fbf192710accff256cb938296096105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Walter?= Date: Mon, 6 Nov 2023 18:56:59 +0100 Subject: [PATCH 1/4] Split constants into storages and constants --- src/kakarot/account.cairo | 4 +-- src/kakarot/constants.cairo | 24 ----------------- .../instructions/block_information.cairo | 3 ++- .../instructions/system_operations.cairo | 3 ++- src/kakarot/library.cairo | 2 +- src/kakarot/state.cairo | 2 +- src/kakarot/storages.cairo | 27 +++++++++++++++++++ tests/fixtures/EVM.cairo | 4 +-- .../kakarot/accounts/eoa/mock_kakarot.cairo | 2 +- .../instructions/test_block_information.cairo | 3 ++- .../test_environmental_information.cairo | 4 +-- .../instructions/test_system_operations.cairo | 3 ++- 12 files changed, 44 insertions(+), 37 deletions(-) create mode 100644 src/kakarot/storages.cairo diff --git a/src/kakarot/account.cairo b/src/kakarot/account.cairo index 6dd1946d6..05d000172 100644 --- a/src/kakarot/account.cairo +++ b/src/kakarot/account.cairo @@ -22,8 +22,8 @@ from starkware.cairo.common.hash_state import ( hash_update_with_hashchain, ) -from kakarot.constants import ( - Constants, +from kakarot.constants import Constants +from kakarot.storages import ( account_proxy_class_hash, native_token_address, contract_account_class_hash, diff --git a/src/kakarot/constants.cairo b/src/kakarot/constants.cairo index 58f3d79c8..b848b3c87 100644 --- a/src/kakarot/constants.cairo +++ b/src/kakarot/constants.cairo @@ -2,30 +2,6 @@ %lang starknet -@storage_var -func native_token_address() -> (res: felt) { -} - -@storage_var -func blockhash_registry_address() -> (res: felt) { -} - -@storage_var -func contract_account_class_hash() -> (value: felt) { -} - -@storage_var -func externally_owned_account_class_hash() -> (res: felt) { -} - -@storage_var -func account_proxy_class_hash() -> (res: felt) { -} - -@storage_var -func deploy_fee() -> (deploy_fee: felt) { -} - // @title Constants file. // @notice This file contains global constants. namespace Constants { diff --git a/src/kakarot/instructions/block_information.cairo b/src/kakarot/instructions/block_information.cairo index 3dbfffcdc..9fd7ccadf 100644 --- a/src/kakarot/instructions/block_information.cairo +++ b/src/kakarot/instructions/block_information.cairo @@ -12,7 +12,8 @@ from starkware.cairo.common.uint256 import Uint256 from starkware.cairo.lang.compiler.lib.registers import get_fp_and_pc // Internal dependencies -from kakarot.constants import Constants, blockhash_registry_address +from kakarot.storages import blockhash_registry_address +from kakarot.constants import Constants from kakarot.execution_context import ExecutionContext from kakarot.interfaces.interfaces import IBlockhashRegistry from kakarot.model import model diff --git a/src/kakarot/instructions/system_operations.cairo b/src/kakarot/instructions/system_operations.cairo index 014f3b965..b0e42b26b 100644 --- a/src/kakarot/instructions/system_operations.cairo +++ b/src/kakarot/instructions/system_operations.cairo @@ -20,7 +20,8 @@ from starkware.cairo.common.registers import get_fp_and_pc // Internal dependencies from kakarot.account import Account -from kakarot.constants import contract_account_class_hash, native_token_address, Constants +from kakarot.storages import contract_account_class_hash, native_token_address +from kakarot.constants import Constants from kakarot.errors import Errors from kakarot.execution_context import ExecutionContext from kakarot.memory import Memory diff --git a/src/kakarot/library.cairo b/src/kakarot/library.cairo index 9f32f1325..79c642307 100644 --- a/src/kakarot/library.cairo +++ b/src/kakarot/library.cairo @@ -12,7 +12,7 @@ from starkware.starknet.common.syscalls import get_caller_address, get_tx_info from starkware.cairo.common.math_cmp import is_not_zero from kakarot.account import Account -from kakarot.constants import ( +from kakarot.storages import ( account_proxy_class_hash, blockhash_registry_address, contract_account_class_hash, diff --git a/src/kakarot/state.cairo b/src/kakarot/state.cairo index 11b5a5430..2900a5f49 100644 --- a/src/kakarot/state.cairo +++ b/src/kakarot/state.cairo @@ -17,7 +17,7 @@ from starkware.starknet.common.syscalls import call_contract from starkware.starknet.common.syscalls import emit_event from kakarot.account import Account -from kakarot.constants import native_token_address, contract_account_class_hash +from kakarot.storages import native_token_address, contract_account_class_hash from kakarot.interfaces.interfaces import IERC20 from kakarot.model import model from utils.dict import default_dict_copy diff --git a/src/kakarot/storages.cairo b/src/kakarot/storages.cairo new file mode 100644 index 000000000..7209b9e38 --- /dev/null +++ b/src/kakarot/storages.cairo @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: MIT + +%lang starknet + +@storage_var +func native_token_address() -> (res: felt) { +} + +@storage_var +func blockhash_registry_address() -> (res: felt) { +} + +@storage_var +func contract_account_class_hash() -> (value: felt) { +} + +@storage_var +func externally_owned_account_class_hash() -> (res: felt) { +} + +@storage_var +func account_proxy_class_hash() -> (res: felt) { +} + +@storage_var +func deploy_fee() -> (deploy_fee: felt) { +} diff --git a/tests/fixtures/EVM.cairo b/tests/fixtures/EVM.cairo index 4a24bacb9..08e7034e4 100644 --- a/tests/fixtures/EVM.cairo +++ b/tests/fixtures/EVM.cairo @@ -17,12 +17,12 @@ from kakarot.library import Kakarot from kakarot.model import model from kakarot.stack import Stack from kakarot.state import Internals as State -from kakarot.constants import ( +from kakarot.constants import Constants +from kakarot.storages import ( native_token_address, contract_account_class_hash, blockhash_registry_address, account_proxy_class_hash, - Constants, ) from utils.dict import dict_keys, dict_values diff --git a/tests/src/kakarot/accounts/eoa/mock_kakarot.cairo b/tests/src/kakarot/accounts/eoa/mock_kakarot.cairo index 0a51bf6ae..8e3d00bfa 100644 --- a/tests/src/kakarot/accounts/eoa/mock_kakarot.cairo +++ b/tests/src/kakarot/accounts/eoa/mock_kakarot.cairo @@ -8,7 +8,7 @@ from starkware.cairo.common.alloc import alloc from starkware.starknet.common.syscalls import get_caller_address from kakarot.account import Account -from kakarot.constants import account_proxy_class_hash, externally_owned_account_class_hash +from kakarot.storages import account_proxy_class_hash, externally_owned_account_class_hash from kakarot.library import native_token_address, Kakarot from kakarot.interfaces.interfaces import IAccount diff --git a/tests/src/kakarot/instructions/test_block_information.cairo b/tests/src/kakarot/instructions/test_block_information.cairo index ebd9fec95..69957c3d7 100644 --- a/tests/src/kakarot/instructions/test_block_information.cairo +++ b/tests/src/kakarot/instructions/test_block_information.cairo @@ -14,7 +14,8 @@ from starkware.cairo.common.registers import get_fp_and_pc from utils.utils import Helpers from kakarot.model import model from kakarot.stack import Stack -from kakarot.constants import Constants, blockhash_registry_address +from kakarot.constants import Constants +from kakarot.storages import blockhash_registry_address from kakarot.execution_context import ExecutionContext from kakarot.instructions.block_information import BlockInformation from tests.utils.helpers import TestHelpers diff --git a/tests/src/kakarot/instructions/test_environmental_information.cairo b/tests/src/kakarot/instructions/test_environmental_information.cairo index 2a273c98a..c952f07dc 100644 --- a/tests/src/kakarot/instructions/test_environmental_information.cairo +++ b/tests/src/kakarot/instructions/test_environmental_information.cairo @@ -16,8 +16,8 @@ from openzeppelin.token.erc20.library import ERC20 // Local dependencies from kakarot.account import Account -from kakarot.constants import ( - Constants, +from kakarot.constants import Constants +from kakarot.storages import ( contract_account_class_hash, account_proxy_class_hash, native_token_address, diff --git a/tests/src/kakarot/instructions/test_system_operations.cairo b/tests/src/kakarot/instructions/test_system_operations.cairo index 5a4ff69eb..019f519b4 100644 --- a/tests/src/kakarot/instructions/test_system_operations.cairo +++ b/tests/src/kakarot/instructions/test_system_operations.cairo @@ -15,7 +15,8 @@ from starkware.starknet.common.syscalls import deploy, get_contract_address from openzeppelin.token.erc20.library import ERC20 // Local dependencies -from kakarot.constants import ( +from kakarot.constants import Constants +from kakarot.storages import ( Constants, native_token_address, contract_account_class_hash, From 564a00721efb14d5018b64fe6487a8024e37e00f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Walter?= Date: Tue, 7 Nov 2023 12:14:22 +0100 Subject: [PATCH 2/4] Move stack and static gas check to exec_opcode --- src/kakarot/constants.cairo | 1544 +++++++++++++++++ src/kakarot/evm.cairo | 69 +- .../instructions/block_information.cairo | 54 +- .../instructions/duplication_operations.cairo | 24 - .../environmental_information.cairo | 173 -- .../instructions/exchange_operations.cairo | 20 - .../instructions/logging_operations.cairo | 19 - .../instructions/memory_operations.cairo | 123 +- .../instructions/push_operations.cairo | 19 - src/kakarot/instructions/sha3.cairo | 12 - .../stop_and_math_operations.cairo | 107 +- .../instructions/system_operations.cairo | 52 - src/kakarot/model.cairo | 9 +- src/kakarot/precompiles/precompiles.cairo | 1 - 14 files changed, 1609 insertions(+), 617 deletions(-) diff --git a/src/kakarot/constants.cairo b/src/kakarot/constants.cairo index b848b3c87..7f1fada04 100644 --- a/src/kakarot/constants.cairo +++ b/src/kakarot/constants.cairo @@ -41,3 +41,1547 @@ namespace Constants { // ACCOUNTS const BYTES_PER_FELT = 16; } + +// See model.Opcode: +// number +// gas +// stack_input +// stack_size_min +// stack_size_diff +opcodes_label: +// STOP +dw 0x00; +dw 0; +dw 0; +dw 0; +dw 0; +// ADD +dw 0x01; +dw 3; +dw 2; +dw 2; +dw -1; +// MUL +dw 0x02; +dw 5; +dw 2; +dw 2; +dw -1; +// SUB +dw 0x03; +dw 3; +dw 2; +dw 2; +dw -1; +// DIV +dw 0x04; +dw 5; +dw 2; +dw 2; +dw -1; +// SDIV +dw 0x05; +dw 5; +dw 2; +dw 2; +dw -1; +// MOD +dw 0x06; +dw 5; +dw 2; +dw 2; +dw -1; +// SMOD +dw 0x07; +dw 5; +dw 2; +dw 2; +dw -1; +// ADDMOD +dw 0x08; +dw 8; +dw 3; +dw 3; +dw -1; +// MULMOD +dw 0x09; +dw 8; +dw 3; +dw 3; +dw -1; +// EXP +dw 0x0a; +dw 10; +dw 2; +dw 2; +dw -1; +// SIGNEXTEND +dw 0x0b; +dw 5; +dw 2; +dw 2; +dw -1; +// INVALID +dw 0x0c; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x0d; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x0e; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x0f; +dw 0; +dw 0; +dw 0; +dw 0; +// LT +dw 0x10; +dw 3; +dw 2; +dw 2; +dw -1; +// GT +dw 0x11; +dw 3; +dw 2; +dw 2; +dw -1; +// SLT +dw 0x12; +dw 3; +dw 2; +dw 2; +dw -1; +// SGT +dw 0x13; +dw 3; +dw 2; +dw 2; +dw -1; +// EQ +dw 0x14; +dw 3; +dw 2; +dw 2; +dw -1; +// ISZERO +dw 0x15; +dw 3; +dw 1; +dw 1; +dw 0; +// AND +dw 0x16; +dw 3; +dw 2; +dw 2; +dw -1; +// OR +dw 0x17; +dw 3; +dw 2; +dw 2; +dw -1; +// XOR +dw 0x18; +dw 3; +dw 2; +dw 2; +dw -1; +// NOT +dw 0x19; +dw 3; +dw 1; +dw 1; +dw -1; +// BYTE +dw 0x1a; +dw 3; +dw 2; +dw 2; +dw -1; +// SHL +dw 0x1b; +dw 3; +dw 2; +dw 2; +dw -1; +// SHR +dw 0x1c; +dw 3; +dw 2; +dw 2; +dw -1; +// SAR +dw 0x1d; +dw 3; +dw 2; +dw 2; +dw -1; +// INVALID +dw 0x1e; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x1f; +dw 0; +dw 0; +dw 0; +dw 0; +// SHA3 +dw 0x20; +dw 30; +dw 2; +dw 2; +dw -1; +// INVALID +dw 0x21; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x22; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x23; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x24; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x25; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x26; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x27; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x28; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x29; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x2a; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x2b; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x2c; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x2d; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x2e; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x2f; +dw 0; +dw 0; +dw 0; +dw 0; +// ADDRESS +dw 0x30; +dw 2; +dw 0; +dw 0; +dw 1; +// BALANCE +dw 0x31; +dw 100; +dw 1; +dw 1; +dw 0; +// ORIGIN +dw 0x32; +dw 2; +dw 0; +dw 0; +dw 1; +// CALLER +dw 0x33; +dw 2; +dw 0; +dw 0; +dw 1; +// CALLVALUE +dw 0x34; +dw 2; +dw 0; +dw 0; +dw 1; +// CALLDATALOAD +dw 0x35; +dw 3; +dw 1; +dw 1; +dw 0; +// CALLDATASIZE +dw 0x36; +dw 2; +dw 0; +dw 0; +dw 1; +// CALLDATACOPY +dw 0x37; +dw 3; +dw 3; +dw 3; +dw 0; +// CODESIZE +dw 0x38; +dw 2; +dw 0; +dw 0; +dw 1; +// CODECOPY +dw 0x39; +dw 3; +dw 3; +dw 3; +dw 0; +// GASPRICE +dw 0x3a; +dw 2; +dw 0; +dw 0; +dw 1; +// EXTCODESIZE +dw 0x3b; +dw 100; +dw 1; +dw 1; +dw 0; +// EXTCODECOPY +dw 0x3c; +dw 100; +dw 4; +dw 4; +dw 0; +// RETURNDATASIZE +dw 0x3d; +dw 2; +dw 0; +dw 0; +dw 1; +// RETURNDATACOPY +dw 0x3e; +dw 3; +dw 3; +dw 3; +dw 0; +// EXTCODEHASH +dw 0x3f; +dw 100; +dw 1; +dw 1; +dw 0; +// BLOCKHASH +dw 0x40; +dw 20; +dw 1; +dw 1; +dw 0; +// COINBASE +dw 0x41; +dw 2; +dw 0; +dw 0; +dw 1; +// TIMESTAMP +dw 0x42; +dw 2; +dw 0; +dw 0; +dw 1; +// NUMBER +dw 0x43; +dw 2; +dw 0; +dw 0; +dw 1; +// PREVRANDAO +dw 0x44; +dw 2; +dw 0; +dw 0; +dw 1; +// GASLIMIT +dw 0x45; +dw 2; +dw 0; +dw 0; +dw 1; +// CHAINID +dw 0x46; +dw 2; +dw 0; +dw 0; +dw 1; +// SELFBALANCE +dw 0x47; +dw 5; +dw 0; +dw 0; +dw 1; +// BASEFEE +dw 0x48; +dw 2; +dw 0; +dw 0; +dw 1; +// INVALID +dw 0x49; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x4a; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x4b; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x4c; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x4d; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x4e; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x4f; +dw 0; +dw 0; +dw 0; +dw 0; +// POP +dw 0x50; +dw 2; +dw 1; +dw 1; +dw -1; +// MLOAD +dw 0x51; +dw 3; +dw 1; +dw 1; +dw 0; +// MSTORE +dw 0x52; +dw 3; +dw 2; +dw 2; +dw -2; +// MSTORE8 +dw 0x53; +dw 3; +dw 2; +dw 2; +dw -2; +// SLOAD +dw 0x54; +dw 100; +dw 1; +dw 1; +dw 0; +// SSTORE +dw 0x55; +dw 100; +dw 2; +dw 2; +dw -2; +// JUMP +dw 0x56; +dw 8; +dw 1; +dw 1; +dw -1; +// JUMPI +dw 0x57; +dw 10; +dw 2; +dw 2; +dw -2; +// PC +dw 0x58; +dw 2; +dw 0; +dw 0; +dw 1; +// MSIZE +dw 0x59; +dw 2; +dw 0; +dw 0; +dw 1; +// GAS +dw 0x5a; +dw 2; +dw 0; +dw 0; +dw 1; +// JUMPDEST +dw 0x5b; +dw 1; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x5c; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x5d; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0x5e; +dw 0; +dw 0; +dw 0; +dw 0; +// PUSH0 +dw 0x5f; +dw 2; +dw 0; +dw 0; +dw 1; +// PUSH1 +dw 0x60; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH2 +dw 0x61; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH3 +dw 0x62; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH4 +dw 0x63; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH5 +dw 0x64; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH6 +dw 0x65; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH7 +dw 0x66; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH8 +dw 0x67; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH9 +dw 0x68; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH10 +dw 0x69; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH11 +dw 0x6a; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH12 +dw 0x6b; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH13 +dw 0x6c; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH14 +dw 0x6d; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH15 +dw 0x6e; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH16 +dw 0x6f; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH17 +dw 0x70; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH18 +dw 0x71; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH19 +dw 0x72; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH20 +dw 0x73; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH21 +dw 0x74; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH22 +dw 0x75; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH23 +dw 0x76; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH24 +dw 0x77; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH25 +dw 0x78; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH26 +dw 0x79; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH27 +dw 0x7a; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH28 +dw 0x7b; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH29 +dw 0x7c; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH30 +dw 0x7d; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH31 +dw 0x7e; +dw 3; +dw 0; +dw 0; +dw 1; +// PUSH32 +dw 0x7f; +dw 3; +dw 0; +dw 0; +dw 1; +// DUP1 +dw 0x80; +dw 3; +dw 0; +dw 1; +dw 1; +// DUP2 +dw 0x81; +dw 3; +dw 0; +dw 2; +dw 1; +// DUP3 +dw 0x82; +dw 3; +dw 0; +dw 3; +dw 1; +// DUP4 +dw 0x83; +dw 3; +dw 0; +dw 4; +dw 1; +// DUP5 +dw 0x84; +dw 3; +dw 0; +dw 5; +dw 1; +// DUP6 +dw 0x85; +dw 3; +dw 0; +dw 6; +dw 1; +// DUP7 +dw 0x86; +dw 3; +dw 0; +dw 7; +dw 1; +// DUP8 +dw 0x87; +dw 3; +dw 0; +dw 8; +dw 1; +// DUP9 +dw 0x88; +dw 3; +dw 0; +dw 9; +dw 1; +// DUP10 +dw 0x89; +dw 3; +dw 0; +dw 10; +dw 1; +// DUP11 +dw 0x8a; +dw 3; +dw 0; +dw 11; +dw 1; +// DUP12 +dw 0x8b; +dw 3; +dw 0; +dw 12; +dw 1; +// DUP13 +dw 0x8c; +dw 3; +dw 0; +dw 13; +dw 1; +// DUP14 +dw 0x8d; +dw 3; +dw 0; +dw 14; +dw 1; +// DUP15 +dw 0x8e; +dw 3; +dw 0; +dw 15; +dw 1; +// DUP16 +dw 0x8f; +dw 3; +dw 0; +dw 16; +dw 1; +// SWAP1 +dw 0x90; +dw 3; +dw 0; +dw 2; +dw 0; +// SWAP2 +dw 0x91; +dw 3; +dw 0; +dw 3; +dw 0; +// SWAP3 +dw 0x92; +dw 3; +dw 0; +dw 4; +dw 0; +// SWAP4 +dw 0x93; +dw 3; +dw 0; +dw 5; +dw 0; +// SWAP5 +dw 0x94; +dw 3; +dw 0; +dw 6; +dw 0; +// SWAP6 +dw 0x95; +dw 3; +dw 0; +dw 7; +dw 0; +// SWAP7 +dw 0x96; +dw 3; +dw 0; +dw 8; +dw 0; +// SWAP8 +dw 0x97; +dw 3; +dw 0; +dw 9; +dw 0; +// SWAP9 +dw 0x98; +dw 3; +dw 0; +dw 10; +dw 0; +// SWAP10 +dw 0x99; +dw 3; +dw 0; +dw 11; +dw 0; +// SWAP11 +dw 0x9a; +dw 3; +dw 0; +dw 12; +dw 0; +// SWAP12 +dw 0x9b; +dw 3; +dw 0; +dw 13; +dw 0; +// SWAP13 +dw 0x9c; +dw 3; +dw 0; +dw 14; +dw 0; +// SWAP14 +dw 0x9d; +dw 3; +dw 0; +dw 15; +dw 0; +// SWAP15 +dw 0x9e; +dw 3; +dw 0; +dw 16; +dw 0; +// SWAP16 +dw 0x9f; +dw 3; +dw 0; +dw 17; +dw 0; +// LOG0 +dw 0xa0; +dw 375; +dw 2; +dw 2; +dw -2; +// LOG1 +dw 0xa1; +dw 750; +dw 3; +dw 3; +dw -3; +// LOG2 +dw 0xa2; +dw 1125; +dw 4; +dw 4; +dw -4; +// LOG3 +dw 0xa3; +dw 1500; +dw 5; +dw 5; +dw -5; +// LOG4 +dw 0xa4; +dw 1875; +dw 6; +dw 6; +dw -6; +// INVALID +dw 0xa5; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xa6; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xa7; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xa8; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xa9; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xaa; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xab; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xac; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xad; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xae; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xaf; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xb0; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xb1; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xb2; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xb3; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xb4; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xb5; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xb6; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xb7; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xb8; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xb9; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xba; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xbb; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xbc; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xbd; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xbe; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xbf; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xc0; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xc1; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xc2; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xc3; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xc4; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xc5; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xc6; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xc7; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xc8; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xc9; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xca; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xcb; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xcc; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xcd; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xce; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xcf; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xd0; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xd1; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xd2; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xd3; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xd4; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xd5; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xd6; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xd7; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xd8; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xd9; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xda; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xdb; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xdc; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xdd; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xde; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xdf; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xe0; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xe1; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xe2; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xe3; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xe4; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xe5; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xe6; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xe7; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xe8; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xe9; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xea; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xeb; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xec; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xed; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xee; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xef; +dw 0; +dw 0; +dw 0; +dw 0; +// CREATE +dw 0xf0; +dw 32000; +dw 3; +dw 3; +dw -2; +// CALL +dw 0xf1; +dw 100; +dw 7; +dw 7; +dw -6; +// CALLCODE +dw 0xf2; +dw 100; +dw 7; +dw 7; +dw -6; +// RETURN +dw 0xf3; +dw 0; +dw 2; +dw 2; +dw -2; +// DELEGATECALL +dw 0xf4; +dw 100; +dw 6; +dw 6; +dw -5; +// CREATE2 +dw 0xf5; +dw 32000; +dw 4; +dw 4; +dw -3; +// INVALID +dw 0xf6; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xf7; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xf8; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xf9; +dw 0; +dw 0; +dw 0; +dw 0; +// STATICCALL +dw 0xfa; +dw 100; +dw 6; +dw 6; +dw -5; +// INVALID +dw 0xfb; +dw 0; +dw 0; +dw 0; +dw 0; +// INVALID +dw 0xfc; +dw 0; +dw 0; +dw 0; +dw 0; +// REVERT +dw 0xfd; +dw 0; +dw 2; +dw 2; +dw -2; +// INVALID +dw 0xfe; +dw 0; +dw 0; +dw 0; +dw 0; +// SELFDESTRUCT +dw 0xff; +dw 5000; +dw 1; +dw 1; +dw -1; diff --git a/src/kakarot/evm.cairo b/src/kakarot/evm.cairo index 51574c589..6e387ea54 100644 --- a/src/kakarot/evm.cairo +++ b/src/kakarot/evm.cairo @@ -3,19 +3,14 @@ %lang starknet // Starkware dependencies -from starkware.cairo.common.alloc import alloc from starkware.cairo.common.bool import FALSE, TRUE from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin -from starkware.cairo.common.invoke import invoke -from starkware.cairo.common.math import assert_nn -from starkware.cairo.common.math_cmp import is_le, is_not_zero -from starkware.cairo.common.memcpy import memcpy -from starkware.cairo.common.registers import get_ap -from starkware.cairo.common.registers import get_label_location -from starkware.cairo.common.uint256 import Uint256 +from starkware.cairo.common.math_cmp import is_le +from starkware.cairo.lang.compiler.lib.registers import get_fp_and_pc // Internal dependencies from kakarot.account import Account +from kakarot.constants import opcodes_label, Constants from kakarot.errors import Errors from kakarot.execution_context import ExecutionContext from kakarot.instructions.block_information import BlockInformation @@ -56,7 +51,7 @@ namespace EVM { // @dev The function uses an internal jump table to execute the corresponding opcode // @param ctx The pointer to the execution context. // @return ExecutionContext The pointer to the updated execution context. - func decode_and_execute{ + func exec_opcode{ syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, @@ -64,24 +59,60 @@ namespace EVM { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - // Retrieve the current program counter. + // Get the current opcode number let pc = ctx.program_counter; - local opcode; + local opcode: model.Opcode*; let is_pc_ge_code_len = is_le(ctx.call_context.bytecode_len, pc); - + local opcode_number; if (is_pc_ge_code_len != FALSE) { - assert opcode = 0; + assert opcode_number = 0; } else { - assert opcode = [ctx.call_context.bytecode + pc]; + assert opcode_number = [ctx.call_context.bytecode + pc]; } - // Compute the corresponding offset in the jump table: - // count 1 for "next line" and 3 steps per opcode: call, opcode, ret - tempvar offset = 1 + 3 * opcode; + // Get the corresponding opcode data + // To cast the codeoffset opcodes_label to a model.Opcode*, we need to use it to offset + // the current pc. We get the pc from the `get_fp_and_pc` util and assign a codeoffset (pc_label) to it. + // In short, this boils down to: opcode = pc + offset - pc = offset + let (_, cairo_pc) = get_fp_and_pc(); + + pc_label: + assert opcode = cast( + cairo_pc + (opcodes_label - pc_label) + opcode_number * model.Opcode.SIZE, model.Opcode* + ); + + // Check stack over/under flow + let stack_underflow = is_le(ctx.stack.size, opcode.stack_size_min - 1); + if (stack_underflow != 0) { + let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); + let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); + return ctx; + } + let stack_overflow = is_le( + Constants.STACK_MAX_DEPTH, ctx.stack.size + opcode.stack_size_diff + 1 + ); + if (stack_overflow != 0) { + let (revert_reason_len, revert_reason) = Errors.stackOverflow(); + let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); + return ctx; + } + + // Check static gas + let out_of_gas = is_le(ctx.call_context.gas_limit, ctx.gas_used + opcode.gas - 1); + if (out_of_gas != 0) { + let (revert_reason_len, revert_reason) = Errors.outOfGas(); + let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); + return ctx; + } - // move program counter + 1 after opcode is read + // Update ctx let ctx = ExecutionContext.increment_program_counter(self=ctx, inc_value=1); + let ctx = ExecutionContext.increment_gas_used(ctx, opcode.gas); + + // Compute the corresponding offset in the jump table: + // count 1 for "next line" and 3 steps per opcode: call, opcode, ret + tempvar offset = 1 + 3 * opcode_number; // Prepare arguments [ap] = syscall_ptr, ap++; @@ -634,7 +665,7 @@ namespace EVM { } } - let ctx = decode_and_execute(ctx); + let ctx = exec_opcode(ctx); return run(ctx); } diff --git a/src/kakarot/instructions/block_information.cairo b/src/kakarot/instructions/block_information.cairo index 9fd7ccadf..ddaf34556 100644 --- a/src/kakarot/instructions/block_information.cairo +++ b/src/kakarot/instructions/block_information.cairo @@ -13,7 +13,7 @@ from starkware.cairo.lang.compiler.lib.registers import get_fp_and_pc // Internal dependencies from kakarot.storages import blockhash_registry_address -from kakarot.constants import Constants +from kakarot.constants import Constants, opcodes_label from kakarot.execution_context import ExecutionContext from kakarot.interfaces.interfaces import IBlockhashRegistry from kakarot.model import model @@ -35,12 +35,6 @@ namespace BlockInformation { local range_check: felt; local opcode: model.Opcode*; - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // See evm.cairo, pc is increased before entering the opcode let opcode_number = [ctx.call_context.bytecode + ctx.program_counter - 1]; @@ -52,16 +46,9 @@ namespace BlockInformation { pc_label: assert opcode = cast( - pc + (opcodes_label - pc_label) + (opcode_number - 0x40) * model.Opcode.SIZE, - model.Opcode*, + pc + (opcodes_label - pc_label) + opcode_number * model.Opcode.SIZE, model.Opcode* ); - let out_of_gas = is_le(ctx.call_context.gas_limit, ctx.gas_used + opcode.gas - 1); - if (out_of_gas != 0) { - let (revert_reason_len, revert_reason) = Errors.outOfGas(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } assert range_check = range_check_ptr; tempvar offset = 2 * (opcode_number - 0x40) + 1; @@ -172,7 +159,6 @@ namespace BlockInformation { // Finalize opcode let stack = Stack.push_uint256(ctx.stack, result); let ctx = ExecutionContext.update_stack(ctx, stack); - let ctx = ExecutionContext.increment_gas_used(ctx, opcode.gas); return ctx; } } @@ -181,12 +167,6 @@ namespace Internals { func blockhash{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( ctx: model.ExecutionContext* ) -> (model.ExecutionContext*, Uint256) { - if (ctx.stack.size == 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return (ctx, Uint256(0, 0)); - } - let (stack, block_number_uint256) = Stack.pop(ctx.stack); let ctx = ExecutionContext.update_stack(ctx, stack); let block_number = block_number_uint256.low; @@ -217,33 +197,3 @@ namespace Internals { return (ctx, balance); } } - -// See model.Opcode -opcodes_label: -// BLOCKHASH -dw 20; // gas -dw 1; // stack_input -// COINBASE -dw 2; // gas -dw 0; // stack_input -// TIMESTAMP -dw 2; // gas -dw 0; // stack_input -// NUMBER -dw 2; // gas -dw 0; // stack_input -// PREVRANDAO -dw 2; // gas -dw 0; // stack_input -// GASLIMIT -dw 2; // gas -dw 0; // stack_input -// CHAINID -dw 2; // gas -dw 0; // stack_input -// SELFBALANCE -dw 5; // gas -dw 0; // stack_input -// BASEFEE -dw 2; // gas -dw 0; // stack_input diff --git a/src/kakarot/instructions/duplication_operations.cairo b/src/kakarot/instructions/duplication_operations.cairo index ec7db85c4..2dfabaaa0 100644 --- a/src/kakarot/instructions/duplication_operations.cairo +++ b/src/kakarot/instructions/duplication_operations.cairo @@ -16,9 +16,6 @@ from kakarot.constants import Constants // @title Duplication operations opcodes. namespace DuplicationOperations { - // Define constants. - const GAS_COST_DUP = 3; - // @notice Generic DUP operation // @dev Duplicate the top i-th stack item to the top of the stack. // @param ctx The pointer to the execution context. @@ -29,35 +26,14 @@ namespace DuplicationOperations { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - let out_of_gas = is_le(ctx.call_context.gas_limit, ctx.gas_used + GAS_COST_DUP - 1); - if (out_of_gas != 0) { - let (revert_reason_len, revert_reason) = Errors.outOfGas(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // See evm.cairo, pc is increased before entering the opcode let opcode_number = [ctx.call_context.bytecode + ctx.program_counter - 1]; let i = opcode_number - 0x7F; - let stack_underflow = is_le(ctx.stack.size, i - 1); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let (stack, element) = Stack.peek(ctx.stack, i - 1); let stack = Stack.push(stack, element); let ctx = ExecutionContext.update_stack(ctx, stack); - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=GAS_COST_DUP); return ctx; } diff --git a/src/kakarot/instructions/environmental_information.cairo b/src/kakarot/instructions/environmental_information.cairo index dec95fc9b..eaf503e84 100644 --- a/src/kakarot/instructions/environmental_information.cairo +++ b/src/kakarot/instructions/environmental_information.cairo @@ -26,24 +26,6 @@ from kakarot.constants import Constants // @title Environmental information opcodes. // @notice This file contains the functions to execute for environmental information opcodes. namespace EnvironmentalInformation { - // Define constants. - const GAS_COST_ADDRESS = 2; - const GAS_COST_BALANCE = 100; - const GAS_COST_ORIGIN = 2; - const GAS_COST_CALLER = 2; - const GAS_COST_CALLVALUE = 2; - const GAS_COST_CALLDATALOAD = 3; - const GAS_COST_CALLDATASIZE = 2; - const GAS_COST_CALLDATACOPY = 3; - const GAS_COST_CODESIZE = 2; - const GAS_COST_CODECOPY = 3; - const GAS_COST_GASPRICE = 2; - const GAS_COST_EXTCODESIZE = 2600; - const GAS_COST_EXTCODECOPY = 2600; - const GAS_COST_RETURNDATASIZE = 2; - const GAS_COST_RETURNDATACOPY = 3; - const GAS_COST_EXTCODEHASH = 2600; - // @notice ADDRESS operation. // @dev Get address of currently executing account. // @custom:since Frontier @@ -59,22 +41,12 @@ namespace EnvironmentalInformation { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - alloc_locals; - - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // Get the current execution contract from the context, // convert to Uint256, and push to Stack. let address = Helpers.to_uint256(ctx.call_context.address.evm); let stack: model.Stack* = Stack.push(ctx.stack, address); // Update the execution context. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=GAS_COST_ADDRESS); return ctx; } @@ -95,12 +67,6 @@ namespace EnvironmentalInformation { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - if (ctx.stack.size == 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let (stack, address_uint256) = Stack.pop(ctx.stack); let evm_address = Helpers.uint256_to_felt([address_uint256]); @@ -111,7 +77,6 @@ namespace EnvironmentalInformation { let ctx = ExecutionContext.update_stack(ctx, stack); let ctx = ExecutionContext.update_state(ctx, state); - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_BALANCE); return ctx; } @@ -130,21 +95,10 @@ namespace EnvironmentalInformation { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - alloc_locals; - - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let origin_address = Helpers.to_uint256(ctx.call_context.origin.evm); - // Update Context stack let stack: model.Stack* = Stack.push(self=ctx.stack, element=origin_address); let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=GAS_COST_ORIGIN); return ctx; } @@ -163,14 +117,6 @@ namespace EnvironmentalInformation { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - alloc_locals; - - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let calling_context = ctx.call_context.calling_context; let is_root = ExecutionContext.is_empty(calling_context); if (is_root == 0) { @@ -184,8 +130,6 @@ namespace EnvironmentalInformation { // Update the execution context. // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=GAS_COST_CALLER); return ctx; } @@ -203,20 +147,10 @@ namespace EnvironmentalInformation { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let uint256_value = Helpers.to_uint256(ctx.call_context.value); let stack: model.Stack* = Stack.push(ctx.stack, uint256_value); - // Update the execution context. - // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_CALLVALUE); return ctx; } @@ -237,12 +171,6 @@ namespace EnvironmentalInformation { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - if (ctx.stack.size == 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = ctx.stack; // Stack input: @@ -265,8 +193,6 @@ namespace EnvironmentalInformation { // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=GAS_COST_CALLDATALOAD); return ctx; } @@ -285,19 +211,11 @@ namespace EnvironmentalInformation { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack: model.Stack* = Stack.push_uint128(ctx.stack, ctx.call_context.calldata_len); // Update the execution context. // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_CALLDATASIZE); return ctx; } @@ -318,13 +236,6 @@ namespace EnvironmentalInformation { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - let stack_underflow = is_le(ctx.stack.size, 2); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = ctx.stack; // Stack input: @@ -356,8 +267,6 @@ namespace EnvironmentalInformation { let ctx = ExecutionContext.update_memory(ctx, memory); // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=GAS_COST_CALLDATACOPY); return ctx; } @@ -376,12 +285,6 @@ namespace EnvironmentalInformation { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // Get the bytecode size. let code_size = Helpers.to_uint256(ctx.call_context.bytecode_len); @@ -390,8 +293,6 @@ namespace EnvironmentalInformation { // Update the execution context. // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=GAS_COST_CODESIZE); return ctx; } @@ -412,13 +313,6 @@ namespace EnvironmentalInformation { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - let stack_underflow = is_le(ctx.stack.size, 2); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = ctx.stack; // Stack input: @@ -449,8 +343,6 @@ namespace EnvironmentalInformation { let ctx = ExecutionContext.update_memory(ctx, memory); // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_CODECOPY); return ctx; } @@ -469,21 +361,11 @@ namespace EnvironmentalInformation { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - alloc_locals; - - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // Get the gasprice. let stack: model.Stack* = Stack.push_uint128(ctx.stack, ctx.call_context.gas_price); // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_GASPRICE); return ctx; } @@ -505,12 +387,6 @@ namespace EnvironmentalInformation { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - if (ctx.stack.size == 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = ctx.stack; // Stack input: @@ -527,11 +403,6 @@ namespace EnvironmentalInformation { let ctx = ExecutionContext.update_stack(ctx, stack); let ctx = ExecutionContext.update_state(ctx, state); - // TODO:distinction between warm and cold addresses determines dynamic cost - // for now we assume a cold address, which sets dynamic cost to 2600 - // see: https://www.evm.codes/about#accesssets - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=GAS_COST_EXTCODESIZE); - return ctx; } @@ -552,13 +423,6 @@ namespace EnvironmentalInformation { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - let stack_underflow = is_le(ctx.stack.size, 3); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = ctx.stack; // Stack input: @@ -600,17 +464,6 @@ namespace EnvironmentalInformation { let ctx = ExecutionContext.update_stack(ctx, stack); let ctx = ExecutionContext.update_state(ctx, state); - // Increment gas used. - let (minimum_word_size) = Helpers.minimum_word_count(size.low); - - // TODO:distinction between warm and cold addresses determines `address_access_cost` - // for now we assume a cold address, which sets `address_access_cost` to 2600 - // see: https://www.evm.codes/about#accesssets - - let ctx = ExecutionContext.increment_gas_used( - self=ctx, inc_value=3 * minimum_word_size + memory_expansion_cost + GAS_COST_EXTCODECOPY - ); - return ctx; } @@ -629,20 +482,12 @@ namespace EnvironmentalInformation { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // Get return data size. let stack: model.Stack* = Stack.push_uint128(ctx.stack, ctx.return_data_len); // Update the execution context. // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=GAS_COST_RETURNDATASIZE); return ctx; } @@ -663,13 +508,6 @@ namespace EnvironmentalInformation { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - let stack_underflow = is_le(ctx.stack.size, 2); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = ctx.stack; // Stack input: @@ -699,8 +537,6 @@ namespace EnvironmentalInformation { let ctx = ExecutionContext.update_memory(ctx, memory); // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_CALLDATACOPY); return ctx; } @@ -721,12 +557,6 @@ namespace EnvironmentalInformation { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - if (ctx.stack.size == 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = ctx.stack; // Stack input: @@ -765,9 +595,6 @@ namespace EnvironmentalInformation { let ctx = ExecutionContext.update_stack(ctx, stack); let ctx = ExecutionContext.update_state(ctx, state); - // Increment gas used (COLD ACCESS) - // see: https://www.evm.codes/about#accesssets - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=GAS_COST_EXTCODEHASH); return ctx; } } diff --git a/src/kakarot/instructions/exchange_operations.cairo b/src/kakarot/instructions/exchange_operations.cairo index 0216f2845..5cb8139a3 100644 --- a/src/kakarot/instructions/exchange_operations.cairo +++ b/src/kakarot/instructions/exchange_operations.cairo @@ -15,9 +15,6 @@ from kakarot.errors import Errors // @title Exchange operations opcodes. namespace ExchangeOperations { - // Define constants. - const GAS_COST_SWAP = 3; - // @notice Generic SWAP operation // @dev Exchange 1st and i-th stack items // @param ctx The pointer to the execution context @@ -28,28 +25,11 @@ namespace ExchangeOperations { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - let out_of_gas = is_le(ctx.call_context.gas_limit, ctx.gas_used + GAS_COST_SWAP - 1); - if (out_of_gas != 0) { - let (revert_reason_len, revert_reason) = Errors.outOfGas(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // See evm.cairo, pc is increased before entering the opcode let opcode_number = [ctx.call_context.bytecode + ctx.program_counter - 1]; let i = opcode_number - 0x8f; - - // SWAP_i_ requires i + 1 items in the stack - let stack_underflow = is_le(ctx.stack.size, i); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = Stack.swap_i(ctx.stack, i); let ctx = ExecutionContext.update_stack(ctx, stack); - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=GAS_COST_SWAP); return ctx; } diff --git a/src/kakarot/instructions/logging_operations.cairo b/src/kakarot/instructions/logging_operations.cairo index f5ab552c1..62e423c2f 100644 --- a/src/kakarot/instructions/logging_operations.cairo +++ b/src/kakarot/instructions/logging_operations.cairo @@ -20,9 +20,6 @@ from utils.utils import Helpers // @title Logging operations opcodes. // @notice This file contains the functions to execute for logging operations opcodes. namespace LoggingOperations { - // Define constants. - const GAS_LOG_STATIC = 350; - // @notice Generic logging operation // @dev Append log record with n topics. // @custom:since Frontier @@ -44,24 +41,10 @@ namespace LoggingOperations { return ctx; } - let out_of_gas = is_le(ctx.call_context.gas_limit, ctx.gas_used + GAS_LOG_STATIC - 1); - if (out_of_gas != 0) { - let (revert_reason_len, revert_reason) = Errors.outOfGas(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // See evm.cairo, pc is increased before entering the opcode let opcode_number = [ctx.call_context.bytecode + ctx.program_counter - 1]; let topics_len = opcode_number - 0xa0; - let stack_underflow = is_le(ctx.stack.size, topics_len + 1); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // Get stack from context. let stack: model.Stack* = ctx.stack; @@ -86,8 +69,6 @@ namespace LoggingOperations { // Update context stack. let ctx = ExecutionContext.update_memory(ctx, memory); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, gas_cost + GAS_LOG_STATIC); return ctx; } } diff --git a/src/kakarot/instructions/memory_operations.cairo b/src/kakarot/instructions/memory_operations.cairo index dbe75a6d1..c14d312e6 100644 --- a/src/kakarot/instructions/memory_operations.cairo +++ b/src/kakarot/instructions/memory_operations.cairo @@ -25,19 +25,6 @@ from kakarot.constants import Constants // @author @LucasLvy @abdelhamidbakhta // @custom:namespace MemoryOperations namespace MemoryOperations { - const GAS_COST_MLOAD = 3; - const GAS_COST_MSTORE = 3; - const GAS_COST_PC = 2; - const GAS_COST_MSIZE = 2; - const GAS_COST_JUMP = 8; - const GAS_COST_JUMPI = 10; - const GAS_COST_JUMPDEST = 1; - const GAS_COST_POP = 2; - const GAS_COST_MSTORE8 = 3; - const GAS_COST_SSTORE = 100; - const GAS_COST_SLOAD = 100; - const GAS_COST_GAS = 2; - // @notice MLOAD operation // @dev Load word from memory and push to stack. // @custom:since Frontier @@ -55,12 +42,6 @@ namespace MemoryOperations { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - if (ctx.stack.size == 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = ctx.stack; // Stack input: @@ -77,8 +58,6 @@ namespace MemoryOperations { let ctx = ExecutionContext.update_memory(ctx, new_memory); // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_MLOAD + cost); return ctx; } @@ -100,13 +79,6 @@ namespace MemoryOperations { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - let stack_underflow = is_le(ctx.stack.size, 1); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = ctx.stack; // Stack input: @@ -123,8 +95,6 @@ namespace MemoryOperations { // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_MSTORE); return ctx; } @@ -140,19 +110,10 @@ namespace MemoryOperations { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - alloc_locals; - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack: model.Stack* = Stack.push_uint128(ctx.stack, ctx.program_counter - 1); // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_PC); return ctx; } @@ -169,19 +130,10 @@ namespace MemoryOperations { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - alloc_locals; - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = Stack.push_uint128(ctx.stack, ctx.memory.bytes_len); // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_MSIZE); return ctx; } @@ -199,23 +151,9 @@ namespace MemoryOperations { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - alloc_locals; - - if (ctx.stack.size == 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - - let stack = ctx.stack; - - // Stack input: - // 0 - offset: offset in the deployed code where execution will continue from - let (stack, offset) = Stack.pop(stack); - - let ctx = ExecutionContext.jump(ctx, offset.low); + let (stack, offset) = Stack.pop(ctx.stack); let ctx = ExecutionContext.update_stack(ctx, stack); - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_JUMP); + let ctx = ExecutionContext.jump(ctx, offset.low); return ctx; } @@ -236,13 +174,6 @@ namespace MemoryOperations { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - let stack_underflow = is_le(ctx.stack.size, 1); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = ctx.stack; // Stack input: @@ -253,7 +184,6 @@ namespace MemoryOperations { let skip_condition = popped[1]; let ctx = ExecutionContext.update_stack(ctx, stack); - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_JUMPI); // If skip_condition is 0, then don't jump let (skip_condition_is_zero) = uint256_eq(Uint256(0, 0), skip_condition); @@ -266,7 +196,7 @@ namespace MemoryOperations { } // @notice JUMPDEST operation - // @dev Serves as a check that JUMP or JUMPI was executed correctly. We only update gas used. + // @dev Serves as a check that JUMP or JUMPI was executed correctly. // @custom:since Frontier // @custom:group Stack Memory Storage and Flow operations. // @custom:stack_produced_elements 0 @@ -279,9 +209,6 @@ namespace MemoryOperations { bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_JUMPDEST); - return ctx; } @@ -299,14 +226,6 @@ namespace MemoryOperations { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - alloc_locals; - - if (ctx.stack.size == 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // Get stack from context. let stack: model.Stack* = ctx.stack; @@ -315,8 +234,6 @@ namespace MemoryOperations { // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_POP); return ctx; } @@ -337,13 +254,6 @@ namespace MemoryOperations { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - let stack_underflow = is_le(ctx.stack.size, 1); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = ctx.stack; // Stack input: @@ -368,8 +278,6 @@ namespace MemoryOperations { // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_MSTORE8); return ctx; } @@ -396,13 +304,6 @@ namespace MemoryOperations { return ctx; } - let stack_underflow = is_le(ctx.stack.size, 1); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let (stack, popped) = Stack.pop_n(self=ctx.stack, n=2); let key = popped; // Uint256* @@ -410,7 +311,6 @@ namespace MemoryOperations { let state = State.write_storage(ctx.state, ctx.call_context.address, key, value); let ctx = ExecutionContext.update_state(ctx, state); let ctx = ExecutionContext.update_stack(ctx, stack); - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_SSTORE); return ctx; } @@ -431,12 +331,6 @@ namespace MemoryOperations { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - if (ctx.stack.size == 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let (stack, key) = Stack.pop(ctx.stack); let (state, value) = State.read_storage(ctx.state, ctx.call_context.address, key); let stack = Stack.push(stack, value); @@ -460,25 +354,16 @@ namespace MemoryOperations { bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // Get stack from context. let stack: model.Stack* = ctx.stack; // Compute remaining gas. - let remaining_gas = ctx.call_context.gas_limit - ctx.gas_used - GAS_COST_GAS; + let remaining_gas = ctx.call_context.gas_limit - ctx.gas_used; let stack: model.Stack* = Stack.push_uint128(ctx.stack, remaining_gas); // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(ctx, GAS_COST_GAS); return ctx; } } diff --git a/src/kakarot/instructions/push_operations.cairo b/src/kakarot/instructions/push_operations.cairo index 6f1df369b..b76db2c9a 100644 --- a/src/kakarot/instructions/push_operations.cairo +++ b/src/kakarot/instructions/push_operations.cairo @@ -21,8 +21,6 @@ from kakarot.stack import Stack // @title Push operations opcodes. namespace PushOperations { - const BASE_GAS_COST = 2; - func exec_push{ syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, @@ -31,26 +29,9 @@ namespace PushOperations { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - if (ctx.stack.size == Constants.STACK_MAX_DEPTH) { - let (revert_reason_len, revert_reason) = Errors.stackOverflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // See evm.cairo, pc is increased before entering the opcode let opcode_number = [ctx.call_context.bytecode + ctx.program_counter - 1]; let i = opcode_number - 0x5f; - let is_not_push_0 = is_not_zero(i); - let gas = BASE_GAS_COST + is_not_push_0; - - let out_of_gas = is_le(ctx.call_context.gas_limit, ctx.gas_used + gas - 1); - if (out_of_gas != 0) { - let (revert_reason_len, revert_reason) = Errors.outOfGas(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - - let ctx = ExecutionContext.increment_gas_used(ctx, gas); // Copy code slice let pc = ctx.program_counter; diff --git a/src/kakarot/instructions/sha3.cairo b/src/kakarot/instructions/sha3.cairo index 6199051b4..3da176c28 100644 --- a/src/kakarot/instructions/sha3.cairo +++ b/src/kakarot/instructions/sha3.cairo @@ -23,8 +23,6 @@ from utils.utils import Helpers // @author @LucasLvy // @custom:namespace Sha3 namespace Sha3 { - const GAS_COST_SHA3 = 30; - // @notice SHA3. // @dev Hashes n memory elements at m memory offset. // @custom:since Frontier @@ -42,13 +40,6 @@ namespace Sha3 { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - let stack_underflow = is_le(ctx.stack.size, 1); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let stack = ctx.stack; // Stack input: @@ -90,9 +81,6 @@ namespace Sha3 { let ctx = ExecutionContext.update_stack(ctx, stack); let ctx = ExecutionContext.update_memory(ctx, memory); - // Increment gas used. - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=GAS_COST_SHA3); - return ctx; } } diff --git a/src/kakarot/instructions/stop_and_math_operations.cairo b/src/kakarot/instructions/stop_and_math_operations.cairo index 7872fcf22..620f22e5f 100644 --- a/src/kakarot/instructions/stop_and_math_operations.cairo +++ b/src/kakarot/instructions/stop_and_math_operations.cairo @@ -27,6 +27,7 @@ from starkware.cairo.common.bool import FALSE, TRUE from starkware.cairo.common.alloc import alloc from starkware.cairo.lang.compiler.lib.registers import get_fp_and_pc +from kakarot.constants import opcodes_label from kakarot.model import model from kakarot.execution_context import ExecutionContext from kakarot.stack import Stack @@ -74,19 +75,6 @@ namespace StopAndMathOperations { pc + (opcodes_label - pc_label) + opcode_number * model.Opcode.SIZE, model.Opcode* ); - let stack_underflow = is_le(ctx.stack.size, opcode.stack_input - 1); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let out_of_gas = is_le(ctx.call_context.gas_limit, ctx.gas_used + opcode.gas - 1); - if (out_of_gas != 0) { - let (revert_reason_len, revert_reason) = Errors.outOfGas(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let (local stack, popped) = Stack.pop_n(ctx.stack, opcode.stack_input); // offset is 1 (new line) + 2 (jmp + label) per opcode @@ -148,7 +136,6 @@ namespace StopAndMathOperations { // Finalize opcode let stack = Stack.push_uint256(stack, result); let ctx = ExecutionContext.update_stack(ctx, stack); - let ctx = ExecutionContext.increment_gas_used(ctx, opcode.gas); return ctx; ADD: @@ -471,95 +458,3 @@ namespace StopAndMathOperations { jmp end; } } - -// See model.Opcode -// gas -// stack_input -opcodes_label: -// STOP; -dw 0; -dw 0; -// ADD; -dw 3; -dw 2; -// MUL; -dw 5; -dw 2; -// SUB; -dw 3; -dw 2; -// DIV; -dw 5; -dw 2; -// SDIV; -dw 5; -dw 2; -// MOD; -dw 5; -dw 2; -// SMOD; -dw 5; -dw 2; -// ADDMOD; -dw 8; -dw 3; -// MULMOD; -dw 8; -dw 3; -// EXP; -dw 10; -dw 2; -// SIGNEXTEND; -dw 5; -dw 2; -// INVALID -dw 0; -dw 0; -dw 0; -dw 0; -dw 0; -dw 0; -dw 0; -dw 0; -// LT -dw 3; -dw 2; -// GT -dw 3; -dw 2; -// SLT -dw 3; -dw 2; -// SGT -dw 3; -dw 2; -// EQ -dw 3; -dw 2; -// ISZERO -dw 3; -dw 1; -// AND -dw 3; -dw 2; -// OR -dw 3; -dw 2; -// XOR -dw 3; -dw 2; -// NOT -dw 3; -dw 1; -// BYTE -dw 3; -dw 2; -// SHL -dw 3; -dw 2; -// SHR -dw 3; -dw 2; -// SAR -dw 3; -dw 2; diff --git a/src/kakarot/instructions/system_operations.cairo b/src/kakarot/instructions/system_operations.cairo index b0e42b26b..fa9738270 100644 --- a/src/kakarot/instructions/system_operations.cairo +++ b/src/kakarot/instructions/system_operations.cairo @@ -35,11 +35,6 @@ from utils.utils import Helpers // @title System operations opcodes. // @notice This file contains the functions to execute for system operations opcodes. namespace SystemOperations { - // Gas cost generated from using a CALL opcode (CALL, STATICCALL, etc.) with positive value parameter - const GAS_COST_POSITIVE_VALUE = 9000; - // Gas cost generated from accessing a "cold" address in the network: https://www.evm.codes/about#accesssets - const GAS_COST_COLD_ADDRESS_ACCESS = 2600; - const GAS_COST_CREATE = 32000; // @notice CREATE operation. // @custom:since Frontier // @custom:group System Operations @@ -61,13 +56,6 @@ namespace SystemOperations { return ctx; } - let stack_underflow = is_le(ctx.stack.size, 2); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // Stack input: // 0 - value: value in wei to send to the new account // 1 - offset: byte offset in the memory in bytes (initialization code) @@ -82,7 +70,6 @@ namespace SystemOperations { let word_size_gas = 6 * minimum_word_size; let ctx = ExecutionContext.update_stack(ctx, stack); - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=word_size_gas); let sub_ctx = CreateHelper.initialize_sub_context(ctx=ctx, popped_len=3, popped=popped); @@ -110,13 +97,6 @@ namespace SystemOperations { return ctx; } - let stack_underflow = is_le(ctx.stack.size, 3); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // Stack input: // 0 - value: value in wei to send to the new account // 1 - offset: byte offset in the memory in bytes (initialization code) @@ -170,13 +150,6 @@ namespace SystemOperations { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - let stack_underflow = is_le(ctx.stack.size, 1); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // Stack input: // 0 - offset: byte offset in the memory in bytes // 1 - size: byte size to copy @@ -189,7 +162,6 @@ namespace SystemOperations { let ctx = ExecutionContext.update_stack(ctx, stack); let ctx = ExecutionContext.update_memory(ctx, memory); - let ctx = ExecutionContext.increment_gas_used(ctx, gas_cost); let ctx = ExecutionContext.stop(ctx, size.low, return_data, FALSE); return ctx; @@ -211,13 +183,6 @@ namespace SystemOperations { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { alloc_locals; - let stack_underflow = is_le(ctx.stack.size, 1); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // Stack input: // 0 - offset: byte offset in the memory in bytes // 1 - size: byte size to copy @@ -231,7 +196,6 @@ namespace SystemOperations { let ctx = ExecutionContext.update_stack(ctx, stack); let ctx = ExecutionContext.update_memory(ctx, memory); - let ctx = ExecutionContext.increment_gas_used(self=ctx, inc_value=gas_cost); let ctx = ExecutionContext.stop(ctx, size.low, return_data, TRUE); return ctx; } @@ -371,12 +335,6 @@ namespace SystemOperations { return ctx; } - if (ctx.stack.size == 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - // Transfer funds let (stack, popped) = Stack.pop(ctx.stack); let (_, address_high) = unsigned_div_rem(popped.high, 2 ** 32); @@ -481,13 +439,6 @@ namespace CallHelper { ) -> model.ExecutionContext* { alloc_locals; - let stack_underflow = is_le(ctx.stack.size, with_value + 3); - if (stack_underflow != 0) { - let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); - let ctx = ExecutionContext.stop(ctx, revert_reason_len, revert_reason, TRUE); - return ctx; - } - let (ctx, local call_args) = CallHelper.prepare_args(ctx, with_value); // Check if the called address is a precompiled contract @@ -829,9 +780,6 @@ namespace CreateHelper { // Update calling context before creating sub context let ctx = ExecutionContext.update_memory(ctx, memory); - let ctx = ExecutionContext.increment_gas_used( - ctx, gas_cost + SystemOperations.GAS_COST_CREATE - ); let ctx = ExecutionContext.update_state(ctx, state); if (is_collision != 0) { diff --git a/src/kakarot/model.cairo b/src/kakarot/model.cairo index 50261ac80..e31a31ea6 100644 --- a/src/kakarot/model.cairo +++ b/src/kakarot/model.cairo @@ -143,10 +143,17 @@ namespace model { } // @dev Stores the constant data of an opcode + // @dev Stores the constant data of an opcode + // @param number The opcode number // @param gas The minimum gas used by the opcode (not including possible dynamic gas) - // @param stack_input The number of parameters popped from the stack + // @param stack_input The number of inputs popped from the stack. + // @param stack_size_min The minimal size of the Stack for this opcode. + // @param stack_size_diff The difference between the stack size after and before struct Opcode { + number: felt, gas: felt, stack_input: felt, + stack_size_min: felt, + stack_size_diff: felt, } } diff --git a/src/kakarot/precompiles/precompiles.cairo b/src/kakarot/precompiles/precompiles.cairo index 0e4363104..77e051c07 100644 --- a/src/kakarot/precompiles/precompiles.cairo +++ b/src/kakarot/precompiles/precompiles.cairo @@ -72,7 +72,6 @@ namespace Precompiles { let sub_ctx = ExecutionContext.init(call_context); let sub_ctx = ExecutionContext.update_state(sub_ctx, calling_context.state); let sub_ctx = ExecutionContext.stop(sub_ctx, output_len, output, FALSE); - let sub_ctx = ExecutionContext.increment_gas_used(sub_ctx, gas_used); return sub_ctx; } From 68485be8c94bf56e1ef7f569cfc45d285e02b509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Walter?= Date: Tue, 7 Nov 2023 14:32:25 +0100 Subject: [PATCH 3/4] Fix gas_used assert --- .../instructions/block_information.cairo | 36 +++++-------------- .../environmental_information.cairo | 21 +++++------ .../test_environmental_information.cairo | 19 ++-------- .../instructions/test_memory_operations.cairo | 14 +------- .../instructions/test_system_operations.cairo | 1 - 5 files changed, 21 insertions(+), 70 deletions(-) diff --git a/src/kakarot/instructions/block_information.cairo b/src/kakarot/instructions/block_information.cairo index ddaf34556..8d827d587 100644 --- a/src/kakarot/instructions/block_information.cairo +++ b/src/kakarot/instructions/block_information.cairo @@ -31,26 +31,9 @@ namespace BlockInformation { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - alloc_locals; - local range_check: felt; - local opcode: model.Opcode*; - // See evm.cairo, pc is increased before entering the opcode let opcode_number = [ctx.call_context.bytecode + ctx.program_counter - 1]; - // To cast the codeoffset opcodes_label to a model.Opcode*, we need to use it to offset - // the current pc. We get the pc from the `get_fp_and_pc` util and assign a codeoffset (pc_label) to it. - // In short, this boilds down to: opcode = pc + offset - pc = offset - // The following lines are equivalent but cheaper than `get_label_location`. - let (_, pc) = get_fp_and_pc(); - - pc_label: - assert opcode = cast( - pc + (opcodes_label - pc_label) + opcode_number * model.Opcode.SIZE, model.Opcode* - ); - - assert range_check = range_check_ptr; - tempvar offset = 2 * (opcode_number - 0x40) + 1; jmp rel offset; @@ -67,7 +50,7 @@ namespace BlockInformation { blockhash: let syscall_ptr = cast([fp - 7], felt*); let pedersen_ptr = cast([fp - 6], HashBuiltin*); - let range_check_ptr = [fp]; + let range_check_ptr = [fp - 5]; let ctx = cast([fp - 3], model.ExecutionContext*); let (ctx, result) = Internals.blockhash(ctx); jmp end; @@ -75,7 +58,7 @@ namespace BlockInformation { coinbase: tempvar syscall_ptr = cast([fp - 7], felt*); tempvar pedersen_ptr = cast([fp - 6], HashBuiltin*); - tempvar range_check_ptr = [fp]; + tempvar range_check_ptr = [fp - 5]; tempvar ctx = cast([fp - 3], model.ExecutionContext*); tempvar result = Uint256( 0xacdffe0cf08e20ed8ba10ea97a487004, 0x388ca486b82e20cc81965d056b4cdca @@ -87,7 +70,7 @@ namespace BlockInformation { let (block_timestamp) = get_block_timestamp(); tempvar syscall_ptr = cast([ap - 2], felt*); tempvar pedersen_ptr = cast([fp - 6], HashBuiltin*); - tempvar range_check_ptr = [fp]; + tempvar range_check_ptr = [fp - 5]; tempvar ctx = cast([fp - 3], model.ExecutionContext*); tempvar result = Uint256(block_timestamp, 0); jmp end; @@ -97,7 +80,7 @@ namespace BlockInformation { let (block_number) = get_block_number(); tempvar syscall_ptr = cast([ap - 2], felt*); tempvar pedersen_ptr = cast([fp - 6], HashBuiltin*); - tempvar range_check_ptr = [fp]; + tempvar range_check_ptr = [fp - 5]; tempvar ctx = cast([fp - 3], model.ExecutionContext*); tempvar result = Uint256(block_number, 0); jmp end; @@ -105,7 +88,7 @@ namespace BlockInformation { prevrandao: tempvar syscall_ptr = cast([fp - 7], felt*); tempvar pedersen_ptr = cast([fp - 6], HashBuiltin*); - tempvar range_check_ptr = [fp]; + tempvar range_check_ptr = [fp - 5]; tempvar ctx = cast([fp - 3], model.ExecutionContext*); tempvar result = Uint256(0, 0); jmp end; @@ -115,7 +98,7 @@ namespace BlockInformation { tempvar gas_limit = ctx.call_context.gas_limit; tempvar syscall_ptr = cast([fp - 7], felt*); tempvar pedersen_ptr = cast([fp - 6], HashBuiltin*); - tempvar range_check_ptr = [fp]; + tempvar range_check_ptr = [fp - 5]; tempvar ctx = cast([fp - 3], model.ExecutionContext*); tempvar result = Uint256(gas_limit, 0); jmp end; @@ -123,7 +106,7 @@ namespace BlockInformation { chainid: tempvar syscall_ptr = cast([fp - 7], felt*); tempvar pedersen_ptr = cast([fp - 6], HashBuiltin*); - tempvar range_check_ptr = [fp]; + tempvar range_check_ptr = [fp - 5]; tempvar ctx = cast([fp - 3], model.ExecutionContext*); tempvar result = Uint256(Constants.CHAIN_ID, 0); jmp end; @@ -131,7 +114,7 @@ namespace BlockInformation { selfbalance: let syscall_ptr = cast([fp - 7], felt*); let pedersen_ptr = cast([fp - 6], HashBuiltin*); - let range_check_ptr = [fp]; + let range_check_ptr = [fp - 5]; let ctx = cast([fp - 3], model.ExecutionContext*); let (ctx, result) = Internals.selfbalance(ctx); jmp end; @@ -139,14 +122,13 @@ namespace BlockInformation { basefee: tempvar syscall_ptr = cast([fp - 7], felt*); tempvar pedersen_ptr = cast([fp - 6], HashBuiltin*); - tempvar range_check_ptr = [fp]; + tempvar range_check_ptr = [fp - 5]; tempvar ctx = cast([fp - 3], model.ExecutionContext*); tempvar result = Uint256(0, 0); jmp end; end: // Rebind unused args with fp - let opcode = cast([fp + 1], model.Opcode*); let bitwise_ptr = cast([fp - 4], BitwiseBuiltin*); // Rebind used args with ap diff --git a/src/kakarot/instructions/environmental_information.cairo b/src/kakarot/instructions/environmental_information.cairo index eaf503e84..e9624c839 100644 --- a/src/kakarot/instructions/environmental_information.cairo +++ b/src/kakarot/instructions/environmental_information.cairo @@ -44,7 +44,7 @@ namespace EnvironmentalInformation { // Get the current execution contract from the context, // convert to Uint256, and push to Stack. let address = Helpers.to_uint256(ctx.call_context.address.evm); - let stack: model.Stack* = Stack.push(ctx.stack, address); + let stack = Stack.push(ctx.stack, address); // Update the execution context. let ctx = ExecutionContext.update_stack(ctx, stack); return ctx; @@ -97,7 +97,7 @@ namespace EnvironmentalInformation { }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { let origin_address = Helpers.to_uint256(ctx.call_context.origin.evm); - let stack: model.Stack* = Stack.push(self=ctx.stack, element=origin_address); + let stack = Stack.push(self=ctx.stack, element=origin_address); let ctx = ExecutionContext.update_stack(ctx, stack); return ctx; } @@ -125,10 +125,7 @@ namespace EnvironmentalInformation { tempvar caller = ctx.call_context.origin.evm; } let evm_address_uint256 = Helpers.to_uint256(caller); - let stack: model.Stack* = Stack.push(self=ctx.stack, element=evm_address_uint256); - - // Update the execution context. - // Update context stack. + let stack = Stack.push(ctx.stack, evm_address_uint256); let ctx = ExecutionContext.update_stack(ctx, stack); return ctx; } @@ -148,7 +145,7 @@ namespace EnvironmentalInformation { bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { let uint256_value = Helpers.to_uint256(ctx.call_context.value); - let stack: model.Stack* = Stack.push(ctx.stack, uint256_value); + let stack = Stack.push(ctx.stack, uint256_value); let ctx = ExecutionContext.update_stack(ctx, stack); return ctx; @@ -189,7 +186,7 @@ namespace EnvironmentalInformation { let uint256_sliced_calldata = Helpers.bytes32_to_uint256(sliced_calldata); // Push CallData word onto stack - let stack: model.Stack* = Stack.push_uint256(stack, uint256_sliced_calldata); + let stack = Stack.push_uint256(stack, uint256_sliced_calldata); // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); @@ -211,7 +208,7 @@ namespace EnvironmentalInformation { range_check_ptr, bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { - let stack: model.Stack* = Stack.push_uint128(ctx.stack, ctx.call_context.calldata_len); + let stack = Stack.push_uint128(ctx.stack, ctx.call_context.calldata_len); // Update the execution context. // Update context stack. @@ -288,7 +285,7 @@ namespace EnvironmentalInformation { // Get the bytecode size. let code_size = Helpers.to_uint256(ctx.call_context.bytecode_len); - let stack: model.Stack* = Stack.push_uint128(ctx.stack, ctx.call_context.bytecode_len); + let stack = Stack.push_uint128(ctx.stack, ctx.call_context.bytecode_len); // Update the execution context. // Update context stack. @@ -362,7 +359,7 @@ namespace EnvironmentalInformation { bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { // Get the gasprice. - let stack: model.Stack* = Stack.push_uint128(ctx.stack, ctx.call_context.gas_price); + let stack = Stack.push_uint128(ctx.stack, ctx.call_context.gas_price); // Update context stack. let ctx = ExecutionContext.update_stack(ctx, stack); @@ -483,7 +480,7 @@ namespace EnvironmentalInformation { bitwise_ptr: BitwiseBuiltin*, }(ctx: model.ExecutionContext*) -> model.ExecutionContext* { // Get return data size. - let stack: model.Stack* = Stack.push_uint128(ctx.stack, ctx.return_data_len); + let stack = Stack.push_uint128(ctx.stack, ctx.return_data_len); // Update the execution context. // Update context stack. diff --git a/tests/src/kakarot/instructions/test_environmental_information.cairo b/tests/src/kakarot/instructions/test_environmental_information.cairo index c952f07dc..90d392a79 100644 --- a/tests/src/kakarot/instructions/test_environmental_information.cairo +++ b/tests/src/kakarot/instructions/test_environmental_information.cairo @@ -107,8 +107,7 @@ func test__exec_address__should_push_address_to_stack{ // When let result = EnvironmentalInformation.exec_address(ctx); - // Then - assert result.gas_used = 2; + // The assert result.stack.size = 1; let (stack, index0) = Stack.peek(result.stack, 0); assert index0.low = 420; @@ -139,9 +138,6 @@ func test__exec_extcodesize__should_handle_address_with_no_code{ bytecode_len, bytecode, stack ); - // we are hardcoding an assumption of 'cold' address access, for now. - let expected_gas = 2600; - // When let ctx = EnvironmentalInformation.exec_extcodesize(ctx); @@ -150,8 +146,6 @@ func test__exec_extcodesize__should_handle_address_with_no_code{ assert extcodesize.low = 0; assert extcodesize.high = 0; - assert ctx.gas_used = expected_gas; - return (); } @@ -200,7 +194,6 @@ func test__exec_extcodecopy__should_handle_address_with_code{ // Then assert result.stack.size = 0; - assert result.gas_used = 3 * minimum_word_size + memory_expansion_cost + address_access_cost; let (output_array) = alloc(); Memory._load_n(result.memory, size, output_array, dest_offset); @@ -256,8 +249,6 @@ func test__exec_extcodecopy__should_handle_address_with_no_code{ // ensure stack is consumed/updated assert result.stack.size = 0; - assert result.gas_used = expected_gas; - assert [output_array] = 0; assert [output_array + 1] = 0; assert [output_array + 2] = 0; @@ -284,8 +275,7 @@ func test__exec_gasprice{ let result = EnvironmentalInformation.exec_gasprice(ctx); let (stack, gasprice) = Stack.peek(result.stack, 0); - // Then - assert result.gas_used = 2; + // The assert_uint256_eq([gasprice], [expected_gas_price_uint256]); return (); @@ -330,7 +320,6 @@ func test__returndatacopy{ assert_uint256_eq( data, Uint256(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) ); - assert ctx.gas_used = 3; // Pushing parameters for another RETURNDATACOPY tempvar item_2 = new Uint256(1, 0); @@ -386,8 +375,6 @@ func test__exec_extcodehash__should_handle_invalid_address{ let (stack, extcodehash) = Stack.peek(result.stack, 0); assert extcodehash.low = 0; assert extcodehash.high = 0; - // 'cold' address access - assert result.gas_used = 2600; return (); } @@ -420,8 +407,6 @@ func test__exec_extcodehash__should_handle_address_with_code{ let (stack, extcodehash) = Stack.peek(result.stack, 0); assert extcodehash.low = expected_hash_low; assert extcodehash.high = expected_hash_high; - // 'cold' address access - assert result.gas_used = 2600; return (); } diff --git a/tests/src/kakarot/instructions/test_memory_operations.cairo b/tests/src/kakarot/instructions/test_memory_operations.cairo index 77e61581b..cf50159a8 100644 --- a/tests/src/kakarot/instructions/test_memory_operations.cairo +++ b/tests/src/kakarot/instructions/test_memory_operations.cairo @@ -31,7 +31,6 @@ func test__exec_pc__should_update_after_incrementing{ let result = MemoryOperations.exec_pc(ctx); // Then - assert result.gas_used = 2; assert result.stack.size = 1; let (stack, index0) = Stack.peek(result.stack, 0); assert index0.low = increment - 1; @@ -61,7 +60,6 @@ func test__exec_pop_should_pop_an_item_from_execution_context{ let result = MemoryOperations.exec_pop(ctx); // Then - assert result.gas_used = 2; assert result.stack.size = 1; let (stack, index0) = Stack.peek(result.stack, 0); assert_uint256_eq([index0], Uint256(1, 0)); @@ -93,12 +91,9 @@ func test__exec_mload_should_load_a_value_from_memory{ let ctx = ExecutionContext.update_stack(ctx, stack); // When - local gas_used_before = ctx.gas_used; let result = MemoryOperations.exec_mload(ctx); - local gas_used = result.gas_used - gas_used_before; // Then - assert gas_used = 3; assert result.stack.size = 1; let (stack, index0) = Stack.peek(result.stack, 0); assert_uint256_eq([index0], Uint256(1, 0)); @@ -131,12 +126,9 @@ func test__exec_mload_should_load_a_value_from_memory_with_memory_expansion{ let ctx = ExecutionContext.update_stack(ctx, stack); // When - local gas_used_before = ctx.gas_used; let result = MemoryOperations.exec_mload(ctx); - local gas_used = result.gas_used - gas_used_before; // Then - assert gas_used = 6; assert result.stack.size = 1; let (stack, index0) = Stack.peek(result.stack, 0); assert_uint256_eq([index0], Uint256(0, 1)); @@ -172,7 +164,6 @@ func test__exec_mload_should_load_a_value_from_memory_with_offset_larger_than_ms let result = MemoryOperations.exec_mload(ctx); // Then - assert result.gas_used = 73; assert result.stack.size = 1; let (stack, index0) = Stack.peek(result.stack, 0); assert_uint256_eq([index0], Uint256(0, 0)); @@ -194,15 +185,12 @@ func test__exec_gas_should_return_remaining_gas{ let ctx = ExecutionContext.update_stack(ctx, stack); // When - local gas_used_before = ctx.gas_used; let result = MemoryOperations.exec_gas(ctx); - local gas_used = result.gas_used - gas_used_before; // Then - assert gas_used = 2; assert result.stack.size = 1; let (stack, actual_remaining_gas) = Stack.peek(result.stack, 0); - let expected_remaining_gas = Constants.TRANSACTION_GAS_LIMIT - gas_used; + let expected_remaining_gas = Constants.TRANSACTION_GAS_LIMIT - result.gas_used; let expected_remaining_gas_uint256 = Uint256(expected_remaining_gas, 0); assert_uint256_eq([actual_remaining_gas], expected_remaining_gas_uint256); return (); diff --git a/tests/src/kakarot/instructions/test_system_operations.cairo b/tests/src/kakarot/instructions/test_system_operations.cairo index 019f519b4..c3f481f9c 100644 --- a/tests/src/kakarot/instructions/test_system_operations.cairo +++ b/tests/src/kakarot/instructions/test_system_operations.cairo @@ -17,7 +17,6 @@ from openzeppelin.token.erc20.library import ERC20 // Local dependencies from kakarot.constants import Constants from kakarot.storages import ( - Constants, native_token_address, contract_account_class_hash, account_proxy_class_hash, From 0fe913ba812fb748c4875a92ba38ecf43894792a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Walter?= Date: Tue, 7 Nov 2023 15:07:02 +0100 Subject: [PATCH 4/4] Fix static gas value for LOG ops --- src/kakarot/constants.cairo | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/kakarot/constants.cairo b/src/kakarot/constants.cairo index 7f1fada04..707607827 100644 --- a/src/kakarot/constants.cairo +++ b/src/kakarot/constants.cairo @@ -1017,25 +1017,25 @@ dw 2; dw -2; // LOG1 dw 0xa1; -dw 750; +dw 375; dw 3; dw 3; dw -3; // LOG2 dw 0xa2; -dw 1125; +dw 375; dw 4; dw 4; dw -4; // LOG3 dw 0xa3; -dw 1500; +dw 375; dw 5; dw 5; dw -5; // LOG4 dw 0xa4; -dw 1875; +dw 375; dw 6; dw 6; dw -6;