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

Use BN reduction context in modexp precompile #463

Merged
merged 1 commit into from
Mar 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.lten(1)) 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