Multi-site clinical trials face a persistent problem: the same participant can enroll at two different sites, either by mistake or to claim duplicate stipends. The traditional fix — sharing participant records across sites — directly conflicts with HIPAA and GDPR. Centralizing names or document numbers creates a single breach target and violates the principle of data minimization.

TrialFence solves this with a cryptographic gate built on Semaphore v4 zero-knowledge proofs. Sites learn only whether a participant is "already enrolled" or "accepted" — never who the participant is. No names, no document numbers, no PII leaves the coordinator.

The Core Idea

TrialFence replaces the shared identity database with a nullifier-based on-chain registry. Each participant generates a Semaphore Identity locally in the browser. The commitment (a hash of the identity) is the only value shared with the coordinator. When a participant tries to enroll, they generate a Groth16 proof that:

  1. They are a member of the protocol's Merkle tree (registered by the issuer)
  2. The proof message matches the submitting site
  3. The scope matches the correct protocol

The on-chain contract verifies the proof and checks the nullifier. If the nullifier already exists, the transaction reverts with TrialFence_DuplicateEnrollment. If the proof is invalid, it reverts with TrialFence_InvalidProof.

Architecture

The system has three services:

  • Issuer (port 4001): Verifies physical ID documents off-chain, HMACs the document number for deduplication, and calls issueMembership(commitment) on-chain. Identity verification stays centralized at this boundary.
  • Relayer (port 4002): Validates enrollment payloads, re-checks the scope, runs a staticCall, then submits the transaction with the relayer key. Participants never pay gas.
  • Web App (Next.js 15): Volunteer, coordinator, and sponsor/IRB flows. Same-origin API proxies avoid CORS.

The smart contract (Solidity 0.8.28) handles the enrollment gate. The scope is computed as keccak256("TrialFence-Protocol-v1:" || protocolId), ensuring that a proof for Protocol A cannot be used for Protocol B. The nullifier is derived from the identity secret and the scope, so the same participant at a second site in the same protocol produces the same nullifier — and gets rejected.

Key Design Decisions

Scope-binding: The proof is scoped to a specific protocol. A wrong protocol reverts with TrialFence_WrongScope. A wrong site reverts with TrialFence_SiteMismatch. This prevents cross-protocol linkage — a participant can enroll in multiple protocols without those enrollments being connected.

Stale root expiration: Membership changes mark the previous Merkle root as superseded. In-flight proofs remain valid for MERKLE_TREE_DURATION (1 hour), then revert with TrialFence_MerkleTreeRootIsExpired. This prevents replay attacks while allowing for network latency.

No PII on-chain: Events carry only protocolId, siteId, scope, nullifier, root, and the relayer address. The issuer stores HMAC keyed-digests of document numbers — never names or raw numbers.

PII-free monitoring: The monitor generates deterministic anomaly flags over public events and denial records. Auditing does not require access to participant identity.

Tech Stack

  • Solidity 0.8.28 with Hardhat (contracts)
  • TypeScript 5.7 with Node.js 22 (server)
  • Next.js 15 App Router (web)
  • Semaphore v4 (ZK proof system, Groth16)
  • SQLite via node:sqlite (local prototype, portable to Postgres)
  • WebAssembly (snarkjs proof generation in browser)

Getting Started

Prerequisites: Node.js >= 22, npm >= 10.

npm ci
npm run start:render

The render-start.mjs script boots an in-memory Hardhat chain on port 8545, deploys the contracts, starts the issuer on 4001 and relayer on 4002, and serves the Next.js app on 3000. The local chain and database are ephemeral — re-run the walkthrough after a restart.

For multi-terminal development:

npm install
npm run node -w contracts
npm run deploy:local -w contracts

Why This Matters

TrialFence demonstrates that privacy-preserving coordination is possible without sacrificing determinism. The on-chain nullifier registry provides a trustless, auditable duplicate check that no single party can tamper with. The ZK proof ensures that the verifier learns nothing about the participant beyond "this person is a valid member of this protocol group."

This pattern generalizes beyond clinical trials. Any multi-organization system that needs to prevent duplicate actions without sharing identity — voter registration across jurisdictions, KYC across financial institutions, license verification across regulatory bodies — can adopt the same architecture.

The full source is available at https://github.com/alwaysalearner1234/TrialFence. The project includes 22 integration tests with real Groth16 proofs, a CI pipeline, and documented configuration.