Skip to content

Commit

Permalink
common/math: copy result in Exp (ethereum#29233)
Browse files Browse the repository at this point in the history
common/math: does not change base parameter
  • Loading branch information
AaronChen0 authored Mar 12, 2024
1 parent 99bbbc0 commit 4bd55a0
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions common/math/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func ReadBits(bigint *big.Int, buf []byte) {
}
}

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

for _, word := range exponent.Bits() {
for i := 0; i < wordBits; i++ {
if word&1 == 1 {
U256(result.Mul(result, base))
U256(result.Mul(result, copyBase))
}
U256(base.Mul(base, base))
U256(copyBase.Mul(copyBase, copyBase))
word >>= 1
}
}
Expand Down

0 comments on commit 4bd55a0

Please sign in to comment.