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 16, 2024
1 parent f6b0fbe commit ced1b34
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 19 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
8 changes: 4 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,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 @@ -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 @@ -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 @@ -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
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_intermediate_mle(),
Curve25519Scalar::from(123u64)
);
assert_eq!(
builder.consume_intermediate_mle(),
Curve25519Scalar::from(456u64)
);
}

#[test]
Expand Down
6 changes: 5 additions & 1 deletion crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs
Original file line number Diff line number Diff line change
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
22 changes: 15 additions & 7 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 @@ -126,11 +126,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 +280,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 +292,7 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for GroupByExec<C> {
let alpha = builder.consume_post_result_challenge();
let beta = builder.consume_post_result_challenge();

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

Expand Down
12 changes: 9 additions & 3 deletions crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ced1b34

Please sign in to comment.