Skip to content

Commit

Permalink
Merge pull request #72 from numtel/fflonk_calldata
Browse files Browse the repository at this point in the history
Fix missing elements in fflonk calldata
  • Loading branch information
erhant authored Apr 15, 2024
2 parents 0af1603 + ed44323 commit e1f8c8c
Show file tree
Hide file tree
Showing 6 changed files with 3,282 additions and 42 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,7 @@ dist
# for init tests
tmptest
inputs/multiplier_3/test-input.json

# Hardhat files
/cache
/artifacts
8 changes: 8 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

const config: HardhatUserConfig = {
solidity: "0.8.24",
};

export default config;
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
"format": "npx prettier --check ./src/**/*.ts ./tests/**/*.ts"
},
"devDependencies": {
"@nomicfoundation/hardhat-chai-matchers": "^2.0.0",
"@nomicfoundation/hardhat-ethers": "^3.0.0",
"@nomicfoundation/hardhat-ignition": "^0.15.0",
"@nomicfoundation/hardhat-ignition-ethers": "^0.15.0",
"@nomicfoundation/hardhat-network-helpers": "^1.0.0",
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"@nomicfoundation/hardhat-verify": "^2.0.0",
"@typechain/ethers-v6": "^0.5.0",
"@typechain/hardhat": "^9.0.0",
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"@types/mocha-each": "^2.0.0",
Expand All @@ -46,11 +55,17 @@
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^5.0.1",
"ethers": "^6.4.0",
"hardhat": "^2.14.0",
"hardhat-gas-reporter": "^1.0.8",
"mocha": "^10.2.0",
"mocha-each": "^2.0.1",
"prettier": "^3.1.0",
"rimraf": "^5.0.1",
"solc": "^0.8.25",
"solidity-coverage": "^0.8.0",
"ts-node": "^10.9.1",
"typechain": "^8.3.0",
"typescript": "^5.3.2"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/utils/calldata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function fflonkCalldata(proof: FflonkProof, pretty: boolean): string {
proof.polynomials.W1[0], proof.polynomials.W1[1],
proof.polynomials.W2[0], proof.polynomials.W2[1],
proof.evaluations.ql, proof.evaluations.qr, proof.evaluations.qm,
proof.evaluations.qo, proof.evaluations.qc,
proof.evaluations.s1, proof.evaluations.s2, proof.evaluations.s3,
proof.evaluations.a, proof.evaluations.b, proof.evaluations.c,
proof.evaluations.z, proof.evaluations.zw,
Expand Down
49 changes: 43 additions & 6 deletions tests/circomkit.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import forEach from 'mocha-each';
//@ts-ignore
import { ethers } from "hardhat";
//@ts-ignore
import solc from "solc";
import {PROTOCOLS} from '../src/utils/config';
import {Circomkit} from '../src';
import {expect} from 'chai';
import {existsSync, rmSync} from 'fs';
import {existsSync, rmSync, readFileSync} from 'fs';
import {PTAU_PATH, prepareMultiplier} from './common';

// we are not testing all curves because PTAU is only available for bn128
Expand Down Expand Up @@ -77,13 +81,46 @@ forEach(PROTOCOLS).describe('protocol: %s', (protocol: (typeof PROTOCOLS)[number
expect(isVerified).to.be.true;
});

it('should export verifier contract', async () => {
it('should export verifier contract and valid calldata', async () => {
const path = await circomkit.contract(circuit.name);
expect(existsSync(path)).to.be.true;
});

it('should export contract calldata', async () => {
await circomkit.calldata(circuit.name, inputName);
const source = readFileSync(path, {encoding: 'utf-8'})
// XXX: snarkjs plonk template has an erroneous hardhat import
// See https://github.com/iden3/snarkjs/pull/464
.replace('import "hardhat/console.sol";\n', '');

// Compile the contract
const input = {
language: 'Solidity',
sources: {
'TestVerifier.sol': {
content: source
}
},
settings: {
outputSelection: {
'*': {
'*': ['abi', 'evm.bytecode.object']
}
}
}
};

const output = JSON.parse(solc.compile(JSON.stringify(input)));
const contractName = Object.keys(output.contracts['TestVerifier.sol'])[0];
const bytecode = output.contracts['TestVerifier.sol'][contractName].evm.bytecode.object;
const abi = output.contracts['TestVerifier.sol'][contractName].abi;

// Deploy the contract using ethers
const ContractFactory = new ethers.ContractFactory(abi, bytecode, (await ethers.getSigners())[0]);
const contract = await ContractFactory.deploy();
await contract.waitForDeployment();

// Interaction with the contract
const calldata = await circomkit.calldata(circuit.name, inputName);
const args = calldata.split('\n').filter(x=>!!x).map(x=>JSON.parse(x));
//@ts-ignore
expect(await contract.verifyProof(...args)).to.equal(true);
});

it('should export JSON files', async () => {
Expand Down
Loading

0 comments on commit e1f8c8c

Please sign in to comment.