Repository navigation
Description
Problem
With OHTTP opt-in from arbitrary relays, the sender and receiver's choice of relays and directory is independent.
E2EE messages flow along this path:
graph LR
sender --- ohttp_relay_1;
ohttp_relay_1 --- directory;
directory --- ohttp_relay_2;
ohttp_relay_2 --- receiver;
unfortunately if both the directory and relay(s) are in the same autonomous system, this can weaken OHTTP privacy, which is already pretty lenient. for example suppose two independent service operators both choose the same cloud provider:
graph LR
sender --- ohttp_relay_1;
subgraph AS
ohttp_relay_1 --- directory;
end
directory --- ohttp_relay_2;
ohttp_relay_2 --- receiver;
this can be used to link users to specific (encrypted) messages on the directory.
this is also the case if a user and the directory share an AS.
graph LR
sender --- ohttp_relay_1;
ohttp_relay_1 --- directory;
directory --- ohttp_relay_2;
ohttp_relay_2 --- receiver;
subgraph AS
sender;
directory;
end
a similar but more concern arises if both relays are in the same AS, then traffic analysis can link the sender and receiver, even if the directory and users are different ones:
graph LR
sender --- ohttp_relay_1;
ohttp_relay_1 --- directory;
directory --- ohttp_relay_2;
ohttp_relay_2 --- receiver;
subgraph AS
ohttp_relay_1;
ohttp_relay_2;
end
unfortunately in the (presumably not uncommon) case of two mobile users in the same physical location sharing the same mobile provider, this is still inherently linkable through traffic analysis. VPNs can mitigate this especially if there is cover traffic, and it helps that BIP 77 is fairly low bandwidth, but this traffic analysis concern is inherent.
graph LR
sender --- ohttp_relay_1;
ohttp_relay_1 --- directory;
directory --- ohttp_relay_2;
ohttp_relay_2 --- receiver;
subgraph AS
sender;
receiver;
end
Proposed Mitigation
Therefore it seems appropriate to consider providing code that can help wallet developers use https://asmap.org/ to select servers.
Given a list of trusted directories and relays, these lists should be filtered to exclude servers that share an AS with the user. This is potentially tricky as it users to be able to determine their public IP(s).
All else being equal, a receiver should select a directory at random from the trusted filtered directories. Both sender and receiver will subsequently filter their list of trusted relays to additionally exclude the AS of this randomly chosen directory.
The receiver's ephemeral key is then generated. This key can be hashed with a tagged hash for domain separation, to generate a random seed shared by both sender and receiver. Let h be a hash function tagged by this random seed.
Both sender and receiver should sort their individual filtered list of relays according to the following rules:
- group relays by AS
- shuffle the AS buckets by
h(ASN)- open question: for relays resolving to multiple IPs with different ASNs, what should be done? the simplest and most conservative thing is to just reject these entirely and discourage such deployments, only allowing one each of A and AAAA entries and strongly suggest favoring ipv6 in the transport layer?
- shuffle the relays in each bucket by
h(relay uri) - cycle through the buckets popping relays one by one to obtain the receiver's total ordering which is well defined (up to hash collisions) over the union of both user's sets of trusted relays
- sender only: reverse the ordering
Both parties should then select relays according to this ordering.
Note that payjoin-cli's RelayManager currently uses the thread RNG to effectively shuffle the list of relays, apart from excluding the relay's AS, this idea just changes this proposed ordering to be deterministic so that both users can more reliably avoid picking a relay from the same AS.
If both parties do this, the worst case scenario is still that they both share an AS and both pick a relay in the same AS:
graph LR
sender --- ohttp_relay_1;
ohttp_relay_1 --- directory;
directory --- ohttp_relay_2;
ohttp_relay_2 --- receiver;
subgraph AS2
ohttp_relay_1;
ohttp_relay_2;
end
subgraph AS1
sender;
receiver;
end
However if they do not share an AS and they have sufficiently large lists of relay, regardless of the degree that those lists overlap they can maximize their chances of selecting 3 distinct AS's for the route between them.
This should be an optional feature, and could be an entirely separate crate, or my preference would be as an optional feature in the spirit of the io feature, but a proof of concept can start as just a payjoin-cli feature that.
Activity
nothingmuch commentedon Aug 3, 2025
more of a math nerd question, doesn't seem that important, but is bucketizing like that actually optimal or close to optimal for avoiding the same AS? seems a bit tricky to analyze but maybe there's a simple combinatorial way of arguing for collision minimzation?
nothingmuch commentedon May 9, 2026
this comment is to describe the tradeoffs of randomizing per request vs. per session, and randomizing statelessly or statefully.
cc @Mshehu5 @bc1cindy
traffic patterns
the overall traffic pattern is that the receiver polls in a loop until the sender posts, after which the sender polls until the receiver posts.
if the mailbox has contents before the other party begins polling then there will be just one GET request and it will be responded to immediately which makes that GET indistinguishable from a POST request as far as timing fingerprints.
however, if the polling requests precede the post, as would be expected, then they can be distinguished from the posting requests due to their longer duration. the receiver's polling phase is likely to be longer since the sender's polling phase may consist of just one request if the receiver is quick to respond (the same conditions that BIP 78 requires), in particular if it's automated.
shared relay for handover request (worst case scenario)
when the polling phase precedes the post, the main leak with two clients using the same relay is that this changes the threat model for traffic analysis so that even a local passive adversary can link the sender and the receiver by observing that the post of one client interrupts the polling request of the other.
the directory could mitigate this to a very limited extent by adding some random delay to decouple the post request from the terminating GET request, but this will degrade responsiveness.
the asymetric traffic patterns will allow the adversary to learn which party is the sender and which party is the receiver, not just link them to each other, because the receiver and the sender's polling phases are disjoint and the receiver's polling phase comes first and is linked to a specific sender.
different relay per client
suppose sender and receiver did not use the same relay as each other, but still each of them only used a single relay. a local passive adversary at the relay will not be able to link them anymore, but sender and receiver have different traffic patterns: the receiver polls until it reads, then posts a response. the sender posts and then polls, so each role can be identified even if seeing the traffic of only one side.
different relay per client and per request type
suppose the sender and receiver used two relays each, one for polling requests and a separate one for posting requests. then the traffic of the polling requests will have the same pattern for either role. the relay will still be able to determine the duration of the session.
different relay per request
if clients shuffle the list of relays consistently, and cycles through the list in a loop then if there are more requests per session than there are relays then any one relay will lose accuracy estimating session duration, but learn more information about the number of relays that the client is using due to the period of the requests relative to the duration of polling responses. this is public information in principle, but it may be a client fingerprint if different wallets are shipped with different default lists of relays even if these lists are user configurable.
by re-shuffling the list every time it is cycled through in a consistent way, noise to the period measurement. instead of sampling without replacement and then repeating the sampled sequence in a loop, it's sampling without replacement to exhaustion and then sampling without replacement (almost certainly) differently the next time. while this adds noise, it does not change the mean, so long lived sessions will have exactly the same leak as those of a client that shuffles only once at session initialization. to re-shuffle the same shared secret would be used but a counter can be added to the derivation.
random relay per short polling interval
the parameters of the periodicity can be adjusted by choosing a relay and then using it for a number of polling requests in sequence, for example with 30 second timeouts, 20 requests would be about 10 minutes of polling activity.
this would still be periodic, but reduces the temporal resolution for the observer, and for short sessions may prevent not only cycling through all of the relays, but potentially make it so that fewer than half are chosen, making the relay shuffling approach truly disjoint.
segregating relays by request type using the shared secret
the goal of the design above, where the same shuffled list is indexed in opposing directions is that for short lived sessions, for the relay receiver uses to make the critical request to be the same as the sender uses for their first request, that would have to be the receiver's nth request.
this can be made more robust by picking different relays for polling and posting consistently among the sender and receiver, so that a post request will never be sent through the same relay as a polling request. this can be done by classifying each relay as whether it's reserved for posting in which case it would be avoided.
in this way the same goal of avoiding the worst case scenario can be achieved more reliably, but at the cost of reducing the set of relays that can be used. if most relays are reliable most of the time, this shouldn't be a problem since each client only needs to make a single POST request.
independently random relay per request or per short polling interval
sampling relays uniformly with replacement will add yet more noise than shuffling, but still have the same mean over long sessions.
the main advantage is that this can be done entirely statelessly (using a clock instead of a counter): during session initialization, go through all the relays, randomly (but consistently) classify them into the POST relays and the GET relays. during the polling phase, a random GET relay can be selected for each request with the same disjointness advantage as described in the mechanism.
here the same goal of avoiding the worst case scenario can be achieved without requiring stateful storage of an index or (although keeping state for unreliable relays is probably a good idea for reliability reasons),
this also works if the same relay is used for several polling requests in succession during some short interval.
generalizing from relays to ASes
for simplicity above i referred to the same relay but we actually care about ASes.
sampling an AS uniformly, then a relay from that AS uniformly will weight relays unevenly, but given but given the asmap is public information the list of relays being used could still be statistically inferred based on the relationship between the request rate for a client so this non-uniformity only helps with reducing the chance of two clients picking the same AS just because it has many relays in it (which is why it's important to still do it).
randomizing polling traffic patterns
with some probability, the directory could terminate polling requests earlier than the maximum timeout even when there is no data.
the directory could be done in response to any unrelated POST or immediately serviced GET request: whenever a POST request is given, pick several random poll requests that are ongoing, and terminate those early regardless of whether one of them is polling the mailbox that was written to.
if after such a dummy response the client makes another polling request through the same relay, the privacy benefits are lost so this will only degrade UX and waste bandwidth. so the client should in this case force switching the relay when relays are reused during a short polling interval, as that would have the same traffic pattern as a completed polling phase at least until the next short polling interval for the same relay (but that could still look like multiple sessions.
clients can also add some random delays in between their polling requests, sacrificing some responsiveness.
looped cover traffic
the client can also request this from the directory, but that would adds more variations to the request, and some complexity on the directory. this could still be forced by sending invalid encapsulated requests, but that seems counter productive.
instead, the same result as that can be achieved with no new
relaymailroom functionality by having a client post messages to itself: if the receiver is waiting for the sender, as it is about to switch to a new relay if it using the short polling strategy, it can switch the mailbox being used for the last request in an interval, and post a dummy message to itself via another relay.at the cost of some multiple of minimum bandwidth, clients could keep long running polling loops for dummy mailboxes, so that the directory can't distinguish dummy mailboxes from real ones even statistically. the same switchover logic should be used (dummy GET request is the last request for a sequence of polling requests made via the same relay)
nothingmuch commentedon May 9, 2026
to mitigate the downside of restricting relays to certain roles, that can be done in a time dependent way:
let
tbe number of seconds since unix epoch plus a random number derived from the shared secret divided by 30 (GET request duration).compute
H(shared secret, message, t, AS)to derive a consistent time dependent labeling of relays. message is either 0 or 1, indicating whether it's the sender's message that is being posted/polled or the receivers, to avoid having the same pattern for the two mailboxes in the session.at time$t_i$ , only send post requests via the $k$ relays whose hash is lexicographically smallest. $k$ should be 2 or 3, a reasonable limit on the number of retries in a 30 second window.
at time$t_i$ , avoid making any real GET requests (i.e. dummy GET requests are OK) using the relays whose hash is lexicographically smallest not just at time $t_i$ , but at times $t_{i-2} .. t_{i+2}$ or at least $t_{i-1} .. t_{i+1}$ . since up to $3k$ autonomous systems will be avoided for GET requests the total number of relays needs to be significantly larger.
so long as there are sufficiently many relays to choose from for polling, that will appear uniformly random since the$3k$ unavailable ones are also randomly selected and the bias against them is short lived.
so long as the sender and receiver's clocks are within of around a minute of each other, which is pretty likely these days, they should always use a different relay for the post and the get, avoiding the worst case leak
Mshehu5 commentedon May 9, 2026
@nothingmuch Thank you for the explanation on the trade offs.
I just wanted to highlight some concerns that were raised by Cindy and Armin. Some integrations/downstream implementations having different relay selection logic #1385. Even if we implement this in payjoin-cli, it was mentioned that downstream integrations may still need to reimplement the same logic individually which could create additional maintenance and consistency issues.
Because of that maybe it makes more sense to go with your first suggestion:
by making this its own separate crate. That way the logic can remain consistent across all integrations while also reducing the implementation burden on downstream wallets.
We could then test the crate across different integrations and once it's successful we can consider merging it into the main codebase.
Though this approach might require a bit more work to get right initially what do you think would be a viable next step?
bc1cindy commentedon May 9, 2026
thank you for sharing this
nothingmuch commentedon May 9, 2026
the main insight i had that is new is instead of the sender and receiver trying to pick different relays, instead of separating based on the client role (sender vs. receiver) you can use the strategy of using a different relay for each kind of request (poll/post) and still achieve disjointness without relying on state, and achieve more or less the same effect since the two protocol roles don't execute the same kinds of requests at the same time.
the main complexity is in regards to reusing a single relay for a few successive poll requests in a short time window, which is seems better than a completely random relay for every requests, but requires some careful design (e.g. pick a relay at time$k < j-1$ times. this requires state, but not persistent state.
t_i, check at what timet_{i+j}that relay may be used by the counterparty to do a post request, and use that relay to pollnote that where i wrote "shared secret" i should have said receiver key since the shared secret is only learned by the receiver after the first message, that is also what's implemented in your PoC. sorry for the confusion.
so i think my main suggestion is, see if this stateless behavior simplifies things enough, it would still need access to the system clock if done according to my last comment but that is easier to provide than a persistence mechanism for a counter or something like that in a generic way, so it would help integrating.
the simpler the asmap aware behavior is, and the less state/io it does the more it makes sense to ship (as an optional feature or even enabled by default) in the payjoin crate itself, but if this is still complicated then having a reusable standalone crate that is straightforward to use in payjoin-cli is the next best thing
benalleng commentedon Jun 2, 2026
Here #919 (comment) you talk about the relays being restricted to certain rolls and choosing their current role based on some time based approach but the directory itself is now a relay "role" based on the current payjoin-mailroom design as a relay is also a directory. Should there be some similar time based way to choose a directory or should this remain hard coded?