Skip to content

Commit

Permalink
Cairo v0.13.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware committed Aug 18, 2024
1 parent 0e4dab8 commit 4ea4fe8
Show file tree
Hide file tree
Showing 32 changed files with 285 additions and 142 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ http_archive(
"//src/starkware/starknet/compiler/v1:BUILD." + CAIRO_COMPILER_ARCHIVE,
),
strip_prefix = "cairo",
url = "https://github.com/starkware-libs/cairo/releases/download/v2.7.0-rc.1/release-x86_64-unknown-linux-musl.tar.gz",
url = "https://github.com/starkware-libs/cairo/releases/download/v2.7.0/release-x86_64-unknown-linux-musl.tar.gz",
)

http_archive(
Expand Down
2 changes: 1 addition & 1 deletion scripts/requirements-gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fastecdsa
frozendict
gprof2dot
lark
marshmallow-dataclass>=7.1.0
marshmallow-dataclass==8.6.1
marshmallow-enum
marshmallow-oneofschema
marshmallow>=3.2.1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
"0x1000a7b3fbe5305ae59f9298ce57619cf9914d810ba876a2c30fc6145904ce2",
"0x32daa3bd81bfc6aba263401761e78ae4a21b63161d6fc74bd4482b11fbdac6",
"0x268e762c9153438f6a9203036739502104708b2f100987927fae7d563e027fb",
"0x7c37f4bb5e5f73b62939127cf3669f3cc5a96eb0fd0f30d5642bce1ce1c9895",
"0x29fa47f641f1d0112ca6c42bc10f31b3539d688a7f8008e750eae1c6dd7e7a",
"0x193641eb151b0f41674641089952e60bc3aded26e3cf42793655c562b8c3aa0",
"0x14c11937eeaf285502dec0e2db494708679fa51c4229f4cad2682344eae6935",
"0x10d4339b5fb9134112e8c8d21b2b800414f2aebbf595dc2a1e1db0ad7c1f11d",
"0x4d817d698a34fa79d97cccf5ba2707861a94b766d749b0dd47203018a43a112",
"0x2a258bceea610b1008bd183365df7809688034a927fabcaff85ff63bb956f09",
"0x672ad4caa2ed1d0c3b4954b32eaf51d867203a66920d8ee6b7455b3989f6a86",
"0x2824b7ddb10287dd935d6e67fb846f32fec9bacb34fea4d2246dce395d4b580",
"0x4f25ecc7f3184b4b5fe3bb34e6d553c1e477ff07c3efa64c433815fbdfb51a8",
"0x7fb130c1bf059a1fa742f84877872ced1b0cb2c0f3b5d1fd63435946c4b11fd"
]
4 changes: 2 additions & 2 deletions src/starkware/cairo/common/cairo_sha256/sha256_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,15 @@ func finalize_sha256{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}(
sha2_compress_function,
)

number_of_missing_blocks = -ids.n % ids.BATCH_SIZE
number_of_missing_blocks = (-ids.n) % ids.BATCH_SIZE
assert 0 <= number_of_missing_blocks < 20
_sha256_input_chunk_size_felts = ids.SHA256_INPUT_CHUNK_SIZE_FELTS
assert 0 <= _sha256_input_chunk_size_felts < 100

message = [0] * _sha256_input_chunk_size_felts
w = compute_message_schedule(message)
output = sha2_compress_function(IV, w)
padding = (message + IV + output) * (number_of_missing_blocks)
padding = (message + IV + output) * number_of_missing_blocks
segments.write_arg(ids.sha256_ptr_end, padding)
%}

Expand Down
2 changes: 2 additions & 0 deletions src/starkware/cairo/common/ec.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func ec_op{ec_op_ptr: EcOpBuiltin*}(p: EcPoint, m: felt, q: EcPoint) -> (r: EcPo
seed = b"".join(map(to_bytes, [ids.p.x, ids.p.y, ids.m, ids.q.x, ids.q.y]))
ids.s.x, ids.s.y = random_ec_point(FIELD_PRIME, ALPHA, BETA, seed)
%}
assert_on_curve(s);
let p_plus_s: EcPoint = ec_add(p, s);

assert ec_op_ptr.p = p_plus_s;
Expand Down Expand Up @@ -217,6 +218,7 @@ func chained_ec_op{ec_op_ptr: EcOpBuiltin*}(p: EcPoint, m: felt*, q: EcPoint*, l
)
ids.s.x, ids.s.y = random_ec_point(FIELD_PRIME, ALPHA, BETA, seed)
%}
assert_on_curve(s);
let p_plus_s: EcPoint = ec_add(p, s);
let r_plus_s: EcPoint = _chained_ec_op_inner(p=p_plus_s, m=m, q=q, len=len);
let r: EcPoint = ec_add(r_plus_s, EcPoint(x=s.x, y=-s.y));
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.13.2a0
0.13.2
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,6 @@ def initialize_segments(self, runner):
def finalize_segments(self, runner):
super().finalize_segments(runner)

def get_additional_data(self):
assert self.zero_value is not None, "Uninitialized self.zero_value."
return [list(RelocatableValue.to_tuple(self.zero_value))]

def extend_additional_data(self, data, relocate_callback, data_is_trusted=True):
self.zero_value = relocate_callback(RelocatableValue.from_tuple(data[0]))

# The structure of the values in the returned dictionary is of the form:
# {keys = INPUT_NAMES, "batch": {index_in_batch: {keys = MEMORY_VAR_NAMES}}}.
def air_private_input(self, runner) -> Dict[str, Any]:
Expand Down
11 changes: 8 additions & 3 deletions src/starkware/cairo/lang/ide/vscode-cairo/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Installation
# CairoZero for Visual Studio Code

This extension provides support for the older version of Cairo, known as CairoZero. For the new language, install the [Cairo 1.0](https://marketplace.visualstudio.com/items?itemName=starkware.cairo1) extension.
Note that the CairoZero compiler and extension are no longer maintained. This extension should only be used by projects that still rely on CairoZero code.

## Installation

From the directory of this file, run:
```
Expand All @@ -8,7 +13,7 @@ vsce package
code --install-extension cairo*.vsix
```

# Configuration
## Configuration

If you have `cairo-format` installed globally (available in PATH), the value of
`cairo.cairoFormatPath` should be `cairo-format` (the default).
Expand All @@ -19,7 +24,7 @@ set the value of `cairo.cairoFormatPath` to
${workspaceFolder}/src/starkware/cairo/lang/scripts/cairo-format
```

# Run the extension (for development)
## Run the extension (for development)

1. Open VSCode in the directory of the extension.
2. Run:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
"name": "Cairo",
"name": "CairoZero",
"patterns": [
{
"name": "meta.control.if",
Expand All @@ -13,11 +13,11 @@
"name": "entity.name.condition"
}
},
"contentName": "source.cairo",
"contentName": "source.cairo0",
"end": "\\}",
"patterns": [
{
"include": "source.cairo"
"include": "source.cairo0"
}
],
"endCaptures": {
Expand All @@ -37,11 +37,11 @@
"name": "entity.name.identifiers"
}
},
"contentName": "source.cairo",
"contentName": "source.cairo0",
"end": "\\}",
"patterns": [
{
"include": "source.cairo"
"include": "source.cairo0"
}
],
"endCaptures": {
Expand All @@ -61,11 +61,11 @@
"name": "entity.name.function"
}
},
"contentName": "source.cairo",
"contentName": "source.cairo0",
"end": "\\}",
"patterns": [
{
"include": "source.cairo"
"include": "source.cairo0"
}
],
"endCaptures": {
Expand Down Expand Up @@ -113,11 +113,11 @@
"name": "entity.name.function"
}
},
"contentName": "source.cairo",
"contentName": "source.cairo0",
"end": "\\}",
"patterns": [
{
"include": "source.cairo"
"include": "source.cairo0"
}
],
"endCaptures": {
Expand All @@ -137,11 +137,11 @@
"name": "entity.name.function"
}
},
"contentName": "source.cairo",
"contentName": "source.cairo0",
"end": "\\}",
"patterns": [
{
"include": "source.cairo"
"include": "source.cairo0"
}
],
"endCaptures": {
Expand Down Expand Up @@ -191,5 +191,5 @@
]
}
],
"scopeName": "source.cairo"
"scopeName": "source.cairo0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,14 @@ async def update(
nonce=nonce,
)

async def fetch_witnesses(self, ffc: FactFetchingContext, updates: Mapping[int, int]):
async def fetch_storage_witnesses(
self, ffc: FactFetchingContext, updates: Mapping[int, int], get_leaves: bool
):
await self.storage_commitment_tree.fetch_witnesses(
ffc=ffc, sorted_leaf_indices=sorted(updates.keys()), fact_cls=StorageLeaf
ffc=ffc,
sorted_leaf_indices=sorted(updates.keys()),
fact_cls=StorageLeaf if get_leaves else None,
empty_leaf=StorageLeaf.empty(),
)


Expand Down
61 changes: 36 additions & 25 deletions src/starkware/starknet/business_logic/fact_state/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,7 @@ async def empty(cls, ffc: FactFetchingContext, general_config: Config) -> "Share
return cls(
contract_states=empty_contract_states,
contract_classes=empty_contract_classes,
block_info=BlockInfo.empty(
sequencer_address=general_config.sequencer_address,
use_kzg_da=general_config.use_kzg_da,
),
block_info=BlockInfo.empty(sequencer_address=general_config.sequencer_address),
)

async def get_contract_class_tree(
Expand Down Expand Up @@ -388,7 +385,12 @@ async def apply_updates(
block_info=block_info,
)

async def fetch_witnesses(self, cached_ffc: FactFetchingContext, state_diff: "StateDiff"):
async def fetch_all_witnesses(
self,
cached_ffc: FactFetchingContext,
state_diff: "StateDiff",
get_leaves: bool,
):
"""
Fetches the necessary witnesses from storage to update the state with the provided diff.
"""
Expand All @@ -400,34 +402,42 @@ async def fetch_witnesses(self, cached_ffc: FactFetchingContext, state_diff: "St
accessed_addresses = (
address_to_class_hash.keys() | address_to_nonce.keys() | storage_updates.keys()
)
current_contract_states = await self.contract_states.get_leaves(
ffc=cached_ffc, indices=accessed_addresses, fact_cls=ContractState

# Fetch global tree's witnesses.
contract_states = await self.contract_states.fetch_witnesses(
ffc=cached_ffc,
sorted_leaf_indices=sorted(accessed_addresses),
fact_cls=ContractState,
empty_leaf=await ContractState.empty(
storage_commitment_tree_height=constants.CONTRACT_STATES_COMMITMENT_TREE_HEIGHT,
ffc=cached_ffc,
),
)

# Fetch classes tree's witnesses.
classes_tree_awaitable = (
[
self.contract_classes.fetch_witnesses(
ffc=cached_ffc,
sorted_leaf_indices=sorted(class_hash_to_compiled_class_hash.keys()),
fact_cls=ContractClassLeaf if get_leaves else None,
empty_leaf=ContractClassLeaf.empty(),
)
]
if self.contract_classes is not None
else []
)

await gather_in_chunks(
awaitables=[
current_contract_states[address].fetch_witnesses(ffc=cached_ffc, updates=updates)
contract_states[address].fetch_storage_witnesses(
ffc=cached_ffc, updates=updates, get_leaves=get_leaves
)
for address, updates in storage_updates.items()
]
+ classes_tree_awaitable
)

# Fetch global tree's witnesses.
await self.contract_states.fetch_witnesses(
ffc=cached_ffc, sorted_leaf_indices=sorted(accessed_addresses), fact_cls=ContractState
)

# Fetch classes tree's witnesses.
if self.contract_classes is not None:
await self.contract_classes.fetch_witnesses(
ffc=cached_ffc,
sorted_leaf_indices=sorted(class_hash_to_compiled_class_hash.keys()),
fact_cls=ContractClassLeaf,
)
else:
assert (
len(class_hash_to_compiled_class_hash) == 0
), "contract_classes must be concrete before fetch."


@marshmallow_dataclass.dataclass(frozen=True)
class StateDiff(EverestStateDiff, DBObject):
Expand All @@ -445,6 +455,7 @@ class StateDiff(EverestStateDiff, DBObject):
declared_classes: Mapping[int, int] = field(
metadata=fields.state_diff_declared_classes_metadata
)
# The block after this StateDiff is applied.
block_info: BlockInfo

@marshmallow.pre_load
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def rename_old_gas_price_fields(
return rename_old_gas_price_fields(data=data)

@classmethod
def empty(cls, sequencer_address: Optional[int], use_kzg_da: bool) -> "BlockInfo":
def empty(cls, sequencer_address: Optional[int]) -> "BlockInfo":
"""
Returns an empty BlockInfo object; i.e., the one before the first in the chain.
"""
Expand All @@ -117,7 +117,7 @@ def empty(cls, sequencer_address: Optional[int], use_kzg_da: bool) -> "BlockInfo
l1_data_gas_price=ResourcePrice(price_in_wei=1, price_in_fri=1),
sequencer_address=sequencer_address,
starknet_version=STARKNET_VERSION,
use_kzg_da=use_kzg_da,
use_kzg_da=True, # Value is ignored for an empty block.
)

@classmethod
Expand Down
7 changes: 4 additions & 3 deletions src/starkware/starknet/common/new_syscalls.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from starkware.cairo.common.cairo_secp.ec import EcPoint
from starkware.cairo.common.sha256_state import Sha256Input, Sha256State
from starkware.cairo.common.uint256 import Uint256

// Syscall selectors.
Expand Down Expand Up @@ -169,8 +170,8 @@ struct KeccakRequest {
}

struct Sha256ProcessBlockRequest {
state_ptr: felt*,
input_start: felt*,
state_ptr: Sha256State*,
input_start: Sha256Input*,
}

struct SecpAddRequest {
Expand Down Expand Up @@ -262,7 +263,7 @@ struct KeccakResponse {
}

struct Sha256ProcessBlockResponse {
state_ptr: felt*,
state_ptr: Sha256State*,
}

struct SecpGetXyResponse {
Expand Down
11 changes: 7 additions & 4 deletions src/starkware/starknet/compiler/external_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
)
from starkware.starknet.definitions import constants
from starkware.starknet.public.abi import DEFAULT_ENTRY_POINT_NAME, DEFAULT_L1_ENTRY_POINT_NAME
from starkware.starknet.services.api.contract_class.contract_class import SUPPORTED_BUILTINS
from starkware.starknet.services.api.contract_class.contract_class import CAIRO0_SUPPORTED_BUILTINS

RAW_INPUT_DECORATOR = "raw_input"
RAW_OUTPUT_DECORATOR = "raw_output"
Expand All @@ -67,7 +67,7 @@
WRAPPER_SCOPE = ScopedName.from_string("__wrappers__")

BUILTIN_PTR_TO_BUILTIN = {
f"{builtin_name}_ptr": builtin_name for builtin_name in SUPPORTED_BUILTINS
f"{builtin_name}_ptr": builtin_name for builtin_name in CAIRO0_SUPPORTED_BUILTINS
}


Expand Down Expand Up @@ -330,9 +330,12 @@ def validate_raw_output_signature(self, elm: CodeElementFunction):

def get_collected_builtins(self):
"""
Returns the builtins collected from the external functions, ordered by SUPPORTED_BUILTINS.
Returns the builtins collected from the external functions, ordered by
CAIRO0_SUPPORTED_BUILTINS.
"""
return [builtin for builtin in SUPPORTED_BUILTINS if builtin in self.collected_builtins]
return [
builtin for builtin in CAIRO0_SUPPORTED_BUILTINS if builtin in self.collected_builtins
]


class PreExternalWrapperStage(Stage):
Expand Down
2 changes: 1 addition & 1 deletion src/starkware/starknet/compiler/external_wrapper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_wrapper_with_return_values(builtins_directive: bool):
"""
Tests that the external wrapper works with return values.
In addition, also tests that the builtins are collected correctly and reordered according to
SUPPORTED_BUILTINS.
CAIRO0_SUPPORTED_BUILTINS.
"""
program = preprocess_str(
f"""
Expand Down
Loading

0 comments on commit 4ea4fe8

Please sign in to comment.