Skip to content

Commit

Permalink
fix(raiko): set default behavior and fix proof format (#354)
Browse files Browse the repository at this point in the history
* set default behavior and fix proof format

Signed-off-by: smtmfft <[email protected]>

* update devnet setting

---------

Signed-off-by: smtmfft <[email protected]>
  • Loading branch information
smtmfft authored Sep 2, 2024
1 parent ecbd621 commit 5533914
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion core/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ impl TryFrom<ProofRequestOpt> for ProofRequest {
.map_err(|_| RaikoError::InvalidRequestConfig("Invalid proof_type".to_string()))?,
blob_proof_type: value
.blob_proof_type
.unwrap_or("kzg_versioned_hash".to_string())
.unwrap_or("proof_of_equivalence".to_string())
.parse()
.map_err(|_| {
RaikoError::InvalidRequestConfig("Invalid blob_proof_type".to_string())
Expand Down
6 changes: 3 additions & 3 deletions host/config/chain_spec_list_devnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"SP1": null,
"RISC0": null
},
"genesis_time": 1722392400,
"genesis_time": 1724760400,
"seconds_per_slot": 12,
"is_taiko": false
},
Expand All @@ -49,13 +49,13 @@
"base_fee_max_decrease_denominator": "0x8",
"elasticity_multiplier": "0x2"
},
"l1_contract": "0x558E38a3286916934Cb63ced04558A52F7Ce67a9",
"l1_contract": "0xcdE816aFd1B7db50f09831097e71F99877809218",
"l2_contract": "0x1670010000000000000000000000000000010001",
"rpc": "https://rpc.internal.taiko.xyz",
"beacon_rpc": null,
"verifier_address": {
"SGX": "0xC069c3d2a9f2479F559AD34485698ad5199C555f",
"SP1": null,
"SP1": "0x5F7eD46Ce19E12639D05a5020C64045273842C83",
"RISC0": "0x28336BC4116B9672000E7C6Ab96B1454D9d138f7"
},
"genesis_time": 0,
Expand Down
52 changes: 39 additions & 13 deletions provers/sp1/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use sp1_sdk::{
};
use sp1_sdk::{HashableKey, ProverClient, SP1Stdin, SP1VerifyingKey};
use std::{
borrow::BorrowMut,
env, fs,
path::{Path, PathBuf},
};
Expand All @@ -33,19 +34,22 @@ pub static VERIFIER: Lazy<Result<PathBuf, ProverError>> = Lazy::new(init_verifie
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Sp1Param {
#[serde(default = "RecursionMode::default")]
pub recursion: RecursionMode,
pub prover: Option<ProverMode>,
#[serde(default = "bool::default")]
pub verify: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum RecursionMode {
/// The proof mode for an SP1 core proof.
Core,
/// The proof mode for a compressed proof.
Compressed,
/// The proof mode for a PlonK proof.
#[default]
Plonk,
}

Expand Down Expand Up @@ -107,6 +111,11 @@ impl Prover for Sp1Prover {
.unwrap_or_else(ProverClient::new);

let (pk, vk) = client.setup(ELF);
info!(
"Sp1 Prover: block {:?} with vk {:?}",
output.header.number,
vk.bytes32()
);

let prove_action = action::Prove::new(client.prover.as_ref(), &pk, stdin.clone());
let prove_result = if !matches!(mode, ProverMode::Network) {
Expand Down Expand Up @@ -146,18 +155,34 @@ impl Prover for Sp1Prover {
.unwrap()
};

let proof = Proof {
proof: serde_json::to_string(&prove_result).ok(),
quote: None,
};

let proof_bytes = prove_result.bytes();
if param.verify {
let time = Measurement::start("verify", false);
verify_sol(vk, prove_result)?;
let pi_hash = prove_result
.clone()
.borrow_mut()
.public_values
.read::<[u8; 32]>();
verify_sol(&vk, &proof_bytes, &pi_hash)?;
time.stop_with("==> Verification complete");
}

Ok::<_, ProverError>(proof)
Ok::<_, ProverError>(Proof {
proof: {
if proof_bytes.is_empty() {
None
} else {
// 0x + 64 bytes of the vkey + the proof
// vkey itself contains 0x prefix
Some(format!(
"{}{}",
vk.bytes32(),
reth_primitives::hex::encode(proof_bytes)
))
}
},
quote: None,
})
}

async fn cancel(key: ProofKey, id_store: Box<&mut dyn IdStore>) -> ProverResult<()> {
Expand Down Expand Up @@ -230,19 +255,20 @@ struct RaikoProofFixture {
}

pub fn verify_sol(
vk: SP1VerifyingKey,
mut proof: sp1_sdk::SP1ProofWithPublicValues,
vk: &SP1VerifyingKey,
proof_bytes: &[u8],
pi_hash: &[u8; 32],
) -> ProverResult<()> {
assert!(VERIFIER.is_ok());

// Deserialize the public values.
let pi_hash = proof.public_values.read::<[u8; 32]>();
// let pi_hash = proof.public_values.read::<[u8; 32]>();

// Create the testing fixture so we can test things end-to-end.
let fixture = RaikoProofFixture {
vkey: vk.bytes32().to_string(),
public_values: B256::from_slice(&pi_hash).to_string(),
proof: format!("0x{}", reth_primitives::hex::encode(proof.bytes())),
public_values: B256::from_slice(pi_hash).to_string(),
proof: format!("0x{}", reth_primitives::hex::encode(proof_bytes)),
};
debug!("===> Fixture: {:#?}", fixture);

Expand Down
Binary file modified provers/sp1/guest/elf/sp1-guest
Binary file not shown.

0 comments on commit 5533914

Please sign in to comment.