-
-
Notifications
You must be signed in to change notification settings - Fork 804
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
uint256
exponentiation can overflow without error as long as the result is greater than the base
#1684
Comments
Could maybe do something like: Would make it more difficult to find a falsifying example, but probably very hard to do a full-on correctness proof without a more complicated and inefficient check/calculation |
I thought about this for awhile and one approach is Probably a better approach is to calculate the largest power that any number can be raised to without overflowing. For powers of 2 this is quite obvious, e.g. for
There is probably some way of interpreting x as a floating point number which makes this super easy, but it's unclear that is any more gas efficient than just calculating |
Working on it a bit more, came to this: |
well for y <= 1 it can never overflow |
EDIT: I added |
There are algorithms for computing |
What about the following approach? Imagine we're handling
|
In the case either |
Another resource: https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious |
Related to this, the current bounds checks raise on |
I'd like to tackle this in Suggested solution:
I'd like to move this over to a VIP to cover the changes. 👍 if you agree. |
A more efficient approach when both values are not literal - thanks @michwill! |
In particular, probably |
It's probably ok if exponentiation for nonconstant a and b will be not via a builtin. And really - is there anyone who calculates |
Took a hand at (3) using the link @michwill sent: https://github.com/fubuloubu/integer-exponential. Looks like it takes 10994 gas in the |
This issue is due to the following code:
https://github.com/ethereum/vyper/blob/c296b2d7532d913103aad494b749f8179a3acddc/vyper/parser/expr.py#L646-L658
The text was updated successfully, but these errors were encountered: