From 5abfed2b775179c6cc818985381ddfe4da3cdf79 Mon Sep 17 00:00:00 2001 From: cedoor Date: Thu, 27 Jan 2022 11:24:48 +0100 Subject: [PATCH] style: fix syntax with prettier Former-commit-id: f86391ad65895ec40fba56160f86e4bff9aceb6e --- .lintstagedrc.json | 2 +- deploy/Poseidon.js | 32 ++--- deploy/Semaphore.js | 48 ++++---- hardhat.config.ts | 26 ++-- scripts/compile-circuits.ts | 90 +++++++------- test/Semaphore.ts | 240 ++++++++++++++++++------------------ tsconfig.json | 28 ++--- types/snarkjs/index.d.ts | 114 ++++++++--------- 8 files changed, 290 insertions(+), 290 deletions(-) diff --git a/.lintstagedrc.json b/.lintstagedrc.json index fef77c62..6e852fb0 100644 --- a/.lintstagedrc.json +++ b/.lintstagedrc.json @@ -1,3 +1,3 @@ { - "**/*.{js,ts}": ["prettier --write", "eslint --fix"] + "**/*.{js,ts}": ["prettier --write", "eslint --fix"] } diff --git a/deploy/Poseidon.js b/deploy/Poseidon.js index 0c378ac9..5fdc7f71 100644 --- a/deploy/Poseidon.js +++ b/deploy/Poseidon.js @@ -1,21 +1,21 @@ -const { poseidon_gencontract: poseidonGenContract } = require('circomlibjs'); +const { poseidon_gencontract: poseidonGenContract } = require("circomlibjs") module.exports = async ({ getNamedAccounts, deployments }) => { - const { deploy } = deployments; - const { deployer } = await getNamedAccounts(); + const { deploy } = deployments + const { deployer } = await getNamedAccounts() - const deployTx = (x) => { - return { - from: deployer, - log: true, - contract: { - abi: poseidonGenContract.generateABI(x), - bytecode: poseidonGenContract.createCode(x) - } - } + const deployTx = (x) => { + return { + from: deployer, + log: true, + contract: { + abi: poseidonGenContract.generateABI(x), + bytecode: poseidonGenContract.createCode(x) + } } + } - await deploy("PoseidonT3", deployTx(2)); - await deploy("PoseidonT6", deployTx(5)); -}; -module.exports.tags = ['Poseidon', 'complete']; \ No newline at end of file + await deploy("PoseidonT3", deployTx(2)) + await deploy("PoseidonT6", deployTx(5)) +} +module.exports.tags = ["Poseidon", "complete"] diff --git a/deploy/Semaphore.js b/deploy/Semaphore.js index b9c07eab..6159556f 100644 --- a/deploy/Semaphore.js +++ b/deploy/Semaphore.js @@ -1,29 +1,29 @@ -const { genExternalNullifier } = require('../utils'); +const { genExternalNullifier } = require("../utils") module.exports = async ({ getNamedAccounts, deployments }) => { - const { deploy } = deployments; - const { deployer } = await getNamedAccounts(); - const depth = 20; + const { deploy } = deployments + const { deployer } = await getNamedAccounts() + const depth = 20 - const poseidonT3 = await deployments.get("PoseidonT3") - const poseidonT6 = await deployments.get("PoseidonT6") + const poseidonT3 = await deployments.get("PoseidonT3") + const poseidonT6 = await deployments.get("PoseidonT6") - const externalNullifier = genExternalNullifier('test-voting'); - const semaphore = await deploy('Semaphore', { - from: deployer, - log: true, - args: [depth, externalNullifier], - libraries: { - PoseidonT3: poseidonT3.address, - PoseidonT6: poseidonT6.address - } - }); + const externalNullifier = genExternalNullifier("test-voting") + const semaphore = await deploy("Semaphore", { + from: deployer, + log: true, + args: [depth, externalNullifier], + libraries: { + PoseidonT3: poseidonT3.address, + PoseidonT6: poseidonT6.address + } + }) - await deploy('SemaphoreClient', { - from: deployer, - log: true, - args: [semaphore.address], - }); -}; -module.exports.tags = ['complete']; -module.exports.dependencies = ['PoseidonT3', 'PoseidonT6']; + await deploy("SemaphoreClient", { + from: deployer, + log: true, + args: [semaphore.address] + }) +} +module.exports.tags = ["complete"] +module.exports.dependencies = ["PoseidonT3", "PoseidonT6"] diff --git a/hardhat.config.ts b/hardhat.config.ts index 3e619533..e9841bc3 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -6,21 +6,21 @@ import "@nomiclabs/hardhat-ethers" import "hardhat-deploy" const hardhatConfig: HardhatUserConfig = { - solidity: config.solidity, - paths: config.paths, - networks: { - hardhat: { - chainId: 1337, - allowUnlimitedContractSize: true - }, - localhost: { - url: "http://localhost:8545", - allowUnlimitedContractSize: true - } + solidity: config.solidity, + paths: config.paths, + networks: { + hardhat: { + chainId: 1337, + allowUnlimitedContractSize: true }, - namedAccounts: { - deployer: 0 + localhost: { + url: "http://localhost:8545", + allowUnlimitedContractSize: true } + }, + namedAccounts: { + deployer: 0 + } } export default hardhatConfig diff --git a/scripts/compile-circuits.ts b/scripts/compile-circuits.ts index c845c222..82dd166c 100644 --- a/scripts/compile-circuits.ts +++ b/scripts/compile-circuits.ts @@ -9,66 +9,66 @@ import logger from "js-logger" logger.useDefaults() async function exec(command: string) { - const { stderr, stdout } = await util.promisify(_exec)(command) + const { stderr, stdout } = await util.promisify(_exec)(command) - if (stderr) { - throw new Error(stderr) - } + if (stderr) { + throw new Error(stderr) + } - logger.info(stdout) + logger.info(stdout) } async function main() { - const buildPath = config.paths.build.snark - const solidityVersion = config.solidity.version + const buildPath = config.paths.build.snark + const solidityVersion = config.solidity.version - if (!fs.existsSync(buildPath)) { - fs.mkdirSync(buildPath, { recursive: true }) - } + if (!fs.existsSync(buildPath)) { + fs.mkdirSync(buildPath, { recursive: true }) + } - if (!fs.existsSync(`${buildPath}/powersOfTau28_hez_final_14.ptau`)) { - const url = "https://hermez.s3-eu-west-1.amazonaws.com" - const fileName = "powersOfTau28_hez_final_14.ptau" + if (!fs.existsSync(`${buildPath}/powersOfTau28_hez_final_14.ptau`)) { + const url = "https://hermez.s3-eu-west-1.amazonaws.com" + const fileName = "powersOfTau28_hez_final_14.ptau" - await download(`${url}/${fileName}`, buildPath) - } + await download(`${url}/${fileName}`, buildPath) + } - await exec(`circom ./circuits/semaphore.circom --r1cs --wasm --sym -o ${buildPath}`) + await exec(`circom ./circuits/semaphore.circom --r1cs --wasm --sym -o ${buildPath}`) - await r1cs.info(`${buildPath}/semaphore.r1cs`, logger) + await r1cs.info(`${buildPath}/semaphore.r1cs`, logger) - await zKey.newZKey( - `${buildPath}/semaphore.r1cs`, - `${buildPath}/powersOfTau28_hez_final_14.ptau`, - `${buildPath}/semaphore_0000.zkey`, - logger - ) + await zKey.newZKey( + `${buildPath}/semaphore.r1cs`, + `${buildPath}/powersOfTau28_hez_final_14.ptau`, + `${buildPath}/semaphore_0000.zkey`, + logger + ) - await zKey.beacon( - `${buildPath}/semaphore_0000.zkey`, - `${buildPath}/semaphore_final.zkey`, - "Final Beacon", - "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", - 10, - logger - ) + await zKey.beacon( + `${buildPath}/semaphore_0000.zkey`, + `${buildPath}/semaphore_final.zkey`, + "Final Beacon", + "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + 10, + logger + ) - const verificationKey = await zKey.exportVerificationKey(`${buildPath}/semaphore_final.zkey`, logger) + const verificationKey = await zKey.exportVerificationKey(`${buildPath}/semaphore_final.zkey`, logger) - let verifierCode = await zKey.exportSolidityVerifier( - `${buildPath}/semaphore_final.zkey`, - { groth16: fs.readFileSync("./node_modules/snarkjs/templates/verifier_groth16.sol.ejs", "utf8") }, - logger - ) - verifierCode = verifierCode.replace(/pragma solidity \^\d+\.\d+\.\d+/, `pragma solidity ^${solidityVersion}`) + let verifierCode = await zKey.exportSolidityVerifier( + `${buildPath}/semaphore_final.zkey`, + { groth16: fs.readFileSync("./node_modules/snarkjs/templates/verifier_groth16.sol.ejs", "utf8") }, + logger + ) + verifierCode = verifierCode.replace(/pragma solidity \^\d+\.\d+\.\d+/, `pragma solidity ^${solidityVersion}`) - fs.writeFileSync(`${buildPath}/verification_key.json`, JSON.stringify(verificationKey), "utf-8") - fs.writeFileSync(`${config.paths.contracts}/Verifier.sol`, verifierCode, "utf-8") + fs.writeFileSync(`${buildPath}/verification_key.json`, JSON.stringify(verificationKey), "utf-8") + fs.writeFileSync(`${config.paths.contracts}/Verifier.sol`, verifierCode, "utf-8") } main() - .then(() => process.exit(0)) - .catch((error) => { - console.error(error) - process.exit(1) - }) + .then(() => process.exit(0)) + .catch((error) => { + console.error(error) + process.exit(1) + }) diff --git a/test/Semaphore.ts b/test/Semaphore.ts index 57bf8287..8ea7b2d0 100644 --- a/test/Semaphore.ts +++ b/test/Semaphore.ts @@ -7,148 +7,148 @@ import { ethers } from "hardhat" import { join } from "path" function deployPoseidonTx(x: number) { - return ethers.getContractFactory(poseidonGenContract.generateABI(x), poseidonGenContract.createCode(x)) + return ethers.getContractFactory(poseidonGenContract.generateABI(x), poseidonGenContract.createCode(x)) } describe("Semaphore", () => { - let semaphore: any - let defaultExternalNullifier: any - let newExternalNullifier: any + let semaphore: any + let defaultExternalNullifier: any + let newExternalNullifier: any - const identityCommitments: bigint[] = [] - const ZERO_VALUE = BigInt(ethers.utils.solidityKeccak256(["bytes"], [ethers.utils.toUtf8Bytes("Semaphore")])) + const identityCommitments: bigint[] = [] + const ZERO_VALUE = BigInt(ethers.utils.solidityKeccak256(["bytes"], [ethers.utils.toUtf8Bytes("Semaphore")])) - before(async () => { - defaultExternalNullifier = genExternalNullifier("voting_1") - newExternalNullifier = genExternalNullifier("voting-2") + before(async () => { + defaultExternalNullifier = genExternalNullifier("voting_1") + newExternalNullifier = genExternalNullifier("voting-2") - const PoseidonT3 = await deployPoseidonTx(2) - const poseidonT3 = await PoseidonT3.deploy() - await poseidonT3.deployed() + const PoseidonT3 = await deployPoseidonTx(2) + const poseidonT3 = await PoseidonT3.deploy() + await poseidonT3.deployed() - const PoseidonT6 = await deployPoseidonTx(5) - const poseidonT6 = await PoseidonT6.deploy() - await poseidonT6.deployed() + const PoseidonT6 = await deployPoseidonTx(5) + const poseidonT6 = await PoseidonT6.deploy() + await poseidonT6.deployed() - const Semaphore = await ethers.getContractFactory("Semaphore", { - libraries: { - PoseidonT3: poseidonT3.address, - PoseidonT6: poseidonT6.address - } - }) - - semaphore = await Semaphore.deploy(20, defaultExternalNullifier) - - await semaphore.deployed() - - const leafIndex = 3 - - for (let i = 0; i < leafIndex; i++) { - const tmpIdentity = new ZkIdentity() - const tmpCommitment = tmpIdentity.genIdentityCommitment() - - identityCommitments.push(tmpCommitment) - - await semaphore.insertIdentity(tmpCommitment) - } + const Semaphore = await ethers.getContractFactory("Semaphore", { + libraries: { + PoseidonT3: poseidonT3.address, + PoseidonT6: poseidonT6.address + } }) - describe("Proof", () => { - it("Should generate full semaphore proof", async () => { - const identity = new ZkIdentity() - const identityCommitment = identity.genIdentityCommitment() + semaphore = await Semaphore.deploy(20, defaultExternalNullifier) - await semaphore.insertIdentity(identityCommitment) + await semaphore.deployed() - const signal = "0x111" - const nullifierHash = Semaphore.genNullifierHash(defaultExternalNullifier, identity.getNullifier()) + const leafIndex = 3 - const commitments = Object.assign([], identityCommitments) - commitments.push(identityCommitment) + for (let i = 0; i < leafIndex; i++) { + const tmpIdentity = new ZkIdentity() + const tmpCommitment = tmpIdentity.genIdentityCommitment() - const merkleProof = generateMerkleProof(20, ZERO_VALUE, 5, commitments, identityCommitment) - const witness = Semaphore.genWitness( - identity.getTrapdoor(), - identity.getNullifier(), - merkleProof, - defaultExternalNullifier, - signal - ) + identityCommitments.push(tmpCommitment) - const wasmFilePath = join("./build/snark/semaphore_js", "semaphore.wasm") - const finalZkeyPath = join("./build/snark", "semaphore_final.zkey") + await semaphore.insertIdentity(tmpCommitment) + } + }) - const fullProof = await Semaphore.genProof(witness, wasmFilePath, finalZkeyPath) - const solidityProof = Semaphore.packToSolidityProof(fullProof) + describe("Proof", () => { + it("Should generate full semaphore proof", async () => { + const identity = new ZkIdentity() + const identityCommitment = identity.genIdentityCommitment() - const packedProof = await semaphore.packProof(solidityProof.a, solidityProof.b, solidityProof.c) + await semaphore.insertIdentity(identityCommitment) - const preBroadcastCheck = await semaphore.preBroadcastCheck( - ethers.utils.hexlify(ethers.utils.toUtf8Bytes(signal)), - packedProof, - merkleProof.root, - nullifierHash, - genSignalHash(signal), - defaultExternalNullifier - ) + const signal = "0x111" + const nullifierHash = Semaphore.genNullifierHash(defaultExternalNullifier, identity.getNullifier()) - expect(preBroadcastCheck).to.be.true + const commitments = Object.assign([], identityCommitments) + commitments.push(identityCommitment) - const response = await semaphore.broadcastSignal( - ethers.utils.hexlify(ethers.utils.toUtf8Bytes(signal)), - packedProof, - merkleProof.root, - nullifierHash, - defaultExternalNullifier - ) + const merkleProof = generateMerkleProof(20, ZERO_VALUE, 5, commitments, identityCommitment) + const witness = Semaphore.genWitness( + identity.getTrapdoor(), + identity.getNullifier(), + merkleProof, + defaultExternalNullifier, + signal + ) - expect(response).to.not.equal(null) - }) + const wasmFilePath = join("./build/snark/semaphore_js", "semaphore.wasm") + const finalZkeyPath = join("./build/snark", "semaphore_final.zkey") + + const fullProof = await Semaphore.genProof(witness, wasmFilePath, finalZkeyPath) + const solidityProof = Semaphore.packToSolidityProof(fullProof) + + const packedProof = await semaphore.packProof(solidityProof.a, solidityProof.b, solidityProof.c) + + const preBroadcastCheck = await semaphore.preBroadcastCheck( + ethers.utils.hexlify(ethers.utils.toUtf8Bytes(signal)), + packedProof, + merkleProof.root, + nullifierHash, + genSignalHash(signal), + defaultExternalNullifier + ) + + expect(preBroadcastCheck).to.be.true + + const response = await semaphore.broadcastSignal( + ethers.utils.hexlify(ethers.utils.toUtf8Bytes(signal)), + packedProof, + merkleProof.root, + nullifierHash, + defaultExternalNullifier + ) + + expect(response).to.not.equal(null) }) + }) - describe("ExternalNullifier", () => { - it("Default nullifier should be active", async () => { - const isActive = await semaphore.isExternalNullifierActive(defaultExternalNullifier) - expect(isActive).to.be.true - }) - it("ExternalNullifier should be active after add", async () => { - await semaphore.addExternalNullifier(newExternalNullifier) - const isActive = await semaphore.isExternalNullifierActive(newExternalNullifier) - expect(isActive).to.be.true - }) - it("ExternalNullifier should not be active after deactivation", async () => { - await semaphore.deactivateExternalNullifier(newExternalNullifier) - const isActive = await semaphore.isExternalNullifierActive(newExternalNullifier) - expect(isActive).to.be.false - }) - it("ExternalNullifier should be active after reactivation", async () => { - await semaphore.reactivateExternalNullifier(newExternalNullifier) - const isActive = await semaphore.isExternalNullifierActive(newExternalNullifier) - expect(isActive).to.be.true - }) - it("Non owner should not be able to add nullifier", async () => { - const [_, addr1] = await ethers.getSigners() - - const newNullifier = genExternalNullifier("voting-3") - await expect(semaphore.connect(addr1).addExternalNullifier(newNullifier)).to.be.revertedWith( - "Ownable: caller is not the owner" - ) - }) - it("Non owner should be able to add nullifier after setPermissioning", async () => { - await semaphore.setPermissioning(true) - const newNullifier = genExternalNullifier("voting-3") - await semaphore.addExternalNullifier(newNullifier) - const isActive = await semaphore.isExternalNullifierActive(newNullifier) - expect(isActive).to.be.true - }) - it("Should fail to add already existing nullifier", async () => { - await expect(semaphore.addExternalNullifier(newExternalNullifier)).to.be.revertedWith( - "Semaphore: external nullifier already set" - ) - }) - it("Should return newExternalNullifier as next nullifier", async () => { - const nextNullifier = await semaphore.getNextExternalNullifier(defaultExternalNullifier) - expect(nextNullifier).to.be.equal(newExternalNullifier) - }) + describe("ExternalNullifier", () => { + it("Default nullifier should be active", async () => { + const isActive = await semaphore.isExternalNullifierActive(defaultExternalNullifier) + expect(isActive).to.be.true }) + it("ExternalNullifier should be active after add", async () => { + await semaphore.addExternalNullifier(newExternalNullifier) + const isActive = await semaphore.isExternalNullifierActive(newExternalNullifier) + expect(isActive).to.be.true + }) + it("ExternalNullifier should not be active after deactivation", async () => { + await semaphore.deactivateExternalNullifier(newExternalNullifier) + const isActive = await semaphore.isExternalNullifierActive(newExternalNullifier) + expect(isActive).to.be.false + }) + it("ExternalNullifier should be active after reactivation", async () => { + await semaphore.reactivateExternalNullifier(newExternalNullifier) + const isActive = await semaphore.isExternalNullifierActive(newExternalNullifier) + expect(isActive).to.be.true + }) + it("Non owner should not be able to add nullifier", async () => { + const [_, addr1] = await ethers.getSigners() + + const newNullifier = genExternalNullifier("voting-3") + await expect(semaphore.connect(addr1).addExternalNullifier(newNullifier)).to.be.revertedWith( + "Ownable: caller is not the owner" + ) + }) + it("Non owner should be able to add nullifier after setPermissioning", async () => { + await semaphore.setPermissioning(true) + const newNullifier = genExternalNullifier("voting-3") + await semaphore.addExternalNullifier(newNullifier) + const isActive = await semaphore.isExternalNullifierActive(newNullifier) + expect(isActive).to.be.true + }) + it("Should fail to add already existing nullifier", async () => { + await expect(semaphore.addExternalNullifier(newExternalNullifier)).to.be.revertedWith( + "Semaphore: external nullifier already set" + ) + }) + it("Should return newExternalNullifier as next nullifier", async () => { + const nextNullifier = await semaphore.getNextExternalNullifier(defaultExternalNullifier) + expect(nextNullifier).to.be.equal(newExternalNullifier) + }) + }) }) diff --git a/tsconfig.json b/tsconfig.json index ba064810..5d4ae97b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,16 +1,16 @@ { - "compilerOptions": { - "moduleResolution": "Node", - "noImplicitAny": true, - "resolveJsonModule": true, - "target": "ES2018", - "module": "CommonJS", - "strict": true, - "esModuleInterop": true, - "outDir": "dist", - "typeRoots": ["node_modules/@types", "types"] - }, - "include": ["scripts", "tasks", "test", "typechain", "types"], - "files": ["./hardhat.config.ts"], - "exclude": ["artifacts", "node_modules"] + "compilerOptions": { + "moduleResolution": "Node", + "noImplicitAny": true, + "resolveJsonModule": true, + "target": "ES2018", + "module": "CommonJS", + "strict": true, + "esModuleInterop": true, + "outDir": "dist", + "typeRoots": ["node_modules/@types", "types"] + }, + "include": ["scripts", "tasks", "test", "typechain", "types"], + "files": ["./hardhat.config.ts"], + "exclude": ["artifacts", "node_modules"] } diff --git a/types/snarkjs/index.d.ts b/types/snarkjs/index.d.ts index 172d268c..07725a88 100644 --- a/types/snarkjs/index.d.ts +++ b/types/snarkjs/index.d.ts @@ -1,57 +1,57 @@ -/** Declaration file generated by dts-gen */ - -declare module "snarkjs" { - export = snarkjs - - const snarkjs: { - groth16: { - exportSolidityCallData: any - fullProve: any - prove: any - verify: any - } - plonk: { - exportSolidityCallData: any - fullProve: any - prove: any - setup: any - verify: any - } - powersOfTau: { - beacon: any - challengeContribute: any - contribute: any - convert: any - exportChallenge: any - exportJson: any - importResponse: any - newAccumulator: any - preparePhase2: any - truncate: any - verify: any - } - r1cs: { - exportJson: any - info: any - print: any - } - wtns: { - calculate: any - debug: any - exportJson: any - } - zKey: { - beacon: any - bellmanContribute: any - contribute: any - exportBellman: any - exportJson: any - exportSolidityVerifier: any - exportVerificationKey: any - importBellman: any - newZKey: any - verifyFromInit: any - verifyFromR1cs: any - } - } -} +/** Declaration file generated by dts-gen */ + +declare module "snarkjs" { + export = snarkjs + + const snarkjs: { + groth16: { + exportSolidityCallData: any + fullProve: any + prove: any + verify: any + } + plonk: { + exportSolidityCallData: any + fullProve: any + prove: any + setup: any + verify: any + } + powersOfTau: { + beacon: any + challengeContribute: any + contribute: any + convert: any + exportChallenge: any + exportJson: any + importResponse: any + newAccumulator: any + preparePhase2: any + truncate: any + verify: any + } + r1cs: { + exportJson: any + info: any + print: any + } + wtns: { + calculate: any + debug: any + exportJson: any + } + zKey: { + beacon: any + bellmanContribute: any + contribute: any + exportBellman: any + exportJson: any + exportSolidityVerifier: any + exportVerificationKey: any + importBellman: any + newZKey: any + verifyFromInit: any + verifyFromR1cs: any + } + } +}