Skip to content

Commit

Permalink
some readme update
Browse files Browse the repository at this point in the history
Signed-off-by: Chengxuan Xing <[email protected]>
  • Loading branch information
Chengxuan committed Aug 14, 2024
1 parent 0ac6133 commit 1f80f1f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
62 changes: 53 additions & 9 deletions zkp/circuits/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ if (!compileOnly && !ptauDownload) {
process.exit(1);
}

console.log(
'Generating circuits with the following settings:\n' +
JSON.stringify(
{
specificCircuits,
compileOnly,
parallelLimit,
circuitsRoot,
provingKeysRoot,
ptauDownload,
},
null,
2
) +
'\n'
);

// load circuits

const circuits = require('./gen-config.json');
Expand Down Expand Up @@ -66,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 @@ -81,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(`snarkjs groth16 setup ${path.join(provingKeysRoot, `${circuit}.r1cs`)} ${ptauFile} ${zkeyOutput}`);
await execAsync(
`snarkjs groth16 setup ${path.join(
provingKeysRoot,
`${circuit}.r1cs`
)} ${ptauFile} ${zkeyOutput}`
);

log(circuit, `Generating verification key`);
await execAsync(`snarkjs zkey export verificationkey ${zkeyOutput} ${path.join(provingKeysRoot, `${circuit}-vkey.json`)}`);
await execAsync(
`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(`snarkjs zkey export solidityverifier ${zkeyOutput} ${solidityFile}`);
const solidityFile = path.join(
'..',
'..',
'solidity',
'contracts',
'lib',
`verifier_${circuit}.sol`
);
await execAsync(
`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
4 changes: 2 additions & 2 deletions zkp/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ npm i
export CIRCUITS_ROOT="$HOME/circuits"
export PROVING_KEYS_ROOT="$HOME/proving-keys"
export PTAU_DOWNLOAD_PATH="$HOME/Downloads"
mkdir -p $PROVING_KEYS_ROOT $PTAU_DOWNLOAD_PATH
mkdir -p $PROVING_KEYS_ROOT $PTAU_DOWNLOAD_PATH $CIRCUITS_ROOT
```
- run the generation script for **ALL** circuits
```console
npm run gen
```
**run `npm run gen $circuit` for developing a single circuit**
**run `npm run gen -- -c $circuit` for developing a single circuit**
**use `GEN_CONCURRENCY` to control how many circuits to be processed in parallel, default to 10**

> Refer to [generation script explanation](#generation-script-explanation) for what the script does
Expand Down

0 comments on commit 1f80f1f

Please sign in to comment.