DEV Community

Cover image for I built ZKP auth so passwords never touch your server
Abhik Mondal
Abhik Mondal

Posted on

I built ZKP auth so passwords never touch your server

Every time you hear about a major breach, the headline is the same: "Millions of passwords exposed." Attackers get in, dump the database, and walk away with your users' bcrypt hashes. Given enough time and a GPU farm, weak passwords crack. Even strong ones end up in breach databases. The root cause is always the same: the password reached your server. It lived in a request body, got hashed, got stored, and now it's someone else's problem. What if the password never traveled at all?


What is Zero-Knowledge Proof auth?

Zero-Knowledge Proofs sound academic, but the core idea is surprisingly simple: you can prove you know a secret without revealing what the secret is.

Here's a real-world analogy. Imagine you want to prove to a bouncer that you know the secret password to a club, but you don't want to say it out loud where others can hear. Instead, the bouncer gives you a random challenge token and asks you to sign it in a way that only someone who knows the password could. You hand back the signed token. The bouncer checks the signature. You're in — and you never said the password.

That's exactly what ZKP auth does at a cryptographic level. Your browser derives a keypair from your password (the password stays in memory, never leaves), uses the private key to sign a server-issued challenge, and the server checks the signature against a stored public key. The server never sees the password. It doesn't even see a hash of the password. It sees a 32-byte public key and a one-time proof.

No password database means no password database to breach.


How the library works

ZKP Auth is a TypeScript monorepo with four focused packages:

Package What it does
@zkp-auth/core Schnorr proof primitives (Ed25519)
@zkp-auth/server Express middleware — register, challenge, verify
@zkp-auth/client Browser client — key derivation + proof generation
@zkp-auth/react useZKPAuth() hook for React apps

Install

# Server
npm install @zkp-auth/server

# Browser (vanilla)
npm install @zkp-auth/client

# Browser (React)
npm install @zkp-auth/client @zkp-auth/react
Enter fullscreen mode Exit fullscreen mode

Server — three middleware functions, that's it

// server.ts
import express from 'express';
import cookieParser from 'cookie-parser';
import {
  zkpRegister,
  zkpChallenge,
  zkpVerify,
  InMemoryChallengeStore,
} from '@zkp-auth/server';

const app = express();
app.use(express.json());
app.use(cookieParser());

// Swap InMemoryChallengeStore for Redis/Postgres in production
const store = new InMemoryChallengeStore();

// Your user store — replace with real DB calls
const users = new Map<string, Uint8Array>();

// Route 1 — register: receives and stores the user's public key
app.post(
  '/auth/register',
  zkpRegister({
    savePublicKey: async (userId, publicKey) => {
      users.set(userId, publicKey);
    },
  }),
  (_req, res) => res.json({ ok: true }),
);

// Route 2 — challenge: issues a fresh one-time nonce
app.post(
  '/auth/challenge',
  zkpChallenge({ store }),
  (_req, res) => res.json({ challengeHex: res.locals.zkpChallengeHex }),
);

// Route 3 — verify: checks the proof and issues a JWT cookie
app.post(
  '/auth/verify',
  zkpVerify({
    getPublicKey: async (userId) => users.get(userId) ?? null,
    store,
    jwtSecret: process.env.JWT_SECRET!,
  }),
  (_req, res) => {
    res.cookie('auth', res.locals.zkpToken, {
      httpOnly: true,
      sameSite: 'strict',
      secure: process.env.NODE_ENV === 'production',
    });
    res.json({ ok: true });
  },
);
Enter fullscreen mode Exit fullscreen mode

Client — React hook

// LoginForm.tsx
import { useZKPAuth } from '@zkp-auth/react';

export function LoginForm() {
  const { register, login, logout, isAuthenticated, loading, error, user } =
    useZKPAuth();

  if (isAuthenticated) {
    return (
      <div>
        <p>Welcome, {user?.userId}!</p>
        <button onClick={logout}>Log out</button>
      </div>
    );
  }

  return (
    <>
      {error && <p style={{ color: 'red' }}>{error.message}</p>}

      <form onSubmit={async (e) => {
        e.preventDefault();
        const f = new FormData(e.currentTarget);
        await register(f.get('username') as string, f.get('password') as string);
      }}>
        <h2>Register</h2>
        <input name="username" placeholder="Username" required />
        <input name="password" type="password" placeholder="Password" />
        <button type="submit" disabled={loading}>Register</button>
      </form>

      <form onSubmit={async (e) => {
        e.preventDefault();
        const f = new FormData(e.currentTarget);
        await login(f.get('username') as string, f.get('password') as string);
      }}>
        <h2>Log in</h2>
        <input name="username" placeholder="Username" required />
        <input name="password" type="password" placeholder="Password" />
        <button type="submit" disabled={loading}>
          {loading ? 'Authenticating…' : 'Log in'}
        </button>
      </form>
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

That's the entire auth surface. No password ever leaves the browser.


How it works under the hood

The protocol is a Schnorr Proof of Knowledge over Ed25519 with the Fiat–Shamir transform. Here's the flow in plain English:

BROWSER                              SERVER
──────────────────────────────────────────────────────
1. Derive keypair from password
   (PBKDF2 + SHA-512 — stays in memory)

2. POST /auth/challenge  ──────────►  Issue challenge c (random nonce)
                         ◄──────────  Return c

3. Generate commitment R = r·G
   Compute s = r + c·privKey        (private scalar stays local)
   POST /auth/verify (R, s, userId)

                         ──────────►  Fetch stored pubKey for userId
                                      Verify: s·G == R + c·pubKey
                                      ✓  Issue JWT cookie
Enter fullscreen mode Exit fullscreen mode

The server only ever stores the 32-byte public key — mathematically useless without the private scalar, which only the browser can derive (from the password, which never travels).

Replay attacks are impossible: every challenge c is a fresh one-time nonce that expires server-side after a single use.

The crypto is built on @noble/curves — an audited, zero-dependency library. No custom crypto primitives.

For the full mathematical derivation and threat model, see the Security Model docs.


Get started

npm install @zkp-auth/server        # server
npm install @zkp-auth/client        # browser / vanilla JS
npm install @zkp-auth/client @zkp-auth/react  # React
Enter fullscreen mode Exit fullscreen mode

Pre-1.0 notice: This library has not yet undergone a formal third-party audit. Review the security model before deploying to production.

Stars, issues, and PRs are all welcome. If you're building something with it, I'd love to hear about it in the comments.

Top comments (0)