Skip to content

Commit c2eb3be

Browse files
committed
Deduplicate some code
1 parent 9bbfdfe commit c2eb3be

File tree

1 file changed

+43
-46
lines changed

1 file changed

+43
-46
lines changed

relations/src/gr1cs/constraint_system.rs

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -285,25 +285,14 @@ impl<F: Field> ConstraintSystem<F> {
285285

286286
let lc_indices = lcs.into_iter().map(|lc| {
287287
let lc = lc();
288-
match lc.0.as_slice() {
289-
// If the linear combination is empty, we return a symbolic LC with index 0.
290-
// If the linear combination is just Zero, we return a symbolic LC with index 0.
291-
[] | [(_, Variable::Zero)] => Variable::SymbolicLc(LcIndex(0)),
292-
// If the linear combination is just another linear combination
293-
// with a coefficient of 1, we return the variable directly.
294-
[(c, var)] if c.is_one() => *var,
295-
// In all other cases, we create a new linear combination
296-
_ => {
297-
let index = LcIndex(*num_lcs);
298-
lc_map.push(Some(lc));
299-
*num_lcs += 1;
300-
if should_generate_lc_assignments {
301-
let value = assignments.eval_lc(index, lc_map).unwrap();
302-
assignments.lc_assignment.push(value)
303-
}
304-
Variable::SymbolicLc(index)
305-
},
306-
}
288+
Self::new_lc_add_helper(
289+
num_lcs,
290+
lc_map,
291+
should_generate_lc_assignments,
292+
assignments,
293+
lc,
294+
)
295+
.unwrap()
307296
});
308297

309298
let predicate = self
@@ -505,40 +494,54 @@ impl<F: Field> ConstraintSystem<F> {
505494
#[inline]
506495
fn new_lc_without_adding(&mut self) -> crate::gr1cs::Result<Variable> {
507496
let index = LcIndex(self.num_linear_combinations);
508-
self.lc_map.push(None);
509497
self.num_linear_combinations += 1;
510498
Ok(Variable::SymbolicLc(index))
511499
}
512500

501+
fn new_lc_add_helper(
502+
cur_num_lcs: &mut usize,
503+
lc_map: &mut Vec<Option<LinearCombination<F>>>,
504+
should_generate_lc_assignments: bool,
505+
assignments: &mut Assignments<F>,
506+
lc: LinearCombination<F>,
507+
) -> crate::gr1cs::Result<Variable> {
508+
match lc.0.as_slice() {
509+
// If the linear combination is empty, we return a symbolic LC with index 0.
510+
[] | [(_, Variable::Zero)] => Ok(Variable::SymbolicLc(LcIndex(0))),
511+
// If the linear combination is just another variable
512+
// with a coefficient of 1, we return the variable directly.
513+
[(c, var)] if c.is_one() => Ok(*var),
514+
// In all other cases, we create a new linear combination
515+
_ => {
516+
let index = LcIndex(*cur_num_lcs);
517+
lc_map.push(Some(lc));
518+
*cur_num_lcs += 1;
519+
if should_generate_lc_assignments {
520+
let value = assignments.eval_lc(index, lc_map).unwrap();
521+
assignments.lc_assignment.push(value)
522+
}
523+
Ok(Variable::SymbolicLc(index))
524+
},
525+
}
526+
}
527+
513528
/// Helper function to add a new linear combination to the constraint system.
514529
#[inline]
515530
fn new_lc_helper(
516531
&mut self,
517532
lc: impl FnOnce() -> LinearCombination<F>,
518533
) -> crate::gr1cs::Result<Variable> {
519534
let should_push = self.should_construct_matrices() || self.should_generate_lc_assignments();
535+
let should_generate_lc_assignments = self.should_generate_lc_assignments();
520536
if should_push {
521537
let lc = lc();
522-
match lc.0.as_slice() {
523-
// If the linear combination is empty, we return a symbolic LC with index 0.
524-
[] | [(_, Variable::Zero)] => Ok(Variable::SymbolicLc(LcIndex(0))),
525-
// If the linear combination is just another linear combination
526-
// with a coefficient of 1, we return the variable directly.
527-
[(c, var)] if c.is_one() => Ok(*var),
528-
// In all other cases, we create a new linear combination
529-
_ => {
530-
let index = LcIndex(self.num_linear_combinations);
531-
self.lc_map.push(Some(lc));
532-
self.num_linear_combinations += 1;
533-
if self.should_generate_lc_assignments() {
534-
let value = self
535-
.eval_lc(index)
536-
.ok_or(SynthesisError::AssignmentMissing)?;
537-
self.assignments.lc_assignment.push(value)
538-
}
539-
Ok(Variable::SymbolicLc(index))
540-
},
541-
}
538+
Self::new_lc_add_helper(
539+
&mut self.num_linear_combinations,
540+
&mut self.lc_map,
541+
should_generate_lc_assignments,
542+
&mut self.assignments,
543+
lc,
544+
)
542545
} else {
543546
self.new_lc_without_adding()
544547
}
@@ -675,12 +678,6 @@ impl<F: Field> ConstraintSystem<F> {
675678
self.assignments.assigned_value(v)
676679
}
677680

678-
/// Evaluate the linear combination `lc` with the assigned values and return
679-
/// the result.
680-
fn eval_lc(&self, lc: LcIndex) -> Option<F> {
681-
self.assignments.eval_lc(lc, &self.lc_map)
682-
}
683-
684681
/// If `self` is satisfied, outputs `Ok(true)`.
685682
/// If `self` is unsatisfied, outputs `Ok(false)`.
686683
/// If `self.is_in_setup_mode()` or if `self == None`, outputs `Err(())`.

0 commit comments

Comments
 (0)