Made Encode::size_hint more exact. - #10108
Conversation
PR SummaryLow Risk Overview Previously Reviewed by Cursor Bugbot for commit 7c0eb5d. Bugbot is set up for automated code reviews on this repo. Configure here. |
eytan-starkware
left a comment
There was a problem hiding this comment.
@eytan-starkware reviewed 1 file and all commit messages, and made 1 comment.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

Summary
Fixes the
size_hintimplementation forBigIntAsHexparity scale codec encoding so that encoding0returns the correct size hint of 2 bytes (1 sign/len byte + 1 data byte) instead of 1 byte.Type of change
Please check one:
Why is this change needed?
The
size_hintmethod was computing the encoded size as1 + bits.div_ceil(8), wherebitsis the number of bits in the value. For the value0,bits()returns0, sodiv_ceil(8)also returns0, yielding a size hint of1. However, the actualencodeimplementation always emits at least one data byte —0encodes as[sign_len_byte, 0x00], which is 2 bytes. This mismatch betweensize_hintand the actual encoded length is incorrect.What was the behavior or documentation before?
size_hintforBigIntAsHexreturned1when encoding0, underreporting the actual encoded size by 1 byte.What is the behavior or documentation after?
size_hintnow returns at least2for any value (including0), 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 thediv_ceil(8)result, ensuring the data byte count is never reported as zero.