Skip to content

Commit

Permalink
refactor!: add get_output_length and modify get_length
Browse files Browse the repository at this point in the history
  • Loading branch information
iajoiner committed Oct 10, 2024
1 parent cc8e2f0 commit 2ff406d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 53 deletions.
53 changes: 37 additions & 16 deletions crates/proof-of-sql/src/sql/proof/proof_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,23 @@ use crate::base::{
},
map::IndexSet,
proof::ProofError,
scalar::Scalar,
};
use alloc::vec::Vec;
use bumpalo::Bump;
use core::fmt::Debug;

/// Provable nodes in the provable AST.
pub trait ProofPlan<C: Commitment>: Debug + Send + Sync + ProverEvaluate<C::Scalar> {
pub trait ProofPlan<C: Commitment>: Debug + Send + Sync + ProverEvaluate<C> {
/// Count terms used within the Query's proof
fn count(
&self,
builder: &mut CountBuilder,
accessor: &dyn MetadataAccessor,
) -> Result<(), ProofError>;

/// The length of the input table
fn get_length(&self, accessor: &dyn MetadataAccessor) -> usize;

/// The offset of the query, that is, how many rows to skip before starting to read the input table
fn get_offset(&self, accessor: &dyn MetadataAccessor) -> usize;

/// Check if the input table is empty
fn is_empty(&self, accessor: &dyn MetadataAccessor) -> bool {
self.get_length(accessor) == 0
}

/// Form components needed to verify and proof store into `VerificationBuilder`
fn verifier_evaluate(
&self,
Expand All @@ -48,14 +39,14 @@ pub trait ProofPlan<C: Commitment>: Debug + Send + Sync + ProverEvaluate<C::Scal
fn get_column_references(&self) -> IndexSet<ColumnRef>;
}

pub trait ProverEvaluate<S: Scalar> {
pub trait ProverEvaluate<C: Commitment> {
/// Evaluate the query and modify `ResultBuilder` to track the result of the query.
fn result_evaluate<'a>(
&self,
builder: &mut ResultBuilder,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
) -> Vec<Column<'a, S>>;
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> Vec<Column<'a, C::Scalar>>;

/// 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 @@ -65,10 +56,40 @@ pub trait ProverEvaluate<S: Scalar> {
/// will be bulk deallocated once the proof is formed.
fn prover_evaluate<'a>(
&self,
builder: &mut ProofBuilder<'a, S>,
builder: &mut ProofBuilder<'a, C::Scalar>,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
) -> Vec<Column<'a, S>>;
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> Vec<Column<'a, C::Scalar>>;

/// The length of the input table
fn get_input_length<'a>(
&self,
builder: &mut ResultBuilder,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> usize;

/// Check if the input table is empty
fn is_empty<'a>(
&self,
builder: &mut ResultBuilder,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> bool {
self.get_input_length(builder, alloc, accessor) == 0
}

/// The length of the output table
fn get_output_length<'a>(
&self,
builder: &mut ResultBuilder,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> usize {
self.result_evaluate(builder, alloc, accessor)
.first()
.map_or(0, |column| column.len())
}
}

/// Marker used as a trait bound for generic [`ProofPlan`] types to indicate the honesty of their implementation.
Expand Down
8 changes: 6 additions & 2 deletions crates/proof-of-sql/src/sql/proof/query_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
expr: &(impl ProofPlan<CP::Commitment> + Serialize),
accessor: &impl DataAccessor<CP::Scalar>,
setup: &CP::ProverPublicSetup<'_>,
builder: &mut ResultBuilder,
) -> (Self, ProvableQueryResult) {
let table_length = expr.get_length(accessor);
let alloc = Bump::new();
let table_length = expr.get_input_length(builder, alloc, accessor);
let num_sumcheck_variables = cmp::max(log2_up(table_length), 1);
let generator_offset = expr.get_offset(accessor);
assert!(num_sumcheck_variables > 0);
Expand Down Expand Up @@ -144,8 +146,10 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
accessor: &impl CommitmentAccessor<CP::Commitment>,
result: &ProvableQueryResult,
setup: &CP::VerifierPublicSetup<'_>,
builder: &mut ResultBuilder,
) -> QueryResult<CP::Scalar> {
let table_length = expr.get_length(accessor);
let alloc = Bump::new();
let table_length = expr.get_input_length(builder, alloc, accessor);
let generator_offset = expr.get_offset(accessor);
let num_sumcheck_variables = cmp::max(log2_up(table_length), 1);
assert!(num_sumcheck_variables > 0);
Expand Down
21 changes: 13 additions & 8 deletions crates/proof-of-sql/src/sql/proof/verifiable_query_result.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use super::{ProofPlan, ProvableQueryResult, QueryData, QueryProof, QueryResult};
use crate::base::{
commitment::CommitmentEvaluationProof,
database::{
ColumnField, ColumnType, CommitmentAccessor, DataAccessor, OwnedColumn, OwnedTable,
use crate::{
base::{
commitment::CommitmentEvaluationProof,
database::{
ColumnField, ColumnType, CommitmentAccessor, DataAccessor, OwnedColumn, OwnedTable,
},
proof::ProofError,
scalar::Scalar,
},
proof::ProofError,
scalar::Scalar,
sql::proof::ResultBuilder,
};
use alloc::{vec, vec::Vec};
use bumpalo::Bump;
use serde::{Deserialize, Serialize};

/// The result of an sql query along with a proof that the query is valid. The
Expand Down Expand Up @@ -83,12 +87,13 @@ impl<CP: CommitmentEvaluationProof> VerifiableQueryResult<CP> {
expr: &(impl ProofPlan<CP::Commitment> + Serialize),
accessor: &impl DataAccessor<CP::Scalar>,
setup: &CP::ProverPublicSetup<'_>,
builder: &mut ResultBuilder,
) -> Self {
// a query must have at least one result column; if not, it should
// have been rejected at the parsing stage.

let alloc = Bump::new();
// handle the empty case
if expr.is_empty(accessor) {
if expr.is_empty(builder, alloc, accessor) {
return VerifiableQueryResult {
provable_result: None,
proof: None,
Expand Down
26 changes: 16 additions & 10 deletions crates/proof-of-sql/src/sql/proof_plans/dyn_proof_plan.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::{FilterExec, GroupByExec, ProjectionExec};
use crate::{
base::{commitment::Commitment, database::Column, map::IndexSet},
sql::proof::{ProofPlan, ProverEvaluate},
sql::proof::{ProofPlan, ProverEvaluate, ResultBuilder},
};
use alloc::vec::Vec;
use bumpalo::Bump;
use serde::{Deserialize, Serialize};

/// The query plan for proving a query
Expand Down Expand Up @@ -44,14 +45,6 @@ impl<C: Commitment> ProofPlan<C> for DynProofPlan<C> {
}
}

fn get_length(&self, accessor: &dyn crate::base::database::MetadataAccessor) -> usize {
match self {
DynProofPlan::Projection(expr) => expr.get_length(accessor),
DynProofPlan::GroupBy(expr) => expr.get_length(accessor),
DynProofPlan::Filter(expr) => expr.get_length(accessor),
}
}

fn get_offset(&self, accessor: &dyn crate::base::database::MetadataAccessor) -> usize {
match self {
DynProofPlan::Projection(expr) => expr.get_offset(accessor),
Expand Down Expand Up @@ -91,7 +84,20 @@ impl<C: Commitment> ProofPlan<C> for DynProofPlan<C> {
}
}

impl<C: Commitment> ProverEvaluate<C::Scalar> for DynProofPlan<C> {
impl<C: Commitment> ProverEvaluate<C> for DynProofPlan<C> {
fn get_input_length<'a>(
&self,
builder: &mut ResultBuilder,
alloc: &'a Bump,
accessor: &'a dyn crate::base::database::DataAccessor<C::Scalar>,
) -> usize {
match self {
DynProofPlan::Projection(expr) => expr.get_input_length(builder, alloc, accessor),
DynProofPlan::GroupBy(expr) => expr.get_input_length(builder, alloc, accessor),
DynProofPlan::Filter(expr) => expr.get_input_length(builder, alloc, accessor),
}
}

#[tracing::instrument(name = "DynProofPlan::result_evaluate", level = "debug", skip_all)]
fn result_evaluate<'a>(
&self,
Expand Down
18 changes: 12 additions & 6 deletions crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<C: Commitment, H: ProverHonestyMarker> OstensibleFilterExec<C, H> {

impl<C: Commitment, H: ProverHonestyMarker> ProofPlan<C> for OstensibleFilterExec<C, H>
where
OstensibleFilterExec<C, H>: ProverEvaluate<C::Scalar>,
OstensibleFilterExec<C, H>: ProverEvaluate<C>,
{
fn count(
&self,
Expand All @@ -77,10 +77,6 @@ where
Ok(())
}

fn get_length(&self, accessor: &dyn MetadataAccessor) -> usize {
accessor.get_length(self.table.table_ref)
}

fn get_offset(&self, accessor: &dyn MetadataAccessor) -> usize {
accessor.get_offset(self.table.table_ref)
}
Expand Down Expand Up @@ -150,7 +146,17 @@ where
/// Alias for a filter expression with a honest prover.
pub type FilterExec<C> = OstensibleFilterExec<C, HonestProver>;

impl<C: Commitment> ProverEvaluate<C::Scalar> for FilterExec<C> {
impl<C: Commitment> ProverEvaluate<C> for FilterExec<C> {
/// The length of the input table
fn get_input_length<'a>(
&self,
_builder: &mut ResultBuilder,
_alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> usize {
accessor.get_length(self.table.table_ref)
}

#[tracing::instrument(name = "FilterExec::result_evaluate", level = "debug", skip_all)]
fn result_evaluate<'a>(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ struct Dishonest;
impl ProverHonestyMarker for Dishonest {}
type DishonestFilterExec<C> = OstensibleFilterExec<C, Dishonest>;

impl ProverEvaluate<Curve25519Scalar> for DishonestFilterExec<RistrettoPoint> {
impl ProverEvaluate<RistrettoPoint> for DishonestFilterExec<RistrettoPoint> {
/// The length of the input table
fn get_input_length<'a>(
&self,
_builder: &mut ResultBuilder,
_alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> usize {
accessor.get_length(self.table.table_ref)
}

#[tracing::instrument(
name = "DishonestFilterExec::result_evaluate",
level = "debug",
Expand Down
16 changes: 11 additions & 5 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 @@ -92,10 +92,6 @@ impl<C: Commitment> ProofPlan<C> for GroupByExec<C> {
Ok(())
}

fn get_length(&self, accessor: &dyn MetadataAccessor) -> usize {
accessor.get_length(self.table.table_ref)
}

fn get_offset(&self, accessor: &dyn MetadataAccessor) -> usize {
accessor.get_offset(self.table.table_ref)
}
Expand Down Expand Up @@ -210,7 +206,17 @@ impl<C: Commitment> ProofPlan<C> for GroupByExec<C> {
}
}

impl<C: Commitment> ProverEvaluate<C::Scalar> for GroupByExec<C> {
impl<C: Commitment> ProverEvaluate<C> for GroupByExec<C> {
/// The length of the input table
fn get_input_length<'a>(
&self,
_builder: &mut ResultBuilder,
_alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> usize {
accessor.get_length(self.table.table_ref)
}

#[tracing::instrument(name = "GroupByExec::result_evaluate", level = "debug", skip_all)]
fn result_evaluate<'a>(
&self,
Expand Down
16 changes: 11 additions & 5 deletions crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ impl<C: Commitment> ProofPlan<C> for ProjectionExec<C> {
Ok(())
}

fn get_length(&self, accessor: &dyn MetadataAccessor) -> usize {
accessor.get_length(self.table.table_ref)
}

fn get_offset(&self, accessor: &dyn MetadataAccessor) -> usize {
accessor.get_offset(self.table.table_ref)
}
Expand Down Expand Up @@ -94,7 +90,17 @@ impl<C: Commitment> ProofPlan<C> for ProjectionExec<C> {
}
}

impl<C: Commitment> ProverEvaluate<C::Scalar> for ProjectionExec<C> {
impl<C: Commitment> ProverEvaluate<C> for ProjectionExec<C> {
/// The length of the input table
fn get_input_length<'a>(
&self,
_builder: &mut ResultBuilder,
_alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> usize {
accessor.get_length(self.table.table_ref)
}

#[tracing::instrument(name = "ProjectionExec::result_evaluate", level = "debug", skip_all)]
fn result_evaluate<'a>(
&self,
Expand Down

0 comments on commit 2ff406d

Please sign in to comment.