A library to generate and verify Semaphore proofs.
| This library provides utility functions to generate and verify Semaphore proofs compatible with the Semaphore circuits. Generating valid zero-knowledge proofs requires files that can only be obtained in an attested trusted-setup ceremony. |
|---|
Install the @semaphore-protocol/proof package and its peer dependencies with npm:
npm i @semaphore-protocol/identity @semaphore-protocol/group @semaphore-protocol/proof
or yarn:
yarn add @semaphore-protocol/identity @semaphore-protocol/group @semaphore-protocol/proof
For more information on the functions provided by @semaphore-protocol/proof, please refer to the TypeDoc documentation.
# generateProof( identity: Identity, group: Group, message: BigNumberish | Uint8Array | string, scope: BigNumberish | Uint8Array | string, merkleTreeDepth: number, snarkArtifacts?: SnarkArtifacts ): Promise<_SemaphoreProof_>
import { Identity } from "@semaphore-protocol/identity"
import { Group } from "@semaphore-protocol/group"
import { generateProof } from "@semaphore-protocol/proof"
const identity1 = new Identity()
const identity2 = new Identity()
const identity3 = new Identity()
const group = new Group([identity1.commitment, identity2.commitment, identity3.commitment])
const message = "Hello world"
const scope = "Semaphore"
// snarkArtifacts are not provided.
// So they will be automatically downloaded (see https://github.com/privacy-scaling-explorations/snark-artifacts).
const proof1 = await generateProof(identity1, group, message, scope)
// You can also specify the maximum tree depth supported by the proof.
const proof2 = await generateProof(identity2, group, message, scope, 20)
// You can also override our default zkey/wasm files.
const proof3 = await generateProof(identity3, group, message, scope, 20, {
wasm: "./semaphore.wasm",
zkey: "./semaphore.zkey"
})
# verifyProof(semaphoreProof: SemaphoreProof): Promise<_boolean_>
import { verifyProof } from "@semaphore-protocol/proof"
await verifyProof(proof1)
When using the Semaphore proof library in Node.js environments, especially in tests or scripts that create and use the bn128 curve (for example, via getCurveFromName("bn128") from the ffjavascript package), it is important to properly release resources associated with the curve after use. Failing to do so can result in leaked handles (such as MessagePort handles), which may prevent Node.js from exiting cleanly. This is particularly relevant when running test suites.
How to terminate the bn128 curve:
If you create a curve instance using getCurveFromName("bn128"), you should call its terminate() method when you are done with it. For example:
import { getCurveFromName } from "ffjavascript"
let curve
beforeAll(async () => {
curve = await getCurveFromName("bn128")
})
afterAll(async () => {
await curve.terminate()
})
This ensures that all resources are properly released and Node.js can exit cleanly after your script or tests finish.