Skip to content

Commit

Permalink
refactor: use u256 for value type (kkrt-labs#895)
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: 0.2d

## 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
- [x] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying,
or link to a relevant issue. -->

Resolves kkrt-labs#773 

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

-
-
-

<!-- 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/895)
<!-- Reviewable:end -->
  • Loading branch information
enitrat authored Jan 22, 2024
1 parent ce77cf7 commit da0a8b3
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 57 deletions.
3 changes: 1 addition & 2 deletions src/kakarot/instructions/environmental_information.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ namespace EnvironmentalInformation {
memory: model.Memory*,
state: model.State*,
}(evm: model.EVM*) -> model.EVM* {
let value = Helpers.to_uint256(evm.message.value);
Stack.push(value);
Stack.push(evm.message.value);

return evm;
}
Expand Down
35 changes: 16 additions & 19 deletions src/kakarot/instructions/system_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace SystemOperations {
let popped_len = 3 + is_create2;
let (popped) = Stack.pop_n(3 + is_create2);

let value = popped[0];
let value = popped;
let offset = popped[1];
let size = popped[2];

Expand Down Expand Up @@ -98,7 +98,7 @@ namespace SystemOperations {
// Check sender balance and nonce
let sender = State.get_account(evm.message.address.evm);
let is_nonce_overflow = is_le(Constants.MAX_NONCE + 1, sender.nonce);
let (is_balance_overflow) = uint256_lt([sender.balance], value);
let (is_balance_overflow) = uint256_lt([sender.balance], [value]);
let stack_depth_limit = is_le(1024, evm.message.depth);
if (is_nonce_overflow + is_balance_overflow + stack_depth_limit != 0) {
Stack.push_uint128(0);
Expand Down Expand Up @@ -146,7 +146,7 @@ namespace SystemOperations {
valid_jumpdests=valid_jumpdests,
calldata=calldata,
calldata_len=0,
value=value.low + value.high * 2 ** 128,
value=value,
parent=parent,
address=target_account.address,
read_only=FALSE,
Expand All @@ -161,7 +161,7 @@ namespace SystemOperations {
let target_account = Account.set_nonce(target_account, 1);
State.update_account(target_account);

let transfer = model.Transfer(evm.message.address, target_account.address, value);
let transfer = model.Transfer(evm.message.address, target_account.address, [value]);
let success = State.add_transfer(transfer);
if (success == 0) {
Stack.push_uint128(0);
Expand Down Expand Up @@ -301,7 +301,7 @@ namespace SystemOperations {
let (popped) = Stack.pop_n(5);
let gas_param = popped[0];
let to = uint256_to_uint160(popped[1]);
let value = popped[2];
let value = popped + 2 * Uint256.SIZE;
let args_offset = popped + 3 * Uint256.SIZE;
let args_size = popped + 4 * Uint256.SIZE;
let (ret_offset) = Stack.peek(0);
Expand Down Expand Up @@ -351,7 +351,7 @@ namespace SystemOperations {
tempvar gas_stipend = gas + is_value_non_zero * Gas.CALL_STIPEND;

let sender = State.get_account(evm.message.address.evm);
let (sender_balance_lt_value) = uint256_lt([sender.balance], value);
let (sender_balance_lt_value) = uint256_lt([sender.balance], [value]);
tempvar is_max_depth_reached = 1 - is_not_zero(
(Constants.STACK_MAX_DEPTH + 1) - evm.message.depth
);
Expand All @@ -373,11 +373,10 @@ namespace SystemOperations {
return evm;
}

// TODO: fix value
let child_evm = CallHelper.generic_call(
evm,
gas=gas_stipend,
value=value.low,
value=value,
to=to,
code_address=to,
is_staticcall=FALSE,
Expand All @@ -387,7 +386,7 @@ namespace SystemOperations {
ret_size=ret_size,
);

let transfer = model.Transfer(evm.message.address, child_evm.message.address, value);
let transfer = model.Transfer(evm.message.address, child_evm.message.address, [value]);
let success = State.add_transfer(transfer);
if (success == 0) {
let (revert_reason_len, revert_reason) = Errors.balanceError();
Expand Down Expand Up @@ -447,11 +446,12 @@ namespace SystemOperations {
return evm;
}

tempvar zero = new Uint256(0, 0);
// Operation
let child_evm = CallHelper.generic_call(
evm,
gas,
value=0,
value=zero,
to=to,
code_address=to,
is_staticcall=TRUE,
Expand Down Expand Up @@ -486,15 +486,12 @@ namespace SystemOperations {
let (popped) = Stack.pop_n(5);
let gas_param = popped[0];
let code_address = uint256_to_uint160(popped[1]);
let value = popped[2];
let value = popped + 2 * Uint256.SIZE;
let args_offset = popped + 3 * Uint256.SIZE;
let args_size = popped + 4 * Uint256.SIZE;
let (ret_offset) = Stack.peek(0);
let (ret_size) = Stack.peek(1);

tempvar is_value_non_zero = is_not_zero(value.low) + is_not_zero(value.high);
tempvar is_value_non_zero = is_not_zero(is_value_non_zero);

// Gas
let memory_expansion_cost = Gas.max_memory_expansion_cost(
memory.words_len, args_offset, args_size, ret_offset, ret_size
Expand All @@ -504,7 +501,8 @@ namespace SystemOperations {
// TODO
let access_gas_cost = 0;

// TODO: fix value when refactoring CALLs for proper gas accounting
tempvar is_value_non_zero = is_not_zero(value.low) + is_not_zero(value.high);
tempvar is_value_non_zero = is_not_zero(is_value_non_zero);
let transfer_gas_cost = is_value_non_zero * Gas.CALL_VALUE;

let extra_gas = access_gas_cost + transfer_gas_cost;
Expand All @@ -522,7 +520,7 @@ namespace SystemOperations {
tempvar gas_stipend = gas + is_value_non_zero * Gas.CALL_STIPEND;

let sender = State.get_account(evm.message.address.evm);
let (sender_balance_lt_value) = uint256_lt([sender.balance], value);
let (sender_balance_lt_value) = uint256_lt([sender.balance], [value]);
tempvar is_max_depth_reached = 1 - is_not_zero(
(Constants.STACK_MAX_DEPTH + 1) - evm.message.depth
);
Expand All @@ -547,7 +545,7 @@ namespace SystemOperations {
let child_evm = CallHelper.generic_call(
evm,
gas=gas_stipend,
value=value.low,
value=value,
to=evm.message.address.evm,
code_address=code_address,
is_staticcall=FALSE,
Expand Down Expand Up @@ -733,7 +731,7 @@ namespace CallHelper {
}(
evm: model.EVM*,
gas: felt,
value: felt,
value: Uint256*,
to: felt,
code_address: felt,
is_staticcall: bool,
Expand All @@ -757,7 +755,6 @@ namespace CallHelper {
evm_address=code_address,
calldata_len=args_size.low,
calldata=calldata,
value=value,
parent=parent,
gas_left=gas,
);
Expand Down
4 changes: 2 additions & 2 deletions src/kakarot/interfaces/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ namespace IKakarot {
to: felt,
gas_limit: felt,
gas_price: felt,
value: felt,
value: Uint256,
data_len: felt,
data: felt*,
) -> (return_data_len: felt, return_data: felt*, success: felt, gas_used: felt) {
}

func eth_send_transaction(
to: felt, gas_limit: felt, gas_price: felt, value: felt, data_len: felt, data: felt*
to: felt, gas_limit: felt, gas_price: felt, value: Uint256, data_len: felt, data: felt*
) -> (return_data_len: felt, return_data: felt*, success: felt, gas_used: felt) {
}
}
6 changes: 3 additions & 3 deletions src/kakarot/interpreter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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
from starkware.cairo.lang.compiler.lib.registers import get_fp_and_pc
from starkware.cairo.common.uint256 import Uint256

// Internal dependencies
from kakarot.account import Account
Expand Down Expand Up @@ -721,7 +722,7 @@ namespace Interpreter {
bytecode: felt*,
calldata_len: felt,
calldata: felt*,
value: felt,
value: Uint256*,
gas_limit: felt,
) -> (model.EVM*, model.Stack*, model.Memory*, model.State*) {
alloc_locals;
Expand Down Expand Up @@ -782,12 +783,11 @@ namespace Interpreter {

with state {
// Handle value
let amount = Helpers.to_uint256(value);
let origin_starknet_address = Account.compute_starknet_address(env.origin);
tempvar origin_address = new model.Address(
starknet=origin_starknet_address, evm=env.origin
);
let transfer = model.Transfer(origin_address, address, [amount]);
let transfer = model.Transfer(origin_address, address, [value]);
let success = State.add_transfer(transfer);
// Check collision
Expand Down
14 changes: 10 additions & 4 deletions src/kakarot/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ from starkware.cairo.common.bool import FALSE, TRUE
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.cairo.common.uint256 import Uint256
from starkware.starknet.common.syscalls import get_caller_address
from starkware.cairo.common.registers import get_fp_and_pc

// Local dependencies
from backend.starknet import Starknet
Expand Down Expand Up @@ -130,11 +131,14 @@ func eth_call{
to: felt,
gas_limit: felt,
gas_price: felt,
value: felt,
value: Uint256,
data_len: felt,
data: felt*,
) -> (return_data_len: felt, return_data: felt*, success: felt, gas_used: felt) {
let (evm, state) = Kakarot.eth_call(origin, to, gas_limit, gas_price, value, data_len, data);
alloc_locals;
let fp_and_pc = get_fp_and_pc();
local __fp__: felt* = fp_and_pc.fp_val;
let (evm, state) = Kakarot.eth_call(origin, to, gas_limit, gas_price, &value, data_len, data);
let gas_used = gas_limit - evm.gas_left;
return (evm.return_data_len, evm.return_data, 1 - evm.reverted, gas_used);
}
Expand All @@ -155,13 +159,15 @@ func eth_call{
@external
func eth_send_transaction{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}(to: felt, gas_limit: felt, gas_price: felt, value: felt, data_len: felt, data: felt*) -> (
}(to: felt, gas_limit: felt, gas_price: felt, value: Uint256, data_len: felt, data: felt*) -> (
return_data_len: felt, return_data: felt*, success: felt, gas_used: felt
) {
alloc_locals;
let fp_and_pc = get_fp_and_pc();
local __fp__: felt* = fp_and_pc.fp_val;
let (local starknet_caller_address) = get_caller_address();
let (local origin) = Kakarot.safe_get_evm_address(starknet_caller_address);
let (evm, state) = Kakarot.eth_call(origin, to, gas_limit, gas_price, value, data_len, data);
let (evm, state) = Kakarot.eth_call(origin, to, gas_limit, gas_price, &value, data_len, data);
let gas_used = gas_limit - evm.gas_left;
let result = (evm.return_data_len, evm.return_data, 1 - evm.reverted, gas_used);

Expand Down
3 changes: 2 additions & 1 deletion src/kakarot/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ from starkware.cairo.common.bool import FALSE, TRUE
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.starknet.common.syscalls import get_caller_address, get_tx_info
from starkware.cairo.common.math_cmp import is_not_zero
from starkware.cairo.common.uint256 import Uint256

from backend.starknet import Starknet
from kakarot.account import Account
Expand Down Expand Up @@ -71,7 +72,7 @@ namespace Kakarot {
to: felt,
gas_limit: felt,
gas_price: felt,
value: felt,
value: Uint256*,
data_len: felt,
data: felt*,
) -> (model.EVM*, model.State*) {
Expand Down
2 changes: 1 addition & 1 deletion src/kakarot/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace model {
valid_jumpdests: DictAccess*,
calldata: felt*,
calldata_len: felt,
value: felt,
value: Uint256*,
parent: Parent*,
address: Address*,
read_only: felt,
Expand Down
6 changes: 3 additions & 3 deletions src/kakarot/precompiles/precompiles.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.cairo.common.default_dict import default_dict_new
from starkware.cairo.common.math_cmp import is_le, is_not_zero
from starkware.cairo.common.dict_access import DictAccess
from starkware.cairo.common.uint256 import Uint256

// Internal dependencies
from kakarot.account import Account
Expand All @@ -32,7 +33,6 @@ namespace Precompiles {
// @param evm_address The precompile evm_address to be executed
// @param calldata_len The calldata length
// @param calldata The calldata.
// @param value The value.
// @param parent The calling context.
// @param gas_left The gas left.
// @return EVM The initialized execution context.
Expand All @@ -45,7 +45,6 @@ namespace Precompiles {
evm_address: felt,
calldata_len: felt,
calldata: felt*,
value: felt,
parent: model.Parent*,
gas_left: felt,
) -> model.EVM* {
Expand All @@ -55,14 +54,15 @@ namespace Precompiles {
// Precompiles don't have an actual Starknet address
tempvar address = new model.Address(starknet=0, evm=evm_address);
let (valid_jumdests) = default_dict_new(0);
tempvar value = new Uint256(0, 0);
tempvar message = new model.Message(
bytecode=cast(0, felt*),
bytecode_len=0,
valid_jumpdests_start=valid_jumdests,
valid_jumpdests=valid_jumdests,
calldata=cast(0, felt*),
calldata_len=0,
value=0,
value=value,
parent=parent,
address=address,
read_only=FALSE,
Expand Down
14 changes: 7 additions & 7 deletions src/utils/eth_transaction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace EthTransaction {
gas_price: felt,
gas_limit: felt,
destination: felt,
amount: felt,
amount: Uint256,
chain_id: felt,
payload_len: felt,
payload: felt*,
Expand Down Expand Up @@ -71,8 +71,8 @@ namespace EthTransaction {
let destination = Helpers.bytes_to_felt(
sub_items[nonce_idx + 3].data_len, sub_items[nonce_idx + 3].data
);
let amount = Helpers.bytes_to_felt(
sub_items[nonce_idx + 4].data_len, sub_items[nonce_idx + 4].data
let amount = Helpers.bytes_i_to_uint256(
sub_items[nonce_idx + 4].data, sub_items[nonce_idx + 4].data_len
);
let payload_len = sub_items[nonce_idx + 5].data_len;
let payload: felt* = sub_items[nonce_idx + 5].data;
Expand Down Expand Up @@ -109,7 +109,7 @@ namespace EthTransaction {
gas_price: felt,
gas_limit: felt,
destination: felt,
amount: felt,
amount: Uint256,
chain_id: felt,
payload_len: felt,
payload: felt*,
Expand Down Expand Up @@ -157,8 +157,8 @@ namespace EthTransaction {
let destination = Helpers.bytes_to_felt(
sub_items[gas_price_idx + 2].data_len, sub_items[gas_price_idx + 2].data
);
let amount = Helpers.bytes_to_felt(
sub_items[gas_price_idx + 3].data_len, sub_items[gas_price_idx + 3].data
let amount = Helpers.bytes_i_to_uint256(
sub_items[gas_price_idx + 3].data, sub_items[gas_price_idx + 3].data_len
);
let payload_len = sub_items[gas_price_idx + 4].data_len;
let payload: felt* = sub_items[gas_price_idx + 4].data;
Expand Down Expand Up @@ -199,7 +199,7 @@ namespace EthTransaction {
gas_price: felt,
gas_limit: felt,
destination: felt,
amount: felt,
amount: Uint256,
chain_id: felt,
payload_len: felt,
payload: felt*,
Expand Down
Loading

0 comments on commit da0a8b3

Please sign in to comment.