Skip to content

Commit

Permalink
feat(circuits): remove zkey-manager in favour of circomkit
Browse files Browse the repository at this point in the history
remove zkey-manager and custom scripts in favour of circomkit, to build circuits and generate
testing zkeys. Update docs to reflect the change.
  • Loading branch information
ctrlc03 committed Jan 15, 2024
1 parent a3b7525 commit 2fdf055
Show file tree
Hide file tree
Showing 20 changed files with 278 additions and 460 deletions.
17 changes: 5 additions & 12 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ jobs:
- name: Download circom Binary v2.1.6
run: |
mkdir -p /home/runner/work/maci/.local/bin
wget -qO /home/runner/work/maci/.local/bin/circom https://github.com/iden3/circom/releases/download/v2.1.6/circom-linux-amd64
chmod +x /home/runner/work/maci/.local/bin/circom
wget -qO ${{ github.workspace }}/circom https://github.com/iden3/circom/releases/download/v2.1.6/circom-linux-amd64
chmod +x ${{ github.workspace }}/circom
sudo mv ${{ github.workspace }}/circom /bin/circom
- name: Create zkeys folder
run: |
Expand All @@ -69,15 +69,8 @@ jobs:
- name: Compile Circuits And Generate zkeys
run: |
cd circuits
export PATH=$PATH:/home/runner/work/maci/.local/bin
pnpm build-test-circuits-c
mv ./build/test/* ../cli/zkeys/
cd ../cli
wget -qO zkeys/powersOfTau28_hez_final_20.ptau https://maci-devops-zkeys.s3.ap-northeast-2.amazonaws.com/powersOfTau28_hez_final_20.ptau
pnpm snarkjs groth16 setup ./zkeys/ProcessMessages_10-2-1-2_test.r1cs ./zkeys/powersOfTau28_hez_final_20.ptau ./zkeys/ProcessMessages_10-2-1-2_test.0.zkey
pnpm snarkjs groth16 setup ./zkeys/TallyVotes_10-1-2_test.r1cs ./zkeys/powersOfTau28_hez_final_20.ptau ./zkeys/TallyVotes_10-1-2_test.0.zkey
pnpm snarkjs groth16 setup ./zkeys/SubsidyPerBatch_10-1-2_test.r1cs ./zkeys/powersOfTau28_hez_final_20.ptau ./zkeys/SubsidyPerBatch_10-1-2_test.0.zkey
pnpm build:circuits-c
pnpm setup:zkeys
- name: ${{ matrix.command }}
run: pnpm run ${{ matrix.command }}
Expand Down
23 changes: 5 additions & 18 deletions .github/workflows/reusable-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,15 @@ jobs:
- name: Download circom Binary v2.1.6
run: |
mkdir -p /home/runner/work/maci/.local/bin
wget -qO /home/runner/work/maci/.local/bin/circom https://github.com/iden3/circom/releases/download/v2.1.6/circom-linux-amd64
chmod +x /home/runner/work/maci/.local/bin/circom
- name: Create zkeys folder
if: ${{ env.CHANGED == 'true' }}
run: |
cd cli
mkdir -p zkeys
wget -qO ${{ github.workspace }}/circom https://github.com/iden3/circom/releases/download/v2.1.6/circom-linux-amd64
chmod +x ${{ github.workspace }}/circom
sudo mv ${{ github.workspace }}/circom /bin/circom
- name: Compile Circuits And Generate zkeys
if: ${{ env.CHANGED == 'true' }}
run: |
cd circuits
export PATH=$PATH:/home/runner/work/maci/.local/bin
pnpm build-test-circuits-c
mv ./build/test/* ../cli/zkeys/
cd ../cli
wget -qO zkeys/powersOfTau28_hez_final_20.ptau https://maci-devops-zkeys.s3.ap-northeast-2.amazonaws.com/powersOfTau28_hez_final_20.ptau
pnpm snarkjs groth16 setup ./zkeys/ProcessMessages_10-2-1-2_test.r1cs ./zkeys/powersOfTau28_hez_final_20.ptau ./zkeys/ProcessMessages_10-2-1-2_test.0.zkey
pnpm snarkjs groth16 setup ./zkeys/TallyVotes_10-1-2_test.r1cs ./zkeys/powersOfTau28_hez_final_20.ptau ./zkeys/TallyVotes_10-1-2_test.0.zkey
pnpm snarkjs groth16 setup ./zkeys/SubsidyPerBatch_10-1-2_test.r1cs ./zkeys/powersOfTau28_hez_final_20.ptau ./zkeys/SubsidyPerBatch_10-1-2_test.0.zkey
pnpm build:circuits-c
pnpm setup:zkeys
- name: Download zkeys
if: ${{ env.CHANGED == 'false' }}
Expand Down
4 changes: 4 additions & 0 deletions circuits/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ptau
input.json
circom/main
circom/test
12 changes: 0 additions & 12 deletions circuits/circom/circuits.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
{
"processMessages": {
"file": "processMessages",
"template": "ProcessMessages",
"params": [6, 8, 2, 3],
"pubs": ["inputHash"]
},
"ProcessMessages_10-2-1-2_test": {
"file": "processMessages",
"template": "ProcessMessages",
"params": [10, 2, 1, 2],
"pubs": ["inputHash"]
},
"tallyVotes": {
"file": "tallyVotes",
"template": "TallyVotes",
"params": [6, 2, 3],
"pubs": ["inputHash"]
},
"TallyVotes_10-1-2_test": {
"file": "tallyVotes",
"template": "TallyVotes",
Expand Down
6 changes: 4 additions & 2 deletions circuits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"description": "zk-SNARK circuits for MACI",
"main": "build/ts/index.js",
"scripts": {
"build-test-circuits-c": "bash ./scripts/build_intel.sh",
"build-test-circuits-wasm": "bash ./scripts/build_arm.sh",
"build-test-circuits-c": "ts-node ./ts/compile.ts --cWitness",
"build-test-circuits-wasm": "ts-node ./ts/compile.ts",
"gen-zkeys": "ts-node ./ts/genZKeys.ts",
"watch": "tsc --watch",
"build": "tsc -p tsconfig.build.json",
"circom:build": "NODE_OPTIONS=--max-old-space-size=4096 circomkit compile",
Expand Down Expand Up @@ -46,6 +47,7 @@
"chai-as-promised": "^7.1.1",
"mocha": "^10.2.0",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.1",
"typescript": "^5.3.3"
}
}
76 changes: 0 additions & 76 deletions circuits/scripts/buildCustomSnarks.sh

This file was deleted.

16 changes: 0 additions & 16 deletions circuits/scripts/build_arm.sh

This file was deleted.

24 changes: 0 additions & 24 deletions circuits/scripts/build_intel.sh

This file was deleted.

101 changes: 101 additions & 0 deletions circuits/ts/compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { type CircomkitConfig, type CircuitConfig, Circomkit } from "circomkit";

import { execSync } from "child_process";
import fs from "fs";
import path from "path";

import type { CircuitConfigWithName } from "./types";

/**
* Compile MACI's circuits using circomkit
* and setup the dir structure
* @param cWitness - whether to compile to the c witness generator
* or not
* @param outputPath - the path to the output folder
* @returns the build directory
*/
export const compileCircuits = async (cWitness?: boolean, outputPath?: string): Promise<string> => {
// read circomkit config files
const configFilePath = path.resolve(__dirname, "..", "circomkit.json");
const circomKitConfig = JSON.parse(fs.readFileSync(configFilePath, "utf-8")) as CircomkitConfig;
const circuitsConfigPath = path.resolve(__dirname, "..", "circom", "circuits.json");
const circuitsConfigContent = JSON.parse(fs.readFileSync(circuitsConfigPath, "utf-8")) as unknown as Record<
string,
CircuitConfig
>;
const circuitsConfigs: CircuitConfigWithName[] = Object.entries(circuitsConfigContent).map(([name, config]) => ({
name,
...config,
}));

const outputPathUpdated = outputPath ? path.resolve(outputPath) : undefined;
// set the config based on whether to compile the c witness or no
if (cWitness) {
circomKitConfig.cWitness = true;
} else {
circomKitConfig.cWitness = false;
}

// update the build directory if we have an output path
if (outputPathUpdated) {
circomKitConfig.dirBuild = outputPathUpdated;
circomKitConfig.dirPtau = outputPathUpdated;
}

const circomkitInstance = new Circomkit({
...circomKitConfig,
verbose: false,
});

// loop through each circuit config and compile them
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < circuitsConfigs.length; i += 1) {
const circuit = circuitsConfigs[i];
// eslint-disable-next-line no-await-in-loop
const outPath = await circomkitInstance.compile(circuit.name, circuit);

// if the circuit is compiled with a c witness, then let's run make in the directory
if (cWitness) {
try {
// build
execSync(`cd ${outPath}/${circuit.name}_cpp && make`);
// move the binary and dat out
fs.renameSync(`${outPath}/${circuit.name}_cpp/${circuit.name}`, `${outPath}/${circuit.name}`);
fs.renameSync(`${outPath}/${circuit.name}_cpp/${circuit.name}.dat`, `${outPath}/${circuit.name}.dat`);
} catch (error) {
throw new Error(`Failed to compile the c witness for ${circuit.name}`);
}
} else {
// move the wasm out
fs.renameSync(`${outPath}/${circuit.name}_js/${circuit.name}.wasm`, `${outPath}/${circuit.name}.wasm`);
}

// read the content of the directory and move the files to the output folder
// don't do it recursively because we would have moved any useful files
// inside the folder already from the steps above
if (outputPathUpdated) {
const files = fs.readdirSync(outPath, { recursive: false });
files.forEach((file) => {
const fileStr = file.toString();
if (fs.statSync(`${outPath}/${fileStr}`).isFile() && path.extname(`${outPath}/${fileStr}`) !== ".r1cs") {
fs.renameSync(`${outPath}/${fileStr}`, `${outputPathUpdated}/${fileStr}`);
}
});
}
}

return circomKitConfig.dirBuild;
};

if (require.main === module) {
(async () => {
const cWitness = process.argv.includes("--cWitness");
const outputPathIndex = process.argv.indexOf("--outPath");
if (outputPathIndex === -1) {
await compileCircuits(cWitness);
} else {
const outputFolder = process.argv[outputPathIndex + 1];
await compileCircuits(cWitness, outputFolder);
}
})();
}
Loading

0 comments on commit 2fdf055

Please sign in to comment.