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

Scarb bootloading #8

Merged
merged 6 commits into from
May 6, 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
4 changes: 3 additions & 1 deletion .github/workflows/run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ jobs:
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: Compile Bootloader
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,6 @@ bootloader*.json
*.memory
*.trace

cairo
corelib
# 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": "Cairo1ProgramPath",
"path": "cairo1.cairo",
"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
37 changes: 36 additions & 1 deletion cairo0-bootloader/bootloader/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,52 @@ def load_task(self, memory=None, args_start=None, args_len=None) -> "CairoPieTas
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.
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):
Expand Down
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
6 changes: 6 additions & 0 deletions cairo1/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "example"
version = "0.1.0"
8 changes: 8 additions & 0 deletions cairo1/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "example"
version = "0.1.0"
edition = "2023_11"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
9 changes: 1 addition & 8 deletions cairo1.cairo → cairo1/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
use core::{
hash::{HashStateTrait, HashStateExTrait, Hash},
integer::U128BitAnd,
pedersen::PedersenTrait,
};
use poseidon::{hades_permutation, poseidon_hash_span};

#[derive(Drop, Serde)]
struct Input {
a: u32,
Expand Down Expand Up @@ -32,4 +25,4 @@ fn main(input: Array<felt252>) -> Output {
b_2,
c_2,
}
}
}
8 changes: 8 additions & 0 deletions compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@
"Compile bootloader program",
cwd="cairo0-bootloader",
)

log_and_run(
[
f"scarb build",
],
"Compile cairo1 project",
cwd="cairo1",
)
22 changes: 5 additions & 17 deletions install.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import subprocess
from colorama import Fore, Style


def log_and_run(commands, description, cwd=None):
from colorama import Fore, Style
full_command = " && ".join(commands)
try:
print(f"{Fore.YELLOW}Starting: {description}...{Style.RESET_ALL}")
Expand All @@ -18,6 +17,8 @@ def log_and_run(commands, description, cwd=None):


if __name__ == "__main__":
subprocess.run("pip install colorama", shell=True, check=True, text=True)

log_and_run(
[
"pip install cairo-lang==0.13.1",
Expand All @@ -28,23 +29,10 @@ def log_and_run(commands, description, cwd=None):
cwd=".",
)

log_and_run(
[
"git clone https://github.com/starkware-libs/cairo.git",
"cd cairo",
"git checkout v2.6.3",
"cd ..",
"mv cairo/corelib/ .",
"rm -rf cairo/",
],
"Clone corelib",
cwd=".",
)

log_and_run(
[
"cargo install --path .",
],
"Install cairo-vm",
cwd="cairo-vm/cairo1-run",
"Installing cairo-vm-runner",
cwd="runner",
)
14 changes: 14 additions & 0 deletions runner/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "runner"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cairo-lang-compiler.workspace = true
cairo-lang-sierra.workspace = true
cairo-vm.workspace = true
cairo1-run.workspace = true
clap.workspace = true
serde_json.workspace = true
40 changes: 40 additions & 0 deletions runner/src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use cairo1_run::FuncArg;
use cairo_vm::Felt252;

#[derive(Debug, Clone, Default)]
pub struct FuncArgs(pub Vec<FuncArg>);

pub fn process_args(value: &str) -> Result<FuncArgs, String> {
if value.is_empty() {
return Ok(FuncArgs::default());
}
let mut args = Vec::new();
let mut input = value.split(' ');
while let Some(value) = input.next() {
// First argument in an array
if value.starts_with('[') {
let mut array_arg =
vec![Felt252::from_dec_str(value.strip_prefix('[').unwrap()).unwrap()];
// Process following args in array
let mut array_end = false;
while !array_end {
if let Some(value) = input.next() {
// Last arg in array
if value.ends_with(']') {
array_arg
.push(Felt252::from_dec_str(value.strip_suffix(']').unwrap()).unwrap());
array_end = true;
} else {
array_arg.push(Felt252::from_dec_str(value).unwrap())
}
}
}
// Finalize array
args.push(FuncArg::Array(array_arg))
} else {
// Single argument
args.push(FuncArg::Single(Felt252::from_dec_str(value).unwrap()))
}
}
Ok(FuncArgs(args))
}
50 changes: 50 additions & 0 deletions runner/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use args::{process_args, FuncArgs};
use cairo1_run::Cairo1RunConfig;
use cairo_vm::types::layout_name::LayoutName;
use clap::{Parser, ValueHint};
use std::path::PathBuf;
pub mod args;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
#[clap(long = "sierra_program", value_parser, value_hint=ValueHint::FilePath)]
sierra_program: PathBuf,
#[clap(long = "args", default_value = "", value_parser=process_args)]
args: FuncArgs,
#[clap(long = "cairo_pie_output", value_parser, value_hint=ValueHint::FilePath)]
cairo_pie_output: PathBuf,
}

fn main() -> std::io::Result<()> {
let args = Args::parse();

// Try to parse the file as a sierra program
let file = std::fs::read(&args.sierra_program)?;
let sierra_program: cairo_lang_sierra::program::Program =
match serde_json::from_slice::<cairo_lang_sierra::program::VersionedProgram>(&file) {
Ok(program) => program.into_v1().unwrap().program,
Err(_) => panic!("program parsing failed"),
};

let (runner, vm, _return_values, _serialized_output) = cairo1_run::cairo_run_program(
&sierra_program,
Cairo1RunConfig {
args: &args.args.0,
layout: LayoutName::all_cairo,
finalize_builtins: true,
serialize_output: true,
append_return_values: true,
..Default::default()
},
)
.unwrap();

runner
.get_cairo_pie(&vm)
.unwrap()
.write_zip_file(&args.cairo_pie_output)
.unwrap();

Ok(())
}
3 changes: 3 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "stable"
profile = "minimal"
Loading