Skip to content

Commit

Permalink
[io_limbs] handle unmarshal for sources longer, but smaller
Browse files Browse the repository at this point in the history
If a given source `src` is given to `unmarshal` where the most
significant bytes are all zero beyond the size that fits into the
destination BigInt, we still return a successful parse. Previously we
would just return false whenever we found more bytes than can possibly
fit (this leads to issues with e.g. EVM precompiles tests, which come
as strings that are longer).

Note: We can probably do the all zero check in a better
way (i.e. using the fact that endianness does not matter and just
compare all relevant bytes, but not sure about the most elegant way to
do that, taking the dynamic sizes into account)
  • Loading branch information
Vindaar committed Jun 28, 2024
1 parent 09755bd commit abddc2b
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions constantine/serialization/io_limbs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,17 @@ func unmarshalLE[T](

# if full, dump
if acc_len >= wordBitWidth:
if dst_idx == dst.len:
return false
if dst_idx == dst.len: # return, however check if all remaining
# elements are 0 (more significant bits beyond `src_idx`)
# For example, given a
# `src = "DDCCBBAA0000"`
# and a 4 byte LittleEndian `BigInt`, `src` is fine, because the most
# significant two bytes are all zero.
var allZero = true
for jidx in src_idx ..< dst.len:
if src[jidx] != 0: allZero = false
if allZero: return true
else: return false

dst[dst_idx] = acc
inc dst_idx
Expand Down Expand Up @@ -118,8 +127,17 @@ func unmarshalBE[T](

# if full, dump
if acc_len >= wordBitWidth:
if dst_idx == dst.len:
return false
if dst_idx == dst.len: # return, however check if all remaining
# elements are 0 (more significant bits beyond `src_idx-1`)
# For examplle, given a
# `src = "0000AABBCCDD"`
# and a 4 byte BigEndian `BigInt`, `src` is fine, because the most
# significant two bytes are all zero.
var allZero = true
for jidx in countdown(src_idx, 0):
if src[jidx] != 0: allZero = false
if allZero: return true
else: return false

dst[dst_idx] = acc
inc dst_idx
Expand Down

0 comments on commit abddc2b

Please sign in to comment.