MediTrust — Private Health AI with Zero-Knowledge Compliance

----------Inspiration Medical records contain the most intimate, sensitive details of our lives—yet navigating complex diagnoses, lab panels, and medication schedules is overwhelming for everyday patients. Modern generative AI could serve as an empowering personal health copilot, but patients rightfully fear uploading their medical history to AI systems and cloud databases.

We asked ourselves: Can we build a health AI assistant that acts strictly on the user's terms, while mathematically proving to third parties that no private health information (PHI) or unauthorized data was disclosed—without revealing the underlying records?

This inspired MediTrust: a privacy-first health AI copilot where users define machine-readable privacy terms, and Midnight Network generates cryptographic Zero-Knowledge (ZK) attestations that prove the AI followed those terms to the letter.

--------What It Does MediTrust allows patients to review and query their health records securely:

User-Defined Terms: Patients define machine-readable policies $\mathcal{T}$ (e.g., redact personal identifiers, disallow verbatim diagnosis quotes, round sensitive lab values, and restrict AI operations). AI Copilot with Deterministic Guardrails: A medical assistant streams conversational answers based only on the patient's active records. A real-time compliance rule engine intercepts and sanitizes every output against the user's policy. Midnight Zero-Knowledge Attestations: For every answer, MediTrust publishes cryptographic commitments and ZK predicate proofs to Midnight's private blockchain. Third-Party Verification Portal: Insurers, doctors, or researchers can verify that an answer adhered to the agreed-upon policy $\mathcal{T}$ without ever seeing the raw patient records.

--------How We Built It

  1. The Architecture MediTrust is engineered as a monorepo consisting of:

Frontend: React + Vite SPA built with modern TypeScript, Radix UI tabs, and responsive CSS modules. Backend API: Express & TypeScript server with Server-Sent Events (SSE) for low-latency streaming and real-time guardrail evaluation. Privacy & ZK Layer: Midnight Network Compact smart contract (attestation-registry.compact) + simulated/live Midnight.js client.

  1. Cryptographic Formulation & Commitments For every interaction, MediTrust computes deterministic cryptographic commitments using SHA-256 and Midnight's private state mechanisms:

$$\mathcal{C}{\text{data}} = \text{SHA256}(\text{Canonicalize}(\mathcal{R}))$$ $$\mathcal{C}{\text{terms}} = \text{SHA256}(\text{Canonicalize}(\mathcal{T}))$$ $$\mathcal{C}{\text{output}} = \text{SHA256}(\mathcal{O}{\text{sanitized}})$$ $$\mathcal{C}_{\text{trace}} = \text{MerkleRoot}(\text{RuleTrace})$$

The Midnight contract registers the public statement $\mathcal{S}$: $$\mathcal{S} = \big( \mathcal{C}{\text{data}}, \mathcal{C}{\text{terms}}, \mathcal{C}{\text{output}}, \mathcal{C}{\text{trace}}, \text{predicateResult} = \text{true} \big)$$

This allows anyone holding $\mathcal{O}{\text{sanitized}}$ and $\mathcal{T}$ to verify the proof $\pi{\text{ZK}}$ against the on-chain commitment without obtaining the private records $\mathcal{R}$.

--------- We Ran Into Deterministic Guardrails vs. LLM Non-Determinism: LLMs can rephrase or hallucinate subtle medical details. We engineered a dual-layer approach: strict system prompt conditioning followed by a deterministic AST/regex rule engine that scans outputs for 8+ classes of PII, verbatim quote matches, and exact numerical lab thresholds. SSE Streaming with Post-Processing Sanitization: Streaming text token-by-token while enforcing sentence-level compliance required designing a multi-event stream (text, compliance, attestation, done) that updates the client UI dynamically. Zero-Knowledge State Management: Structuring data commitments so that zero-knowledge proofs could be verified without exposing intermediate record state required careful canonicalization of JSON structures and rule traces.

----------- Accomplishments We're Proud Of End-to-End Working Pipeline: Real-time LLM streaming, live rule engine sanitization, SHA-256 commitment generation, and Midnight smart contract integration all operating seamlessly. Comprehensive Test Suite: 20 passing automated tests covering crypto hashing, canonical stringification, PII redaction, and compliance edge cases. Usable & Empathetic UX: Clean dark-mode interface with clear visual feedback showing which rules passed, what was redacted, and direct access to cryptographic proof inspect cards.

--------- What We Learned How to model decentralized privacy applications where computation happens on sensitive data, but verification happens on-chain via Zero-Knowledge proofs. Writing and structuring Compact smart contracts for the Midnight Network. Techniques for building robust compliance audit trails that bridge probabilistic AI outputs with deterministic cryptographic commitments.

--------What's Next for MediTrust FHIR & Apple HealthKit Integration: Directly import EHR records from clinical portals and wearable devices. Multi-Party Computation (MPC): Allow multiple medical specialists or clinical trials to query aggregated patient records under joint privacy terms. Full Mainnet Deployment: Transitioning from testnet/simulation to fully deployed ZK-SNARK verifiers on Midnight mainnet.

Built With

  • blockchain
  • compact
  • cryptography
  • css-modules
  • express.js
  • google-gemini
  • midnight-network
  • node.js
  • openai-api
  • privacy
  • radix-ui
  • react
  • rest-api
  • server-sent-events
  • sha-256
  • smart-contracts
  • supabase
  • typescript
  • vite
  • vitest
  • web3
  • zero-knowledge-proofs
  • zod
Share this project:

Updates

Submission history