CLAUDE_consumer - xero/leviathan-crypto GitHub Wiki
Note
Ships with the npm package. Inside the repo? Read AGENTS.md instead.
Zero-dependency WASM crypto for TS/JS. All compute in WASM (outside JS JIT); TS layer = input validation + ergonomics.
| Family | Primitives |
|---|---|
| Symmetric AEAD | Serpent-256, XChaCha20-Poly1305, AES-256-GCM-SIV |
| Post-quantum sig | ML-DSA, SLH-DSA, PQ-only hybrid composites |
| Classical sig | Ed25519, ECDSA-P256, classical+PQ hybrid composites |
| Key agreement | ML-KEM, X25519 |
| Transparency log | Merkle log (C2SP-conformant) |
| Forward-secret ratchet | Signal SPQR KDF (rule 8) |
Two hierarchies, one suite extension point. Tier = data shape. Suite = crypto choice.
| Tier | AEAD | Signatures |
|---|---|---|
| One-shot | Seal | Sign |
| Streaming | SealStream / OpenStream | SignStream / VerifyStream |
| Parallel | SealStreamPool (Web Workers) | n/a |
| Suite arg | CipherSuite | SignatureSuite |
Seal blob = single-chunk SealStream output (interchangeable). Symmetric: SerpentCipher, XChaCha20Cipher, AESGCMSIVCipher. MlKemSuite(MlKem*, inner) wraps any of them for PQ hybrid (same CipherSuite interface).
Prefer the high-level surface (Seal / Sign / Fortuna). Handles KDF, nonce management, auth, key wipes, counter binding. Rejects tampered/reordered/spliced inputs before plaintext release. Low-level primitives (raw ChaCha20, SerpentCbc, etc.) require reading their wiki page first.
-
init()required. Nothing works beforeinit(). Throws on missing module. Idempotent. Use/embeddedsubpath for bundled gzip+base64 blob. -
dispose()stateful infinally. Stateful classes hold key material in WASM untildispose()zeros it. Wrap intry { ... } finally { x.dispose() }. Atomic one-shots (Seal.encrypt,Sign.sign, hashes, MACs) self-wipe. -
Stateful = exclusive module access. A stateful class (
SHAKE128,ChaCha20,SerpentCtr/Cbc,SealStream,MlKem*, etc.) owns its WASM module for its lifetime. Second stateful instance on same module throws. Atomic methods on same module throw while a stateful holder is alive. Pool workers isolated. -
AEAD
decrypt()throws on auth failure.Seal.decrypt, AEADdecrypt(),OpenStream.pullnever return null or corrupted plaintext. Wrong key, tampered blob, corrupted bytes all surface as exceptions. -
Raw
verify()returns bool.Sign.verifythrows. Raw primitives (MlDsa*.verify,SlhDsa*.verify, hybridverifyPrehashed) returnfalseon bad sig; only contract violations throw.Sign.verifyenvelope throws on bad sig (parity withSeal.decrypt). -
Pure-mode and prehash sigs NOT interchangeable.
dsa.signvsdsa.verifyHashbind different M' domain bytes (0x00 vs 0x01, FIPS 204 §3.6.4 / FIPS 205 §10.2.2). Sigs don't cross even with identical messages. SignatureSuite enforces at the type level. -
v3 sign envelope:
ctxrequired.Sign.sign/Sign.verify/SignStream/VerifyStreamall requirectx. Passnew Uint8Array()for empty. Each suite prependsctxDomain(blocks cross-suite verify). Per-callctx≤ 255 bytes (FIPS 204 §3.6.1); longer throwsSigningError('sig-ctx-too-long'). Per-call ceiling =253 - len(ctxDomain)(221-234 bytes across catalog). -
Ratchet = KDF primitives, not a session. Forward secrecy + post-compromise security primitives only. State machine, message counters, header format, epoch orchestration, transport = app concerns. NOT a drop-in Signal client.
-
Stream plaintext chunk ≤ 64 KiB.
SealStream.push/SealStreamPool.sealcap each plaintext chunk at the suite'swasmChunkSize(65536; Serpent reserves 16B for PKCS7). ThechunkSizeopt is validated onlyCHUNK_MIN..CHUNK_MAXat construct, then throwsplaintext exceeds N bytes, split into smaller chunksat call time. To bound memory or wire-frame size, send more chunks; never raisechunkSize. (Sealone-shot caps atCHUNK_MAX.)
Pattern: leviathan-crypto/<mod> exports <mod>Init(source); leviathan-crypto/<mod>/embedded exports <mod>Wasm. Twelve modules: serpent, chacha20, aes, sha2, sha3, keccak, mlkem, mldsa, slhdsa, blake3, curve25519, p256.
Aliases share binary + instance slot:
| Alias | Backed by |
|---|---|
| keccak | sha3 |
| ed25519 | curve25519 |
| x25519 | curve25519 |
| ecdsa | p256 |
No /embedded: leviathan-crypto/ratchet, leviathan-crypto/stream, leviathan-crypto/sign, leviathan-crypto/merkle.
Other refs:
| Topic | wiki |
|---|---|
| init() / WasmSource | https://github.com/xero/leviathan-crypto/wiki/init |
| Loading strategies | https://github.com/xero/leviathan-crypto/wiki/loader |
| CDN usage | https://github.com/xero/leviathan-crypto/wiki/cdn |
| Content-Security-Policy | https://github.com/xero/leviathan-crypto/wiki/csp |
| Worked examples | https://github.com/xero/leviathan-crypto/wiki/examples |
| Utilities | https://github.com/xero/leviathan-crypto/wiki/utils |
| Argon2id integration | https://github.com/xero/leviathan-crypto/wiki/argon2id |
| CipherSuite interface | https://github.com/xero/leviathan-crypto/wiki/ciphersuite |
| SignatureSuite catalog | https://github.com/xero/leviathan-crypto/wiki/signaturesuite |
Seal + SerpentCipher round-trip:
import { init, Seal, SerpentCipher } from 'leviathan-crypto'
import { serpentWasm } from 'leviathan-crypto/serpent/embedded'
import { sha2Wasm } from 'leviathan-crypto/sha2/embedded'
await init({ serpent: serpentWasm, sha2: sha2Wasm })
const key = SerpentCipher.keygen()
const blob = Seal.encrypt(SerpentCipher, key, plaintext)
try {
const pt = Seal.decrypt(SerpentCipher, key, blob)
} catch {
// wrong key, tampered blob, or corrupted bytes
}Streaming: Seal → SealStream + OpenStream. PQ hybrid: SerpentCipher → MlKemSuite(new MlKem768(), SerpentCipher). Same call site, wire format, catch semantics.