From 23590e2b755bedb711faf0c2e4a184d9f1a62e18 Mon Sep 17 00:00:00 2001 From: Chengxuan Xing Date: Sat, 7 Dec 2024 06:45:56 +0000 Subject: [PATCH] version check for circuit tooling Signed-off-by: Chengxuan Xing --- zkp/circuits/gen-config.json | 1 + zkp/circuits/gen.js | 61 +++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/zkp/circuits/gen-config.json b/zkp/circuits/gen-config.json index e2de29f..84200d9 100644 --- a/zkp/circuits/gen-config.json +++ b/zkp/circuits/gen-config.json @@ -1,4 +1,5 @@ { + "circomVersion": "2.1.9", "anon": { "ptau": "powersOfTau28_hez_final_12", "batchPtau": "powersOfTau28_hez_final_14", diff --git a/zkp/circuits/gen.js b/zkp/circuits/gen.js index d15b714..f727f72 100644 --- a/zkp/circuits/gen.js +++ b/zkp/circuits/gen.js @@ -81,9 +81,9 @@ console.log( "\n", ); -// load circuits +// load genConfig -const circuits = require("./gen-config.json"); +const genConfig = require("./gen-config.json"); const toCamelCase = (str) => { return str @@ -243,14 +243,14 @@ const run = async () => { // if specific circuits are provided, check it's in the map for (const circuit of onlyCircuits) { - if (!circuits[circuit]) { + if (!genConfig[circuit]) { console.error(`Error: Unknown circuit: ${circuit}`); process.exit(1); } } } - const circuitsArray = Object.entries(circuits); + const circuitsArray = Object.entries(genConfig); const activePromises = new Set(); for (const [ @@ -261,6 +261,59 @@ const run = async () => { continue; } + let snarkjsVersion; + // first check cirom version and snarkjs version matches the one in the package.json + try { + const { stdout: circomVersion } = await execAsync("circom --version"); + // Trigger error to get snarkjs version + try { + await execAsync("npx snarkjs --version"); + } catch (error) { + // Extract snarkjs version from error message + snarkjsVersion = error.stdout.match(/snarkjs@([\d.]+)/)?.[1]; + if (!snarkjsVersion) { + throw new Error( + "Failed to extract SnarkJS version from error output.", + ); + } + } + const { stdout: packageJson } = await execAsync("cat package.json"); + + const packageJsonObj = JSON.parse(packageJson); + const expectedCircomVersion = genConfig.circomVersion; + + // Sanitize and extract version numbers + const circomVersionTrimmed = circomVersion + .trim() + .replace("circom compiler ", ""); + let hasMismatch = false; + if (circomVersionTrimmed !== expectedCircomVersion) { + console.error( + `Error: circom version mismatch with the version in gen-config.json :\n` + + `\tExpected circom: ${expectedCircomVersion}, got: ${circomVersionTrimmed}\n` + + `\tFollow https://docs.circom.io/getting-started/installation/ to update your circom\n`, + ); + hasMismatch = true; + } + + if (snarkjsVersion !== packageJsonObj.devDependencies.snarkjs) { + console.error( + `Error: snarkjs version mismatch with package.json:\n` + + `\tExpected snarkjs: ${packageJsonObj.devDependencies.snarkjs}, got: ${snarkjsVersion}\n` + + `\tUse npm to update your snarkjs node module\n`, + ); + hasMismatch = true; + } + if (hasMismatch) { + process.exit(1); + } + + console.log("Version check passed."); + } catch (error) { + console.error(`An error occurred: ${error.message}`); + process.exit(1); + } + const pcPromise = processCircuit(circuit, ptau, skipSolidityGenaration); activePromises.add(pcPromise);