Mix: Re-define max_payload_size parameter

This commit is contained in:
Youngjoon Lee 2024-11-22 18:38:18 +09:00
parent 006f5dba89
commit cb708de1b0
No known key found for this signature in database
GPG Key ID: 303963A54A81DD4D
3 changed files with 16 additions and 14 deletions

View File

@ -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)
}

View File

@ -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::<Vec<_>>(),
MAX_LAYERS,
payload,
PADDED_PAYLOAD_SIZE,
MAX_PAYLOAD_SIZE,
)?;
Ok(packet.to_bytes())
}

View File

@ -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
}
}