Skip to content

Commit

Permalink
Merge branch 'guest-input-serialization' of https://github.com/taikox…
Browse files Browse the repository at this point in the history
…yz/raiko into guest-input-serialization
  • Loading branch information
CeciliaZ030 committed Jun 9, 2024
2 parents 24968b2 + da368ff commit c89b10c
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 69 deletions.
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Raiko {
"block hash unexpected",
)?;

let output = GuestOutput::Success { header, hash: pi };
let output = GuestOutput { header, hash: pi };

Ok(output)
}
Expand Down
11 changes: 6 additions & 5 deletions core/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
18 changes: 7 additions & 11 deletions host/src/server/api/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
/// 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<u8>,
/// Instance hash.
hash: String,
}

#[must_use]
Expand Down
11 changes: 4 additions & 7 deletions lib/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
13 changes: 9 additions & 4 deletions lib/src/protocol_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion provers/risc0/driver/src/bonsai.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 2 additions & 5 deletions provers/risc0/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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::<GuestInput, GuestOutput>(
let result = maybe_prove::<GuestInput, B256>(
&config,
encoded_input,
RISC0_GUEST_ELF,
output,
&output.hash,
Default::default(),
)
.await;
Expand Down
20 changes: 5 additions & 15 deletions provers/risc0/guest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
6 changes: 1 addition & 5 deletions provers/sp1/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::<GuestOutput>();
// Verify proof.
client
.verify(&proof, &vk)
Expand All @@ -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,
}))
}
}
Expand Down
20 changes: 5 additions & 15 deletions provers/sp1/guest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down

0 comments on commit c89b10c

Please sign in to comment.