1use std::{marker::PhantomData, sync::Arc};
4
5use binius_core::{oracle::ShiftVariant, polynomial::MultivariatePoly};
6use binius_field::{ExtensionField, TowerField};
7use binius_math::ArithCircuit;
8
9use super::{structured::StructuredDynSize, table::TableId, types::B128};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
13pub(crate) struct ColumnIndex(pub(crate) usize);
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub(crate) struct ColumnPartitionIndex(pub(crate) usize);
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub struct Col<F: TowerField, const VALUES_PER_ROW: usize = 1> {
27 pub table_id: TableId,
28 pub(crate) table_index: ColumnIndex,
29 pub(crate) partition_index: ColumnPartitionIndex,
32 _marker: PhantomData<F>,
33}
34
35impl<F: TowerField, const VALUES_PER_ROW: usize> Col<F, VALUES_PER_ROW> {
36 pub(super) fn new(id: ColumnId, partition_index: ColumnPartitionIndex) -> Self {
41 assert!(VALUES_PER_ROW.is_power_of_two());
42 Self {
43 table_id: id.table_id,
44 table_index: id.table_index,
45 partition_index,
46 _marker: PhantomData,
47 }
48 }
49
50 pub fn shape(&self) -> ColumnShape {
51 ColumnShape {
52 tower_height: F::TOWER_LEVEL,
53 log_values_per_row: VALUES_PER_ROW.ilog2() as usize,
54 }
55 }
56
57 pub fn id(&self) -> ColumnId {
58 ColumnId {
59 table_id: self.table_id,
60 table_index: self.table_index,
61 }
62 }
63}
64
65pub fn upcast_col<F, FSub, const V: usize>(col: Col<FSub, V>) -> Col<F, V>
67where
68 FSub: TowerField,
69 F: TowerField + ExtensionField<FSub>,
70{
71 let Col {
72 table_id,
73 table_index,
74 partition_index,
75 _marker: _,
76 } = col;
77 Col {
79 table_id,
80 table_index,
81 partition_index,
82 _marker: PhantomData,
83 }
84}
85
86#[derive(Debug)]
88pub struct ColumnInfo<F: TowerField = B128> {
89 pub id: ColumnId,
90 pub col: ColumnDef<F>,
91 pub name: String,
92 pub shape: ColumnShape,
93 pub is_nonzero: bool,
95}
96
97#[derive(Debug, Clone, Copy)]
99pub struct ColumnShape {
100 pub tower_height: usize,
102 pub log_values_per_row: usize,
104}
105
106impl ColumnShape {
107 pub fn log_cell_size(&self) -> usize {
109 self.tower_height + self.log_values_per_row
110 }
111}
112
113#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
118pub struct ColumnId {
119 pub(crate) table_id: TableId,
120 pub(crate) table_index: ColumnIndex,
121}
122
123#[derive(Debug)]
125pub enum ColumnDef<F: TowerField = B128> {
126 Committed {
127 tower_level: usize,
128 },
129 Selected {
130 col: ColumnId,
131 index: usize,
132 index_bits: usize,
133 },
134 Projected {
135 col: ColumnId,
136 start_index: usize,
137 query_size: usize,
138 query_bits: usize,
139 },
140 ZeroPadded {
141 col: ColumnId,
142 n_pad_vars: usize,
143 start_index: usize,
144 nonzero_index: usize,
145 },
146 Shifted {
147 col: ColumnId,
148 offset: usize,
149 log_block_size: usize,
150 variant: ShiftVariant,
151 },
152 Packed {
153 col: ColumnId,
154 log_degree: usize,
155 },
156 Computed {
157 cols: Vec<ColumnId>,
158 expr: ArithCircuit<F>,
159 },
160 Constant {
161 poly: Arc<dyn MultivariatePoly<F>>,
162 data: Vec<F>,
163 },
164 StructuredDynSize(StructuredDynSize),
165 StructuredFixedSize {
166 expr: ArithCircuit<F>,
167 },
168 StaticExp {
169 bit_cols: Vec<ColumnId>,
170 base: F,
171 base_tower_level: usize,
172 },
173 DynamicExp {
174 bit_cols: Vec<ColumnId>,
175 base: ColumnId,
176 base_tower_level: usize,
177 },
178}