Skip to content

Commit

Permalink
refactor prover::types and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
roynalnaruto committed Oct 25, 2024
1 parent c8cd343 commit fc050c4
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 42 deletions.
3 changes: 1 addition & 2 deletions prover/src/proof/batch.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::{dump_as_json, dump_vk, from_json_file, Proof};
use crate::types::base64;
use anyhow::Result;
use eth_types::H256;
use eth_types::{base64, H256};
use halo2_proofs::{halo2curves::bn256::G1Affine, plonk::ProvingKey};
use serde_derive::{Deserialize, Serialize};
use snark_verifier::Protocol;
Expand Down
3 changes: 2 additions & 1 deletion prover/src/proof/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{dump_as_json, dump_data, dump_vk, from_json_file, Proof};
use crate::{types::base64, zkevm::SubCircuitRowUsage};
use crate::zkevm::SubCircuitRowUsage;
use aggregator::ChunkInfo;
use eth_types::base64;
use halo2_proofs::{halo2curves::bn256::G1Affine, plonk::ProvingKey};
use serde_derive::{Deserialize, Serialize};
use snark_verifier::Protocol;
Expand Down
2 changes: 1 addition & 1 deletion prover/src/proof/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
io::{deserialize_fr, deserialize_vk, serialize_fr, serialize_vk, write_file},
types::base64,
utils::short_git_version,
};
use anyhow::Result;
use eth_types::base64;
use halo2_proofs::{
halo2curves::bn256::{Fr, G1Affine},
plonk::{Circuit, ProvingKey, VerifyingKey},
Expand Down
10 changes: 3 additions & 7 deletions prover/src/test/batch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};
use std::sync::{LazyLock, Mutex};

use crate::{
aggregator::{Prover, Verifier},
Expand All @@ -7,14 +7,10 @@ use crate::{
io::force_to_read,
types::BundleProvingTask,
utils::read_env_var,
BatchProvingTask,
};
use std::{
collections::BTreeMap,
sync::{LazyLock, Mutex},
BatchProvingTask, ParamsMap,
};

static PARAMS_MAP: LazyLock<BTreeMap<u32, ParamsKZG<Bn256>>> = LazyLock::new(|| {
static PARAMS_MAP: LazyLock<ParamsMap> = LazyLock::new(|| {
let params_dir = read_env_var("SCROLL_PROVER_PARAMS_DIR", "./test_params".to_string());
crate::common::Prover::load_params_map(&params_dir, &AGG_DEGREES)
});
Expand Down
10 changes: 3 additions & 7 deletions prover/src/test/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};
use std::sync::{LazyLock, Mutex};

use crate::{
config::ZKEVM_DEGREES,
utils::read_env_var,
zkevm::{Prover, Verifier},
ChunkProof, ChunkProvingTask,
};
use std::{
collections::BTreeMap,
sync::{LazyLock, Mutex},
ChunkProof, ChunkProvingTask, ParamsMap,
};

static PARAMS_MAP: LazyLock<BTreeMap<u32, ParamsKZG<Bn256>>> = LazyLock::new(|| {
static PARAMS_MAP: LazyLock<ParamsMap> = LazyLock::new(|| {
let params_dir = read_env_var("SCROLL_PROVER_PARAMS_DIR", "./test_params".to_string());
crate::common::Prover::load_params_map(&params_dir, &ZKEVM_DEGREES)
});
Expand Down
10 changes: 3 additions & 7 deletions prover/src/test/inner.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};
use std::sync::{LazyLock, Mutex};

use crate::{
common::{Prover, Verifier},
config::{LayerId, INNER_DEGREE},
utils::{gen_rng, read_env_var},
zkevm::circuit::{SuperCircuit, TargetCircuit},
WitnessBlock,
};
use std::{
collections::BTreeMap,
sync::{LazyLock, Mutex},
ParamsMap, WitnessBlock,
};

static PARAMS_MAP: LazyLock<BTreeMap<u32, ParamsKZG<Bn256>>> = LazyLock::new(|| {
static PARAMS_MAP: LazyLock<ParamsMap> = LazyLock::new(|| {
let params_dir = read_env_var("SCROLL_PROVER_PARAMS_DIR", "./test_params".to_string());
crate::common::Prover::load_params_map(&params_dir, &[*INNER_DEGREE])
});
Expand Down
50 changes: 42 additions & 8 deletions prover/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,84 @@
use aggregator::{BatchHeader, ChunkInfo, MAX_AGG_SNARKS};
use eth_types::l2_types::BlockTrace;
use eth_types::{base64, l2_types::BlockTrace};
use serde::{Deserialize, Serialize};
use zkevm_circuits::evm_circuit::witness::Block;

use crate::{BatchProof, ChunkProof};

/// Alias for convenience.
pub type WitnessBlock = Block;

/// Helper type to deserialize JSON-encoded RPC result for [`BlockTrace`].
#[derive(Deserialize, Serialize, Default, Debug, Clone)]
pub struct BlockTraceJsonRpcResult {
/// The value of the "result" key.
pub result: BlockTrace,
}
pub use eth_types::base64;

use crate::{BatchProof, ChunkProof};

/// Defines a proving task for chunk proof generation.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ChunkProvingTask {
/// Prover can check `chunk_info` is consistent with block traces
/// Optional chunk data encapsulated within the proving task.
///
/// As part of a sanity check, the prover reconstructs the chunk data using the EVM execution
/// traces from all blocks in the chunk and compares against the supplied chunk data.
pub chunk_info: Option<ChunkInfo>,
/// The EVM execution traces for all blocks in the chunk.
pub block_traces: Vec<BlockTrace>,
}

impl ChunkProvingTask {
/// Create a new chunk proving task given the chunk trace.
pub fn new(block_traces: Vec<BlockTrace>) -> Self {
Self {
block_traces,
chunk_info: None,
}
}

/// Returns true if there are no block traces in the chunk.
pub fn is_empty(&self) -> bool {
self.block_traces.is_empty()
}
/// Used for cache/load proof from disk

/// An identifier for the chunk. It is the block number of the first block in the chunk.
///
/// 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.block_traces
.first()
.map_or(0, |trace: &BlockTrace| {
trace.header.number.expect("block num").low_u64()
trace
.header
.number
.expect("block number should be present")
.low_u64()
})
.to_string()
}
}

/// Defines a proving task for batch proof generation.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BatchProvingTask {
/// Chunk proofs for the contiguous list of chunks within the batch.
pub chunk_proofs: Vec<ChunkProof>,
/// The [`BatchHeader`], as computed on-chain for this batch.
///
/// Ref: https://github.com/scroll-tech/scroll-contracts/blob/2ac4f3f7e090d7127db4b13b3627cb3ce2d762bc/src/libraries/codec/BatchHeaderV3Codec.sol
pub batch_header: BatchHeader<MAX_AGG_SNARKS>,
/// The bytes encoding the batch data that will finally be published on-chain in the form of an
/// EIP-4844 blob.
#[serde(with = "base64")]
pub blob_bytes: Vec<u8>,
}

impl BatchProvingTask {
/// Used for cache/load proof from disk
/// An identifier for the batch. It is the public input hash of the last chunk in the batch.
///
/// 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.chunk_proofs
.last()
Expand All @@ -62,12 +90,18 @@ impl BatchProvingTask {
}
}

/// Defines a proving task for bundle proof generation.
#[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<BatchProof>,
}

impl BundleProvingTask {
/// An identifier for the bundle. It is the batch hash of the last batch in the bundle.
///
/// 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()
}
Expand Down
20 changes: 11 additions & 9 deletions prover/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#![allow(deprecated)]
use crate::types::BlockTraceJsonRpcResult;
use std::{
fs::{self, metadata, File},
io::{BufReader, Read},
path::{Path, PathBuf},
str::FromStr,
sync::Once,
};

use anyhow::{bail, Result};
use chrono::Utc;
use eth_types::l2_types::BlockTrace;
Expand All @@ -16,18 +22,14 @@ use log4rs::{
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;
use std::fmt::Debug;
use std::{
fs::{self, metadata, File},
io::{BufReader, Read},
path::{Path, PathBuf},
str::FromStr,
sync::Once,
};
use zkevm_circuits::evm_circuit::witness::Block;

use crate::types::BlockTraceJsonRpcResult;

pub static LOGGER: Once = Once::new();

pub const DEFAULT_SERDE_FORMAT: SerdeFormat = SerdeFormat::RawBytesUnchecked;

pub const GIT_VERSION: &str = git_version!(args = ["--abbrev=7", "--always"]);

pub const PARAMS_G2_SECRET_POWER: &str = "(Fq2 { c0: 0x17944351223333f260ddc3b4af45191b856689eda9eab5cbcddbbe570ce860d2, c1: 0x186282957db913abd99f91db59fe69922e95040603ef44c0bd7aa3adeef8f5ac }, Fq2 { c0: 0x297772d34bc9aa8ae56162486363ffe417b02dc7e8c207fc2cc20203e67a02ad, c1: 0x298adc7396bd3865cbf6d6df91bae406694e6d2215baa893bdeadb63052895f4 })";
Expand Down

0 comments on commit fc050c4

Please sign in to comment.