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

Refactor and Enhance rpow Function in AssemblyBinExp.sol #328

Open
wants to merge 3 commits into
base: gh-pages
Choose a base branch
from

Conversation

SITADRITA1
Copy link

@SITADRITA1 SITADRITA1 commented Mar 16, 2025

Removed duplicate pragma statement

Old:

pragma solidity 0.8.26;
pragma solidity ^0.8.26;

New:

pragma solidity ^0.8.26;

Reason: The second declaration was redundant and unnecessary.
Fixed incorrect handling of 0^n for n > 0

Old:

case 0 { z := b } default { z := 0 }

New:

case 0 { z := b } default { revert(0, 0) }

Reason: Previously, when x = 0 and n > 0, the function returned 0 instead of reverting. This change ensures mathematical correctness by reverting in such cases.
Optimized conditional assignment for exponent parity check

Old:

switch mod(n, 2)
case 0 { z := b }
case 1 { z := x }

New:

z := b
if mod(n, 2) { z := x }

Reason: Replacing switch with an if statement reduces gas consumption and simplifies the code.
Removed duplicate declaration of let half := div(b, 2)

Old:

let half := div(b, 2) 
let half := div(b, 2) 

New:

let half := div(b, 2)

Reason: The duplicate declaration was unnecessary and increased execution costs.
Eliminated redundant overflow checks

Old:

if iszero(eq(div(xx, x), x)) { revert(0, 0) }
if and(iszero(iszero(x)), iszero(eq(div(xx, x), x))) { revert(0, 0) }

New:

if iszero(eq(div(xx, x), x)) { revert(0, 0) }

Reason: The second check was redundant and performed the same validation.
Removed unnecessary reassignments for rounding calculations

Old:

zx := add(zx, half);
z := div(zx, b);

New:

let zxRound := add(zx, half);
if lt(zxRound, zx) { revert(0, 0) }
z := div(zxRound, b);

Reason: The old code contained repeated assignments that increased gas costs without adding extra functionality.

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.

1 participant