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
use core::fmt;
use miden_crypto::{hash::rpo::RpoDigest, Felt};
use miden_formatting::prettier::PrettyPrint;
use crate::{
chiplets::hasher,
mast::{MastForest, MastForestError, MastNodeId},
OPCODE_CALL, OPCODE_SYSCALL,
};
// CALL NODE
// ================================================================================================
/// A Call node describes a function call such that the callee is executed in a different execution
/// context from the currently executing code.
///
/// A call node can be of two types:
/// - A simple call: the callee is executed in the new user context.
/// - A syscall: the callee is executed in the root context.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CallNode {
callee: MastNodeId,
is_syscall: bool,
digest: RpoDigest,
}
//-------------------------------------------------------------------------------------------------
/// Constants
impl CallNode {
/// The domain of the call block (used for control block hashing).
pub const CALL_DOMAIN: Felt = Felt::new(OPCODE_CALL as u64);
/// The domain of the syscall block (used for control block hashing).
pub const SYSCALL_DOMAIN: Felt = Felt::new(OPCODE_SYSCALL as u64);
}
//-------------------------------------------------------------------------------------------------
/// Constructors
impl CallNode {
/// Returns a new [`CallNode`] instantiated with the specified callee.
pub fn new(callee: MastNodeId, mast_forest: &MastForest) -> Result<Self, MastForestError> {
if callee.as_usize() >= mast_forest.nodes.len() {
return Err(MastForestError::NodeIdOverflow(callee, mast_forest.nodes.len()));
}
let digest = {
let callee_digest = mast_forest[callee].digest();
hasher::merge_in_domain(&[callee_digest, RpoDigest::default()], Self::CALL_DOMAIN)
};
Ok(Self { callee, is_syscall: false, digest })
}
/// Returns a new [`CallNode`] 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(callee: MastNodeId, digest: RpoDigest) -> Self {
Self { callee, is_syscall: false, digest }
}
/// Returns a new [`CallNode`] instantiated with the specified callee and marked as a kernel
/// call.
pub fn new_syscall(
callee: MastNodeId,
mast_forest: &MastForest,
) -> Result<Self, MastForestError> {
if callee.as_usize() >= mast_forest.nodes.len() {
return Err(MastForestError::NodeIdOverflow(callee, mast_forest.nodes.len()));
}
let digest = {
let callee_digest = mast_forest[callee].digest();
hasher::merge_in_domain(&[callee_digest, RpoDigest::default()], Self::SYSCALL_DOMAIN)
};
Ok(Self { callee, is_syscall: true, digest })
}
/// Returns a new syscall [`CallNode`] 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_syscall_unsafe(callee: MastNodeId, digest: RpoDigest) -> Self {
Self { callee, is_syscall: true, digest }
}
}
//-------------------------------------------------------------------------------------------------
/// Public accessors
impl CallNode {
/// Returns a commitment to this Call node.
///
/// The commitment is computed as a hash of the callee and an empty word ([ZERO; 4]) in the
/// domain defined by either [Self::CALL_DOMAIN] or [Self::SYSCALL_DOMAIN], depending on
/// whether the node represents a simple call or a syscall - i.e.,:
/// ```
/// # use miden_core::mast::CallNode;
/// # use miden_crypto::{hash::rpo::{RpoDigest as Digest, Rpo256 as Hasher}};
/// # let callee_digest = Digest::default();
/// Hasher::merge_in_domain(&[callee_digest, Digest::default()], CallNode::CALL_DOMAIN);
/// ```
/// or
/// ```
/// # use miden_core::mast::CallNode;
/// # use miden_crypto::{hash::rpo::{RpoDigest as Digest, Rpo256 as Hasher}};
/// # let callee_digest = Digest::default();
/// Hasher::merge_in_domain(&[callee_digest, Digest::default()], CallNode::SYSCALL_DOMAIN);
/// ```
pub fn digest(&self) -> RpoDigest {
self.digest
}
/// Returns the ID of the node to be invoked by this call node.
pub fn callee(&self) -> MastNodeId {
self.callee
}
/// Returns true if this call node represents a syscall.
pub fn is_syscall(&self) -> bool {
self.is_syscall
}
/// Returns the domain of this call node.
pub fn domain(&self) -> Felt {
if self.is_syscall() {
Self::SYSCALL_DOMAIN
} else {
Self::CALL_DOMAIN
}
}
}
// PRETTY PRINTING
// ================================================================================================
impl CallNode {
pub(super) fn to_pretty_print<'a>(
&'a self,
mast_forest: &'a MastForest,
) -> impl PrettyPrint + 'a {
CallNodePrettyPrint { call_node: self, mast_forest }
}
pub(super) fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> impl fmt::Display + 'a {
CallNodePrettyPrint { call_node: self, mast_forest }
}
}
struct CallNodePrettyPrint<'a> {
call_node: &'a CallNode,
mast_forest: &'a MastForest,
}
impl<'a> PrettyPrint for CallNodePrettyPrint<'a> {
#[rustfmt::skip]
fn render(&self) -> crate::prettier::Document {
use crate::prettier::*;
use miden_formatting::hex::ToHex;
let callee_digest = self.mast_forest[self.call_node.callee].digest();
let doc = if self.call_node.is_syscall {
const_text("syscall")
} else {
const_text("call")
};
doc + const_text(".") + text(callee_digest.as_bytes().to_hex_with_prefix())
}
}
impl<'a> fmt::Display for CallNodePrettyPrint<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use crate::prettier::PrettyPrint;
self.pretty_print(f)
}
}