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

feat: Enhance power operation gas estimation #4522

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
23 changes: 22 additions & 1 deletion vyper/codegen/arithmetic.py
Original file line number Diff line number Diff line change
@@ -337,6 +337,23 @@ def safe_mod(x, y):
return IRnode.from_list([MOD, x, clamp("gt", y, 0)], error_msg="safemod")


def estimate_pow_gas(x, y, typ):
"""
Estimate gas cost for power operation.
"""
# Base gas cost for EXP opcode
base_gas = 10

# Gas per byte of exponent
if y.is_literal:
byte_length = (y.value.bit_length() + 7) // 8
else:
# Conservative estimate for unknown exponent
byte_length = (typ.bits + 7) // 8

return base_gas + (50 * byte_length)


# def safe_pow(x: IRnode, y: IRnode) -> IRnode:
def safe_pow(x, y):
typ = x.typ
@@ -371,6 +388,10 @@ def safe_pow(x, y):
# TODO this is currently unreachable, once we implement a way to do it safely
# remove the check in `vyper/context/types/value/numeric.py`
return
gas_estimate = estimate_pow_gas(x, y, typ)
operation = ["exp", x, y]
if hasattr(x, "add_gas_estimate"):
Copy link
Member

Choose a reason for hiding this comment

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

why do we use hasattr here?

operation = IRnode.from_list(operation, add_gas_estimate=gas_estimate)

assertion = IRnode.from_list(["assert", ok], error_msg="safepow")
return IRnode.from_list(["seq", assertion, ["exp", x, y]])
return IRnode.from_list(["seq", assertion, operation])