Skip to content

Commit

Permalink
lints
Browse files Browse the repository at this point in the history
Signed-off-by: noelwei <[email protected]>
  • Loading branch information
noel2004 committed Oct 7, 2024
1 parent 37e41f6 commit 6cf60cf
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 86 deletions.
6 changes: 3 additions & 3 deletions bin/src/trace_prover.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;
use integration::{prove::prove_and_verify_chunk, test_util::load_chunk};
use prover::{utils::init_env_and_log, ChunkProvingTask, zkevm::Prover};
use prover::{utils::init_env_and_log, zkevm::Prover, ChunkProvingTask};
use std::env;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -40,8 +40,8 @@ fn main() {
*prover::config::LAYER2_DEGREE,
],
);
let mut prover = Prover::from_params_and_assets(&params_map, &args.assets_path,);
log::info!("Constructed chunk prover");
let mut prover = Prover::from_params_and_assets(&params_map, &args.assets_path);
log::info!("Constructed chunk prover");
prove_and_verify_chunk(
&params_map,
&output_dir,
Expand Down
37 changes: 16 additions & 21 deletions integration/src/prove.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};
use prover::{
aggregator::Prover as BatchProver, zkevm::Prover as ChunkProver, ChunkProof, BatchData, BatchProof,
BatchProvingTask, BundleProvingTask, ChunkInfo, ChunkProvingTask, MAX_AGG_SNARKS,
aggregator::Prover as BatchProver, zkevm::Prover as ChunkProver, BatchData, BatchProof,
BatchProvingTask, BundleProvingTask, ChunkInfo, ChunkProof, ChunkProvingTask, MAX_AGG_SNARKS,
};
use std::{collections::BTreeMap, env, time::Instant};

Expand All @@ -19,12 +19,12 @@ pub fn new_batch_prover<'a>(
prover
}

use prover::{Snark, utils::chunk_trace_to_witness_block};
use anyhow::Result;
use prover::{utils::chunk_trace_to_witness_block, Snark};

/// SP1Prover simple compress a snark from sp1, so we have
/// same snark (only different preprocess bytes) as zkevm's chunk proof
pub struct SP1Prover<'p> (ChunkProver<'p>);
pub struct SP1Prover<'p>(ChunkProver<'p>);

impl<'params> SP1Prover<'params> {
pub fn from_params_and_assets(
Expand All @@ -45,24 +45,22 @@ impl<'params> SP1Prover<'params> {
sp1_snark: Snark,
output_dir: Option<&str>,
) -> Result<ChunkProof> {

use prover::config::LayerId::Layer2;

let witness_block = chunk_trace_to_witness_block(chunk.block_traces)?;
let chunk_info =
if let Some(chunk_info_input) = chunk.chunk_info {
let chunk_info = if let Some(chunk_info_input) = chunk.chunk_info {
chunk_info_input
} else {
log::info!("gen chunk_info {chunk_identifier:?}");
ChunkInfo::from_witness_block(&witness_block, false)
};

let comp_snark = self.0.prover_impl.load_or_gen_comp_snark(
chunk_identifier,
chunk_identifier,
Layer2.id(),
false,
Layer2.degree(),
sp1_snark,
false,
Layer2.degree(),
sp1_snark,
output_dir,
)?;

Expand All @@ -74,12 +72,11 @@ impl<'params> SP1Prover<'params> {
);

if let (Some(output_dir), Ok(proof)) = (output_dir, &result) {
proof.dump(output_dir, &chunk_identifier)?;
proof.dump(output_dir, chunk_identifier)?;
}

result
result
}

}

/// prove_and_verify_sp1_chunk would expect a sp1 snark name "sp1_snark_<chunk_id>.json"
Expand All @@ -91,9 +88,8 @@ pub fn prove_and_verify_sp1_chunk(
prover: &mut SP1Prover,
chunk_identifier: Option<&str>,
) -> ChunkProof {

use prover::io::load_snark;
use std::path::Path;
use std::path::Path;

let chunk_identifier =
chunk_identifier.map_or_else(|| chunk.identifier(), |name| name.to_string());
Expand All @@ -102,9 +98,10 @@ pub fn prove_and_verify_sp1_chunk(
let sp1_snark_name = format!("sp1_snark_{}.json", chunk_identifier);

let now = Instant::now();
let sp1_snark = load_snark(
Path::new(sp1_dir).join(&sp1_snark_name).to_str().unwrap()
).ok().flatten().unwrap();
let sp1_snark = load_snark(Path::new(sp1_dir).join(&sp1_snark_name).to_str().unwrap())
.ok()
.flatten()
.unwrap();
let chunk_proof = prover
.gen_chunk_proof(chunk, &chunk_identifier, sp1_snark, Some(output_dir))
.expect("cannot generate sp1 chunk snark");
Expand All @@ -125,15 +122,13 @@ pub fn prove_and_verify_sp1_chunk(
chunk_proof
}


pub fn prove_and_verify_chunk(
params_map: &BTreeMap<u32, ParamsKZG<Bn256>>,
output_dir: &str,
chunk: ChunkProvingTask,
prover: &mut ChunkProver,
chunk_identifier: Option<&str>,
) -> ChunkProof {

let chunk_identifier =
chunk_identifier.map_or_else(|| chunk.identifier(), |name| name.to_string());

Expand Down
39 changes: 18 additions & 21 deletions integration/tests/batch_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,12 @@ fn test_batches_with_each_chunk_num_prove_verify() {
fn test_batch_prove_verify_after_chunk_tests() {
use integration::{
prove::get_blob_from_chunks,
test_util::{PARAMS_DIR, trace_path_for_test, load_chunk},
test_util::{load_chunk, trace_path_for_test, PARAMS_DIR},
};
use itertools::Itertools;
use prover::{
eth_types::H256,
BatchHeader, ChunkProvingTask,
config::AGG_DEGREES,
proof::ChunkProof};
config::AGG_DEGREES, eth_types::H256, proof::ChunkProof, BatchHeader, ChunkProvingTask,
};

let output_dir = init_env_and_log("batch_tests");
log::info!("Initialized ENV and created output-dir {output_dir}");
Expand All @@ -108,7 +106,7 @@ fn test_batch_prove_verify_after_chunk_tests() {
);

let trace_paths_env = trace_path_for_test();
let trace_paths : Vec<_> = trace_paths_env.split(';').collect();
let trace_paths: Vec<_> = trace_paths_env.split(';').collect();
log::info!("Use traces paths {trace_paths:?}");

let mut l1_message_popped = 0;
Expand All @@ -118,21 +116,18 @@ fn test_batch_prove_verify_after_chunk_tests() {
let chunk_proofs = trace_paths
.iter()
.map(|chunk_dir| load_chunk(chunk_dir).1)
.map(|traces|{
.map(|traces| {
// make use of traces before consumed by chunkproof
l1_message_popped += traces.iter().map(|tr|tr.num_l1_txs()).sum::<u64>();
last_block_timestamp = traces.last().map_or(
last_block_timestamp,
|tr|tr.header.timestamp.as_u64()
);
l1_message_popped += traces.iter().map(|tr| tr.num_l1_txs()).sum::<u64>();
last_block_timestamp = traces
.last()
.map_or(last_block_timestamp, |tr| tr.header.timestamp.as_u64());

let task = ChunkProvingTask::from(traces);
ChunkProof::from_json_file(
&output_dir,
&task.identifier(),
)
ChunkProof::from_json_file(&output_dir, &task.identifier())
})
.collect::<Result<Vec<_>, _>>().unwrap();
.collect::<Result<Vec<_>, _>>()
.unwrap();

let chunk_infos = chunk_proofs
.iter()
Expand All @@ -141,12 +136,14 @@ fn test_batch_prove_verify_after_chunk_tests() {

let blob_bytes = get_blob_from_chunks(&chunk_infos);


let batch_header = BatchHeader::construct_from_chunks(
4, 123, l1_message_popped, l1_message_popped,
4,
123,
l1_message_popped,
l1_message_popped,
H256([
0xab, 0xac, 0xad, 0xae, 0xaf, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0xab, 0xac, 0xad, 0xae, 0xaf, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
]),
last_block_timestamp,
&chunk_infos,
Expand Down
27 changes: 16 additions & 11 deletions integration/tests/chunk_tests.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use integration::test_util::{ASSETS_DIR, PARAMS_DIR};
use prover::utils::init_env_and_log;
use integration::{
prove::{prove_and_verify_chunk, prove_and_verify_sp1_chunk, SP1Prover},
test_util::{load_chunk, trace_path_for_test},
test_util::{load_chunk, trace_path_for_test, ASSETS_DIR, PARAMS_DIR},
};
use prover::{
config::ZKEVM_DEGREES, utils::init_env_and_log, zkevm::Prover as ChunkProver, ChunkProvingTask,
};
use prover::{config::ZKEVM_DEGREES, ChunkProvingTask, zkevm::Prover as ChunkProver};

#[cfg(feature = "prove_verify")]
#[test]
fn test_chunk_prove_verify() {

use itertools::Itertools;
let output_dir = init_env_and_log("chunk_tests");
log::info!("Initialized ENV and created output-dir {output_dir}");
Expand Down Expand Up @@ -48,16 +47,22 @@ fn test_sp1_chunk_prove_verify() {
let trace_asset_path = if trace_asset_path.is_dir() {
trace_asset_path.to_path_buf()
} else {
trace_asset_path.parent()
.map(|p| p.to_path_buf())
.unwrap_or_else(|| Path::new(".").to_path_buf())
trace_asset_path
.parent()
.map(|p| p.to_path_buf())
.unwrap_or_else(|| Path::new(".").to_path_buf())
};

let traces = load_chunk(&trace_path).1;
let chunk = ChunkProvingTask::from(traces);
let mut prover = SP1Prover::from_params_and_assets(&params_map, ASSETS_DIR);
log::info!("Constructed sp1 chunk prover");
prove_and_verify_sp1_chunk(&params_map, &output_dir,
Some(trace_asset_path.to_str().unwrap()), chunk, &mut prover, None);

prove_and_verify_sp1_chunk(
&params_map,
&output_dir,
Some(trace_asset_path.to_str().unwrap()),
chunk,
&mut prover,
None,
);
}
66 changes: 36 additions & 30 deletions integration/tests/e2e_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};
use integration::{
prove::{prove_and_verify_chunk, prove_and_verify_sp1_chunk, get_blob_from_chunks, SP1Prover},
prove::{get_blob_from_chunks, prove_and_verify_chunk, prove_and_verify_sp1_chunk, SP1Prover},
test_util::{load_batch, load_chunk, load_chunk_for_test, ASSETS_DIR, PARAMS_DIR},
};
use prover::{
Expand Down Expand Up @@ -43,9 +43,11 @@ fn test_e2e_prove_verify() {
.collect_vec(),
);

let chunk_paths = read_env_var("E2E_TRACE_PATHS",
"./tests/extra_traces/batch1;./tests/extra_traces/batch2".to_string());
let chunks = chunk_paths.split(';').map(|dir|load_batch(dir).unwrap());
let chunk_paths = read_env_var(
"E2E_TRACE_PATHS",
"./tests/extra_traces/batch1;./tests/extra_traces/batch2".to_string(),
);
let chunks = chunk_paths.split(';').map(|dir| load_batch(dir).unwrap());

let sp1_path = {
let p = read_env_var("SP1_PATH", String::new());
Expand All @@ -61,9 +63,13 @@ fn test_e2e_prove_verify() {
let mut batch_proofs = Vec::new();

for (i, chunk) in chunks.enumerate() {
let (batch, batch_header) =
gen_batch_proving_task(&params_map, &output_dir,
&chunk, opt_batch_header, sp1_path.as_ref().map(String::as_str));
let (batch, batch_header) = gen_batch_proving_task(
&params_map,
&output_dir,
&chunk,
opt_batch_header,
sp1_path.as_ref().map(String::as_str),
);
dump_as_json(
&output_dir,
format!("batch_prove_{}", i + 1).as_str(),
Expand Down Expand Up @@ -200,33 +206,33 @@ fn gen_batch_proving_task(
let mut zkevm_prover = SP1Prover::from_params_and_assets(params_map, ASSETS_DIR);
log::info!("Constructed sp1 prover");
chunks
.into_iter()
.map(|block_traces| {
prove_and_verify_sp1_chunk(
params_map,
output_dir,
Some(sp1_path),
ChunkProvingTask::from(block_traces),
&mut zkevm_prover,
None,
)
})
.collect::<Vec<_>>()
.into_iter()
.map(|block_traces| {
prove_and_verify_sp1_chunk(
params_map,
output_dir,
Some(sp1_path),
ChunkProvingTask::from(block_traces),
&mut zkevm_prover,
None,
)
})
.collect::<Vec<_>>()
} else {
let mut zkevm_prover = zkevm::Prover::from_params_and_assets(params_map, ASSETS_DIR);
log::info!("Constructed zkevm prover");
chunks
.into_iter()
.map(|block_traces| {
prove_and_verify_chunk(
params_map,
output_dir,
ChunkProvingTask::from(block_traces),
&mut zkevm_prover,
None,
)
})
.collect::<Vec<_>>()
.into_iter()
.map(|block_traces| {
prove_and_verify_chunk(
params_map,
output_dir,
ChunkProvingTask::from(block_traces),
&mut zkevm_prover,
None,
)
})
.collect::<Vec<_>>()
};

log::info!("Generated chunk proofs");
Expand Down

0 comments on commit 6cf60cf

Please sign in to comment.