From 7bc70ad9965ecb69d5242957594339975a0b3a8c Mon Sep 17 00:00:00 2001 From: Oba Date: Mon, 22 Jul 2024 10:30:42 +0200 Subject: [PATCH] opti: replace is_le by is_nn (#1287) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Time spent on this PR: ## Pull request type Please check the type of change your PR introduces: - [ ] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no api changes) - [ ] Build related changes - [ ] Documentation content changes - [x] Other (please describe): opti Resolves #1262 - - - This change is [Reviewable](https://reviewable.io/reviews/kkrt-labs/kakarot/1287) --- src/backend/starknet.cairo | 4 ++-- src/kakarot/account.cairo | 2 +- src/kakarot/accounts/account_contract.cairo | 6 +++--- src/kakarot/accounts/library.cairo | 16 ++++++++-------- src/kakarot/evm.cairo | 4 ++-- src/kakarot/gas.cairo | 6 +++--- .../environmental_information.cairo | 4 ++-- .../instructions/memory_operations.cairo | 2 +- src/kakarot/instructions/push_operations.cairo | 4 ++-- .../instructions/system_operations.cairo | 10 +++++----- src/kakarot/interpreter.cairo | 18 +++++++++--------- src/kakarot/memory.cairo | 1 - src/kakarot/precompiles/blake2f.cairo | 2 +- .../precompiles/kakarot_precompiles.cairo | 8 ++++---- src/kakarot/precompiles/precompiles.cairo | 4 ++-- .../precompiles/precompiles_helpers.cairo | 4 ++-- src/kakarot/precompiles/ripemd160.cairo | 4 ++-- src/utils/eth_transaction.cairo | 6 +++--- src/utils/rlp.cairo | 12 ++++++------ src/utils/uint256.cairo | 6 +++--- src/utils/utils.cairo | 4 ++-- 21 files changed, 63 insertions(+), 64 deletions(-) diff --git a/src/backend/starknet.cairo b/src/backend/starknet.cairo index c54cd47bd..1a4fdd001 100644 --- a/src/backend/starknet.cairo +++ b/src/backend/starknet.cairo @@ -8,7 +8,7 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.dict_access import DictAccess from starkware.cairo.common.uint256 import Uint256 from starkware.cairo.common.math import unsigned_div_rem -from starkware.cairo.common.math_cmp import is_le +from starkware.cairo.common.math_cmp import is_nn from starkware.cairo.common.memset import memset from starkware.starknet.common.syscalls import ( emit_event, @@ -245,7 +245,7 @@ namespace Internals { // See 300 max data_len for events // https://github.com/starkware-libs/blockifier/blob/9bfb3d4c8bf1b68a0c744d1249b32747c75a4d87/crates/blockifier/resources/versioned_constants.json // The whole data_len should be less than 300 - tempvar data_len = is_le(event.data_len, 300) * (event.data_len - 300) + 300; + tempvar data_len = is_nn(300 - event.data_len) * (event.data_len - 300) + 300; emit_event( keys_len=event.topics_len, keys=event.topics, data_len=data_len, data=event.data diff --git a/src/kakarot/account.cairo b/src/kakarot/account.cairo index 4d0461ca2..2c89618ea 100644 --- a/src/kakarot/account.cairo +++ b/src/kakarot/account.cairo @@ -9,7 +9,7 @@ from starkware.cairo.common.default_dict import default_dict_new from starkware.cairo.common.dict import dict_read, dict_write from starkware.cairo.common.dict_access import DictAccess from starkware.cairo.common.hash import hash2 -from starkware.cairo.common.math_cmp import is_not_zero, is_le +from starkware.cairo.common.math_cmp import is_not_zero from starkware.cairo.common.memcpy import memcpy from starkware.cairo.common.uint256 import Uint256 from starkware.cairo.common.hash_state import ( diff --git a/src/kakarot/accounts/account_contract.cairo b/src/kakarot/accounts/account_contract.cairo index aabc9ed22..0319fe5f8 100644 --- a/src/kakarot/accounts/account_contract.cairo +++ b/src/kakarot/accounts/account_contract.cairo @@ -6,7 +6,7 @@ from openzeppelin.access.ownable.library import Ownable, Ownable_owner from starkware.cairo.common.alloc import alloc from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin, SignatureBuiltin from starkware.cairo.common.math import assert_le, unsigned_div_rem -from starkware.cairo.common.math_cmp import is_le +from starkware.cairo.common.math_cmp import is_nn from starkware.cairo.common.uint256 import Uint256 from starkware.starknet.common.syscalls import ( get_tx_info, @@ -116,11 +116,11 @@ func execute_from_outside{ assert caller = outside_execution.caller; } let (block_timestamp) = get_block_timestamp(); - let too_early = is_le(block_timestamp, outside_execution.execute_after); + let too_early = is_nn(outside_execution.execute_after - block_timestamp); with_attr error_message("Execute from outside: too early") { assert too_early = FALSE; } - let too_late = is_le(outside_execution.execute_before, block_timestamp); + let too_late = is_nn(block_timestamp - outside_execution.execute_before); with_attr error_message("Execute from outside: too late") { assert too_late = FALSE; } diff --git a/src/kakarot/accounts/library.cairo b/src/kakarot/accounts/library.cairo index b398d00ef..ae6661ecf 100644 --- a/src/kakarot/accounts/library.cairo +++ b/src/kakarot/accounts/library.cairo @@ -8,7 +8,7 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin from starkware.cairo.common.math import unsigned_div_rem, split_int, split_felt from starkware.cairo.common.memcpy import memcpy from starkware.cairo.common.uint256 import Uint256, uint256_not, uint256_le -from starkware.cairo.common.math_cmp import is_le +from starkware.cairo.common.math_cmp import is_nn from starkware.starknet.common.syscalls import ( StorageRead, StorageWrite, @@ -179,7 +179,7 @@ namespace AccountContract { local y_parity: felt; local pre_eip155_tx: felt; if (tx_type == 0) { - let is_eip155_tx = is_le(v, 28); + let is_eip155_tx = is_nn(28 - v); assert pre_eip155_tx = is_eip155_tx; if (is_eip155_tx != FALSE) { assert y_parity = v - 27; @@ -262,25 +262,25 @@ namespace AccountContract { } let (block_gas_limit) = IKakarot.get_block_gas_limit(kakarot_address); - let tx_gas_fits_in_block = is_le(tx.gas_limit, block_gas_limit); + let tx_gas_fits_in_block = is_nn(block_gas_limit - tx.gas_limit); with_attr error_message("Transaction gas_limit > Block gas_limit") { assert tx_gas_fits_in_block = TRUE; } let (block_base_fee) = IKakarot.get_base_fee(kakarot_address); - let enough_fee = is_le(block_base_fee, tx.max_fee_per_gas); + let enough_fee = is_nn(tx.max_fee_per_gas - block_base_fee); with_attr error_message("Max fee per gas too low") { assert enough_fee = TRUE; } - let max_fee_greater_priority_fee = is_le(tx.max_priority_fee_per_gas, tx.max_fee_per_gas); + let max_fee_greater_priority_fee = is_nn(tx.max_fee_per_gas - tx.max_priority_fee_per_gas); with_attr error_message("Max priority fee greater than max fee per gas") { assert max_fee_greater_priority_fee = TRUE; } let possible_priority_fee = tx.max_fee_per_gas - block_base_fee; - let priority_fee_is_max_priority_fee = is_le( - tx.max_priority_fee_per_gas, possible_priority_fee + let priority_fee_is_max_priority_fee = is_nn( + possible_priority_fee - tx.max_priority_fee_per_gas ); let priority_fee_per_gas = priority_fee_is_max_priority_fee * tx.max_priority_fee_per_gas + (1 - priority_fee_is_max_priority_fee) * possible_priority_fee; @@ -304,7 +304,7 @@ namespace AccountContract { // See 300 max data_len for events // https://github.com/starkware-libs/blockifier/blob/9bfb3d4c8bf1b68a0c744d1249b32747c75a4d87/crates/blockifier/resources/versioned_constants.json // The whole data_len should be less than 300, so it's the return_data should be less than 297 (+3 for return_data_len, success, gas_used) - tempvar return_data_len = is_le(return_data_len, 297) * (return_data_len - 297) + 297; + tempvar return_data_len = is_nn(297 - return_data_len) * (return_data_len - 297) + 297; transaction_executed.emit( response_len=return_data_len, response=return_data, success=success, gas_used=gas_used ); diff --git a/src/kakarot/evm.cairo b/src/kakarot/evm.cairo index 299d92fdc..4398a0e75 100644 --- a/src/kakarot/evm.cairo +++ b/src/kakarot/evm.cairo @@ -5,7 +5,7 @@ from starkware.cairo.common.alloc import alloc from starkware.cairo.common.bool import TRUE, FALSE from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin -from starkware.cairo.common.math_cmp import is_le, is_le_felt +from starkware.cairo.common.math_cmp import is_nn, is_le_felt from starkware.cairo.common.memcpy import memcpy from starkware.cairo.common.registers import get_label_location from starkware.cairo.common.uint256 import Uint256 @@ -225,7 +225,7 @@ namespace EVM { func jump{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, state: model.State*}( self: model.EVM*, new_pc_offset: felt ) -> model.EVM* { - let out_of_range = is_le(self.message.bytecode_len, new_pc_offset); + let out_of_range = is_nn(new_pc_offset - self.message.bytecode_len); if (out_of_range != FALSE) { let (revert_reason_len, revert_reason) = Errors.invalidJumpDestError(); let evm = EVM.stop(self, revert_reason_len, revert_reason, Errors.EXCEPTIONAL_HALT); diff --git a/src/kakarot/gas.cairo b/src/kakarot/gas.cairo index 2495aaece..11d7fb0f7 100644 --- a/src/kakarot/gas.cairo +++ b/src/kakarot/gas.cairo @@ -1,5 +1,5 @@ from starkware.cairo.common.math import split_felt, unsigned_div_rem -from starkware.cairo.common.math_cmp import is_le, is_not_zero, is_nn +from starkware.cairo.common.math_cmp import is_not_zero, is_nn from starkware.cairo.common.bool import FALSE from starkware.cairo.common.uint256 import Uint256, uint256_lt, uint256_eq @@ -80,7 +80,7 @@ namespace Gas { words_len: felt, max_offset: felt ) -> model.MemoryExpansion { alloc_locals; - let memory_expansion = is_le(words_len * 32 - 1, max_offset); + let memory_expansion = is_nn(max_offset - (words_len * 32 - 1)); if (memory_expansion == FALSE) { let expansion = model.MemoryExpansion(cost=0, new_words_len=words_len); return expansion; @@ -144,7 +144,7 @@ namespace Gas { let expansion = model.MemoryExpansion(cost=0, new_words_len=words_len); return expansion; } - let max_expansion_is_2 = is_le(offset_1.low + size_1.low, offset_2.low + size_2.low); + let max_expansion_is_2 = is_nn(offset_2.low + size_2.low - (offset_1.low + size_1.low)); let max_expansion = max_expansion_is_2 * (offset_2.low + size_2.low) + ( 1 - max_expansion_is_2 ) * (offset_1.low + size_1.low); diff --git a/src/kakarot/instructions/environmental_information.cairo b/src/kakarot/instructions/environmental_information.cairo index fa28ac948..16cb15bea 100644 --- a/src/kakarot/instructions/environmental_information.cairo +++ b/src/kakarot/instructions/environmental_information.cairo @@ -7,7 +7,7 @@ from starkware.cairo.common.bool import FALSE from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin from starkware.cairo.common.memset import memset from starkware.cairo.common.math import unsigned_div_rem, split_felt -from starkware.cairo.common.math_cmp import is_not_zero, is_le +from starkware.cairo.common.math_cmp import is_not_zero, is_nn from starkware.cairo.common.uint256 import Uint256, uint256_le, uint256_eq from kakarot.account import Account @@ -213,7 +213,7 @@ namespace EnvironmentalInformation { } let (sliced_data: felt*) = alloc(); - tempvar is_in_bounds = is_le(returndata_offset.low + size.low, evm.return_data_len); + tempvar is_in_bounds = is_nn(evm.return_data_len - (returndata_offset.low + size.low)); if (is_in_bounds == FALSE) { let (revert_reason_len, revert_reason) = Errors.outOfBoundsRead(); let evm = EVM.stop(evm, revert_reason_len, revert_reason, Errors.EXCEPTIONAL_HALT); diff --git a/src/kakarot/instructions/memory_operations.cairo b/src/kakarot/instructions/memory_operations.cairo index 79bcc8478..2092d0051 100644 --- a/src/kakarot/instructions/memory_operations.cairo +++ b/src/kakarot/instructions/memory_operations.cairo @@ -6,7 +6,7 @@ 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.uint256 import Uint256, uint256_eq -from starkware.cairo.common.math_cmp import is_le, is_le_felt, is_not_zero +from starkware.cairo.common.math_cmp import is_le_felt, is_not_zero from starkware.cairo.common.math import unsigned_div_rem from kakarot.errors import Errors diff --git a/src/kakarot/instructions/push_operations.cairo b/src/kakarot/instructions/push_operations.cairo index 9741c2ef1..83a8165ce 100644 --- a/src/kakarot/instructions/push_operations.cairo +++ b/src/kakarot/instructions/push_operations.cairo @@ -3,7 +3,7 @@ %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin -from starkware.cairo.common.math_cmp import is_le +from starkware.cairo.common.math_cmp import is_nn from kakarot.constants import Constants from kakarot.errors import Errors @@ -31,7 +31,7 @@ namespace PushOperations { // Copy code slice let pc = evm.program_counter + 1; - let out_of_bounds = is_le(evm.message.bytecode_len, pc + i); + let out_of_bounds = is_nn(pc + i - evm.message.bytecode_len); local len = (1 - out_of_bounds) * i + out_of_bounds * (evm.message.bytecode_len - pc); let stack_element = Helpers.bytes_to_uint256(len, evm.message.bytecode + pc); diff --git a/src/kakarot/instructions/system_operations.cairo b/src/kakarot/instructions/system_operations.cairo index 1ad49a333..e25e05445 100644 --- a/src/kakarot/instructions/system_operations.cairo +++ b/src/kakarot/instructions/system_operations.cairo @@ -6,7 +6,7 @@ from starkware.cairo.common.alloc import alloc from starkware.cairo.common.bool import TRUE, FALSE from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin from starkware.cairo.common.math import split_felt, unsigned_div_rem -from starkware.cairo.common.math_cmp import is_le, is_nn, is_not_zero +from starkware.cairo.common.math_cmp import is_nn, is_not_zero from starkware.cairo.common.registers import get_fp_and_pc from starkware.cairo.common.uint256 import Uint256, uint256_lt, uint256_le, uint256_eq from starkware.cairo.common.default_dict import default_dict_new @@ -138,7 +138,7 @@ namespace SystemOperations { } // Check code size - let code_size_too_big = is_le(2 * Constants.MAX_CODE_SIZE + 1, size.low); + let code_size_too_big = is_nn(size.low - (2 * Constants.MAX_CODE_SIZE + 1)); if (code_size_too_big != FALSE) { let evm = EVM.charge_gas(evm, evm.gas_left + 1); return evm; @@ -962,7 +962,7 @@ namespace CallHelper { return evm; } - let actual_output_size_is_ret_size = is_le(ret_size.low, evm.return_data_len); + let actual_output_size_is_ret_size = is_nn(evm.return_data_len - ret_size.low); let actual_output_size = actual_output_size_is_ret_size * ret_size.low + ( 1 - actual_output_size_is_ret_size ) * evm.return_data_len; @@ -1008,7 +1008,7 @@ namespace CreateHelper { let (message: felt*) = alloc(); assert [message + 1] = 0x80 + 20; felt_to_bytes20(message + 2, sender_address); - let encode_nonce = is_le(0x80, nonce); + let encode_nonce = is_nn(nonce - 0x80); if (encode_nonce != FALSE) { let nonce_len = felt_to_bytes(message + 2 + 20 + 1, nonce); assert [message + 2 + 20] = 0x80 + nonce_len; @@ -1171,7 +1171,7 @@ namespace CreateHelper { } // Charge final deposit gas - let code_size_limit = is_le(evm.return_data_len, Constants.MAX_CODE_SIZE); + let code_size_limit = is_nn(Constants.MAX_CODE_SIZE - evm.return_data_len); let code_deposit_cost = Gas.CODE_DEPOSIT * evm.return_data_len; let remaining_gas = evm.gas_left - code_deposit_cost; let enough_gas = is_nn(remaining_gas); diff --git a/src/kakarot/interpreter.cairo b/src/kakarot/interpreter.cairo index 638648ebd..197788abb 100644 --- a/src/kakarot/interpreter.cairo +++ b/src/kakarot/interpreter.cairo @@ -6,7 +6,7 @@ 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.math_cmp import is_le, is_not_zero, is_nn, is_le_felt +from starkware.cairo.common.math_cmp import is_not_zero, is_nn, is_le_felt from starkware.cairo.common.math import split_felt from starkware.cairo.common.default_dict import default_dict_new from starkware.cairo.common.dict import DictAccess @@ -62,7 +62,7 @@ namespace Interpreter { local opcode: model.Opcode*; let pc = evm.program_counter; - let is_pc_ge_code_len = is_le(evm.message.bytecode_len, pc); + let is_pc_ge_code_len = is_nn(pc - evm.message.bytecode_len); if (is_pc_ge_code_len != FALSE) { let is_precompile = PrecompilesHelpers.is_precompile(evm.message.code_address.evm); if (is_precompile != FALSE) { @@ -110,14 +110,14 @@ namespace Interpreter { ); // Check stack over/under flow - let stack_underflow = is_le(stack.size, opcode.stack_size_min - 1); + let stack_underflow = is_nn(opcode.stack_size_min - 1 - stack.size); if (stack_underflow != 0) { let (revert_reason_len, revert_reason) = Errors.stackUnderflow(); let evm = EVM.stop(evm, revert_reason_len, revert_reason, Errors.EXCEPTIONAL_HALT); return evm; } - let stack_overflow = is_le( - Constants.STACK_MAX_DEPTH + 1, stack.size + opcode.stack_size_diff + let stack_overflow = is_nn( + stack.size + opcode.stack_size_diff - (Constants.STACK_MAX_DEPTH + 1) ); if (stack_overflow != 0) { let (revert_reason_len, revert_reason) = Errors.stackOverflow(); @@ -886,8 +886,8 @@ namespace Interpreter { return (evm, stack, memory, state, 0, 0); } - tempvar is_initcode_invalid = is_deploy_tx * is_le( - 2 * Constants.MAX_CODE_SIZE + 1, bytecode_len + tempvar is_initcode_invalid = is_deploy_tx * is_nn( + bytecode_len - (2 * Constants.MAX_CODE_SIZE + 1) ); if (is_initcode_invalid != FALSE) { let evm = EVM.halt_validation_failed(evm); @@ -943,7 +943,7 @@ namespace Interpreter { let required_gas = gas_limit - evm.gas_left; let (max_refund, _) = unsigned_div_rem(required_gas, 5); - let is_max_refund_le_gas_refund = is_le(max_refund, evm.gas_refund); + let is_max_refund_le_gas_refund = is_nn(evm.gas_refund - max_refund); tempvar gas_refund = is_max_refund_le_gas_refund * max_refund + ( 1 - is_max_refund_le_gas_refund ) * evm.gas_refund; @@ -1014,7 +1014,7 @@ namespace Internals { } // Charge final deposit gas - let code_size_limit = is_le(evm.return_data_len, Constants.MAX_CODE_SIZE); + let code_size_limit = is_nn(Constants.MAX_CODE_SIZE - evm.return_data_len); let code_deposit_cost = Gas.CODE_DEPOSIT * evm.return_data_len; let enough_gas = is_nn(evm.gas_left - code_deposit_cost); // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3540.md diff --git a/src/kakarot/memory.cairo b/src/kakarot/memory.cairo index 80656fdc9..1f7f8c8bb 100644 --- a/src/kakarot/memory.cairo +++ b/src/kakarot/memory.cairo @@ -4,7 +4,6 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin from starkware.cairo.common.default_dict import default_dict_new, default_dict_finalize from starkware.cairo.common.dict import DictAccess, dict_read, dict_write from starkware.cairo.common.math import unsigned_div_rem -from starkware.cairo.common.math_cmp import is_le from starkware.cairo.common.uint256 import Uint256 from kakarot.model import model diff --git a/src/kakarot/precompiles/blake2f.cairo b/src/kakarot/precompiles/blake2f.cairo index 1f72fd688..105da3d30 100644 --- a/src/kakarot/precompiles/blake2f.cairo +++ b/src/kakarot/precompiles/blake2f.cairo @@ -8,7 +8,7 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.cairo_builtins import BitwiseBuiltin from starkware.cairo.common.math import unsigned_div_rem from starkware.cairo.common.registers import get_fp_and_pc, get_label_location -from starkware.cairo.common.math_cmp import is_nn, is_le +from starkware.cairo.common.math_cmp import is_nn from starkware.cairo.common.bool import FALSE // Internal dependencies diff --git a/src/kakarot/precompiles/kakarot_precompiles.cairo b/src/kakarot/precompiles/kakarot_precompiles.cairo index 03c6f49cb..99cfbe836 100644 --- a/src/kakarot/precompiles/kakarot_precompiles.cairo +++ b/src/kakarot/precompiles/kakarot_precompiles.cairo @@ -1,7 +1,7 @@ %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin -from starkware.cairo.common.math_cmp import is_le +from starkware.cairo.common.math_cmp import is_nn from starkware.cairo.common.math import assert_not_zero from starkware.cairo.common.alloc import alloc from starkware.starknet.common.syscalls import call_contract, library_call, get_caller_address @@ -45,7 +45,7 @@ namespace KakarotPrecompiles { alloc_locals; // Input must be at least 4 + 3*32 bytes long. - let is_input_invalid = is_le(input_len, 99); + let is_input_invalid = is_nn(99 - input_len); if (is_input_invalid != 0) { let (revert_reason_len, revert_reason) = Errors.outOfBoundsRead(); return (revert_reason_len, revert_reason, CAIRO_PRECOMPILE_GAS, TRUE); @@ -138,7 +138,7 @@ namespace KakarotPrecompiles { alloc_locals; // Input must be at least 3*32 bytes long. - let is_input_invalid = is_le(input_len, 95); + let is_input_invalid = is_nn(95 - input_len); if (is_input_invalid != 0) { let (revert_reason_len, revert_reason) = Errors.outOfBoundsRead(); return (revert_reason_len, revert_reason, CAIRO_MESSAGE_GAS, TRUE); @@ -151,7 +151,7 @@ namespace KakarotPrecompiles { let target_address = Helpers.bytes32_to_felt(input); let data_bytes_len = Helpers.bytes32_to_felt(input + 2 * 32); - let data_fits_in_input = is_le(data_bytes_len, input_len - 3 * 32); + let data_fits_in_input = is_nn(input_len - 3 * 32 - data_bytes_len); if (data_fits_in_input == 0) { let (revert_reason_len, revert_reason) = Errors.outOfBoundsRead(); return (revert_reason_len, revert_reason, CAIRO_MESSAGE_GAS, TRUE); diff --git a/src/kakarot/precompiles/precompiles.cairo b/src/kakarot/precompiles/precompiles.cairo index 868f08b57..60cb1d9d3 100644 --- a/src/kakarot/precompiles/precompiles.cairo +++ b/src/kakarot/precompiles/precompiles.cairo @@ -1,7 +1,7 @@ %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin -from starkware.cairo.common.math_cmp import is_le, is_not_zero, is_in_range +from starkware.cairo.common.math_cmp import is_nn, is_not_zero, is_in_range from starkware.starknet.common.syscalls import library_call from starkware.cairo.common.alloc import alloc from starkware.cairo.common.bool import FALSE @@ -50,7 +50,7 @@ namespace Precompiles { caller_code_address: felt, caller_address: felt, ) -> (output_len: felt, output: felt*, gas_used: felt, reverted: felt) { - let is_eth_precompile = is_le(precompile_address, LAST_ETHEREUM_PRECOMPILE_ADDRESS); + let is_eth_precompile = is_nn(LAST_ETHEREUM_PRECOMPILE_ADDRESS - precompile_address); tempvar syscall_ptr = syscall_ptr; tempvar pedersen_ptr = pedersen_ptr; tempvar range_check_ptr = range_check_ptr; diff --git a/src/kakarot/precompiles/precompiles_helpers.cairo b/src/kakarot/precompiles/precompiles_helpers.cairo index c653996ed..10b5c02de 100644 --- a/src/kakarot/precompiles/precompiles_helpers.cairo +++ b/src/kakarot/precompiles/precompiles_helpers.cairo @@ -1,4 +1,4 @@ -from starkware.cairo.common.math_cmp import is_le, is_not_zero, is_in_range +from starkware.cairo.common.math_cmp import is_nn, is_not_zero, is_in_range const LAST_ETHEREUM_PRECOMPILE_ADDRESS = 0x0a; const FIRST_ROLLUP_PRECOMPILE_ADDRESS = 0x100; @@ -28,7 +28,7 @@ namespace PrecompilesHelpers { let is_rollup_precompile_ = is_rollup_precompile(address); let is_kakarot_precompile_ = is_kakarot_precompile(address); return is_not_zero(address) * ( - is_le(address, LAST_ETHEREUM_PRECOMPILE_ADDRESS) + + is_nn(LAST_ETHEREUM_PRECOMPILE_ADDRESS - address) + is_rollup_precompile_ + is_kakarot_precompile_ ); diff --git a/src/kakarot/precompiles/ripemd160.cairo b/src/kakarot/precompiles/ripemd160.cairo index 3ca4816d3..ac53bc29e 100644 --- a/src/kakarot/precompiles/ripemd160.cairo +++ b/src/kakarot/precompiles/ripemd160.cairo @@ -7,7 +7,7 @@ from starkware.cairo.common.cairo_builtins import BitwiseBuiltin, HashBuiltin from starkware.cairo.common.alloc import alloc from starkware.cairo.common.math import unsigned_div_rem, assert_nn_le -from starkware.cairo.common.math_cmp import is_nn_le, is_le +from starkware.cairo.common.math_cmp import is_nn_le, is_nn from starkware.cairo.common.bitwise import bitwise_and, bitwise_xor, bitwise_or from starkware.cairo.common.dict_access import DictAccess from starkware.cairo.common.default_dict import default_dict_new, default_dict_finalize @@ -128,7 +128,7 @@ func compress_data{dict_ptr: DictAccess*, range_check_ptr, bitwise_ptr: BitwiseB buf: felt*, bufsize: felt, input_len: felt, input: felt* ) -> (res: felt*, rsize: felt, new_msg: felt*) { alloc_locals; - let len_lt_63 = is_le(input_len, 63); + let len_lt_63 = is_nn(63 - input_len); if (len_lt_63 == FALSE) { parse_msg{dict_ptr=dict_ptr}(input, 0); let (local arr_x: felt*) = alloc(); diff --git a/src/utils/eth_transaction.cairo b/src/utils/eth_transaction.cairo index a957c4efb..2f29dbda5 100644 --- a/src/utils/eth_transaction.cairo +++ b/src/utils/eth_transaction.cairo @@ -3,7 +3,7 @@ from starkware.cairo.common.alloc import alloc from starkware.cairo.common.bool import TRUE, FALSE from starkware.cairo.common.cairo_builtins import BitwiseBuiltin, HashBuiltin -from starkware.cairo.common.math_cmp import is_not_zero, is_le +from starkware.cairo.common.math_cmp import is_not_zero, is_nn from starkware.cairo.common.memcpy import memcpy from starkware.cairo.common.uint256 import Uint256 @@ -173,7 +173,7 @@ namespace EthTransaction { // @param tx_data The raw transaction data func get_tx_type{range_check_ptr}(tx_data: felt*) -> felt { let type = [tx_data]; - let is_legacy = is_le(0xc0, type); + let is_legacy = is_nn(type - 0xc0); if (is_legacy != FALSE) { return 0; } @@ -187,7 +187,7 @@ namespace EthTransaction { tx_data_len: felt, tx_data: felt* ) -> model.EthTransaction* { let tx_type = get_tx_type(tx_data); - let is_supported = is_le(tx_type, 2); + let is_supported = is_nn(2 - tx_type); with_attr error_message("Kakarot: transaction type not supported") { assert is_supported = TRUE; } diff --git a/src/utils/rlp.cairo b/src/utils/rlp.cairo index a4ed5c3ee..554c4d703 100644 --- a/src/utils/rlp.cairo +++ b/src/utils/rlp.cairo @@ -1,5 +1,5 @@ from starkware.cairo.common.bool import FALSE -from starkware.cairo.common.math_cmp import is_le +from starkware.cairo.common.math_cmp import is_nn from starkware.cairo.common.alloc import alloc from utils.utils import Helpers @@ -28,24 +28,24 @@ namespace RLP { let prefix = [data]; // Char - let is_le_127 = is_le(prefix, 0x7f); + let is_le_127 = is_nn(0x7f - prefix); if (is_le_127 != FALSE) { return (0, 0, 1); } - let is_le_183 = is_le(prefix, 0xb7); // a max 55 bytes long string + let is_le_183 = is_nn(0xb7 - prefix); // a max 55 bytes long string if (is_le_183 != FALSE) { return (0, 1, prefix - 0x80); } - let is_le_191 = is_le(prefix, 0xbf); // string longer than 55 bytes + let is_le_191 = is_nn(0xbf - prefix); // string longer than 55 bytes if (is_le_191 != FALSE) { local len_bytes_count = prefix - 0xb7; let string_len = Helpers.bytes_to_felt(len_bytes_count, data + 1); return (0, 1 + len_bytes_count, string_len); } - let is_le_247 = is_le(prefix, 0xf7); // list 0-55 bytes long + let is_le_247 = is_nn(0xf7 - prefix); // list 0-55 bytes long if (is_le_247 != FALSE) { local list_len = prefix - 0xc0; return (1, 1, list_len); @@ -92,7 +92,7 @@ namespace RLP { tempvar items = items + Item.SIZE; let total_item_len = len + offset; - let is_lt_input = is_le(total_item_len, data_len + 1); + let is_lt_input = is_nn(data_len + 1 - total_item_len); if (is_lt_input != FALSE) { let items_len = decode( items=items, data_len=data_len - total_item_len, data=data + total_item_len diff --git a/src/utils/uint256.cairo b/src/utils/uint256.cairo index 2fd4017ee..e352168b5 100644 --- a/src/utils/uint256.cairo +++ b/src/utils/uint256.cairo @@ -11,7 +11,7 @@ from starkware.cairo.common.uint256 import ( ) from starkware.cairo.common.math import unsigned_div_rem from starkware.cairo.common.bool import FALSE -from starkware.cairo.common.math_cmp import is_le +from starkware.cairo.common.math_cmp import is_nn // Adds two integers. Returns the result as a 256-bit integer and the (1-bit) carry. // Strictly equivalent and faster version of common.uint256.uint256_add using the same whitelisted hint. @@ -256,12 +256,12 @@ func uint256_signed_div_rem{range_check_ptr}(a: Uint256, div: Uint256) -> ( } // Take the absolute value of a. - local a_sign = is_le(2 ** 127, a.high); + local a_sign = is_nn(a.high - 2 ** 127); local range_check_ptr = range_check_ptr; let (local a) = uint256_cond_neg(a, should_neg=a_sign); // Take the absolute value of div. - local div_sign = is_le(2 ** 127, div.high); + local div_sign = is_nn(div.high - 2 ** 127); local range_check_ptr = range_check_ptr; let (div) = uint256_cond_neg(div, should_neg=div_sign); diff --git a/src/utils/utils.cairo b/src/utils/utils.cairo index 97744db25..28a787452 100644 --- a/src/utils/utils.cairo +++ b/src/utils/utils.cairo @@ -3,7 +3,7 @@ // StarkWare dependencies from starkware.cairo.common.alloc import alloc from starkware.cairo.common.math import assert_le, split_felt, assert_nn_le, unsigned_div_rem -from starkware.cairo.common.math_cmp import is_le, is_nn, is_not_zero +from starkware.cairo.common.math_cmp import is_nn, is_not_zero from starkware.cairo.common.memcpy import memcpy from starkware.cairo.common.dict_access import DictAccess from starkware.cairo.common.bool import TRUE, FALSE @@ -118,7 +118,7 @@ namespace Helpers { return res; } - let is_bytes_len_16_bytes_or_less = is_le(bytes_len, 16); + let is_bytes_len_16_bytes_or_less = is_nn(16 - bytes_len); // 1 - 16 bytes if (is_bytes_len_16_bytes_or_less != FALSE) {