# Validation, Defect Discovery, Fault Injection, and Benchmarks

`Ridge-Lab/moonmls` is verified across four independent layers, orchestrated by a single reproducible entry point (`scripts/verify.py`) that records a deterministic SHA-256 source fingerprint and writes per-step logs and `summary.json` under `work/verification/<timestamp>-<profile>/`.

## 1. Official IETF RFC 9420 Interoperability (`16 / 16` Suites)

Run via:
```bash
node scripts/verify_ietf_vectors.mjs --fetch --profile quick
# Or run all 55 TreeKEM vectors (CS1/2/3/4/6) in addition to the other 15 suites:
node scripts/verify_ietf_vectors.mjs --fetch --treekem-suites 1,2,3,4,6
```

Vectors are fetched on demand from `mlswg/mls-implementations` pinned at commit `66e31c515528f7e69c4ad440fcedb733f4b0bf33` into gitignored `work/ietf-vectors/`. Every single assertion executes inside compiled MoonBit (`bridge/vectors.mbt`), not JavaScript.

**Verified run (`profile=quick`, Node.js v24, JS backend):**
- **16 / 16 suites passing** (`622` vectors passed, `0` failed, `130` skipped for unsupported CS5/CS7)
- `messages.json`: **300 / 300** (5,100 wire struct roundtrips across all 17 RFC 9420 types in 114 ms)
- `passive-client-random.json`: **200 consecutive randomized multi-member epochs** matching RFC 9420 `epoch_authenticator` byte-for-byte
- `tree-validation.json`: **70 / 70** across CS1, CS2, CS3, CS4, CS6
- `passive-client-welcome.json` (**40 / 40**) + `passive-client-handling-commit.json` (**65 / 65**)

### Real Protocol Defect Caught and Fixed via Independent IETF Vectors

During initial testing against `key-schedule.json` and `welcome.json`, every cipher suite passed `joiner_secret`, `welcome_secret`, and `init_secret`, yet failed `confirmation_tag` verification.
- **Root cause**: `key_schedule/key_schedule.mbt` derived `confirmation_key` with label `"confirmation"` instead of the exact RFC 9420 §8 label `"confirm"`.
- **Why unit tests alone did not catch it**: Self-consistent roundtrip tests between our own sender and receiver both used `"confirmation"`, so they agreed with each other while disagreeing with RFC 9420.
- **Fix & verification**: Corrected the label to `"confirm"` in commit `28d251a`, immediately turning `key-schedule.json`, `welcome.json`, `passive-client-welcome.json`, `passive-client-handling-commit.json`, and `passive-client-random.json` 100% green.

## 2. Adversarial Security & Fault-Injection Suite (`12 / 12` + 5 MoonBit Tests)

1. **Multi-target MoonBit security suite (`security_test.mbt`)** runs natively on `js`, `wasm`, `wasm-gc`, and `native`:
   - Tampered `PrivateMessage` ciphertext, AAD, and `encrypted_sender_data` rejection
   - Atomic state preservation when `process_commit` rejects a tampered `confirmation_tag`
   - Post-Compromise Security (PCS): cloned epoch-1 attacker state cannot decrypt epoch-2 traffic or process the victim's own `UpdatePath` commit after key rotation
   - RFC 9420 §10.1 rejection of `KeyPackage` with `init_key == encryption_key`
   - Rejection of tampered `RatchetTree` `parent_hash`
2. **Independent fault-injection harness (`scripts/verify_faults.mjs`)**: **12 / 12** passing (`0` failed), covering baseline validity plus 11 injected wire/AEAD/replay/PCS/eviction/varint faults.

## 3. Reproducible Performance & TreeKEM Scaling Benchmark

Measured with `node scripts/benchmark.mjs` (compiled MoonBit JS backend on Node.js; `native` and `wasm-gc` run substantially faster):

| Benchmark | Metric | Throughput / Size |
| --- | ---: | --- |
| `keygen` CS1 (`X25519/AES-128-GCM/Ed25519`) | 45.2 ms/op | 22.1 ops/s |
| `keygen` CS2 (`P-256/AES-128-GCM/ECDSA-P256`) | 104.8 ms/op | 9.5 ops/s |
| `keygen` CS3 (`X25519/ChaCha20-Poly1305/Ed25519`) | 41.8 ms/op | 24.0 ops/s |
| `keygen` CS4 (`X448/AES-256-GCM/Ed448`) | 167.7 ms/op | 6.0 ops/s |
| `keygen` CS6 (`X448/ChaCha20-Poly1305/Ed448`) | 168.0 ms/op | 6.0 ops/s |
| TreeKEM `Update` (CS1, `N = 2` members) | 67.3 ms/commit | Ratchet tree: 419 B |
| TreeKEM `Update` (CS1, `N = 8` members) | 140.2 ms/commit | Ratchet tree: 1,641 B |
| TreeKEM `Update` (CS1, `N = 32` members) | 406.3 ms/commit | Ratchet tree: 6,143 B |
| TreeKEM `Update` (CS1, `N = 64` members) | 808.2 ms/commit | Ratchet tree: 12,035 B |
| `PrivateMessage` encrypt (CS1, 256 B payload) | 12.4 ms/op | 80.5 ops/s (408 B wire) |
| `PrivateMessage` decrypt (CS1, 256 B payload) | 10.0 ms/op | 99.5 ops/s |
| `PrivateMessage` encrypt (CS3, 256 B payload) | 11.7 ms/op | 85.7 ops/s (408 B wire) |
| `PrivateMessage` decrypt (CS3, 256 B payload) | 10.3 ms/op | 97.1 ops/s |

## 4. Harness Failure-Propagation Self-Tests

`scripts/verify_harness_test.py` proves that `scripts/verify.py` never masks failures: an injected failing step returns a non-zero exit code and writes `passed: false` with the exact exit code and 64-hex-character `source_sha256` to `summary.json`.
