Skip to content

[codec] Add Checksummed type - #4483

Open
crazywriter1 wants to merge 2 commits into
commonwarexyz:mainfrom
crazywriter1:codec-add-checksummed
Open

[codec] Add Checksummed type#4483
crazywriter1 wants to merge 2 commits into
commonwarexyz:mainfrom
crazywriter1:codec-add-checksummed

Conversation

@crazywriter1

Copy link
Copy Markdown
Contributor

Closes #1704.

Summary

Adds Checksummed<T, C>, a wrapper that appends a checksum of a value's encoding when writing and verifies it when reading. This lets any Codec type detect accidental corruption at rest or in transit without each caller hand-rolling the framing.

The wrapper is generic over a new Checksummer trait rather than over a hasher from commonware-cryptography, since the hashing crates depend on commonware-codec and the reverse dependency isn't possible. Checksummer::Digest is any FixedSize codec type, so implementations can range from a CRC to a cryptographic digest.

Details worth calling out for review:

  • Read::Cfg forwards to T::Cfg, so wrapping a type doesn't change how it is configured. Config-driven failures (e.g. a RangeCfg rejection) surface before any checksum work.
  • Verification re-encodes the decoded value instead of retaining the consumed bytes, which avoids buffering but assumes canonical encodings. Every implementation in this crate is canonical; for anything else, a non-canonical encoding that decodes to the same value is rejected as a mismatch. This is documented on the type.
  • The PhantomData<fn() -> C> marker keeps the wrapper's auto traits independent of C.
  • Docs note explicitly that a checksum is not authentication.

Testing

  • Unit tests cover round trips for fixed- and variable-size inner types, config forwarding, encode_size agreement with the written bytes, corrupted payload and corrupted checksum rejection, a missing trailing checksum, and that read consumes exactly one value from a buffer holding more.
  • Doctests on both Checksummer and Checksummed.
  • just pre-pr passes, including the no_std build (the type adds no std usage).

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 08c115b. Configure here.

Comment thread codec/src/types/checksummed.rs Outdated
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.

[codec] Add Checksummed type

1 participant