Skip to content

Commit 8c5f5c7

Browse files
dflupuwechman
authored andcommitted
add rules for mod(mul(X, Y), A) & mod(add(X, Y), A)
1 parent 75300c3 commit 8c5f5c7

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

libevmasm/RuleList.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,32 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart5(
279279
Pattern B,
280280
Pattern,
281281
Pattern X,
282-
Pattern
282+
Pattern Y
283283
)
284284
{
285285
using Word = typename Pattern::Word;
286286
using Builtins = typename Pattern::Builtins;
287287

288288
std::vector<SimplificationRule<Pattern>> rules;
289289

290+
// Replace MOD(MUL(X, Y), A) with MULMOD(X, Y, A) iff A=2**N
291+
rules.push_back({
292+
Builtins::MOD(Builtins::MUL(X, Y), A),
293+
[=]() -> Pattern { return Builtins::MULMOD(X, Y, A); },
294+
[=] {
295+
return A.d() > 0 && ((A.d() & (A.d() - 1)) == 0);
296+
}
297+
});
298+
299+
// Replace MOD(ADD(X, Y), A) with ADDMOD(X, Y, A) iff A=2**N
300+
rules.push_back({
301+
Builtins::MOD(Builtins::ADD(X, Y), A),
302+
[=]() -> Pattern { return Builtins::ADDMOD(X, Y, A); },
303+
[=] {
304+
return A.d() > 0 && ((A.d() & (A.d() - 1)) == 0);
305+
}
306+
});
307+
290308
// Replace MOD X, <power-of-two> with AND X, <power-of-two> - 1
291309
for (size_t i = 0; i < Pattern::WordSize; ++i)
292310
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
mstore(0, mod(add(mload(0), mload(1)), 32))
3+
}
4+
// ----
5+
// step: expressionSimplifier
6+
//
7+
// {
8+
// {
9+
// let _3 := mload(1)
10+
// let _4 := 0
11+
// mstore(_4, addmod(mload(_4), _3, 32))
12+
// }
13+
// }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
mstore(0, mod(mul(mload(0), mload(1)), 32))
3+
}
4+
// ----
5+
// step: expressionSimplifier
6+
//
7+
// {
8+
// {
9+
// let _3 := mload(1)
10+
// let _4 := 0
11+
// mstore(_4, mulmod(mload(_4), _3, 32))
12+
// }
13+
// }

0 commit comments

Comments
 (0)