noir-lang/acvm

Generated ACIR contains expressions with multiple unknowns

dbanks12 opened this issue · 1 comments

Aim

Compile Aztec Noir code after some substantial changes. I am using arrays of structs and doing some fancy things. I am not sure if this code is the culprit, but here is an example:

    fn flatten_and_push_array<S, InnerLen, Len>(&mut self, serialize: fn(S) -> [T; InnerLen], array: [S; Len]) {
        let newLen = self.len + (array.len() * InnerLen);
        assert(newLen as u64 <= MaxLen as u64);
        for outer in 0..array.len() {
            let inner_array = serialize(array[outer]);
            self.push_array(inner_array);
            // TODO: more efficient:
            //for inner in 0..inner_array.len() {
            //    self.storage[self.len + (outer * InnerLen) + inner] = inner_array[inner];
            //}
        }
    }

Bug

Compiles fine, but the following failure occurs during ACIR simulator runtime:

  ● Private Execution test suite › Context oracles › this_portal_address should return the current context's portal address

    Simulation error: Cannot solve opcode: expression has too many unknowns %EXPR [ (1, _22) (-1, _442) 0 ]%

      at 0x0185a3a84767c40003f8b229ef459e022bde786bf7600078da2dd82b0505e31c.82cc9431

    Cause:
    Cannot solve opcode: expression has too many unknowns %EXPR [ (1, _22) (-1, _442) 0 ]%

      at Object.<anonymous>.module.exports.__wbg_constructor_0f1be29bba11a757 (../../node_modules/@noir-lang/acvm_js/nodejs/acvm_js.js:554:17)
      at null.<anonymous> (../wasm:/wasm/00af7092:1:168803)
      at null.<anonymous> (../wasm:/wasm/00af7092:1:528895)
      at null.<anonymous> (../wasm:/wasm/00af7092:1:568185)
      at null.<anonymous> (../wasm:/wasm/00af7092:1:687431)
      at __wbg_adapter_54 (../../node_modules/@noir-lang/acvm_js/nodejs/acvm_js.js:229:10)
      at real (../../node_modules/@noir-lang/acvm_js/nodejs/acvm_js.js:214:20)Error:
      at acvm/acvm.ts:162:19
      at acvm (acvm/acvm.ts:135:26)
      at PrivateFunctionExecution.run (client/private_execution.ts:66:32)
      at AcirSimulator.run (client/simulator.ts:107:14)
      at Object.<anonymous> (client/private_execution.test.ts:1037:22

To Reproduce

A bit of a pain to reproduce my environment, but I can help do so. The steps will be lengthy 🙃

Environment

No response

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

This was resolved and was not a bug in the ACVM! It was a misalignment between the initialWitness that Aztec's ACIR Simulator passes into the ACVM versus the one that nargo computes when compiling a contract.

@sirasistant helped me debug this by suggesting that I print out the initial witness in the ACIR Simulator and compare it to the one generated by nargo (param_witnesses entry for the relevant Noir contract function in target/<contract-dirname>-<ContractName>.json).

This showed me that they were different and I therefore needed to update context.getInitialWitness's serialization to match PrivateContextInputs in Aztec's Noir library.