Skip to main content

zk_scribble/
target.rs

1use hekate_core::trace::ColumnTrace;
2use hekate_math::TowerField;
3use hekate_program::ProgramWitness;
4
5/// Selects which trace
6/// a mutation operates on.
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8pub enum Target {
9    Main,
10    Chiplet(usize),
11}
12
13impl Target {
14    /// Borrows the underlying
15    /// [`ColumnTrace`] this target selects.
16    ///
17    /// # Panics
18    ///
19    /// Panics if `Chiplet(idx)` is out of
20    /// range for the witness's chiplet table.
21    pub fn resolve_mut<'w, F: TowerField>(
22        &self,
23        witness: &'w mut ProgramWitness<F, ColumnTrace>,
24    ) -> &'w mut ColumnTrace {
25        match self {
26            Self::Main => &mut witness.trace,
27            Self::Chiplet(idx) => {
28                let n = witness.chiplet_traces.len();
29                witness.chiplet_traces.get_mut(*idx).unwrap_or_else(|| {
30                    panic!("Target::Chiplet({idx}) out of bounds (witness has {n} chiplet traces)")
31                })
32            }
33        }
34    }
35
36    /// Immutable counterpart to
37    /// [`resolve_mut`](Self::resolve_mut).
38    ///
39    /// # Panics
40    ///
41    /// Panics if `Chiplet(idx)` is out of
42    /// range for the witness's chiplet table.
43    pub fn resolve<'w, F: TowerField>(
44        &self,
45        witness: &'w ProgramWitness<F, ColumnTrace>,
46    ) -> &'w ColumnTrace {
47        match self {
48            Self::Main => &witness.trace,
49            Self::Chiplet(idx) => {
50                let n = witness.chiplet_traces.len();
51                witness.chiplet_traces.get(*idx).unwrap_or_else(|| {
52                    panic!("Target::Chiplet({idx}) out of bounds (witness has {n} chiplet traces)")
53                })
54            }
55        }
56    }
57}