perf: build note plaintexts in a stack buffer instead of a heap Vec#8
Conversation
note_plaintext_bytes assembled the secret note plaintext (diversifier, value, rseed, memo) in a 52-byte stack array, copied it into a transient alloc::vec! buffer, and copied that again via from_slice -- one heap allocation plus two redundant copies per encrypted output on a previously allocation-free path, with the Vec freed without zeroization leaving the only heap copy of note plaintext in the encryption path. Build the plaintext directly in a single max-size stack buffer (COMPACT_NOTE_SIZE + MAX_MEMO_SIZE = 564 bytes) and convert once with from_slice(&buf[..len]). A new MAX_MEMO_SIZE bound in SIZE_CHECK guarantees at compile time that the buffer is large enough for any MemoSize implementation that compiles. Output is byte-for-byte unchanged, as pinned by the note encryption test vectors and the golden vectors added in #7. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Warning Review limit reached
Next review available in: 29 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. 📝 WalkthroughWalkthroughAdds a ChangesMemo size bound and plaintext buffer
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/note_encryption.rs`:
- Around line 205-224: The memo handling in the `encrypt`/plaintext assembly
logic needs a runtime length check because `M::SIZE_CHECK` only validates the
type size, not the length returned by `AsRef<[u8]>`. In the `note_encryption`
path, assert that `memo.as_ref().len() == M::MEMO_SIZE` before copying, and
compute the final `len` using `M::MEMO_SIZE` rather than the dynamic slice
length so the stack buffer copy and `NotePlaintextBytes::from_slice` stay
consistent.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 54b4afd2-8706-4f7f-bc27-c39a4e182885
📒 Files selected for processing (2)
src/memo.rssrc/note_encryption.rs
SIZE_CHECK constrains the size of the Memo type, but a custom AsRef implementation could still return a slice of a different length, surfacing as an opaque slice-index panic in the buffer copy. Assert the slice length explicitly and derive the plaintext length from M::MEMO_SIZE, which SIZE_CHECK already bounds by MAX_MEMO_SIZE. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Summary
Fixes finding #4 from the MemoSize PR review:
Domain::note_plaintext_bytesassembled the secret note plaintext (diversifier, value, rseed, memo) in a 52-byte stack array, copied it into a transientalloc::vec!heap buffer, and copied that again viafrom_slice— one heap allocation plus two extra copies per encrypted output on a path that was allocation-free before the memo generalization. The Vec was also dropped without zeroization, making it the only heap copy of note plaintext anywhere in the encryption path.Changes
note_plaintext_bytesnow writes the plaintext fields directly into a single max-size stack buffer ([u8; COMPACT_NOTE_SIZE + MAX_MEMO_SIZE]= 564 bytes) and converts once withfrom_slice(&buf[..len]): zero heap allocations, one copy instead of three bufferspub const MAX_MEMO_SIZE: usize = 512inmemo.rs, with a corresponding bound added toMemoSize::SIZE_CHECK— the compiler now guarantees the stack buffer is large enough for everyMthat compiles, so the change introduces no new panic pathCompatibility
MEMO_SIZE <= 512bound is a new compile-time constraint on third-partyMemoSizeimplementations; both provided impls (512/36) are unaffected, and 512 is the largest known protocol memo. A future larger-memo protocol would bump one constantTest plan
cargo test— 87 tests pass (full suite including proving tests; golden vectors unchanged)cargo clippy --all-features --all-targetscleancargo fmt -- --checkRUSTDOCFLAGS="-D warnings" cargo doc --all-features --document-private-itemsclean🤖 Generated with Claude Code
Summary by CodeRabbit