Compare commits

...

7 Commits

Author SHA1 Message Date
Drazen 1fe4f3621e Add 1 Delay(0) 2023-11-14 16:15:42 +00:00
Drazen b5739c7308 Disable delays 2023-11-14 15:56:55 +00:00
Drazen 60518f2f2b Cache sphinx header on gateway 2023-11-14 15:31:19 +00:00
Drazen aaec36addc use from SURB 2023-11-14 13:18:03 +00:00
Drazen 592d6de8e6 Reuse SURB for Sphinx 2023-11-14 13:12:19 +00:00
Drazen cf4746dbf1 Try 0 hops 2023-11-14 11:18:39 +00:00
Drazen 75837d73c2 Hardcode 1 hop 2023-11-14 11:13:42 +00:00
7 changed files with 486 additions and 405 deletions
Generated
+392 -393
View File
File diff suppressed because it is too large Load Diff
+13 -3
View File
@@ -30,7 +30,9 @@ nym-topology = { path = "../topology" }
[dev-dependencies]
nym-mixnet-contract-common = { path = "../cosmwasm-smart-contracts/mixnet-contract" }
nym-crypto = { path = "../crypto", version = "0.4.0", features = ["asymmetric"] }
nym-crypto = { path = "../crypto", version = "0.4.0", features = [
"asymmetric",
] }
# do not include this when compiling into wasm as it somehow when combined together with reqwest, it will require
# net2 via tokio-util -> tokio -> mio -> net2
@@ -43,5 +45,13 @@ features = ["sync"]
[features]
default = ["sphinx"]
sphinx = ["nym-crypto/sphinx", "nym-sphinx-params/sphinx", "nym-sphinx-types/sphinx"]
outfox = ["nym-crypto/outfox", "nym-sphinx-params/outfox", "nym-sphinx-types/outfox"]
sphinx = [
"nym-crypto/sphinx",
"nym-sphinx-params/sphinx",
"nym-sphinx-types/sphinx",
]
outfox = [
"nym-crypto/outfox",
"nym-sphinx-params/outfox",
"nym-sphinx-types/outfox",
]
+1 -1
View File
@@ -17,7 +17,7 @@ pub mod packet_version;
// If somebody can provide an argument why it might be reasonable to have more than 255 mix hops,
// I will change this to [`usize`]
pub const DEFAULT_NUM_MIX_HOPS: u8 = 3;
pub const DEFAULT_NUM_MIX_HOPS: u8 = 0;
// TODO: not entirely sure how to feel about those being defined here, ideally it'd be where [`Fragment`]
// is defined, but that'd introduce circular dependencies as the acknowledgements crate also needs
+3 -1
View File
@@ -8,9 +8,11 @@ license = { workspace = true }
repository = { workspace = true }
[dependencies]
sphinx-packet = { version = "0.1.0", optional = true }
# sphinx-packet = { version = "0.1.0", optional = true }
sphinx-packet = { git = "https://github.com/nymtech/sphinx.git", branch="feature/clone-surb", optional = true }
nym-outfox = { path = "../../../nym-outfox", optional = true }
thiserror = { workspace = true }
once_cell = "1.18"
[features]
default = ["sphinx"]
+72 -6
View File
@@ -9,6 +9,8 @@ pub use nym_outfox::{
// re-exporting types and constants available in sphinx
#[cfg(feature = "outfox")]
use nym_outfox::packet::{OutfoxPacket, OutfoxProcessedPacket};
use once_cell::sync::OnceCell;
#[cfg(feature = "sphinx")]
pub use sphinx_packet::{
constants::{
@@ -28,6 +30,9 @@ use sphinx_packet::{SphinxPacket, SphinxPacketBuilder};
use std::{array::TryFromSliceError, fmt};
use thiserror::Error;
static REUSABLE_SURB: OnceCell<SURB> = OnceCell::new();
static REUSABLE_HEADER: OnceCell<ProcessedHeader> = OnceCell::new();
#[derive(Error, Debug)]
pub enum NymPacketError {
#[error("Sphinx error: {0}")]
@@ -77,6 +82,30 @@ impl fmt::Debug for NymPacket {
}
impl NymPacket {
#[cfg(feature = "sphinx")]
pub fn from_surb<M: AsRef<[u8]>>(
size: usize,
message: M,
route: &[Node],
destination: &Destination,
delays: &[Delay],
) -> Result<NymPacket, NymPacketError> {
let (packet, _address) = if let Some(surb) = REUSABLE_SURB.get() {
let new_surb = surb.clone();
new_surb.use_surb(message.as_ref(), size)?
} else {
let surb_material =
SURBMaterial::new(route.to_vec(), vec![Delay::new_from_millis(0)], destination.to_owned());
let surb = SURB::new(EphemeralSecret::new(), surb_material)?;
REUSABLE_SURB
.set(surb.clone())
.expect("ReusableSURB was already set!");
surb.use_surb(message.as_ref(), size)?
};
Ok(NymPacket::Sphinx(packet))
}
#[cfg(feature = "sphinx")]
pub fn sphinx_build<M: AsRef<[u8]>>(
size: usize,
@@ -85,11 +114,12 @@ impl NymPacket {
destination: &Destination,
delays: &[Delay],
) -> Result<NymPacket, NymPacketError> {
Ok(NymPacket::Sphinx(
SphinxPacketBuilder::new()
.with_payload_size(size)
.build_packet(message, route, destination, delays)?,
))
NymPacket::from_surb(size, message, route, destination, delays)
// Ok(NymPacket::Sphinx(
// SphinxPacketBuilder::new()
// .with_payload_size(size)
// .build_packet(message, route, destination, delays)?,
// ))
}
#[cfg(feature = "sphinx")]
pub fn sphinx_from_bytes(bytes: &[u8]) -> Result<NymPacket, NymPacketError> {
@@ -149,7 +179,43 @@ impl NymPacket {
) -> Result<NymProcessedPacket, NymPacketError> {
match self {
NymPacket::Sphinx(packet) => {
Ok(NymProcessedPacket::Sphinx(packet.process(node_secret_key)?))
let unwrapped_header: ProcessedHeader = if let Some(header) = REUSABLE_HEADER.get() {
header.clone()
} else {
let header = packet.header.process(node_secret_key)?;
let _ = REUSABLE_HEADER.set(header.clone());
header
};
let processed_packet = match unwrapped_header {
ProcessedHeader::ForwardHop(
new_header,
next_hop_address,
delay,
payload_key,
) => {
let new_payload = packet.payload.unwrap(&payload_key)?;
let new_packet = SphinxPacket {
header: *new_header,
payload: new_payload,
};
ProcessedPacket::ForwardHop(
Box::new(new_packet),
next_hop_address,
delay,
)
}
ProcessedHeader::FinalHop(destination, identifier, payload_key) => {
let new_payload = packet.payload.unwrap(&payload_key)?;
ProcessedPacket::FinalHop(
destination,
identifier,
new_payload,
)
}
};
Ok(NymProcessedPacket::Sphinx(processed_packet))
}
#[cfg(feature = "outfox")]
NymPacket::Outfox(mut packet) => {
@@ -34,6 +34,9 @@ impl PacketProcessor {
&self,
received: FramedNymPacket,
) -> Result<ProcessedFinalHop, GatewayProcessingError> {
match self.inner_processor.process_received(received)? {
MixProcessingResult::ForwardHop(..) => {
Err(GatewayProcessingError::ForwardHopReceivedError)
+2 -1
View File
@@ -18,7 +18,8 @@ curve25519-dalek = "3.2"
chacha20poly1305 = "0.10.1"
getrandom = { workspace = true, features = ["js"] }
thiserror = { workspace = true }
sphinx-packet = "0.1.0"
# sphinx-packet = "0.1.0"
sphinx-packet = { git = "https://github.com/nymtech/sphinx.git", branch="feature/clone-surb" }
rand = "0.7.3"
log = "0.4"