use alloc::{boxed::Box, vec::Vec};
use core::fmt;
use miden_formatting::{
hex::ToHex,
prettier::{Document, PrettyPrint, const_text, text},
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::{MastForestContributor, MastNodeExt, fingerprint_with_child_fingerprints};
use crate::{
Felt, Word,
chiplets::hasher,
mast::{MastForest, MastForestError, MastNodeId},
operations::opcodes,
utils::{Idx, LookupByIdx},
};
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(all(feature = "arbitrary", test), miden_test_serde_macros::serde_test)]
pub struct CallNode {
callee: MastNodeId,
is_syscall: bool,
digest: Word,
}
impl CallNode {
pub const CALL_DOMAIN: Felt = Felt::new_unchecked(opcodes::CALL as u64);
pub const SYSCALL_DOMAIN: Felt = Felt::new_unchecked(opcodes::SYSCALL as u64);
}
impl CallNode {
pub fn callee(&self) -> MastNodeId {
self.callee
}
pub fn is_syscall(&self) -> bool {
self.is_syscall
}
pub fn domain(&self) -> Felt {
if self.is_syscall() {
Self::SYSCALL_DOMAIN
} else {
Self::CALL_DOMAIN
}
}
}
impl CallNode {
pub(super) fn to_pretty_print<'a>(
&'a self,
mast_forest: &'a MastForest,
) -> impl PrettyPrint + 'a {
CallNodePrettyPrint { node: self, mast_forest }
}
pub(super) fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> impl fmt::Display + 'a {
CallNodePrettyPrint { node: self, mast_forest }
}
}
struct CallNodePrettyPrint<'a> {
node: &'a CallNode,
mast_forest: &'a MastForest,
}
impl PrettyPrint for CallNodePrettyPrint<'_> {
fn render(&self) -> Document {
let callee_digest = self.mast_forest[self.node.callee].digest();
if self.node.is_syscall {
const_text("syscall")
+ const_text(".")
+ text(callee_digest.as_bytes().to_hex_with_prefix())
} else {
const_text("call")
+ const_text(".")
+ text(callee_digest.as_bytes().to_hex_with_prefix())
}
}
}
impl fmt::Display for CallNodePrettyPrint<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use crate::prettier::PrettyPrint;
self.pretty_print(f)
}
}
impl MastNodeExt for CallNode {
fn digest(&self) -> Word {
self.digest
}
fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> Box<dyn fmt::Display + 'a> {
Box::new(CallNode::to_display(self, mast_forest))
}
fn to_pretty_print<'a>(&'a self, mast_forest: &'a MastForest) -> Box<dyn PrettyPrint + 'a> {
Box::new(CallNode::to_pretty_print(self, mast_forest))
}
fn has_children(&self) -> bool {
true
}
fn append_children_to(&self, target: &mut Vec<MastNodeId>) {
target.push(self.callee());
}
fn for_each_child<F>(&self, mut f: F)
where
F: FnMut(MastNodeId),
{
f(self.callee());
}
fn domain(&self) -> Felt {
self.domain()
}
type Builder = CallNodeBuilder;
fn to_builder(self, _forest: &MastForest) -> Self::Builder {
let builder = if self.is_syscall {
CallNodeBuilder::new_syscall(self.callee)
} else {
CallNodeBuilder::new(self.callee)
};
builder.with_digest(self.digest)
}
}
#[cfg(all(feature = "arbitrary", test))]
impl proptest::prelude::Arbitrary for CallNode {
type Parameters = ();
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
use proptest::prelude::*;
use crate::Felt;
(any::<MastNodeId>(), any::<[u64; 4]>(), any::<bool>())
.prop_map(|(callee, digest_array, is_syscall)| {
let digest = Word::from(digest_array.map(Felt::new_unchecked));
CallNode {
callee,
is_syscall,
digest,
}
})
.no_shrink() .boxed()
}
type Strategy = proptest::prelude::BoxedStrategy<Self>;
}
#[derive(Debug)]
pub struct CallNodeBuilder {
callee: MastNodeId,
is_syscall: bool,
digest: Option<Word>,
}
impl CallNodeBuilder {
pub fn new(callee: MastNodeId) -> Self {
Self { callee, is_syscall: false, digest: None }
}
pub fn new_syscall(callee: MastNodeId) -> Self {
Self { callee, is_syscall: true, digest: None }
}
pub fn build(self, mast_forest: &MastForest) -> Result<CallNode, MastForestError> {
if self.callee.to_usize() >= mast_forest.nodes.len() {
return Err(MastForestError::NodeIdOverflow(self.callee, mast_forest.nodes.len()));
}
let digest = if let Some(forced_digest) = self.digest {
forced_digest
} else {
let callee_digest = mast_forest[self.callee].digest();
let domain = if self.is_syscall {
CallNode::SYSCALL_DOMAIN
} else {
CallNode::CALL_DOMAIN
};
hasher::merge_in_domain(&[callee_digest, Word::default()], domain)
};
Ok(CallNode {
callee: self.callee,
is_syscall: self.is_syscall,
digest,
})
}
pub(in crate::mast) fn build_linked(self) -> Result<CallNode, MastForestError> {
Ok(CallNode {
callee: self.callee,
is_syscall: self.is_syscall,
digest: self.digest.ok_or(MastForestError::DigestRequiredForDeserialization)?,
})
}
}
impl MastForestContributor for CallNodeBuilder {
fn add_to_forest(self, forest: &mut MastForest) -> Result<MastNodeId, MastForestError> {
if self.callee.to_usize() >= forest.nodes.len() {
return Err(MastForestError::NodeIdOverflow(self.callee, forest.nodes.len()));
}
let digest = if let Some(forced_digest) = self.digest {
forced_digest
} else {
let callee_digest = forest[self.callee].digest();
let domain = if self.is_syscall {
CallNode::SYSCALL_DOMAIN
} else {
CallNode::CALL_DOMAIN
};
hasher::merge_in_domain(&[callee_digest, Word::default()], domain)
};
let node_id = forest
.nodes
.push(
CallNode {
callee: self.callee,
is_syscall: self.is_syscall,
digest,
}
.into(),
)
.map_err(|_| MastForestError::TooManyNodes)?;
Ok(node_id)
}
fn fingerprint_for_node(
&self,
forest: &MastForest,
hash_by_node_id: &impl LookupByIdx<MastNodeId, Word>,
) -> Result<Word, MastForestError> {
let node_digest = if let Some(forced_digest) = self.digest {
forced_digest
} else {
let callee_digest = forest[self.callee].digest();
let domain = if self.is_syscall {
CallNode::SYSCALL_DOMAIN
} else {
CallNode::CALL_DOMAIN
};
hasher::merge_in_domain(&[callee_digest, Word::default()], domain)
};
fingerprint_with_child_fingerprints(node_digest, &[self.callee], forest, hash_by_node_id)
}
fn remap_children(self, remapping: &impl LookupByIdx<MastNodeId, MastNodeId>) -> Self {
CallNodeBuilder {
callee: *remapping.get(self.callee).unwrap_or(&self.callee),
is_syscall: self.is_syscall,
digest: self.digest,
}
}
fn with_digest(mut self, digest: Word) -> Self {
self.digest = Some(digest);
self
}
}
impl CallNodeBuilder {
pub(in crate::mast) fn add_to_forest_relaxed(
self,
forest: &mut MastForest,
) -> Result<MastNodeId, MastForestError> {
let Some(digest) = self.digest else {
return Err(MastForestError::DigestRequiredForDeserialization);
};
let node_id = forest
.nodes
.push(
CallNode {
callee: self.callee,
is_syscall: self.is_syscall,
digest,
}
.into(),
)
.map_err(|_| MastForestError::TooManyNodes)?;
Ok(node_id)
}
}
#[cfg(any(test, feature = "arbitrary"))]
impl proptest::prelude::Arbitrary for CallNodeBuilder {
type Parameters = CallNodeBuilderParams;
type Strategy = proptest::strategy::BoxedStrategy<Self>;
fn arbitrary_with(params: Self::Parameters) -> Self::Strategy {
use proptest::prelude::*;
let _ = params;
(any::<MastNodeId>(), any::<bool>())
.prop_map(|(callee, is_syscall)| {
if is_syscall {
Self::new_syscall(callee)
} else {
Self::new(callee)
}
})
.boxed()
}
}
#[cfg(any(test, feature = "arbitrary"))]
#[derive(Clone, Debug, Default)]
pub struct CallNodeBuilderParams {}