-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(raiko): use simple contract to verify sp1 proof (#381)
* use simple contract to verify sp1 proof * fix fmt
- Loading branch information
Showing
8 changed files
with
168 additions
and
85 deletions.
There are no files selected for viewing
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 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 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 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,2 @@ | ||
pub(crate) mod forge_proof_verify; | ||
pub(crate) mod remote_contract_verify; |
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,82 @@ | ||
#![cfg(feature = "foundry-verify")] | ||
|
||
use once_cell::sync::Lazy; | ||
use raiko_lib::prover::{ProverError, ProverResult}; | ||
use std::{ | ||
env, fs, | ||
path::{Path, PathBuf}, | ||
}; | ||
use tracing::{debug, info}; | ||
|
||
use crate::RaikoProofFixture; | ||
|
||
static FIXTURE_PATH: Lazy<PathBuf> = | ||
Lazy::new(|| Path::new(env!("CARGO_MANIFEST_DIR")).join("../contracts/src/fixtures/")); | ||
static CONTRACT_PATH: Lazy<PathBuf> = | ||
Lazy::new(|| Path::new(env!("CARGO_MANIFEST_DIR")).join("../contracts/src/exports/")); | ||
|
||
pub static VERIFIER: Lazy<Result<PathBuf, ProverError>> = Lazy::new(init_verifier); | ||
|
||
fn init_verifier() -> Result<PathBuf, ProverError> { | ||
// In cargo run, Cargo sets the working directory to the root of the workspace | ||
let contract_path = &*CONTRACT_PATH; | ||
info!("Contract dir: {contract_path:?}"); | ||
let artifacts_dir = sp1_sdk::install::try_install_circuit_artifacts(); | ||
// Create the destination directory if it doesn't exist | ||
fs::create_dir_all(contract_path)?; | ||
|
||
// Read the entries in the source directory | ||
for entry in fs::read_dir(artifacts_dir)? { | ||
let entry = entry?; | ||
let src = entry.path(); | ||
|
||
// Check if the entry is a file and ends with .sol | ||
if src.is_file() && src.extension().map(|s| s == "sol").unwrap_or(false) { | ||
let out = contract_path.join(src.file_name().unwrap()); | ||
fs::copy(&src, &out)?; | ||
println!("Copied: {:?}", src.file_name().unwrap()); | ||
} | ||
} | ||
Ok(contract_path.clone()) | ||
} | ||
|
||
/// verify the proof by using forge test, which involves downloading the whole sp1 sdk & | ||
/// starting a forge environment to run test. | ||
pub(crate) fn verify_sol_by_forge_test(fixture: &RaikoProofFixture) -> ProverResult<()> { | ||
assert!(VERIFIER.is_ok()); | ||
debug!("===> Fixture: {fixture:#?}"); | ||
|
||
// Save the fixture to a file. | ||
let fixture_path = &*FIXTURE_PATH; | ||
info!("Writing fixture to: {fixture_path:?}"); | ||
|
||
if !fixture_path.exists() { | ||
std::fs::create_dir_all(fixture_path.clone()) | ||
.map_err(|e| ProverError::GuestError(format!("Failed to create fixture path: {e}")))?; | ||
} | ||
std::fs::write( | ||
fixture_path.join("fixture.json"), | ||
serde_json::to_string_pretty(&fixture).unwrap(), | ||
) | ||
.map_err(|e| ProverError::GuestError(format!("Failed to write fixture: {e}")))?; | ||
|
||
let child = std::process::Command::new("forge") | ||
.arg("test") | ||
.current_dir(&*CONTRACT_PATH) | ||
.stdout(std::process::Stdio::inherit()) // Inherit the parent process' stdout | ||
.spawn(); | ||
info!("Verification started {:?}", child); | ||
child.map_err(|e| ProverError::GuestError(format!("Failed to run forge: {e}")))?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_init_verifier() { | ||
VERIFIER.as_ref().expect("Failed to init verifier"); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
provers/sp1/driver/src/proof_verify/remote_contract_verify.rs
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,57 @@ | ||
use alloy_primitives::Address; | ||
use alloy_provider::ProviderBuilder; | ||
use alloy_sol_types::sol; | ||
use raiko_lib::prover::ProverResult; | ||
use reth_primitives::B256; | ||
use sp1_sdk::proto::network::twirp::url::Url; | ||
use std::{env, str::FromStr}; | ||
use tracing::{error, info}; | ||
|
||
use crate::RaikoProofFixture; | ||
|
||
sol!( | ||
#[sol(rpc)] | ||
#[allow(dead_code)] | ||
contract ISP1Verifier { | ||
#[derive(Debug)] | ||
function verifyProof( | ||
bytes32 programVKey, | ||
bytes calldata publicValues, | ||
bytes calldata proofBytes | ||
) external view; | ||
} | ||
); | ||
|
||
/// using pre-deployed contract to verify the proof, the only problem is to double check the verification version. | ||
pub(crate) async fn verify_sol_by_contract_call(fixture: &RaikoProofFixture) -> ProverResult<()> { | ||
let sp1_verifier_rpc_url = env::var("SP1_VERIFIER_RPC_URL").expect("env SP1_VERIFIER_RPC_URL"); | ||
let sp1_verifier_addr = { | ||
let addr = env::var("SP1_VERIFIER_ADDRESS").expect("env SP1_VERIFIER_RPC_URL"); | ||
Address::from_str(&addr).unwrap() | ||
}; | ||
|
||
let provider = ProviderBuilder::new().on_http(Url::parse(&sp1_verifier_rpc_url).unwrap()); | ||
let program_key: B256 = B256::from_str(&fixture.vkey).unwrap(); | ||
let public_value = fixture.public_values; | ||
let proof_bytes = fixture.proof.clone(); | ||
|
||
info!( | ||
"verify sp1 proof with program key: {:?} public value: {:?} proof: {:?}", | ||
program_key, | ||
public_value, | ||
reth_primitives::hex::encode(&proof_bytes) | ||
); | ||
|
||
let sp1_verifier = ISP1Verifier::new(sp1_verifier_addr, provider); | ||
let call_builder = | ||
sp1_verifier.verifyProof(program_key, public_value.into(), proof_bytes.into()); | ||
let verify_call_res = call_builder.call().await; | ||
|
||
if verify_call_res.is_ok() { | ||
info!("SP1 proof verified successfully using {sp1_verifier_addr:?}!"); | ||
} else { | ||
error!("SP1 proof verification failed: {:?}!", verify_call_res); | ||
} | ||
|
||
Ok(()) | ||
} |
Binary file not shown.