phantom-protocol 0.1.0

Phantom Universal Transport Core SDK — post-quantum secure L4/L6 network framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Byte-exact wire-format vectors — the wire-stability freeze (Phase 6).
//!
//! Every other protocol test drives Rust types ↔ Rust types (in-proc or
//! loopback), so both ends move together: a layout, endianness, or
//! discriminant regression in the hand-rolled packet codec
//! ([`PacketHeader::to_wire`]) or in `borsh` (handshake messages) passes
//! silently. These vectors are the only test in the repo that pins the
//! *bytes*. Each case asserts two directions:
//!
//!   * **encode** — `serialize(value) == fixture`, and
//!   * **decode** — `deserialize(fixture)` reconstructs the same value
//!     (direct `PartialEq` where the type derives it; canonical re-encode plus
//!     field spot-checks for the borsh handshake structs, which do not).
//!
//! The fixtures live in `core/tests/wire_vectors/*.bin` and are committed. A
//! change to any on-wire byte fails CI; an *intentional* wire change is landed
//! by bumping `WIRE_VERSION` / `PROTOCOL_VERSION` and regenerating:
//!
//! ```sh
//! PHANTOM_REGEN_WIRE_VECTORS=1 cargo test --manifest-path core/Cargo.toml \
//!     --test wire_vectors
//! ```
//!
//! The crypto material in the handshake vectors is deterministic *filler* of
//! the real field lengths (1184-byte ML-KEM-768 encap key, 1088-byte
//! ciphertext, 1952-byte ML-DSA-65 verifying key, 3309-byte signature, …), not
//! a valid KEM/signature. This freezes the serialization *container*; the PQ
//! encodings themselves are validated against published NIST KATs separately.
//! The transcript-hash freeze (real `compute_transcript_hash` over real borsh) is a
//! `#[cfg(test)]` unit test inside `transport::handshake`, which can reach the
//! private transcript type.
//!
//! Scoped to the default (non-fips) build: the fips build is a distinct wire
//! (different `PROTOCOL_VARIANT`, 65-byte classical KEM key) and would need its
//! own vector set. Under `--features fips` this test compiles to nothing.
#![cfg(not(feature = "fips"))]

use std::fs;
use std::path::PathBuf;

use phantom_protocol::crypto::hybrid_kem::{HybridCiphertext, HybridKeyPackage};
use phantom_protocol::crypto::hybrid_sign::{HybridSignature, HybridVerifyingKey};
use phantom_protocol::crypto::pow::{PoWChallenge, PoWSolution};
use phantom_protocol::transport::handshake::{
    ClientHello, HelloRetryRequest, ServerHello, PROTOCOL_VARIANT, PROTOCOL_VERSION,
};
use phantom_protocol::transport::types::{
    PacketFlags, PacketHeader, PhantomPacket, SessionId, WIRE_VERSION,
};

// ─── Deterministic filler ──────────────────────────────────────────────────
//
// A distinct `seed` per field makes a cross-field copy bug visible in a
// hexdump and gives the independent decoder (`tests/wire_vectors_decode.py`)
// recognisable, recomputable bytes.

/// `n` bytes ramping from `seed` (wrapping) — `seed, seed+1, seed+2, …`.
fn pat(seed: u8, n: usize) -> Vec<u8> {
    (0..n).map(|i| seed.wrapping_add(i as u8)).collect()
}

/// 32-byte ramp from `seed`.
fn arr32(seed: u8) -> [u8; 32] {
    pat(seed, 32)
        .try_into()
        .expect("pat(seed, 32) is exactly 32 bytes")
}

// Canonical field lengths for the default (non-fips) build.
const ML_KEM_PK_LEN: usize = 1184; // FIPS-203 ML-KEM-768 encapsulation key
const ML_KEM_CT_LEN: usize = 1088; // FIPS-203 ML-KEM-768 ciphertext
const ML_DSA_PK_LEN: usize = 1952; // FIPS-204 ML-DSA-65 verifying key
const ML_DSA_SIG_LEN: usize = 3309; // FIPS-204 ML-DSA-65 signature
const CLASSICAL_PK_LEN: usize = 32; // X25519 public key

fn sample_key_package() -> HybridKeyPackage {
    HybridKeyPackage {
        classical_pk: arr32(0x10),
        ml_kem_pk: pat(0x20, ML_KEM_PK_LEN),
    }
}

fn sample_ciphertext() -> HybridCiphertext {
    HybridCiphertext {
        classical_pk: arr32(0x30),
        ml_kem_ct: pat(0x40, ML_KEM_CT_LEN),
    }
}

fn sample_verify_key() -> HybridVerifyingKey {
    HybridVerifyingKey {
        ed25519_pk: arr32(0x50),
        ml_dsa_pk: pat(0x60, ML_DSA_PK_LEN),
    }
}

fn sample_signature() -> HybridSignature {
    HybridSignature {
        ed25519_sig: pat(0x70, 64)
            .try_into()
            .expect("pat(_, 64) is exactly 64 bytes"),
        ml_dsa_sig: pat(0x80, ML_DSA_SIG_LEN),
    }
}

fn sample_pow_challenge() -> PoWChallenge {
    PoWChallenge {
        nonce: arr32(0x90),
        difficulty: 20,
    }
}

fn sample_pow_solution() -> PoWSolution {
    PoWSolution {
        nonce: arr32(0x90),
        solution: 0x0123_4567_89AB_CDEF,
    }
}

fn sample_client_hello_minimal() -> ClientHello {
    ClientHello {
        client_key_package: sample_key_package(),
        client_verify_key: sample_verify_key(),
        nonce: arr32(0xA0),
        version: PROTOCOL_VERSION,
        cookie: None,
        pow_solution: None,
        resume_session_id: None,
        resumption_binder: None,
        protocol_variant: PROTOCOL_VARIANT.to_vec(),
        early_data: None,
    }
}

fn sample_client_hello_full() -> ClientHello {
    ClientHello {
        client_key_package: sample_key_package(),
        client_verify_key: sample_verify_key(),
        nonce: arr32(0xA0),
        version: PROTOCOL_VERSION,
        cookie: Some(arr32(0xB0)),
        pow_solution: Some(sample_pow_solution()),
        resume_session_id: Some(arr32(0xC0)),
        resumption_binder: Some(arr32(0xC8)),
        protocol_variant: PROTOCOL_VARIANT.to_vec(),
        early_data: Some(pat(0xD0, 48)),
    }
}

fn sample_server_hello(accepted: bool) -> ServerHello {
    ServerHello {
        server_key_package: sample_key_package(),
        ciphertext: sample_ciphertext(),
        server_verify_key: sample_verify_key(),
        signature: sample_signature(),
        session_id: arr32(0xE0),
        early_data_accepted: accepted,
    }
}

fn sample_hrr_cookie() -> HelloRetryRequest {
    HelloRetryRequest {
        challenge: None,
        cookie: Some(arr32(0xF0)),
    }
}

fn sample_hrr_pow() -> HelloRetryRequest {
    HelloRetryRequest {
        challenge: Some(sample_pow_challenge()),
        cookie: None,
    }
}

fn sample_header() -> PacketHeader {
    PacketHeader::new(
        SessionId::from_bytes(arr32(0x01)),
        7,
        42,
        PacketFlags::new(PacketFlags::ENCRYPTED | PacketFlags::RELIABLE),
    )
    .with_epoch(3)
    .with_path_id(1)
}

fn sample_packet_data() -> PhantomPacket {
    PhantomPacket::new(sample_header(), pat(0x11, 64))
}

fn sample_packet_ack() -> PhantomPacket {
    PhantomPacket::ack(SessionId::from_bytes(arr32(0x01)), 7, 42)
}

fn sample_packet_ext() -> PhantomPacket {
    let mut p = PhantomPacket::new(sample_header(), pat(0x11, 16));
    p.extensions = vec![0xFF, 0x01, 0x00, 0x04, b't', b'e', b's', b't'];
    p
}

// ─── wire / borsh encoders ───────────────────────────────────────────────────

fn ser_header(h: &PacketHeader) -> Vec<u8> {
    h.to_wire().to_vec()
}

fn ser_packet(p: &PhantomPacket) -> Vec<u8> {
    p.to_wire()
}

fn ser_borsh<T: borsh::BorshSerialize>(v: &T) -> Vec<u8> {
    borsh::to_vec(v).expect("borsh serialize")
}

// ─── fixture I/O + freeze assertion ─────────────────────────────────────────

fn fixtures_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/wire_vectors")
}

fn regen() -> bool {
    std::env::var_os("PHANTOM_REGEN_WIRE_VECTORS").is_some()
}

/// Pretty index of the first differing byte, for actionable failures.
fn first_diff(a: &[u8], b: &[u8]) -> String {
    let n = a.len().min(b.len());
    for i in 0..n {
        if a[i] != b[i] {
            return format!(
                "first diff at byte {i}: got 0x{:02x}, fixture 0x{:02x}",
                a[i], b[i]
            );
        }
    }
    format!(
        "identical for first {n} bytes; lengths differ ({} vs {})",
        a.len(),
        b.len()
    )
}

/// Assert `actual` equals the committed fixture `name`, or (re)write it when
/// `PHANTOM_REGEN_WIRE_VECTORS` is set. Returns the canonical fixture bytes so
/// the caller can run its decode check against exactly what is on disk.
fn freeze(name: &str, actual: &[u8]) -> Vec<u8> {
    let path = fixtures_dir().join(name);
    if regen() {
        fs::create_dir_all(fixtures_dir()).expect("create wire_vectors dir");
        fs::write(&path, actual).expect("write fixture");
        eprintln!("regenerated {name} ({} bytes)", actual.len());
        return actual.to_vec();
    }
    let expected = fs::read(&path).unwrap_or_else(|e| {
        panic!("missing wire-vector fixture {name}: {e}\nregenerate with PHANTOM_REGEN_WIRE_VECTORS=1 cargo test --test wire_vectors")
    });
    assert_eq!(
        actual,
        expected.as_slice(),
        "wire bytes changed for {name} ({}). This is a WIRE-BREAKING change: \
         if intentional, bump WIRE_VERSION/PROTOCOL_VERSION and regenerate with \
         PHANTOM_REGEN_WIRE_VECTORS=1.",
        first_diff(actual, &expected),
    );
    expected
}

// ─── packet vectors (hand-rolled big-endian codec) ──────────────────────────

#[test]
fn vector_packet_header() {
    let h = sample_header();
    let bytes = ser_header(&h);
    assert_eq!(
        bytes.len(),
        PacketHeader::SIZE,
        "header is the AEAD AAD; must be exactly {} bytes",
        PacketHeader::SIZE
    );
    let frozen = freeze("packet_header.bin", &bytes);
    let decoded = PacketHeader::from_wire(&frozen).expect("decode packet header");
    assert_eq!(decoded, h);
    assert_eq!(decoded.version, WIRE_VERSION);
    assert_eq!(decoded.stream_id, 7);
    assert_eq!(decoded.sequence, 42);
    assert_eq!(decoded.epoch, 3);
    assert_eq!(decoded.path_id, 1);
}

#[test]
fn vector_phantom_packet_data() {
    let p = sample_packet_data();
    let frozen = freeze("phantom_packet_data.bin", &ser_packet(&p));
    let decoded = PhantomPacket::from_wire(&frozen).expect("decode data packet");
    assert_eq!(decoded, p);
    assert_eq!(decoded.payload, pat(0x11, 64));
    assert!(decoded.extensions.is_empty());
}

#[test]
fn vector_phantom_packet_ack() {
    let p = sample_packet_ack();
    let frozen = freeze("phantom_packet_ack.bin", &ser_packet(&p));
    let decoded = PhantomPacket::from_wire(&frozen).expect("decode ack packet");
    assert_eq!(decoded, p);
    assert!(decoded.header.flags.is_ack());
    assert!(decoded.payload.is_empty());
}

#[test]
fn vector_phantom_packet_extensions() {
    let p = sample_packet_ext();
    let frozen = freeze("phantom_packet_extensions.bin", &ser_packet(&p));
    let decoded = PhantomPacket::from_wire(&frozen).expect("decode ext packet");
    assert_eq!(decoded, p);
    assert_eq!(
        decoded.extensions,
        vec![0xFF, 0x01, 0x00, 0x04, b't', b'e', b's', b't']
    );
}

// ─── handshake vectors (borsh) ──────────────────────────────────────────────
//
// The handshake structs do not derive PartialEq, so the decode check is
// canonical-re-encode (borsh has exactly one encoding per value) plus field
// spot-checks.

fn roundtrip_borsh<T: borsh::BorshSerialize + borsh::BorshDeserialize>(name: &str, value: &T) -> T {
    let frozen = freeze(name, &ser_borsh(value));
    let decoded: T = borsh::from_slice(&frozen).expect("borsh decode");
    assert_eq!(
        ser_borsh(&decoded),
        frozen,
        "borsh re-encode of the decoded {name} must reproduce the fixture"
    );
    decoded
}

#[test]
fn vector_hybrid_key_package() {
    let v = sample_key_package();
    let d = roundtrip_borsh("hybrid_key_package.bin", &v);
    assert_eq!(d.classical_pk.len(), CLASSICAL_PK_LEN);
    assert_eq!(d.ml_kem_pk.len(), ML_KEM_PK_LEN);
}

#[test]
fn vector_hybrid_ciphertext() {
    let v = sample_ciphertext();
    let d = roundtrip_borsh("hybrid_ciphertext.bin", &v);
    assert_eq!(d.classical_pk.len(), CLASSICAL_PK_LEN);
    assert_eq!(d.ml_kem_ct.len(), ML_KEM_CT_LEN);
}

#[test]
fn vector_hybrid_verifying_key() {
    let v = sample_verify_key();
    // HybridVerifyingKey derives PartialEq/Eq — use direct equality.
    let frozen = freeze("hybrid_verifying_key.bin", &ser_borsh(&v));
    let decoded: HybridVerifyingKey = borsh::from_slice(&frozen).expect("decode vk");
    assert_eq!(decoded, v);
    assert_eq!(decoded.ml_dsa_pk.len(), ML_DSA_PK_LEN);
}

#[test]
fn vector_hybrid_signature() {
    let v = sample_signature();
    let d = roundtrip_borsh("hybrid_signature.bin", &v);
    assert_eq!(d.ed25519_sig.len(), 64);
    assert_eq!(d.ml_dsa_sig.len(), ML_DSA_SIG_LEN);
}

#[test]
fn vector_pow_challenge() {
    let v = sample_pow_challenge();
    let d = roundtrip_borsh("pow_challenge.bin", &v);
    assert_eq!(d.difficulty, 20);
    assert_eq!(d.nonce, arr32(0x90));
}

#[test]
fn vector_pow_solution() {
    let v = sample_pow_solution();
    let d = roundtrip_borsh("pow_solution.bin", &v);
    assert_eq!(d.solution, 0x0123_4567_89AB_CDEF);
}

#[test]
fn vector_client_hello_minimal() {
    let v = sample_client_hello_minimal();
    let d = roundtrip_borsh("client_hello_minimal.bin", &v);
    assert_eq!(d.version, PROTOCOL_VERSION);
    assert_eq!(d.protocol_variant, PROTOCOL_VARIANT.to_vec());
    assert!(d.cookie.is_none());
    assert!(d.pow_solution.is_none());
    assert!(d.resume_session_id.is_none());
    assert!(d.early_data.is_none());
    assert_eq!(d.client_key_package.ml_kem_pk.len(), ML_KEM_PK_LEN);
}

#[test]
fn vector_client_hello_full() {
    let v = sample_client_hello_full();
    let d = roundtrip_borsh("client_hello_full.bin", &v);
    assert_eq!(d.version, PROTOCOL_VERSION);
    assert_eq!(d.protocol_variant, PROTOCOL_VARIANT.to_vec());
    assert!(d.cookie.is_some());
    assert!(d.pow_solution.is_some());
    assert!(d.resume_session_id.is_some());
    assert_eq!(d.early_data.as_deref(), Some(pat(0xD0, 48).as_slice()));
}

#[test]
fn vector_server_hello() {
    let v = sample_server_hello(true);
    let d = roundtrip_borsh("server_hello.bin", &v);
    assert!(d.early_data_accepted);
    assert_eq!(d.ciphertext.ml_kem_ct.len(), ML_KEM_CT_LEN);
    assert_eq!(d.signature.ml_dsa_sig.len(), ML_DSA_SIG_LEN);
}

#[test]
fn vector_server_hello_rejected() {
    let v = sample_server_hello(false);
    let d = roundtrip_borsh("server_hello_rejected.bin", &v);
    assert!(!d.early_data_accepted);
}

#[test]
fn vector_hello_retry_request_cookie() {
    let v = sample_hrr_cookie();
    let d = roundtrip_borsh("hello_retry_request_cookie.bin", &v);
    assert!(d.challenge.is_none());
    assert_eq!(d.cookie, Some(arr32(0xF0)));
}

#[test]
fn vector_hello_retry_request_pow() {
    let v = sample_hrr_pow();
    let d = roundtrip_borsh("hello_retry_request_pow.bin", &v);
    assert!(d.cookie.is_none());
    assert_eq!(d.challenge.as_ref().map(|c| c.difficulty), Some(20));
}