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 a1fe87f
Show file tree
Hide file tree
Showing 9 changed files with 105 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,10 @@ pub fn exercise_verification(
"Verification failed: {:?}",
verification_result.err()
);

dbg!("1");
// try changing the result
tamper_result(res, expr, accessor);

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

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

dbg!("3");
// try changing the offset
//
// Note: in the n = 1 case with proof.commmitments all the identity element,
Expand All @@ -84,6 +84,7 @@ pub fn exercise_verification(
fake_accessor.update_offset(table_ref, offset_generators);
res.verify(expr, &fake_accessor, &()).unwrap();
fake_accessor.update_offset(table_ref, offset_generators + 1);
dbg!("4");
assert!(res.verify(expr, &fake_accessor, &()).is_err());
}
}
Expand All @@ -94,11 +95,12 @@ fn tamper_no_result(
accessor: &impl CommitmentAccessor<RistrettoPoint>,
) {
// add a result
dbg!("tamper_no_result11");
let mut res_p = res.clone();
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());

dbg!("tamper_no_result22");
// add a proof
let mut res_p = res.clone();
let expr_p = EmptyTestQueryExpr {
Expand All @@ -114,6 +116,7 @@ fn tamper_no_result(
);
let (proof, _result) = QueryProof::new(&expr_p, &accessor_p, &());
res_p.proof = Some(proof);
dbg!("tamper_no_result33");
assert!(res_p.verify(expr, accessor, &()).is_err());
}

Expand Down Expand Up @@ -141,13 +144,16 @@ fn tamper_result(
expr: &(impl ProofPlan + Serialize),
accessor: &impl CommitmentAccessor<RistrettoPoint>,
) {
dbg!("tamper_result");
if res.provable_result.is_none() {
dbg!("tamper_no_result");
tamper_no_result(res, expr, accessor);
return;
}
let provable_res = res.provable_result.as_ref().unwrap();

if provable_res.table_length() == 0 {
dbg!("tamper_empty_result");
tamper_empty_result(res, expr, accessor);
return;
}
Expand All @@ -157,5 +163,6 @@ fn tamper_result(
let mut provable_res_p = provable_res.clone();
provable_res_p.data_mut()[0] += 1;
res_p.provable_result = Some(provable_res_p);
dbg!("tamper_result 1");
assert!(res_p.verify(expr, accessor, &()).is_err());
}
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.into_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.into_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
Loading

0 comments on commit a1fe87f

Please sign in to comment.