openvm_verify_stark_host/
vk.rs1use 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#[derive(Clone, Debug, Serialize, Deserialize)]
16pub struct VmStarkVerifyingKey {
17 pub mvk: MultiStarkVerifyingKey<BabyBearPoseidon2Config>,
18 pub baseline: VerificationBaseline,
19}
20
21#[derive(Clone, Debug, Serialize, Deserialize)]
24pub struct VerificationBaseline {
25 pub app_exe_commit: Digest,
28 pub memory_dimensions: MemoryDimensions,
30 pub num_user_pvs: usize,
32 pub app_vk_commit: VkCommit,
34 pub leaf_vk_commit: VkCommit,
37 pub internal_for_leaf_vk_commit: VkCommit,
40 pub internal_recursive_vk_commit: VkCommit,
43 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}