Skip to content

Commit

Permalink
More debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
moodlezoup committed Sep 18, 2024
1 parent a2f07ce commit 40c6c72
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 48 deletions.
6 changes: 6 additions & 0 deletions jolt-core/src/field/binius.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,9 @@ impl<F: BiniusSpecific> ark_serialize::Valid for BiniusField<F> {
todo!()
}
}

impl<F: BiniusSpecific> std::fmt::Display for BiniusField<F> {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}
3 changes: 2 additions & 1 deletion jolt-core/src/field/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::Debug;
use std::fmt::{Debug, Display};
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};

use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
Expand Down Expand Up @@ -31,6 +31,7 @@ pub trait JoltField:
+ Copy
+ Sync
+ Send
+ Display
+ Debug
+ Default
+ CanonicalSerialize
Expand Down
1 change: 1 addition & 0 deletions jolt-core/src/jolt/vm/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct BytecodeStuff<T: CanonicalSerialize + CanonicalDeserialize> {
a_init_final: VerifierComputedOpening<T>,
v_init_final: VerifierComputedOpening<[T; 6]>,
}

pub type BytecodePolynomials<F: JoltField> = BytecodeStuff<DensePolynomial<F>>;
pub type BytecodeOpenings<F: JoltField> = BytecodeStuff<F>;
pub type BytecodeCommitments<PCS: CommitmentScheme> = BytecodeStuff<PCS::Commitment>;
Expand Down
12 changes: 2 additions & 10 deletions jolt-core/src/jolt/vm/instruction_lookups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{

use super::{JoltCommitments, JoltPolynomials, JoltTraceStep};

#[derive(Default, CanonicalSerialize, CanonicalDeserialize)]
#[derive(Debug, Default, CanonicalSerialize, CanonicalDeserialize)]
pub struct InstructionLookupStuff<T: CanonicalSerialize + CanonicalDeserialize> {
pub(crate) dim: Vec<T>,
read_cts: Vec<T>,
Expand Down Expand Up @@ -604,19 +604,10 @@ where
transcript,
);

// let sumcheck_opening_proof = PrimarySumcheckOpenings::prove_openings(
// generators,
// witness,
// &r_primary_sumcheck,
// &sumcheck_openings,
// transcript,
// );

let primary_sumcheck = PrimarySumcheck {
sumcheck_proof: primary_sumcheck_proof,
num_rounds,
openings: sumcheck_openings,
// opening_proof: sumcheck_opening_proof,
};

let memory_checking = Self::prove_memory_checking(
Expand Down Expand Up @@ -1072,6 +1063,7 @@ where
preprocessing: &InstructionLookupsPreprocessing<C, F>,
instruction_flags: &[F],
) -> Vec<F> {
debug_assert_eq!(instruction_flags.len(), Self::NUM_INSTRUCTIONS);
let mut memory_flags = vec![F::zero(); preprocessing.num_memories];
for instruction_index in 0..Self::NUM_INSTRUCTIONS {
for memory_index in &preprocessing.instruction_to_memory_indices[instruction_index] {
Expand Down
1 change: 1 addition & 0 deletions jolt-core/src/jolt/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ pub trait Jolt<F: JoltField, PCS: CommitmentScheme<Field = F>, const C: usize, c
Self::Constraints::construct_constraints(padded_trace_length, memory_start);
let spartan_key =
spartan::UniformSpartanProof::setup_precommitted(&r1cs_builder, padded_trace_length);
transcript.append_scalar(&spartan_key.vk_digest);

let r1cs_proof = R1CSProof {
key: spartan_key,
Expand Down
7 changes: 6 additions & 1 deletion jolt-core/src/jolt/vm/read_write_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,12 @@ where
"Output sumcheck check failed."
);

opening_accumulator.append(&[&commitment.v_final], r_sumcheck, &[&proof.opening]);
opening_accumulator.append(
&[&commitment.v_final],
r_sumcheck,
&[&proof.opening],
transcript,
);

Ok(())
}
Expand Down
16 changes: 9 additions & 7 deletions jolt-core/src/lasso/memory_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,26 +235,26 @@ where
}

fn compute_openings(
_preprocessing: &Self::Preprocessing,
preprocessing: &Self::Preprocessing,
opening_accumulator: &mut ProverOpeningAccumulator<F>,
polynomials: &Self::Polynomials,
jolt_polynomials: &JoltPolynomials<F>,
r_read_write: &[F],
r_init_final: &[F],
transcript: &mut ProofTranscript,
) -> (Self::Openings, Self::ExogenousOpenings) {
let mut openings = Self::Openings::default();
let mut openings = Self::Openings::initialize(preprocessing);
let mut exogenous_openings = Self::ExogenousOpenings::default();

let eq_read_write = EqPolynomial::evals(r_read_write);
polynomials
.read_write_values()
.par_iter()
.zip(openings.read_write_values_mut().into_par_iter())
.zip_eq(openings.read_write_values_mut().into_par_iter())
.chain(
Self::ExogenousOpenings::exogenous_data(jolt_polynomials)
.par_iter()
.zip(exogenous_openings.openings_mut().into_par_iter()),
.zip_eq(exogenous_openings.openings_mut().into_par_iter()),
)
.for_each(|(poly, opening)| {
let claim = poly.evaluate_at_chi(&eq_read_write);
Expand All @@ -280,7 +280,7 @@ where
polynomials
.init_final_values()
.par_iter()
.zip(openings.init_final_values_mut().into_par_iter())
.zip_eq(openings.init_final_values_mut().into_par_iter())
.for_each(|(poly, opening)| {
let claim = poly.evaluate_at_chi(&eq_init_final);
*opening = claim;
Expand Down Expand Up @@ -426,7 +426,7 @@ where
pcs_setup: &PCS::Setup,
mut proof: MemoryCheckingProof<F, PCS, Self::Openings, Self::ExogenousOpenings>,
commitments: &Self::Commitments,
exogenous_commitments: &JoltCommitments<PCS>,
jolt_commitments: &JoltCommitments<PCS>,
opening_accumulator: &mut VerifierOpeningAccumulator<F, PCS>,
transcript: &mut ProofTranscript,
) -> Result<(), ProofVerifyError> {
Expand Down Expand Up @@ -457,7 +457,7 @@ where

let read_write_commits: Vec<_> = [
commitments.read_write_values(),
exogenous_commitments.read_write_values(),
Self::ExogenousOpenings::exogenous_data(jolt_commitments),
]
.concat();
let read_write_claims: Vec<_> = [
Expand All @@ -469,12 +469,14 @@ where
&read_write_commits,
r_read_write.to_vec(),
&read_write_claims,
transcript,
);

opening_accumulator.append(
&commitments.init_final_values(),
r_read_write.to_vec(),
&proof.openings.init_final_values(),
transcript,
);

// proof.read_write_openings.verify_openings(
Expand Down
9 changes: 7 additions & 2 deletions jolt-core/src/poly/opening_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ impl<F: JoltField> ProverOpeningAccumulator<F> {
claims: &[&F],
transcript: &mut ProofTranscript,

Check failure on line 86 in jolt-core/src/poly/opening_proof.rs

View workflow job for this annotation

GitHub Actions / Onchain Verifier Tests

unused variable: `transcript`

Check failure on line 86 in jolt-core/src/poly/opening_proof.rs

View workflow job for this annotation

GitHub Actions / test

unused variable: `transcript`

Check failure on line 86 in jolt-core/src/poly/opening_proof.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `transcript`
) {
assert_eq!(polynomials.len(), claims.len());
// Generate batching challenge \rho and powers 1,...,\rho^{m-1}
let rho: F = transcript.challenge_scalar();
// let rho: F = transcript.challenge_scalar();
let rho: F = F::one(); // TODO(moodlezoup)

let mut rho_powers = vec![F::one()];
for i in 1..polynomials.len() {
rho_powers.push(rho_powers[i - 1] * rho);
Expand Down Expand Up @@ -355,8 +358,10 @@ impl<F: JoltField, PCS: CommitmentScheme<Field = F>> VerifierOpeningAccumulator<
commitments: &[&PCS::Commitment],
opening_point: Vec<F>,

Check failure on line 359 in jolt-core/src/poly/opening_proof.rs

View workflow job for this annotation

GitHub Actions / Onchain Verifier Tests

unused variable: `opening_point`

Check failure on line 359 in jolt-core/src/poly/opening_proof.rs

View workflow job for this annotation

GitHub Actions / test

unused variable: `opening_point`

Check failure on line 359 in jolt-core/src/poly/opening_proof.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `opening_point`
claims: &[&F],
transcript: &mut ProofTranscript,

Check failure on line 361 in jolt-core/src/poly/opening_proof.rs

View workflow job for this annotation

GitHub Actions / Onchain Verifier Tests

unused variable: `transcript`

Check failure on line 361 in jolt-core/src/poly/opening_proof.rs

View workflow job for this annotation

GitHub Actions / test

unused variable: `transcript`

Check failure on line 361 in jolt-core/src/poly/opening_proof.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `transcript`
) {
todo!("Compute RLC commitment/claim");
assert_eq!(commitments.len(), claims.len());
// todo!("Compute RLC commitment/claim");
// self.openings
// .push(VerifierOpening::new(commitment, opening_point, claim));
}
Expand Down
58 changes: 48 additions & 10 deletions jolt-core/src/r1cs/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,48 @@ struct Constraint {
}

impl Constraint {
fn pretty_fmt<const C: usize, I: ConstraintInput>(&self, f: &mut String) -> std::fmt::Result {
#[cfg(test)]
fn pretty_fmt<const C: usize, I: ConstraintInput, F: JoltField>(
&self,
f: &mut String,
flattened_polynomials: &[&DensePolynomial<F>],
step_index: usize,
) -> std::fmt::Result {
self.a.pretty_fmt::<C, I>(f)?;
write!(f, " ⋅ ")?;
self.b.pretty_fmt::<C, I>(f)?;
write!(f, " == ")?;
self.c.pretty_fmt::<C, I>(f)
self.c.pretty_fmt::<C, I>(f)?;
writeln!(f, "")?;

let mut terms = Vec::new();
for term in self
.a
.terms()
.iter()
.chain(self.b.terms().iter())
.chain(self.c.terms().iter())
{
if !terms.contains(term) {
terms.push(*term);
}
}

for term in terms {
match term.0 {
Variable::Input(var_index) | Variable::Auxiliary(var_index) => {
writeln!(
f,
" {:?} = {}",
I::from_index::<C>(var_index),
flattened_polynomials[var_index][step_index]
)?;
}
Variable::Constant => {}
}
}

Ok(())
}

#[cfg(test)]
Expand Down Expand Up @@ -779,14 +815,15 @@ impl<const C: usize, F: JoltField, I: ConstraintInput> CombinedUniformBuilder<C,
let cz_poly = SparsePolynomial::new(num_vars, cz_sparse);

#[cfg(test)]
self.assert_valid(&az_poly, &bz_poly, &cz_poly);
self.assert_valid(flattened_polynomials, &az_poly, &bz_poly, &cz_poly);

(az_poly, bz_poly, cz_poly)
}

#[cfg(test)]
pub fn assert_valid(
&self,
flattened_polynomials: &[&DensePolynomial<F>],
az: &SparsePolynomial<F>,
bz: &SparsePolynomial<F>,
cz: &SparsePolynomial<F>,
Expand All @@ -805,19 +842,20 @@ impl<const C: usize, F: JoltField, I: ConstraintInput> CombinedUniformBuilder<C,
let step_index = constraint_index % self.uniform_repeat;
if uniform_constraint_index >= self.uniform_builder.constraints.len() {
panic!(
"Mismatch at non-uniform constraint: {}\n\
step: {step_index}",
"Non-uniform constraint {} violated at step {step_index}",
uniform_constraint_index - self.uniform_builder.constraints.len()
)
} else {
let mut constraint_string = String::new();
let _ = self.uniform_builder.constraints[uniform_constraint_index]
.pretty_fmt::<C, I>(&mut constraint_string);
.pretty_fmt::<C, I, F>(
&mut constraint_string,
flattened_polynomials,
step_index,
);
println!("{constraint_string}");
panic!(
"Mismatch at global constraint {constraint_index} => {:?}\n\
uniform constraint: {uniform_constraint_index}\n\
step: {step_index}",
constraint_string
"Uniform constraint {uniform_constraint_index} violated at step {step_index}",
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion jolt-core/src/r1cs/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl<const C: usize, F: JoltField> R1CSConstraints<C, F> for JoltRV32IMConstrain
JoltIn::Bytecode_RD,
JoltIn::OpFlags(CircuitFlags::Jump),
);
let lhs = JoltIn::Bytecode_ELFAddress + (PC_START_ADDRESS - PC_NOOP_SHIFT);
let lhs = 4 * JoltIn::Bytecode_ELFAddress + PC_START_ADDRESS; // TODO(moodlezoup): is this right?
let rhs = JoltIn::RD_Write;
cs.constrain_eq_conditional(rd_nonzero_and_jmp, lhs, rhs);

Expand Down
2 changes: 1 addition & 1 deletion jolt-core/src/r1cs/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl ConstraintInput for JoltIn {
JoltIn::RS1_Read => &jolt_polynomials.read_write_memory.v_read[0],
JoltIn::RS2_Read => &jolt_polynomials.read_write_memory.v_read[1],
JoltIn::RD_Read => &jolt_polynomials.read_write_memory.v_read[2],
JoltIn::RAM_Read(i) => &jolt_polynomials.read_write_memory.v_read[2 + i],
JoltIn::RAM_Read(i) => &jolt_polynomials.read_write_memory.v_read[3 + i],
JoltIn::RD_Write => &jolt_polynomials.read_write_memory.v_write_rd,
JoltIn::RAM_Write(i) => &jolt_polynomials.read_write_memory.v_write_ram[*i],
JoltIn::ChunksQuery(i) => &jolt_polynomials.instruction_lookups.dim[*i],
Expand Down
16 changes: 4 additions & 12 deletions jolt-core/src/r1cs/spartan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<const C: usize, I: ConstraintInput, F: JoltField> UniformSpartanProof<C, I,
constraint_builder.compute_spartan_Az_Bz_Cz::<PCS>(&flattened_polys);

let comb_func_outer = |eq: &F, az: &F, bz: &F, cz: &F| -> F {
// Below is an optimized form of: *A * (*B * *C - *D)
// Below is an optimized form of: eq * (Az * Bz - Cz)
if az.is_zero() || bz.is_zero() {
if cz.is_zero() {
F::zero()
Expand Down Expand Up @@ -181,21 +181,13 @@ impl<const C: usize, I: ConstraintInput, F: JoltField> UniformSpartanProof<C, I,
let r_col_step = &inner_sumcheck_r[r_col_segment_bits..];

let chi = EqPolynomial::evals(r_col_step);
let witness_polys: Vec<_> = I::flatten::<C>()
.iter()
.map(|witness| witness.get_poly_ref(polynomials))
.collect();
let claimed_witness_evals: Vec<_> = I::flatten::<C>()
let claimed_witness_evals: Vec<_> = flattened_polys
.par_iter()
.map(|witness| {
witness
.get_poly_ref(polynomials)
.evaluate_at_chi_low_optimized(&chi)
})
.map(|poly| poly.evaluate_at_chi_low_optimized(&chi))
.collect();

opening_accumulator.append(
&witness_polys,
&flattened_polys,
DensePolynomial::new(chi),
r_col_step.to_vec(),
&claimed_witness_evals.iter().collect::<Vec<_>>(),
Expand Down
42 changes: 39 additions & 3 deletions jolt-core/src/subprotocols/sumcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,19 @@ impl<F: JoltField> SumcheckInstanceProof<F> {
}
};

// assert_eq!(len, W.len());
#[cfg(test)]
{
assert_eq!(
claim_per_round,
poly_A
.evals_ref()
.iter()
.enumerate()
.map(|(i, a)| *a * witness_value(i))
.sum(),
"Assertion failed at beginning of sumcheck"
);
}

let poly = {
// eval_point_0 = \sum_i A[i] * B[i]
Expand Down Expand Up @@ -421,6 +433,20 @@ impl<F: JoltField> SumcheckInstanceProof<F> {
},
);

#[cfg(test)]
{
assert_eq!(
claim_per_round,
poly_A
.evals_ref()
.iter()
.zip(poly_B.evals_ref().iter())
.map(|(a, b)| *a * b)
.sum(),
"Assertion failed at round 0 of sumcheck"
);
}

/* Round 0 END */

for i in 1..num_rounds {
Expand Down Expand Up @@ -451,8 +477,18 @@ impl<F: JoltField> SumcheckInstanceProof<F> {
|| poly_B.bound_poly_var_top_zero_optimized(&r_i),
);

if i == num_rounds - 1 {
assert_eq!(poly.evaluate(&r_i), poly_A[0] * poly_B[0]);
#[cfg(test)]
{
assert_eq!(
claim_per_round,
poly_A
.evals_ref()
.iter()
.zip(poly_B.evals_ref().iter())
.map(|(a, b)| *a * b)
.sum(),
"Assertion failed at round {i} of sumcheck"
);
}
}

Expand Down

0 comments on commit 40c6c72

Please sign in to comment.