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 all 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.

3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ test-inner-prove:
test-chunk-prove:
@cargo test --features prove_verify --release test_chunk_prove_verify

test-comp-prove:
@cargo test --features prove_verify --release test_comp_prove_verify

test-agg-prove:
@cargo test --features prove_verify --release test_agg_prove_verify

Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +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.

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.
`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.

### Binaries

Expand Down
2 changes: 1 addition & 1 deletion bin/src/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn main() {

let now = Instant::now();
let chunk_proof = prover
.gen_chunk_proof(traces.as_slice())
.gen_chunk_proof(traces, None)
.expect("cannot generate chunk proof");
info!(
"finish generating chunk proof, elapsed: {:?}",
Expand Down
4 changes: 2 additions & 2 deletions bin/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ fn main() {

let args = Args::parse();
let chunk_vk = read_from_file(&args.vk_path);
let v = Verifier::from_params_dir(&args.params_path, Some(chunk_vk));
let v = Verifier::from_params_dir(&args.params_path, &chunk_vk);

let proof_path = PathBuf::from("proof_data").join("chunk_full_proof.json");
let proof_vec = read_from_file(&proof_path.to_string_lossy());
let proof = serde_json::from_slice::<Proof>(proof_vec.as_slice()).unwrap();
let verified = v.verify_chunk_proof(proof).is_ok();
let verified = v.verify_chunk_proof(proof);
info!("verify agg proof: {}", verified)
}

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
76 changes: 76 additions & 0 deletions ffi/src/aggregator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::utils::{c_char_to_str, c_char_to_vec, vec_to_c_char, OUTPUT_DIR};
use libc::c_char;
use prover::{
aggregator::{Prover, Verifier},
utils::init_env_and_log,
ChunkHash, Proof,
};
use std::{cell::OnceCell, fs::File, io::Read};

static mut AGG_PROVER: OnceCell<Prover> = OnceCell::new();
static mut AGG_VERIFIER: Option<&Verifier> = None;

/// # 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 = Prover::from_params_dir(params_dir);
AGG_PROVER.set(prover).unwrap();
}

/// # 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(Verifier::from_params_dir(params_dir, &vk));

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

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn create_agg_proof(
chunk_hashes: *const c_char,
chunk_proofs: *const c_char,
) -> *const c_char {
let chunk_hashes = c_char_to_vec(chunk_hashes);
let chunk_proofs = c_char_to_vec(chunk_proofs);

let chunk_hashes = serde_json::from_slice::<Vec<ChunkHash>>(&chunk_hashes).unwrap();
let chunk_proofs = serde_json::from_slice::<Vec<Proof>>(&chunk_proofs).unwrap();
assert_eq!(chunk_hashes.len(), chunk_proofs.len());

let chunks = chunk_hashes
.into_iter()
.zip(chunk_proofs.into_iter())
.collect();

let proof = AGG_PROVER
.get_mut()
.unwrap()
.gen_agg_proof(chunks, OUTPUT_DIR.as_deref())
.unwrap();

let proof_bytes = serde_json::to_vec(&proof).unwrap();
vec_to_c_char(proof_bytes)
}

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

let verified = AGG_VERIFIER.unwrap().verify_agg_proof(proof);
verified as c_char
}
26 changes: 3 additions & 23 deletions ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
#![feature(once_cell)]

pub mod prove;
pub mod verify;

pub(crate) mod utils {
use std::{
ffi::{CStr, CString},
os::raw::c_char,
};

pub(crate) fn c_char_to_str(c: *const c_char) -> &'static str {
let cstr = unsafe { CStr::from_ptr(c) };
cstr.to_str().unwrap()
}

pub(crate) fn c_char_to_vec(c: *const c_char) -> Vec<u8> {
let cstr = unsafe { CStr::from_ptr(c) };
cstr.to_bytes().to_vec()
}

pub(crate) fn vec_to_c_char(bytes: Vec<u8>) -> *const c_char {
CString::new(bytes).unwrap().into_raw()
}
}
mod aggregator;
mod utils;
mod zkevm;
41 changes: 0 additions & 41 deletions ffi/src/prove.rs

This file was deleted.

24 changes: 24 additions & 0 deletions ffi/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use once_cell::sync::Lazy;
use std::{
env,
ffi::{CStr, CString},
os::raw::c_char,
};

// Only used for debugging.
pub(crate) static OUTPUT_DIR: Lazy<Option<String>> =
Lazy::new(|| env::var("PROVER_OUTPUT_DIR").ok());

pub(crate) fn c_char_to_str(c: *const c_char) -> &'static str {
let cstr = unsafe { CStr::from_ptr(c) };
cstr.to_str().unwrap()
}

pub(crate) fn c_char_to_vec(c: *const c_char) -> Vec<u8> {
let cstr = unsafe { CStr::from_ptr(c) };
cstr.to_bytes().to_vec()
}

pub(crate) fn vec_to_c_char(bytes: Vec<u8>) -> *const c_char {
CString::new(bytes).unwrap().into_raw()
}
30 changes: 0 additions & 30 deletions ffi/src/verify.rs

This file was deleted.

Loading