From e21ccedc154863a73434e6321e48da8f90480023 Mon Sep 17 00:00:00 2001 From: cedoor Date: Mon, 6 Jun 2022 12:01:24 +0200 Subject: [PATCH] chore: update scripts to download snark artifacts Former-commit-id: d9c069784fca203124975e1b45c29427c07c0e82 --- .github/workflows/coverage.yml | 2 +- .github/workflows/test.yml | 2 +- README.md | 8 +++---- package.json | 9 ++++--- scripts/download-snark-artifacts.ts | 24 +++++++++++++++++++ scripts/download-zk-files.ts | 25 ------------------- scripts/generate-verifiers.ts | 37 ----------------------------- tasks/deploy-verifier.ts | 2 +- test/Semaphore.ts | 10 +++----- test/SemaphoreVoting.ts | 6 ++--- test/SemaphoreWhistleblowing.ts | 6 ++--- 11 files changed, 44 insertions(+), 87 deletions(-) create mode 100644 scripts/download-snark-artifacts.ts delete mode 100644 scripts/download-zk-files.ts delete mode 100644 scripts/generate-verifiers.ts diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index eeaa327e..21546903 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -17,7 +17,7 @@ jobs: node-version: 16.x - run: yarn --frozen-lockfile - - run: yarn download:zk-files + - run: yarn download:snark-artifacts - run: yarn compile - run: yarn test:coverage diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b33ad885..2b50e90b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,6 +18,6 @@ jobs: node-version: 16.x - run: yarn --frozen-lockfile - - run: yarn download:zk-files + - run: yarn download:snark-artifacts - run: yarn compile - run: yarn test:prod diff --git a/README.md b/README.md index 6647163e..9320e854 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ | Semaphore is a protocol, designed to be a simple and generic privacy layer for Ethereum DApps. Using zero knowledge, Ethereum users can prove their membership of a group and send signals such as votes or endorsements without revealing their original identity. | -| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | The core of the Semaphore protocol is in the [circuit logic](/circuits/scheme.png). However Semaphore also provides [Solidity contracts](/contracts) (NPM: `@semaphore-protocol/contracts`) and [JavaScript libraries](https://github.com/privacy-scaling-explorations/zk-kit) (NPM: `@zk-kit/identity`, `@zk-kit/protocols`) to make the steps for offchain proof creation and onchain verification easier. To learn more about Semaphore visit https://semaphore.appliedzkp.org. @@ -70,12 +70,12 @@ yarn # or `npm i` Copy the `.env.example` file and rename it `.env`. -### ZK files +### Snark artifacts -Download the Semaphore zero-knowledge files needed to generate proofs: +Download the Semaphore snark artifacts needed to generate and verify proofs: ```bash -yarn download:zk-files +yarn download:snark-artifacts ``` ### Compile diff --git a/package.json b/package.json index 0dad7b65..f167d42e 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,7 @@ "scripts": { "start": "hardhat node", "compile": "hardhat compile", - "download:zk-files": "ts-node scripts/download-zk-files.ts", - "generate:verifiers": "ts-node scripts/generate-verifiers.ts", + "download:snark-artifacts": "hardhat run scripts/download-snark-artifacts.ts", "deploy:all": "hardhat run scripts/deploy-all.ts", "deploy:verifier": "hardhat deploy:verifier", "deploy:semaphore": "hardhat deploy:semaphore", @@ -28,10 +27,10 @@ "test:prod": "yarn lint && yarn test", "typechain": "hardhat typechain", "lint": "yarn lint:sol && yarn lint:ts", - "lint:ts": "eslint . --ext .js,.jsx,.ts,.tsx", + "lint:ts": "eslint . --ext .js,.ts", "lint:sol": "solhint 'contracts/**/*.sol'", "prettier": "prettier -c .", - "prettier:fix": "prettier -w .", + "prettier:write": "prettier -w .", "commit": "cz", "precommit": "lint-staged" }, @@ -89,7 +88,7 @@ "cache": "./cache", "snarkjs-templates": "./snarkjs-templates", "build": { - "zk-files": "./build/zk-files", + "snark-artifacts": "./build/snark-artifacts", "contracts": "./build/contracts", "typechain": "./build/typechain" } diff --git a/scripts/download-snark-artifacts.ts b/scripts/download-snark-artifacts.ts new file mode 100644 index 00000000..83a5bf2e --- /dev/null +++ b/scripts/download-snark-artifacts.ts @@ -0,0 +1,24 @@ +import download from "download" +import fs from "fs" +import { config } from "../package.json" + +async function main() { + const snarkArtifactsPath = config.paths.build["snark-artifacts"] + const url = `http://www.trusted-setup-pse.org/semaphore/${process.env.TREE_DEPTH}` + + if (!fs.existsSync(snarkArtifactsPath)) { + fs.mkdirSync(snarkArtifactsPath, { recursive: true }) + } + + if (!fs.existsSync(`${snarkArtifactsPath}/semaphore.zkey`)) { + await download(`${url}/semaphore.wasm`, snarkArtifactsPath) + await download(`${url}/semaphore.zkey`, snarkArtifactsPath) + } +} + +main() + .then(() => process.exit(0)) + .catch((error) => { + console.error(error) + process.exit(1) + }) diff --git a/scripts/download-zk-files.ts b/scripts/download-zk-files.ts deleted file mode 100644 index 209390ee..00000000 --- a/scripts/download-zk-files.ts +++ /dev/null @@ -1,25 +0,0 @@ -import download from "download" -import fs from "fs" -import { config } from "../package.json" - -async function main() { - const buildPath = config.paths.build["zk-files"] - const url = "http://www.trusted-setup-pse.org/semaphore/semaphore.zip" - - if (!fs.existsSync(buildPath)) { - fs.mkdirSync(buildPath, { recursive: true }) - } - - if (!fs.existsSync(`${buildPath}/16/semaphore.zkey`)) { - await download(url, buildPath, { - extract: true - }) - } -} - -main() - .then(() => process.exit(0)) - .catch((error) => { - console.error(error) - process.exit(1) - }) diff --git a/scripts/generate-verifiers.ts b/scripts/generate-verifiers.ts deleted file mode 100644 index 89e3bf6d..00000000 --- a/scripts/generate-verifiers.ts +++ /dev/null @@ -1,37 +0,0 @@ -import fs from "fs" -import logger from "js-logger" -import { zKey } from "snarkjs" -import { config } from "../package.json" - -logger.useDefaults() - -async function main() { - const buildPath = config.paths.build["zk-files"] - const contractsPath = config.paths.contracts - const templatesPath = config.paths["snarkjs-templates"] - const solidityVersion = config.solidity.version - - if (fs.existsSync(`${buildPath}/16/semaphore.zkey`)) { - for (let treeDepth = 16; treeDepth <= 32; treeDepth++) { - let verifierCode = await zKey.exportSolidityVerifier( - `${buildPath}/${treeDepth}/semaphore.zkey`, - { groth16: fs.readFileSync(`${templatesPath}/verifier_groth16.sol.ejs`, "utf8") }, - logger - ) - verifierCode = verifierCode.replace( - /pragma solidity \^\d+\.\d+\.\d+/, - `pragma solidity ^${solidityVersion}` - ) - verifierCode = verifierCode.replace(/Verifier/, `Verifier${treeDepth}`) - - fs.writeFileSync(`${contractsPath}/verifiers/Verifier${treeDepth}.sol`, verifierCode, "utf-8") - } - } -} - -main() - .then(() => process.exit(0)) - .catch((error) => { - console.error(error) - process.exit(1) - }) diff --git a/tasks/deploy-verifier.ts b/tasks/deploy-verifier.ts index c6a88cc4..0bc4d961 100644 --- a/tasks/deploy-verifier.ts +++ b/tasks/deploy-verifier.ts @@ -2,7 +2,7 @@ import { Contract } from "ethers" import { task, types } from "hardhat/config" task("deploy:verifier", "Deploy a Verifier contract") - .addOptionalParam("depth", "Tree depth", 20, types.int) + .addOptionalParam("depth", "Tree depth", Number(process.env.TREE_DEPTH) || 20, types.int) .addOptionalParam("logs", "Print the logs", true, types.boolean) .setAction(async ({ depth, logs }, { ethers }): Promise => { const ContractFactory = await ethers.getContractFactory(`Verifier${depth}`) diff --git a/test/Semaphore.ts b/test/Semaphore.ts index ba7b9644..c658c0d1 100644 --- a/test/Semaphore.ts +++ b/test/Semaphore.ts @@ -1,27 +1,23 @@ import { Strategy, ZkIdentity } from "@zk-kit/identity" import { generateMerkleProof, Semaphore, SemaphoreFullProof, SemaphoreSolidityProof } from "@zk-kit/protocols" import { expect } from "chai" -import { config as dotenvConfig } from "dotenv" import { constants, Signer, utils } from "ethers" import { run } from "hardhat" -import { resolve } from "path" import { Semaphore as SemaphoreContract } from "../build/typechain/Semaphore" import { createIdentityCommitments, createTree } from "./utils" import { config } from "../package.json" -dotenvConfig({ path: resolve(__dirname, "../.env") }) - describe("Semaphore", () => { let contract: SemaphoreContract let signers: Signer[] let accounts: string[] - const depth = 20 + const depth = Number(process.env.TREE_DEPTH) const groupId = 1 const members = createIdentityCommitments(3) - const wasmFilePath = `${config.paths.build["zk-files"]}/${depth}/semaphore.wasm` - const zkeyFilePath = `${config.paths.build["zk-files"]}/${depth}/semaphore.zkey` + const wasmFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.wasm` + const zkeyFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.zkey` before(async () => { const { address: verifierAddress } = await run("deploy:verifier", { logs: false, depth }) diff --git a/test/SemaphoreVoting.ts b/test/SemaphoreVoting.ts index 16d50454..0115aa71 100644 --- a/test/SemaphoreVoting.ts +++ b/test/SemaphoreVoting.ts @@ -12,13 +12,13 @@ describe("SemaphoreVoting", () => { let accounts: Signer[] let coordinator: string - const depth = 20 + const depth = Number(process.env.TREE_DEPTH) const pollIds = [BigInt(1), BigInt(2), BigInt(3)] const encryptionKey = BigInt(0) const decryptionKey = BigInt(0) - const wasmFilePath = `${config.paths.build["zk-files"]}/${depth}/semaphore.wasm` - const zkeyFilePath = `${config.paths.build["zk-files"]}/${depth}/semaphore.zkey` + const wasmFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.wasm` + const zkeyFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.zkey` before(async () => { const { address: verifierAddress } = await run("deploy:verifier", { logs: false, depth }) diff --git a/test/SemaphoreWhistleblowing.ts b/test/SemaphoreWhistleblowing.ts index 1538bdd5..987113ef 100644 --- a/test/SemaphoreWhistleblowing.ts +++ b/test/SemaphoreWhistleblowing.ts @@ -12,11 +12,11 @@ describe("SemaphoreWhistleblowing", () => { let accounts: Signer[] let editor: string - const depth = 20 + const depth = Number(process.env.TREE_DEPTH) const entityIds = [BigInt(1), BigInt(2)] - const wasmFilePath = `${config.paths.build["zk-files"]}/${depth}/semaphore.wasm` - const zkeyFilePath = `${config.paths.build["zk-files"]}/${depth}/semaphore.zkey` + const wasmFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.wasm` + const zkeyFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.zkey` before(async () => { const { address: verifierAddress } = await run("deploy:verifier", { logs: false, depth })