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

Compiler panics with Cannot flatten a dynamic array when returning an array from memory #4258

Closed
TomAFrench opened this issue Feb 5, 2024 · 2 comments · Fixed by #4351
Closed
Labels
bug Something isn't working

Comments

@TomAFrench
Copy link
Member

TomAFrench commented Feb 5, 2024

Aim

I would like to prove the following program

fn main(mut x: [Field; 256], index: u8) -> pub [Field; 256] {
    x[index] = 0;
    x
}

Expected Behavior

This should be able to be compiled to a circuit where I write x into memory, write 0 at index index in this memory, then read out the entirety of the block of memory to form the return value.

Bug

The compiler produces the SSA

After Dead Instruction Elimination:
acir fn main f0 {
  b0(v0: [Field; 256], v1: u8):
    v22 = array_set v0, index v1, value Field 0
    return v22
}

acirgen then panics with

The application panicked (crashed).
Message:  internal error: entered unreachable code: Cannot flatten a dynamic array
Location: compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs:1411

This is a bug. We may have already fixed this in newer versions of Nargo so try searching for similar issues at https://github.com/noir-lang/noir/issues/.
If there isn't an open issue for this bug, consider opening one at https://github.com/noir-lang/noir/issues/new?labels=bug&template=bug_report.yml

To Reproduce

  1. compile the above program

Installation Method

Compiled from source

Nargo Version

noirc version = 0.23.0+bdf64ed81d7f34d6478ed9bde5b69e45d171d5f0

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@TomAFrench TomAFrench added the bug Something isn't working label Feb 5, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Feb 5, 2024
@LHerskind
Copy link
Contributor

Seems to also happen on non-returned values (or might be from the sha256? I ran into it when trying to do assertions on the following:

fn compute_root(leaf: [u8; 32], path: [u8; 64], _index: u32, root: [u8; 32]) {
    let mut current = leaf;
    let mut index = _index;

    for i in 0..2 {
        let mut hash_input = [0; 64];
        let offset = i * 32;
        let is_right = (index & 1) != 0;
        let a = if is_right { 32 } else { 0 };
        let b = if is_right { 0 } else { 32 };

        for j in 0..32 {
            hash_input[j + a] = current[j];
            hash_input[j + b] = path[offset + j];
        }

        current = sha256(hash_input);
        index = index >> 1;
    }
    // Either of the following two lines will make it fail with `Cannot flatten a dynamic array`
    // println(current);
    // assert(root == current);
}

On noirc version = 0.23.0+5be9f9d7e2f39ca228df10e5a530474af0331704

@TomAFrench
Copy link
Member Author

or might be from the sha256?

Yep, blackbox functions are another situation where we flatten values.

github-merge-queue bot pushed a commit that referenced this issue Feb 13, 2024
# Description

## Problem\*

Resolves #4258 

## Summary\*

There are some instances where we want to flatten a single AcirValue
into its AcirVars. We were operating under the assumption that we cannot
flatten dynamic arrays. I have changed the `flatten` method to be on an
`AcirContext` so that we can read from dynamic memory when appropriate.

I have added two tests performing what is done in the issue.
1. Returning a dynamic array from main
2. Preparing a dynamic array as inputs to a black box function

## Additional Context

Further investigation should be done to see if we should fully remove
the `flatten()` method on `AcirValue` and move to only using the
`flatten` which lives on the AcirContext. I chose to leave that for
separate work though and scope this PR to just the bug fix in the issue.

## Documentation\*

Check one:
- [X] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [X] I have tested the changes locally.
- [X] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Tom French <tom@tomfren.ch>
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Feb 13, 2024
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