openvm_continuations/verifier/root/
types.rs

1use std::array;
2
3use derivative::Derivative;
4use openvm_native_compiler::ir::{Builder, Config, Felt, DIGEST_SIZE};
5use openvm_stark_sdk::{
6    config::baby_bear_poseidon2::BabyBearPoseidon2Config,
7    openvm_stark_backend::{
8        config::{Com, StarkGenericConfig, Val},
9        p3_field::PrimeField32,
10        proof::Proof,
11    },
12};
13use serde::{de::DeserializeOwned, Deserialize, Serialize};
14use static_assertions::assert_impl_all;
15
16#[derive(Debug)]
17pub struct RootVmVerifierPvs<T> {
18    /// The commitment of the App VM executable.
19    pub exe_commit: [T; DIGEST_SIZE],
20    /// The commitment of the leaf verifier program, which commits the VM config of App VM.
21    pub leaf_verifier_commit: [T; DIGEST_SIZE],
22    /// Raw public values from App VM execution.
23    pub public_values: Vec<T>,
24}
25
26/// Input for the root VM verifier.
27/// Note: Root verifier is proven in Root SC, but it usually verifies proofs in SC. So
28/// usually only RootVmVerifierInput\<SC\> is needed.
29#[derive(Serialize, Deserialize, Derivative)]
30#[serde(bound = "")]
31#[derivative(Clone(bound = "Com<SC>: Clone"))]
32pub struct RootVmVerifierInput<SC: StarkGenericConfig> {
33    /// The proofs of leaf verifier or internal verifier in the execution order.
34    pub proofs: Vec<Proof<SC>>,
35    /// Public values to expose directly
36    pub public_values: Vec<Val<SC>>,
37}
38assert_impl_all!(RootVmVerifierInput<BabyBearPoseidon2Config>: Serialize, DeserializeOwned);
39
40impl<F: PrimeField32> RootVmVerifierPvs<Felt<F>> {
41    pub fn uninit<C: Config<F = F>>(builder: &mut Builder<C>, num_public_values: usize) -> Self {
42        Self {
43            exe_commit: array::from_fn(|_| builder.uninit()),
44            leaf_verifier_commit: array::from_fn(|_| builder.uninit()),
45            public_values: (0..num_public_values).map(|_| builder.uninit()).collect(),
46        }
47    }
48}
49
50impl<F: Copy> RootVmVerifierPvs<F> {
51    pub fn flatten(self) -> Vec<F> {
52        let mut ret = self.exe_commit.to_vec();
53        ret.extend(self.leaf_verifier_commit);
54        ret.extend(self.public_values);
55        ret
56    }
57    pub fn from_flatten(flatten: Vec<F>) -> Self {
58        let exe_commit = flatten[..DIGEST_SIZE].try_into().unwrap();
59        let leaf_verifier_commit = flatten[DIGEST_SIZE..2 * DIGEST_SIZE].try_into().unwrap();
60        let public_values = flatten[2 * DIGEST_SIZE..].to_vec();
61        Self {
62            exe_commit,
63            leaf_verifier_commit,
64            public_values,
65        }
66    }
67}