Skip to content

cipher: document SeekNum encoding conventions and debug panic - #2481

Open
tautschnig wants to merge 2 commits into
RustCrypto:masterfrom
tautschnig:document-seeknum-conventions
Open

cipher: document SeekNum encoding conventions and debug panic#2481
tautschnig wants to merge 2 commits into
RustCrypto:masterfrom
tautschnig:document-seeknum-conventions

Conversation

@tautschnig

Copy link
Copy Markdown

SeekNum::from_block_byte and SeekNum::into_block_byte use different (block, byte) encodings and are not inverses of each other:

  • into_block_byte returns the position-division pair: block = self / bs, byte = self % bs (so byte is in 0..bs);
  • from_block_byte expects the keystream-buffer pair used by StreamCipherCoreWrapper::try_current_pos: block is the next block to generate and byte in 1..=bs is the consumed byte count, computing block * bs - (bs - byte).

Feeding the output of one into the other therefore misbehaves: for a position at an exact block boundary (e.g. 16u32.into_block_byte(16) = (1, 0)), from_block_byte(1, 0, 16) panics in debug builds (debug_assert!(byte != 0)) and silently returns the wrong position (0 instead of 16) in release builds. Only # Errors was documented.

This PR documents both conventions, their non-inverse relationship, and the debug panic — no functional change, since StreamCipherCoreWrapper uses the conventions consistently and correctly. If you would rather make the pair self-consistent (or replace the debug_assert with an OverflowError), happy to rework in that direction instead.

Found by running Kani's autoharness (model-checking/kani#3832) over cipher 0.5.2, which reported the debug_assert reachable on the public trait method; reproduced with plain cargo before filing.

from_block_byte and into_block_byte use different (block, byte) encodings
(keystream-buffer convention with byte in 1..=bs vs. position division with
byte in 0..bs) and are not inverses of each other: feeding the output of
into_block_byte back into from_block_byte panics in debug builds
(debug_assert!(byte != 0) fires for positions at exact block boundaries)
and silently computes a wrong position in release builds (e.g. 16 -> 0 for
bs=16). Only '# Errors' was documented.

Document both conventions, their non-inverse relationship, and the debug
panic. Found by running Kani's autoharness (model-checking/kani#3832) over
cipher 0.5.2, which reported the debug_assert reachable; the wrapper's
try_seek/try_current_pos use the conventions correctly, so this is a
documentation gap on the public trait, not a functional bug in the wrapper.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings August 5, 2026 08:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the public documentation for SeekNum::{from_block_byte, into_block_byte} by clarifying that they intentionally use different (block, byte) encodings (and therefore are not inverses), and by documenting the debug-assert panic behavior in from_block_byte when byte == 0.

Changes:

  • Document the keystream-buffer encoding expected by SeekNum::from_block_byte and explicitly note it differs from into_block_byte.
  • Document the position-division encoding returned by SeekNum::into_block_byte and explicitly note it differs from from_block_byte.
  • Add # Panics docs describing the debug-assert panic scenario for from_block_byte.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cipher/src/stream.rs Outdated
Comment on lines 291 to 292
/// # Errors
/// Returns [`OverflowError`] in the event of a counter overflow.
Comment thread cipher/src/stream.rs Outdated
Comment on lines 306 to 308
///
/// # Errors
/// Returns [`OverflowError`] in the event of a counter overflow.
Review feedback: OverflowError is also returned for out-of-range inputs
(byte > bs, unconvertible block), and into_block_byte divides by bs, so
bs == 0 panics; document both.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants