Skip to content

Commit

Permalink
Make prover work without docker dependency (#8)
Browse files Browse the repository at this point in the history
* CairoZero docker remove

* cairo0 paths changes

* Cairo1 docker removed

* changed comments

* Dockerfile changes

* Podman Crate deleted

* cairo1-compile

* Removing files

* Docker file updates

* cairo-lang dependencies

* fibonacci examples changes

* gitignore update

* updates

* corelib installation script

* fmt

* wait for prover and curl

* wait for prover

* test fix

* test workflow trigger

* check error

* automated tests in ci

* fmt, clippy

* verifier in docker

* script update, check corectness of build and run

* fix docker image

* fix clippy

* publish ci

* run on 32 core

* remove docker

* test all

* test serially

* script update, check corectness of build and run

* fix docker image

* fix clippy

* publish ci

* run on 32 core

* remove docker

* test all

* test serially

* proof verification on tests, using cpu_air_verifier

* Port in tests as env, container_engine variable

---------

Co-authored-by: Piotr Stec <[email protected]>
Co-authored-by: Mateusz Chudkowski <[email protected]>
Co-authored-by: Mateusz Chudkowski <[email protected]>
  • Loading branch information
4 people authored Aug 16, 2024
1 parent c6e501a commit dea6182
Show file tree
Hide file tree
Showing 34 changed files with 1,912 additions and 406 deletions.
46 changes: 44 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
# Generated by Cargo
# will have compiled files and executables
**/debug/
**/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
**/Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

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

# Scarb
**/Scarb.lock

# vscode
**/.vscode/

# Git
**/.git

# Docker
**/Dockerfile

# Ignores
**/.gitignore
**/.dockerignore

# Envs
**/*.cargo

# Venvs
**/.venv


/resources
output.json
examples/Cairo/prover_input.json
examples/CairoZero/prover_input.json
/corelib

.idea/
.git/
target/
8 changes: 2 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Build
run: cargo build --verbose
test:
runs-on: ubuntu-latest
runs-on: ubuntu-latest-32-core
steps:
- uses: actions/checkout@v4
- uses: asdf-vm/actions/install@v3
Expand All @@ -37,9 +37,5 @@ jobs:
ls -Rl examples/
- name: Build
run: cargo build --verbose
- name: Run prover
run: |
cargo run -p prover -- --jwt-secret-key "asdf" --authorized-keys "0xd16b71c90dbf897e5964d2f267d04664b3c035036559d712994739ea6cf2fd9f" --message-expiration-time 60 --session-expiration-time 3600 &
sleep 5
- name: Run tests
run: cargo test --no-fail-fast --workspace --verbose
run: ./scripts/e2e_test.sh
15 changes: 12 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@
# vscode
**/.vscode/

# Git
**/.git

# Envs
**/*.cargo

# Venvs
**/.venv

/resources
/.cargo
.venv
output.json
examples/Cairo/prover_input.json
examples/CairoZero/prover_input.json
examples/CairoZero/prover_input.json
/corelib

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ members = [
"bin/keygen",
"bin/register",
"crates/common",
"crates/podman",
"crates/utils",
"prover",
"prover-sdk",
"cairo1-compile",
]

[workspace.package]
Expand All @@ -30,13 +30,12 @@ http = "1.1.0"
hyper-util = "0.1.3"
jsonwebtoken = "9.3.0"
lazy_static = "1.4.0"
podman = { path = "crates/podman" }
prefix-hex = "0.7.1"
prover = { path = "prover" }
prover-sdk = { path = "prover-sdk" }
rand = "0.8.5"
reqwest = { version = "0.12.4", features = ["blocking", "json", "rustls-tls"], default-features = false }
reqwest_cookie_store = "0.7.0"
prover-sdk = { path = "prover-sdk" }
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.116"
serde_with = "3.8.1"
Expand All @@ -54,3 +53,6 @@ tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
url = { version = "2.5.0", features = ["serde"] }
utils = { path = "crates/utils" }
config-generator = { git = "https://github.com/piotr-stec/genereate-config.git" }
cairo-lang-compiler = { git = "https://github.com/starkware-libs/cairo", tag = "v2.7.0-rc.3", default-features = false }
cairo-lang-sierra = { git = "https://github.com/starkware-libs/cairo", tag = "v2.7.0-rc.3", default-features = false }
35 changes: 32 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,38 @@ COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .
ENV PATH="/root/.cargo/bin:${PATH}"

RUN cargo build --release -p prover
RUN cargo install --git https://github.com/lambdaclass/cairo-vm --rev 37ea72977dccbc2b90b8b7534c1edabd2e2fef79 cairo1-run


FROM docker.io/piotr439/prover AS prover


FROM python:3.9.18-slim-bookworm AS final
WORKDIR /
RUN apt update && apt install -y build-essential libgmp-dev elfutils jq git
RUN pip install --upgrade pip

COPY --from=builder /app/target/release/prover /usr/local/bin/prover
COPY --from=builder /usr/local/cargo/bin/cairo1-run /usr/local/bin/cairo1-run
COPY --from=prover /usr/bin/cpu_air_prover /usr/local/bin/cpu_air_prover
COPY --from=prover /usr/bin/cpu_air_verifier /usr/local/bin/cpu_air_verifier

COPY --from=builder /app/config/cpu_air_prover_config.json /config/cpu_air_prover_config.json
RUN git clone --depth=1 -b v2.7.0-rc.3 https://github.com/starkware-libs/cairo.git
RUN mv cairo/corelib/ .

RUN rm -rf cairo

RUN pip install cairo-lang==0.13.1
RUN pip install sympy==1.12.1

RUN mkdir resources/
RUN mkdir resources/cairo/
RUN mkdir resources/cairoZero/

EXPOSE 3000

# We do not need the Rust toolchain to run the binary!
FROM alpine AS runtime
COPY --from=builder /app/target/release/prover /usr/local/bin
ENTRYPOINT [ "prover" ]
3 changes: 2 additions & 1 deletion bin/cairo-prove/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
cargo run --bin cairo-prove -- --key <your secret key for sdk> --cairo-version <1/0> --url <url for prover> your_input.json > output.json
```

By default cairo version is set to cairo 1
By default cairo version is set to cairo 1
test workflow

## Input format

Expand Down
39 changes: 0 additions & 39 deletions bin/cairo-prove/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@
use std::path::PathBuf;

use prover::server::start;
use prover::Args;
use prover_sdk::ProverAccessKey;
use tokio::fs::File;
use tokio::io::AsyncReadExt;
use tokio::net::TcpListener;
use tokio::task::JoinHandle;
use url::Url;

pub async fn spawn_prover() -> (JoinHandle<()>, ProverAccessKey, Url) {
use url::Url;

let port = TcpListener::bind("127.0.0.1:0")
.await
.unwrap()
.local_addr()
.unwrap()
.port();

let key = ProverAccessKey::generate();
let encoded_key = prefix_hex::encode(key.0.verifying_key().to_bytes());

let args = Args {
host: "0.0.0.0".to_string(),
port,
jwt_secret_key: "placeholder".to_string(),
message_expiration_time: 60,
session_expiration_time: 3600,
authorized_keys: Some(vec![encoded_key]),
authorized_keys_path: None,
};

let handle = tokio::spawn(async move {
start(args).await.unwrap();
});

let url = Url::parse(&format!("http://localhost:{}", port)).unwrap();

(handle, key, url)
}

pub async fn read_file(path: PathBuf) -> Result<String, std::io::Error> {
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand Down
33 changes: 16 additions & 17 deletions bin/cairo-prove/tests/prove.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
use crate::common::{read_file, spawn_prover};
use crate::common::read_file;
use cairo_proof_parser::output::{extract_output, ExtractOutputResult};
use cairo_prove::{prove, CliInput};
use serde_json::Value;
use std::path::PathBuf;

use std::{env, path::PathBuf};
use url::Url;
mod common;

// #[tokio::test]
#[tokio::test]
async fn test_cairo1_fibonacci() -> Result<(), cairo_prove::ProveError> {
let (handle, key, url) = spawn_prover().await;

let key = "0x5883b0e30b008e48af3d0bf5cfc138fb6093496da6f87d24b65def88470356d3";
let port = env::var("PORT").unwrap();
let prover_url = Url::parse(&format!("http://localhost:{}", port)).unwrap();
let args = CliInput {
key: key.signing_key_as_hex_string(),
key: key.to_string(),
cairo_version: 1,
url,
url: prover_url,
};

let prover_input = read_file(PathBuf::from("examples/Cairo/prover_input.json")).await?;
let prover_input =
read_file(PathBuf::from("examples/Cairo/fibonacci_prover_input.json")).await?;
let proof = prove(args, prover_input).await?;
assert!(extract_output(&proof).is_ok());
handle.abort();
Ok(())
}
#[tokio::test]
async fn test_cairo0_fibonacci() -> Result<(), cairo_prove::ProveError> {
let (handle, key, url) = spawn_prover().await;

let key = "0x5883b0e30b008e48af3d0bf5cfc138fb6093496da6f87d24b65def88470356d3";
let port = env::var("PORT").unwrap();
let prover_url = Url::parse(&format!("http://localhost:{}", port)).unwrap();
let args = CliInput {
key: key.signing_key_as_hex_string(),
key: key.to_string(),
cairo_version: 0,
url,
url: prover_url,
};
let prover_input = read_file(PathBuf::from("examples/CairoZero/prover_input.json")).await?;
let program_input: Value = serde_json::from_str(&prover_input)?;
Expand All @@ -49,8 +51,5 @@ async fn test_cairo0_fibonacci() -> Result<(), cairo_prove::ProveError> {
expected_input, fibonacci_claim_index,
"Fibonacci index mismatch."
);

handle.abort();

Ok(())
}
9 changes: 6 additions & 3 deletions crates/podman/Cargo.toml → cairo1-compile/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
[package]
name = "podman"
name = "cairo1-compile"
version.workspace = true
edition.workspace = true

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

[dependencies]
thiserror.workspace = true
tokio.workspace = true
cairo-lang-compiler.workspace = true
cairo-lang-sierra.workspace = true
clap.workspace = true
serde.workspace = true
serde_json.workspace = true
26 changes: 26 additions & 0 deletions cairo1-compile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# Installation Guide

Follow the steps below to install `cairo1-compile` and `cairo1-run`.

## Install `cairo1-compile`

To install `cairo1-compile`, run the following command:

```sh
cargo install --path cairo1-compile
```

## Install `cairo1-run`

To install `cairo1-run` from the repository, run the following command:

```sh
cargo install --git https://github.com/lambdaclass/cairo-vm cairo1-run
```


`cairo1-compile compile --output resources/fibonacci_compiled.sierra.json e2e_test/Cairo/fibonacci.cairo`
`cairo1-compile merge -o resources/fibonacci_prover_input.json resources/fibonacci_compiled.sierra.json e2e_test/Cairo/input.json`
`podman build -t stone5-cairo1:recursive -f Dockerfile .`
`podman run -i --rm stone5-cairo1:recursive < resources/fibonacci_prover_input.json > resources/proof.json`
Loading

0 comments on commit dea6182

Please sign in to comment.