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

fix: Remove panic when we divide 0/0 in quotient directive #437

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Changes from all 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
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() {
vezenovm marked this conversation as resolved.
Show resolved Hide resolved
(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");
}
}