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 2091bd2 commit 98eb3f0
Show file tree
Hide file tree
Showing 30 changed files with 475 additions and 526 deletions.
14 changes: 8 additions & 6 deletions .github/scripts/ceremony-param-tests-c-witness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ HARDHAT_CONFIG=./build/hardhat.config.js node build/ts/index.js setVerifyingKeys
--msg-tree-depth 8 \
--vote-option-tree-depth 3 \
--msg-batch-depth 2 \
--process-messages-zkey ./zkeys/processMessages_6-8-2-3.zkey \
--tally-votes-zkey ./zkeys/tallyVotes_6-2-3.zkey \
--process-messages-zkey ./zkeys/processMessages_6-8-2-3/processMessages_6-8-2-3.zkey \
--tally-votes-zkey ./zkeys/tallyVotes_6-2-3/tallyVotes_6-2-3.zkey \
-q true
HARDHAT_CONFIG=./build/hardhat.config.js node build/ts/index.js create -s 6 -q true
HARDHAT_CONFIG=./build/hardhat.config.js node build/ts/index.js deployPoll \
Expand Down Expand Up @@ -55,12 +55,14 @@ HARDHAT_CONFIG=./build/hardhat.config.js node build/ts/index.js genProofs \
--privkey macisk.1751146b59d32e3c0d7426de411218172428263f93b2fc4d981c036047a4d8c0 \
--poll-id 0 \
--rapidsnark ~/rapidsnark/build/prover \
--process-zkey ./zkeys/processMessages_6-8-2-3.zkey \
--tally-zkey ./zkeys/tallyVotes_6-2-3.zkey \
--process-zkey ./zkeys/processMessages_6-8-2-3/processMessages_6-8-2-3.zkey \
--tally-zkey ./zkeys/tallyVotes_6-2-3/tallyVotes_6-2-3.zkey \
--tally-file tally.json \
--output proofs/ \
--tally-witnessgen ./zkeys/tallyVotes_6-2-3 \
--process-witnessgen ./zkeys/processMessages_6-8-2-3 \
--tally-witnessgen ./zkeys/tallyVotes_6-2-3/tallyVotes_6-2-3_cpp/tallyVotes_6-2-3 \
--tally-witnessdat ./zkeys/tallyVotes_6-2-3/tallyVotes_6-2-3_cpp/tallyVotes_6-2-3.dat \
--process-witnessgen ./zkeys/processMessages_6-8-2-3/processMessages_6-8-2-3_cpp/processMessages_6-8-2-3 \
--process-witnessdat ./zkeys/processMessages_6-8-2-3/processMessages_6-8-2-3_cpp/processMessages_6-8-2-3.dat \
-q true
HARDHAT_CONFIG=./build/hardhat.config.js node build/ts/index.js proveOnChain \
--poll-id 0 \
Expand Down
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.

109 changes: 109 additions & 0 deletions circuits/ts/compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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,
}));

// generate the absolute path to the output folder
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;
}

// create an instance of circomkit with a custom config
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`);
} catch (error) {
throw new Error(`Failed to compile the c witness for ${circuit.name}`);
}
}

// // 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) {
// // list all the files inside the directory
// const files = fs.readdirSync(outPath, { recursive: false });
// // loop through each file in the dir
// files.forEach((file) => {
// // convert to string from potential Buffer
// const fileStr = file.toString();
// // get the source and destination paths
// const sourceFile = path.resolve(outPath, fileStr);
// const destFile = path.resolve(outputPathUpdated, fileStr);

// // we only care about files and not about the r1cs file
// if (path.extname(sourceFile) !== ".r1cs") {
// fs.renameSync(sourceFile, destFile);
// }
// });
// }
}

// return the build directory
return circomKitConfig.dirBuild;
};

if (require.main === module) {
(async () => {
// check if we want to compile the c witness or not
const cWitness = process.argv.includes("--cWitness");
// the output path is the next argument after the --outPath flag
// and is not mandatory
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 98eb3f0

Please sign in to comment.