Skip to content

Bounds checks for exponentiation#2072

Merged
iamdefinitelyahuman merged 11 commits into
vyperlang:masterfrom
iamdefinitelyahuman:safe-exp
Jun 30, 2020
Merged

Bounds checks for exponentiation#2072
iamdefinitelyahuman merged 11 commits into
vyperlang:masterfrom
iamdefinitelyahuman:safe-exp

Conversation

@iamdefinitelyahuman

@iamdefinitelyahuman iamdefinitelyahuman commented Jun 30, 2020

Copy link
Copy Markdown
Contributor

What I did

Add overflow checks for exponentiation

Closes #1684
Closes #1685

How I did it

Given a ** b:

  • when a or b is a literal, calculate the highest possible variable value at compile-time, and perform a check against that value at runtime. The logic for finding the upper limits is thanks to @fubuloubu's proof of concept 🙏 I'm just a copy-paster
  • when both a and b are variables, raise an exception suggesting the use of pow_mod256
  • add a new builtin function pow_mod256 that allows access to the EXP instruction without any overflow checks

How to verify it

Run tests. I added quite a few new cases.

Cute Animal Picture

image

Comment thread tests/fuzzing/test_exponents.py
Comment thread tests/parser/types/numbers/test_uint256.py
Comment thread vyper/context/types/value/numeric.py
Comment thread vyper/parser/expr.py
Comment thread vyper/parser/expr.py
Comment thread vyper/parser/expr.py Outdated
Comment thread vyper/parser/expr.py Outdated
Comment thread tests/fuzzing/test_exponents.py Outdated
@lgtm-com

lgtm-com Bot commented Jun 30, 2020

Copy link
Copy Markdown
Contributor

This pull request introduces 1 alert when merging e0c596a into bd8e0d3 - view on LGTM.com

new alerts:

  • 1 for Unused import

iamdefinitelyahuman and others added 2 commits July 1, 2020 00:39
Co-authored-by: Bryant Eisenbach <3859395+fubuloubu@users.noreply.github.com>
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.

int128 exponentiation can overflow without error uint256 exponentiation can overflow without error as long as the result is greater than the base

2 participants