diff --git a/nomos-mix/message/src/mock/mod.rs b/nomos-mix/message/src/mock/mod.rs index 4fea886a..fcb8cf6f 100644 --- a/nomos-mix/message/src/mock/mod.rs +++ b/nomos-mix/message/src/mock/mod.rs @@ -9,12 +9,13 @@ use crate::MixMessage; const NODE_ID_SIZE: usize = 32; -// TODO: Move PADDED_PAYLOAD_SIZE and MAX_LAYERS to the upper layer (service layer). -const PADDED_PAYLOAD_SIZE: usize = 2048; +// TODO: Move MAX_PAYLOAD_SIZE and MAX_LAYERS to the upper layer (service layer). +const MAX_PAYLOAD_SIZE: usize = 2048; const PAYLOAD_PADDING_SEPARATOR: u8 = 0x01; const PAYLOAD_PADDING_SEPARATOR_SIZE: usize = 1; const MAX_LAYERS: usize = 5; -pub const MESSAGE_SIZE: usize = NODE_ID_SIZE * MAX_LAYERS + PADDED_PAYLOAD_SIZE; +pub const MESSAGE_SIZE: usize = + NODE_ID_SIZE * MAX_LAYERS + MAX_PAYLOAD_SIZE + PAYLOAD_PADDING_SEPARATOR_SIZE; #[derive(Clone, Debug)] pub struct MockMixMessage; @@ -38,7 +39,7 @@ impl MixMessage for MockMixMessage { if node_ids.is_empty() || node_ids.len() > MAX_LAYERS { return Err(Error::InvalidNumberOfLayers); } - if payload.len() > PADDED_PAYLOAD_SIZE - PAYLOAD_PADDING_SEPARATOR_SIZE { + if payload.len() > MAX_PAYLOAD_SIZE { return Err(Error::PayloadTooLarge); } @@ -53,10 +54,7 @@ impl MixMessage for MockMixMessage { // Append payload with padding message.extend(payload); message.push(PAYLOAD_PADDING_SEPARATOR); - message.extend( - std::iter::repeat(0) - .take(PADDED_PAYLOAD_SIZE - payload.len() - PAYLOAD_PADDING_SEPARATOR_SIZE), - ); + message.extend(std::iter::repeat(0).take(MAX_PAYLOAD_SIZE - payload.len())); Ok(message) } diff --git a/nomos-mix/message/src/sphinx/mod.rs b/nomos-mix/message/src/sphinx/mod.rs index b70ceb2f..c7d62fb5 100644 --- a/nomos-mix/message/src/sphinx/mod.rs +++ b/nomos-mix/message/src/sphinx/mod.rs @@ -12,7 +12,7 @@ pub struct SphinxMessage; const ASYM_KEY_SIZE: usize = 32; // TODO: Move these constants to the upper layer (service layer). -const PADDED_PAYLOAD_SIZE: usize = 2048; +const MAX_PAYLOAD_SIZE: usize = 2048; const MAX_LAYERS: usize = 5; impl MixMessage for SphinxMessage { @@ -20,7 +20,7 @@ impl MixMessage for SphinxMessage { type PrivateKey = [u8; ASYM_KEY_SIZE]; type Error = Error; - const DROP_MESSAGE: &'static [u8] = &[0; Packet::size(MAX_LAYERS, PADDED_PAYLOAD_SIZE)]; + const DROP_MESSAGE: &'static [u8] = &[0; Packet::size(MAX_LAYERS, MAX_PAYLOAD_SIZE)]; fn build_message( payload: &[u8], @@ -33,7 +33,7 @@ impl MixMessage for SphinxMessage { .collect::>(), MAX_LAYERS, payload, - PADDED_PAYLOAD_SIZE, + MAX_PAYLOAD_SIZE, )?; Ok(packet.to_bytes()) } diff --git a/nomos-mix/message/src/sphinx/packet.rs b/nomos-mix/message/src/sphinx/packet.rs index 583ac3ce..2db9159e 100644 --- a/nomos-mix/message/src/sphinx/packet.rs +++ b/nomos-mix/message/src/sphinx/packet.rs @@ -4,7 +4,7 @@ use sphinx_packet::{ keys::RoutingKeys, routing::{FINAL_HOP, FORWARD_HOP}, }, - payload::Payload, + payload::{Payload, PAYLOAD_OVERHEAD_SIZE}, }; use crate::sphinx::ASYM_KEY_SIZE; @@ -55,7 +55,8 @@ impl Packet { let payload = sphinx_packet::payload::Payload::encapsulate_message( payload, &payload_keys, - max_payload_size, + // sphinx_packet::payload requires this parameter to include the overhead size. + max_payload_size + PAYLOAD_OVERHEAD_SIZE, )?; Ok(Packet { @@ -191,7 +192,10 @@ impl Packet { } pub const fn size(max_layers: usize, max_payload_size: usize) -> usize { - ASYM_KEY_SIZE + EncryptedRoutingInformation::size(max_layers) + max_payload_size + ASYM_KEY_SIZE + + EncryptedRoutingInformation::size(max_layers) + + max_payload_size + + PAYLOAD_OVERHEAD_SIZE } }