Skip to content

Commit

Permalink
fix(compile): handled error and await bug edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepyqadir committed Sep 19, 2022
1 parent 75fe071 commit 1ff4d86
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 178 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
yarn.lock

# Dists
/dist
Expand Down
137 changes: 77 additions & 60 deletions src/commands/actions/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@ import fsExtra from "fs-extra";
// @ts-ignore because they don't ship types
import { CircomRunner, bindings } from "circom2";
import { log } from "../../../utils/logger";
import {
groth16Setup,
groth16Contribute,
verificationFile,
solidityVerifier,
downloadPtauFile,
plonkSetup,
} from "../../../utils/snarkjs";
import { WrappedSnarkJs } from "../../../utils/snarkjs";
import { initFS } from "../../../utils/wasm";
import * as fs from "fs/promises";
import {
bumpSolidityVersion,
fileExists,
getEmptyDir,
getEmptyDirByPath,
} from "../../../utils/utils";
Expand Down Expand Up @@ -214,71 +208,94 @@ export const compile = async (options: any) => {

await getEmptyDirByPath(`${outputBasePath}/${circuitName}`, 0);

const spinner = ora(
chalk.greenBright(`Compiling ${circuitName}\n`)
).start();
if (await fileExists(circuitPath)) {
const spinner = ora(
chalk.greenBright(`Compiling ${circuitName}\n`)
).start();

const circom = new CircomRunner({
args: [
circuitPath,
"--r1cs",
"--wat",
"--wasm",
"--sym",
"-o",
`${outputBasePath}/${circuitName}`,
],
env: {},
preopens: {
"/": "/",
},
bindings: {
...bindings,
fs: wasmFs,
},
returnOnExit: true,
});
const circom = new CircomRunner({
args: [
circuitPath,
"--r1cs",
"--wat",
"--wasm",
"--sym",
"-o",
`${outputBasePath}/${circuitName}`,
],
env: {},
preopens: {
"/": "/",
},
bindings: {
...bindings,
fs: wasmFs,
},
returnOnExit: true,
});

const circomWasm = await fs.readFile(wasmPath);
const circomWasm = await fs.readFile(wasmPath);

try {
await circom.execute(circomWasm);
} catch (err) {
if (`${err}` !== "RuntimeError: unreachable") {
log(`${err}`, "error");
try {
// step 1:
await circom.execute(circomWasm);
} catch (err) {
if (`${err}` !== "RuntimeError: unreachable") {
log(`${err}`, "error");
}
}
}

await downloadPtauFile(ptauPath, finalConfig.circom.ptau);
// step 2:
await WrappedSnarkJs.util.downloadPtau(
ptauPath,
finalConfig.circom.ptau
);

if (finalConfig.circom.circuits[i].protocol === "groth16") {
await groth16Setup(r1csPath, ptauPath, zKeyPath.zero);
if (finalConfig.circom.circuits[i].protocol === "groth16") {
// step 3 (b):
await WrappedSnarkJs.groth16.setup(r1csPath, ptauPath, zKeyPath.zero);

const contribution = contributions[finalConfig.circom.circuits[i].name];
const contribution =
contributions[finalConfig.circom.circuits[i].name];

// step 4:
await WrappedSnarkJs.groth16.contribute(
zKeyPath.zero,
zKeyPath.final,
contribution ? contribution.contributerName : "",
contribution ? contribution.randomEntropy : ""
);
} else {
// step 3 (b):
await WrappedSnarkJs.plonk.setup(r1csPath, ptauPath, zKeyPath.final);
}

await groth16Contribute(
zKeyPath.zero,
// step 5:
await WrappedSnarkJs.util.generateVkey(zKeyPath.final, vKeyPath);

// step 6:
await WrappedSnarkJs.util.generateSolidityVerifier(
zKeyPath.final,
contribution ? contribution.contributerName : "",
contribution ? contribution.randomEntropy : ""
solVerifierPath
);

// step 7:
await bumpSolidityVersion(
finalConfig.solidity ? finalConfig.solidity : "^0.8.0",
circuitName,
finalConfig.circom.circuits[i].protocol as string
);
spinner.succeed(
chalk.greenBright(`${circuitName} succesfully compiled.`)
);
} else {
await plonkSetup(r1csPath, ptauPath, zKeyPath.final);
log(
`unable to locate ${finalConfig.circom.circuits[i].circuit} at dir ${circuitPath} user defined config in file shield.config.js.`,
"error"
);
}

await verificationFile(zKeyPath.final, vKeyPath);

solidityVerifier(zKeyPath.final, solVerifierPath);

bumpSolidityVersion(
finalConfig.solidity ? finalConfig.solidity : "^0.8.0",
circuitName,
finalConfig.circom.circuits[i].protocol as string
);
spinner.succeed(
chalk.greenBright(`${circuitName} succesfully compiled.`)
);
}
process.exit(0);
} catch (e: any) {
log(e.message, "error");
}
Expand Down
73 changes: 0 additions & 73 deletions src/commands/scripts/compile.sh

This file was deleted.

103 changes: 59 additions & 44 deletions utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,32 @@ export const getEmptyDirByPath = async (path: string, exitCode: number) => {
if (exitCode === 1) {
process.exit(1);
}
return
return;
}
log(`✓ "${path}" dir created`, "success");
await fsExtra.emptyDir(path);
return path;
};

export const getEmptyDir = async (name: string, exitCode: number) => {
const tmpDir = path.join(process.cwd(), `/${name}`);
const dir = await fsExtra.ensureDir(tmpDir);
if (dir === undefined) {
log(
`A folder named "${name}" already exist, delete or move it to somewhere else and try again!!`,
"error"
);
if (exitCode === 1) {
process.exit(1);
try {
const tmpDir = path.join(process.cwd(), `/${name}`);
const dir = await fsExtra.ensureDir(tmpDir);
if (dir === undefined) {
log(
`A folder named "${name}" already exist, delete or move it to somewhere else and try again!!`,
"error"
);
if (exitCode === 1) {
process.exit(1);
}
}
await fsExtra.emptyDir(tmpDir);
return tmpDir;
} catch (error) {
log(`${error}`, "error");
throw error;
}
await fsExtra.emptyDir(tmpDir);
return tmpDir;
};

export const fileExists = async (file: fsExtra.PathLike) => {
Expand All @@ -94,7 +99,7 @@ export const updateCopyProjectName = async (
JSON.stringify(packageJson, null, 3)
);
return res;
} catch (e) {
} catch (error) {
log(
"unable to locate the package.json file or rewrite the project name",
"error"
Expand All @@ -109,37 +114,46 @@ export const createInterface = async (
content: string,
SOLIDITY_VERSION: string
) => {
const tmpDir = path.join(process.cwd(), `/contracts/interfaces`);
await fsExtra.ensureDir(tmpDir);

fsExtra.createFileSync(`./contracts/interfaces/I${CIRCUIT_NAME}Verifier.sol`);

let inputVariable = "";
let interfaceBumped = "";
if (protocol === "groth16") {
inputVariable = content.split("uint[2] memory c,")[1].split(")")[0].trim();
interfaceBumped = groth16InterfaceContent(
inputVariable,
CIRCUIT_NAME,
SOLIDITY_VERSION
try {
const tmpDir = path.join(process.cwd(), `/contracts/interfaces`);
await fsExtra.ensureDir(tmpDir);
await fsExtra.createFileSync(
`./contracts/interfaces/I${CIRCUIT_NAME}Verifier.sol`
);
} else {
inputVariable = content
.split("bytes memory proof,")[1]
.split(")")[0]
.trim();

interfaceBumped = plonkInterfaceContent(
inputVariable,
CIRCUIT_NAME,
SOLIDITY_VERSION

let inputVariable = "";
let interfaceBumped = "";
if (protocol === "groth16") {
inputVariable = content
.split("uint[2] memory c,")[1]
.split(")")[0]
.trim();

interfaceBumped = groth16InterfaceContent(
inputVariable,
CIRCUIT_NAME,
SOLIDITY_VERSION
);
} else {
inputVariable = content
.split("bytes memory proof,")[1]
.split(")")[0]
.trim();
interfaceBumped = plonkInterfaceContent(
inputVariable,
CIRCUIT_NAME,
SOLIDITY_VERSION
);
}

await fsExtra.writeFileSync(
`./contracts/interfaces/I${CIRCUIT_NAME}Verifier.sol`,
interfaceBumped
);
} catch (error) {
log(`${error}`, "error");
throw error;
}

fsExtra.writeFileSync(
`./contracts/interfaces/I${CIRCUIT_NAME}Verifier.sol`,
interfaceBumped
);
};

export const bumpSolidityVersion = async (
Expand All @@ -157,7 +171,7 @@ export const bumpSolidityVersion = async (
}
);

createInterface(CIRCUIT_NAME, PROTOCOL, content, SOLIDITY_VERSION);
await createInterface(CIRCUIT_NAME, PROTOCOL, content, SOLIDITY_VERSION);

const bumped = content.replace(
solidityRegex,
Expand All @@ -182,7 +196,8 @@ export const bumpSolidityVersion = async (
`./contracts/${CIRCUIT_NAME}_Verifier.sol`,
bumpedContractName
);
} catch (e) {
throw e;
} catch (error) {
log(`${error}`, "error");
throw error;
}
};
Loading

0 comments on commit 1ff4d86

Please sign in to comment.