Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure witness size is power of two #338

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update crr1csproof.rs
  • Loading branch information
DeVikingMark authored Dec 27, 2024
commit 6220fea818efa368cc5627bcbf045b70efad5b9f
31 changes: 31 additions & 0 deletions spartan/src/crr1csproof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,14 @@
instance: &CRR1CSInstance<G, PC>,
witness: CRR1CSWitness<G::ScalarField>,
key: &CRR1CSKey<G, PC>,
transcript: &mut Transcript,

Check warning on line 256 in spartan/src/crr1csproof.rs

View workflow job for this annotation

GitHub Actions / check-fmt

Diff in /home/runner/work/nexus-zkvm/nexus-zkvm/spartan/src/crr1csproof.rs
) -> (CRR1CSProof<G, PC>, Vec<G::ScalarField>, Vec<G::ScalarField>) {
let timer_prove = Timer::new("CRR1CSProof::prove");

// Check if witness size is a power of two
let witness_size = witness.W.len();

Check failure on line 261 in spartan/src/crr1csproof.rs

View workflow job for this annotation

GitHub Actions / check-build

no method named `len` found for struct `Assignment` in the current scope

Check failure on line 261 in spartan/src/crr1csproof.rs

View workflow job for this annotation

GitHub Actions / cargo-clippy

no method named `len` found for struct `Assignment` in the current scope
assert!(witness_size.is_power_of_two(), "Witness size must be a power of two");

<Transcript as ProofTranscript<G>>::append_protocol_name(
transcript,
CRR1CSProof::<G, PC>::protocol_name(),
Expand All @@ -272,9 +277,14 @@
let CRR1CSWitness { W: _vars, E } = witness;

let (inst, input, vars) = (&_inst, _input.assignment.as_slice(), _vars.assignment);

Check warning on line 280 in spartan/src/crr1csproof.rs

View workflow job for this annotation

GitHub Actions / check-fmt

Diff in /home/runner/work/nexus-zkvm/nexus-zkvm/spartan/src/crr1csproof.rs
// we currently require the number of |inputs| + 1 to be at most number of vars
assert!(input.len() < vars.len());

// Check if E vector size is power of two and matches witness size
assert!(E.len().is_power_of_two(), "Error vector size must be a power of two");
assert_eq!(witness_size, E.len(), "Witness and error vector sizes must match");

<Transcript as ProofTranscript<G>>::append_scalars(transcript, b"input", input);
<Transcript as ProofTranscript<G>>::append_scalar(transcript, b"u", u);
comm_W.append_to_transcript(b"comm_W", transcript);
Expand Down Expand Up @@ -676,4 +686,25 @@
)
.is_ok());
}

#[test]
#[should_panic(expected = "Witness size must be a power of two")]
fn test_witness_size_not_power_of_two() {
let num_vars = 1023; // Not a power of two
let num_cons = 1024;
let num_inputs = 10;

Check warning on line 695 in spartan/src/crr1csproof.rs

View workflow job for this annotation

GitHub Actions / check-fmt

Diff in /home/runner/work/nexus-zkvm/nexus-zkvm/spartan/src/crr1csproof.rs
let (shape, instance, witness, gens) =
produce_synthetic_crr1cs::<G1Projective, Hyrax<G1Projective>>(num_cons, num_vars, num_inputs);

let mut prover_transcript = Transcript::new(b"example");

// This should panic because witness size is not power of two
let _ = CRR1CSProof::prove(
&shape,
&instance,
witness,
&gens.gens_r1cs_sat,
&mut prover_transcript,
);
}
}
Loading