ratchet: hybrid ML-KEM steps (KDF_RK v2, 2312-byte header, state v2)

Every ratchet keypair is {x25519, ml-kem-768}. On a rotation the stepper
encapsulates to the receiver's current ratchet KEM key; both the X25519 DH
and the ML-KEM secret feed KDF_RK under the new onionwire-ratchet-v2-kdfrk
domain, so a later X25519 break does not open a chain established by a
hybrid step.

Epoch 0 stays classical by construction: the bootstrap root must be a pure
function of the two long-lived prekeys for both-send-first convergence, and
a per-session ss_kem differs for every one-shot Noise dial. The first
rotation (including the bootstrap tie-break rotation) is hybrid, so the
residual HNDL window is exactly the epoch-0 chain. docs/THREAT_MODEL.md
states that window; no full PQ forward secrecy is claimed.

Header 0x02 adds kem_pub[1184] || kem_ct[1088] to the 40-byte classical
header and is used on every frame of a hybrid chain, so a lost first frame
is harmless. A step with no KEM section is rejected (hybrid step required)
and a hybrid step at a classical friend is rejected (unexpected hybrid
step): no classical fallback.

State moves to blob v0.2: fixed 211-byte prefix plus flagged KEM fields.
A v1 blob is dropped and re-bootstrapped (hard cut, like F8); a corrupt v2
or a newer version is an error, not a silent reset.

Tests: hybrid root agreement on both sides, both-send-first convergence
with hybrid keys, no-downgrade, tampered kem_pub/kem_ct fail closed,
MAX_SKIP on a hybrid chain, classical friends still step classically,
state freeze at v0.2 and the node-level hard cut.
This commit is contained in:
rust-dev 2026-10-20 12:54:30 -04:00 • committed by agent
commit b22abd7b2b
No known key found for this signature in database
7 changed files with 1198 additions and 168 deletions

View file

@ -86,6 +86,16 @@ pub fn decapsulate(seed: &[u8], ct: &[u8]) -> Result<Vec<u8>> {
Ok(ss.as_slice().to_vec())
}
/// Canonical encapsulation key for a stored 64-byte decapsulation seed.
///
/// The ratchet derives its KEM half from the X25519 ratchet secret, so it never
/// stores the ML-KEM seed separately; this recomputes the public half.
pub fn pk_from_seed(seed: &[u8]) -> Result<Vec<u8>> {
let seed_arr = Seed::try_from(seed).map_err(|_| Error("seed length".into()))?;
let dk = DecapsulationKey768::from_seed(seed_arr);
Ok(dk.encapsulation_key().to_bytes().as_slice().to_vec())
}
fn encapsulation_key(pk: &[u8]) -> Result<EncapsulationKey768> {
if pk.len() != PK_LEN {
return Err(Error("pk length".into()));