o1vm/pickles/
column_env.rs

1use ark_ff::FftField;
2use ark_poly::{Evaluations, Radix2EvaluationDomain};
3use kimchi_msm::columns::Column;
4
5use crate::{
6    interpreters::mips::column::{N_MIPS_SEL_COLS, SCRATCH_SIZE, SCRATCH_SIZE_INVERSE},
7    pickles::proof::WitnessColumns,
8};
9use kimchi::circuits::{
10    berkeley_columns::{BerkeleyChallengeTerm, BerkeleyChallenges},
11    domains::{Domain, EvaluationDomains},
12    expr::{ColumnEnvironment as TColumnEnvironment, Constants},
13};
14
15type Evals<F> = Evaluations<F, Radix2EvaluationDomain<F>>;
16
17#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
18pub enum RelationColumnType {
19    Scratch(usize),
20    ScratchInverse(usize),
21    LookupState(usize),
22    InstructionCounter,
23    Error,
24}
25
26/// The collection of polynomials (all in evaluation form) and constants
27/// required to evaluate an expression as a polynomial.
28///
29/// All are evaluations.
30pub struct ColumnEnvironment<'a, F: FftField> {
31    /// The witness column polynomials. Includes relation columns and dynamic
32    /// selector columns.
33    pub witness: &'a WitnessColumns<Evals<F>, [Evals<F>; N_MIPS_SEL_COLS]>,
34    /// The value `prod_{j != 1} (1 - ω^j)`, used for efficiently
35    /// computing the evaluations of the unnormalized Lagrange basis
36    /// polynomials.
37    pub l0_1: F,
38    /// Constant values required
39    pub constants: Constants<F>,
40    /// Challenges from the IOP.
41    // FIXME: change for other challenges
42    pub challenges: BerkeleyChallenges<F>,
43    /// The domains used in the PLONK argument.
44    pub domain: EvaluationDomains<F>,
45}
46
47pub fn get_all_columns(num_lookup_columns: usize) -> Vec<Column<RelationColumnType>> {
48    let mut cols = Vec::<Column<RelationColumnType>>::with_capacity(
49        SCRATCH_SIZE + SCRATCH_SIZE_INVERSE + num_lookup_columns + 2 + N_MIPS_SEL_COLS,
50    );
51    for i in 0..SCRATCH_SIZE {
52        cols.push(Column::Relation(RelationColumnType::Scratch(i)));
53    }
54    for i in 0..SCRATCH_SIZE_INVERSE {
55        cols.push(Column::Relation(RelationColumnType::ScratchInverse(i)));
56    }
57    for i in 0..num_lookup_columns {
58        cols.push(Column::Relation(RelationColumnType::LookupState(i)));
59    }
60    cols.push(Column::Relation(RelationColumnType::InstructionCounter));
61    cols.push(Column::Relation(RelationColumnType::Error));
62    for i in 0..N_MIPS_SEL_COLS {
63        cols.push(Column::DynamicSelector(i));
64    }
65    cols
66}
67
68impl<G> WitnessColumns<G, [G; N_MIPS_SEL_COLS]> {
69    pub fn get_column(&self, col: &Column<RelationColumnType>) -> Option<&G> {
70        match *col {
71            Column::Relation(i) => match i {
72                RelationColumnType::Scratch(i) => Some(&self.scratch[i]),
73                RelationColumnType::ScratchInverse(i) => Some(&self.scratch_inverse[i]),
74                RelationColumnType::LookupState(i) => Some(&self.lookup_state[i]),
75                RelationColumnType::InstructionCounter => Some(&self.instruction_counter),
76                RelationColumnType::Error => Some(&self.error),
77            },
78            Column::DynamicSelector(i) => {
79                assert!(
80                    i < N_MIPS_SEL_COLS,
81                    "We do not have that many dynamic selector columns. We have {} columns and index {} was given",
82                    N_MIPS_SEL_COLS,
83                    i
84                );
85                let res = &self.selector[i];
86                Some(res)
87            }
88            _ => {
89                panic!(
90                    "We should not have any other type of columns. The column {:?} was given",
91                    col
92                );
93            }
94        }
95    }
96}
97
98impl<'a, F: FftField> TColumnEnvironment<'a, F, BerkeleyChallengeTerm, BerkeleyChallenges<F>>
99    for ColumnEnvironment<'a, F>
100{
101    type Column = Column<RelationColumnType>;
102
103    fn get_column(&self, col: &Self::Column) -> Option<&'a Evals<F>> {
104        self.witness.get_column(col)
105    }
106
107    fn get_domain(&self, d: Domain) -> Radix2EvaluationDomain<F> {
108        match d {
109            Domain::D1 => self.domain.d1,
110            Domain::D2 => self.domain.d2,
111            Domain::D4 => self.domain.d4,
112            Domain::D8 => self.domain.d8,
113        }
114    }
115
116    fn column_domain(&self, _col: &Self::Column) -> Domain {
117        Domain::D8
118    }
119
120    fn get_constants(&self) -> &Constants<F> {
121        &self.constants
122    }
123
124    fn get_challenges(&self) -> &BerkeleyChallenges<F> {
125        &self.challenges
126    }
127
128    fn vanishes_on_zero_knowledge_and_previous_rows(
129        &self,
130    ) -> &'a Evaluations<F, Radix2EvaluationDomain<F>> {
131        panic!("Not supposed to be used in MIPS. We do not support zero-knowledge for now")
132    }
133
134    fn l0_1(&self) -> F {
135        self.l0_1
136    }
137}