Guide · `@toip-tools/anon-membership`, `@toip-tools/trust-core`

# Prove group membership with zero knowledge (Noir)

Prove membership in a group without saying which member, with a Noir circuit and UltraHonk proofs, and count one voice per member with nullifiers.

Sometimes the verifier should learn only "this is one of our members", not which one: a whistleblower, a union vote, a forum for licensed nurses. A zero-knowledge membership proof says exactly that. A *nullifier* tied to a scope lets the verifier notice the same member twice in that scope, still without learning who it is.

This program enrolls three members with an issuer, has two of them vote, has one vote again, and shows that someone who never enrolled can't produce a proof.

## Before you start

You need Node 22.13 or later. In a new folder, install the packages and `tsx`, a TypeScript runner:

Terminal:

```bash
mkdir zero-knowledge-membership && cd zero-knowledge-membership
npm init -y && npm pkg set type=module
npm install @toip-tools/anon-membership @toip-tools/trust-core tsx
```

## The code

Save this as `zero-knowledge-membership.ts`. It is the complete program; the site's checks run it on every build.

zero-knowledge-membership.ts:

```ts
// Prove "I am a member of this group" with a zero-knowledge proof (Noir, UltraHonk), without saying which
// member. A scoped nullifier lets the verifier count one voice per member, still without knowing who.
import {
  GroupIssuer, MembershipProver, MembershipVerifier, NullifierBook, acceptMembership, dayOf, enroll,
  initHashing, memberSecret, membershipInputs, messageOf, scopeOf, treeFromRoot,
} from "@toip-tools/anon-membership";
import { Bus, Endpoint, SystemClock, identityFromSeed } from "@toip-tools/trust-core";

await initHashing();
const clock = new SystemClock();
const bus = new Bus(clock);
const random = () => crypto.getRandomValues(new Uint8Array(32));

// The issuer (a union, say) keeps the group and decides who may enroll.
const union = identityFromSeed(random());
const roster = new Set<string>();
const issuer = new GroupIssuer({ identity: union, clock, name: "members" });
issuer.serve(new Endpoint({ identity: union, transport: bus, clock }), (did) => roster.has(did));

// Each member enrolls a commitment to a secret derived from their own seed. The issuer never sees the secret.
const members = [random(), random(), random()].map((seed) => ({ seed, id: identityFromSeed(seed) }));
for (const m of members) {
  roster.add(m.id.did);
  await enroll(new Endpoint({ identity: m.id, transport: bus, clock }), union.did, issuer.groupId, memberSecret(m.seed, issuer.groupId));
}
const { root, snapshot } = issuer.publish(); // a signed root for today

const prover = await MembershipProver.create();
const verifier = await MembershipVerifier.create();
const book = new NullifierBook(undefined, { prefix: "Member" }); // labels distinct members "Member A", "Member B", …
const scope = scopeOf("survey/v1", union.did, "strike-vote-2026"); // one voice per member in this scope

async function vote(seed: Uint8Array, choice: string) {
  const signal = messageOf("survey/v1", { choice }); // the proof is bound to this exact message
  const secret = memberSecret(seed, issuer.groupId);
  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 proof = await prover.prove(statement, witness);
  const r = await acceptMembership(proof, {
    policy: { groups: [{ group: issuer.groupId, issuer: union.did }], vkHashes: verifier.vkHashes(), maxEpochAge: 3, minSize: 3 },
    logs: { [issuer.groupId]: issuer.log() },
    verifier, scope, signal, clock,
  });
  if (!r.ok) return console.log(`"${choice}": refused (${r.reason})`);
  const { label, first } = book.label(issuer.groupId, scope, r.nullifier);
  console.log(`"${choice}": a member voted (${label}${first ? "" : ", again: count it once"}); proof ${JSON.stringify(proof).length} bytes`);
}

await vote(members[1].seed, "yes");
await vote(members[2].seed, "no");
await vote(members[1].seed, "yes"); // same member, same scope: same nullifier

// Someone who never enrolled can't build a proof for the group.
try {
  await vote(random(), "yes");
} catch (e) {
  console.log("An outsider:", (e as Error).message);
}

await prover.destroy(); // the prover and verifier hold worker threads until destroyed
await verifier.destroy();
```

Run it:

Terminal:

```bash
npx tsx zero-knowledge-membership.ts
```

Output:

```text
"yes": a member voted (Member A); proof 20274 bytes
"no": a member voted (Member B); proof 20274 bytes
"yes": a member voted (Member A, again: count it once); proof 20274 bytes
An outsider: not a member: no group holds this secret's leaf
```

Keys are random, so identifiers and sizes differ on each run. From a clone of the repository, run `npx tsx --tsconfig site/tsconfig.json site/examples/guides/zero-knowledge-membership.ts`, or read it in [`site/examples/guides/zero-knowledge-membership.ts`](https://github.com/yourbuddyconner/toip-tools/blob/main/site/examples/guides/zero-knowledge-membership.ts).

## How it works

- Each member derives a secret from their own seed and enrolls only a commitment to it, over a TSP Trust Task. The issuer adds it to a Merkle tree (depth 20, up to about a million members) and signs one root per day.
- The member proves, in a Noir circuit, that their commitment is in a tree under a signed root, and outputs a nullifier: a hash of their secret and the scope. The proof is bound to the exact message (`signal`).
- `acceptMembership` checks the proof, the issuer's signed roots, how old the root may be (`maxEpochAge`), the smallest acceptable group (`minSize`), and the scope and signal. `NullifierBook` gives each distinct nullifier a stable label.
- Proofs use UltraHonk from Barretenberg in its zero-knowledge mode. The proving setup data is bundled: nothing is downloaded when proving or verifying.

## Limits and production notes

- This is experimental cryptography. UltraHonk's zero-knowledge mode is *designed* to be zero-knowledge; the package README knows of no published proof that it is, and Noir says it is not yet for production use.
- In a browser, prove in a Web Worker with `@toip-tools/anon-membership/worker`, and serve the page cross-origin isolated (`Cross-Origin-Opener-Policy: same-origin`, `Cross-Origin-Embedder-Policy: require-corp`) for multithreaded proving. The package's tests prove and verify in Chromium.
- A small group gives little anonymity: set `minSize` to what your users need.

## Related

### API used

[`GroupIssuer`](https://toip.tools/toolkit/anon-membership/api/GroupIssuer/) · [`MembershipProver`](https://toip.tools/toolkit/anon-membership/api/MembershipProver/) · [`MembershipVerifier`](https://toip.tools/toolkit/anon-membership/api/MembershipVerifier/) · [`acceptMembership`](https://toip.tools/toolkit/anon-membership/api/acceptMembership/) · [`NullifierBook`](https://toip.tools/toolkit/anon-membership/api/NullifierBook/) · [`membershipInputs`](https://toip.tools/toolkit/anon-membership/api/membershipInputs/) · [`enroll`](https://toip.tools/toolkit/anon-membership/api/enroll/) · [`memberSecret`](https://toip.tools/toolkit/anon-membership/api/memberSecret/) · [`scopeOf`](https://toip.tools/toolkit/anon-membership/api/scopeOf/) · [`messageOf`](https://toip.tools/toolkit/anon-membership/api/messageOf/)

### Libraries

- [`@toip-tools/anon-membership`](https://toip.tools/toolkit/anon-membership/): Prove group membership and attribute requirements without revealing which member you are.
- [`@toip-tools/trust-core`](https://toip.tools/toolkit/trust-core/): Identities, signed and encrypted Trust Tasks over TSP, credentials, status lists and local policy.

### Concepts

- [Selective disclosure and zero-knowledge membership](https://toip.tools/concepts/#zk)

### More guides

- [Issue and verify a W3C verifiable credential in TypeScript](https://toip.tools/guides/issue-verify-credential/)
- [Delegate bounded authority to an AI agent with mandates](https://toip.tools/guides/delegate-to-ai-agent/)

---

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