Skip to content

Commit 395e9f3

Browse files
committed
Move verify_openvm_stark into a new crate & refactor helper functions for verify_openvm_stark
1 parent c43c618 commit 395e9f3

File tree

14 files changed

+241
-158
lines changed

14 files changed

+241
-158
lines changed

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ members = [
6666
"extensions/pairing/transpiler",
6767
"extensions/pairing/guest",
6868
"extensions/pairing/tests",
69+
"guest-libs/verify_stark/guest",
70+
"guest-libs/verify_stark/tests",
6971
]
7072
exclude = ["crates/sdk/example"]
7173
resolver = "2"
@@ -162,6 +164,7 @@ openvm-ecc-sw-macros = { path = "extensions/ecc/sw-macros", default-features = f
162164
openvm-pairing-circuit = { path = "extensions/pairing/circuit", default-features = false }
163165
openvm-pairing-transpiler = { path = "extensions/pairing/transpiler", default-features = false }
164166
openvm-pairing-guest = { path = "extensions/pairing/guest", default-features = false }
167+
openvm-verify-stark = { path = "guest-libs/verify_stark/guest", default-features = false }
165168

166169
# Benchmarking
167170
openvm-benchmarks-utils = { path = "benchmarks/utils", default-features = false }

crates/sdk/guest/verify_openvm_stark/src/main.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

crates/sdk/tests/integration_test.rs

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use std::{borrow::Borrow, path::PathBuf, sync::Arc};
22

33
use eyre::Result;
4-
use openvm::host::{compute_hint_key_for_verify_openvm_stark, encode_rv32_public_values};
54
use openvm_build::GuestOptions;
65
use openvm_circuit::{
76
arch::{
87
hasher::poseidon2::vm_poseidon2_hasher, ContinuationVmProof, ExecutionError,
98
GenerationError, SingleSegmentVmExecutor, SystemConfig, VmConfig, VmExecutor,
10-
DEFAULT_MAX_NUM_PUBLIC_VALUES,
119
},
1210
system::{memory::tree::public_values::UserPublicValuesProof, program::trace::VmCommittedExe},
1311
};
@@ -29,19 +27,17 @@ use openvm_native_recursion::{
2927
wrapper::Halo2WrapperProvingKey,
3028
RawEvmProof,
3129
},
32-
hints::Hintable,
3330
types::InnerConfig,
3431
vars::StarkProofVariable,
3532
};
36-
use openvm_rv32im_guest::hint_load_by_key_encode;
3733
use openvm_rv32im_transpiler::{
3834
Rv32ITranspilerExtension, Rv32IoTranspilerExtension, Rv32MTranspilerExtension,
3935
};
4036
use openvm_sdk::{
4137
codec::{Decode, Encode},
4238
commit::AppExecutionCommit,
4339
config::{AggConfig, AggStarkConfig, AppConfig, Halo2Config, SdkSystemConfig, SdkVmConfig},
44-
keygen::{AggStarkProvingKey, AppProvingKey},
40+
keygen::AppProvingKey,
4541
types::{EvmHalo2Verifier, EvmProof},
4642
DefaultStaticVerifierPvHandler, Sdk, StdIn,
4743
};
@@ -578,90 +574,3 @@ fn test_segmentation_retry() {
578574
.sum();
579575
assert!(new_total_height < total_height);
580576
}
581-
#[test]
582-
fn test_verify_openvm_stark_e2e() -> Result<()> {
583-
const ASM_FILENAME: &str = "root_verifier.asm";
584-
let sdk = Sdk::new();
585-
let guest_opts = GuestOptions::default();
586-
let mut pkg_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).to_path_buf();
587-
pkg_dir.push("guest/fib");
588-
let elf = sdk.build(guest_opts.clone(), &pkg_dir, &Default::default())?;
589-
590-
let vm_config = SdkVmConfig::builder()
591-
.system(SdkSystemConfig {
592-
config: SystemConfig::default().with_continuations(),
593-
})
594-
.rv32i(Default::default())
595-
.rv32m(Default::default())
596-
.io(Default::default())
597-
.native(Default::default())
598-
.build();
599-
assert!(vm_config.system.config.continuation_enabled);
600-
601-
let app_exe = sdk.transpile(elf, vm_config.transpiler())?;
602-
let fri_params = FriParameters::new_for_testing(LEAF_LOG_BLOWUP);
603-
let app_config = AppConfig::new_with_leaf_fri_params(fri_params, vm_config.clone(), fri_params);
604-
605-
let app_pk = sdk.app_keygen(app_config.clone())?;
606-
let committed_app_exe = sdk.commit_app_exe(fri_params, app_exe.clone())?;
607-
608-
let commits =
609-
AppExecutionCommit::compute(&vm_config, &committed_app_exe, &app_pk.leaf_committed_exe);
610-
611-
let agg_pk = AggStarkProvingKey::keygen(AggStarkConfig {
612-
max_num_user_public_values: DEFAULT_MAX_NUM_PUBLIC_VALUES,
613-
leaf_fri_params: FriParameters::new_for_testing(LEAF_LOG_BLOWUP),
614-
internal_fri_params: FriParameters::new_for_testing(INTERNAL_LOG_BLOWUP),
615-
root_fri_params: FriParameters::new_for_testing(ROOT_LOG_BLOWUP),
616-
profiling: false,
617-
compiler_options: CompilerOptions {
618-
enable_cycle_tracker: true,
619-
..Default::default()
620-
},
621-
root_max_constraint_degree: (1 << ROOT_LOG_BLOWUP) + 1,
622-
});
623-
let asm = sdk.generate_root_verifier_asm(&agg_pk);
624-
let asm_path = format!(
625-
"{}/guest/verify_openvm_stark/{}",
626-
env!("CARGO_MANIFEST_DIR"),
627-
ASM_FILENAME
628-
);
629-
std::fs::write(asm_path, asm)?;
630-
631-
let e2e_stark_proof = sdk.generate_e2e_stark_proof(
632-
Arc::new(app_pk),
633-
committed_app_exe,
634-
agg_pk,
635-
StdIn::default(),
636-
)?;
637-
638-
let verify_exe = {
639-
let mut pkg_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).to_path_buf();
640-
pkg_dir.push("guest/verify_openvm_stark");
641-
let elf = sdk.build(guest_opts.clone(), &pkg_dir, &Default::default())?;
642-
sdk.transpile(elf, vm_config.transpiler())?
643-
};
644-
645-
// app_exe publishes 7th and 8th fibonacci numbers.
646-
let pvs = [13u32, 21, 0, 0, 0, 0, 0, 0];
647-
let pvs_u32 = encode_rv32_public_values(&pvs);
648-
649-
let mut stdin = StdIn::default();
650-
let key = compute_hint_key_for_verify_openvm_stark(
651-
ASM_FILENAME,
652-
&commits.exe_commit,
653-
&commits.vm_commit,
654-
&pvs_u32,
655-
);
656-
let to_encode: Vec<Vec<F>> = e2e_stark_proof.proof.write();
657-
let value = hint_load_by_key_encode(&to_encode);
658-
stdin.add_key_value(key, value);
659-
660-
stdin.write(&commits.exe_commit);
661-
stdin.write(&commits.vm_commit);
662-
stdin.write(&pvs_u32);
663-
664-
sdk.execute(verify_exe, vm_config, stdin)?;
665-
666-
Ok(())
667-
}

crates/toolchain/openvm/src/host.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,33 +98,6 @@ pub fn read_u32() -> u32 {
9898
u32::from_le_bytes(bytes.try_into().unwrap())
9999
}
100100

101-
/// Compute the hint key for `verify_openvm_stark` function, which reads a stark proof from stream
102-
/// `kv_store`.
103-
pub fn compute_hint_key_for_verify_openvm_stark(
104-
asm_filename: &str,
105-
exe_commit_u32: &[u32; 8],
106-
vm_commit_u32: &[u32; 8],
107-
pvs_u32: &[u32],
108-
) -> Vec<u8> {
109-
asm_filename
110-
.as_bytes()
111-
.iter()
112-
.cloned()
113-
.chain(exe_commit_u32.iter().flat_map(|x| x.to_le_bytes()))
114-
.chain(vm_commit_u32.iter().flat_map(|x| x.to_le_bytes()))
115-
.chain(pvs_u32.iter().flat_map(|x| x.to_le_bytes()))
116-
.collect()
117-
}
118-
119-
/// Encode the public values in guest program(revealed by `reveal_u32`) into the format in proofs.
120-
pub fn encode_rv32_public_values(logic_pvs: &[u32]) -> Vec<u32> {
121-
logic_pvs
122-
.iter()
123-
.flat_map(|x| x.to_le_bytes())
124-
.map(|x| x as u32)
125-
.collect()
126-
}
127-
128101
#[cfg(all(feature = "std", test, not(target_os = "zkvm")))]
129102
mod tests {
130103
use alloc::vec;

crates/toolchain/openvm/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ pub mod utils;
2929

3030
#[cfg(not(target_os = "zkvm"))]
3131
pub mod host;
32-
#[cfg(target_os = "zkvm")]
33-
pub mod verify_stark;
3432

3533
#[cfg(target_os = "zkvm")]
3634
core::arch::global_asm!(include_str!("memset.s"));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "openvm-verify-stark"
3+
description = "OpenVM guest library for verifying STARKs"
4+
version.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
authors.workspace = true
8+
homepage.workspace = true
9+
repository.workspace = true
10+
license.workspace = true
11+
12+
[target.'cfg(not(target_os = "zkvm"))'.dependencies]
13+
openvm-native-recursion.workspace = true
14+
openvm-rv32im-guest.workspace = true
15+
openvm-sdk = { workspace = true }
16+
openvm-stark-sdk = { workspace = true }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use openvm_native_recursion::hints::Hintable;
2+
use openvm_rv32im_guest::hint_load_by_key_encode;
3+
use openvm_sdk::SC;
4+
use openvm_stark_sdk::{openvm_stark_backend::proof::Proof, p3_baby_bear::BabyBear};
5+
6+
/// Compute the hint key for `verify_openvm_stark` function, which reads a stark proof from stream
7+
/// `kv_store`.
8+
pub fn compute_hint_key_for_verify_openvm_stark(
9+
asm_filename: &str,
10+
exe_commit_u32: &[u32; 8],
11+
vm_commit_u32: &[u32; 8],
12+
pvs: &[u8],
13+
) -> Vec<u8> {
14+
asm_filename
15+
.as_bytes()
16+
.iter()
17+
.cloned()
18+
.chain(exe_commit_u32.iter().flat_map(|x| x.to_le_bytes()))
19+
.chain(vm_commit_u32.iter().flat_map(|x| x.to_le_bytes()))
20+
.chain(pvs.iter().cloned())
21+
.collect()
22+
}
23+
24+
/// Encode a proof into a KV store value so `verify_openvm_stark` can hint it.
25+
pub fn encode_proof_to_kv_store_value(proof: &Proof<SC>) -> Vec<u8> {
26+
let to_encode: Vec<Vec<BabyBear>> = proof.write();
27+
hint_load_by_key_encode(&to_encode)
28+
}

crates/toolchain/openvm/src/verify_stark.rs renamed to guest-libs/verify_stark/guest/src/lib.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1+
#[cfg(not(target_os = "zkvm"))]
2+
pub mod host;
3+
14
/// Define a function that verifies an OpenVM Stark proof.
25
/// To define this function, users need to specify the function name and an ASM file containing the
36
/// assembly code for the verification(this ASM file can be generated by
47
/// `Sdk.generate_root_verifier_asm` function). To specify the ASM file, users need to provide the
58
/// parent folder and filename of the ASM file.
69
/// To call this function:
710
/// 1. users need to provide `app_exe_commit`/`app_vm_commit`/`user_pvs`(user public values) as the
8-
/// arguments. CAREFUL: `app_exe_commit`/`app_vm_commit`/`user_pvs` are in u32 and are interpreted
9-
/// as native fields. `user_pvs` are the exact public values of that proof. Which means, if a guest
10-
/// program calls `reveal_u32` to publish an u32(e.g. `258`) in the proof, that u32 is decomposed
11-
/// into 4 field elements in byte. In `user_pvs`, it should be an u32 array, `[2, 1, 0, 0]`.
11+
/// arguments. CAREFUL: `app_exe_commit`/`app_vm_commit` are in u32 and are interpreted as native
12+
/// fields. `user_pvs` are the exact public values of that proof. Here we assume all the public
13+
/// values are `u8`s.
1214
/// 2. Provide the corresponding stark proof in the key-value store in OpenVM streams. The key
13-
/// should be the concatenation of the filename, `app_exe_commit`, `app_vm_commit`, and `user_pvs`
14-
/// in little-endian bytes. Users can use `openvm::host::compute_hint_key_for_verify_openvm_stark`
15-
/// to compute the hint key.
15+
/// should be the concatenation of the filename, `app_exe_commit`, `app_vm_commit`, and
16+
/// `user_pvs` in little-endian bytes. Users can use
17+
/// `openvm::host::compute_hint_key_for_verify_openvm_stark` to compute the hint key.
1618
#[macro_export]
1719
macro_rules! define_verify_openvm_stark {
1820
($fn_name: ident, $asm_folder: expr, $asm_filename: literal) => {
19-
pub fn $fn_name(app_exe_commit: &[u32; 8], app_vm_commit: &[u32; 8], user_pvs: &[u32]) {
21+
pub fn $fn_name(app_exe_commit: &[u32; 8], app_vm_commit: &[u32; 8], user_pvs: &[u8]) {
2022
// The memory location for the start of the heap.
2123
const HEAP_START_ADDRESS: u32 = 1 << 24;
2224
const FIELDS_PER_U32: u32 = 4;
@@ -28,7 +30,7 @@ macro_rules! define_verify_openvm_stark {
2830
.cloned()
2931
.chain(app_exe_commit.iter().flat_map(|x| x.to_le_bytes()))
3032
.chain(app_vm_commit.iter().flat_map(|x| x.to_le_bytes()))
31-
.chain(user_pvs.iter().flat_map(|x| x.to_le_bytes()))
33+
.chain(user_pvs.iter().cloned())
3234
.collect();
3335
openvm::io::hint_load_by_key(&hint_key);
3436
// Store the expected public values into the beginning of the native heap.
@@ -42,7 +44,7 @@ macro_rules! define_verify_openvm_stark {
4244
native_addr += FIELDS_PER_U32;
4345
}
4446
for &x in user_pvs {
45-
openvm::io::store_u32_to_native(native_addr, x);
47+
openvm::io::store_u32_to_native(native_addr, x as u32);
4648
native_addr += FIELDS_PER_U32;
4749
}
4850
// Assumption: the asm file should be generated by SDK. The code block should:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "openvm-verify-stark-integration-tests"
3+
description = "Integration tests for OpenVM verify-stark"
4+
version.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
authors.workspace = true
8+
homepage.workspace = true
9+
repository.workspace = true
10+
license.workspace = true
11+
12+
[dev-dependencies]
13+
openvm-sdk = { workspace = true }
14+
openvm-circuit.workspace = true
15+
openvm-stark-sdk = { workspace = true }
16+
openvm-build.workspace = true
17+
openvm-native-compiler.workspace = true
18+
openvm-verify-stark.workspace = true
19+
eyre.workspace = true
20+
21+
[features]
22+
default = ["openvm-circuit/parallel"]

crates/sdk/guest/verify_openvm_stark/Cargo.toml renamed to guest-libs/verify_stark/tests/examples/verify_openvm_stark/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ version = "0.0.0"
55
edition = "2021"
66

77
[dependencies]
8-
openvm = { path = "../../../toolchain/openvm", features = ["std"] }
9-
hex-literal = { version = "0.4.1", default-features = false }
10-
bytemuck = { version = "1.20.0", features = ["extern_crate_alloc"] }
8+
openvm = { path = "../../../../../crates/toolchain/openvm", features = ["std"] }
9+
openvm-verify-stark = { path = "../../../guest" }
1110

1211
[features]
1312
default = []
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
extern crate alloc;
2+
use alloc::vec::Vec;
3+
4+
use openvm::io::read;
5+
use openvm_verify_stark::define_verify_openvm_stark;
6+
7+
define_verify_openvm_stark!(
8+
verify_openvm_stark,
9+
env!("CARGO_MANIFEST_DIR"),
10+
"root_verifier.asm"
11+
);
12+
13+
pub fn main() {
14+
let app_exe_commit: [u32; 8] = read();
15+
let app_vm_commit: [u32; 8] = read();
16+
let pvs: Vec<u8> = read();
17+
verify_openvm_stark(&app_exe_commit, &app_vm_commit, &pvs);
18+
}

0 commit comments

Comments
 (0)