1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
use alloc::vec::Vec;
use core::{fmt, ops::Index};
use miden_crypto::hash::rpo::RpoDigest;
mod node;
pub use node::{
BasicBlockNode, CallNode, DynNode, ExternalNode, JoinNode, LoopNode, MastNode, OpBatch,
OperationOrDecorator, SplitNode, OP_BATCH_SIZE, OP_GROUP_SIZE,
};
use winter_utils::DeserializationError;
use crate::{DecoratorList, Operation};
mod serialization;
#[cfg(test)]
mod tests;
// MAST FOREST
// ================================================================================================
/// Represents one or more procedures, represented as a collection of [`MastNode`]s.
///
/// A [`MastForest`] does not have an entrypoint, and hence is not executable. A [`crate::Program`]
/// can be built from a [`MastForest`] to specify an entrypoint.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct MastForest {
/// All of the nodes local to the trees comprising the MAST forest.
nodes: Vec<MastNode>,
/// Roots of procedures defined within this MAST forest.
roots: Vec<MastNodeId>,
}
// ------------------------------------------------------------------------------------------------
/// Constructors
impl MastForest {
/// Creates a new empty [`MastForest`].
pub fn new() -> Self {
Self::default()
}
}
// ------------------------------------------------------------------------------------------------
/// State mutators
impl MastForest {
/// The maximum number of nodes that can be stored in a single MAST forest.
const MAX_NODES: usize = (1 << 30) - 1;
/// Adds a node to the forest, and returns the associated [`MastNodeId`].
///
/// Adding two duplicate nodes will result in two distinct returned [`MastNodeId`]s.
pub fn add_node(&mut self, node: MastNode) -> Result<MastNodeId, MastForestError> {
if self.nodes.len() == Self::MAX_NODES {
return Err(MastForestError::TooManyNodes);
}
let new_node_id = MastNodeId(self.nodes.len() as u32);
self.nodes.push(node);
Ok(new_node_id)
}
/// Adds a basic block node to the forest, and returns the [`MastNodeId`] associated with it.
pub fn add_block(
&mut self,
operations: Vec<Operation>,
decorators: Option<DecoratorList>,
) -> Result<MastNodeId, MastForestError> {
match decorators {
Some(decorators) => {
self.add_node(MastNode::new_basic_block_with_decorators(operations, decorators))
},
None => self.add_node(MastNode::new_basic_block(operations)),
}
}
/// Adds a join node to the forest, and returns the [`MastNodeId`] associated with it.
pub fn add_join(
&mut self,
left_child: MastNodeId,
right_child: MastNodeId,
) -> Result<MastNodeId, MastForestError> {
let join = MastNode::new_join(left_child, right_child, self)?;
self.add_node(join)
}
/// Adds a split node to the forest, and returns the [`MastNodeId`] associated with it.
pub fn add_split(
&mut self,
if_branch: MastNodeId,
else_branch: MastNodeId,
) -> Result<MastNodeId, MastForestError> {
let split = MastNode::new_split(if_branch, else_branch, self)?;
self.add_node(split)
}
/// Adds a loop node to the forest, and returns the [`MastNodeId`] associated with it.
pub fn add_loop(&mut self, body: MastNodeId) -> Result<MastNodeId, MastForestError> {
let loop_node = MastNode::new_loop(body, self)?;
self.add_node(loop_node)
}
/// Adds a call node to the forest, and returns the [`MastNodeId`] associated with it.
pub fn add_call(&mut self, callee: MastNodeId) -> Result<MastNodeId, MastForestError> {
let call = MastNode::new_call(callee, self)?;
self.add_node(call)
}
/// Adds a syscall node to the forest, and returns the [`MastNodeId`] associated with it.
pub fn add_syscall(&mut self, callee: MastNodeId) -> Result<MastNodeId, MastForestError> {
let syscall = MastNode::new_syscall(callee, self)?;
self.add_node(syscall)
}
/// Adds a dyn node to the forest, and returns the [`MastNodeId`] associated with it.
pub fn add_dyn(&mut self) -> Result<MastNodeId, MastForestError> {
self.add_node(MastNode::new_dyn())
}
/// Adds an external node to the forest, and returns the [`MastNodeId`] associated with it.
pub fn add_external(&mut self, mast_root: RpoDigest) -> Result<MastNodeId, MastForestError> {
self.add_node(MastNode::new_external(mast_root))
}
/// Marks the given [`MastNodeId`] as being the root of a procedure.
///
/// # Panics
/// - if `new_root_id`'s internal index is larger than the number of nodes in this forest (i.e.
/// clearly doesn't belong to this MAST forest).
pub fn make_root(&mut self, new_root_id: MastNodeId) {
assert!((new_root_id.0 as usize) < self.nodes.len());
if !self.roots.contains(&new_root_id) {
self.roots.push(new_root_id);
}
}
}
// ------------------------------------------------------------------------------------------------
/// Public accessors
impl MastForest {
/// Returns the [`MastNode`] associated with the provided [`MastNodeId`] if valid, or else
/// `None`.
///
/// This is the failable version of indexing (e.g. `mast_forest[node_id]`).
#[inline(always)]
pub fn get_node_by_id(&self, node_id: MastNodeId) -> Option<&MastNode> {
let idx = node_id.0 as usize;
self.nodes.get(idx)
}
/// Returns the [`MastNodeId`] of the procedure associated with a given digest, if any.
#[inline(always)]
pub fn find_procedure_root(&self, digest: RpoDigest) -> Option<MastNodeId> {
self.roots.iter().find(|&&root_id| self[root_id].digest() == digest).copied()
}
/// Returns true if a node with the specified ID is a root of a procedure in this MAST forest.
pub fn is_procedure_root(&self, node_id: MastNodeId) -> bool {
self.roots.contains(&node_id)
}
/// Returns an iterator over the digest of the procedures in this MAST forest.
pub fn procedure_digests(&self) -> impl Iterator<Item = RpoDigest> + '_ {
self.roots.iter().map(|&root_id| self[root_id].digest())
}
/// Returns an iterator over the IDs of the procedures in this MAST forest.
pub fn procedure_roots(&self) -> &[MastNodeId] {
&self.roots
}
/// Returns the number of procedures in this MAST forest.
pub fn num_procedures(&self) -> u32 {
self.roots
.len()
.try_into()
.expect("MAST forest contains more than 2^32 procedures.")
}
}
impl Index<MastNodeId> for MastForest {
type Output = MastNode;
#[inline(always)]
fn index(&self, node_id: MastNodeId) -> &Self::Output {
let idx = node_id.0 as usize;
&self.nodes[idx]
}
}
// MAST NODE ID
// ================================================================================================
/// An opaque handle to a [`MastNode`] in some [`MastForest`]. It is the responsibility of the user
/// to use a given [`MastNodeId`] with the corresponding [`MastForest`].
///
/// Note that the [`MastForest`] does *not* ensure that equal [`MastNode`]s have equal
/// [`MastNodeId`] handles. Hence, [`MastNodeId`] equality must not be used to test for equality of
/// the underlying [`MastNode`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MastNodeId(u32);
impl MastNodeId {
/// Returns a new `MastNodeId` with the provided inner value, or an error if the provided
/// `value` is greater than the number of nodes in the forest.
///
/// For use in deserialization.
pub fn from_u32_safe(
value: u32,
mast_forest: &MastForest,
) -> Result<Self, DeserializationError> {
if (value as usize) < mast_forest.nodes.len() {
Ok(Self(value))
} else {
Err(DeserializationError::InvalidValue(format!(
"Invalid deserialized MAST node ID '{}', but only {} nodes in the forest",
value,
mast_forest.nodes.len(),
)))
}
}
pub fn as_usize(&self) -> usize {
self.0 as usize
}
pub fn as_u32(&self) -> u32 {
self.0
}
}
impl From<MastNodeId> for usize {
fn from(value: MastNodeId) -> Self {
value.0 as usize
}
}
impl From<MastNodeId> for u32 {
fn from(value: MastNodeId) -> Self {
value.0
}
}
impl From<&MastNodeId> for u32 {
fn from(value: &MastNodeId) -> Self {
value.0
}
}
impl fmt::Display for MastNodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MastNodeId({})", self.0)
}
}
// MAST FOREST ERROR
// ================================================================================================
/// Represents the types of errors that can occur when dealing with MAST forest.
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum MastForestError {
#[error(
"invalid node count: MAST forest exceeds the maximum of {} nodes",
MastForest::MAX_NODES
)]
TooManyNodes,
#[error("node id: {0} is greater than or equal to forest length: {1}")]
NodeIdOverflow(MastNodeId, usize),
}