Skip to content
Lattice

SemaphoreLib

Git Source

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

NameTypeDescription
$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

NameTypeDescription
verifier_addressThe 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

NameTypeDescription
verifier_address

createGroup

Creates a group administered by admin.

function createGroup(address admin) internal returns (uint256 groupId);

Parameters

NameTypeDescription
adminaddress

Returns

NameTypeDescription
groupIduint256

addMember

Adds a member to a group. Only the group admin.

function addMember(uint256 groupId, uint256 identityCommitment) internal;

Parameters

NameTypeDescription
groupIduint256
identityCommitmentuint256

addMembers

Adds many members to a group, in order. Only the group admin.

function addMembers(uint256 groupId, uint256[] calldata identityCommitments) internal;

Parameters

NameTypeDescription
groupIduint256
identityCommitmentsuint256[]

_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

NameTypeDescription
groupIduint256

Returns

NameTypeDescription
gGroup

validateProof

Validates a proof against a group and consumes its nullifier.

function validateProof(uint256 groupId, ISemaphore.SemaphoreProof calldata proof) internal;

Parameters

NameTypeDescription
groupIduint256
proofISemaphore.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

NameTypeDescription
groupIduint256
proofISemaphore.SemaphoreProof

Returns

NameTypeDescription
<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

NameTypeDescription
gGroup
proofISemaphore.SemaphoreProof

Returns

NameTypeDescription
<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

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint256

groupCount

function groupCount() internal view returns (uint256);

Returns

NameTypeDescription
<none>uint256

groupAdmin

function groupAdmin(uint256 groupId) internal view returns (address);

Parameters

NameTypeDescription
groupIduint256

Returns

NameTypeDescription
<none>address

getMerkleTreeRoot

function getMerkleTreeRoot(uint256 groupId) internal view returns (uint256);

Parameters

NameTypeDescription
groupIduint256

Returns

NameTypeDescription
<none>uint256

getMerkleTreeSize

function getMerkleTreeSize(uint256 groupId) internal view returns (uint256);

Parameters

NameTypeDescription
groupIduint256

Returns

NameTypeDescription
<none>uint256

hasMember

function hasMember(uint256 groupId, uint256 identityCommitment) internal view returns (bool);

Parameters

NameTypeDescription
groupIduint256
identityCommitmentuint256

Returns

NameTypeDescription
<none>bool

verifier

function verifier() internal view returns (address);

Returns

NameTypeDescription
<none>address