Skip to content

Commit

Permalink
Feature: Stone Prover SDK
Browse files Browse the repository at this point in the history
Imported the code from the Madara Prover API.
  • Loading branch information
odesenfans committed Feb 8, 2024
1 parent d0ee1bc commit facce85
Show file tree
Hide file tree
Showing 22 changed files with 2,334 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# IDE files
.idea/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "dependencies/stone-prover"]
path = dependencies/stone-prover
url = https://github.com/starkware-libs/stone-prover.git
19 changes: 19 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "stone-prover-sdk"
version = "0.1.0"
edition = "2021"
description = "Rust SDK for the Starkware Stone prover and verifier."
build = "build.rs"

[dependencies]
cairo-vm = { git = "https://github.com/Moonsong-Labs/cairo-vm", rev = "b2f69d230416129a84ad8237ccc13d088992f74b", features=["extensive_hints"] }
serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108"
stark_evm_adapter = "0.1.5"
tempfile = "3.8.1"
thiserror = "1.0.50"
tokio = { version = "1.34.0", features = ["macros", "process", "rt-multi-thread"] }


[dev-dependencies]
rstest = "0.18.2"
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# stone-prover-sdk
Rust SDK for the Stone prover and verifier.
# Stone Prover SDK

Rust SDK for the Starkware Stone prover and verifier.
131 changes: 131 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/// Builds the Stone Prover C++ submodule to make it callable from the wrapper.
use std::path::{Path, PathBuf};

#[derive(Debug)]
enum CommandError {
/// The command failed with a non-zero return code.
CommandFailed(std::process::Output),
/// The command could not be launched.
IoError(std::io::Error),
}

impl From<std::io::Error> for CommandError {
fn from(value: std::io::Error) -> Self {
Self::IoError(value)
}
}

/// Run any shell command line and retrieve its output.
fn run_command(command: &str) -> Result<std::process::Output, CommandError> {
let output = std::process::Command::new("sh")
.arg("-c")
.arg(command)
.output()?;

if !output.status.success() {
return Err(CommandError::CommandFailed(output));
}
Ok(output)
}

/// Copy a file from a running Docker container.
fn copy_file_from_container(
container_name: &str,
container_file: &Path,
target: &Path,
) -> Result<(), CommandError> {
let docker_copy_command = format!(
"docker cp -L {container_name}:{} {}",
container_file.to_string_lossy(),
target.to_string_lossy()
);
let _ = run_command(&docker_copy_command);
Ok(())
}

/// Copy the prover and verifier binary files from the prover build container.
fn copy_prover_files_from_container(
container_name: &str,
output_dir: &Path,
) -> Result<(), CommandError> {
copy_file_from_container(container_name, Path::new("/bin/cpu_air_prover"), output_dir)?;
copy_file_from_container(
container_name,
Path::new("/bin/cpu_air_verifier"),
output_dir,
)?;

Ok(())
}

fn make_docker_build_command(repo_dir: &Path, image_name: &str) -> String {
let mut docker_build_command = format!(
"docker build -t {image_name} {}",
repo_dir.to_string_lossy()
);

// Check if the platform is Mac
#[cfg(target_os = "macos")]
{
// Add build args
docker_build_command.push_str(" --build-arg CMAKE_ARGS=-DNO_AVX=1");
}

// Check if a cache image exists. Used by the CI/CD pipeline.
if let Ok(cache_image) = std::env::var("STONE_PROVER_DOCKER_CACHE") {
docker_build_command.push_str(&format!(" --cache-from {cache_image}"));
}

docker_build_command
}

/// Build the Stone Prover and copy binaries to `output_dir`.
///
/// The prover repository contains a Dockerfile to build the prover. This function:
/// 1. Builds the Dockerfile
/// 2. Starts a container based on the generated image
/// 3. Extracts the binaries from the container
/// 4. Stops the container.
fn build_stone_prover(repo_dir: &Path, output_dir: &Path) {
// Build the Stone Prover build Docker image
let image_name = "stone-prover-build:latest";
let docker_build_command = make_docker_build_command(repo_dir, image_name);
run_command(&docker_build_command).expect("Failed to build Stone Prover using Dockerfile");

// Run a container based on the Docker image
let docker_create_command = format!("docker create {image_name}");
let docker_create_output = run_command(&docker_create_command)
.expect("Failed to start container to copy prover files");
let container_name = String::from_utf8_lossy(&docker_create_output.stdout)
.trim()
.to_owned();
println!("Started container {container_name}");

// Copy the files
let copy_result = copy_prover_files_from_container(&container_name, output_dir);

// Stop the container
let docker_delete_command = format!("docker rm {container_name}");
run_command(&docker_delete_command).expect("Failed to stop and delete prover build container");

// Handle a potential error during copy
if let Err(e) = copy_result {
panic!(
"Failed to copy files from the prover build container: {:?}",
e
);
}
}

fn main() {
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
// Copy the prover and verifier to the root of the target directory
// to make them easy to find for all the crates in the workspace.
let target_dir = out_dir.join("../../..").canonicalize().unwrap();

let stone_prover_repo_dir = Path::new("dependencies/stone-prover");
build_stone_prover(stone_prover_repo_dir, target_dir.as_path());

// Rerun if the submodule is updated
println!("cargo:rerun-if-changed=.git/modules/dependencies/stone-prover/HEAD");
}
11 changes: 11 additions & 0 deletions cairo-programs/fibonacci/air_private_input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"trace_path": "/home/root/fibonacci_trace.bin",
"memory_path": "/home/root/fibonacci_memory.bin",
"pedersen": [],
"range_check": [],
"ecdsa": [],
"bitwise": [],
"ec_op": [],
"keccak": [],
"poseidon": []
}
211 changes: 211 additions & 0 deletions cairo-programs/fibonacci/air_public_input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
{
"layout": "starknet_with_keccak",
"rc_min": 32763,
"rc_max": 32769,
"n_steps": 32768,
"memory_segments": {
"program": {
"begin_addr": 1,
"stop_ptr": 5
},
"execution": {
"begin_addr": 33,
"stop_ptr": 102
},
"output": {
"begin_addr": 102,
"stop_ptr": 102
},
"pedersen": {
"begin_addr": 102,
"stop_ptr": 102
},
"range_check": {
"begin_addr": 3174,
"stop_ptr": 3174
},
"ecdsa": {
"begin_addr": 5222,
"stop_ptr": 5222
},
"bitwise": {
"begin_addr": 5254,
"stop_ptr": 5254
},
"ec_op": {
"begin_addr": 7814,
"stop_ptr": 7814
},
"keccak": {
"begin_addr": 8038,
"stop_ptr": 8038
},
"poseidon": {
"begin_addr": 8294,
"stop_ptr": 8294
}
},
"public_memory": [
{
"address": 1,
"value": "0x40780017fff7fff",
"page": 0
},
{
"address": 2,
"value": "0x0",
"page": 0
},
{
"address": 3,
"value": "0x1104800180018000",
"page": 0
},
{
"address": 4,
"value": "0x4",
"page": 0
},
{
"address": 5,
"value": "0x10780017fff7fff",
"page": 0
},
{
"address": 6,
"value": "0x0",
"page": 0
},
{
"address": 7,
"value": "0x480680017fff8000",
"page": 0
},
{
"address": 8,
"value": "0x1",
"page": 0
},
{
"address": 9,
"value": "0x480680017fff8000",
"page": 0
},
{
"address": 10,
"value": "0x1",
"page": 0
},
{
"address": 11,
"value": "0x480680017fff8000",
"page": 0
},
{
"address": 12,
"value": "0xa",
"page": 0
},
{
"address": 13,
"value": "0x1104800180018000",
"page": 0
},
{
"address": 14,
"value": "0x5",
"page": 0
},
{
"address": 15,
"value": "0x400680017fff7fff",
"page": 0
},
{
"address": 16,
"value": "0x90",
"page": 0
},
{
"address": 17,
"value": "0x208b7fff7fff7ffe",
"page": 0
},
{
"address": 18,
"value": "0x20780017fff7ffd",
"page": 0
},
{
"address": 19,
"value": "0x5",
"page": 0
},
{
"address": 20,
"value": "0x480a7ffc7fff8000",
"page": 0
},
{
"address": 21,
"value": "0x480a7ffc7fff8000",
"page": 0
},
{
"address": 22,
"value": "0x208b7fff7fff7ffe",
"page": 0
},
{
"address": 23,
"value": "0x482a7ffc7ffb8000",
"page": 0
},
{
"address": 24,
"value": "0x480a7ffc7fff8000",
"page": 0
},
{
"address": 25,
"value": "0x48127ffe7fff8000",
"page": 0
},
{
"address": 26,
"value": "0x482680017ffd8000",
"page": 0
},
{
"address": 27,
"value": "0x800000000000011000000000000000000000000000000000000000000000000",
"page": 0
},
{
"address": 28,
"value": "0x1104800180018000",
"page": 0
},
{
"address": 29,
"value": "0x800000000000010fffffffffffffffffffffffffffffffffffffffffffffff7",
"page": 0
},
{
"address": 30,
"value": "0x208b7fff7fff7ffe",
"page": 0
},
{
"address": 31,
"value": "0x21",
"page": 0
},
{
"address": 32,
"value": "0x0",
"page": 0
}
],
"dynamic_params": null
}
Loading

0 comments on commit facce85

Please sign in to comment.