SemaphoreLib
Title: SemaphoreLib
Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from Semaphore (https://github.com/semaphore-protocol/semaphore)
Library implementing the Semaphore anonymous-membership / signaling module: groups of identity commitments (Poseidon incremental Merkle trees) and zero-knowledge membership + signaling proofs validated against the audited Semaphore v4 verifier, with per-group scope nullifiers.
Three-layer pattern: this library holds the logic + namespaced storage; the stateless Semaphore
facet forwards to it. Membership uses IncrementalMerkleTreeLib (Poseidon LeanIMT + recent-root
history) and double-signaling protection uses NullifierRegistryLib. The Groth16 verification is
delegated to the configurable, audited ISemaphoreVerifier.
Constants
MIN_DEPTH
Minimum / maximum supported Merkle tree depth (matches Semaphore v4).
uint256 internal constant MIN_DEPTH = 1;MAX_DEPTH
uint256 internal constant MAX_DEPTH = 32;Functions
semaphoreStorage
function semaphoreStorage() internal pure returns (SemaphoreStorage storage $);Returns
| Name | Type | Description |
|---|---|---|
| $ | SemaphoreStorage |
__Semaphore_init
Initializes the Semaphore module with a verifier.
Must be called inside a pre/postInitializer block. Registers ISemaphore for ERC-165.
function __Semaphore_init(address verifier_) internal;Parameters
| Name | Type | Description |
|---|---|---|
| verifier_ | address | The ISemaphoreVerifier contract address. |
registerInterface
Registers support for the ISemaphore interface via ERC-165.
function registerInterface() internal;setVerifier
Sets the Semaphore verifier. Gated on the default admin role.
function setVerifier(address verifier_) internal;Parameters
| Name | Type | Description |
|---|---|---|
| verifier_ | address |
createGroup
Creates a group administered by admin.
function createGroup(address admin) internal returns (uint256 groupId);Parameters
| Name | Type | Description |
|---|---|---|
| admin | address |
Returns
| Name | Type | Description |
|---|---|---|
| groupId | uint256 |
addMember
Adds a member to a group. Only the group admin.
function addMember(uint256 groupId, uint256 identityCommitment) internal;Parameters
| Name | Type | Description |
|---|---|---|
| groupId | uint256 | |
| identityCommitment | uint256 |
addMembers
Adds many members to a group, in order. Only the group admin.
function addMembers(uint256 groupId, uint256[] calldata identityCommitments) internal;Parameters
| Name | Type | Description |
|---|---|---|
| groupId | uint256 | |
| identityCommitments | uint256[] |
_adminGroup
Loads a group, reverting if it does not exist or the caller is not its admin.
function _adminGroup(uint256 groupId) private view returns (Group storage g);Parameters
| Name | Type | Description |
|---|---|---|
| groupId | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| g | Group |
validateProof
Validates a proof against a group and consumes its nullifier.
function validateProof(uint256 groupId, ISemaphore.SemaphoreProof calldata proof) internal;Parameters
| Name | Type | Description |
|---|---|---|
| groupId | uint256 | |
| proof | ISemaphore.SemaphoreProof |
verifyProof
Verifies a proof against a group without consuming its nullifier.
function verifyProof(uint256 groupId, ISemaphore.SemaphoreProof calldata proof) internal view returns (bool);Parameters
| Name | Type | Description |
|---|---|---|
| groupId | uint256 | |
| proof | ISemaphore.SemaphoreProof |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bool |
_verify
Shared depth/root checks + delegation to the audited Semaphore verifier.
function _verify(Group storage g, ISemaphore.SemaphoreProof calldata proof) private view returns (bool);Parameters
| Name | Type | Description |
|---|---|---|
| g | Group | |
| proof | ISemaphore.SemaphoreProof |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bool |
_hash
keccak256 hash of a value compatible with the SNARK scalar field (matches Semaphore v4).
function _hash(uint256 value) private pure returns (uint256);Parameters
| Name | Type | Description |
|---|---|---|
| value | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
groupCount
function groupCount() internal view returns (uint256);Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
groupAdmin
function groupAdmin(uint256 groupId) internal view returns (address);Parameters
| Name | Type | Description |
|---|---|---|
| groupId | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | address |
getMerkleTreeRoot
function getMerkleTreeRoot(uint256 groupId) internal view returns (uint256);Parameters
| Name | Type | Description |
|---|---|---|
| groupId | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
getMerkleTreeSize
function getMerkleTreeSize(uint256 groupId) internal view returns (uint256);Parameters
| Name | Type | Description |
|---|---|---|
| groupId | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
hasMember
function hasMember(uint256 groupId, uint256 identityCommitment) internal view returns (bool);Parameters
| Name | Type | Description |
|---|---|---|
| groupId | uint256 | |
| identityCommitment | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bool |
verifier
function verifier() internal view returns (address);Returns
| Name | Type | Description |
|---|---|---|
| <none> | address |