Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
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
58 changes: 58 additions & 0 deletions spartan/src/crr1csproof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ impl<G: CurveGroup, PC: PolyCommitmentScheme<G>> CRR1CSProof<G, PC> {
transcript: &mut Transcript,
) -> (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();
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 @@ -275,6 +280,11 @@ impl<G: CurveGroup, PC: PolyCommitmentScheme<G>> CRR1CSProof<G, PC> {

// 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,52 @@ mod tests {
)
.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 = 1024;
let num_cons = 1024;
let num_inputs = 10;
let (shape, instance, mut witness, gens) =
produce_synthetic_crr1cs::<G1Projective, Hyrax<G1Projective>>(num_cons, num_vars, num_inputs);

// Modify witness to have non-power-of-two size
witness.W = vec![Fr::zero(); 1023]; // Not a power of two

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,
);
}

#[test]
#[should_panic(expected = "Error vector size must be a power of two")]
fn test_error_vector_size_not_power_of_two() {
let num_vars = 1024;
let num_cons = 1024;
let num_inputs = 10;
let (shape, instance, mut witness, gens) =
produce_synthetic_crr1cs::<G1Projective, Hyrax<G1Projective>>(num_cons, num_vars, num_inputs);

// Modify error vector to have non-power-of-two size
witness.E = vec![Fr::zero(); 1023]; // Not a power of two

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

// This should panic because error vector size is not power of two
let _ = CRR1CSProof::prove(
&shape,
&instance,
witness,
&gens.gens_r1cs_sat,
&mut prover_transcript,
);
}
}
3 changes: 1 addition & 2 deletions vm/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ fn end_profile(vm: &mut NexusVM<impl Memory>, fn_name: String) -> Result<()> {
/// evaluate next instruction
pub fn eval_inst(vm: &mut NexusVM<impl Memory>) -> Result<()> {
if vm
.max_trace_len
.map_or(false, |max_trace_len| max_trace_len <= vm.trace_len)
.max_trace_len.is_some_and(|max_trace_len| max_trace_len <= vm.trace_len)
{
return Err(NexusVMError::MaxTraceLengthExceeded(vm.trace_len));
}
Expand Down