Skip to content

Commit

Permalink
Improve montgomeryAdd implementation (matter-labs#113)
Browse files Browse the repository at this point in the history
* Make montgomery add more efficient

* Fix add montgomery for edge case

* Add montgomery add improvement for ecPairing
  • Loading branch information
IAvecilla authored Sep 21, 2023
1 parent e155896 commit f660dca
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 4 additions & 1 deletion precompiles/EcAdd.yul
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ object "EcAdd" {
}

function montgomeryAdd(augend, addend) -> ret {
ret := addmod(augend, addend, P())
ret := add(augend, addend)
if iszero(lt(ret, P())) {
ret := sub(ret, P())
}
}

function montgomerySub(minuend, subtrahend) -> ret {
Expand Down
5 changes: 4 additions & 1 deletion precompiles/EcMul.yul
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ object "EcMul" {
}

function montgomeryAdd(augend, addend) -> ret {
ret := addmod(augend, addend, P())
ret := add(augend, addend)
if iszero(lt(ret, P())) {
ret := sub(ret, P())
}
}

function montgomerySub(minuend, subtrahend) -> ret {
Expand Down
5 changes: 4 additions & 1 deletion precompiles/EcPairing.yul
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ object "EcPairing" {
/// @param addend The addend in Montgomery form.
/// @return ret The result of the Montgomery addition.
function montgomeryAdd(augend, addend) -> ret {
ret := addmod(augend, addend, P())
ret := add(augend, addend)
if iszero(lt(ret, P())) {
ret := sub(ret, P())
}
}

/// @notice Computes the Montgomery subtraction.
Expand Down

0 comments on commit f660dca

Please sign in to comment.