Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inner join #71

Merged
merged 105 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 103 commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
eed044d
feat: sorted_limbs chip checking each limb less than limb_bits bits
bfan05 May 29, 2024
473670c
wip
OsamaAlkhodairy May 29, 2024
8792d55
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy May 29, 2024
023c034
feat: completed sorted_limbs chip with tests
bfan05 May 30, 2024
4c21140
Merge branch 'main' into feat/sort-limbs
bfan05 May 30, 2024
f080051
wip
OsamaAlkhodairy May 31, 2024
6a7fafd
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy May 31, 2024
a5c0982
feat: SortedLimbsChip with LessThan subchip
bfan05 May 31, 2024
06ada4b
feat: less_than subchip refactored
bfan05 May 31, 2024
0caddfd
chore: merge main
bfan05 May 31, 2024
b349571
feat: rename SortedLimbsChip to AssertSortedChip and write LessThanCh…
bfan05 May 31, 2024
dbada83
chore: change name of assert sorted chip
bfan05 May 31, 2024
b62172a
chore: fix names in tests for AssertSortedChip
bfan05 May 31, 2024
7f8daba
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy Jun 2, 2024
0cf5509
Merge branch 'main' into feat/sort-limbs
bfan05 Jun 3, 2024
451f4c4
chore: address comments
bfan05 Jun 3, 2024
d77fd08
chore: cleanup
bfan05 Jun 3, 2024
c320b27
Merge branch 'main' into feat/sort-limbs
bfan05 Jun 3, 2024
3c81318
chore: change MAX from generic to instance field for LessThanChip and…
bfan05 Jun 3, 2024
00f38a4
wip: added extra bits to middle chip trace
OsamaAlkhodairy Jun 3, 2024
1aa7cea
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy Jun 3, 2024
f0c371d
wip
OsamaAlkhodairy Jun 3, 2024
7a7cf4c
adding connection to IsEqualVec chip
OsamaAlkhodairy Jun 4, 2024
296b66f
feat: IsLessThanChip to compare two numbers
bfan05 Jun 4, 2024
c860b80
Merge branch 'main' into feat/sort-limbs
bfan05 Jun 4, 2024
3610e0d
feat: IsLessThanTuple subchip for different limb_bits
bfan05 Jun 4, 2024
8dac425
test: added tests for partially and non-allocated pages
OsamaAlkhodairy Jun 4, 2024
295b3c8
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy Jun 4, 2024
3811b1c
test: added negative tests
OsamaAlkhodairy Jun 4, 2024
b755a28
added constraints
OsamaAlkhodairy Jun 4, 2024
2ab6964
adding comments
OsamaAlkhodairy Jun 4, 2024
50d54a4
feat: IsLessThanTupleChip subchip in AssertSortedChip
bfan05 Jun 4, 2024
12949c8
renaming
OsamaAlkhodairy Jun 4, 2024
8db7282
removing TODO comments
OsamaAlkhodairy Jun 4, 2024
d35655a
Merge branch 'feat/sort-limbs' into feat/page_read_write_checker
OsamaAlkhodairy Jun 4, 2024
1748721
fixing clippy
OsamaAlkhodairy Jun 4, 2024
9fe511a
chore: renaming to idx and data
OsamaAlkhodairy Jun 5, 2024
d0ae2cf
chore: address comments first pass
bfan05 Jun 5, 2024
81494d9
Merge branch 'main' into feat/sort-limbs
bfan05 Jun 5, 2024
0099324
chore: moving page_controller inside page_read
OsamaAlkhodairy Jun 5, 2024
664ca0f
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy Jun 5, 2024
6ca339f
chore: refactor AssertSorted, IsEqual, IsLessThan, and IsLessThanTupl…
bfan05 Jun 5, 2024
aef744a
chore: address comments
bfan05 Jun 5, 2024
38d5dec
chore: eliminate high dim poly from IsLessThanTupleChip
bfan05 Jun 5, 2024
d76a94d
chore: fix tests
bfan05 Jun 5, 2024
eca7ef3
chore: address comments for AssertSortedChip
bfan05 Jun 6, 2024
4890798
chore: cleanup AssertSorted
bfan05 Jun 6, 2024
a086260
chore: cleanup
bfan05 Jun 6, 2024
6896610
chore: include roundtrip flatten and from_slice tests
bfan05 Jun 6, 2024
7c23b03
feat: flatten and from_slice for IO and Aux columns
bfan05 Jun 6, 2024
4c392c1
Merge remote-tracking branch 'origin/feat/sort-limbs' into feat/page_…
OsamaAlkhodairy Jun 6, 2024
981341b
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy Jun 7, 2024
b77f738
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy Jun 7, 2024
3fdbf91
wip: final page chip
OsamaAlkhodairy Jun 7, 2024
e45e2df
remove txt file
OsamaAlkhodairy Jun 7, 2024
816829a
feat: final_page_chip
OsamaAlkhodairy Jun 8, 2024
6179c0c
feat: integrating all chips for page_read_write
OsamaAlkhodairy Jun 10, 2024
dbc423d
fixing lib
OsamaAlkhodairy Jun 10, 2024
eb057dc
using field bits()
OsamaAlkhodairy Jun 10, 2024
0ad01f4
Merge branch 'main' into feat/page_read_write_checker
jonathanpwang Jun 10, 2024
f1e85da
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy Jun 10, 2024
58ae266
adding extra communication between checker and final chip
OsamaAlkhodairy Jun 10, 2024
b6fec02
muting clippy
OsamaAlkhodairy Jun 10, 2024
e3b7015
renaming to AIR
OsamaAlkhodairy Jun 10, 2024
15c91f0
adding comments
OsamaAlkhodairy Jun 10, 2024
490d282
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy Jun 10, 2024
7cecdd1
comment fix
OsamaAlkhodairy Jun 11, 2024
e5ccb12
optimization to one less bus
OsamaAlkhodairy Jun 11, 2024
42a8cd1
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy Jun 11, 2024
75bda9d
comments
OsamaAlkhodairy Jun 12, 2024
87a859b
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy Jun 12, 2024
e715723
moving old page_controller inside page_read
OsamaAlkhodairy Jun 12, 2024
674f8f8
updateing comment
OsamaAlkhodairy Jun 12, 2024
229abbd
wip: inner join
OsamaAlkhodairy Jun 13, 2024
80af93e
feat: more general FinalPageAir
OsamaAlkhodairy Jun 13, 2024
9875aa2
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy Jun 13, 2024
bd84538
making IsEqualVecAuxCols clonable
OsamaAlkhodairy Jun 13, 2024
775dde6
enforcing unallocated rows to be zero
OsamaAlkhodairy Jun 13, 2024
8153534
Merge branch 'main' into feat/page_read_write_checker
OsamaAlkhodairy Jun 13, 2024
d4729e1
aligning with new less than chip
OsamaAlkhodairy Jun 13, 2024
31ca8b4
Merge branch 'feat/page_read_write_checker' into feat/inner_join
OsamaAlkhodairy Jun 13, 2024
fa81ed0
wip
OsamaAlkhodairy Jun 13, 2024
6bc34a2
using Air::eval instead of SubAir::eval
OsamaAlkhodairy Jun 13, 2024
1aaaa52
fix comment
OsamaAlkhodairy Jun 13, 2024
6bc6818
Merge branch 'feat/page_read_write_checker' into feat/inner_join
OsamaAlkhodairy Jun 13, 2024
c94da13
wip
OsamaAlkhodairy Jun 13, 2024
6265949
removed arguments from load_page for readability
OsamaAlkhodairy Jun 13, 2024
1685371
addressing comments and adding common/ directory with Page struct
OsamaAlkhodairy Jun 14, 2024
500cc67
Merge branch 'feat/page_read_write_checker' into feat/inner_join
OsamaAlkhodairy Jun 14, 2024
8de6099
wip
OsamaAlkhodairy Jun 14, 2024
d296eb4
controller complete
OsamaAlkhodairy Jun 14, 2024
4ccd40a
controller complete
OsamaAlkhodairy Jun 17, 2024
2cf22a0
Merge branch 'main' into feat/inner_join
OsamaAlkhodairy Jun 17, 2024
69b42ea
adding less than constraints
OsamaAlkhodairy Jun 17, 2024
452b595
adding comments
OsamaAlkhodairy Jun 17, 2024
97d0315
more comments
OsamaAlkhodairy Jun 17, 2024
f778a3b
added keygen and prove functions to controller
OsamaAlkhodairy Jun 17, 2024
caaf985
added verify function to controller
OsamaAlkhodairy Jun 17, 2024
08f7605
addressing comments
OsamaAlkhodairy Jun 18, 2024
467499f
Merge branch 'main' into feat/inner_join
OsamaAlkhodairy Jun 20, 2024
30af271
merging new chanegs
OsamaAlkhodairy Jun 20, 2024
6c6f40c
Merge branch 'main' into feat/inner_join
OsamaAlkhodairy Jun 28, 2024
6015fbb
some renaming
OsamaAlkhodairy Jun 28, 2024
db9a4f2
addressing comments
OsamaAlkhodairy Jul 1, 2024
277cf9a
removing too_many_arguments bypassing
OsamaAlkhodairy Jul 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
493 changes: 493 additions & 0 deletions chips/src/inner_join/controller.rs

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions chips/src/inner_join/final_table/bridge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use afs_stark_backend::interaction::{AirBridge, Interaction};
use p3_air::VirtualPairCol;
use p3_field::PrimeField;

use super::FinalTableAir;
use crate::{indexed_output_page_air::columns::IndexedOutputPageCols, utils::to_vcols};

impl<F: PrimeField> AirBridge<F> for FinalTableAir {
/// Sends the same thing as FinalPageAir
fn sends(&self) -> Vec<Interaction<F>> {
AirBridge::sends(&self.final_air)
}

/// Receives (idx, data) of T1 for every allocated row on t1_output_bus (sent by t1_chip)
/// Receives (idx, data) of T2 for every allocated row on t2_output_bus (sent by t2_chip)
fn receives(&self) -> Vec<Interaction<F>> {
let num_cols = self.air_width();
let all_cols = (0..num_cols).collect::<Vec<usize>>();

let table_cols = IndexedOutputPageCols::<usize>::from_slice(
&all_cols,
self.final_air.idx_len,
self.final_air.data_len,
self.final_air.idx_limb_bits,
self.final_air.idx_decomp,
);

let t1_cols = table_cols.page_cols.data[self.fkey_start..self.fkey_end]
.iter()
.chain(table_cols.page_cols.data[self.t2_data_len..].iter())
.copied()
.collect::<Vec<usize>>();

let t2_cols = table_cols
.page_cols
.idx
.iter()
.chain(table_cols.page_cols.data[..self.t2_data_len].iter())
.copied()
.collect::<Vec<usize>>();

vec![
Interaction {
fields: to_vcols(&t1_cols),
count: VirtualPairCol::single_main(table_cols.page_cols.is_alloc),
argument_index: self.t1_output_bus_index,
},
Interaction {
fields: to_vcols(&t2_cols),
count: VirtualPairCol::single_main(table_cols.page_cols.is_alloc),
argument_index: self.t2_output_bus_index,
},
]
}
}
101 changes: 101 additions & 0 deletions chips/src/inner_join/final_table/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/// This chip is mostly just FinalPageAir, but with different interactions
/// Most of the code for it is here (just calls the correspondsing functions
/// from FinalPageAir), but the new interactions are in bridge.rs
use std::sync::Arc;

use afs_stark_backend::air_builders::PartitionedAirBuilder;
use p3_air::{Air, BaseAir};
use p3_field::{Field, PrimeField};
use p3_matrix::dense::RowMajorMatrix;
use p3_uni_stark::{StarkGenericConfig, Val};

use crate::{
common::page::Page, indexed_output_page_air::IndexedOutputPageAir,
range_gate::RangeCheckerGateChip,
};

pub mod bridge;

#[derive(Clone)]
pub(super) struct FinalTableAir {
t1_output_bus_index: usize,
t2_output_bus_index: usize,

/// Foreign key indices within the data
fkey_start: usize,
fkey_end: usize,

t2_data_len: usize,

final_air: IndexedOutputPageAir,
}

impl FinalTableAir {
#[allow(clippy::too_many_arguments)]
OsamaAlkhodairy marked this conversation as resolved.
Show resolved Hide resolved
pub fn new(
t1_output_bus_index: usize,
t2_output_bus_index: usize,
range_bus_index: usize,
idx_len: usize,
t1_data_len: usize,
t2_data_len: usize,
fkey_start: usize,
fkey_end: usize,
idx_limb_bits: usize,
decomp: usize,
) -> Self {
Self {
t1_output_bus_index,
t2_output_bus_index,
fkey_start,
fkey_end,
t2_data_len,
final_air: IndexedOutputPageAir::new(
range_bus_index,
idx_len,
t1_data_len + t2_data_len,
idx_limb_bits,
decomp,
),
}
}

pub fn table_width(&self) -> usize {
self.final_air.page_width()
}

pub fn aux_width(&self) -> usize {
self.final_air.aux_width()
}

pub fn air_width(&self) -> usize {
self.final_air.air_width()
}

pub fn gen_aux_trace<SC: StarkGenericConfig>(
&self,
page: &Page,
range_checker: Arc<RangeCheckerGateChip>,
) -> RowMajorMatrix<Val<SC>>
where
Val<SC>: PrimeField,
{
self.final_air.gen_aux_trace::<SC>(page, range_checker)
}
}

impl<F: Field> BaseAir<F> for FinalTableAir {
fn width(&self) -> usize {
self.air_width()
}
}

impl<AB: PartitionedAirBuilder> Air<AB> for FinalTableAir
where
AB::M: Clone,
{
fn eval(&self, builder: &mut AB) {
// Making sure the page is in the proper format
Air::eval(&self.final_air, builder);
}
}
32 changes: 32 additions & 0 deletions chips/src/inner_join/initial_table/air.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use afs_stark_backend::air_builders::PartitionedAirBuilder;
use p3_air::{Air, AirBuilder, BaseAir};
use p3_field::Field;
use p3_matrix::Matrix;

use super::InitialTableAir;

impl<F: Field> BaseAir<F> for InitialTableAir {
fn width(&self) -> usize {
self.air_width()
}
}

impl<AB: PartitionedAirBuilder> Air<AB> for InitialTableAir
where
AB::M: Clone,
{
fn eval(&self, builder: &mut AB) {
let table_trace: &<AB as AirBuilder>::M = &builder.partitioned_main()[0].clone();
let aux_trace: &<AB as AirBuilder>::M = &builder.partitioned_main()[1].clone();

let (table_local, aux_local) = (table_trace.row_slice(0), aux_trace.row_slice(0));

let is_alloc = table_local[0];
let mult_cnt = aux_local[0];

// Ensuring that mult_cnt is zero if is_alloc is zero
// This is important because we never want to send/receive data if
// the row in unallocated
builder.assert_eq(mult_cnt, mult_cnt * is_alloc);
}
}
91 changes: 91 additions & 0 deletions chips/src/inner_join/initial_table/bridge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use afs_stark_backend::interaction::{AirBridge, Interaction};
use p3_air::VirtualPairCol;
use p3_field::PrimeField;

use crate::utils::to_vcols;

use super::{columns::TableCols, InitialTableAir, TableType};

impl<F: PrimeField> AirBridge<F> for InitialTableAir {
/// For T1:
/// - Sends idx (primary key) with multiplicity is_alloc on t1_intersector_bus (received by intersector_chip)
/// - Sends (idx, data) with multiplicity out_mult on t1_output_bus (received by output_chip)
/// For T2:
/// - Sends foreign key with multiplicity is_alloc on t2_intersector_bus (received by intersector_chip)
/// - Sends (idx, data) with multiplicity out_mult on t2_output_bus (received by output_chip)
fn sends(&self) -> Vec<Interaction<F>> {
let num_cols = self.air_width();
let all_cols = (0..num_cols).collect::<Vec<usize>>();

let table_cols = TableCols::<usize>::from_slice(&all_cols, self.idx_len, self.data_len);

match self.table_type {
TableType::T1 {
t1_intersector_bus_index,
t1_output_bus_index,
} => {
vec![
Interaction {
fields: to_vcols(&table_cols.page_cols.idx),
count: VirtualPairCol::single_main(table_cols.page_cols.is_alloc),
argument_index: t1_intersector_bus_index,
},
Interaction {
fields: to_vcols(
&[table_cols.page_cols.idx, table_cols.page_cols.data].concat(),
),
count: VirtualPairCol::single_main(table_cols.out_mult),
argument_index: t1_output_bus_index,
},
]
}
TableType::T2 {
t2_intersector_bus_index,
t2_output_bus_index,
fkey_start,
fkey_end,
..
} => {
vec![
Interaction {
fields: to_vcols(&table_cols.page_cols.data[fkey_start..fkey_end]),
count: VirtualPairCol::single_main(table_cols.page_cols.is_alloc),
argument_index: t2_intersector_bus_index,
},
Interaction {
fields: to_vcols(
&[table_cols.page_cols.idx, table_cols.page_cols.data].concat(),
),
count: VirtualPairCol::single_main(table_cols.out_mult),
argument_index: t2_output_bus_index,
},
]
}
}
}

/// For T2:
/// - Receives foreign key with multiplicity out_mult on intersector_t2_bus (sent by intersector_chip)
fn receives(&self) -> Vec<Interaction<F>> {
let num_cols = self.air_width();
let all_cols = (0..num_cols).collect::<Vec<usize>>();

let table_cols = TableCols::<usize>::from_slice(&all_cols, self.idx_len, self.data_len);

if let TableType::T2 {
intersector_t2_bus_index,
fkey_start,
fkey_end,
..
} = self.table_type
{
vec![Interaction {
fields: to_vcols(&table_cols.page_cols.data[fkey_start..fkey_end]),
count: VirtualPairCol::single_main(table_cols.out_mult),
argument_index: intersector_t2_bus_index,
}]
} else {
vec![]
}
}
}
18 changes: 18 additions & 0 deletions chips/src/inner_join/initial_table/columns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::common::page_cols::PageCols;

#[derive(Debug)]
pub struct TableCols<T> {
pub page_cols: PageCols<T>,

/// The multiplicity with which we will send (idx, data) to output_chip
pub out_mult: T,
}

impl<T: Clone> TableCols<T> {
pub fn from_slice(cols: &[T], idx_len: usize, data_len: usize) -> TableCols<T> {
TableCols {
page_cols: PageCols::from_slice(&cols[..cols.len() - 1], idx_len, data_len),
out_mult: cols[idx_len + data_len + 1].clone(),
}
}
}
42 changes: 42 additions & 0 deletions chips/src/inner_join/initial_table/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
pub mod air;
pub mod bridge;
pub mod columns;
pub mod trace;

// An enum for the different table types and their bus indices
pub enum TableType {
T1 {
t1_intersector_bus_index: usize,
t1_output_bus_index: usize,
},
T2 {
fkey_start: usize,
fkey_end: usize,

t2_intersector_bus_index: usize,
intersector_t2_bus_index: usize,
t2_output_bus_index: usize,
},
}

#[derive(derive_new::new)]
pub struct InitialTableAir {
idx_len: usize,
data_len: usize,

pub table_type: TableType,
}

impl InitialTableAir {
pub fn table_width(&self) -> usize {
1 + self.idx_len + self.data_len
}

pub fn aux_width(&self) -> usize {
1
}

pub fn air_width(&self) -> usize {
self.table_width() + self.aux_width()
}
}
11 changes: 11 additions & 0 deletions chips/src/inner_join/initial_table/trace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use afs_test_utils::utils::to_field_vec;
use p3_field::Field;
use p3_matrix::dense::RowMajorMatrix;

use super::InitialTableAir;

impl InitialTableAir {
pub fn gen_aux_trace<F: Field>(&self, out_mult: &[u32]) -> RowMajorMatrix<F> {
RowMajorMatrix::new_col(to_field_vec(out_mult.to_vec()))
}
}
Loading