From 89cad03f62fbf761eb6ee76f346e70f4968a4dec Mon Sep 17 00:00:00 2001 From: mllwchrry Date: Tue, 29 Oct 2024 12:46:46 +0200 Subject: [PATCH 1/6] upgraded snarkjs version, added termination fix --- package-lock.json | 242 ++++++++++++++++++++++++++----- package.json | 2 +- src/constants.ts | 2 + src/core/setup/SetupProcessor.ts | 9 ++ src/utils/utils.ts | 8 + 5 files changed, 224 insertions(+), 39 deletions(-) diff --git a/package-lock.json b/package-lock.json index 121ca36..eeb5ecd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,7 +26,7 @@ "path-browserify": "1.0.1", "resolve": "1.22.8", "semver": "7.6.3", - "snarkjs": "0.7.3", + "snarkjs": "0.7.5", "uuid": "9.0.1", "zod": "3.23.8" }, @@ -1032,12 +1032,12 @@ "integrity": "sha512-Xzdyxqm1bOFF6pdIsiHLLl3HkSLjbhqJHVyqaTxXt3RqXBEnmsUmEW47H7VOi/ak7TdkRpNkxjyK5Zbkm+y52g==" }, "node_modules/@iden3/binfileutils": { - "version": "0.0.11", - "resolved": "https://registry.npmjs.org/@iden3/binfileutils/-/binfileutils-0.0.11.tgz", - "integrity": "sha512-LylnJoZ0CTdgErnKY8OxohvW4K+p6UHD3sxt+3P9AmMyBQjYR4IpoqoYZZ+9aMj89cmCQ21UvdhndAx04er3NA==", + "version": "0.0.12", + "resolved": "https://registry.npmjs.org/@iden3/binfileutils/-/binfileutils-0.0.12.tgz", + "integrity": "sha512-naAmzuDufRIcoNfQ1d99d7hGHufLA3wZSibtr4dMe6ZeiOPV1KwOZWTJ1YVz4HbaWlpDuzVU72dS4ATQS4PXBQ==", "dependencies": { "fastfile": "0.0.20", - "ffjavascript": "^0.2.48" + "ffjavascript": "^0.3.0" } }, "node_modules/@istanbuljs/load-nyc-config": { @@ -2174,6 +2174,101 @@ "ejs": "3.1.10" } }, + "node_modules/@solarity/hardhat-zkit/node_modules/@iden3/binfileutils": { + "version": "0.0.11", + "resolved": "https://registry.npmjs.org/@iden3/binfileutils/-/binfileutils-0.0.11.tgz", + "integrity": "sha512-LylnJoZ0CTdgErnKY8OxohvW4K+p6UHD3sxt+3P9AmMyBQjYR4IpoqoYZZ+9aMj89cmCQ21UvdhndAx04er3NA==", + "dev": true, + "peer": true, + "dependencies": { + "fastfile": "0.0.20", + "ffjavascript": "^0.2.48" + } + }, + "node_modules/@solarity/hardhat-zkit/node_modules/circom_runtime": { + "version": "0.1.24", + "resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.24.tgz", + "integrity": "sha512-H7/7I2J/cBmRnZm9docOCGhfxzS61BEm4TMCWcrZGsWNBQhePNfQq88Oj2XpUfzmBTCd8pRvRb3Mvazt3TMrJw==", + "dev": true, + "peer": true, + "dependencies": { + "ffjavascript": "0.2.60" + }, + "bin": { + "calcwit": "calcwit.js" + } + }, + "node_modules/@solarity/hardhat-zkit/node_modules/circom_runtime/node_modules/ffjavascript": { + "version": "0.2.60", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.60.tgz", + "integrity": "sha512-T/9bnEL5xAZRDbQoEMf+pM9nrhK+C3JyZNmqiWub26EQorW7Jt+jR54gpqDhceA4Nj0YctPQwYnl8xa52/A26A==", + "dev": true, + "peer": true, + "dependencies": { + "wasmbuilder": "0.0.16", + "wasmcurves": "0.2.2", + "web-worker": "^1.2.0" + } + }, + "node_modules/@solarity/hardhat-zkit/node_modules/ffjavascript": { + "version": "0.2.63", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.63.tgz", + "integrity": "sha512-dBgdsfGks58b66JnUZeZpGxdMIDQ4QsD3VYlRJyFVrKQHb2kJy4R2gufx5oetrTxXPT+aEjg0dOvOLg1N0on4A==", + "dev": true, + "peer": true, + "dependencies": { + "wasmbuilder": "0.0.16", + "wasmcurves": "0.2.2", + "web-worker": "1.2.0" + } + }, + "node_modules/@solarity/hardhat-zkit/node_modules/r1csfile": { + "version": "0.0.47", + "resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.47.tgz", + "integrity": "sha512-oI4mAwuh1WwuFg95eJDNDDL8hCaZkwnPuNZrQdLBWvDoRU7EG+L/MOHL7SwPW2Y+ZuYcTLpj3rBkgllBQZN/JA==", + "dev": true, + "peer": true, + "dependencies": { + "@iden3/bigarray": "0.0.2", + "@iden3/binfileutils": "0.0.11", + "fastfile": "0.0.20", + "ffjavascript": "0.2.60" + } + }, + "node_modules/@solarity/hardhat-zkit/node_modules/r1csfile/node_modules/ffjavascript": { + "version": "0.2.60", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.60.tgz", + "integrity": "sha512-T/9bnEL5xAZRDbQoEMf+pM9nrhK+C3JyZNmqiWub26EQorW7Jt+jR54gpqDhceA4Nj0YctPQwYnl8xa52/A26A==", + "dev": true, + "peer": true, + "dependencies": { + "wasmbuilder": "0.0.16", + "wasmcurves": "0.2.2", + "web-worker": "^1.2.0" + } + }, + "node_modules/@solarity/hardhat-zkit/node_modules/snarkjs": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.7.3.tgz", + "integrity": "sha512-cDLpWqdqEJSCQNc+cXYX1XTKdUZBtYEisuOsgmXf/HUsN5WmGN+FO7HfCS+cMQT1Nzbm1a9gAEpKH6KRtDtS1Q==", + "dev": true, + "peer": true, + "dependencies": { + "@iden3/binfileutils": "0.0.11", + "bfj": "^7.0.2", + "blake2b-wasm": "^2.4.0", + "circom_runtime": "0.1.24", + "ejs": "^3.1.6", + "fastfile": "0.0.20", + "ffjavascript": "0.2.63", + "js-sha3": "^0.8.0", + "logplease": "^1.2.15", + "r1csfile": "0.0.47" + }, + "bin": { + "snarkjs": "build/cli.cjs" + } + }, "node_modules/@solarity/zkit": { "version": "0.2.6", "resolved": "https://registry.npmjs.org/@solarity/zkit/-/zkit-0.2.6.tgz", @@ -2183,6 +2278,87 @@ "snarkjs": "0.7.3" } }, + "node_modules/@solarity/zkit/node_modules/@iden3/binfileutils": { + "version": "0.0.11", + "resolved": "https://registry.npmjs.org/@iden3/binfileutils/-/binfileutils-0.0.11.tgz", + "integrity": "sha512-LylnJoZ0CTdgErnKY8OxohvW4K+p6UHD3sxt+3P9AmMyBQjYR4IpoqoYZZ+9aMj89cmCQ21UvdhndAx04er3NA==", + "dependencies": { + "fastfile": "0.0.20", + "ffjavascript": "^0.2.48" + } + }, + "node_modules/@solarity/zkit/node_modules/circom_runtime": { + "version": "0.1.24", + "resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.24.tgz", + "integrity": "sha512-H7/7I2J/cBmRnZm9docOCGhfxzS61BEm4TMCWcrZGsWNBQhePNfQq88Oj2XpUfzmBTCd8pRvRb3Mvazt3TMrJw==", + "dependencies": { + "ffjavascript": "0.2.60" + }, + "bin": { + "calcwit": "calcwit.js" + } + }, + "node_modules/@solarity/zkit/node_modules/circom_runtime/node_modules/ffjavascript": { + "version": "0.2.60", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.60.tgz", + "integrity": "sha512-T/9bnEL5xAZRDbQoEMf+pM9nrhK+C3JyZNmqiWub26EQorW7Jt+jR54gpqDhceA4Nj0YctPQwYnl8xa52/A26A==", + "dependencies": { + "wasmbuilder": "0.0.16", + "wasmcurves": "0.2.2", + "web-worker": "^1.2.0" + } + }, + "node_modules/@solarity/zkit/node_modules/ffjavascript": { + "version": "0.2.63", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.63.tgz", + "integrity": "sha512-dBgdsfGks58b66JnUZeZpGxdMIDQ4QsD3VYlRJyFVrKQHb2kJy4R2gufx5oetrTxXPT+aEjg0dOvOLg1N0on4A==", + "dependencies": { + "wasmbuilder": "0.0.16", + "wasmcurves": "0.2.2", + "web-worker": "1.2.0" + } + }, + "node_modules/@solarity/zkit/node_modules/r1csfile": { + "version": "0.0.47", + "resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.47.tgz", + "integrity": "sha512-oI4mAwuh1WwuFg95eJDNDDL8hCaZkwnPuNZrQdLBWvDoRU7EG+L/MOHL7SwPW2Y+ZuYcTLpj3rBkgllBQZN/JA==", + "dependencies": { + "@iden3/bigarray": "0.0.2", + "@iden3/binfileutils": "0.0.11", + "fastfile": "0.0.20", + "ffjavascript": "0.2.60" + } + }, + "node_modules/@solarity/zkit/node_modules/r1csfile/node_modules/ffjavascript": { + "version": "0.2.60", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.60.tgz", + "integrity": "sha512-T/9bnEL5xAZRDbQoEMf+pM9nrhK+C3JyZNmqiWub26EQorW7Jt+jR54gpqDhceA4Nj0YctPQwYnl8xa52/A26A==", + "dependencies": { + "wasmbuilder": "0.0.16", + "wasmcurves": "0.2.2", + "web-worker": "^1.2.0" + } + }, + "node_modules/@solarity/zkit/node_modules/snarkjs": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.7.3.tgz", + "integrity": "sha512-cDLpWqdqEJSCQNc+cXYX1XTKdUZBtYEisuOsgmXf/HUsN5WmGN+FO7HfCS+cMQT1Nzbm1a9gAEpKH6KRtDtS1Q==", + "dependencies": { + "@iden3/binfileutils": "0.0.11", + "bfj": "^7.0.2", + "blake2b-wasm": "^2.4.0", + "circom_runtime": "0.1.24", + "ejs": "^3.1.6", + "fastfile": "0.0.20", + "ffjavascript": "0.2.63", + "js-sha3": "^0.8.0", + "logplease": "^1.2.15", + "r1csfile": "0.0.47" + }, + "bin": { + "snarkjs": "build/cli.cjs" + } + }, "node_modules/@solarity/zktype": { "version": "0.3.1", "resolved": "https://registry.npmjs.org/@solarity/zktype/-/zktype-0.3.1.tgz", @@ -3506,26 +3682,16 @@ } }, "node_modules/circom_runtime": { - "version": "0.1.24", - "resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.24.tgz", - "integrity": "sha512-H7/7I2J/cBmRnZm9docOCGhfxzS61BEm4TMCWcrZGsWNBQhePNfQq88Oj2XpUfzmBTCd8pRvRb3Mvazt3TMrJw==", + "version": "0.1.28", + "resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.28.tgz", + "integrity": "sha512-ACagpQ7zBRLKDl5xRZ4KpmYIcZDUjOiNRuxvXLqhnnlLSVY1Dbvh73TI853nqoR0oEbihtWmMSjgc5f+pXf/jQ==", "dependencies": { - "ffjavascript": "0.2.60" + "ffjavascript": "0.3.1" }, "bin": { "calcwit": "calcwit.js" } }, - "node_modules/circom_runtime/node_modules/ffjavascript": { - "version": "0.2.60", - "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.60.tgz", - "integrity": "sha512-T/9bnEL5xAZRDbQoEMf+pM9nrhK+C3JyZNmqiWub26EQorW7Jt+jR54gpqDhceA4Nj0YctPQwYnl8xa52/A26A==", - "dependencies": { - "wasmbuilder": "0.0.16", - "wasmcurves": "0.2.2", - "web-worker": "^1.2.0" - } - }, "node_modules/circomlib": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/circomlib/-/circomlib-2.0.5.tgz", @@ -5005,9 +5171,9 @@ } }, "node_modules/ffjavascript": { - "version": "0.2.63", - "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.63.tgz", - "integrity": "sha512-dBgdsfGks58b66JnUZeZpGxdMIDQ4QsD3VYlRJyFVrKQHb2kJy4R2gufx5oetrTxXPT+aEjg0dOvOLg1N0on4A==", + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.3.1.tgz", + "integrity": "sha512-4PbK1WYodQtuF47D4pRI5KUg3Q392vuP5WjE1THSnceHdXwU3ijaoS0OqxTzLknCtz4Z2TtABzkBdBdMn3B/Aw==", "dependencies": { "wasmbuilder": "0.0.16", "wasmcurves": "0.2.2", @@ -7901,24 +8067,24 @@ ] }, "node_modules/r1csfile": { - "version": "0.0.47", - "resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.47.tgz", - "integrity": "sha512-oI4mAwuh1WwuFg95eJDNDDL8hCaZkwnPuNZrQdLBWvDoRU7EG+L/MOHL7SwPW2Y+ZuYcTLpj3rBkgllBQZN/JA==", + "version": "0.0.48", + "resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.48.tgz", + "integrity": "sha512-kHRkKUJNaor31l05f2+RFzvcH5XSa7OfEfd/l4hzjte6NL6fjRkSMfZ4BjySW9wmfdwPOtq3mXurzPvPGEf5Tw==", "dependencies": { "@iden3/bigarray": "0.0.2", - "@iden3/binfileutils": "0.0.11", + "@iden3/binfileutils": "0.0.12", "fastfile": "0.0.20", - "ffjavascript": "0.2.60" + "ffjavascript": "0.3.0" } }, "node_modules/r1csfile/node_modules/ffjavascript": { - "version": "0.2.60", - "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.60.tgz", - "integrity": "sha512-T/9bnEL5xAZRDbQoEMf+pM9nrhK+C3JyZNmqiWub26EQorW7Jt+jR54gpqDhceA4Nj0YctPQwYnl8xa52/A26A==", + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.3.0.tgz", + "integrity": "sha512-l7sR5kmU3gRwDy8g0Z2tYBXy5ttmafRPFOqY7S6af5cq51JqJWt5eQ/lSR/rs2wQNbDYaYlQr5O+OSUf/oMLoQ==", "dependencies": { "wasmbuilder": "0.0.16", "wasmcurves": "0.2.2", - "web-worker": "^1.2.0" + "web-worker": "1.2.0" } }, "node_modules/randombytes": { @@ -8403,20 +8569,20 @@ } }, "node_modules/snarkjs": { - "version": "0.7.3", - "resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.7.3.tgz", - "integrity": "sha512-cDLpWqdqEJSCQNc+cXYX1XTKdUZBtYEisuOsgmXf/HUsN5WmGN+FO7HfCS+cMQT1Nzbm1a9gAEpKH6KRtDtS1Q==", + "version": "0.7.5", + "resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.7.5.tgz", + "integrity": "sha512-h+3c4rXZKLhLuHk4LHydZCk/h5GcNvk5GjVKRRkHmfb6Ntf8gHOA9zea3g656iclRuhqQ3iKDWFgiD9ypLrKiA==", "dependencies": { - "@iden3/binfileutils": "0.0.11", + "@iden3/binfileutils": "0.0.12", "bfj": "^7.0.2", "blake2b-wasm": "^2.4.0", - "circom_runtime": "0.1.24", + "circom_runtime": "0.1.28", "ejs": "^3.1.6", "fastfile": "0.0.20", - "ffjavascript": "0.2.63", + "ffjavascript": "0.3.1", "js-sha3": "^0.8.0", "logplease": "^1.2.15", - "r1csfile": "0.0.47" + "r1csfile": "0.0.48" }, "bin": { "snarkjs": "build/cli.cjs" diff --git a/package.json b/package.json index e4fc8cc..87c7964 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "ora": "5.4.1", "resolve": "1.22.8", "semver": "7.6.3", - "snarkjs": "0.7.3", + "snarkjs": "0.7.5", "uuid": "9.0.1", "zod": "3.23.8", "is-typed-array": "1.1.13", diff --git a/src/constants.ts b/src/constants.ts index 2a0a634..4197fbd 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -34,3 +34,5 @@ export const WASM_COMPILER_VERSIONING: { [key: string]: string } = { "2.1.8": "0.2.18-rc.3", "2.1.9": "0.2.19-rc.0", }; + +export const BN128_CURVE_NAME = "bn128"; diff --git a/src/core/setup/SetupProcessor.ts b/src/core/setup/SetupProcessor.ts index 852b7fe..cc864fd 100644 --- a/src/core/setup/SetupProcessor.ts +++ b/src/core/setup/SetupProcessor.ts @@ -9,6 +9,7 @@ import { PtauDownloader } from "../utils/PtauDownloader"; import { HardhatZKitError } from "../../errors"; import { PTAU_FILE_REG_EXP } from "../../constants"; import { Reporter } from "../../reporter"; +import { getBn128Curve } from "../../utils/utils"; import { getNormalizedFullPath } from "../../utils/path-utils"; import { CircuitArtifact, ICircuitArtifacts } from "../../types/artifacts/circuit-artifacts"; @@ -80,6 +81,8 @@ export class SetupProcessor { Reporter!.reportZKeyFilesGenerationHeader(contributions); + const curve = await getBn128Curve(); + for (const circuitArtifact of circuitArtifacts) { const r1csFilePath = circuitArtifact.compilerOutputFiles.r1cs?.fileSourcePath; const zkeyFilePath = this._circuitArtifacts.getCircuitArtifactFileFullPath(circuitArtifact, "zkey"); @@ -120,12 +123,16 @@ export class SetupProcessor { Reporter!.reportZKeyFileGenerationResult(spinnerId, circuitArtifact.circuitTemplateName, contributions); } + curve.terminate(); + return circuitArtifacts; } private async _generateVKeyFiles(circuitArtifacts: CircuitArtifact[]) { Reporter!.reportVKeyFilesGenerationHeader(); + const curve = await getBn128Curve(); + for (const circuitArtifact of circuitArtifacts) { const zkeyFilePath = this._circuitArtifacts.getCircuitArtifactFileFullPath(circuitArtifact, "zkey"); const vkeyFilePath = this._circuitArtifacts.getCircuitArtifactFileFullPath(circuitArtifact, "vkey"); @@ -145,6 +152,8 @@ export class SetupProcessor { Reporter!.reportVKeyFileGenerationResult(spinnerId, circuitArtifact.circuitTemplateName); } + + curve.terminate(); } private async _getPtauFile(circuitArtifacts: CircuitArtifact[]): Promise { diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 4174cfc..d171276 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -2,12 +2,16 @@ import fsExtra from "fs-extra"; import https from "https"; import { exec } from "child_process"; +import * as snarkjs from "snarkjs"; + import { createNonCryptographicHashBasedIdentifier } from "hardhat/internal/util/hash"; import { Reporter } from "../reporter"; import { ExecCallResult } from "../types/utils"; +import { BN128_CURVE_NAME } from "../constants"; + /** * Downloads a file from the specified URL * @@ -101,3 +105,7 @@ export async function execCall(execFile: string, callArgs: string[]): Promise Date: Tue, 29 Oct 2024 14:26:39 +0200 Subject: [PATCH 2/6] updated latest supported circom version --- src/constants.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constants.ts b/src/constants.ts index 4197fbd..ebe5535 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -27,7 +27,7 @@ export const COMPILER_AMD_REPOSITORY_URL = "https://github.com/iden3/circom/rele export const COMPILER_ARM_REPOSITORY_URL = "https://github.com/distributed-lab/circom/releases/download"; export const COMPILER_WASM_REPOSITORY_URL = "https://github.com/distributed-lab/circom-wasm/releases/download"; -export const LATEST_SUPPORTED_CIRCOM_VERSION = "2.1.9"; +export const LATEST_SUPPORTED_CIRCOM_VERSION = "2.2.0"; export const OLDEST_SUPPORTED_CIRCOM_VERSION = "2.0.5"; export const WASM_COMPILER_VERSIONING: { [key: string]: string } = { From 439d3e6c005b68857d7a611662db2d51de8bd0dc Mon Sep 17 00:00:00 2001 From: mllwchrry Date: Tue, 29 Oct 2024 14:56:31 +0200 Subject: [PATCH 3/6] got an old latest supported circom version back --- src/constants.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constants.ts b/src/constants.ts index ebe5535..4197fbd 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -27,7 +27,7 @@ export const COMPILER_AMD_REPOSITORY_URL = "https://github.com/iden3/circom/rele export const COMPILER_ARM_REPOSITORY_URL = "https://github.com/distributed-lab/circom/releases/download"; export const COMPILER_WASM_REPOSITORY_URL = "https://github.com/distributed-lab/circom-wasm/releases/download"; -export const LATEST_SUPPORTED_CIRCOM_VERSION = "2.2.0"; +export const LATEST_SUPPORTED_CIRCOM_VERSION = "2.1.9"; export const OLDEST_SUPPORTED_CIRCOM_VERSION = "2.0.5"; export const WASM_COMPILER_VERSIONING: { [key: string]: string } = { From 7b2fc579af7a35dd4b150677dc83654eb3525e6d Mon Sep 17 00:00:00 2001 From: mllwchrry Date: Tue, 29 Oct 2024 17:04:17 +0200 Subject: [PATCH 4/6] refactored curve termination --- src/core/setup/SetupProcessor.ts | 90 ++++++++++++++++---------------- src/utils/utils.ts | 4 +- 2 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/core/setup/SetupProcessor.ts b/src/core/setup/SetupProcessor.ts index cc864fd..2da1bf2 100644 --- a/src/core/setup/SetupProcessor.ts +++ b/src/core/setup/SetupProcessor.ts @@ -9,7 +9,7 @@ import { PtauDownloader } from "../utils/PtauDownloader"; import { HardhatZKitError } from "../../errors"; import { PTAU_FILE_REG_EXP } from "../../constants"; import { Reporter } from "../../reporter"; -import { getBn128Curve } from "../../utils/utils"; +import { terminateCurve } from "../../utils/utils"; import { getNormalizedFullPath } from "../../utils/path-utils"; import { CircuitArtifact, ICircuitArtifacts } from "../../types/artifacts/circuit-artifacts"; @@ -81,58 +81,56 @@ export class SetupProcessor { Reporter!.reportZKeyFilesGenerationHeader(contributions); - const curve = await getBn128Curve(); - - for (const circuitArtifact of circuitArtifacts) { - const r1csFilePath = circuitArtifact.compilerOutputFiles.r1cs?.fileSourcePath; - const zkeyFilePath = this._circuitArtifacts.getCircuitArtifactFileFullPath(circuitArtifact, "zkey"); - - if (!r1csFilePath) { - throw new HardhatZKitError(`R1CS file for ${name} circuit not found. Compile circuits and try again.`); - } - - Reporter!.verboseLog("setup-processor:zkey", "Generating ZKey file for %s circuit with params %o", [ - circuitArtifact.circuitTemplateName, - { r1csFilePath, zkeyFilePath }, - ]); - - const spinnerId: string | null = Reporter!.reportZKeyFileGenerationStartWithSpinner( - circuitArtifact.circuitTemplateName, - ); - - if (provingSystem === "groth16") { - await snarkjs.zKey.newZKey(r1csFilePath, ptauFilePath, zkeyFilePath); - - const zKeyFileNext = `${zkeyFilePath}.next.zkey`; + try { + for (const circuitArtifact of circuitArtifacts) { + const r1csFilePath = circuitArtifact.compilerOutputFiles.r1cs?.fileSourcePath; + const zkeyFilePath = this._circuitArtifacts.getCircuitArtifactFileFullPath(circuitArtifact, "zkey"); - for (let i = 0; i < contributions; ++i) { - await snarkjs.zKey.contribute( - zkeyFilePath, - zKeyFileNext, - `${zkeyFilePath}_contribution_${i}`, - randomBytes(32).toString("hex"), - ); + if (!r1csFilePath) { + throw new HardhatZKitError(`R1CS file for ${name} circuit not found. Compile circuits and try again.`); + } - fsExtra.rmSync(zkeyFilePath); - fsExtra.renameSync(zKeyFileNext, zkeyFilePath); + Reporter!.verboseLog("setup-processor:zkey", "Generating ZKey file for %s circuit with params %o", [ + circuitArtifact.circuitTemplateName, + { r1csFilePath, zkeyFilePath }, + ]); + + const spinnerId: string | null = Reporter!.reportZKeyFileGenerationStartWithSpinner( + circuitArtifact.circuitTemplateName, + ); + + if (provingSystem === "groth16") { + await snarkjs.zKey.newZKey(r1csFilePath, ptauFilePath, zkeyFilePath); + + const zKeyFileNext = `${zkeyFilePath}.next.zkey`; + + for (let i = 0; i < contributions; ++i) { + await snarkjs.zKey.contribute( + zkeyFilePath, + zKeyFileNext, + `${zkeyFilePath}_contribution_${i}`, + randomBytes(32).toString("hex"), + ); + + fsExtra.rmSync(zkeyFilePath); + fsExtra.renameSync(zKeyFileNext, zkeyFilePath); + } + } else { + throw new HardhatZKitError(`Unsupported proving system - ${provingSystem}`); } - } else { - throw new HardhatZKitError(`Unsupported proving system - ${provingSystem}`); - } - Reporter!.reportZKeyFileGenerationResult(spinnerId, circuitArtifact.circuitTemplateName, contributions); + Reporter!.reportZKeyFileGenerationResult(spinnerId, circuitArtifact.circuitTemplateName, contributions); + } + } finally { + await terminateCurve(); } - curve.terminate(); - return circuitArtifacts; } private async _generateVKeyFiles(circuitArtifacts: CircuitArtifact[]) { Reporter!.reportVKeyFilesGenerationHeader(); - const curve = await getBn128Curve(); - for (const circuitArtifact of circuitArtifacts) { const zkeyFilePath = this._circuitArtifacts.getCircuitArtifactFileFullPath(circuitArtifact, "zkey"); const vkeyFilePath = this._circuitArtifacts.getCircuitArtifactFileFullPath(circuitArtifact, "vkey"); @@ -146,14 +144,18 @@ export class SetupProcessor { circuitArtifact.circuitTemplateName, ); - const vKeyData = await snarkjs.zKey.exportVerificationKey(zkeyFilePath); + let vKeyData; + + try { + vKeyData = await snarkjs.zKey.exportVerificationKey(zkeyFilePath); + } finally { + await terminateCurve(); + } fsExtra.outputFileSync(vkeyFilePath, JSON.stringify(vKeyData)); Reporter!.reportVKeyFileGenerationResult(spinnerId, circuitArtifact.circuitTemplateName); } - - curve.terminate(); } private async _getPtauFile(circuitArtifacts: CircuitArtifact[]): Promise { diff --git a/src/utils/utils.ts b/src/utils/utils.ts index d171276..cb378ae 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -106,6 +106,6 @@ export function getFileHash(filePath: string): string { return createNonCryptographicHashBasedIdentifier(Buffer.from(fsExtra.readFileSync(filePath))).toString("hex"); } -export async function getBn128Curve() { - return (snarkjs as any).curves.getCurveFromName(BN128_CURVE_NAME); +export async function terminateCurve() { + (await (snarkjs as any).curves.getCurveFromName(BN128_CURVE_NAME)).terminate(); } From 1d527273452f16820cad2025e61cfdc8afd3f1dc Mon Sep 17 00:00:00 2001 From: mllwchrry Date: Tue, 29 Oct 2024 17:25:33 +0200 Subject: [PATCH 5/6] placed vkey generation loop in try statement --- src/core/setup/SetupProcessor.ts | 36 +++++++++++++++----------------- src/utils/utils.ts | 2 +- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/core/setup/SetupProcessor.ts b/src/core/setup/SetupProcessor.ts index 2da1bf2..fc69dc3 100644 --- a/src/core/setup/SetupProcessor.ts +++ b/src/core/setup/SetupProcessor.ts @@ -131,30 +131,28 @@ export class SetupProcessor { private async _generateVKeyFiles(circuitArtifacts: CircuitArtifact[]) { Reporter!.reportVKeyFilesGenerationHeader(); - for (const circuitArtifact of circuitArtifacts) { - const zkeyFilePath = this._circuitArtifacts.getCircuitArtifactFileFullPath(circuitArtifact, "zkey"); - const vkeyFilePath = this._circuitArtifacts.getCircuitArtifactFileFullPath(circuitArtifact, "vkey"); - - Reporter!.verboseLog("setup-processor:vkey", "Generating VKey file for %s circuit with params %o", [ - circuitArtifact.circuitTemplateName, - { zkeyFilePath, vkeyFilePath }, - ]); + try { + for (const circuitArtifact of circuitArtifacts) { + const zkeyFilePath = this._circuitArtifacts.getCircuitArtifactFileFullPath(circuitArtifact, "zkey"); + const vkeyFilePath = this._circuitArtifacts.getCircuitArtifactFileFullPath(circuitArtifact, "vkey"); - const spinnerId: string | null = Reporter!.reportVKeyFileGenerationStartWithSpinner( - circuitArtifact.circuitTemplateName, - ); + Reporter!.verboseLog("setup-processor:vkey", "Generating VKey file for %s circuit with params %o", [ + circuitArtifact.circuitTemplateName, + { zkeyFilePath, vkeyFilePath }, + ]); - let vKeyData; + const spinnerId: string | null = Reporter!.reportVKeyFileGenerationStartWithSpinner( + circuitArtifact.circuitTemplateName, + ); - try { - vKeyData = await snarkjs.zKey.exportVerificationKey(zkeyFilePath); - } finally { - await terminateCurve(); - } + const vKeyData = await snarkjs.zKey.exportVerificationKey(zkeyFilePath); - fsExtra.outputFileSync(vkeyFilePath, JSON.stringify(vKeyData)); + fsExtra.outputFileSync(vkeyFilePath, JSON.stringify(vKeyData)); - Reporter!.reportVKeyFileGenerationResult(spinnerId, circuitArtifact.circuitTemplateName); + Reporter!.reportVKeyFileGenerationResult(spinnerId, circuitArtifact.circuitTemplateName); + } + } finally { + await terminateCurve(); } } diff --git a/src/utils/utils.ts b/src/utils/utils.ts index cb378ae..9c38f85 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -107,5 +107,5 @@ export function getFileHash(filePath: string): string { } export async function terminateCurve() { - (await (snarkjs as any).curves.getCurveFromName(BN128_CURVE_NAME)).terminate(); + await (await (snarkjs as any).curves.getCurveFromName(BN128_CURVE_NAME)).terminate(); } From e44df7928abb8e764846ce8120ec3444995a55cf Mon Sep 17 00:00:00 2001 From: Artem Chystiakov Date: Tue, 29 Oct 2024 18:46:30 +0200 Subject: [PATCH 6/6] fix readme --- README.md | 6 +++--- package-lock.json | 4 ++-- package.json | 2 +- src/constants.ts | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 9bb03b6..bf8573e 100644 --- a/README.md +++ b/README.md @@ -233,7 +233,7 @@ Afterward, copy the provided script to the `test` directory and run the tests vi --- -- **`getCircuit() -> zkit`** +- **`async getCircuit() -> zkit`** The method accepts the name of the `main` component of the circuit and returns the instantiated zkit object pointing to that circuit. @@ -251,7 +251,7 @@ Where: ## Known limitations -- Temporarily, the only supported proving system is `groth16`. -- Sometimes `hardhat` scripts that generate `zkey` files may run indefinitely. Waiting for [this snarkjs fix](https://github.com/iden3/snarkjs/pull/512) to be published. +- Temporarily, the only available proving system is `groth16`. Support for `plonk` is just behind the corner. +- Sometimes `hardhat` scripts that generate ZK proofs may run indefinitely. This will be fixed in the next major release. - Currently there is minimal support for `var` Circom variables. Some circuits may not work if you are using complex `var`-dependent expressions. - Due to current `wasm` memory limitations (address space is 32-bit), the plugin may fail to compile especially large circuits on some platforms. diff --git a/package-lock.json b/package-lock.json index eeb5ecd..e0697a1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@solarity/hardhat-zkit", - "version": "0.4.9", + "version": "0.4.10", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@solarity/hardhat-zkit", - "version": "0.4.9", + "version": "0.4.10", "license": "MIT", "workspaces": [ "test/fixture-projects/*" diff --git a/package.json b/package.json index 87c7964..0f9f55b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@solarity/hardhat-zkit", - "version": "0.4.9", + "version": "0.4.10", "description": "The ultimate TypeScript environment for Circom development", "main": "dist/src/index.js", "types": "dist/src/index.d.ts", diff --git a/src/constants.ts b/src/constants.ts index 4197fbd..1b4e17f 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -7,6 +7,8 @@ export const CIRCUIT_COMPILE_CACHE_VERSION = "hh-zkit-compile-cache-1"; export const CIRCUIT_SETUP_CACHE_VERSION = "hh-zkit-setup-cache-1"; export const CIRCUIT_ARTIFACT_VERSION = "hh-zkit-artifacts-1"; +export const BN128_CURVE_NAME = "bn128"; + export const NODE_MODULES = "node_modules"; export const MAGIC_DESCRIPTOR = 1337; @@ -34,5 +36,3 @@ export const WASM_COMPILER_VERSIONING: { [key: string]: string } = { "2.1.8": "0.2.18-rc.3", "2.1.9": "0.2.19-rc.0", }; - -export const BN128_CURVE_NAME = "bn128";