Skip to content

Commit 2f1fbd8

Browse files
authored
Merge pull request #210 from semaphore-protocol/refactor/general
General refactoring and additional documentation to contracts
2 parents 5726c98 + 4c32f4f commit 2f1fbd8

File tree

10 files changed

+37
-33
lines changed

10 files changed

+37
-33
lines changed

packages/contracts/contracts/Semaphore.sol

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import "./interfaces/ISemaphoreVerifier.sol";
66
import "./base/SemaphoreGroups.sol";
77

88
/// @title Semaphore
9+
/// @dev This contract uses the Semaphore base contracts to provide a complete service
10+
/// to allow admins to create and manage groups and their members to generate Semaphore proofs
11+
/// and verify them. Group admins can add, update or remove group members, and can be
12+
/// an Ethereum account or a smart contract. This contract also assigns each new Merkle tree
13+
/// generated with a new root a duration (or an expiry) within which the proofs generated with that root
14+
/// can be validated.
915
contract Semaphore is ISemaphore, SemaphoreGroups {
1016
ISemaphoreVerifier public verifier;
1117

@@ -151,12 +157,16 @@ contract Semaphore is ISemaphore, SemaphoreGroups {
151157
uint256 externalNullifier,
152158
uint256[8] calldata proof
153159
) external override {
154-
if (getMerkleTreeDepth(groupId) == 0) {
160+
uint256 merkleTreeDepth = getMerkleTreeDepth(groupId);
161+
162+
if (merkleTreeDepth == 0) {
155163
revert Semaphore__GroupDoesNotExist();
156164
}
157165

158166
uint256 currentMerkleTreeRoot = getMerkleTreeRoot(groupId);
159167

168+
// A proof could have used an old Merkle tree root.
169+
// https://github.com/semaphore-protocol/semaphore/issues/98
160170
if (merkleTreeRoot != currentMerkleTreeRoot) {
161171
uint256 merkleRootCreationDate = groups[groupId].merkleRootCreationDates[merkleTreeRoot];
162172
uint256 merkleTreeDuration = groups[groupId].merkleTreeDuration;
@@ -174,8 +184,6 @@ contract Semaphore is ISemaphore, SemaphoreGroups {
174184
revert Semaphore__YouAreUsingTheSameNillifierTwice();
175185
}
176186

177-
uint256 merkleTreeDepth = getMerkleTreeDepth(groupId);
178-
179187
verifier.verifyProof(merkleTreeRoot, nullifierHash, signal, externalNullifier, proof, merkleTreeDepth);
180188

181189
groups[groupId].nullifierHashes[nullifierHash] = true;

packages/contracts/contracts/base/SemaphoreGroups.sol

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import "@zk-kit/incremental-merkle-tree.sol/IncrementalBinaryTree.sol";
66
import "@openzeppelin/contracts/utils/Context.sol";
77

88
/// @title Semaphore groups contract.
9-
/// @dev The following code allows you to create groups, add and remove members.
9+
/// @dev This contract allows you to create groups, add, remove and update members.
1010
/// You can use getters to obtain informations about groups (root, depth, number of leaves).
1111
abstract contract SemaphoreGroups is Context, ISemaphoreGroups {
1212
using IncrementalBinaryTree for IncrementalTreeData;
1313

1414
/// @dev Gets a group id and returns the tree data.
15-
mapping(uint256 => IncrementalTreeData) internal merkleTree;
15+
mapping(uint256 => IncrementalTreeData) internal merkleTrees;
1616

1717
/// @dev Creates a new group by initializing the associated tree.
1818
/// @param groupId: Id of the group.
@@ -22,12 +22,12 @@ abstract contract SemaphoreGroups is Context, ISemaphoreGroups {
2222
revert Semaphore__GroupAlreadyExists();
2323
}
2424

25-
// The zeroValue is in fact an implicit member of the group.
25+
// The zeroValue is an implicit member of the group, or an implicit leaf of the Merkle tree.
2626
// Although there is a remote possibility that the preimage of
27-
// the hash may be calculated, using this value minimizes the risk.
27+
// the hash may be calculated, using this value we aim to minimize the risk.
2828
uint256 zeroValue = uint256(keccak256(abi.encodePacked(groupId))) >> 8;
2929

30-
merkleTree[groupId].init(merkleTreeDepth, zeroValue);
30+
merkleTrees[groupId].init(merkleTreeDepth, zeroValue);
3131

3232
emit GroupCreated(groupId, merkleTreeDepth, zeroValue);
3333
}
@@ -40,7 +40,7 @@ abstract contract SemaphoreGroups is Context, ISemaphoreGroups {
4040
revert Semaphore__GroupDoesNotExist();
4141
}
4242

43-
merkleTree[groupId].insert(identityCommitment);
43+
merkleTrees[groupId].insert(identityCommitment);
4444

4545
uint256 merkleTreeRoot = getMerkleTreeRoot(groupId);
4646
uint256 index = getNumberOfMerkleTreeLeaves(groupId) - 1;
@@ -66,7 +66,7 @@ abstract contract SemaphoreGroups is Context, ISemaphoreGroups {
6666
revert Semaphore__GroupDoesNotExist();
6767
}
6868

69-
merkleTree[groupId].update(identityCommitment, newIdentityCommitment, proofSiblings, proofPathIndices);
69+
merkleTrees[groupId].update(identityCommitment, newIdentityCommitment, proofSiblings, proofPathIndices);
7070

7171
uint256 merkleTreeRoot = getMerkleTreeRoot(groupId);
7272
uint256 index = proofPathIndicesToMemberIndex(proofPathIndices);
@@ -90,7 +90,7 @@ abstract contract SemaphoreGroups is Context, ISemaphoreGroups {
9090
revert Semaphore__GroupDoesNotExist();
9191
}
9292

93-
merkleTree[groupId].remove(identityCommitment, proofSiblings, proofPathIndices);
93+
merkleTrees[groupId].remove(identityCommitment, proofSiblings, proofPathIndices);
9494

9595
uint256 merkleTreeRoot = getMerkleTreeRoot(groupId);
9696
uint256 index = proofPathIndicesToMemberIndex(proofPathIndices);
@@ -100,17 +100,17 @@ abstract contract SemaphoreGroups is Context, ISemaphoreGroups {
100100

101101
/// @dev See {ISemaphoreGroups-getMerkleTreeRoot}.
102102
function getMerkleTreeRoot(uint256 groupId) public view virtual override returns (uint256) {
103-
return merkleTree[groupId].root;
103+
return merkleTrees[groupId].root;
104104
}
105105

106106
/// @dev See {ISemaphoreGroups-getMerkleTreeDepth}.
107107
function getMerkleTreeDepth(uint256 groupId) public view virtual override returns (uint256) {
108-
return merkleTree[groupId].depth;
108+
return merkleTrees[groupId].depth;
109109
}
110110

111111
/// @dev See {ISemaphoreGroups-getNumberOfMerkleTreeLeaves}.
112112
function getNumberOfMerkleTreeLeaves(uint256 groupId) public view virtual override returns (uint256) {
113-
return merkleTree[groupId].numberOfLeaves;
113+
return merkleTrees[groupId].numberOfLeaves;
114114
}
115115

116116
/// @dev Converts the path indices of a Merkle proof to the identity commitment index in the tree.

packages/contracts/contracts/base/SemaphoreVerifier.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import "../interfaces/ISemaphoreVerifier.sol";
55

66
/// @title Semaphore verifier contract.
77
/// @notice Minimal code to allow users to verify their Semaphore proofs.
8-
/// @dev The following code verifies that the proof is correct and it is a
9-
/// modified version of the Verifier Groth16 template of SnarkJS
8+
/// @dev This contract allows you to verify whether a Semaphore proof is correct.
9+
/// It is a modified version of the Groth16 verifier template of SnarkJS
1010
/// (https://github.com/iden3/snarkjs) adapted to Semaphore. The Pairing library
11-
/// is extarnal.
11+
/// is external.
1212
contract SemaphoreVerifier is ISemaphoreVerifier {
1313
using Pairing for *;
1414

packages/contracts/contracts/extensions/SemaphoreVoting.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "../interfaces/ISemaphoreVerifier.sol";
66
import "../base/SemaphoreGroups.sol";
77

88
/// @title Semaphore voting contract.
9+
/// @notice It allows users to vote anonymously in a poll.
910
/// @dev The following code allows you to create polls, add voters and allow them to vote anonymously.
1011
contract SemaphoreVoting is ISemaphoreVoting, SemaphoreGroups {
1112
ISemaphoreVerifier public verifier;

packages/contracts/contracts/extensions/SemaphoreWhistleblowing.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import "../interfaces/ISemaphoreVerifier.sol";
66
import "../base/SemaphoreGroups.sol";
77

88
/// @title Semaphore whistleblowing contract.
9+
/// @notice It allows users to leak information anonymously .
910
/// @dev The following code allows you to create entities for whistleblowers (e.g. non-profit
10-
/// organization, newspaper) and to allow them to publish news leaks anonymously.
11-
/// Leaks can be IPFS hashes, permanent links or other kinds of reference.
11+
/// organization, newspaper) and allow them to leak anonymously.
12+
/// Leaks can be IPFS hashes, permanent links or other kinds of references.
1213
contract SemaphoreWhistleblowing is ISemaphoreWhistleblowing, SemaphoreGroups {
1314
ISemaphoreVerifier public verifier;
1415

packages/contracts/contracts/interfaces/ISemaphore.sol

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//SPDX-License-Identifier: MIT
22
pragma solidity 0.8.4;
33

4-
/// @title Semaphore interface.
5-
/// @dev Interface of a Semaphore contract.
4+
/// @title Semaphore contract interface.
65
interface ISemaphore {
76
error Semaphore__CallerIsNotTheGroupAdmin();
87
error Semaphore__MerkleTreeDepthIsNotSupported();
@@ -42,8 +41,8 @@ interface ISemaphore {
4241
/// @param signal: Semaphore signal.
4342
event ProofVerified(
4443
uint256 indexed groupId,
45-
uint256 merkleTreeRoot,
46-
uint256 externalNullifier,
44+
uint256 indexed merkleTreeRoot,
45+
uint256 indexed externalNullifier,
4746
uint256 nullifierHash,
4847
uint256 signal
4948
);

packages/contracts/contracts/interfaces/ISemaphoreGroups.sol

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
//SPDX-License-Identifier: MIT
22
pragma solidity 0.8.4;
33

4-
/// @title SemaphoreGroups interface.
5-
/// @dev Interface of a SemaphoreGroups contract.
4+
/// @title SemaphoreGroups contract interface.
65
interface ISemaphoreGroups {
76
error Semaphore__GroupDoesNotExist();
87
error Semaphore__GroupAlreadyExists();
9-
error Semaphore__GroupIdIsNotLessThanSnarkScalarField();
108

119
/// @dev Emitted when a new group is created.
1210
/// @param groupId: Id of the group.

packages/contracts/contracts/interfaces/ISemaphoreVerifier.sol

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ pragma solidity 0.8.4;
33

44
import "../base/Pairing.sol";
55

6-
/// @title SemaphoreVerifier interface.
7-
/// @dev Interface of SemaphoreVerifier contract.
6+
/// @title SemaphoreVerifier contract interface.
87
interface ISemaphoreVerifier {
98
struct VerificationKey {
109
Pairing.G1Point alfa1;
@@ -20,7 +19,7 @@ interface ISemaphoreVerifier {
2019
Pairing.G1Point C;
2120
}
2221

23-
/// @dev Verifies that the zero-knowledge proof is valid.
22+
/// @dev Verifies whether a Semaphore proof is valid.
2423
/// @param merkleTreeRoot: Root of the Merkle tree.
2524
/// @param nullifierHash: Nullifier hash.
2625
/// @param signal: Semaphore signal.

packages/contracts/contracts/interfaces/ISemaphoreVoting.sol

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//SPDX-License-Identifier: MIT
22
pragma solidity 0.8.4;
33

4-
/// @title SemaphoreVoting interface.
5-
/// @dev Interface of SemaphoreVoting contract.
4+
/// @title SemaphoreVoting contract interface.
65
interface ISemaphoreVoting {
76
error Semaphore__CallerIsNotThePollCoordinator();
87
error Semaphore__MerkleTreeDepthIsNotSupported();

packages/contracts/contracts/interfaces/ISemaphoreWhistleblowing.sol

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//SPDX-License-Identifier: MIT
22
pragma solidity 0.8.4;
33

4-
/// @title SemaphoreWhistleblowing interface.
5-
/// @dev Interface of SemaphoreWhistleblowing contract.
4+
/// @title SemaphoreWhistleblowing contract interface.
65
interface ISemaphoreWhistleblowing {
76
error Semaphore__CallerIsNotTheEditor();
87
error Semaphore__MerkleTreeDepthIsNotSupported();

0 commit comments

Comments
 (0)