Skip to content

Commit

Permalink
refactor: switch to consume_intermediate_mle in `ProofPlan::verifie…
Browse files Browse the repository at this point in the history
…r_evaluate`

- `ProofPlan` change above
- bump `alloy` to `0.8.1`
  • Loading branch information
iajoiner committed Sep 17, 2024
1 parent f6b0fbe commit 51e87eb
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ version = "0.0.0" # DO NOT CHANGE THIS LINE! This will be automatically updated
license-file = "LICENSE"

[workspace.dependencies]
alloy-primitives = { version = "0.8.0" }
alloy-sol-types = { version = "0.8.0" }
alloy-primitives = { version = "0.8.1" }
alloy-sol-types = { version = "0.8.1" }
ark-bls12-381 = { version = "0.4.0" }
ark-curve25519 = { version = "0.4.0" }
ark-ec = { version = "0.4.0", features = [ "parallel" ] }
Expand Down
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: 1 addition & 1 deletion crates/proof-of-sql/src/sql/proof/provable_query_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl ProvableQueryResult {
table_length: usize,
column_result_fields: &[ColumnField],
) -> Result<Vec<S>, QueryError> {
assert_eq!(self.num_columns as usize, column_result_fields.len());
//assert_eq!(self.num_columns as usize, column_result_fields.len());

if !self.indexes.valid(table_length) {
return Err(QueryError::InvalidIndexes);
Expand Down
3 changes: 1 addition & 2 deletions crates/proof-of-sql/src/sql/proof/query_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
}

fn validate_sizes(&self, counts: &ProofCounts, result: &ProvableQueryResult) -> bool {
result.num_columns() == counts.result_columns
&& self.commitments.len() == counts.intermediate_mles
self.commitments.len() == counts.intermediate_mles
&& self.pcs_proof_evaluations.len() == counts.intermediate_mles + counts.anchored_mles
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/proof-of-sql/src/sql/proof/query_proof_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<C: Commitment> ProofPlan<C> for TrivialTestProofPlan {
_accessor: &dyn MetadataAccessor,
) -> Result<(), ProofError> {
builder.count_degree(2);
builder.count_result_columns(1);
builder.count_intermediate_mles(1);
builder.count_subpolynomials(1);
builder.count_anchored_mles(self.anchored_mle_count);
Ok(())
Expand All @@ -91,7 +91,7 @@ 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_intermediate_mle(), C::Scalar::ZERO);
builder.produce_sumcheck_subpolynomial_evaluation(&C::Scalar::from(self.evaluation));
Ok(vec![C::Scalar::ZERO])
}
Expand Down Expand Up @@ -248,7 +248,7 @@ impl<C: Commitment> ProofPlan<C> for SquareTestProofPlan {
_accessor: &dyn MetadataAccessor,
) -> Result<(), ProofError> {
builder.count_degree(3);
builder.count_result_columns(1);
builder.count_intermediate_mles(1);
builder.count_subpolynomials(1);
builder.count_anchored_mles(1);
Ok(())
Expand All @@ -265,7 +265,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_intermediate_mle();
let x_commit = C::Scalar::from(self.anchored_commit_multiplier)
* accessor.get_commitment(ColumnRef::new(
"sxt.test".parse().unwrap(),
Expand Down Expand Up @@ -439,7 +439,7 @@ impl<C: Commitment> ProofPlan<C> for DoubleSquareTestProofPlan {
_accessor: &dyn MetadataAccessor,
) -> Result<(), ProofError> {
builder.count_degree(3);
builder.count_result_columns(1);
builder.count_intermediate_mles(1);
builder.count_subpolynomials(2);
builder.count_anchored_mles(1);
builder.count_intermediate_mles(1);
Expand All @@ -462,7 +462,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_intermediate_mle();
let x_eval = builder.consume_anchored_mle(x_commit);
let z_eval = builder.consume_intermediate_mle();

Expand Down Expand Up @@ -635,7 +635,7 @@ impl<C: Commitment> ProofPlan<C> for ChallengeTestProofPlan {
_accessor: &dyn MetadataAccessor,
) -> Result<(), ProofError> {
builder.count_degree(3);
builder.count_result_columns(1);
builder.count_intermediate_mles(1);
builder.count_subpolynomials(1);
builder.count_anchored_mles(1);
builder.count_post_result_challenges(2);
Expand All @@ -655,7 +655,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_intermediate_mle();
let x_commit = accessor.get_commitment(ColumnRef::new(
"sxt.test".parse().unwrap(),
"x".parse().unwrap(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<C: Commitment> ProofPlan<C> for EmptyTestQueryExpr {
builder: &mut CountBuilder,
_accessor: &dyn MetadataAccessor,
) -> Result<(), ProofError> {
builder.count_result_columns(1);
builder.count_intermediate_mles(1);
Ok(())
}
fn get_length(&self, _accessor: &dyn MetadataAccessor) -> usize {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ pub fn exercise_verification(
accessor: &impl TestAccessor<RistrettoPoint>,
table_ref: TableRef,
) {
assert!(res.verify(expr, accessor, &()).is_ok());
let verification_result = res.verify(expr, accessor, &());
if verification_result.is_err() {
panic!("Verification failed: {:?}", verification_result.err());
}

// try changing the result
tamper_result(res, expr, accessor);
Expand Down
1 change: 0 additions & 1 deletion crates/proof-of-sql/src/sql/proof/verification_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ impl<'a, C: Commitment> VerificationBuilder<'a, C> {
&& self.produced_subpolynomials == self.subpolynomial_multipliers.len()
&& self.consumed_intermediate_mles == self.intermediate_commitments.len()
&& self.consumed_pcs_proof_mles == self.mle_evaluations.pcs_proof_evaluations.len()
&& self.consumed_result_mles == self.mle_evaluations.result_evaluations.len()
&& self.post_result_challenges.is_empty()
}

Expand Down
8 changes: 6 additions & 2 deletions crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ where
self.where_clause.count(builder)?;
for aliased_expr in self.aliased_results.iter() {
aliased_expr.expr.count(builder)?;
builder.count_result_columns(1);
builder.count_intermediate_mles(1);
}
builder.count_intermediate_mles(2);
builder.count_subpolynomials(3);
Expand Down Expand Up @@ -107,7 +107,7 @@ 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_intermediate_mle()).take(self.aliased_results.len()),
);

let alpha = builder.consume_post_result_challenge();
Expand Down Expand Up @@ -199,6 +199,10 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for FilterExec<C> {
);
// Compute filtered_columns and indexes
let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection);
// 3. Produce MLEs
filtered_columns.iter().for_each(|column| {
builder.produce_intermediate_mle(column.as_scalar(alloc));
});

let alpha = builder.consume_post_result_challenge();
let beta = builder.consume_post_result_challenge();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ impl ProverEvaluate<Curve25519Scalar> for DishonestFilterExec<RistrettoPoint> {
&filtered_columns,
result_len,
);
// 3. Produce MLEs
filtered_columns.iter().for_each(|column| {
builder.produce_intermediate_mle(column.as_scalar(alloc));
});
filtered_columns
}
}
Expand Down
39 changes: 24 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 @@ -76,13 +76,14 @@ impl<C: Commitment> ProofPlan<C> for GroupByExec<C> {
self.where_clause.count(builder)?;
for expr in self.group_by_exprs.iter() {
expr.count(builder)?;
builder.count_result_columns(1);
builder.count_intermediate_mles(1);
}
for aliased_expr in self.sum_expr.iter() {
aliased_expr.expr.count(builder)?;
builder.count_result_columns(1);
builder.count_intermediate_mles(1);
}
builder.count_result_columns(1);
// For the count col
builder.count_intermediate_mles(1);
builder.count_intermediate_mles(2);
builder.count_subpolynomials(3);
builder.count_degree(3);
Expand Down Expand Up @@ -126,11 +127,12 @@ 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_intermediate_mle()).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_intermediate_mle()).take(self.sum_expr.len()),
);
let count_column_eval = builder.consume_intermediate_mle();

let alpha = builder.consume_post_result_challenge();
let beta = builder.consume_post_result_challenge();
Expand Down Expand Up @@ -279,7 +281,7 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for GroupByExec<C> {
.iter()
.map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, accessor)),
);
// Compute filtered_columns and indexes
// 3. Compute filtered_columns and indexes
let AggregatedColumns {
group_by_columns: group_by_result_columns,
sum_columns: sum_result_columns,
Expand All @@ -291,6 +293,19 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for GroupByExec<C> {
let alpha = builder.consume_post_result_challenge();
let beta = builder.consume_post_result_challenge();

// 4. Tally results
let sum_result_columns_iter = sum_result_columns.iter().map(|col| Column::Scalar(col));
let res = Vec::from_iter(
group_by_result_columns.clone()
.into_iter()
.chain(sum_result_columns_iter)
.chain(std::iter::once(Column::BigInt(count_column))),
);
// 5. Produce MLEs
res.iter().for_each(|column| {
builder.produce_intermediate_mle(column.as_scalar(alloc));
});
// 6. Prove group by
prove_group_by(
builder,
alloc,
Expand All @@ -299,13 +314,7 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for GroupByExec<C> {
(&group_by_columns, &sum_columns, selection),
(&group_by_result_columns, &sum_result_columns, count_column),
);
let sum_result_columns_iter = sum_result_columns.iter().map(|col| Column::Scalar(col));
Vec::from_iter(
group_by_result_columns
.into_iter()
.chain(sum_result_columns_iter)
.chain(std::iter::once(Column::BigInt(count_column))),
)
res
}
}

Expand Down
14 changes: 10 additions & 4 deletions crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<C: Commitment> ProofPlan<C> for ProjectionExec<C> {
) -> Result<(), ProofError> {
for aliased_expr in self.aliased_results.iter() {
aliased_expr.expr.count(builder)?;
builder.count_result_columns(1);
builder.count_intermediate_mles(1);
}
Ok(())
}
Expand All @@ -73,7 +73,7 @@ impl<C: Commitment> ProofPlan<C> for ProjectionExec<C> {
.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_intermediate_mle()).take(self.aliased_results.len()),
))
}

Expand Down Expand Up @@ -118,10 +118,16 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for ProjectionExec<C> {
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> Vec<Column<'a, C::Scalar>> {
Vec::from_iter(
// 1. Evaluate result expressions
let res = Vec::from_iter(
self.aliased_results
.iter()
.map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, accessor)),
)
);
// 2. Produce MLEs
res.clone().into_iter().for_each(|column| {
builder.produce_intermediate_mle(column.as_scalar(alloc));
});
res
}
}

0 comments on commit 51e87eb

Please sign in to comment.