Skip to content

Commit

Permalink
Remove reformatting
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Zhang <jim.zhang@kaleido.io>
  • Loading branch information
jimthematrix committed Aug 29, 2024
1 parent 80bdc6a commit f670da7
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions zkp/circuits/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ const processCircuit = async (circuit, ptau, skipSolidityGenaration) => {
if (!compileOnly && !fs.existsSync(ptauFile)) {
log(circuit, `PTAU file does not exist, downloading: ${ptauFile}`);
try {
const response = await axios.get(`https://storage.googleapis.com/zkevm/ptau/${ptau}.ptau`, {
responseType: 'stream',
});
const response = await axios.get(
`https://storage.googleapis.com/zkevm/ptau/${ptau}.ptau`,
{
responseType: 'stream',
}
);
response.data.pipe(fs.createWriteStream(ptauFile));
await new Promise((resolve, reject) => {
response.data.on('end', resolve);
Expand All @@ -98,34 +101,58 @@ const processCircuit = async (circuit, ptau, skipSolidityGenaration) => {
}

log(circuit, `Compiling circuit`);
await execAsync(`circom ${circomInput} --output ${circuitsRoot} --sym --wasm`);
await execAsync(
`circom ${circomInput} --output ${circuitsRoot} --sym --wasm`
);
if (compileOnly) {
return;
}

await execAsync(`circom ${circomInput} --output ${provingKeysRoot} --r1cs`);

log(circuit, `Generating test proving key with ${ptau}`);
await execAsync(`npx snarkjs groth16 setup ${path.join(provingKeysRoot, `${circuit}.r1cs`)} ${ptauFile} ${zkeyOutput}`);
await execAsync(
`npx snarkjs groth16 setup ${path.join(
provingKeysRoot,
`${circuit}.r1cs`
)} ${ptauFile} ${zkeyOutput}`
);

log(circuit, `Generating verification key`);
await execAsync(`npx snarkjs zkey export verificationkey ${zkeyOutput} ${path.join(provingKeysRoot, `${circuit}-vkey.json`)}`);
await execAsync(
`npx snarkjs zkey export verificationkey ${zkeyOutput} ${path.join(
provingKeysRoot,
`${circuit}-vkey.json`
)}`
);

if (skipSolidityGenaration) {
log(circuit, `Skipping solidity verifier generation`);
return;
}

log(circuit, `Generating solidity verifier`);
const solidityFile = path.join('..', '..', 'solidity', 'contracts', 'lib', `verifier_${circuit}.sol`);
await execAsync(`npx snarkjs zkey export solidityverifier ${zkeyOutput} ${solidityFile}`);
const solidityFile = path.join(
'..',
'..',
'solidity',
'contracts',
'lib',
`verifier_${circuit}.sol`
);
await execAsync(
`npx snarkjs zkey export solidityverifier ${zkeyOutput} ${solidityFile}`
);

log(circuit, `Modifying the contract name in the Solidity file`);
const camelCaseCircuitName = toCamelCase(circuit);
const solidityFileTmp = `${solidityFile}.tmp`;

const fileContent = fs.readFileSync(solidityFile, 'utf8');
const updatedContent = fileContent.replace(' Groth16Verifier ', ` Groth16Verifier_${camelCaseCircuitName} `);
const updatedContent = fileContent.replace(
' Groth16Verifier ',
` Groth16Verifier_${camelCaseCircuitName} `
);
fs.writeFileSync(solidityFileTmp, updatedContent);
fs.renameSync(solidityFileTmp, solidityFile);
};
Expand Down

0 comments on commit f670da7

Please sign in to comment.