Exp implements exponentiation by squaring. Exp returns a newly-allocated big integer and does not change base or exponent. The result is truncated to 256 bits. Courtesy @karalabe and @chfast.
(base, exponent *big.Int)
| 210 | // |
| 211 | // Courtesy @karalabe and @chfast. |
| 212 | func Exp(base, exponent *big.Int) *big.Int { |
| 213 | result := big.NewInt(1) |
| 214 | |
| 215 | for _, word := range exponent.Bits() { |
| 216 | for i := 0; i < wordBits; i++ { |
| 217 | if word&1 == 1 { |
| 218 | U256(result.Mul(result, base)) |
| 219 | } |
| 220 | U256(base.Mul(base, base)) |
| 221 | word >>= 1 |
| 222 | } |
| 223 | } |
| 224 | return result |
| 225 | } |