Skip to content

Commit

Permalink
improving --O1 simplification: avoiding unnecessary constraint normal…
Browse files Browse the repository at this point in the history
…izations
  • Loading branch information
clararod9 committed Feb 7, 2023
1 parent c7432b4 commit 6035538
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
12 changes: 9 additions & 3 deletions circom_algebra/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,14 +762,20 @@ impl<C: Default + Clone + Display + Hash + Eq> ArithmeticExpression<C> {
) {
use ArithmeticExpression::*;
match expr {
Linear { coefficients } => raw_substitution(coefficients, substitution, field),
Linear { coefficients } => {
raw_substitution(coefficients, substitution, field);
*coefficients = remove_zero_value_coefficients(std::mem::take(coefficients));
}
Signal { symbol } if *symbol == substitution.from => {
*expr = Linear { coefficients: substitution.to.clone() };
}
Quadratic { a, b, c } => {
raw_substitution(a, substitution, field);
*a = remove_zero_value_coefficients(std::mem::take(a));
raw_substitution(b, substitution, field);
*b = remove_zero_value_coefficients(std::mem::take(b));
raw_substitution(c, substitution, field);
*c = remove_zero_value_coefficients(std::mem::take(c));
}
_ => {}
}
Expand Down Expand Up @@ -1137,7 +1143,7 @@ impl<C: Default + Clone + Display + Hash + Eq> Constraint<C> {
raw_substitution(&mut constraint.a, substitution, field);
raw_substitution(&mut constraint.b, substitution, field);
raw_substitution(&mut constraint.c, substitution, field);
Constraint::fix_constraint(constraint, field);
//Constraint::fix_constraint(constraint, field);
}

pub fn remove_zero_value_coefficients(constraint: &mut Constraint<C>) {
Expand Down Expand Up @@ -1284,7 +1290,7 @@ fn raw_substitution<C>(
ArithmeticExpression::multiply_coefficients_by_constant(&val, &mut coefficients, field);
ArithmeticExpression::add_coefficients_to_coefficients(&coefficients, change, field);
}
*change = remove_zero_value_coefficients(std::mem::take(change));
//*change = remove_zero_value_coefficients(std::mem::take(change));
}

fn remove_zero_value_coefficients<C>(raw_expression: HashMap<C, BigInt>) -> HashMap<C, BigInt>
Expand Down
7 changes: 6 additions & 1 deletion circom_algebra/src/simplification_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,17 @@ pub fn debug_substitution_check(substitutions: &HashMap<usize, S>) -> bool {
result
}

pub fn fast_encoded_constraint_substitution(c: &mut C, enc: &HashMap<usize, A>, field: &BigInt) {
pub fn fast_encoded_constraint_substitution(c: &mut C, enc: &HashMap<usize, A>, field: &BigInt)-> bool {
let signals = C::take_cloned_signals(c);
let mut applied_substitution = false;
for signal in signals {
if let Some(expr) = HashMap::get(enc, &signal) {
let sub = S::new(signal, expr.clone()).unwrap();
C::apply_substitution(c, &sub, field);
applied_substitution = true;
}
}
applied_substitution
}

pub fn fast_encoded_substitution_substitution(s: &mut S, enc: &HashMap<usize, A>, field: &BigInt) {
Expand Down Expand Up @@ -658,6 +661,7 @@ pub fn check_substitutions(
let mut cons = S::substitution_into_constraint(s.clone(), field);
for s_2 in subs_2{
C::apply_substitution(&mut cons, s_2, field);
C::fix_constraint(&mut cons, field);
}
if !cons.is_empty(){
println!("ERROR: FOUND NOT EMPTY SUBS");
Expand All @@ -672,6 +676,7 @@ pub fn check_substitutions(
let mut cons = S::substitution_into_constraint(s.clone(), field);
for s_2 in subs_1{
C::apply_substitution(&mut cons, s_2, field);
C::fix_constraint(&mut cons, field);
}
if !cons.is_empty(){
println!("ERROR: FOUND NOT EMPTY SUBS");
Expand Down
18 changes: 14 additions & 4 deletions constraint_list/src/constraint_simplification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ fn apply_substitution_to_map(
let c_id = *c_id;
let mut constraint = storage.read_constraint(c_id).unwrap();
C::apply_substitution(&mut constraint, substitution, field);
C::fix_constraint(&mut constraint, field);
if C::is_linear(&constraint) {
linear.push_back(c_id);
}
Expand Down Expand Up @@ -486,10 +487,14 @@ pub fn simplification(smp: &mut Simplifier) -> (ConstraintStorage, SignalMap) {
LinkedList::append(&mut lconst, &mut cons);
let mut substitutions = build_encoded_fast_substitutions(subs);
for constraint in &mut linear {
fast_encoded_constraint_substitution(constraint, &substitutions, &field);
if fast_encoded_constraint_substitution(constraint, &substitutions, &field){
C::fix_constraint(constraint, &field);
}
}
for constraint in &mut cons_equalities {
fast_encoded_constraint_substitution(constraint, &substitutions, &field);
if fast_encoded_constraint_substitution(constraint, &substitutions, &field){
C::fix_constraint(constraint, &field);
}
}
for signal in substitutions.keys().cloned() {
deleted.insert(signal);
Expand All @@ -508,7 +513,9 @@ pub fn simplification(smp: &mut Simplifier) -> (ConstraintStorage, SignalMap) {
LinkedList::append(&mut lconst, &mut cons);
let substitutions = build_encoded_fast_substitutions(subs);
for constraint in &mut linear {
fast_encoded_constraint_substitution(constraint, &substitutions, &field);
if fast_encoded_constraint_substitution(constraint, &substitutions, &field){
C::fix_constraint(constraint, &field);
}
}
for signal in substitutions.keys().cloned() {
deleted.insert(signal);
Expand Down Expand Up @@ -555,7 +562,9 @@ pub fn simplification(smp: &mut Simplifier) -> (ConstraintStorage, SignalMap) {
// println!("End of cluster simplification: {} ms", dur);
LinkedList::append(&mut lconst, &mut cons);
for constraint in &mut lconst {
fast_encoded_constraint_substitution(constraint, &substitutions, &field);
if fast_encoded_constraint_substitution(constraint, &substitutions, &field){
C::fix_constraint(constraint, &field);
}
}
substitutions
} else {
Expand Down Expand Up @@ -616,6 +625,7 @@ pub fn simplification(smp: &mut Simplifier) -> (ConstraintStorage, SignalMap) {
for substitution in &substitutions {
C::apply_substitution(constraint, substitution, &field);
}
C::fix_constraint(constraint, &field);
}
linear = apply_substitution_to_map(
&mut constraint_storage,
Expand Down

0 comments on commit 6035538

Please sign in to comment.