Skip to content

Commit

Permalink
fix(bench): verify_fibair use volatile memory
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpwang committed Dec 31, 2024
1 parent fff23b3 commit edb901b
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 125 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions benchmarks/src/bin/fib_e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ async fn main() -> Result<()> {
stdin.write(&n);
run_with_metric_collection("OUTPUT_PATH", || {
let mut e2e_prover =
ContinuationProver::new(&halo2_params_reader, app_pk, app_committed_exe, full_agg_pk)
.with_profiling();
ContinuationProver::new(&halo2_params_reader, app_pk, app_committed_exe, full_agg_pk);
e2e_prover.set_program_name("fib_e2e");
let _proof = e2e_prover.generate_proof_for_evm(stdin);
});
Expand Down
47 changes: 22 additions & 25 deletions benchmarks/src/bin/verify_fibair.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
use clap::Parser;
use eyre::Result;
use openvm_benchmarks::utils::{bench_from_exe, BenchmarkCli};
use openvm_benchmarks::utils::BenchmarkCli;
use openvm_circuit::arch::instructions::program::DEFAULT_MAX_NUM_PUBLIC_VALUES;
use openvm_native_circuit::NativeConfig;
use openvm_native_compiler::conversion::CompilerOptions;
use openvm_native_recursion::testing_utils::inner::build_verification_program;
use openvm_sdk::config::AppConfig;
/// Benchmark of aggregation VM performance.
/// Proofs:
/// 1. Prove Fibonacci AIR.
/// 2. Verify the proof of 1. by execution VM program in STARK VM.
use openvm_sdk::{config::AppConfig, prover::AppProver, Sdk};
use openvm_stark_sdk::{
bench::run_with_metric_collection,
config::{baby_bear_poseidon2::BabyBearPoseidon2Engine, FriParameters},
dummy_airs::fib_air::chip::FibonacciChip,
engine::StarkFriEngine,
openvm_stark_backend::Chip,
};
use tracing::info_span;

/// Benchmark of aggregation VM performance.
/// Proofs:
/// 1. Prove Fibonacci AIR.
/// 2. Verify the proof of 1. by execution VM program in STARK VM.
fn main() -> Result<()> {
let cli_args = BenchmarkCli::parse();
let app_log_blowup = cli_args.app_log_blowup.unwrap_or(2);
Expand All @@ -30,36 +29,34 @@ fn main() -> Result<()> {
FriParameters::standard_with_100_bits_conjectured_security(app_log_blowup),
);

run_with_metric_collection("OUTPUT_PATH", || {
run_with_metric_collection("OUTPUT_PATH", || -> Result<()> {
// run_test tries to setup tracing, but it will be ignored since run_with_metric_collection already sets it.
let vdata = engine
.run_test(vec![fib_chip.generate_air_proof_input()])
.unwrap();
let leaf_fri_params =
// Unlike other apps, this "app" does not have continuations enabled.
let app_fri_params =
FriParameters::standard_with_100_bits_conjectured_security(agg_log_blowup);
// FIXME: this should be benchmarked as a single segment VM.
let app_vm_config = NativeConfig::aggregation(
DEFAULT_MAX_NUM_PUBLIC_VALUES,
leaf_fri_params.max_constraint_degree().min(7),
)
.with_continuations();
app_fri_params.max_constraint_degree().min(7),
);
let compiler_options = CompilerOptions::default();
let app_config = AppConfig {
app_fri_params: leaf_fri_params.into(),
app_fri_params: app_fri_params.into(),
app_vm_config,
leaf_fri_params: leaf_fri_params.into(),
leaf_fri_params: app_fri_params.into(),
compiler_options,
};
info_span!("Verify Fibonacci AIR").in_scope(|| {
let (program, input_stream) = build_verification_program(vdata, compiler_options);
bench_from_exe(
"verify_fibair",
app_config,
program,
input_stream.into(),
false,
)
})
let (program, input_stream) = build_verification_program(vdata, compiler_options);
let sdk = Sdk;
let app_pk = sdk.app_keygen(app_config)?;
let app_vk = app_pk.get_app_vk();
let committed_exe = sdk.commit_app_exe(app_fri_params, program.into())?;
let prover = AppProver::new(app_pk.app_vm_pk, committed_exe);
let proof = prover.generate_app_proof_without_continuations(input_stream.into());
sdk.verify_app_proof_without_continuations(&app_vk, &proof)?;
Ok(())
})?;
Ok(())
}
2 changes: 1 addition & 1 deletion crates/cli/src/commands/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl KeygenCmd {
pub fn run(&self) -> Result<()> {
let app_config = read_config_toml_or_default(&self.config)?;
let app_pk = Sdk.app_keygen(app_config)?;
write_app_vk_to_file(app_pk.get_vk(), &self.vk_output)?;
write_app_vk_to_file(app_pk.get_app_vk(), &self.vk_output)?;
write_app_pk_to_file(app_pk, &self.output)?;
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions crates/sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async-trait.workspace = true
metrics.workspace = true
tracing.workspace = true
itertools.workspace = true
getset.workspace = true

[features]
default = ["parallel"]
Expand Down
2 changes: 1 addition & 1 deletion crates/sdk/examples/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// ANCHOR: verification
// 10. Verify your program
let app_vk = app_pk.get_vk();
let app_vk = app_pk.get_app_vk();
sdk.verify_app_proof(&app_vk, &proof)?;
// ANCHOR_END: verification

Expand Down
3 changes: 1 addition & 2 deletions crates/sdk/src/keygen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ where
vm_pk.max_constraint_degree
<= config.app_fri_params.fri_params.max_constraint_degree()
);
assert!(config.app_vm_config.system().continuation_enabled);
VmProvingKey {
fri_params: config.app_fri_params.fri_params,
vm_config: config.app_vm_config.clone(),
Expand Down Expand Up @@ -122,7 +121,7 @@ where
self.app_vm_pk.vm_config.system().num_public_values
}

pub fn get_vk(&self) -> AppVerifyingKey {
pub fn get_app_vk(&self) -> AppVerifyingKey {
AppVerifyingKey {
fri_params: self.app_vm_pk.fri_params,
app_vm_vk: self.app_vm_pk.vm_pk.get_vk(),
Expand Down
13 changes: 11 additions & 2 deletions crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use openvm_native_recursion::{
},
types::InnerConfig,
};
use openvm_stark_backend::engine::StarkEngine;
use openvm_stark_backend::{engine::StarkEngine, prover::types::Proof};
use openvm_stark_sdk::{
config::{
baby_bear_poseidon2::{BabyBearPoseidon2Config, BabyBearPoseidon2Engine},
Expand Down Expand Up @@ -161,10 +161,19 @@ impl Sdk {
for seg_proof in &proof.per_segment {
e.verify(&app_vk.app_vm_vk, seg_proof)?
}
// TODO: verify continuation.
// TODO: verify continuation: requires App VC.
Ok(())
}

pub fn verify_app_proof_without_continuations(
&self,
app_vk: &AppVerifyingKey,
proof: &Proof<SC>,
) -> Result<(), VerificationError> {
let e = BabyBearPoseidon2Engine::new(app_vk.fri_params);
e.verify(&app_vk.app_vm_vk, proof)
}

pub fn agg_keygen(
&self,
config: AggConfig,
Expand Down
43 changes: 3 additions & 40 deletions crates/sdk/src/prover/agg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use std::sync::Arc;

#[cfg(feature = "bench-metrics")]
use openvm_circuit::arch::SingleSegmentVmExecutor;
use openvm_circuit::arch::Streams;
use openvm_native_circuit::NativeConfig;
use openvm_native_recursion::hints::Hintable;
use openvm_stark_sdk::{
config::baby_bear_poseidon2::BabyBearPoseidon2Engine, engine::StarkFriEngine,
config::baby_bear_poseidon2::BabyBearPoseidon2Engine,
openvm_stark_backend::prover::types::Proof,
};
use tracing::info_span;
Expand Down Expand Up @@ -37,13 +36,10 @@ pub struct AggStarkProver {

pub num_children_internal: usize,
pub max_internal_wrapper_layers: usize,

pub profile: bool,
}
pub struct LeafProver {
prover: VmLocalProver<SC, NativeConfig, BabyBearPoseidon2Engine>,
pub num_children_leaf: usize,
pub profile: bool,
}

impl AggStarkProver {
Expand All @@ -63,7 +59,6 @@ impl AggStarkProver {
root_prover,
num_children_internal: DEFAULT_NUM_CHILDREN_INTERNAL,
max_internal_wrapper_layers: DEFAULT_MAX_INTERNAL_WRAPPER_LAYERS,
profile: false,
}
}

Expand All @@ -82,16 +77,6 @@ impl AggStarkProver {
self
}

pub fn set_profile(&mut self, profile: bool) -> &mut Self {
self.profile = profile;
self.leaf_prover.profile = profile;
self
}
pub fn with_profiling(mut self) -> Self {
self.set_profile(true);
self
}

/// Generate a proof to aggregate app proofs.
pub fn generate_agg_proof(&self, app_proofs: ContinuationVmProof<SC>) -> Proof<RootSC> {
let leaf_proofs = self.leaf_prover.generate_proof(&app_proofs);
Expand Down Expand Up @@ -156,7 +141,7 @@ impl AggStarkProver {
hgt = internal_node_height
)
.in_scope(|| {
single_segment_prove(&self.internal_prover, input.write(), self.profile)
SingleSegmentVmProver::prove(&self.internal_prover, input.write())
})
})
.collect()
Expand Down Expand Up @@ -207,17 +192,12 @@ impl LeafProver {
Self {
prover,
num_children_leaf: DEFAULT_NUM_CHILDREN_LEAF,
profile: false,
}
}
pub fn with_num_children_leaf(mut self, num_children_leaf: usize) -> Self {
self.num_children_leaf = num_children_leaf;
self
}
pub fn with_profile(mut self) -> Self {
self.profile = true;
self
}
pub fn generate_proof(&self, app_proofs: &ContinuationVmProof<SC>) -> Vec<Proof<SC>> {
info_span!("leaf verifier", group = "leaf").in_scope(|| {
#[cfg(feature = "bench-metrics")]
Expand All @@ -232,31 +212,14 @@ impl LeafProver {
.enumerate()
.map(|(leaf_node_idx, input)| {
info_span!("leaf verifier proof", idx = leaf_node_idx).in_scope(|| {
single_segment_prove(&self.prover, input.write_to_stream(), self.profile)
SingleSegmentVmProver::prove(&self.prover, input.write_to_stream())
})
})
.collect::<Vec<_>>()
})
}
}

#[allow(unused)]
fn single_segment_prove<E: StarkFriEngine<SC>>(
prover: &VmLocalProver<SC, NativeConfig, E>,
input: impl Into<Streams<F>> + Clone,
profile: bool,
) -> Proof<SC> {
#[cfg(feature = "bench-metrics")]
if profile {
let mut vm_config = prover.pk.vm_config.clone();
vm_config.system.profiling = true;
let vm = SingleSegmentVmExecutor::new(vm_config);
vm.execute(prover.committed_exe.exe.clone(), input.clone())
.unwrap();
}
SingleSegmentVmProver::prove(prover, input)
}

fn heights_le(a: &[usize], b: &[usize]) -> bool {
assert_eq!(a.len(), b.len());
a.iter().zip(b.iter()).all(|(a, b)| a <= b)
Expand Down
Loading

0 comments on commit edb901b

Please sign in to comment.