Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Integrate aggregation circuit #160

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ make download-setup -e degree=DEGREE params_dir=PARAMS_DIR

### Testing

`make test-chunk-prove` is the main testing entry point for the multi-level circuit constraint system of scroll-zkevm. Developers could understand how the system works by reading the codes of this test.
`make test-chunk-prove` and `make test-agg-prove` are the main testing entries for multi-level circuit constraint system of scroll-prover. Developers could understand how the system works by reading the codes of these tests.

Besides it, `make test-inner-prove` could be used to test the first-level circuit, and `make-comp-prove` could be used to test two-layers compression circuits.

Expand Down
2 changes: 1 addition & 1 deletion ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ffi"
version = "0.4.0"
version = "0.5.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
75 changes: 66 additions & 9 deletions ffi/src/prove.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
use crate::utils::{c_char_to_str, c_char_to_vec, vec_to_c_char};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel it's better to organize the files as zkevm.rs and aggregator.rs where each file contains both its prover and verifier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-organize FFI to zkevm.rs and aggregator.rs in commit d23c3e7.

use libc::c_char;
use prover::{utils::init_env_and_log, zkevm};
use prover::{aggregator, config::ALL_AGG_DEGREES, utils::init_env_and_log, zkevm};
use std::cell::OnceCell;
use types::eth::BlockTrace;

static mut PROVER: OnceCell<zkevm::Prover> = OnceCell::new();
static mut CHUNK_PROVER: OnceCell<zkevm::Prover> = OnceCell::new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static mut CHUNK_PROVER: OnceCell<zkevm::Prover> = OnceCell::new();
static mut ZKEVM_PROVER: OnceCell<zkevm::Prover> = OnceCell::new();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, should we also need zkevm-prover (suppose to delete it when update)?

Since both two-layer-compression proof (inner-snark -> compression-wide -> compression-thin) and aggregation-proof are generated by aggregator-prover.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or two-layer-compression proof should be generated by zkevm-prover?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to ZKEVM_PROVER in commit fc94edf.

static mut AGG_PROVER: OnceCell<aggregator::Prover> = OnceCell::new();
static mut AGG_CHUNK_TRACES: OnceCell<Vec<Vec<BlockTrace>>> = OnceCell::new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line

Copy link
Contributor Author

@silathdiir silathdiir Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete it in commit fc94edf.


/// # Safety
#[no_mangle]
pub unsafe extern "C" fn init_prover(params_path: *const c_char, _seed_path: *const c_char) {
init_env_and_log("ffi_prove");
pub unsafe extern "C" fn init_chunk_prover(params_dir: *const c_char) {
init_env_and_log("ffi_chunk_prove");

let params_path = c_char_to_str(params_path);
let p = zkevm::Prover::from_params_dir(params_path);
PROVER.set(p).unwrap();
let params_dir = c_char_to_str(params_dir);
let prover = zkevm::Prover::from_params_dir(params_dir);
CHUNK_PROVER.set(prover).unwrap();
}

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn init_agg_prover(params_dir: *const c_char) {
init_env_and_log("ffi_agg_prove");

let params_dir = c_char_to_str(params_dir);

let prover = aggregator::Prover::from_params_dir(params_dir, &ALL_AGG_DEGREES);
AGG_PROVER.set(prover).unwrap();
}

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn create_block_proof(trace_char: *const c_char) -> *const c_char {
let trace_vec = c_char_to_vec(trace_char);
let trace = serde_json::from_slice::<BlockTrace>(&trace_vec).unwrap();
let proof = PROVER.get_mut().unwrap().gen_chunk_proof(&[trace]).unwrap();
let proof = CHUNK_PROVER
.get_mut()
.unwrap()
.gen_chunk_proof(&[trace])
.unwrap();
let proof_bytes = serde_json::to_vec(&proof).unwrap();
vec_to_c_char(proof_bytes)
}
Expand All @@ -31,11 +48,51 @@ pub unsafe extern "C" fn create_block_proof(trace_char: *const c_char) -> *const
pub unsafe extern "C" fn create_chunk_proof(trace_char: *const c_char) -> *const c_char {
let trace_vec = c_char_to_vec(trace_char);
let traces = serde_json::from_slice::<Vec<BlockTrace>>(&trace_vec).unwrap();
let proof = PROVER
let proof = CHUNK_PROVER
.get_mut()
.unwrap()
.gen_chunk_proof(traces.as_slice())
.unwrap();
let proof_bytes = serde_json::to_vec(&proof).unwrap();
vec_to_c_char(proof_bytes)
}

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn add_agg_chunk_trace(trace_char: *const c_char) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not make a stateful API. You should just create a single API

fn create_agg_proof(chunk_trace: *const c_char) -> *const c_char;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. There is no this API for final FFI (have not finished updated FFI).

Will update as two FFI functions:

  • one chunk-trace -> one two-layer-compression proof (as chunk-proof)
  • Multiple two-layer-compression proofs -> one aggregation-proof (as batch-proof)

Is it right? Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated (in-progress) functions gen_chunk_proof and gen_agg_proof in agg-funs/ffi-refactor branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update FFI prove to gen_chunk_proof and gen_agg_proof in commit fc94edf.

let trace_vec = c_char_to_vec(trace_char);
let trace = serde_json::from_slice::<Vec<_>>(&trace_vec).unwrap();

AGG_CHUNK_TRACES
.get_mut()
.or_else(|| {
AGG_CHUNK_TRACES.set(vec![]).unwrap();
AGG_CHUNK_TRACES.get_mut()
})
.unwrap()
.push(trace);
}

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn clear_agg_chunk_traces() {
if let Some(chunk_traces) = AGG_CHUNK_TRACES.get_mut() {
chunk_traces.clear();
}
}

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn create_agg_proof() -> *const c_char {
// Consume the chunk traces (take and clear).
let chunk_traces = AGG_CHUNK_TRACES.take().unwrap();

let proof = AGG_PROVER
.get_mut()
.unwrap()
.gen_agg_proof(chunk_traces, None)
.unwrap();

let proof_bytes = serde_json::to_vec(&proof).unwrap();
vec_to_c_char(proof_bytes)
}
60 changes: 47 additions & 13 deletions ffi/src/verify.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,64 @@
use crate::utils::{c_char_to_str, c_char_to_vec};
use libc::c_char;
use prover::{utils::init_env_and_log, zkevm, Proof};
use prover::{aggregator, config::AGG_LAYER4_DEGREE, utils::init_env_and_log, zkevm, Proof};
use std::{fs::File, io::Read};

static mut VERIFIER: Option<&zkevm::Verifier> = None;
static mut CHUNK_VERIFIER: Option<&zkevm::Verifier> = None;
static mut AGG_VERIFIER: Option<&aggregator::Verifier> = None;

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn init_verifier(params_path: *const c_char, agg_vk_path: *const c_char) {
init_env_and_log("ffi_verify");
pub unsafe extern "C" fn init_chunk_verifier(params_dir: *const c_char, vk_path: *const c_char) {
init_env_and_log("ffi_chunk_verify");

let params_path = c_char_to_str(params_path);
let agg_vk_path = c_char_to_str(agg_vk_path);
let mut f = File::open(agg_vk_path).unwrap();
let mut agg_vk = vec![];
f.read_to_end(&mut agg_vk).unwrap();
let vk_path = c_char_to_str(vk_path);
let mut f = File::open(vk_path).unwrap();
let mut vk = vec![];
f.read_to_end(&mut vk).unwrap();

let v = Box::new(zkevm::Verifier::from_params_dir(params_path, Some(agg_vk)));
VERIFIER = Some(Box::leak(v))
let params_dir = c_char_to_str(params_dir);
let verifier = Box::new(zkevm::Verifier::from_params_dir(params_dir, Some(vk)));

CHUNK_VERIFIER = Some(Box::leak(verifier));
}

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn init_agg_verifier(params_dir: *const c_char, vk_path: *const c_char) {
init_env_and_log("ffi_agg_verify");

let vk_path = c_char_to_str(vk_path);
let mut f = File::open(vk_path).unwrap();
let mut vk = vec![];
f.read_to_end(&mut vk).unwrap();

let params_dir = c_char_to_str(params_dir);
let verifier = Box::new(aggregator::Verifier::from_params_dir(
params_dir,
*AGG_LAYER4_DEGREE,
Some(vk),
));

AGG_VERIFIER = Some(Box::leak(verifier));
}

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn verify_chunk_proof(proof: *const c_char) -> c_char {
let proof_vec = c_char_to_vec(proof);
let chunk_proof = serde_json::from_slice::<Proof>(proof_vec.as_slice()).unwrap();
let verified = VERIFIER.unwrap().verify_chunk_proof(chunk_proof).is_ok();
let proof = serde_json::from_slice::<Proof>(proof_vec.as_slice()).unwrap();
let verified = CHUNK_VERIFIER.unwrap().verify_chunk_proof(proof).is_ok();

verified as c_char
}

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn verify_agg_proof(proof: *const c_char) -> c_char {
let proof_vec = c_char_to_vec(proof);
let proof = serde_json::from_slice::<Proof>(proof_vec.as_slice()).unwrap();

let verified = AGG_VERIFIER.unwrap().verify_agg_proof(proof).is_ok();

verified as c_char
}
12 changes: 6 additions & 6 deletions prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ edition = "2021"
[dependencies]
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2023_02_02" }

aggregator = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "develop" }
bus-mapping = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "develop" }
eth-types = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "develop" }
zkevm-circuits = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "develop", default-features = false, features = ["test","scroll","scroll-trace","shanghai"] }
mpt-zktrie = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "develop" }
mock = { git = "https://github.com/scroll-tech/zkevm-circuits", branch = "develop" }
aggregator = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "integrate-dynamic-proof-agg" }
bus-mapping = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "integrate-dynamic-proof-agg" }
eth-types = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "integrate-dynamic-proof-agg" }
zkevm-circuits = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "integrate-dynamic-proof-agg", default-features = false, features = ["test","scroll","scroll-trace","shanghai"] }
mpt-zktrie = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "integrate-dynamic-proof-agg" }
mock = { git = "https://github.com/scroll-tech/zkevm-circuits", branch = "integrate-dynamic-proof-agg" }

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" }
Expand Down
Loading