Skip to content

Commit

Permalink
expose root verifier input (#1272)
Browse files Browse the repository at this point in the history
  • Loading branch information
luffykai authored Jan 24, 2025
1 parent 09bebcc commit d45b226
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
12 changes: 5 additions & 7 deletions crates/sdk/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
F, SC,
};

// TODO: remove these type specific functions
pub fn read_exe_from_file<P: AsRef<Path>>(path: P) -> Result<VmExe<F>> {
read_from_file_bitcode(path)
}
Expand Down Expand Up @@ -78,13 +79,13 @@ pub fn write_evm_verifier_to_file<P: AsRef<Path>>(verifier: EvmVerifier, path: P
write_to_file_bytes(path, verifier)
}

pub(crate) fn read_from_file_bitcode<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<T> {
pub fn read_from_file_bitcode<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<T> {
let data = std::fs::read(path)?;
let ret = bitcode::deserialize(&data)?;
Ok(ret)
}

pub(crate) fn write_to_file_bitcode<T: Serialize, P: AsRef<Path>>(path: P, data: T) -> Result<()> {
pub fn write_to_file_bitcode<T: Serialize, P: AsRef<Path>>(path: P, data: T) -> Result<()> {
let bytes = bitcode::serialize(&data)?;
if let Some(parent) = path.as_ref().parent() {
create_dir_all(parent)?;
Expand All @@ -93,15 +94,12 @@ pub(crate) fn write_to_file_bitcode<T: Serialize, P: AsRef<Path>>(path: P, data:
Ok(())
}

pub(crate) fn read_from_file_bytes<T: From<Vec<u8>>, P: AsRef<Path>>(path: P) -> Result<T> {
pub fn read_from_file_bytes<T: From<Vec<u8>>, P: AsRef<Path>>(path: P) -> Result<T> {
let bytes = read(path)?;
Ok(T::from(bytes))
}

pub(crate) fn write_to_file_bytes<T: Into<Vec<u8>>, P: AsRef<Path>>(
path: P,
data: T,
) -> Result<()> {
pub fn write_to_file_bytes<T: Into<Vec<u8>>, P: AsRef<Path>>(path: P, data: T) -> Result<()> {
if let Some(parent) = path.as_ref().parent() {
create_dir_all(parent)?;
}
Expand Down
21 changes: 19 additions & 2 deletions crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use openvm_transpiler::{
FromElf,
};
use prover::vm::ContinuationVmProof;
use verifier::root::types::RootVmVerifierInput;

pub mod commit;
pub mod config;
Expand All @@ -54,8 +55,8 @@ pub mod fs;

use crate::{
config::AggConfig,
keygen::AggProvingKey,
prover::{AppProver, ContinuationProver},
keygen::{AggProvingKey, AggStarkProvingKey},
prover::{AppProver, ContinuationProver, StarkProver},
};

pub type SC = BabyBearPoseidon2Config;
Expand Down Expand Up @@ -184,6 +185,22 @@ impl Sdk {
Ok(agg_pk)
}

pub fn generate_root_verifier_input<VC: VmConfig<F>>(
&self,
app_pk: Arc<AppProvingKey<VC>>,
app_exe: Arc<NonRootCommittedExe>,
agg_stark_pk: AggStarkProvingKey,
inputs: StdIn,
) -> Result<RootVmVerifierInput<SC>>
where
VC::Executor: Chip<SC>,
VC::Periphery: Chip<SC>,
{
let stark_prover = StarkProver::new(app_pk, app_exe, agg_stark_pk);
let proof = stark_prover.generate_root_verifier_input(inputs);
Ok(proof)
}

pub fn generate_evm_proof<VC: VmConfig<F>>(
&self,
reader: &impl Halo2ParamsReader,
Expand Down
12 changes: 10 additions & 2 deletions crates/sdk/src/prover/agg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,23 @@ impl AggStarkProver {

/// Generate a proof to aggregate app proofs.
pub fn generate_agg_proof(&self, app_proofs: ContinuationVmProof<SC>) -> Proof<RootSC> {
let root_verifier_input = self.generate_root_verifier_input(app_proofs);
self.generate_root_proof_impl(root_verifier_input)
}

pub fn generate_root_verifier_input(
&self,
app_proofs: ContinuationVmProof<SC>,
) -> RootVmVerifierInput<SC> {
let leaf_proofs = self
.leaf_controller
.generate_proof(&self.leaf_prover, &app_proofs);
let public_values = app_proofs.user_public_values.public_values;
let internal_proof = self.generate_internal_proof_impl(leaf_proofs, &public_values);
self.generate_root_proof_impl(RootVmVerifierInput {
RootVmVerifierInput {
proofs: vec![internal_proof],
public_values,
})
}
}

fn generate_internal_proof_impl(
Expand Down
5 changes: 1 addition & 4 deletions crates/sdk/src/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ pub mod vm;
#[allow(unused_imports)]
pub use stark::*;

use crate::{
keygen::AggProvingKey,
prover::{halo2::Halo2Prover, stark::StarkProver},
};
use crate::{keygen::AggProvingKey, prover::halo2::Halo2Prover};

pub struct ContinuationProver<VC> {
stark_prover: StarkProver<VC>,
Expand Down
11 changes: 11 additions & 0 deletions crates/sdk/src/prover/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use openvm_stark_backend::{prover::types::Proof, Chip};
use crate::{
keygen::{AggStarkProvingKey, AppProvingKey},
prover::{agg::AggStarkProver, app::AppProver},
verifier::root::types::RootVmVerifierInput,
NonRootCommittedExe, RootSC, StdIn, F, SC,
};

Expand Down Expand Up @@ -50,4 +51,14 @@ impl<VC> StarkProver<VC> {
let app_proof = self.app_prover.generate_app_proof(input);
self.agg_prover.generate_agg_proof(app_proof)
}

pub fn generate_root_verifier_input(&self, input: StdIn) -> RootVmVerifierInput<SC>
where
VC: VmConfig<F>,
VC::Executor: Chip<SC>,
VC::Periphery: Chip<SC>,
{
let app_proof = self.app_prover.generate_app_proof(input);
self.agg_prover.generate_root_verifier_input(app_proof)
}
}

0 comments on commit d45b226

Please sign in to comment.