Skip to content

Commit

Permalink
Use BN.red in modexp precompile
Browse files Browse the repository at this point in the history
Add checks for E == 0 and M == 1

Fix M == 1 check
  • Loading branch information
s1na committed Mar 5, 2019
1 parent 0162d47 commit 8453ed8
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/precompiled/05-modexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ function getAdjustedExponentLength (data) {
return adjustedExpLen
}

// Taken from https://stackoverflow.com/a/1503019
function expmod (B, E, M) {
if (E.isZero()) return new BN(1).mod(M)
var BM = B.mod(M)
var R = expmod(BM, E.divn(2), M)
R = (R.mul(R)).mod(M)
if (E.mod(new BN(2)).isZero()) return R
return (R.mul(BM)).mod(M)
// Red asserts M > 1
if (M.cmpn(1) === 0) return new BN(0)
const red = BN.red(M)
const redB = B.toRed(red)
const res = redB.redPow(E)
return res.fromRed()
}

function getOOGResults (opts, results) {
Expand Down

0 comments on commit 8453ed8

Please sign in to comment.