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

"Unreachable" % can cause panics #2572

Closed
LHerskind opened this issue Sep 6, 2023 · 0 comments · Fixed by #2578
Closed

"Unreachable" % can cause panics #2572

LHerskind opened this issue Sep 6, 2023 · 0 comments · Fixed by #2578
Labels
bug Something isn't working

Comments

@LHerskind
Copy link
Contributor

Aim

In the process of testing a safe-math library (safe_u120.nr) I'm running into a strange thing while testing.

For my mul_div_up function as outlined below, I perform a mod operation against user provided input.

fn mul_div_up(
    self: Self,
    b: Self,
    divisor: Self
) -> Self {
    let c = self.mul(b);
    assert(!divisor.is_zero());
    let adder = ((self.value * b.value % divisor.value) as u120 > 0) as u120;
    c.div(divisor).add(Self {value: adder})
}

When running the test:

#[test(should_fail)]
fn test_mul_div_up_zero_divisor() {
    let a = SafeU120::new(6);
    let b = SafeU120::new(3);
    let c = SafeU120::new(0);
    let _d = SafeU120::mul_div_up(a, b, c);
}

It will fail due to a panic. If replacing % with / it will fail due to the assert.

Expected Behavior

Expect to fail due to the assert !divisor.is_zero() similarly to divisions.

Bug

When containing an unreachable modulo computation % divisor with divisor = 0, the program panics instead of failing due to the assert divisor != 0 which is checked earlier.

To Reproduce

  1. Steal the tests in safe_u120.nr
  2. Uncomment the test_mul_div_up_zero_divisor test
  3. Run nargo test

Installation Method

None

Nargo Version

nargo 0.10.5 (git version hash: 5f78772, is dirty: false)

Additional Context

Installed nargo using noirup -v aztec.

Would you like to submit a PR for this Issue?

No

Support Needs

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant