Skip to content

Commit

Permalink
refactor: add is_top_level to prover_evaluate and `verifier_evalu…
Browse files Browse the repository at this point in the history
…ate` for `ProofPlan`s
  • Loading branch information
iajoiner committed Sep 12, 2024
1 parent a99a8f0 commit 6c8f329
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 49 deletions.
17 changes: 17 additions & 0 deletions crates/proof-of-sql/src/base/database/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ impl<'a, S: Scalar> Column<'a, S> {
}
}

/// Returns the column as a slice of scalars
pub(crate) fn as_scalar(&self, alloc: &'a Bump) -> &'a [S] {
match self {
Self::Boolean(col) => alloc.alloc_slice_fill_with(col.len(), |i| S::from(col[i])),
Self::SmallInt(col) => alloc.alloc_slice_fill_with(col.len(), |i| S::from(col[i])),
Self::Int(col) => alloc.alloc_slice_fill_with(col.len(), |i| S::from(col[i])),
Self::BigInt(col) => alloc.alloc_slice_fill_with(col.len(), |i| S::from(col[i])),
Self::Int128(col) => alloc.alloc_slice_fill_with(col.len(), |i| S::from(col[i])),
Self::Scalar(col) => col,
Self::Decimal75(_, _, col) => col,
Self::VarChar((_, scals)) => scals,
Self::TimestampTZ(_, _, col) => {
alloc.alloc_slice_fill_with(col.len(), |i| S::from(col[i]))
}
}
}

/// Returns element at index as scalar
///
/// Note that if index is out of bounds, this function will return None
Expand Down
2 changes: 2 additions & 0 deletions crates/proof-of-sql/src/sql/proof/proof_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub trait ProofPlan<C: Commitment>: Debug + Send + Sync + ProverEvaluate<C::Scal
builder: &mut VerificationBuilder<C>,
accessor: &dyn CommitmentAccessor<C>,
result: Option<&OwnedTable<C::Scalar>>,
is_top_level: bool,
) -> Result<Vec<C::Scalar>, ProofError>;

/// Return all the result column fields
Expand Down Expand Up @@ -67,6 +68,7 @@ pub trait ProverEvaluate<S: Scalar> {
builder: &mut ProofBuilder<'a, S>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
is_top_level: bool,
) -> 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 @@ -76,7 +76,7 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {

let mut builder =
ProofBuilder::new(table_length, num_sumcheck_variables, post_result_challenges);
expr.prover_evaluate(&mut builder, &alloc, accessor);
expr.prover_evaluate(&mut builder, &alloc, accessor, true);

let num_sumcheck_variables = builder.num_sumcheck_variables();
let table_length = builder.table_length();
Expand Down
11 changes: 7 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 @@ -91,7 +91,10 @@ impl<C: Commitment> ProofPlan<C> for TrivialTestProofPlan {
_accessor: &dyn CommitmentAccessor<C>,
_result: Option<&OwnedTable<C::Scalar>>,
) -> Result<Vec<C::Scalar>, ProofError> {
assert_eq!(builder.consume_result_mle(), C::Scalar::ZERO);
assert_eq!(
builder.consume_result_or_intermediate_mle(self.is_top_level),
C::Scalar::ZERO
);
builder.produce_sumcheck_subpolynomial_evaluation(&C::Scalar::from(self.evaluation));
Ok(vec![C::Scalar::ZERO])
}
Expand Down Expand Up @@ -265,7 +268,7 @@ impl<C: Commitment> ProofPlan<C> for SquareTestProofPlan {
accessor: &dyn CommitmentAccessor<C>,
_result: Option<&OwnedTable<C::Scalar>>,
) -> Result<Vec<C::Scalar>, ProofError> {
let res_eval = builder.consume_result_mle();
let res_eval = builder.consume_result_or_intermediate_mle(self.is_top_level);
let x_commit = C::Scalar::from(self.anchored_commit_multiplier)
* accessor.get_commitment(ColumnRef::new(
"sxt.test".parse().unwrap(),
Expand Down Expand Up @@ -462,7 +465,7 @@ impl<C: Commitment> ProofPlan<C> for DoubleSquareTestProofPlan {
"x".parse().unwrap(),
ColumnType::BigInt,
));
let res_eval = builder.consume_result_mle();
let res_eval = builder.consume_result_or_intermediate_mle(self.is_top_level);
let x_eval = builder.consume_anchored_mle(x_commit);
let z_eval = builder.consume_intermediate_mle();

Expand Down Expand Up @@ -655,7 +658,7 @@ impl<C: Commitment> ProofPlan<C> for ChallengeTestProofPlan {
) -> Result<Vec<C::Scalar>, ProofError> {
let alpha = builder.consume_post_result_challenge();
let _beta = builder.consume_post_result_challenge();
let res_eval = builder.consume_result_mle();
let res_eval = builder.consume_result_or_intermediate_mle(self.is_top_level);
let x_commit = accessor.get_commitment(ColumnRef::new(
"sxt.test".parse().unwrap(),
"x".parse().unwrap(),
Expand Down
12 changes: 12 additions & 0 deletions crates/proof-of-sql/src/sql/proof/verification_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ impl<'a, C: Commitment> VerificationBuilder<'a, C> {
self.mle_evaluations.result_evaluations[index]
}

/// Consume the evaluation of an intermediate MLE if NOT at the top level
/// and consume the evaluation of a result MLE if at the top level
///
/// This is used to consume the evaluation of an MLE used in sumcheck
pub fn consume_result_or_intermediate_mle(&mut self, is_top_level: bool) -> C::Scalar {
if is_top_level {
self.consume_result_mle()
} else {
self.consume_intermediate_mle()
}
}

/// Produce the evaluation of a subpolynomial used in sumcheck
pub fn produce_sumcheck_subpolynomial_evaluation(&mut self, eval: &C::Scalar) {
self.sumcheck_evaluation +=
Expand Down
10 changes: 8 additions & 2 deletions crates/proof-of-sql/src/sql/proof/verification_builder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,14 @@ fn we_can_consume_result_evaluations() {
&[][..],
Vec::new(),
);
assert_eq!(builder.consume_result_mle(), Curve25519Scalar::from(123u64));
assert_eq!(builder.consume_result_mle(), Curve25519Scalar::from(456u64));
assert_eq!(
builder.consume_result_or_intermediate_mle(true),
Curve25519Scalar::from(123u64)
);
assert_eq!(
builder.consume_result_or_intermediate_mle(true),
Curve25519Scalar::from(456u64)
);
}

#[test]
Expand Down
26 changes: 20 additions & 6 deletions crates/proof-of-sql/src/sql/proof_plans/dyn_proof_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ impl<C: Commitment> ProofPlan<C> for DynProofPlan<C> {
builder: &mut crate::sql::proof::VerificationBuilder<C>,
accessor: &dyn crate::base::database::CommitmentAccessor<C>,
result: Option<&crate::base::database::OwnedTable<C::Scalar>>,
is_top_level: bool,
) -> Result<Vec<C::Scalar>, crate::base::proof::ProofError> {
match self {
DynProofPlan::Projection(expr) => expr.verifier_evaluate(builder, accessor, result),
DynProofPlan::GroupBy(expr) => expr.verifier_evaluate(builder, accessor, result),
DynProofPlan::Filter(expr) => expr.verifier_evaluate(builder, accessor, result),
DynProofPlan::Projection(expr) => {
expr.verifier_evaluate(builder, accessor, result, is_top_level)
}
DynProofPlan::GroupBy(expr) => {
expr.verifier_evaluate(builder, accessor, result, is_top_level)
}
DynProofPlan::Filter(expr) => {
expr.verifier_evaluate(builder, accessor, result, is_top_level)
}
}
}

Expand Down Expand Up @@ -111,11 +118,18 @@ 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>,
is_top_level: bool,
) -> Vec<Column<'a, C::Scalar>> {
match self {
DynProofPlan::Projection(expr) => expr.prover_evaluate(builder, alloc, accessor),
DynProofPlan::GroupBy(expr) => expr.prover_evaluate(builder, alloc, accessor),
DynProofPlan::Filter(expr) => expr.prover_evaluate(builder, alloc, accessor),
DynProofPlan::Projection(expr) => {
expr.prover_evaluate(builder, alloc, accessor, is_top_level)
}
DynProofPlan::GroupBy(expr) => {
expr.prover_evaluate(builder, alloc, accessor, is_top_level)
}
DynProofPlan::Filter(expr) => {
expr.prover_evaluate(builder, alloc, accessor, is_top_level)
}
}
}
}
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 @@ -89,6 +89,7 @@ where
builder: &mut VerificationBuilder<C>,
accessor: &dyn CommitmentAccessor<C>,
_result: Option<&OwnedTable<C::Scalar>>,
is_top_level: bool,
) -> Result<Vec<C::Scalar>, ProofError> {
// 1. selection
let selection_eval = self.where_clause.verifier_evaluate(builder, accessor)?;
Expand All @@ -106,7 +107,8 @@ where
.ok_or(ProofError::VerificationError("invalid indexes"))?;
// 4. filtered_columns
let filtered_columns_evals = Vec::from_iter(
repeat_with(|| builder.consume_result_mle()).take(self.aliased_results.len()),
repeat_with(|| builder.consume_result_or_intermediate_mle(is_top_level))
.take(self.aliased_results.len()),
);

let alpha = builder.consume_post_result_challenge();
Expand Down Expand Up @@ -182,20 +184,22 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for FilterExec<C> {
builder: &mut ProofBuilder<'a, C::Scalar>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
is_top_level: bool,
) -> Vec<Column<'a, C::Scalar>> {
// 1. selection
let selection_column: Column<'a, C::Scalar> =
self.where_clause.prover_evaluate(builder, alloc, accessor);
let selection_column: Column<'a, C::Scalar> = self
.where_clause
.prover_evaluate(builder, alloc, accessor, false);
let selection = selection_column
.as_boolean()
.expect("selection is not boolean");

// 2. columns
let columns = Vec::from_iter(
self.aliased_results
.iter()
.map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, accessor)),
);
let columns = Vec::from_iter(self.aliased_results.iter().map(|aliased_expr| {
aliased_expr
.expr
.prover_evaluate(builder, alloc, accessor, false)
}));
// Compute filtered_columns and indexes
let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,21 @@ impl ProverEvaluate<Curve25519Scalar> for DishonestFilterExec<RistrettoPoint> {
builder: &mut ProofBuilder<'a, Curve25519Scalar>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<Curve25519Scalar>,
is_top_level: bool,
) -> Vec<Column<'a, Curve25519Scalar>> {
// 1. selection
let selection_column: Column<'a, Curve25519Scalar> =
self.where_clause.prover_evaluate(builder, alloc, accessor);
let selection_column: Column<'a, Curve25519Scalar> = self
.where_clause
.prover_evaluate(builder, alloc, accessor, false);
let selection = selection_column
.as_boolean()
.expect("selection is not boolean");
// 2. columns
let columns = Vec::from_iter(
self.aliased_results
.iter()
.map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, accessor)),
);
let columns = Vec::from_iter(self.aliased_results.iter().map(|aliased_expr| {
aliased_expr
.expr
.prover_evaluate(builder, alloc, accessor, false)
}));
// Compute filtered_columns and indexes
let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection);
let filtered_columns = tamper_column(alloc, filtered_columns);
Expand Down
42 changes: 27 additions & 15 deletions crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,26 @@ impl<C: Commitment> ProofPlan<C> for GroupByExec<C> {
builder: &mut VerificationBuilder<C>,
accessor: &dyn CommitmentAccessor<C>,
result: Option<&OwnedTable<C::Scalar>>,
is_top_level: bool,
) -> Result<Vec<C::Scalar>, ProofError> {
// 1. selection
let where_eval = self.where_clause.verifier_evaluate(builder, accessor)?;
let where_eval = self
.where_clause
.verifier_evaluate(builder, accessor, false)?;
// 2. columns
let group_by_evals = self
.group_by_exprs
.iter()
.map(|expr| expr.verifier_evaluate(builder, accessor))
.map(|expr| expr.verifier_evaluate(builder, accessor, false))
.collect::<Result<Vec<_>, _>>()?;
let aggregate_evals = self
.sum_expr
.iter()
.map(|aliased_expr| aliased_expr.expr.verifier_evaluate(builder, accessor))
.map(|aliased_expr| {
aliased_expr
.expr
.verifier_evaluate(builder, accessor, false)
})
.collect::<Result<Vec<_>, _>>()?;
// 3. indexes
let indexes_eval = builder
Expand All @@ -126,11 +133,14 @@ impl<C: Commitment> ProofPlan<C> for GroupByExec<C> {
// 4. filtered_columns

let group_by_result_columns_evals = Vec::from_iter(
repeat_with(|| builder.consume_result_mle()).take(self.group_by_exprs.len()),
repeat_with(|| builder.consume_result_or_intermediate_mle(is_top_level))
.take(self.group_by_exprs.len()),
);
let sum_result_columns_evals =
Vec::from_iter(repeat_with(|| builder.consume_result_mle()).take(self.sum_expr.len()));
let count_column_eval = builder.consume_result_mle();
let sum_result_columns_evals = Vec::from_iter(
repeat_with(|| builder.consume_result_or_intermediate_mle(is_top_level))
.take(self.sum_expr.len()),
);
let count_column_eval = builder.consume_result_or_intermediate_mle(is_top_level);

let alpha = builder.consume_post_result_challenge();
let beta = builder.consume_post_result_challenge();
Expand Down Expand Up @@ -260,10 +270,12 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for GroupByExec<C> {
builder: &mut ProofBuilder<'a, C::Scalar>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
is_top_level: bool,
) -> Vec<Column<'a, C::Scalar>> {
// 1. selection
let selection_column: Column<'a, C::Scalar> =
self.where_clause.prover_evaluate(builder, alloc, accessor);
let selection_column: Column<'a, C::Scalar> = self
.where_clause
.prover_evaluate(builder, alloc, accessor, false);
let selection = selection_column
.as_boolean()
.expect("selection is not boolean");
Expand All @@ -272,13 +284,13 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for GroupByExec<C> {
let group_by_columns = Vec::from_iter(
self.group_by_exprs
.iter()
.map(|expr| expr.prover_evaluate(builder, alloc, accessor)),
);
let sum_columns = Vec::from_iter(
self.sum_expr
.iter()
.map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, accessor)),
.map(|expr| expr.prover_evaluate(builder, alloc, accessor, false)),
);
let sum_columns = Vec::from_iter(self.sum_expr.iter().map(|aliased_expr| {
aliased_expr
.expr
.prover_evaluate(builder, alloc, accessor, false)
}));
// Compute filtered_columns and indexes
let AggregatedColumns {
group_by_columns: group_by_result_columns,
Expand Down
15 changes: 9 additions & 6 deletions crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ impl<C: Commitment> ProofPlan<C> for ProjectionExec<C> {
builder: &mut VerificationBuilder<C>,
accessor: &dyn CommitmentAccessor<C>,
_result: Option<&OwnedTable<C::Scalar>>,
is_top_level: bool,
) -> Result<Vec<C::Scalar>, ProofError> {
self.aliased_results
.iter()
.map(|aliased_expr| aliased_expr.expr.verifier_evaluate(builder, accessor))
.collect::<Result<Vec<_>, _>>()?;
Ok(Vec::from_iter(
repeat_with(|| builder.consume_result_mle()).take(self.aliased_results.len()),
repeat_with(|| builder.consume_result_or_intermediate_mle(is_top_level))
.take(self.aliased_results.len()),
))
}

Expand Down Expand Up @@ -117,11 +119,12 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for ProjectionExec<C> {
builder: &mut ProofBuilder<'a, C::Scalar>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
is_top_level: bool,
) -> Vec<Column<'a, C::Scalar>> {
Vec::from_iter(
self.aliased_results
.iter()
.map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, accessor)),
)
Vec::from_iter(self.aliased_results.iter().map(|aliased_expr| {
aliased_expr
.expr
.prover_evaluate(builder, alloc, accessor, is_top_level)
}))
}
}

0 comments on commit 6c8f329

Please sign in to comment.