Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to Use ZkVm-Specific ProofReceipt #543

Merged
merged 11 commits into from
Dec 19, 2024
6 changes: 4 additions & 2 deletions crates/proof-impl/btc-blockspace/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use bitcoin::consensus::serialize;
use strata_zkvm::{ProofType, PublicValues, ZkVmHost, ZkVmInputBuilder, ZkVmProver, ZkVmResult};
use strata_zkvm::{
ProofType, PublicValues, ZkVmHost, ZkVmInputBuilder, ZkVmInputResult, ZkVmProver, ZkVmResult,
};

use crate::logic::{BlockspaceProofInput, BlockspaceProofOutput};

Expand All @@ -14,7 +16,7 @@ impl ZkVmProver for BtcBlockspaceProver {
}

/// Prepares the input for the zkVM.
fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmResult<B::Input>
fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmInputResult<B::Input>
where
B: ZkVmInputBuilder<'a>,
{
Expand Down
6 changes: 1 addition & 5 deletions crates/proof-impl/checkpoint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,7 @@

if let Some(prev_checkpoint) = prev_checkpoint {
let (checkpoint, proof) = prev_checkpoint;
zkvm.verify_groth16_proof(
&proof,
rollup_vk.as_bytes(),
&borsh::to_vec(&checkpoint).unwrap(),
);
zkvm.verify_groth16_proof(&proof, &rollup_vk.0, &borsh::to_vec(&checkpoint).unwrap());

Check warning on line 156 in crates/proof-impl/checkpoint/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/proof-impl/checkpoint/src/lib.rs#L156

Added line #L156 was not covered by tests
}

zkvm.commit_borsh(&output);
Expand Down
4 changes: 2 additions & 2 deletions crates/proof-impl/checkpoint/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use strata_primitives::params::RollupParams;
use strata_zkvm::{AggregationInput, PublicValues, ZkVmProver, ZkVmResult};
use strata_zkvm::{AggregationInput, PublicValues, ZkVmInputResult, ZkVmProver, ZkVmResult};

use crate::CheckpointProofOutput;

Expand All @@ -19,7 +19,7 @@ impl ZkVmProver for CheckpointProver {
strata_zkvm::ProofType::Groth16
}

fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmResult<B::Input>
fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmInputResult<B::Input>
where
B: strata_zkvm::ZkVmInputBuilder<'a>,
{
Expand Down
5 changes: 3 additions & 2 deletions crates/proof-impl/cl-agg/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use strata_proofimpl_cl_stf::L2BatchProofOutput;
use strata_zkvm::{
AggregationInput, ProofReceipt, PublicValues, VerificationKey, ZkVmProver, ZkVmResult,
AggregationInput, ProofReceipt, PublicValues, VerificationKey, ZkVmInputResult, ZkVmProver,
ZkVmResult,
};

pub struct ClAggInput {
Expand All @@ -18,7 +19,7 @@ impl ZkVmProver for ClAggProver {
strata_zkvm::ProofType::Compressed
}

fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmResult<B::Input>
fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmInputResult<B::Input>
where
B: strata_zkvm::ZkVmInputBuilder<'a>,
{
Expand Down
5 changes: 3 additions & 2 deletions crates/proof-impl/cl-stf/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use strata_primitives::params::RollupParams;
use strata_state::{block::L2Block, chain_state::Chainstate};
use strata_zkvm::{
AggregationInput, ProofReceipt, PublicValues, VerificationKey, ZkVmProver, ZkVmResult,
AggregationInput, ProofReceipt, PublicValues, VerificationKey, ZkVmInputResult, ZkVmProver,
ZkVmResult,
};

use crate::L2BatchProofOutput;
Expand All @@ -24,7 +25,7 @@ impl ZkVmProver for ClStfProver {
strata_zkvm::ProofType::Compressed
}

fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmResult<B::Input>
fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmInputResult<B::Input>
where
B: strata_zkvm::ZkVmInputBuilder<'a>,
{
Expand Down
4 changes: 2 additions & 2 deletions crates/proof-impl/evm-ee-stf/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use strata_zkvm::{ProofType, PublicValues, ZkVmProver, ZkVmResult};
use strata_zkvm::{ProofType, PublicValues, ZkVmInputResult, ZkVmProver, ZkVmResult};

use crate::{ELProofInput, ELProofPublicParams};

Expand All @@ -12,7 +12,7 @@ impl ZkVmProver for EvmEeProver {
ProofType::Compressed
}

fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmResult<B::Input>
fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmInputResult<B::Input>
where
B: strata_zkvm::ZkVmInputBuilder<'a>,
{
Expand Down
5 changes: 3 additions & 2 deletions crates/proof-impl/l1-batch/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use borsh::{BorshDeserialize, BorshSerialize};
use strata_state::l1::HeaderVerificationState;
use strata_zkvm::{
AggregationInput, ProofReceipt, PublicValues, VerificationKey, ZkVmProver, ZkVmResult,
AggregationInput, ProofReceipt, PublicValues, VerificationKey, ZkVmInputResult, ZkVmProver,
ZkVmResult,
};

use crate::logic::L1BatchProofOutput;
Expand All @@ -23,7 +24,7 @@ impl ZkVmProver for L1BatchProver {
strata_zkvm::ProofType::Compressed
}

fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmResult<B::Input>
fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmInputResult<B::Input>
where
B: strata_zkvm::ZkVmInputBuilder<'a>,
{
Expand Down
2 changes: 1 addition & 1 deletion crates/zkvm/adapters/native/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
fn verify_groth16_proof(
&self,
_proof: &Proof,
_verification_key: &[u8],
_verification_key: &[u8; 32],

Check warning on line 76 in crates/zkvm/adapters/native/src/env.rs

View check run for this annotation

Codecov / codecov/patch

crates/zkvm/adapters/native/src/env.rs#L76

Added line #L76 was not covered by tests
_public_params_raw: &[u8],
) {
}
Expand Down
23 changes: 10 additions & 13 deletions crates/zkvm/adapters/native/src/host.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{fmt, sync::Arc};

use strata_zkvm::{
Proof, ProofReceipt, ProofType, PublicValues, VerificationKey, ZkVmHost, ZkVmResult,
Proof, ProofReceipt, ProofType, PublicValues, VerificationKey, ZkVmError, ZkVmHost, ZkVmResult,
};

use crate::{env::NativeMachine, input::NativeMachineInputBuilder};
use crate::{env::NativeMachine, input::NativeMachineInputBuilder, proof::NativeProofReceipt};

type ProcessProofFn = dyn Fn(&NativeMachine) -> ZkVmResult<()> + Send + Sync;

Expand All @@ -15,36 +15,33 @@ pub struct NativeHost {

impl ZkVmHost for NativeHost {
type Input<'a> = NativeMachineInputBuilder;
type ZkVmProofReceipt = NativeProofReceipt;

fn prove<'a>(
fn prove_inner<'a>(
&self,
native_machine: NativeMachine,
_proof_type: ProofType,
) -> ZkVmResult<ProofReceipt> {
) -> ZkVmResult<NativeProofReceipt> {
prajwolrg marked this conversation as resolved.
Show resolved Hide resolved
(self.process_proof)(&native_machine)?;
let output = native_machine.state.borrow().output.clone();
let proof = Proof::default();
let public_values = PublicValues::new(output);
Ok(ProofReceipt::new(proof, public_values))
Ok(ProofReceipt::new(proof, public_values).try_into()?)
}

fn get_verification_key(&self) -> VerificationKey {
VerificationKey::default()
}

fn extract_borsh_public_output<T: borsh::BorshDeserialize>(
public_values_raw: &PublicValues,
) -> ZkVmResult<T> {
borsh::from_slice(public_values_raw.as_bytes()).map_err(|e| e.into())
}

fn extract_serde_public_output<T: serde::Serialize + serde::de::DeserializeOwned>(
public_values_raw: &PublicValues,
) -> ZkVmResult<T> {
bincode::deserialize(public_values_raw.as_bytes()).map_err(|e| e.into())
let public_params: T = bincode::deserialize(public_values_raw.as_bytes())
.map_err(|e| ZkVmError::OutputExtractionError { source: e.into() })?;
Ok(public_params)
}

fn verify(&self, _proof: &ProofReceipt) -> ZkVmResult<()> {
fn verify_inner(&self, _proof: &NativeProofReceipt) -> ZkVmResult<()> {
Ok(())
}
}
Expand Down
13 changes: 7 additions & 6 deletions crates/zkvm/adapters/native/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
use strata_zkvm::{AggregationInput, ZkVmInputBuilder, ZkVmResult};
use strata_zkvm::{AggregationInput, ProofReceipt, ZkVmInputBuilder, ZkVmInputResult};

use crate::env::NativeMachine;

pub struct NativeMachineInputBuilder(pub NativeMachine);

impl<'a> ZkVmInputBuilder<'a> for NativeMachineInputBuilder {
type Input = NativeMachine;
type ZkVmProofReceipt = ProofReceipt;

fn new() -> NativeMachineInputBuilder {
Self(NativeMachine::new())
}

fn write_buf(&mut self, item: &[u8]) -> ZkVmResult<&mut Self> {
fn write_buf(&mut self, item: &[u8]) -> ZkVmInputResult<&mut Self> {
self.0.write_slice(item.to_vec());
Ok(self)
}

fn write_serde<T: serde::Serialize>(&mut self, item: &T) -> ZkVmResult<&mut Self> {
fn write_serde<T: serde::Serialize>(&mut self, item: &T) -> ZkVmInputResult<&mut Self> {
let slice = bincode::serialize(&item)?;
self.write_buf(&slice)
}

fn write_borsh<T: borsh::BorshSerialize>(&mut self, item: &T) -> ZkVmResult<&mut Self> {
fn write_borsh<T: borsh::BorshSerialize>(&mut self, item: &T) -> ZkVmInputResult<&mut Self> {
let slice = borsh::to_vec(item)?;
self.write_buf(&slice)
}

fn write_proof(&mut self, item: &AggregationInput) -> ZkVmResult<&mut Self> {
fn write_proof(&mut self, item: &AggregationInput) -> ZkVmInputResult<&mut Self> {
// For the native mode we only write the public values since the proof is expected to be
// empty
self.write_buf(item.receipt().public_values().as_bytes())
}

fn build(&mut self) -> ZkVmResult<Self::Input> {
fn build(&mut self) -> ZkVmInputResult<Self::Input> {
Ok(self.0.clone())
}
}
1 change: 1 addition & 0 deletions crates/zkvm/adapters/native/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod env;
mod host;
mod input;
mod proof;

pub use env::NativeMachine;
pub use host::NativeHost;
18 changes: 18 additions & 0 deletions crates/zkvm/adapters/native/src/proof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use strata_zkvm::{ProofReceipt, ZkVmProofError};

#[derive(Debug, Clone)]
pub struct NativeProofReceipt(ProofReceipt);

impl TryFrom<ProofReceipt> for NativeProofReceipt {
type Error = ZkVmProofError;
fn try_from(value: ProofReceipt) -> Result<Self, Self::Error> {
Ok(NativeProofReceipt(value))
}
}

impl TryFrom<NativeProofReceipt> for ProofReceipt {
type Error = ZkVmProofError;
fn try_from(value: NativeProofReceipt) -> Result<Self, Self::Error> {
Ok(value.0)
}
}
2 changes: 1 addition & 1 deletion crates/zkvm/adapters/risc0/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
fn verify_groth16_proof(
&self,
proof: &Proof,
verification_key: &[u8],
verification_key: &[u8; 32],

Check warning on line 37 in crates/zkvm/adapters/risc0/src/env.rs

View check run for this annotation

Codecov / codecov/patch

crates/zkvm/adapters/risc0/src/env.rs#L37

Added line #L37 was not covered by tests
public_params_raw: &[u8],
) {
verify_groth16(proof, verification_key, public_params_raw).unwrap();
Expand Down
32 changes: 12 additions & 20 deletions crates/zkvm/adapters/risc0/src/host.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use std::fmt;

use hex::encode;
use risc0_zkvm::{
compute_image_id, default_prover, sha::Digest, InnerReceipt, Journal, ProverOpts, Receipt,
};
use risc0_zkvm::{compute_image_id, default_prover, sha::Digest, Journal, ProverOpts};
use serde::{de::DeserializeOwned, Serialize};
use strata_zkvm::{
Proof, ProofReceipt, ProofType, PublicValues, VerificationKey, ZkVmError, ZkVmHost,
ZkVmInputBuilder, ZkVmResult,
ProofType, PublicValues, VerificationKey, ZkVmError, ZkVmHost, ZkVmInputBuilder, ZkVmResult,
};

use crate::input::Risc0ProofInputBuilder;
use crate::{input::Risc0ProofInputBuilder, proof::Risc0ProofReceipt};

/// A host for the `Risc0` zkVM that stores the guest program in ELF format
/// The `Risc0Host` is responsible for program execution and proving
Expand All @@ -32,12 +29,13 @@ impl Risc0Host {

impl ZkVmHost for Risc0Host {
type Input<'a> = Risc0ProofInputBuilder<'a>;
type ZkVmProofReceipt = Risc0ProofReceipt;

fn prove<'a>(
fn prove_inner<'a>(
&self,
prover_input: <Self::Input<'a> as ZkVmInputBuilder<'a>>::Input,
proof_type: ProofType,
) -> ZkVmResult<ProofReceipt> {
) -> ZkVmResult<Risc0ProofReceipt> {
#[cfg(feature = "mock")]
{
std::env::set_var("RISC0_DEV_MODE", "true");
Expand All @@ -57,11 +55,7 @@ impl ZkVmHost for Risc0Host {
.prove_with_opts(prover_input, &self.elf, &opts)
.map_err(|e| ZkVmError::ProofGenerationError(e.to_string()))?;

// Proof serialization
let proof = Proof::new(bincode::serialize(&proof_info.receipt.inner)?);
let public_values = PublicValues::new(proof_info.receipt.journal.bytes);

Ok(ProofReceipt::new(proof, public_values))
Ok(proof_info.receipt.into())
}

fn extract_serde_public_output<T: Serialize + DeserializeOwned>(
Expand All @@ -70,20 +64,18 @@ impl ZkVmHost for Risc0Host {
let journal = Journal::new(proof.as_bytes().to_vec());
journal
.decode()
.map_err(|e| ZkVmError::DeserializationError {
source: strata_zkvm::DeserializationErrorSource::Serde(e.to_string()),
.map_err(|e| ZkVmError::OutputExtractionError {
source: strata_zkvm::DataFormatError::Serde(e.to_string()),
})
}

fn get_verification_key(&self) -> VerificationKey {
VerificationKey::new(self.id.as_bytes().to_vec())
}

fn verify(&self, proof: &ProofReceipt) -> ZkVmResult<()> {
let journal = proof.public_values().as_bytes().to_vec();
let inner: InnerReceipt = bincode::deserialize(proof.proof().as_bytes())?;
let receipt = Receipt::new(inner, journal);
receipt
fn verify_inner(&self, proof: &Risc0ProofReceipt) -> ZkVmResult<()> {
proof
.as_ref()
.verify(self.id)
.map_err(|e| ZkVmError::ProofVerificationError(e.to_string()))?;
Ok(())
Expand Down
Loading
Loading