diff --git a/core/src/lib.rs b/core/src/lib.rs index 5cd91ca1..403cfaf4 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -119,7 +119,7 @@ impl Raiko { "block hash unexpected", )?; - let output = GuestOutput::Success { header, hash: pi }; + let output = GuestOutput { header, hash: pi }; Ok(output) } diff --git a/core/src/prover.rs b/core/src/prover.rs index 302fe6a4..84a1e26b 100644 --- a/core/src/prover.rs +++ b/core/src/prover.rs @@ -47,12 +47,13 @@ impl Prover for NativeProver { trace!("Running the native prover for input {input:?}"); - let GuestOutput::Success { header, .. } = output.clone() else { - return Err(ProverError::GuestError("Unexpected output".to_owned())); - }; - - ProtocolInstance::new(&input, &header, VerifierType::None) + let pi = ProtocolInstance::new(&input, &output.header, VerifierType::None) .map_err(|e| ProverError::GuestError(e.to_string()))?; + if pi.instance_hash() != output.hash { + return Err(ProverError::GuestError( + "Protocol Instance hash not matched".to_string(), + )); + } to_proof(Ok(NativeResponse { output: output.clone(), diff --git a/host/src/server/api/v1/mod.rs b/host/src/server/api/v1/mod.rs index f9ba17a1..3977e49c 100644 --- a/host/src/server/api/v1/mod.rs +++ b/host/src/server/api/v1/mod.rs @@ -88,17 +88,13 @@ pub enum Status { #[derive(Debug, Serialize, ToSchema)] #[allow(dead_code)] -pub enum GuestOutputDoc { - #[schema(example = json!({"header": [0, 0, 0, 0], "hash":"0x0...0"}))] - /// The output of the prover when the proof generation was successful. - Success { - /// Header bytes. - header: Vec, - /// Instance hash. - hash: String, - }, - /// The output of the prover when the proof generation failed. - Failure, +#[schema(example = json!({"header": [0, 0, 0, 0], "hash":"0x0...0"}))] +/// The output of the prover when the proof generation was successful. +pub struct GuestOutputDoc { + /// Header bytes. + header: Vec, + /// Instance hash. + hash: String, } #[must_use] diff --git a/lib/src/input.rs b/lib/src/input.rs index afddeb54..d0b30e47 100644 --- a/lib/src/input.rs +++ b/lib/src/input.rs @@ -106,13 +106,10 @@ pub struct TaikoProverData { #[serde_as] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum GuestOutput { - Success { - #[serde_as(as = "RlpHexBytes")] - header: AlloyConsensusHeader, - hash: B256, - }, - Failure, +pub struct GuestOutput { + #[serde_as(as = "RlpHexBytes")] + pub header: AlloyConsensusHeader, + pub hash: B256, } sol! { diff --git a/lib/src/protocol_instance.rs b/lib/src/protocol_instance.rs index f8bd86f2..e61d9bfa 100644 --- a/lib/src/protocol_instance.rs +++ b/lib/src/protocol_instance.rs @@ -17,7 +17,7 @@ use crate::{ const KZG_TRUST_SETUP_DATA: &[u8] = include_bytes!("../../kzg_settings_raw.bin"); -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct ProtocolInstance { pub transition: Transition, pub block_metadata: BlockMetadata, @@ -43,12 +43,17 @@ impl ProtocolInstance { let mut data = Vec::from(KZG_TRUST_SETUP_DATA); let kzg_settings = KzgSettings::from_u8_slice(&mut data); let kzg_commit = KzgCommitment::blob_to_kzg_commitment( - &Blob::from_bytes(input.taiko.tx_data.as_slice()).unwrap(), + &Blob::from_bytes(input.taiko.tx_data.as_slice()) + .expect("Fail to form blob from tx bytes"), &kzg_settings, ) - .unwrap(); + .expect("Fail to calculate KZG commitment"); let versioned_hash = kzg_to_versioned_hash(&kzg_commit); - assert_eq!(versioned_hash, input.taiko.tx_blob_hash.unwrap()); + assert_eq!( + versioned_hash, + input.taiko.tx_blob_hash.unwrap(), + "Blob version hash not matching" + ); versioned_hash } } else { diff --git a/provers/risc0/driver/src/bonsai.rs b/provers/risc0/driver/src/bonsai.rs index 5fd03c0a..41ac540f 100644 --- a/provers/risc0/driver/src/bonsai.rs +++ b/provers/risc0/driver/src/bonsai.rs @@ -1,5 +1,5 @@ use log::{debug, error, info, warn}; -use raiko_lib::{primitives::keccak::keccak, prover::Prover}; +use raiko_lib::primitives::keccak::keccak; use risc0_zkvm::{ compute_image_id, is_dev_mode, serde::to_vec, sha::Digest, Assumption, ExecutorEnv, ExecutorImpl, Receipt, diff --git a/provers/risc0/driver/src/lib.rs b/provers/risc0/driver/src/lib.rs index fd8ef842..59e6642a 100644 --- a/provers/risc0/driver/src/lib.rs +++ b/provers/risc0/driver/src/lib.rs @@ -2,14 +2,11 @@ use std::fmt::Debug; use alloy_primitives::B256; -use alloy_sol_types::SolValue; use hex::ToHex; use raiko_lib::{ input::{GuestInput, GuestOutput}, - primitives::keccak::keccak, - protocol_instance::ProtocolInstance, prover::{to_proof, Proof, Prover, ProverConfig, ProverResult}, }; use risc0_zkvm::{serde::to_vec, sha::Digest}; @@ -53,11 +50,11 @@ impl Prover for Risc0Prover { println!("elf code length: {}", RISC0_GUEST_ELF.len()); let encoded_input = to_vec(&input).expect("Could not serialize proving input!"); - let result = maybe_prove::( + let result = maybe_prove::( &config, encoded_input, RISC0_GUEST_ELF, - output, + &output.hash, Default::default(), ) .await; diff --git a/provers/risc0/guest/src/main.rs b/provers/risc0/guest/src/main.rs index b1b665b4..f592589a 100644 --- a/provers/risc0/guest/src/main.rs +++ b/provers/risc0/guest/src/main.rs @@ -25,22 +25,12 @@ fn main() { .set(Box::new(vec![ZkOperation::Sha256, ZkOperation::Secp256k1])) .expect("Failed to set ZkvmOperations"); - let build_result = TaikoStrategy::build_from(&input); - - let output = match &build_result { - Ok((header, _mpt_node)) => { - let pi = ProtocolInstance::new(&input, header, VerifierType::RISC0) - .expect("Failed to assemble protocol instance") - .instance_hash(); - GuestOutput::Success { - header: header.clone(), - hash: pi, - } - } - Err(_) => GuestOutput::Failure, - }; + let (header, _mpt_node) = TaikoStrategy::build_from(&input).unwrap(); + let pi = ProtocolInstance::new(&input, &header, VerifierType::RISC0) + .unwrap() + .instance_hash(); - env::commit(&output); + env::commit(&pi); } harness::zk_suits!( diff --git a/provers/sp1/driver/src/lib.rs b/provers/sp1/driver/src/lib.rs index e7d1e498..1ea6b507 100644 --- a/provers/sp1/driver/src/lib.rs +++ b/provers/sp1/driver/src/lib.rs @@ -12,7 +12,6 @@ const ELF: &[u8] = include_bytes!("../../guest/elf/sp1-guest"); #[derive(Clone, Serialize, Deserialize)] pub struct Sp1Response { pub proof: String, - pub output: GuestOutput, } pub struct Sp1Prover; @@ -30,10 +29,8 @@ impl Prover for Sp1Prover { // Generate the proof for the given program. let client = ProverClient::new(); let (pk, vk) = client.setup(ELF); - let mut proof = client.prove(&pk, stdin).expect("Sp1: proving failed"); + let proof = client.prove(&pk, stdin).expect("Sp1: proving failed"); - // Read the output. - let output = proof.public_values.read::(); // Verify proof. client .verify(&proof, &vk) @@ -54,7 +51,6 @@ impl Prover for Sp1Prover { println!("successfully generated and verified proof for the program!"); to_proof(Ok(Sp1Response { proof: serde_json::to_string(&proof).unwrap(), - output, })) } } diff --git a/provers/sp1/guest/src/main.rs b/provers/sp1/guest/src/main.rs index 0aa368de..f146b935 100644 --- a/provers/sp1/guest/src/main.rs +++ b/provers/sp1/guest/src/main.rs @@ -26,22 +26,12 @@ pub fn main() { ])) .expect("Failed to set ZkvmOperations"); - let build_result = TaikoStrategy::build_from(&input); + let (header, _mpt_node) = TaikoStrategy::build_from(&input).unwrap(); + let pi = ProtocolInstance::new(&input, &header, VerifierType::SP1) + .unwrap() + .instance_hash(); - let output = match &build_result { - Ok((header, _mpt_node)) => { - let pi = ProtocolInstance::new(&input, header, VerifierType::SP1) - .expect("Failed to assemble protocol instance") - .instance_hash(); - GuestOutput::Success { - header: header.clone(), - hash: pi, - } - } - Err(_) => GuestOutput::Failure, - }; - - sp1_zkvm::io::commit(&output); + sp1_zkvm::io::commit(&pi); } harness::zk_suits!(