SDK library · Browser and Node

# anon-membership

Prove membership in one of up to four groups with scoped nullifiers and signal binding. UltraHonk's mode is designed to be zero-knowledge; this is experimental cryptography, and the package README states that Noir is not yet for production use.

`@toip-tools/anon-membership` · v0.1.0 · [Source](https://github.com/yourbuddyconner/toip-tools/blob/main/packages/anon-membership/src/index.ts)

## Setup

Install the package from npm. It needs Node 22.13 or later, or a bundler for browser entry points.

Install:

```bash
npm install @toip-tools/anon-membership
```

**Runtime:** Node and browsers, proving in a Web Worker. Proving takes about 0.15 s on an Apple M1 Max; phones aren't measured.

Import:

```ts
import { GroupIssuer, enroll, ENROLL } from "@toip-tools/anon-membership";
```

## Usage

### Enroll members and publish signed roots

Await `initHashing()` before constructing trees or computing membership hashes. Create a `GroupIssuer`, register its admission callback with `serve`, and let members derive a per-group `memberSecret` and call `enroll`. Publish a signed root and snapshot once per epoch after enrollment changes. Enrollment after an epoch's publication becomes usable under a later published root.

[`initHashing`](https://toip.tools/toolkit/anon-membership/api/initHashing/) · [`GroupIssuer`](https://toip.tools/toolkit/anon-membership/api/GroupIssuer/) · [`memberSecret`](https://toip.tools/toolkit/anon-membership/api/memberSecret/) · [`enroll`](https://toip.tools/toolkit/anon-membership/api/enroll/) · [`ENROLL`](https://toip.tools/toolkit/anon-membership/api/ENROLL/)

### Build and prove the verifier's statement

Rebuild a snapshot with `treeFromRoot`, then call `membershipInputs` with the member secret, ordered groups, verifier-agreed scope and signal, current day, and any required leaf fields/predicates. Reuse a `MembershipProver` from `create()` for successive `prove` calls. In a browser, use the `/worker` entry's `MembershipWorkerClient` and `serveMembershipWorker` to keep proving off the page thread.

[`treeFromRoot`](https://toip.tools/toolkit/anon-membership/api/treeFromRoot/) · [`membershipInputs`](https://toip.tools/toolkit/anon-membership/api/membershipInputs/) · [`scopeOf`](https://toip.tools/toolkit/anon-membership/api/scopeOf/) · [`messageOf`](https://toip.tools/toolkit/anon-membership/api/messageOf/) · [`dayOf`](https://toip.tools/toolkit/anon-membership/api/dayOf/) · [`MembershipProver`](https://toip.tools/toolkit/anon-membership/api/MembershipProver/)

### Accept a proof and enforce nullifier policy

Create a `MembershipVerifier` with accepted artifacts. Call `acceptMembership` with pinned group issuers and verification-key hashes, verified-policy settings, fetched root logs, and a scope/signal you computed independently. It checks root history, freshness, anonymity-set size, statement, and proof. After acceptance, use `NullifierBook.has`/`label` for duplicate or rate-limit handling; acceptance itself does not record nullifier use.

[`MembershipVerifier`](https://toip.tools/toolkit/anon-membership/api/MembershipVerifier/) · [`acceptMembership`](https://toip.tools/toolkit/anon-membership/api/acceptMembership/) · [`AcceptPolicy`](https://toip.tools/toolkit/anon-membership/api/AcceptPolicy/) · [`NullifierBook`](https://toip.tools/toolkit/anon-membership/api/NullifierBook/)

## Example

A newsroom learns "a current employee of Acme sent this" and nothing about which one.

site/examples/snippets/anon-membership.ts:

```ts
import {
  GroupIssuer, MembershipProver, MembershipVerifier, NullifierBook, acceptMembership, dayOf, enroll,
  initHashing, memberSecret, membershipInputs, messageOf, scopeOf, treeFromRoot,
} from "@toip-tools/anon-membership";
import { Bus, Endpoint, SystemClock, identityFromSeed, seedFrom } from "@toip-tools/trust-core";

await initHashing();
const clock = new SystemClock();
const bus = new Bus(clock);
const acme = identityFromSeed(seedFrom("example/acme"));
const walletSeed = seedFrom("example/employee-wallet");
const employee = identityFromSeed(walletSeed);
const newsroom = identityFromSeed(seedFrom("example/newsroom"));

// The issuer keeps the group and signs one root per day. It never learns a member's secret.
const issuer = new GroupIssuer({ identity: acme, clock, name: "employees" });
issuer.serve(new Endpoint({ identity: acme, transport: bus, clock }), (did) => did === employee.did);

// The member enrolls a commitment to a secret derived from their wallet seed.
const secret = memberSecret(walletSeed, issuer.groupId);
await enroll(new Endpoint({ identity: employee, transport: bus, clock }), acme.did, issuer.groupId, secret);
const { root, snapshot } = issuer.publish();

// Proving: membership, a nullifier for this story, bound to this message.
const scope = scopeOf("tipline/v1", newsroom.did, "story-41");
const signal = messageOf("tipline/v1", { kind: "tip", body: "sha256:9f86d081" });
const tree = treeFromRoot(root, snapshot); // checks the snapshot against the signed root
const { statement, witness } = membershipInputs({ secret, groups: [{ group: issuer.groupId, tree }], scope, signal, today: dayOf(clock.now()) });
const prover = await MembershipProver.create();
const proof = await prover.prove(statement, witness);

// The newsroom accepts or refuses, and counts distinct sources by nullifier.
const verifier = await MembershipVerifier.create();
const r = await acceptMembership(proof, {
  policy: { groups: [{ group: issuer.groupId, issuer: acme.did }], vkHashes: verifier.vkHashes(), maxEpochAge: 3, minSize: 1 },
  logs: { [issuer.groupId]: issuer.log() },
  verifier, scope, signal, clock,
});
const book = new NullifierBook();
if (r.ok) console.log("accepted:", book.label(issuer.groupId, scope, r.nullifier));
else console.log("refused:", r.reason);
await prover.destroy(); // the prover and verifier hold worker threads until destroyed
await verifier.destroy();
```

Run from the repository root:

```bash
npx tsx --tsconfig site/tsconfig.json site/examples/snippets/anon-membership.ts
```

Output:

```text
accepted: { label: 'Source A', first: true }
```

## Lifecycle and persistence

- Reuse warmed prover/verifier instances and await their `destroy()` methods when finished to free Barretenberg resources. For a worker client, await `destroy()` to free its backends and terminate the worker. Browser threading depends on cross-origin isolation; otherwise proving uses one thread.

- Persist `GroupIssuer.state()`, member secrets and recorded leaf fields, root logs, snapshots, and `NullifierBook.state()` where continuity matters. The issuer defaults to retaining 30 recent snapshots; its state also contains member DIDs and commitments and needs access controls.

- The bundled CRS avoids third-party CRS downloads at proving time; artifacts still need to be packaged and served by the app. Pin verification keys out of band. Changing a circuit or acceptance policy requires coordinating member and verifier inputs.

## Error handling

### `MembershipError`

`membershipInputs` or `prove` throws when the member is absent or the witness fails membership, validity, predicates, or rate-limit constraints.

Check the published snapshot, enrollment leaf fields, current day, and message index. Refresh membership data rather than retrying the same unsatisfiable witness.

### `VerifyResult: malformed / unknownCircuit / statementMismatch / proofInvalid`

Low-level `MembershipVerifier.verify` returns `ok: false` for proof shape, unpinned artifact identity, an unexpected statement, or failed cryptographic verification.

Reject the proof and preserve the reason for diagnostics. Do not replace a pinned key or expected statement with values supplied by the prover.

### `Acceptance: stale / tooSmall / scope / signal / equivocation`

`acceptMembership` can reject a cryptographically valid proof on policy grounds. `equivocation` includes two conflicting signed roots as evidence.

Refresh roots for staleness, require the configured minimum set size, and recompute context locally. Preserve equivocation evidence and refuse that acceptance; use the full `AcceptReason` union for remaining failures.

### Limitations

- Selective disclosure of individual credential fields (BBS+, SD-JWT). This is group membership only.

## API reference

Choose an entry point for the complete export list. Each export has a reference page with its signature, parameter types, return type, fields, and public methods where applicable.

- [`@toip-tools/anon-membership`](https://toip.tools/toolkit/anon-membership/api/)

  Browser and Node · 93 exports

- [`@toip-tools/anon-membership/worker`](https://toip.tools/toolkit/anon-membership/api/worker/)

  Browser · 2 exports

## Related

### Guides

- [Prove group membership with zero knowledge (Noir)](https://toip.tools/guides/zero-knowledge-membership/)

### Built on

- [`@toip-tools/trust-core`](https://toip.tools/toolkit/trust-core/): Identities, signed and encrypted Trust Tasks over TSP, credentials, status lists and local policy.

### Used by these examples

- [Tipline](https://toip.tools/examples/#tipline): A drop box that lets sources prove membership without revealing their identity.
- [Agora](https://toip.tools/examples/#agora): Anonymous forums with verified membership.

### Concepts

[Concepts](https://toip.tools/concepts/) explains the protocols behind anon-membership; the [FAQ](https://toip.tools/faq/) covers install, runtimes and support.

---

This is the Markdown version of https://toip.tools/toolkit/anon-membership/. An index of the documentation for AI assistants is at https://toip.tools/llms.txt.
