Skip to content

Commit

Permalink
chore: use async fs instead of sync version
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmad committed Aug 5, 2024
1 parent b865a21 commit fb8317e
Show file tree
Hide file tree
Showing 26 changed files with 269 additions and 204 deletions.
4 changes: 3 additions & 1 deletion .github/scripts/downloadZkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export async function downloadZkeys(): Promise<void> {
throw new Error(`${type} doesn't exist`);
}

if (!fs.existsSync(ZKEY_PATH)) {
const isExists = fs.existsSync(ZKEY_PATH);

if (!isExists) {
await fs.promises.mkdir(ZKEY_PATH);
}

Expand Down
17 changes: 10 additions & 7 deletions circuits/ts/compile.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { type CircomkitConfig, type CircuitConfig, Circomkit } from "circomkit";

import { execFileSync } from "child_process";
import childProcess from "child_process";
import fs from "fs";
import path from "path";
import { promisify } from "util";

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

const execFile = promisify(childProcess.execFile);

/**
* Compile MACI's circuits using circomkit
* and setup the dir structure
Expand All @@ -17,12 +20,11 @@ import type { CircuitConfigWithName } from "./types";
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 circomKitConfig = JSON.parse(await fs.promises.readFile(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 circuitsConfigContent = JSON.parse(
await fs.promises.readFile(circuitsConfigPath, "utf-8"),
) as unknown as Record<string, CircuitConfig>;
const circuitsConfigs: CircuitConfigWithName[] = Object.entries(circuitsConfigContent).map(([name, config]) => ({
name,
...config,
Expand Down Expand Up @@ -63,7 +65,8 @@ export const compileCircuits = async (cWitness?: boolean, outputPath?: string):
if (cWitness) {
try {
// build
execFileSync("bash", ["-c", `cd ${outPath}/${circuit.name}_cpp && make`]);
// eslint-disable-next-line no-await-in-loop
await execFile("bash", ["-c", `cd ${outPath}/${circuit.name}_cpp && make`]);
} catch (error) {
throw new Error(`Failed to compile the c witness for ${circuit.name}`);
}
Expand Down
12 changes: 6 additions & 6 deletions circuits/ts/genZkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import { cleanThreads } from "./utils";
export const generateZkeys = async (outputPath?: string): Promise<void> => {
// read circomkit config files
const configFilePath = path.resolve(__dirname, "..", "circomkit.json");
const circomKitConfig = JSON.parse(fs.readFileSync(configFilePath, "utf-8")) as CircomkitConfig;
const circomKitConfig = JSON.parse(await fs.promises.readFile(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 circuitsConfigContent = JSON.parse(
await fs.promises.readFile(circuitsConfigPath, "utf-8"),
) as unknown as Record<string, CircuitConfig>;
const circuitsConfigs: CircuitConfigWithName[] = Object.entries(circuitsConfigContent).map(([name, config]) => ({
name,
...config,
Expand Down Expand Up @@ -52,7 +51,8 @@ export const generateZkeys = async (outputPath?: string): Promise<void> => {
const { proverKeyPath } = await circomkitInstance.setup(circuit.name);
// rename the zkey
const zkeyPath = path.resolve(circomKitConfig.dirBuild, circuit.name, `${circuit.name}.0.zkey`);
fs.renameSync(proverKeyPath, zkeyPath);
// eslint-disable-next-line no-await-in-loop
await fs.promises.rename(proverKeyPath, zkeyPath);
}

// clean up the threads so we can exit
Expand Down
38 changes: 25 additions & 13 deletions circuits/ts/proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export const genProof = async ({
throw new Error("wasmPath must be specified");
}

if (!fs.existsSync(wasmPath)) {
const isWasmExists = fs.existsSync(wasmPath);

if (!isWasmExists) {
throw new Error(`wasmPath ${wasmPath} does not exist`);
}

Expand All @@ -57,7 +59,7 @@ export const genProof = async ({
// intel chip flow (use rapidnsark)
// Create tmp directory
const tmpPath = path.resolve(tmpdir(), `tmp-${Date.now()}`);
fs.mkdirSync(tmpPath, { recursive: true });
await fs.promises.mkdir(tmpPath, { recursive: true });

const inputJsonPath = path.resolve(tmpPath, "input.json");
const outputWtnsPath = path.resolve(tmpPath, "output.wtns");
Expand All @@ -66,41 +68,51 @@ export const genProof = async ({

// Write input.json
const jsonData = JSON.stringify(stringifyBigInts(inputs));
fs.writeFileSync(inputJsonPath, jsonData);
await fs.promises.writeFile(inputJsonPath, jsonData);

// Generate the witness
await execFile(witnessExePath!, [inputJsonPath, outputWtnsPath]);

if (!fs.existsSync(outputWtnsPath)) {
const isOutputWtnsExists = fs.existsSync(outputWtnsPath);

if (!isOutputWtnsExists) {
throw new Error(`Error executing ${witnessExePath} ${inputJsonPath} ${outputWtnsPath}`);
}

// Generate the proof
await execFile(rapidsnarkExePath!, [zkeyPath, outputWtnsPath, proofJsonPath, publicJsonPath]);

if (!fs.existsSync(proofJsonPath)) {
const isProofJsonPathExists = fs.existsSync(proofJsonPath);

if (!isProofJsonPathExists) {
throw new Error(
`Error executing ${rapidsnarkExePath} ${zkeyPath} ${outputWtnsPath} ${proofJsonPath} ${publicJsonPath}`,
);
}

// Read the proof and public inputs
const proof = JSON.parse(fs.readFileSync(proofJsonPath).toString()) as Groth16Proof;
const publicSignals = JSON.parse(fs.readFileSync(publicJsonPath).toString()) as PublicSignals;
const proof = JSON.parse(await fs.promises.readFile(proofJsonPath).then((res) => res.toString())) as Groth16Proof;
const publicSignals = JSON.parse(
await fs.promises.readFile(publicJsonPath).then((res) => res.toString()),
) as PublicSignals;

// remove all artifacts
[proofJsonPath, publicJsonPath, inputJsonPath, outputWtnsPath].forEach((f) => {
if (fs.existsSync(f)) {
fs.unlinkSync(f);
}
});
await Promise.all([proofJsonPath, publicJsonPath, inputJsonPath, outputWtnsPath].map(unlinkFile));

// remove tmp directory
fs.rmdirSync(tmpPath);
await fs.promises.rmdir(tmpPath);

return { proof, publicSignals };
};

async function unlinkFile(filepath: string): Promise<void> {
const isFileExists = fs.existsSync(filepath);

if (isFileExists) {
await fs.promises.unlink(filepath);
}
}

/**
* Verify a zk-SNARK proof using snarkjs
* @param publicInputs - the public inputs to the circuit
Expand Down
24 changes: 12 additions & 12 deletions cli/tests/ceremony-params/ceremonyParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ describe("Stress tests with ceremony params (6,9,2,3)", function test() {
});

describe("1 user, 2 messages", () => {
after(() => {
clean();
after(async () => {
await clean();
});

before(async () => {
Expand Down Expand Up @@ -165,13 +165,13 @@ describe("Stress tests with ceremony params (6,9,2,3)", function test() {
await mergeSignups({ ...mergeSignupsArgs, signer });
await genProofs({ ...genProofsCeremonyArgs, signer });
await proveOnChain({ ...proveOnChainArgs, signer });
await verify({ ...verifyArgs(), signer });
await verify({ ...(await verifyArgs()), signer });
});
});

describe("25 signups, 100 messages", () => {
after(() => {
clean();
after(async () => {
await clean();
});

before(async () => {
Expand Down Expand Up @@ -216,7 +216,7 @@ describe("Stress tests with ceremony params (6,9,2,3)", function test() {
await mergeSignups({ ...mergeSignupsArgs, signer });
await genProofs({ ...genProofsCeremonyArgs, signer });
await proveOnChain({ ...proveOnChainArgs, signer });
await verify({ ...verifyArgs(), signer });
await verify({ ...(await verifyArgs()), signer });
});
});
});
Expand Down Expand Up @@ -251,8 +251,8 @@ describe("Stress tests with ceremony params (6,9,2,3)", function test() {
});

describe("1 signup, 1 message", () => {
after(() => {
clean();
after(async () => {
await clean();
});

before(async () => {
Expand Down Expand Up @@ -288,7 +288,7 @@ describe("Stress tests with ceremony params (6,9,2,3)", function test() {
const tallyFileData = await genProofs({ ...genProofsArgs, signer, useQuadraticVoting: false });
await proveOnChain({ ...proveOnChainArgs, signer });
await verify({
...verifyArgs(),
...(await verifyArgs()),
tallyData: tallyFileData,
maciAddress: tallyFileData.maci,
signer,
Expand All @@ -297,8 +297,8 @@ describe("Stress tests with ceremony params (6,9,2,3)", function test() {
});

describe("25 signups, 100 messages", () => {
after(() => {
clean();
after(async () => {
await clean();
});

before(async () => {
Expand Down Expand Up @@ -344,7 +344,7 @@ describe("Stress tests with ceremony params (6,9,2,3)", function test() {
const tallyFileData = await genProofs({ ...genProofsArgs, signer, useQuadraticVoting: false });
await proveOnChain({ ...proveOnChainArgs, signer });
await verify({
...verifyArgs(),
...(await verifyArgs()),
tallyData: tallyFileData,
maciAddress: tallyFileData.maci,
signer,
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ export const proveOnChainArgs: Omit<ProveOnChainArgs, "signer"> = {
proofDir: testProofsDirPath,
};

export const verifyArgs = (): Omit<VerifyArgs, "signer"> => {
const tallyData = readJSONFile(testTallyFilePath) as unknown as TallyData;
export const verifyArgs = async (): Promise<Omit<VerifyArgs, "signer">> => {
const tallyData = (await readJSONFile(testTallyFilePath)) as unknown as TallyData;

return {
pollId: 0n,
Expand Down
6 changes: 3 additions & 3 deletions cli/tests/e2e/e2e.nonQv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ describe("e2e tests with non quadratic voting", function test() {
});

describe("1 signup, 1 message", () => {
after(() => {
clean();
after(async () => {
await clean();
});

const user = new Keypair();
Expand Down Expand Up @@ -122,7 +122,7 @@ describe("e2e tests with non quadratic voting", function test() {
const tallyFileData = await genProofs({ ...genProofsArgs, signer, useQuadraticVoting: false });
await proveOnChain({ ...proveOnChainArgs, signer });
await verify({
...verifyArgs(),
...(await verifyArgs()),
tallyData: tallyFileData,
maciAddress: tallyFileData.maci,
signer,
Expand Down
Loading

0 comments on commit fb8317e

Please sign in to comment.