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!: add get_table_references, get_output_length and modify get_length #248

Closed
wants to merge 2 commits into from
Closed
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
Empty file added co.txt
Empty file.
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ mod test_accessor;
#[cfg(any(test, feature = "test"))]
pub use test_accessor::TestAccessor;
#[cfg(test)]
#[allow(unused_imports)]
pub(crate) use test_accessor::UnimplementedTestAccessor;

#[cfg(test)]
Expand Down
37 changes: 27 additions & 10 deletions crates/proof-of-sql/src/sql/proof/proof_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::base::{
commitment::Commitment,
database::{
Column, ColumnField, ColumnRef, CommitmentAccessor, DataAccessor, MetadataAccessor,
OwnedTable,
OwnedTable, TableRef,
},
map::IndexSet,
proof::ProofError,
Expand All @@ -22,17 +22,9 @@ pub trait ProofPlan<C: Commitment>: Debug + Send + Sync + ProverEvaluate<C::Scal
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 @@ -44,11 +36,36 @@ pub trait ProofPlan<C: Commitment>: Debug + Send + Sync + ProverEvaluate<C::Scal
/// Return all the result column fields
fn get_column_result_fields(&self) -> Vec<ColumnField>;

/// Return all the columns referenced in the Query
/// Return all the columns referenced in the query
fn get_column_references(&self) -> IndexSet<ColumnRef>;

/// Return all the tables referenced in the query
fn get_table_references(&self) -> IndexSet<TableRef> {
self.get_column_references()
.iter()
.map(ColumnRef::table_ref)
.collect()
}

/// Check if the input tables are all empty
fn is_empty(&self, accessor: &dyn MetadataAccessor) -> bool {
self.get_table_references()
.iter()
.all(|table_ref| accessor.get_length(*table_ref) == 0)
}
}

pub trait ProverEvaluate<S: Scalar> {
/// Get the length of the input tables
fn get_input_lengths<'a>(
&self,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
) -> Vec<usize>;

/// Get the length of the output table
fn get_output_length<'a>(&self, alloc: &'a Bump, accessor: &'a dyn DataAccessor<S>) -> usize;

/// Evaluate the query and modify `FirstRoundBuilder` to track the result of the query.
fn result_evaluate<'a>(
&self,
Expand Down
10 changes: 6 additions & 4 deletions crates/proof-of-sql/src/sql/proof/query_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
accessor: &impl DataAccessor<CP::Scalar>,
setup: &CP::ProverPublicSetup<'_>,
) -> (Self, ProvableQueryResult) {
let table_length = expr.get_length(accessor);
let alloc = Bump::new();
// TODO: Modify this to handle multiple tables
let table_length = expr.get_input_lengths(&alloc, accessor)[0];
let num_sumcheck_variables = cmp::max(log2_up(table_length), 1);
let generator_offset = expr.get_offset(accessor);
assert!(num_sumcheck_variables > 0);

let alloc = Bump::new();

// Evaluate query result
let result_cols = expr.result_evaluate(table_length, &alloc, accessor);
let output_length = result_cols.first().map_or(0, Column::len);
Expand Down Expand Up @@ -150,7 +150,9 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
result: &ProvableQueryResult,
setup: &CP::VerifierPublicSetup<'_>,
) -> QueryResult<CP::Scalar> {
let input_length = expr.get_length(accessor);
//TODO: Modify this when we have multiple tables
assert!(expr.get_table_references().len() == 1);
let input_length = accessor.get_length(*expr.get_table_references().first().unwrap());
let output_length = result.table_length();
let generator_offset = expr.get_offset(accessor);
let num_sumcheck_variables = cmp::max(log2_up(input_length), 1);
Expand Down
112 changes: 90 additions & 22 deletions crates/proof-of-sql/src/sql/proof/query_proof_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use crate::{
database::{
owned_table_utility::{bigint, owned_table},
Column, ColumnField, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor,
MetadataAccessor, OwnedTable, OwnedTableTestAccessor, TestAccessor,
UnimplementedTestAccessor,
MetadataAccessor, OwnedTable, OwnedTableTestAccessor,
},
map::IndexSet,
proof::ProofError,
Expand Down Expand Up @@ -41,6 +40,16 @@ impl Default for TrivialTestProofPlan {
}
}
impl<S: Scalar> ProverEvaluate<S> for TrivialTestProofPlan {
fn get_input_lengths<'a>(
&self,
_alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Vec<usize> {
vec![self.length]
}
fn get_output_length<'a>(&self, _alloc: &'a Bump, _accessor: &'a dyn DataAccessor<S>) -> usize {
self.length
}
fn result_evaluate<'a>(
&self,
_input_length: usize,
Expand Down Expand Up @@ -80,9 +89,6 @@ impl<C: Commitment> ProofPlan<C> for TrivialTestProofPlan {
builder.count_anchored_mles(self.anchored_mle_count);
Ok(())
}
fn get_length(&self, _accessor: &dyn MetadataAccessor) -> usize {
self.length
}
fn get_offset(&self, _accessor: &dyn MetadataAccessor) -> usize {
self.offset
}
Expand All @@ -107,7 +113,12 @@ impl<C: Commitment> ProofPlan<C> for TrivialTestProofPlan {
vec![ColumnField::new("a1".parse().unwrap(), ColumnType::BigInt)]
}
fn get_column_references(&self) -> IndexSet<ColumnRef> {
unimplemented!("no real usage for this function yet")
core::iter::once(ColumnRef::new(
"sxt.test".parse().unwrap(),
"a1".parse().unwrap(),
ColumnType::BigInt,
))
.collect()
}
}

Expand All @@ -117,7 +128,13 @@ fn verify_a_trivial_query_proof_with_given_offset(n: usize, offset_generators: u
offset: offset_generators,
..Default::default()
};
let accessor = UnimplementedTestAccessor::new_empty();
let column: Vec<i64> = vec![0_i64; n];
let accessor = OwnedTableTestAccessor::<InnerProductProof>::new_from_table(
"sxt.test".parse().unwrap(),
owned_table([bigint("a1", column)]),
offset_generators,
(),
);
let (proof, result) = QueryProof::<InnerProductProof>::new(&expr, &accessor, &());
let QueryData {
verification_hash,
Expand Down Expand Up @@ -150,7 +167,12 @@ fn verify_fails_if_the_summation_in_sumcheck_isnt_zero() {
column_fill_value: 123,
..Default::default()
};
let accessor = UnimplementedTestAccessor::new_empty();
let accessor = OwnedTableTestAccessor::<InnerProductProof>::new_from_table(
"sxt.test".parse().unwrap(),
owned_table([bigint("a1", [123_i64; 2])]),
0,
(),
);
let (proof, result) = QueryProof::<InnerProductProof>::new(&expr, &accessor, &());
assert!(proof.verify(&expr, &accessor, &result, &()).is_err());
}
Expand All @@ -163,7 +185,12 @@ fn verify_fails_if_the_sumcheck_evaluation_isnt_correct() {
evaluation: 123,
..Default::default()
};
let accessor = UnimplementedTestAccessor::new_empty();
let accessor = OwnedTableTestAccessor::<InnerProductProof>::new_from_table(
"sxt.test".parse().unwrap(),
owned_table([bigint("a1", [0_i64; 2])]),
0,
(),
);
let (proof, result) = QueryProof::<InnerProductProof>::new(&expr, &accessor, &());
assert!(proof.verify(&expr, &accessor, &result, &()).is_err());
}
Expand All @@ -176,7 +203,12 @@ fn verify_fails_if_counts_dont_match() {
anchored_mle_count: 1,
..Default::default()
};
let accessor = UnimplementedTestAccessor::new_empty();
let accessor = OwnedTableTestAccessor::<InnerProductProof>::new_from_table(
"sxt.test".parse().unwrap(),
owned_table([bigint("a1", [0_i64; 2])]),
0,
(),
);
let (proof, result) = QueryProof::<InnerProductProof>::new(&expr, &accessor, &());
assert!(proof.verify(&expr, &accessor, &result, &()).is_err());
}
Expand All @@ -198,6 +230,16 @@ impl Default for SquareTestProofPlan {
}
}
impl<S: Scalar> ProverEvaluate<S> for SquareTestProofPlan {
fn get_input_lengths<'a>(
&self,
_alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Vec<usize> {
vec![2]
}
fn get_output_length<'a>(&self, _alloc: &'a Bump, _accessor: &'a dyn DataAccessor<S>) -> usize {
2
}
fn result_evaluate<'a>(
&self,
_table_length: usize,
Expand Down Expand Up @@ -246,9 +288,6 @@ impl<C: Commitment> ProofPlan<C> for SquareTestProofPlan {
builder.count_anchored_mles(1);
Ok(())
}
fn get_length(&self, _accessor: &dyn MetadataAccessor) -> usize {
2
}
fn get_offset(&self, accessor: &dyn MetadataAccessor) -> usize {
accessor.get_offset("sxt.test".parse().unwrap())
}
Expand Down Expand Up @@ -276,7 +315,12 @@ impl<C: Commitment> ProofPlan<C> for SquareTestProofPlan {
vec![ColumnField::new("a1".parse().unwrap(), ColumnType::BigInt)]
}
fn get_column_references(&self) -> IndexSet<ColumnRef> {
unimplemented!("no real usage for this function yet")
core::iter::once(ColumnRef::new(
"sxt.test".parse().unwrap(),
"a1".parse().unwrap(),
ColumnType::BigInt,
))
.collect()
}
}

Expand Down Expand Up @@ -380,6 +424,16 @@ impl Default for DoubleSquareTestProofPlan {
}
}
impl<S: Scalar> ProverEvaluate<S> for DoubleSquareTestProofPlan {
fn get_input_lengths<'a>(
&self,
_alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Vec<usize> {
vec![2]
}
fn get_output_length<'a>(&self, _alloc: &'a Bump, _accessor: &'a dyn DataAccessor<S>) -> usize {
2
}
fn result_evaluate<'a>(
&self,
_input_length: usize,
Expand Down Expand Up @@ -441,9 +495,6 @@ impl<C: Commitment> ProofPlan<C> for DoubleSquareTestProofPlan {
builder.count_anchored_mles(1);
Ok(())
}
fn get_length(&self, _accessor: &dyn MetadataAccessor) -> usize {
2
}
fn get_offset(&self, accessor: &dyn MetadataAccessor) -> usize {
accessor.get_offset("sxt.test".parse().unwrap())
}
Expand Down Expand Up @@ -479,7 +530,12 @@ impl<C: Commitment> ProofPlan<C> for DoubleSquareTestProofPlan {
vec![ColumnField::new("a1".parse().unwrap(), ColumnType::BigInt)]
}
fn get_column_references(&self) -> IndexSet<ColumnRef> {
unimplemented!("no real usage for this function yet")
core::iter::once(ColumnRef::new(
"sxt.test".parse().unwrap(),
"a1".parse().unwrap(),
ColumnType::BigInt,
))
.collect()
}
}

Expand Down Expand Up @@ -592,6 +648,16 @@ fn verify_fails_the_result_doesnt_satisfy_an_intermediate_equation() {
#[derive(Debug, Serialize)]
struct ChallengeTestProofPlan {}
impl<S: Scalar> ProverEvaluate<S> for ChallengeTestProofPlan {
fn get_input_lengths<'a>(
&self,
_alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Vec<usize> {
vec![2]
}
fn get_output_length<'a>(&self, _alloc: &'a Bump, _accessor: &'a dyn DataAccessor<S>) -> usize {
2
}
fn result_evaluate<'a>(
&self,
_input_length: usize,
Expand Down Expand Up @@ -644,9 +710,6 @@ impl<C: Commitment> ProofPlan<C> for ChallengeTestProofPlan {
builder.count_post_result_challenges(2);
Ok(())
}
fn get_length(&self, _accessor: &dyn MetadataAccessor) -> usize {
2
}
fn get_offset(&self, accessor: &dyn MetadataAccessor) -> usize {
accessor.get_offset("sxt.test".parse().unwrap())
}
Expand Down Expand Up @@ -675,7 +738,12 @@ impl<C: Commitment> ProofPlan<C> for ChallengeTestProofPlan {
vec![ColumnField::new("a1".parse().unwrap(), ColumnType::BigInt)]
}
fn get_column_references(&self) -> IndexSet<ColumnRef> {
unimplemented!("no real usage for this function yet")
core::iter::once(ColumnRef::new(
"sxt.test".parse().unwrap(),
"a1".parse().unwrap(),
ColumnType::BigInt,
))
.collect()
}
}

Expand Down
Loading