Skip to content

Commit 971b354

Browse files
committed
chore: update semaphore-protocol packages
Former-commit-id: a9bc129
1 parent fa1e86c commit 971b354

File tree

5 files changed

+83
-82
lines changed

5 files changed

+83
-82
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
"@commitlint/config-conventional": "^16.0.0",
4040
"@nomiclabs/hardhat-ethers": "^2.0.6",
4141
"@nomiclabs/hardhat-waffle": "^2.0.3",
42-
"@semaphore-protocol/identity": "0.2.1",
43-
"@semaphore-protocol/proof": "0.2.1",
42+
"@semaphore-protocol/group": "0.1.2",
43+
"@semaphore-protocol/identity": "0.2.2",
44+
"@semaphore-protocol/proof": "0.3.3",
4445
"@typechain/ethers-v5": "^10.0.0",
4546
"@typechain/hardhat": "^6.0.0",
4647
"@types/chai": "^4.3.0",
@@ -50,7 +51,6 @@
5051
"@types/rimraf": "^3.0.2",
5152
"@typescript-eslint/eslint-plugin": "^5.10.1",
5253
"@typescript-eslint/parser": "^5.10.1",
53-
"@zk-kit/incremental-merkle-tree": "^0.4.3",
5454
"chai": "^4.3.5",
5555
"circomlib": "^2.0.2",
5656
"circomlibjs": "^0.0.8",

test/Semaphore.ts

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1+
import { Group } from "@semaphore-protocol/group"
12
import { Identity } from "@semaphore-protocol/identity"
2-
import {
3-
createMerkleProof,
4-
createMerkleTree,
5-
FullProof,
6-
generateProof,
7-
packToSolidityProof,
8-
SolidityProof
9-
} from "@semaphore-protocol/proof"
3+
import { FullProof, generateProof, packToSolidityProof, SolidityProof } from "@semaphore-protocol/proof"
104
import { expect } from "chai"
115
import { constants, Signer, utils } from "ethers"
126
import { run } from "hardhat"
@@ -19,18 +13,18 @@ describe("Semaphore", () => {
1913
let signers: Signer[]
2014
let accounts: string[]
2115

22-
const depth = Number(process.env.TREE_DEPTH)
16+
const treeDepth = Number(process.env.TREE_DEPTH)
2317
const groupId = 1
2418
const members = createIdentityCommitments(3)
2519

2620
const wasmFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.wasm`
2721
const zkeyFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.zkey`
2822

2923
before(async () => {
30-
const { address: verifierAddress } = await run("deploy:verifier", { logs: false, depth })
24+
const { address: verifierAddress } = await run("deploy:verifier", { logs: false, depth: treeDepth })
3125
contract = await run("deploy:semaphore", {
3226
logs: false,
33-
verifiers: [{ merkleTreeDepth: depth, contractAddress: verifierAddress }]
27+
verifiers: [{ merkleTreeDepth: treeDepth, contractAddress: verifierAddress }]
3428
})
3529

3630
signers = await run("accounts", { logs: false })
@@ -45,9 +39,9 @@ describe("Semaphore", () => {
4539
})
4640

4741
it("Should create a group", async () => {
48-
const transaction = contract.connect(signers[1]).createGroup(groupId, depth, 0, accounts[1])
42+
const transaction = contract.connect(signers[1]).createGroup(groupId, treeDepth, 0, accounts[1])
4943

50-
await expect(transaction).to.emit(contract, "GroupCreated").withArgs(groupId, depth, 0)
44+
await expect(transaction).to.emit(contract, "GroupCreated").withArgs(groupId, treeDepth, 0)
5145
await expect(transaction)
5246
.to.emit(contract, "GroupAdminUpdated")
5347
.withArgs(groupId, constants.AddressZero, accounts[1])
@@ -99,23 +93,20 @@ describe("Semaphore", () => {
9993

10094
it("Should remove a member from an existing group", async () => {
10195
const groupId = 100
102-
const tree = createMerkleTree(depth, BigInt(0), [BigInt(1), BigInt(2), BigInt(3)])
96+
const group = new Group(treeDepth)
10397

104-
tree.delete(0)
98+
group.addMembers([BigInt(1), BigInt(2), BigInt(3)])
10599

106-
await contract.createGroup(groupId, depth, 0, accounts[0])
100+
group.removeMember(0)
101+
102+
await contract.createGroup(groupId, treeDepth, 0, accounts[0])
107103
await contract.addMember(groupId, BigInt(1))
108104
await contract.addMember(groupId, BigInt(2))
109105
await contract.addMember(groupId, BigInt(3))
110106

111-
const { siblings, pathIndices, root } = tree.createProof(0)
107+
const { siblings, pathIndices, root } = group.generateProofOfMembership(0)
112108

113-
const transaction = contract.removeMember(
114-
groupId,
115-
BigInt(1),
116-
siblings.map((s) => s[0]),
117-
pathIndices
118-
)
109+
const transaction = contract.removeMember(groupId, BigInt(1), siblings, pathIndices)
119110

120111
await expect(transaction).to.emit(contract, "MemberRemoved").withArgs(groupId, BigInt(1), root)
121112
})
@@ -125,8 +116,10 @@ describe("Semaphore", () => {
125116
const signal = "Hello world"
126117
const bytes32Signal = utils.formatBytes32String(signal)
127118
const identity = new Identity("0")
128-
const identityCommitment = identity.generateCommitment()
129-
const merkleProof = createMerkleProof(depth, BigInt(0), members, identityCommitment)
119+
120+
const group = new Group(treeDepth)
121+
122+
group.addMembers(members)
130123

131124
let fullProof: FullProof
132125
let solidityProof: SolidityProof
@@ -135,7 +128,7 @@ describe("Semaphore", () => {
135128
await contract.addMember(groupId, members[1])
136129
await contract.addMember(groupId, members[2])
137130

138-
fullProof = await generateProof(identity, merkleProof, merkleProof.root, signal, {
131+
fullProof = await generateProof(identity, group, group.root, signal, {
139132
wasmFilePath,
140133
zkeyFilePath
141134
})

test/SemaphoreVoting.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Identity } from "@semaphore-protocol/identity"
2+
import { Group } from "@semaphore-protocol/group"
23
import {
3-
createMerkleProof,
44
generateNullifierHash,
55
generateProof,
66
packToSolidityProof,
@@ -18,7 +18,7 @@ describe("SemaphoreVoting", () => {
1818
let accounts: Signer[]
1919
let coordinator: string
2020

21-
const depth = Number(process.env.TREE_DEPTH)
21+
const treeDepth = Number(process.env.TREE_DEPTH)
2222
const pollIds = [BigInt(1), BigInt(2), BigInt(3)]
2323
const encryptionKey = BigInt(0)
2424
const decryptionKey = BigInt(0)
@@ -27,7 +27,7 @@ describe("SemaphoreVoting", () => {
2727
const zkeyFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.zkey`
2828

2929
before(async () => {
30-
const { address: verifierAddress } = await run("deploy:verifier", { logs: false, depth })
30+
const { address: verifierAddress } = await run("deploy:verifier", { logs: false, depth: treeDepth })
3131
contract = await run("deploy:semaphore-voting", { logs: false, verifier: verifierAddress })
3232
accounts = await ethers.getSigners()
3333
coordinator = await accounts[1].getAddress()
@@ -44,20 +44,20 @@ describe("SemaphoreVoting", () => {
4444
const transaction = contract.createPoll(
4545
BigInt("21888242871839275222246405745257275088548364400416034343698204186575808495618"),
4646
coordinator,
47-
depth
47+
treeDepth
4848
)
4949

5050
await expect(transaction).to.be.revertedWith("SemaphoreGroups: group id must be < SNARK_SCALAR_FIELD")
5151
})
5252

5353
it("Should create a poll", async () => {
54-
const transaction = contract.createPoll(pollIds[0], coordinator, depth)
54+
const transaction = contract.createPoll(pollIds[0], coordinator, treeDepth)
5555

5656
await expect(transaction).to.emit(contract, "PollCreated").withArgs(pollIds[0], coordinator)
5757
})
5858

5959
it("Should not create a poll if it already exists", async () => {
60-
const transaction = contract.createPoll(pollIds[0], coordinator, depth)
60+
const transaction = contract.createPoll(pollIds[0], coordinator, treeDepth)
6161

6262
await expect(transaction).to.be.revertedWith("SemaphoreGroups: group already exists")
6363
})
@@ -85,7 +85,7 @@ describe("SemaphoreVoting", () => {
8585

8686
describe("# addVoter", () => {
8787
before(async () => {
88-
await contract.createPoll(pollIds[1], coordinator, depth)
88+
await contract.createPoll(pollIds[1], coordinator, treeDepth)
8989
})
9090

9191
it("Should not add a voter if the caller is not the coordinator", async () => {
@@ -131,19 +131,22 @@ describe("SemaphoreVoting", () => {
131131
describe("# castVote", () => {
132132
const identity = new Identity("test")
133133
const identityCommitment = identity.generateCommitment()
134-
const merkleProof = createMerkleProof(depth, BigInt(0), [identityCommitment, BigInt(1)], identityCommitment)
135134
const vote = "1"
136135
const bytes32Vote = utils.formatBytes32String(vote)
137136

137+
const group = new Group(treeDepth)
138+
139+
group.addMembers([identityCommitment, BigInt(1)])
140+
138141
let solidityProof: SolidityProof
139142
let publicSignals: PublicSignals
140143

141144
before(async () => {
142145
await contract.connect(accounts[1]).addVoter(pollIds[1], BigInt(1))
143146
await contract.connect(accounts[1]).startPoll(pollIds[1], encryptionKey)
144-
await contract.createPoll(pollIds[2], coordinator, depth)
147+
await contract.createPoll(pollIds[2], coordinator, treeDepth)
145148

146-
const fullProof = await generateProof(identity, merkleProof, pollIds[1], vote, {
149+
const fullProof = await generateProof(identity, group, pollIds[1], vote, {
147150
wasmFilePath,
148151
zkeyFilePath
149152
})

test/SemaphoreWhistleblowing.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { Group } from "@semaphore-protocol/group"
12
import { Identity } from "@semaphore-protocol/identity"
23
import {
3-
createMerkleProof,
44
generateNullifierHash,
55
generateProof,
66
packToSolidityProof,
@@ -18,14 +18,14 @@ describe("SemaphoreWhistleblowing", () => {
1818
let accounts: Signer[]
1919
let editor: string
2020

21-
const depth = Number(process.env.TREE_DEPTH)
21+
const treeDepth = Number(process.env.TREE_DEPTH)
2222
const entityIds = [BigInt(1), BigInt(2)]
2323

2424
const wasmFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.wasm`
2525
const zkeyFilePath = `${config.paths.build["snark-artifacts"]}/semaphore.zkey`
2626

2727
before(async () => {
28-
const { address: verifierAddress } = await run("deploy:verifier", { logs: false, depth })
28+
const { address: verifierAddress } = await run("deploy:verifier", { logs: false, depth: treeDepth })
2929
contract = await run("deploy:semaphore-whistleblowing", { logs: false, verifier: verifierAddress })
3030
accounts = await ethers.getSigners()
3131
editor = await accounts[1].getAddress()
@@ -42,20 +42,20 @@ describe("SemaphoreWhistleblowing", () => {
4242
const transaction = contract.createEntity(
4343
BigInt("21888242871839275222246405745257275088548364400416034343698204186575808495618"),
4444
editor,
45-
depth
45+
treeDepth
4646
)
4747

4848
await expect(transaction).to.be.revertedWith("SemaphoreGroups: group id must be < SNARK_SCALAR_FIELD")
4949
})
5050

5151
it("Should create an entity", async () => {
52-
const transaction = contract.createEntity(entityIds[0], editor, depth)
52+
const transaction = contract.createEntity(entityIds[0], editor, treeDepth)
5353

5454
await expect(transaction).to.emit(contract, "EntityCreated").withArgs(entityIds[0], editor)
5555
})
5656

5757
it("Should not create a entity if it already exists", async () => {
58-
const transaction = contract.createEntity(entityIds[0], editor, depth)
58+
const transaction = contract.createEntity(entityIds[0], editor, treeDepth)
5959

6060
await expect(transaction).to.be.revertedWith("SemaphoreGroups: group already exists")
6161
})
@@ -97,12 +97,11 @@ describe("SemaphoreWhistleblowing", () => {
9797
it("Should not remove a whistleblower if the caller is not the editor", async () => {
9898
const identity = new Identity()
9999
const identityCommitment = identity.generateCommitment()
100-
const { siblings, pathIndices } = createMerkleProof(
101-
depth,
102-
BigInt(0),
103-
[identityCommitment],
104-
identityCommitment
105-
)
100+
const group = new Group(treeDepth)
101+
102+
group.addMember(identityCommitment)
103+
104+
const { siblings, pathIndices } = group.generateProofOfMembership(0)
106105

107106
const transaction = contract.removeWhistleblower(entityIds[0], identityCommitment, siblings, pathIndices)
108107

@@ -112,12 +111,11 @@ describe("SemaphoreWhistleblowing", () => {
112111
it("Should remove a whistleblower from an existing entity", async () => {
113112
const identity = new Identity("test")
114113
const identityCommitment = identity.generateCommitment()
115-
const { siblings, pathIndices } = createMerkleProof(
116-
depth,
117-
BigInt(0),
118-
[identityCommitment],
119-
identityCommitment
120-
)
114+
const group = new Group(treeDepth)
115+
116+
group.addMember(identityCommitment)
117+
118+
const { siblings, pathIndices } = group.generateProofOfMembership(0)
121119

122120
const transaction = contract
123121
.connect(accounts[1])
@@ -136,19 +134,22 @@ describe("SemaphoreWhistleblowing", () => {
136134
describe("# publishLeak", () => {
137135
const identity = new Identity("test")
138136
const identityCommitment = identity.generateCommitment()
139-
const merkleProof = createMerkleProof(depth, BigInt(0), [identityCommitment, BigInt(1)], identityCommitment)
140137
const leak = "leak"
141138
const bytes32Leak = utils.formatBytes32String(leak)
142139

140+
const group = new Group(treeDepth)
141+
142+
group.addMembers([identityCommitment, BigInt(1)])
143+
143144
let solidityProof: SolidityProof
144145
let publicSignals: PublicSignals
145146

146147
before(async () => {
147-
await contract.createEntity(entityIds[1], editor, depth)
148+
await contract.createEntity(entityIds[1], editor, treeDepth)
148149
await contract.connect(accounts[1]).addWhistleblower(entityIds[1], identityCommitment)
149150
await contract.connect(accounts[1]).addWhistleblower(entityIds[1], BigInt(1))
150151

151-
const fullProof = await generateProof(identity, merkleProof, entityIds[1], leak, {
152+
const fullProof = await generateProof(identity, group, entityIds[1], leak, {
152153
wasmFilePath,
153154
zkeyFilePath
154155
})

0 commit comments

Comments
 (0)