Skip to content
This repository was archived by the owner on Jan 30, 2026. It is now read-only.

Commit 2f08f36

Browse files
committed
refactor: each surb has a different I
1 parent e45cd05 commit 2f08f36

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

mix/mix_protocol.nim

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ when defined(enable_mix_benchmarks):
1515

1616
const MixProtocolID* = "/mix/1.0.0"
1717

18-
type ConnCreds = object
19-
incoming: AsyncQueue[seq[byte]]
20-
surbSKSeq: seq[(secret, key)]
18+
type
19+
IGroup = ref object
20+
members: HashSet[I]
21+
22+
ConnCreds = object
23+
group: IGroup
24+
incoming: AsyncQueue[seq[byte]]
25+
surbSecret: secret
26+
surbKey: key
2127

2228
## Mix Protocol defines a decentralized anonymous message routing layer for libp2p networks.
2329
## It enables sender anonymity by routing each message through a decentralized mix overlay
@@ -231,45 +237,43 @@ proc handleMixNodeConnection(
231237
return
232238

233239
let connCred = mixProto.connCreds[processedSP.id]
234-
mixProto.connCreds.del(processedSP.id)
235-
236-
var couldProcessReply = false
237-
var reply: seq[byte]
238-
for sk in connCred.surbSKSeq:
239-
let processReplyRes = processReply(sk[1], sk[0], processedSP.delta_prime)
240-
if processReplyRes.isOk:
241-
couldProcessReply = true
242-
reply = processReplyRes.value()
243-
break
244-
245-
if couldProcessReply:
246-
let msgChunk = MessageChunk.deserialize(reply).valueOr:
247-
error "Deserialization failed", err = error
248-
mix_messages_error.inc(labelValues = ["Reply", "INVALID_SPHINX"])
249-
return
250240

251-
let unpaddedMsg = unpadMessage(msgChunk).valueOr:
252-
error "Unpadding message failed", err = error
253-
mix_messages_error.inc(labelValues = ["Reply", "INVALID_SPHINX"])
254-
return
241+
# Deleting all other SURBs associated to this
242+
for id in connCred.group.members:
243+
mixProto.connCreds.del(id)
255244

256-
let deserialized = MixMessage.deserialize(unpaddedMsg).valueOr:
257-
error "Deserialization failed", err = error
258-
mix_messages_error.inc(labelValues = ["Reply", "INVALID_SPHINX"])
259-
return
245+
let reply = processReply(
246+
connCred.surbKey, connCred.surbSecret, processedSP.delta_prime
247+
).valueOr:
248+
error "could not process reply", id = processedSP.id
249+
mix_messages_error.inc(labelValues = ["Reply", "INVALID_CREDS"])
250+
return
260251

261-
when defined(enable_mix_benchmarks):
262-
benchmarkLog "Reply",
263-
mixProto.switch.peerInfo.peerId,
264-
startTime,
265-
msgId,
266-
orig,
267-
Opt.some(fromPeerId),
268-
Opt.none(PeerId)
252+
let msgChunk = MessageChunk.deserialize(reply).valueOr:
253+
error "Deserialization failed", err = error
254+
mix_messages_error.inc(labelValues = ["Reply", "INVALID_SPHINX"])
255+
return
269256

270-
await connCred.incoming.put(deserialized.message)
271-
else:
272-
error "could not process reply", id = processedSP.id
257+
let unpaddedMsg = unpadMessage(msgChunk).valueOr:
258+
error "Unpadding message failed", err = error
259+
mix_messages_error.inc(labelValues = ["Reply", "INVALID_SPHINX"])
260+
return
261+
262+
let deserialized = MixMessage.deserialize(unpaddedMsg).valueOr:
263+
error "Deserialization failed", err = error
264+
mix_messages_error.inc(labelValues = ["Reply", "INVALID_SPHINX"])
265+
return
266+
267+
when defined(enable_mix_benchmarks):
268+
benchmarkLog "Reply",
269+
mixProto.switch.peerInfo.peerId,
270+
startTime,
271+
msgId,
272+
orig,
273+
Opt.some(fromPeerId),
274+
Opt.none(PeerId)
275+
276+
await connCred.incoming.put(deserialized.message)
273277
except KeyError:
274278
doAssert false, "checked with hasKey"
275279
of Intermediate:
@@ -362,17 +366,18 @@ proc buildSurbs(
362366
exitPeerId: PeerId,
363367
): Result[seq[SURB], string] =
364368
var response: seq[SURB]
365-
var surbSK: seq[(secret, key)] = @[]
366-
var id: I
367-
hmacDrbgGenerate(mixProto.rng[], id)
368-
369+
var group = IGroup(members: initHashSet[I]())
370+
369371
for _ in uint8(0) ..< numSurbs:
370372
var
373+
id: I
371374
multiAddrs: seq[string] = @[]
372375
publicKeys: seq[FieldElement] = @[]
373376
hops: seq[Hop] = @[]
374377
delay: seq[seq[byte]] = @[]
375378

379+
hmacDrbgGenerate(mixProto.rng[], id)
380+
376381
# Select L mix nodes at random
377382
let numMixNodes = mixProto.pubNodeInfo.len
378383

@@ -427,13 +432,10 @@ proc buildSurbs(
427432
let surb = createSURB(publicKeys, delay, hops, id).valueOr:
428433
return err(error)
429434

430-
surbSK.add((surb.secret.get(), surb.key))
431-
435+
group.members.incl(id)
436+
mixProto.connCreds[id] = ConnCreds(group: group, surbSecret: surb.secret.get(), surbKey: surb.key, incoming: incoming)
432437
response.add(surb)
433438

434-
if surbSK.len != 0:
435-
mixProto.connCreds[id] = ConnCreds(surbSKSeq: surbSK, incoming: incoming)
436-
437439
return ok(response)
438440

439441
proc prepareMsgWithSurbs(

0 commit comments

Comments
 (0)