Skip to main content

miden_air_definitions/vm/
inputs.rs

1//! VM public inputs, their wire encoding, and statement-input layout.
2
3use alloc::vec::Vec;
4
5use miden_core::{
6    Felt, WORD_SIZE, Word,
7    deferred::DeferredRoot,
8    program::{MIN_STACK_DEPTH, ProgramInfo, StackInputs, StackOutputs},
9    serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
10    utils::ToElements,
11};
12#[cfg(feature = "arbitrary")]
13mod arbitrary;
14
15// PUBLIC INPUTS
16// ================================================================================================
17
18#[derive(Debug, Clone, PartialEq, Eq)]
19#[cfg_attr(
20    all(feature = "arbitrary", test),
21    miden_test_serialization_macros::serialization_test
22)]
23pub struct PublicInputs {
24    program_info: ProgramInfo,
25    stack_inputs: StackInputs,
26    stack_outputs: StackOutputs,
27    deferred_root: DeferredRoot,
28}
29
30impl PublicInputs {
31    /// Creates a new instance of `PublicInputs` from program information, stack inputs and outputs,
32    /// and the final deferred root.
33    pub fn new(
34        program_info: ProgramInfo,
35        stack_inputs: StackInputs,
36        stack_outputs: StackOutputs,
37        deferred_root: DeferredRoot,
38    ) -> Self {
39        Self {
40            program_info,
41            stack_inputs,
42            stack_outputs,
43            deferred_root,
44        }
45    }
46
47    pub fn stack_inputs(&self) -> StackInputs {
48        self.stack_inputs
49    }
50
51    pub fn stack_outputs(&self) -> StackOutputs {
52        self.stack_outputs
53    }
54
55    pub fn program_info(&self) -> ProgramInfo {
56        self.program_info.clone()
57    }
58
59    /// Returns the final deferred root.
60    pub fn deferred_root(&self) -> DeferredRoot {
61        self.deferred_root
62    }
63
64    /// Returns the canonical commitment to the kernel of the verified statement: the value the
65    /// recursive verifier observes into the transcript in place of the raw kernel-procedure
66    /// digest list. See
67    /// [`KernelDescriptor::commitment`](miden_core::program::KernelDescriptor::commitment).
68    pub fn kernel_commitment(&self) -> Word {
69        self.program_info.kernel_commitment()
70    }
71
72    /// Returns the AIR public values (`air_inputs`) and statement inputs (`aux_inputs`).
73    ///
74    /// `air_inputs` (read by Core; all other AIRs have empty slices) layout:
75    ///   [0..16]  stack inputs
76    ///   [16..32] stack outputs
77    ///
78    /// `aux_inputs` (statement inputs not read by the AIRs, consumed only by `observe` and
79    /// `eval_external`) layout:
80    ///   [0..4]   program hash
81    ///   [4..8]   final deferred root
82    ///   [8..]    kernel procedure digests (concatenated words, variable length)
83    pub fn to_air_inputs(&self) -> (Vec<Felt>, Vec<Felt>) {
84        let mut air_inputs = Vec::with_capacity(NUM_PUBLIC_VALUES);
85        air_inputs.extend_from_slice(self.stack_inputs.as_ref());
86        air_inputs.extend_from_slice(self.stack_outputs.as_ref());
87
88        let kernel_felts = Word::words_as_elements(self.program_info.kernel_procedures());
89        let mut aux_inputs = Vec::with_capacity(AUX_KERNEL_DIGESTS + kernel_felts.len());
90        aux_inputs.extend_from_slice(self.program_info.program_hash().as_elements());
91        aux_inputs.extend_from_slice(self.deferred_root.as_ref());
92        aux_inputs.extend_from_slice(kernel_felts);
93
94        (air_inputs, aux_inputs)
95    }
96
97    /// Converts public inputs into a vector of field elements (Felt) in the canonical order:
98    /// - program info elements (including kernel procedure hashes)
99    /// - stack inputs
100    /// - stack outputs
101    /// - final deferred root
102    pub fn to_elements(&self) -> Vec<Felt> {
103        let mut result = self.program_info.to_elements();
104        result.extend_from_slice(self.stack_inputs.as_ref());
105        result.extend_from_slice(self.stack_outputs.as_ref());
106        result.extend_from_slice(self.deferred_root.as_ref());
107        result
108    }
109}
110
111// SERIALIZATION
112// ================================================================================================
113
114impl Serializable for PublicInputs {
115    fn write_into<W: ByteWriter>(&self, target: &mut W) {
116        self.program_info.write_into(target);
117        self.stack_inputs.write_into(target);
118        self.stack_outputs.write_into(target);
119        self.deferred_root.write_into(target);
120    }
121}
122
123impl Deserializable for PublicInputs {
124    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
125        let program_info = ProgramInfo::read_from(source)?;
126        let stack_inputs = StackInputs::read_from(source)?;
127        let stack_outputs = StackOutputs::read_from(source)?;
128        let deferred_root = DeferredRoot::read_from(source)?;
129
130        Ok(PublicInputs {
131            program_info,
132            stack_inputs,
133            stack_outputs,
134            deferred_root,
135        })
136    }
137}
138
139// PROCESSOR AIR
140// ================================================================================================
141
142/// Number of public values read by Core; the other Miden VM AIRs have no public values.
143///
144/// Layout (32 Felts total):
145///   [0..16]  stack inputs
146///   [16..32] stack outputs
147///
148/// The program hash and final deferred root are not read by any AIR constraint, so they
149/// are carried as `aux_inputs` and consumed only by [`crate::vm::statement::observe`] and
150/// [`crate::vm::statement::eval_external`].
151pub const NUM_PUBLIC_VALUES: usize = MIN_STACK_DEPTH + MIN_STACK_DEPTH;
152
153// `aux_inputs` layout offsets — statement inputs that the AIRs do not read. The fixed program
154// hash and final deferred root occupy the first two words; the variable-length kernel-procedure
155// digests follow.
156pub(super) const AUX_PROGRAM_HASH: usize = 0;
157pub(super) const AUX_DEFERRED_ROOT: usize = WORD_SIZE;
158pub(super) const AUX_KERNEL_DIGESTS: usize = 2 * WORD_SIZE;