Skip to content

Commit

Permalink
refactor!: replace DataAccessor with Table in ProofExpr && remo…
Browse files Browse the repository at this point in the history
…ve `table_length` from `ProofPlan::result_evaluate`
  • Loading branch information
iajoiner committed Nov 12, 2024
1 parent 3262b8d commit ed5d485
Show file tree
Hide file tree
Showing 32 changed files with 308 additions and 235 deletions.
3 changes: 1 addition & 2 deletions crates/proof-of-sql/src/sql/proof/proof_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>,
) -> Vec<Column<'a, S>>;
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/sql/proof/query_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
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);

Expand Down
4 changes: 0 additions & 4 deletions crates/proof-of-sql/src/sql/proof/query_proof_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>,
) -> Vec<Column<'a, S>> {
Expand Down Expand Up @@ -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<S>,
) -> Vec<Column<'a, S>> {
Expand Down Expand Up @@ -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<S>,
) -> Vec<Column<'a, S>> {
Expand Down Expand Up @@ -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<S>,
) -> Vec<Column<'a, S>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>,
) -> Vec<Column<'a, S>> {
Expand Down
16 changes: 7 additions & 9 deletions crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<S>,
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,
Expand All @@ -74,10 +72,10 @@ impl ProofExpr for AddSubtractExpr {
&self,
builder: &mut FinalRoundBuilder<'a, S>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
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,
Expand Down
21 changes: 12 additions & 9 deletions crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr_test.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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::<InnerProductProof>::new_from_table(t, data, 0, ());
let accessor = TableTestAccessor::<InnerProductProof>::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))
Expand Down
11 changes: 5 additions & 6 deletions crates/proof-of-sql/src/sql/proof_exprs/aggregate_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -46,21 +46,20 @@ 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<S>,
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)]
fn prover_evaluate<'a, S: Scalar>(
&self,
builder: &mut FinalRoundBuilder<'a, S>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
table: &Table<'a, S>,
) -> Column<'a, S> {
self.expr.prover_evaluate(builder, alloc, accessor)
self.expr.prover_evaluate(builder, alloc, table)
}

fn verifier_evaluate<C: Commitment>(
Expand Down
19 changes: 10 additions & 9 deletions crates/proof-of-sql/src/sql/proof_exprs/and_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -44,26 +44,27 @@ 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<S>,
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)]
fn prover_evaluate<'a, S: Scalar>(
&self,
builder: &mut FinalRoundBuilder<'a, S>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
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();
Expand Down
21 changes: 12 additions & 9 deletions crates/proof-of-sql/src/sql/proof_exprs/and_expr_test.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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::<InnerProductProof>::new_from_table(t, data, 0, ());
let accessor = TableTestAccessor::<InnerProductProof>::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);
}
21 changes: 13 additions & 8 deletions crates/proof-of-sql/src/sql/proof_exprs/column_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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<S>,
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
Expand All @@ -73,9 +78,9 @@ impl ProofExpr for ColumnExpr {
&self,
builder: &mut FinalRoundBuilder<'a, S>,
_alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
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
}
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/sql/proof_exprs/dyn_proof_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 12 additions & 9 deletions crates/proof-of-sql/src/sql/proof_exprs/equals_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -43,28 +43,31 @@ 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<S>,
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)]
fn prover_evaluate<'a, S: Scalar>(
&self,
builder: &mut FinalRoundBuilder<'a, S>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
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)
Expand Down
Loading

0 comments on commit ed5d485

Please sign in to comment.