feat(secret-stream): add SecretStream::into_split() for owned read/write halves#26
Open
eshork wants to merge 1 commit into
Open
feat(secret-stream): add SecretStream::into_split() for owned read/write halves#26eshork wants to merge 1 commit into
eshork wants to merge 1 commit into
Conversation
…ite halves Adds `SecretStream::into_split(self) -> (SecretStreamReadHalf<ReadHalf<T>>, SecretStreamWriteHalf<WriteHalf<T>>)`, splitting the underlying transport with `tokio::io::split` and moving the decryptor (`Pull`) into the read half and the encryptor (`Push`) into the write half. This lets a caller drive read and write from separate tasks concurrently without either half cancelling the other's in-flight `read_exact`/`write_all`, which is required for full-duplex use over a single stream (e.g. multiplexing many TCP flows). The half methods reuse the existing `read_frame`/`write_frame` helpers and the same `Push`/`Pull` cipher state, so wire framing and AEAD behavior are byte-for-byte identical to the unsplit `read()`/`write()`. No change to the Noise handshake, secretstream cipher, or framing. Purely additive: a new import, `into_split()`, two half structs, and two tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
SecretStream::into_split()so a caller can own the read half and write half in separate tasks:It splits the underlying transport with
tokio::io::splitand moves the decryptor (Pull) intoSecretStreamReadHalfand the encryptor (Push) intoSecretStreamWriteHalf.Motivation
A
SecretStreamis inherently bidirectional, butread()andwrite()both take&mut self, so full-duplex use over a single stream forces either anArc<Mutex<...>>(the reader holds the lock acrossread_exact, starving writes) or a single-taskselect!(read, outbound)driver (dropping an in-flightread()mid-frame loses bytes -> framing desync -> AEAD failure -> link reset). Owned halves eliminate both hazards: because the two halves hold disjoint cipher state, neither task can cancel the other's in-flight framed read/write. This is exactly what a mux carrying many TCP flows over oneSecretStreamneeds.What changed
Purely additive to
peeroxide-dht/src/secret_stream.rs:ReadHalf,WriteHalf),SecretStream::into_split(),SecretStreamReadHalf<R>withread(),SecretStreamWriteHalf<W>withwrite()andshutdown(),The half methods reuse the existing
read_frame/write_framehelpers and the samePush/Pullstate, so wire framing and AEAD behavior are byte-for-byte identical to the unsplitread()/write()(same 3-byte LE length prefix, same empty-keepalive skipping, sameCiphertextTooShortguard). No change to the Noise handshake or the secretstream cipher.Tests
cargo test -p peeroxide-dhtpasses, including two new tests:into_split_roundtrip(concurrent bidirectional bulk in separate tasks) andinto_split_empty_keepalive(keepalive handling parity).