miden_core/events/sys_events.rs
1use core::fmt;
2
3use super::{EventId, EventName};
4
5// SYSTEM EVENTS
6// ================================================================================================
7
8/// Defines a set of actions which can be initiated from the VM to inject new data into the advice
9/// provider.
10///
11/// These actions can affect all 3 components of the advice provider: Merkle store, advice stack,
12/// and advice map.
13///
14/// All actions, except for `MerkleNodeMerge`, `Ext2Inv` and `UpdateMerkleNode` can be invoked
15/// directly from Miden assembly via dedicated instructions.
16///
17/// System event IDs are derived from blake3-hashing their names (prefixed with "sys::").
18///
19/// The enum variant order matches the indices in SYSTEM_EVENT_LOOKUP, allowing efficient const
20/// lookup via `to_event_id()`. The discriminants are implicitly 0, 1, 2, ... 15.
21#[derive(Copy, Clone, Debug, Eq, PartialEq)]
22#[repr(u8)]
23pub enum SystemEvent {
24 // MERKLE STORE EVENTS
25 // --------------------------------------------------------------------------------------------
26 /// Creates a new Merkle tree in the advice provider by combining Merkle trees with the
27 /// specified roots. The root of the new tree is defined as `Hash(LEFT_ROOT, RIGHT_ROOT)`.
28 ///
29 /// Inputs:
30 /// Operand stack: [LEFT_ROOT, RIGHT_ROOT, ...]
31 /// Merkle store: {LEFT_ROOT, RIGHT_ROOT}
32 ///
33 /// Outputs:
34 /// Operand stack: [LEFT_ROOT, RIGHT_ROOT, ...]
35 /// Merkle store: {LEFT_ROOT, RIGHT_ROOT, hash(LEFT_ROOT, RIGHT_ROOT)}
36 ///
37 /// After the operation, both the original trees and the new tree remains in the advice
38 /// provider (i.e., the input trees are not removed).
39 MerkleNodeMerge,
40
41 // ADVICE STACK SYSTEM EVENTS
42 // --------------------------------------------------------------------------------------------
43 /// Pushes a node of the Merkle tree specified by the values on the top of the operand stack
44 /// onto the advice stack in structural order for consumption by `AdvPopW`.
45 ///
46 /// Inputs:
47 /// Operand stack: [depth, index, TREE_ROOT, ...]
48 /// Advice stack: [...]
49 /// Merkle store: {TREE_ROOT<-NODE}
50 ///
51 /// Outputs:
52 /// Operand stack: [depth, index, TREE_ROOT, ...]
53 /// Advice stack: [NODE, ...]
54 /// Merkle store: {TREE_ROOT<-NODE}
55 MerkleNodeToStack,
56
57 /// Pushes a list of field elements onto the advice stack. The list is looked up in the advice
58 /// map using the specified word from the operand stack as the key.
59 ///
60 /// Inputs:
61 /// Operand stack: [KEY, ...]
62 /// Advice stack: [...]
63 /// Advice map: {KEY: values}
64 ///
65 /// Outputs:
66 /// Operand stack: [KEY, ...]
67 /// Advice stack: [values, ...]
68 /// Advice map: {KEY: values}
69 MapValueToStack,
70
71 /// Pushes the number of elements in a list of field elements onto the advice stack. The list is
72 /// looked up in the advice map using the specified word from the operand stack as the key.
73 ///
74 /// Inputs:
75 /// Operand stack: [KEY, ...]
76 /// Advice stack: [...]
77 /// Advice map: {KEY: values}
78 ///
79 /// Outputs:
80 /// Operand stack: [KEY, ...]
81 /// Advice stack: [values.len(), ...]
82 /// Advice map: {KEY: values}
83 MapValueCountToStack,
84
85 /// Pushes a list of field elements onto the advice stack, along with the number of elements in
86 /// that list. The list is looked up in the advice map using the word at the top of the operand
87 /// stack as the key.
88 ///
89 /// Notice that the resulting elements list is not padded.
90 ///
91 /// Inputs:
92 /// Operand stack: [KEY, ...]
93 /// Advice stack: [...]
94 /// Advice map: {KEY: values}
95 ///
96 /// Outputs:
97 /// Operand stack: [KEY, ...]
98 /// Advice stack: [num_values, values, ...]
99 /// Advice map: {KEY: values}
100 MapValueToStackN0,
101
102 /// Pushes a padded list of field elements onto the advice stack, along with the number of
103 /// elements in that list. The list is looked up in the advice map using the word at the top of
104 /// the operand stack as the key.
105 ///
106 /// Notice that the elements list obtained from the advice map will be padded with zeros,
107 /// increasing its length to the next multiple of 4.
108 ///
109 /// Inputs:
110 /// Operand stack: [KEY, ...]
111 /// Advice stack: [...]
112 /// Advice map: {KEY: values}
113 ///
114 /// Outputs:
115 /// Operand stack: [KEY, ...]
116 /// Advice stack: [num_values, values, padding, ...]
117 /// Advice map: {KEY: values}
118 MapValueToStackN4,
119
120 /// Pushes a padded list of field elements onto the advice stack, along with the number of
121 /// elements in that list. The list is looked up in the advice map using the word at the top of
122 /// the operand stack as the key.
123 ///
124 /// Notice that the elements list obtained from the advice map will be padded with zeros,
125 /// increasing its length to the next multiple of 8.
126 ///
127 /// Inputs:
128 /// Operand stack: [KEY, ...]
129 /// Advice stack: [...]
130 /// Advice map: {KEY: values}
131 ///
132 /// Outputs:
133 /// Operand stack: [KEY, ...]
134 /// Advice stack: [num_values, values, padding, ...]
135 /// Advice map: {KEY: values}
136 MapValueToStackN8,
137
138 /// Pushes a flag onto the advice stack whether advice map has an entry with specified key.
139 ///
140 /// If the advice map has the entry with the key equal to the key placed at the top of the
141 /// operand stack, `1` will be pushed to the advice stack and `0` otherwise.
142 ///
143 /// Inputs:
144 /// Operand stack: [KEY, ...]
145 /// Advice stack: [...]
146 ///
147 /// Outputs:
148 /// Operand stack: [KEY, ...]
149 /// Advice stack: [has_mapkey, ...]
150 HasMapKey,
151
152 /// Given an element in a quadratic extension field on the top of the stack (i.e., a0, b1),
153 /// computes its multiplicative inverse and push the result onto the advice stack.
154 ///
155 /// Inputs:
156 /// Operand stack: [a1, a0, ...]
157 /// Advice stack: [...]
158 ///
159 /// Outputs:
160 /// Operand stack: [a1, a0, ...]
161 /// Advice stack: [b0, b1...]
162 ///
163 /// Where (b0, b1) is the multiplicative inverse of the extension field element (a0, a1) at the
164 /// top of the stack.
165 Ext2Inv,
166
167 /// Pushes the number of the leading zeros of the top stack element onto the advice stack.
168 ///
169 /// Inputs:
170 /// Operand stack: [n, ...]
171 /// Advice stack: [...]
172 ///
173 /// Outputs:
174 /// Operand stack: [n, ...]
175 /// Advice stack: [leading_zeros, ...]
176 U32Clz,
177
178 /// Pushes the number of the trailing zeros of the top stack element onto the advice stack.
179 ///
180 /// Inputs:
181 /// Operand stack: [n, ...]
182 /// Advice stack: [...]
183 ///
184 /// Outputs:
185 /// Operand stack: [n, ...]
186 /// Advice stack: [trailing_zeros, ...]
187 U32Ctz,
188
189 /// Pushes the number of the leading ones of the top stack element onto the advice stack.
190 ///
191 /// Inputs:
192 /// Operand stack: [n, ...]
193 /// Advice stack: [...]
194 ///
195 /// Outputs:
196 /// Operand stack: [n, ...]
197 /// Advice stack: [leading_ones, ...]
198 U32Clo,
199
200 /// Pushes the number of the trailing ones of the top stack element onto the advice stack.
201 ///
202 /// Inputs:
203 /// Operand stack: [n, ...]
204 /// Advice stack: [...]
205 ///
206 /// Outputs:
207 /// Operand stack: [n, ...]
208 /// Advice stack: [trailing_ones, ...]
209 U32Cto,
210
211 /// Pushes the base 2 logarithm of the top stack element, rounded down.
212 /// Inputs:
213 /// Operand stack: [n, ...]
214 /// Advice stack: [...]
215 ///
216 /// Outputs:
217 /// Operand stack: [n, ...]
218 /// Advice stack: [ilog2(n), ...]
219 ILog2,
220
221 // ADVICE MAP SYSTEM EVENTS
222 // --------------------------------------------------------------------------------------------
223 /// Reads words from memory at the specified range and inserts them into the advice map under
224 /// the key `KEY` located at the top of the stack.
225 ///
226 /// Inputs:
227 /// Operand stack: [KEY, start_addr, end_addr, ...]
228 /// Advice map: {...}
229 ///
230 /// Outputs:
231 /// Operand stack: [KEY, start_addr, end_addr, ...]
232 /// Advice map: {KEY: values}
233 ///
234 /// Where `values` are the elements located in memory[start_addr..end_addr].
235 MemToMap,
236
237 /// Reads two word from the operand stack and inserts them into the advice map under the key
238 /// defined by the hash of these words.
239 ///
240 /// Inputs:
241 /// Operand stack: [A, B, ...]
242 /// Advice map: {...}
243 ///
244 /// Outputs:
245 /// Operand stack: [A, B, ...]
246 /// Advice map: {KEY: [a0, a1, a2, a3, b0, b1, b2, b3]}
247 ///
248 /// Where KEY is computed as hash(A || B, domain=0).
249 HdwordToMap,
250
251 /// Reads two words from the operand stack and inserts them into the advice map under the key
252 /// defined by the hash of these words (using `d` as the domain).
253 ///
254 /// Inputs:
255 /// Operand stack: [A, B, d, ...]
256 /// Advice map: {...}
257 ///
258 /// Outputs:
259 /// Operand stack: [A, B, d, ...]
260 /// Advice map: {KEY: [a0, a1, a2, a3, b0, b1, b2, b3]}
261 ///
262 /// Where KEY is computed as hash(A || B, d).
263 HdwordToMapWithDomain,
264
265 /// Reads four words from the operand stack and inserts them into the advice map under the key
266 /// defined by the hash of these words.
267 ///
268 /// Inputs:
269 /// Operand stack: [A, B, C, D, ...]
270 /// Advice map: {...}
271 ///
272 /// Outputs:
273 /// Operand stack: [A, B, C, D, ...]
274 /// Advice map: {KEY: [A, B, C, D]} (16 elements)
275 ///
276 /// Where:
277 /// - KEY is computed as hash_elements([A, B, C, D]) using the sponge construction (sequential
278 /// absorption; two rounds for four words).
279 HqwordToMap,
280
281 /// Reads three words from the operand stack and inserts the top two words into the advice map
282 /// under the key defined by applying a Poseidon2 permutation to all three words.
283 ///
284 /// Inputs:
285 /// Operand stack: [A, B, C, ...]
286 /// Advice map: {...}
287 ///
288 /// Outputs:
289 /// Operand stack: [A, B, C, ...]
290 /// Advice map: {KEY: [a0, a1, a2, a3, b0, b1, b2, b3]}
291 ///
292 /// Where KEY is computed by extracting the digest elements from hperm([C, A, B]). For example,
293 /// if C is [0, d, 0, 0], KEY will be set as hash(A || B, d).
294 HpermToMap,
295}
296
297impl SystemEvent {
298 /// Attempts to convert an EventId into a SystemEvent by looking it up in the const table.
299 ///
300 /// Returns `Some(SystemEvent)` if the ID matches a known system event, `None` otherwise.
301 /// This uses a const lookup table with hardcoded EventIds, avoiding runtime hash computation.
302 pub const fn from_event_id(event_id: EventId) -> Option<Self> {
303 let lookup = Self::LOOKUP;
304 let mut i = 0;
305 while i < lookup.len() {
306 if lookup[i].id.as_u64() == event_id.as_u64() {
307 return Some(lookup[i].event);
308 }
309 i += 1;
310 }
311 None
312 }
313
314 /// Attempts to convert a name into a SystemEvent by looking it up in the const table.
315 ///
316 /// Returns `Some(SystemEvent)` if the name matches a known system event, `None` otherwise.
317 /// This uses const string comparison against the lookup table.
318 pub const fn from_name(name: &str) -> Option<Self> {
319 let lookup = Self::LOOKUP;
320 let mut i = 0;
321 while i < lookup.len() {
322 if str_eq(name, lookup[i].name) {
323 return Some(lookup[i].event);
324 }
325 i += 1;
326 }
327 None
328 }
329
330 /// Returns the human-readable name of this system event as an [`EventName`].
331 ///
332 /// System event names are prefixed with `sys::` to distinguish them from user-defined events.
333 pub const fn event_name(&self) -> EventName {
334 EventName::new(Self::LOOKUP[*self as usize].name)
335 }
336
337 /// Returns the [`EventId`] for this system event.
338 ///
339 /// The ID is looked up from the const LOOKUP table using the enum's discriminant
340 /// as the index. The discriminants are explicitly set to match the array indices.
341 pub const fn event_id(&self) -> EventId {
342 Self::LOOKUP[*self as usize].id
343 }
344
345 /// Returns an array of all system event variants.
346 pub const fn all() -> [Self; Self::COUNT] {
347 [
348 Self::MerkleNodeMerge,
349 Self::MerkleNodeToStack,
350 Self::MapValueToStack,
351 Self::MapValueCountToStack,
352 Self::MapValueToStackN0,
353 Self::MapValueToStackN4,
354 Self::MapValueToStackN8,
355 Self::HasMapKey,
356 Self::Ext2Inv,
357 Self::U32Clz,
358 Self::U32Ctz,
359 Self::U32Clo,
360 Self::U32Cto,
361 Self::ILog2,
362 Self::MemToMap,
363 Self::HdwordToMap,
364 Self::HdwordToMapWithDomain,
365 Self::HqwordToMap,
366 Self::HpermToMap,
367 ]
368 }
369}
370
371impl From<SystemEvent> for EventName {
372 fn from(system_event: SystemEvent) -> Self {
373 system_event.event_name()
374 }
375}
376
377impl crate::prettier::PrettyPrint for SystemEvent {
378 fn render(&self) -> crate::prettier::Document {
379 crate::prettier::display(self)
380 }
381}
382
383impl fmt::Display for SystemEvent {
384 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
385 const PREFIX_LEN: usize = "sys::".len();
386
387 let (_prefix, rest) = Self::LOOKUP[*self as usize].name.split_at(PREFIX_LEN);
388 write!(f, "{rest}")
389 }
390}
391
392// LOOKUP TABLE
393// ================================================================================================
394
395/// An entry in the system event lookup table, containing all metadata for a system event.
396#[derive(Copy, Clone, Debug)]
397pub(crate) struct SystemEventEntry {
398 /// The unique event ID (hash of the name)
399 pub id: EventId,
400 /// The system event variant
401 pub event: SystemEvent,
402 /// The full event name string (e.g., "sys::merkle_node_merge")
403 pub name: &'static str,
404}
405
406impl SystemEvent {
407 /// The total number of system events.
408 pub const COUNT: usize = 19;
409
410 /// Lookup table mapping system events to their metadata.
411 ///
412 /// The enum variant order matches the indices in this table, allowing efficient const
413 /// lookup via array indexing using discriminants.
414 const LOOKUP: [SystemEventEntry; Self::COUNT] = [
415 SystemEventEntry {
416 id: EventId::from_u64(7243907139105902342),
417 event: SystemEvent::MerkleNodeMerge,
418 name: "sys::merkle_node_merge",
419 },
420 SystemEventEntry {
421 id: EventId::from_u64(6873007751276594108),
422 event: SystemEvent::MerkleNodeToStack,
423 name: "sys::merkle_node_to_stack",
424 },
425 SystemEventEntry {
426 id: EventId::from_u64(17843484659000820118),
427 event: SystemEvent::MapValueToStack,
428 name: "sys::map_value_to_stack",
429 },
430 SystemEventEntry {
431 id: EventId::from_u64(3470274154276391308),
432 event: SystemEvent::MapValueCountToStack,
433 name: "sys::map_value_count_to_stack",
434 },
435 SystemEventEntry {
436 id: EventId::from_u64(11775886982554463322),
437 event: SystemEvent::MapValueToStackN0,
438 name: "sys::map_value_to_stack_n_0",
439 },
440 SystemEventEntry {
441 id: EventId::from_u64(3443305460233942990),
442 event: SystemEvent::MapValueToStackN4,
443 name: "sys::map_value_to_stack_n_4",
444 },
445 SystemEventEntry {
446 id: EventId::from_u64(1741586542981559489),
447 event: SystemEvent::MapValueToStackN8,
448 name: "sys::map_value_to_stack_n_8",
449 },
450 SystemEventEntry {
451 id: EventId::from_u64(5642583036089175977),
452 event: SystemEvent::HasMapKey,
453 name: "sys::has_map_key",
454 },
455 SystemEventEntry {
456 id: EventId::from_u64(9660728691489438960),
457 event: SystemEvent::Ext2Inv,
458 name: "sys::ext2_inv",
459 },
460 SystemEventEntry {
461 id: EventId::from_u64(1503707361178382932),
462 event: SystemEvent::U32Clz,
463 name: "sys::u32_clz",
464 },
465 SystemEventEntry {
466 id: EventId::from_u64(10656887096526143429),
467 event: SystemEvent::U32Ctz,
468 name: "sys::u32_ctz",
469 },
470 SystemEventEntry {
471 id: EventId::from_u64(12846584985739176048),
472 event: SystemEvent::U32Clo,
473 name: "sys::u32_clo",
474 },
475 SystemEventEntry {
476 id: EventId::from_u64(6773574803673468616),
477 event: SystemEvent::U32Cto,
478 name: "sys::u32_cto",
479 },
480 SystemEventEntry {
481 id: EventId::from_u64(7444351342957461231),
482 event: SystemEvent::ILog2,
483 name: "sys::ilog2",
484 },
485 SystemEventEntry {
486 id: EventId::from_u64(5768534446586058686),
487 event: SystemEvent::MemToMap,
488 name: "sys::mem_to_map",
489 },
490 SystemEventEntry {
491 id: EventId::from_u64(5988159172915333521),
492 event: SystemEvent::HdwordToMap,
493 name: "sys::hdword_to_map",
494 },
495 SystemEventEntry {
496 id: EventId::from_u64(6143777601072385586),
497 event: SystemEvent::HdwordToMapWithDomain,
498 name: "sys::hdword_to_map_with_domain",
499 },
500 SystemEventEntry {
501 id: EventId::from_u64(11723176702659679401),
502 event: SystemEvent::HqwordToMap,
503 name: "sys::hqword_to_map",
504 },
505 SystemEventEntry {
506 id: EventId::from_u64(6190830263511605775),
507 event: SystemEvent::HpermToMap,
508 name: "sys::hperm_to_map",
509 },
510 ];
511}
512
513// HELPERS
514// ================================================================================================
515
516/// Const-compatible string equality check.
517const fn str_eq(a: &str, b: &str) -> bool {
518 let a_bytes = a.as_bytes();
519 let b_bytes = b.as_bytes();
520
521 if a_bytes.len() != b_bytes.len() {
522 return false;
523 }
524
525 let mut i = 0;
526 while i < a_bytes.len() {
527 if a_bytes[i] != b_bytes[i] {
528 return false;
529 }
530 i += 1;
531 }
532 true
533}
534
535#[cfg(test)]
536mod test {
537 use super::*;
538
539 #[test]
540 fn test_system_events() {
541 // Comprehensive test verifying consistency between SystemEvent::all() and
542 // SystemEvent::LOOKUP. This ensures all() and LOOKUP are in sync, lookup table has
543 // correct IDs/names, and all variants are covered.
544
545 // Verify lengths match COUNT
546 assert_eq!(SystemEvent::all().len(), SystemEvent::COUNT);
547 assert_eq!(SystemEvent::LOOKUP.len(), SystemEvent::COUNT);
548
549 // Iterate through both all() and LOOKUP together, checking all invariants
550 for (i, (event, entry)) in
551 SystemEvent::all().iter().zip(SystemEvent::LOOKUP.iter()).enumerate()
552 {
553 // Verify LOOKUP entry matches the event at the same index
554 assert_eq!(
555 entry.event, *event,
556 "LOOKUP[{}].event ({:?}) doesn't match all()[{}] ({:?})",
557 i, entry.event, i, event
558 );
559
560 // Verify LOOKUP entry ID matches the computed ID
561 let computed_id = event.event_id();
562 assert_eq!(
563 entry.id,
564 computed_id,
565 "LOOKUP[{}].id is EventId::from_u64({}), but {:?}.to_event_id() returns EventId::from_u64({})",
566 i,
567 entry.id.as_u64(),
568 event,
569 computed_id.as_u64()
570 );
571
572 // Verify name has correct "sys::" prefix
573 assert!(
574 entry.name.starts_with("sys::"),
575 "SystemEvent name should start with 'sys::': {}",
576 entry.name
577 );
578
579 // Verify from_event_id lookup works
580 let looked_up =
581 SystemEvent::from_event_id(entry.id).expect("SystemEvent should be found by ID");
582 assert_eq!(looked_up, *event);
583
584 // Verify from_name lookup works
585 let looked_up_by_name =
586 SystemEvent::from_name(entry.name).expect("SystemEvent should be found by name");
587 assert_eq!(looked_up_by_name, *event);
588
589 // Verify EventName conversion works
590 let event_name = event.event_name();
591 assert_eq!(event_name.as_str(), entry.name);
592 assert!(SystemEvent::from_name(event_name.as_str()).is_some());
593 let event_name_from_into: EventName = (*event).into();
594 assert_eq!(event_name_from_into.as_str(), entry.name);
595 assert!(SystemEvent::from_name(event_name_from_into.as_str()).is_some());
596
597 // Exhaustive match to ensure compile-time error when adding new variants
598 match event {
599 SystemEvent::MerkleNodeMerge
600 | SystemEvent::MerkleNodeToStack
601 | SystemEvent::MapValueToStack
602 | SystemEvent::MapValueCountToStack
603 | SystemEvent::MapValueToStackN0
604 | SystemEvent::MapValueToStackN4
605 | SystemEvent::MapValueToStackN8
606 | SystemEvent::HasMapKey
607 | SystemEvent::Ext2Inv
608 | SystemEvent::U32Clz
609 | SystemEvent::U32Ctz
610 | SystemEvent::U32Clo
611 | SystemEvent::U32Cto
612 | SystemEvent::ILog2
613 | SystemEvent::MemToMap
614 | SystemEvent::HdwordToMap
615 | SystemEvent::HdwordToMapWithDomain
616 | SystemEvent::HqwordToMap
617 | SystemEvent::HpermToMap => {},
618 }
619 }
620 }
621}