Skip to content

Commit

Permalink
Cairo v0.13.2a0 (pre).
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware committed Jul 11, 2024
1 parent efa9648 commit 0e4dab8
Show file tree
Hide file tree
Showing 203 changed files with 31,578 additions and 18,478 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.13.1)
[github](https://github.com/starkware-libs/cairo-lang/releases/tag/v0.13.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.13.1.zip .
> docker cp ${container_id}:/app/cairo-lang-0.13.2.zip .
> docker rm -v ${container_id}
```

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.6.0/release-x86_64-unknown-linux-musl.tar.gz",
url = "https://github.com/starkware-libs/cairo/releases/download/v2.7.0-rc.1/release-x86_64-unknown-linux-musl.tar.gz",
)

http_archive(
Expand Down
2 changes: 1 addition & 1 deletion src/services/config/general_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from starkware.starkware_utils.config_base import Config


@marshmallow_dataclass.dataclass(frozen=True)
@marshmallow_dataclass.dataclass
class GeneralConfigBase(Config):
pass
10 changes: 9 additions & 1 deletion src/services/everest/definitions/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
from starkware.starkware_utils.error_handling import StarkErrorCode
from starkware.starkware_utils.field_validators import validate_non_negative
from starkware.starkware_utils.marshmallow_dataclass_fields import StrictRequiredInteger
from starkware.starkware_utils.validated_fields import RangeValidatedField, ValidatedField
from starkware.starkware_utils.validated_fields import (
OptionalField,
RangeValidatedField,
ValidatedField,
)

# Fields data: validation data, dataclass metadata.
tx_id_marshmallow_field = StrictRequiredInteger(validate=validate_non_negative("tx_id"))
Expand Down Expand Up @@ -74,6 +78,8 @@ def convert_valid_to_checksum(self, value: str) -> ChecksumAddress:
formatter=None,
)

EthAddressHexField = dataclasses.replace(EthAddressIntField, formatter=hex)

FeltField = RangeValidatedField(
lower_bound=0,
upper_bound=FIELD_PRIME,
Expand All @@ -82,6 +88,8 @@ def convert_valid_to_checksum(self, value: str) -> ChecksumAddress:
formatter=hex,
)

OptionalFeltField = OptionalField(field=FeltField, none_probability=0)

felt_as_hex_list_metadata = dict(marshmallow_field=mfields.List(FeltField.get_marshmallow_field()))


Expand Down
2 changes: 1 addition & 1 deletion src/services/everest/definitions/general_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from services.config.general_config import GeneralConfigBase


@marshmallow_dataclass.dataclass(frozen=True)
@marshmallow_dataclass.dataclass
class EverestGeneralConfig(GeneralConfigBase):
pass
20 changes: 19 additions & 1 deletion src/starkware/cairo/bootloaders/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("//bazel_utils:get_from_cairo_lang.bzl", "get_from_cairo_lang")
load("//bazel_utils/python:defs.bzl", "requirement")
load("//bazel_utils:python.bzl", "py_exe")
load("//bazel_utils:python.bzl", "py_exe", "pytest_test")

py_library(
name = "program_hash_test_utils_lib",
Expand Down Expand Up @@ -58,6 +58,7 @@ py_library(
],
visibility = ["//visibility:public"],
deps = [
":aggregator_utils",
":cairo_bootloader_fact_topology_lib",
":cairo_hash_program_lib",
"//src/starkware/cairo/lang/vm:cairo_relocatable_lib",
Expand All @@ -66,3 +67,20 @@ py_library(
requirement("eth_hash"),
],
)

py_library(
name = "aggregator_utils",
srcs = [
"aggregator_utils.py",
],
deps = [
"//src/starkware/cairo/lang/vm:cairo_vm_crypto_lib",
"//src/starkware/python:starkware_python_utils_lib",
],
)

pytest_test(
name = "aggregator_utils_test",
srcs = ["aggregator_utils_test.py"],
deps = [":aggregator_utils"],
)
28 changes: 28 additions & 0 deletions src/starkware/cairo/bootloaders/aggregator_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import List

from starkware.cairo.lang.vm.crypto import pedersen_hash
from starkware.python.utils import from_bytes


def get_aggregator_input_size(program_output: List[int]) -> int:
"""
Returns the size of the input to an aggregator program.
"""
assert len(program_output) > 0, "Invalid program output for an aggregator program."
n_tasks = program_output[0]

offset = 1
for _ in range(n_tasks):
assert len(program_output) > offset, "Invalid program output for an aggregator program."
task_size = program_output[offset]
offset += task_size

return offset


def add_aggregator_prefix(program_hash: int) -> int:
"""
Computes H("AGGREGATOR", program_hash) which indicates that the program was treated by the
bootloader as an aggregator program.
"""
return pedersen_hash(from_bytes(b"AGGREGATOR"), program_hash)
10 changes: 10 additions & 0 deletions src/starkware/cairo/bootloaders/aggregator_utils_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from starkware.cairo.bootloaders.aggregator_utils import get_aggregator_input_size


def test_get_aggregator_input_size():
# Create an aggregator input (bootloader output) with 2 tasks with output sizes 4 and 3.
aggregator_input = [2] + [4, 100, 100, 100] + [3, 100, 100]
aggregator_output = [100, 100, 100, 100]
assert get_aggregator_input_size(program_output=aggregator_input + aggregator_output) == len(
aggregator_input
)
22 changes: 22 additions & 0 deletions src/starkware/cairo/bootloaders/applicative_bootloader/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("//src/starkware/cairo/lang:cairo_rules.bzl", "cairo_binary", "cairo_library")

cairo_library(
name = "applicative_bootloader_lib",
srcs = [
"applicative_bootloader.cairo",
],
deps = ["//src/starkware/cairo/bootloaders/bootloader:bootloader_lib"],
)

cairo_binary(
name = "applicative_bootloader_program",
cairoopts = [
"--debug_info_with_source",
"--proof_mode",
],
compiled_program_name = "applicative_bootloader_compiled.json",
main = "applicative_bootloader.cairo",
deps = [":applicative_bootloader_lib"],
)

package(default_visibility = ["//visibility:public"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
%builtins output pedersen range_check ecdsa bitwise ec_op keccak poseidon range_check96 add_mod mul_mod

from starkware.cairo.bootloaders.bootloader.run_bootloader import run_bootloader
from starkware.cairo.bootloaders.simple_bootloader.run_simple_bootloader import (
run_simple_bootloader,
verify_non_negative,
)
from starkware.cairo.common.cairo_builtins import HashBuiltin, PoseidonBuiltin
from starkware.cairo.common.hash import hash2
from starkware.cairo.common.memcpy import memcpy
from starkware.cairo.common.registers import get_fp_and_pc

const AGGREGATOR_CONSTANT = 'AGGREGATOR';

func main{
output_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
ecdsa_ptr,
bitwise_ptr,
ec_op_ptr,
keccak_ptr,
poseidon_ptr: PoseidonBuiltin*,
range_check96_ptr,
add_mod_ptr,
mul_mod_ptr,
}() {
ap += SIZEOF_LOCALS;
local task_range_check_ptr;

// A pointer to the aggregator's task output.
local aggregator_output_ptr: felt*;
%{
from starkware.cairo.bootloaders.applicative_bootloader.objects import (
ApplicativeBootloaderInput,
)
from starkware.cairo.bootloaders.simple_bootloader.objects import SimpleBootloaderInput

# Create a segment for the aggregator output.
ids.aggregator_output_ptr = segments.add()

# Load the applicative bootloader input and the aggregator task.
applicative_bootloader_input = ApplicativeBootloaderInput.Schema().load(program_input)
aggregator_task = applicative_bootloader_input.aggregator_task.load_task()

# Create the simple bootloader input.
simple_bootloader_input = SimpleBootloaderInput(
tasks=[aggregator_task], fact_topologies_path=None, single_page=False
)

# Change output builtin state to a different segment in preparation for running the
# aggregator task.
applicative_output_builtin_state = output_builtin.get_state()
output_builtin.new_state(base=ids.aggregator_output_ptr)
%}

// Save aggregator output start.
let aggregator_output_start: felt* = aggregator_output_ptr;

// Execute the simple bootloader with the aggregator task.
run_simple_bootloader{output_ptr=aggregator_output_ptr}();
local aggregator_output_end: felt* = aggregator_output_ptr;

// Check that exactly one task was executed.
assert aggregator_output_start[0] = 1;

// Extract the aggregator output size and program hash.
let aggregator_output_length = aggregator_output_start[1];
assert aggregator_output_length = aggregator_output_end - aggregator_output_start - 1;
let aggregator_program_hash = aggregator_output_start[2];
let aggregator_input_ptr = &aggregator_output_start[3];

// Allocate a segment for the bootloader output.
local bootloader_output_ptr: felt*;
%{
from starkware.cairo.bootloaders.bootloader.objects import BootloaderInput

# Save the aggregator's fact_topologies before running the bootloader.
aggregator_fact_topologies = fact_topologies
fact_topologies = []

# Create a segment for the bootloader output.
ids.bootloader_output_ptr = segments.add()

# Create the bootloader input.
bootloader_input = BootloaderInput(
tasks=applicative_bootloader_input.tasks,
fact_topologies_path=None,
bootloader_config=applicative_bootloader_input.bootloader_config,
packed_outputs=applicative_bootloader_input.packed_outputs,
single_page=True,
)

# Change output builtin state to a different segment in preparation for running the
# bootloader.
output_builtin.new_state(base=ids.bootloader_output_ptr)
%}

// Save the bootloader output start.
let bootloader_output_start = bootloader_output_ptr;

// Execute the bootloader.
run_bootloader{output_ptr=bootloader_output_ptr}();
local range_check_ptr = range_check_ptr;
local ecdsa_ptr = ecdsa_ptr;
local bitwise_ptr = bitwise_ptr;
local ec_op_ptr = ec_op_ptr;
local keccak_ptr = keccak_ptr;
local pedersen_ptr: HashBuiltin* = pedersen_ptr;
local poseidon_ptr: PoseidonBuiltin* = poseidon_ptr;
local range_check96_ptr = range_check96_ptr;
local add_mod_ptr = add_mod_ptr;
local mul_mod_ptr = mul_mod_ptr;
local bootloader_output_end: felt* = bootloader_output_ptr;

%{
# Restore the output builtin state.
output_builtin.set_state(applicative_output_builtin_state)
%}

// Output:
// * The aggregator program hash, hashed with the word "AGGREGATOR".
// * The bootloader config: the simple bootloader hash and the hash of the list of the Cairo
// verifiers.
let (modified_aggregator_program_hash) = hash2{hash_ptr=pedersen_ptr}(
AGGREGATOR_CONSTANT, aggregator_program_hash
);
local pedersen_ptr: HashBuiltin* = pedersen_ptr;
assert output_ptr[0] = modified_aggregator_program_hash;
// Copy the bootloader config.
assert output_ptr[1] = bootloader_output_start[0];
assert output_ptr[2] = bootloader_output_start[1];
let output_ptr = &output_ptr[3];
let output_start = output_ptr;

// Assert that the bootloader output agrees with the aggregator input.
let bootloader_tasks_output_ptr = &bootloader_output_start[2];
let bootloader_tasks_output_length = bootloader_output_end - bootloader_tasks_output_ptr;
memcpy(
dst=aggregator_input_ptr,
src=bootloader_tasks_output_ptr,
len=bootloader_tasks_output_length,
);

// Output the aggregated output.
let aggregated_output_ptr = aggregator_input_ptr + bootloader_tasks_output_length;
let aggregated_output_length = aggregator_output_end - aggregated_output_ptr;
memcpy(dst=output_ptr, src=aggregated_output_ptr, len=aggregated_output_length);
let output_ptr = output_ptr + aggregated_output_length;

%{
from starkware.cairo.bootloaders.fact_topology import FactTopology
from starkware.cairo.bootloaders.simple_bootloader.utils import (
configure_fact_topologies,
write_to_fact_topologies_file,
)

assert len(aggregator_fact_topologies) == 1
# Subtract the bootloader output length from the first page's length. Note that the
# bootloader output is always fully contained in the first page.
original_first_page_length = aggregator_fact_topologies[0].page_sizes[0]
first_page_length = original_first_page_length - ids.bootloader_tasks_output_length

# Update the first page's length to account for the bootloader output.
fact_topology = FactTopology(
tree_structure=aggregator_fact_topologies[0].tree_structure,
page_sizes=[first_page_length] + aggregator_fact_topologies[0].page_sizes[1:]
)

# Configure the memory pages in the output builtin, based on plain_fact_topologies.
configure_fact_topologies(
fact_topologies=[fact_topology], output_start=ids.output_start,
output_builtin=output_builtin,
)

# Dump fact topologies to a json file.
if applicative_bootloader_input.fact_topologies_path is not None:
write_to_fact_topologies_file(
fact_topologies_path=applicative_bootloader_input.fact_topologies_path,
fact_topologies=[fact_topology],
)
%}

return ();
}
13 changes: 11 additions & 2 deletions src/starkware/cairo/bootloaders/bootloader/BUILD
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
load("//src/starkware/cairo/lang:cairo_rules.bzl", "cairo_binary", "cairo_library")

cairo_library(
name = "bootloader_lib",
name = "run_bootloader_lib",
srcs = [
"bootloader.cairo",
"run_bootloader.cairo",
"//src/starkware/cairo/bootloaders/simple_bootloader:execute_task.cairo",
"//src/starkware/cairo/bootloaders/simple_bootloader:run_simple_bootloader.cairo",
"//src/starkware/cairo/builtin_selection:inner_select_builtins.cairo",
Expand All @@ -22,10 +22,19 @@ cairo_library(
"//src/starkware/cairo/common:memcpy.cairo",
"//src/starkware/cairo/common:poseidon_state.cairo",
"//src/starkware/cairo/common:registers.cairo",
"//src/starkware/cairo/common:sha256_state.cairo",
"//src/starkware/cairo/lang/compiler/lib:registers.cairo",
],
)

cairo_library(
name = "bootloader_lib",
srcs = [
"bootloader.cairo",
],
deps = [":run_bootloader_lib"],
)

cairo_binary(
name = "bootloader_program",
cairoopts = [
Expand Down
Loading

0 comments on commit 0e4dab8

Please sign in to comment.