Skip to content

Commit

Permalink
opti: replace is_le by is_nn (#1287)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

<!-- Give an estimate of the time you spent on this PR in terms of work
days.
Did you spend 0.5 days on this PR or rather 2 days?  -->

Time spent on this PR:

## Pull request type

<!-- Please try to limit your pull request to one type,
submit multiple pull requests if needed. -->

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

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/1287)
<!-- Reviewable:end -->
  • Loading branch information
obatirou authored Jul 22, 2024
1 parent 3c13496 commit 7bc70ad
Show file tree
Hide file tree
Showing 21 changed files with 63 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/backend/starknet.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/kakarot/account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
6 changes: 3 additions & 3 deletions src/kakarot/accounts/account_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 8 additions & 8 deletions src/kakarot/accounts/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
);
Expand Down
4 changes: 2 additions & 2 deletions src/kakarot/evm.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/kakarot/gas.cairo
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/kakarot/instructions/environmental_information.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/kakarot/instructions/memory_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/kakarot/instructions/push_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/kakarot/instructions/system_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions src/kakarot/interpreter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/kakarot/memory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/kakarot/precompiles/blake2f.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/kakarot/precompiles/kakarot_precompiles.cairo
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/kakarot/precompiles/precompiles.cairo
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 7bc70ad

Please sign in to comment.