Skip to content

Commit

Permalink
Merge branch 'feat/upgrade4' of github.com:scroll-tech/scroll into fe…
Browse files Browse the repository at this point in the history
…at/upgrade4_mock
  • Loading branch information
georgehao committed Jul 26, 2024
2 parents f6499be + f76fa41 commit f3f990e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 44 deletions.
22 changes: 11 additions & 11 deletions common/libzkp/impl/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ use crate::{
},
};
use libc::c_char;
use prover_v3::BatchProof as BatchProofV3;
use prover_v3::BatchProof as BatchProofLoVersion;
use prover_v4::{
aggregator::{Prover, Verifier as VerifierV4},
aggregator::{Prover, Verifier as VerifierHiVersion},
check_chunk_hashes,
consts::BATCH_VK_FILENAME,
utils::{chunk_trace_to_witness_block, init_env_and_log},
BatchHeader, BatchProof as BatchProofV4, BatchProvingTask, BlockTrace, BundleProof,
BatchHeader, BatchProof as BatchProofHiVersion, BatchProvingTask, BlockTrace, BundleProof,
BundleProvingTask, ChunkInfo, ChunkProof, MAX_AGG_SNARKS,
};
use snark_verifier_sdk::verify_evm_calldata;
use std::{cell::OnceCell, env, ptr::null};

static mut PROVER: OnceCell<Prover> = OnceCell::new();
static mut VERIFIER_V4: OnceCell<VerifierV4> = OnceCell::new();
static mut VERIFIER: OnceCell<VerifierHiVersion> = OnceCell::new();

/// # Safety
#[no_mangle]
Expand Down Expand Up @@ -52,9 +52,9 @@ pub unsafe extern "C" fn init_batch_verifier(params_dir: *const c_char, assets_d

// TODO: add a settings in scroll-prover.
env::set_var("SCROLL_PROVER_ASSETS_DIR", assets_dir);
let verifier_v4 = VerifierV4::from_dirs(params_dir, assets_dir);
let verifier_hi = VerifierHiVersion::from_dirs(params_dir, assets_dir);

VERIFIER_V4.set(verifier_v4).unwrap();
VERIFIER.set(verifier_hi).unwrap();
}

/// # Safety
Expand Down Expand Up @@ -189,16 +189,16 @@ pub unsafe extern "C" fn verify_batch_proof(
let verified = panic_catch(|| {
if fork_id == 3 {
// As of upgrade #3 (Curie), we verify batch proofs on-chain (EVM).
let proof = serde_json::from_slice::<BatchProofV3>(proof.as_slice()).unwrap();
let proof = serde_json::from_slice::<BatchProofLoVersion>(proof.as_slice()).unwrap();
verify_evm_calldata(
include_bytes!("plonk_verifier_0.11.4.bin").to_vec(),
proof.calldata(),
)
} else {
// Post upgrade #4 (Darwin), batch proofs are not EVM-verifiable. Instead they are
// halo2 proofs meant to be bundled recursively.
let proof = serde_json::from_slice::<BatchProofV4>(proof.as_slice()).unwrap();
VERIFIER_V4.get().unwrap().verify_batch_proof(&proof)
let proof = serde_json::from_slice::<BatchProofHiVersion>(proof.as_slice()).unwrap();
VERIFIER.get().unwrap().verify_batch_proof(&proof)
}
});
verified.unwrap_or(false) as c_char
Expand All @@ -209,7 +209,7 @@ pub unsafe extern "C" fn verify_batch_proof(
pub unsafe extern "C" fn gen_bundle_proof(batch_proofs: *const c_char) -> *const c_char {
let proof_result: Result<Vec<u8>, String> = panic_catch(|| {
let batch_proofs = c_char_to_vec(batch_proofs);
let batch_proofs = serde_json::from_slice::<Vec<BatchProofV4>>(&batch_proofs)
let batch_proofs = serde_json::from_slice::<Vec<BatchProofHiVersion>>(&batch_proofs)
.map_err(|e| format!("failed to deserialize batch proofs: {e:?}"))?;

let bundle = BundleProvingTask { batch_proofs };
Expand Down Expand Up @@ -243,7 +243,7 @@ pub unsafe extern "C" fn gen_bundle_proof(batch_proofs: *const c_char) -> *const
pub unsafe extern "C" fn verify_bundle_proof(proof: *const c_char) -> c_char {
let proof = c_char_to_vec(proof);
let proof = serde_json::from_slice::<BundleProof>(proof.as_slice()).unwrap();
let verified = panic_catch(|| VERIFIER_V4.get().unwrap().verify_bundle_proof(proof));
let verified = panic_catch(|| VERIFIER.get().unwrap().verify_bundle_proof(proof));
verified.unwrap_or(false) as c_char
}

Expand Down
38 changes: 22 additions & 16 deletions common/libzkp/impl/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ use crate::{
},
};
use libc::c_char;
use prover_v3::{zkevm::Verifier as VerifierV3, ChunkProof as ChunkProofV3};
use prover_v3::{zkevm::Verifier as VerifierLoVersion, ChunkProof as ChunkProofLoVersion};
use prover_v4::{
consts::CHUNK_VK_FILENAME,
utils::init_env_and_log,
zkevm::{Prover, Verifier as VerifierV4},
BlockTrace, ChunkProof as ChunkProofV4, ChunkProvingTask,
zkevm::{Prover, Verifier as VerifierHiVersion},
BlockTrace, ChunkProof as ChunkProofHiVersion, ChunkProvingTask,
};
use std::{cell::OnceCell, env, ptr::null};

static mut PROVER: OnceCell<Prover> = OnceCell::new();
static mut VERIFIER_V3: OnceCell<VerifierV3> = OnceCell::new();
static mut VERIFIER_V4: OnceCell<VerifierV4> = OnceCell::new();
static mut VERIFIER_LO_VERSION: OnceCell<VerifierLoVersion> = OnceCell::new();
static mut VERIFIER_HI_VERSION: OnceCell<VerifierHiVersion> = OnceCell::new();

/// # Safety
#[no_mangle]
Expand All @@ -42,19 +42,25 @@ pub unsafe extern "C" fn init_chunk_prover(params_dir: *const c_char, assets_dir

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn init_chunk_verifier(params_dir: *const c_char, assets_dir: *const c_char) {
pub unsafe extern "C" fn init_chunk_verifier(
params_dir: *const c_char,
v3_assets_dir: *const c_char,
v4_assets_dir: *const c_char,
) {
init_env_and_log("ffi_chunk_verify");

let params_dir = c_char_to_str(params_dir);
let assets_dir = c_char_to_str(assets_dir);
let v3_assets_dir = c_char_to_str(v3_assets_dir);
let v4_assets_dir = c_char_to_str(v4_assets_dir);

// TODO: add a settings in scroll-prover.
env::set_var("SCROLL_PROVER_ASSETS_DIR", assets_dir);
let verifier_v3 = VerifierV3::from_dirs(params_dir, assets_dir);
let verifier_v4 = VerifierV4::from_dirs(params_dir, assets_dir);
env::set_var("SCROLL_PROVER_ASSETS_DIR", v3_assets_dir);
let verifier_lo = VerifierLoVersion::from_dirs(params_dir, v3_assets_dir);
env::set_var("SCROLL_PROVER_ASSETS_DIR", v4_assets_dir);
let verifier_hi = VerifierHiVersion::from_dirs(params_dir, v4_assets_dir);

VERIFIER_V3.set(verifier_v3).unwrap();
VERIFIER_V4.set(verifier_v4).unwrap();
VERIFIER_LO_VERSION.set(verifier_lo).unwrap();
VERIFIER_HI_VERSION.set(verifier_hi).unwrap();
}

/// # Safety
Expand Down Expand Up @@ -120,11 +126,11 @@ pub unsafe extern "C" fn verify_chunk_proof(
};
let verified = panic_catch(|| {
if fork_id == 3 {
let proof = serde_json::from_slice::<ChunkProofV3>(proof.as_slice()).unwrap();
VERIFIER_V3.get().unwrap().verify_chunk_proof(proof)
let proof = serde_json::from_slice::<ChunkProofLoVersion>(proof.as_slice()).unwrap();
VERIFIER_LO_VERSION.get().unwrap().verify_chunk_proof(proof)
} else {
let proof = serde_json::from_slice::<ChunkProofV4>(proof.as_slice()).unwrap();
VERIFIER_V4.get().unwrap().verify_chunk_proof(proof)
let proof = serde_json::from_slice::<ChunkProofHiVersion>(proof.as_slice()).unwrap();
VERIFIER_HI_VERSION.get().unwrap().verify_chunk_proof(proof)
}
});
verified.unwrap_or(false) as c_char
Expand Down
2 changes: 1 addition & 1 deletion common/libzkp/interface/libzkp.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ char* gen_bundle_proof(char* batch_proofs);
char verify_bundle_proof(char* proof);

void init_chunk_prover(char* params_dir, char* assets_dir);
void init_chunk_verifier(char* params_dir, char* assets_dir);
void init_chunk_verifier(char* params_dir, char* v3_assets_dir, char* v4_assets_dir);
char* get_chunk_vk();
char* gen_chunk_proof(char* block_traces);
char verify_chunk_proof(char* proof, char* fork_name);
Expand Down
3 changes: 2 additions & 1 deletion coordinator/conf/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"fork_name": "bernoulli",
"mock_mode": true,
"params_path": "",
"assets_path": ""
"assets_path_lo": "",
"assets_path_hi": ""
},
"max_verifier_workers": 4,
"min_prover_version": "v1.0.0"
Expand Down
9 changes: 5 additions & 4 deletions coordinator/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ type Config struct {

// VerifierConfig load zk verifier config.
type VerifierConfig struct {
ForkName string `json:"fork_name"`
MockMode bool `json:"mock_mode"`
ParamsPath string `json:"params_path"`
AssetsPath string `json:"assets_path"`
ForkName string `json:"fork_name"`
MockMode bool `json:"mock_mode"`
ParamsPath string `json:"params_path"`
AssetsPathLo string `json:"assets_path_lo"` // lower version Verifier
AssetsPathHi string `json:"assets_path_hi"` // higher version Verifier
}

// NewConfig returns a new instance of Config.
Expand Down
16 changes: 9 additions & 7 deletions coordinator/internal/logic/verifier/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ func NewVerifier(cfg *config.VerifierConfig) (*Verifier, error) {
return &Verifier{cfg: cfg, ChunkVKMap: chunkVKMap, BatchVKMap: batchVKMap, BundleVkMap: bundleVKMap}, nil
}
paramsPathStr := C.CString(cfg.ParamsPath)
assetsPathStr := C.CString(cfg.AssetsPath)
assetsPathLoStr := C.CString(cfg.AssetsPathLo)
assetsPathHiStr := C.CString(cfg.AssetsPathHi)
defer func() {
C.free(unsafe.Pointer(paramsPathStr))
C.free(unsafe.Pointer(assetsPathStr))
C.free(unsafe.Pointer(assetsPathLoStr))
C.free(unsafe.Pointer(assetsPathHiStr))
}()

C.init_batch_verifier(paramsPathStr, assetsPathStr)
C.init_chunk_verifier(paramsPathStr, assetsPathStr)
C.init_batch_verifier(paramsPathStr, assetsPathHiStr)
C.init_chunk_verifier(paramsPathStr, assetsPathLoStr, assetsPathHiStr)

v := &Verifier{
cfg: cfg,
Expand All @@ -52,15 +54,15 @@ func NewVerifier(cfg *config.VerifierConfig) (*Verifier, error) {
BundleVkMap: make(map[string]string),
}

bundleVK, err := v.readVK(path.Join(cfg.AssetsPath, "vk_bundle.vkey"))
bundleVK, err := v.readVK(path.Join(cfg.AssetsPathHi, "vk_bundle.vkey"))
if err != nil {
return nil, err
}
batchVK, err := v.readVK(path.Join(cfg.AssetsPath, "vk_batch.vkey"))
batchVK, err := v.readVK(path.Join(cfg.AssetsPathHi, "vk_batch.vkey"))
if err != nil {
return nil, err
}
chunkVK, err := v.readVK(path.Join(cfg.AssetsPath, "vk_chunk.vkey"))
chunkVK, err := v.readVK(path.Join(cfg.AssetsPathHi, "vk_chunk.vkey"))
if err != nil {
return nil, err
}
Expand Down
10 changes: 6 additions & 4 deletions coordinator/internal/logic/verifier/verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (

var (
paramsPath = flag.String("params", "/assets/test_params", "params dir")
assetsPath = flag.String("assets", "/assets/test_assets", "assets dir")
assetsPathLo = flag.String("assets", "/assets/test_assets_lo", "assets dir")
assetsPathHi = flag.String("assets", "/assets/test_assets_hi", "assets dir")
batchProofPath = flag.String("batch_proof", "/assets/proof_data/batch_proof", "batch proof file path")
chunkProofPath1 = flag.String("chunk_proof1", "/assets/proof_data/chunk_proof1", "chunk proof file path 1")
chunkProofPath2 = flag.String("chunk_proof2", "/assets/proof_data/chunk_proof2", "chunk proof file path 2")
Expand All @@ -28,9 +29,10 @@ func TestFFI(t *testing.T) {
as := assert.New(t)

cfg := &config.VerifierConfig{
MockMode: false,
ParamsPath: *paramsPath,
AssetsPath: *assetsPath,
MockMode: false,
ParamsPath: *paramsPath,
AssetsPathLo: *assetsPathLo,
AssetsPathHi: *assetsPathHi,
}

v, err := NewVerifier(cfg)
Expand Down

0 comments on commit f3f990e

Please sign in to comment.