mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-14 16:03:45 +00:00
core/vm: use optimized bigint (#26021)
This commit is contained in:
parent
a7bf4ba6bd
commit
7303e59f52
1 changed files with 17 additions and 10 deletions
|
|
@ -29,8 +29,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/crypto/bls12381"
|
"github.com/ethereum/go-ethereum/crypto/bls12381"
|
||||||
"github.com/ethereum/go-ethereum/crypto/bn256"
|
"github.com/ethereum/go-ethereum/crypto/bn256"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
|
big2 "github.com/holiman/big"
|
||||||
//lint:ignore SA1019 Needed for precompile
|
|
||||||
"golang.org/x/crypto/ripemd160"
|
"golang.org/x/crypto/ripemd160"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -266,9 +265,10 @@ var (
|
||||||
// modexpMultComplexity implements bigModexp multComplexity formula, as defined in EIP-198
|
// modexpMultComplexity implements bigModexp multComplexity formula, as defined in EIP-198
|
||||||
//
|
//
|
||||||
// def mult_complexity(x):
|
// def mult_complexity(x):
|
||||||
// if x <= 64: return x ** 2
|
//
|
||||||
// elif x <= 1024: return x ** 2 // 4 + 96 * x - 3072
|
// if x <= 64: return x ** 2
|
||||||
// else: return x ** 2 // 16 + 480 * x - 199680
|
// elif x <= 1024: return x ** 2 // 4 + 96 * x - 3072
|
||||||
|
// else: return x ** 2 // 16 + 480 * x - 199680
|
||||||
//
|
//
|
||||||
// where is x is max(length_of_MODULUS, length_of_BASE)
|
// where is x is max(length_of_MODULUS, length_of_BASE)
|
||||||
func modexpMultComplexity(x *big.Int) *big.Int {
|
func modexpMultComplexity(x *big.Int) *big.Int {
|
||||||
|
|
@ -379,15 +379,22 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) {
|
||||||
}
|
}
|
||||||
// Retrieve the operands and execute the exponentiation
|
// Retrieve the operands and execute the exponentiation
|
||||||
var (
|
var (
|
||||||
base = new(big.Int).SetBytes(getData(input, 0, baseLen))
|
base = new(big2.Int).SetBytes(getData(input, 0, baseLen))
|
||||||
exp = new(big.Int).SetBytes(getData(input, baseLen, expLen))
|
exp = new(big2.Int).SetBytes(getData(input, baseLen, expLen))
|
||||||
mod = new(big.Int).SetBytes(getData(input, baseLen+expLen, modLen))
|
mod = new(big2.Int).SetBytes(getData(input, baseLen+expLen, modLen))
|
||||||
|
v []byte
|
||||||
)
|
)
|
||||||
if mod.BitLen() == 0 {
|
switch {
|
||||||
|
case mod.BitLen() == 0:
|
||||||
// Modulo 0 is undefined, return zero
|
// Modulo 0 is undefined, return zero
|
||||||
return common.LeftPadBytes([]byte{}, int(modLen)), nil
|
return common.LeftPadBytes([]byte{}, int(modLen)), nil
|
||||||
|
case base.BitLen() == 1: // a bit length of 1 means it's 1 (or -1).
|
||||||
|
//If base == 1, then we can just return base % mod (if mod >= 1, which it is)
|
||||||
|
v = base.Mod(base, mod).Bytes()
|
||||||
|
default:
|
||||||
|
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,
|
// newCurvePoint unmarshals a binary blob into a bn256 elliptic curve point,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue