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 Jan 3, 2024
1 parent ea9903a commit 8618acf
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 12 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";
21 changes: 18 additions & 3 deletions circuits/ts/proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { cleanThreads, isArm } from "./utils";
* 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 @@ -25,16 +26,30 @@ import { cleanThreads, isArm } from "./utils";
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()) {
const { proof, publicSignals } = await groth16.fullProve(inputs, wasmPath!, zkeyPath);
// if we want to use a wasm witness we use snarkjs
if (useWasm) {
if (!wasmPath) {
throw new Error("wasmPath must be specified");
}

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

const { proof, publicSignals } = await groth16.fullProve(inputs, wasmPath, zkeyPath);
return { proof, publicSignals };
}

if (isArm()) {
throw new Error("To use rapidnsnark you currently need to be running on an intel chip");
}

// intel chip flow (use rapidnsark)
// Create tmp directory
const tmpPath = path.resolve(tmpdir(), `tmp-${Date.now()}`);
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 type { CircuitInputs } from "maci-core";
export interface IGenProofOptions {
inputs: CircuitInputs;
zkeyPath: string;
useWasm?: boolean;
rapidsnarkExePath?: string;
witnessExePath?: string;
wasmPath?: string;
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 8618acf

Please sign in to comment.