chat: pin the classical and hybrid frame floors under padding
All checks were successful
ci / test (pull_request) Successful in 19m7s
ci / sdk (pull_request) Successful in 2m13s

Padding and the hybrid header are independent layers: the bucket is inside
the ratchet AEAD, the 2312-byte header is outside it. tests/padding.rs now
drives the ladder through a real hybrid step and asserts 2601 bytes in the
Noise body (2622 on the wire) against the classical 329/350, plus the
35113-byte largest-bucket frame under the 65535 cap. The node-level PQ
round-trip is corrected to say what it actually proves: epoch 0 stays
classical, so its frame does not widen.
This commit is contained in:
agent 2026-10-20 13:12:06 -04:00
commit 7a3c0055b3
No known key found for this signature in database
2 changed files with 123 additions and 3 deletions

View file

@ -4,6 +4,12 @@
//! unit tests, because `tests/*.rs` cannot build a `Node`.
use onionwire::padding::{self, BUCKETS, MAX_BODY, PAD_HEADER_LEN};
use onionwire::ratchet::{
FRAME_VERSION, FRAME_VERSION_HYBRID, HEADER_LEN, HEADER_LEN_HYBRID, RatchetKeyPair,
RatchetState,
};
const ROOT: [u8; 32] = [0x7au8; 32];
#[test]
fn the_ladder_is_the_documented_one() {
@ -104,6 +110,117 @@ fn a_truncated_padding_header_is_rejected() {
assert!(padding::decode(b"\x00").is_err());
}
/// Two hybrid peers from one bootstrap root. `a` holds the lower ratchet key,
/// so the documented tie-break (`a` rotates first) is deterministic instead of
/// a coin flip per run.
fn hybrid_peers() -> (RatchetState, RatchetState) {
let k1 = RatchetKeyPair::from_secret([0x11u8; 32]);
let k2 = RatchetKeyPair::from_secret([0x22u8; 32]);
let (lo, hi) = if k1.public() < k2.public() {
(k1, k2)
} else {
(k2, k1)
};
let (lo_pub, hi_pub) = (lo.public(), hi.public());
let (lo_kem, hi_kem) = (lo.kem_public().to_vec(), hi.kem_public().to_vec());
(
RatchetState::bootstrap(ROOT, lo, hi_pub, Some(&hi_kem)),
RatchetState::bootstrap(ROOT, hi, lo_pub, Some(&lo_kem)),
)
}
/// Burn the bootstrap epoch so the next `a` send is its first hybrid step.
fn hybrid_step() -> (RatchetState, RatchetState) {
let (mut a, mut b) = hybrid_peers();
let a1 = a.encrypt(b"bootstrap").expect("encrypt");
let b1 = b.encrypt(b"bootstrap").expect("encrypt");
assert_eq!(b.decrypt(&a1).expect("b bootstrap"), b"bootstrap");
assert_eq!(a.decrypt(&b1).expect("a bootstrap"), b"bootstrap");
(a, b)
}
/// Padding sits *inside* the AEAD, so the bucket is unchanged by the header
/// version; the header sits outside it, so a hybrid step adds 2272 bytes to
/// the same padded plaintext. This is the true hybrid floor: 2601 in Noise
/// (2622 on the wire), against 329 (350) for the classical case.
#[test]
fn a_padded_plaintext_rides_inside_a_hybrid_header() {
let (mut a, mut b) = hybrid_step();
let padded = padding::encode(b"pq").expect("encode");
assert_eq!(padded.len(), 256, "a short body still pads to the floor");
let frame = a.encrypt(&padded).expect("encrypt");
assert_eq!(
frame[4], FRAME_VERSION_HYBRID,
"the first rotation is a hybrid step"
);
assert_eq!(
frame.len(),
4 + 1 + HEADER_LEN_HYBRID + 12 + 256 + 16,
"hybrid rat frame"
);
assert_eq!(frame.len(), 2601);
// The Noise transport adds its 16-byte tag and the framing header 5 bytes.
assert_eq!(frame.len() + 16 + 5, 2622, "hybrid frame on the wire");
assert_eq!(
HEADER_LEN_HYBRID - HEADER_LEN,
2272,
"a hybrid step's header is 2272 bytes larger"
);
assert_eq!(
padding::decode(&b.decrypt(&frame).expect("decrypt")).expect("decode"),
b"pq"
);
}
/// The ceiling: largest bucket (32768) plus the 2312-byte hybrid header plus
/// the frame's fixed overhead still fits one Noise body.
#[test]
fn the_largest_bucket_still_fits_inside_a_hybrid_frame() {
let (mut a, mut b) = hybrid_step();
let body = vec![b'a'; MAX_BODY];
let padded = padding::encode(&body).expect("encode");
assert_eq!(padded.len(), 32768);
let frame = a.encrypt(&padded).expect("encrypt");
assert_eq!(frame[4], FRAME_VERSION_HYBRID);
assert_eq!(frame.len(), 4 + 1 + HEADER_LEN_HYBRID + 12 + 32768 + 16);
assert_eq!(frame.len(), 35113, "largest hybrid rat frame");
assert!(
frame.len() + 16 <= onionwire::frame::MAX_FRAME,
"a hybrid frame must fit one Noise body"
);
assert!(frame.len() <= 65519, "documented rat frame maximum");
assert_eq!(
padding::decode(&b.decrypt(&frame).expect("decrypt"))
.expect("decode")
.len(),
MAX_BODY
);
}
/// A classical friend's floor is unchanged by the hybrid work: 329 in Noise.
#[test]
fn a_classical_frame_keeps_the_329_byte_floor() {
let padded = padding::encode(b"").expect("encode");
let k1 = RatchetKeyPair::from_secret([0x11u8; 32]);
let k2 = RatchetKeyPair::from_secret([0x22u8; 32]);
let (lo, hi) = if k1.public() < k2.public() {
(k1, k2)
} else {
(k2, k1)
};
let hi_pub = hi.public();
let mut a = RatchetState::bootstrap(ROOT, lo, hi_pub, None);
let frame = a.encrypt(&padded).expect("encrypt");
assert_eq!(frame[4], FRAME_VERSION, "classical friend, classical frame");
assert_eq!(frame.len(), 4 + 1 + HEADER_LEN + 12 + 256 + 16);
assert_eq!(frame.len(), 329);
assert_eq!(frame.len() + 16 + 5, 350, "classical frame on the wire");
}
#[test]
fn a_receiver_accepts_a_padded_plaintext_of_any_total_length() {
// Interop: another implementation's ladder is not ours, and the receiver