Skip to content

Commit 4bd55a0

Browse files
authored
common/math: copy result in Exp (ethereum#29233)
common/math: does not change base parameter
1 parent 99bbbc0 commit 4bd55a0

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

common/math/big.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func ReadBits(bigint *big.Int, buf []byte) {
224224
}
225225
}
226226

227-
// U256 encodes as a 256 bit two's complement number. This operation is destructive.
227+
// U256 encodes x as a 256 bit two's complement number. This operation is destructive.
228228
func U256(x *big.Int) *big.Int {
229229
return x.And(x, tt256m1)
230230
}
@@ -255,14 +255,15 @@ func S256(x *big.Int) *big.Int {
255255
//
256256
// Courtesy @karalabe and @chfast
257257
func Exp(base, exponent *big.Int) *big.Int {
258+
copyBase := new(big.Int).Set(base)
258259
result := big.NewInt(1)
259260

260261
for _, word := range exponent.Bits() {
261262
for i := 0; i < wordBits; i++ {
262263
if word&1 == 1 {
263-
U256(result.Mul(result, base))
264+
U256(result.Mul(result, copyBase))
264265
}
265-
U256(base.Mul(base, base))
266+
U256(copyBase.Mul(copyBase, copyBase))
266267
word >>= 1
267268
}
268269
}

0 commit comments

Comments
 (0)