use alloc::{boxed::Box, vec::Vec};
use core::fmt;
use miden_crypto::{Felt, Word};
use miden_formatting::prettier::PrettyPrint;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::{MastNodeErrorContext, MastNodeExt};
use crate::{
Idx, OPCODE_SPLIT,
chiplets::hasher,
mast::{DecoratedOpLink, DecoratorId, MastForest, MastForestError, MastNodeId, Remapping},
};
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SplitNode {
branches: [MastNodeId; 2],
digest: Word,
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Vec::is_empty"))]
before_enter: Vec<DecoratorId>,
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Vec::is_empty"))]
after_exit: Vec<DecoratorId>,
}
impl SplitNode {
pub const DOMAIN: Felt = Felt::new(OPCODE_SPLIT as u64);
}
impl SplitNode {
pub fn new(
branches: [MastNodeId; 2],
mast_forest: &MastForest,
) -> Result<Self, MastForestError> {
let forest_len = mast_forest.nodes.len();
if branches[0].to_usize() >= forest_len {
return Err(MastForestError::NodeIdOverflow(branches[0], forest_len));
} else if branches[1].to_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,
before_enter: Vec::new(),
after_exit: Vec::new(),
})
}
pub fn new_unsafe(branches: [MastNodeId; 2], digest: Word) -> Self {
Self {
branches,
digest,
before_enter: Vec::new(),
after_exit: Vec::new(),
}
}
}
impl SplitNode {
pub fn on_true(&self) -> MastNodeId {
self.branches[0]
}
pub fn on_false(&self) -> MastNodeId {
self.branches[1]
}
}
impl MastNodeErrorContext for SplitNode {
fn decorators(&self) -> impl Iterator<Item = DecoratedOpLink> {
self.before_enter.iter().chain(&self.after_exit).copied().enumerate()
}
}
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 PrettyPrint for SplitNodePrettyPrint<'_> {
#[rustfmt::skip]
fn render(&self) -> crate::prettier::Document {
use crate::prettier::*;
let pre_decorators = {
let mut pre_decorators = self
.split_node
.before_enter()
.iter()
.map(|&decorator_id| self.mast_forest[decorator_id].render())
.reduce(|acc, doc| acc + const_text(" ") + doc)
.unwrap_or_default();
if !pre_decorators.is_empty() {
pre_decorators += nl();
}
pre_decorators
};
let post_decorators = {
let mut post_decorators = self
.split_node
.after_exit()
.iter()
.map(|&decorator_id| self.mast_forest[decorator_id].render())
.reduce(|acc, doc| acc + const_text(" ") + doc)
.unwrap_or_default();
if !post_decorators.is_empty() {
post_decorators = nl() + post_decorators;
}
post_decorators
};
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 = pre_decorators;
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");
doc + post_decorators
}
}
impl fmt::Display for SplitNodePrettyPrint<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use crate::prettier::PrettyPrint;
self.pretty_print(f)
}
}
impl MastNodeExt for SplitNode {
fn digest(&self) -> Word {
self.digest
}
fn before_enter(&self) -> &[DecoratorId] {
&self.before_enter
}
fn after_exit(&self) -> &[DecoratorId] {
&self.after_exit
}
fn append_before_enter(&mut self, decorator_ids: &[DecoratorId]) {
self.before_enter.extend_from_slice(decorator_ids);
}
fn append_after_exit(&mut self, decorator_ids: &[DecoratorId]) {
self.after_exit.extend_from_slice(decorator_ids);
}
fn remove_decorators(&mut self) {
self.before_enter.truncate(0);
self.after_exit.truncate(0);
}
fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> Box<dyn fmt::Display + 'a> {
Box::new(SplitNode::to_display(self, mast_forest))
}
fn to_pretty_print<'a>(&'a self, mast_forest: &'a MastForest) -> Box<dyn PrettyPrint + 'a> {
Box::new(SplitNode::to_pretty_print(self, mast_forest))
}
fn remap_children(&self, remapping: &Remapping) -> Self {
let mut node = self.clone();
node.branches[0] = node.branches[0].remap(remapping);
node.branches[1] = node.branches[1].remap(remapping);
node
}
fn has_children(&self) -> bool {
true
}
fn append_children_to(&self, target: &mut Vec<MastNodeId>) {
target.push(self.on_true());
target.push(self.on_false());
}
fn for_each_child<F>(&self, mut f: F)
where
F: FnMut(MastNodeId),
{
f(self.on_true());
f(self.on_false());
}
fn domain(&self) -> Felt {
Self::DOMAIN
}
}