common/math: optimized modexp (+ fuzzer) (#25525)

This adds a 
* core/vm, tests: optimized modexp + fuzzer

* common/math: modexp optimizations

* core/vm: special case base 1 in big modexp

* core/vm: disable fastexp
This commit is contained in:
Martin Holst Swende
2022-10-12 10:34:52 +02:00
committed by GitHub
parent a007ab786c
commit bed3b10086
4 changed files with 181 additions and 2 deletions
+13 -2
View File
@@ -380,12 +380,23 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) {
base = new(big.Int).SetBytes(getData(input, 0, baseLen))
exp = new(big.Int).SetBytes(getData(input, baseLen, expLen))
mod = new(big.Int).SetBytes(getData(input, baseLen+expLen, modLen))
v []byte
)
if mod.BitLen() == 0 {
switch {
case mod.BitLen() == 0:
// Modulo 0 is undefined, return zero
return common.LeftPadBytes([]byte{}, int(modLen)), nil
case base.Cmp(common.Big1) == 0:
//If base == 1, then we can just return base % mod (if mod >= 1, which it is)
v = base.Mod(base, mod).Bytes()
//case mod.Bit(0) == 0:
// // Modulo is even
// v = math.FastExp(base, exp, mod).Bytes()
default:
// Modulo is odd
v = base.Exp(base, exp, mod).Bytes()
}
return common.LeftPadBytes(base.Exp(base, exp, mod).Bytes(), int(modLen)), nil
return common.LeftPadBytes(v, int(modLen)), nil
}
// newCurvePoint unmarshals a binary blob into a bn256 elliptic curve point,