Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
fix: Remove panic when we divide 0/0 in quotient directive (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray authored Jul 17, 2023
1 parent 0e1a64e commit 9c8ff64
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion acvm/src/pwg/directives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn solve_directives_internal(
None => FieldElement::one(),
};

let (int_r, int_q) = if pred_value.is_zero() {
let (int_r, int_q) = if pred_value.is_zero() || int_b.is_zero() {
(BigUint::zero(), BigUint::zero())
} else {
(&int_a % &int_b, &int_a / &int_b)
Expand Down Expand Up @@ -188,3 +188,31 @@ fn format_field_string(field: FieldElement) -> String {
}
"0x".to_owned() + &trimmed_field
}

#[cfg(test)]
mod tests {
use acir::{
circuit::directives::{Directive, QuotientDirective},
native_types::{Expression, Witness, WitnessMap},
FieldElement,
};

use super::solve_directives_internal;

#[test]
fn divisor_is_zero() {
let quotient_directive = QuotientDirective {
a: Expression::zero(),
b: Expression::zero(),
q: Witness(0),
r: Witness(0),
predicate: Some(Expression::one()),
};

let mut witness_map = WitnessMap::new();
witness_map.insert(Witness(0), FieldElement::zero());

solve_directives_internal(&mut witness_map, &Directive::Quotient(quotient_directive))
.expect("expected 0/0 to return 0");
}
}

0 comments on commit 9c8ff64

Please sign in to comment.