diff --git a/crates/proof-of-sql/src/sql/proof/proof_builder.rs b/crates/proof-of-sql/src/sql/proof/final_round_builder.rs similarity index 93% rename from crates/proof-of-sql/src/sql/proof/proof_builder.rs rename to crates/proof-of-sql/src/sql/proof/final_round_builder.rs index 3b011aaa5..f3c6a1d39 100644 --- a/crates/proof-of-sql/src/sql/proof/proof_builder.rs +++ b/crates/proof-of-sql/src/sql/proof/final_round_builder.rs @@ -12,7 +12,7 @@ use alloc::{boxed::Box, vec, vec::Vec}; use num_traits::Zero; /// Track components used to form a query's proof -pub struct ProofBuilder<'a, S: Scalar> { +pub struct FinalRoundBuilder<'a, S: Scalar> { table_length: usize, num_sumcheck_variables: usize, bit_distributions: Vec, @@ -29,7 +29,7 @@ pub struct ProofBuilder<'a, S: Scalar> { post_result_challenges: Vec, } -impl<'a, S: Scalar> ProofBuilder<'a, S> { +impl<'a, S: Scalar> FinalRoundBuilder<'a, S> { pub fn new( table_length: usize, num_sumcheck_variables: usize, @@ -96,7 +96,7 @@ impl<'a, S: Scalar> ProofBuilder<'a, S> { /// Compute commitments of all the interemdiate MLEs used in sumcheck #[tracing::instrument( - name = "ProofBuilder::commit_intermediate_mles", + name = "FinalRoundBuilder::commit_intermediate_mles", level = "debug", skip_all )] @@ -115,7 +115,7 @@ impl<'a, S: Scalar> ProofBuilder<'a, S> { /// Given random multipliers, construct an aggregatated sumcheck polynomial from all /// the individual subpolynomials. #[tracing::instrument( - name = "ProofBuilder::make_sumcheck_polynomial", + name = "FinalRoundBuilder::make_sumcheck_polynomial", level = "debug", skip_all )] @@ -140,7 +140,7 @@ impl<'a, S: Scalar> ProofBuilder<'a, S> { /// Given the evaluation vector, compute evaluations of all the MLEs used in sumcheck except /// for those that correspond to result columns sent to the verifier. #[tracing::instrument( - name = "ProofBuilder::evaluate_pcs_proof_mles", + name = "FinalRoundBuilder::evaluate_pcs_proof_mles", level = "debug", skip_all )] @@ -154,7 +154,11 @@ impl<'a, S: Scalar> ProofBuilder<'a, S> { /// Given random multipliers, multiply and add together all of the MLEs used in sumcheck except /// for those that correspond to result columns sent to the verifier. - #[tracing::instrument(name = "ProofBuilder::fold_pcs_proof_mles", level = "debug", skip_all)] + #[tracing::instrument( + name = "FinalRoundBuilder::fold_pcs_proof_mles", + level = "debug", + skip_all + )] pub fn fold_pcs_proof_mles(&self, multipliers: &[S]) -> Vec { assert_eq!(multipliers.len(), self.pcs_proof_mles.len()); let mut res = vec![Zero::zero(); self.table_length]; diff --git a/crates/proof-of-sql/src/sql/proof/proof_builder_test.rs b/crates/proof-of-sql/src/sql/proof/final_round_builder_test.rs similarity index 92% rename from crates/proof-of-sql/src/sql/proof/proof_builder_test.rs rename to crates/proof-of-sql/src/sql/proof/final_round_builder_test.rs index 788920551..204821772 100644 --- a/crates/proof-of-sql/src/sql/proof/proof_builder_test.rs +++ b/crates/proof-of-sql/src/sql/proof/final_round_builder_test.rs @@ -1,4 +1,4 @@ -use super::{ProofBuilder, ProvableQueryResult, SumcheckRandomScalars}; +use super::{FinalRoundBuilder, ProvableQueryResult, SumcheckRandomScalars}; use crate::{ base::{ commitment::{Commitment, CommittableColumn}, @@ -22,7 +22,7 @@ use num_traits::{One, Zero}; fn we_can_compute_commitments_for_intermediate_mles_using_a_zero_offset() { let mle1 = [1, 2]; let mle2 = [10i64, 20]; - let mut builder = ProofBuilder::::new(2, 1, Vec::new()); + let mut builder = FinalRoundBuilder::::new(2, 1, Vec::new()); builder.produce_anchored_mle(&mle1); builder.produce_intermediate_mle(&mle2[..]); let offset_generators = 0_usize; @@ -41,7 +41,7 @@ fn we_can_compute_commitments_for_intermediate_mles_using_a_zero_offset() { fn we_can_compute_commitments_for_intermediate_mles_using_a_non_zero_offset() { let mle1 = [1, 2]; let mle2 = [10i64, 20]; - let mut builder = ProofBuilder::::new(2, 1, Vec::new()); + let mut builder = FinalRoundBuilder::::new(2, 1, Vec::new()); builder.produce_anchored_mle(&mle1); builder.produce_intermediate_mle(&mle2[..]); let offset_generators = 123_usize; @@ -60,7 +60,7 @@ fn we_can_compute_commitments_for_intermediate_mles_using_a_non_zero_offset() { fn we_can_evaluate_pcs_proof_mles() { let mle1 = [1, 2]; let mle2 = [10i64, 20]; - let mut builder = ProofBuilder::new(2, 1, Vec::new()); + let mut builder = FinalRoundBuilder::new(2, 1, Vec::new()); builder.produce_anchored_mle(&mle1); builder.produce_intermediate_mle(&mle2[..]); let evaluation_vec = [ @@ -80,7 +80,7 @@ fn we_can_form_an_aggregated_sumcheck_polynomial() { let mle1 = [1, 2, -1]; let mle2 = [10i64, 20, 100, 30]; let mle3 = [2000i64, 3000, 5000, 7000]; - let mut builder = ProofBuilder::new(4, 2, Vec::new()); + let mut builder = FinalRoundBuilder::new(4, 2, Vec::new()); builder.produce_anchored_mle(&mle1); builder.produce_intermediate_mle(&mle2[..]); builder.produce_intermediate_mle(&mle3[..]); @@ -170,7 +170,7 @@ fn we_can_form_the_provable_query_result() { fn we_can_fold_pcs_proof_mles() { let mle1 = [1, 2]; let mle2 = [10i64, 20]; - let mut builder = ProofBuilder::new(2, 1, Vec::new()); + let mut builder = FinalRoundBuilder::new(2, 1, Vec::new()); builder.produce_anchored_mle(&mle1); builder.produce_intermediate_mle(&mle2[..]); let multipliers = [Curve25519Scalar::from(100u64), Curve25519Scalar::from(2u64)]; @@ -184,7 +184,7 @@ fn we_can_fold_pcs_proof_mles() { #[test] fn we_can_consume_post_result_challenges_in_proof_builder() { - let mut builder = ProofBuilder::new( + let mut builder = FinalRoundBuilder::new( 0, 0, vec![ diff --git a/crates/proof-of-sql/src/sql/proof/result_builder.rs b/crates/proof-of-sql/src/sql/proof/first_round_builder.rs similarity index 76% rename from crates/proof-of-sql/src/sql/proof/result_builder.rs rename to crates/proof-of-sql/src/sql/proof/first_round_builder.rs index 4f296c46f..88d2c025a 100644 --- a/crates/proof-of-sql/src/sql/proof/result_builder.rs +++ b/crates/proof-of-sql/src/sql/proof/first_round_builder.rs @@ -1,7 +1,5 @@ /// Track the result created by a query -pub struct ResultBuilder { - result_table_length: usize, - +pub struct FirstRoundBuilder { /// The number of challenges used in the proof. /// Specifically, these are the challenges that the verifier sends to /// the prover after the prover sends the result, but before the prover @@ -9,31 +7,20 @@ pub struct ResultBuilder { num_post_result_challenges: usize, } -impl Default for ResultBuilder { +impl Default for FirstRoundBuilder { fn default() -> Self { Self::new() } } -impl ResultBuilder { +impl FirstRoundBuilder { /// Create a new result builder for a table with the given length. For multi table queries, this will likely need to change. pub fn new() -> Self { Self { - result_table_length: 0, num_post_result_challenges: 0, } } - /// Get the length of the output table - pub fn result_table_length(&self) -> usize { - self.result_table_length - } - - /// Set the length of the output table - pub fn set_result_table_length(&mut self, result_table_length: usize) { - self.result_table_length = result_table_length; - } - /// The number of challenges used in the proof. /// Specifically, these are the challenges that the verifier sends to /// the prover after the prover sends the result, but before the prover diff --git a/crates/proof-of-sql/src/sql/proof/mod.rs b/crates/proof-of-sql/src/sql/proof/mod.rs index c3082b476..48139dc22 100644 --- a/crates/proof-of-sql/src/sql/proof/mod.rs +++ b/crates/proof-of-sql/src/sql/proof/mod.rs @@ -2,10 +2,10 @@ mod count_builder; pub(crate) use count_builder::CountBuilder; -mod proof_builder; -pub(crate) use proof_builder::ProofBuilder; +mod final_round_builder; +pub(crate) use final_round_builder::FinalRoundBuilder; #[cfg(all(test, feature = "blitzar"))] -mod proof_builder_test; +mod final_round_builder_test; mod composite_polynomial_builder; pub(crate) use composite_polynomial_builder::CompositePolynomialBuilder; @@ -68,5 +68,5 @@ pub(crate) use result_element_serialization::{ decode_and_convert, decode_multiple_elements, ProvableResultElement, }; -mod result_builder; -pub(crate) use result_builder::ResultBuilder; +mod first_round_builder; +pub(crate) use first_round_builder::FirstRoundBuilder; diff --git a/crates/proof-of-sql/src/sql/proof/proof_plan.rs b/crates/proof-of-sql/src/sql/proof/proof_plan.rs index f500d8b0a..430485308 100644 --- a/crates/proof-of-sql/src/sql/proof/proof_plan.rs +++ b/crates/proof-of-sql/src/sql/proof/proof_plan.rs @@ -1,4 +1,4 @@ -use super::{CountBuilder, ProofBuilder, ResultBuilder, VerificationBuilder}; +use super::{CountBuilder, FinalRoundBuilder, FirstRoundBuilder, VerificationBuilder}; use crate::base::{ commitment::Commitment, database::{ @@ -49,23 +49,26 @@ pub trait ProofPlan: Debug + Send + Sync + ProverEvaluate { - /// Evaluate the query and modify `ResultBuilder` to track the result of the query. + /// Evaluate the query and modify `FirstRoundBuilder` to track the result of the query. fn result_evaluate<'a>( &self, - builder: &mut ResultBuilder, + input_length: usize, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec>; - /// Evaluate the query and modify `ProofBuilder` to store an intermediate representation + /// Evaluate the query and modify `FirstRoundBuilder` to form the query's proof. + fn first_round_evaluate(&self, builder: &mut FirstRoundBuilder); + + /// Evaluate the query and modify `FinalRoundBuilder` to store an intermediate representation /// of the query result and track all the components needed to form the query's proof. /// /// Intermediate values that are needed to form the proof are allocated into the arena /// allocator alloc. These intermediate values will persist through proof creation and /// will be bulk deallocated once the proof is formed. - fn prover_evaluate<'a>( + fn final_round_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, S>, + builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec>; diff --git a/crates/proof-of-sql/src/sql/proof/query_proof.rs b/crates/proof-of-sql/src/sql/proof/query_proof.rs index afa488d7c..62f3ada00 100644 --- a/crates/proof-of-sql/src/sql/proof/query_proof.rs +++ b/crates/proof-of-sql/src/sql/proof/query_proof.rs @@ -1,18 +1,18 @@ use super::{ - CountBuilder, ProofBuilder, ProofCounts, ProofPlan, ProvableQueryResult, QueryResult, + CountBuilder, FinalRoundBuilder, ProofCounts, ProofPlan, ProvableQueryResult, QueryResult, SumcheckMleEvaluations, SumcheckRandomScalars, VerificationBuilder, }; use crate::{ base::{ bit::BitDistribution, commitment::{Commitment, CommitmentEvaluationProof}, - database::{CommitmentAccessor, DataAccessor}, + database::{Column, CommitmentAccessor, DataAccessor}, math::log2_up, polynomial::{compute_evaluation_vector, CompositePolynomialInfo}, proof::{Keccak256Transcript, ProofError, Transcript}, }, proof_primitive::sumcheck::SumcheckProof, - sql::proof::{QueryData, ResultBuilder}, + sql::proof::{FirstRoundBuilder, QueryData}, }; use alloc::{vec, vec::Vec}; use bumpalo::Bump; @@ -53,10 +53,15 @@ impl QueryProof { assert!(num_sumcheck_variables > 0); let alloc = Bump::new(); - let mut result_builder = ResultBuilder::new(); - let result_cols = expr.result_evaluate(&mut result_builder, &alloc, accessor); - let provable_result = - ProvableQueryResult::new(result_builder.result_table_length() as u64, &result_cols); + + // Evaluate query result + let result_cols = expr.result_evaluate(table_length, &alloc, accessor); + let output_length = result_cols.first().map_or(0, Column::len); + let provable_result = ProvableQueryResult::new(output_length as u64, &result_cols); + + // Prover First Round + let mut first_round_builder = FirstRoundBuilder::new(); + expr.first_round_evaluate(&mut first_round_builder); // construct a transcript for the proof let mut transcript: Keccak256Transcript = @@ -69,12 +74,12 @@ impl QueryProof { // Note: the last challenge in the vec is the first one that is consumed. let post_result_challenges = core::iter::repeat_with(|| transcript.scalar_challenge_as_be()) - .take(result_builder.num_post_result_challenges()) + .take(first_round_builder.num_post_result_challenges()) .collect(); let mut builder = - ProofBuilder::new(table_length, num_sumcheck_variables, post_result_challenges); - expr.prover_evaluate(&mut builder, &alloc, accessor); + FinalRoundBuilder::new(table_length, num_sumcheck_variables, post_result_challenges); + expr.final_round_evaluate(&mut builder, &alloc, accessor); let num_sumcheck_variables = builder.num_sumcheck_variables(); let table_length = builder.table_length(); diff --git a/crates/proof-of-sql/src/sql/proof/query_proof_test.rs b/crates/proof-of-sql/src/sql/proof/query_proof_test.rs index 3817c3e19..684cde01e 100644 --- a/crates/proof-of-sql/src/sql/proof/query_proof_test.rs +++ b/crates/proof-of-sql/src/sql/proof/query_proof_test.rs @@ -1,5 +1,5 @@ use super::{ - CountBuilder, ProofBuilder, ProofPlan, ProverEvaluate, QueryProof, VerificationBuilder, + CountBuilder, FinalRoundBuilder, ProofPlan, ProverEvaluate, QueryProof, VerificationBuilder, }; use crate::{ base::{ @@ -14,7 +14,7 @@ use crate::{ proof::ProofError, scalar::{Curve25519Scalar, Scalar}, }, - sql::proof::{QueryData, ResultBuilder, SumcheckSubpolynomialType}, + sql::proof::{FirstRoundBuilder, QueryData, SumcheckSubpolynomialType}, }; use bumpalo::Bump; use serde::Serialize; @@ -43,19 +43,19 @@ impl Default for TrivialTestProofPlan { impl ProverEvaluate for TrivialTestProofPlan { fn result_evaluate<'a>( &self, - builder: &mut ResultBuilder, + _input_length: usize, alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { - let input_length = self.length; - let col = alloc.alloc_slice_fill_copy(input_length, self.column_fill_value); - builder.set_result_table_length(input_length); + let col = alloc.alloc_slice_fill_copy(self.length, self.column_fill_value); vec![Column::BigInt(col)] } - fn prover_evaluate<'a>( + fn first_round_evaluate(&self, _builder: &mut FirstRoundBuilder) {} + + fn final_round_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, S>, + builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { @@ -200,18 +200,19 @@ impl Default for SquareTestProofPlan { impl ProverEvaluate for SquareTestProofPlan { fn result_evaluate<'a>( &self, - builder: &mut ResultBuilder, + _table_length: usize, alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { let res: &[_] = alloc.alloc_slice_copy(&self.res); - builder.set_result_table_length(2); vec![Column::BigInt(res)] } - fn prover_evaluate<'a>( + fn first_round_evaluate(&self, _builder: &mut FirstRoundBuilder) {} + + fn final_round_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, S>, + builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { @@ -381,18 +382,19 @@ impl Default for DoubleSquareTestProofPlan { impl ProverEvaluate for DoubleSquareTestProofPlan { fn result_evaluate<'a>( &self, - builder: &mut ResultBuilder, + _input_length: usize, alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { let res: &[_] = alloc.alloc_slice_copy(&self.res); - builder.set_result_table_length(2); vec![Column::BigInt(res)] } - fn prover_evaluate<'a>( + fn first_round_evaluate(&self, _builder: &mut FirstRoundBuilder) {} + + fn final_round_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, S>, + builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { @@ -592,18 +594,20 @@ struct ChallengeTestProofPlan {} impl ProverEvaluate for ChallengeTestProofPlan { fn result_evaluate<'a>( &self, - builder: &mut ResultBuilder, + _input_length: usize, _alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { - builder.request_post_result_challenges(2); - builder.set_result_table_length(2); vec![Column::BigInt(&[9, 25])] } - fn prover_evaluate<'a>( + fn first_round_evaluate(&self, builder: &mut FirstRoundBuilder) { + builder.request_post_result_challenges(2); + } + + fn final_round_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, S>, + builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { diff --git a/crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs b/crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs index 505984fb8..5d299e408 100644 --- a/crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs +++ b/crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs @@ -1,5 +1,5 @@ use super::{ - CountBuilder, ProofBuilder, ProofPlan, ProverEvaluate, VerifiableQueryResult, + CountBuilder, FinalRoundBuilder, ProofPlan, ProverEvaluate, VerifiableQueryResult, VerificationBuilder, }; use crate::{ @@ -14,7 +14,7 @@ use crate::{ proof::ProofError, scalar::Scalar, }, - sql::proof::{ProvableQueryResult, QueryData, ResultBuilder}, + sql::proof::{FirstRoundBuilder, ProvableQueryResult, QueryData}, }; use bumpalo::Bump; use serde::Serialize; @@ -27,18 +27,18 @@ pub(super) struct EmptyTestQueryExpr { impl ProverEvaluate for EmptyTestQueryExpr { fn result_evaluate<'a>( &self, - builder: &mut ResultBuilder, + _input_length: usize, alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { let zeros = vec![0; self.length]; let res: &[_] = alloc.alloc_slice_copy(&zeros); - builder.set_result_table_length(self.length); vec![Column::BigInt(res); self.columns] } - fn prover_evaluate<'a>( + fn first_round_evaluate(&self, _builder: &mut FirstRoundBuilder) {} + fn final_round_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, S>, + builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Vec> { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr.rs index 2b7611c06..10a623f85 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr.rs @@ -9,7 +9,7 @@ use crate::{ map::IndexSet, proof::ProofError, }, - sql::proof::{CountBuilder, ProofBuilder, VerificationBuilder}, + sql::proof::{CountBuilder, FinalRoundBuilder, VerificationBuilder}, }; use alloc::boxed::Box; use bumpalo::Bump; @@ -79,7 +79,7 @@ impl ProofExpr for AddSubtractExpr { )] fn prover_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Column<'a, C::Scalar> { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/aggregate_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/aggregate_expr.rs index ddeb08b77..d11c157b5 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/aggregate_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/aggregate_expr.rs @@ -6,7 +6,7 @@ use crate::{ map::IndexSet, proof::ProofError, }, - sql::proof::{CountBuilder, ProofBuilder, VerificationBuilder}, + sql::proof::{CountBuilder, FinalRoundBuilder, VerificationBuilder}, }; use alloc::boxed::Box; use bumpalo::Bump; @@ -55,7 +55,7 @@ impl ProofExpr for AggregateExpr { #[tracing::instrument(name = "AggregateExpr::prover_evaluate", level = "debug", skip_all)] fn prover_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Column<'a, C::Scalar> { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/and_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/and_expr.rs index 190e9f7a7..d1166733d 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/and_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/and_expr.rs @@ -6,7 +6,7 @@ use crate::{ map::IndexSet, proof::ProofError, }, - sql::proof::{CountBuilder, ProofBuilder, SumcheckSubpolynomialType, VerificationBuilder}, + sql::proof::{CountBuilder, FinalRoundBuilder, SumcheckSubpolynomialType, VerificationBuilder}, }; use alloc::{boxed::Box, vec}; use bumpalo::Bump; @@ -60,7 +60,7 @@ impl ProofExpr for AndExpr { #[tracing::instrument(name = "AndExpr::prover_evaluate", level = "debug", skip_all)] fn prover_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Column<'a, C::Scalar> { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/column_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/column_expr.rs index ba078a100..93b7be813 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/column_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/column_expr.rs @@ -6,7 +6,7 @@ use crate::{ map::IndexSet, proof::ProofError, }, - sql::proof::{CountBuilder, ProofBuilder, VerificationBuilder}, + sql::proof::{CountBuilder, FinalRoundBuilder, VerificationBuilder}, }; use bumpalo::Bump; use core::marker::PhantomData; @@ -59,7 +59,7 @@ impl ProofExpr for ColumnExpr { } /// Evaluate the column expression and - /// add the result to the [`ResultBuilder`](crate::sql::proof::ResultBuilder) + /// add the result to the [`FirstRoundBuilder`](crate::sql::proof::FirstRoundBuilder) fn result_evaluate<'a>( &self, table_length: usize, @@ -75,7 +75,7 @@ impl ProofExpr for ColumnExpr { /// add the components needed to prove the result fn prover_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, _alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Column<'a, C::Scalar> { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/dyn_proof_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/dyn_proof_expr.rs index eae4c37d3..88527ad60 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/dyn_proof_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/dyn_proof_expr.rs @@ -11,7 +11,7 @@ use crate::{ }, sql::{ parse::{type_check_binary_operation, ConversionError, ConversionResult}, - proof::{CountBuilder, ProofBuilder, VerificationBuilder}, + proof::{CountBuilder, FinalRoundBuilder, VerificationBuilder}, }, }; use alloc::{boxed::Box, string::ToString}; @@ -253,7 +253,7 @@ impl ProofExpr for DynProofExpr { fn prover_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Column<'a, C::Scalar> { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/equals_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/equals_expr.rs index faf9af5e1..58cfa6155 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/equals_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/equals_expr.rs @@ -8,7 +8,7 @@ use crate::{ scalar::Scalar, slice_ops, }, - sql::proof::{CountBuilder, ProofBuilder, SumcheckSubpolynomialType, VerificationBuilder}, + sql::proof::{CountBuilder, FinalRoundBuilder, SumcheckSubpolynomialType, VerificationBuilder}, }; use alloc::{boxed::Box, vec}; use bumpalo::Bump; @@ -59,7 +59,7 @@ impl ProofExpr for EqualsExpr { #[tracing::instrument(name = "EqualsExpr::prover_evaluate", level = "debug", skip_all)] fn prover_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Column<'a, C::Scalar> { @@ -105,7 +105,7 @@ pub fn result_evaluate_equals_zero<'a, S: Scalar>( } pub fn prover_evaluate_equals_zero<'a, S: Scalar>( - builder: &mut ProofBuilder<'a, S>, + builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, lhs: &'a [S], ) -> &'a [bool] { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/inequality_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/inequality_expr.rs index 06ce2a217..f1f647682 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/inequality_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/inequality_expr.rs @@ -11,7 +11,7 @@ use crate::{ map::IndexSet, proof::ProofError, }, - sql::proof::{CountBuilder, ProofBuilder, VerificationBuilder}, + sql::proof::{CountBuilder, FinalRoundBuilder, VerificationBuilder}, }; use alloc::boxed::Box; use bumpalo::Bump; @@ -86,7 +86,7 @@ impl ProofExpr for InequalityExpr { #[tracing::instrument(name = "InequalityExpr::prover_evaluate", level = "debug", skip_all)] fn prover_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Column<'a, C::Scalar> { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/literal_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/literal_expr.rs index 4ea429cc5..c00af32d1 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/literal_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/literal_expr.rs @@ -7,7 +7,7 @@ use crate::{ proof::ProofError, scalar::Scalar, }, - sql::proof::{CountBuilder, ProofBuilder, VerificationBuilder}, + sql::proof::{CountBuilder, FinalRoundBuilder, VerificationBuilder}, }; use bumpalo::Bump; use serde::{Deserialize, Serialize}; @@ -57,7 +57,7 @@ impl ProofExpr for LiteralExpr { #[tracing::instrument(name = "LiteralExpr::prover_evaluate", level = "debug", skip_all)] fn prover_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, _accessor: &'a dyn DataAccessor, ) -> Column<'a, C::Scalar> { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/multiply_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/multiply_expr.rs index 391091745..9e3dad92e 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/multiply_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/multiply_expr.rs @@ -10,7 +10,7 @@ use crate::{ proof::ProofError, }, sql::{ - proof::{CountBuilder, ProofBuilder, SumcheckSubpolynomialType, VerificationBuilder}, + proof::{CountBuilder, FinalRoundBuilder, SumcheckSubpolynomialType, VerificationBuilder}, proof_exprs::multiply_columns, }, }; @@ -69,7 +69,7 @@ impl ProofExpr for MultiplyExpr { )] fn prover_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Column<'a, C::Scalar> { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/not_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/not_expr.rs index c0f2f899f..194d5b9be 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/not_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/not_expr.rs @@ -6,7 +6,7 @@ use crate::{ map::IndexSet, proof::ProofError, }, - sql::proof::{CountBuilder, ProofBuilder, VerificationBuilder}, + sql::proof::{CountBuilder, FinalRoundBuilder, VerificationBuilder}, }; use alloc::boxed::Box; use bumpalo::Bump; @@ -50,7 +50,7 @@ impl ProofExpr for NotExpr { #[tracing::instrument(name = "NotExpr::prover_evaluate", level = "debug", skip_all)] fn prover_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Column<'a, C::Scalar> { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/or_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/or_expr.rs index efdeb3d5a..e31c2a9c9 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/or_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/or_expr.rs @@ -7,7 +7,7 @@ use crate::{ proof::ProofError, scalar::Scalar, }, - sql::proof::{CountBuilder, ProofBuilder, SumcheckSubpolynomialType, VerificationBuilder}, + sql::proof::{CountBuilder, FinalRoundBuilder, SumcheckSubpolynomialType, VerificationBuilder}, }; use alloc::{boxed::Box, vec}; use bumpalo::Bump; @@ -58,7 +58,7 @@ impl ProofExpr for OrExpr { #[tracing::instrument(name = "OrExpr::prover_evaluate", level = "debug", skip_all)] fn prover_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Column<'a, C::Scalar> { @@ -106,7 +106,7 @@ pub fn result_evaluate_or<'a>( reason = "lhs and rhs are guaranteed to have the same length, ensuring no panic occurs" )] pub fn prover_evaluate_or<'a, S: Scalar>( - builder: &mut ProofBuilder<'a, S>, + builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, lhs: &'a [bool], rhs: &'a [bool], diff --git a/crates/proof-of-sql/src/sql/proof_exprs/proof_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/proof_expr.rs index 681fd0194..88f215484 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/proof_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/proof_expr.rs @@ -5,7 +5,7 @@ use crate::{ map::IndexSet, proof::ProofError, }, - sql::proof::{CountBuilder, ProofBuilder, VerificationBuilder}, + sql::proof::{CountBuilder, FinalRoundBuilder, VerificationBuilder}, }; use bumpalo::Bump; use core::fmt::Debug; @@ -32,7 +32,7 @@ pub trait ProofExpr: Debug + Send + Sync { /// of values fn prover_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Column<'a, C::Scalar>; diff --git a/crates/proof-of-sql/src/sql/proof_exprs/sign_expr.rs b/crates/proof-of-sql/src/sql/proof_exprs/sign_expr.rs index 9e614d22e..5dc668972 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/sign_expr.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/sign_expr.rs @@ -10,7 +10,7 @@ use crate::{ scalar::Scalar, }, sql::proof::{ - CountBuilder, ProofBuilder, SumcheckSubpolynomialTerm, SumcheckSubpolynomialType, + CountBuilder, FinalRoundBuilder, SumcheckSubpolynomialTerm, SumcheckSubpolynomialType, VerificationBuilder, }, }; @@ -80,7 +80,7 @@ pub fn result_evaluate_sign<'a, S: Scalar>( /// Note: We can only prove the sign bit for non-zero scalars, and we restict /// the range of non-zero scalar so that there is a unique sign representation. pub fn prover_evaluate_sign<'a, S: Scalar>( - builder: &mut ProofBuilder<'a, S>, + builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, expr: &'a [S], #[cfg(test)] treat_column_of_zeros_as_negative: bool, @@ -173,7 +173,10 @@ fn verifier_const_sign_evaluate( } } -fn prove_bits_are_binary<'a, S: Scalar>(builder: &mut ProofBuilder<'a, S>, bits: &[&'a [bool]]) { +fn prove_bits_are_binary<'a, S: Scalar>( + builder: &mut FinalRoundBuilder<'a, S>, + bits: &[&'a [bool]], +) { for &seq in bits { builder.produce_intermediate_mle(seq); builder.produce_sumcheck_subpolynomial( @@ -203,7 +206,7 @@ fn verify_bits_are_binary( /// /// This function generates subpolynomial terms for sumcheck, involving the scalar expression and its bit decomposition. fn prove_bit_decomposition<'a, S: Scalar>( - builder: &mut ProofBuilder<'a, S>, + builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, expr: &'a [S], bits: &[&'a [bool]], diff --git a/crates/proof-of-sql/src/sql/proof_exprs/sign_expr_test.rs b/crates/proof-of-sql/src/sql/proof_exprs/sign_expr_test.rs index 7e24b66ae..61ec2402c 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/sign_expr_test.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/sign_expr_test.rs @@ -2,7 +2,7 @@ use super::{count_sign, prover_evaluate_sign, result_evaluate_sign, verifier_eva use crate::{ base::{bit::BitDistribution, polynomial::MultilinearExtension, scalar::Curve25519Scalar}, sql::proof::{ - CountBuilder, ProofBuilder, SumcheckMleEvaluations, SumcheckRandomScalars, + CountBuilder, FinalRoundBuilder, SumcheckMleEvaluations, SumcheckRandomScalars, VerificationBuilder, }, }; @@ -16,7 +16,7 @@ fn prover_evaluation_generates_the_bit_distribution_of_a_constant_column() { let dist = BitDistribution::new::(&data); let alloc = Bump::new(); let data: Vec = data.into_iter().map(Curve25519Scalar::from).collect(); - let mut builder = ProofBuilder::new(3, 2, Vec::new()); + let mut builder = FinalRoundBuilder::new(3, 2, Vec::new()); let sign = prover_evaluate_sign(&mut builder, &alloc, &data, false); assert_eq!(sign, [false; 3]); assert_eq!(builder.bit_distributions(), [dist]); @@ -28,7 +28,7 @@ fn prover_evaluation_generates_the_bit_distribution_of_a_negative_constant_colum let dist = BitDistribution::new::(&data); let alloc = Bump::new(); let data: Vec = data.into_iter().map(Curve25519Scalar::from).collect(); - let mut builder = ProofBuilder::new(3, 2, Vec::new()); + let mut builder = FinalRoundBuilder::new(3, 2, Vec::new()); let sign = prover_evaluate_sign(&mut builder, &alloc, &data, false); assert_eq!(sign, [true; 3]); assert_eq!(builder.bit_distributions(), [dist]); diff --git a/crates/proof-of-sql/src/sql/proof_plans/dyn_proof_plan.rs b/crates/proof-of-sql/src/sql/proof_plans/dyn_proof_plan.rs index 2ee976dd2..c524a2c76 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/dyn_proof_plan.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/dyn_proof_plan.rs @@ -95,28 +95,36 @@ impl ProverEvaluate for DynProofPlan { #[tracing::instrument(name = "DynProofPlan::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a>( &self, - builder: &mut crate::sql::proof::ResultBuilder, + input_length: usize, alloc: &'a bumpalo::Bump, accessor: &'a dyn crate::base::database::DataAccessor, ) -> Vec> { match self { - DynProofPlan::Projection(expr) => expr.result_evaluate(builder, alloc, accessor), - DynProofPlan::GroupBy(expr) => expr.result_evaluate(builder, alloc, accessor), - DynProofPlan::Filter(expr) => expr.result_evaluate(builder, alloc, accessor), + DynProofPlan::Projection(expr) => expr.result_evaluate(input_length, alloc, accessor), + DynProofPlan::GroupBy(expr) => expr.result_evaluate(input_length, alloc, accessor), + DynProofPlan::Filter(expr) => expr.result_evaluate(input_length, alloc, accessor), } } - #[tracing::instrument(name = "DynProofPlan::prover_evaluate", level = "debug", skip_all)] - fn prover_evaluate<'a>( + fn first_round_evaluate(&self, builder: &mut crate::sql::proof::FirstRoundBuilder) { + match self { + DynProofPlan::Projection(expr) => expr.first_round_evaluate(builder), + DynProofPlan::GroupBy(expr) => expr.first_round_evaluate(builder), + DynProofPlan::Filter(expr) => expr.first_round_evaluate(builder), + } + } + + #[tracing::instrument(name = "DynProofPlan::final_round_evaluate", level = "debug", skip_all)] + fn final_round_evaluate<'a>( &self, - builder: &mut crate::sql::proof::ProofBuilder<'a, C::Scalar>, + builder: &mut crate::sql::proof::FinalRoundBuilder<'a, C::Scalar>, alloc: &'a bumpalo::Bump, accessor: &'a dyn crate::base::database::DataAccessor, ) -> Vec> { match self { - DynProofPlan::Projection(expr) => expr.prover_evaluate(builder, alloc, accessor), - DynProofPlan::GroupBy(expr) => expr.prover_evaluate(builder, alloc, accessor), - DynProofPlan::Filter(expr) => expr.prover_evaluate(builder, alloc, accessor), + DynProofPlan::Projection(expr) => expr.final_round_evaluate(builder, alloc, accessor), + DynProofPlan::GroupBy(expr) => expr.final_round_evaluate(builder, alloc, accessor), + DynProofPlan::Filter(expr) => expr.final_round_evaluate(builder, alloc, accessor), } } } diff --git a/crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs b/crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs index 2408a8303..28895df61 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs @@ -13,8 +13,8 @@ use crate::{ }, sql::{ proof::{ - CountBuilder, HonestProver, ProofBuilder, ProofPlan, ProverEvaluate, - ProverHonestyMarker, ResultBuilder, SumcheckSubpolynomialType, VerificationBuilder, + CountBuilder, FinalRoundBuilder, FirstRoundBuilder, HonestProver, ProofPlan, + ProverEvaluate, ProverHonestyMarker, SumcheckSubpolynomialType, VerificationBuilder, }, proof_exprs::{AliasedDynProofExpr, DynProofExpr, ProofExpr, TableExpr}, }, @@ -148,11 +148,10 @@ impl ProverEvaluate for FilterExec { #[tracing::instrument(name = "FilterExec::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a>( &self, - builder: &mut ResultBuilder, + input_length: usize, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { - let input_length = accessor.get_length(self.table.table_ref); // 1. selection let selection_column: Column<'a, C::Scalar> = self.where_clause @@ -173,17 +172,19 @@ impl ProverEvaluate for FilterExec { .collect(); // Compute filtered_columns and indexes - let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection); - builder.set_result_table_length(result_len); - builder.request_post_result_challenges(2); + let (filtered_columns, _) = filter_columns(alloc, &columns, selection); filtered_columns } - #[tracing::instrument(name = "FilterExec::prover_evaluate", level = "debug", skip_all)] + fn first_round_evaluate(&self, builder: &mut FirstRoundBuilder) { + builder.request_post_result_challenges(2); + } + + #[tracing::instrument(name = "FilterExec::final_round_evaluate", level = "debug", skip_all)] #[allow(unused_variables)] - fn prover_evaluate<'a>( + fn final_round_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { @@ -263,7 +264,7 @@ fn verify_filter( #[allow(clippy::too_many_arguments, clippy::many_single_char_names)] pub(super) fn prove_filter<'a, S: Scalar + 'a>( - builder: &mut ProofBuilder<'a, S>, + builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, alpha: S, beta: S, diff --git a/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs b/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs index f9a9363c3..c6252d133 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs @@ -2,8 +2,8 @@ use super::{test_utility::*, FilterExec}; use crate::{ base::{ database::{ - owned_table_utility::*, ColumnField, ColumnRef, ColumnType, LiteralValue, OwnedTable, - OwnedTableTestAccessor, TableRef, TestAccessor, + owned_table_utility::*, Column, ColumnField, ColumnRef, ColumnType, LiteralValue, + OwnedTable, OwnedTableTestAccessor, TableRef, TestAccessor, }, map::{IndexMap, IndexSet}, math::decimal::Precision, @@ -11,8 +11,8 @@ use crate::{ }, sql::{ proof::{ - exercise_verification, ProofPlan, ProvableQueryResult, ProverEvaluate, ResultBuilder, - VerifiableQueryResult, + exercise_verification, FirstRoundBuilder, ProofPlan, ProvableQueryResult, + ProverEvaluate, VerifiableQueryResult, }, proof_exprs::{test_utility::*, ColumnExpr, DynProofExpr, LiteralExpr, TableExpr}, }, @@ -192,8 +192,10 @@ fn we_can_get_an_empty_result_from_a_basic_filter_on_an_empty_table_using_result where_clause, ); let alloc = Bump::new(); - let mut builder = ResultBuilder::new(); - let result_cols = expr.result_evaluate(&mut builder, &alloc, &accessor); + let result_cols = expr.result_evaluate(0, &alloc, &accessor); + let output_length = result_cols.first().map_or(0, Column::len) as u64; + let mut builder = FirstRoundBuilder::new(); + expr.first_round_evaluate(&mut builder); let fields = &[ ColumnField::new("b".parse().unwrap(), ColumnType::BigInt), ColumnField::new("c".parse().unwrap(), ColumnType::Int128), @@ -204,7 +206,7 @@ fn we_can_get_an_empty_result_from_a_basic_filter_on_an_empty_table_using_result ), ]; let res: OwnedTable = - ProvableQueryResult::new(builder.result_table_length() as u64, &result_cols) + ProvableQueryResult::new(output_length as u64, &result_cols) .to_owned_table(fields) .unwrap(); let expected: OwnedTable = owned_table([ @@ -237,8 +239,10 @@ fn we_can_get_an_empty_result_from_a_basic_filter_using_result_evaluate() { where_clause, ); let alloc = Bump::new(); - let mut builder = ResultBuilder::new(); - let result_cols = expr.result_evaluate(&mut builder, &alloc, &accessor); + let result_cols = expr.result_evaluate(5, &alloc, &accessor); + let output_length = result_cols.first().map_or(0, Column::len) as u64; + let mut builder = FirstRoundBuilder::new(); + expr.first_round_evaluate(&mut builder); let fields = &[ ColumnField::new("b".parse().unwrap(), ColumnType::BigInt), ColumnField::new("c".parse().unwrap(), ColumnType::Int128), @@ -249,7 +253,7 @@ fn we_can_get_an_empty_result_from_a_basic_filter_using_result_evaluate() { ), ]; let res: OwnedTable = - ProvableQueryResult::new(builder.result_table_length() as u64, &result_cols) + ProvableQueryResult::new(output_length as u64, &result_cols) .to_owned_table(fields) .unwrap(); let expected: OwnedTable = owned_table([ @@ -278,11 +282,13 @@ fn we_can_get_no_columns_from_a_basic_filter_with_no_selected_columns_using_resu equal(column(t, "a", &accessor), const_int128(5)); let expr = filter(cols_expr_plan(t, &[], &accessor), tab(t), where_clause); let alloc = Bump::new(); - let mut builder = ResultBuilder::new(); - let result_cols = expr.result_evaluate(&mut builder, &alloc, &accessor); + let result_cols = expr.result_evaluate(5, &alloc, &accessor); + let output_length = result_cols.first().map_or(0, Column::len) as u64; + let mut builder = FirstRoundBuilder::new(); + expr.first_round_evaluate(&mut builder); let fields = &[]; let res: OwnedTable = - ProvableQueryResult::new(builder.result_table_length() as u64, &result_cols) + ProvableQueryResult::new(output_length as u64, &result_cols) .to_owned_table(fields) .unwrap(); let expected = OwnedTable::try_new(IndexMap::default()).unwrap(); @@ -309,8 +315,10 @@ fn we_can_get_the_correct_result_from_a_basic_filter_using_result_evaluate() { where_clause, ); let alloc = Bump::new(); - let mut builder = ResultBuilder::new(); - let result_cols = expr.result_evaluate(&mut builder, &alloc, &accessor); + let result_cols = expr.result_evaluate(5, &alloc, &accessor); + let output_length = result_cols.first().map_or(0, Column::len) as u64; + let mut builder = FirstRoundBuilder::new(); + expr.first_round_evaluate(&mut builder); let fields = &[ ColumnField::new("b".parse().unwrap(), ColumnType::BigInt), ColumnField::new("c".parse().unwrap(), ColumnType::Int128), @@ -321,7 +329,7 @@ fn we_can_get_the_correct_result_from_a_basic_filter_using_result_evaluate() { ), ]; let res: OwnedTable = - ProvableQueryResult::new(builder.result_table_length() as u64, &result_cols) + ProvableQueryResult::new(output_length as u64, &result_cols) .to_owned_table(fields) .unwrap(); let expected: OwnedTable = owned_table([ diff --git a/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test_dishonest_prover.rs b/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test_dishonest_prover.rs index efcb0695c..4d32bc735 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test_dishonest_prover.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test_dishonest_prover.rs @@ -8,7 +8,7 @@ use crate::{ }, sql::{ proof::{ - ProofBuilder, ProverEvaluate, ProverHonestyMarker, QueryError, ResultBuilder, + FinalRoundBuilder, FirstRoundBuilder, ProverEvaluate, ProverHonestyMarker, QueryError, VerifiableQueryResult, }, // Making this explicit to ensure that we don't accidentally use the @@ -37,11 +37,10 @@ impl ProverEvaluate for DishonestFilterExec { )] fn result_evaluate<'a>( &self, - builder: &mut ResultBuilder, + input_length: usize, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { - let input_length = accessor.get_length(self.table.table_ref); // 1. selection let selection_column: Column<'a, Curve25519Scalar> = self.where_clause @@ -60,22 +59,24 @@ impl ProverEvaluate for DishonestFilterExec { }) .collect(); // Compute filtered_columns - let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection); - builder.set_result_table_length(result_len); + let (filtered_columns, _) = filter_columns(alloc, &columns, selection); let filtered_columns = tamper_column(alloc, filtered_columns); - builder.request_post_result_challenges(2); filtered_columns } + fn first_round_evaluate(&self, builder: &mut FirstRoundBuilder) { + builder.request_post_result_challenges(2); + } + #[tracing::instrument( - name = "DishonestFilterExec::prover_evaluate", + name = "DishonestFilterExec::final_round_evaluate", level = "debug", skip_all )] #[allow(unused_variables)] - fn prover_evaluate<'a>( + fn final_round_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, Curve25519Scalar>, + builder: &mut FinalRoundBuilder<'a, Curve25519Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { diff --git a/crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs b/crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs index 35d324516..069aa08d1 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs @@ -16,7 +16,7 @@ use crate::{ }, sql::{ proof::{ - CountBuilder, ProofBuilder, ProofPlan, ProverEvaluate, ResultBuilder, + CountBuilder, FinalRoundBuilder, FirstRoundBuilder, ProofPlan, ProverEvaluate, SumcheckSubpolynomialType, VerificationBuilder, }, proof_exprs::{AliasedDynProofExpr, ColumnExpr, DynProofExpr, ProofExpr, TableExpr}, @@ -208,11 +208,10 @@ impl ProverEvaluate for GroupByExec { #[tracing::instrument(name = "GroupByExec::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a>( &self, - builder: &mut ResultBuilder, + input_length: usize, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { - let input_length = accessor.get_length(self.table.table_ref); // 1. selection let selection_column: Column<'a, C::Scalar> = self.where_clause @@ -246,8 +245,6 @@ impl ProverEvaluate for GroupByExec { } = aggregate_columns(alloc, &group_by_columns, &sum_columns, &[], &[], selection) .expect("columns should be aggregatable"); let sum_result_columns_iter = sum_result_columns.iter().map(|col| Column::Scalar(col)); - builder.set_result_table_length(count_column.len()); - builder.request_post_result_challenges(2); group_by_result_columns .into_iter() .chain(sum_result_columns_iter) @@ -255,11 +252,15 @@ impl ProverEvaluate for GroupByExec { .collect::>() } - #[tracing::instrument(name = "GroupByExec::prover_evaluate", level = "debug", skip_all)] + fn first_round_evaluate(&self, builder: &mut FirstRoundBuilder) { + builder.request_post_result_challenges(2); + } + + #[tracing::instrument(name = "GroupByExec::final_round_evaluate", level = "debug", skip_all)] #[allow(unused_variables)] - fn prover_evaluate<'a>( + fn final_round_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { @@ -365,7 +366,7 @@ fn verify_group_by( reason = "alpha is guaranteed to not be zero in this context" )] pub fn prove_group_by<'a, S: Scalar>( - builder: &mut ProofBuilder<'a, S>, + builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, alpha: S, beta: S, diff --git a/crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs b/crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs index 001e4b9f1..fb66bff00 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs @@ -10,7 +10,7 @@ use crate::{ }, sql::{ proof::{ - CountBuilder, ProofBuilder, ProofPlan, ProverEvaluate, ResultBuilder, + CountBuilder, FinalRoundBuilder, FirstRoundBuilder, ProofPlan, ProverEvaluate, VerificationBuilder, }, proof_exprs::{AliasedDynProofExpr, ProofExpr, TableExpr}, @@ -98,11 +98,10 @@ impl ProverEvaluate for ProjectionExec { #[tracing::instrument(name = "ProjectionExec::result_evaluate", level = "debug", skip_all)] fn result_evaluate<'a>( &self, - builder: &mut ResultBuilder, + input_length: usize, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { - let input_length = accessor.get_length(self.table.table_ref); let columns: Vec<_> = self .aliased_results .iter() @@ -112,16 +111,20 @@ impl ProverEvaluate for ProjectionExec { .result_evaluate(input_length, alloc, accessor) }) .collect(); - // For projection, the result table length is the same as the input table length - builder.set_result_table_length(input_length); columns } - #[tracing::instrument(name = "ProjectionExec::prover_evaluate", level = "debug", skip_all)] + fn first_round_evaluate(&self, _builder: &mut FirstRoundBuilder) {} + + #[tracing::instrument( + name = "ProjectionExec::final_round_evaluate", + level = "debug", + skip_all + )] #[allow(unused_variables)] - fn prover_evaluate<'a>( + fn final_round_evaluate<'a>( &self, - builder: &mut ProofBuilder<'a, C::Scalar>, + builder: &mut FinalRoundBuilder<'a, C::Scalar>, alloc: &'a Bump, accessor: &'a dyn DataAccessor, ) -> Vec> { diff --git a/crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs b/crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs index 53914c11c..3addcfb17 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs @@ -2,7 +2,7 @@ use super::{test_utility::*, DynProofPlan, ProjectionExec}; use crate::{ base::{ database::{ - owned_table_utility::*, ColumnField, ColumnRef, ColumnType, OwnedTable, + owned_table_utility::*, Column, ColumnField, ColumnRef, ColumnType, OwnedTable, OwnedTableTestAccessor, TableRef, TestAccessor, }, map::{IndexMap, IndexSet}, @@ -11,8 +11,8 @@ use crate::{ }, sql::{ proof::{ - exercise_verification, ProofPlan, ProvableQueryResult, ProverEvaluate, ResultBuilder, - VerifiableQueryResult, + exercise_verification, FirstRoundBuilder, ProofPlan, ProvableQueryResult, + ProverEvaluate, VerifiableQueryResult, }, proof_exprs::{test_utility::*, ColumnExpr, DynProofExpr, TableExpr}, }, @@ -165,8 +165,10 @@ fn we_can_get_an_empty_result_from_a_basic_projection_on_an_empty_table_using_re let expr: DynProofPlan = projection(cols_expr_plan(t, &["b", "c", "d", "e"], &accessor), tab(t)); let alloc = Bump::new(); - let mut builder = ResultBuilder::new(); - let result_cols = expr.result_evaluate(&mut builder, &alloc, &accessor); + let result_cols = expr.result_evaluate(0, &alloc, &accessor); + let output_length = result_cols.first().map_or(0, Column::len) as u64; + let mut builder = FirstRoundBuilder::new(); + expr.first_round_evaluate(&mut builder); let fields = &[ ColumnField::new("b".parse().unwrap(), ColumnType::BigInt), ColumnField::new("c".parse().unwrap(), ColumnType::Int128), @@ -177,7 +179,7 @@ fn we_can_get_an_empty_result_from_a_basic_projection_on_an_empty_table_using_re ), ]; let res: OwnedTable = - ProvableQueryResult::new(builder.result_table_length() as u64, &result_cols) + ProvableQueryResult::new(output_length as u64, &result_cols) .to_owned_table(fields) .unwrap(); let expected: OwnedTable = owned_table([ @@ -204,11 +206,13 @@ fn we_can_get_no_columns_from_a_basic_projection_with_no_selected_columns_using_ accessor.add_table(t, data, 0); let expr: DynProofPlan = projection(cols_expr_plan(t, &[], &accessor), tab(t)); let alloc = Bump::new(); - let mut builder = ResultBuilder::new(); - let result_cols = expr.result_evaluate(&mut builder, &alloc, &accessor); + let result_cols = expr.result_evaluate(5, &alloc, &accessor); + let output_length = result_cols.first().map_or(0, Column::len) as u64; + let mut builder = FirstRoundBuilder::new(); + expr.first_round_evaluate(&mut builder); let fields = &[]; let res: OwnedTable = - ProvableQueryResult::new(builder.result_table_length() as u64, &result_cols) + ProvableQueryResult::new(output_length as u64, &result_cols) .to_owned_table(fields) .unwrap(); let expected = OwnedTable::try_new(IndexMap::default()).unwrap(); @@ -240,8 +244,10 @@ fn we_can_get_the_correct_result_from_a_basic_projection_using_result_evaluate() tab(t), ); let alloc = Bump::new(); - let mut builder = ResultBuilder::new(); - let result_cols = expr.result_evaluate(&mut builder, &alloc, &accessor); + let result_cols = expr.result_evaluate(5, &alloc, &accessor); + let output_length = result_cols.first().map_or(0, Column::len) as u64; + let mut builder = FirstRoundBuilder::new(); + expr.first_round_evaluate(&mut builder); let fields = &[ ColumnField::new("b".parse().unwrap(), ColumnType::BigInt), ColumnField::new("prod".parse().unwrap(), ColumnType::Int128), @@ -252,7 +258,7 @@ fn we_can_get_the_correct_result_from_a_basic_projection_using_result_evaluate() ), ]; let res: OwnedTable = - ProvableQueryResult::new(builder.result_table_length() as u64, &result_cols) + ProvableQueryResult::new(output_length as u64, &result_cols) .to_owned_table(fields) .unwrap(); let expected: OwnedTable = owned_table([