Skip to content

Commit

Permalink
refactor: fixing dep tree for prover, recursion, core and sdk (
Browse files Browse the repository at this point in the history
…#545)

Co-authored-by: John Guibas <[email protected]>
  • Loading branch information
ratankaliani and jtguibas authored Apr 20, 2024
1 parent 08a6281 commit 892db4b
Show file tree
Hide file tree
Showing 32 changed files with 596 additions and 484 deletions.
9 changes: 6 additions & 3 deletions Cargo.lock

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

19 changes: 15 additions & 4 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
vergen = { version = "8", default-features = false, features = ["build", "git", "git2"] }
vergen = { version = "8", default-features = false, features = [
"build",
"git",
"git2",
] }

[dependencies]
anyhow = { version = "1.0.79", features = ["backtrace"] }
cargo_metadata = "0.18.1"
clap = { version = "4.4.15", features = ["derive", "env"] }
sp1-core = { path = "../core" }
sp1-prover = { path = "../prover" }
sp1-sdk = { path = "../sdk" }
reqwest = { version = "0.11.3", features = ["stream", "json", "rustls-tls"], default-features = false }
sp1-core = { path = "../core" }
reqwest = { version = "0.11.3", features = [
"stream",
"json",
"rustls-tls",
], default-features = false }
futures-util = "0.3.14"
indicatif = "0.15.0"
tokio = { version = "1", features = ["full"] }
Expand All @@ -23,7 +32,9 @@ flate2 = "1.0"
dirs = "4.0"
serde = { version = "1", features = ["derive"] }
rand = "0.8"
downloader = { version = "0.2", default-features = false, features = ["rustls-tls"] }
downloader = { version = "0.2", default-features = false, features = [
"rustls-tls",
] }
serde_json = "1.0.113"
yansi = "0.5.1"
hex = "0.4.3"
Expand Down
8 changes: 5 additions & 3 deletions cli/src/commands/prove.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anstyle::*;
use anyhow::Result;
use clap::Parser;
use sp1_sdk::{utils, ProverClient, SP1Stdin};
use sp1_core::utils::{setup_logger, setup_tracer};
use sp1_prover::SP1Stdin;
use sp1_sdk::ProverClient;
use std::time::Instant;
use std::{env, fs::File, io::Read, path::PathBuf, str::FromStr};

Expand Down Expand Up @@ -80,13 +82,13 @@ impl ProveCmd {
Ok(_) => {}
Err(_) => env::set_var("RUST_LOG", "info"),
}
utils::setup_logger();
setup_logger();
} else {
match env::var("RUST_TRACER") {
Ok(_) => {}
Err(_) => env::set_var("RUST_TRACER", "info"),
}
utils::setup_tracer();
setup_tracer();
}

let mut elf = Vec::new();
Expand Down
3 changes: 1 addition & 2 deletions core/src/io.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::utils::Buffer;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

/// Standard input for the prover.
#[derive(Serialize, Deserialize, Clone)]
Expand Down
16 changes: 0 additions & 16 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub mod alu;
pub mod bytes;
pub mod cpu;
pub mod disassembler;
#[deprecated(note = "Import from sp1_sdk instead of sp1_core")]
pub mod io;
pub mod lookup;
pub mod memory;
Expand All @@ -33,21 +32,6 @@ pub mod stark;
pub mod syscall;
pub mod utils;

pub use io::*;

#[allow(unused_imports)]
use runtime::{Program, Runtime};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use stark::MachineProof;
use stark::StarkGenericConfig;

/// A proof of a RISCV ELF execution with given inputs and outputs.
#[derive(Serialize, Deserialize)]
#[deprecated(note = "Import from sp1_sdk instead of sp1_core")]
pub struct SP1ProofWithIO<SC: StarkGenericConfig + Serialize + DeserializeOwned> {
#[serde(with = "proof_serde")]
pub proof: MachineProof<SC>,
pub stdin: SP1Stdin,
pub public_values: SP1PublicValues,
}
2 changes: 1 addition & 1 deletion core/src/syscall/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ mod tests {
use rand::RngCore;

use crate::{
io::SP1Stdin,
runtime::Program,
utils::{run_and_prove, setup_logger, BabyBearPoseidon2},
SP1Stdin,
};

const HINT_IO_ELF: &[u8] =
Expand Down
2 changes: 1 addition & 1 deletion core/src/syscall/precompiles/keccak256/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ where

#[cfg(test)]
mod test {
use crate::io::{SP1PublicValues, SP1Stdin};
use crate::runtime::Program;
use crate::stark::{RiscvAir, StarkGenericConfig};
use crate::utils::{run_and_prove, setup_logger, tests::KECCAK256_ELF, BabyBearPoseidon2};
use crate::{SP1PublicValues, SP1Stdin};

use rand::Rng;
use rand::SeedableRng;
Expand Down
2 changes: 1 addition & 1 deletion core/src/syscall/precompiles/uint256/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ mod tests {

use crate::operations::field::params::FieldParameters;
use crate::{
io::SP1Stdin,
runtime::Program,
utils::{
self,
ec::{uint256::U256Field, utils::biguint_from_limbs},
run_test_io,
tests::{UINT256_DIV, UINT256_MUL},
},
SP1Stdin,
};

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,9 @@ where

#[cfg(test)]
mod tests {
use crate::io::SP1Stdin;
use crate::utils::{self, tests::BLS12381_DECOMPRESS_ELF};
use crate::Program;
use crate::{
utils::{self, tests::BLS12381_DECOMPRESS_ELF},
SP1Stdin,
};
use amcl::bls381::bls381::basic::key_pair_generate_g2;
use amcl::bls381::bls381::utils::deserialize_g1;
use amcl::rand::RAND;
Expand All @@ -359,11 +357,11 @@ mod tests {
rand.seed(len, &random_slice);
let (_, compressed) = key_pair_generate_g2(&mut RAND::new());

let inputs = SP1Stdin::from(&compressed);
let mut proof = run_test_io(Program::from(BLS12381_DECOMPRESS_ELF), inputs).unwrap();
let stdin = SP1Stdin::from(&compressed);
let mut public_values = run_test_io(Program::from(BLS12381_DECOMPRESS_ELF), stdin).unwrap();

let mut result = [0; 96];
proof.public_values.read_slice(&mut result);
public_values.read_slice(&mut result);

let point = deserialize_g1(&compressed).unwrap();
let x = point.getx().to_string();
Expand Down Expand Up @@ -391,7 +389,7 @@ mod tests {

let mut proof = run_test_io(Program::from(SECP256K1_DECOMPRESS_ELF), inputs).unwrap();
let mut result = [0; 65];
proof.public_values.read_slice(&mut result);
proof.buffer.read_slice(&mut result);
assert_eq!(result, decompressed);
}
}
Expand Down
14 changes: 5 additions & 9 deletions core/src/utils/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs::File;
use std::io::{Seek, Write};
use web_time::Instant;

use crate::io::{SP1PublicValues, SP1Stdin};
pub use baby_bear_blake3::BabyBearBlake3;
use p3_challenger::CanObserve;
use p3_field::PrimeField32;
Expand All @@ -19,8 +20,6 @@ use crate::{
stark::{LocalProver, OpeningProof, ShardMainData},
};

use crate::{SP1ProofWithIO, SP1PublicValues, SP1Stdin};

const LOG_DEGREE_BOUND: usize = 31;

pub fn get_cycles(program: Program) -> u64 {
Expand All @@ -29,23 +28,20 @@ pub fn get_cycles(program: Program) -> u64 {
runtime.state.global_clk as u64
}

/// Runs a program and returns the public values stream.
pub fn run_test_io(
program: Program,
inputs: SP1Stdin,
) -> Result<SP1ProofWithIO<BabyBearBlake3>, crate::stark::ProgramVerificationError> {
) -> Result<SP1PublicValues, crate::stark::ProgramVerificationError> {
let runtime = tracing::info_span!("runtime.run(...)").in_scope(|| {
let mut runtime = Runtime::new(program);
runtime.write_vecs(&inputs.buffer);
runtime.run();
runtime
});
let public_values = SP1PublicValues::from(&runtime.state.public_values_stream);
let proof = run_test_core(runtime)?;
Ok(SP1ProofWithIO {
proof,
stdin: inputs,
public_values,
})
let _ = run_test_core(runtime)?;
Ok(public_values)
}

pub fn run_test(
Expand Down
4 changes: 2 additions & 2 deletions eval/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition = "2021"

[dependencies]
sp1-core = { path = "../core" }
sp1-sdk = { path = "../sdk" }
sp1-prover = { path = "../prover" }

clap = { version = "4.4.0", features = ["derive"] }
csv = "1.3.0"
serde = "1.0.196"

34 changes: 8 additions & 26 deletions eval/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use clap::{command, Parser};
use csv::WriterBuilder;
use serde::Serialize;
use sp1_core::runtime::{Program, Runtime};
use sp1_core::utils::{get_cycles, prove_core};
use sp1_sdk::utils::{BabyBearBlake3, BabyBearKeccak, BabyBearPoseidon2};
use sp1_sdk::{ProverClient, SP1ProofWithIO, SP1PublicValues, SP1Stdin};
use sp1_core::utils::{get_cycles, prove_core, BabyBearBlake3, BabyBearKeccak, BabyBearPoseidon2};
use std::fmt;
use std::fs::OpenOptions;
use std::io;
Expand Down Expand Up @@ -131,8 +129,7 @@ fn main() {
}
}

fn run_evaluation(hashfn: &HashFnId, program: &Program, elf: &[u8]) -> (f64, f64, f64) {
let client = ProverClient::new();
fn run_evaluation(hashfn: &HashFnId, program: &Program, _elf: &[u8]) -> (f64, f64, f64) {
match hashfn {
HashFnId::Blake3 => {
let mut runtime = Runtime::new(program.clone());
Expand All @@ -142,16 +139,11 @@ fn run_evaluation(hashfn: &HashFnId, program: &Program, elf: &[u8]) -> (f64, f64

let config = BabyBearBlake3::new();
let prove_start = Instant::now();
let proof = prove_core(config.clone(), runtime);
let _proof = prove_core(config.clone(), runtime);
let prove_duration = prove_start.elapsed().as_secs_f64();
let proof = SP1ProofWithIO {
stdin: SP1Stdin::new(),
public_values: SP1PublicValues::new(),
proof,
};

let verify_start = Instant::now();
client.verify_with_config(elf, &proof, config).unwrap();
// SP1ProverImpl::verify_with_config(elf, &proof, config).unwrap();
let verify_duration = verify_start.elapsed().as_secs_f64();

(execution_duration, prove_duration, verify_duration)
Expand All @@ -164,16 +156,11 @@ fn run_evaluation(hashfn: &HashFnId, program: &Program, elf: &[u8]) -> (f64, f64

let config = BabyBearPoseidon2::new();
let prove_start = Instant::now();
let proof = prove_core(config.clone(), runtime);
let _proof = prove_core(config.clone(), runtime);
let prove_duration = prove_start.elapsed().as_secs_f64();
let proof = SP1ProofWithIO {
stdin: SP1Stdin::new(),
public_values: SP1PublicValues::new(),
proof,
};

let verify_start = Instant::now();
client.verify_with_config(elf, &proof, config).unwrap();
// SP1ProverImpl::verify_with_config(elf, &proof, config).unwrap();
let verify_duration = verify_start.elapsed().as_secs_f64();

(execution_duration, prove_duration, verify_duration)
Expand All @@ -186,16 +173,11 @@ fn run_evaluation(hashfn: &HashFnId, program: &Program, elf: &[u8]) -> (f64, f64

let config = BabyBearKeccak::new();
let prove_start = Instant::now();
let proof = prove_core(config.clone(), runtime);
let _proof = prove_core(config.clone(), runtime);
let prove_duration = prove_start.elapsed().as_secs_f64();
let proof = SP1ProofWithIO {
stdin: SP1Stdin::new(),
public_values: SP1PublicValues::new(),
proof,
};

let verify_start = Instant::now();
client.verify_with_config(elf, &proof, config).unwrap();
// SP1ProverImpl::verify_with_config(elf, &proof, config).unwrap();
let verify_duration = verify_start.elapsed().as_secs_f64();

(execution_duration, prove_duration, verify_duration)
Expand Down
Loading

0 comments on commit 892db4b

Please sign in to comment.