Compare commits

...

3 Commits

Author SHA1 Message Date
Drazen 5ed7da9bdb Fix orphan ! 2025-02-25 10:42:51 +01:00
Drazen 9bedda8a24 Remove prints 2025-02-25 10:39:20 +01:00
Drazen 7d601782fb Disable acks, friendlier SURB construction 2025-02-21 14:12:55 +01:00
6 changed files with 45 additions and 7 deletions
Generated
+2
View File
@@ -6778,6 +6778,7 @@ dependencies = [
"rand 0.8.5", "rand 0.8.5",
"rand_chacha 0.3.1", "rand_chacha 0.3.1",
"serde", "serde",
"sphinx-packet",
"thiserror 2.0.11", "thiserror 2.0.11",
"wasm-bindgen", "wasm-bindgen",
] ]
@@ -6832,6 +6833,7 @@ name = "nym-sphinx-framing"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bytes", "bytes",
"cfg-if",
"log", "log",
"nym-metrics", "nym-metrics",
"nym-sphinx-acknowledgements", "nym-sphinx-acknowledgements",
@@ -12,6 +12,7 @@ rand = { workspace = true }
bs58 = { workspace = true } bs58 = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
sphinx-packet = { workspace = true }
nym-crypto = { path = "../../crypto", features = ["stream_cipher", "rand"] } nym-crypto = { path = "../../crypto", features = ["stream_cipher", "rand"] }
nym-sphinx-addressing = { path = "../addressing" } nym-sphinx-addressing = { path = "../addressing" }
@@ -7,11 +7,12 @@ use nym_sphinx_addressing::clients::Recipient;
use nym_sphinx_addressing::nodes::{NymNodeRoutingAddress, MAX_NODE_ADDRESS_UNPADDED_LEN}; use nym_sphinx_addressing::nodes::{NymNodeRoutingAddress, MAX_NODE_ADDRESS_UNPADDED_LEN};
use nym_sphinx_params::packet_sizes::PacketSize; use nym_sphinx_params::packet_sizes::PacketSize;
use nym_sphinx_params::{PacketType, ReplySurbKeyDigestAlgorithm}; use nym_sphinx_params::{PacketType, ReplySurbKeyDigestAlgorithm};
use nym_sphinx_types::{NymPacket, SURBMaterial, SphinxError, SURB}; use nym_sphinx_types::{Destination, NymPacket, SURBMaterial, SphinxError, SURB};
use nym_topology::{NymRouteProvider, NymTopologyError}; use nym_topology::{NymRouteProvider, NymTopologyError};
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
use serde::de::{Error as SerdeError, Visitor}; use serde::de::{Error as SerdeError, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sphinx_packet::route::Node;
use std::fmt::{self, Formatter}; use std::fmt::{self, Formatter};
use std::time; use std::time;
@@ -83,6 +84,25 @@ impl ReplySurb {
packet_size.plaintext_size() - ack_overhead - ReplySurbKeyDigestAlgorithm::output_size() - 1 packet_size.plaintext_size() - ack_overhead - ReplySurbKeyDigestAlgorithm::output_size() - 1
} }
pub fn construct_with_route<R>(
rng: &mut R,
destination: Destination,
average_delay: time::Duration,
route: &[Node],
) -> Result<Self, NymTopologyError>
where
R: RngCore + CryptoRng,
{
let delays = nym_sphinx_routing::generate_hop_delays(average_delay, route.len());
let surb_material = SURBMaterial::new(route.to_vec(), delays, destination);
// this can't fail as we know we have a valid route to gateway and have correct number of delays
Ok(ReplySurb {
surb: surb_material.construct_SURB().unwrap(),
encryption_key: SurbEncryptionKey::new(rng),
})
}
// TODO: should this return `ReplySURBError` for consistency sake // TODO: should this return `ReplySURBError` for consistency sake
// or keep `NymTopologyError` because it's the only error it can actually return? // or keep `NymTopologyError` because it's the only error it can actually return?
pub fn construct<R>( pub fn construct<R>(
@@ -123,11 +143,15 @@ impl ReplySurb {
pub fn to_bytes(&self) -> Vec<u8> { pub fn to_bytes(&self) -> Vec<u8> {
// KEY || SURB_BYTES // KEY || SURB_BYTES
self.encryption_key let bytes: Vec<u8> = self.encryption_key
.to_bytes() .to_bytes()
.into_iter() .into_iter()
.chain(self.surb.to_bytes()) .chain(self.surb.to_bytes())
.collect() .collect();
assert_eq!(bytes.len(), ReplySurb::serialized_len());
bytes
} }
pub fn from_bytes(bytes: &[u8]) -> Result<Self, ReplySurbError> { pub fn from_bytes(bytes: &[u8]) -> Result<Self, ReplySurbError> {
@@ -170,6 +170,7 @@ impl RepliableMessage {
} }
pub fn try_from_bytes(bytes: &[u8]) -> Result<Self, InvalidReplyRequestError> { pub fn try_from_bytes(bytes: &[u8]) -> Result<Self, InvalidReplyRequestError> {
// println!("Trying to deserialize message: {} bytes", bytes.len());
if bytes.len() < SENDER_TAG_SIZE + 1 { if bytes.len() < SENDER_TAG_SIZE + 1 {
return Err(InvalidReplyRequestError::RequestTooShortToDeserialize); return Err(InvalidReplyRequestError::RequestTooShortToDeserialize);
} }
+4
View File
@@ -9,6 +9,7 @@ repository = { workspace = true }
[dependencies] [dependencies]
bytes = { workspace = true } bytes = { workspace = true }
cfg-if = { workspace = true }
tokio-util = { workspace = true, features = ["codec"] } tokio-util = { workspace = true, features = ["codec"] }
thiserror = { workspace = true } thiserror = { workspace = true }
log = { workspace = true } log = { workspace = true }
@@ -20,5 +21,8 @@ nym-metrics = { path = "../../nym-metrics" }
nym-sphinx-addressing = { path = "../addressing" } nym-sphinx-addressing = { path = "../addressing" }
nym-sphinx-acknowledgements = { path = "../acknowledgements" } nym-sphinx-acknowledgements = { path = "../acknowledgements" }
[features]
no-acks = []
[dev-dependencies] [dev-dependencies]
tokio = { workspace = true, features = ["full"] } tokio = { workspace = true, features = ["full"] }
+10 -4
View File
@@ -230,8 +230,12 @@ fn split_into_ack_and_message(
| PacketSize::ExtendedPacket32 | PacketSize::ExtendedPacket32
| PacketSize::OutfoxRegularPacket => { | PacketSize::OutfoxRegularPacket => {
trace!("received a normal packet!"); trace!("received a normal packet!");
let (ack_data, message) = split_hop_data_into_ack_and_message(data, packet_type)?; cfg_if::cfg_if! {
let (ack_first_hop, ack_packet) = if #[cfg(feature = "no-acks")] {
return Ok((None, data));
} else {
let (ack_data, message) = split_hop_data_into_ack_and_message(data, packet_type)?;
let (ack_first_hop, ack_packet) =
match SurbAck::try_recover_first_hop_packet(&ack_data, packet_type) { match SurbAck::try_recover_first_hop_packet(&ack_data, packet_type) {
Ok((first_hop, packet)) => (first_hop, packet), Ok((first_hop, packet)) => (first_hop, packet),
Err(err) => { Err(err) => {
@@ -239,8 +243,10 @@ fn split_into_ack_and_message(
return Err(err.into()); return Err(err.into());
} }
}; };
let forward_ack = MixPacket::new(ack_first_hop, ack_packet, packet_type); let forward_ack = MixPacket::new(ack_first_hop, ack_packet, packet_type);
Ok((Some(forward_ack), message)) Ok((Some(forward_ack), message))
}
}
} }
} }
} }