Skip to content

Commit

Permalink
fix: update first round builder to accept initial range length
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin-Ray committed Dec 18, 2024
1 parent db8d46c commit cc0d7e9
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 22 deletions.
17 changes: 15 additions & 2 deletions crates/proof-of-sql/src/sql/proof/first_round_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,32 @@ pub struct FirstRoundBuilder {
num_post_result_challenges: usize,
/// The extra one evaluation lengths used in the proof.
one_evaluation_lengths: Vec<usize>,

range_length: usize,
}

impl Default for FirstRoundBuilder {
fn default() -> Self {
Self::new()
Self::new(0)
}
}

impl FirstRoundBuilder {
pub fn new() -> Self {
pub fn new(initial_range_length: usize) -> Self {
Self {
num_post_result_challenges: 0,
one_evaluation_lengths: Vec::new(),
range_length: initial_range_length,
}
}

pub(crate) fn range_length(&self) -> usize {
self.range_length
}

pub(crate) fn update_range_length(&mut self, new_range_length: usize) {
if new_range_length > self.range_length {
self.range_length = new_range_length;
}
}

Expand Down
9 changes: 2 additions & 7 deletions crates/proof-of-sql/src/sql/proof/query_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,13 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
.collect();

// Prover First Round: Evaluate the query && get the right number of post result challenges
let mut first_round_builder = FirstRoundBuilder::new();
let mut first_round_builder = FirstRoundBuilder::new(initial_range_length);
let query_result = expr.first_round_evaluate(&mut first_round_builder, &alloc, &table_map);
let owned_table_result = OwnedTable::from(&query_result);
let provable_result = query_result.into();
let one_evaluation_lengths = first_round_builder.one_evaluation_lengths();

let range_length = one_evaluation_lengths
.iter()
.copied()
.chain(core::iter::once(initial_range_length))
.max()
.expect("Will always have at least one element"); // safe to unwrap because we have at least one element
let range_length = first_round_builder.range_length();

let num_sumcheck_variables = cmp::max(log2_up(range_length), 1);
assert!(num_sumcheck_variables > 0);
Expand Down
9 changes: 8 additions & 1 deletion crates/proof-of-sql/src/sql/proof_gadgets/range_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@
//! * Parallelization: Single-threaded execution of these operations is a performance bottleneck
use crate::{
base::{polynomial::MultilinearExtension, proof::ProofSizeMismatch, scalar::Scalar, slice_ops},
sql::proof::{FinalRoundBuilder, SumcheckSubpolynomialType, VerificationBuilder},
sql::proof::{
FinalRoundBuilder, FirstRoundBuilder, SumcheckSubpolynomialType, VerificationBuilder,
},
};
use alloc::{boxed::Box, vec::Vec};
use bumpalo::Bump;
use bytemuck::cast_slice;
use core::{cmp::max, iter::repeat};

/// Update the max range length for the range check.
pub fn first_round_evaluate_range_check<'a, S: Scalar + 'a>(builder: &mut FirstRoundBuilder) {
builder.update_range_length(256);
}

/// Prove that a word-wise decomposition of a collection of scalars
/// are all within the range 0 to 2^248.
pub fn final_round_evaluate_range_check<'a, S: Scalar + 'a>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl ProverEvaluate for RangeCheckTestPlan {
) -> Table<'a, S> {
builder.request_post_result_challenges(1);
builder.produce_one_evaluation_length(256);
builder.update_range_length(256);
table_map[&self.column.table_ref()].clone()
}

Expand Down
8 changes: 4 additions & 4 deletions crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn we_can_get_an_empty_result_from_a_basic_filter_on_an_empty_table_using_first_
ColumnType::Decimal75(Precision::new(75).unwrap(), 0),
),
];
let first_round_builder = &mut FirstRoundBuilder::new();
let first_round_builder = &mut FirstRoundBuilder::new(0);
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::from(expr.first_round_evaluate(
first_round_builder,
&alloc,
Expand Down Expand Up @@ -241,7 +241,7 @@ fn we_can_get_an_empty_result_from_a_basic_filter_using_first_round_evaluate() {
ColumnType::Decimal75(Precision::new(1).unwrap(), 0),
),
];
let first_round_builder = &mut FirstRoundBuilder::new();
let first_round_builder = &mut FirstRoundBuilder::new(0);
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::from(expr.first_round_evaluate(
first_round_builder,
&alloc,
Expand Down Expand Up @@ -278,7 +278,7 @@ fn we_can_get_no_columns_from_a_basic_filter_with_no_selected_columns_using_firs
let where_clause: DynProofExpr = equal(column(t, "a", &accessor), const_int128(5));
let expr = filter(cols_expr_plan(t, &[], &accessor), tab(t), where_clause);
let fields = &[];
let first_round_builder = &mut FirstRoundBuilder::new();
let first_round_builder = &mut FirstRoundBuilder::new(0);
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::from(expr.first_round_evaluate(
first_round_builder,
&alloc,
Expand Down Expand Up @@ -321,7 +321,7 @@ fn we_can_get_the_correct_result_from_a_basic_filter_using_first_round_evaluate(
ColumnType::Decimal75(Precision::new(1).unwrap(), 0),
),
];
let first_round_builder = &mut FirstRoundBuilder::new();
let first_round_builder = &mut FirstRoundBuilder::new(0);
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::from(expr.first_round_evaluate(
first_round_builder,
&alloc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn we_can_get_an_empty_result_from_a_basic_projection_on_an_empty_table_using_fi
ColumnType::Decimal75(Precision::new(75).unwrap(), 0),
),
];
let first_round_builder = &mut FirstRoundBuilder::new();
let first_round_builder = &mut FirstRoundBuilder::new(0);
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::from(expr.first_round_evaluate(
first_round_builder,
&alloc,
Expand Down Expand Up @@ -211,7 +211,7 @@ 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 fields = &[];
let first_round_builder = &mut FirstRoundBuilder::new();
let first_round_builder = &mut FirstRoundBuilder::new(0);
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::from(expr.first_round_evaluate(
first_round_builder,
&alloc,
Expand Down Expand Up @@ -260,7 +260,7 @@ fn we_can_get_the_correct_result_from_a_basic_projection_using_first_round_evalu
ColumnType::Decimal75(Precision::new(1).unwrap(), 0),
),
];
let first_round_builder = &mut FirstRoundBuilder::new();
let first_round_builder = &mut FirstRoundBuilder::new(0);
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::from(expr.first_round_evaluate(
first_round_builder,
&alloc,
Expand Down
8 changes: 4 additions & 4 deletions crates/proof-of-sql/src/sql/proof_plans/slice_exec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn we_can_get_an_empty_result_from_a_slice_on_an_empty_table_using_first_round_e
ColumnType::Decimal75(Precision::new(75).unwrap(), 0),
),
];
let first_round_builder = &mut FirstRoundBuilder::new();
let first_round_builder = &mut FirstRoundBuilder::new(0);
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::from(expr.first_round_evaluate(
first_round_builder,
&alloc,
Expand Down Expand Up @@ -156,7 +156,7 @@ fn we_can_get_an_empty_result_from_a_slice_using_first_round_evaluate() {
ColumnType::Decimal75(Precision::new(1).unwrap(), 0),
),
];
let first_round_builder = &mut FirstRoundBuilder::new();
let first_round_builder = &mut FirstRoundBuilder::new(0);
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::from(expr.first_round_evaluate(
first_round_builder,
&alloc,
Expand Down Expand Up @@ -197,7 +197,7 @@ fn we_can_get_no_columns_from_a_slice_with_empty_input_using_first_round_evaluat
None,
);
let fields = &[];
let first_round_builder = &mut FirstRoundBuilder::new();
let first_round_builder = &mut FirstRoundBuilder::new(0);
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::from(expr.first_round_evaluate(
first_round_builder,
&alloc,
Expand Down Expand Up @@ -244,7 +244,7 @@ fn we_can_get_the_correct_result_from_a_slice_using_first_round_evaluate() {
ColumnType::Decimal75(Precision::new(1).unwrap(), 0),
),
];
let first_round_builder = &mut FirstRoundBuilder::new();
let first_round_builder = &mut FirstRoundBuilder::new(0);
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::from(expr.first_round_evaluate(
first_round_builder,
&alloc,
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/sql/proof_plans/union_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl ProverEvaluate for UnionExec {
let res = table_union(&inputs, alloc, self.schema.clone()).expect("Failed to union tables");
builder.request_post_result_challenges(2);
builder.produce_one_evaluation_length(res.num_rows());
builder.update_range_length(res.num_rows());
res
}

Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/sql/proof_plans/union_exec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fn we_can_get_result_from_union_using_first_round_evaluate() {
],
fields.clone(),
);
let first_round_builder = &mut FirstRoundBuilder::new();
let first_round_builder = &mut FirstRoundBuilder::new(0);
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::from(ast.first_round_evaluate(
first_round_builder,
&alloc,
Expand Down

0 comments on commit cc0d7e9

Please sign in to comment.