From 00ee90123c0bbaffe13dd05a79d2d57b0cb747be Mon Sep 17 00:00:00 2001 From: Lior Goldberg Date: Wed, 24 May 2023 11:09:17 +0300 Subject: [PATCH] Cairo v0.11.2a0 (pre). --- README.md | 4 +- WORKSPACE | 8 +- src/starkware/cairo/lang/VERSION | 2 +- .../cairo/lang/create_cairo_lang_zip.py | 2 +- .../cairo/lang/ide/vscode-cairo/package.json | 2 +- src/starkware/cairo/lang/vm/cairo_pie.py | 69 ++++++++- src/starkware/cairo/lang/vm/cairo_pie_test.py | 36 ++++- src/starkware/cairo/lang/vm/memory_dict.py | 11 +- src/starkware/cairo/vars.bzl | 2 +- .../business_logic/state/state_api_objects.py | 2 +- ... => BUILD.cairo-compiler-archive-1.1.0-rc} | 2 +- src/starkware/starknet/compiler/v1/compile.py | 2 +- .../compiler/v1/mainnet_libfuncs.json | 132 +++++++++++++++++- .../starknet/definitions/constants.py | 2 +- .../starknet/definitions/general_config.yml | 4 +- 15 files changed, 253 insertions(+), 27 deletions(-) rename src/starkware/starknet/compiler/v1/{BUILD.cairo-compiler-archive-1.0.0 => BUILD.cairo-compiler-archive-1.1.0-rc} (74%) diff --git a/README.md b/README.md index b3530ef4..4e12b196 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ We recommend starting from [Setting up the environment](https://cairo-lang.org/d # Installation instructions You should be able to download the python package zip file directly from -[github](https://github.com/starkware-libs/cairo-lang/releases/tag/v0.11.0.2) +[github](https://github.com/starkware-libs/cairo-lang/releases/tag/v0.11.2) and install it using ``pip``. See [Setting up the environment](https://cairo-lang.org/docs/quickstart.html). @@ -54,7 +54,7 @@ Once the docker image is built, you can fetch the python package zip file using: ```bash > container_id=$(docker create cairo) -> docker cp ${container_id}:/app/cairo-lang-0.11.1.zip . +> docker cp ${container_id}:/app/cairo-lang-0.11.2.zip . > docker rm -v ${container_id} ``` diff --git a/WORKSPACE b/WORKSPACE index d01f71da..517369af 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -40,10 +40,12 @@ http_archive( ) http_archive( - name = "cairo-compiler-archive-1.0.0", - build_file = get_from_cairo_lang("//src/starkware/starknet/compiler/v1:BUILD.cairo-compiler-archive-1.0.0"), + name = "cairo-compiler-archive-1.1.0-rc", + build_file = get_from_cairo_lang( + "//src/starkware/starknet/compiler/v1:BUILD.cairo-compiler-archive-1.1.0-rc", + ), strip_prefix = "cairo", - url = "https://github.com/starkware-libs/cairo/releases/download/v1.0.0/release-x86_64-unknown-linux-musl.tar.gz", + url = "https://github.com/starkware-libs/cairo/releases/download/v1.1.0-rc0/release-x86_64-unknown-linux-musl.tar.gz", ) http_archive( diff --git a/src/starkware/cairo/lang/VERSION b/src/starkware/cairo/lang/VERSION index 071f9c18..90ecd16f 100644 --- a/src/starkware/cairo/lang/VERSION +++ b/src/starkware/cairo/lang/VERSION @@ -1 +1 @@ -0.11.1.1 +0.11.2a0 diff --git a/src/starkware/cairo/lang/create_cairo_lang_zip.py b/src/starkware/cairo/lang/create_cairo_lang_zip.py index 9894b2f1..966416aa 100644 --- a/src/starkware/cairo/lang/create_cairo_lang_zip.py +++ b/src/starkware/cairo/lang/create_cairo_lang_zip.py @@ -10,7 +10,7 @@ from starkware.python.utils import get_build_dir_path -COMPILER_DIR = get_build_dir_path("../cairo-compiler-archive-1.0.0") +COMPILER_DIR = get_build_dir_path("../cairo-compiler-archive-1.1.0-rc") INIT_FILE_CONTENT = "__path__ = __import__('pkgutil').extend_path(__path__, __name__)\n" diff --git a/src/starkware/cairo/lang/ide/vscode-cairo/package.json b/src/starkware/cairo/lang/ide/vscode-cairo/package.json index 8bdda557..78ff6120 100644 --- a/src/starkware/cairo/lang/ide/vscode-cairo/package.json +++ b/src/starkware/cairo/lang/ide/vscode-cairo/package.json @@ -2,7 +2,7 @@ "name": "cairo", "displayName": "Cairo", "description": "Support Cairo syntax", - "version": "0.11.1", + "version": "0.11.2", "engines": { "vscode": "^1.30.0" }, diff --git a/src/starkware/cairo/lang/vm/cairo_pie.py b/src/starkware/cairo/lang/vm/cairo_pie.py index fbd40e27..eec48145 100644 --- a/src/starkware/cairo/lang/vm/cairo_pie.py +++ b/src/starkware/cairo/lang/vm/cairo_pie.py @@ -4,21 +4,22 @@ import copy import dataclasses +import functools import io import json import math import zipfile from dataclasses import field -from typing import Any, ClassVar, Dict, List, Type +from typing import Any, ClassVar, Dict, List, Mapping, Optional, Tuple, Type import marshmallow import marshmallow.fields as mfields import marshmallow_dataclass from starkware.cairo.lang.compiler.program import StrippedProgram, is_valid_builtin_name -from starkware.cairo.lang.vm.memory_dict import MemoryDict +from starkware.cairo.lang.vm.memory_dict import MemoryDict, RelocateValueFunc from starkware.cairo.lang.vm.memory_segments import is_valid_memory_addr, is_valid_memory_value -from starkware.cairo.lang.vm.relocatable import RelocatableValue +from starkware.cairo.lang.vm.relocatable import MaybeRelocatable, RelocatableValue, relocate_value from starkware.python.utils import add_counters, multiply_counter_by_scalar, sub_counters from starkware.starkware_utils.marshmallow_dataclass_fields import additional_metadata @@ -279,12 +280,68 @@ def from_file(cls, fileobj) -> "CairoPie": return cls(metadata, memory, additional_data, execution_resources, version) - def to_file(self, file): + def merge_extra_segments(self) -> Tuple[List[SegmentInfo], Dict[int, RelocatableValue]]: + """ + Merges extra_segments to one segment. + Returns a tuple of the new extra_segments (which contains a single merged segment) and a + dictionary from old segment index to its offset in the new segment. + """ + assert len(self.metadata.extra_segments) > 0 + + # Take the index of the segment from the first merged segment. + new_segment_index = self.metadata.extra_segments[0].index + segment_offsets = {} + segments_accumulated_size = 0 + for segment in self.metadata.extra_segments: + segment_offsets[segment.index] = RelocatableValue( + new_segment_index, segments_accumulated_size + ) + segments_accumulated_size += segment.size + return ( + [SegmentInfo(index=new_segment_index, size=segments_accumulated_size)], + segment_offsets, + ) + + def get_relocate_value_func( + self, segment_offsets: Optional[Mapping[int, MaybeRelocatable]] + ) -> Optional[RelocateValueFunc]: + """ + Returns a relocate_value function that relocates values according to the given segment + offsets. + """ + if segment_offsets is None: + return None + + return functools.partial( + relocate_value, + segment_offsets=segment_offsets, + prime=self.program.prime, + # The known segments (such as builtins) are missing since we do not want to relocate + # them. + allow_missing_segments=True, + ) + + def to_file(self, file, merge_extra_segments: bool = False): + extra_segments, segment_offsets = ( + self.merge_extra_segments() + if merge_extra_segments and len(self.metadata.extra_segments) > 0 + else (None, None) + ) + metadata = self.metadata + if extra_segments is not None: + metadata = dataclasses.replace(metadata, extra_segments=extra_segments) with zipfile.ZipFile(file, mode="w", compression=zipfile.ZIP_DEFLATED) as zf: with zf.open(self.METADATA_FILENAME, "w") as fp: - fp.write(json.dumps(CairoPieMetadata.Schema().dump(self.metadata)).encode("ascii")) + fp.write(json.dumps(CairoPieMetadata.Schema().dump(metadata)).encode("ascii")) with zf.open(self.MEMORY_FILENAME, "w") as fp: - fp.write(self.memory.serialize(self.metadata.field_bytes)) + fp.write( + self.memory.serialize( + field_bytes=self.metadata.field_bytes, + relocate_value=self.get_relocate_value_func( + segment_offsets=segment_offsets + ), + ) + ) with zf.open(self.ADDITIONAL_DATA_FILENAME, "w") as fp: fp.write(json.dumps(self.additional_data).encode("ascii")) with zf.open(self.EXECUTION_RESOURCES_FILENAME, "w") as fp: diff --git a/src/starkware/cairo/lang/vm/cairo_pie_test.py b/src/starkware/cairo/lang/vm/cairo_pie_test.py index fccb45f5..f253651a 100644 --- a/src/starkware/cairo/lang/vm/cairo_pie_test.py +++ b/src/starkware/cairo/lang/vm/cairo_pie_test.py @@ -1,6 +1,6 @@ import io import random -from typing import Dict +from typing import Dict, Mapping import pytest @@ -15,7 +15,11 @@ from starkware.cairo.lang.vm.cairo_runner import get_runner_from_code from starkware.cairo.lang.vm.memory_dict import MemoryDict from starkware.cairo.lang.vm.memory_segments import SEGMENT_SIZE_UPPER_BOUND -from starkware.cairo.lang.vm.relocatable import MaybeRelocatableDict, RelocatableValue +from starkware.cairo.lang.vm.relocatable import ( + MaybeRelocatable, + MaybeRelocatableDict, + RelocatableValue, +) from starkware.python.utils import add_counters @@ -206,3 +210,31 @@ def test_filter_unused_builtins(): diff = (execution_resources2 - execution_resources1).filter_unused_builtins() assert diff.builtin_instance_counter == {"builtin3": 2} + + +def test_cairo_pie_merge_extra_segments(cairo_pie: CairoPie): + """ + Tests to_file when merge_extra_segments parameter is True. + """ + cairo_pie.metadata.extra_segments = [SegmentInfo(8, 10), SegmentInfo(9, 20)] + initializer: Mapping[MaybeRelocatable, MaybeRelocatable] = { + 1: 2, + RelocatableValue(3, 4): RelocatableValue(6, 7), + RelocatableValue(8, 0): RelocatableValue(8, 4), + RelocatableValue(9, 3): RelocatableValue(9, 7), + } + cairo_pie.memory = MemoryDict(initializer) + + fileobj = io.BytesIO() + cairo_pie.to_file(fileobj, merge_extra_segments=True) + actual_cairo_pie = CairoPie.from_file(fileobj) + + assert actual_cairo_pie.metadata.extra_segments == [SegmentInfo(8, 30)] + + expected_memory_initializer: Mapping[MaybeRelocatable, MaybeRelocatable] = { + 1: 2, + RelocatableValue(3, 4): RelocatableValue(6, 7), + RelocatableValue(8, 0): RelocatableValue(8, 4), + RelocatableValue(8, 13): RelocatableValue(8, 17), + } + assert actual_cairo_pie.memory == MemoryDict(expected_memory_initializer) diff --git a/src/starkware/cairo/lang/vm/memory_dict.py b/src/starkware/cairo/lang/vm/memory_dict.py index f706221b..8c881d25 100644 --- a/src/starkware/cairo/lang/vm/memory_dict.py +++ b/src/starkware/cairo/lang/vm/memory_dict.py @@ -32,6 +32,8 @@ def __init__(self, addr, old_value, new_value): ] ] +RelocateValueFunc = Callable[[MaybeRelocatable], MaybeRelocatable] + class MemoryDict: """ @@ -213,14 +215,17 @@ def set_without_checks(self, addr: MaybeRelocatable, value: MaybeRelocatable): """ self.data[addr] = value - def serialize(self, field_bytes): + def serialize(self, field_bytes, relocate_value: Optional[RelocateValueFunc] = None): assert ( len(self.relocation_rules) == 0 ), "Cannot serialize a MemoryDict with active segment relocation rules." + if relocate_value is None: + relocate_value = lambda val: val + return b"".join( - RelocatableValue.to_bytes(addr, ADDR_SIZE_IN_BYTES, "little") - + RelocatableValue.to_bytes(value, field_bytes, "little") + RelocatableValue.to_bytes(relocate_value(addr), ADDR_SIZE_IN_BYTES, "little") + + RelocatableValue.to_bytes(relocate_value(value), field_bytes, "little") for addr, value in self.items() ) diff --git a/src/starkware/cairo/vars.bzl b/src/starkware/cairo/vars.bzl index 9ff5d914..b0a13a7a 100644 --- a/src/starkware/cairo/vars.bzl +++ b/src/starkware/cairo/vars.bzl @@ -3,4 +3,4 @@ CAIRO_VM_CRYPTO_ADDITIONAL_LIBS = [] CAIRO_COMMON_LIB_ADDITIONAL_FILES = [] CAIRO_COMMON_LIB_ADDITIONAL_LIBS = [] CAIRO_INSTANCES_LIB_ADDITIONAL_FILES = [] -CAIRO_COMPILER_ARCHIVE = "cairo-compiler-archive-1.0.0" +CAIRO_COMPILER_ARCHIVE = "cairo-compiler-archive-1.1.0-rc" diff --git a/src/starkware/starknet/business_logic/state/state_api_objects.py b/src/starkware/starknet/business_logic/state/state_api_objects.py index aa775c00..414c8aa5 100644 --- a/src/starkware/starknet/business_logic/state/state_api_objects.py +++ b/src/starkware/starknet/business_logic/state/state_api_objects.py @@ -28,7 +28,7 @@ class BlockInfo(ValidatedMarshmallowDataclass): # The sequencer address of this block. sequencer_address: Optional[int] = field(metadata=fields.optional_sequencer_address_metadata) - # The version of StarkNet system (e.g. "0.11.1"). + # The version of Starknet system (e.g. "0.11.2"). starknet_version: Optional[str] = field(metadata=fields.starknet_version_metadata) @classmethod diff --git a/src/starkware/starknet/compiler/v1/BUILD.cairo-compiler-archive-1.0.0 b/src/starkware/starknet/compiler/v1/BUILD.cairo-compiler-archive-1.1.0-rc similarity index 74% rename from src/starkware/starknet/compiler/v1/BUILD.cairo-compiler-archive-1.0.0 rename to src/starkware/starknet/compiler/v1/BUILD.cairo-compiler-archive-1.1.0-rc index bf38bd7e..36b400c4 100644 --- a/src/starkware/starknet/compiler/v1/BUILD.cairo-compiler-archive-1.0.0 +++ b/src/starkware/starknet/compiler/v1/BUILD.cairo-compiler-archive-1.1.0-rc @@ -1,7 +1,7 @@ package(default_visibility = ["//visibility:public"]) filegroup( - name = "cairo-compiler-archive-1.0.0", + name = "cairo-compiler-archive-1.1.0-rc", srcs = glob(["**/*"]), visibility = ["//visibility:public"], ) diff --git a/src/starkware/starknet/compiler/v1/compile.py b/src/starkware/starknet/compiler/v1/compile.py index d305f044..b4a3a73b 100644 --- a/src/starkware/starknet/compiler/v1/compile.py +++ b/src/starkware/starknet/compiler/v1/compile.py @@ -14,7 +14,7 @@ r = runfiles.Create() - COMPILER_DIR = r.Rlocation("cairo-compiler-archive-1.0.0/bin") + COMPILER_DIR = r.Rlocation("cairo-compiler-archive-1.1.0-rc/bin") else: COMPILER_DIR = os.path.join(os.path.dirname(__file__), "bin") diff --git a/src/starkware/starknet/compiler/v1/mainnet_libfuncs.json b/src/starkware/starknet/compiler/v1/mainnet_libfuncs.json index a189a916..eab55f5f 100644 --- a/src/starkware/starknet/compiler/v1/mainnet_libfuncs.json +++ b/src/starkware/starknet/compiler/v1/mainnet_libfuncs.json @@ -1,3 +1,133 @@ { - "allowed_libfuncs": [] + "allowed_libfuncs": [ + "alloc_local", + "array_append", + "array_get", + "array_len", + "array_new", + "array_pop_front", + "array_snapshot_pop_front", + "bitwise", + "bool_and_impl", + "bool_not_impl", + "bool_or_impl", + "bool_to_felt252", + "bool_xor_impl", + "branch_align", + "call_contract_syscall", + "class_hash_const", + "class_hash_to_felt252", + "class_hash_try_from_felt252", + "contract_address_const", + "contract_address_to_felt252", + "contract_address_try_from_felt252", + "deploy_syscall", + "disable_ap_tracking", + "drop", + "dup", + "ec_neg", + "ec_point_from_x_nz", + "ec_point_is_zero", + "ec_point_try_new_nz", + "ec_point_unwrap", + "ec_point_zero", + "ec_state_add", + "ec_state_add_mul", + "ec_state_init", + "ec_state_try_finalize_nz", + "emit_event_syscall", + "enable_ap_tracking", + "enum_init", + "enum_match", + "enum_snapshot_match", + "felt252_add", + "felt252_const", + "felt252_dict_entry_finalize", + "felt252_dict_entry_get", + "felt252_dict_new", + "felt252_dict_squash", + "felt252_div", + "felt252_is_zero", + "felt252_mul", + "felt252_sub", + "finalize_locals", + "function_call", + "get_builtin_costs", + "get_execution_info_syscall", + "into_box", + "jump", + "library_call_syscall", + "match_nullable", + "null", + "nullable_from_box", + "pedersen", + "rename", + "replace_class_syscall", + "revoke_ap_tracking", + "send_message_to_l1_syscall", + "snapshot_take", + "storage_address_from_base", + "storage_address_from_base_and_offset", + "storage_address_to_felt252", + "storage_address_try_from_felt252", + "storage_base_address_const", + "storage_base_address_from_felt252", + "storage_read_syscall", + "storage_write_syscall", + "store_local", + "store_temp", + "struct_construct", + "struct_deconstruct", + "struct_snapshot_deconstruct", + "u128_const", + "u128_eq", + "u128_is_zero", + "u128_overflowing_add", + "u128_overflowing_sub", + "u128_safe_divmod", + "u128_to_felt252", + "u128_guarantee_mul", + "u128_mul_guarantee_verify", + "u128s_from_felt252", + "u16_const", + "u16_eq", + "u16_is_zero", + "u16_overflowing_add", + "u16_overflowing_sub", + "u16_safe_divmod", + "u16_to_felt252", + "u16_try_from_felt252", + "u16_wide_mul", + "u32_const", + "u32_eq", + "u32_is_zero", + "u32_overflowing_add", + "u32_overflowing_sub", + "u32_safe_divmod", + "u32_to_felt252", + "u32_try_from_felt252", + "u32_wide_mul", + "u64_const", + "u64_eq", + "u64_is_zero", + "u64_overflowing_add", + "u64_overflowing_sub", + "u64_safe_divmod", + "u64_to_felt252", + "u64_try_from_felt252", + "u64_wide_mul", + "u8_const", + "u8_eq", + "u8_is_zero", + "u8_overflowing_add", + "u8_overflowing_sub", + "u8_safe_divmod", + "u8_to_felt252", + "u8_try_from_felt252", + "u8_wide_mul", + "unbox", + "unwrap_non_zero", + "withdraw_gas", + "withdraw_gas_all" + ] } diff --git a/src/starkware/starknet/definitions/constants.py b/src/starkware/starknet/definitions/constants.py index 6f5f6ebb..22d61684 100644 --- a/src/starkware/starknet/definitions/constants.py +++ b/src/starkware/starknet/definitions/constants.py @@ -71,7 +71,7 @@ ) # Sierra -> Casm compilation version. -SIERRA_VERSION = [1, 0, 0] +SIERRA_VERSION = [1, 1, 0] # The version of contract class leaf. CONTRACT_CLASS_LEAF_VERSION: bytes = b"CONTRACT_CLASS_LEAF_V0" diff --git a/src/starkware/starknet/definitions/general_config.yml b/src/starkware/starknet/definitions/general_config.yml index cc711af1..dfa77ff1 100644 --- a/src/starkware/starknet/definitions/general_config.yml +++ b/src/starkware/starknet/definitions/general_config.yml @@ -7,9 +7,9 @@ event_commitment_tree_height: 64 global_state_commitment_tree_height: 251 invoke_tx_max_n_steps: 1000000 min_gas_price: 100000000000 -sequencer_address: "0x4035e7e01f09b8bc746a549fdf1bafa8d975bdbc53d25646635d0ab58425f30" +sequencer_address: "0xedad892bb8c0cd384ff67c0cfabed06ccef6216a1a4ae553a260fb30b03826" starknet_os_config: chain_id: 1536727068981429685321 # StarknetChainId.TESTNET.value - fee_token_address: "0x3d946ac56929feafba12eee7bf5d07e0416a710087fdcb3a224d53b4e56f6cb" + fee_token_address: "0x168d0cad4a83436210cd43fcf842270762fbca1d55abf291c929b5fab828ed9" tx_commitment_tree_height: 64 validate_max_n_steps: 1000000