Exponential Redundant Calculations in uint256_fast_exp
used for EXP opcode
#96
Labels
bug
Something isn't working
downgraded by judge
Judge downgraded the risk level of this issue
duplicate-2
grade-b
QA (Quality Assurance)
Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax
🤖_105_group
AI based duplicate group recommendation
sponsor disputed
Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue
sufficient quality report
This report is of sufficient quality
Lines of code
https://github.com/kkrt-labs/kakarot/blob/7411a5520e8a00be6f5243a50c160e66ad285563/src/utils/uint256.cairo#L344-L373
Vulnerability details
Impact
The implementation of
uint256_fast_exp
performs redundant recursive calculations that grow exponentially for certain exponent values, particularly powers of 2. This results in significantly increased Cairo steps consumption (up to ~28,380 extra steps for exponent 128) and consequently higher gas costs. The issue affects the EXP opcode execution in Kakarot's EVM implementation.The severity is high because:
Proof of Concept
utils/uint256.cairo:L344-L373
The function makes a redundant recursive call in the even number case. Let's analyze the execution flow for exponent = 8 as an example:
Initial call with exponent = 8
For each call with exponent = 4
For each call with exponent = 2
Total recursive calls:
Results in 15 recursive calls instead of the optimal 4 calls!
Cairo steps rough estimation:
This inefficiency grows exponentially, for larger powers of 2:
Note: those are rough estimations, Cairo steps count changes based on the execution trace.
Tools Used
Manual Review
Recommended Mitigation Steps
Remove the redundant recursive call in the even number case and reuse the result from the first call.
Assessed type
Other
The text was updated successfully, but these errors were encountered: