1#[cfg(test)]
44use alloc::collections::BTreeSet;
45use alloc::{collections::BTreeMap, string::String, sync::Arc, vec::Vec};
46use core::{fmt, ops::Index};
47
48use dense_order::{
49 canonicalize_parts, validate_dense_node_order, validate_mast_forest_parts_bounds,
50};
51#[cfg(any(test, feature = "arbitrary"))]
52use proptest::prelude::*;
53
54mod dense_order;
55mod node;
56#[cfg(any(test, feature = "arbitrary"))]
57pub use node::arbitrary;
58pub(crate) use node::collect_immediate_placements;
59pub use node::{
60 BasicBlockNode, BasicBlockNodeBuilder, CallNode, CallNodeBuilder, DynNode, DynNodeBuilder,
61 ExternalNode, ExternalNodeBuilder, JoinNode, JoinNodeBuilder, LoopNode, LoopNodeBuilder,
62 MastForestContributor, MastNode, MastNodeBuilder, MastNodeContext, MastNodeExt, OP_BATCH_SIZE,
63 OP_GROUP_SIZE, OpBatch, SplitNode, SplitNodeBuilder,
64};
65
66use crate::{
67 Felt, Word,
68 advice::AdviceMap,
69 serde::{ByteWriter, Deserializable, DeserializationError, Serializable},
70 utils::{DenseIdMap, Idx, IndexVec, hash_string_to_word},
71};
72
73mod serialization;
74pub use serialization::{
75 AdviceMapView, AdviceValueView, MastForestReadMode, MastForestReadView, MastForestView,
76 MastForestWireView, MastNodeEntry, MastNodeInfo,
77};
78
79mod dense_builder;
80pub use dense_builder::DenseMastForestBuilder;
81
82mod untrusted;
83pub use untrusted::{UntrustedMastForest, UntrustedMastForestReadOptions};
84
85mod merger;
86pub use merger::{MastForestMergeSource, MastForestRootMap};
87pub(crate) use merger::{MastForestMerger, MixedMastForestMerger};
88
89mod multi_forest_node_iterator;
90pub(crate) use multi_forest_node_iterator::*;
91
92mod node_builder_utils;
93pub use node_builder_utils::build_node_with_remapped_ids;
94
95mod sparse;
96pub use sparse::{MastForestId, SparseMastForest, SparseMastForestBuilder, VisitKind};
97
98mod forest_read;
99pub use forest_read::MastForestRead;
100pub(crate) use forest_read::StoredContentKey;
101
102#[cfg(test)]
103mod tests;
104
105#[derive(Clone, Debug, Default)]
125#[cfg_attr(
126 all(feature = "arbitrary", test),
127 miden_test_serialization_macros::serialization_test
128)]
129pub struct MastForest {
130 nodes: IndexVec<MastNodeId, MastNode>,
132
133 roots: Vec<MastNodeId>,
135
136 advice_map: AdviceMap,
138
139 commitment: MastForestCommitment,
141}
142
143#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
154struct MastForestCommitment {
155 commitment: Word,
157
158 interface_commitment: Word,
160
161 dependency_commitment: Word,
163
164 advice_commitment: Word,
166}
167
168pub(crate) struct MastForestParts {
170 pub nodes: IndexVec<MastNodeId, MastNode>,
171 pub roots: Vec<MastNodeId>,
172 pub advice_map: AdviceMap,
173}
174
175impl MastForest {
178 pub fn new() -> Self {
180 Self {
181 nodes: IndexVec::new(),
182 roots: Vec::new(),
183 advice_map: AdviceMap::default(),
184 commitment: empty_mast_forest_commitment(),
185 }
186 }
187
188 #[doc(hidden)]
192 pub fn from_raw_parts(
193 nodes: IndexVec<MastNodeId, MastNode>,
194 roots: Vec<MastNodeId>,
195 advice_map: AdviceMap,
196 ) -> Result<Self, MastForestError> {
197 Self::from_parts(MastForestParts { nodes, roots, advice_map })
198 }
199
200 #[doc(hidden)]
206 pub fn from_raw_parts_with_id_map(
207 nodes: IndexVec<MastNodeId, MastNode>,
208 roots: Vec<MastNodeId>,
209 advice_map: AdviceMap,
210 ) -> Result<(Self, DenseIdMap<MastNodeId, MastNodeId>), MastForestError> {
211 Self::from_parts_with_id_map(MastForestParts { nodes, roots, advice_map })
212 }
213
214 pub(crate) fn from_parts(parts: MastForestParts) -> Result<Self, MastForestError> {
216 Self::from_parts_with_id_map(parts).map(|(forest, _remapping)| forest)
217 }
218
219 pub(crate) fn from_parts_with_id_map(
220 parts: MastForestParts,
221 ) -> Result<(Self, DenseIdMap<MastNodeId, MastNodeId>), MastForestError> {
222 validate_mast_forest_parts_bounds(&parts)?;
223 let (parts, id_remapping) = canonicalize_parts(parts)?;
224
225 let forest = Self {
226 commitment: compute_mast_forest_commitment(
227 &parts.nodes,
228 &parts.roots,
229 &parts.advice_map,
230 ),
231 nodes: parts.nodes,
232 roots: parts.roots,
233 advice_map: parts.advice_map,
234 };
235
236 forest.validate_dense_node_order()?;
237 forest.validate()?;
238 forest.validate_node_hashes()?;
239 Ok((forest, id_remapping))
240 }
241
242 pub(in crate::mast) fn from_trusted_deserialization_parts(
243 parts: MastForestParts,
244 ) -> Result<Self, MastForestError> {
245 validate_mast_forest_parts_bounds(&parts)?;
246 validate_dense_node_order(&parts.nodes)?;
249 Ok(Self {
250 commitment: compute_mast_forest_commitment(
251 &parts.nodes,
252 &parts.roots,
253 &parts.advice_map,
254 ),
255 nodes: parts.nodes,
256 roots: parts.roots,
257 advice_map: parts.advice_map,
258 })
259 }
260}
261
262impl PartialEq for MastForest {
265 fn eq(&self, other: &Self) -> bool {
266 self.nodes == other.nodes
267 && self.roots == other.roots
268 && self.advice_map == other.advice_map
269 }
270}
271
272impl Eq for MastForest {}
273
274impl MastForest {
277 const MAX_NODES: usize = (1 << 30) - 1;
279
280 #[cfg(any(test, feature = "arbitrary"))]
288 pub fn make_root(&mut self, new_root_id: MastNodeId) {
289 assert!(new_root_id.to_usize() < self.nodes.len());
290
291 if !self.roots.contains(&new_root_id) {
292 self.roots.push(new_root_id);
293 self.commitment = self.compute_mast_forest_commitment();
294 }
295 }
296
297 #[cfg(test)]
305 fn remove_nodes(
306 &mut self,
307 nodes_to_remove: &BTreeSet<MastNodeId>,
308 ) -> BTreeMap<MastNodeId, MastNodeId> {
309 if nodes_to_remove.is_empty() {
310 return BTreeMap::new();
311 }
312
313 self.assert_nodes_to_remove_are_orphaned(nodes_to_remove);
314
315 let old_nodes = core::mem::replace(&mut self.nodes, IndexVec::new());
316 let old_root_ids = core::mem::take(&mut self.roots);
317 let (retained_nodes, id_remappings) = remove_nodes(old_nodes.into_inner(), nodes_to_remove);
318
319 self.remap_and_add_nodes(retained_nodes, &id_remappings);
320 self.remap_and_add_roots(old_root_ids, &id_remappings);
321
322 self.commitment = self.compute_mast_forest_commitment();
323
324 id_remappings
325 }
326
327 pub fn merge<'forest, F>(
374 forests: impl IntoIterator<Item = F>,
375 ) -> Result<(MastForest, MastForestRootMap), MastForestError>
376 where
377 F: Into<MastForestMergeSource<'forest>>,
378 {
379 let forests = forests.into_iter().map(Into::into).collect::<Vec<_>>();
380 if forests.iter().all(|forest| forest.as_dense().is_some()) {
381 return MastForestMerger::merge(forests.iter().map(|forest| {
382 forest.as_dense().expect("all merge sources were checked as dense")
383 }));
384 }
385
386 MixedMastForestMerger::merge_dense(forests)
387 }
388}
389
390impl MastForest {
393 #[cfg(test)]
394 fn assert_nodes_to_remove_are_orphaned(&self, nodes_to_remove: &BTreeSet<MastNodeId>) {
395 for (node_idx, node) in self.nodes.iter().enumerate() {
396 let node_id = MastNodeId::new_unchecked(node_idx.try_into().expect("too many nodes"));
397 if nodes_to_remove.contains(&node_id) {
398 continue;
399 }
400
401 node.for_each_child(|child_id| {
402 assert!(
403 !nodes_to_remove.contains(&child_id),
404 "cannot remove node {child_id:?}; retained node {node_id:?} references it"
405 );
406 });
407 }
408 }
409
410 #[cfg(test)]
416 fn remap_and_add_nodes(
417 &mut self,
418 nodes_to_add: Vec<MastNode>,
419 id_remappings: &BTreeMap<MastNodeId, MastNodeId>,
420 ) {
421 assert!(self.nodes.is_empty());
422 let node_builders =
423 nodes_to_add.into_iter().map(|node| node.to_builder(self)).collect::<Vec<_>>();
424
425 for live_node_builder in node_builders {
428 let node = live_node_builder.remap_children(id_remappings).build_linked().unwrap();
429 self.nodes.push(node).unwrap();
430 }
431 }
432
433 #[cfg(test)]
438 fn remap_and_add_roots(
439 &mut self,
440 old_root_ids: Vec<MastNodeId>,
441 id_remappings: &BTreeMap<MastNodeId, MastNodeId>,
442 ) {
443 assert!(self.roots.is_empty());
444
445 for old_root_id in old_root_ids {
446 if let Some(new_root_id) = id_remappings.get(&old_root_id).copied() {
447 self.make_root(new_root_id);
448 }
449 }
450 }
451}
452
453#[cfg(test)]
456fn remove_nodes(
457 mast_nodes: Vec<MastNode>,
458 nodes_to_remove: &BTreeSet<MastNodeId>,
459) -> (Vec<MastNode>, BTreeMap<MastNodeId, MastNodeId>) {
460 assert!(mast_nodes.len() < u32::MAX as usize);
462
463 let mut retained_nodes = Vec::with_capacity(mast_nodes.len());
464 let mut id_remappings = BTreeMap::new();
465
466 for (old_node_index, old_node) in mast_nodes.into_iter().enumerate() {
467 let old_node_id: MastNodeId = MastNodeId(old_node_index as u32);
468
469 if !nodes_to_remove.contains(&old_node_id) {
470 let new_node_id: MastNodeId = MastNodeId(retained_nodes.len() as u32);
471 id_remappings.insert(old_node_id, new_node_id);
472
473 retained_nodes.push(old_node);
474 }
475 }
476
477 (retained_nodes, id_remappings)
478}
479
480fn empty_mast_forest_commitment() -> MastForestCommitment {
481 let interface_commitment = crate::chiplets::hasher::hash_words(&[]);
482 let dependency_commitment = crate::chiplets::hasher::hash_words(&[]);
483 let advice_commitment = AdviceMap::default().commitment();
484 MastForestCommitment::new(interface_commitment, dependency_commitment, advice_commitment)
485}
486
487fn compute_nodes_commitment(
488 nodes: &IndexVec<MastNodeId, MastNode>,
489 node_ids: &[MastNodeId],
490) -> Word {
491 let mut digests: Vec<Word> = node_ids.iter().map(|&id| nodes[id].digest()).collect();
492 digests.sort_unstable();
493 crate::chiplets::hasher::hash_words(&digests)
494}
495
496fn compute_dependency_commitment(nodes: &IndexVec<MastNodeId, MastNode>) -> Word {
497 let mut digests: Vec<Word> = nodes
498 .iter()
499 .filter(|node| node.is_external())
500 .map(MastNodeExt::digest)
501 .collect();
502 digests.sort_unstable();
503 crate::chiplets::hasher::hash_words(&digests)
504}
505
506impl MastForestCommitment {
507 fn new(
508 interface_commitment: Word,
509 dependency_commitment: Word,
510 advice_commitment: Word,
511 ) -> Self {
512 let commitment = crate::chiplets::hasher::hash_words(&[
513 interface_commitment,
514 dependency_commitment,
515 advice_commitment,
516 ]);
517 Self {
518 commitment,
519 interface_commitment,
520 dependency_commitment,
521 advice_commitment,
522 }
523 }
524}
525
526fn compute_mast_forest_commitment(
527 nodes: &IndexVec<MastNodeId, MastNode>,
528 roots: &[MastNodeId],
529 advice_map: &AdviceMap,
530) -> MastForestCommitment {
531 let interface_commitment = compute_nodes_commitment(nodes, roots);
532 let dependency_commitment = compute_dependency_commitment(nodes);
533 let advice_commitment = advice_map.commitment();
534 MastForestCommitment::new(interface_commitment, dependency_commitment, advice_commitment)
535}
536
537impl MastForest {
540 #[inline(always)]
545 pub fn get_node_by_id(&self, node_id: MastNodeId) -> Option<&MastNode> {
546 self.nodes.get(node_id)
547 }
548
549 #[inline(always)]
551 pub fn find_procedure_root(&self, digest: Word) -> Option<MastNodeId> {
552 self.roots.iter().find(|&&root_id| self[root_id].digest() == digest).copied()
553 }
554
555 pub fn is_procedure_root(&self, node_id: MastNodeId) -> bool {
557 self.roots.contains(&node_id)
558 }
559
560 pub fn is_procedure_root_with_exact_digest(&self, node_id: MastNodeId, digest: Word) -> bool {
566 self.is_procedure_root(node_id) && self[node_id].digest() == digest
567 }
568
569 pub fn procedure_digests(&self) -> impl Iterator<Item = Word> + '_ {
571 self.roots.iter().map(|&root_id| self[root_id].digest())
572 }
573
574 pub fn local_procedure_digests(&self) -> impl Iterator<Item = Word> + '_ {
578 self.roots.iter().filter_map(|&root_id| {
579 let node = &self[root_id];
580 if node.is_external() { None } else { Some(node.digest()) }
581 })
582 }
583
584 pub fn procedure_roots(&self) -> &[MastNodeId] {
586 &self.roots
587 }
588
589 pub fn num_procedures(&self) -> u32 {
591 self.roots
592 .len()
593 .try_into()
594 .expect("MAST forest contains more than 2^32 procedures.")
595 }
596
597 pub fn compute_nodes_commitment<'a>(
602 &self,
603 node_ids: impl IntoIterator<Item = &'a MastNodeId>,
604 ) -> Word {
605 let node_ids = node_ids.into_iter().copied().collect::<Vec<_>>();
606 compute_nodes_commitment(&self.nodes, &node_ids)
607 }
608
609 pub fn interface_commitment(&self) -> Word {
614 self.commitment.interface_commitment
615 }
616
617 pub fn dependency_commitment(&self) -> Word {
622 self.commitment.dependency_commitment
623 }
624
625 pub fn advice_commitment(&self) -> Word {
629 self.commitment.advice_commitment
630 }
631
632 fn compute_mast_forest_commitment(&self) -> MastForestCommitment {
633 compute_mast_forest_commitment(&self.nodes, &self.roots, &self.advice_map)
634 }
635
636 pub fn commitment(&self) -> Word {
640 self.commitment.commitment
641 }
642
643 pub fn num_nodes(&self) -> u32 {
645 self.nodes.len() as u32
646 }
647
648 pub fn nodes(&self) -> &[MastNode] {
650 self.nodes.as_slice()
651 }
652
653 pub fn advice_map(&self) -> &AdviceMap {
654 &self.advice_map
655 }
656
657 pub fn with_advice_map(mut self, advice_map: AdviceMap) -> Self {
659 self.advice_map.extend(advice_map);
660 self.commitment = self.compute_mast_forest_commitment();
661 self
662 }
663
664 pub fn write_hashless<W: ByteWriter>(&self, target: &mut W) {
675 serialization::write_hashless_into(self, target);
676 }
677
678 pub fn hashless_size_hint(&self) -> usize {
687 self.serialized_size_hint(true)
688 }
689}
690
691impl MastForest {
693 pub(in crate::mast) fn validate_dense_node_order(&self) -> Result<(), MastForestError> {
694 validate_dense_node_order(&self.nodes)
695 }
696
697 fn validate_basic_block_invariants(&self) -> Result<(), MastForestError> {
698 for (node_id_idx, node) in self.nodes.iter().enumerate() {
699 let node_id =
700 MastNodeId::new_unchecked(node_id_idx.try_into().expect("too many nodes"));
701 if let MastNode::Block(basic_block) = node {
702 basic_block.validate_batch_invariants().map_err(|error_msg| {
703 MastForestError::InvalidBatchPadding(node_id, error_msg)
704 })?;
705 }
706 }
707
708 Ok(())
709 }
710
711 pub fn validate(&self) -> Result<(), MastForestError> {
722 self.validate_basic_block_invariants()?;
723 Ok(())
724 }
725
726 fn validate_node_hashes(&self) -> Result<(), MastForestError> {
731 let computed_hashes = self.compute_node_hashes()?;
732 for (node_idx, (node, computed_digest)) in
733 self.nodes.iter().zip(computed_hashes).enumerate()
734 {
735 let expected_digest = node.digest();
736 if expected_digest != computed_digest {
737 return Err(MastForestError::HashMismatch {
738 node_id: MastNodeId::new_unchecked(node_idx as u32),
739 expected: expected_digest,
740 computed: computed_digest,
741 });
742 }
743 }
744
745 Ok(())
746 }
747
748 fn compute_node_hashes(&self) -> Result<Vec<Word>, MastForestError> {
757 let mut computed_hashes = Vec::with_capacity(self.nodes.len());
758 for (node_idx, node) in self.nodes.iter().enumerate() {
759 let node_id = MastNodeId::new_unchecked(node_idx as u32);
760 let digest = node.compute_digest(|child_id| {
761 if child_id >= node_id {
762 return Err(MastForestError::ForwardReference(node_id, child_id));
763 }
764 Ok(computed_hashes[child_id.0 as usize])
765 })?;
766 computed_hashes.push(digest);
767 }
768
769 Ok(computed_hashes)
770 }
771}
772
773impl Index<MastNodeId> for MastForest {
777 type Output = MastNode;
778
779 #[inline(always)]
780 fn index(&self, node_id: MastNodeId) -> &Self::Output {
781 &self.nodes[node_id]
782 }
783}
784
785pub trait ExecutableMastForest {
795 fn get_node_by_id(&self, node_id: MastNodeId) -> Option<&MastNode>;
798
799 fn get_digest_by_id(&self, node_id: MastNodeId) -> Option<Word>;
808
809 fn find_procedure_root(&self, digest: Word) -> Option<MastNodeId>;
811
812 fn advice_map(&self) -> &AdviceMap;
814}
815
816impl ExecutableMastForest for MastForest {
817 #[inline(always)]
818 fn get_node_by_id(&self, node_id: MastNodeId) -> Option<&MastNode> {
819 MastForest::get_node_by_id(self, node_id)
820 }
821
822 #[inline(always)]
823 fn get_digest_by_id(&self, node_id: MastNodeId) -> Option<Word> {
824 MastForest::get_node_by_id(self, node_id).map(MastNodeExt::digest)
825 }
826
827 #[inline(always)]
828 fn find_procedure_root(&self, digest: Word) -> Option<MastNodeId> {
829 MastForest::find_procedure_root(self, digest)
830 }
831
832 #[inline(always)]
833 fn advice_map(&self) -> &AdviceMap {
834 MastForest::advice_map(self)
835 }
836}
837
838impl<T> Index<MastNodeId> for Arc<T>
842where
843 T: Index<MastNodeId, Output = MastNode> + ?Sized,
844{
845 type Output = MastNode;
846
847 #[inline(always)]
848 fn index(&self, node_id: MastNodeId) -> &Self::Output {
849 &(**self)[node_id]
850 }
851}
852
853impl<T: ExecutableMastForest + ?Sized> ExecutableMastForest for Arc<T> {
854 #[inline(always)]
855 fn get_node_by_id(&self, node_id: MastNodeId) -> Option<&MastNode> {
856 T::get_node_by_id(self, node_id)
857 }
858
859 #[inline(always)]
860 fn get_digest_by_id(&self, node_id: MastNodeId) -> Option<Word> {
861 T::get_digest_by_id(self, node_id)
862 }
863
864 #[inline(always)]
865 fn find_procedure_root(&self, digest: Word) -> Option<MastNodeId> {
866 T::find_procedure_root(self, digest)
867 }
868
869 #[inline(always)]
870 fn advice_map(&self) -> &AdviceMap {
871 T::advice_map(self)
872 }
873}
874
875#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
885#[cfg_attr(
886 all(feature = "arbitrary", test),
887 miden_test_serialization_macros::serialization_test
888)]
889pub struct MastNodeId(u32);
890
891pub type Remapping = BTreeMap<MastNodeId, MastNodeId>;
893
894impl MastNodeId {
895 pub fn from_u32_safe(
900 value: u32,
901 mast_forest: &MastForest,
902 ) -> Result<Self, DeserializationError> {
903 Self::from_u32_with_node_count(value, mast_forest.nodes.len())
904 }
905
906 pub fn new_unchecked(value: u32) -> Self {
908 Self(value)
909 }
910
911 pub(super) fn from_u32_with_node_count(
925 id: u32,
926 node_count: usize,
927 ) -> Result<Self, DeserializationError> {
928 if (id as usize) < node_count {
929 Ok(Self(id))
930 } else {
931 Err(DeserializationError::InvalidValue(format!(
932 "Invalid deserialized MAST node ID '{id}', but {node_count} is the number of nodes in the forest",
933 )))
934 }
935 }
936
937 pub fn remap(&self, remapping: &Remapping) -> Self {
939 *remapping.get(self).unwrap_or(self)
940 }
941}
942
943impl From<u32> for MastNodeId {
944 fn from(value: u32) -> Self {
945 MastNodeId::new_unchecked(value)
946 }
947}
948
949impl Idx for MastNodeId {}
950
951impl From<MastNodeId> for u32 {
952 fn from(value: MastNodeId) -> Self {
953 value.0
954 }
955}
956
957impl Serializable for MastNodeId {
958 fn write_into<W: ByteWriter>(&self, target: &mut W) {
959 Serializable::write_into(&self.0, target);
960 }
961}
962
963impl Deserializable for MastNodeId {
964 fn read_from<R: crate::serde::ByteReader>(
965 source: &mut R,
966 ) -> Result<Self, DeserializationError> {
967 Ok(Self(<u32 as Deserializable>::read_from(source)?))
968 }
969
970 fn min_serialized_size() -> usize {
971 <u32 as Deserializable>::min_serialized_size()
972 }
973}
974
975impl fmt::Display for MastNodeId {
976 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
977 write!(f, "MastNodeId({})", self.0)
978 }
979}
980
981#[cfg(any(test, feature = "arbitrary"))]
982impl Arbitrary for MastNodeId {
983 type Parameters = ();
984
985 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
986 use proptest::prelude::*;
987 any::<u32>().prop_map(MastNodeId).boxed()
988 }
989
990 type Strategy = BoxedStrategy<Self>;
991}
992
993pub struct SubtreeIterator<'a> {
998 forest: &'a MastForest,
999 discovered: Vec<MastNodeId>,
1000 unvisited: Vec<MastNodeId>,
1001}
1002impl<'a> SubtreeIterator<'a> {
1003 pub fn new(root: &MastNodeId, forest: &'a MastForest) -> Self {
1004 let discovered = vec![];
1005 let unvisited = vec![*root];
1006 SubtreeIterator { forest, discovered, unvisited }
1007 }
1008}
1009impl Iterator for SubtreeIterator<'_> {
1010 type Item = MastNodeId;
1011 fn next(&mut self) -> Option<MastNodeId> {
1012 while let Some(id) = self.unvisited.pop() {
1013 let node = &self.forest[id];
1014 if !node.has_children() {
1015 return Some(id);
1016 } else {
1017 self.discovered.push(id);
1018 node.append_children_to(&mut self.unvisited);
1019 }
1020 }
1021 self.discovered.pop()
1022 }
1023}
1024
1025pub fn error_code_from_msg(msg: impl AsRef<str>) -> Felt {
1028 hash_string_to_word(msg.as_ref())[0]
1030}
1031
1032#[derive(Debug, thiserror::Error, PartialEq, Eq)]
1037pub enum MastForestError {
1038 #[error("MAST forest node count exceeds the maximum of {} nodes", MastForest::MAX_NODES)]
1039 TooManyNodes,
1040 #[error("node id {0} is greater than or equal to forest length {1}")]
1041 NodeIdOverflow(MastNodeId, usize),
1042 #[error("basic block cannot be created from an empty list of operations")]
1043 EmptyBasicBlock,
1044 #[error("advice map key {0} already exists when merging forests")]
1045 AdviceMapKeyCollisionOnMerge(Word),
1046 #[error("sparse MAST merge cannot retain advice map entries")]
1047 AdviceMapNotSupportedInSparseMerge,
1048 #[error("dense MAST merge requires full node data for {0:?}")]
1049 MissingNodeOnDenseMerge(MastNodeId),
1050 #[error("MAST merge requires node data or a digest for procedure root {0:?}")]
1051 MissingRootOnMerge(MastNodeId),
1052 #[error("node {0:?} is not a procedure root in the source MAST forest")]
1053 NotAProcedureRoot(MastNodeId),
1054 #[error("external node digest {0} resolves to conflicting full nodes")]
1055 ConflictingNodeDigestOnMerge(Word),
1056 #[error("cyclic dependency encountered while merging node {0:?}")]
1057 CyclicDependencyOnMerge(MastNodeId),
1058 #[error("digest is required for deserialization")]
1059 DigestRequiredForDeserialization,
1060 #[error("invalid batch in basic block node {0:?}: {1}")]
1061 InvalidBatchPadding(MastNodeId, String),
1062 #[error("invalid node order at {node_id:?}: {reason}")]
1063 InvalidNodeOrder { node_id: MastNodeId, reason: String },
1064 #[error(
1065 "node {0:?} references child {1:?} which comes after it in the forest (forward reference)"
1066 )]
1067 ForwardReference(MastNodeId, MastNodeId),
1068 #[error("hash mismatch for node {node_id:?}: expected {expected:?}, computed {computed:?}")]
1069 HashMismatch {
1070 node_id: MastNodeId,
1071 expected: Word,
1072 computed: Word,
1073 },
1074 #[error("deserialization failed: {0}")]
1075 Deserialization(DeserializationError),
1076}
1077
1078