Skip to content

Commit

Permalink
Merge branch 'main' into ethvs-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
ethvs authored Dec 9, 2024
2 parents 2596945 + 5684a33 commit 1ceae5e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 73 deletions.
2 changes: 1 addition & 1 deletion crates/proof-of-sql-parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum ParseError {
error: String,
},
#[snafu(display("Unable to parse resource_id"))]
/// Can not parse the `resource_id`
/// Cannot parse the `resource_id`
ResourceIdParseError {
/// The underlying error
error: String,
Expand Down
18 changes: 0 additions & 18 deletions crates/proof-of-sql/src/base/polynomial/composite_polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ pub struct CompositePolynomial<S: Scalar> {
raw_pointers_lookup_table: IndexMap<*const Vec<S>, usize>,
}

/// Stores the number of variables and max number of multiplicands of the added polynomial used by the prover.
/// This data structures will is used as the verifier key.
#[derive(Clone, Debug)]
pub struct CompositePolynomialInfo {
/// max number of multiplicands in each product
pub max_multiplicands: usize,
/// number of variables of the polynomial
pub num_variables: usize,
}

impl<S: Scalar> CompositePolynomial<S> {
/// Returns an empty polynomial
pub fn new(num_variables: usize) -> Self {
Expand All @@ -60,14 +50,6 @@ impl<S: Scalar> CompositePolynomial<S> {
}
}

/// Extract the max number of multiplicands and number of variables of the list of products.
pub fn info(&self) -> CompositePolynomialInfo {
CompositePolynomialInfo {
max_multiplicands: self.max_multiplicands,
num_variables: self.num_variables,
}
}

/// Add a list of multilinear extensions that is meant to be multiplied together.
/// The resulting polynomial will be multiplied by the scalar `coefficient`.
#[allow(clippy::missing_panics_doc)]
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/base/polynomial/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod composite_polynomial;
pub use composite_polynomial::{CompositePolynomial, CompositePolynomialInfo};
pub use composite_polynomial::CompositePolynomial;
#[cfg(test)]
mod composite_polynomial_test;

Expand Down
33 changes: 12 additions & 21 deletions crates/proof-of-sql/src/proof_primitive/sumcheck/proof.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
base::{
polynomial::{
interpolate_evaluations_to_reverse_coefficients, CompositePolynomial,
CompositePolynomialInfo,
},
polynomial::{interpolate_evaluations_to_reverse_coefficients, CompositePolynomial},
proof::{ProofError, Transcript},
scalar::Scalar,
},
Expand Down Expand Up @@ -64,38 +61,32 @@ impl<S: Scalar> SumcheckProof<S> {
pub fn verify_without_evaluation(
&self,
transcript: &mut impl Transcript,
polynomial_info: CompositePolynomialInfo,
max_multiplicands: usize,
num_variables: usize,
claimed_sum: &S,
) -> Result<Subclaim<S>, ProofError> {
transcript.extend_as_be([
polynomial_info.max_multiplicands as u64,
polynomial_info.num_variables as u64,
]);
transcript.extend_as_be([max_multiplicands as u64, num_variables as u64]);
// This challenge is in order to keep transcript messages grouped. (This simplifies the Solidity implementation.)
transcript.scalar_challenge_as_be::<S>();
if self.coefficients.len()
!= polynomial_info.num_variables * (polynomial_info.max_multiplicands + 1)
{
if self.coefficients.len() != num_variables * (max_multiplicands + 1) {
return Err(ProofError::VerificationError {
error: "invalid proof size",
});
}
let mut evaluation_point = Vec::with_capacity(polynomial_info.num_variables);
let mut evaluation_point = Vec::with_capacity(num_variables);

let mut expected_evaluation = *claimed_sum;
for round_index in 0..polynomial_info.num_variables {
let start_index = round_index * (polynomial_info.max_multiplicands + 1);
for round_index in 0..num_variables {
let start_index = round_index * (max_multiplicands + 1);
transcript.extend_scalars_as_be(
&self.coefficients[start_index..=(start_index + polynomial_info.max_multiplicands)],
&self.coefficients[start_index..=(start_index + max_multiplicands)],
);
let round_evaluation_point = transcript.scalar_challenge_as_be();
evaluation_point.push(round_evaluation_point);
let mut round_evaluation = self.coefficients[start_index];
let mut actual_sum = round_evaluation
+ self.coefficients[start_index + polynomial_info.max_multiplicands];
for coefficient_index in
(start_index + 1)..=(start_index + polynomial_info.max_multiplicands)
{
let mut actual_sum =
round_evaluation + self.coefficients[start_index + max_multiplicands];
for coefficient_index in (start_index + 1)..=(start_index + max_multiplicands) {
round_evaluation *= round_evaluation_point;
round_evaluation += self.coefficients[coefficient_index];
actual_sum += self.coefficients[coefficient_index];
Expand Down
48 changes: 24 additions & 24 deletions crates/proof-of-sql/src/proof_primitive/sumcheck/proof_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::test_cases::sumcheck_test_cases;
use crate::base::{
polynomial::{CompositePolynomial, CompositePolynomialInfo},
polynomial::CompositePolynomial,
proof::Transcript as _,
scalar::{test_scalar::TestScalar, Curve25519Scalar, MontScalar, Scalar},
};
Expand Down Expand Up @@ -36,7 +36,8 @@ fn test_create_verify_proof() {
let subclaim = proof
.verify_without_evaluation(
&mut transcript,
poly.info(),
poly.max_multiplicands,
poly.num_variables,
&Curve25519Scalar::from(579u64),
)
.expect("verify failed");
Expand All @@ -52,7 +53,8 @@ fn test_create_verify_proof() {
let subclaim = proof
.verify_without_evaluation(
&mut transcript,
poly.info(),
poly.max_multiplicands,
poly.num_variables,
&Curve25519Scalar::from(579u64),
)
.expect("verify failed");
Expand All @@ -62,7 +64,8 @@ fn test_create_verify_proof() {
let mut transcript = Transcript::new(b"sumchecktest");
let subclaim = proof.verify_without_evaluation(
&mut transcript,
poly.info(),
poly.max_multiplicands,
poly.num_variables,
&Curve25519Scalar::from(123u64),
);
assert!(subclaim.is_err());
Expand All @@ -71,7 +74,8 @@ fn test_create_verify_proof() {
proof.coefficients[0] += Curve25519Scalar::from(3u64);
let subclaim = proof.verify_without_evaluation(
&mut transcript,
poly.info(),
poly.max_multiplicands,
poly.num_variables,
&Curve25519Scalar::from(579u64),
);
assert!(subclaim.is_err());
Expand Down Expand Up @@ -125,17 +129,21 @@ fn test_polynomial(nv: usize, num_multiplicands_range: (usize, usize), num_produ
let mut rng = <ark_std::rand::rngs::StdRng as ark_std::rand::SeedableRng>::from_seed([0u8; 32]);
let (poly, asserted_sum) =
random_polynomial(nv, num_multiplicands_range, num_products, &mut rng);
let poly_info = poly.info();

// create a proof
let mut transcript = Transcript::new(b"sumchecktest");
let mut evaluation_point = vec![Curve25519Scalar::zero(); poly_info.num_variables];
let mut evaluation_point = vec![Curve25519Scalar::zero(); poly.num_variables];
let proof = SumcheckProof::create(&mut transcript, &mut evaluation_point, &poly);

// verify proof
let mut transcript = Transcript::new(b"sumchecktest");
let subclaim = proof
.verify_without_evaluation(&mut transcript, poly_info, &asserted_sum)
.verify_without_evaluation(
&mut transcript,
poly.max_multiplicands,
poly.num_variables,
&asserted_sum,
)
.expect("verify failed");
assert_eq!(subclaim.evaluation_point, evaluation_point);
assert_eq!(
Expand Down Expand Up @@ -179,10 +187,8 @@ fn we_can_verify_many_random_test_cases() {
let subclaim = proof
.verify_without_evaluation(
&mut transcript,
CompositePolynomialInfo {
max_multiplicands: test_case.max_multiplicands,
num_variables: test_case.num_vars,
},
test_case.max_multiplicands,
test_case.num_vars,
&test_case.sum,
)
.expect("verification should succeed with the correct setup");
Expand All @@ -200,10 +206,8 @@ fn we_can_verify_many_random_test_cases() {
transcript.extend_serialize_as_le(&123u64);
let verify_result = proof.verify_without_evaluation(
&mut transcript,
CompositePolynomialInfo {
max_multiplicands: test_case.max_multiplicands,
num_variables: test_case.num_vars,
},
test_case.max_multiplicands,
test_case.num_vars,
&test_case.sum,
);
if let Ok(subclaim) = verify_result {
Expand All @@ -218,10 +222,8 @@ fn we_can_verify_many_random_test_cases() {
proof
.verify_without_evaluation(
&mut transcript,
CompositePolynomialInfo {
max_multiplicands: test_case.max_multiplicands,
num_variables: test_case.num_vars,
},
test_case.max_multiplicands,
test_case.num_vars,
&(test_case.sum + TestScalar::ONE),
)
.is_err(),
Expand All @@ -235,10 +237,8 @@ fn we_can_verify_many_random_test_cases() {
modified_proof
.verify_without_evaluation(
&mut transcript,
CompositePolynomialInfo {
max_multiplicands: test_case.max_multiplicands,
num_variables: test_case.num_vars,
},
test_case.max_multiplicands,
test_case.num_vars,
&test_case.sum,
)
.is_err(),
Expand Down
13 changes: 5 additions & 8 deletions crates/proof-of-sql/src/sql/proof/query_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
map::{IndexMap, IndexSet},
math::log2_up,
polynomial::{compute_evaluation_vector, CompositePolynomialInfo},
polynomial::compute_evaluation_vector,
proof::{Keccak256Transcript, ProofError, Transcript},
},
proof_primitive::sumcheck::SumcheckProof,
Expand Down Expand Up @@ -272,15 +272,12 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
SumcheckRandomScalars::new(&random_scalars, self.range_length, num_sumcheck_variables);

// verify sumcheck up to the evaluation check
let poly_info = CompositePolynomialInfo {
// This needs to be at least 2 since `CompositePolynomialBuilder::make_composite_polynomial`
// always adds a degree 2 term.
max_multiplicands: core::cmp::max(counts.sumcheck_max_multiplicands, 2),
num_variables: num_sumcheck_variables,
};
let subclaim = self.sumcheck_proof.verify_without_evaluation(
&mut transcript,
poly_info,
// This needs to be at least 2 since `CompositePolynomialBuilder::make_composite_polynomial`
// always adds a degree 2 term.
core::cmp::max(counts.sumcheck_max_multiplicands, 2),
num_sumcheck_variables,
&Zero::zero(),
)?;

Expand Down

0 comments on commit 1ceae5e

Please sign in to comment.