Skip to content

Commit b04d3ba

Browse files
committed
add reserved byte to reply surb serialisation (#5731)
1 parent b2dfdda commit b04d3ba

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

common/nymsphinx/anonymous-replies/src/reply_surb.rs

-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use nym_sphinx_addressing::nodes::{
99
};
1010
use nym_sphinx_params::packet_sizes::PacketSize;
1111
use nym_sphinx_params::{PacketType, ReplySurbKeyDigestAlgorithm};
12-
use nym_sphinx_types::constants::PAYLOAD_KEY_SEED_SIZE;
1312
use nym_sphinx_types::{
1413
NymPacket, SURBMaterial, SphinxError, HEADER_SIZE, NODE_ADDRESS_LENGTH, SURB,
1514
X25519_WITH_EXPLICIT_PAYLOAD_KEYS_VERSION,
@@ -127,12 +126,6 @@ impl ReplySurb {
127126
})
128127
}
129128

130-
/// Returns the expected number of bytes the [`ReplySURB`] will take after serialization using the new encoding format.
131-
/// Useful for deserialization from a bytes stream.
132-
pub fn v2_serialised_len(num_hops: u8) -> usize {
133-
Self::BASE_OVERHEAD + num_hops as usize * PAYLOAD_KEY_SEED_SIZE
134-
}
135-
136129
pub fn encryption_key(&self) -> &SurbEncryptionKey {
137130
&self.encryption_key
138131
}

common/nymsphinx/anonymous-replies/src/requests/v2.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,28 @@ fn v2_reply_surbs_serialised_len(surbs: &[ReplySurb]) -> usize {
3232
}
3333
}
3434

35-
// when serialising surbs are always prepended with u16-encoded count an u8-encoded number of hops
36-
3 + num_surbs * v2_reply_surb_serialised_len(num_hops)
35+
// when serialising surbs are always prepended with:
36+
// - u16-encoded count,
37+
// - u8-encoded number of hops
38+
// - u8 reserved value
39+
4 + num_surbs * v2_reply_surb_serialised_len(num_hops)
3740
}
3841

39-
// NUM_SURBS (u16) || HOPS (u8) || SURB_DATA
42+
// NUM_SURBS (u16) || HOPS (u8) || RESERVED (u8) || SURB_DATA
4043
fn recover_reply_surbs_v2(
4144
bytes: &[u8],
4245
) -> Result<(Vec<ReplySurb>, usize), InvalidReplyRequestError> {
43-
if bytes.len() < 2 {
46+
if bytes.len() < 4 {
4447
return Err(InvalidReplyRequestError::RequestTooShortToDeserialize);
4548
}
4649

4750
// we're not attaching more than 65k surbs...
4851
let num_surbs = u16::from_be_bytes([bytes[0], bytes[1]]);
4952
let num_hops = bytes[2];
50-
let mut consumed = 3;
53+
let _reserved = bytes[3];
54+
let mut consumed = 4;
5155

52-
let surb_size = ReplySurb::v2_serialised_len(num_hops);
56+
let surb_size = v2_reply_surb_serialised_len(num_hops);
5357
if bytes[consumed..].len() < num_surbs as usize * surb_size {
5458
return Err(InvalidReplyRequestError::RequestTooShortToDeserialize);
5559
}
@@ -69,11 +73,13 @@ fn recover_reply_surbs_v2(
6973
fn reply_surbs_bytes_v2(reply_surbs: &[ReplySurb]) -> impl Iterator<Item = u8> + use<'_> {
7074
let num_surbs = reply_surbs.len() as u16;
7175
let num_hops = reply_surbs_hops(reply_surbs);
76+
let reserved = 0;
7277

7378
num_surbs
7479
.to_be_bytes()
7580
.into_iter()
7681
.chain(once(num_hops))
82+
.chain(once(reserved))
7783
.chain(reply_surbs.iter().flat_map(|surb| surb.to_bytes()))
7884
}
7985

0 commit comments

Comments
 (0)