Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: identity precompile #612

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cairo/ethereum/cancun/vm/precompiled_contracts/identity.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin, KeccakBuiltin
from starkware.cairo.common.math_cmp import is_le_felt
from ethereum.cancun.vm import Evm, EvmImpl
from ethereum.exceptions import EthereumException
from ethereum.cancun.vm.exceptions import OutOfGasError
from ethereum.utils.numeric import ceil32
from ethereum.cancun.vm.gas import GasConstants, charge_gas
from ethereum_types.numeric import Uint

// @notice Writes the message data to the output
func identity{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, keccak_ptr: KeccakBuiltin*, evm: Evm}(
) -> EthereumException* {
let data = evm.value.message.value.data;

// Gas
let ceiled_words = ceil32(Uint(data.value.len));
let word_count = ceiled_words.value / 32; // simple div as we're dividing a number divisible by 32
obatirou marked this conversation as resolved.
Show resolved Hide resolved
let size_oog = is_le_felt(2 ** 31, word_count);
if (size_oog != 0) {
tempvar err = new EthereumException(OutOfGasError);
return err;
}
let gas_cost = Uint(word_count * GasConstants.GAS_IDENTITY_WORD + GasConstants.GAS_IDENTITY);
let err = charge_gas(gas_cost);
if (cast(err, felt) != 0) {
return err;
}

// Output and Data fields are pointers to the same segment. However these segments are never appended,
// thus we can do this safely and avoid a memcpy.
EvmImpl.set_output(data);
tempvar ok = cast(0, EthereumException*);
return ok;
}
3 changes: 2 additions & 1 deletion cairo/ethereum/cancun/vm/precompiled_contracts/mapping.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from starkware.cairo.common.math_cmp import is_le_felt
from ethereum.cancun.vm import Evm
from starkware.cairo.common.math import split_felt
from ethereum.utils.numeric import divmod
from ethereum.cancun.vm.precompiled_contracts.identity import identity

// currently 10 precompiles.
const N_PRECOMPILES = 10;
Expand Down Expand Up @@ -77,7 +78,7 @@ func precompile_table_lookup{range_check_ptr}(address: felt) -> (felt, felt) {
dw 0x300000000000000000000000000000000000000;
call invalid_precompile; // RIPEMD160
dw 0x400000000000000000000000000000000000000;
call invalid_precompile; // IDENTITY
call identity; // IDENTITY
dw 0x500000000000000000000000000000000000000;
call invalid_precompile; // MODEXP
dw 0x600000000000000000000000000000000000000;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from hypothesis import given

from ethereum.cancun.vm.precompiled_contracts.identity import identity
from ethereum.exceptions import EthereumException
from tests.utils.args_gen import Evm
from tests.utils.errors import strict_raises
from tests.utils.evm_builder import EvmBuilder
from tests.utils.message_builder import MessageBuilder


@given(
evm=EvmBuilder()
.with_gas_left()
.with_message(MessageBuilder().with_data().build())
.build()
)
def test_identity(cairo_run, evm: Evm):
try:
cairo_evm = cairo_run("identity", evm)
except EthereumException as e:
with strict_raises(type(e)):
identity(evm)
return

identity(evm)
assert cairo_evm == evm
1 change: 0 additions & 1 deletion cairo/tests/ethereum/cancun/vm/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
1,
2,
3,
4,
5,
6,
7,
Expand Down