From 56406638f81dd153b31624fb27ba364f1ba407f6 Mon Sep 17 00:00:00 2001 From: Zhang Zhuo Date: Thu, 7 Nov 2024 14:47:23 +0800 Subject: [PATCH] fix building --- aggregator/src/blob_consistency.rs | 4 +- aggregator/src/blob_consistency/avail.rs | 5 ++ aggregator/src/blob_consistency/eip4844.rs | 1 - aggregator/src/lib.rs | 1 + prover/src/aggregator/mod.rs | 2 +- prover/src/aggregator/prover.rs | 25 ++++---- prover/src/inner/mod.rs | 5 -- prover/src/inner/prover/mock.rs | 43 ------------- prover/src/inner/prover/mod.rs | 70 ---------------------- prover/src/inner/verifier.rs | 42 ------------- prover/src/lib.rs | 10 +++- prover/src/proof/chunk.rs | 2 +- prover/src/proof/proof_v2.rs | 12 ++++ prover/src/types.rs | 16 +++-- 14 files changed, 55 insertions(+), 183 deletions(-) delete mode 100644 prover/src/inner/mod.rs delete mode 100644 prover/src/inner/prover/mock.rs delete mode 100644 prover/src/inner/prover/mod.rs delete mode 100644 prover/src/inner/verifier.rs diff --git a/aggregator/src/blob_consistency.rs b/aggregator/src/blob_consistency.rs index b49f676011..86eabd90dc 100644 --- a/aggregator/src/blob_consistency.rs +++ b/aggregator/src/blob_consistency.rs @@ -19,9 +19,9 @@ mod eip4844; cfg_if! { if #[cfg(feature = "da-avail")] { // const DATA_AVAILABILITY: DataAvailability = DataAvailability::Avail; - pub use avail::{BlobConsistencyConfig, BlobConsistencyWitness, BLOB_WIDTH}; + pub use avail::{BlobConsistencyConfig, BlobConsistencyWitness, BLOB_WIDTH, get_blob_bytes}; } else { // const DATA_AVAILABILITY: DatayAvailability = DataAvailability::Eip4844; - pub use eip4844::{BlobConsistencyConfig, BlobConsistencyWitness, BLOB_WIDTH}; + pub use eip4844::{BlobConsistencyConfig, BlobConsistencyWitness, BLOB_WIDTH, get_blob_bytes}; } } diff --git a/aggregator/src/blob_consistency/avail.rs b/aggregator/src/blob_consistency/avail.rs index 7c7474c8be..88b93eada7 100644 --- a/aggregator/src/blob_consistency/avail.rs +++ b/aggregator/src/blob_consistency/avail.rs @@ -98,3 +98,8 @@ pub struct AssignedBarycentricEvaluationConfig { /// 32 Assigned cells representing the LE-bytes of evaluation y. pub(crate) y_le: Vec>, } + +/// Get the blob data bytes that will be populated in BlobDataConfig. +pub fn get_blob_bytes(_batch_bytes: &[u8]) -> Vec { + unimplemented!("trick for linting"); +} diff --git a/aggregator/src/blob_consistency/eip4844.rs b/aggregator/src/blob_consistency/eip4844.rs index 8958f0ffd5..3a2b81781b 100644 --- a/aggregator/src/blob_consistency/eip4844.rs +++ b/aggregator/src/blob_consistency/eip4844.rs @@ -84,7 +84,6 @@ fn kzg_to_versioned_hash(commitment: &c_kzg::KzgCommitment) -> H256 { H256::from_slice(&res[..]) } -#[cfg(test)] /// Get the blob data bytes that will be populated in BlobDataConfig. pub fn get_blob_bytes(batch_bytes: &[u8]) -> Vec { let mut blob_bytes = crate::witgen::zstd_encode(batch_bytes); diff --git a/aggregator/src/lib.rs b/aggregator/src/lib.rs index 684a33c973..4ce52cd83a 100644 --- a/aggregator/src/lib.rs +++ b/aggregator/src/lib.rs @@ -27,6 +27,7 @@ mod tests; pub use self::core::extract_proof_and_instances_with_pairing_check; pub use aggregation::*; pub use batch::{BatchHash, BatchHeader}; +pub use blob_consistency::get_blob_bytes; pub use chunk::ChunkInfo; pub use compression::*; pub use constants::MAX_AGG_SNARKS; diff --git a/prover/src/aggregator/mod.rs b/prover/src/aggregator/mod.rs index 95c49fd29e..6351df6be3 100644 --- a/prover/src/aggregator/mod.rs +++ b/prover/src/aggregator/mod.rs @@ -11,7 +11,7 @@ mod verifier; pub use verifier::Verifier; /// Re-export some types from the [`aggregator`] crate. -pub use aggregator::{BatchData, BatchHash, BatchHeader, MAX_AGG_SNARKS}; +pub use aggregator::{get_blob_bytes, BatchData, BatchHash, BatchHeader, MAX_AGG_SNARKS}; /// Alias for convenience. pub type BatchProver<'a> = Prover<'a>; diff --git a/prover/src/aggregator/prover.rs b/prover/src/aggregator/prover.rs index 7383e6fa70..3be718656a 100644 --- a/prover/src/aggregator/prover.rs +++ b/prover/src/aggregator/prover.rs @@ -16,7 +16,7 @@ use crate::{ types::BundleProvingTask, utils::{force_to_read, try_to_read}, BatchProofV2, BatchProofV2Metadata, BatchProvingTask, BundleProofV2, ChunkKind, ChunkProof, - ParamsMap, ProverError, + ChunkProofV2, ParamsMap, ProverError, }; /// Prover capable of generating [`BatchProof`] and [`BundleProof`]. @@ -197,8 +197,8 @@ impl<'params> Prover<'params> { let bundle_snarks = bundle .batch_proofs .iter() - .map(|proof| proof.into()) - .collect::>(); + .map(Snark::try_from) + .collect::, _>>()?; // Load from disk or generate a layer-5 Recursive Circuit SNARK. let layer5_snark = self @@ -266,11 +266,16 @@ impl<'params> Prover<'params> { self.check_protocol_of_chunks(&batch.chunk_proofs)?; // Split chunk info and snarks from the batch proving task. - let (mut chunk_infos, mut layer2_snarks): (Vec<_>, Vec<_>) = batch + let mut chunk_infos = batch + .chunk_proofs + .iter() + .map(|proof| proof.inner.chunk_info().clone()) + .collect::>(); + let mut layer2_snarks = batch .chunk_proofs .iter() - .map(|proof| (proof.chunk_info.clone(), proof.to_snark())) - .unzip(); + .map(Snark::try_from) + .collect::, ProverError>>()?; // Pad the SNARKs with the last SNARK until we have MAX_AGG_SNARKS number of SNARKs. if num_chunks < MAX_AGG_SNARKS { @@ -372,15 +377,15 @@ impl<'params> Prover<'params> { /// Sanity check: validate that the SNARK [`protocol`][snark_verifier::Protocol] for the SNARKs /// being aggregated by the [`BatchCircuit`][aggregator::BatchCircuit] match the expected SNARK /// protocols conditional to the chunk proof generation route utilised, i.e. halo2 or sp1. - fn check_protocol_of_chunks(&self, chunk_proofs: &[ChunkProof]) -> Result<(), ProverError> { + fn check_protocol_of_chunks(&self, chunk_proofs: &[ChunkProofV2]) -> Result<(), ProverError> { for (i, proof) in chunk_proofs.iter().enumerate() { - let expected = match proof.chunk_kind { + let expected = match proof.inner.chunk_kind() { ChunkKind::Halo2 => &self.halo2_protocol, ChunkKind::Sp1 => &self.sp1_protocol, }; - if proof.protocol.ne(expected) { + if proof.inner.protocol().ne(expected) { let expected_digest = format!("{:x}", Sha256::digest(expected)); - let found_digest = format!("{:x}", Sha256::digest(&proof.protocol)); + let found_digest = format!("{:x}", Sha256::digest(proof.inner.protocol())); log::error!( "BatchProver: SNARK protocol mismatch! index={}, expected={}, found={}", i, diff --git a/prover/src/inner/mod.rs b/prover/src/inner/mod.rs deleted file mode 100644 index 8c95b64445..0000000000 --- a/prover/src/inner/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod prover; -pub use prover::Prover as InnerProver; - -mod verifier; -pub use verifier::Verifier as InnerVerifier; diff --git a/prover/src/inner/prover/mock.rs b/prover/src/inner/prover/mock.rs deleted file mode 100644 index e02f9589a0..0000000000 --- a/prover/src/inner/prover/mock.rs +++ /dev/null @@ -1,43 +0,0 @@ -use anyhow::bail; -use eth_types::l2_types::BlockTrace; -use halo2_proofs::{dev::MockProver, halo2curves::bn256::Fr}; -use snark_verifier_sdk::CircuitExt; -use zkevm_circuits::witness::Block; - -use crate::{ - config::INNER_DEGREE, - utils::metric_of_witness_block, - zkevm::circuit::{block_traces_to_witness_block, TargetCircuit}, -}; - -impl<'params, C: TargetCircuit> super::Prover<'params, C> { - pub fn mock_prove_target_circuit(block_trace: BlockTrace) -> anyhow::Result<()> { - Self::mock_prove_target_circuit_chunk(vec![block_trace]) - } - - pub fn mock_prove_target_circuit_chunk(block_traces: Vec) -> anyhow::Result<()> { - let witness_block = block_traces_to_witness_block(block_traces)?; - Self::mock_prove_witness_block(&witness_block) - } - - pub fn mock_prove_witness_block(witness_block: &Block) -> anyhow::Result<()> { - log::info!( - "mock proving chunk, chunk metric {:?}", - metric_of_witness_block(witness_block) - ); - let circuit = C::from_witness_block(witness_block)?; - let prover = MockProver::::run(*INNER_DEGREE, &circuit, circuit.instances())?; - if let Err(errs) = prover.verify_par() { - log::error!("err num: {}", errs.len()); - for err in &errs { - log::error!("{}", err); - } - bail!("{:#?}", errs); - } - log::info!( - "mock prove done. chunk metric: {:?}", - metric_of_witness_block(witness_block), - ); - Ok(()) - } -} diff --git a/prover/src/inner/prover/mod.rs b/prover/src/inner/prover/mod.rs deleted file mode 100644 index c8f9bd96d6..0000000000 --- a/prover/src/inner/prover/mod.rs +++ /dev/null @@ -1,70 +0,0 @@ -use anyhow::Result; -use eth_types::l2_types::BlockTrace; -use snark_verifier_sdk::Snark; -use std::marker::PhantomData; - -use crate::{ - common, - config::INNER_DEGREE, - utils::gen_rng, - utils::serialize_vk, - zkevm::circuit::{chunk_trace_to_witness_block, TargetCircuit}, - Proof, -}; - -mod mock; - -#[derive(Debug)] -pub struct Prover<'params, C: TargetCircuit> { - // Make it public for testing with inner functions (unnecessary for FFI). - pub prover_impl: common::Prover<'params>, - phantom: PhantomData, -} - -impl<'params, C: TargetCircuit> From> for Prover<'params, C> { - fn from(prover_impl: common::Prover<'params>) -> Self { - Self { - prover_impl, - phantom: PhantomData, - } - } -} - -impl<'params, C: TargetCircuit> Prover<'params, C> { - pub fn degrees() -> Vec { - vec![*INNER_DEGREE] - } - - pub fn gen_inner_snark(&mut self, id: &str, block_traces: Vec) -> Result { - assert!(!block_traces.is_empty()); - let rng = gen_rng(); - let witness_block = chunk_trace_to_witness_block(block_traces)?; - self.prover_impl - .gen_inner_snark::(id, rng, &witness_block) - } - - pub fn load_or_gen_inner_proof( - &mut self, - name: &str, - id: &str, - block_traces: Vec, - output_dir: Option<&str>, - ) -> Result { - let filename = format!("{id}_{name}"); - match output_dir.and_then(|output_dir| Proof::from_json_file(output_dir, &filename).ok()) { - Some(proof) => Ok(proof), - None => { - let result = self.gen_inner_snark(id, block_traces).map(|snark| { - let raw_vk = serialize_vk(self.prover_impl.pk(id).unwrap().get_vk()); - Proof::from_snark(snark, raw_vk) - }); - - if let (Some(output_dir), Ok(proof)) = (output_dir, &result) { - proof.dump(output_dir, &filename)?; - } - - result - } - } - } -} diff --git a/prover/src/inner/verifier.rs b/prover/src/inner/verifier.rs deleted file mode 100644 index 98bd65988a..0000000000 --- a/prover/src/inner/verifier.rs +++ /dev/null @@ -1,42 +0,0 @@ -use std::collections::BTreeMap; - -use halo2_proofs::{halo2curves::bn256::Bn256, plonk::keygen_vk, poly::kzg::commitment::ParamsKZG}; -use snark_verifier_sdk::Snark; - -use crate::{common, config::INNER_DEGREE, utils::deserialize_vk, zkevm::circuit::TargetCircuit}; - -#[derive(Debug)] -pub struct Verifier<'params, C: TargetCircuit> { - // Make it public for testing with inner functions (unnecessary for FFI). - pub inner: common::Verifier<'params, C::Inner>, -} - -impl<'params, C: TargetCircuit> From> for Verifier<'params, C> { - fn from(inner: common::Verifier<'params, C::Inner>) -> Self { - Self { inner } - } -} - -impl<'params, C: TargetCircuit> Verifier<'params, C> { - pub fn from_params_map( - params_map: &'params BTreeMap>, - raw_vk: Option<&[u8]>, - ) -> Self { - let params = params_map.get(&*INNER_DEGREE).expect("should be loaded"); - - let vk = raw_vk.map_or_else( - || { - let dummy_circuit = C::dummy_inner_circuit().expect("gen dummy circuit"); - keygen_vk(params, &dummy_circuit).unwrap() - }, - deserialize_vk::, - ); - - let verifier = common::Verifier::new(params, vk); - verifier.into() - } - - pub fn verify_inner_snark(&self, snark: Snark) -> bool { - self.inner.verify_snark(snark) - } -} diff --git a/prover/src/lib.rs b/prover/src/lib.rs index 19f410bd93..48293e5c55 100644 --- a/prover/src/lib.rs +++ b/prover/src/lib.rs @@ -39,8 +39,8 @@ mod aggregator; pub use aggregator::{ - check_chunk_hashes, BatchData, BatchHash, BatchHeader, BatchProver, BatchProverError, - BatchVerifier, RecursionTask, MAX_AGG_SNARKS, + check_chunk_hashes, get_blob_bytes, BatchData, BatchHash, BatchHeader, BatchProver, + BatchProverError, BatchVerifier, RecursionTask, MAX_AGG_SNARKS, }; mod common; @@ -68,7 +68,11 @@ mod utils; pub use utils::*; mod zkevm; -pub use zkevm::{ChunkProver, ChunkProverError, ChunkVerifier, CircuitCapacityChecker}; +pub use zkevm::{ + circuit::calculate_row_usage_of_witness_block, circuit::chunk_trace_to_witness_block, + ChunkProver, ChunkProverError, ChunkVerifier, CircuitCapacityChecker, RowUsage, + SubCircuitRowUsage, +}; /// Re-export the eth-types crate. pub use eth_types; diff --git a/prover/src/proof/chunk.rs b/prover/src/proof/chunk.rs index bc69f873cf..b7f3e117db 100644 --- a/prover/src/proof/chunk.rs +++ b/prover/src/proof/chunk.rs @@ -12,7 +12,7 @@ use crate::{utils::read_json_deep, zkevm::SubCircuitRowUsage}; use super::{dump_as_json, dump_data, dump_vk, InnerProof}; /// The innermost SNARK belongs to the following variants. -#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)] pub enum ChunkKind { /// halo2-based SuperCircuit. Halo2, diff --git a/prover/src/proof/proof_v2.rs b/prover/src/proof/proof_v2.rs index b1c30df311..772125553f 100644 --- a/prover/src/proof/proof_v2.rs +++ b/prover/src/proof/proof_v2.rs @@ -265,6 +265,18 @@ impl ChunkProofV2Metadata { row_usage, }) } + /// Get the chunk info embedded + pub fn chunk_info(&self) -> &ChunkInfo { + &self.chunk_info + } + /// Get the chunk kind + pub fn chunk_kind(&self) -> ChunkKind { + self.chunk_kind + } + /// Get the chunk protocol + pub fn protocol(&self) -> &Vec { + &self.protocol + } } impl Proof for ChunkProofV2Metadata { diff --git a/prover/src/types.rs b/prover/src/types.rs index 29592ec155..6b0757d7b0 100644 --- a/prover/src/types.rs +++ b/prover/src/types.rs @@ -3,7 +3,7 @@ use eth_types::{base64, l2_types::BlockTrace}; use serde::{Deserialize, Serialize}; use zkevm_circuits::evm_circuit::witness::Block; -use crate::{BatchProof, ChunkProof}; +use crate::{BatchProofV2, ChunkProofV2}; /// Alias for convenience. pub type WitnessBlock = Block; @@ -63,7 +63,7 @@ impl ChunkProvingTask { #[derive(Debug, Clone, Deserialize, Serialize)] pub struct BatchProvingTask { /// Chunk proofs for the contiguous list of chunks within the batch. - pub chunk_proofs: Vec, + pub chunk_proofs: Vec, /// The [`BatchHeader`], as computed on-chain for this batch. /// /// Ref: https://github.com/scroll-tech/scroll-contracts/blob/2ac4f3f7e090d7127db4b13b3627cb3ce2d762bc/src/libraries/codec/BatchHeaderV3Codec.sol @@ -83,7 +83,8 @@ impl BatchProvingTask { self.chunk_proofs .last() .unwrap() - .chunk_info + .inner + .chunk_info() .public_input_hash() .to_low_u64_le() .to_string() @@ -94,7 +95,7 @@ impl BatchProvingTask { #[derive(Debug, Clone, Deserialize, Serialize)] pub struct BundleProvingTask { /// The [`BatchProofs`][BatchProof] for the contiguous list of batches to be bundled together. - pub batch_proofs: Vec, + pub batch_proofs: Vec, } impl BundleProvingTask { @@ -103,6 +104,11 @@ impl BundleProvingTask { /// This is used as a file descriptor to save to (load from) disk in order to avoid proof /// generation if the same proof/SNARK is already found on disk. pub fn identifier(&self) -> String { - self.batch_proofs.last().unwrap().batch_hash.to_string() + self.batch_proofs + .last() + .unwrap() + .inner + .batch_hash + .to_string() } }