feat(mesh): add transport capability negotiation
TransportCapability enum classifies transports by bandwidth/MTU: - Unconstrained (≥1 Mbps): Full MLS with PQ-KEM - Medium (≥10 kbps): Full MLS classical - Constrained (≥1 kbps): MLS-Lite with signature - SeverelyConstrained (<1 kbps): MLS-Lite minimal TransportManager now provides: - best_transport() - highest capability transport - recommended_crypto() - appropriate crypto mode - supports_mls() - whether any transport handles full MLS - select_for_size() - best transport for a given payload CryptoMode enum with overhead estimates for each mode.
This commit is contained in:
@@ -35,6 +35,77 @@ impl fmt::Display for TransportAddr {
|
||||
}
|
||||
}
|
||||
|
||||
/// Transport capability level for crypto mode selection.
|
||||
///
|
||||
/// Ordered from worst to best so max_by_key picks the best transport.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum TransportCapability {
|
||||
/// Very low bandwidth, severely duty-cycled (LoRa SF11-SF12, serial).
|
||||
/// MLS-Lite without signature preferred.
|
||||
SeverelyConstrained = 0,
|
||||
/// Low bandwidth, duty-cycled (LoRa SF7-SF10).
|
||||
/// Classical MLS marginal, prefer MLS-Lite with sig.
|
||||
Constrained = 1,
|
||||
/// Medium bandwidth (BLE, slower WiFi).
|
||||
/// Supports full MLS with classical crypto.
|
||||
Medium = 2,
|
||||
/// High-bandwidth, low-latency (QUIC, TCP, WiFi).
|
||||
/// Supports full MLS with PQ-KEM, large KeyPackages.
|
||||
Unconstrained = 3,
|
||||
}
|
||||
|
||||
impl TransportCapability {
|
||||
/// Determine capability from bitrate and MTU.
|
||||
pub fn from_metrics(bitrate_bps: u64, mtu: usize) -> Self {
|
||||
match (bitrate_bps, mtu) {
|
||||
(b, _) if b >= 1_000_000 => Self::Unconstrained, // ≥1 Mbps
|
||||
(b, m) if b >= 10_000 && m >= 200 => Self::Medium, // ≥10 kbps, decent MTU
|
||||
(b, m) if b >= 1_000 || m >= 100 => Self::Constrained, // ≥1 kbps
|
||||
_ => Self::SeverelyConstrained,
|
||||
}
|
||||
}
|
||||
|
||||
/// Recommended crypto mode for this capability level.
|
||||
pub fn recommended_crypto(&self) -> CryptoMode {
|
||||
match self {
|
||||
Self::Unconstrained => CryptoMode::MlsHybrid,
|
||||
Self::Medium => CryptoMode::MlsClassical,
|
||||
Self::Constrained => CryptoMode::MlsLiteSigned,
|
||||
Self::SeverelyConstrained => CryptoMode::MlsLiteUnsigned,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether full MLS is viable on this transport.
|
||||
pub fn supports_mls(&self) -> bool {
|
||||
matches!(self, Self::Unconstrained | Self::Medium)
|
||||
}
|
||||
}
|
||||
|
||||
/// Crypto mode for mesh messaging.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum CryptoMode {
|
||||
/// Full MLS with X25519 + ML-KEM-768 hybrid.
|
||||
MlsHybrid,
|
||||
/// Full MLS with classical X25519 only.
|
||||
MlsClassical,
|
||||
/// MLS-Lite with Ed25519 signature.
|
||||
MlsLiteSigned,
|
||||
/// MLS-Lite without signature (smallest overhead).
|
||||
MlsLiteUnsigned,
|
||||
}
|
||||
|
||||
impl CryptoMode {
|
||||
/// Approximate overhead in bytes for this mode.
|
||||
pub fn overhead_bytes(&self) -> usize {
|
||||
match self {
|
||||
Self::MlsHybrid => 2700, // PQ KeyPackage alone
|
||||
Self::MlsClassical => 400, // Classical KeyPackage + message
|
||||
Self::MlsLiteSigned => 262, // MLS-Lite with sig
|
||||
Self::MlsLiteUnsigned => 129, // MLS-Lite minimal
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Metadata about a transport's capabilities.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TransportInfo {
|
||||
@@ -48,6 +119,18 @@ pub struct TransportInfo {
|
||||
pub bidirectional: bool,
|
||||
}
|
||||
|
||||
impl TransportInfo {
|
||||
/// Compute capability level from this transport's metrics.
|
||||
pub fn capability(&self) -> TransportCapability {
|
||||
TransportCapability::from_metrics(self.bitrate, self.mtu)
|
||||
}
|
||||
|
||||
/// Recommended crypto mode for this transport.
|
||||
pub fn recommended_crypto(&self) -> CryptoMode {
|
||||
self.capability().recommended_crypto()
|
||||
}
|
||||
}
|
||||
|
||||
/// Received packet from a transport.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TransportPacket {
|
||||
@@ -137,4 +220,70 @@ mod tests {
|
||||
assert_eq!(a, b);
|
||||
assert_ne!(a, c);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn capability_ordering() {
|
||||
// Higher value = better capability
|
||||
assert!(TransportCapability::Unconstrained > TransportCapability::Medium);
|
||||
assert!(TransportCapability::Medium > TransportCapability::Constrained);
|
||||
assert!(TransportCapability::Constrained > TransportCapability::SeverelyConstrained);
|
||||
|
||||
// max_by_key should pick the best
|
||||
let caps = vec![
|
||||
TransportCapability::Constrained,
|
||||
TransportCapability::Unconstrained,
|
||||
TransportCapability::Medium,
|
||||
];
|
||||
let best = caps.into_iter().max().unwrap();
|
||||
assert_eq!(best, TransportCapability::Unconstrained);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn capability_recommended_crypto() {
|
||||
assert_eq!(
|
||||
TransportCapability::Unconstrained.recommended_crypto(),
|
||||
CryptoMode::MlsHybrid
|
||||
);
|
||||
assert_eq!(
|
||||
TransportCapability::Medium.recommended_crypto(),
|
||||
CryptoMode::MlsClassical
|
||||
);
|
||||
assert_eq!(
|
||||
TransportCapability::Constrained.recommended_crypto(),
|
||||
CryptoMode::MlsLiteSigned
|
||||
);
|
||||
assert_eq!(
|
||||
TransportCapability::SeverelyConstrained.recommended_crypto(),
|
||||
CryptoMode::MlsLiteUnsigned
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transport_info_capability() {
|
||||
let tcp_info = TransportInfo {
|
||||
name: "tcp".to_string(),
|
||||
mtu: 1500,
|
||||
bitrate: 100_000_000, // 100 Mbps
|
||||
bidirectional: true,
|
||||
};
|
||||
assert_eq!(tcp_info.capability(), TransportCapability::Unconstrained);
|
||||
assert_eq!(tcp_info.recommended_crypto(), CryptoMode::MlsHybrid);
|
||||
|
||||
let lora_info = TransportInfo {
|
||||
name: "lora".to_string(),
|
||||
mtu: 51,
|
||||
bitrate: 300,
|
||||
bidirectional: true,
|
||||
};
|
||||
assert_eq!(lora_info.capability(), TransportCapability::SeverelyConstrained);
|
||||
assert_eq!(lora_info.recommended_crypto(), CryptoMode::MlsLiteUnsigned);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crypto_mode_overhead() {
|
||||
assert!(CryptoMode::MlsHybrid.overhead_bytes() > 2000);
|
||||
assert!(CryptoMode::MlsClassical.overhead_bytes() < 500);
|
||||
assert!(CryptoMode::MlsLiteSigned.overhead_bytes() < 300);
|
||||
assert!(CryptoMode::MlsLiteUnsigned.overhead_bytes() < 150);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user