Skip to content

Commit

Permalink
r1cs optimization bugfix: use tracking (#175)
Browse files Browse the repository at this point in the history
The R1CS optimizer tracks variable uses so that it can run faster.

Our tracker would sometimes think that a variable has cancelled from a
constraint when it had not.

Now, the tracker is conservative, but correct.
  • Loading branch information
alex-ozdemir authored Oct 31, 2023
1 parent 805a7f4 commit c035529
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/target/r1cs/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ impl LinReducer {
let m = e.get_mut();
*m += sc.clone() * v;
if e.get().is_zero() {
uses.get_mut(i).unwrap().remove(&con_id);
// You might think that removing con_id from `uses.get_mut(i)`
// would be okay here.
//
// But no! Why? Because the constraint has three LCs. Just because
// the variable cancelled in *this* LC doesn't mean that it has
// cancelled in the others.
e.remove_entry();
}
}
Expand Down Expand Up @@ -120,8 +125,9 @@ impl LinReducer {
if let Some((var, lc)) = as_linear_sub(&self.r1cs.constraints[con_id], &self.r1cs) {
if lc.monomials.len() < self.lc_size_thresh {
debug!(
"Elim: {} -> {}",
"Elim: {} ({:?}) -> {}",
self.r1cs.idx_to_sig.get_fwd(&var).unwrap(),
var,
self.r1cs.format_lc(&lc)
);
self.clear_constraint(con_id);
Expand Down

0 comments on commit c035529

Please sign in to comment.