mirror of
https://github.com/semaphore-protocol/semaphore.git
synced 2026-04-28 03:00:41 -04:00
Merge pull request #692 from zkfriendly/main
Gas optimization in getting new merkle tree roots after modifying group members
Former-commit-id: 81e8a6885a
This commit is contained in:
@@ -56,18 +56,14 @@ contract Semaphore is ISemaphore, SemaphoreGroups {
|
||||
|
||||
/// @dev See {SemaphoreGroups-_addMember}.
|
||||
function addMember(uint256 groupId, uint256 identityCommitment) external override {
|
||||
_addMember(groupId, identityCommitment);
|
||||
|
||||
uint256 merkleTreeRoot = getMerkleTreeRoot(groupId);
|
||||
uint256 merkleTreeRoot = _addMember(groupId, identityCommitment);
|
||||
|
||||
groups[groupId].merkleRootCreationDates[merkleTreeRoot] = block.timestamp;
|
||||
}
|
||||
|
||||
/// @dev See {SemaphoreGroups-_addMembers}.
|
||||
function addMembers(uint256 groupId, uint256[] calldata identityCommitments) external override {
|
||||
_addMembers(groupId, identityCommitments);
|
||||
|
||||
uint256 merkleTreeRoot = getMerkleTreeRoot(groupId);
|
||||
uint256 merkleTreeRoot = _addMembers(groupId, identityCommitments);
|
||||
|
||||
groups[groupId].merkleRootCreationDates[merkleTreeRoot] = block.timestamp;
|
||||
}
|
||||
@@ -79,9 +75,7 @@ contract Semaphore is ISemaphore, SemaphoreGroups {
|
||||
uint256 newIdentityCommitment,
|
||||
uint256[] calldata merkleProofSiblings
|
||||
) external override {
|
||||
_updateMember(groupId, identityCommitment, newIdentityCommitment, merkleProofSiblings);
|
||||
|
||||
uint256 merkleTreeRoot = getMerkleTreeRoot(groupId);
|
||||
uint256 merkleTreeRoot = _updateMember(groupId, identityCommitment, newIdentityCommitment, merkleProofSiblings);
|
||||
|
||||
groups[groupId].merkleRootCreationDates[merkleTreeRoot] = block.timestamp;
|
||||
}
|
||||
@@ -92,9 +86,7 @@ contract Semaphore is ISemaphore, SemaphoreGroups {
|
||||
uint256 identityCommitment,
|
||||
uint256[] calldata merkleProofSiblings
|
||||
) external override {
|
||||
_removeMember(groupId, identityCommitment, merkleProofSiblings);
|
||||
|
||||
uint256 merkleTreeRoot = getMerkleTreeRoot(groupId);
|
||||
uint256 merkleTreeRoot = _removeMember(groupId, identityCommitment, merkleProofSiblings);
|
||||
|
||||
groups[groupId].merkleRootCreationDates[merkleTreeRoot] = block.timestamp;
|
||||
}
|
||||
|
||||
@@ -67,12 +67,13 @@ abstract contract SemaphoreGroups is ISemaphoreGroups {
|
||||
/// @dev Adds an identity commitment to an existing group.
|
||||
/// @param groupId: Id of the group.
|
||||
/// @param identityCommitment: New identity commitment.
|
||||
/// @return merkleTreeRoot New root hash of the tree.
|
||||
function _addMember(
|
||||
uint256 groupId,
|
||||
uint256 identityCommitment
|
||||
) internal virtual onlyExistingGroup(groupId) onlyGroupAdmin(groupId) {
|
||||
) internal virtual onlyExistingGroup(groupId) onlyGroupAdmin(groupId) returns (uint256 merkleTreeRoot) {
|
||||
uint256 index = getMerkleTreeSize(groupId);
|
||||
uint256 merkleTreeRoot = merkleTrees[groupId]._insert(identityCommitment);
|
||||
merkleTreeRoot = merkleTrees[groupId]._insert(identityCommitment);
|
||||
|
||||
emit MemberAdded(groupId, index, identityCommitment, merkleTreeRoot);
|
||||
}
|
||||
@@ -80,9 +81,13 @@ abstract contract SemaphoreGroups is ISemaphoreGroups {
|
||||
/// @dev Adds new members to an existing group.
|
||||
/// @param groupId: Id of the group.
|
||||
/// @param identityCommitments: New identity commitments.
|
||||
function _addMembers(uint256 groupId, uint256[] calldata identityCommitments) internal virtual {
|
||||
/// @return merkleTreeRoot New root hash of the tree.
|
||||
function _addMembers(
|
||||
uint256 groupId,
|
||||
uint256[] calldata identityCommitments
|
||||
) internal virtual returns (uint256 merkleTreeRoot) {
|
||||
uint256 startIndex = getMerkleTreeSize(groupId);
|
||||
uint256 merkleTreeRoot = merkleTrees[groupId]._insertMany(identityCommitments);
|
||||
merkleTreeRoot = merkleTrees[groupId]._insertMany(identityCommitments);
|
||||
|
||||
emit MembersAdded(groupId, startIndex, identityCommitments, merkleTreeRoot);
|
||||
}
|
||||
@@ -93,14 +98,15 @@ abstract contract SemaphoreGroups is ISemaphoreGroups {
|
||||
/// @param oldIdentityCommitment: Existing identity commitment to be updated.
|
||||
/// @param newIdentityCommitment: New identity commitment.
|
||||
/// @param merkleProofSiblings: Array of the sibling nodes of the proof of membership.
|
||||
/// @return merkleTreeRoot New root hash of the tree.
|
||||
function _updateMember(
|
||||
uint256 groupId,
|
||||
uint256 oldIdentityCommitment,
|
||||
uint256 newIdentityCommitment,
|
||||
uint256[] calldata merkleProofSiblings
|
||||
) internal virtual onlyExistingGroup(groupId) onlyGroupAdmin(groupId) {
|
||||
) internal virtual onlyExistingGroup(groupId) onlyGroupAdmin(groupId) returns (uint256 merkleTreeRoot) {
|
||||
uint256 index = merkleTrees[groupId]._indexOf(oldIdentityCommitment);
|
||||
uint256 merkleTreeRoot = merkleTrees[groupId]._update(
|
||||
merkleTreeRoot = merkleTrees[groupId]._update(
|
||||
oldIdentityCommitment,
|
||||
newIdentityCommitment,
|
||||
merkleProofSiblings
|
||||
@@ -114,14 +120,15 @@ abstract contract SemaphoreGroups is ISemaphoreGroups {
|
||||
/// @param groupId: Id of the group.
|
||||
/// @param identityCommitment: Existing identity commitment to be removed.
|
||||
/// @param merkleProofSiblings: Array of the sibling nodes of the proof of membership.
|
||||
/// @return merkleTreeRoot New root hash of the tree.
|
||||
function _removeMember(
|
||||
uint256 groupId,
|
||||
uint256 identityCommitment,
|
||||
uint256[] calldata merkleProofSiblings
|
||||
) internal virtual onlyExistingGroup(groupId) onlyGroupAdmin(groupId) {
|
||||
) internal virtual onlyExistingGroup(groupId) onlyGroupAdmin(groupId) returns (uint256 merkleTreeRoot) {
|
||||
uint256 index = merkleTrees[groupId]._indexOf(identityCommitment);
|
||||
|
||||
uint256 merkleTreeRoot = merkleTrees[groupId]._remove(identityCommitment, merkleProofSiblings);
|
||||
merkleTreeRoot = merkleTrees[groupId]._remove(identityCommitment, merkleProofSiblings);
|
||||
|
||||
emit MemberRemoved(groupId, index, identityCommitment, merkleTreeRoot);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user