diff --git a/crates/rns-link/src/link.rs b/crates/rns-link/src/link.rs index 10403bf..31164be 100644 --- a/crates/rns-link/src/link.rs +++ b/crates/rns-link/src/link.rs @@ -231,7 +231,7 @@ impl Link { stale_since: None, keepalive: KeepaliveState::new(true), mtu: rns_wire::constants::MTU as u32, - mdu: rns_wire::constants::ENCRYPTED_MDU, + mdu: rns_wire::constants::LINK_MDU, resource_strategy: ResourceStrategy::default(), pending_requests: Vec::new(), incoming_resources: Vec::new(), @@ -490,7 +490,7 @@ impl Link { stale_since: None, keepalive: KeepaliveState::new(false), mtu: request.signalling.mtu.min(rns_wire::constants::MTU as u32), - mdu: rns_wire::constants::ENCRYPTED_MDU, + mdu: rns_wire::constants::LINK_MDU, resource_strategy: ResourceStrategy::default(), pending_requests: Vec::new(), incoming_resources: Vec::new(), diff --git a/crates/rns-protocol/src/resource.rs b/crates/rns-protocol/src/resource.rs index 174e0e5..ab6a564 100644 --- a/crates/rns-protocol/src/resource.rs +++ b/crates/rns-protocol/src/resource.rs @@ -1579,8 +1579,7 @@ impl OutboundTransfer { // The collision guard widens the search window so out-of-order // part requests don't fall outside the scanned range when the // receiver's consecutive height lags ours. - let guard_size = - crate::resource_adv::collision_guard_size(rns_wire::constants::ENCRYPTED_MDU); + let guard_size = crate::resource_adv::collision_guard_size(rns_wire::constants::LINK_MDU); let search_start = self.receiver_min_consecutive_height; let search_end = (search_start + guard_size).min(self.resource.num_parts()); @@ -1627,7 +1626,7 @@ impl OutboundTransfer { }; let hashmap_max_len = - crate::resource_adv::hashmap_max_len(rns_wire::constants::ENCRYPTED_MDU); + crate::resource_adv::hashmap_max_len(rns_wire::constants::LINK_MDU); // Python cancels if an exhausted request cursor is not aligned to // a hashmap segment boundary. @@ -1723,7 +1722,7 @@ impl OutboundTransfer { self.resource.random_hash.to_vec(), self.resource.flags, &self.resource.map_hashes, - rns_wire::constants::ENCRYPTED_MDU, + rns_wire::constants::LINK_MDU, ); // `ResourceAdvertisement::new` defaults to single-segment metadata // (`segment_index = 1`, `total_segments = 1`, `original_hash = @@ -2176,8 +2175,7 @@ impl InboundTransfer { self.last_activity = Instant::now(); self.retries_left = MAX_RETRIES; - let hashmap_max_len = - crate::resource_adv::hashmap_max_len(rns_wire::constants::ENCRYPTED_MDU); + let hashmap_max_len = crate::resource_adv::hashmap_max_len(rns_wire::constants::LINK_MDU); // Parse hashes from the hashmap data and insert into our map_hashes. // `segment` is wire-supplied: indices are bounded by total_parts @@ -4023,7 +4021,7 @@ mod tests { let mut sender = OutboundTransfer::new(data.clone(), false, Duration::from_millis(100)).unwrap(); assert!( - crate::resource_adv::hashmap_max_len(rns_wire::constants::ENCRYPTED_MDU) > 1, + crate::resource_adv::hashmap_max_len(rns_wire::constants::LINK_MDU) > 1, "test requires a multi-entry hashmap segment" ); @@ -4252,8 +4250,7 @@ mod tests { assert_eq!(inbound.hashmap_height, initial_height); // Build hashmap data for segment 0 with additional hashes - let hashmap_max_len = - crate::resource_adv::hashmap_max_len(rns_wire::constants::ENCRYPTED_MDU); + let hashmap_max_len = crate::resource_adv::hashmap_max_len(rns_wire::constants::LINK_MDU); let mut hashmap_bytes = Vec::new(); let end = outbound.map_hashes.len().min(hashmap_max_len); for i in 0..end { @@ -4368,8 +4365,7 @@ mod tests { fn test_hashmap_update_second_segment_bounded_by_total_parts() { // Resource large enough that its hashmap spans >1 segment: segment 1 // writes land, but never past total_parts even if overfull. - let hashmap_max_len = - crate::resource_adv::hashmap_max_len(rns_wire::constants::ENCRYPTED_MDU); + let hashmap_max_len = crate::resource_adv::hashmap_max_len(rns_wire::constants::LINK_MDU); let data = vec![0xCD; (hashmap_max_len + 12) * SDU]; let outbound = OutboundResource::new(data.clone(), false, None).unwrap(); let total_parts = outbound.num_parts(); @@ -4577,8 +4573,7 @@ mod tests { let mut request_data = Vec::new(); request_data.push(HASHMAP_IS_EXHAUSTED); // last_map_hash -- use the hash at hashmap_max_len boundary - let hashmap_max_len = - crate::resource_adv::hashmap_max_len(rns_wire::constants::ENCRYPTED_MDU); + let hashmap_max_len = crate::resource_adv::hashmap_max_len(rns_wire::constants::LINK_MDU); let boundary_idx = hashmap_max_len.min(sender.resource.num_parts()) - 1; request_data.extend_from_slice(&sender.resource.map_hashes[boundary_idx]); request_data.extend_from_slice(&sender.resource.resource_hash); diff --git a/crates/rns-protocol/src/resource_adv.rs b/crates/rns-protocol/src/resource_adv.rs index 8bf5b8a..d448853 100644 --- a/crates/rns-protocol/src/resource_adv.rs +++ b/crates/rns-protocol/src/resource_adv.rs @@ -289,8 +289,8 @@ mod tests { #[test] fn test_default_link_mdu_matches_python_hashmap_capacity() { - assert_eq!(rns_wire::constants::ENCRYPTED_MDU, 431); - assert_eq!(hashmap_max_len(rns_wire::constants::ENCRYPTED_MDU), 74); + assert_eq!(rns_wire::constants::LINK_MDU, 431); + assert_eq!(hashmap_max_len(rns_wire::constants::LINK_MDU), 74); } #[test] diff --git a/crates/rns-runtime/src/link_manager.rs b/crates/rns-runtime/src/link_manager.rs index 0ab1d8a..6bf6b92 100644 --- a/crates/rns-runtime/src/link_manager.rs +++ b/crates/rns-runtime/src/link_manager.rs @@ -4506,7 +4506,7 @@ mod tests { ..Default::default() }, &[], - rns_wire::constants::ENCRYPTED_MDU, + rns_wire::constants::LINK_MDU, 0, ); adv.original_hash = [0x99; 32]; @@ -4586,7 +4586,7 @@ mod tests { ..Default::default() }, &[], - rns_wire::constants::ENCRYPTED_MDU, + rns_wire::constants::LINK_MDU, 0, ); let encrypted = sender_link.encrypt(&adv.pack()).expect("encrypt"); diff --git a/crates/rns-wire/src/constants.rs b/crates/rns-wire/src/constants.rs index 20b41e4..9a1c6da 100644 --- a/crates/rns-wire/src/constants.rs +++ b/crates/rns-wire/src/constants.rs @@ -33,9 +33,23 @@ pub const MDU: usize = MTU - HEADER_MAXSIZE - 1; /// Encrypted link MDU, matching Python `RNS.Link.MDU`: /// floor((MTU - IFAC_MIN - HEADER_MIN - TOKEN_OVERHEAD) / AES128_BLOCKSIZE) /// * AES128_BLOCKSIZE - 1 = 431. -pub const ENCRYPTED_MDU: usize = +pub const LINK_MDU: usize = ((MTU - 1 - HEADER_MINSIZE - TOKEN_OVERHEAD) / AES128_BLOCKSIZE) * AES128_BLOCKSIZE - 1; +/// Maximum plaintext payload for one encrypted packet to a SINGLE destination. +/// +/// This is Python `RNS.Packet.ENCRYPTED_MDU`: the general MDU minus identity +/// encryption's ephemeral key and token overhead, rounded to an AES block. +pub const SINGLE_PACKET_ENCRYPTED_MDU: usize = + ((MDU - TOKEN_OVERHEAD - KEYSIZE / 16) / AES128_BLOCKSIZE) * AES128_BLOCKSIZE - 1; + +/// Former name for [`LINK_MDU`]. +/// +/// Kept as a compatibility alias while downstream users migrate to the +/// unambiguous name. +#[deprecated(since = "1.0.2", note = "use LINK_MDU")] +pub const ENCRYPTED_MDU: usize = LINK_MDU; + /// MDU available to plaintext (unencrypted) payloads. pub const PLAIN_MDU: usize = MDU; @@ -72,7 +86,8 @@ mod tests { assert_eq!(HEADER_MINSIZE, 19); assert_eq!(HEADER_MAXSIZE, 35); assert_eq!(MDU, 464); - assert_eq!(ENCRYPTED_MDU, 431); + assert_eq!(LINK_MDU, 431); + assert_eq!(SINGLE_PACKET_ENCRYPTED_MDU, 383); assert_eq!(TRUNCATED_HASHLENGTH / 8, 16); assert_eq!(HASHLENGTH / 8, 32); assert_eq!(KEYSIZE / 8, 64);