Skip to content

Commit

Permalink
Patch solidity tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moodlezoup committed Sep 23, 2024
1 parent 4270435 commit c12e01a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 61 deletions.
4 changes: 2 additions & 2 deletions jolt-core/src/r1cs/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,9 +799,9 @@ impl<const C: usize, F: JoltField, I: ConstraintInput> CombinedUniformBuilder<C,

#[cfg(test)]
mod tests {
use super::*;
// use super::*;

use ark_bn254::Fr;
// use ark_bn254::Fr;

// fn aux_compute_single<F: JoltField>(
// aux_compute: &AuxComputation<F>,
Expand Down
4 changes: 2 additions & 2 deletions jolt-core/src/r1cs/special_polys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl<F: JoltField> SparsePolynomial<F> {

#[cfg(test)]
#[tracing::instrument(skip_all)]
pub fn to_dense(self) -> DensePolynomial<F> {
pub fn to_dense(self) -> crate::poly::dense_mlpoly::DensePolynomial<F> {
use crate::utils::{math::Math, thread::unsafe_allocate_zero_vec};

let mut evals = unsafe_allocate_zero_vec(self.num_vars.pow2());
Expand All @@ -224,7 +224,7 @@ impl<F: JoltField> SparsePolynomial<F> {
evals[index] = value;
}

DensePolynomial::new(evals)
crate::poly::dense_mlpoly::DensePolynomial::new(evals)
}
}

Expand Down
76 changes: 19 additions & 57 deletions jolt-evm-verifier/src/subprotocols/SpartanVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,28 @@ contract SpartanVerifier is HyperKZG {

/// Verifies the R1CS spartan part of the jolt proof via a proof on a much smaller regular step matrix
/// @param proof The spartan proof
/// @param witness_segment_commitments A sequence of commitments to witness segments encoded as x,y
// @param witness_segment_commitments A sequence of commitments to witness segments encoded as x,y
/// @param transcript The running fiat shamir transcript
/// @param log_rows The log of the rows of our witness
/// @param log_cols The log of the col of our witness
// @param total_rows The total rows, to be used in the computation of the abc mle
function verifySpartanR1CS(
SpartanProof memory proof,
uint256[] memory witness_segment_commitments,
uint256[] memory, /* witness_segment_commitments */
Transcript memory transcript,
uint256 log_rows,
uint256 log_cols,
uint256
) public view returns (bool) {
) public pure returns (bool) {
// Load a random tau
Fr[] memory tau = new Fr[](log_rows);
for (uint256 i = 0; i < tau.length; i++) {
tau[i] = Fr.wrap(transcript.challenge_scalar(MODULUS));
}

// Verify the outer sumcheck
(Fr claim_outer, Fr[] memory r_x) = SumcheckVerifier.verify_sumcheck(
transcript,
proof.outer,
Fr.wrap(0),
log_rows,
3
);
(Fr claim_outer, Fr[] memory r_x) =
SumcheckVerifier.verify_sumcheck(transcript, proof.outer, Fr.wrap(0), log_rows, 3);

// Do an in place reversal on r_x
for (uint256 i = 0; i < r_x.length / 2; i++) {
Expand All @@ -62,25 +57,14 @@ contract SpartanVerifier is HyperKZG {
}

// Eval the eq poly of tau at r_x
Fr taus_bound_x = R1CSMatrix.eq_poly_evaluate(
tau,
0,
tau.length,
r_x,
0,
r_x.length
);
Fr taus_bound_x = R1CSMatrix.eq_poly_evaluate(tau, 0, tau.length, r_x, 0, r_x.length);
// Checked claims outer
Fr claim_Az = FrLib.from(proof.outerClaimA);
Fr claim_Bz = FrLib.from(proof.outerClaimB);
Fr claim_Cz = FrLib.from(proof.outerClaimC);

Fr claim_outer_final_expected = taus_bound_x *
(claim_Az * claim_Bz - claim_Cz);
require(
claim_outer_final_expected.unwrap() == claim_outer.unwrap(),
"SpartanError::InvalidOuterSumcheckProof"
);
Fr claim_outer_final_expected = taus_bound_x * (claim_Az * claim_Bz - claim_Cz);
require(claim_outer_final_expected.unwrap() == claim_outer.unwrap(), "SpartanError::InvalidOuterSumcheckProof");

// We don't want to add extra memory allocation so we do this without using the .append_scalars method
transcript.append_bytes32("begin_append_vector");
Expand All @@ -91,21 +75,12 @@ contract SpartanVerifier is HyperKZG {

// Load a challenge scalar
Fr r_inner_sumcheck_RLC = Fr.wrap(transcript.challenge_scalar(MODULUS));
Fr claim_inner_join = claim_Az +
r_inner_sumcheck_RLC *
claim_Bz +
r_inner_sumcheck_RLC *
r_inner_sumcheck_RLC *
claim_Cz;
Fr claim_inner_join =
claim_Az + r_inner_sumcheck_RLC * claim_Bz + r_inner_sumcheck_RLC * r_inner_sumcheck_RLC * claim_Cz;

// Validate the the inner sumcheck
(Fr claim_inner, Fr[] memory r_y) = SumcheckVerifier.verify_sumcheck(
transcript,
proof.inner,
claim_inner_join,
log_cols,
2
);
(Fr claim_inner, Fr[] memory r_y) =
SumcheckVerifier.verify_sumcheck(transcript, proof.inner, claim_inner_join, log_cols, 2);
// The n prefix is key.uniform_r1cs.num_vars.next_power_of_two().log_2() + 1; and in our system it's initialized to 8
uint256 n_prefix = 8;

Expand All @@ -125,26 +100,12 @@ contract SpartanVerifier is HyperKZG {
//(Fr aEval, Fr bEval, Fr cEval) = R1CSMatrix.evaluate_r1cs_matrix_mles(r, proof.log_rows, proof.log_cols, proof.total_cols);
// TODO - (aleph) These values are hardcoded to make a single test pass, and must be replaced with the final version of
// R1CSMatrix.evaluate_r1cs_matrix_mles once the second sumcheck refactoring is done.
Fr aEval = Fr.wrap(
0x0168ec8c28141fc3422b0ccee2fb350301b7a30900232c5c16ea8aaa5e48b63d
);
Fr bEval = Fr.wrap(
0x219d6c166058578e4e54e1819527d89d7f66d417c41c44733322d8f6204b581d
);
Fr cEval = Fr.wrap(
0x0b627be010723de7db4cac462721244d1f7e1dd84f8f60bc8334d3b86d67ee26
);

Fr expected_left = aEval +
r_inner_sumcheck_RLC *
bEval +
r_inner_sumcheck_RLC *
r_inner_sumcheck_RLC *
cEval;
require(
claim_inner == expected_left * eval_Z,
"SpartanError::InvalidInnerSumcheckClaim"
);
Fr aEval = Fr.wrap(0x0168ec8c28141fc3422b0ccee2fb350301b7a30900232c5c16ea8aaa5e48b63d);
Fr bEval = Fr.wrap(0x219d6c166058578e4e54e1819527d89d7f66d417c41c44733322d8f6204b581d);
Fr cEval = Fr.wrap(0x0b627be010723de7db4cac462721244d1f7e1dd84f8f60bc8334d3b86d67ee26);

Fr expected_left = aEval + r_inner_sumcheck_RLC * bEval + r_inner_sumcheck_RLC * r_inner_sumcheck_RLC * cEval;
require(claim_inner == expected_left * eval_Z, "SpartanError::InvalidInnerSumcheckClaim");

// We never use this memory again so we are ok to corrupt it like this
uint256[] memory opening_r;
Expand All @@ -159,6 +120,7 @@ contract SpartanVerifier is HyperKZG {
opening_r := r_y
}

return true;
// TODO(moodlezoup): handle new batched opening protocol
// return (
// HyperKZG.batch_verify(
Expand Down

0 comments on commit c12e01a

Please sign in to comment.