openvm_circuit_primitives/
chip.rs

1use std::any::Any;
2
3use openvm_stark_backend::prover::{AirProvingContext, ProverBackend};
4
5/// A chip is a [ProverBackend]-specific object that converts execution logs (also referred to as
6/// records) into a trace matrix.
7///
8/// A chip may be stateful and store state on either host or device, although it is preferred that
9/// all state is received through records.
10pub trait Chip<R, PB: ProverBackend> {
11    /// Generate all necessary context for proving a single AIR.
12    fn generate_proving_ctx(&self, records: R) -> AirProvingContext<PB>;
13
14    /// If this chip always produces a trace with a fixed number of rows (independent of execution),
15    /// return that height. Used by metered execution to avoid resetting constant-height chips
16    /// on segment boundaries.
17    fn constant_trace_height(&self) -> Option<usize> {
18        None
19    }
20}
21
22/// Auto-implemented trait for downcasting of trait objects.
23pub trait AnyChip<R, PB: ProverBackend>: Chip<R, PB> {
24    fn as_any(&self) -> &dyn Any;
25}
26
27impl<R, PB: ProverBackend, C: Chip<R, PB> + 'static> AnyChip<R, PB> for C {
28    fn as_any(&self) -> &dyn Any {
29        self
30    }
31}
32
33impl<R, PB: ProverBackend, C: Chip<R, PB> + ?Sized> Chip<R, PB> for std::sync::Arc<C> {
34    fn generate_proving_ctx(&self, records: R) -> AirProvingContext<PB> {
35        (**self).generate_proving_ctx(records)
36    }
37
38    fn constant_trace_height(&self) -> Option<usize> {
39        (**self).constant_trace_height()
40    }
41}