Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstam committed May 30, 2024
1 parent 1d36e43 commit 874ac9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
24 changes: 24 additions & 0 deletions sdk/src/provers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use anyhow::Result;
pub use local::LocalProver;
pub use mock::MockProver;
pub use network::NetworkProver;
use sp1_core::runtime::{Program, Runtime};
use sp1_core::stark::MachineVerificationError;
use sp1_core::utils::SP1CoreOpts;
use sp1_prover::CoreSC;
use sp1_prover::SP1CoreProofData;
use sp1_prover::SP1Prover;
Expand Down Expand Up @@ -72,4 +74,26 @@ pub trait Prover: Send + Sync {

Ok(())
}

// Simulate the execution of a program with the given input, and return the number of cycles.
fn simulate(&self, elf_bytes: &[u8], stdin: &SP1Stdin) -> Result<u64> {
let program = Program::from(elf_bytes);
let mut runtime = Runtime::new(program, SP1CoreOpts::default());
runtime.write_vecs(&stdin.buffer);
for (proof, vkey) in stdin.proofs.iter() {
runtime.write_proof(proof.clone(), vkey.clone());
}

match runtime.run_untraced() {
Ok(_) => {
let cycles = runtime.state.global_clk;
log::info!("Simulation complete, cycles: {}", cycles);
Ok(cycles)
}
Err(e) => {
log::error!("Failed to simulate program: {}", e);
Err(e.into())
}
}
}
}
25 changes: 3 additions & 22 deletions sdk/src/provers/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,6 @@ impl NetworkProver {
) -> Result<P> {
let client = &self.client;

let skip_simulation = env::var("SKIP_SIMULATION")
.map(|val| val == "true")
.unwrap_or(false);

// If the simulation is not skipped, execute the runtime before creating the proof request.
if !skip_simulation {
let program = Program::from(elf);
let opts = SP1CoreOpts::default();
let mut runtime = Runtime::new(program, opts);
runtime.write_vecs(&stdin.buffer);
for (proof, vkey) in stdin.proofs.iter() {
runtime.write_proof(proof.clone(), vkey.clone());
}
runtime
.run_untraced()
.context("Failed to execute program")?;

log::info!("Simulation complete, cycles: {}", runtime.state.global_clk);
} else {
log::info!("Skipping simulation");
}

let proof_id = client.create_proof(elf, &stdin, mode).await?;
log::info!("Created {}", proof_id);

Expand Down Expand Up @@ -174,14 +152,17 @@ impl Prover for NetworkProver {
}

fn prove(&self, pk: &SP1ProvingKey, stdin: SP1Stdin) -> Result<SP1Proof> {
let _ = self.simulate(&pk.elf, &stdin);
block_on(self.prove_async(&pk.elf, stdin, ProofMode::Core))
}

fn prove_compressed(&self, pk: &SP1ProvingKey, stdin: SP1Stdin) -> Result<SP1CompressedProof> {
let _ = self.simulate(&pk.elf, &stdin);
block_on(self.prove_async(&pk.elf, stdin, ProofMode::Compressed))
}

fn prove_plonk(&self, pk: &SP1ProvingKey, stdin: SP1Stdin) -> Result<SP1PlonkBn254Proof> {
let _ = self.simulate(&pk.elf, &stdin);
block_on(self.prove_async(&pk.elf, stdin, ProofMode::Plonk))
}
}
Expand Down

0 comments on commit 874ac9c

Please sign in to comment.