Skip to content

Commit

Permalink
chore: add integration test for verifying a recursive proof onchain
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Oct 15, 2023
1 parent 37315f8 commit 16d1c23
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 3 deletions.
3 changes: 3 additions & 0 deletions compiler/integration-tests/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const config: HardhatUserConfig = {
},
},
},
mocha: {
timeout: 5 * 60 * 1000,
},
};

export default config;
6 changes: 6 additions & 0 deletions compiler/integration-tests/scripts/codegen-verifiers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ nargo --program-dir $mul_dir codegen-verifier
main_dir=$repo_root/compiler/integration-tests/circuits/main
nargo --program-dir $main_dir codegen-verifier

# Run codegen-verifier for recursion
recursion_dir=$repo_root/compiler/integration-tests/circuits/recursion
nargo --program-dir $recursion_dir codegen-verifier

# Copy compiled contracts from the root of compiler/integration-tests
contracts_dir=$self_path/../contracts
rm -rf $contracts_dir
mkdir $contracts_dir

cp $mul_dir/contract/1_mul/plonk_vk.sol $contracts_dir/1_mul.sol
cp $main_dir/contract/main/plonk_vk.sol $contracts_dir/main.sol
cp $recursion_dir/contract/recursion/plonk_vk.sol $contracts_dir/recursion.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { expect } from 'chai';
import { ethers } from 'hardhat';

import { readFileSync } from 'node:fs';
import { resolve } from 'path';
import toml from 'toml';

import { compile, init_log_level as compilerLogLevel } from '@noir-lang/noir_wasm';
import { Noir } from '@noir-lang/noir_js';
import { BarretenbergBackend } from '@noir-lang/backend_barretenberg';
import { Field, InputMap } from '@noir-lang/noirc_abi';

compilerLogLevel('INFO');

it(`smart contract can verify a recursive proof`, async () => {
const main_source_path = resolve(`./circuits/main/src/main.nr`);
const main_program = compile(main_source_path);

const recursion_source_path = resolve(`./circuits/recursion/src/main.nr`);
const recursion_program = compile(recursion_source_path);

// Intermediate proof

const main_backend = new BarretenbergBackend(main_program);
const main = new Noir(main_program);

const main_prover_toml = readFileSync(resolve(`./circuits/main/Prover.toml`)).toString();
const main_inputs = toml.parse(main_prover_toml);

const { witness: main_witness } = await main.execute(main_inputs);
const intermediate_proof = await main_backend.generateIntermediateProof(main_witness);

expect(await main_backend.verifyIntermediateProof(intermediate_proof)).to.be.true;

const { proofAsFields, vkAsFields, vkHash } = await main_backend.generateIntermediateProofArtifacts(
intermediate_proof,
1, // 1 public input
);

// Final proof

const recursion_backend = new BarretenbergBackend(recursion_program);
const recursion = new Noir(recursion_program, recursion_backend);

const recursion_inputs: InputMap = {
verification_key: vkAsFields,
proof: proofAsFields,
public_inputs: [main_inputs.y as Field],
key_hash: vkHash,
input_aggregation_object: ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
};

const recursion_proof = await recursion.generateFinalProof(recursion_inputs);

// Smart contract verification

const contract = await ethers.deployContract('contracts/recursion.sol:UltraVerifier', []);

const result = await contract.verify.staticCall(recursion_proof.proof, recursion_proof.publicInputs);

expect(result).to.be.true;
});
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ test_cases.forEach((testInfo) => {

// Smart contract verification

const contract = await ethers.deployContract(testInfo.compiled, [], {});
const contract = await ethers.deployContract(testInfo.compiled, []);

const result = await contract.verify(proofData.proof, proofData.publicInputs);
const result = await contract.verify.staticCall(proofData.proof, proofData.publicInputs);

expect(result).to.be.true;
});
Expand Down
1 change: 0 additions & 1 deletion compiler/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0",
"build:nix": "nix build -L .#noir_wasm",
"install:from:nix": "yarn clean && yarn build:nix && cp -rL ./result/noir_wasm/nodejs ./ && cp -rL ./result/noir_wasm/web ./"

},
"peerDependencies": {
"@noir-lang/source-resolver": "workspace:*"
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"nixpkgs",
"noirc",
"noirup",
"nomicfoundation",
"pedersen",
"peekable",
"plonkc",
Expand Down

0 comments on commit 16d1c23

Please sign in to comment.