The Wayback Machine - https://web.archive.org./web/20210502044036/https://github.com/ethereum/solidity/issues/10687
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

Convert certain and(mul(a, b), mask) and and(add(a, b), mask) into mulmod(a, b, x) and addmod(a, b, y) #10687

Open
hrkrshnn opened this issue on Dec 23, 2020 · 1 comment

Comments

@hrkrshnn
Copy link
Member

@hrkrshnn hrkrshnn commented on Dec 23, 2020

For example the following solidity snippet

uint160 a, b;
unchecked { a + b }

would be converted into (by the optimizer)

let _1 := sub(shl(160, 1), 1)
let value := and(mul(a, b), _2)

Here _1 is a shorter representation of the constant.

Instead the more optimized version would be

let _1 := shl(160, 1)
let value := mulmod(a, b, _1)

It is shorter and avoids a sub. and + mul has the same gas as mulmod.

Similarly, for addmod, but add + and is 6, and addmod is 8. We save a sub. So total saving of 2 gas and shorter deploy code.


It maybe easier to implement this as a rule and(mul(X, Y), A) -> mulmod(X, Y, 2**N) where A = 2**N - 1. Similarly, for the other?

@chriseth
Copy link
Contributor

@chriseth chriseth commented on Jan 11, 2021

This sounds good in general, but I'm a bit concerned about this transform destroying some other potential opportunities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants