o1vm/interpreters/riscv32im/
column.rs

1use super::{
2    interpreter::{
3        IInstruction,
4        Instruction::{self, IType, MType, RType, SBType, SType, SyscallType, UJType, UType},
5        RInstruction, SBInstruction, SInstruction, SyscallInstruction, UInstruction, UJInstruction,
6    },
7    INSTRUCTION_SET_SIZE, SCRATCH_SIZE, SCRATCH_SIZE_INVERSE,
8};
9use kimchi::circuits::{
10    berkeley_columns::BerkeleyChallengeTerm,
11    expr::{ConstantExpr, Expr},
12};
13use strum::EnumCount;
14
15#[derive(Clone, Debug, PartialEq, Eq)]
16pub enum Column {
17    ScratchState(usize),
18    ScratchStateInverse(usize),
19    InstructionCounter,
20    Selector(usize),
21}
22
23impl From<Column> for usize {
24    fn from(col: Column) -> usize {
25        match col {
26            Column::ScratchState(i) => {
27                assert!(i < SCRATCH_SIZE);
28                i
29            }
30            Column::ScratchStateInverse(i) => {
31                assert!(i < SCRATCH_SIZE_INVERSE);
32                SCRATCH_SIZE + i
33            }
34            Column::InstructionCounter => SCRATCH_SIZE + SCRATCH_SIZE_INVERSE,
35            Column::Selector(s) => {
36                assert!(
37                    s < INSTRUCTION_SET_SIZE,
38                    "There is only {INSTRUCTION_SET_SIZE}"
39                );
40                SCRATCH_SIZE + SCRATCH_SIZE_INVERSE + 1 + s
41            }
42        }
43    }
44}
45
46impl From<Instruction> for usize {
47    fn from(instr: Instruction) -> usize {
48        match instr {
49            RType(rtype) => SCRATCH_SIZE + SCRATCH_SIZE_INVERSE + 1 + rtype as usize,
50            IType(itype) => {
51                SCRATCH_SIZE + SCRATCH_SIZE_INVERSE + 1 + RInstruction::COUNT + itype as usize
52            }
53            SType(stype) => {
54                SCRATCH_SIZE
55                    + SCRATCH_SIZE_INVERSE
56                    + 1
57                    + RInstruction::COUNT
58                    + IInstruction::COUNT
59                    + stype as usize
60            }
61            SBType(sbtype) => {
62                SCRATCH_SIZE
63                    + SCRATCH_SIZE_INVERSE
64                    + 1
65                    + RInstruction::COUNT
66                    + IInstruction::COUNT
67                    + SInstruction::COUNT
68                    + sbtype as usize
69            }
70            UType(utype) => {
71                SCRATCH_SIZE
72                    + SCRATCH_SIZE_INVERSE
73                    + 1
74                    + RInstruction::COUNT
75                    + IInstruction::COUNT
76                    + SInstruction::COUNT
77                    + SBInstruction::COUNT
78                    + utype as usize
79            }
80            UJType(ujtype) => {
81                SCRATCH_SIZE
82                    + SCRATCH_SIZE_INVERSE
83                    + 1
84                    + RInstruction::COUNT
85                    + IInstruction::COUNT
86                    + SInstruction::COUNT
87                    + SBInstruction::COUNT
88                    + UInstruction::COUNT
89                    + ujtype as usize
90            }
91            SyscallType(syscalltype) => {
92                SCRATCH_SIZE
93                    + SCRATCH_SIZE_INVERSE
94                    + 1
95                    + RInstruction::COUNT
96                    + IInstruction::COUNT
97                    + SInstruction::COUNT
98                    + SBInstruction::COUNT
99                    + UInstruction::COUNT
100                    + UJInstruction::COUNT
101                    + syscalltype as usize
102            }
103            MType(mtype) => {
104                SCRATCH_SIZE
105                    + SCRATCH_SIZE_INVERSE
106                    + 1
107                    + RInstruction::COUNT
108                    + IInstruction::COUNT
109                    + SInstruction::COUNT
110                    + SBInstruction::COUNT
111                    + UInstruction::COUNT
112                    + UJInstruction::COUNT
113                    + SyscallInstruction::COUNT
114                    + mtype as usize
115            }
116        }
117    }
118}
119
120// FIXME: use other challenges, not Berkeley.
121pub type E<F> = Expr<ConstantExpr<F, BerkeleyChallengeTerm>, Column>;