Skip to content

Commit

Permalink
compile + clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
roynalnaruto committed Nov 8, 2024
1 parent 0b78006 commit 7bb2795
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 122 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/src/trace_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() {
let chunk = ChunkProvingTask::new(traces);
let params_map =
prover::Prover::load_params_map(&args.params_path, &prover::CHUNK_PROVER_DEGREES);
let mut prover = ChunkProverProver::from_params_and_assets(&params_map, &args.assets_path);
let mut prover = ChunkProver::from_params_and_assets(&params_map, &args.assets_path);
log::info!("Constructed chunk prover");
prove_and_verify_chunk(
&params_map,
Expand Down
38 changes: 18 additions & 20 deletions integration/src/prove.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use anyhow::Result;
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};
use prover::{
get_blob_bytes, BatchData, BatchProofV2, BatchProver, BatchProvingTask, BatchVerifier,
BundleProvingTask, ChunkInfo, ChunkProofV2, ChunkProver, ChunkProvingTask, ChunkVerifier, MAX_AGG_SNARKS,
chunk_trace_to_witness_block, get_blob_bytes, BatchData, BatchProofV2, BatchProver,
BatchProvingTask, BatchVerifier, BundleProvingTask, ChunkInfo, ChunkProofV2,
ChunkProofV2Metadata, ChunkProver, ChunkProvingTask, ChunkVerifier, Snark, MAX_AGG_SNARKS,
};
use std::{collections::BTreeMap, env, time::Instant};

use crate::verifier::EVMVerifier;
use crate::verifier::{new_chunk_verifier, EVMVerifier};

/// The `output_dir` is assumed to output_dir of chunk proving.
pub fn new_batch_prover<'a>(
Expand All @@ -21,9 +23,6 @@ pub fn new_batch_prover<'a>(
prover
}

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>);
Expand All @@ -46,8 +45,8 @@ impl<'params> SP1Prover<'params> {
chunk_identifier: &str,
sp1_snark: Snark,
output_dir: Option<&str>,
) -> Result<ChunkProof> {
use prover::config::LayerId::Layer2;
) -> Result<ChunkProofV2> {
use prover::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 {
Expand All @@ -67,18 +66,21 @@ impl<'params> SP1Prover<'params> {
)?;

let pk = self.0.prover_impl.pk(Layer2.id());
let result = ChunkProof::new(comp_snark, pk, chunk_info, chunk.chunk_kind, Vec::new());
let proof_metadata =
ChunkProofV2Metadata::new(&comp_snark, prover::ChunkKind::Sp1, chunk_info, None)?;
let proof = ChunkProofV2::new(comp_snark, pk, proof_metadata)?;

// in case we read the snark directly from previous calculation,
// the pk is not avaliable and we skip dumping the proof
if pk.is_some() {
if let (Some(output_dir), Ok(proof)) = (output_dir, &result) {
if let Some(output_dir) = output_dir {
proof.dump(output_dir, chunk_identifier)?;
}
} else {
log::info!("skip dumping vk since snark is restore from disk")
}
result

Ok(proof)
}
}

Expand All @@ -90,21 +92,16 @@ pub fn prove_and_verify_sp1_chunk(
chunk: ChunkProvingTask,
prover: &mut SP1Prover,
chunk_identifier: Option<&str>,
) -> ChunkProof {
use prover::io::load_snark;
use std::path::Path;

) -> ChunkProofV2 {
let chunk_identifier =
chunk_identifier.map_or_else(|| chunk.identifier(), |name| name.to_string());

let sp1_dir = sp1_dir.unwrap_or(output_dir);
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 snark_path = std::path::Path::new(sp1_dir).join(&sp1_snark_name);
let sp1_snark = prover::read_json_deep(snark_path).expect("failed to load SNARK");
let chunk_proof = prover
.gen_chunk_proof(chunk, &chunk_identifier, sp1_snark, Some(output_dir))
.expect("cannot generate sp1 chunk snark");
Expand All @@ -119,7 +116,8 @@ pub fn prove_and_verify_sp1_chunk(
&format!("vk_chunk_{chunk_identifier}.vkey"),
);
let verifier = new_chunk_verifier(params_map, output_dir);
assert!(verifier.verify_snark(chunk_proof.clone().to_snark()));
let snark = Snark::try_from(&chunk_proof).expect("should be ok");
assert!(verifier.verify_snark(snark));
log::info!("Verified sp1 chunk proof");

chunk_proof
Expand Down
40 changes: 40 additions & 0 deletions integration/src/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
use std::path::PathBuf;

use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};
use prover::{batch_vk_filename, CompressionCircuit, Verifier};
use std::{collections::BTreeMap, env};

use prover::{force_read, DEPLOYMENT_CODE_FILENAME};

type SnarkVerifier<'a> = Verifier<'a, CompressionCircuit>;

// FIXME: why we use common::Verifier instead of ChunkVerifier here?
pub fn new_chunk_verifier<'a>(
params_map: &'a BTreeMap<u32, ParamsKZG<Bn256>>,
assets_dir: &str,
) -> SnarkVerifier<'a> {
let path = std::path::PathBuf::from(assets_dir).join(prover::chunk_vk_filename());
let raw_vk = force_read(&path);
if raw_vk.is_empty() {
panic!("empty vk read from {path:?}");
}
env::set_var("COMPRESSION_CONFIG", &*prover::LAYER2_CONFIG_PATH);
let params = params_map
.get(&prover::LAYER2_DEGREE)
.expect("should be loaded");
SnarkVerifier::from_params(params, &raw_vk)
}

#[allow(dead_code)]
pub fn new_batch_verifier<'a>(
params_map: &'a BTreeMap<u32, ParamsKZG<Bn256>>,
assets_dir: &str,
) -> SnarkVerifier<'a> {
let path = PathBuf::from(assets_dir).join(batch_vk_filename());
let raw_vk = force_read(&path);
if raw_vk.is_empty() {
panic!("empty vk read from {path:?}");
}
env::set_var("COMPRESSION_CONFIG", &*prover::LAYER4_CONFIG_PATH);
let params = params_map
.get(&prover::LAYER4_DEGREE)
.expect("should be loaded");
SnarkVerifier::from_params(params, &raw_vk)
}

#[derive(Debug)]
pub struct EVMVerifier(Vec<u8>);

Expand Down
14 changes: 7 additions & 7 deletions integration/tests/batch_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ fn test_batch_prove_verify_after_chunk_tests() {
};
use itertools::Itertools;
use prover::{
config::AGG_DEGREES, eth_types::H256, proof::ChunkProof, BatchHeader, ChunkProvingTask,
eth_types::H256, BatchHeader, ChunkProofV2, ChunkProvingTask, BATCH_PROVER_DEGREES,
};

let output_dir = init_env_and_log("batch_tests");
log::info!("Initialized ENV and created output-dir {output_dir}");

let params_map = prover::common::Prover::load_params_map(
let params_map = prover::Prover::load_params_map(
PARAMS_DIR,
&AGG_DEGREES.iter().copied().collect_vec(),
&BATCH_PROVER_DEGREES.iter().copied().collect_vec(),
);

let trace_paths_env = trace_path_for_test();
Expand All @@ -122,13 +122,13 @@ fn test_batch_prove_verify_after_chunk_tests() {
.last()
.map_or(last_block_timestamp, |tr| tr.header.timestamp.as_u64());

let task = ChunkProvingTask::new(traces, prover::ChunkKind::Halo2);
let loaded_proof = ChunkProof::from_json_file(&output_dir, &task.identifier());
let task = ChunkProvingTask::new(traces);
let loaded_proof = ChunkProofV2::from_json(&output_dir, &task.identifier());
if let Ok(proof) = loaded_proof.as_ref() {
log::info!(
"expected PI of {} is {:#x?}",
task.identifier(),
proof.chunk_info.public_input_hash(),
proof.inner.chunk_info().public_input_hash(),
);
}
loaded_proof
Expand All @@ -138,7 +138,7 @@ fn test_batch_prove_verify_after_chunk_tests() {

let chunk_infos = chunk_proofs
.iter()
.map(|proof| proof.chunk_info.clone())
.map(|proof| proof.inner.chunk_info().clone())
.collect::<Vec<_>>();

let blob_bytes = get_blob_from_chunks(&chunk_infos);
Expand Down
85 changes: 6 additions & 79 deletions integration/tests/bundle_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use integration::prove::{new_batch_prover, prove_and_verify_bundle};
use prover::{init_env_and_log, read_json, BatchProofV2, BundleProvingTask};
//use std::{fs, path::PathBuf};

#[cfg(feature = "prove_verify")]
#[test]
Expand Down Expand Up @@ -47,27 +46,26 @@ fn test_bundle_prove_verify_after_batch() {
use glob::glob;
use integration::test_util::PARAMS_DIR;
use itertools::Itertools;
use prover::{config::AGG_DEGREES, io::from_json_file, BatchProvingTask};
use prover::{read_json_deep, BatchProvingTask, BATCH_PROVER_DEGREES};

let output_dir = init_env_and_log("bundle_tests");

let mut batch_tasks = glob(&format!("{output_dir}/full_proof_batch_prove_?.json"))
.unwrap()
.into_iter()
.map(|task_path| {
from_json_file::<BatchProvingTask>(task_path.unwrap().to_str().unwrap()).unwrap()
read_json_deep::<_, BatchProvingTask>(task_path.unwrap().to_str().unwrap()).unwrap()
})
.collect::<Vec<_>>();

batch_tasks
.as_mut_slice()
.sort_by_key(|task| task.batch_header.batch_index);

let batch_proofs: Vec<BatchProof> = batch_tasks
let batch_proofs: Vec<BatchProofV2> = batch_tasks
.iter()
.map(|task| {
log::info!("local batch proof {}", task.identifier());
from_json_file(&format!(
read_json_deep(&format!(
"{output_dir}/full_proof_batch_{}.json",
task.identifier()
))
Expand All @@ -76,82 +74,11 @@ fn test_bundle_prove_verify_after_batch() {
.collect();

let bundle = BundleProvingTask { batch_proofs };
let params_map = prover::common::Prover::load_params_map(
let params_map = prover::Prover::load_params_map(
PARAMS_DIR,
&AGG_DEGREES.iter().copied().collect_vec(),
&BATCH_PROVER_DEGREES.iter().copied().collect_vec(),
);

let mut prover = new_batch_prover(&params_map, &output_dir);
prove_and_verify_bundle(&output_dir, &mut prover, bundle);
}

#[ignore]
#[test]
fn test_evm_verifier_new_revm() {
use prover::io::read_all;
use revm::{
primitives::{CreateScheme, ExecutionResult, Output, TransactTo, TxEnv},
InMemoryDB, EVM,
};

let output_dir = init_env_and_log("test_evm_verifer");

let bytecode = read_all(&format!("{output_dir}/evm_verifier.bin"));
log::info!("bytecode len {}", bytecode.len());

let mut evm = EVM {
env: Default::default(),
db: Some(InMemoryDB::default()),
};

// only deployment code
evm.env.tx = TxEnv {
gas_limit: u64::MAX,
transact_to: TransactTo::Create(CreateScheme::Create),
data: bytecode.into(),
..Default::default()
};

let result = evm.transact_commit().unwrap();
let contract = match result {
ExecutionResult::Success {
output: Output::Create(_, Some(contract)),
..
} => contract,
ExecutionResult::Revert { gas_used, output } => {
panic!(
"Contract deployment transaction reverts with gas_used {gas_used} and output {:#x}",
output
)
}
ExecutionResult::Halt { reason, gas_used } => panic!(
"Contract deployment transaction halts unexpectedly with gas_used {gas_used} and reason {:?}",
reason
),
_ => unreachable!(),
};

log::info!("contrace done at {}", contract);
}

#[ignore]
#[test]
fn test_evm_verifier_old_revm() {
use prover::{eth_types::Address, io::read_all};
use snark_verifier::loader::evm::ExecutorBuilder;

let output_dir = init_env_and_log("test_evm_verifer");

let bytecode = read_all(&format!("{output_dir}/evm_verifier.bin"));
log::info!("bytecode len {}", bytecode.len());

let mut evm = ExecutorBuilder::default()
.with_gas_limit(u64::MAX.into())
.build();

let caller = Address::from_low_u64_be(0xfe);
let deploy_result = evm.deploy(caller, bytecode.into(), 0.into());

log::info!("exit reason = {:?}", deploy_result.exit_reason);
log::info!("reverted = {:?}", deploy_result.reverted);
}
6 changes: 3 additions & 3 deletions integration/tests/chunk_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ fn test_chunk_prove_verify() {

let params_map = prover::Prover::load_params_map(
PARAMS_DIR,
&ZKEVM_DEGREES.iter().copied().collect_vec(),
&CHUNK_PROVER_DEGREES.iter().copied().collect_vec(),
);

let trace_path = trace_path_for_test();
let traces = load_chunk(&trace_path).1;
let chunk = ChunkProvingTask::new(traces, prover::ChunkKind::Halo2);
let mut prover = ChunkProver::from_params_and_assets(&params_map, ASSETS_DIR);
let chunk = ChunkProvingTask::new(traces);
let mut prover = prover::ChunkProver::from_params_and_assets(&params_map, ASSETS_DIR);
log::info!("Constructed chunk prover");
prove_and_verify_chunk(&params_map, &output_dir, chunk, &mut prover, None, true);
}
Expand Down
Loading

0 comments on commit 7bb2795

Please sign in to comment.