Skip to content

Commit

Permalink
fix: fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
iajoiner committed Nov 14, 2024
1 parent c1f78c3 commit 72e3cb9
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 92 deletions.
18 changes: 16 additions & 2 deletions crates/proof-of-sql/src/base/database/table_utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! borrowed_decimal75("f", 12, 1, [1, 2, 3], &alloc),
//! ]);
//! ```
use super::{Column, Table};
use super::{Column, Table, TableOptions};
use crate::base::scalar::Scalar;
use alloc::{string::String, vec::Vec};
use bumpalo::Bump;
Expand All @@ -27,7 +27,8 @@ use proof_of_sql_parser::{

/// Creates an [`Table`] from a list of `(Identifier, Column)` pairs.
/// This is a convenience wrapper around [`Table::try_from_iter`] primarily for use in tests and
/// intended to be used along with the other methods in this module (e.g. [bigint], [boolean], etc).
/// intended to be used along with the other methods in this module (e.g. [`borrowed_bigint`],
/// [`borrowed_boolean`], etc).
/// The function will panic under a variety of conditions. See [`Table::try_from_iter`] for more details.
///
/// # Example
Expand All @@ -53,6 +54,19 @@ pub fn table<'a, S: Scalar>(
Table::try_from_iter(iter).unwrap()
}

/// Creates an [`Table`] from a list of `(Identifier, Column)` pairs with a specified row count.
/// The main reason for this function is to allow for creating tables that may potentially have
/// no columns, but still have a specified row count.
///
/// # Panics
/// - Panics if the given row count doesn't match the number of rows in any of the columns.
pub fn table_with_row_count<'a, S: Scalar>(
iter: impl IntoIterator<Item = (Identifier, Column<'a, S>)>,
row_count: usize,
) -> Table<'a, S> {
Table::try_from_iter_with_options(iter, TableOptions::new(Some(row_count))).unwrap()
}

/// Creates a (Identifier, `Column`) pair for a tinyint column.
/// This is primarily intended for use in conjunction with [`table`].
/// # Example
Expand Down
57 changes: 18 additions & 39 deletions crates/proof-of-sql/src/sql/proof/query_proof_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use crate::{
commitment::InnerProductProof,
database::{
owned_table_utility::{bigint, owned_table},
Column, ColumnField, ColumnRef, ColumnType, DataAccessor, OwnedTable,
OwnedTableTestAccessor, Table, TableRef,
table_utility::*,
ColumnField, ColumnRef, ColumnType, DataAccessor, OwnedTable, OwnedTableTestAccessor,
Table, TableRef,
},
map::{indexmap, indexset, IndexMap, IndexSet},
map::{indexset, IndexMap, IndexSet},
proof::ProofError,
scalar::{Curve25519Scalar, Scalar},
},
Expand Down Expand Up @@ -45,11 +46,8 @@ impl ProverEvaluate for TrivialTestProofPlan {
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Table<'a, S> {
let col = alloc.alloc_slice_fill_copy(self.length, self.column_fill_value);
Table::<'a, S>::try_new(indexmap! {
"a1".parse().unwrap() => Column::BigInt(col),
})
.unwrap()
let col = vec![self.column_fill_value; self.length];
table([borrowed_bigint("a1", col, alloc)])
}

fn first_round_evaluate(&self, _builder: &mut FirstRoundBuilder) {}
Expand All @@ -66,10 +64,11 @@ impl ProverEvaluate for TrivialTestProofPlan {
SumcheckSubpolynomialType::Identity,
vec![(S::ONE, vec![Box::new(col as &[_])])],
);
Table::<'a, S>::try_new(indexmap! {
"a1".parse().unwrap() => Column::BigInt(col),
})
.unwrap()
table([borrowed_bigint(
"a1",
vec![self.column_fill_value; self.length],
alloc,
)])
}
}
impl ProofPlan for TrivialTestProofPlan {
Expand Down Expand Up @@ -220,11 +219,7 @@ impl ProverEvaluate for SquareTestProofPlan {
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Table<'a, S> {
let res: &[_] = alloc.alloc_slice_copy(&self.res);
Table::<'a, S>::try_new(indexmap! {
"a1".parse().unwrap() => Column::BigInt(res),
})
.unwrap()
table([borrowed_bigint("a1", self.res, alloc)])
}

fn first_round_evaluate(&self, _builder: &mut FirstRoundBuilder) {}
Expand All @@ -249,10 +244,7 @@ impl ProverEvaluate for SquareTestProofPlan {
(-S::ONE, vec![Box::new(x), Box::new(x)]),
],
);
Table::<'a, S>::try_new(indexmap! {
"a1".parse().unwrap() => Column::BigInt(&[9, 25]),
})
.unwrap()
table([borrowed_bigint("a1", self.res, alloc)])
}
}
impl ProofPlan for SquareTestProofPlan {
Expand Down Expand Up @@ -403,11 +395,7 @@ impl ProverEvaluate for DoubleSquareTestProofPlan {
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Table<'a, S> {
let res: &[_] = alloc.alloc_slice_copy(&self.res);
Table::<'a, S>::try_new(indexmap! {
"a1".parse().unwrap() => Column::BigInt(res),
})
.unwrap()
table([borrowed_bigint("a1", self.res, alloc)])
}

fn first_round_evaluate(&self, _builder: &mut FirstRoundBuilder) {}
Expand Down Expand Up @@ -445,10 +433,7 @@ impl ProverEvaluate for DoubleSquareTestProofPlan {
],
);
builder.produce_intermediate_mle(res);
Table::<'a, S>::try_new(indexmap! {
"a1".parse().unwrap() => Column::BigInt(res),
})
.unwrap()
table([borrowed_bigint("a1", self.res, alloc)])
}
}
impl ProofPlan for DoubleSquareTestProofPlan {
Expand Down Expand Up @@ -613,13 +598,10 @@ struct ChallengeTestProofPlan {}
impl ProverEvaluate for ChallengeTestProofPlan {
fn result_evaluate<'a, S: Scalar>(
&self,
_alloc: &'a Bump,
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Table<'a, S> {
Table::<'a, S>::try_new(indexmap! {
"a1".parse().unwrap() => Column::BigInt(&[9, 25]),
})
.unwrap()
table([borrowed_bigint("a1", [9, 25], alloc)])
}

fn first_round_evaluate(&self, builder: &mut FirstRoundBuilder) {
Expand Down Expand Up @@ -648,10 +630,7 @@ impl ProverEvaluate for ChallengeTestProofPlan {
(-alpha, vec![Box::new(x), Box::new(x)]),
],
);
Table::<'a, S>::try_new(indexmap! {
"a1".parse().unwrap() => Column::BigInt(&[9, 25]),
})
.unwrap()
table([borrowed_bigint("a1", [9, 25], alloc)])
}
}
impl ProofPlan for ChallengeTestProofPlan {
Expand Down
22 changes: 11 additions & 11 deletions crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use crate::{
commitment::InnerProductProof,
database::{
owned_table_utility::{bigint, owned_table},
Column, ColumnField, ColumnRef, ColumnType, DataAccessor, OwnedTable,
OwnedTableTestAccessor, Table, TableRef,
table_utility::*,
ColumnField, ColumnRef, ColumnType, DataAccessor, OwnedTable, OwnedTableTestAccessor,
Table, TableRef,
},
map::{indexset, IndexMap, IndexSet},
proof::ProofError,
Expand All @@ -30,12 +31,11 @@ impl ProverEvaluate for EmptyTestQueryExpr {
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Table<'a, S> {
let zeros = vec![0; self.length];
let res: &[_] = alloc.alloc_slice_copy(&zeros);
Table::<'a, S>::try_from_iter(
(1..=self.columns).map(|i| (format!("a{i}").parse().unwrap(), Column::BigInt(res))),
let zeros = vec![0_i64; self.length];
table_with_row_count(
(1..=self.columns).map(|i| borrowed_bigint(format!("a{i}"), zeros.clone(), alloc)),
self.length,
)
.unwrap()
}
fn first_round_evaluate(&self, _builder: &mut FirstRoundBuilder) {}
fn final_round_evaluate<'a, S: Scalar>(
Expand All @@ -44,15 +44,15 @@ impl ProverEvaluate for EmptyTestQueryExpr {
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Table<'a, S> {
let zeros = vec![0; self.length];
let zeros = vec![0_i64; self.length];
let res: &[_] = alloc.alloc_slice_copy(&zeros);
let _ = std::iter::repeat_with(|| builder.produce_intermediate_mle(res))
.take(self.columns)
.collect::<Vec<_>>();
Table::<'a, S>::try_from_iter(
(1..=self.columns).map(|i| (format!("a{i}").parse().unwrap(), Column::BigInt(res))),
table_with_row_count(
(1..=self.columns).map(|i| borrowed_bigint(format!("a{i}"), zeros.clone(), alloc)),
self.length,
)
.unwrap()
}
}
impl ProofPlan for EmptyTestQueryExpr {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ pub fn exercise_verification(
"Verification failed: {:?}",
verification_result.err()
);

// try changing the result
tamper_result(res, expr, accessor);

if res.proof.is_none() {
return;
}
Expand All @@ -54,7 +52,6 @@ pub fn exercise_verification(
res_p.proof.as_mut().unwrap().pcs_proof_evaluations[i] += Curve25519Scalar::one();
assert!(res_p.verify(expr, accessor, &()).is_err());
}

// try changing intermediate commitments
let commit_p = RistrettoPoint::compute_commitments(
&[CommittableColumn::BigInt(&[
Expand All @@ -70,7 +67,6 @@ pub fn exercise_verification(
res_p.proof.as_mut().unwrap().commitments[i] = commit_p;
assert!(res_p.verify(expr, accessor, &()).is_err());
}

// try changing the offset
//
// Note: in the n = 1 case with proof.commmitments all the identity element,
Expand Down Expand Up @@ -98,7 +94,6 @@ fn tamper_no_result(
let cols: [Column<'_, Curve25519Scalar>; 1] = [Column::BigInt(&[0_i64; 0])];
res_p.provable_result = Some(ProvableQueryResult::new(0, &cols));
assert!(res_p.verify(expr, accessor, &()).is_err());

// add a proof
let mut res_p = res.clone();
let expr_p = EmptyTestQueryExpr {
Expand Down
12 changes: 9 additions & 3 deletions crates/proof-of-sql/src/sql/proof_plans/empty_exec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{
base::{
database::{ColumnField, ColumnRef, DataAccessor, OwnedTable, Table, TableRef},
database::{
ColumnField, ColumnRef, DataAccessor, OwnedTable, Table, TableOptions, TableRef,
},
map::{IndexMap, IndexSet},
proof::ProofError,
scalar::Scalar,
Expand Down Expand Up @@ -68,7 +70,9 @@ impl ProverEvaluate for EmptyExec {
_alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Table<'a, S> {
Table::<'a, S>::try_new(IndexMap::default()).unwrap()
// Create an empty table with one row
Table::<'a, S>::try_new_with_options(IndexMap::default(), TableOptions::new(Some(1)))
.unwrap()
}

fn first_round_evaluate(&self, _builder: &mut FirstRoundBuilder) {}
Expand All @@ -81,6 +85,8 @@ impl ProverEvaluate for EmptyExec {
_alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Table<'a, S> {
Table::<'a, S>::try_new(IndexMap::default()).unwrap()
// Create an empty table with one row
Table::<'a, S>::try_new_with_options(IndexMap::default(), TableOptions::new(Some(1)))
.unwrap()
}
}
10 changes: 7 additions & 3 deletions crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
base::{
database::{
filter_util::filter_columns, Column, ColumnField, ColumnRef, DataAccessor, OwnedTable,
TableRef,
Table, TableOptions, TableRef,
},
map::{IndexMap, IndexSet},
proof::ProofError,
Expand Down Expand Up @@ -149,6 +149,7 @@ impl ProverEvaluate for FilterExec {
let selection = selection_column
.as_boolean()
.expect("selection is not boolean");
let output_length = selection.iter().filter(|b| **b).count();

// 2. columns
let columns: Vec<_> = self
Expand All @@ -159,11 +160,12 @@ impl ProverEvaluate for FilterExec {

// Compute filtered_columns and indexes
let (filtered_columns, _) = filter_columns(alloc, &columns, selection);
Table::<'a, S>::try_from_iter(
Table::<'a, S>::try_from_iter_with_options(
self.aliased_results
.iter()
.map(|expr| expr.alias)
.zip(filtered_columns),
TableOptions::new(Some(output_length)),
)
.expect("Failed to create table from iterator")
}
Expand All @@ -189,6 +191,7 @@ impl ProverEvaluate for FilterExec {
let selection = selection_column
.as_boolean()
.expect("selection is not boolean");
let output_length = selection.iter().filter(|b| **b).count();

// 2. columns
let columns: Vec<_> = self
Expand Down Expand Up @@ -220,11 +223,12 @@ impl ProverEvaluate for FilterExec {
&filtered_columns,
result_len,
);
Table::<'a, S>::try_from_iter(
Table::<'a, S>::try_from_iter_with_options(
self.aliased_results
.iter()
.map(|expr| expr.alias)
.zip(filtered_columns),
TableOptions::new(Some(output_length)),
)
.expect("Failed to create table from iterator")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
base::{
database::{
filter_util::*, owned_table_utility::*, Column, DataAccessor, OwnedTableTestAccessor,
TestAccessor,
Table, TableOptions, TestAccessor,
},
proof::ProofError,
scalar::Scalar,
Expand Down Expand Up @@ -45,6 +45,7 @@ impl ProverEvaluate for DishonestFilterExec {
let selection = selection_column
.as_boolean()
.expect("selection is not boolean");
let output_length = selection.iter().filter(|b| **b).count();
// 2. columns
let columns: Vec<_> = self
.aliased_results
Expand All @@ -54,11 +55,12 @@ impl ProverEvaluate for DishonestFilterExec {
// Compute filtered_columns
let (filtered_columns, _) = filter_columns(alloc, &columns, selection);
let filtered_columns = tamper_column(alloc, filtered_columns);
Table::<'a, S>::try_from_iter(
Table::<'a, S>::try_from_iter_with_options(
self.aliased_results
.iter()
.map(|expr| expr.alias)
.zip(filtered_columns),
TableOptions::new(Some(output_length)),
)
.expect("Failed to create table from iterator")
}
Expand Down Expand Up @@ -88,6 +90,7 @@ impl ProverEvaluate for DishonestFilterExec {
let selection = selection_column
.as_boolean()
.expect("selection is not boolean");
let output_length = selection.iter().filter(|b| **b).count();
// 2. columns
let columns: Vec<_> = self
.aliased_results
Expand Down Expand Up @@ -119,11 +122,12 @@ impl ProverEvaluate for DishonestFilterExec {
&filtered_columns,
result_len,
);
Table::<'a, S>::try_from_iter(
Table::<'a, S>::try_from_iter_with_options(
self.aliased_results
.iter()
.map(|expr| expr.alias)
.zip(filtered_columns),
TableOptions::new(Some(output_length)),
)
.expect("Failed to create table from iterator")
}
Expand Down
Loading

0 comments on commit 72e3cb9

Please sign in to comment.