8000 dev-utils: extract method for serializing the proof to file by m-kus · Pull Request #955 · starkware-libs/stwo-cairo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

dev-utils: extract method for serializing the proof to file #955

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

Merged
merged 1 commit into from
Jun 5, 2025
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
3 changes: 3 additions & 0 deletions stwo_cairo_prover/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions stwo_cairo_prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rand = "0.8.5"
ruint = "1.12.3"
serde = "1.0.207"
serde_json = "1.0.1"
sonic-rs = "0.3.17"
starknet-ff = "0.3.7"
starknet-types-core = "0.1.7"
stwo_cairo_prover = { path = "crates/prover", version = "~0.1.0" }
Expand Down
3 changes: 3 additions & 0 deletions stwo_cairo_prover/crates/cairo-air/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ edition.workspace = true

[dependencies]
itertools.workspace = true
clap.workspace = true
sonic-rs.workspace = true
log.workspace = true
num-traits.workspace = true
paste.workspace = true
Expand All @@ -20,6 +22,7 @@ stwo-cairo-serialize = { path = "../cairo-serialize" }
stwo-prover.workspace = true
thiserror.workspace = true
serde_json.workspace = true
tracing.workspace = true

[dev-dependencies]
rand.workspace = true
1 change: 1 addition & 0 deletions stwo_cairo_prover/crates/cairo-air/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod preprocessed;
pub mod preprocessed_utils;
pub mod range_checks_air;
pub mod relations;
pub mod utils;

// TODO(Ohad): verifier crate.
pub mod verifier;
Expand Down
56 changes: 56 additions & 0 deletions stwo_cairo_prover/crates/cairo-air/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

use serde::Serialize;
use stwo_cairo_serialize::CairoSerialize;
use stwo_prover::core::channel::MerkleChannel;
use stwo_prover::core::vcs::ops::MerkleHasher;
use tracing::{span, Level};

use crate::CairoProof;

/// Cairo proof format
#[derive(Debug, Clone, clap::ValueEnum)]
pub enum ProofFormat {
/// Standard JSON format.
Json,
/// Array of field elements serialized as hex strings.
/// Compatible with `scarb execute`
CairoSerde,
}

/// Serializes Cairo proof given the desired format and writes it to a file.
pub fn serialize_proof_to_file<MC: MerkleChannel>(
proof: &CairoProof<MC::H>,
proof_path: PathBuf,
proof_format: ProofFormat,
) -> Result<(), std::io::Error>
where
MC::H: Serialize,
<MC::H as MerkleHasher>::Hash: CairoSerialize,
{
let span = span!(Level::INFO, "Serialize proof").entered();

let mut proof_file = File::create(proof_path)?;

match proof_format {
ProofFormat::Json => {
proof_file.write_all(sonic_rs::to_string_pretty(proof)?.as_bytes())?;
}
ProofFormat::CairoSerde => {
let mut serialized: Vec<starknet_ff::FieldElement> = Vec::new();
CairoSerialize::serialize(proof, &mut serialized);

let hex_strings: Vec<String> = serialized
.into_iter()
.map(|felt| format!("0x{:x}", felt))
.collect();

proof_file.write_all(sonic_rs::to_string_pretty(&hex_strings)?.as_bytes())?;
}
}

span.exit();
Ok(())
}
3 changes: 1 addition & 2 deletions stwo_cairo_prover/crates/dev_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cairo-air.workspace = true
clap.workspace = true
serde.workspace = true
starknet-ff.workspace = true
sonic-rs = "0.3.17"
sonic-rs.workspace = true
stwo-cairo-adapter = {path= "../adapter", features = ["std"]}
stwo-cairo-serialize.workspace = true
stwo-prover.workspace = true
Expand All @@ -17,4 +17,3 @@ tracing-subscriber.workspace = true
thiserror.workspace = true
tracing.workspace = true
log.workspace = true

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::path::PathBuf;

use cairo_air::utils::ProofFormat;
use clap::Parser;
use dev_utils::utils::{create_and_serialize_proof, Error, ProofFormat};
use dev_utils::utils::{create_and_serialize_proof, Error};
use stwo_cairo_adapter::test_utils::{read_compiled_cairo_program, run_program_and_adapter};
use tracing::{span, Level};
use tracing_subscriber::fmt::format::FmtSpan;
Expand Down
32 changes: 2 additions & 30 deletions stwo_cairo_prover/crates/dev_utils/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

use cairo_air::utils::{serialize_proof_to_file, ProofFormat};
use cairo_air::verifier::{verify_cairo, CairoVerificationError};
use cairo_air::PreProcessedTraceVariant;
use serde::Serialize;
Expand All @@ -20,16 +19,6 @@ use stwo_prover::core::vcs::blake2_merkle::Blake2sMerkleChannel;
use stwo_prover::core::vcs::ops::MerkleHasher;
use stwo_prover::core::vcs::poseidon252_merkle::Poseidon252MerkleChannel;
use thiserror::Error;
use tracing::{span, Level};

#[derive(Debug, Clone, clap::ValueEnum)]
pub enum ProofFormat {
/// Standard JSON format.
Json,
/// Array of field elements serialized as hex strings.
/// Compatible with `scarb execute`
CairoSerde,
}

#[derive(Debug, Error)]
pub enum Error {
Expand Down Expand Up @@ -64,26 +53,9 @@ where
<MC::H as MerkleHasher>::Hash: CairoSerialize,
{
let proof = prove_cairo::<MC>(input, pcs_config, preprocessed_trace)?;
let mut proof_file = File::create(proof_path)?;

let span = span!(Level::INFO, "Serialize proof").entered();
match proof_format {
ProofFormat::Json => {
proof_file.write_all(sonic_rs::to_string_pretty(&proof)?.as_bytes())?;
}
ProofFormat::CairoSerde => {
let mut serialized: Vec<starknet_ff::FieldElement> = Vec::new();
CairoSerialize::serialize(&proof, &mut serialized);
serialize_proof_to_file::<MC>(&proof, proof_path, proof_format)?;

let hex_strings: Vec<String> = serialized
60DC .into_iter()
.map(|felt| format!("0x{:x}", felt))
.collect();

proof_file.write_all(sonic_rs::to_string_pretty(&hex_strings)?.as_bytes())?;
}
}
span.exit();
if verify {
verify_cairo::<MC>(proof, pcs_config, preprocessed_trace)?;
log::info!("Proof verified successfully");
Expand Down
Loading
0