Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cairo1 input output cairo0 controlled #7

Merged
merged 13 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: '3.9'


- name: Setup Scarb
uses: software-mansion/setup-scarb@v1

- name: Install
run: |
python -m pip install --upgrade pip
pip install colorama
python install.py

- name: Bootloader & Cairo1 pie - Compile
- name: Compile Bootloader
run: |
python compile.py

Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,8 @@ cython_debug/
bootloader*.json
!bootloader_input.json
*.memory
*.trace
*.trace

# Added by cargo

/target
4 changes: 0 additions & 4 deletions .gitmodules

This file was deleted.

1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scarb 2.6.3
15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[workspace]
resolver = "2"
members = ["runner"]

[workspace.package]
edition = "2021"
version = "0.1.0"

[workspace.dependencies]
cairo-lang-compiler = { version = "2.6.3", default-features = false }
cairo-lang-sierra = { version = "2.6.3", default-features = false }
cairo-vm = { git = "https://github.com/Okm165/cairo-vm.git", branch = "cairo1-cairo0bootloader"}
cairo1-run = { git = "https://github.com/Okm165/cairo-vm.git", branch = "cairo1-cairo0bootloader"}
clap = { version = "4.3.10", features = ["derive"] }
serde_json = "1"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To set up the project environment and run the bootloader, follow these steps:
git submodule update --init
```

1. **Setup Python Environment**: Ensure you have a Python 3.9.0 environment set up & `pip install colorama` for pretty outputs.
1. **Setup Python Environment**: Ensure you have a Python 3.9.0 environment set up.

2. **Installation**: Run `python install.py` to install the necessary dependencies and set up the project.

Expand Down
4 changes: 2 additions & 2 deletions bootloader_input.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"tasks": [
{
"type": "CairoPiePath",
"path": "cairo1_pie.zip",
"type": "CairoSierra",
"path": "cairo1/target/dev/example.sierra.json",
"use_poseidon": true
}
],
Expand Down
1 change: 0 additions & 1 deletion cairo-vm
Submodule cairo-vm deleted from b72f5a
97 changes: 0 additions & 97 deletions cairo/bootloader/objects.py

This file was deleted.

File renamed without changes.
File renamed without changes.
177 changes: 177 additions & 0 deletions cairo0-bootloader/bootloader/objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import os
import tempfile
import subprocess
import dataclasses
from abc import abstractmethod
from dataclasses import field
from typing import ClassVar, Dict, List, Optional, Type

import marshmallow
import marshmallow.fields as mfields
import marshmallow_dataclass
from marshmallow_oneofschema import OneOfSchema

from starkware.cairo.lang.compiler.program import Program, ProgramBase, StrippedProgram
from starkware.cairo.lang.vm.cairo_pie import CairoPie
from starkware.starkware_utils.marshmallow_dataclass_fields import additional_metadata
from starkware.starkware_utils.validated_dataclass import ValidatedMarshmallowDataclass


class TaskSpec(ValidatedMarshmallowDataclass):
"""
Contains task's specification.
"""

@abstractmethod
def load_task(self, memory=None, args_start=None, args_len=None) -> "Task":
"""
Returns the corresponding task.
"""


class Task:
@abstractmethod
def get_program(self) -> ProgramBase:
"""
Returns the task's Cairo program.
"""


@marshmallow_dataclass.dataclass(frozen=True)
class RunProgramTask(TaskSpec, Task):
TYPE: ClassVar[str] = "RunProgramTask"
program: Program
program_input: dict
use_poseidon: bool

def get_program(self) -> Program:
return self.program

def load_task(self, memory=None, args_start=None, args_len=None) -> "Task":
return self


@marshmallow_dataclass.dataclass(frozen=True)
class CairoPiePath(TaskSpec):
TYPE: ClassVar[str] = "CairoPiePath"
path: str
use_poseidon: bool

def load_task(self, memory=None, args_start=None, args_len=None) -> "CairoPieTask":
"""
Loads the PIE to memory.
"""
return CairoPieTask(
cairo_pie=CairoPie.from_file(self.path), use_poseidon=self.use_poseidon
)


@marshmallow_dataclass.dataclass(frozen=True)
class Cairo1ProgramPath(TaskSpec):
TYPE: ClassVar[str] = "Cairo1ProgramPath"
path: str
use_poseidon: bool

def load_task(self, memory=None, args_start=None, args_len=None) -> "CairoPieTask":
"""
Builds and Loads the PIE to memory.
"""
with tempfile.NamedTemporaryFile() as cairo_pie_file:
cairo_pie_file_path = cairo_pie_file.name

args = [memory[args_start.address_ + i] for i in range(args_len)]
formatted_args = f'[{" ".join(map(str, args))}]'

subprocess.run(
[
"cairo1-run",
self.path,
"--layout",
"all_cairo",
"--args",
formatted_args,
"--cairo_pie_output",
cairo_pie_file_path,
"--append_return_values",
],
check=True,
)

return CairoPieTask(
cairo_pie=CairoPie.from_file(cairo_pie_file_path),
use_poseidon=self.use_poseidon,
)

@marshmallow_dataclass.dataclass(frozen=True)
class CairoSierra(TaskSpec):
TYPE: ClassVar[str] = "CairoSierra"
path: str
use_poseidon: bool

def load_task(self, memory=None, args_start=None, args_len=None) -> "CairoPieTask":
"""
Builds and Loads the PIE to memory.
"""
with tempfile.NamedTemporaryFile() as cairo_pie_file:
cairo_pie_file_path = cairo_pie_file.name

args = [memory[args_start.address_ + i] for i in range(args_len)]
formatted_args = f'[{" ".join(map(str, args))}]'

subprocess.run(
[
"runner",
"--sierra_program",
self.path,
"--args",
formatted_args,
"--cairo_pie_output",
cairo_pie_file_path,
],
check=True,
)

return CairoPieTask(
cairo_pie=CairoPie.from_file(cairo_pie_file_path),
use_poseidon=self.use_poseidon,
)


class TaskSchema(OneOfSchema):
"""
Schema for Task/CairoPiePath/Cairo1ProgramPath/CairoSierra
OneOfSchema adds a "type" field.
"""

type_schemas: Dict[str, Type[marshmallow.Schema]] = {
RunProgramTask.TYPE: RunProgramTask.Schema,
CairoPiePath.TYPE: CairoPiePath.Schema,
Cairo1ProgramPath.TYPE: Cairo1ProgramPath.Schema,
CairoSierra.TYPE: CairoSierra.Schema,
}

def get_obj_type(self, obj):
return obj.TYPE


@dataclasses.dataclass(frozen=True)
class CairoPieTask(Task):
cairo_pie: CairoPie
use_poseidon: bool

def get_program(self) -> StrippedProgram:
return self.cairo_pie.program


@marshmallow_dataclass.dataclass(frozen=True)
class SimpleBootloaderInput(ValidatedMarshmallowDataclass):
tasks: List[TaskSpec] = field(
metadata=additional_metadata(
marshmallow_field=mfields.List(mfields.Nested(TaskSchema))
)
)
fact_topologies_path: Optional[str]

# If true, the bootloader will put all the outputs in a single page, ignoring the
# tasks' fact topologies.
single_page: bool
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func execute_task{builtin_ptrs: BuiltinData*, self_range_check_ptr}(
program_address, program_data_size = load_program(
task=task, memory=memory, program_header=ids.program_header,
builtins_offset=ids.ProgramHeader.builtin_list)
segments.finalize(program_data_base.segment_index, program_data_size)
segments.finalize(program_data_base.segment_index, program_data_size+1)
%}

// Verify that the bootloader version is compatible with the bootloader.
Expand Down
Loading
Loading