Skip to content

Commit

Permalink
Small cleanup (#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaziciahmet authored Dec 24, 2024
1 parent 865d9b9 commit a2adbdf
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 45 deletions.
2 changes: 0 additions & 2 deletions crates/batch-prover/src/proving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ where
// save proof along with tx id to db, should be queryable by slot number or slot hash
// TODO: select output version based on spec
let (last_active_spec_id, circuit_output) = match Vm::extract_output::<
<Da as DaService>::Spec,
BatchProofCircuitOutput<<Da as DaService>::Spec, StateRoot>,
>(&proof)
{
Expand All @@ -335,7 +334,6 @@ where
Err(e) => {
info!("Failed to extract post fork 1 output from proof: {:?}. Trying to extract pre fork 1 output", e);
let output = Vm::extract_output::<
<Da as DaService>::Spec,
OldBatchProofCircuitOutput<<Da as DaService>::Spec, StateRoot>,
>(&proof)
.expect("Should be able to extract either pre or post fork 1 output");
Expand Down
7 changes: 3 additions & 4 deletions crates/batch-prover/tests/prover_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,9 @@ async fn spawn_prove(
}

fn extract_output_header(proof: &Vec<u8>) -> MockBlockHeader {
MockZkvm::extract_output::<
MockDaSpec,
BatchProofCircuitInput<'static, [u8; 0], Vec<u8>, MockDaSpec, ()>,
>(proof)
MockZkvm::extract_output::<BatchProofCircuitInput<'static, [u8; 0], Vec<u8>, MockDaSpec, ()>>(
proof,
)
.unwrap()
.da_block_header_of_commitments
}
2 changes: 0 additions & 2 deletions crates/fullnode/src/da_block_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ where

// TODO: select output version based on spec
let (last_active_spec_id, batch_proof_output) = match Vm::extract_output::<
<Da as DaService>::Spec,
BatchProofCircuitOutput<<Da as DaService>::Spec, StateRoot>,
>(&proof)
{
Expand All @@ -315,7 +314,6 @@ where
Err(e) => {
info!("Failed to extract post fork 1 output from proof: {:?}. Trying to extract pre fork 1 output", e);
let output = Vm::extract_output::<
<Da as DaService>::Spec,
OldBatchProofCircuitOutput<<Da as DaService>::Spec, StateRoot>,
>(&proof)
.expect("Should be able to extract either pre or post fork 1 output");
Expand Down
15 changes: 1 addition & 14 deletions crates/light-client-prover/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn run_circuit<DaV: DaVerifier, G: ZkvmGuest>(
// Extract previous light client proof output
let previous_light_client_proof_output =
if let Some(journal) = input.previous_light_client_proof_journal {
let prev_output = G::verify_and_extract_output::<LightClientCircuitOutput>(
let prev_output = G::verify_and_deserialize_output::<LightClientCircuitOutput>(
&journal,
&input.light_client_proof_method_id.into(),
)
Expand Down Expand Up @@ -205,16 +205,3 @@ pub fn run_circuit<DaV: DaVerifier, G: ZkvmGuest>(
batch_proof_method_ids,
})
}

#[test]
fn test_binary_search() {
let ve = [1, 4, 7, 9, 14];
let idx = ve.binary_search(&4); // 1
assert_eq!(idx, Ok(1));
let idx = ve.binary_search(&100); // 5 - 1
assert_eq!(idx, Err(5));
let idx = ve.binary_search(&7); // 2
assert_eq!(idx, Ok(2));
let idx = ve.binary_search(&8); // 3-1
assert_eq!(idx, Err(3));
}
4 changes: 1 addition & 3 deletions crates/light-client-prover/src/da_block_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,13 @@ where
for batch_proof in batch_proofs {
if let DaDataLightClient::Complete(proof) = batch_proof {
let last_l2_height = match Vm::extract_output::<
<Da as DaService>::Spec,
BatchProofCircuitOutput<<Da as DaService>::Spec, [u8; 32]>,
>(&proof)
{
Ok(output) => output.last_l2_height,
Err(e) => {
info!("Failed to extract post fork 1 output from proof: {:?}. Trying to extract pre fork 1 output", e);
Vm::extract_output::<
<Da as DaService>::Spec,
OldBatchProofCircuitOutput<<Da as DaService>::Spec, [u8; 32]>,
>(&proof)
.map_err(|_| anyhow!("Proof should be deserializable"))?;
Expand Down Expand Up @@ -262,7 +260,7 @@ where
.prove(light_client_elf, circuit_input, assumptions)
.await?;

let circuit_output = Vm::extract_output::<Da::Spec, LightClientCircuitOutput>(&proof)
let circuit_output = Vm::extract_output::<LightClientCircuitOutput>(&proof)
.expect("Should deserialize valid proof");

tracing::info!(
Expand Down
2 changes: 1 addition & 1 deletion crates/risc0/src/guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Zkvm for Risc0Guest {
Ok(T::try_from_slice(journal)?)
}

fn verify_and_extract_output<T: BorshDeserialize>(
fn verify_and_deserialize_output<T: BorshDeserialize>(
journal: &[u8],
code_commitment: &Self::CodeCommitment,
) -> Result<T, Self::Error> {
Expand Down
12 changes: 3 additions & 9 deletions crates/risc0/src/host.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//! This module implements the [`ZkvmHost`] trait for the RISC0 VM.
use std::io::Cursor;

use borsh::{BorshDeserialize, BorshSerialize};
use metrics::histogram;
use risc0_zkvm::sha::Digest;
Expand Down Expand Up @@ -162,9 +159,7 @@ impl ZkvmHost for Risc0BonsaiHost {
Ok(serialized_receipt)
}

fn extract_output<Da: sov_rollup_interface::da::DaSpec, T: BorshDeserialize>(
proof: &Proof,
) -> Result<T, Self::Error> {
fn extract_output<T: BorshDeserialize>(proof: &Proof) -> Result<T, Self::Error> {
let receipt: Receipt = bincode::deserialize(proof)?;
let journal = receipt.journal;

Expand Down Expand Up @@ -225,11 +220,10 @@ impl Zkvm for Risc0BonsaiHost {
}

fn deserialize_output<T: BorshDeserialize>(journal: &[u8]) -> Result<T, Self::Error> {
let mut reader = Cursor::new(journal);
Ok(T::deserialize_reader(&mut reader)?)
Ok(T::try_from_slice(journal)?)
}

fn verify_and_extract_output<T: BorshDeserialize>(
fn verify_and_deserialize_output<T: BorshDeserialize>(
serialized_proof: &[u8],
code_commitment: &Self::CodeCommitment,
) -> Result<T, Self::Error> {
Expand Down
8 changes: 3 additions & 5 deletions crates/sovereign-sdk/adapters/mock-zkvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl sov_rollup_interface::zk::Zkvm for MockZkvm {
}
}

fn verify_and_extract_output<T: BorshDeserialize>(
fn verify_and_deserialize_output<T: BorshDeserialize>(
serialized_proof: &[u8],
code_commitment: &Self::CodeCommitment,
) -> Result<T, Self::Error> {
Expand Down Expand Up @@ -201,9 +201,7 @@ impl sov_rollup_interface::zk::ZkvmHost for MockZkvm {
Ok(self.committed_data.pop_front().unwrap_or_default())
}

fn extract_output<Da: sov_rollup_interface::da::DaSpec, T: BorshDeserialize>(
proof: &Proof,
) -> Result<T, Self::Error> {
fn extract_output<T: BorshDeserialize>(proof: &Proof) -> Result<T, Self::Error> {
let data: ProofInfo = borsh::from_slice(proof)?;

T::try_from_slice(&data.hint).map_err(Into::into)
Expand Down Expand Up @@ -259,7 +257,7 @@ impl sov_rollup_interface::zk::Zkvm for MockZkGuest {
}
}

fn verify_and_extract_output<T: BorshDeserialize>(
fn verify_and_deserialize_output<T: BorshDeserialize>(
journal: &[u8],
_code_commitment: &Self::CodeCommitment,
) -> Result<T, Self::Error> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub trait ZkvmHost: Zkvm + Clone {
fn run(&mut self, elf: Vec<u8>, with_proof: bool) -> Result<Proof, anyhow::Error>;

/// Extracts public input and receipt from the proof.
fn extract_output<Da: DaSpec, T: BorshDeserialize>(proof: &Proof) -> Result<T, Self::Error>;
fn extract_output<T: BorshDeserialize>(proof: &Proof) -> Result<T, Self::Error>;

/// Host recovers pending proving sessions and returns proving results
fn recover_proving_sessions(&self) -> Result<Vec<Proof>, anyhow::Error>;
Expand Down Expand Up @@ -92,7 +92,7 @@ pub trait Zkvm: Send + Sync {
/// Same as [`verify`](Zkvm::verify), except that instead of returning the output
/// as a serialized array, it returns a state transition structure.
/// TODO: specify a deserializer for the output
fn verify_and_extract_output<T: BorshDeserialize>(
fn verify_and_deserialize_output<T: BorshDeserialize>(
serialized_proof: &[u8],
code_commitment: &Self::CodeCommitment,
) -> Result<T, Self::Error>;
Expand Down
2 changes: 1 addition & 1 deletion crates/sp1/src/guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Zkvm for SP1Guest {
unimplemented!()
}

fn verify_and_extract_output<T: BorshDeserialize>(
fn verify_and_deserialize_output<T: BorshDeserialize>(
_serialized_proof: &[u8],
_code_commitment: &Self::CodeCommitment,
) -> Result<T, Self::Error> {
Expand Down
4 changes: 2 additions & 2 deletions crates/sp1/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl ZkvmHost for SP1Host {
}
}

fn extract_output<Da: sov_rollup_interface::da::DaSpec, T: BorshDeserialize>(
fn extract_output<T: BorshDeserialize>(
proof: &Proof,
) -> Result<T, Self::Error> {
let public_values = match proof {
Expand Down Expand Up @@ -194,7 +194,7 @@ impl Zkvm for SP1Host {
Ok(proof.public_values.to_vec())
}

fn verify_and_extract_output<T: BorshDeserialize>(
fn verify_and_deserialize_output<T: BorshDeserialize>(
serialized_proof: &[u8],
code_commitment: &Self::CodeCommitment,
) -> Result<T, Self::Error> {
Expand Down

0 comments on commit a2adbdf

Please sign in to comment.