Skip to content

Commit

Permalink
Merge branch 'feat/migrate_compression' into feat/update-prover-migra…
Browse files Browse the repository at this point in the history
…te-compression
  • Loading branch information
roynalnaruto committed Sep 19, 2024
2 parents eb4c891 + b5d18f8 commit 5b67992
Show file tree
Hide file tree
Showing 11 changed files with 561 additions and 255 deletions.
2 changes: 1 addition & 1 deletion aggregator/data/test_batches/batch274.hex

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions aggregator/src/mock_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use snark_verifier::loader::halo2::halo2_ecc::halo2_base::SKIP_FIRST_PASS;
use snark_verifier_sdk::CircuitExt;
use std::iter;
use zkevm_circuits::{table::KeccakTable, util::Challenges};
use rand::RngCore;

use crate::{
constants::{ACC_LEN, DIGEST_LEN},
Expand Down Expand Up @@ -49,8 +50,8 @@ impl MockChunkCircuit {
}

impl MockChunkCircuit {
pub fn random<R: rand::RngCore>(r: &mut R, has_accumulator: bool, is_padding: bool) -> Self {
let chunk = ChunkInfo::mock_random_chunk_info_for_testing(r);
pub fn random<R: RngCore>(mut r: R, has_accumulator: bool, is_padding: bool) -> Self {
let chunk = ChunkInfo::mock_random_chunk_info_for_testing(&mut r);
Self {
has_accumulator,
chunk: if is_padding {
Expand Down
45 changes: 24 additions & 21 deletions aggregator/src/tests/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,18 @@ fn check_circuit(circuit: &BlobCircuit) -> Result<(), Vec<VerifyFailure>> {

#[test]
fn blob_circuit_completeness() {
// TODO: enable this once we have another deterministic case of batch -> blob (fully packed).
// now batch274 is an deterministic case of batch -> blob (fully packed).
// Full blob test case
// batch274 contains batch bytes that will produce a full blob
// let full_blob = hex::decode(
// fs::read_to_string("./data/test_batches/batch274.hex")
// .expect("file path exists")
// .trim(),
// )
// .expect("should load full blob batch bytes");
// batch274 contains metadata
// let segmented_full_blob_src = BatchData::<MAX_AGG_SNARKS>::segment_with_metadata(full_blob);
let full_blob = hex::decode(
fs::read_to_string("./data/test_batches/batch274.hex")
.expect("file path exists")
.trim(),
)
.expect("should load full blob batch bytes");

// batch274 contains metadatas
let segmented_full_blob_src = BatchData::<MAX_AGG_SNARKS>::segment_with_metadata(full_blob);

let all_empty_chunks: Vec<Vec<u8>> = vec![vec![]; MAX_AGG_SNARKS];
let one_chunk = vec![vec![2, 3, 4, 100, 1]];
Expand All @@ -305,8 +306,8 @@ fn blob_circuit_completeness() {
.chain(std::iter::once(vec![3, 100, 24, 30]))
.collect::<Vec<_>>();

for blob in [
// segmented_full_blob_src,
for (idx, blob) in [
segmented_full_blob_src,
one_chunk,
two_chunks,
max_chunks,
Expand All @@ -317,19 +318,21 @@ fn blob_circuit_completeness() {
all_empty_except_last,
]
.into_iter()
.enumerate()
{
let batch_data = BatchData::from(&blob);

// TODO: enable this once we have another deterministic case of batch -> blob (fully
// packed).
// First blob is purposely constructed to take full blob space
// if idx == 0 {
// let blob_data_bytes_len = batch_data.get_blob_data_bytes().len();
// assert_eq!(
// blob_data_bytes_len, N_BLOB_BYTES,
// "should be full blob: expected={N_BLOB_BYTES}, got={blob_data_bytes_len}",
// );
// }
// First blob(batch274's blob data) is purposely constructed to take full blob space
if idx == 0 {
let batch_data_bytes = batch_data.get_batch_data_bytes();
let blob_data_bytes = get_blob_bytes(&batch_data_bytes);
let blob_data_bytes_len = blob_data_bytes.len();

assert_eq!(
blob_data_bytes_len, N_BLOB_BYTES,
"should be full blob: expected={N_BLOB_BYTES}, got={blob_data_bytes_len}",
);
}

assert_eq!(check_data(batch_data), Ok(()), "{:?}", blob);
}
Expand Down
6 changes: 6 additions & 0 deletions bus-mapping/src/circuit_input_builder/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ impl Transaction {
);

let tx_type = TxType::get_tx_type(eth_tx);
log::debug!(
"{:?}th tx, tx hash {:?}, tx_type {:?}",
eth_tx.transaction_index,
eth_tx.hash(),
tx_type
);
let (l1_fee, l1_fee_committed) = if tx_type.is_l1_msg() {
Default::default()
} else {
Expand Down
56 changes: 48 additions & 8 deletions compression/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::params::ConfigParams;
use ark_std::{end_timer, start_timer};
use ce_snark_verifier::halo2_base::gates::circuit::{BaseConfig, CircuitBuilderStage};
use ce_snark_verifier_sdk::{
gen_pk,
halo2::aggregation::{AggregationCircuit, AggregationConfigParams, VerifierUniversality},
CircuitExt as CeCircuitExt, SHPLONK,
};
Expand Down Expand Up @@ -93,31 +94,70 @@ impl CeCircuitExt<Fr> for CompressionCircuit {
impl CompressionCircuit {
/// Build a new circuit from a snark, with a flag whether this snark has been compressed before
pub fn new(
degree: u32,
params: &ParamsKZG<Bn256>,
snark: snark_verifier_sdk::Snark,
has_accumulator: bool,
rng: impl Rng + Send,
) -> Result<Self, ce_snark_verifier::Error> {
verify_snark_accumulator_pairing(&snark, params)
.expect("Compression circuit accumulator pre-check should not fail.");
Self::new_from_ce_snark(params, to_ce_snark(&snark), has_accumulator, rng)
// compression_debug
// verify_snark_accumulator_pairing(&snark, params)
// .expect("Compression circuit accumulator pre-check should not fail.");
Self::new_from_ce_snark(degree, params, to_ce_snark(&snark), has_accumulator, rng)
}

pub fn new_from_ce_snark(
degree: u32,
params: &ParamsKZG<Bn256>,
snark: ce_snark_verifier_sdk::Snark,
has_accumulator: bool,
_rng: impl Rng + Send, // TODO: hook this up to the rng in AggregationCircuit? is that even needed?
) -> Result<Self, ce_snark_verifier::Error> {
// compression_debug
// let mut inner = AggregationCircuit::new::<SHPLONK>(
// CircuitBuilderStage::Mock,
// load_params(),
// params,
// [snark],
// VerifierUniversality::None,
// );
// inner.expose_previous_instances(has_accumulator);
// Ok(Self(inner))

let lookup_bits = degree as usize - 1;
let mut agg_circuit = AggregationCircuit::new::<SHPLONK>(
CircuitBuilderStage::Keygen,
AggregationConfigParams { degree, lookup_bits, ..Default::default() },
&params,
[snark.clone()],
VerifierUniversality::Full,
);
let agg_config = agg_circuit.calculate_params(Some(10));

let _pk = gen_pk(&params, &agg_circuit, None);
let break_points = agg_circuit.break_points();
drop(agg_circuit);

let mut inner = AggregationCircuit::new::<SHPLONK>(
CircuitBuilderStage::Mock,
load_params(),
params,
CircuitBuilderStage::Prover,
agg_config,
&params,
[snark],
VerifierUniversality::None,
);
VerifierUniversality::Full,
)
.use_break_points(break_points);
inner.expose_previous_instances(has_accumulator);

Ok(Self(inner))

// let _proof = gen_evm_proof_shplonk(&params, &pk, agg_circuit, instances.clone());

// let _deployment_code = gen_evm_verifier_shplonk::<AggregationCircuit>(
// &params,
// pk.get_vk(),
// num_instances,
// Some(Path::new("examples/StandardPlonkVerifier.sol")),
// );
}
}

Expand Down
Loading

0 comments on commit 5b67992

Please sign in to comment.