Skip to content

Commit

Permalink
proof as felts, added verification on proof parser to tests (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
chudkowsky authored Sep 23, 2024
1 parent c157393 commit 0466083
Show file tree
Hide file tree
Showing 8 changed files with 1,185 additions and 169 deletions.
1,216 changes: 1,115 additions & 101 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ base64 = "0.22.1"
starknet-types-core = "~0.1.4"
futures = "0.3.30"
async-stream = "0.3.5"
swiftness_proof_parser = { git = "https://github.com/cartridge-gg/swiftness", rev = "1d46e21"}
cairo-proof-parser = {git = "https://github.com/cartridge-gg/cairo-proof-parser.git",rev = "f175d58"}
starknet-crypto = "0.7.0"
anyhow = "1.0.89"
1 change: 1 addition & 0 deletions common/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum JobStatus {
#[derive(Clone, Serialize, Deserialize)]
pub struct ProverResult {
pub proof: String,
pub serialized_proof: Vec<Felt>,
pub program_hash: Felt,
pub program_output: Vec<Felt>,
pub program_output_hash: Felt,
Expand Down
41 changes: 40 additions & 1 deletion prover-sdk/tests/prove_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ async fn test_cairo_prove() {
let result = fetch_job(sdk.clone(), job).await;
assert!(result.is_some());
let result = result.unwrap();

//Values calculated using https://github.com/HerodotusDev/integrity
assert_eq!(result.serialized_proof.len(), 2533);
assert_eq!(
result.program_hash,
Felt::from_dec_str(
"2251972324230578422543092394494031242690791181195034520556584290316798249271"
)
.unwrap()
);
assert_eq!(result.program_output.len(), 7);
assert_eq!(
result.program_output_hash,
Felt::from_dec_str(
"2144555888719052742880342011775786530333616377198088482005787934731079204155"
)
.unwrap()
);
let result = sdk.clone().verify(result.proof).await;
assert!(result.is_ok(), "Failed to verify proof");
assert_eq!("true", result.unwrap());
Expand All @@ -61,7 +79,28 @@ async fn test_cairo0_prove() {
};
let job = sdk.prove_cairo0(data).await.unwrap();
let result = fetch_job(sdk.clone(), job).await;
let result = sdk.clone().verify(result.unwrap().proof).await.unwrap();
assert!(result.is_some());
let result = result.unwrap();

//Values calculated using https://github.com/HerodotusDev/integrity
assert_eq!(result.serialized_proof.len(), 2370);
assert_eq!(
result.program_hash,
Felt::from_dec_str(
"3470677812397724434300536580370163457237813256743569044140337948140729574027"
)
.unwrap()
);
assert_eq!(result.program_output.len(), 2);
assert_eq!(
result.program_output_hash,
Felt::from_dec_str(
"2736399355406991235942465207827961599881564213022637607206006880098495172292"
)
.unwrap()
);

let result = sdk.clone().verify(result.proof).await.unwrap();
assert_eq!("true", result);
}
#[tokio::test]
Expand Down
2 changes: 1 addition & 1 deletion prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ base64.workspace = true
starknet-types-core.workspace = true
futures.workspace = true
async-stream.workspace = true
swiftness_proof_parser.workspace = true
cairo-proof-parser.workspace = true
starknet-crypto.workspace = true
anyhow.workspace = true
39 changes: 27 additions & 12 deletions prover/src/threadpool/prove.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use super::run::RunPaths;
use super::CairoVersionedInput;
use crate::errors::ProverError;
use crate::utils::proof_parser::{
extract_program_hash, extract_program_output, program_output_hash,
};
use crate::utils::{config::Template, job::JobStore};
use cairo_proof_parser::json_parser::proof_from_annotations;
use cairo_proof_parser::output::ExtractOutputResult;
use cairo_proof_parser::program::{CairoVersion, ExtractProgramResult};
use cairo_proof_parser::{self, ProofJSON};
use common::models::{JobStatus, ProverResult};
use serde_json::Value;
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use swiftness_proof_parser::{json_parser, stark_proof};
use tempfile::TempDir;
use tokio::process::Command;
use tokio::sync::broadcast::Sender;
Expand Down Expand Up @@ -45,7 +45,14 @@ pub async fn prove(
let sender = sse_tx.lock().await;

if prove_status.success() {
let prover_result = prover_result(final_result)?;
let prover_result = match program_input {
CairoVersionedInput::Cairo(_cairo_input) => {
prover_result(final_result, CairoVersion::Cairo)?
}
CairoVersionedInput::Cairo0(_cairo0_input) => {
prover_result(final_result, CairoVersion::Cairo0)?
}
};
job_store
.update_job_status(
job_id,
Expand All @@ -71,17 +78,25 @@ pub async fn prove(
Ok(())
}

fn prover_result(final_result: String) -> Result<ProverResult, ProverError> {
let proof_json = serde_json::from_str::<json_parser::StarkProof>(&final_result)?;
let stark_proof = stark_proof::StarkProof::try_from(proof_json)?;
let program_hash = extract_program_hash(stark_proof.clone());
let program_output = extract_program_output(stark_proof.clone());
let program_output_hash = program_output_hash(program_output.clone());
fn prover_result(proof: String, cairo_version: CairoVersion) -> Result<ProverResult, ProverError> {
let proof_json = serde_json::from_str::<ProofJSON>(&proof)?;
let proof_from_annotations = proof_from_annotations(proof_json)?;
let ExtractProgramResult { program_hash, .. } = if cairo_version == CairoVersion::Cairo0 {
proof_from_annotations.extract_program(CairoVersion::Cairo0)?
} else {
proof_from_annotations.extract_program(CairoVersion::Cairo)?
};
let ExtractOutputResult {
program_output,
program_output_hash,
} = proof_from_annotations.extract_output()?;
let serialized_proof = proof_from_annotations.to_felts();
let prover_result = ProverResult {
proof: final_result.clone(),
proof: proof.clone(),
program_hash,
program_output,
program_output_hash,
serialized_proof,
};
Ok(prover_result)
}
Expand Down
1 change: 0 additions & 1 deletion prover/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod config;
pub mod job;
pub mod proof_parser;
pub mod shutdown;
52 changes: 0 additions & 52 deletions prover/src/utils/proof_parser.rs

This file was deleted.

0 comments on commit 0466083

Please sign in to comment.