From daf8365a6e65a269f8308865f442b9f66649a29e Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Tue, 6 Oct 2026 15:22:52 -0500 Subject: [PATCH] fix(frost): a resend names the whole room, because the p-tags are its identity The resend narrowed its NIP-17 p-tags to the members it was for, on the reasoning that a wrap is sealed per recipient so repeating one for the whole room wastes wraps. Both halves of that were wrong. A receiver does not read the p-tags as a recipient list. It hashes them together with the sender to work out which room the payload belongs to -- `GiftWrapPayload.aggregatedParticipantsPublicKey`, via `ChatRoom.deriveChatRoomId`, which is what `GiftWrapSeal.decryptGiftWrapPayload` files an inbound payload under. Leave a member out and the id changes, so the resend is filed under a room that does not exist; `getOrCreateChatRoom` then mints that room rather than rejecting it. A repaired session would have become a phantom two-person conversation in the reader's list, and the session would have stayed stalled. There was no saving to trade against it either: `sealGiftWrapPayload` wraps for every participant of the room it looks up, not for the p-tags it was handed, so a narrowed payload cost exactly as many wraps as a full one. Who a resend is for travels in `frost_silent`, which is not part of that hash, so nothing is lost by naming everybody. Marmot was never affected -- a group event names nobody and is routed by its h-tag. The two tests that asserted the narrowing now assert the opposite, which is the property worth having: the p-tag set does not change between a publish and its repeat. The p-tags were the right thing to assert all along; they just mean something other than what they look like. Found while answering whether this is backwards compatible with older receivers. It is -- no new kind, an unknown tag that old builds ignore, and every duplicate-folding gate in `record` and `acceptProposal` predates this work. This bug was never a compatibility bug; it broke old and new receivers identically. 1279 tests, no failures. m3Audit: all budgets met. Co-Authored-By: Claude Opus 5 --- .../compose/managers/FrostSigningManager.kt | 48 +++++++++++-------- .../managers/FrostSigningResendJvmTest.kt | 31 +++++++----- docs/frost-session-resend.md | 46 +++++++++++++----- 3 files changed, 81 insertions(+), 44 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/managers/FrostSigningManager.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/managers/FrostSigningManager.kt index 26b82557..6e4c23f5 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/managers/FrostSigningManager.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/managers/FrostSigningManager.kt @@ -1994,8 +1994,7 @@ object FrostSigningManager { kind = FrostSigningEvents.PROPOSAL, content = FrostSigningEvents.encodeProposal(unsigned), includeKey = true, - silentMembers = silent.toList(), - recipients = silent + silentMembers = silent.toList() ) when (session.stage) { @@ -2009,8 +2008,7 @@ object FrostSigningManager { session = session, kind = FrostSigningEvents.SIGNER_SET, content = joinPayload(aggregated.map { it!! }), - signerIds = chosen, - recipients = silent + signerIds = chosen ) } } @@ -2023,8 +2021,7 @@ object FrostSigningManager { localChatRoom = localChatRoom, session = session, kind = FrostSigningEvents.SIGNATURE, - content = joinPayload(signatures.map { it!! }), - recipients = silent + content = joinPayload(signatures.map { it!! }) ) } } @@ -2109,14 +2106,18 @@ object FrostSigningManager { "republishing ${own.size} message(s) it already sent" ) + // To the room, not to the coordinator alone. Only the coordinator reads a + // nonce, so addressing one would be the honest shape -- but on NIP-17 the + // p-tag set is the room's identity rather than a recipient list, and + // naming fewer members files the message under a different room. See + // [recipientTags]. own.forEach { message -> broadcast( database = database, localChatRoom = localChatRoom, session = session, kind = message.kind, - content = message.payload, - recipients = setOf(session.coordinatorPublicKey) + content = message.payload ) } } @@ -2375,8 +2376,7 @@ object FrostSigningManager { content: String, includeKey: Boolean = false, signerIds: List? = null, - silentMembers: List? = null, - recipients: Set? = null + silentMembers: List? = null ): Boolean { val sessionTags = FrostSigningEvents.assembleTags( sessionId = session.id, @@ -2393,7 +2393,7 @@ object FrostSigningManager { val tags = if (isMarmotRoom) { sessionTags } else { - recipientTags(localChatRoom, session.userPublicKey, recipients) + sessionTags + recipientTags(localChatRoom, session.userPublicKey) + sessionTags } // The rumor id the outbound pipeline will recompute from these same @@ -2491,21 +2491,29 @@ object FrostSigningManager { * records it locally. Deduplicated by member, since `localParticipants` is a * row list and a room can carry the same person twice. * - * [only] narrows it, for a resend: a wrap is sealed per recipient, so - * repeating a message for the two members who missed it costs two wraps - * rather than the room's worth. Null means everybody, which is what every - * first-time publish wants -- and a narrowing that matched nobody is read as - * null rather than as a message addressed to no one, since an untagged - * NIP-17 payload reaches nobody at all. + * **Every member, always.** This list is not a recipient list that a caller + * may narrow -- on this transport it is the room's *identity*. A receiver + * computes the room a payload belongs to as + * `ChatRoom.deriveChatRoomId({sender} + p-tags)`; see + * `GiftWrapPayload.aggregatedParticipantsPublicKey`, which is what + * `GiftWrapSeal.decryptGiftWrapPayload` files an inbound payload under. Leave + * a member out and the id changes, so the message is filed under a room that + * is not the one it was sent in -- and `getOrCreateChatRoom` will mint that + * room rather than reject it, which turns a lost message into a phantom + * conversation in the reader's list. + * + * A resend was briefly narrowed to the members it was for, on the reasoning + * that a wrap is sealed per recipient and repeating one for the whole room + * wastes wraps. Both halves of that were wrong: the saving does not exist + * either, because `sealGiftWrapPayload` seals for every participant of the + * room it looks up rather than for the p-tags it was handed. */ private fun recipientTags( localChatRoom: LocalChatRoom, - senderPublicKey: HexKey, - only: Set? = null + senderPublicKey: HexKey ): Array> = localChatRoom.localParticipants .distinctBy { it.participant.participantPublicKey } .filter { it.participant.participantPublicKey != senderPublicKey } - .filter { only.isNullOrEmpty() || it.participant.participantPublicKey in only } .map { localParticipant -> PTag.assemble( localParticipant.participant.participantPublicKey, diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningResendJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningResendJvmTest.kt index ffc1df59..664d64ff 100644 --- a/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningResendJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningResendJvmTest.kt @@ -350,7 +350,7 @@ class FrostSigningResendJvmTest { } @Test - fun `a resend is narrowed to the members it is for`() = runBlocking { + fun `a resend names the whole room, because the p-tags are the room's identity`() = runBlocking { openDevice() val session = propose() @@ -365,11 +365,17 @@ class FrostSigningResendJvmTest { val resent = queuedBy(FrostSigningEvents.PROPOSAL) { assertTrue(resend(session.id)) } - // A NIP-17 payload is sealed once per p-tag, so repeating a message for - // the one member who missed it should cost one wrap and not the room's - // worth. The first proposal named everybody; this one names Carol. - assertEquals(setOf(carol), pTags(resent.tags)) - assertEquals(setOf(bob, carol), pTags(first.tags)) + // Tempting to send this to Carol alone, and wrong. A receiver works out + // which room a NIP-17 payload belongs to by hashing the sender together + // with the p-tags -- `GiftWrapPayload.aggregatedParticipantsPublicKey`. + // Name fewer members and the id changes, so the resend is filed under a + // room that does not exist, and `getOrCreateChatRoom` mints it rather + // than rejecting it: a phantom conversation instead of a repaired + // session. Who the resend is *for* travels in the silent tag, which is + // not part of that hash. + assertEquals(pTags(first.tags), pTags(resent.tags)) + assertEquals(setOf(bob, carol), pTags(resent.tags)) + assertEquals(setOf(carol), FrostSigningEvents.parseSilentMembers(resent.tags)?.toSet()) } @Test @@ -526,20 +532,23 @@ class FrostSigningResendJvmTest { } @Test - fun `a republished nonce goes only to the coordinator`() = runBlocking { + fun `a republished nonce names the whole room`() = runBlocking { openDevice(bob) val session = proposalArrivedAt(bob) FrostSigningManager.approve(database = db, localChatRoom = room, sessionId = session.id) + val first = queued(FrostSigningEvents.NONCE).single() val resent = queuedBy(FrostSigningEvents.NONCE) { receive(session.id, FrostSigningEvents.PROPOSAL, alice, proposalContent(), silent = listOf(bob)) } - // A nonce is read by nobody but the coordinator -- `orderedNonces` is - // called under `isCoordinator` -- so repeating it for the room would be - // two wraps spent telling people who do not look. - assertEquals(setOf(alice), pTags(resent.tags)) + // Only the coordinator reads a nonce, so addressing one would be the + // honest shape -- and it would file the message under a two-person room + // that is not the one the session is running in. The p-tag set is the + // room's identity on this transport, not a recipient list. + assertEquals(pTags(first.tags), pTags(resent.tags)) + assertEquals(setOf(alice, carol), pTags(resent.tags)) } @Test diff --git a/docs/frost-session-resend.md b/docs/frost-session-resend.md index 9fbd9ea6..bf578b2b 100644 --- a/docs/frost-session-resend.md +++ b/docs/frost-session-resend.md @@ -257,10 +257,21 @@ group event reaches everybody, and every receiver folds the duplicate away, whic is cheaper than one message per silent member and tells the room nothing it does not already know. -NIP-17 is the opposite — `recipientTags` seals one wrap per recipient — so there the -resend narrows to the silent set plus the chosen signers, which is a real saving in -a room where one member out of nine is quiet. This is the first signature a group -makes, on gift wraps, per `GroupKeyStateManager.propose`. +NIP-17 names everybody too, and the reason is worth getting right because the +first version of this got it wrong. **On that transport the p-tag set is the +room's identity, not a recipient list.** A receiver works out which room a payload +belongs to by hashing the sender together with the p-tags — +`GiftWrapPayload.aggregatedParticipantsPublicKey`, via `ChatRoom.deriveChatRoomId` +— so leaving a member out changes the id, and the resend is filed under a room +that does not exist. `getOrCreateChatRoom` then mints that room rather than +rejecting it, which turns a repaired session into a phantom conversation in the +reader's list. + +There is no saving to trade against that, either: `sealGiftWrapPayload` wraps for +every participant of the room it looks up, not for the p-tags it was handed, so a +narrowed payload costs exactly as many wraps as a full one. Who a resend is *for* +travels in `frost_silent`, which is not part of the hash. This is the first +signature a group makes, on gift wraps, per `GroupKeyStateManager.propose`. ## Phase 3 — a named member republishes what it authored @@ -545,11 +556,18 @@ queued nothing must record nothing, or the group waits forever on a message that never went out. The comment on `publishOwn` had the trade written down in one direction only. -**Narrowing is a `recipientTags` change, not a `broadcast` branch.** On NIP-17 a -resend names the quiet members and the signers; on Marmot the parameter is -ignored, because a group event is encrypted to the group and names nobody. One -optional argument covers both, which is the shape `broadcast` already had for -the transports. +**Narrowing the recipients is not something this transport allows, and the test +meant to prove it instead encoded the bug.** A resend was narrowed to the quiet +members, with a passing test asserting the p-tags were narrowed. Both the code and +the test were wrong about what a p-tag is for: on NIP-17 the set is hashed into +the room id a receiver files the payload under, so a narrowed resend would have +landed in a room nobody is in — and `getOrCreateChatRoom` would have created it. +Found by asking whether the change was backwards compatible. It was; this was +never a compatibility bug, it broke old and new receivers identically. + +The lesson is narrower than "assert behaviour, not bytes". The p-tags *are* the +right thing to assert — they just mean something other than what they look like, +and the assertion needed to be that they do not change. ## What this does not do @@ -601,10 +619,12 @@ events where one does, and per `docs/marmot-direct-messages.md` every member of group sees that a private message was sent and to whom — so a nudge would read as a side channel in a room where the nudge is public information anyway. -**Narrowing the Marmot resend to the silent set.** There is no narrowing to do: a -group event is encrypted to the group and names nobody, which is the point -`broadcast` makes about why its p-tags are not cosmetic. Narrowing is real on -NIP-17, where a wrap is sealed per recipient, and Phase 2 does it there. +**Narrowing the resend to the silent set.** Tried, landed, and reverted. On Marmot +there is nothing to narrow — a group event is encrypted to the group and names +nobody. On NIP-17 it is actively wrong: the p-tag set is what a receiver hashes +into the room id, so a narrowed resend lands in a room that does not exist, and it +buys nothing anyway because the sealer wraps per room participant rather than per +p-tag. See the transport note in Phase 2. **Resetting the queue row instead of writing a new one.** `@Upsert` of a `MarmotInnerEvent` with `marmotGroupEventId = null` puts an already-sent row back on