-
Notifications
You must be signed in to change notification settings - Fork 616
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prover&coordinator): use circuits version replace hard fork name (…
- Loading branch information
Showing
26 changed files
with
345 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,63 @@ | ||
mod batch; | ||
mod chunk; | ||
mod types; | ||
mod utils; | ||
mod verifier; | ||
|
||
use crate::utils::{c_char_to_str, c_char_to_vec}; | ||
use libc::c_char; | ||
use prover_v5::utils::init_env_and_log; | ||
use verifier::{TaskType, VerifierConfig}; | ||
|
||
/// # Safety | ||
#[no_mangle] | ||
pub unsafe extern "C" fn init(config: *const c_char) { | ||
init_env_and_log("ffi_init"); | ||
|
||
let config_str = c_char_to_str(config); | ||
let verifier_config = serde_json::from_str::<VerifierConfig>(config_str).unwrap(); | ||
verifier::init(verifier_config); | ||
} | ||
|
||
/// # Safety | ||
#[no_mangle] | ||
pub unsafe extern "C" fn verify_chunk_proof( | ||
proof: *const c_char, | ||
fork_name: *const c_char, | ||
) -> c_char { | ||
verify_proof(proof, fork_name, TaskType::Chunk) | ||
} | ||
|
||
fn verify_proof(proof: *const c_char, fork_name: *const c_char, task_type: TaskType) -> c_char { | ||
let proof = c_char_to_vec(proof); | ||
|
||
let fork_name_str = c_char_to_str(fork_name); | ||
let verifier = verifier::get_verifier(fork_name_str); | ||
|
||
if let Err(e) = verifier { | ||
log::warn!("failed to get verifier, error: {:#}", e); | ||
return 0 as c_char; | ||
} | ||
match verifier.unwrap().verify(task_type, proof) { | ||
Err(e) => { | ||
log::error!("{:?} verify failed, error: {:#}", task_type, e); | ||
false as c_char | ||
} | ||
Ok(result) => result as c_char, | ||
} | ||
} | ||
|
||
/// # Safety | ||
#[no_mangle] | ||
pub unsafe extern "C" fn verify_batch_proof( | ||
proof: *const c_char, | ||
fork_name: *const c_char, | ||
) -> c_char { | ||
verify_proof(proof, fork_name, TaskType::Batch) | ||
} | ||
|
||
/// # Safety | ||
#[no_mangle] | ||
pub unsafe extern "C" fn verify_bundle_proof( | ||
proof: *const c_char, | ||
fork_name: *const c_char, | ||
) -> c_char { | ||
verify_proof(proof, fork_name, TaskType::Bundle) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
mod darwin; | ||
mod darwin_v2; | ||
|
||
use anyhow::{bail, Result}; | ||
use darwin::DarwinVerifier; | ||
use darwin_v2::DarwinV2Verifier; | ||
use serde::{Deserialize, Serialize}; | ||
use std::{cell::OnceCell, rc::Rc}; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq)] | ||
pub enum TaskType { | ||
Chunk, | ||
Batch, | ||
Bundle, | ||
} | ||
|
||
pub trait ProofVerifier { | ||
fn verify(&self, task_type: TaskType, proof: Vec<u8>) -> Result<bool>; | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct CircuitConfig { | ||
pub fork_name: String, | ||
pub params_path: String, | ||
pub assets_path: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct VerifierConfig { | ||
pub low_version_circuit: CircuitConfig, | ||
pub high_version_circuit: CircuitConfig, | ||
} | ||
|
||
type HardForkName = String; | ||
|
||
struct VerifierPair(HardForkName, Rc<Box<dyn ProofVerifier>>); | ||
|
||
static mut VERIFIER_HIGH: OnceCell<VerifierPair> = OnceCell::new(); | ||
static mut VERIFIER_LOW: OnceCell<VerifierPair> = OnceCell::new(); | ||
|
||
pub fn init(config: VerifierConfig) { | ||
let low_conf = config.low_version_circuit; | ||
let verifier = DarwinVerifier::new(&low_conf.params_path, &low_conf.assets_path); | ||
|
||
unsafe { | ||
VERIFIER_LOW | ||
.set(VerifierPair( | ||
low_conf.fork_name, | ||
Rc::new(Box::new(verifier)), | ||
)) | ||
.unwrap_unchecked(); | ||
} | ||
let high_conf = config.high_version_circuit; | ||
let verifier = DarwinV2Verifier::new(&high_conf.params_path, &high_conf.assets_path); | ||
unsafe { | ||
VERIFIER_HIGH | ||
.set(VerifierPair( | ||
high_conf.fork_name, | ||
Rc::new(Box::new(verifier)), | ||
)) | ||
.unwrap_unchecked(); | ||
} | ||
} | ||
|
||
pub fn get_verifier(fork_name: &str) -> Result<Rc<Box<dyn ProofVerifier>>> { | ||
unsafe { | ||
if let Some(verifier) = VERIFIER_LOW.get() { | ||
if verifier.0 == fork_name { | ||
return Ok(verifier.1.clone()); | ||
} | ||
} | ||
|
||
if let Some(verifier) = VERIFIER_HIGH.get() { | ||
if verifier.0 == fork_name { | ||
return Ok(verifier.1.clone()); | ||
} | ||
} | ||
} | ||
bail!("failed to get verifier, key not found, {}", fork_name) | ||
} |
Oops, something went wrong.