Skip to content

Commit

Permalink
Remove Receipt output type
Browse files Browse the repository at this point in the history
  • Loading branch information
yaziciahmet committed Aug 19, 2024
1 parent 69da3fe commit af90bb8
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 99 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion bin/citrea/provers/risc0/guest-bitcoin/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion bin/citrea/provers/risc0/guest-mock/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions crates/fullnode/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ where
);
tracing::debug!("ZK proof: {:?}", proof);

let (state_transition, receipt) =
let state_transition =
Vm::extract_output::<<Da as DaService>::Spec, Stf::StateRoot>(&proof)
.expect("Proof should be deserializable");
if state_transition.sequencer_da_public_key != self.sequencer_da_pub_key
Expand All @@ -243,16 +243,14 @@ where
).into());
}

match proof {
Proof::Full(_) => {
let receipt = receipt.expect("Receipt must exist with full proof");
match &proof {
Proof::Full(data) => {
let code_commitment = self
.code_commitments_by_spec
.get(&state_transition.final_spec_id)
.expect("Proof public input must contain valid spec id");
receipt
.verify(code_commitment.clone())
.map_err(|err| anyhow!("Proof verification: SNARK verification failed: {}. Skipping to next proof...", err))?;
Vm::verify(data, code_commitment)
.map_err(|err| anyhow!("Failed to verify proof: {:?}. Skipping it...", err))?;
}
Proof::PublicInput(_) => {
if !self.accept_public_input_as_proven {
Expand Down
13 changes: 6 additions & 7 deletions crates/prover/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,22 +798,21 @@ where

// l1_height => (tx_id, proof, transition_data)
// save proof along with tx id to db, should be queriable by slot number or slot hash
let (transition_data, receipt) =
Vm::extract_output::<<Da as DaService>::Spec, Stf::StateRoot>(&proof)
.expect("Proof should be deserializable");
let transition_data = Vm::extract_output::<<Da as DaService>::Spec, Stf::StateRoot>(&proof)
.expect("Proof should be deserializable");

match proof {
match &proof {
Proof::PublicInput(_) => {
warn!("Proof is public input, skipping");
}
Proof::Full(_) => {
Proof::Full(data) => {
info!("Verifying proof!");
let receipt = receipt.expect("Receipt must exist in full proof");
let code_commitment = self
.code_commitments_by_spec
.get(&transition_data.final_spec_id)
.expect("Proof public input must contain valid spec id");
receipt.verify(code_commitment.clone())?;
Vm::verify(data, code_commitment)
.map_err(|err| anyhow!("Failed to verify proof: {:?}. Skipping it...", err))?;
}
}

Expand Down
31 changes: 15 additions & 16 deletions crates/risc0-bonsai/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,24 +358,18 @@ impl<'a> ZkvmHost for Risc0BonsaiHost<'a> {

fn extract_output<Da: sov_rollup_interface::da::DaSpec, Root: BorshDeserialize>(
proof: &Proof,
) -> Result<
(
sov_rollup_interface::zk::StateTransition<Da, Root>,
Option<Receipt>,
),
Self::Error,
> {
let (journal, receipt) = match proof {
) -> Result<sov_rollup_interface::zk::StateTransition<Da, Root>, Self::Error> {
let journal = match proof {
Proof::PublicInput(journal) => {
let journal: Journal = bincode::deserialize(journal)?;
(journal, None)
journal
}
Proof::Full(data) => {
let receipt: Receipt = bincode::deserialize(data)?;
(receipt.journal.clone(), Some(receipt))
receipt.journal
}
};
Ok((BorshDeserialize::try_from_slice(&journal.bytes)?, receipt))
Ok(BorshDeserialize::try_from_slice(&journal.bytes)?)
}

fn recover_proving_sessions(&self) -> Result<Vec<Proof>, anyhow::Error> {
Expand Down Expand Up @@ -411,11 +405,16 @@ impl<'host> Zkvm for Risc0BonsaiHost<'host> {

type Error = anyhow::Error;

fn verify<'a>(
_serialized_proof: &'a [u8],
_code_commitment: &Self::CodeCommitment,
) -> Result<&'a [u8], Self::Error> {
unimplemented!();
fn verify(
serialized_proof: &[u8],
code_commitment: &Self::CodeCommitment,
) -> Result<Vec<u8>, Self::Error> {
let receipt: Receipt = bincode::deserialize(serialized_proof)?;

#[allow(clippy::clone_on_copy)]
receipt.verify(code_commitment.clone())?;

Ok(receipt.journal.bytes)
}

fn verify_and_extract_output<Da: sov_rollup_interface::da::DaSpec, Root: BorshDeserialize>(
Expand Down
50 changes: 20 additions & 30 deletions crates/sovereign-sdk/adapters/mock-zkvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::sync::{Arc, Condvar, Mutex};
use anyhow::ensure;
use borsh::{BorshDeserialize, BorshSerialize};
use risc0_zkvm::sha::Digest;
use risc0_zkvm::Receipt;
use serde::{Deserialize, Serialize};
use sov_rollup_interface::da::BlockHeaderTrait;
use sov_rollup_interface::spec::SpecId;
Expand Down Expand Up @@ -131,17 +130,17 @@ impl<ValidityCond: ValidityCondition> sov_rollup_interface::zk::Zkvm for MockZkv

type Error = anyhow::Error;

fn verify<'a>(
serialized_proof: &'a [u8],
fn verify(
serialized_proof: &[u8],
code_commitment: &Self::CodeCommitment,
) -> Result<&'a [u8], Self::Error> {
) -> Result<Vec<u8>, Self::Error> {
let proof = MockProof::decode(serialized_proof)?;
anyhow::ensure!(
proof.program_id.matches(code_commitment),
"Proof failed to verify against requested code commitment"
);
anyhow::ensure!(proof.is_valid, "Proof is not valid");
Ok(&serialized_proof[33..])
Ok(serialized_proof[33..].to_vec())
}

fn verify_and_extract_output<Da: sov_rollup_interface::da::DaSpec, Root: BorshDeserialize>(
Expand Down Expand Up @@ -181,34 +180,25 @@ impl<ValidityCond: ValidityCondition> sov_rollup_interface::zk::ZkvmHost

fn extract_output<Da: sov_rollup_interface::da::DaSpec, Root: BorshDeserialize>(
proof: &Proof,
) -> Result<
(
sov_rollup_interface::zk::StateTransition<Da, Root>,
Option<Receipt>,
),
Self::Error,
> {
) -> Result<sov_rollup_interface::zk::StateTransition<Da, Root>, Self::Error> {
match proof {
sov_rollup_interface::zk::Proof::PublicInput(pub_input) => {
let data: ProofInfo<Da::ValidityCondition> = bincode::deserialize(pub_input)?;
let st: StateTransitionData<Root, (), Da> =
BorshDeserialize::deserialize(&mut &*data.hint)?;

Ok((
sov_rollup_interface::zk::StateTransition {
initial_state_root: st.initial_state_root,
final_state_root: st.final_state_root,
initial_batch_hash: st.initial_batch_hash,
validity_condition: data.validity_condition,
state_diff: Default::default(),
da_slot_hash: st.da_block_header_of_commitments.hash(),
sequencer_public_key: vec![],
sequencer_da_public_key: vec![],
sequencer_commitments_range: (0, 0),
final_spec_id: SpecId::Genesis,
},
None,
))
Ok(sov_rollup_interface::zk::StateTransition {
initial_state_root: st.initial_state_root,
final_state_root: st.final_state_root,
initial_batch_hash: st.initial_batch_hash,
validity_condition: data.validity_condition,
state_diff: Default::default(),
da_slot_hash: st.da_block_header_of_commitments.hash(),
sequencer_public_key: vec![],
sequencer_da_public_key: vec![],
sequencer_commitments_range: (0, 0),
final_spec_id: SpecId::Genesis,
})
}
sov_rollup_interface::zk::Proof::Full(_) => {
panic!("Mock DA doesn't generate real proofs")
Expand All @@ -229,10 +219,10 @@ impl sov_rollup_interface::zk::Zkvm for MockZkGuest {

type Error = anyhow::Error;

fn verify<'a>(
_serialized_proof: &'a [u8],
fn verify(
_serialized_proof: &[u8],
_code_commitment: &Self::CodeCommitment,
) -> Result<&'a [u8], Self::Error> {
) -> Result<Vec<u8>, Self::Error> {
unimplemented!()
}

Expand Down
6 changes: 3 additions & 3 deletions crates/sovereign-sdk/adapters/risc0/src/guest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ impl Zkvm for Risc0Guest {

type Error = anyhow::Error;

fn verify<'a>(
_serialized_proof: &'a [u8],
fn verify(
_serialized_proof: &[u8],
_code_commitment: &Self::CodeCommitment,
) -> Result<&'a [u8], Self::Error> {
) -> Result<Vec<u8>, Self::Error> {
// Implement this method once risc0 supports recursion: issue #633
todo!("Implement once risc0 supports recursion: https://github.com/Sovereign-Labs/sovereign-sdk/issues/633")
}
Expand Down
38 changes: 16 additions & 22 deletions crates/sovereign-sdk/adapters/risc0/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,18 @@ impl<'a> ZkvmHost for Risc0Host<'a> {

fn extract_output<Da: sov_rollup_interface::da::DaSpec, Root: BorshDeserialize>(
proof: &Proof,
) -> Result<
(
sov_rollup_interface::zk::StateTransition<Da, Root>,
Option<Receipt>,
),
Self::Error,
> {
let (journal, receipt) = match proof {
) -> Result<sov_rollup_interface::zk::StateTransition<Da, Root>, Self::Error> {
let journal = match proof {
Proof::PublicInput(journal) => {
let journal: Journal = bincode::deserialize(journal)?;
(journal, None)
journal
}
Proof::Full(data) => {
let receipt: Receipt = bincode::deserialize(data)?;
(receipt.journal.clone(), Some(receipt))
receipt.journal
}
};
Ok((BorshDeserialize::try_from_slice(&journal.bytes)?, receipt))
Ok(BorshDeserialize::try_from_slice(&journal.bytes)?)
}

fn recover_proving_sessions(&self) -> Result<Vec<Proof>, anyhow::Error> {
Expand All @@ -122,10 +116,10 @@ impl<'host> Zkvm for Risc0Host<'host> {

type Error = anyhow::Error;

fn verify<'a>(
serialized_proof: &'a [u8],
fn verify(
serialized_proof: &[u8],
code_commitment: &Self::CodeCommitment,
) -> Result<&'a [u8], Self::Error> {
) -> Result<Vec<u8>, Self::Error> {
verify_from_slice(serialized_proof, code_commitment)
}

Expand All @@ -146,10 +140,10 @@ impl Zkvm for Risc0Verifier {

type Error = anyhow::Error;

fn verify<'a>(
serialized_proof: &'a [u8],
fn verify(
serialized_proof: &[u8],
code_commitment: &Self::CodeCommitment,
) -> Result<&'a [u8], Self::Error> {
) -> Result<Vec<u8>, Self::Error> {
verify_from_slice(serialized_proof, code_commitment)
}

Expand All @@ -162,11 +156,11 @@ impl Zkvm for Risc0Verifier {
}
}

fn verify_from_slice<'a>(
serialized_proof: &'a [u8],
fn verify_from_slice(
serialized_proof: &[u8],
code_commitment: &Risc0MethodId,
) -> Result<&'a [u8], anyhow::Error> {
let Risc0Proof::<'a> {
) -> Result<Vec<u8>, anyhow::Error> {
let Risc0Proof {
receipt, journal, ..
} = bincode::deserialize(serialized_proof)?;

Expand All @@ -176,7 +170,7 @@ fn verify_from_slice<'a>(

receipt.verify(code_commitment.0)?;

Ok(journal)
Ok(journal.to_vec())
}

/// A convenience type which contains the same data a Risc0 [`Receipt`] but borrows the journal
Expand Down
1 change: 0 additions & 1 deletion crates/sovereign-sdk/rollup-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ borsh = { workspace = true }
bytes = { workspace = true, optional = true, default-features = true }
digest = { workspace = true }
hex = { workspace = true }
risc0-zkvm = { workspace = true }
serde = { workspace = true }
sha2 = { workspace = true, optional = true }
thiserror = { workspace = true, optional = true }
Expand Down
15 changes: 5 additions & 10 deletions crates/sovereign-sdk/rollup-interface/src/state_machine/zk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use core::fmt::Debug;

use borsh::{BorshDeserialize, BorshSerialize};
use digest::Digest;
use risc0_zkvm::Receipt;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -57,7 +56,7 @@ pub trait ZkvmHost: Zkvm + Clone {
#[allow(clippy::type_complexity)]
fn extract_output<Da: DaSpec, Root: BorshDeserialize>(
proof: &Proof,
) -> Result<(StateTransition<Da, Root>, Option<Receipt>), Self::Error>;
) -> Result<StateTransition<Da, Root>, Self::Error>;

/// Host recovers pending proving sessions and returns proving results
fn recover_proving_sessions(&self) -> Result<Vec<Proof>, anyhow::Error>;
Expand All @@ -67,21 +66,17 @@ pub trait ZkvmHost: Zkvm + Clone {
/// Must support recursive proofs.
pub trait Zkvm: Send + Sync {
/// A commitment to the zkVM program which is being proven
type CodeCommitment: Clone
+ Debug
+ Serialize
+ DeserializeOwned
+ Into<risc0_zkvm::sha::Digest>;
type CodeCommitment: Clone + Debug + Serialize + DeserializeOwned;

/// The error type which is returned when a proof fails to verify
type Error: Debug;

/// Interpret a sequence of a bytes as a proof and attempt to verify it against the code commitment.
/// If the proof is valid, return a reference to the public outputs of the proof.
fn verify<'a>(
serialized_proof: &'a [u8],
fn verify(
serialized_proof: &[u8],
code_commitment: &Self::CodeCommitment,
) -> Result<&'a [u8], Self::Error>;
) -> Result<Vec<u8>, Self::Error>;

/// Same as [`verify`](Zkvm::verify), except that instead of returning the output
/// as a serialized array, it returns a state transition structure.
Expand Down

0 comments on commit af90bb8

Please sign in to comment.