-
Notifications
You must be signed in to change notification settings - Fork 377
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
Changes from 9 commits
fdf5832
50f6eb7
af076dd
16b24b9
4a13b82
c2d873f
682c46d
2c22756
092b17f
fc94edf
8ab29fe
d23c3e7
201addb
dfa6def
6b37759
5b01a20
5341952
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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}; | ||||||
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(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this line There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||
} | ||||||
|
@@ -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) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Is it right? Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated (in-progress) functions gen_chunk_proof and gen_agg_proof in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||
} |
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 | ||
} |
There was a problem hiding this comment.
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
andaggregator.rs
where each file contains both its prover and verifier.There was a problem hiding this comment.
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
andaggregator.rs
in commit d23c3e7.