noir-lang / noir

Noir is a domain specific language for zero knowledge proofs
https://noir-lang.org
Apache License 2.0
854 stars 185 forks source link

Unable to generate intermediary artifacts for a large circuit #3801

Open mikirov opened 8 months ago

mikirov commented 8 months ago

Aim

Generation of intermediate proof artifacts using the NoirJS SDK for later use for recursive proof verification.

Expected Behavior

Proper generation of verification key as an array of field elements, proof as an array of field elements, verification key hash.

Bug

The intermediate artifact generation code gives the following error:

     RuntimeError: unreachable
      at wasm://wasm/010be49a:wasm-function[11252]:0x403f71
      at wasm://wasm/010be49a:wasm-function[108]:0x1f9d0
      at wasm://wasm/010be49a:wasm-function[103]:0x1f859
      at wasm://wasm/010be49a:wasm-function[50]:0x1e784
      at BarretenbergWasmMain.call (file:///Users/shimonchick/Repos/utxo-poc/node_modules/@aztec/bb.js/dest/node/barretenberg_wasm/barretenberg_wasm_base/index.js:91:40)
      at BarretenbergWasmMain.callWasmExport (file:///Users/shimonchick/Repos/utxo-poc/node_modules/@aztec/bb.js/dest/node/barretenberg_wasm/barretenberg_wasm_main/index.js:90:14)
      at callback (node_modules/comlink/dist/umd/comlink.js:109:52)
      at MessagePort.l (file:///Users/shimonchick/Repos/utxo-poc/node_modules/@aztec/bb.js/dest/node/barretenberg_wasm/helpers/node/node_endpoint.js:11:21)
      at [nodejs.internal.kHybridDispatch] (node:internal/event_target:822:20)
      at exports.emitMessage (node:internal/per_context/messageport:23:28)

To Reproduce

  1. Clone noir-rsa repo
  2. Add a hardhat test with the following code:
    
    // @ts-ignore
    import { expect } from 'chai';
    import { Noir } from '@noir-lang/noir_js';
    import { BarretenbergBackend } from '@noir-lang/backend_barretenberg';

import path from 'path'; import { ProofData, CompiledCircuit } from '@noir-lang/types';

import dkim_circuit from "../circuits/target/dkim.json"

import * as fs from 'fs';

const getArtifactsPath = (name: string) => { return path.join("circuits", "contract", name, "plonk_vk.sol:UltraVerifier") }

const loadProofData = (name: string): ProofData => { // Read the file as a Buffer to get the hex data const hexData = fs.readFileSync(path.join("circuits", "proofs", ${name}.proof));

// Convert the hex data to a string
const bytes = Uint8Array.from(Buffer.from(hexData.toString(), 'hex'));
console.log(bytes.length)
// Parse the JSON string into ProofData type
return {
    publicInputs: [],
    proof: bytes
};

}

describe("Recursive flow", async () => { let backends; let noirs;

before(async () => {
    backends = {
        dkim: new BarretenbergBackend(dkim_circuit as unknown as CompiledCircuit, { threads: 8 }),
    }
    noirs = {
        dkim: new Noir(dkim_circuit as unknown as CompiledCircuit, backends.dkim),
    }
})

after(async () => {
    await backends.dkim.destroy();
})

it('Should generate an intermediate proof', async () => {

    const dkim_proof_data = loadProofData('dkim');
    const {proofAsFields: dkim_proofAsFields, vkAsFields: dkim_vkAsFields, vkHash: dkim_vkHash } = await backends.dkim.generateIntermediateProofArtifacts(
        dkim_proof_data,
        0, // numPublicInputs
    );
    expect(dkim_vkAsFields).to.be.of.length(114);
    expect(dkim_vkHash).to.be.a('string');

    console.log(dkim_vkAsFields)
    console.log(dkim_vkHash)

});

})


3. Run `npx hardhat test`
4. Notice `RuntimeError: unreachable` error

### Installation Method

Binary

### Nargo Version

nargo version = 0.19.4 noirc version = 0.19.4+4d133c50a50f21ca23861a9d1987207bd8783d36

### Additional Context

Circuit size limits might be related to https://github.com/noir-lang/noir/issues/3786 and https://github.com/noir-lang/noir/issues/3525

### Would you like to submit a PR for this Issue?

No

### Support Needs

_No response_
Savio-Sou commented 6 months ago

Hi @mikirov, thank you for submitting the Issue.

Could you confirm if this is still a problem with the latest versions of Noir?

If yes, it would help a ton with further debugging if you could share a minimal example for reproduction. Thanks!