diff --git a/cairo/ethereum/cancun/vm/memory.cairo b/cairo/ethereum/cancun/vm/memory.cairo index 1f9b7455..cc8796db 100644 --- a/cairo/ethereum/cancun/vm/memory.cairo +++ b/cairo/ethereum/cancun/vm/memory.cairo @@ -12,62 +12,50 @@ from starkware.cairo.common.math_cmp import is_le, is_not_zero from ethereum_types.bytes import Bytes, BytesStruct, Bytearray, BytearrayStruct, Bytes1DictAccess from ethereum_types.numeric import U256 +from ethereum.utils.numeric import max // @notice Write bytes to memory at a given position. -// @dev assumption: start_position < 2**128 -// @dev assumption: value.len < 2**128 // @param memory The pointer to the bytearray. // @param start_position Starting position to write at. // @param value Bytes to write. func memory_write{range_check_ptr, memory: Bytearray}(start_position: U256, value: Bytes) { alloc_locals; let bytes_len = value.value.len; - let bytes_data = value.value.data; let start_position_felt = start_position.value.low; - with_attr error_message("memory_write: start_position > 2**128") { + with_attr error_message("memory_write: start_position > 2**128 || value.len > 2**128") { assert start_position.value.high = 0; } - let new_len = start_position_felt + bytes_len; + let bytes_data = value.value.data; let dict_ptr = cast(memory.value.dict_ptr, DictAccess*); with dict_ptr { Internals._write_bytes(start_position_felt, bytes_data, bytes_len); } let new_dict_ptr = cast(dict_ptr, Bytes1DictAccess*); - // Update length if we wrote beyond current length - tempvar current_len = memory.value.len; - let is_new_le_current = is_le(new_len, current_len); - if (is_new_le_current != TRUE) { - tempvar final_len = new_len; - } else { - tempvar final_len = current_len; - } - - tempvar memory = Bytearray( - new BytearrayStruct(memory.value.dict_ptr_start, new_dict_ptr, final_len) - ); + let len = max(memory.value.len, start_position.value.low + value.value.len); + tempvar memory = Bytearray(new BytearrayStruct(memory.value.dict_ptr_start, new_dict_ptr, len)); return (); } // @notice Read bytes from memory. -// @dev assumption: start_position < 2**128 -// @dev assumption: size < 2**128 // @param memory The pointer to the bytearray. // @param start_position Starting position to read from. // @param size Number of bytes to read. // @return The bytes read from memory. func memory_read_bytes{memory: Bytearray}(start_position: U256, size: U256) -> Bytes { alloc_locals; - let (local output: felt*) = alloc(); - let start_position_felt = start_position.value.low; - let size_felt = size.value.low; with_attr error_message("memory_read_bytes: start_position > 2**128 || size > 2**128") { assert start_position.value.high = 0; assert size.value.high = 0; } + + let (local output: felt*) = alloc(); let dict_ptr = cast(memory.value.dict_ptr, DictAccess*); + let start_position_felt = start_position.value.low; + let size_felt = size.value.low; + with dict_ptr { Internals._read_bytes(start_position_felt, size_felt, output); } @@ -114,23 +102,22 @@ namespace Internals { return (); } - tempvar start_position = start_position; - tempvar data = data; - tempvar len = len; + tempvar index = len; tempvar dict_ptr = dict_ptr; body: - let start_position = [ap - 4]; - let data = cast([ap - 3], felt*); - let len = [ap - 2]; + let index = [ap - 2] - 1; let dict_ptr = cast([ap - 1], DictAccess*); - dict_write(start_position, [data]); + let start_position = [fp - 5]; + let data = cast([fp - 4], felt*); + + dict_write(start_position + index, data[index]); - tempvar start_position = start_position + 1; - tempvar data = data + 1; - tempvar len = len - 1; + tempvar index = index; tempvar dict_ptr = dict_ptr; - jmp body if len != 0; + jmp body if index != 0; + + end: return (); } @@ -139,29 +126,28 @@ namespace Internals { // @param size Number of bytes to read. // @param output Pointer to write output bytes to. func _read_bytes{dict_ptr: DictAccess*}(start_position: felt, size: felt, output: felt*) { + alloc_locals; if (size == 0) { return (); } - tempvar start_position = start_position; - tempvar size = size; - tempvar output = output; + tempvar dict_index = start_position + size; tempvar dict_ptr = dict_ptr; body: - let start_position = [ap - 4]; - let size = [ap - 3]; - let output = cast([ap - 2], felt*); + let dict_index = [ap - 2] - 1; let dict_ptr = cast([ap - 1], DictAccess*); + let output = cast([fp - 3], felt*); + let start_position = [fp - 5]; + tempvar output_index = dict_index - start_position; - let (value) = dict_read(start_position); - assert [output] = value; + let (value) = dict_read(dict_index); + assert output[output_index] = value; - tempvar start_position = start_position + 1; - tempvar size = size - 1; - tempvar output = output + 1; + tempvar dict_index = dict_index; tempvar dict_ptr = dict_ptr; - jmp body if size != 0; + jmp body if output_index != 0; + return (); } diff --git a/cairo/pyproject.toml b/cairo/pyproject.toml index e4c72f08..3cc42647 100644 --- a/cairo/pyproject.toml +++ b/cairo/pyproject.toml @@ -9,7 +9,6 @@ dependencies = [ "cairo-lang>=0.13.2", "ethereum", "marshmallow-dataclass>=8.6.1", - "polars>=1.18.0", "python-dotenv>=1.0.1", "toml>=0.10.2", "web3>=7.2.0", diff --git a/cairo/tests/utils/serde.py b/cairo/tests/utils/serde.py index 4444cb10..7ee5acf7 100644 --- a/cairo/tests/utils/serde.py +++ b/cairo/tests/utils/serde.py @@ -132,7 +132,7 @@ def serialize_type(self, path: Tuple[str, ...], ptr) -> Any: if "__main__" in full_path: full_path = self.main_part + full_path[full_path.index("__main__") + 1 :] python_cls = to_python_type(full_path) - origin_cls = get_origin(python_cls) + origin_cls = get_origin(python_cls) or python_cls annotations = [] if get_origin(python_cls) is Annotated: diff --git a/uv.lock b/uv.lock index 02380d59..a8551707 100644 --- a/uv.lock +++ b/uv.lock @@ -366,7 +366,6 @@ dependencies = [ { name = "cairo-lang" }, { name = "ethereum" }, { name = "marshmallow-dataclass" }, - { name = "polars" }, { name = "python-dotenv" }, { name = "toml" }, { name = "web3" }, @@ -391,7 +390,6 @@ requires-dist = [ { name = "cairo-lang", specifier = ">=0.13.2" }, { name = "ethereum", git = "https://github.com/kkrt-labs/execution-specs.git?rev=b255036441d64437bd4fc9f9068bc64c45470e93" }, { name = "marshmallow-dataclass", specifier = ">=8.6.1" }, - { name = "polars", specifier = ">=1.18.0" }, { name = "python-dotenv", specifier = ">=1.0.1" }, { name = "toml", specifier = ">=0.10.2" }, { name = "web3", specifier = ">=7.2.0" }, @@ -2101,20 +2099,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/88/5f/e351af9a41f866ac3f1fac4ca0613908d9a41741cfcf2228f4ad853b697d/pluggy-1.5.0-py3-none-any.whl", hash = "sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669", size = 20556 }, ] -[[package]] -name = "polars" -version = "1.18.0" -source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/9b/57/821f6b625e63516bf5f0ba428618dd013c43fd79b20e722c454a978cbefe/polars-1.18.0.tar.gz", hash = "sha256:5c2f119555ae8d822a5322509c6abd91601a8931115d2e4c3fff13fadf39e877", size = 4257494 } -wheels = [ - { url = "https://files.pythonhosted.org/packages/4e/df/289578844b299f97125178ad6db60dc1b494ec8d813d397118f2493c392a/polars-1.18.0-cp39-abi3-macosx_10_12_x86_64.whl", hash = "sha256:27a6c7e5d2d15afb5f06291433019411c9a28e59e49741442d11a6a945f21daa", size = 29067782 }, - { url = "https://files.pythonhosted.org/packages/7b/79/cdc5d888a5f858f5c572d3a3a8fa65724d4426aa9edbfd6461d3dc85bc47/polars-1.18.0-cp39-abi3-macosx_11_0_arm64.whl", hash = "sha256:6431563aee2dfa6787b0debbed3f565ebb4322da32317d95c8eac3e48330bc28", size = 25807696 }, - { url = "https://files.pythonhosted.org/packages/f4/cd/cd49096ead3dd208495945021d3042dad01d0dd63702b6f4f4f7e3a3983b/polars-1.18.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a333ff578373e29e0cacc79c35afe42c0620813c9b0c832009ab8b330e421093", size = 32287987 }, - { url = "https://files.pythonhosted.org/packages/66/28/7eb57b1f37f7c0206baf1877d0dcd082518ef3de34fe8621190b6da4801b/polars-1.18.0-cp39-abi3-manylinux_2_24_aarch64.whl", hash = "sha256:3a3a65a3ad6b6b0bd61a33f215856cfdd3e3abc9942e69526b2b88c0ef8683a4", size = 29314336 }, - { url = "https://files.pythonhosted.org/packages/09/9e/184f777b41bba086463771edf7dbd3ba13c071222117ab5c19c99e76bc66/polars-1.18.0-cp39-abi3-win_amd64.whl", hash = "sha256:a79ef2542454d9cace63e8fa528cf808b6377077173be522df9b8c0e792ce96a", size = 32356143 }, - { url = "https://files.pythonhosted.org/packages/2c/dc/5b3345688bb14cda0ea23f42c96553aa75c4b8a6992f38cac0df6a44ec31/polars-1.18.0-cp39-abi3-win_arm64.whl", hash = "sha256:52b543da52f4f6a661a2fa3cdd4b499938bdb34eeae538ec3bcef6c8c41bfc33", size = 28631561 }, -] - [[package]] name = "prometheus-client" version = "0.21.1"