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

Proving phase fails for operations on negative literals #2098

Closed
Jurarpereurs opened this issue Aug 1, 2023 · 2 comments · Fixed by #2153
Closed

Proving phase fails for operations on negative literals #2098

Jurarpereurs opened this issue Aug 1, 2023 · 2 comments · Fixed by #2153
Labels
bug Something isn't working

Comments

@Jurarpereurs
Copy link

Aim

If you do some operations over a negative literal, e.g., 2 * -1, the proving phase would complain that some constraints are not satisfied. This is unexpected, since 2 * -1 is just -2, which is a valid literal.

Consider the following example:

let t: u32 =  2 * -1;

The proving phase would complain that the following constraints are not satisfied:

error: Unsatisfied constraint
  ┌─ /home/Jurarpereurs/noir/src/main.nr:5:19
  │
5 │     let t: u32 =  2 * -1;
  │                   ------ Constraint failed

Error: could not satisfy all constraints

Location:
    crates/nargo_cli/src/cli/mod.rs:74:5

If you explicitly cast the negative literal to the target type, the proving phase still fails:

let t: u32 =  2 * (-1 as u32);

If you directly assign the negative literal to the target type, the proving phase would succeed:

let t: u32 =  -2;

Expected Behavior

The proving phase should succeed for all the three cases.

let t: u32 =  2 * -1;
let t: u32 =  2 * (-1 as u32);
let t: u32 =  -2;

Bug

The proving phase throws an error message for the line:

let t: u32 =  2 * -1;

with the error message as:

error: Unsatisfied constraint
  ┌─ /home/Jurarpereurs/noir/src/main.nr:5:19
  │
5 │     let t: u32 =  2 * -1;          // This one fails
  │                   ------ Constraint failed

Error: could not satisfy all constraints

Location:
    crates/nargo_cli/src/cli/mod.rs:74:5

To Reproduce

Save the following code into src/main.nr:

use dep::std;

fn main() {
    let t: u32 = -2;               // This one succeeds
    let t: u32 =  2 * -1;          // This one fails
    // let t: u32 =  2 * (-1 as u32); // This one also fails
    std::println(t);
}

Then run the following command:

nargo prove p

Installation Method

Binary

Nargo Version

nargo 0.9.0 (git version hash: 6acc242, is dirty: false)

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@Jurarpereurs Jurarpereurs added the bug Something isn't working label Aug 1, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Aug 1, 2023
@jfecher
Copy link
Contributor

jfecher commented Aug 1, 2023

Probably related to #2045

@jfecher jfecher linked a pull request Aug 4, 2023 that will close this issue
5 tasks
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Aug 7, 2023
@Jurarpereurs
Copy link
Author

I just quickly checked the latest version of Noir, and found that this issue hasn't been correctly fixed yet. Although the compiler doesn't complain about the 2 * -1 operation, it somehow throws an error on a simple statement as let a: u32 = -1. To reproduce, please refer to the issue #2045, and you will find the compiler would throw an error that piece of code with the following message:

error: u8 cannot be used in a unary operation

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.

2 participants