style: fix syntax with prettier

Former-commit-id: f86391ad65
This commit is contained in:
cedoor
2022-01-27 11:24:48 +01:00
parent 82c4dea962
commit 5abfed2b77
8 changed files with 290 additions and 290 deletions

View File

@@ -1,3 +1,3 @@
{
"**/*.{js,ts}": ["prettier --write", "eslint --fix"]
"**/*.{js,ts}": ["prettier --write", "eslint --fix"]
}

View File

@@ -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'];
await deploy("PoseidonT3", deployTx(2))
await deploy("PoseidonT6", deployTx(5))
}
module.exports.tags = ["Poseidon", "complete"]

View File

@@ -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"]

View File

@@ -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

View File

@@ -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)
})

View File

@@ -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)
})
})
})

View File

@@ -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"]
}

View File

@@ -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
}
}
}