Skip to content

Commit 6b5701a

Browse files
committed
Make getAdjustedExponentLength in modexp precompile more robust toward > 53 bit number handling
1 parent 7f5a1e2 commit 6b5701a

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

lib/precompiled/05-modexp.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ function multComplexity (x) {
2525
}
2626

2727
function getAdjustedExponentLength (data) {
28-
var baseLen = new BN(data.slice(0, 32)).toNumber()
28+
var expBytesStart
29+
try {
30+
var baseLen = new BN(data.slice(0, 32)).toNumber()
31+
expBytesStart = 96 + baseLen // 96 for base length, then exponent length, and modulus length, then baseLen for the base data, then exponent bytes start
32+
} catch (e) {
33+
expBytesStart = Number.MAX_SAFE_INTEGER - 32
34+
}
2935
var expLen = new BN(data.slice(32, 64))
30-
var expBytesStart = 96 + baseLen // 96 for base length, then exponent length, and modulus length, then baseLen for the base data, then exponent bytes start
3136
var firstExpBytes = Buffer.from(data.slice(expBytesStart, expBytesStart + 32)) // first word of the exponent data
3237
firstExpBytes = utils.setLengthRight(firstExpBytes, 32) // reading past the data reads virtual zeros
3338
firstExpBytes = new BN(firstExpBytes)

0 commit comments

Comments
 (0)