Skip to content

Commit

Permalink
Merge pull request #731 from semaphore-protocol/refactor/bigint-type
Browse files Browse the repository at this point in the history
Use native `bigint` type
  • Loading branch information
cedoor authored Mar 28, 2024
2 parents 7fd8d82 + fa4d986 commit 4adc328
Show file tree
Hide file tree
Showing 13 changed files with 342 additions and 350 deletions.
8 changes: 4 additions & 4 deletions apps/docs/versioned_docs/version-V4-beta/guides/groups.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ You can also initialize a group with multiple members by passing the list of ide

```ts
const members = [
"11237622825477336339577122413451117718539783476837539122310492284566644730311",
"9332663527862709610616009715800254142772436825222910251631161087138559093425",
"13255821893820536903335282929376140649646180444238593676033702344407594536519"
11237622825477336339577122413451117718539783476837539122310492284566644730311n,
9332663527862709610616009715800254142772436825222910251631161087138559093425n,
13255821893820536903335282929376140649646180444238593676033702344407594536519n
]

const group2 = new Group(members)
Expand Down Expand Up @@ -117,7 +117,7 @@ group.removeMember(0)
To update members in a group, pass the member index and the new value to the `updateMember` method. For example:

```ts
group.updateMember(0, 2)
group.updateMember(0, 2n)
```

:::caution
Expand Down
34 changes: 16 additions & 18 deletions packages/contracts/test/Semaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe("Semaphore", () => {

describe("# addMember", () => {
it("Should not add a member if the caller is not the group admin", async () => {
const member = BigInt(2)
const member = 2n

const transaction = semaphoreContract.connect(accounts[1]).addMember(groupId, member)

Expand All @@ -149,7 +149,7 @@ describe("Semaphore", () => {

describe("# addMembers", () => {
it("Should not add members if the caller is not the group admin", async () => {
const members = [BigInt(1), BigInt(2), BigInt(3)]
const members = [1n, 2n, 3n]

const transaction = semaphoreContract.connect(accounts[1]).addMembers(groupId, members)

Expand All @@ -161,7 +161,7 @@ describe("Semaphore", () => {

it("Should add new members to an existing group", async () => {
const groupId = 3
const members = [BigInt(1), BigInt(2), BigInt(3)]
const members = [1n, 2n, 3n]
const group = new Group()

group.addMembers(members)
Expand All @@ -178,7 +178,7 @@ describe("Semaphore", () => {

describe("# updateMember", () => {
it("Should not update a member if the caller is not the group admin", async () => {
const member = BigInt(2)
const member = 2n

const transaction = semaphoreContract.connect(accounts[1]).updateMember(groupId, member, 1, [0, 1])

Expand All @@ -190,29 +190,27 @@ describe("Semaphore", () => {

it("Should update a member from an existing group", async () => {
const groupId = 4
const members = [BigInt(1), BigInt(2), BigInt(3)]
const members = [1n, 2n, 3n]
const group = new Group()

group.addMembers(members)

group.updateMember(0, BigInt(4))
group.updateMember(0, 4n)

await semaphoreContract["createGroup(address)"](accountAddresses[0])
await semaphoreContract.addMembers(groupId, members)

const { siblings, root } = group.generateMerkleProof(0)

const transaction = semaphoreContract.updateMember(groupId, BigInt(1), BigInt(4), siblings)
const transaction = semaphoreContract.updateMember(groupId, 1n, 4n, siblings)

await expect(transaction)
.to.emit(semaphoreContract, "MemberUpdated")
.withArgs(groupId, 0, BigInt(1), BigInt(4), root)
await expect(transaction).to.emit(semaphoreContract, "MemberUpdated").withArgs(groupId, 0, 1n, 4n, root)
})
})

describe("# removeMember", () => {
it("Should not remove a member if the caller is not the group admin", async () => {
const member = BigInt(2)
const member = 2n

const transaction = semaphoreContract.connect(accounts[1]).removeMember(groupId, member, [0, 1])

Expand All @@ -224,7 +222,7 @@ describe("Semaphore", () => {

it("Should remove a member from an existing group", async () => {
const groupId = 5
const members = [BigInt(1), BigInt(2), BigInt(3)]
const members = [1n, 2n, 3n]
const group = new Group()

group.addMembers(members)
Expand All @@ -236,9 +234,9 @@ describe("Semaphore", () => {

const { siblings, root } = group.generateMerkleProof(2)

const transaction = semaphoreContract.removeMember(groupId, BigInt(3), siblings)
const transaction = semaphoreContract.removeMember(groupId, 3n, siblings)

await expect(transaction).to.emit(semaphoreContract, "MemberRemoved").withArgs(groupId, 2, BigInt(3), root)
await expect(transaction).to.emit(semaphoreContract, "MemberRemoved").withArgs(groupId, 2, 3n, root)
})
})

Expand Down Expand Up @@ -272,7 +270,7 @@ describe("Semaphore", () => {

await semaphoreContract.addMembers(groupId, members)

proof = await generateProof(identity, group, message, group.root as string, merkleTreeDepth)
proof = await generateProof(identity, group, message, group.root, merkleTreeDepth)
})

it("Should not verify a proof if the group does not exist", async () => {
Expand Down Expand Up @@ -303,7 +301,7 @@ describe("Semaphore", () => {

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

const proof = await generateProof(identity, group, message, group.root as string, merkleTreeDepth)
const proof = await generateProof(identity, group, message, group.root, merkleTreeDepth)

const transaction = semaphoreContract.verifyProof(groupId, proof)

Expand Down Expand Up @@ -358,12 +356,12 @@ describe("Semaphore", () => {
await semaphoreContract.addMembers(groupId, [members[1], members[2]])
await semaphoreContract.addMember(groupOneMemberId, members[0])

proof = await generateProof(identity, group, message, group.root as string, merkleTreeDepth)
proof = await generateProof(identity, group, message, group.root, merkleTreeDepth)
proofOneMember = await generateProof(
identity,
groupOneMember,
message,
groupOneMember.root as string,
groupOneMember.root,
merkleTreeDepth
)
})
Expand Down
18 changes: 9 additions & 9 deletions packages/group/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const { commitment } = new Identity()

group.addMember(commitment)

// "12989101133047504182892154686643420754368236204022364847543591045056549053997"
// 12989101133047504182892154686643420754368236204022364847543591045056549053997n
console.log(group.members[0])
```

Expand All @@ -121,31 +121,31 @@ group.addMembers([identity1.commitment, identity2.commitment])
```typescript
import { Group } from "@semaphore-protocol/group"

const group = new Group([1, 3])
const group = new Group([1n, 3n])

group.updateMember(0, 2)

console.log(group.members[0]) // "2"
console.log(group.members[0]) // "2n"
```

\# **removeMember**(index: _number_)

```typescript
import { Group } from "@semaphore-protocol/group"

const group = new Group([1, 3])
const group = new Group([1n, 3n])

group.removeMember(0)

console.log(group.members[0]) // "0"
console.log(group.members[0]) // 0n
```

\# **indexOf**(member: _BigNumberish_): _number_

```typescript
import { Group } from "@semaphore-protocol/group"

const group = new Group([1])
const group = new Group([1n])

const index = group.indexOf(1)

Expand All @@ -157,7 +157,7 @@ console.log(index) // 0
```typescript
import { Group } from "@semaphore-protocol/group"

const group = new Group([1, 3])
const group = new Group([1n, 3n])

const proof = group.generateMerkleProof(0)

Expand All @@ -177,7 +177,7 @@ console.log(proof)
```typescript
import { Group } from "@semaphore-protocol/group"

const group = new Group([1, 2, 3])
const group = new Group([1n, 2n, 3n])

const exportedGroup = group.export()

Expand All @@ -192,7 +192,7 @@ console.log(exportedGroup)
```typescript
import { Group } from "@semaphore-protocol/group"

const group1 = new Group([1, 2, 3])
const group1 = new Group([1n, 2n, 3n])

const exportedGroup = group.export()

Expand Down
3 changes: 2 additions & 1 deletion packages/group/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"rollup-plugin-cleanup": "^3.2.1"
},
"dependencies": {
"@zk-kit/imt": "2.0.0-beta.2"
"@zk-kit/imt": "2.0.0-beta.2",
"@zk-kit/utils": "1.0.0-beta"
}
}
143 changes: 0 additions & 143 deletions packages/group/src/group.ts

This file was deleted.

Loading

0 comments on commit 4adc328

Please sign in to comment.