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: Divide by zero should fail to satisfy constraints for Field and ints #2475

Merged
merged 10 commits into from
Aug 29, 2023

Conversation

vezenovm
Copy link
Contributor

@vezenovm vezenovm commented Aug 29, 2023

Description

Problem*

Resolves #2149

Summary*

The issue was posted using nargo 0.9.0 and there has since been some changes, although the bug still exists. let a: Field = 3 / 0; still unexpectedly prints zero while let a: u8 = 3 / 0; actually now panics during instruction simplification for attempting to divide by zero. let a: i8 = 3 / 0; fails in the same cases as u8.

When the dividend comes from inputs to main, Field behaves the same way, while u8 panics inside of execution of the Brillig VM. The Brillig VM should return an error for division by zero rather than panic'ing but this is a separate issue.

The following changes were made:

  • For a field division, we have a check in ACIR gen for whether the brillig inverse is valid. However, we were missing this check for constants, this has now been included.
  • I have also introduced constraints to check whether we are dividing by zero in euclidean division. If we are dividing by zero, the execution of the program will fail to satisfy the constraints.
  • I had to remove a simplification for when we have an rhs of zero with a division binary op. Now, instead of a panic on let a: u8 = 3 / 0 we will get a constraint failure that points to the span 3 / 0.

All variants of division by zero (whether by constant or witness) now trigger a failed constraint.

Documentation

  • This PR requires documentation updates when merged.

    • I will submit a noir-lang/docs PR.
    • I will request for and support Dev Rel's help in documenting this PR.

Additional Context

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@vezenovm vezenovm changed the title fix: Divide by zero should fail on Field fix: Divide by zero should fail to satisfy constraints for Field and ints Aug 29, 2023
Copy link
Member

@TomAFrench TomAFrench left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking this temporarily as this is a breaking change

@kevaundray
Copy link
Contributor

Blocking this temporarily as this is a breaking change

I don't think anyone was relying on their program panicking before

@vezenovm
Copy link
Contributor Author

vezenovm commented Aug 29, 2023

I don't think anyone was relying on their program panicking before

Or a Field division by 0 being 0. If anything this will give them the correct result now no?

@vezenovm vezenovm added this pull request to the merge queue Aug 29, 2023
Merged via the queue into master with commit 1b85816 Aug 29, 2023
16 checks passed
@vezenovm vezenovm deleted the mv/div-by-zero-fix branch August 29, 2023 19:11
TomAFrench added a commit that referenced this pull request Aug 30, 2023
* master: (42 commits)
  fix(ssa): Handle right shift with constants (#2481)
  chore(noir): Release 0.10.4 (#2354)
  fix: Divide by zero should fail to satisfy constraints for `Field` and ints (#2475)
  fix(ssa): Remove padding from ToRadix call with constant inputs (#2479)
  fix: Implement handling of array aliasing in the mem2reg optimization pass (#2463)
  chore: resolve `Instruction` inputs fully before checking against cache (#2472)
  chore: Move independent `run_test` function into nargo core (#2468)
  feat: Standard library functions can now be called with closure args  (#2471)
  feat(frontend): aztec syntactic sugar (feature flagged) (#2403)
  chore(ci): enforce compliance with `cargo fmt` (#2467)
  chore(ci): Allow releases to have additional feature flags (#2405)
  feat: Add `assert_eq` keyword (#2137)
  fix(ssa): Do not optimize for allocates in constant folding (#2466)
  feat(ssa): Reuse existing results for duplicated instructions with no side-effects (#2460)
  fix: Closure lvalue capture bugfix (#2457)
  feat: Syntax for environment types now works with generics (#2383)
  fix(parser): fixes for the parsing of 'where' clauses (#2430)
  fix: Run `wasm` nodejs tests with no fails (#2387)
  chore: Run `cargo fmt` (#2455)
  chore: Perform formatting changes to integration tests (#2448)
  ...
TomAFrench added a commit that referenced this pull request Aug 30, 2023
* master: (42 commits)
  fix(ssa): Handle right shift with constants (#2481)
  chore(noir): Release 0.10.4 (#2354)
  fix: Divide by zero should fail to satisfy constraints for `Field` and ints (#2475)
  fix(ssa): Remove padding from ToRadix call with constant inputs (#2479)
  fix: Implement handling of array aliasing in the mem2reg optimization pass (#2463)
  chore: resolve `Instruction` inputs fully before checking against cache (#2472)
  chore: Move independent `run_test` function into nargo core (#2468)
  feat: Standard library functions can now be called with closure args  (#2471)
  feat(frontend): aztec syntactic sugar (feature flagged) (#2403)
  chore(ci): enforce compliance with `cargo fmt` (#2467)
  chore(ci): Allow releases to have additional feature flags (#2405)
  feat: Add `assert_eq` keyword (#2137)
  fix(ssa): Do not optimize for allocates in constant folding (#2466)
  feat(ssa): Reuse existing results for duplicated instructions with no side-effects (#2460)
  fix: Closure lvalue capture bugfix (#2457)
  feat: Syntax for environment types now works with generics (#2383)
  fix(parser): fixes for the parsing of 'where' clauses (#2430)
  fix: Run `wasm` nodejs tests with no fails (#2387)
  chore: Run `cargo fmt` (#2455)
  chore: Perform formatting changes to integration tests (#2448)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Divide-by-zero is a constraint failure for integers but not for Field
3 participants