Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: field zero division in brillig #7386

Merged
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion acvm-repo/brillig_vm/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ pub(crate) fn evaluate_binary_field_op<F: AcirField>(
BinaryFieldOp::Add => MemoryValue::new_field(a + b),
BinaryFieldOp::Sub => MemoryValue::new_field(a - b),
BinaryFieldOp::Mul => MemoryValue::new_field(a * b),
BinaryFieldOp::Div => MemoryValue::new_field(a / b),
BinaryFieldOp::Div => {
if b.is_zero() {
return Err(BrilligArithmeticError::DivisionByZero);
} else {
MemoryValue::new_field(a / b)
}
}
BinaryFieldOp::IntegerDiv => {
if b.is_zero() {
return Err(BrilligArithmeticError::DivisionByZero);
Expand Down
41 changes: 41 additions & 0 deletions acvm-repo/brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@
self.set_program_counter(*location)
}
Opcode::Const { destination, value, bit_size } => {
// Consts are not checked in runtime to fit in the bit size, since they can safely be checked statically.

Check warning on line 393 in acvm-repo/brillig_vm/src/lib.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Consts)
self.memory.write(*destination, MemoryValue::new_from_field(*value, *bit_size));
self.increment_program_counter()
}
Expand Down Expand Up @@ -958,7 +958,7 @@
}

#[test]
fn jmpifnot_opcode() {

Check warning on line 961 in acvm-repo/brillig_vm/src/lib.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (jmpifnot)
let calldata: Vec<FieldElement> = vec![1u128.into(), 2u128.into()];

let opcodes = vec![
Expand Down Expand Up @@ -1207,7 +1207,7 @@
}

#[test]
fn cmov_opcode() {

Check warning on line 1210 in acvm-repo/brillig_vm/src/lib.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (cmov)
let calldata: Vec<FieldElement> =
vec![(0u128).into(), (1u128).into(), (2u128).into(), (3u128).into()];

Expand Down Expand Up @@ -1464,7 +1464,7 @@
}

#[test]
fn iconst_opcode() {

Check warning on line 1467 in acvm-repo/brillig_vm/src/lib.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (iconst)
let opcodes = &[
Opcode::Const {
destination: MemoryAddress::direct(0),
Expand Down Expand Up @@ -2406,4 +2406,45 @@

assert_eq!(output_value.to_field(), FieldElement::from(1u128));
}

#[test]
fn field_zero_division_regression() {
let calldata: Vec<FieldElement> = vec![];

let opcodes = &[
Opcode::Const {
destination: MemoryAddress::direct(0),
bit_size: BitSize::Field,
value: FieldElement::from(1u64),
},
Opcode::Const {
destination: MemoryAddress::direct(1),
bit_size: BitSize::Field,
value: FieldElement::from(0u64),
},
Opcode::BinaryFieldOp {
destination: MemoryAddress::direct(2),
op: BinaryFieldOp::Div,
lhs: MemoryAddress::direct(0),
rhs: MemoryAddress::direct(1),
},
];
let solver = StubbedBlackBoxSolver::default();
let mut vm = VM::new(calldata, opcodes, &solver, false);

let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
let status = vm.process_opcode();
assert_eq!(
status,
VMStatus::Failure {
reason: FailureReason::RuntimeError {
message: "Attempted to divide by zero".into()
},
call_stack: vec![2]
}
);
}
}
Loading