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

Incorrect % (modulo) operator behavior for negative divisors #6609

Closed
Aristotelis2002 opened this issue Nov 25, 2024 · 1 comment · Fixed by #6635
Closed

Incorrect % (modulo) operator behavior for negative divisors #6609

Aristotelis2002 opened this issue Nov 25, 2024 · 1 comment · Fixed by #6635
Assignees
Labels
bug Something isn't working

Comments

@Aristotelis2002
Copy link

Aristotelis2002 commented Nov 25, 2024

Aim

Attempted to create a simple program which does the modulo operation between a positive and a negative number.

The following main.nr:

fn main(x: i16, y: i16) {
    println(x % -13);
    println(x % y);
}

with Prover.toml:

x = "169"
y = "-13"

Outputs

169
169

This output is unexpected. It should be

0
0

Another example is the following main.nr:

fn main(x: i16, y: i16) {
    println(x % -107);
    println(x % y);
}

with Prover.toml:

x = "769"
y = "-107"

Outputs

769
769

In Rust the same program would have the output 20. In some other programming languages the output would be -20

Expected Behavior

Consisted behavior of the modulo operator. Currently the modulo operation between negative constants fails to compile but it has undefined behavior for negative integer parameters.

Bug

The modulo operator produces an incorrect value when working with negative values passed as parameters.

To Reproduce

  1. Create a Noir project with the provided code
  2. nargo check
  3. Insert the provided values into the Prover.toml
  4. nargo execute

Workaround

None

Workaround Description

No response

Additional Context

No response

Project Impact

None

Blocker Context

No response

Nargo Version

nargo version = 0.39.0 noirc version = 0.39.0+45eb7568d56b2d254453b85f236d554232aa5df9 (git version hash: 45eb756, is dirty: false)

NoirJS Version

No response

Proving Backend Tooling & Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@Aristotelis2002 Aristotelis2002 added the bug Something isn't working label Nov 25, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Nov 25, 2024
@guipublic guipublic self-assigned this Nov 27, 2024
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Nov 28, 2024
@Subway2023
Copy link

Subway2023 commented Dec 16, 2024

Is this really a bug? I get the same result in the circom compiler as well.
test.sh

circom circuit.circom --r1cs --wasm --sym

snarkjs wtns calculate ./circuit_js/circuit.wasm input.json witness.wtns
snarkjs wtns export json witness.wtns witness.json

snarkjs wtns check circuit.r1cs witness.wtns
template Multiplier() {
    signal input x;
    signal input y;

    log("x%-13: ",x%-13);
    log("x % y: ",x % y);
}

component main = Multiplier();
x%-13:  169
x % y:  169

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
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

3 participants