Skip to content

Commit

Permalink
Split zkvm trait (Sovereign-Labs#214)
Browse files Browse the repository at this point in the history
* Split zkvm trait

* Remove hint methods from mockzkvm

* Remove unused Error type from zkvm guest
  • Loading branch information
preston-evans98 authored Apr 28, 2023
1 parent 54e58be commit 68eb895
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
16 changes: 4 additions & 12 deletions sdk/src/state_machine/core/mocks.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use borsh::{BorshDeserialize, BorshSerialize};

use crate::zk::traits::{Matches, ProofTrait, ZkVm};
use crate::zk::traits::{Matches, ProofTrait, Zkvm};

use super::{traits::Witness, types::ArrayWitness};
use super::types::ArrayWitness;

#[derive(Debug, Clone, PartialEq, Eq, BorshDeserialize, BorshSerialize)]
pub struct MockCodeCommitment(pub [u8; 32]);
Expand Down Expand Up @@ -32,25 +32,17 @@ impl ProofTrait<MockZkvm> for MockProof {

pub struct MockZkvm(ArrayWitness);

impl ZkVm for MockZkvm {
impl Zkvm for MockZkvm {
type CodeCommitment = MockCodeCommitment;

type Proof = MockProof;

type Error = anyhow::Error;

fn write_to_guest<T: crate::serial::Encode>(&self, hint: T) {
self.0.add_hint(hint)
}

fn read_from_host<T: crate::serial::Decode>(&self) -> T {
self.0.get_hint()
}

fn verify(
proof: Self::Proof,
code_commitment: &Self::CodeCommitment,
) -> Result<<<Self as ZkVm>::Proof as crate::zk::traits::ProofTrait<Self>>::Output, Self::Error>
) -> Result<<<Self as Zkvm>::Proof as crate::zk::traits::ProofTrait<Self>>::Output, Self::Error>
{
proof.verify(code_commitment)
}
Expand Down
22 changes: 15 additions & 7 deletions sdk/src/state_machine/zk/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@ use borsh::{BorshDeserialize, BorshSerialize};
use crate::serial::{Decode, Encode};

/// A proof that a program was executed in a zkVM.
pub trait ZkVm {
pub trait ZkvmHost: Zkvm {
fn write_to_guest<T: Encode>(&self, item: T);
}

/// A Zk proof system capable of proving and verifying arbitrary Rust code
pub trait Zkvm {
type CodeCommitment: Matches<Self::CodeCommitment> + Clone;
type Proof: ProofTrait<Self>;
type Error: Debug;

fn write_to_guest<T: Encode>(&self, item: T);
fn read_from_host<T: Decode>(&self) -> T;
fn verify(
proof: Self::Proof,
code_commitment: &Self::CodeCommitment,
) -> Result<<<Self as ZkVm>::Proof as ProofTrait<Self>>::Output, Self::Error>;
) -> Result<<<Self as Zkvm>::Proof as ProofTrait<Self>>::Output, Self::Error>;
}

/// A proof that a program was executed in a zkVM.
pub trait ZkvmGuest: Zkvm {
fn read_from_host<T: Decode>(&self) -> T;
}

pub trait ProofTrait<VM: ZkVm + ?Sized> {
pub trait ProofTrait<VM: Zkvm + ?Sized> {
type Output: Encode + Decode;
/// Verify the proof, deserializing the result if successful.
fn verify(self, code_commitment: &VM::CodeCommitment) -> Result<Self::Output, VM::Error>;
Expand All @@ -28,13 +36,13 @@ pub trait Matches<T> {
fn matches(&self, other: &T) -> bool;
}

pub enum RecursiveProofInput<Vm: ZkVm, T, Pf: ProofTrait<Vm, Output = T>> {
pub enum RecursiveProofInput<Vm: Zkvm, T, Pf: ProofTrait<Vm, Output = T>> {
Base(T),
Recursive(Pf, std::marker::PhantomData<Vm>),
}

#[derive(BorshSerialize, BorshDeserialize)]
pub struct RecursiveProofOutput<Vm: ZkVm, T> {
pub struct RecursiveProofOutput<Vm: Zkvm, T> {
pub claimed_method_id: Vm::CodeCommitment,
pub output: T,
}
Expand Down

0 comments on commit 68eb895

Please sign in to comment.