Skip to main content

miden_core/mast/
mod.rs

1//! MAST forest: a collection of procedures represented as Merkle trees.
2//!
3//! # Deserializing from untrusted sources
4//!
5//! When loading a `MastForest` from bytes you don't fully trust (network, user upload, etc.),
6//! use [`UntrustedMastForest`] instead of calling `MastForest::read_from_bytes` directly:
7//!
8//! ```ignore
9//! use miden_core::mast::UntrustedMastForest;
10//!
11//! let forest = UntrustedMastForest::read_from_bytes(&bytes)?
12//!     .validate()?;
13//! ```
14//!
15//! [`UntrustedMastForest::read_from_bytes`] applies default parsing and validation budgets derived
16//! from the input size. Use [`UntrustedMastForest::read_from_bytes_with_budget`] to tune only the
17//! wire-parsing budget, or [`UntrustedMastForest::read_from_bytes_with_budgets`] to tune both:
18//! the parsing budget limits allocations driven directly by wire counts while reading the payload,
19//! and the validation budget limits later helper allocations needed to materialize and check
20//! stripped or hashless payloads.
21//!
22//! ```ignore
23//! use miden_core::mast::UntrustedMastForest;
24//!
25//! // Parsing budget only
26//! let forest = UntrustedMastForest::read_from_bytes_with_budget(&bytes, bytes.len())?
27//!     .validate()?;
28//!
29//! // Parsing budget plus explicit validation-allocation budget
30//! let forest = UntrustedMastForest::read_from_bytes_with_budgets(&bytes, bytes.len(), bytes.len() * 7)?
31//!     .validate()?;
32//! ```
33//!
34//! This recomputes all node hashes and checks structural invariants before returning a usable
35//! `MastForest`. Direct deserialization via `MastForest::read_from_bytes` trusts the serialized
36//! hashes and should only be used for data from trusted sources (e.g. compiled locally).
37//!
38//! In practice, the public entry points split into three policies:
39//! - [`MastForest::read_from_bytes`]: trusted full deserialization; rejects hashless payloads and
40//!   trusts serialized non-external digests.
41//! - [`SerializedMastForest::new`]: structural inspection path for local tooling; scans only the
42//!   layout needed for random access and may accept full, stripped, or hashless payloads, but it is
43//!   not an untrusted-validation entry point.
44//! - [`UntrustedMastForest::read_from_bytes`] and
45//!   [`UntrustedMastForest::read_from_bytes_with_budgets`]: untrusted paths; parse with bounded
46//!   readers and require [`UntrustedMastForest::validate`] before use.
47
48use alloc::{
49    collections::{BTreeMap, BTreeSet},
50    string::String,
51    sync::Arc,
52    vec::Vec,
53};
54use core::{
55    fmt,
56    ops::{Index, IndexMut},
57};
58
59use miden_utils_sync::OnceLockCompat;
60#[cfg(any(test, feature = "arbitrary"))]
61use proptest::prelude::*;
62#[cfg(feature = "serde")]
63use serde::{Deserialize, Serialize};
64
65mod node;
66#[cfg(any(test, feature = "arbitrary"))]
67pub use node::arbitrary;
68pub(crate) use node::collect_immediate_placements;
69pub use node::{
70    BasicBlockNode, BasicBlockNodeBuilder, CallNode, CallNodeBuilder, DecoratedOpLink,
71    DecoratorOpLinkIterator, DecoratorStore, DynNode, DynNodeBuilder, ExternalNode,
72    ExternalNodeBuilder, JoinNode, JoinNodeBuilder, LoopNode, LoopNodeBuilder,
73    MastForestContributor, MastNode, MastNodeBuilder, MastNodeExt, OP_BATCH_SIZE, OP_GROUP_SIZE,
74    OpBatch, OperationOrDecorator, SplitNode, SplitNodeBuilder,
75};
76
77#[cfg(feature = "serde")]
78use crate::serde::SliceReader;
79use crate::{
80    Felt, Word,
81    advice::AdviceMap,
82    operations::{AssemblyOp, DebugVarInfo, Decorator},
83    serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
84    utils::{Idx, IndexVec, hash_string_to_word},
85};
86
87mod debuginfo;
88pub use debuginfo::{
89    AsmOpIndexError, DebugInfo, DebugVarId, DecoratedLinks, DecoratedLinksIter,
90    DecoratorIndexError, NodeToDecoratorIds, OpToAsmOpId, OpToDebugVarIds, OpToDecoratorIds,
91};
92
93mod serialization;
94pub use serialization::{MastForestView, MastNodeEntry, MastNodeInfo, SerializedMastForest};
95
96mod untrusted;
97pub use untrusted::UntrustedMastForest;
98
99mod merger;
100pub(crate) use merger::MastForestMerger;
101pub use merger::MastForestRootMap;
102
103mod multi_forest_node_iterator;
104pub(crate) use multi_forest_node_iterator::*;
105
106mod node_fingerprint;
107pub use node_fingerprint::{DecoratorFingerprint, MastNodeFingerprint};
108
109mod node_builder_utils;
110pub use node_builder_utils::build_node_with_remapped_ids;
111
112#[cfg(test)]
113mod tests;
114
115// MAST FOREST
116// ================================================================================================
117
118/// Represents one or more procedures, represented as a collection of [`MastNode`]s.
119///
120/// A [`MastForest`] does not have an entrypoint, and hence is not executable. A
121/// [`crate::program::Program`] can be built from a [`MastForest`] to specify an entrypoint.
122#[derive(Clone, Debug, Default)]
123#[cfg_attr(
124    all(feature = "arbitrary", test),
125    miden_test_serde_macros::serde_test(binary_serde(true))
126)]
127pub struct MastForest {
128    /// All of the nodes local to the trees comprising the MAST forest.
129    nodes: IndexVec<MastNodeId, MastNode>,
130
131    /// Roots of procedures defined within this MAST forest.
132    roots: Vec<MastNodeId>,
133
134    /// Advice map to be loaded into the VM prior to executing procedures from this MAST forest.
135    advice_map: AdviceMap,
136
137    /// Debug information including decorators and error codes.
138    /// Always present (as per issue #1821), but can be empty for stripped builds.
139    debug_info: DebugInfo,
140
141    /// Cached commitment to this MAST forest (commitment to all roots).
142    /// This is computed lazily on first access and invalidated on any mutation.
143    commitment_cache: OnceLockCompat<Word>,
144}
145
146// ------------------------------------------------------------------------------------------------
147/// Constructors
148impl MastForest {
149    /// Creates a new empty [`MastForest`].
150    pub fn new() -> Self {
151        Self {
152            nodes: IndexVec::new(),
153            roots: Vec::new(),
154            advice_map: AdviceMap::default(),
155            debug_info: DebugInfo::new(),
156            commitment_cache: OnceLockCompat::new(),
157        }
158    }
159}
160
161// ------------------------------------------------------------------------------------------------
162/// Equality implementations
163impl PartialEq for MastForest {
164    fn eq(&self, other: &Self) -> bool {
165        // Compare all fields except commitment_cache, which is derived data
166        self.nodes == other.nodes
167            && self.roots == other.roots
168            && self.advice_map == other.advice_map
169            && self.debug_info == other.debug_info
170    }
171}
172
173impl Eq for MastForest {}
174
175// ------------------------------------------------------------------------------------------------
176/// State mutators
177impl MastForest {
178    /// The maximum number of nodes that can be stored in a single MAST forest.
179    const MAX_NODES: usize = (1 << 30) - 1;
180
181    /// Marks the given [`MastNodeId`] as being the root of a procedure.
182    ///
183    /// If the specified node is already marked as a root, this will have no effect.
184    ///
185    /// # Panics
186    /// - if `new_root_id`'s internal index is larger than the number of nodes in this forest (i.e.
187    ///   clearly doesn't belong to this MAST forest).
188    pub fn make_root(&mut self, new_root_id: MastNodeId) {
189        assert!(new_root_id.to_usize() < self.nodes.len());
190
191        if !self.roots.contains(&new_root_id) {
192            self.roots.push(new_root_id);
193            // Invalidate the cached commitment since we modified the roots
194            self.commitment_cache.take();
195        }
196    }
197
198    /// Removes all nodes in the provided set from the MAST forest. The nodes MUST be orphaned (i.e.
199    /// have no parent). Otherwise, this parent's reference is considered "dangling" after the
200    /// removal (i.e. will point to an incorrect node after the removal), and this removal operation
201    /// would result in an invalid [`MastForest`].
202    ///
203    /// It also returns the map from old node IDs to new node IDs. Any [`MastNodeId`] used in
204    /// reference to the old [`MastForest`] should be remapped using this map.
205    pub fn remove_nodes(
206        &mut self,
207        nodes_to_remove: &BTreeSet<MastNodeId>,
208    ) -> BTreeMap<MastNodeId, MastNodeId> {
209        if nodes_to_remove.is_empty() {
210            return BTreeMap::new();
211        }
212
213        let old_nodes = core::mem::replace(&mut self.nodes, IndexVec::new());
214        let old_root_ids = core::mem::take(&mut self.roots);
215        let (retained_nodes, id_remappings) = remove_nodes(old_nodes.into_inner(), nodes_to_remove);
216
217        self.remap_and_add_nodes(retained_nodes, &id_remappings);
218        self.remap_and_add_roots(old_root_ids, &id_remappings);
219
220        // Remap the asm_op_storage to use the new node IDs
221        self.debug_info.remap_asm_op_storage(&id_remappings);
222
223        // Invalidate the cached commitment since we modified the forest structure
224        self.commitment_cache.take();
225
226        id_remappings
227    }
228
229    /// Clears all [`DebugInfo`] from this forest: decorators, error codes, and procedure names.
230    ///
231    /// ```
232    /// # use miden_core::mast::MastForest;
233    /// let mut forest = MastForest::new();
234    /// forest.clear_debug_info();
235    /// assert!(forest.decorators().is_empty());
236    /// ```
237    pub fn clear_debug_info(&mut self) {
238        self.debug_info = DebugInfo::empty_for_nodes(self.nodes.len());
239    }
240
241    /// Compacts the forest by merging duplicate nodes.
242    ///
243    /// This operation performs node deduplication by merging the forest with itself.
244    /// The method assumes that debug info has already been cleared if that is desired.
245    /// This method consumes the forest and returns a new compacted forest.
246    ///
247    /// The process works by:
248    /// 1. Merging the forest with itself to deduplicate identical nodes
249    /// 2. Updating internal node references and remappings
250    /// 3. Returning the compacted forest and root map
251    ///
252    /// # Examples
253    ///
254    /// ```rust
255    /// use miden_core::mast::MastForest;
256    ///
257    /// let mut forest = MastForest::new();
258    /// // Add nodes to the forest
259    ///
260    /// // First clear debug info if needed
261    /// forest.clear_debug_info();
262    ///
263    /// // Then compact the forest (consumes the original)
264    /// let (compacted_forest, root_map) = forest.compact();
265    ///
266    /// // compacted_forest is now compacted with duplicate nodes merged
267    /// ```
268    pub fn compact(self) -> (MastForest, MastForestRootMap) {
269        // Merge with itself to deduplicate nodes
270        // Note: This cannot fail for a self-merge under normal conditions.
271        // The only possible failures (TooManyNodes, TooManyDecorators) would require the
272        // original forest to be at capacity limits, at which point compaction wouldn't help.
273        MastForest::merge([&self])
274            .expect("Failed to compact MastForest: this should never happen during self-merge")
275    }
276
277    /// Merges all `forests` into a new [`MastForest`].
278    ///
279    /// Merging two forests means combining all their constituent parts, i.e. [`MastNode`]s,
280    /// [`Decorator`]s and roots. During this process, any duplicate or
281    /// unreachable nodes are removed. Additionally, [`MastNodeId`]s of nodes as well as
282    /// [`DecoratorId`]s of decorators may change and references to them are remapped to their new
283    /// location.
284    ///
285    /// For example, consider this representation of a forest's nodes with all of these nodes being
286    /// roots:
287    ///
288    /// ```text
289    /// [Block(foo), Block(bar)]
290    /// ```
291    ///
292    /// If we merge another forest into it:
293    ///
294    /// ```text
295    /// [Block(bar), Call(0)]
296    /// ```
297    ///
298    /// then we would expect this forest:
299    ///
300    /// ```text
301    /// [Block(foo), Block(bar), Call(1)]
302    /// ```
303    ///
304    /// - The `Call` to the `bar` block was remapped to its new index (now 1, previously 0).
305    /// - The `Block(bar)` was deduplicated any only exists once in the merged forest.
306    ///
307    /// The function also returns a vector of [`MastForestRootMap`]s, whose length equals the number
308    /// of passed `forests`. The indices in the vector correspond to the ones in `forests`. The map
309    /// of a given forest contains the new locations of its roots in the merged forest. To
310    /// illustrate, the above example would return a vector of two maps:
311    ///
312    /// ```text
313    /// vec![{0 -> 0, 1 -> 1}
314    ///      {0 -> 1, 1 -> 2}]
315    /// ```
316    ///
317    /// - The root locations of the original forest are unchanged.
318    /// - For the second forest, the `bar` block has moved from index 0 to index 1 in the merged
319    ///   forest, and the `Call` has moved from index 1 to 2.
320    ///
321    /// If any forest being merged contains an `External(qux)` node and another forest contains a
322    /// node whose digest is `qux`, then the external node will be replaced with the `qux` node,
323    /// which is effectively deduplication. Decorators are ignored when it comes to merging
324    /// External nodes. This means that an External node with decorators may be replaced by a node
325    /// without decorators or vice versa.
326    pub fn merge<'forest>(
327        forests: impl IntoIterator<Item = &'forest MastForest>,
328    ) -> Result<(MastForest, MastForestRootMap), MastForestError> {
329        MastForestMerger::merge(forests)
330    }
331}
332
333// ------------------------------------------------------------------------------------------------
334/// Helpers
335impl MastForest {
336    /// Adds all provided nodes to the internal set of nodes, remapping all [`MastNodeId`]
337    /// references in those nodes.
338    ///
339    /// # Panics
340    /// - Panics if the internal set of nodes is not empty.
341    fn remap_and_add_nodes(
342        &mut self,
343        nodes_to_add: Vec<MastNode>,
344        id_remappings: &BTreeMap<MastNodeId, MastNodeId>,
345    ) {
346        assert!(self.nodes.is_empty());
347        // extract decorator information from the nodes by converting them into builders
348        let node_builders =
349            nodes_to_add.into_iter().map(|node| node.to_builder(self)).collect::<Vec<_>>();
350
351        // Clear decorator storage after extracting builders (builders contain decorator data)
352        self.debug_info.clear_mappings();
353
354        // Add each node to the new MAST forest, making sure to rewrite any outdated internal
355        // `MastNodeId`s
356        for live_node_builder in node_builders {
357            live_node_builder.remap_children(id_remappings).add_to_forest(self).unwrap();
358        }
359    }
360
361    /// Remaps and adds all old root ids to the internal set of roots.
362    ///
363    /// # Panics
364    /// - Panics if the internal set of roots is not empty.
365    fn remap_and_add_roots(
366        &mut self,
367        old_root_ids: Vec<MastNodeId>,
368        id_remappings: &BTreeMap<MastNodeId, MastNodeId>,
369    ) {
370        assert!(self.roots.is_empty());
371
372        for old_root_id in old_root_ids {
373            let new_root_id = id_remappings.get(&old_root_id).copied().unwrap_or(old_root_id);
374            self.make_root(new_root_id);
375        }
376    }
377}
378
379/// Returns the set of nodes that are live, as well as the mapping from "old ID" to "new ID" for all
380/// live nodes.
381fn remove_nodes(
382    mast_nodes: Vec<MastNode>,
383    nodes_to_remove: &BTreeSet<MastNodeId>,
384) -> (Vec<MastNode>, BTreeMap<MastNodeId, MastNodeId>) {
385    // Note: this allows us to safely use `usize as u32`, guaranteeing that it won't wrap around.
386    assert!(mast_nodes.len() < u32::MAX as usize);
387
388    let mut retained_nodes = Vec::with_capacity(mast_nodes.len());
389    let mut id_remappings = BTreeMap::new();
390
391    for (old_node_index, old_node) in mast_nodes.into_iter().enumerate() {
392        let old_node_id: MastNodeId = MastNodeId(old_node_index as u32);
393
394        if !nodes_to_remove.contains(&old_node_id) {
395            let new_node_id: MastNodeId = MastNodeId(retained_nodes.len() as u32);
396            id_remappings.insert(old_node_id, new_node_id);
397
398            retained_nodes.push(old_node);
399        }
400    }
401
402    (retained_nodes, id_remappings)
403}
404
405// ------------------------------------------------------------------------------------------------
406/// Public accessors
407impl MastForest {
408    /// Returns the [`MastNode`] associated with the provided [`MastNodeId`] if valid, or else
409    /// `None`.
410    ///
411    /// This is the fallible version of indexing (e.g. `mast_forest[node_id]`).
412    #[inline(always)]
413    pub fn get_node_by_id(&self, node_id: MastNodeId) -> Option<&MastNode> {
414        self.nodes.get(node_id)
415    }
416
417    /// Returns the [`MastNodeId`] of the procedure associated with a given digest, if any.
418    #[inline(always)]
419    pub fn find_procedure_root(&self, digest: Word) -> Option<MastNodeId> {
420        self.roots.iter().find(|&&root_id| self[root_id].digest() == digest).copied()
421    }
422
423    /// Returns true if a node with the specified ID is a root of a procedure in this MAST forest.
424    pub fn is_procedure_root(&self, node_id: MastNodeId) -> bool {
425        self.roots.contains(&node_id)
426    }
427
428    /// Returns an iterator over the digests of all procedures in this MAST forest.
429    pub fn procedure_digests(&self) -> impl Iterator<Item = Word> + '_ {
430        self.roots.iter().map(|&root_id| self[root_id].digest())
431    }
432
433    /// Returns an iterator over the digests of local procedures in this MAST forest.
434    ///
435    /// A local procedure is defined as a procedure which is not a single external node.
436    pub fn local_procedure_digests(&self) -> impl Iterator<Item = Word> + '_ {
437        self.roots.iter().filter_map(|&root_id| {
438            let node = &self[root_id];
439            if node.is_external() { None } else { Some(node.digest()) }
440        })
441    }
442
443    /// Returns an iterator over the IDs of the procedures in this MAST forest.
444    pub fn procedure_roots(&self) -> &[MastNodeId] {
445        &self.roots
446    }
447
448    /// Returns the number of procedures in this MAST forest.
449    pub fn num_procedures(&self) -> u32 {
450        self.roots
451            .len()
452            .try_into()
453            .expect("MAST forest contains more than 2^32 procedures.")
454    }
455
456    /// Returns the [Word] representing the content hash of a subset of [`MastNodeId`]s.
457    ///
458    /// # Panics
459    /// This function panics if any `node_ids` is not a node of this forest.
460    pub fn compute_nodes_commitment<'a>(
461        &self,
462        node_ids: impl IntoIterator<Item = &'a MastNodeId>,
463    ) -> Word {
464        let mut digests: Vec<Word> = node_ids.into_iter().map(|&id| self[id].digest()).collect();
465        digests.sort_unstable();
466        miden_crypto::hash::poseidon2::Poseidon2::merge_many(&digests)
467    }
468
469    /// Returns the commitment to this MAST forest.
470    ///
471    /// The commitment is computed as the sequential hash of all procedure roots in the forest.
472    /// This value is cached after the first computation and reused for subsequent calls,
473    /// unless the forest is mutated (in which case the cache is invalidated).
474    ///
475    /// The commitment uniquely identifies the forest's structure, as each root's digest
476    /// transitively includes all of its descendants. Therefore, a commitment to all roots
477    /// is a commitment to the entire forest.
478    pub fn commitment(&self) -> Word {
479        *self.commitment_cache.get_or_init(|| self.compute_nodes_commitment(&self.roots))
480    }
481
482    /// Returns the number of nodes in this MAST forest.
483    pub fn num_nodes(&self) -> u32 {
484        self.nodes.len() as u32
485    }
486
487    /// Returns the underlying nodes in this MAST forest.
488    pub fn nodes(&self) -> &[MastNode] {
489        self.nodes.as_slice()
490    }
491
492    pub fn advice_map(&self) -> &AdviceMap {
493        &self.advice_map
494    }
495
496    pub fn advice_map_mut(&mut self) -> &mut AdviceMap {
497        &mut self.advice_map
498    }
499
500    // SERIALIZATION
501    // --------------------------------------------------------------------------------------------
502
503    /// Serializes this MastForest without debug information.
504    ///
505    /// This produces a smaller output by omitting decorators, error codes, and procedure names.
506    /// The resulting bytes can be deserialized with the standard [`Deserializable`] impl,
507    /// which auto-detects the format and creates an empty [`DebugInfo`].
508    ///
509    /// Use this for production builds where debug info is not needed.
510    ///
511    /// # Example
512    ///
513    /// ```
514    /// use miden_core::{mast::MastForest, serde::Serializable};
515    ///
516    /// let forest = MastForest::new();
517    ///
518    /// // Full serialization (with debug info)
519    /// let full_bytes = forest.to_bytes();
520    ///
521    /// // Stripped serialization (without debug info)
522    /// let mut stripped_bytes = Vec::new();
523    /// forest.write_stripped(&mut stripped_bytes);
524    ///
525    /// // Both can be deserialized the same way
526    /// // let restored = MastForest::read_from_bytes(&stripped_bytes).unwrap();
527    /// ```
528    pub fn write_stripped<W: ByteWriter>(&self, target: &mut W) {
529        serialization::write_stripped_into(self, target);
530    }
531
532    /// Serializes this MastForest with the HASHLESS flag set.
533    ///
534    /// Hashless implies stripped: debug info is omitted, and digests must be recomputed during
535    /// validation. Trusted deserialization rejects this flag.
536    ///
537    /// Use this when producing data for untrusted validation.
538    pub fn write_hashless<W: ByteWriter>(&self, target: &mut W) {
539        serialization::write_hashless_into(self, target);
540    }
541
542    /// Returns the exact size of stripped serialization in bytes.
543    pub fn stripped_size_hint(&self) -> usize {
544        serialization::stripped_size_hint(self)
545    }
546}
547
548// ------------------------------------------------------------------------------------------------
549/// Decorator methods
550impl MastForest {
551    /// Returns a list of all decorators contained in this [MastForest].
552    pub fn decorators(&self) -> &[Decorator] {
553        self.debug_info.decorators()
554    }
555
556    /// Returns the [`Decorator`] associated with the provided [`DecoratorId`] if valid, or else
557    /// `None`.
558    ///
559    /// This is the fallible version of indexing (e.g. `mast_forest[decorator_id]`).
560    #[inline]
561    pub fn decorator_by_id(&self, decorator_id: DecoratorId) -> Option<&Decorator> {
562        self.debug_info.decorator(decorator_id)
563    }
564
565    /// Returns decorator indices for a specific operation within a node.
566    ///
567    /// This is the primary accessor for reading decorators from the centralized storage.
568    /// Returns a slice of decorator IDs for the given operation.
569    #[inline]
570    pub(crate) fn decorator_indices_for_op(
571        &self,
572        node_id: MastNodeId,
573        local_op_idx: usize,
574    ) -> &[DecoratorId] {
575        self.debug_info.decorators_for_operation(node_id, local_op_idx)
576    }
577
578    /// Returns an iterator over decorator references for a specific operation within a node.
579    ///
580    /// This is the preferred method for accessing decorators, as it provides direct
581    /// references to the decorator objects.
582    #[inline]
583    pub fn decorators_for_op<'a>(
584        &'a self,
585        node_id: MastNodeId,
586        local_op_idx: usize,
587    ) -> impl Iterator<Item = &'a Decorator> + 'a {
588        self.decorator_indices_for_op(node_id, local_op_idx)
589            .iter()
590            .map(move |&decorator_id| &self[decorator_id])
591    }
592
593    /// Returns the decorators to be executed before this node is executed.
594    #[inline]
595    pub fn before_enter_decorators(&self, node_id: MastNodeId) -> &[DecoratorId] {
596        self.debug_info.before_enter_decorators(node_id)
597    }
598
599    /// Returns the decorators to be executed after this node is executed.
600    #[inline]
601    pub fn after_exit_decorators(&self, node_id: MastNodeId) -> &[DecoratorId] {
602        self.debug_info.after_exit_decorators(node_id)
603    }
604
605    /// Returns decorator links for a node, including operation indices.
606    ///
607    /// This provides a flattened view of all decorators for a node with their operation indices.
608    #[inline]
609    pub(crate) fn decorator_links_for_node<'a>(
610        &'a self,
611        node_id: MastNodeId,
612    ) -> Result<DecoratedLinks<'a>, DecoratorIndexError> {
613        self.debug_info.decorator_links_for_node(node_id)
614    }
615
616    /// Adds a decorator to the forest, and returns the associated [`DecoratorId`].
617    pub fn add_decorator(&mut self, decorator: Decorator) -> Result<DecoratorId, MastForestError> {
618        self.debug_info.add_decorator(decorator)
619    }
620
621    /// Adds a debug variable to the forest, and returns the associated [`DebugVarId`].
622    pub fn add_debug_var(
623        &mut self,
624        debug_var: DebugVarInfo,
625    ) -> Result<DebugVarId, MastForestError> {
626        self.debug_info.add_debug_var(debug_var)
627    }
628
629    /// Returns debug variable IDs for a specific operation within a node.
630    pub fn debug_vars_for_operation(
631        &self,
632        node_id: MastNodeId,
633        local_op_idx: usize,
634    ) -> &[DebugVarId] {
635        self.debug_info.debug_vars_for_operation(node_id, local_op_idx)
636    }
637
638    /// Returns the debug variable with the given ID, if it exists.
639    pub fn debug_var(&self, debug_var_id: DebugVarId) -> Option<&DebugVarInfo> {
640        self.debug_info.debug_var(debug_var_id)
641    }
642
643    /// Adds decorator IDs for a node to the storage.
644    ///
645    /// Used when building nodes for efficient decorator access during execution.
646    ///
647    /// # Note
648    /// This method does not validate decorator IDs immediately. Validation occurs during
649    /// operations that need to access the actual decorator data (e.g., merging, serialization).
650    #[inline]
651    pub(crate) fn register_node_decorators(
652        &mut self,
653        node_id: MastNodeId,
654        before_enter: &[DecoratorId],
655        after_exit: &[DecoratorId],
656    ) {
657        if before_enter.is_empty() && after_exit.is_empty() {
658            return;
659        }
660
661        self.debug_info.register_node_decorators(node_id, before_enter, after_exit);
662    }
663
664    /// Returns the [`AssemblyOp`] associated with a node.
665    ///
666    /// For basic block nodes with a `target_op_idx`, returns the AssemblyOp for that operation.
667    /// For other nodes or when no `target_op_idx` is provided, returns the first AssemblyOp.
668    pub fn get_assembly_op(
669        &self,
670        node_id: MastNodeId,
671        target_op_idx: Option<usize>,
672    ) -> Option<&AssemblyOp> {
673        match target_op_idx {
674            Some(op_idx) => self.debug_info.asm_op_for_operation(node_id, op_idx),
675            None => self.debug_info.first_asm_op_for_node(node_id),
676        }
677    }
678}
679
680// ------------------------------------------------------------------------------------------------
681/// Validation methods
682impl MastForest {
683    fn validate_basic_block_invariants(&self) -> Result<(), MastForestError> {
684        for (node_id_idx, node) in self.nodes.iter().enumerate() {
685            let node_id =
686                MastNodeId::new_unchecked(node_id_idx.try_into().expect("too many nodes"));
687            if let MastNode::Block(basic_block) = node {
688                basic_block.validate_batch_invariants().map_err(|error_msg| {
689                    MastForestError::InvalidBatchPadding(node_id, error_msg)
690                })?;
691
692                let num_operations = basic_block.num_operations() as usize;
693                let decorator_links = match self.decorator_links_for_node(node_id) {
694                    Ok(decorator_links) => decorator_links,
695                    Err(DecoratorIndexError::NodeIndex(_)) => continue,
696                    Err(error) => return Err(MastForestError::DecoratorError(error)),
697                };
698                for (operation_idx, _) in decorator_links {
699                    if operation_idx >= num_operations {
700                        return Err(MastForestError::DecoratorOpIndexOutOfBounds {
701                            operation_idx,
702                            num_operations,
703                        });
704                    }
705                }
706            }
707        }
708
709        Ok(())
710    }
711
712    fn validate_procedure_name_digests(&self) -> Result<(), MastForestError> {
713        for (digest, _) in self.debug_info.procedure_names() {
714            if self.find_procedure_root(digest).is_none() {
715                return Err(MastForestError::InvalidProcedureNameDigest(digest));
716            }
717        }
718
719        Ok(())
720    }
721
722    /// Validates that all BasicBlockNodes in this forest satisfy the core invariants:
723    /// 1. Power-of-two number of groups in each batch
724    /// 2. No operation group ends with an operation requiring an immediate value
725    /// 3. The last operation group in a batch cannot contain operations requiring immediate values
726    /// 4. OpBatch structural consistency (num_groups <= BATCH_SIZE, group size <= GROUP_SIZE,
727    ///    indptr integrity, bounds checking)
728    ///
729    /// This also validates that each stored procedure-name digest resolves to a procedure root in
730    /// the forest.
731    ///
732    /// This addresses the gap created by PR 2094, where padding NOOPs are now inserted
733    /// at assembly time rather than dynamically during execution, and adds comprehensive
734    /// structural validation to prevent deserialization-time panics.
735    pub fn validate(&self) -> Result<(), MastForestError> {
736        self.validate_basic_block_invariants()?;
737        self.validate_procedure_name_digests()
738    }
739
740    /// Validates that stored node digests match the hashes implied by local structure.
741    ///
742    /// For `External` nodes the digest is accepted as-is because it is externally provided and
743    /// cannot be reconstructed from local structure alone.
744    fn validate_node_hashes(&self) -> Result<(), MastForestError> {
745        let computed_hashes = self.compute_node_hashes()?;
746        for (node_idx, (node, computed_digest)) in
747            self.nodes.iter().zip(computed_hashes).enumerate()
748        {
749            let expected_digest = node.digest();
750            if expected_digest != computed_digest {
751                return Err(MastForestError::HashMismatch {
752                    node_id: MastNodeId::new_unchecked(node_idx as u32),
753                    expected: expected_digest,
754                    computed: computed_digest,
755                });
756            }
757        }
758
759        Ok(())
760    }
761
762    /// Computes node hashes in topological order.
763    ///
764    /// The returned vector is aligned with node indices, so `digests[node_id as usize]` is the
765    /// digest of that node.
766    ///
767    /// For `External` nodes, the existing digest is returned unchanged.
768    ///
769    /// Returns [`MastForestError::ForwardReference`] if nodes are not in topological order.
770    fn compute_node_hashes(&self) -> Result<Vec<Word>, MastForestError> {
771        use crate::chiplets::hasher;
772
773        /// Checks that child_id references a node that appears before node_id in topological order.
774        fn check_no_forward_ref(
775            node_id: MastNodeId,
776            child_id: MastNodeId,
777        ) -> Result<(), MastForestError> {
778            if child_id.0 >= node_id.0 {
779                return Err(MastForestError::ForwardReference(node_id, child_id));
780            }
781            Ok(())
782        }
783
784        let mut computed_hashes = Vec::with_capacity(self.nodes.len());
785        for (node_idx, node) in self.nodes.iter().enumerate() {
786            let node_id = MastNodeId::new_unchecked(node_idx as u32);
787
788            // Check topological ordering and compute digest.
789            let computed_digest = match node {
790                MastNode::Block(block) => {
791                    let op_groups: Vec<Felt> =
792                        block.op_batches().iter().flat_map(|batch| *batch.groups()).collect();
793                    hasher::hash_elements(&op_groups)
794                },
795                MastNode::Join(join) => {
796                    let left_id = join.first();
797                    let right_id = join.second();
798                    check_no_forward_ref(node_id, left_id)?;
799                    check_no_forward_ref(node_id, right_id)?;
800
801                    let left_digest = computed_hashes[left_id.0 as usize];
802                    let right_digest = computed_hashes[right_id.0 as usize];
803                    hasher::merge_in_domain(&[left_digest, right_digest], JoinNode::DOMAIN)
804                },
805                MastNode::Split(split) => {
806                    let true_id = split.on_true();
807                    let false_id = split.on_false();
808                    check_no_forward_ref(node_id, true_id)?;
809                    check_no_forward_ref(node_id, false_id)?;
810
811                    let true_digest = computed_hashes[true_id.0 as usize];
812                    let false_digest = computed_hashes[false_id.0 as usize];
813                    hasher::merge_in_domain(&[true_digest, false_digest], SplitNode::DOMAIN)
814                },
815                MastNode::Loop(loop_node) => {
816                    let body_id = loop_node.body();
817                    check_no_forward_ref(node_id, body_id)?;
818
819                    let body_digest = computed_hashes[body_id.0 as usize];
820                    hasher::merge_in_domain(&[body_digest, Word::default()], LoopNode::DOMAIN)
821                },
822                MastNode::Call(call) => {
823                    let callee_id = call.callee();
824                    check_no_forward_ref(node_id, callee_id)?;
825
826                    let callee_digest = computed_hashes[callee_id.0 as usize];
827                    let domain = if call.is_syscall() {
828                        CallNode::SYSCALL_DOMAIN
829                    } else {
830                        CallNode::CALL_DOMAIN
831                    };
832                    hasher::merge_in_domain(&[callee_digest, Word::default()], domain)
833                },
834                MastNode::Dyn(dyn_node) => {
835                    if dyn_node.is_dyncall() {
836                        DynNode::DYNCALL_DEFAULT_DIGEST
837                    } else {
838                        DynNode::DYN_DEFAULT_DIGEST
839                    }
840                },
841                MastNode::External(_) => {
842                    // External nodes have externally-provided digests that cannot be recomputed.
843                    node.digest()
844                },
845            };
846
847            computed_hashes.push(computed_digest);
848        }
849
850        Ok(computed_hashes)
851    }
852}
853
854// ------------------------------------------------------------------------------------------------
855/// Error message methods
856impl MastForest {
857    /// Given an error code as a Felt, resolves it to its corresponding error message.
858    pub fn resolve_error_message(&self, code: Felt) -> Option<Arc<str>> {
859        let key = code.as_canonical_u64();
860        self.debug_info.error_message(key)
861    }
862
863    /// Registers an error message in the MAST Forest and returns the corresponding error code as a
864    /// Felt.
865    pub fn register_error(&mut self, msg: Arc<str>) -> Felt {
866        let code: Felt = error_code_from_msg(&msg);
867        // we use u64 as keys for the map
868        self.debug_info.insert_error_code(code.as_canonical_u64(), msg);
869        code
870    }
871}
872
873// ------------------------------------------------------------------------------------------------
874/// Procedure name methods
875impl MastForest {
876    /// Returns the procedure name for the given MAST root digest, if present.
877    pub fn procedure_name(&self, digest: &Word) -> Option<&str> {
878        self.debug_info.procedure_name(digest)
879    }
880
881    /// Returns an iterator over all (digest, name) pairs of procedure names.
882    pub fn procedure_names(&self) -> impl Iterator<Item = (Word, &Arc<str>)> {
883        self.debug_info.procedure_names()
884    }
885
886    /// Inserts a procedure name for the given MAST root digest.
887    pub fn insert_procedure_name(&mut self, digest: Word, name: Arc<str>) {
888        assert!(
889            self.find_procedure_root(digest).is_some(),
890            "attempted to insert procedure name for digest that is not a procedure root"
891        );
892        self.debug_info.insert_procedure_name(digest, name);
893    }
894
895    /// Returns a reference to the debug info for this forest.
896    pub fn debug_info(&self) -> &DebugInfo {
897        &self.debug_info
898    }
899
900    /// Returns a mutable reference to the debug info.
901    ///
902    /// This is intended for use by the assembler to register AssemblyOps and other debug
903    /// information during compilation.
904    pub fn debug_info_mut(&mut self) -> &mut DebugInfo {
905        &mut self.debug_info
906    }
907}
908
909// TEST HELPERS
910// ================================================================================================
911
912#[cfg(test)]
913impl MastForest {
914    /// Returns all decorators for a given node as a vector of (position, DecoratorId) tuples.
915    ///
916    /// This helper method combines before_enter, operation-indexed, and after_exit decorators
917    /// into a single collection, which is useful for testing decorator positions and ordering.
918    ///
919    /// **Performance Warning**: This method performs multiple allocations through collect() calls
920    /// and should not be relied upon for performance-critical code. It is intended for testing
921    /// only.
922    pub fn all_decorators(&self, node_id: MastNodeId) -> Vec<(usize, DecoratorId)> {
923        let node = &self[node_id];
924
925        // For non-basic blocks, just get before_enter and after_exit decorators at position 0
926        if !node.is_basic_block() {
927            let before_enter_decorators: Vec<_> = self
928                .before_enter_decorators(node_id)
929                .iter()
930                .map(|&deco_id| (0, deco_id))
931                .collect();
932
933            let after_exit_decorators: Vec<_> = self
934                .after_exit_decorators(node_id)
935                .iter()
936                .map(|&deco_id| (1, deco_id))
937                .collect();
938
939            return [before_enter_decorators, after_exit_decorators].concat();
940        }
941
942        // For basic blocks, we need to handle operation-indexed decorators with proper positioning
943        let block = node.unwrap_basic_block();
944
945        // Before-enter decorators are at position 0
946        let before_enter_decorators: Vec<_> = self
947            .before_enter_decorators(node_id)
948            .iter()
949            .map(|&deco_id| (0, deco_id))
950            .collect();
951
952        // Operation-indexed decorators with their actual positions
953        let op_indexed_decorators: Vec<_> =
954            self.decorator_links_for_node(node_id).unwrap().into_iter().collect();
955
956        // After-exit decorators are positioned after all operations
957        let after_exit_decorators: Vec<_> = self
958            .after_exit_decorators(node_id)
959            .iter()
960            .map(|&deco_id| (block.num_operations() as usize, deco_id))
961            .collect();
962
963        [before_enter_decorators, op_indexed_decorators, after_exit_decorators].concat()
964    }
965}
966
967// MAST FOREST INDEXING
968// ------------------------------------------------------------------------------------------------
969
970impl Index<MastNodeId> for MastForest {
971    type Output = MastNode;
972
973    #[inline(always)]
974    fn index(&self, node_id: MastNodeId) -> &Self::Output {
975        &self.nodes[node_id]
976    }
977}
978
979impl IndexMut<MastNodeId> for MastForest {
980    #[inline(always)]
981    fn index_mut(&mut self, node_id: MastNodeId) -> &mut Self::Output {
982        &mut self.nodes[node_id]
983    }
984}
985
986impl Index<DecoratorId> for MastForest {
987    type Output = Decorator;
988
989    #[inline(always)]
990    fn index(&self, decorator_id: DecoratorId) -> &Self::Output {
991        self.debug_info.decorator(decorator_id).expect("DecoratorId out of bounds")
992    }
993}
994
995impl IndexMut<DecoratorId> for MastForest {
996    #[inline(always)]
997    fn index_mut(&mut self, decorator_id: DecoratorId) -> &mut Self::Output {
998        self.debug_info.decorator_mut(decorator_id).expect("DecoratorId out of bounds")
999    }
1000}
1001
1002// MAST NODE ID
1003// ================================================================================================
1004
1005/// An opaque handle to a [`MastNode`] in some [`MastForest`]. It is the responsibility of the user
1006/// to use a given [`MastNodeId`] with the corresponding [`MastForest`].
1007///
1008/// Note that the [`MastForest`] does *not* ensure that equal [`MastNode`]s have equal
1009/// [`MastNodeId`] handles. Hence, [`MastNodeId`] equality must not be used to test for equality of
1010/// the underlying [`MastNode`].
1011#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
1012#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1013#[cfg_attr(feature = "serde", serde(transparent))]
1014#[cfg_attr(all(feature = "arbitrary", test), miden_test_serde_macros::serde_test)]
1015pub struct MastNodeId(u32);
1016
1017/// Operations that mutate a MAST often produce this mapping between old and new NodeIds.
1018pub type Remapping = BTreeMap<MastNodeId, MastNodeId>;
1019
1020impl MastNodeId {
1021    /// Returns a new `MastNodeId` with the provided inner value, or an error if the provided
1022    /// `value` is greater than the number of nodes in the forest.
1023    ///
1024    /// For use in deserialization.
1025    pub fn from_u32_safe(
1026        value: u32,
1027        mast_forest: &MastForest,
1028    ) -> Result<Self, DeserializationError> {
1029        Self::from_u32_with_node_count(value, mast_forest.nodes.len())
1030    }
1031
1032    /// Returns a new [`MastNodeId`] with the provided `node_id`, or an error if `node_id` is
1033    /// greater than the number of nodes in the [`MastForest`] for which this ID is being
1034    /// constructed.
1035    pub fn from_usize_safe(
1036        node_id: usize,
1037        mast_forest: &MastForest,
1038    ) -> Result<Self, DeserializationError> {
1039        let node_id: u32 = node_id.try_into().map_err(|_| {
1040            DeserializationError::InvalidValue(format!(
1041                "node id '{node_id}' does not fit into a u32"
1042            ))
1043        })?;
1044        MastNodeId::from_u32_safe(node_id, mast_forest)
1045    }
1046
1047    /// Returns a new [`MastNodeId`] from the given `value` without checking its validity.
1048    pub fn new_unchecked(value: u32) -> Self {
1049        Self(value)
1050    }
1051
1052    /// Returns a new [`MastNodeId`] with the provided `id`, or an error if `id` is greater or equal
1053    /// to `node_count`. The `node_count` is the total number of nodes in the [`MastForest`] for
1054    /// which this ID is being constructed.
1055    ///
1056    /// This function can be used when deserializing an id whose corresponding node is not yet in
1057    /// the forest and [`Self::from_u32_safe`] would fail. For instance, when deserializing the ids
1058    /// referenced by the Join node in this forest:
1059    ///
1060    /// ```text
1061    /// [Join(1, 2), Block(foo), Block(bar)]
1062    /// ```
1063    ///
1064    /// Since it is less safe than [`Self::from_u32_safe`] and usually not needed it is not public.
1065    pub(super) fn from_u32_with_node_count(
1066        id: u32,
1067        node_count: usize,
1068    ) -> Result<Self, DeserializationError> {
1069        if (id as usize) < node_count {
1070            Ok(Self(id))
1071        } else {
1072            Err(DeserializationError::InvalidValue(format!(
1073                "Invalid deserialized MAST node ID '{id}', but {node_count} is the number of nodes in the forest",
1074            )))
1075        }
1076    }
1077
1078    /// Remap the NodeId to its new position using the given [`Remapping`].
1079    pub fn remap(&self, remapping: &Remapping) -> Self {
1080        *remapping.get(self).unwrap_or(self)
1081    }
1082}
1083
1084impl From<u32> for MastNodeId {
1085    fn from(value: u32) -> Self {
1086        MastNodeId::new_unchecked(value)
1087    }
1088}
1089
1090impl Idx for MastNodeId {}
1091
1092impl From<MastNodeId> for u32 {
1093    fn from(value: MastNodeId) -> Self {
1094        value.0
1095    }
1096}
1097
1098impl fmt::Display for MastNodeId {
1099    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1100        write!(f, "MastNodeId({})", self.0)
1101    }
1102}
1103
1104#[cfg(any(test, feature = "arbitrary"))]
1105impl Arbitrary for MastNodeId {
1106    type Parameters = ();
1107
1108    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
1109        use proptest::prelude::*;
1110        any::<u32>().prop_map(MastNodeId).boxed()
1111    }
1112
1113    type Strategy = BoxedStrategy<Self>;
1114}
1115
1116// ITERATOR
1117
1118/// Iterates over all the nodes a root depends on, in pre-order. The iteration can include other
1119/// roots in the same forest.
1120pub struct SubtreeIterator<'a> {
1121    forest: &'a MastForest,
1122    discovered: Vec<MastNodeId>,
1123    unvisited: Vec<MastNodeId>,
1124}
1125impl<'a> SubtreeIterator<'a> {
1126    pub fn new(root: &MastNodeId, forest: &'a MastForest) -> Self {
1127        let discovered = vec![];
1128        let unvisited = vec![*root];
1129        SubtreeIterator { forest, discovered, unvisited }
1130    }
1131}
1132impl Iterator for SubtreeIterator<'_> {
1133    type Item = MastNodeId;
1134    fn next(&mut self) -> Option<MastNodeId> {
1135        while let Some(id) = self.unvisited.pop() {
1136            let node = &self.forest[id];
1137            if !node.has_children() {
1138                return Some(id);
1139            } else {
1140                self.discovered.push(id);
1141                node.append_children_to(&mut self.unvisited);
1142            }
1143        }
1144        self.discovered.pop()
1145    }
1146}
1147
1148// DECORATOR ID
1149// ================================================================================================
1150
1151/// An opaque handle to a [`Decorator`] in some [`MastForest`]. It is the responsibility of the user
1152/// to use a given [`DecoratorId`] with the corresponding [`MastForest`].
1153#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
1154#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1155#[cfg_attr(feature = "serde", serde(transparent))]
1156#[cfg_attr(
1157    all(feature = "arbitrary", test),
1158    miden_test_serde_macros::serde_test(binary_serde(true))
1159)]
1160pub struct DecoratorId(u32);
1161
1162impl DecoratorId {
1163    /// Returns a new `DecoratorId` with the provided inner value, or an error if the provided
1164    /// `value` is greater than the number of nodes in the forest.
1165    ///
1166    /// For use in deserialization.
1167    pub fn from_u32_safe(
1168        value: u32,
1169        mast_forest: &MastForest,
1170    ) -> Result<Self, DeserializationError> {
1171        Self::from_u32_bounded(value, mast_forest.debug_info.num_decorators())
1172    }
1173
1174    /// Returns a new `DecoratorId` with the provided inner value, or an error if the provided
1175    /// `value` is greater than or equal to `bound`.
1176    ///
1177    /// For use in deserialization when the bound is known without needing the full MastForest.
1178    pub fn from_u32_bounded(value: u32, bound: usize) -> Result<Self, DeserializationError> {
1179        if (value as usize) < bound {
1180            Ok(Self(value))
1181        } else {
1182            Err(DeserializationError::InvalidValue(format!(
1183                "Invalid deserialized MAST decorator id '{value}', but allows only {bound} decorators",
1184            )))
1185        }
1186    }
1187
1188    /// Creates a new [`DecoratorId`] without checking its validity.
1189    pub(crate) fn new_unchecked(value: u32) -> Self {
1190        Self(value)
1191    }
1192}
1193
1194impl From<u32> for DecoratorId {
1195    fn from(value: u32) -> Self {
1196        DecoratorId::new_unchecked(value)
1197    }
1198}
1199
1200impl Idx for DecoratorId {}
1201
1202impl From<DecoratorId> for u32 {
1203    fn from(value: DecoratorId) -> Self {
1204        value.0
1205    }
1206}
1207
1208impl fmt::Display for DecoratorId {
1209    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1210        write!(f, "DecoratorId({})", self.0)
1211    }
1212}
1213
1214#[cfg(feature = "arbitrary")]
1215impl Arbitrary for DecoratorId {
1216    type Parameters = ();
1217    type Strategy = BoxedStrategy<Self>;
1218
1219    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
1220        any::<u32>().prop_map(Self::from).boxed()
1221    }
1222}
1223
1224impl Serializable for DecoratorId {
1225    fn write_into<W: ByteWriter>(&self, target: &mut W) {
1226        self.0.write_into(target)
1227    }
1228}
1229
1230impl Deserializable for DecoratorId {
1231    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
1232        let value = u32::read_from(source)?;
1233        Ok(Self(value))
1234    }
1235}
1236
1237// ASM OP ID
1238// ================================================================================================
1239
1240/// Unique identifier for an [`AssemblyOp`] within a [`MastForest`].
1241///
1242/// Unlike decorators (which are executed at runtime), AssemblyOps are metadata
1243/// used only for error context and debugging tools.
1244#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
1245#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1246#[cfg_attr(feature = "serde", serde(transparent))]
1247#[cfg_attr(
1248    all(feature = "arbitrary", test),
1249    miden_test_serde_macros::serde_test(binary_serde(true))
1250)]
1251pub struct AsmOpId(u32);
1252
1253impl AsmOpId {
1254    /// Creates a new [`AsmOpId`] with the provided inner value.
1255    pub const fn new(value: u32) -> Self {
1256        Self(value)
1257    }
1258}
1259
1260impl From<u32> for AsmOpId {
1261    fn from(value: u32) -> Self {
1262        AsmOpId::new(value)
1263    }
1264}
1265
1266impl Idx for AsmOpId {}
1267
1268impl From<AsmOpId> for u32 {
1269    fn from(id: AsmOpId) -> Self {
1270        id.0
1271    }
1272}
1273
1274impl fmt::Display for AsmOpId {
1275    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1276        write!(f, "AsmOpId({})", self.0)
1277    }
1278}
1279
1280#[cfg(feature = "arbitrary")]
1281impl Arbitrary for AsmOpId {
1282    type Parameters = ();
1283    type Strategy = BoxedStrategy<Self>;
1284
1285    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
1286        any::<u32>().prop_map(Self::from).boxed()
1287    }
1288}
1289
1290impl Serializable for AsmOpId {
1291    fn write_into<W: ByteWriter>(&self, target: &mut W) {
1292        self.0.write_into(target)
1293    }
1294}
1295
1296impl Deserializable for AsmOpId {
1297    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
1298        let value = u32::read_from(source)?;
1299        Ok(Self(value))
1300    }
1301}
1302
1303/// Derives an error code from an error message by hashing the message and returning the 0th element
1304/// of the resulting [`Word`].
1305pub fn error_code_from_msg(msg: impl AsRef<str>) -> Felt {
1306    // hash the message and return 0th felt of the resulting Word
1307    hash_string_to_word(msg.as_ref())[0]
1308}
1309
1310// MAST FOREST ERROR
1311// ================================================================================================
1312
1313/// Represents the types of errors that can occur when dealing with MAST forest.
1314#[derive(Debug, thiserror::Error, PartialEq, Eq)]
1315pub enum MastForestError {
1316    #[error("MAST forest decorator count exceeds the maximum of {} decorators", u32::MAX)]
1317    TooManyDecorators,
1318    #[error("MAST forest node count exceeds the maximum of {} nodes", MastForest::MAX_NODES)]
1319    TooManyNodes,
1320    #[error("node id {0} is greater than or equal to forest length {1}")]
1321    NodeIdOverflow(MastNodeId, usize),
1322    #[error("decorator id {0} is greater than or equal to decorator count {1}")]
1323    DecoratorIdOverflow(DecoratorId, usize),
1324    #[error("basic block cannot be created from an empty list of operations")]
1325    EmptyBasicBlock,
1326    #[error(
1327        "decorator operation index {operation_idx} is greater than or equal to operation count {num_operations}"
1328    )]
1329    DecoratorOpIndexOutOfBounds {
1330        operation_idx: usize,
1331        num_operations: usize,
1332    },
1333    #[error(
1334        "decorator root of child with node id {0} is missing but is required for fingerprint computation"
1335    )]
1336    ChildFingerprintMissing(MastNodeId),
1337    #[error("advice map key {0} already exists when merging forests")]
1338    AdviceMapKeyCollisionOnMerge(Word),
1339    #[error("decorator storage error: {0}")]
1340    DecoratorError(DecoratorIndexError),
1341    #[error("digest is required for deserialization")]
1342    DigestRequiredForDeserialization,
1343    #[error("invalid batch in basic block node {0:?}: {1}")]
1344    InvalidBatchPadding(MastNodeId, String),
1345    #[error("procedure name references digest that is not a procedure root: {0:?}")]
1346    InvalidProcedureNameDigest(Word),
1347    #[error(
1348        "node {0:?} references child {1:?} which comes after it in the forest (forward reference)"
1349    )]
1350    ForwardReference(MastNodeId, MastNodeId),
1351    #[error("hash mismatch for node {node_id:?}: expected {expected:?}, computed {computed:?}")]
1352    HashMismatch {
1353        node_id: MastNodeId,
1354        expected: Word,
1355        computed: Word,
1356    },
1357    #[error("deserialization failed: {0}")]
1358    Deserialization(DeserializationError),
1359}
1360
1361// Custom serde implementations for MastForest that handle linked decorators properly
1362// by delegating to the existing miden-crypto serialization which already handles
1363// the conversion between linked and owned decorator formats.
1364#[cfg(feature = "serde")]
1365impl Serialize for MastForest {
1366    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1367    where
1368        S: serde::Serializer,
1369    {
1370        // Use the existing miden-crypto serialization which already handles linked decorators
1371        let bytes = Serializable::to_bytes(self);
1372        serializer.serialize_bytes(&bytes)
1373    }
1374}
1375
1376#[cfg(feature = "serde")]
1377impl<'de> Deserialize<'de> for MastForest {
1378    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1379    where
1380        D: serde::Deserializer<'de>,
1381    {
1382        // Deserialize bytes, then use miden-crypto Deserializable
1383        let bytes = Vec::<u8>::deserialize(deserializer)?;
1384        let mut slice_reader = SliceReader::new(&bytes);
1385        Deserializable::read_from(&mut slice_reader).map_err(serde::de::Error::custom)
1386    }
1387}