Skip to content

Commit

Permalink
Cairo v0.11.2a0 (pre).
Browse files Browse the repository at this point in the history
  • Loading branch information
liorgold2 committed May 24, 2023
1 parent 4040487 commit 00ee901
Show file tree
Hide file tree
Showing 15 changed files with 253 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down Expand Up @@ -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}
```

8 changes: 5 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/starkware/cairo/lang/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.1.1
0.11.2a0
2 changes: 1 addition & 1 deletion src/starkware/cairo/lang/create_cairo_lang_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion src/starkware/cairo/lang/ide/vscode-cairo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
69 changes: 63 additions & 6 deletions src/starkware/cairo/lang/vm/cairo_pie.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
36 changes: 34 additions & 2 deletions src/starkware/cairo/lang/vm/cairo_pie_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import io
import random
from typing import Dict
from typing import Dict, Mapping

import pytest

Expand All @@ -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


Expand Down Expand Up @@ -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)
11 changes: 8 additions & 3 deletions src/starkware/cairo/lang/vm/memory_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def __init__(self, addr, old_value, new_value):
]
]

RelocateValueFunc = Callable[[MaybeRelocatable], MaybeRelocatable]


class MemoryDict:
"""
Expand Down Expand Up @@ -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()
)

Expand Down
2 changes: 1 addition & 1 deletion src/starkware/cairo/vars.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"],
)
2 changes: 1 addition & 1 deletion src/starkware/starknet/compiler/v1/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Loading

0 comments on commit 00ee901

Please sign in to comment.