noir: Init vs Final SSA: array mutation; ACIR and Brillig both wrong

Aim

Discovered testing https://github.com/noir-lang/noir/pull/8216

The following program returns a different result with --minimal-ssa compared to both ACIR and Brillig:

fn main() -> pub [Field; 3] {
    let mut a = [10, 20, 30];
    a[1] = {
        a = {
            a[2] = a[0];
            [a[0], 40, a[2]]
        };
        a[1]
    };
    a
}

Expected Behavior

The expected return value is [10, 40, 10], confirmed by Rust

Bug

$ cargo run -q -p nargo_cli -- execute --skip-underconstrained-check --skip-brillig-constraints-check --silence-warnings --force 
[regression_fuzz_testing] Circuit witness successfully solved
[regression_fuzz_testing] Witness saved to target/regression_fuzz_testing.gz
[regression_fuzz_testing] Circuit output: Vec([Field(10), Field(40), Field(30)])
$ cargo run -q -p nargo_cli -- execute --skip-underconstrained-check --skip-brillig-constraints-check --silence-warnings --force --force-brillig
[regression_fuzz_testing] Circuit witness successfully solved
[regression_fuzz_testing] Witness saved to target/regression_fuzz_testing.gz
[regression_fuzz_testing] Circuit output: Vec([Field(10), Field(40), Field(30)])
$ cargo run -q -p nargo_cli -- execute --skip-underconstrained-check --skip-brillig-constraints-check --silence-warnings --force --minimal-ssa
[regression_fuzz_testing] Circuit witness successfully solved
[regression_fuzz_testing] Witness saved to target/regression_fuzz_testing.gz
[regression_fuzz_testing] Circuit output: Vec([Field(10), Field(40), Field(10)])
$ 


To Reproduce

See above.

Workaround

None

Workaround Description

No response

Additional Context

No response

Project Impact

None

Blocker Context

No response

Nargo Version

nargo version = 1.0.0-beta.4 noirc version = 1.0.0-beta.4+b0d9c680c3e9f6a818382b4f57ae2b5a93c657ab (git version hash: 0879bacabb4dcbf5f436575049b464e8fb35abac, is dirty: true)

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

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (15 by maintainers)

Most upvoted comments

I thought if the shifting happened earlier, then the mem2reg would have been correct.

No unfortunately, the shifted offsets are a Brillig only optimization and should never happen earlier. Check out the module comments and tests of that pass to learn more.

The crux of the issue looks to be how we code gen assignments with block expressions.

mem2reg looks fine here, running this in the ssa interpreter (and manually verifying) shows that the result even before Mem2Reg (1) is still 10, 40, 30:

acir(inline) fn main f0 {
  b0():
    v3 = make_array [Field 10, Field 20, Field 30] : [Field; 3]
    v4 = allocate -> &mut [Field; 3]   
    store v3 at v4                                     ; 10, 20, 30
    v5 = load v4 -> [Field; 3]                         ; 10, 20, 30
    v6 = load v4 -> [Field; 3]                         ; 10, 20, 30
    v7 = load v4 -> [Field; 3]                         ; 10, 20, 30
    v9 = array_get v7, index u32 0 -> Field            ; 10
    v11 = array_set v6, index u32 2, value v9          ; 10, 20, 10
    store v11 at v4                                    ; 10, 20, 10
    v12 = load v4 -> [Field; 3]                        ; 10, 20, 10
    v13 = array_get v12, index u32 0 -> Field          ; 10
    v14 = load v4 -> [Field; 3]                        ; 10, 20, 10
    v15 = array_get v14, index u32 2 -> Field          ; 10
    v17 = make_array [v13, Field 40, v15] : [Field; 3] ; 10, 40, 10
    store v17 at v4                                    ; 10, 40, 10
    v18 = load v4 -> [Field; 3]                        ; 10, 40, 10
    v20 = array_get v18, index u32 1 -> Field          ; 40
    v21 = array_set v5, index u32 1, value v20         ; 10, 40, 30
    store v21 at v4                                    ; 10, 40, 30
    v22 = load v4 -> [Field; 3]                        ; 10, 40, 30
    return v22                                         ; 10, 40, 30
}