Sitelet https://github.com/starkware-libs/cairo/pull/10108
Skip to content

Made Encode::size_hint more exact. - #10108

Merged
orizi merged 1 commit into
mainfrom
orizi/06-16-made_encode_size_hint_more_exact
Jun 16, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-16-made_encode_size_hint_more_exact

Conversation

@orizi

@orizi orizi commented Jun 16, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes the size_hint implementation for BigIntAsHex parity scale codec encoding so that encoding 0 returns the correct size hint of 2 bytes (1 sign/len byte + 1 data byte) instead of 1 byte.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

The size_hint method was computing the encoded size as 1 + bits.div_ceil(8), where bits is the number of bits in the value. For the value 0, bits() returns 0, so div_ceil(8) also returns 0, yielding a size hint of 1. However, the actual encode implementation always emits at least one data byte — 0 encodes as [sign_len_byte, 0x00], which is 2 bytes. This mismatch between size_hint and the actual encoded length is incorrect.


What was the behavior or documentation before?

size_hint for BigIntAsHex returned 1 when encoding 0, underreporting the actual encoded size by 1 byte.


What is the behavior or documentation after?

size_hint now returns at least 2 for any value (including 0), correctly reflecting that the encoder always emits a minimum of one data byte in addition to the sign/length byte.


Related issue or discussion (if any)


Additional context

The fix applies .max(1) to the div_ceil(8) result, ensuring the data byte count is never reported as zero.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@orizi
orizi marked this pull request as ready for review June 16, 2026 07:43
@cursor

cursor Bot commented Jun 16, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Single-line sizing fix in codec hints only; no change to encode/decode logic or wire format.

Overview
Fixes BigIntAsHex parity-scale Encode::size_hint so it matches encode_to: the data length term now uses .max(1), because encoding always writes at least one payload byte (e.g. 0 → sign/len byte + 0x00, 2 bytes total).

Previously bits().div_ceil(8) was 0 for zero, so the hint was 1 byte while the encoder emitted 2.

Reviewed by Cursor Bugbot for commit 7c0eb5d. Bugbot is set up for automated code reviews on this repo. Configure here.

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:lgtm:

@eytan-starkware reviewed 1 file and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@orizi
orizi added this pull request to the merge queue Jun 16, 2026
Merged via the queue into main with commit 2079977 Jun 16, 2026
55 checks passed
@orizi
orizi deleted the orizi/06-16-made_encode_size_hint_more_exact branch June 16, 2026 11:00
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.

3 participants