Skip to content

Commit

Permalink
evmmax: Clean up mul implementation (#864)
Browse files Browse the repository at this point in the history
- Improve `c` carry value handling.
- Correctly place the TODO comments about possible optimization.
  • Loading branch information
chfast authored Apr 17, 2024
1 parent ed5562f commit fa061d7
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/evmmax/evmmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,18 @@ UintT ModArith<UintT>::mul(const UintT& x, const UintT& y) const noexcept
std::tie(c, t[j]) = addmul(t[j], x[j], y[i], c);
auto tmp = addc(t[S], c);
t[S] = tmp.value;
auto d = tmp.carry;
const auto d = tmp.carry; // TODO: Carry is 0 for sparse modulus.

c = 0;
auto m = t[0] * m_mod_inv;
std::tie(c, t[0]) = addmul(t[0], m, mod[0], c);
const auto m = t[0] * m_mod_inv;
std::tie(c, std::ignore) = addmul(t[0], m, mod[0], 0);
for (size_t j = 1; j != S; ++j)
std::tie(c, t[j - 1]) = addmul(t[j], m, mod[j], c);
tmp = addc(t[S], c);
t[S - 1] = tmp.value;
t[S] = d + tmp.carry; // TODO: Carry is 0 for sparse modulus.
}

if (t >= mod) // TODO: cannot overflow if modulus is sparse (e.g. 255 bits).
if (t >= mod)
t -= mod;

return static_cast<UintT>(t);
Expand Down

0 comments on commit fa061d7

Please sign in to comment.