Skip to content

Commit

Permalink
feat: add a range check word type for mle (#162)
Browse files Browse the repository at this point in the history
# Rationale for this change

The ```CommitableColumn``` types are defined as types that can be used
to produce an MLE. We need to add support here for committable columns
of u8s, as this is the initial word size we have chosen for the range
check.

# What changes are included in this PR?

- [x] Add CommitableColumn RangeCheckWord type
- [x] Add RangeCheckWord match arm in column bounds ordering
- [x] Add support for RangeCheckWord in dory commitment cpu helper
- [x] Add support for RangeCheckWord in pack scalars
- [x] Add from [u8] implementation for CommitableColumn   

# Are these changes tested?

- [x] we_can_commit_to_rangecheckword_column_through_committable_column
- [x] we_can_get_length_of_rangecheckword_column
  • Loading branch information
Dustin-Ray authored Sep 15, 2024
1 parent cc436c6 commit 5617893
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/commitment/column_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ impl ColumnBounds {
| CommittableColumn::Decimal75(_, _, _)
| CommittableColumn::Scalar(_)
| CommittableColumn::VarChar(_) => ColumnBounds::NoOrder,
CommittableColumn::RangeCheckWord(_) => ColumnBounds::NoOrder,
}
}

Expand Down
49 changes: 49 additions & 0 deletions crates/proof-of-sql/src/base/commitment/committable_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub enum CommittableColumn<'a> {
VarChar(Vec<[u64; 4]>),
/// Borrowed Timestamp column with Timezone, mapped to `i64`.
TimestampTZ(PoSQLTimeUnit, PoSQLTimeZone, &'a [i64]),
/// Borrowed byte column, mapped to `u8`. This is not a PoSQL
/// type, we need this to commit to words in the range check.
RangeCheckWord(&'a [u8]),
}

impl<'a> CommittableColumn<'a> {
Expand All @@ -55,6 +58,7 @@ impl<'a> CommittableColumn<'a> {
CommittableColumn::VarChar(col) => col.len(),
CommittableColumn::Boolean(col) => col.len(),
CommittableColumn::TimestampTZ(_, _, col) => col.len(),
CommittableColumn::RangeCheckWord(col) => col.len(),
}
}

Expand Down Expand Up @@ -83,6 +87,9 @@ impl<'a> From<&CommittableColumn<'a>> for ColumnType {
CommittableColumn::VarChar(_) => ColumnType::VarChar,
CommittableColumn::Boolean(_) => ColumnType::Boolean,
CommittableColumn::TimestampTZ(tu, tz, _) => ColumnType::TimestampTZ(*tu, *tz),
CommittableColumn::RangeCheckWord(_) => {
unimplemented!("Range check words are not a column type.")
}
}
}
}
Expand Down Expand Up @@ -141,6 +148,11 @@ impl<'a, S: Scalar> From<&'a OwnedColumn<S>> for CommittableColumn<'a> {
}
}

impl<'a> From<&'a [u8]> for CommittableColumn<'a> {
fn from(value: &'a [u8]) -> Self {
CommittableColumn::RangeCheckWord(value)
}
}
impl<'a> From<&'a [i16]> for CommittableColumn<'a> {
fn from(value: &'a [i16]) -> Self {
CommittableColumn::SmallInt(value)
Expand Down Expand Up @@ -187,6 +199,7 @@ impl<'a, 'b> From<&'a CommittableColumn<'b>> for Sequence<'a> {
CommittableColumn::VarChar(limbs) => Sequence::from(limbs),
CommittableColumn::Boolean(bools) => Sequence::from(*bools),
CommittableColumn::TimestampTZ(_, _, times) => Sequence::from(*times),
CommittableColumn::RangeCheckWord(words) => Sequence::from(*words),
}
}
}
Expand Down Expand Up @@ -384,6 +397,18 @@ mod tests {
assert_eq!(bool_committable_column.column_type(), ColumnType::Boolean);
}

#[test]
fn we_can_get_length_of_rangecheckword_column() {
// empty case
let bool_committable_column = CommittableColumn::RangeCheckWord(&[]);
assert_eq!(bool_committable_column.len(), 0);
assert!(bool_committable_column.is_empty());

let bool_committable_column = CommittableColumn::RangeCheckWord(&[12, 34, 56]);
assert_eq!(bool_committable_column.len(), 3);
assert!(!bool_committable_column.is_empty());
}

#[test]
fn we_can_convert_from_borrowing_timestamp_column() {
// empty case
Expand Down Expand Up @@ -692,6 +717,30 @@ mod tests {
assert_eq!(commitment_buffer[0], commitment_buffer[1]);
}

#[test]
fn we_can_commit_to_rangecheckword_column_through_committable_column() {
// empty case
let committable_column = CommittableColumn::RangeCheckWord(&[]);
let sequence = Sequence::from(&committable_column);
let mut commitment_buffer = [CompressedRistretto::default()];
compute_curve25519_commitments(&mut commitment_buffer, &[sequence], 0);
assert_eq!(commitment_buffer[0], CompressedRistretto::default());

// nonempty case
let values = [12, 34, 56];
let committable_column = CommittableColumn::RangeCheckWord(&values);

let sequence_actual = Sequence::from(&committable_column);
let sequence_expected = Sequence::from(values.as_slice());
let mut commitment_buffer = [CompressedRistretto::default(); 2];
compute_curve25519_commitments(
&mut commitment_buffer,
&[sequence_actual, sequence_expected],
0,
);
assert_eq!(commitment_buffer[0], commitment_buffer[1]);
}

#[test]
fn we_can_commit_to_smallint_column_through_committable_column() {
// empty case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ fn compute_dory_commitment(
CommittableColumn::TimestampTZ(_, _, column) => {
compute_dory_commitment_impl(column, offset, setup)
}
CommittableColumn::RangeCheckWord(column) => {
compute_dory_commitment_impl(column, offset, setup)
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions crates/proof-of-sql/src/proof_primitive/dory/pack_scalars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,17 @@ pub fn bit_table_and_scalars_for_packed_msm(
num_matrix_commitment_columns,
);
}
CommittableColumn::RangeCheckWord(column) => {
pack_bit(
column,
&mut packed_scalars,
cumulative_bit_sum_table[i],
offset,
committable_columns[i].column_type().byte_size(),
bit_table_full_sum_in_bytes,
num_matrix_commitment_columns,
);
}
});

(bit_table, packed_scalars)
Expand Down

0 comments on commit 5617893

Please sign in to comment.