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

Output of to_le_bits doesn't work for functions that take generic array inputs #1354

Closed
1 task
shuklaayush opened this issue May 16, 2023 · 4 comments · Fixed by #2070
Closed
1 task

Output of to_le_bits doesn't work for functions that take generic array inputs #1354

shuklaayush opened this issue May 16, 2023 · 4 comments · Fixed by #2070
Assignees
Labels
bug Something isn't working discussion

Comments

@shuklaayush
Copy link
Contributor

Aim

Running the following tests causes in a panic in test1 while test2 passes

fn and<N>(bits: [u1; N]) -> u1 {
    let mut ans: u1 = 1;
    for i in 0..bits.len() {
        ans *= bits[i];
    }
    ans
}

global n = 11;

#[test]
fn test1() {
    let bits = n.to_le_bits(4);

    let ans = and(bits);
    assert(ans == 0);
}

#[test]
fn test2() {
    let tmp = n.to_le_bits(4);
    let mut bits = [0; 4];
    for i in 0..4 {
       bits[i]= tmp[i]; 
    }

    let ans = and(bits);
    assert(ans == 0);
}

Expected behavior

Both tests should pass

Bug

Error message:

Running 1 test functions...
Testing test1...
The application panicked (crashed).
Message:  called `Option::unwrap()` on a `None` value
Location: crates/noirc_frontend/src/monomorphization/mod.rs:729

To reproduce

  1. Copy the code above
  2. Run nargo test --show-output --allow-warnings test1

Installation method

Binary

Nargo version

nargo 0.5.1 (git version hash: 7f6dede, is dirty: false)

@noir-lang/noir_wasm version

No response

@noir-lang/barretenberg version

No response

@noir-lang/aztec_backend version

No response

Additional context

No response

Submission Checklist

  • Once I hit submit, I will assign this issue to the Project Board with the appropriate tags.
@shuklaayush shuklaayush added the bug Something isn't working label May 16, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir May 16, 2023
@jfecher
Copy link
Contributor

jfecher commented May 16, 2023

This is caused by a bug where the length of the array returned by to_le_bits (and friends) is unknown to the type system since it depends on the argument to the function. A quick fix for now may be to specify the size you expect the array to be:

let bits: [u1; 3] = n.to_le_bits(4);

But this is obviously suboptimal if the argument is not a constant and thus the length is unknown.

@shuklaayush
Copy link
Contributor Author

shuklaayush commented May 16, 2023

A quick fix for now may be to specify the size you expect the array to be:

let bits: [u1; 4] = n.to_le_bits(4);

Even this doesn't work. I get the following error which I guess is suggesting the same problem that you mentioned

error: Expected type [u1; 4], found type [u1; _]

@kevaundray
Copy link
Contributor

@jfecher what is the status of this issue?

@jfecher
Copy link
Contributor

jfecher commented Jul 24, 2023

@kevaundray still open. It has the same cause as most other to_{le,be}_{bits,bytes} bugs. These functions claim they return an array of any length in the stdlib but really they just return a different length depending on the input. We can't express this in the type system so we need to change them to either return slices or return an array with a maximum size and fill zeroes in for the rest.

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

Successfully merging a pull request may close this issue.

3 participants