Source code for zisk/state-machines/arith/src/arith.rs
1//! The `ArithSM` module implements the Arithmetic State Machine,
2//! coordinating sub-state machines to handle various arithmetic operations seamlessly.
3//!
4//! Key components of this module include:
5//! - The `ArithSM` struct, encapsulating the full, table, and range table state machines.
6//! - `ComponentBuilder` trait implementations for creating counters, planners, input collectors,
7//! and input generators specific to arithmetic computations.
8
9use std::sync::Arc;
10
11use fields::PrimeField64;
12use pil_std_lib::Std;
13use zisk_common::{
14 BusDevice, BusDeviceMetrics, BusDeviceMode, ComponentBuilder, Instance, InstanceCtx,
15 InstanceInfo, PayloadType, Planner,
16};
17use zisk_core::ZiskOperationType;
18use zisk_pil::ArithTrace;
19
20use crate::{ArithCounterInputGen, ArithFullInstance, ArithFullSM, ArithPlanner};
21
22/// The `ArithSM` struct represents the Arithmetic State Machine, which
23/// is a proxy machine to manage state machines involved in arithmetic operations.
24pub struct ArithSM<F: PrimeField64> {
25 /// Arith Full state machine
26 arith_full_sm: Arc<ArithFullSM<F>>,
27}
28
29impl<F: PrimeField64> ArithSM<F> {
30 /// Creates a new instance of the `ArithSM` state machine.
31 ///
32 /// # Returns
33 /// An `Arc`-wrapped instance of `ArithSM` containing initialized sub-state machines.
34 pub fn new(std: Arc<Std<F>>) -> Arc<Self> {
35 let arith_full_sm = ArithFullSM::new(std);
36
37 Arc::new(Self { arith_full_sm })
38 }
39
40 pub fn build_arith_counter(&self) -> ArithCounterInputGen {
41 ArithCounterInputGen::new(BusDeviceMode::Counter)
42 }
46 }
47}
48
49impl<F: PrimeField64> ComponentBuilder<F> for ArithSM<F> {
50 /// Builds and returns a new counter for monitoring arithmetic operations.
51 ///
52 /// # Returns
53 /// A boxed implementation of `ArithCounter`.
54 fn build_counter(&self) -> Option<Box<dyn BusDeviceMetrics>> {
55 Some(Box::new(ArithCounterInputGen::new(BusDeviceMode::Counter)))
56 }
57
58 /// Builds a planner to plan arithmetic-related instances.
59 ///
60 /// # Returns
61 /// A boxed implementation of `ArithPlanner`.
62 fn build_planner(&self) -> Box<dyn Planner> {
63 Box::new(ArithPlanner::new().add_instance(InstanceInfo::new(
64 ArithTrace::<F>::AIRGROUP_ID,
65 ArithTrace::<F>::AIR_ID,
66 ArithTrace::<F>::NUM_ROWS,
67 ZiskOperationType::Arith,
68 )))
69 }
70
71 /// Builds an instance of the Arithmetic state machine.
72 ///
73 /// # Arguments
74 /// * `ictx` - The context of the instance, containing the plan and its associated
75 ///
76 /// # Returns
77 /// A boxed implementation of `StdInstance`.
78 fn build_instance(&self, ictx: InstanceCtx) -> Box<dyn Instance<F>> {
79 match ictx.plan.air_id {
80 ArithTrace::<F>::AIR_ID => {
81 Box::new(ArithFullInstance::new(self.arith_full_sm.clone(), ictx))
82 }
83 _ => panic!("BinarySM::get_instance() Unsupported air_id: {:?}", ictx.plan.air_id),
84 }
85 }
86
87 /// Creates and returns an input generator for arithmetic state machine computations.
88 ///
89 /// # Returns
90 /// A boxed implementation of `ArithInputGenerator`.
91 fn build_inputs_generator(&self) -> Option<Box<dyn BusDevice<PayloadType>>> {
92 Some(Box::new(ArithCounterInputGen::new(BusDeviceMode::InputGenerator)))
93 }
94}