Skip to content

Commit

Permalink
feat: v4
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani committed Dec 21, 2024
1 parent fbc15cf commit e8680e1
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 575 deletions.
619 changes: 218 additions & 401 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,10 @@ op-alloy-rpc-types-engine = { version = "0.6.8", default-features = false }
op-alloy-network = { version = "0.6.8", default-features = false }

# sp1
sp1-lib = { version = "3.4.0", features = ["verify"] }
# Note: This rev includes the SP1 mock groth16 bytes fix, skipping deferred verification, executor opts and network-v2 changes.
# ratan/v3.4.0-with-additions includes the network-v2 changes from `19ae3e1`. Has the latest FD's for ECDSA patch.
sp1-sdk = { git = "https://github.com/succinctlabs/sp1.git", rev = "f0b61cf262388c1be7edfcf984c8e070064e980f", features = [
"network-v2",
] }
sp1-zkvm = { version = "3.4.0", features = ["verify"] }
sp1-build = { version = "3.4.0" }
sp1-lib = { git = "https://github.com/succinctlabs/sp1.git", branch = "dev", features = ["verify"] }
sp1-sdk = { git = "https://github.com/succinctlabs/sp1.git", branch = "dev" }
sp1-zkvm = { git = "https://github.com/succinctlabs/sp1.git", branch = "dev", features = ["verify"] }
sp1-build = { git = "https://github.com/succinctlabs/sp1.git", branch = "dev"}

[profile.release-client-lto]
inherits = "release"
Expand Down
117 changes: 28 additions & 89 deletions proposer/succinct/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ use op_succinct_proposer::{
ValidateConfigRequest, ValidateConfigResponse,
};
use sp1_sdk::{
network_v2::{
client::NetworkClient,
proto::network::{ExecutionStatus, FulfillmentStatus, FulfillmentStrategy, ProofMode},
network::{
proto::network::{ExecutionStatus, FulfillmentStatus},
FulfillmentStrategy,
},
utils, HashableKey, NetworkProverV2, ProverClient, SP1Proof, SP1ProofWithPublicValues,
utils, HashableKey, ProverClient, SP1Proof, SP1ProofWithPublicValues,
};
use std::{env, str::FromStr, time::Duration};
use tower_http::limit::RequestBodyLimitLayer;
Expand All @@ -41,7 +41,7 @@ async fn main() -> Result<()> {

dotenv::dotenv().ok();

let prover = ProverClient::new();
let prover = ProverClient::from_env();
let (range_pk, range_vk) = prover.setup(RANGE_ELF);
let (agg_pk, agg_vk) = prover.setup(AGG_ELF);
let multi_block_vkey_u8 = u32_to_u8(range_vk.vk.hash_u32());
Expand Down Expand Up @@ -179,59 +179,19 @@ async fn request_span_proof(
}
};

let private_key = match env::var("SP1_PRIVATE_KEY") {
Ok(private_key) => private_key,
Err(e) => {
error!("Failed to get SP1 private key: {}", e);
return Err(AppError(anyhow::anyhow!(
"Failed to get SP1 private key: {}",
e
)));
}
};
let rpc_url = match env::var("PROVER_NETWORK_RPC") {
Ok(rpc_url) => rpc_url,
Err(e) => {
error!("Failed to get PROVER_NETWORK_RPC: {}", e);
return Err(AppError(anyhow::anyhow!(
"Failed to get PROVER_NETWORK_RPC: {}",
e
)));
}
};
let mut prover = NetworkProverV2::new(&private_key, Some(rpc_url.to_string()), false);
// Use the reserved strategy to route to a specific cluster.
prover.with_strategy(FulfillmentStrategy::Reserved);

// Set simulation to false on range proofs as they're large.
env::set_var("SKIP_SIMULATION", "true");
let vk_hash = match prover.register_program(&state.range_vk, RANGE_ELF).await {
Ok(vk_hash) => vk_hash,
Err(e) => {
error!("Failed to register program: {}", e);
return Err(AppError(anyhow::anyhow!(
"Failed to register program: {}",
e
)));
}
};
let proof_id = match prover
.request_proof(
&vk_hash,
&sp1_stdin,
ProofMode::Compressed,
1_000_000_000_000,
None,
)
let client = ProverClient::builder().network().build();
let proof_id = client
.prove(&state.range_pk, &sp1_stdin)
.compressed()
.strategy(FulfillmentStrategy::Reserved)
.skip_simulation(true)
.cycle_limit(1_000_000_000_000)
.request()
.await
{
Ok(proof_id) => proof_id,
Err(e) => {
.map_err(|e| {
error!("Failed to request proof: {}", e);
return Err(AppError(anyhow::anyhow!("Failed to request proof: {}", e)));
}
};
env::set_var("SKIP_SIMULATION", "false");
AppError(anyhow::anyhow!("Failed to request proof: {}", e))
})?;

Ok((StatusCode::OK, Json(ProofResponse { proof_id })))
}
Expand Down Expand Up @@ -285,11 +245,7 @@ async fn request_agg_proof(
}
};

let private_key = env::var("SP1_PRIVATE_KEY")?;
let rpc_url = env::var("PROVER_NETWORK_RPC")?;
let mut prover = NetworkProverV2::new(&private_key, Some(rpc_url.to_string()), false);
// Use the reserved strategy to route to a specific cluster.
prover.with_strategy(FulfillmentStrategy::Reserved);
let prover = ProverClient::builder().network().build();

let stdin =
match get_agg_proof_stdin(proofs, boot_infos, headers, &state.range_vk, l1_head.into()) {
Expand All @@ -303,24 +259,11 @@ async fn request_agg_proof(
}
};

let vk_hash = match prover.register_program(&state.agg_vk, AGG_ELF).await {
Ok(vk_hash) => vk_hash,
Err(e) => {
error!("Failed to register program: {}", e);
return Err(AppError(anyhow::anyhow!(
"Failed to register program: {}",
e
)));
}
};
let proof_id = match prover
.request_proof(
&vk_hash,
&stdin,
ProofMode::Groth16,
1_000_000_000_000,
None,
)
.prove(&state.agg_pk, &stdin)
.groth16()
.strategy(FulfillmentStrategy::Reserved)
.request()
.await
{
Ok(id) => id,
Expand Down Expand Up @@ -388,10 +331,9 @@ async fn request_mock_span_proof(
}
};

let prover = ProverClient::mock();
let prover = ProverClient::builder().mock().build();
let proof = prover
.prove(&state.range_pk, sp1_stdin)
.set_skip_deferred_proof_verification(true)
.prove(&state.range_pk, &sp1_stdin)
.compressed()
.run()?;

Expand Down Expand Up @@ -462,7 +404,7 @@ async fn request_mock_agg_proof(
}
};

let prover = ProverClient::mock();
let prover = ProverClient::builder().mock().build();

let stdin =
match get_agg_proof_stdin(proofs, boot_infos, headers, &state.range_vk, l1_head.into()) {
Expand All @@ -475,9 +417,9 @@ async fn request_mock_agg_proof(

// Simulate the mock proof. proof.bytes() returns an empty byte array for mock proofs.
let proof = match prover
.prove(&state.agg_pk, stdin)
.set_skip_deferred_proof_verification(true)
.prove(&state.agg_pk, &stdin)
.groth16()
.deferred_proof_verification(false)
.run()
{
Ok(p) => p,
Expand All @@ -502,18 +444,15 @@ async fn get_proof_status(
Path(proof_id): Path<String>,
) -> Result<(StatusCode, Json<ProofStatus>), AppError> {
info!("Received proof status request: {:?}", proof_id);
let private_key = env::var("SP1_PRIVATE_KEY")?;
let rpc_url = env::var("PROVER_NETWORK_RPC")?;

let client = NetworkClient::new(&private_key, Some(rpc_url.to_string()));
let client = ProverClient::builder().network().build();

let proof_id_bytes = hex::decode(proof_id)?;

// Time out this request if it takes too long.
let timeout = Duration::from_secs(10);
let (status, maybe_proof) =
match tokio::time::timeout(timeout, client.get_proof_request_status(&proof_id_bytes)).await
{
match tokio::time::timeout(timeout, client.get_proof_status(&proof_id_bytes)).await {
Ok(Ok(result)) => result,
Ok(Err(_)) => {
return Ok((
Expand Down
1 change: 1 addition & 0 deletions scripts/prove/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ kona-host = { workspace = true }
# local
op-succinct-host-utils.workspace = true
op-succinct-client-utils.workspace = true
op-succinct-scripts = { path = "../utils" }

# op-alloy
op-alloy-genesis.workspace = true
Expand Down
11 changes: 6 additions & 5 deletions scripts/prove/bin/agg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ struct Args {
fn load_aggregation_proof_data(
proof_names: Vec<String>,
range_vkey: &SP1VerifyingKey,
prover: &ProverClient,
) -> (Vec<SP1Proof>, Vec<BootInfoStruct>) {
let metadata = MetadataCommand::new().exec().unwrap();
let workspace_root = metadata.workspace_root;
Expand All @@ -42,6 +41,8 @@ fn load_aggregation_proof_data(
let mut proofs = Vec::with_capacity(proof_names.len());
let mut boot_infos = Vec::with_capacity(proof_names.len());

let prover = ProverClient::from_env();

for proof_name in proof_names.iter() {
let proof_path = format!("{}/{}.bin", proof_directory, proof_name);
if fs::metadata(&proof_path).is_err() {
Expand Down Expand Up @@ -71,12 +72,12 @@ async fn main() -> Result<()> {

dotenv::from_filename(args.env_file).ok();

let prover = ProverClient::new();
let prover = ProverClient::from_env();
let fetcher = OPSuccinctDataFetcher::new_with_rollup_config(RunContext::Dev).await?;

let (_, vkey) = prover.setup(RANGE_ELF);

let (proofs, boot_infos) = load_aggregation_proof_data(args.proofs, &vkey, &prover);
let (proofs, boot_infos) = load_aggregation_proof_data(args.proofs, &vkey);

let header = fetcher.get_latest_l1_head_in_batch(&boot_infos).await?;
let headers = fetcher
Expand All @@ -96,12 +97,12 @@ async fn main() -> Result<()> {

if args.prove {
prover
.prove(&agg_pk, stdin)
.prove(&agg_pk, &stdin)
.groth16()
.run()
.expect("proving failed");
} else {
let (_, report) = prover.execute(AGG_ELF, stdin).run().unwrap();
let (_, report) = prover.execute(AGG_ELF, &stdin).run().unwrap();
println!("report: {:?}", report);
}

Expand Down
54 changes: 13 additions & 41 deletions scripts/prove/bin/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,14 @@ use op_succinct_host_utils::{
ProgramType,
};
use op_succinct_prove::{execute_multi, generate_witness, DEFAULT_RANGE, RANGE_ELF};
use sp1_sdk::{utils, ProverClient};
use std::{fs, path::PathBuf, time::Duration};

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Start L2 block number.
#[arg(short, long)]
start: Option<u64>,

/// End L2 block number.
#[arg(short, long)]
end: Option<u64>,

/// Verbosity level.
#[arg(short, long, default_value = "0")]
verbosity: u8,

/// Skip running native execution.
#[arg(short, long)]
use_cache: bool,

/// Generate proof.
#[arg(short, long)]
prove: bool,

/// Env file.
#[arg(long, default_value = ".env")]
env_file: PathBuf,
}
use op_succinct_scripts::HostExecutorArgs;
use sp1_sdk::{network::FulfillmentStrategy, utils, Prover, ProverClient};
use std::{fs, time::Duration};

/// Execute the OP Succinct program for multiple blocks.
#[tokio::main]
async fn main() -> Result<()> {
let args = Args::parse();
let args = HostExecutorArgs::parse();

dotenv::from_path(&args.env_file)?;
utils::setup_logger();
Expand Down Expand Up @@ -73,14 +46,19 @@ async fn main() -> Result<()> {
// Get the stdin for the block.
let sp1_stdin = get_proof_stdin(&host_cli)?;

let prover = ProverClient::new();
let prover = ProverClient::builder().network().build();

if args.prove {
// If the prove flag is set, generate a proof.
let (pk, _) = prover.setup(RANGE_ELF);

// Generate proofs in compressed mode for aggregation verification.
let proof = prover.prove(&pk, sp1_stdin).compressed().run().unwrap();
let proof = prover
.prove(&pk, &sp1_stdin)
.compressed()
.strategy(FulfillmentStrategy::Reserved)
.run()
.unwrap();

// Create a proof directory for the chain ID if it doesn't exist.
let proof_dir = format!(
Expand All @@ -100,14 +78,8 @@ async fn main() -> Result<()> {
} else {
let l2_chain_id = data_fetcher.get_l2_chain_id().await?;

let (block_data, report, execution_duration) = execute_multi(
&prover,
&data_fetcher,
sp1_stdin,
l2_start_block,
l2_end_block,
)
.await?;
let (block_data, report, execution_duration) =
execute_multi(&data_fetcher, sp1_stdin, l2_start_block, l2_end_block).await?;

let stats = ExecutionStats::new(
&block_data,
Expand Down
4 changes: 2 additions & 2 deletions scripts/prove/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ pub async fn generate_witness(host_cli: &HostCli) -> Result<Duration> {
}

pub async fn execute_multi(
prover: &ProverClient,
data_fetcher: &OPSuccinctDataFetcher,
sp1_stdin: SP1Stdin,
l2_start_block: u64,
l2_end_block: u64,
) -> Result<(Vec<BlockInfo>, ExecutionReport, Duration)> {
let start_time = Instant::now();
let (_, report) = prover.execute(RANGE_ELF, sp1_stdin.clone()).run().unwrap();
let prover = ProverClient::from_env();
let (_, report) = prover.execute(RANGE_ELF, &sp1_stdin).run().unwrap();
let execution_duration = start_time.elapsed();

let block_data = data_fetcher
Expand Down
13 changes: 2 additions & 11 deletions scripts/prove/tests/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use op_succinct_host_utils::{
ProgramType,
};
use op_succinct_prove::{execute_multi, generate_witness, DEFAULT_RANGE, ONE_HOUR};
use sp1_sdk::ProverClient;

mod common;

Expand Down Expand Up @@ -36,16 +35,8 @@ async fn execute_batch() -> Result<()> {
// Get the stdin for the block.
let sp1_stdin = get_proof_stdin(&host_cli)?;

let prover = ProverClient::new();

let (block_data, report, execution_duration) = execute_multi(
&prover,
&data_fetcher,
sp1_stdin,
l2_start_block,
l2_end_block,
)
.await?;
let (block_data, report, execution_duration) =
execute_multi(&data_fetcher, sp1_stdin, l2_start_block, l2_end_block).await?;

let stats = ExecutionStats::new(
&block_data,
Expand Down
Loading

0 comments on commit e8680e1

Please sign in to comment.