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
use core::fmt;
use miden_crypto::{hash::rpo::RpoDigest, Felt};
use miden_formatting::prettier::PrettyPrint;
use crate::{
chiplets::hasher,
mast::{MastForest, MastForestError, MastNodeId},
OPCODE_SPLIT,
};
// SPLIT NODE
// ================================================================================================
/// A Split node defines conditional execution. When the VM encounters a Split node it executes
/// either the `on_true` child or `on_false` child.
///
/// Which child is executed is determined based on the top of the stack. If the value is `1`, then
/// the `on_true` child is executed. If the value is `0`, then the `on_false` child is executed. If
/// the value is neither `0` nor `1`, the execution fails.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SplitNode {
branches: [MastNodeId; 2],
digest: RpoDigest,
}
/// Constants
impl SplitNode {
/// The domain of the split node (used for control block hashing).
pub const DOMAIN: Felt = Felt::new(OPCODE_SPLIT as u64);
}
/// Constructors
impl SplitNode {
pub fn new(
branches: [MastNodeId; 2],
mast_forest: &MastForest,
) -> Result<Self, MastForestError> {
let forest_len = mast_forest.nodes.len();
if branches[0].as_usize() >= forest_len {
return Err(MastForestError::NodeIdOverflow(branches[0], forest_len));
} else if branches[1].as_usize() >= forest_len {
return Err(MastForestError::NodeIdOverflow(branches[1], forest_len));
}
let digest = {
let if_branch_hash = mast_forest[branches[0]].digest();
let else_branch_hash = mast_forest[branches[1]].digest();
hasher::merge_in_domain(&[if_branch_hash, else_branch_hash], Self::DOMAIN)
};
Ok(Self { branches, digest })
}
/// Returns a new [`SplitNode`] from values that are assumed to be correct.
/// Should only be used when the source of the inputs is trusted (e.g. deserialization).
pub fn new_unsafe(branches: [MastNodeId; 2], digest: RpoDigest) -> Self {
Self { branches, digest }
}
}
/// Public accessors
impl SplitNode {
/// Returns a commitment to this Split node.
///
/// The commitment is computed as a hash of the `on_true` and `on_false` child nodes in the
/// domain defined by [Self::DOMAIN] - i..e,:
/// ```
/// # use miden_core::mast::SplitNode;
/// # use miden_crypto::{hash::rpo::{RpoDigest as Digest, Rpo256 as Hasher}};
/// # let on_true_digest = Digest::default();
/// # let on_false_digest = Digest::default();
/// Hasher::merge_in_domain(&[on_true_digest, on_false_digest], SplitNode::DOMAIN);
/// ```
pub fn digest(&self) -> RpoDigest {
self.digest
}
/// Returns the ID of the node which is to be executed if the top of the stack is `1`.
pub fn on_true(&self) -> MastNodeId {
self.branches[0]
}
/// Returns the ID of the node which is to be executed if the top of the stack is `0`.
pub fn on_false(&self) -> MastNodeId {
self.branches[1]
}
}
// PRETTY PRINTING
// ================================================================================================
impl SplitNode {
pub(super) fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> impl fmt::Display + 'a {
SplitNodePrettyPrint { split_node: self, mast_forest }
}
pub(super) fn to_pretty_print<'a>(
&'a self,
mast_forest: &'a MastForest,
) -> impl PrettyPrint + 'a {
SplitNodePrettyPrint { split_node: self, mast_forest }
}
}
struct SplitNodePrettyPrint<'a> {
split_node: &'a SplitNode,
mast_forest: &'a MastForest,
}
impl<'a> PrettyPrint for SplitNodePrettyPrint<'a> {
#[rustfmt::skip]
fn render(&self) -> crate::prettier::Document {
use crate::prettier::*;
let true_branch = self.mast_forest[self.split_node.on_true()].to_pretty_print(self.mast_forest);
let false_branch = self.mast_forest[self.split_node.on_false()].to_pretty_print(self.mast_forest);
let mut doc = indent(4, const_text("if.true") + nl() + true_branch.render()) + nl();
doc += indent(4, const_text("else") + nl() + false_branch.render());
doc + nl() + const_text("end")
}
}
impl<'a> fmt::Display for SplitNodePrettyPrint<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use crate::prettier::PrettyPrint;
self.pretty_print(f)
}
}