Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: switch to consume_intermediate_mle in ProofPlan::verifier_evaluate #155

Merged
merged 4 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn test_simple_commitment_evaluation_proof<CP: CommitmentEvaluationProof>(
);

let commits = Vec::from_columns_with_offset(
&[Column::Scalar(&[
[Column::Scalar(&[
CP::Scalar::one(),
CP::Scalar::one() + CP::Scalar::one(),
])],
Expand Down Expand Up @@ -50,7 +50,7 @@ pub fn test_commitment_evaluation_proof_with_length_1<CP: CommitmentEvaluationPr
let mut transcript = Transcript::new(b"evaluation_proof");
let proof = CP::new(&mut transcript, &[r], &[], 0, prover_setup);

let commits = Vec::from_columns_with_offset(&[Column::Scalar(&[r])], 0, prover_setup);
let commits = Vec::from_columns_with_offset([Column::Scalar(&[r])], 0, prover_setup);

let mut transcript = Transcript::new(b"evaluation_proof");
let r = proof.verify_proof(&mut transcript, &commits[0], &r, &[], 0, 1, verifier_setup);
Expand Down Expand Up @@ -78,7 +78,7 @@ pub fn test_random_commitment_evaluation_proof<CP: CommitmentEvaluationProof>(
let mut transcript = Transcript::new(b"evaluation_proof");
let proof = CP::new(&mut transcript, &a, &b_point, offset as u64, prover_setup);

let commits = Vec::from_columns_with_offset(&[Column::Scalar(&a)], offset, prover_setup);
let commits = Vec::from_columns_with_offset([Column::Scalar(&a)], offset, prover_setup);

let mut b = vec![CP::Scalar::zero(); a.len()];
crate::base::polynomial::compute_evaluation_vector(&mut b, &b_point);
Expand Down
6 changes: 6 additions & 0 deletions crates/proof-of-sql/src/base/commitment/committable_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ impl<'a, S: Scalar> From<&Column<'a, S>> for CommittableColumn<'a> {
}
}

impl<'a, S: Scalar> From<Column<'a, S>> for CommittableColumn<'a> {
fn from(value: Column<'a, S>) -> Self {
(&value).into()
}
}

impl<'a, S: Scalar> From<&'a OwnedColumn<S>> for CommittableColumn<'a> {
fn from(value: &'a OwnedColumn<S>) -> Self {
match value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ mod tests {
fn we_can_convert_from_columns() {
// empty case
let commitments = Vec::<RistrettoPoint>::from_columns_with_offset(
&Vec::<Column<Curve25519Scalar>>::new(),
Vec::<Column<Curve25519Scalar>>::new(),
0,
&(),
);
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 @@ -169,6 +169,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
20 changes: 19 additions & 1 deletion crates/proof-of-sql/src/base/polynomial/multilinear_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ where
slice_like_mle_impl!();
}

impl<S: Scalar> MultilinearExtension<S> for Column<'_, S> {
impl<S: Scalar> MultilinearExtension<S> for &Column<'_, S> {
fn inner_product(&self, evaluation_vec: &[S]) -> S {
match self {
Column::Boolean(c) => c.inner_product(evaluation_vec),
Expand Down Expand Up @@ -153,3 +153,21 @@ impl<S: Scalar> MultilinearExtension<S> for Column<'_, S> {
}
}
}

impl<S: Scalar> MultilinearExtension<S> for Column<'_, S> {
fn inner_product(&self, evaluation_vec: &[S]) -> S {
(&self).inner_product(evaluation_vec)
}

fn mul_add(&self, res: &mut [S], multiplier: &S) {
(&self).mul_add(res, multiplier)
}

fn to_sumcheck_term(&self, num_vars: usize) -> Rc<Vec<S>> {
(&self).to_sumcheck_term(num_vars)
}

fn id(&self) -> *const c_void {
(&self).id()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ mod tests {

// empty case
let commitments = Vec::<DoryCommitment>::from_columns_with_offset(
&Vec::<Column<DoryScalar>>::new(),
Vec::<Column<DoryScalar>>::new(),
0,
&setup,
);
Expand Down
5 changes: 4 additions & 1 deletion crates/proof-of-sql/src/sql/proof/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::{ops::Range, slice};
use num_traits::Zero;
use serde::{Deserialize, Serialize};

#[derive(Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
/// Indexes of a table for use in the ProvableQueryResult
pub enum Indexes {
/// Sparse indexes. (i.e. explicitly specified indexes)
Expand Down Expand Up @@ -93,6 +93,9 @@ impl Indexes {
Indexes::Dense(range) => {
if range.is_empty() {
Some(Zero::zero())
} else if range.end as usize > 2usize.pow(evaluation_point.len() as u32) {
// This only happens when the indexes are tampered with.
None
} else {
Some(
compute_truncated_lagrange_basis_sum(range.end as usize, evaluation_point)
Expand Down
10 changes: 7 additions & 3 deletions crates/proof-of-sql/src/sql/proof/provable_query_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};

/// An intermediate form of a query result that can be transformed
/// to either the finalized query result form or a query error
#[derive(Default, Clone, Serialize, Deserialize)]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct ProvableQueryResult {
num_columns: u64,
indexes: Indexes,
Expand Down Expand Up @@ -85,7 +85,9 @@ impl ProvableQueryResult {
table_length: usize,
column_result_fields: &[ColumnField],
) -> Result<Vec<S>, QueryError> {
assert_eq!(self.num_columns as usize, column_result_fields.len());
if self.num_columns as usize != column_result_fields.len() {
return Err(QueryError::InvalidColumnCount);
}

if !self.indexes.valid(table_length) {
return Err(QueryError::InvalidIndexes);
Expand Down Expand Up @@ -140,7 +142,9 @@ impl ProvableQueryResult {
&self,
column_result_fields: &[ColumnField],
) -> Result<OwnedTable<S>, QueryError> {
assert_eq!(column_result_fields.len(), self.num_columns());
if column_result_fields.len() != self.num_columns() {
return Err(QueryError::InvalidColumnCount);
}

let n = self.indexes.len();
let mut offset: usize = 0;
Expand Down
30 changes: 17 additions & 13 deletions crates/proof-of-sql/src/sql/proof/query_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
}?;

// verify sizes
if !self.validate_sizes(&counts, result) {
if !self.validate_sizes(&counts) {
Err(ProofError::VerificationError {
error: "invalid proof size",
})?;
Expand Down Expand Up @@ -224,20 +224,12 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {

let column_result_fields = expr.get_column_result_fields();

// compute the evaluation of the result MLEs
let result_evaluations = result.evaluate(
&subclaim.evaluation_point,
table_length,
&column_result_fields[..],
)?;

// pass over the provable AST to fill in the verification builder
let sumcheck_evaluations = SumcheckMleEvaluations::new(
table_length,
&subclaim.evaluation_point,
&sumcheck_random_scalars,
&self.pcs_proof_evaluations,
&result_evaluations,
result.indexes(),
);
let mut builder = VerificationBuilder::new(
Expand All @@ -250,7 +242,20 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
post_result_challenges,
);
let owned_table_result = result.to_owned_table(&column_result_fields[..])?;
expr.verifier_evaluate(&mut builder, accessor, Some(&owned_table_result))?;
let verifier_evaluations =
expr.verifier_evaluate(&mut builder, accessor, Some(&owned_table_result))?;
// compute the evaluation of the result MLEs
let result_evaluations = result.evaluate(
&subclaim.evaluation_point,
table_length,
&column_result_fields[..],
)?;
// check the evaluation of the result MLEs
if verifier_evaluations != result_evaluations {
Err(ProofError::VerificationError {
error: "result evaluation check failed",
})?;
}

// perform the evaluation check of the sumcheck polynomial
if builder.sumcheck_evaluation() != subclaim.expected_evaluation {
Expand Down Expand Up @@ -283,9 +288,8 @@ 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
fn validate_sizes(&self, counts: &ProofCounts) -> bool {
self.commitments.len() == counts.intermediate_mles
&& self.pcs_proof_evaluations.len() == counts.intermediate_mles + counts.anchored_mles
}
}
Expand Down
39 changes: 12 additions & 27 deletions crates/proof-of-sql/src/sql/proof/query_proof_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl<S: Scalar> ProverEvaluate<S> for TrivialTestProofPlan {
_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_intermediate_mle(col as &[_]);
builder.produce_sumcheck_subpolynomial(
SumcheckSubpolynomialType::Identity,
vec![(S::ONE, vec![Box::new(col as &[_])])],
Expand All @@ -74,7 +75,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 +92,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(
SumcheckSubpolynomialType::ZeroSum,
C::Scalar::from(self.evaluation),
Expand Down Expand Up @@ -162,24 +163,6 @@ fn verify_fails_if_the_sumcheck_evaluation_isnt_correct() {
assert!(proof.verify(&expr, &accessor, &result, &()).is_err());
}

#[test]
fn veriy_fails_if_result_mle_evaluation_fails() {
// prove and try to verify an artificial polynomial where we prove
// that every entry in the result is zero
let expr = TrivialTestProofPlan {
..Default::default()
};
let accessor = UnimplementedTestAccessor::new_empty();
let (proof, mut result) = QueryProof::<InnerProductProof>::new(&expr, &accessor, &());
match result.indexes_mut() {
Indexes::Sparse(ref mut indexes) => {
indexes.pop();
}
_ => panic!("unexpected indexes type"),
}
assert!(proof.verify(&expr, &accessor, &result, &()).is_err());
}

#[test]
fn verify_fails_if_counts_dont_match() {
// prove and verify an artificial polynomial where we try to prove
Expand Down Expand Up @@ -234,6 +217,7 @@ impl<S: Scalar> ProverEvaluate<S> for SquareTestProofPlan {
));
let res: &[_] = alloc.alloc_slice_copy(&self.res);
builder.produce_anchored_mle(x);
builder.produce_intermediate_mle(res);
builder.produce_sumcheck_subpolynomial(
SumcheckSubpolynomialType::Identity,
vec![
Expand All @@ -251,7 +235,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 @@ -268,14 +252,14 @@ 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 x_commit = C::Scalar::from(self.anchored_commit_multiplier)
* accessor.get_commitment(ColumnRef::new(
"sxt.test".parse().unwrap(),
"x".parse().unwrap(),
ColumnType::BigInt,
));
let x_eval = builder.consume_anchored_mle(x_commit);
let res_eval = builder.consume_intermediate_mle();
builder.produce_sumcheck_subpolynomial_evaluation(
SumcheckSubpolynomialType::Identity,
res_eval - x_eval * x_eval,
Expand Down Expand Up @@ -434,6 +418,7 @@ impl<S: Scalar> ProverEvaluate<S> for DoubleSquareTestProofPlan {
(-S::ONE, vec![Box::new(z), Box::new(z)]),
],
);
builder.produce_intermediate_mle(res);
vec![Column::BigInt(res)]
}
}
Expand All @@ -444,10 +429,9 @@ 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(2);
builder.count_subpolynomials(2);
builder.count_anchored_mles(1);
builder.count_intermediate_mles(1);
Ok(())
}
fn get_length(&self, _accessor: &dyn MetadataAccessor) -> usize {
Expand All @@ -467,9 +451,9 @@ impl<C: Commitment> ProofPlan<C> for DoubleSquareTestProofPlan {
"x".parse().unwrap(),
ColumnType::BigInt,
));
let res_eval = builder.consume_result_mle();
let x_eval = builder.consume_anchored_mle(x_commit);
let z_eval = builder.consume_intermediate_mle();
let res_eval = builder.consume_intermediate_mle();

// poly1
builder.produce_sumcheck_subpolynomial_evaluation(
Expand Down Expand Up @@ -627,6 +611,7 @@ impl<S: Scalar> ProverEvaluate<S> for ChallengeTestProofPlan {
let alpha = builder.consume_post_result_challenge();
let _beta = builder.consume_post_result_challenge();
builder.produce_anchored_mle(x);
builder.produce_intermediate_mle(res);
builder.produce_sumcheck_subpolynomial(
SumcheckSubpolynomialType::Identity,
vec![
Expand All @@ -644,7 +629,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 @@ -664,13 +649,13 @@ 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 x_commit = accessor.get_commitment(ColumnRef::new(
"sxt.test".parse().unwrap(),
"x".parse().unwrap(),
ColumnType::BigInt,
));
let x_eval = builder.consume_anchored_mle(x_commit);
let res_eval = builder.consume_intermediate_mle();
builder.produce_sumcheck_subpolynomial_evaluation(
SumcheckSubpolynomialType::Identity,
alpha * res_eval - alpha * x_eval * x_eval,
Expand Down
3 changes: 3 additions & 0 deletions crates/proof-of-sql/src/sql/proof/query_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub enum QueryError {
/// The underlying source error
source: OwnedTableError,
},
/// The number of columns in the table was invalid.
#[snafu(display("Invalid number of columns"))]
InvalidColumnCount,
}

/// The verified results of a query along with metadata produced by verification
Expand Down
Loading
Loading