Skip to content

Commit

Permalink
feat!: allow ProofPlan::result_evaluate and `ProofPlan::prover_eval…
Browse files Browse the repository at this point in the history
…uate` to return columns (#132)

# Rationale for this change
This is a simple PR to make it possible to compose `ProofPlan`s which
facilitates `SliceExec` and `UnionExec`.
<!--
Why are you proposing this change? If this is already explained clearly
in the linked Jira ticket then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

# What changes are included in this PR?
- make `ProofPlan::result_evaluate` and `ProofPlan::prover_evaluate`
return `Vec<Column<'a, S>>`
<!--
There is no need to duplicate the description in the ticket here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

# Are these changes tested?
Existing tests should pass
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
  • Loading branch information
iajoiner authored Sep 6, 2024
1 parent 5b783af commit 2a19752
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 62 deletions.
7 changes: 4 additions & 3 deletions crates/proof-of-sql/src/sql/proof/proof_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use super::{CountBuilder, ProofBuilder, ResultBuilder, VerificationBuilder};
use crate::base::{
commitment::Commitment,
database::{
ColumnField, ColumnRef, CommitmentAccessor, DataAccessor, MetadataAccessor, OwnedTable,
Column, ColumnField, ColumnRef, CommitmentAccessor, DataAccessor, MetadataAccessor,
OwnedTable,
},
proof::ProofError,
scalar::Scalar,
Expand Down Expand Up @@ -53,7 +54,7 @@ pub trait ProverEvaluate<S: Scalar> {
builder: &mut ResultBuilder<'a>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
);
) -> Vec<Column<'a, S>>;

/// Evaluate the query and modify `ProofBuilder` to store an intermediate representation
/// of the query result and track all the components needed to form the query's proof.
Expand All @@ -66,7 +67,7 @@ pub trait ProverEvaluate<S: Scalar> {
builder: &mut ProofBuilder<'a, S>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
);
) -> Vec<Column<'a, S>>;
}

/// Marker used as a trait bound for generic [`ProofPlan`] types to indicate the honesty of their implementation.
Expand Down
35 changes: 23 additions & 12 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,8 +6,9 @@ use crate::{
commitment::{Commitment, InnerProductProof},
database::{
owned_table_utility::{bigint, owned_table},
ColumnField, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor, MetadataAccessor,
OwnedTable, OwnedTableTestAccessor, TestAccessor, UnimplementedTestAccessor,
Column, ColumnField, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor,
MetadataAccessor, OwnedTable, OwnedTableTestAccessor, TestAccessor,
UnimplementedTestAccessor,
},
proof::ProofError,
scalar::{Curve25519Scalar, Scalar},
Expand Down Expand Up @@ -45,24 +46,26 @@ impl<S: Scalar> ProverEvaluate<S> for TrivialTestProofPlan {
builder: &mut ResultBuilder<'a>,
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) {
) -> Vec<Column<'a, S>> {
let col = alloc.alloc_slice_fill_copy(builder.table_length(), self.column_fill_value);
let indexes = Indexes::Sparse(vec![0u64]);
builder.set_result_indexes(indexes);
builder.produce_result_column(col as &[_]);
vec![Column::BigInt(col)]
}

fn prover_evaluate<'a>(
&self,
builder: &mut ProofBuilder<'a, S>,
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) {
) -> Vec<Column<'a, S>> {
let col = alloc.alloc_slice_fill_copy(builder.table_length(), self.column_fill_value);
builder.produce_sumcheck_subpolynomial(
SumcheckSubpolynomialType::Identity,
vec![(S::ONE, vec![Box::new(col as &[_])])],
);
vec![Column::BigInt(col)]
}
}
impl<C: Commitment> ProofPlan<C> for TrivialTestProofPlan {
Expand Down Expand Up @@ -208,19 +211,21 @@ impl<S: Scalar> ProverEvaluate<S> for SquareTestProofPlan {
fn result_evaluate<'a>(
&self,
builder: &mut ResultBuilder<'a>,
_alloc: &'a Bump,
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) {
) -> Vec<Column<'a, S>> {
builder.set_result_indexes(Indexes::Sparse(vec![0, 1]));
builder.produce_result_column(self.res);
let res: &[_] = alloc.alloc_slice_copy(&self.res);
vec![Column::BigInt(res)]
}

fn prover_evaluate<'a>(
&self,
builder: &mut ProofBuilder<'a, S>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
) {
) -> Vec<Column<'a, S>> {
let x = accessor.get_column(ColumnRef::new(
"sxt.test".parse().unwrap(),
"x".parse().unwrap(),
Expand All @@ -235,6 +240,7 @@ impl<S: Scalar> ProverEvaluate<S> for SquareTestProofPlan {
(-S::ONE, vec![Box::new(x.clone()), Box::new(x)]),
],
);
vec![Column::BigInt(res)]
}
}
impl<C: Commitment> ProofPlan<C> for SquareTestProofPlan {
Expand Down Expand Up @@ -384,19 +390,21 @@ impl<S: Scalar> ProverEvaluate<S> for DoubleSquareTestProofPlan {
fn result_evaluate<'a>(
&self,
builder: &mut ResultBuilder<'a>,
_alloc: &'a Bump,
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) {
) -> Vec<Column<'a, S>> {
builder.set_result_indexes(Indexes::Sparse(vec![0, 1]));
builder.produce_result_column(self.res);
let res: &[_] = alloc.alloc_slice_copy(&self.res);
vec![Column::BigInt(res)]
}

fn prover_evaluate<'a>(
&self,
builder: &mut ProofBuilder<'a, S>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
) {
) -> Vec<Column<'a, S>> {
let x = accessor.get_column(ColumnRef::new(
"sxt.test".parse().unwrap(),
"x".parse().unwrap(),
Expand Down Expand Up @@ -424,6 +432,7 @@ impl<S: Scalar> ProverEvaluate<S> for DoubleSquareTestProofPlan {
(-S::ONE, vec![Box::new(z), Box::new(z)]),
],
);
vec![Column::BigInt(res)]
}
}
impl<C: Commitment> ProofPlan<C> for DoubleSquareTestProofPlan {
Expand Down Expand Up @@ -591,18 +600,19 @@ impl<S: Scalar> ProverEvaluate<S> for ChallengeTestProofPlan {
builder: &mut ResultBuilder<'a>,
_alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) {
) -> Vec<Column<'a, S>> {
builder.set_result_indexes(Indexes::Sparse(vec![0, 1]));
builder.produce_result_column([9, 25]);
builder.request_post_result_challenges(2);
vec![Column::BigInt(&[9, 25])]
}

fn prover_evaluate<'a>(
&self,
builder: &mut ProofBuilder<'a, S>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
) {
) -> Vec<Column<'a, S>> {
let x = accessor.get_column(ColumnRef::new(
"sxt.test".parse().unwrap(),
"x".parse().unwrap(),
Expand All @@ -619,6 +629,7 @@ impl<S: Scalar> ProverEvaluate<S> for ChallengeTestProofPlan {
(-alpha, vec![Box::new(x.clone()), Box::new(x)]),
],
);
vec![Column::BigInt(&[9, 25])]
}
}
impl<C: Commitment> ProofPlan<C> for ChallengeTestProofPlan {
Expand Down
18 changes: 12 additions & 6 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,8 @@ use crate::{
commitment::{Commitment, InnerProductProof},
database::{
owned_table_utility::{bigint, owned_table},
ColumnField, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor, MetadataAccessor,
OwnedTable, TestAccessor, UnimplementedTestAccessor,
Column, ColumnField, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor,
MetadataAccessor, OwnedTable, TestAccessor, UnimplementedTestAccessor,
},
proof::ProofError,
scalar::Scalar,
Expand All @@ -28,16 +28,22 @@ impl<S: Scalar> ProverEvaluate<S> for EmptyTestQueryExpr {
fn result_evaluate<'a>(
&self,
_builder: &mut ResultBuilder<'a>,
_alloc: &'a Bump,
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) {
) -> Vec<Column<'a, S>> {
let zeros = vec![0; self.length];
let res: &[_] = alloc.alloc_slice_copy(&zeros);
vec![Column::BigInt(res); self.columns]
}
fn prover_evaluate<'a>(
&self,
_builder: &mut ProofBuilder<'a, S>,
_alloc: &'a Bump,
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) {
) -> Vec<Column<'a, S>> {
let zeros = vec![0; self.length];
let res: &[_] = alloc.alloc_slice_copy(&zeros);
vec![Column::BigInt(res); self.columns]
}
}
impl<C: Commitment> ProofPlan<C> for EmptyTestQueryExpr {
Expand Down
10 changes: 6 additions & 4 deletions crates/proof-of-sql/src/sql/proof_plans/dense_filter_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for DenseFilterExec<C> {
builder: &mut ResultBuilder<'a>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) {
) -> Vec<Column<'a, C::Scalar>> {
// 1. selection
let selection_column: Column<'a, C::Scalar> =
self.where_clause
Expand All @@ -172,10 +172,11 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for DenseFilterExec<C> {
// 3. set indexes
builder.set_result_indexes(Indexes::Dense(0..(result_len as u64)));
// 4. set filtered_columns
for col in filtered_columns {
builder.produce_result_column(col);
for col in &filtered_columns {
builder.produce_result_column(col.clone());
}
builder.request_post_result_challenges(2);
filtered_columns
}

#[tracing::instrument(name = "DenseFilterExec::prover_evaluate", level = "debug", skip_all)]
Expand All @@ -185,7 +186,7 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for DenseFilterExec<C> {
builder: &mut ProofBuilder<'a, C::Scalar>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) {
) -> Vec<Column<'a, C::Scalar>> {
// 1. selection
let selection_column: Column<'a, C::Scalar> =
self.where_clause.prover_evaluate(builder, alloc, accessor);
Expand Down Expand Up @@ -215,6 +216,7 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for DenseFilterExec<C> {
&filtered_columns,
result_len,
);
filtered_columns
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl ProverEvaluate<Curve25519Scalar> for DishonestDenseFilterExec<RistrettoPoin
builder: &mut ResultBuilder<'a>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<Curve25519Scalar>,
) {
) -> Vec<Column<'a, Curve25519Scalar>> {
// 1. selection
let selection_column: Column<'a, Curve25519Scalar> =
self.where_clause
Expand All @@ -60,10 +60,11 @@ impl ProverEvaluate<Curve25519Scalar> for DishonestDenseFilterExec<RistrettoPoin
// 3. set indexes
builder.set_result_indexes(Indexes::Dense(0..(result_len as u64)));
// 4. set filtered_columns
for col in filtered_columns {
builder.produce_result_column(col);
for col in &filtered_columns {
builder.produce_result_column(col.clone());
}
builder.request_post_result_challenges(2);
filtered_columns
}

#[tracing::instrument(
Expand All @@ -77,7 +78,7 @@ impl ProverEvaluate<Curve25519Scalar> for DishonestDenseFilterExec<RistrettoPoin
builder: &mut ProofBuilder<'a, Curve25519Scalar>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<Curve25519Scalar>,
) {
) -> Vec<Column<'a, Curve25519Scalar>> {
// 1. selection
let selection_column: Column<'a, Curve25519Scalar> =
self.where_clause.prover_evaluate(builder, alloc, accessor);
Expand Down Expand Up @@ -107,6 +108,7 @@ impl ProverEvaluate<Curve25519Scalar> for DishonestDenseFilterExec<RistrettoPoin
&filtered_columns,
result_len,
);
filtered_columns
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/proof-of-sql/src/sql/proof_plans/dyn_proof_plan.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{DenseFilterExec, FilterExec, GroupByExec, ProjectionExec};
use crate::{
base::commitment::Commitment,
base::{commitment::Commitment, database::Column},
sql::proof::{ProofPlan, ProverEvaluate},
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for DynProofPlan<C> {
builder: &mut crate::sql::proof::ResultBuilder<'a>,
alloc: &'a bumpalo::Bump,
accessor: &'a dyn crate::base::database::DataAccessor<C::Scalar>,
) {
) -> Vec<Column<'a, C::Scalar>> {
match self {
DynProofPlan::Projection(expr) => expr.result_evaluate(builder, alloc, accessor),
DynProofPlan::Filter(expr) => expr.result_evaluate(builder, alloc, accessor),
Expand All @@ -123,7 +123,7 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for DynProofPlan<C> {
builder: &mut crate::sql::proof::ProofBuilder<'a, C::Scalar>,
alloc: &'a bumpalo::Bump,
accessor: &'a dyn crate::base::database::DataAccessor<C::Scalar>,
) {
) -> Vec<Column<'a, C::Scalar>> {
match self {
DynProofPlan::Projection(expr) => expr.prover_evaluate(builder, alloc, accessor),
DynProofPlan::Filter(expr) => expr.prover_evaluate(builder, alloc, accessor),
Expand Down
20 changes: 12 additions & 8 deletions crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for FilterExec<C> {
builder: &mut ResultBuilder<'a>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) {
) -> Vec<Column<'a, C::Scalar>> {
// evaluate where clause
let selection_column: Column<'a, C::Scalar> =
self.where_clause
Expand All @@ -139,9 +139,11 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for FilterExec<C> {
builder.set_result_indexes(Indexes::Sparse(indexes));

// evaluate result columns
for expr in self.results.iter() {
expr.result_evaluate(builder, accessor);
}
Vec::from_iter(
self.results
.iter()
.map(|expr| expr.result_evaluate(builder, accessor)),
)
}

#[tracing::instrument(name = "FilterExec::prover_evaluate", level = "debug", skip_all)]
Expand All @@ -150,15 +152,17 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for FilterExec<C> {
builder: &mut ProofBuilder<'a, C::Scalar>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) {
) -> Vec<Column<'a, C::Scalar>> {
// evaluate where clause
let selection_column: Column<'a, C::Scalar> =
self.where_clause.prover_evaluate(builder, alloc, accessor);
let selection = selection_column
.as_boolean()
.expect("selection is not boolean");
for expr in self.results.iter() {
expr.prover_evaluate(builder, alloc, accessor, selection);
}
Vec::from_iter(
self.results
.iter()
.map(|expr| expr.prover_evaluate(builder, alloc, accessor, selection)),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl ProverEvaluate<Curve25519Scalar> for DishonestFilterExec {
builder: &mut ResultBuilder<'a>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<Curve25519Scalar>,
) {
) -> Vec<Column<'a, Curve25519Scalar>> {
// evaluate where clause
let selection_column: Column<'a, Curve25519Scalar> =
self.where_clause
Expand All @@ -51,6 +51,11 @@ impl ProverEvaluate<Curve25519Scalar> for DishonestFilterExec {
.collect();
indexes[0] += 1;
builder.set_result_indexes(Indexes::Sparse(indexes));
Vec::from_iter(
self.results
.iter()
.map(|expr| expr.result_evaluate(builder, accessor)),
)
}

#[tracing::instrument(
Expand All @@ -63,7 +68,7 @@ impl ProverEvaluate<Curve25519Scalar> for DishonestFilterExec {
builder: &mut ProofBuilder<'a, Curve25519Scalar>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<Curve25519Scalar>,
) {
) -> Vec<Column<'a, Curve25519Scalar>> {
// evaluate where clause
let selection_column: Column<'a, Curve25519Scalar> =
self.where_clause.prover_evaluate(builder, alloc, accessor);
Expand All @@ -72,9 +77,11 @@ impl ProverEvaluate<Curve25519Scalar> for DishonestFilterExec {
.expect("selection is not boolean");

// evaluate result columns
for expr in self.results.iter() {
expr.prover_evaluate(builder, alloc, accessor, selection);
}
Vec::from_iter(
self.results
.iter()
.map(|expr| expr.prover_evaluate(builder, alloc, accessor, selection)),
)
}
}

Expand Down
Loading

0 comments on commit 2a19752

Please sign in to comment.