Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The summation check in the function is necessary (and even quite clever) to verify that if you weight the provided “digits” (or bytes) by the base powers and add them up you get back the original value (or its lower bits). However, this check by itself is not sufficient to guarantee that the output is the canonical byte‐decomposition of the input. Here’s why:
The reconstruction only checks that
equals either the original value (if$(256^{\text{len}})$ bits. This equality would be satisfied by many different choices of the
len == 31
) or the value “masked” to the loweroutput
array if there were no constraints on an individual element. In a unique base‑256 representation, each byte must satisfyHowever, the code does not explicitly check that each
output[i]
falls in this range. Without this per‑digit range check, you might have a “non‑canonical” decomposition where, for example, one digit is larger than 255 but another digit is adjusted so that the overall weighted sum still equals the original value.The Bitwise Mask Step Isn’t Enough:
In the branch where
len < 31
, the function computesThis sequence checks that the reconstruction matches the lower
len
bytes ofvalue
(i.e.value & (256^len - 1)
). But again, this only tests the aggregate value. It does not, by itself, enforce that the "digits" making up that aggregate were chosen from the unique set of values between 0 and 255.Canonical Representation Assumption:
The uniqueness of the base‑256 representation (and hence the guarantee that the output is “correct”) relies on the assumption that each digit is already in the canonical range. If for any reason the code (or the hint in the
%{ felt252_to_bytes_le %}
section) does not enforce or assume that each byte is in ([0, 255]), then the summation check alone does not rule out alternative representations that would produce the same sum.Conclusion
To rule out non‑canonical decompositions, you would need explicit assertions like:
(performed for each index) to fully ensure that the decomposition is unique and correct.