Skip to main content

sp1_core_machine/control_flow/branch/
columns.rs

1use sp1_derive::AlignedBorrow;
2use std::mem::size_of;
3use struct_reflection::{StructReflection, StructReflectionHelper};
4
5use crate::{
6    adapter::{register::i_type::ITypeReader, state::CPUState},
7    operations::LtOperationSigned,
8    SupervisorMode, TrustMode, UserMode,
9};
10
11/// The number of main trace columns for `BranchChip` in Supervisor mode.
12pub const NUM_BRANCH_COLS_SUPERVISOR: usize = size_of::<BranchColumns<u8, SupervisorMode>>();
13/// The number of main trace columns for `BranchChip` in User mode.
14pub const NUM_BRANCH_COLS_USER: usize = size_of::<BranchColumns<u8, UserMode>>();
15
16/// The column layout for branching.
17#[derive(AlignedBorrow, Default, Debug, Clone, Copy, StructReflection)]
18#[repr(C)]
19pub struct BranchColumns<T, M: TrustMode> {
20    /// The current shard, timestamp, program counter of the CPU.
21    pub state: CPUState<T>,
22
23    /// The adapter to read program and register information.
24    pub adapter: ITypeReader<T>,
25
26    /// The next program counter.
27    pub next_pc: [T; 3],
28
29    /// Branch Instructions.
30    pub is_beq: T,
31    pub is_bne: T,
32    pub is_blt: T,
33    pub is_bge: T,
34    pub is_bltu: T,
35    pub is_bgeu: T,
36
37    /// The is_branching column is equal to:
38    ///
39    /// > is_beq & a_eq_b ||
40    /// > is_bne & (a_lt_b | a_gt_b) ||
41    /// > (is_blt | is_bltu) & a_lt_b ||
42    /// > (is_bge | is_bgeu) & (a_eq_b | a_gt_b)
43    pub is_branching: T,
44
45    /// The comparison between `a` and `b`.
46    pub compare_operation: LtOperationSigned<T>,
47
48    /// Adapter columns for trust mode specific data.
49    pub adapter_cols: M::AdapterCols<T>,
50}