@semaphore-protocol/contracts 0.1.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ <p align="center">
2
+ <h1 align="center">
3
+ Semaphore contracts
4
+ </h1>
5
+ <p align="center">Semaphore contracts to manage groups and broadcast anonymous signals.</p>
6
+ </p>
7
+
8
+ <p align="center">
9
+ <a href="https://github.com/semaphore-protocol">
10
+ <img src="https://img.shields.io/badge/project-semaphore-blue.svg?style=flat-square">
11
+ </a>
12
+ <a href="https://github.com/semaphore-protocol/semaphore/blob/main/LICENSE">
13
+ <img alt="Github license" src="https://img.shields.io/github/license/semaphore-protocol/semaphore.svg?style=flat-square">
14
+ </a>
15
+ <a href="https://www.npmjs.com/package/@semaphore-protocol/contracts">
16
+ <img alt="NPM version" src="https://img.shields.io/npm/v/@semaphore-protocol/contracts?style=flat-square" />
17
+ </a>
18
+ <a href="https://npmjs.org/package/@semaphore-protocol/contracts">
19
+ <img alt="Downloads" src="https://img.shields.io/npm/dm/@semaphore-protocol/contracts.svg?style=flat-square" />
20
+ </a>
21
+ </p>
22
+
23
+ <div align="center">
24
+ <h4>
25
+ <a href="https://github.com/semaphore-protocol/semaphore/blob/main/CONTRIBUTING.md">
26
+ 👥 Contributing
27
+ </a>
28
+ <span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
29
+ <a href="https://github.com/semaphore-protocol/semaphore/blob/main/CODE_OF_CONDUCT.md">
30
+ 🤝 Code of conduct
31
+ </a>
32
+ <span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
33
+ <a href="https://github.com/semaphore-protocol/semaphore/contribute">
34
+ 🔎 Issues
35
+ </a>
36
+ <span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
37
+ <a href="https://t.me/joinchat/B-PQx1U3GtAh--Z4Fwo56A">
38
+ 🗣️ Chat &amp; Support
39
+ </a>
40
+ </h4>
41
+ </div>
42
+
43
+ To learn more about contracts visit [semaphore.appliedzkp.org](https://semaphore.appliedzkp.org/docs/technical-reference/contracts).
44
+
45
+ ---
46
+
47
+ ## 🛠 Install
48
+
49
+ ### npm or yarn
50
+
51
+ Install the `@semaphore-protocol/contracts` package with npm:
52
+
53
+ ```bash
54
+ npm i @semaphore-protocol/contracts
55
+ ```
56
+
57
+ or yarn:
58
+
59
+ ```bash
60
+ yarn add @semaphore-protocol/contracts
61
+ ```
package/Semaphore.sol CHANGED
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: MIT
2
- pragma solidity ^0.8.4;
2
+ pragma solidity 0.8.4;
3
3
 
4
4
  import "./interfaces/ISemaphore.sol";
5
5
  import "./interfaces/IVerifier.sol";
@@ -8,89 +8,149 @@ import "./base/SemaphoreGroups.sol";
8
8
 
9
9
  /// @title Semaphore
10
10
  contract Semaphore is ISemaphore, SemaphoreCore, SemaphoreGroups {
11
- /// @dev Gets a tree depth and returns its verifier address.
12
- mapping(uint8 => IVerifier) public verifiers;
13
-
14
- /// @dev Gets a group id and returns the group admin address.
15
- mapping(uint256 => address) public groupAdmins;
16
-
17
- /// @dev Checks if the group admin is the transaction sender.
18
- /// @param groupId: Id of the group.
19
- modifier onlyGroupAdmin(uint256 groupId) {
20
- require(groupAdmins[groupId] == _msgSender(), "Semaphore: caller is not the group admin");
21
- _;
22
- }
23
-
24
- /// @dev Checks if there is a verifier for the given tree depth.
25
- /// @param depth: Depth of the tree.
26
- modifier onlySupportedDepth(uint8 depth) {
27
- require(address(verifiers[depth]) != address(0), "Semaphore: tree depth is not supported");
28
- _;
29
- }
30
-
31
- /// @dev Initializes the Semaphore verifiers used to verify the user's ZK proofs.
32
- /// @param _verifiers: List of Semaphore verifiers (address and related Merkle tree depth).
33
- constructor(Verifier[] memory _verifiers) {
34
- for (uint8 i = 0; i < _verifiers.length; i++) {
35
- verifiers[_verifiers[i].merkleTreeDepth] = IVerifier(_verifiers[i].contractAddress);
11
+ /// @dev Gets a tree depth and returns its verifier address.
12
+ mapping(uint256 => IVerifier) public verifiers;
13
+
14
+ /// @dev Gets a group id and returns the group admin address.
15
+ mapping(uint256 => address) public groupAdmins;
16
+
17
+ /// @dev Gets a group id and returns data to check if a Merkle root is expired.
18
+ mapping(uint256 => MerkleTreeExpiry) public merkleTreeExpiries;
19
+
20
+ /// @dev Checks if the group admin is the transaction sender.
21
+ /// @param groupId: Id of the group.
22
+ modifier onlyGroupAdmin(uint256 groupId) {
23
+ if (groupAdmins[groupId] != _msgSender()) {
24
+ revert Semaphore__CallerIsNotTheGroupAdmin();
25
+ }
26
+ _;
27
+ }
28
+
29
+ /// @dev Checks if there is a verifier for the given tree depth.
30
+ /// @param merkleTreeDepth: Depth of the tree.
31
+ modifier onlySupportedMerkleTreeDepth(uint256 merkleTreeDepth) {
32
+ if (address(verifiers[merkleTreeDepth]) == address(0)) {
33
+ revert Semaphore__MerkleTreeDepthIsNotSupported();
34
+ }
35
+ _;
36
+ }
37
+
38
+ /// @dev Initializes the Semaphore verifiers used to verify the user's ZK proofs.
39
+ /// @param _verifiers: List of Semaphore verifiers (address and related Merkle tree depth).
40
+ constructor(Verifier[] memory _verifiers) {
41
+ for (uint8 i = 0; i < _verifiers.length; ) {
42
+ verifiers[_verifiers[i].merkleTreeDepth] = IVerifier(_verifiers[i].contractAddress);
43
+
44
+ unchecked {
45
+ ++i;
46
+ }
47
+ }
48
+ }
49
+
50
+ /// @dev See {ISemaphore-createGroup}.
51
+ function createGroup(
52
+ uint256 groupId,
53
+ uint256 merkleTreeDepth,
54
+ uint256 zeroValue,
55
+ address admin
56
+ ) external override onlySupportedMerkleTreeDepth(merkleTreeDepth) {
57
+ _createGroup(groupId, merkleTreeDepth, zeroValue);
58
+
59
+ groupAdmins[groupId] = admin;
60
+ merkleTreeExpiries[groupId].rootDuration = 1 hours;
61
+
62
+ emit GroupAdminUpdated(groupId, address(0), admin);
63
+ }
64
+
65
+ /// @dev See {ISemaphore-createGroup}.
66
+ function createGroup(
67
+ uint256 groupId,
68
+ uint256 merkleTreeDepth,
69
+ uint256 zeroValue,
70
+ address admin,
71
+ uint256 merkleTreeRootDuration
72
+ ) external override onlySupportedMerkleTreeDepth(merkleTreeDepth) {
73
+ _createGroup(groupId, merkleTreeDepth, zeroValue);
74
+
75
+ groupAdmins[groupId] = admin;
76
+ merkleTreeExpiries[groupId].rootDuration = merkleTreeRootDuration;
77
+
78
+ emit GroupAdminUpdated(groupId, address(0), admin);
79
+ }
80
+
81
+ /// @dev See {ISemaphore-updateGroupAdmin}.
82
+ function updateGroupAdmin(uint256 groupId, address newAdmin) external override onlyGroupAdmin(groupId) {
83
+ groupAdmins[groupId] = newAdmin;
84
+
85
+ emit GroupAdminUpdated(groupId, _msgSender(), newAdmin);
86
+ }
87
+
88
+ /// @dev See {ISemaphore-addMember}.
89
+ function addMember(uint256 groupId, uint256 identityCommitment) external override onlyGroupAdmin(groupId) {
90
+ _addMember(groupId, identityCommitment);
91
+
92
+ uint256 merkleTreeRoot = getMerkleTreeRoot(groupId);
93
+
94
+ merkleTreeExpiries[groupId].rootCreationDates[merkleTreeRoot] = block.timestamp;
95
+ }
96
+
97
+ /// @dev See {ISemaphore-updateMember}.
98
+ function updateMember(
99
+ uint256 groupId,
100
+ uint256 identityCommitment,
101
+ uint256 newIdentityCommitment,
102
+ uint256[] calldata proofSiblings,
103
+ uint8[] calldata proofPathIndices
104
+ ) external override onlyGroupAdmin(groupId) {
105
+ _updateMember(groupId, identityCommitment, newIdentityCommitment, proofSiblings, proofPathIndices);
106
+ }
107
+
108
+ /// @dev See {ISemaphore-removeMember}.
109
+ function removeMember(
110
+ uint256 groupId,
111
+ uint256 identityCommitment,
112
+ uint256[] calldata proofSiblings,
113
+ uint8[] calldata proofPathIndices
114
+ ) external override onlyGroupAdmin(groupId) {
115
+ _removeMember(groupId, identityCommitment, proofSiblings, proofPathIndices);
116
+ }
117
+
118
+ /// @dev See {ISemaphore-verifyProof}.
119
+ function verifyProof(
120
+ uint256 groupId,
121
+ uint256 merkleTreeRoot,
122
+ bytes32 signal,
123
+ uint256 nullifierHash,
124
+ uint256 externalNullifier,
125
+ uint256[8] calldata proof
126
+ ) external override {
127
+ uint256 currentMerkleTreeRoot = getMerkleTreeRoot(groupId);
128
+
129
+ if (currentMerkleTreeRoot == 0) {
130
+ revert Semaphore__GroupDoesNotExist();
131
+ }
132
+
133
+ if (merkleTreeRoot != currentMerkleTreeRoot) {
134
+ uint256 rootCreationDate = merkleTreeExpiries[groupId].rootCreationDates[merkleTreeRoot];
135
+ uint256 rootDuration = merkleTreeExpiries[groupId].rootDuration;
136
+
137
+ if (rootCreationDate == 0) {
138
+ revert Semaphore__MerkleTreeRootIsNotPartOfTheGroup();
139
+ }
140
+
141
+ if (block.timestamp > rootCreationDate + rootDuration) {
142
+ revert Semaphore__MerkleTreeRootIsExpired();
143
+ }
144
+ }
145
+
146
+ uint256 merkleTreeDepth = getMerkleTreeDepth(groupId);
147
+
148
+ IVerifier verifier = verifiers[merkleTreeDepth];
149
+
150
+ _verifyProof(signal, merkleTreeRoot, nullifierHash, externalNullifier, proof, verifier);
151
+
152
+ _saveNullifierHash(nullifierHash);
153
+
154
+ emit ProofVerified(groupId, signal);
36
155
  }
37
- }
38
-
39
- /// @dev See {ISemaphore-createGroup}.
40
- function createGroup(
41
- uint256 groupId,
42
- uint8 depth,
43
- uint256 zeroValue,
44
- address admin
45
- ) external override onlySupportedDepth(depth) {
46
- _createGroup(groupId, depth, zeroValue);
47
-
48
- groupAdmins[groupId] = admin;
49
-
50
- emit GroupAdminUpdated(groupId, address(0), admin);
51
- }
52
-
53
- /// @dev See {ISemaphore-updateGroupAdmin}.
54
- function updateGroupAdmin(uint256 groupId, address newAdmin) external override onlyGroupAdmin(groupId) {
55
- groupAdmins[groupId] = newAdmin;
56
-
57
- emit GroupAdminUpdated(groupId, _msgSender(), newAdmin);
58
- }
59
-
60
- /// @dev See {ISemaphore-addMember}.
61
- function addMember(uint256 groupId, uint256 identityCommitment) external override onlyGroupAdmin(groupId) {
62
- _addMember(groupId, identityCommitment);
63
- }
64
-
65
- /// @dev See {ISemaphore-removeMember}.
66
- function removeMember(
67
- uint256 groupId,
68
- uint256 identityCommitment,
69
- uint256[] calldata proofSiblings,
70
- uint8[] calldata proofPathIndices
71
- ) external override onlyGroupAdmin(groupId) {
72
- _removeMember(groupId, identityCommitment, proofSiblings, proofPathIndices);
73
- }
74
-
75
- /// @dev See {ISemaphore-verifyProof}.
76
- function verifyProof(
77
- uint256 groupId,
78
- bytes32 signal,
79
- uint256 nullifierHash,
80
- uint256 externalNullifier,
81
- uint256[8] calldata proof
82
- ) external override {
83
- uint256 root = getRoot(groupId);
84
- uint8 depth = getDepth(groupId);
85
-
86
- require(depth != 0, "Semaphore: group does not exist");
87
-
88
- IVerifier verifier = verifiers[depth];
89
-
90
- _verifyProof(signal, root, nullifierHash, externalNullifier, proof, verifier);
91
-
92
- _saveNullifierHash(nullifierHash);
93
-
94
- emit ProofVerified(groupId, signal);
95
- }
96
156
  }
@@ -1,4 +1,4 @@
1
1
  //SPDX-License-Identifier: MIT
2
- pragma solidity ^0.8.4;
2
+ pragma solidity 0.8.4;
3
3
 
4
4
  uint256 constant SNARK_SCALAR_FIELD = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
@@ -1,5 +1,5 @@
1
1
  //SPDX-License-Identifier: MIT
2
- pragma solidity ^0.8.4;
2
+ pragma solidity 0.8.4;
3
3
 
4
4
  import "../interfaces/ISemaphoreCore.sol";
5
5
  import "../interfaces/IVerifier.sol";
@@ -10,50 +10,52 @@ import "../interfaces/IVerifier.sol";
10
10
  /// nullifier to prevent double-signaling. External nullifier and Merkle trees (i.e. groups) must be
11
11
  /// managed externally.
12
12
  contract SemaphoreCore is ISemaphoreCore {
13
- /// @dev Gets a nullifier hash and returns true or false.
14
- /// It is used to prevent double-signaling.
15
- mapping(uint256 => bool) internal nullifierHashes;
13
+ /// @dev Gets a nullifier hash and returns true or false.
14
+ /// It is used to prevent double-signaling.
15
+ mapping(uint256 => bool) internal nullifierHashes;
16
16
 
17
- /// @dev Asserts that no nullifier already exists and if the zero-knowledge proof is valid.
18
- /// Otherwise it reverts.
19
- /// @param signal: Semaphore signal.
20
- /// @param root: Root of the Merkle tree.
21
- /// @param nullifierHash: Nullifier hash.
22
- /// @param externalNullifier: External nullifier.
23
- /// @param proof: Zero-knowledge proof.
24
- /// @param verifier: Verifier address.
25
- function _verifyProof(
26
- bytes32 signal,
27
- uint256 root,
28
- uint256 nullifierHash,
29
- uint256 externalNullifier,
30
- uint256[8] calldata proof,
31
- IVerifier verifier
32
- ) internal view {
33
- require(!nullifierHashes[nullifierHash], "SemaphoreCore: you cannot use the same nullifier twice");
17
+ /// @dev Asserts that no nullifier already exists and if the zero-knowledge proof is valid.
18
+ /// Otherwise it reverts.
19
+ /// @param signal: Semaphore signal.
20
+ /// @param root: Root of the Merkle tree.
21
+ /// @param nullifierHash: Nullifier hash.
22
+ /// @param externalNullifier: External nullifier.
23
+ /// @param proof: Zero-knowledge proof.
24
+ /// @param verifier: Verifier address.
25
+ function _verifyProof(
26
+ bytes32 signal,
27
+ uint256 root,
28
+ uint256 nullifierHash,
29
+ uint256 externalNullifier,
30
+ uint256[8] calldata proof,
31
+ IVerifier verifier
32
+ ) internal view {
33
+ if (nullifierHashes[nullifierHash]) {
34
+ revert Semaphore__YouAreUsingTheSameNillifierTwice();
35
+ }
34
36
 
35
- uint256 signalHash = _hashSignal(signal);
37
+ uint256 signalHash = _hashSignal(signal);
36
38
 
37
- verifier.verifyProof(
38
- [proof[0], proof[1]],
39
- [[proof[2], proof[3]], [proof[4], proof[5]]],
40
- [proof[6], proof[7]],
41
- [root, nullifierHash, signalHash, externalNullifier]
42
- );
43
- }
39
+ verifier.verifyProof(
40
+ [proof[0], proof[1]],
41
+ [[proof[2], proof[3]], [proof[4], proof[5]]],
42
+ [proof[6], proof[7]],
43
+ [root, nullifierHash, signalHash, externalNullifier]
44
+ );
45
+ }
44
46
 
45
- /// @dev Stores the nullifier hash to prevent double-signaling.
46
- /// Attention! Remember to call it when you verify a proof if you
47
- /// need to prevent double-signaling.
48
- /// @param nullifierHash: Semaphore nullifier hash.
49
- function _saveNullifierHash(uint256 nullifierHash) internal {
50
- nullifierHashes[nullifierHash] = true;
51
- }
47
+ /// @dev Stores the nullifier hash to prevent double-signaling.
48
+ /// Attention! Remember to call it when you verify a proof if you
49
+ /// need to prevent double-signaling.
50
+ /// @param nullifierHash: Semaphore nullifier hash.
51
+ function _saveNullifierHash(uint256 nullifierHash) internal {
52
+ nullifierHashes[nullifierHash] = true;
53
+ }
52
54
 
53
- /// @dev Creates a keccak256 hash of the signal.
54
- /// @param signal: Semaphore signal.
55
- /// @return Hash of the signal.
56
- function _hashSignal(bytes32 signal) private pure returns (uint256) {
57
- return uint256(keccak256(abi.encodePacked(signal))) >> 8;
58
- }
55
+ /// @dev Creates a keccak256 hash of the signal.
56
+ /// @param signal: Semaphore signal.
57
+ /// @return Hash of the signal.
58
+ function _hashSignal(bytes32 signal) private pure returns (uint256) {
59
+ return uint256(keccak256(abi.encodePacked(signal))) >> 8;
60
+ }
59
61
  }
@@ -1,83 +1,117 @@
1
1
  //SPDX-License-Identifier: MIT
2
- pragma solidity ^0.8.4;
2
+ pragma solidity 0.8.4;
3
3
 
4
4
  import {SNARK_SCALAR_FIELD} from "./SemaphoreConstants.sol";
5
5
  import "../interfaces/ISemaphoreGroups.sol";
6
- import "@zk-kit/incremental-merkle-tree.sol/contracts/IncrementalBinaryTree.sol";
6
+ import "@zk-kit/incremental-merkle-tree.sol/IncrementalBinaryTree.sol";
7
7
  import "@openzeppelin/contracts/utils/Context.sol";
8
8
 
9
9
  /// @title Semaphore groups contract.
10
10
  /// @dev The following code allows you to create groups, add and remove members.
11
11
  /// You can use getters to obtain informations about groups (root, depth, number of leaves).
12
12
  abstract contract SemaphoreGroups is Context, ISemaphoreGroups {
13
- using IncrementalBinaryTree for IncrementalTreeData;
14
-
15
- /// @dev Gets a group id and returns the group/tree data.
16
- mapping(uint256 => IncrementalTreeData) internal groups;
17
-
18
- /// @dev Creates a new group by initializing the associated tree.
19
- /// @param groupId: Id of the group.
20
- /// @param depth: Depth of the tree.
21
- /// @param zeroValue: Zero value of the tree.
22
- function _createGroup(
23
- uint256 groupId,
24
- uint8 depth,
25
- uint256 zeroValue
26
- ) internal virtual {
27
- require(groupId < SNARK_SCALAR_FIELD, "SemaphoreGroups: group id must be < SNARK_SCALAR_FIELD");
28
- require(getDepth(groupId) == 0, "SemaphoreGroups: group already exists");
29
-
30
- groups[groupId].init(depth, zeroValue);
31
-
32
- emit GroupCreated(groupId, depth, zeroValue);
33
- }
34
-
35
- /// @dev Adds an identity commitment to an existing group.
36
- /// @param groupId: Id of the group.
37
- /// @param identityCommitment: New identity commitment.
38
- function _addMember(uint256 groupId, uint256 identityCommitment) internal virtual {
39
- require(getDepth(groupId) != 0, "SemaphoreGroups: group does not exist");
40
-
41
- groups[groupId].insert(identityCommitment);
42
-
43
- uint256 root = getRoot(groupId);
44
-
45
- emit MemberAdded(groupId, identityCommitment, root);
46
- }
47
-
48
- /// @dev Removes an identity commitment from an existing group. A proof of membership is
49
- /// needed to check if the node to be deleted is part of the tree.
50
- /// @param groupId: Id of the group.
51
- /// @param identityCommitment: Existing identity commitment to be deleted.
52
- /// @param proofSiblings: Array of the sibling nodes of the proof of membership.
53
- /// @param proofPathIndices: Path of the proof of membership.
54
- function _removeMember(
55
- uint256 groupId,
56
- uint256 identityCommitment,
57
- uint256[] calldata proofSiblings,
58
- uint8[] calldata proofPathIndices
59
- ) internal virtual {
60
- require(getDepth(groupId) != 0, "SemaphoreGroups: group does not exist");
61
-
62
- groups[groupId].remove(identityCommitment, proofSiblings, proofPathIndices);
63
-
64
- uint256 root = getRoot(groupId);
65
-
66
- emit MemberRemoved(groupId, identityCommitment, root);
67
- }
68
-
69
- /// @dev See {ISemaphoreGroups-getRoot}.
70
- function getRoot(uint256 groupId) public view virtual override returns (uint256) {
71
- return groups[groupId].root;
72
- }
73
-
74
- /// @dev See {ISemaphoreGroups-getDepth}.
75
- function getDepth(uint256 groupId) public view virtual override returns (uint8) {
76
- return groups[groupId].depth;
77
- }
78
-
79
- /// @dev See {ISemaphoreGroups-getNumberOfLeaves}.
80
- function getNumberOfLeaves(uint256 groupId) public view virtual override returns (uint256) {
81
- return groups[groupId].numberOfLeaves;
82
- }
13
+ using IncrementalBinaryTree for IncrementalTreeData;
14
+
15
+ /// @dev Gets a group id and returns the group/tree data.
16
+ mapping(uint256 => IncrementalTreeData) internal groups;
17
+
18
+ /// @dev Creates a new group by initializing the associated tree.
19
+ /// @param groupId: Id of the group.
20
+ /// @param merkleTreeDepth: Depth of the tree.
21
+ /// @param zeroValue: Zero value of the tree.
22
+ function _createGroup(
23
+ uint256 groupId,
24
+ uint256 merkleTreeDepth,
25
+ uint256 zeroValue
26
+ ) internal virtual {
27
+ if (groupId >= SNARK_SCALAR_FIELD) {
28
+ revert Semaphore__GroupIdIsNotLessThanSnarkScalarField();
29
+ }
30
+
31
+ if (getMerkleTreeDepth(groupId) != 0) {
32
+ revert Semaphore__GroupAlreadyExists();
33
+ }
34
+
35
+ groups[groupId].init(merkleTreeDepth, zeroValue);
36
+
37
+ emit GroupCreated(groupId, merkleTreeDepth, zeroValue);
38
+ }
39
+
40
+ /// @dev Adds an identity commitment to an existing group.
41
+ /// @param groupId: Id of the group.
42
+ /// @param identityCommitment: New identity commitment.
43
+ function _addMember(uint256 groupId, uint256 identityCommitment) internal virtual {
44
+ if (getMerkleTreeDepth(groupId) == 0) {
45
+ revert Semaphore__GroupDoesNotExist();
46
+ }
47
+
48
+ groups[groupId].insert(identityCommitment);
49
+
50
+ uint256 merkleTreeRoot = getMerkleTreeRoot(groupId);
51
+
52
+ emit MemberAdded(groupId, identityCommitment, merkleTreeRoot);
53
+ }
54
+
55
+ /// @dev Updates an identity commitment of an existing group. A proof of membership is
56
+ /// needed to check if the node to be updated is part of the tree.
57
+ /// @param groupId: Id of the group.
58
+ /// @param identityCommitment: Existing identity commitment to be updated.
59
+ /// @param newIdentityCommitment: New identity commitment.
60
+ /// @param proofSiblings: Array of the sibling nodes of the proof of membership.
61
+ /// @param proofPathIndices: Path of the proof of membership.
62
+ function _updateMember(
63
+ uint256 groupId,
64
+ uint256 identityCommitment,
65
+ uint256 newIdentityCommitment,
66
+ uint256[] calldata proofSiblings,
67
+ uint8[] calldata proofPathIndices
68
+ ) internal virtual {
69
+ if (getMerkleTreeRoot(groupId) == 0) {
70
+ revert Semaphore__GroupDoesNotExist();
71
+ }
72
+
73
+ groups[groupId].update(identityCommitment, newIdentityCommitment, proofSiblings, proofPathIndices);
74
+
75
+ uint256 merkleTreeRoot = getMerkleTreeRoot(groupId);
76
+
77
+ emit MemberUpdated(groupId, identityCommitment, newIdentityCommitment, merkleTreeRoot);
78
+ }
79
+
80
+ /// @dev Removes an identity commitment from an existing group. A proof of membership is
81
+ /// needed to check if the node to be deleted is part of the tree.
82
+ /// @param groupId: Id of the group.
83
+ /// @param identityCommitment: Existing identity commitment to be removed.
84
+ /// @param proofSiblings: Array of the sibling nodes of the proof of membership.
85
+ /// @param proofPathIndices: Path of the proof of membership.
86
+ function _removeMember(
87
+ uint256 groupId,
88
+ uint256 identityCommitment,
89
+ uint256[] calldata proofSiblings,
90
+ uint8[] calldata proofPathIndices
91
+ ) internal virtual {
92
+ if (getMerkleTreeRoot(groupId) == 0) {
93
+ revert Semaphore__GroupDoesNotExist();
94
+ }
95
+
96
+ groups[groupId].remove(identityCommitment, proofSiblings, proofPathIndices);
97
+
98
+ uint256 merkleTreeRoot = getMerkleTreeRoot(groupId);
99
+
100
+ emit MemberRemoved(groupId, identityCommitment, merkleTreeRoot);
101
+ }
102
+
103
+ /// @dev See {ISemaphoreGroups-getMerkleTreeRoot}.
104
+ function getMerkleTreeRoot(uint256 groupId) public view virtual override returns (uint256) {
105
+ return groups[groupId].root;
106
+ }
107
+
108
+ /// @dev See {ISemaphoreGroups-getMerkleTreeDepth}.
109
+ function getMerkleTreeDepth(uint256 groupId) public view virtual override returns (uint256) {
110
+ return groups[groupId].depth;
111
+ }
112
+
113
+ /// @dev See {ISemaphoreGroups-getNumberOfMerkleTreeLeaves}.
114
+ function getNumberOfMerkleTreeLeaves(uint256 groupId) public view virtual override returns (uint256) {
115
+ return groups[groupId].numberOfLeaves;
116
+ }
83
117
  }