Skip to content

Commit

Permalink
Merge branch 'main' into feat/hybrid-snark-agg
Browse files Browse the repository at this point in the history
  • Loading branch information
roynalnaruto authored Nov 8, 2024
2 parents 42eb985 + 66b0aee commit 0b78006
Show file tree
Hide file tree
Showing 23 changed files with 453 additions and 703 deletions.
628 changes: 256 additions & 372 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [
]

[workspace.package]
version = "0.13.0"
version = "0.14.0"
edition = "2021"
license = "MIT OR Apache-2.0"

Expand All @@ -27,7 +27,7 @@ serde_json = "1.0"
tokio = { version = "1.32", features = ["full"] }

halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.1" }
prover = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "feat/hybrid-snark-agg", default-features = false, features = ["parallel_syn", "scroll"] }
prover = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "develop", default-features = false, features = ["parallel_syn", "scroll"] }
integration = { path = "integration" }

[patch.crates-io]
Expand Down
22 changes: 10 additions & 12 deletions bin/src/chain_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ use integration::{
l2geth,
};
use prover::{
aggregator,
utils::init_env_and_log,
zkevm::{circuit::block_traces_to_witness_block, CircuitCapacityChecker, RowUsage},
BatchData, BlockTrace, ChunkInfo, ChunkProof, MAX_AGG_SNARKS,
eth_types::l2_types::BlockTrace, init_env_and_log, BatchData, ChunkInfo, ChunkProofV2,
CircuitCapacityChecker, RowUsage, MAX_AGG_SNARKS,
};
use std::env;

Expand Down Expand Up @@ -81,7 +79,7 @@ impl BatchBuilder {
}

let batch_bytes = self.batch_data.get_batch_data_bytes();
let blob_bytes = aggregator::eip4844::get_blob_bytes(&batch_bytes);
let blob_bytes = prover::get_blob_bytes(&batch_bytes);
let compressed_da_size = blob_bytes.len();
let uncompressed_da_size = self
.batch_data
Expand Down Expand Up @@ -141,7 +139,6 @@ impl ChunkBuilder {
// Condition2: ccc
let ccc_result = {
let mut checker = CircuitCapacityChecker::new();
checker.set_light_mode(false);
checker.estimate_circuit_capacity(trace.clone()).unwrap()
};
self.acc_row_usage_normalized.add(&ccc_result);
Expand Down Expand Up @@ -195,15 +192,15 @@ async fn prove_by_block(l2geth: &l2geth::Client, begin_block: i64, end_block: i6
unimplemented!("uncomment below");
//ChunkInfo::from_block_traces(&chunk)
} else {
let witness_block = block_traces_to_witness_block(chunk).unwrap();
let witness_block = prover::chunk_trace_to_witness_block(chunk).unwrap();
ChunkInfo::from_witness_block(&witness_block, false)
};
if let Some(batch) = batch_builder.add(chunk_info) {
let mut padded_batch = batch.clone();
padding_chunk(&mut padded_batch);
let batch_data = BatchData::<{ MAX_AGG_SNARKS }>::new(batch.len(), &padded_batch);
let compressed_da_size =
aggregator::eip4844::get_blob_bytes(&batch_data.get_batch_data_bytes()).len();
prover::get_blob_bytes(&batch_data.get_batch_data_bytes()).len();
log::info!(
"batch built: blob usage {:.3}, chunk num {}, block num {}, block range {} to {}",
compressed_da_size as f32 / constants::N_BLOB_BYTES as f32,
Expand Down Expand Up @@ -235,7 +232,11 @@ fn padding_chunk(chunks: &mut Vec<ChunkInfo>) {
}
}

fn prove_chunk(batch_id: u64, chunk_id: u64, block_traces: Vec<BlockTrace>) -> Option<ChunkProof> {
fn prove_chunk(
batch_id: u64,
chunk_id: u64,
block_traces: Vec<BlockTrace>,
) -> Option<ChunkProofV2> {
let total_gas: u64 = block_traces
.iter()
.map(|b| b.header.gas_used.as_u64())
Expand Down Expand Up @@ -320,8 +321,6 @@ async fn prove_by_batch(
}
}

// Make sure tx-by-tx light_mode=false row usage >= real row usage

async fn txtx_ccc(l2geth: &l2geth::Client, begin_block: i64, end_block: i64) {
let (begin_block, end_block) = if begin_block == 0 && end_block == 0 {
// Blocks within last 24 hours
Expand Down Expand Up @@ -354,7 +353,6 @@ async fn txtx_ccc(l2geth: &l2geth::Client, begin_block: i64, end_block: i64) {
// part2: tx by tx row usage
let tx_num = tx_traces.len();
let mut checker = CircuitCapacityChecker::new();
checker.light_mode = false;
let start_time = std::time::Instant::now();
for tx in tx_traces {
checker.estimate_circuit_capacity(tx).unwrap();
Expand Down
28 changes: 10 additions & 18 deletions bin/src/prove_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use prover::{BlockTrace, ChunkProof};
use prover::{eth_types::l2_types::BlockTrace, ChunkProofV2};
use std::panic::{catch_unwind, AssertUnwindSafe};

#[cfg(feature = "batch-prove")]
Expand All @@ -7,7 +7,7 @@ use prover::{BatchHeader, MAX_AGG_SNARKS};
#[cfg(feature = "batch-prove")]
pub fn prove_batch(
id: &str,
chunk_proofs: Vec<ChunkProof>,
chunk_proofs: Vec<ChunkProofV2>,
batch_header: BatchHeader<MAX_AGG_SNARKS>,
) {
use integration::prove::get_blob_from_chunks;
Expand All @@ -16,15 +16,15 @@ pub fn prove_batch(

let chunk_infos = chunk_proofs
.iter()
.map(|p| p.chunk_info.clone())
.map(|p| p.inner.chunk_info().clone())
.collect_vec();
let blob_bytes = get_blob_from_chunks(&chunk_infos);
let batch = BatchProvingTask {
chunk_proofs,
batch_header,
blob_bytes,
};
let result = catch_unwind(AssertUnwindSafe(|| prover::test::batch_prove(id, batch)));
let result = catch_unwind(AssertUnwindSafe(|| prover::batch_prove(id, batch)));

match result {
Ok(_) => log::info!("{id}: succeeded to prove batch"),
Expand All @@ -41,21 +41,15 @@ pub fn prove_batch(
}
}

pub fn prove_chunk(id: &str, traces: Vec<BlockTrace>) -> Option<ChunkProof> {
pub fn prove_chunk(id: &str, traces: Vec<BlockTrace>) -> Option<ChunkProofV2> {
let result = catch_unwind(AssertUnwindSafe(|| {
#[cfg(not(feature = "chunk-prove"))]
let proof = None::<ChunkProof>;
let proof = None::<ChunkProofV2>;

#[cfg(feature = "inner-prove")]
{
let witness_block =
prover::zkevm::circuit::block_traces_to_witness_block(traces.clone()).unwrap();
prover::test::inner_prove(id, &witness_block);
}
#[cfg(feature = "chunk-prove")]
let proof = Some(prover::test::chunk_prove(
let proof = Some(prover::chunk_prove(
id,
prover::ChunkProvingTask::new(traces, prover::ChunkKind::Halo2),
prover::ChunkProvingTask::new(traces),
));
#[cfg(not(any(feature = "inner-prove", feature = "chunk-prove")))]
mock_prove(id, traces);
Expand Down Expand Up @@ -84,12 +78,10 @@ pub fn prove_chunk(id: &str, traces: Vec<BlockTrace>) -> Option<ChunkProof> {
}

#[cfg(not(any(feature = "inner-prove", feature = "chunk-prove")))]
fn mock_prove(id: &str, traces: Vec<BlockTrace>) {
use prover::{inner::Prover, zkevm::circuit::SuperCircuit};

fn mock_prove(id: &str, traces: Vec<prover::eth_types::l2_types::BlockTrace>) {
log::info!("{id}: mock-prove BEGIN");

Prover::<SuperCircuit>::mock_prove_target_circuit_chunk(traces)
integration::mock::mock_prove_target_circuit_chunk(traces)
.unwrap_or_else(|err| panic!("{id}: failed to mock-prove: {err}"));

log::info!("{id}: mock-prove END");
Expand Down
16 changes: 5 additions & 11 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, zkevm::Prover, ChunkProvingTask};
use prover::{init_env_and_log, ChunkProver, ChunkProvingTask};
use std::env;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -31,16 +31,10 @@ fn main() {

let traces = load_chunk(&args.trace_path).1;
prover::eth_types::constants::set_scroll_block_constants_with_trace(&traces[0]);
let chunk = ChunkProvingTask::new(traces, prover::ChunkKind::Halo2);
let params_map = prover::common::Prover::load_params_map(
&args.params_path,
&[
*prover::config::INNER_DEGREE,
*prover::config::LAYER1_DEGREE,
*prover::config::LAYER2_DEGREE,
],
);
let mut prover = Prover::from_params_and_assets(&params_map, &args.assets_path);
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);
log::info!("Constructed chunk prover");
prove_and_verify_chunk(
&params_map,
Expand Down
6 changes: 3 additions & 3 deletions integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ serde_json.workspace = true
serde_derive.workspace = true
tokio.workspace = true

revm = { version = "3.5.0", default-features = false, features = ["std"] }
snark-verifier = { git = "https://github.com/scroll-tech/snark-verifier", branch = "v0.1" }
snark-verifier-sdk = { git = "https://github.com/scroll-tech/snark-verifier", branch = "v0.1", default-features = false, features = ["loader_halo2", "loader_evm", "halo2-pse"] }
snark-verifier = { git = "https://github.com/scroll-tech/snark-verifier", branch = "develop" }
snark-verifier-sdk = { git = "https://github.com/scroll-tech/snark-verifier", branch = "develop", default-features = false, features = ["loader_halo2", "loader_evm", "halo2-pse"] }

halo2_proofs.workspace = true
prover.workspace = true

[features]
default = ["prove_verify"]
prove_verify = []
fix_later = [] # commented codes
2 changes: 1 addition & 1 deletion integration/configs/layer3.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"strategy": "Simple",
"degree": 21,
"num_advice": [
63
85
],
"num_lookup_advice": [
8
Expand Down
47 changes: 8 additions & 39 deletions integration/src/capacity_checker.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
use itertools::Itertools;
use prover::{
zkevm::{
circuit::{block_traces_to_witness_block, calculate_row_usage_of_witness_block},
CircuitCapacityChecker, RowUsage, SubCircuitRowUsage,
},
zkevm_circuits::evm_circuit::ExecutionState,
BlockTrace,
calculate_row_usage_of_witness_block, chunk_trace_to_witness_block,
eth_types::l2_types::BlockTrace, zkevm_circuits::evm_circuit::ExecutionState,
CircuitCapacityChecker, RowUsage, SubCircuitRowUsage,
};
use std::time::Duration;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CCCMode {
Optimal,
Siger,
FollowerLight,
FollowerFull,
}

Expand All @@ -39,7 +35,6 @@ pub fn run_circuit_capacity_checker(
match mode {
CCCMode::Optimal => ccc_by_chunk(batch_id, chunk_id, block_traces),
CCCMode::Siger => ccc_as_signer(chunk_id, block_traces),
CCCMode::FollowerLight => ccc_as_follower_light(chunk_id, block_traces),
CCCMode::FollowerFull => ccc_as_follower_full(chunk_id, block_traces),
},
)
Expand Down Expand Up @@ -155,7 +150,6 @@ fn get_ccc_result_of_chunk(
blocks: &[BlockTrace],
by_block: bool, // by block instead of by tx
norm: bool,
light_mode: bool,
tag: &str,
) -> (RowUsage, Duration) {
log::info!(
Expand All @@ -167,11 +161,6 @@ fn get_ccc_result_of_chunk(
);

let mut checker = CircuitCapacityChecker::new();
checker.light_mode = light_mode;

if !checker.light_mode && !by_block {
unimplemented!("!checker.light_mode && !by_block")
}

let start_time = std::time::Instant::now();

Expand Down Expand Up @@ -221,29 +210,14 @@ fn get_ccc_result_of_chunk(
}

#[allow(dead_code)]
fn get_ccc_result_by_whole_block(
chunk_id: u64,
light_mode: bool,
blocks: &[BlockTrace],
) -> RowUsage {
log::info!("estimating circuit rows whole block, light_mode {light_mode}");
fn get_ccc_result_by_whole_block(chunk_id: u64, blocks: &[BlockTrace]) -> RowUsage {
let mut checker = CircuitCapacityChecker::new();
checker.light_mode = light_mode;

for block in blocks {
checker.estimate_circuit_capacity(block.clone()).unwrap();
}
let ccc_result = checker.get_acc_row_usage(false);
pretty_print_row_usage(
&ccc_result,
blocks,
chunk_id,
if light_mode {
"block-light"
} else {
"block-full"
},
);
pretty_print_row_usage(&ccc_result, blocks, chunk_id, "block-full");

ccc_result
}
Expand Down Expand Up @@ -285,7 +259,7 @@ pub fn ccc_by_chunk(
log::info!("ccc_by_chunk: run ccc for batch-{batch_id} chunk-{chunk_id}");

let start_time = std::time::Instant::now();
let witness_block = block_traces_to_witness_block(Vec::from(block_traces)).unwrap();
let witness_block = chunk_trace_to_witness_block(Vec::from(block_traces)).unwrap();
let rows = calculate_row_usage_of_witness_block(&witness_block).unwrap();
let row_usage = RowUsage::from_row_usage_details(rows);
pretty_print_row_usage(&row_usage, block_traces, chunk_id, "chunk-opt");
Expand All @@ -298,14 +272,9 @@ pub fn ccc_by_chunk(
}

pub fn ccc_as_signer(chunk_id: u64, blocks: &[BlockTrace]) -> (RowUsage, Duration) {
get_ccc_result_of_chunk(chunk_id, blocks, false, false, true, "chunk-signer")
}

/// current stats inside db
pub fn ccc_as_follower_light(chunk_id: u64, blocks: &[BlockTrace]) -> (RowUsage, Duration) {
get_ccc_result_of_chunk(chunk_id, blocks, true, false, true, "chunk-f-l")
get_ccc_result_of_chunk(chunk_id, blocks, false, false, "chunk-signer")
}

pub fn ccc_as_follower_full(chunk_id: u64, blocks: &[BlockTrace]) -> (RowUsage, Duration) {
get_ccc_result_of_chunk(chunk_id, blocks, true, false, false, "chunk-f-f")
get_ccc_result_of_chunk(chunk_id, blocks, true, false, "chunk-f-f")
}
59 changes: 0 additions & 59 deletions integration/src/evm.rs

This file was deleted.

Loading

0 comments on commit 0b78006

Please sign in to comment.