Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unmarshal bugs #409

Merged
merged 6 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion constantine/ethereum_evm_precompiles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ func parseRawUint[Name: static Algebra](
## Return false if the integer is larger than the field modulus.
## Returns true on success.
var big {.noInit.}: Fp[Name].getBigInt()
big.unmarshal(src, bigEndian)
if not big.unmarshal(src, bigEndian):
return cttEVM_IntLargerThanModulus # `dst` too small for `src`!

if not bool(big < Fp[Name].getModulus()):
return cttEVM_IntLargerThanModulus
Expand Down
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
Copy link
Owner

Choose a reason for hiding this comment

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

I'll have to review more in-depth that case but we can't leak the position of non-zero bytes as this breaks constant-time property.
And even leaking if something is zero is tricky.

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
Copy link
Owner

Choose a reason for hiding this comment

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

same remark, analyzing the data itself instead of metadata (length) breaks constant-time.

if allZero: return true
else: return false

dst[dst_idx] = acc
inc dst_idx
Expand Down
Loading