Skip to content

Commit

Permalink
feat(proofs): make preferred witness type in circuit's genProof function
Browse files Browse the repository at this point in the history
Remove isArm check in circuits and instead accept a parameter to decide which witness type to use.
Made appropriate changes in tests and cli
  • Loading branch information
ctrlc03 committed Dec 30, 2023
1 parent 4c53b29 commit 148cef5
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion circuits/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { genProof, verifyProof, extractVk } from "./proofs";
export { isArm, cleanThreads } from "./utils";
export { cleanThreads } from "./utils";
30 changes: 18 additions & 12 deletions circuits/ts/proofs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { readFileSync, writeFileSync, unlinkSync, existsSync, mkdirSync, rmdirSync } from "fs";
import path from "path";
import fs from "fs";
import { execSync } from "child_process";
import { tmpdir } from "os";
import { zKey, groth16, FullProveResult, PublicSignals, Groth16Proof, ISnarkJSVerificationKey } from "snarkjs";
import { stringifyBigInts } from "maci-crypto";
import { cleanThreads, isArm } from "./utils";
import { cleanThreads } from "./utils";
import { IGenProofOptions } from "./types";

/**
Expand All @@ -14,6 +14,7 @@ import { IGenProofOptions } from "./types";
* snark and a WASM witness
* @param inputs - the inputs to the circuit
* @param zkeyPath - the path to the zkey
* @param useWasm - whether we want to use the wasm witness or not
* @param rapidsnarkExePath - the path to the rapidnsark binary
* @param witnessExePath - the path to the compiled witness binary
* @param wasmPath - the path to the wasm witness
Expand All @@ -23,20 +24,24 @@ import { IGenProofOptions } from "./types";
export const genProof = async ({
inputs,
zkeyPath,
useWasm,
rapidsnarkExePath,
witnessExePath,
wasmPath,
silent = false,
}: IGenProofOptions): Promise<FullProveResult> => {
// if we are running on an arm chip we can use snarkjs directly
if (isArm()) {
// if we want to use a wasm witness we use snarkjs
if (useWasm) {
if (!wasmPath && !fs.existsSync(wasmPath)) {
throw new Error("wasmPath must be specified and valid");
}
const { proof, publicSignals } = await groth16.fullProve(inputs, wasmPath, zkeyPath);
return { proof, publicSignals };
}
// intel chip flow (use rapidnsark)
// Create tmp directory
const tmpPath = path.resolve(tmpdir(), `tmp-${Date.now()}`);
mkdirSync(tmpPath, { recursive: true });
fs.mkdirSync(tmpPath, { recursive: true });

const inputJsonPath = path.resolve(tmpPath, "input.json");
const outputWtnsPath = path.resolve(tmpPath, "output.wtns");
Expand All @@ -45,14 +50,14 @@ export const genProof = async ({

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

// Generate the witness
const witnessGenCmd = `${witnessExePath} ${inputJsonPath} ${outputWtnsPath}`;

execSync(witnessGenCmd, { stdio: silent ? "ignore" : "pipe" });

if (!existsSync(outputWtnsPath)) {
if (!fs.existsSync(outputWtnsPath)) {
throw new Error("Error executing " + witnessGenCmd);
}

Expand All @@ -61,19 +66,20 @@ export const genProof = async ({

execSync(proofGenCmd, { stdio: silent ? "ignore" : "pipe" });

if (!existsSync(proofJsonPath)) {
if (!fs.existsSync(proofJsonPath)) {
throw new Error("Error executing " + proofGenCmd);
}

// Read the proof and public inputs
const proof = JSON.parse(readFileSync(proofJsonPath).toString()) as Groth16Proof;
const publicSignals = JSON.parse(readFileSync(publicJsonPath).toString()) as PublicSignals;
const proof = JSON.parse(fs.readFileSync(proofJsonPath).toString()) as Groth16Proof;
const publicSignals = JSON.parse(fs.readFileSync(publicJsonPath).toString()) as PublicSignals;

// remove all artifacts
for (const f of [proofJsonPath, publicJsonPath, inputJsonPath, outputWtnsPath]) if (existsSync(f)) unlinkSync(f);
for (const f of [proofJsonPath, publicJsonPath, inputJsonPath, outputWtnsPath])
if (fs.existsSync(f)) fs.unlinkSync(f);

// remove tmp directory
rmdirSync(tmpPath);
fs.rmdirSync(tmpPath);

return { proof, publicSignals };
};
Expand Down
1 change: 1 addition & 0 deletions circuits/ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CircuitInputs } from "maci-core";
export interface IGenProofOptions {
inputs: CircuitInputs;
zkeyPath: string;
useWasm: boolean;
rapidsnarkExePath?: string;
witnessExePath?: string;
wasmPath?: string;
Expand Down
10 changes: 0 additions & 10 deletions circuits/ts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
import { arch } from "os";

/**
* Check if we are running on an arm chip
* @returns whether we are running on an arm chip
*/
export const isArm = (): boolean => {
return arch().includes("arm");
};

/*
* https://github.com/iden3/snarkjs/issues/152
* Need to cleanup the threads to avoid stalling
Expand Down
3 changes: 1 addition & 2 deletions cli/tests/e2e.subsidy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ import {
testTallyVotesWasmPath,
testTallyVotesWitnessPath,
} from "./constants";
import { cleanSubsidy } from "./utils";
import { isArm } from "maci-circuits";
import { cleanSubsidy, isArm } from "./utils";
import { genRandomSalt } from "maci-crypto";
import { DeployedContracts, PollContracts } from "../ts/utils";

Expand Down
3 changes: 1 addition & 2 deletions cli/tests/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ import {
testTallyVotesWasmPath,
testTallyVotesWitnessPath,
} from "./constants";
import { cleanVanilla } from "./utils";
import { isArm } from "maci-circuits";
import { cleanVanilla, isArm } from "./utils";
import { DeployedContracts, PollContracts } from "../ts/utils";
import { Keypair } from "maci-domainobjs";
import { genRandomSalt } from "maci-crypto";
Expand Down
3 changes: 1 addition & 2 deletions cli/tests/keyChange.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isArm } from "maci-circuits";
import {
deploy,
deployPoll,
Expand Down Expand Up @@ -32,7 +31,7 @@ import {
testTallyVotesWitnessPath,
} from "./constants";
import { Keypair } from "maci-domainobjs";
import { cleanVanilla } from "./utils";
import { cleanVanilla, isArm } from "./utils";
import { readFileSync } from "fs";
import { expect } from "chai";
import { genRandomSalt } from "maci-crypto";
Expand Down
14 changes: 12 additions & 2 deletions cli/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { existsSync, readdirSync, rmSync } from "fs";
import { join } from "path";
import { arch } from "os";

import path from "path";

/**
* Test utility to clean up the proofs directory
Expand All @@ -8,7 +10,7 @@ import { join } from "path";
export const cleanVanilla = () => {
const files = readdirSync("./proofs");
for (const file of files) {
rmSync(join("./proofs", file));
rmSync(path.join("./proofs", file));
}
if (existsSync("./tally.json")) rmSync("./tally.json");
};
Expand All @@ -21,3 +23,11 @@ export const cleanSubsidy = () => {
cleanVanilla();
if (existsSync("./subsidy.json")) rmSync("./subsidy.json");
};

/**
* Check if we are running on an arm chip
* @returns whether we are running on an arm chip
*/
export const isArm = (): boolean => {
return arch().includes("arm");
};
3 changes: 3 additions & 0 deletions cli/ts/commands/genProofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export const genProofs = async (
const r = await genProof({
inputs: circuitInputs,
zkeyPath: processZkey,
useWasm,
rapidsnarkExePath: rapidsnark,
witnessExePath: processWitgen,
wasmPath: processWasm,
Expand Down Expand Up @@ -287,6 +288,7 @@ export const genProofs = async (
const r = await genProof({
inputs: subsidyCircuitInputs,
zkeyPath: subsidyZkey,
useWasm,
rapidsnarkExePath: rapidsnark,
witnessExePath: subsidyWitgen,
wasmPath: subsidyWasm,
Expand Down Expand Up @@ -350,6 +352,7 @@ export const genProofs = async (
const r = await genProof({
inputs: tallyCircuitInputs,
zkeyPath: tallyZkey,
useWasm,
rapidsnarkExePath: rapidsnark,
witnessExePath: tallyWitgen,
wasmPath: tallyWasm,
Expand Down

0 comments on commit 148cef5

Please sign in to comment.