Skip to content

Commit

Permalink
refactor(contracts): update tests, tasks and scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Jan 16, 2024
1 parent 937f2ee commit 8a8c21c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/contracts/scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { readFileSync, writeFileSync } from "fs"

type DeployedContracts = {
SemaphoreVerifier: string
Poseidon: string
Semaphore: string
Verifiers: string[]
}

export function getDeployedContracts(network: string | undefined): DeployedContracts | null {
Expand Down
7 changes: 5 additions & 2 deletions packages/contracts/scripts/verify-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ async function main() {
const deployedContracts = getDeployedContracts(hardhatArguments.network)

if (deployedContracts) {
for (const Verifier of deployedContracts.Verifiers) {
await verify(Verifier)
}

await verify(deployedContracts.Poseidon)
await verify(deployedContracts.SemaphoreVerifier)
await verify(deployedContracts.Semaphore, [deployedContracts.SemaphoreVerifier])
await verify(deployedContracts.Semaphore, [deployedContracts.Verifiers])
}
}

Expand Down
26 changes: 15 additions & 11 deletions packages/contracts/tasks/deploy-semaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ import { task, types } from "hardhat/config"
import { saveDeployedContracts } from "../scripts/utils"

task("deploy:semaphore", "Deploy a Semaphore contract")
.addOptionalParam<boolean>("semaphoreVerifier", "SemaphoreVerifier contract address", undefined, types.string)
.addOptionalParam<boolean>("verifiers", "Verifier contract addresses", undefined, types.json)
.addOptionalParam<boolean>("poseidon", "Poseidon library address", undefined, types.string)
.addOptionalParam<boolean>("logs", "Print the logs", true, types.boolean)
.setAction(
async (
{ logs, semaphoreVerifier: semaphoreVerifierAddress, poseidon: poseidonAddress },
{ logs, verifiers: verifierAddresses, poseidon: poseidonAddress },
{ ethers, hardhatArguments }
): Promise<any> => {
if (!semaphoreVerifierAddress) {
const SemaphoreVerifierFactory = await ethers.getContractFactory("SemaphoreVerifier")
if (!verifierAddresses) {
verifierAddresses = []

const semaphoreVerifier = await SemaphoreVerifierFactory.deploy()
for (let i = 0; i < 12; i += 1) {
const VerifierFactory = await ethers.getContractFactory(`Verifier${i + 1}`)

semaphoreVerifierAddress = await semaphoreVerifier.getAddress()
const verifier = await VerifierFactory.deploy()

if (logs) {
console.info(`SemaphoreVerifier contract has been deployed to: ${semaphoreVerifierAddress}`)
verifierAddresses.push(await verifier.getAddress())

if (logs) {
console.info(`SemaphoreVerifier contract has been deployed to: ${verifierAddresses[i]}`)
}
}
}

Expand All @@ -39,7 +43,7 @@ task("deploy:semaphore", "Deploy a Semaphore contract")
}
})

const semaphore = await SemaphoreFactory.deploy(semaphoreVerifierAddress)
const semaphore = await SemaphoreFactory.deploy(verifierAddresses)

const semaphoreAddress = await semaphore.getAddress()

Expand All @@ -48,14 +52,14 @@ task("deploy:semaphore", "Deploy a Semaphore contract")
}

saveDeployedContracts(hardhatArguments.network, {
SemaphoreVerifier: semaphoreVerifierAddress,
Verifiers: verifierAddresses,
Poseidon: poseidonAddress,
Semaphore: semaphoreAddress
})

return {
semaphore,
semaphoreVerifierAddress,
verifierAddresses,
poseidonAddress
}
}
Expand Down
30 changes: 24 additions & 6 deletions packages/contracts/test/Semaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe("Semaphore", () => {
let signers: Signer[]
let accounts: string[]

const treeDepth = 12

const groupId = 1
const members = Array.from({ length: 3 }, (_, i) => new Identity(i)).map(({ commitment }) => commitment)

Expand Down Expand Up @@ -220,17 +222,25 @@ describe("Semaphore", () => {

await semaphoreContract.addMembers(groupId, members)

fullProof = await generateProof(identity, group, message, group.root as string, 10)
fullProof = await generateProof(identity, group, message, group.root as string, treeDepth)
})

it("Should not verify a proof if the group does not exist", async () => {
const transaction = semaphoreContract.verifyProof(11, 1, 0, message, 0, [0, 0, 0, 0, 0, 0, 0, 0])
const transaction = semaphoreContract.verifyProof(11, treeDepth, 1, 0, message, 0, [0, 0, 0, 0, 0, 0, 0, 0])

await expect(transaction).to.be.revertedWithCustomError(semaphoreContract, "Semaphore__GroupDoesNotExist")
})

it("Should not verify a proof if the Merkle tree root is not part of the group", async () => {
const transaction = semaphoreContract.verifyProof(groupId, 1, 0, message, 0, [0, 0, 0, 0, 0, 0, 0, 0])
const transaction = semaphoreContract.verifyProof(
groupId,
treeDepth,
1,
0,
message,
0,
[0, 0, 0, 0, 0, 0, 0, 0]
)

await expect(transaction).to.be.revertedWithCustomError(
semaphoreContract,
Expand All @@ -241,6 +251,7 @@ describe("Semaphore", () => {
it("Should verify a proof for an onchain group", async () => {
const validProof = await semaphoreContract.verifyProof(
groupId,
treeDepth,
fullProof.merkleRoot,
fullProof.nullifier,
fullProof.message,
Expand All @@ -258,10 +269,11 @@ describe("Semaphore", () => {

group.addMembers([members[0], members[1]])

const fullProof = await generateProof(identity, group, message, group.root as string, 10)
const fullProof = await generateProof(identity, group, message, group.root as string, treeDepth)

const transaction = semaphoreContract.verifyProof(
groupId,
treeDepth,
fullProof.merkleRoot,
fullProof.nullifier,
fullProof.message,
Expand Down Expand Up @@ -296,19 +308,20 @@ describe("Semaphore", () => {
await semaphoreContract.addMembers(groupId, [members[1], members[2]])
await semaphoreContract.addMember(groupOneMemberId, members[0])

fullProof = await generateProof(identity, group, message, group.root as string, 10)
fullProof = await generateProof(identity, group, message, group.root as string, treeDepth)
fullProofOneMember = await generateProof(
identity,
groupOneMember,
message,
groupOneMember.root as string,
10
treeDepth
)
})

it("Should throw an exception if the proof is not valid", async () => {
const transaction = semaphoreContract.validateProof(
groupId,
treeDepth,
fullProof.merkleRoot,
fullProof.nullifier,
fullProof.message,
Expand All @@ -322,6 +335,7 @@ describe("Semaphore", () => {
it("Should validate a proof for an onchain group with one member correctly", async () => {
const transaction = semaphoreContract.validateProof(
groupOneMemberId,
treeDepth,
fullProofOneMember.merkleRoot,
fullProofOneMember.nullifier,
fullProofOneMember.message,
Expand All @@ -333,6 +347,7 @@ describe("Semaphore", () => {
.to.emit(semaphoreContract, "ProofValidated")
.withArgs(
groupOneMemberId,
treeDepth,
fullProofOneMember.merkleRoot,
fullProofOneMember.nullifier,
fullProofOneMember.message,
Expand All @@ -344,6 +359,7 @@ describe("Semaphore", () => {
it("Should validate a proof for an onchain group with more than one member correctly", async () => {
const transaction = semaphoreContract.validateProof(
groupId,
treeDepth,
fullProof.merkleRoot,
fullProof.nullifier,
fullProof.message,
Expand All @@ -355,6 +371,7 @@ describe("Semaphore", () => {
.to.emit(semaphoreContract, "ProofValidated")
.withArgs(
groupId,
treeDepth,
fullProof.merkleRoot,
fullProof.nullifier,
fullProof.message,
Expand All @@ -366,6 +383,7 @@ describe("Semaphore", () => {
it("Should not validate the same proof for an onchain group twice", async () => {
const transaction = semaphoreContract.validateProof(
groupId,
treeDepth,
fullProof.merkleRoot,
fullProof.nullifier,
fullProof.message,
Expand Down

0 comments on commit 8a8c21c

Please sign in to comment.