From ed5d4854c88fcb8c8196e1c4ee3003702f5fb06e Mon Sep 17 00:00:00 2001 From: Ian Joiner <14581281+iajoiner@users.noreply.github.com> Date: Tue, 12 Nov 2024 15:35:25 -0500 Subject: [PATCH] refactor!: replace `DataAccessor` with `Table` in `ProofExpr` && remove `table_length` from `ProofPlan::result_evaluate` --- .../proof-of-sql/src/sql/proof/proof_plan.rs | 3 +- .../proof-of-sql/src/sql/proof/query_proof.rs | 2 +- .../src/sql/proof/query_proof_test.rs | 4 -- .../sql/proof/verifiable_query_result_test.rs | 1 - .../src/sql/proof_exprs/add_subtract_expr.rs | 16 +++---- .../sql/proof_exprs/add_subtract_expr_test.rs | 21 +++++---- .../src/sql/proof_exprs/aggregate_expr.rs | 11 +++-- .../src/sql/proof_exprs/and_expr.rs | 19 ++++---- .../src/sql/proof_exprs/and_expr_test.rs | 21 +++++---- .../src/sql/proof_exprs/column_expr.rs | 21 +++++---- .../src/sql/proof_exprs/dyn_proof_expr.rs | 2 +- .../src/sql/proof_exprs/equals_expr.rs | 21 +++++---- .../src/sql/proof_exprs/equals_expr_test.rs | 22 +++++---- .../src/sql/proof_exprs/inequality_expr.rs | 16 +++---- .../sql/proof_exprs/inequality_expr_test.rs | 30 ++++++++----- .../src/sql/proof_exprs/literal_expr.rs | 14 +++--- .../src/sql/proof_exprs/literal_expr_test.rs | 14 +++--- .../src/sql/proof_exprs/multiply_expr.rs | 16 +++---- .../src/sql/proof_exprs/multiply_expr_test.rs | 21 +++++---- .../src/sql/proof_exprs/not_expr.rs | 11 +++-- .../src/sql/proof_exprs/not_expr_test.rs | 21 +++++---- .../src/sql/proof_exprs/or_expr.rs | 22 +++++---- .../src/sql/proof_exprs/or_expr_test.rs | 23 +++++----- .../src/sql/proof_exprs/proof_expr.rs | 7 ++- .../src/sql/proof_exprs/proof_expr_test.rs | 24 ++++++---- .../src/sql/proof_plans/empty_exec.rs | 1 - .../src/sql/proof_plans/filter_exec.rs | 34 +++++++++----- .../src/sql/proof_plans/filter_exec_test.rs | 8 ++-- .../filter_exec_test_dishonest_prover.rs | 45 ++++++++++++------- .../src/sql/proof_plans/group_by_exec.rs | 38 ++++++++++------ .../src/sql/proof_plans/projection_exec.rs | 28 ++++++++---- .../sql/proof_plans/projection_exec_test.rs | 6 +-- 32 files changed, 308 insertions(+), 235 deletions(-) diff --git a/crates/proof-of-sql/src/sql/proof/proof_plan.rs b/crates/proof-of-sql/src/sql/proof/proof_plan.rs index ac73f183e..240c67218 100644 --- a/crates/proof-of-sql/src/sql/proof/proof_plan.rs +++ b/crates/proof-of-sql/src/sql/proof/proof_plan.rs @@ -38,10 +38,9 @@ pub trait ProofPlan: Debug + Send + Sync + ProverEvaluate { #[enum_dispatch::enum_dispatch(DynProofPlan)] pub trait ProverEvaluate { - /// Evaluate the query and modify `FirstRoundBuilder` to track the result of the query. + /// Evaluate the query and return the result. fn result_evaluate<'a, S: Scalar>( &self, - input_length: usize, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec>; diff --git a/crates/proof-of-sql/src/sql/proof/query_proof.rs b/crates/proof-of-sql/src/sql/proof/query_proof.rs index 64e7869dd..7053e1752 100644 --- a/crates/proof-of-sql/src/sql/proof/query_proof.rs +++ b/crates/proof-of-sql/src/sql/proof/query_proof.rs @@ -75,7 +75,7 @@ impl QueryProof { let alloc = Bump::new(); // Evaluate query result - let result_cols = expr.result_evaluate(range_length, &alloc, accessor); + let result_cols = expr.result_evaluate(&alloc, accessor); let output_length = result_cols.first().map_or(0, Column::len); let provable_result = ProvableQueryResult::new(output_length as u64, &result_cols); diff --git a/crates/proof-of-sql/src/sql/proof/query_proof_test.rs b/crates/proof-of-sql/src/sql/proof/query_proof_test.rs index 22046721f..4b9a8ee8a 100644 --- a/crates/proof-of-sql/src/sql/proof/query_proof_test.rs +++ b/crates/proof-of-sql/src/sql/proof/query_proof_test.rs @@ -42,7 +42,6 @@ impl Default for TrivialTestProofPlan { impl ProverEvaluate for TrivialTestProofPlan { fn result_evaluate<'a, S: Scalar>( &self, - _input_length: usize, alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { @@ -212,7 +211,6 @@ impl Default for SquareTestProofPlan { impl ProverEvaluate for SquareTestProofPlan { fn result_evaluate<'a, S: Scalar>( &self, - _table_length: usize, alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { @@ -387,7 +385,6 @@ impl Default for DoubleSquareTestProofPlan { impl ProverEvaluate for DoubleSquareTestProofPlan { fn result_evaluate<'a, S: Scalar>( &self, - _input_length: usize, alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { @@ -592,7 +589,6 @@ struct ChallengeTestProofPlan {} impl ProverEvaluate for ChallengeTestProofPlan { fn result_evaluate<'a, S: Scalar>( &self, - _input_length: usize, _alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { diff --git a/crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs b/crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs index 03c07837b..1c1018d27 100644 --- a/crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs +++ b/crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs @@ -27,7 +27,6 @@ pub(super) struct EmptyTestQueryExpr { impl ProverEvaluate for EmptyTestQueryExpr { fn result_evaluate<'a, S: Scalar>( &self, - _input_length: usize, alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr.rs index d47004f62..d16b9d84e 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr.rs @@ -3,8 +3,7 @@ use crate::{ base::{ commitment::Commitment, database::{ - try_add_subtract_column_types, Column, ColumnRef, ColumnType, CommitmentAccessor, - DataAccessor, + try_add_subtract_column_types, Column, ColumnRef, ColumnType, CommitmentAccessor, Table, }, map::IndexSet, proof::ProofError, @@ -49,12 +48,11 @@ impl ProofExpr for AddSubtractExpr { fn result_evaluate<'a, S: Scalar>( &self, - table_length: usize, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let lhs_column: Column<'a, S> = self.lhs.result_evaluate(table_length, alloc, accessor); - let rhs_column: Column<'a, S> = self.rhs.result_evaluate(table_length, alloc, accessor); + let lhs_column: Column<'a, S> = self.lhs.result_evaluate(alloc, table); + let rhs_column: Column<'a, S> = self.rhs.result_evaluate(alloc, table); Column::Scalar(add_subtract_columns( lhs_column, rhs_column, @@ -74,10 +72,10 @@ impl ProofExpr for AddSubtractExpr { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let lhs_column: Column<'a, S> = self.lhs.prover_evaluate(builder, alloc, accessor); - let rhs_column: Column<'a, S> = self.rhs.prover_evaluate(builder, alloc, accessor); + let lhs_column: Column<'a, S> = self.lhs.prover_evaluate(builder, alloc, table); + let rhs_column: Column<'a, S> = self.rhs.prover_evaluate(builder, alloc, table); Column::Scalar(add_subtract_columns( lhs_column, rhs_column, diff --git a/crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr_test.rs b/crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr_test.rs index 371c9c254..7e25c27bb 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr_test.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr_test.rs @@ -1,7 +1,10 @@ use crate::{ base::{ commitment::InnerProductProof, - database::{owned_table_utility::*, Column, OwnedTableTestAccessor}, + database::{ + owned_table_utility::*, table_utility::*, Column, OwnedTableTestAccessor, + TableTestAccessor, + }, scalar::{test_scalar::TestScalar, Curve25519Scalar}, }, sql::{ @@ -306,20 +309,20 @@ fn we_can_query_random_tables_using_a_non_zero_offset() { // b + a - 1 #[test] fn we_can_compute_the_correct_output_of_an_add_subtract_expr_using_result_evaluate() { - let data = owned_table([ - smallint("a", [1_i16, 2, 3, 4]), - int("b", [0_i32, 1, 0, 1]), - varchar("d", ["ab", "t", "efg", "g"]), - bigint("c", [0_i64, 2, 2, 0]), + let alloc = Bump::new(); + let data = table([ + borrowed_smallint("a", [1_i16, 2, 3, 4], &alloc), + borrowed_int("b", [0_i32, 1, 0, 1], &alloc), + borrowed_varchar("d", ["ab", "t", "efg", "g"], &alloc), + borrowed_bigint("c", [0_i64, 2, 2, 0], &alloc), ]); let t = "sxt.t".parse().unwrap(); - let accessor = OwnedTableTestAccessor::::new_from_table(t, data, 0, ()); + let accessor = TableTestAccessor::::new_from_table(t, data.clone(), 0, ()); let add_subtract_expr: DynProofExpr = add( column(t, "b", &accessor), subtract(column(t, "a", &accessor), const_bigint(1)), ); - let alloc = Bump::new(); - let res = add_subtract_expr.result_evaluate(4, &alloc, &accessor); + let res = add_subtract_expr.result_evaluate(&alloc, &data); let expected_res_scalar = [0, 2, 2, 4] .iter() .map(|v| Curve25519Scalar::from(*v)) diff --git a/crates/proof-of-sql/src/sql/proof_exprs/aggregate_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/aggregate_expr.rs index b9fdb3f8d..35f5baf4b 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/aggregate_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/aggregate_expr.rs @@ -2,7 +2,7 @@ use super::{DynProofExpr, ProofExpr}; use crate::{ base::{ commitment::Commitment, - database::{Column, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor}, + database::{Column, ColumnRef, ColumnType, CommitmentAccessor, Table}, map::IndexSet, proof::ProofError, scalar::Scalar, @@ -46,11 +46,10 @@ impl ProofExpr for AggregateExpr { #[tracing::instrument(name = "AggregateExpr::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a, S: Scalar>( &self, - table_length: usize, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - self.expr.result_evaluate(table_length, alloc, accessor) + self.expr.result_evaluate(alloc, table) } #[tracing::instrument(name = "AggregateExpr::prover_evaluate", level = "debug", skip_all)] @@ -58,9 +57,9 @@ impl ProofExpr for AggregateExpr { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - self.expr.prover_evaluate(builder, alloc, accessor) + self.expr.prover_evaluate(builder, alloc, table) } fn verifier_evaluate( diff --git a/crates/proof-of-sql/src/sql/proof_exprs/and_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/and_expr.rs index 0979d0fa2..9fcf97a0d 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/and_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/and_expr.rs @@ -2,7 +2,7 @@ use super::{DynProofExpr, ProofExpr}; use crate::{ base::{ commitment::Commitment, - database::{Column, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor}, + database::{Column, ColumnRef, ColumnType, CommitmentAccessor, Table}, map::IndexSet, proof::ProofError, scalar::Scalar, @@ -44,15 +44,16 @@ impl ProofExpr for AndExpr { #[tracing::instrument(name = "AndExpr::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a, S: Scalar>( &self, - table_length: usize, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let lhs_column: Column<'a, S> = self.lhs.result_evaluate(table_length, alloc, accessor); - let rhs_column: Column<'a, S> = self.rhs.result_evaluate(table_length, alloc, accessor); + let lhs_column: Column<'a, S> = self.lhs.result_evaluate(alloc, table); + let rhs_column: Column<'a, S> = self.rhs.result_evaluate(alloc, table); let lhs = lhs_column.as_boolean().expect("lhs is not boolean"); let rhs = rhs_column.as_boolean().expect("rhs is not boolean"); - Column::Boolean(alloc.alloc_slice_fill_with(table_length, |i| lhs[i] && rhs[i])) + Column::Boolean( + alloc.alloc_slice_fill_with(table.num_rows().unwrap_or(0), |i| lhs[i] && rhs[i]), + ) } #[tracing::instrument(name = "AndExpr::prover_evaluate", level = "debug", skip_all)] @@ -60,10 +61,10 @@ impl ProofExpr for AndExpr { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let lhs_column: Column<'a, S> = self.lhs.prover_evaluate(builder, alloc, accessor); - let rhs_column: Column<'a, S> = self.rhs.prover_evaluate(builder, alloc, accessor); + let lhs_column: Column<'a, S> = self.lhs.prover_evaluate(builder, alloc, table); + let rhs_column: Column<'a, S> = self.rhs.prover_evaluate(builder, alloc, table); let lhs = lhs_column.as_boolean().expect("lhs is not boolean"); let rhs = rhs_column.as_boolean().expect("rhs is not boolean"); let n = lhs.len(); diff --git a/crates/proof-of-sql/src/sql/proof_exprs/and_expr_test.rs b/crates/proof-of-sql/src/sql/proof_exprs/and_expr_test.rs index 6ad569e8e..bf4d50645 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/and_expr_test.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/and_expr_test.rs @@ -1,7 +1,10 @@ use crate::{ base::{ commitment::InnerProductProof, - database::{owned_table_utility::*, Column, OwnedTableTestAccessor}, + database::{ + owned_table_utility::*, table_utility::*, Column, OwnedTableTestAccessor, + TableTestAccessor, + }, scalar::test_scalar::TestScalar, }, sql::{ @@ -153,20 +156,20 @@ fn we_can_query_random_tables_using_a_non_zero_offset() { #[test] fn we_can_compute_the_correct_output_of_an_and_expr_using_result_evaluate() { - let data = owned_table([ - bigint("a", [1, 2, 3, 4]), - bigint("b", [0, 1, 0, 1]), - varchar("d", ["ab", "t", "efg", "g"]), - bigint("c", [0, 2, 2, 0]), + let alloc = Bump::new(); + let data = table([ + borrowed_bigint("a", [1, 2, 3, 4], &alloc), + borrowed_bigint("b", [0, 1, 0, 1], &alloc), + borrowed_varchar("d", ["ab", "t", "efg", "g"], &alloc), + borrowed_bigint("c", [0, 2, 2, 0], &alloc), ]); let t = "sxt.t".parse().unwrap(); - let accessor = OwnedTableTestAccessor::::new_from_table(t, data, 0, ()); + let accessor = TableTestAccessor::::new_from_table(t, data.clone(), 0, ()); let and_expr: DynProofExpr = and( equal(column(t, "b", &accessor), const_int128(1)), equal(column(t, "d", &accessor), const_varchar("t")), ); - let alloc = Bump::new(); - let res = and_expr.result_evaluate(4, &alloc, &accessor); + let res = and_expr.result_evaluate(&alloc, &data); let expected_res = Column::Boolean(&[false, true, false, false]); assert_eq!(res, expected_res); } diff --git a/crates/proof-of-sql/src/sql/proof_exprs/column_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/column_expr.rs index a5df03a3b..f71cd3fec 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/column_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/column_expr.rs @@ -2,7 +2,7 @@ use super::ProofExpr; use crate::{ base::{ commitment::Commitment, - database::{Column, ColumnField, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor}, + database::{Column, ColumnField, ColumnRef, ColumnType, CommitmentAccessor, Table}, map::IndexSet, proof::ProofError, scalar::Scalar, @@ -40,6 +40,14 @@ impl ColumnExpr { pub fn column_id(&self) -> Identifier { self.column_ref.column_id() } + + /// Get the column + pub fn get_column<'a, S: Scalar>(&self, table: &Table<'a, S>) -> Column<'a, S> { + *table + .inner_table() + .get(&self.column_ref.column_id()) + .expect("Column not found") + } } impl ProofExpr for ColumnExpr { @@ -58,13 +66,10 @@ impl ProofExpr for ColumnExpr { /// add the result to the [`FirstRoundBuilder`](crate::sql::proof::FirstRoundBuilder) fn result_evaluate<'a, S: Scalar>( &self, - table_length: usize, _alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let column = accessor.get_column(self.column_ref); - assert_eq!(column.len(), table_length); - column + self.get_column(table) } /// Given the selected rows (as a slice of booleans), evaluate the column expression and @@ -73,9 +78,9 @@ impl ProofExpr for ColumnExpr { &self, builder: &mut FinalRoundBuilder<'a, S>, _alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let column = accessor.get_column(self.column_ref); + let column = self.get_column(table); builder.produce_anchored_mle(column); column } diff --git a/crates/proof-of-sql/src/sql/proof_exprs/dyn_proof_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/dyn_proof_expr.rs index 06c46578c..433193255 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/dyn_proof_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/dyn_proof_expr.rs @@ -5,7 +5,7 @@ use super::{ use crate::{ base::{ commitment::Commitment, - database::{Column, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor, LiteralValue}, + database::{Column, ColumnRef, ColumnType, CommitmentAccessor, LiteralValue, Table}, map::IndexSet, proof::ProofError, scalar::Scalar, diff --git a/crates/proof-of-sql/src/sql/proof_exprs/equals_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/equals_expr.rs index e495f2676..5bc57cbe8 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/equals_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/equals_expr.rs @@ -2,7 +2,7 @@ use super::{scale_and_add_subtract_eval, scale_and_subtract, DynProofExpr, Proof use crate::{ base::{ commitment::Commitment, - database::{Column, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor}, + database::{Column, ColumnRef, ColumnType, CommitmentAccessor, Table}, map::IndexSet, proof::ProofError, scalar::Scalar, @@ -43,17 +43,20 @@ impl ProofExpr for EqualsExpr { #[tracing::instrument(name = "EqualsExpr::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a, S: Scalar>( &self, - table_length: usize, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let lhs_column = self.lhs.result_evaluate(table_length, alloc, accessor); - let rhs_column = self.rhs.result_evaluate(table_length, alloc, accessor); + let lhs_column = self.lhs.result_evaluate(alloc, table); + let rhs_column = self.rhs.result_evaluate(alloc, table); let lhs_scale = self.lhs.data_type().scale().unwrap_or(0); let rhs_scale = self.rhs.data_type().scale().unwrap_or(0); let res = scale_and_subtract(alloc, lhs_column, rhs_column, lhs_scale, rhs_scale, true) .expect("Failed to scale and subtract"); - Column::Boolean(result_evaluate_equals_zero(table_length, alloc, res)) + Column::Boolean(result_evaluate_equals_zero( + table.num_rows().unwrap_or(0), + alloc, + res, + )) } #[tracing::instrument(name = "EqualsExpr::prover_evaluate", level = "debug", skip_all)] @@ -61,10 +64,10 @@ impl ProofExpr for EqualsExpr { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let lhs_column = self.lhs.prover_evaluate(builder, alloc, accessor); - let rhs_column = self.rhs.prover_evaluate(builder, alloc, accessor); + let lhs_column = self.lhs.prover_evaluate(builder, alloc, table); + let rhs_column = self.rhs.prover_evaluate(builder, alloc, table); let lhs_scale = self.lhs.data_type().scale().unwrap_or(0); let rhs_scale = self.rhs.data_type().scale().unwrap_or(0); let res = scale_and_subtract(alloc, lhs_column, rhs_column, lhs_scale, rhs_scale, true) diff --git a/crates/proof-of-sql/src/sql/proof_exprs/equals_expr_test.rs b/crates/proof-of-sql/src/sql/proof_exprs/equals_expr_test.rs index ea4cd9247..cb18ae20f 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/equals_expr_test.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/equals_expr_test.rs @@ -1,7 +1,10 @@ use crate::{ base::{ commitment::InnerProductProof, - database::{owned_table_utility::*, Column, OwnedTable, OwnedTableTestAccessor}, + database::{ + owned_table_utility::*, table_utility::*, Column, OwnedTable, OwnedTableTestAccessor, + Table, TableTestAccessor, + }, scalar::{Curve25519Scalar, Scalar}, }, sql::{ @@ -388,11 +391,12 @@ fn we_can_query_random_tables_using_a_non_zero_offset() { #[test] fn we_can_compute_the_correct_output_of_an_equals_expr_using_result_evaluate() { - let data: OwnedTable = owned_table([ - bigint("a", [1, 2, 3, 4]), - bigint("b", [0, 5, 0, 5]), - varchar("c", ["t", "ghi", "jj", "f"]), - decimal75( + let alloc = Bump::new(); + let data: Table = table([ + borrowed_bigint("a", [1, 2, 3, 4], &alloc), + borrowed_bigint("b", [0, 5, 0, 5], &alloc), + borrowed_varchar("c", ["t", "ghi", "jj", "f"], &alloc), + borrowed_decimal75( "e", 42, 10, @@ -402,16 +406,16 @@ fn we_can_compute_the_correct_output_of_an_equals_expr_using_result_evaluate() { Curve25519Scalar::ZERO, Curve25519Scalar::from(-1), ], + &alloc, ), ]); let t = "sxt.t".parse().unwrap(); - let accessor = OwnedTableTestAccessor::::new_from_table(t, data, 0, ()); + let accessor = TableTestAccessor::::new_from_table(t, data.clone(), 0, ()); let equals_expr: DynProofExpr = equal( column(t, "e", &accessor), const_scalar::(Curve25519Scalar::ZERO), ); - let alloc = Bump::new(); - let res = equals_expr.result_evaluate(4, &alloc, &accessor); + let res = equals_expr.result_evaluate(&alloc, &data); let expected_res = Column::Boolean(&[true, false, true, false]); assert_eq!(res, expected_res); } diff --git a/crates/proof-of-sql/src/sql/proof_exprs/inequality_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/inequality_expr.rs index 96fb847fc..c4f32fa59 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/inequality_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/inequality_expr.rs @@ -7,7 +7,7 @@ use super::{ use crate::{ base::{ commitment::Commitment, - database::{Column, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor}, + database::{Column, ColumnRef, ColumnType, CommitmentAccessor, Table}, map::IndexSet, proof::ProofError, scalar::Scalar, @@ -58,14 +58,14 @@ impl ProofExpr for InequalityExpr { #[tracing::instrument(name = "InequalityExpr::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a, S: Scalar>( &self, - table_length: usize, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let lhs_column = self.lhs.result_evaluate(table_length, alloc, accessor); - let rhs_column = self.rhs.result_evaluate(table_length, alloc, accessor); + let lhs_column = self.lhs.result_evaluate(alloc, table); + let rhs_column = self.rhs.result_evaluate(alloc, table); let lhs_scale = self.lhs.data_type().scale().unwrap_or(0); let rhs_scale = self.rhs.data_type().scale().unwrap_or(0); + let table_length = table.num_rows().unwrap_or(0); let diff = if self.is_lte { scale_and_subtract(alloc, lhs_column, rhs_column, lhs_scale, rhs_scale, false) .expect("Failed to scale and subtract") @@ -89,10 +89,10 @@ impl ProofExpr for InequalityExpr { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let lhs_column = self.lhs.prover_evaluate(builder, alloc, accessor); - let rhs_column = self.rhs.prover_evaluate(builder, alloc, accessor); + let lhs_column = self.lhs.prover_evaluate(builder, alloc, table); + let rhs_column = self.rhs.prover_evaluate(builder, alloc, table); let lhs_scale = self.lhs.data_type().scale().unwrap_or(0); let rhs_scale = self.rhs.data_type().scale().unwrap_or(0); let diff = if self.is_lte { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/inequality_expr_test.rs b/crates/proof-of-sql/src/sql/proof_exprs/inequality_expr_test.rs index d8bf6895a..45362f18b 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/inequality_expr_test.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/inequality_expr_test.rs @@ -2,8 +2,8 @@ use crate::{ base::{ commitment::InnerProductProof, database::{ - owned_table_utility::*, Column, LiteralValue, OwnedTable, OwnedTableTestAccessor, - TestAccessor, + owned_table_utility::*, table_utility::*, Column, LiteralValue, OwnedTable, + OwnedTableTestAccessor, TableTestAccessor, TestAccessor, }, scalar::{Curve25519Scalar, Scalar, ScalarExt}, }, @@ -560,30 +560,36 @@ fn we_can_query_random_tables_using_a_non_zero_offset() { #[test] fn we_can_compute_the_correct_output_of_a_lte_inequality_expr_using_result_evaluate() { - let data = owned_table([bigint("a", [-1, 9, 1]), bigint("b", [1, 2, 3])]); - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); + let alloc = Bump::new(); + let data = table([ + borrowed_bigint("a", [-1, 9, 1], &alloc), + borrowed_bigint("b", [1, 2, 3], &alloc), + ]); + let mut accessor = TableTestAccessor::::new_empty_with_setup(()); let t = "sxt.t".parse().unwrap(); - accessor.add_table(t, data, 0); + accessor.add_table(t, data.clone(), 0); let lhs_expr: DynProofExpr = column(t, "a", &accessor); let rhs_expr = column(t, "b", &accessor); let lte_expr = lte(lhs_expr, rhs_expr); - let alloc = Bump::new(); - let res = lte_expr.result_evaluate(3, &alloc, &accessor); + let res = lte_expr.result_evaluate(&alloc, &data); let expected_res = Column::Boolean(&[true, false, true]); assert_eq!(res, expected_res); } #[test] fn we_can_compute_the_correct_output_of_a_gte_inequality_expr_using_result_evaluate() { - let data = owned_table([bigint("a", [-1, 9, 1]), bigint("b", [1, 2, 3])]); - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); + let alloc = Bump::new(); + let data = table([ + borrowed_bigint("a", [-1, 9, 1], &alloc), + borrowed_bigint("b", [1, 2, 3], &alloc), + ]); + let mut accessor = TableTestAccessor::::new_empty_with_setup(()); let t = "sxt.t".parse().unwrap(); - accessor.add_table(t, data, 0); + accessor.add_table(t, data.clone(), 0); let col_expr: DynProofExpr = column(t, "a", &accessor); let lit_expr = const_bigint(1); let gte_expr = gte(col_expr, lit_expr); - let alloc = Bump::new(); - let res = gte_expr.result_evaluate(3, &alloc, &accessor); + let res = gte_expr.result_evaluate(&alloc, &data); let expected_res = Column::Boolean(&[false, true, true]); assert_eq!(res, expected_res); } diff --git a/crates/proof-of-sql/src/sql/proof_exprs/literal_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/literal_expr.rs index f5e126364..6534878e1 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/literal_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/literal_expr.rs @@ -2,7 +2,7 @@ use super::ProofExpr; use crate::{ base::{ commitment::Commitment, - database::{Column, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor, LiteralValue}, + database::{Column, ColumnRef, ColumnType, CommitmentAccessor, LiteralValue, Table}, map::IndexSet, proof::ProofError, scalar::Scalar, @@ -47,22 +47,20 @@ impl ProofExpr for LiteralExpr { #[tracing::instrument(name = "LiteralExpr::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a, S: Scalar>( &self, - table_length: usize, alloc: &'a Bump, - _accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - Column::from_literal_with_length(&self.value, table_length, alloc) + Column::from_literal_with_length(&self.value, table.num_rows().unwrap_or(0), alloc) } #[tracing::instrument(name = "LiteralExpr::prover_evaluate", level = "debug", skip_all)] fn prover_evaluate<'a, S: Scalar>( &self, - builder: &mut FinalRoundBuilder<'a, S>, + _builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - _accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let table_length = builder.table_length(); - Column::from_literal_with_length(&self.value, table_length, alloc) + Column::from_literal_with_length(&self.value, table.num_rows().unwrap_or(0), alloc) } fn verifier_evaluate( diff --git a/crates/proof-of-sql/src/sql/proof_exprs/literal_expr_test.rs b/crates/proof-of-sql/src/sql/proof_exprs/literal_expr_test.rs index af5867e26..bd225fad3 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/literal_expr_test.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/literal_expr_test.rs @@ -2,7 +2,10 @@ use super::{DynProofExpr, ProofExpr}; use crate::{ base::{ commitment::InnerProductProof, - database::{owned_table_utility::*, Column, OwnedTableTestAccessor}, + database::{ + owned_table_utility::*, table_utility::*, Column, OwnedTableTestAccessor, Table, + }, + scalar::Curve25519Scalar, }, sql::{ proof::{exercise_verification, VerifiableQueryResult}, @@ -118,12 +121,11 @@ fn we_can_prove_a_query_with_a_single_non_selected_row() { #[test] fn we_can_compute_the_correct_output_of_a_literal_expr_using_result_evaluate() { - let data = owned_table([bigint("a", [123_i64, 456, 789, 1011])]); - let t = "sxt.t".parse().unwrap(); - let accessor = OwnedTableTestAccessor::::new_from_table(t, data, 0, ()); - let literal_expr: DynProofExpr = const_bool(true); let alloc = Bump::new(); - let res = literal_expr.result_evaluate(4, &alloc, &accessor); + let data: Table = + table([borrowed_bigint("a", [123_i64, 456, 789, 1011], &alloc)]); + let literal_expr: DynProofExpr = const_bool(true); + let res = literal_expr.result_evaluate(&alloc, &data); let expected_res = Column::Boolean(&[true, true, true, true]); assert_eq!(res, expected_res); } diff --git a/crates/proof-of-sql/src/sql/proof_exprs/multiply_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/multiply_expr.rs index a1cd937b2..b9e180e3e 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/multiply_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/multiply_expr.rs @@ -3,8 +3,7 @@ use crate::{ base::{ commitment::Commitment, database::{ - try_multiply_column_types, Column, ColumnRef, ColumnType, CommitmentAccessor, - DataAccessor, + try_multiply_column_types, Column, ColumnRef, ColumnType, CommitmentAccessor, Table, }, map::IndexSet, proof::ProofError, @@ -50,12 +49,11 @@ impl ProofExpr for MultiplyExpr { fn result_evaluate<'a, S: Scalar>( &self, - table_length: usize, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let lhs_column: Column<'a, S> = self.lhs.result_evaluate(table_length, alloc, accessor); - let rhs_column: Column<'a, S> = self.rhs.result_evaluate(table_length, alloc, accessor); + let lhs_column: Column<'a, S> = self.lhs.result_evaluate(alloc, table); + let rhs_column: Column<'a, S> = self.rhs.result_evaluate(alloc, table); let scalars = multiply_columns(&lhs_column, &rhs_column, alloc); Column::Scalar(scalars) } @@ -69,10 +67,10 @@ impl ProofExpr for MultiplyExpr { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let lhs_column: Column<'a, S> = self.lhs.prover_evaluate(builder, alloc, accessor); - let rhs_column: Column<'a, S> = self.rhs.prover_evaluate(builder, alloc, accessor); + let lhs_column: Column<'a, S> = self.lhs.prover_evaluate(builder, alloc, table); + let rhs_column: Column<'a, S> = self.rhs.prover_evaluate(builder, alloc, table); // lhs_times_rhs let lhs_times_rhs: &'a [S] = multiply_columns(&lhs_column, &rhs_column, alloc); diff --git a/crates/proof-of-sql/src/sql/proof_exprs/multiply_expr_test.rs b/crates/proof-of-sql/src/sql/proof_exprs/multiply_expr_test.rs index e462aec2e..4a16f451c 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/multiply_expr_test.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/multiply_expr_test.rs @@ -1,7 +1,10 @@ use crate::{ base::{ commitment::InnerProductProof, - database::{owned_table_utility::*, Column, OwnedTableTestAccessor}, + database::{ + owned_table_utility::*, table_utility::*, Column, OwnedTableTestAccessor, + TableTestAccessor, + }, scalar::{test_scalar::TestScalar, Curve25519Scalar}, }, sql::{ @@ -332,20 +335,20 @@ fn we_can_query_random_tables_using_a_non_zero_offset() { // b * (a - 1.5) #[test] fn we_can_compute_the_correct_output_of_a_multiply_expr_using_result_evaluate() { - let data = owned_table([ - smallint("a", [1_i16, 2, 3, 4]), - int("b", [0_i32, 1, 5, 1]), - varchar("d", ["ab", "t", "efg", "g"]), - bigint("c", [0_i64, 2, 2, 0]), + let alloc = Bump::new(); + let data = table([ + borrowed_smallint("a", [1_i16, 2, 3, 4], &alloc), + borrowed_int("b", [0_i32, 1, 5, 1], &alloc), + borrowed_varchar("d", ["ab", "t", "efg", "g"], &alloc), + borrowed_bigint("c", [0_i64, 2, 2, 0], &alloc), ]); let t = "sxt.t".parse().unwrap(); - let accessor = OwnedTableTestAccessor::::new_from_table(t, data, 0, ()); + let accessor = TableTestAccessor::::new_from_table(t, data.clone(), 0, ()); let arithmetic_expr: DynProofExpr = multiply( column(t, "b", &accessor), subtract(column(t, "a", &accessor), const_decimal75(2, 1, 15)), ); - let alloc = Bump::new(); - let res = arithmetic_expr.result_evaluate(4, &alloc, &accessor); + let res = arithmetic_expr.result_evaluate(&alloc, &data); let expected_res_scalar = [0, 5, 75, 25] .iter() .map(|v| Curve25519Scalar::from(*v)) diff --git a/crates/proof-of-sql/src/sql/proof_exprs/not_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/not_expr.rs index e01c3f0d0..c11ebbc1b 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/not_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/not_expr.rs @@ -2,7 +2,7 @@ use super::{DynProofExpr, ProofExpr}; use crate::{ base::{ commitment::Commitment, - database::{Column, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor}, + database::{Column, ColumnRef, ColumnType, CommitmentAccessor, Table}, map::IndexSet, proof::ProofError, scalar::Scalar, @@ -38,11 +38,10 @@ impl ProofExpr for NotExpr { #[tracing::instrument(name = "NotExpr::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a, S: Scalar>( &self, - table_length: usize, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let expr_column: Column<'a, S> = self.expr.result_evaluate(table_length, alloc, accessor); + let expr_column: Column<'a, S> = self.expr.result_evaluate(alloc, table); let expr = expr_column.as_boolean().expect("expr is not boolean"); Column::Boolean(alloc.alloc_slice_fill_with(expr.len(), |i| !expr[i])) } @@ -52,9 +51,9 @@ impl ProofExpr for NotExpr { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let expr_column: Column<'a, S> = self.expr.prover_evaluate(builder, alloc, accessor); + let expr_column: Column<'a, S> = self.expr.prover_evaluate(builder, alloc, table); let expr = expr_column.as_boolean().expect("expr is not boolean"); Column::Boolean(alloc.alloc_slice_fill_with(expr.len(), |i| !expr[i])) } diff --git a/crates/proof-of-sql/src/sql/proof_exprs/not_expr_test.rs b/crates/proof-of-sql/src/sql/proof_exprs/not_expr_test.rs index c462d3f9b..6af6b9a69 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/not_expr_test.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/not_expr_test.rs @@ -1,7 +1,10 @@ use crate::{ base::{ commitment::InnerProductProof, - database::{owned_table_utility::*, Column, OwnedTableTestAccessor, TestAccessor}, + database::{ + owned_table_utility::*, table_utility::*, Column, OwnedTableTestAccessor, + TableTestAccessor, TestAccessor, + }, scalar::test_scalar::TestScalar, }, sql::{ @@ -109,17 +112,17 @@ fn we_can_query_random_tables_with_a_non_zero_offset() { #[test] fn we_can_compute_the_correct_output_of_a_not_expr_using_result_evaluate() { - let data = owned_table([ - bigint("a", [123, 456]), - bigint("b", [0, 1]), - varchar("d", ["alfa", "gama"]), + let alloc = Bump::new(); + let data = table([ + borrowed_bigint("a", [123, 456], &alloc), + borrowed_bigint("b", [0, 1], &alloc), + borrowed_varchar("d", ["alfa", "gama"], &alloc), ]); - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); + let mut accessor = TableTestAccessor::::new_empty_with_setup(()); let t = "sxt.t".parse().unwrap(); - accessor.add_table(t, data, 0); + accessor.add_table(t, data.clone(), 0); let not_expr: DynProofExpr = not(equal(column(t, "b", &accessor), const_int128(1))); - let alloc = Bump::new(); - let res = not_expr.result_evaluate(2, &alloc, &accessor); + let res = not_expr.result_evaluate(&alloc, &data); let expected_res = Column::Boolean(&[true, false]); assert_eq!(res, expected_res); } diff --git a/crates/proof-of-sql/src/sql/proof_exprs/or_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/or_expr.rs index 2a6330ea0..ccda6c85c 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/or_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/or_expr.rs @@ -2,7 +2,7 @@ use super::{DynProofExpr, ProofExpr}; use crate::{ base::{ commitment::Commitment, - database::{Column, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor}, + database::{Column, ColumnRef, ColumnType, CommitmentAccessor, Table}, map::IndexSet, proof::ProofError, scalar::Scalar, @@ -42,15 +42,19 @@ impl ProofExpr for OrExpr { #[tracing::instrument(name = "OrExpr::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a, S: Scalar>( &self, - table_length: usize, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let lhs_column: Column<'a, S> = self.lhs.result_evaluate(table_length, alloc, accessor); - let rhs_column: Column<'a, S> = self.rhs.result_evaluate(table_length, alloc, accessor); + let lhs_column: Column<'a, S> = self.lhs.result_evaluate(alloc, table); + let rhs_column: Column<'a, S> = self.rhs.result_evaluate(alloc, table); let lhs = lhs_column.as_boolean().expect("lhs is not boolean"); let rhs = rhs_column.as_boolean().expect("rhs is not boolean"); - Column::Boolean(result_evaluate_or(table_length, alloc, lhs, rhs)) + Column::Boolean(result_evaluate_or( + table.num_rows().unwrap_or(0), + alloc, + lhs, + rhs, + )) } #[tracing::instrument(name = "OrExpr::prover_evaluate", level = "debug", skip_all)] @@ -58,10 +62,10 @@ impl ProofExpr for OrExpr { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S> { - let lhs_column: Column<'a, S> = self.lhs.prover_evaluate(builder, alloc, accessor); - let rhs_column: Column<'a, S> = self.rhs.prover_evaluate(builder, alloc, accessor); + let lhs_column: Column<'a, S> = self.lhs.prover_evaluate(builder, alloc, table); + let rhs_column: Column<'a, S> = self.rhs.prover_evaluate(builder, alloc, table); let lhs = lhs_column.as_boolean().expect("lhs is not boolean"); let rhs = rhs_column.as_boolean().expect("rhs is not boolean"); Column::Boolean(prover_evaluate_or(builder, alloc, lhs, rhs)) diff --git a/crates/proof-of-sql/src/sql/proof_exprs/or_expr_test.rs b/crates/proof-of-sql/src/sql/proof_exprs/or_expr_test.rs index 5c2ca738a..60bddef9b 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/or_expr_test.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/or_expr_test.rs @@ -1,7 +1,10 @@ use crate::{ base::{ commitment::InnerProductProof, - database::{owned_table_utility::*, Column, OwnedTableTestAccessor, TestAccessor}, + database::{ + owned_table_utility::*, table_utility::*, Column, OwnedTableTestAccessor, + TableTestAccessor, TestAccessor, + }, }, sql::{ proof::{exercise_verification, VerifiableQueryResult}, @@ -169,21 +172,21 @@ fn we_can_query_random_tables_with_a_non_zero_offset() { #[test] fn we_can_compute_the_correct_output_of_an_or_expr_using_result_evaluate() { - let data = owned_table([ - bigint("a", [1, 2, 3, 4]), - bigint("b", [0, 1, 0, 1]), - bigint("c", [0, 2, 2, 0]), - varchar("d", ["ab", "t", "g", "efg"]), + let alloc = Bump::new(); + let data = table([ + borrowed_bigint("a", [1, 2, 3, 4], &alloc), + borrowed_bigint("b", [0, 1, 0, 1], &alloc), + borrowed_bigint("c", [0, 2, 2, 0], &alloc), + borrowed_varchar("d", ["ab", "t", "g", "efg"], &alloc), ]); - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); + let mut accessor = TableTestAccessor::::new_empty_with_setup(()); let t = "sxt.t".parse().unwrap(); - accessor.add_table(t, data, 0); + accessor.add_table(t, data.clone(), 0); let and_expr: DynProofExpr = or( equal(column(t, "b", &accessor), const_int128(1)), equal(column(t, "d", &accessor), const_varchar("g")), ); - let alloc = Bump::new(); - let res = and_expr.result_evaluate(4, &alloc, &accessor); + let res = and_expr.result_evaluate(&alloc, &data); let expected_res = Column::Boolean(&[false, true, true, true]); assert_eq!(res, expected_res); } diff --git a/crates/proof-of-sql/src/sql/proof_exprs/proof_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/proof_expr.rs index 3ca5a8408..fd91651db 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/proof_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/proof_expr.rs @@ -1,7 +1,7 @@ use crate::{ base::{ commitment::Commitment, - database::{Column, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor}, + database::{Column, ColumnRef, ColumnType, CommitmentAccessor, Table}, map::IndexSet, proof::ProofError, scalar::Scalar, @@ -25,9 +25,8 @@ pub trait ProofExpr: Debug + Send + Sync { /// Implementations must ensure that the returned slice has length `table_length`. fn result_evaluate<'a, S: Scalar>( &self, - table_length: usize, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S>; /// Evaluate the expression, add components needed to prove it, and return thet resulting column @@ -36,7 +35,7 @@ pub trait ProofExpr: Debug + Send + Sync { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table: &Table<'a, S>, ) -> Column<'a, S>; /// Compute the evaluation of a multilinear extension from this expression diff --git a/crates/proof-of-sql/src/sql/proof_exprs/proof_expr_test.rs b/crates/proof-of-sql/src/sql/proof_exprs/proof_expr_test.rs index c9d459bda..3ddcaa255 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/proof_expr_test.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/proof_expr_test.rs @@ -1,31 +1,38 @@ use super::{test_utility::*, DynProofExpr, ProofExpr}; use crate::base::{ commitment::InnerProductProof, - database::{owned_table_utility::*, Column, OwnedTableTestAccessor, TestAccessor}, + database::{table_utility::*, Column, TableTestAccessor, TestAccessor}, }; use bumpalo::Bump; #[test] fn we_can_compute_the_correct_result_of_a_complex_bool_expr_using_result_evaluate() { - let data = owned_table([ - bigint("a", [1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 8, 9, 999]), - varchar( + let alloc = Bump::new(); + let data = table([ + borrowed_bigint( + "a", + [1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 8, 9, 999], + &alloc, + ), + borrowed_varchar( "b", [ "g", "g", "t", "ghi", "g", "g", "jj", "f", "g", "g", "gar", "qwe", "g", "g", "poi", "zxc", "999", ], + &alloc, ), - int128( + borrowed_int128( "c", [ 3, 123, 3, 234, 3, 345, 3, 456, 3, 567, 3, 678, 3, 789, 3, 890, 999, ], + &alloc, ), ]); - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); + let mut accessor = TableTestAccessor::::new_empty_with_setup(()); let t = "sxt.t".parse().unwrap(); - accessor.add_table(t, data, 0); + accessor.add_table(t, data.clone(), 0); // (a <= 5 || b == "g") && c != 3 let bool_expr: DynProofExpr = and( or( @@ -34,8 +41,7 @@ fn we_can_compute_the_correct_result_of_a_complex_bool_expr_using_result_evaluat ), not(equal(column(t, "c", &accessor), const_int128(3))), ); - let alloc = Bump::new(); - let res = bool_expr.result_evaluate(17, &alloc, &accessor); + let res = bool_expr.result_evaluate(&alloc, &data); let expected_res = Column::Boolean(&[ false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, false, false, diff --git a/crates/proof-of-sql/src/sql/proof_plans/empty_exec.rs b/crates/proof-of-sql/src/sql/proof_plans/empty_exec.rs index 575c781c8..cfe1df3e0 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/empty_exec.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/empty_exec.rs @@ -68,7 +68,6 @@ impl ProverEvaluate for EmptyExec { #[tracing::instrument(name = "EmptyExec::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a, S: Scalar>( &self, - _input_length: usize, _alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { diff --git a/crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs b/crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs index 5ee0cd40f..93ae93196 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs @@ -4,7 +4,7 @@ use crate::{ commitment::Commitment, database::{ filter_util::filter_columns, Column, ColumnField, ColumnRef, CommitmentAccessor, - DataAccessor, OwnedTable, TableRef, + DataAccessor, OwnedTable, Table, TableRef, }, map::IndexSet, proof::ProofError, @@ -140,14 +140,17 @@ impl ProverEvaluate for FilterExec { #[tracing::instrument(name = "FilterExec::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a, S: Scalar>( &self, - input_length: usize, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { + let column_refs = self.get_column_references(); + let used_table = Table::<'a, S>::try_from_iter(column_refs.iter().map(|column_ref| { + let column = accessor.get_column(*column_ref); + (column_ref.column_id(), column) + })) + .expect("Failed to create table from column references"); // 1. selection - let selection_column: Column<'a, S> = - self.where_clause - .result_evaluate(input_length, alloc, accessor); + let selection_column: Column<'a, S> = self.where_clause.result_evaluate(alloc, &used_table); let selection = selection_column .as_boolean() .expect("selection is not boolean"); @@ -156,11 +159,7 @@ impl ProverEvaluate for FilterExec { let columns: Vec<_> = self .aliased_results .iter() - .map(|aliased_expr| { - aliased_expr - .expr - .result_evaluate(input_length, alloc, accessor) - }) + .map(|aliased_expr| aliased_expr.expr.result_evaluate(alloc, &used_table)) .collect(); // Compute filtered_columns and indexes @@ -180,9 +179,16 @@ impl ProverEvaluate for FilterExec { alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { + let column_refs = self.get_column_references(); + let used_table = Table::<'a, S>::try_from_iter(column_refs.iter().map(|column_ref| { + let column = accessor.get_column(*column_ref); + (column_ref.column_id(), column) + })) + .expect("Failed to create table from column references"); // 1. selection let selection_column: Column<'a, S> = - self.where_clause.prover_evaluate(builder, alloc, accessor); + self.where_clause + .prover_evaluate(builder, alloc, &used_table); let selection = selection_column .as_boolean() .expect("selection is not boolean"); @@ -191,7 +197,11 @@ impl ProverEvaluate for FilterExec { let columns: Vec<_> = self .aliased_results .iter() - .map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, accessor)) + .map(|aliased_expr| { + aliased_expr + .expr + .prover_evaluate(builder, alloc, &used_table) + }) .collect(); // Compute filtered_columns let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection); diff --git a/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs b/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs index f441355d4..ca52a65c3 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs @@ -194,7 +194,7 @@ fn we_can_get_an_empty_result_from_a_basic_filter_on_an_empty_table_using_result where_clause, ); let alloc = Bump::new(); - let result_cols = expr.result_evaluate(0, &alloc, &accessor); + let result_cols = expr.result_evaluate(&alloc, &accessor); let output_length = result_cols.first().map_or(0, Column::len) as u64; let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); @@ -240,7 +240,7 @@ fn we_can_get_an_empty_result_from_a_basic_filter_using_result_evaluate() { where_clause, ); let alloc = Bump::new(); - let result_cols = expr.result_evaluate(5, &alloc, &accessor); + let result_cols = expr.result_evaluate(&alloc, &accessor); let output_length = result_cols.first().map_or(0, Column::len) as u64; let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); @@ -282,7 +282,7 @@ fn we_can_get_no_columns_from_a_basic_filter_with_no_selected_columns_using_resu let where_clause: DynProofExpr = equal(column(t, "a", &accessor), const_int128(5)); let expr = filter(cols_expr_plan(t, &[], &accessor), tab(t), where_clause); let alloc = Bump::new(); - let result_cols = expr.result_evaluate(5, &alloc, &accessor); + let result_cols = expr.result_evaluate(&alloc, &accessor); let output_length = result_cols.first().map_or(0, Column::len) as u64; let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); @@ -314,7 +314,7 @@ fn we_can_get_the_correct_result_from_a_basic_filter_using_result_evaluate() { where_clause, ); let alloc = Bump::new(); - let result_cols = expr.result_evaluate(5, &alloc, &accessor); + let result_cols = expr.result_evaluate(&alloc, &accessor); let output_length = result_cols.first().map_or(0, Column::len) as u64; let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); diff --git a/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test_dishonest_prover.rs b/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test_dishonest_prover.rs index 29dfb1d48..234b22658 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test_dishonest_prover.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test_dishonest_prover.rs @@ -1,17 +1,18 @@ use super::{filter_exec::prove_filter, OstensibleFilterExec}; -use crate::base::{database::owned_table_utility::*, scalar::Scalar}; use crate::{ base::{ - database::{filter_util::*, Column, DataAccessor, OwnedTableTestAccessor, TestAccessor}, + database::{ + filter_util::*, owned_table_utility::*, Column, DataAccessor, OwnedTableTestAccessor, + Table, TestAccessor, + }, proof::ProofError, + scalar::Scalar, }, sql::{ proof::{ - FinalRoundBuilder, FirstRoundBuilder, ProverEvaluate, ProverHonestyMarker, QueryError, - VerifiableQueryResult, + FinalRoundBuilder, FirstRoundBuilder, ProofPlan, ProverEvaluate, ProverHonestyMarker, + QueryError, VerifiableQueryResult, }, - // Making this explicit to ensure that we don't accidentally use the - // sparse filter for these tests proof_exprs::{ test_utility::{cols_expr_plan, column, const_int128, equal, tab}, ProofExpr, @@ -34,14 +35,17 @@ impl ProverEvaluate for DishonestFilterExec { )] fn result_evaluate<'a, S: Scalar>( &self, - input_length: usize, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { + let column_refs = self.get_column_references(); + let used_table = Table::<'a, S>::try_from_iter(column_refs.iter().map(|column_ref| { + let column = accessor.get_column(*column_ref); + (column_ref.column_id(), column) + })) + .expect("Failed to create table from column references"); // 1. selection - let selection_column: Column<'a, S> = - self.where_clause - .result_evaluate(input_length, alloc, accessor); + let selection_column: Column<'a, S> = self.where_clause.result_evaluate(alloc, &used_table); let selection = selection_column .as_boolean() .expect("selection is not boolean"); @@ -49,11 +53,7 @@ impl ProverEvaluate for DishonestFilterExec { let columns: Vec<_> = self .aliased_results .iter() - .map(|aliased_expr| { - aliased_expr - .expr - .result_evaluate(input_length, alloc, accessor) - }) + .map(|aliased_expr| aliased_expr.expr.result_evaluate(alloc, &used_table)) .collect(); // Compute filtered_columns let (filtered_columns, _) = filter_columns(alloc, &columns, selection); @@ -77,9 +77,16 @@ impl ProverEvaluate for DishonestFilterExec { alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { + let column_refs = self.get_column_references(); + let used_table = Table::<'a, S>::try_from_iter(column_refs.iter().map(|column_ref| { + let column = accessor.get_column(*column_ref); + (column_ref.column_id(), column) + })) + .expect("Failed to create table from column references"); // 1. selection let selection_column: Column<'a, S> = - self.where_clause.prover_evaluate(builder, alloc, accessor); + self.where_clause + .prover_evaluate(builder, alloc, &used_table); let selection = selection_column .as_boolean() .expect("selection is not boolean"); @@ -87,7 +94,11 @@ impl ProverEvaluate for DishonestFilterExec { let columns: Vec<_> = self .aliased_results .iter() - .map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, accessor)) + .map(|aliased_expr| { + aliased_expr + .expr + .prover_evaluate(builder, alloc, &used_table) + }) .collect(); // Compute filtered_columns let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection); diff --git a/crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs b/crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs index bc369377f..9134a0391 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs @@ -7,7 +7,7 @@ use crate::{ aggregate_columns, compare_indexes_by_owned_columns, AggregatedColumns, }, Column, ColumnField, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor, - OwnedTable, TableRef, + OwnedTable, Table, TableRef, }, map::IndexSet, proof::ProofError, @@ -200,14 +200,17 @@ impl ProverEvaluate for GroupByExec { #[tracing::instrument(name = "GroupByExec::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a, S: Scalar>( &self, - input_length: usize, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { + let column_refs = self.get_column_references(); + let used_table = Table::<'a, S>::try_from_iter(column_refs.iter().map(|column_ref| { + let column = accessor.get_column(*column_ref); + (column_ref.column_id(), column) + })) + .expect("Failed to create table from column references"); // 1. selection - let selection_column: Column<'a, S> = - self.where_clause - .result_evaluate(input_length, alloc, accessor); + let selection_column: Column<'a, S> = self.where_clause.result_evaluate(alloc, &used_table); let selection = selection_column .as_boolean() @@ -217,16 +220,12 @@ impl ProverEvaluate for GroupByExec { let group_by_columns = self .group_by_exprs .iter() - .map(|expr| expr.result_evaluate(input_length, alloc, accessor)) + .map(|expr| expr.result_evaluate(alloc, &used_table)) .collect::>(); let sum_columns = self .sum_expr .iter() - .map(|aliased_expr| { - aliased_expr - .expr - .result_evaluate(input_length, alloc, accessor) - }) + .map(|aliased_expr| aliased_expr.expr.result_evaluate(alloc, &used_table)) .collect::>(); // Compute filtered_columns let AggregatedColumns { @@ -256,9 +255,16 @@ impl ProverEvaluate for GroupByExec { alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { + let column_refs = self.get_column_references(); + let used_table = Table::<'a, S>::try_from_iter(column_refs.iter().map(|column_ref| { + let column = accessor.get_column(*column_ref); + (column_ref.column_id(), column) + })) + .expect("Failed to create table from column references"); // 1. selection let selection_column: Column<'a, S> = - self.where_clause.prover_evaluate(builder, alloc, accessor); + self.where_clause + .prover_evaluate(builder, alloc, &used_table); let selection = selection_column .as_boolean() .expect("selection is not boolean"); @@ -267,12 +273,16 @@ impl ProverEvaluate for GroupByExec { let group_by_columns = self .group_by_exprs .iter() - .map(|expr| expr.prover_evaluate(builder, alloc, accessor)) + .map(|expr| expr.prover_evaluate(builder, alloc, &used_table)) .collect::>(); let sum_columns = self .sum_expr .iter() - .map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, accessor)) + .map(|aliased_expr| { + aliased_expr + .expr + .prover_evaluate(builder, alloc, &used_table) + }) .collect::>(); // 3. Compute filtered_columns let AggregatedColumns { diff --git a/crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs b/crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs index dd40414ea..c82f28b2b 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs @@ -2,7 +2,8 @@ use crate::{ base::{ commitment::Commitment, database::{ - Column, ColumnField, ColumnRef, CommitmentAccessor, DataAccessor, OwnedTable, TableRef, + Column, ColumnField, ColumnRef, CommitmentAccessor, DataAccessor, OwnedTable, Table, + TableRef, }, map::IndexSet, proof::ProofError, @@ -90,18 +91,19 @@ impl ProverEvaluate for ProjectionExec { #[tracing::instrument(name = "ProjectionExec::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a, S: Scalar>( &self, - input_length: usize, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { + let column_refs = self.get_column_references(); + let used_table = Table::<'a, S>::try_from_iter(column_refs.iter().map(|column_ref| { + let column = accessor.get_column(*column_ref); + (column_ref.column_id(), column) + })) + .expect("Failed to create table from column references"); let columns: Vec<_> = self .aliased_results .iter() - .map(|aliased_expr| { - aliased_expr - .expr - .result_evaluate(input_length, alloc, accessor) - }) + .map(|aliased_expr| aliased_expr.expr.result_evaluate(alloc, &used_table)) .collect(); columns } @@ -120,11 +122,21 @@ impl ProverEvaluate for ProjectionExec { alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { + let column_refs = self.get_column_references(); + let used_table = Table::<'a, S>::try_from_iter(column_refs.iter().map(|column_ref| { + let column = accessor.get_column(*column_ref); + (column_ref.column_id(), column) + })) + .expect("Failed to create table from column references"); // 1. Evaluate result expressions let res: Vec<_> = self .aliased_results .iter() - .map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, accessor)) + .map(|aliased_expr| { + aliased_expr + .expr + .prover_evaluate(builder, alloc, &used_table) + }) .collect(); // 2. Produce MLEs res.clone().into_iter().for_each(|column| { diff --git a/crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs b/crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs index 4c733a762..dede8b063 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs @@ -168,7 +168,7 @@ fn we_can_get_an_empty_result_from_a_basic_projection_on_an_empty_table_using_re let expr: DynProofPlan = projection(cols_expr_plan(t, &["b", "c", "d", "e"], &accessor), tab(t)); let alloc = Bump::new(); - let result_cols = expr.result_evaluate(0, &alloc, &accessor); + let result_cols = expr.result_evaluate(&alloc, &accessor); let output_length = result_cols.first().map_or(0, Column::len) as u64; let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); @@ -209,7 +209,7 @@ fn we_can_get_no_columns_from_a_basic_projection_with_no_selected_columns_using_ accessor.add_table(t, data, 0); let expr: DynProofPlan = projection(cols_expr_plan(t, &[], &accessor), tab(t)); let alloc = Bump::new(); - let result_cols = expr.result_evaluate(5, &alloc, &accessor); + let result_cols = expr.result_evaluate(&alloc, &accessor); let output_length = result_cols.first().map_or(0, Column::len) as u64; let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); @@ -247,7 +247,7 @@ fn we_can_get_the_correct_result_from_a_basic_projection_using_result_evaluate() tab(t), ); let alloc = Bump::new(); - let result_cols = expr.result_evaluate(5, &alloc, &accessor); + let result_cols = expr.result_evaluate(&alloc, &accessor); let output_length = result_cols.first().map_or(0, Column::len) as u64; let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder);