1use alloc::{boxed::Box, vec::Vec};
2use core::fmt;
3
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7use super::{MastForestContributor, MastNodeExt};
8#[cfg(debug_assertions)]
9use crate::mast::MastNode;
10use crate::{
11 Felt, Word,
12 chiplets::hasher,
13 mast::{
14 DecoratorId, DecoratorStore, MastForest, MastForestError, MastNodeFingerprint, MastNodeId,
15 },
16 operations::opcodes,
17 prettier::PrettyPrint,
18 utils::{Idx, LookupByIdx},
19};
20
21#[derive(Debug, Clone, PartialEq, Eq)]
31#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
32#[cfg_attr(all(feature = "arbitrary", test), miden_test_serde_macros::serde_test)]
33pub struct SplitNode {
34 branches: [MastNodeId; 2],
35 digest: Word,
36 decorator_store: DecoratorStore,
37}
38
39impl SplitNode {
41 pub const DOMAIN: Felt = Felt::new_unchecked(opcodes::SPLIT as u64);
43}
44
45impl SplitNode {
47 pub fn on_true(&self) -> MastNodeId {
49 self.branches[0]
50 }
51
52 pub fn on_false(&self) -> MastNodeId {
54 self.branches[1]
55 }
56}
57
58impl SplitNode {
62 pub(super) fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> impl fmt::Display + 'a {
63 SplitNodePrettyPrint { split_node: self, mast_forest }
64 }
65
66 pub(super) fn to_pretty_print<'a>(
67 &'a self,
68 mast_forest: &'a MastForest,
69 ) -> impl PrettyPrint + 'a {
70 SplitNodePrettyPrint { split_node: self, mast_forest }
71 }
72}
73
74struct SplitNodePrettyPrint<'a> {
75 split_node: &'a SplitNode,
76 mast_forest: &'a MastForest,
77}
78
79impl PrettyPrint for SplitNodePrettyPrint<'_> {
80 #[rustfmt::skip]
81 fn render(&self) -> crate::prettier::Document {
82 use crate::prettier::*;
83
84 let pre_decorators = {
85 let mut pre_decorators = self
86 .split_node
87 .before_enter(self.mast_forest)
88 .iter()
89 .map(|&decorator_id| self.mast_forest[decorator_id].render())
90 .reduce(|acc, doc| acc + const_text(" ") + doc)
91 .unwrap_or_default();
92 if !pre_decorators.is_empty() {
93 pre_decorators += nl();
94 }
95
96 pre_decorators
97 };
98
99 let post_decorators = {
100 let mut post_decorators = self
101 .split_node
102 .after_exit(self.mast_forest)
103 .iter()
104 .map(|&decorator_id| self.mast_forest[decorator_id].render())
105 .reduce(|acc, doc| acc + const_text(" ") + doc)
106 .unwrap_or_default();
107 if !post_decorators.is_empty() {
108 post_decorators = nl() + post_decorators;
109 }
110
111 post_decorators
112 };
113
114 let true_branch = self.mast_forest[self.split_node.on_true()].to_pretty_print(self.mast_forest);
115 let false_branch = self.mast_forest[self.split_node.on_false()].to_pretty_print(self.mast_forest);
116
117 let mut doc = pre_decorators;
118 doc += indent(4, const_text("if.true") + nl() + true_branch.render()) + nl();
119 doc += indent(4, const_text("else") + nl() + false_branch.render());
120 doc += nl() + const_text("end");
121 doc + post_decorators
122 }
123}
124
125impl fmt::Display for SplitNodePrettyPrint<'_> {
126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127 use crate::prettier::PrettyPrint;
128 self.pretty_print(f)
129 }
130}
131
132impl MastNodeExt for SplitNode {
136 fn digest(&self) -> Word {
148 self.digest
149 }
150
151 fn before_enter<'a>(&'a self, forest: &'a MastForest) -> &'a [DecoratorId] {
153 #[cfg(debug_assertions)]
154 self.verify_node_in_forest(forest);
155 self.decorator_store.before_enter(forest)
156 }
157
158 fn after_exit<'a>(&'a self, forest: &'a MastForest) -> &'a [DecoratorId] {
160 #[cfg(debug_assertions)]
161 self.verify_node_in_forest(forest);
162 self.decorator_store.after_exit(forest)
163 }
164
165 fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> Box<dyn fmt::Display + 'a> {
166 Box::new(SplitNode::to_display(self, mast_forest))
167 }
168
169 fn to_pretty_print<'a>(&'a self, mast_forest: &'a MastForest) -> Box<dyn PrettyPrint + 'a> {
170 Box::new(SplitNode::to_pretty_print(self, mast_forest))
171 }
172
173 fn has_children(&self) -> bool {
174 true
175 }
176
177 fn append_children_to(&self, target: &mut Vec<MastNodeId>) {
178 target.push(self.on_true());
179 target.push(self.on_false());
180 }
181
182 fn for_each_child<F>(&self, mut f: F)
183 where
184 F: FnMut(MastNodeId),
185 {
186 f(self.on_true());
187 f(self.on_false());
188 }
189
190 fn domain(&self) -> Felt {
191 Self::DOMAIN
192 }
193
194 type Builder = SplitNodeBuilder;
195
196 fn to_builder(self, forest: &MastForest) -> Self::Builder {
197 match self.decorator_store {
199 DecoratorStore::Owned { before_enter, after_exit, .. } => {
200 let mut builder = SplitNodeBuilder::new(self.branches);
201 builder = builder.with_before_enter(before_enter).with_after_exit(after_exit);
202 builder
203 },
204 DecoratorStore::Linked { id } => {
205 let before_enter = forest.before_enter_decorators(id).to_vec();
207 let after_exit = forest.after_exit_decorators(id).to_vec();
208 let mut builder = SplitNodeBuilder::new(self.branches);
209 builder = builder.with_before_enter(before_enter).with_after_exit(after_exit);
210 builder
211 },
212 }
213 }
214
215 #[cfg(debug_assertions)]
216 fn verify_node_in_forest(&self, forest: &MastForest) {
217 if let Some(id) = self.decorator_store.linked_id() {
218 let self_ptr = self as *const Self;
220 let forest_node = &forest.nodes[id];
221 let forest_node_ptr = match forest_node {
222 MastNode::Split(split_node) => split_node as *const SplitNode as *const (),
223 _ => panic!("Node type mismatch at {id:?}"),
224 };
225 let self_as_void = self_ptr as *const ();
226 debug_assert_eq!(
227 self_as_void, forest_node_ptr,
228 "Node pointer mismatch: expected node at {id:?} to be self"
229 );
230 }
231 }
232}
233
234#[cfg(all(feature = "arbitrary", test))]
238impl proptest::prelude::Arbitrary for SplitNode {
239 type Parameters = ();
240
241 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
242 use proptest::prelude::*;
243
244 use crate::Felt;
245
246 (any::<MastNodeId>(), any::<MastNodeId>(), any::<[u64; 4]>())
248 .prop_map(|(true_branch, false_branch, digest_array)| {
249 let digest = Word::from(digest_array.map(Felt::new_unchecked));
251 SplitNode {
253 branches: [true_branch, false_branch],
254 digest,
255 decorator_store: DecoratorStore::default(),
256 }
257 })
258 .no_shrink() .boxed()
260 }
261
262 type Strategy = proptest::prelude::BoxedStrategy<Self>;
263}
264
265#[derive(Debug)]
268pub struct SplitNodeBuilder {
269 branches: [MastNodeId; 2],
270 before_enter: Vec<DecoratorId>,
271 after_exit: Vec<DecoratorId>,
272 digest: Option<Word>,
273}
274
275impl SplitNodeBuilder {
276 pub fn new(branches: [MastNodeId; 2]) -> Self {
278 Self {
279 branches,
280 before_enter: Vec::new(),
281 after_exit: Vec::new(),
282 digest: None,
283 }
284 }
285
286 pub fn build(self, mast_forest: &MastForest) -> Result<SplitNode, MastForestError> {
288 let forest_len = mast_forest.nodes.len();
289 if self.branches[0].to_usize() >= forest_len {
290 return Err(MastForestError::NodeIdOverflow(self.branches[0], forest_len));
291 } else if self.branches[1].to_usize() >= forest_len {
292 return Err(MastForestError::NodeIdOverflow(self.branches[1], forest_len));
293 }
294
295 let digest = if let Some(forced_digest) = self.digest {
297 forced_digest
298 } else {
299 let true_branch_hash = mast_forest[self.branches[0]].digest();
300 let false_branch_hash = mast_forest[self.branches[1]].digest();
301
302 hasher::merge_in_domain(&[true_branch_hash, false_branch_hash], SplitNode::DOMAIN)
303 };
304
305 Ok(SplitNode {
306 branches: self.branches,
307 digest,
308 decorator_store: DecoratorStore::new_owned_with_decorators(
309 self.before_enter,
310 self.after_exit,
311 ),
312 })
313 }
314}
315
316impl MastForestContributor for SplitNodeBuilder {
317 fn add_to_forest(self, forest: &mut MastForest) -> Result<MastNodeId, MastForestError> {
318 let forest_len = forest.nodes.len();
320 if self.branches[0].to_usize() >= forest_len {
321 return Err(MastForestError::NodeIdOverflow(self.branches[0], forest_len));
322 } else if self.branches[1].to_usize() >= forest_len {
323 return Err(MastForestError::NodeIdOverflow(self.branches[1], forest_len));
324 }
325
326 let digest = if let Some(forced_digest) = self.digest {
328 forced_digest
329 } else {
330 let true_branch_hash = forest[self.branches[0]].digest();
331 let false_branch_hash = forest[self.branches[1]].digest();
332
333 hasher::merge_in_domain(&[true_branch_hash, false_branch_hash], SplitNode::DOMAIN)
334 };
335
336 let future_node_id = MastNodeId::new_unchecked(forest.nodes.len() as u32);
338
339 forest.register_node_decorators(future_node_id, &self.before_enter, &self.after_exit);
341
342 let node_id = forest
345 .nodes
346 .push(
347 SplitNode {
348 branches: self.branches,
349 digest,
350 decorator_store: DecoratorStore::Linked { id: future_node_id },
351 }
352 .into(),
353 )
354 .map_err(|_| MastForestError::TooManyNodes)?;
355
356 Ok(node_id)
357 }
358
359 fn fingerprint_for_node(
360 &self,
361 forest: &MastForest,
362 hash_by_node_id: &impl LookupByIdx<MastNodeId, MastNodeFingerprint>,
363 ) -> Result<MastNodeFingerprint, MastForestError> {
364 crate::mast::node_fingerprint::fingerprint_from_parts(
366 forest,
367 hash_by_node_id,
368 &self.before_enter,
369 &self.after_exit,
370 &self.branches,
371 if let Some(forced_digest) = self.digest {
373 forced_digest
374 } else {
375 let if_branch_hash = forest[self.branches[0]].digest();
376 let else_branch_hash = forest[self.branches[1]].digest();
377
378 hasher::merge_in_domain(&[if_branch_hash, else_branch_hash], SplitNode::DOMAIN)
379 },
380 )
381 }
382
383 fn remap_children(self, remapping: &impl LookupByIdx<MastNodeId, MastNodeId>) -> Self {
384 SplitNodeBuilder {
385 branches: [
386 *remapping.get(self.branches[0]).unwrap_or(&self.branches[0]),
387 *remapping.get(self.branches[1]).unwrap_or(&self.branches[1]),
388 ],
389 before_enter: self.before_enter,
390 after_exit: self.after_exit,
391 digest: self.digest,
392 }
393 }
394
395 fn with_before_enter(mut self, decorators: impl Into<Vec<DecoratorId>>) -> Self {
396 self.before_enter = decorators.into();
397 self
398 }
399
400 fn with_after_exit(mut self, decorators: impl Into<Vec<DecoratorId>>) -> Self {
401 self.after_exit = decorators.into();
402 self
403 }
404
405 fn append_before_enter(&mut self, decorators: impl IntoIterator<Item = DecoratorId>) {
406 self.before_enter.extend(decorators);
407 }
408
409 fn append_after_exit(&mut self, decorators: impl IntoIterator<Item = DecoratorId>) {
410 self.after_exit.extend(decorators);
411 }
412
413 fn with_digest(mut self, digest: Word) -> Self {
414 self.digest = Some(digest);
415 self
416 }
417}
418
419impl SplitNodeBuilder {
420 pub(in crate::mast) fn add_to_forest_relaxed(
430 self,
431 forest: &mut MastForest,
432 ) -> Result<MastNodeId, MastForestError> {
433 let Some(digest) = self.digest else {
436 return Err(MastForestError::DigestRequiredForDeserialization);
437 };
438
439 let future_node_id = MastNodeId::new_unchecked(forest.nodes.len() as u32);
440
441 let node_id = forest
444 .nodes
445 .push(
446 SplitNode {
447 branches: self.branches,
448 digest,
449 decorator_store: DecoratorStore::Linked { id: future_node_id },
450 }
451 .into(),
452 )
453 .map_err(|_| MastForestError::TooManyNodes)?;
454
455 Ok(node_id)
456 }
457}
458
459#[cfg(any(test, feature = "arbitrary"))]
460impl proptest::prelude::Arbitrary for SplitNodeBuilder {
461 type Parameters = SplitNodeBuilderParams;
462 type Strategy = proptest::strategy::BoxedStrategy<Self>;
463
464 fn arbitrary_with(params: Self::Parameters) -> Self::Strategy {
465 use proptest::prelude::*;
466
467 (
468 any::<[MastNodeId; 2]>(),
469 proptest::collection::vec(
470 super::arbitrary::decorator_id_strategy(params.max_decorator_id_u32),
471 0..=params.max_decorators,
472 ),
473 proptest::collection::vec(
474 super::arbitrary::decorator_id_strategy(params.max_decorator_id_u32),
475 0..=params.max_decorators,
476 ),
477 )
478 .prop_map(|(branches, before_enter, after_exit)| {
479 Self::new(branches).with_before_enter(before_enter).with_after_exit(after_exit)
480 })
481 .boxed()
482 }
483}
484
485#[cfg(any(test, feature = "arbitrary"))]
487#[derive(Clone, Debug)]
488pub struct SplitNodeBuilderParams {
489 pub max_decorators: usize,
490 pub max_decorator_id_u32: u32,
491}
492
493#[cfg(any(test, feature = "arbitrary"))]
494impl Default for SplitNodeBuilderParams {
495 fn default() -> Self {
496 Self {
497 max_decorators: 4,
498 max_decorator_id_u32: 10,
499 }
500 }
501}