Skip to content

Commit

Permalink
test: remove later
Browse files Browse the repository at this point in the history
  • Loading branch information
iajoiner committed Dec 3, 2024
1 parent 2a99ea1 commit 36eda67
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 32 deletions.
33 changes: 28 additions & 5 deletions crates/proof-of-sql/src/base/polynomial/multilinear_extension.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::base::{database::Column, if_rayon, scalar::Scalar, slice_ops};
use alloc::vec::Vec;
use core::ffi::c_void;
use core::{ffi::c_void, fmt::Debug};
use num_traits::Zero;
#[cfg(feature = "rayon")]
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

/// Interface for operating on multilinear extension's in-place
pub trait MultilinearExtension<S: Scalar> {
pub trait MultilinearExtension<S: Scalar>: Debug {
/// Given an evaluation vector, compute the evaluation of the multilinear
/// extension
fn inner_product(&self, evaluation_vec: &[S]) -> S;
Expand All @@ -20,6 +20,9 @@ pub trait MultilinearExtension<S: Scalar> {
/// pointer to identify the slice forming the MLE
fn id(&self) -> *const c_void;

#[cfg(test)]
fn to_scalar_vec(&self) -> Vec<S>;

#[cfg(test)]
/// Given an evaluation point, compute the evaluation of the multilinear
/// extension. This is inefficient and should only be used for testing.
Expand All @@ -30,7 +33,7 @@ pub trait MultilinearExtension<S: Scalar> {
}
}

impl<'a, T: Sync, S: Scalar> MultilinearExtension<S> for &'a [T]
impl<'a, T: Sync + Debug, S: Scalar> MultilinearExtension<S> for &'a [T]
where
&'a T: Into<S>,
{
Expand Down Expand Up @@ -58,6 +61,11 @@ where
fn id(&self) -> *const c_void {
self.as_ptr().cast::<c_void>()
}

#[cfg(test)]
fn to_scalar_vec(&self) -> Vec<S> {
slice_ops::slice_cast(self)
}
}

/// TODO: add docs
Expand All @@ -78,17 +86,22 @@ macro_rules! slice_like_mle_impl {
fn id(&self) -> *const c_void {
(&self[..]).id()
}

#[cfg(test)]
fn to_scalar_vec(&self) -> Vec<S> {
(&self[..]).to_scalar_vec()
}
};
}

impl<'a, T: Sync, S: Scalar> MultilinearExtension<S> for &'a Vec<T>
impl<'a, T: Sync + Debug, S: Scalar> MultilinearExtension<S> for &'a Vec<T>
where
&'a T: Into<S>,
{
slice_like_mle_impl!();
}

impl<'a, T: Sync, const N: usize, S: Scalar> MultilinearExtension<S> for &'a [T; N]
impl<'a, T: Sync + Debug, const N: usize, S: Scalar> MultilinearExtension<S> for &'a [T; N]
where
&'a T: Into<S>,
{
Expand Down Expand Up @@ -151,6 +164,11 @@ impl<S: Scalar> MultilinearExtension<S> for &Column<'_, S> {
Column::Int128(c) => MultilinearExtension::<S>::id(c),
}
}

#[cfg(test)]
fn to_scalar_vec(&self) -> Vec<S> {
self.to_scalar_with_scaling(0)
}
}

impl<S: Scalar> MultilinearExtension<S> for Column<'_, S> {
Expand All @@ -169,4 +187,9 @@ impl<S: Scalar> MultilinearExtension<S> for Column<'_, S> {
fn id(&self) -> *const c_void {
(&self).id()
}

#[cfg(test)]
fn to_scalar_vec(&self) -> Vec<S> {
(&self).to_scalar_vec()
}
}
6 changes: 6 additions & 0 deletions crates/proof-of-sql/src/proof_primitive/sumcheck/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl<S: Scalar> SumcheckProof<S> {
polynomial_info: CompositePolynomialInfo,
claimed_sum: &S,
) -> Result<Subclaim<S>, ProofError> {
dbg!(&polynomial_info);
transcript.extend_as_be([
polynomial_info.max_multiplicands as u64,
polynomial_info.num_variables as u64,
Expand Down Expand Up @@ -95,6 +96,11 @@ impl<S: Scalar> SumcheckProof<S> {
actual_sum += self.coefficients[coefficient_index];
}
if actual_sum != expected_evaluation {
dbg!(round_index);
dbg!(actual_sum, expected_evaluation);
dbg!(&self.coefficients);
dbg!("Verifier error");
dbg!(&evaluation_point);
return Err(ProofError::VerificationError {
error: "round evaluation does not match claimed sum",
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ impl<S: Scalar> ProverState<S> {
flattened_ml_extensions: Vec<Vec<S>>,
num_vars: usize,
) -> Self {
dbg!(&list_of_products);
dbg!(&flattened_ml_extensions);
dbg!(num_vars);
let max_multiplicands = list_of_products
.iter()
.map(|(_, product)| product.len())
Expand Down
5 changes: 4 additions & 1 deletion crates/proof-of-sql/src/sql/proof/query_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
let mut evaluation_point = vec![Zero::zero(); num_sumcheck_variables];
let sumcheck_proof =
SumcheckProof::create(&mut transcript, &mut evaluation_point, sumcheck_state);

dbg!("Prover");
dbg!(&evaluation_point);
// evaluate the MLEs used in sumcheck except for the result columns
let mut evaluation_vec = vec![Zero::zero(); range_length];
compute_evaluation_vector(&mut evaluation_vec, &evaluation_point);
Expand Down Expand Up @@ -306,6 +307,8 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
.chain(self.one_evaluation_lengths.iter())
.copied();

dbg!("Verifier");
dbg!(&subclaim.evaluation_point);
// pass over the provable AST to fill in the verification builder
let sumcheck_evaluations = SumcheckMleEvaluations::new(
self.range_length,
Expand Down
60 changes: 59 additions & 1 deletion crates/proof-of-sql/src/sql/proof/sumcheck_subpolynomial.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::base::{polynomial::MultilinearExtension, scalar::Scalar};
use alloc::{boxed::Box, vec::Vec};
use core::fmt::Debug;

/// The type of a sumcheck subpolynomial
#[derive(Copy, Clone, Hash, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum SumcheckSubpolynomialType {
/// The subpolynomial should be zero at every entry/row
Identity,
Expand All @@ -22,6 +23,7 @@ pub type SumcheckSubpolynomialTerm<'a, S> = (S, Vec<Box<dyn MultilinearExtension
///
/// The subpolynomial is represented as a sum of terms, where each term is a
/// product of multilinear extensions and a constant.
#[derive(Debug)]
pub struct SumcheckSubpolynomial<'a, S: Scalar> {
terms: Vec<SumcheckSubpolynomialTerm<'a, S>>,
subpolynomial_type: SumcheckSubpolynomialType,
Expand All @@ -33,6 +35,62 @@ impl<'a, S: Scalar> SumcheckSubpolynomial<'a, S> {
subpolynomial_type: SumcheckSubpolynomialType,
terms: Vec<SumcheckSubpolynomialTerm<'a, S>>,
) -> Self {
dbg!(&subpolynomial_type);
dbg!(terms.len());
#[cfg(test)]
let concrete_terms = terms
.iter()
.map(|(mul, mles)| {
(
mul,
mles.iter()
.map(|mle| mle.to_scalar_vec())
.collect::<Vec<_>>(),
)
})
.collect::<Vec<_>>();
#[cfg(test)]
let len = concrete_terms
.iter()
.flat_map(|(_, mles)| mles.iter().map(|mle| mle.len()))
.max()
.unwrap_or(0);
#[cfg(test)]
match subpolynomial_type {
SumcheckSubpolynomialType::Identity => {
(0..len).for_each(|i| {
let sum = concrete_terms
.iter()
.map(|(mul, mles)| {
**mul
* mles
.iter()
.map(|mle| *mle.get(i).unwrap_or(&S::zero()))
.product::<S>()
})
.sum::<S>();
assert_eq!(sum, S::zero());
});
}
SumcheckSubpolynomialType::ZeroSum => {
dbg!(&terms);
let sum = (0..len)
.map(|i| {
concrete_terms
.iter()
.map(|(mul, mles)| {
**mul
* mles
.iter()
.map(|mle| *mle.get(i).unwrap_or(&S::zero()))
.product::<S>()
})
.sum::<S>()
})
.sum::<S>();
assert_eq!(sum, S::zero());
}
}
Self {
terms,
subpolynomial_type,
Expand Down
61 changes: 61 additions & 0 deletions crates/proof-of-sql/src/sql/proof/sumcheck_subpolynomial.rs.rej
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
diff a/crates/proof-of-sql/src/sql/proof/sumcheck_subpolynomial.rs b/crates/proof-of-sql/src/sql/proof/sumcheck_subpolynomial.rs (rejected hunks)
@@ -33,6 +33,59 @@ impl<'a, S: Scalar> SumcheckSubpolynomial<'a, S> {
subpolynomial_type: SumcheckSubpolynomialType,
terms: Vec<SumcheckSubpolynomialTerm<'a, S>>,
) -> Self {
+ #[cfg(test)]
+ let concrete_terms = terms
+ .iter()
+ .map(|(mul, mles)| {
+ (
+ mul,
+ mles.iter()
+ .map(|mle| mle.to_scalar_vec())
+ .collect::<Vec<_>>(),
+ )
+ })
+ .collect::<Vec<_>>();
+ #[cfg(test)]
+ let len = concrete_terms
+ .iter()
+ .flat_map(|(_, mles)| mles.iter().map(|mle| mle.len()))
+ .max()
+ .unwrap_or(0);
+ #[cfg(test)]
+ match subpolynomial_type {
+ SumcheckSubpolynomialType::Identity => {
+ (0..len).for_each(|i| {
+ let sum = concrete_terms
+ .iter()
+ .map(|(mul, mles)| {
+ **mul
+ * mles
+ .iter()
+ .map(|mle| *mle.get(i).unwrap_or(&S::zero()))
+ .product::<S>()
+ })
+ .sum::<S>();
+ assert_eq!(sum, S::zero());
+ });
+ }
+ SumcheckSubpolynomialType::ZeroSum => {
+ let sum = (0..len)
+ .map(|i| {
+ concrete_terms
+ .iter()
+ .map(|(mul, mles)| {
+ **mul
+ * mles
+ .iter()
+ .map(|mle| *mle.get(i).unwrap_or(&S::zero()))
+ .product::<S>()
+ })
+ .sum::<S>()
+ })
+ .sum::<S>();
+ assert_eq!(sum, S::zero());
+ }
+ }
Self {
terms,
subpolynomial_type,
11 changes: 9 additions & 2 deletions crates/proof-of-sql/src/sql/proof_plans/slice_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ impl ProverEvaluate for SliceExec {
) -> (Table<'a, S>, Vec<usize>) {
// 1. columns
let (input, input_one_eval_lengths) = self.input.result_evaluate(alloc, table_map);
dbg!(&input_one_eval_lengths);
dbg!(&input);
let columns = input.columns().copied().collect::<Vec<_>>();
// 2. select
let select = get_slice_select(input.num_rows(), self.skip, self.fetch);
Expand All @@ -140,6 +142,8 @@ impl ProverEvaluate for SliceExec {
.expect("Failed to create table from iterator");
let mut one_eval_lengths = input_one_eval_lengths;
one_eval_lengths.push(output_length);
dbg!(&res);
dbg!(&one_eval_lengths);
(res, one_eval_lengths)
}

Expand All @@ -158,6 +162,7 @@ impl ProverEvaluate for SliceExec {
) -> Table<'a, S> {
// 1. columns
let input = self.input.final_round_evaluate(builder, alloc, table_map);
dbg!(&input);
let columns = input.columns().copied().collect::<Vec<_>>();
// 2. select
let select = get_slice_select(input.num_rows(), self.skip, self.fetch);
Expand Down Expand Up @@ -185,13 +190,15 @@ impl ProverEvaluate for SliceExec {
input.num_rows(),
result_len,
);
Table::<'a, S>::try_from_iter_with_options(
let res = Table::<'a, S>::try_from_iter_with_options(
self.get_column_result_fields()
.into_iter()
.map(|expr| expr.name())
.zip(filtered_columns),
TableOptions::new(Some(output_length)),
)
.expect("Failed to create table from iterator")
.expect("Failed to create table from iterator");
dbg!(&res);
res
}
}
Loading

0 comments on commit 36eda67

Please sign in to comment.