openvm_verify_stark_host/
vk.rs

1use std::{
2    fs::{create_dir_all, read, write},
3    path::Path,
4};
5
6use eyre::{Report, Result};
7use openvm_circuit::system::memory::dimensions::MemoryDimensions;
8use openvm_stark_backend::keygen::types::MultiStarkVerifyingKey;
9use openvm_stark_sdk::config::baby_bear_poseidon2::{BabyBearPoseidon2Config, Digest};
10use serde::{Deserialize, Serialize};
11
12use crate::VkCommit;
13
14/// Verifying key and artifacts used to verify a STARK proof for a fixed VM and executable
15#[derive(Clone, Debug, Serialize, Deserialize)]
16pub struct VmStarkVerifyingKey {
17    pub mvk: MultiStarkVerifyingKey<BabyBearPoseidon2Config>,
18    pub baseline: VerificationBaseline,
19}
20
21/// Baseline artifacts for a specific VM and fixed executable that are used to verify a final
22/// (i.e. internal-recursive) VM STARK proof
23#[derive(Clone, Debug, Serialize, Deserialize)]
24pub struct VerificationBaseline {
25    /// Commit to the app exe (i.e. hash of the program commit, initial memory merkle root,
26    /// and initial program counter)
27    pub app_exe_commit: Digest,
28    /// VM memory metadata used to verify the user public values merkle proof
29    pub memory_dimensions: MemoryDimensions,
30    /// Number of raw user public values
31    pub num_user_pvs: usize,
32    /// Commit to the app_vk's DAG and its pre-hash, first exposed by the leaf verifier.
33    pub app_vk_commit: VkCommit,
34    /// Commit to the leaf_vk's DAG and its pre-hash, first exposed by the internal-for-leaf
35    /// verifier.
36    pub leaf_vk_commit: VkCommit,
37    /// Commit to the internal_for_leaf_vk's DAG and its pre-hash, first exposed by the first
38    /// (i.e. index 0) internal-recursive layer verifier.
39    pub internal_for_leaf_vk_commit: VkCommit,
40    /// Commit to the internal_recursive_vk's DAG and its pre-hash, exposed by subsequent (i.e.
41    /// index > 0) internal-recursive layer verifiers.
42    pub internal_recursive_vk_commit: VkCommit,
43    /// Expected deferral VK commit (hash of the deferral aggregation prover's vk commits).
44    /// When Some, the proof must have deferral_flag == 0 || deferral_flag == 2 and valid
45    /// deferral Merkle proofs. If deferral_flag == 2, def_hook_commit must match this value.
46    /// If deferral_flag == 0, the deferral public values must be unset and the Merkle proofs
47    /// must show that the deferral address space is unchanged. When None, there must be no
48    /// deferral public values.
49    pub expected_def_hook_commit: Option<Digest>,
50}
51
52pub fn read_vk_from_file<P: AsRef<Path>>(path: P) -> Result<VmStarkVerifyingKey> {
53    let ret = read(&path)
54        .map_err(|e| read_error(&path, e.into()))
55        .and_then(|data| {
56            bitcode::deserialize(&data).map_err(|e: bitcode::Error| read_error(&path, e.into()))
57        })?;
58    Ok(ret)
59}
60
61pub fn write_vk_to_file<P: AsRef<Path>>(path: P, vk: &VmStarkVerifyingKey) -> Result<()> {
62    if let Some(parent) = path.as_ref().parent() {
63        create_dir_all(parent).map_err(|e| write_error(&path, e.into()))?;
64    }
65    bitcode::serialize(vk)
66        .map_err(|e| write_error(&path, e.into()))
67        .and_then(|bytes| write(&path, bytes).map_err(|e| write_error(&path, e.into())))?;
68    Ok(())
69}
70
71fn read_error<P: AsRef<Path>>(path: P, error: Report) -> Report {
72    eyre::eyre!(
73        "reading from {} failed with the following error:\n    {}",
74        path.as_ref().display(),
75        error,
76    )
77}
78
79fn write_error<P: AsRef<Path>>(path: P, error: Report) -> Report {
80    eyre::eyre!(
81        "writing to {} failed with the following error:\n    {}",
82        path.as_ref().display(),
83        error,
84    )
85}